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