1 /*****************************************************************************
2 * ctrl_text.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id: 729fdab85ded06622fb878fda7a91a85088710bd $
6 *
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
9 * Erwan Tulou <erwan10 At videolan Dot org<
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
25
26 #include "ctrl_text.hpp"
27 #include "../events/evt_generic.hpp"
28 #include "../events/evt_mouse.hpp"
29 #include "../src/generic_bitmap.hpp"
30 #include "../src/generic_font.hpp"
31 #include "../src/os_factory.hpp"
32 #include "../src/os_graphics.hpp"
33 #include "../src/os_timer.hpp"
34 #include "../utils/position.hpp"
35 #include "../utils/ustring.hpp"
36 #include "../utils/var_text.hpp"
37
38
39 #define MOVING_TEXT_STEP 1
40 #define MOVING_TEXT_DELAY 30
41 #define SEPARATOR_STRING " "
42
43
CtrlText(intf_thread_t * pIntf,VarText & rVariable,const GenericFont & rFont,const UString & rHelp,uint32_t color,VarBool * pVisible,VarBool * pFocus,Scrolling_t scrollMode,Align_t alignment)44 CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
45 const GenericFont &rFont, const UString &rHelp,
46 uint32_t color, VarBool *pVisible, VarBool *pFocus,
47 Scrolling_t scrollMode, Align_t alignment ):
48 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
49 m_rVariable( rVariable ), m_cmdToManual( this ),
50 m_cmdManualMoving( this ), m_cmdManualStill( this ),
51 m_cmdMove( this ), m_pEvt( NULL ), m_rFont( rFont ),
52 m_color( color ), m_scrollMode( scrollMode ), m_alignment( alignment ),
53 m_pFocus( pFocus), m_pImg( NULL ), m_pImgDouble( NULL ),
54 m_pCurrImg( NULL ), m_xPos( 0 ), m_xOffset( 0 ),
55 m_cmdUpdateText( this )
56 {
57 m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
58
59 // States
60 m_fsm.addState( "still" );
61 m_fsm.addState( "moving" );
62 m_fsm.addState( "manual1" );
63 m_fsm.addState( "manual2" );
64 m_fsm.addState( "outStill" );
65 m_fsm.addState( "outMoving" );
66
67 // Transitions
68 m_fsm.addTransition( "still", "leave", "outStill" );
69 m_fsm.addTransition( "outStill", "enter", "still" );
70 if( m_scrollMode == kManual )
71 {
72 m_fsm.addTransition( "still", "mouse:left:down", "manual1",
73 &m_cmdToManual );
74 m_fsm.addTransition( "manual1", "mouse:left:up", "still",
75 &m_cmdManualStill );
76 m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
77 }
78 else if( m_scrollMode == kAutomatic )
79 {
80 m_fsm.addTransition( "still", "mouse:left:down", "manual1",
81 &m_cmdToManual );
82 m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
83 &m_cmdManualMoving );
84 m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
85 &m_cmdToManual );
86 m_fsm.addTransition( "manual2", "mouse:left:up", "still",
87 &m_cmdManualStill );
88 m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
89 m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
90 m_fsm.addTransition( "moving", "leave", "outMoving" );
91 m_fsm.addTransition( "outMoving", "enter", "moving" );
92 }
93
94 // Initial state
95 m_fsm.setState( (m_scrollMode != kAutomatic) ? "outStill" : "outMoving" );
96
97 // Observe the variable
98 m_rVariable.addObserver( this );
99
100 // initialize pictures
101 setPictures( m_rVariable.get() );
102 }
103
104
~CtrlText()105 CtrlText::~CtrlText()
106 {
107 m_rVariable.delObserver( this );
108 delete m_pTimer;
109 delete m_pImg;
110 delete m_pImgDouble;
111 }
112
113
handleEvent(EvtGeneric & rEvent)114 void CtrlText::handleEvent( EvtGeneric &rEvent )
115 {
116 // Save the event to use it in callbacks
117 m_pEvt = &rEvent;
118
119 m_fsm.handleTransition( rEvent.getAsString() );
120 }
121
122
mouseOver(int x,int y) const123 bool CtrlText::mouseOver( int x, int y ) const
124 {
125 if( !m_pFocus->get() )
126 return false;
127
128 if( m_pCurrImg )
129 {
130 // We have 3 different ways of deciding when to return true here:
131 // 1) the mouse is exactly over the text (so if you click between two
132 // letters, the text control doesn't catch the event)
133 // 2) the mouse is over the rectangle of the control
134 // 3) the mouse is over the rectangle of the visible text
135 // I don't know which one is the best...
136 #if 0
137 return( x >= 0 && x < getPosition()->getWidth()
138 && m_pCurrImg->hit( x - m_xPos, y ) );
139 #endif
140 #if 1
141 return( x >= 0 && x < getPosition()->getWidth()
142 && y >= 0 && y < getPosition()->getHeight() );
143 #endif
144 #if 0
145 return( x >= 0 && x < getPosition()->getWidth()
146 && y >= 0 && y < getPosition()->getHeight()
147 && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
148 #endif
149 }
150 else
151 {
152 return false;
153 }
154 }
155
156
draw(OSGraphics & rImage,int xDest,int yDest,int w,int h)157 void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
158 {
159 rect clip( xDest, yDest, w, h );
160 const Position *pPos = getPosition();
161 if( m_pCurrImg )
162 {
163 // Compute the dimensions to draw
164 int width = std::min( m_pCurrImg->getWidth() + m_xPos,
165 getPosition()->getWidth() );
166 int height = std::min( m_pCurrImg->getHeight(), getPosition()->getHeight() );
167 // Draw the current image
168 if( width > 0 && height > 0 )
169 {
170 int offset = 0;
171 if( m_alignment == kLeft )
172 {
173 // We align to the left
174 offset = 0;
175 }
176 else if( m_alignment == kRight &&
177 width < getPosition()->getWidth() )
178
179 {
180 // The text is shorter than the width of the control, so we
181 // can align it to the right
182 offset = getPosition()->getWidth() - width;
183 }
184 else if( m_alignment == kCenter &&
185 width < getPosition()->getWidth() )
186 {
187 // The text is shorter than the width of the control, so we
188 // can center it
189 offset = (getPosition()->getWidth() - width) / 2;
190 }
191 rect region( pPos->getLeft() + offset,
192 pPos->getTop(), width, height );
193 rect inter;
194 if( rect::intersect( region, clip, &inter ) )
195 rImage.drawBitmap( *m_pCurrImg, -m_xPos + inter.x - region.x,
196 inter.y - region.y,
197 inter.x, inter.y,
198 inter.width, inter.height, true );
199 }
200 }
201 }
202
203
setText(const UString & rText,uint32_t color)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
onUpdate(Subject<VarText> & rVariable,void * arg)217 void CtrlText::onUpdate( Subject<VarText> &rVariable, void* arg )
218 {
219 (void)rVariable; (void)arg;
220 if( isVisible() )
221 {
222 setPictures( m_rVariable.get() );
223 updateContext();
224 notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
225 }
226 }
227
228
onUpdate(Subject<VarBool> & rVariable,void * arg)229 void CtrlText::onUpdate( Subject<VarBool> &rVariable, void *arg )
230 {
231 (void)arg;
232 // Visibility changed
233 if( &rVariable == m_pVisible )
234 {
235 if( isVisible() )
236 {
237 setPictures( m_rVariable.get() );
238 updateContext();
239 }
240
241 // notify in any case
242 notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
243 }
244 }
245
246
setPictures(const UString & rText)247 void CtrlText::setPictures( const UString &rText )
248 {
249 // reset the images ('normal' and 'double') from the text
250 // 'Normal' image
251 delete m_pImg;
252 m_pImg = m_rFont.drawString( rText, m_color );
253 if( !m_pImg )
254 return;
255
256 // 'Double' image
257 const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
258 delete m_pImgDouble;
259 m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
260 }
261
262
updateContext()263 void CtrlText::updateContext()
264 {
265 if( !m_pImg || !getPosition() )
266 return;
267
268 if( m_pImg->getWidth() < getPosition()->getWidth() )
269 {
270 m_pCurrImg = m_pImg;
271
272 // When the control becomes wide enough for the text to display,
273 // make sure to stop any scrolling effect
274 m_pTimer->stop();
275 m_xPos = 0;
276 }
277 else
278 {
279 m_pCurrImg = m_pImgDouble;
280 }
281
282 // If the control is in the moving state,
283 // automatically start or stop the timer accordingly
284 const std::string &rState = m_fsm.getState();
285 if( rState == "moving" || rState == "outMoving" )
286 {
287 if( m_pCurrImg == m_pImgDouble )
288 {
289 m_pTimer->start( MOVING_TEXT_DELAY, false );
290 }
291 else
292 {
293 m_pTimer->stop();
294 }
295 }
296
297 // compute alignment
298 if( m_alignment == kRight &&
299 getPosition()->getWidth() < m_pImg->getWidth() )
300 {
301 m_xPos = getPosition()->getWidth() - m_pImg->getWidth();
302 }
303 else if( m_alignment == kCenter &&
304 getPosition()->getWidth() < m_pImg->getWidth() )
305 {
306 m_xPos = (getPosition()->getWidth() - m_pImg->getWidth()) / 2;
307 }
308 else
309 {
310 m_xPos = 0;
311 }
312 }
313
314
onPositionChange()315 void CtrlText::onPositionChange()
316 {
317 updateContext();
318 }
319
320
onResize()321 void CtrlText::onResize()
322 {
323 updateContext();
324 }
325
326
execute()327 void CtrlText::CmdToManual::execute()
328 {
329 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
330
331 // Compute the offset
332 m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
333
334 m_pParent->m_pTimer->stop();
335 m_pParent->captureMouse();
336 }
337
338
execute()339 void CtrlText::CmdManualMoving::execute()
340 {
341 m_pParent->releaseMouse();
342
343 // Start the automatic movement, but only if the text is wider than the
344 // control and if the control can scroll (either in manual or automatic
345 // mode)
346 if( m_pParent->m_pCurrImg &&
347 m_pParent->m_pCurrImg == m_pParent->m_pImgDouble )
348 {
349 m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
350 }
351 }
352
353
execute()354 void CtrlText::CmdManualStill::execute()
355 {
356 m_pParent->releaseMouse();
357 }
358
359
execute()360 void CtrlText::CmdMove::execute()
361 {
362 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
363
364 // Move text only when it is larger than the control
365 if( m_pParent->m_pCurrImg &&
366 m_pParent->m_pCurrImg == m_pParent->m_pImgDouble )
367 {
368 // Compute the new position of the left side, and make sure it is
369 // in the correct range
370 m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
371 m_pParent->adjust( m_pParent->m_xPos );
372
373 m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
374 m_pParent->getPosition()->getHeight() );
375 }
376 }
377
378
execute()379 void CtrlText::CmdUpdateText::execute()
380 {
381 m_pParent->m_xPos -= MOVING_TEXT_STEP;
382 m_pParent->adjust( m_pParent->m_xPos );
383
384 m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
385 m_pParent->getPosition()->getHeight() );
386 }
387
388
adjust(int & position)389 void CtrlText::adjust( int &position )
390 {
391 if( !m_pImg || !m_pImgDouble )
392 return;
393
394 // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
395 // bitmap; remember that the string used to generate m_pImgDouble is of the
396 // form: "foo foo", the number of spaces being a parameter
397 position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
398 if( position > 0 )
399 {
400 position -= m_pImgDouble->getWidth() - m_pImg->getWidth();
401 }
402 }
403
404