]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_text.cpp
* all: beginning of bitmap font support. At the moment only the digits
[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( "outStill" );
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         rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest, yDest,
161                            width, height );
162     }
163 }
164
165
166 void CtrlText::setText( const UString &rText, uint32_t color )
167 {
168     // Change the color
169     if( color != 0xFFFFFFFF )
170     {
171         m_color = color;
172     }
173
174     // Change the text
175     m_rVariable.set( rText );
176 }
177
178
179 void CtrlText::onUpdate( Subject<VarText> &rVariable )
180 {
181     displayText( m_rVariable.get() );
182 }
183
184
185 void CtrlText::displayText( const UString &rText )
186 {
187     // Create the images ('normal' and 'double') from the text
188     // 'Normal' image
189     if( m_pImg )
190     {
191         delete m_pImg;
192     }
193     m_pImg = m_rFont.drawString( rText, m_color );
194     if( !m_pImg )
195     {
196         return;
197     }
198     // 'Double' image
199     const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
200     if( m_pImgDouble )
201     {
202         delete m_pImgDouble;
203     }
204     m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
205
206     // Update the current image used, as if the control size had changed
207     onChangePosition();
208
209     notifyLayout();
210 }
211
212
213 void CtrlText::onChangePosition()
214 {
215     if( m_pImg && getPosition() )
216     {
217         if( m_pImg->getWidth() < getPosition()->getWidth() )
218         {
219             m_pCurrImg = m_pImg;
220         }
221         else
222         {
223             m_pCurrImg = m_pImgDouble;
224         }
225     }
226     else
227     {
228         // m_pImg is a better default value than m_pImgDouble, but anyway we
229         // don't care because the control is never drawn without position :)
230         m_pCurrImg = m_pImg;
231     }
232 }
233
234
235 void CtrlText::transToManual( SkinObject *pCtrl )
236 {
237     CtrlText *pThis = (CtrlText*)pCtrl;
238     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
239
240     // Compute the offset
241     pThis->m_xOffset = pEvtMouse->getXPos() - pThis->m_xPos;
242
243     pThis->m_pTimer->stop();
244     pThis->captureMouse();
245 }
246
247
248 void CtrlText::transManualMoving( SkinObject *pCtrl )
249 {
250     CtrlText *pThis = (CtrlText*)pCtrl;
251     pThis->releaseMouse();
252
253     // Start the automatic movement, but only if the text is wider than the
254     // control
255     if( pThis->m_pImg &&
256         pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
257     {
258         // The current image may have been set incorrectly in displayText(), so
259         // set the correct value
260         pThis->m_pCurrImg = pThis->m_pImgDouble;
261
262         pThis->m_pTimer->start( MOVING_TEXT_DELAY, false );
263     }
264 }
265
266
267 void CtrlText::transManualStill( SkinObject *pCtrl )
268 {
269     CtrlText *pThis = (CtrlText*)pCtrl;
270     pThis->releaseMouse();
271 }
272
273
274 void CtrlText::transMove( SkinObject *pCtrl )
275 {
276     CtrlText *pThis = (CtrlText*)pCtrl;
277     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
278
279     // Do nothing if the text fits in the control
280     if( pThis->m_pImg &&
281         pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
282     {
283         // The current image may have been set incorrectly in displayText(), so
284         // we set the correct value
285         pThis->m_pCurrImg = pThis->m_pImgDouble;
286
287         // Compute the new position of the left side, and make sure it is
288         // in the correct range
289         pThis->m_xPos = (pEvtMouse->getXPos() - pThis->m_xOffset);
290         pThis->adjust( pThis->m_xPos );
291
292         pThis->notifyLayout();
293     }
294 }
295
296
297 void CtrlText::updateText( SkinObject *pCtrl )
298 {
299     CtrlText *pThis = (CtrlText*)pCtrl;
300
301     pThis->m_xPos -= MOVING_TEXT_STEP;
302     pThis->adjust( pThis->m_xPos );
303
304     pThis->notifyLayout();
305 }
306
307
308 void CtrlText::adjust( int &position )
309 {
310     // {m_pImgDouble->getWidth()  - m_pImg->getWidth()} is the period of the
311     // bitmap; remember that the string used to generate m_pImgDouble is of the
312     // form: "foo  foo", the number of spaces being a parameter
313     if( !m_pImg )
314     {
315         return;
316     }
317     position %= m_pImgDouble->getWidth()  - m_pImg->getWidth();
318     if( position > 0 )
319     {
320         position -= m_pImgDouble->getWidth()  - m_pImg->getWidth();
321     }
322 }
323