ctrl_resize.cpp 5.56 KB
Newer Older
1 2 3
/*****************************************************************************
 * ctrl_resize.cpp
 *****************************************************************************
4
 * Copyright (C) 2003 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *          Olivier Teulire <ipkiss@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "ctrl_resize.hpp"
#include "../events/evt_generic.hpp"
#include "../events/evt_mouse.hpp"
#include "../events/evt_motion.hpp"
#include "../src/generic_layout.hpp"
30
#include "../src/os_factory.hpp"
31 32 33 34 35 36
#include "../utils/position.hpp"
#include "../commands/async_queue.hpp"
#include "../commands/cmd_resize.hpp"


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 42 43 44 45
    m_cmdStillOut( this ),
    m_cmdStillStill( this ),
    m_cmdStillResize( this ),
    m_cmdResizeStill( this ),
    m_cmdResizeResize( this )
46 47 48 49 50 51
{
    m_pEvt = NULL;
    m_xPos = 0;
    m_yPos = 0;

    // States
52
    m_fsm.addState( "out" );
53
    m_fsm.addState( "still" );
54
    m_fsm.addState( "resize" );
55 56

    // Transitions
57 58 59
    m_fsm.addTransition( "out", "enter", "still", &m_cmdOutStill );
    m_fsm.addTransition( "still", "leave", "out", &m_cmdStillOut );
    m_fsm.addTransition( "still", "motion", "still", &m_cmdStillStill );
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    m_fsm.addTransition( "resize", "mouse:left:up:none", "still",
                         &m_cmdResizeStill );
    m_fsm.addTransition( "still", "mouse:left:down:none", "resize",
                         &m_cmdStillResize );
    m_fsm.addTransition( "resize", "motion", "resize", &m_cmdResizeResize );

    m_fsm.setState( "still" );
}


bool CtrlResize::mouseOver( int x, int y ) const
{
    return m_rCtrl.mouseOver( x, y );
}


void CtrlResize::draw( OSGraphics &rImage, int xDest, int yDest )
{
    m_rCtrl.draw( rImage, xDest, yDest );
}


void CtrlResize::setLayout( GenericLayout *pLayout, const Position &rPosition )
{
    CtrlGeneric::setLayout( pLayout, rPosition );
    // Set the layout of the decorated control as well
    m_rCtrl.setLayout( pLayout, rPosition );
}


const Position *CtrlResize::getPosition() const
{
    return m_rCtrl.getPosition();
}


void CtrlResize::handleEvent( EvtGeneric &rEvent )
{
    m_pEvt = &rEvent;
    m_fsm.handleTransition( rEvent.getAsString() );
    // Transmit the event to the decorated control
    // XXX: Is it really a good idea?
    m_rCtrl.handleEvent( rEvent );
}


106 107 108 109 110 111 112 113 114 115 116 117 118 119
void CtrlResize::changeCursor( Direction_t direction ) const
{
    OSFactory *pOsFactory = OSFactory::instance( getIntf() );
    if( direction == kResizeSE )
        pOsFactory->changeCursor( OSFactory::kResizeNWSE );
    else if( direction == kResizeS )
        pOsFactory->changeCursor( OSFactory::kResizeNS );
    else if( direction == kResizeE )
        pOsFactory->changeCursor( OSFactory::kResizeWE );
    else if( direction == kNone )
        pOsFactory->changeCursor( OSFactory::kDefaultArrow );
}


120
void CtrlResize::CmdOutStill::execute()
121
{
122
    m_pParent->changeCursor( m_pParent->m_direction );
123 124 125
}


126
void CtrlResize::CmdStillOut::execute()
127
{
128
    m_pParent->changeCursor( kNone );
129 130 131
}


132
void CtrlResize::CmdStillStill::execute()
133
{
134
    m_pParent->changeCursor( m_pParent->m_direction );
135 136 137
}


138
void CtrlResize::CmdStillResize::execute()
139
{
140
    EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
141

142
    // Set the cursor
143
    m_pParent->changeCursor( m_pParent->m_direction );
144

145 146
    m_pParent->m_xPos = pEvtMouse->getXPos();
    m_pParent->m_yPos = pEvtMouse->getYPos();
147

148
    m_pParent->captureMouse();
149

150 151
    m_pParent->m_width = m_pParent->m_rLayout.getWidth();
    m_pParent->m_height = m_pParent->m_rLayout.getHeight();
152 153 154
}


155
void CtrlResize::CmdResizeStill::execute()
156 157
{
    // Set the cursor
158
    m_pParent->changeCursor( m_pParent->m_direction );
159

160
    m_pParent->releaseMouse();
161 162 163
}


164
void CtrlResize::CmdResizeResize::execute()
165
{
166
    EvtMotion *pEvtMotion = (EvtMotion*)m_pParent->m_pEvt;
167

168
    // Set the cursor
169
    m_pParent->changeCursor( m_pParent->m_direction );
170

171 172 173 174 175 176
    int newWidth = m_pParent->m_width;
    int newHeight = m_pParent->m_height;
    if( m_pParent->m_direction != kResizeS )
        newWidth += pEvtMotion->getXPos() - m_pParent->m_xPos;
    if( m_pParent->m_direction != kResizeE )
        newHeight += pEvtMotion->getYPos() - m_pParent->m_yPos;
177 178

    // Create a resize command
179 180
    CmdGeneric *pCmd = new CmdResize( m_pParent->getIntf(),
                                      m_pParent->m_rLayout,
181 182
                                      newWidth, newHeight );
    // Push the command in the asynchronous command queue
183
    AsyncQueue *pQueue = AsyncQueue::instance( m_pParent->getIntf() );
184 185
    pQueue->push( CmdGenericPtr( pCmd ) );
}