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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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, VarBool *pFocus,
46 Scrolling_t scrollMode, Align_t alignment ):
47 CtrlGeneric( pIntf, rHelp, pVisible ), m_pFocus( pFocus), m_fsm( pIntf ),
48 m_rVariable( rVariable ), m_cmdToManual( this ),
49 m_cmdManualMoving( this ), m_cmdManualStill( this ),
50 m_cmdMove( this ), m_pEvt( NULL ), m_rFont( rFont ),
51 m_color( color ), m_scrollMode( scrollMode ), m_alignment( alignment ),
52 m_pImg( NULL ), m_pImgDouble( NULL ),
53 m_pCurrImg( NULL ), m_xPos( 0 ), m_xOffset( 0 ),
54 m_cmdUpdateText( this )
56 m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
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", "leave", "outStill" );
68 m_fsm.addTransition( "outStill", "enter", "still" );
69 if( m_scrollMode == kManual )
71 m_fsm.addTransition( "still", "mouse:left:down", "manual1",
73 m_fsm.addTransition( "manual1", "mouse:left:up", "still",
75 m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
77 else if( m_scrollMode == kAutomatic )
79 m_fsm.addTransition( "still", "mouse:left:down", "manual1",
81 m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
83 m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
85 m_fsm.addTransition( "manual2", "mouse:left:up", "still",
87 m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
88 m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
89 m_fsm.addTransition( "moving", "leave", "outMoving" );
90 m_fsm.addTransition( "outMoving", "enter", "moving" );
94 m_fsm.setState( "outStill" );
96 // Observe the variable
97 m_rVariable.addObserver( this );
100 displayText( m_rVariable.get() );
104 CtrlText::~CtrlText()
106 m_rVariable.delObserver( this );
113 void CtrlText::handleEvent( EvtGeneric &rEvent )
115 // Save the event to use it in callbacks
118 m_fsm.handleTransition( rEvent.getAsString() );
122 bool CtrlText::mouseOver( int x, int y ) const
124 if( !m_pFocus->get() )
129 // We have 3 different ways of deciding when to return true here:
130 // 1) the mouse is exactly over the text (so if you click between two
131 // letters, the text control doesn't catch the event)
132 // 2) the mouse is over the rectangle of the control
133 // 3) the mouse is over the rectangle of the visible text
134 // I don't know which one is the best...
136 return( x >= 0 && x < getPosition()->getWidth()
137 && m_pCurrImg->hit( x - m_xPos, y ) );
140 return( x >= 0 && x < getPosition()->getWidth()
141 && y >= 0 && y < getPosition()->getHeight() );
144 return( x >= 0 && x < getPosition()->getWidth()
145 && y >= 0 && y < getPosition()->getHeight()
146 && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
156 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
158 rect clip( xDest, yDest, w, h );
159 const Position *pPos = getPosition();
162 // Compute the dimensions to draw
163 int width = min( m_pCurrImg->getWidth() + m_xPos,
164 getPosition()->getWidth() );
165 int height = min( m_pCurrImg->getHeight(), getPosition()->getHeight() );
166 // Draw the current image
167 if( width > 0 && height > 0 )
170 if( m_alignment == kLeft )
172 // We align to the left
175 else if( m_alignment == kRight &&
176 width < getPosition()->getWidth() )
179 // The text is shorter than the width of the control, so we
180 // can align it to the right
181 offset = getPosition()->getWidth() - width;
183 else if( m_alignment == kCenter &&
184 width < getPosition()->getWidth() )
186 // The text is shorter than the width of the control, so we
188 offset = (getPosition()->getWidth() - width) / 2;
190 rect region( pPos->getLeft() + offset,
191 pPos->getTop(), width, height );
193 if( rect::intersect( region, clip, &inter ) )
194 rImage.drawBitmap( *m_pCurrImg, -m_xPos + inter.x - region.x,
197 inter.width, inter.height, true );
203 void CtrlText::setText( const UString &rText, uint32_t color )
206 if( color != 0xFFFFFFFF )
212 m_rVariable.set( rText );
216 void CtrlText::onUpdate( Subject<VarText> &rVariable, void* arg )
220 displayText( m_rVariable.get() );
221 notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
226 void CtrlText::onUpdate( Subject<VarBool> &rVariable, void *arg )
228 // Visibility changed
229 if( &rVariable == m_pVisible )
233 displayText( m_rVariable.get() );
234 notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
244 void CtrlText::displayText( const UString &rText )
246 // Create the images ('normal' and 'double') from the text
249 m_pImg = m_rFont.drawString( rText, m_color );
255 const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
257 m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
259 // Update the current image used, as if the control size had changed
262 if( m_alignment == kRight && getPosition() &&
263 getPosition()->getWidth() < m_pImg->getWidth() )
265 m_xPos = getPosition()->getWidth() - m_pImg->getWidth();
267 else if( m_alignment == kCenter && getPosition() &&
268 getPosition()->getWidth() < m_pImg->getWidth() )
270 m_xPos = (getPosition()->getWidth() - m_pImg->getWidth()) / 2;
279 // If the control was in the moving state, check if the scrolling is
281 const string &rState = m_fsm.getState();
282 if( rState == "moving" || rState == "outMoving" )
284 if( m_pImg && m_pImg->getWidth() >= getPosition()->getWidth() )
286 m_pCurrImg = m_pImgDouble;
287 m_pTimer->start( MOVING_TEXT_DELAY, false );
298 void CtrlText::onPositionChange()
300 if( m_pImg && getPosition() )
302 if( m_pImg->getWidth() < getPosition()->getWidth() )
306 // When the control becomes wide enough for the text to display,
307 // make sure to stop any scrolling effect
313 m_pCurrImg = m_pImgDouble;
318 // m_pImg is a better default value than m_pImgDouble, but anyway we
319 // don't care because the control is never drawn without position :)
325 void CtrlText::onResize()
331 void CtrlText::CmdToManual::execute()
333 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
335 // Compute the offset
336 m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
338 m_pParent->m_pTimer->stop();
339 m_pParent->captureMouse();
343 void CtrlText::CmdManualMoving::execute()
345 m_pParent->releaseMouse();
347 // Start the automatic movement, but only if the text is wider than the
348 // control and if the control can scroll (either in manual or automatic
350 if( m_pParent->m_pImg &&
351 m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
353 // The current image may have been set incorrectly in displayText(), so
354 // set the correct value
355 m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
357 m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
362 void CtrlText::CmdManualStill::execute()
364 m_pParent->releaseMouse();
368 void CtrlText::CmdMove::execute()
370 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
372 // Do nothing if the text fits in the control
373 if( m_pParent->m_pImg &&
374 m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
376 // The current image may have been set incorrectly in displayText(), so
377 // we set the correct value
378 m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
380 // Compute the new position of the left side, and make sure it is
381 // in the correct range
382 m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
383 m_pParent->adjust( m_pParent->m_xPos );
385 m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
386 m_pParent->getPosition()->getHeight() );
391 void CtrlText::CmdUpdateText::execute()
393 m_pParent->m_xPos -= MOVING_TEXT_STEP;
394 m_pParent->adjust( m_pParent->m_xPos );
396 m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
397 m_pParent->getPosition()->getHeight() );
401 void CtrlText::adjust( int &position )
403 // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
404 // bitmap; remember that the string used to generate m_pImgDouble is of the
405 // form: "foo foo", the number of spaces being a parameter
410 position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
413 position -= m_pImgDouble->getWidth() - m_pImg->getWidth();