1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "ctrl_text.hpp"
26 #include "../events/evt_generic.hpp"
27 #include "../events/evt_mouse.hpp"
28 #include "../src/generic_bitmap.hpp"
29 #include "../src/generic_font.hpp"
30 #include "../src/os_factory.hpp"
31 #include "../src/os_graphics.hpp"
32 #include "../src/os_timer.hpp"
33 #include "../utils/position.hpp"
34 #include "../utils/ustring.hpp"
35 #include "../utils/var_text.hpp"
38 #define MOVING_TEXT_STEP 3
39 #define MOVING_TEXT_DELAY 200
40 #define SEPARATOR_STRING " "
43 CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
44 const GenericFont &rFont, const UString &rHelp,
45 uint32_t color, VarBool *pVisible ):
46 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
47 m_rVariable( rVariable ), m_cmdToManual( this, &transToManual ),
48 m_cmdManualMoving( this, &transManualMoving ),
49 m_cmdManualStill( this, &transManualStill ),
50 m_cmdMove( this, &transMove ),
51 m_pEvt( NULL ), m_rFont( rFont ), m_color( color ),
52 m_pImg( NULL ), m_pImgDouble( NULL ), m_pCurrImg( NULL ),
53 m_xPos( 0 ), m_xOffset( 0 )
55 m_pTimer = OSFactory::instance( getIntf() )->createOSTimer(
56 Callback( this, &updateText ) );
59 m_fsm.addState( "still" );
60 m_fsm.addState( "moving" );
61 m_fsm.addState( "manual1" );
62 m_fsm.addState( "manual2" );
63 m_fsm.addState( "outStill" );
64 m_fsm.addState( "outMoving" );
67 m_fsm.addTransition( "still", "mouse:left:down", "manual1",
69 m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
71 m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
73 m_fsm.addTransition( "manual2", "mouse:left:up", "still",
75 m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
76 m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
77 m_fsm.addTransition( "still", "leave", "outStill" );
78 m_fsm.addTransition( "outStill", "enter", "still" );
79 m_fsm.addTransition( "moving", "leave", "outMoving" );
80 m_fsm.addTransition( "outMoving", "enter", "moving" );
83 m_fsm.setState( "outStill" );
85 // Observe the variable
86 m_rVariable.addObserver( this );
89 displayText( m_rVariable.get() );
95 m_rVariable.delObserver( this );
111 void CtrlText::handleEvent( EvtGeneric &rEvent )
113 // Save the event to use it in callbacks
116 m_fsm.handleTransition( rEvent.getAsString() );
120 bool CtrlText::mouseOver( int x, int y ) const
124 // We have 3 different ways of deciding when to return true here:
125 // 1) the mouse is exactly over the text (so if you click between two
126 // letters, the text control doesn't catch the event)
127 // 2) the mouse is over the rectangle of the control
128 // 3) the mouse is over the rectangle of the visible text
129 // I don't know which one is the best...
131 return( x >= 0 && x < getPosition()->getWidth()
132 && m_pCurrImg->hit( x - m_xPos, y ) );
135 return( x >= 0 && x < getPosition()->getWidth()
136 && y >= 0 && y < getPosition()->getHeight() );
139 return( x >= 0 && x < getPosition()->getWidth()
140 && y >= 0 && y < getPosition()->getHeight()
141 && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
151 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
155 // Compute the dimensions to draw
156 int width = min( m_pCurrImg->getWidth() + m_xPos,
157 getPosition()->getWidth() );
158 int height = min( m_pCurrImg->getHeight(), getPosition()->getHeight() );
159 // Draw the current image
160 rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest, yDest,
166 void CtrlText::setText( const UString &rText, uint32_t color )
169 if( color != 0xFFFFFFFF )
175 m_rVariable.set( rText );
179 void CtrlText::onUpdate( Subject<VarText> &rVariable )
181 displayText( m_rVariable.get() );
185 void CtrlText::displayText( const UString &rText )
187 // Create the images ('normal' and 'double') from the text
193 m_pImg = m_rFont.drawString( rText, m_color );
199 const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
204 m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
206 // Update the current image used, as if the control size had changed
213 void CtrlText::onChangePosition()
215 if( m_pImg && getPosition() )
217 if( m_pImg->getWidth() < getPosition()->getWidth() )
223 m_pCurrImg = m_pImgDouble;
228 // m_pImg is a better default value than m_pImgDouble, but anyway we
229 // don't care because the control is never drawn without position :)
235 void CtrlText::transToManual( SkinObject *pCtrl )
237 CtrlText *pThis = (CtrlText*)pCtrl;
238 EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
240 // Compute the offset
241 pThis->m_xOffset = pEvtMouse->getXPos() - pThis->m_xPos;
243 pThis->m_pTimer->stop();
244 pThis->captureMouse();
248 void CtrlText::transManualMoving( SkinObject *pCtrl )
250 CtrlText *pThis = (CtrlText*)pCtrl;
251 pThis->releaseMouse();
253 // Start the automatic movement, but only if the text is wider than the
256 pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
258 // The current image may have been set incorrectly in displayText(), so
259 // set the correct value
260 pThis->m_pCurrImg = pThis->m_pImgDouble;
262 pThis->m_pTimer->start( MOVING_TEXT_DELAY, false );
267 void CtrlText::transManualStill( SkinObject *pCtrl )
269 CtrlText *pThis = (CtrlText*)pCtrl;
270 pThis->releaseMouse();
274 void CtrlText::transMove( SkinObject *pCtrl )
276 CtrlText *pThis = (CtrlText*)pCtrl;
277 EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
279 // Do nothing if the text fits in the control
281 pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
283 // The current image may have been set incorrectly in displayText(), so
284 // we set the correct value
285 pThis->m_pCurrImg = pThis->m_pImgDouble;
287 // Compute the new position of the left side, and make sure it is
288 // in the correct range
289 pThis->m_xPos = (pEvtMouse->getXPos() - pThis->m_xOffset);
290 pThis->adjust( pThis->m_xPos );
292 pThis->notifyLayout();
297 void CtrlText::updateText( SkinObject *pCtrl )
299 CtrlText *pThis = (CtrlText*)pCtrl;
301 pThis->m_xPos -= MOVING_TEXT_STEP;
302 pThis->adjust( pThis->m_xPos );
304 pThis->notifyLayout();
308 void CtrlText::adjust( int &position )
310 // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
311 // bitmap; remember that the string used to generate m_pImgDouble is of the
312 // form: "foo foo", the number of spaces being a parameter
317 position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
320 position -= m_pImgDouble->getWidth() - m_pImg->getWidth();