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

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


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

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

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

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

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

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

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

217 218
#define TOOLBAR_NOWHERE (-1)

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

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

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

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

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

253

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

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

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

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

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

281 282 283
    return lpText;
}

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


static void
308
TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
309 310 311 312
{
    if (TRACE_ON(toolbar)) {
	INT i;

313
	TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
314 315
	      iP->hwndSelf, line,
	      iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
316
	      iP->nNumStrings, iP->dwStyle);
317
	TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
318
	      iP->hwndSelf, line,
319 320
	      iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
	      (iP->bDoRedraw) ? "TRUE" : "FALSE");
321
 	for(i=0; i<iP->nNumButtons; i++) {
322
            TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
323 324 325 326
	}
    }
}

327 328 329 330 331
static inline BOOL
TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
{
    return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
}
332

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
{
    if (!IS_INTRESOURCE( str ) && str != -1)
    {
        if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;

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

        btn->iString  = str;
    }
}

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

358 359 360 361 362 363
static void free_string( TBUTTON_INFO *btn )
{
    set_string_index( btn, 0, TRUE );

}

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


379
static INT
380
TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
381
{
382 383
	if(!IsWindow(infoPtr->hwndSelf))
	    return 0;   /* we have just been destroyed */
384

385 386 387 388
    nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
    nmhdr->hwndFrom = infoPtr->hwndSelf;
    nmhdr->code = code;

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

392
    return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
393 394 395 396 397 398 399 400 401
}

/***********************************************************************
* 		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.
*/
402
static INT
403
TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
404 405 406
{
    INT ret = btnPtr->iBitmap;

407 408 409
    if (ret == I_IMAGECALLBACK)
    {
        /* issue TBN_GETDISPINFO */
410
        NMTBDISPINFOW nmgd;
411 412 413 414 415

        memset(&nmgd, 0, sizeof(nmgd));
        nmgd.idCommand = btnPtr->idCommand;
        nmgd.lParam = btnPtr->dwData;
        nmgd.dwMask = TBNF_IMAGE;
416
        nmgd.iImage = -1;
417 418
        /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
        TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
419 420 421
        if (nmgd.dwMask & TBNF_DI_SETITEM)
            btnPtr->iBitmap = nmgd.iImage;
        ret = nmgd.iImage;
422
        TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
423
            ret, nmgd.dwMask, infoPtr->nNumBitmaps);
424
    }
425 426 427 428

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

429 430 431 432
    return ret;
}


433
static BOOL
434
TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
435
{
436 437 438 439 440 441 442
    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))
443 444 445 446 447
      return TRUE;
    else
      return FALSE;
}

448

449
static inline BOOL
450
TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
451 452 453 454 455 456
{
    HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
    return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
}


457
/***********************************************************************
Robert Shearman's avatar
Robert Shearman committed
458
* 		TOOLBAR_GetImageListForDrawing
459 460
*
* This function validates the bitmap index (including I_IMAGECALLBACK
Robert Shearman's avatar
Robert Shearman committed
461
* functionality) and returns the corresponding image list.
462
*/
Robert Shearman's avatar
Robert Shearman committed
463
static HIMAGELIST
464 465
TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
                                IMAGE_LIST_TYPE imagelist, INT * index)
466
{
467
    HIMAGELIST himl;
468 469

    if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
Robert Shearman's avatar
Robert Shearman committed
470
	if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
471
	WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
Robert Shearman's avatar
Robert Shearman committed
472
	    HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
Robert Shearman's avatar
Robert Shearman committed
473
	return NULL;
474 475
    }

Robert Shearman's avatar
Robert Shearman committed
476 477 478
    if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
	if ((*index == I_IMAGECALLBACK) ||
	    (*index == I_IMAGENONE)) return NULL;
479
	ERR("TBN_GETDISPINFO returned invalid index %d\n",
Robert Shearman's avatar
Robert Shearman committed
480 481
	    *index);
	return NULL;
482
    }
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

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

Robert Shearman's avatar
Robert Shearman committed
503
    return himl;
504 505 506
}


Alexandre Julliard's avatar
Alexandre Julliard committed
507
static void
508
TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
509
{
510 511
    RECT myrect;
    COLORREF oldcolor, newcolor;
Alexandre Julliard's avatar
Alexandre Julliard committed
512

513 514 515 516 517
    myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
    myrect.right = myrect.left + 1;
    myrect.top = lpRect->top + 2;
    myrect.bottom = lpRect->bottom - 2;

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

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

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

    SetBkColor (hdc, oldcolor);
Alexandre Julliard's avatar
Alexandre Julliard committed
532 533
}

534 535

/***********************************************************************
536
* 		TOOLBAR_DrawFlatHorizontalSeparator
537
*
538
* This function draws horizontal separator for toolbars having CCS_VERT style.
539 540 541 542 543 544 545 546
* 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
547
TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
548
                             const TOOLBAR_INFO *infoPtr)
549 550 551 552 553 554 555 556 557 558 559
{
    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);

560
    TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
561

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

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

570
    newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
571
	        comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
572
    SetBkColor (hdc, newcolor);
573
    ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
574 575 576 577 578

    SetBkColor (hdc, oldcolor);
}


579
static void
Robert Shearman's avatar
Robert Shearman committed
580
TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
581 582
{
    INT x, y;
583 584
    HPEN hPen, hOldPen;

Robert Shearman's avatar
Robert Shearman committed
585
    if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
586
    hOldPen = SelectObject ( hdc, hPen );
587
    x = left + 2;
588
    y = top;
589 590 591 592 593
    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);
594
    LineTo (hdc, x+1, y);
595 596
    SelectObject( hdc, hOldPen );
    DeleteObject( hPen );
597 598
}

599 600
/*
 * Draw the text string for this button.
601 602 603
 * 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
604
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
605
static void
606 607
TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
                    const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
Alexandre Julliard's avatar
Alexandre Julliard committed
608
{
Robert Shearman's avatar
Robert Shearman committed
609
    HDC hdc = tbcd->nmcd.hdc;
610 611
    HFONT  hOldFont = 0;
    COLORREF clrOld = 0;
Robert Shearman's avatar
Robert Shearman committed
612
    COLORREF clrOldBk = 0;
613
    int oldBkMode = 0;
614
    UINT state = tbcd->nmcd.uItemState;
Alexandre Julliard's avatar
Alexandre Julliard committed
615 616

    /* draw text */
617
    if (lpText && infoPtr->nMaxTextRows > 0) {
618 619
        TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
              wine_dbgstr_rect(rcText));
620

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

644
	DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
645
	SetTextColor (hdc, clrOld);
646
	if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
647
	{
Robert Shearman's avatar
Robert Shearman committed
648
	    SetBkColor (hdc, clrOldBk);
649 650
	    SetBkMode (hdc, oldBkMode);
	}
651
	SelectObject (hdc, hOldFont);
Alexandre Julliard's avatar
Alexandre Julliard committed
652 653 654 655
    }
}


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


677
static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
Alexandre Julliard's avatar
Alexandre Julliard committed
678
{
679 680 681
    INT cx, cy;
    HBITMAP hbmMask, hbmImage;
    HDC hdcMask, hdcImage;
682

683 684 685 686
    ImageList_GetIconSize(himl, &cx, &cy);

    /* Create src image */
    hdcImage = CreateCompatibleDC(hdc);
687
    hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
688 689 690 691 692 693 694 695 696 697
    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 */
698 699
    ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
                     RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    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
716 717 718
}


719
static UINT
720
TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
721 722 723 724 725 726 727 728
{
    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;
729
    retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
730
    /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
731 732 733
    return retstate;
}

Robert Shearman's avatar
Robert Shearman committed
734 735
/* draws the image on a toolbar button */
static void
736 737
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
738 739 740 741 742
{
    HIMAGELIST himl = NULL;
    BOOL draw_masked = FALSE;
    INT index;
    INT offset = 0;
743
    UINT draw_flags = ILD_TRANSPARENT;
Robert Shearman's avatar
Robert Shearman committed
744

745
    if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
Robert Shearman's avatar
Robert Shearman committed
746 747 748
    {
        himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
        if (!himl)
749 750 751 752
        {
            himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
            draw_masked = TRUE;
        }
Robert Shearman's avatar
Robert Shearman committed
753
    }
754 755 756
    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
757 758 759 760 761 762 763 764 765 766
    {
        /* 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);

767 768 769
    if (!himl)
        return;

770
    if (!(dwItemCDFlag & TBCDRF_NOOFFSET) && 
Robert Shearman's avatar
Robert Shearman committed
771 772 773
        (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
        offset = 1;

774
    if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
Robert Shearman's avatar
Robert Shearman committed
775 776 777 778 779 780 781
        (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)
782 783
        TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
    else
Robert Shearman's avatar
Robert Shearman committed
784 785 786 787 788
        ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
}

/* draws a blank frame for a toolbar button */
static void
789
TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
790 791
{
    HDC hdc = tbcd->nmcd.hdc;
792
    RECT rc = *rect;
Robert Shearman's avatar
Robert Shearman committed
793 794 795 796 797 798 799 800 801
    /* 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 */
802
    if (dwItemCDFlag & TBCDRF_NOEDGES)
Robert Shearman's avatar
Robert Shearman committed
803 804
        return;

805
    if (infoPtr->dwStyle & TBSTYLE_FLAT)
Robert Shearman's avatar
Robert Shearman committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    {
        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
823
TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
Robert Shearman's avatar
Robert Shearman committed
824 825 826
{
    HDC hdc = tbcd->nmcd.hdc;
    int offset = 0;
827 828
    BOOL pressed = bDropDownPressed ||
        (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
Robert Shearman's avatar
Robert Shearman committed
829

830
    if (infoPtr->dwStyle & TBSTYLE_FLAT)
Robert Shearman's avatar
Robert Shearman committed
831
    {
832
        if (pressed)
833
            DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
Robert Shearman's avatar
Robert Shearman committed
834 835 836 837 838 839 840
        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
    {
841
        if (pressed)
842
            DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
Robert Shearman's avatar
Robert Shearman committed
843 844
        else
            DrawEdge (hdc, rcArrow, EDGE_RAISED,
845
              BF_SOFT | BF_RECT | BF_MIDDLE);
Robert Shearman's avatar
Robert Shearman committed
846 847
    }

848
    if (pressed)
849
        offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
Robert Shearman's avatar
Robert Shearman committed
850 851 852 853 854 855 856 857 858

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

Robert Shearman's avatar
Robert Shearman committed
860
/* draws a complete toolbar button */
Alexandre Julliard's avatar
Alexandre Julliard committed
861
static void
862
TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
Alexandre Julliard's avatar
Alexandre Julliard committed
863
{
864
    DWORD dwStyle = infoPtr->dwStyle;
865
    BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
866
    BOOL drawSepDropDownArrow = hasDropDownArrow && 
Robert Shearman's avatar
Robert Shearman committed
867 868
                                (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
    RECT rc, rcArrow, rcBitmap, rcText;
869 870 871 872
    LPWSTR lpText = NULL;
    NMTBCUSTOMDRAW tbcd;
    DWORD ntfret;
    INT offset;
873
    INT oldBkMode;
874
    DWORD dwItemCustDraw;
875
    DWORD dwItemCDFlag;
876
    HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
877 878

    rc = btnPtr->rect;
879
    rcArrow = rc;
880

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    /* 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) */) {
896 897 898 899 900
            if (dwStyle & CCS_VERT) {
                RECT rcsep = rc;
                InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
                TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
            }
901 902 903 904 905 906 907 908 909 910
	    else
		TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
	}
	else if (btnPtr->fsStyle != BTNS_SEP) {
	    FIXME("Draw some kind of separator: fsStyle=%x\n",
		  btnPtr->fsStyle);
	}
	return;
    }

911 912
    /* get a pointer to the text */
    lpText = TOOLBAR_GetText(infoPtr, btnPtr);
913 914 915

    if (hasDropDownArrow)
    {
916 917
        int right;

Robert Shearman's avatar
Robert Shearman committed
918
        if (dwStyle & TBSTYLE_FLAT)
919
            right = max(rc.left, rc.right - DDARROW_WIDTH);
Robert Shearman's avatar
Robert Shearman committed
920
        else
921 922 923 924 925
            right = max(rc.left, rc.right - DDARROW_WIDTH - 2);

        if (drawSepDropDownArrow)
           rc.right = right;

Robert Shearman's avatar
Robert Shearman committed
926
        rcArrow.left = right;
927
    }
928

929
    /* copy text & bitmap rects after adjusting for drop-down arrow
930
     * so that text & bitmap is centered in the rectangle not containing
931
     * the arrow */
932 933
    rcText = rc;
    rcBitmap = rc;
934

935
    /* Center the bitmap horizontally and vertically */
936
    if (dwStyle & TBSTYLE_LIST)
937 938 939 940 941 942 943 944 945
    {
        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;
    }
946
    else
947
        rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
948

949
    rcBitmap.top += infoPtr->szPadding.cy / 2;
950

951
    TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
Robert Shearman's avatar
Robert Shearman committed
952 953
      btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
      infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
954
    TRACE("Text=%s\n", debugstr_w(lpText));
955
    TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
956

957 958 959
    /* calculate text position */
    if (lpText)
    {
960
        InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
961
        if (dwStyle & TBSTYLE_LIST)
Robert Shearman's avatar
Robert Shearman committed
962
        {
963
            rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
Robert Shearman's avatar
Robert Shearman committed
964 965
        }
        else
966 967 968 969 970 971
        {
            if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
                rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
            else
                rcText.top += infoPtr->szPadding.cy/2 + 2;
        }
972 973
    }

Robert Shearman's avatar
Robert Shearman committed
974 975 976
    /* Initialize fields in all cases, because we use these later
     * NOTE: applications can and do alter these to customize their
     * toolbars */
977 978 979 980 981 982 983
    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;
984 985
    tbcd.nStringBkMode = TRANSPARENT;
    tbcd.nHLStringBkMode = OPAQUE;
986 987 988 989
    tbcd.rcText.left = 0;
    tbcd.rcText.top = 0;
    tbcd.rcText.right = rcText.right - rc.left;
    tbcd.rcText.bottom = rcText.bottom - rc.top;
990
    tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
991
    tbcd.nmcd.hdc = hdc;
992
    tbcd.nmcd.rc = btnPtr->rect;
993
    tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
994

Robert Shearman's avatar
Robert Shearman committed
995
    /* FIXME: what are these used for? */
996 997 998
    tbcd.hbrLines = 0;
    tbcd.hpenLines = 0;

999
    /* Issue Item Prepaint notify */
1000
    dwItemCustDraw = 0;
1001 1002
    dwItemCDFlag = 0;
    if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1003 1004 1005 1006
    {
	tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
	tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
	tbcd.nmcd.lItemlParam = btnPtr->dwData;
1007
	ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
Robert Shearman's avatar
Robert Shearman committed
1008 1009
        /* reset these fields so the user can't alter the behaviour like native */
        tbcd.nmcd.hdc = hdc;
1010
        tbcd.nmcd.rc = btnPtr->rect;
Robert Shearman's avatar
Robert Shearman committed
1011

1012
	dwItemCustDraw = ntfret & 0xffff;
1013
	dwItemCDFlag = ntfret & 0xffff0000;
1014
	if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1015 1016 1017 1018 1019 1020
	    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;
    }

1021
    if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1022 1023 1024
        (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
        OffsetRect(&rcText, 1, 1);

Robert Shearman's avatar
Robert Shearman committed
1025 1026 1027 1028
    if (!(tbcd.nmcd.uItemState & CDIS_HOT) && 
        ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
        TOOLBAR_DrawPattern (&rc, &tbcd);

1029 1030
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
        && (tbcd.nmcd.uItemState & CDIS_HOT))
1031
    {
1032
        if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
Robert Shearman's avatar
Robert Shearman committed
1033
        {
1034 1035 1036
            COLORREF oldclr;

            oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1037
            ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1038
            if (hasDropDownArrow)
1039
                ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1040 1041 1042
            SetBkColor(hdc, oldclr);
        }
    }
1043

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    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;
            
1059
        DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1060 1061
    }
    else
1062
        TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1063

Robert Shearman's avatar
Robert Shearman committed
1064
    if (drawSepDropDownArrow)
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    {
        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
1083
            TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1084
    }
1085

1086
    oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
Robert Shearman's avatar
Robert Shearman committed
1087
    if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1088
        TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1089
    SetBkMode (hdc, oldBkMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
1090

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

Robert Shearman's avatar
Robert Shearman committed
1093
    if (hasDropDownArrow && !drawSepDropDownArrow)
1094
    {
Robert Shearman's avatar
Robert Shearman committed
1095 1096 1097 1098 1099 1100 1101
        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))
        {
1102
            offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
Robert Shearman's avatar
Robert Shearman committed
1103 1104 1105 1106
            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);
1107
    }
1108

1109
    if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1110
    {
1111 1112
        tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
        TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1113
    }
1114

Alexandre Julliard's avatar
Alexandre Julliard committed
1115 1116 1117 1118
}


static void
1119
TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
Alexandre Julliard's avatar
Alexandre Julliard committed
1120 1121
{
    TBUTTON_INFO *btnPtr;
1122
    INT i;
1123
    RECT rcTemp, rcClient;
1124 1125
    NMTBCUSTOMDRAW tbcd;
    DWORD ntfret;
1126
    DWORD dwBaseCustDraw;
Alexandre Julliard's avatar
Alexandre Julliard committed
1127

Robert Shearman's avatar
Robert Shearman committed
1128 1129 1130 1131
    /* the app has told us not to redraw the toolbar */
    if (!infoPtr->bDoRedraw)
        return;

1132 1133
    /* if imagelist belongs to the app, it can be changed
       by the app after setting it */
1134 1135 1136 1137 1138 1139
    if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
    {
        infoPtr->nNumBitmaps = 0;
        for (i = 0; i < infoPtr->cimlDef; i++)
            infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
    }
1140 1141 1142

    TOOLBAR_DumpToolbar (infoPtr, __LINE__);

1143 1144 1145
    /* change the imagelist icon size if we manage the list and it is necessary */
    TOOLBAR_CheckImageListIconSize(infoPtr);

1146 1147 1148 1149 1150
    /* Send initial notify */
    ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
    tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
    tbcd.nmcd.hdc = hdc;
    tbcd.nmcd.rc = ps->rcPaint;
1151
    ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1152
    dwBaseCustDraw = ntfret & 0xffff;
1153

1154
    GetClientRect(infoPtr->hwndSelf, &rcClient);
1155

1156
    /* redraw necessary buttons */
Alexandre Julliard's avatar
Alexandre Julliard committed
1157
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1158
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1159
    {
1160
        BOOL bDraw;
1161 1162
        if (!RectVisible(hdc, &btnPtr->rect))
            continue;
1163 1164 1165 1166 1167 1168 1169 1170
        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
1171
        bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1172
        if (bDraw)
1173
            TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1174
    }
1175

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    /* 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);
    }

1190
    if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1191 1192 1193 1194 1195
    {
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
	tbcd.nmcd.hdc = hdc;
	tbcd.nmcd.rc = ps->rcPaint;
1196
	TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1197
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1198 1199
}

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
/***********************************************************************
* 		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
1210
* only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1211
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1212
static void
1213
TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1214
		      HDC hdc, LPSIZE lpSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
1215
{
1216
    RECT myrect;
1217

Alexandre Julliard's avatar
Alexandre Julliard committed
1218 1219
    lpSize->cx = 0;
    lpSize->cy = 0;
1220

1221 1222
    if (infoPtr->nMaxTextRows > 0 &&
        !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1223 1224
        (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
        (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1225
    {
1226
        LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1227

1228 1229 1230 1231 1232
	if(lpText != NULL) {
	    /* first get size of all the text */
	    GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);

	    /* feed above size into the rectangle for DrawText */
1233
            SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1234 1235 1236

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

1240 1241 1242 1243
	    /* feed back to caller  */
	    lpSize->cx = myrect.right;
	    lpSize->cy = myrect.bottom;
	}
1244
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1245

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

1249 1250 1251 1252 1253 1254
/***********************************************************************
* 		TOOLBAR_CalcStrings
*
* This function walks through each string and measures it and returns
* the largest height and width to caller.
*/
1255
static void
1256
TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1257 1258 1259 1260
{
    TBUTTON_INFO *btnPtr;
    INT i;
    SIZE sz;
1261 1262
    HDC hdc;
    HFONT hOldFont;
1263 1264 1265 1266

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

1267
    if (infoPtr->nMaxTextRows == 0)
Huw Davies's avatar
Huw Davies committed
1268 1269
        return;

1270
    hdc = GetDC (infoPtr->hwndSelf);
1271 1272
    hOldFont = SelectObject (hdc, infoPtr->hFont);

1273
    if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1274 1275 1276 1277 1278 1279 1280
    {
        TEXTMETRICW tm;

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

1281 1282
    btnPtr = infoPtr->buttons;
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1283
        if(TOOLBAR_GetText(infoPtr, btnPtr))
1284
        {
1285
            TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1286 1287 1288 1289 1290
            if (sz.cx > lpSize->cx)
                lpSize->cx = sz.cx;
            if (sz.cy > lpSize->cy)
                lpSize->cy = sz.cy;
        }
1291 1292
    }

1293
    SelectObject (hdc, hOldFont);
1294
    ReleaseDC (infoPtr->hwndSelf, hdc);
1295

1296
    TRACE("max string size %d x %d\n", lpSize->cx, lpSize->cy);
1297 1298
}

1299 1300 1301
/***********************************************************************
* 		TOOLBAR_WrapToolbar
*
Francois Gouget's avatar
Francois Gouget committed
1302
* This function walks through the buttons and separators in the
1303 1304 1305 1306 1307
* 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
1308
* flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1309
*
1310
* Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1311 1312
* vertical toolbar lists.
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1313

Alexandre Julliard's avatar
Alexandre Julliard committed
1314
static void
1315
TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
1316
{
1317
    TBUTTON_INFO *btnPtr;
1318
    INT x, cx, i, j, width;
1319
    BOOL bButtonWrap;
1320

1321
    /* 	When the toolbar window style is not TBSTYLE_WRAPABLE,	*/
1322 1323
    /*	no layout is necessary. Applications may use this style */
    /*	to perform their own layout on the toolbar. 		*/
1324
    if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1325
	!(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) )  return;
1326 1327 1328

    btnPtr = infoPtr->buttons;
    x  = infoPtr->nIndent;
1329
    width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1330

1331 1332
    bButtonWrap = FALSE;

1333 1334
    TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
	  infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1335 1336
	  infoPtr->nIndent);

1337 1338 1339
    for (i = 0; i < infoPtr->nNumButtons; i++ )
    {
	btnPtr[i].fsState &= ~TBSTATE_WRAP;
1340

1341 1342 1343
	if (btnPtr[i].fsState & TBSTATE_HIDDEN)
	    continue;

1344 1345
        if (btnPtr[i].cx > 0)
            cx = btnPtr[i].cx;
1346
        /* horizontal separators are treated as buttons for width    */
1347
	else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1348
            !(infoPtr->dwStyle & CCS_VERT))
1349
            cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1350
	else
1351
	    cx = infoPtr->nButtonWidth;
1352

1353 1354 1355
        if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
            cx += DDARROW_WIDTH;

1356
	/* Two or more adjacent separators form a separator group.   */
1357 1358 1359
	/* The first separator in a group should be wrapped to the   */
	/* next row if the previous wrapping is on a button.	     */
	if( bButtonWrap &&
1360
		(btnPtr[i].fsStyle & BTNS_SEP) &&
1361
		(i + 1 < infoPtr->nNumButtons ) &&
1362
		(btnPtr[i + 1].fsStyle & BTNS_SEP) )
1363
	{
1364
	    TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1365 1366 1367 1368 1369 1370 1371 1372
	    btnPtr[i].fsState |= TBSTATE_WRAP;
	    x = infoPtr->nIndent;
	    i++;
	    bButtonWrap = FALSE;
	    continue;
	}

	/* The layout makes sure the bitmap is visible, but not the button. */
1373 1374
	/* Test added to also wrap after a button that starts a row but     */
	/* is bigger than the area.  - GA  8/01                             */
1375 1376
        if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
            ((x == infoPtr->nIndent) && (cx > width)))
1377 1378 1379
	{
	    BOOL bFound = FALSE;

1380
	    /* 	If the current button is a separator and not hidden,  */
1381 1382
	    /*	go to the next until it reaches a non separator.      */
	    /*	Wrap the last separator if it is before a button.     */
1383 1384
	    while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
		      !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1385
		     (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1386 1387 1388 1389 1390
			i < infoPtr->nNumButtons )
	    {
		i++;
		bFound = TRUE;
	    }
1391

1392 1393 1394
	    if( bFound && i < infoPtr->nNumButtons )
	    {
		i--;
1395
		TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1396
		      i, btnPtr[i].fsStyle, x, cx);
1397 1398 1399 1400 1401 1402 1403 1404
		btnPtr[i].fsState |= TBSTATE_WRAP;
		x = infoPtr->nIndent;
		bButtonWrap = FALSE;
		continue;
	    }
	    else if ( i >= infoPtr->nNumButtons)
		break;

1405
	    /* 	If the current button is not a separator, find the last  */
1406 1407 1408
	    /*	separator and wrap it.   				 */
	    for ( j = i - 1; j >= 0  &&  !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
	    {
1409
		if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1410 1411
			!(btnPtr[j].fsState & TBSTATE_HIDDEN))
		{
1412 1413 1414
		    bFound = TRUE;
		    i = j;
		    TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1415
			  i, btnPtr[i].fsStyle, x, cx);
1416 1417
		    x = infoPtr->nIndent;
		    btnPtr[j].fsState |= TBSTATE_WRAP;
1418
		    bButtonWrap = FALSE;
1419 1420 1421 1422 1423 1424 1425 1426
		    break;
		}
	    }

	    /* 	If no separator available for wrapping, wrap one of 	*/
	    /*  non-hidden previous button.  			     	*/
	    if (!bFound)
	    {
1427
		for ( j = i - 1;
1428 1429
			j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
		{
1430
		    if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1431 1432
			continue;

1433 1434 1435
		    bFound = TRUE;
		    i = j;
		    TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1436
			  i, btnPtr[i].fsStyle, x, cx);
1437 1438 1439 1440 1441 1442 1443 1444
		    x = infoPtr->nIndent;
		    btnPtr[j].fsState |= TBSTATE_WRAP;
		    bButtonWrap = TRUE;
		    break;
		}
	    }

	    /* If all above failed, wrap the current button. */
1445
	    if (!bFound)
1446
	    {
1447 1448
		TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
		      i, btnPtr[i].fsStyle, x, cx);
1449 1450
		btnPtr[i].fsState |= TBSTATE_WRAP;
		x = infoPtr->nIndent;
1451
		if (btnPtr[i].fsStyle & BTNS_SEP )
1452 1453 1454
		    bButtonWrap = FALSE;
		else
		    bButtonWrap = TRUE;
1455
	    }
1456
	}
1457
	else {
1458
	    TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1459
		  i, btnPtr[i].fsStyle, x, cx);
1460
	    x += cx;
1461
	}
1462 1463
    }
}
1464 1465


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

1601 1602 1603 1604 1605
    }
    else
    {
        if (bHasBitmap)
        {
1606
            sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1607 1608
            if (sizeString.cy > 0)
                sizeButton.cy += 1 + sizeString.cy;
1609 1610 1611 1612 1613 1614 1615
            sizeButton.cx = infoPtr->szPadding.cx +
                max(sizeString.cx, infoPtr->nBitmapWidth);
        }
        else
        {
            sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
                NONLIST_NOTEXT_OFFSET;
1616 1617
            sizeButton.cx = infoPtr->szPadding.cx +
                max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1618 1619 1620 1621 1622 1623
        }
    }
    return sizeButton;
}


1624 1625 1626
/***********************************************************************
* 		TOOLBAR_CalcToolbar
*
1627 1628 1629
* 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
1630
* on. It assigns a new location to each item and sends this location to
1631 1632 1633
* the tooltip window if appropriate. Finally, it updates the rcBound
* rect and calculates the new required toolbar window height.
*/
1634
static void
1635
TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1636
{
1637 1638
    SIZE  sizeString, sizeButton;
    BOOL validImageList = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1639

1640
    TOOLBAR_CalcStrings (infoPtr, &sizeString);
Alexandre Julliard's avatar
Alexandre Julliard committed
1641

1642 1643
    TOOLBAR_DumpToolbar (infoPtr, __LINE__);

1644 1645
    if (TOOLBAR_IsValidImageList(infoPtr, 0))
        validImageList = TRUE;
1646
    sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1647 1648
    infoPtr->nButtonWidth = sizeButton.cx;
    infoPtr->nButtonHeight = sizeButton.cy;
1649
    infoPtr->iTopMargin = default_top_margin(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1650

1651 1652
    if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
        infoPtr->nButtonWidth = infoPtr->cxMin;
1653
    if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1654 1655
        infoPtr->nButtonWidth = infoPtr->cxMax;

1656
    TOOLBAR_LayoutToolbar(infoPtr);
1657
}
1658

1659
static void
1660
TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1661 1662 1663 1664 1665 1666 1667 1668
{
    TBUTTON_INFO *btnPtr;
    SIZE sizeButton;
    INT i, nRows, nSepRows;
    INT x, y, cx, cy;
    BOOL bWrap;
    BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);

1669
    TOOLBAR_WrapToolbar(infoPtr);
1670

1671
    x  = infoPtr->nIndent;
1672
    y  = infoPtr->iTopMargin;
Alexandre Julliard's avatar
Alexandre Julliard committed
1673
    cx = infoPtr->nButtonWidth;
1674
    cy = infoPtr->nButtonHeight;
1675

1676
    nRows = nSepRows = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1677 1678 1679 1680 1681

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

    btnPtr = infoPtr->buttons;
1684

1685 1686
    TRACE("cy=%d\n", cy);

1687 1688 1689 1690 1691
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
    {
	bWrap = FALSE;
	if (btnPtr->fsState & TBSTATE_HIDDEN)
	{
1692
	    SetRectEmpty (&btnPtr->rect);
1693
	    TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1694
	    continue;
1695 1696
	}

1697
	cy = infoPtr->nButtonHeight;
1698

1699
	if (btnPtr->fsStyle & BTNS_SEP) {
1700
	    if (infoPtr->dwStyle & CCS_VERT) {
1701
                cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1702
                cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1703 1704
	    }
	    else
1705 1706
                cx = (btnPtr->cx > 0) ? btnPtr->cx :
                    (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1707
	}
1708 1709
	else
	{
Robert Shearman's avatar
Robert Shearman committed
1710 1711
            if (btnPtr->cx)
              cx = btnPtr->cx;
1712
            else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1713 1714
            {
              SIZE sz;
1715 1716 1717
	      HDC hdc;
	      HFONT hOldFont;

1718
	      hdc = GetDC (infoPtr->hwndSelf);
1719 1720 1721 1722 1723
	      hOldFont = SelectObject (hdc, infoPtr->hFont);

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

	      SelectObject (hdc, hOldFont);
1724
	      ReleaseDC (infoPtr->hwndSelf, hdc);
1725

1726 1727 1728 1729
              sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
                  TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
                  validImageList);
              cx = sizeButton.cx;
1730 1731 1732
            }
            else
	      cx = infoPtr->nButtonWidth;
1733

Robert Shearman's avatar
Robert Shearman committed
1734 1735
            /* if size has been set manually then don't add on extra space
             * for the drop down arrow */
1736 1737
            if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
              cx += DDARROW_WIDTH;
1738
	}
1739
	if (btnPtr->fsState & TBSTATE_WRAP)
1740 1741
		    bWrap = TRUE;

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

Alexandre Julliard's avatar
Alexandre Julliard committed
1744 1745 1746 1747 1748 1749
	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
1750

1751
        TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1752

1753 1754 1755
	/* btnPtr->nRow is zero based. The space between the rows is 	*/
	/* also considered as a row. 					*/
	btnPtr->nRow = nRows + nSepRows;
1756

1757 1758 1759
	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);
1760

1761 1762
	if( bWrap )
	{
1763
	    if ( !(btnPtr->fsStyle & BTNS_SEP) )
1764
	        y += cy;
1765 1766
	    else
	    {
1767
               if ( !(infoPtr->dwStyle & CCS_VERT))
1768 1769
                    y += cy + ( (btnPtr->cx > 0 ) ?
                                btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1770 1771
		else
		    y += cy;
1772

1773
		/* nSepRows is used to calculate the extra height following  */
1774 1775 1776 1777
		/* the last row.					     */
		nSepRows++;
	    }
	    x = infoPtr->nIndent;
1778 1779 1780 1781

	    /* 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
1782 1783
		nRows++;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1784
	else
Alexandre Julliard's avatar
Alexandre Julliard committed
1785
	    x += cx;
Alexandre Julliard's avatar
Alexandre Julliard committed
1786
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1787

1788 1789 1790
    /* infoPtr->nRows is the number of rows on the toolbar */
    infoPtr->nRows = nRows + nSepRows + 1;

1791
    TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
Alexandre Julliard's avatar
Alexandre Julliard committed
1792 1793 1794
}


1795
static INT
1796
TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
Alexandre Julliard's avatar
Alexandre Julliard committed
1797 1798
{
    TBUTTON_INFO *btnPtr;
1799
    INT i;
1800

1801 1802 1803
    if (button)
        *button = FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1804
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1805
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1806 1807 1808
	if (btnPtr->fsState & TBSTATE_HIDDEN)
	    continue;

1809
	if (btnPtr->fsStyle & BTNS_SEP) {
1810
	    if (PtInRect (&btnPtr->rect, *lpPt)) {
1811
		TRACE(" ON SEPARATOR %d\n", i);
Alexandre Julliard's avatar
Alexandre Julliard committed
1812 1813 1814 1815
		return -i;
	    }
	}
	else {
1816
	    if (PtInRect (&btnPtr->rect, *lpPt)) {
1817
		TRACE(" ON BUTTON %d\n", i);
1818 1819
                if (button)
                    *button = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1820 1821 1822 1823 1824
		return i;
	    }
	}
    }

1825
    TRACE(" NOWHERE\n");
1826
    return TOOLBAR_NOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1827 1828 1829
}


1830 1831
/* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
static BOOL
1832
TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
{
    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];
1851
        INT_PTR str;
1852

1853
        TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1854

1855
        ZeroMemory(btnPtr, sizeof(*btnPtr));
1856

1857
        btnPtr->iBitmap   = lpTbb[iButton].iBitmap;
1858 1859 1860 1861
        btnPtr->idCommand = lpTbb[iButton].idCommand;
        btnPtr->fsState   = lpTbb[iButton].fsState;
        btnPtr->fsStyle   = lpTbb[iButton].fsStyle;
        btnPtr->dwData    = lpTbb[iButton].dwData;
1862

1863
        if (btnPtr->fsStyle & BTNS_SEP)
1864
            str = -1;
1865
        else
1866 1867 1868
            str = lpTbb[iButton].iString;
        set_string_index( btnPtr, str, fUnicode );
        fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1869 1870 1871 1872 1873

        TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
    }

    if (infoPtr->nNumStrings > 0 || fHasString)
1874
        TOOLBAR_CalcToolbar(infoPtr);
1875
    else
1876 1877
        TOOLBAR_LayoutToolbar(infoPtr);
    TOOLBAR_AutoSize(infoPtr);
1878 1879 1880 1881 1882 1883 1884

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


1885
static INT
1886
TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
1887 1888
{
    TBUTTON_INFO *btnPtr;
1889
    INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
1890

1891 1892
    if (CommandIsIndex) {
	TRACE("command is really index command=%d\n", idCommand);
1893
	if (idCommand >= infoPtr->nNumButtons) return -1;
1894 1895
	return idCommand;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1896
    btnPtr = infoPtr->buttons;
Alexandre Julliard's avatar
Alexandre Julliard committed
1897
    for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1898
	if (btnPtr->idCommand == idCommand) {
1899
	    TRACE("command=%d index=%d\n", idCommand, i);
Alexandre Julliard's avatar
Alexandre Julliard committed
1900
	    return i;
Alexandre Julliard's avatar
Alexandre Julliard committed
1901
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1902
    }
1903
    TRACE("no index found for command=%d\n", idCommand);
Alexandre Julliard's avatar
Alexandre Julliard committed
1904 1905 1906 1907
    return -1;
}


1908
static INT
1909
TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
1910 1911
{
    TBUTTON_INFO *btnPtr;
1912
    INT nRunIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
1913 1914 1915 1916 1917 1918

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

    /* check index button */
    btnPtr = &infoPtr->buttons[nIndex];
1919
    if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1920 1921 1922 1923 1924 1925 1926 1927
	if (btnPtr->fsState & TBSTATE_CHECKED)
	    return nIndex;
    }

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

    /* check next buttons */
    nRunIndex = nIndex + 1;
    while (nRunIndex < infoPtr->nNumButtons) {
1940
	btnPtr = &infoPtr->buttons[nRunIndex];
1941
	if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
1942 1943 1944 1945 1946 1947 1948 1949
	    if (btnPtr->fsState & TBSTATE_CHECKED)
		return nRunIndex;
	}
	else
	    break;
	nRunIndex++;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1950 1951 1952 1953
    return -1;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1954
static VOID
1955 1956
TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
		    WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1957
{
1958
    MSG msg;
Alexandre Julliard's avatar
Alexandre Julliard committed
1959 1960 1961 1962 1963 1964

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

1968
    SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
Alexandre Julliard's avatar
Alexandre Julliard committed
1969 1970
}

1971
static void
1972
TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
{
    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
1991
TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
{
    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);
    }
}

2005
static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
{
    /* 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);
    }
}

2021 2022 2023 2024 2025 2026 2027
/* Creates the tooltip control */
static void
TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
{
    int i;
    NMTOOLTIPSCREATED nmttc;

2028
    infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
            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]);
    }
}

2046 2047 2048 2049 2050 2051 2052 2053
/* 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
2054
    TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2055

2056
    count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2057 2058 2059 2060

    /* position 0 is always separator */
    for (i = 1; i < count; i++)
    {
2061
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2062 2063
        if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
        {
2064 2065
            i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
            SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2066 2067 2068 2069
            return;
        }
    }
    /* id higher than all others add to end */
2070 2071
    i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
    SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2072 2073
}

2074
static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2075
{
2076
    NMTOOLBARW nmtb;
2077 2078 2079 2080 2081 2082

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

    if (nIndexFrom == nIndexTo)
        return;

2083 2084 2085
    /* 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;
2086
    if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2087 2088 2089 2090
    {
        PCUSTOMBUTTON btnInfo;
        NMHDR hdr;
        HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2091
        int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2092

2093
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2094

2095 2096 2097 2098
        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);
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110

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

2111
        SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
Robert Shearman's avatar
Robert Shearman committed
2112
        SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2113 2114 2115 2116 2117

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

2118
static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2119
{
2120
    NMTOOLBARW nmtb;
2121 2122 2123

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

2124 2125 2126
    /* 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;
2127
    if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2128 2129 2130 2131 2132
    {
        PCUSTOMBUTTON btnInfo;
        NMHDR hdr;
        HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
        HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2133
        int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2134

2135
        btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2136 2137 2138 2139

        if (nIndexAvail != 0) /* index == 0 indicates separator */
        {
            /* remove from 'available buttons' list */
2140
            SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2141
            if (nIndexAvail == count-1)
2142
                SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2143
            else
2144
                SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2145 2146 2147 2148 2149 2150
        }
        else
        {
            PCUSTOMBUTTON btnNew;

            /* duplicate 'separator' button */
2151
            btnNew = Alloc(sizeof(CUSTOMBUTTON));
2152
            *btnNew = *btnInfo;
2153 2154 2155 2156
            btnInfo = btnNew;
        }

        /* insert into 'toolbar button' list */
2157 2158
        SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
        SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2159

2160
        SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2161 2162 2163 2164 2165

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

2166
static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2167 2168 2169 2170 2171 2172
{
    PCUSTOMBUTTON btnInfo;
    HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);

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

2173
    btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2174 2175 2176 2177 2178 2179

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

2180 2181
        SendMessageW(hwndList, LB_DELETESTRING, index, 0);
        SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2182

2183
        SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195

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

Robert Shearman's avatar
Robert Shearman committed
2267
    return 0;
2268 2269 2270
}

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

2327
extern UINT uDragListMessage DECLSPEC_HIDDEN;
2328 2329 2330 2331 2332

/***********************************************************************
 * TOOLBAR_CustomizeDialogProc
 * This function implements the toolbar customization dialog.
 */
2333
static INT_PTR CALLBACK
2334 2335
TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
2336
    PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2337 2338
    PCUSTOMBUTTON btnInfo;
    NMTOOLBARA nmtb;
2339
    TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2340 2341 2342 2343

    switch (uMsg)
    {
	case WM_INITDIALOG:
2344
	    custInfo = (PCUSTDLG_INFO)lParam;
2345
	    SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2346

2347
	    if (custInfo)
2348
	    {
2349
		WCHAR Buffer[256];
2350 2351
		int i = 0;
		int index;
2352
		NMTBINITCUSTOMIZE nmtbic;
2353

2354
		infoPtr = custInfo->tbInfo;
2355 2356 2357 2358

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

2359
		if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2360 2361
		    return FALSE;

2362
		nmtbic.hwndDialog = hwnd;
2363
		/* Send TBN_INITCUSTOMIZE notification */
2364
		if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2365
		    TBNRF_HIDEHELP)
2366
                {
2367 2368 2369
                    TRACE("TBNRF_HIDEHELP requested\n");
                    ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
                }
2370

2371 2372
		/* add items to 'toolbar buttons' list and check if removable */
		for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2373
                {
2374
                    btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2375
                    memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2376
                    btnInfo->btn.fsStyle = BTNS_SEP;
2377 2378
                    btnInfo->bVirtual = FALSE;
		    LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2379 2380

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

Robert Shearman's avatar
Robert Shearman committed
2383 2384
		    index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
		    SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2385
		}
2386

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

2389
		/* insert separator button into 'available buttons' list */
2390
                btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2391
		memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2392
		btnInfo->btn.fsStyle = BTNS_SEP;
2393 2394
		btnInfo->bVirtual = FALSE;
		btnInfo->bRemovable = TRUE;
2395
		LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
Robert Shearman's avatar
Robert Shearman committed
2396 2397
		index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2398 2399 2400 2401 2402

		/* insert all buttons into dsa */
		for (i = 0;; i++)
		{
		    /* send TBN_GETBUTTONINFO notification */
2403
                    NMTOOLBARW nmtb;
2404 2405 2406 2407
		    nmtb.iItem = i;
		    nmtb.pszText = Buffer;
		    nmtb.cchText = 256;

2408 2409 2410 2411
                    /* Clear previous button's text */
                    ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));

                    if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2412 2413
			break;

2414
		    TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2415 2416 2417 2418 2419
                        nmtb.tbButton.fsStyle, i, 
                        nmtb.tbButton.idCommand,
                        nmtb.tbButton.iString,
                        nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
                        : "");
2420

2421
		    /* insert button into the appropriate list */
2422
		    index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2423
		    if (index == -1)
2424
		    {
2425
                        btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2426 2427
			btnInfo->bVirtual = FALSE;
			btnInfo->bRemovable = TRUE;
2428 2429 2430
		    }
		    else
		    {
Robert Shearman's avatar
Robert Shearman committed
2431
                        btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, 
2432 2433 2434
                            IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
                    }

2435
                    btnInfo->btn = nmtb.tbButton;
2436
                    if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2437 2438 2439 2440 2441 2442 2443 2444 2445
                    {
                        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]);
                        }
2446
		    }
2447 2448 2449

		    if (index == -1)
			TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2450 2451
		}

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

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

2457
		/* append 'virtual' separator button to the 'toolbar buttons' list */
2458
                btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2459
		memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2460
		btnInfo->btn.fsStyle = BTNS_SEP;
2461 2462
		btnInfo->bVirtual = TRUE;
		btnInfo->bRemovable = FALSE;
2463
		LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
Robert Shearman's avatar
Robert Shearman committed
2464 2465
		index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2466 2467

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

2471 2472
		MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
		MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2473

2474
		/* set focus and disable buttons */
2475
		PostMessageW (hwnd, WM_USER, 0, 0);
2476 2477 2478
	    }
	    return TRUE;

2479 2480 2481 2482 2483 2484 2485
	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;

2486 2487 2488 2489 2490 2491 2492
	case WM_CLOSE:
	    EndDialog(hwnd, FALSE);
	    return TRUE;

	case WM_COMMAND:
	    switch (LOWORD(wParam))
	    {
2493 2494 2495 2496 2497 2498 2499 2500
		case IDC_TOOLBARBTN_LBOX:
		    if (HIWORD(wParam) == LBN_SELCHANGE)
		    {
			PCUSTOMBUTTON btnInfo;
			NMTOOLBARA nmtb;
			int count;
			int index;

Robert Shearman's avatar
Robert Shearman committed
2501 2502
			count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
			index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2503 2504 2505

			/* send TBN_QUERYINSERT notification */
			nmtb.iItem = index;
2506
		        TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2507 2508

			/* get list box item */
Robert Shearman's avatar
Robert Shearman committed
2509
			btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540

			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
2541
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2542
			TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2543 2544 2545 2546 2547
		    }
		    break;

		case IDC_MOVEDN_BTN: /* move down */
		    {
Robert Shearman's avatar
Robert Shearman committed
2548
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2549
			TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2550 2551 2552 2553 2554
		    }
		    break;

		case IDC_REMOVE_BTN: /* remove button */
		    {
Robert Shearman's avatar
Robert Shearman committed
2555
			int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2556 2557 2558 2559

			if (LB_ERR == index)
				break;

2560
			TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2561 2562
		    }
		    break;
2563 2564 2565 2566 2567 2568
		case IDC_HELP_BTN:
			TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
			break;
		case IDC_RESET_BTN:
			TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
			break;
2569 2570 2571 2572

		case IDOK: /* Add button */
		    {
			int index;
2573
			int indexto;
2574

Robert Shearman's avatar
Robert Shearman committed
2575 2576
			index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
			indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2577

2578
			TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2579 2580 2581
		    }
		    break;

2582 2583 2584 2585 2586 2587 2588
		case IDCANCEL:
		    EndDialog(hwnd, FALSE);
		    break;
	    }
	    return TRUE;

	case WM_DESTROY:
2589 2590 2591 2592 2593
	    {
		int count;
		int i;

		/* delete items from 'toolbar buttons' listbox*/
Robert Shearman's avatar
Robert Shearman committed
2594
		count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2595 2596
		for (i = 0; i < count; i++)
		{
Robert Shearman's avatar
Robert Shearman committed
2597
		    btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2598
		    Free(btnInfo);
Robert Shearman's avatar
Robert Shearman committed
2599
		    SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2600
		}
Robert Shearman's avatar
Robert Shearman committed
2601
		SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2602 2603 2604


		/* delete items from 'available buttons' listbox*/
Robert Shearman's avatar
Robert Shearman committed
2605
		count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2606 2607
		for (i = 0; i < count; i++)
		{
Robert Shearman's avatar
Robert Shearman committed
2608
		    btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2609
		    Free(btnInfo);
Robert Shearman's avatar
Robert Shearman committed
2610
		    SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2611
		}
Robert Shearman's avatar
Robert Shearman committed
2612
		SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2613
            }
2614 2615 2616 2617 2618 2619 2620 2621
	    return TRUE;

	case WM_DRAWITEM:
	    if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
	    {
		LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
		RECT rcButton;
		RECT rcText;
2622
		HPEN hPen, hOldPen;
2623 2624 2625 2626
		HBRUSH hOldBrush;
		COLORREF oldText = 0;
		COLORREF oldBk = 0;

2627
		/* get item data */
2628
               btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2629
		if (btnInfo == NULL)
2630
		{
2631
		    FIXME("btnInfo invalid\n");
2632
		    return TRUE;
2633 2634
		}

2635
		/* set colors and select objects */
2636
		oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2637
		if (btnInfo->bVirtual)
2638
		   oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2639
		else
2640
		   oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2641
                hPen = CreatePen( PS_SOLID, 1,
2642
                                 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2643
		hOldPen = SelectObject (lpdis->hDC, hPen );
2644 2645 2646 2647 2648 2649 2650
		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 */
2651
                rcButton = lpdis->rcItem;
2652
		InflateRect (&rcButton, -1, -1);
2653
                rcText = rcButton;
2654
		rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2655 2656 2657 2658 2659 2660 2661
		rcText.left = rcButton.right + 2;

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

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

2665
		/* draw image and text */
2666
		if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2667 2668 2669 2670 2671 2672
			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,
2673 2674
			       DT_LEFT | DT_VCENTER | DT_SINGLELINE);

2675
		/* delete objects and reset colors */
2676 2677
		SelectObject (lpdis->hDC, hOldBrush);
		SelectObject (lpdis->hDC, hOldPen);
2678 2679
		SetBkColor (lpdis->hDC, oldBk);
		SetTextColor (lpdis->hDC, oldText);
2680
                DeleteObject( hPen );
2681 2682 2683 2684 2685 2686 2687 2688 2689
		return TRUE;
	    }
	    return FALSE;

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

2690
		lpmis->itemHeight = 15 + 8; /* default height */
2691 2692 2693 2694 2695 2696

		return TRUE;
	    }
	    return FALSE;

	default:
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
            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;
2715 2716 2717
    }
}

2718 2719 2720 2721 2722 2723
static BOOL
TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
{
    HBITMAP hbmLoad;
    INT nCountBefore = ImageList_GetImageCount(himlDef);
    INT nCountAfter;
2724
    INT cxIcon, cyIcon;
2725 2726 2727 2728 2729 2730
    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 */
2731
        hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2732 2733 2734
    else if (bitmap->hInst == COMCTL32_hModule)
        hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
                              IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2735 2736
    else
        hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2737 2738 2739

    /* enlarge the bitmap if needed */
    ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2740 2741
    if (bitmap->hInst != COMCTL32_hModule)
        COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
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 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
    
    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,
2782
                                ILC_COLOR32|ILC_MASK, 8, 2);
2783 2784 2785 2786 2787 2788 2789 2790
    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);
}
2791

2792 2793 2794 2795
/***********************************************************************
 * TOOLBAR_AddBitmap:  Add the bitmaps to the default image list.
 *
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
2796
static LRESULT
2797
TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
2798
{
2799
    TBITMAP_INFO info;
2800
    INT iSumButtons, i;
2801
    HIMAGELIST himlDef;
Alexandre Julliard's avatar
Alexandre Julliard committed
2802

2803
    TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2804
    if (!lpAddBmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
2805 2806
	return -1;

2807 2808
    if (lpAddBmp->hInst == HINST_COMMCTRL)
    {
2809 2810 2811 2812
        info.hInst = COMCTL32_hModule;
        switch (lpAddBmp->nID)
        {
            case IDB_STD_SMALL_COLOR:
2813
            case 2:
2814 2815 2816 2817
	        info.nButtons = 15;
	        info.nID = IDB_STD_SMALL;
	        break;
            case IDB_STD_LARGE_COLOR:
2818
            case 3:
2819 2820 2821 2822
	        info.nButtons = 15;
	        info.nID = IDB_STD_LARGE;
	        break;
            case IDB_VIEW_SMALL_COLOR:
2823
            case 6:
2824 2825 2826 2827
	        info.nButtons = 12;
	        info.nID = IDB_VIEW_SMALL;
	        break;
            case IDB_VIEW_LARGE_COLOR:
2828
            case 7:
2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
	        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:
2841
                WARN("unknown bitmap id, %ld\n", lpAddBmp->nID);
2842 2843
	        return -1;
	}
2844

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

2847
	/* Windows resize all the buttons to the size of a newly added standard image */
2848
	if (lpAddBmp->nID & 1)
2849
	{
2850
	    /* large icons: 24x24. Will make the button 31x30 */
2851
	    SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2852 2853
	}
	else
2854
	{
2855
	    /* small icons: 16x16. Will make the buttons 23x22 */
2856
	    SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2857
	}
2858

2859
	TOOLBAR_CalcToolbar (infoPtr);
2860 2861 2862
    }
    else
    {
2863
	info.nButtons = count;
2864 2865
	info.hInst = lpAddBmp->hInst;
	info.nID = lpAddBmp->nID;
2866
	TRACE("adding %d bitmaps\n", info.nButtons);
2867
    }
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
    
    /* 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;
    }
2878

2879
    if (!infoPtr->cimlDef) {
2880
	/* create new default image list */
2881
        TRACE ("creating default image list\n");
2882

2883
        himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2884
                                    ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2885
	TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2886
        infoPtr->himlInt = himlDef;
2887 2888 2889 2890 2891 2892 2893 2894
    }
    else {
        himlDef = GETDEFIMAGELIST(infoPtr, 0);
    }

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

2897 2898
    if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
        return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2899

2900
    TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2901
    infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2902
    infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2903 2904 2905
    infoPtr->nNumBitmapInfos++;
    TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);

2906
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2907
    return iSumButtons;
Alexandre Julliard's avatar
Alexandre Julliard committed
2908 2909 2910 2911
}


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

2916
    return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2917
}
Alexandre Julliard's avatar
Alexandre Julliard committed
2918 2919 2920


static LRESULT
2921
TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
2922
{
2923
#define MAX_RESOURCE_STRING_LENGTH 512
2924
    BOOL fFirstString = (infoPtr->nNumStrings == 0);
2925
    INT nIndex = infoPtr->nNumStrings;
Alexandre Julliard's avatar
Alexandre Julliard committed
2926

2927 2928 2929
    TRACE("%p, %lx\n", hInstance, lParam);

    if (IS_INTRESOURCE(lParam)) {
2930 2931 2932
	WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
	WCHAR delimiter;
	WCHAR *next_delim;
2933
        HRSRC hrsrc;
2934
	WCHAR *p;
2935
	INT len;
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947

	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
2948

2949
        len = LoadStringW (hInstance, (UINT)lParam,
2950
                             szString, MAX_RESOURCE_STRING_LENGTH);
Alexandre Julliard's avatar
Alexandre Julliard committed
2951

2952 2953 2954
        TRACE("len=%d %s\n", len, debugstr_w(szString));
        if (len == 0 || len == 1)
            return nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
2955

2956
        TRACE("delimiter: 0x%x\n", *szString);
2957 2958 2959 2960 2961
        delimiter = *szString;
        p = szString + 1;

        while ((next_delim = strchrW(p, delimiter)) != NULL) {
            *next_delim = 0;
2962 2963 2964 2965 2966 2967
            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;
            }
2968 2969 2970 2971 2972 2973 2974

            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
2975 2976
    }
    else {
2977
	LPWSTR p = (LPWSTR)lParam;
2978
	INT len;
Alexandre Julliard's avatar
Alexandre Julliard committed
2979

2980 2981
	if (p == NULL)
	    return -1;
2982
	TRACE("adding string(s) from array\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
2983
	while (*p) {
2984
            len = strlenW (p);
Alexandre Julliard's avatar
Alexandre Julliard committed
2985

2986 2987 2988
            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
2989 2990 2991 2992 2993 2994
	    infoPtr->nNumStrings++;

	    p += (len+1);
	}
    }

2995
    if (fFirstString)
2996
        TOOLBAR_CalcToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
2997 2998 2999 3000
    return nIndex;
}


3001
static LRESULT
3002
TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3003
{
3004
    BOOL fFirstString = (infoPtr->nNumStrings == 0);
3005
    LPSTR p;
3006
    INT nIndex;
3007
    INT len;
3008

3009 3010 3011
    TRACE("%p, %lx\n", hInstance, lParam);

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

3014 3015 3016
    p = (LPSTR)lParam;
    if (p == NULL)
        return -1;
3017

3018
    TRACE("adding string(s) from array\n");
3019 3020 3021 3022
    nIndex = infoPtr->nNumStrings;
    while (*p) {
        len = strlen (p);
        TRACE("len=%d \"%s\"\n", len, p);
3023

3024 3025 3026
        infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
        Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
        infoPtr->nNumStrings++;
3027

3028
        p += (len+1);
3029 3030
    }

3031
    if (fFirstString)
3032
        TOOLBAR_CalcToolbar(infoPtr);
3033 3034
    return nIndex;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3035 3036 3037


static LRESULT
3038
TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3039
{
3040
    TRACE("auto sizing, style=%#x\n", infoPtr->dwStyle);
3041
    TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
Alexandre Julliard's avatar
Alexandre Julliard committed
3042

3043 3044 3045 3046 3047 3048
    if (!(infoPtr->dwStyle & CCS_NORESIZE))
    {
        RECT window_rect, parent_rect;
        UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
        HWND parent;
        INT  x, y, cx, cy;
Alexandre Julliard's avatar
Alexandre Julliard committed
3049

3050
        parent = GetParent (infoPtr->hwndSelf);
3051

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

3055
        GetClientRect(parent, &parent_rect);
3056

3057 3058
        x = parent_rect.left;
        y = parent_rect.top;
3059

3060 3061
        cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
        cx = parent_rect.right - parent_rect.left;
3062 3063 3064

        if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
        {
3065
            GetWindowRect(infoPtr->hwndSelf, &window_rect);
3066
            MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3067 3068 3069 3070
            y = window_rect.top;
        }
        if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
        {
3071
            GetWindowRect(infoPtr->hwndSelf, &window_rect);
3072 3073
            y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3074

3075 3076 3077 3078 3079
        if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
            uPosFlags |= SWP_NOMOVE;
    
        if (!(infoPtr->dwStyle & CCS_NODIVIDER))
            cy += GetSystemMetrics(SM_CYEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3080

3081 3082
        if (infoPtr->dwStyle & WS_BORDER)
        {
3083 3084
            cx += 2 * GetSystemMetrics(SM_CXBORDER);
            cy += 2 * GetSystemMetrics(SM_CYBORDER);
3085
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3086

3087
        SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3088 3089
    }

3090 3091 3092 3093 3094 3095
    if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
    {
        TOOLBAR_LayoutToolbar(infoPtr);
        InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
3096 3097
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3098 3099


3100
static inline LRESULT
3101
TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3102 3103 3104 3105 3106
{
    return infoPtr->nNumButtons;
}


3107 3108
static inline LRESULT
TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
Alexandre Julliard's avatar
Alexandre Julliard committed
3109
{
3110
    infoPtr->dwStructSize = Size;
Alexandre Julliard's avatar
Alexandre Julliard committed
3111 3112 3113 3114 3115 3116

    return 0;
}


static LRESULT
3117
TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
Alexandre Julliard's avatar
Alexandre Julliard committed
3118 3119
{
    TBUTTON_INFO *btnPtr;
3120
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3121

3122
    TRACE("button %d, iBitmap now %d\n", Id, Index);
3123

3124
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3125 3126 3127 3128
    if (nIndex == -1)
	return FALSE;

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

3131 3132
    /* we HAVE to erase the background, the new bitmap could be */
    /* transparent */
3133
    InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3134 3135 3136 3137 3138 3139

    return TRUE;
}


static LRESULT
3140
TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3141 3142
{
    TBUTTON_INFO *btnPtr;
3143 3144
    INT nIndex;
    INT nOldIndex = -1;
3145
    BOOL bChecked = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3146

3147
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3148

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

Alexandre Julliard's avatar
Alexandre Julliard committed
3151 3152 3153 3154
    if (nIndex == -1)
	return FALSE;

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

3156
    bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3157

3158
    if (!LOWORD(lParam))
Alexandre Julliard's avatar
Alexandre Julliard committed
3159
	btnPtr->fsState &= ~TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3160
    else {
3161
	if (btnPtr->fsStyle & BTNS_GROUP) {
3162
	    nOldIndex =
Alexandre Julliard's avatar
Alexandre Julliard committed
3163 3164 3165 3166 3167 3168
		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
3169
	btnPtr->fsState |= TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3170
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3171

3172 3173
    if( bChecked != LOWORD(lParam) )
    {
Robert Shearman's avatar
Robert Shearman committed
3174
        if (nOldIndex != -1)
3175 3176
            InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3177
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3178

Alexandre Julliard's avatar
Alexandre Julliard committed
3179
    /* FIXME: Send a WM_NOTIFY?? */
Alexandre Julliard's avatar
Alexandre Julliard committed
3180

Alexandre Julliard's avatar
Alexandre Julliard committed
3181 3182 3183 3184 3185
    return TRUE;
}


static LRESULT
3186
TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3187
{
3188
    return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3189 3190 3191
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3192
static LRESULT
3193
TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3194
{
3195
    CUSTDLG_INFO custInfo;
3196 3197
    LRESULT ret;
    NMHDR nmhdr;
Alexandre Julliard's avatar
Alexandre Julliard committed
3198

3199
    custInfo.tbInfo = infoPtr;
3200
    custInfo.tbHwnd = infoPtr->hwndSelf;
3201

3202
    /* send TBN_BEGINADJUST notification */
3203
    TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3204

3205 3206
    ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
                           infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3207 3208

    /* send TBN_ENDADJUST notification */
3209
    TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3210 3211

    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3212
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3213 3214 3215


static LRESULT
3216
TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
Alexandre Julliard's avatar
Alexandre Julliard committed
3217
{
Robert Shearman's avatar
Robert Shearman committed
3218
    NMTOOLBARW nmtb;
3219
    TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
Alexandre Julliard's avatar
Alexandre Julliard committed
3220

Alexandre Julliard's avatar
Alexandre Julliard committed
3221
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3222
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3223

Robert Shearman's avatar
Robert Shearman committed
3224
    memset(&nmtb, 0, sizeof(nmtb));
3225 3226 3227 3228 3229 3230 3231
    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;
3232
    TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
Robert Shearman's avatar
Robert Shearman committed
3233

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

3236
    infoPtr->nHotItem = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3237
    if (infoPtr->nNumButtons == 1) {
3238
	TRACE(" simple delete\n");
3239
        free_string( infoPtr->buttons );
3240
	Free (infoPtr->buttons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3241 3242 3243 3244
	infoPtr->buttons = NULL;
	infoPtr->nNumButtons = 0;
    }
    else {
Alexandre Julliard's avatar
Alexandre Julliard committed
3245
	TBUTTON_INFO *oldButtons = infoPtr->buttons;
3246
        TRACE("complex delete [nIndex=%d]\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3247

Alexandre Julliard's avatar
Alexandre Julliard committed
3248
	infoPtr->nNumButtons--;
3249
	infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3250 3251 3252 3253
        if (nIndex > 0) {
            memcpy (&infoPtr->buttons[0], &oldButtons[0],
                    nIndex * sizeof(TBUTTON_INFO));
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3254

Alexandre Julliard's avatar
Alexandre Julliard committed
3255 3256 3257 3258 3259
        if (nIndex < infoPtr->nNumButtons) {
            memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
                    (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
        }

3260
        free_string( oldButtons + nIndex );
3261
	Free (oldButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
3262 3263
    }

3264
    TOOLBAR_LayoutToolbar(infoPtr);
3265

3266
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3267 3268 3269 3270 3271 3272

    return TRUE;
}


static LRESULT
3273
TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3274 3275
{
    TBUTTON_INFO *btnPtr;
3276
    INT nIndex;
3277
    DWORD bState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3278

3279
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3280

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

Alexandre Julliard's avatar
Alexandre Julliard committed
3283 3284 3285 3286
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3287 3288 3289 3290

    bState = btnPtr->fsState & TBSTATE_ENABLED;

    /* update the toolbar button state */
3291
    if(!LOWORD(lParam)) {
3292 3293
 	btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
    } else {
Alexandre Julliard's avatar
Alexandre Julliard committed
3294
	btnPtr->fsState |= TBSTATE_ENABLED;
3295
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3296

3297
    /* redraw the button only if the state of the button changed */
3298
    if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3299
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3300 3301 3302 3303 3304

    return TRUE;
}


3305
static inline LRESULT
3306
TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3307 3308 3309
{
    return infoPtr->bAnchor;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3310 3311 3312


static LRESULT
3313
TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3314
{
3315
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3316

3317
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3318
    if (nIndex == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3319
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3320 3321 3322 3323 3324

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


Patrik Stridvall's avatar
Patrik Stridvall committed
3325
static inline LRESULT
3326
TOOLBAR_GetBitmapFlags (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
3327
{
3328
    return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3329 3330 3331 3332
}


static LRESULT
3333
TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
Alexandre Julliard's avatar
Alexandre Julliard committed
3334 3335 3336
{
    TBUTTON_INFO *btnPtr;

3337 3338
    if (lpTbb == NULL)
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3339 3340 3341 3342 3343 3344 3345 3346 3347

    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;
3348 3349
    lpTbb->bReserved[0] = 0;
    lpTbb->bReserved[1] = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3350 3351 3352 3353 3354 3355 3356
    lpTbb->dwData    = btnPtr->dwData;
    lpTbb->iString   = btnPtr->iString;

    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3357
static LRESULT
3358
TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
3359
{
3360
    /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
Alexandre Julliard's avatar
Alexandre Julliard committed
3361
    TBUTTON_INFO *btnPtr;
3362
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3363

3364 3365
    if (lpTbInfo == NULL)
	return -1;
3366

3367
    /* MSDN documents an iImageLabel field added in Vista but it is not present in
3368 3369 3370 3371 3372 3373
     * 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");
3374
	return -1;
3375
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3376

3377
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
Alexandre Julliard's avatar
Alexandre Julliard committed
3378 3379 3380
    if (nIndex == -1)
	return -1;

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


3414
static inline LRESULT
3415
TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3416
{
3417 3418
    return MAKELONG((WORD)infoPtr->nButtonWidth,
                    (WORD)infoPtr->nButtonHeight);
Alexandre Julliard's avatar
Alexandre Julliard committed
3419
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3420 3421 3422


static LRESULT
3423
TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3424
{
3425 3426
    INT nIndex;
    LPWSTR lpText;
3427
    LRESULT ret = 0;
3428

3429
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3430
    if (nIndex == -1)
3431 3432
	return -1;

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

3435
    if (isW)
3436
    {
3437 3438 3439 3440 3441
        if (lpText)
        {
            ret = strlenW (lpText);
            if (lpStr) strcpyW (lpStr, lpText);
        }
3442
    }
3443 3444 3445
    else
        ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
                                  (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3446
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3447 3448 3449
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3450
static LRESULT
3451
TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3452
{
3453
    TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3454
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3455
    return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3456 3457 3458
}


3459
static inline LRESULT
3460
TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3461
{
3462 3463
    TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
3464 3465
    return infoPtr->dwExStyle;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3466 3467 3468


static LRESULT
3469
TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
3470
{
3471
    TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
Robert Shearman's avatar
Robert Shearman committed
3472
    /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3473
    return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
3474 3475 3476
}


3477
static LRESULT
3478
TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3479
{
3480
    if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3481 3482 3483 3484 3485 3486 3487
	return -1;

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

    return (LRESULT)infoPtr->nHotItem;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3488 3489 3490


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


3499
static LRESULT
3500
TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3501
{
3502
    TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3503 3504 3505 3506 3507 3508 3509

    *lptbim = infoPtr->tbim;

    return 0;
}


3510
static inline LRESULT
3511
TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3512
{
3513
    TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3514 3515 3516

    return (LRESULT)infoPtr->clrInsertMark;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3517 3518 3519


static LRESULT
3520
TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
3521 3522 3523 3524 3525 3526
{
    TBUTTON_INFO *btnPtr;

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

3528 3529 3530 3531
    if (lpRect == NULL)
	return FALSE;
    if (btnPtr->fsState & TBSTATE_HIDDEN)
	return FALSE;
3532

Alexandre Julliard's avatar
Alexandre Julliard committed
3533 3534 3535 3536 3537 3538 3539 3540 3541
    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
3542
static LRESULT
3543
TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
3544
{
Alexandre Julliard's avatar
Alexandre Julliard committed
3545 3546 3547
    if (lpSize == NULL)
	return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
3548 3549
    lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
    lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
Alexandre Julliard's avatar
Alexandre Julliard committed
3550

3551
    TRACE("maximum size %d x %d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
3552 3553
	   infoPtr->rcBound.right - infoPtr->rcBound.left,
	   infoPtr->rcBound.bottom - infoPtr->rcBound.top);
Alexandre Julliard's avatar
Alexandre Julliard committed
3554 3555 3556 3557 3558

    return TRUE;
}


3559
/* << TOOLBAR_GetObject >> */
3560 3561


3562
static inline LRESULT
3563
TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3564
{
3565
    return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3566
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3567 3568 3569


static LRESULT
3570
TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
Alexandre Julliard's avatar
Alexandre Julliard committed
3571 3572
{
    TBUTTON_INFO *btnPtr;
3573
    INT        nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3574

3575
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3576 3577 3578
    btnPtr = &infoPtr->buttons[nIndex];
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;
3579

3580 3581
    if (lpRect == NULL)
	return FALSE;
3582

Alexandre Julliard's avatar
Alexandre Julliard committed
3583 3584 3585 3586 3587 3588 3589
    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
3590 3591


3592
static inline LRESULT
3593
TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3594
{
3595
    return infoPtr->nRows;
Alexandre Julliard's avatar
Alexandre Julliard committed
3596 3597 3598 3599
}


static LRESULT
3600
TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3601
{
3602
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3603

3604
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3605 3606
    if (nIndex == -1)
	return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3607 3608 3609 3610 3611

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


3612
static inline LRESULT
3613
TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3614
{
3615
    return infoPtr->dwStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
3616 3617 3618
}


3619
static inline LRESULT
3620
TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3621 3622 3623
{
    return infoPtr->nMaxTextRows;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3624 3625 3626


static LRESULT
3627
TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3628
{
3629 3630
    if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
        TOOLBAR_TooltipCreateControl(infoPtr);
3631
    return (LRESULT)infoPtr->hwndToolTip;
Alexandre Julliard's avatar
Alexandre Julliard committed
3632 3633 3634
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3635
static LRESULT
3636
TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
3637
{
Robert Shearman's avatar
Robert Shearman committed
3638
    TRACE("%s hwnd=%p\n",
3639
	   infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
Alexandre Julliard's avatar
Alexandre Julliard committed
3640 3641 3642

    return infoPtr->bUnicode;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3643 3644


3645
static inline LRESULT
3646
TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3647 3648 3649 3650 3651
{
    return infoPtr->iVersion;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3652
static LRESULT
3653
TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
Alexandre Julliard's avatar
Alexandre Julliard committed
3654 3655
{
    TBUTTON_INFO *btnPtr;
3656
    BYTE oldState;
3657
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3658

3659 3660
    TRACE("\n");

3661
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3662 3663 3664 3665
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3666
    oldState = btnPtr->fsState;
3667 3668

    if (fHide)
Alexandre Julliard's avatar
Alexandre Julliard committed
3669
	btnPtr->fsState |= TBSTATE_HIDDEN;
3670 3671
    else
	btnPtr->fsState &= ~TBSTATE_HIDDEN;
Alexandre Julliard's avatar
Alexandre Julliard committed
3672

3673 3674 3675 3676
    if (oldState != btnPtr->fsState) {
        TOOLBAR_LayoutToolbar (infoPtr);
        InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3677 3678 3679 3680 3681

    return TRUE;
}


3682
static inline LRESULT
3683
TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
Alexandre Julliard's avatar
Alexandre Julliard committed
3684
{
3685
    return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
3686 3687 3688
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3689
static LRESULT
3690
TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
Alexandre Julliard's avatar
Alexandre Julliard committed
3691 3692
{
    TBUTTON_INFO *btnPtr;
3693
    INT nIndex;
3694
    DWORD oldState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3695

3696
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3697 3698 3699 3700
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3701
    oldState = btnPtr->fsState;
3702 3703

    if (fIndeterminate)
Alexandre Julliard's avatar
Alexandre Julliard committed
3704
	btnPtr->fsState |= TBSTATE_INDETERMINATE;
3705 3706
    else
	btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3707

3708
    if(oldState != btnPtr->fsState)
3709
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3710 3711 3712 3713 3714 3715

    return TRUE;
}


static LRESULT
3716
TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
3717
{
3718 3719
    if (lpTbb == NULL)
	return FALSE;
3720 3721 3722 3723 3724 3725 3726 3727

    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).
	*/
3728
	nIndex = infoPtr->nNumButtons;
3729 3730 3731

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

3733
    TRACE("inserting button index=%d\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3734 3735
    if (nIndex > infoPtr->nNumButtons) {
	nIndex = infoPtr->nNumButtons;
3736
	TRACE("adjust index=%d\n", nIndex);
Alexandre Julliard's avatar
Alexandre Julliard committed
3737 3738
    }

3739
    return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3740 3741
}

3742
/* << TOOLBAR_InsertMarkHitTest >> */
Alexandre Julliard's avatar
Alexandre Julliard committed
3743 3744


Alexandre Julliard's avatar
Alexandre Julliard committed
3745
static LRESULT
3746
TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3747
{
3748
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3749

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

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


static LRESULT
3759
TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3760
{
3761
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3762

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

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


static LRESULT
3772
TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3773
{
3774
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3775

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

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


static LRESULT
3785
TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3786
{
3787
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3788

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

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


static LRESULT
3798
TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3799
{
3800
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3801

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

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


static LRESULT
3811
TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
Alexandre Julliard's avatar
Alexandre Julliard committed
3812
{
3813
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
3814

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

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


3823
static LRESULT
3824
TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3825 3826
{
    TBADDBITMAP tbab;
3827
    tbab.hInst = hInstance;
3828
    tbab.nID = wParam;
3829

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

3832
    return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3833 3834 3835 3836
}


static LRESULT
3837
TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3838 3839 3840 3841 3842
{
    WCHAR wszAccel[] = {'&',wAccel,0};
    int i;
    
    TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3843
        infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877
    
    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
3878
TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3879 3880
{
    INT nIndex;
3881 3882
    DWORD oldState;
    TBUTTON_INFO *btnPtr;
3883

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

3886
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3887 3888 3889
    if (nIndex == -1)
        return FALSE;

3890 3891 3892
    btnPtr = &infoPtr->buttons[nIndex];
    oldState = btnPtr->fsState;

3893
    if (fMark)
3894
        btnPtr->fsState |= TBSTATE_MARKED;
3895
    else
3896 3897 3898
        btnPtr->fsState &= ~TBSTATE_MARKED;

    if(oldState != btnPtr->fsState)
3899
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3900 3901 3902 3903

    return TRUE;
}

Robert Shearman's avatar
Robert Shearman committed
3904 3905

/* fixes up an index of a button affected by a move */
3906
static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
Robert Shearman's avatar
Robert Shearman committed
3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
{
    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
3926
TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
Robert Shearman's avatar
Robert Shearman committed
3927 3928 3929 3930 3931
{
    INT nIndex;
    INT nCount;
    TBUTTON_INFO button;

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

3934
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
Robert Shearman's avatar
Robert Shearman committed
3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
    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);
    }

3967 3968 3969
    TOOLBAR_LayoutToolbar(infoPtr);
    TOOLBAR_AutoSize(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Robert Shearman's avatar
Robert Shearman committed
3970 3971 3972

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3973 3974 3975


static LRESULT
3976
TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
Alexandre Julliard's avatar
Alexandre Julliard committed
3977 3978
{
    TBUTTON_INFO *btnPtr;
3979
    INT nIndex;
3980
    DWORD oldState;
Alexandre Julliard's avatar
Alexandre Julliard committed
3981

3982
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3983 3984 3985 3986
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];
3987
    oldState = btnPtr->fsState;
3988 3989

    if (fPress)
Alexandre Julliard's avatar
Alexandre Julliard committed
3990
	btnPtr->fsState |= TBSTATE_PRESSED;
3991 3992
    else
	btnPtr->fsState &= ~TBSTATE_PRESSED;
Alexandre Julliard's avatar
Alexandre Julliard committed
3993

3994
    if(oldState != btnPtr->fsState)
3995
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
3996 3997 3998 3999

    return TRUE;
}

4000 4001
/* FIXME: there might still be some confusion her between number of buttons
 * and number of bitmaps */
4002
static LRESULT
4003
TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
4004 4005 4006
{
    HBITMAP hBitmap;
    int i = 0, nOldButtons = 0, pos = 0;
4007
    int nOldBitmaps, nNewBitmaps = 0;
4008
    HIMAGELIST himlDef = 0;
4009

4010
    TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4011 4012
          lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
          lpReplace->nButtons);
Alexandre Julliard's avatar
Alexandre Julliard committed
4013

4014
    if (lpReplace->hInstOld == HINST_COMMCTRL)
4015 4016 4017 4018
    {
        FIXME("changing standard bitmaps not implemented\n");
        return FALSE;
    }
4019
    else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4020
        FIXME("resources not in the current module not implemented\n");
4021

4022
    TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4023 4024
    for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
        TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4025
        TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4026 4027
        if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
        {
4028
            TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4029 4030 4031 4032
            nOldButtons = tbi->nButtons;
            tbi->nButtons = lpReplace->nButtons;
            tbi->hInst = lpReplace->hInstNew;
            tbi->nID = lpReplace->nIDNew;
4033
            TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4034 4035 4036 4037 4038 4039 4040
            break;
        }
        pos += tbi->nButtons;
    }

    if (nOldButtons == 0)
    {
4041
        WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4042 4043
        return FALSE;
    }
4044
    
4045 4046 4047 4048 4049 4050 4051 4052
    /* 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);

4053 4054
    himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
    nOldBitmaps = ImageList_GetImageCount(himlDef);
4055

4056
    /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4057

4058
    for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4059
        ImageList_Remove(himlDef, i);
4060

4061
    if (hBitmap)
4062
    {
4063
       ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4064
       nNewBitmaps = ImageList_GetImageCount(himlDef);
4065
       DeleteObject(hBitmap);
4066
    }
4067

4068 4069 4070 4071 4072
    infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;

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

4073
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4074 4075
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4076

4077 4078 4079

/* helper for TOOLBAR_SaveRestoreW */
static BOOL
4080
TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
Alexandre Julliard's avatar
Alexandre Julliard committed
4081
{
4082 4083 4084 4085
    NMTBSAVE save;
    INT ret, i;
    BOOL alloced = FALSE;
    HKEY key;
Alexandre Julliard's avatar
Alexandre Julliard committed
4086

4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128
    TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );

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

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

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

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

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

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

    if (alloced) Free( save.pData );
    return !ret;
4129
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4130 4131


4132 4133 4134 4135 4136
/* helper for TOOLBAR_Restore */
static void
TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
{
    INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
4137

4138 4139
    for (i = 0; i < infoPtr->nNumButtons; i++)
    {
4140
        free_string( infoPtr->buttons + i );
4141
        TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
Alexandre Julliard's avatar
Alexandre Julliard committed
4142 4143
    }

4144 4145 4146 4147 4148 4149 4150 4151
    Free(infoPtr->buttons);
    infoPtr->buttons = NULL;
    infoPtr->nNumButtons = 0;
}


/* helper for TOOLBAR_SaveRestoreW */
static BOOL
4152
TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4153 4154 4155 4156 4157 4158 4159
{
    LONG res;
    HKEY hkey = NULL;
    BOOL ret = FALSE;
    DWORD dwType;
    DWORD dwSize = 0;
    NMTBRESTORE nmtbr;
4160
    NMHDR hdr;
4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176

    /* 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)
    {
4177
        nmtbr.pData = Alloc(dwSize);
4178
        nmtbr.cbData = dwSize;
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
        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))
        {
4193
            INT i, count = nmtbr.cButtons;
4194 4195 4196 4197 4198

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

4199
            for (i = 0; i < count; i++)
4200 4201 4202 4203 4204
            {
                nmtbr.iItem = i;
                nmtbr.tbButton.iBitmap = -1;
                nmtbr.tbButton.fsState = 0;
                nmtbr.tbButton.fsStyle = 0;
4205 4206
                nmtbr.tbButton.dwData = 0;
                nmtbr.tbButton.iString = 0;
4207 4208

                if (*nmtbr.pCurrent & 0x80000000)
4209 4210
                {
                    /* separator */
4211
                    nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4212
                    nmtbr.tbButton.idCommand = 0;
4213
                    nmtbr.tbButton.fsStyle = BTNS_SEP;
4214 4215
                    if (*nmtbr.pCurrent != (DWORD)-1)
                        nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4216 4217 4218 4219 4220 4221 4222 4223
                }
                else
                    nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;

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

4224
                /* All returned ptrs and -1 are ignored */
4225
                if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4226
                    nmtbr.tbButton.iString = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4227

4228
                TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4229
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
4230

4231 4232
            TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
            for (i = 0; ; i++)
4233
            {
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266
                NMTOOLBARW tb;
                TBBUTTONINFOW bi;
                WCHAR buf[128];
                UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
                INT idx;

                memset( &tb, 0, sizeof(tb) );
                tb.iItem = i;
                tb.cchText = sizeof(buf) / sizeof(buf[0]);
                tb.pszText = buf;

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

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

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

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

                TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4267
            }
4268
            TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4269 4270 4271 4272 4273 4274

            /* 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)
4275
                    TOOLBAR_DeleteButton(infoPtr, i);
4276 4277 4278 4279

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

4284
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
4285 4286 4287
}


4288
static LRESULT
4289
TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4290
{
4291
    if (lpSave == NULL) return 0;
4292

4293
    if (wParam)
4294
        return TOOLBAR_Save(infoPtr, lpSave);
4295 4296 4297
    else
        return TOOLBAR_Restore(infoPtr, lpSave);
}
4298 4299


4300
static LRESULT
4301
TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4302
{
4303
    LPWSTR pszValueName = 0, pszSubKey = 0;
4304
    TBSAVEPARAMSW SaveW;
4305
    LRESULT result = 0;
4306
    int len;
4307

4308
    if (lpSave == NULL) return 0;
4309

4310
    len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4311
    pszSubKey = Alloc(len * sizeof(WCHAR));
4312
    if (!pszSubKey) goto exit;
4313
    MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4314

4315
    len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4316
    pszValueName = Alloc(len * sizeof(WCHAR));
4317 4318 4319 4320 4321 4322
    if (!pszValueName) goto exit;
    MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);

    SaveW.pszValueName = pszValueName;
    SaveW.pszSubKey = pszSubKey;
    SaveW.hkr = lpSave->hkr;
4323
    result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4324 4325

exit:
4326 4327
    Free (pszValueName);
    Free (pszSubKey);
4328

4329
    return result;
4330 4331 4332 4333
}


static LRESULT
4334
TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4335 4336 4337
{
    BOOL bOldAnchor = infoPtr->bAnchor;

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

4340
    infoPtr->bAnchor = bAnchor;
4341

4342
    /* Native does not remove the hot effect from an already hot button */
4343

4344 4345
    return (LRESULT)bOldAnchor;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4346

Alexandre Julliard's avatar
Alexandre Julliard committed
4347

Alexandre Julliard's avatar
Alexandre Julliard committed
4348
static LRESULT
4349
TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4350
{
4351
    HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4352 4353
    short width = (short)LOWORD(lParam);
    short height = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4354

4355
    TRACE("hwnd=%p, wParam=%ld, size %d x %d\n", infoPtr->hwndSelf, wParam, width, height);
Robert Shearman's avatar
Robert Shearman committed
4356 4357

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

4360 4361 4362 4363 4364
    /* 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
4365

4366
    if (infoPtr->nNumButtons > 0)
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377
        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;
    }
4378

4379 4380 4381 4382 4383
    /* width or height of -1 means no change */
    if (width != -1)
        infoPtr->nBitmapWidth = width;
    if (height != -1)
        infoPtr->nBitmapHeight = height;
4384

Robert Shearman's avatar
Robert Shearman committed
4385 4386 4387 4388 4389
    if ((himlDef == infoPtr->himlInt) &&
        (ImageList_GetImageCount(infoPtr->himlInt) == 0))
    {
        ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
            infoPtr->nBitmapHeight);
4390
    }
4391

4392
    TOOLBAR_CalcToolbar(infoPtr);
4393
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4394 4395 4396 4397
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4398
static LRESULT
4399 4400
TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
                       const TBBUTTONINFOW *lptbbi, BOOL isW)
4401 4402 4403
{
    TBUTTON_INFO *btnPtr;
    INT nIndex;
Robert Shearman's avatar
Robert Shearman committed
4404
    RECT oldBtnRect;
4405 4406 4407 4408 4409 4410

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

4411
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4412 4413 4414 4415 4416 4417 4418 4419 4420 4421
    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
4422 4423
    if (lptbbi->dwMask & TBIF_SIZE)
	btnPtr->cx = lptbbi->cx;
4424 4425 4426 4427 4428
    if (lptbbi->dwMask & TBIF_STATE)
	btnPtr->fsState = lptbbi->fsState;
    if (lptbbi->dwMask & TBIF_STYLE)
	btnPtr->fsStyle = lptbbi->fsStyle;

4429 4430
    if (lptbbi->dwMask & TBIF_TEXT)
        set_stringT( btnPtr, lptbbi->pszText, isW );
Robert Shearman's avatar
Robert Shearman committed
4431 4432 4433

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

    if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4437
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Robert Shearman's avatar
Robert Shearman committed
4438
    else
4439
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
Robert Shearman's avatar
Robert Shearman committed
4440

4441 4442
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4443 4444 4445


static LRESULT
4446
TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4447
{
4448
    INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4449
    int top = default_top_margin(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
4450

4451
    if ((cx < 0) || (cy < 0))
4452
    {
4453
        ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4454
        return FALSE;
4455
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4456

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

4459
    /* The documentation claims you can only change the button size before
4460 4461
     * any button has been added. But this is wrong.
     * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4462 4463 4464
     * 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.
     */
4465 4466 4467
    /*
     * 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
4468
     * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4469
     */
4470
    if (cx == 0) cx = 24;
4471
    if (cy == 0) cy = 22;
4472

4473
    cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4474
    cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4475

4476 4477 4478 4479 4480 4481 4482 4483 4484 4485
    if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
        top != infoPtr->iTopMargin)
    {
        infoPtr->nButtonWidth = cx;
        infoPtr->nButtonHeight = cy;
        infoPtr->iTopMargin = top;

        TOOLBAR_LayoutToolbar( infoPtr );
        InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4486 4487 4488 4489
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4490
static LRESULT
4491
TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4492
{
4493
    /* if setting to current values, ignore */
4494 4495
    if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
	(infoPtr->cxMax == (short)HIWORD(lParam))) {
4496 4497
	TRACE("matches current width, min=%d, max=%d, no recalc\n",
	      infoPtr->cxMin, infoPtr->cxMax);
4498
	return TRUE;
4499
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4500

4501
    /* save new values */
4502 4503
    infoPtr->cxMin = (short)LOWORD(lParam);
    infoPtr->cxMax = (short)HIWORD(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4504

4505 4506 4507
    /* 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). */
4508
    TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4509 4510
	infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);

4511
    TOOLBAR_CalcToolbar (infoPtr);
4512

4513
    InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4514

Alexandre Julliard's avatar
Alexandre Julliard committed
4515 4516
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4517 4518 4519


static LRESULT
4520
TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
Alexandre Julliard's avatar
Alexandre Julliard committed
4521 4522 4523 4524
{
    if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
	return FALSE;

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

Alexandre Julliard's avatar
Alexandre Julliard committed
4527 4528
    if (infoPtr->hwndToolTip) {

4529
	FIXME("change tool tip\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
4530 4531 4532

    }

Alexandre Julliard's avatar
Alexandre Julliard committed
4533 4534 4535 4536
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4537
static LRESULT
4538
TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4539 4540
{
    HIMAGELIST himlTemp;
4541
    INT id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4542

4543 4544
    if (infoPtr->iVersion >= 5)
        id = wParam;
4545

4546 4547
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis, 
        &infoPtr->cimlDis, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4548 4549 4550

    /* FIXME: redraw ? */

4551
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4552 4553 4554
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4555
static LRESULT
4556
TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD flags)
Alexandre Julliard's avatar
Alexandre Julliard committed
4557
{
4558
    DWORD old_flags;
Alexandre Julliard's avatar
Alexandre Julliard committed
4559

4560
    TRACE("hwnd = %p, mask = 0x%08x, flags = 0x%08x\n", infoPtr->hwndSelf, mask, flags);
4561

4562 4563
    old_flags = infoPtr->dwDTFlags;
    infoPtr->dwDTFlags = (old_flags & ~mask) | (flags & mask);
Alexandre Julliard's avatar
Alexandre Julliard committed
4564

4565
    return (LRESULT)old_flags;
Alexandre Julliard's avatar
Alexandre Julliard committed
4566
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4567

Robert Shearman's avatar
Robert Shearman committed
4568 4569 4570 4571 4572 4573
/* 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
4574
static LRESULT
4575
TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
Alexandre Julliard's avatar
Alexandre Julliard committed
4576
{
4577
    DWORD old_style = infoPtr->dwExStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
4578

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

4581 4582 4583 4584
    if (mask)
	infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
    else
	infoPtr->dwExStyle = style;
4585 4586

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

4590
    if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4591
        TOOLBAR_CalcToolbar(infoPtr);
4592
    else
4593
        TOOLBAR_LayoutToolbar(infoPtr);
4594

4595 4596
    TOOLBAR_AutoSize(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4597

4598
    return old_style;
Alexandre Julliard's avatar
Alexandre Julliard committed
4599 4600 4601 4602
}


static LRESULT
4603
TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4604 4605
{
    HIMAGELIST himlTemp;
4606
    INT id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4607

4608 4609
    if (infoPtr->iVersion >= 5)
        id = wParam;
Guy Albertelli's avatar
Guy Albertelli committed
4610

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

4613 4614
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot, 
        &infoPtr->cimlHot, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4615 4616 4617

    /* FIXME: redraw ? */

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


4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
/* 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;
4635

4636
        nmhotitem.dwFlags = dwReason;
4637 4638 4639
        if(infoPtr->nHotItem >= 0)
        {
            oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4640
            nmhotitem.idOld = oldBtnPtr->idCommand;
4641
        }
4642
        else
4643
        {
4644
            nmhotitem.dwFlags |= HICF_ENTERING;
4645 4646
            nmhotitem.idOld = 0;
        }
4647 4648

        if (nHit >= 0)
4649
        {
4650
            btnPtr = &infoPtr->buttons[nHit];
4651 4652
            nmhotitem.idNew = btnPtr->idCommand;
        }
4653
	else
4654
	{
4655
	    nmhotitem.dwFlags |= HICF_LEAVING;
4656 4657
	    nmhotitem.idNew = 0;
	}
4658

4659 4660 4661
	/* now change the hot and invalidate the old and new buttons - if the
	 * parent agrees */
	if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4662
	{
4663 4664 4665 4666
            if (oldBtnPtr) {
                oldBtnPtr->bHot = FALSE;
                InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
            }
4667 4668 4669 4670 4671 4672 4673 4674
            /* 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;            
4675 4676 4677 4678
        }
    }
}

4679
static LRESULT
4680
TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4681 4682 4683
{
    INT nOldHotItem = infoPtr->nHotItem;

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

4686
    if (nHotItem >= infoPtr->nNumButtons)
4687 4688
        return infoPtr->nHotItem;
    
4689 4690
    if (nHotItem < 0)
        nHotItem = -1;
4691 4692 4693

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

4695
    TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4696 4697

    if (nOldHotItem < 0)
4698
        return -1;
4699 4700 4701

    return (LRESULT)nOldHotItem;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4702 4703 4704


static LRESULT
4705
TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
Alexandre Julliard's avatar
Alexandre Julliard committed
4706 4707
{
    HIMAGELIST himlTemp;
4708
    INT oldButtonWidth = infoPtr->nButtonWidth;
4709 4710
    INT oldBitmapWidth = infoPtr->nBitmapWidth;
    INT oldBitmapHeight = infoPtr->nBitmapHeight;
4711
    INT i, id = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4712

4713 4714
    if (infoPtr->iVersion >= 5)
        id = wParam;
Guy Albertelli's avatar
Guy Albertelli committed
4715

4716 4717
    himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef, 
        &infoPtr->cimlDef, himl, id);
Alexandre Julliard's avatar
Alexandre Julliard committed
4718

4719 4720 4721
    infoPtr->nNumBitmaps = 0;
    for (i = 0; i < infoPtr->cimlDef; i++)
        infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4722

4723 4724 4725
    if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
            &infoPtr->nBitmapHeight))
    {
4726 4727
        infoPtr->nBitmapWidth = 1;
        infoPtr->nBitmapHeight = 1;
4728
    }
4729 4730
    if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
    {
4731
        TOOLBAR_CalcToolbar(infoPtr);
4732
        if (infoPtr->nButtonWidth < oldButtonWidth)
4733
            TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4734
    }
4735 4736

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

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

4742
    return (LRESULT)himlTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
4743
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4744

Alexandre Julliard's avatar
Alexandre Julliard committed
4745 4746

static LRESULT
4747
TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
Alexandre Julliard's avatar
Alexandre Julliard committed
4748
{
4749
    infoPtr->nIndent = nIndent;
4750

4751
    TRACE("\n");
4752

4753
    /* process only on indent changing */
4754
    if(infoPtr->nIndent != nIndent)
4755
    {
4756
        infoPtr->nIndent = nIndent;
4757 4758
        TOOLBAR_CalcToolbar (infoPtr);
        InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4759
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4760 4761 4762 4763 4764

    return TRUE;
}


4765
static LRESULT
4766
TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4767
{
4768
    TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4769 4770 4771

    if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
    {
4772
        FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4773 4774 4775 4776 4777 4778 4779 4780 4781
        return 0;
    }

    if ((lptbim->iButton == -1) || 
        ((lptbim->iButton < infoPtr->nNumButtons) &&
         (lptbim->iButton >= 0)))
    {
        infoPtr->tbim = *lptbim;
        /* FIXME: don't need to update entire toolbar */
4782
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4783 4784 4785 4786 4787 4788
    }
    else
        ERR("Invalid button index %d\n", lptbim->iButton);

    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4789 4790 4791


static LRESULT
4792
TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
Alexandre Julliard's avatar
Alexandre Julliard committed
4793
{
4794
    infoPtr->clrInsertMark = clr;
Alexandre Julliard's avatar
Alexandre Julliard committed
4795

4796
    /* FIXME: don't need to update entire toolbar */
4797
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4798 4799 4800 4801 4802

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4803
static LRESULT
4804
TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
Alexandre Julliard's avatar
Alexandre Julliard committed
4805
{
4806
    infoPtr->nMaxTextRows = nMaxRows;
Alexandre Julliard's avatar
Alexandre Julliard committed
4807

4808
    TOOLBAR_CalcToolbar(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
4809 4810 4811 4812
    return TRUE;
}


4813 4814 4815 4816 4817 4818 4819 4820 4821
/* 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.
4822
 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4823
 */
4824
static LRESULT
4825
TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4826 4827 4828 4829
{
    DWORD  oldPad;

    oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4830 4831
    infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
    infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4832
    TRACE("cx=%d, cy=%d\n",
4833 4834 4835
	  infoPtr->szPadding.cx, infoPtr->szPadding.cy);
    return (LRESULT) oldPad;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
4836 4837 4838


static LRESULT
4839
TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
Alexandre Julliard's avatar
Alexandre Julliard committed
4840
{
4841
    HWND hwndOldNotify;
Alexandre Julliard's avatar
Alexandre Julliard committed
4842

4843 4844
    TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
4845
    hwndOldNotify = infoPtr->hwndNotify;
4846
    infoPtr->hwndNotify = hParent;
Alexandre Julliard's avatar
Alexandre Julliard committed
4847

4848
    return (LRESULT)hwndOldNotify;
Alexandre Julliard's avatar
Alexandre Julliard committed
4849 4850 4851 4852
}


static LRESULT
4853
TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
Alexandre Julliard's avatar
Alexandre Julliard committed
4854
{
4855 4856
    int rows = LOWORD(wParam);
    BOOL bLarger = HIWORD(wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
4857

4858
    TRACE("\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
4859

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

4862
    if(infoPtr->nRows != rows)
4863
    {
4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
        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).

4879
           Basic algorithm - If N buttons, and y rows requested, each row
4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
           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
4899
        /* Round up so more per line, i.e., less rows */
4900
        idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941

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

4943
        /* recalculate toolbar */
4944
        TOOLBAR_CalcToolbar (infoPtr);
4945

4946 4947 4948 4949
        /* 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) {
4950
            SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4951 4952
                         infoPtr->rcBound.right - infoPtr->rcBound.left,
                         infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4953
                         SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4954 4955
        }

4956
        /* repaint toolbar */
4957
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4958
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4959 4960 4961 4962 4963 4964 4965 4966 4967

    /* 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
4968 4969 4970
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
4971 4972

static LRESULT
4973
TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
4974 4975
{
    TBUTTON_INFO *btnPtr;
4976
    INT nIndex;
Alexandre Julliard's avatar
Alexandre Julliard committed
4977

4978
    nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
4979 4980 4981 4982 4983
    if (nIndex == -1)
	return FALSE;

    btnPtr = &infoPtr->buttons[nIndex];

4984 4985 4986
    /* if hidden state has changed the invalidate entire window and recalc */
    if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
	btnPtr->fsState = LOWORD(lParam);
4987 4988
	TOOLBAR_CalcToolbar (infoPtr);
	InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4989 4990 4991
	return TRUE;
    }

4992 4993 4994 4995
    /* process state changing if current state doesn't match new state */
    if(btnPtr->fsState != LOWORD(lParam))
    {
        btnPtr->fsState = LOWORD(lParam);
4996
        InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4997
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4998 4999 5000 5001

    return TRUE;
}

5002 5003 5004 5005 5006 5007 5008
static inline void unwrap(TOOLBAR_INFO *info)
{
    int i;

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

static LRESULT
5011
TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
Alexandre Julliard's avatar
Alexandre Julliard committed
5012
{
5013 5014 5015 5016 5017 5018 5019 5020 5021
    DWORD dwOldStyle = infoPtr->dwStyle;

    TRACE("new style 0x%08x\n", style);

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

5022
    infoPtr->dwStyle = style;
5023 5024
    TOOLBAR_CheckStyle(infoPtr);

5025
    if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5026 5027 5028
    {
        if (dwOldStyle & TBSTYLE_WRAPABLE)
            unwrap(infoPtr);
5029
        TOOLBAR_CalcToolbar(infoPtr);
5030
    }
5031
    else if ((dwOldStyle ^ style) & CCS_VERT)
5032 5033 5034 5035 5036 5037 5038 5039 5040
        TOOLBAR_LayoutToolbar(infoPtr);

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

5041
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
5042 5043 5044
}


5045
static inline LRESULT
5046
TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
Alexandre Julliard's avatar
Alexandre Julliard committed
5047
{
5048
    TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5049

5050
    infoPtr->hwndToolTip = hwndTooltip;
Alexandre Julliard's avatar
Alexandre Julliard committed
5051 5052 5053 5054
    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5055
static LRESULT
5056
TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5057
{
5058
    BOOL bTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
5059

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

Alexandre Julliard's avatar
Alexandre Julliard committed
5063
    bTemp = infoPtr->bUnicode;
5064
    infoPtr->bUnicode = (BOOL)wParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
5065 5066

    return bTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
5067
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5068 5069


5070
static LRESULT
5071
TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5072
{
5073 5074
    lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
	                       comctl32_color.clrBtnHighlight :
5075
                               infoPtr->clrBtnHighlight;
5076
    lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5077
	                   comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5078 5079 5080 5081 5082
    return 1;
}


static LRESULT
5083
TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5084
{
5085
    TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5086 5087 5088 5089 5090
	  lParam->clrBtnHighlight, lParam->clrBtnShadow,
	  infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);

    infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
    infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5091
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5092 5093 5094 5095
    return 0;
}


5096
static LRESULT
5097
TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5098 5099 5100 5101 5102
{
    INT iOldVersion = infoPtr->iVersion;

    infoPtr->iVersion = iVersion;

5103
    if (infoPtr->iVersion >= 5)
5104
        TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5105

5106 5107 5108
    return iOldVersion;
}

Robert Shearman's avatar
Robert Shearman committed
5109 5110

static LRESULT
5111
TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
Robert Shearman's avatar
Robert Shearman committed
5112 5113 5114 5115 5116
{
    WORD iString = HIWORD(wParam);
    WORD buffersize = LOWORD(wParam);
    LRESULT ret = -1;

5117
    TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
Robert Shearman's avatar
Robert Shearman committed
5118 5119 5120 5121

    if (iString < infoPtr->nNumStrings)
    {
        ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5122
        ret--;
Robert Shearman's avatar
Robert Shearman committed
5123 5124 5125 5126

        TRACE("returning %s\n", debugstr_a(str));
    }
    else
5127
        WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
Robert Shearman's avatar
Robert Shearman committed
5128 5129 5130 5131 5132 5133

    return ret;
}


static LRESULT
5134
TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
Robert Shearman's avatar
Robert Shearman committed
5135 5136 5137 5138 5139
{
    WORD iString = HIWORD(wParam);
    WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
    LRESULT ret = -1;

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

    if (iString < infoPtr->nNumStrings)
    {
        len = min(len, strlenW(infoPtr->strings[iString]));
        ret = (len+1)*sizeof(WCHAR);
5146 5147 5148 5149 5150
        if (str)
        {
            memcpy(str, infoPtr->strings[iString], ret);
            str[len] = '\0';
        }
5151
        ret = len;
Robert Shearman's avatar
Robert Shearman committed
5152 5153 5154 5155

        TRACE("returning %s\n", debugstr_w(str));
    }
    else
5156
        WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
Robert Shearman's avatar
Robert Shearman committed
5157 5158 5159 5160

    return ret;
}

5161
static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5162
{
5163
    SIZE * pSize = (SIZE*)lParam;
5164
    FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub\n", hwnd, wParam, pSize->cx, pSize->cy);
5165 5166
    return 0;
}
5167

5168 5169 5170
/* 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). */
5171
static LRESULT
5172
TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5173 5174
{
    INT nOldHotItem = infoPtr->nHotItem;
5175

5176
    TRACE("old item=%d, new item=%d, flags=%08x\n",
5177
	  nOldHotItem, nHotItem, (DWORD)lParam);
5178

5179 5180
    if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
        nHotItem = -1;
5181

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

5185
    TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5186

5187
    return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5188 5189
}

5190 5191 5192
/* Sets the toolbar global iListGap parameter which controls the amount of
 * spacing between the image and the text of buttons for TBSTYLE_LIST
 * toolbars. */
5193
static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5194
{
5195
    TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5196
    
5197
    infoPtr->iListGap = iListGap;
5198

5199
    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5200

5201 5202
    return 0;
}
5203

5204 5205
/* Returns the number of maximum number of image lists associated with the
 * various states. */
5206
static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
Robert Shearman's avatar
Robert Shearman committed
5207
{
5208
    TRACE("hwnd=%p\n", infoPtr->hwndSelf);
Robert Shearman's avatar
Robert Shearman committed
5209 5210 5211 5212

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

5213
static LRESULT
5214
TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227
{
    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
     *
     */
5228
    TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5229 5230 5231 5232 5233 5234 5235 5236 5237
	  wParam, lParam, lpsize->cx, lpsize->cy);

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

5240
	    GetWindowRect(infoPtr->hwndSelf, &rc);
5241
	    MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5242
            TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253
	    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:
5254
	FIXME("Unknown wParam %ld\n", wParam);
5255 5256
	return 0;
    }
5257
    TRACE("set to -> 0x%08x 0x%08x\n",
5258 5259 5260 5261
	  lpsize->cx, lpsize->cy);
    return 1;
}

5262 5263
static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
5264
    FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5265 5266 5267 5268 5269

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

5270

Alexandre Julliard's avatar
Alexandre Julliard committed
5271
static LRESULT
5272
TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
5273
{
5274
    TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5275
    LOGFONTW logFont;
Alexandre Julliard's avatar
Alexandre Julliard committed
5276

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

5279
    infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5280
    GetClientRect(hwnd, &infoPtr->client_rect);
5281
    infoPtr->bUnicode = infoPtr->hwndNotify && 
5282
        (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5283
    infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
Alexandre Julliard's avatar
Alexandre Julliard committed
5284

5285 5286
    SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
    infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5287 5288
    
    OpenThemeData (hwnd, themeClass);
5289

5290
    TOOLBAR_CheckStyle (infoPtr);
5291

Alexandre Julliard's avatar
Alexandre Julliard committed
5292 5293 5294 5295 5296
    return 0;
}


static LRESULT
5297
TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
5298
{
5299 5300
    INT i;

Alexandre Julliard's avatar
Alexandre Julliard committed
5301
    /* delete tooltip control */
Alexandre Julliard's avatar
Alexandre Julliard committed
5302
    if (infoPtr->hwndToolTip)
5303
	DestroyWindow (infoPtr->hwndToolTip);
Alexandre Julliard's avatar
Alexandre Julliard committed
5304

5305
    /* delete temporary buffer for tooltip text */
5306
    Free (infoPtr->pszTooltipText);
5307
    Free (infoPtr->bitmaps);            /* bitmaps list */
5308

Alexandre Julliard's avatar
Alexandre Julliard committed
5309
    /* delete button data */
5310
    for (i = 0; i < infoPtr->nNumButtons; i++)
5311
        free_string( infoPtr->buttons + i );
5312
    Free (infoPtr->buttons);
Alexandre Julliard's avatar
Alexandre Julliard committed
5313

Alexandre Julliard's avatar
Alexandre Julliard committed
5314 5315 5316
    /* delete strings */
    if (infoPtr->strings) {
	for (i = 0; i < infoPtr->nNumStrings; i++)
5317
	    Free (infoPtr->strings[i]);
Alexandre Julliard's avatar
Alexandre Julliard committed
5318

5319
	Free (infoPtr->strings);
Alexandre Julliard's avatar
Alexandre Julliard committed
5320 5321
    }

5322 5323 5324
    /* destroy internal image list */
    if (infoPtr->himlInt)
	ImageList_Destroy (infoPtr->himlInt);
Alexandre Julliard's avatar
Alexandre Julliard committed
5325

5326 5327 5328
    TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
    TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
    TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5329

Alexandre Julliard's avatar
Alexandre Julliard committed
5330
    /* delete default font */
5331
    DeleteObject (infoPtr->hDefaultFont);
5332
        
5333
    CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
Alexandre Julliard's avatar
Alexandre Julliard committed
5334

Alexandre Julliard's avatar
Alexandre Julliard committed
5335
    /* free toolbar info data */
5336
    SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5337
    Free (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5338 5339 5340 5341 5342

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5343
static LRESULT
5344
TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5345
{
5346
    NMTBCUSTOMDRAW tbcd;
5347 5348
    INT ret = FALSE;
    DWORD ntfret;
5349
    HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5350
    DWORD dwEraseCustDraw = 0;
5351

5352 5353 5354 5355
    /* the app has told us not to redraw the toolbar */
    if (!infoPtr->bDoRedraw)
        return FALSE;

5356
    if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5357 5358 5359
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
	tbcd.nmcd.hdc = (HDC)wParam;
5360
	ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5361
	dwEraseCustDraw = ntfret & 0xffff;
5362

5363
	/* FIXME: in general the return flags *can* be or'ed together */
5364
	switch (dwEraseCustDraw)
5365 5366 5367 5368 5369 5370
	    {
	    case CDRF_DODEFAULT:
		break;
	    case CDRF_SKIPDEFAULT:
		return TRUE;
	    default:
5371
		FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5372
		      infoPtr->hwndSelf, ntfret);
5373 5374
	    }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5375

5376
    /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5377 5378
     * to my parent for processing.
     */
5379
    if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5380 5381 5382 5383 5384 5385
	POINT pt, ptorig;
	HDC hdc = (HDC)wParam;
	HWND parent;

	pt.x = 0;
	pt.y = 0;
5386 5387
	parent = GetParent(infoPtr->hwndSelf);
	MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5388
	OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5389
	ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5390 5391
	SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
    }
Guy Albertelli's avatar
Guy Albertelli committed
5392
    if (!ret)
5393
	ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
5394

5395
    if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5396 5397 5398
	ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
	tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
	tbcd.nmcd.hdc = (HDC)wParam;
5399
	ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5400 5401
	dwEraseCustDraw = ntfret & 0xffff;
	switch (dwEraseCustDraw)
5402 5403 5404 5405 5406 5407
	    {
	    case CDRF_DODEFAULT:
		break;
	    case CDRF_SKIPDEFAULT:
		return TRUE;
	    default:
5408
		FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5409
		      infoPtr->hwndSelf, ntfret);
5410 5411
	    }
    }
5412
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
5413 5414 5415
}


5416
static inline LRESULT
5417
TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
Eric Pouech's avatar
Eric Pouech committed
5418
{
5419
    return (LRESULT)infoPtr->hFont;
Eric Pouech's avatar
Eric Pouech committed
5420 5421 5422
}


5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
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
5457
TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5458 5459 5460 5461 5462 5463 5464
{
    NMKEY nmkey;

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

    if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5465
        return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483

    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),
5484
                (LPARAM)infoPtr->hwndSelf);
5485 5486 5487 5488 5489 5490 5491 5492
        }
        break;
    }

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
5493
static LRESULT
5494
TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5495
{
5496
    POINT pt;
5497
    BOOL button;
Alexandre Julliard's avatar
Alexandre Julliard committed
5498

5499 5500
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5501
    TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5502

5503
    if (button)
5504
        TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5505
    else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5506
	TOOLBAR_Customize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
5507 5508 5509 5510 5511 5512

    return 0;
}


static LRESULT
5513
TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5514 5515
{
    TBUTTON_INFO *btnPtr;
5516 5517
    POINT pt;
    INT   nHit;
5518
    NMTOOLBARA nmtb;
5519
    NMMOUSE nmmouse;
Robert Shearman's avatar
Robert Shearman committed
5520
    BOOL bDragKeyPressed;
5521
    BOOL button;
Robert Shearman's avatar
Robert Shearman committed
5522

5523 5524
    TRACE("\n");

5525
    if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
Robert Shearman's avatar
Robert Shearman committed
5526 5527 5528
        bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
    else
        bDragKeyPressed = (wParam & MK_SHIFT);
Alexandre Julliard's avatar
Alexandre Julliard committed
5529

Alexandre Julliard's avatar
Alexandre Julliard committed
5530
    if (infoPtr->hwndToolTip)
5531
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5532 5533
			    WM_LBUTTONDOWN, wParam, lParam);

5534 5535
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5536
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5537

5538
    if (button)
Robert Shearman's avatar
Robert Shearman committed
5539
    {
5540
        btnPtr = &infoPtr->buttons[nHit];
5541

5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557
        if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
        {
            infoPtr->nButtonDrag = nHit;
            SetCapture (infoPtr->hwndSelf);

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

5558
            arrowRect = btnPtr->rect;
5559 5560 5561 5562 5563 5564 5565 5566
            arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);

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

5570 5571 5572 5573 5574 5575
                /* draw in pressed state */
                if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
                    btnPtr->fsState |= TBSTATE_PRESSED;
                else
                    btnPtr->bDropDownPressed = TRUE;
                RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5576

5577 5578 5579 5580 5581
                memset(&nmtb, 0, sizeof(nmtb));
                nmtb.iItem = btnPtr->idCommand;
                nmtb.rcButton = btnPtr->rect;
                res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
                TRACE("TBN_DROPDOWN responded with %ld\n", res);
5582

5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609
                if (res != TBDDRET_TREATPRESSED)
                {
                    MSG msg;

                    /* redraw button in unpressed state */
                    if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
                        btnPtr->fsState &= ~TBSTATE_PRESSED;
                    else
                        btnPtr->bDropDownPressed = FALSE;
                    InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);

                    /* find and set hot item */
                    GetCursorPos(&pt);
                    ScreenToClient(infoPtr->hwndSelf, &pt);
                    nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
                    if (!infoPtr->bAnchor || button)
                        TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);

                    /* remove any left mouse button down or double-click messages
                     * so that we can get a toggle effect on the button */
                    while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
                           PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
                        ;

                    return 0;
                }
                /* otherwise drop through and process as pushed */
5610
            }
5611 5612 5613
            infoPtr->bCaptured = TRUE;
            infoPtr->nButtonDown = nHit;
            infoPtr->bDragOutSent = FALSE;
5614

5615
            btnPtr->fsState |= TBSTATE_PRESSED;
5616

5617
            TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5618

5619 5620 5621 5622 5623
            if (btnPtr->fsState & TBSTATE_ENABLED)
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
            UpdateWindow(infoPtr->hwndSelf);
            SetCapture (infoPtr->hwndSelf);
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
5624

5625
        memset(&nmtb, 0, sizeof(nmtb));
5626 5627 5628
        nmtb.iItem = btnPtr->idCommand;
        TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
    }
Robert Shearman's avatar
Robert Shearman committed
5629

5630 5631 5632
    nmmouse.dwHitInfo = nHit;

    /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5633
    if (!button)
5634 5635 5636 5637 5638 5639 5640
        nmmouse.dwItemSpec = -1;
    else
    {
        nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
        nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
    }

5641
    ClientToScreen(infoPtr->hwndSelf, &pt);
5642 5643
    nmmouse.pt = pt;

5644
    if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5645
        return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5646

Alexandre Julliard's avatar
Alexandre Julliard committed
5647 5648 5649 5650
    return 0;
}

static LRESULT
5651
TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5652 5653
{
    TBUTTON_INFO *btnPtr;
5654 5655 5656
    POINT pt;
    INT   nHit;
    INT   nOldIndex = -1;
5657 5658 5659
    NMHDR hdr;
    NMMOUSE nmmouse;
    NMTOOLBARA nmtb;
5660
    BOOL button;
Alexandre Julliard's avatar
Alexandre Julliard committed
5661

Alexandre Julliard's avatar
Alexandre Julliard committed
5662
    if (infoPtr->hwndToolTip)
5663
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5664 5665
			    WM_LBUTTONUP, wParam, lParam);

5666 5667
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5668
    nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
Alexandre Julliard's avatar
Alexandre Julliard committed
5669

5670
    if (!infoPtr->bAnchor || button)
5671
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5672

Robert Shearman's avatar
Robert Shearman committed
5673 5674 5675 5676 5677 5678 5679 5680 5681
    if (infoPtr->nButtonDrag >= 0) {
        RECT rcClient;
        NMHDR hdr;

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

5682
        GetClientRect(infoPtr->hwndSelf, &rcClient);
Robert Shearman's avatar
Robert Shearman committed
5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699
        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))
5700
                        TOOLBAR_DeleteButton(infoPtr, nButton - 1);
Robert Shearman's avatar
Robert Shearman committed
5701 5702 5703 5704 5705 5706 5707
                }
                else /* else insert a separator before the dragged button */
                {
                    TBBUTTON tbb;
                    memset(&tbb, 0, sizeof(tbb));
                    tbb.fsStyle = BTNS_SEP;
                    tbb.iString = -1;
5708
                    TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
Robert Shearman's avatar
Robert Shearman committed
5709 5710 5711 5712 5713 5714 5715
                }
            }
            else
            {
                if (nButton == -1)
                {
                    if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5716
                        TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
Robert Shearman's avatar
Robert Shearman committed
5717
                    else
5718
                        TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
Robert Shearman's avatar
Robert Shearman committed
5719 5720
                }
                else
5721
                    TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
Robert Shearman's avatar
Robert Shearman committed
5722 5723 5724 5725 5726
            }
        }
        else
        {
            TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5727
            TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
Robert Shearman's avatar
Robert Shearman committed
5728 5729 5730
        }

        /* button under cursor changed so need to re-set hot item */
5731
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
Robert Shearman's avatar
Robert Shearman committed
5732 5733 5734 5735 5736
        infoPtr->nButtonDrag = -1;

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

5740 5741
	if (btnPtr->fsStyle & BTNS_CHECK) {
		if (btnPtr->fsStyle & BTNS_GROUP) {
Alexandre Julliard's avatar
Alexandre Julliard committed
5742
		    nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5743
			nHit);
5744
		    if ((nOldIndex != nHit) &&
Alexandre Julliard's avatar
Alexandre Julliard committed
5745 5746
			(nOldIndex != -1))
			infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5747
		    btnPtr->fsState |= TBSTATE_CHECKED;
Alexandre Julliard's avatar
Alexandre Julliard committed
5748 5749 5750 5751 5752 5753 5754
		}
		else {
		    if (btnPtr->fsState & TBSTATE_CHECKED)
			btnPtr->fsState &= ~TBSTATE_CHECKED;
		    else
			btnPtr->fsState |= TBSTATE_CHECKED;
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
5755
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
5756

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

5760 5761 5762
	/*
	 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
	 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5763
	 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5764
	 */
5765 5766
	if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
	    ReleaseCapture ();
5767
	infoPtr->nButtonDown = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
5768

5769
	/* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5770
	TOOLBAR_SendNotify (&hdr, infoPtr,
5771
			NM_RELEASEDCAPTURE);
5772

5773
	memset(&nmtb, 0, sizeof(nmtb));
5774
	nmtb.iItem = btnPtr->idCommand;
5775 5776
	TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
			TBN_ENDDRAG);
5777

5778 5779
	if (btnPtr->fsState & TBSTATE_ENABLED)
	{
5780
	    SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5781
	      MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5782 5783

            /* In case we have just been destroyed... */
5784
            if(!IsWindow(infoPtr->hwndSelf))
5785
                return 0;
5786 5787
        }
    }
5788

5789 5790 5791 5792
    /* !!! Undocumented - toolbar at 4.71 level and above sends
    * NM_CLICK with the NMMOUSE structure. */
    nmmouse.dwHitInfo = nHit;

5793
    if (!button)
5794 5795 5796 5797 5798
        nmmouse.dwItemSpec = -1;
    else
    {
        nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
        nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5799
    }
5800

5801
    ClientToScreen(infoPtr->hwndSelf, &pt);
5802 5803 5804
    nmmouse.pt = pt;

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

Alexandre Julliard's avatar
Alexandre Julliard committed
5807 5808 5809
    return 0;
}

5810
static LRESULT
5811
TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5812
{
5813
    INT nHit;
5814 5815
    NMMOUSE nmmouse;
    POINT pt;
5816
    BOOL button;
5817

5818 5819
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5820

5821
    nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5822
    nmmouse.dwHitInfo = nHit;
5823

5824
    if (!button) {
5825 5826 5827 5828 5829 5830
	nmmouse.dwItemSpec = -1;
    } else {
	nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
	nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
    }

5831
    ClientToScreen(infoPtr->hwndSelf, &pt);
5832
    nmmouse.pt = pt;
5833

5834
    if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5835
        return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5836 5837 5838 5839

    return 0;
}

5840
static LRESULT
5841
TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5842
{
5843
    NMHDR nmhdr;
5844

5845
    if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5846
        return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5847 5848 5849 5850

    return 0;
}

5851
static LRESULT
5852
TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864
{
    TBUTTON_INFO *btnPtr;

    infoPtr->bCaptured = FALSE;

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

        infoPtr->nOldHit = -1;

5865
        if (btnPtr->fsState & TBSTATE_ENABLED)
5866
            InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5867 5868 5869 5870
    }
    return 0;
}

5871
static LRESULT
5872
TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5873
{
5874 5875
    /* don't remove hot effects when in anchor highlighting mode or when a
     * drop-down button is pressed */
5876 5877 5878 5879 5880 5881
    if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
    {
        TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
        if (!hotBtnPtr->bDropDownPressed)
            TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
    }
5882

5883 5884 5885
    if (infoPtr->nOldHit < 0)
      return TRUE;

5886 5887 5888 5889
    /* If the last button we were over is depressed then make it not */
    /* depressed and redraw it */
    if(infoPtr->nOldHit == infoPtr->nButtonDown)
    {
5890 5891 5892
      TBUTTON_INFO *btnPtr;
      RECT rc1;

5893 5894 5895 5896
      btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];

      btnPtr->fsState &= ~TBSTATE_PRESSED;

5897
      rc1 = btnPtr->rect;
5898
      InflateRect (&rc1, 1, 1);
5899
      InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5900 5901
    }

5902 5903 5904 5905 5906 5907 5908 5909 5910
    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;
    }

5911 5912 5913 5914
    infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */

    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5915 5916

static LRESULT
5917
TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5918
{
5919
    POINT pt;
5920
    TRACKMOUSEEVENT trackinfo;
5921 5922
    INT   nHit;
    TBUTTON_INFO *btnPtr;
5923
    BOOL button;
5924
    
5925 5926 5927
    if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
        TOOLBAR_TooltipCreateControl(infoPtr);
    
5928
    if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5929 5930 5931 5932 5933 5934
        /* 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);
5935

5936
        /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5937
        if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5938
            trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5939
            trackinfo.hwndTrack = infoPtr->hwndSelf;
5940

5941 5942 5943
            /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
            /* and can properly deactivate the hot toolbar button */
            _TrackMouseEvent(&trackinfo);
5944
        }
5945
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
5946

Alexandre Julliard's avatar
Alexandre Julliard committed
5947
    if (infoPtr->hwndToolTip)
5948
	TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
Alexandre Julliard's avatar
Alexandre Julliard committed
5949 5950
			    WM_MOUSEMOVE, wParam, lParam);

5951 5952
    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
5953

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

5956
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
5957
        && (!infoPtr->bAnchor || button))
5958
        TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5959

5960 5961
    if (infoPtr->nOldHit != nHit)
    {
5962 5963
        if (infoPtr->bCaptured)
        {
5964 5965 5966 5967 5968 5969 5970 5971 5972
            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;
            }

5973 5974 5975
            btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
            if (infoPtr->nOldHit == infoPtr->nButtonDown) {
                btnPtr->fsState &= ~TBSTATE_PRESSED;
5976
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5977 5978 5979
            }
            else if (nHit == infoPtr->nButtonDown) {
                btnPtr->fsState |= TBSTATE_PRESSED;
5980
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5981 5982 5983
            }
            infoPtr->nOldHit = nHit;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
5984
    }
5985

Alexandre Julliard's avatar
Alexandre Julliard committed
5986 5987 5988 5989
    return 0;
}


5990
static inline LRESULT
5991
TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
5992
{
5993
/*    if (wndPtr->dwStyle & CCS_NODIVIDER) */
5994
	return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5995 5996
/*    else */
/*	return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
Alexandre Julliard's avatar
Alexandre Julliard committed
5997
}
Alexandre Julliard's avatar
Alexandre Julliard committed
5998 5999


6000
static inline LRESULT
6001
TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6002
{
6003
    if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6004
	((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
6005

6006
    return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6007
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6008 6009 6010


static LRESULT
6011
TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
6012
{
Alexandre Julliard's avatar
Alexandre Julliard committed
6013
    TOOLBAR_INFO *infoPtr;
6014
    DWORD styleadd = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
6015

Alexandre Julliard's avatar
Alexandre Julliard committed
6016
    /* allocate memory for info structure */
6017
    infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6018
    SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6019

Alexandre Julliard's avatar
Alexandre Julliard committed
6020
    /* paranoid!! */
Alexandre Julliard's avatar
Alexandre Julliard committed
6021
    infoPtr->dwStructSize = sizeof(TBBUTTON);
6022
    infoPtr->nRows = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
6023

6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040
    /* 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;
6041 6042
    infoPtr->hwndNotify = lpcs->hwndParent;
    infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053
    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);
6054
    infoPtr->dwStyle = lpcs->style;
6055 6056
    infoPtr->tbim.iButton = -1;

Alexandre Julliard's avatar
Alexandre Julliard committed
6057
    /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6058 6059 6060
    if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
        HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
	SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6061
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6062

6063 6064 6065 6066 6067
    /* 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!!!!
6068
     * Somehow, the only cases of this seem to be MFC programs.
6069 6070 6071 6072 6073 6074
     *
     * Note also that the addition of _TRANSPARENT occurs *only* here. It
     * does not occur in the WM_STYLECHANGING routine.
     *    (Guy Albertelli   9/2001)
     *
     */
6075
    if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
6076
        && !(lpcs->style & TBSTYLE_TRANSPARENT))
6077
	styleadd |= TBSTYLE_TRANSPARENT;
6078
    if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6079
	styleadd |= CCS_TOP;   /* default to top */
6080
	SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6081 6082
    }

6083
    return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
Alexandre Julliard's avatar
Alexandre Julliard committed
6084 6085 6086 6087
}


static LRESULT
6088
TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6089
{
6090
    DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6091
    RECT rcWindow;
6092
    HDC hdc;
Alexandre Julliard's avatar
Alexandre Julliard committed
6093

6094 6095
    if (dwStyle & WS_MINIMIZE)
	return 0; /* Nothing to do */
Alexandre Julliard's avatar
Alexandre Julliard committed
6096

6097
    DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6098

6099
    if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
Alexandre Julliard's avatar
Alexandre Julliard committed
6100 6101
	return 0;

6102 6103 6104 6105
    if (!(dwStyle & CCS_NODIVIDER))
    {
	GetWindowRect (hwnd, &rcWindow);
	OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6106
	if( dwStyle & WS_BORDER )
6107
	    InflateRect (&rcWindow, -1, -1);
6108 6109
	DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6110

6111
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
6112 6113 6114 6115 6116

    return 0;
}


6117 6118
/* 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
6119
{
6120
    int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
6121

6122
    TRACE("button index = %d\n", index);
6123

6124
    Free (infoPtr->pszTooltipText);
6125
    infoPtr->pszTooltipText = NULL;
6126

6127 6128
    if (index < 0)
        return 0;
6129

6130
    if (infoPtr->bUnicode)
6131 6132 6133
    {
        WCHAR wszBuffer[INFOTIPSIZE+1];
        NMTBGETINFOTIPW tbgit;
6134
        unsigned int len; /* in chars */
6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153

        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 */
6154
            infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166
            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;
        }
6167
    }
6168 6169 6170 6171
    else
    {
        CHAR szBuffer[INFOTIPSIZE+1];
        NMTBGETINFOTIPA tbgit;
6172
        unsigned int len; /* in chars */
6173

6174 6175
        szBuffer[0] = '\0';
        szBuffer[INFOTIPSIZE] = '\0';
6176

6177 6178 6179 6180
        tbgit.pszText = szBuffer;
        tbgit.cchTextMax = INFOTIPSIZE;
        tbgit.iItem = lpnmtdi->hdr.idFrom;
        tbgit.lParam = infoPtr->buttons[index].dwData;
Alexandre Julliard's avatar
Alexandre Julliard committed
6181

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

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

6186 6187
        len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
        if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6188 6189 6190 6191
        {
            /* need to allocate temporary buffer in infoPtr as there
             * isn't enough space in buffer passed to us by the
             * tooltip control */
6192
            infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6193 6194
            if (infoPtr->pszTooltipText)
            {
6195
                MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6196 6197 6198 6199
                lpnmtdi->lpszText = infoPtr->pszTooltipText;
                return 0;
            }
        }
6200
        else if (tbgit.pszText && tbgit.pszText[0])
6201
        {
6202 6203
            MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
                                lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6204 6205 6206
            return 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6207

6208 6209 6210 6211 6212 6213
    /* 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]);
6214
        unsigned int len = pszText ? strlenW(pszText) : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
6215

6216 6217 6218 6219 6220 6221 6222
        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 */
6223
            infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236
            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
6237

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

6240 6241
    /* last resort: send notification on to app */
    /* FIXME: find out what is really used here */
6242
    return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6243 6244 6245
}


6246
static inline LRESULT
6247
TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6248 6249 6250 6251 6252
{
    switch (lpnmh->code)
    {
    case PGN_CALCSIZE:
    {
6253
        LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265

        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
6266 6267
    }

6268 6269
    case PGN_SCROLL:
    {
6270
        LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6271 6272 6273 6274 6275 6276 6277 6278 6279

        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:
6280
        return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6281 6282

    case TTN_GETDISPINFOA:
6283
        FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6284 6285 6286 6287 6288
        return 0;

    default:
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
6289 6290 6291
}


6292
static LRESULT
6293
TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6294
{
6295
    LRESULT format;
6296

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

6299
    if (lParam == NF_QUERY)
6300 6301
        return NFR_UNICODE;

6302
    if (lParam == NF_REQUERY) {
6303
	format = SendMessageW(infoPtr->hwndNotify,
6304
			 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6305 6306 6307 6308
	if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
	    ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
		format);
	    format = NFR_ANSI;
6309
	}
6310
	return format;
6311
    }
6312
    return 0;
6313 6314 6315
}


Alexandre Julliard's avatar
Alexandre Julliard committed
6316
static LRESULT
6317
TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6318
{
6319 6320
    HDC hdc;
    PAINTSTRUCT ps;
Alexandre Julliard's avatar
Alexandre Julliard committed
6321

6322
    /* fill ps.rcPaint with a default rect */
6323
    ps.rcPaint = infoPtr->rcBound;
6324

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

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

6329 6330
    TOOLBAR_Refresh (infoPtr, hdc, &ps);
    if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6331

Alexandre Julliard's avatar
Alexandre Julliard committed
6332 6333 6334 6335
    return 0;
}


6336
static LRESULT
6337
TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6338 6339 6340 6341 6342 6343 6344 6345 6346 6347
{
    TRACE("nHotItem = %d\n", infoPtr->nHotItem);

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

    return 0;
}

6348
static LRESULT
6349
TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6350
{
6351
    TRACE("font=%p redraw=%d\n", hFont, Redraw);
6352
    
6353
    if (hFont == 0)
6354 6355
        infoPtr->hFont = infoPtr->hDefaultFont;
    else
6356
        infoPtr->hFont = hFont;
6357

6358
    TOOLBAR_CalcToolbar(infoPtr);
6359

6360
    if (Redraw)
6361
        InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6362 6363
    return 1;
}
6364

6365
static LRESULT
6366
TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6367 6368 6369 6370 6371 6372
     /*****************************************************
      *
      * Function;
      *  Handles the WM_SETREDRAW message.
      *
      * Documentation:
6373
      *  According to testing V4.71 of COMCTL32 returns the
6374 6375
      *  *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
6376
      *  (For laughs see the "consistency" with same function
6377 6378 6379 6380 6381 6382
      *   in rebar.)
      *
      *****************************************************/
{
    BOOL oldredraw = infoPtr->bDoRedraw;

6383
    TRACE("set to %s\n",
6384 6385 6386 6387 6388 6389 6390 6391 6392
	  (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
6393
static LRESULT
6394
TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
6395
{
6396
    TRACE("sizing toolbar\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
6397

6398 6399 6400 6401 6402 6403
    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;
6404

6405
        GetClientRect(infoPtr->hwndSelf, &client);
6406
        if(client.right > infoPtr->client_rect.right)
6407
        {
6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424
            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;
6425 6426
        }

6427 6428 6429 6430 6431 6432 6433 6434
        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))
6435
                InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6436
    }
6437 6438
    GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
    TOOLBAR_AutoSize(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6439 6440
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6441 6442


Alexandre Julliard's avatar
Alexandre Julliard committed
6443
static LRESULT
6444
TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
6445
{
6446
    if (nType == GWL_STYLE)
6447
        return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6448

Alexandre Julliard's avatar
Alexandre Julliard committed
6449 6450
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
6451 6452


6453
static LRESULT
6454
TOOLBAR_SysColorChange (void)
6455 6456 6457 6458 6459 6460 6461
{
    COMCTL32_RefreshSysColors();

    return 0;
}


6462 6463 6464 6465 6466 6467 6468 6469 6470
/* 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
6471

6472
static LRESULT WINAPI
6473
ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
6474
{
6475
    TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6476

6477
    TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6478 6479
	  hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);

6480
    if (!infoPtr && (uMsg != WM_NCCREATE))
6481
	return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6482

Alexandre Julliard's avatar
Alexandre Julliard committed
6483 6484 6485
    switch (uMsg)
    {
	case TB_ADDBITMAP:
6486
	    return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6487

6488
	case TB_ADDBUTTONSA:
6489
	case TB_ADDBUTTONSW:
6490 6491
	    return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
	                                uMsg == TB_ADDBUTTONSW);
6492
	case TB_ADDSTRINGA:
6493
	    return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6494

6495
	case TB_ADDSTRINGW:
6496
	    return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6497 6498

	case TB_AUTOSIZE:
6499
	    return TOOLBAR_AutoSize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6500 6501

	case TB_BUTTONCOUNT:
6502
	    return TOOLBAR_ButtonCount (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6503 6504

	case TB_BUTTONSTRUCTSIZE:
6505
	    return TOOLBAR_ButtonStructSize (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6506 6507

	case TB_CHANGEBITMAP:
6508
	    return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6509

Alexandre Julliard's avatar
Alexandre Julliard committed
6510
	case TB_CHECKBUTTON:
6511
	    return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6512 6513

	case TB_COMMANDTOINDEX:
6514
	    return TOOLBAR_CommandToIndex (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6515

Alexandre Julliard's avatar
Alexandre Julliard committed
6516
	case TB_CUSTOMIZE:
6517
	    return TOOLBAR_Customize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6518 6519

	case TB_DELETEBUTTON:
6520
	    return TOOLBAR_DeleteButton (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6521 6522

	case TB_ENABLEBUTTON:
6523
	    return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6524

6525
	case TB_GETANCHORHIGHLIGHT:
6526
	    return TOOLBAR_GetAnchorHighlight (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6527 6528

	case TB_GETBITMAP:
6529
	    return TOOLBAR_GetBitmap (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6530

Alexandre Julliard's avatar
Alexandre Julliard committed
6531
	case TB_GETBITMAPFLAGS:
6532
	    return TOOLBAR_GetBitmapFlags ();
Alexandre Julliard's avatar
Alexandre Julliard committed
6533 6534

	case TB_GETBUTTON:
6535
	    return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6536

6537
	case TB_GETBUTTONINFOA:
6538
	case TB_GETBUTTONINFOW:
6539 6540
	    return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
	                                   uMsg == TB_GETBUTTONINFOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
6541
	case TB_GETBUTTONSIZE:
6542
	    return TOOLBAR_GetButtonSize (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6543

6544
	case TB_GETBUTTONTEXTA:
6545
	case TB_GETBUTTONTEXTW:
6546 6547
	    return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
	                                  uMsg == TB_GETBUTTONTEXTW);
6548

Alexandre Julliard's avatar
Alexandre Julliard committed
6549
	case TB_GETDISABLEDIMAGELIST:
6550
	    return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6551

Alexandre Julliard's avatar
Alexandre Julliard committed
6552
	case TB_GETEXTENDEDSTYLE:
6553
	    return TOOLBAR_GetExtendedStyle (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6554 6555

	case TB_GETHOTIMAGELIST:
6556
	    return TOOLBAR_GetHotImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6557

6558
	case TB_GETHOTITEM:
6559
	    return TOOLBAR_GetHotItem (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6560 6561

	case TB_GETIMAGELIST:
6562
	    return TOOLBAR_GetDefImageList (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6563

6564
	case TB_GETINSERTMARK:
6565
	    return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6566 6567

	case TB_GETINSERTMARKCOLOR:
6568
	    return TOOLBAR_GetInsertMarkColor (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6569 6570

	case TB_GETITEMRECT:
6571
	    return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6572

Alexandre Julliard's avatar
Alexandre Julliard committed
6573
	case TB_GETMAXSIZE:
6574
	    return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6575

6576
/*	case TB_GETOBJECT:			*/ /* 4.71 */
6577 6578

	case TB_GETPADDING:
6579
	    return TOOLBAR_GetPadding (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6580 6581

	case TB_GETRECT:
6582
	    return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6583 6584

	case TB_GETROWS:
6585
	    return TOOLBAR_GetRows (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6586 6587

	case TB_GETSTATE:
6588
	    return TOOLBAR_GetState (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6589

Robert Shearman's avatar
Robert Shearman committed
6590
	case TB_GETSTRINGA:
6591
            return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
Robert Shearman's avatar
Robert Shearman committed
6592 6593

	case TB_GETSTRINGW:
6594
	    return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
Robert Shearman's avatar
Robert Shearman committed
6595

Alexandre Julliard's avatar
Alexandre Julliard committed
6596
	case TB_GETSTYLE:
6597
	    return TOOLBAR_GetStyle (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6598

Alexandre Julliard's avatar
Alexandre Julliard committed
6599
	case TB_GETTEXTROWS:
6600
	    return TOOLBAR_GetTextRows (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6601 6602

	case TB_GETTOOLTIPS:
6603
	    return TOOLBAR_GetToolTips (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6604

Alexandre Julliard's avatar
Alexandre Julliard committed
6605
	case TB_GETUNICODEFORMAT:
6606
	    return TOOLBAR_GetUnicodeFormat (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6607 6608

	case TB_HIDEBUTTON:
6609
	    return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6610 6611

	case TB_HITTEST:
6612
	    return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6613

Alexandre Julliard's avatar
Alexandre Julliard committed
6614
	case TB_INDETERMINATE:
6615
	    return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6616

6617
	case TB_INSERTBUTTONA:
6618
	case TB_INSERTBUTTONW:
6619 6620
	    return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
	                                 uMsg == TB_INSERTBUTTONW);
6621

6622
/*	case TB_INSERTMARKHITTEST:		*/ /* 4.71 */
Alexandre Julliard's avatar
Alexandre Julliard committed
6623 6624

	case TB_ISBUTTONCHECKED:
6625
	    return TOOLBAR_IsButtonChecked (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6626 6627

	case TB_ISBUTTONENABLED:
6628
	    return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6629 6630

	case TB_ISBUTTONHIDDEN:
6631
	    return TOOLBAR_IsButtonHidden (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6632 6633

	case TB_ISBUTTONHIGHLIGHTED:
6634
	    return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6635 6636

	case TB_ISBUTTONINDETERMINATE:
6637
	    return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6638 6639

	case TB_ISBUTTONPRESSED:
6640
	    return TOOLBAR_IsButtonPressed (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6641

6642
	case TB_LOADIMAGES:
6643
	    return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6644 6645 6646

	case TB_MAPACCELERATORA:
	case TB_MAPACCELERATORW:
6647
	    return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6648 6649

	case TB_MARKBUTTON:
6650
	    return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6651

Robert Shearman's avatar
Robert Shearman committed
6652
	case TB_MOVEBUTTON:
6653
	    return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6654 6655

	case TB_PRESSBUTTON:
6656
	    return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
Alexandre Julliard's avatar
Alexandre Julliard committed
6657

6658
	case TB_REPLACEBITMAP:
6659
            return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6660

6661
	case TB_SAVERESTOREA:
6662
	    return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6663

6664
	case TB_SAVERESTOREW:
6665
	    return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6666 6667

	case TB_SETANCHORHIGHLIGHT:
6668
	    return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6669 6670

	case TB_SETBITMAPSIZE:
6671
	    return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6672

6673
	case TB_SETBUTTONINFOA:
6674
	case TB_SETBUTTONINFOW:
6675 6676
	    return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
                                          uMsg == TB_SETBUTTONINFOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
6677
	case TB_SETBUTTONSIZE:
6678
	    return TOOLBAR_SetButtonSize (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6679

Alexandre Julliard's avatar
Alexandre Julliard committed
6680
	case TB_SETBUTTONWIDTH:
6681
	    return TOOLBAR_SetButtonWidth (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6682 6683

	case TB_SETCMDID:
6684
	    return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6685

Alexandre Julliard's avatar
Alexandre Julliard committed
6686
	case TB_SETDISABLEDIMAGELIST:
6687
	    return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6688

Alexandre Julliard's avatar
Alexandre Julliard committed
6689
	case TB_SETDRAWTEXTFLAGS:
6690
	    return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6691 6692

	case TB_SETEXTENDEDSTYLE:
6693
	    return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6694 6695

	case TB_SETHOTIMAGELIST:
6696
	    return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6697

6698
	case TB_SETHOTITEM:
6699
	    return TOOLBAR_SetHotItem (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6700 6701

	case TB_SETIMAGELIST:
6702
	    return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6703 6704

	case TB_SETINDENT:
6705
	    return TOOLBAR_SetIndent (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6706

6707
	case TB_SETINSERTMARK:
6708
	    return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6709 6710

	case TB_SETINSERTMARKCOLOR:
6711
	    return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6712

Alexandre Julliard's avatar
Alexandre Julliard committed
6713
	case TB_SETMAXTEXTROWS:
6714
	    return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6715

6716
	case TB_SETPADDING:
6717
	    return TOOLBAR_SetPadding (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6718 6719

	case TB_SETPARENT:
6720
	    return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6721 6722

	case TB_SETROWS:
6723
	    return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6724 6725

	case TB_SETSTATE:
6726
	    return TOOLBAR_SetState (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6727 6728

	case TB_SETSTYLE:
6729
	    return TOOLBAR_SetStyle (infoPtr, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6730

Alexandre Julliard's avatar
Alexandre Julliard committed
6731
	case TB_SETTOOLTIPS:
6732
	    return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6733

Alexandre Julliard's avatar
Alexandre Julliard committed
6734
	case TB_SETUNICODEFORMAT:
6735
	    return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6736

6737 6738
	case TB_SETBOUNDINGSIZE:
	    return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6739

6740
	case TB_SETHOTITEM2:
6741
	    return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6742

6743
	case TB_SETLISTGAP:
6744
	    return TOOLBAR_SetListGap(infoPtr, wParam);
6745

6746
	case TB_GETIMAGELISTCOUNT:
6747
	    return TOOLBAR_GetImageListCount(infoPtr);
Robert Shearman's avatar
Robert Shearman committed
6748

6749
	case TB_GETIDEALSIZE:
6750
	    return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6751

6752 6753
	case TB_UNKWN464:
	    return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6754 6755 6756 6757 6758

/* Common Control Messages */

/*	case TB_GETCOLORSCHEME:			*/ /* identical to CCM_ */
	case CCM_GETCOLORSCHEME:
6759
	    return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6760 6761 6762

/*	case TB_SETCOLORSCHEME:			*/ /* identical to CCM_ */
	case CCM_SETCOLORSCHEME:
6763
	    return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6764 6765

	case CCM_GETVERSION:
6766
	    return TOOLBAR_GetVersion (infoPtr);
6767

6768
	case CCM_SETVERSION:
6769
	    return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6770

Alexandre Julliard's avatar
Alexandre Julliard committed
6771

6772
/*	case WM_CHAR: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6773

Alexandre Julliard's avatar
Alexandre Julliard committed
6774
	case WM_CREATE:
6775
	    return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6776

6777
	case WM_DESTROY:
6778
	  return TOOLBAR_Destroy (infoPtr);
6779

Alexandre Julliard's avatar
Alexandre Julliard committed
6780
	case WM_ERASEBKGND:
6781
	    return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6782

Eric Pouech's avatar
Eric Pouech committed
6783
	case WM_GETFONT:
6784
		return TOOLBAR_GetFont (infoPtr);
Eric Pouech's avatar
Eric Pouech committed
6785

6786
	case WM_KEYDOWN:
6787
	    return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6788

6789
/*	case WM_KILLFOCUS: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6790

Alexandre Julliard's avatar
Alexandre Julliard committed
6791
	case WM_LBUTTONDBLCLK:
6792
	    return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6793 6794

	case WM_LBUTTONDOWN:
6795
	    return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6796 6797

	case WM_LBUTTONUP:
6798
	    return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6799

6800
	case WM_RBUTTONUP:
6801
	    return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6802

6803
	case WM_RBUTTONDBLCLK:
6804
	    return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6805

Alexandre Julliard's avatar
Alexandre Julliard committed
6806
	case WM_MOUSEMOVE:
6807
	    return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6808

6809
	case WM_MOUSELEAVE:
6810
	    return TOOLBAR_MouseLeave (infoPtr);
6811

6812
	case WM_CAPTURECHANGED:
6813
	    if (hwnd == (HWND)lParam) return 0;
6814
	    return TOOLBAR_CaptureChanged(infoPtr);
6815

Alexandre Julliard's avatar
Alexandre Julliard committed
6816
	case WM_NCACTIVATE:
6817
	    return TOOLBAR_NCActivate (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6818 6819

	case WM_NCCALCSIZE:
6820
	    return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6821

6822
	case WM_NCCREATE:
6823
	    return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6824

Alexandre Julliard's avatar
Alexandre Julliard committed
6825
	case WM_NCPAINT:
6826
	    return TOOLBAR_NCPaint (hwnd, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6827

Alexandre Julliard's avatar
Alexandre Julliard committed
6828
	case WM_NOTIFY:
6829
	    return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6830

6831
	case WM_NOTIFYFORMAT:
6832
	    return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6833

6834
	case WM_PRINTCLIENT:
Alexandre Julliard's avatar
Alexandre Julliard committed
6835
	case WM_PAINT:
6836
	    return TOOLBAR_Paint (infoPtr, wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6837

6838
	case WM_SETFOCUS:
6839
	    return TOOLBAR_SetFocus (infoPtr);
6840

6841
	case WM_SETFONT:
6842
            return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6843

6844
	case WM_SETREDRAW:
6845
	    return TOOLBAR_SetRedraw (infoPtr, wParam);
6846

Alexandre Julliard's avatar
Alexandre Julliard committed
6847
	case WM_SIZE:
6848
	    return TOOLBAR_Size (infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
6849 6850

	case WM_STYLECHANGED:
6851
	    return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6852

6853
	case WM_SYSCOLORCHANGE:
6854
	    return TOOLBAR_SysColorChange ();
6855 6856 6857
            
        case WM_THEMECHANGED:
            return theme_changed (hwnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
6858

6859
/*	case WM_WININICHANGE: */
Alexandre Julliard's avatar
Alexandre Julliard committed
6860 6861 6862 6863 6864 6865

	case WM_CHARTOITEM:
	case WM_COMMAND:
	case WM_DRAWITEM:
	case WM_MEASUREITEM:
	case WM_VKEYTOITEM:
6866
            return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6867

6868 6869
	/* We see this in Outlook Express 5.x and just does DefWindowProc */
        case PGM_FORWARDMOUSE:
6870
	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6871

Alexandre Julliard's avatar
Alexandre Julliard committed
6872
	default:
6873
	    if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6874
		ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
6875
		     uMsg, wParam, lParam);
6876
	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
6877 6878 6879 6880
    }
}


6881
VOID
Patrik Stridvall's avatar
Patrik Stridvall committed
6882
TOOLBAR_Register (void)
Alexandre Julliard's avatar
Alexandre Julliard committed
6883
{
6884
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
6885

6886
    ZeroMemory (&wndClass, sizeof(WNDCLASSW));
Alexandre Julliard's avatar
Alexandre Julliard committed
6887
    wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
6888
    wndClass.lpfnWndProc   = ToolbarWindowProc;
Alexandre Julliard's avatar
Alexandre Julliard committed
6889 6890
    wndClass.cbClsExtra    = 0;
    wndClass.cbWndExtra    = sizeof(TOOLBAR_INFO *);
6891 6892 6893
    wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6894

6895
    RegisterClassW (&wndClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
6896
}
6897 6898 6899


VOID
Patrik Stridvall's avatar
Patrik Stridvall committed
6900
TOOLBAR_Unregister (void)
6901
{
6902
    UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6903
}
6904 6905 6906 6907 6908 6909 6910 6911 6912

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

6913 6914 6915 6916
    /* Don't add new entry for NULL imagelist */
    if (!c && !himl)
        return NULL;

6917 6918 6919 6920 6921
    /* If this is a new entry we must create it and insert into the array */
    if (!c)
    {
        PIMLENTRY *pnies;

6922
        c = Alloc(sizeof(IMLENTRY));
6923 6924
	c->id = id;

6925
	pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6926 6927 6928 6929
	memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
	pnies[*cies] = c;
	(*cies)++;

6930
	Free(*pies);
6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945
	*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++)
6946
	Free((*pies)[i]);
6947

6948
    Free(*pies);
6949 6950 6951 6952 6953 6954

    *cies = 0;
    *pies = NULL;
}


6955
static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976
{
    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;
}


6977
static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988
{
    HIMAGELIST himlDef = 0;
    PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);

    if (pie)
        himlDef = pie->himl;

    return himlDef;
}


6989
static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6990 6991
{
    if (infoPtr->bUnicode)
6992
        return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003
    else
    {
        CHAR Buffer[256];
        NMTOOLBARA nmtba;
        BOOL bRet = FALSE;

        nmtba.iItem = nmtb->iItem;
        nmtba.pszText = Buffer;
        nmtba.cchText = 256;
        ZeroMemory(nmtba.pszText, nmtba.cchText);

7004
        if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7005 7006 7007
        {
            int ccht = strlen(nmtba.pszText);
            if (ccht)
7008
               MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7009 7010
                  nmtb->pszText, nmtb->cchText);

7011
            nmtb->tbButton = nmtba.tbButton;
7012 7013 7014 7015 7016 7017 7018 7019
            bRet = TRUE;
        }

        return bRet;
    }
}


7020
static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7021
{
7022
    NMTOOLBARW nmtb;
7023

7024 7025
    /* MSDN states that iItem is the index of the button, rather than the
     * command ID as used by every other NMTOOLBAR notification */
7026 7027 7028
    nmtb.iItem = iItem;
    memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));

7029
    return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);
7030
}