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, CtrlFlat &rCtrl,
37 GenericLayout &rLayout, const UString &rHelp,
38 VarBool *pVisible, Direction_t direction ):
39 CtrlFlat( pIntf, rHelp, pVisible ), m_fsm( pIntf ), m_rCtrl( rCtrl ),
40 m_rLayout( rLayout ), m_direction( direction ), m_cmdOutStill( this ),
41 m_cmdStillOut( this ),
42 m_cmdStillStill( this ),
43 m_cmdStillResize( this ),
44 m_cmdResizeStill( this ),
45 m_cmdResizeResize( this )
52 m_fsm.addState( "out" );
53 m_fsm.addState( "still" );
54 m_fsm.addState( "resize" );
57 m_fsm.addTransition( "out", "enter", "still", &m_cmdOutStill );
58 m_fsm.addTransition( "still", "leave", "out", &m_cmdStillOut );
59 m_fsm.addTransition( "still", "motion", "still", &m_cmdStillStill );
60 m_fsm.addTransition( "resize", "mouse:left:up:none", "still",
62 m_fsm.addTransition( "still", "mouse:left:down:none", "resize",
64 m_fsm.addTransition( "resize", "motion", "resize", &m_cmdResizeResize );
66 m_fsm.setState( "still" );
70 bool CtrlResize::mouseOver( int x, int y ) const
72 return m_rCtrl.mouseOver( x, y );
76 void CtrlResize::draw( OSGraphics &rImage, int xDest, int yDest )
78 m_rCtrl.draw( rImage, xDest, yDest );
82 void CtrlResize::setLayout( GenericLayout *pLayout, const Position &rPosition )
84 CtrlGeneric::setLayout( pLayout, rPosition );
85 // Set the layout of the decorated control as well
86 m_rCtrl.setLayout( pLayout, rPosition );
90 const Position *CtrlResize::getPosition() const
92 return m_rCtrl.getPosition();
96 void CtrlResize::handleEvent( EvtGeneric &rEvent )
99 m_fsm.handleTransition( rEvent.getAsString() );
100 // Transmit the event to the decorated control
101 // XXX: Is it really a good idea?
102 m_rCtrl.handleEvent( rEvent );
106 void CtrlResize::changeCursor( Direction_t direction ) const
108 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
109 if( direction == kResizeSE )
110 pOsFactory->changeCursor( OSFactory::kResizeNWSE );
111 else if( direction == kResizeS )
112 pOsFactory->changeCursor( OSFactory::kResizeNS );
113 else if( direction == kResizeE )
114 pOsFactory->changeCursor( OSFactory::kResizeWE );
115 else if( direction == kNone )
116 pOsFactory->changeCursor( OSFactory::kDefaultArrow );
120 void CtrlResize::CmdOutStill::execute()
122 m_pParent->changeCursor( m_pParent->m_direction );
126 void CtrlResize::CmdStillOut::execute()
128 m_pParent->changeCursor( kNone );
132 void CtrlResize::CmdStillStill::execute()
134 m_pParent->changeCursor( m_pParent->m_direction );
138 void CtrlResize::CmdStillResize::execute()
140 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
143 m_pParent->changeCursor( m_pParent->m_direction );
145 m_pParent->m_xPos = pEvtMouse->getXPos();
146 m_pParent->m_yPos = pEvtMouse->getYPos();
148 m_pParent->captureMouse();
150 m_pParent->m_width = m_pParent->m_rLayout.getWidth();
151 m_pParent->m_height = m_pParent->m_rLayout.getHeight();
155 void CtrlResize::CmdResizeStill::execute()
158 m_pParent->changeCursor( m_pParent->m_direction );
160 m_pParent->releaseMouse();
164 void CtrlResize::CmdResizeResize::execute()
166 EvtMotion *pEvtMotion = (EvtMotion*)m_pParent->m_pEvt;
169 m_pParent->changeCursor( m_pParent->m_direction );
171 int newWidth = m_pParent->m_width;
172 int newHeight = m_pParent->m_height;
173 if( m_pParent->m_direction != kResizeS )
174 newWidth += pEvtMotion->getXPos() - m_pParent->m_xPos;
175 if( m_pParent->m_direction != kResizeE )
176 newHeight += pEvtMotion->getYPos() - m_pParent->m_yPos;
178 // Create a resize command
179 CmdGeneric *pCmd = new CmdResize( m_pParent->getIntf(),
180 m_pParent->m_rLayout,
181 newWidth, newHeight );
182 // Push the command in the asynchronous command queue
183 AsyncQueue *pQueue = AsyncQueue::instance( m_pParent->getIntf() );
184 pQueue->push( CmdGenericPtr( pCmd ) );