Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2747 lines
76 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "vgui_controls/pch_vgui_controls.h"
  8. #include "vgui/ILocalize.h"
  9. // memdbgon must be the last include file in a .cpp file
  10. #include "tier0/memdbgon.h"
  11. enum
  12. {
  13. MAX_BUFFER_SIZE = 999999, // maximum size of text buffer
  14. DRAW_OFFSET_X = 3,
  15. DRAW_OFFSET_Y = 1,
  16. };
  17. using namespace vgui;
  18. #ifndef max
  19. #define max(a,b) (((a) > (b)) ? (a) : (b))
  20. #endif
  21. namespace vgui
  22. {
  23. //#define DRAW_CLICK_PANELS
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Panel used for clickable URL's
  26. //-----------------------------------------------------------------------------
  27. class ClickPanel : public Panel
  28. {
  29. DECLARE_CLASS_SIMPLE( ClickPanel, Panel );
  30. public:
  31. ClickPanel(Panel *parent)
  32. {
  33. _viewIndex = 0;
  34. _textIndex = 0;
  35. SetParent(parent);
  36. AddActionSignalTarget(parent);
  37. SetCursor(dc_hand);
  38. SetPaintBackgroundEnabled(false);
  39. SetPaintEnabled(false);
  40. // SetPaintAppearanceEnabled(false);
  41. #if defined( DRAW_CLICK_PANELS )
  42. SetPaintEnabled(true);
  43. #endif
  44. }
  45. void SetTextIndex( int linkStartIndex, int viewStartIndex )
  46. {
  47. _textIndex = linkStartIndex;
  48. _viewIndex = viewStartIndex;
  49. }
  50. #if defined( DRAW_CLICK_PANELS )
  51. virtual void Paint()
  52. {
  53. surface()->DrawSetColor( Color( 255, 0, 0, 255 ) );
  54. surface()->DrawOutlinedRect( 0, 0, GetWide(), GetTall() );
  55. }
  56. #endif
  57. int GetTextIndex()
  58. {
  59. return _textIndex;
  60. }
  61. int GetViewTextIndex()
  62. {
  63. return _viewIndex;
  64. }
  65. void OnMousePressed(MouseCode code)
  66. {
  67. if (code == MOUSE_LEFT)
  68. {
  69. PostActionSignal(new KeyValues("ClickPanel", "index", _textIndex));
  70. }
  71. else
  72. {
  73. GetParent()->OnMousePressed( code );
  74. }
  75. }
  76. private:
  77. int _textIndex;
  78. int _viewIndex;
  79. };
  80. //-----------------------------------------------------------------------------
  81. // Purpose: Panel used only to draw the interior border region
  82. //-----------------------------------------------------------------------------
  83. class RichTextInterior : public Panel
  84. {
  85. DECLARE_CLASS_SIMPLE( RichTextInterior, Panel );
  86. public:
  87. RichTextInterior( RichText *pParent, const char *pchName ) : BaseClass( pParent, pchName )
  88. {
  89. SetKeyBoardInputEnabled( false );
  90. SetMouseInputEnabled( false );
  91. SetPaintBackgroundEnabled( false );
  92. SetPaintEnabled( false );
  93. m_pRichText = pParent;
  94. }
  95. /* virtual IAppearance *GetAppearance()
  96. {
  97. if ( m_pRichText->IsScrollbarVisible() )
  98. return m_pAppearanceScrollbar;
  99. return BaseClass::GetAppearance();
  100. }*/
  101. virtual void ApplySchemeSettings( IScheme *pScheme )
  102. {
  103. BaseClass::ApplySchemeSettings( pScheme );
  104. // m_pAppearanceScrollbar = FindSchemeAppearance( pScheme, "scrollbar_visible" );
  105. }
  106. private:
  107. RichText *m_pRichText;
  108. // IAppearance *m_pAppearanceScrollbar;
  109. };
  110. }; // namespace vgui
  111. DECLARE_BUILD_FACTORY( RichText );
  112. //-----------------------------------------------------------------------------
  113. // Purpose: Constructor
  114. //-----------------------------------------------------------------------------
  115. RichText::RichText(Panel *parent, const char *panelName) : BaseClass(parent, panelName)
  116. {
  117. m_bAllTextAlphaIsZero = false;
  118. _font = INVALID_FONT;
  119. m_hFontUnderline = INVALID_FONT;
  120. m_bRecalcLineBreaks = true;
  121. m_pszInitialText = NULL;
  122. _cursorPos = 0;
  123. _mouseSelection = false;
  124. _mouseDragSelection = false;
  125. _vertScrollBar = new ScrollBar(this, "ScrollBar", true);
  126. _vertScrollBar->AddActionSignalTarget(this);
  127. _recalcSavedRenderState = true;
  128. _maxCharCount = (64 * 1024);
  129. AddActionSignalTarget(this);
  130. m_pInterior = new RichTextInterior( this, NULL );
  131. //a -1 for _select[0] means that the selection is empty
  132. _select[0] = -1;
  133. _select[1] = -1;
  134. m_pEditMenu = NULL;
  135. SetCursor(dc_ibeam);
  136. //position the cursor so it is at the end of the text
  137. GotoTextEnd();
  138. // set default foreground color to black
  139. _defaultTextColor = Color(0, 0, 0, 0);
  140. // initialize the line break array
  141. InvalidateLineBreakStream();
  142. if ( IsProportional() )
  143. {
  144. int width, height;
  145. int sw,sh;
  146. surface()->GetProportionalBase( width, height );
  147. surface()->GetScreenSize(sw, sh);
  148. _drawOffsetX = static_cast<int>( static_cast<float>( DRAW_OFFSET_X )*( static_cast<float>( sw )/ static_cast<float>( width )));
  149. _drawOffsetY = static_cast<int>( static_cast<float>( DRAW_OFFSET_Y )*( static_cast<float>( sw )/ static_cast<float>( width )));
  150. }
  151. else
  152. {
  153. _drawOffsetX = DRAW_OFFSET_X;
  154. _drawOffsetY = DRAW_OFFSET_Y;
  155. }
  156. // add a basic format string
  157. TFormatStream stream;
  158. stream.color = _defaultTextColor;
  159. stream.fade.flFadeStartTime = 0.0f;
  160. stream.fade.flFadeLength = -1.0f;
  161. stream.pixelsIndent = 0;
  162. stream.textStreamIndex = 0;
  163. stream.textClickable = false;
  164. m_FormatStream.AddToTail(stream);
  165. m_bResetFades = false;
  166. m_bInteractive = true;
  167. m_bUnusedScrollbarInvis = false;
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose: Destructor
  171. //-----------------------------------------------------------------------------
  172. RichText::~RichText()
  173. {
  174. delete [] m_pszInitialText;
  175. delete m_pEditMenu;
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Purpose:
  179. //-----------------------------------------------------------------------------
  180. void RichText::SetDrawOffsets( int ofsx, int ofsy )
  181. {
  182. _drawOffsetX = ofsx;
  183. _drawOffsetY = ofsy;
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Purpose: sets it as drawing text only - used for embedded RichText control into other text drawing situations
  187. //-----------------------------------------------------------------------------
  188. void RichText::SetDrawTextOnly()
  189. {
  190. SetDrawOffsets( 0, 0 );
  191. SetPaintBackgroundEnabled( false );
  192. // SetPaintAppearanceEnabled( false );
  193. SetPostChildPaintEnabled( false );
  194. m_pInterior->SetVisible( false );
  195. SetVerticalScrollbar( false );
  196. }
  197. //-----------------------------------------------------------------------------
  198. // Purpose: configures colors
  199. //-----------------------------------------------------------------------------
  200. void RichText::ApplySchemeSettings(IScheme *pScheme)
  201. {
  202. BaseClass::ApplySchemeSettings(pScheme);
  203. _font = pScheme->GetFont("Default", IsProportional() );
  204. m_hFontUnderline = pScheme->GetFont("DefaultUnderline", IsProportional() );
  205. SetFgColor(GetSchemeColor("RichText.TextColor", pScheme));
  206. SetBgColor(GetSchemeColor("RichText.BgColor", pScheme));
  207. _selectionTextColor = GetSchemeColor("RichText.SelectedTextColor", GetFgColor(), pScheme);
  208. _selectionColor = GetSchemeColor("RichText.SelectedBgColor", pScheme);
  209. if ( Q_strlen( pScheme->GetResourceString( "RichText.InsetX" ) ) )
  210. {
  211. SetDrawOffsets( atoi( pScheme->GetResourceString( "RichText.InsetX" ) ), atoi( pScheme->GetResourceString( "RichText.InsetY" ) ) );
  212. }
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Purpose: if the default format color isn't set then set it
  216. //-----------------------------------------------------------------------------
  217. void RichText::SetFgColor( Color color )
  218. {
  219. // Replace default format color if
  220. // the stream is empty and the color is the default ( or the previous FgColor )
  221. if ( m_FormatStream.Size() == 1 &&
  222. ( m_FormatStream[0].color == _defaultTextColor || m_FormatStream[0].color == GetFgColor() ) )
  223. {
  224. m_FormatStream[0].color = color;
  225. }
  226. BaseClass::SetFgColor( color );
  227. }
  228. //-----------------------------------------------------------------------------
  229. // Purpose: Sends a message if the data has changed
  230. // Turns off any selected text in the window if we are not using the edit menu
  231. //-----------------------------------------------------------------------------
  232. void RichText::OnKillFocus()
  233. {
  234. // check if we clicked the right mouse button or if it is down
  235. bool mouseRightClicked = input()->WasMousePressed(MOUSE_RIGHT);
  236. bool mouseRightUp = input()->WasMouseReleased(MOUSE_RIGHT);
  237. bool mouseRightDown = input()->IsMouseDown(MOUSE_RIGHT);
  238. if (mouseRightClicked || mouseRightDown || mouseRightUp )
  239. {
  240. // get the start and ends of the selection area
  241. int start, end;
  242. if (GetSelectedRange(start, end)) // we have selected text
  243. {
  244. // see if we clicked in the selection area
  245. int startX, startY;
  246. CursorToPixelSpace(start, startX, startY);
  247. int endX, endY;
  248. CursorToPixelSpace(end, endX, endY);
  249. int cursorX, cursorY;
  250. input()->GetCursorPos(cursorX, cursorY);
  251. ScreenToLocal(cursorX, cursorY);
  252. // check the area vertically
  253. // we need to handle the horizontal edge cases eventually
  254. int fontTall = GetLineHeight();
  255. endY = endY + fontTall;
  256. if ((startY < cursorY) && (endY > cursorY))
  257. {
  258. // if we clicked in the selection area, leave the text highlighted
  259. return;
  260. }
  261. }
  262. }
  263. // clear any selection
  264. SelectNone();
  265. // chain
  266. BaseClass::OnKillFocus();
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Purpose: Wipe line breaks after the size of a panel has been changed
  270. //-----------------------------------------------------------------------------
  271. void RichText::OnSizeChanged( int wide, int tall )
  272. {
  273. BaseClass::OnSizeChanged( wide, tall );
  274. // blow away the line breaks list
  275. _invalidateVerticalScrollbarSlider = true;
  276. InvalidateLineBreakStream();
  277. InvalidateLayout();
  278. if ( _vertScrollBar->IsVisible() )
  279. {
  280. _vertScrollBar->MakeReadyForUse();
  281. m_pInterior->SetBounds( 0, 0, wide - _vertScrollBar->GetWide(), tall );
  282. }
  283. else
  284. {
  285. m_pInterior->SetBounds( 0, 0, wide, tall );
  286. }
  287. }
  288. const wchar_t *RichText::ResolveLocalizedTextAndVariables( char const *pchLookup, wchar_t *outbuf, size_t outbufsizeinbytes )
  289. {
  290. if ( pchLookup[ 0 ] == '#' )
  291. {
  292. // try lookup in localization tables
  293. StringIndex_t index = g_pVGuiLocalize->FindIndex( pchLookup + 1 );
  294. if ( index == INVALID_LOCALIZE_STRING_INDEX )
  295. {
  296. /* // if it's not found, maybe it's a special expanded variable - look for an expansion
  297. char rgchT[MAX_PATH];
  298. // get the variables
  299. KeyValues *variables = GetDialogVariables_R();
  300. if ( variables )
  301. {
  302. // see if any are any special vars to put in
  303. for ( KeyValues *pkv = variables->GetFirstSubKey(); pkv != NULL; pkv = pkv->GetNextKey() )
  304. {
  305. if ( !Q_strncmp( pkv->GetName(), "$", 1 ) )
  306. {
  307. // make a new lookup, with this key appended
  308. Q_snprintf( rgchT, sizeof( rgchT ), "%s%s=%s", pchLookup, pkv->GetName(), pkv->GetString() );
  309. index = localize()->FindIndex( rgchT );
  310. break;
  311. }
  312. }
  313. }
  314. */
  315. }
  316. // see if we have a valid string
  317. if ( index != INVALID_LOCALIZE_STRING_INDEX )
  318. {
  319. wchar_t *format = g_pVGuiLocalize->GetValueByIndex( index );
  320. Assert( format );
  321. if ( format )
  322. {
  323. /*// Try and substitute variables if any
  324. KeyValues *variables = GetDialogVariables_R();
  325. if ( variables )
  326. {
  327. localize()->ConstructString( outbuf, outbufsizeinbytes, index, variables );
  328. return outbuf;
  329. }*/
  330. }
  331. V_wcsncpy( outbuf, format, outbufsizeinbytes );
  332. return outbuf;
  333. }
  334. }
  335. Q_UTF8ToUnicode( pchLookup, outbuf, outbufsizeinbytes );
  336. return outbuf;
  337. }
  338. //-----------------------------------------------------------------------------
  339. // Purpose: Set the text array
  340. // Using this function will cause all lineBreaks to be discarded.
  341. // This is because this fxn replaces the contents of the text buffer.
  342. // For modifying large buffers use insert functions.
  343. //-----------------------------------------------------------------------------
  344. void RichText::SetText(const char *text)
  345. {
  346. if (!text)
  347. {
  348. text = "";
  349. }
  350. wchar_t unicode[1024];
  351. if (text[0] == '#')
  352. {
  353. ResolveLocalizedTextAndVariables( text, unicode, sizeof( unicode ) );
  354. SetText( unicode );
  355. return;
  356. }
  357. // convert to unicode
  358. Q_UTF8ToUnicode(text, unicode, sizeof(unicode));
  359. SetText(unicode);
  360. }
  361. //-----------------------------------------------------------------------------
  362. // Purpose:
  363. //-----------------------------------------------------------------------------
  364. void RichText::SetText(const wchar_t *text)
  365. {
  366. // reset the formatting stream
  367. m_FormatStream.RemoveAll();
  368. TFormatStream stream;
  369. stream.color = GetFgColor();
  370. stream.fade.flFadeLength = -1.0f;
  371. stream.fade.flFadeStartTime = 0.0f;
  372. stream.pixelsIndent = 0;
  373. stream.textStreamIndex = 0;
  374. stream.textClickable = false;
  375. m_FormatStream.AddToTail(stream);
  376. // set the new text stream
  377. m_TextStream.RemoveAll();
  378. if ( text && *text )
  379. {
  380. int textLen = wcslen(text) + 1;
  381. m_TextStream.EnsureCapacity(textLen);
  382. for(int i = 0; i < textLen; i++)
  383. {
  384. m_TextStream.AddToTail(text[i]);
  385. }
  386. }
  387. GotoTextStart();
  388. SelectNone();
  389. // blow away the line breaks list
  390. InvalidateLineBreakStream();
  391. InvalidateLayout();
  392. }
  393. //-----------------------------------------------------------------------------
  394. // Purpose: Given cursor's position in the text buffer, convert it to
  395. // the local window's x and y pixel coordinates
  396. // Input: cursorPos: cursor index
  397. // Output: cx, cy, the corresponding coords in the local window
  398. //-----------------------------------------------------------------------------
  399. void RichText::CursorToPixelSpace(int cursorPos, int &cx, int &cy)
  400. {
  401. int yStart = _drawOffsetY;
  402. int x = _drawOffsetX, y = yStart;
  403. _pixelsIndent = 0;
  404. int lineBreakIndexIndex = 0;
  405. for (int i = GetStartDrawIndex(lineBreakIndexIndex); i < m_TextStream.Count(); i++)
  406. {
  407. wchar_t ch = m_TextStream[i];
  408. // if we've found the position, break
  409. if (cursorPos == i)
  410. {
  411. // if we've passed a line break go to that
  412. if (m_LineBreaks[lineBreakIndexIndex] == i)
  413. {
  414. // add another line
  415. AddAnotherLine(x, y);
  416. lineBreakIndexIndex++;
  417. }
  418. break;
  419. }
  420. // if we've passed a line break go to that
  421. if (m_LineBreaks[lineBreakIndexIndex] == i)
  422. {
  423. // add another line
  424. AddAnotherLine(x, y);
  425. lineBreakIndexIndex++;
  426. }
  427. // add to the current position
  428. x += surface()->GetCharacterWidth(_font, ch);
  429. }
  430. cx = x;
  431. cy = y;
  432. }
  433. //-----------------------------------------------------------------------------
  434. // Purpose: Converts local pixel coordinates to an index in the text buffer
  435. //-----------------------------------------------------------------------------
  436. int RichText::PixelToCursorSpace(int cx, int cy)
  437. {
  438. int fontTall = GetLineHeight();
  439. // where to start reading
  440. int yStart = _drawOffsetY;
  441. int x = _drawOffsetX, y = yStart;
  442. _pixelsIndent = 0;
  443. int lineBreakIndexIndex = 0;
  444. int startIndex = GetStartDrawIndex(lineBreakIndexIndex);
  445. if (_recalcSavedRenderState)
  446. {
  447. RecalculateDefaultState(startIndex);
  448. }
  449. _pixelsIndent = m_CachedRenderState.pixelsIndent;
  450. _currentTextClickable = m_CachedRenderState.textClickable;
  451. TRenderState renderState = m_CachedRenderState;
  452. bool onRightLine = false;
  453. int i;
  454. for (i = startIndex; i < m_TextStream.Count(); i++)
  455. {
  456. wchar_t ch = m_TextStream[i];
  457. renderState.x = x;
  458. if ( UpdateRenderState( i, renderState ) )
  459. {
  460. x = renderState.x;
  461. }
  462. // if we are on the right line but off the end of if put the cursor at the end of the line
  463. if (m_LineBreaks[lineBreakIndexIndex] == i)
  464. {
  465. // add another line
  466. AddAnotherLine(x, y);
  467. lineBreakIndexIndex++;
  468. if (onRightLine)
  469. break;
  470. }
  471. // check to see if we're on the right line
  472. if (cy < yStart)
  473. {
  474. // cursor is above panel
  475. onRightLine = true;
  476. }
  477. else if (cy >= y && (cy < (y + fontTall + _drawOffsetY)))
  478. {
  479. onRightLine = true;
  480. }
  481. int wide = surface()->GetCharacterWidth(_font, ch);
  482. // if we've found the position, break
  483. if (onRightLine)
  484. {
  485. if (cx > GetWide()) // off right side of window
  486. {
  487. }
  488. else if (cx < (_drawOffsetX + renderState.pixelsIndent) || cy < yStart) // off left side of window
  489. {
  490. // Msg( "PixelToCursorSpace() off left size, returning %d '%c'\n", i, m_TextStream[i] );
  491. return i; // move cursor one to left
  492. }
  493. if (cx >= x && cx < (x + wide))
  494. {
  495. // check which side of the letter they're on
  496. if (cx < (x + (wide * 0.5))) // left side
  497. {
  498. // Msg( "PixelToCursorSpace() on the left size, returning %d '%c'\n", i, m_TextStream[i] );
  499. return i;
  500. }
  501. else // right side
  502. {
  503. // Msg( "PixelToCursorSpace() on the right size, returning %d '%c'\n", i + 1, m_TextStream[i + 1] );
  504. return i + 1;
  505. }
  506. }
  507. }
  508. x += wide;
  509. }
  510. // Msg( "PixelToCursorSpace() never hit, returning %d\n", i );
  511. return i;
  512. }
  513. //-----------------------------------------------------------------------------
  514. // Purpose: Draws a string of characters in the panel
  515. // Input: iFirst - Index of the first character to draw
  516. // iLast - Index of the last character to draw
  517. // renderState - Render state to use
  518. // font- font to use
  519. // Output: returns the width of the character drawn
  520. //-----------------------------------------------------------------------------
  521. int RichText::DrawString(int iFirst, int iLast, TRenderState &renderState, HFont font)
  522. {
  523. // VPROF( "RichText::DrawString" );
  524. // Calculate the render size
  525. int fontTall = surface()->GetFontTall(font);
  526. // BUGBUG John: This won't exactly match the rendered size
  527. int charWide = 0;
  528. for ( int i = iFirst; i <= iLast; i++ )
  529. {
  530. wchar_t ch = m_TextStream[i];
  531. #if USE_GETKERNEDCHARWIDTH
  532. wchar_t chBefore = 0;
  533. wchar_t chAfter = 0;
  534. if ( i > 0 )
  535. chBefore = m_TextStream[i-1];
  536. if ( i < iLast )
  537. chAfter = m_TextStream[i+1];
  538. float flWide = 0.0f, flabcA = 0.0f;
  539. surface()->GetKernedCharWidth(font, ch, chBefore, chAfter, flWide, flabcA);
  540. if ( ch == L' ' )
  541. flWide = ceil( flWide );
  542. charWide += floor( flWide + 0.6 );
  543. #else
  544. charWide += surface()->GetCharacterWidth(font, ch);
  545. #endif
  546. }
  547. // draw selection, if any
  548. int selection0 = -1, selection1 = -1;
  549. GetSelectedRange(selection0, selection1);
  550. if (iFirst >= selection0 && iFirst < selection1)
  551. {
  552. // draw background selection color
  553. surface()->DrawSetColor(_selectionColor);
  554. surface()->DrawFilledRect(renderState.x, renderState.y, renderState.x + charWide, renderState.y + 1 + fontTall);
  555. // reset text color
  556. surface()->DrawSetTextColor(_selectionTextColor);
  557. m_bAllTextAlphaIsZero = false;
  558. }
  559. else
  560. {
  561. surface()->DrawSetTextColor(renderState.textColor);
  562. }
  563. if ( renderState.textColor.a() != 0 )
  564. {
  565. m_bAllTextAlphaIsZero = false;
  566. surface()->DrawSetTextPos(renderState.x, renderState.y);
  567. surface()->DrawPrintText(&m_TextStream[iFirst], iLast - iFirst + 1);
  568. }
  569. return charWide;
  570. }
  571. //-----------------------------------------------------------------------------
  572. // Purpose: Finish drawing url
  573. //-----------------------------------------------------------------------------
  574. void RichText::FinishingURL(int x, int y)
  575. {
  576. // finishing URL
  577. if ( _clickableTextPanels.IsValidIndex( _clickableTextIndex ) )
  578. {
  579. ClickPanel *clickPanel = _clickableTextPanels[ _clickableTextIndex ];
  580. int px, py;
  581. clickPanel->GetPos(px, py);
  582. int fontTall = GetLineHeight();
  583. clickPanel->SetSize( MAX( x - px, 6 ), y - py + fontTall );
  584. clickPanel->SetVisible(true);
  585. // if we haven't actually advanced any, step back and ignore this one
  586. // this is probably a data input problem though, need to find root cause
  587. if ( x - px <= 0 )
  588. {
  589. --_clickableTextIndex;
  590. clickPanel->SetVisible(false);
  591. }
  592. }
  593. }
  594. void RichText::CalculateFade( TRenderState &renderState )
  595. {
  596. if ( m_FormatStream.IsValidIndex( renderState.formatStreamIndex ) )
  597. {
  598. if ( m_bResetFades == false )
  599. {
  600. if ( m_FormatStream[renderState.formatStreamIndex].fade.flFadeLength != -1.0f )
  601. {
  602. float frac = ( m_FormatStream[renderState.formatStreamIndex].fade.flFadeStartTime - system()->GetCurrentTime() ) / m_FormatStream[renderState.formatStreamIndex].fade.flFadeLength;
  603. int alpha = frac * m_FormatStream[renderState.formatStreamIndex].fade.iOriginalAlpha;
  604. alpha = clamp( alpha, 0, m_FormatStream[renderState.formatStreamIndex].fade.iOriginalAlpha );
  605. renderState.textColor.SetColor( renderState.textColor.r(), renderState.textColor.g(), renderState.textColor.b(), alpha );
  606. }
  607. }
  608. }
  609. }
  610. //-----------------------------------------------------------------------------
  611. // Purpose: Draws the text in the panel
  612. //-----------------------------------------------------------------------------
  613. void RichText::Paint()
  614. {
  615. // Assume the worst
  616. m_bAllTextAlphaIsZero = true;
  617. HFont hFontCurrent = _font;
  618. // hide all the clickable panels until we know where they are to reside
  619. for (int j = 0; j < _clickableTextPanels.Count(); j++)
  620. {
  621. _clickableTextPanels[j]->SetVisible(false);
  622. }
  623. if ( !HasText() )
  624. return;
  625. int wide, tall;
  626. GetSize( wide, tall );
  627. int lineBreakIndexIndex = 0;
  628. int startIndex = GetStartDrawIndex(lineBreakIndexIndex);
  629. _currentTextClickable = false;
  630. _clickableTextIndex = GetClickableTextIndexStart(startIndex);
  631. // recalculate and cache the render state at the render start
  632. if (_recalcSavedRenderState)
  633. {
  634. RecalculateDefaultState(startIndex);
  635. }
  636. // copy off the cached render state
  637. TRenderState renderState = m_CachedRenderState;
  638. _pixelsIndent = m_CachedRenderState.pixelsIndent;
  639. _currentTextClickable = m_CachedRenderState.textClickable;
  640. renderState.textClickable = _currentTextClickable;
  641. if ( m_FormatStream.IsValidIndex( renderState.formatStreamIndex ) )
  642. renderState.textColor = m_FormatStream[renderState.formatStreamIndex].color;
  643. CalculateFade( renderState );
  644. renderState.formatStreamIndex++;
  645. if ( _currentTextClickable )
  646. {
  647. _clickableTextIndex = startIndex;
  648. }
  649. // where to start drawing
  650. renderState.x = _drawOffsetX + _pixelsIndent;
  651. renderState.y = _drawOffsetY;
  652. // draw the text
  653. int selection0 = -1, selection1 = -1;
  654. GetSelectedRange(selection0, selection1);
  655. surface()->DrawSetTextFont( hFontCurrent );
  656. for (int i = startIndex; i < m_TextStream.Count() && renderState.y < tall; )
  657. {
  658. // 1.
  659. // Update our current render state based on the formatting and color streams,
  660. // this has to happen if it's our very first iteration, or if we are actually changing
  661. // state.
  662. int nXBeforeStateChange = renderState.x;
  663. if ( UpdateRenderState(i, renderState) || i == startIndex )
  664. {
  665. // check for url state change
  666. if (renderState.textClickable != _currentTextClickable)
  667. {
  668. if (renderState.textClickable)
  669. {
  670. // entering new URL
  671. _clickableTextIndex++;
  672. hFontCurrent = m_hFontUnderline;
  673. surface()->DrawSetTextFont( hFontCurrent );
  674. // set up the panel
  675. ClickPanel *clickPanel = _clickableTextPanels.IsValidIndex( _clickableTextIndex ) ? _clickableTextPanels[_clickableTextIndex] : NULL;
  676. if (clickPanel)
  677. {
  678. clickPanel->SetPos(renderState.x, renderState.y);
  679. }
  680. }
  681. else
  682. {
  683. FinishingURL(nXBeforeStateChange, renderState.y);
  684. hFontCurrent = _font;
  685. surface()->DrawSetTextFont( hFontCurrent );
  686. }
  687. _currentTextClickable = renderState.textClickable;
  688. }
  689. }
  690. // 2.
  691. // if we've passed a line break go to that
  692. if ( m_LineBreaks.IsValidIndex( lineBreakIndexIndex ) && m_LineBreaks[lineBreakIndexIndex] <= i )
  693. {
  694. if (_currentTextClickable)
  695. {
  696. FinishingURL(renderState.x, renderState.y);
  697. }
  698. // add another line
  699. AddAnotherLine(renderState.x, renderState.y);
  700. lineBreakIndexIndex++;
  701. // Skip white space unless the previous line ended from the hard carriage return
  702. if ( i && ( m_TextStream[i-1] != '\n' ) && ( m_TextStream[i-1] != '\r') )
  703. {
  704. while ( m_TextStream[i] == L' ' )
  705. {
  706. if ( i+1 < m_TextStream.Count() )
  707. ++i;
  708. else
  709. break;
  710. }
  711. }
  712. if (renderState.textClickable)
  713. {
  714. // move to the next URL
  715. _clickableTextIndex++;
  716. ClickPanel *clickPanel = _clickableTextPanels.IsValidIndex( _clickableTextIndex ) ? _clickableTextPanels[_clickableTextIndex] : NULL;
  717. if (clickPanel)
  718. {
  719. clickPanel->SetPos(renderState.x, renderState.y);
  720. }
  721. }
  722. }
  723. // 3.
  724. // Calculate the range of text to draw all at once
  725. int iLim = m_TextStream.Count();
  726. // Stop at the next format change
  727. if ( m_FormatStream.IsValidIndex(renderState.formatStreamIndex) &&
  728. m_FormatStream[renderState.formatStreamIndex].textStreamIndex < iLim &&
  729. m_FormatStream[renderState.formatStreamIndex].textStreamIndex >= i &&
  730. m_FormatStream[renderState.formatStreamIndex].textStreamIndex )
  731. {
  732. iLim = m_FormatStream[renderState.formatStreamIndex].textStreamIndex;
  733. }
  734. // Stop at the next line break
  735. if ( m_LineBreaks.IsValidIndex( lineBreakIndexIndex ) && m_LineBreaks[lineBreakIndexIndex] < iLim )
  736. iLim = m_LineBreaks[lineBreakIndexIndex];
  737. // Handle non-drawing characters specially
  738. for ( int iT = i; iT < iLim; iT++ )
  739. {
  740. if ( iswcntrl(m_TextStream[iT]) )
  741. {
  742. iLim = iT;
  743. break;
  744. }
  745. }
  746. // 4.
  747. // Draw the current text range
  748. if ( iLim <= i )
  749. {
  750. if ( m_TextStream[i] == '\t' )
  751. {
  752. int dxTabWidth = 8 * surface()->GetCharacterWidth(hFontCurrent, ' ');
  753. dxTabWidth = MAX( 1, dxTabWidth );
  754. renderState.x = ( dxTabWidth * ( 1 + ( renderState.x / dxTabWidth ) ) );
  755. }
  756. i++;
  757. }
  758. else
  759. {
  760. renderState.x += DrawString(i, iLim - 1, renderState, hFontCurrent );
  761. i = iLim;
  762. }
  763. }
  764. if (renderState.textClickable)
  765. {
  766. FinishingURL(renderState.x, renderState.y);
  767. }
  768. }
  769. //-----------------------------------------------------------------------------
  770. // Purpose:
  771. //-----------------------------------------------------------------------------
  772. int RichText::GetClickableTextIndexStart(int startIndex)
  773. {
  774. // cycle to the right url panel for what is visible after the startIndex.
  775. for (int i = 0; i < _clickableTextPanels.Count(); i++)
  776. {
  777. if (_clickableTextPanels[i]->GetViewTextIndex() >= startIndex)
  778. {
  779. return i - 1;
  780. }
  781. }
  782. return -1;
  783. }
  784. //-----------------------------------------------------------------------------
  785. // Purpose: Recalcultes the formatting state from the specified index
  786. //-----------------------------------------------------------------------------
  787. void RichText::RecalculateDefaultState(int startIndex)
  788. {
  789. if (!HasText() )
  790. return;
  791. Assert(startIndex < m_TextStream.Count());
  792. m_CachedRenderState.textColor = GetFgColor();
  793. _pixelsIndent = 0;
  794. _currentTextClickable = false;
  795. _clickableTextIndex = GetClickableTextIndexStart(startIndex);
  796. // find where in the formatting stream we need to be
  797. GenerateRenderStateForTextStreamIndex(startIndex, m_CachedRenderState);
  798. _recalcSavedRenderState = false;
  799. }
  800. //-----------------------------------------------------------------------------
  801. // Purpose: updates a render state based on the formatting and color streams
  802. // Output: true if we changed the render state
  803. //-----------------------------------------------------------------------------
  804. bool RichText::UpdateRenderState(int textStreamPos, TRenderState &renderState)
  805. {
  806. // check the color stream
  807. if (m_FormatStream.IsValidIndex(renderState.formatStreamIndex) &&
  808. m_FormatStream[renderState.formatStreamIndex].textStreamIndex == textStreamPos)
  809. {
  810. // set the current formatting
  811. renderState.textColor = m_FormatStream[renderState.formatStreamIndex].color;
  812. renderState.textClickable = m_FormatStream[renderState.formatStreamIndex].textClickable;
  813. CalculateFade( renderState );
  814. int indentChange = m_FormatStream[renderState.formatStreamIndex].pixelsIndent - renderState.pixelsIndent;
  815. renderState.pixelsIndent = m_FormatStream[renderState.formatStreamIndex].pixelsIndent;
  816. if (indentChange)
  817. {
  818. renderState.x = renderState.pixelsIndent + _drawOffsetX;
  819. }
  820. //!! for supporting old functionality, store off state in globals
  821. _pixelsIndent = renderState.pixelsIndent;
  822. // move to the next position in the color stream
  823. renderState.formatStreamIndex++;
  824. return true;
  825. }
  826. return false;
  827. }
  828. //-----------------------------------------------------------------------------
  829. // Purpose: Returns the index in the format stream for the specified text stream index
  830. //-----------------------------------------------------------------------------
  831. int RichText::FindFormatStreamIndexForTextStreamPos(int textStreamIndex)
  832. {
  833. int formatStreamIndex = 0;
  834. for (; m_FormatStream.IsValidIndex(formatStreamIndex); formatStreamIndex++)
  835. {
  836. if (m_FormatStream[formatStreamIndex].textStreamIndex > textStreamIndex)
  837. break;
  838. }
  839. // step back to the color change before the new line
  840. formatStreamIndex--;
  841. if (!m_FormatStream.IsValidIndex(formatStreamIndex))
  842. {
  843. formatStreamIndex = 0;
  844. }
  845. return formatStreamIndex;
  846. }
  847. //-----------------------------------------------------------------------------
  848. // Purpose: Generates a base renderstate given a index into the text stream
  849. //-----------------------------------------------------------------------------
  850. void RichText::GenerateRenderStateForTextStreamIndex(int textStreamIndex, TRenderState &renderState)
  851. {
  852. // find where in the format stream we need to be given the specified place in the text stream
  853. renderState.formatStreamIndex = FindFormatStreamIndexForTextStreamPos(textStreamIndex);
  854. // copy the state data
  855. renderState.textColor = m_FormatStream[renderState.formatStreamIndex].color;
  856. renderState.pixelsIndent = m_FormatStream[renderState.formatStreamIndex].pixelsIndent;
  857. renderState.textClickable = m_FormatStream[renderState.formatStreamIndex].textClickable;
  858. }
  859. //-----------------------------------------------------------------------------
  860. // Purpose: Called pre render
  861. //-----------------------------------------------------------------------------
  862. void RichText::OnThink()
  863. {
  864. if (m_bRecalcLineBreaks)
  865. {
  866. _recalcSavedRenderState = true;
  867. RecalculateLineBreaks();
  868. // recalculate scrollbar position
  869. if (_invalidateVerticalScrollbarSlider)
  870. {
  871. LayoutVerticalScrollBarSlider();
  872. }
  873. }
  874. }
  875. //-----------------------------------------------------------------------------
  876. // Purpose: Called when data changes or panel size changes
  877. //-----------------------------------------------------------------------------
  878. void RichText::PerformLayout()
  879. {
  880. BaseClass::PerformLayout();
  881. // force a Repaint
  882. Repaint();
  883. }
  884. //-----------------------------------------------------------------------------
  885. // Purpose: inserts a color change into the formatting stream
  886. //-----------------------------------------------------------------------------
  887. void RichText::InsertColorChange(Color col)
  888. {
  889. // see if color already exists in text stream
  890. TFormatStream &prevItem = m_FormatStream[m_FormatStream.Count() - 1];
  891. if (prevItem.color == col)
  892. {
  893. // inserting same color into stream, just ignore
  894. }
  895. else if (prevItem.textStreamIndex == m_TextStream.Count())
  896. {
  897. // this item is in the same place; update values
  898. prevItem.color = col;
  899. }
  900. else
  901. {
  902. // add to text stream, based off existing item
  903. TFormatStream streamItem = prevItem;
  904. streamItem.color = col;
  905. streamItem.textStreamIndex = m_TextStream.Count();
  906. m_FormatStream.AddToTail(streamItem);
  907. }
  908. }
  909. //-----------------------------------------------------------------------------
  910. // Purpose: inserts a fade into the formatting stream
  911. //-----------------------------------------------------------------------------
  912. void RichText::InsertFade( float flSustain, float flLength )
  913. {
  914. // see if color already exists in text stream
  915. TFormatStream &prevItem = m_FormatStream[m_FormatStream.Count() - 1];
  916. if (prevItem.textStreamIndex == m_TextStream.Count())
  917. {
  918. // this item is in the same place; update values
  919. prevItem.fade.flFadeStartTime = system()->GetCurrentTime() + flSustain;
  920. prevItem.fade.flFadeSustain = flSustain;
  921. prevItem.fade.flFadeLength = flLength;
  922. prevItem.fade.iOriginalAlpha = prevItem.color.a();
  923. }
  924. else
  925. {
  926. // add to text stream, based off existing item
  927. TFormatStream streamItem = prevItem;
  928. prevItem.fade.flFadeStartTime = system()->GetCurrentTime() + flSustain;
  929. prevItem.fade.flFadeLength = flLength;
  930. prevItem.fade.flFadeSustain = flSustain;
  931. prevItem.fade.iOriginalAlpha = prevItem.color.a();
  932. streamItem.textStreamIndex = m_TextStream.Count();
  933. m_FormatStream.AddToTail(streamItem);
  934. }
  935. }
  936. void RichText::ResetAllFades( bool bHold, bool bOnlyExpired, float flNewSustain )
  937. {
  938. m_bResetFades = bHold;
  939. if ( m_bResetFades == false )
  940. {
  941. for (int i = 1; i < m_FormatStream.Count(); i++)
  942. {
  943. if ( bOnlyExpired == true )
  944. {
  945. if ( m_FormatStream[i].fade.flFadeStartTime >= system()->GetCurrentTime() )
  946. continue;
  947. }
  948. if ( flNewSustain == -1.0f )
  949. {
  950. flNewSustain = m_FormatStream[i].fade.flFadeSustain;
  951. }
  952. m_FormatStream[i].fade.flFadeStartTime = system()->GetCurrentTime() + flNewSustain;
  953. }
  954. }
  955. }
  956. //-----------------------------------------------------------------------------
  957. // Purpose: inserts an indent change into the formatting stream
  958. //-----------------------------------------------------------------------------
  959. void RichText::InsertIndentChange(int pixelsIndent)
  960. {
  961. if (pixelsIndent < 0)
  962. {
  963. pixelsIndent = 0;
  964. }
  965. else if (pixelsIndent > 255)
  966. {
  967. pixelsIndent = 255;
  968. }
  969. // see if indent change already exists in text stream
  970. TFormatStream &prevItem = m_FormatStream[m_FormatStream.Count() - 1];
  971. if (prevItem.pixelsIndent == pixelsIndent)
  972. {
  973. // inserting same indent into stream, just ignore
  974. }
  975. else if (prevItem.textStreamIndex == m_TextStream.Count())
  976. {
  977. // this item is in the same place; update
  978. prevItem.pixelsIndent = pixelsIndent;
  979. }
  980. else
  981. {
  982. // add to text stream, based off existing item
  983. TFormatStream streamItem = prevItem;
  984. streamItem.pixelsIndent = pixelsIndent;
  985. streamItem.textStreamIndex = m_TextStream.Count();
  986. m_FormatStream.AddToTail(streamItem);
  987. }
  988. }
  989. //-----------------------------------------------------------------------------
  990. // Purpose: Inserts character Start for clickable text, eg. URLS
  991. //-----------------------------------------------------------------------------
  992. void RichText::InsertClickableTextStart( const char *pchClickAction )
  993. {
  994. // see if indent change already exists in text stream
  995. TFormatStream &prevItem = m_FormatStream[m_FormatStream.Count() - 1];
  996. TFormatStream *pFormatStream = &prevItem;
  997. if (prevItem.textStreamIndex == m_TextStream.Count())
  998. {
  999. // this item is in the same place; update
  1000. prevItem.textClickable = true;
  1001. pFormatStream->m_sClickableTextAction = pchClickAction;
  1002. }
  1003. else
  1004. {
  1005. // add to text stream, based off existing item
  1006. TFormatStream formatStreamCopy = prevItem;
  1007. int iFormatStream = m_FormatStream.AddToTail( formatStreamCopy );
  1008. // set the new params
  1009. pFormatStream = &m_FormatStream[iFormatStream];
  1010. pFormatStream->textStreamIndex = m_TextStream.Count();
  1011. pFormatStream->textClickable = true;
  1012. pFormatStream->m_sClickableTextAction = pchClickAction;
  1013. }
  1014. // invalidate the layout to recalculate where the click panels should go
  1015. InvalidateLineBreakStream();
  1016. InvalidateLayout();
  1017. }
  1018. //-----------------------------------------------------------------------------
  1019. // Purpose: Inserts character end for clickable text, eg. URLS
  1020. //-----------------------------------------------------------------------------
  1021. void RichText::InsertClickableTextEnd()
  1022. {
  1023. // see if indent change already exists in text stream
  1024. TFormatStream &prevItem = m_FormatStream[m_FormatStream.Count() - 1];
  1025. if (!prevItem.textClickable)
  1026. {
  1027. // inserting same indent into stream, just ignore
  1028. }
  1029. else if (prevItem.textStreamIndex == m_TextStream.Count())
  1030. {
  1031. // this item is in the same place; update
  1032. prevItem.textClickable = false;
  1033. }
  1034. else
  1035. {
  1036. // add to text stream, based off existing item
  1037. TFormatStream streamItem = prevItem;
  1038. streamItem.textClickable = false;
  1039. streamItem.textStreamIndex = m_TextStream.Count();
  1040. m_FormatStream.AddToTail(streamItem);
  1041. }
  1042. }
  1043. //-----------------------------------------------------------------------------
  1044. // Purpose: moves x,y to the Start of the next line of text
  1045. //-----------------------------------------------------------------------------
  1046. void RichText::AddAnotherLine(int &cx, int &cy)
  1047. {
  1048. cx = _drawOffsetX + _pixelsIndent;
  1049. cy += (GetLineHeight() + _drawOffsetY);
  1050. }
  1051. //-----------------------------------------------------------------------------
  1052. // Purpose: Recalculates line breaks
  1053. //-----------------------------------------------------------------------------
  1054. void RichText::RecalculateLineBreaks()
  1055. {
  1056. if ( !m_bRecalcLineBreaks )
  1057. return;
  1058. int wide = GetWide();
  1059. if (!wide)
  1060. return;
  1061. wide -= _drawOffsetX;
  1062. m_bRecalcLineBreaks = false;
  1063. _recalcSavedRenderState = true;
  1064. if (!HasText())
  1065. return;
  1066. int selection0 = -1, selection1 = -1;
  1067. // subtract the scrollbar width
  1068. if (_vertScrollBar->IsVisible())
  1069. {
  1070. wide -= _vertScrollBar->GetWide();
  1071. }
  1072. int x = _drawOffsetX, y = _drawOffsetY;
  1073. HFont fontWordStart = INVALID_FONT;
  1074. int wordStartIndex = 0;
  1075. int lineStartIndex = 0;
  1076. bool hasWord = false;
  1077. bool justStartedNewLine = true;
  1078. bool wordStartedOnNewLine = true;
  1079. int startChar = 0;
  1080. if (_recalculateBreaksIndex <= 0)
  1081. {
  1082. m_LineBreaks.RemoveAll();
  1083. }
  1084. else
  1085. {
  1086. // remove the rest of the linebreaks list since its out of date.
  1087. for (int i = _recalculateBreaksIndex + 1; i < m_LineBreaks.Count(); ++i)
  1088. {
  1089. m_LineBreaks.Remove(i);
  1090. --i; // removing shrinks the list!
  1091. }
  1092. startChar = m_LineBreaks[_recalculateBreaksIndex];
  1093. lineStartIndex = m_LineBreaks[_recalculateBreaksIndex];
  1094. wordStartIndex = lineStartIndex;
  1095. }
  1096. // handle the case where this char is a new line, in that case
  1097. // we have already taken its break index into account above so skip it.
  1098. if (m_TextStream[startChar] == '\r' || m_TextStream[startChar] == '\n')
  1099. {
  1100. startChar++;
  1101. lineStartIndex = startChar;
  1102. }
  1103. // cycle to the right url panel for what is visible after the startIndex.
  1104. int clickableTextNum = GetClickableTextIndexStart(startChar);
  1105. clickableTextNum++;
  1106. // initialize the renderstate with the start
  1107. TRenderState renderState;
  1108. GenerateRenderStateForTextStreamIndex(startChar, renderState);
  1109. _currentTextClickable = false;
  1110. HFont font = _font;
  1111. bool bForceBreak = false;
  1112. float flLineWidthSoFar = 0;
  1113. // loop through all the characters
  1114. for (int i = startChar; i < m_TextStream.Count(); ++i)
  1115. {
  1116. wchar_t ch = m_TextStream[i];
  1117. renderState.x = x;
  1118. if (UpdateRenderState(i, renderState))
  1119. {
  1120. x = renderState.x;
  1121. int preI = i;
  1122. // check for clickable text
  1123. if (renderState.textClickable != _currentTextClickable)
  1124. {
  1125. if (renderState.textClickable)
  1126. {
  1127. // make a new clickable text panel
  1128. if (clickableTextNum >= _clickableTextPanels.Count())
  1129. {
  1130. _clickableTextPanels.AddToTail(new ClickPanel(this));
  1131. }
  1132. ClickPanel *clickPanel = _clickableTextPanels[clickableTextNum++];
  1133. clickPanel->SetTextIndex(preI, preI);
  1134. }
  1135. // url state change
  1136. _currentTextClickable = renderState.textClickable;
  1137. }
  1138. }
  1139. bool bIsWSpace = iswspace( ch ) ? true : false;
  1140. bool bPreviousWordStartedOnNewLine = wordStartedOnNewLine;
  1141. int iPreviousWordStartIndex = wordStartIndex;
  1142. if ( !bIsWSpace && ch != L'\t' && ch != L'\n' && ch != L'\r' )
  1143. {
  1144. if (!hasWord)
  1145. {
  1146. // Start a new word
  1147. wordStartIndex = i;
  1148. hasWord = true;
  1149. wordStartedOnNewLine = justStartedNewLine;
  1150. fontWordStart = font;
  1151. }
  1152. // else append to the current word
  1153. }
  1154. else
  1155. {
  1156. // whitespace/punctuation character
  1157. // end the word
  1158. hasWord = false;
  1159. }
  1160. float w = 0;
  1161. wchar_t wchBefore = 0;
  1162. wchar_t wchAfter = 0;
  1163. if ( i > 0 && i > lineStartIndex && i != selection0 && i-1 != selection1 )
  1164. wchBefore = m_TextStream[i-1];
  1165. if ( i < m_TextStream.Count() - 1 && i+1 != selection0 && i != selection1 )
  1166. wchAfter = m_TextStream[i+1];
  1167. float flabcA;
  1168. surface()->GetKernedCharWidth( font, ch, wchBefore, wchAfter, w, flabcA );
  1169. flLineWidthSoFar += w;
  1170. // See if we've exceeded the width we have available, with
  1171. if ( floor(flLineWidthSoFar + 0.6) + x > wide )
  1172. {
  1173. bForceBreak = true;
  1174. }
  1175. if (!iswcntrl(ch))
  1176. {
  1177. justStartedNewLine = false;
  1178. }
  1179. if ( bForceBreak || ch == '\r' || ch == '\n' )
  1180. {
  1181. bForceBreak = false;
  1182. // add another line
  1183. AddAnotherLine(x, y);
  1184. if ( ch == '\r' || ch == '\n' )
  1185. {
  1186. // skip the newline so it's not at the beginning of the new line
  1187. lineStartIndex = i + 1;
  1188. m_LineBreaks.AddToTail(i + 1);
  1189. }
  1190. else if ( bPreviousWordStartedOnNewLine || iPreviousWordStartIndex <= lineStartIndex )
  1191. {
  1192. lineStartIndex = i;
  1193. m_LineBreaks.AddToTail( i );
  1194. if (renderState.textClickable)
  1195. {
  1196. // need to split the url into two panels
  1197. int oldIndex = _clickableTextPanels[clickableTextNum - 1]->GetTextIndex();
  1198. // make a new clickable text panel
  1199. if (clickableTextNum >= _clickableTextPanels.Count())
  1200. {
  1201. _clickableTextPanels.AddToTail(new ClickPanel(this));
  1202. }
  1203. ClickPanel *clickPanel = _clickableTextPanels[clickableTextNum++];
  1204. clickPanel->SetTextIndex(oldIndex, i);
  1205. }
  1206. }
  1207. else
  1208. {
  1209. m_LineBreaks.AddToTail( iPreviousWordStartIndex );
  1210. lineStartIndex = iPreviousWordStartIndex;
  1211. i = iPreviousWordStartIndex;
  1212. TRenderState renderStateAtLastWord;
  1213. GenerateRenderStateForTextStreamIndex( i, renderStateAtLastWord );
  1214. // If the word is clickable, and that started prior to the beginning of the word, then we must split the click panel
  1215. if ( renderStateAtLastWord.textClickable && m_FormatStream[ renderStateAtLastWord.formatStreamIndex ].textStreamIndex < i )
  1216. {
  1217. // need to split the url into two panels
  1218. int oldIndex = _clickableTextPanels[clickableTextNum - 1]->GetTextIndex();
  1219. // make a new clickable text panel
  1220. if (clickableTextNum >= _clickableTextPanels.Count())
  1221. {
  1222. _clickableTextPanels.AddToTail(new ClickPanel(this));
  1223. }
  1224. ClickPanel *clickPanel = _clickableTextPanels[clickableTextNum++];
  1225. clickPanel->SetTextIndex(oldIndex, i);
  1226. }
  1227. }
  1228. flLineWidthSoFar = 0;
  1229. justStartedNewLine = true;
  1230. hasWord = false;
  1231. wordStartedOnNewLine = false;
  1232. _currentTextClickable = false;
  1233. continue;
  1234. }
  1235. }
  1236. // end the list
  1237. m_LineBreaks.AddToTail(MAX_BUFFER_SIZE);
  1238. // set up the scrollbar
  1239. _invalidateVerticalScrollbarSlider = true;
  1240. }
  1241. //-----------------------------------------------------------------------------
  1242. // Purpose: Recalculate where the vertical scroll bar slider should be
  1243. // based on the current cursor line we are on.
  1244. //-----------------------------------------------------------------------------
  1245. void RichText::LayoutVerticalScrollBarSlider()
  1246. {
  1247. _invalidateVerticalScrollbarSlider = false;
  1248. // set up the scrollbar
  1249. //if (!_vertScrollBar->IsVisible())
  1250. // return;
  1251. // see where the scrollbar currently is
  1252. int previousValue = _vertScrollBar->GetValue();
  1253. bool bCurrentlyAtEnd = false;
  1254. int rmin, rmax;
  1255. _vertScrollBar->GetRange(rmin, rmax);
  1256. if (rmax && (previousValue + rmin + _vertScrollBar->GetRangeWindow() == rmax))
  1257. {
  1258. bCurrentlyAtEnd = true;
  1259. }
  1260. // work out position to put scrollbar, factoring in insets
  1261. int wide, tall;
  1262. GetSize( wide, tall );
  1263. _vertScrollBar->SetPos( wide - _vertScrollBar->GetWide(), 0 );
  1264. // scrollbar is inside the borders.
  1265. _vertScrollBar->SetSize( _vertScrollBar->GetWide(), tall );
  1266. // calculate how many lines we can fully display
  1267. int displayLines = tall / (GetLineHeight() + _drawOffsetY);
  1268. int numLines = m_LineBreaks.Count();
  1269. if (numLines <= displayLines)
  1270. {
  1271. // disable the scrollbar
  1272. _vertScrollBar->SetEnabled(false);
  1273. _vertScrollBar->SetRange(0, numLines);
  1274. _vertScrollBar->SetRangeWindow(numLines);
  1275. _vertScrollBar->SetValue(0);
  1276. if ( m_bUnusedScrollbarInvis )
  1277. {
  1278. SetVerticalScrollbar( false );
  1279. }
  1280. }
  1281. else
  1282. {
  1283. if ( m_bUnusedScrollbarInvis )
  1284. {
  1285. SetVerticalScrollbar( true );
  1286. }
  1287. // set the scrollbars range
  1288. _vertScrollBar->SetRange(0, numLines);
  1289. _vertScrollBar->SetRangeWindow(displayLines);
  1290. _vertScrollBar->SetEnabled(true);
  1291. // this should make it scroll one line at a time
  1292. _vertScrollBar->SetButtonPressedScrollValue(1);
  1293. if (bCurrentlyAtEnd)
  1294. {
  1295. _vertScrollBar->SetValue(numLines - displayLines);
  1296. }
  1297. _vertScrollBar->InvalidateLayout();
  1298. _vertScrollBar->Repaint();
  1299. }
  1300. }
  1301. //-----------------------------------------------------------------------------
  1302. // Purpose: Sets whether a vertical scrollbar is visible
  1303. //-----------------------------------------------------------------------------
  1304. void RichText::SetVerticalScrollbar(bool state)
  1305. {
  1306. if (_vertScrollBar->IsVisible() != state)
  1307. {
  1308. _vertScrollBar->SetVisible(state);
  1309. InvalidateLineBreakStream();
  1310. InvalidateLayout();
  1311. }
  1312. }
  1313. //-----------------------------------------------------------------------------
  1314. // Purpose: Create cut/copy/paste dropdown menu
  1315. //-----------------------------------------------------------------------------
  1316. void RichText::CreateEditMenu()
  1317. {
  1318. // create a drop down cut/copy/paste menu appropriate for this object's states
  1319. if (m_pEditMenu)
  1320. delete m_pEditMenu;
  1321. m_pEditMenu = new Menu(this, "EditMenu");
  1322. // add cut/copy/paste drop down options if its editable, just copy if it is not
  1323. m_pEditMenu->AddMenuItem("C&opy", new KeyValues("DoCopySelected"), this);
  1324. m_pEditMenu->SetVisible(false);
  1325. m_pEditMenu->SetParent(this);
  1326. m_pEditMenu->AddActionSignalTarget(this);
  1327. }
  1328. //-----------------------------------------------------------------------------
  1329. // Purpose: We want single line windows to scroll horizontally and select text
  1330. // in response to clicking and holding outside window
  1331. //-----------------------------------------------------------------------------
  1332. void RichText::OnMouseFocusTicked()
  1333. {
  1334. // if a button is down move the scrollbar slider the appropriate direction
  1335. if (_mouseDragSelection) // text is being selected via mouse clicking and dragging
  1336. {
  1337. OnCursorMoved(0,0); // we want the text to scroll as if we were dragging
  1338. }
  1339. }
  1340. //-----------------------------------------------------------------------------
  1341. // Purpose: If a cursor enters the window, we are not elegible for
  1342. // MouseFocusTicked events
  1343. //-----------------------------------------------------------------------------
  1344. void RichText::OnCursorEntered()
  1345. {
  1346. _mouseDragSelection = false; // outside of window dont recieve drag scrolling ticks
  1347. }
  1348. //-----------------------------------------------------------------------------
  1349. // Purpose: When the cursor is outside the window, if we are holding the mouse
  1350. // button down, then we want the window to scroll the text one char at a time using Ticks
  1351. //-----------------------------------------------------------------------------
  1352. void RichText::OnCursorExited()
  1353. {
  1354. // outside of window recieve drag scrolling ticks
  1355. if (_mouseSelection)
  1356. {
  1357. _mouseDragSelection = true;
  1358. }
  1359. }
  1360. //-----------------------------------------------------------------------------
  1361. // Purpose: Handle selection of text by mouse
  1362. //-----------------------------------------------------------------------------
  1363. void RichText::OnCursorMoved(int ignX, int ignY)
  1364. {
  1365. if (_mouseSelection)
  1366. {
  1367. // update the cursor position
  1368. int x, y;
  1369. input()->GetCursorPos(x, y);
  1370. ScreenToLocal(x, y);
  1371. _cursorPos = PixelToCursorSpace(x, y);
  1372. if (_cursorPos != _select[1])
  1373. {
  1374. _select[1] = _cursorPos;
  1375. Repaint();
  1376. }
  1377. // Msg( "selecting range [%d..%d]\n", _select[0], _select[1] );
  1378. }
  1379. }
  1380. //-----------------------------------------------------------------------------
  1381. // Purpose: Handle mouse button down events.
  1382. //-----------------------------------------------------------------------------
  1383. void RichText::OnMousePressed(MouseCode code)
  1384. {
  1385. if (code == MOUSE_LEFT)
  1386. {
  1387. // clear current selection
  1388. SelectNone();
  1389. // move the cursor to where the mouse was pressed
  1390. int x, y;
  1391. input()->GetCursorPos(x, y);
  1392. ScreenToLocal(x, y);
  1393. _cursorPos = PixelToCursorSpace(x, y);
  1394. if ( m_bInteractive )
  1395. {
  1396. // enter selection mode
  1397. input()->SetMouseCapture(GetVPanel());
  1398. _mouseSelection = true;
  1399. if (_select[0] < 0)
  1400. {
  1401. // if no initial selection position, Start selection position at cursor
  1402. _select[0] = _cursorPos;
  1403. }
  1404. _select[1] = _cursorPos;
  1405. }
  1406. RequestFocus();
  1407. Repaint();
  1408. }
  1409. else if (code == MOUSE_RIGHT) // check for context menu open
  1410. {
  1411. if ( m_bInteractive )
  1412. {
  1413. CreateEditMenu();
  1414. Assert(m_pEditMenu);
  1415. OpenEditMenu();
  1416. }
  1417. }
  1418. }
  1419. //-----------------------------------------------------------------------------
  1420. // Purpose: Handle mouse button up events
  1421. //-----------------------------------------------------------------------------
  1422. void RichText::OnMouseReleased(MouseCode code)
  1423. {
  1424. _mouseSelection = false;
  1425. input()->SetMouseCapture(NULL);
  1426. // make sure something has been selected
  1427. int cx0, cx1;
  1428. if (GetSelectedRange(cx0, cx1))
  1429. {
  1430. if (cx1 - cx0 == 0)
  1431. {
  1432. // nullify selection
  1433. _select[0] = -1;
  1434. }
  1435. }
  1436. }
  1437. //-----------------------------------------------------------------------------
  1438. // Purpose: Handle mouse double clicks
  1439. //-----------------------------------------------------------------------------
  1440. void RichText::OnMouseDoublePressed(MouseCode code)
  1441. {
  1442. if ( !m_bInteractive )
  1443. return;
  1444. // left double clicking on a word selects the word
  1445. if (code == MOUSE_LEFT)
  1446. {
  1447. // move the cursor just as if you single clicked.
  1448. OnMousePressed(code);
  1449. // then find the start and end of the word we are in to highlight it.
  1450. int selectSpot[2];
  1451. GotoWordLeft();
  1452. selectSpot[0] = _cursorPos;
  1453. GotoWordRight();
  1454. selectSpot[1] = _cursorPos;
  1455. if ( _cursorPos > 0 && (_cursorPos-1) < m_TextStream.Count() )
  1456. {
  1457. if (iswspace(m_TextStream[_cursorPos-1]))
  1458. {
  1459. selectSpot[1]--;
  1460. _cursorPos--;
  1461. }
  1462. }
  1463. _select[0] = selectSpot[0];
  1464. _select[1] = selectSpot[1];
  1465. _mouseSelection = true;
  1466. }
  1467. }
  1468. //-----------------------------------------------------------------------------
  1469. // Purpose: Turn off text selection code when mouse button is not down
  1470. //-----------------------------------------------------------------------------
  1471. void RichText::OnMouseCaptureLost()
  1472. {
  1473. _mouseSelection = false;
  1474. }
  1475. //-----------------------------------------------------------------------------
  1476. // Purpose: Masks which keys get chained up
  1477. // Maps keyboard input to text window functions.
  1478. //-----------------------------------------------------------------------------
  1479. void RichText::OnKeyCodeTyped(KeyCode code)
  1480. {
  1481. bool shift = (input()->IsKeyDown(KEY_LSHIFT) || input()->IsKeyDown(KEY_RSHIFT));
  1482. bool ctrl = (input()->IsKeyDown(KEY_LCONTROL) || input()->IsKeyDown(KEY_RCONTROL));
  1483. bool alt = (input()->IsKeyDown(KEY_LALT) || input()->IsKeyDown(KEY_RALT));
  1484. bool winkey = (input()->IsKeyDown(KEY_LWIN) || input()->IsKeyDown(KEY_RWIN));
  1485. bool fallThrough = false;
  1486. if ( ctrl || ( winkey && IsOSX() ) )
  1487. {
  1488. switch(code)
  1489. {
  1490. case KEY_INSERT:
  1491. case KEY_C:
  1492. case KEY_X:
  1493. {
  1494. CopySelected();
  1495. break;
  1496. }
  1497. case KEY_PAGEUP:
  1498. case KEY_HOME:
  1499. {
  1500. GotoTextStart();
  1501. break;
  1502. }
  1503. case KEY_PAGEDOWN:
  1504. case KEY_END:
  1505. {
  1506. GotoTextEnd();
  1507. break;
  1508. }
  1509. default:
  1510. {
  1511. fallThrough = true;
  1512. break;
  1513. }
  1514. }
  1515. }
  1516. else if (alt)
  1517. {
  1518. // do nothing with ALT-x keys
  1519. fallThrough = true;
  1520. }
  1521. else
  1522. {
  1523. switch(code)
  1524. {
  1525. case KEY_TAB:
  1526. case KEY_LSHIFT:
  1527. case KEY_RSHIFT:
  1528. case KEY_ESCAPE:
  1529. case KEY_ENTER:
  1530. {
  1531. fallThrough = true;
  1532. break;
  1533. }
  1534. case KEY_DELETE:
  1535. {
  1536. if (shift)
  1537. {
  1538. // shift-delete is cut
  1539. CopySelected();
  1540. }
  1541. break;
  1542. }
  1543. case KEY_HOME:
  1544. {
  1545. GotoTextStart();
  1546. break;
  1547. }
  1548. case KEY_END:
  1549. {
  1550. GotoTextEnd();
  1551. break;
  1552. }
  1553. case KEY_PAGEUP:
  1554. {
  1555. // if there is a scroll bar scroll down one rangewindow
  1556. if (_vertScrollBar->IsVisible())
  1557. {
  1558. int window = _vertScrollBar->GetRangeWindow();
  1559. int newval = _vertScrollBar->GetValue();
  1560. _vertScrollBar->SetValue(newval - window - 1);
  1561. }
  1562. break;
  1563. }
  1564. case KEY_PAGEDOWN:
  1565. {
  1566. // if there is a scroll bar scroll down one rangewindow
  1567. if (_vertScrollBar->IsVisible())
  1568. {
  1569. int window = _vertScrollBar->GetRangeWindow();
  1570. int newval = _vertScrollBar->GetValue();
  1571. _vertScrollBar->SetValue(newval + window + 1);
  1572. }
  1573. break;
  1574. }
  1575. default:
  1576. {
  1577. // return if any other char is pressed.
  1578. // as it will be a unicode char.
  1579. // and we don't want select[1] changed unless a char was pressed that this fxn handles
  1580. return;
  1581. }
  1582. }
  1583. }
  1584. // select[1] is the location in the line where the blinking cursor started
  1585. _select[1] = _cursorPos;
  1586. // chain back on some keys
  1587. if (fallThrough)
  1588. {
  1589. BaseClass::OnKeyCodeTyped(code);
  1590. }
  1591. }
  1592. //-----------------------------------------------------------------------------
  1593. // Purpose: Scrolls the list according to the mouse wheel movement
  1594. //-----------------------------------------------------------------------------
  1595. void RichText::OnMouseWheeled(int delta)
  1596. {
  1597. MoveScrollBar(delta);
  1598. }
  1599. //-----------------------------------------------------------------------------
  1600. // Purpose: Scrolls the list
  1601. // Input : delta - amount to move scrollbar up
  1602. //-----------------------------------------------------------------------------
  1603. void RichText::MoveScrollBar(int delta)
  1604. {
  1605. MoveScrollBarDirect( delta * 3 );
  1606. }
  1607. //-----------------------------------------------------------------------------
  1608. // Purpose: Scrolls the list
  1609. // Input : delta - amount to move scrollbar up
  1610. //-----------------------------------------------------------------------------
  1611. void RichText::MoveScrollBarDirect(int delta)
  1612. {
  1613. if (_vertScrollBar->IsVisible())
  1614. {
  1615. int val = _vertScrollBar->GetValue();
  1616. val -= delta;
  1617. _vertScrollBar->SetValue(val);
  1618. _recalcSavedRenderState = true;
  1619. }
  1620. }
  1621. //-----------------------------------------------------------------------------
  1622. // Purpose: set the maximum number of chars in the text buffer
  1623. //-----------------------------------------------------------------------------
  1624. void RichText::SetMaximumCharCount(int maxChars)
  1625. {
  1626. _maxCharCount = maxChars;
  1627. }
  1628. //-----------------------------------------------------------------------------
  1629. // Purpose: Find out what line the cursor is on
  1630. //-----------------------------------------------------------------------------
  1631. int RichText::GetCursorLine()
  1632. {
  1633. // always returns the last place
  1634. int pos = m_LineBreaks[m_LineBreaks.Count() - 1];
  1635. Assert(pos == MAX_BUFFER_SIZE);
  1636. return pos;
  1637. }
  1638. //-----------------------------------------------------------------------------
  1639. // Purpose: Move the cursor over to the Start of the next word to the right
  1640. //-----------------------------------------------------------------------------
  1641. void RichText::GotoWordRight()
  1642. {
  1643. // search right until we hit a whitespace character or a newline
  1644. while (++_cursorPos < m_TextStream.Count())
  1645. {
  1646. if (iswspace(m_TextStream[_cursorPos]))
  1647. break;
  1648. }
  1649. // search right until we hit an nonspace character
  1650. while (++_cursorPos < m_TextStream.Count())
  1651. {
  1652. if (!iswspace(m_TextStream[_cursorPos]))
  1653. break;
  1654. }
  1655. if (_cursorPos > m_TextStream.Count())
  1656. {
  1657. _cursorPos = m_TextStream.Count();
  1658. }
  1659. // now we are at the start of the next word
  1660. Repaint();
  1661. }
  1662. //-----------------------------------------------------------------------------
  1663. // Purpose: Move the cursor over to the Start of the next word to the left
  1664. //-----------------------------------------------------------------------------
  1665. void RichText::GotoWordLeft()
  1666. {
  1667. if (_cursorPos < 1)
  1668. return;
  1669. // search left until we hit an nonspace character
  1670. while (--_cursorPos >= 0)
  1671. {
  1672. if (!iswspace(m_TextStream[_cursorPos]))
  1673. break;
  1674. }
  1675. // search left until we hit a whitespace character
  1676. while (--_cursorPos >= 0)
  1677. {
  1678. if (iswspace(m_TextStream[_cursorPos]))
  1679. {
  1680. break;
  1681. }
  1682. }
  1683. // we end one character off
  1684. _cursorPos++;
  1685. // now we are at the start of the previous word
  1686. Repaint();
  1687. }
  1688. //-----------------------------------------------------------------------------
  1689. // Purpose: Move cursor to the Start of the text buffer
  1690. //-----------------------------------------------------------------------------
  1691. void RichText::GotoTextStart()
  1692. {
  1693. _cursorPos = 0; // set cursor to start
  1694. _invalidateVerticalScrollbarSlider = true;
  1695. // force scrollbar to the top
  1696. _vertScrollBar->SetValue(0);
  1697. Repaint();
  1698. }
  1699. //-----------------------------------------------------------------------------
  1700. // Purpose: Move cursor to the end of the text buffer
  1701. //-----------------------------------------------------------------------------
  1702. void RichText::GotoTextEnd()
  1703. {
  1704. _cursorPos = m_TextStream.Count(); // set cursor to end of buffer
  1705. _invalidateVerticalScrollbarSlider = true;
  1706. // force the scrollbar to the bottom
  1707. int min, max;
  1708. _vertScrollBar->GetRange(min, max);
  1709. _vertScrollBar->SetValue(max);
  1710. Repaint();
  1711. }
  1712. //-----------------------------------------------------------------------------
  1713. // Purpose: Culls the text stream down to a managable size
  1714. //-----------------------------------------------------------------------------
  1715. void RichText::TruncateTextStream()
  1716. {
  1717. if (_maxCharCount < 1)
  1718. return;
  1719. // choose a point to cull at
  1720. int cullPos = _maxCharCount / 2;
  1721. // kill half the buffer
  1722. m_TextStream.RemoveMultiple(0, cullPos);
  1723. // work out where in the format stream we can start
  1724. int formatIndex = FindFormatStreamIndexForTextStreamPos(cullPos);
  1725. if (formatIndex > 0)
  1726. {
  1727. // take a copy, make it first
  1728. m_FormatStream[0] = m_FormatStream[formatIndex];
  1729. m_FormatStream[0].textStreamIndex = 0;
  1730. // kill the others
  1731. m_FormatStream.RemoveMultiple(1, formatIndex);
  1732. }
  1733. // renormalize the remainder of the format stream
  1734. for (int i = 1; i < m_FormatStream.Count(); i++)
  1735. {
  1736. Assert(m_FormatStream[i].textStreamIndex > cullPos);
  1737. m_FormatStream[i].textStreamIndex -= cullPos;
  1738. }
  1739. // mark everything to be recalculated
  1740. InvalidateLineBreakStream();
  1741. InvalidateLayout();
  1742. _invalidateVerticalScrollbarSlider = true;
  1743. }
  1744. //-----------------------------------------------------------------------------
  1745. // Purpose: Insert a character into the text buffer
  1746. //-----------------------------------------------------------------------------
  1747. void RichText::InsertChar(wchar_t wch)
  1748. {
  1749. // throw away redundant linefeed characters
  1750. if ( wch == '\r' )
  1751. return;
  1752. if (_maxCharCount > 0 && m_TextStream.Count() > _maxCharCount)
  1753. {
  1754. TruncateTextStream();
  1755. }
  1756. // insert the new char at the end of the buffer
  1757. m_TextStream.AddToTail(wch);
  1758. // mark the linebreak steam as needing recalculating from that point
  1759. _recalculateBreaksIndex = m_LineBreaks.Count() - 2;
  1760. Repaint();
  1761. }
  1762. //-----------------------------------------------------------------------------
  1763. // Purpose: Insert a string into the text buffer, this is just a series
  1764. // of char inserts because we have to check each char is ok to insert
  1765. //-----------------------------------------------------------------------------
  1766. void RichText::InsertString(const char *text)
  1767. {
  1768. if (text[0] == '#')
  1769. {
  1770. wchar_t unicode[ 1024 ];
  1771. ResolveLocalizedTextAndVariables( text, unicode, sizeof( unicode ) );
  1772. InsertString( unicode );
  1773. return;
  1774. }
  1775. // upgrade the ansi text to unicode to display it
  1776. int len = strlen(text);
  1777. wchar_t *unicode = (wchar_t *)_alloca((len + 1) * sizeof(wchar_t));
  1778. Q_UTF8ToUnicode(text, unicode, ((len + 1) * sizeof(wchar_t)));
  1779. InsertString(unicode);
  1780. }
  1781. //-----------------------------------------------------------------------------
  1782. // Purpose: Insertsa a unicode string into the buffer
  1783. //-----------------------------------------------------------------------------
  1784. void RichText::InsertString(const wchar_t *wszText)
  1785. {
  1786. // insert the whole string
  1787. for (const wchar_t *ch = wszText; *ch != 0; ++ch)
  1788. {
  1789. InsertChar(*ch);
  1790. }
  1791. InvalidateLayout();
  1792. m_bRecalcLineBreaks = true;
  1793. Repaint();
  1794. }
  1795. //-----------------------------------------------------------------------------
  1796. // Purpose: Declare a selection empty
  1797. //-----------------------------------------------------------------------------
  1798. void RichText::SelectNone()
  1799. {
  1800. // tag the selection as empty
  1801. _select[0] = -1;
  1802. Repaint();
  1803. }
  1804. //-----------------------------------------------------------------------------
  1805. // Purpose: Load in the selection range so cx0 is the Start and cx1 is the end
  1806. // from smallest to highest (right to left)
  1807. //-----------------------------------------------------------------------------
  1808. bool RichText::GetSelectedRange(int &cx0, int &cx1)
  1809. {
  1810. // if there is nothing selected return false
  1811. if (_select[0] == -1)
  1812. return false;
  1813. // sort the two position so cx0 is the smallest
  1814. cx0 = _select[0];
  1815. cx1 = _select[1];
  1816. if (cx1 < cx0)
  1817. {
  1818. int temp = cx0;
  1819. cx0 = cx1;
  1820. cx1 = temp;
  1821. }
  1822. return true;
  1823. }
  1824. //-----------------------------------------------------------------------------
  1825. // Purpose: Opens the cut/copy/paste dropdown menu
  1826. //-----------------------------------------------------------------------------
  1827. void RichText::OpenEditMenu()
  1828. {
  1829. // get cursor position, this is local to this text edit window
  1830. // so we need to adjust it relative to the parent
  1831. int cursorX, cursorY;
  1832. input()->GetCursorPos(cursorX, cursorY);
  1833. /* !! disabled since it recursively gets panel pointers, potentially across dll boundaries,
  1834. and doesn't need to be necessary (it's just for handling windowed mode)
  1835. // find the frame that has no parent (the one on the desktop)
  1836. Panel *panel = this;
  1837. while ( panel->GetParent() != NULL)
  1838. {
  1839. panel = panel->GetParent();
  1840. }
  1841. panel->ScreenToLocal(cursorX, cursorY);
  1842. int x, y;
  1843. // get base panel's postition
  1844. panel->GetPos(x, y);
  1845. // adjust our cursor position accordingly
  1846. cursorX += x;
  1847. cursorY += y;
  1848. */
  1849. int x0, x1;
  1850. if (GetSelectedRange(x0, x1)) // there is something selected
  1851. {
  1852. m_pEditMenu->SetItemEnabled("&Cut", true);
  1853. m_pEditMenu->SetItemEnabled("C&opy", true);
  1854. }
  1855. else // there is nothing selected, disable cut/copy options
  1856. {
  1857. m_pEditMenu->SetItemEnabled("&Cut", false);
  1858. m_pEditMenu->SetItemEnabled("C&opy", false);
  1859. }
  1860. m_pEditMenu->SetVisible(true);
  1861. m_pEditMenu->RequestFocus();
  1862. // relayout the menu immediately so that we know it's size
  1863. m_pEditMenu->InvalidateLayout(true);
  1864. int menuWide, menuTall;
  1865. m_pEditMenu->GetSize(menuWide, menuTall);
  1866. // work out where the cursor is and therefore the best place to put the menu
  1867. int wide, tall;
  1868. surface()->GetScreenSize(wide, tall);
  1869. if (wide - menuWide > cursorX)
  1870. {
  1871. // menu hanging right
  1872. if (tall - menuTall > cursorY)
  1873. {
  1874. // menu hanging down
  1875. m_pEditMenu->SetPos(cursorX, cursorY);
  1876. }
  1877. else
  1878. {
  1879. // menu hanging up
  1880. m_pEditMenu->SetPos(cursorX, cursorY - menuTall);
  1881. }
  1882. }
  1883. else
  1884. {
  1885. // menu hanging left
  1886. if (tall - menuTall > cursorY)
  1887. {
  1888. // menu hanging down
  1889. m_pEditMenu->SetPos(cursorX - menuWide, cursorY);
  1890. }
  1891. else
  1892. {
  1893. // menu hanging up
  1894. m_pEditMenu->SetPos(cursorX - menuWide, cursorY - menuTall);
  1895. }
  1896. }
  1897. m_pEditMenu->RequestFocus();
  1898. }
  1899. //-----------------------------------------------------------------------------
  1900. // Purpose: Cuts the selected chars from the buffer and
  1901. // copies them into the clipboard
  1902. //-----------------------------------------------------------------------------
  1903. void RichText::CutSelected()
  1904. {
  1905. CopySelected();
  1906. // have to request focus if we used the menu
  1907. RequestFocus();
  1908. }
  1909. //-----------------------------------------------------------------------------
  1910. // Purpose: Copies the selected chars into the clipboard
  1911. //-----------------------------------------------------------------------------
  1912. void RichText::CopySelected()
  1913. {
  1914. int x0, x1;
  1915. if (GetSelectedRange(x0, x1))
  1916. {
  1917. CUtlVector<wchar_t> buf;
  1918. for (int i = x0; i < x1; i++)
  1919. {
  1920. if ( m_TextStream.IsValidIndex(i) == false )
  1921. continue;
  1922. if (m_TextStream[i] == '\n')
  1923. {
  1924. buf.AddToTail( '\r' );
  1925. }
  1926. // remove any rich edit commands
  1927. buf.AddToTail(m_TextStream[i]);
  1928. }
  1929. buf.AddToTail('\0');
  1930. system()->SetClipboardText(buf.Base(), buf.Count() - 1);
  1931. }
  1932. // have to request focus if we used the menu
  1933. RequestFocus();
  1934. }
  1935. //-----------------------------------------------------------------------------
  1936. // Purpose: Returns the index in the text buffer of the
  1937. // character the drawing should Start at
  1938. //-----------------------------------------------------------------------------
  1939. int RichText::GetStartDrawIndex(int &lineBreakIndexIndex)
  1940. {
  1941. int startIndex = 0;
  1942. int startLine = _vertScrollBar->GetValue();
  1943. if ( startLine >= m_LineBreaks.Count() ) // incase the line breaks got reset and the scroll bar hasn't
  1944. {
  1945. startLine = m_LineBreaks.Count() - 1;
  1946. }
  1947. lineBreakIndexIndex = startLine;
  1948. if (startLine && startLine < m_LineBreaks.Count())
  1949. {
  1950. startIndex = m_LineBreaks[startLine - 1];
  1951. }
  1952. return startIndex;
  1953. }
  1954. //-----------------------------------------------------------------------------
  1955. // Purpose: Get a string from text buffer
  1956. // Input: offset - index to Start reading from
  1957. // bufLen - length of string
  1958. //-----------------------------------------------------------------------------
  1959. void RichText::GetText(int offset, wchar_t *buf, int bufLenInBytes)
  1960. {
  1961. if (!buf)
  1962. return;
  1963. Assert( bufLenInBytes >= sizeof(buf[0]) );
  1964. int bufLen = bufLenInBytes / sizeof(wchar_t);
  1965. int i;
  1966. for (i = offset; i < (offset + bufLen - 1); i++)
  1967. {
  1968. if (i >= m_TextStream.Count())
  1969. break;
  1970. buf[i-offset] = m_TextStream[i];
  1971. }
  1972. buf[(i-offset)] = 0;
  1973. buf[bufLen-1] = 0;
  1974. }
  1975. //-----------------------------------------------------------------------------
  1976. // Purpose: gets text from the buffer
  1977. //-----------------------------------------------------------------------------
  1978. void RichText::GetText(int offset, char *pch, int bufLenInBytes)
  1979. {
  1980. wchar_t rgwchT[4096];
  1981. GetText(offset, rgwchT, sizeof(rgwchT));
  1982. Q_UnicodeToUTF8(rgwchT, pch, bufLenInBytes);
  1983. }
  1984. //-----------------------------------------------------------------------------
  1985. // Purpose: Set the font of the buffer text
  1986. //-----------------------------------------------------------------------------
  1987. void RichText::SetFont(HFont font)
  1988. {
  1989. _font = font;
  1990. InvalidateLayout();
  1991. m_bRecalcLineBreaks = true;
  1992. Repaint();
  1993. }
  1994. //-----------------------------------------------------------------------------
  1995. // Purpose: Called when the scrollbar slider is moved
  1996. //-----------------------------------------------------------------------------
  1997. void RichText::OnSliderMoved()
  1998. {
  1999. _recalcSavedRenderState = true;
  2000. Repaint();
  2001. }
  2002. //-----------------------------------------------------------------------------
  2003. // Purpose:
  2004. //-----------------------------------------------------------------------------
  2005. bool RichText::RequestInfo(KeyValues *outputData)
  2006. {
  2007. if (!stricmp(outputData->GetName(), "GetText"))
  2008. {
  2009. wchar_t wbuf[512];
  2010. GetText(0, wbuf, sizeof(wbuf));
  2011. outputData->SetWString("text", wbuf);
  2012. return true;
  2013. }
  2014. return BaseClass::RequestInfo(outputData);
  2015. }
  2016. //-----------------------------------------------------------------------------
  2017. // Purpose:
  2018. //-----------------------------------------------------------------------------
  2019. void RichText::OnSetText(const wchar_t *text)
  2020. {
  2021. SetText(text);
  2022. }
  2023. //-----------------------------------------------------------------------------
  2024. // Purpose: Called when a URL, etc has been clicked on
  2025. //-----------------------------------------------------------------------------
  2026. void RichText::OnClickPanel(int index)
  2027. {
  2028. wchar_t wBuf[512];
  2029. int outIndex = 0;
  2030. // parse out the clickable text, and send it to our listeners
  2031. _currentTextClickable = true;
  2032. TRenderState renderState;
  2033. GenerateRenderStateForTextStreamIndex(index, renderState);
  2034. for (int i = index; i < (sizeof(wBuf) - 1) && i < m_TextStream.Count(); i++)
  2035. {
  2036. // stop getting characters when text is no longer clickable
  2037. UpdateRenderState(i, renderState);
  2038. if (!renderState.textClickable)
  2039. break;
  2040. // copy out the character
  2041. wBuf[outIndex++] = m_TextStream[i];
  2042. }
  2043. wBuf[outIndex] = 0;
  2044. int iFormatSteam = FindFormatStreamIndexForTextStreamPos( index );
  2045. if ( m_FormatStream[iFormatSteam].m_sClickableTextAction )
  2046. {
  2047. Q_UTF8ToUnicode( m_FormatStream[iFormatSteam].m_sClickableTextAction.String(), wBuf, sizeof( wBuf ) );
  2048. }
  2049. PostActionSignal(new KeyValues("TextClicked", "text", wBuf));
  2050. OnTextClicked(wBuf);
  2051. }
  2052. //-----------------------------------------------------------------------------
  2053. // Purpose:
  2054. //-----------------------------------------------------------------------------
  2055. void RichText::ApplySettings(KeyValues *inResourceData)
  2056. {
  2057. BaseClass::ApplySettings(inResourceData);
  2058. SetMaximumCharCount(inResourceData->GetInt("maxchars", -1));
  2059. SetVerticalScrollbar(inResourceData->GetInt("scrollbar", 1));
  2060. // get the starting text, if any
  2061. const char *text = inResourceData->GetString("text", "");
  2062. if (*text)
  2063. {
  2064. delete [] m_pszInitialText;
  2065. int len = Q_strlen(text) + 1;
  2066. m_pszInitialText = new char[ len ];
  2067. Q_strncpy( m_pszInitialText, text, len );
  2068. SetText(text);
  2069. }
  2070. else
  2071. {
  2072. const char *textfilename = inResourceData->GetString("textfile", NULL);
  2073. if ( textfilename )
  2074. {
  2075. FileHandle_t f = g_pFullFileSystem->Open( textfilename, "rt" );
  2076. if (!f)
  2077. {
  2078. Warning( "RichText: textfile parameter '%s' not found.\n", textfilename );
  2079. return;
  2080. }
  2081. int len = g_pFullFileSystem->Size( f );
  2082. delete [] m_pszInitialText;
  2083. m_pszInitialText = new char[ len + 1 ];
  2084. g_pFullFileSystem->Read( m_pszInitialText, len, f );
  2085. m_pszInitialText[len - 1] = 0;
  2086. SetText( m_pszInitialText );
  2087. g_pFullFileSystem->Close( f );
  2088. }
  2089. }
  2090. }
  2091. //-----------------------------------------------------------------------------
  2092. // Purpose:
  2093. //-----------------------------------------------------------------------------
  2094. void RichText::GetSettings(KeyValues *outResourceData)
  2095. {
  2096. BaseClass::GetSettings(outResourceData);
  2097. outResourceData->SetInt("maxchars", _maxCharCount);
  2098. outResourceData->SetInt("scrollbar", _vertScrollBar->IsVisible() );
  2099. if (m_pszInitialText)
  2100. {
  2101. outResourceData->SetString("text", m_pszInitialText);
  2102. }
  2103. }
  2104. //-----------------------------------------------------------------------------
  2105. // Purpose:
  2106. //-----------------------------------------------------------------------------
  2107. const char *RichText::GetDescription()
  2108. {
  2109. static char buf[1024];
  2110. Q_snprintf(buf, sizeof(buf), "%s, string text, bool scrollbar", BaseClass::GetDescription());
  2111. return buf;
  2112. }
  2113. //-----------------------------------------------------------------------------
  2114. // Purpose: Get the number of lines in the window
  2115. //-----------------------------------------------------------------------------
  2116. int RichText::GetNumLines()
  2117. {
  2118. return m_LineBreaks.Count();
  2119. }
  2120. //-----------------------------------------------------------------------------
  2121. // Purpose: Sets the height of the text entry window so all text will fit inside
  2122. //-----------------------------------------------------------------------------
  2123. void RichText::SetToFullHeight()
  2124. {
  2125. PerformLayout();
  2126. int wide, tall;
  2127. GetSize(wide, tall);
  2128. tall = GetNumLines() * (GetLineHeight() + _drawOffsetY) + _drawOffsetY + 2;
  2129. SetSize (wide, tall);
  2130. PerformLayout();
  2131. }
  2132. //-----------------------------------------------------------------------------
  2133. // Purpose: Select all the text.
  2134. //-----------------------------------------------------------------------------
  2135. void RichText::SelectAllText()
  2136. {
  2137. _cursorPos = 0;
  2138. _select[0] = 0;
  2139. _select[1] = m_TextStream.Count();
  2140. }
  2141. //-----------------------------------------------------------------------------
  2142. // Purpose: Select all the text.
  2143. //-----------------------------------------------------------------------------
  2144. void RichText::SelectNoText()
  2145. {
  2146. _select[0] = 0;
  2147. _select[1] = 0;
  2148. }
  2149. //-----------------------------------------------------------------------------
  2150. // Purpose:
  2151. //-----------------------------------------------------------------------------
  2152. void RichText::OnSetFocus()
  2153. {
  2154. BaseClass::OnSetFocus();
  2155. }
  2156. //-----------------------------------------------------------------------------
  2157. // Purpose: Invalidates the current linebreak stream
  2158. //-----------------------------------------------------------------------------
  2159. void RichText::InvalidateLineBreakStream()
  2160. {
  2161. // clear the buffer
  2162. m_LineBreaks.RemoveAll();
  2163. m_LineBreaks.AddToTail(MAX_BUFFER_SIZE);
  2164. _recalculateBreaksIndex = 0;
  2165. m_bRecalcLineBreaks = true;
  2166. }
  2167. //-----------------------------------------------------------------------------
  2168. // Purpose: Inserts a text string while making URLs clickable/different color
  2169. // Input : *text - string that may contain URLs to make clickable/color coded
  2170. // URLTextColor - color for URL text
  2171. // normalTextColor - color for normal text
  2172. //-----------------------------------------------------------------------------
  2173. void RichText::InsertPossibleURLString(const char* text, Color URLTextColor, Color normalTextColor)
  2174. {
  2175. InsertColorChange(normalTextColor);
  2176. // parse out the string for URL's
  2177. int len = Q_strlen(text), pos = 0;
  2178. bool clickable = false;
  2179. char *pchURLText = (char *)stackalloc( len + 1 );
  2180. char *pchURL = (char *)stackalloc( len + 1 );
  2181. while (pos < len)
  2182. {
  2183. pos = ParseTextStringForUrls( text, pos, pchURLText, len, pchURL, len, clickable );
  2184. if ( clickable )
  2185. {
  2186. InsertClickableTextStart( pchURL );
  2187. InsertColorChange( URLTextColor );
  2188. }
  2189. InsertString( pchURLText );
  2190. if ( clickable )
  2191. {
  2192. InsertColorChange(normalTextColor);
  2193. InsertClickableTextEnd();
  2194. }
  2195. }
  2196. }
  2197. //-----------------------------------------------------------------------------
  2198. // Purpose: looks for URLs in the string and returns information about the URL
  2199. //-----------------------------------------------------------------------------
  2200. int RichText::ParseTextStringForUrls( const char *text, int startPos, char *pchURLText, int cchURLText, char *pchURL, int cchURL, bool &clickable )
  2201. {
  2202. // scan for text that looks like a URL
  2203. int i = startPos;
  2204. while (text[i] != 0)
  2205. {
  2206. bool bURLFound = false;
  2207. if ( !Q_strnicmp(text + i, "<a href=", 8) )
  2208. {
  2209. if (i > startPos)
  2210. break;
  2211. // embedded link
  2212. bURLFound = true;
  2213. clickable = true;
  2214. // get the url
  2215. i += Q_strlen( "<a href=" );
  2216. const char *pchURLEnd = Q_strstr( text + i, ">" );
  2217. Q_strncpy( pchURL, text + i, min( pchURLEnd - text - i + 1, cchURL ) );
  2218. i += ( pchURLEnd - text - i + 1 );
  2219. // get the url text
  2220. pchURLEnd = Q_strstr( text, "</a>" );
  2221. Q_strncpy( pchURLText, text + i, min( pchURLEnd - text - i + 1, cchURLText ) );
  2222. i += ( pchURLEnd - text - i );
  2223. i += Q_strlen( "</a>" );
  2224. // we're done
  2225. return i;
  2226. }
  2227. else if (!Q_strnicmp(text + i, "www.", 4))
  2228. {
  2229. // scan ahead for another '.'
  2230. bool bPeriodFound = false;
  2231. for (const char *ch = text + i + 5; ch != 0; ch++)
  2232. {
  2233. if (*ch == '.')
  2234. {
  2235. bPeriodFound = true;
  2236. break;
  2237. }
  2238. }
  2239. // URL found
  2240. if (bPeriodFound)
  2241. {
  2242. bURLFound = true;
  2243. }
  2244. }
  2245. else if (!Q_strnicmp(text + i, "http://", 7))
  2246. {
  2247. bURLFound = true;
  2248. }
  2249. else if (!Q_strnicmp(text + i, "ftp://", 6))
  2250. {
  2251. bURLFound = true;
  2252. }
  2253. else if (!Q_strnicmp(text + i, "steam://", 8))
  2254. {
  2255. bURLFound = true;
  2256. }
  2257. else if (!Q_strnicmp(text + i, "steambeta://", 12))
  2258. {
  2259. bURLFound = true;
  2260. }
  2261. else if (!Q_strnicmp(text + i, "mailto:", 7))
  2262. {
  2263. bURLFound = true;
  2264. }
  2265. else if (!Q_strnicmp(text + i, "\\\\", 2))
  2266. {
  2267. bURLFound = true;
  2268. }
  2269. if (bURLFound)
  2270. {
  2271. if (i == startPos)
  2272. {
  2273. // we're at the Start of a URL, so parse that out
  2274. clickable = true;
  2275. int outIndex = 0;
  2276. while (text[i] != 0 && !iswspace(text[i]))
  2277. {
  2278. pchURLText[outIndex++] = text[i++];
  2279. }
  2280. pchURLText[outIndex] = 0;
  2281. Q_strncpy( pchURL, pchURLText, cchURL );
  2282. return i;
  2283. }
  2284. else
  2285. {
  2286. // no url
  2287. break;
  2288. }
  2289. }
  2290. // increment and loop
  2291. i++;
  2292. }
  2293. // nothing found;
  2294. // parse out the text before the end
  2295. clickable = false;
  2296. int outIndex = 0;
  2297. int fromIndex = startPos;
  2298. while ( fromIndex < i && outIndex < cchURLText )
  2299. {
  2300. pchURLText[outIndex++] = text[fromIndex++];
  2301. }
  2302. pchURLText[outIndex] = 0;
  2303. Q_strncpy( pchURL, pchURLText, cchURL );
  2304. return i;
  2305. }
  2306. //-----------------------------------------------------------------------------
  2307. // Purpose: Executes the text-clicked command, which opens a web browser by
  2308. // default.
  2309. //-----------------------------------------------------------------------------
  2310. void RichText::OnTextClicked(const wchar_t *wszText)
  2311. {
  2312. // Strip leading/trailing quotes, which may be present on href tags or may not.
  2313. const wchar_t *pwchURL = wszText;
  2314. if ( pwchURL[0] == L'"' || pwchURL[0] == L'\'' )
  2315. pwchURL = wszText + 1;
  2316. char ansi[2048];
  2317. Q_UnicodeToUTF8( pwchURL, ansi, sizeof(ansi) );
  2318. size_t strLen = Q_strlen(ansi);
  2319. if ( strLen && ( ansi[strLen-1] == '"' || ansi[strLen] == '\'' ) )
  2320. {
  2321. ansi[strLen-1] = 0;
  2322. }
  2323. if ( m_hPanelToHandleClickingURLs.Get() )
  2324. {
  2325. PostMessage( m_hPanelToHandleClickingURLs.Get(), new KeyValues( "URLClicked", "url", ansi ) );
  2326. }
  2327. else
  2328. {
  2329. system()->ShellExecute( "open", ansi );
  2330. }
  2331. }
  2332. //-----------------------------------------------------------------------------
  2333. // Purpose:
  2334. //-----------------------------------------------------------------------------
  2335. void RichText::SetURLClickedHandler( Panel *pPanelToHandleClickMsg )
  2336. {
  2337. m_hPanelToHandleClickingURLs = pPanelToHandleClickMsg;
  2338. }
  2339. //-----------------------------------------------------------------------------
  2340. // Purpose: data accessor
  2341. //-----------------------------------------------------------------------------
  2342. bool RichText::IsScrollbarVisible()
  2343. {
  2344. return _vertScrollBar->IsVisible();
  2345. }
  2346. void RichText::SetUnderlineFont( HFont font )
  2347. {
  2348. m_hFontUnderline = font;
  2349. }
  2350. bool RichText::IsAllTextAlphaZero() const
  2351. {
  2352. return m_bAllTextAlphaIsZero;
  2353. }
  2354. bool RichText::HasText() const
  2355. {
  2356. int c = m_TextStream.Count();
  2357. if ( c == 0 )
  2358. {
  2359. return false;
  2360. }
  2361. return true;
  2362. }
  2363. //-----------------------------------------------------------------------------
  2364. // Purpose: Returns the height of the base font
  2365. //-----------------------------------------------------------------------------
  2366. int RichText::GetLineHeight()
  2367. {
  2368. return surface()->GetFontTall( _font );
  2369. }
  2370. #ifdef DBGFLAG_VALIDATE
  2371. //-----------------------------------------------------------------------------
  2372. // Purpose: Run a global validation pass on all of our data structures and memory
  2373. // allocations.
  2374. // Input: validator - Our global validator object
  2375. // pchName - Our name (typically a member var in our container)
  2376. //-----------------------------------------------------------------------------
  2377. void RichText::Validate( CValidator &validator, char *pchName )
  2378. {
  2379. validator.Push( "vgui::RichText", this, pchName );
  2380. ValidateObj( m_TextStream );
  2381. ValidateObj( m_FormatStream );
  2382. ValidateObj( m_LineBreaks );
  2383. ValidateObj( _clickableTextPanels );
  2384. validator.ClaimMemory( m_pszInitialText );
  2385. BaseClass::Validate( validator, "vgui::RichText" );
  2386. validator.Pop();
  2387. }
  2388. #endif // DBGFLAG_VALIDATE