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 static void set_advanced(HWND dialog
)
62 /* FIXME: internationalization */
66 text
= "&Hide Advanced";
71 text
= "&Show Advanced";
74 ShowWindow(GetDlgItem(dialog
, IDC_RADIO_AUTODETECT
), state
);
75 ShowWindow(GetDlgItem(dialog
, IDC_RADIO_ASSIGN
), state
);
76 ShowWindow(GetDlgItem(dialog
, IDC_EDIT_LABEL
), state
);
77 ShowWindow(GetDlgItem(dialog
, IDC_EDIT_DEVICE
), state
);
78 ShowWindow(GetDlgItem(dialog
, IDC_STATIC_LABEL
), state
);
79 ShowWindow(GetDlgItem(dialog
, IDC_BUTTON_BROWSE_DEVICE
), state
);
80 ShowWindow(GetDlgItem(dialog
, IDC_EDIT_SERIAL
), state
);
81 ShowWindow(GetDlgItem(dialog
, IDC_STATIC_SERIAL
), state
);
82 ShowWindow(GetDlgItem(dialog
, IDC_LABELSERIAL_STATIC
), state
);
84 /* update the button text based on the state */
85 SetWindowText(GetDlgItem(dialog
, IDC_BUTTON_SHOW_HIDE_ADVANCED
), text
);
87 /* redraw for the etched line */
88 get_etched_rect(dialog
, &rect
);
89 InflateRect(&rect
, 5, 5);
90 InvalidateRect(dialog
, &rect
, TRUE
);
93 struct drive_typemap
{
98 static struct drive_typemap type_pairs
[] = {
99 { DRIVE_FIXED
, "Local hard disk" },
100 { DRIVE_REMOTE
, "Network share" },
101 { DRIVE_REMOVABLE
, "Floppy disk" },
102 { DRIVE_CDROM
, "CD-ROM" }
105 #define DRIVE_TYPE_DEFAULT 1
107 void fill_drive_droplist(long mask
, char curletter
, HWND dialog
)
113 char sName
[4] = "A:";
115 for (i
= 0, count
= 0, selection
= -1, next_letter
= -1; i
<= 'Z'-'A'; ++i
)
117 if (mask
& DRIVE_MASK_BIT('A' + i
))
122 index
= SendDlgItemMessage(dialog
, IDC_COMBO_LETTER
, CB_ADDSTRING
, 0, (LPARAM
) sName
);
124 if (toupper(curletter
) == 'A' + i
)
129 if (i
>= 2 && next_letter
== -1)
131 /* default drive is first one of C-Z */
141 selection
= next_letter
;
144 SendDlgItemMessage(dialog
, IDC_COMBO_LETTER
, CB_SETCURSEL
, selection
, 0);
148 void enable_labelserial_box(HWND dialog
, int mode
)
150 WINE_TRACE("mode=%d\n", mode
);
154 case BOX_MODE_CD_ASSIGN
:
155 enable(IDC_RADIO_ASSIGN
);
156 disable(IDC_EDIT_DEVICE
);
157 disable(IDC_BUTTON_BROWSE_DEVICE
);
158 enable(IDC_EDIT_SERIAL
);
159 enable(IDC_EDIT_LABEL
);
160 enable(IDC_STATIC_SERIAL
);
161 enable(IDC_STATIC_LABEL
);
164 case BOX_MODE_CD_AUTODETECT
:
165 enable(IDC_RADIO_ASSIGN
);
166 enable(IDC_EDIT_DEVICE
);
167 enable(IDC_BUTTON_BROWSE_DEVICE
);
168 disable(IDC_EDIT_SERIAL
);
169 disable(IDC_EDIT_LABEL
);
170 disable(IDC_STATIC_SERIAL
);
171 disable(IDC_STATIC_LABEL
);
175 disable(IDC_RADIO_ASSIGN
);
176 disable(IDC_EDIT_DEVICE
);
177 disable(IDC_BUTTON_BROWSE_DEVICE
);
178 disable(IDC_EDIT_SERIAL
);
179 disable(IDC_EDIT_LABEL
);
180 disable(IDC_STATIC_SERIAL
);
181 disable(IDC_STATIC_LABEL
);
184 case BOX_MODE_NORMAL
:
185 enable(IDC_RADIO_ASSIGN
);
186 disable(IDC_EDIT_DEVICE
);
187 disable(IDC_BUTTON_BROWSE_DEVICE
);
188 enable(IDC_EDIT_SERIAL
);
189 enable(IDC_EDIT_LABEL
);
190 enable(IDC_STATIC_SERIAL
);
191 enable(IDC_STATIC_LABEL
);
196 int fill_drives_list(HWND dialog
)
199 BOOL drivec_present
= FALSE
;
207 prevsel
= SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
209 /* Clear the listbox */
210 SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_RESETCONTENT
, 0, 0);
212 for(i
= 0; i
< 26; i
++)
218 /* skip over any unused drives */
219 if (!drives
[i
].in_use
)
222 if (drives
[i
].letter
== 'C')
223 drivec_present
= TRUE
;
225 len
= snprintf(title
, 0, "%c: %s", 'A' + i
,
227 len
++; /* add a byte for the trailing null */
229 title
= HeapAlloc(GetProcessHeap(), 0, len
);
231 /* the %s in the item label will be replaced by the drive letter, so -1, then
232 -2 for the second %s which will be expanded to the label, finally + 1 for terminating #0 */
233 snprintf(title
, len
, "%c: %s", 'A' + i
,
236 WINE_TRACE("title is '%s'\n", title
);
238 /* the first SendMessage call adds the string and returns the index, the second associates that index with it */
239 index
= SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_ADDSTRING
,(WPARAM
) -1, (LPARAM
) title
);
240 SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_SETITEMDATA
, index
, (LPARAM
) &drives
[i
]);
242 HeapFree(GetProcessHeap(), 0, title
);
246 WINE_TRACE("loaded %d drives\n", count
);
248 /* show the warning if there is no Drive C */
250 ShowWindow(GetDlgItem(dialog
, IDS_DRIVE_NO_C
), SW_NORMAL
);
252 ShowWindow(GetDlgItem(dialog
, IDS_DRIVE_NO_C
), SW_HIDE
);
254 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, prevsel
== -1 ? 0 : prevsel
, 0);
261 void on_add_click(HWND dialog
)
263 /* we should allocate a drive letter automatically. We also need
264 some way to let the user choose the mapping point, for now we
265 will just force them to enter a path automatically, with / being
266 the default. In future we should be able to temporarily map /
267 then invoke the directory chooser dialog. */
269 char new = 'C'; /* we skip A and B, they are historically floppy drives */
270 long mask
= ~drive_available_mask(0); /* the mask is now which drives aren't available */
273 while (mask
& (1 << (new - 'A')))
278 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
);
283 WINE_TRACE("allocating drive letter %c\n", new);
285 if (new == 'C') add_drive(new, "../drive_c", "System Drive", "", DRIVE_FIXED
);
286 else add_drive(new, "/", "", "", DRIVE_FIXED
);
288 fill_drives_list(dialog
);
290 /* select the newly created drive */
291 mask
= ~drive_available_mask(0);
293 for (i
= 0; i
< 26; i
++)
295 if ('A' + i
== new) break;
296 if ((1 << i
) & mask
) c
++;
298 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, c
, 0);
300 SetFocus(GetDlgItem(dialog
, IDC_LIST_DRIVES
));
302 update_controls(dialog
);
305 void on_remove_click(HWND dialog
)
310 item
= SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
311 if (item
== -1) return; /* no selection */
313 drive
= (struct drive
*) SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETITEMDATA
, item
, 0);
315 if (drive
->letter
== 'C')
317 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
);
318 if (result
== IDNO
) return;
323 fill_drives_list(dialog
);
326 if (item
< 0) item
= 0;
327 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, item
, 0); /* previous item */
329 SetFocus(GetDlgItem(dialog
, IDC_LIST_DRIVES
));
331 update_controls(dialog
);
334 static void update_controls(HWND dialog
)
341 int i
, selection
= -1;
345 i
= SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
348 /* no selection? let's select something for the user. this will re-enter */
349 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, 0, 0);
352 current_drive
= (struct drive
*) SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETITEMDATA
, i
, 0);
354 WINE_TRACE("Updating sheet for drive %c\n", current_drive
->letter
);
357 fill_drive_droplist(drive_available_mask(current_drive
->letter
), current_drive
->letter
, dialog
);
360 path
= current_drive
->unixpath
;
361 WINE_TRACE("set path control text to '%s'\n", path
);
362 set_text(dialog
, IDC_EDIT_PATH
, path
);
365 type
= current_drive
->type
;
368 for (i
= 0; i
< sizeof(type_pairs
) / sizeof(struct drive_typemap
); i
++)
370 SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_ADDSTRING
, 0, (LPARAM
) type_pairs
[i
].sDesc
);
372 if (type_pairs
[i
].sCode
== type
)
378 if (selection
== -1) selection
= DRIVE_TYPE_DEFAULT
;
379 SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_SETCURSEL
, selection
, 0);
380 } else WINE_WARN("no Type field?\n");
383 /* removeable media properties */
384 label
= current_drive
->label
;
385 set_text(dialog
, IDC_EDIT_LABEL
, label
);
387 /* set serial edit text */
388 serial
= current_drive
->serial
;
389 set_text(dialog
, IDC_EDIT_SERIAL
, serial
);
391 /* TODO: get the device here to put into the edit box */
392 device
= "Not implemented yet";
393 set_text(dialog
, IDC_EDIT_DEVICE
, device
);
396 selection
= IDC_RADIO_ASSIGN
;
397 if ((type
== DRIVE_CDROM
) || (type
== DRIVE_REMOVABLE
))
401 selection
= IDC_RADIO_AUTODETECT
;
402 enable_labelserial_box(dialog
, BOX_MODE_CD_AUTODETECT
);
406 selection
= IDC_RADIO_ASSIGN
;
407 enable_labelserial_box(dialog
, BOX_MODE_CD_ASSIGN
);
412 enable_labelserial_box(dialog
, BOX_MODE_NORMAL
);
413 selection
= IDC_RADIO_ASSIGN
;
416 CheckRadioButton(dialog
, IDC_RADIO_AUTODETECT
, IDC_RADIO_ASSIGN
, selection
);
423 void on_edit_changed(HWND dialog
, WORD id
)
425 if (updating_ui
) return;
427 WINE_TRACE("edit id %d changed\n", id
);
429 /* using fill_drives_list here is pretty lazy, but i'm tired
431 fortunately there are only 26 letters in the alphabet, so
432 we don't have to worry about efficiency too much here :) */
440 label
= get_text(dialog
, id
);
441 HeapFree(GetProcessHeap(), 0, current_drive
->label
);
442 current_drive
->label
= label
? label
: strdupA("");
444 WINE_TRACE("set label to %s\n", current_drive
->label
);
446 fill_drives_list(dialog
);
454 path
= get_text(dialog
, id
);
455 HeapFree(GetProcessHeap(), 0, current_drive
->unixpath
);
456 current_drive
->unixpath
= path
? path
: strdupA("drive_c");
458 WINE_TRACE("set path to %s\n", current_drive
->unixpath
);
460 fill_drives_list(dialog
);
464 case IDC_EDIT_SERIAL
:
468 serial
= get_text(dialog
, id
);
469 HeapFree(GetProcessHeap(), 0, current_drive
->serial
);
470 current_drive
->serial
= serial
? serial
: strdupA("");
472 WINE_TRACE("set serial to %s", current_drive
->serial
);
477 case IDC_EDIT_DEVICE
:
479 char *device
= get_text(dialog
, id
);
480 /* TODO: handle device if/when it makes sense to do so.... */
481 HeapFree(GetProcessHeap(), 0, device
);
482 fill_drives_list(dialog
);
488 static void get_etched_rect(HWND dialog
, RECT
*rect
)
490 GetClientRect(dialog
, rect
);
492 /* these dimensions from the labelserial static in En.rc */
499 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
500 static void paint(HWND dialog
)
504 BeginPaint(dialog
, &ps
);
510 get_etched_rect(dialog
, &rect
);
512 DrawEdge(ps
.hdc
, &rect
, EDGE_ETCHED
, BF_TOP
);
515 EndPaint(dialog
, &ps
);
519 DriveDlgProc (HWND dialog
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
529 if (!drives
[2].in_use
)
530 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
);
532 fill_drives_list(dialog
);
533 update_controls(dialog
);
534 /* put in non-advanced mode by default */
535 set_advanced(dialog
);
539 set_window_title(dialog
);
547 if (HIWORD(wParam
) == EN_CHANGE
)
549 on_edit_changed(dialog
, LOWORD(wParam
));
553 switch (LOWORD(wParam
))
555 case IDC_LIST_DRIVES
:
556 if (HIWORD(wParam
) == LBN_SELCHANGE
)
557 update_controls(dialog
);
562 if (HIWORD(wParam
) != BN_CLICKED
) break;
563 on_add_click(dialog
);
566 case IDC_BUTTON_REMOVE
:
567 if (HIWORD(wParam
) != BN_CLICKED
) break;
568 on_remove_click(dialog
);
571 case IDC_BUTTON_EDIT
:
572 if (HIWORD(wParam
) != BN_CLICKED
) break;
573 item
= SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_GETCURSEL
, 0, 0);
574 drive
= (struct drive
*) SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_GETITEMDATA
, item
, 0);
575 /*DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) drive); */
578 case IDC_BUTTON_AUTODETECT
:
580 fill_drives_list(dialog
);
583 case IDC_BUTTON_SHOW_HIDE_ADVANCED
:
584 advanced
= !advanced
;
585 set_advanced(dialog
);
588 case IDC_BUTTON_BROWSE_PATH
:
589 MessageBox(dialog
, "", "Write me!", MB_OK
);
592 case IDC_RADIO_ASSIGN
:
596 str
= get_text(dialog
, IDC_EDIT_LABEL
);
597 HeapFree(GetProcessHeap(), 0, current_drive
->label
);
598 current_drive
->label
= str
? str
: strdupA("");
600 str
= get_text(dialog
, IDC_EDIT_SERIAL
);
601 HeapFree(GetProcessHeap(), 0, current_drive
->serial
);
602 current_drive
->serial
= str
? str
: strdupA("");
604 /* TODO: we don't have a device at this point */
606 enable_labelserial_box(dialog
, BOX_MODE_CD_ASSIGN
);
614 int mode
= BOX_MODE_NORMAL
;
617 if (HIWORD(wParam
) != CBN_SELCHANGE
) break;
619 selection
= SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_GETCURSEL
, 0, 0);
621 if (selection
== 2 || selection
== 3) /* cdrom or floppy */
623 if (IsDlgButtonChecked(dialog
, IDC_RADIO_AUTODETECT
))
624 mode
= BOX_MODE_CD_AUTODETECT
;
626 mode
= BOX_MODE_CD_ASSIGN
;
629 enable_labelserial_box(dialog
, mode
);
631 current_drive
->type
= type_pairs
[selection
].sCode
;
639 switch (((LPNMHDR
)lParam
)->code
)
642 WINE_TRACE("PSN_KILLACTIVE\n");
643 SetWindowLong(dialog
, DWL_MSGRESULT
, FALSE
);
646 apply_drive_changes();
647 SetWindowLong(dialog
, DWL_MSGRESULT
, PSNRET_NOERROR
);