ExportPresetsDialog.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // ExportPresetsDialog.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "leveledit.h"
  22. #include "exportpresetsdialog.h"
  23. #include "presetmgr.h"
  24. #include "nodecategories.h"
  25. #include "utils.h"
  26. #include "definitionfactory.h"
  27. #include "definitionfactorymgr.h"
  28. #include "presetexport.h"
  29. #ifdef _DEBUG
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34. /////////////////////////////////////////////////////////////////////////////
  35. //
  36. // ExportPresetsDialogClass
  37. //
  38. /////////////////////////////////////////////////////////////////////////////
  39. ExportPresetsDialogClass::ExportPresetsDialogClass(CWnd* pParent /*=NULL*/)
  40. : CDialog(ExportPresetsDialogClass::IDD, pParent)
  41. {
  42. //{{AFX_DATA_INIT(ExportPresetsDialogClass)
  43. // NOTE: the ClassWizard will add member initialization here
  44. //}}AFX_DATA_INIT
  45. return ;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. //
  49. // DoDataExchange
  50. //
  51. /////////////////////////////////////////////////////////////////////////////
  52. void
  53. ExportPresetsDialogClass::DoDataExchange (CDataExchange* pDX)
  54. {
  55. CDialog::DoDataExchange(pDX);
  56. //{{AFX_DATA_MAP(ExportPresetsDialogClass)
  57. DDX_Control(pDX, IDC_TREE_CTRL, m_TreeCtrl);
  58. //}}AFX_DATA_MAP
  59. }
  60. BEGIN_MESSAGE_MAP(ExportPresetsDialogClass, CDialog)
  61. //{{AFX_MSG_MAP(ExportPresetsDialogClass)
  62. ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
  63. //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65. /////////////////////////////////////////////////////////////////////////////
  66. //
  67. // OnInitDialog
  68. //
  69. /////////////////////////////////////////////////////////////////////////////
  70. BOOL
  71. ExportPresetsDialogClass::OnInitDialog (void)
  72. {
  73. CDialog::OnInitDialog ();
  74. //
  75. // Configure the tree control
  76. //
  77. m_TreeCtrl.SetImageList (::Get_Global_Image_List (), TVSIL_NORMAL);
  78. Fill_Tree ();
  79. return TRUE;
  80. }
  81. /////////////////////////////////////////////////////////////////////////////
  82. //
  83. // OnBrowse
  84. //
  85. /////////////////////////////////////////////////////////////////////////////
  86. void
  87. ExportPresetsDialogClass::OnBrowse (void)
  88. {
  89. CFileDialog dialog (FALSE, ".txt", "presets.txt",
  90. OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,
  91. "Text Files (*.txt)|*.txt||", ::AfxGetMainWnd ());
  92. //
  93. // Ask the user what file they want to create
  94. //
  95. if (dialog.DoModal () == IDOK) {
  96. //
  97. // Check to make sure the destination filename is not read-only
  98. //
  99. CString path = dialog.GetPathName ();
  100. DWORD file_attributes = ::GetFileAttributes (path);
  101. if (file_attributes != 0xFFFFFFFF && file_attributes & FILE_ATTRIBUTE_READONLY) {
  102. ::MessageBox (::AfxGetMainWnd ()->m_hWnd, "File is read-only, export operation can not complete.", "File Error", MB_ICONERROR | MB_OK);
  103. } else {
  104. SetDlgItemText (IDC_FILENAME_EDIT, path);
  105. }
  106. }
  107. return ;
  108. }
  109. /////////////////////////////////////////////////////////////////////////////
  110. //
  111. // OnOK
  112. //
  113. /////////////////////////////////////////////////////////////////////////////
  114. void
  115. ExportPresetsDialogClass::OnOK (void)
  116. {
  117. //
  118. // Get the filename the user entered
  119. //
  120. CString filename;
  121. GetDlgItemText (IDC_FILENAME_EDIT, filename);
  122. if (filename.IsEmpty ()) {
  123. ::MessageBox (m_hWnd, "Please enter a filename.", "Invalid Setting", MB_ICONERROR | MB_OK);
  124. } else {
  125. //
  126. // Lookup the class ID of the selected preset category
  127. //
  128. HTREEITEM selected_item = m_TreeCtrl.GetSelectedItem ();
  129. int class_id = 0;
  130. if (selected_item != NULL) {
  131. class_id = m_TreeCtrl.GetItemData (selected_item);
  132. }
  133. if (class_id == 0) {
  134. ::MessageBox (m_hWnd, "Please select a preset category to export.", "Invalid Setting", MB_ICONERROR | MB_OK);
  135. } else {
  136. //
  137. // Export the settings to the given filename
  138. //
  139. PresetExportClass::Export (class_id, filename);
  140. CDialog::OnOK ();
  141. }
  142. }
  143. return ;
  144. }
  145. /////////////////////////////////////////////////////////////////////////////
  146. //
  147. // Fill_Tree
  148. //
  149. /////////////////////////////////////////////////////////////////////////////
  150. void
  151. ExportPresetsDialogClass::Fill_Tree (void)
  152. {
  153. //
  154. // Add all the node categories to the tree control
  155. //
  156. for (int index = 0; index < PRESET_CATEGORY_COUNT; index ++) {
  157. //
  158. // Add this factory to the tree
  159. //
  160. HTREEITEM tree_item = m_TreeCtrl.InsertItem (PRESET_CATEGORIES[index].name, FOLDER_ICON, FOLDER_ICON);
  161. //
  162. // Is there a factory to create this class of defintion?
  163. //
  164. DefinitionFactoryClass *factory = DefinitionFactoryMgrClass::Find_Factory (PRESET_CATEGORIES[index].clsid);
  165. if (factory == NULL) {
  166. //
  167. // Find all the sub-factories
  168. //
  169. for ( factory = DefinitionFactoryMgrClass::Get_First (PRESET_CATEGORIES[index].clsid);
  170. factory != NULL;
  171. factory = DefinitionFactoryMgrClass::Get_Next (factory, PRESET_CATEGORIES[index].clsid))
  172. {
  173. //
  174. // Add this sub-factory to the tree
  175. //
  176. if (factory->Is_Displayed ()) {
  177. LPCTSTR name = factory->Get_Name ();
  178. HTREEITEM child_item = m_TreeCtrl.InsertItem (factory->Get_Name (), FOLDER_ICON, FOLDER_ICON, tree_item);
  179. m_TreeCtrl.SetItemData (child_item, (DWORD)factory->Get_Class_ID ());
  180. }
  181. }
  182. //
  183. // Sort the entries we just made
  184. //
  185. m_TreeCtrl.SortChildren (tree_item);
  186. } else {
  187. m_TreeCtrl.SetItemData (tree_item, (DWORD)PRESET_CATEGORIES[index].clsid);
  188. }
  189. }
  190. return ;
  191. }