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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include <wine/debug.h>
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
46 #define BOX_MODE_CD_ASSIGN 1
47 #define BOX_MODE_CD_AUTODETECT 2
48 #define BOX_MODE_NONE 3
49 #define BOX_MODE_NORMAL 4
51 static BOOL advanced = FALSE;
52 static BOOL updating_ui = FALSE;
53 static struct drive* current_drive;
55 static void get_etched_rect(HWND dialog, RECT *rect);
56 static void update_controls(HWND dialog);
58 /**** listview helper functions ****/
60 /* clears the item at index in the listview */
61 static void lv_clear_curr_select(HWND dialog, int index)
63 ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, 0, LVIS_SELECTED);
66 /* selects the item at index in the listview */
67 static void lv_set_curr_select(HWND dialog, int index)
69 /* no more than one item can be selected in our listview */
70 lv_clear_curr_select(dialog, -1);
71 ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, LVIS_SELECTED, LVIS_SELECTED);
74 /* returns the currently selected item in the listview */
75 static int lv_get_curr_select(HWND dialog)
77 return SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
80 /* sets the item in the listview at item->iIndex */
81 static void lv_set_item(HWND dialog, LVITEM *item)
83 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_SETITEM, 0, (LPARAM) item);
86 /* inserts an item into the listview */
87 static void lv_insert_item(HWND dialog, LVITEM *item)
89 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTITEM, 0, (LPARAM) item);
92 /* retrieve the item at index item->iIndex */
93 static void lv_get_item(HWND dialog, LVITEM *item)
95 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETITEM, 0, (LPARAM) item);
98 static void set_advanced(HWND dialog)
107 LoadString(GetModuleHandle(NULL), IDS_HIDE_ADVANCED, text, 256);
112 LoadString(GetModuleHandle(NULL), IDS_SHOW_ADVANCED, text, 256);
115 ShowWindow(GetDlgItem(dialog, IDC_RADIO_AUTODETECT), state);
116 ShowWindow(GetDlgItem(dialog, IDC_RADIO_ASSIGN), state);
117 ShowWindow(GetDlgItem(dialog, IDC_EDIT_LABEL), state);
118 ShowWindow(GetDlgItem(dialog, IDC_EDIT_DEVICE), state);
119 ShowWindow(GetDlgItem(dialog, IDC_STATIC_LABEL), state);
120 ShowWindow(GetDlgItem(dialog, IDC_BUTTON_BROWSE_DEVICE), state);
121 ShowWindow(GetDlgItem(dialog, IDC_EDIT_SERIAL), state);
122 ShowWindow(GetDlgItem(dialog, IDC_STATIC_SERIAL), state);
123 ShowWindow(GetDlgItem(dialog, IDC_LABELSERIAL_STATIC), state);
125 /* update the button text based on the state */
126 SetWindowText(GetDlgItem(dialog, IDC_BUTTON_SHOW_HIDE_ADVANCED), text);
128 /* redraw for the etched line */
129 get_etched_rect(dialog, &rect);
130 InflateRect(&rect, 5, 5);
131 InvalidateRect(dialog, &rect, TRUE);
134 struct drive_typemap {
139 static const struct drive_typemap type_pairs[] = {
140 { DRIVE_FIXED, "Local hard disk" },
141 { DRIVE_REMOTE, "Network share" },
142 { DRIVE_REMOVABLE, "Floppy disk" },
143 { DRIVE_CDROM, "CD-ROM" }
146 #define DRIVE_TYPE_DEFAULT 1
148 static void fill_drive_droplist(long mask, char curletter, HWND dialog)
157 for (i = 0, count = 0, selection = -1, next_letter = -1; i <= 'Z'-'A'; ++i)
159 if (mask & DRIVE_MASK_BIT('A' + i))
164 index = SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName);
166 if (toupper(curletter) == 'A' + i)
171 if (i >= 2 && next_letter == -1)
173 /* default drive is first one of C-Z */
183 selection = next_letter;
186 SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0);
190 static void enable_labelserial_box(HWND dialog, int mode)
192 WINE_TRACE("mode=%d\n", mode);
196 case BOX_MODE_CD_ASSIGN:
197 enable(IDC_RADIO_ASSIGN);
198 disable(IDC_EDIT_DEVICE);
199 disable(IDC_BUTTON_BROWSE_DEVICE);
200 enable(IDC_EDIT_SERIAL);
201 enable(IDC_EDIT_LABEL);
202 enable(IDC_STATIC_SERIAL);
203 enable(IDC_STATIC_LABEL);
206 case BOX_MODE_CD_AUTODETECT:
207 enable(IDC_RADIO_ASSIGN);
208 enable(IDC_EDIT_DEVICE);
209 enable(IDC_BUTTON_BROWSE_DEVICE);
210 disable(IDC_EDIT_SERIAL);
211 disable(IDC_EDIT_LABEL);
212 disable(IDC_STATIC_SERIAL);
213 disable(IDC_STATIC_LABEL);
217 disable(IDC_RADIO_ASSIGN);
218 disable(IDC_EDIT_DEVICE);
219 disable(IDC_BUTTON_BROWSE_DEVICE);
220 disable(IDC_EDIT_SERIAL);
221 disable(IDC_EDIT_LABEL);
222 disable(IDC_STATIC_SERIAL);
223 disable(IDC_STATIC_LABEL);
226 case BOX_MODE_NORMAL:
227 enable(IDC_RADIO_ASSIGN);
228 disable(IDC_EDIT_DEVICE);
229 disable(IDC_BUTTON_BROWSE_DEVICE);
230 enable(IDC_EDIT_SERIAL);
231 enable(IDC_EDIT_LABEL);
232 enable(IDC_STATIC_SERIAL);
233 enable(IDC_STATIC_LABEL);
238 static int fill_drives_list(HWND dialog)
241 BOOL drivec_present = FALSE;
249 prevsel = lv_get_curr_select(dialog);
251 /* Clear the listbox */
252 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_DELETEALLITEMS, 0, 0);
254 for(i = 0; i < 26; i++)
260 /* skip over any unused drives */
261 if (!drives[i].in_use)
264 if (drives[i].letter == 'C')
265 drivec_present = TRUE;
267 len = snprintf(letter, 0, "%c:", 'A' + i);
268 len++; /* add a byte for the trailing null */
270 letter = HeapAlloc(GetProcessHeap(), 0, len);
271 snprintf(letter, len, "%c:", 'A' + i);
273 memset(&item, 0, sizeof(item));
274 item.mask = LVIF_TEXT;
277 item.pszText = letter;
278 item.cchTextMax = lstrlen(item.pszText);
280 lv_insert_item(dialog, &item);
283 item.pszText = drives[i].unixpath;
284 item.cchTextMax = lstrlen(item.pszText);
286 lv_set_item(dialog, &item);
288 item.mask = LVIF_PARAM;
290 item.lParam = (LPARAM) &drives[i];
292 lv_set_item(dialog, &item);
294 HeapFree(GetProcessHeap(), 0, letter);
298 WINE_TRACE("loaded %d drives\n", count);
300 /* show the warning if there is no Drive C */
302 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
304 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
306 lv_set_curr_select(dialog, prevsel == -1 ? 0 : prevsel);
312 static void on_options_click(HWND dialog)
314 if (IsDlgButtonChecked(dialog, IDC_SHOW_DIRSYM_LINK) == BST_CHECKED)
315 set_reg_key(config_key, "", "ShowDirSymLinks", "Y");
317 set_reg_key(config_key, "", "ShowDirSymLinks", "N");
319 if (IsDlgButtonChecked(dialog, IDC_SHOW_DOT_FILES) == BST_CHECKED)
320 set_reg_key(config_key, "", "ShowDotFiles", "Y");
322 set_reg_key(config_key, "", "ShowDotFiles", "N");
325 static void on_add_click(HWND dialog)
327 /* we should allocate a drive letter automatically. We also need
328 some way to let the user choose the mapping point, for now we
329 will just force them to enter a path automatically, with / being
330 the default. In future we should be able to temporarily map /
331 then invoke the directory chooser dialog. */
333 char new = 'C'; /* we skip A and B, they are historically floppy drives */
334 long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
337 while (mask & (1 << (new - 'A')))
342 MessageBox(dialog, "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);
347 WINE_TRACE("allocating drive letter %c\n", new);
349 if (new == 'C') add_drive(new, "../drive_c", "System Drive", "", DRIVE_FIXED);
350 else add_drive(new, "/", "", "", DRIVE_FIXED);
352 fill_drives_list(dialog);
354 /* select the newly created drive */
355 mask = ~drive_available_mask(0);
357 for (i = 0; i < 26; i++)
359 if ('A' + i == new) break;
360 if ((1 << i) & mask) c++;
362 lv_set_curr_select(dialog, c);
364 SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
366 update_controls(dialog);
369 static void on_remove_click(HWND dialog)
375 itemIndex = lv_get_curr_select(dialog);
376 if (itemIndex == -1) return; /* no selection */
378 memset(&item, 0, sizeof(item));
379 item.mask = LVIF_PARAM;
380 item.iItem = itemIndex;
383 lv_get_item(dialog, &item);
385 drive = (struct drive *) item.lParam;
387 WINE_ERR("unixpath: %s\n", drive->unixpath);
389 if (drive->letter == 'C')
391 DWORD result = MessageBox(dialog, "Are you sure you want to delete drive C?\n\nMost Windows applications expect drive C to exist, and will die messily if it doesn't. If you proceed remember to recreate it!", "", MB_YESNO | MB_ICONEXCLAMATION);
392 if (result == IDNO) return;
397 fill_drives_list(dialog);
399 itemIndex = itemIndex - 1;
400 if (itemIndex < 0) itemIndex = 0;
401 lv_set_curr_select(dialog, itemIndex); /* previous item */
403 SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
405 update_controls(dialog);
408 static void update_controls(HWND dialog)
415 int i, selection = -1;
420 i = lv_get_curr_select(dialog);
423 /* no selection? let's select something for the user. this will re-enter */
424 lv_set_curr_select(dialog, i);
428 memset(&item, 0, sizeof(item));
429 item.mask = LVIF_PARAM;
433 lv_get_item(dialog, &item);
434 current_drive = (struct drive *) item.lParam;
436 WINE_TRACE("Updating sheet for drive %c\n", current_drive->letter);
439 fill_drive_droplist(drive_available_mask(current_drive->letter), current_drive->letter, dialog);
442 path = current_drive->unixpath;
443 WINE_TRACE("set path control text to '%s'\n", path);
444 set_text(dialog, IDC_EDIT_PATH, path);
447 type = current_drive->type;
450 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_RESETCONTENT, 0, 0);
452 for (i = 0; i < sizeof(type_pairs) / sizeof(struct drive_typemap); i++)
454 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0, (LPARAM) type_pairs[i].sDesc);
456 if (type_pairs[i].sCode == type)
462 if (selection == -1) selection = DRIVE_TYPE_DEFAULT;
463 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
464 } else WINE_WARN("no Type field?\n");
467 /* removeable media properties */
468 label = current_drive->label;
469 set_text(dialog, IDC_EDIT_LABEL, label);
471 /* set serial edit text */
472 serial = current_drive->serial;
473 set_text(dialog, IDC_EDIT_SERIAL, serial);
475 /* TODO: get the device here to put into the edit box */
476 device = "Not implemented yet";
477 set_text(dialog, IDC_EDIT_DEVICE, device);
480 selection = IDC_RADIO_ASSIGN;
481 if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE))
485 selection = IDC_RADIO_AUTODETECT;
486 enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
490 selection = IDC_RADIO_ASSIGN;
491 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
496 enable_labelserial_box(dialog, BOX_MODE_NORMAL);
497 selection = IDC_RADIO_ASSIGN;
500 CheckRadioButton(dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection);
507 static void on_edit_changed(HWND dialog, WORD id)
509 if (updating_ui) return;
511 WINE_TRACE("edit id %d changed\n", id);
513 /* using fill_drives_list here is pretty lazy, but i'm tired
515 fortunately there are only 26 letters in the alphabet, so
516 we don't have to worry about efficiency too much here :) */
524 label = get_text(dialog, id);
525 HeapFree(GetProcessHeap(), 0, current_drive->label);
526 current_drive->label = label ? label : strdupA("");
528 WINE_TRACE("set label to %s\n", current_drive->label);
530 fill_drives_list(dialog);
538 path = get_text(dialog, id);
539 HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
540 current_drive->unixpath = path ? path : strdupA("drive_c");
542 WINE_TRACE("set path to %s\n", current_drive->unixpath);
544 fill_drives_list(dialog);
548 case IDC_EDIT_SERIAL:
552 serial = get_text(dialog, id);
553 HeapFree(GetProcessHeap(), 0, current_drive->serial);
554 current_drive->serial = serial ? serial : strdupA("");
556 WINE_TRACE("set serial to %s", current_drive->serial);
561 case IDC_EDIT_DEVICE:
563 char *device = get_text(dialog, id);
564 /* TODO: handle device if/when it makes sense to do so.... */
565 HeapFree(GetProcessHeap(), 0, device);
566 fill_drives_list(dialog);
572 static void get_etched_rect(HWND dialog, RECT *rect)
574 GetClientRect(dialog, rect);
576 /* these dimensions from the labelserial static in En.rc */
583 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
584 static void paint(HWND dialog)
588 BeginPaint(dialog, &ps);
594 get_etched_rect(dialog, &rect);
596 DrawEdge(ps.hdc, &rect, EDGE_ETCHED, BF_TOP);
599 EndPaint(dialog, &ps);
602 static void browse_for_folder(HWND dialog)
604 static WCHAR wszUnixRootDisplayName[] =
605 { ':',':','{','C','C','7','0','2','E','B','2','-','7','D','C','5','-','1','1','D','9','-',
606 'C','6','8','7','-','0','0','0','4','2','3','8','A','0','1','C','D','}', 0 };
607 char pszChoosePath[256];
618 IShellFolder *pDesktop;
619 LPITEMIDLIST pidlUnixRoot, pidlSelectedPath;
622 LoadString(GetModuleHandle(NULL), IDS_CHOOSE_PATH, pszChoosePath, 256);
624 hr = SHGetDesktopFolder(&pDesktop);
625 if (!SUCCEEDED(hr)) return;
627 hr = IShellFolder_ParseDisplayName(pDesktop, NULL, NULL, wszUnixRootDisplayName, NULL,
628 &pidlUnixRoot, NULL);
629 if (!SUCCEEDED(hr)) {
630 IShellFolder_Release(pDesktop);
634 bi.pidlRoot = pidlUnixRoot;
635 pidlSelectedPath = SHBrowseForFolderA(&bi);
636 SHFree(pidlUnixRoot);
638 if (pidlSelectedPath) {
639 STRRET strSelectedPath;
640 char *pszSelectedPath;
643 hr = IShellFolder_GetDisplayNameOf(pDesktop, pidlSelectedPath, SHGDN_FORPARSING,
645 IShellFolder_Release(pDesktop);
646 if (!SUCCEEDED(hr)) {
647 SHFree(pidlSelectedPath);
651 hr = StrRetToStr(&strSelectedPath, pidlSelectedPath, &pszSelectedPath);
652 SHFree(pidlSelectedPath);
653 if (!SUCCEEDED(hr)) return;
655 HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
656 current_drive->unixpath = strdupA(pszSelectedPath);
657 fill_drives_list(dialog);
658 update_controls(dialog);
660 CoTaskMemFree(pszSelectedPath);
664 static void init_listview_columns(HWND dialog)
670 GetClientRect(GetDlgItem(dialog, IDC_LIST_DRIVES), &viewRect);
671 width = (viewRect.right - viewRect.left) / 6 - 5;
673 listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
674 listColumn.pszText = (char*) "Letter";
675 listColumn.cchTextMax = lstrlen(listColumn.pszText);
676 listColumn.cx = width;
678 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMN, 0, (LPARAM) &listColumn);
680 listColumn.cx = viewRect.right - viewRect.left - width;
681 listColumn.pszText = (char*) "Drive Mapping";
682 listColumn.cchTextMax = lstrlen(listColumn.pszText);
684 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMN, 1, (LPARAM) &listColumn);
687 static void load_drive_options(HWND dialog)
689 if (!strcmp(get_reg_key(config_key, "", "ShowDirSymLinks", "N"), "Y"))
690 CheckDlgButton(dialog, IDC_SHOW_DIRSYM_LINK, BST_CHECKED);
692 if (!strcmp(get_reg_key(config_key, "", "ShowDotFiles", "N"), "Y"))
693 CheckDlgButton(dialog, IDC_SHOW_DOT_FILES, BST_CHECKED);
697 DriveDlgProc (HWND dialog, UINT msg, WPARAM wParam, LPARAM lParam)
705 init_listview_columns(dialog);
707 load_drive_options(dialog);
709 if (!drives[2].in_use)
710 MessageBox(dialog, "You don't have a drive C. This is not so great.\n\nRemember to click 'Add' in the Drives tab to create one!\n", "", MB_OK | MB_ICONEXCLAMATION);
712 fill_drives_list(dialog);
713 update_controls(dialog);
714 /* put in non-advanced mode by default */
715 set_advanced(dialog);
719 set_window_title(dialog);
727 switch (HIWORD(wParam))
730 on_edit_changed(dialog, LOWORD(wParam));
734 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
736 switch (LOWORD(wParam))
738 case IDC_SHOW_DIRSYM_LINK:
739 case IDC_SHOW_DOT_FILES:
740 on_options_click(dialog);
746 switch (LOWORD(wParam))
749 if (HIWORD(wParam) != BN_CLICKED) break;
750 on_add_click(dialog);
753 case IDC_BUTTON_REMOVE:
754 if (HIWORD(wParam) != BN_CLICKED) break;
755 on_remove_click(dialog);
758 case IDC_BUTTON_EDIT:
759 if (HIWORD(wParam) != BN_CLICKED) break;
760 item = SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETCURSEL, 0, 0);
761 drive = (struct drive *) SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETITEMDATA, item, 0);
762 /*DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) drive); */
765 case IDC_BUTTON_AUTODETECT:
767 fill_drives_list(dialog);
770 case IDC_BUTTON_SHOW_HIDE_ADVANCED:
771 advanced = !advanced;
772 set_advanced(dialog);
775 case IDC_BUTTON_BROWSE_PATH:
776 browse_for_folder(dialog);
779 case IDC_RADIO_ASSIGN:
783 str = get_text(dialog, IDC_EDIT_LABEL);
784 HeapFree(GetProcessHeap(), 0, current_drive->label);
785 current_drive->label = str ? str : strdupA("");
787 str = get_text(dialog, IDC_EDIT_SERIAL);
788 HeapFree(GetProcessHeap(), 0, current_drive->serial);
789 current_drive->serial = str ? str : strdupA("");
791 /* TODO: we don't have a device at this point */
793 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
801 int mode = BOX_MODE_NORMAL;
804 if (HIWORD(wParam) != CBN_SELCHANGE) break;
806 selection = SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
808 if (selection == 2 || selection == 3) /* cdrom or floppy */
810 if (IsDlgButtonChecked(dialog, IDC_RADIO_AUTODETECT))
811 mode = BOX_MODE_CD_AUTODETECT;
813 mode = BOX_MODE_CD_ASSIGN;
816 enable_labelserial_box(dialog, mode);
818 current_drive->type = type_pairs[selection].sCode;
826 switch (((LPNMHDR)lParam)->code)
829 WINE_TRACE("PSN_KILLACTIVE\n");
830 SetWindowLongPtr(dialog, DWLP_MSGRESULT, FALSE);
833 apply_drive_changes();
834 SetWindowLongPtr(dialog, DWLP_MSGRESULT, PSNRET_NOERROR);
840 switch (((LPNMITEMACTIVATE)lParam)->hdr.code)
843 update_controls(dialog);