toolbar.c 211 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Toolbar control
 *
4
 * Copyright 1998,1999 Eric Kohl
5
 * Copyright 2000 Eric Kohl for CodeWeavers
6
 * Copyright 2004 Robert Shearman
Alexandre Julliard's avatar
Alexandre Julliard committed
7
 *
8 9 10 11 12 13 14 15 16 17 18 19
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21
 *
22 23 24 25 26 27 28 29 30
 * NOTES
 *
 * This code was audited for completeness against the documented features
 * of Comctl32.dll version 6.0 on Mar. 14, 2004, by Robert Shearman.
 * 
 * Unless otherwise noted, we believe this code to be complete, as per
 * the specification mentioned above.
 * If you discover missing features or bugs please note them below.
 * 
Alexandre Julliard's avatar
Alexandre Julliard committed
31
 * TODO:
32 33 34
 *   - Styles:
 *     - TBSTYLE_REGISTERDROP
 *     - TBSTYLE_EX_DOUBLEBUFFER
35 36 37 38
 *   - Messages:
 *     - TB_GETMETRICS
 *     - TB_GETOBJECT
 *     - TB_INSERTMARKHITTEST
39
 *     - TB_SAVERESTORE
40
 *     - TB_SETMETRICS
41
 *     - WM_WININICHANGE
42
 *   - Notifications:
43 44 45
 *     - NM_CHAR
 *     - TBN_GETOBJECT
 *     - TBN_SAVE
46
 *   - Button wrapping (under construction).
47
 *   - Fix TB_SETROWS and Separators.
48
 *   - iListGap custom draw support.
Alexandre Julliard's avatar
Alexandre Julliard committed
49 50 51 52 53 54 55 56
 *
 * Testing:
 *   - Run tests using Waite Group Windows95 API Bible Volume 2.
 *     The second cdrom contains executables addstr.exe, btncount.exe,
 *     btnstate.exe, butstrsz.exe, chkbtn.exe, chngbmp.exe, customiz.exe,
 *     enablebtn.exe, getbmp.exe, getbtn.exe, getflags.exe, hidebtn.exe,
 *     indetbtn.exe, insbtn.exe, pressbtn.exe, setbtnsz.exe, setcmdid.exe,
 *     setparnt.exe, setrows.exe, toolwnd.exe.
57
 *   - Microsoft's controlspy examples.
58
 *   - Charles Petzold's 'Programming Windows': gadgets.exe
59 60 61 62 63 64 65 66 67 68
 *
 *  Differences between MSDN and actual native control operation:
 *   1. MSDN says: "TBSTYLE_LIST: Creates a flat toolbar with button text
 *                  to the right of the bitmap. Otherwise, this style is
 *                  identical to TBSTYLE_FLAT."
 *      As implemented by both v4.71 and v5.80 of the native COMCTL32.DLL
 *      you can create a TBSTYLE_LIST without TBSTYLE_FLAT and the result
 *      is non-flat non-transparent buttons. Therefore TBSTYLE_LIST does
 *      *not* imply TBSTYLE_FLAT as documented.  (GA 8/2001)
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
69 70
 */

71
#include <stdarg.h>
72 73
#include <string.h>

74
#include "windef.h"
75
#include "winbase.h"
76
#include "winreg.h"
77
#include "wingdi.h"
78
#include "winuser.h"
79
#include "winnls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
80
#include "commctrl.h"
81
#include "comctl32.h"
82
#include "uxtheme.h"
83
#include "vssym32.h"
84
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
85

86
WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
87

Robert Shearman's avatar
Robert Shearman committed
88 89
static HCURSOR hCursorDrag = NULL;

90 91 92 93 94 95
typedef struct
{
    INT iBitmap;
    INT idCommand;
    BYTE  fsState;
    BYTE  fsStyle;
96 97
    BOOL  bHot;
    BOOL  bDropDownPressed;
98
    DWORD_PTR dwData;
Frank Richter's avatar
Frank Richter committed
99
    INT_PTR iString;
100 101
    INT nRow;
    RECT rect;
Robert Shearman's avatar
Robert Shearman committed
102
    INT cx; /* manually set size */
103
} TBUTTON_INFO;
104

105 106 107 108 109 110 111
typedef struct
{
    UINT nButtons;
    HINSTANCE hInst;
    UINT nID;
} TBITMAP_INFO;

112 113 114 115 116 117
typedef struct
{
    HIMAGELIST himl;
    INT id;
} IMLENTRY, *PIMLENTRY;

118 119
typedef struct
{
120
    DWORD    dwStructSize;    /* size of TBBUTTON struct */
121
    RECT     client_rect;
122
    RECT     rcBound;         /* bounding rectangle */
123 124 125 126 127 128 129 130 131 132 133 134
    INT      nButtonHeight;
    INT      nButtonWidth;
    INT      nBitmapHeight;
    INT      nBitmapWidth;
    INT      nIndent;
    INT      nRows;           /* number of button rows */
    INT      nMaxTextRows;    /* maximum number of text rows */
    INT      cxMin;           /* minimum button width */
    INT      cxMax;           /* maximum button width */
    INT      nNumButtons;     /* number of buttons */
    INT      nNumBitmaps;     /* number of bitmaps */
    INT      nNumStrings;     /* number of strings */
135
    INT      nNumBitmapInfos;
Robert Shearman's avatar
Robert Shearman committed
136 137
    INT      nButtonDown;     /* toolbar button being pressed or -1 if none */
    INT      nButtonDrag;     /* toolbar button being dragged or -1 if none */
138 139
    INT      nOldHit;
    INT      nHotItem;        /* index of the "hot" item */
140
    SIZE     szPadding;       /* padding values around button */
141
    INT      iTopMargin;      /* the top margin */
142
    INT      iListGap;        /* default gap between text and image for toolbar with list style */
143
    HFONT    hDefaultFont;
144
    HFONT    hFont;           /* text font */
145
    HTHEME   hTheme;          /* theme */
146
    HIMAGELIST himlInt;       /* image list created internally */
147 148 149 150 151 152
    PIMLENTRY *himlDef;       /* default image list array */
    INT       cimlDef;        /* default image list array count */
    PIMLENTRY *himlHot;       /* hot image list array */
    INT       cimlHot;        /* hot image list array count */
    PIMLENTRY *himlDis;       /* disabled image list array */
    INT       cimlDis;        /* disabled image list array count */
153 154
    HWND     hwndToolTip;     /* handle to tool tip control */
    HWND     hwndNotify;      /* handle to the window that gets notifications */
155
    HWND     hwndSelf;        /* my own handle */
156
    BOOL     bAnchor;         /* anchor highlight enabled */
157
    BOOL     bDoRedraw;       /* Redraw status */
158
    BOOL     bDragOutSent;    /* has TBN_DRAGOUT notification been sent for this drag? */
159
    BOOL     bUnicode;        /* Notifications are ANSI (FALSE) or Unicode (TRUE)? */
160
    BOOL     bCaptured;       /* mouse captured? */
161 162 163
    DWORD      dwStyle;       /* regular toolbar style */
    DWORD      dwExStyle;     /* extended toolbar style */
    DWORD      dwDTFlags;     /* DrawText flags */
164 165

    COLORREF   clrInsertMark;   /* insert mark color */
166 167
    COLORREF   clrBtnHighlight; /* color for Flat Separator */
    COLORREF   clrBtnShadow;    /* color for Flag Separator */
168
    INT      iVersion;
169 170
    LPWSTR   pszTooltipText;    /* temporary store for a string > 80 characters
                                 * for TTN_GETDISPINFOW notification */
171
    TBINSERTMARK  tbim;         /* info on insertion mark */
172 173
    TBUTTON_INFO *buttons;      /* pointer to button array */
    LPWSTR       *strings;      /* pointer to string array */
174
    TBITMAP_INFO *bitmaps;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
} TOOLBAR_INFO, *PTOOLBAR_INFO;


/* used by customization dialog */
typedef struct
{
    PTOOLBAR_INFO tbInfo;
    HWND          tbHwnd;
} CUSTDLG_INFO, *PCUSTDLG_INFO;

typedef struct
{
    TBBUTTON btn;
    BOOL     bVirtual;
    BOOL     bRemovable;
190
    WCHAR    text[64];
191 192
} CUSTOMBUTTON, *PCUSTOMBUTTON;

193 194 195 196 197 198
typedef enum
{
    IMAGE_LIST_DEFAULT,
    IMAGE_LIST_HOT,
    IMAGE_LIST_DISABLED
} IMAGE_LIST_TYPE;
199

Alexandre Julliard's avatar
Alexandre Julliard committed
200 201 202
#define SEPARATOR_WIDTH    8
#define TOP_BORDER         2
#define BOTTOM_BORDER      2
203
#define DDARROW_WIDTH      11
204
#define ARROW_HEIGHT       3
205
#define INSERTMARK_WIDTH   2
Alexandre Julliard's avatar
Alexandre Julliard committed
206

207 208
#define DEFPAD_CX 7
#define DEFPAD_CY 6
209 210 211 212
#define DEFLISTGAP 4

/* vertical padding used in list mode when image is present */
#define LISTPAD_CY 9
213

214
/* how wide to treat the bitmap if it isn't present */
215
#define NONLIST_NOTEXT_OFFSET 2
216

217 218
#define TOOLBAR_NOWHERE (-1)

219 220
/* Used to find undocumented extended styles */
#define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
221
                        TBSTYLE_EX_VERTICAL | \
222
                        TBSTYLE_EX_MIXEDBUTTONS | \
223
                        TBSTYLE_EX_DOUBLEBUFFER | \
224 225
                        TBSTYLE_EX_HIDECLIPPEDBUTTONS)

226 227 228 229
/* all of the CCS_ styles */
#define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
                       CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)

230 231 232 233 234 235
#define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
#define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
#define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
#define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
#define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)

236
static const WCHAR themeClass[] = L"Toolbar";
237

238
static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
239
static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
240 241
static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
242 243
static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
244 245 246
static LRESULT TOOLBAR_LButtonDown(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
static void TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr);
static LRESULT TOOLBAR_AutoSize(TOOLBAR_INFO *infoPtr);
247
static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
248
static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
249
static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
250 251
static LRESULT TOOLBAR_SetButtonInfo(TOOLBAR_INFO *infoPtr, INT Id,
                                     const TBBUTTONINFOW *lptbbi, BOOL isW);
252

253

254
static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
255 256 257
{
    return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
}
258

259 260 261 262 263
static inline BOOL TOOLBAR_HasDropDownArrows(DWORD exStyle)
{
    return (exStyle & TBSTYLE_EX_DRAWDDARROWS) != 0;
}

264 265 266 267 268 269
static inline BOOL button_has_ddarrow(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
{
    return (TOOLBAR_HasDropDownArrows( infoPtr->dwExStyle ) && (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
        (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
}

270
static LPWSTR
271
TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
272 273 274
{
    LPWSTR lpText = NULL;

Robert Shearman's avatar
Robert Shearman committed
275
    /* NOTE: iString == -1 is undocumented */
276
    if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
277 278 279
        lpText = (LPWSTR)btnPtr->iString;
    else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
        lpText = infoPtr->strings[btnPtr->iString];
280

281 282 283
    return lpText;
}

284
static void
285 286
TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
{
287 288 289
    TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%p, stringid=%p (%s)\n", tbb->idCommand,
        tbb->iBitmap, tbb->fsState, tbb->fsStyle, (void *)tbb->dwData, (void *)tbb->iString,
        tbb->iString != -1 ? (fUnicode ? debugstr_w((LPWSTR)tbb->iString) : debugstr_a((LPSTR)tbb->iString)) : "");
290 291 292 293
}

static void
TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
294
{
295 296 297 298 299
    TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%Ix, stringid=%Ix\n",
            btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap), bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
    TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
    TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n", btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
            wine_dbgstr_rect(&bP->rect));
300 301 302 303
}


static void
304
TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
305
{
306 307 308 309 310 311 312 313 314 315 316 317
    INT i;

    if (!TRACE_ON(toolbar))
        return;

    TRACE("toolbar %p at line %d, exStyle=%#lx, buttons=%d, bitmaps=%d, strings=%d, style=%#lx\n",
            iP->hwndSelf, line, iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps, iP->nNumStrings, iP->dwStyle);
    TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
            iP->hwndSelf, line, iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis, (iP->bDoRedraw) ? "TRUE" : "FALSE");

    for (i = 0; i < iP->nNumButtons; ++i)
        TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
318 319
}

320 321 322 323 324
static inline BOOL
TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
{
    return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
}
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
{
    if (!IS_INTRESOURCE( str ) && str != -1)
    {
        if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;

        if (unicode)
            Str_SetPtrW( (WCHAR **)&btn->iString, (WCHAR *)str );
        else
            Str_SetPtrAtoW( (WCHAR **)&btn->iString, (char *)str );
    }
    else
    {
        if (TOOLBAR_ButtonHasString( btn )) Free( (WCHAR *)btn->iString );

        btn->iString  = str;
    }
}

static void set_stringT( TBUTTON_INFO *btn, const WCHAR *str, BOOL unicode )
{
    if (IS_INTRESOURCE( (DWORD_PTR)str ) || (DWORD_PTR)str == -1) return;
    set_string_index( btn, (DWORD_PTR)str, unicode );
}

351 352 353 354 355 356
static void free_string( TBUTTON_INFO *btn )
{
    set_string_index( btn, 0, TRUE );

}

357 358 359 360
/***********************************************************************
* 		TOOLBAR_CheckStyle
*
* This function validates that the styles set are implemented and
361
* issues FIXMEs warning of possible problems. In a perfect world this
362 363 364
* function should be null.
*/
static void
365
TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
366
{
367
    if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
368
	FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
369 370 371
}


372
static INT
373
TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
374
{
375 376
	if(!IsWindow(infoPtr->hwndSelf))
	    return 0;   /* we have just been destroyed */
377

378 379 380 381
    nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
    nmhdr->hwndFrom = infoPtr->hwndSelf;
    nmhdr->code = code;

382
    TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
383
	  (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
384

385
    return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
386 387 388 389 390 391 392 393 394
}

/***********************************************************************
* 		TOOLBAR_GetBitmapIndex
*
* This function returns the bitmap index associated with a button.
* If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
* is issued to retrieve the index.
*/
395
static INT
396
TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
397 398 399
{
    INT ret = btnPtr->iBitmap;

400 401 402
    if (ret == I_IMAGECALLBACK)
    {
        /* issue TBN_GETDISPINFO */
403
        NMTBDISPINFOW nmgd;
404 405 406 407 408

        memset(&nmgd, 0, sizeof(nmgd));
        nmgd.idCommand = btnPtr->idCommand;
        nmgd.lParam = btnPtr->dwData;
        nmgd.dwMask = TBNF_IMAGE;
409
        nmgd.iImage = -1;
410 411
        /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
        TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
412 413 414
        if (nmgd.dwMask & TBNF_DI_SETITEM)
            btnPtr->iBitmap = nmgd.iImage;
        ret = nmgd.iImage;
415
        TRACE("TBN_GETDISPINFO returned bitmap id %d, mask %#lx, nNumBitmaps %d\n",
416
            ret, nmgd.dwMask, infoPtr->nNumBitmaps);
417
    }
418 419 420 421

    if (ret != I_IMAGENONE)
        ret = GETIBITMAP(infoPtr, ret);

422 423 424 425
    return ret;
}


426
static BOOL
427
TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
428
{
429 430 431 432 433 434 435
    HIMAGELIST himl;
    INT id = GETHIMLID(infoPtr, index);
    INT iBitmap = GETIBITMAP(infoPtr, index);

    if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
        iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
        (index == I_IMAGECALLBACK))
436 437 438 439 440
      return TRUE;
    else
      return FALSE;
}

441

442
static inline BOOL
443
TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
444 445 446 447 448 449
{
    HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
    return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
}


450
/***********************************************************************
Robert Shearman's avatar
Robert Shearman committed
451
* 		TOOLBAR_GetImageListForDrawing
452 453
*
* This function validates the bitmap index (including I_IMAGECALLBACK
Robert Shearman's avatar
Robert Shearman committed
454
* functionality) and returns the corresponding image list.
455
*/
Robert Shearman's avatar
Robert Shearman committed
456
static HIMAGELIST
457 458
TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
                                IMAGE_LIST_TYPE imagelist, INT * index)
459
{
460
    HIMAGELIST himl;
461 462

    if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
Robert Shearman's avatar
Robert Shearman committed
463
	if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
464
	WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
Robert Shearman's avatar
Robert Shearman committed
465
	    HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
Robert Shearman's avatar
Robert Shearman committed
466
	return NULL;
467 468
    }

Robert Shearman's avatar
Robert Shearman committed
469 470 471
    if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
	if ((*index == I_IMAGECALLBACK) ||
	    (*index == I_IMAGENONE)) return NULL;
472
	ERR("TBN_GETDISPINFO returned invalid index %d\n",
Robert Shearman's avatar
Robert Shearman committed
473 474
	    *index);
	return NULL;
475
    }
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

    switch(imagelist)
    {
    case IMAGE_LIST_DEFAULT:
        himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
        break;
    case IMAGE_LIST_HOT:
        himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
        break;
    case IMAGE_LIST_DISABLED:
        himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
        break;
    default:
        himl = NULL;
        FIXME("Shouldn't reach here\n");
    }

    if (!himl)
Robert Shearman's avatar
Robert Shearman committed
494
       TRACE("no image list\n");
495

Robert Shearman's avatar
Robert Shearman committed
496
    return himl;
497 498 499
}


Alexandre Julliard's avatar
Alexandre Julliard committed
500
static void
501
TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
502
{
503 504
    RECT myrect;
    COLORREF oldcolor, newcolor;
Alexandre Julliard's avatar
Alexandre Julliard committed
505

506 507 508 509 510
    myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
    myrect.right = myrect.left + 1;
    myrect.top = lpRect->top + 2;
    myrect.bottom = lpRect->bottom - 2;

511
    newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
512
	        comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
513
    oldcolor = SetBkColor (hdc, newcolor);
514
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
515 516 517 518

    myrect.left = myrect.right;
    myrect.right = myrect.left + 1;

519
    newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
520
	        comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
521
    SetBkColor (hdc, newcolor);
522
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
523 524

    SetBkColor (hdc, oldcolor);
Alexandre Julliard's avatar
Alexandre Julliard committed
525 526
}

527 528

/***********************************************************************
529
* 		TOOLBAR_DrawFlatHorizontalSeparator
530
*
531
* This function draws horizontal separator for toolbars having CCS_VERT style.
532 533 534 535 536 537 538 539
* In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
* followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
* are horizontal as opposed to the vertical separators for not dropdown
* type.
*
* FIXME: It is possible that the height of each line is really SM_CYBORDER.
*/
static void
540
TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
541
                             const TOOLBAR_INFO *infoPtr)
542 543 544 545 546 547 548 549 550 551 552
{
    RECT myrect;
    COLORREF oldcolor, newcolor;

    myrect.left = lpRect->left;
    myrect.right = lpRect->right;
    myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
    myrect.bottom = myrect.top + 1;

    InflateRect (&myrect, -2, 0);

553
    TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
554

555
    newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
556
	        comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
557
    oldcolor = SetBkColor (hdc, newcolor);
558
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
559 560 561 562

    myrect.top = myrect.bottom;
    myrect.bottom = myrect.top + 1;

563
    newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
564
	        comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
565
    SetBkColor (hdc, newcolor);
566
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
567 568 569 570 571

    SetBkColor (hdc, oldcolor);
}


572
static void
Robert Shearman's avatar
Robert Shearman committed
573
TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
574 575
{
    INT x, y;
576 577
    HPEN hPen, hOldPen;

Robert Shearman's avatar
Robert Shearman committed
578
    if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
579
    hOldPen = SelectObject ( hdc, hPen );
580
    x = left + 2;
581
    y = top;
582 583 584 585 586
    MoveToEx (hdc, x, y, NULL);
    LineTo (hdc, x+5, y++); x++;
    MoveToEx (hdc, x, y, NULL);
    LineTo (hdc, x+3, y++); x++;
    MoveToEx (hdc, x, y, NULL);
587
    LineTo (hdc, x+1, y);
588 589
    SelectObject( hdc, hOldPen );
    DeleteObject( hPen );
590 591
}

592 593
/*
 * Draw the text string for this button.
594 595 596
 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
 * 	is non-zero, so we can simply check himlDef to see if we have
 *      an image list
597
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
598
static void
599 600
TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
                    const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
601
{
Robert Shearman's avatar
Robert Shearman committed
602
    HDC hdc = tbcd->nmcd.hdc;
603 604
    HFONT  hOldFont = 0;
    COLORREF clrOld = 0;
Robert Shearman's avatar
Robert Shearman committed
605
    COLORREF clrOldBk = 0;
606
    int oldBkMode = 0;
607
    UINT state = tbcd->nmcd.uItemState;
Alexandre Julliard's avatar
Alexandre Julliard committed
608 609

    /* draw text */
610
    if (lpText && infoPtr->nMaxTextRows > 0) {
611 612
        TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
              wine_dbgstr_rect(rcText));
613

614
	hOldFont = SelectObject (hdc, infoPtr->hFont);
615
	if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
616 617 618
	    clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
	}
	else if (state & CDIS_DISABLED) {
619 620 621
	    clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
	    OffsetRect (rcText, 1, 1);
	    DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
622
	    SetTextColor (hdc, comctl32_color.clr3dShadow);
623
	    OffsetRect (rcText, -1, -1);
Alexandre Julliard's avatar
Alexandre Julliard committed
624
	}
625
	else if (state & CDIS_INDETERMINATE) {
626 627
	    clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
	}
628
	else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
629
	    clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
Robert Shearman's avatar
Robert Shearman committed
630
	    clrOldBk = SetBkColor (hdc, tbcd->clrMark);
631
	    oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
Robert Shearman's avatar
Robert Shearman committed
632
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
633
	else {
634
	    clrOld = SetTextColor (hdc, tbcd->clrText);
Alexandre Julliard's avatar
Alexandre Julliard committed
635 636
	}

637
	DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
638
	SetTextColor (hdc, clrOld);
639
	if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
640
	{
Robert Shearman's avatar
Robert Shearman committed
641
	    SetBkColor (hdc, clrOldBk);
642 643
	    SetBkMode (hdc, oldBkMode);
	}
644
	SelectObject (hdc, hOldFont);
Alexandre Julliard's avatar
Alexandre Julliard committed
645 646 647 648
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
649
static void
650
TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
Alexandre Julliard's avatar
Alexandre Julliard committed
651
{
652
    HDC hdc = tbcd->nmcd.hdc;
Robert Shearman's avatar
Robert Shearman committed
653 654 655
    HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
    COLORREF clrTextOld;
    COLORREF clrBkOld;
656 657
    INT cx = lpRect->right - lpRect->left;
    INT cy = lpRect->bottom - lpRect->top;
658 659
    INT cxEdge = GetSystemMetrics(SM_CXEDGE);
    INT cyEdge = GetSystemMetrics(SM_CYEDGE);
Robert Shearman's avatar
Robert Shearman committed
660 661
    clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
    clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
662 663
    PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
            cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
Robert Shearman's avatar
Robert Shearman committed
664 665
    SetBkColor(hdc, clrBkOld);
    SetTextColor(hdc, clrTextOld);
666
    SelectObject (hdc, hbr);
Alexandre Julliard's avatar
Alexandre Julliard committed
667 668 669
}


670
static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
Alexandre Julliard's avatar
Alexandre Julliard committed
671
{
672
    IMAGELISTDRAWPARAMS draw_params = { 0 };
673 674 675
    INT cx, cy;
    HBITMAP hbmMask, hbmImage;
    HDC hdcMask, hdcImage;
676

677 678
    ImageList_GetIconSize(himl, &cx, &cy);

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
    draw_params.cbSize = sizeof(draw_params);
    draw_params.himl = himl;
    draw_params.i = index;
    draw_params.hdcDst = hdc;
    draw_params.x = x;
    draw_params.y = y;
    draw_params.cx = cx;
    draw_params.cy = cy;
    draw_params.rgbBk = CLR_NONE;
    draw_params.rgbFg = CLR_NONE;
    draw_params.fStyle = draw_flags;
    draw_params.fState = ILS_NORMAL;

    /* 32bpp image with alpha channel is converted to grayscale */
    if (imagelist_has_alpha(himl, index))
    {
        draw_params.fState = ILS_SATURATE;
        ImageList_DrawIndirect(&draw_params);
        return;
    }

700 701
    /* Create src image */
    hdcImage = CreateCompatibleDC(hdc);
702
    hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
703
    SelectObject(hdcImage, hbmImage);
704 705 706 707 708 709 710

    draw_params.x = 0;
    draw_params.y = 0;
    draw_params.rgbBk = RGB(0xff, 0xff, 0xff);
    draw_params.rgbFg = RGB(0,0,0);
    draw_params.hdcDst = hdcImage;
    ImageList_DrawIndirect(&draw_params);
711 712 713 714 715 716 717

    /* Create Mask */
    hdcMask = CreateCompatibleDC(0);
    hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
    SelectObject(hdcMask, hbmMask);

    /* Remove the background and all white pixels */
718 719 720
    draw_params.fStyle = ILD_MASK;
    draw_params.hdcDst = hdcMask;
    ImageList_DrawIndirect(&draw_params);
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
    BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);

    /* draw the new mask 'etched' to hdc */
    SetBkColor(hdc, RGB(255, 255, 255));
    SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
    /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
    BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
    SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
    BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);

    /* Cleanup */
    DeleteObject(hbmImage);
    DeleteDC(hdcImage);
    DeleteObject (hbmMask);
    DeleteDC(hdcMask);
Alexandre Julliard's avatar
Alexandre Julliard committed
737 738 739
}


740
static UINT
741
TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr, BOOL captured)
742 743 744 745 746 747 748
{
    UINT retstate = 0;

    retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED  : 0;
    retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
    retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
    retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED   : 0;
749
    retstate |= (btnPtr->bHot & !captured         ) ? CDIS_HOT      : 0;
750
    retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
751
    /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
752 753 754
    return retstate;
}

Robert Shearman's avatar
Robert Shearman committed
755 756
/* draws the image on a toolbar button */
static void
757 758
TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
                  const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
759 760 761 762 763
{
    HIMAGELIST himl = NULL;
    BOOL draw_masked = FALSE;
    INT index;
    INT offset = 0;
764
    UINT draw_flags = ILD_TRANSPARENT;
Robert Shearman's avatar
Robert Shearman committed
765

766
    if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
Robert Shearman's avatar
Robert Shearman committed
767 768 769
    {
        himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
        if (!himl)
770 771 772 773
        {
            himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
            draw_masked = TRUE;
        }
Robert Shearman's avatar
Robert Shearman committed
774
    }
775 776
    else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
      ((tbcd->nmcd.uItemState & CDIS_HOT) 
777
      && ((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme)))
Robert Shearman's avatar
Robert Shearman committed
778 779 780 781 782 783 784 785 786 787
    {
        /* if hot, attempt to draw with hot image list, if fails, 
           use default image list */
        himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
        if (!himl)
            himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
	}
    else
        himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);

788 789 790
    if (!himl)
        return;

791
    if (!(dwItemCDFlag & TBCDRF_NOOFFSET) && 
Robert Shearman's avatar
Robert Shearman committed
792 793 794
        (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
        offset = 1;

795
    if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
Robert Shearman's avatar
Robert Shearman committed
796 797 798 799 800 801 802
        (tbcd->nmcd.uItemState & CDIS_MARKED))
        draw_flags |= ILD_BLEND50;

    TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
      index, himl, left, top, offset);

    if (draw_masked)
803 804
        TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
    else
Robert Shearman's avatar
Robert Shearman committed
805 806 807 808 809
        ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
}

/* draws a blank frame for a toolbar button */
static void
810
TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
811 812
{
    HDC hdc = tbcd->nmcd.hdc;
813
    RECT rc = *rect;
Robert Shearman's avatar
Robert Shearman committed
814 815 816 817 818 819 820 821 822
    /* if the state is disabled or indeterminate then the button
     * cannot have an interactive look like pressed or hot */
    BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
                                 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
    BOOL pressed_look = !non_interactive_state &&
                        ((tbcd->nmcd.uItemState & CDIS_SELECTED) || 
                         (tbcd->nmcd.uItemState & CDIS_CHECKED));

    /* app don't want us to draw any edges */
823
    if (dwItemCDFlag & TBCDRF_NOEDGES)
Robert Shearman's avatar
Robert Shearman committed
824 825
        return;

826
    if (infoPtr->dwStyle & TBSTYLE_FLAT)
Robert Shearman's avatar
Robert Shearman committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    {
        if (pressed_look)
            DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
        else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
            DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
    }
    else
    {
        if (pressed_look)
            DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
        else
            DrawEdge (hdc, &rc, EDGE_RAISED,
              BF_SOFT | BF_RECT | BF_MIDDLE);
    }
}

static void
844
TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
845 846 847
{
    HDC hdc = tbcd->nmcd.hdc;
    int offset = 0;
848 849
    BOOL pressed = bDropDownPressed ||
        (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
Robert Shearman's avatar
Robert Shearman committed
850

851
    if (infoPtr->dwStyle & TBSTYLE_FLAT)
Robert Shearman's avatar
Robert Shearman committed
852
    {
853
        if (pressed)
854
            DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
Robert Shearman's avatar
Robert Shearman committed
855 856 857 858 859 860 861
        else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
                 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
                 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
            DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
    }
    else
    {
862
        if (pressed)
863
            DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
Robert Shearman's avatar
Robert Shearman committed
864 865
        else
            DrawEdge (hdc, rcArrow, EDGE_RAISED,
866
              BF_SOFT | BF_RECT | BF_MIDDLE);
Robert Shearman's avatar
Robert Shearman committed
867 868
    }

869
    if (pressed)
870
        offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
Robert Shearman's avatar
Robert Shearman committed
871 872 873 874 875 876 877 878 879

    if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
    {
        TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
        TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
    }
    else
        TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
}
880

Robert Shearman's avatar
Robert Shearman committed
881
/* draws a complete toolbar button */
Alexandre Julliard's avatar
Alexandre Julliard committed
882
static void
883
TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
Alexandre Julliard's avatar
Alexandre Julliard committed
884
{
885
    DWORD dwStyle = infoPtr->dwStyle;
886
    BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
887
    BOOL drawSepDropDownArrow = hasDropDownArrow && 
Robert Shearman's avatar
Robert Shearman committed
888 889
                                (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
    RECT rc, rcArrow, rcBitmap, rcText;
890 891 892 893
    LPWSTR lpText = NULL;
    NMTBCUSTOMDRAW tbcd;
    DWORD ntfret;
    INT offset;
894
    INT oldBkMode;
895
    DWORD dwItemCustDraw;
896
    DWORD dwItemCDFlag;
897
    HTHEME theme = infoPtr->hTheme;
Alexandre Julliard's avatar
Alexandre Julliard committed
898 899

    rc = btnPtr->rect;
900
    rcArrow = rc;
901

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    /* separator - doesn't send NM_CUSTOMDRAW */
    if (btnPtr->fsStyle & BTNS_SEP) {
        if (theme)
        {
            DrawThemeBackground (theme, hdc, 
                (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0, 
                &rc, NULL);
        }
        else
        /* with the FLAT style, iBitmap is the width and has already */
        /* been taken into consideration in calculating the width    */
        /* so now we need to draw the vertical separator             */
        /* empirical tests show that iBitmap can/will be non-zero    */
        /* when drawing the vertical bar...      */
        if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
917 918 919 920 921
            if (dwStyle & CCS_VERT) {
                RECT rcsep = rc;
                InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
                TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
            }
922 923 924 925 926 927 928 929 930 931
	    else
		TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
	}
	else if (btnPtr->fsStyle != BTNS_SEP) {
	    FIXME("Draw some kind of separator: fsStyle=%x\n",
		  btnPtr->fsStyle);
	}
	return;
    }

932 933
    /* get a pointer to the text */
    lpText = TOOLBAR_GetText(infoPtr, btnPtr);
934 935 936

    if (hasDropDownArrow)
    {
937 938
        int right;

Robert Shearman's avatar
Robert Shearman committed
939
        if (dwStyle & TBSTYLE_FLAT)
940
            right = max(rc.left, rc.right - DDARROW_WIDTH);
Robert Shearman's avatar
Robert Shearman committed
941
        else
942 943 944 945 946
            right = max(rc.left, rc.right - DDARROW_WIDTH - 2);

        if (drawSepDropDownArrow)
           rc.right = right;

Robert Shearman's avatar
Robert Shearman committed
947
        rcArrow.left = right;
948
    }
949

950
    /* copy text & bitmap rects after adjusting for drop-down arrow
951
     * so that text & bitmap is centered in the rectangle not containing
952
     * the arrow */
953 954
    rcText = rc;
    rcBitmap = rc;
955

956
    /* Center the bitmap horizontally and vertically */
957
    if (dwStyle & TBSTYLE_LIST)
958 959 960 961 962 963 964 965 966
    {
        if (lpText &&
            infoPtr->nMaxTextRows > 0 &&
            (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
            (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
            rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
        else
            rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
    }
967
    else
968
        rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
969

970
    rcBitmap.top += infoPtr->szPadding.cy / 2;
971

972
    TRACE("iBitmap=%d, start=(%ld,%ld) w=%d, h=%d\n", btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
Robert Shearman's avatar
Robert Shearman committed
973
      infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
974
    TRACE("Text=%s\n", debugstr_w(lpText));
975
    TRACE("iListGap=%d, padding = { %ld, %ld }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
976

977 978 979
    /* calculate text position */
    if (lpText)
    {
980
        InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
981
        if (dwStyle & TBSTYLE_LIST)
Robert Shearman's avatar
Robert Shearman committed
982
        {
983
            rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
Robert Shearman's avatar
Robert Shearman committed
984 985
        }
        else
986 987 988 989 990 991
        {
            if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
                rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
            else
                rcText.top += infoPtr->szPadding.cy/2 + 2;
        }
992 993
    }

Robert Shearman's avatar
Robert Shearman committed
994 995 996
    /* Initialize fields in all cases, because we use these later
     * NOTE: applications can and do alter these to customize their
     * toolbars */
997 998 999 1000 1001 1002 1003
    ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
    tbcd.clrText = comctl32_color.clrBtnText;
    tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
    tbcd.clrBtnFace = comctl32_color.clrBtnFace;
    tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
    tbcd.clrMark = comctl32_color.clrHighlight;
    tbcd.clrHighlightHotTrack = 0;
1004 1005
    tbcd.nStringBkMode = TRANSPARENT;
    tbcd.nHLStringBkMode = OPAQUE;
1006 1007 1008 1009
    tbcd.rcText.left = 0;
    tbcd.rcText.top = 0;
    tbcd.rcText.right = rcText.right - rc.left;
    tbcd.rcText.bottom = rcText.bottom - rc.top;
1010
    tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr, infoPtr->bCaptured);
1011
    tbcd.nmcd.hdc = hdc;
1012
    tbcd.nmcd.rc = btnPtr->rect;
1013
    tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
1014

Robert Shearman's avatar
Robert Shearman committed
1015
    /* FIXME: what are these used for? */
1016 1017 1018
    tbcd.hbrLines = 0;
    tbcd.hpenLines = 0;

1019
    /* Issue Item Prepaint notify */
1020
    dwItemCustDraw = 0;
1021 1022
    dwItemCDFlag = 0;
    if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1023 1024 1025 1026
    {
	tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
	tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
	tbcd.nmcd.lItemlParam = btnPtr->dwData;
1027
	ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
Robert Shearman's avatar
Robert Shearman committed
1028 1029
        /* reset these fields so the user can't alter the behaviour like native */
        tbcd.nmcd.hdc = hdc;
1030
        tbcd.nmcd.rc = btnPtr->rect;
Robert Shearman's avatar
Robert Shearman committed
1031

1032
	dwItemCustDraw = ntfret & 0xffff;
1033
	dwItemCDFlag = ntfret & 0xffff0000;
1034
	if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1035 1036 1037 1038 1039 1040
	    return;
	/* save the only part of the rect that the user can change */
	rcText.right = tbcd.rcText.right + rc.left;
	rcText.bottom = tbcd.rcText.bottom + rc.top;
    }

1041
    if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1042 1043 1044
        (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
        OffsetRect(&rcText, 1, 1);

1045
    if (!theme && !(tbcd.nmcd.uItemState & CDIS_HOT) &&
Robert Shearman's avatar
Robert Shearman committed
1046 1047 1048
        ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
        TOOLBAR_DrawPattern (&rc, &tbcd);

1049
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || theme) && (tbcd.nmcd.uItemState & CDIS_HOT))
1050
    {
1051
        if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
Robert Shearman's avatar
Robert Shearman committed
1052
        {
1053 1054 1055
            COLORREF oldclr;

            oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1056
            ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1057
            if (hasDropDownArrow)
1058
                ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1059 1060 1061
            SetBkColor(hdc, oldclr);
        }
    }
1062

1063 1064
    if (theme)
    {
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
        if (!(dwItemCDFlag & TBCDRF_NOBACKGROUND))
        {
            int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
            int stateId = TS_NORMAL;

            if (tbcd.nmcd.uItemState & CDIS_DISABLED)
                stateId = TS_DISABLED;
            else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
                stateId = TS_PRESSED;
            else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
                stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_CHECKED;
            else if ((tbcd.nmcd.uItemState & CDIS_HOT)
                     || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
                stateId = TS_HOT;

            DrawThemeBackground(theme, hdc, partId, stateId, &rc, NULL);
        }
1082 1083
    }
    else
1084
        TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1085

Robert Shearman's avatar
Robert Shearman committed
1086
    if (drawSepDropDownArrow)
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    {
        if (theme)
        {
            int stateId = TS_NORMAL;
            
            if (tbcd.nmcd.uItemState & CDIS_DISABLED)
                stateId = TS_DISABLED;
            else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
                stateId = TS_PRESSED;
            else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1097
                stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_CHECKED;
1098 1099 1100 1101 1102 1103 1104
            else if (tbcd.nmcd.uItemState & CDIS_HOT)
                stateId = TS_HOT;
                
            DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
            DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
        }
        else
1105
            TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1106
    }
1107

1108
    oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
Robert Shearman's avatar
Robert Shearman committed
1109
    if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1110
        TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1111
    SetBkMode (hdc, oldBkMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
1112

1113
    TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
Alexandre Julliard's avatar
Alexandre Julliard committed
1114

Robert Shearman's avatar
Robert Shearman committed
1115
    if (hasDropDownArrow && !drawSepDropDownArrow)
1116
    {
Robert Shearman's avatar
Robert Shearman committed
1117 1118 1119 1120 1121 1122 1123
        if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
        {
            TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
            TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
        }
        else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
        {
1124
            offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
Robert Shearman's avatar
Robert Shearman committed
1125 1126 1127 1128
            TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
        }
        else
            TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1129
    }
1130

1131
    if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1132
    {
1133 1134
        tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
        TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1135
    }
1136

Alexandre Julliard's avatar
Alexandre Julliard committed
1137 1138 1139 1140
}


static void
1141
TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
Alexandre Julliard's avatar
Alexandre Julliard committed
1142 1143
{
    TBUTTON_INFO *btnPtr;
1144
    INT i;
1145
    RECT rcTemp, rcClient;
1146 1147
    NMTBCUSTOMDRAW tbcd;
    DWORD ntfret;
1148
    DWORD dwBaseCustDraw;
Alexandre Julliard's avatar
Alexandre Julliard committed
1149

Robert Shearman's avatar
Robert Shearman committed
1150 1151 1152 1153
    /* the app has told us not to redraw the toolbar */
    if (!infoPtr->bDoRedraw)
        return;

1154 1155
    /* if imagelist belongs to the app, it can be changed
       by the app after setting it */
1156 1157 1158 1159 1160 1161
    if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
    {
        infoPtr->nNumBitmaps = 0;
        for (i = 0; i < infoPtr->cimlDef; i++)
            infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
    }
1162 1163 1164

    TOOLBAR_DumpToolbar (infoPtr, __LINE__);

1165 1166 1167
    /* change the imagelist icon size if we manage the list and it is necessary */
    TOOLBAR_CheckImageListIconSize(infoPtr);

1168 1169 1170 1171 1172
    /* Send initial notify */
    ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
    tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
    tbcd.nmcd.hdc = hdc;
    tbcd.nmcd.rc = ps->rcPaint;
1173
    ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1174
    dwBaseCustDraw = ntfret & 0xffff;
1175

1176
    GetClientRect(infoPtr->hwndSelf, &rcClient);
1177

1178
    /* redraw necessary buttons */
Alexandre Julliard's avatar
Alexandre Julliard committed
1179
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1180
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1181
    {
1182
        BOOL bDraw;
1183 1184
        if (!RectVisible(hdc, &btnPtr->rect))
            continue;
1185 1186 1187 1188 1189 1190 1191 1192
        if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
        {
            IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
            bDraw = EqualRect(&rcTemp, &btnPtr->rect);
        }
        else
            bDraw = TRUE;
        bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
Robert Shearman's avatar
Robert Shearman committed
1193
        bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1194
        if (bDraw)
1195
            TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1196
    }
1197

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    /* draw insert mark if required */
    if (infoPtr->tbim.iButton != -1)
    {
        RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
        RECT rcInsertMark;
        rcInsertMark.top = rcButton.top;
        rcInsertMark.bottom = rcButton.bottom;
        if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
            rcInsertMark.left = rcInsertMark.right = rcButton.right;
        else
            rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
        COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
    }

1212
    if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1213 1214 1215 1216 1217
    {
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
	tbcd.nmcd.hdc = hdc;
	tbcd.nmcd.rc = ps->rcPaint;
1218
	TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1219
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1220 1221
}

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
/***********************************************************************
* 		TOOLBAR_MeasureString
*
* This function gets the width and height of a string in pixels. This
* is done first by using GetTextExtentPoint to get the basic width
* and height. The DrawText is called with DT_CALCRECT to get the exact
* width. The reason is because the text may have more than one "&" (or
* prefix characters as M$ likes to call them). The prefix character
* indicates where the underline goes, except for the string "&&" which
* is reduced to a single "&". GetTextExtentPoint does not process these
1232
* only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1233
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1234
static void
1235
TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1236
		      HDC hdc, LPSIZE lpSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
1237
{
1238
    RECT myrect;
1239

Alexandre Julliard's avatar
Alexandre Julliard committed
1240 1241
    lpSize->cx = 0;
    lpSize->cy = 0;
1242

1243 1244
    if (infoPtr->nMaxTextRows > 0 &&
        !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1245 1246
        (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
        (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1247
    {
1248
        LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1249

1250 1251
	if(lpText != NULL) {
	    /* first get size of all the text */
1252
	    GetTextExtentPoint32W (hdc, lpText, lstrlenW (lpText), lpSize);
1253 1254

	    /* feed above size into the rectangle for DrawText */
1255
            SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1256 1257 1258

	    /* Use DrawText to get true size as drawn (less pesky "&") */
	    DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1259
	    	   DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1260 1261
				  DT_NOPREFIX : 0));

1262 1263 1264 1265
	    /* feed back to caller  */
	    lpSize->cx = myrect.right;
	    lpSize->cy = myrect.bottom;
	}
1266
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1267

1268
    TRACE("string size %ld x %ld.\n", lpSize->cx, lpSize->cy);
Alexandre Julliard's avatar
Alexandre Julliard committed
1269 1270
}

1271 1272 1273 1274 1275 1276
/***********************************************************************
* 		TOOLBAR_CalcStrings
*
* This function walks through each string and measures it and returns
* the largest height and width to caller.
*/
1277
static void
1278
TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1279 1280 1281 1282
{
    TBUTTON_INFO *btnPtr;
    INT i;
    SIZE sz;
1283 1284
    HDC hdc;
    HFONT hOldFont;
1285 1286 1287 1288

    lpSize->cx = 0;
    lpSize->cy = 0;

1289
    if (infoPtr->nMaxTextRows == 0)
Huw Davies's avatar
Huw Davies committed
1290 1291
        return;

1292
    hdc = GetDC (infoPtr->hwndSelf);
1293 1294
    hOldFont = SelectObject (hdc, infoPtr->hFont);

1295
    if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1296 1297 1298 1299 1300 1301 1302
    {
        TEXTMETRICW tm;

        GetTextMetricsW(hdc, &tm);
        lpSize->cy = tm.tmHeight;
    }

1303 1304
    btnPtr = infoPtr->buttons;
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1305
        if(TOOLBAR_GetText(infoPtr, btnPtr))
1306
        {
1307
            TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1308 1309 1310 1311 1312
            if (sz.cx > lpSize->cx)
                lpSize->cx = sz.cx;
            if (sz.cy > lpSize->cy)
                lpSize->cy = sz.cy;
        }
1313 1314
    }

1315
    SelectObject (hdc, hOldFont);
1316
    ReleaseDC (infoPtr->hwndSelf, hdc);
1317

1318
    TRACE("max string size %ld x %ld\n", lpSize->cx, lpSize->cy);
1319 1320
}

1321 1322 1323
/***********************************************************************
* 		TOOLBAR_WrapToolbar
*
Francois Gouget's avatar
Francois Gouget committed
1324
* This function walks through the buttons and separators in the
1325 1326 1327 1328 1329
* toolbar, and sets the TBSTATE_WRAP flag only on those items where
* wrapping should occur based on the width of the toolbar window.
* It does *not* calculate button placement itself.  That task
* takes place in TOOLBAR_CalcToolbar. If the program wants to manage
* the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1330
* flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1331
*
1332
* Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1333 1334
* vertical toolbar lists.
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1335

Alexandre Julliard's avatar
Alexandre Julliard committed
1336
static void
1337
TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
1338
{
1339
    TBUTTON_INFO *btnPtr;
1340
    INT x, cx, i, j, width;
1341
    BOOL bButtonWrap;
1342

1343
    /* 	When the toolbar window style is not TBSTYLE_WRAPABLE,	*/
1344 1345
    /*	no layout is necessary. Applications may use this style */
    /*	to perform their own layout on the toolbar. 		*/
1346
    if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1347
	!(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) )  return;
1348 1349 1350

    btnPtr = infoPtr->buttons;
    x  = infoPtr->nIndent;
1351
    width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1352

1353 1354
    bButtonWrap = FALSE;

1355 1356
    TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
	  infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1357 1358
	  infoPtr->nIndent);

1359 1360 1361
    for (i = 0; i < infoPtr->nNumButtons; i++ )
    {
	btnPtr[i].fsState &= ~TBSTATE_WRAP;
1362

1363 1364 1365
	if (btnPtr[i].fsState & TBSTATE_HIDDEN)
	    continue;

1366 1367
        if (btnPtr[i].cx > 0)
            cx = btnPtr[i].cx;
1368
        /* horizontal separators are treated as buttons for width    */
1369
	else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1370
            !(infoPtr->dwStyle & CCS_VERT))
1371
            cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1372
	else
1373
	    cx = infoPtr->nButtonWidth;
1374

1375 1376 1377
        if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
            cx += DDARROW_WIDTH;

1378
	/* Two or more adjacent separators form a separator group.   */
1379 1380 1381
	/* The first separator in a group should be wrapped to the   */
	/* next row if the previous wrapping is on a button.	     */
	if( bButtonWrap &&
1382
		(btnPtr[i].fsStyle & BTNS_SEP) &&
1383
		(i + 1 < infoPtr->nNumButtons ) &&
1384
		(btnPtr[i + 1].fsStyle & BTNS_SEP) )
1385
	{
1386
	    TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1387 1388 1389 1390 1391 1392 1393 1394
	    btnPtr[i].fsState |= TBSTATE_WRAP;
	    x = infoPtr->nIndent;
	    i++;
	    bButtonWrap = FALSE;
	    continue;
	}

	/* The layout makes sure the bitmap is visible, but not the button. */
1395 1396
	/* Test added to also wrap after a button that starts a row but     */
	/* is bigger than the area.  - GA  8/01                             */
1397 1398
        if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
            ((x == infoPtr->nIndent) && (cx > width)))
1399 1400 1401
	{
	    BOOL bFound = FALSE;

1402
	    /* 	If the current button is a separator and not hidden,  */
1403 1404
	    /*	go to the next until it reaches a non separator.      */
	    /*	Wrap the last separator if it is before a button.     */
1405 1406
	    while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
		      !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1407
		     (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1408 1409 1410 1411 1412
			i < infoPtr->nNumButtons )
	    {
		i++;
		bFound = TRUE;
	    }
1413

1414 1415 1416
	    if( bFound && i < infoPtr->nNumButtons )
	    {
		i--;
1417
		TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1418
		      i, btnPtr[i].fsStyle, x, cx);
1419 1420 1421 1422 1423 1424 1425 1426
		btnPtr[i].fsState |= TBSTATE_WRAP;
		x = infoPtr->nIndent;
		bButtonWrap = FALSE;
		continue;
	    }
	    else if ( i >= infoPtr->nNumButtons)
		break;

1427
	    /* 	If the current button is not a separator, find the last  */
1428 1429 1430
	    /*	separator and wrap it.   				 */
	    for ( j = i - 1; j >= 0  &&  !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
	    {
1431
		if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1432 1433
			!(btnPtr[j].fsState & TBSTATE_HIDDEN))
		{
1434 1435 1436
		    bFound = TRUE;
		    i = j;
		    TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1437
			  i, btnPtr[i].fsStyle, x, cx);
1438 1439
		    x = infoPtr->nIndent;
		    btnPtr[j].fsState |= TBSTATE_WRAP;
1440
		    bButtonWrap = FALSE;
1441 1442 1443 1444 1445 1446 1447 1448
		    break;
		}
	    }

	    /* 	If no separator available for wrapping, wrap one of 	*/
	    /*  non-hidden previous button.  			     	*/
	    if (!bFound)
	    {
1449
		for ( j = i - 1;
1450 1451
			j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
		{
1452
		    if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1453 1454
			continue;

1455 1456 1457
		    bFound = TRUE;
		    i = j;
		    TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1458
			  i, btnPtr[i].fsStyle, x, cx);
1459 1460 1461 1462 1463 1464 1465 1466
		    x = infoPtr->nIndent;
		    btnPtr[j].fsState |= TBSTATE_WRAP;
		    bButtonWrap = TRUE;
		    break;
		}
	    }

	    /* If all above failed, wrap the current button. */
1467
	    if (!bFound)
1468
	    {
1469 1470
		TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
		      i, btnPtr[i].fsStyle, x, cx);
1471 1472
		btnPtr[i].fsState |= TBSTATE_WRAP;
		x = infoPtr->nIndent;
1473
		if (btnPtr[i].fsStyle & BTNS_SEP )
1474 1475 1476
		    bButtonWrap = FALSE;
		else
		    bButtonWrap = TRUE;
1477
	    }
1478
	}
1479
	else {
1480
	    TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1481
		  i, btnPtr[i].fsStyle, x, cx);
1482
	    x += cx;
1483
	}
1484 1485
    }
}
1486 1487


1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
/***********************************************************************
* 		TOOLBAR_MeasureButton
*
* Calculates the width and height required for a button. Used in
* TOOLBAR_CalcToolbar to set the all-button width and height and also for
* the width of buttons that are autosized.
*
* Note that it would have been rather elegant to use one piece of code for
* both the laying out of the toolbar and for controlling where button parts
* are drawn, but the native control has inconsistencies between the two that
* prevent this from being effectively. These inconsistencies can be seen as
* artefacts where parts of the button appear outside of the bounding button
* rectangle.
*
* There are several cases for the calculation of the button dimensions and
* button part positioning:
*
* List
* ====
*
* With Bitmap:
*
* +--------------------------------------------------------+ ^
* |                    ^                     ^             | |
1512
* |                    | pad.cy / 2          | centered    | |
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
* | pad.cx/2 + cxedge +--------------+     +------------+  | | DEFPAD_CY +
* |<----------------->| nBitmapWidth |     | Text       |  | | max(nBitmapHeight, szText.cy)
* |                   |<------------>|     |            |  | |
* |                   +--------------+     +------------+  | |
* |<-------------------------------------->|               | |
* |  cxedge + iListGap + nBitmapWidth + 2  |<----------->  | |
* |                                           szText.cx    | |
* +--------------------------------------------------------+ -
* <-------------------------------------------------------->
*  2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
*
* Without Bitmap (I_IMAGENONE):
*
* +-----------------------------------+ ^
* |                     ^             | |
1528
* |                     | centered    | | LISTPAD_CY +
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
* |                   +------------+  | | szText.cy
* |                   | Text       |  | |
* |                   |            |  | |
* |                   +------------+  | |
* |<----------------->|               | |
* |      cxedge       |<----------->  | |
* |                      szText.cx    | |
* +-----------------------------------+ -
* <----------------------------------->
*          szText.cx + pad.cx
*
* Without text:
*
* +--------------------------------------+ ^
* |                       ^              | |
* |                       | padding.cy/2 | | DEFPAD_CY +
* |                     +------------+   | | nBitmapHeight
* |                     | Bitmap     |   | |
* |                     |            |   | |
* |                     +------------+   | |
* |<------------------->|                | |
* | cxedge + iListGap/2 |<----------->   | |
* |                       nBitmapWidth   | |
* +--------------------------------------+ -
* <-------------------------------------->
*     2*cxedge + nBitmapWidth + iListGap
*
* Non-List
* ========
*
* With bitmap:
*
* +-----------------------------------+ ^
* |                     ^             | |
* |                     | pad.cy / 2  | | nBitmapHeight +
* |                     -             | | szText.cy +
* |                   +------------+  | | DEFPAD_CY + 1
1566
* |    centered       |   Bitmap   |  | |
1567 1568 1569 1570 1571
* |<----------------->|            |  | |
* |                   +------------+  | |
* |                         ^         | |
* |                       1 |         | |
* |                         -         | |
1572
* |     centered    +---------------+ | |
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
* |<--------------->|      Text     | | |
* |                 +---------------+ | |
* +-----------------------------------+ -
* <----------------------------------->
* pad.cx + max(nBitmapWidth, szText.cx)
*
* Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
*
* +---------------------------------------+ ^
* |                     ^                 | |
* |                     | 2 + pad.cy / 2  | |
* |                     -                 | | szText.cy +
1585
* |    centered     +-----------------+   | | pad.cy + 2
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
* |<--------------->|   Text          |   | |
* |                 +-----------------+   | |
* |                                       | |
* +---------------------------------------+ -
* <--------------------------------------->
*          2*cxedge + pad.cx + szText.cx
*
* Without text:
*   As for with bitmaps, but with szText.cx zero.
*/
1596 1597
static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
                                         BOOL bHasBitmap, BOOL bValidImageList)
1598 1599 1600 1601 1602
{
    SIZE sizeButton;
    if (infoPtr->dwStyle & TBSTYLE_LIST)
    {
        /* set button height from bitmap / text height... */
1603
        sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
            sizeString.cy);

        /* ... add on the necessary padding */
        if (bValidImageList)
        {
            if (bHasBitmap)
                sizeButton.cy += DEFPAD_CY;
            else
                sizeButton.cy += LISTPAD_CY;
        }
        else
            sizeButton.cy += infoPtr->szPadding.cy;

        /* calculate button width */
1618 1619 1620 1621 1622
        sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
            infoPtr->nBitmapWidth + infoPtr->iListGap;
        if (sizeString.cx > 0)
            sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;

1623 1624 1625 1626 1627
    }
    else
    {
        if (bHasBitmap)
        {
1628
            sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1629 1630
            if (sizeString.cy > 0)
                sizeButton.cy += 1 + sizeString.cy;
1631 1632 1633 1634 1635 1636 1637
            sizeButton.cx = infoPtr->szPadding.cx +
                max(sizeString.cx, infoPtr->nBitmapWidth);
        }
        else
        {
            sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
                NONLIST_NOTEXT_OFFSET;
1638 1639
            sizeButton.cx = infoPtr->szPadding.cx +
                max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1640 1641 1642 1643 1644 1645
        }
    }
    return sizeButton;
}


1646 1647 1648
/***********************************************************************
* 		TOOLBAR_CalcToolbar
*
1649 1650 1651
* This function calculates button and separator placement. It first
* calculates the button sizes, gets the toolbar window width and then
* calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1652
* on. It assigns a new location to each item and sends this location to
1653 1654 1655
* the tooltip window if appropriate. Finally, it updates the rcBound
* rect and calculates the new required toolbar window height.
*/
1656
static void
1657
TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1658
{
1659 1660
    SIZE  sizeString, sizeButton;
    BOOL validImageList = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1661

1662
    TOOLBAR_CalcStrings (infoPtr, &sizeString);
Alexandre Julliard's avatar
Alexandre Julliard committed
1663

1664 1665
    TOOLBAR_DumpToolbar (infoPtr, __LINE__);

1666 1667
    if (TOOLBAR_IsValidImageList(infoPtr, 0))
        validImageList = TRUE;
1668
    sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1669 1670
    infoPtr->nButtonWidth = sizeButton.cx;
    infoPtr->nButtonHeight = sizeButton.cy;
1671
    infoPtr->iTopMargin = default_top_margin(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1672

1673 1674
    if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
        infoPtr->nButtonWidth = infoPtr->cxMin;
1675
    if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1676 1677
        infoPtr->nButtonWidth = infoPtr->cxMax;

1678
    TOOLBAR_LayoutToolbar(infoPtr);
1679
}
1680

1681
static void
1682
TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1683 1684 1685 1686 1687 1688 1689 1690
{
    TBUTTON_INFO *btnPtr;
    SIZE sizeButton;
    INT i, nRows, nSepRows;
    INT x, y, cx, cy;
    BOOL bWrap;
    BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);

1691
    TOOLBAR_WrapToolbar(infoPtr);
1692

1693
    x  = infoPtr->nIndent;
1694
    y  = infoPtr->iTopMargin;
Alexandre Julliard's avatar
Alexandre Julliard committed
1695
    cx = infoPtr->nButtonWidth;
1696
    cy = infoPtr->nButtonHeight;
1697

1698
    nRows = nSepRows = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1699 1700 1701 1702 1703

    infoPtr->rcBound.top = y;
    infoPtr->rcBound.left = x;
    infoPtr->rcBound.bottom = y + cy;
    infoPtr->rcBound.right = x;
Alexandre Julliard's avatar
Alexandre Julliard committed
1704 1705

    btnPtr = infoPtr->buttons;
1706

1707 1708
    TRACE("cy=%d\n", cy);

1709 1710 1711 1712 1713
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
    {
	bWrap = FALSE;
	if (btnPtr->fsState & TBSTATE_HIDDEN)
	{
1714
	    SetRectEmpty (&btnPtr->rect);
1715
	    TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1716
	    continue;
1717 1718
	}

1719
	cy = infoPtr->nButtonHeight;
1720

1721
	if (btnPtr->fsStyle & BTNS_SEP) {
1722
	    if (infoPtr->dwStyle & CCS_VERT) {
1723
                cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1724
                cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1725 1726
	    }
	    else
1727 1728
                cx = (btnPtr->cx > 0) ? btnPtr->cx :
                    (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1729
	}
1730 1731
	else
	{
Robert Shearman's avatar
Robert Shearman committed
1732 1733
            if (btnPtr->cx)
              cx = btnPtr->cx;
1734
            else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1735 1736
            {
              SIZE sz;
1737 1738 1739
	      HDC hdc;
	      HFONT hOldFont;

1740
	      hdc = GetDC (infoPtr->hwndSelf);
1741 1742 1743 1744 1745
	      hOldFont = SelectObject (hdc, infoPtr->hFont);

              TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);

	      SelectObject (hdc, hOldFont);
1746
	      ReleaseDC (infoPtr->hwndSelf, hdc);
1747

1748 1749 1750 1751
              sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
                  TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
                  validImageList);
              cx = sizeButton.cx;
1752 1753 1754
            }
            else
	      cx = infoPtr->nButtonWidth;
1755

Robert Shearman's avatar
Robert Shearman committed
1756 1757
            /* if size has been set manually then don't add on extra space
             * for the drop down arrow */
1758 1759
            if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
              cx += DDARROW_WIDTH;
1760
	}
1761
	if (btnPtr->fsState & TBSTATE_WRAP)
1762 1763
		    bWrap = TRUE;

1764
	SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
Alexandre Julliard's avatar
Alexandre Julliard committed
1765

Alexandre Julliard's avatar
Alexandre Julliard committed
1766 1767 1768 1769 1770 1771
	if (infoPtr->rcBound.left > x)
	    infoPtr->rcBound.left = x;
	if (infoPtr->rcBound.right < x + cx)
	    infoPtr->rcBound.right = x + cx;
	if (infoPtr->rcBound.bottom < y + cy)
	    infoPtr->rcBound.bottom = y + cy;
Alexandre Julliard's avatar
Alexandre Julliard committed
1772

1773
        TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1774

1775 1776 1777
	/* btnPtr->nRow is zero based. The space between the rows is 	*/
	/* also considered as a row. 					*/
	btnPtr->nRow = nRows + nSepRows;
1778

1779 1780 1781
	TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
	      i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
	      x, y, x+cx, y+cy);
1782

1783 1784
	if( bWrap )
	{
1785
	    if ( !(btnPtr->fsStyle & BTNS_SEP) )
1786
	        y += cy;
1787 1788
	    else
	    {
1789
               if ( !(infoPtr->dwStyle & CCS_VERT))
1790 1791
                    y += cy + ( (btnPtr->cx > 0 ) ?
                                btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1792 1793
		else
		    y += cy;
1794

1795
		/* nSepRows is used to calculate the extra height following  */
1796 1797 1798 1799
		/* the last row.					     */
		nSepRows++;
	    }
	    x = infoPtr->nIndent;
1800 1801 1802 1803

	    /* Increment row number unless this is the last button    */
	    /* and it has Wrap set.                                   */
	    if (i != infoPtr->nNumButtons-1)
Alexandre Julliard's avatar
Alexandre Julliard committed
1804 1805
		nRows++;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1806
	else
Alexandre Julliard's avatar
Alexandre Julliard committed
1807
	    x += cx;
Alexandre Julliard's avatar
Alexandre Julliard committed
1808
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1809

1810 1811 1812
    /* infoPtr->nRows is the number of rows on the toolbar */
    infoPtr->nRows = nRows + nSepRows + 1;

1813
    TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
Alexandre Julliard's avatar
Alexandre Julliard committed
1814 1815 1816
}


1817
static INT
1818
TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
Alexandre Julliard's avatar
Alexandre Julliard committed
1819 1820
{
    TBUTTON_INFO *btnPtr;
1821
    INT i;
1822

1823 1824 1825
    if (button)
        *button = FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1826
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1827
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1828 1829 1830
	if (btnPtr->fsState & TBSTATE_HIDDEN)
	    continue;

1831
	if (btnPtr->fsStyle & BTNS_SEP) {
1832
	    if (PtInRect (&btnPtr->rect, *lpPt)) {
1833
		TRACE(" ON SEPARATOR %d\n", i);
Alexandre Julliard's avatar
Alexandre Julliard committed
1834 1835 1836 1837
		return -i;
	    }
	}
	else {
1838
	    if (PtInRect (&btnPtr->rect, *lpPt)) {
1839
		TRACE(" ON BUTTON %d\n", i);
1840 1841
                if (button)
                    *button = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1842 1843 1844 1845 1846
		return i;
	    }
	}
    }

1847
    TRACE(" NOWHERE\n");
1848
    return TOOLBAR_NOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1849 1850 1851
}


1852 1853
/* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
static BOOL
1854
TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
{
    INT nOldButtons, nNewButtons, iButton;
    BOOL fHasString = FALSE;

    if (iIndex < 0)  /* iIndex can be negative, what means adding at the end */
        iIndex = infoPtr->nNumButtons;

    nOldButtons = infoPtr->nNumButtons;
    nNewButtons = nOldButtons + nAddButtons;

    infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
    memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
            (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
    infoPtr->nNumButtons += nAddButtons;

    /* insert new buttons data */
    for (iButton = 0; iButton < nAddButtons; iButton++) {
        TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1873
        INT_PTR str;
1874

1875
        TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1876

1877
        ZeroMemory(btnPtr, sizeof(*btnPtr));
1878

1879
        btnPtr->iBitmap   = lpTbb[iButton].iBitmap;
1880 1881 1882 1883
        btnPtr->idCommand = lpTbb[iButton].idCommand;
        btnPtr->fsState   = lpTbb[iButton].fsState;
        btnPtr->fsStyle   = lpTbb[iButton].fsStyle;
        btnPtr->dwData    = lpTbb[iButton].dwData;
1884

1885
        if (btnPtr->fsStyle & BTNS_SEP)
1886
            str = -1;
1887
        else
1888 1889 1890
            str = lpTbb[iButton].iString;
        set_string_index( btnPtr, str, fUnicode );
        fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1891 1892 1893 1894 1895

        TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
    }

    if (infoPtr->nNumStrings > 0 || fHasString)
1896
        TOOLBAR_CalcToolbar(infoPtr);
1897
    else
1898 1899
        TOOLBAR_LayoutToolbar(infoPtr);
    TOOLBAR_AutoSize(infoPtr);
1900 1901 1902 1903 1904 1905 1906

    TOOLBAR_DumpToolbar(infoPtr, __LINE__);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
    return TRUE;
}


1907
static INT
1908
TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
1909 1910
{
    TBUTTON_INFO *btnPtr;
1911
    INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
1912

1913 1914
    if (CommandIsIndex) {
	TRACE("command is really index command=%d\n", idCommand);
1915
	if (idCommand >= infoPtr->nNumButtons) return -1;
1916 1917
	return idCommand;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1918
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1919
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1920
	if (btnPtr->idCommand == idCommand) {
1921
	    TRACE("command=%d index=%d\n", idCommand, i);
Alexandre Julliard's avatar
Alexandre Julliard committed
1922
	    return i;
Alexandre Julliard's avatar
Alexandre Julliard committed
1923
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1924
    }
1925
    TRACE("no index found for command=%d\n", idCommand);
Alexandre Julliard's avatar
Alexandre Julliard committed
1926 1927 1928 1929
    return -1;
}


1930
static INT
1931
TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
1932 1933
{
    TBUTTON_INFO *btnPtr;
1934
    INT nRunIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
1935 1936 1937 1938 1939 1940

    if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
	return -1;

    /* check index button */
    btnPtr = &infoPtr->buttons[nIndex];
1941
    if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1942 1943 1944 1945 1946 1947 1948 1949
	if (btnPtr->fsState & TBSTATE_CHECKED)
	    return nIndex;
    }

    /* check previous buttons */
    nRunIndex = nIndex - 1;
    while (nRunIndex >= 0) {
	btnPtr = &infoPtr->buttons[nRunIndex];
1950
	if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
	    if (btnPtr->fsState & TBSTATE_CHECKED)
		return nRunIndex;
	}
	else
	    break;
	nRunIndex--;
    }

    /* check next buttons */
    nRunIndex = nIndex + 1;
    while (nRunIndex < infoPtr->nNumButtons) {
1962
	btnPtr = &infoPtr->buttons[nRunIndex];
1963
	if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1964 1965 1966 1967 1968 1969 1970 1971
	    if (btnPtr->fsState & TBSTATE_CHECKED)
		return nRunIndex;
	}
	else
	    break;
	nRunIndex++;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1972 1973 1974 1975
    return -1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1976
static VOID
1977 1978
TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
		    WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1979
{
1980
    MSG msg;
Alexandre Julliard's avatar
Alexandre Julliard committed
1981 1982 1983 1984 1985 1986

    msg.hwnd = hwndMsg;
    msg.message = uMsg;
    msg.wParam = wParam;
    msg.lParam = lParam;
    msg.time = GetMessageTime ();
1987 1988
    msg.pt.x = (short)LOWORD(GetMessagePos ());
    msg.pt.y = (short)HIWORD(GetMessagePos ());
Alexandre Julliard's avatar
Alexandre Julliard committed
1989

1990
    SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
Alexandre Julliard's avatar
Alexandre Julliard committed
1991 1992
}

1993
static void
1994
TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
{
    if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
        TTTOOLINFOW ti;

        ZeroMemory(&ti, sizeof(TTTOOLINFOW));
        ti.cbSize   = sizeof (TTTOOLINFOW);
        ti.hwnd     = infoPtr->hwndSelf;
        ti.uId      = button->idCommand;
        ti.hinst    = 0;
        ti.lpszText = LPSTR_TEXTCALLBACKW;
        /* ti.lParam = random value from the stack? */

        SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
            0, (LPARAM)&ti);
    }
}

static void
2013
TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
{
    if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
        TTTOOLINFOW ti;

        ZeroMemory(&ti, sizeof(ti));
        ti.cbSize   = sizeof(ti);
        ti.hwnd     = infoPtr->hwndSelf;
        ti.uId      = button->idCommand;

        SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
    }
}

2027
static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
{
    /* Set the toolTip only for non-hidden, non-separator button */
    if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
    {
        TTTOOLINFOW ti;

        ZeroMemory(&ti, sizeof(ti));
        ti.cbSize = sizeof(ti);
        ti.hwnd = infoPtr->hwndSelf;
        ti.uId = button->idCommand;
        ti.rect = button->rect;
        SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
    }
}

2043 2044 2045 2046 2047 2048 2049
/* Creates the tooltip control */
static void
TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
{
    int i;
    NMTOOLTIPSCREATED nmttc;

2050
    infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            infoPtr->hwndSelf, 0, 0, 0);

    if (!infoPtr->hwndToolTip)
        return;

    /* Send NM_TOOLTIPSCREATED notification */
    nmttc.hwndToolTips = infoPtr->hwndToolTip;
    TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);

    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
        TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
        TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
    }
}

2068 2069 2070 2071 2072 2073 2074 2075
/* keeps available button list box sorted by button id */
static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
{
    int i;
    int count;
    PCUSTOMBUTTON btnInfo;
    HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);

Robert Shearman's avatar
Robert Shearman committed
2076
    TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2077

2078
    count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2079 2080 2081 2082

    /* position 0 is always separator */
    for (i = 1; i < count; i++)
    {
2083
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2084 2085
        if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
        {
2086 2087
            i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
            SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2088 2089 2090 2091
            return;
        }
    }
    /* id higher than all others add to end */
2092 2093
    i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
    SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2094 2095
}

2096
static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2097
{
2098
    NMTOOLBARW nmtb;
2099 2100 2101 2102 2103 2104

	TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);

    if (nIndexFrom == nIndexTo)
        return;

2105 2106 2107
    /* MSDN states that iItem is the index of the button, rather than the
     * command ID as used by every other NMTOOLBAR notification */
    nmtb.iItem = nIndexFrom;
2108
    if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2109 2110 2111 2112
    {
        PCUSTOMBUTTON btnInfo;
        NMHDR hdr;
        HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2113
        int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2114

2115
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2116

2117 2118 2119 2120
        SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
        SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
        SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
        SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

        if (nIndexTo <= 0)
            EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
        else
            EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);

        /* last item is always separator, so -2 instead of -1 */
        if (nIndexTo >= (count - 2))
            EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
        else
            EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);

2133
        SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
Robert Shearman's avatar
Robert Shearman committed
2134
        SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2135 2136 2137 2138 2139

        TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
    }
}

2140
static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2141
{
2142
    NMTOOLBARW nmtb;
2143 2144 2145

    TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);

2146 2147 2148
    /* MSDN states that iItem is the index of the button, rather than the
     * command ID as used by every other NMTOOLBAR notification */
    nmtb.iItem = nIndexAvail;
2149
    if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2150 2151 2152 2153 2154
    {
        PCUSTOMBUTTON btnInfo;
        NMHDR hdr;
        HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
        HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2155
        int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2156

2157
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2158 2159 2160 2161

        if (nIndexAvail != 0) /* index == 0 indicates separator */
        {
            /* remove from 'available buttons' list */
2162
            SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2163
            if (nIndexAvail == count-1)
2164
                SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2165
            else
2166
                SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2167 2168 2169 2170 2171 2172
        }
        else
        {
            PCUSTOMBUTTON btnNew;

            /* duplicate 'separator' button */
2173
            btnNew = Alloc(sizeof(CUSTOMBUTTON));
2174
            *btnNew = *btnInfo;
2175 2176 2177 2178
            btnInfo = btnNew;
        }

        /* insert into 'toolbar button' list */
2179 2180
        SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
        SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2181

2182
        SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2183 2184 2185 2186 2187

        TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
    }
}

2188
static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2189 2190 2191 2192 2193 2194
{
    PCUSTOMBUTTON btnInfo;
    HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);

    TRACE("Remove: index %d\n", index);

2195
    btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2196 2197 2198 2199 2200 2201

    /* send TBN_QUERYDELETE notification */
    if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
    {
        NMHDR hdr;

2202 2203
        SendMessageW(hwndList, LB_DELETESTRING, index, 0);
        SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2204

2205
        SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217

        /* insert into 'available button' list */
        if (!(btnInfo->btn.fsStyle & BTNS_SEP))
            TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
        else
            Free(btnInfo);

        TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
    }
}

/* drag list notification function for toolbar buttons list box */
2218 2219
static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
                                                        const DRAGLISTINFO *pDLI)
2220 2221 2222 2223 2224 2225 2226
{
    HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
    switch (pDLI->uNotification)
    {
    case DL_BEGINDRAG:
    {
        INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2227
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2228 2229 2230 2231 2232 2233 2234
        /* no dragging for last item (separator) */
        if (nCurrentItem >= (nCount - 1)) return FALSE;
        return TRUE;
    }
    case DL_DRAGGING:
    {
        INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2235
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
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
        /* no dragging past last item (separator) */
        if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
        {
            DrawInsert(hwnd, hwndList, nCurrentItem);
            /* FIXME: native uses "move button" cursor */
            return DL_COPYCURSOR;
        }

        /* not over toolbar buttons list */
        if (nCurrentItem < 0)
        {
            POINT ptWindow = pDLI->ptCursor;
            HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
            MapWindowPoints(NULL, hwnd, &ptWindow, 1);
            /* over available buttons list? */
            if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
                /* FIXME: native uses "move button" cursor */
                return DL_COPYCURSOR;
        }
        /* clear drag arrow */
        DrawInsert(hwnd, hwndList, -1);
        return DL_STOPCURSOR;
    }
    case DL_DROPPED:
    {
        INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2262 2263
        INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
        if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
        {
            /* clear drag arrow */
            DrawInsert(hwnd, hwndList, -1);
            /* move item */
            TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
        }
        /* not over toolbar buttons list */
        if (nIndexTo < 0)
        {
            POINT ptWindow = pDLI->ptCursor;
            HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
            MapWindowPoints(NULL, hwnd, &ptWindow, 1);
            /* over available buttons list? */
            if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
                TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
        }
        break;
    }
Robert Shearman's avatar
Robert Shearman committed
2283
    case DL_CANCELDRAG:
2284 2285 2286 2287 2288
        /* Clear drag arrow */
        DrawInsert(hwnd, hwndList, -1);
        break;
    }

Robert Shearman's avatar
Robert Shearman committed
2289
    return 0;
2290 2291 2292
}

/* drag list notification function for available buttons list box */
2293 2294
static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
                                                      const DRAGLISTINFO *pDLI)
2295 2296 2297 2298 2299 2300 2301 2302 2303
{
    HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
    switch (pDLI->uNotification)
    {
    case DL_BEGINDRAG:
        return TRUE;
    case DL_DRAGGING:
    {
        INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2304
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2305
        /* no dragging past last item (separator) */
Robert Shearman's avatar
Robert Shearman committed
2306
        if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
        {
            DrawInsert(hwnd, hwndList, nCurrentItem);
            /* FIXME: native uses "move button" cursor */
            return DL_COPYCURSOR;
        }

        /* not over toolbar buttons list */
        if (nCurrentItem < 0)
        {
            POINT ptWindow = pDLI->ptCursor;
            HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
            MapWindowPoints(NULL, hwnd, &ptWindow, 1);
            /* over available buttons list? */
            if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
                /* FIXME: native uses "move button" cursor */
                return DL_COPYCURSOR;
        }
        /* clear drag arrow */
        DrawInsert(hwnd, hwndList, -1);
        return DL_STOPCURSOR;
    }
    case DL_DROPPED:
    {
        INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2331 2332
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
        INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
Robert Shearman's avatar
Robert Shearman committed
2333
        if ((nIndexTo >= 0) && (nIndexTo < nCount))
2334 2335 2336 2337 2338 2339 2340
        {
            /* clear drag arrow */
            DrawInsert(hwnd, hwndList, -1);
            /* add item */
            TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
        }
    }
Robert Shearman's avatar
Robert Shearman committed
2341
    case DL_CANCELDRAG:
2342 2343 2344
        /* Clear drag arrow */
        DrawInsert(hwnd, hwndList, -1);
        break;
Robert Shearman's avatar
Robert Shearman committed
2345 2346
    }
    return 0;
2347 2348
}

2349
extern UINT uDragListMessage;
2350 2351 2352 2353 2354

/***********************************************************************
 * TOOLBAR_CustomizeDialogProc
 * This function implements the toolbar customization dialog.
 */
2355
static INT_PTR CALLBACK
2356 2357
TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
2358
    PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2359 2360
    PCUSTOMBUTTON btnInfo;
    NMTOOLBARA nmtb;
2361
    TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2362 2363 2364 2365

    switch (uMsg)
    {
	case WM_INITDIALOG:
2366
	    custInfo = (PCUSTDLG_INFO)lParam;
2367
	    SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2368

2369
	    if (custInfo)
2370
	    {
2371
		WCHAR Buffer[256];
2372 2373
		int i = 0;
		int index;
2374
		NMTBINITCUSTOMIZE nmtbic;
2375

2376
		infoPtr = custInfo->tbInfo;
2377 2378 2379 2380

		/* send TBN_QUERYINSERT notification */
		nmtb.iItem = custInfo->tbInfo->nNumButtons;

2381
		if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2382 2383
		    return FALSE;

2384
		nmtbic.hwndDialog = hwnd;
2385
		/* Send TBN_INITCUSTOMIZE notification */
2386
		if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2387
		    TBNRF_HIDEHELP)
2388
                {
2389 2390 2391
                    TRACE("TBNRF_HIDEHELP requested\n");
                    ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
                }
2392

2393 2394
		/* add items to 'toolbar buttons' list and check if removable */
		for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2395
                {
2396
                    btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2397
                    memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2398
                    btnInfo->btn.fsStyle = BTNS_SEP;
2399 2400
                    btnInfo->bVirtual = FALSE;
		    LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2401 2402

		    /* send TBN_QUERYDELETE notification */
2403
                    btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2404

Robert Shearman's avatar
Robert Shearman committed
2405 2406
		    index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
		    SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2407
		}
2408

Robert Shearman's avatar
Robert Shearman committed
2409
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2410

2411
		/* insert separator button into 'available buttons' list */
2412
                btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2413
		memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2414
		btnInfo->btn.fsStyle = BTNS_SEP;
2415 2416
		btnInfo->bVirtual = FALSE;
		btnInfo->bRemovable = TRUE;
2417
		LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
Robert Shearman's avatar
Robert Shearman committed
2418 2419
		index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2420 2421 2422 2423 2424

		/* insert all buttons into dsa */
		for (i = 0;; i++)
		{
		    /* send TBN_GETBUTTONINFO notification */
2425
                    NMTOOLBARW nmtb;
2426 2427 2428 2429
		    nmtb.iItem = i;
		    nmtb.pszText = Buffer;
		    nmtb.cchText = 256;

2430 2431 2432 2433
                    /* Clear previous button's text */
                    ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));

                    if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2434 2435
			break;

2436
		    TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%Id) %s\n",
2437 2438 2439 2440 2441
                        nmtb.tbButton.fsStyle, i, 
                        nmtb.tbButton.idCommand,
                        nmtb.tbButton.iString,
                        nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
                        : "");
2442

2443
		    /* insert button into the appropriate list */
2444
		    index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2445
		    if (index == -1)
2446
		    {
2447
                        btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2448 2449
			btnInfo->bVirtual = FALSE;
			btnInfo->bRemovable = TRUE;
2450 2451 2452
		    }
		    else
		    {
Robert Shearman's avatar
Robert Shearman committed
2453
                        btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, 
2454 2455 2456
                            IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
                    }

2457
                    btnInfo->btn = nmtb.tbButton;
2458
                    if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2459
                    {
2460
                        if (*nmtb.pszText)
2461 2462 2463 2464 2465 2466 2467
                            lstrcpyW(btnInfo->text, nmtb.pszText);
                        else if (nmtb.tbButton.iString >= 0 && 
                            nmtb.tbButton.iString < infoPtr->nNumStrings)
                        {
                            lstrcpyW(btnInfo->text, 
                                infoPtr->strings[nmtb.tbButton.iString]);
                        }
2468
		    }
2469 2470 2471

		    if (index == -1)
			TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2472 2473
		}

Robert Shearman's avatar
Robert Shearman committed
2474
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2475

2476
		/* select first item in the 'available' list */
Robert Shearman's avatar
Robert Shearman committed
2477
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2478

2479
		/* append 'virtual' separator button to the 'toolbar buttons' list */
2480
                btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2481
		memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2482
		btnInfo->btn.fsStyle = BTNS_SEP;
2483 2484
		btnInfo->bVirtual = TRUE;
		btnInfo->bRemovable = FALSE;
2485
		LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
Robert Shearman's avatar
Robert Shearman committed
2486 2487
		index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2488 2489

		/* select last item in the 'toolbar' list */
Robert Shearman's avatar
Robert Shearman committed
2490 2491
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2492

2493 2494
		MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
		MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2495

2496
		/* set focus and disable buttons */
2497
		PostMessageW (hwnd, WM_USER, 0, 0);
2498 2499 2500
	    }
	    return TRUE;

2501 2502 2503 2504 2505 2506 2507
	case WM_USER:
	    EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
	    EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
	    EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
	    SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
	    return TRUE;

2508 2509 2510 2511 2512 2513 2514
	case WM_CLOSE:
	    EndDialog(hwnd, FALSE);
	    return TRUE;

	case WM_COMMAND:
	    switch (LOWORD(wParam))
	    {
2515 2516 2517 2518 2519 2520 2521 2522
		case IDC_TOOLBARBTN_LBOX:
		    if (HIWORD(wParam) == LBN_SELCHANGE)
		    {
			PCUSTOMBUTTON btnInfo;
			NMTOOLBARA nmtb;
			int count;
			int index;

Robert Shearman's avatar
Robert Shearman committed
2523 2524
			count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
			index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2525 2526 2527

			/* send TBN_QUERYINSERT notification */
			nmtb.iItem = index;
2528
		        TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2529 2530

			/* get list box item */
Robert Shearman's avatar
Robert Shearman committed
2531
			btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2532 2533 2534 2535 2536 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

			if (index == (count - 1))
			{
			    /* last item (virtual separator) */
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
			}
			else if (index == (count - 2))
			{
			    /* second last item (last non-virtual item) */
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
			}
			else if (index == 0)
			{
			    /* first item */
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
			}
			else
			{
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
			    EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
			}

			EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
		    }
		    break;

		case IDC_MOVEUP_BTN:
		    {
Robert Shearman's avatar
Robert Shearman committed
2563
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2564
			TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2565 2566 2567 2568 2569
		    }
		    break;

		case IDC_MOVEDN_BTN: /* move down */
		    {
Robert Shearman's avatar
Robert Shearman committed
2570
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2571
			TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2572 2573 2574 2575 2576
		    }
		    break;

		case IDC_REMOVE_BTN: /* remove button */
		    {
Robert Shearman's avatar
Robert Shearman committed
2577
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2578 2579 2580 2581

			if (LB_ERR == index)
				break;

2582
			TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2583 2584
		    }
		    break;
2585 2586 2587 2588 2589 2590
		case IDC_HELP_BTN:
			TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
			break;
		case IDC_RESET_BTN:
			TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
			break;
2591 2592 2593 2594

		case IDOK: /* Add button */
		    {
			int index;
2595
			int indexto;
2596

Robert Shearman's avatar
Robert Shearman committed
2597 2598
			index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
			indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2599

2600
			TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2601 2602 2603
		    }
		    break;

2604 2605 2606 2607 2608 2609 2610
		case IDCANCEL:
		    EndDialog(hwnd, FALSE);
		    break;
	    }
	    return TRUE;

	case WM_DESTROY:
2611 2612 2613 2614 2615
	    {
		int count;
		int i;

		/* delete items from 'toolbar buttons' listbox*/
Robert Shearman's avatar
Robert Shearman committed
2616
		count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2617 2618
		for (i = 0; i < count; i++)
		{
Robert Shearman's avatar
Robert Shearman committed
2619
		    btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2620
		    Free(btnInfo);
Robert Shearman's avatar
Robert Shearman committed
2621
		    SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2622
		}
Robert Shearman's avatar
Robert Shearman committed
2623
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2624 2625 2626


		/* delete items from 'available buttons' listbox*/
Robert Shearman's avatar
Robert Shearman committed
2627
		count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2628 2629
		for (i = 0; i < count; i++)
		{
Robert Shearman's avatar
Robert Shearman committed
2630
		    btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2631
		    Free(btnInfo);
Robert Shearman's avatar
Robert Shearman committed
2632
		    SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2633
		}
Robert Shearman's avatar
Robert Shearman committed
2634
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2635
            }
2636 2637 2638 2639 2640 2641 2642 2643
	    return TRUE;

	case WM_DRAWITEM:
	    if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
	    {
		LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
		RECT rcButton;
		RECT rcText;
2644
		HPEN hPen, hOldPen;
2645 2646 2647 2648
		HBRUSH hOldBrush;
		COLORREF oldText = 0;
		COLORREF oldBk = 0;

2649
		/* get item data */
2650
               btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2651
		if (btnInfo == NULL)
2652
		{
2653
		    FIXME("btnInfo invalid\n");
2654
		    return TRUE;
2655 2656
		}

2657
		/* set colors and select objects */
2658
		oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2659
		if (btnInfo->bVirtual)
2660
		   oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2661
		else
2662
		   oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2663
                hPen = CreatePen( PS_SOLID, 1,
2664
                                 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2665
		hOldPen = SelectObject (lpdis->hDC, hPen );
2666 2667 2668 2669 2670 2671 2672
		hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));

		/* fill background rectangle */
		Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
			   lpdis->rcItem.right, lpdis->rcItem.bottom);

		/* calculate button and text rectangles */
2673
                rcButton = lpdis->rcItem;
2674
		InflateRect (&rcButton, -1, -1);
2675
                rcText = rcButton;
2676
		rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2677 2678 2679 2680 2681 2682 2683
		rcText.left = rcButton.right + 2;

		/* draw focus rectangle */
		if (lpdis->itemState & ODS_FOCUS)
		    DrawFocusRect (lpdis->hDC, &lpdis->rcItem);

		/* draw button */
2684
		if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2685
		    DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2686

2687
		/* draw image and text */
2688
		if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2689 2690 2691 2692 2693 2694
			HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, 
				btnInfo->btn.iBitmap));
		    ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap), 
				lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
		}
		DrawTextW (lpdis->hDC,  btnInfo->text, -1, &rcText,
2695 2696
			       DT_LEFT | DT_VCENTER | DT_SINGLELINE);

2697
		/* delete objects and reset colors */
2698 2699
		SelectObject (lpdis->hDC, hOldBrush);
		SelectObject (lpdis->hDC, hOldPen);
2700 2701
		SetBkColor (lpdis->hDC, oldBk);
		SetTextColor (lpdis->hDC, oldText);
2702
                DeleteObject( hPen );
2703 2704 2705 2706 2707 2708 2709 2710 2711
		return TRUE;
	    }
	    return FALSE;

	case WM_MEASUREITEM:
	    if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
	    {
		MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;

2712
		lpmis->itemHeight = 15 + 8; /* default height */
2713 2714 2715 2716 2717 2718

		return TRUE;
	    }
	    return FALSE;

	default:
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
            if (uDragListMessage && (uMsg == uDragListMessage))
            {
                if (wParam == IDC_TOOLBARBTN_LBOX)
                {
                    LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
                        custInfo, hwnd, (DRAGLISTINFO *)lParam);
                    SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
                    return TRUE;
                }
                else if (wParam == IDC_AVAILBTN_LBOX)
                {
                    LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
                        custInfo, hwnd, (DRAGLISTINFO *)lParam);
                    SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
                    return TRUE;
                }
            }
            return FALSE;
2737 2738 2739
    }
}

2740 2741 2742 2743 2744 2745
static BOOL
TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
{
    HBITMAP hbmLoad;
    INT nCountBefore = ImageList_GetImageCount(himlDef);
    INT nCountAfter;
2746
    INT cxIcon, cyIcon;
2747 2748 2749 2750 2751 2752
    INT nAdded;
    INT nIndex;

    TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
    /* Add bitmaps to the default image list */
    if (bitmap->hInst == NULL)         /* a handle was passed */
2753
        hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2754 2755 2756
    else if (bitmap->hInst == COMCTL32_hModule)
        hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
                              IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2757 2758
    else
        hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2759 2760 2761

    /* enlarge the bitmap if needed */
    ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2762 2763
    if (bitmap->hInst != COMCTL32_hModule)
        COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
    
    nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
    DeleteObject(hbmLoad);
    if (nIndex == -1)
        return FALSE;
    
    nCountAfter = ImageList_GetImageCount(himlDef);
    nAdded =  nCountAfter - nCountBefore;
    if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
    {
        ImageList_SetImageCount(himlDef, nCountBefore + 1);
    } else if (nAdded > (INT)bitmap->nButtons) {
        TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
            nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
    }

    infoPtr->nNumBitmaps += nAdded;
    return TRUE;
}

static void
TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
{
    HIMAGELIST himlDef;
    HIMAGELIST himlNew;
    INT cx, cy;
    INT i;
    
    himlDef = GETDEFIMAGELIST(infoPtr, 0);
    if (himlDef == NULL || himlDef != infoPtr->himlInt)
        return;
    if (!ImageList_GetIconSize(himlDef, &cx, &cy))
        return;
    if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
        return;

    TRACE("Update icon size: %dx%d -> %dx%d\n",
        cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);

    himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2804
                                ILC_COLOR32|ILC_MASK, 8, 2);
2805 2806 2807 2808 2809 2810 2811 2812
    for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
        TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
    TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
    infoPtr->himlInt = himlNew;

    infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
    ImageList_Destroy(himlDef);
}
2813

2814 2815 2816 2817
/***********************************************************************
 * TOOLBAR_AddBitmap:  Add the bitmaps to the default image list.
 *
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
2818
static LRESULT
2819
TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
2820
{
2821
    TBITMAP_INFO info;
2822
    INT iSumButtons, i;
2823
    HIMAGELIST himlDef;
Alexandre Julliard's avatar
Alexandre Julliard committed
2824

2825
    TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2826
    if (!lpAddBmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
2827 2828
	return -1;

2829 2830
    if (lpAddBmp->hInst == HINST_COMMCTRL)
    {
2831 2832 2833 2834
        info.hInst = COMCTL32_hModule;
        switch (lpAddBmp->nID)
        {
            case IDB_STD_SMALL_COLOR:
2835
            case 2:
2836 2837 2838 2839
	        info.nButtons = 15;
	        info.nID = IDB_STD_SMALL;
	        break;
            case IDB_STD_LARGE_COLOR:
2840
            case 3:
2841 2842 2843 2844
	        info.nButtons = 15;
	        info.nID = IDB_STD_LARGE;
	        break;
            case IDB_VIEW_SMALL_COLOR:
2845
            case 6:
2846 2847 2848 2849
	        info.nButtons = 12;
	        info.nID = IDB_VIEW_SMALL;
	        break;
            case IDB_VIEW_LARGE_COLOR:
2850
            case 7:
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
	        info.nButtons = 12;
	        info.nID = IDB_VIEW_LARGE;
	        break;
            case IDB_HIST_SMALL_COLOR:
	        info.nButtons = 5;
	        info.nID = IDB_HIST_SMALL;
	        break;
            case IDB_HIST_LARGE_COLOR:
	        info.nButtons = 5;
	        info.nID = IDB_HIST_LARGE;
	        break;
	    default:
2863
                WARN("unknown bitmap id, %Id\n", lpAddBmp->nID);
2864 2865
	        return -1;
	}
2866

2867
        TRACE ("adding %d internal bitmaps\n", info.nButtons);
2868

2869
	/* Windows resize all the buttons to the size of a newly added standard image */
2870
	if (lpAddBmp->nID & 1)
2871
	{
2872
	    /* large icons: 24x24. Will make the button 31x30 */
2873
	    SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2874 2875
	}
	else
2876
	{
2877
	    /* small icons: 16x16. Will make the buttons 23x22 */
2878
	    SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2879
	}
2880

2881
	TOOLBAR_CalcToolbar (infoPtr);
2882 2883 2884
    }
    else
    {
2885
	info.nButtons = count;
2886 2887
	info.hInst = lpAddBmp->hInst;
	info.nID = lpAddBmp->nID;
2888
	TRACE("adding %d bitmaps\n", info.nButtons);
2889
    }
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
    
    /* check if the bitmap is already loaded and compute iSumButtons */
    iSumButtons = 0;
    for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
    {
        if (infoPtr->bitmaps[i].hInst == info.hInst &&
            infoPtr->bitmaps[i].nID == info.nID)
            return iSumButtons;
        iSumButtons += infoPtr->bitmaps[i].nButtons;
    }
2900

2901
    if (!infoPtr->cimlDef) {
2902
	/* create new default image list */
2903
        TRACE ("creating default image list\n");
2904

2905
        himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2906
                                    ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2907
	TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2908
        infoPtr->himlInt = himlDef;
2909 2910 2911 2912 2913 2914 2915 2916
    }
    else {
        himlDef = GETDEFIMAGELIST(infoPtr, 0);
    }

    if (!himlDef) {
        WARN("No default image list available\n");
        return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2917 2918
    }

2919 2920
    if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
        return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2921

2922
    TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2923
    infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2924
    infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2925 2926 2927
    infoPtr->nNumBitmapInfos++;
    TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);

2928
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2929
    return iSumButtons;
Alexandre Julliard's avatar
Alexandre Julliard committed
2930 2931 2932 2933
}


static LRESULT
2934
TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
2935
{
2936
    TRACE("adding %d buttons (unicode=%d)\n", nAddButtons, fUnicode);
Alexandre Julliard's avatar
Alexandre Julliard committed
2937

2938
    return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2939
}
Alexandre Julliard's avatar
Alexandre Julliard committed
2940 2941 2942


static LRESULT
2943
TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
2944
{
2945
#define MAX_RESOURCE_STRING_LENGTH 512
2946
    BOOL fFirstString = (infoPtr->nNumStrings == 0);
2947
    INT nIndex = infoPtr->nNumStrings;
Alexandre Julliard's avatar
Alexandre Julliard committed
2948

2949
    TRACE("%p, %Ix\n", hInstance, lParam);
2950 2951

    if (IS_INTRESOURCE(lParam)) {
2952 2953 2954
	WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
	WCHAR delimiter;
	WCHAR *next_delim;
2955
        HRSRC hrsrc;
2956
	WCHAR *p;
2957
	INT len;
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969

	TRACE("adding string from resource\n");

        if (!hInstance) return -1;

        hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
                               (LPWSTR)RT_STRING );
        if (!hrsrc)
        {
            TRACE("string not found in resources\n");
            return -1;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2970

2971
        len = LoadStringW (hInstance, (UINT)lParam,
2972
                             szString, MAX_RESOURCE_STRING_LENGTH);
Alexandre Julliard's avatar
Alexandre Julliard committed
2973

2974 2975 2976
        TRACE("len=%d %s\n", len, debugstr_w(szString));
        if (len == 0 || len == 1)
            return nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
2977

2978
        TRACE("delimiter: 0x%x\n", *szString);
2979 2980 2981
        delimiter = *szString;
        p = szString + 1;

2982
        while ((next_delim = wcschr(p, delimiter)) != NULL) {
2983
            *next_delim = 0;
2984 2985 2986 2987 2988 2989
            if (next_delim + 1 >= szString + len)
            {
                /* this may happen if delimiter == '\0' or if the last char is a
                 * delimiter (then it is ignored like the native does) */
                break;
            }
2990 2991 2992 2993 2994 2995 2996

            infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
            Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
            infoPtr->nNumStrings++;

            p = next_delim + 1;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2997 2998
    }
    else {
2999
	LPWSTR p = (LPWSTR)lParam;
3000
	INT len;
Alexandre Julliard's avatar
Alexandre Julliard committed
3001

3002 3003
	if (p == NULL)
	    return -1;
3004
	TRACE("adding string(s) from array\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
3005
	while (*p) {
3006
            len = lstrlenW (p);
Alexandre Julliard's avatar
Alexandre Julliard committed
3007

3008 3009 3010
            TRACE("len=%d %s\n", len, debugstr_w(p));
            infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
            Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
Alexandre Julliard's avatar
Alexandre Julliard committed
3011 3012 3013 3014 3015 3016
	    infoPtr->nNumStrings++;

	    p += (len+1);
	}
    }

3017
    if (fFirstString)
3018
        TOOLBAR_CalcToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
3019 3020 3021 3022
    return nIndex;
}


3023
static LRESULT
3024
TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3025
{
3026
    BOOL fFirstString = (infoPtr->nNumStrings == 0);
3027
    LPSTR p;
3028
    INT nIndex;
3029
    INT len;
3030

3031
    TRACE("%p, %Ix\n", hInstance, lParam);
3032 3033

    if (IS_INTRESOURCE(lParam))  /* load from resources */
3034
        return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3035

3036 3037 3038
    p = (LPSTR)lParam;
    if (p == NULL)
        return -1;
3039

3040
    TRACE("adding string(s) from array\n");
3041 3042 3043 3044
    nIndex = infoPtr->nNumStrings;
    while (*p) {
        len = strlen (p);
        TRACE("len=%d \"%s\"\n", len, p);
3045

3046 3047 3048
        infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
        Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
        infoPtr->nNumStrings++;
3049

3050
        p += (len+1);
3051 3052
    }

3053
    if (fFirstString)
3054
        TOOLBAR_CalcToolbar(infoPtr);
3055 3056
    return nIndex;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3057 3058 3059


static LRESULT
3060
TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3061
{
3062
    TRACE("auto sizing, style=%#lx\n", infoPtr->dwStyle);
3063
    TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
Alexandre Julliard's avatar
Alexandre Julliard committed
3064

3065 3066
    if (!(infoPtr->dwStyle & CCS_NORESIZE))
    {
3067
        RECT window_rect, client_rect, parent_rect, border;
3068 3069 3070
        UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
        HWND parent;
        INT  x, y, cx, cy;
Alexandre Julliard's avatar
Alexandre Julliard committed
3071

3072
        parent = GetParent (infoPtr->hwndSelf);
3073

3074 3075
        if (!parent || !infoPtr->bDoRedraw)
            return 0;
3076

3077 3078 3079 3080 3081 3082 3083
        GetWindowRect(infoPtr->hwndSelf, &window_rect);
        GetClientRect(infoPtr->hwndSelf, &client_rect);
        border = window_rect;
        MapWindowPoints(0, infoPtr->hwndSelf, (POINT *)&border, 2);
        border.right -= border.left + client_rect.right - client_rect.left;
        border.bottom -= border.top + client_rect.bottom - client_rect.top;

3084
        GetClientRect(parent, &parent_rect);
3085

3086 3087
        x = parent_rect.left;
        y = parent_rect.top;
3088

3089 3090
        cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
        cx = parent_rect.right - parent_rect.left;
3091 3092 3093

        if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
        {
3094
            MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3095 3096 3097
            y = window_rect.top;
        }
        if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3098
            y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
Alexandre Julliard's avatar
Alexandre Julliard committed
3099

3100 3101
        if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
            uPosFlags |= SWP_NOMOVE;
3102 3103 3104

        if (!(infoPtr->dwStyle & CCS_NOMOVEY) && !(infoPtr->dwStyle & CCS_NODIVIDER))
            y += GetSystemMetrics(SM_CYEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3105

3106 3107 3108 3109
        x += border.left;
        y += border.top;
        cx += border.right;
        cy += border.bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
3110

3111
        SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3112 3113
    }

3114 3115 3116 3117 3118 3119
    if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
    {
        TOOLBAR_LayoutToolbar(infoPtr);
        InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
3120 3121
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3122 3123


3124
static inline LRESULT
3125
TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3126 3127 3128 3129 3130
{
    return infoPtr->nNumButtons;
}


3131 3132
static inline LRESULT
TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
Alexandre Julliard's avatar
Alexandre Julliard committed
3133
{
3134
    infoPtr->dwStructSize = Size;
Alexandre Julliard's avatar
Alexandre Julliard committed
3135 3136 3137 3138 3139 3140

    return 0;
}


static LRESULT
3141
TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
Alexandre Julliard's avatar
Alexandre Julliard committed
3142 3143
{
    TBUTTON_INFO *btnPtr;
3144
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3145

3146
    TRACE("button %d, iBitmap now %d\n", Id, Index);
3147

3148
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3149 3150 3151 3152
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3153
    btnPtr->iBitmap = Index;
Alexandre Julliard's avatar
Alexandre Julliard committed
3154

3155 3156
    /* we HAVE to erase the background, the new bitmap could be */
    /* transparent */
3157
    InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3158 3159 3160 3161 3162 3163

    return TRUE;
}


static LRESULT
3164
TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3165 3166
{
    TBUTTON_INFO *btnPtr;
3167 3168
    INT nIndex;
    INT nOldIndex = -1;
3169
    BOOL bChecked = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3170

3171
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3172

3173
    TRACE("hwnd %p, btn index %d, lParam %Ix\n", infoPtr->hwndSelf, nIndex, lParam);
3174

Alexandre Julliard's avatar
Alexandre Julliard committed
3175 3176 3177 3178
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
Alexandre Julliard's avatar
Alexandre Julliard committed
3179

3180
    bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3181

3182
    if (!LOWORD(lParam))
Alexandre Julliard's avatar
Alexandre Julliard committed
3183
	btnPtr->fsState &= ~TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3184
    else {
3185
	if (btnPtr->fsStyle & BTNS_GROUP) {
3186
	    nOldIndex =
Alexandre Julliard's avatar
Alexandre Julliard committed
3187 3188 3189 3190 3191 3192
		TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
	    if (nOldIndex == nIndex)
		return 0;
	    if (nOldIndex != -1)
		infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
3193
	btnPtr->fsState |= TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3194
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3195

3196 3197
    if( bChecked != LOWORD(lParam) )
    {
Robert Shearman's avatar
Robert Shearman committed
3198
        if (nOldIndex != -1)
3199 3200
            InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3201
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3202

Alexandre Julliard's avatar
Alexandre Julliard committed
3203
    /* FIXME: Send a WM_NOTIFY?? */
Alexandre Julliard's avatar
Alexandre Julliard committed
3204

Alexandre Julliard's avatar
Alexandre Julliard committed
3205 3206 3207 3208 3209
    return TRUE;
}


static LRESULT
3210
TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3211
{
3212
    return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3213 3214 3215
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3216
static LRESULT
3217
TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3218
{
3219
    CUSTDLG_INFO custInfo;
3220 3221
    LRESULT ret;
    NMHDR nmhdr;
Alexandre Julliard's avatar
Alexandre Julliard committed
3222

3223
    custInfo.tbInfo = infoPtr;
3224
    custInfo.tbHwnd = infoPtr->hwndSelf;
3225

3226
    /* send TBN_BEGINADJUST notification */
3227
    TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3228

3229 3230
    ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
                           infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3231 3232

    /* send TBN_ENDADJUST notification */
3233
    TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3234 3235

    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3236
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3237 3238 3239


static LRESULT
3240
TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
3241
{
Robert Shearman's avatar
Robert Shearman committed
3242
    NMTOOLBARW nmtb;
3243
    TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
Alexandre Julliard's avatar
Alexandre Julliard committed
3244

Alexandre Julliard's avatar
Alexandre Julliard committed
3245
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3246
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3247

Robert Shearman's avatar
Robert Shearman committed
3248
    memset(&nmtb, 0, sizeof(nmtb));
3249 3250 3251 3252 3253 3254 3255
    nmtb.iItem = btnPtr->idCommand;
    nmtb.tbButton.iBitmap = btnPtr->iBitmap;
    nmtb.tbButton.idCommand = btnPtr->idCommand;
    nmtb.tbButton.fsState = btnPtr->fsState;
    nmtb.tbButton.fsStyle = btnPtr->fsStyle;
    nmtb.tbButton.dwData = btnPtr->dwData;
    nmtb.tbButton.iString = btnPtr->iString;
3256
    TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
Robert Shearman's avatar
Robert Shearman committed
3257

3258
    TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
Alexandre Julliard's avatar
Alexandre Julliard committed
3259

3260
    infoPtr->nHotItem = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3261
    if (infoPtr->nNumButtons == 1) {
3262
	TRACE(" simple delete\n");
3263
        free_string( infoPtr->buttons );
3264
	Free (infoPtr->buttons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3265 3266 3267 3268
	infoPtr->buttons = NULL;
	infoPtr->nNumButtons = 0;
    }
    else {
Alexandre Julliard's avatar
Alexandre Julliard committed
3269
	TBUTTON_INFO *oldButtons = infoPtr->buttons;
3270
        TRACE("complex delete [nIndex=%d]\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3271

Alexandre Julliard's avatar
Alexandre Julliard committed
3272
	infoPtr->nNumButtons--;
3273
	infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3274 3275 3276 3277
        if (nIndex > 0) {
            memcpy (&infoPtr->buttons[0], &oldButtons[0],
                    nIndex * sizeof(TBUTTON_INFO));
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3278

Alexandre Julliard's avatar
Alexandre Julliard committed
3279 3280 3281 3282 3283
        if (nIndex < infoPtr->nNumButtons) {
            memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
                    (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
        }

3284
        free_string( oldButtons + nIndex );
3285
	Free (oldButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3286 3287
    }

3288
    TOOLBAR_LayoutToolbar(infoPtr);
3289

3290
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3291 3292 3293 3294 3295 3296

    return TRUE;
}


static LRESULT
3297
TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3298 3299
{
    TBUTTON_INFO *btnPtr;
3300
    INT nIndex;
3301
    DWORD bState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3302

3303
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3304

3305
    TRACE("hwnd %p, btn id %d, lParam %Ix\n", infoPtr->hwndSelf, Id, lParam);
3306

Alexandre Julliard's avatar
Alexandre Julliard committed
3307 3308 3309 3310
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3311 3312 3313 3314

    bState = btnPtr->fsState & TBSTATE_ENABLED;

    /* update the toolbar button state */
3315
    if(!LOWORD(lParam)) {
3316 3317
 	btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
    } else {
Alexandre Julliard's avatar
Alexandre Julliard committed
3318
	btnPtr->fsState |= TBSTATE_ENABLED;
3319
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3320

3321
    /* redraw the button only if the state of the button changed */
3322
    if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3323
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3324 3325 3326 3327 3328

    return TRUE;
}


3329
static inline LRESULT
3330
TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3331 3332 3333
{
    return infoPtr->bAnchor;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3334 3335 3336


static LRESULT
3337
TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3338
{
3339
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3340

3341
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3342
    if (nIndex == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3343
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3344 3345 3346 3347 3348

    return infoPtr->buttons[nIndex].iBitmap;
}


Patrik Stridvall's avatar
Patrik Stridvall committed
3349
static inline LRESULT
3350
TOOLBAR_GetBitmapFlags (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
3351
{
3352
    return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3353 3354 3355 3356
}


static LRESULT
3357
TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
Alexandre Julliard's avatar
Alexandre Julliard committed
3358 3359 3360
{
    TBUTTON_INFO *btnPtr;

3361 3362
    if (lpTbb == NULL)
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3363 3364 3365 3366 3367 3368 3369 3370 3371

    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
    lpTbb->iBitmap   = btnPtr->iBitmap;
    lpTbb->idCommand = btnPtr->idCommand;
    lpTbb->fsState   = btnPtr->fsState;
    lpTbb->fsStyle   = btnPtr->fsStyle;
3372 3373
    lpTbb->bReserved[0] = 0;
    lpTbb->bReserved[1] = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3374 3375 3376 3377 3378 3379 3380
    lpTbb->dwData    = btnPtr->dwData;
    lpTbb->iString   = btnPtr->iString;

    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3381
static LRESULT
3382
TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
3383
{
3384
    /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
Alexandre Julliard's avatar
Alexandre Julliard committed
3385
    TBUTTON_INFO *btnPtr;
3386
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3387

3388 3389
    if (lpTbInfo == NULL)
	return -1;
3390

3391
    /* MSDN documents an iImageLabel field added in Vista but it is not present in
3392 3393 3394 3395 3396 3397
     * the headers and tests shows that even with comctl 6 Vista accepts only the
     * original TBBUTTONINFO size
     */
    if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
    {
        WARN("Invalid button size\n");
3398
	return -1;
3399
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3400

3401
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
Alexandre Julliard's avatar
Alexandre Julliard committed
3402 3403 3404
    if (nIndex == -1)
	return -1;

3405
    btnPtr = &infoPtr->buttons[nIndex];
Alexandre Julliard's avatar
Alexandre Julliard committed
3406 3407 3408 3409 3410 3411 3412
    if (lpTbInfo->dwMask & TBIF_COMMAND)
	lpTbInfo->idCommand = btnPtr->idCommand;
    if (lpTbInfo->dwMask & TBIF_IMAGE)
	lpTbInfo->iImage = btnPtr->iBitmap;
    if (lpTbInfo->dwMask & TBIF_LPARAM)
	lpTbInfo->lParam = btnPtr->dwData;
    if (lpTbInfo->dwMask & TBIF_SIZE)
3413 3414 3415 3416 3417
        /* tests show that for separators TBIF_SIZE returns not calculated width,
           but cx property, that differs from 0 only if application have
           specifically set it */
        lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
            ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
Alexandre Julliard's avatar
Alexandre Julliard committed
3418 3419 3420 3421
    if (lpTbInfo->dwMask & TBIF_STATE)
	lpTbInfo->fsState = btnPtr->fsState;
    if (lpTbInfo->dwMask & TBIF_STYLE)
	lpTbInfo->fsStyle = btnPtr->fsStyle;
3422 3423 3424
    if (lpTbInfo->dwMask & TBIF_TEXT) {
        /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
           can't use TOOLBAR_GetText here */
3425
        if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3426 3427 3428 3429 3430
            LPWSTR lpText = (LPWSTR)btnPtr->iString;
            if (bUnicode)
                Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
            else
                Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3431
        } else if (!bUnicode || lpTbInfo->pszText)
3432
            lpTbInfo->pszText[0] = '\0';
3433 3434 3435
    }
    return nIndex;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3436 3437


3438
static inline LRESULT
3439
TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3440
{
3441 3442
    return MAKELONG((WORD)infoPtr->nButtonWidth,
                    (WORD)infoPtr->nButtonHeight);
Alexandre Julliard's avatar
Alexandre Julliard committed
3443
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3444 3445 3446


static LRESULT
3447
TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3448
{
3449 3450
    INT nIndex;
    LPWSTR lpText;
3451
    LRESULT ret = 0;
3452

3453
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3454
    if (nIndex == -1)
3455 3456
	return -1;

3457
    lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
Alexandre Julliard's avatar
Alexandre Julliard committed
3458

3459
    if (isW)
3460
    {
3461 3462
        if (lpText)
        {
3463 3464
            ret = lstrlenW (lpText);
            if (lpStr) lstrcpyW (lpStr, lpText);
3465
        }
3466
    }
3467 3468 3469
    else
        ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
                                  (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3470
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3471 3472 3473
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3474
static LRESULT
3475
TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3476
{
3477
    TRACE("hwnd %p, wParam %Id\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3478
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3479
    return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3480 3481 3482
}


3483
static inline LRESULT
3484
TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3485
{
3486 3487
    TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
3488 3489
    return infoPtr->dwExStyle;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3490 3491 3492


static LRESULT
3493
TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3494
{
3495
    TRACE("hwnd %p, wParam %Id\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3496
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3497
    return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3498 3499 3500
}


3501
static LRESULT
3502
TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3503
{
3504
    if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme))
3505 3506 3507 3508 3509 3510 3511
	return -1;

    if (infoPtr->nHotItem < 0)
	return -1;

    return (LRESULT)infoPtr->nHotItem;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3512 3513 3514


static LRESULT
3515
TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3516
{
3517
    TRACE("hwnd %p, wParam %Id\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3518
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3519
    return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3520 3521 3522
}


3523
static LRESULT
3524
TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3525
{
3526
    TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3527 3528 3529 3530 3531 3532 3533

    *lptbim = infoPtr->tbim;

    return 0;
}


3534
static inline LRESULT
3535
TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3536
{
3537
    TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3538 3539 3540

    return (LRESULT)infoPtr->clrInsertMark;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3541 3542 3543


static LRESULT
3544
TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
3545 3546 3547 3548 3549 3550
{
    TBUTTON_INFO *btnPtr;

    btnPtr = &infoPtr->buttons[nIndex];
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;
3551

3552 3553 3554 3555
    if (lpRect == NULL)
	return FALSE;
    if (btnPtr->fsState & TBSTATE_HIDDEN)
	return FALSE;
3556

Alexandre Julliard's avatar
Alexandre Julliard committed
3557 3558 3559 3560 3561 3562 3563 3564 3565
    lpRect->left   = btnPtr->rect.left;
    lpRect->right  = btnPtr->rect.right;
    lpRect->bottom = btnPtr->rect.bottom;
    lpRect->top    = btnPtr->rect.top;

    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3566
static LRESULT
3567
TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
3568
{
Alexandre Julliard's avatar
Alexandre Julliard committed
3569 3570 3571
    if (lpSize == NULL)
	return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
3572 3573
    lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
    lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
Alexandre Julliard's avatar
Alexandre Julliard committed
3574

3575 3576
    TRACE("maximum size %ld x %ld\n", infoPtr->rcBound.right - infoPtr->rcBound.left,
            infoPtr->rcBound.bottom - infoPtr->rcBound.top);
Alexandre Julliard's avatar
Alexandre Julliard committed
3577 3578 3579 3580 3581

    return TRUE;
}


3582
/* << TOOLBAR_GetObject >> */
3583 3584


3585
static inline LRESULT
3586
TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3587
{
3588
    return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3589
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3590 3591 3592


static LRESULT
3593
TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
3594 3595
{
    TBUTTON_INFO *btnPtr;
3596
    INT        nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3597

3598
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3599 3600 3601
    btnPtr = &infoPtr->buttons[nIndex];
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;
3602

3603 3604
    if (lpRect == NULL)
	return FALSE;
3605

Alexandre Julliard's avatar
Alexandre Julliard committed
3606 3607 3608 3609 3610 3611 3612
    lpRect->left   = btnPtr->rect.left;
    lpRect->right  = btnPtr->rect.right;
    lpRect->bottom = btnPtr->rect.bottom;
    lpRect->top    = btnPtr->rect.top;

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3613 3614


3615
static inline LRESULT
3616
TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3617
{
3618
    return infoPtr->nRows;
Alexandre Julliard's avatar
Alexandre Julliard committed
3619 3620 3621 3622
}


static LRESULT
3623
TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3624
{
3625
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3626

3627
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3628 3629
    if (nIndex == -1)
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3630 3631 3632 3633 3634

    return infoPtr->buttons[nIndex].fsState;
}


3635
static inline LRESULT
3636
TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3637
{
3638
    return infoPtr->dwStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
3639 3640 3641
}


3642
static inline LRESULT
3643
TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3644 3645 3646
{
    return infoPtr->nMaxTextRows;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3647 3648 3649


static LRESULT
3650
TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3651
{
3652 3653
    if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
        TOOLBAR_TooltipCreateControl(infoPtr);
3654
    return (LRESULT)infoPtr->hwndToolTip;
Alexandre Julliard's avatar
Alexandre Julliard committed
3655 3656 3657
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3658
static LRESULT
3659
TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3660
{
Robert Shearman's avatar
Robert Shearman committed
3661
    TRACE("%s hwnd=%p\n",
3662
	   infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
3663 3664 3665

    return infoPtr->bUnicode;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3666 3667


3668
static inline LRESULT
3669
TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3670 3671 3672 3673 3674
{
    return infoPtr->iVersion;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3675
static LRESULT
3676
TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
Alexandre Julliard's avatar
Alexandre Julliard committed
3677 3678
{
    TBUTTON_INFO *btnPtr;
3679
    BYTE oldState;
3680
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3681

3682 3683
    TRACE("\n");

3684
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3685 3686 3687 3688
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3689
    oldState = btnPtr->fsState;
3690 3691

    if (fHide)
Alexandre Julliard's avatar
Alexandre Julliard committed
3692
	btnPtr->fsState |= TBSTATE_HIDDEN;
3693 3694
    else
	btnPtr->fsState &= ~TBSTATE_HIDDEN;
Alexandre Julliard's avatar
Alexandre Julliard committed
3695

3696 3697 3698 3699
    if (oldState != btnPtr->fsState) {
        TOOLBAR_LayoutToolbar (infoPtr);
        InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3700 3701 3702 3703 3704

    return TRUE;
}


3705
static inline LRESULT
3706
TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
Alexandre Julliard's avatar
Alexandre Julliard committed
3707
{
3708
    return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
3709 3710 3711
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3712
static LRESULT
3713
TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
Alexandre Julliard's avatar
Alexandre Julliard committed
3714 3715
{
    TBUTTON_INFO *btnPtr;
3716
    INT nIndex;
3717
    DWORD oldState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3718

3719
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3720 3721 3722 3723
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3724
    oldState = btnPtr->fsState;
3725 3726

    if (fIndeterminate)
Alexandre Julliard's avatar
Alexandre Julliard committed
3727
	btnPtr->fsState |= TBSTATE_INDETERMINATE;
3728 3729
    else
	btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3730

3731
    if(oldState != btnPtr->fsState)
3732
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3733 3734 3735 3736 3737 3738

    return TRUE;
}


static LRESULT
3739
TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
3740
{
3741 3742
    if (lpTbb == NULL)
	return FALSE;
3743 3744 3745 3746 3747 3748 3749 3750

    if (nIndex == -1) {
       /* EPP: this seems to be an undocumented call (from my IE4)
	* I assume in that case that:
	* - index of insertion is at the end of existing buttons
	* I only see this happen with nIndex == -1, but it could have a special
	* meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
	*/
3751
	nIndex = infoPtr->nNumButtons;
3752 3753 3754

    } else if (nIndex < 0)
       return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3755

3756
    TRACE("inserting button index=%d\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3757 3758
    if (nIndex > infoPtr->nNumButtons) {
	nIndex = infoPtr->nNumButtons;
3759
	TRACE("adjust index=%d\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3760 3761
    }

3762
    return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3763 3764
}

3765
/* << TOOLBAR_InsertMarkHitTest >> */
Alexandre Julliard's avatar
Alexandre Julliard committed
3766 3767


Alexandre Julliard's avatar
Alexandre Julliard committed
3768
static LRESULT
3769
TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3770
{
3771
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3772

3773
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3774
    if (nIndex == -1)
3775
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3776 3777 3778 3779 3780 3781

    return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
}


static LRESULT
3782
TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3783
{
3784
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3785

3786
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3787
    if (nIndex == -1)
3788
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3789 3790 3791 3792 3793 3794

    return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
}


static LRESULT
3795
TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3796
{
3797
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3798

3799
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3800
    if (nIndex == -1)
3801
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3802 3803 3804 3805 3806 3807

    return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
}


static LRESULT
3808
TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3809
{
3810
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3811

3812
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3813
    if (nIndex == -1)
3814
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3815 3816 3817 3818 3819 3820

    return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
}


static LRESULT
3821
TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3822
{
3823
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3824

3825
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3826
    if (nIndex == -1)
3827
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3828 3829 3830 3831 3832 3833

    return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
}


static LRESULT
3834
TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3835
{
3836
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3837

3838
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3839
    if (nIndex == -1)
3840
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3841 3842 3843 3844 3845

    return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
}


3846
static LRESULT
3847
TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3848 3849
{
    TBADDBITMAP tbab;
3850
    tbab.hInst = hInstance;
3851
    tbab.nID = wParam;
3852

3853
    TRACE("hwnd = %p, hInst = %p, nID = %Iu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3854

3855
    return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3856 3857 3858 3859
}


static LRESULT
3860
TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3861 3862 3863 3864 3865
{
    WCHAR wszAccel[] = {'&',wAccel,0};
    int i;
    
    TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3866
        infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3867 3868 3869 3870 3871 3872 3873
    
    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
        TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
        if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
            !(btnPtr->fsState & TBSTATE_HIDDEN))
        {
3874
            int iLen = lstrlenW(wszAccel);
3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886
            LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
            
            if (!lpszStr)
                continue;

            while (*lpszStr)
            {
                if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
                {
                    lpszStr += 2;
                    continue;
                }
3887
                if (!wcsnicmp(lpszStr, wszAccel, iLen))
3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900
                {
                    *pIDButton = btnPtr->idCommand;
                    return TRUE;
                }
                lpszStr++;
            }
        }
    }
    return FALSE;
}


static LRESULT
3901
TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3902 3903
{
    INT nIndex;
3904 3905
    DWORD oldState;
    TBUTTON_INFO *btnPtr;
3906

3907
    TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3908

3909
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3910 3911 3912
    if (nIndex == -1)
        return FALSE;

3913 3914 3915
    btnPtr = &infoPtr->buttons[nIndex];
    oldState = btnPtr->fsState;

3916
    if (fMark)
3917
        btnPtr->fsState |= TBSTATE_MARKED;
3918
    else
3919 3920 3921
        btnPtr->fsState &= ~TBSTATE_MARKED;

    if(oldState != btnPtr->fsState)
3922
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3923 3924 3925 3926

    return TRUE;
}

Robert Shearman's avatar
Robert Shearman committed
3927 3928

/* fixes up an index of a button affected by a move */
3929
static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
Robert Shearman's avatar
Robert Shearman committed
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948
{
    if (bMoveUp)
    {
        if (*pIndex > nIndex && *pIndex <= nMoveIndex)
            (*pIndex)--;
        else if (*pIndex == nIndex)
            *pIndex = nMoveIndex;
    }
    else
    {
        if (*pIndex >= nMoveIndex && *pIndex < nIndex)
            (*pIndex)++;
        else if (*pIndex == nIndex)
            *pIndex = nMoveIndex;
    }
}


static LRESULT
3949
TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
Robert Shearman's avatar
Robert Shearman committed
3950 3951 3952 3953 3954
{
    INT nIndex;
    INT nCount;
    TBUTTON_INFO button;

3955
    TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
Robert Shearman's avatar
Robert Shearman committed
3956

3957
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
Robert Shearman's avatar
Robert Shearman committed
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989
    if ((nIndex == -1) || (nMoveIndex < 0))
        return FALSE;

    if (nMoveIndex > infoPtr->nNumButtons - 1)
        nMoveIndex = infoPtr->nNumButtons - 1;

    button = infoPtr->buttons[nIndex];

    /* move button right */
    if (nIndex < nMoveIndex)
    {
        nCount = nMoveIndex - nIndex;
        memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
        infoPtr->buttons[nMoveIndex] = button;

        TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
        TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
        TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
        TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
    }
    else if (nIndex > nMoveIndex) /* move button left */
    {
        nCount = nIndex - nMoveIndex;
        memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
        infoPtr->buttons[nMoveIndex] = button;

        TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
        TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
        TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
        TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
    }

3990 3991 3992
    TOOLBAR_LayoutToolbar(infoPtr);
    TOOLBAR_AutoSize(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Robert Shearman's avatar
Robert Shearman committed
3993 3994 3995

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3996 3997 3998


static LRESULT
3999
TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
Alexandre Julliard's avatar
Alexandre Julliard committed
4000 4001
{
    TBUTTON_INFO *btnPtr;
4002
    INT nIndex;
4003
    DWORD oldState;
Alexandre Julliard's avatar
Alexandre Julliard committed
4004

4005
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4006 4007 4008 4009
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
4010
    oldState = btnPtr->fsState;
4011 4012

    if (fPress)
Alexandre Julliard's avatar
Alexandre Julliard committed
4013
	btnPtr->fsState |= TBSTATE_PRESSED;
4014 4015
    else
	btnPtr->fsState &= ~TBSTATE_PRESSED;
Alexandre Julliard's avatar
Alexandre Julliard committed
4016

4017
    if(oldState != btnPtr->fsState)
4018
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4019 4020 4021 4022

    return TRUE;
}

4023 4024
/* FIXME: there might still be some confusion her between number of buttons
 * and number of bitmaps */
4025
static LRESULT
4026
TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
4027 4028 4029
{
    HBITMAP hBitmap;
    int i = 0, nOldButtons = 0, pos = 0;
4030
    int nOldBitmaps, nNewBitmaps = 0;
4031
    HIMAGELIST himlDef = 0;
4032

4033
    TRACE("hInstOld %p nIDOld %Ix hInstNew %p nIDNew %Ix nButtons %x\n",
4034 4035
          lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
          lpReplace->nButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
4036

4037
    if (lpReplace->hInstOld == HINST_COMMCTRL)
4038 4039 4040 4041
    {
        FIXME("changing standard bitmaps not implemented\n");
        return FALSE;
    }
4042
    else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4043
        FIXME("resources not in the current module not implemented\n");
4044

4045
    TRACE("To be replaced hInstOld %p nIDOld %Ix\n", lpReplace->hInstOld, lpReplace->nIDOld);
4046 4047
    for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
        TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4048
        TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4049 4050
        if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
        {
4051
            TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4052 4053 4054 4055
            nOldButtons = tbi->nButtons;
            tbi->nButtons = lpReplace->nButtons;
            tbi->hInst = lpReplace->hInstNew;
            tbi->nID = lpReplace->nIDNew;
4056
            TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4057 4058 4059 4060 4061 4062 4063
            break;
        }
        pos += tbi->nButtons;
    }

    if (nOldButtons == 0)
    {
4064
        WARN("No hinst/bitmap found! hInst %p nID %Ix\n", lpReplace->hInstOld, lpReplace->nIDOld);
4065 4066
        return FALSE;
    }
4067
    
4068 4069 4070 4071 4072 4073 4074 4075
    /* copy the bitmap before adding it as ImageList_AddMasked modifies the
    * bitmap
    */
    if (lpReplace->hInstNew)
        hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
    else
        hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);

4076 4077
    himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
    nOldBitmaps = ImageList_GetImageCount(himlDef);
4078

4079
    /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4080

4081
    for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4082
        ImageList_Remove(himlDef, i);
4083

4084
    if (hBitmap)
4085
    {
4086
       ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4087
       nNewBitmaps = ImageList_GetImageCount(himlDef);
4088
       DeleteObject(hBitmap);
4089
    }
4090

4091 4092 4093 4094 4095
    infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;

    TRACE(" pos %d  %d old bitmaps replaced by %d new ones.\n",
            pos, nOldBitmaps, nNewBitmaps);

4096
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4097 4098
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4099

4100 4101 4102

/* helper for TOOLBAR_SaveRestoreW */
static BOOL
4103
TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
Alexandre Julliard's avatar
Alexandre Julliard committed
4104
{
4105 4106 4107 4108
    NMTBSAVE save;
    INT ret, i;
    BOOL alloced = FALSE;
    HKEY key;
Alexandre Julliard's avatar
Alexandre Julliard committed
4109

4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151
    TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );

    memset( &save, 0, sizeof(save) );
    save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
    save.iItem = -1;
    save.cButtons = infoPtr->nNumButtons;
    save.tbButton.idCommand = -1;
    TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );

    if (!save.pData)
    {
        save.pData = Alloc( save.cbData );
        if (!save.pData) return FALSE;
        alloced = TRUE;
    }
    if (!save.pCurrent) save.pCurrent = save.pData;

    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
        save.iItem = i;
        save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
        save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
        save.tbButton.fsState = infoPtr->buttons[i].fsState;
        save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
        memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
        save.tbButton.dwData = infoPtr->buttons[i].dwData;
        save.tbButton.iString = infoPtr->buttons[i].iString;

        *save.pCurrent++ = save.tbButton.idCommand;

        TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
    }

    ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
    if (ret == ERROR_SUCCESS)
    {
        ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
        RegCloseKey( key );
    }

    if (alloced) Free( save.pData );
    return !ret;
4152
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4153 4154


4155 4156 4157 4158 4159
/* helper for TOOLBAR_Restore */
static void
TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
{
    INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
4160

4161 4162
    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
4163
        free_string( infoPtr->buttons + i );
4164
        TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
Alexandre Julliard's avatar
Alexandre Julliard committed
4165 4166
    }

4167 4168 4169 4170 4171 4172 4173 4174
    Free(infoPtr->buttons);
    infoPtr->buttons = NULL;
    infoPtr->nNumButtons = 0;
}


/* helper for TOOLBAR_SaveRestoreW */
static BOOL
4175
TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4176 4177 4178 4179 4180 4181 4182
{
    LONG res;
    HKEY hkey = NULL;
    BOOL ret = FALSE;
    DWORD dwType;
    DWORD dwSize = 0;
    NMTBRESTORE nmtbr;
4183
    NMHDR hdr;
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199

    /* restore toolbar information */
    TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
        debugstr_w(lpSave->pszValueName));

    memset(&nmtbr, 0, sizeof(nmtbr));

    res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
        KEY_QUERY_VALUE, &hkey);
    if (!res)
        res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
            NULL, &dwSize);
    if (!res && dwType != REG_BINARY)
        res = ERROR_FILE_NOT_FOUND;
    if (!res)
    {
4200
        nmtbr.pData = Alloc(dwSize);
4201
        nmtbr.cbData = dwSize;
4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215
        if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
    }
    if (!res)
        res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
            (LPBYTE)nmtbr.pData, &dwSize);
    if (!res)
    {
        nmtbr.pCurrent = nmtbr.pData;
        nmtbr.iItem = -1;
        nmtbr.cbBytesPerRecord = sizeof(DWORD);
        nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;

        if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
        {
4216
            INT i, count = nmtbr.cButtons;
4217 4218 4219 4220 4221

            /* remove all existing buttons as this function is designed to
             * restore the toolbar to a previously saved state */
            TOOLBAR_DeleteAllButtons(infoPtr);

4222
            for (i = 0; i < count; i++)
4223 4224 4225 4226 4227
            {
                nmtbr.iItem = i;
                nmtbr.tbButton.iBitmap = -1;
                nmtbr.tbButton.fsState = 0;
                nmtbr.tbButton.fsStyle = 0;
4228 4229
                nmtbr.tbButton.dwData = 0;
                nmtbr.tbButton.iString = 0;
4230 4231

                if (*nmtbr.pCurrent & 0x80000000)
4232 4233
                {
                    /* separator */
4234
                    nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4235
                    nmtbr.tbButton.idCommand = 0;
4236
                    nmtbr.tbButton.fsStyle = BTNS_SEP;
4237 4238
                    if (*nmtbr.pCurrent != (DWORD)-1)
                        nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4239 4240 4241 4242 4243 4244 4245 4246
                }
                else
                    nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;

                nmtbr.pCurrent++;
                
                TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);

4247
                /* All returned ptrs and -1 are ignored */
4248
                if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4249
                    nmtbr.tbButton.iString = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4250

4251
                TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4252
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
4253

4254 4255
            TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
            for (i = 0; ; i++)
4256
            {
4257 4258 4259 4260 4261 4262 4263 4264
                NMTOOLBARW tb;
                TBBUTTONINFOW bi;
                WCHAR buf[128];
                UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
                INT idx;

                memset( &tb, 0, sizeof(tb) );
                tb.iItem = i;
4265
                tb.cchText = ARRAY_SIZE(buf);
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
                tb.pszText = buf;

                /* Use the same struct for both A and W versions since the layout is the same. */
                if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
                    break;

                idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
                if (idx == -1) continue;

                /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
                   be an index or a ptr.  Either way it is simply copied.  There is no api to change
                   the string index, so we set it manually.  The other properties can be set with SetButtonInfo. */
                free_string( infoPtr->buttons + idx );
                infoPtr->buttons[idx].iString = tb.tbButton.iString;

                memset( &bi, 0, sizeof(bi) );
                bi.cbSize = sizeof(bi);
                bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
                bi.iImage = tb.tbButton.iBitmap;
                bi.fsState = tb.tbButton.fsState;
                bi.fsStyle = tb.tbButton.fsStyle;
                bi.lParam = tb.tbButton.dwData;

                TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4290
            }
4291
            TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4292 4293 4294 4295 4296 4297

            /* remove all uninitialised buttons
             * note: loop backwards to avoid having to fixup i on a
             * delete */
            for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
                if (infoPtr->buttons[i].iBitmap == -1)
4298
                    TOOLBAR_DeleteButton(infoPtr, i);
4299 4300 4301 4302

            /* only indicate success if at least one button survived */
            if (infoPtr->nNumButtons > 0) ret = TRUE;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
4303
    }
4304
    Free (nmtbr.pData);
4305
    RegCloseKey(hkey);
Alexandre Julliard's avatar
Alexandre Julliard committed
4306

4307
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
4308 4309 4310
}


4311
static LRESULT
4312
TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4313
{
4314
    if (lpSave == NULL) return 0;
4315

4316
    if (wParam)
4317
        return TOOLBAR_Save(infoPtr, lpSave);
4318 4319 4320
    else
        return TOOLBAR_Restore(infoPtr, lpSave);
}
4321 4322


4323
static LRESULT
4324
TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4325
{
4326
    LPWSTR pszValueName = 0, pszSubKey = 0;
4327
    TBSAVEPARAMSW SaveW;
4328
    LRESULT result = 0;
4329
    int len;
4330

4331
    if (lpSave == NULL) return 0;
4332

4333
    len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4334
    pszSubKey = Alloc(len * sizeof(WCHAR));
4335
    if (!pszSubKey) goto exit;
4336
    MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4337

4338
    len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4339
    pszValueName = Alloc(len * sizeof(WCHAR));
4340 4341 4342 4343 4344 4345
    if (!pszValueName) goto exit;
    MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);

    SaveW.pszValueName = pszValueName;
    SaveW.pszSubKey = pszSubKey;
    SaveW.hkr = lpSave->hkr;
4346
    result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4347 4348

exit:
4349 4350
    Free (pszValueName);
    Free (pszSubKey);
4351

4352
    return result;
4353 4354 4355 4356
}


static LRESULT
4357
TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4358 4359 4360
{
    BOOL bOldAnchor = infoPtr->bAnchor;

4361
    TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4362

4363
    infoPtr->bAnchor = bAnchor;
4364

4365
    /* Native does not remove the hot effect from an already hot button */
4366

4367 4368
    return (LRESULT)bOldAnchor;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4369

Alexandre Julliard's avatar
Alexandre Julliard committed
4370

Alexandre Julliard's avatar
Alexandre Julliard committed
4371
static LRESULT
4372
TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4373
{
4374
    HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4375 4376
    short width = (short)LOWORD(lParam);
    short height = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4377

4378
    TRACE("hwnd %p, wParam %Id, size %d x %d\n", infoPtr->hwndSelf, wParam, width, height);
Robert Shearman's avatar
Robert Shearman committed
4379 4380

    if (wParam != 0)
4381
        FIXME("wParam is %Id. Perhaps image list index?\n", wParam);
Robert Shearman's avatar
Robert Shearman committed
4382

4383 4384 4385 4386 4387
    /* 0 width or height is changed to 1 */
    if (width == 0)
        width = 1;
    if (height == 0)
        height = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
4388

4389
    if (infoPtr->nNumButtons > 0)
4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400
        TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
              infoPtr->nNumButtons,
              infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);

    if (width < -1 || height < -1)
    {
        /* Windows destroys the imagelist and seems to actually use negative
         * values to compute button sizes */
        FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
        return FALSE;
    }
4401

4402 4403 4404 4405 4406
    /* width or height of -1 means no change */
    if (width != -1)
        infoPtr->nBitmapWidth = width;
    if (height != -1)
        infoPtr->nBitmapHeight = height;
4407

Robert Shearman's avatar
Robert Shearman committed
4408 4409 4410 4411 4412
    if ((himlDef == infoPtr->himlInt) &&
        (ImageList_GetImageCount(infoPtr->himlInt) == 0))
    {
        ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
            infoPtr->nBitmapHeight);
4413
    }
4414

4415
    TOOLBAR_CalcToolbar(infoPtr);
4416
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4417 4418 4419 4420
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4421
static LRESULT
4422 4423
TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
                       const TBBUTTONINFOW *lptbbi, BOOL isW)
4424 4425 4426
{
    TBUTTON_INFO *btnPtr;
    INT nIndex;
Robert Shearman's avatar
Robert Shearman committed
4427
    RECT oldBtnRect;
4428 4429 4430 4431 4432 4433

    if (lptbbi == NULL)
	return FALSE;
    if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
	return FALSE;

4434
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4435 4436 4437 4438 4439 4440 4441 4442 4443 4444
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
    if (lptbbi->dwMask & TBIF_COMMAND)
	btnPtr->idCommand = lptbbi->idCommand;
    if (lptbbi->dwMask & TBIF_IMAGE)
	btnPtr->iBitmap = lptbbi->iImage;
    if (lptbbi->dwMask & TBIF_LPARAM)
	btnPtr->dwData = lptbbi->lParam;
Robert Shearman's avatar
Robert Shearman committed
4445 4446
    if (lptbbi->dwMask & TBIF_SIZE)
	btnPtr->cx = lptbbi->cx;
4447 4448 4449 4450 4451
    if (lptbbi->dwMask & TBIF_STATE)
	btnPtr->fsState = lptbbi->fsState;
    if (lptbbi->dwMask & TBIF_STYLE)
	btnPtr->fsStyle = lptbbi->fsStyle;

4452 4453
    if (lptbbi->dwMask & TBIF_TEXT)
        set_stringT( btnPtr, lptbbi->pszText, isW );
Robert Shearman's avatar
Robert Shearman committed
4454 4455 4456

    /* save the button rect to see if we need to redraw the whole toolbar */
    oldBtnRect = btnPtr->rect;
4457
    TOOLBAR_LayoutToolbar(infoPtr);
Robert Shearman's avatar
Robert Shearman committed
4458 4459

    if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4460
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Robert Shearman's avatar
Robert Shearman committed
4461
    else
4462
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Robert Shearman's avatar
Robert Shearman committed
4463

4464 4465
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4466 4467 4468


static LRESULT
4469
TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4470
{
4471
    INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4472
    int top = default_top_margin(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
4473

4474
    if ((cx < 0) || (cy < 0))
4475
    {
4476
        ERR("invalid parameter %#Ix\n", lParam);
4477
        return FALSE;
4478
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4479

4480
    TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4481

4482
    /* The documentation claims you can only change the button size before
4483 4484
     * any button has been added. But this is wrong.
     * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4485 4486 4487
     * it to the toolbar, and it checks that the return value is nonzero - mjm
     * Further testing shows that we must actually perform the change too.
     */
4488 4489 4490
    /*
     * The documentation also does not mention that if 0 is supplied for
     * either size, the system changes it to the default of 24 wide and
4491
     * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4492
     */
4493
    if (cx == 0) cx = 24;
4494
    if (cy == 0) cy = 22;
4495

4496
    cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4497
    cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4498

4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
    if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
        top != infoPtr->iTopMargin)
    {
        infoPtr->nButtonWidth = cx;
        infoPtr->nButtonHeight = cy;
        infoPtr->iTopMargin = top;

        TOOLBAR_LayoutToolbar( infoPtr );
        InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4509 4510 4511 4512
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4513
static LRESULT
4514
TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4515
{
4516
    /* if setting to current values, ignore */
4517 4518
    if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
	(infoPtr->cxMax == (short)HIWORD(lParam))) {
4519 4520
	TRACE("matches current width, min=%d, max=%d, no recalc\n",
	      infoPtr->cxMin, infoPtr->cxMax);
4521
	return TRUE;
4522
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4523

4524
    /* save new values */
4525 4526
    infoPtr->cxMin = (short)LOWORD(lParam);
    infoPtr->cxMax = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4527

4528 4529 4530
    /* otherwise we need to recalc the toolbar and in some cases
       recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
       which doesn't actually draw - GA). */
4531
    TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4532 4533
	infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);

4534
    TOOLBAR_CalcToolbar (infoPtr);
4535

4536
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4537

Alexandre Julliard's avatar
Alexandre Julliard committed
4538 4539
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4540 4541 4542


static LRESULT
4543
TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
Alexandre Julliard's avatar
Alexandre Julliard committed
4544 4545 4546 4547
{
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;

4548
    infoPtr->buttons[nIndex].idCommand = nId;
Alexandre Julliard's avatar
Alexandre Julliard committed
4549

Alexandre Julliard's avatar
Alexandre Julliard committed
4550 4551
    if (infoPtr->hwndToolTip) {

4552
	FIXME("change tool tip\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
4553 4554 4555

    }

Alexandre Julliard's avatar
Alexandre Julliard committed
4556 4557 4558 4559
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4560
static LRESULT
4561
TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4562 4563
{
    HIMAGELIST himlTemp;
4564
    INT id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4565

4566 4567
    if (infoPtr->iVersion >= 5)
        id = wParam;
4568

4569 4570
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis, 
        &infoPtr->cimlDis, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4571 4572 4573

    /* FIXME: redraw ? */

4574
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4575 4576 4577
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4578
static LRESULT
4579
TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD flags)
Alexandre Julliard's avatar
Alexandre Julliard committed
4580
{
4581
    DWORD old_flags;
Alexandre Julliard's avatar
Alexandre Julliard committed
4582

4583
    TRACE("hwnd = %p, mask = %#lx, flags %#lx\n", infoPtr->hwndSelf, mask, flags);
4584

4585 4586
    old_flags = infoPtr->dwDTFlags;
    infoPtr->dwDTFlags = (old_flags & ~mask) | (flags & mask);
Alexandre Julliard's avatar
Alexandre Julliard committed
4587

4588
    return (LRESULT)old_flags;
Alexandre Julliard's avatar
Alexandre Julliard committed
4589
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4590

Robert Shearman's avatar
Robert Shearman committed
4591 4592 4593 4594 4595 4596
/* This function differs a bit from what MSDN says it does:
 * 1. lParam contains extended style flags to OR with current style
 *  (MSDN isn't clear on the OR bit)
 * 2. wParam appears to contain extended style flags to be reset
 *  (MSDN says that this parameter is reserved)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
4597
static LRESULT
4598
TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
Alexandre Julliard's avatar
Alexandre Julliard committed
4599
{
4600
    DWORD old_style = infoPtr->dwExStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
4601

4602
    TRACE("mask %#lx, style %#lx\n", mask, style);
Alexandre Julliard's avatar
Alexandre Julliard committed
4603

4604 4605 4606 4607
    if (mask)
	infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
    else
	infoPtr->dwExStyle = style;
4608 4609

    if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4610
        FIXME("Unknown Toolbar Extended Style %#lx. Please report.\n", (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4611

4612
    if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4613
        TOOLBAR_CalcToolbar(infoPtr);
4614
    else
4615
        TOOLBAR_LayoutToolbar(infoPtr);
4616

4617 4618
    TOOLBAR_AutoSize(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4619

4620
    return old_style;
Alexandre Julliard's avatar
Alexandre Julliard committed
4621 4622 4623 4624
}


static LRESULT
4625
TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4626 4627
{
    HIMAGELIST himlTemp;
4628
    INT id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4629

4630 4631
    if (infoPtr->iVersion >= 5)
        id = wParam;
Guy Albertelli's avatar
Guy Albertelli committed
4632

4633
    TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4634

4635 4636
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot, 
        &infoPtr->cimlHot, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4637 4638 4639

    /* FIXME: redraw ? */

4640
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4641 4642 4643
}


4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656
/* Makes previous hot button no longer hot, makes the specified
 * button hot and sends appropriate notifications. dwReason is one or
 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
 * NOTE 1: this function does not validate nHit
 * NOTE 2: the name of this function is completely made up and
 * not based on any documentation from Microsoft. */
static void
TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
{
    if (infoPtr->nHotItem != nHit)
    {
        NMTBHOTITEM nmhotitem;
        TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4657

4658
        nmhotitem.dwFlags = dwReason;
4659 4660 4661
        if(infoPtr->nHotItem >= 0)
        {
            oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4662
            nmhotitem.idOld = oldBtnPtr->idCommand;
4663
        }
4664
        else
4665
        {
4666
            nmhotitem.dwFlags |= HICF_ENTERING;
4667 4668
            nmhotitem.idOld = 0;
        }
4669 4670

        if (nHit >= 0)
4671
        {
4672
            btnPtr = &infoPtr->buttons[nHit];
4673 4674
            nmhotitem.idNew = btnPtr->idCommand;
        }
4675
	else
4676
	{
4677
	    nmhotitem.dwFlags |= HICF_LEAVING;
4678 4679
	    nmhotitem.idNew = 0;
	}
4680

4681 4682 4683
	/* now change the hot and invalidate the old and new buttons - if the
	 * parent agrees */
	if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4684
	{
4685 4686 4687 4688
            if (oldBtnPtr) {
                oldBtnPtr->bHot = FALSE;
                InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
            }
4689 4690 4691 4692 4693 4694 4695 4696
            /* setting disabled buttons as hot fails even if the notify contains the button id */
            if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
                btnPtr->bHot = TRUE;
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
                infoPtr->nHotItem = nHit;
            }
            else
                infoPtr->nHotItem = -1;            
4697 4698 4699 4700
        }
    }
}

4701
static LRESULT
4702
TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4703 4704 4705
{
    INT nOldHotItem = infoPtr->nHotItem;

4706
    TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4707

4708
    if (nHotItem >= infoPtr->nNumButtons)
4709 4710
        return infoPtr->nHotItem;
    
4711 4712
    if (nHotItem < 0)
        nHotItem = -1;
4713 4714 4715

    /* NOTE: an application can still remove the hot item even if anchor
     * highlighting is enabled */
4716

4717
    TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4718 4719

    if (nOldHotItem < 0)
4720
        return -1;
4721 4722 4723

    return (LRESULT)nOldHotItem;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4724 4725 4726


static LRESULT
4727
TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4728 4729
{
    HIMAGELIST himlTemp;
4730
    INT oldButtonWidth = infoPtr->nButtonWidth;
4731 4732
    INT oldBitmapWidth = infoPtr->nBitmapWidth;
    INT oldBitmapHeight = infoPtr->nBitmapHeight;
4733
    INT i, id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4734

4735 4736
    if (infoPtr->iVersion >= 5)
        id = wParam;
Guy Albertelli's avatar
Guy Albertelli committed
4737

4738 4739
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef, 
        &infoPtr->cimlDef, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4740

4741 4742 4743
    infoPtr->nNumBitmaps = 0;
    for (i = 0; i < infoPtr->cimlDef; i++)
        infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4744

4745 4746 4747
    if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
            &infoPtr->nBitmapHeight))
    {
4748 4749
        infoPtr->nBitmapWidth = 1;
        infoPtr->nBitmapHeight = 1;
4750
    }
4751 4752
    if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
    {
4753
        TOOLBAR_CalcToolbar(infoPtr);
4754
        if (infoPtr->nButtonWidth < oldButtonWidth)
4755
            TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4756
    }
4757 4758

    TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4759
	  infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4760 4761
	  infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);

4762
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4763

4764
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4765
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4766

Alexandre Julliard's avatar
Alexandre Julliard committed
4767 4768

static LRESULT
4769
TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
Alexandre Julliard's avatar
Alexandre Julliard committed
4770
{
4771
    infoPtr->nIndent = nIndent;
4772

4773
    TRACE("\n");
4774

4775
    /* process only on indent changing */
4776
    if(infoPtr->nIndent != nIndent)
4777
    {
4778
        infoPtr->nIndent = nIndent;
4779 4780
        TOOLBAR_CalcToolbar (infoPtr);
        InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4781
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4782 4783 4784 4785 4786

    return TRUE;
}


4787
static LRESULT
4788
TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4789
{
4790
    TRACE("hwnd = %p, lptbim = { %d, %#lx}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4791 4792 4793

    if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
    {
4794
        FIXME("Unrecognized flag(s): %#lx\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4795 4796 4797 4798 4799 4800 4801 4802 4803
        return 0;
    }

    if ((lptbim->iButton == -1) || 
        ((lptbim->iButton < infoPtr->nNumButtons) &&
         (lptbim->iButton >= 0)))
    {
        infoPtr->tbim = *lptbim;
        /* FIXME: don't need to update entire toolbar */
4804
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4805 4806 4807 4808 4809 4810
    }
    else
        ERR("Invalid button index %d\n", lptbim->iButton);

    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4811 4812 4813


static LRESULT
4814
TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
Alexandre Julliard's avatar
Alexandre Julliard committed
4815
{
4816
    infoPtr->clrInsertMark = clr;
Alexandre Julliard's avatar
Alexandre Julliard committed
4817

4818
    /* FIXME: don't need to update entire toolbar */
4819
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4820 4821 4822 4823 4824

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4825
static LRESULT
4826
TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
Alexandre Julliard's avatar
Alexandre Julliard committed
4827
{
4828
    infoPtr->nMaxTextRows = nMaxRows;
Alexandre Julliard's avatar
Alexandre Julliard committed
4829

4830
    TOOLBAR_CalcToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
4831 4832 4833 4834
    return TRUE;
}


4835 4836 4837 4838 4839 4840 4841 4842 4843
/* MSDN gives slightly wrong info on padding.
 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
 * 2. It is not used to create a blank area between the edge of the button
 *    and the text or image if TBSTYLE_LIST is set. It is used to control
 *    the gap between the image and text. 
 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used 
 *    to control the bottom and right borders [with the border being
 *    szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
 *    is shared evenly on both sides of the button.
4844
 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4845
 */
4846
static LRESULT
4847
TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4848 4849 4850 4851
{
    DWORD  oldPad;

    oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4852 4853
    infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
    infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4854
    TRACE("cx %ld, cy %ld\n", infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4855 4856
    return (LRESULT) oldPad;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4857 4858 4859


static LRESULT
4860
TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
Alexandre Julliard's avatar
Alexandre Julliard committed
4861
{
4862
    HWND hwndOldNotify;
Alexandre Julliard's avatar
Alexandre Julliard committed
4863

4864 4865
    TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
4866
    hwndOldNotify = infoPtr->hwndNotify;
4867
    infoPtr->hwndNotify = hParent;
Alexandre Julliard's avatar
Alexandre Julliard committed
4868

4869
    return (LRESULT)hwndOldNotify;
Alexandre Julliard's avatar
Alexandre Julliard committed
4870 4871 4872 4873
}


static LRESULT
4874
TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
Alexandre Julliard's avatar
Alexandre Julliard committed
4875
{
4876 4877
    int rows = LOWORD(wParam);
    BOOL bLarger = HIWORD(wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4878

4879
    TRACE("\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
4880

4881
    TRACE("Setting rows to %d (%d)\n", rows, bLarger);
Alexandre Julliard's avatar
Alexandre Julliard committed
4882

4883
    if(infoPtr->nRows != rows)
4884
    {
4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899
        TBUTTON_INFO *btnPtr = infoPtr->buttons;
        int curColumn = 0; /* Current column                      */
        int curRow    = 0; /* Current row                         */
        int hidden    = 0; /* Number of hidden buttons */
        int seps      = 0; /* Number of separators     */
        int idealWrap = 0; /* Ideal wrap point         */
        int i;
        BOOL wrap;

        /*
           Calculate new size and wrap points - Under windows, setrows will
           change the dimensions if needed to show the number of requested
           rows (if CCS_NORESIZE is set), or will take up the whole window
           (if no CCS_NORESIZE).

4900
           Basic algorithm - If N buttons, and y rows requested, each row
4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919
           contains N/y buttons.

           FIXME: Handling of separators not obvious from testing results
           FIXME: Take width of window into account?
         */

        /* Loop through the buttons one by one counting key items  */
        for (i = 0; i < infoPtr->nNumButtons; i++ )
        {
            btnPtr[i].fsState &= ~TBSTATE_WRAP;
            if (btnPtr[i].fsState & TBSTATE_HIDDEN)
                hidden++;
            else if (btnPtr[i].fsStyle & BTNS_SEP)
                seps++;
        }

        /* FIXME: Separators make this quite complex */
        if (seps) FIXME("Separators unhandled\n");

Austin English's avatar
Austin English committed
4920
        /* Round up so more per line, i.e., less rows */
4921
        idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962

        /* Calculate ideal wrap point if we are allowed to grow, but cannot
           achieve the requested number of rows. */
        if (bLarger && idealWrap > 1)
        {
            int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
            int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);

            if (resRows < rows && moreRows > rows)
            {
                idealWrap--;
                TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
            }
        }

        curColumn = curRow = 0;
        wrap = FALSE;
        TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
              infoPtr->nNumButtons, hidden, rows);

        for (i = 0; i < infoPtr->nNumButtons; i++ )
        {
            if (btnPtr[i].fsState & TBSTATE_HIDDEN)
                continue;

            /* Step on, wrap if necessary or flag next to wrap */
            if (!wrap) {
                curColumn++;
            } else {
                wrap = FALSE;
                curColumn = 1;
                curRow++;
            }

            if (curColumn > (idealWrap-1)) {
                wrap = TRUE;
                btnPtr[i].fsState |= TBSTATE_WRAP;
            }
        }

        TRACE("Result - %d rows\n", curRow + 1);
4963

4964
        /* recalculate toolbar */
4965
        TOOLBAR_CalcToolbar (infoPtr);
4966

4967 4968 4969 4970
        /* Resize if necessary (Only if NORESIZE is set - odd, but basically
           if NORESIZE is NOT set, then the toolbar will always be resized to
           take up the whole window. With it set, sizing needs to be manual. */
        if (infoPtr->dwStyle & CCS_NORESIZE) {
4971
            SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4972 4973
                         infoPtr->rcBound.right - infoPtr->rcBound.left,
                         infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4974
                         SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4975 4976
        }

4977
        /* repaint toolbar */
4978
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4979
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4980 4981 4982 4983 4984 4985 4986 4987 4988

    /* return bounding rectangle */
    if (lprc) {
	lprc->left   = infoPtr->rcBound.left;
	lprc->right  = infoPtr->rcBound.right;
	lprc->top    = infoPtr->rcBound.top;
	lprc->bottom = infoPtr->rcBound.bottom;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
4989 4990 4991
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
4992 4993

static LRESULT
4994
TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4995 4996
{
    TBUTTON_INFO *btnPtr;
4997
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
4998

4999
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
5000 5001 5002 5003 5004
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];

5005 5006 5007
    /* if hidden state has changed the invalidate entire window and recalc */
    if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
	btnPtr->fsState = LOWORD(lParam);
5008 5009
	TOOLBAR_CalcToolbar (infoPtr);
	InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
5010 5011 5012
	return TRUE;
    }

5013 5014 5015 5016
    /* process state changing if current state doesn't match new state */
    if(btnPtr->fsState != LOWORD(lParam))
    {
        btnPtr->fsState = LOWORD(lParam);
5017
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5018
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5019 5020 5021 5022

    return TRUE;
}

5023 5024 5025 5026 5027 5028 5029
static inline void unwrap(TOOLBAR_INFO *info)
{
    int i;

    for (i = 0; i < info->nNumButtons; i++)
	info->buttons[i].fsState &= ~TBSTATE_WRAP;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5030 5031

static LRESULT
5032
TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
Alexandre Julliard's avatar
Alexandre Julliard committed
5033
{
5034 5035
    DWORD dwOldStyle = infoPtr->dwStyle;

5036
    TRACE("new style %#lx\n", style);
5037 5038 5039 5040 5041 5042

    if (style & TBSTYLE_LIST)
        infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
    else
        infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;

5043
    infoPtr->dwStyle = style;
5044 5045
    TOOLBAR_CheckStyle(infoPtr);

5046
    if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5047 5048 5049
    {
        if (dwOldStyle & TBSTYLE_WRAPABLE)
            unwrap(infoPtr);
5050
        TOOLBAR_CalcToolbar(infoPtr);
5051
    }
5052
    else if ((dwOldStyle ^ style) & CCS_VERT)
5053 5054 5055 5056 5057 5058 5059 5060 5061
        TOOLBAR_LayoutToolbar(infoPtr);

    /* only resize if one of the CCS_* styles was changed */
    if ((dwOldStyle ^ style) & COMMON_STYLES)
    {
        TOOLBAR_AutoSize(infoPtr);
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
    }

5062
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
5063 5064 5065
}


5066
static inline LRESULT
5067
TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
Alexandre Julliard's avatar
Alexandre Julliard committed
5068
{
5069
    TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5070

5071
    infoPtr->hwndToolTip = hwndTooltip;
Alexandre Julliard's avatar
Alexandre Julliard committed
5072 5073 5074 5075
    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5076
static LRESULT
5077
TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5078
{
5079
    BOOL bTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
5080

Robert Shearman's avatar
Robert Shearman committed
5081
    TRACE("%s hwnd=%p\n",
5082
	   ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
5083

Alexandre Julliard's avatar
Alexandre Julliard committed
5084
    bTemp = infoPtr->bUnicode;
5085
    infoPtr->bUnicode = (BOOL)wParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
5086 5087

    return bTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
5088
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5089 5090


5091
static LRESULT
5092
TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5093
{
5094 5095
    lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
	                       comctl32_color.clrBtnHighlight :
5096
                               infoPtr->clrBtnHighlight;
5097
    lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5098
	                   comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5099 5100 5101 5102 5103
    return 1;
}


static LRESULT
5104
TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5105
{
5106
    TRACE("new colors Hl=%#lx Shd=%#lx, old colors Hl=%#lx Shd=%#lx\n",
5107 5108 5109 5110 5111
	  lParam->clrBtnHighlight, lParam->clrBtnShadow,
	  infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);

    infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
    infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5112
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5113 5114 5115 5116
    return 0;
}


5117
static LRESULT
5118
TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5119 5120 5121 5122 5123
{
    INT iOldVersion = infoPtr->iVersion;

    infoPtr->iVersion = iVersion;

5124
    if (infoPtr->iVersion >= 5)
5125
        TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5126

5127 5128 5129
    return iOldVersion;
}

Robert Shearman's avatar
Robert Shearman committed
5130 5131

static LRESULT
5132
TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
Robert Shearman's avatar
Robert Shearman committed
5133 5134 5135 5136 5137
{
    WORD iString = HIWORD(wParam);
    WORD buffersize = LOWORD(wParam);
    LRESULT ret = -1;

5138
    TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
Robert Shearman's avatar
Robert Shearman committed
5139 5140 5141 5142

    if (iString < infoPtr->nNumStrings)
    {
        ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5143
        ret--;
Robert Shearman's avatar
Robert Shearman committed
5144 5145 5146 5147

        TRACE("returning %s\n", debugstr_a(str));
    }
    else
5148
        WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
Robert Shearman's avatar
Robert Shearman committed
5149 5150 5151 5152 5153 5154

    return ret;
}


static LRESULT
5155
TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
Robert Shearman's avatar
Robert Shearman committed
5156 5157 5158 5159 5160
{
    WORD iString = HIWORD(wParam);
    WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
    LRESULT ret = -1;

5161
    TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
Robert Shearman's avatar
Robert Shearman committed
5162 5163 5164

    if (iString < infoPtr->nNumStrings)
    {
5165
        len = min(len, lstrlenW(infoPtr->strings[iString]));
Robert Shearman's avatar
Robert Shearman committed
5166
        ret = (len+1)*sizeof(WCHAR);
5167 5168 5169 5170 5171
        if (str)
        {
            memcpy(str, infoPtr->strings[iString], ret);
            str[len] = '\0';
        }
5172
        ret = len;
Robert Shearman's avatar
Robert Shearman committed
5173 5174 5175 5176

        TRACE("returning %s\n", debugstr_w(str));
    }
    else
5177
        WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
Robert Shearman's avatar
Robert Shearman committed
5178 5179 5180 5181

    return ret;
}

5182
static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5183
{
5184
    SIZE * pSize = (SIZE*)lParam;
5185
    FIXME("hwnd=%p, wParam=%Ix, size.cx=%ld, size.cy=%ld stub\n", hwnd, wParam, pSize->cx, pSize->cy);
5186 5187
    return 0;
}
5188

5189 5190 5191
/* This is an extended version of the TB_SETHOTITEM message. It allows the
 * caller to specify a reason why the hot item changed (rather than just the
 * HICF_OTHER that TB_SETHOTITEM sends). */
5192
static LRESULT
5193
TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5194 5195
{
    INT nOldHotItem = infoPtr->nHotItem;
5196

5197
    TRACE("old item %d, new item %d, flags %Ix\n", nOldHotItem, nHotItem, lParam);
5198

5199 5200
    if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
        nHotItem = -1;
5201

5202 5203 5204
    /* NOTE: an application can still remove the hot item even if anchor
     * highlighting is enabled */

5205
    TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5206

5207
    return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5208 5209
}

5210 5211 5212
/* Sets the toolbar global iListGap parameter which controls the amount of
 * spacing between the image and the text of buttons for TBSTYLE_LIST
 * toolbars. */
5213
static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5214
{
5215
    TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5216
    
5217
    infoPtr->iListGap = iListGap;
5218

5219
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5220

5221 5222
    return 0;
}
5223

5224 5225
/* Returns the number of maximum number of image lists associated with the
 * various states. */
5226
static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
Robert Shearman's avatar
Robert Shearman committed
5227
{
5228
    TRACE("hwnd=%p\n", infoPtr->hwndSelf);
Robert Shearman's avatar
Robert Shearman committed
5229 5230 5231 5232

    return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
}

5233
static LRESULT
5234
TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247
{
    LPSIZE lpsize = (LPSIZE)lParam;

    if (lpsize == NULL)
	return FALSE;

    /*
     * Testing shows the following:
     *   wParam    = 0 adjust cx value
     *             = 1 set cy value to max size.
     *   lParam    pointer to SIZE structure
     *
     */
5248
    TRACE("wParam %Id, lParam %Ix -> %lx, %lx\n", wParam, lParam, lpsize->cx, lpsize->cy);
5249 5250 5251 5252 5253 5254 5255 5256

    switch(wParam) {
    case 0:
	if (lpsize->cx == -1) {
	    lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
	}
	else if(HIWORD(lpsize->cx)) {
	    RECT rc;
5257
	    HWND hwndParent = GetParent(infoPtr->hwndSelf);
5258

5259
	    GetWindowRect(infoPtr->hwndSelf, &rc);
5260
	    MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5261
            TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272
	    lpsize->cx = max(rc.right-rc.left,
			     infoPtr->rcBound.right - infoPtr->rcBound.left);
	}
	else {
	    lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
	}
	break;
    case 1:
	lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
	break;
    default:
5273
	FIXME("Unknown wParam %Id\n", wParam);
5274 5275
	return 0;
    }
5276
    TRACE("set to -> %lu, %lu\n", lpsize->cx, lpsize->cy);
5277 5278 5279
    return 1;
}

5280 5281
static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
5282
    FIXME("hwnd %p, wParam %Ix, lParam %Ix\n", hwnd, wParam, lParam);
5283 5284 5285 5286 5287

    InvalidateRect(hwnd, NULL, TRUE);
    return 1;
}

5288

Alexandre Julliard's avatar
Alexandre Julliard committed
5289
static LRESULT
5290
TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
5291
{
5292
    TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5293
    LOGFONTW logFont;
Alexandre Julliard's avatar
Alexandre Julliard committed
5294

5295
    TRACE("hwnd = %p, style = %#lx\n", hwnd, lpcs->style);
Robert Shearman's avatar
Robert Shearman committed
5296

5297
    infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5298
    GetClientRect(hwnd, &infoPtr->client_rect);
5299
    infoPtr->bUnicode = infoPtr->hwndNotify && 
5300
        (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5301
    infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
Alexandre Julliard's avatar
Alexandre Julliard committed
5302

5303 5304
    SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
    infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5305
    infoPtr->hTheme = OpenThemeDataForDpi (NULL, themeClass, GetDpiForWindow (hwnd));
5306

5307
    TOOLBAR_CheckStyle (infoPtr);
5308

Alexandre Julliard's avatar
Alexandre Julliard committed
5309 5310 5311 5312 5313
    return 0;
}


static LRESULT
5314
TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
5315
{
5316 5317
    INT i;

Alexandre Julliard's avatar
Alexandre Julliard committed
5318
    /* delete tooltip control */
Alexandre Julliard's avatar
Alexandre Julliard committed
5319
    if (infoPtr->hwndToolTip)
5320
	DestroyWindow (infoPtr->hwndToolTip);
Alexandre Julliard's avatar
Alexandre Julliard committed
5321

5322
    /* delete temporary buffer for tooltip text */
5323
    Free (infoPtr->pszTooltipText);
5324
    Free (infoPtr->bitmaps);            /* bitmaps list */
5325

Alexandre Julliard's avatar
Alexandre Julliard committed
5326
    /* delete button data */
5327
    for (i = 0; i < infoPtr->nNumButtons; i++)
5328
        free_string( infoPtr->buttons + i );
5329
    Free (infoPtr->buttons);
Alexandre Julliard's avatar
Alexandre Julliard committed
5330

Alexandre Julliard's avatar
Alexandre Julliard committed
5331 5332 5333
    /* delete strings */
    if (infoPtr->strings) {
	for (i = 0; i < infoPtr->nNumStrings; i++)
5334
	    Free (infoPtr->strings[i]);
Alexandre Julliard's avatar
Alexandre Julliard committed
5335

5336
	Free (infoPtr->strings);
Alexandre Julliard's avatar
Alexandre Julliard committed
5337 5338
    }

5339 5340 5341
    /* destroy internal image list */
    if (infoPtr->himlInt)
	ImageList_Destroy (infoPtr->himlInt);
Alexandre Julliard's avatar
Alexandre Julliard committed
5342

5343 5344 5345
    TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
    TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
    TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5346

Alexandre Julliard's avatar
Alexandre Julliard committed
5347
    /* delete default font */
5348
    DeleteObject (infoPtr->hDefaultFont);
5349 5350

    CloseThemeData (infoPtr->hTheme);
Alexandre Julliard's avatar
Alexandre Julliard committed
5351

Alexandre Julliard's avatar
Alexandre Julliard committed
5352
    /* free toolbar info data */
5353
    SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5354
    Free (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5355 5356 5357 5358 5359

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5360
static LRESULT
5361
TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5362
{
5363
    NMTBCUSTOMDRAW tbcd;
5364 5365
    INT ret = FALSE;
    DWORD ntfret;
5366
    DWORD dwEraseCustDraw = 0;
5367

5368 5369 5370 5371
    /* the app has told us not to redraw the toolbar */
    if (!infoPtr->bDoRedraw)
        return FALSE;

5372
    if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5373 5374 5375
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
	tbcd.nmcd.hdc = (HDC)wParam;
5376
	ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5377
	dwEraseCustDraw = ntfret & 0xffff;
5378

5379
	/* FIXME: in general the return flags *can* be or'ed together */
5380
	switch (dwEraseCustDraw)
5381 5382 5383 5384 5385 5386
	    {
	    case CDRF_DODEFAULT:
		break;
	    case CDRF_SKIPDEFAULT:
		return TRUE;
	    default:
5387
		FIXME("[%p] response %ld not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5388
		      infoPtr->hwndSelf, ntfret);
5389 5390
	    }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5391

5392
    /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5393 5394
     * to my parent for processing.
     */
5395
    if (infoPtr->hTheme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5396 5397 5398 5399 5400 5401
	POINT pt, ptorig;
	HDC hdc = (HDC)wParam;
	HWND parent;

	pt.x = 0;
	pt.y = 0;
5402 5403
	parent = GetParent(infoPtr->hwndSelf);
	MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5404
	OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5405
	ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5406 5407
	SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
    }
Guy Albertelli's avatar
Guy Albertelli committed
5408
    if (!ret)
5409
	ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
5410

5411
    if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5412 5413 5414
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
	tbcd.nmcd.hdc = (HDC)wParam;
5415
	ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5416 5417
	dwEraseCustDraw = ntfret & 0xffff;
	switch (dwEraseCustDraw)
5418 5419 5420 5421 5422 5423
	    {
	    case CDRF_DODEFAULT:
		break;
	    case CDRF_SKIPDEFAULT:
		return TRUE;
	    default:
5424
		FIXME("[%p] response %ld not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5425
		      infoPtr->hwndSelf, ntfret);
5426 5427
	    }
    }
5428
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
5429 5430 5431
}


5432
static inline LRESULT
5433
TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
Eric Pouech's avatar
Eric Pouech committed
5434
{
5435
    return (LRESULT)infoPtr->hFont;
Eric Pouech's avatar
Eric Pouech committed
5436 5437 5438
}


5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472
static void
TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
{
    INT i;
    INT nNewHotItem = infoPtr->nHotItem;

    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
        /* did we wrap? */
        if ((nNewHotItem + iDirection < 0) ||
            (nNewHotItem + iDirection >= infoPtr->nNumButtons))
        {
            NMTBWRAPHOTITEM nmtbwhi;
            nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
            nmtbwhi.iDirection = iDirection;
            nmtbwhi.dwReason = dwReason;
    
            if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
                return;
        }

        nNewHotItem += iDirection;
        nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;

        if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
            !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
        {
            TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
            break;
        }
    }
}

static LRESULT
5473
TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5474 5475 5476 5477 5478 5479 5480
{
    NMKEY nmkey;

    nmkey.nVKey = (UINT)wParam;
    nmkey.uFlags = HIWORD(lParam);

    if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5481
        return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499

    switch ((UINT)wParam)
    {
    case VK_LEFT:
    case VK_UP:
        TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
        break;
    case VK_RIGHT:
    case VK_DOWN:
        TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
        break;
    case VK_SPACE:
    case VK_RETURN:
        if ((infoPtr->nHotItem >= 0) &&
            (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
        {
            SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
                MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5500
                (LPARAM)infoPtr->hwndSelf);
5501 5502 5503 5504 5505 5506 5507 5508
        }
        break;
    }

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5509
static LRESULT
5510
TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5511
{
5512
    POINT pt;
5513
    BOOL button;
Alexandre Julliard's avatar
Alexandre Julliard committed
5514

5515 5516
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5517
    TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5518

5519
    if (button)
5520
        TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5521
    else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5522
	TOOLBAR_Customize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5523 5524 5525 5526 5527 5528

    return 0;
}


static LRESULT
5529
TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5530 5531
{
    TBUTTON_INFO *btnPtr;
5532 5533
    POINT pt;
    INT   nHit;
5534
    NMTOOLBARA nmtb;
5535
    NMMOUSE nmmouse;
Robert Shearman's avatar
Robert Shearman committed
5536
    BOOL bDragKeyPressed;
5537
    BOOL button;
Robert Shearman's avatar
Robert Shearman committed
5538

5539 5540
    TRACE("\n");

5541
    if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
Robert Shearman's avatar
Robert Shearman committed
5542 5543 5544
        bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
    else
        bDragKeyPressed = (wParam & MK_SHIFT);
Alexandre Julliard's avatar
Alexandre Julliard committed
5545

Alexandre Julliard's avatar
Alexandre Julliard committed
5546
    if (infoPtr->hwndToolTip)
5547
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5548 5549
			    WM_LBUTTONDOWN, wParam, lParam);

5550 5551
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5552
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5553

5554
    if (button)
Robert Shearman's avatar
Robert Shearman committed
5555
    {
5556
        btnPtr = &infoPtr->buttons[nHit];
5557

5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573
        if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
        {
            infoPtr->nButtonDrag = nHit;
            SetCapture (infoPtr->hwndSelf);

            /* If drag cursor has not been loaded, load it.
             * Note: it doesn't need to be freed */
            if (!hCursorDrag)
                hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
            SetCursor(hCursorDrag);
        }
        else
        {
            RECT arrowRect;
            infoPtr->nOldHit = nHit;

5574
            arrowRect = btnPtr->rect;
5575 5576 5577 5578 5579 5580 5581 5582
            arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);

            /* for EX_DRAWDDARROWS style,  click must be in the drop-down arrow rect */
            if ((btnPtr->fsState & TBSTATE_ENABLED) &&
                 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
                  ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
                   ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
                   (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5583
            {
5584
                LRESULT res;
5585

5586 5587 5588 5589 5590 5591
                /* draw in pressed state */
                if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
                    btnPtr->fsState |= TBSTATE_PRESSED;
                else
                    btnPtr->bDropDownPressed = TRUE;
                RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5592

5593 5594 5595 5596
                memset(&nmtb, 0, sizeof(nmtb));
                nmtb.iItem = btnPtr->idCommand;
                nmtb.rcButton = btnPtr->rect;
                res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5597
                TRACE("TBN_DROPDOWN responded with %Id\n", res);
5598

5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625
                if (res != TBDDRET_TREATPRESSED)
                {
                    MSG msg;

                    /* redraw button in unpressed state */
                    if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
                        btnPtr->fsState &= ~TBSTATE_PRESSED;
                    else
                        btnPtr->bDropDownPressed = FALSE;
                    InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);

                    /* find and set hot item */
                    GetCursorPos(&pt);
                    ScreenToClient(infoPtr->hwndSelf, &pt);
                    nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
                    if (!infoPtr->bAnchor || button)
                        TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);

                    /* remove any left mouse button down or double-click messages
                     * so that we can get a toggle effect on the button */
                    while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
                           PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
                        ;

                    return 0;
                }
                /* otherwise drop through and process as pushed */
5626
            }
5627 5628 5629
            infoPtr->bCaptured = TRUE;
            infoPtr->nButtonDown = nHit;
            infoPtr->bDragOutSent = FALSE;
5630

5631
            btnPtr->fsState |= TBSTATE_PRESSED;
5632

5633
            TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5634

5635 5636 5637 5638 5639
            if (btnPtr->fsState & TBSTATE_ENABLED)
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
            UpdateWindow(infoPtr->hwndSelf);
            SetCapture (infoPtr->hwndSelf);
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
5640

5641
        memset(&nmtb, 0, sizeof(nmtb));
5642 5643 5644
        nmtb.iItem = btnPtr->idCommand;
        TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
    }
Robert Shearman's avatar
Robert Shearman committed
5645

5646 5647 5648
    nmmouse.dwHitInfo = nHit;

    /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5649
    if (!button)
5650 5651 5652 5653 5654 5655 5656
        nmmouse.dwItemSpec = -1;
    else
    {
        nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
        nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
    }

5657
    ClientToScreen(infoPtr->hwndSelf, &pt);
5658 5659
    nmmouse.pt = pt;

5660
    if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5661
        return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5662

Alexandre Julliard's avatar
Alexandre Julliard committed
5663 5664 5665 5666
    return 0;
}

static LRESULT
5667
TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5668 5669
{
    TBUTTON_INFO *btnPtr;
5670 5671 5672
    POINT pt;
    INT   nHit;
    INT   nOldIndex = -1;
5673 5674 5675
    NMHDR hdr;
    NMMOUSE nmmouse;
    NMTOOLBARA nmtb;
5676
    BOOL button;
Alexandre Julliard's avatar
Alexandre Julliard committed
5677

Alexandre Julliard's avatar
Alexandre Julliard committed
5678
    if (infoPtr->hwndToolTip)
5679
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5680 5681
			    WM_LBUTTONUP, wParam, lParam);

5682 5683
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5684
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5685

5686
    if (!infoPtr->bAnchor || button)
5687
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5688

Robert Shearman's avatar
Robert Shearman committed
5689 5690 5691 5692 5693 5694 5695 5696 5697
    if (infoPtr->nButtonDrag >= 0) {
        RECT rcClient;
        NMHDR hdr;

        btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
        ReleaseCapture();
        /* reset cursor */
        SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));

5698
        GetClientRect(infoPtr->hwndSelf, &rcClient);
Robert Shearman's avatar
Robert Shearman committed
5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710
        if (PtInRect(&rcClient, pt))
        {
            INT nButton = -1;
            if (nHit >= 0)
                nButton = nHit;
            else if (nHit < -1)
                nButton = -nHit;
            else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
                nButton = -nHit;

            if (nButton == infoPtr->nButtonDrag)
            {
5711
                /* if the button is moved slightly left and we have a
Robert Shearman's avatar
Robert Shearman committed
5712 5713 5714 5715
                 * separator there then remove it */
                if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
                {
                    if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5716
                        TOOLBAR_DeleteButton(infoPtr, nButton - 1);
Robert Shearman's avatar
Robert Shearman committed
5717 5718 5719 5720 5721 5722 5723
                }
                else /* else insert a separator before the dragged button */
                {
                    TBBUTTON tbb;
                    memset(&tbb, 0, sizeof(tbb));
                    tbb.fsStyle = BTNS_SEP;
                    tbb.iString = -1;
5724
                    TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
Robert Shearman's avatar
Robert Shearman committed
5725 5726 5727 5728 5729 5730 5731
                }
            }
            else
            {
                if (nButton == -1)
                {
                    if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5732
                        TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
Robert Shearman's avatar
Robert Shearman committed
5733
                    else
5734
                        TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
Robert Shearman's avatar
Robert Shearman committed
5735 5736
                }
                else
5737
                    TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
Robert Shearman's avatar
Robert Shearman committed
5738 5739 5740 5741 5742
            }
        }
        else
        {
            TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5743
            TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
Robert Shearman's avatar
Robert Shearman committed
5744 5745 5746
        }

        /* button under cursor changed so need to re-set hot item */
5747
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
Robert Shearman's avatar
Robert Shearman committed
5748 5749 5750 5751
        infoPtr->nButtonDrag = -1;

        TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
    }
5752 5753 5754 5755
    else if (infoPtr->nButtonDown >= 0)
    {
        BOOL was_clicked = nHit == infoPtr->nButtonDown;

Alexandre Julliard's avatar
Alexandre Julliard committed
5756
	btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
Alexandre Julliard's avatar
Alexandre Julliard committed
5757
	btnPtr->fsState &= ~TBSTATE_PRESSED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5758

5759 5760
	if (btnPtr->fsStyle & BTNS_CHECK) {
		if (btnPtr->fsStyle & BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
5761
		    nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5762
			nHit);
5763
		    if ((nOldIndex != nHit) &&
Alexandre Julliard's avatar
Alexandre Julliard committed
5764 5765
			(nOldIndex != -1))
			infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5766
		    btnPtr->fsState |= TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5767 5768 5769 5770 5771 5772 5773
		}
		else {
		    if (btnPtr->fsState & TBSTATE_CHECKED)
			btnPtr->fsState &= ~TBSTATE_CHECKED;
		    else
			btnPtr->fsState |= TBSTATE_CHECKED;
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
5774
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
5775

Robert Shearman's avatar
Robert Shearman committed
5776
        if (nOldIndex != -1)
5777
            InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5778

5779 5780 5781
	/*
	 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
	 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5782
	 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5783
	 */
5784 5785
	if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
	    ReleaseCapture ();
5786
	infoPtr->nButtonDown = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
5787

5788
	/* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5789
	TOOLBAR_SendNotify (&hdr, infoPtr,
5790
			NM_RELEASEDCAPTURE);
5791

5792
	memset(&nmtb, 0, sizeof(nmtb));
5793
	nmtb.iItem = btnPtr->idCommand;
5794 5795
	TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
			TBN_ENDDRAG);
5796

5797
	if (was_clicked && btnPtr->fsState & TBSTATE_ENABLED)
5798
	{
5799
	    SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5800
	      MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5801 5802

            /* In case we have just been destroyed... */
5803
            if(!IsWindow(infoPtr->hwndSelf))
5804
                return 0;
5805 5806
        }
    }
5807

5808 5809 5810 5811
    /* !!! Undocumented - toolbar at 4.71 level and above sends
    * NM_CLICK with the NMMOUSE structure. */
    nmmouse.dwHitInfo = nHit;

5812
    if (!button)
5813 5814 5815 5816 5817
        nmmouse.dwItemSpec = -1;
    else
    {
        nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
        nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5818
    }
5819

5820
    ClientToScreen(infoPtr->hwndSelf, &pt);
5821 5822 5823
    nmmouse.pt = pt;

    if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5824
        return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5825

Alexandre Julliard's avatar
Alexandre Julliard committed
5826 5827 5828
    return 0;
}

5829
static LRESULT
5830
TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5831
{
5832
    INT nHit;
5833 5834
    NMMOUSE nmmouse;
    POINT pt;
5835
    BOOL button;
5836

5837 5838
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5839

5840
    nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5841
    nmmouse.dwHitInfo = nHit;
5842

5843
    if (!button) {
5844 5845 5846 5847 5848 5849
	nmmouse.dwItemSpec = -1;
    } else {
	nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
	nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
    }

5850
    ClientToScreen(infoPtr->hwndSelf, &pt);
5851
    nmmouse.pt = pt;
5852

5853
    if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5854
        return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5855 5856 5857 5858

    return 0;
}

5859
static LRESULT
5860
TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5861
{
5862
    NMHDR nmhdr;
5863

5864
    if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5865
        return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5866 5867 5868 5869

    return 0;
}

5870
static LRESULT
5871
TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883
{
    TBUTTON_INFO *btnPtr;

    infoPtr->bCaptured = FALSE;

    if (infoPtr->nButtonDown >= 0)
    {
        btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
       	btnPtr->fsState &= ~TBSTATE_PRESSED;

        infoPtr->nOldHit = -1;

5884
        if (btnPtr->fsState & TBSTATE_ENABLED)
5885
            InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5886 5887 5888 5889
    }
    return 0;
}

5890
static LRESULT
5891
TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5892
{
5893 5894
    /* don't remove hot effects when in anchor highlighting mode or when a
     * drop-down button is pressed */
5895 5896 5897 5898 5899 5900
    if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
    {
        TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
        if (!hotBtnPtr->bDropDownPressed)
            TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
    }
5901

5902 5903 5904
    if (infoPtr->nOldHit < 0)
      return TRUE;

5905 5906 5907 5908
    /* If the last button we were over is depressed then make it not */
    /* depressed and redraw it */
    if(infoPtr->nOldHit == infoPtr->nButtonDown)
    {
5909 5910 5911
      TBUTTON_INFO *btnPtr;
      RECT rc1;

5912 5913 5914 5915
      btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];

      btnPtr->fsState &= ~TBSTATE_PRESSED;

5916
      rc1 = btnPtr->rect;
5917
      InflateRect (&rc1, 1, 1);
5918
      InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5919 5920
    }

5921 5922 5923 5924 5925 5926 5927 5928 5929
    if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
    {
        NMTOOLBARW nmt;
        ZeroMemory(&nmt, sizeof(nmt));
        nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
        TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
        infoPtr->bDragOutSent = TRUE;
    }

5930 5931 5932 5933
    infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5934 5935

static LRESULT
5936
TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5937
{
5938
    POINT pt;
5939
    TRACKMOUSEEVENT trackinfo;
5940 5941
    INT   nHit;
    TBUTTON_INFO *btnPtr;
5942
    BOOL button;
5943
    
5944 5945 5946
    if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
        TOOLBAR_TooltipCreateControl(infoPtr);
    
5947
    if ((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme) {
5948 5949 5950 5951 5952 5953
        /* fill in the TRACKMOUSEEVENT struct */
        trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
        trackinfo.dwFlags = TME_QUERY;

        /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
        _TrackMouseEvent(&trackinfo);
5954

5955
        /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5956
        if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5957
            trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5958
            trackinfo.hwndTrack = infoPtr->hwndSelf;
5959

5960 5961 5962
            /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
            /* and can properly deactivate the hot toolbar button */
            _TrackMouseEvent(&trackinfo);
5963
        }
5964
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5965

Alexandre Julliard's avatar
Alexandre Julliard committed
5966
    if (infoPtr->hwndToolTip)
5967
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5968 5969
			    WM_MOUSEMOVE, wParam, lParam);

5970 5971
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5972

5973
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5974

5975
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme) && (!infoPtr->bAnchor || button))
5976
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5977

5978 5979
    if (infoPtr->nOldHit != nHit)
    {
5980 5981
        if (infoPtr->bCaptured)
        {
5982 5983 5984 5985 5986 5987 5988 5989 5990
            if (!infoPtr->bDragOutSent)
            {
                NMTOOLBARW nmt;
                ZeroMemory(&nmt, sizeof(nmt));
                nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
                TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
                infoPtr->bDragOutSent = TRUE;
            }

5991 5992 5993
            btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
            if (infoPtr->nOldHit == infoPtr->nButtonDown) {
                btnPtr->fsState &= ~TBSTATE_PRESSED;
5994
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5995 5996 5997
            }
            else if (nHit == infoPtr->nButtonDown) {
                btnPtr->fsState |= TBSTATE_PRESSED;
5998
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5999 6000 6001
            }
            infoPtr->nOldHit = nHit;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
6002
    }
6003

Alexandre Julliard's avatar
Alexandre Julliard committed
6004 6005 6006 6007
    return 0;
}


6008
static inline LRESULT
6009
TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6010
{
6011
/*    if (wndPtr->dwStyle & CCS_NODIVIDER) */
6012
	return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
6013 6014
/*    else */
/*	return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
Alexandre Julliard's avatar
Alexandre Julliard committed
6015
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6016 6017


6018
static inline LRESULT
6019
TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6020
{
6021
    if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6022
	((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
6023

6024
    return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6025
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6026 6027 6028


static LRESULT
6029
TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
6030
{
Alexandre Julliard's avatar
Alexandre Julliard committed
6031
    TOOLBAR_INFO *infoPtr;
6032
    DWORD styleadd = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
6033

Alexandre Julliard's avatar
Alexandre Julliard committed
6034
    /* allocate memory for info structure */
6035
    infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6036
    SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6037

Alexandre Julliard's avatar
Alexandre Julliard committed
6038
    /* paranoid!! */
Alexandre Julliard's avatar
Alexandre Julliard committed
6039
    infoPtr->dwStructSize = sizeof(TBBUTTON);
6040
    infoPtr->nRows = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
6041

6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058
    /* initialize info structure */
    infoPtr->nButtonWidth = 23;
    infoPtr->nButtonHeight = 22;
    infoPtr->nBitmapHeight = 16;
    infoPtr->nBitmapWidth = 16;

    infoPtr->nMaxTextRows = 1;
    infoPtr->cxMin = -1;
    infoPtr->cxMax = -1;
    infoPtr->nNumBitmaps = 0;
    infoPtr->nNumStrings = 0;

    infoPtr->bCaptured = FALSE;
    infoPtr->nButtonDown = -1;
    infoPtr->nButtonDrag = -1;
    infoPtr->nOldHit = -1;
    infoPtr->nHotItem = -1;
6059 6060
    infoPtr->hwndNotify = lpcs->hwndParent;
    infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071
    infoPtr->bAnchor = FALSE; /* no anchor highlighting */
    infoPtr->bDragOutSent = FALSE;
    infoPtr->iVersion = 0;
    infoPtr->hwndSelf = hwnd;
    infoPtr->bDoRedraw = TRUE;
    infoPtr->clrBtnHighlight = CLR_DEFAULT;
    infoPtr->clrBtnShadow = CLR_DEFAULT;
    infoPtr->szPadding.cx = DEFPAD_CX;
    infoPtr->szPadding.cy = DEFPAD_CY;
    infoPtr->iListGap = DEFLISTGAP;
    infoPtr->iTopMargin = default_top_margin(infoPtr);
6072
    infoPtr->dwStyle = lpcs->style;
6073 6074
    infoPtr->tbim.iButton = -1;

Alexandre Julliard's avatar
Alexandre Julliard committed
6075
    /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6076 6077 6078
    if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
        HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
	SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6079
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6080

6081 6082 6083 6084 6085
    /* I think the code below is a bug, but it is the way that the native
     * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
     * forgets to specify TBSTYLE_TRANSPARENT but does specify either
     * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
     * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6086
     * Somehow, the only cases of this seem to be MFC programs.
6087 6088 6089 6090 6091 6092
     *
     * Note also that the addition of _TRANSPARENT occurs *only* here. It
     * does not occur in the WM_STYLECHANGING routine.
     *    (Guy Albertelli   9/2001)
     *
     */
6093
    if ((infoPtr->dwStyle & TBSTYLE_FLAT) && !(lpcs->style & TBSTYLE_TRANSPARENT))
6094
	styleadd |= TBSTYLE_TRANSPARENT;
6095
    if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6096
	styleadd |= CCS_TOP;   /* default to top */
6097
	SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6098 6099
    }

6100
    return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
Alexandre Julliard's avatar
Alexandre Julliard committed
6101 6102 6103 6104
}


static LRESULT
6105
TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6106
{
6107
    DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6108
    RECT rcWindow;
6109
    HDC hdc;
Alexandre Julliard's avatar
Alexandre Julliard committed
6110

6111 6112
    if (dwStyle & WS_MINIMIZE)
	return 0; /* Nothing to do */
Alexandre Julliard's avatar
Alexandre Julliard committed
6113

6114
    DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6115

6116
    if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
Alexandre Julliard's avatar
Alexandre Julliard committed
6117 6118
	return 0;

6119 6120 6121 6122
    if (!(dwStyle & CCS_NODIVIDER))
    {
	GetWindowRect (hwnd, &rcWindow);
	OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6123
	if( dwStyle & WS_BORDER )
6124
	    InflateRect (&rcWindow, -1, -1);
6125 6126
	DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6127

6128
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
6129 6130 6131 6132 6133

    return 0;
}


6134 6135
/* handles requests from the tooltip control on what text to display */
static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
Alexandre Julliard's avatar
Alexandre Julliard committed
6136
{
6137
    int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6138 6139 6140
    NMTTDISPINFOA nmtdi;
    unsigned int len;
    LRESULT ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
6141

6142
    TRACE("button index = %d\n", index);
6143

6144
    Free (infoPtr->pszTooltipText);
6145
    infoPtr->pszTooltipText = NULL;
6146

6147 6148
    if (index < 0)
        return 0;
6149

6150
    if (infoPtr->bUnicode)
6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166
    {
        WCHAR wszBuffer[INFOTIPSIZE+1];
        NMTBGETINFOTIPW tbgit;

        wszBuffer[0] = '\0';
        wszBuffer[INFOTIPSIZE] = '\0';

        tbgit.pszText = wszBuffer;
        tbgit.cchTextMax = INFOTIPSIZE;
        tbgit.iItem = lpnmtdi->hdr.idFrom;
        tbgit.lParam = infoPtr->buttons[index].dwData;

        TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);

        TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));

6167
        len = tbgit.pszText ? lstrlenW(tbgit.pszText) : 0;
6168
        if (len > ARRAY_SIZE(lpnmtdi->szText) - 1)
6169 6170 6171 6172
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6173
            infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185
            if (infoPtr->pszTooltipText)
            {
                memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
                lpnmtdi->lpszText = infoPtr->pszTooltipText;
                return 0;
            }
        }
        else if (len > 0)
        {
            memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
            return 0;
        }
6186
    }
6187 6188 6189 6190
    else
    {
        CHAR szBuffer[INFOTIPSIZE+1];
        NMTBGETINFOTIPA tbgit;
6191

6192 6193
        szBuffer[0] = '\0';
        szBuffer[INFOTIPSIZE] = '\0';
6194

6195 6196 6197 6198
        tbgit.pszText = szBuffer;
        tbgit.cchTextMax = INFOTIPSIZE;
        tbgit.iItem = lpnmtdi->hdr.idFrom;
        tbgit.lParam = infoPtr->buttons[index].dwData;
Alexandre Julliard's avatar
Alexandre Julliard committed
6199

6200
        TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
Alexandre Julliard's avatar
Alexandre Julliard committed
6201

6202 6203
        TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));

6204
        len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6205
        if (len > ARRAY_SIZE(lpnmtdi->szText))
6206 6207 6208 6209
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6210
            infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6211 6212
            if (infoPtr->pszTooltipText)
            {
6213
                MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6214 6215 6216 6217
                lpnmtdi->lpszText = infoPtr->pszTooltipText;
                return 0;
            }
        }
6218
        else if (tbgit.pszText && tbgit.pszText[0])
6219
        {
6220
            MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, lpnmtdi->lpszText, ARRAY_SIZE(lpnmtdi->szText));
6221 6222 6223
            return 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6224

6225 6226 6227 6228 6229 6230
    /* if button has text, but it is not shown then automatically
     * use that text as tooltip */
    if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
        !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
    {
        LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6231
        len = pszText ? lstrlenW(pszText) : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
6232

6233 6234
        TRACE("using button hidden text %s\n", debugstr_w(pszText));

6235
        if (len > ARRAY_SIZE(lpnmtdi->szText) - 1)
6236 6237 6238 6239
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6240
            infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253
            if (infoPtr->pszTooltipText)
            {
                memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
                lpnmtdi->lpszText = infoPtr->pszTooltipText;
                return 0;
            }
        }
        else if (len > 0)
        {
            memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
            return 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6254

6255
    TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
Alexandre Julliard's avatar
Alexandre Julliard committed
6256

6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267
    /* Last resort, forward TTN_GETDISPINFO to the app:

       - NFR_UNICODE gets TTN_GETDISPINFOW, and TTN_GETDISPINFOA if -W returned no text;
       - NFR_ANSI gets only TTN_GETDISPINFOA.
    */
    if (infoPtr->bUnicode)
    {
        ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);

        TRACE("TTN_GETDISPINFOW - got string %s\n", debugstr_w(lpnmtdi->lpszText));

6268 6269 6270
        if (IS_INTRESOURCE(lpnmtdi->lpszText))
            return ret;

6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287
        if (lpnmtdi->lpszText && *lpnmtdi->lpszText)
            return ret;
    }

    nmtdi.hdr.hwndFrom = lpnmtdi->hdr.hwndFrom;
    nmtdi.hdr.idFrom = lpnmtdi->hdr.idFrom;
    nmtdi.hdr.code = TTN_GETDISPINFOA;
    nmtdi.lpszText = nmtdi.szText;
    nmtdi.szText[0] = 0;
    nmtdi.hinst = lpnmtdi->hinst;
    nmtdi.uFlags = lpnmtdi->uFlags;
    nmtdi.lParam = lpnmtdi->lParam;

    ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmtdi.hdr.idFrom, (LPARAM)&nmtdi);

    TRACE("TTN_GETDISPINFOA - got string %s\n", debugstr_a(nmtdi.lpszText));

6288 6289 6290 6291 6292 6293 6294 6295 6296 6297
    lpnmtdi->hinst = nmtdi.hinst;
    lpnmtdi->uFlags = nmtdi.uFlags;
    lpnmtdi->lParam = nmtdi.lParam;

    if (IS_INTRESOURCE(nmtdi.lpszText))
    {
        lpnmtdi->lpszText = (WCHAR *)nmtdi.lpszText;
        return ret;
    }

6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318
    if (!nmtdi.lpszText || !*nmtdi.lpszText)
        return ret;

    len = MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, NULL, 0);
    if (len > ARRAY_SIZE(lpnmtdi->szText))
    {
        infoPtr->pszTooltipText = Alloc(len * sizeof(WCHAR));
        if (infoPtr->pszTooltipText)
        {
            MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, infoPtr->pszTooltipText, len);
            lpnmtdi->lpszText = infoPtr->pszTooltipText;
            return 0;
        }
    }
    else
    {
        MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, lpnmtdi->lpszText, ARRAY_SIZE(nmtdi.szText));
        return 0;
    }

    return ret;
6319 6320 6321
}


6322
static inline LRESULT
6323
TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6324 6325 6326 6327 6328
{
    switch (lpnmh->code)
    {
    case PGN_CALCSIZE:
    {
6329
        LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341

        if (lppgc->dwFlag == PGF_CALCWIDTH) {
            lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
            TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
                  lppgc->iWidth);
        }
        else {
            lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
            TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
                  lppgc->iHeight);
        }
    	return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
6342 6343
    }

6344 6345
    case PGN_SCROLL:
    {
6346
        LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6347 6348 6349 6350 6351 6352 6353 6354 6355

        lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
                          infoPtr->nButtonWidth : infoPtr->nButtonHeight;
        TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
              lppgs->iScroll, lppgs->iDir);
        return 0;
    }

    case TTN_GETDISPINFOW:
6356
        return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6357 6358

    case TTN_GETDISPINFOA:
6359
        FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6360 6361 6362 6363 6364
        return 0;

    default:
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6365 6366 6367
}


6368
static LRESULT
6369
TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6370
{
6371
    LRESULT format;
6372

6373
    TRACE("wParam %Ix, lParam %Ix\n", wParam, lParam);
6374

6375
    if (lParam == NF_QUERY)
6376 6377
        return NFR_UNICODE;

6378
    if (lParam == NF_REQUERY) {
6379
	format = SendMessageW(infoPtr->hwndNotify,
6380
			 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6381
	if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6382
	    ERR("wrong response to WM_NOTIFYFORMAT (%Id), assuming ANSI\n", format);
6383
	    format = NFR_ANSI;
6384
	}
6385
	return format;
6386
    }
6387
    return 0;
6388 6389 6390
}


Alexandre Julliard's avatar
Alexandre Julliard committed
6391
static LRESULT
6392
TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6393
{
6394 6395
    HDC hdc;
    PAINTSTRUCT ps;
Alexandre Julliard's avatar
Alexandre Julliard committed
6396

6397
    /* fill ps.rcPaint with a default rect */
6398
    ps.rcPaint = infoPtr->rcBound;
6399

6400
    hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6401

6402
    TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6403

6404 6405
    TOOLBAR_Refresh (infoPtr, hdc, &ps);
    if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6406

Alexandre Julliard's avatar
Alexandre Julliard committed
6407 6408 6409 6410
    return 0;
}


6411
static LRESULT
6412
TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6413 6414 6415 6416 6417 6418 6419 6420 6421 6422
{
    TRACE("nHotItem = %d\n", infoPtr->nHotItem);

    /* make first item hot */
    if (infoPtr->nNumButtons > 0)
        TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);

    return 0;
}

6423
static LRESULT
6424
TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6425
{
6426
    TRACE("font=%p redraw=%d\n", hFont, Redraw);
6427
    
6428
    if (hFont == 0)
6429 6430
        infoPtr->hFont = infoPtr->hDefaultFont;
    else
6431
        infoPtr->hFont = hFont;
6432

6433
    TOOLBAR_CalcToolbar(infoPtr);
6434

6435
    if (Redraw)
6436
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6437 6438
    return 1;
}
6439

6440
static LRESULT
6441
TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6442 6443 6444 6445 6446 6447
     /*****************************************************
      *
      * Function;
      *  Handles the WM_SETREDRAW message.
      *
      * Documentation:
6448
      *  According to testing V4.71 of COMCTL32 returns the
6449 6450
      *  *previous* status of the redraw flag (either 0 or 1)
      *  instead of the MSDN documented value of 0 if handled.
Francois Gouget's avatar
Francois Gouget committed
6451
      *  (For laughs see the "consistency" with same function
6452 6453 6454 6455 6456 6457
      *   in rebar.)
      *
      *****************************************************/
{
    BOOL oldredraw = infoPtr->bDoRedraw;

6458
    TRACE("set to %s\n",
6459 6460 6461 6462 6463 6464 6465 6466 6467
	  (wParam) ? "TRUE" : "FALSE");
    infoPtr->bDoRedraw = (BOOL) wParam;
    if (wParam) {
	InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
    }
    return (oldredraw) ? 1 : 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
6468
static LRESULT
6469
TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
6470
{
6471
    TRACE("sizing toolbar\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
6472

6473 6474 6475 6476 6477 6478
    if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
    {
        RECT delta_width, delta_height, client, dummy;
        DWORD min_x, max_x, min_y, max_y;
        TBUTTON_INFO *btnPtr;
        INT i;
6479

6480
        GetClientRect(infoPtr->hwndSelf, &client);
6481
        if(client.right > infoPtr->client_rect.right)
6482
        {
6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499
            min_x = infoPtr->client_rect.right;
            max_x = client.right;
        }
        else
        {
            max_x = infoPtr->client_rect.right;
            min_x = client.right;
        }
        if(client.bottom > infoPtr->client_rect.bottom)
        {
            min_y = infoPtr->client_rect.bottom;
            max_y = client.bottom;
        }
        else
        {
            max_y = infoPtr->client_rect.bottom;
            min_y = client.bottom;
6500 6501
        }

6502 6503 6504 6505 6506 6507 6508 6509
        SetRect(&delta_width, min_x, 0, max_x, min_y);
        SetRect(&delta_height, 0, min_y, max_x, max_y);

        TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
        btnPtr = infoPtr->buttons;
        for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
            if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
                IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6510
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6511
    }
6512 6513
    GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
    TOOLBAR_AutoSize(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6514 6515
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6516 6517


Alexandre Julliard's avatar
Alexandre Julliard committed
6518
static LRESULT
6519
TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
6520
{
6521
    if (nType == GWL_STYLE)
6522
        return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6523

Alexandre Julliard's avatar
Alexandre Julliard committed
6524 6525
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6526 6527


6528
static LRESULT
6529
TOOLBAR_SysColorChange (void)
6530 6531 6532 6533 6534 6535 6536
{
    COMCTL32_RefreshSysColors();

    return 0;
}


6537
/* update theme after a WM_THEMECHANGED message */
6538
static LRESULT theme_changed (TOOLBAR_INFO *infoPtr)
6539
{
6540
    CloseThemeData (infoPtr->hTheme);
6541
    infoPtr->hTheme = OpenThemeDataForDpi (NULL, themeClass, GetDpiForWindow (infoPtr->hwndSelf));
6542
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
6543 6544 6545
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
6546

6547
static LRESULT WINAPI
6548
ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6549
{
6550
    TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6551

6552
    TRACE("hwnd %p, msg %x, wparam %Ix, lparam %Ix\n", hwnd, uMsg, wParam, lParam);
6553

6554
    if (!infoPtr && (uMsg != WM_NCCREATE))
6555
	return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6556

Alexandre Julliard's avatar
Alexandre Julliard committed
6557 6558 6559
    switch (uMsg)
    {
	case TB_ADDBITMAP:
6560
	    return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6561

6562
	case TB_ADDBUTTONSA:
6563
	case TB_ADDBUTTONSW:
6564 6565
	    return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
	                                uMsg == TB_ADDBUTTONSW);
6566
	case TB_ADDSTRINGA:
6567
	    return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6568

6569
	case TB_ADDSTRINGW:
6570
	    return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6571 6572

	case TB_AUTOSIZE:
6573
	    return TOOLBAR_AutoSize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6574 6575

	case TB_BUTTONCOUNT:
6576
	    return TOOLBAR_ButtonCount (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6577 6578

	case TB_BUTTONSTRUCTSIZE:
6579
	    return TOOLBAR_ButtonStructSize (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6580 6581

	case TB_CHANGEBITMAP:
6582
	    return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6583

Alexandre Julliard's avatar
Alexandre Julliard committed
6584
	case TB_CHECKBUTTON:
6585
	    return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6586 6587

	case TB_COMMANDTOINDEX:
6588
	    return TOOLBAR_CommandToIndex (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6589

Alexandre Julliard's avatar
Alexandre Julliard committed
6590
	case TB_CUSTOMIZE:
6591
	    return TOOLBAR_Customize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6592 6593

	case TB_DELETEBUTTON:
6594
	    return TOOLBAR_DeleteButton (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6595 6596

	case TB_ENABLEBUTTON:
6597
	    return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6598

6599
	case TB_GETANCHORHIGHLIGHT:
6600
	    return TOOLBAR_GetAnchorHighlight (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6601 6602

	case TB_GETBITMAP:
6603
	    return TOOLBAR_GetBitmap (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6604

Alexandre Julliard's avatar
Alexandre Julliard committed
6605
	case TB_GETBITMAPFLAGS:
6606
	    return TOOLBAR_GetBitmapFlags ();
Alexandre Julliard's avatar
Alexandre Julliard committed
6607 6608

	case TB_GETBUTTON:
6609
	    return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6610

6611
	case TB_GETBUTTONINFOA:
6612
	case TB_GETBUTTONINFOW:
6613 6614
	    return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
	                                   uMsg == TB_GETBUTTONINFOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
6615
	case TB_GETBUTTONSIZE:
6616
	    return TOOLBAR_GetButtonSize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6617

6618
	case TB_GETBUTTONTEXTA:
6619
	case TB_GETBUTTONTEXTW:
6620 6621
	    return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
	                                  uMsg == TB_GETBUTTONTEXTW);
6622

Alexandre Julliard's avatar
Alexandre Julliard committed
6623
	case TB_GETDISABLEDIMAGELIST:
6624
	    return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6625

Alexandre Julliard's avatar
Alexandre Julliard committed
6626
	case TB_GETEXTENDEDSTYLE:
6627
	    return TOOLBAR_GetExtendedStyle (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6628 6629

	case TB_GETHOTIMAGELIST:
6630
	    return TOOLBAR_GetHotImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6631

6632
	case TB_GETHOTITEM:
6633
	    return TOOLBAR_GetHotItem (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6634 6635

	case TB_GETIMAGELIST:
6636
	    return TOOLBAR_GetDefImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6637

6638
	case TB_GETINSERTMARK:
6639
	    return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6640 6641

	case TB_GETINSERTMARKCOLOR:
6642
	    return TOOLBAR_GetInsertMarkColor (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6643 6644

	case TB_GETITEMRECT:
6645
	    return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6646

Alexandre Julliard's avatar
Alexandre Julliard committed
6647
	case TB_GETMAXSIZE:
6648
	    return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6649

6650
/*	case TB_GETOBJECT:			*/ /* 4.71 */
6651 6652

	case TB_GETPADDING:
6653
	    return TOOLBAR_GetPadding (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6654 6655

	case TB_GETRECT:
6656
	    return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6657 6658

	case TB_GETROWS:
6659
	    return TOOLBAR_GetRows (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6660 6661

	case TB_GETSTATE:
6662
	    return TOOLBAR_GetState (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6663

Robert Shearman's avatar
Robert Shearman committed
6664
	case TB_GETSTRINGA:
6665
            return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
Robert Shearman's avatar
Robert Shearman committed
6666 6667

	case TB_GETSTRINGW:
6668
	    return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
Robert Shearman's avatar
Robert Shearman committed
6669

Alexandre Julliard's avatar
Alexandre Julliard committed
6670
	case TB_GETSTYLE:
6671
	    return TOOLBAR_GetStyle (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6672

Alexandre Julliard's avatar
Alexandre Julliard committed
6673
	case TB_GETTEXTROWS:
6674
	    return TOOLBAR_GetTextRows (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6675 6676

	case TB_GETTOOLTIPS:
6677
	    return TOOLBAR_GetToolTips (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6678

Alexandre Julliard's avatar
Alexandre Julliard committed
6679
	case TB_GETUNICODEFORMAT:
6680
	    return TOOLBAR_GetUnicodeFormat (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6681 6682

	case TB_HIDEBUTTON:
6683
	    return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6684 6685

	case TB_HITTEST:
6686
	    return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6687

Alexandre Julliard's avatar
Alexandre Julliard committed
6688
	case TB_INDETERMINATE:
6689
	    return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6690

6691
	case TB_INSERTBUTTONA:
6692
	case TB_INSERTBUTTONW:
6693 6694
	    return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
	                                 uMsg == TB_INSERTBUTTONW);
6695

6696
/*	case TB_INSERTMARKHITTEST:		*/ /* 4.71 */
Alexandre Julliard's avatar
Alexandre Julliard committed
6697 6698

	case TB_ISBUTTONCHECKED:
6699
	    return TOOLBAR_IsButtonChecked (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6700 6701

	case TB_ISBUTTONENABLED:
6702
	    return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6703 6704

	case TB_ISBUTTONHIDDEN:
6705
	    return TOOLBAR_IsButtonHidden (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6706 6707

	case TB_ISBUTTONHIGHLIGHTED:
6708
	    return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6709 6710

	case TB_ISBUTTONINDETERMINATE:
6711
	    return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6712 6713

	case TB_ISBUTTONPRESSED:
6714
	    return TOOLBAR_IsButtonPressed (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6715

6716
	case TB_LOADIMAGES:
6717
	    return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6718 6719 6720

	case TB_MAPACCELERATORA:
	case TB_MAPACCELERATORW:
6721
	    return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6722 6723

	case TB_MARKBUTTON:
6724
	    return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6725

Robert Shearman's avatar
Robert Shearman committed
6726
	case TB_MOVEBUTTON:
6727
	    return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6728 6729

	case TB_PRESSBUTTON:
6730
	    return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6731

6732
	case TB_REPLACEBITMAP:
6733
            return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6734

6735
	case TB_SAVERESTOREA:
6736
	    return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6737

6738
	case TB_SAVERESTOREW:
6739
	    return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6740 6741

	case TB_SETANCHORHIGHLIGHT:
6742
	    return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6743 6744

	case TB_SETBITMAPSIZE:
6745
	    return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6746

6747
	case TB_SETBUTTONINFOA:
6748
	case TB_SETBUTTONINFOW:
6749 6750
	    return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
                                          uMsg == TB_SETBUTTONINFOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
6751
	case TB_SETBUTTONSIZE:
6752
	    return TOOLBAR_SetButtonSize (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6753

Alexandre Julliard's avatar
Alexandre Julliard committed
6754
	case TB_SETBUTTONWIDTH:
6755
	    return TOOLBAR_SetButtonWidth (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6756 6757

	case TB_SETCMDID:
6758
	    return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6759

Alexandre Julliard's avatar
Alexandre Julliard committed
6760
	case TB_SETDISABLEDIMAGELIST:
6761
	    return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6762

Alexandre Julliard's avatar
Alexandre Julliard committed
6763
	case TB_SETDRAWTEXTFLAGS:
6764
	    return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6765 6766

	case TB_SETEXTENDEDSTYLE:
6767
	    return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6768 6769

	case TB_SETHOTIMAGELIST:
6770
	    return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6771

6772
	case TB_SETHOTITEM:
6773
	    return TOOLBAR_SetHotItem (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6774 6775

	case TB_SETIMAGELIST:
6776
	    return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6777 6778

	case TB_SETINDENT:
6779
	    return TOOLBAR_SetIndent (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6780

6781
	case TB_SETINSERTMARK:
6782
	    return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6783 6784

	case TB_SETINSERTMARKCOLOR:
6785
	    return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6786

Alexandre Julliard's avatar
Alexandre Julliard committed
6787
	case TB_SETMAXTEXTROWS:
6788
	    return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6789

6790
	case TB_SETPADDING:
6791
	    return TOOLBAR_SetPadding (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6792 6793

	case TB_SETPARENT:
6794
	    return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6795 6796

	case TB_SETROWS:
6797
	    return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6798 6799

	case TB_SETSTATE:
6800
	    return TOOLBAR_SetState (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6801 6802

	case TB_SETSTYLE:
6803
	    return TOOLBAR_SetStyle (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6804

Alexandre Julliard's avatar
Alexandre Julliard committed
6805
	case TB_SETTOOLTIPS:
6806
	    return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6807

Alexandre Julliard's avatar
Alexandre Julliard committed
6808
	case TB_SETUNICODEFORMAT:
6809
	    return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6810

6811 6812
	case TB_SETBOUNDINGSIZE:
	    return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6813

6814
	case TB_SETHOTITEM2:
6815
	    return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6816

6817
	case TB_SETLISTGAP:
6818
	    return TOOLBAR_SetListGap(infoPtr, wParam);
6819

6820
	case TB_GETIMAGELISTCOUNT:
6821
	    return TOOLBAR_GetImageListCount(infoPtr);
Robert Shearman's avatar
Robert Shearman committed
6822

6823
	case TB_GETIDEALSIZE:
6824
	    return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6825

6826 6827
	case TB_UNKWN464:
	    return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6828 6829 6830 6831 6832

/* Common Control Messages */

/*	case TB_GETCOLORSCHEME:			*/ /* identical to CCM_ */
	case CCM_GETCOLORSCHEME:
6833
	    return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6834 6835 6836

/*	case TB_SETCOLORSCHEME:			*/ /* identical to CCM_ */
	case CCM_SETCOLORSCHEME:
6837
	    return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6838 6839

	case CCM_GETVERSION:
6840
	    return TOOLBAR_GetVersion (infoPtr);
6841

6842
	case CCM_SETVERSION:
6843
	    return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6844

Alexandre Julliard's avatar
Alexandre Julliard committed
6845

6846
/*	case WM_CHAR: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6847

Alexandre Julliard's avatar
Alexandre Julliard committed
6848
	case WM_CREATE:
6849
	    return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6850

6851
	case WM_DESTROY:
6852
	  return TOOLBAR_Destroy (infoPtr);
6853

Alexandre Julliard's avatar
Alexandre Julliard committed
6854
	case WM_ERASEBKGND:
6855
	    return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6856

Eric Pouech's avatar
Eric Pouech committed
6857
	case WM_GETFONT:
6858
		return TOOLBAR_GetFont (infoPtr);
Eric Pouech's avatar
Eric Pouech committed
6859

6860
	case WM_KEYDOWN:
6861
	    return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6862

6863
/*	case WM_KILLFOCUS: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6864

Alexandre Julliard's avatar
Alexandre Julliard committed
6865
	case WM_LBUTTONDBLCLK:
6866
	    return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6867 6868

	case WM_LBUTTONDOWN:
6869
	    return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6870 6871

	case WM_LBUTTONUP:
6872
	    return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6873

6874
	case WM_RBUTTONUP:
6875
	    return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6876

6877
	case WM_RBUTTONDBLCLK:
6878
	    return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6879

Alexandre Julliard's avatar
Alexandre Julliard committed
6880
	case WM_MOUSEMOVE:
6881
	    return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6882

6883
	case WM_MOUSELEAVE:
6884
	    return TOOLBAR_MouseLeave (infoPtr);
6885

6886
	case WM_CAPTURECHANGED:
6887
	    if (hwnd == (HWND)lParam) return 0;
6888
	    return TOOLBAR_CaptureChanged(infoPtr);
6889

Alexandre Julliard's avatar
Alexandre Julliard committed
6890
	case WM_NCACTIVATE:
6891
	    return TOOLBAR_NCActivate (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6892 6893

	case WM_NCCALCSIZE:
6894
	    return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6895

6896
	case WM_NCCREATE:
6897
	    return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6898

Alexandre Julliard's avatar
Alexandre Julliard committed
6899
	case WM_NCPAINT:
6900
	    return TOOLBAR_NCPaint (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6901

Alexandre Julliard's avatar
Alexandre Julliard committed
6902
	case WM_NOTIFY:
6903
	    return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6904

6905
	case WM_NOTIFYFORMAT:
6906
	    return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6907

6908
	case WM_PRINTCLIENT:
Alexandre Julliard's avatar
Alexandre Julliard committed
6909
	case WM_PAINT:
6910
	    return TOOLBAR_Paint (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6911

6912
	case WM_SETFOCUS:
6913
	    return TOOLBAR_SetFocus (infoPtr);
6914

6915
	case WM_SETFONT:
6916
            return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6917

6918
	case WM_SETREDRAW:
6919
	    return TOOLBAR_SetRedraw (infoPtr, wParam);
6920

Alexandre Julliard's avatar
Alexandre Julliard committed
6921
	case WM_SIZE:
6922
	    return TOOLBAR_Size (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6923 6924

	case WM_STYLECHANGED:
6925
	    return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6926

6927
	case WM_SYSCOLORCHANGE:
6928
	    return TOOLBAR_SysColorChange ();
6929 6930
            
        case WM_THEMECHANGED:
6931
            return theme_changed (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6932

6933
/*	case WM_WININICHANGE: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6934 6935 6936 6937 6938 6939

	case WM_CHARTOITEM:
	case WM_COMMAND:
	case WM_DRAWITEM:
	case WM_MEASUREITEM:
	case WM_VKEYTOITEM:
6940
            return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6941

6942 6943
	/* We see this in Outlook Express 5.x and just does DefWindowProc */
        case PGM_FORWARDMOUSE:
6944
	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6945

Alexandre Julliard's avatar
Alexandre Julliard committed
6946
	default:
6947
	    if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6948
		ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
6949
	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6950 6951 6952 6953
    }
}


6954
VOID
Patrik Stridvall's avatar
Patrik Stridvall committed
6955
TOOLBAR_Register (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
6956
{
6957
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
6958

6959
    ZeroMemory (&wndClass, sizeof(WNDCLASSW));
Alexandre Julliard's avatar
Alexandre Julliard committed
6960
    wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
6961
    wndClass.lpfnWndProc   = ToolbarWindowProc;
Alexandre Julliard's avatar
Alexandre Julliard committed
6962 6963
    wndClass.cbClsExtra    = 0;
    wndClass.cbWndExtra    = sizeof(TOOLBAR_INFO *);
6964 6965 6966
    wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6967

6968
    RegisterClassW (&wndClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
6969
}
6970 6971 6972


VOID
Patrik Stridvall's avatar
Patrik Stridvall committed
6973
TOOLBAR_Unregister (void)
6974
{
6975
    UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6976
}
6977 6978 6979 6980 6981 6982 6983 6984 6985

static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
{
    HIMAGELIST himlold;
    PIMLENTRY c = NULL;

    /* Check if the entry already exists */
    c = TOOLBAR_GetImageListEntry(*pies, *cies, id);

6986 6987 6988 6989
    /* Don't add new entry for NULL imagelist */
    if (!c && !himl)
        return NULL;

6990 6991 6992 6993 6994
    /* If this is a new entry we must create it and insert into the array */
    if (!c)
    {
        PIMLENTRY *pnies;

6995
        c = Alloc(sizeof(IMLENTRY));
6996 6997
	c->id = id;

6998
	pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6999 7000 7001 7002
	memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
	pnies[*cies] = c;
	(*cies)++;

7003
	Free(*pies);
7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018
	*pies = pnies;
    }

    himlold = c->himl;
    c->himl = himl;

    return himlold;
}


static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
{
    int i;

    for (i = 0; i < *cies; i++)
7019
	Free((*pies)[i]);
7020

7021
    Free(*pies);
7022 7023 7024 7025 7026 7027

    *cies = 0;
    *pies = NULL;
}


7028
static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049
{
    PIMLENTRY c = NULL;

    if (pies != NULL)
    {
	int i;

        for (i = 0; i < cies; i++)
        {
            if (pies[i]->id == id)
            {
                c = pies[i];
                break;
            }
        }
    }

    return c;
}


7050
static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061
{
    HIMAGELIST himlDef = 0;
    PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);

    if (pie)
        himlDef = pie->himl;

    return himlDef;
}


7062
static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
7063 7064
{
    if (infoPtr->bUnicode)
7065
        return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076
    else
    {
        CHAR Buffer[256];
        NMTOOLBARA nmtba;
        BOOL bRet = FALSE;

        nmtba.iItem = nmtb->iItem;
        nmtba.pszText = Buffer;
        nmtba.cchText = 256;
        ZeroMemory(nmtba.pszText, nmtba.cchText);

7077
        if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7078 7079 7080
        {
            int ccht = strlen(nmtba.pszText);
            if (ccht)
7081
               MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7082 7083
                  nmtb->pszText, nmtb->cchText);

7084
            nmtb->tbButton = nmtba.tbButton;
7085 7086 7087 7088 7089 7090 7091 7092
            bRet = TRUE;
        }

        return bRet;
    }
}


7093
static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7094
{
7095
    NMTOOLBARW nmtb;
7096

7097 7098
    /* MSDN states that iItem is the index of the button, rather than the
     * command ID as used by every other NMTOOLBAR notification */
7099 7100 7101
    nmtb.iItem = iItem;
    memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));

7102
    return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);
7103
}