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
);
55 static void set_advanced(HWND dialog
)
61 /* FIXME: internationalization */
65 text
= "&Hide Advanced";
70 text
= "&Show Advanced";
73 ShowWindow(GetDlgItem(dialog
, IDC_RADIO_AUTODETECT
), state
);
74 ShowWindow(GetDlgItem(dialog
, IDC_RADIO_ASSIGN
), state
);
75 ShowWindow(GetDlgItem(dialog
, IDC_EDIT_LABEL
), state
);
76 ShowWindow(GetDlgItem(dialog
, IDC_EDIT_DEVICE
), state
);
77 ShowWindow(GetDlgItem(dialog
, IDC_STATIC_LABEL
), state
);
78 ShowWindow(GetDlgItem(dialog
, IDC_BUTTON_BROWSE_DEVICE
), state
);
79 ShowWindow(GetDlgItem(dialog
, IDC_EDIT_SERIAL
), state
);
80 ShowWindow(GetDlgItem(dialog
, IDC_STATIC_SERIAL
), state
);
81 ShowWindow(GetDlgItem(dialog
, IDC_LABELSERIAL_STATIC
), state
);
83 /* update the button text based on the state */
84 SetWindowText(GetDlgItem(dialog
, IDC_BUTTON_SHOW_HIDE_ADVANCED
), text
);
86 /* redraw for the etched line */
87 get_etched_rect(dialog
, &rect
);
88 InflateRect(&rect
, 5, 5);
89 InvalidateRect(dialog
, &rect
, TRUE
);
92 struct drive_typemap
{
97 static struct drive_typemap type_pairs
[] = {
98 { DRIVE_FIXED
, "Local hard disk" },
99 { DRIVE_REMOTE
, "Network share" },
100 { DRIVE_REMOVABLE
, "Floppy disk" },
101 { DRIVE_CDROM
, "CD-ROM" }
104 #define DRIVE_TYPE_DEFAULT 1
106 void fill_drive_droplist(long mask
, char curletter
, HWND dialog
)
112 char sName
[4] = "A:";
114 for (i
= 0, count
= 0, selection
= -1, next_letter
= -1; i
<= 'Z'-'A'; ++i
)
116 if (mask
& DRIVE_MASK_BIT('A' + i
))
121 index
= SendDlgItemMessage(dialog
, IDC_COMBO_LETTER
, CB_ADDSTRING
, 0, (LPARAM
) sName
);
123 if (toupper(curletter
) == 'A' + i
)
128 if (i
>= 2 && next_letter
== -1)
130 /* default drive is first one of C-Z */
140 selection
= next_letter
;
143 SendDlgItemMessage(dialog
, IDC_COMBO_LETTER
, CB_SETCURSEL
, selection
, 0);
147 void enable_labelserial_box(HWND dialog
, int mode
)
149 WINE_TRACE("mode=%d\n", mode
);
153 case BOX_MODE_CD_ASSIGN
:
154 enable(IDC_RADIO_ASSIGN
);
155 disable(IDC_EDIT_DEVICE
);
156 disable(IDC_BUTTON_BROWSE_DEVICE
);
157 enable(IDC_EDIT_SERIAL
);
158 enable(IDC_EDIT_LABEL
);
159 enable(IDC_STATIC_SERIAL
);
160 enable(IDC_STATIC_LABEL
);
163 case BOX_MODE_CD_AUTODETECT
:
164 enable(IDC_RADIO_ASSIGN
);
165 enable(IDC_EDIT_DEVICE
);
166 enable(IDC_BUTTON_BROWSE_DEVICE
);
167 disable(IDC_EDIT_SERIAL
);
168 disable(IDC_EDIT_LABEL
);
169 disable(IDC_STATIC_SERIAL
);
170 disable(IDC_STATIC_LABEL
);
174 disable(IDC_RADIO_ASSIGN
);
175 disable(IDC_EDIT_DEVICE
);
176 disable(IDC_BUTTON_BROWSE_DEVICE
);
177 disable(IDC_EDIT_SERIAL
);
178 disable(IDC_EDIT_LABEL
);
179 disable(IDC_STATIC_SERIAL
);
180 disable(IDC_STATIC_LABEL
);
183 case BOX_MODE_NORMAL
:
184 enable(IDC_RADIO_ASSIGN
);
185 disable(IDC_EDIT_DEVICE
);
186 disable(IDC_BUTTON_BROWSE_DEVICE
);
187 enable(IDC_EDIT_SERIAL
);
188 enable(IDC_EDIT_LABEL
);
189 enable(IDC_STATIC_SERIAL
);
190 enable(IDC_STATIC_LABEL
);
195 int fill_drives_list(HWND dialog
)
198 BOOL drivec_present
= FALSE
;
206 prevsel
= SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
208 /* Clear the listbox */
209 SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_RESETCONTENT
, 0, 0);
211 for(i
= 0; i
< 26; i
++)
217 /* skip over any unused drives */
218 if (!drives
[i
].in_use
)
221 if (drives
[i
].letter
== 'C')
222 drivec_present
= TRUE
;
224 len
= snprintf(title
, 0, "%c: %s", 'A' + i
,
226 len
++; /* add a byte for the trailing null */
228 title
= HeapAlloc(GetProcessHeap(), 0, len
);
230 /* the %s in the item label will be replaced by the drive letter, so -1, then
231 -2 for the second %s which will be expanded to the label, finally + 1 for terminating #0 */
232 snprintf(title
, len
, "%c: %s", 'A' + i
,
235 WINE_TRACE("title is '%s'\n", title
);
237 /* the first SendMessage call adds the string and returns the index, the second associates that index with it */
238 index
= SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_ADDSTRING
,(WPARAM
) -1, (LPARAM
) title
);
239 SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_SETITEMDATA
, index
, (LPARAM
) &drives
[i
]);
241 HeapFree(GetProcessHeap(), 0, title
);
245 WINE_TRACE("loaded %d drives\n", count
);
247 /* show the warning if there is no Drive C */
249 ShowWindow(GetDlgItem(dialog
, IDS_DRIVE_NO_C
), SW_NORMAL
);
251 ShowWindow(GetDlgItem(dialog
, IDS_DRIVE_NO_C
), SW_HIDE
);
253 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, prevsel
== -1 ? 0 : prevsel
, 0);
260 void on_add_click(HWND dialog
)
262 /* we should allocate a drive letter automatically. We also need
263 some way to let the user choose the mapping point, for now we
264 will just force them to enter a path automatically, with / being
265 the default. In future we should be able to temporarily map /
266 then invoke the directory chooser dialog. */
268 char new = 'C'; /* we skip A and B, they are historically floppy drives */
269 long mask
= ~drive_available_mask(0); /* the mask is now which drives aren't available */
272 while (mask
& (1 << (new - 'A')))
277 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
);
282 WINE_TRACE("allocating drive letter %c\n", new);
284 if (new == 'C') add_drive(new, "../drive_c", "System Drive", "", DRIVE_FIXED
);
285 else add_drive(new, "/", "", "", DRIVE_FIXED
);
287 fill_drives_list(dialog
);
289 /* select the newly created drive */
290 mask
= ~drive_available_mask(0);
292 for (i
= 0; i
< 26; i
++)
294 if ('A' + i
== new) break;
295 if ((1 << i
) & mask
) c
++;
297 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, c
, 0);
299 SetFocus(GetDlgItem(dialog
, IDC_LIST_DRIVES
));
302 void on_remove_click(HWND dialog
)
307 item
= SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
308 if (item
== -1) return; /* no selection */
310 drive
= (struct drive
*) SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETITEMDATA
, item
, 0);
312 if (drive
->letter
== 'C')
314 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
);
315 if (result
== IDNO
) return;
320 fill_drives_list(dialog
);
323 if (item
< 0) item
= 0;
324 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, item
, 0); /* previous item */
326 SetFocus(GetDlgItem(dialog
, IDC_LIST_DRIVES
));
329 void update_controls(HWND dialog
) {
335 int i
, selection
= -1;
339 i
= SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETCURSEL
, 0, 0);
342 /* no selection? let's select something for the user. this will re-enter */
343 SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_SETCURSEL
, 0, 0);
346 current_drive
= (struct drive
*) SendDlgItemMessage(dialog
, IDC_LIST_DRIVES
, LB_GETITEMDATA
, i
, 0);
348 WINE_TRACE("Updating sheet for drive %c\n", current_drive
->letter
);
351 fill_drive_droplist(drive_available_mask(current_drive
->letter
), current_drive
->letter
, dialog
);
354 path
= current_drive
->unixpath
;
355 WINE_TRACE("set path control text to '%s'\n", path
);
356 set_text(dialog
, IDC_EDIT_PATH
, path
);
359 type
= current_drive
->type
;
362 for (i
= 0; i
< sizeof(type_pairs
) / sizeof(struct drive_typemap
); i
++)
364 SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_ADDSTRING
, 0, (LPARAM
) type_pairs
[i
].sDesc
);
366 if (type_pairs
[i
].sCode
== type
)
372 if (selection
== -1) selection
= DRIVE_TYPE_DEFAULT
;
373 SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_SETCURSEL
, selection
, 0);
374 } else WINE_WARN("no Type field?\n");
377 /* removeable media properties */
378 label
= current_drive
->label
;
379 set_text(dialog
, IDC_EDIT_LABEL
, label
);
381 /* set serial edit text */
382 serial
= current_drive
->serial
;
383 set_text(dialog
, IDC_EDIT_SERIAL
, serial
);
385 /* TODO: get the device here to put into the edit box */
386 device
= "Not implemented yet";
387 set_text(dialog
, IDC_EDIT_DEVICE
, device
);
390 selection
= IDC_RADIO_ASSIGN
;
391 if ((type
== DRIVE_CDROM
) || (type
== DRIVE_REMOVABLE
))
395 selection
= IDC_RADIO_AUTODETECT
;
396 enable_labelserial_box(dialog
, BOX_MODE_CD_AUTODETECT
);
400 selection
= IDC_RADIO_ASSIGN
;
401 enable_labelserial_box(dialog
, BOX_MODE_CD_ASSIGN
);
406 enable_labelserial_box(dialog
, BOX_MODE_NORMAL
);
407 selection
= IDC_RADIO_ASSIGN
;
410 CheckRadioButton(dialog
, IDC_RADIO_AUTODETECT
, IDC_RADIO_ASSIGN
, selection
);
417 void on_edit_changed(HWND dialog
, WORD id
)
419 if (updating_ui
) return;
421 WINE_TRACE("edit id %d changed\n", id
);
423 /* using fill_drives_list here is pretty lazy, but i'm tired
425 fortunately there are only 26 letters in the alphabet, so
426 we don't have to worry about efficiency too much here :) */
434 label
= get_text(dialog
, id
);
435 HeapFree(GetProcessHeap(), 0, current_drive
->label
);
436 current_drive
->label
= label
? label
: strdupA("");
438 WINE_TRACE("set label to %s\n", current_drive
->label
);
440 fill_drives_list(dialog
);
448 path
= get_text(dialog
, id
);
449 HeapFree(GetProcessHeap(), 0, current_drive
->unixpath
);
450 current_drive
->unixpath
= path
? path
: strdupA("drive_c");
452 WINE_TRACE("set path to %s\n", current_drive
->unixpath
);
454 fill_drives_list(dialog
);
458 case IDC_EDIT_SERIAL
:
462 serial
= get_text(dialog
, id
);
463 HeapFree(GetProcessHeap(), 0, current_drive
->serial
);
464 current_drive
->serial
= serial
? serial
: strdupA("");
466 WINE_TRACE("set serial to %s", current_drive
->serial
);
471 case IDC_EDIT_DEVICE
:
473 char *device
= get_text(dialog
, id
);
474 /* TODO: handle device if/when it makes sense to do so.... */
475 HeapFree(GetProcessHeap(), 0, device
);
476 fill_drives_list(dialog
);
482 static void get_etched_rect(HWND dialog
, RECT
*rect
)
484 GetClientRect(dialog
, rect
);
486 /* these dimensions from the labelserial static in En.rc */
493 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
494 static void paint(HWND dialog
)
498 BeginPaint(dialog
, &ps
);
504 get_etched_rect(dialog
, &rect
);
506 DrawEdge(ps
.hdc
, &rect
, EDGE_ETCHED
, BF_TOP
);
509 EndPaint(dialog
, &ps
);
513 DriveDlgProc (HWND dialog
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
523 if (!drives
[2].in_use
)
524 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
);
526 fill_drives_list(dialog
);
527 update_controls(dialog
);
528 /* put in non-advanced mode by default */
529 set_advanced(dialog
);
533 set_window_title(dialog
);
541 if (HIWORD(wParam
) == EN_CHANGE
)
543 on_edit_changed(dialog
, LOWORD(wParam
));
547 switch (LOWORD(wParam
))
549 case IDC_LIST_DRIVES
:
550 if (HIWORD(wParam
) == LBN_SELCHANGE
)
551 update_controls(dialog
);
556 if (HIWORD(wParam
) != BN_CLICKED
) break;
557 on_add_click(dialog
);
560 case IDC_BUTTON_REMOVE
:
561 if (HIWORD(wParam
) != BN_CLICKED
) break;
562 on_remove_click(dialog
);
565 case IDC_BUTTON_EDIT
:
566 if (HIWORD(wParam
) != BN_CLICKED
) break;
567 item
= SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_GETCURSEL
, 0, 0);
568 drive
= (struct drive
*) SendMessage(GetDlgItem(dialog
, IDC_LIST_DRIVES
), LB_GETITEMDATA
, item
, 0);
569 /*DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) drive); */
572 case IDC_BUTTON_AUTODETECT
:
574 fill_drives_list(dialog
);
577 case IDC_BUTTON_SHOW_HIDE_ADVANCED
:
578 advanced
= !advanced
;
579 set_advanced(dialog
);
582 case IDC_BUTTON_BROWSE_PATH
:
583 MessageBox(dialog
, "", "Write me!", MB_OK
);
586 case IDC_RADIO_ASSIGN
:
590 str
= get_text(dialog
, IDC_EDIT_LABEL
);
591 HeapFree(GetProcessHeap(), 0, current_drive
->label
);
592 current_drive
->label
= str
? str
: strdupA("");
594 str
= get_text(dialog
, IDC_EDIT_SERIAL
);
595 HeapFree(GetProcessHeap(), 0, current_drive
->serial
);
596 current_drive
->serial
= str
? str
: strdupA("");
598 /* TODO: we don't have a device at this point */
600 enable_labelserial_box(dialog
, BOX_MODE_CD_ASSIGN
);
608 int mode
= BOX_MODE_NORMAL
;
611 if (HIWORD(wParam
) != CBN_SELCHANGE
) break;
613 selection
= SendDlgItemMessage(dialog
, IDC_COMBO_TYPE
, CB_GETCURSEL
, 0, 0);
615 if (selection
== 2 || selection
== 3) /* cdrom or floppy */
617 if (IsDlgButtonChecked(dialog
, IDC_RADIO_AUTODETECT
))
618 mode
= BOX_MODE_CD_AUTODETECT
;
620 mode
= BOX_MODE_CD_ASSIGN
;
623 enable_labelserial_box(dialog
, mode
);
625 current_drive
->type
= type_pairs
[selection
].sCode
;
633 switch (((LPNMHDR
)lParam
)->code
)
636 WINE_TRACE("PSN_KILLACTIVE\n");
637 SetWindowLong(dialog
, DWL_MSGRESULT
, FALSE
);
640 apply_drive_changes();
641 SetWindowLong(dialog
, DWL_MSGRESULT
, PSNRET_NOERROR
);