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
32 #include <wine/debug.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 */
46 static int lastSel = 0; /* the last drive selected in the property sheet */
49 /* returns NULL on failure. caller is responsible for freeing result */
50 char *getDriveValue(char letter, const char *valueName) {
57 WINE_TRACE("letter=%c, valueName=%s\n", letter, valueName);
59 subKeyName = malloc(strlen("Drive X")+1);
60 sprintf(subKeyName, "Drive %c", letter);
62 hr = RegOpenKeyEx(configKey, subKeyName, 0, KEY_READ, &hkDrive);
63 if (hr != ERROR_SUCCESS) goto end;
65 hr = RegQueryValueEx(hkDrive, valueName, NULL, NULL, NULL, &bufferSize);
66 if (hr != ERROR_SUCCESS) goto end;
68 result = malloc(bufferSize);
69 hr = RegQueryValueEx(hkDrive, valueName, NULL, NULL, result, &bufferSize);
70 if (hr != ERROR_SUCCESS) goto end;
73 if (hkDrive) RegCloseKey(hkDrive);
78 /* call with newValue == NULL to remove a value */
79 void setDriveValue(char letter, const char *valueName, const char *newValue) {
80 char *driveSection = malloc(strlen("Drive X")+1);
81 sprintf(driveSection, "Drive %c", letter);
83 addTransaction(driveSection, valueName, ACTION_SET, newValue);
85 addTransaction(driveSection, valueName, ACTION_REMOVE, NULL);
89 /* copies a drive configuration branch */
90 void copyDrive(char srcLetter, char destLetter) {
91 char driveSection[sizeof("Drive X")];
92 char *path, *label, *type, *serial, *fs;
94 WINE_TRACE("srcLetter=%c, destLetter=%c\n", srcLetter, destLetter);
96 sprintf(driveSection, "Drive %c", srcLetter);
97 path = getDriveValue(srcLetter, "Path");
98 label = getDriveValue(srcLetter, "Label");
99 type = getDriveValue(srcLetter, "Type");
100 serial = getDriveValue(srcLetter, "Serial");
101 fs = getDriveValue(srcLetter, "FileSystem");
103 sprintf(driveSection, "Drive %c", destLetter);
104 if (path) addTransaction(driveSection, "Path", ACTION_SET, path);
105 if (label) addTransaction(driveSection, "Label", ACTION_SET, label);
106 if (type) addTransaction(driveSection, "Type", ACTION_SET, type);
107 if (serial) addTransaction(driveSection, "Serial", ACTION_SET, serial);
108 if (fs) addTransaction(driveSection, "FileSystem", ACTION_SET, fs);
110 if (path) free(path);
111 if (label) free(label);
112 if (type) free(type);
113 if (serial) free(serial);
117 void removeDrive(char letter) {
118 char driveSection[sizeof("Drive X")];
119 sprintf(driveSection, "Drive %c", letter);
120 addTransaction(driveSection, NULL, ACTION_REMOVE, NULL);
123 int refreshDriveDlg (HWND dialog)
126 char *subKeyName = malloc(MAX_NAME_LENGTH);
128 DWORD sizeOfSubKeyName = MAX_NAME_LENGTH;
129 int doesDriveCExist = FALSE;
135 /* Clear the listbox */
136 SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_RESETCONTENT, 0, 0);
138 RegEnumKeyExA(configKey, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS;
139 ++i, sizeOfSubKeyName = MAX_NAME_LENGTH) {
142 char returnBuffer[MAX_NAME_LENGTH];
143 DWORD sizeOfReturnBuffer = sizeof(returnBuffer);
145 WINE_TRACE("%s\n", subKeyName);
147 if (!strncmp("Drive ", subKeyName, 5)) {
148 char driveLetter = '\0';
153 const char *itemLabel = "Drive %s (%s)";
156 if (RegOpenKeyExA (configKey, subKeyName, 0, KEY_READ, &hkDrive) != ERROR_SUCCESS) {
157 WINE_ERR("unable to open drive registry key");
158 RegCloseKey(configKey);
162 /* extract the drive letter, force to upper case */
163 driveLetter = subKeyName[strlen(subKeyName)-1];
164 if (driveLetter) driveLetter = toupper(driveLetter);
165 if (driveLetter == 'C') doesDriveCExist = TRUE;
167 ZeroMemory(returnBuffer, sizeof(*returnBuffer));
168 sizeOfReturnBuffer = sizeof(returnBuffer);
169 r = RegQueryValueExA(hkDrive, "Label", NULL, NULL, returnBuffer, &sizeOfReturnBuffer);
170 if (r == ERROR_SUCCESS) {
171 label = malloc(sizeOfReturnBuffer);
172 strncpy(label, returnBuffer, sizeOfReturnBuffer);
174 WINE_WARN("label not loaded: %ld\n", r);
178 device = getDriveValue(driveLetter, "Device");
180 /* We now know the label and drive letter, so we can add to the list. The list items will have the letter associated
181 * with them, which acts as the key. We can then use that letter to get/set the properties of the drive. */
182 WINE_TRACE("Adding %c: label=%s to the listbox, device=%s\n", driveLetter, label, device);
185 if (!label && device) {
186 label = malloc(strlen("[label read from device ]")+1+strlen(device));
187 sprintf(label, "[label read from device %s]", device);
189 if (!label) label = strdup("(no label)");
191 titleLen = strlen(itemLabel) - 1 + strlen(label) - 2 + 1;
192 title = malloc(titleLen);
193 /* the %s in the item label will be replaced by the drive letter, so -1, then
194 -2 for the second %s which will be expanded to the label, finally + 1 for terminating #0 */
195 snprintf(title, titleLen, "Drive %c: %s", driveLetter, label);
197 /* the first SendMessage call adds the string and returns the index, the second associates that index with it */
198 itemIndex = SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_ADDSTRING ,(WPARAM) -1, (LPARAM) title);
199 SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_SETITEMDATA, itemIndex, (LPARAM) driveLetter);
209 WINE_TRACE("loaded %d drives\n", driveCount);
210 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LB_SETSEL, TRUE, lastSel);
212 /* show the warning if there is no Drive C */
213 if (!doesDriveCExist)
214 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
216 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
220 /* disable or enable controls depending on whether we are editing global vs app specific config */
221 if (appSettings == EDITING_GLOBAL) {
222 WINE_TRACE("enabling controls\n");
223 enable(IDC_LIST_DRIVES);
224 enable(IDC_BUTTON_ADD);
225 enable(IDC_BUTTON_REMOVE);
226 enable(IDC_BUTTON_EDIT);
227 enable(IDC_BUTTON_AUTODETECT);
230 WINE_TRACE("disabling controls\n");
231 disable(IDC_LIST_DRIVES);
232 disable(IDC_BUTTON_ADD);
233 disable(IDC_BUTTON_REMOVE);
234 disable(IDC_BUTTON_EDIT);
235 disable(IDC_BUTTON_AUTODETECT);
243 /******************************************************************************/
244 /* The Drive Editing Dialog */
245 /******************************************************************************/
246 #define DRIVE_MASK_BIT(B) 1<<(toupper(B)-'A')
253 static code_desc_pair type_pairs[] = {
254 {"hd", "Local hard disk"},
255 {"network", "Network share" },
256 {"floppy", "Floppy disk"},
259 #define DRIVE_TYPE_DEFAULT 1
261 static code_desc_pair fs_pairs[] = {
262 {"win95", "Long file names"},
263 {"msdos", "MS-DOS 8 character file names"},
264 {"unix", "UNIX file names"}
267 #define DRIVE_FS_DEFAULT 0
270 void fill_drive_droplist(long mask, char currentLetter, HWND hDlg)
276 char sName[4] = "A:";
278 for( i=0, count=0, selection=-1, next_letter=-1; i <= 'Z'-'A'; ++i ) {
279 if( mask & DRIVE_MASK_BIT('A'+i) ) {
283 index = SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName );
285 if( toupper(currentLetter) == 'A' + i ) {
289 if( i >= 2 && next_letter == -1){ /*default drive is first one of C-Z */
297 if( selection == -1 ) {
298 selection = next_letter;
301 SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0 );
304 #define BOX_MODE_CD_ASSIGN 1
305 #define BOX_MODE_CD_AUTODETECT 2
306 #define BOX_MODE_NONE 3
307 #define BOX_MODE_NORMAL 4
308 void enable_labelserial_box(HWND dialog, int mode)
310 WINE_TRACE("mode=%d\n", mode);
312 case BOX_MODE_CD_ASSIGN:
313 enable(IDC_RADIO_AUTODETECT);
314 enable(IDC_RADIO_ASSIGN);
315 disable(IDC_EDIT_DEVICE);
316 disable(IDC_BUTTON_BROWSE_DEVICE);
317 enable(IDC_EDIT_SERIAL);
318 enable(IDC_EDIT_LABEL);
319 enable(IDC_STATIC_SERIAL);
320 enable(IDC_STATIC_LABEL);
323 case BOX_MODE_CD_AUTODETECT:
324 enable(IDC_RADIO_AUTODETECT);
325 enable(IDC_RADIO_ASSIGN);
326 enable(IDC_EDIT_DEVICE);
327 enable(IDC_BUTTON_BROWSE_DEVICE);
328 disable(IDC_EDIT_SERIAL);
329 disable(IDC_EDIT_LABEL);
330 disable(IDC_STATIC_SERIAL);
331 disable(IDC_STATIC_LABEL);
335 disable(IDC_RADIO_AUTODETECT);
336 disable(IDC_RADIO_ASSIGN);
337 disable(IDC_EDIT_DEVICE);
338 disable(IDC_BUTTON_BROWSE_DEVICE);
339 disable(IDC_EDIT_SERIAL);
340 disable(IDC_EDIT_LABEL);
341 disable(IDC_STATIC_SERIAL);
342 disable(IDC_STATIC_LABEL);
345 case BOX_MODE_NORMAL:
346 disable(IDC_RADIO_AUTODETECT);
347 enable(IDC_RADIO_ASSIGN);
348 disable(IDC_EDIT_DEVICE);
349 disable(IDC_BUTTON_BROWSE_DEVICE);
350 enable(IDC_EDIT_SERIAL);
351 enable(IDC_EDIT_LABEL);
352 enable(IDC_STATIC_SERIAL);
353 enable(IDC_STATIC_LABEL);
358 /* This function produces a mask for each drive letter that isn't currently used. Each bit of the long result
359 * represents a letter, with A being the least significant bit, and Z being the most significant.
361 * To calculate this, we loop over each letter, and see if we can get a drive entry for it. If so, we
362 * set the appropriate bit. At the end, we flip each bit, to give the desired result.
364 * The letter parameter is always marked as being available. This is so the edit dialog can display the
365 * currently used drive letter alongside the available ones.
367 long drive_available_mask(char letter)
375 for (curLetter = 'A'; curLetter < 'Z'; curLetter++) {
376 slop = getDriveValue(curLetter, "Path");
378 result |= DRIVE_MASK_BIT(curLetter);
384 if (letter) result |= DRIVE_MASK_BIT(letter);
386 WINE_TRACE( "finished drive letter loop with %lx\n", result );
391 void refreshDriveEditDialog(HWND dialog) {
403 fill_drive_droplist( drive_available_mask( editWindowLetter ), editWindowLetter, dialog );
406 path = getDriveValue(editWindowLetter, "Path");
408 SetWindowText(GetDlgItem(dialog, IDC_EDIT_PATH), path);
409 } else WINE_WARN("no Path field?\n");
412 type = getDriveValue(editWindowLetter, "Type");
414 for(i = 0, selection = -1; i < sizeof(type_pairs)/sizeof(code_desc_pair); i++) {
415 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0,
416 (LPARAM) type_pairs[i].sDesc);
417 if(strcasecmp(type_pairs[i].sCode, type) == 0){
422 if( selection == -1 ) selection = DRIVE_TYPE_DEFAULT;
423 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
424 } else WINE_WARN("no Type field?\n");
427 /* FileSystem name handling */
428 fs = getDriveValue(editWindowLetter, "FileSystem");
430 for( i=0, selection=-1; i < sizeof(fs_pairs)/sizeof(code_desc_pair); i++) {
431 SendDlgItemMessage(dialog, IDC_COMBO_NAMES, CB_ADDSTRING, 0,
432 (LPARAM) fs_pairs[i].sDesc);
433 if(strcasecmp(fs_pairs[i].sCode, fs) == 0){
438 if( selection == -1 ) selection = DRIVE_FS_DEFAULT;
439 SendDlgItemMessage(dialog, IDC_COMBO_NAMES, CB_SETCURSEL, selection, 0);
440 } else WINE_WARN("no FileSystem field?\n");
443 /* removeable media properties */
444 serial = getDriveValue(editWindowLetter, "Serial");
446 SendDlgItemMessage(dialog, IDC_EDIT_SERIAL, WM_SETTEXT, 0,(LPARAM)serial);
447 } else WINE_WARN("no Serial field?\n");
449 label = getDriveValue(editWindowLetter, "Label");
451 SendDlgItemMessage(dialog, IDC_EDIT_LABEL, WM_SETTEXT, 0,(LPARAM)label);
452 } else WINE_WARN("no Label field?\n");
454 device = getDriveValue(editWindowLetter, "Device");
456 SendDlgItemMessage(dialog, IDC_EDIT_DEVICE, WM_SETTEXT, 0,(LPARAM)device);
457 } else WINE_WARN("no Device field?\n");
459 selection = IDC_RADIO_ASSIGN;
460 if ((type && strcmp("cdrom", type) == 0) || (type && strcmp("floppy", type) == 0)) {
462 selection = IDC_RADIO_AUTODETECT;
463 enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
465 selection = IDC_RADIO_ASSIGN;
466 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
469 enable_labelserial_box(dialog, BOX_MODE_NORMAL);
470 selection = IDC_RADIO_ASSIGN;
473 CheckRadioButton( dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection );
474 if (path) SendDlgItemMessage(dialog, IDC_EDIT_PATH, WM_SETTEXT, 0,(LPARAM)path);
476 if (path) free(path);
477 if (type) free(type);
479 if (serial) free(serial);
480 if (label) free(label);
481 if (device) free(device);
489 /* storing the drive propsheet HWND here is a bit ugly, but the simplest solution for now */
490 static HWND driveDlgHandle;
492 void onEditChanged(HWND hDlg, WORD controlID) {
493 WINE_TRACE("controlID=%d\n", controlID);
495 case IDC_EDIT_LABEL: {
496 char *label = getDialogItemText(hDlg, controlID);
497 setDriveValue(editWindowLetter, "Label", label);
498 refreshDriveDlg(driveDlgHandle);
499 if (label) free(label);
502 case IDC_EDIT_PATH: {
503 char *path = getDialogItemText(hDlg, controlID);
504 if (!path) path = strdup("fake_windows"); /* default to assuming fake_windows in the .wine directory */
505 setDriveValue(editWindowLetter, "Path", path);
509 case IDC_EDIT_SERIAL: {
510 char *serial = getDialogItemText(hDlg, controlID);
511 setDriveValue(editWindowLetter, "Serial", serial);
512 if (serial) free (serial);
515 case IDC_EDIT_DEVICE: {
516 char *device = getDialogItemText(hDlg,controlID);
517 setDriveValue(editWindowLetter, "Device", device);
518 if (device) free(device);
519 refreshDriveDlg(driveDlgHandle);
525 INT_PTR CALLBACK DriveEditDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
530 case WM_INITDIALOG: {
531 editWindowLetter = (char) lParam;
532 refreshDriveEditDialog(hDlg);
536 switch (LOWORD(wParam)) {
538 if (HIWORD(wParam) != CBN_SELCHANGE) break;
539 selection = SendDlgItemMessage( hDlg, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
540 if( selection == 2 || selection == 3 ) { /* cdrom or floppy */
541 if (IsDlgButtonChecked(hDlg, IDC_RADIO_AUTODETECT))
542 enable_labelserial_box(hDlg, BOX_MODE_CD_AUTODETECT);
544 enable_labelserial_box(hDlg, BOX_MODE_CD_ASSIGN);
547 enable_labelserial_box( hDlg, BOX_MODE_NORMAL );
549 setDriveValue(editWindowLetter, "Type", type_pairs[selection].sCode);
552 case IDC_COMBO_LETTER: {
553 int item = SendDlgItemMessage(hDlg, IDC_COMBO_LETTER, CB_GETCURSEL, 0, 0);
555 SendDlgItemMessage(hDlg, IDC_COMBO_LETTER, CB_GETLBTEXT, item, (LPARAM) &newLetter);
557 if (HIWORD(wParam) != CBN_SELCHANGE) break;
558 if (newLetter == editWindowLetter) break;
560 WINE_TRACE("changing drive letter to %c\n", newLetter);
561 copyDrive(editWindowLetter, newLetter);
562 removeDrive(editWindowLetter);
563 editWindowLetter = newLetter;
564 refreshDriveDlg(driveDlgHandle);
568 case IDC_BUTTON_BROWSE_PATH:
572 case IDC_RADIO_AUTODETECT: {
573 setDriveValue(editWindowLetter, "Label", NULL);
574 setDriveValue(editWindowLetter, "Serial", NULL);
575 setDriveValue(editWindowLetter, "Device", getDialogItemText(hDlg, IDC_EDIT_DEVICE));
576 enable_labelserial_box(hDlg, BOX_MODE_CD_AUTODETECT);
577 refreshDriveDlg(driveDlgHandle);
581 case IDC_RADIO_ASSIGN:
582 setDriveValue(editWindowLetter, "Device", NULL);
583 setDriveValue(editWindowLetter, "Label", getDialogItemText(hDlg, IDC_EDIT_LABEL));
584 setDriveValue(editWindowLetter, "Serial", getDialogItemText(hDlg, IDC_EDIT_SERIAL));
585 enable_labelserial_box(hDlg, BOX_MODE_CD_ASSIGN);
586 refreshDriveDlg(driveDlgHandle);
590 EndDialog(hDlg, wParam);
593 if (HIWORD(wParam) == EN_CHANGE) onEditChanged(hDlg, LOWORD(wParam));
599 void onAddDriveClicked(HWND hDlg) {
600 /* we should allocate a drive letter automatically. We also need some way to let the user choose the mapping point,
601 for now we will just force them to enter a path automatically, with / being the default. In future we should
602 be able to temporarily map / then invoke the directory chooser dialog. */
604 char newLetter = 'C'; /* we skip A and B, they are historically floppy drives */
605 long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
608 while (mask & (1 << (newLetter - 'A'))) {
610 if (newLetter > 'Z') {
611 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);
615 WINE_TRACE("allocating drive letter %c\n", newLetter);
617 sectionName = malloc(strlen("Drive X") + 1);
618 sprintf(sectionName, "Drive %c", newLetter);
619 if (newLetter == 'C') {
620 addTransaction(sectionName, "Path", ACTION_SET, "fake_windows");
621 addTransaction(sectionName, "Label", ACTION_SET, "System Drive");
623 addTransaction(sectionName, "Path", ACTION_SET, "/"); /* default to root path */
624 addTransaction(sectionName, "Type", ACTION_SET, "hd");
625 processTransQueue(); /* make sure the drive has been added, even if we are not in instant apply mode */
628 refreshDriveDlg(driveDlgHandle);
630 DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) newLetter);
635 DriveDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
642 switch (LOWORD(wParam)) {
643 case IDC_LIST_DRIVES:
644 /* double click should open the edit window for the chosen drive */
645 if (HIWORD(wParam) == LBN_DBLCLK)
646 SendMessageA(hDlg, WM_COMMAND, IDC_BUTTON_EDIT, 0);
648 if (HIWORD(wParam) == LBN_SELCHANGE) lastSel = SendDlgItemMessage(hDlg, IDC_LIST_DRIVES, LB_GETCURSEL, 0, 0);
652 onAddDriveClicked(hDlg);
655 case IDC_BUTTON_REMOVE:
656 if (HIWORD(wParam) != BN_CLICKED) break;
657 nItem = SendDlgItemMessage(hDlg, IDC_LIST_DRIVES, LB_GETCURSEL, 0, 0);
658 letter = SendDlgItemMessage(hDlg, IDC_LIST_DRIVES, LB_GETITEMDATA, nItem, 0);
660 refreshDriveDlg(driveDlgHandle);
663 case IDC_BUTTON_EDIT:
664 if (HIWORD(wParam) != BN_CLICKED) break;
665 nItem = SendMessage(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_GETCURSEL, 0, 0);
666 letter = SendMessage(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_GETITEMDATA, nItem, 0);
667 DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) letter);
670 case IDC_BUTTON_AUTODETECT:
676 case WM_NOTIFY: switch(((LPNMHDR)lParam)->code) {
678 SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
681 SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
684 driveDlgHandle = hDlg;
685 refreshDriveDlg (driveDlgHandle);