]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_text.cpp
* ctrl_text.cpp: put the control text in the "moving" state initially
[vlc] / modules / gui / skins2 / controls / ctrl_text.cpp
1 /*****************************************************************************
2  * ctrl_text.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
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"
36
37
38 #define MOVING_TEXT_STEP 3
39 #define MOVING_TEXT_DELAY 200
40 #define SEPARATOR_STRING "   "
41
42
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 )
54 {
55     m_pTimer = OSFactory::instance( getIntf() )->createOSTimer(
56         Callback( this, &updateText ) );
57
58     // States
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" );
65
66     // Transitions
67     m_fsm.addTransition( "still", "mouse:left:down", "manual1",
68                          &m_cmdToManual );
69     m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
70                          &m_cmdManualMoving );
71     m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
72                          &m_cmdToManual );
73     m_fsm.addTransition( "manual2", "mouse:left:up", "still",
74                          &m_cmdManualStill );
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" );
81
82     // Initial state
83     m_fsm.setState( "moving" );
84
85     // Observe the variable
86     m_rVariable.addObserver( this );
87
88     // Set the text
89     displayText( m_rVariable.get() );
90 }
91
92
93 CtrlText::~CtrlText()
94 {
95     m_rVariable.delObserver( this );
96     if( m_pTimer )
97     {
98         delete m_pTimer;
99     }
100     if( m_pImg )
101     {
102         delete m_pImg;
103     }
104     if( m_pImgDouble )
105     {
106         delete m_pImgDouble;
107     }
108 }
109
110
111 void CtrlText::handleEvent( EvtGeneric &rEvent )
112 {
113     // Save the event to use it in callbacks
114     m_pEvt = &rEvent;
115
116     m_fsm.handleTransition( rEvent.getAsString() );
117 }
118
119
120 bool CtrlText::mouseOver( int x, int y ) const
121 {
122     if( m_pCurrImg )
123     {
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...
130 #if 0
131         return( x >= 0 && x < getPosition()->getWidth()
132              && m_pCurrImg->hit( x - m_xPos, y ) );
133 #endif
134 #if 1
135         return( x >= 0 && x < getPosition()->getWidth()
136              && y >= 0 && y < getPosition()->getHeight() );
137 #endif
138 #if 0
139         return( x >= 0 && x < getPosition()->getWidth()
140              && y >= 0 && y < getPosition()->getHeight()
141              && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
142 #endif
143     }
144     else
145     {
146         return false;
147     }
148 }
149
150
151 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
152 {
153     if( m_pCurrImg )
154     {
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         if( width > 0 && height > 0 )
161         {
162             rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest, yDest,
163                             width, height );
164         }
165     }
166 }
167
168
169 void CtrlText::setText( const UString &rText, uint32_t color )
170 {
171     // Change the color
172     if( color != 0xFFFFFFFF )
173     {
174         m_color = color;
175     }
176
177     // Change the text
178     m_rVariable.set( rText );
179 }
180
181
182 void CtrlText::onUpdate( Subject<VarText> &rVariable )
183 {
184     displayText( m_rVariable.get() );
185 }
186
187
188 void CtrlText::displayText( const UString &rText )
189 {
190     // Create the images ('normal' and 'double') from the text
191     // 'Normal' image
192     if( m_pImg )
193     {
194         delete m_pImg;
195     }
196     m_pImg = m_rFont.drawString( rText, m_color );
197     if( !m_pImg )
198     {
199         return;
200     }
201     // 'Double' image
202     const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
203     if( m_pImgDouble )
204     {
205         delete m_pImgDouble;
206     }
207     m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
208
209     // Update the current image used, as if the control size had changed
210     onChangePosition();
211     m_xPos = 0;
212
213     if( getPosition() )
214     {
215         // If the control was in the moving state, check if the scrolling is
216         // still necessary
217         const string &rState = m_fsm.getState();
218         if( rState == "moving" || rState == "outMoving" )
219         {
220             if( m_pImg && m_pImg->getWidth() >= getPosition()->getWidth() )
221             {
222                 m_pCurrImg = m_pImgDouble;
223                 m_pTimer->start( MOVING_TEXT_DELAY, false );
224             }
225             else
226             {
227                 m_pTimer->stop();
228             }
229         }
230         notifyLayout();
231     }
232 }
233
234
235 void CtrlText::onChangePosition()
236 {
237     if( m_pImg && getPosition() )
238     {
239         if( m_pImg->getWidth() < getPosition()->getWidth() )
240         {
241             m_pCurrImg = m_pImg;
242         }
243         else
244         {
245             m_pCurrImg = m_pImgDouble;
246         }
247     }
248     else
249     {
250         // m_pImg is a better default value than m_pImgDouble, but anyway we
251         // don't care because the control is never drawn without position :)
252         m_pCurrImg = m_pImg;
253     }
254 }
255
256
257 void CtrlText::transToManual( SkinObject *pCtrl )
258 {
259     CtrlText *pThis = (CtrlText*)pCtrl;
260     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
261
262     // Compute the offset
263     pThis->m_xOffset = pEvtMouse->getXPos() - pThis->m_xPos;
264
265     pThis->m_pTimer->stop();
266     pThis->captureMouse();
267 }
268
269
270 void CtrlText::transManualMoving( SkinObject *pCtrl )
271 {
272     CtrlText *pThis = (CtrlText*)pCtrl;
273     pThis->releaseMouse();
274
275     // Start the automatic movement, but only if the text is wider than the
276     // control
277     if( pThis->m_pImg &&
278         pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
279     {
280         // The current image may have been set incorrectly in displayText(), so
281         // set the correct value
282         pThis->m_pCurrImg = pThis->m_pImgDouble;
283
284         pThis->m_pTimer->start( MOVING_TEXT_DELAY, false );
285     }
286 }
287
288
289 void CtrlText::transManualStill( SkinObject *pCtrl )
290 {
291     CtrlText *pThis = (CtrlText*)pCtrl;
292     pThis->releaseMouse();
293 }
294
295
296 void CtrlText::transMove( SkinObject *pCtrl )
297 {
298     CtrlText *pThis = (CtrlText*)pCtrl;
299     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
300
301     // Do nothing if the text fits in the control
302     if( pThis->m_pImg &&
303         pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
304     {
305         // The current image may have been set incorrectly in displayText(), so
306         // we set the correct value
307         pThis->m_pCurrImg = pThis->m_pImgDouble;
308
309         // Compute the new position of the left side, and make sure it is
310         // in the correct range
311         pThis->m_xPos = (pEvtMouse->getXPos() - pThis->m_xOffset);
312         pThis->adjust( pThis->m_xPos );
313
314         pThis->notifyLayout();
315     }
316 }
317
318
319 void CtrlText::updateText( SkinObject *pCtrl )
320 {
321     CtrlText *pThis = (CtrlText*)pCtrl;
322
323     pThis->m_xPos -= MOVING_TEXT_STEP;
324     pThis->adjust( pThis->m_xPos );
325
326     pThis->notifyLayout();
327 }
328
329
330 void CtrlText::adjust( int &position )
331 {
332     // {m_pImgDouble->getWidth()  - m_pImg->getWidth()} is the period of the
333     // bitmap; remember that the string used to generate m_pImgDouble is of the
334     // form: "foo  foo", the number of spaces being a parameter
335     if( !m_pImg )
336     {
337         return;
338     }
339     position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
340     if( position > 0 )
341     {
342         position -= m_pImgDouble->getWidth() - m_pImg->getWidth();
343     }
344 }
345