2 * Drive management UI code
4 * Copyright 2003 Mark Westcott
5 * Copyright 2004 Chris Morgan
6 * Copyright 2003-2004 Mike Hearn
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.
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.
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
26 #define WIN32_LEAN_AND_MEAN
36 #include <wine/debug.h>
41 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
43 #define BOX_MODE_CD_ASSIGN 1
44 #define BOX_MODE_CD_AUTODETECT 2
45 #define BOX_MODE_NONE 3
46 #define BOX_MODE_NORMAL 4
48 static BOOL advanced = FALSE;
49 static BOOL updating_ui = FALSE;
50 static struct drive* current_drive;
52 static void get_etched_rect(HWND dialog, RECT *rect);
53 static void update_controls(HWND dialog);
55 static DWORD driveui_msgbox (HWND parent, UINT messageId, DWORD flags)
57 WCHAR* caption = load_string (IDS_WINECFG_TITLE);
58 WCHAR* text = load_string (messageId);
59 DWORD result = MessageBoxW (parent, text, caption, flags);
60 HeapFree (GetProcessHeap(), 0, caption);
61 HeapFree (GetProcessHeap(), 0, text);
65 /**** listview helper functions ****/
67 /* clears the item at index in the listview */
68 static void lv_clear_curr_select(HWND dialog, int index)
70 ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, 0, LVIS_SELECTED);
73 /* selects the item at index in the listview */
74 static void lv_set_curr_select(HWND dialog, int index)
76 /* no more than one item can be selected in our listview */
77 lv_clear_curr_select(dialog, -1);
78 ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, LVIS_SELECTED, LVIS_SELECTED);
81 /* returns the currently selected item in the listview */
82 static int lv_get_curr_select(HWND dialog)
84 return SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
87 /* sets the item in the listview at item->iIndex */
88 static void lv_set_item(HWND dialog, LVITEM *item)
90 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_SETITEM, 0, (LPARAM) item);
93 /* sets specified item's text */
94 static void lv_set_item_text(HWND dialog, int item, int subItem, char *text)
97 if (item < 0 || subItem < 0) return;
98 lvItem.mask = LVIF_TEXT;
100 lvItem.iSubItem = subItem;
101 lvItem.pszText = text;
102 lvItem.cchTextMax = lstrlen(lvItem.pszText);
103 lv_set_item(dialog, &lvItem);
106 /* inserts an item into the listview */
107 static void lv_insert_item(HWND dialog, LVITEM *item)
109 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTITEM, 0, (LPARAM) item);
112 /* retrieve the item at index item->iIndex */
113 static void lv_get_item(HWND dialog, LVITEM *item)
115 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETITEM, 0, (LPARAM) item);
118 static void set_advanced(HWND dialog)
127 LoadString(GetModuleHandle(NULL), IDS_HIDE_ADVANCED, text, 256);
132 LoadString(GetModuleHandle(NULL), IDS_SHOW_ADVANCED, text, 256);
135 ShowWindow(GetDlgItem(dialog, IDC_RADIO_AUTODETECT), state);
136 ShowWindow(GetDlgItem(dialog, IDC_RADIO_ASSIGN), state);
137 ShowWindow(GetDlgItem(dialog, IDC_EDIT_LABEL), state);
138 ShowWindow(GetDlgItem(dialog, IDC_EDIT_DEVICE), state);
139 ShowWindow(GetDlgItem(dialog, IDC_STATIC_LABEL), state);
140 ShowWindow(GetDlgItem(dialog, IDC_BUTTON_BROWSE_DEVICE), state);
141 ShowWindow(GetDlgItem(dialog, IDC_EDIT_SERIAL), state);
142 ShowWindow(GetDlgItem(dialog, IDC_STATIC_SERIAL), state);
143 ShowWindow(GetDlgItem(dialog, IDC_LABELSERIAL_STATIC), state);
144 ShowWindow(GetDlgItem(dialog, IDC_COMBO_TYPE), state);
145 ShowWindow(GetDlgItem(dialog, IDC_STATIC_TYPE), state);
147 /* update the button text based on the state */
148 SetWindowText(GetDlgItem(dialog, IDC_BUTTON_SHOW_HIDE_ADVANCED), text);
150 /* redraw for the etched line */
151 get_etched_rect(dialog, &rect);
152 InflateRect(&rect, 5, 5);
153 InvalidateRect(dialog, &rect, TRUE);
156 struct drive_typemap {
161 static const struct drive_typemap type_pairs[] = {
162 { DRIVE_UNKNOWN, IDS_DRIVE_UNKNOWN },
163 { DRIVE_FIXED, IDS_DRIVE_FIXED },
164 { DRIVE_REMOTE, IDS_DRIVE_REMOTE },
165 { DRIVE_REMOVABLE, IDS_DRIVE_REMOVABLE },
166 { DRIVE_CDROM, IDS_DRIVE_CDROM }
169 #define DRIVE_TYPE_DEFAULT 0
171 static void enable_labelserial_box(HWND dialog, int mode)
173 WINE_TRACE("mode=%d\n", mode);
177 case BOX_MODE_CD_ASSIGN:
178 enable(IDC_RADIO_ASSIGN);
179 disable(IDC_EDIT_DEVICE);
180 disable(IDC_BUTTON_BROWSE_DEVICE);
181 enable(IDC_EDIT_SERIAL);
182 enable(IDC_EDIT_LABEL);
183 enable(IDC_STATIC_SERIAL);
184 enable(IDC_STATIC_LABEL);
187 case BOX_MODE_CD_AUTODETECT:
188 enable(IDC_RADIO_ASSIGN);
189 enable(IDC_EDIT_DEVICE);
190 enable(IDC_BUTTON_BROWSE_DEVICE);
191 disable(IDC_EDIT_SERIAL);
192 disable(IDC_EDIT_LABEL);
193 disable(IDC_STATIC_SERIAL);
194 disable(IDC_STATIC_LABEL);
198 disable(IDC_RADIO_ASSIGN);
199 disable(IDC_EDIT_DEVICE);
200 disable(IDC_BUTTON_BROWSE_DEVICE);
201 disable(IDC_EDIT_SERIAL);
202 disable(IDC_EDIT_LABEL);
203 disable(IDC_STATIC_SERIAL);
204 disable(IDC_STATIC_LABEL);
207 case BOX_MODE_NORMAL:
208 enable(IDC_RADIO_ASSIGN);
209 disable(IDC_EDIT_DEVICE);
210 disable(IDC_BUTTON_BROWSE_DEVICE);
211 enable(IDC_EDIT_SERIAL);
212 enable(IDC_EDIT_LABEL);
213 enable(IDC_STATIC_SERIAL);
214 enable(IDC_STATIC_LABEL);
219 static int fill_drives_list(HWND dialog)
222 BOOL drivec_present = FALSE;
230 prevsel = lv_get_curr_select(dialog);
232 /* Clear the listbox */
233 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_DELETEALLITEMS, 0, 0);
235 for(i = 0; i < 26; i++)
240 /* skip over any unused drives */
241 if (!drives[i].in_use)
244 if (drives[i].letter == 'C')
245 drivec_present = TRUE;
251 item.mask = LVIF_TEXT | LVIF_PARAM;
254 item.pszText = letter;
255 item.cchTextMax = lstrlen(item.pszText);
256 item.lParam = (LPARAM) &drives[i];
258 lv_insert_item(dialog, &item);
259 lv_set_item_text(dialog, count, 1, drives[i].unixpath);
264 WINE_TRACE("loaded %d drives\n", count);
266 /* show the warning if there is no Drive C */
268 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
270 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
272 lv_set_curr_select(dialog, prevsel == -1 ? 0 : prevsel);
278 static void on_options_click(HWND dialog)
280 if (IsDlgButtonChecked(dialog, IDC_SHOW_DOT_FILES) == BST_CHECKED)
281 set_reg_key(config_key, "", "ShowDotFiles", "Y");
283 set_reg_key(config_key, "", "ShowDotFiles", "N");
285 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
288 static void on_add_click(HWND dialog)
290 /* we should allocate a drive letter automatically. We also need
291 some way to let the user choose the mapping point, for now we
292 will just force them to enter a path automatically, with / being
293 the default. In future we should be able to temporarily map /
294 then invoke the directory chooser dialog. */
296 char new = 'C'; /* we skip A and B, they are historically floppy drives */
297 long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
300 while (mask & (1 << (new - 'A')))
305 driveui_msgbox (dialog, IDS_DRIVE_LETTERS_EXCEEDED, MB_OK | MB_ICONEXCLAMATION);
310 WINE_TRACE("allocating drive letter %c\n", new);
315 LoadStringA (GetModuleHandle (NULL), IDS_SYSTEM_DRIVE_LABEL, label,
316 sizeof(label)/sizeof(label[0]));
317 add_drive(new, "../drive_c", label, "", DRIVE_FIXED);
319 else add_drive(new, "/", "", "", DRIVE_UNKNOWN);
321 fill_drives_list(dialog);
323 /* select the newly created drive */
324 mask = ~drive_available_mask(0);
326 for (i = 0; i < 26; i++)
328 if ('A' + i == new) break;
329 if ((1 << i) & mask) c++;
331 lv_set_curr_select(dialog, c);
333 SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
335 update_controls(dialog);
338 static void on_remove_click(HWND dialog)
344 itemIndex = lv_get_curr_select(dialog);
345 if (itemIndex == -1) return; /* no selection */
347 item.mask = LVIF_PARAM;
348 item.iItem = itemIndex;
351 lv_get_item(dialog, &item);
353 drive = (struct drive *) item.lParam;
355 WINE_TRACE("unixpath: %s\n", drive->unixpath);
357 if (drive->letter == 'C')
359 DWORD result = driveui_msgbox (dialog, IDS_CONFIRM_DELETE_C, MB_YESNO | MB_ICONEXCLAMATION);
360 if (result == IDNO) return;
365 fill_drives_list(dialog);
367 itemIndex = itemIndex - 1;
368 if (itemIndex < 0) itemIndex = 0;
369 lv_set_curr_select(dialog, itemIndex); /* previous item */
371 SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
373 update_controls(dialog);
376 static void update_controls(HWND dialog)
383 int i, selection = -1;
388 i = lv_get_curr_select(dialog);
391 /* no selection? let's select something for the user. this will re-enter */
392 lv_set_curr_select(dialog, i);
396 item.mask = LVIF_PARAM;
400 lv_get_item(dialog, &item);
401 current_drive = (struct drive *) item.lParam;
403 WINE_TRACE("Updating sheet for drive %c\n", current_drive->letter);
406 path = current_drive->unixpath;
407 WINE_TRACE("set path control text to '%s'\n", path);
408 set_text(dialog, IDC_EDIT_PATH, path);
411 type = current_drive->type;
412 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_RESETCONTENT, 0, 0);
414 for (i = 0; i < sizeof(type_pairs) / sizeof(struct drive_typemap); i++)
417 LoadStringW (GetModuleHandle (NULL), type_pairs[i].idDesc, driveDesc,
418 sizeof(driveDesc)/sizeof(driveDesc[0]));
419 SendDlgItemMessageW (dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0, (LPARAM)driveDesc);
421 if (type_pairs[i].sCode == type)
427 if (selection == -1) selection = DRIVE_TYPE_DEFAULT;
428 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
430 /* removeable media properties */
431 label = current_drive->label;
432 set_text(dialog, IDC_EDIT_LABEL, label);
434 /* set serial edit text */
435 serial = current_drive->serial;
436 set_text(dialog, IDC_EDIT_SERIAL, serial);
438 /* TODO: get the device here to put into the edit box */
439 device = "Not implemented yet";
440 set_text(dialog, IDC_EDIT_DEVICE, device);
443 selection = IDC_RADIO_ASSIGN;
444 if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE))
448 selection = IDC_RADIO_AUTODETECT;
449 enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
453 selection = IDC_RADIO_ASSIGN;
454 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
459 enable_labelserial_box(dialog, BOX_MODE_NORMAL);
460 selection = IDC_RADIO_ASSIGN;
463 CheckRadioButton(dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection);
470 static void on_edit_changed(HWND dialog, WORD id)
472 if (updating_ui) return;
474 WINE_TRACE("edit id %d changed\n", id);
482 label = get_text(dialog, id);
483 HeapFree(GetProcessHeap(), 0, current_drive->label);
484 current_drive->label = label ? label : strdupA("");
486 WINE_TRACE("set label to %s\n", current_drive->label);
488 /* enable the apply button */
489 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
497 path = get_text(dialog, id);
498 HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
499 current_drive->unixpath = path ? path : strdupA("drive_c");
501 WINE_TRACE("set path to %s\n", current_drive->unixpath);
503 lv_set_item_text(dialog, lv_get_curr_select(dialog), 1,
504 current_drive->unixpath);
506 /* enable the apply button */
507 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
511 case IDC_EDIT_SERIAL:
515 serial = get_text(dialog, id);
516 HeapFree(GetProcessHeap(), 0, current_drive->serial);
517 current_drive->serial = serial ? serial : strdupA("");
519 WINE_TRACE("set serial to %s\n", current_drive->serial);
521 /* enable the apply button */
522 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
526 case IDC_EDIT_DEVICE:
528 char *device = get_text(dialog, id);
529 /* TODO: handle device if/when it makes sense to do so.... */
530 HeapFree(GetProcessHeap(), 0, device);
536 static void get_etched_rect(HWND dialog, RECT *rect)
538 GetClientRect(dialog, rect);
540 /* these dimensions from the labelserial static in En.rc */
547 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
548 static void paint(HWND dialog)
552 BeginPaint(dialog, &ps);
558 get_etched_rect(dialog, &rect);
560 DrawEdge(ps.hdc, &rect, EDGE_ETCHED, BF_TOP);
563 EndPaint(dialog, &ps);
566 BOOL browse_for_unix_folder(HWND dialog, char *pszPath)
568 static WCHAR wszUnixRootDisplayName[] =
569 { ':',':','{','C','C','7','0','2','E','B','2','-','7','D','C','5','-','1','1','D','9','-',
570 'C','6','8','7','-','0','0','0','4','2','3','8','A','0','1','C','D','}', 0 };
571 char pszChoosePath[256];
582 IShellFolder *pDesktop;
583 LPITEMIDLIST pidlUnixRoot, pidlSelectedPath;
586 LoadString(GetModuleHandle(NULL), IDS_CHOOSE_PATH, pszChoosePath, 256);
588 hr = SHGetDesktopFolder(&pDesktop);
589 if (!SUCCEEDED(hr)) return FALSE;
591 hr = IShellFolder_ParseDisplayName(pDesktop, NULL, NULL, wszUnixRootDisplayName, NULL,
592 &pidlUnixRoot, NULL);
593 if (!SUCCEEDED(hr)) {
594 IShellFolder_Release(pDesktop);
598 bi.pidlRoot = pidlUnixRoot;
599 pidlSelectedPath = SHBrowseForFolderA(&bi);
600 SHFree(pidlUnixRoot);
602 if (pidlSelectedPath) {
603 STRRET strSelectedPath;
604 char *pszSelectedPath;
607 hr = IShellFolder_GetDisplayNameOf(pDesktop, pidlSelectedPath, SHGDN_FORPARSING,
609 IShellFolder_Release(pDesktop);
610 if (!SUCCEEDED(hr)) {
611 SHFree(pidlSelectedPath);
615 hr = StrRetToStr(&strSelectedPath, pidlSelectedPath, &pszSelectedPath);
616 SHFree(pidlSelectedPath);
617 if (!SUCCEEDED(hr)) return FALSE;
619 lstrcpy(pszPath, pszSelectedPath);
621 CoTaskMemFree(pszSelectedPath);
627 static void init_listview_columns(HWND dialog)
629 LVCOLUMNW listColumn;
634 GetClientRect(GetDlgItem(dialog, IDC_LIST_DRIVES), &viewRect);
635 width = (viewRect.right - viewRect.left) / 6 - 5;
637 LoadStringW (GetModuleHandle (NULL), IDS_COL_DRIVELETTER, column,
638 sizeof(column)/sizeof(column[0]));
639 listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
640 listColumn.pszText = column;
641 listColumn.cchTextMax = lstrlenW (listColumn.pszText);
642 listColumn.cx = width;
644 SendDlgItemMessageW (dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMNW, 0, (LPARAM) &listColumn);
646 LoadStringW (GetModuleHandle (NULL), IDS_COL_DRIVEMAPPING, column,
647 sizeof(column)/sizeof(column[0]));
648 listColumn.cx = viewRect.right - viewRect.left - width;
649 listColumn.pszText = column;
650 listColumn.cchTextMax = lstrlenW (listColumn.pszText);
652 SendDlgItemMessageW (dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMNW, 1, (LPARAM) &listColumn);
655 static void load_drive_options(HWND dialog)
657 if (!strcmp(get_reg_key(config_key, "", "ShowDotFiles", "N"), "Y"))
658 CheckDlgButton(dialog, IDC_SHOW_DOT_FILES, BST_CHECKED);
662 DriveDlgProc (HWND dialog, UINT msg, WPARAM wParam, LPARAM lParam)
670 init_listview_columns(dialog);
672 load_drive_options(dialog);
674 if (!drives[2].in_use)
675 driveui_msgbox (dialog, IDS_NO_DRIVE_C, MB_OK | MB_ICONEXCLAMATION);
677 fill_drives_list(dialog);
678 update_controls(dialog);
679 /* put in non-advanced mode by default */
680 set_advanced(dialog);
684 set_window_title(dialog);
692 switch (HIWORD(wParam))
695 on_edit_changed(dialog, LOWORD(wParam));
699 switch (LOWORD(wParam))
701 case IDC_SHOW_DOT_FILES:
702 on_options_click(dialog);
708 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
712 switch (LOWORD(wParam))
715 if (HIWORD(wParam) != BN_CLICKED) break;
716 on_add_click(dialog);
719 case IDC_BUTTON_REMOVE:
720 if (HIWORD(wParam) != BN_CLICKED) break;
721 on_remove_click(dialog);
724 case IDC_BUTTON_EDIT:
725 if (HIWORD(wParam) != BN_CLICKED) break;
726 item = SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETCURSEL, 0, 0);
727 drive = (struct drive *) SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETITEMDATA, item, 0);
730 case IDC_BUTTON_AUTODETECT:
732 fill_drives_list(dialog);
733 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
736 case IDC_BUTTON_SHOW_HIDE_ADVANCED:
737 advanced = !advanced;
738 set_advanced(dialog);
741 case IDC_BUTTON_BROWSE_PATH:
743 char szTargetPath[FILENAME_MAX];
744 if (browse_for_unix_folder(dialog, szTargetPath))
745 set_text(dialog, IDC_EDIT_PATH, szTargetPath);
749 case IDC_RADIO_ASSIGN:
753 str = get_text(dialog, IDC_EDIT_LABEL);
754 HeapFree(GetProcessHeap(), 0, current_drive->label);
755 current_drive->label = str ? str : strdupA("");
757 str = get_text(dialog, IDC_EDIT_SERIAL);
758 HeapFree(GetProcessHeap(), 0, current_drive->serial);
759 current_drive->serial = str ? str : strdupA("");
761 /* TODO: we don't have a device at this point */
763 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
771 int mode = BOX_MODE_NORMAL;
774 if (HIWORD(wParam) != CBN_SELCHANGE) break;
776 selection = SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
778 if (selection >= 0 &&
779 (type_pairs[selection].sCode == DRIVE_CDROM ||
780 type_pairs[selection].sCode == DRIVE_REMOVABLE))
782 if (IsDlgButtonChecked(dialog, IDC_RADIO_AUTODETECT))
783 mode = BOX_MODE_CD_AUTODETECT;
785 mode = BOX_MODE_CD_ASSIGN;
788 enable_labelserial_box(dialog, mode);
790 current_drive->type = type_pairs[selection].sCode;
798 switch (((LPNMHDR)lParam)->code)
801 WINE_TRACE("PSN_KILLACTIVE\n");
802 SetWindowLongPtr(dialog, DWLP_MSGRESULT, FALSE);
805 apply_drive_changes();
806 SetWindowLongPtr(dialog, DWLP_MSGRESULT, PSNRET_NOERROR);
810 case LVN_ITEMCHANGED:
812 LPNMLISTVIEW lpnm = (LPNMLISTVIEW)lParam;
813 if (!(lpnm->uOldState & LVIS_SELECTED) &&
814 (lpnm->uNewState & LVIS_SELECTED))
815 update_controls(dialog);