MixViewerView.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. // MixViewerView.cpp : implementation of the CMixViewerView class
  19. //
  20. #include "stdafx.h"
  21. #include "MixViewer.h"
  22. #include "mixviewerdoc.h"
  23. #include "mixviewerview.h"
  24. #include "ffactory.h"
  25. #include "mixfile.h"
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Local prototypes
  33. /////////////////////////////////////////////////////////////////////////////
  34. static int CALLBACK MixFilenamesListSortCallback (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CMixViewerView
  37. IMPLEMENT_DYNCREATE(CMixViewerView, CListView)
  38. BEGIN_MESSAGE_MAP(CMixViewerView, CListView)
  39. //{{AFX_MSG_MAP(CMixViewerView)
  40. ON_WM_CREATE()
  41. ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnDeleteitem)
  42. ON_WM_WINDOWPOSCHANGING()
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. //
  47. // CMixViewerView
  48. //
  49. /////////////////////////////////////////////////////////////////////////////
  50. CMixViewerView::CMixViewerView (void)
  51. {
  52. return ;
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. //
  56. // ~CMixViewerView
  57. //
  58. /////////////////////////////////////////////////////////////////////////////
  59. CMixViewerView::~CMixViewerView (void)
  60. {
  61. return ;
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. //
  65. // PreCreateWindow
  66. //
  67. /////////////////////////////////////////////////////////////////////////////
  68. BOOL
  69. CMixViewerView::PreCreateWindow (CREATESTRUCT &cs)
  70. {
  71. cs.style |= LVS_REPORT;
  72. return CListView::PreCreateWindow (cs);
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. //
  76. // OnDraw
  77. //
  78. /////////////////////////////////////////////////////////////////////////////
  79. void
  80. CMixViewerView::OnDraw (CDC *pDC)
  81. {
  82. CMixViewerDoc* pDoc = GetDocument();
  83. ASSERT_VALID(pDoc);
  84. return ;
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. //
  88. // OnInitialUpdate
  89. //
  90. /////////////////////////////////////////////////////////////////////////////
  91. void
  92. CMixViewerView::OnInitialUpdate (void)
  93. {
  94. CListView::OnInitialUpdate ();
  95. //
  96. // Size the column
  97. //
  98. CRect rect;
  99. GetListCtrl ().GetClientRect (&rect);
  100. rect.right -= ::GetSystemMetrics (SM_CXVSCROLL) + 2;
  101. GetListCtrl ().SetColumnWidth (0, rect.Width ());
  102. return ;
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CMixViewerView diagnostics
  106. #ifdef _DEBUG
  107. void CMixViewerView::AssertValid() const
  108. {
  109. CListView::AssertValid();
  110. }
  111. void CMixViewerView::Dump(CDumpContext& dc) const
  112. {
  113. CListView::Dump(dc);
  114. }
  115. CMixViewerDoc* CMixViewerView::GetDocument() // non-debug version is inline
  116. {
  117. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMixViewerDoc)));
  118. return (CMixViewerDoc*)m_pDocument;
  119. }
  120. #endif //_DEBUG
  121. /////////////////////////////////////////////////////////////////////////////
  122. //
  123. // Reload
  124. //
  125. /////////////////////////////////////////////////////////////////////////////
  126. void
  127. CMixViewerView::Reload (const char *filename)
  128. {
  129. CurrentFilename = filename;
  130. CMixViewerDoc *document = GetDocument ();
  131. if (document == NULL) {
  132. return ;
  133. }
  134. //
  135. // Start fresh
  136. //
  137. GetListCtrl ().DeleteAllItems ();
  138. //
  139. // Get the filename list from the mix file
  140. //
  141. MixFileFactoryClass mix_factory (filename, _TheFileFactory);
  142. if (mix_factory.Build_Filename_List (FilenameList)) {
  143. //
  144. // Add each filename to the list
  145. //
  146. for (int index = 0; index < FilenameList.Count (); index ++) {
  147. int item_index = GetListCtrl ().InsertItem (index, FilenameList[index]);
  148. if (item_index != -1) {
  149. GetListCtrl ().SetItemData (item_index, (DWORD)new StringClass (FilenameList[index]));
  150. }
  151. }
  152. //
  153. // Sort the data
  154. //
  155. GetListCtrl ().SortItems (MixFilenamesListSortCallback, 0);
  156. } else {
  157. //
  158. // Notify the user
  159. //
  160. StringClass message;
  161. message.Format ("Error reading the filename list from %s.", filename);
  162. MessageBox (message, "Mix File Error", MB_ICONERROR | MB_OK);
  163. }
  164. return ;
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. //
  168. // Reset
  169. //
  170. /////////////////////////////////////////////////////////////////////////////
  171. void
  172. CMixViewerView::Reset (void)
  173. {
  174. GetListCtrl ().DeleteAllItems ();
  175. FilenameList.Delete_All ();
  176. CurrentFilename = "";
  177. return ;
  178. }
  179. /////////////////////////////////////////////////////////////////////////////
  180. //
  181. // OnCreate
  182. //
  183. /////////////////////////////////////////////////////////////////////////////
  184. int
  185. CMixViewerView::OnCreate (LPCREATESTRUCT lpCreateStruct)
  186. {
  187. if (CListView::OnCreate (lpCreateStruct) == -1) {
  188. return -1;
  189. }
  190. //
  191. // Configure the list control
  192. //
  193. GetListCtrl ().SetExtendedStyle (GetListCtrl ().GetExtendedStyle () | LVS_EX_FULLROWSELECT);
  194. GetListCtrl ().InsertColumn (0, "Filename");
  195. return 0;
  196. }
  197. ////////////////////////////////////////////////////////////////////////////
  198. //
  199. // MixFilenamesListSortCallback
  200. //
  201. ////////////////////////////////////////////////////////////////////////////
  202. int CALLBACK
  203. MixFilenamesListSortCallback
  204. (
  205. LPARAM lParam1,
  206. LPARAM lParam2,
  207. LPARAM lParamSort
  208. )
  209. {
  210. int retval = 0;
  211. //
  212. // Get the data from list control
  213. //
  214. StringClass *item_data1 = (StringClass *)lParam1;
  215. StringClass *item_data2 = (StringClass *)lParam2;
  216. if (item_data1 != NULL && item_data2 != NULL) {
  217. bool is_1_dir = (::strrchr (*item_data1, '\\') != NULL);
  218. bool is_2_dir = (::strrchr (*item_data2, '\\') != NULL);
  219. if (is_1_dir && is_2_dir == false) {
  220. retval = -1;
  221. } else if (is_1_dir == false && is_2_dir) {
  222. retval = 1;
  223. } else {
  224. //
  225. // Do a simple string compare
  226. //
  227. retval = item_data1->Compare_No_Case (*item_data2);
  228. }
  229. }
  230. return retval;
  231. }
  232. /////////////////////////////////////////////////////////////////////////////
  233. //
  234. // OnDeleteitem
  235. //
  236. /////////////////////////////////////////////////////////////////////////////
  237. void
  238. CMixViewerView::OnDeleteitem (NMHDR *pNMHDR, LRESULT *pResult)
  239. {
  240. NM_LISTVIEW* pNMListView = (NM_LISTVIEW *)pNMHDR;
  241. *pResult = 0;
  242. //
  243. // Get the string associated with this entry
  244. //
  245. StringClass *string = (StringClass *)GetListCtrl ().GetItemData (pNMListView->iItem);
  246. GetListCtrl ().SetItemData (pNMListView->iItem, 0);
  247. //
  248. // Free the string
  249. //
  250. if (string != NULL) {
  251. delete string;
  252. string = NULL;
  253. }
  254. return ;
  255. }
  256. /////////////////////////////////////////////////////////////////////////////
  257. //
  258. // OnWindowPosChanging
  259. //
  260. /////////////////////////////////////////////////////////////////////////////
  261. void
  262. CMixViewerView::OnWindowPosChanging (WINDOWPOS FAR *lpwndpos)
  263. {
  264. if ((lpwndpos->flags & SWP_NOSIZE) == 0) {
  265. //
  266. // Get the current percent of the column width
  267. //
  268. CRect rect;
  269. GetListCtrl ().GetWindowRect (&rect);
  270. int curr_width = GetListCtrl ().GetColumnWidth (0);
  271. float percent = (float)curr_width / (float)rect.Width ();
  272. //
  273. // Size the column
  274. //
  275. int new_width = int(lpwndpos->cx * percent);
  276. GetListCtrl ().SetColumnWidth (0, new_width);
  277. }
  278. CListView::OnWindowPosChanging (lpwndpos);
  279. return ;
  280. }