]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_resize.cpp
Qt4 - should fix the double-free and the freezing on quit. Ref #1348
[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
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 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 const Position *CtrlResize::getPosition() const
93 {
94     return m_rCtrl.getPosition();
95 }
96
97
98 void CtrlResize::onResize()
99 {
100     m_rCtrl.onResize();
101 }
102
103
104 void CtrlResize::handleEvent( EvtGeneric &rEvent )
105 {
106     m_pEvt = &rEvent;
107     m_fsm.handleTransition( rEvent.getAsString() );
108     // Transmit the event to the decorated control
109     // XXX: Is it really a good idea?
110     m_rCtrl.handleEvent( rEvent );
111 }
112
113
114 void CtrlResize::changeCursor( WindowManager::Direction_t direction ) const
115 {
116     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
117     if( direction == WindowManager::kResizeSE )
118         pOsFactory->changeCursor( OSFactory::kResizeNWSE );
119     else if( direction == WindowManager::kResizeS )
120         pOsFactory->changeCursor( OSFactory::kResizeNS );
121     else if( direction == WindowManager::kResizeE )
122         pOsFactory->changeCursor( OSFactory::kResizeWE );
123     else if( direction == WindowManager::kNone )
124         pOsFactory->changeCursor( OSFactory::kDefaultArrow );
125 }
126
127
128 void CtrlResize::CmdOutStill::execute()
129 {
130     m_pParent->changeCursor( m_pParent->m_direction );
131 }
132
133
134 void CtrlResize::CmdStillOut::execute()
135 {
136     m_pParent->changeCursor( WindowManager::kNone );
137 }
138
139
140 void CtrlResize::CmdStillStill::execute()
141 {
142     m_pParent->changeCursor( m_pParent->m_direction );
143 }
144
145
146 void CtrlResize::CmdStillResize::execute()
147 {
148     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
149
150     // Set the cursor
151     m_pParent->changeCursor( m_pParent->m_direction );
152
153     m_pParent->m_xPos = pEvtMouse->getXPos();
154     m_pParent->m_yPos = pEvtMouse->getYPos();
155
156     m_pParent->captureMouse();
157
158     m_pParent->m_width = m_pParent->m_rLayout.getWidth();
159     m_pParent->m_height = m_pParent->m_rLayout.getHeight();
160
161     m_pParent->m_rWindowManager.startResize( m_pParent->m_rLayout,
162                                              m_pParent->m_direction);
163 }
164
165
166 void CtrlResize::CmdResizeStill::execute()
167 {
168     // Set the cursor
169     m_pParent->changeCursor( m_pParent->m_direction );
170
171     m_pParent->releaseMouse();
172
173     m_pParent->m_rWindowManager.stopResize();
174 }
175
176
177 void CtrlResize::CmdResizeResize::execute()
178 {
179     EvtMotion *pEvtMotion = (EvtMotion*)m_pParent->m_pEvt;
180
181     // Set the cursor
182     m_pParent->changeCursor( m_pParent->m_direction );
183
184     int newWidth = m_pParent->m_width;
185     newWidth += pEvtMotion->getXPos() - m_pParent->m_xPos;
186     int newHeight = m_pParent->m_height;
187     newHeight += pEvtMotion->getYPos() - m_pParent->m_yPos;
188
189     // Create a resize command, instead of calling the window manager directly.
190     // Thanks to this trick, the duplicate resizing commands will be trashed
191     // in the asynchronous queue, thus making resizing faster
192     CmdGeneric *pCmd = new CmdResize( m_pParent->getIntf(),
193                                       m_pParent->m_rWindowManager,
194                                       m_pParent->m_rLayout,
195                                       newWidth, newHeight );
196     // Push the command in the asynchronous command queue
197     AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
198     pQueue->push( CmdGenericPtr( pCmd ) );
199 }