Removed the A/W constants for builtin cursors, icons and resource
[wine/multimedia.git] / programs / winecfg / drive.c
blob726ac258e48ad08c568299ea183b7a588a260c42
1 /*
2 * Drive management UI code
4 * Copyright 2003 Mark Westcott
5 * Copyright 2003 Mike Hearn
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <windef.h>
30 #include <winbase.h>
31 #include <winreg.h>
32 #include <wine/debug.h>
33 #include <shellapi.h>
34 #include <objbase.h>
35 #include <shlguid.h>
36 #include <shlwapi.h>
37 #include <shlobj.h>
39 #include "winecfg.h"
40 #include "resource.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
44 static BOOL updatingUI = FALSE;
45 static char editWindowLetter; /* drive letter of the drive we are currently editing */
48 /* returns NULL on failure. caller is responsible for freeing result */
49 char *getDriveValue(char letter, char *valueName) {
50 HKEY hkDrive = 0;
51 char *subKeyName;
52 char *result = NULL;
53 HRESULT hr;
54 DWORD bufferSize;
56 WINE_TRACE("letter=%c, valueName=%s\n", letter, valueName);
58 subKeyName = malloc(strlen("Drive X")+1);
59 sprintf(subKeyName, "Drive %c", letter);
61 hr = RegOpenKeyEx(configKey, subKeyName, 0, KEY_READ, &hkDrive);
62 if (hr != ERROR_SUCCESS) goto end;
64 hr = RegQueryValueEx(hkDrive, valueName, NULL, NULL, NULL, &bufferSize);
65 if (hr != ERROR_SUCCESS) goto end;
67 result = malloc(bufferSize);
68 hr = RegQueryValueEx(hkDrive, valueName, NULL, NULL, result, &bufferSize);
69 if (hr != ERROR_SUCCESS) goto end;
71 end:
72 if (hkDrive) RegCloseKey(hkDrive);
73 free(subKeyName);
74 return result;
77 void setDriveValue(char letter, char *valueName, char *newValue) {
78 char *driveSection = malloc(strlen("Drive X")+1);
79 sprintf(driveSection, "Drive %c", letter);
80 addTransaction(driveSection, valueName, ACTION_SET, newValue);
81 free(driveSection);
84 void refreshDriveDlg (HWND hDlg)
86 int i;
87 char *subKeyName = malloc(MAX_NAME_LENGTH);
88 int driveCount = 0;
89 DWORD sizeOfSubKeyName;
91 WINE_TRACE("\n");
93 updatingUI = TRUE;
95 /* Clear the listbox */
96 SendMessageA(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_RESETCONTENT, 0, 0);
97 for (i = 0;
98 RegEnumKeyExA(configKey, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS;
99 ++i, sizeOfSubKeyName = 50) {
101 HKEY hkDrive;
102 char returnBuffer[MAX_NAME_LENGTH];
103 DWORD sizeOfReturnBuffer = sizeof(returnBuffer);
104 LONG r;
106 if (!strncmp("Drive ", subKeyName, 5)) {
107 char driveLetter = '\0';
108 char *label;
109 char *title;
110 int titleLen;
111 const char *itemLabel = "Drive %s (%s)";
112 int itemIndex;
114 if (RegOpenKeyExA (configKey, subKeyName, 0, KEY_READ, &hkDrive) != ERROR_SUCCESS) {
115 WINE_ERR("unable to open drive registry key");
116 RegCloseKey(configKey);
117 return;
120 /* extract the drive letter, force to upper case */
121 driveLetter = subKeyName[strlen(subKeyName)-1];
122 if(driveLetter) driveLetter = toupper(driveLetter);
124 ZeroMemory(returnBuffer, sizeof(*returnBuffer));
125 sizeOfReturnBuffer = sizeof(returnBuffer);
126 r = RegQueryValueExA(hkDrive, "Label", NULL, NULL, returnBuffer, &sizeOfReturnBuffer);
127 if (r == ERROR_SUCCESS) {
128 label = malloc(sizeOfReturnBuffer);
129 strncpy(label, returnBuffer, sizeOfReturnBuffer);
130 } else {
131 WINE_WARN("label not loaded: %ld\n", r);
132 label = NULL;
135 /* We now know the label and drive letter, so we can add to the list. The list items will have the letter associated
136 * with them, which acts as the key. We can then use that letter to get/set the properties of the drive. */
137 WINE_TRACE("Adding %c (%s) to the listbox\n", driveLetter, label);
139 if (!label) label = "no label";
140 titleLen = strlen(itemLabel) - 1 + strlen(label) - 2 + 1;
141 title = malloc(titleLen);
142 /* the %s in the item label will be replaced by the drive letter, so -1, then
143 -2 for the second %s which will be expanded to the label, finally + 1 for terminating #0 */
144 snprintf(title, titleLen, "Drive %c (%s)", driveLetter, label);
146 /* the first SendMessage call adds the string and returns the index, the second associates that index with it */
147 itemIndex = SendMessageA(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_ADDSTRING ,(WPARAM) -1, (LPARAM) title);
148 SendMessageA(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_SETITEMDATA, itemIndex, (LPARAM) driveLetter);
150 free(title);
151 if (label && (strcmp(label, "no label") != 0)) free(label);
153 driveCount++;
158 WINE_TRACE("loaded %d drives\n", driveCount);
159 SendMessageA(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_SETSEL, TRUE, 0);
161 free(subKeyName);
162 updatingUI = FALSE;
163 return;
166 /******************************************************************************/
167 /* The Drive Editing Dialog */
168 /******************************************************************************/
169 #define DRIVE_MASK_BIT(B) 1<<(toupper(B)-'A')
171 typedef struct{
172 char *sCode;
173 char *sDesc;
174 } code_desc_pair;
176 static code_desc_pair type_pairs[] = {
177 {"hd", "Local hard disk"},
178 {"network", "Network share" },
179 {"floppy", "Floppy disk"},
180 {"cdrom", "CD-ROM"}
182 #define DRIVE_TYPE_DEFAULT 1
184 static code_desc_pair fs_pairs[] = {
185 {"win95", "Long file names"},
186 {"msdos", "MS-DOS 8 character file names"},
187 {"unix", "UNIX file names"}
190 #define DRIVE_FS_DEFAULT 0
193 void fill_drive_droplist(long mask, char currentLetter, HWND hDlg)
195 int i;
196 int selection;
197 int count;
198 int next_letter;
199 char sName[4] = "A:";
201 for( i=0, count=0, selection=-1, next_letter=-1; i <= 'Z'-'A'; ++i ) {
202 if( mask & DRIVE_MASK_BIT('A'+i) ) {
203 sName[0] = 'A' + i;
204 SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName );
206 if( toupper(currentLetter) == 'A' + i ) {
207 selection = count;
210 if( i >= 2 && next_letter == -1){ /*default drive is first one of C-Z */
211 next_letter = count;
214 count++;
218 if( selection == -1 ) {
219 selection = next_letter;
222 SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0 );
225 /* if bEnable is 1 then we are editing a CDROM, so can enable all controls, otherwise we want to disable
226 * "detect from device" and "serial number", but still allow the user to manually set the path. The UI
227 * for this could be somewhat better -mike
229 void enable_labelserial_box(HWND hDlg, int bEnable)
231 EnableWindow( GetDlgItem( hDlg, IDC_RADIO_AUTODETECT ), bEnable );
232 EnableWindow( GetDlgItem( hDlg, IDC_EDIT_DEVICE ), bEnable );
233 EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_BROWSE_DEVICE ), bEnable );
234 EnableWindow( GetDlgItem( hDlg, IDC_EDIT_SERIAL ), bEnable );
235 EnableWindow( GetDlgItem( hDlg, IDC_STATIC_SERIAL ), bEnable );
236 EnableWindow( GetDlgItem( hDlg, IDC_STATIC_LABEL ), bEnable );
239 /* This function produces a mask for each drive letter that isn't currently used. Each bit of the long result
240 * represents a letter, with A being the least significant bit, and Z being the most significant.
242 * To calculate this, we loop over each letter, and see if we can get a drive entry for it. If so, we
243 * set the appropriate bit. At the end, we flip each bit, to give the desired result.
245 * The letter parameter is always marked as being available. This is so the edit dialog can display the
246 * currently used drive letter alongside the available ones.
248 long drive_available_mask(char letter)
250 long result = 0;
251 char curLetter;
252 char *slop;
254 WINE_TRACE("\n");
256 for (curLetter = 'A'; curLetter < 'Z'; curLetter++) {
257 slop = getDriveValue(curLetter, "Path");
258 if (slop != NULL) {
259 result |= DRIVE_MASK_BIT(curLetter);
260 free(slop);
264 result = ~result;
265 if (letter) result |= DRIVE_MASK_BIT(letter);
267 WINE_TRACE( "finished drive letter loop with %lx\n", result );
268 return result;
272 void refreshDriveEditDialog(HWND hDlg) {
273 char *path;
274 char *type;
275 char *fs;
276 char *serial;
277 char *label;
278 char *device;
279 int i, selection;
281 updatingUI = TRUE;
283 /* Drive letters */
284 fill_drive_droplist( drive_available_mask( editWindowLetter ), editWindowLetter, hDlg );
286 /* path */
287 path = getDriveValue(editWindowLetter, "Path");
288 if (path) {
289 SetWindowText(GetDlgItem(hDlg, IDC_EDIT_PATH), path);
290 } else WINE_WARN("no Path field?\n");
292 /* drive type */
293 type = getDriveValue(editWindowLetter, "Type");
294 if (type) {
295 for(i = 0, selection = -1; i < sizeof(type_pairs)/sizeof(code_desc_pair); i++) {
296 SendDlgItemMessage(hDlg, IDC_COMBO_TYPE, CB_ADDSTRING, 0,
297 (LPARAM) type_pairs[i].sDesc);
298 if(strcasecmp(type_pairs[i].sCode, type) == 0){
299 selection = i;
303 if( selection == -1 ) selection = DRIVE_TYPE_DEFAULT;
304 SendDlgItemMessage(hDlg, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
305 } else WINE_WARN("no Type field?\n");
308 /* FileSystem name handling */
309 fs = getDriveValue(editWindowLetter, "FileSystem");
310 if (fs) {
311 for( i=0, selection=-1; i < sizeof(fs_pairs)/sizeof(code_desc_pair); i++) {
312 SendDlgItemMessage(hDlg, IDC_COMBO_NAMES, CB_ADDSTRING, 0,
313 (LPARAM) fs_pairs[i].sDesc);
314 if(strcasecmp(fs_pairs[i].sCode, fs) == 0){
315 selection = i;
319 if( selection == -1 ) selection = DRIVE_FS_DEFAULT;
320 SendDlgItemMessage(hDlg, IDC_COMBO_NAMES, CB_SETCURSEL, selection, 0);
321 } else WINE_WARN("no FileSystem field?\n");
324 /* removeable media properties */
325 serial = getDriveValue(editWindowLetter, "Serial");
326 if (serial) {
327 SendDlgItemMessage(hDlg, IDC_EDIT_SERIAL, WM_SETTEXT, 0,(LPARAM)serial);
328 } else WINE_WARN("no Serial field?\n");
330 label = getDriveValue(editWindowLetter, "Label");
331 if (label) {
332 SendDlgItemMessage(hDlg, IDC_EDIT_LABEL, WM_SETTEXT, 0,(LPARAM)label);
333 } else WINE_WARN("no Label field?\n");
335 device = getDriveValue(editWindowLetter, "Device");
336 if (device) {
337 SendDlgItemMessage(hDlg, IDC_EDIT_DEVICE, WM_SETTEXT, 0,(LPARAM)device);
338 } else WINE_WARN("no Device field?\n");
340 selection = IDC_RADIO_ASSIGN;
341 if ((type && strcmp("cdrom", type) == 0) ||
342 (type && strcmp("floppy", type) == 0)) {
344 if( (type && (strlen( device ) == 0)) &&
345 ((serial && strlen( serial ) > 0) || (label && strlen( label ) > 0)) ) {
346 selection = IDC_RADIO_ASSIGN;
348 else {
349 selection = IDC_RADIO_AUTODETECT;
352 enable_labelserial_box( hDlg, 1 );
354 else {
355 enable_labelserial_box( hDlg, 0 );
356 selection = IDC_RADIO_ASSIGN;
359 CheckRadioButton( hDlg, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection );
360 if (path) SendDlgItemMessage(hDlg, IDC_EDIT_PATH, WM_SETTEXT, 0,(LPARAM)path);
362 if (path) free(path);
363 if (type) free(type);
364 if (fs) free(fs);
365 if (serial) free(serial);
366 if (label) free(label);
367 if (device) free(device);
369 updatingUI = FALSE;
371 return;
374 /* storing the drive propsheet HWND here is a bit ugly, but the simplest solution for now */
375 static HWND driveDlgHandle;
377 void onEditChanged(HWND hDlg, WORD controlID) {
378 WINE_TRACE("controlID=%d\n", controlID);
379 switch (controlID) {
380 case IDC_EDIT_LABEL: { /* drive label edit box */
381 char *label = getDialogItemText(hDlg, controlID);
382 setDriveValue(editWindowLetter, "Label", label);
383 refreshDriveDlg(driveDlgHandle);
384 free(label);
385 break;
390 INT_PTR CALLBACK DriveEditDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
392 int selection;
394 switch (uMsg) {
395 case WM_INITDIALOG: {
396 editWindowLetter = (char) lParam;
397 refreshDriveEditDialog(hDlg);
400 case WM_COMMAND:
401 switch (LOWORD(wParam)) {
402 case IDC_COMBO_TYPE:
403 switch( HIWORD(wParam)) {
404 case CBN_SELCHANGE:
405 selection = SendDlgItemMessage( hDlg, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
406 if( selection == 2 || selection == 3 ) { /* cdrom or floppy */
407 enable_labelserial_box( hDlg, 1 );
409 else {
410 enable_labelserial_box( hDlg, 0 );
412 break;
414 break;
416 case ID_BUTTON_OK:
417 case ID_BUTTON_CANCEL:
418 EndDialog(hDlg, wParam);
419 return TRUE;
421 if (HIWORD(wParam) == EN_CHANGE) onEditChanged(hDlg, LOWORD(wParam));
422 break;
424 return FALSE;
427 void onAddDriveClicked(HWND hDlg) {
428 /* we should allocate a drive letter automatically. We also need some way to let the user choose the mapping point,
429 for now we will just force them to enter a path automatically, with / being the default. In future we should
430 be able to temporarily map / then invoke the directory chooser dialog. */
432 char newLetter = 'D'; /* we skip A, B and of course C is already mapped, right? */
433 long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
434 char *sectionName;
436 while (mask & (1 << (newLetter - 'A'))) {
437 newLetter++;
438 if (newLetter > 'Z') {
439 MessageBox(NULL, "You cannot add any more drives.\n\nEach drive must have a letter, from A to Z, so you cannot have more than 26", "", MB_OK | MB_ICONEXCLAMATION);
440 return;
443 WINE_TRACE("allocating drive letter %c\n", newLetter);
445 sectionName = malloc(strlen("Drive X") + 1);
446 sprintf(sectionName, "Drive %c", newLetter);
447 addTransaction(sectionName, "Path", ACTION_SET, "/"); /* default to root path */
448 addTransaction(sectionName, "Type", ACTION_SET, "hd");
449 processTransQueue(); /* make sure the drive has been added, even if we are not in instant apply mode */
450 free(sectionName);
452 refreshDriveDlg(driveDlgHandle);
454 DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT2), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) newLetter);
458 INT_PTR CALLBACK
459 DriveDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
461 int selection = -1;
463 switch (uMsg) {
464 case WM_COMMAND:
465 switch (LOWORD(wParam)) {
466 case IDC_LIST_DRIVES:
467 switch(HIWORD( wParam )) {
468 case LBN_DBLCLK:
469 selection = -1;
470 break;
472 break;
474 case IDC_BUTTON_ADD:
475 onAddDriveClicked(hDlg);
476 break;
478 case IDC_BUTTON_REMOVE:
479 /* temporarily disabled, awaiting rewrite */
480 break;
482 case IDC_BUTTON_EDIT:
483 if (HIWORD(wParam) == BN_CLICKED) {
484 int nItem = SendMessage(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_GETCURSEL, 0, 0);
485 char letter = SendMessage(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_GETITEMDATA, nItem, 0);
487 DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT2), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) letter);
489 break;
492 break;
494 case WM_NOTIFY: switch(((LPNMHDR)lParam)->code) {
495 case PSN_KILLACTIVE:
496 SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
497 break;
498 case PSN_APPLY:
499 SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
500 break;
501 case PSN_SETACTIVE:
502 driveDlgHandle = hDlg;
503 refreshDriveDlg (driveDlgHandle);
504 break;
506 break;
510 return FALSE;