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
37 #include <wine/debug.h>
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
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
49 static BOOL advanced = FALSE;
50 static BOOL updating_ui = FALSE;
51 static struct drive* current_drive;
53 static void get_etched_rect(HWND dialog, RECT *rect);
54 static void update_controls(HWND dialog);
56 /**** listview helper functions ****/
58 /* clears the item at index in the listview */
59 static void lv_clear_curr_select(HWND dialog, int index)
61 ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, 0, LVIS_SELECTED);
64 /* selects the item at index in the listview */
65 static void lv_set_curr_select(HWND dialog, int index)
67 /* no more than one item can be selected in our listview */
68 lv_clear_curr_select(dialog, -1);
69 ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, LVIS_SELECTED, LVIS_SELECTED);
72 /* returns the currently selected item in the listview */
73 static int lv_get_curr_select(HWND dialog)
75 return SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
78 /* sets the item in the listview at item->iIndex */
79 static void lv_set_item(HWND dialog, LVITEM *item)
81 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_SETITEM, 0, (LPARAM) item);
84 /* inserts an item into the listview */
85 static void lv_insert_item(HWND dialog, LVITEM *item)
87 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTITEM, 0, (LPARAM) item);
90 /* retrieve the item at index item->iIndex */
91 static void lv_get_item(HWND dialog, LVITEM *item)
93 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETITEM, 0, (LPARAM) item);
96 static void set_advanced(HWND dialog)
105 LoadString(GetModuleHandle(NULL), IDS_SHOW_ADVANCED, text, 256);
110 LoadString(GetModuleHandle(NULL), IDS_HIDE_ADVANCED, text, 256);
113 ShowWindow(GetDlgItem(dialog, IDC_RADIO_AUTODETECT), state);
114 ShowWindow(GetDlgItem(dialog, IDC_RADIO_ASSIGN), state);
115 ShowWindow(GetDlgItem(dialog, IDC_EDIT_LABEL), state);
116 ShowWindow(GetDlgItem(dialog, IDC_EDIT_DEVICE), state);
117 ShowWindow(GetDlgItem(dialog, IDC_STATIC_LABEL), state);
118 ShowWindow(GetDlgItem(dialog, IDC_BUTTON_BROWSE_DEVICE), state);
119 ShowWindow(GetDlgItem(dialog, IDC_EDIT_SERIAL), state);
120 ShowWindow(GetDlgItem(dialog, IDC_STATIC_SERIAL), state);
121 ShowWindow(GetDlgItem(dialog, IDC_LABELSERIAL_STATIC), state);
123 /* update the button text based on the state */
124 SetWindowText(GetDlgItem(dialog, IDC_BUTTON_SHOW_HIDE_ADVANCED), text);
126 /* redraw for the etched line */
127 get_etched_rect(dialog, &rect);
128 InflateRect(&rect, 5, 5);
129 InvalidateRect(dialog, &rect, TRUE);
132 struct drive_typemap {
137 static const struct drive_typemap type_pairs[] = {
138 { DRIVE_FIXED, "Local hard disk" },
139 { DRIVE_REMOTE, "Network share" },
140 { DRIVE_REMOVABLE, "Floppy disk" },
141 { DRIVE_CDROM, "CD-ROM" }
144 #define DRIVE_TYPE_DEFAULT 1
146 static void fill_drive_droplist(long mask, char curletter, HWND dialog)
155 for (i = 0, count = 0, selection = -1, next_letter = -1; i <= 'Z'-'A'; ++i)
157 if (mask & DRIVE_MASK_BIT('A' + i))
162 index = SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName);
164 if (toupper(curletter) == 'A' + i)
169 if (i >= 2 && next_letter == -1)
171 /* default drive is first one of C-Z */
181 selection = next_letter;
184 SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0);
188 static void enable_labelserial_box(HWND dialog, int mode)
190 WINE_TRACE("mode=%d\n", mode);
194 case BOX_MODE_CD_ASSIGN:
195 enable(IDC_RADIO_ASSIGN);
196 disable(IDC_EDIT_DEVICE);
197 disable(IDC_BUTTON_BROWSE_DEVICE);
198 enable(IDC_EDIT_SERIAL);
199 enable(IDC_EDIT_LABEL);
200 enable(IDC_STATIC_SERIAL);
201 enable(IDC_STATIC_LABEL);
204 case BOX_MODE_CD_AUTODETECT:
205 enable(IDC_RADIO_ASSIGN);
206 enable(IDC_EDIT_DEVICE);
207 enable(IDC_BUTTON_BROWSE_DEVICE);
208 disable(IDC_EDIT_SERIAL);
209 disable(IDC_EDIT_LABEL);
210 disable(IDC_STATIC_SERIAL);
211 disable(IDC_STATIC_LABEL);
215 disable(IDC_RADIO_ASSIGN);
216 disable(IDC_EDIT_DEVICE);
217 disable(IDC_BUTTON_BROWSE_DEVICE);
218 disable(IDC_EDIT_SERIAL);
219 disable(IDC_EDIT_LABEL);
220 disable(IDC_STATIC_SERIAL);
221 disable(IDC_STATIC_LABEL);
224 case BOX_MODE_NORMAL:
225 enable(IDC_RADIO_ASSIGN);
226 disable(IDC_EDIT_DEVICE);
227 disable(IDC_BUTTON_BROWSE_DEVICE);
228 enable(IDC_EDIT_SERIAL);
229 enable(IDC_EDIT_LABEL);
230 enable(IDC_STATIC_SERIAL);
231 enable(IDC_STATIC_LABEL);
236 static int fill_drives_list(HWND dialog)
239 BOOL drivec_present = FALSE;
247 prevsel = lv_get_curr_select(dialog);
249 /* Clear the listbox */
250 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_DELETEALLITEMS, 0, 0);
252 for(i = 0; i < 26; i++)
258 /* skip over any unused drives */
259 if (!drives[i].in_use)
262 if (drives[i].letter == 'C')
263 drivec_present = TRUE;
265 len = snprintf(letter, 0, "%c:", 'A' + i);
266 len++; /* add a byte for the trailing null */
268 letter = HeapAlloc(GetProcessHeap(), 0, len);
269 snprintf(letter, len, "%c:", 'A' + i);
271 memset(&item, 0, sizeof(item));
272 item.mask = LVIF_TEXT;
275 item.pszText = letter;
276 item.cchTextMax = lstrlen(item.pszText);
278 lv_insert_item(dialog, &item);
281 item.pszText = drives[i].unixpath;
282 item.cchTextMax = lstrlen(item.pszText);
284 lv_set_item(dialog, &item);
286 item.mask = LVIF_PARAM;
288 item.lParam = (LPARAM) &drives[i];
290 lv_set_item(dialog, &item);
292 HeapFree(GetProcessHeap(), 0, letter);
296 WINE_TRACE("loaded %d drives\n", count);
298 /* show the warning if there is no Drive C */
300 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
302 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
304 lv_set_curr_select(dialog, prevsel == -1 ? 0 : prevsel);
310 static void on_options_click(HWND dialog)
312 if (IsDlgButtonChecked(dialog, IDC_SHOW_DIRSYM_LINK) == BST_CHECKED)
313 set_reg_key("wine", "ShowDirSymLinks", "Y");
315 set_reg_key("wine", "ShowDIrSymLinks", "N");
317 if (IsDlgButtonChecked(dialog, IDC_SHOW_DOT_FILES) == BST_CHECKED)
318 set_reg_key("wine", "ShowDotFiles", "Y");
320 set_reg_key("wine", "ShowDotFiles", "N");
323 static void on_add_click(HWND dialog)
325 /* we should allocate a drive letter automatically. We also need
326 some way to let the user choose the mapping point, for now we
327 will just force them to enter a path automatically, with / being
328 the default. In future we should be able to temporarily map /
329 then invoke the directory chooser dialog. */
331 char new = 'C'; /* we skip A and B, they are historically floppy drives */
332 long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
335 while (mask & (1 << (new - 'A')))
340 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);
345 WINE_TRACE("allocating drive letter %c\n", new);
347 if (new == 'C') add_drive(new, "../drive_c", "System Drive", "", DRIVE_FIXED);
348 else add_drive(new, "/", "", "", DRIVE_FIXED);
350 fill_drives_list(dialog);
352 /* select the newly created drive */
353 mask = ~drive_available_mask(0);
355 for (i = 0; i < 26; i++)
357 if ('A' + i == new) break;
358 if ((1 << i) & mask) c++;
360 lv_set_curr_select(dialog, c);
362 SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
364 update_controls(dialog);
367 static void on_remove_click(HWND dialog)
373 itemIndex = lv_get_curr_select(dialog);
374 if (itemIndex == -1) return; /* no selection */
376 memset(&item, 0, sizeof(item));
377 item.mask = LVIF_PARAM;
378 item.iItem = itemIndex;
381 lv_get_item(dialog, &item);
383 drive = (struct drive *) item.lParam;
385 WINE_ERR("unixpath: %s\n", drive->unixpath);
387 if (drive->letter == 'C')
389 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);
390 if (result == IDNO) return;
395 fill_drives_list(dialog);
397 itemIndex = itemIndex - 1;
398 if (itemIndex < 0) itemIndex = 0;
399 lv_set_curr_select(dialog, itemIndex); /* previous item */
401 SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
403 update_controls(dialog);
406 static void update_controls(HWND dialog)
413 int i, selection = -1;
418 i = lv_get_curr_select(dialog);
421 /* no selection? let's select something for the user. this will re-enter */
422 lv_set_curr_select(dialog, i);
426 memset(&item, 0, sizeof(item));
427 item.mask = LVIF_PARAM;
431 lv_get_item(dialog, &item);
432 current_drive = (struct drive *) item.lParam;
434 WINE_TRACE("Updating sheet for drive %c\n", current_drive->letter);
437 fill_drive_droplist(drive_available_mask(current_drive->letter), current_drive->letter, dialog);
440 path = current_drive->unixpath;
441 WINE_TRACE("set path control text to '%s'\n", path);
442 set_text(dialog, IDC_EDIT_PATH, path);
445 type = current_drive->type;
448 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_RESETCONTENT, 0, 0);
450 for (i = 0; i < sizeof(type_pairs) / sizeof(struct drive_typemap); i++)
452 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0, (LPARAM) type_pairs[i].sDesc);
454 if (type_pairs[i].sCode == type)
460 if (selection == -1) selection = DRIVE_TYPE_DEFAULT;
461 SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
462 } else WINE_WARN("no Type field?\n");
465 /* removeable media properties */
466 label = current_drive->label;
467 set_text(dialog, IDC_EDIT_LABEL, label);
469 /* set serial edit text */
470 serial = current_drive->serial;
471 set_text(dialog, IDC_EDIT_SERIAL, serial);
473 /* TODO: get the device here to put into the edit box */
474 device = "Not implemented yet";
475 set_text(dialog, IDC_EDIT_DEVICE, device);
478 selection = IDC_RADIO_ASSIGN;
479 if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE))
483 selection = IDC_RADIO_AUTODETECT;
484 enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
488 selection = IDC_RADIO_ASSIGN;
489 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
494 enable_labelserial_box(dialog, BOX_MODE_NORMAL);
495 selection = IDC_RADIO_ASSIGN;
498 CheckRadioButton(dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection);
505 static void on_edit_changed(HWND dialog, WORD id)
507 if (updating_ui) return;
509 WINE_TRACE("edit id %d changed\n", id);
511 /* using fill_drives_list here is pretty lazy, but i'm tired
513 fortunately there are only 26 letters in the alphabet, so
514 we don't have to worry about efficiency too much here :) */
522 label = get_text(dialog, id);
523 HeapFree(GetProcessHeap(), 0, current_drive->label);
524 current_drive->label = label ? label : strdupA("");
526 WINE_TRACE("set label to %s\n", current_drive->label);
528 fill_drives_list(dialog);
536 path = get_text(dialog, id);
537 HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
538 current_drive->unixpath = path ? path : strdupA("drive_c");
540 WINE_TRACE("set path to %s\n", current_drive->unixpath);
542 fill_drives_list(dialog);
546 case IDC_EDIT_SERIAL:
550 serial = get_text(dialog, id);
551 HeapFree(GetProcessHeap(), 0, current_drive->serial);
552 current_drive->serial = serial ? serial : strdupA("");
554 WINE_TRACE("set serial to %s", current_drive->serial);
559 case IDC_EDIT_DEVICE:
561 char *device = get_text(dialog, id);
562 /* TODO: handle device if/when it makes sense to do so.... */
563 HeapFree(GetProcessHeap(), 0, device);
564 fill_drives_list(dialog);
570 static void get_etched_rect(HWND dialog, RECT *rect)
572 GetClientRect(dialog, rect);
574 /* these dimensions from the labelserial static in En.rc */
581 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
582 static void paint(HWND dialog)
586 BeginPaint(dialog, &ps);
592 get_etched_rect(dialog, &rect);
594 DrawEdge(ps.hdc, &rect, EDGE_ETCHED, BF_TOP);
597 EndPaint(dialog, &ps);
600 static void browse_for_folder(HWND dialog)
602 static WCHAR wszUnixRootDisplayName[] =
603 { ':',':','{','C','C','7','0','2','E','B','2','-','7','D','C','5','-','1','1','D','9','-',
604 'C','6','8','7','-','0','0','0','4','2','3','8','A','0','1','C','D','}', 0 };
605 char pszChoosePath[256];
616 IShellFolder *pDesktop;
617 LPITEMIDLIST pidlUnixRoot, pidlSelectedPath;
620 LoadString(GetModuleHandle(NULL), IDS_CHOOSE_PATH, pszChoosePath, 256);
622 hr = SHGetDesktopFolder(&pDesktop);
623 if (!SUCCEEDED(hr)) return;
625 hr = pDesktop->lpVtbl->ParseDisplayName(pDesktop, NULL, NULL, wszUnixRootDisplayName, NULL,
626 &pidlUnixRoot, NULL);
627 if (!SUCCEEDED(hr)) {
628 pDesktop->lpVtbl->Release(pDesktop);
632 bi.pidlRoot = pidlUnixRoot;
633 pidlSelectedPath = SHBrowseForFolderA(&bi);
634 SHFree(pidlUnixRoot);
636 if (pidlSelectedPath) {
637 STRRET strSelectedPath;
638 char *pszSelectedPath;
641 hr = pDesktop->lpVtbl->GetDisplayNameOf(pDesktop, pidlSelectedPath, SHGDN_FORPARSING,
643 pDesktop->lpVtbl->Release(pDesktop);
644 if (!SUCCEEDED(hr)) {
645 SHFree(pidlSelectedPath);
649 hr = StrRetToStr(&strSelectedPath, pidlSelectedPath, &pszSelectedPath);
650 SHFree(pidlSelectedPath);
651 if (!SUCCEEDED(hr)) return;
653 HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
654 current_drive->unixpath = strdupA(pszSelectedPath);
655 fill_drives_list(dialog);
656 update_controls(dialog);
658 CoTaskMemFree(pszSelectedPath);
662 static void init_listview_columns(HWND dialog)
668 GetClientRect(GetDlgItem(dialog, IDC_LIST_DRIVES), &viewRect);
669 width = (viewRect.right - viewRect.left) / 6 - 5;
671 listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
672 listColumn.pszText = (char*) "Letter";
673 listColumn.cchTextMax = lstrlen(listColumn.pszText);
674 listColumn.cx = width;
676 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMN, 0, (LPARAM) &listColumn);
678 listColumn.cx = viewRect.right - viewRect.left - width;
679 listColumn.pszText = (char*) "Drive Mapping";
680 listColumn.cchTextMax = lstrlen(listColumn.pszText);
682 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMN, 1, (LPARAM) &listColumn);
685 static void load_drive_options(HWND dialog)
687 if (!strcmp(get_reg_key("wine", "ShowDirSymLinks", "N"), "Y"))
688 CheckDlgButton(dialog, IDC_SHOW_DIRSYM_LINK, BST_CHECKED);
690 if (!strcmp(get_reg_key("wine", "ShowDotFiles", "N"), "Y"))
691 CheckDlgButton(dialog, IDC_SHOW_DOT_FILES, BST_CHECKED);
695 DriveDlgProc (HWND dialog, UINT msg, WPARAM wParam, LPARAM lParam)
703 init_listview_columns(dialog);
705 load_drive_options(dialog);
707 if (!drives[2].in_use)
708 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);
710 fill_drives_list(dialog);
711 update_controls(dialog);
712 /* put in non-advanced mode by default */
713 set_advanced(dialog);
717 set_window_title(dialog);
725 switch (HIWORD(wParam))
728 on_edit_changed(dialog, LOWORD(wParam));
732 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
734 switch (LOWORD(wParam))
736 case IDC_SHOW_DIRSYM_LINK:
737 case IDC_SHOW_DOT_FILES:
738 on_options_click(dialog);
744 switch (LOWORD(wParam))
747 if (HIWORD(wParam) != BN_CLICKED) break;
748 on_add_click(dialog);
751 case IDC_BUTTON_REMOVE:
752 if (HIWORD(wParam) != BN_CLICKED) break;
753 on_remove_click(dialog);
756 case IDC_BUTTON_EDIT:
757 if (HIWORD(wParam) != BN_CLICKED) break;
758 item = SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETCURSEL, 0, 0);
759 drive = (struct drive *) SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETITEMDATA, item, 0);
760 /*DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) drive); */
763 case IDC_BUTTON_AUTODETECT:
765 fill_drives_list(dialog);
768 case IDC_BUTTON_SHOW_HIDE_ADVANCED:
769 advanced = !advanced;
770 set_advanced(dialog);
773 case IDC_BUTTON_BROWSE_PATH:
774 browse_for_folder(dialog);
777 case IDC_RADIO_ASSIGN:
781 str = get_text(dialog, IDC_EDIT_LABEL);
782 HeapFree(GetProcessHeap(), 0, current_drive->label);
783 current_drive->label = str ? str : strdupA("");
785 str = get_text(dialog, IDC_EDIT_SERIAL);
786 HeapFree(GetProcessHeap(), 0, current_drive->serial);
787 current_drive->serial = str ? str : strdupA("");
789 /* TODO: we don't have a device at this point */
791 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
799 int mode = BOX_MODE_NORMAL;
802 if (HIWORD(wParam) != CBN_SELCHANGE) break;
804 selection = SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
806 if (selection == 2 || selection == 3) /* cdrom or floppy */
808 if (IsDlgButtonChecked(dialog, IDC_RADIO_AUTODETECT))
809 mode = BOX_MODE_CD_AUTODETECT;
811 mode = BOX_MODE_CD_ASSIGN;
814 enable_labelserial_box(dialog, mode);
816 current_drive->type = type_pairs[selection].sCode;
824 switch (((LPNMHDR)lParam)->code)
827 WINE_TRACE("PSN_KILLACTIVE\n");
828 SetWindowLongPtr(dialog, DWLP_MSGRESULT, FALSE);
831 apply_drive_changes();
832 SetWindowLongPtr(dialog, DWLP_MSGRESULT, PSNRET_NOERROR);
838 switch (((LPNMITEMACTIVATE)lParam)->hdr.code)
841 update_controls(dialog);