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") + 1];
92 char *path
, *label
, *type
, *serial
;
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");
102 sprintf(driveSection
, "Drive %c", destLetter
);
103 if (path
) addTransaction(driveSection
, "Path", ACTION_SET
, path
);
104 if (label
) addTransaction(driveSection
, "Label", ACTION_SET
, label
);
105 if (type
) addTransaction(driveSection
, "Type", ACTION_SET
, type
);
106 if (serial
) addTransaction(driveSection
, "Serial", ACTION_SET
, serial
);
108 if (path
) free(path
);
109 if (label
) free(label
);
110 if (type
) free(type
);
111 if (serial
) free(serial
);
114 void removeDrive(char letter
) {
115 char driveSection
[sizeof("Drive X") + 1];
116 sprintf(driveSection
, "Drive %c", letter
);
117 addTransaction(driveSection
, NULL
, ACTION_REMOVE
, NULL
);
120 int refreshDriveDlg (HWND dialog
)
123 char *subKeyName
= malloc(MAX_NAME_LENGTH
);
125 DWORD sizeOfSubKeyName
= MAX_NAME_LENGTH
;
126 int doesDriveCExist
= FALSE
;
132 /* Clear the listbox */
133 SendMessageA(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_RESETCONTENT
, 0, 0);
135 RegEnumKeyExA(configKey
, i
, subKeyName
, &sizeOfSubKeyName
, NULL
, NULL
, NULL
, NULL
) != ERROR_NO_MORE_ITEMS
;
136 ++i
, sizeOfSubKeyName
= MAX_NAME_LENGTH
) {
139 char returnBuffer
[MAX_NAME_LENGTH
];
140 DWORD sizeOfReturnBuffer
= sizeof(returnBuffer
);
142 WINE_TRACE("%s\n", subKeyName
);
144 if (!strncmp("Drive ", subKeyName
, 5)) {
145 char driveLetter
= '\0';
150 const char *itemLabel
= "Drive %s (%s)";
153 if (RegOpenKeyExA (configKey
, subKeyName
, 0, KEY_READ
, &hkDrive
) != ERROR_SUCCESS
) {
154 WINE_ERR("unable to open drive registry key");
155 RegCloseKey(configKey
);
159 /* extract the drive letter, force to upper case */
160 driveLetter
= subKeyName
[strlen(subKeyName
)-1];
161 if (driveLetter
) driveLetter
= toupper(driveLetter
);
162 if (driveLetter
== 'C') doesDriveCExist
= TRUE
;
164 ZeroMemory(returnBuffer
, sizeof(*returnBuffer
));
165 sizeOfReturnBuffer
= sizeof(returnBuffer
);
166 r
= RegQueryValueExA(hkDrive
, "Label", NULL
, NULL
, returnBuffer
, &sizeOfReturnBuffer
);
167 if (r
== ERROR_SUCCESS
) {
168 label
= malloc(sizeOfReturnBuffer
);
169 strncpy(label
, returnBuffer
, sizeOfReturnBuffer
);
171 WINE_WARN("label not loaded: %ld\n", r
);
175 device
= getDriveValue(driveLetter
, "Device");
177 /* We now know the label and drive letter, so we can add to the list. The list items will have the letter associated
178 * with them, which acts as the key. We can then use that letter to get/set the properties of the drive. */
179 WINE_TRACE("Adding %c: label=%s to the listbox, device=%s\n", driveLetter
, label
, device
);
182 if (!label
&& device
) {
183 label
= malloc(strlen("[label read from device ]")+1+strlen(device
));
184 sprintf(label
, "[label read from device %s]", device
);
186 if (!label
) label
= strdup("(no label)");
188 titleLen
= strlen(itemLabel
) - 1 + strlen(label
) - 2 + 1;
189 title
= malloc(titleLen
);
190 /* the %s in the item label will be replaced by the drive letter, so -1, then
191 -2 for the second %s which will be expanded to the label, finally + 1 for terminating #0 */
192 snprintf(title
, titleLen
, "Drive %c: %s", driveLetter
, label
);
194 /* the first SendMessage call adds the string and returns the index, the second associates that index with it */
195 itemIndex
= SendMessageA(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_ADDSTRING
,(WPARAM
) -1, (LPARAM
) title
);
196 SendMessageA(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_SETITEMDATA
, itemIndex
, (LPARAM
) driveLetter
);
206 WINE_TRACE("loaded %d drives\n", driveCount
);
207 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETSEL
, TRUE
, lastSel
);
209 /* show the warning if there is no Drive C */
210 if (!doesDriveCExist
)
211 ShowWindow(GetDlgItem(dialog
, IDS_DRIVE_NO_C
), SW_NORMAL
);
213 ShowWindow(GetDlgItem(dialog
, IDS_DRIVE_NO_C
), SW_HIDE
);
217 /* disable or enable controls depending on whether we are editing global vs app specific config */
218 if (appSettings
== EDITING_GLOBAL
) {
219 WINE_TRACE("enabling controls\n");
220 enable(IDC_LIST_DRIVES
);
221 enable(IDC_BUTTON_ADD
);
222 enable(IDC_BUTTON_REMOVE
);
223 enable(IDC_BUTTON_EDIT
);
224 enable(IDC_BUTTON_AUTODETECT
);
227 WINE_TRACE("disabling controls\n");
228 disable(IDC_LIST_DRIVES
);
229 disable(IDC_BUTTON_ADD
);
230 disable(IDC_BUTTON_REMOVE
);
231 disable(IDC_BUTTON_EDIT
);
232 disable(IDC_BUTTON_AUTODETECT
);
240 /******************************************************************************/
241 /* The Drive Editing Dialog */
242 /******************************************************************************/
243 #define DRIVE_MASK_BIT(B) 1<<(toupper(B)-'A')
250 static code_desc_pair type_pairs
[] = {
251 {"hd", "Local hard disk"},
252 {"network", "Network share" },
253 {"floppy", "Floppy disk"},
256 #define DRIVE_TYPE_DEFAULT 1
259 void fill_drive_droplist(long mask
, char currentLetter
, HWND hDlg
)
265 char sName
[4] = "A:";
267 for( i
=0, count
=0, selection
=-1, next_letter
=-1; i
<= 'Z'-'A'; ++i
) {
268 if( mask
& DRIVE_MASK_BIT('A'+i
) ) {
272 index
= SendDlgItemMessage( hDlg
, IDC_COMBO_LETTER
, CB_ADDSTRING
, 0, (LPARAM
) sName
);
274 if( toupper(currentLetter
) == 'A' + i
) {
278 if( i
>= 2 && next_letter
== -1){ /*default drive is first one of C-Z */
286 if( selection
== -1 ) {
287 selection
= next_letter
;
290 SendDlgItemMessage( hDlg
, IDC_COMBO_LETTER
, CB_SETCURSEL
, selection
, 0 );
293 #define BOX_MODE_CD_ASSIGN 1
294 #define BOX_MODE_CD_AUTODETECT 2
295 #define BOX_MODE_NONE 3
296 #define BOX_MODE_NORMAL 4
297 void enable_labelserial_box(HWND dialog
, int mode
)
299 WINE_TRACE("mode=%d\n", mode
);
301 case BOX_MODE_CD_ASSIGN
:
302 enable(IDC_RADIO_AUTODETECT
);
303 enable(IDC_RADIO_ASSIGN
);
304 disable(IDC_EDIT_DEVICE
);
305 disable(IDC_BUTTON_BROWSE_DEVICE
);
306 enable(IDC_EDIT_SERIAL
);
307 enable(IDC_EDIT_LABEL
);
308 enable(IDC_STATIC_SERIAL
);
309 enable(IDC_STATIC_LABEL
);
312 case BOX_MODE_CD_AUTODETECT
:
313 enable(IDC_RADIO_AUTODETECT
);
314 enable(IDC_RADIO_ASSIGN
);
315 enable(IDC_EDIT_DEVICE
);
316 enable(IDC_BUTTON_BROWSE_DEVICE
);
317 disable(IDC_EDIT_SERIAL
);
318 disable(IDC_EDIT_LABEL
);
319 disable(IDC_STATIC_SERIAL
);
320 disable(IDC_STATIC_LABEL
);
324 disable(IDC_RADIO_AUTODETECT
);
325 disable(IDC_RADIO_ASSIGN
);
326 disable(IDC_EDIT_DEVICE
);
327 disable(IDC_BUTTON_BROWSE_DEVICE
);
328 disable(IDC_EDIT_SERIAL
);
329 disable(IDC_EDIT_LABEL
);
330 disable(IDC_STATIC_SERIAL
);
331 disable(IDC_STATIC_LABEL
);
334 case BOX_MODE_NORMAL
:
335 disable(IDC_RADIO_AUTODETECT
);
336 enable(IDC_RADIO_ASSIGN
);
337 disable(IDC_EDIT_DEVICE
);
338 disable(IDC_BUTTON_BROWSE_DEVICE
);
339 enable(IDC_EDIT_SERIAL
);
340 enable(IDC_EDIT_LABEL
);
341 enable(IDC_STATIC_SERIAL
);
342 enable(IDC_STATIC_LABEL
);
347 /* This function produces a mask for each drive letter that isn't currently used. Each bit of the long result
348 * represents a letter, with A being the least significant bit, and Z being the most significant.
350 * To calculate this, we loop over each letter, and see if we can get a drive entry for it. If so, we
351 * set the appropriate bit. At the end, we flip each bit, to give the desired result.
353 * The letter parameter is always marked as being available. This is so the edit dialog can display the
354 * currently used drive letter alongside the available ones.
356 long drive_available_mask(char letter
)
364 for (curLetter
= 'A'; curLetter
< 'Z'; curLetter
++) {
365 slop
= getDriveValue(curLetter
, "Path");
367 result
|= DRIVE_MASK_BIT(curLetter
);
373 if (letter
) result
|= DRIVE_MASK_BIT(letter
);
375 WINE_TRACE( "finished drive letter loop with %lx\n", result
);
380 void refreshDriveEditDialog(HWND dialog
) {
391 fill_drive_droplist( drive_available_mask( editWindowLetter
), editWindowLetter
, dialog
);
394 path
= getDriveValue(editWindowLetter
, "Path");
396 SetWindowText(GetDlgItem(dialog
, IDC_EDIT_PATH
), path
);
397 } else WINE_WARN("no Path field?\n");
400 type
= getDriveValue(editWindowLetter
, "Type");
402 for(i
= 0, selection
= -1; i
< sizeof(type_pairs
)/sizeof(code_desc_pair
); i
++) {
403 SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_ADDSTRING
, 0,
404 (LPARAM
) type_pairs
[i
].sDesc
);
405 if(strcasecmp(type_pairs
[i
].sCode
, type
) == 0){
410 if( selection
== -1 ) selection
= DRIVE_TYPE_DEFAULT
;
411 SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_SETCURSEL
, selection
, 0);
412 } else WINE_WARN("no Type field?\n");
415 /* removeable media properties */
416 serial
= getDriveValue(editWindowLetter
, "Serial");
418 SendDlgItemMessage(dialog
, IDC_EDIT_SERIAL
, WM_SETTEXT
, 0,(LPARAM
)serial
);
419 } else WINE_WARN("no Serial field?\n");
421 label
= getDriveValue(editWindowLetter
, "Label");
423 SendDlgItemMessage(dialog
, IDC_EDIT_LABEL
, WM_SETTEXT
, 0,(LPARAM
)label
);
424 } else WINE_WARN("no Label field?\n");
426 device
= getDriveValue(editWindowLetter
, "Device");
428 SendDlgItemMessage(dialog
, IDC_EDIT_DEVICE
, WM_SETTEXT
, 0,(LPARAM
)device
);
429 } else WINE_WARN("no Device field?\n");
431 selection
= IDC_RADIO_ASSIGN
;
432 if ((type
&& strcmp("cdrom", type
) == 0) || (type
&& strcmp("floppy", type
) == 0)) {
434 selection
= IDC_RADIO_AUTODETECT
;
435 enable_labelserial_box(dialog
, BOX_MODE_CD_AUTODETECT
);
437 selection
= IDC_RADIO_ASSIGN
;
438 enable_labelserial_box(dialog
, BOX_MODE_CD_ASSIGN
);
441 enable_labelserial_box(dialog
, BOX_MODE_NORMAL
);
442 selection
= IDC_RADIO_ASSIGN
;
445 CheckRadioButton( dialog
, IDC_RADIO_AUTODETECT
, IDC_RADIO_ASSIGN
, selection
);
446 if (path
) SendDlgItemMessage(dialog
, IDC_EDIT_PATH
, WM_SETTEXT
, 0,(LPARAM
)path
);
448 if (path
) free(path
);
449 if (type
) free(type
);
450 if (serial
) free(serial
);
451 if (label
) free(label
);
452 if (device
) free(device
);
460 /* storing the drive propsheet HWND here is a bit ugly, but the simplest solution for now */
461 static HWND driveDlgHandle
;
463 void onEditChanged(HWND hDlg
, WORD controlID
) {
464 WINE_TRACE("controlID=%d\n", controlID
);
466 case IDC_EDIT_LABEL
: {
467 char *label
= getDialogItemText(hDlg
, controlID
);
468 setDriveValue(editWindowLetter
, "Label", label
);
469 refreshDriveDlg(driveDlgHandle
);
470 if (label
) free(label
);
473 case IDC_EDIT_PATH
: {
474 char *path
= getDialogItemText(hDlg
, controlID
);
475 if (!path
) path
= strdup("fake_windows"); /* default to assuming fake_windows in the .wine directory */
476 setDriveValue(editWindowLetter
, "Path", path
);
480 case IDC_EDIT_SERIAL
: {
481 char *serial
= getDialogItemText(hDlg
, controlID
);
482 setDriveValue(editWindowLetter
, "Serial", serial
);
483 if (serial
) free (serial
);
486 case IDC_EDIT_DEVICE
: {
487 char *device
= getDialogItemText(hDlg
,controlID
);
488 setDriveValue(editWindowLetter
, "Device", device
);
489 if (device
) free(device
);
490 refreshDriveDlg(driveDlgHandle
);
496 INT_PTR CALLBACK
DriveEditDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
501 case WM_INITDIALOG
: {
502 editWindowLetter
= (char) lParam
;
503 refreshDriveEditDialog(hDlg
);
507 switch (LOWORD(wParam
)) {
509 if (HIWORD(wParam
) != CBN_SELCHANGE
) break;
510 selection
= SendDlgItemMessage( hDlg
, IDC_COMBO_TYPE
, CB_GETCURSEL
, 0, 0);
511 if( selection
== 2 || selection
== 3 ) { /* cdrom or floppy */
512 if (IsDlgButtonChecked(hDlg
, IDC_RADIO_AUTODETECT
))
513 enable_labelserial_box(hDlg
, BOX_MODE_CD_AUTODETECT
);
515 enable_labelserial_box(hDlg
, BOX_MODE_CD_ASSIGN
);
518 enable_labelserial_box( hDlg
, BOX_MODE_NORMAL
);
520 setDriveValue(editWindowLetter
, "Type", type_pairs
[selection
].sCode
);
523 case IDC_COMBO_LETTER
: {
524 int item
= SendDlgItemMessage(hDlg
, IDC_COMBO_LETTER
, CB_GETCURSEL
, 0, 0);
526 SendDlgItemMessage(hDlg
, IDC_COMBO_LETTER
, CB_GETLBTEXT
, item
, (LPARAM
) &newLetter
);
528 if (HIWORD(wParam
) != CBN_SELCHANGE
) break;
529 if (newLetter
== editWindowLetter
) break;
531 WINE_TRACE("changing drive letter to %c\n", newLetter
);
532 copyDrive(editWindowLetter
, newLetter
);
533 removeDrive(editWindowLetter
);
534 editWindowLetter
= newLetter
;
535 refreshDriveDlg(driveDlgHandle
);
539 case IDC_BUTTON_BROWSE_PATH
:
543 case IDC_RADIO_AUTODETECT
: {
544 setDriveValue(editWindowLetter
, "Label", NULL
);
545 setDriveValue(editWindowLetter
, "Serial", NULL
);
546 setDriveValue(editWindowLetter
, "Device", getDialogItemText(hDlg
, IDC_EDIT_DEVICE
));
547 enable_labelserial_box(hDlg
, BOX_MODE_CD_AUTODETECT
);
548 refreshDriveDlg(driveDlgHandle
);
552 case IDC_RADIO_ASSIGN
:
553 setDriveValue(editWindowLetter
, "Device", NULL
);
554 setDriveValue(editWindowLetter
, "Label", getDialogItemText(hDlg
, IDC_EDIT_LABEL
));
555 setDriveValue(editWindowLetter
, "Serial", getDialogItemText(hDlg
, IDC_EDIT_SERIAL
));
556 enable_labelserial_box(hDlg
, BOX_MODE_CD_ASSIGN
);
557 refreshDriveDlg(driveDlgHandle
);
561 EndDialog(hDlg
, wParam
);
564 if (HIWORD(wParam
) == EN_CHANGE
) onEditChanged(hDlg
, LOWORD(wParam
));
570 void onAddDriveClicked(HWND hDlg
) {
571 /* we should allocate a drive letter automatically. We also need some way to let the user choose the mapping point,
572 for now we will just force them to enter a path automatically, with / being the default. In future we should
573 be able to temporarily map / then invoke the directory chooser dialog. */
575 char newLetter
= 'C'; /* we skip A and B, they are historically floppy drives */
576 long mask
= ~drive_available_mask(0); /* the mask is now which drives aren't available */
579 while (mask
& (1 << (newLetter
- 'A'))) {
581 if (newLetter
> 'Z') {
582 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
);
586 WINE_TRACE("allocating drive letter %c\n", newLetter
);
588 sectionName
= malloc(strlen("Drive X") + 1);
589 sprintf(sectionName
, "Drive %c", newLetter
);
590 if (newLetter
== 'C') {
591 addTransaction(sectionName
, "Path", ACTION_SET
, "fake_windows");
592 addTransaction(sectionName
, "Label", ACTION_SET
, "System Drive");
594 addTransaction(sectionName
, "Path", ACTION_SET
, "/"); /* default to root path */
595 addTransaction(sectionName
, "Type", ACTION_SET
, "hd");
596 processTransQueue(); /* make sure the drive has been added, even if we are not in instant apply mode */
599 refreshDriveDlg(driveDlgHandle
);
601 DialogBoxParam(NULL
, MAKEINTRESOURCE(IDD_DRIVE_EDIT
), NULL
, (DLGPROC
) DriveEditDlgProc
, (LPARAM
) newLetter
);
606 DriveDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
613 switch (LOWORD(wParam
)) {
614 case IDC_LIST_DRIVES
:
615 /* double click should open the edit window for the chosen drive */
616 if (HIWORD(wParam
) == LBN_DBLCLK
)
617 SendMessageA(hDlg
, WM_COMMAND
, IDC_BUTTON_EDIT
, 0);
619 if (HIWORD(wParam
) == LBN_SELCHANGE
) lastSel
= SendDlgItemMessage(hDlg
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
623 onAddDriveClicked(hDlg
);
626 case IDC_BUTTON_REMOVE
:
627 if (HIWORD(wParam
) != BN_CLICKED
) break;
628 nItem
= SendDlgItemMessage(hDlg
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
629 letter
= SendDlgItemMessage(hDlg
, IDC_LIST_DRIVES
, LB_GETITEMDATA
, nItem
, 0);
631 refreshDriveDlg(driveDlgHandle
);
634 case IDC_BUTTON_EDIT
:
635 if (HIWORD(wParam
) != BN_CLICKED
) break;
636 nItem
= SendMessage(GetDlgItem(hDlg
, IDC_LIST_DRIVES
), LB_GETCURSEL
, 0, 0);
637 letter
= SendMessage(GetDlgItem(hDlg
, IDC_LIST_DRIVES
), LB_GETITEMDATA
, nItem
, 0);
638 DialogBoxParam(NULL
, MAKEINTRESOURCE(IDD_DRIVE_EDIT
), NULL
, (DLGPROC
) DriveEditDlgProc
, (LPARAM
) letter
);
641 case IDC_BUTTON_AUTODETECT
:
647 case WM_NOTIFY
: switch(((LPNMHDR
)lParam
)->code
) {
649 SetWindowLong(hDlg
, DWL_MSGRESULT
, FALSE
);
652 SetWindowLong(hDlg
, DWL_MSGRESULT
, PSNRET_NOERROR
);
655 driveDlgHandle
= hDlg
;
656 refreshDriveDlg (driveDlgHandle
);