ctrl_button.cpp 5.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * ctrl_button.cpp
 *****************************************************************************
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 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

#include "ctrl_button.hpp"
#include "../events/evt_generic.hpp"
#include "../src/generic_bitmap.hpp"
#include "../src/os_factory.hpp"
#include "../src/os_graphics.hpp"
#include "../commands/cmd_generic.hpp"


CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
                        const GenericBitmap &rBmpOver,
                        const GenericBitmap &rBmpDown, CmdGeneric &rCommand,
36 37 38 39
                        const UString &rTooltip, const UString &rHelp,
                        VarBool *pVisible ):
    CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
    m_rCommand( rCommand ), m_tooltip( rTooltip ),
40 41 42 43 44 45
    m_imgUp( pIntf, rBmpUp ), m_imgOver( pIntf, rBmpOver ),
    m_imgDown( pIntf, rBmpDown ), m_pImg( NULL ), m_cmdUpOverDownOver( this ),
    m_cmdDownOverUpOver( this ), m_cmdDownOverDown( this ),
    m_cmdDownDownOver( this ), m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
    m_cmdDownUp( this ), m_cmdUpHidden( this ), m_cmdHiddenUp( this )
{
46 47 48 49 50 51 52 53 54 55
    // States
    m_fsm.addState( "up" );
    m_fsm.addState( "down" );
    m_fsm.addState( "upOver" );
    m_fsm.addState( "downOver" );
    m_fsm.addState( "hidden" );

    // Transitions
    m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
                         &m_cmdUpOverDownOver );
56 57
    m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
                         &m_cmdUpOverDownOver );
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
                         &m_cmdDownOverUpOver );
    m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
    m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
    m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
    m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
    m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
    // XXX: It would be easy to use a "ANY" initial state to handle these
    // four lines in only one. But till now it isn't worthwhile...
    m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
    m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
    m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
    m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
    m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );

    // Initial state
    m_fsm.setState( "up" );
75
    setImage( &m_imgUp );
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 106 107
}


CtrlButton::~CtrlButton()
{
}


void CtrlButton::handleEvent( EvtGeneric &rEvent )
{
    m_fsm.handleTransition( rEvent.getAsString() );
}


bool CtrlButton::mouseOver( int x, int y ) const
{
    if( m_pImg )
    {
        return m_pImg->hit( x, y );
    }
    else
    {
        return false;
    }
}


void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
{
    if( m_pImg )
    {
        // Draw the current image
108 109 110 111 112 113 114 115 116 117 118 119 120 121
        m_pImg->draw( rImage, xDest, yDest );
    }
}


void CtrlButton::setImage( AnimBitmap *pImg )
{
    AnimBitmap *pOldImg = m_pImg;
    m_pImg = pImg;

    if( pOldImg )
    {
        pOldImg->stopAnim();
        pOldImg->delObserver( this );
122
    }
123 124 125 126 127 128 129 130 131 132 133

    if( pImg )
    {
        pImg->startAnim();
        pImg->addObserver( this );
    }

    notifyLayoutMaxSize( pOldImg, pImg );
}


134
void CtrlButton::onUpdate( Subject<AnimBitmap, void*> &rBitmap, void *arg )
135 136
{
    notifyLayout();
137 138 139
}


140
void CtrlButton::CmdUpOverDownOver::execute()
141
{
142
    m_pParent->captureMouse();
143
    m_pParent->setImage( &m_pParent->m_imgDown );
144 145 146
}


147
void CtrlButton::CmdDownOverUpOver::execute()
148
{
149
    m_pParent->releaseMouse();
150
    m_pParent->setImage( &m_pParent->m_imgUp );
151
    // Execute the command associated to this button
152
    m_pParent->m_rCommand.execute();
153 154 155
}


156
void CtrlButton::CmdDownOverDown::execute()
157
{
158
    m_pParent->setImage( &m_pParent->m_imgUp );
159 160 161
}


162
void CtrlButton::CmdDownDownOver::execute()
163
{
164
    m_pParent->setImage( &m_pParent->m_imgDown );
165 166 167
}


168
void CtrlButton::CmdUpUpOver::execute()
169
{
170
    m_pParent->setImage( &m_pParent->m_imgOver );
171 172 173
}


174
void CtrlButton::CmdUpOverUp::execute()
175
{
176
    m_pParent->setImage( &m_pParent->m_imgUp );
177 178 179
}


180
void CtrlButton::CmdDownUp::execute()
181
{
182
    m_pParent->releaseMouse();
183 184 185
}


186
void CtrlButton::CmdUpHidden::execute()
187
{
188
    m_pParent->setImage( NULL );
189 190 191
}


192
void CtrlButton::CmdHiddenUp::execute()
193
{
194
    m_pParent->setImage( &m_pParent->m_imgUp );
195 196
}