dialogparser.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 : Combat *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/dialogparser.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 10/25/01 3:54p $*
  29. * *
  30. * $Revision:: 12 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "dialogparser.h"
  36. #include "win.h"
  37. #include "translatedb.h"
  38. #include <commctrl.h>
  39. //////////////////////////////////////////////////////////////////////////////
  40. // Macros
  41. //////////////////////////////////////////////////////////////////////////////
  42. #define ALIGN_WORD_PTR(p) ((WORD *)(((DWORD)p + 1) & ~1))
  43. #define ALIGN_DWORD_PTR(p) ((DWORD *)(((DWORD)p + 3) & ~3))
  44. //////////////////////////////////////////////////////////////////////////////
  45. // Local prototypes
  46. //////////////////////////////////////////////////////////////////////////////
  47. WORD *Skip_Dlg_Field (WORD *src, WCHAR *buffer = NULL, int buffer_len = 0, WORD *ctrl_type = NULL);
  48. //////////////////////////////////////////////////////////////////////////////
  49. //
  50. // Skip_Dlg_Field
  51. //
  52. //////////////////////////////////////////////////////////////////////////////
  53. WORD *
  54. Skip_Dlg_Field (WORD *src, WCHAR *buffer, int buffer_len, WORD *ctrl_type)
  55. {
  56. //
  57. // These fields always start on the next word boundary, so align
  58. // the source pointer on this boundary.
  59. //
  60. WORD *retval = ALIGN_WORD_PTR(src);
  61. //
  62. // Note: The field codes are as follows:
  63. //
  64. // 0xFFFF - The following WORD is an ordinal value of a system class.
  65. // 0x0000 - Empty field
  66. // Otherwise - The remaining data is a NULL terminated WCHAR string.
  67. //
  68. if (*retval == 0xFFFF) {
  69. //
  70. // Move past the field designator
  71. //
  72. retval ++;
  73. //
  74. // Does the user want information about the ctrl type?
  75. //
  76. if (ctrl_type != NULL) {
  77. *ctrl_type = *retval;
  78. }
  79. //
  80. // Move past the ctrl type identifier
  81. //
  82. retval ++;
  83. } else if (*retval == 0x0000) {
  84. //
  85. // Null terminate the string if the user is expecting data
  86. //
  87. if (buffer != NULL) {
  88. *buffer = 0;
  89. }
  90. //
  91. // Move past the field designator
  92. //
  93. retval ++;
  94. } else {
  95. //
  96. // The following data is a null-terminated string. Scan
  97. // as much data into our desination buffer as possible.
  98. // Note: The data is stored in wide character format.
  99. //
  100. while (*retval != 0x0000) {
  101. if (buffer != NULL && buffer_len > 1) {
  102. //
  103. // Store this character in the supplied buffer
  104. // and decrement the remaining buffer length.
  105. //
  106. *buffer++ = *retval;
  107. buffer_len --;
  108. }
  109. retval ++;
  110. }
  111. //
  112. // Ensure the supplied buffer is NULL terminated
  113. //
  114. if (buffer != NULL) {
  115. *buffer = 0;
  116. }
  117. //
  118. // Advance to the next field
  119. //
  120. retval ++;
  121. }
  122. //
  123. // Return the new buffer position to the caller
  124. //
  125. return retval;
  126. }
  127. //////////////////////////////////////////////////////////////////////////////
  128. //
  129. // Parse_Template
  130. //
  131. //////////////////////////////////////////////////////////////////////////////
  132. void
  133. DialogParserClass::Parse_Template
  134. (
  135. int res_id,
  136. int * dlg_width,
  137. int * dlg_height,
  138. WideStringClass * dlg_title,
  139. DynamicVectorClass<ControlDefinitionStruct> * control_list
  140. )
  141. {
  142. //
  143. // Load the resource file
  144. //
  145. HRSRC resource = ::FindResource (ProgramInstance, MAKEINTRESOURCE (res_id), RT_DIALOG);
  146. HGLOBAL hglobal = ::LoadResource (ProgramInstance, resource);
  147. LPVOID res_buffer = ::LockResource (hglobal);
  148. if(res_buffer != NULL) {
  149. //
  150. // The first few bytes of the resource buffer are the DLGTEMPLATE structure
  151. //
  152. DLGTEMPLATE *dlg_template = (DLGTEMPLATE *)res_buffer;
  153. (*dlg_width) = (int)dlg_template->cx;
  154. (*dlg_height) = (int)dlg_template->cy;
  155. //
  156. // Move past the DLGTEMPLATE header to the other fields
  157. //
  158. WORD *buffer = (WORD *)(((char *)res_buffer) + sizeof (DLGTEMPLATE));
  159. //
  160. // Skip the menu, and window class
  161. //
  162. buffer = Skip_Dlg_Field (buffer);
  163. buffer = Skip_Dlg_Field (buffer);
  164. //
  165. // Read the title
  166. //
  167. buffer = Skip_Dlg_Field (buffer, dlg_title->Get_Buffer (96), 96);
  168. WCHAR *string_id = ::wcsstr (dlg_title->Peek_Buffer (), L"IDS_");
  169. if (string_id != NULL) {
  170. WideStringClass wide_string_id = string_id;
  171. StringClass ascii_string_id;
  172. wide_string_id.Convert_To (ascii_string_id);
  173. (*dlg_title) = TRANSLATE_BY_DESC(ascii_string_id);
  174. }
  175. //
  176. // Do we need to skip past the font settings?
  177. //
  178. if (dlg_template->style & DS_SETFONT) {
  179. buffer ++;
  180. while (*buffer != 0x0000) {
  181. buffer ++;
  182. }
  183. buffer ++;
  184. }
  185. //
  186. // Loop over each control and gather information about them
  187. //
  188. for (int index = 0; index < dlg_template->cdit; index ++) {
  189. DLGITEMTEMPLATE *dlg_item_template = (DLGITEMTEMPLATE *)ALIGN_DWORD_PTR((DWORD *)buffer);
  190. buffer = (WORD *)(((char *)dlg_item_template) + sizeof (DLGITEMTEMPLATE));
  191. //
  192. // Read the ctrl type
  193. //
  194. WCHAR text_buffer[256] = { 0 };
  195. WORD ctrl_type = 0x0000;
  196. buffer = Skip_Dlg_Field (buffer, text_buffer, 256, &ctrl_type);
  197. //
  198. // Wasn't one of the standard types, so see if we can determine
  199. // what it is by its class name.
  200. //
  201. if (ctrl_type == 0) {
  202. ::_wcsupr (text_buffer);
  203. if (::wcsstr (text_buffer, L"TRACKBAR") != 0) {
  204. ctrl_type = SLIDER;
  205. } else if (::wcsstr (text_buffer, L"TABCONTROL") != 0) {
  206. ctrl_type = TAB;
  207. } else if (::wcsstr (text_buffer, L"LISTVIEW") != 0) {
  208. ctrl_type = LIST_CTRL;
  209. } else if (::wcsstr (text_buffer, L"MAP") != 0) {
  210. ctrl_type = MAP;
  211. } else if (::wcsstr (text_buffer, L"VIEWER") != 0) {
  212. ctrl_type = VIEWER;
  213. } else if (::wcsstr (text_buffer, L"HOTKEY") != 0) {
  214. ctrl_type = HOTKEY;
  215. } else if (::wcsstr (text_buffer, L"SHORTCUTBAR") != 0) {
  216. ctrl_type = SHORTCUT_BAR;
  217. } else if (::wcsstr (text_buffer, L"MERCHANDISE") != 0) {
  218. ctrl_type = MERCHANDISE_CTRL;
  219. } else if (::wcsstr (text_buffer, L"TREEVIEW") != 0) {
  220. ctrl_type = TREE_CTRL;
  221. } else if (::wcsicmp(text_buffer, PROGRESS_CLASSW) == 0) {
  222. ctrl_type = PROGRESS_BAR;
  223. } else if (::wcsstr (text_buffer, L"HEALTHBAR") != 0) {
  224. ctrl_type = HEALTH_BAR;
  225. }
  226. }
  227. //
  228. // Read the window text
  229. //
  230. buffer = Skip_Dlg_Field (buffer, text_buffer, 256);
  231. WCHAR *string_id = ::wcsstr (text_buffer, L"IDS_");
  232. if (string_id != NULL) {
  233. WideStringClass wide_string_id = string_id;
  234. StringClass ascii_string_id;
  235. wide_string_id.Convert_To (ascii_string_id);
  236. WideStringClass translation = TRANSLATE_BY_DESC(ascii_string_id);
  237. ::wcscpy (string_id, translation);
  238. }
  239. //
  240. // Add this control definition to the list
  241. //
  242. ControlDefinitionStruct definition;
  243. definition.id = (int)dlg_item_template->id;
  244. definition.style = dlg_item_template->style;
  245. definition.x = dlg_item_template->x;
  246. definition.y = dlg_item_template->y;
  247. definition.cx = dlg_item_template->cx;
  248. definition.cy = dlg_item_template->cy;
  249. definition.type = (CONTROL_TYPE)ctrl_type;
  250. definition.title = text_buffer;
  251. control_list->Add (definition);
  252. //
  253. // Skip past the extra data
  254. //
  255. WORD extra_data_size = *buffer;
  256. buffer ++;
  257. if (extra_data_size > 0) {
  258. buffer = (WORD *)(((char *)ALIGN_WORD_PTR(buffer)) + extra_data_size);
  259. }
  260. }
  261. }
  262. return ;
  263. }