- More heapification.
[wine/wine-kai.git] / programs / winecfg / driveui.c
blob0b99f31a087e25d379f543e0096c7d10e3ce49de
1 /*
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
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winreg.h>
30 #include <shellapi.h>
31 #include <objbase.h>
32 #include <shlguid.h>
33 #include <shlwapi.h>
34 #include <shlobj.h>
35 #include <winuser.h>
37 #include <wine/debug.h>
39 #include "winecfg.h"
40 #include "resource.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)
57 int state;
58 char *text;
59 RECT rect;
61 /* FIXME: internationalization */
62 if (advanced)
64 state = SW_NORMAL;
65 text = "&Hide Advanced";
67 else
69 state = SW_HIDE;
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 {
93 const uint sCode;
94 const char *sDesc;
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)
108 int i;
109 int selection;
110 int count;
111 int next_letter;
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))
118 int index;
120 sName[0] = 'A' + i;
121 index = SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName);
123 if (toupper(curletter) == 'A' + i)
125 selection = count;
128 if (i >= 2 && next_letter == -1)
130 /* default drive is first one of C-Z */
131 next_letter = count;
134 count++;
138 if (selection == -1)
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);
151 switch (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);
161 break;
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);
171 break;
173 case BOX_MODE_NONE:
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);
181 break;
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);
191 break;
195 int fill_drives_list(HWND dialog)
197 int count = 0;
198 BOOL drivec_present = FALSE;
199 int i;
200 int prevsel = -1;
202 WINE_TRACE("\n");
204 updating_ui = TRUE;
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++)
213 char *title = 0;
214 int len;
215 int index;
217 /* skip over any unused drives */
218 if (!drives[i].in_use)
219 continue;
221 if (drives[i].letter == 'C')
222 drivec_present = TRUE;
224 len = snprintf(title, 0, "%c: %s", 'A' + i,
225 drives[i].unixpath);
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,
233 drives[i].unixpath);
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);
242 count++;
245 WINE_TRACE("loaded %d drives\n", count);
247 /* show the warning if there is no Drive C */
248 if (!drivec_present)
249 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
250 else
251 ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
253 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LB_SETCURSEL, prevsel == -1 ? 0 : prevsel, 0);
255 updating_ui = FALSE;
256 return count;
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 */
270 int i, c;
272 while (mask & (1 << (new - 'A')))
274 new++;
275 if (new > 'Z')
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);
278 return;
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);
291 c = 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)
304 int item;
305 struct drive *drive;
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;
318 delete_drive(drive);
320 fill_drives_list(dialog);
322 item = item - 1;
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) {
330 char *path;
331 uint type;
332 char *label;
333 char *serial;
334 char *device;
335 int i, selection = -1;
337 updating_ui = TRUE;
339 i = SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LB_GETCURSEL, 0, 0);
340 if (i == -1)
342 /* no selection? let's select something for the user. this will re-enter */
343 SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LB_SETCURSEL, 0, 0);
344 return;
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);
350 /* Drive letters */
351 fill_drive_droplist(drive_available_mask(current_drive->letter), current_drive->letter, dialog);
353 /* path */
354 path = current_drive->unixpath;
355 WINE_TRACE("set path control text to '%s'\n", path);
356 set_text(dialog, IDC_EDIT_PATH, path);
358 /* drive type */
359 type = current_drive->type;
360 if (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)
368 selection = i;
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);
388 device = NULL;
390 selection = IDC_RADIO_ASSIGN;
391 if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE))
393 if (device)
395 selection = IDC_RADIO_AUTODETECT;
396 enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
398 else
400 selection = IDC_RADIO_ASSIGN;
401 enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
404 else
406 enable_labelserial_box(dialog, BOX_MODE_NORMAL);
407 selection = IDC_RADIO_ASSIGN;
410 CheckRadioButton(dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection);
412 updating_ui = FALSE;
414 return;
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 :) */
428 switch (id)
430 case IDC_EDIT_LABEL:
432 char *label;
434 label = get_text(dialog, id);
435 if (current_drive->label) 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);
441 break;
444 case IDC_EDIT_PATH:
446 char *path;
448 path = get_text(dialog, id);
449 if (current_drive->unixpath) 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);
455 break;
458 case IDC_EDIT_SERIAL:
460 char *serial;
462 serial = get_text(dialog, id);
463 if (current_drive->serial) HeapFree(GetProcessHeap(), 0, current_drive->serial);
464 current_drive->serial = serial ? serial : strdupA("");
466 WINE_TRACE("set serial to %s", current_drive->serial);
468 break;
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 if (device) HeapFree(GetProcessHeap(), 0, device);
476 fill_drives_list(dialog);
477 break;
482 static void get_etched_rect(HWND dialog, RECT *rect)
484 GetClientRect(dialog, rect);
486 /* these dimensions from the labelserial static in En.rc */
487 rect->top = 258;
488 rect->bottom = 258;
489 rect->left += 35;
490 rect->right -= 25;
493 /* this just draws a nice line to separate the advanced gui from the n00b gui :) */
494 static void paint(HWND dialog)
496 PAINTSTRUCT ps;
498 BeginPaint(dialog, &ps);
500 if (advanced)
502 RECT rect;
504 get_etched_rect(dialog, &rect);
506 DrawEdge(ps.hdc, &rect, EDGE_ETCHED, BF_TOP);
509 EndPaint(dialog, &ps);
512 INT_PTR CALLBACK
513 DriveDlgProc (HWND dialog, UINT msg, WPARAM wParam, LPARAM lParam)
515 int item;
516 struct drive *drive;
518 switch (msg)
520 case WM_INITDIALOG:
521 load_drives();
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);
530 break;
532 case WM_SHOWWINDOW:
533 set_window_title(dialog);
534 break;
536 case WM_PAINT:
537 paint(dialog);
538 break;
540 case WM_COMMAND:
541 if (HIWORD(wParam) == EN_CHANGE)
543 on_edit_changed(dialog, LOWORD(wParam));
544 break;
547 switch (LOWORD(wParam))
549 case IDC_LIST_DRIVES:
550 if (HIWORD(wParam) == LBN_SELCHANGE)
551 update_controls(dialog);
553 break;
555 case IDC_BUTTON_ADD:
556 if (HIWORD(wParam) != BN_CLICKED) break;
557 on_add_click(dialog);
558 break;
560 case IDC_BUTTON_REMOVE:
561 if (HIWORD(wParam) != BN_CLICKED) break;
562 on_remove_click(dialog);
563 break;
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); */
570 break;
572 case IDC_BUTTON_AUTODETECT:
573 autodetect_drives();
574 fill_drives_list(dialog);
575 break;
577 case IDC_BUTTON_SHOW_HIDE_ADVANCED:
578 advanced = !advanced;
579 set_advanced(dialog);
580 break;
582 case IDC_BUTTON_BROWSE_PATH:
583 MessageBox(dialog, "", "Write me!", MB_OK);
584 break;
586 case IDC_RADIO_ASSIGN:
588 char *str;
590 str = get_text(dialog, IDC_EDIT_LABEL);
591 if (current_drive->label) HeapFree(GetProcessHeap(), 0, current_drive->label);
592 current_drive->label = str ? str : strdupA("");
594 str = get_text(dialog, IDC_EDIT_SERIAL);
595 if (current_drive->serial) 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);
602 break;
606 case IDC_COMBO_TYPE:
608 int mode = BOX_MODE_NORMAL;
609 int selection;
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;
619 else
620 mode = BOX_MODE_CD_ASSIGN;
623 enable_labelserial_box(dialog, mode);
625 current_drive->type = type_pairs[selection].sCode;
626 break;
630 break;
632 case WM_NOTIFY:
633 switch (((LPNMHDR)lParam)->code)
635 case PSN_KILLACTIVE:
636 WINE_TRACE("PSN_KILLACTIVE\n");
637 SetWindowLong(dialog, DWL_MSGRESULT, FALSE);
638 break;
639 case PSN_APPLY:
640 apply_drive_changes();
641 SetWindowLong(dialog, DWL_MSGRESULT, PSNRET_NOERROR);
642 break;
643 case PSN_SETACTIVE:
644 break;
646 break;
649 return FALSE;