Rename 'General tab' to 'About', move to the last position.
[wine] / programs / winecfg / drive.c
1 /*
2  * Drive management UI code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2003 Mike Hearn
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
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>
38
39 #include "winecfg.h"
40 #include "resource.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
43
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 */
47
48
49 /* returns NULL on failure. caller is responsible for freeing result */
50 char *getDriveValue(char letter, const char *valueName) {
51   HKEY hkDrive = 0;
52   char *subKeyName;
53   char *result = NULL;
54   HRESULT hr;
55   DWORD bufferSize;
56   
57   WINE_TRACE("letter=%c, valueName=%s\n", letter, valueName);
58
59   subKeyName = malloc(strlen("Drive X")+1);
60   sprintf(subKeyName, "Drive %c", letter);
61
62   hr = RegOpenKeyEx(configKey, subKeyName, 0, KEY_READ, &hkDrive);
63   if (hr != ERROR_SUCCESS) goto end;
64
65   hr = RegQueryValueEx(hkDrive, valueName, NULL, NULL, NULL, &bufferSize);
66   if (hr != ERROR_SUCCESS) goto end;
67
68   result = malloc(bufferSize);
69   hr = RegQueryValueEx(hkDrive, valueName, NULL, NULL, result, &bufferSize);
70   if (hr != ERROR_SUCCESS) goto end;
71   
72 end:
73   if (hkDrive) RegCloseKey(hkDrive);
74   free(subKeyName);
75   return result;
76 }
77
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);
82   if (newValue)
83     addTransaction(driveSection, valueName, ACTION_SET, newValue);
84   else
85     addTransaction(driveSection, valueName, ACTION_REMOVE, NULL);
86   free(driveSection);
87 }
88
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;
93   
94   WINE_TRACE("srcLetter=%c, destLetter=%c\n", srcLetter, destLetter);
95
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");
102   
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);
109
110   if (path)   free(path);
111   if (label)  free(label);
112   if (type)   free(type);
113   if (serial) free(serial);
114   if (fs)     free(fs);
115 }
116
117 void removeDrive(char letter) {
118   char driveSection[sizeof("Drive X")];
119   sprintf(driveSection, "Drive %c", letter);
120   addTransaction(driveSection, NULL, ACTION_REMOVE, NULL);
121 }
122
123 int refreshDriveDlg (HWND dialog)
124 {
125   int i;
126   char *subKeyName = malloc(MAX_NAME_LENGTH);
127   int driveCount = 0;
128   DWORD sizeOfSubKeyName = MAX_NAME_LENGTH;
129   int doesDriveCExist = FALSE;
130
131   WINE_TRACE("\n");
132
133   updatingUI = TRUE;
134
135   /* Clear the listbox */
136   SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_RESETCONTENT, 0, 0);
137   for (i = 0;
138        RegEnumKeyExA(configKey, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS;
139        ++i, sizeOfSubKeyName = MAX_NAME_LENGTH) {
140     
141     HKEY hkDrive;
142     char returnBuffer[MAX_NAME_LENGTH];
143     DWORD sizeOfReturnBuffer = sizeof(returnBuffer);
144     LONG r;
145     WINE_TRACE("%s\n", subKeyName);
146     
147     if (!strncmp("Drive ", subKeyName, 5)) {
148       char driveLetter = '\0';
149       char *label;
150       char *title;
151       char *device;
152       int titleLen;
153       const char *itemLabel = "Drive %s (%s)";
154       int itemIndex;
155       
156       if (RegOpenKeyExA (configKey, subKeyName, 0, KEY_READ, &hkDrive) != ERROR_SUCCESS)        {
157         WINE_ERR("unable to open drive registry key");
158         RegCloseKey(configKey);
159         return -1;
160       }
161       
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;
166       
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);
173       } else {
174         WINE_WARN("label not loaded: %ld\n", r);
175         label = NULL;
176       }
177
178       device = getDriveValue(driveLetter, "Device");
179       
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);
183
184       /* fixup label */
185       if (!label && device) {
186         label = malloc(strlen("[label read from device ]")+1+strlen(device));
187         sprintf(label, "[label read from device %s]", device);
188       }
189       if (!label) label = strdup("(no label)");
190       
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);
196       
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);
200       
201       free(title);
202       free(label);
203
204       driveCount++;
205         
206     }
207   }
208   
209   WINE_TRACE("loaded %d drives\n", driveCount);
210   SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LB_SETSEL, TRUE, lastSel);
211
212   /* show the warning if there is no Drive C */
213   if (!doesDriveCExist)
214     ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
215   else
216     ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
217   
218   free(subKeyName);
219
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);
228     
229   } else {
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);    
236   }
237
238   
239   updatingUI = FALSE;
240   return driveCount;
241 }
242
243 /******************************************************************************/
244 /*  The Drive Editing Dialog                                                  */
245 /******************************************************************************/
246 #define DRIVE_MASK_BIT(B) 1<<(toupper(B)-'A')
247
248 typedef struct {
249   const char *sCode;
250   const char *sDesc;
251 } code_desc_pair;
252
253 static code_desc_pair type_pairs[] = {
254   {"hd", "Local hard disk"},
255   {"network", "Network share" },
256   {"floppy", "Floppy disk"},
257   {"cdrom", "CD-ROM"}
258 };
259 #define DRIVE_TYPE_DEFAULT 1
260
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"}
265 };
266  
267 #define DRIVE_FS_DEFAULT 0
268
269
270 void fill_drive_droplist(long mask, char currentLetter, HWND hDlg)
271 {
272   int i;
273   int selection;
274   int count;
275   int next_letter;
276   char sName[4] = "A:";
277
278   for( i=0, count=0, selection=-1, next_letter=-1; i <= 'Z'-'A'; ++i ) {
279     if( mask & DRIVE_MASK_BIT('A'+i) ) {
280       int index;
281       
282       sName[0] = 'A' + i;
283       index = SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName );
284                           
285       if( toupper(currentLetter) == 'A' + i ) {
286         selection = count;
287       }
288       
289       if( i >= 2 && next_letter == -1){ /*default drive is first one of C-Z */
290         next_letter = count;
291       }
292       
293       count++;
294     }
295   }
296   
297   if( selection == -1 ) {
298     selection = next_letter;
299   }
300   
301   SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0 );
302 }
303
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)
309 {
310   WINE_TRACE("mode=%d\n", mode);
311   switch (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);
321         break;
322         
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);
332         break;
333
334       case BOX_MODE_NONE:
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);
343         break;
344
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);
354         break;  
355   }
356 }
357
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.
360  *
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.
363  *
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.
366  */
367 long drive_available_mask(char letter)
368 {
369   long result = 0;
370   char curLetter;
371   char *slop;
372   
373   WINE_TRACE("\n");
374  
375   for (curLetter = 'A'; curLetter < 'Z'; curLetter++) {
376     slop = getDriveValue(curLetter, "Path");
377     if (slop != NULL) {
378       result |= DRIVE_MASK_BIT(curLetter);
379       free(slop);
380     }
381   }
382   
383   result = ~result;
384   if (letter) result |= DRIVE_MASK_BIT(letter);
385   
386   WINE_TRACE( "finished drive letter loop with %lx\n", result );
387   return result;
388 }
389
390
391 void refreshDriveEditDialog(HWND dialog) {
392   char *path;
393   char *type;
394   char *fs;
395   char *serial;
396   char *label;
397   char *device;
398   int i, selection;
399
400   updatingUI = TRUE;
401   
402   /* Drive letters */
403   fill_drive_droplist( drive_available_mask( editWindowLetter ), editWindowLetter, dialog );
404
405   /* path */
406   path = getDriveValue(editWindowLetter, "Path");
407   if (path) {
408     SetWindowText(GetDlgItem(dialog, IDC_EDIT_PATH), path);
409   } else WINE_WARN("no Path field?\n");
410   
411   /* drive type */
412   type = getDriveValue(editWindowLetter, "Type");
413   if (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){
418         selection = i;
419       }
420     }
421   
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");
425
426   
427   /* FileSystem name handling */
428   fs = getDriveValue(editWindowLetter, "FileSystem");
429   if (fs) {
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){
434         selection = i;
435       }
436     }
437   
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");
441
442
443   /* removeable media properties */
444   serial = getDriveValue(editWindowLetter, "Serial");
445   if (serial) {
446     SendDlgItemMessage(dialog, IDC_EDIT_SERIAL, WM_SETTEXT, 0,(LPARAM)serial);
447   } else WINE_WARN("no Serial field?\n");
448
449   label = getDriveValue(editWindowLetter, "Label");
450   if (label) {
451     SendDlgItemMessage(dialog, IDC_EDIT_LABEL, WM_SETTEXT, 0,(LPARAM)label);
452   } else WINE_WARN("no Label field?\n");
453
454   device = getDriveValue(editWindowLetter, "Device");
455   if (device) {
456     SendDlgItemMessage(dialog, IDC_EDIT_DEVICE, WM_SETTEXT, 0,(LPARAM)device);
457   } else WINE_WARN("no Device field?\n");
458
459   selection = IDC_RADIO_ASSIGN;
460   if ((type && strcmp("cdrom", type) == 0) || (type && strcmp("floppy", type) == 0)) {
461     if (device) {
462       selection = IDC_RADIO_AUTODETECT;
463       enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
464     } else {
465       selection = IDC_RADIO_ASSIGN;
466       enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
467     }
468   } else {
469     enable_labelserial_box(dialog, BOX_MODE_NORMAL);
470     selection = IDC_RADIO_ASSIGN;
471   }
472
473   CheckRadioButton( dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection );
474   if (path) SendDlgItemMessage(dialog, IDC_EDIT_PATH, WM_SETTEXT, 0,(LPARAM)path);
475   
476   if (path) free(path);
477   if (type) free(type);
478   if (fs) free(fs);
479   if (serial) free(serial);
480   if (label) free(label);
481   if (device) free(device);
482
483   
484   updatingUI = FALSE;
485   
486   return;
487 }
488
489 /* storing the drive propsheet HWND here is a bit ugly, but the simplest solution for now */
490 static HWND driveDlgHandle;
491
492 void onEditChanged(HWND hDlg, WORD controlID) {
493   WINE_TRACE("controlID=%d\n", controlID);
494   switch (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);
500         break;
501       }
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);
506         free(path);
507         break;
508       }
509       case IDC_EDIT_SERIAL: {
510         char *serial = getDialogItemText(hDlg, controlID);
511         setDriveValue(editWindowLetter, "Serial", serial);
512         if (serial) free (serial);
513         break;
514       }
515       case IDC_EDIT_DEVICE: {
516         char *device = getDialogItemText(hDlg,controlID);
517         setDriveValue(editWindowLetter, "Device", device);
518         if (device) free(device);
519         refreshDriveDlg(driveDlgHandle);
520         break;
521       }
522   }
523 }
524
525 INT_PTR CALLBACK DriveEditDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
526 {
527   int selection;
528
529   switch (uMsg)  {
530       case WM_INITDIALOG: {
531         editWindowLetter = (char) lParam;
532         refreshDriveEditDialog(hDlg);
533       }
534
535     case WM_COMMAND:
536       switch (LOWORD(wParam)) {
537           case IDC_COMBO_TYPE:
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);
543               else
544                 enable_labelserial_box(hDlg, BOX_MODE_CD_ASSIGN);
545             }
546             else {
547               enable_labelserial_box( hDlg, BOX_MODE_NORMAL );
548             }
549             setDriveValue(editWindowLetter, "Type", type_pairs[selection].sCode);
550             break;
551
552           case IDC_COMBO_LETTER: {
553             int item = SendDlgItemMessage(hDlg, IDC_COMBO_LETTER, CB_GETCURSEL, 0, 0);
554             char newLetter;
555             SendDlgItemMessage(hDlg, IDC_COMBO_LETTER, CB_GETLBTEXT, item, (LPARAM) &newLetter);
556             
557             if (HIWORD(wParam) != CBN_SELCHANGE) break;
558             if (newLetter == editWindowLetter) break;
559                     
560             WINE_TRACE("changing drive letter to %c\n", newLetter);
561             copyDrive(editWindowLetter, newLetter);
562             removeDrive(editWindowLetter);
563             editWindowLetter = newLetter;
564             refreshDriveDlg(driveDlgHandle);
565             break;
566           }
567
568           case IDC_BUTTON_BROWSE_PATH:
569             WRITEME(hDlg);
570             break;
571
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);
578             break;
579           }
580             
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);
587             break;
588             
589           case ID_BUTTON_OK:      
590             EndDialog(hDlg, wParam);
591             return TRUE;
592       }
593       if (HIWORD(wParam) == EN_CHANGE) onEditChanged(hDlg, LOWORD(wParam));
594       break;
595   }
596   return FALSE;
597 }
598
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. */
603   
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 */
606   char *sectionName;
607   
608   while (mask & (1 << (newLetter - 'A'))) {
609     newLetter++;
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);
612       return;
613     }
614   }
615   WINE_TRACE("allocating drive letter %c\n", newLetter);
616   
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");
622   } else
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 */
626   free(sectionName);
627
628   refreshDriveDlg(driveDlgHandle);
629
630   DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) newLetter);
631 }
632
633
634 INT_PTR CALLBACK
635 DriveDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
636 {
637   int nItem;
638   char letter;
639   
640   switch (uMsg) {
641       case WM_COMMAND:
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);
647
648               if (HIWORD(wParam) == LBN_SELCHANGE) lastSel = SendDlgItemMessage(hDlg, IDC_LIST_DRIVES, LB_GETCURSEL, 0, 0);
649               break;
650
651             case IDC_BUTTON_ADD:
652               onAddDriveClicked(hDlg);
653               break;
654
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);
659               removeDrive(letter);
660               refreshDriveDlg(driveDlgHandle);
661               break;
662               
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);
668               break;
669
670             case IDC_BUTTON_AUTODETECT:
671               WRITEME(hDlg);
672               break;
673         }
674         break;
675         
676       case WM_NOTIFY: switch(((LPNMHDR)lParam)->code) {
677             case PSN_KILLACTIVE:
678               SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
679               break;
680             case PSN_APPLY:
681               SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
682               break;
683             case PSN_SETACTIVE:
684               driveDlgHandle = hDlg;
685               refreshDriveDlg (driveDlgHandle);
686               break;
687         }
688         break;
689   }
690
691   return FALSE;
692 }