wined3d: GLSL: Implement texrect coord fixup.
[wine] / programs / winecfg / driveui.c
1 /*
2  * Drive management UI code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2004 Chris Morgan
6  * Copyright 2003-2004 Mike Hearn
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  */
23
24 #include <stdio.h>
25
26 #define WIN32_LEAN_AND_MEAN
27 #define COBJMACROS
28
29 #include <windows.h>
30 #include <shellapi.h>
31 #include <objbase.h>
32 #include <shlguid.h>
33 #include <shlwapi.h>
34 #include <shlobj.h>
35
36 #include <wine/unicode.h>
37 #include <wine/debug.h>
38
39 #include "winecfg.h"
40 #include "resource.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
43
44 #define BOX_MODE_CD_ASSIGN 1
45 #define BOX_MODE_CD_AUTODETECT 2
46 #define BOX_MODE_NONE 3
47 #define BOX_MODE_NORMAL 4
48
49 static BOOL advanced = FALSE;
50 static BOOL updating_ui = FALSE;
51 static struct drive* current_drive;
52
53 static void get_etched_rect(HWND dialog, RECT *rect);
54 static void update_controls(HWND dialog);
55
56 static DWORD driveui_msgbox (HWND parent, UINT messageId, DWORD flags)
57 {
58   WCHAR* caption = load_string (IDS_WINECFG_TITLE);
59   WCHAR* text = load_string (messageId);
60   DWORD result = MessageBoxW (parent, text, caption, flags);
61   HeapFree (GetProcessHeap(), 0, caption);
62   HeapFree (GetProcessHeap(), 0, text);
63   return result;
64 }
65
66 /**** listview helper functions ****/
67
68 /* clears the item at index in the listview */
69 static void lv_clear_curr_select(HWND dialog, int index)
70 {
71     ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, 0, LVIS_SELECTED);
72 }
73
74 /* selects the item at index in the listview */
75 static void lv_set_curr_select(HWND dialog, int index)
76 {
77     /* no more than one item can be selected in our listview */
78     lv_clear_curr_select(dialog, -1);
79     ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, LVIS_SELECTED, LVIS_SELECTED);
80 }
81
82 /* returns the currently selected item in the listview */
83 static int lv_get_curr_select(HWND dialog)
84 {
85     return SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
86 }
87
88 /* sets the item in the listview at item->iIndex */
89 static void lv_set_item(HWND dialog, LVITEMW *item)
90 {
91     SendDlgItemMessageW(dialog, IDC_LIST_DRIVES, LVM_SETITEMW, 0, (LPARAM) item);
92 }
93
94 /* sets specified item's text */
95 static void lv_set_item_text(HWND dialog, int item, int subItem, WCHAR *text)
96 {
97     LVITEMW lvItem;
98     if (item < 0 || subItem < 0) return;
99     lvItem.mask = LVIF_TEXT;
100     lvItem.iItem = item;
101     lvItem.iSubItem = subItem;
102     lvItem.pszText = text;
103     lvItem.cchTextMax = lstrlenW(lvItem.pszText);
104     lv_set_item(dialog, &lvItem);
105 }
106
107 /* inserts an item into the listview */
108 static void lv_insert_item(HWND dialog, LVITEMW *item)
109 {
110     SendDlgItemMessageW(dialog, IDC_LIST_DRIVES, LVM_INSERTITEMW, 0, (LPARAM) item);
111 }
112
113 /* retrieve the item at index item->iIndex */
114 static void lv_get_item(HWND dialog, LVITEMW *item)
115 {
116     SendDlgItemMessageW(dialog, IDC_LIST_DRIVES, LVM_GETITEMW, 0, (LPARAM) item);
117 }
118
119 static void set_advanced(HWND dialog)
120 {
121     int state;
122     WCHAR text[256];
123     RECT rect;
124
125     if (advanced)
126     {
127         state = SW_NORMAL;
128         LoadStringW(GetModuleHandle(NULL), IDS_HIDE_ADVANCED, text, 256);
129     }
130     else
131     {
132         state = SW_HIDE;
133         LoadStringW(GetModuleHandle(NULL), IDS_SHOW_ADVANCED, text, 256);
134     }
135
136     ShowWindow(GetDlgItem(dialog, IDC_RADIO_AUTODETECT), state);
137     ShowWindow(GetDlgItem(dialog, IDC_RADIO_ASSIGN), state);
138     ShowWindow(GetDlgItem(dialog, IDC_EDIT_LABEL), state);
139     ShowWindow(GetDlgItem(dialog, IDC_EDIT_DEVICE), state);
140     ShowWindow(GetDlgItem(dialog, IDC_STATIC_LABEL), state);
141     ShowWindow(GetDlgItem(dialog, IDC_BUTTON_BROWSE_DEVICE), state);
142     ShowWindow(GetDlgItem(dialog, IDC_EDIT_SERIAL), state);
143     ShowWindow(GetDlgItem(dialog, IDC_STATIC_SERIAL), state);
144     ShowWindow(GetDlgItem(dialog, IDC_LABELSERIAL_STATIC), state);
145     ShowWindow(GetDlgItem(dialog, IDC_COMBO_TYPE), state);
146     ShowWindow(GetDlgItem(dialog, IDC_STATIC_TYPE), state);
147
148     /* update the button text based on the state */
149     SetWindowTextW(GetDlgItem(dialog, IDC_BUTTON_SHOW_HIDE_ADVANCED), text);
150
151     /* redraw for the etched line */
152     get_etched_rect(dialog, &rect);
153     InflateRect(&rect, 5, 5);
154     InvalidateRect(dialog, &rect, TRUE);
155 }
156
157 struct drive_typemap {
158     unsigned int sCode;
159     UINT idDesc;
160 };
161
162 static const struct drive_typemap type_pairs[] = {
163   { DRIVE_UNKNOWN,    IDS_DRIVE_UNKNOWN   },
164   { DRIVE_FIXED,      IDS_DRIVE_FIXED     },
165   { DRIVE_REMOTE,     IDS_DRIVE_REMOTE    },
166   { DRIVE_REMOVABLE,  IDS_DRIVE_REMOVABLE },
167   { DRIVE_CDROM,      IDS_DRIVE_CDROM     }
168 };
169
170 #define DRIVE_TYPE_DEFAULT 0
171
172 static void enable_labelserial_box(HWND dialog, int mode)
173 {
174     WINE_TRACE("mode=%d\n", mode);
175
176     switch (mode)
177     {
178         case BOX_MODE_CD_ASSIGN:
179             enable(IDC_RADIO_ASSIGN);
180             disable(IDC_EDIT_DEVICE);
181             disable(IDC_BUTTON_BROWSE_DEVICE);
182             enable(IDC_EDIT_SERIAL);
183             enable(IDC_EDIT_LABEL);
184             enable(IDC_STATIC_SERIAL);
185             enable(IDC_STATIC_LABEL);
186             break;
187
188         case BOX_MODE_CD_AUTODETECT:
189             enable(IDC_RADIO_ASSIGN);
190             enable(IDC_EDIT_DEVICE);
191             enable(IDC_BUTTON_BROWSE_DEVICE);
192             disable(IDC_EDIT_SERIAL);
193             disable(IDC_EDIT_LABEL);
194             disable(IDC_STATIC_SERIAL);
195             disable(IDC_STATIC_LABEL);
196             break;
197
198         case BOX_MODE_NONE:
199             disable(IDC_RADIO_ASSIGN);
200             disable(IDC_EDIT_DEVICE);
201             disable(IDC_BUTTON_BROWSE_DEVICE);
202             disable(IDC_EDIT_SERIAL);
203             disable(IDC_EDIT_LABEL);
204             disable(IDC_STATIC_SERIAL);
205             disable(IDC_STATIC_LABEL);
206             break;
207
208         case BOX_MODE_NORMAL:
209             enable(IDC_RADIO_ASSIGN);
210             disable(IDC_EDIT_DEVICE);
211             disable(IDC_BUTTON_BROWSE_DEVICE);
212             enable(IDC_EDIT_SERIAL);
213             enable(IDC_EDIT_LABEL);
214             enable(IDC_STATIC_SERIAL);
215             enable(IDC_STATIC_LABEL);
216             break;
217     }
218 }
219
220 static int fill_drives_list(HWND dialog)
221 {
222     int count = 0;
223     BOOL drivec_present = FALSE;
224     int i;
225     int prevsel = -1;
226
227     WINE_TRACE("\n");
228
229     updating_ui = TRUE;
230
231     prevsel = lv_get_curr_select(dialog); 
232
233     /* Clear the listbox */
234     SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_DELETEALLITEMS, 0, 0);
235
236     for(i = 0; i < 26; i++)
237     {
238         LVITEMW item;
239         WCHAR *path;
240         char letter[4];
241
242         /* skip over any unused drives */
243         if (!drives[i].in_use)
244             continue;
245
246         if (drives[i].letter == 'C')
247             drivec_present = TRUE;
248
249         letter[0] = 'A' + i;
250         letter[1] = ':';
251         letter[2] = 0;
252
253         item.mask = LVIF_TEXT | LVIF_PARAM;
254         item.iItem = count;
255         item.iSubItem = 0;
256         item.pszText = strdupU2W(letter);
257         item.cchTextMax = lstrlenW(item.pszText);
258         item.lParam = (LPARAM) &drives[i];
259
260         lv_insert_item(dialog, &item);
261         HeapFree(GetProcessHeap(), 0, item.pszText);
262
263         path = strdupU2W(drives[i].unixpath);
264         lv_set_item_text(dialog, count, 1, path);
265         HeapFree(GetProcessHeap(), 0, path);
266
267         count++;
268     }
269
270     WINE_TRACE("loaded %d drives\n", count);
271
272     /* show the warning if there is no Drive C */
273     if (!drivec_present)
274         ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
275     else
276         ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
277
278     lv_set_curr_select(dialog, prevsel == -1 ? 0 : prevsel);
279
280     updating_ui = FALSE;
281     return count;
282 }
283
284 static void on_options_click(HWND dialog)
285 {
286     if (IsDlgButtonChecked(dialog, IDC_SHOW_DOT_FILES) == BST_CHECKED)
287         set_reg_key(config_key, "", "ShowDotFiles", "Y");
288     else
289         set_reg_key(config_key, "", "ShowDotFiles", "N");
290
291     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
292 }
293
294 static void on_add_click(HWND dialog)
295 {
296     /* we should allocate a drive letter automatically. We also need
297        some way to let the user choose the mapping point, for now we
298        will just force them to enter a path automatically, with / being
299        the default. In future we should be able to temporarily map /
300        then invoke the directory chooser dialog. */
301
302     char new = 'C'; /* we skip A and B, they are historically floppy drives */
303     long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
304     int i, c;
305
306     while (mask & (1 << (new - 'A')))
307     {
308         new++;
309         if (new > 'Z')
310         {
311             driveui_msgbox (dialog, IDS_DRIVE_LETTERS_EXCEEDED, MB_OK | MB_ICONEXCLAMATION);
312             return;
313         }
314     }
315
316     WINE_TRACE("allocating drive letter %c\n", new);
317
318     if (new == 'C')
319     {
320         WCHAR label[64];
321         LoadStringW (GetModuleHandle (NULL), IDS_SYSTEM_DRIVE_LABEL, label,
322                      sizeof(label)/sizeof(label[0]));
323         add_drive(new, "../drive_c", NULL, label, 0, DRIVE_FIXED);
324     }
325     else add_drive(new, "/", NULL, NULL, 0, DRIVE_UNKNOWN);
326
327     fill_drives_list(dialog);
328
329     /* select the newly created drive */
330     mask = ~drive_available_mask(0);
331     c = 0;
332     for (i = 0; i < 26; i++)
333     {
334         if ('A' + i == new) break;
335         if ((1 << i) & mask) c++;
336     }
337     lv_set_curr_select(dialog, c);
338
339     SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
340
341     update_controls(dialog);
342     SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
343 }
344
345 static void on_remove_click(HWND dialog)
346 {
347     int itemIndex;
348     struct drive *drive;
349     LVITEMW item;
350
351     itemIndex = lv_get_curr_select(dialog);
352     if (itemIndex == -1) return; /* no selection */
353
354     item.mask = LVIF_PARAM;
355     item.iItem = itemIndex;
356     item.iSubItem = 0;
357
358     lv_get_item(dialog, &item);
359
360     drive = (struct drive *) item.lParam;
361
362     WINE_TRACE("unixpath: %s\n", drive->unixpath);
363
364     if (drive->letter == 'C')
365     {
366         DWORD result = driveui_msgbox (dialog, IDS_CONFIRM_DELETE_C, MB_YESNO | MB_ICONEXCLAMATION);
367         if (result == IDNO) return;
368     }
369
370     delete_drive(drive);
371
372     fill_drives_list(dialog);
373
374     itemIndex = itemIndex - 1;
375     if (itemIndex < 0) itemIndex = 0;
376     lv_set_curr_select(dialog, itemIndex);   /* previous item */
377
378     SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
379
380     update_controls(dialog);
381     SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
382 }
383
384 static void update_controls(HWND dialog)
385 {
386     static const WCHAR emptyW[1];
387     WCHAR *path;
388     unsigned int type;
389     char serial[16];
390     const char *device;
391     int i, selection = -1;
392     LVITEMW item;
393
394     updating_ui = TRUE;
395
396     i = lv_get_curr_select(dialog);
397     if (i == -1)
398     {
399         /* no selection? let's select something for the user. this will re-enter */
400         lv_set_curr_select(dialog, i);
401         return;
402     }
403
404     item.mask = LVIF_PARAM;
405     item.iItem = i;
406     item.iSubItem = 0;
407
408     lv_get_item(dialog, &item);
409     current_drive = (struct drive *) item.lParam;
410
411     WINE_TRACE("Updating sheet for drive %c\n", current_drive->letter);
412
413     /* path */
414     WINE_TRACE("set path control text to '%s'\n", current_drive->unixpath);
415     path = strdupU2W(current_drive->unixpath);
416     set_textW(dialog, IDC_EDIT_PATH, path);
417     HeapFree(GetProcessHeap(), 0, path);
418
419     /* drive type */
420     type = current_drive->type;
421     SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_RESETCONTENT, 0, 0);
422
423     for (i = 0; i < sizeof(type_pairs) / sizeof(struct drive_typemap); i++)
424     {
425         WCHAR driveDesc[64];
426         LoadStringW (GetModuleHandle (NULL), type_pairs[i].idDesc, driveDesc,
427             sizeof(driveDesc)/sizeof(driveDesc[0]));
428         SendDlgItemMessageW (dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0, (LPARAM)driveDesc);
429
430         if (type_pairs[i].sCode ==  type)
431         {
432             selection = i;
433         }
434     }
435
436     if (selection == -1) selection = DRIVE_TYPE_DEFAULT;
437     SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
438
439     EnableWindow( GetDlgItem( dialog, IDC_BUTTON_REMOVE ), (current_drive->letter != 'C') );
440     EnableWindow( GetDlgItem( dialog, IDC_EDIT_PATH ), (current_drive->letter != 'C') );
441     EnableWindow( GetDlgItem( dialog, IDC_BUTTON_BROWSE_PATH ), (current_drive->letter != 'C') );
442     EnableWindow( GetDlgItem( dialog, IDC_COMBO_TYPE ), (current_drive->letter != 'C') );
443
444     /* removeable media properties */
445     set_textW(dialog, IDC_EDIT_LABEL, current_drive->label ? current_drive->label : emptyW);
446
447     /* set serial edit text */
448     sprintf( serial, "%X", current_drive->serial );
449     set_text(dialog, IDC_EDIT_SERIAL, serial);
450
451     /* TODO: get the device here to put into the edit box */
452     device = "Not implemented yet";
453     set_text(dialog, IDC_EDIT_DEVICE, device);
454     device = NULL;
455
456     selection = IDC_RADIO_ASSIGN;
457     if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE))
458     {
459         if (device)
460         {
461             selection = IDC_RADIO_AUTODETECT;
462             enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
463         }
464         else
465         {
466             selection = IDC_RADIO_ASSIGN;
467             enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
468         }
469     }
470     else
471     {
472         enable_labelserial_box(dialog, BOX_MODE_NORMAL);
473         selection = IDC_RADIO_ASSIGN;
474     }
475
476     CheckRadioButton(dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection);
477
478     updating_ui = FALSE;
479
480     return;
481 }
482
483 static void on_edit_changed(HWND dialog, WORD id)
484 {
485     if (updating_ui) return;
486
487     WINE_TRACE("edit id %d changed\n", id);
488
489     switch (id)
490     {
491         case IDC_EDIT_LABEL:
492         {
493             WCHAR *label = get_textW(dialog, id);
494             HeapFree(GetProcessHeap(), 0, current_drive->label);
495             current_drive->label = label;
496             current_drive->modified = TRUE;
497
498             WINE_TRACE("set label to %s\n", wine_dbgstr_w(current_drive->label));
499
500             /* enable the apply button  */
501             SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
502             break;
503         }
504
505         case IDC_EDIT_PATH:
506         {
507             WCHAR *wpath;
508             char *path;
509             int lenW;
510
511             wpath = get_textW(dialog, id);
512             if( (lenW = WideCharToMultiByte(CP_UNIXCP, 0, wpath, -1, NULL, 0, NULL, NULL)) )
513             {
514                 path = HeapAlloc(GetProcessHeap(), 0, lenW);
515                 WideCharToMultiByte(CP_UNIXCP, 0, wpath, -1, path, lenW, NULL, NULL);
516             }
517             else
518             {
519                 path = NULL;
520                 wpath = strdupU2W("drive_c");
521             }
522
523             HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
524             current_drive->unixpath = path ? path : strdupA("drive_c");
525             current_drive->modified = TRUE;
526
527             WINE_TRACE("set path to %s\n", current_drive->unixpath);
528
529             lv_set_item_text(dialog, lv_get_curr_select(dialog), 1,
530                              wpath);
531             HeapFree(GetProcessHeap(), 0, wpath);
532
533             /* enable the apply button  */
534             SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
535             break;
536         }
537
538         case IDC_EDIT_SERIAL:
539         {
540             char *serial;
541
542             serial = get_text(dialog, id);
543             current_drive->serial = serial ? strtoul( serial, NULL, 16 ) : 0;
544             HeapFree(GetProcessHeap(), 0, serial);
545             current_drive->modified = TRUE;
546
547             WINE_TRACE("set serial to %08X\n", current_drive->serial);
548
549             /* enable the apply button  */
550             SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
551             break;
552         }
553
554         case IDC_EDIT_DEVICE:
555         {
556             char *device = get_text(dialog, id);
557             /* TODO: handle device if/when it makes sense to do so.... */
558             HeapFree(GetProcessHeap(), 0, device);
559             break;
560         }
561     }
562 }
563
564 static void get_etched_rect(HWND dialog, RECT *rect)
565 {
566     GetClientRect(dialog, rect);
567
568     /* these dimensions from the labelserial static in En.rc  */
569     rect->top = 265;
570     rect->bottom = 265;
571     rect->left += 25;
572     rect->right -= 25;
573 }
574
575 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
576 static void paint(HWND dialog)
577 {
578     PAINTSTRUCT ps;
579
580     BeginPaint(dialog, &ps);
581
582     if (advanced)
583     {
584         RECT rect;
585
586         get_etched_rect(dialog, &rect);
587
588         DrawEdge(ps.hdc, &rect, EDGE_ETCHED, BF_TOP);
589     }
590
591     EndPaint(dialog, &ps);
592 }
593
594 BOOL browse_for_unix_folder(HWND dialog, WCHAR *pszPath)
595 {
596     static WCHAR wszUnixRootDisplayName[] = 
597         { ':',':','{','C','C','7','0','2','E','B','2','-','7','D','C','5','-','1','1','D','9','-',
598           'C','6','8','7','-','0','0','0','4','2','3','8','A','0','1','C','D','}', 0 };
599     WCHAR pszChoosePath[FILENAME_MAX];
600     BROWSEINFOW bi = {
601         dialog,
602         NULL,
603         NULL,
604         pszChoosePath,
605         0,
606         NULL,
607         0,
608         0
609     };
610     IShellFolder *pDesktop;
611     LPITEMIDLIST pidlUnixRoot, pidlSelectedPath;
612     HRESULT hr;
613    
614     LoadStringW(GetModuleHandle(NULL), IDS_CHOOSE_PATH, pszChoosePath, FILENAME_MAX);
615     
616     hr = SHGetDesktopFolder(&pDesktop);
617     if (FAILED(hr)) return FALSE;
618
619     hr = IShellFolder_ParseDisplayName(pDesktop, NULL, NULL, wszUnixRootDisplayName, NULL, 
620                                        &pidlUnixRoot, NULL);
621     if (FAILED(hr)) {
622         IShellFolder_Release(pDesktop);
623         return FALSE;
624     }
625
626     bi.pidlRoot = pidlUnixRoot;
627     pidlSelectedPath = SHBrowseForFolderW(&bi);
628     SHFree(pidlUnixRoot);
629     
630     if (pidlSelectedPath) {
631         STRRET strSelectedPath;
632         WCHAR *pszSelectedPath;
633         HRESULT hr;
634         
635         hr = IShellFolder_GetDisplayNameOf(pDesktop, pidlSelectedPath, SHGDN_FORPARSING, 
636                                            &strSelectedPath);
637         IShellFolder_Release(pDesktop);
638         if (FAILED(hr)) {
639             SHFree(pidlSelectedPath);
640             return FALSE;
641         }
642
643         hr = StrRetToStrW(&strSelectedPath, pidlSelectedPath, &pszSelectedPath);
644         SHFree(pidlSelectedPath);
645         if (FAILED(hr)) return FALSE;
646
647         lstrcpyW(pszPath, pszSelectedPath);
648         
649         CoTaskMemFree(pszSelectedPath);
650         return TRUE;
651     }
652     return FALSE;
653 }
654
655 static void init_listview_columns(HWND dialog)
656 {
657     LVCOLUMNW listColumn;
658     RECT viewRect;
659     int width;
660     WCHAR column[64];
661
662     GetClientRect(GetDlgItem(dialog, IDC_LIST_DRIVES), &viewRect);
663     width = (viewRect.right - viewRect.left) / 6 - 5;
664
665     LoadStringW (GetModuleHandle (NULL), IDS_COL_DRIVELETTER, column,
666         sizeof(column)/sizeof(column[0]));
667     listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
668     listColumn.pszText = column;
669     listColumn.cchTextMax = lstrlenW (listColumn.pszText);
670     listColumn.cx = width;
671
672     SendDlgItemMessageW (dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMNW, 0, (LPARAM) &listColumn);
673
674     LoadStringW (GetModuleHandle (NULL), IDS_COL_DRIVEMAPPING, column,
675         sizeof(column)/sizeof(column[0]));
676     listColumn.cx = viewRect.right - viewRect.left - width;
677     listColumn.pszText = column;
678     listColumn.cchTextMax = lstrlenW (listColumn.pszText);
679
680     SendDlgItemMessageW (dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMNW, 1, (LPARAM) &listColumn);
681 }
682
683 static void load_drive_options(HWND dialog)
684 {
685     if (!strcmp(get_reg_key(config_key, "", "ShowDotFiles", "N"), "Y"))
686         CheckDlgButton(dialog, IDC_SHOW_DOT_FILES, BST_CHECKED);
687 }
688
689 INT_PTR CALLBACK
690 DriveDlgProc (HWND dialog, UINT msg, WPARAM wParam, LPARAM lParam)
691 {
692     int item;
693     struct drive *drive;
694
695     switch (msg)
696     {
697         case WM_INITDIALOG:
698             init_listview_columns(dialog);
699             if (!load_drives())
700             {
701                 ShowWindow( GetDlgItem( dialog, IDC_STATIC_MOUNTMGR_ERROR ), SW_SHOW );
702                 ShowWindow( GetDlgItem( dialog, IDC_LIST_DRIVES ), SW_HIDE );
703                 ShowWindow( GetDlgItem( dialog, IDC_BUTTON_ADD ), SW_HIDE );
704                 ShowWindow( GetDlgItem( dialog, IDC_BUTTON_REMOVE ), SW_HIDE );
705                 ShowWindow( GetDlgItem( dialog, IDC_BUTTON_AUTODETECT ), SW_HIDE );
706                 ShowWindow( GetDlgItem( dialog, IDC_STATIC_PATH ), SW_HIDE );
707                 ShowWindow( GetDlgItem( dialog, IDC_EDIT_PATH ), SW_HIDE );
708                 ShowWindow( GetDlgItem( dialog, IDC_BUTTON_BROWSE_PATH ), SW_HIDE );
709                 ShowWindow( GetDlgItem( dialog, IDC_COMBO_TYPE ), SW_HIDE );
710                 ShowWindow( GetDlgItem( dialog, IDC_BUTTON_SHOW_HIDE_ADVANCED ), SW_HIDE );
711                 set_advanced(dialog);
712                 break;
713             }
714             ShowWindow( GetDlgItem( dialog, IDC_STATIC_MOUNTMGR_ERROR ), SW_HIDE );
715             load_drive_options(dialog);
716
717             if (!drives[2].in_use)
718                 driveui_msgbox (dialog, IDS_NO_DRIVE_C, MB_OK | MB_ICONEXCLAMATION);
719
720             fill_drives_list(dialog);
721             update_controls(dialog);
722             /* put in non-advanced mode by default  */
723             set_advanced(dialog);
724             break;
725
726         case WM_SHOWWINDOW:
727             set_window_title(dialog);
728             break;
729
730         case WM_PAINT:
731             paint(dialog);
732             break;
733
734         case WM_COMMAND:
735             switch (HIWORD(wParam))
736             {
737                 case EN_CHANGE:
738                     on_edit_changed(dialog, LOWORD(wParam));
739                     break;
740
741                 case BN_CLICKED:
742                     switch (LOWORD(wParam))
743                     {
744                         case IDC_SHOW_DOT_FILES:
745                             on_options_click(dialog);
746                         break;
747                     }
748                     break;
749
750                 case CBN_SELCHANGE:
751                     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
752                     break;
753             }
754
755             switch (LOWORD(wParam))
756             {
757                 case IDC_BUTTON_ADD:
758                     if (HIWORD(wParam) != BN_CLICKED) break;
759                     on_add_click(dialog);
760                     break;
761
762                 case IDC_BUTTON_REMOVE:
763                     if (HIWORD(wParam) != BN_CLICKED) break;
764                     on_remove_click(dialog);
765                     break;
766
767                 case IDC_BUTTON_EDIT:
768                     if (HIWORD(wParam) != BN_CLICKED) break;
769                     item = SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES),  LB_GETCURSEL, 0, 0);
770                     drive = (struct drive *) SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETITEMDATA, item, 0);
771                     break;
772
773                 case IDC_BUTTON_AUTODETECT:
774                     autodetect_drives();
775                     fill_drives_list(dialog);
776                     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
777                     break;
778
779                 case IDC_BUTTON_SHOW_HIDE_ADVANCED:
780                     advanced = !advanced;
781                     set_advanced(dialog);
782                     break;
783
784                 case IDC_BUTTON_BROWSE_PATH:
785                 {
786                     WCHAR szTargetPath[FILENAME_MAX];
787                     if (browse_for_unix_folder(dialog, szTargetPath)) 
788                         set_textW(dialog, IDC_EDIT_PATH, szTargetPath);
789                     break;
790                 }
791
792                 case IDC_RADIO_ASSIGN:
793                 {
794                     WCHAR *str = get_textW(dialog, IDC_EDIT_LABEL);
795                     HeapFree(GetProcessHeap(), 0, current_drive->label);
796                     current_drive->label = str;
797
798                     str = get_textW(dialog, IDC_EDIT_SERIAL);
799                     current_drive->serial = str ? strtoulW( str, NULL, 16 ) : 0;
800                     HeapFree(GetProcessHeap(), 0, str);
801                     current_drive->modified = TRUE;
802
803                     /* TODO: we don't have a device at this point */
804
805                     enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
806
807                     break;
808                 }
809
810
811                 case IDC_COMBO_TYPE:
812                 {
813                     int mode = BOX_MODE_NORMAL;
814                     int selection;
815
816                     if (HIWORD(wParam) != CBN_SELCHANGE) break;
817
818                     selection = SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
819
820                     if (selection >= 0 &&
821                         (type_pairs[selection].sCode == DRIVE_CDROM ||
822                          type_pairs[selection].sCode == DRIVE_REMOVABLE))
823                     {
824                         if (IsDlgButtonChecked(dialog, IDC_RADIO_AUTODETECT))
825                             mode = BOX_MODE_CD_AUTODETECT;
826                         else
827                             mode = BOX_MODE_CD_ASSIGN;
828                     }
829
830                     enable_labelserial_box(dialog, mode);
831
832                     current_drive->type = type_pairs[selection].sCode;
833                     current_drive->modified = TRUE;
834                     break;
835                 }
836
837             }
838             break;
839
840         case WM_NOTIFY:
841             switch (((LPNMHDR)lParam)->code)
842             {
843                 case PSN_KILLACTIVE:
844                     WINE_TRACE("PSN_KILLACTIVE\n");
845                     SetWindowLongPtr(dialog, DWLP_MSGRESULT, FALSE);
846                     break;
847                 case PSN_APPLY:
848                     apply_drive_changes();
849                     SetWindowLongPtr(dialog, DWLP_MSGRESULT, PSNRET_NOERROR);
850                     break;
851                 case PSN_SETACTIVE:
852                     break;
853                 case LVN_ITEMCHANGED:
854                 {
855                     LPNMLISTVIEW lpnm = (LPNMLISTVIEW)lParam;
856                     if (!(lpnm->uOldState & LVIS_SELECTED) &&
857                          (lpnm->uNewState & LVIS_SELECTED))
858                     update_controls(dialog);
859                     break;
860                 }
861             }
862             break;
863     }
864
865     return FALSE;
866 }