]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_resize.cpp
3cb5512e71fb955db1a47192a4cf767d95040ea0
[vlc] / modules / gui / skins2 / controls / ctrl_resize.cpp
1 /*****************************************************************************
2  * ctrl_resize.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: ctrl_resize.cpp,v 1.1 2004/01/03 23:31:33 asmax Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 "../utils/position.hpp"
31 #include "../commands/async_queue.hpp"
32 #include "../commands/cmd_resize.hpp"
33
34
35 CtrlResize::CtrlResize( intf_thread_t *pIntf, CtrlFlat &rCtrl,
36                         GenericLayout &rLayout, const UString &rHelp ):
37     CtrlFlat( pIntf, rHelp ), m_fsm( pIntf ), m_rCtrl( rCtrl ),
38     m_rLayout( rLayout ), m_cmdResizeResize( this, &transResizeResize ),
39     m_cmdStillResize( this, &transStillResize ),
40     m_cmdResizeStill( this, &transResizeStill )
41 {
42     m_pEvt = NULL;
43     m_xPos = 0;
44     m_yPos = 0;
45
46     // States
47     m_fsm.addState( "resize" );
48     m_fsm.addState( "still" );
49
50     // Transitions
51     m_fsm.addTransition( "resize", "mouse:left:up:none", "still",
52                          &m_cmdResizeStill );
53     m_fsm.addTransition( "still", "mouse:left:down:none", "resize",
54                          &m_cmdStillResize );
55     m_fsm.addTransition( "resize", "motion", "resize", &m_cmdResizeResize );
56
57     m_fsm.setState( "still" );
58 }
59
60
61 bool CtrlResize::mouseOver( int x, int y ) const
62 {
63     return m_rCtrl.mouseOver( x, y );
64 }
65
66
67 void CtrlResize::draw( OSGraphics &rImage, int xDest, int yDest )
68 {
69     m_rCtrl.draw( rImage, xDest, yDest );
70 }
71
72
73 void CtrlResize::setLayout( GenericLayout *pLayout, const Position &rPosition )
74 {
75     CtrlGeneric::setLayout( pLayout, rPosition );
76     // Set the layout of the decorated control as well
77     m_rCtrl.setLayout( pLayout, rPosition );
78 }
79
80
81 const Position *CtrlResize::getPosition() const
82 {
83     return m_rCtrl.getPosition();
84 }
85
86
87 void CtrlResize::handleEvent( EvtGeneric &rEvent )
88 {
89     m_pEvt = &rEvent;
90     m_fsm.handleTransition( rEvent.getAsString() );
91     // Transmit the event to the decorated control
92     // XXX: Is it really a good idea?
93     m_rCtrl.handleEvent( rEvent );
94 }
95
96
97 void CtrlResize::transStillResize( SkinObject *pCtrl )
98 {
99     CtrlResize *pThis = (CtrlResize*)pCtrl;
100     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
101
102     pThis->m_xPos = pEvtMouse->getXPos();
103     pThis->m_yPos = pEvtMouse->getYPos();
104
105     pThis->captureMouse();
106
107     pThis->m_width = pThis->m_rLayout.getWidth();
108     pThis->m_height = pThis->m_rLayout.getHeight();
109 }
110
111
112 void CtrlResize::transResizeResize( SkinObject *pCtrl )
113 {
114     CtrlResize *pThis = (CtrlResize*)pCtrl;
115     EvtMotion *pEvtMotion = (EvtMotion*)pThis->m_pEvt;
116
117     int newWidth = pEvtMotion->getXPos() - pThis->m_xPos + pThis->m_width;
118     int newHeight = pEvtMotion->getYPos() - pThis->m_yPos + pThis->m_height;
119
120     // Check boundaries
121     if( newWidth < pThis->m_rLayout.getMinWidth() )
122     {
123         newWidth = pThis->m_rLayout.getMinWidth();
124     }
125     if( newWidth > pThis->m_rLayout.getMaxWidth() )
126     {
127         newWidth = pThis->m_rLayout.getMaxWidth();
128     }
129     if( newHeight < pThis->m_rLayout.getMinHeight() )
130     {
131         newHeight = pThis->m_rLayout.getMinHeight();
132     }
133     if( newHeight > pThis->m_rLayout.getMaxHeight() )
134     {
135         newHeight = pThis->m_rLayout.getMaxHeight();
136     }
137
138     // Create a resize command
139     CmdGeneric *pCmd = new CmdResize( pThis->getIntf(), pThis->m_rLayout,
140                                       newWidth, newHeight );
141     // Push the command in the asynchronous command queue
142     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
143     pQueue->remove( "resize" );
144     pQueue->push( CmdGenericPtr( pCmd ) );
145 }
146
147
148 void CtrlResize::transResizeStill( SkinObject *pCtrl )
149 {
150     CtrlResize *pThis = (CtrlResize*)pCtrl;
151
152     pThis->releaseMouse();
153 }
154