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., 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 1
39 #define MOVING_TEXT_DELAY 30
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 ),
48 m_cmdManualMoving( this ), m_cmdManualStill( this ),
49 m_cmdMove( this ), m_pEvt( NULL ), m_rFont( rFont ),
50 m_color( color ), m_pImg( NULL ), m_pImgDouble( NULL ),
51 m_pCurrImg( NULL ), m_xPos( 0 ), m_xOffset( 0 ),
52 m_cmdUpdateText( this )
54 m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
57 m_fsm.addState( "still" );
58 m_fsm.addState( "moving" );
59 m_fsm.addState( "manual1" );
60 m_fsm.addState( "manual2" );
61 m_fsm.addState( "outStill" );
62 m_fsm.addState( "outMoving" );
65 m_fsm.addTransition( "still", "mouse:left:down", "manual1",
67 m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
69 m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
71 m_fsm.addTransition( "manual2", "mouse:left:up", "still",
73 m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
74 m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
75 m_fsm.addTransition( "still", "leave", "outStill" );
76 m_fsm.addTransition( "outStill", "enter", "still" );
77 m_fsm.addTransition( "moving", "leave", "outMoving" );
78 m_fsm.addTransition( "outMoving", "enter", "moving" );
81 m_fsm.setState( "moving" );
83 // Observe the variable
84 m_rVariable.addObserver( this );
87 displayText( m_rVariable.get() );
93 m_rVariable.delObserver( this );
109 void CtrlText::handleEvent( EvtGeneric &rEvent )
111 // Save the event to use it in callbacks
114 m_fsm.handleTransition( rEvent.getAsString() );
118 bool CtrlText::mouseOver( int x, int y ) const
122 // We have 3 different ways of deciding when to return true here:
123 // 1) the mouse is exactly over the text (so if you click between two
124 // letters, the text control doesn't catch the event)
125 // 2) the mouse is over the rectangle of the control
126 // 3) the mouse is over the rectangle of the visible text
127 // I don't know which one is the best...
129 return( x >= 0 && x < getPosition()->getWidth()
130 && m_pCurrImg->hit( x - m_xPos, y ) );
133 return( x >= 0 && x < getPosition()->getWidth()
134 && y >= 0 && y < getPosition()->getHeight() );
137 return( x >= 0 && x < getPosition()->getWidth()
138 && y >= 0 && y < getPosition()->getHeight()
139 && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
149 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
153 // Compute the dimensions to draw
154 int width = min( m_pCurrImg->getWidth() + m_xPos,
155 getPosition()->getWidth() );
156 int height = min( m_pCurrImg->getHeight(), getPosition()->getHeight() );
157 // Draw the current image
158 if( width > 0 && height > 0 )
160 rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest, yDest,
161 width, height, true );
167 void CtrlText::setText( const UString &rText, uint32_t color )
170 if( color != 0xFFFFFFFF )
176 m_rVariable.set( rText );
180 void CtrlText::onUpdate( Subject<VarText> &rVariable )
182 displayText( m_rVariable.get() );
186 void CtrlText::displayText( const UString &rText )
188 // Create the images ('normal' and 'double') from the text
194 m_pImg = m_rFont.drawString( rText, m_color );
200 const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
205 m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
207 // Update the current image used, as if the control size had changed
213 // If the control was in the moving state, check if the scrolling is
215 const string &rState = m_fsm.getState();
216 if( rState == "moving" || rState == "outMoving" )
218 if( m_pImg && m_pImg->getWidth() >= getPosition()->getWidth() )
220 m_pCurrImg = m_pImgDouble;
221 m_pTimer->start( MOVING_TEXT_DELAY, false );
228 notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
233 void CtrlText::onChangePosition()
235 if( m_pImg && getPosition() )
237 if( m_pImg->getWidth() < getPosition()->getWidth() )
243 m_pCurrImg = m_pImgDouble;
248 // m_pImg is a better default value than m_pImgDouble, but anyway we
249 // don't care because the control is never drawn without position :)
255 void CtrlText::CmdToManual::execute()
257 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
259 // Compute the offset
260 m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
262 m_pParent->m_pTimer->stop();
263 m_pParent->captureMouse();
267 void CtrlText::CmdManualMoving::execute()
269 m_pParent->releaseMouse();
271 // Start the automatic movement, but only if the text is wider than the
273 if( m_pParent->m_pImg &&
274 m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
276 // The current image may have been set incorrectly in displayText(), so
277 // set the correct value
278 m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
280 m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
285 void CtrlText::CmdManualStill::execute()
287 m_pParent->releaseMouse();
291 void CtrlText::CmdMove::execute()
293 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
295 // Do nothing if the text fits in the control
296 if( m_pParent->m_pImg &&
297 m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
299 // The current image may have been set incorrectly in displayText(), so
300 // we set the correct value
301 m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
303 // Compute the new position of the left side, and make sure it is
304 // in the correct range
305 m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
306 m_pParent->adjust( m_pParent->m_xPos );
308 m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
309 m_pParent->getPosition()->getHeight() );
314 void CtrlText::CmdUpdateText::execute()
316 m_pParent->m_xPos -= MOVING_TEXT_STEP;
317 m_pParent->adjust( m_pParent->m_xPos );
319 m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
320 m_pParent->getPosition()->getHeight() );
324 void CtrlText::adjust( int &position )
326 // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
327 // bitmap; remember that the string used to generate m_pImgDouble is of the
328 // form: "foo foo", the number of spaces being a parameter
333 position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
336 position -= m_pImgDouble->getWidth() - m_pImg->getWidth();