multilinetextctrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : wwui *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/multilinetextctrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/09/02 5:57p $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "multilinetextctrl.h"
  36. #include "assetmgr.h"
  37. #include "stylemgr.h"
  38. #include "dialogbase.h"
  39. #include <winuser.h>
  40. //////////////////////////////////////////////////////////////////////
  41. //
  42. // MultiLineTextCtrlClass
  43. //
  44. //////////////////////////////////////////////////////////////////////
  45. MultiLineTextCtrlClass::MultiLineTextCtrlClass (void) :
  46. ScrollPos (0),
  47. IsScrollBarDisplayed (false),
  48. IsInitialized (false),
  49. RowCount (0),
  50. RowsPerPage (0)
  51. {
  52. UINT scrolllines;
  53. //
  54. // Configure the renderers
  55. //
  56. StyleMgrClass::Assign_Font (&TextRenderer, StyleMgrClass::FONT_CONTROLS);
  57. StyleMgrClass::Configure_Renderer (&ControlRenderer);
  58. TextRenderer.Set_Texture_Size_Hint (256);
  59. //
  60. // We don't want the scroll bar getting focus
  61. //
  62. ScrollBarCtrl.Set_Wants_Focus (false);
  63. ScrollBarCtrl.Set_Advise_Sink (this);
  64. ScrollBarCtrl.Set_Is_Embedded (true);
  65. // Calculate the no. of lines to scroll for each mouse wheel increment.
  66. SystemParametersInfo (SPI_GETWHEELSCROLLLINES, 0, &scrolllines, 0);
  67. MouseWheelIncrement = ((float) scrolllines) / ((float) WHEEL_DELTA);
  68. return ;
  69. }
  70. //////////////////////////////////////////////////////////////////////
  71. //
  72. // ~MultiLineTextCtrlClass
  73. //
  74. //////////////////////////////////////////////////////////////////////
  75. MultiLineTextCtrlClass::~MultiLineTextCtrlClass (void)
  76. {
  77. ScrollBarCtrl.Set_Advise_Sink (NULL);
  78. if (Parent != NULL) {
  79. Parent->Remove_Control (&ScrollBarCtrl);
  80. }
  81. return ;
  82. }
  83. //////////////////////////////////////////////////////////////////////
  84. //
  85. // Create_Control_Renderer
  86. //
  87. //////////////////////////////////////////////////////////////////////
  88. void
  89. MultiLineTextCtrlClass::Create_Control_Renderer (void)
  90. {
  91. Render2DClass &renderer = ControlRenderer;
  92. //
  93. // Configure this renderer
  94. //
  95. renderer.Reset ();
  96. renderer.Enable_Texturing (false);
  97. //
  98. // Determine which color to draw the outline in
  99. //
  100. int color = StyleMgrClass::Get_Line_Color ();
  101. int bkcolor = StyleMgrClass::Get_Bk_Color ();
  102. if (IsEnabled == false) {
  103. color = StyleMgrClass::Get_Disabled_Line_Color ();
  104. bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
  105. }
  106. //
  107. // Draw the control's outline
  108. //
  109. renderer.Add_Outline (Rect, 1.0F, color);
  110. return ;
  111. }
  112. //////////////////////////////////////////////////////////////////////
  113. //
  114. // Create_Text_Renderer
  115. //
  116. //////////////////////////////////////////////////////////////////////
  117. void
  118. MultiLineTextCtrlClass::Create_Text_Renderer (void)
  119. {
  120. TextRenderer.Reset ();
  121. //
  122. // Handy macro
  123. //
  124. #define COPY_LINE(dest, src_start, src_end) \
  125. if (src_end == NULL) { \
  126. dest = src_start; \
  127. } else { \
  128. uint32 bytes = ((uint32)src_end - (uint32)src_start); \
  129. uint32 len = bytes / sizeof (WCHAR); \
  130. ::memcpy (dest.Get_Buffer (len + 1), src_start, bytes); \
  131. dest.Peek_Buffer ()[len] = 0; \
  132. }
  133. //
  134. // Determine where to start drawing the text from
  135. //
  136. const WCHAR *text_start = TextRenderer.Find_Row_Start (Title, ScrollPos);
  137. const WCHAR *text_end = TextRenderer.Find_Row_Start (Title, ScrollPos+RowsPerPage);
  138. if (text_start != NULL) {
  139. //
  140. // Should we draw the text centered?
  141. //
  142. if ((Style & ES_CENTER) == ES_CENTER) {
  143. float char_height = TextRenderer.Peek_Font ()->Get_Char_Height ();
  144. RectClass text_rect = ClientRect;
  145. //
  146. // Render each line separately
  147. //
  148. const WCHAR *line_start = text_start;
  149. const WCHAR *line_end = TextRenderer.Find_Row_Start (Title, ScrollPos + 1);
  150. for (int index = 0; index < RowsPerPage; index ++) {
  151. //
  152. // Copy this line of text into a buffer
  153. //
  154. WideStringClass text;
  155. COPY_LINE (text, line_start, line_end);
  156. //
  157. // Render this line of text centered
  158. //
  159. TextRenderer.Set_Wrapping_Width (0);
  160. StyleMgrClass::Render_Text (text, &TextRenderer, text_rect, true, true, StyleMgrClass::CENTER_JUSTIFY, IsEnabled, false);
  161. text_rect.Top = int (text_rect.Top + char_height);
  162. TextRenderer.Set_Wrapping_Width (ClientRect.Width ());
  163. if (line_end == NULL) {
  164. break;
  165. }
  166. //
  167. // Advance to the next line...
  168. //
  169. line_start = line_end;
  170. line_end = TextRenderer.Find_Row_Start (line_start, 1);
  171. }
  172. } else {
  173. WideStringClass text;
  174. COPY_LINE (text, text_start, text_end);
  175. //
  176. // Draw the text
  177. //
  178. StyleMgrClass::Render_Wrapped_Text (text, &TextRenderer, ClientRect, true, false, IsEnabled);
  179. }
  180. }
  181. return ;
  182. }
  183. ////////////////////////////////////////////////////////////////
  184. //
  185. // Render
  186. //
  187. ////////////////////////////////////////////////////////////////
  188. void
  189. MultiLineTextCtrlClass::Render (void)
  190. {
  191. //
  192. // Recreate the renderers (if necessary)
  193. //
  194. if (IsDirty) {
  195. Update_Scroll_Bar_Visibility ();
  196. Create_Control_Renderer ();
  197. Create_Text_Renderer ();
  198. }
  199. //
  200. // Render the image...
  201. //
  202. ControlRenderer.Render ();
  203. TextRenderer.Render ();
  204. DialogControlClass::Render ();
  205. return ;
  206. }
  207. ////////////////////////////////////////////////////////////////
  208. //
  209. // Update_Client_Rect
  210. //
  211. ////////////////////////////////////////////////////////////////
  212. void
  213. MultiLineTextCtrlClass::Update_Client_Rect (void)
  214. {
  215. //
  216. // Set the client area
  217. //
  218. ClientRect = Rect;
  219. ClientRect.Inflate (Vector2 (-5.0F * StyleMgrClass::Get_X_Scale (), -3.75F * StyleMgrClass::Get_Y_Scale ()));
  220. ClientRect.Left = int(ClientRect.Left);
  221. ClientRect.Top = int(ClientRect.Top);
  222. ClientRect.Right = int(ClientRect.Right);
  223. ClientRect.Bottom = int(ClientRect.Bottom);
  224. //
  225. // Determine how many rows we can fit on one page
  226. //
  227. float char_height = TextRenderer.Peek_Font ()->Get_Char_Height ();
  228. RowsPerPage = ClientRect.Height () / char_height;
  229. IsInitialized = true;
  230. //
  231. // Choose an arbitrary width, the scroll bar
  232. // will snap to the only width it supports
  233. //
  234. float width = 10;
  235. //
  236. // Calculate the scroll bar's rectangle
  237. //
  238. RectClass scroll_rect;
  239. scroll_rect.Left = Rect.Right - width;
  240. scroll_rect.Top = Rect.Top;
  241. scroll_rect.Right = Rect.Right;
  242. scroll_rect.Bottom = Rect.Bottom;
  243. //
  244. // Size the scroll bar
  245. //
  246. ScrollBarCtrl.Set_Window_Rect (scroll_rect);
  247. ScrollBarCtrl.Set_Small_BMP_Mode ((Rect.Height () < (StyleMgrClass::Get_Y_Scale () * 100.0F)));
  248. //Update_Scroll_Bar_Visibility ();
  249. Set_Dirty ();
  250. return ;
  251. }
  252. ////////////////////////////////////////////////////////////////
  253. //
  254. // Update_Scroll_Bar_Visibility
  255. //
  256. ////////////////////////////////////////////////////////////////
  257. void
  258. MultiLineTextCtrlClass::Update_Scroll_Bar_Visibility (void)
  259. {
  260. if (IsInitialized == false ) {
  261. return ;
  262. }
  263. Calculate_Row_Count ();
  264. //
  265. // Determine if we have more entries then we can
  266. // display on one page
  267. //
  268. bool needs_scrollbar = (RowCount > RowsPerPage);
  269. float new_right = 0;
  270. if (needs_scrollbar != IsScrollBarDisplayed) {
  271. //
  272. // Do we need to show a scroll bar?
  273. //
  274. if (needs_scrollbar) {
  275. new_right = ScrollBarCtrl.Get_Window_Rect ().Left;
  276. IsScrollBarDisplayed = true;
  277. } else if (Parent != NULL) {
  278. new_right = ScrollBarCtrl.Get_Window_Rect ().Right;
  279. IsScrollBarDisplayed = false;
  280. }
  281. //
  282. // Reset our window size (as necessary)
  283. //
  284. Rect.Right = new_right;
  285. ClientRect = Rect;
  286. ClientRect.Inflate (Vector2 (-5.0F * StyleMgrClass::Get_X_Scale (), -3.75F * StyleMgrClass::Get_Y_Scale ()));
  287. ClientRect.Left = int(ClientRect.Left);
  288. ClientRect.Top = int(ClientRect.Top);
  289. ClientRect.Right = int(ClientRect.Right);
  290. ClientRect.Bottom = int(ClientRect.Bottom);
  291. Calculate_Row_Count ();
  292. //
  293. // Add or remove the scrollbar (as necessary)
  294. //
  295. if (IsScrollBarDisplayed) {
  296. Parent->Add_Control (&ScrollBarCtrl);
  297. } else {
  298. Parent->Remove_Control (&ScrollBarCtrl);
  299. }
  300. }
  301. //
  302. // Configure the scroll-bar's ranges
  303. //
  304. ScrollBarCtrl.Set_Page_Size (RowsPerPage);
  305. ScrollBarCtrl.Set_Range (0, RowCount - RowsPerPage);
  306. ScrollBarCtrl.Set_Pos (ScrollPos);
  307. ScrollPos = ScrollBarCtrl.Get_Pos ();
  308. return ;
  309. }
  310. ////////////////////////////////////////////////////////////////
  311. //
  312. // Calculate_Row_Count
  313. //
  314. ////////////////////////////////////////////////////////////////
  315. void
  316. MultiLineTextCtrlClass::Calculate_Row_Count (void)
  317. {
  318. TextRenderer.Set_Wrapping_Width (ClientRect.Width ());
  319. //
  320. // Get the number of rows we'll need to display
  321. //
  322. RowCount = 0;
  323. TextRenderer.Get_Formatted_Text_Extents (Title, &RowCount);
  324. return ;
  325. }
  326. ////////////////////////////////////////////////////////////////
  327. //
  328. // On_Key_Down
  329. //
  330. ////////////////////////////////////////////////////////////////
  331. bool
  332. MultiLineTextCtrlClass::On_Key_Down (uint32 key_id, uint32 key_data)
  333. {
  334. switch (key_id)
  335. {
  336. case VK_UP:
  337. Set_Scroll_Pos (ScrollPos - 1);
  338. break;
  339. case VK_DOWN:
  340. Set_Scroll_Pos (ScrollPos + 1);
  341. break;
  342. case VK_PRIOR:
  343. Set_Scroll_Pos (ScrollPos - RowsPerPage);
  344. break;
  345. case VK_NEXT:
  346. Set_Scroll_Pos (ScrollPos + RowsPerPage);
  347. break;
  348. case VK_HOME:
  349. Set_Scroll_Pos (0);
  350. break;
  351. case VK_END:
  352. Set_Scroll_Pos (RowCount);
  353. break;
  354. }
  355. return true;
  356. }
  357. ////////////////////////////////////////////////////////////////
  358. //
  359. // On_VScroll
  360. //
  361. ////////////////////////////////////////////////////////////////
  362. void
  363. MultiLineTextCtrlClass::On_VScroll (ScrollBarCtrlClass *, int , int new_position)
  364. {
  365. Set_Scroll_Pos (new_position);
  366. return ;
  367. }
  368. ////////////////////////////////////////////////////////////////
  369. //
  370. // On_Mouse_Wheel
  371. //
  372. ////////////////////////////////////////////////////////////////
  373. void
  374. MultiLineTextCtrlClass::On_Mouse_Wheel (int direction)
  375. {
  376. int lineoffset = direction * MouseWheelIncrement;
  377. Set_Scroll_Pos (ScrollPos + lineoffset);
  378. return ;
  379. }
  380. ////////////////////////////////////////////////////////////////
  381. //
  382. // Set_Scroll_Pos
  383. //
  384. ////////////////////////////////////////////////////////////////
  385. void
  386. MultiLineTextCtrlClass::Set_Scroll_Pos (int new_position)
  387. {
  388. if (ScrollPos != new_position) {
  389. ScrollPos = new_position;
  390. ScrollPos = min (ScrollPos, RowCount - RowsPerPage);
  391. ScrollPos = max (ScrollPos, 0);
  392. ScrollBarCtrl.Set_Pos (ScrollPos, false);
  393. Set_Dirty ();
  394. }
  395. return ;
  396. }
  397. ////////////////////////////////////////////////////////////////
  398. //
  399. // Set_Text
  400. //
  401. ////////////////////////////////////////////////////////////////
  402. void
  403. MultiLineTextCtrlClass::Set_Text (const WCHAR *title)
  404. {
  405. DialogControlClass::Set_Text (title);
  406. ScrollPos = 0;
  407. RowCount = 0;
  408. //Update_Scroll_Bar_Visibility ();
  409. return ;
  410. }