driveui.c 24.2 KB
Newer Older
Mike Hearn's avatar
Mike Hearn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Drive management UI code
 *
 * Copyright 2003 Mark Westcott
 * Copyright 2004 Chris Morgan
 * Copyright 2003-2004 Mike Hearn
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

24 25
#include <stdio.h>

26
#define WIN32_LEAN_AND_MEAN
27 28
#define COBJMACROS

29
#include <windows.h>
Mike Hearn's avatar
Mike Hearn committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <shellapi.h>
#include <objbase.h>
#include <shlguid.h>
#include <shlwapi.h>
#include <shlobj.h>

#include <wine/debug.h>

#include "winecfg.h"
#include "resource.h"

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);

#define BOX_MODE_CD_ASSIGN 1
#define BOX_MODE_CD_AUTODETECT 2
#define BOX_MODE_NONE 3
#define BOX_MODE_NORMAL 4

static BOOL advanced = FALSE;
static BOOL updating_ui = FALSE;
static struct drive* current_drive;

static void get_etched_rect(HWND dialog, RECT *rect);
53
static void update_controls(HWND dialog);
Mike Hearn's avatar
Mike Hearn committed
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/**** listview helper functions ****/

/* clears the item at index in the listview */
static void lv_clear_curr_select(HWND dialog, int index)
{
    ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, 0, LVIS_SELECTED);
}

/* selects the item at index in the listview */
static void lv_set_curr_select(HWND dialog, int index)
{
    /* no more than one item can be selected in our listview */
    lv_clear_curr_select(dialog, -1);
    ListView_SetItemState(GetDlgItem(dialog, IDC_LIST_DRIVES), index, LVIS_SELECTED, LVIS_SELECTED);
}

/* returns the currently selected item in the listview */
static int lv_get_curr_select(HWND dialog)
{
    return SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
}

/* sets the item in the listview at item->iIndex */
static void lv_set_item(HWND dialog, LVITEM *item)
{
    SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_SETITEM, 0, (LPARAM) item);
}

83 84 85 86 87 88 89 90 91 92 93 94 95
/* sets specified item's text */
static void lv_set_item_text(HWND dialog, int item, int subItem, char *text)
{
    LVITEM lvItem;
    if (item < 0 || subItem < 0) return;
    lvItem.mask = LVIF_TEXT;
    lvItem.iItem = item;
    lvItem.iSubItem = subItem;
    lvItem.pszText = text;
    lvItem.cchTextMax = lstrlen(lvItem.pszText);
    lv_set_item(dialog, &lvItem);
}

96 97 98 99 100 101 102 103 104 105 106 107
/* inserts an item into the listview */
static void lv_insert_item(HWND dialog, LVITEM *item)
{
    SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTITEM, 0, (LPARAM) item);
}

/* retrieve the item at index item->iIndex */
static void lv_get_item(HWND dialog, LVITEM *item)
{
    SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_GETITEM, 0, (LPARAM) item);
}

Mike Hearn's avatar
Mike Hearn committed
108 109 110
static void set_advanced(HWND dialog)
{
    int state;
111
    char text[256];
Mike Hearn's avatar
Mike Hearn committed
112 113 114 115 116
    RECT rect;

    if (advanced)
    {
        state = SW_NORMAL;
Paul Vriens's avatar
Paul Vriens committed
117
        LoadString(GetModuleHandle(NULL), IDS_HIDE_ADVANCED, text, 256);
Mike Hearn's avatar
Mike Hearn committed
118 119 120 121
    }
    else
    {
        state = SW_HIDE;
Paul Vriens's avatar
Paul Vriens committed
122
        LoadString(GetModuleHandle(NULL), IDS_SHOW_ADVANCED, text, 256);
Mike Hearn's avatar
Mike Hearn committed
123 124 125 126 127 128 129 130 131 132 133
    }

    ShowWindow(GetDlgItem(dialog, IDC_RADIO_AUTODETECT), state);
    ShowWindow(GetDlgItem(dialog, IDC_RADIO_ASSIGN), state);
    ShowWindow(GetDlgItem(dialog, IDC_EDIT_LABEL), state);
    ShowWindow(GetDlgItem(dialog, IDC_EDIT_DEVICE), state);
    ShowWindow(GetDlgItem(dialog, IDC_STATIC_LABEL), state);
    ShowWindow(GetDlgItem(dialog, IDC_BUTTON_BROWSE_DEVICE), state);
    ShowWindow(GetDlgItem(dialog, IDC_EDIT_SERIAL), state);
    ShowWindow(GetDlgItem(dialog, IDC_STATIC_SERIAL), state);
    ShowWindow(GetDlgItem(dialog, IDC_LABELSERIAL_STATIC), state);
134 135
    ShowWindow(GetDlgItem(dialog, IDC_COMBO_TYPE), state);
    ShowWindow(GetDlgItem(dialog, IDC_STATIC_TYPE), state);
Mike Hearn's avatar
Mike Hearn committed
136 137 138 139 140 141 142 143 144 145 146

    /* update the button text based on the state */
    SetWindowText(GetDlgItem(dialog, IDC_BUTTON_SHOW_HIDE_ADVANCED), text);

    /* redraw for the etched line */
    get_etched_rect(dialog, &rect);
    InflateRect(&rect, 5, 5);
    InvalidateRect(dialog, &rect, TRUE);
}

struct drive_typemap {
Steven Edwards's avatar
Steven Edwards committed
147
    unsigned int sCode;
Mike Hearn's avatar
Mike Hearn committed
148 149 150
    const char *sDesc;
};

Steven Edwards's avatar
Steven Edwards committed
151
static const struct drive_typemap type_pairs[] = {
152
  { DRIVE_UNKNOWN,    "Autodetect"      },
Mike Hearn's avatar
Mike Hearn committed
153 154 155 156 157 158
  { DRIVE_FIXED,      "Local hard disk" },
  { DRIVE_REMOTE,     "Network share"   },
  { DRIVE_REMOVABLE,  "Floppy disk"     },
  { DRIVE_CDROM,      "CD-ROM"          }
};

159
#define DRIVE_TYPE_DEFAULT 0
Mike Hearn's avatar
Mike Hearn committed
160

161
static void fill_drive_droplist(long mask, char curletter, HWND dialog)
Mike Hearn's avatar
Mike Hearn committed
162 163 164 165 166
{
    int i;
    int selection;
    int count;
    int next_letter;
167
    char sName[4];
Mike Hearn's avatar
Mike Hearn committed
168

169
    strcpy(sName, "A:");
Mike Hearn's avatar
Mike Hearn committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    for (i = 0, count = 0, selection = -1, next_letter = -1; i <= 'Z'-'A'; ++i)
    {
        if (mask & DRIVE_MASK_BIT('A' + i))
        {
            int index;

            sName[0] = 'A' + i;
            index = SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName);

            if (toupper(curletter) == 'A' + i)
            {
                selection = count;
            }

            if (i >= 2 && next_letter == -1)
            {
                /* default drive is first one of C-Z */
                next_letter = count;
            }

            count++;
        }
    }

    if (selection == -1)
    {
        selection = next_letter;
    }

    SendDlgItemMessage(dialog, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0);
}


203
static void enable_labelserial_box(HWND dialog, int mode)
Mike Hearn's avatar
Mike Hearn committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
{
    WINE_TRACE("mode=%d\n", mode);

    switch (mode)
    {
        case BOX_MODE_CD_ASSIGN:
            enable(IDC_RADIO_ASSIGN);
            disable(IDC_EDIT_DEVICE);
            disable(IDC_BUTTON_BROWSE_DEVICE);
            enable(IDC_EDIT_SERIAL);
            enable(IDC_EDIT_LABEL);
            enable(IDC_STATIC_SERIAL);
            enable(IDC_STATIC_LABEL);
            break;

        case BOX_MODE_CD_AUTODETECT:
            enable(IDC_RADIO_ASSIGN);
            enable(IDC_EDIT_DEVICE);
            enable(IDC_BUTTON_BROWSE_DEVICE);
            disable(IDC_EDIT_SERIAL);
            disable(IDC_EDIT_LABEL);
            disable(IDC_STATIC_SERIAL);
            disable(IDC_STATIC_LABEL);
            break;

        case BOX_MODE_NONE:
            disable(IDC_RADIO_ASSIGN);
            disable(IDC_EDIT_DEVICE);
            disable(IDC_BUTTON_BROWSE_DEVICE);
            disable(IDC_EDIT_SERIAL);
            disable(IDC_EDIT_LABEL);
            disable(IDC_STATIC_SERIAL);
            disable(IDC_STATIC_LABEL);
            break;

        case BOX_MODE_NORMAL:
            enable(IDC_RADIO_ASSIGN);
            disable(IDC_EDIT_DEVICE);
            disable(IDC_BUTTON_BROWSE_DEVICE);
            enable(IDC_EDIT_SERIAL);
            enable(IDC_EDIT_LABEL);
            enable(IDC_STATIC_SERIAL);
            enable(IDC_STATIC_LABEL);
            break;
    }
}

251
static int fill_drives_list(HWND dialog)
Mike Hearn's avatar
Mike Hearn committed
252 253 254 255 256 257 258 259 260 261
{
    int count = 0;
    BOOL drivec_present = FALSE;
    int i;
    int prevsel = -1;

    WINE_TRACE("\n");

    updating_ui = TRUE;

262
    prevsel = lv_get_curr_select(dialog); 
Mike Hearn's avatar
Mike Hearn committed
263 264

    /* Clear the listbox */
265
    SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_DELETEALLITEMS, 0, 0);
Mike Hearn's avatar
Mike Hearn committed
266 267 268

    for(i = 0; i < 26; i++)
    {
269
        LVITEM item;
270
        char letter[4];
Mike Hearn's avatar
Mike Hearn committed
271 272 273 274 275 276 277 278

        /* skip over any unused drives */
        if (!drives[i].in_use)
            continue;

        if (drives[i].letter == 'C')
            drivec_present = TRUE;

279 280 281
        letter[0] = 'A' + i;
        letter[1] = ':';
        letter[2] = 0;
282 283

        memset(&item, 0, sizeof(item));
284
        item.mask = LVIF_TEXT | LVIF_PARAM;
285 286 287 288
        item.iItem = count;
        item.iSubItem = 0;
        item.pszText = letter;
        item.cchTextMax = lstrlen(item.pszText);
289
        item.lParam = (LPARAM) &drives[i];
Mike Hearn's avatar
Mike Hearn committed
290

291
        lv_insert_item(dialog, &item);
292
        lv_set_item_text(dialog, count, 1, drives[i].unixpath);
Mike Hearn's avatar
Mike Hearn committed
293 294 295 296 297 298 299 300 301 302 303 304

        count++;
    }

    WINE_TRACE("loaded %d drives\n", count);

    /* show the warning if there is no Drive C */
    if (!drivec_present)
        ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
    else
        ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);

305
    lv_set_curr_select(dialog, prevsel == -1 ? 0 : prevsel);
Mike Hearn's avatar
Mike Hearn committed
306 307 308 309 310

    updating_ui = FALSE;
    return count;
}

311
static void on_options_click(HWND dialog)
312 313
{
    if (IsDlgButtonChecked(dialog, IDC_SHOW_DOT_FILES) == BST_CHECKED)
314
        set_reg_key(config_key, "", "ShowDotFiles", "Y");
315
    else
316
        set_reg_key(config_key, "", "ShowDotFiles", "N");
317 318

    SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
319
}
Mike Hearn's avatar
Mike Hearn committed
320

321
static void on_add_click(HWND dialog)
Mike Hearn's avatar
Mike Hearn committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
    /* we should allocate a drive letter automatically. We also need
       some way to let the user choose the mapping point, for now we
       will just force them to enter a path automatically, with / being
       the default. In future we should be able to temporarily map /
       then invoke the directory chooser dialog. */

    char new = 'C'; /* we skip A and B, they are historically floppy drives */
    long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
    int i, c;

    while (mask & (1 << (new - 'A')))
    {
        new++;
        if (new > 'Z')
        {
            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);
            return;
        }
    }

    WINE_TRACE("allocating drive letter %c\n", new);

    if (new == 'C') add_drive(new, "../drive_c", "System Drive", "", DRIVE_FIXED);
346
    else add_drive(new, "/", "", "", DRIVE_UNKNOWN);
Mike Hearn's avatar
Mike Hearn committed
347 348 349 350 351 352 353 354 355 356 357

    fill_drives_list(dialog);

    /* select the newly created drive */
    mask = ~drive_available_mask(0);
    c = 0;
    for (i = 0; i < 26; i++)
    {
        if ('A' + i == new) break;
        if ((1 << i) & mask) c++;
    }
358
    lv_set_curr_select(dialog, c);
Mike Hearn's avatar
Mike Hearn committed
359 360

    SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
361 362

    update_controls(dialog);
Mike Hearn's avatar
Mike Hearn committed
363 364
}

365
static void on_remove_click(HWND dialog)
Mike Hearn's avatar
Mike Hearn committed
366
{
367
    int itemIndex;
Mike Hearn's avatar
Mike Hearn committed
368
    struct drive *drive;
369 370 371 372 373 374 375 376 377 378 379 380 381
    LVITEM item;

    itemIndex = lv_get_curr_select(dialog);
    if (itemIndex == -1) return; /* no selection */

    memset(&item, 0, sizeof(item));
    item.mask = LVIF_PARAM;
    item.iItem = itemIndex;
    item.iSubItem = 0;

    lv_get_item(dialog, &item);

    drive = (struct drive *) item.lParam;
Mike Hearn's avatar
Mike Hearn committed
382

383
    WINE_ERR("unixpath: %s\n", drive->unixpath);
Mike Hearn's avatar
Mike Hearn committed
384 385 386 387 388 389 390 391 392 393 394

    if (drive->letter == 'C')
    {
        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);
        if (result == IDNO) return;
    }

    delete_drive(drive);

    fill_drives_list(dialog);

395 396 397
    itemIndex = itemIndex - 1;
    if (itemIndex < 0) itemIndex = 0;
    lv_set_curr_select(dialog, itemIndex);   /* previous item */
Mike Hearn's avatar
Mike Hearn committed
398 399

    SetFocus(GetDlgItem(dialog, IDC_LIST_DRIVES));
400 401

    update_controls(dialog);
Mike Hearn's avatar
Mike Hearn committed
402 403
}

404 405
static void update_controls(HWND dialog)
{
Mike Hearn's avatar
Mike Hearn committed
406
    char *path;
Steven Edwards's avatar
Steven Edwards committed
407
    unsigned int type;
Mike Hearn's avatar
Mike Hearn committed
408 409
    char *label;
    char *serial;
410
    const char *device;
Mike Hearn's avatar
Mike Hearn committed
411
    int i, selection = -1;
412
    LVITEM item;
Mike Hearn's avatar
Mike Hearn committed
413 414 415

    updating_ui = TRUE;

416
    i = lv_get_curr_select(dialog);
Mike Hearn's avatar
Mike Hearn committed
417 418 419
    if (i == -1)
    {
        /* no selection? let's select something for the user. this will re-enter */
420
        lv_set_curr_select(dialog, i);
Mike Hearn's avatar
Mike Hearn committed
421 422
        return;
    }
423 424 425 426 427 428 429 430

    memset(&item, 0, sizeof(item));
    item.mask = LVIF_PARAM;
    item.iItem = i;
    item.iSubItem = 0;

    lv_get_item(dialog, &item);
    current_drive = (struct drive *) item.lParam;
Mike Hearn's avatar
Mike Hearn committed
431 432 433 434 435 436 437 438 439 440 441 442 443

    WINE_TRACE("Updating sheet for drive %c\n", current_drive->letter);

    /* Drive letters */
    fill_drive_droplist(drive_available_mask(current_drive->letter), current_drive->letter, dialog);

    /* path */
    path = current_drive->unixpath;
    WINE_TRACE("set path control text to '%s'\n", path);
    set_text(dialog, IDC_EDIT_PATH, path);

    /* drive type */
    type = current_drive->type;
444 445 446
    SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_RESETCONTENT, 0, 0);

    for (i = 0; i < sizeof(type_pairs) / sizeof(struct drive_typemap); i++)
Mike Hearn's avatar
Mike Hearn committed
447
    {
448
        SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0, (LPARAM) type_pairs[i].sDesc);
Mike Hearn's avatar
Mike Hearn committed
449

450 451 452
        if (type_pairs[i].sCode ==  type)
        {
            selection = i;
Mike Hearn's avatar
Mike Hearn committed
453
        }
454
    }
Mike Hearn's avatar
Mike Hearn committed
455

456 457
    if (selection == -1) selection = DRIVE_TYPE_DEFAULT;
    SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
Mike Hearn's avatar
Mike Hearn committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

    /* removeable media properties */
    label = current_drive->label;
    set_text(dialog, IDC_EDIT_LABEL, label);

    /* set serial edit text */
    serial = current_drive->serial;
    set_text(dialog, IDC_EDIT_SERIAL, serial);

    /* TODO: get the device here to put into the edit box */
    device = "Not implemented yet";
    set_text(dialog, IDC_EDIT_DEVICE, device);
    device = NULL;

    selection = IDC_RADIO_ASSIGN;
    if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE))
    {
        if (device)
        {
            selection = IDC_RADIO_AUTODETECT;
            enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
        }
        else
        {
            selection = IDC_RADIO_ASSIGN;
            enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
        }
    }
    else
    {
        enable_labelserial_box(dialog, BOX_MODE_NORMAL);
        selection = IDC_RADIO_ASSIGN;
    }

    CheckRadioButton(dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection);

    updating_ui = FALSE;

    return;
}

499
static void on_edit_changed(HWND dialog, WORD id)
Mike Hearn's avatar
Mike Hearn committed
500 501 502 503 504 505 506 507 508 509 510 511
{
    if (updating_ui) return;

    WINE_TRACE("edit id %d changed\n", id);

    switch (id)
    {
        case IDC_EDIT_LABEL:
        {
            char *label;

            label = get_text(dialog, id);
512
            HeapFree(GetProcessHeap(), 0, current_drive->label);
Mike Hearn's avatar
Mike Hearn committed
513 514 515 516
            current_drive->label = label ? label :  strdupA("");

            WINE_TRACE("set label to %s\n", current_drive->label);

517 518
            /* enable the apply button  */
            SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
Mike Hearn's avatar
Mike Hearn committed
519 520 521 522 523 524 525 526
            break;
        }

        case IDC_EDIT_PATH:
        {
            char *path;

            path = get_text(dialog, id);
527
            HeapFree(GetProcessHeap(), 0, current_drive->unixpath);
Mike Hearn's avatar
Mike Hearn committed
528 529 530 531
            current_drive->unixpath = path ? path : strdupA("drive_c");

            WINE_TRACE("set path to %s\n", current_drive->unixpath);

532 533 534 535 536
            lv_set_item_text(dialog, lv_get_curr_select(dialog), 1,
                             current_drive->unixpath);

            /* enable the apply button  */
            SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
Mike Hearn's avatar
Mike Hearn committed
537 538 539 540 541 542 543 544
            break;
        }

        case IDC_EDIT_SERIAL:
        {
            char *serial;

            serial = get_text(dialog, id);
545
            HeapFree(GetProcessHeap(), 0, current_drive->serial);
Mike Hearn's avatar
Mike Hearn committed
546 547 548 549
            current_drive->serial = serial ? serial : strdupA("");

            WINE_TRACE("set serial to %s", current_drive->serial);

550 551
            /* enable the apply button  */
            SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
Mike Hearn's avatar
Mike Hearn committed
552 553 554 555 556 557 558
            break;
        }

        case IDC_EDIT_DEVICE:
        {
            char *device = get_text(dialog, id);
            /* TODO: handle device if/when it makes sense to do so.... */
559
            HeapFree(GetProcessHeap(), 0, device);
Mike Hearn's avatar
Mike Hearn committed
560 561 562 563 564 565 566 567 568 569
            break;
        }
    }
}

static void get_etched_rect(HWND dialog, RECT *rect)
{
    GetClientRect(dialog, rect);

    /* these dimensions from the labelserial static in En.rc  */
570 571 572
    rect->top = 265;
    rect->bottom = 265;
    rect->left += 25;
Mike Hearn's avatar
Mike Hearn committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    rect->right -= 25;
}

/* this just draws a nice line to separate the advanced gui from the n00b gui :) */
static void paint(HWND dialog)
{
    PAINTSTRUCT ps;

    BeginPaint(dialog, &ps);

    if (advanced)
    {
        RECT rect;

        get_etched_rect(dialog, &rect);

        DrawEdge(ps.hdc, &rect, EDGE_ETCHED, BF_TOP);
    }

    EndPaint(dialog, &ps);
}

595
BOOL browse_for_unix_folder(HWND dialog, char *pszPath)
596 597 598
{
    static WCHAR wszUnixRootDisplayName[] = 
        { ':',':','{','C','C','7','0','2','E','B','2','-','7','D','C','5','-','1','1','D','9','-',
599 600
          'C','6','8','7','-','0','0','0','4','2','3','8','A','0','1','C','D','}', 0 };
    char pszChoosePath[256];
601 602 603 604
    BROWSEINFOA bi = {
        dialog,
        NULL,
        NULL,
605
        pszChoosePath,
606 607 608 609 610 611 612 613
        0,
        NULL,
        0,
        0
    };
    IShellFolder *pDesktop;
    LPITEMIDLIST pidlUnixRoot, pidlSelectedPath;
    HRESULT hr;
614 615
   
    LoadString(GetModuleHandle(NULL), IDS_CHOOSE_PATH, pszChoosePath, 256);
616 617
    
    hr = SHGetDesktopFolder(&pDesktop);
618
    if (!SUCCEEDED(hr)) return FALSE;
619

620 621
    hr = IShellFolder_ParseDisplayName(pDesktop, NULL, NULL, wszUnixRootDisplayName, NULL, 
                                       &pidlUnixRoot, NULL);
622
    if (!SUCCEEDED(hr)) {
623
        IShellFolder_Release(pDesktop);
624
        return FALSE;
625 626 627 628 629 630 631 632 633 634 635
    }

    bi.pidlRoot = pidlUnixRoot;
    pidlSelectedPath = SHBrowseForFolderA(&bi);
    SHFree(pidlUnixRoot);
    
    if (pidlSelectedPath) {
        STRRET strSelectedPath;
        char *pszSelectedPath;
        HRESULT hr;
        
636 637 638
        hr = IShellFolder_GetDisplayNameOf(pDesktop, pidlSelectedPath, SHGDN_FORPARSING, 
                                           &strSelectedPath);
        IShellFolder_Release(pDesktop);
639 640
        if (!SUCCEEDED(hr)) {
            SHFree(pidlSelectedPath);
641
            return FALSE;
642 643 644 645
        }

        hr = StrRetToStr(&strSelectedPath, pidlSelectedPath, &pszSelectedPath);
        SHFree(pidlSelectedPath);
646
        if (!SUCCEEDED(hr)) return FALSE;
647

648
        lstrcpy(pszPath, pszSelectedPath);
649 650
        
        CoTaskMemFree(pszSelectedPath);
651
        return TRUE;
652
    }
653
    return FALSE;
654 655
}

656 657 658 659 660 661 662 663 664 665
static void init_listview_columns(HWND dialog)
{
    LVCOLUMN listColumn;
    RECT viewRect;
    int width;

    GetClientRect(GetDlgItem(dialog, IDC_LIST_DRIVES), &viewRect);
    width = (viewRect.right - viewRect.left) / 6 - 5;

    listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
666
    listColumn.pszText = (char*) "Letter";
667 668 669 670 671 672
    listColumn.cchTextMax = lstrlen(listColumn.pszText);
    listColumn.cx = width;

    SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMN, 0, (LPARAM) &listColumn);

    listColumn.cx = viewRect.right - viewRect.left - width;
673
    listColumn.pszText = (char*) "Drive Mapping";
674 675 676 677 678
    listColumn.cchTextMax = lstrlen(listColumn.pszText);

    SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LVM_INSERTCOLUMN, 1, (LPARAM) &listColumn);
}

679 680
static void load_drive_options(HWND dialog)
{
681
    if (!strcmp(get_reg_key(config_key, "", "ShowDotFiles", "N"), "Y"))
682 683
        CheckDlgButton(dialog, IDC_SHOW_DOT_FILES, BST_CHECKED);
}
684

Mike Hearn's avatar
Mike Hearn committed
685 686 687 688 689 690 691 692 693
INT_PTR CALLBACK
DriveDlgProc (HWND dialog, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int item;
    struct drive *drive;

    switch (msg)
    {
        case WM_INITDIALOG:
694
            init_listview_columns(dialog);
Mike Hearn's avatar
Mike Hearn committed
695
            load_drives();
696
            load_drive_options(dialog);
Mike Hearn's avatar
Mike Hearn committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

            if (!drives[2].in_use)
                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);

            fill_drives_list(dialog);
            update_controls(dialog);
            /* put in non-advanced mode by default  */
            set_advanced(dialog);
            break;

        case WM_SHOWWINDOW:
            set_window_title(dialog);
            break;

        case WM_PAINT:
            paint(dialog);
            break;

        case WM_COMMAND:
716
            switch (HIWORD(wParam))
Mike Hearn's avatar
Mike Hearn committed
717
            {
718 719 720 721 722 723 724 725 726 727 728 729
                case EN_CHANGE:
                    on_edit_changed(dialog, LOWORD(wParam));
                    break;

                case BN_CLICKED:
                    switch (LOWORD(wParam))
                    {
                        case IDC_SHOW_DOT_FILES:
                            on_options_click(dialog);
                        break;
                    }
                    break;
730 731 732 733

                case CBN_SELCHANGE:
                    SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
                    break;
Mike Hearn's avatar
Mike Hearn committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
            }

            switch (LOWORD(wParam))
            {
                case IDC_BUTTON_ADD:
                    if (HIWORD(wParam) != BN_CLICKED) break;
                    on_add_click(dialog);
                    break;

                case IDC_BUTTON_REMOVE:
                    if (HIWORD(wParam) != BN_CLICKED) break;
                    on_remove_click(dialog);
                    break;

                case IDC_BUTTON_EDIT:
                    if (HIWORD(wParam) != BN_CLICKED) break;
                    item = SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES),  LB_GETCURSEL, 0, 0);
                    drive = (struct drive *) SendMessage(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_GETITEMDATA, item, 0);
                    /*DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) drive); */
                    break;

                case IDC_BUTTON_AUTODETECT:
                    autodetect_drives();
                    fill_drives_list(dialog);
758
                    SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
Mike Hearn's avatar
Mike Hearn committed
759 760 761 762 763 764 765 766
                    break;

                case IDC_BUTTON_SHOW_HIDE_ADVANCED:
                    advanced = !advanced;
                    set_advanced(dialog);
                    break;

                case IDC_BUTTON_BROWSE_PATH:
767 768 769 770
                {
                    char szTargetPath[FILENAME_MAX];
                    if (browse_for_unix_folder(dialog, szTargetPath)) 
                        set_text(dialog, IDC_EDIT_PATH, szTargetPath);
Mike Hearn's avatar
Mike Hearn committed
771
                    break;
772
                }
Mike Hearn's avatar
Mike Hearn committed
773 774 775 776 777 778

                case IDC_RADIO_ASSIGN:
                {
                    char *str;

                    str = get_text(dialog, IDC_EDIT_LABEL);
779
                    HeapFree(GetProcessHeap(), 0, current_drive->label);
Mike Hearn's avatar
Mike Hearn committed
780 781 782
                    current_drive->label = str ? str : strdupA("");

                    str = get_text(dialog, IDC_EDIT_SERIAL);
783
                    HeapFree(GetProcessHeap(), 0, current_drive->serial);
Mike Hearn's avatar
Mike Hearn committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
                    current_drive->serial = str ? str : strdupA("");

                    /* TODO: we don't have a device at this point */

                    enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);

                    break;
                }


                case IDC_COMBO_TYPE:
                {
                    int mode = BOX_MODE_NORMAL;
                    int selection;

                    if (HIWORD(wParam) != CBN_SELCHANGE) break;

                    selection = SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);

803 804 805
                    if (selection >= 0 &&
                        (type_pairs[selection].sCode == DRIVE_CDROM ||
                         type_pairs[selection].sCode == DRIVE_REMOVABLE))
Mike Hearn's avatar
Mike Hearn committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
                    {
                        if (IsDlgButtonChecked(dialog, IDC_RADIO_AUTODETECT))
                            mode = BOX_MODE_CD_AUTODETECT;
                        else
                            mode = BOX_MODE_CD_ASSIGN;
                    }

                    enable_labelserial_box(dialog, mode);

                    current_drive->type = type_pairs[selection].sCode;
                    break;
                }

            }
            break;

        case WM_NOTIFY:
            switch (((LPNMHDR)lParam)->code)
            {
                case PSN_KILLACTIVE:
                    WINE_TRACE("PSN_KILLACTIVE\n");
827
                    SetWindowLongPtr(dialog, DWLP_MSGRESULT, FALSE);
Mike Hearn's avatar
Mike Hearn committed
828 829 830
                    break;
                case PSN_APPLY:
                    apply_drive_changes();
831
                    SetWindowLongPtr(dialog, DWLP_MSGRESULT, PSNRET_NOERROR);
Mike Hearn's avatar
Mike Hearn committed
832 833 834
                    break;
                case PSN_SETACTIVE:
                    break;
835 836 837 838 839
                case LVN_ITEMCHANGED:
                {
                    LPNMLISTVIEW lpnm = (LPNMLISTVIEW)lParam;
                    if (!(lpnm->uOldState & LVIS_SELECTED) &&
                         (lpnm->uNewState & LVIS_SELECTED))
840 841
                    update_controls(dialog);
                    break;
842
                }
843
            }
Mike Hearn's avatar
Mike Hearn committed
844 845 846 847 848
            break;
    }

    return FALSE;
}