]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_text.cpp
ec76b6a5eeeb1c731617d0e79b812fdf93f89010
[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., 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 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 ):
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 )
53 {
54     m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
55
56     // States
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" );
63
64     // Transitions
65     m_fsm.addTransition( "still", "mouse:left:down", "manual1",
66                          &m_cmdToManual );
67     m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
68                          &m_cmdManualMoving );
69     m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
70                          &m_cmdToManual );
71     m_fsm.addTransition( "manual2", "mouse:left:up", "still",
72                          &m_cmdManualStill );
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" );
79
80     // Initial state
81     m_fsm.setState( "moving" );
82
83     // Observe the variable
84     m_rVariable.addObserver( this );
85
86     // Set the text
87     displayText( m_rVariable.get() );
88 }
89
90
91 CtrlText::~CtrlText()
92 {
93     m_rVariable.delObserver( this );
94     if( m_pTimer )
95     {
96         delete m_pTimer;
97     }
98     if( m_pImg )
99     {
100         delete m_pImg;
101     }
102     if( m_pImgDouble )
103     {
104         delete m_pImgDouble;
105     }
106 }
107
108
109 void CtrlText::handleEvent( EvtGeneric &rEvent )
110 {
111     // Save the event to use it in callbacks
112     m_pEvt = &rEvent;
113
114     m_fsm.handleTransition( rEvent.getAsString() );
115 }
116
117
118 bool CtrlText::mouseOver( int x, int y ) const
119 {
120     if( m_pCurrImg )
121     {
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...
128 #if 0
129         return( x >= 0 && x < getPosition()->getWidth()
130              && m_pCurrImg->hit( x - m_xPos, y ) );
131 #endif
132 #if 1
133         return( x >= 0 && x < getPosition()->getWidth()
134              && y >= 0 && y < getPosition()->getHeight() );
135 #endif
136 #if 0
137         return( x >= 0 && x < getPosition()->getWidth()
138              && y >= 0 && y < getPosition()->getHeight()
139              && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
140 #endif
141     }
142     else
143     {
144         return false;
145     }
146 }
147
148
149 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
150 {
151     if( m_pCurrImg )
152     {
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 )
159         {
160             rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest, yDest,
161                             width, height, true );
162         }
163     }
164 }
165
166
167 void CtrlText::setText( const UString &rText, uint32_t color )
168 {
169     // Change the color
170     if( color != 0xFFFFFFFF )
171     {
172         m_color = color;
173     }
174
175     // Change the text
176     m_rVariable.set( rText );
177 }
178
179
180 void CtrlText::onUpdate( Subject<VarText> &rVariable )
181 {
182     displayText( m_rVariable.get() );
183 }
184
185
186 void CtrlText::displayText( const UString &rText )
187 {
188     // Create the images ('normal' and 'double') from the text
189     // 'Normal' image
190     if( m_pImg )
191     {
192         delete m_pImg;
193     }
194     m_pImg = m_rFont.drawString( rText, m_color );
195     if( !m_pImg )
196     {
197         return;
198     }
199     // 'Double' image
200     const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
201     if( m_pImgDouble )
202     {
203         delete m_pImgDouble;
204     }
205     m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
206
207     // Update the current image used, as if the control size had changed
208     onChangePosition();
209     m_xPos = 0;
210
211     if( getPosition() )
212     {
213         // If the control was in the moving state, check if the scrolling is
214         // still necessary
215         const string &rState = m_fsm.getState();
216         if( rState == "moving" || rState == "outMoving" )
217         {
218             if( m_pImg && m_pImg->getWidth() >= getPosition()->getWidth() )
219             {
220                 m_pCurrImg = m_pImgDouble;
221                 m_pTimer->start( MOVING_TEXT_DELAY, false );
222             }
223             else
224             {
225                 m_pTimer->stop();
226             }
227         }
228         notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
229     }
230 }
231
232
233 void CtrlText::onChangePosition()
234 {
235     if( m_pImg && getPosition() )
236     {
237         if( m_pImg->getWidth() < getPosition()->getWidth() )
238         {
239             m_pCurrImg = m_pImg;
240         }
241         else
242         {
243             m_pCurrImg = m_pImgDouble;
244         }
245     }
246     else
247     {
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 :)
250         m_pCurrImg = m_pImg;
251     }
252 }
253
254
255 void CtrlText::CmdToManual::execute()
256 {
257     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
258
259     // Compute the offset
260     m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
261
262     m_pParent->m_pTimer->stop();
263     m_pParent->captureMouse();
264 }
265
266
267 void CtrlText::CmdManualMoving::execute()
268 {
269     m_pParent->releaseMouse();
270
271     // Start the automatic movement, but only if the text is wider than the
272     // control
273     if( m_pParent->m_pImg &&
274         m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
275     {
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;
279
280         m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
281     }
282 }
283
284
285 void CtrlText::CmdManualStill::execute()
286 {
287     m_pParent->releaseMouse();
288 }
289
290
291 void CtrlText::CmdMove::execute()
292 {
293     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
294
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() )
298     {
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;
302
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 );
307
308         m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
309                              m_pParent->getPosition()->getHeight() );
310     }
311 }
312
313
314 void CtrlText::CmdUpdateText::execute()
315 {
316     m_pParent->m_xPos -= MOVING_TEXT_STEP;
317     m_pParent->adjust( m_pParent->m_xPos );
318
319     m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
320                          m_pParent->getPosition()->getHeight() );
321 }
322
323
324 void CtrlText::adjust( int &position )
325 {
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
329     if( !m_pImg )
330     {
331         return;
332     }
333     position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
334     if( position > 0 )
335     {
336         position -= m_pImgDouble->getWidth() - m_pImg->getWidth();
337     }
338 }
339