]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_text.cpp
47644929f9b4d28919edbc20fd148a76001e0028
[vlc] / modules / gui / skins2 / controls / ctrl_text.cpp
1 /*****************************************************************************
2  * ctrl_text.cpp
3  *****************************************************************************
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  * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 1
39 #define MOVING_TEXT_DELAY 30
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, Scrolling_t scrollMode,
46                     Align_t alignment ):
47     CtrlGeneric( pIntf, rHelp, pVisible ), 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 )
55 {
56     m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
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", "leave", "outStill" );
68     m_fsm.addTransition( "outStill", "enter", "still" );
69     if( m_scrollMode == kManual )
70     {
71         m_fsm.addTransition( "still", "mouse:left:down", "manual1",
72                              &m_cmdToManual );
73         m_fsm.addTransition( "manual1", "mouse:left:up", "still",
74                              &m_cmdManualStill );
75         m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
76     }
77     else if( m_scrollMode == kAutomatic || m_scrollMode == kAutomaticOff )
78     {
79         m_fsm.addTransition( "still", "mouse:left:down", "manual1",
80                              &m_cmdToManual );
81         m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
82                              &m_cmdManualMoving );
83         m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
84                              &m_cmdToManual );
85         m_fsm.addTransition( "manual2", "mouse:left:up", "still",
86                              &m_cmdManualStill );
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" );
91     }
92
93     // Initial state
94     if( m_scrollMode == kAutomatic )
95         m_fsm.setState( "outMoving" );
96     else
97         m_fsm.setState( "outStill" );
98
99     // Observe the variable
100     m_rVariable.addObserver( this );
101
102     // Set the text
103     displayText( m_rVariable.get() );
104 }
105
106
107 CtrlText::~CtrlText()
108 {
109     m_rVariable.delObserver( this );
110     if( m_pTimer )
111     {
112         delete m_pTimer;
113     }
114     if( m_pImg )
115     {
116         delete m_pImg;
117     }
118     if( m_pImgDouble )
119     {
120         delete m_pImgDouble;
121     }
122 }
123
124
125 void CtrlText::handleEvent( EvtGeneric &rEvent )
126 {
127     // Save the event to use it in callbacks
128     m_pEvt = &rEvent;
129
130     m_fsm.handleTransition( rEvent.getAsString() );
131 }
132
133
134 bool CtrlText::mouseOver( int x, int y ) const
135 {
136     if( m_pCurrImg )
137     {
138         // We have 3 different ways of deciding when to return true here:
139         //  1) the mouse is exactly over the text (so if you click between two
140         //     letters, the text control doesn't catch the event)
141         //  2) the mouse is over the rectangle of the control
142         //  3) the mouse is over the rectangle of the visible text
143         // I don't know which one is the best...
144 #if 0
145         return( x >= 0 && x < getPosition()->getWidth()
146              && m_pCurrImg->hit( x - m_xPos, y ) );
147 #endif
148 #if 1
149         return( x >= 0 && x < getPosition()->getWidth()
150              && y >= 0 && y < getPosition()->getHeight() );
151 #endif
152 #if 0
153         return( x >= 0 && x < getPosition()->getWidth()
154              && y >= 0 && y < getPosition()->getHeight()
155              && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
156 #endif
157     }
158     else
159     {
160         return false;
161     }
162 }
163
164
165 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
166 {
167     if( m_pCurrImg )
168     {
169         // Compute the dimensions to draw
170         int width = min( m_pCurrImg->getWidth() + m_xPos,
171                          getPosition()->getWidth() );
172         int height = min( m_pCurrImg->getHeight(), getPosition()->getHeight() );
173         // Draw the current image
174         if( width > 0 && height > 0 )
175         {
176             int offset = 0;
177             if( m_alignment == kLeft )
178             {
179                 // We align to the left
180                 offset = 0;
181             }
182             else if( m_alignment == kRight &&
183                      width < getPosition()->getWidth() )
184
185             {
186                 // The text is shorter than the width of the control, so we
187                 // can align it to the right
188                 offset = getPosition()->getWidth() - width;
189             }
190             else if( m_alignment == kCenter &&
191                      width < getPosition()->getWidth() )
192             {
193                     // The text is shorter than the width of the control, so we
194                     // can center it
195                 offset = (getPosition()->getWidth() - width) / 2;
196             }
197             rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest + offset,
198                                yDest, width, height, true );
199         }
200     }
201 }
202
203
204 void CtrlText::setText( const UString &rText, uint32_t color )
205 {
206     // Change the color
207     if( color != 0xFFFFFFFF )
208     {
209         m_color = color;
210     }
211
212     // Change the text
213     m_rVariable.set( rText );
214 }
215
216
217 void CtrlText::onUpdate( Subject<VarText, void*> &rVariable, void* arg )
218 {
219     if( isVisible() )
220     {
221         displayText( m_rVariable.get() );
222     }
223 }
224
225
226 void CtrlText::displayText( const UString &rText )
227 {
228     // Create the images ('normal' and 'double') from the text
229     // 'Normal' image
230     if( m_pImg )
231     {
232         delete m_pImg;
233     }
234     m_pImg = m_rFont.drawString( rText, m_color );
235     if( !m_pImg )
236     {
237         return;
238     }
239     // 'Double' image
240     const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
241     if( m_pImgDouble )
242     {
243         delete m_pImgDouble;
244     }
245     m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
246
247     // Update the current image used, as if the control size had changed
248     onChangePosition();
249
250     if( m_alignment == kRight && getPosition() &&
251         getPosition()->getWidth() < m_pImg->getWidth() )
252     {
253         m_xPos = getPosition()->getWidth() - m_pImg->getWidth();
254     }
255     else if( m_alignment == kCenter && getPosition() &&
256              getPosition()->getWidth() < m_pImg->getWidth() )
257     {
258         m_xPos = (getPosition()->getWidth() - m_pImg->getWidth()) / 2;
259     }
260     else
261     {
262         m_xPos = 0;
263     }
264
265     if( getPosition() )
266     {
267         // If the control was in the moving state, check if the scrolling is
268         // still necessary
269         const string &rState = m_fsm.getState();
270         if( rState == "moving" || rState == "outMoving" )
271         {
272             if( m_pImg && m_pImg->getWidth() >= getPosition()->getWidth() )
273             {
274                 m_pCurrImg = m_pImgDouble;
275                 m_pTimer->start( MOVING_TEXT_DELAY, false );
276             }
277             else
278             {
279                 m_pTimer->stop();
280             }
281         }
282         notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
283     }
284 }
285
286
287 void CtrlText::onChangePosition()
288 {
289     if( m_pImg && getPosition() )
290     {
291         if( m_pImg->getWidth() < getPosition()->getWidth() )
292         {
293             m_pCurrImg = m_pImg;
294         }
295         else
296         {
297             m_pCurrImg = m_pImgDouble;
298         }
299     }
300     else
301     {
302         // m_pImg is a better default value than m_pImgDouble, but anyway we
303         // don't care because the control is never drawn without position :)
304         m_pCurrImg = m_pImg;
305     }
306 }
307
308
309 void CtrlText::CmdToManual::execute()
310 {
311     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
312
313     // Compute the offset
314     m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
315
316     m_pParent->m_pTimer->stop();
317     m_pParent->captureMouse();
318 }
319
320
321 void CtrlText::CmdManualMoving::execute()
322 {
323     m_pParent->releaseMouse();
324
325     // Start the automatic movement, but only if the text is wider than the
326     // control and if the control can scroll (either in manual or automatic
327     // mode)
328     if( m_pParent->m_pImg &&
329         m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
330     {
331         // The current image may have been set incorrectly in displayText(), so
332         // set the correct value
333         m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
334
335         m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
336     }
337 }
338
339
340 void CtrlText::CmdManualStill::execute()
341 {
342     m_pParent->releaseMouse();
343 }
344
345
346 void CtrlText::CmdMove::execute()
347 {
348     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
349
350     // Do nothing if the text fits in the control
351     if( m_pParent->m_pImg &&
352         m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
353     {
354         // The current image may have been set incorrectly in displayText(), so
355         // we set the correct value
356         m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
357
358         // Compute the new position of the left side, and make sure it is
359         // in the correct range
360         m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
361         m_pParent->adjust( m_pParent->m_xPos );
362
363         m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
364                                  m_pParent->getPosition()->getHeight() );
365     }
366 }
367
368
369 void CtrlText::CmdUpdateText::execute()
370 {
371     m_pParent->m_xPos -= MOVING_TEXT_STEP;
372     m_pParent->adjust( m_pParent->m_xPos );
373
374     m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
375                              m_pParent->getPosition()->getHeight() );
376 }
377
378
379 void CtrlText::adjust( int &position )
380 {
381     // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
382     // bitmap; remember that the string used to generate m_pImgDouble is of the
383     // form: "foo  foo", the number of spaces being a parameter
384     if( !m_pImg )
385     {
386         return;
387     }
388     position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
389     if( position > 0 )
390     {
391         position -= m_pImgDouble->getWidth() - m_pImg->getWidth();
392     }
393 }
394