dlgcontrolsaveload.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/commando/dlgcontrolsaveload.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/09/02 11:43a $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "dlgcontrolsaveload.h"
  36. #include "resource.h"
  37. #include "listctrl.h"
  38. #include "dialogresource.h"
  39. #include "inputconfigmgr.h"
  40. #include "string_ids.h"
  41. #include "translatedb.h"
  42. ////////////////////////////////////////////////////////////////
  43. // Local constants
  44. ////////////////////////////////////////////////////////////////
  45. static enum
  46. {
  47. MBEVENT_DELETE_PROMPT = 1,
  48. MBEVENT_SAVE_PROMPT
  49. };
  50. ////////////////////////////////////////////////////////////////
  51. //
  52. // ControlSaveLoadMenuClass
  53. //
  54. ////////////////////////////////////////////////////////////////
  55. ControlSaveLoadMenuClass::ControlSaveLoadMenuClass (void) :
  56. MenuDialogClass (IDD_MENU_CONTROL_SAVELOAD)
  57. {
  58. return ;
  59. }
  60. ////////////////////////////////////////////////////////////////
  61. //
  62. // On_Init_Dialog
  63. //
  64. ////////////////////////////////////////////////////////////////
  65. void
  66. ControlSaveLoadMenuClass::On_Init_Dialog (void)
  67. {
  68. ListCtrlClass *list_ctrl = (ListCtrlClass *)Get_Dlg_Item (IDC_LIST_CTRL);
  69. if (list_ctrl != NULL) {
  70. //
  71. // Configure the column
  72. //
  73. list_ctrl->Add_Column (L"", 1.0F, Vector3 (1, 1, 1));
  74. //
  75. // Loop over all the configurations
  76. //
  77. int count = InputConfigMgrClass::Get_Configuration_Count ();
  78. for (int index = 0; index < count; index ++) {
  79. //
  80. // Get information about this configuration
  81. //
  82. InputConfigClass config;
  83. InputConfigMgrClass::Get_Configuration (index, config);
  84. Insert_Configuration (config);
  85. }
  86. //
  87. // Add an entry so the user can add new configurations
  88. //
  89. int item_index = list_ctrl->Insert_Entry (index, TRANSLATE (IDS_MENU_EMPTY_SLOT));
  90. list_ctrl->Set_Curr_Sel (item_index);
  91. //
  92. // Sort the entries
  93. //
  94. list_ctrl->Sort (ListSortCallback, 0);
  95. }
  96. MenuDialogClass::On_Init_Dialog ();
  97. return ;
  98. }
  99. ////////////////////////////////////////////////////////////////
  100. //
  101. // On_Command
  102. //
  103. ////////////////////////////////////////////////////////////////
  104. void
  105. ControlSaveLoadMenuClass::On_Command (int ctrl_id, int message_id, DWORD param)
  106. {
  107. switch (ctrl_id)
  108. {
  109. case IDC_DELETE_BUTTON:
  110. Delete_Config ();
  111. break;
  112. case IDC_SAVE_BUTTON:
  113. Save_Config (true);
  114. break;
  115. case IDC_LOAD_BUTTON:
  116. Load_Config ();
  117. break;
  118. }
  119. MenuDialogClass::On_Command (ctrl_id, message_id, param);
  120. return ;
  121. }
  122. ////////////////////////////////////////////////////////////////
  123. //
  124. // Delete_Config
  125. //
  126. ////////////////////////////////////////////////////////////////
  127. void
  128. ControlSaveLoadMenuClass::Delete_Config (void)
  129. {
  130. ListCtrlClass *list_ctrl = (ListCtrlClass *)Get_Dlg_Item (IDC_LIST_CTRL);
  131. if (list_ctrl == NULL) {
  132. return ;
  133. }
  134. //
  135. // Get the current selection
  136. //
  137. int curr_sel = list_ctrl->Get_Curr_Sel ();
  138. if (curr_sel != -1) {
  139. //
  140. // Get the configuration object associated with this entry
  141. //
  142. InputConfigClass *config = (InputConfigClass *)list_ctrl->Get_Entry_Data (curr_sel, 0);
  143. if (config != NULL) {
  144. //
  145. // Delete the configuration (if possible)
  146. //
  147. if (config->Is_Custom ()) {
  148. //
  149. // Build a confirmation message to display to the user
  150. //
  151. WideStringClass message;
  152. message.Format (TRANSLATE (IDS_MENU_DELETE_SAVE_MSG),
  153. list_ctrl->Get_Entry_Text (curr_sel, 0));
  154. //
  155. // Display a message to the user asking if they really want to do this...
  156. //
  157. DlgMsgBox::DoDialog (TRANSLATE (IDS_MENU_DELETE_SAVE_TITLE), message, DlgMsgBox::YesNo,
  158. this, MBEVENT_DELETE_PROMPT);
  159. }
  160. }
  161. }
  162. return ;
  163. }
  164. ////////////////////////////////////////////////////////////////
  165. //
  166. // HandleNotification
  167. //
  168. ////////////////////////////////////////////////////////////////
  169. void
  170. ControlSaveLoadMenuClass::HandleNotification (DlgMsgBoxEvent &event)
  171. {
  172. if (event.Get_User_Data () == MBEVENT_DELETE_PROMPT) {
  173. //
  174. // The user has confirmed the delete, so delete the configuration
  175. //
  176. if (event.Event () == DlgMsgBoxEvent::Yes) {
  177. ListCtrlClass *list_ctrl = (ListCtrlClass *)Get_Dlg_Item (IDC_LIST_CTRL);
  178. if (list_ctrl != NULL) {
  179. //
  180. // Get the current selection
  181. //
  182. int curr_sel = list_ctrl->Get_Curr_Sel ();
  183. if (curr_sel != -1) {
  184. //
  185. // Get the configuration object associated with this entry
  186. //
  187. InputConfigClass *config = (InputConfigClass *)list_ctrl->Get_Entry_Data (curr_sel, 0);
  188. if (config != NULL) {
  189. InputConfigMgrClass::Delete_Configuration (config->Get_Filename ());
  190. list_ctrl->Delete_Entry (curr_sel);
  191. }
  192. }
  193. }
  194. }
  195. } else if (event.Get_User_Data () == MBEVENT_SAVE_PROMPT) {
  196. //
  197. // The user has confirmed the overwrite, so save the configuration
  198. //
  199. if (event.Event () == DlgMsgBoxEvent::Yes) {
  200. Save_Config (false);
  201. }
  202. }
  203. return ;
  204. }
  205. ////////////////////////////////////////////////////////////////
  206. //
  207. // Load_Config
  208. //
  209. ////////////////////////////////////////////////////////////////
  210. void
  211. ControlSaveLoadMenuClass::Load_Config (void)
  212. {
  213. ListCtrlClass *list_ctrl = (ListCtrlClass *)Get_Dlg_Item (IDC_LIST_CTRL);
  214. if (list_ctrl == NULL) {
  215. return ;
  216. }
  217. //
  218. // Get the current selection
  219. //
  220. int curr_sel = list_ctrl->Get_Curr_Sel ();
  221. if (curr_sel != -1) {
  222. //
  223. // Get the configuration object associated with this entry
  224. //
  225. InputConfigClass *config = (InputConfigClass *)list_ctrl->Get_Entry_Data (curr_sel, 0);
  226. if (config != NULL) {
  227. //
  228. // Load this configuration
  229. //
  230. InputConfigMgrClass::Load_Configuration (*config);
  231. End_Dialog ();
  232. }
  233. }
  234. return ;
  235. }
  236. ////////////////////////////////////////////////////////////////
  237. //
  238. // Save_Config
  239. //
  240. ////////////////////////////////////////////////////////////////
  241. void
  242. ControlSaveLoadMenuClass::Save_Config (bool prompt)
  243. {
  244. ListCtrlClass *list_ctrl = (ListCtrlClass *)Get_Dlg_Item (IDC_LIST_CTRL);
  245. if (list_ctrl == NULL) {
  246. return ;
  247. }
  248. //
  249. // Get the current selection
  250. //
  251. int curr_sel = list_ctrl->Get_Curr_Sel ();
  252. if (curr_sel != -1) {
  253. //
  254. // Get the configuration object associated with this entry
  255. //
  256. InputConfigClass *config = (InputConfigClass *)list_ctrl->Get_Entry_Data (curr_sel, 0);
  257. if (config != NULL) {
  258. //
  259. // We can only save custom configurations...
  260. //
  261. if (config->Is_Custom ()) {
  262. //
  263. // Get the new display name for this configuration
  264. //
  265. const WCHAR *display_name = Get_Dlg_Item_Text (IDC_NAME_EDIT);
  266. if (display_name[0] != 0) {
  267. //
  268. // Display a message to the user asking if they really want to do this...
  269. //
  270. bool save_file = true;
  271. if (prompt) {
  272. DlgMsgBox::DoDialog (IDS_MENU_CONTROLS_OVERWRITE_PROMPT_TITLE, IDS_MENU_CONTROLS_OVERWRITE_PROMPT_MSG,
  273. DlgMsgBox::YesNo, this, MBEVENT_SAVE_PROMPT);
  274. save_file = false;
  275. }
  276. if (save_file) {
  277. //
  278. // Update the name of this configuration
  279. //
  280. config->Set_Display_Name (display_name);
  281. list_ctrl->Set_Entry_Text (curr_sel, 0, display_name);
  282. //
  283. // Save the configuration
  284. //
  285. InputConfigMgrClass::Save_Configuration (*config);
  286. End_Dialog ();
  287. }
  288. } else {
  289. //
  290. // Let the user know they can't save a configuration without a name
  291. //
  292. DlgMsgBox::DoDialog (IDS_MENU_CANT_SAVE_CONFIG, IDS_MENU_CONFIG_NEEDS_NAME, DlgMsgBox::Okay, NULL, 0);
  293. }
  294. }
  295. } else {
  296. //
  297. // Get the new display name for this configuration
  298. //
  299. const WCHAR *display_name = Get_Dlg_Item_Text (IDC_NAME_EDIT);
  300. if (display_name[0] != 0) {
  301. //
  302. // Add a new configuration
  303. //
  304. InputConfigMgrClass::Add_Configuration (display_name);
  305. End_Dialog ();
  306. } else {
  307. //
  308. // Let the user know they can't save a configuration without a name
  309. //
  310. DlgMsgBox::DoDialog (IDS_MENU_CANT_SAVE_CONFIG, IDS_MENU_CONFIG_NEEDS_NAME, DlgMsgBox::Okay, NULL, 0);
  311. }
  312. }
  313. }
  314. return ;
  315. }
  316. ////////////////////////////////////////////////////////////////
  317. //
  318. // On_ListCtrl_Delete_Entry
  319. //
  320. ////////////////////////////////////////////////////////////////
  321. void
  322. ControlSaveLoadMenuClass::On_ListCtrl_Delete_Entry
  323. (
  324. ListCtrlClass *list_ctrl,
  325. int ctrl_id,
  326. int item_index
  327. )
  328. {
  329. //
  330. // Remove the data we associated with this entry
  331. //
  332. InputConfigClass *config = (InputConfigClass *)list_ctrl->Get_Entry_Data (item_index, 0);
  333. list_ctrl->Set_Entry_Data (item_index, 0, NULL);
  334. //
  335. // Free the config object
  336. //
  337. if (config != NULL) {
  338. delete config;
  339. }
  340. return ;
  341. }
  342. ////////////////////////////////////////////////////////////////
  343. //
  344. // Insert_Configuration
  345. //
  346. ////////////////////////////////////////////////////////////////
  347. int
  348. ControlSaveLoadMenuClass::Insert_Configuration (const InputConfigClass &config)
  349. {
  350. ListCtrlClass *list_ctrl = (ListCtrlClass *)Get_Dlg_Item (IDC_LIST_CTRL);
  351. if (list_ctrl == NULL) {
  352. return -1;
  353. }
  354. //
  355. // Add an entry for this configuration to the list
  356. //
  357. int item_index = list_ctrl->Insert_Entry (0xFFFF, config.Get_Display_Name ());
  358. if (item_index != -1) {
  359. //
  360. // Make a copy of the config object and store it with the entry
  361. //
  362. InputConfigClass *local_copy = new InputConfigClass (config);
  363. list_ctrl->Set_Entry_Data (item_index, 0, (DWORD)local_copy);
  364. //
  365. // Change the color of this configuration if the user cannot edit it
  366. //
  367. if (config.Is_Custom () == false) {
  368. list_ctrl->Set_Entry_Color (item_index, 0, Vector3 (1.0F, 1.0F, 1.0F));
  369. }
  370. }
  371. return item_index;
  372. }
  373. ////////////////////////////////////////////////////////////////
  374. //
  375. // On_ListCtrl_Sel_Change
  376. //
  377. ////////////////////////////////////////////////////////////////
  378. void
  379. ControlSaveLoadMenuClass::On_ListCtrl_Sel_Change
  380. (
  381. ListCtrlClass * list_ctrl,
  382. int ctrl_id,
  383. int old_index,
  384. int new_index
  385. )
  386. {
  387. bool enable_edit = true;
  388. //
  389. // Remove the data we associated with this entry
  390. //
  391. InputConfigClass *config = (InputConfigClass *)list_ctrl->Get_Entry_Data (new_index, 0);
  392. if (config != NULL) {
  393. //
  394. // We want to disable the edit control if the user can't edit this entry
  395. //
  396. if (config->Is_Custom () == false) {
  397. enable_edit = false;
  398. }
  399. //
  400. // Put the name of the currently selected configuration in the control
  401. //
  402. Set_Dlg_Item_Text (IDC_NAME_EDIT, config->Get_Display_Name ());
  403. } else {
  404. //
  405. // Clear the name of the current configuration
  406. //
  407. Set_Dlg_Item_Text (IDC_NAME_EDIT, L"");
  408. }
  409. //
  410. // Fix the enable state of the edit control
  411. //
  412. Enable_Dlg_Item (IDC_NAME_EDIT, enable_edit);
  413. return ;
  414. }
  415. ////////////////////////////////////////////////////////////////
  416. //
  417. // ListSortCallback
  418. //
  419. ////////////////////////////////////////////////////////////////
  420. int CALLBACK
  421. ControlSaveLoadMenuClass::ListSortCallback
  422. (
  423. ListCtrlClass * list_ctrl,
  424. int item_index1,
  425. int item_index2,
  426. uint32 user_param
  427. )
  428. {
  429. int retval = 0;
  430. if (list_ctrl->Get_Entry_Data (item_index1, 0) == NULL) {
  431. retval = 1;
  432. } else if (list_ctrl->Get_Entry_Data (item_index2, 0) == NULL) {
  433. retval = -1;
  434. } else {
  435. InputConfigClass *config1 = (InputConfigClass *)list_ctrl->Get_Entry_Data (item_index1, 0);
  436. InputConfigClass *config2 = (InputConfigClass *)list_ctrl->Get_Entry_Data (item_index2, 0);
  437. //
  438. // Sort based on the type of configuration
  439. //
  440. if (config1->Is_Default () && config2->Is_Default () == false) {
  441. retval = -1;
  442. } else if (config1->Is_Default () == false && config2->Is_Default ()) {
  443. retval = 1;
  444. } else if (config1->Is_Custom () && config2->Is_Custom () == false) {
  445. retval = -1;
  446. } else if (config1->Is_Custom () == false && config2->Is_Custom ()) {
  447. retval = 1;
  448. } else {
  449. //
  450. // Sort based on the names
  451. //
  452. retval = ::wcsicmp (config1->Get_Display_Name (), config2->Get_Display_Name ());
  453. }
  454. }
  455. return retval;
  456. }
  457. ////////////////////////////////////////////////////////////////
  458. //
  459. // On_EditCtrl_Enter_Pressed
  460. //
  461. ////////////////////////////////////////////////////////////////
  462. void
  463. ControlSaveLoadMenuClass::On_EditCtrl_Enter_Pressed (EditCtrlClass *edit_ctrl, int ctrl_id)
  464. {
  465. if (ctrl_id == IDC_NAME_EDIT) {
  466. Save_Config (true);
  467. }
  468. return ;
  469. }