]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_resize.cpp
Do not include vlc_modules.h in vlc_common.h
[vlc] / modules / gui / skins2 / controls / ctrl_resize.cpp
1 /*****************************************************************************
2  * ctrl_resize.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "ctrl_resize.hpp"
26 #include "../events/evt_generic.hpp"
27 #include "../events/evt_mouse.hpp"
28 #include "../events/evt_motion.hpp"
29 #include "../src/generic_layout.hpp"
30 #include "../src/os_factory.hpp"
31 #include "../utils/position.hpp"
32 #include "../commands/async_queue.hpp"
33 #include "../commands/cmd_resize.hpp"
34
35
36 CtrlResize::CtrlResize( intf_thread_t *pIntf, WindowManager &rWindowManager,
37                         CtrlFlat &rCtrl, GenericLayout &rLayout,
38                         const UString &rHelp, VarBool *pVisible,
39                         WindowManager::Direction_t direction ):
40     CtrlFlat( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
41     m_rWindowManager( rWindowManager ), m_rCtrl( rCtrl ),
42     m_rLayout( rLayout ), m_direction( direction ),  m_cmdOutStill( this ),
43     m_cmdStillOut( this ),
44     m_cmdStillStill( this ),
45     m_cmdStillResize( this ),
46     m_cmdResizeStill( this ),
47     m_cmdResizeResize( this )
48 {
49     m_pEvt = NULL;
50     m_xPos = 0;
51     m_yPos = 0;
52
53     // States
54     m_fsm.addState( "out" );
55     m_fsm.addState( "still" );
56     m_fsm.addState( "resize" );
57
58     // Transitions
59     m_fsm.addTransition( "out", "enter", "still", &m_cmdOutStill );
60     m_fsm.addTransition( "still", "leave", "out", &m_cmdStillOut );
61     m_fsm.addTransition( "still", "motion", "still", &m_cmdStillStill );
62     m_fsm.addTransition( "resize", "mouse:left:up:none", "still",
63                          &m_cmdResizeStill );
64     m_fsm.addTransition( "still", "mouse:left:down:none", "resize",
65                          &m_cmdStillResize );
66     m_fsm.addTransition( "resize", "motion", "resize", &m_cmdResizeResize );
67
68     m_fsm.setState( "still" );
69 }
70
71
72 bool CtrlResize::mouseOver( int x, int y ) const
73 {
74     return m_rCtrl.mouseOver( x, y );
75 }
76
77
78 void CtrlResize::draw( OSGraphics &rImage, int xDest, int yDest )
79 {
80     m_rCtrl.draw( rImage, xDest, yDest );
81 }
82
83
84 void CtrlResize::setLayout( GenericLayout *pLayout, const Position &rPosition )
85 {
86     CtrlGeneric::setLayout( pLayout, rPosition );
87     // Set the layout of the decorated control as well
88     m_rCtrl.setLayout( pLayout, rPosition );
89 }
90
91
92 void CtrlResize::unsetLayout()
93 {
94     m_rCtrl.unsetLayout();
95     CtrlGeneric::unsetLayout();
96 }
97
98
99 const Position *CtrlResize::getPosition() const
100 {
101     return m_rCtrl.getPosition();
102 }
103
104
105 void CtrlResize::onResize()
106 {
107     m_rCtrl.onResize();
108 }
109
110
111 void CtrlResize::handleEvent( EvtGeneric &rEvent )
112 {
113     m_pEvt = &rEvent;
114     m_fsm.handleTransition( rEvent.getAsString() );
115     // Transmit the event to the decorated control
116     // XXX: Is it really a good idea?
117     m_rCtrl.handleEvent( rEvent );
118 }
119
120
121 void CtrlResize::changeCursor( WindowManager::Direction_t direction ) const
122 {
123     OSFactory::CursorType_t cursor;
124     switch( direction )
125     {
126     default:
127     case WindowManager::kNone:     cursor = OSFactory::kDefaultArrow; break;
128     case WindowManager::kResizeSE: cursor = OSFactory::kResizeNWSE;   break;
129     case WindowManager::kResizeS:  cursor = OSFactory::kResizeNS;     break;
130     case WindowManager::kResizeE:  cursor = OSFactory::kResizeWE;     break;
131     }
132     OSFactory::instance( getIntf() )->changeCursor( cursor );
133 }
134
135
136 void CtrlResize::CmdOutStill::execute()
137 {
138     m_pParent->changeCursor( m_pParent->m_direction );
139 }
140
141
142 void CtrlResize::CmdStillOut::execute()
143 {
144     m_pParent->changeCursor( WindowManager::kNone );
145 }
146
147
148 void CtrlResize::CmdStillStill::execute()
149 {
150     m_pParent->changeCursor( m_pParent->m_direction );
151 }
152
153
154 void CtrlResize::CmdStillResize::execute()
155 {
156     EvtMouse *pEvtMouse = static_cast<EvtMouse*>(m_pParent->m_pEvt);
157
158     // Set the cursor
159     m_pParent->changeCursor( m_pParent->m_direction );
160
161     m_pParent->m_xPos = pEvtMouse->getXPos();
162     m_pParent->m_yPos = pEvtMouse->getYPos();
163
164     m_pParent->captureMouse();
165
166     m_pParent->m_width = m_pParent->m_rLayout.getWidth();
167     m_pParent->m_height = m_pParent->m_rLayout.getHeight();
168
169     m_pParent->m_rWindowManager.startResize( m_pParent->m_rLayout,
170                                              m_pParent->m_direction);
171 }
172
173
174 void CtrlResize::CmdResizeStill::execute()
175 {
176     // Set the cursor
177     m_pParent->changeCursor( m_pParent->m_direction );
178
179     m_pParent->releaseMouse();
180
181     m_pParent->m_rWindowManager.stopResize();
182 }
183
184
185 void CtrlResize::CmdResizeResize::execute()
186 {
187     EvtMotion *pEvtMotion = static_cast<EvtMotion*>(m_pParent->m_pEvt);
188
189     m_pParent->changeCursor( m_pParent->m_direction );
190
191     int newWidth = m_pParent->m_width;
192     newWidth += pEvtMotion->getXPos() - m_pParent->m_xPos;
193     int newHeight = m_pParent->m_height;
194     newHeight += pEvtMotion->getYPos() - m_pParent->m_yPos;
195
196     // Create a resize command, instead of calling the window manager directly.
197     // Thanks to this trick, the duplicate resizing commands will be trashed
198     // in the asynchronous queue, thus making resizing faster
199     CmdGeneric *pCmd = new CmdResize( m_pParent->getIntf(),
200                                       m_pParent->m_rWindowManager,
201                                       m_pParent->m_rLayout,
202                                       newWidth, newHeight );
203     // Push the command in the asynchronous command queue
204     AsyncQueue::instance( getIntf() )->push( CmdGenericPtr( pCmd ) );
205 }