tooltips.c 42.9 KB
Newer Older
1 2
/*
 * Copyright 2005 Dmitry Timoshkov
3
 * Copyright 2008 Jason Edmeades
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20 21 22
 */

#include <windows.h>
#include <commctrl.h>

23 24
#include "resources.h"

25 26
#include "wine/test.h"

27
#include "v6util.h"
28
#include "msg.h"
29

30 31
#define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)

32 33 34 35 36 37 38 39
enum seq_index
{
    PARENT_SEQ_INDEX = 0,
    NUM_MSG_SEQUENCES
};

static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];

40
static void test_create_tooltip(BOOL is_v6)
41 42 43 44
{
    HWND parent, hwnd;
    DWORD style, exp_style;

45
    parent = CreateWindowExA(0, "static", NULL, WS_POPUP,
46 47
                          0, 0, 0, 0,
                          NULL, NULL, NULL, 0);
48
    ok(parent != NULL, "failed to create parent wnd\n");
49

50
    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0x7fffffff | WS_POPUP,
51 52
                          10, 10, 300, 100,
                          parent, NULL, NULL, 0);
53
    ok(hwnd != NULL, "failed to create tooltip wnd\n");
54

55
    style = GetWindowLongA(hwnd, GWL_STYLE);
56 57
    exp_style = 0x7fffffff | WS_POPUP;
    exp_style &= ~(WS_CHILD | WS_MAXIMIZE | WS_BORDER | WS_DLGFRAME);
58 59
    ok(style == exp_style || broken(style == (exp_style | WS_BORDER)), /* nt4 */
       "wrong style %08x/%08x\n", style, exp_style);
60 61 62

    DestroyWindow(hwnd);

63
    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0,
64 65
                          10, 10, 300, 100,
                          parent, NULL, NULL, 0);
66
    ok(hwnd != NULL, "failed to create tooltip wnd\n");
67

68
    style = GetWindowLongA(hwnd, GWL_STYLE);
69 70 71 72 73 74
    exp_style = WS_POPUP | WS_CLIPSIBLINGS;
    if (!is_v6)
        exp_style |= WS_BORDER;
todo_wine_if(is_v6)
    ok(style == exp_style || broken(style == (exp_style | WS_BORDER)) /* XP */,
        "Unexpected window style %#x.\n", style);
75 76 77 78 79 80

    DestroyWindow(hwnd);

    DestroyWindow(parent);
}

81 82 83 84 85 86 87 88 89 90
/* try to make sure pending X events have been processed before continuing */
static void flush_events(int waitTime)
{
    MSG msg;
    int diff = waitTime;
    DWORD time = GetTickCount() + waitTime;

    while (diff > 0)
    {
        if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min(100,diff), QS_ALLEVENTS) == WAIT_TIMEOUT) break;
91
        while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        diff = time - GetTickCount();
    }
}

static int CD_Stages;
static LRESULT CD_Result;
static HWND g_hwnd;

#define TEST_CDDS_PREPAINT           0x00000001
#define TEST_CDDS_POSTPAINT          0x00000002
#define TEST_CDDS_PREERASE           0x00000004
#define TEST_CDDS_POSTERASE          0x00000008
#define TEST_CDDS_ITEMPREPAINT       0x00000010
#define TEST_CDDS_ITEMPOSTPAINT      0x00000020
#define TEST_CDDS_ITEMPREERASE       0x00000040
#define TEST_CDDS_ITEMPOSTERASE      0x00000080
#define TEST_CDDS_SUBITEM            0x00000100

110
static LRESULT CALLBACK custom_draw_wnd_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
111 112 113 114 115 116 117 118 119 120
{
    switch(msg) {

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_NOTIFY:
        if (((NMHDR *)lParam)->code == NM_CUSTOMDRAW) {
            NMTTCUSTOMDRAW *ttcd = (NMTTCUSTOMDRAW*) lParam;
121 122
            ok(ttcd->nmcd.hdr.hwndFrom == g_hwnd, "Unexpected hwnd source %p (%p)\n",
                 ttcd->nmcd.hdr.hwndFrom, g_hwnd);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            ok(ttcd->nmcd.hdr.idFrom == 0x1234ABCD, "Unexpected id %x\n", (int)ttcd->nmcd.hdr.idFrom);

            switch (ttcd->nmcd.dwDrawStage) {
            case CDDS_PREPAINT     : CD_Stages |= TEST_CDDS_PREPAINT; break;
            case CDDS_POSTPAINT    : CD_Stages |= TEST_CDDS_POSTPAINT; break;
            case CDDS_PREERASE     : CD_Stages |= TEST_CDDS_PREERASE; break;
            case CDDS_POSTERASE    : CD_Stages |= TEST_CDDS_POSTERASE; break;
            case CDDS_ITEMPREPAINT : CD_Stages |= TEST_CDDS_ITEMPREPAINT; break;
            case CDDS_ITEMPOSTPAINT: CD_Stages |= TEST_CDDS_ITEMPOSTPAINT; break;
            case CDDS_ITEMPREERASE : CD_Stages |= TEST_CDDS_ITEMPREERASE; break;
            case CDDS_ITEMPOSTERASE: CD_Stages |= TEST_CDDS_ITEMPOSTERASE; break;
            case CDDS_SUBITEM      : CD_Stages |= TEST_CDDS_SUBITEM; break;
            default: CD_Stages = -1;
            }

            if (ttcd->nmcd.dwDrawStage == CDDS_PREPAINT) return CD_Result;
        }
        /* drop through */

    default:
        return DefWindowProcA(hWnd, msg, wParam, lParam);
    }

    return 0L;
}

static void test_customdraw(void) {
    static struct {
        LRESULT FirstReturnValue;
        int ExpectedCalls;
    } expectedResults[] = {
        /* Valid notification responses */
        {CDRF_DODEFAULT, TEST_CDDS_PREPAINT},
        {CDRF_SKIPDEFAULT, TEST_CDDS_PREPAINT},
        {CDRF_NOTIFYPOSTPAINT, TEST_CDDS_PREPAINT | TEST_CDDS_POSTPAINT},

        /* Invalid notification responses */
        {CDRF_NOTIFYITEMDRAW, TEST_CDDS_PREPAINT},
        {CDRF_NOTIFYPOSTERASE, TEST_CDDS_PREPAINT},
        {CDRF_NEWFONT, TEST_CDDS_PREPAINT}
    };

165
   DWORD       iterationNumber;
166
   WNDCLASSA wc;
167
   POINT orig_pos;
168
   LRESULT ret;
169 170 171 172 173 174 175

   /* Create a class to use the custom draw wndproc */
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = GetModuleHandleA(NULL);
   wc.hIcon = NULL;
176
   wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
177 178 179
   wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
   wc.lpszMenuName = NULL;
   wc.lpszClassName = "CustomDrawClass";
180 181
   wc.lpfnWndProc = custom_draw_wnd_proc;
   RegisterClassA(&wc);
182

183 184
   GetCursorPos(&orig_pos);

185
   for (iterationNumber = 0;
186
        iterationNumber < ARRAY_SIZE(expectedResults);
187 188 189
        iterationNumber++) {

       HWND parent, hwndTip;
190
       RECT rect;
191
       TTTOOLINFOA toolInfo = { 0 };
192 193

       /* Create a main window */
194
       parent = CreateWindowExA(0, "CustomDrawClass", NULL,
195 196 197 198 199
                               WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
                               WS_MAXIMIZEBOX | WS_VISIBLE,
                               50, 50,
                               300, 300,
                               NULL, NULL, NULL, 0);
200
       ok(parent != NULL, "%d: Creation of main window failed\n", iterationNumber);
201 202 203 204 205 206

       /* Make it show */
       ShowWindow(parent, SW_SHOWNORMAL);
       flush_events(100);

       /* Create Tooltip */
207
       hwndTip = CreateWindowExA(WS_EX_TOPMOST, TOOLTIPS_CLASSA,
208 209 210 211
                                NULL, TTS_NOPREFIX | TTS_ALWAYSTIP,
                                CW_USEDEFAULT, CW_USEDEFAULT,
                                CW_USEDEFAULT, CW_USEDEFAULT,
                                parent, NULL, GetModuleHandleA(NULL), 0);
212
       ok(hwndTip != NULL, "%d: Creation of tooltip window failed\n", iterationNumber);
213 214 215 216 217 218 219 220 221 222 223

       /* Set up parms for the wndproc to handle */
       CD_Stages = 0;
       CD_Result = expectedResults[iterationNumber].FirstReturnValue;
       g_hwnd    = hwndTip;

       /* Make it topmost, as per the MSDN */
       SetWindowPos(hwndTip, HWND_TOPMOST, 0, 0, 0, 0,
             SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

       /* Create a tool */
224
       toolInfo.cbSize = TTTOOLINFOA_V1_SIZE;
225 226 227
       toolInfo.hwnd = parent;
       toolInfo.hinst = GetModuleHandleA(NULL);
       toolInfo.uFlags = TTF_SUBCLASS;
228
       toolInfo.uId = 0x1234ABCD;
229 230 231
       toolInfo.lpszText = (LPSTR)"This is a test tooltip";
       toolInfo.lParam = 0xdeadbeef;
       GetClientRect (parent, &toolInfo.rect);
232
       ret = SendMessageA(hwndTip, TTM_ADDTOOLA, 0, (LPARAM)&toolInfo);
233
       ok(ret, "%d: Failed to add the tool.\n", iterationNumber);
234 235

       /* Make tooltip appear quickly */
236
       SendMessageA(hwndTip, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELPARAM(1,0));
237 238

       /* Put cursor inside window, tooltip will appear immediately */
239 240
       GetWindowRect( parent, &rect );
       SetCursorPos( (rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2 );
241 242
       flush_events(200);

243 244 245 246 247
       if (CD_Stages)
       {
           /* Check CustomDraw results */
           ok(CD_Stages == expectedResults[iterationNumber].ExpectedCalls ||
              broken(CD_Stages == (expectedResults[iterationNumber].ExpectedCalls & ~TEST_CDDS_POSTPAINT)), /* nt4 */
248
              "%d: CustomDraw stages %x, expected %x\n", iterationNumber, CD_Stages,
249 250
              expectedResults[iterationNumber].ExpectedCalls);
       }
251

252
       ret = SendMessageA(hwndTip, TTM_GETCURRENTTOOLA, 0, 0);
253
       ok(ret, "%d: Failed to get current tool %#lx.\n", iterationNumber, ret);
254 255 256 257 258 259

       memset(&toolInfo, 0xcc, sizeof(toolInfo));
       toolInfo.cbSize = sizeof(toolInfo);
       toolInfo.lpszText = NULL;
       toolInfo.lpReserved = (void *)0xdeadbeef;
       SendMessageA(hwndTip, TTM_GETCURRENTTOOLA, 0, (LPARAM)&toolInfo);
260 261 262 263 264
       ok(toolInfo.hwnd == parent, "%d: Unexpected hwnd %p.\n", iterationNumber, toolInfo.hwnd);
       ok(toolInfo.hinst == GetModuleHandleA(NULL), "%d: Unexpected hinst %p.\n", iterationNumber, toolInfo.hinst);
       ok(toolInfo.uId == 0x1234abcd, "%d: Unexpected uId %lx.\n", iterationNumber, toolInfo.uId);
       ok(toolInfo.lParam == 0, "%d: Unexpected lParam %lx.\n", iterationNumber, toolInfo.lParam);
       ok(toolInfo.lpReserved == (void *)0xdeadbeef, "%d: Unexpected lpReserved %p.\n", iterationNumber, toolInfo.lpReserved);
265

266 267 268 269 270
       /* Clean up */
       DestroyWindow(hwndTip);
       DestroyWindow(parent);
   }

271
   SetCursorPos(orig_pos.x, orig_pos.y);
272 273
}

274 275
static const CHAR testcallbackA[]  = "callback";

276
static RECT g_ttip_rect;
277 278
static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
279 280 281 282
    static LONG defwndproc_counter = 0;
    struct message msg;
    LRESULT ret;

283 284 285
    if (message == WM_NOTIFY && lParam)
    {
        NMTTDISPINFOA *ttnmdi = (NMTTDISPINFOA*)lParam;
286 287
        NMHDR *hdr = (NMHDR *)lParam;
        RECT rect;
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302
        if (hdr->code != NM_CUSTOMDRAW)
        {
            msg.message = message;
            msg.flags = sent|wparam|lparam;
            if (defwndproc_counter) msg.flags |= defwinproc;
            msg.wParam = wParam;
            msg.lParam = lParam;
            msg.id = hdr->code;
            add_message(sequences, PARENT_SEQ_INDEX, &msg);
        }

        switch (hdr->code)
        {
        case TTN_GETDISPINFOA:
303
            lstrcpyA(ttnmdi->lpszText, testcallbackA);
304 305 306 307 308 309
            break;
        case TTN_SHOW:
            GetWindowRect(hdr->hwndFrom, &rect);
            ok(!EqualRect(&g_ttip_rect, &rect), "Unexpected window rectangle.\n");
            break;
        }
310 311
    }

312 313 314 315 316 317 318 319
    defwndproc_counter++;
    if (IsWindowUnicode(hwnd))
        ret = DefWindowProcW(hwnd, message, wParam, lParam);
    else
        ret = DefWindowProcA(hwnd, message, wParam, lParam);
    defwndproc_counter--;

    return ret;
320 321
}

322
static void register_parent_wnd_class(void)
323 324
{
    WNDCLASSA cls;
325
    BOOL ret;
326 327 328 329 330 331 332

    cls.style = 0;
    cls.lpfnWndProc = parent_wnd_proc;
    cls.cbClsExtra = 0;
    cls.cbWndExtra = 0;
    cls.hInstance = GetModuleHandleA(NULL);
    cls.hIcon = 0;
333
    cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
334 335 336
    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
    cls.lpszMenuName = NULL;
    cls.lpszClassName = "Tooltips test parent class";
337 338
    ret = RegisterClassA(&cls);
    ok(ret, "Failed to register test parent class.\n");
339 340 341 342
}

static HWND create_parent_window(void)
{
343
    return CreateWindowExA(0, "Tooltips test parent class",
344 345 346 347 348 349 350
                          "Tooltips test parent window",
                          WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
                          WS_MAXIMIZEBOX | WS_VISIBLE,
                          0, 0, 100, 100,
                          GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
}

351 352
static void test_gettext(void)
{
353 354
    static const CHAR testtip2A[] = "testtip\ttest2";
    static const CHAR testtipA[] = "testtip";
355
    HWND hwnd, notify;
356 357 358
    TTTOOLINFOA toolinfoA;
    TTTOOLINFOW toolinfoW;
    LRESULT r;
359
    CHAR bufA[16] = "";
360
    WCHAR bufW[10] = { 0 };
361
    DWORD length, style;
362 363 364

    notify = create_parent_window();
    ok(notify != NULL, "Expected notification window to be created\n");
365 366 367 368 369

    /* For bug 14790 - lpszText is NULL */
    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);
370
    ok(hwnd != NULL, "failed to create tooltip wnd\n");
371

372 373
    /* use sizeof(TTTOOLINFOA) instead of TTTOOLINFOA_V1_SIZE so that adding it fails on Win9x */
    /* otherwise it crashes on the NULL lpszText */
374 375 376 377
    toolinfoA.cbSize = sizeof(TTTOOLINFOA);
    toolinfoA.hwnd = NULL;
    toolinfoA.hinst = GetModuleHandleA(NULL);
    toolinfoA.uFlags = 0;
378
    toolinfoA.uId = 0x1234ABCD;
379 380 381
    toolinfoA.lpszText = NULL;
    toolinfoA.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoA.rect);
382
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&toolinfoA);
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    ok(r, "got %ld\n", r);

    toolinfoA.hwnd = NULL;
    toolinfoA.uId = 0x1234abcd;
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
    ok(!*toolinfoA.lpszText, "lpszText should be empty, got %s\n", toolinfoA.lpszText);

    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&toolinfoA);
todo_wine
    ok(!r, "got %ld\n", r);
    ok(toolinfoA.lpszText == NULL, "expected NULL, got %p\n", toolinfoA.lpszText);

    /* NULL hinst, valid resource id for text */
    toolinfoA.cbSize = sizeof(TTTOOLINFOA);
    toolinfoA.hwnd = NULL;
    toolinfoA.hinst = NULL;
    toolinfoA.uFlags = 0;
    toolinfoA.uId = 0x1233abcd;
    toolinfoA.lpszText = MAKEINTRESOURCEA(IDS_TBADD1);
    toolinfoA.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoA.rect);
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&toolinfoA);
    ok(r, "failed to add a tool\n");

    toolinfoA.hwnd = NULL;
    toolinfoA.uId = 0x1233abcd;
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
    ok(!strcmp(toolinfoA.lpszText, "abc"), "got wrong text, %s\n", toolinfoA.lpszText);

    toolinfoA.hinst = (HINSTANCE)0xdeadbee;
    r = SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&toolinfoA);
todo_wine
    ok(!r, "got %ld\n", r);
    ok(toolinfoA.hinst == NULL, "expected NULL, got %p\n", toolinfoA.hinst);

    r = SendMessageA(hwnd, TTM_DELTOOLA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
425

426 427 428 429 430 431 432 433 434 435
    /* add another tool with text */
    toolinfoA.cbSize = sizeof(TTTOOLINFOA);
    toolinfoA.hwnd = NULL;
    toolinfoA.hinst = GetModuleHandleA(NULL);
    toolinfoA.uFlags = 0;
    toolinfoA.uId = 0x1235ABCD;
    strcpy(bufA, testtipA);
    toolinfoA.lpszText = bufA;
    toolinfoA.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoA.rect);
436
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&toolinfoA);
437 438
    ok(r, "Adding the tool to the tooltip failed\n");

439 440
    length = SendMessageA(hwnd, WM_GETTEXTLENGTH, 0, 0);
    ok(length == 0, "Expected 0, got %d\n", length);
441

442 443 444 445 446 447
    toolinfoA.hwnd = NULL;
    toolinfoA.uId = 0x1235abcd;
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
    ok(!strcmp(toolinfoA.lpszText, testtipA), "expected %s, got %p\n", testtipA, toolinfoA.lpszText);
448

449 450 451 452 453 454
    memset(bufA, 0x1f, sizeof(bufA));
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&toolinfoA);
todo_wine
    ok(!r, "got %ld\n", r);
    ok(!strcmp(toolinfoA.lpszText, testtipA), "expected %s, got %p\n", testtipA, toolinfoA.lpszText);
455

456 457
    length = SendMessageA(hwnd, WM_GETTEXTLENGTH, 0, 0);
    ok(length == 0, "Expected 0, got %d\n", length);
458 459 460 461 462 463 464 465 466 467

    /* add another with callback text */
    toolinfoA.cbSize = sizeof(TTTOOLINFOA);
    toolinfoA.hwnd = notify;
    toolinfoA.hinst = GetModuleHandleA(NULL);
    toolinfoA.uFlags = 0;
    toolinfoA.uId = 0x1236ABCD;
    toolinfoA.lpszText = LPSTR_TEXTCALLBACKA;
    toolinfoA.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoA.rect);
468
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&toolinfoA);
469
    ok(r, "Adding the tool to the tooltip failed\n");
470 471 472 473 474 475 476 477 478 479 480 481 482

    toolinfoA.hwnd = notify;
    toolinfoA.uId = 0x1236abcd;
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
    ok(!strcmp(toolinfoA.lpszText, testcallbackA), "lpszText should be an (%s) string\n", testcallbackA);

    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&toolinfoA);
todo_wine
    ok(!r, "got %ld\n", r);
    ok(toolinfoA.lpszText == LPSTR_TEXTCALLBACKA, "expected LPSTR_TEXTCALLBACKA, got %p\n", toolinfoA.lpszText);
483

484
    DestroyWindow(hwnd);
485
    DestroyWindow(notify);
486 487 488 489

    hwnd = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);
490
    ok(hwnd != NULL, "failed to create tooltip wnd\n");
491

492
    toolinfoW.cbSize = TTTOOLINFOW_V2_SIZE + 1;
493 494 495
    toolinfoW.hwnd = NULL;
    toolinfoW.hinst = GetModuleHandleA(NULL);
    toolinfoW.uFlags = 0;
496
    toolinfoW.uId = 0x1234ABCD;
497 498 499
    toolinfoW.lpszText = NULL;
    toolinfoW.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoW.rect);
500
    r = SendMessageW(hwnd, TTM_ADDTOOLW, 0, (LPARAM)&toolinfoW);
501 502 503
    /* Wine currently checks for V3 structure size, which matches what V6 control does.
       Older implementation was never updated to support lpReserved field. */
todo_wine
504
    ok(!r, "Adding the tool to the tooltip succeeded!\n");
505

506 507 508 509 510 511 512 513
    if (0)  /* crashes on NT4 */
    {
        toolinfoW.hwnd = NULL;
        toolinfoW.uId = 0x1234ABCD;
        toolinfoW.lpszText = bufW;
        SendMessageW(hwnd, TTM_GETTEXTW, 0, (LPARAM)&toolinfoW);
        ok(toolinfoW.lpszText[0] == 0, "lpszText should be an empty string\n");
    }
514

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    /* text with embedded tabs */
    toolinfoA.cbSize = sizeof(TTTOOLINFOA);
    toolinfoA.hwnd = NULL;
    toolinfoA.hinst = GetModuleHandleA(NULL);
    toolinfoA.uFlags = 0;
    toolinfoA.uId = 0x1235abce;
    strcpy(bufA, testtip2A);
    toolinfoA.lpszText = bufA;
    toolinfoA.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoA.rect);
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&toolinfoA);
    ok(r, "got %ld\n", r);

    toolinfoA.hwnd = NULL;
    toolinfoA.uId = 0x1235abce;
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
    ok(!strcmp(toolinfoA.lpszText, testtipA), "expected %s, got %s\n", testtipA, toolinfoA.lpszText);

    /* enable TTS_NOPREFIX, original text is retained */
    style = GetWindowLongA(hwnd, GWL_STYLE);
    SetWindowLongA(hwnd, GWL_STYLE, style | TTS_NOPREFIX);

    toolinfoA.hwnd = NULL;
    toolinfoA.uId = 0x1235abce;
    toolinfoA.lpszText = bufA;
    r = SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
    ok(!r, "got %ld\n", r);
544
    ok(!strcmp(toolinfoA.lpszText, testtip2A), "expected %s, got %s\n", testtip2A, toolinfoA.lpszText);
545

546 547 548
    DestroyWindow(hwnd);
}

549 550 551 552 553 554 555
static void test_ttm_gettoolinfo(void)
{
    TTTOOLINFOA ti;
    TTTOOLINFOW tiW;
    HWND hwnd;
    DWORD r;

556 557
    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0, 10, 10, 300, 100, NULL, NULL, NULL, 0);
    ok(hwnd != NULL, "Failed to create tooltip control.\n");
558 559 560 561 562 563 564

    ti.cbSize = TTTOOLINFOA_V2_SIZE;
    ti.hwnd = NULL;
    ti.hinst = GetModuleHandleA(NULL);
    ti.uFlags = 0;
    ti.uId = 0x1234ABCD;
    ti.lpszText = NULL;
565
    ti.lParam = 0x1abe11ed;
566 567 568 569 570 571 572 573
    GetClientRect(hwnd, &ti.rect);
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&ti);
    ok(r, "Adding the tool to the tooltip failed\n");

    ti.cbSize = TTTOOLINFOA_V2_SIZE;
    ti.lParam = 0xaaaaaaaa;
    r = SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&ti);
    ok(r, "Getting tooltip info failed\n");
574 575 576
    ok(0x1abe11ed == ti.lParam ||
       broken(0x1abe11ed != ti.lParam), /* comctl32 < 5.81 */
       "Expected 0x1abe11ed, got %lx\n", ti.lParam);
577 578 579 580 581

    tiW.cbSize = TTTOOLINFOW_V2_SIZE;
    tiW.hwnd = NULL;
    tiW.uId = 0x1234ABCD;
    tiW.lParam = 0xaaaaaaaa;
582
    tiW.lpszText = NULL;
583 584
    r = SendMessageA(hwnd, TTM_GETTOOLINFOW, 0, (LPARAM)&tiW);
    ok(r, "Getting tooltip info failed\n");
585 586 587
    ok(0x1abe11ed == tiW.lParam ||
       broken(0x1abe11ed != tiW.lParam), /* comctl32 < 5.81 */
       "Expected 0x1abe11ed, got %lx\n", tiW.lParam);
588 589 590

    ti.cbSize = TTTOOLINFOA_V2_SIZE;
    ti.uId = 0x1234ABCD;
591
    ti.lParam = 0x55555555;
592
    SendMessageA(hwnd, TTM_SETTOOLINFOA, 0, (LPARAM)&ti);
593 594 595 596 597

    ti.cbSize = TTTOOLINFOA_V2_SIZE;
    ti.lParam = 0xdeadbeef;
    r = SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&ti);
    ok(r, "Getting tooltip info failed\n");
598 599 600
    ok(0x55555555 == ti.lParam ||
       broken(0x55555555 != ti.lParam), /* comctl32 < 5.81 */
       "Expected 0x55555555, got %lx\n", ti.lParam);
601 602

    DestroyWindow(hwnd);
603 604 605 606 607 608 609 610 611 612 613 614 615 616

    /* 1. test size parameter validation rules (ansi messages) */
    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);

    ti.cbSize = TTTOOLINFOA_V1_SIZE - 1;
    ti.hwnd = NULL;
    ti.hinst = GetModuleHandleA(NULL);
    ti.uFlags = 0;
    ti.uId = 0x1234ABCD;
    ti.lpszText = NULL;
    ti.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &ti.rect);
617
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&ti);
618
    ok(r, "Adding the tool to the tooltip failed\n");
619
    r = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
620 621 622 623 624
    expect(1, r);

    ti.cbSize = TTTOOLINFOA_V1_SIZE - 1;
    ti.hwnd = NULL;
    ti.uId = 0x1234ABCD;
625 626
    SendMessageA(hwnd, TTM_DELTOOLA, 0, (LPARAM)&ti);
    r = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
627 628 629 630 631 632 633 634 635 636
    expect(0, r);

    ti.cbSize = TTTOOLINFOA_V2_SIZE - 1;
    ti.hwnd = NULL;
    ti.hinst = GetModuleHandleA(NULL);
    ti.uFlags = 0;
    ti.uId = 0x1234ABCD;
    ti.lpszText = NULL;
    ti.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &ti.rect);
637
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&ti);
638
    ok(r, "Adding the tool to the tooltip failed\n");
639
    r = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
640 641 642 643 644
    expect(1, r);

    ti.cbSize = TTTOOLINFOA_V2_SIZE - 1;
    ti.hwnd = NULL;
    ti.uId = 0x1234ABCD;
645 646
    SendMessageA(hwnd, TTM_DELTOOLA, 0, (LPARAM)&ti);
    r = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
647 648 649 650 651 652 653 654 655 656
    expect(0, r);

    ti.cbSize = TTTOOLINFOA_V2_SIZE + 1;
    ti.hwnd = NULL;
    ti.hinst = GetModuleHandleA(NULL);
    ti.uFlags = 0;
    ti.uId = 0x1234ABCD;
    ti.lpszText = NULL;
    ti.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &ti.rect);
657
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&ti);
658
    ok(r, "Adding the tool to the tooltip failed\n");
659
    r = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
660 661 662 663 664
    expect(1, r);

    ti.cbSize = TTTOOLINFOA_V2_SIZE + 1;
    ti.hwnd = NULL;
    ti.uId = 0x1234ABCD;
665 666
    SendMessageA(hwnd, TTM_DELTOOLA, 0, (LPARAM)&ti);
    r = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
667 668 669 670 671 672 673 674
    expect(0, r);

    DestroyWindow(hwnd);

    /* 2. test size parameter validation rules (w-messages) */
    hwnd = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);
675
    ok(hwnd != NULL, "Failed to create tooltip window.\n");
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

    tiW.cbSize = TTTOOLINFOW_V1_SIZE - 1;
    tiW.hwnd = NULL;
    tiW.hinst = GetModuleHandleA(NULL);
    tiW.uFlags = 0;
    tiW.uId = 0x1234ABCD;
    tiW.lpszText = NULL;
    tiW.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &tiW.rect);
    r = SendMessageW(hwnd, TTM_ADDTOOLW, 0, (LPARAM)&tiW);
    ok(r, "Adding the tool to the tooltip failed\n");
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(1, r);

    tiW.cbSize = TTTOOLINFOW_V1_SIZE - 1;
    tiW.hwnd = NULL;
    tiW.uId = 0x1234ABCD;
    SendMessageW(hwnd, TTM_DELTOOLW, 0, (LPARAM)&tiW);
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(0, r);

    tiW.cbSize = TTTOOLINFOW_V2_SIZE - 1;
    tiW.hwnd = NULL;
    tiW.hinst = GetModuleHandleA(NULL);
    tiW.uFlags = 0;
    tiW.uId = 0x1234ABCD;
    tiW.lpszText = NULL;
    tiW.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &tiW.rect);
    r = SendMessageW(hwnd, TTM_ADDTOOLW, 0, (LPARAM)&tiW);
    ok(r, "Adding the tool to the tooltip failed\n");
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(1, r);

    tiW.cbSize = TTTOOLINFOW_V2_SIZE - 1;
    tiW.hwnd = NULL;
    tiW.uId = 0x1234ABCD;
    SendMessageW(hwnd, TTM_DELTOOLW, 0, (LPARAM)&tiW);
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(0, r);

    tiW.cbSize = TTTOOLINFOW_V2_SIZE + 1;
    tiW.hwnd = NULL;
    tiW.hinst = GetModuleHandleA(NULL);
    tiW.uFlags = 0;
    tiW.uId = 0x1234ABCD;
    tiW.lpszText = NULL;
    tiW.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &tiW.rect);
    r = SendMessageW(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&tiW);
    ok(r, "Adding the tool to the tooltip failed\n");
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(1, r);
    /* looks like TTM_DELTOOLW doesn't work with invalid size */
    tiW.cbSize = TTTOOLINFOW_V2_SIZE + 1;
    tiW.hwnd = NULL;
    tiW.uId = 0x1234ABCD;
    SendMessageW(hwnd, TTM_DELTOOLW, 0, (LPARAM)&tiW);
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(1, r);

    tiW.cbSize = TTTOOLINFOW_V2_SIZE;
    tiW.hwnd = NULL;
    tiW.uId = 0x1234ABCD;
    SendMessageW(hwnd, TTM_DELTOOLW, 0, (LPARAM)&tiW);
    r = SendMessageW(hwnd, TTM_GETTOOLCOUNT, 0, 0);
    expect(0, r);

    DestroyWindow(hwnd);
745 746
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
static void test_longtextA(void)
{
    static const char longtextA[] =
        "According to MSDN, TTM_ENUMTOOLS claims that TOOLINFO's lpszText is maximum "
        "80 chars including null. In fact, the buffer is not null-terminated.";
    HWND hwnd;
    TTTOOLINFOA toolinfoA = { 0 };
    CHAR bufA[256];
    LRESULT r;

    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);
    toolinfoA.cbSize = sizeof(TTTOOLINFOA);
    toolinfoA.hwnd = NULL;
    toolinfoA.hinst = GetModuleHandleA(NULL);
    toolinfoA.uFlags = 0;
    toolinfoA.uId = 0x1234ABCD;
    strcpy(bufA, longtextA);
    toolinfoA.lpszText = bufA;
    toolinfoA.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoA.rect);
    r = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&toolinfoA);
    if (r)
    {
        int textlen;
        memset(bufA, 0, sizeof(bufA));
        toolinfoA.hwnd = NULL;
        toolinfoA.uId = 0xABCD1234;
        toolinfoA.lpszText = bufA;
        SendMessageA(hwnd, TTM_ENUMTOOLSA, 0, (LPARAM)&toolinfoA);
        textlen = lstrlenA(toolinfoA.lpszText);
779
        ok(textlen == 80, "lpszText has %d chars\n", textlen);
780 781 782 783 784 785 786 787 788
        ok(toolinfoA.uId == 0x1234ABCD,
           "uId should be retrieved, got %p\n", (void*)toolinfoA.uId);

        memset(bufA, 0, sizeof(bufA));
        toolinfoA.hwnd = NULL;
        toolinfoA.uId = 0x1234ABCD;
        toolinfoA.lpszText = bufA;
        SendMessageA(hwnd, TTM_GETTOOLINFOA, 0, (LPARAM)&toolinfoA);
        textlen = lstrlenA(toolinfoA.lpszText);
789
        ok(textlen == 80, "lpszText has %d chars\n", textlen);
790 791 792 793 794 795 796

        memset(bufA, 0, sizeof(bufA));
        toolinfoA.hwnd = NULL;
        toolinfoA.uId = 0x1234ABCD;
        toolinfoA.lpszText = bufA;
        SendMessageA(hwnd, TTM_GETTEXTA, 0, (LPARAM)&toolinfoA);
        textlen = lstrlenA(toolinfoA.lpszText);
797
        ok(textlen == 80, "lpszText has %d chars\n", textlen);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
    }

    DestroyWindow(hwnd);
}

static void test_longtextW(void)
{
    static const char longtextA[] =
        "According to MSDN, TTM_ENUMTOOLS claims that TOOLINFO's lpszText is maximum "
        "80 chars including null. Actually, this is not applied for wide version.";
    HWND hwnd;
    TTTOOLINFOW toolinfoW = { 0 };
    WCHAR bufW[256];
    LRESULT r;
    int lenW;

    hwnd = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);
817
    ok(hwnd != NULL, "Failed to create tooltip window.\n");
818 819 820 821 822 823

    toolinfoW.cbSize = TTTOOLINFOW_V2_SIZE;
    toolinfoW.hwnd = NULL;
    toolinfoW.hinst = GetModuleHandleW(NULL);
    toolinfoW.uFlags = 0;
    toolinfoW.uId = 0x1234ABCD;
824
    MultiByteToWideChar(CP_ACP, 0, longtextA, -1, bufW, ARRAY_SIZE(bufW));
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
    lenW = lstrlenW(bufW);
    toolinfoW.lpszText = bufW;
    toolinfoW.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &toolinfoW.rect);
    r = SendMessageW(hwnd, TTM_ADDTOOLW, 0, (LPARAM)&toolinfoW);
    if (r)
    {
        int textlen;
        memset(bufW, 0, sizeof(bufW));
        toolinfoW.hwnd = NULL;
        toolinfoW.uId = 0xABCD1234;
        toolinfoW.lpszText = bufW;
        SendMessageW(hwnd, TTM_ENUMTOOLSW, 0, (LPARAM)&toolinfoW);
        textlen = lstrlenW(toolinfoW.lpszText);
        ok(textlen == lenW, "lpszText has %d chars\n", textlen);
        ok(toolinfoW.uId == 0x1234ABCD,
           "uId should be retrieved, got %p\n", (void*)toolinfoW.uId);

        memset(bufW, 0, sizeof(bufW));
        toolinfoW.hwnd = NULL;
        toolinfoW.uId = 0x1234ABCD;
        toolinfoW.lpszText = bufW;
        SendMessageW(hwnd, TTM_GETTOOLINFOW, 0, (LPARAM)&toolinfoW);
        textlen = lstrlenW(toolinfoW.lpszText);
        ok(textlen == lenW, "lpszText has %d chars\n", textlen);

        memset(bufW, 0, sizeof(bufW));
        toolinfoW.hwnd = NULL;
        toolinfoW.uId = 0x1234ABCD;
        toolinfoW.lpszText = bufW;
        SendMessageW(hwnd, TTM_GETTEXTW, 0, (LPARAM)&toolinfoW);
        textlen = lstrlenW(toolinfoW.lpszText);
        ok(textlen == lenW ||
           broken(textlen == 0 && toolinfoW.lpszText == NULL), /* nt4, kb186177 */
           "lpszText has %d chars\n", textlen);
    }

    DestroyWindow(hwnd);
}

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
static BOOL almost_eq(int a, int b)
{
    return a-5<b && a+5>b;
}

static void test_track(void)
{
    WCHAR textW[] = {'t','e','x','t',0};
    TTTOOLINFOW info = { 0 };
    HWND parent, tt;
    LRESULT res;
    RECT pos;

    parent = CreateWindowExW(0, WC_STATICW, NULL, WS_CAPTION | WS_VISIBLE,
            50, 50, 300, 300, NULL, NULL, NULL, 0);
    ok(parent != NULL, "creation of parent window failed\n");

    ShowWindow(parent, SW_SHOWNORMAL);
    flush_events(100);

    tt = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL, TTS_NOPREFIX | TTS_ALWAYSTIP,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            parent, NULL, GetModuleHandleW(NULL), 0);
    ok(tt != NULL, "creation of tooltip window failed\n");

890
    info.cbSize = TTTOOLINFOW_V1_SIZE;
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
    info.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
    info.hwnd = parent;
    info.hinst = GetModuleHandleW(NULL);
    info.lpszText = textW;
    info.uId = (UINT_PTR)parent;
    GetClientRect(parent, &info.rect);

    res = SendMessageW(tt, TTM_ADDTOOLW, 0, (LPARAM)&info);
    ok(res, "adding the tool to the tooltip failed\n");

    SendMessageW(tt, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELPARAM(1,0));
    SendMessageW(tt, TTM_TRACKACTIVATE, (WPARAM)TRUE, (LPARAM)&info);
    SendMessageW(tt, TTM_TRACKPOSITION, 0, MAKELPARAM(10, 10));

    GetWindowRect(tt, &pos);
    ok(almost_eq(pos.left, 10), "pos.left = %d\n", pos.left);
    ok(almost_eq(pos.top, 10), "pos.top = %d\n", pos.top);

    info.uFlags = TTF_IDISHWND | TTF_ABSOLUTE;
910
    SendMessageW(tt, TTM_SETTOOLINFOW, 0, (LPARAM)&info);
911 912 913 914 915 916 917 918 919 920
    SendMessageW(tt, TTM_TRACKPOSITION, 0, MAKELPARAM(10, 10));

    GetWindowRect(tt, &pos);
    ok(!almost_eq(pos.left, 10), "pos.left = %d\n", pos.left);
    ok(!almost_eq(pos.top, 10), "pos.top = %d\n", pos.top);

    DestroyWindow(tt);
    DestroyWindow(parent);
}

921 922 923 924 925 926 927 928 929 930 931 932 933 934
static LRESULT CALLBACK info_wnd_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg) {

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProcA(hWnd, msg, wParam, lParam);
    }
    return 0;
}

935
static void test_setinfo(BOOL is_v6)
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 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
{
   WNDCLASSA wc;
   LRESULT   lResult;
   HWND parent, parent2, hwndTip, hwndTip2;
   TTTOOLINFOA toolInfo = { 0 };
   TTTOOLINFOA toolInfo2 = { 0 };
   WNDPROC wndProc;

   /* Create a class to use the custom draw wndproc */
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = GetModuleHandleA(NULL);
   wc.hIcon = NULL;
   wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
   wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
   wc.lpszMenuName = NULL;
   wc.lpszClassName = "SetInfoClass";
   wc.lpfnWndProc = info_wnd_proc;
   RegisterClassA(&wc);

   /* Create a main window */
   parent = CreateWindowExA(0, "SetInfoClass", NULL,
                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
                           WS_MAXIMIZEBOX | WS_VISIBLE,
                           50, 50,
                           300, 300,
                           NULL, NULL, NULL, 0);
   ok(parent != NULL, "Creation of main window failed\n");

   parent2 = CreateWindowExA(0, "SetInfoClass", NULL,
                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
                           WS_MAXIMIZEBOX | WS_VISIBLE,
                           50, 50,
                           300, 300,
                           NULL, NULL, NULL, 0);
   ok(parent2 != NULL, "Creation of main window failed\n");

   /* Make it show */
   ShowWindow(parent2, SW_SHOWNORMAL);
   flush_events(100);

   /* Create Tooltip */
   hwndTip = CreateWindowExA(WS_EX_TOPMOST, TOOLTIPS_CLASSA,
                            NULL, TTS_NOPREFIX | TTS_ALWAYSTIP,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            parent, NULL, GetModuleHandleA(NULL), 0);
   ok(hwndTip != NULL, "Creation of tooltip window failed\n");

   hwndTip2 = CreateWindowExA(WS_EX_TOPMOST, TOOLTIPS_CLASSA,
                            NULL, TTS_NOPREFIX | TTS_ALWAYSTIP,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            parent, NULL, GetModuleHandleA(NULL), 0);
   ok(hwndTip2 != NULL, "Creation of tooltip window failed\n");


   /* Make it topmost, as per the MSDN */
   SetWindowPos(hwndTip, HWND_TOPMOST, 0, 0, 0, 0,
         SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

   /* Create a tool */
   toolInfo.cbSize = TTTOOLINFOA_V1_SIZE;
   toolInfo.hwnd = parent;
   toolInfo.hinst = GetModuleHandleA(NULL);
   toolInfo.uFlags = TTF_SUBCLASS;
   toolInfo.uId = 0x1234ABCD;
   toolInfo.lpszText = (LPSTR)"This is a test tooltip";
   toolInfo.lParam = 0xdeadbeef;
   GetClientRect (parent, &toolInfo.rect);
   lResult = SendMessageA(hwndTip, TTM_ADDTOOLA, 0, (LPARAM)&toolInfo);
   ok(lResult, "Adding the tool to the tooltip failed\n");

   toolInfo.cbSize = TTTOOLINFOA_V1_SIZE;
   toolInfo.hwnd = parent2;
   toolInfo.hinst = GetModuleHandleA(NULL);
   toolInfo.uFlags = 0;
   toolInfo.uId = 0x1234ABCE;
   toolInfo.lpszText = (LPSTR)"This is a test tooltip";
   toolInfo.lParam = 0xdeadbeef;
   GetClientRect (parent, &toolInfo.rect);
   lResult = SendMessageA(hwndTip, TTM_ADDTOOLA, 0, (LPARAM)&toolInfo);
   ok(lResult, "Adding the tool to the tooltip failed\n");

   /* Try to Remove Subclass */
   toolInfo2.cbSize = TTTOOLINFOA_V1_SIZE;
   toolInfo2.hwnd = parent;
   toolInfo2.uId = 0x1234ABCD;
   lResult = SendMessageA(hwndTip, TTM_GETTOOLINFOA, 0, (LPARAM)&toolInfo2);
   ok(lResult, "GetToolInfo failed\n");
1027 1028
   ok(toolInfo2.uFlags & TTF_SUBCLASS || broken(is_v6 && !(toolInfo2.uFlags & TTF_SUBCLASS)) /* XP */,
       "uFlags does not have subclass\n");
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
   wndProc = (WNDPROC)GetWindowLongPtrA(parent, GWLP_WNDPROC);
   ok (wndProc != info_wnd_proc, "Window Proc is wrong\n");

   toolInfo2.uFlags &= ~TTF_SUBCLASS;
   SendMessageA(hwndTip, TTM_SETTOOLINFOA, 0, (LPARAM)&toolInfo2);
   lResult = SendMessageA(hwndTip, TTM_GETTOOLINFOA, 0, (LPARAM)&toolInfo2);
   ok(lResult, "GetToolInfo failed\n");
   ok(!(toolInfo2.uFlags & TTF_SUBCLASS), "uFlags has subclass\n");
   wndProc = (WNDPROC)GetWindowLongPtrA(parent, GWLP_WNDPROC);
   ok (wndProc != info_wnd_proc, "Window Proc is wrong\n");

   /* Try to Add Subclass */

   /* Make it topmost, as per the MSDN */
   SetWindowPos(hwndTip2, HWND_TOPMOST, 0, 0, 0, 0,
         SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

   toolInfo2.cbSize = TTTOOLINFOA_V1_SIZE;
   toolInfo2.hwnd = parent2;
   toolInfo2.uId = 0x1234ABCE;
   lResult = SendMessageA(hwndTip, TTM_GETTOOLINFOA, 0, (LPARAM)&toolInfo2);
   ok(lResult, "GetToolInfo failed\n");
   ok(!(toolInfo2.uFlags & TTF_SUBCLASS), "uFlags has subclass\n");
   wndProc = (WNDPROC)GetWindowLongPtrA(parent2, GWLP_WNDPROC);
   ok (wndProc == info_wnd_proc, "Window Proc is wrong\n");

   toolInfo2.uFlags |= TTF_SUBCLASS;
   SendMessageA(hwndTip, TTM_SETTOOLINFOA, 0, (LPARAM)&toolInfo2);
   lResult = SendMessageA(hwndTip, TTM_GETTOOLINFOA, 0, (LPARAM)&toolInfo2);
   ok(lResult, "GetToolInfo failed\n");
   ok(toolInfo2.uFlags & TTF_SUBCLASS, "uFlags does not have subclass\n");
   wndProc = (WNDPROC)GetWindowLongPtrA(parent2, GWLP_WNDPROC);
   ok (wndProc == info_wnd_proc, "Window Proc is wrong\n");

   /* Clean up */
   DestroyWindow(hwndTip);
   DestroyWindow(hwndTip2);
   DestroyWindow(parent);
   DestroyWindow(parent2);
}

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
static void test_margin(void)
{
    RECT r, r1;
    HWND hwnd;
    DWORD ret;

    hwnd = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL, 0,
                           10, 10, 300, 100,
                           NULL, NULL, NULL, 0);
    ok(hwnd != NULL, "failed to create tooltip wnd\n");

    ret = SendMessageA(hwnd, TTM_SETMARGIN, 0, 0);
    ok(!ret, "got %d\n", ret);

    SetRect(&r, -1, -1, 1, 1);
    ret = SendMessageA(hwnd, TTM_SETMARGIN, 0, (LPARAM)&r);
    ok(!ret, "got %d\n", ret);

1088
    SetRectEmpty(&r1);
1089 1090 1091 1092 1093 1094 1095
    ret = SendMessageA(hwnd, TTM_GETMARGIN, 0, (LPARAM)&r1);
    ok(!ret, "got %d\n", ret);
    ok(EqualRect(&r, &r1), "got %s, was %s\n", wine_dbgstr_rect(&r1), wine_dbgstr_rect(&r));

    ret = SendMessageA(hwnd, TTM_SETMARGIN, 0, 0);
    ok(!ret, "got %d\n", ret);

1096
    SetRectEmpty(&r1);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    ret = SendMessageA(hwnd, TTM_GETMARGIN, 0, (LPARAM)&r1);
    ok(!ret, "got %d\n", ret);
    ok(EqualRect(&r, &r1), "got %s, was %s\n", wine_dbgstr_rect(&r1), wine_dbgstr_rect(&r));

    ret = SendMessageA(hwnd, TTM_GETMARGIN, 0, 0);
    ok(!ret, "got %d\n", ret);

    DestroyWindow(hwnd);
}

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
static void test_TTM_ADDTOOL(BOOL is_v6)
{
    static const WCHAR testW[] = {'T','e','s','t',0};
    TTTOOLINFOW tiW;
    TTTOOLINFOA ti;
    int ret, size;
    HWND hwnd, parent;
    UINT max_size;

    parent = CreateWindowExA(0, "Static", NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, NULL, 0);
    ok(parent != NULL, "failed to create parent wnd\n");

    hwnd = CreateWindowExA(WS_EX_TOPMOST, TOOLTIPS_CLASSA, NULL, TTS_NOPREFIX | TTS_ALWAYSTIP,
        0, 0, 100, 100, NULL, NULL, GetModuleHandleA(NULL), 0);
    ok(hwnd != NULL, "Failed to create tooltip window.\n");

    for (size = 0; size <= TTTOOLINFOW_V3_SIZE + 1; size++)
    {
        ti.cbSize = size;
        ti.hwnd = NULL;
        ti.hinst = GetModuleHandleA(NULL);
        ti.uFlags = 0;
        ti.uId = 0x1234abce;
        ti.lpszText = (LPSTR)"Test";
        ti.lParam = 0xdeadbeef;
        GetClientRect(hwnd, &ti.rect);

        ret = SendMessageA(hwnd, TTM_ADDTOOLA, 0, (LPARAM)&ti);
        ok(ret, "Failed to add a tool, size %d.\n", size);

        ret = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
        ok(ret == 1, "Unexpected tool count %d, size %d.\n", ret, size);

        ret = SendMessageA(hwnd, TTM_DELTOOLA, 0, (LPARAM)&ti);
        ok(!ret, "Unexpected ret value %d.\n", ret);

        ret = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
        ok(ret == 0, "Unexpected tool count %d, size %d.\n", ret, size);
    }

    /* W variant checks cbSize. */
    max_size = is_v6 ? TTTOOLINFOW_V3_SIZE : TTTOOLINFOW_V2_SIZE;
    for (size = 0; size <= max_size + 1; size++)
    {
        tiW.cbSize = size;
        tiW.hwnd = NULL;
        tiW.hinst = GetModuleHandleA(NULL);
        tiW.uFlags = 0;
        tiW.uId = 0x1234abce;
        tiW.lpszText = (LPWSTR)testW;
        tiW.lParam = 0xdeadbeef;
        GetClientRect(hwnd, &tiW.rect);

        ret = SendMessageA(hwnd, TTM_ADDTOOLW, 0, (LPARAM)&tiW);
    todo_wine_if(!is_v6 && size > max_size)
        ok(size <= max_size ? ret : !ret, "%d: Unexpected ret value %d, size %d, max size %d.\n", is_v6, ret, size, max_size);
        if (ret)
        {
            ret = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
            ok(ret == 1, "Unexpected tool count %d.\n", ret);

            ret = SendMessageA(hwnd, TTM_DELTOOLA, 0, (LPARAM)&tiW);
            ok(!ret, "Unexpected ret value %d.\n", ret);

            ret = SendMessageA(hwnd, TTM_GETTOOLCOUNT, 0, 0);
            ok(ret == 0, "Unexpected tool count %d.\n", ret);
        }
    }

    DestroyWindow(hwnd);
}

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
static const struct message ttn_show_parent_seq[] =
{
    { WM_NOTIFY, sent|id, 0, 0, TTN_SHOW },
    { 0 }
};

static void test_TTN_SHOW(void)
{
    HWND hwndTip, hwnd;
    TTTOOLINFOA ti;
    RECT rect;
    BOOL ret;

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

    /* Put cursor outside the window */
    GetWindowRect(hwnd, &rect);
    SetCursorPos(rect.right + 200, 0);

    ShowWindow(hwnd, SW_SHOWNORMAL);
    flush_events(100);

    /* Create tooltip */
    hwndTip = CreateWindowExA(WS_EX_TOPMOST, TOOLTIPS_CLASSA, NULL, TTS_ALWAYSTIP, 10, 10, 300, 300,
        hwnd, NULL, NULL, 0);
    ok(hwndTip != NULL, "Failed to create tooltip window.\n");

    SetWindowPos(hwndTip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

    ti.cbSize = sizeof(TTTOOLINFOA);
    ti.hwnd = hwnd;
    ti.hinst = GetModuleHandleA(NULL);
    ti.uFlags = TTF_SUBCLASS;
    ti.uId = 0x1234abcd;
    ti.lpszText = (LPSTR)"This is a test tooltip";
    ti.lParam = 0xdeadbeef;
    GetClientRect(hwnd, &ti.rect);
    ret = SendMessageA(hwndTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
    ok(ret, "Failed to add a tool.\n");

    /* Make tooltip appear quickly */
    SendMessageA(hwndTip, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELPARAM(1, 0));

    flush_sequences(sequences, NUM_MSG_SEQUENCES);

    /* Put cursor inside window, tooltip will appear immediately */
    GetWindowRect(hwnd, &rect);
    SetCursorPos((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2);
    flush_events(200);

    ok_sequence(sequences, PARENT_SEQ_INDEX, ttn_show_parent_seq, "TTN_SHOW parent seq", FALSE);

    DestroyWindow(hwndTip);
    DestroyWindow(hwnd);
}

1236 1237
START_TEST(tooltips)
{
1238 1239 1240
    ULONG_PTR ctx_cookie;
    HANDLE hCtx;

1241 1242
    init_msg_sequences(sequences, NUM_MSG_SEQUENCES);

1243
    LoadLibraryA("comctl32.dll");
1244

1245 1246 1247
    register_parent_wnd_class();

    test_create_tooltip(FALSE);
1248
    test_customdraw();
1249
    test_gettext();
1250
    test_ttm_gettoolinfo();
1251 1252
    test_longtextA();
    test_longtextW();
1253
    test_track();
1254
    test_setinfo(FALSE);
1255
    test_margin();
1256
    test_TTM_ADDTOOL(FALSE);
1257
    test_TTN_SHOW();
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

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

    test_create_tooltip(TRUE);
    test_customdraw();
    test_longtextW();
    test_track();
    test_setinfo(TRUE);
    test_margin();
1268
    test_TTM_ADDTOOL(TRUE);
1269
    test_TTN_SHOW();
1270 1271

    unload_v6_module(ctx_cookie, hCtx);
1272
}