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 */
48 /* returns NULL on failure. caller is responsible for freeing result */
49 char *getDriveValue(char letter
, char *valueName
) {
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
;
72 if (hkDrive
) RegCloseKey(hkDrive
);
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
);
84 void refreshDriveDlg (HWND hDlg
)
87 char *subKeyName
= malloc(MAX_NAME_LENGTH
);
89 DWORD sizeOfSubKeyName
;
95 /* Clear the listbox */
96 SendMessageA(GetDlgItem(hDlg
, IDC_LIST_DRIVES
), LB_RESETCONTENT
, 0, 0);
98 RegEnumKeyExA(configKey
, i
, subKeyName
, &sizeOfSubKeyName
, NULL
, NULL
, NULL
, NULL
) != ERROR_NO_MORE_ITEMS
;
99 ++i
, sizeOfSubKeyName
= 50) {
102 char returnBuffer
[MAX_NAME_LENGTH
];
103 DWORD sizeOfReturnBuffer
= sizeof(returnBuffer
);
106 if (!strncmp("Drive ", subKeyName
, 5)) {
107 char driveLetter
= '\0';
111 const char *itemLabel
= "Drive %s (%s)";
114 if (RegOpenKeyExA (configKey
, subKeyName
, 0, KEY_READ
, &hkDrive
) != ERROR_SUCCESS
) {
115 WINE_ERR("unable to open drive registry key");
116 RegCloseKey(configKey
);
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
);
131 WINE_WARN("label not loaded: %ld\n", r
);
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
);
151 if (label
&& (strcmp(label
, "no label") != 0)) free(label
);
158 WINE_TRACE("loaded %d drives\n", driveCount
);
159 SendMessageA(GetDlgItem(hDlg
, IDC_LIST_DRIVES
), LB_SETSEL
, TRUE
, 0);
166 /******************************************************************************/
167 /* The Drive Editing Dialog */
168 /******************************************************************************/
169 #define DRIVE_MASK_BIT(B) 1<<(toupper(B)-'A')
176 static code_desc_pair type_pairs
[] = {
177 {"hd", "Local hard disk"},
178 {"network", "Network share" },
179 {"floppy", "Floppy disk"},
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
)
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
) ) {
204 SendDlgItemMessage( hDlg
, IDC_COMBO_LETTER
, CB_ADDSTRING
, 0, (LPARAM
) sName
);
206 if( toupper(currentLetter
) == 'A' + i
) {
210 if( i
>= 2 && next_letter
== -1){ /*default drive is first one of C-Z */
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
)
256 for (curLetter
= 'A'; curLetter
< 'Z'; curLetter
++) {
257 slop
= getDriveValue(curLetter
, "Path");
259 result
|= DRIVE_MASK_BIT(curLetter
);
265 if (letter
) result
|= DRIVE_MASK_BIT(letter
);
267 WINE_TRACE( "finished drive letter loop with %lx\n", result
);
272 void refreshDriveEditDialog(HWND hDlg
) {
284 fill_drive_droplist( drive_available_mask( editWindowLetter
), editWindowLetter
, hDlg
);
287 path
= getDriveValue(editWindowLetter
, "Path");
289 SetWindowText(GetDlgItem(hDlg
, IDC_EDIT_PATH
), path
);
290 } else WINE_WARN("no Path field?\n");
293 type
= getDriveValue(editWindowLetter
, "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){
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");
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){
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");
327 SendDlgItemMessage(hDlg
, IDC_EDIT_SERIAL
, WM_SETTEXT
, 0,(LPARAM
)serial
);
328 } else WINE_WARN("no Serial field?\n");
330 label
= getDriveValue(editWindowLetter
, "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");
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
;
349 selection
= IDC_RADIO_AUTODETECT
;
352 enable_labelserial_box( hDlg
, 1 );
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
);
365 if (serial
) free(serial
);
366 if (label
) free(label
);
367 if (device
) free(device
);
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
);
380 case IDC_EDIT_LABEL
: { /* drive label edit box */
381 char *label
= getDialogItemText(hDlg
, controlID
);
382 setDriveValue(editWindowLetter
, "Label", label
);
383 refreshDriveDlg(driveDlgHandle
);
390 INT_PTR CALLBACK
DriveEditDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
395 case WM_INITDIALOG
: {
396 editWindowLetter
= (char) lParam
;
397 refreshDriveEditDialog(hDlg
);
401 switch (LOWORD(wParam
)) {
403 switch( HIWORD(wParam
)) {
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 );
410 enable_labelserial_box( hDlg
, 0 );
417 case ID_BUTTON_CANCEL
:
418 EndDialog(hDlg
, wParam
);
421 if (HIWORD(wParam
) == EN_CHANGE
) onEditChanged(hDlg
, LOWORD(wParam
));
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 */
436 while (mask
& (1 << (newLetter
- 'A'))) {
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
);
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 */
452 refreshDriveDlg(driveDlgHandle
);
454 DialogBoxParam(NULL
, MAKEINTRESOURCE(IDD_DRIVE_EDIT2
), NULL
, (DLGPROC
) DriveEditDlgProc
, (LPARAM
) newLetter
);
459 DriveDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
465 switch (LOWORD(wParam
)) {
466 case IDC_LIST_DRIVES
:
467 switch(HIWORD( wParam
)) {
475 onAddDriveClicked(hDlg
);
478 case IDC_BUTTON_REMOVE
:
479 /* temporarily disabled, awaiting rewrite */
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
);
494 case WM_NOTIFY
: switch(((LPNMHDR
)lParam
)->code
) {
496 SetWindowLong(hDlg
, DWL_MSGRESULT
, FALSE
);
499 SetWindowLong(hDlg
, DWL_MSGRESULT
, PSNRET_NOERROR
);
502 driveDlgHandle
= hDlg
;
503 refreshDriveDlg (driveDlgHandle
);