PageRenderTime 64ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/gui/skins2/controls/ctrl_text.cpp

https://github.com/mstorsjo/vlc
C++ | 402 lines | 276 code | 62 blank | 64 comment | 58 complexity | 1c488b2263ea8ddb5c2a56180c6d5cff MD5 | raw file
  1. /*****************************************************************************
  2. * ctrl_text.cpp
  3. *****************************************************************************
  4. * Copyright (C) 2003 the VideoLAN team
  5. *
  6. * Authors: Cyril Deguet <asmax@via.ecp.fr>
  7. * Olivier Teulière <ipkiss@via.ecp.fr>
  8. * Erwan Tulou <erwan10 At videolan Dot org<
  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. #include "ctrl_text.hpp"
  25. #include "../events/evt_generic.hpp"
  26. #include "../events/evt_mouse.hpp"
  27. #include "../src/generic_bitmap.hpp"
  28. #include "../src/generic_font.hpp"
  29. #include "../src/os_factory.hpp"
  30. #include "../src/os_graphics.hpp"
  31. #include "../src/os_timer.hpp"
  32. #include "../utils/position.hpp"
  33. #include "../utils/ustring.hpp"
  34. #include "../utils/var_text.hpp"
  35. #define MOVING_TEXT_STEP 1
  36. #define MOVING_TEXT_DELAY 30
  37. #define SEPARATOR_STRING " "
  38. CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
  39. const GenericFont &rFont, const UString &rHelp,
  40. uint32_t color, VarBool *pVisible, VarBool *pFocus,
  41. Scrolling_t scrollMode, Align_t alignment ):
  42. CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
  43. m_rVariable( rVariable ), m_cmdToManual( this ),
  44. m_cmdManualMoving( this ), m_cmdManualStill( this ),
  45. m_cmdMove( this ), m_pEvt( NULL ), m_rFont( rFont ),
  46. m_color( color ), m_scrollMode( scrollMode ), m_alignment( alignment ),
  47. m_pFocus( pFocus), m_pImg( NULL ), m_pImgDouble( NULL ),
  48. m_pCurrImg( NULL ), m_xPos( 0 ), m_xOffset( 0 ),
  49. m_cmdUpdateText( this )
  50. {
  51. m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
  52. // States
  53. m_fsm.addState( "still" );
  54. m_fsm.addState( "moving" );
  55. m_fsm.addState( "manual1" );
  56. m_fsm.addState( "manual2" );
  57. m_fsm.addState( "outStill" );
  58. m_fsm.addState( "outMoving" );
  59. // Transitions
  60. m_fsm.addTransition( "still", "leave", "outStill" );
  61. m_fsm.addTransition( "outStill", "enter", "still" );
  62. if( m_scrollMode == kManual )
  63. {
  64. m_fsm.addTransition( "still", "mouse:left:down", "manual1",
  65. &m_cmdToManual );
  66. m_fsm.addTransition( "manual1", "mouse:left:up", "still",
  67. &m_cmdManualStill );
  68. m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
  69. }
  70. else if( m_scrollMode == kAutomatic )
  71. {
  72. m_fsm.addTransition( "still", "mouse:left:down", "manual1",
  73. &m_cmdToManual );
  74. m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
  75. &m_cmdManualMoving );
  76. m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
  77. &m_cmdToManual );
  78. m_fsm.addTransition( "manual2", "mouse:left:up", "still",
  79. &m_cmdManualStill );
  80. m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
  81. m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
  82. m_fsm.addTransition( "moving", "leave", "outMoving" );
  83. m_fsm.addTransition( "outMoving", "enter", "moving" );
  84. }
  85. // Initial state
  86. m_fsm.setState( (m_scrollMode != kAutomatic) ? "outStill" : "outMoving" );
  87. // Observe the variable
  88. m_rVariable.addObserver( this );
  89. // initialize pictures
  90. setPictures( m_rVariable.get() );
  91. }
  92. CtrlText::~CtrlText()
  93. {
  94. m_rVariable.delObserver( this );
  95. delete m_pTimer;
  96. delete m_pImg;
  97. delete m_pImgDouble;
  98. }
  99. void CtrlText::handleEvent( EvtGeneric &rEvent )
  100. {
  101. // Save the event to use it in callbacks
  102. m_pEvt = &rEvent;
  103. m_fsm.handleTransition( rEvent.getAsString() );
  104. }
  105. bool CtrlText::mouseOver( int x, int y ) const
  106. {
  107. if( !m_pFocus->get() )
  108. return false;
  109. if( m_pCurrImg )
  110. {
  111. // We have 3 different ways of deciding when to return true here:
  112. // 1) the mouse is exactly over the text (so if you click between two
  113. // letters, the text control doesn't catch the event)
  114. // 2) the mouse is over the rectangle of the control
  115. // 3) the mouse is over the rectangle of the visible text
  116. // I don't know which one is the best...
  117. #if 0
  118. return( x >= 0 && x < getPosition()->getWidth()
  119. && m_pCurrImg->hit( x - m_xPos, y ) );
  120. #endif
  121. #if 1
  122. return( x >= 0 && x < getPosition()->getWidth()
  123. && y >= 0 && y < getPosition()->getHeight() );
  124. #endif
  125. #if 0
  126. return( x >= 0 && x < getPosition()->getWidth()
  127. && y >= 0 && y < getPosition()->getHeight()
  128. && x < m_pCurrImg->getWidth() && x < m_pCurrImg->getHeight() );
  129. #endif
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
  137. {
  138. rect clip( xDest, yDest, w, h );
  139. const Position *pPos = getPosition();
  140. if( m_pCurrImg )
  141. {
  142. // Compute the dimensions to draw
  143. int width = std::min( m_pCurrImg->getWidth() + m_xPos,
  144. getPosition()->getWidth() );
  145. int height = std::min( m_pCurrImg->getHeight(), getPosition()->getHeight() );
  146. // Draw the current image
  147. if( width > 0 && height > 0 )
  148. {
  149. int offset = 0;
  150. if( m_alignment == kLeft )
  151. {
  152. // We align to the left
  153. offset = 0;
  154. }
  155. else if( m_alignment == kRight &&
  156. width < getPosition()->getWidth() )
  157. {
  158. // The text is shorter than the width of the control, so we
  159. // can align it to the right
  160. offset = getPosition()->getWidth() - width;
  161. }
  162. else if( m_alignment == kCenter &&
  163. width < getPosition()->getWidth() )
  164. {
  165. // The text is shorter than the width of the control, so we
  166. // can center it
  167. offset = (getPosition()->getWidth() - width) / 2;
  168. }
  169. rect region( pPos->getLeft() + offset,
  170. pPos->getTop(), width, height );
  171. rect inter;
  172. if( rect::intersect( region, clip, &inter ) )
  173. rImage.drawBitmap( *m_pCurrImg, -m_xPos + inter.x - region.x,
  174. inter.y - region.y,
  175. inter.x, inter.y,
  176. inter.width, inter.height, true );
  177. }
  178. }
  179. }
  180. void CtrlText::setText( const UString &rText, uint32_t color )
  181. {
  182. // Change the color
  183. if( color != 0xFFFFFFFF )
  184. {
  185. m_color = color;
  186. }
  187. // Change the text
  188. m_rVariable.set( rText );
  189. }
  190. void CtrlText::onUpdate( Subject<VarText> &rVariable, void* arg )
  191. {
  192. (void)rVariable; (void)arg;
  193. if( isVisible() )
  194. {
  195. setPictures( m_rVariable.get() );
  196. updateContext();
  197. notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
  198. }
  199. }
  200. void CtrlText::onUpdate( Subject<VarBool> &rVariable, void *arg )
  201. {
  202. (void)arg;
  203. // Visibility changed
  204. if( &rVariable == m_pVisible )
  205. {
  206. if( isVisible() )
  207. {
  208. setPictures( m_rVariable.get() );
  209. updateContext();
  210. }
  211. // notify in any case
  212. notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
  213. }
  214. }
  215. void CtrlText::setPictures( const UString &rText )
  216. {
  217. // reset the images ('normal' and 'double') from the text
  218. // 'Normal' image
  219. delete m_pImg;
  220. m_pImg = m_rFont.drawString( rText, m_color );
  221. if( !m_pImg )
  222. return;
  223. // 'Double' image
  224. const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
  225. delete m_pImgDouble;
  226. m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
  227. }
  228. void CtrlText::updateContext()
  229. {
  230. if( !m_pImg || !getPosition() )
  231. return;
  232. if( m_pImg->getWidth() < getPosition()->getWidth() )
  233. {
  234. m_pCurrImg = m_pImg;
  235. // When the control becomes wide enough for the text to display,
  236. // make sure to stop any scrolling effect
  237. m_pTimer->stop();
  238. m_xPos = 0;
  239. }
  240. else
  241. {
  242. m_pCurrImg = m_pImgDouble;
  243. }
  244. // If the control is in the moving state,
  245. // automatically start or stop the timer accordingly
  246. const std::string &rState = m_fsm.getState();
  247. if( rState == "moving" || rState == "outMoving" )
  248. {
  249. if( m_pCurrImg == m_pImgDouble )
  250. {
  251. m_pTimer->start( MOVING_TEXT_DELAY, false );
  252. }
  253. else
  254. {
  255. m_pTimer->stop();
  256. }
  257. }
  258. // compute alignment
  259. if( m_alignment == kRight &&
  260. getPosition()->getWidth() < m_pImg->getWidth() )
  261. {
  262. m_xPos = getPosition()->getWidth() - m_pImg->getWidth();
  263. }
  264. else if( m_alignment == kCenter &&
  265. getPosition()->getWidth() < m_pImg->getWidth() )
  266. {
  267. m_xPos = (getPosition()->getWidth() - m_pImg->getWidth()) / 2;
  268. }
  269. else
  270. {
  271. m_xPos = 0;
  272. }
  273. }
  274. void CtrlText::onPositionChange()
  275. {
  276. updateContext();
  277. }
  278. void CtrlText::onResize()
  279. {
  280. updateContext();
  281. }
  282. void CtrlText::CmdToManual::execute()
  283. {
  284. EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
  285. // Compute the offset
  286. m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
  287. m_pParent->m_pTimer->stop();
  288. m_pParent->captureMouse();
  289. }
  290. void CtrlText::CmdManualMoving::execute()
  291. {
  292. m_pParent->releaseMouse();
  293. // Start the automatic movement, but only if the text is wider than the
  294. // control and if the control can scroll (either in manual or automatic
  295. // mode)
  296. if( m_pParent->m_pCurrImg &&
  297. m_pParent->m_pCurrImg == m_pParent->m_pImgDouble )
  298. {
  299. m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
  300. }
  301. }
  302. void CtrlText::CmdManualStill::execute()
  303. {
  304. m_pParent->releaseMouse();
  305. }
  306. void CtrlText::CmdMove::execute()
  307. {
  308. EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
  309. // Move text only when it is larger than the control
  310. if( m_pParent->m_pCurrImg &&
  311. m_pParent->m_pCurrImg == m_pParent->m_pImgDouble )
  312. {
  313. // Compute the new position of the left side, and make sure it is
  314. // in the correct range
  315. m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
  316. m_pParent->adjust( m_pParent->m_xPos );
  317. m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
  318. m_pParent->getPosition()->getHeight() );
  319. }
  320. }
  321. void CtrlText::CmdUpdateText::execute()
  322. {
  323. m_pParent->m_xPos -= MOVING_TEXT_STEP;
  324. m_pParent->adjust( m_pParent->m_xPos );
  325. m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
  326. m_pParent->getPosition()->getHeight() );
  327. }
  328. void CtrlText::adjust( int &position )
  329. {
  330. if( !m_pImg || !m_pImgDouble )
  331. return;
  332. // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
  333. // bitmap; remember that the string used to generate m_pImgDouble is of the
  334. // form: "foo foo", the number of spaces being a parameter
  335. position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
  336. if( position > 0 )
  337. {
  338. position -= m_pImgDouble->getWidth() - m_pImg->getWidth();
  339. }
  340. }