StringPickerDialog.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. // StringPickerDialog.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "leveledit.h"
  22. #include "stringpickerdialog.h"
  23. #include "stringscategoryviewdialog.h"
  24. #include "translatedb.h"
  25. #include "translateobj.h"
  26. #include "utils.h"
  27. #ifdef _DEBUG
  28. #define new DEBUG_NEW
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #endif
  32. /////////////////////////////////////////////////////////////////////////////
  33. // Contants
  34. /////////////////////////////////////////////////////////////////////////////
  35. static const int BORDER_X = 10;
  36. static const int BORDER_Y = 10;
  37. static const int SPACING_X = 10;
  38. static const int SPACING_Y = 10;
  39. /////////////////////////////////////////////////////////////////////////////
  40. //
  41. // StringPickerDialogClass
  42. //
  43. /////////////////////////////////////////////////////////////////////////////
  44. StringPickerDialogClass::StringPickerDialogClass (CWnd *pParent)
  45. : SelectedObjectID (0),
  46. CurrentTab (0),
  47. IsInitialized (false),
  48. CDialog(StringPickerDialogClass::IDD, pParent)
  49. {
  50. //{{AFX_DATA_INIT(StringPickerDialogClass)
  51. // NOTE: the ClassWizard will add member initialization here
  52. //}}AFX_DATA_INIT
  53. return ;
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. //
  57. // ~StringPickerDialogClass
  58. //
  59. /////////////////////////////////////////////////////////////////////////////
  60. StringPickerDialogClass::~StringPickerDialogClass (void)
  61. {
  62. //
  63. // Free the child dialog objects
  64. //
  65. for (int index = 0; index < CategoryPages.Count (); index ++) {
  66. delete CategoryPages[index];
  67. }
  68. CategoryPages.Delete_All ();
  69. return ;
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. //
  73. // DoDataExchange
  74. //
  75. /////////////////////////////////////////////////////////////////////////////
  76. void
  77. StringPickerDialogClass::DoDataExchange (CDataExchange *pDX)
  78. {
  79. CDialog::DoDataExchange(pDX);
  80. //{{AFX_DATA_MAP(StringPickerDialogClass)
  81. DDX_Control(pDX, IDC_TAB_CTRL, m_TabCtrl);
  82. //}}AFX_DATA_MAP
  83. return ;
  84. }
  85. BEGIN_MESSAGE_MAP(StringPickerDialogClass, CDialog)
  86. //{{AFX_MSG_MAP(StringPickerDialogClass)
  87. ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_CTRL, OnSelchangeTabCtrl)
  88. ON_WM_SIZE()
  89. //}}AFX_MSG_MAP
  90. END_MESSAGE_MAP()
  91. /////////////////////////////////////////////////////////////////////////////
  92. //
  93. // Create
  94. //
  95. /////////////////////////////////////////////////////////////////////////////
  96. void
  97. StringPickerDialogClass::Create (CWnd *parent_wnd)
  98. {
  99. CDialog::Create (StringPickerDialogClass::IDD, parent_wnd);
  100. return ;
  101. }
  102. /////////////////////////////////////////////////////////////////////////////
  103. //
  104. // Resize_Controls
  105. //
  106. /////////////////////////////////////////////////////////////////////////////
  107. void
  108. StringPickerDialogClass::Resize_Controls (void)
  109. {
  110. //
  111. // Get the window and button bounding rectangles
  112. //
  113. CRect rect;
  114. CRect edit_rect;
  115. GetClientRect (&rect);
  116. ::GetWindowRect (::GetDlgItem (m_hWnd, IDC_STRING_EDIT), &edit_rect);
  117. //
  118. // Calculate some positions and widths
  119. //
  120. int width = rect.Width ();
  121. int height = rect.Height ();
  122. int edit_height = edit_rect.Height ();
  123. int tab_width = width;
  124. int tab_height = height - (SPACING_Y + edit_height);
  125. int edit_y_pos = tab_height + SPACING_Y;
  126. //
  127. // Resize the tab control
  128. //
  129. m_TabCtrl.SetWindowPos (NULL, 0, 0, tab_width, tab_height, SWP_NOZORDER | SWP_NOCOPYBITS);
  130. //
  131. // Get the display rectangle of the tab control
  132. //
  133. CRect tab_rect;
  134. m_TabCtrl.GetWindowRect (&tab_rect);
  135. m_TabCtrl.AdjustRect (FALSE, &tab_rect);
  136. ScreenToClient (&tab_rect);
  137. //
  138. // Resize all the category page controls
  139. //
  140. for (int index = 0; index < CategoryPages.Count (); index ++) {
  141. CategoryPages[index]->SetWindowPos (NULL, tab_rect.left + BORDER_X, tab_rect.top + BORDER_Y,
  142. tab_rect.Width () - BORDER_X * 2, tab_rect.Height () - BORDER_Y * 2, SWP_NOZORDER | SWP_NOCOPYBITS | SWP_NOACTIVATE);
  143. }
  144. //
  145. // Reposition the edit control
  146. //
  147. ::SetWindowPos (::GetDlgItem (m_hWnd, IDC_STRING_EDIT), NULL, 0, edit_y_pos,
  148. tab_width, edit_height, SWP_NOZORDER | SWP_NOCOPYBITS | SWP_NOACTIVATE);
  149. return ;
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. //
  153. // OnSelchangeTabCtrl
  154. //
  155. /////////////////////////////////////////////////////////////////////////////
  156. void
  157. StringPickerDialogClass::OnSelchangeTabCtrl
  158. (
  159. NMHDR * pNMHDR,
  160. LRESULT * pResult
  161. )
  162. {
  163. (*pResult) = 0;
  164. Update_Page_Visibility ();
  165. //
  166. // Update our cached selection ID
  167. //
  168. if (IsInitialized) {
  169. Get_Selection ();
  170. Update_Selected_String ();
  171. }
  172. return ;
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. //
  176. // Update_Page_Visibility
  177. //
  178. /////////////////////////////////////////////////////////////////////////////
  179. void
  180. StringPickerDialogClass::Update_Page_Visibility (void)
  181. {
  182. //
  183. // Check to see if the user has selected a new tab
  184. //
  185. int newtab = m_TabCtrl.GetCurSel ();
  186. if (CurrentTab != newtab) {
  187. //
  188. // Hide the old tab
  189. //
  190. if (CurrentTab < CategoryPages.Count () && CategoryPages[CurrentTab] != NULL) {
  191. CategoryPages[CurrentTab]->ShowWindow (SW_HIDE);
  192. }
  193. //
  194. // Show the new tab
  195. //
  196. if (CategoryPages[newtab] != NULL) {
  197. CategoryPages[newtab]->ShowWindow (SW_SHOW);
  198. }
  199. //
  200. // Remember what our new current tab is
  201. //
  202. CurrentTab = newtab;
  203. }
  204. return ;
  205. }
  206. /////////////////////////////////////////////////////////////////////////////
  207. //
  208. // OnSize
  209. //
  210. /////////////////////////////////////////////////////////////////////////////
  211. void
  212. StringPickerDialogClass::OnSize
  213. (
  214. UINT nType,
  215. int cx,
  216. int cy
  217. )
  218. {
  219. CDialog::OnSize (nType, cx, cy);
  220. if (IsInitialized) {
  221. Resize_Controls ();
  222. }
  223. return ;
  224. }
  225. /////////////////////////////////////////////////////////////////////////////
  226. //
  227. // OnInitDialog
  228. //
  229. /////////////////////////////////////////////////////////////////////////////
  230. BOOL
  231. StringPickerDialogClass::OnInitDialog (void)
  232. {
  233. CDialog::OnInitDialog ();
  234. //
  235. // Loop over all the categories in the database
  236. //
  237. int count = TranslateDBClass::Get_Category_Count ();
  238. for (int index = 0; index < count; index ++) {
  239. //
  240. // Lookup this category
  241. //
  242. TDBCategoryClass *category = TranslateDBClass::Get_Category (index);
  243. if (category != NULL) {
  244. Add_Category_Page (category);
  245. }
  246. }
  247. //
  248. // Try to find the tab that the selected object is displayed on
  249. //
  250. for (index = 0; index < CategoryPages.Count (); index ++) {
  251. if (CategoryPages[index]->Get_Selection () == SelectedObjectID) {
  252. m_TabCtrl.SetCurSel (index);
  253. CurrentTab = index;
  254. break;
  255. }
  256. }
  257. //
  258. // Display the first category page
  259. //
  260. if (CategoryPages.Count () > CurrentTab) {
  261. CategoryPages[CurrentTab]->ShowWindow (SW_SHOW);
  262. }
  263. Resize_Controls ();
  264. Update_Selected_String ();
  265. IsInitialized = true;
  266. return TRUE;
  267. }
  268. /////////////////////////////////////////////////////////////////////////////
  269. //
  270. // Add_Category_Page
  271. //
  272. /////////////////////////////////////////////////////////////////////////////
  273. void
  274. StringPickerDialogClass::Add_Category_Page (TDBCategoryClass *category)
  275. {
  276. //
  277. // Add a tab to the dialog for this category
  278. //
  279. TC_ITEM tab_info = { 0 };
  280. tab_info.mask = TCIF_TEXT;
  281. tab_info.pszText = (char *)(LPCTSTR)category->Get_Name ();
  282. m_TabCtrl.InsertItem (0xFF, &tab_info);
  283. //
  284. // Create a page for this new category
  285. //
  286. StringsCategoryViewDialogClass *child_wnd = new StringsCategoryViewDialogClass;
  287. child_wnd->Set_Category_ID (category->Get_ID ());
  288. child_wnd->Set_Edit_Mode (StringsCategoryViewDialogClass::EDIT_MODE_NONE);
  289. child_wnd->Set_Selection (SelectedObjectID);
  290. child_wnd->Set_Callback (this);
  291. child_wnd->Remove_Column (StringsCategoryViewDialogClass::COL_TEXT);
  292. child_wnd->Set_Column_Width (StringsCategoryViewDialogClass::COL_ID, 0.95F);
  293. child_wnd->Create (this);
  294. CategoryPages.Add (child_wnd);
  295. return ;
  296. }
  297. /////////////////////////////////////////////////////////////////////////////
  298. //
  299. // Get_Selection
  300. //
  301. /////////////////////////////////////////////////////////////////////////////
  302. int
  303. StringPickerDialogClass::Get_Selection (void)
  304. {
  305. //
  306. // Check to see if we need to update the cached selection ID
  307. //
  308. if (::IsWindow (m_hWnd)) {
  309. SelectedObjectID = CategoryPages[CurrentTab]->Get_Selection ();
  310. }
  311. return SelectedObjectID;
  312. }
  313. /////////////////////////////////////////////////////////////////////////////
  314. //
  315. // On_Selection_Changed
  316. //
  317. /////////////////////////////////////////////////////////////////////////////
  318. void
  319. StringPickerDialogClass::On_Selection_Changed (int sel_object_id)
  320. {
  321. SelectedObjectID = sel_object_id;
  322. Update_Selected_String ();
  323. return ;
  324. }
  325. /////////////////////////////////////////////////////////////////////////////
  326. //
  327. // Update_Selected_String
  328. //
  329. /////////////////////////////////////////////////////////////////////////////
  330. void
  331. StringPickerDialogClass::Update_Selected_String (void)
  332. {
  333. StringClass english_text;
  334. if (SelectedObjectID != 0) {
  335. //
  336. // Lookup the text associated with the new string
  337. //
  338. TDBObjClass *object = TranslateDBClass::Find_Object (SelectedObjectID);
  339. if (object != NULL) {
  340. english_text = object->Get_English_String ();
  341. }
  342. }
  343. //
  344. // Set the text of the selected string
  345. //
  346. SetDlgItemText (IDC_STRING_EDIT, english_text);
  347. return ;
  348. }