1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
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"
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 )
54 m_fsm.addState( "out" );
55 m_fsm.addState( "still" );
56 m_fsm.addState( "resize" );
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",
64 m_fsm.addTransition( "still", "mouse:left:down:none", "resize",
66 m_fsm.addTransition( "resize", "motion", "resize", &m_cmdResizeResize );
68 m_fsm.setState( "still" );
72 bool CtrlResize::mouseOver( int x, int y ) const
74 return m_rCtrl.mouseOver( x, y );
78 void CtrlResize::draw( OSGraphics &rImage, int xDest, int yDest )
80 m_rCtrl.draw( rImage, xDest, yDest );
84 void CtrlResize::setLayout( GenericLayout *pLayout, const Position &rPosition )
86 CtrlGeneric::setLayout( pLayout, rPosition );
87 // Set the layout of the decorated control as well
88 m_rCtrl.setLayout( pLayout, rPosition );
92 const Position *CtrlResize::getPosition() const
94 return m_rCtrl.getPosition();
98 void CtrlResize::onResize()
104 void CtrlResize::handleEvent( EvtGeneric &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 );
114 void CtrlResize::changeCursor( WindowManager::Direction_t direction ) const
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 );
128 void CtrlResize::CmdOutStill::execute()
130 m_pParent->changeCursor( m_pParent->m_direction );
134 void CtrlResize::CmdStillOut::execute()
136 m_pParent->changeCursor( WindowManager::kNone );
140 void CtrlResize::CmdStillStill::execute()
142 m_pParent->changeCursor( m_pParent->m_direction );
146 void CtrlResize::CmdStillResize::execute()
148 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
151 m_pParent->changeCursor( m_pParent->m_direction );
153 m_pParent->m_xPos = pEvtMouse->getXPos();
154 m_pParent->m_yPos = pEvtMouse->getYPos();
156 m_pParent->captureMouse();
158 m_pParent->m_width = m_pParent->m_rLayout.getWidth();
159 m_pParent->m_height = m_pParent->m_rLayout.getHeight();
161 m_pParent->m_rWindowManager.startResize( m_pParent->m_rLayout,
162 m_pParent->m_direction);
166 void CtrlResize::CmdResizeStill::execute()
169 m_pParent->changeCursor( m_pParent->m_direction );
171 m_pParent->releaseMouse();
173 m_pParent->m_rWindowManager.stopResize();
177 void CtrlResize::CmdResizeResize::execute()
179 EvtMotion *pEvtMotion = (EvtMotion*)m_pParent->m_pEvt;
182 m_pParent->changeCursor( m_pParent->m_direction );
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;
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 ) );