listbox.c 104 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Unit test suite for list boxes.
 *
 * Copyright 2003 Ferenc Wagner
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
#include <stdio.h>

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
28
#include "commctrl.h"
29

30
#include "wine/heap.h"
31 32
#include "wine/test.h"
#include "v6util.h"
33 34 35 36
#include "msg.h"

enum seq_index
{
37
    LB_SEQ_INDEX,
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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
    PARENT_SEQ_INDEX,
    NUM_MSG_SEQUENCES
};

static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];

/* encoded MEASUREITEMSTRUCT into a WPARAM */
typedef struct
{
    union
    {
        struct
        {
            UINT CtlType : 4;
            UINT CtlID   : 4;
            UINT itemID  : 4;
            UINT wParam  : 20;
        } item;
        WPARAM wp;
    } u;
} MEASURE_ITEM_STRUCT;

static unsigned hash_Ly_W(const WCHAR *str)
{
    unsigned hash = 0;

    for (; *str; str++)
        hash = hash * 1664525u + (unsigned char)(*str) + 1013904223u;

    return hash;
}

static unsigned hash_Ly(const char *str)
{
    unsigned hash = 0;

    for (; *str; str++)
        hash = hash * 1664525u + (unsigned char)(*str) + 1013904223u;

    return hash;
}
79 80 81 82 83 84 85 86 87 88 89 90 91

static const char * const strings[4] = {
    "First added",
    "Second added",
    "Third added",
    "Fourth added which is very long because at some time we only had a 256 byte character buffer and "
      "that was overflowing in one of those applications that had a common dialog file open box and tried "
      "to add a 300 characters long custom filter string which of course the code did not like and crashed. "
      "Just make sure this string is longer than 256 characters."
};

static const char BAD_EXTENSION[] = "*.badtxt";

92
#define ID_LISTBOX 1
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static LRESULT WINAPI listbox_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
    static LONG defwndproc_counter = 0;
    struct message msg = { 0 };
    LRESULT ret;

    switch (message)
    {
        case WM_SIZE:
        case WM_GETTEXT:
        case WM_PAINT:
        case WM_ERASEBKGND:
        case WM_WINDOWPOSCHANGING:
        case WM_WINDOWPOSCHANGED:
        case WM_NCCALCSIZE:
        case WM_NCPAINT:
        case WM_NCHITTEST:
        case WM_DEVICECHANGE:
            break;

        default:
            msg.message = message;
            msg.flags = sent|wparam|lparam;
            if (defwndproc_counter) msg.flags |= defwinproc;
            msg.wParam = wParam;
            msg.lParam = lParam;
            add_message(sequences, LB_SEQ_INDEX, &msg);
    }

    defwndproc_counter++;
    ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
    defwndproc_counter--;

    return ret;
}

131 132 133
static HWND create_listbox(DWORD add_style, HWND parent)
{
    INT_PTR ctl_id = 0;
134
    WNDPROC oldproc;
135 136 137
    HWND handle;

    if (parent)
138
        ctl_id = ID_LISTBOX;
139

140
    handle = CreateWindowA(WC_LISTBOXA, "TestList", (LBS_STANDARD & ~LBS_SORT) | add_style, 0, 0, 100, 100,
141 142 143 144 145 146 147 148
        parent, (HMENU)ctl_id, NULL, 0);
    ok(handle != NULL, "Failed to create listbox window.\n");

    SendMessageA(handle, LB_ADDSTRING, 0, (LPARAM) strings[0]);
    SendMessageA(handle, LB_ADDSTRING, 0, (LPARAM) strings[1]);
    SendMessageA(handle, LB_ADDSTRING, 0, (LPARAM) strings[2]);
    SendMessageA(handle, LB_ADDSTRING, 0, (LPARAM) strings[3]);

149 150 151
    oldproc = (WNDPROC)SetWindowLongPtrA(handle, GWLP_WNDPROC, (LONG_PTR)listbox_wnd_proc);
    SetWindowLongPtrA(handle, GWLP_USERDATA, (LONG_PTR)oldproc);

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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
    return handle;
}

struct listbox_prop
{
    DWORD add_style;
};

struct listbox_stat
{
    int selected, anchor, caret, selcount;
};

struct listbox_test
{
    struct listbox_stat  init,  init_todo;
    struct listbox_stat click, click_todo;
    struct listbox_stat  step,  step_todo;
    struct listbox_stat   sel,   sel_todo;
};

static void listbox_query(HWND handle, struct listbox_stat *results)
{
    results->selected = SendMessageA(handle, LB_GETCURSEL, 0, 0);
    results->anchor   = SendMessageA(handle, LB_GETANCHORINDEX, 0, 0);
    results->caret    = SendMessageA(handle, LB_GETCARETINDEX, 0, 0);
    results->selcount = SendMessageA(handle, LB_GETSELCOUNT, 0, 0);
}

static void buttonpress(HWND handle, WORD x, WORD y)
{
    LPARAM lp = x + (y << 16);

    SendMessageA(handle, WM_LBUTTONDOWN, MK_LBUTTON, lp);
    SendMessageA(handle, WM_LBUTTONUP, 0, lp);
}

static void keypress(HWND handle, WPARAM keycode, BYTE scancode, BOOL extended)
{
    LPARAM lp = 1 + (scancode << 16) + (extended ? KEYEVENTF_EXTENDEDKEY : 0);

    SendMessageA(handle, WM_KEYDOWN, keycode, lp);
    SendMessageA(handle, WM_KEYUP  , keycode, lp | 0xc000000);
}

#define listbox_field_ok(t, s, f, got) \
  ok (t.s.f==got.f, "style %#x, step " #s ", field " #f \
199
      ": expected %d, got %d\n", style, t.s.f, got.f)
200 201 202 203 204 205 206 207 208 209

#define listbox_todo_field_ok(t, s, f, got) \
  todo_wine_if (t.s##_todo.f) { listbox_field_ok(t, s, f, got); }

#define listbox_ok(t, s, got) \
  listbox_todo_field_ok(t, s, selected, got); \
  listbox_todo_field_ok(t, s, anchor, got); \
  listbox_todo_field_ok(t, s, caret, got); \
  listbox_todo_field_ok(t, s, selcount, got)

210
static void run_test(DWORD style, const struct listbox_test test)
211
{
212 213 214 215 216 217 218 219 220
    static const struct message delete_seq[] =
    {
        { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
        { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
        { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
        { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
        { LB_RESETCONTENT, sent|wparam|lparam|defwinproc, 0, 0 },
        { 0 }
    };
221
    struct listbox_stat answer;
222
    int i, res, count;
223
    RECT second_item;
224 225 226
    HWND hLB;

    hLB = create_listbox (style, 0);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    listbox_query (hLB, &answer);
    listbox_ok (test, init, answer);

    SendMessageA(hLB, LB_GETITEMRECT, 1, (LPARAM) &second_item);
    buttonpress(hLB, (WORD)second_item.left, (WORD)second_item.top);

    listbox_query(hLB, &answer);
    listbox_ok(test, click, answer);

    keypress(hLB, VK_DOWN, 0x50, TRUE);

    listbox_query(hLB, &answer);
    listbox_ok(test, step, answer);

    DestroyWindow(hLB);

244
    hLB = create_listbox(style, 0);
245 246 247 248 249

    SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
    listbox_query(hLB, &answer);
    listbox_ok(test, sel, answer);

250
    for (i = 0; i < 4 && !(style & LBS_NODATA); i++)
251 252 253 254 255 256
    {
        DWORD size = SendMessageA(hLB, LB_GETTEXTLEN, i, 0);
        int resA, resW;
        WCHAR *txtw;
        CHAR *txt;

257
        txt = heap_alloc_zero(size + 1);
258 259 260
        resA = SendMessageA(hLB, LB_GETTEXT, i, (LPARAM)txt);
        ok(!strcmp(txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);

261
        txtw = heap_alloc_zero((size + 1) * sizeof(*txtw));
262
        resW = SendMessageW(hLB, LB_GETTEXT, i, (LPARAM)txtw);
263 264 265
        ok(resA == resW, "Unexpected text length.\n");
        WideCharToMultiByte(CP_ACP, 0, txtw, -1, txt, size, NULL, NULL);
        ok(!strcmp (txt, strings[i]), "Unexpected string for item %d, %s vs %s.\n", i, txt, strings[i]);
266

267 268
        heap_free(txtw);
        heap_free(txt);
269 270 271 272 273 274 275 276 277
    }

    /* Confirm the count of items, and that an invalid delete does not remove anything */
    res = SendMessageA(hLB, LB_GETCOUNT, 0, 0);
    ok(res == 4, "Expected 4 items, got %d\n", res);
    res = SendMessageA(hLB, LB_DELETESTRING, -1, 0);
    ok(res == LB_ERR, "Expected LB_ERR items, got %d\n", res);
    res = SendMessageA(hLB, LB_DELETESTRING, 4, 0);
    ok(res == LB_ERR, "Expected LB_ERR items, got %d\n", res);
278 279 280 281 282 283 284 285 286 287 288
    count = SendMessageA(hLB, LB_GETCOUNT, 0, 0);
    ok(count == 4, "Unexpected item count %d.\n", count);

    /* Emptying listbox sends a LB_RESETCONTENT to itself. */
    flush_sequence(sequences, LB_SEQ_INDEX);
    for (i = count; i--;)
    {
        res = SendMessageA(hLB, LB_DELETESTRING, 0, 0);
        ok(res == i, "Unexpected return value %d.\n", res);
    }
    ok_sequence(sequences, LB_SEQ_INDEX, delete_seq, "Emptying listbox", FALSE);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

    DestroyWindow(hLB);
}

static void test_item_height(void)
{
    INT itemHeight;
    TEXTMETRICA tm;
    HFONT font;
    HWND hLB;
    HDC hdc;

    hLB = create_listbox (0, 0);
    ok ((hdc = GetDCEx( hLB, 0, DCX_CACHE )) != 0, "Can't get hdc\n");
    ok ((font = GetCurrentObject(hdc, OBJ_FONT)) != 0, "Can't get the current font\n");
    ok (GetTextMetricsA( hdc, &tm ), "Can't read font metrics\n");
    ReleaseDC( hLB, hdc);

    ok (SendMessageA(hLB, WM_SETFONT, (WPARAM)font, 0) == 0, "Can't set font\n");

    itemHeight = SendMessageA(hLB, LB_GETITEMHEIGHT, 0, 0);
    ok (itemHeight == tm.tmHeight, "Item height wrong, got %d, expecting %d\n", itemHeight, tm.tmHeight);

    DestroyWindow (hLB);

314
    hLB = CreateWindowA(WC_LISTBOXA, "TestList", LBS_OWNERDRAWVARIABLE,  0, 0, 100, 100, NULL, NULL, NULL, 0);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

    itemHeight = SendMessageA(hLB, LB_GETITEMHEIGHT, 0, 0);
    ok(itemHeight > 0 && itemHeight <= tm.tmHeight, "Unexpected item height %d, expected %d.\n",
        itemHeight, tm.tmHeight);
    itemHeight = SendMessageA(hLB, LB_GETITEMHEIGHT, 5, 0);
    ok(itemHeight > 0 && itemHeight <= tm.tmHeight, "Unexpected item height %d, expected %d.\n",
        itemHeight, tm.tmHeight);
    itemHeight = SendMessageA(hLB, LB_GETITEMHEIGHT, -5, 0);
    ok(itemHeight > 0 && itemHeight <= tm.tmHeight, "Unexpected item height %d, expected %d.\n",
        itemHeight, tm.tmHeight);

    DestroyWindow (hLB);
}

static int got_selchange;

331
static LRESULT WINAPI main_window_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
332
{
333 334 335 336 337 338 339 340 341 342
    static LONG defwndproc_counter = 0;
    struct message m = { 0 };
    LRESULT ret;

    m.message = msg;
    m.flags = sent|wparam|lparam;
    if (defwndproc_counter) m.flags |= defwinproc;
    m.wParam = wParam;
    m.lParam = lParam;

343 344
    switch (msg)
    {
345 346
    case WM_MEASUREITEM:
    {
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        MEASUREITEMSTRUCT *mis = (void *)lParam;
        BOOL is_unicode_data = FALSE;
        MEASURE_ITEM_STRUCT mi;

        if (mis->CtlType == ODT_LISTBOX)
        {
            HWND ctrl = GetDlgItem(hwnd, mis->CtlID);
            is_unicode_data = GetWindowLongA(ctrl, GWL_STYLE) & LBS_HASSTRINGS;
        }

        mi.u.wp = 0;
        mi.u.item.CtlType = mis->CtlType;
        mi.u.item.CtlID = mis->CtlID;
        mi.u.item.itemID = mis->itemID;
        mi.u.item.wParam = wParam;

        m.wParam = mi.u.wp;
        if (is_unicode_data)
            m.lParam = mis->itemData ? hash_Ly_W((const WCHAR *)mis->itemData) : 0;
        else
            m.lParam = mis->itemData ? hash_Ly((const char *)mis->itemData) : 0;
        add_message(sequences, PARENT_SEQ_INDEX, &m);

        ok(wParam == mis->CtlID, "got wParam=%08lx, expected %08x\n", wParam, mis->CtlID);
        ok(mis->CtlType == ODT_LISTBOX, "mi->CtlType = %u\n", mis->CtlType);
        ok(mis->CtlID == 1, "mi->CtlID = %u\n", mis->CtlID);
        ok(mis->itemHeight, "mi->itemHeight = 0\n");

        break;
    }
    case WM_COMPAREITEM:
    {
        COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)lParam;
        HWND ctrl = GetDlgItem(hwnd, cis->CtlID);
        BOOL is_unicode_data = TRUE;
382

383 384 385 386
        ok(wParam == cis->CtlID, "expected %#x, got %#lx\n", cis->CtlID, wParam);
        ok(cis->hwndItem == ctrl, "expected %p, got %p\n", ctrl, cis->hwndItem);
        ok((int)cis->itemID1 >= 0, "expected >= 0, got %d\n", cis->itemID1);
        ok((int)cis->itemID2 == -1, "expected -1, got %d\n", cis->itemID2);
387

388 389
        if (cis->CtlType == ODT_LISTBOX)
            is_unicode_data = GetWindowLongA(ctrl, GWL_STYLE) & LBS_HASSTRINGS;
390

391
        if (is_unicode_data)
392
        {
393 394
            m.wParam = cis->itemData1 ? hash_Ly_W((const WCHAR *)cis->itemData1) : 0;
            m.lParam = cis->itemData2 ? hash_Ly_W((const WCHAR *)cis->itemData2) : 0;
395 396 397
        }
        else
        {
398 399
            m.wParam = cis->itemData1 ? hash_Ly((const char *)cis->itemData1) : 0;
            m.lParam = cis->itemData2 ? hash_Ly((const char *)cis->itemData2) : 0;
400
        }
401
        add_message(sequences, PARENT_SEQ_INDEX, &m);
402 403
        break;
    }
404 405 406
    case WM_DRAWITEM:
    {
        RECT rc_item, rc_client, rc_clip;
407
        DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lParam;
408

409
        ok(wParam == dis->CtlID, "got wParam=%08lx instead of %08x\n", wParam, dis->CtlID);
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
        ok(dis->CtlType == ODT_LISTBOX, "wrong CtlType %04x\n", dis->CtlType);

        GetClientRect(dis->hwndItem, &rc_client);
        GetClipBox(dis->hDC, &rc_clip);
        ok(EqualRect(&rc_client, &rc_clip) || IsRectEmpty(&rc_clip),
           "client rect of the listbox should be equal to the clip box,"
           "or the clip box should be empty\n");

        SendMessageA(dis->hwndItem, LB_GETITEMRECT, dis->itemID, (LPARAM)&rc_item);
        ok(EqualRect(&dis->rcItem, &rc_item), "item rects are not equal\n");

        break;
    }

    case WM_COMMAND:
425
        if (HIWORD( wParam ) == LBN_SELCHANGE) got_selchange++;
426 427 428 429 430 431
        break;

    default:
        break;
    }

432 433 434 435 436
    defwndproc_counter++;
    ret = DefWindowProcA(hwnd, msg, wParam, lParam);
    defwndproc_counter--;

    return msg == WM_COMPAREITEM ? -1 : ret;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
}

static HWND create_parent( void )
{
    static ATOM class;
    WNDCLASSA cls;

    if (!class)
    {
        cls.style = 0;
        cls.lpfnWndProc = main_window_proc;
        cls.cbClsExtra = 0;
        cls.cbWndExtra = 0;
        cls.hInstance = GetModuleHandleA(NULL);
        cls.hIcon = 0;
        cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
        cls.hbrBackground = GetStockObject(WHITE_BRUSH);
        cls.lpszMenuName = NULL;
        cls.lpszClassName = "main_window_class";
        class = RegisterClassA( &cls );
    }

    return CreateWindowExA(0, "main_window_class", NULL, WS_POPUP | WS_VISIBLE, 100, 100, 400, 400, GetDesktopWindow(),
        0, GetModuleHandleA(NULL), NULL);
}

static void test_ownerdraw(void)
{
465 466 467 468 469
    static const DWORD styles[] =
    {
        0,
        LBS_NODATA
    };
470 471 472
    HWND parent, hLB;
    INT ret;
    RECT rc;
473
    UINT i;
474 475

    parent = create_parent();
476
    ok(parent != NULL, "Failed to create parent window.\n");
477

478 479 480 481
    for (i = 0; i < ARRAY_SIZE(styles); i++)
    {
        hLB = create_listbox(LBS_OWNERDRAWFIXED | WS_CHILD | WS_VISIBLE | styles[i], parent);
        ok(hLB != NULL, "Failed to create listbox window.\n");
482

483 484
        SetForegroundWindow(hLB);
        UpdateWindow(hLB);
485

486 487 488
        /* make height short enough */
        SendMessageA(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
        SetWindowPos(hLB, 0, 0, 0, 100, rc.bottom - rc.top + 1, SWP_NOZORDER | SWP_NOMOVE);
489

490 491 492 493
        /* make 0 item invisible */
        SendMessageA(hLB, LB_SETTOPINDEX, 1, 0);
        ret = SendMessageA(hLB, LB_GETTOPINDEX, 0, 0);
        ok(ret == 1, "wrong top index %d\n", ret);
494

495 496 497
        SendMessageA(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
        ok(!IsRectEmpty(&rc), "empty item rect\n");
        ok(rc.top < 0, "rc.top is not negative (%d)\n", rc.top);
498

499
        DestroyWindow(hLB);
500

501 502 503 504
        /* Both FIXED and VARIABLE, FIXED should override VARIABLE. */
        hLB = CreateWindowA(WC_LISTBOXA, "TestList", LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE | styles[i],
            0, 0, 100, 100, NULL, NULL, NULL, 0);
        ok(hLB != NULL, "last error 0x%08x\n", GetLastError());
505

506
        ok(GetWindowLongA(hLB, GWL_STYLE) & LBS_OWNERDRAWVARIABLE, "Unexpected window style.\n");
507

508 509 510 511
        ret = SendMessageA(hLB, LB_INSERTSTRING, -1, 0);
        ok(ret == 0, "Unexpected return value %d.\n", ret);
        ret = SendMessageA(hLB, LB_INSERTSTRING, -1, 0);
        ok(ret == 1, "Unexpected return value %d.\n", ret);
512

513 514
        ret = SendMessageA(hLB, LB_SETITEMHEIGHT, 0, 13);
        ok(ret == LB_OKAY, "Failed to set item height, %d.\n", ret);
515

516 517
        ret = SendMessageA(hLB, LB_GETITEMHEIGHT, 0, 0);
        ok(ret == 13, "Unexpected item height %d.\n", ret);
518

519 520
        ret = SendMessageA(hLB, LB_SETITEMHEIGHT, 1, 42);
        ok(ret == LB_OKAY, "Failed to set item height, %d.\n", ret);
521

522 523
        ret = SendMessageA(hLB, LB_GETITEMHEIGHT, 0, 0);
        ok(ret == 42, "Unexpected item height %d.\n", ret);
524

525 526
        ret = SendMessageA(hLB, LB_GETITEMHEIGHT, 1, 0);
        ok(ret == 42, "Unexpected item height %d.\n", ret);
527

528 529
        DestroyWindow (hLB);
    }
530

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    DestroyWindow(parent);
}

#define listbox_test_query(exp, got) \
  ok(exp.selected == got.selected, "expected selected %d, got %d\n", exp.selected, got.selected); \
  ok(exp.anchor == got.anchor, "expected anchor %d, got %d\n", exp.anchor, got.anchor); \
  ok(exp.caret == got.caret, "expected caret %d, got %d\n", exp.caret, got.caret); \
  ok(exp.selcount == got.selcount, "expected selcount %d, got %d\n", exp.selcount, got.selcount);

static void test_LB_SELITEMRANGE(void)
{
    static const struct listbox_stat test_nosel = { 0, LB_ERR, 0, 0 };
    static const struct listbox_stat test_1 = { 0, LB_ERR, 0, 2 };
    static const struct listbox_stat test_2 = { 0, LB_ERR, 0, 3 };
    static const struct listbox_stat test_3 = { 0, LB_ERR, 0, 4 };
    struct listbox_stat answer;
    HWND hLB;
    INT ret;

    hLB = create_listbox(LBS_EXTENDEDSEL, 0);
551
    ok(hLB != NULL, "Failed to create listbox window.\n");
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_1, answer);

    SendMessageA(hLB, LB_SETSEL, FALSE, -1);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(0, 4));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_3, answer);

    SendMessageA(hLB, LB_SETSEL, FALSE, -1);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(-5, 5));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    SendMessageA(hLB, LB_SETSEL, FALSE, -1);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(2, 10));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_1, answer);

    SendMessageA(hLB, LB_SETSEL, FALSE, -1);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(4, 10));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    SendMessageA(hLB, LB_SETSEL, FALSE, -1);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(10, 1));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_2, answer);

    SendMessageA(hLB, LB_SETSEL, FALSE, -1);
    listbox_query(hLB, &answer);
    listbox_test_query(test_nosel, answer);

    ret = SendMessageA(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, -1));
    ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
    listbox_query(hLB, &answer);
    listbox_test_query(test_2, answer);

    DestroyWindow(hLB);
}

static void test_LB_SETCURSEL(void)
{
    HWND parent, hLB;
    INT ret;

    parent = create_parent();
    ok(parent != NULL, "Failed to create parent window.\n");

    hLB = create_listbox(LBS_NOINTEGRALHEIGHT | WS_CHILD, parent);
    ok(hLB != NULL, "Failed to create listbox.\n");

    SendMessageA(hLB, LB_SETITEMHEIGHT, 0, 32);

631 632 633
    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

634 635 636 637 638
    ret = SendMessageA(hLB, LB_SETCURSEL, 2, 0);
    ok(ret == 2, "LB_SETCURSEL returned %d instead of 2\n", ret);
    ret = GetScrollPos(hLB, SB_VERT);
    ok(ret == 0, "expected vscroll 0, got %d\n", ret);

639 640 641
    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

642 643 644 645 646
    ret = SendMessageA(hLB, LB_SETCURSEL, 3, 0);
    ok(ret == 3, "LB_SETCURSEL returned %d instead of 3\n", ret);
    ret = GetScrollPos(hLB, SB_VERT);
    ok(ret == 1, "expected vscroll 1, got %d\n", ret);

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    DestroyWindow(hLB);

    hLB = create_listbox(0, 0);
    ok(hLB != NULL, "Failed to create ListBox window.\n");

    ret = SendMessageA(hLB, LB_SETCURSEL, 1, 0);
    ok(ret == 1, "Unexpected return value %d.\n", ret);

    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    DestroyWindow(hLB);

    /* LBS_EXTENDEDSEL */
    hLB = create_listbox(LBS_EXTENDEDSEL, 0);
    ok(hLB != NULL, "Failed to create listbox.\n");

    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    ret = SendMessageA(hLB, LB_SETCURSEL, 2, 0);
    ok(ret == -1, "LB_SETCURSEL returned %d instead of 2\n", ret);

    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    DestroyWindow(hLB);

    /* LBS_MULTIPLESEL */
    hLB = create_listbox(LBS_MULTIPLESEL, 0);
    ok(hLB != NULL, "Failed to create listbox.\n");

    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    ret = SendMessageA(hLB, LB_SETCURSEL, 2, 0);
    ok(ret == -1, "LB_SETCURSEL returned %d instead of 2\n", ret);

    ret = SendMessageA(hLB, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

691 692 693
    DestroyWindow(hLB);
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
static void test_LB_SETSEL(void)
{
    HWND list;
    int ret;

    /* LBS_EXTENDEDSEL */
    list = create_listbox(LBS_EXTENDEDSEL, 0);
    ok(list != NULL, "Failed to create ListBox window.\n");

    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    ret = SendMessageA(list, LB_SETSEL, TRUE, 0);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == 0, "Unexpected anchor index %d.\n", ret);
710 711
    ret = SendMessageA(list, LB_GETCARETINDEX, 0, 0);
    ok(ret == 0, "Unexpected caret index %d.\n", ret);
712 713 714 715 716

    ret = SendMessageA(list, LB_SETSEL, TRUE, 1);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == 1, "Unexpected anchor index %d.\n", ret);
717 718
    ret = SendMessageA(list, LB_GETCARETINDEX, 0, 0);
    ok(ret == 1, "Unexpected caret index %d.\n", ret);
719 720 721 722 723

    ret = SendMessageA(list, LB_SETSEL, FALSE, 1);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == 1, "Unexpected anchor index %d.\n", ret);
724 725
    ret = SendMessageA(list, LB_GETCARETINDEX, 0, 0);
    ok(ret == 1, "Unexpected caret index %d.\n", ret);
726 727 728 729 730 731 732 733 734 735 736 737 738 739

    DestroyWindow(list);

    /* LBS_MULTIPLESEL */
    list = create_listbox(LBS_MULTIPLESEL, 0);
    ok(list != NULL, "Failed to create ListBox window.\n");

    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == -1, "Unexpected anchor index %d.\n", ret);

    ret = SendMessageA(list, LB_SETSEL, TRUE, 0);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == 0, "Unexpected anchor index %d.\n", ret);
740 741
    ret = SendMessageA(list, LB_GETCARETINDEX, 0, 0);
    ok(ret == 0, "Unexpected caret index %d.\n", ret);
742 743 744 745 746

    ret = SendMessageA(list, LB_SETSEL, TRUE, 1);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == 1, "Unexpected anchor index %d.\n", ret);
747 748
    ret = SendMessageA(list, LB_GETCARETINDEX, 0, 0);
    ok(ret == 1, "Unexpected caret index %d.\n", ret);
749 750 751 752 753

    ret = SendMessageA(list, LB_SETSEL, FALSE, 1);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(list, LB_GETANCHORINDEX, 0, 0);
    ok(ret == 1, "Unexpected anchor index %d.\n", ret);
754 755
    ret = SendMessageA(list, LB_GETCARETINDEX, 0, 0);
    ok(ret == 1, "Unexpected caret index %d.\n", ret);
756 757 758 759

    DestroyWindow(list);
}

760 761 762 763 764
static void test_listbox_height(void)
{
    HWND hList;
    int r, id;

765
    hList = CreateWindowA( WC_LISTBOXA, "list test", 0,
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
                          1, 1, 600, 100, NULL, NULL, NULL, NULL );
    ok( hList != NULL, "failed to create listbox\n");

    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
    ok( id == 0, "item id wrong\n");

    r = SendMessageA( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 20, 0 ));
    ok( r == 0, "send message failed\n");

    r = SendMessageA(hList, LB_GETITEMHEIGHT, 0, 0 );
    ok( r == 20, "height wrong\n");

    r = SendMessageA( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0, 30 ));
    ok( r == -1, "send message failed\n");

    r = SendMessageA(hList, LB_GETITEMHEIGHT, 0, 0 );
    ok( r == 20, "height wrong\n");

784 785 786
    /* Before Windows 10 1709 (or 1703?) the item height was limited to 255.
     * Since then, with comctl32 V6 the limit is 65535.
     */
787
    r = SendMessageA( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 256, 0 ));
788 789 790 791 792 793 794 795 796 797
    ok(r == 0 || broken(r == -1), "Failed to set item height, %d.\n", r);
    if (r == -1)
    {
        r = SendMessageA(hList, LB_GETITEMHEIGHT, 0, 0 );
        ok( r == 20, "Unexpected item height %d.\n", r);
    }
    else
    {
        r = SendMessageA(hList, LB_GETITEMHEIGHT, 0, 0 );
        ok( r == 256, "Unexpected item height %d.\n", r);
798

799 800 801 802 803 804
        r = SendMessageA( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 65535, 0 ));
        ok(r == 0, "Failed to set item height, %d.\n", r);

        r = SendMessageA(hList, LB_GETITEMHEIGHT, 0, 0 );
        ok( r == 65535, "Unexpected item height %d.\n", r);
    }
805 806 807 808 809 810 811 812 813 814

    r = SendMessageA( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0xff, 0 ));
    ok( r == 0, "send message failed\n");

    r = SendMessageA(hList, LB_GETITEMHEIGHT, 0, 0 );
    ok( r == 0xff, "height wrong\n");

    DestroyWindow( hList );
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
static void test_changing_selection_styles(void)
{
    static const DWORD styles[] =
    {
        0,
        LBS_NODATA | LBS_OWNERDRAWFIXED
    };
    static const DWORD selstyles[] =
    {
        0,
        LBS_MULTIPLESEL,
        LBS_EXTENDEDSEL,
        LBS_MULTIPLESEL | LBS_EXTENDEDSEL
    };
    static const LONG selexpect_single[]  = { 0, 0, 1 };
    static const LONG selexpect_single2[] = { 1, 0, 0 };
    static const LONG selexpect_multi[]   = { 1, 0, 1 };
    static const LONG selexpect_multi2[]  = { 1, 1, 0 };

    HWND parent, listbox;
    DWORD style;
    LONG ret;
    UINT i, j, k;

    parent = create_parent();
    ok(parent != NULL, "Failed to create parent window.\n");
    for (i = 0; i < ARRAY_SIZE(styles); i++)
    {
        /* Test if changing selection styles affects selection storage */
        for (j = 0; j < ARRAY_SIZE(selstyles); j++)
        {
            LONG setcursel_expect, selitemrange_expect, getselcount_expect;
            const LONG *selexpect;

            listbox = CreateWindowA(WC_LISTBOXA, "TestList", styles[i] | selstyles[j] | WS_CHILD | WS_VISIBLE,
                                    0, 0, 100, 100, parent, (HMENU)ID_LISTBOX, NULL, 0);
            ok(listbox != NULL, "%u: Failed to create ListBox window.\n", j);

            if (selstyles[j] & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL))
            {
                setcursel_expect = LB_ERR;
                selitemrange_expect = LB_OKAY;
                getselcount_expect = 2;
                selexpect = selexpect_multi;
            }
            else
            {
                setcursel_expect = 2;
                selitemrange_expect = LB_ERR;
                getselcount_expect = LB_ERR;
                selexpect = selexpect_single;
            }

            for (k = 0; k < ARRAY_SIZE(selexpect_multi); k++)
            {
                ret = SendMessageA(listbox, LB_INSERTSTRING, -1, (LPARAM)"x");
                ok(ret == k, "%u: Unexpected return value %d, expected %d.\n", j, ret, k);
            }
            ret = SendMessageA(listbox, LB_GETCOUNT, 0, 0);
            ok(ret == ARRAY_SIZE(selexpect_multi), "%u: Unexpected count %d.\n", j, ret);

            /* Select items with different methods */
            ret = SendMessageA(listbox, LB_SETCURSEL, 2, 0);
            ok(ret == setcursel_expect, "%u: Unexpected return value %d.\n", j, ret);
            ret = SendMessageA(listbox, LB_SELITEMRANGE, TRUE, MAKELPARAM(0, 0));
            ok(ret == selitemrange_expect, "%u: Unexpected return value %d.\n", j, ret);
            ret = SendMessageA(listbox, LB_SELITEMRANGE, TRUE, MAKELPARAM(2, 2));
            ok(ret == selitemrange_expect, "%u: Unexpected return value %d.\n", j, ret);

            /* Verify that the proper items are selected */
            for (k = 0; k < ARRAY_SIZE(selexpect_multi); k++)
            {
                ret = SendMessageA(listbox, LB_GETSEL, k, 0);
                ok(ret == selexpect[k], "%u: Unexpected selection state %d, expected %d.\n",
                    j, ret, selexpect[k]);
            }

            /* Now change the selection style */
            style = GetWindowLongA(listbox, GWL_STYLE);
            ok((style & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)) == selstyles[j],
                "%u: unexpected window styles %#x.\n", j, style);
            if (selstyles[j] & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL))
                style &= ~selstyles[j];
            else
                style |= LBS_MULTIPLESEL | LBS_EXTENDEDSEL;
            SetWindowLongA(listbox, GWL_STYLE, style);
            style = GetWindowLongA(listbox, GWL_STYLE);
            ok(!(style & selstyles[j]), "%u: unexpected window styles %#x.\n", j, style);

            /* Verify that the same items are selected */
            ret = SendMessageA(listbox, LB_GETSELCOUNT, 0, 0);
            ok(ret == getselcount_expect, "%u: expected %d from LB_GETSELCOUNT, got %d\n",
                j, getselcount_expect, ret);

            for (k = 0; k < ARRAY_SIZE(selexpect_multi); k++)
            {
                ret = SendMessageA(listbox, LB_GETSEL, k, 0);
                ok(ret == selexpect[k], "%u: Unexpected selection state %d, expected %d.\n",
                    j, ret, selexpect[k]);
            }

            /* Lastly see if we can still change the selection as before with old style */
            if (setcursel_expect != LB_ERR) setcursel_expect = 0;
            ret = SendMessageA(listbox, LB_SETCURSEL, 0, 0);
            ok(ret == setcursel_expect, "%u: Unexpected return value %d.\n", j, ret);
            ret = SendMessageA(listbox, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 1));
            ok(ret == selitemrange_expect, "%u: Unexpected return value %d.\n", j, ret);
            ret = SendMessageA(listbox, LB_SELITEMRANGE, FALSE, MAKELPARAM(2, 2));
            ok(ret == selitemrange_expect, "%u: Unexpected return value %d.\n", j, ret);

            /* And verify the selections */
            selexpect = (selstyles[j] & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)) ? selexpect_multi2 : selexpect_single2;
            ret = SendMessageA(listbox, LB_GETSELCOUNT, 0, 0);
            ok(ret == getselcount_expect, "%u: expected %d from LB_GETSELCOUNT, got %d\n",
                j, getselcount_expect, ret);

            for (k = 0; k < ARRAY_SIZE(selexpect_multi); k++)
            {
                ret = SendMessageA(listbox, LB_GETSEL, k, 0);
                ok(ret == selexpect[k], "%u: Unexpected selection state %d, expected %d.\n",
                    j, ret, selexpect[k]);
            }

            DestroyWindow(listbox);
        }
    }
    DestroyWindow(parent);
}

944 945 946 947 948 949
static void test_itemfrompoint(void)
{
    /* WS_POPUP is required in order to have a more accurate size calculation (
       without caption). LBS_NOINTEGRALHEIGHT is required in order to test
       behavior of partially-displayed item.
     */
950
    HWND hList = CreateWindowA( WC_LISTBOXA, "list test",
951 952 953 954 955 956
                               WS_VISIBLE|WS_POPUP|LBS_NOINTEGRALHEIGHT,
                               1, 1, 600, 100, NULL, NULL, NULL, NULL );
    ULONG r, id;
    RECT rc;

    r = SendMessageA(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 30 ));
957
    ok( r == MAKELPARAM(0xffff, 1), "Unexpected ret value %#x.\n", r );
958 959

    r = SendMessageA(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( 700, 30 ));
960
    ok( r == MAKELPARAM(0xffff, 1), "Unexpected ret value %#x.\n", r );
961 962

    r = SendMessageA(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( 30, 300 ));
963
    ok( r == MAKELPARAM(0xffff, 1), "Unexpected ret value %#x.\n", r );
964 965 966 967 968 969 970 971 972 973

    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
    ok( id == 0, "item id wrong\n");
    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi1");
    ok( id == 1, "item id wrong\n");

    r = SendMessageA(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 30 ));
    ok( r == 0x1, "ret %x\n", r );

    r = SendMessageA(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 601 ));
974
    ok( r == MAKELPARAM(1, 1), "Unexpected ret value %#x.\n", r );
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

    /* Resize control so that below assertions about sizes are valid */
    r = SendMessageA( hList, LB_GETITEMRECT, 0, (LPARAM)&rc);
    ok( r == 1, "ret %x\n", r);
    r = MoveWindow(hList, 1, 1, 600, (rc.bottom - rc.top + 1) * 9 / 2, TRUE);
    ok( r != 0, "ret %x\n", r);

    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi2");
    ok( id == 2, "item id wrong\n");
    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi3");
    ok( id == 3, "item id wrong\n");
    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi4");
    ok( id == 4, "item id wrong\n");
    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi5");
    ok( id == 5, "item id wrong\n");
    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi6");
    ok( id == 6, "item id wrong\n");
    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi7");
    ok( id == 7, "item id wrong\n");

    /* Set the listbox up so that id 1 is at the top, this leaves 5
       partially visible at the bottom and 6, 7 are invisible */

    SendMessageA( hList, LB_SETTOPINDEX, 1, 0);
    r = SendMessageA( hList, LB_GETTOPINDEX, 0, 0);
    ok( r == 1, "top %d\n", r);

    r = SendMessageA( hList, LB_GETITEMRECT, 5, (LPARAM)&rc);
    ok( r == 1, "ret %x\n", r);
    r = SendMessageA( hList, LB_GETITEMRECT, 6, (LPARAM)&rc);
    ok( r == 0, "ret %x\n", r);

    r = SendMessageA( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(/* x */ 10, /* y */ 10) );
    ok( r == 1, "ret %x\n", r);

    r = SendMessageA( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(1000, 10) );
    ok( r == 0x10001, "ret %x\n", r );

    r = SendMessageA( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, -10) );
    ok( r == 0x10001, "ret %x\n", r );

    r = SendMessageA( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, 100) );
    ok( r == 0x10005, "item %x\n", r );

    r = SendMessageA( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, 200) );
    ok( r == 0x10005, "item %x\n", r );

    DestroyWindow( hList );
}

static void test_listbox_item_data(void)
{
    HWND hList;
    int r, id;

1030
    hList = CreateWindowA( WC_LISTBOXA, "list test", 0,
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
                          1, 1, 600, 100, NULL, NULL, NULL, NULL );
    ok( hList != NULL, "failed to create listbox\n");

    id = SendMessageA( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
    ok( id == 0, "item id wrong\n");

    r = SendMessageA( hList, LB_SETITEMDATA, 0, MAKELPARAM( 20, 0 ));
    ok(r == TRUE, "LB_SETITEMDATA returned %d instead of TRUE\n", r);

    r = SendMessageA( hList, LB_GETITEMDATA, 0, 0);
    ok( r == 20, "get item data failed\n");

    DestroyWindow( hList );
}

static void test_listbox_LB_DIR(void)
{
1048
    char path[MAX_PATH], curdir[MAX_PATH];
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    HWND hList;
    int res, itemCount;
    int itemCount_justFiles;
    int itemCount_justDrives;
    int itemCount_allFiles;
    int itemCount_allDirs;
    int i;
    char pathBuffer[MAX_PATH];
    char * p;
    char driveletter;
    const char *wildcard = "*";
    HANDLE file;
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    BOOL ret;

    GetCurrentDirectoryA(ARRAY_SIZE(curdir), curdir);

    GetTempPathA(ARRAY_SIZE(path), path);
    ret = SetCurrentDirectoryA(path);
    ok(ret, "Failed to set current directory.\n");

    ret = CreateDirectoryA("lb_dir_test", NULL);
    ok(ret, "Failed to create test directory.\n");
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

    file = CreateFileA( "wtest1.tmp.c", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
    ok(file != INVALID_HANDLE_VALUE, "Error creating the test file: %d\n", GetLastError());
    CloseHandle( file );

    /* NOTE: for this test to succeed, there must be no subdirectories
       under the current directory. In addition, there must be at least
       one file that fits the wildcard w*.c . Normally, the test
       directory itself satisfies both conditions.
     */
1081
    hList = CreateWindowA( WC_LISTBOXA, "list test", WS_VISIBLE|WS_POPUP,
1082
                          1, 1, 600, 100, NULL, NULL, NULL, NULL );
1083
    ok(hList != NULL, "Failed to create listbox window.\n");
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

    /* Test for standard usage */

    /* This should list all the files in the test directory. */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, 0, (LPARAM)pathBuffer);
    if (res == -1)  /* "*" wildcard doesn't work on win9x */
    {
        wildcard = "*.*";
        strcpy(pathBuffer, wildcard);
        res = SendMessageA(hList, LB_DIR, 0, (LPARAM)pathBuffer);
    }
    ok (res >= 0, "SendMessage(LB_DIR, 0, *) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount > 0, "SendMessage(LB_DIR) did NOT fill the listbox!\n");
    itemCount_allFiles = itemCount;
    ok(res + 1 == itemCount,
        "SendMessage(LB_DIR, 0, *) returned incorrect index (expected %d got %d)!\n",
        itemCount - 1, res);

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, 0, (LPARAM)pathBuffer);
    ok (res == -1, "SendMessage(LB_DIR, 0, %s) returned %d, expected -1\n", BAD_EXTENSION, res);

    /* There should be NO content in the listbox */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == 0, "SendMessage(LB_DIR) DID fill the listbox!\n");


    /* This should list all the w*.c files in the test directory
     * As of this writing, this includes win.c, winstation.c, wsprintf.c
     */
    strcpy(pathBuffer, "w*.c");
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, 0, (LPARAM)pathBuffer);
    ok (res >= 0, "SendMessage(LB_DIR, 0, w*.c) failed - 0x%08x\n", GetLastError());

    /* Path specification does NOT converted to uppercase */
    ok (!strcmp(pathBuffer, "w*.c"),
        "expected no change to pathBuffer, got %s\n", pathBuffer);

    /* There should be some content in the listbox */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount > 0, "SendMessage(LB_DIR) did NOT fill the listbox!\n");
    itemCount_justFiles = itemCount;
    ok(res + 1 == itemCount,
        "SendMessage(LB_DIR, 0, w*.c) returned incorrect index (expected %d got %d)!\n",
        itemCount - 1, res);

    /* Every single item in the control should start with a w and end in .c */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        ok(((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
            (*(p-1) == 'c' || *(p-1) == 'C') &&
            (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
    }

    /* Test DDL_DIRECTORY */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY, (LPARAM)pathBuffer);
    ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY, *) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox.
     * All files plus "[..]"
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    itemCount_allDirs = itemCount - itemCount_allFiles;
    ok (itemCount >= itemCount_allFiles,
        "SendMessage(LB_DIR, DDL_DIRECTORY, *) filled with %d entries, expected > %d\n",
        itemCount, itemCount_allFiles);
    ok(res + 1 == itemCount,
        "SendMessage(LB_DIR, DDL_DIRECTORY, *) returned incorrect index (expected %d got %d)!\n",
        itemCount - 1, res);

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY, (LPARAM)pathBuffer);
    ok (res == -1, "SendMessage(LB_DIR, DDL_DIRECTORY, %s) returned %d, expected -1\n", BAD_EXTENSION, res);

    /* There should be NO content in the listbox */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == 0, "SendMessage(LB_DIR) DID fill the listbox!\n");

    /* Test DDL_DIRECTORY */
    strcpy(pathBuffer, "w*.c");
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY, (LPARAM)pathBuffer);
    ok (res >= 0, "SendMessage(LB_DIR, DDL_DIRECTORY, w*.c) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. Since the parent directory does not
     * fit w*.c, there should be exactly the same number of items as without DDL_DIRECTORY
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justFiles,
        "SendMessage(LB_DIR, DDL_DIRECTORY, w*.c) filled with %d entries, expected %d\n",
        itemCount, itemCount_justFiles);
    ok(res + 1 == itemCount,
        "SendMessage(LB_DIR, DDL_DIRECTORY, w*.c) returned incorrect index (expected %d got %d)!\n",
        itemCount - 1, res);

    /* Every single item in the control should start with a w and end in .c. */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        ok(
            ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
            (*(p-1) == 'c' || *(p-1) == 'C') &&
            (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
    }

    /* Test DDL_DRIVES|DDL_EXCLUSIVE */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res >= 0, "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *)  failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be at least one element before, since the string "[-c-]" should
     * have been added. Depending on the user setting, more drives might have
     * been added.
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount >= 1,
        "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *) filled with %d entries, expected at least %d\n",
        itemCount, 1);
    itemCount_justDrives = itemCount;
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *) returned incorrect index!\n");

    /* Every single item in the control should fit the format [-c-] */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        ok( strlen(pathBuffer) == 5, "Length of drive string is not 5\n" );
        ok( sscanf(pathBuffer, "[-%c-]", &driveletter) == 1, "Element %d (%s) does not fit [-X-]\n", i, pathBuffer);
        ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        if (!(driveletter >= 'a' && driveletter <= 'z'))
        {
            /* Correct after invalid entry is found */
            itemCount_justDrives--;
        }
    }

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, %s) returned %d, expected %d\n",
        BAD_EXTENSION, res, itemCount_justDrives -1);

    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives, "SendMessage(LB_DIR) returned %d expected %d\n",
        itemCount, itemCount_justDrives);

    /* Test DDL_DRIVES. */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DRIVES, (LPARAM)pathBuffer);
    ok (res > 0, "SendMessage(LB_DIR, DDL_DRIVES, *)  failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be at least one element before, since the string "[-c-]" should
     * have been added. Depending on the user setting, more drives might have
     * been added.
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives + itemCount_allFiles,
        "SendMessage(LB_DIR, DDL_DRIVES, *) filled with %d entries, expected %d\n",
        itemCount, itemCount_justDrives + itemCount_allFiles);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DRIVES, *) returned incorrect index!\n");

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DRIVES, (LPARAM)pathBuffer);
    ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DRIVES, %s) returned %d, expected %d\n",
        BAD_EXTENSION, res, itemCount_justDrives -1);

    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);

    /* Test DDL_DRIVES. */
    strcpy(pathBuffer, "w*.c");
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DRIVES, (LPARAM)pathBuffer);
    ok (res > 0, "SendMessage(LB_DIR, DDL_DRIVES, w*.c)  failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be at least one element before, since the string "[-c-]" should
     * have been added. Depending on the user setting, more drives might have
     * been added.
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives + itemCount_justFiles,
        "SendMessage(LB_DIR, DDL_DRIVES, w*.c) filled with %d entries, expected %d\n",
        itemCount, itemCount_justDrives + itemCount_justFiles);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DRIVES, w*.c) returned incorrect index!\n");

    /* Every single item in the control should fit the format [-c-], or w*.c */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1)
        {
            ok( strlen(pathBuffer) == 5, "Length of drive string is not 5\n" );
            ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        }
        else
        {
            ok(
                ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
                (*(p-1) == 'c' || *(p-1) == 'C') &&
                (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
        }
    }

    /* Test DDL_DIRECTORY|DDL_DRIVES. This does *not* imply DDL_EXCLUSIVE */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES, (LPARAM)pathBuffer);
    ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, *) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be exactly the number of plain files, plus the number of mapped drives.
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_allFiles + itemCount_justDrives + itemCount_allDirs,
        "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES) filled with %d entries, expected %d\n",
        itemCount, itemCount_allFiles + itemCount_justDrives + itemCount_allDirs);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, w*.c) returned incorrect index!\n");

    /* Every single item in the control should start with a w and end in .c,
     * except for the "[..]" string, which should appear exactly as it is,
     * and the mapped drives in the format "[-X-]".
     */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1)
            ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
    }

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES, (LPARAM)pathBuffer);
    ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, %s) returned %d, expected %d\n",
        BAD_EXTENSION, res, itemCount_justDrives -1);

    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);

    /* Test DDL_DIRECTORY|DDL_DRIVES. */
    strcpy(pathBuffer, "w*.c");
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES, (LPARAM)pathBuffer);
    ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, w*.c) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be exactly the number of plain files, plus the number of mapped drives.
     */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justFiles + itemCount_justDrives,
        "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES) filled with %d entries, expected %d\n",
        itemCount, itemCount_justFiles + itemCount_justDrives);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, w*.c) returned incorrect index!\n");

    /* Every single item in the control should start with a w and end in .c,
     * except the mapped drives in the format "[-X-]". The "[..]" directory
     * should not appear.
     */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1)
            ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        else
            ok(
                ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
                (*(p-1) == 'c' || *(p-1) == 'C') &&
                (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
    }

    /* Test DDL_DIRECTORY|DDL_EXCLUSIVE. */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
1392
    ok (res != -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, *) failed err %u\n",
1393 1394 1395 1396 1397 1398 1399 1400
        GetLastError());

    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_allDirs,
        "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
        itemCount, itemCount_allDirs);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, *) returned incorrect index!\n");

1401
    if (itemCount)
1402 1403 1404
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(hList, LB_GETTEXT, 0, (LPARAM)pathBuffer);
1405
        ok( !strcmp(pathBuffer, "[..]"), "First element is %s, not [..]\n", pathBuffer);
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
    }

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res == -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, %s) returned %d, expected %d\n",
        BAD_EXTENSION, res, -1);

    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);


    /* Test DDL_DIRECTORY|DDL_EXCLUSIVE. */
    strcpy(pathBuffer, "w*.c");
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res == LB_ERR, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, w*.c) returned %d expected %d\n", res, LB_ERR);

    /* There should be no elements, since "[..]" does not fit w*.c */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == 0,
        "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
        itemCount, 0);

    /* Test DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE. */
    strcpy(pathBuffer, wildcard);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c,) failed - 0x%08x\n", GetLastError());

    /* There should be no plain files on the listbox */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives + itemCount_allDirs,
        "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
        itemCount, itemCount_justDrives + itemCount_allDirs);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c) returned incorrect index!\n");

    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1)
            ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        else
            ok( pathBuffer[0] == '[' && pathBuffer[strlen(pathBuffer)-1] == ']',
                "Element %d (%s) does not fit expected [...]\n", i, pathBuffer);
    }

    /* This tests behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, %s) returned %d, expected %d\n",
        BAD_EXTENSION, res, itemCount_justDrives -1);

    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);

    /* Test DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE. */
    strcpy(pathBuffer, "w*.c");
    SendMessageA(hList, LB_RESETCONTENT, 0, 0);
    res = SendMessageA(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
    ok (res >= 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c,) failed - 0x%08x\n", GetLastError());

    /* There should be no plain files on the listbox, and no [..], since it does not fit w*.c */
    itemCount = SendMessageA(hList, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives,
        "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
        itemCount, itemCount_justDrives);
    ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c) returned incorrect index!\n");

    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
        ok (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1, "Element %d (%s) does not fit [-X-]\n", i, pathBuffer);
        ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
    }
    DestroyWindow(hList);

    DeleteFileA( "wtest1.tmp.c" );
1490 1491 1492
    RemoveDirectoryA("lb_dir_test");

    SetCurrentDirectoryA(curdir);
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
}

static HWND g_listBox;
static HWND g_label;

#define ID_TEST_LABEL    1001
#define ID_TEST_LISTBOX  1002

static BOOL on_listbox_container_create(HWND hwnd, CREATESTRUCTA *lpcs)
{
1503
    g_label = CreateWindowA(WC_STATICA, "Contents of static control before DlgDirList.",
1504 1505 1506
        WS_CHILD | WS_VISIBLE, 10, 10, 512, 32, hwnd, (HMENU)ID_TEST_LABEL, NULL, 0);
    if (!g_label) return FALSE;

1507
    g_listBox = CreateWindowA(WC_LISTBOXA, "DlgDirList test",
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
        WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | WS_VSCROLL, 10, 60, 256, 256,
        hwnd, (HMENU)ID_TEST_LISTBOX, NULL, 0);
    if (!g_listBox) return FALSE;

    return TRUE;
}

static LRESULT CALLBACK listbox_container_window_procA(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
    LRESULT result = 0;

    switch (uiMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
        result = on_listbox_container_create(hwnd, (CREATESTRUCTA *)lParam) ? 0 : (LRESULT)-1;
        break;
    default:
        result = DefWindowProcA(hwnd, uiMsg, wParam, lParam);
        break;
    }
    return result;
}

static BOOL RegisterListboxWindowClass(HINSTANCE hInst)
{
    WNDCLASSA cls;

    cls.style = 0;
    cls.cbClsExtra = 0;
    cls.cbWndExtra = 0;
    cls.hInstance = hInst;
    cls.hIcon = NULL;
    cls.hCursor = LoadCursorA (NULL, (LPCSTR)IDC_ARROW);
    cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    cls.lpszMenuName = NULL;
    cls.lpfnWndProc = listbox_container_window_procA;
    cls.lpszClassName = "ListboxContainerClass";
    if (!RegisterClassA (&cls)) return FALSE;

    return TRUE;
}

static void test_listbox_dlgdir(void)
{
    HINSTANCE hInst;
    HWND hWnd;
    int res, itemCount;
    int itemCount_allDirs;
    int itemCount_justFiles;
    int itemCount_justDrives;
    int i;
    char pathBuffer[MAX_PATH];
    char itemBuffer[MAX_PATH];
    char tempBuffer[MAX_PATH];
    char * p;
    char driveletter;
    HANDLE file;
1568
    BOOL ret;
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580

    file = CreateFileA( "wtest1.tmp.c", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
    ok(file != INVALID_HANDLE_VALUE, "Error creating the test file: %d\n", GetLastError());
    CloseHandle( file );

    /* NOTE: for this test to succeed, there must be no subdirectories
       under the current directory. In addition, there must be at least
       one file that fits the wildcard w*.c . Normally, the test
       directory itself satisfies both conditions.
     */

    hInst = GetModuleHandleA(0);
1581 1582
    ret = RegisterListboxWindowClass(hInst);
    ok(ret, "Failed to register test class.\n");
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

    hWnd = CreateWindowA("ListboxContainerClass", "ListboxContainerClass",
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            NULL, NULL, hInst, 0);
    ok(hWnd != NULL, "Failed to create container window.\n");

    /* Test for standard usage */

    /* The following should be overwritten by the directory path */
    SendMessageA(g_label, WM_SETTEXT, 0, (LPARAM)"default contents");

    /* This should list all the w*.c files in the test directory
     * As of this writing, this includes win.c, winstation.c, wsprintf.c
     */
    strcpy(pathBuffer, "w*.c");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, 0);
    ok (res == 1, "DlgDirList(*.c, 0) returned %d - expected 1 - 0x%08x\n", res, GetLastError());

    /* Path specification gets converted to uppercase */
    ok (!strcmp(pathBuffer, "W*.C"),
        "expected conversion to uppercase, got %s\n", pathBuffer);

    /* Loaded path should have overwritten the label text */
    SendMessageA(g_label, WM_GETTEXT, MAX_PATH, (LPARAM)pathBuffer);
    ok (strcmp("default contents", pathBuffer), "DlgDirList() did not modify static control!\n");

    /* There should be some content in the listbox */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount > 0, "DlgDirList() did NOT fill the listbox!\n");
    itemCount_justFiles = itemCount;

    /* Every single item in the control should start with a w and end in .c */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        ok(((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
            (*(p-1) == 'c' || *(p-1) == 'C') &&
            (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
    }

    /* Test behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, 0);
    ok (res == 1, "DlgDirList(%s, 0) returned %d expected 1\n", BAD_EXTENSION, res);

    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == 0, "DlgDirList() DID fill the listbox!\n");

    /* Test DDL_DIRECTORY */
    strcpy(pathBuffer, "w*.c");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY);
    ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be exactly more elements than before, since the directories should
     * have been added.
     */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    itemCount_allDirs = itemCount - itemCount_justFiles;
    ok (itemCount >= itemCount_justFiles, "DlgDirList(DDL_DIRECTORY) filled with %d entries, expected > %d\n",
        itemCount, itemCount_justFiles);

    /* Every single item in the control should start with a w and end in .c,
     * except for the "[..]" string, which should appear exactly as it is.
     */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        ok( (pathBuffer[0] == '[' && pathBuffer[strlen(pathBuffer)-1] == ']') ||
            ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
            (*(p-1) == 'c' || *(p-1) == 'C') &&
            (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
    }

    /* Test behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY);
    ok (res == 1, "DlgDirList(%s, DDL_DIRECTORY) returned %d expected 1\n", BAD_EXTENSION, res);

    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_allDirs, "DlgDirList() incorrectly filled the listbox! (expected %d got %d)\n",
        itemCount_allDirs, itemCount);
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
        ok( pathBuffer[0] == '[' && pathBuffer[strlen(pathBuffer)-1] == ']',
            "Element %d (%s) does not fit requested [...]\n", i, pathBuffer);
    }

    /* Test DDL_DRIVES. At least on WinXP-SP2, this implies DDL_EXCLUSIVE */
    strcpy(pathBuffer, "w*.c");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DRIVES);
    ok (res == 1, "DlgDirList(*.c, DDL_DRIVES) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be at least one element before, since the string "[-c-]" should
     * have been added. Depending on the user setting, more drives might have
     * been added.
     */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount >= 1,
        "DlgDirList(DDL_DRIVES) filled with %d entries, expected at least %d\n",
        itemCount, 1);
    itemCount_justDrives = itemCount;

    /* Every single item in the control should fit the format [-c-] */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
        ok( strlen(pathBuffer) == 5, "Length of drive string is not 5\n" );
        ok( sscanf(pathBuffer, "[-%c-]", &driveletter) == 1, "Element %d (%s) does not fit [-X-]\n", i, pathBuffer);
        ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        if (!(driveletter >= 'a' && driveletter <= 'z')) {
            /* Correct after invalid entry is found */
            trace("removing count of invalid entry %s\n", pathBuffer);
            itemCount_justDrives--;
        }
    }

    /* Test behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DRIVES);
    ok (res == 1, "DlgDirList(%s, DDL_DRIVES) returned %d expected 1\n", BAD_EXTENSION, res);

    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives, "DlgDirList() incorrectly filled the listbox!\n");

    /* Test DDL_DIRECTORY|DDL_DRIVES. This does *not* imply DDL_EXCLUSIVE */
    strcpy(pathBuffer, "w*.c");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_DRIVES);
    ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY|DDL_DRIVES) failed - 0x%08x\n", GetLastError());

    /* There should be some content in the listbox. In particular, there should
     * be exactly the number of plain files, plus the number of mapped drives,
     * plus one "[..]"
     */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justFiles + itemCount_justDrives + itemCount_allDirs,
        "DlgDirList(DDL_DIRECTORY|DDL_DRIVES) filled with %d entries, expected %d\n",
        itemCount, itemCount_justFiles + itemCount_justDrives + itemCount_allDirs);

    /* Every single item in the control should start with a w and end in .c,
     * except for the "[..]" string, which should appear exactly as it is,
     * and the mapped drives in the format "[-X-]".
     */
    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
        p = pathBuffer + strlen(pathBuffer);
        if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1)
            ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        else
            ok( (pathBuffer[0] == '[' && pathBuffer[strlen(pathBuffer)-1] == ']') ||
                ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
                (*(p-1) == 'c' || *(p-1) == 'C') &&
                (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
    }

    /* Test behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_DRIVES);
    ok (res == 1, "DlgDirList(%s, DDL_DIRECTORY|DDL_DRIVES) returned %d expected 1\n", BAD_EXTENSION, res);

    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives + itemCount_allDirs,
        "DlgDirList() incorrectly filled the listbox! (expected %d got %d)\n",
        itemCount_justDrives + itemCount_allDirs, itemCount);

    /* Test DDL_DIRECTORY|DDL_EXCLUSIVE. */
    strcpy(pathBuffer, "w*.c");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_EXCLUSIVE);
    ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY|DDL_EXCLUSIVE) failed - 0x%08x\n", GetLastError());

    /* There should be exactly one element: "[..]" */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_allDirs,
        "DlgDirList(DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
        itemCount, itemCount_allDirs);

    if (itemCount && GetCurrentDirectoryA( MAX_PATH, pathBuffer ) > 3)  /* there's no [..] in drive root */
    {
        memset(pathBuffer, 0, MAX_PATH);
        SendMessageA(g_listBox, LB_GETTEXT, 0, (LPARAM)pathBuffer);
        ok( !strcmp(pathBuffer, "[..]"), "First (and only) element is not [..]\n");
    }

    /* Test behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_EXCLUSIVE);
    ok (res == 1, "DlgDirList(%s, DDL_DIRECTORY|DDL_EXCLUSIVE) returned %d expected 1\n", BAD_EXTENSION, res);

    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_allDirs, "DlgDirList() incorrectly filled the listbox!\n");

    /* Test DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE. */
    strcpy(pathBuffer, "w*.c");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE);
    ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) failed - 0x%08x\n", GetLastError());

    /* There should be no plain files on the listbox */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives + itemCount_allDirs,
        "DlgDirList(DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
        itemCount, itemCount_justDrives + itemCount_allDirs);

    for (i = 0; i < itemCount; i++)
    {
        memset(pathBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
        if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1)
            ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
        else
            ok( pathBuffer[0] == '[' && pathBuffer[strlen(pathBuffer)-1] == ']',
                "Element %d (%s) does not fit expected [...]\n", i, pathBuffer);
    }

    /* Test behavior when no files match the wildcard */
    strcpy(pathBuffer, BAD_EXTENSION);
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE);
    ok (res == 1, "DlgDirList(%s, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) returned %d expected 1\n", BAD_EXTENSION, res);

    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    ok (itemCount == itemCount_justDrives + itemCount_allDirs, "DlgDirList() incorrectly filled the listbox!\n");

    /* Now test DlgDirSelectEx() in normal operation */
    /* Fill with everything - drives, directory and all plain files. */
    strcpy(pathBuffer, "*");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, DDL_DIRECTORY|DDL_DRIVES);
    ok (res != 0, "DlgDirList(*, DDL_DIRECTORY|DDL_DRIVES) failed - 0x%08x\n", GetLastError());

    SendMessageA(g_listBox, LB_SETCURSEL, -1, 0); /* Unselect any current selection */
    memset(pathBuffer, 0, MAX_PATH);
    SetLastError(0xdeadbeef);
    res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
    ok (GetLastError() == 0xdeadbeef,
        "DlgDirSelectEx() with no selection modified last error code from 0xdeadbeef to 0x%08x\n",
        GetLastError());
    ok (res == 0, "DlgDirSelectEx() with no selection returned %d, expected 0\n", res);
    /* WinXP-SP2 leaves pathBuffer untouched, but Win98 fills it with garbage. */
    /*
1834
    ok (!*pathBuffer, "DlgDirSelectEx() with no selection filled buffer with %s\n", pathBuffer);
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
    */
    /* Test proper drive/dir/file recognition */
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    for (i = 0; i < itemCount; i++)
    {
        memset(itemBuffer, 0, MAX_PATH);
        memset(pathBuffer, 0, MAX_PATH);
        memset(tempBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)itemBuffer);
        res = SendMessageA(g_listBox, LB_SETCURSEL, i, 0);
        ok (res == i, "SendMessageA(LB_SETCURSEL, %d) failed\n", i);
        if (sscanf(itemBuffer, "[-%c-]", &driveletter) == 1)
        {
            /* Current item is a drive letter */
            SetLastError(0xdeadbeef);
            res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
            ok (GetLastError() == 0xdeadbeef,
               "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
                i, GetLastError());
            ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);

            /* For drive letters, DlgDirSelectEx tacks on a colon */
            ok (pathBuffer[0] == driveletter && pathBuffer[1] == ':' && pathBuffer[2] == '\0',
                "%d: got \"%s\" expected \"%c:\"\n", i, pathBuffer, driveletter);
        }
        else if (itemBuffer[0] == '[')
        {
            /* Current item is the parent directory */
            SetLastError(0xdeadbeef);
            res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
            ok (GetLastError() == 0xdeadbeef,
               "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
                i, GetLastError());
            ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);

            /* For directories, DlgDirSelectEx tacks on a backslash */
            p = pathBuffer + strlen(pathBuffer);
            ok (*(p-1) == '\\', "DlgDirSelectEx did NOT tack on a backslash to dir, got %s\n", pathBuffer);

            tempBuffer[0] = '[';
            lstrcpynA(tempBuffer + 1, pathBuffer, strlen(pathBuffer));
            strcat(tempBuffer, "]");
            ok (!strcmp(tempBuffer, itemBuffer), "Formatted directory should be %s, got %s\n", tempBuffer, itemBuffer);
        }
        else
        {
            /* Current item is a plain file */
            SetLastError(0xdeadbeef);
            res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
            ok (GetLastError() == 0xdeadbeef,
               "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
                i, GetLastError());
            ok(res == 0, "DlgDirSelectEx() thinks %s (%s) is a drive/directory!\n", itemBuffer, pathBuffer);

            /* NOTE: WinXP tacks a period on all files that lack an extension. This affects
             * for example, "Makefile", which gets reported as "Makefile."
             */
            strcpy(tempBuffer, itemBuffer);
            if (strchr(tempBuffer, '.') == NULL) strcat(tempBuffer, ".");
            ok (!strcmp(pathBuffer, tempBuffer), "Formatted file should be %s, got %s\n", tempBuffer, pathBuffer);
        }
    }

    DeleteFileA( "wtest1.tmp.c" );

    /* Now test DlgDirSelectEx() in abnormal operation */
    /* Fill list with bogus entries, that look somewhat valid */
    SendMessageA(g_listBox, LB_RESETCONTENT, 0, 0);
    SendMessageA(g_listBox, LB_ADDSTRING, 0, (LPARAM)"[notexist.dir]");
    SendMessageA(g_listBox, LB_ADDSTRING, 0, (LPARAM)"notexist.fil");
    itemCount = SendMessageA(g_listBox, LB_GETCOUNT, 0, 0);
    for (i = 0; i < itemCount; i++)
    {
        memset(itemBuffer, 0, MAX_PATH);
        memset(pathBuffer, 0, MAX_PATH);
        memset(tempBuffer, 0, MAX_PATH);
        driveletter = '\0';
        SendMessageA(g_listBox, LB_GETTEXT, i, (LPARAM)itemBuffer);
        res = SendMessageA(g_listBox, LB_SETCURSEL, i, 0);
        ok (res == i, "SendMessage(LB_SETCURSEL, %d) failed\n", i);
        if (sscanf(itemBuffer, "[-%c-]", &driveletter) == 1)
        {
            /* Current item is a drive letter */
            SetLastError(0xdeadbeef);
            res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
            ok (GetLastError() == 0xdeadbeef,
               "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
                i, GetLastError());
            ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);

            /* For drive letters, DlgDirSelectEx tacks on a colon */
            ok (pathBuffer[0] == driveletter && pathBuffer[1] == ':' && pathBuffer[2] == '\0',
                "%d: got \"%s\" expected \"%c:\"\n", i, pathBuffer, driveletter);
        }
        else if (itemBuffer[0] == '[')
        {
            /* Current item is the parent directory */
            SetLastError(0xdeadbeef);
            res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
            ok (GetLastError() == 0xdeadbeef,
               "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
                i, GetLastError());
            ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);

            /* For directories, DlgDirSelectEx tacks on a backslash */
            p = pathBuffer + strlen(pathBuffer);
            ok (*(p-1) == '\\', "DlgDirSelectEx did NOT tack on a backslash to dir, got %s\n", pathBuffer);

            tempBuffer[0] = '[';
            lstrcpynA(tempBuffer + 1, pathBuffer, strlen(pathBuffer));
            strcat(tempBuffer, "]");
            ok (!strcmp(tempBuffer, itemBuffer), "Formatted directory should be %s, got %s\n", tempBuffer, itemBuffer);
        }
        else
        {
            /* Current item is a plain file */
            SetLastError(0xdeadbeef);
            res = DlgDirSelectExA(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
            ok (GetLastError() == 0xdeadbeef,
               "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
                i, GetLastError());
            ok(res == 0, "DlgDirSelectEx() thinks %s (%s) is a drive/directory!\n", itemBuffer, pathBuffer);

            /* NOTE: WinXP and Win98 tack a period on all files that lack an extension.
             * This affects for example, "Makefile", which gets reported as "Makefile."
             */
            strcpy(tempBuffer, itemBuffer);
            if (strchr(tempBuffer, '.') == NULL) strcat(tempBuffer, ".");
            ok (!strcmp(pathBuffer, tempBuffer), "Formatted file should be %s, got %s\n", tempBuffer, pathBuffer);
        }
    }

    /* Test behavior when loading folders from root with and without wildcard */
    strcpy(pathBuffer, "C:\\");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, 0, DDL_DIRECTORY | DDL_EXCLUSIVE);
    ok(res, "DlgDirList failed to list C:\\ folders\n");
1972
    ok(!strcmp(pathBuffer, "*"), "DlgDirList set the invalid path spec '%s', expected '*'\n", pathBuffer);
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

    strcpy(pathBuffer, "C:\\*");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, 0, DDL_DIRECTORY | DDL_EXCLUSIVE);
    ok(res, "DlgDirList failed to list C:\\* folders\n");
    ok(!strcmp(pathBuffer, "*"), "DlgDirList set the invalid path spec '%s', expected '*'\n", pathBuffer);

    /* Try loading files from an invalid folder */
    SetLastError(0xdeadbeef);
    strcpy(pathBuffer, "C:\\INVALID$$DIR");
    res = DlgDirListA(hWnd, pathBuffer, ID_TEST_LISTBOX, 0, DDL_DIRECTORY | DDL_EXCLUSIVE);
1983 1984
    ok(!res, "DlgDirList should have failed with 0 but %d was returned\n", res);
    ok(GetLastError() == ERROR_NO_WILDCARD_CHARACTERS,
1985 1986 1987 1988 1989 1990 1991
       "GetLastError should return 0x589, got 0x%X\n",GetLastError());

    DestroyWindow(hWnd);
}

static void test_set_count( void )
{
1992 1993 1994 1995 1996
    static const DWORD styles[] =
    {
        LBS_OWNERDRAWFIXED,
        LBS_HASSTRINGS,
    };
1997
    HWND parent, listbox;
1998
    unsigned int i;
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
    LONG ret;
    RECT r;

    parent = create_parent();
    listbox = create_listbox( LBS_OWNERDRAWFIXED | LBS_NODATA | WS_CHILD | WS_VISIBLE, parent );

    UpdateWindow( listbox );
    GetUpdateRect( listbox, &r, TRUE );
    ok( IsRectEmpty( &r ), "got non-empty rect\n");

    ret = SendMessageA( listbox, LB_SETCOUNT, 100, 0 );
    ok( ret == 0, "got %d\n", ret );
    ret = SendMessageA( listbox, LB_GETCOUNT, 0, 0 );
    ok( ret == 100, "got %d\n", ret );

    GetUpdateRect( listbox, &r, TRUE );
    ok( !IsRectEmpty( &r ), "got empty rect\n");

    ValidateRect( listbox, NULL );
    GetUpdateRect( listbox, &r, TRUE );
    ok( IsRectEmpty( &r ), "got non-empty rect\n");

    ret = SendMessageA( listbox, LB_SETCOUNT, 99, 0 );
    ok( ret == 0, "got %d\n", ret );

    GetUpdateRect( listbox, &r, TRUE );
    ok( !IsRectEmpty( &r ), "got empty rect\n");

2027 2028 2029 2030 2031
    ret = SendMessageA( listbox, LB_SETCOUNT, -5, 0 );
    ok( ret == 0, "got %d\n", ret );
    ret = SendMessageA( listbox, LB_GETCOUNT, 0, 0 );
    ok( ret == -5, "got %d\n", ret );

2032
    DestroyWindow( listbox );
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045

    for (i = 0; i < ARRAY_SIZE(styles); ++i)
    {
        listbox = create_listbox( styles[i] | WS_CHILD | WS_VISIBLE, parent );

        SetLastError( 0xdeadbeef );
        ret = SendMessageA( listbox, LB_SETCOUNT, 100, 0 );
        ok( ret == LB_ERR, "expected %d, got %d\n", LB_ERR, ret );
        ok( GetLastError() == 0xdeadbeef, "Unexpected error %d.\n", GetLastError() );

        DestroyWindow( listbox );
    }

2046 2047 2048 2049 2050
    DestroyWindow( parent );
}

static void test_GetListBoxInfo(void)
{
2051 2052 2053 2054 2055
    static const struct message getlistboxinfo_seq[] =
    {
        { LB_GETLISTBOXINFO, sent },
        { 0 }
    };
2056 2057 2058 2059 2060 2061
    HWND listbox, parent;
    DWORD ret;

    parent = create_parent();
    listbox = create_listbox(WS_CHILD | WS_VISIBLE, parent);

2062
    flush_sequences(sequences, NUM_MSG_SEQUENCES);
2063 2064
    ret = GetListBoxInfo(listbox);
    ok(ret > 0, "got %d\n", ret);
2065
    ok_sequence(sequences, LB_SEQ_INDEX, getlistboxinfo_seq, "GetListBoxInfo()", FALSE);
2066 2067 2068 2069 2070

    DestroyWindow(listbox);
    DestroyWindow(parent);
}

2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
static void test_init_storage( void )
{
    static const DWORD styles[] =
    {
        LBS_HASSTRINGS,
        LBS_NODATA | LBS_OWNERDRAWFIXED,
    };
    HWND parent, listbox;
    LONG ret, items_size;
    int i, j;

    parent = create_parent();
    for (i = 0; i < ARRAY_SIZE(styles); i++)
    {
        listbox = CreateWindowA(WC_LISTBOXA, "TestList", styles[i] | WS_CHILD,
                                0, 0, 100, 100, parent, (HMENU)ID_LISTBOX, NULL, 0);

        items_size = SendMessageA(listbox, LB_INITSTORAGE, 100, 0);
        ok(items_size >= 100, "expected at least 100, got %d\n", items_size);

        ret = SendMessageA(listbox, LB_INITSTORAGE, 0, 0);
        ok(ret == items_size, "expected %d, got %d\n", items_size, ret);

        /* it doesn't grow since the space was already reserved */
        ret = SendMessageA(listbox, LB_INITSTORAGE, items_size, 0);
        ok(ret == items_size, "expected %d, got %d\n", items_size, ret);

        /* it doesn't shrink the reserved space */
        ret = SendMessageA(listbox, LB_INITSTORAGE, 42, 0);
        ok(ret == items_size, "expected %d, got %d\n", items_size, ret);

        /* now populate almost all of it so it's not reserved anymore */
        if (styles[i] & LBS_NODATA)
        {
            ret = SendMessageA(listbox, LB_SETCOUNT, items_size - 1, 0);
            ok(ret == 0, "unexpected return value %d\n", ret);
        }
        else
        {
            for (j = 0; j < items_size - 1; j++)
            {
                ret = SendMessageA(listbox, LB_INSERTSTRING, -1, (LPARAM)"");
                ok(ret == j, "expected %d, got %d\n", j, ret);
            }
        }

        /* we still have one more reserved slot, so it doesn't grow yet */
        ret = SendMessageA(listbox, LB_INITSTORAGE, 1, 0);
        ok(ret == items_size, "expected %d, got %d\n", items_size, ret);

        /* fill the slot and check again, it should grow this time */
        ret = SendMessageA(listbox, LB_INSERTSTRING, -1, (LPARAM)"");
        ok(ret == items_size - 1, "expected %d, got %d\n", items_size - 1, ret);
        ret = SendMessageA(listbox, LB_INITSTORAGE, 0, 0);
        ok(ret == items_size, "expected %d, got %d\n", items_size, ret);
        ret = SendMessageA(listbox, LB_INITSTORAGE, 1, 0);
        ok(ret > items_size, "expected it to grow past %d, got %d\n", items_size, ret);

        DestroyWindow(listbox);
    }
    DestroyWindow(parent);
}

2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
static void test_missing_lbuttonup(void)
{
    HWND listbox, parent, capture;

    parent = create_parent();
    listbox = create_listbox(WS_CHILD | WS_VISIBLE, parent);

    /* Send button down without a corresponding button up */
    SendMessageA(listbox, WM_LBUTTONDOWN, 0, MAKELPARAM(10, 10));
    capture = GetCapture();
    ok(capture == listbox, "got %p expected %p\n", capture, listbox);

    /* Capture is released and LBN_SELCHANGE sent during WM_KILLFOCUS */
    got_selchange = 0;
    SetFocus(NULL);
    capture = GetCapture();
    ok(capture == NULL, "got %p\n", capture);
    ok(got_selchange, "got %d\n", got_selchange);

    DestroyWindow(listbox);
    DestroyWindow(parent);
}

static void test_extents(void)
{
    HWND listbox, parent;
    SCROLLINFO sinfo;
    DWORD res;
    BOOL br;

    parent = create_parent();

    listbox = create_listbox(WS_CHILD | WS_VISIBLE, parent);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 0, "Got wrong initial horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 100, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) == 0,
        "List box should not have a horizontal scroll bar\n");

    /* horizontal extent < width */
    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 64, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 64, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 100, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) == 0,
        "List box should not have a horizontal scroll bar\n");

    /* horizontal extent > width */
    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 184, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 184, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 100, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) == 0,
        "List box should not have a horizontal scroll bar\n");

    DestroyWindow(listbox);

    listbox = create_listbox(WS_CHILD | WS_VISIBLE | WS_HSCROLL, parent);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 0, "Got wrong initial horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 100, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) == 0,
        "List box should not have a horizontal scroll bar\n");

    /* horizontal extent < width */
    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 64, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 64, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 63, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) == 0,
        "List box should not have a horizontal scroll bar\n");

    /* horizontal extent > width */
    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 184, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 184, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 183, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) != 0,
        "List box should have a horizontal scroll bar\n");

    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 0, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 0, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 0, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) == 0,
        "List box should not have a horizontal scroll bar\n");

    DestroyWindow(listbox);


    listbox = create_listbox(WS_CHILD | WS_VISIBLE | WS_HSCROLL | LBS_DISABLENOSCROLL, parent);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 0, "Got wrong initial horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 0, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) != 0,
        "List box should have a horizontal scroll bar\n");

    /* horizontal extent < width */
    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 64, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 64, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 63, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) != 0,
        "List box should have a horizontal scroll bar\n");

    /* horizontal extent > width */
    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 184, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 184, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 183, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) != 0,
        "List box should have a horizontal scroll bar\n");

    SendMessageA(listbox, LB_SETHORIZONTALEXTENT, 0, 0);

    res = SendMessageA(listbox, LB_GETHORIZONTALEXTENT, 0, 0);
    ok(res == 0, "Got wrong horizontal extent: %u\n", res);

    sinfo.cbSize = sizeof(sinfo);
    sinfo.fMask = SIF_RANGE;
    br = GetScrollInfo(listbox, SB_HORZ, &sinfo);
    ok(br == TRUE, "GetScrollInfo failed\n");
    ok(sinfo.nMin == 0, "got wrong min: %u\n", sinfo.nMin);
    ok(sinfo.nMax == 0, "got wrong max: %u\n", sinfo.nMax);
    ok((GetWindowLongA(listbox, GWL_STYLE) & WS_HSCROLL) != 0,
        "List box should have a horizontal scroll bar\n");

    DestroyWindow(listbox);

    DestroyWindow(parent);
}

static void test_listbox(void)
{
    static const struct listbox_test SS =
        /*   {add_style} */
2340
        {{LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
2341 2342 2343 2344 2345 2346
         {     1,      1,      1, LB_ERR}, {0,0,0,0},
         {     2,      2,      2, LB_ERR}, {0,0,0,0},
         {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};

        /* {selected, anchor,  caret, selcount}{TODO fields} */
    static const struct listbox_test SS_NS =
2347
        {{LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
2348 2349 2350 2351 2352
         {     1,      1,      1, LB_ERR}, {0,0,0,0},
         {     2,      2,      2, LB_ERR}, {0,0,0,0},
         {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};

    static const struct listbox_test MS =
2353
        {{     0, LB_ERR,      0,      0}, {0,0,0,0},
2354 2355 2356 2357 2358
         {     1,      1,      1,      1}, {0,0,0,0},
         {     2,      1,      2,      1}, {0,0,0,0},
         {     0, LB_ERR,      0,      2}, {0,0,0,0}};

    static const struct listbox_test MS_NS =
2359
        {{LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
2360 2361 2362 2363 2364
         {     1,      1,      1, LB_ERR}, {0,0,0,0},
         {     2,      2,      2, LB_ERR}, {0,0,0,0},
         {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};

    static const struct listbox_test ES =
2365
        {{     0, LB_ERR,      0,      0}, {0,0,0,0},
2366 2367 2368 2369 2370
         {     1,      1,      1,      1}, {0,0,0,0},
         {     2,      2,      2,      1}, {0,0,0,0},
         {     0, LB_ERR,      0,      2}, {0,0,0,0}};

    static const struct listbox_test ES_NS =
2371
        {{LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
2372 2373 2374 2375 2376
         {     1,      1,      1, LB_ERR}, {0,0,0,0},
         {     2,      2,      2, LB_ERR}, {0,0,0,0},
         {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};

    static const struct listbox_test EMS =
2377
        {{     0, LB_ERR,      0,      0}, {0,0,0,0},
2378 2379 2380 2381 2382
         {     1,      1,      1,      1}, {0,0,0,0},
         {     2,      2,      2,      1}, {0,0,0,0},
         {     0, LB_ERR,      0,      2}, {0,0,0,0}};

    static const struct listbox_test EMS_NS =
2383
        {{LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
2384 2385 2386 2387
         {     1,      1,      1, LB_ERR}, {0,0,0,0},
         {     2,      2,      2, LB_ERR}, {0,0,0,0},
         {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};

2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
    run_test(0, SS);
    run_test(LBS_NOSEL, SS_NS);
    run_test(LBS_MULTIPLESEL, MS);
    run_test(LBS_MULTIPLESEL | LBS_NOSEL, MS_NS);
    run_test(LBS_EXTENDEDSEL, ES);
    run_test(LBS_EXTENDEDSEL | LBS_NOSEL, ES_NS);
    run_test(LBS_EXTENDEDSEL | LBS_MULTIPLESEL, EMS);
    run_test(LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOSEL, EMS_NS);

    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED, SS);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_NOSEL, SS_NS);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_MULTIPLESEL, MS);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_MULTIPLESEL | LBS_NOSEL, MS_NS);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_EXTENDEDSEL, ES);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_EXTENDEDSEL | LBS_NOSEL, ES_NS);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_EXTENDEDSEL | LBS_MULTIPLESEL, EMS);
    run_test(LBS_NODATA | LBS_OWNERDRAWFIXED | LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOSEL, EMS_NS);
2405 2406
}

2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
static const struct message lb_addstring_ownerdraw_parent_seq[] =
{
    { WM_MEASUREITEM, sent|wparam|lparam, 0x1012, 0xf30604ed },
    { WM_MEASUREITEM, sent|wparam|lparam, 0x1112, 0xf30604ee },
    { WM_MEASUREITEM, sent|wparam|lparam, 0x1212, 0xf30604ef },
    { 0 }
};

static const struct message lb_addstring_sort_parent_seq[] =
{
    { WM_MEASUREITEM, sent|wparam|lparam, 0x1012, 0xf30604ed },
    { WM_COMPAREITEM, sent|wparam|lparam, 0xf30604ed, 0xf30604ee },
    { WM_MEASUREITEM, sent|wparam|lparam, 0x1112, 0xf30604ee },
    { WM_COMPAREITEM, sent|wparam|lparam, 0xf30604ed, 0xf30604ef },
    { WM_COMPAREITEM, sent|wparam|lparam, 0xf30604ee, 0xf30604ef },
    { WM_MEASUREITEM, sent|wparam|lparam, 0x1212, 0xf30604ef },
    { 0 }
};

static const struct message empty_seq[] =
{
    { 0 }
};

2431 2432 2433
static void test_WM_MEASUREITEM(void)
{
    HWND parent, listbox;
2434
    LRESULT data, ret;
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447

    parent = create_parent();
    listbox = create_listbox(WS_CHILD | LBS_OWNERDRAWVARIABLE, parent);

    data = SendMessageA(listbox, LB_GETITEMDATA, 0, 0);
    ok(data == (LRESULT)strings[0], "data = %08lx, expected %p\n", data, strings[0]);
    DestroyWindow(parent);

    parent = create_parent();
    listbox = create_listbox(WS_CHILD | LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS, parent);

    data = SendMessageA(listbox, LB_GETITEMDATA, 0, 0);
    ok(!data, "data = %08lx\n", data);
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481

    /* LBS_HASSTRINGS */
    parent = create_parent();
    listbox = CreateWindowExA(WS_EX_NOPARENTNOTIFY, WC_LISTBOXA, NULL,
                              WS_CHILD | LBS_NOTIFY | LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS | WS_VISIBLE,
                              10, 10, 80, 80, parent, (HMENU)ID_LISTBOX, 0, NULL);

    flush_sequences(sequences, NUM_MSG_SEQUENCES);

    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 0");
    ok(ret == 0, "expected 0, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 1");
    ok(ret == 1, "expected 1, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 2");
    ok(ret == 2, "expected 2, got %ld\n", ret);

    ok_sequence(sequences, PARENT_SEQ_INDEX, lb_addstring_ownerdraw_parent_seq,
        "LB_ADDSTRING (LBS_HASSTRINGS, ownerdraw)", FALSE);
    DestroyWindow(listbox);

    /* LBS_SORT, no LBS_HASSTRINGS */
    listbox = CreateWindowExA(WS_EX_NOPARENTNOTIFY, WC_LISTBOXA, NULL,
                              WS_CHILD | LBS_NOTIFY | LBS_OWNERDRAWVARIABLE | LBS_SORT | WS_VISIBLE,
                              10, 10, 80, 80, parent, (HMENU)ID_LISTBOX, 0, NULL);

    flush_sequences(sequences, NUM_MSG_SEQUENCES);

    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 0");
    ok(ret == 0, "expected 0, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 1");
    ok(ret == 1, "expected 1, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 2");
    ok(ret == 2, "expected 2, got %ld\n", ret);

2482
    ok_sequence(sequences, PARENT_SEQ_INDEX, lb_addstring_sort_parent_seq, "LB_ADDSTRING (LBS_SORT)", FALSE);
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
    DestroyWindow(listbox);

    /* LBS_HASSTRINGS */
    listbox = CreateWindowExA(WS_EX_NOPARENTNOTIFY, WC_LISTBOXA, NULL,
                              WS_CHILD | LBS_NOTIFY | LBS_HASSTRINGS | WS_VISIBLE,
                              10, 10, 80, 80, parent, (HMENU)ID_LISTBOX, 0, NULL);

    flush_sequences(sequences, NUM_MSG_SEQUENCES);

    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 2");
    ok(ret == 0, "expected 0, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 0");
    ok(ret == 1, "expected 1, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 1");
    ok(ret == 2, "expected 2, got %ld\n", ret);

    ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "LB_ADDSTRING (LBS_HASSTRINGS)", FALSE);
    DestroyWindow(listbox);

    /* LBS_HASSTRINGS, LBS_SORT */
    listbox = CreateWindowExA(WS_EX_NOPARENTNOTIFY, WC_LISTBOXA, NULL,
                              WS_CHILD | LBS_NOTIFY | LBS_HASSTRINGS | LBS_SORT | WS_VISIBLE,
                              10, 10, 80, 80, parent, (HMENU)ID_LISTBOX, 0, NULL);

    flush_sequences(sequences, NUM_MSG_SEQUENCES);

    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 2");
    ok(ret == 0, "expected 0, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 0");
    ok(ret == 0, "expected 0, got %ld\n", ret);
    ret = SendMessageA(listbox, LB_ADDSTRING, 0, (LPARAM)"item 1");
    ok(ret == 1, "expected 1, got %ld\n", ret);

    ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "LB_ADDSTRING (LBS_HASSTRINGS, LBS_SORT)", FALSE);
    DestroyWindow(listbox);

2519 2520 2521
    DestroyWindow(parent);
}

2522 2523
static void test_LBS_NODATA(void)
{
2524 2525 2526 2527 2528 2529 2530 2531 2532
    static const DWORD invalid_styles[] =
    {
        0,
        LBS_OWNERDRAWVARIABLE,
        LBS_SORT,
        LBS_HASSTRINGS,
        LBS_OWNERDRAWFIXED | LBS_SORT,
        LBS_OWNERDRAWFIXED | LBS_HASSTRINGS,
    };
2533 2534 2535
    static const UINT invalid_idx[] = { -2, 2 };
    static const UINT valid_idx[] = { 0, 1 };
    static const ULONG_PTR zero_data;
2536
    HWND listbox, parent;
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
    INT ret, text_len;
    unsigned int i;
    ULONG_PTR data;
    BOOL is_wow64;

    listbox = CreateWindowA(WC_LISTBOXA, "TestList", LBS_NODATA | LBS_OWNERDRAWFIXED | WS_VISIBLE,
        0, 0, 100, 100, NULL, NULL, NULL, 0);
    ok(listbox != NULL, "Failed to create ListBox window.\n");

    ret = SendMessageA(listbox, LB_INSERTSTRING, -1, 0);
    ok(ret == 0, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(listbox, LB_INSERTSTRING, -1, 0);
    ok(ret == 1, "Unexpected return value %d.\n", ret);
    ret = SendMessageA(listbox, LB_GETCOUNT, 0, 0);
    ok(ret == 2, "Unexpected return value %d.\n", ret);

    /* Invalid indices. */
    for (i = 0; i < ARRAY_SIZE(invalid_idx); ++i)
    {
        ret = SendMessageA(listbox, LB_SETITEMDATA, invalid_idx[i], 42);
        ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
        ret = SendMessageA(listbox, LB_GETTEXTLEN, invalid_idx[i], 0);
        ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
        if (ret == LB_ERR)
        {
            ret = SendMessageA(listbox, LB_GETTEXT, invalid_idx[i], (LPARAM)&data);
            ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
        }
        ret = SendMessageA(listbox, LB_GETITEMDATA, invalid_idx[i], 0);
        ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
    }

    IsWow64Process(GetCurrentProcess(), &is_wow64);
#ifdef _WIN64
    text_len = 8;
#else
    text_len = is_wow64 ? 8 : 4;
#endif

    /* Valid indices. */
    for (i = 0; i < ARRAY_SIZE(valid_idx); ++i)
    {
        ret = SendMessageA(listbox, LB_SETITEMDATA, valid_idx[i], 42);
        ok(ret == TRUE, "Unexpected return value %d.\n", ret);
        ret = SendMessageA(listbox, LB_GETTEXTLEN, valid_idx[i], 0);
2582
    todo_wine_if(is_wow64)
2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
        ok(ret == text_len, "Unexpected return value %d.\n", ret);

        memset(&data, 0xee, sizeof(data));
        ret = SendMessageA(listbox, LB_GETTEXT, valid_idx[i], (LPARAM)&data);
        ok(ret == sizeof(data), "Unexpected return value %d.\n", ret);
        ok(!memcmp(&data, &zero_data, sizeof(data)), "Unexpected item data.\n");

        ret = SendMessageA(listbox, LB_GETITEMDATA, valid_idx[i], 0);
        ok(ret == 0, "Unexpected return value %d.\n", ret);
    }

    /* More messages that don't work with LBS_NODATA. */
2595 2596
    ret = SendMessageA(listbox, LB_FINDSTRING, 1, 0);
    ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
2597 2598
    ret = SendMessageA(listbox, LB_FINDSTRING, 1, 42);
    ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
2599 2600
    ret = SendMessageA(listbox, LB_FINDSTRINGEXACT, 1, 0);
    ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
2601 2602
    ret = SendMessageA(listbox, LB_FINDSTRINGEXACT, 1, 42);
    ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
2603 2604
    ret = SendMessageA(listbox, LB_SELECTSTRING, 1, 0);
    ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);
2605 2606 2607 2608
    ret = SendMessageA(listbox, LB_SELECTSTRING, 1, 42);
    ok(ret == LB_ERR, "Unexpected return value %d.\n", ret);

    DestroyWindow(listbox);
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629

    /* Invalid window style combinations. */
    parent = create_parent();
    ok(parent != NULL, "Failed to create parent window.\n");

    for (i = 0; i < ARRAY_SIZE(invalid_styles); ++i)
    {
        DWORD style;

        listbox = CreateWindowA(WC_LISTBOXA, "TestList", LBS_NODATA | WS_CHILD | invalid_styles[i],
                0, 0, 100, 100, parent, (HMENU)ID_LISTBOX, NULL, 0);
        ok(listbox != NULL, "Failed to create a listbox.\n");

        style = GetWindowLongA(listbox, GWL_STYLE);
        ok((style & invalid_styles[i]) == invalid_styles[i], "%u: unexpected window styles %#x.\n", i, style);
        ret = SendMessageA(listbox, LB_SETCOUNT, 100, 0);
        ok(ret == LB_ERR, "%u: unexpected return value %d.\n", i, ret);
        DestroyWindow(listbox);
    }

    DestroyWindow(parent);
2630 2631
}

2632 2633 2634 2635 2636 2637 2638 2639
START_TEST(listbox)
{
    ULONG_PTR ctx_cookie;
    HANDLE hCtx;

    if (!load_v6_module(&ctx_cookie, &hCtx))
        return;

2640 2641
    init_msg_sequences(sequences, NUM_MSG_SEQUENCES);

2642 2643 2644 2645 2646 2647
    test_listbox();
    test_item_height();
    test_ownerdraw();
    test_LB_SELITEMRANGE();
    test_LB_SETCURSEL();
    test_listbox_height();
2648
    test_changing_selection_styles();
2649 2650 2651 2652 2653
    test_itemfrompoint();
    test_listbox_item_data();
    test_listbox_LB_DIR();
    test_listbox_dlgdir();
    test_set_count();
2654
    test_init_storage();
2655 2656 2657
    test_GetListBoxInfo();
    test_missing_lbuttonup();
    test_extents();
2658
    test_WM_MEASUREITEM();
2659
    test_LB_SETSEL();
2660
    test_LBS_NODATA();
2661 2662 2663

    unload_v6_module(ctx_cookie, hCtx);
}