DirectorySelect.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. ** Command & Conquer Generals(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. // FILE: DirectorySelect.cpp //////////////////////////////////////////////////
  19. //-----------------------------------------------------------------------------
  20. //
  21. // Westwood Studios Pacific.
  22. //
  23. // Confidential Information
  24. // Copyright (C) 2001 - All Rights Reserved
  25. //
  26. //-----------------------------------------------------------------------------
  27. //
  28. // Project: ImagePacker
  29. //
  30. // File name: DirectorySelect.cpp
  31. //
  32. // Created: Colin Day, August 2001
  33. //
  34. // Desc: Directory selection dialog
  35. //
  36. //-----------------------------------------------------------------------------
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  39. #include <windows.h>
  40. #include <stdio.h>
  41. // USER INCLUDES //////////////////////////////////////////////////////////////
  42. #include "ImagePacker.h"
  43. #include "Resource.h"
  44. // DEFINES ////////////////////////////////////////////////////////////////////
  45. // PRIVATE TYPES //////////////////////////////////////////////////////////////
  46. ///////////////////////////////////////////////////////////////////////////////
  47. // PRIVATE DATA ///////////////////////////////////////////////////////////////
  48. ///////////////////////////////////////////////////////////////////////////////
  49. static char startDir[ _MAX_PATH ];
  50. // PUBLIC DATA ////////////////////////////////////////////////////////////////
  51. // PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // selectDrive ================================================================
  56. /** Based on the drive of the current working directory, select the
  57. * drive item in the COMBO_DRIVE combo box matching the current
  58. * drive letter */
  59. //=============================================================================
  60. static void selectDrive( HWND dialog )
  61. {
  62. char currDir[ _MAX_PATH ];
  63. char drive;
  64. // get current directory
  65. GetCurrentDirectory( _MAX_PATH, currDir );
  66. // drive letter is the first character
  67. drive = currDir[ 0 ];
  68. // construct string to match in the combo
  69. char string[ 32 ];
  70. sprintf( string, "[-%c-]", drive );
  71. // select the string in the combo
  72. Int index;
  73. index = SendDlgItemMessage( dialog, COMBO_DRIVE,
  74. CB_FINDSTRINGEXACT, -1, (LPARAM)string );
  75. if( index != CB_ERR )
  76. SendDlgItemMessage( dialog, COMBO_DRIVE, CB_SETCURSEL, index, 0 );
  77. } // end selectDrive
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // DirectorySelectProc ========================================================
  82. /** Directory selection dialog */
  83. //=============================================================================
  84. BOOL CALLBACK DirectorySelectProc( HWND hWndDialog, UINT message,
  85. WPARAM wParam, LPARAM lParam )
  86. {
  87. static char buffer[ _MAX_PATH ];
  88. switch( message )
  89. {
  90. // ------------------------------------------------------------------------
  91. case WM_INITDIALOG:
  92. {
  93. // save our current directory as we start up
  94. GetCurrentDirectory( _MAX_PATH, startDir );
  95. // set the extents for the horizontal scroll bar in the listbox
  96. SendDlgItemMessage( hWndDialog, LIST_DIR,
  97. LB_SETHORIZONTALEXTENT, 1280, 0 );
  98. // load the listbox with directory items
  99. SendDlgItemMessage( hWndDialog, LIST_DIR,
  100. LB_DIR,
  101. DDL_DIRECTORY | DDL_EXCLUSIVE,
  102. (LPARAM)"*.*" );
  103. // set the current directory in the top label
  104. GetCurrentDirectory( _MAX_PATH, buffer );
  105. if( buffer[ strlen( buffer ) - 1 ] != '\\' )
  106. strcat( buffer, "\\" );
  107. SetDlgItemText( hWndDialog, STATIC_CURRENT_DIR, buffer );
  108. // load the drive box
  109. SendDlgItemMessage( hWndDialog, COMBO_DRIVE,
  110. CB_DIR,
  111. DDL_DRIVES | DDL_EXCLUSIVE,
  112. (LPARAM)"*.*" );
  113. // select the right drive in the drive combo box
  114. selectDrive( hWndDialog );
  115. return TRUE;
  116. } // end init
  117. // ------------------------------------------------------------------------
  118. case WM_COMMAND:
  119. {
  120. Int notifyCode = HIWORD( wParam );
  121. Int controlID = LOWORD( wParam );
  122. // HWND hWndControl = (HWND)lParam;
  123. switch( controlID )
  124. {
  125. // --------------------------------------------------------------------
  126. case BUTTON_ADD:
  127. {
  128. Int count;
  129. char text[ _MAX_PATH ];
  130. char toAdd[ _MAX_PATH ];
  131. // get number of items in listbox
  132. count = SendDlgItemMessage( hWndDialog, LIST_DIR, LB_GETCOUNT, 0, 0 );
  133. // for each selected item add that directory
  134. for( Int i = 0; i < count; i++ )
  135. {
  136. // is this item selected
  137. if( SendDlgItemMessage( hWndDialog, LIST_DIR, LB_GETSEL, i, 0 ) > 0 )
  138. {
  139. // get the text
  140. SendDlgItemMessage( hWndDialog, LIST_DIR, LB_GETTEXT,
  141. i, (LPARAM)text );
  142. // remove the brackets on the text
  143. Int j, len = strlen( text );
  144. for( j = 0; j < len - 1; j++ )
  145. text[ j ] = text[ j + 1 ];
  146. text[ len - 2 ] = '\0';
  147. // ignore the ".." directory
  148. if( strcmp( text, ".." ) == 0 )
  149. continue;
  150. //
  151. // construct directory to add, make sure we have a '\' on the
  152. // end cause we assume that in the image file name code
  153. //
  154. sprintf( toAdd, "%s%s\\", buffer, text );
  155. // do not add directory if it's already in the listbox
  156. if( SendDlgItemMessage( TheImagePacker->getWindowHandle(),
  157. LIST_FOLDERS,
  158. LB_FINDSTRINGEXACT, -1, (LPARAM)toAdd ) != LB_ERR )
  159. {
  160. char message[ _MAX_PATH + 32 ];
  161. sprintf( message, "Ignoring folder '%s', already in list.", toAdd );
  162. MessageBox( NULL, message, "Folder Already In List",
  163. MB_OK | MB_ICONINFORMATION );
  164. continue;
  165. } // end if
  166. // add path to the listbox
  167. SendDlgItemMessage( TheImagePacker->getWindowHandle(),
  168. LIST_FOLDERS,
  169. LB_INSERTSTRING, -1, (LPARAM)toAdd );
  170. } // end if
  171. } // end if
  172. SetCurrentDirectory( startDir );
  173. EndDialog( hWndDialog, TRUE );
  174. break;
  175. } // end proceed
  176. // --------------------------------------------------------------------
  177. case BUTTON_CANCEL:
  178. {
  179. SetCurrentDirectory( startDir );
  180. EndDialog( hWndDialog, FALSE );
  181. break;
  182. } // end cancel
  183. // --------------------------------------------------------------------
  184. case COMBO_DRIVE:
  185. {
  186. if( notifyCode == CBN_SELCHANGE )
  187. {
  188. Int selected;
  189. // get selected index
  190. selected = SendDlgItemMessage( hWndDialog, COMBO_DRIVE,
  191. CB_GETCURSEL, 0, 0 );
  192. if( selected != CB_ERR )
  193. {
  194. char string[ 32 ];
  195. SendDlgItemMessage( hWndDialog, COMBO_DRIVE,
  196. CB_GETLBTEXT, selected, (LPARAM)string );
  197. //
  198. // construct a drive letter and colon from the combo
  199. // box strign [-x-]
  200. //
  201. string[ 0 ] = string[ 2 ]; // the drive letter in the form of "[-x-]"
  202. string[ 1 ] = ':';
  203. string[ 2 ] = '\0';
  204. // switch to that drive
  205. SetCurrentDirectory( string );
  206. // construct new direcotry name and update status text
  207. GetCurrentDirectory( _MAX_PATH, buffer );
  208. if( buffer[ strlen( buffer ) - 1 ] != '\\' )
  209. strcat( buffer, "\\" );
  210. SetDlgItemText( hWndDialog, STATIC_CURRENT_DIR, buffer );
  211. EnableWindow( GetDlgItem( hWndDialog, BUTTON_ADD ), FALSE );
  212. // reset the content of the listbox and reload
  213. SendDlgItemMessage( hWndDialog, LIST_DIR,
  214. LB_RESETCONTENT, 0, 0 );
  215. SendDlgItemMessage( hWndDialog, LIST_DIR,
  216. LB_DIR,
  217. DDL_DIRECTORY | DDL_EXCLUSIVE,
  218. (LPARAM)"*.*" );
  219. } // end if
  220. } // end if
  221. break;
  222. } // end drive
  223. // --------------------------------------------------------------------
  224. case LIST_DIR:
  225. {
  226. if( notifyCode == LBN_SELCHANGE )
  227. {
  228. Int selCount;
  229. Bool enable;
  230. // get selected count
  231. selCount = SendDlgItemMessage( hWndDialog, LIST_DIR,
  232. LB_GETSELCOUNT, 0, 0 );
  233. // if we have selected items, enable the add button
  234. if( selCount > 0 )
  235. {
  236. //
  237. // if the selected item is only the ".." directory we won't
  238. // enable it, we know that _IF_ ".." is present it is at the
  239. // top zero index position
  240. //
  241. enable = TRUE;
  242. Int index = 0;
  243. if( selCount == 1 &&
  244. SendDlgItemMessage( hWndDialog, LIST_DIR,
  245. LB_GETSEL, index, 0 ) > 0 )
  246. {
  247. char text[ _MAX_PATH ];
  248. SendDlgItemMessage( hWndDialog, LIST_DIR, LB_GETTEXT,
  249. index, (LPARAM)text );
  250. if( strcmp( text, "[..]" ) == 0 )
  251. enable = FALSE;
  252. } // end if
  253. } // end if
  254. else
  255. enable = FALSE;
  256. // do the enable
  257. EnableWindow( GetDlgItem( hWndDialog, BUTTON_ADD ), enable );
  258. } // end if
  259. if( notifyCode == LBN_DBLCLK )
  260. {
  261. Int selected;
  262. char text[ _MAX_PATH ];
  263. // get currently selected item
  264. selected = SendDlgItemMessage( hWndDialog, LIST_DIR,
  265. LB_GETCURSEL, 0, 0 );
  266. // get text of selected item
  267. SendDlgItemMessage( hWndDialog, LIST_DIR, LB_GETTEXT,
  268. selected, (LPARAM)text );
  269. // strip the backets off the directory listing
  270. Int len = strlen( text );
  271. for( Int i = 0; i < len - 1; i++ )
  272. text[ i ] = text[ i + 1 ];
  273. text[ len - 2 ] = '\0';
  274. // go into that directory
  275. SetCurrentDirectory( text );
  276. // construct new direcotry name and update status text
  277. GetCurrentDirectory( _MAX_PATH, buffer );
  278. if( buffer[ strlen( buffer ) - 1 ] != '\\' )
  279. strcat( buffer, "\\" );
  280. SetDlgItemText( hWndDialog, STATIC_CURRENT_DIR, buffer );
  281. EnableWindow( GetDlgItem( hWndDialog, BUTTON_ADD ), FALSE );
  282. // reset the content of the listbox and reload
  283. SendDlgItemMessage( hWndDialog, LIST_DIR,
  284. LB_RESETCONTENT, 0, 0 );
  285. SendDlgItemMessage( hWndDialog, LIST_DIR,
  286. LB_DIR,
  287. DDL_DIRECTORY | DDL_EXCLUSIVE,
  288. (LPARAM)"*.*" );
  289. } // end if
  290. break;
  291. } // end list command
  292. } // end switch
  293. break;
  294. } // end command
  295. } // end switch message
  296. return 0;
  297. } // end DirectorySelectProc