toolbar.c 207 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 "wine/unicode.h"
80
#include "winnls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
81
#include "commctrl.h"
82
#include "comctl32.h"
83
#include "uxtheme.h"
84
#include "vssym32.h"
85
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
86

87
WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
88

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

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

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

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

119 120
typedef struct
{
121 122
    DWORD    dwStructSize;    /* size of TBBUTTON struct */
    INT      nWidth;          /* width of the toolbar */
123
    RECT     client_rect;
124
    RECT     rcBound;         /* bounding rectangle */
125 126 127 128 129 130 131 132 133 134 135 136
    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 */
137
    INT      nNumBitmapInfos;
Robert Shearman's avatar
Robert Shearman committed
138 139
    INT      nButtonDown;     /* toolbar button being pressed or -1 if none */
    INT      nButtonDrag;     /* toolbar button being dragged or -1 if none */
140 141
    INT      nOldHit;
    INT      nHotItem;        /* index of the "hot" item */
142
    SIZE     szPadding;       /* padding values around button */
143
    INT      iTopMargin;      /* the top margin */
144
    INT      iListGap;        /* default gap between text and image for toolbar with list style */
145
    HFONT    hDefaultFont;
146
    HFONT    hFont;           /* text font */
147
    HIMAGELIST himlInt;       /* image list created internally */
148 149 150 151 152 153
    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 */
154 155
    HWND     hwndToolTip;     /* handle to tool tip control */
    HWND     hwndNotify;      /* handle to the window that gets notifications */
156
    HWND     hwndSelf;        /* my own handle */
157
    BOOL     bAnchor;         /* anchor highlight enabled */
158
    BOOL     bDoRedraw;       /* Redraw status */
159
    BOOL     bDragOutSent;    /* has TBN_DRAGOUT notification been sent for this drag? */
160
    BOOL     bUnicode;        /* Notifications are ASCII (FALSE) or Unicode (TRUE)? */
161
    BOOL     bCaptured;       /* mouse captured? */
162 163 164
    DWORD      dwStyle;       /* regular toolbar style */
    DWORD      dwExStyle;     /* extended toolbar style */
    DWORD      dwDTFlags;     /* DrawText flags */
165 166

    COLORREF   clrInsertMark;   /* insert mark color */
167 168
    COLORREF   clrBtnHighlight; /* color for Flat Separator */
    COLORREF   clrBtnShadow;    /* color for Flag Separator */
169
    INT      iVersion;
170 171
    LPWSTR   pszTooltipText;    /* temporary store for a string > 80 characters
                                 * for TTN_GETDISPINFOW notification */
172
    TBINSERTMARK  tbim;         /* info on insertion mark */
173 174
    TBUTTON_INFO *buttons;      /* pointer to button array */
    LPWSTR       *strings;      /* pointer to string array */
175
    TBITMAP_INFO *bitmaps;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
} 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;
191
    WCHAR    text[64];
192 193
} CUSTOMBUTTON, *PCUSTOMBUTTON;

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

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

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

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

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

218 219
#define TOOLBAR_NOWHERE (-1)

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

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

231 232 233 234 235 236
#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)

237 238
static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };

239
static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
240
static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
241 242
static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
243 244
static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
245 246 247
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);
248
static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
249
static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
250
static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
251

252

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

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

263
static LPWSTR
264
TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
265 266 267
{
    LPWSTR lpText = NULL;

Robert Shearman's avatar
Robert Shearman committed
268
    /* NOTE: iString == -1 is undocumented */
269
    if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
270 271 272
        lpText = (LPWSTR)btnPtr->iString;
    else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
        lpText = infoPtr->strings[btnPtr->iString];
273

274 275 276
    return lpText;
}

277
static void
278 279 280 281 282 283 284 285 286
TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
{
    TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx (%s)\n",
          tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
          (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
}

static void
TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
287 288
{
    if (TRACE_ON(toolbar)){
289
        TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
290 291
              btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap), 
              bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
292
	TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
293 294 295
        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));
296 297 298 299 300
    }
}


static void
301
TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
302 303 304 305
{
    if (TRACE_ON(toolbar)) {
	INT i;

306
	TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
307 308
	      iP->hwndSelf, line,
	      iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
309
	      iP->nNumStrings, iP->dwStyle);
310
	TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
311
	      iP->hwndSelf, line,
312 313
	      iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
	      (iP->bDoRedraw) ? "TRUE" : "FALSE");
314
 	for(i=0; i<iP->nNumButtons; i++) {
315
            TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
316 317 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
/***********************************************************************
* 		TOOLBAR_CheckStyle
*
* This function validates that the styles set are implemented and
330
* issues FIXMEs warning of possible problems. In a perfect world this
331 332 333
* function should be null.
*/
static void
334
TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
335
{
336
    if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
337
	FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
338 339 340
}


341
static INT
342
TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
343
{
344 345
	if(!IsWindow(infoPtr->hwndSelf))
	    return 0;   /* we have just been destroyed */
346

347 348 349 350
    nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
    nmhdr->hwndFrom = infoPtr->hwndSelf;
    nmhdr->code = code;

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

354
    return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
355 356 357 358 359 360 361 362 363
}

/***********************************************************************
* 		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.
*/
364
static INT
365
TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
366 367 368
{
    INT ret = btnPtr->iBitmap;

369 370 371
    if (ret == I_IMAGECALLBACK)
    {
        /* issue TBN_GETDISPINFO */
372
        NMTBDISPINFOW nmgd;
373 374 375 376 377

        memset(&nmgd, 0, sizeof(nmgd));
        nmgd.idCommand = btnPtr->idCommand;
        nmgd.lParam = btnPtr->dwData;
        nmgd.dwMask = TBNF_IMAGE;
378
        nmgd.iImage = -1;
379 380
        /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
        TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
381 382 383
        if (nmgd.dwMask & TBNF_DI_SETITEM)
            btnPtr->iBitmap = nmgd.iImage;
        ret = nmgd.iImage;
384
        TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
385
            ret, nmgd.dwMask, infoPtr->nNumBitmaps);
386
    }
387 388 389 390

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

391 392 393 394
    return ret;
}


395
static BOOL
396
TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
397
{
398 399 400 401 402 403 404
    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))
405 406 407 408 409
      return TRUE;
    else
      return FALSE;
}

410

411
static inline BOOL
412
TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
413 414 415 416 417 418
{
    HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
    return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
}


419
/***********************************************************************
Robert Shearman's avatar
Robert Shearman committed
420
* 		TOOLBAR_GetImageListForDrawing
421 422
*
* This function validates the bitmap index (including I_IMAGECALLBACK
Robert Shearman's avatar
Robert Shearman committed
423
* functionality) and returns the corresponding image list.
424
*/
Robert Shearman's avatar
Robert Shearman committed
425
static HIMAGELIST
426 427
TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
                                IMAGE_LIST_TYPE imagelist, INT * index)
428
{
429
    HIMAGELIST himl;
430 431

    if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
Robert Shearman's avatar
Robert Shearman committed
432
	if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
433
	WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
Robert Shearman's avatar
Robert Shearman committed
434
	    HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
Robert Shearman's avatar
Robert Shearman committed
435
	return NULL;
436 437
    }

Robert Shearman's avatar
Robert Shearman committed
438 439 440
    if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
	if ((*index == I_IMAGECALLBACK) ||
	    (*index == I_IMAGENONE)) return NULL;
441
	ERR("TBN_GETDISPINFO returned invalid index %d\n",
Robert Shearman's avatar
Robert Shearman committed
442 443
	    *index);
	return NULL;
444
    }
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

    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
463
       TRACE("no image list\n");
464

Robert Shearman's avatar
Robert Shearman committed
465
    return himl;
466 467 468
}


Alexandre Julliard's avatar
Alexandre Julliard committed
469
static void
470
TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
471
{
472 473
    RECT myrect;
    COLORREF oldcolor, newcolor;
Alexandre Julliard's avatar
Alexandre Julliard committed
474

475 476 477 478 479
    myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
    myrect.right = myrect.left + 1;
    myrect.top = lpRect->top + 2;
    myrect.bottom = lpRect->bottom - 2;

480
    newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
481
	        comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
482
    oldcolor = SetBkColor (hdc, newcolor);
483
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
484 485 486 487

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

488
    newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
489
	        comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
490
    SetBkColor (hdc, newcolor);
491
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
492 493

    SetBkColor (hdc, oldcolor);
Alexandre Julliard's avatar
Alexandre Julliard committed
494 495
}

496 497

/***********************************************************************
498
* 		TOOLBAR_DrawFlatHorizontalSeparator
499
*
500
* This function draws horizontal separator for toolbars having CCS_VERT style.
501 502 503 504 505 506 507 508
* 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
509
TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
510
                             const TOOLBAR_INFO *infoPtr)
511 512 513 514 515 516 517 518 519 520 521
{
    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);

522
    TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
523

524
    newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
525
	        comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
526
    oldcolor = SetBkColor (hdc, newcolor);
527
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
528 529 530 531

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

532
    newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
533
	        comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
534
    SetBkColor (hdc, newcolor);
535
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
536 537 538 539 540

    SetBkColor (hdc, oldcolor);
}


541
static void
Robert Shearman's avatar
Robert Shearman committed
542
TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
543 544
{
    INT x, y;
545 546
    HPEN hPen, hOldPen;

Robert Shearman's avatar
Robert Shearman committed
547
    if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
548
    hOldPen = SelectObject ( hdc, hPen );
549
    x = left + 2;
550
    y = top;
551 552 553 554 555
    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);
556
    LineTo (hdc, x+1, y);
557 558
    SelectObject( hdc, hOldPen );
    DeleteObject( hPen );
559 560
}

561 562
/*
 * Draw the text string for this button.
563 564 565
 * 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
566
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
567
static void
568 569
TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
                    const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
570
{
Robert Shearman's avatar
Robert Shearman committed
571
    HDC hdc = tbcd->nmcd.hdc;
572 573
    HFONT  hOldFont = 0;
    COLORREF clrOld = 0;
Robert Shearman's avatar
Robert Shearman committed
574
    COLORREF clrOldBk = 0;
575
    int oldBkMode = 0;
576
    UINT state = tbcd->nmcd.uItemState;
Alexandre Julliard's avatar
Alexandre Julliard committed
577 578

    /* draw text */
579
    if (lpText && infoPtr->nMaxTextRows > 0) {
580 581
        TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
              wine_dbgstr_rect(rcText));
582

583
	hOldFont = SelectObject (hdc, infoPtr->hFont);
584
	if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
585 586 587
	    clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
	}
	else if (state & CDIS_DISABLED) {
588 589 590
	    clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
	    OffsetRect (rcText, 1, 1);
	    DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
591
	    SetTextColor (hdc, comctl32_color.clr3dShadow);
592
	    OffsetRect (rcText, -1, -1);
Alexandre Julliard's avatar
Alexandre Julliard committed
593
	}
594
	else if (state & CDIS_INDETERMINATE) {
595 596
	    clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
	}
597
	else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
598
	    clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
Robert Shearman's avatar
Robert Shearman committed
599
	    clrOldBk = SetBkColor (hdc, tbcd->clrMark);
600
	    oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
Robert Shearman's avatar
Robert Shearman committed
601
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
602
	else {
603
	    clrOld = SetTextColor (hdc, tbcd->clrText);
Alexandre Julliard's avatar
Alexandre Julliard committed
604 605
	}

606
	DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
607
	SetTextColor (hdc, clrOld);
608
	if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
609
	{
Robert Shearman's avatar
Robert Shearman committed
610
	    SetBkColor (hdc, clrOldBk);
611 612
	    SetBkMode (hdc, oldBkMode);
	}
613
	SelectObject (hdc, hOldFont);
Alexandre Julliard's avatar
Alexandre Julliard committed
614 615 616 617
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
618
static void
619
TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
Alexandre Julliard's avatar
Alexandre Julliard committed
620
{
621
    HDC hdc = tbcd->nmcd.hdc;
Robert Shearman's avatar
Robert Shearman committed
622 623 624
    HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
    COLORREF clrTextOld;
    COLORREF clrBkOld;
625 626
    INT cx = lpRect->right - lpRect->left;
    INT cy = lpRect->bottom - lpRect->top;
627 628
    INT cxEdge = GetSystemMetrics(SM_CXEDGE);
    INT cyEdge = GetSystemMetrics(SM_CYEDGE);
Robert Shearman's avatar
Robert Shearman committed
629 630
    clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
    clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
631 632
    PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
            cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
Robert Shearman's avatar
Robert Shearman committed
633 634
    SetBkColor(hdc, clrBkOld);
    SetTextColor(hdc, clrTextOld);
635
    SelectObject (hdc, hbr);
Alexandre Julliard's avatar
Alexandre Julliard committed
636 637 638
}


639
static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
Alexandre Julliard's avatar
Alexandre Julliard committed
640
{
641 642 643
    INT cx, cy;
    HBITMAP hbmMask, hbmImage;
    HDC hdcMask, hdcImage;
644

645 646 647 648
    ImageList_GetIconSize(himl, &cx, &cy);

    /* Create src image */
    hdcImage = CreateCompatibleDC(hdc);
649
    hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
650 651 652 653 654 655 656 657 658 659
    SelectObject(hdcImage, hbmImage);
    ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
                     RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);

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

    /* Remove the background and all white pixels */
660 661
    ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
                     RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    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
678 679 680
}


681
static UINT
682
TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
683 684 685 686 687 688 689 690
{
    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;
    retstate |= (btnPtr->bHot                     ) ? CDIS_HOT      : 0;
691
    retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
692
    /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
693 694 695
    return retstate;
}

Robert Shearman's avatar
Robert Shearman committed
696 697
/* draws the image on a toolbar button */
static void
698 699
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
700 701 702 703 704
{
    HIMAGELIST himl = NULL;
    BOOL draw_masked = FALSE;
    INT index;
    INT offset = 0;
705
    UINT draw_flags = ILD_TRANSPARENT;
Robert Shearman's avatar
Robert Shearman committed
706

707
    if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
Robert Shearman's avatar
Robert Shearman committed
708 709 710
    {
        himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
        if (!himl)
711 712 713 714
        {
            himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
            draw_masked = TRUE;
        }
Robert Shearman's avatar
Robert Shearman committed
715
    }
716 717 718
    else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
      ((tbcd->nmcd.uItemState & CDIS_HOT) 
      && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
Robert Shearman's avatar
Robert Shearman committed
719 720 721 722 723 724 725 726 727 728
    {
        /* 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);

729 730 731
    if (!himl)
        return;

732
    if (!(dwItemCDFlag & TBCDRF_NOOFFSET) && 
Robert Shearman's avatar
Robert Shearman committed
733 734 735
        (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
        offset = 1;

736
    if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
Robert Shearman's avatar
Robert Shearman committed
737 738 739 740 741 742 743
        (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)
744 745
        TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
    else
Robert Shearman's avatar
Robert Shearman committed
746 747 748 749 750
        ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
}

/* draws a blank frame for a toolbar button */
static void
751
TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
752 753 754 755 756 757 758 759 760 761 762 763
{
    HDC hdc = tbcd->nmcd.hdc;
    RECT rc = tbcd->nmcd.rc;
    /* 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 */
764
    if (dwItemCDFlag & TBCDRF_NOEDGES)
Robert Shearman's avatar
Robert Shearman committed
765 766
        return;

767
    if (infoPtr->dwStyle & TBSTYLE_FLAT)
Robert Shearman's avatar
Robert Shearman committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    {
        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
785
TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
786 787 788
{
    HDC hdc = tbcd->nmcd.hdc;
    int offset = 0;
789 790
    BOOL pressed = bDropDownPressed ||
        (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
Robert Shearman's avatar
Robert Shearman committed
791

792
    if (infoPtr->dwStyle & TBSTYLE_FLAT)
Robert Shearman's avatar
Robert Shearman committed
793
    {
794
        if (pressed)
795
            DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
Robert Shearman's avatar
Robert Shearman committed
796 797 798 799 800 801 802
        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
    {
803
        if (pressed)
804
            DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
Robert Shearman's avatar
Robert Shearman committed
805 806
        else
            DrawEdge (hdc, rcArrow, EDGE_RAISED,
807
              BF_SOFT | BF_RECT | BF_MIDDLE);
Robert Shearman's avatar
Robert Shearman committed
808 809
    }

810
    if (pressed)
811
        offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
Robert Shearman's avatar
Robert Shearman committed
812 813 814 815 816 817 818 819 820

    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);
}
821

Robert Shearman's avatar
Robert Shearman committed
822
/* draws a complete toolbar button */
Alexandre Julliard's avatar
Alexandre Julliard committed
823
static void
824
TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
Alexandre Julliard's avatar
Alexandre Julliard committed
825
{
826
    DWORD dwStyle = infoPtr->dwStyle;
827 828 829 830
    BOOL hasDropDownArrow = (TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) &&
                            (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
                            (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
    BOOL drawSepDropDownArrow = hasDropDownArrow && 
Robert Shearman's avatar
Robert Shearman committed
831 832
                                (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
    RECT rc, rcArrow, rcBitmap, rcText;
833 834 835 836
    LPWSTR lpText = NULL;
    NMTBCUSTOMDRAW tbcd;
    DWORD ntfret;
    INT offset;
837
    INT oldBkMode;
838
    DWORD dwItemCustDraw;
839
    DWORD dwItemCDFlag;
840
    HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
841 842

    rc = btnPtr->rect;
843 844
    CopyRect (&rcArrow, &rc);

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    /* 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) */) {
860 861
            if (dwStyle & CCS_VERT)
               TOOLBAR_DrawFlatHorizontalSeparator (&rc, hdc, infoPtr);
862 863 864 865 866 867 868 869 870 871
	    else
		TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
	}
	else if (btnPtr->fsStyle != BTNS_SEP) {
	    FIXME("Draw some kind of separator: fsStyle=%x\n",
		  btnPtr->fsStyle);
	}
	return;
    }

872 873
    /* get a pointer to the text */
    lpText = TOOLBAR_GetText(infoPtr, btnPtr);
874 875 876

    if (hasDropDownArrow)
    {
877 878
        int right;

Robert Shearman's avatar
Robert Shearman committed
879
        if (dwStyle & TBSTYLE_FLAT)
880
            right = max(rc.left, rc.right - DDARROW_WIDTH);
Robert Shearman's avatar
Robert Shearman committed
881
        else
882 883 884 885 886
            right = max(rc.left, rc.right - DDARROW_WIDTH - 2);

        if (drawSepDropDownArrow)
           rc.right = right;

Robert Shearman's avatar
Robert Shearman committed
887
        rcArrow.left = right;
888
    }
889

890
    /* copy text & bitmap rects after adjusting for drop-down arrow
891
     * so that text & bitmap is centered in the rectangle not containing
892 893
     * the arrow */
    CopyRect(&rcText, &rc);
894
    CopyRect(&rcBitmap, &rc);
895

896
    /* Center the bitmap horizontally and vertically */
897
    if (dwStyle & TBSTYLE_LIST)
898 899 900 901 902 903 904 905 906
    {
        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;
    }
907
    else
908
        rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
909

910
    rcBitmap.top += infoPtr->szPadding.cy / 2;
911

912
    TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
Robert Shearman's avatar
Robert Shearman committed
913 914
      btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
      infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
915
    TRACE("Text=%s\n", debugstr_w(lpText));
916
    TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
917

918 919 920 921 922 923
    /* calculate text position */
    if (lpText)
    {
        rcText.left += GetSystemMetrics(SM_CXEDGE);
        rcText.right -= GetSystemMetrics(SM_CXEDGE);
        if (dwStyle & TBSTYLE_LIST)
Robert Shearman's avatar
Robert Shearman committed
924
        {
925
            rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
Robert Shearman's avatar
Robert Shearman committed
926 927
        }
        else
928 929 930 931 932 933
        {
            if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
                rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
            else
                rcText.top += infoPtr->szPadding.cy/2 + 2;
        }
934 935
    }

Robert Shearman's avatar
Robert Shearman committed
936 937 938
    /* Initialize fields in all cases, because we use these later
     * NOTE: applications can and do alter these to customize their
     * toolbars */
939 940 941 942 943 944 945
    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;
946 947
    tbcd.nStringBkMode = TRANSPARENT;
    tbcd.nHLStringBkMode = OPAQUE;
Robert Shearman's avatar
Robert Shearman committed
948 949 950 951 952
    /* MSDN says that this is the text rectangle.
     * But (why always a but) tracing of v5.7 of native shows
     * that this is really a *relative* rectangle based on the
     * the nmcd.rc. Also the left and top are always 0 ignoring
     * any bitmap that might be present. */
953 954 955 956
    tbcd.rcText.left = 0;
    tbcd.rcText.top = 0;
    tbcd.rcText.right = rcText.right - rc.left;
    tbcd.rcText.bottom = rcText.bottom - rc.top;
957
    tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
958
    tbcd.nmcd.hdc = hdc;
Robert Shearman's avatar
Robert Shearman committed
959
    tbcd.nmcd.rc = rc;
960
    tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
961

Robert Shearman's avatar
Robert Shearman committed
962
    /* FIXME: what are these used for? */
963 964 965
    tbcd.hbrLines = 0;
    tbcd.hpenLines = 0;

966
    /* Issue Item Prepaint notify */
967
    dwItemCustDraw = 0;
968 969
    dwItemCDFlag = 0;
    if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
970 971 972 973
    {
	tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
	tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
	tbcd.nmcd.lItemlParam = btnPtr->dwData;
974
	ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
Robert Shearman's avatar
Robert Shearman committed
975 976 977 978
        /* reset these fields so the user can't alter the behaviour like native */
        tbcd.nmcd.hdc = hdc;
        tbcd.nmcd.rc = rc;

979
	dwItemCustDraw = ntfret & 0xffff;
980
	dwItemCDFlag = ntfret & 0xffff0000;
981
	if (dwItemCustDraw & CDRF_SKIPDEFAULT)
982 983 984 985 986 987
	    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;
    }

988
    if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
989 990 991
        (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
        OffsetRect(&rcText, 1, 1);

Robert Shearman's avatar
Robert Shearman committed
992 993 994 995
    if (!(tbcd.nmcd.uItemState & CDIS_HOT) && 
        ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
        TOOLBAR_DrawPattern (&rc, &tbcd);

996 997
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
        && (tbcd.nmcd.uItemState & CDIS_HOT))
998
    {
999
        if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
Robert Shearman's avatar
Robert Shearman committed
1000
        {
1001 1002 1003
            COLORREF oldclr;

            oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1004
            ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1005
            if (hasDropDownArrow)
1006
                ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1007 1008 1009
            SetBkColor(hdc, oldclr);
        }
    }
1010

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    if (theme)
    {
        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_HOT;
        else if ((tbcd.nmcd.uItemState & CDIS_HOT)
            || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
            stateId = TS_HOT;
            
        DrawThemeBackground (theme, hdc, partId, stateId, &tbcd.nmcd.rc, NULL);
    }
    else
1029
        TOOLBAR_DrawFrame(infoPtr, &tbcd, dwItemCDFlag);
1030

Robert Shearman's avatar
Robert Shearman committed
1031
    if (drawSepDropDownArrow)
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    {
        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)
                stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
            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
1050
            TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1051
    }
1052

1053
    oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
Robert Shearman's avatar
Robert Shearman committed
1054
    if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1055
        TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1056
    SetBkMode (hdc, oldBkMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
1057

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

Robert Shearman's avatar
Robert Shearman committed
1060
    if (hasDropDownArrow && !drawSepDropDownArrow)
1061
    {
Robert Shearman's avatar
Robert Shearman committed
1062 1063 1064 1065 1066 1067 1068
        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))
        {
1069
            offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
Robert Shearman's avatar
Robert Shearman committed
1070 1071 1072 1073
            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);
1074
    }
1075

1076
    if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1077
    {
1078 1079
        tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
        TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1080
    }
1081

Alexandre Julliard's avatar
Alexandre Julliard committed
1082 1083 1084 1085
}


static void
1086
TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
Alexandre Julliard's avatar
Alexandre Julliard committed
1087 1088
{
    TBUTTON_INFO *btnPtr;
1089
    INT i;
1090
    RECT rcTemp, rcClient;
1091 1092
    NMTBCUSTOMDRAW tbcd;
    DWORD ntfret;
1093
    DWORD dwBaseCustDraw;
Alexandre Julliard's avatar
Alexandre Julliard committed
1094

Robert Shearman's avatar
Robert Shearman committed
1095 1096 1097 1098
    /* the app has told us not to redraw the toolbar */
    if (!infoPtr->bDoRedraw)
        return;

1099 1100
    /* if imagelist belongs to the app, it can be changed
       by the app after setting it */
1101 1102 1103 1104 1105 1106
    if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
    {
        infoPtr->nNumBitmaps = 0;
        for (i = 0; i < infoPtr->cimlDef; i++)
            infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
    }
1107 1108 1109

    TOOLBAR_DumpToolbar (infoPtr, __LINE__);

1110 1111 1112
    /* change the imagelist icon size if we manage the list and it is necessary */
    TOOLBAR_CheckImageListIconSize(infoPtr);

1113 1114 1115 1116 1117
    /* Send initial notify */
    ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
    tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
    tbcd.nmcd.hdc = hdc;
    tbcd.nmcd.rc = ps->rcPaint;
1118
    ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1119
    dwBaseCustDraw = ntfret & 0xffff;
1120

1121
    GetClientRect(infoPtr->hwndSelf, &rcClient);
1122

1123
    /* redraw necessary buttons */
Alexandre Julliard's avatar
Alexandre Julliard committed
1124
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1125
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1126
    {
1127
        BOOL bDraw;
1128 1129
        if (!RectVisible(hdc, &btnPtr->rect))
            continue;
1130 1131 1132 1133 1134 1135 1136 1137
        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
1138
        bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1139
        if (bDraw)
1140
            TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1141
    }
1142

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    /* 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);
    }

1157
    if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1158 1159 1160 1161 1162
    {
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
	tbcd.nmcd.hdc = hdc;
	tbcd.nmcd.rc = ps->rcPaint;
1163
	TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1164
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1165 1166
}

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
/***********************************************************************
* 		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
1177
* only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1178
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1179
static void
1180
TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1181
		      HDC hdc, LPSIZE lpSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
1182
{
1183
    RECT myrect;
1184

Alexandre Julliard's avatar
Alexandre Julliard committed
1185 1186
    lpSize->cx = 0;
    lpSize->cy = 0;
1187

1188 1189
    if (infoPtr->nMaxTextRows > 0 &&
        !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1190 1191
        (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
        (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1192
    {
1193
        LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1194

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	if(lpText != NULL) {
	    /* first get size of all the text */
	    GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);

	    /* feed above size into the rectangle for DrawText */
	    myrect.left = myrect.top = 0;
	    myrect.right = lpSize->cx;
	    myrect.bottom = lpSize->cy;

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

1209 1210 1211 1212
	    /* feed back to caller  */
	    lpSize->cx = myrect.right;
	    lpSize->cy = myrect.bottom;
	}
1213
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1214

1215
    TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
Alexandre Julliard's avatar
Alexandre Julliard committed
1216 1217
}

1218 1219 1220 1221 1222 1223
/***********************************************************************
* 		TOOLBAR_CalcStrings
*
* This function walks through each string and measures it and returns
* the largest height and width to caller.
*/
1224
static void
1225
TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1226 1227 1228 1229
{
    TBUTTON_INFO *btnPtr;
    INT i;
    SIZE sz;
1230 1231
    HDC hdc;
    HFONT hOldFont;
1232 1233 1234 1235

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

1236
    if (infoPtr->nMaxTextRows == 0)
Huw Davies's avatar
Huw Davies committed
1237 1238
        return;

1239
    hdc = GetDC (infoPtr->hwndSelf);
1240 1241
    hOldFont = SelectObject (hdc, infoPtr->hFont);

1242
    if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1243 1244 1245 1246 1247 1248 1249
    {
        TEXTMETRICW tm;

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

1250 1251
    btnPtr = infoPtr->buttons;
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1252
        if(TOOLBAR_GetText(infoPtr, btnPtr))
1253
        {
1254
            TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1255 1256 1257 1258 1259
            if (sz.cx > lpSize->cx)
                lpSize->cx = sz.cx;
            if (sz.cy > lpSize->cy)
                lpSize->cy = sz.cy;
        }
1260 1261
    }

1262
    SelectObject (hdc, hOldFont);
1263
    ReleaseDC (infoPtr->hwndSelf, hdc);
1264

1265
    TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1266 1267
}

1268 1269 1270
/***********************************************************************
* 		TOOLBAR_WrapToolbar
*
Francois Gouget's avatar
Francois Gouget committed
1271
* This function walks through the buttons and separators in the
1272 1273 1274 1275 1276
* 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
1277
* flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1278
*
1279 1280 1281
* Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_UNDOC1 can be used also to allow
* vertical toolbar lists.
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1282

Alexandre Julliard's avatar
Alexandre Julliard committed
1283
static void
1284
TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
1285
{
1286 1287 1288
    TBUTTON_INFO *btnPtr;
    INT x, cx, i, j;
    RECT rc;
1289
    BOOL bButtonWrap;
1290

1291
    /* 	When the toolbar window style is not TBSTYLE_WRAPABLE,	*/
1292 1293
    /*	no layout is necessary. Applications may use this style */
    /*	to perform their own layout on the toolbar. 		*/
1294
    if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1295
	!(infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1) )  return;
1296 1297 1298 1299

    btnPtr = infoPtr->buttons;
    x  = infoPtr->nIndent;

1300
    if (GetParent(infoPtr->hwndSelf))
1301 1302 1303 1304 1305
    {
        /* this can get the parents width, to know how far we can extend
         * this toolbar.  We cannot use its height, as there may be multiple
         * toolbars in a rebar control
         */
1306
        GetClientRect( GetParent(infoPtr->hwndSelf), &rc );
1307 1308 1309 1310
        infoPtr->nWidth = rc.right - rc.left;
    }
    else
    {
1311
        GetWindowRect( infoPtr->hwndSelf, &rc );
1312 1313 1314
        infoPtr->nWidth = rc.right - rc.left;
    }

1315 1316
    bButtonWrap = FALSE;

1317 1318 1319 1320
    TRACE("start ButtonWidth=%d, BitmapWidth=%d, nWidth=%d, nIndent=%d\n",
	  infoPtr->nButtonWidth, infoPtr->nBitmapWidth, infoPtr->nWidth,
	  infoPtr->nIndent);

1321 1322 1323
    for (i = 0; i < infoPtr->nNumButtons; i++ )
    {
	btnPtr[i].fsState &= ~TBSTATE_WRAP;
1324

1325 1326 1327
	if (btnPtr[i].fsState & TBSTATE_HIDDEN)
	    continue;

1328 1329
        if (btnPtr[i].cx > 0)
            cx = btnPtr[i].cx;
1330
        /* horizontal separators are treated as buttons for width    */
1331
	else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1332
            !(infoPtr->dwStyle & CCS_VERT))
1333
            cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1334
	else
1335
	    cx = infoPtr->nButtonWidth;
1336

1337
	/* Two or more adjacent separators form a separator group.   */
1338 1339 1340
	/* The first separator in a group should be wrapped to the   */
	/* next row if the previous wrapping is on a button.	     */
	if( bButtonWrap &&
1341
		(btnPtr[i].fsStyle & BTNS_SEP) &&
1342
		(i + 1 < infoPtr->nNumButtons ) &&
1343
		(btnPtr[i + 1].fsStyle & BTNS_SEP) )
1344
	{
1345
	    TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1346 1347 1348 1349 1350 1351 1352 1353
	    btnPtr[i].fsState |= TBSTATE_WRAP;
	    x = infoPtr->nIndent;
	    i++;
	    bButtonWrap = FALSE;
	    continue;
	}

	/* The layout makes sure the bitmap is visible, but not the button. */
1354 1355
	/* Test added to also wrap after a button that starts a row but     */
	/* is bigger than the area.  - GA  8/01                             */
1356
	if (( x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2
1357 1358
	   > infoPtr->nWidth ) ||
	    ((x == infoPtr->nIndent) && (cx > infoPtr->nWidth)))
1359 1360 1361
	{
	    BOOL bFound = FALSE;

1362
	    /* 	If the current button is a separator and not hidden,  */
1363 1364
	    /*	go to the next until it reaches a non separator.      */
	    /*	Wrap the last separator if it is before a button.     */
1365 1366
	    while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
		      !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1367
		     (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1368 1369 1370 1371 1372
			i < infoPtr->nNumButtons )
	    {
		i++;
		bFound = TRUE;
	    }
1373

1374 1375 1376
	    if( bFound && i < infoPtr->nNumButtons )
	    {
		i--;
1377
		TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1378
		      i, btnPtr[i].fsStyle, x, cx);
1379 1380 1381 1382 1383 1384 1385 1386
		btnPtr[i].fsState |= TBSTATE_WRAP;
		x = infoPtr->nIndent;
		bButtonWrap = FALSE;
		continue;
	    }
	    else if ( i >= infoPtr->nNumButtons)
		break;

1387
	    /* 	If the current button is not a separator, find the last  */
1388 1389 1390
	    /*	separator and wrap it.   				 */
	    for ( j = i - 1; j >= 0  &&  !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
	    {
1391
		if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1392 1393
			!(btnPtr[j].fsState & TBSTATE_HIDDEN))
		{
1394 1395 1396
		    bFound = TRUE;
		    i = j;
		    TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1397
			  i, btnPtr[i].fsStyle, x, cx);
1398 1399
		    x = infoPtr->nIndent;
		    btnPtr[j].fsState |= TBSTATE_WRAP;
1400
		    bButtonWrap = FALSE;
1401 1402 1403 1404 1405 1406 1407 1408
		    break;
		}
	    }

	    /* 	If no separator available for wrapping, wrap one of 	*/
	    /*  non-hidden previous button.  			     	*/
	    if (!bFound)
	    {
1409
		for ( j = i - 1;
1410 1411
			j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
		{
1412
		    if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1413 1414
			continue;

1415 1416 1417
		    bFound = TRUE;
		    i = j;
		    TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1418
			  i, btnPtr[i].fsStyle, x, cx);
1419 1420 1421 1422 1423 1424 1425 1426
		    x = infoPtr->nIndent;
		    btnPtr[j].fsState |= TBSTATE_WRAP;
		    bButtonWrap = TRUE;
		    break;
		}
	    }

	    /* If all above failed, wrap the current button. */
1427
	    if (!bFound)
1428
	    {
1429 1430
		TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
		      i, btnPtr[i].fsStyle, x, cx);
1431 1432
		btnPtr[i].fsState |= TBSTATE_WRAP;
		x = infoPtr->nIndent;
1433
		if (btnPtr[i].fsStyle & BTNS_SEP )
1434 1435 1436
		    bButtonWrap = FALSE;
		else
		    bButtonWrap = TRUE;
1437
	    }
1438
	}
1439
	else {
1440
	    TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1441
		  i, btnPtr[i].fsStyle, x, cx);
1442
	    x += cx;
1443
	}
1444 1445
    }
}
1446 1447


1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
/***********************************************************************
* 		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:
*
* +--------------------------------------------------------+ ^
* |                    ^                     ^             | |
1472
* |                    | pad.cy / 2          | centered    | |
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
* | 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):
*
* +-----------------------------------+ ^
* |                     ^             | |
1488
* |                     | centered    | | LISTPAD_CY +
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
* |                   +------------+  | | 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
1526
* |    centered       |   Bitmap   |  | |
1527 1528 1529 1530 1531
* |<----------------->|            |  | |
* |                   +------------+  | |
* |                         ^         | |
* |                       1 |         | |
* |                         -         | |
1532
* |     centered    +---------------+ | |
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
* |<--------------->|      Text     | | |
* |                 +---------------+ | |
* +-----------------------------------+ -
* <----------------------------------->
* pad.cx + max(nBitmapWidth, szText.cx)
*
* Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
*
* +---------------------------------------+ ^
* |                     ^                 | |
* |                     | 2 + pad.cy / 2  | |
* |                     -                 | | szText.cy +
1545
* |    centered     +-----------------+   | | pad.cy + 2
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
* |<--------------->|   Text          |   | |
* |                 +-----------------+   | |
* |                                       | |
* +---------------------------------------+ -
* <--------------------------------------->
*          2*cxedge + pad.cx + szText.cx
*
* Without text:
*   As for with bitmaps, but with szText.cx zero.
*/
1556 1557
static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
                                         BOOL bHasBitmap, BOOL bValidImageList)
1558 1559 1560 1561 1562
{
    SIZE sizeButton;
    if (infoPtr->dwStyle & TBSTYLE_LIST)
    {
        /* set button height from bitmap / text height... */
1563
        sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
            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 */
1578 1579 1580 1581 1582
        sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
            infoPtr->nBitmapWidth + infoPtr->iListGap;
        if (sizeString.cx > 0)
            sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;

1583 1584 1585 1586 1587
    }
    else
    {
        if (bHasBitmap)
        {
1588
            sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1589 1590
            if (sizeString.cy > 0)
                sizeButton.cy += 1 + sizeString.cy;
1591 1592 1593 1594 1595 1596 1597
            sizeButton.cx = infoPtr->szPadding.cx +
                max(sizeString.cx, infoPtr->nBitmapWidth);
        }
        else
        {
            sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
                NONLIST_NOTEXT_OFFSET;
1598 1599
            sizeButton.cx = infoPtr->szPadding.cx +
                max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1600 1601 1602 1603 1604 1605
        }
    }
    return sizeButton;
}


1606 1607 1608
/***********************************************************************
* 		TOOLBAR_CalcToolbar
*
1609 1610 1611
* 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
1612
* on. It assigns a new location to each item and sends this location to
1613 1614 1615
* the tooltip window if appropriate. Finally, it updates the rcBound
* rect and calculates the new required toolbar window height.
*/
1616
static void
1617
TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1618
{
1619 1620
    SIZE  sizeString, sizeButton;
    BOOL validImageList = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1621

1622
    TOOLBAR_CalcStrings (infoPtr, &sizeString);
Alexandre Julliard's avatar
Alexandre Julliard committed
1623

1624 1625
    TOOLBAR_DumpToolbar (infoPtr, __LINE__);

1626 1627
    if (TOOLBAR_IsValidImageList(infoPtr, 0))
        validImageList = TRUE;
1628
    sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1629 1630
    infoPtr->nButtonWidth = sizeButton.cx;
    infoPtr->nButtonHeight = sizeButton.cy;
1631
    infoPtr->iTopMargin = default_top_margin(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1632

1633 1634
    if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
        infoPtr->nButtonWidth = infoPtr->cxMin;
1635
    if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1636 1637
        infoPtr->nButtonWidth = infoPtr->cxMax;

1638
    TOOLBAR_LayoutToolbar(infoPtr);
1639
}
1640

1641
static void
1642
TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1643 1644 1645 1646 1647 1648 1649 1650 1651
{
    TBUTTON_INFO *btnPtr;
    SIZE sizeButton;
    INT i, nRows, nSepRows;
    INT x, y, cx, cy;
    BOOL bWrap;
    BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
    BOOL hasDropDownArrows = TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle);

1652
    TOOLBAR_WrapToolbar(infoPtr);
1653

1654
    x  = infoPtr->nIndent;
1655
    y  = infoPtr->iTopMargin;
Alexandre Julliard's avatar
Alexandre Julliard committed
1656
    cx = infoPtr->nButtonWidth;
1657
    cy = infoPtr->nButtonHeight;
1658

1659
    nRows = nSepRows = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1660 1661 1662 1663 1664

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

    btnPtr = infoPtr->buttons;
1667

1668 1669
    TRACE("cy=%d\n", cy);

1670 1671 1672 1673 1674
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
    {
	bWrap = FALSE;
	if (btnPtr->fsState & TBSTATE_HIDDEN)
	{
1675
	    SetRectEmpty (&btnPtr->rect);
Alexandre Julliard's avatar
Alexandre Julliard committed
1676
	    continue;
1677 1678
	}

1679
	cy = infoPtr->nButtonHeight;
1680

1681
	if (btnPtr->fsStyle & BTNS_SEP) {
1682
	    if (infoPtr->dwStyle & CCS_VERT) {
1683 1684
                cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
                cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nWidth;
1685 1686
	    }
	    else
1687 1688
                cx = (btnPtr->cx > 0) ? btnPtr->cx :
                    (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1689
	}
1690 1691
	else
	{
Robert Shearman's avatar
Robert Shearman committed
1692 1693 1694
            if (btnPtr->cx)
              cx = btnPtr->cx;
            else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || 
1695
                (btnPtr->fsStyle & BTNS_AUTOSIZE))
1696 1697
            {
              SIZE sz;
1698 1699 1700
	      HDC hdc;
	      HFONT hOldFont;

1701
	      hdc = GetDC (infoPtr->hwndSelf);
1702 1703 1704 1705 1706
	      hOldFont = SelectObject (hdc, infoPtr->hFont);

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

	      SelectObject (hdc, hOldFont);
1707
	      ReleaseDC (infoPtr->hwndSelf, hdc);
1708

1709 1710 1711 1712
              sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
                  TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
                  validImageList);
              cx = sizeButton.cx;
1713 1714 1715
            }
            else
	      cx = infoPtr->nButtonWidth;
1716

Robert Shearman's avatar
Robert Shearman committed
1717 1718 1719 1720
            /* if size has been set manually then don't add on extra space
             * for the drop down arrow */
	    if (!btnPtr->cx && hasDropDownArrows && 
                ((btnPtr->fsStyle & BTNS_DROPDOWN) || (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)))
1721
	      cx += DDARROW_WIDTH;
1722
	}
1723
	if (btnPtr->fsState & TBSTATE_WRAP )
1724 1725
		    bWrap = TRUE;

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

Alexandre Julliard's avatar
Alexandre Julliard committed
1728 1729 1730 1731 1732 1733
	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
1734

1735
        TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1736

1737 1738 1739
	/* btnPtr->nRow is zero based. The space between the rows is 	*/
	/* also considered as a row. 					*/
	btnPtr->nRow = nRows + nSepRows;
1740

1741 1742 1743
	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);
1744

1745 1746
	if( bWrap )
	{
1747
	    if ( !(btnPtr->fsStyle & BTNS_SEP) )
1748
	        y += cy;
1749 1750
	    else
	    {
1751
               if ( !(infoPtr->dwStyle & CCS_VERT))
1752 1753
                    y += cy + ( (btnPtr->cx > 0 ) ?
                                btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1754 1755
		else
		    y += cy;
1756

1757
		/* nSepRows is used to calculate the extra height following  */
1758 1759 1760 1761
		/* the last row.					     */
		nSepRows++;
	    }
	    x = infoPtr->nIndent;
1762 1763 1764 1765

	    /* 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
1766 1767
		nRows++;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1768
	else
Alexandre Julliard's avatar
Alexandre Julliard committed
1769
	    x += cx;
Alexandre Julliard's avatar
Alexandre Julliard committed
1770
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1771

1772 1773 1774
    /* infoPtr->nRows is the number of rows on the toolbar */
    infoPtr->nRows = nRows + nSepRows + 1;

1775
    TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
Alexandre Julliard's avatar
Alexandre Julliard committed
1776 1777 1778
}


1779
static INT
1780
TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
Alexandre Julliard's avatar
Alexandre Julliard committed
1781 1782
{
    TBUTTON_INFO *btnPtr;
1783
    INT i;
1784

1785 1786 1787
    if (button)
        *button = FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1788
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1789
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1790 1791 1792
	if (btnPtr->fsState & TBSTATE_HIDDEN)
	    continue;

1793
	if (btnPtr->fsStyle & BTNS_SEP) {
1794
	    if (PtInRect (&btnPtr->rect, *lpPt)) {
1795
		TRACE(" ON SEPARATOR %d!\n", i);
Alexandre Julliard's avatar
Alexandre Julliard committed
1796 1797 1798 1799
		return -i;
	    }
	}
	else {
1800
	    if (PtInRect (&btnPtr->rect, *lpPt)) {
1801
		TRACE(" ON BUTTON %d!\n", i);
1802 1803
                if (button)
                    *button = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1804 1805 1806 1807 1808
		return i;
	    }
	}
    }

1809
    TRACE(" NOWHERE!\n");
1810
    return TOOLBAR_NOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1811 1812 1813
}


1814 1815
/* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
static BOOL
1816
TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
{
    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];
1835

1836
        TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1837

1838
        ZeroMemory(btnPtr, sizeof(*btnPtr));
1839

1840
        btnPtr->iBitmap   = lpTbb[iButton].iBitmap;
1841 1842 1843 1844
        btnPtr->idCommand = lpTbb[iButton].idCommand;
        btnPtr->fsState   = lpTbb[iButton].fsState;
        btnPtr->fsStyle   = lpTbb[iButton].fsStyle;
        btnPtr->dwData    = lpTbb[iButton].dwData;
1845 1846
        if (btnPtr->fsStyle & BTNS_SEP)
            btnPtr->iString = -1;
1847
        else if(!IS_INTRESOURCE(lpTbb[iButton].iString) && lpTbb[iButton].iString != -1)
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
        {
            if (fUnicode)
                Str_SetPtrW((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[iButton].iString );
            else
                Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[iButton].iString);
            fHasString = TRUE;
        }
        else
            btnPtr->iString   = lpTbb[iButton].iString;

        TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
    }

    if (infoPtr->nNumStrings > 0 || fHasString)
1862
        TOOLBAR_CalcToolbar(infoPtr);
1863
    else
1864 1865
        TOOLBAR_LayoutToolbar(infoPtr);
    TOOLBAR_AutoSize(infoPtr);
1866 1867 1868 1869 1870 1871 1872

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


1873
static INT
1874
TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
1875 1876
{
    TBUTTON_INFO *btnPtr;
1877
    INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
1878

1879 1880
    if (CommandIsIndex) {
	TRACE("command is really index command=%d\n", idCommand);
1881
	if (idCommand >= infoPtr->nNumButtons) return -1;
1882 1883
	return idCommand;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1884
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1885
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1886
	if (btnPtr->idCommand == idCommand) {
1887
	    TRACE("command=%d index=%d\n", idCommand, i);
Alexandre Julliard's avatar
Alexandre Julliard committed
1888
	    return i;
Alexandre Julliard's avatar
Alexandre Julliard committed
1889
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1890
    }
1891
    TRACE("no index found for command=%d\n", idCommand);
Alexandre Julliard's avatar
Alexandre Julliard committed
1892 1893 1894 1895
    return -1;
}


1896
static INT
1897
TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
1898 1899
{
    TBUTTON_INFO *btnPtr;
1900
    INT nRunIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
1901 1902 1903 1904 1905 1906

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

    /* check index button */
    btnPtr = &infoPtr->buttons[nIndex];
1907
    if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1908 1909 1910 1911 1912 1913 1914 1915
	if (btnPtr->fsState & TBSTATE_CHECKED)
	    return nIndex;
    }

    /* check previous buttons */
    nRunIndex = nIndex - 1;
    while (nRunIndex >= 0) {
	btnPtr = &infoPtr->buttons[nRunIndex];
1916
	if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
	    if (btnPtr->fsState & TBSTATE_CHECKED)
		return nRunIndex;
	}
	else
	    break;
	nRunIndex--;
    }

    /* check next buttons */
    nRunIndex = nIndex + 1;
    while (nRunIndex < infoPtr->nNumButtons) {
1928
	btnPtr = &infoPtr->buttons[nRunIndex];
1929
	if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1930 1931 1932 1933 1934 1935 1936 1937
	    if (btnPtr->fsState & TBSTATE_CHECKED)
		return nRunIndex;
	}
	else
	    break;
	nRunIndex++;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1938 1939 1940 1941
    return -1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1942
static VOID
1943 1944
TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
		    WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1945
{
1946
    MSG msg;
Alexandre Julliard's avatar
Alexandre Julliard committed
1947 1948 1949 1950 1951 1952

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

1956
    SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
Alexandre Julliard's avatar
Alexandre Julliard committed
1957 1958
}

1959
static void
1960
TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
{
    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
1979
TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
{
    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);
    }
}

1993
static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
{
    /* 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);
    }
}

2009 2010 2011 2012 2013 2014 2015
/* Creates the tooltip control */
static void
TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
{
    int i;
    NMTOOLTIPSCREATED nmttc;

2016
    infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
            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]);
    }
}

2034 2035 2036 2037 2038 2039 2040 2041
/* 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
2042
    TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2043

2044
    count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2045 2046 2047 2048

    /* position 0 is always separator */
    for (i = 1; i < count; i++)
    {
2049
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2050 2051
        if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
        {
2052 2053
            i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
            SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2054 2055 2056 2057
            return;
        }
    }
    /* id higher than all others add to end */
2058 2059
    i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
    SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2060 2061
}

2062
static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2063
{
2064
    NMTOOLBARW nmtb;
2065 2066 2067 2068 2069 2070

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

    if (nIndexFrom == nIndexTo)
        return;

2071 2072 2073
    /* 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;
2074
    if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2075 2076 2077 2078
    {
        PCUSTOMBUTTON btnInfo;
        NMHDR hdr;
        HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2079
        int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2080

2081
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2082

2083 2084 2085 2086
        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);
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098

        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);

2099
        SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
Robert Shearman's avatar
Robert Shearman committed
2100
        SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2101 2102 2103 2104 2105

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

2106
static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2107
{
2108
    NMTOOLBARW nmtb;
2109 2110 2111

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

2112 2113 2114
    /* 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;
2115
    if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2116 2117 2118 2119 2120
    {
        PCUSTOMBUTTON btnInfo;
        NMHDR hdr;
        HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
        HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2121
        int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2122

2123
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2124 2125 2126 2127

        if (nIndexAvail != 0) /* index == 0 indicates separator */
        {
            /* remove from 'available buttons' list */
2128
            SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2129
            if (nIndexAvail == count-1)
2130
                SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2131
            else
2132
                SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2133 2134 2135 2136 2137 2138
        }
        else
        {
            PCUSTOMBUTTON btnNew;

            /* duplicate 'separator' button */
2139
            btnNew = Alloc(sizeof(CUSTOMBUTTON));
2140
            *btnNew = *btnInfo;
2141 2142 2143 2144
            btnInfo = btnNew;
        }

        /* insert into 'toolbar button' list */
2145 2146
        SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
        SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2147

2148
        SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2149 2150 2151 2152 2153

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

2154
static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2155 2156 2157 2158 2159 2160
{
    PCUSTOMBUTTON btnInfo;
    HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);

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

2161
    btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2162 2163 2164 2165 2166 2167

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

2168 2169
        SendMessageW(hwndList, LB_DELETESTRING, index, 0);
        SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2170

2171
        SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183

        /* 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 */
2184 2185
static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
                                                        const DRAGLISTINFO *pDLI)
2186 2187 2188 2189 2190 2191 2192
{
    HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
    switch (pDLI->uNotification)
    {
    case DL_BEGINDRAG:
    {
        INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2193
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2194 2195 2196 2197 2198 2199 2200
        /* no dragging for last item (separator) */
        if (nCurrentItem >= (nCount - 1)) return FALSE;
        return TRUE;
    }
    case DL_DRAGGING:
    {
        INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2201
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
        /* 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);
2228 2229
        INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
        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
2249
    case DL_CANCELDRAG:
2250 2251 2252 2253 2254
        /* Clear drag arrow */
        DrawInsert(hwnd, hwndList, -1);
        break;
    }

Robert Shearman's avatar
Robert Shearman committed
2255
    return 0;
2256 2257 2258
}

/* drag list notification function for available buttons list box */
2259 2260
static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
                                                      const DRAGLISTINFO *pDLI)
2261 2262 2263 2264 2265 2266 2267 2268 2269
{
    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);
2270
        INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2271
        /* no dragging past last item (separator) */
Robert Shearman's avatar
Robert Shearman committed
2272
        if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
        {
            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);
2297 2298
        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
2299
        if ((nIndexTo >= 0) && (nIndexTo < nCount))
2300 2301 2302 2303 2304 2305 2306
        {
            /* clear drag arrow */
            DrawInsert(hwnd, hwndList, -1);
            /* add item */
            TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
        }
    }
Robert Shearman's avatar
Robert Shearman committed
2307
    case DL_CANCELDRAG:
2308 2309 2310
        /* Clear drag arrow */
        DrawInsert(hwnd, hwndList, -1);
        break;
Robert Shearman's avatar
Robert Shearman committed
2311 2312
    }
    return 0;
2313 2314
}

2315
extern UINT uDragListMessage DECLSPEC_HIDDEN;
2316 2317 2318 2319 2320

/***********************************************************************
 * TOOLBAR_CustomizeDialogProc
 * This function implements the toolbar customization dialog.
 */
2321
static INT_PTR CALLBACK
2322 2323
TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
2324
    PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2325 2326
    PCUSTOMBUTTON btnInfo;
    NMTOOLBARA nmtb;
2327
    TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2328 2329 2330 2331

    switch (uMsg)
    {
	case WM_INITDIALOG:
2332
	    custInfo = (PCUSTDLG_INFO)lParam;
2333
	    SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2334

2335
	    if (custInfo)
2336
	    {
2337
		WCHAR Buffer[256];
2338 2339
		int i = 0;
		int index;
2340
		NMTBINITCUSTOMIZE nmtbic;
2341

2342
		infoPtr = custInfo->tbInfo;
2343 2344 2345 2346

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

2347
		if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2348 2349
		    return FALSE;

2350
		nmtbic.hwndDialog = hwnd;
2351
		/* Send TBN_INITCUSTOMIZE notification */
2352
		if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2353
		    TBNRF_HIDEHELP)
2354
                {
2355 2356 2357
                    TRACE("TBNRF_HIDEHELP requested\n");
                    ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
                }
2358

2359 2360
		/* add items to 'toolbar buttons' list and check if removable */
		for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2361
                {
2362
                    btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2363
                    memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2364
                    btnInfo->btn.fsStyle = BTNS_SEP;
2365 2366
                    btnInfo->bVirtual = FALSE;
		    LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2367 2368

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

Robert Shearman's avatar
Robert Shearman committed
2371 2372
		    index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
		    SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2373
		}
2374

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

2377
		/* insert separator button into 'available buttons' list */
2378
                btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2379
		memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2380
		btnInfo->btn.fsStyle = BTNS_SEP;
2381 2382
		btnInfo->bVirtual = FALSE;
		btnInfo->bRemovable = TRUE;
2383
		LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
Robert Shearman's avatar
Robert Shearman committed
2384 2385
		index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2386 2387 2388 2389 2390

		/* insert all buttons into dsa */
		for (i = 0;; i++)
		{
		    /* send TBN_GETBUTTONINFO notification */
2391
                    NMTOOLBARW nmtb;
2392 2393 2394 2395
		    nmtb.iItem = i;
		    nmtb.pszText = Buffer;
		    nmtb.cchText = 256;

2396 2397 2398 2399
                    /* Clear previous button's text */
                    ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));

                    if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2400 2401
			break;

2402
		    TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2403 2404 2405 2406 2407
                        nmtb.tbButton.fsStyle, i, 
                        nmtb.tbButton.idCommand,
                        nmtb.tbButton.iString,
                        nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
                        : "");
2408

2409
		    /* insert button into the appropriate list */
2410
		    index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2411
		    if (index == -1)
2412
		    {
2413
                        btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2414 2415
			btnInfo->bVirtual = FALSE;
			btnInfo->bRemovable = TRUE;
2416 2417 2418
		    }
		    else
		    {
Robert Shearman's avatar
Robert Shearman committed
2419
                        btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, 
2420 2421 2422
                            IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
                    }

2423
                    btnInfo->btn = nmtb.tbButton;
2424
                    if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2425 2426 2427 2428 2429 2430 2431 2432 2433
                    {
                        if (lstrlenW(nmtb.pszText))
                            lstrcpyW(btnInfo->text, nmtb.pszText);
                        else if (nmtb.tbButton.iString >= 0 && 
                            nmtb.tbButton.iString < infoPtr->nNumStrings)
                        {
                            lstrcpyW(btnInfo->text, 
                                infoPtr->strings[nmtb.tbButton.iString]);
                        }
2434
		    }
2435 2436 2437

		    if (index == -1)
			TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2438 2439
		}

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

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

2445
		/* append 'virtual' separator button to the 'toolbar buttons' list */
2446
                btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2447
		memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2448
		btnInfo->btn.fsStyle = BTNS_SEP;
2449 2450
		btnInfo->bVirtual = TRUE;
		btnInfo->bRemovable = FALSE;
2451
		LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
Robert Shearman's avatar
Robert Shearman committed
2452 2453
		index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2454 2455

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

2459 2460
		MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
		MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2461

2462
		/* set focus and disable buttons */
2463
		PostMessageW (hwnd, WM_USER, 0, 0);
2464 2465 2466
	    }
	    return TRUE;

2467 2468 2469 2470 2471 2472 2473
	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;

2474 2475 2476 2477 2478 2479 2480
	case WM_CLOSE:
	    EndDialog(hwnd, FALSE);
	    return TRUE;

	case WM_COMMAND:
	    switch (LOWORD(wParam))
	    {
2481 2482 2483 2484 2485 2486 2487 2488
		case IDC_TOOLBARBTN_LBOX:
		    if (HIWORD(wParam) == LBN_SELCHANGE)
		    {
			PCUSTOMBUTTON btnInfo;
			NMTOOLBARA nmtb;
			int count;
			int index;

Robert Shearman's avatar
Robert Shearman committed
2489 2490
			count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
			index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2491 2492 2493

			/* send TBN_QUERYINSERT notification */
			nmtb.iItem = index;
2494
		        TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2495 2496

			/* get list box item */
Robert Shearman's avatar
Robert Shearman committed
2497
			btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528

			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
2529
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2530
			TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2531 2532 2533 2534 2535
		    }
		    break;

		case IDC_MOVEDN_BTN: /* move down */
		    {
Robert Shearman's avatar
Robert Shearman committed
2536
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2537
			TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2538 2539 2540 2541 2542
		    }
		    break;

		case IDC_REMOVE_BTN: /* remove button */
		    {
Robert Shearman's avatar
Robert Shearman committed
2543
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2544 2545 2546 2547

			if (LB_ERR == index)
				break;

2548
			TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2549 2550
		    }
		    break;
2551 2552 2553 2554 2555 2556
		case IDC_HELP_BTN:
			TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
			break;
		case IDC_RESET_BTN:
			TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
			break;
2557 2558 2559 2560

		case IDOK: /* Add button */
		    {
			int index;
2561
			int indexto;
2562

Robert Shearman's avatar
Robert Shearman committed
2563 2564
			index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
			indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2565

2566
			TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2567 2568 2569
		    }
		    break;

2570 2571 2572 2573 2574 2575 2576
		case IDCANCEL:
		    EndDialog(hwnd, FALSE);
		    break;
	    }
	    return TRUE;

	case WM_DESTROY:
2577 2578 2579 2580 2581
	    {
		int count;
		int i;

		/* delete items from 'toolbar buttons' listbox*/
Robert Shearman's avatar
Robert Shearman committed
2582
		count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2583 2584
		for (i = 0; i < count; i++)
		{
Robert Shearman's avatar
Robert Shearman committed
2585
		    btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2586
		    Free(btnInfo);
Robert Shearman's avatar
Robert Shearman committed
2587
		    SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2588
		}
Robert Shearman's avatar
Robert Shearman committed
2589
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2590 2591 2592


		/* delete items from 'available buttons' listbox*/
Robert Shearman's avatar
Robert Shearman committed
2593
		count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2594 2595
		for (i = 0; i < count; i++)
		{
Robert Shearman's avatar
Robert Shearman committed
2596
		    btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2597
		    Free(btnInfo);
Robert Shearman's avatar
Robert Shearman committed
2598
		    SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2599
		}
Robert Shearman's avatar
Robert Shearman committed
2600
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2601
            }
2602 2603 2604 2605 2606 2607 2608 2609
	    return TRUE;

	case WM_DRAWITEM:
	    if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
	    {
		LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
		RECT rcButton;
		RECT rcText;
2610
		HPEN hPen, hOldPen;
2611 2612 2613 2614
		HBRUSH hOldBrush;
		COLORREF oldText = 0;
		COLORREF oldBk = 0;

2615
		/* get item data */
2616
               btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2617
		if (btnInfo == NULL)
2618
		{
2619 2620
		    FIXME("btnInfo invalid!\n");
		    return TRUE;
2621 2622
		}

2623
		/* set colors and select objects */
2624
		oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2625
		if (btnInfo->bVirtual)
2626
		   oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2627
		else
2628
		   oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2629
                hPen = CreatePen( PS_SOLID, 1,
2630
                                 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2631
		hOldPen = SelectObject (lpdis->hDC, hPen );
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
		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 */
		CopyRect (&rcButton, &lpdis->rcItem);
		InflateRect (&rcButton, -1, -1);
		CopyRect (&rcText, &rcButton);
2642
		rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2643 2644 2645 2646 2647 2648 2649
		rcText.left = rcButton.right + 2;

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

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

2653
		/* draw image and text */
2654
		if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2655 2656 2657 2658 2659 2660
			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,
2661 2662
			       DT_LEFT | DT_VCENTER | DT_SINGLELINE);

2663
		/* delete objects and reset colors */
2664 2665
		SelectObject (lpdis->hDC, hOldBrush);
		SelectObject (lpdis->hDC, hOldPen);
2666 2667
		SetBkColor (lpdis->hDC, oldBk);
		SetTextColor (lpdis->hDC, oldText);
2668
                DeleteObject( hPen );
2669 2670 2671 2672 2673 2674 2675 2676 2677
		return TRUE;
	    }
	    return FALSE;

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

2678
		lpmis->itemHeight = 15 + 8; /* default height */
2679 2680 2681 2682 2683 2684

		return TRUE;
	    }
	    return FALSE;

	default:
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
            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;
2703 2704 2705
    }
}

2706 2707 2708 2709 2710 2711
static BOOL
TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
{
    HBITMAP hbmLoad;
    INT nCountBefore = ImageList_GetImageCount(himlDef);
    INT nCountAfter;
2712
    INT cxIcon, cyIcon;
2713 2714 2715 2716 2717 2718
    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 */
2719
        hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2720 2721 2722
    else if (bitmap->hInst == COMCTL32_hModule)
        hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
                              IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2723 2724
    else
        hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2725 2726 2727

    /* enlarge the bitmap if needed */
    ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2728 2729
    if (bitmap->hInst != COMCTL32_hModule)
        COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
    
    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,
2770
                                ILC_COLOR32|ILC_MASK, 8, 2);
2771 2772 2773 2774 2775 2776 2777 2778
    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);
}
2779

2780 2781 2782 2783
/***********************************************************************
 * TOOLBAR_AddBitmap:  Add the bitmaps to the default image list.
 *
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
2784
static LRESULT
2785
TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
2786
{
2787
    TBITMAP_INFO info;
2788
    INT iSumButtons, i;
2789
    HIMAGELIST himlDef;
Alexandre Julliard's avatar
Alexandre Julliard committed
2790

2791
    TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2792
    if (!lpAddBmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
2793 2794
	return -1;

2795 2796
    if (lpAddBmp->hInst == HINST_COMMCTRL)
    {
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
        info.hInst = COMCTL32_hModule;
        switch (lpAddBmp->nID)
        {
            case IDB_STD_SMALL_COLOR:
	        info.nButtons = 15;
	        info.nID = IDB_STD_SMALL;
	        break;
            case IDB_STD_LARGE_COLOR:
	        info.nButtons = 15;
	        info.nID = IDB_STD_LARGE;
	        break;
            case IDB_VIEW_SMALL_COLOR:
	        info.nButtons = 12;
	        info.nID = IDB_VIEW_SMALL;
	        break;
            case IDB_VIEW_LARGE_COLOR:
	        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:
	        return -1;
	}
2827

2828
	TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2829

2830
	/* Windows resize all the buttons to the size of a newly added standard image */
2831
	if (lpAddBmp->nID & 1)
2832
	{
2833
	    /* large icons: 24x24. Will make the button 31x30 */
2834
	    SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2835 2836
	}
	else
2837
	{
2838
	    /* small icons: 16x16. Will make the buttons 23x22 */
2839
	    SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2840
	}
2841

2842
	TOOLBAR_CalcToolbar (infoPtr);
2843 2844 2845
    }
    else
    {
2846
	info.nButtons = count;
2847 2848 2849
	info.hInst = lpAddBmp->hInst;
	info.nID = lpAddBmp->nID;
	TRACE("adding %d bitmaps!\n", info.nButtons);
2850
    }
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
    
    /* 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;
    }
2861

2862
    if (!infoPtr->cimlDef) {
2863 2864
	/* create new default image list */
	TRACE ("creating default image list!\n");
2865

2866
        himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2867
                                    ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2868
	TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2869
        infoPtr->himlInt = himlDef;
2870 2871 2872 2873 2874 2875 2876 2877
    }
    else {
        himlDef = GETDEFIMAGELIST(infoPtr, 0);
    }

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

2880 2881
    if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
        return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2882

2883
    TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2884
    infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2885
    infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2886 2887 2888
    infoPtr->nNumBitmapInfos++;
    TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);

2889
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2890
    return iSumButtons;
Alexandre Julliard's avatar
Alexandre Julliard committed
2891 2892 2893 2894
}


static LRESULT
2895
TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
2896
{
2897
    TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
Alexandre Julliard's avatar
Alexandre Julliard committed
2898

2899
    return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2900
}
Alexandre Julliard's avatar
Alexandre Julliard committed
2901 2902 2903


static LRESULT
2904
TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
2905
{
2906
#define MAX_RESOURCE_STRING_LENGTH 512
2907
    BOOL fFirstString = (infoPtr->nNumStrings == 0);
2908
    INT nIndex = infoPtr->nNumStrings;
Alexandre Julliard's avatar
Alexandre Julliard committed
2909

2910 2911 2912
    TRACE("%p, %lx\n", hInstance, lParam);

    if (IS_INTRESOURCE(lParam)) {
2913 2914 2915
	WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
	WCHAR delimiter;
	WCHAR *next_delim;
2916
        HRSRC hrsrc;
2917
	WCHAR *p;
2918
	INT len;
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930

	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
2931

2932
        len = LoadStringW (hInstance, (UINT)lParam,
2933
                             szString, MAX_RESOURCE_STRING_LENGTH);
Alexandre Julliard's avatar
Alexandre Julliard committed
2934

2935 2936 2937
        TRACE("len=%d %s\n", len, debugstr_w(szString));
        if (len == 0 || len == 1)
            return nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
2938

2939
        TRACE("delimiter: 0x%x\n", *szString);
2940 2941 2942 2943 2944
        delimiter = *szString;
        p = szString + 1;

        while ((next_delim = strchrW(p, delimiter)) != NULL) {
            *next_delim = 0;
2945 2946 2947 2948 2949 2950
            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;
            }
2951 2952 2953 2954 2955 2956 2957

            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
2958 2959
    }
    else {
2960
	LPWSTR p = (LPWSTR)lParam;
2961
	INT len;
Alexandre Julliard's avatar
Alexandre Julliard committed
2962

2963 2964
	if (p == NULL)
	    return -1;
2965
	TRACE("adding string(s) from array\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
2966
	while (*p) {
2967
            len = strlenW (p);
Alexandre Julliard's avatar
Alexandre Julliard committed
2968

2969 2970 2971
            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
2972 2973 2974 2975 2976 2977
	    infoPtr->nNumStrings++;

	    p += (len+1);
	}
    }

2978
    if (fFirstString)
2979
        TOOLBAR_CalcToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
2980 2981 2982 2983
    return nIndex;
}


2984
static LRESULT
2985
TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2986
{
2987
    BOOL fFirstString = (infoPtr->nNumStrings == 0);
2988
    LPSTR p;
2989
    INT nIndex;
2990
    INT len;
2991

2992 2993 2994
    TRACE("%p, %lx\n", hInstance, lParam);

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

2997 2998 2999
    p = (LPSTR)lParam;
    if (p == NULL)
        return -1;
3000

3001
    TRACE("adding string(s) from array\n");
3002 3003 3004 3005
    nIndex = infoPtr->nNumStrings;
    while (*p) {
        len = strlen (p);
        TRACE("len=%d \"%s\"\n", len, p);
3006

3007 3008 3009
        infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
        Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
        infoPtr->nNumStrings++;
3010

3011
        p += (len+1);
3012 3013
    }

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


static LRESULT
3021
TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3022
{
3023 3024
    RECT parent_rect;
    HWND parent;
3025
    INT  x, y;
3026
    INT  cx, cy;
Alexandre Julliard's avatar
Alexandre Julliard committed
3027

3028
    TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
Alexandre Julliard's avatar
Alexandre Julliard committed
3029

3030
    parent = GetParent (infoPtr->hwndSelf);
3031 3032 3033 3034

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

3035
    GetClientRect(parent, &parent_rect);
Alexandre Julliard's avatar
Alexandre Julliard committed
3036

3037 3038 3039
    x = parent_rect.left;
    y = parent_rect.top;

3040
    TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3041

3042 3043 3044 3045 3046
    cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
    cx = parent_rect.right - parent_rect.left;

    if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1))
    {
3047 3048
        TOOLBAR_LayoutToolbar(infoPtr);
        InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
3049
    }
3050 3051 3052 3053

    if (!(infoPtr->dwStyle & CCS_NORESIZE))
    {
        RECT window_rect;
3054
        UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3055 3056 3057

        if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
        {
3058
            GetWindowRect(infoPtr->hwndSelf, &window_rect);
3059
            MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3060 3061 3062 3063
            y = window_rect.top;
        }
        if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
        {
3064
            GetWindowRect(infoPtr->hwndSelf, &window_rect);
3065 3066
            y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3067

3068 3069 3070 3071 3072
        if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
            uPosFlags |= SWP_NOMOVE;
    
        if (!(infoPtr->dwStyle & CCS_NODIVIDER))
            cy += GetSystemMetrics(SM_CYEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3073

3074 3075
        if (infoPtr->dwStyle & WS_BORDER)
        {
3076 3077
            cx += 2 * GetSystemMetrics(SM_CXBORDER);
            cy += 2 * GetSystemMetrics(SM_CYBORDER);
3078
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3079

3080
        SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3081 3082
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
3083 3084
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3085 3086


3087
static inline LRESULT
3088
TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3089 3090 3091 3092 3093
{
    return infoPtr->nNumButtons;
}


3094 3095
static inline LRESULT
TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
Alexandre Julliard's avatar
Alexandre Julliard committed
3096
{
3097
    infoPtr->dwStructSize = Size;
Alexandre Julliard's avatar
Alexandre Julliard committed
3098 3099 3100 3101 3102 3103

    return 0;
}


static LRESULT
3104
TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
Alexandre Julliard's avatar
Alexandre Julliard committed
3105 3106
{
    TBUTTON_INFO *btnPtr;
3107
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3108

3109
    TRACE("button %d, iBitmap now %d\n", Id, Index);
3110

3111
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3112 3113 3114 3115
    if (nIndex == -1)
	return FALSE;

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

3118 3119
    /* we HAVE to erase the background, the new bitmap could be */
    /* transparent */
3120
    InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3121 3122 3123 3124 3125 3126

    return TRUE;
}


static LRESULT
3127
TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3128 3129
{
    TBUTTON_INFO *btnPtr;
3130 3131
    INT nIndex;
    INT nOldIndex = -1;
3132
    BOOL bChecked = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3133

3134
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3135

3136
    TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3137

Alexandre Julliard's avatar
Alexandre Julliard committed
3138 3139 3140 3141
    if (nIndex == -1)
	return FALSE;

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

3143
    bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3144

Alexandre Julliard's avatar
Alexandre Julliard committed
3145 3146
    if (LOWORD(lParam) == FALSE)
	btnPtr->fsState &= ~TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3147
    else {
3148
	if (btnPtr->fsStyle & BTNS_GROUP) {
3149
	    nOldIndex =
Alexandre Julliard's avatar
Alexandre Julliard committed
3150 3151 3152 3153 3154 3155
		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
3156
	btnPtr->fsState |= TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3157
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3158

3159 3160
    if( bChecked != LOWORD(lParam) )
    {
Robert Shearman's avatar
Robert Shearman committed
3161
        if (nOldIndex != -1)
3162 3163
            InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3164
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3165

Alexandre Julliard's avatar
Alexandre Julliard committed
3166
    /* FIXME: Send a WM_NOTIFY?? */
Alexandre Julliard's avatar
Alexandre Julliard committed
3167

Alexandre Julliard's avatar
Alexandre Julliard committed
3168 3169 3170 3171 3172
    return TRUE;
}


static LRESULT
3173
TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3174
{
3175
    return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3176 3177 3178
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3179
static LRESULT
3180
TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3181
{
3182
    CUSTDLG_INFO custInfo;
3183 3184
    LRESULT ret;
    NMHDR nmhdr;
Alexandre Julliard's avatar
Alexandre Julliard committed
3185

3186
    custInfo.tbInfo = infoPtr;
3187
    custInfo.tbHwnd = infoPtr->hwndSelf;
3188

3189
    /* send TBN_BEGINADJUST notification */
3190
    TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3191

3192 3193
    ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
                           infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3194 3195

    /* send TBN_ENDADJUST notification */
3196
    TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3197 3198

    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3199
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3200 3201 3202


static LRESULT
3203
TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
3204
{
Robert Shearman's avatar
Robert Shearman committed
3205
    NMTOOLBARW nmtb;
3206
    TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
Alexandre Julliard's avatar
Alexandre Julliard committed
3207

Alexandre Julliard's avatar
Alexandre Julliard committed
3208
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3209
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3210

Robert Shearman's avatar
Robert Shearman committed
3211
    memset(&nmtb, 0, sizeof(nmtb));
3212 3213 3214 3215 3216 3217 3218
    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;
3219
    TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
Robert Shearman's avatar
Robert Shearman committed
3220

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

Alexandre Julliard's avatar
Alexandre Julliard committed
3223
    if (infoPtr->nNumButtons == 1) {
3224
	TRACE(" simple delete!\n");
3225 3226
	if (TOOLBAR_ButtonHasString(infoPtr->buttons))
	    Free((LPWSTR)infoPtr->buttons[0].iString);
3227
	Free (infoPtr->buttons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3228 3229 3230 3231
	infoPtr->buttons = NULL;
	infoPtr->nNumButtons = 0;
    }
    else {
Alexandre Julliard's avatar
Alexandre Julliard committed
3232
	TBUTTON_INFO *oldButtons = infoPtr->buttons;
3233
        TRACE("complex delete! [nIndex=%d]\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3234

Alexandre Julliard's avatar
Alexandre Julliard committed
3235
	infoPtr->nNumButtons--;
3236
	infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3237 3238 3239 3240
        if (nIndex > 0) {
            memcpy (&infoPtr->buttons[0], &oldButtons[0],
                    nIndex * sizeof(TBUTTON_INFO));
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3241

Alexandre Julliard's avatar
Alexandre Julliard committed
3242 3243 3244 3245 3246
        if (nIndex < infoPtr->nNumButtons) {
            memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
                    (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
        }

3247 3248
        if (TOOLBAR_ButtonHasString(&oldButtons[nIndex]))
            Free((LPWSTR)oldButtons[nIndex].iString);
3249
	Free (oldButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3250 3251
    }

3252
    TOOLBAR_LayoutToolbar(infoPtr);
3253

3254
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3255 3256 3257 3258 3259 3260

    return TRUE;
}


static LRESULT
3261
TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3262 3263
{
    TBUTTON_INFO *btnPtr;
3264
    INT nIndex;
3265
    DWORD bState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3266

3267
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3268

3269
    TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3270

Alexandre Julliard's avatar
Alexandre Julliard committed
3271 3272 3273 3274
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3275 3276 3277 3278 3279 3280 3281

    bState = btnPtr->fsState & TBSTATE_ENABLED;

    /* update the toolbar button state */
    if(LOWORD(lParam) == FALSE) {
 	btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
    } else {
Alexandre Julliard's avatar
Alexandre Julliard committed
3282
	btnPtr->fsState |= TBSTATE_ENABLED;
3283
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3284

3285
    /* redraw the button only if the state of the button changed */
3286
    if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3287
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3288 3289 3290 3291 3292

    return TRUE;
}


3293
static inline LRESULT
3294
TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3295 3296 3297
{
    return infoPtr->bAnchor;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3298 3299 3300


static LRESULT
3301
TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3302
{
3303
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3304

3305
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3306
    if (nIndex == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3307
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3308 3309 3310 3311 3312

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


Patrik Stridvall's avatar
Patrik Stridvall committed
3313
static inline LRESULT
3314
TOOLBAR_GetBitmapFlags (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
3315
{
3316
    return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3317 3318 3319 3320
}


static LRESULT
3321
TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
Alexandre Julliard's avatar
Alexandre Julliard committed
3322 3323 3324
{
    TBUTTON_INFO *btnPtr;

3325 3326
    if (lpTbb == NULL)
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3327 3328 3329 3330 3331 3332 3333 3334 3335

    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;
3336 3337
    lpTbb->bReserved[0] = 0;
    lpTbb->bReserved[1] = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3338 3339 3340 3341 3342 3343 3344
    lpTbb->dwData    = btnPtr->dwData;
    lpTbb->iString   = btnPtr->iString;

    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3345
static LRESULT
3346
TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
3347
{
3348
    /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
Alexandre Julliard's avatar
Alexandre Julliard committed
3349
    TBUTTON_INFO *btnPtr;
3350
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3351

3352 3353
    if (lpTbInfo == NULL)
	return -1;
3354 3355 3356 3357 3358 3359 3360 3361

    /* MSDN documents a iImageLabel field added in Vista but it is not present in
     * 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");
3362
	return -1;
3363
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3364

3365
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
Alexandre Julliard's avatar
Alexandre Julliard committed
3366 3367 3368
    if (nIndex == -1)
	return -1;

Guy Albertelli's avatar
Guy Albertelli committed
3369
    if (!(btnPtr = &infoPtr->buttons[nIndex])) return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3370 3371 3372 3373 3374 3375 3376 3377

    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)
3378 3379 3380 3381 3382
        /* 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
3383 3384 3385 3386
    if (lpTbInfo->dwMask & TBIF_STATE)
	lpTbInfo->fsState = btnPtr->fsState;
    if (lpTbInfo->dwMask & TBIF_STYLE)
	lpTbInfo->fsStyle = btnPtr->fsStyle;
3387 3388 3389
    if (lpTbInfo->dwMask & TBIF_TEXT) {
        /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
           can't use TOOLBAR_GetText here */
3390
        if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3391 3392 3393 3394 3395
            LPWSTR lpText = (LPWSTR)btnPtr->iString;
            if (bUnicode)
                Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
            else
                Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3396 3397
        } else
            lpTbInfo->pszText[0] = '\0';
3398 3399 3400
    }
    return nIndex;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3401 3402


3403
static inline LRESULT
3404
TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3405
{
3406 3407
    return MAKELONG((WORD)infoPtr->nButtonWidth,
                    (WORD)infoPtr->nButtonHeight);
Alexandre Julliard's avatar
Alexandre Julliard committed
3408
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3409 3410 3411


static LRESULT
3412
TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3413
{
3414 3415
    INT nIndex;
    LPWSTR lpText;
3416
    LRESULT ret = 0;
3417

3418
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3419
    if (nIndex == -1)
3420 3421
	return -1;

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

3424
    if (isW)
3425
    {
3426 3427 3428 3429 3430
        if (lpText)
        {
            ret = strlenW (lpText);
            if (lpStr) strcpyW (lpStr, lpText);
        }
3431
    }
3432 3433 3434
    else
        ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
                                  (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3435
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3436 3437 3438
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3439
static LRESULT
3440
TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3441
{
3442
    TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3443
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3444
    return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3445 3446 3447
}


3448
static inline LRESULT
3449
TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3450
{
3451 3452
    TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
3453 3454
    return infoPtr->dwExStyle;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3455 3456 3457


static LRESULT
3458
TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3459
{
3460
    TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3461
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3462
    return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3463 3464 3465
}


3466
static LRESULT
3467
TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3468
{
3469
    if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3470 3471 3472 3473 3474 3475 3476
	return -1;

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

    return (LRESULT)infoPtr->nHotItem;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3477 3478 3479


static LRESULT
3480
TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3481
{
3482
    TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3483
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3484
    return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3485 3486 3487
}


3488
static LRESULT
3489
TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3490
{
3491
    TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3492 3493 3494 3495 3496 3497 3498

    *lptbim = infoPtr->tbim;

    return 0;
}


3499
static inline LRESULT
3500
TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3501
{
3502
    TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3503 3504 3505

    return (LRESULT)infoPtr->clrInsertMark;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3506 3507 3508


static LRESULT
3509
TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
3510 3511 3512 3513 3514 3515
{
    TBUTTON_INFO *btnPtr;

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

3517 3518 3519 3520
    if (lpRect == NULL)
	return FALSE;
    if (btnPtr->fsState & TBSTATE_HIDDEN)
	return FALSE;
3521

Alexandre Julliard's avatar
Alexandre Julliard committed
3522 3523 3524 3525 3526 3527 3528 3529 3530
    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
3531
static LRESULT
3532
TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
3533
{
Alexandre Julliard's avatar
Alexandre Julliard committed
3534 3535 3536
    if (lpSize == NULL)
	return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
3537 3538
    lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
    lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
Alexandre Julliard's avatar
Alexandre Julliard committed
3539

3540
    TRACE("maximum size %d x %d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
3541 3542
	   infoPtr->rcBound.right - infoPtr->rcBound.left,
	   infoPtr->rcBound.bottom - infoPtr->rcBound.top);
Alexandre Julliard's avatar
Alexandre Julliard committed
3543 3544 3545 3546 3547

    return TRUE;
}


3548
/* << TOOLBAR_GetObject >> */
3549 3550


3551
static inline LRESULT
3552
TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3553
{
3554
    return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3555
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3556 3557 3558


static LRESULT
3559
TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
3560 3561
{
    TBUTTON_INFO *btnPtr;
3562
    INT        nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3563

3564
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3565 3566 3567
    btnPtr = &infoPtr->buttons[nIndex];
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;
3568

3569 3570
    if (lpRect == NULL)
	return FALSE;
3571

Alexandre Julliard's avatar
Alexandre Julliard committed
3572 3573 3574 3575 3576 3577 3578
    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
3579 3580


3581
static inline LRESULT
3582
TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3583
{
3584
    return infoPtr->nRows;
Alexandre Julliard's avatar
Alexandre Julliard committed
3585 3586 3587 3588
}


static LRESULT
3589
TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3590
{
3591
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3592

3593
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3594 3595
    if (nIndex == -1)
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3596 3597 3598 3599 3600

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


3601
static inline LRESULT
3602
TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3603
{
3604
    return infoPtr->dwStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
3605 3606 3607
}


3608
static inline LRESULT
3609
TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3610 3611 3612
{
    return infoPtr->nMaxTextRows;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3613 3614 3615


static LRESULT
3616
TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3617
{
3618 3619
    if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
        TOOLBAR_TooltipCreateControl(infoPtr);
3620
    return (LRESULT)infoPtr->hwndToolTip;
Alexandre Julliard's avatar
Alexandre Julliard committed
3621 3622 3623
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3624
static LRESULT
3625
TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3626
{
Robert Shearman's avatar
Robert Shearman committed
3627
    TRACE("%s hwnd=%p\n",
3628
	   infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
3629 3630 3631

    return infoPtr->bUnicode;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3632 3633


3634
static inline LRESULT
3635
TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3636 3637 3638 3639 3640
{
    return infoPtr->iVersion;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3641
static LRESULT
3642
TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
Alexandre Julliard's avatar
Alexandre Julliard committed
3643 3644
{
    TBUTTON_INFO *btnPtr;
3645
    BYTE oldState;
3646
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3647

3648 3649
    TRACE("\n");

3650
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3651 3652 3653 3654
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3655
    oldState = btnPtr->fsState;
3656 3657

    if (fHide)
Alexandre Julliard's avatar
Alexandre Julliard committed
3658
	btnPtr->fsState |= TBSTATE_HIDDEN;
3659 3660
    else
	btnPtr->fsState &= ~TBSTATE_HIDDEN;
Alexandre Julliard's avatar
Alexandre Julliard committed
3661

3662 3663 3664 3665
    if (oldState != btnPtr->fsState) {
        TOOLBAR_LayoutToolbar (infoPtr);
        InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3666 3667 3668 3669 3670

    return TRUE;
}


3671
static inline LRESULT
3672
TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
Alexandre Julliard's avatar
Alexandre Julliard committed
3673
{
3674
    return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
3675 3676 3677
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3678
static LRESULT
3679
TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
Alexandre Julliard's avatar
Alexandre Julliard committed
3680 3681
{
    TBUTTON_INFO *btnPtr;
3682
    INT nIndex;
3683
    DWORD oldState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3684

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

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

    if (fIndeterminate)
Alexandre Julliard's avatar
Alexandre Julliard committed
3693
	btnPtr->fsState |= TBSTATE_INDETERMINATE;
3694 3695
    else
	btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3696

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

    return TRUE;
}


static LRESULT
3705
TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
3706
{
3707 3708
    if (lpTbb == NULL)
	return FALSE;
3709 3710 3711 3712 3713 3714 3715 3716

    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).
	*/
3717
	nIndex = infoPtr->nNumButtons;
3718 3719 3720

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

3722
    TRACE("inserting button index=%d\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3723 3724
    if (nIndex > infoPtr->nNumButtons) {
	nIndex = infoPtr->nNumButtons;
3725
	TRACE("adjust index=%d\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3726 3727
    }

3728
    return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3729 3730
}

3731
/* << TOOLBAR_InsertMarkHitTest >> */
Alexandre Julliard's avatar
Alexandre Julliard committed
3732 3733


Alexandre Julliard's avatar
Alexandre Julliard committed
3734
static LRESULT
3735
TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3736
{
3737
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3738

3739
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3740
    if (nIndex == -1)
3741
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3742 3743 3744 3745 3746 3747

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


static LRESULT
3748
TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3749
{
3750
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3751

3752
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3753
    if (nIndex == -1)
3754
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3755 3756 3757 3758 3759 3760

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


static LRESULT
3761
TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3762
{
3763
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3764

3765
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3766
    if (nIndex == -1)
3767
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3768 3769 3770 3771 3772 3773

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


static LRESULT
3774
TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3775
{
3776
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3777

3778
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3779
    if (nIndex == -1)
3780
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3781 3782 3783 3784 3785 3786

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


static LRESULT
3787
TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3788
{
3789
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3790

3791
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3792
    if (nIndex == -1)
3793
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3794 3795 3796 3797 3798 3799

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


static LRESULT
3800
TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3801
{
3802
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3803

3804
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3805
    if (nIndex == -1)
3806
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3807 3808 3809 3810 3811

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


3812
static LRESULT
3813
TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3814 3815
{
    TBADDBITMAP tbab;
3816
    tbab.hInst = hInstance;
3817
    tbab.nID = wParam;
3818

3819
    TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3820

3821
    return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3822 3823 3824 3825
}


static LRESULT
3826
TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3827 3828 3829 3830 3831
{
    WCHAR wszAccel[] = {'&',wAccel,0};
    int i;
    
    TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3832
        infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866
    
    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
        TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
        if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
            !(btnPtr->fsState & TBSTATE_HIDDEN))
        {
            int iLen = strlenW(wszAccel);
            LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
            
            if (!lpszStr)
                continue;

            while (*lpszStr)
            {
                if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
                {
                    lpszStr += 2;
                    continue;
                }
                if (!strncmpiW(lpszStr, wszAccel, iLen))
                {
                    *pIDButton = btnPtr->idCommand;
                    return TRUE;
                }
                lpszStr++;
            }
        }
    }
    return FALSE;
}


static LRESULT
3867
TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3868 3869
{
    INT nIndex;
3870 3871
    DWORD oldState;
    TBUTTON_INFO *btnPtr;
3872

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

3875
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3876 3877 3878
    if (nIndex == -1)
        return FALSE;

3879 3880 3881
    btnPtr = &infoPtr->buttons[nIndex];
    oldState = btnPtr->fsState;

3882
    if (fMark)
3883
        btnPtr->fsState |= TBSTATE_MARKED;
3884
    else
3885 3886 3887
        btnPtr->fsState &= ~TBSTATE_MARKED;

    if(oldState != btnPtr->fsState)
3888
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3889 3890 3891 3892

    return TRUE;
}

Robert Shearman's avatar
Robert Shearman committed
3893 3894

/* fixes up an index of a button affected by a move */
3895
static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
Robert Shearman's avatar
Robert Shearman committed
3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
{
    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
3915
TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
Robert Shearman's avatar
Robert Shearman committed
3916 3917 3918 3919 3920
{
    INT nIndex;
    INT nCount;
    TBUTTON_INFO button;

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

3923
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
Robert Shearman's avatar
Robert Shearman committed
3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
    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);
    }

3956 3957 3958
    TOOLBAR_LayoutToolbar(infoPtr);
    TOOLBAR_AutoSize(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Robert Shearman's avatar
Robert Shearman committed
3959 3960 3961

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3962 3963 3964


static LRESULT
3965
TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
Alexandre Julliard's avatar
Alexandre Julliard committed
3966 3967
{
    TBUTTON_INFO *btnPtr;
3968
    INT nIndex;
3969
    DWORD oldState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3970

3971
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3972 3973 3974 3975
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3976
    oldState = btnPtr->fsState;
3977 3978

    if (fPress)
Alexandre Julliard's avatar
Alexandre Julliard committed
3979
	btnPtr->fsState |= TBSTATE_PRESSED;
3980 3981
    else
	btnPtr->fsState &= ~TBSTATE_PRESSED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3982

3983
    if(oldState != btnPtr->fsState)
3984
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3985 3986 3987 3988

    return TRUE;
}

3989 3990
/* FIXME: there might still be some confusion her between number of buttons
 * and number of bitmaps */
3991
static LRESULT
3992
TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
3993 3994 3995
{
    HBITMAP hBitmap;
    int i = 0, nOldButtons = 0, pos = 0;
3996
    int nOldBitmaps, nNewBitmaps = 0;
3997
    HIMAGELIST himlDef = 0;
3998

3999
    TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4000 4001
          lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
          lpReplace->nButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
4002

4003
    if (lpReplace->hInstOld == HINST_COMMCTRL)
4004 4005 4006 4007
    {
        FIXME("changing standard bitmaps not implemented\n");
        return FALSE;
    }
4008
    else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4009
        FIXME("resources not in the current module not implemented\n");
4010

4011
    TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4012 4013
    for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
        TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4014
        TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4015 4016
        if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
        {
4017
            TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4018 4019 4020 4021
            nOldButtons = tbi->nButtons;
            tbi->nButtons = lpReplace->nButtons;
            tbi->hInst = lpReplace->hInstNew;
            tbi->nID = lpReplace->nIDNew;
4022
            TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4023 4024 4025 4026 4027 4028 4029
            break;
        }
        pos += tbi->nButtons;
    }

    if (nOldButtons == 0)
    {
4030
        WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4031 4032
        return FALSE;
    }
4033
    
4034 4035 4036 4037 4038 4039 4040 4041
    /* 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);

4042 4043
    himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
    nOldBitmaps = ImageList_GetImageCount(himlDef);
4044

4045
    /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4046

4047
    for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4048
        ImageList_Remove(himlDef, i);
4049

4050
    if (hBitmap)
4051
    {
4052
       ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4053
       nNewBitmaps = ImageList_GetImageCount(himlDef);
4054
       DeleteObject(hBitmap);
4055
    }
4056

4057 4058 4059 4060 4061
    infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;

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

4062
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4063 4064
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4065

4066 4067 4068

/* helper for TOOLBAR_SaveRestoreW */
static BOOL
4069
TOOLBAR_Save(const TBSAVEPARAMSW *lpSave)
Alexandre Julliard's avatar
Alexandre Julliard committed
4070
{
4071 4072
    FIXME("save to %s %s\n", debugstr_w(lpSave->pszSubKey),
        debugstr_w(lpSave->pszValueName));
Alexandre Julliard's avatar
Alexandre Julliard committed
4073

4074 4075
    return FALSE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4076 4077


4078 4079 4080 4081 4082
/* helper for TOOLBAR_Restore */
static void
TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
{
    INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
4083

4084 4085
    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
4086
        TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
Alexandre Julliard's avatar
Alexandre Julliard committed
4087 4088
    }

4089 4090 4091 4092 4093 4094 4095 4096
    Free(infoPtr->buttons);
    infoPtr->buttons = NULL;
    infoPtr->nNumButtons = 0;
}


/* helper for TOOLBAR_SaveRestoreW */
static BOOL
4097
TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120
{
    LONG res;
    HKEY hkey = NULL;
    BOOL ret = FALSE;
    DWORD dwType;
    DWORD dwSize = 0;
    NMTBRESTORE nmtbr;

    /* 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)
    {
4121
        nmtbr.pData = Alloc(dwSize);
4122
        nmtbr.cbData = dwSize;
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 4152 4153
        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))
        {
            INT i;

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

            for (i = 0; i < nmtbr.cButtons; i++)
            {
                nmtbr.iItem = i;
                nmtbr.tbButton.iBitmap = -1;
                nmtbr.tbButton.fsState = 0;
                nmtbr.tbButton.fsStyle = 0;
                nmtbr.tbButton.idCommand = 0;
                if (*nmtbr.pCurrent == (DWORD)-1)
                {
                    /* separator */
                    nmtbr.tbButton.fsStyle = TBSTYLE_SEP;
4154 4155 4156
                    /* when inserting separators, iBitmap controls it's size.
                       0 sets default size (width) */
                    nmtbr.tbButton.iBitmap = 0;
4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
                }
                else if (*nmtbr.pCurrent == (DWORD)-2)
                    /* hidden button */
                    nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
                else
                    nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;

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

                /* can't contain real string as we don't know whether
                 * the client put an ANSI or Unicode string in there */
4170
                if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4171
                    nmtbr.tbButton.iString = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4172

4173
                TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4174
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
4175

4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188
            /* do legacy notifications */
            if (infoPtr->iVersion < 5)
            {
                /* FIXME: send TBN_BEGINADJUST */
                FIXME("send TBN_GETBUTTONINFO for each button\n");
                /* FIXME: send TBN_ENDADJUST */
            }

            /* 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)
4189
                    TOOLBAR_DeleteButton(infoPtr, i);
4190 4191 4192 4193

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

4198
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
4199 4200 4201
}


4202
static LRESULT
4203
TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4204
{
4205
    if (lpSave == NULL) return 0;
4206

4207
    if (wParam)
4208
        return TOOLBAR_Save(lpSave);
4209 4210 4211
    else
        return TOOLBAR_Restore(infoPtr, lpSave);
}
4212 4213


4214
static LRESULT
4215
TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4216
{
4217
    LPWSTR pszValueName = 0, pszSubKey = 0;
4218
    TBSAVEPARAMSW SaveW;
4219
    LRESULT result = 0;
4220
    int len;
4221

4222
    if (lpSave == NULL) return 0;
4223

4224
    len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4225
    pszSubKey = Alloc(len * sizeof(WCHAR));
4226 4227
    if (pszSubKey) goto exit;
    MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4228

4229
    len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4230
    pszValueName = Alloc(len * sizeof(WCHAR));
4231 4232 4233 4234 4235 4236
    if (!pszValueName) goto exit;
    MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);

    SaveW.pszValueName = pszValueName;
    SaveW.pszSubKey = pszSubKey;
    SaveW.hkr = lpSave->hkr;
4237
    result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4238 4239

exit:
4240 4241
    Free (pszValueName);
    Free (pszSubKey);
4242

4243
    return result;
4244 4245 4246 4247
}


static LRESULT
4248
TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4249 4250 4251
{
    BOOL bOldAnchor = infoPtr->bAnchor;

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

4254
    infoPtr->bAnchor = bAnchor;
4255

4256
    /* Native does not remove the hot effect from an already hot button */
4257

4258 4259
    return (LRESULT)bOldAnchor;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4260

Alexandre Julliard's avatar
Alexandre Julliard committed
4261

Alexandre Julliard's avatar
Alexandre Julliard committed
4262
static LRESULT
4263
TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4264
{
4265
    HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4266 4267
    short width = (short)LOWORD(lParam);
    short height = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4268

4269
    TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", infoPtr->hwndSelf, wParam, lParam);
Robert Shearman's avatar
Robert Shearman committed
4270 4271

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

4274 4275 4276 4277 4278
    /* 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
4279

4280
    if (infoPtr->nNumButtons > 0)
4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291
        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;
    }
4292

4293 4294 4295 4296 4297
    /* width or height of -1 means no change */
    if (width != -1)
        infoPtr->nBitmapWidth = width;
    if (height != -1)
        infoPtr->nBitmapHeight = height;
4298

Robert Shearman's avatar
Robert Shearman committed
4299 4300 4301 4302 4303
    if ((himlDef == infoPtr->himlInt) &&
        (ImageList_GetImageCount(infoPtr->himlInt) == 0))
    {
        ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
            infoPtr->nBitmapHeight);
4304
    }
4305

4306
    TOOLBAR_CalcToolbar(infoPtr);
4307
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4308 4309 4310 4311
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4312
static LRESULT
4313 4314
TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
                       const TBBUTTONINFOW *lptbbi, BOOL isW)
4315 4316 4317
{
    TBUTTON_INFO *btnPtr;
    INT nIndex;
Robert Shearman's avatar
Robert Shearman committed
4318
    RECT oldBtnRect;
4319 4320 4321 4322 4323 4324

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

4325
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
    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
4336 4337
    if (lptbbi->dwMask & TBIF_SIZE)
	btnPtr->cx = lptbbi->cx;
4338 4339 4340 4341 4342
    if (lptbbi->dwMask & TBIF_STATE)
	btnPtr->fsState = lptbbi->fsState;
    if (lptbbi->dwMask & TBIF_STYLE)
	btnPtr->fsStyle = lptbbi->fsStyle;

Frank Richter's avatar
Frank Richter committed
4343
    if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4344 4345 4346
        /* iString is index, zero it to make Str_SetPtr succeed */
        if (!TOOLBAR_ButtonHasString(btnPtr)) btnPtr->iString = 0;

4347 4348 4349 4350
        if (isW)
            Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
        else
            Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, (LPSTR)lptbbi->pszText);
4351
    }
Robert Shearman's avatar
Robert Shearman committed
4352 4353 4354

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

    if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4358
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Robert Shearman's avatar
Robert Shearman committed
4359
    else
4360
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Robert Shearman's avatar
Robert Shearman committed
4361

4362 4363
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4364 4365 4366


static LRESULT
4367
TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4368
{
4369
    INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4370

4371
    if ((cx < 0) || (cy < 0))
4372
    {
4373
        ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4374
        return FALSE;
4375
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4376

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

4379
    /* The documentation claims you can only change the button size before
4380 4381
     * any button has been added. But this is wrong.
     * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4382 4383 4384
     * 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.
     */
4385 4386 4387
    /*
     * 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
4388
     * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4389
     */
4390
    if (cx == 0) cx = 24;
4391
    if (cy == 0) cy = 22;
4392 4393
    
    cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4394
    cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4395 4396 4397 4398 4399

    infoPtr->nButtonWidth = cx;
    infoPtr->nButtonHeight = cy;
    
    infoPtr->iTopMargin = default_top_margin(infoPtr);
4400
    TOOLBAR_LayoutToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
4401 4402 4403 4404
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4405
static LRESULT
4406
TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4407
{
4408
    /* if setting to current values, ignore */
4409 4410
    if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
	(infoPtr->cxMax == (short)HIWORD(lParam))) {
4411 4412
	TRACE("matches current width, min=%d, max=%d, no recalc\n",
	      infoPtr->cxMin, infoPtr->cxMax);
4413
	return TRUE;
4414
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4415

4416
    /* save new values */
4417 4418
    infoPtr->cxMin = (short)LOWORD(lParam);
    infoPtr->cxMax = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4419

4420 4421 4422
    /* 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). */
4423
    TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4424 4425
	infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);

4426
    TOOLBAR_CalcToolbar (infoPtr);
4427

4428
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4429

Alexandre Julliard's avatar
Alexandre Julliard committed
4430 4431
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4432 4433 4434


static LRESULT
4435
TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
Alexandre Julliard's avatar
Alexandre Julliard committed
4436 4437 4438 4439
{
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;

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

Alexandre Julliard's avatar
Alexandre Julliard committed
4442 4443
    if (infoPtr->hwndToolTip) {

4444
	FIXME("change tool tip!\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
4445 4446 4447

    }

Alexandre Julliard's avatar
Alexandre Julliard committed
4448 4449 4450 4451
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4452
static LRESULT
4453
TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4454 4455
{
    HIMAGELIST himlTemp;
4456
    INT id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4457

4458 4459
    if (infoPtr->iVersion >= 5)
        id = wParam;
4460

4461 4462
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis, 
        &infoPtr->cimlDis, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4463 4464 4465

    /* FIXME: redraw ? */

4466
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4467 4468 4469
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4470
static LRESULT
4471
TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4472 4473 4474
{
    DWORD dwTemp;

4475
    TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", infoPtr->hwndSelf, (DWORD)wParam, (DWORD)lParam);
4476

Alexandre Julliard's avatar
Alexandre Julliard committed
4477 4478 4479 4480 4481 4482
    dwTemp = infoPtr->dwDTFlags;
    infoPtr->dwDTFlags =
	(infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;

    return (LRESULT)dwTemp;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4483

Robert Shearman's avatar
Robert Shearman committed
4484 4485 4486 4487 4488 4489
/* 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
4490
static LRESULT
4491
TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
Alexandre Julliard's avatar
Alexandre Julliard committed
4492
{
4493
    DWORD old_style = infoPtr->dwExStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
4494

4495
    TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
Alexandre Julliard's avatar
Alexandre Julliard committed
4496

4497 4498 4499 4500
    if (mask)
	infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
    else
	infoPtr->dwExStyle = style;
4501 4502

    if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4503
	FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4504 4505
	      (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));

4506
    if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4507
        TOOLBAR_CalcToolbar(infoPtr);
4508
    else
4509
        TOOLBAR_LayoutToolbar(infoPtr);
4510

4511 4512
    TOOLBAR_AutoSize(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4513

4514
    return old_style;
Alexandre Julliard's avatar
Alexandre Julliard committed
4515 4516 4517 4518
}


static LRESULT
4519
TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4520 4521
{
    HIMAGELIST himlTemp;
4522
    INT id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4523

4524 4525
    if (infoPtr->iVersion >= 5)
        id = wParam;
Guy Albertelli's avatar
Guy Albertelli committed
4526

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

4529 4530
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot, 
        &infoPtr->cimlHot, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4531 4532 4533

    /* FIXME: redraw ? */

4534
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4535 4536 4537
}


4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550
/* 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;
4551

4552
        nmhotitem.dwFlags = dwReason;
4553 4554 4555
        if(infoPtr->nHotItem >= 0)
        {
            oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4556
            nmhotitem.idOld = oldBtnPtr->idCommand;
4557
        }
4558
        else
4559
        {
4560
            nmhotitem.dwFlags |= HICF_ENTERING;
4561 4562
            nmhotitem.idOld = 0;
        }
4563 4564

        if (nHit >= 0)
4565
        {
4566
            btnPtr = &infoPtr->buttons[nHit];
4567 4568
            nmhotitem.idNew = btnPtr->idCommand;
        }
4569
	else
4570
	{
4571
	    nmhotitem.dwFlags |= HICF_LEAVING;
4572 4573
	    nmhotitem.idNew = 0;
	}
4574

4575 4576 4577
	/* now change the hot and invalidate the old and new buttons - if the
	 * parent agrees */
	if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4578
	{
4579 4580 4581 4582
            if (oldBtnPtr) {
                oldBtnPtr->bHot = FALSE;
                InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
            }
4583 4584 4585 4586 4587 4588 4589 4590
            /* 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;            
4591 4592 4593 4594
        }
    }
}

4595
static LRESULT
4596
TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4597 4598 4599
{
    INT nOldHotItem = infoPtr->nHotItem;

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

4602
    if (nHotItem >= infoPtr->nNumButtons)
4603 4604
        return infoPtr->nHotItem;
    
4605 4606
    if (nHotItem < 0)
        nHotItem = -1;
4607 4608 4609

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

4611
    TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4612 4613

    if (nOldHotItem < 0)
4614
        return -1;
4615 4616 4617

    return (LRESULT)nOldHotItem;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4618 4619 4620


static LRESULT
4621
TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4622 4623
{
    HIMAGELIST himlTemp;
4624
    INT oldButtonWidth = infoPtr->nButtonWidth;
4625 4626
    INT oldBitmapWidth = infoPtr->nBitmapWidth;
    INT oldBitmapHeight = infoPtr->nBitmapHeight;
4627
    INT i, id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4628

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

4632 4633
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef, 
        &infoPtr->cimlDef, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4634

4635 4636 4637
    infoPtr->nNumBitmaps = 0;
    for (i = 0; i < infoPtr->cimlDef; i++)
        infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4638

4639 4640 4641
    if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
            &infoPtr->nBitmapHeight))
    {
4642 4643
        infoPtr->nBitmapWidth = 1;
        infoPtr->nBitmapHeight = 1;
4644
    }
4645 4646
    if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
    {
4647
        TOOLBAR_CalcToolbar(infoPtr);
4648
        if (infoPtr->nButtonWidth < oldButtonWidth)
4649
            TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4650
    }
4651 4652

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

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

4658
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4659
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4660

Alexandre Julliard's avatar
Alexandre Julliard committed
4661 4662

static LRESULT
4663
TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
Alexandre Julliard's avatar
Alexandre Julliard committed
4664
{
4665
    infoPtr->nIndent = nIndent;
4666

4667
    TRACE("\n");
4668

4669
    /* process only on indent changing */
4670
    if(infoPtr->nIndent != nIndent)
4671
    {
4672
        infoPtr->nIndent = nIndent;
4673 4674
        TOOLBAR_CalcToolbar (infoPtr);
        InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4675
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4676 4677 4678 4679 4680

    return TRUE;
}


4681
static LRESULT
4682
TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4683
{
4684
    TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4685 4686 4687

    if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
    {
4688
        FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4689 4690 4691 4692 4693 4694 4695 4696 4697
        return 0;
    }

    if ((lptbim->iButton == -1) || 
        ((lptbim->iButton < infoPtr->nNumButtons) &&
         (lptbim->iButton >= 0)))
    {
        infoPtr->tbim = *lptbim;
        /* FIXME: don't need to update entire toolbar */
4698
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4699 4700 4701 4702 4703 4704
    }
    else
        ERR("Invalid button index %d\n", lptbim->iButton);

    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4705 4706 4707


static LRESULT
4708
TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
Alexandre Julliard's avatar
Alexandre Julliard committed
4709
{
4710
    infoPtr->clrInsertMark = clr;
Alexandre Julliard's avatar
Alexandre Julliard committed
4711

4712
    /* FIXME: don't need to update entire toolbar */
4713
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4714 4715 4716 4717 4718

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4719
static LRESULT
4720
TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
Alexandre Julliard's avatar
Alexandre Julliard committed
4721
{
4722
    infoPtr->nMaxTextRows = nMaxRows;
Alexandre Julliard's avatar
Alexandre Julliard committed
4723

4724
    TOOLBAR_CalcToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
4725 4726 4727 4728
    return TRUE;
}


4729 4730 4731 4732 4733 4734 4735 4736 4737
/* 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.
4738
 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4739
 */
4740
static LRESULT
4741
TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4742 4743 4744 4745
{
    DWORD  oldPad;

    oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4746 4747
    infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
    infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4748
    TRACE("cx=%d, cy=%d\n",
4749 4750 4751
	  infoPtr->szPadding.cx, infoPtr->szPadding.cy);
    return (LRESULT) oldPad;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4752 4753 4754


static LRESULT
4755
TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
Alexandre Julliard's avatar
Alexandre Julliard committed
4756
{
4757
    HWND hwndOldNotify;
Alexandre Julliard's avatar
Alexandre Julliard committed
4758

4759 4760
    TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
4761
    hwndOldNotify = infoPtr->hwndNotify;
4762
    infoPtr->hwndNotify = hParent;
Alexandre Julliard's avatar
Alexandre Julliard committed
4763

4764
    return (LRESULT)hwndOldNotify;
Alexandre Julliard's avatar
Alexandre Julliard committed
4765 4766 4767 4768
}


static LRESULT
4769
TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
Alexandre Julliard's avatar
Alexandre Julliard committed
4770
{
4771 4772
    int rows = LOWORD(wParam);
    BOOL bLarger = HIWORD(wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4773

4774
    TRACE("\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
4775

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

4778
    if(infoPtr->nRows != rows)
4779
    {
4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794
        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).

4795
           Basic algorithm - If N buttons, and y rows requested, each row
4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814
           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
4815
        /* Round up so more per line, i.e., less rows */
4816
        idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857

        /* 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);
4858

4859
        /* recalculate toolbar */
4860
        TOOLBAR_CalcToolbar (infoPtr);
4861

4862 4863 4864 4865
        /* 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) {
4866
            SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4867 4868
                         infoPtr->rcBound.right - infoPtr->rcBound.left,
                         infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4869
                         SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4870 4871
        }

4872
        /* repaint toolbar */
4873
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4874
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4875 4876 4877 4878 4879 4880 4881 4882 4883

    /* 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
4884 4885 4886
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
4887 4888

static LRESULT
4889
TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4890 4891
{
    TBUTTON_INFO *btnPtr;
4892
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
4893

4894
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4895 4896 4897 4898 4899
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];

4900 4901 4902
    /* if hidden state has changed the invalidate entire window and recalc */
    if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
	btnPtr->fsState = LOWORD(lParam);
4903 4904
	TOOLBAR_CalcToolbar (infoPtr);
	InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4905 4906 4907
	return TRUE;
    }

4908 4909 4910 4911
    /* process state changing if current state doesn't match new state */
    if(btnPtr->fsState != LOWORD(lParam))
    {
        btnPtr->fsState = LOWORD(lParam);
4912
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4913
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4914 4915 4916 4917 4918 4919

    return TRUE;
}


static LRESULT
4920
TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
Alexandre Julliard's avatar
Alexandre Julliard committed
4921
{
4922 4923
    infoPtr->dwStyle = style;
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4924 4925 4926
}


4927
static inline LRESULT
4928
TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
Alexandre Julliard's avatar
Alexandre Julliard committed
4929
{
4930
    TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
4931

4932
    infoPtr->hwndToolTip = hwndTooltip;
Alexandre Julliard's avatar
Alexandre Julliard committed
4933 4934 4935 4936
    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4937
static LRESULT
4938
TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4939
{
4940
    BOOL bTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4941

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

Alexandre Julliard's avatar
Alexandre Julliard committed
4945
    bTemp = infoPtr->bUnicode;
4946
    infoPtr->bUnicode = (BOOL)wParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
4947 4948

    return bTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4949
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4950 4951


4952
static LRESULT
4953
TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
4954
{
4955 4956
    lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
	                       comctl32_color.clrBtnHighlight :
4957
                               infoPtr->clrBtnHighlight;
4958
    lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
4959
	                   comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
4960 4961 4962 4963 4964
    return 1;
}


static LRESULT
4965
TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
4966
{
4967
    TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
4968 4969 4970 4971 4972
	  lParam->clrBtnHighlight, lParam->clrBtnShadow,
	  infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);

    infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
    infoPtr->clrBtnShadow = lParam->clrBtnShadow;
4973
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4974 4975 4976 4977
    return 0;
}


4978
static LRESULT
4979
TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
4980 4981 4982 4983 4984
{
    INT iOldVersion = infoPtr->iVersion;

    infoPtr->iVersion = iVersion;

4985
    if (infoPtr->iVersion >= 5)
4986
        TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
4987

4988 4989 4990
    return iOldVersion;
}

Robert Shearman's avatar
Robert Shearman committed
4991 4992

static LRESULT
4993
TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
Robert Shearman's avatar
Robert Shearman committed
4994 4995 4996 4997 4998
{
    WORD iString = HIWORD(wParam);
    WORD buffersize = LOWORD(wParam);
    LRESULT ret = -1;

4999
    TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
Robert Shearman's avatar
Robert Shearman committed
5000 5001 5002 5003

    if (iString < infoPtr->nNumStrings)
    {
        ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5004
        ret--;
Robert Shearman's avatar
Robert Shearman committed
5005 5006 5007 5008

        TRACE("returning %s\n", debugstr_a(str));
    }
    else
5009
        WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
Robert Shearman's avatar
Robert Shearman committed
5010 5011 5012 5013 5014 5015

    return ret;
}


static LRESULT
5016
TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
Robert Shearman's avatar
Robert Shearman committed
5017 5018 5019 5020 5021
{
    WORD iString = HIWORD(wParam);
    WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
    LRESULT ret = -1;

5022
    TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
Robert Shearman's avatar
Robert Shearman committed
5023 5024 5025 5026 5027

    if (iString < infoPtr->nNumStrings)
    {
        len = min(len, strlenW(infoPtr->strings[iString]));
        ret = (len+1)*sizeof(WCHAR);
5028 5029 5030 5031 5032
        if (str)
        {
            memcpy(str, infoPtr->strings[iString], ret);
            str[len] = '\0';
        }
5033
        ret = len;
Robert Shearman's avatar
Robert Shearman committed
5034 5035 5036 5037

        TRACE("returning %s\n", debugstr_w(str));
    }
    else
5038
        WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
Robert Shearman's avatar
Robert Shearman committed
5039 5040 5041 5042

    return ret;
}

5043 5044
/* UNDOCUMENTED MESSAGE: This appears to set some kind of size. Perhaps it
 * is the maximum size of the toolbar? */
5045 5046
static LRESULT TOOLBAR_Unkwn45D(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
5047
    SIZE * pSize = (SIZE*)lParam;
5048
    FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5049 5050
    return 0;
}
5051

5052

5053 5054 5055
/* 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). */
5056
static LRESULT
5057
TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5058 5059
{
    INT nOldHotItem = infoPtr->nHotItem;
5060

5061
    TRACE("old item=%d, new item=%d, flags=%08x\n",
5062
	  nOldHotItem, nHotItem, (DWORD)lParam);
5063

5064 5065
    if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
        nHotItem = -1;
5066

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

5070
    TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5071

5072
    GetFocus();
5073

5074
    return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5075 5076
}

5077 5078 5079
/* Sets the toolbar global iListGap parameter which controls the amount of
 * spacing between the image and the text of buttons for TBSTYLE_LIST
 * toolbars. */
5080
static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5081
{
5082
    TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5083
    
5084
    infoPtr->iListGap = iListGap;
5085

5086
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5087

5088 5089
    return 0;
}
5090

5091 5092
/* Returns the number of maximum number of image lists associated with the
 * various states. */
5093
static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
Robert Shearman's avatar
Robert Shearman committed
5094
{
5095
    TRACE("hwnd=%p\n", infoPtr->hwndSelf);
Robert Shearman's avatar
Robert Shearman committed
5096 5097 5098 5099

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

5100
static LRESULT
5101
TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114
{
    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
     *
     */
5115
    TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5116 5117 5118 5119 5120 5121 5122 5123 5124 5125
	  wParam, lParam, lpsize->cx, lpsize->cy);

    switch(wParam) {
    case 0:
	if (lpsize->cx == -1) {
	    /* **** this is wrong, native measures each button and sets it */
	    lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
	}
	else if(HIWORD(lpsize->cx)) {
	    RECT rc;
5126
	    HWND hwndParent = GetParent(infoPtr->hwndSelf);
5127

5128
	    GetWindowRect(infoPtr->hwndSelf, &rc);
5129
	    MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5130
            TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141
	    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:
5142
	FIXME("Unknown wParam %ld\n", wParam);
5143 5144
	return 0;
    }
5145
    TRACE("set to -> 0x%08x 0x%08x\n",
5146 5147 5148 5149
	  lpsize->cx, lpsize->cy);
    return 1;
}

5150 5151
static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
5152
    FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5153 5154 5155 5156 5157

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

5158

Alexandre Julliard's avatar
Alexandre Julliard committed
5159
static LRESULT
5160
TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
5161
{
5162
    TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5163
    LOGFONTW logFont;
Alexandre Julliard's avatar
Alexandre Julliard committed
5164

5165
    TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
Robert Shearman's avatar
Robert Shearman committed
5166

5167
    infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5168
    GetClientRect(hwnd, &infoPtr->client_rect);
5169
    infoPtr->bUnicode = infoPtr->hwndNotify && 
5170
        (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5171
    infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
Alexandre Julliard's avatar
Alexandre Julliard committed
5172

5173 5174
    SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
    infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5175 5176
    
    OpenThemeData (hwnd, themeClass);
5177

5178
    TOOLBAR_CheckStyle (infoPtr);
5179

Alexandre Julliard's avatar
Alexandre Julliard committed
5180 5181 5182 5183 5184
    return 0;
}


static LRESULT
5185
TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
5186
{
5187 5188
    INT i;

Alexandre Julliard's avatar
Alexandre Julliard committed
5189
    /* delete tooltip control */
Alexandre Julliard's avatar
Alexandre Julliard committed
5190
    if (infoPtr->hwndToolTip)
5191
	DestroyWindow (infoPtr->hwndToolTip);
Alexandre Julliard's avatar
Alexandre Julliard committed
5192

5193
    /* delete temporary buffer for tooltip text */
5194
    Free (infoPtr->pszTooltipText);
5195
    Free (infoPtr->bitmaps);            /* bitmaps list */
5196

Alexandre Julliard's avatar
Alexandre Julliard committed
5197
    /* delete button data */
5198 5199 5200
    for (i = 0; i < infoPtr->nNumButtons; i++)
        if (TOOLBAR_ButtonHasString(&infoPtr->buttons[i]))
            Free ((LPWSTR)infoPtr->buttons[i].iString);
5201
    Free (infoPtr->buttons);
Alexandre Julliard's avatar
Alexandre Julliard committed
5202

Alexandre Julliard's avatar
Alexandre Julliard committed
5203 5204 5205
    /* delete strings */
    if (infoPtr->strings) {
	for (i = 0; i < infoPtr->nNumStrings; i++)
5206
	    Free (infoPtr->strings[i]);
Alexandre Julliard's avatar
Alexandre Julliard committed
5207

5208
	Free (infoPtr->strings);
Alexandre Julliard's avatar
Alexandre Julliard committed
5209 5210
    }

5211 5212 5213
    /* destroy internal image list */
    if (infoPtr->himlInt)
	ImageList_Destroy (infoPtr->himlInt);
Alexandre Julliard's avatar
Alexandre Julliard committed
5214

5215 5216 5217 5218
	TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
	TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
	TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);

Alexandre Julliard's avatar
Alexandre Julliard committed
5219
    /* delete default font */
5220
    DeleteObject (infoPtr->hDefaultFont);
5221
        
5222
    CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
Alexandre Julliard's avatar
Alexandre Julliard committed
5223

Alexandre Julliard's avatar
Alexandre Julliard committed
5224
    /* free toolbar info data */
5225
    SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5226
    Free (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5227 5228 5229 5230 5231

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5232
static LRESULT
5233
TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5234
{
5235
    NMTBCUSTOMDRAW tbcd;
5236 5237
    INT ret = FALSE;
    DWORD ntfret;
5238
    HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5239
    DWORD dwEraseCustDraw = 0;
5240

5241 5242 5243 5244
    /* the app has told us not to redraw the toolbar */
    if (!infoPtr->bDoRedraw)
        return FALSE;

5245
    if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5246 5247 5248
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
	tbcd.nmcd.hdc = (HDC)wParam;
5249
	ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5250
	dwEraseCustDraw = ntfret & 0xffff;
5251

5252
	/* FIXME: in general the return flags *can* be or'ed together */
5253
	switch (dwEraseCustDraw)
5254 5255 5256 5257 5258 5259
	    {
	    case CDRF_DODEFAULT:
		break;
	    case CDRF_SKIPDEFAULT:
		return TRUE;
	    default:
5260
		FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5261
		      infoPtr->hwndSelf, ntfret);
5262 5263
	    }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5264

5265
    /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5266 5267
     * to my parent for processing.
     */
5268
    if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5269 5270 5271 5272 5273 5274
	POINT pt, ptorig;
	HDC hdc = (HDC)wParam;
	HWND parent;

	pt.x = 0;
	pt.y = 0;
5275 5276
	parent = GetParent(infoPtr->hwndSelf);
	MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5277
	OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5278
	ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5279 5280
	SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
    }
Guy Albertelli's avatar
Guy Albertelli committed
5281
    if (!ret)
5282
	ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
5283

5284
    if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5285 5286 5287
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
	tbcd.nmcd.hdc = (HDC)wParam;
5288
	ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5289 5290
	dwEraseCustDraw = ntfret & 0xffff;
	switch (dwEraseCustDraw)
5291 5292 5293 5294 5295 5296
	    {
	    case CDRF_DODEFAULT:
		break;
	    case CDRF_SKIPDEFAULT:
		return TRUE;
	    default:
5297
		FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5298
		      infoPtr->hwndSelf, ntfret);
5299 5300
	    }
    }
5301
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
5302 5303 5304
}


5305
static inline LRESULT
5306
TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
Eric Pouech's avatar
Eric Pouech committed
5307
{
5308
    return (LRESULT)infoPtr->hFont;
Eric Pouech's avatar
Eric Pouech committed
5309 5310 5311
}


5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
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
5346
TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5347 5348 5349 5350 5351 5352 5353
{
    NMKEY nmkey;

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

    if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5354
        return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372

    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),
5373
                (LPARAM)infoPtr->hwndSelf);
5374 5375 5376 5377 5378 5379 5380 5381
        }
        break;
    }

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5382
static LRESULT
5383
TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5384
{
5385
    POINT pt;
5386
    BOOL button;
Alexandre Julliard's avatar
Alexandre Julliard committed
5387

5388 5389
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5390
    TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5391

5392
    if (button)
5393
        TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5394
    else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5395
	TOOLBAR_Customize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5396 5397 5398 5399 5400 5401

    return 0;
}


static LRESULT
5402
TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5403 5404
{
    TBUTTON_INFO *btnPtr;
5405 5406
    POINT pt;
    INT   nHit;
5407
    NMTOOLBARA nmtb;
5408
    NMMOUSE nmmouse;
Robert Shearman's avatar
Robert Shearman committed
5409
    BOOL bDragKeyPressed;
5410
    BOOL button;
Robert Shearman's avatar
Robert Shearman committed
5411

5412 5413
    TRACE("\n");

5414
    if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
Robert Shearman's avatar
Robert Shearman committed
5415 5416 5417
        bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
    else
        bDragKeyPressed = (wParam & MK_SHIFT);
Alexandre Julliard's avatar
Alexandre Julliard committed
5418

Alexandre Julliard's avatar
Alexandre Julliard committed
5419
    if (infoPtr->hwndToolTip)
5420
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5421 5422
			    WM_LBUTTONDOWN, wParam, lParam);

5423 5424
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5425
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5426

5427 5428
    if (button)
        btnPtr = &infoPtr->buttons[nHit];
5429

5430
    if (button && bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
Robert Shearman's avatar
Robert Shearman committed
5431 5432
    {
        infoPtr->nButtonDrag = nHit;
5433
        SetCapture (infoPtr->hwndSelf);
Robert Shearman's avatar
Robert Shearman committed
5434 5435 5436 5437 5438 5439 5440
        
        /* 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);
    }
5441
    else if (button)
Robert Shearman's avatar
Robert Shearman committed
5442
    {
5443 5444 5445 5446 5447
	RECT arrowRect;
	infoPtr->nOldHit = nHit;

	CopyRect(&arrowRect, &btnPtr->rect);
	arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5448

5449
	/* for EX_DRAWDDARROWS style,  click must be in the drop-down arrow rect */
5450 5451 5452 5453 5454
	if ((btnPtr->fsState & TBSTATE_ENABLED) && 
	     ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
	      ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
	       ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
	       (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5455
	{
5456
	    LRESULT res;
5457 5458

	    /* draw in pressed state */
5459 5460 5461 5462
	    if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
	        btnPtr->fsState |= TBSTATE_PRESSED;
	    else
	        btnPtr->bDropDownPressed = TRUE;
5463
	    RedrawWindow(infoPtr->hwndSelf,&btnPtr->rect,0,
5464
			RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5465

5466
	    memset(&nmtb, 0, sizeof(nmtb));
5467
	    nmtb.iItem = btnPtr->idCommand;
5468
	    nmtb.rcButton = btnPtr->rect;
5469 5470
	    res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
				  TBN_DROPDOWN);
5471 5472 5473 5474 5475 5476 5477
	    TRACE("TBN_DROPDOWN responded with %ld\n", res);

            if (res != TBDDRET_TREATPRESSED)
            {
                MSG msg;

                /* redraw button in unpressed state */
5478 5479 5480 5481
	        if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
       	            btnPtr->fsState &= ~TBSTATE_PRESSED;
       	        else
       	            btnPtr->bDropDownPressed = FALSE;
5482
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5483 5484 5485 5486

                /* find and set hot item
                 * NOTE: native doesn't do this, but that is a bug */
                GetCursorPos(&pt);
5487
                ScreenToClient(infoPtr->hwndSelf, &pt);
5488 5489
                nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
                if (!infoPtr->bAnchor || button)
5490
                    TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5491
                
5492 5493
                /* remove any left mouse button down or double-click messages
                 * so that we can get a toggle effect on the button */
5494 5495
                while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
                       PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5496 5497
                    ;

5498
		return 0;
5499
            }
5500 5501 5502 5503
	    /* otherwise drop through and process as pushed */
       	}
	infoPtr->bCaptured = TRUE;
	infoPtr->nButtonDown = nHit;
5504
	infoPtr->bDragOutSent = FALSE;
5505

5506
	btnPtr->fsState |= TBSTATE_PRESSED;
5507

5508
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5509

5510
        if (btnPtr->fsState & TBSTATE_ENABLED)
5511 5512 5513
	    InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
	UpdateWindow(infoPtr->hwndSelf);
	SetCapture (infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
5514 5515
    }

5516
    if (button)
5517
    {
5518
        memset(&nmtb, 0, sizeof(nmtb));
5519 5520 5521
        nmtb.iItem = btnPtr->idCommand;
        TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
    }
Robert Shearman's avatar
Robert Shearman committed
5522

5523 5524 5525
    nmmouse.dwHitInfo = nHit;

    /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5526
    if (!button)
5527 5528 5529 5530 5531 5532 5533
        nmmouse.dwItemSpec = -1;
    else
    {
        nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
        nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
    }

5534
    ClientToScreen(infoPtr->hwndSelf, &pt);
5535 5536
    nmmouse.pt = pt;

5537
    if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5538
        return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5539

Alexandre Julliard's avatar
Alexandre Julliard committed
5540 5541 5542 5543
    return 0;
}

static LRESULT
5544
TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5545 5546
{
    TBUTTON_INFO *btnPtr;
5547 5548 5549
    POINT pt;
    INT   nHit;
    INT   nOldIndex = -1;
5550 5551 5552
    NMHDR hdr;
    NMMOUSE nmmouse;
    NMTOOLBARA nmtb;
5553
    BOOL button;
Alexandre Julliard's avatar
Alexandre Julliard committed
5554

Alexandre Julliard's avatar
Alexandre Julliard committed
5555
    if (infoPtr->hwndToolTip)
5556
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5557 5558
			    WM_LBUTTONUP, wParam, lParam);

5559 5560
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5561
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5562

5563
    if (!infoPtr->bAnchor || button)
5564
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5565

Robert Shearman's avatar
Robert Shearman committed
5566 5567 5568 5569 5570 5571 5572 5573 5574
    if (infoPtr->nButtonDrag >= 0) {
        RECT rcClient;
        NMHDR hdr;

        btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
        ReleaseCapture();
        /* reset cursor */
        SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));

5575
        GetClientRect(infoPtr->hwndSelf, &rcClient);
Robert Shearman's avatar
Robert Shearman committed
5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592
        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)
            {
                /* if the button is moved sightly left and we have a
                 * 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))
5593
                        TOOLBAR_DeleteButton(infoPtr, nButton - 1);
Robert Shearman's avatar
Robert Shearman committed
5594 5595 5596 5597 5598 5599 5600
                }
                else /* else insert a separator before the dragged button */
                {
                    TBBUTTON tbb;
                    memset(&tbb, 0, sizeof(tbb));
                    tbb.fsStyle = BTNS_SEP;
                    tbb.iString = -1;
5601
                    TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
Robert Shearman's avatar
Robert Shearman committed
5602 5603 5604 5605 5606 5607 5608
                }
            }
            else
            {
                if (nButton == -1)
                {
                    if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5609
                        TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
Robert Shearman's avatar
Robert Shearman committed
5610
                    else
5611
                        TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
Robert Shearman's avatar
Robert Shearman committed
5612 5613
                }
                else
5614
                    TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
Robert Shearman's avatar
Robert Shearman committed
5615 5616 5617 5618 5619
            }
        }
        else
        {
            TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5620
            TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
Robert Shearman's avatar
Robert Shearman committed
5621 5622 5623
        }

        /* button under cursor changed so need to re-set hot item */
5624
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
Robert Shearman's avatar
Robert Shearman committed
5625 5626 5627 5628 5629
        infoPtr->nButtonDrag = -1;

        TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
    }
    else if (infoPtr->nButtonDown >= 0) {
Alexandre Julliard's avatar
Alexandre Julliard committed
5630
	btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
Alexandre Julliard's avatar
Alexandre Julliard committed
5631
	btnPtr->fsState &= ~TBSTATE_PRESSED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5632

5633 5634
	if (btnPtr->fsStyle & BTNS_CHECK) {
		if (btnPtr->fsStyle & BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
5635
		    nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5636
			nHit);
5637
		    if ((nOldIndex != nHit) &&
Alexandre Julliard's avatar
Alexandre Julliard committed
5638 5639
			(nOldIndex != -1))
			infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5640
		    btnPtr->fsState |= TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5641 5642 5643 5644 5645 5646 5647
		}
		else {
		    if (btnPtr->fsState & TBSTATE_CHECKED)
			btnPtr->fsState &= ~TBSTATE_CHECKED;
		    else
			btnPtr->fsState |= TBSTATE_CHECKED;
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
5648
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
5649

Robert Shearman's avatar
Robert Shearman committed
5650
        if (nOldIndex != -1)
5651
            InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5652

5653 5654 5655
	/*
	 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
	 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5656
	 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5657
	 */
5658 5659
	if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
	    ReleaseCapture ();
5660
	infoPtr->nButtonDown = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
5661

5662
	/* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5663
	TOOLBAR_SendNotify (&hdr, infoPtr,
5664
			NM_RELEASEDCAPTURE);
5665

5666
	/* native issues TBN_ENDDRAG here, if _LBUTTONDOWN issued the
5667 5668
	 * TBN_BEGINDRAG
	 */
5669
	memset(&nmtb, 0, sizeof(nmtb));
5670
	nmtb.iItem = btnPtr->idCommand;
5671 5672
	TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
			TBN_ENDDRAG);
5673

5674 5675
	if (btnPtr->fsState & TBSTATE_ENABLED)
	{
5676
	    SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5677
	      MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5678 5679

            /* In case we have just been destroyed... */
5680
            if(!IsWindow(infoPtr->hwndSelf))
5681
                return 0;
5682 5683
        }
    }
5684

5685 5686 5687 5688
    /* !!! Undocumented - toolbar at 4.71 level and above sends
    * NM_CLICK with the NMMOUSE structure. */
    nmmouse.dwHitInfo = nHit;

5689
    if (!button)
5690 5691 5692 5693 5694
        nmmouse.dwItemSpec = -1;
    else
    {
        nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
        nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5695
    }
5696

5697
    ClientToScreen(infoPtr->hwndSelf, &pt);
5698 5699 5700
    nmmouse.pt = pt;

    if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5701
        return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5702

Alexandre Julliard's avatar
Alexandre Julliard committed
5703 5704 5705
    return 0;
}

5706
static LRESULT
5707
TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5708
{
5709
    INT nHit;
5710 5711
    NMMOUSE nmmouse;
    POINT pt;
5712
    BOOL button;
5713

5714 5715
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5716

5717
    nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5718
    nmmouse.dwHitInfo = nHit;
5719

5720
    if (!button) {
5721 5722 5723 5724 5725 5726
	nmmouse.dwItemSpec = -1;
    } else {
	nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
	nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
    }

5727
    ClientToScreen(infoPtr->hwndSelf, &pt);
5728
    nmmouse.pt = pt;
5729

5730
    if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5731
        return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5732 5733 5734 5735

    return 0;
}

5736
static LRESULT
5737
TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5738
{
5739
    NMHDR nmhdr;
5740

5741
    if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5742
        return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5743 5744 5745 5746

    return 0;
}

5747
static LRESULT
5748
TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760
{
    TBUTTON_INFO *btnPtr;

    infoPtr->bCaptured = FALSE;

    if (infoPtr->nButtonDown >= 0)
    {
        btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
       	btnPtr->fsState &= ~TBSTATE_PRESSED;

        infoPtr->nOldHit = -1;

5761
        if (btnPtr->fsState & TBSTATE_ENABLED)
5762
            InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5763 5764 5765 5766
    }
    return 0;
}

5767
static LRESULT
5768
TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5769
{
5770 5771
    /* don't remove hot effects when in anchor highlighting mode or when a
     * drop-down button is pressed */
5772 5773 5774 5775 5776 5777
    if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
    {
        TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
        if (!hotBtnPtr->bDropDownPressed)
            TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
    }
5778

5779 5780 5781
    if (infoPtr->nOldHit < 0)
      return TRUE;

5782 5783 5784 5785
    /* If the last button we were over is depressed then make it not */
    /* depressed and redraw it */
    if(infoPtr->nOldHit == infoPtr->nButtonDown)
    {
5786 5787 5788
      TBUTTON_INFO *btnPtr;
      RECT rc1;

5789 5790 5791 5792
      btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];

      btnPtr->fsState &= ~TBSTATE_PRESSED;

5793
      rc1 = btnPtr->rect;
5794
      InflateRect (&rc1, 1, 1);
5795
      InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5796 5797
    }

5798 5799 5800 5801 5802 5803 5804 5805 5806
    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;
    }

5807 5808 5809 5810
    infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5811 5812

static LRESULT
5813
TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5814
{
5815
    POINT pt;
5816
    TRACKMOUSEEVENT trackinfo;
5817 5818
    INT   nHit;
    TBUTTON_INFO *btnPtr;
5819
    BOOL button;
5820
    
5821 5822 5823
    if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
        TOOLBAR_TooltipCreateControl(infoPtr);
    
5824
    if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5825 5826 5827 5828 5829 5830
        /* 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);
5831

5832
        /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5833
        if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5834
            trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5835
            trackinfo.hwndTrack = infoPtr->hwndSelf;
5836

5837 5838 5839
            /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
            /* and can properly deactivate the hot toolbar button */
            _TrackMouseEvent(&trackinfo);
5840
        }
5841
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5842

Alexandre Julliard's avatar
Alexandre Julliard committed
5843
    if (infoPtr->hwndToolTip)
5844
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5845 5846
			    WM_MOUSEMOVE, wParam, lParam);

5847 5848
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5849

5850
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5851

5852
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
5853
        && (!infoPtr->bAnchor || button))
5854
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5855

5856 5857
    if (infoPtr->nOldHit != nHit)
    {
5858 5859
        if (infoPtr->bCaptured)
        {
5860 5861 5862 5863 5864 5865 5866 5867 5868
            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;
            }

5869 5870 5871
            btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
            if (infoPtr->nOldHit == infoPtr->nButtonDown) {
                btnPtr->fsState &= ~TBSTATE_PRESSED;
5872
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5873 5874 5875
            }
            else if (nHit == infoPtr->nButtonDown) {
                btnPtr->fsState |= TBSTATE_PRESSED;
5876
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5877 5878 5879
            }
            infoPtr->nOldHit = nHit;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
5880
    }
5881

Alexandre Julliard's avatar
Alexandre Julliard committed
5882 5883 5884 5885
    return 0;
}


5886
static inline LRESULT
5887
TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5888
{
5889
/*    if (wndPtr->dwStyle & CCS_NODIVIDER) */
5890
	return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5891 5892
/*    else */
/*	return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
Alexandre Julliard's avatar
Alexandre Julliard committed
5893
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5894 5895


5896
static inline LRESULT
5897
TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5898
{
5899
    if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
5900
	((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
5901

5902
    return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
5903
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5904 5905 5906


static LRESULT
5907
TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
5908
{
Alexandre Julliard's avatar
Alexandre Julliard committed
5909
    TOOLBAR_INFO *infoPtr;
5910
    DWORD styleadd = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
5911

Alexandre Julliard's avatar
Alexandre Julliard committed
5912
    /* allocate memory for info structure */
5913
    infoPtr = Alloc (sizeof(TOOLBAR_INFO));
5914
    SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5915

Alexandre Julliard's avatar
Alexandre Julliard committed
5916
    /* paranoid!! */
Alexandre Julliard's avatar
Alexandre Julliard committed
5917
    infoPtr->dwStructSize = sizeof(TBBUTTON);
5918
    infoPtr->nRows = 1;
5919
    infoPtr->nWidth = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
5920

5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937
    /* 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;
5938 5939
    infoPtr->hwndNotify = lpcs->hwndParent;
    infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950
    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);
5951
    infoPtr->dwStyle = lpcs->style;
5952 5953
    infoPtr->tbim.iButton = -1;

Alexandre Julliard's avatar
Alexandre Julliard committed
5954
    /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
5955 5956 5957
    if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
        HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
	SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
5958
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5959

5960 5961 5962
    /* native control does:
     *    Get a lot of colors and brushes
     *    WM_NOTIFYFORMAT
5963 5964
     *    SystemParametersInfoW(0x1f, 0x3c, adr1, 0)
     *    CreateFontIndirectW(adr1)
5965 5966 5967
     *    CreateBitmap(0x27, 0x24, 1, 1, 0)
     *    hdc = GetDC(toolbar)
     *    GetSystemMetrics(0x48)
5968
     *    fnt2=CreateFontW(0xe, 0, 0, 0, 0x190, 0, 0, 0, 0, 2,
5969 5970
     *                     0, 0, 0, 0, "MARLETT")
     *    oldfnt = SelectObject(hdc, fnt2)
5971 5972
     *    GetCharWidthW(hdc, 0x36, 0x36, adr2)
     *    GetTextMetricsW(hdc, adr3)
5973 5974 5975 5976
     *    SelectObject(hdc, oldfnt)
     *    DeleteObject(fnt2)
     *    ReleaseDC(hdc)
     *    InvalidateRect(toolbar, 0, 1)
5977 5978
     *    SetWindowLongW(toolbar, 0, addr)
     *    SetWindowLongW(toolbar, -16, xxx)  **sometimes**
5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997
     *                                          WM_STYLECHANGING
     *                             CallWinEx   old         new
     *                       ie 1  0x56000a4c  0x46000a4c  0x56008a4d
     *                       ie 2  0x4600094c  0x4600094c  0x4600894d
     *                       ie 3  0x56000b4c  0x46000b4c  0x56008b4d
     *                      rebar  0x50008844  0x40008844  0x50008845
     *                      pager  0x50000844  0x40000844  0x50008845
     *                    IC35mgr  0x5400084e  **nochange**
     *           on entry to _NCCREATE         0x5400084e
     *                    rowlist  0x5400004e  **nochange**
     *           on entry to _NCCREATE         0x5400004e
     *
     */

    /* 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!!!!
5998
     * Somehow, the only cases of this seem to be MFC programs.
5999 6000 6001 6002 6003 6004
     *
     * Note also that the addition of _TRANSPARENT occurs *only* here. It
     * does not occur in the WM_STYLECHANGING routine.
     *    (Guy Albertelli   9/2001)
     *
     */
6005
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
6006
        && !(lpcs->style & TBSTYLE_TRANSPARENT))
6007
	styleadd |= TBSTYLE_TRANSPARENT;
6008
    if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6009
	styleadd |= CCS_TOP;   /* default to top */
6010
	SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6011 6012
    }

6013
    return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
Alexandre Julliard's avatar
Alexandre Julliard committed
6014 6015 6016 6017
}


static LRESULT
6018
TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6019
{
6020
    DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6021
    RECT rcWindow;
6022
    HDC hdc;
Alexandre Julliard's avatar
Alexandre Julliard committed
6023

6024 6025
    if (dwStyle & WS_MINIMIZE)
	return 0; /* Nothing to do */
Alexandre Julliard's avatar
Alexandre Julliard committed
6026

6027
    DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6028

6029
    if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
Alexandre Julliard's avatar
Alexandre Julliard committed
6030 6031
	return 0;

6032 6033 6034 6035
    if (!(dwStyle & CCS_NODIVIDER))
    {
	GetWindowRect (hwnd, &rcWindow);
	OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6036
	if( dwStyle & WS_BORDER )
6037
	    InflateRect (&rcWindow, -1, -1);
6038 6039
	DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6040

6041
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
6042 6043 6044 6045 6046

    return 0;
}


6047 6048
/* 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
6049
{
6050
    int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
6051

6052
    TRACE("button index = %d\n", index);
6053

6054
    Free (infoPtr->pszTooltipText);
6055
    infoPtr->pszTooltipText = NULL;
6056

6057 6058
    if (index < 0)
        return 0;
6059

6060
    if (infoPtr->bUnicode)
6061 6062 6063
    {
        WCHAR wszBuffer[INFOTIPSIZE+1];
        NMTBGETINFOTIPW tbgit;
6064
        unsigned int len; /* in chars */
6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083

        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));

        len = strlenW(tbgit.pszText);
        if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6084
            infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096
            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;
        }
6097
    }
6098 6099 6100 6101
    else
    {
        CHAR szBuffer[INFOTIPSIZE+1];
        NMTBGETINFOTIPA tbgit;
6102
        unsigned int len; /* in chars */
6103

6104 6105
        szBuffer[0] = '\0';
        szBuffer[INFOTIPSIZE] = '\0';
6106

6107 6108 6109 6110
        tbgit.pszText = szBuffer;
        tbgit.cchTextMax = INFOTIPSIZE;
        tbgit.iItem = lpnmtdi->hdr.idFrom;
        tbgit.lParam = infoPtr->buttons[index].dwData;
Alexandre Julliard's avatar
Alexandre Julliard committed
6111

6112
        TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
Alexandre Julliard's avatar
Alexandre Julliard committed
6113

6114 6115
        TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));

6116 6117
        len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
        if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6118 6119 6120 6121
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6122
            infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6123 6124
            if (infoPtr->pszTooltipText)
            {
6125
                MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6126 6127 6128 6129
                lpnmtdi->lpszText = infoPtr->pszTooltipText;
                return 0;
            }
        }
6130
        else if (tbgit.pszText && tbgit.pszText[0])
6131
        {
6132 6133
            MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
                                lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6134 6135 6136
            return 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6137

6138 6139 6140 6141 6142 6143
    /* 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]);
6144
        unsigned int len = pszText ? strlenW(pszText) : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
6145

6146 6147 6148 6149 6150 6151 6152
        TRACE("using button hidden text %s\n", debugstr_w(pszText));

        if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6153
            infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166
            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
6167

6168
    TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
Alexandre Julliard's avatar
Alexandre Julliard committed
6169

6170 6171
    /* last resort: send notification on to app */
    /* FIXME: find out what is really used here */
6172
    return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6173 6174 6175
}


6176
static inline LRESULT
6177
TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6178 6179 6180 6181 6182
{
    switch (lpnmh->code)
    {
    case PGN_CALCSIZE:
    {
6183
        LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195

        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
6196 6197
    }

6198 6199
    case PGN_SCROLL:
    {
6200
        LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6201 6202 6203 6204 6205 6206 6207 6208 6209

        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:
6210
        return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6211 6212

    case TTN_GETDISPINFOA:
6213
        FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6214 6215 6216 6217 6218
        return 0;

    default:
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6219 6220 6221
}


6222
static LRESULT
6223
TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6224
{
6225
    LRESULT format;
6226

6227
    TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6228

6229
    if (lParam == NF_QUERY)
6230 6231
        return NFR_UNICODE;

6232
    if (lParam == NF_REQUERY) {
6233
	format = SendMessageW(infoPtr->hwndNotify,
6234
			 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6235 6236 6237 6238
	if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
	    ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
		format);
	    format = NFR_ANSI;
6239
	}
6240
	return format;
6241
    }
6242
    return 0;
6243 6244 6245
}


Alexandre Julliard's avatar
Alexandre Julliard committed
6246
static LRESULT
6247
TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6248
{
6249 6250
    HDC hdc;
    PAINTSTRUCT ps;
Alexandre Julliard's avatar
Alexandre Julliard committed
6251

6252
    /* fill ps.rcPaint with a default rect */
6253
    ps.rcPaint = infoPtr->rcBound;
6254

6255
    hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6256

6257
    TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6258

6259 6260
    TOOLBAR_Refresh (infoPtr, hdc, &ps);
    if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6261

Alexandre Julliard's avatar
Alexandre Julliard committed
6262 6263 6264 6265
    return 0;
}


6266
static LRESULT
6267
TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6268 6269 6270 6271 6272 6273 6274 6275 6276 6277
{
    TRACE("nHotItem = %d\n", infoPtr->nHotItem);

    /* make first item hot */
    if (infoPtr->nNumButtons > 0)
        TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);

    return 0;
}

6278
static LRESULT
6279
TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6280
{
6281
    TRACE("font=%p redraw=%d\n", hFont, Redraw);
6282
    
6283
    if (hFont == 0)
6284 6285
        infoPtr->hFont = infoPtr->hDefaultFont;
    else
6286
        infoPtr->hFont = hFont;
6287

6288
    TOOLBAR_CalcToolbar(infoPtr);
6289

6290
    if (Redraw)
6291
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6292 6293
    return 1;
}
6294

6295
static LRESULT
6296
TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6297 6298 6299 6300 6301 6302
     /*****************************************************
      *
      * Function;
      *  Handles the WM_SETREDRAW message.
      *
      * Documentation:
6303
      *  According to testing V4.71 of COMCTL32 returns the
6304 6305
      *  *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
6306
      *  (For laughs see the "consistency" with same function
6307 6308 6309 6310 6311 6312
      *   in rebar.)
      *
      *****************************************************/
{
    BOOL oldredraw = infoPtr->bDoRedraw;

6313
    TRACE("set to %s\n",
6314 6315 6316 6317 6318 6319 6320 6321 6322
	  (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
6323
static LRESULT
6324
TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
6325
{
6326
    TRACE("sizing toolbar!\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
6327

6328 6329 6330 6331 6332 6333
    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;
6334

6335
        GetClientRect(infoPtr->hwndSelf, &client);
6336
        if(client.right > infoPtr->client_rect.right)
6337
        {
6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354
            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;
6355 6356
        }

6357 6358 6359 6360 6361 6362 6363 6364
        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))
6365
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6366
    }
6367 6368
    GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
    TOOLBAR_AutoSize(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6369 6370
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6371 6372


Alexandre Julliard's avatar
Alexandre Julliard committed
6373
static LRESULT
6374
TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
6375
{
6376 6377
    if (nType == GWL_STYLE)
    {
6378 6379
        DWORD dwOldStyle = infoPtr->dwStyle;

6380 6381 6382 6383 6384
        if (lpStyle->styleNew & TBSTYLE_LIST)
            infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
        else
            infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;

6385
        TRACE("new style 0x%08x\n", lpStyle->styleNew);
6386

6387
        infoPtr->dwStyle = lpStyle->styleNew;
6388
        TOOLBAR_CheckStyle (infoPtr);
6389

6390
        if ((dwOldStyle ^ lpStyle->styleNew) & (TBSTYLE_WRAPABLE | CCS_VERT))
6391
            TOOLBAR_LayoutToolbar(infoPtr);
6392

6393
        /* only resize if one of the CCS_* styles was changed */
6394
        if ((dwOldStyle ^ lpStyle->styleNew) & COMMON_STYLES)
6395
        {
6396
            TOOLBAR_AutoSize (infoPtr);
6397
    
6398
            InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6399
        }
6400 6401
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
6402 6403
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6404 6405


6406
static LRESULT
6407
TOOLBAR_SysColorChange (void)
6408 6409 6410 6411 6412 6413 6414
{
    COMCTL32_RefreshSysColors();

    return 0;
}


6415 6416 6417 6418 6419 6420 6421 6422 6423
/* update theme after a WM_THEMECHANGED message */
static LRESULT theme_changed (HWND hwnd)
{
    HTHEME theme = GetWindowTheme (hwnd);
    CloseThemeData (theme);
    OpenThemeData (hwnd, themeClass);
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
6424

6425
static LRESULT WINAPI
6426
ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6427
{
6428
    TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6429

6430
    TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6431 6432
	  hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);

6433
    if (!infoPtr && (uMsg != WM_NCCREATE))
6434
	return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6435

Alexandre Julliard's avatar
Alexandre Julliard committed
6436 6437 6438
    switch (uMsg)
    {
	case TB_ADDBITMAP:
6439
	    return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6440

6441
	case TB_ADDBUTTONSA:
6442
	case TB_ADDBUTTONSW:
6443 6444
	    return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
	                                uMsg == TB_ADDBUTTONSW);
6445
	case TB_ADDSTRINGA:
6446
	    return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6447

6448
	case TB_ADDSTRINGW:
6449
	    return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6450 6451

	case TB_AUTOSIZE:
6452
	    return TOOLBAR_AutoSize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6453 6454

	case TB_BUTTONCOUNT:
6455
	    return TOOLBAR_ButtonCount (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6456 6457

	case TB_BUTTONSTRUCTSIZE:
6458
	    return TOOLBAR_ButtonStructSize (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6459 6460

	case TB_CHANGEBITMAP:
6461
	    return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6462

Alexandre Julliard's avatar
Alexandre Julliard committed
6463
	case TB_CHECKBUTTON:
6464
	    return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6465 6466

	case TB_COMMANDTOINDEX:
6467
	    return TOOLBAR_CommandToIndex (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6468

Alexandre Julliard's avatar
Alexandre Julliard committed
6469
	case TB_CUSTOMIZE:
6470
	    return TOOLBAR_Customize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6471 6472

	case TB_DELETEBUTTON:
6473
	    return TOOLBAR_DeleteButton (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6474 6475

	case TB_ENABLEBUTTON:
6476
	    return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6477

6478
	case TB_GETANCHORHIGHLIGHT:
6479
	    return TOOLBAR_GetAnchorHighlight (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6480 6481

	case TB_GETBITMAP:
6482
	    return TOOLBAR_GetBitmap (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6483

Alexandre Julliard's avatar
Alexandre Julliard committed
6484
	case TB_GETBITMAPFLAGS:
6485
	    return TOOLBAR_GetBitmapFlags ();
Alexandre Julliard's avatar
Alexandre Julliard committed
6486 6487

	case TB_GETBUTTON:
6488
	    return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6489

6490
	case TB_GETBUTTONINFOA:
6491
	case TB_GETBUTTONINFOW:
6492 6493
	    return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
	                                   uMsg == TB_GETBUTTONINFOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
6494
	case TB_GETBUTTONSIZE:
6495
	    return TOOLBAR_GetButtonSize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6496

6497
	case TB_GETBUTTONTEXTA:
6498
	case TB_GETBUTTONTEXTW:
6499 6500
	    return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
	                                  uMsg == TB_GETBUTTONTEXTW);
6501

Alexandre Julliard's avatar
Alexandre Julliard committed
6502
	case TB_GETDISABLEDIMAGELIST:
6503
	    return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6504

Alexandre Julliard's avatar
Alexandre Julliard committed
6505
	case TB_GETEXTENDEDSTYLE:
6506
	    return TOOLBAR_GetExtendedStyle (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6507 6508

	case TB_GETHOTIMAGELIST:
6509
	    return TOOLBAR_GetHotImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6510

6511
	case TB_GETHOTITEM:
6512
	    return TOOLBAR_GetHotItem (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6513 6514

	case TB_GETIMAGELIST:
6515
	    return TOOLBAR_GetDefImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6516

6517
	case TB_GETINSERTMARK:
6518
	    return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6519 6520

	case TB_GETINSERTMARKCOLOR:
6521
	    return TOOLBAR_GetInsertMarkColor (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6522 6523

	case TB_GETITEMRECT:
6524
	    return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6525

Alexandre Julliard's avatar
Alexandre Julliard committed
6526
	case TB_GETMAXSIZE:
6527
	    return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6528

6529
/*	case TB_GETOBJECT:			*/ /* 4.71 */
6530 6531

	case TB_GETPADDING:
6532
	    return TOOLBAR_GetPadding (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6533 6534

	case TB_GETRECT:
6535
	    return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6536 6537

	case TB_GETROWS:
6538
	    return TOOLBAR_GetRows (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6539 6540

	case TB_GETSTATE:
6541
	    return TOOLBAR_GetState (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6542

Robert Shearman's avatar
Robert Shearman committed
6543
	case TB_GETSTRINGA:
6544
            return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
Robert Shearman's avatar
Robert Shearman committed
6545 6546

	case TB_GETSTRINGW:
6547
	    return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
Robert Shearman's avatar
Robert Shearman committed
6548

Alexandre Julliard's avatar
Alexandre Julliard committed
6549
	case TB_GETSTYLE:
6550
	    return TOOLBAR_GetStyle (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6551

Alexandre Julliard's avatar
Alexandre Julliard committed
6552
	case TB_GETTEXTROWS:
6553
	    return TOOLBAR_GetTextRows (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6554 6555

	case TB_GETTOOLTIPS:
6556
	    return TOOLBAR_GetToolTips (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6557

Alexandre Julliard's avatar
Alexandre Julliard committed
6558
	case TB_GETUNICODEFORMAT:
6559
	    return TOOLBAR_GetUnicodeFormat (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6560 6561

	case TB_HIDEBUTTON:
6562
	    return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6563 6564

	case TB_HITTEST:
6565
	    return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6566

Alexandre Julliard's avatar
Alexandre Julliard committed
6567
	case TB_INDETERMINATE:
6568
	    return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6569

6570
	case TB_INSERTBUTTONA:
6571
	case TB_INSERTBUTTONW:
6572 6573
	    return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
	                                 uMsg == TB_INSERTBUTTONW);
6574

6575
/*	case TB_INSERTMARKHITTEST:		*/ /* 4.71 */
Alexandre Julliard's avatar
Alexandre Julliard committed
6576 6577

	case TB_ISBUTTONCHECKED:
6578
	    return TOOLBAR_IsButtonChecked (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6579 6580

	case TB_ISBUTTONENABLED:
6581
	    return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6582 6583

	case TB_ISBUTTONHIDDEN:
6584
	    return TOOLBAR_IsButtonHidden (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6585 6586

	case TB_ISBUTTONHIGHLIGHTED:
6587
	    return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6588 6589

	case TB_ISBUTTONINDETERMINATE:
6590
	    return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6591 6592

	case TB_ISBUTTONPRESSED:
6593
	    return TOOLBAR_IsButtonPressed (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6594

6595
	case TB_LOADIMAGES:
6596
	    return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6597 6598 6599

	case TB_MAPACCELERATORA:
	case TB_MAPACCELERATORW:
6600
	    return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6601 6602

	case TB_MARKBUTTON:
6603
	    return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6604

Robert Shearman's avatar
Robert Shearman committed
6605
	case TB_MOVEBUTTON:
6606
	    return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6607 6608

	case TB_PRESSBUTTON:
6609
	    return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6610

6611
	case TB_REPLACEBITMAP:
6612
            return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6613

6614
	case TB_SAVERESTOREA:
6615
	    return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6616

6617
	case TB_SAVERESTOREW:
6618
	    return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6619 6620

	case TB_SETANCHORHIGHLIGHT:
6621
	    return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6622 6623

	case TB_SETBITMAPSIZE:
6624
	    return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6625

6626
	case TB_SETBUTTONINFOA:
6627
	case TB_SETBUTTONINFOW:
6628 6629
	    return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
                                          uMsg == TB_SETBUTTONINFOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
6630
	case TB_SETBUTTONSIZE:
6631
	    return TOOLBAR_SetButtonSize (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6632

Alexandre Julliard's avatar
Alexandre Julliard committed
6633
	case TB_SETBUTTONWIDTH:
6634
	    return TOOLBAR_SetButtonWidth (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6635 6636

	case TB_SETCMDID:
6637
	    return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6638

Alexandre Julliard's avatar
Alexandre Julliard committed
6639
	case TB_SETDISABLEDIMAGELIST:
6640
	    return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6641

Alexandre Julliard's avatar
Alexandre Julliard committed
6642
	case TB_SETDRAWTEXTFLAGS:
6643
	    return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6644 6645

	case TB_SETEXTENDEDSTYLE:
6646
	    return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6647 6648

	case TB_SETHOTIMAGELIST:
6649
	    return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6650

6651
	case TB_SETHOTITEM:
6652
	    return TOOLBAR_SetHotItem (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6653 6654

	case TB_SETIMAGELIST:
6655
	    return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6656 6657

	case TB_SETINDENT:
6658
	    return TOOLBAR_SetIndent (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6659

6660
	case TB_SETINSERTMARK:
6661
	    return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6662 6663

	case TB_SETINSERTMARKCOLOR:
6664
	    return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6665

Alexandre Julliard's avatar
Alexandre Julliard committed
6666
	case TB_SETMAXTEXTROWS:
6667
	    return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6668

6669
	case TB_SETPADDING:
6670
	    return TOOLBAR_SetPadding (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6671 6672

	case TB_SETPARENT:
6673
	    return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6674 6675

	case TB_SETROWS:
6676
	    return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6677 6678

	case TB_SETSTATE:
6679
	    return TOOLBAR_SetState (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6680 6681

	case TB_SETSTYLE:
6682
	    return TOOLBAR_SetStyle (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6683

Alexandre Julliard's avatar
Alexandre Julliard committed
6684
	case TB_SETTOOLTIPS:
6685
	    return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6686

Alexandre Julliard's avatar
Alexandre Julliard committed
6687
	case TB_SETUNICODEFORMAT:
6688
	    return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6689

6690 6691 6692
	case TB_UNKWN45D:
	    return TOOLBAR_Unkwn45D(hwnd, wParam, lParam);

6693
	case TB_SETHOTITEM2:
6694
	    return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6695

6696
	case TB_SETLISTGAP:
6697
	    return TOOLBAR_SetListGap(infoPtr, wParam);
6698

6699
	case TB_GETIMAGELISTCOUNT:
6700
	    return TOOLBAR_GetImageListCount(infoPtr);
Robert Shearman's avatar
Robert Shearman committed
6701

6702
	case TB_GETIDEALSIZE:
6703
	    return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6704

6705 6706
	case TB_UNKWN464:
	    return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6707 6708 6709 6710 6711

/* Common Control Messages */

/*	case TB_GETCOLORSCHEME:			*/ /* identical to CCM_ */
	case CCM_GETCOLORSCHEME:
6712
	    return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6713 6714 6715

/*	case TB_SETCOLORSCHEME:			*/ /* identical to CCM_ */
	case CCM_SETCOLORSCHEME:
6716
	    return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6717 6718

	case CCM_GETVERSION:
6719
	    return TOOLBAR_GetVersion (infoPtr);
6720

6721
	case CCM_SETVERSION:
6722
	    return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6723

Alexandre Julliard's avatar
Alexandre Julliard committed
6724

6725
/*	case WM_CHAR: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6726

Alexandre Julliard's avatar
Alexandre Julliard committed
6727
	case WM_CREATE:
6728
	    return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6729

6730
	case WM_DESTROY:
6731
	  return TOOLBAR_Destroy (infoPtr);
6732

Alexandre Julliard's avatar
Alexandre Julliard committed
6733
	case WM_ERASEBKGND:
6734
	    return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6735

Eric Pouech's avatar
Eric Pouech committed
6736
	case WM_GETFONT:
6737
		return TOOLBAR_GetFont (infoPtr);
Eric Pouech's avatar
Eric Pouech committed
6738

6739
	case WM_KEYDOWN:
6740
	    return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6741

6742
/*	case WM_KILLFOCUS: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6743

Alexandre Julliard's avatar
Alexandre Julliard committed
6744
	case WM_LBUTTONDBLCLK:
6745
	    return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6746 6747

	case WM_LBUTTONDOWN:
6748
	    return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6749 6750

	case WM_LBUTTONUP:
6751
	    return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6752

6753
	case WM_RBUTTONUP:
6754
	    return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6755

6756
	case WM_RBUTTONDBLCLK:
6757
	    return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6758

Alexandre Julliard's avatar
Alexandre Julliard committed
6759
	case WM_MOUSEMOVE:
6760
	    return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6761

6762
	case WM_MOUSELEAVE:
6763
	    return TOOLBAR_MouseLeave (infoPtr);
6764

6765
	case WM_CAPTURECHANGED:
6766
	    return TOOLBAR_CaptureChanged(infoPtr);
6767

Alexandre Julliard's avatar
Alexandre Julliard committed
6768
	case WM_NCACTIVATE:
6769
	    return TOOLBAR_NCActivate (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6770 6771

	case WM_NCCALCSIZE:
6772
	    return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6773

6774
	case WM_NCCREATE:
6775
	    return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6776

Alexandre Julliard's avatar
Alexandre Julliard committed
6777
	case WM_NCPAINT:
6778
	    return TOOLBAR_NCPaint (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6779

Alexandre Julliard's avatar
Alexandre Julliard committed
6780
	case WM_NOTIFY:
6781
	    return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6782

6783
	case WM_NOTIFYFORMAT:
6784
	    return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6785

6786
	case WM_PRINTCLIENT:
Alexandre Julliard's avatar
Alexandre Julliard committed
6787
	case WM_PAINT:
6788
	    return TOOLBAR_Paint (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6789

6790
	case WM_SETFOCUS:
6791
	    return TOOLBAR_SetFocus (infoPtr);
6792

6793
	case WM_SETFONT:
6794
            return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6795

6796
	case WM_SETREDRAW:
6797
	    return TOOLBAR_SetRedraw (infoPtr, wParam);
6798

Alexandre Julliard's avatar
Alexandre Julliard committed
6799
	case WM_SIZE:
6800
	    return TOOLBAR_Size (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6801 6802

	case WM_STYLECHANGED:
6803
	    return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6804

6805
	case WM_SYSCOLORCHANGE:
6806
	    return TOOLBAR_SysColorChange ();
6807 6808 6809
            
        case WM_THEMECHANGED:
            return theme_changed (hwnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
6810

6811
/*	case WM_WININICHANGE: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6812 6813 6814 6815 6816 6817

	case WM_CHARTOITEM:
	case WM_COMMAND:
	case WM_DRAWITEM:
	case WM_MEASUREITEM:
	case WM_VKEYTOITEM:
6818
            return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6819

6820 6821
	/* We see this in Outlook Express 5.x and just does DefWindowProc */
        case PGM_FORWARDMOUSE:
6822
	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6823

Alexandre Julliard's avatar
Alexandre Julliard committed
6824
	default:
6825
	    if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6826
		ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
6827
		     uMsg, wParam, lParam);
6828
	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6829 6830 6831 6832
    }
}


6833
VOID
Patrik Stridvall's avatar
Patrik Stridvall committed
6834
TOOLBAR_Register (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
6835
{
6836
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
6837

6838
    ZeroMemory (&wndClass, sizeof(WNDCLASSW));
Alexandre Julliard's avatar
Alexandre Julliard committed
6839
    wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
6840
    wndClass.lpfnWndProc   = ToolbarWindowProc;
Alexandre Julliard's avatar
Alexandre Julliard committed
6841 6842
    wndClass.cbClsExtra    = 0;
    wndClass.cbWndExtra    = sizeof(TOOLBAR_INFO *);
6843 6844 6845
    wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6846

6847
    RegisterClassW (&wndClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
6848
}
6849 6850 6851


VOID
Patrik Stridvall's avatar
Patrik Stridvall committed
6852
TOOLBAR_Unregister (void)
6853
{
6854
    UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6855
}
6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869

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);

    /* If this is a new entry we must create it and insert into the array */
    if (!c)
    {
        PIMLENTRY *pnies;

6870
        c = Alloc(sizeof(IMLENTRY));
6871 6872
	c->id = id;

6873
	pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6874 6875 6876 6877
	memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
	pnies[*cies] = c;
	(*cies)++;

6878
	Free(*pies);
6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893
	*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++)
6894
	Free((*pies)[i]);
6895

6896
    Free(*pies);
6897 6898 6899 6900 6901 6902

    *cies = 0;
    *pies = NULL;
}


6903
static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924
{
    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;
}


6925
static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936
{
    HIMAGELIST himlDef = 0;
    PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);

    if (pie)
        himlDef = pie->himl;

    return himlDef;
}


6937
static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6938 6939
{
    if (infoPtr->bUnicode)
6940
        return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951
    else
    {
        CHAR Buffer[256];
        NMTOOLBARA nmtba;
        BOOL bRet = FALSE;

        nmtba.iItem = nmtb->iItem;
        nmtba.pszText = Buffer;
        nmtba.cchText = 256;
        ZeroMemory(nmtba.pszText, nmtba.cchText);

6952
        if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
6953 6954 6955
        {
            int ccht = strlen(nmtba.pszText);
            if (ccht)
6956
               MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
6957 6958
                  nmtb->pszText, nmtb->cchText);

6959
            nmtb->tbButton = nmtba.tbButton;
6960 6961 6962 6963 6964 6965 6966 6967
            bRet = TRUE;
        }

        return bRet;
    }
}


6968
static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
6969
{
6970
    NMTOOLBARW nmtb;
6971

6972 6973
    /* MSDN states that iItem is the index of the button, rather than the
     * command ID as used by every other NMTOOLBAR notification */
6974 6975 6976
    nmtb.iItem = iItem;
    memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));

6977
    return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);
6978
}