SelectPresetDialog.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. // SelectPresetDialog.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "leveledit.h"
  22. #include "SelectPresetDialog.h"
  23. #include "presetmgr.h"
  24. #include "preset.h"
  25. #include "icons.h"
  26. #include "nodecategories.h"
  27. #include "definitionfactory.h"
  28. #include "definitionfactorymgr.h"
  29. #ifdef _DEBUG
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34. /////////////////////////////////////////////////////////////////////////////
  35. //
  36. // SelectPresetDialogClass
  37. //
  38. /////////////////////////////////////////////////////////////////////////////
  39. SelectPresetDialogClass::SelectPresetDialogClass(CWnd* pParent /*=NULL*/)
  40. : m_ClassID (0),
  41. m_Preset (NULL),
  42. m_IconIndex (0),
  43. m_InitialSelection (NULL),
  44. m_AllowNoneSelection (true),
  45. CDialog(SelectPresetDialogClass::IDD, pParent)
  46. {
  47. //{{AFX_DATA_INIT(SelectPresetDialogClass)
  48. // NOTE: the ClassWizard will add member initialization here
  49. //}}AFX_DATA_INIT
  50. return ;
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. //
  54. // DoDataExchange
  55. //
  56. /////////////////////////////////////////////////////////////////////////////
  57. void
  58. SelectPresetDialogClass::DoDataExchange (CDataExchange* pDX)
  59. {
  60. CDialog::DoDataExchange(pDX);
  61. //{{AFX_DATA_MAP(SelectPresetDialogClass)
  62. DDX_Control(pDX, IDC_PRESET_TREE, m_TreeCtrl);
  63. //}}AFX_DATA_MAP
  64. return ;
  65. }
  66. BEGIN_MESSAGE_MAP(SelectPresetDialogClass, CDialog)
  67. //{{AFX_MSG_MAP(SelectPresetDialogClass)
  68. ON_WM_DESTROY()
  69. ON_NOTIFY(TVN_SELCHANGED, IDC_PRESET_TREE, OnSelchangedPresetTree)
  70. ON_BN_CLICKED(IDC_INFO, OnInfo)
  71. //}}AFX_MSG_MAP
  72. END_MESSAGE_MAP()
  73. /////////////////////////////////////////////////////////////////////////////
  74. //
  75. // OnInitDialog
  76. //
  77. /////////////////////////////////////////////////////////////////////////////
  78. BOOL
  79. SelectPresetDialogClass::OnInitDialog (void)
  80. {
  81. CDialog::OnInitDialog ();
  82. CWaitCursor wait_cursor;
  83. //
  84. // Configure the tree control
  85. //
  86. m_TreeCtrl.SetImageList (::Get_Global_Image_List (), TVSIL_NORMAL);
  87. //
  88. // Determine if we should display all presets or just a specific branch
  89. // of the tree.
  90. //
  91. if (m_ClassID == 0) {
  92. //
  93. // Build the complete preset tree
  94. //
  95. Build_Full_Preset_Tree ();
  96. } else {
  97. //
  98. // Build the tree for just this branch of presets
  99. //
  100. Generate_Tree (m_ClassID, TVI_ROOT);
  101. }
  102. //
  103. // Add a way for the user to clear the selection
  104. //
  105. HTREEITEM none_entry = NULL;
  106. if (m_AllowNoneSelection) {
  107. none_entry = m_TreeCtrl.InsertItem ("<None>", NULL_ICON, NULL_ICON, TVI_ROOT, TVI_FIRST);
  108. }
  109. //
  110. // Select the correct entry (if necessary)
  111. //
  112. if (m_InitialSelection != NULL) {
  113. m_TreeCtrl.EnsureVisible (m_InitialSelection);
  114. m_TreeCtrl.SelectItem (m_InitialSelection);
  115. } else if (none_entry != NULL) {
  116. m_TreeCtrl.SelectItem (none_entry);
  117. }
  118. //
  119. // Change the window title if necessary
  120. //
  121. if (m_Title.GetLength () > 0) {
  122. SetWindowText (m_Title);
  123. }
  124. //
  125. // Change the dialog message if necessary
  126. //
  127. if (m_Message.GetLength () > 0) {
  128. SetDlgItemText (IDC_MESSAGE, m_Message);
  129. }
  130. return TRUE;
  131. }
  132. /////////////////////////////////////////////////////////////////////////////
  133. //
  134. // Fill_Tree
  135. //
  136. /////////////////////////////////////////////////////////////////////////////
  137. void
  138. SelectPresetDialogClass::Fill_Tree (NTreeLeafClass<PresetClass *> *leaf, HTREEITEM parent_item)
  139. {
  140. //
  141. // Loop over all the presets
  142. //
  143. for ( ; leaf != NULL; leaf = leaf->Peek_Next ()) {
  144. PresetClass *preset = leaf->Get_Value ();
  145. ASSERT (preset != NULL);
  146. if (preset != NULL) {
  147. //
  148. // Insert the new preset into the tree
  149. //
  150. int icon_index = preset->Get_Icon_Index ();
  151. HTREEITEM new_item = m_TreeCtrl.InsertItem (preset->Get_Name (), icon_index, icon_index, parent_item);
  152. if (new_item != NULL) {
  153. //
  154. // Associate the preset with its tree entry
  155. //
  156. m_TreeCtrl.SetItemData (new_item, (LONG)preset);
  157. //
  158. // Recurse if necessary
  159. //
  160. NTreeLeafClass<PresetClass *> *child = leaf->Peek_Child ();
  161. if (child != NULL) {
  162. Fill_Tree (child, new_item);
  163. m_TreeCtrl.SortChildren (parent_item);
  164. }
  165. //
  166. // Remember that we need to select this item
  167. //
  168. if (preset == m_Preset) {
  169. m_InitialSelection = new_item;
  170. }
  171. }
  172. }
  173. }
  174. return ;
  175. }
  176. /////////////////////////////////////////////////////////////////////////////
  177. //
  178. // OnOK
  179. //
  180. /////////////////////////////////////////////////////////////////////////////
  181. void
  182. SelectPresetDialogClass::OnOK (void)
  183. {
  184. //
  185. // Save the selected preset
  186. //
  187. HTREEITEM selected_item = m_TreeCtrl.GetSelectedItem ();
  188. if (selected_item != NULL) {
  189. m_Preset = (PresetClass *)m_TreeCtrl.GetItemData (selected_item);
  190. }
  191. CDialog::OnOK ();
  192. return ;
  193. }
  194. /////////////////////////////////////////////////////////////////////////////
  195. //
  196. // OnDestroy
  197. //
  198. /////////////////////////////////////////////////////////////////////////////
  199. void
  200. SelectPresetDialogClass::OnDestroy (void)
  201. {
  202. //
  203. // Remove the main image list we associated with the control
  204. //
  205. m_TreeCtrl.SetImageList (NULL, TVSIL_NORMAL);
  206. CDialog::OnDestroy ();
  207. return ;
  208. }
  209. /////////////////////////////////////////////////////////////////////////////
  210. //
  211. // Build_Full_Preset_Tree
  212. //
  213. /////////////////////////////////////////////////////////////////////////////
  214. void
  215. SelectPresetDialogClass::Build_Full_Preset_Tree (void)
  216. {
  217. //
  218. // Add all the node categories to the list control
  219. //
  220. for (int index = 0; index < PRESET_CATEGORY_COUNT; index ++) {
  221. //
  222. // Add this factory to the tree
  223. //
  224. HTREEITEM tree_item = m_TreeCtrl.InsertItem (PRESET_CATEGORIES[index].name, FOLDER_ICON, FOLDER_ICON);
  225. m_IconIndex = PRESET_CATEGORIES[index].icon;
  226. //
  227. // Add all presets for this factory into the tree
  228. //
  229. Generate_Tree (PRESET_CATEGORIES[index].clsid, tree_item);
  230. //
  231. // Is there a factory to create this class of defintion?
  232. //
  233. /*DefinitionFactoryClass *factory = DefinitionFactoryMgrClass::Find_Factory (PRESET_CATEGORIES[index].clsid);
  234. if (factory == NULL) {
  235. //
  236. // Find all the sub-factories
  237. //
  238. for ( factory = DefinitionFactoryMgrClass::Get_First (PRESET_CATEGORIES[index].clsid);
  239. factory != NULL;
  240. factory = DefinitionFactoryMgrClass::Get_Next (factory, PRESET_CATEGORIES[index].clsid))
  241. {
  242. //
  243. // Add this sub-factory and all its definitions to the tree
  244. //
  245. LPCTSTR name = factory->Get_Name ();
  246. HTREEITEM child_item = m_TreeCtrl.InsertItem (factory->Get_Name (), FOLDER_ICON, FOLDER_ICON, tree_item);
  247. Generate_Tree (factory->Get_Class_ID (), child_item);
  248. }
  249. //
  250. // Sort the entries we just made
  251. //
  252. m_TreeCtrl.SortChildren (tree_item);
  253. }*/
  254. }
  255. return ;
  256. }
  257. /////////////////////////////////////////////////////////////////////////////
  258. //
  259. // Generate_Tree
  260. //
  261. /////////////////////////////////////////////////////////////////////////////
  262. void
  263. SelectPresetDialogClass::Generate_Tree (int class_id, HTREEITEM parent_item)
  264. {
  265. //
  266. // Is there a factory to create this class of defintion?
  267. //
  268. DefinitionFactoryClass *factory = DefinitionFactoryMgrClass::Find_Factory (class_id);
  269. if (factory == NULL) {
  270. //
  271. // Find all the sub-factories
  272. //
  273. for ( factory = DefinitionFactoryMgrClass::Get_First (class_id);
  274. factory != NULL;
  275. factory = DefinitionFactoryMgrClass::Get_Next (factory, class_id))
  276. {
  277. //
  278. // Add this sub-factory and all its definitions to the tree
  279. //
  280. if (factory->Is_Displayed ()) {
  281. LPCTSTR name = factory->Get_Name ();
  282. HTREEITEM child_item = m_TreeCtrl.InsertItem (factory->Get_Name (), FOLDER_ICON, FOLDER_ICON, parent_item);
  283. Generate_Tree (factory->Get_Class_ID (), child_item);
  284. }
  285. }
  286. //
  287. // Sort the entries we just made
  288. //
  289. m_TreeCtrl.SortChildren (parent_item);
  290. } else {
  291. //
  292. // Generate the tree of presets
  293. //
  294. PRESET_TREE preset_tree;
  295. PresetMgrClass::Build_Preset_Tree (class_id, preset_tree);
  296. //
  297. // Populate the tree control
  298. //
  299. Fill_Tree (preset_tree.Peek_Root (), parent_item);
  300. }
  301. return ;
  302. }
  303. /////////////////////////////////////////////////////////////////////////////
  304. //
  305. // OnSelchangedPresetTree
  306. //
  307. /////////////////////////////////////////////////////////////////////////////
  308. void
  309. SelectPresetDialogClass::OnSelchangedPresetTree
  310. (
  311. NMHDR * pNMHDR,
  312. LRESULT * pResult
  313. )
  314. {
  315. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  316. (*pResult) = 0;
  317. //
  318. // Either disable or enable the OK button depending on if the user selected
  319. // a real preset or not...
  320. //
  321. HTREEITEM selected_item = m_TreeCtrl.GetSelectedItem ();
  322. if (selected_item != NULL) {
  323. PresetClass *preset = (PresetClass *)m_TreeCtrl.GetItemData (selected_item);
  324. if (m_AllowNoneSelection == false) {
  325. ::EnableWindow (::GetDlgItem (m_hWnd, IDOK), preset != NULL);
  326. }
  327. ::EnableWindow (::GetDlgItem (m_hWnd, IDC_INFO), preset != NULL);
  328. }
  329. return ;
  330. }
  331. /////////////////////////////////////////////////////////////////////////////
  332. //
  333. // OnInfo
  334. //
  335. /////////////////////////////////////////////////////////////////////////////
  336. void
  337. SelectPresetDialogClass::OnInfo (void)
  338. {
  339. HTREEITEM selected_item = m_TreeCtrl.GetSelectedItem ();
  340. if (selected_item != NULL) {
  341. PresetClass *preset = (PresetClass *)m_TreeCtrl.GetItemData (selected_item);
  342. if (preset != NULL) {
  343. //
  344. // Show the settings for this preset
  345. //
  346. preset->Show_Properties (true);
  347. }
  348. }
  349. return ;
  350. }