kernel32: FindExSearchLimitToDirectories has no effect on FindFirstFileEx.
[wine] / dlls / comctl32 / toolbar.c
1 /*
2  * Toolbar control
3  *
4  * Copyright 1998,1999 Eric Kohl
5  * Copyright 2000 Eric Kohl for CodeWeavers
6  * Copyright 2004 Robert Shearman
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * 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  * NOTES
23  *
24  * This code was audited for completeness against the documented features
25  * of Comctl32.dll version 6.0 on Mar. 14, 2004, by Robert Shearman.
26  * 
27  * Unless otherwise noted, we believe this code to be complete, as per
28  * the specification mentioned above.
29  * If you discover missing features or bugs please note them below.
30  * 
31  * TODO:
32  *   - Styles:
33  *     - TBSTYLE_REGISTERDROP
34  *     - TBSTYLE_EX_DOUBLEBUFFER
35  *   - Messages:
36  *     - TB_GETMETRICS
37  *     - TB_GETOBJECT
38  *     - TB_INSERTMARKHITTEST
39  *     - TB_SAVERESTORE
40  *     - TB_SETMETRICS
41  *     - WM_WININICHANGE
42  *   - Notifications:
43  *     - NM_CHAR
44  *     - TBN_GETOBJECT
45  *     - TBN_SAVE
46  *   - Button wrapping (under construction).
47  *   - Fix TB_SETROWS and Separators.
48  *   - iListGap custom draw support.
49  *
50  * Testing:
51  *   - Run tests using Waite Group Windows95 API Bible Volume 2.
52  *     The second cdrom contains executables addstr.exe, btncount.exe,
53  *     btnstate.exe, butstrsz.exe, chkbtn.exe, chngbmp.exe, customiz.exe,
54  *     enablebtn.exe, getbmp.exe, getbtn.exe, getflags.exe, hidebtn.exe,
55  *     indetbtn.exe, insbtn.exe, pressbtn.exe, setbtnsz.exe, setcmdid.exe,
56  *     setparnt.exe, setrows.exe, toolwnd.exe.
57  *   - Microsoft's controlspy examples.
58  *   - Charles Petzold's 'Programming Windows': gadgets.exe
59  *
60  *  Differences between MSDN and actual native control operation:
61  *   1. MSDN says: "TBSTYLE_LIST: Creates a flat toolbar with button text
62  *                  to the right of the bitmap. Otherwise, this style is
63  *                  identical to TBSTYLE_FLAT."
64  *      As implemented by both v4.71 and v5.80 of the native COMCTL32.DLL
65  *      you can create a TBSTYLE_LIST without TBSTYLE_FLAT and the result
66  *      is non-flat non-transparent buttons. Therefore TBSTYLE_LIST does
67  *      *not* imply TBSTYLE_FLAT as documented.  (GA 8/2001)
68  *
69  */
70
71 #include <stdarg.h>
72 #include <string.h>
73
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"
81 #include "commctrl.h"
82 #include "comctl32.h"
83 #include "uxtheme.h"
84 #include "tmschema.h"
85 #include "wine/debug.h"
86
87 WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
88
89 static HCURSOR hCursorDrag = NULL;
90
91 typedef struct
92 {
93     INT iBitmap;
94     INT idCommand;
95     BYTE  fsState;
96     BYTE  fsStyle;
97     BYTE  bHot;
98     BYTE  bDropDownPressed;
99     DWORD_PTR dwData;
100     INT_PTR iString;
101     INT nRow;
102     RECT rect;
103     INT cx; /* manually set size */
104 } TBUTTON_INFO;
105
106 typedef struct
107 {
108     UINT nButtons;
109     HINSTANCE hInst;
110     UINT nID;
111 } TBITMAP_INFO;
112
113 typedef struct
114 {
115     HIMAGELIST himl;
116     INT id;
117 } IMLENTRY, *PIMLENTRY;
118
119 typedef struct
120 {
121     DWORD    dwStructSize;    /* size of TBBUTTON struct */
122     INT      nWidth;          /* width of the toolbar */
123     RECT     client_rect;
124     RECT     rcBound;         /* bounding rectangle */
125     INT      nButtonHeight;
126     INT      nButtonWidth;
127     INT      nBitmapHeight;
128     INT      nBitmapWidth;
129     INT      nIndent;
130     INT      nRows;           /* number of button rows */
131     INT      nMaxTextRows;    /* maximum number of text rows */
132     INT      cxMin;           /* minimum button width */
133     INT      cxMax;           /* maximum button width */
134     INT      nNumButtons;     /* number of buttons */
135     INT      nNumBitmaps;     /* number of bitmaps */
136     INT      nNumStrings;     /* number of strings */
137     INT      nNumBitmapInfos;
138     INT      nButtonDown;     /* toolbar button being pressed or -1 if none */
139     INT      nButtonDrag;     /* toolbar button being dragged or -1 if none */
140     INT      nOldHit;
141     INT      nHotItem;        /* index of the "hot" item */
142     SIZE     szPadding;       /* padding values around button */
143     INT      iTopMargin;      /* the top margin */
144     INT      iListGap;        /* default gap between text and image for toolbar with list style */
145     HFONT    hDefaultFont;
146     HFONT    hFont;           /* text font */
147     HIMAGELIST himlInt;       /* image list created internally */
148     PIMLENTRY *himlDef;       /* default image list array */
149     INT       cimlDef;        /* default image list array count */
150     PIMLENTRY *himlHot;       /* hot image list array */
151     INT       cimlHot;        /* hot image list array count */
152     PIMLENTRY *himlDis;       /* disabled image list array */
153     INT       cimlDis;        /* disabled image list array count */
154     HWND     hwndToolTip;     /* handle to tool tip control */
155     HWND     hwndNotify;      /* handle to the window that gets notifications */
156     HWND     hwndSelf;        /* my own handle */
157     BOOL     bAnchor;         /* anchor highlight enabled */
158     BOOL     bDoRedraw;       /* Redraw status */
159     BOOL     bDragOutSent;    /* has TBN_DRAGOUT notification been sent for this drag? */
160     BOOL     bUnicode;        /* Notifications are ASCII (FALSE) or Unicode (TRUE)? */
161     BOOL     bCaptured;       /* mouse captured? */
162     DWORD      dwStyle;       /* regular toolbar style */
163     DWORD      dwExStyle;     /* extended toolbar style */
164     DWORD      dwDTFlags;     /* DrawText flags */
165
166     COLORREF   clrInsertMark;   /* insert mark color */
167     COLORREF   clrBtnHighlight; /* color for Flat Separator */
168     COLORREF   clrBtnShadow;    /* color for Flag Separator */
169     INT      iVersion;
170     LPWSTR   pszTooltipText;    /* temporary store for a string > 80 characters
171                                  * for TTN_GETDISPINFOW notification */
172     TBINSERTMARK  tbim;         /* info on insertion mark */
173     TBUTTON_INFO *buttons;      /* pointer to button array */
174     LPWSTR       *strings;      /* pointer to string array */
175     TBITMAP_INFO *bitmaps;
176 } TOOLBAR_INFO, *PTOOLBAR_INFO;
177
178
179 /* used by customization dialog */
180 typedef struct
181 {
182     PTOOLBAR_INFO tbInfo;
183     HWND          tbHwnd;
184 } CUSTDLG_INFO, *PCUSTDLG_INFO;
185
186 typedef struct
187 {
188     TBBUTTON btn;
189     BOOL     bVirtual;
190     BOOL     bRemovable;
191     WCHAR    text[64];
192 } CUSTOMBUTTON, *PCUSTOMBUTTON;
193
194 typedef enum
195 {
196     IMAGE_LIST_DEFAULT,
197     IMAGE_LIST_HOT,
198     IMAGE_LIST_DISABLED
199 } IMAGE_LIST_TYPE;
200
201 #define SEPARATOR_WIDTH    8
202 #define TOP_BORDER         2
203 #define BOTTOM_BORDER      2
204 #define DDARROW_WIDTH      11
205 #define ARROW_HEIGHT       3
206 #define INSERTMARK_WIDTH   2
207
208 #define DEFPAD_CX 7
209 #define DEFPAD_CY 6
210 #define DEFLISTGAP 4
211
212 /* vertical padding used in list mode when image is present */
213 #define LISTPAD_CY 9
214
215 /* how wide to treat the bitmap if it isn't present */
216 #define NONLIST_NOTEXT_OFFSET 2
217
218 #define TOOLBAR_NOWHERE (-1)
219
220 #define TOOLBAR_GetInfoPtr(hwnd) ((TOOLBAR_INFO *)GetWindowLongPtrW(hwnd,0))
221 #define TOOLBAR_HasText(x, y) (TOOLBAR_GetText(x, y) ? TRUE : FALSE)
222 #define TOOLBAR_HasDropDownArrows(exStyle) ((exStyle & TBSTYLE_EX_DRAWDDARROWS) ? TRUE : FALSE)
223
224 /* Used to find undocumented extended styles */
225 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
226                         TBSTYLE_EX_UNDOC1 | \
227                         TBSTYLE_EX_MIXEDBUTTONS | \
228                         TBSTYLE_EX_HIDECLIPPEDBUTTONS)
229
230 /* all of the CCS_ styles */
231 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
232                        CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
233
234 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
235 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
236 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
237 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
238 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
239
240 static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };
241
242 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
243 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, PCUSTOMBUTTON btnInfo);
244 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
245 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
246 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
247 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
248 static LRESULT TOOLBAR_LButtonDown(HWND hwnd, WPARAM wParam, LPARAM lParam);
249 static void TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason);
250 static void TOOLBAR_LayoutToolbar(HWND hwnd);
251 static LRESULT TOOLBAR_AutoSize(HWND hwnd);
252 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
253 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
254
255 static LRESULT
256 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
257
258 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
259 {
260     return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
261 }
262
263 static LPWSTR
264 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
265 {
266     LPWSTR lpText = NULL;
267
268     /* NOTE: iString == -1 is undocumented */
269     if ((HIWORD(btnPtr->iString) != 0) && (btnPtr->iString != -1))
270         lpText = (LPWSTR)btnPtr->iString;
271     else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
272         lpText = infoPtr->strings[btnPtr->iString];
273
274     return lpText;
275 }
276
277 static void
278 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num, BOOL internal)
279 {
280     if (TRACE_ON(toolbar)){
281         TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
282               btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap), 
283               bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
284         TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
285         if (internal)
286             TRACE("button %d id %d, hot=%s, row=%d, rect=(%d,%d)-(%d,%d)\n",
287                   btn_num, bP->idCommand,
288                   (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
289                   bP->rect.left, bP->rect.top,
290                   bP->rect.right, bP->rect.bottom);
291     }
292 }
293
294
295 static void
296 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
297 {
298     if (TRACE_ON(toolbar)) {
299         INT i;
300
301         TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
302               iP->hwndSelf, line,
303               iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
304               iP->nNumStrings, iP->dwStyle);
305         TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
306               iP->hwndSelf, line,
307               iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
308               (iP->bDoRedraw) ? "TRUE" : "FALSE");
309         for(i=0; i<iP->nNumButtons; i++) {
310             TOOLBAR_DumpButton(iP, &iP->buttons[i], i, TRUE);
311         }
312     }
313 }
314
315
316 /***********************************************************************
317 *               TOOLBAR_CheckStyle
318 *
319 * This function validates that the styles set are implemented and
320 * issues FIXME's warning of possible problems. In a perfect world this
321 * function should be null.
322 */
323 static void
324 TOOLBAR_CheckStyle (HWND hwnd, DWORD dwStyle)
325 {
326     if (dwStyle & TBSTYLE_REGISTERDROP)
327         FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", hwnd);
328 }
329
330
331 static INT
332 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
333 {
334         if(!IsWindow(infoPtr->hwndSelf))
335             return 0;   /* we have just been destroyed */
336
337     nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
338     nmhdr->hwndFrom = infoPtr->hwndSelf;
339     nmhdr->code = code;
340
341     TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
342           (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
343
344     return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, 
345         (WPARAM)nmhdr->idFrom, (LPARAM)nmhdr);
346 }
347
348 /***********************************************************************
349 *               TOOLBAR_GetBitmapIndex
350 *
351 * This function returns the bitmap index associated with a button.
352 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
353 * is issued to retrieve the index.
354 */
355 static INT
356 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
357 {
358     INT ret = btnPtr->iBitmap;
359
360     if (ret == I_IMAGECALLBACK)
361     {
362         /* issue TBN_GETDISPINFO */
363         NMTBDISPINFOW nmgd;
364
365         memset(&nmgd, 0, sizeof(nmgd));
366         nmgd.idCommand = btnPtr->idCommand;
367         nmgd.lParam = btnPtr->dwData;
368         nmgd.dwMask = TBNF_IMAGE;
369         nmgd.iImage = -1;
370         /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
371         TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
372         if (nmgd.dwMask & TBNF_DI_SETITEM)
373             btnPtr->iBitmap = nmgd.iImage;
374         ret = nmgd.iImage;
375         TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
376             ret, nmgd.dwMask, infoPtr->nNumBitmaps);
377     }
378
379     if (ret != I_IMAGENONE)
380         ret = GETIBITMAP(infoPtr, ret);
381
382     return ret;
383 }
384
385
386 static BOOL
387 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
388 {
389     HIMAGELIST himl;
390     INT id = GETHIMLID(infoPtr, index);
391     INT iBitmap = GETIBITMAP(infoPtr, index);
392
393     if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
394         iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
395         (index == I_IMAGECALLBACK))
396       return TRUE;
397     else
398       return FALSE;
399 }
400
401
402 static inline BOOL
403 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
404 {
405     HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
406     return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
407 }
408
409
410 /***********************************************************************
411 *               TOOLBAR_GetImageListForDrawing
412 *
413 * This function validates the bitmap index (including I_IMAGECALLBACK
414 * functionality) and returns the corresponding image list.
415 */
416 static HIMAGELIST
417 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
418                                 IMAGE_LIST_TYPE imagelist, INT * index)
419 {
420     HIMAGELIST himl;
421
422     if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
423         if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
424         ERR("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
425             HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
426         return NULL;
427     }
428
429     if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
430         if ((*index == I_IMAGECALLBACK) ||
431             (*index == I_IMAGENONE)) return NULL;
432         ERR("TBN_GETDISPINFO returned invalid index %d\n",
433             *index);
434         return NULL;
435     }
436
437     switch(imagelist)
438     {
439     case IMAGE_LIST_DEFAULT:
440         himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
441         break;
442     case IMAGE_LIST_HOT:
443         himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
444         break;
445     case IMAGE_LIST_DISABLED:
446         himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
447         break;
448     default:
449         himl = NULL;
450         FIXME("Shouldn't reach here\n");
451     }
452
453     if (!himl)
454        TRACE("no image list\n");
455
456     return himl;
457 }
458
459
460 static void
461 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
462 {
463     RECT myrect;
464     COLORREF oldcolor, newcolor;
465
466     myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
467     myrect.right = myrect.left + 1;
468     myrect.top = lpRect->top + 2;
469     myrect.bottom = lpRect->bottom - 2;
470
471     newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
472                 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
473     oldcolor = SetBkColor (hdc, newcolor);
474     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
475
476     myrect.left = myrect.right;
477     myrect.right = myrect.left + 1;
478
479     newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
480                 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
481     SetBkColor (hdc, newcolor);
482     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
483
484     SetBkColor (hdc, oldcolor);
485 }
486
487
488 /***********************************************************************
489 *               TOOLBAR_DrawDDFlatSeparator
490 *
491 * This function draws the separator that was flagged as BTNS_DROPDOWN.
492 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
493 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
494 * are horizontal as opposed to the vertical separators for not dropdown
495 * type.
496 *
497 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
498 */
499 static void
500 TOOLBAR_DrawDDFlatSeparator (const RECT *lpRect, HDC hdc, const TBUTTON_INFO *btnPtr,
501                              const TOOLBAR_INFO *infoPtr)
502 {
503     RECT myrect;
504     COLORREF oldcolor, newcolor;
505
506     myrect.left = lpRect->left;
507     myrect.right = lpRect->right;
508     myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
509     myrect.bottom = myrect.top + 1;
510
511     InflateRect (&myrect, -2, 0);
512
513     TRACE("rect=(%d,%d)-(%d,%d)\n",
514           myrect.left, myrect.top, myrect.right, myrect.bottom);
515
516     newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
517                 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
518     oldcolor = SetBkColor (hdc, newcolor);
519     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
520
521     myrect.top = myrect.bottom;
522     myrect.bottom = myrect.top + 1;
523
524     newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
525                 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
526     SetBkColor (hdc, newcolor);
527     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
528
529     SetBkColor (hdc, oldcolor);
530 }
531
532
533 static void
534 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
535 {
536     INT x, y;
537     HPEN hPen, hOldPen;
538
539     if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
540     hOldPen = SelectObject ( hdc, hPen );
541     x = left + 2;
542     y = top;
543     MoveToEx (hdc, x, y, NULL);
544     LineTo (hdc, x+5, y++); x++;
545     MoveToEx (hdc, x, y, NULL);
546     LineTo (hdc, x+3, y++); x++;
547     MoveToEx (hdc, x, y, NULL);
548     LineTo (hdc, x+1, y++);
549     SelectObject( hdc, hOldPen );
550     DeleteObject( hPen );
551 }
552
553 /*
554  * Draw the text string for this button.
555  * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
556  *      is non-zero, so we can simply check himlDef to see if we have
557  *      an image list
558  */
559 static void
560 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
561                     const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
562 {
563     HDC hdc = tbcd->nmcd.hdc;
564     HFONT  hOldFont = 0;
565     COLORREF clrOld = 0;
566     COLORREF clrOldBk = 0;
567     int oldBkMode = 0;
568     UINT state = tbcd->nmcd.uItemState;
569
570     /* draw text */
571     if (lpText) {
572         TRACE("string=%s rect=(%d,%d)-(%d,%d)\n", debugstr_w(lpText),
573               rcText->left, rcText->top, rcText->right, rcText->bottom);
574
575         hOldFont = SelectObject (hdc, infoPtr->hFont);
576         if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
577             clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
578         }
579         else if (state & CDIS_DISABLED) {
580             clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
581             OffsetRect (rcText, 1, 1);
582             DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
583             SetTextColor (hdc, comctl32_color.clr3dShadow);
584             OffsetRect (rcText, -1, -1);
585         }
586         else if (state & CDIS_INDETERMINATE) {
587             clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
588         }
589         else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
590             clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
591             clrOldBk = SetBkColor (hdc, tbcd->clrMark);
592             oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
593         }
594         else {
595             clrOld = SetTextColor (hdc, tbcd->clrText);
596         }
597
598         DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
599         SetTextColor (hdc, clrOld);
600         if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
601         {
602             SetBkColor (hdc, clrOldBk);
603             SetBkMode (hdc, oldBkMode);
604         }
605         SelectObject (hdc, hOldFont);
606     }
607 }
608
609
610 static void
611 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
612 {
613     HDC hdc = tbcd->nmcd.hdc;
614     HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
615     COLORREF clrTextOld;
616     COLORREF clrBkOld;
617     INT cx = lpRect->right - lpRect->left;
618     INT cy = lpRect->bottom - lpRect->top;
619     INT cxEdge = GetSystemMetrics(SM_CXEDGE);
620     INT cyEdge = GetSystemMetrics(SM_CYEDGE);
621     clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
622     clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
623     PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
624             cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
625     SetBkColor(hdc, clrBkOld);
626     SetTextColor(hdc, clrTextOld);
627     SelectObject (hdc, hbr);
628 }
629
630
631 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
632 {
633     INT cx, cy;
634     HBITMAP hbmMask, hbmImage;
635     HDC hdcMask, hdcImage;
636
637     ImageList_GetIconSize(himl, &cx, &cy);
638
639     /* Create src image */
640     hdcImage = CreateCompatibleDC(hdc);
641     hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
642     SelectObject(hdcImage, hbmImage);
643     ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
644                      RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
645
646     /* Create Mask */
647     hdcMask = CreateCompatibleDC(0);
648     hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
649     SelectObject(hdcMask, hbmMask);
650
651     /* Remove the background and all white pixels */
652     ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
653                      RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
654     SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
655     BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
656
657     /* draw the new mask 'etched' to hdc */
658     SetBkColor(hdc, RGB(255, 255, 255));
659     SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
660     /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
661     BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
662     SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
663     BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
664
665     /* Cleanup */
666     DeleteObject(hbmImage);
667     DeleteDC(hdcImage);
668     DeleteObject (hbmMask);
669     DeleteDC(hdcMask);
670 }
671
672
673 static UINT
674 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
675 {
676     UINT retstate = 0;
677
678     retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED  : 0;
679     retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
680     retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
681     retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED   : 0;
682     retstate |= (btnPtr->bHot                     ) ? CDIS_HOT      : 0;
683     retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
684     /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
685     return retstate;
686 }
687
688 /* draws the image on a toolbar button */
689 static void
690 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
691                   const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
692 {
693     HIMAGELIST himl = NULL;
694     BOOL draw_masked = FALSE;
695     INT index;
696     INT offset = 0;
697     UINT draw_flags = ILD_TRANSPARENT;
698
699     if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
700     {
701         himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
702         if (!himl)
703         {
704             himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
705             draw_masked = TRUE;
706         }
707     }
708     else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
709       ((tbcd->nmcd.uItemState & CDIS_HOT) 
710       && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
711     {
712         /* if hot, attempt to draw with hot image list, if fails, 
713            use default image list */
714         himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
715         if (!himl)
716             himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
717         }
718     else
719         himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
720
721     if (!himl)
722         return;
723
724     if (!(dwItemCDFlag & TBCDRF_NOOFFSET) && 
725         (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
726         offset = 1;
727
728     if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
729         (tbcd->nmcd.uItemState & CDIS_MARKED))
730         draw_flags |= ILD_BLEND50;
731
732     TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
733       index, himl, left, top, offset);
734
735     if (draw_masked)
736         TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
737     else
738         ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
739 }
740
741 /* draws a blank frame for a toolbar button */
742 static void
743 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
744 {
745     HDC hdc = tbcd->nmcd.hdc;
746     RECT rc = tbcd->nmcd.rc;
747     /* if the state is disabled or indeterminate then the button
748      * cannot have an interactive look like pressed or hot */
749     BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
750                                  (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
751     BOOL pressed_look = !non_interactive_state &&
752                         ((tbcd->nmcd.uItemState & CDIS_SELECTED) || 
753                          (tbcd->nmcd.uItemState & CDIS_CHECKED));
754
755     /* app don't want us to draw any edges */
756     if (dwItemCDFlag & TBCDRF_NOEDGES)
757         return;
758
759     if (infoPtr->dwStyle & TBSTYLE_FLAT)
760     {
761         if (pressed_look)
762             DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
763         else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
764             DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
765     }
766     else
767     {
768         if (pressed_look)
769             DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
770         else
771             DrawEdge (hdc, &rc, EDGE_RAISED,
772               BF_SOFT | BF_RECT | BF_MIDDLE);
773     }
774 }
775
776 static void
777 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
778 {
779     HDC hdc = tbcd->nmcd.hdc;
780     int offset = 0;
781     BOOL pressed = bDropDownPressed ||
782         (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
783
784     if (infoPtr->dwStyle & TBSTYLE_FLAT)
785     {
786         if (pressed)
787             DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
788         else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
789                  !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
790                  !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
791             DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
792     }
793     else
794     {
795         if (pressed)
796             DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
797         else
798             DrawEdge (hdc, rcArrow, EDGE_RAISED,
799               BF_SOFT | BF_RECT | BF_MIDDLE);
800     }
801
802     if (pressed)
803         offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
804
805     if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
806     {
807         TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
808         TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
809     }
810     else
811         TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
812 }
813
814 /* draws a complete toolbar button */
815 static void
816 TOOLBAR_DrawButton (HWND hwnd, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
817 {
818     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
819     DWORD dwStyle = infoPtr->dwStyle;
820     BOOL hasDropDownArrow = (TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) &&
821                             (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
822                             (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
823     BOOL drawSepDropDownArrow = hasDropDownArrow && 
824                                 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
825     RECT rc, rcArrow, rcBitmap, rcText;
826     LPWSTR lpText = NULL;
827     NMTBCUSTOMDRAW tbcd;
828     DWORD ntfret;
829     INT offset;
830     INT oldBkMode;
831     DWORD dwItemCustDraw;
832     DWORD dwItemCDFlag;
833     HTHEME theme = GetWindowTheme (hwnd);
834
835     rc = btnPtr->rect;
836     CopyRect (&rcArrow, &rc);
837
838     /* separator - doesn't send NM_CUSTOMDRAW */
839     if (btnPtr->fsStyle & BTNS_SEP) {
840         if (theme)
841         {
842             DrawThemeBackground (theme, hdc, 
843                 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0, 
844                 &rc, NULL);
845         }
846         else
847         /* with the FLAT style, iBitmap is the width and has already */
848         /* been taken into consideration in calculating the width    */
849         /* so now we need to draw the vertical separator             */
850         /* empirical tests show that iBitmap can/will be non-zero    */
851         /* when drawing the vertical bar...      */
852         if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
853             if (btnPtr->fsStyle & BTNS_DROPDOWN)
854                 TOOLBAR_DrawDDFlatSeparator (&rc, hdc, btnPtr, infoPtr);
855             else
856                 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
857         }
858         else if (btnPtr->fsStyle != BTNS_SEP) {
859             FIXME("Draw some kind of separator: fsStyle=%x\n",
860                   btnPtr->fsStyle);
861         }
862         return;
863     }
864
865     /* get a pointer to the text */
866     lpText = TOOLBAR_GetText(infoPtr, btnPtr);
867
868     if (hasDropDownArrow)
869     {
870         int right;
871
872         if (dwStyle & TBSTYLE_FLAT)
873             right = max(rc.left, rc.right - DDARROW_WIDTH);
874         else
875             right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
876
877         if (drawSepDropDownArrow)
878            rc.right = right;
879
880         rcArrow.left = right;
881     }
882
883     /* copy text & bitmap rects after adjusting for drop-down arrow
884      * so that text & bitmap is centred in the rectangle not containing
885      * the arrow */
886     CopyRect(&rcText, &rc);
887     CopyRect(&rcBitmap, &rc);
888
889     /* Center the bitmap horizontally and vertically */
890     if (dwStyle & TBSTYLE_LIST)
891     {
892         if (lpText &&
893             infoPtr->nMaxTextRows > 0 &&
894             (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
895             (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
896             rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
897         else
898             rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
899     }
900     else
901         rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
902
903     rcBitmap.top += infoPtr->szPadding.cy / 2;
904
905     TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
906       btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
907       infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
908     TRACE("Text=%s\n", debugstr_w(lpText));
909     TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
910
911     /* calculate text position */
912     if (lpText)
913     {
914         rcText.left += GetSystemMetrics(SM_CXEDGE);
915         rcText.right -= GetSystemMetrics(SM_CXEDGE);
916         if (dwStyle & TBSTYLE_LIST)
917         {
918             if (TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap))
919                 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
920         }
921         else
922         {
923             if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
924                 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
925             else
926                 rcText.top += infoPtr->szPadding.cy/2 + 2;
927         }
928     }
929
930     /* Initialize fields in all cases, because we use these later
931      * NOTE: applications can and do alter these to customize their
932      * toolbars */
933     ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
934     tbcd.clrText = comctl32_color.clrBtnText;
935     tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
936     tbcd.clrBtnFace = comctl32_color.clrBtnFace;
937     tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
938     tbcd.clrMark = comctl32_color.clrHighlight;
939     tbcd.clrHighlightHotTrack = 0;
940     tbcd.nStringBkMode = TRANSPARENT;
941     tbcd.nHLStringBkMode = OPAQUE;
942     /* MSDN says that this is the text rectangle.
943      * But (why always a but) tracing of v5.7 of native shows
944      * that this is really a *relative* rectangle based on the
945      * the nmcd.rc. Also the left and top are always 0 ignoring
946      * any bitmap that might be present. */
947     tbcd.rcText.left = 0;
948     tbcd.rcText.top = 0;
949     tbcd.rcText.right = rcText.right - rc.left;
950     tbcd.rcText.bottom = rcText.bottom - rc.top;
951     tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
952     tbcd.nmcd.hdc = hdc;
953     tbcd.nmcd.rc = rc;
954     tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
955
956     /* FIXME: what are these used for? */
957     tbcd.hbrLines = 0;
958     tbcd.hpenLines = 0;
959
960     /* Issue Item Prepaint notify */
961     dwItemCustDraw = 0;
962     dwItemCDFlag = 0;
963     if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
964     {
965         tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
966         tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
967         tbcd.nmcd.lItemlParam = btnPtr->dwData;
968         ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
969         /* reset these fields so the user can't alter the behaviour like native */
970         tbcd.nmcd.hdc = hdc;
971         tbcd.nmcd.rc = rc;
972
973         dwItemCustDraw = ntfret & 0xffff;
974         dwItemCDFlag = ntfret & 0xffff0000;
975         if (dwItemCustDraw & CDRF_SKIPDEFAULT)
976             return;
977         /* save the only part of the rect that the user can change */
978         rcText.right = tbcd.rcText.right + rc.left;
979         rcText.bottom = tbcd.rcText.bottom + rc.top;
980     }
981
982     if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
983         (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
984         OffsetRect(&rcText, 1, 1);
985
986     if (!(tbcd.nmcd.uItemState & CDIS_HOT) && 
987         ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
988         TOOLBAR_DrawPattern (&rc, &tbcd);
989
990     if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
991         && (tbcd.nmcd.uItemState & CDIS_HOT))
992     {
993         if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
994         {
995             COLORREF oldclr;
996
997             oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
998             ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
999             if (hasDropDownArrow)
1000                 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1001             SetBkColor(hdc, oldclr);
1002         }
1003     }
1004
1005     if (theme)
1006     {
1007         int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1008         int stateId = TS_NORMAL;
1009         
1010         if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1011             stateId = TS_DISABLED;
1012         else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1013             stateId = TS_PRESSED;
1014         else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1015             stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1016         else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1017             || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1018             stateId = TS_HOT;
1019             
1020         DrawThemeBackground (theme, hdc, partId, stateId, &tbcd.nmcd.rc, NULL);
1021     }
1022     else
1023         TOOLBAR_DrawFrame(infoPtr, &tbcd, dwItemCDFlag);
1024
1025     if (drawSepDropDownArrow)
1026     {
1027         if (theme)
1028         {
1029             int stateId = TS_NORMAL;
1030             
1031             if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1032                 stateId = TS_DISABLED;
1033             else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1034                 stateId = TS_PRESSED;
1035             else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1036                 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1037             else if (tbcd.nmcd.uItemState & CDIS_HOT)
1038                 stateId = TS_HOT;
1039                 
1040             DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1041             DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1042         }
1043         else
1044             TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1045     }
1046
1047     oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1048     if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1049         TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1050     SetBkMode (hdc, oldBkMode);
1051
1052     TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1053
1054     if (hasDropDownArrow && !drawSepDropDownArrow)
1055     {
1056         if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1057         {
1058             TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1059             TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1060         }
1061         else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1062         {
1063             offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1064             TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1065         }
1066         else
1067             TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1068     }
1069
1070     if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1071     {
1072         tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1073         TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1074     }
1075
1076 }
1077
1078
1079 static void
1080 TOOLBAR_Refresh (HWND hwnd, HDC hdc, const PAINTSTRUCT *ps)
1081 {
1082     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1083     TBUTTON_INFO *btnPtr;
1084     INT i;
1085     RECT rcTemp, rcClient;
1086     NMTBCUSTOMDRAW tbcd;
1087     DWORD ntfret;
1088     DWORD dwBaseCustDraw;
1089
1090     /* the app has told us not to redraw the toolbar */
1091     if (!infoPtr->bDoRedraw)
1092         return;
1093
1094     /* if imagelist belongs to the app, it can be changed
1095        by the app after setting it */
1096     if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1097     {
1098         infoPtr->nNumBitmaps = 0;
1099         for (i = 0; i < infoPtr->cimlDef; i++)
1100             infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1101     }
1102
1103     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1104
1105     /* change the imagelist icon size if we manage the list and it is necessary */
1106     TOOLBAR_CheckImageListIconSize(infoPtr);
1107
1108     /* Send initial notify */
1109     ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1110     tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1111     tbcd.nmcd.hdc = hdc;
1112     tbcd.nmcd.rc = ps->rcPaint;
1113     ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1114     dwBaseCustDraw = ntfret & 0xffff;
1115
1116     GetClientRect(hwnd, &rcClient);
1117
1118     /* redraw necessary buttons */
1119     btnPtr = infoPtr->buttons;
1120     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1121     {
1122         BOOL bDraw;
1123         if (!RectVisible(hdc, &btnPtr->rect))
1124             continue;
1125         if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1126         {
1127             IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1128             bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1129         }
1130         else
1131             bDraw = TRUE;
1132         bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1133         bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1134         if (bDraw)
1135             TOOLBAR_DrawButton(hwnd, btnPtr, hdc, dwBaseCustDraw);
1136     }
1137
1138     /* draw insert mark if required */
1139     if (infoPtr->tbim.iButton != -1)
1140     {
1141         RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1142         RECT rcInsertMark;
1143         rcInsertMark.top = rcButton.top;
1144         rcInsertMark.bottom = rcButton.bottom;
1145         if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1146             rcInsertMark.left = rcInsertMark.right = rcButton.right;
1147         else
1148             rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1149         COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1150     }
1151
1152     if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1153     {
1154         ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1155         tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1156         tbcd.nmcd.hdc = hdc;
1157         tbcd.nmcd.rc = ps->rcPaint;
1158         ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1159     }
1160 }
1161
1162 /***********************************************************************
1163 *               TOOLBAR_MeasureString
1164 *
1165 * This function gets the width and height of a string in pixels. This
1166 * is done first by using GetTextExtentPoint to get the basic width
1167 * and height. The DrawText is called with DT_CALCRECT to get the exact
1168 * width. The reason is because the text may have more than one "&" (or
1169 * prefix characters as M$ likes to call them). The prefix character
1170 * indicates where the underline goes, except for the string "&&" which
1171 * is reduced to a single "&". GetTextExtentPoint does not process these
1172 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1173 */
1174 static void
1175 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1176                       HDC hdc, LPSIZE lpSize)
1177 {
1178     RECT myrect;
1179
1180     lpSize->cx = 0;
1181     lpSize->cy = 0;
1182
1183     if (infoPtr->nMaxTextRows > 0 &&
1184         !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1185         (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1186         (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1187     {
1188         LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1189
1190         if(lpText != NULL) {
1191             /* first get size of all the text */
1192             GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1193
1194             /* feed above size into the rectangle for DrawText */
1195             myrect.left = myrect.top = 0;
1196             myrect.right = lpSize->cx;
1197             myrect.bottom = lpSize->cy;
1198
1199             /* Use DrawText to get true size as drawn (less pesky "&") */
1200             DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1201                    DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1202                                   DT_NOPREFIX : 0));
1203
1204             /* feed back to caller  */
1205             lpSize->cx = myrect.right;
1206             lpSize->cy = myrect.bottom;
1207         }
1208     }
1209
1210     TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1211 }
1212
1213 /***********************************************************************
1214 *               TOOLBAR_CalcStrings
1215 *
1216 * This function walks through each string and measures it and returns
1217 * the largest height and width to caller.
1218 */
1219 static void
1220 TOOLBAR_CalcStrings (HWND hwnd, LPSIZE lpSize)
1221 {
1222     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1223     TBUTTON_INFO *btnPtr;
1224     INT i;
1225     SIZE sz;
1226     HDC hdc;
1227     HFONT hOldFont;
1228
1229     lpSize->cx = 0;
1230     lpSize->cy = 0;
1231
1232     if (infoPtr->nMaxTextRows == 0)
1233         return;
1234
1235     hdc = GetDC (hwnd);
1236     hOldFont = SelectObject (hdc, infoPtr->hFont);
1237
1238     if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1239     {
1240         TEXTMETRICW tm;
1241
1242         GetTextMetricsW(hdc, &tm);
1243         lpSize->cy = tm.tmHeight;
1244     }
1245
1246     btnPtr = infoPtr->buttons;
1247     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1248         if(TOOLBAR_HasText(infoPtr, btnPtr))
1249         {
1250             TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1251             if (sz.cx > lpSize->cx)
1252                 lpSize->cx = sz.cx;
1253             if (sz.cy > lpSize->cy)
1254                 lpSize->cy = sz.cy;
1255         }
1256     }
1257
1258     SelectObject (hdc, hOldFont);
1259     ReleaseDC (hwnd, hdc);
1260
1261     TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1262 }
1263
1264 /***********************************************************************
1265 *               TOOLBAR_WrapToolbar
1266 *
1267 * This function walks through the buttons and separators in the
1268 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1269 * wrapping should occur based on the width of the toolbar window.
1270 * It does *not* calculate button placement itself.  That task
1271 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1272 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1273 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1274 *
1275 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_UNDOC1 can be used also to allow
1276 * vertical toolbar lists.
1277 */
1278
1279 static void
1280 TOOLBAR_WrapToolbar( HWND hwnd, DWORD dwStyle )
1281 {
1282     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1283     TBUTTON_INFO *btnPtr;
1284     INT x, cx, i, j;
1285     RECT rc;
1286     BOOL bWrap, bButtonWrap;
1287
1288     /*  When the toolbar window style is not TBSTYLE_WRAPABLE,  */
1289     /*  no layout is necessary. Applications may use this style */
1290     /*  to perform their own layout on the toolbar.             */
1291     if( !(dwStyle & TBSTYLE_WRAPABLE) &&
1292         !(infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1) )  return;
1293
1294     btnPtr = infoPtr->buttons;
1295     x  = infoPtr->nIndent;
1296
1297     if (GetParent(hwnd))
1298     {
1299         /* this can get the parents width, to know how far we can extend
1300          * this toolbar.  We cannot use its height, as there may be multiple
1301          * toolbars in a rebar control
1302          */
1303         GetClientRect( GetParent(hwnd), &rc );
1304         infoPtr->nWidth = rc.right - rc.left;
1305     }
1306     else
1307     {
1308         GetWindowRect( hwnd, &rc );
1309         infoPtr->nWidth = rc.right - rc.left;
1310     }
1311
1312     bButtonWrap = FALSE;
1313
1314     TRACE("start ButtonWidth=%d, BitmapWidth=%d, nWidth=%d, nIndent=%d\n",
1315           infoPtr->nButtonWidth, infoPtr->nBitmapWidth, infoPtr->nWidth,
1316           infoPtr->nIndent);
1317
1318     for (i = 0; i < infoPtr->nNumButtons; i++ )
1319     {
1320         bWrap = FALSE;
1321         btnPtr[i].fsState &= ~TBSTATE_WRAP;
1322
1323         if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1324             continue;
1325
1326         /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1327         /* it is the actual width of the separator. This is used for */
1328         /* custom controls in toolbars.                              */
1329         /*                                                           */
1330         /* BTNS_DROPDOWN separators are treated as buttons for    */
1331         /* width.  - GA 8/01                                         */
1332         if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1333             !(btnPtr[i].fsStyle & BTNS_DROPDOWN))
1334             cx = (btnPtr[i].iBitmap > 0) ?
1335                         btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1336         else
1337             cx = infoPtr->nButtonWidth;
1338
1339         /* Two or more adjacent separators form a separator group.   */
1340         /* The first separator in a group should be wrapped to the   */
1341         /* next row if the previous wrapping is on a button.         */
1342         if( bButtonWrap &&
1343                 (btnPtr[i].fsStyle & BTNS_SEP) &&
1344                 (i + 1 < infoPtr->nNumButtons ) &&
1345                 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1346         {
1347             TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1348             btnPtr[i].fsState |= TBSTATE_WRAP;
1349             x = infoPtr->nIndent;
1350             i++;
1351             bButtonWrap = FALSE;
1352             continue;
1353         }
1354
1355         /* The layout makes sure the bitmap is visible, but not the button. */
1356         /* Test added to also wrap after a button that starts a row but     */
1357         /* is bigger than the area.  - GA  8/01                             */
1358         if (( x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2
1359            > infoPtr->nWidth ) ||
1360             ((x == infoPtr->nIndent) && (cx > infoPtr->nWidth)))
1361         {
1362             BOOL bFound = FALSE;
1363
1364             /*  If the current button is a separator and not hidden,  */
1365             /*  go to the next until it reaches a non separator.      */
1366             /*  Wrap the last separator if it is before a button.     */
1367             while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1368                       !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1369                      (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1370                         i < infoPtr->nNumButtons )
1371             {
1372                 i++;
1373                 bFound = TRUE;
1374             }
1375
1376             if( bFound && i < infoPtr->nNumButtons )
1377             {
1378                 i--;
1379                 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1380                       i, btnPtr[i].fsStyle, x, cx);
1381                 btnPtr[i].fsState |= TBSTATE_WRAP;
1382                 x = infoPtr->nIndent;
1383                 bButtonWrap = FALSE;
1384                 continue;
1385             }
1386             else if ( i >= infoPtr->nNumButtons)
1387                 break;
1388
1389             /*  If the current button is not a separator, find the last  */
1390             /*  separator and wrap it.                                   */
1391             for ( j = i - 1; j >= 0  &&  !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1392             {
1393                 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1394                         !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1395                 {
1396                     bFound = TRUE;
1397                     i = j;
1398                     TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1399                           i, btnPtr[i].fsStyle, x, cx);
1400                     x = infoPtr->nIndent;
1401                     btnPtr[j].fsState |= TBSTATE_WRAP;
1402                     bButtonWrap = FALSE;
1403                     break;
1404                 }
1405             }
1406
1407             /*  If no separator available for wrapping, wrap one of     */
1408             /*  non-hidden previous button.                             */
1409             if (!bFound)
1410             {
1411                 for ( j = i - 1;
1412                         j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1413                 {
1414                     if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1415                         continue;
1416
1417                     bFound = TRUE;
1418                     i = j;
1419                     TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1420                           i, btnPtr[i].fsStyle, x, cx);
1421                     x = infoPtr->nIndent;
1422                     btnPtr[j].fsState |= TBSTATE_WRAP;
1423                     bButtonWrap = TRUE;
1424                     break;
1425                 }
1426             }
1427
1428             /* If all above failed, wrap the current button. */
1429             if (!bFound)
1430             {
1431                 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1432                       i, btnPtr[i].fsStyle, x, cx);
1433                 btnPtr[i].fsState |= TBSTATE_WRAP;
1434                 bFound = TRUE;
1435                 x = infoPtr->nIndent;
1436                 if (btnPtr[i].fsStyle & BTNS_SEP )
1437                     bButtonWrap = FALSE;
1438                 else
1439                     bButtonWrap = TRUE;
1440             }
1441         }
1442         else {
1443             TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1444                   i, btnPtr[i].fsStyle, x, cx);
1445             x += cx;
1446         }
1447     }
1448 }
1449
1450
1451 /***********************************************************************
1452 *               TOOLBAR_MeasureButton
1453 *
1454 * Calculates the width and height required for a button. Used in
1455 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1456 * the width of buttons that are autosized.
1457 *
1458 * Note that it would have been rather elegant to use one piece of code for
1459 * both the laying out of the toolbar and for controlling where button parts
1460 * are drawn, but the native control has inconsistencies between the two that
1461 * prevent this from being effectively. These inconsistencies can be seen as
1462 * artefacts where parts of the button appear outside of the bounding button
1463 * rectangle.
1464 *
1465 * There are several cases for the calculation of the button dimensions and
1466 * button part positioning:
1467 *
1468 * List
1469 * ====
1470 *
1471 * With Bitmap:
1472 *
1473 * +--------------------------------------------------------+ ^
1474 * |                    ^                     ^             | |
1475 * |                    | pad.cy / 2          | centred     | |
1476 * | pad.cx/2 + cxedge +--------------+     +------------+  | | DEFPAD_CY +
1477 * |<----------------->| nBitmapWidth |     | Text       |  | | max(nBitmapHeight, szText.cy)
1478 * |                   |<------------>|     |            |  | |
1479 * |                   +--------------+     +------------+  | |
1480 * |<-------------------------------------->|               | |
1481 * |  cxedge + iListGap + nBitmapWidth + 2  |<----------->  | |
1482 * |                                           szText.cx    | |
1483 * +--------------------------------------------------------+ -
1484 * <-------------------------------------------------------->
1485 *  2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1486 *
1487 * Without Bitmap (I_IMAGENONE):
1488 *
1489 * +-----------------------------------+ ^
1490 * |                     ^             | |
1491 * |                     | centred     | | LISTPAD_CY +
1492 * |                   +------------+  | | szText.cy
1493 * |                   | Text       |  | |
1494 * |                   |            |  | |
1495 * |                   +------------+  | |
1496 * |<----------------->|               | |
1497 * |      cxedge       |<----------->  | |
1498 * |                      szText.cx    | |
1499 * +-----------------------------------+ -
1500 * <----------------------------------->
1501 *          szText.cx + pad.cx
1502 *
1503 * Without text:
1504 *
1505 * +--------------------------------------+ ^
1506 * |                       ^              | |
1507 * |                       | padding.cy/2 | | DEFPAD_CY +
1508 * |                     +------------+   | | nBitmapHeight
1509 * |                     | Bitmap     |   | |
1510 * |                     |            |   | |
1511 * |                     +------------+   | |
1512 * |<------------------->|                | |
1513 * | cxedge + iListGap/2 |<----------->   | |
1514 * |                       nBitmapWidth   | |
1515 * +--------------------------------------+ -
1516 * <-------------------------------------->
1517 *     2*cxedge + nBitmapWidth + iListGap
1518 *
1519 * Non-List
1520 * ========
1521 *
1522 * With bitmap:
1523 *
1524 * +-----------------------------------+ ^
1525 * |                     ^             | |
1526 * |                     | pad.cy / 2  | | nBitmapHeight +
1527 * |                     -             | | szText.cy +
1528 * |                   +------------+  | | DEFPAD_CY + 1
1529 * |    centred        |   Bitmap   |  | |
1530 * |<----------------->|            |  | |
1531 * |                   +------------+  | |
1532 * |                         ^         | |
1533 * |                       1 |         | |
1534 * |                         -         | |
1535 * |     centred     +---------------+ | |
1536 * |<--------------->|      Text     | | |
1537 * |                 +---------------+ | |
1538 * +-----------------------------------+ -
1539 * <----------------------------------->
1540 * pad.cx + max(nBitmapWidth, szText.cx)
1541 *
1542 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1543 *
1544 * +---------------------------------------+ ^
1545 * |                     ^                 | |
1546 * |                     | 2 + pad.cy / 2  | |
1547 * |                     -                 | | szText.cy +
1548 * |    centred      +-----------------+   | | pad.cy + 2
1549 * |<--------------->|   Text          |   | |
1550 * |                 +-----------------+   | |
1551 * |                                       | |
1552 * +---------------------------------------+ -
1553 * <--------------------------------------->
1554 *          2*cxedge + pad.cx + szText.cx
1555 *
1556 * Without text:
1557 *   As for with bitmaps, but with szText.cx zero.
1558 */
1559 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1560                                          BOOL bHasBitmap, BOOL bValidImageList)
1561 {
1562     SIZE sizeButton;
1563     if (infoPtr->dwStyle & TBSTYLE_LIST)
1564     {
1565         /* set button height from bitmap / text height... */
1566         sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1567             sizeString.cy);
1568
1569         /* ... add on the necessary padding */
1570         if (bValidImageList)
1571         {
1572             if (bHasBitmap)
1573                 sizeButton.cy += DEFPAD_CY;
1574             else
1575                 sizeButton.cy += LISTPAD_CY;
1576         }
1577         else
1578             sizeButton.cy += infoPtr->szPadding.cy;
1579
1580         /* calculate button width */
1581         if (bHasBitmap)
1582         {
1583             sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1584                 infoPtr->nBitmapWidth + infoPtr->iListGap;
1585             if (sizeString.cx > 0)
1586                 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1587         }
1588         else
1589             sizeButton.cx = sizeString.cx + infoPtr->szPadding.cx;
1590     }
1591     else
1592     {
1593         if (bHasBitmap)
1594         {
1595             sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1596             if (sizeString.cy > 0)
1597                 sizeButton.cy += 1 + sizeString.cy;
1598             sizeButton.cx = infoPtr->szPadding.cx +
1599                 max(sizeString.cx, infoPtr->nBitmapWidth);
1600         }
1601         else
1602         {
1603             sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1604                 NONLIST_NOTEXT_OFFSET;
1605             sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1606                 infoPtr->szPadding.cx + sizeString.cx;
1607         }
1608     }
1609     return sizeButton;
1610 }
1611
1612
1613 /***********************************************************************
1614 *               TOOLBAR_CalcToolbar
1615 *
1616 * This function calculates button and separator placement. It first
1617 * calculates the button sizes, gets the toolbar window width and then
1618 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1619 * on. It assigns a new location to each item and sends this location to
1620 * the tooltip window if appropriate. Finally, it updates the rcBound
1621 * rect and calculates the new required toolbar window height.
1622 */
1623 static void
1624 TOOLBAR_CalcToolbar (HWND hwnd)
1625 {
1626     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1627     SIZE  sizeString, sizeButton;
1628     BOOL validImageList = FALSE;
1629
1630     TOOLBAR_CalcStrings (hwnd, &sizeString);
1631
1632     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1633
1634     if (TOOLBAR_IsValidImageList(infoPtr, 0))
1635         validImageList = TRUE;
1636     sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1637     infoPtr->nButtonWidth = sizeButton.cx;
1638     infoPtr->nButtonHeight = sizeButton.cy;
1639     infoPtr->iTopMargin = default_top_margin(infoPtr);
1640
1641     if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1642         infoPtr->nButtonWidth = infoPtr->cxMin;
1643     if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1644         infoPtr->nButtonWidth = infoPtr->cxMax;
1645
1646     TOOLBAR_LayoutToolbar(hwnd);
1647 }
1648
1649 static void
1650 TOOLBAR_LayoutToolbar(HWND hwnd)
1651 {
1652     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1653     TBUTTON_INFO *btnPtr;
1654     SIZE sizeButton;
1655     INT i, nRows, nSepRows;
1656     INT x, y, cx, cy;
1657     BOOL bWrap;
1658     BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1659     BOOL hasDropDownArrows = TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle);
1660
1661     TOOLBAR_WrapToolbar(hwnd, infoPtr->dwStyle);
1662
1663     x  = infoPtr->nIndent;
1664     y  = infoPtr->iTopMargin;
1665     cx = infoPtr->nButtonWidth;
1666     cy = infoPtr->nButtonHeight;
1667
1668     nRows = nSepRows = 0;
1669
1670     infoPtr->rcBound.top = y;
1671     infoPtr->rcBound.left = x;
1672     infoPtr->rcBound.bottom = y + cy;
1673     infoPtr->rcBound.right = x;
1674
1675     btnPtr = infoPtr->buttons;
1676
1677     TRACE("cy=%d\n", cy);
1678
1679     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1680     {
1681         bWrap = FALSE;
1682         if (btnPtr->fsState & TBSTATE_HIDDEN)
1683         {
1684             SetRectEmpty (&btnPtr->rect);
1685             continue;
1686         }
1687
1688         cy = infoPtr->nButtonHeight;
1689
1690         /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1691         /* it is the actual width of the separator. This is used for */
1692         /* custom controls in toolbars.                              */
1693         if (btnPtr->fsStyle & BTNS_SEP) {
1694             if (btnPtr->fsStyle & BTNS_DROPDOWN) {
1695                 cy = (btnPtr->iBitmap > 0) ?
1696                      btnPtr->iBitmap : SEPARATOR_WIDTH;
1697                 cx = infoPtr->nButtonWidth;
1698             }
1699             else
1700                 cx = (btnPtr->iBitmap > 0) ?
1701                      btnPtr->iBitmap : SEPARATOR_WIDTH;
1702         }
1703         else
1704         {
1705             if (btnPtr->cx)
1706               cx = btnPtr->cx;
1707             else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || 
1708                 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1709             {
1710               SIZE sz;
1711               HDC hdc;
1712               HFONT hOldFont;
1713
1714               hdc = GetDC (hwnd);
1715               hOldFont = SelectObject (hdc, infoPtr->hFont);
1716
1717               TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1718
1719               SelectObject (hdc, hOldFont);
1720               ReleaseDC (hwnd, hdc);
1721
1722               sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1723                   TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1724                   validImageList);
1725               cx = sizeButton.cx;
1726             }
1727             else
1728               cx = infoPtr->nButtonWidth;
1729
1730             /* if size has been set manually then don't add on extra space
1731              * for the drop down arrow */
1732             if (!btnPtr->cx && hasDropDownArrows && 
1733                 ((btnPtr->fsStyle & BTNS_DROPDOWN) || (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)))
1734               cx += DDARROW_WIDTH;
1735         }
1736         if (btnPtr->fsState & TBSTATE_WRAP )
1737                     bWrap = TRUE;
1738
1739         SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1740
1741         if (infoPtr->rcBound.left > x)
1742             infoPtr->rcBound.left = x;
1743         if (infoPtr->rcBound.right < x + cx)
1744             infoPtr->rcBound.right = x + cx;
1745         if (infoPtr->rcBound.bottom < y + cy)
1746             infoPtr->rcBound.bottom = y + cy;
1747
1748         TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1749
1750         /* btnPtr->nRow is zero based. The space between the rows is    */
1751         /* also considered as a row.                                    */
1752         btnPtr->nRow = nRows + nSepRows;
1753
1754         TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1755               i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1756               x, y, x+cx, y+cy);
1757
1758         if( bWrap )
1759         {
1760             if ( !(btnPtr->fsStyle & BTNS_SEP) )
1761                 y += cy;
1762             else
1763             {
1764                 /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1765                 /* it is the actual width of the separator. This is used for */
1766                 /* custom controls in toolbars.                              */
1767                 if ( !(btnPtr->fsStyle & BTNS_DROPDOWN))
1768                     y += cy + ( (btnPtr->iBitmap > 0 ) ?
1769                                 btnPtr->iBitmap : SEPARATOR_WIDTH) * 2 /3;
1770                 else
1771                     y += cy;
1772
1773                 /* nSepRows is used to calculate the extra height follwoing  */
1774                 /* the last row.                                             */
1775                 nSepRows++;
1776             }
1777             x = infoPtr->nIndent;
1778
1779             /* Increment row number unless this is the last button    */
1780             /* and it has Wrap set.                                   */
1781             if (i != infoPtr->nNumButtons-1)
1782                 nRows++;
1783         }
1784         else
1785             x += cx;
1786     }
1787
1788     /* infoPtr->nRows is the number of rows on the toolbar */
1789     infoPtr->nRows = nRows + nSepRows + 1;
1790
1791     TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1792 }
1793
1794
1795 static INT
1796 TOOLBAR_InternalHitTest (HWND hwnd, const POINT *lpPt)
1797 {
1798     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1799     TBUTTON_INFO *btnPtr;
1800     INT i;
1801
1802     btnPtr = infoPtr->buttons;
1803     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1804         if (btnPtr->fsState & TBSTATE_HIDDEN)
1805             continue;
1806
1807         if (btnPtr->fsStyle & BTNS_SEP) {
1808             if (PtInRect (&btnPtr->rect, *lpPt)) {
1809                 TRACE(" ON SEPARATOR %d!\n", i);
1810                 return -i;
1811             }
1812         }
1813         else {
1814             if (PtInRect (&btnPtr->rect, *lpPt)) {
1815                 TRACE(" ON BUTTON %d!\n", i);
1816                 return i;
1817             }
1818         }
1819     }
1820
1821     TRACE(" NOWHERE!\n");
1822     return TOOLBAR_NOWHERE;
1823 }
1824
1825
1826 static INT
1827 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1828 {
1829     TBUTTON_INFO *btnPtr;
1830     INT i;
1831
1832     if (CommandIsIndex) {
1833         TRACE("command is really index command=%d\n", idCommand);
1834         if (idCommand >= infoPtr->nNumButtons) return -1;
1835         return idCommand;
1836     }
1837     btnPtr = infoPtr->buttons;
1838     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1839         if (btnPtr->idCommand == idCommand) {
1840             TRACE("command=%d index=%d\n", idCommand, i);
1841             return i;
1842         }
1843     }
1844     TRACE("no index found for command=%d\n", idCommand);
1845     return -1;
1846 }
1847
1848
1849 static INT
1850 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1851 {
1852     TBUTTON_INFO *btnPtr;
1853     INT nRunIndex;
1854
1855     if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1856         return -1;
1857
1858     /* check index button */
1859     btnPtr = &infoPtr->buttons[nIndex];
1860     if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1861         if (btnPtr->fsState & TBSTATE_CHECKED)
1862             return nIndex;
1863     }
1864
1865     /* check previous buttons */
1866     nRunIndex = nIndex - 1;
1867     while (nRunIndex >= 0) {
1868         btnPtr = &infoPtr->buttons[nRunIndex];
1869         if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1870             if (btnPtr->fsState & TBSTATE_CHECKED)
1871                 return nRunIndex;
1872         }
1873         else
1874             break;
1875         nRunIndex--;
1876     }
1877
1878     /* check next buttons */
1879     nRunIndex = nIndex + 1;
1880     while (nRunIndex < infoPtr->nNumButtons) {
1881         btnPtr = &infoPtr->buttons[nRunIndex];
1882         if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1883             if (btnPtr->fsState & TBSTATE_CHECKED)
1884                 return nRunIndex;
1885         }
1886         else
1887             break;
1888         nRunIndex++;
1889     }
1890
1891     return -1;
1892 }
1893
1894
1895 static VOID
1896 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1897                     WPARAM wParam, LPARAM lParam)
1898 {
1899     MSG msg;
1900
1901     msg.hwnd = hwndMsg;
1902     msg.message = uMsg;
1903     msg.wParam = wParam;
1904     msg.lParam = lParam;
1905     msg.time = GetMessageTime ();
1906     msg.pt.x = (short)LOWORD(GetMessagePos ());
1907     msg.pt.y = (short)HIWORD(GetMessagePos ());
1908
1909     SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1910 }
1911
1912 static void
1913 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1914 {
1915     if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1916         TTTOOLINFOW ti;
1917
1918         ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1919         ti.cbSize   = sizeof (TTTOOLINFOW);
1920         ti.hwnd     = infoPtr->hwndSelf;
1921         ti.uId      = button->idCommand;
1922         ti.hinst    = 0;
1923         ti.lpszText = LPSTR_TEXTCALLBACKW;
1924         /* ti.lParam = random value from the stack? */
1925
1926         SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1927             0, (LPARAM)&ti);
1928     }
1929 }
1930
1931 static void
1932 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1933 {
1934     if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1935         TTTOOLINFOW ti;
1936
1937         ZeroMemory(&ti, sizeof(ti));
1938         ti.cbSize   = sizeof(ti);
1939         ti.hwnd     = infoPtr->hwndSelf;
1940         ti.uId      = button->idCommand;
1941
1942         SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
1943     }
1944 }
1945
1946 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1947 {
1948     /* Set the toolTip only for non-hidden, non-separator button */
1949     if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
1950     {
1951         TTTOOLINFOW ti;
1952
1953         ZeroMemory(&ti, sizeof(ti));
1954         ti.cbSize = sizeof(ti);
1955         ti.hwnd = infoPtr->hwndSelf;
1956         ti.uId = button->idCommand;
1957         ti.rect = button->rect;
1958         SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
1959     }
1960 }
1961
1962 /* Creates the tooltip control */
1963 static void
1964 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
1965 {
1966     int i;
1967     NMTOOLTIPSCREATED nmttc;
1968
1969     infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
1970             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1971             infoPtr->hwndSelf, 0, 0, 0);
1972
1973     if (!infoPtr->hwndToolTip)
1974         return;
1975
1976     /* Send NM_TOOLTIPSCREATED notification */
1977     nmttc.hwndToolTips = infoPtr->hwndToolTip;
1978     TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
1979
1980     for (i = 0; i < infoPtr->nNumButtons; i++)
1981     {
1982         TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
1983         TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
1984     }
1985 }
1986
1987 /* keeps available button list box sorted by button id */
1988 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
1989 {
1990     int i;
1991     int count;
1992     PCUSTOMBUTTON btnInfo;
1993     HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
1994
1995     TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
1996
1997     count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
1998
1999     /* position 0 is always separator */
2000     for (i = 1; i < count; i++)
2001     {
2002         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2003         if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2004         {
2005             i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2006             SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2007             return;
2008         }
2009     }
2010     /* id higher than all others add to end */
2011     i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2012     SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2013 }
2014
2015 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2016 {
2017     NMTOOLBARW nmtb;
2018
2019         TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2020
2021     if (nIndexFrom == nIndexTo)
2022         return;
2023
2024     /* MSDN states that iItem is the index of the button, rather than the
2025      * command ID as used by every other NMTOOLBAR notification */
2026     nmtb.iItem = nIndexFrom;
2027     if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2028     {
2029         PCUSTOMBUTTON btnInfo;
2030         NMHDR hdr;
2031         HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2032         int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2033
2034         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2035
2036         SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2037         SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2038         SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2039         SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2040
2041         if (nIndexTo <= 0)
2042             EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2043         else
2044             EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2045
2046         /* last item is always separator, so -2 instead of -1 */
2047         if (nIndexTo >= (count - 2))
2048             EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2049         else
2050             EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2051
2052         SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2053         SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2054
2055         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2056     }
2057 }
2058
2059 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2060 {
2061     NMTOOLBARW nmtb;
2062
2063     TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2064
2065     /* MSDN states that iItem is the index of the button, rather than the
2066      * command ID as used by every other NMTOOLBAR notification */
2067     nmtb.iItem = nIndexAvail;
2068     if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2069     {
2070         PCUSTOMBUTTON btnInfo;
2071         NMHDR hdr;
2072         HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2073         HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2074         int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2075
2076         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2077
2078         if (nIndexAvail != 0) /* index == 0 indicates separator */
2079         {
2080             /* remove from 'available buttons' list */
2081             SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2082             if (nIndexAvail == count-1)
2083                 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2084             else
2085                 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2086         }
2087         else
2088         {
2089             PCUSTOMBUTTON btnNew;
2090
2091             /* duplicate 'separator' button */
2092             btnNew = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2093             memcpy(btnNew, btnInfo, sizeof(CUSTOMBUTTON));
2094             btnInfo = btnNew;
2095         }
2096
2097         /* insert into 'toolbar button' list */
2098         SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2099         SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2100
2101         SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2102
2103         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2104     }
2105 }
2106
2107 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2108 {
2109     PCUSTOMBUTTON btnInfo;
2110     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2111
2112     TRACE("Remove: index %d\n", index);
2113
2114     btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2115
2116     /* send TBN_QUERYDELETE notification */
2117     if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2118     {
2119         NMHDR hdr;
2120
2121         SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2122         SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2123
2124         SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2125
2126         /* insert into 'available button' list */
2127         if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2128             TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2129         else
2130             Free(btnInfo);
2131
2132         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2133     }
2134 }
2135
2136 /* drag list notification function for toolbar buttons list box */
2137 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2138                                                         const DRAGLISTINFO *pDLI)
2139 {
2140     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2141     switch (pDLI->uNotification)
2142     {
2143     case DL_BEGINDRAG:
2144     {
2145         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2146         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2147         /* no dragging for last item (separator) */
2148         if (nCurrentItem >= (nCount - 1)) return FALSE;
2149         return TRUE;
2150     }
2151     case DL_DRAGGING:
2152     {
2153         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2154         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2155         /* no dragging past last item (separator) */
2156         if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2157         {
2158             DrawInsert(hwnd, hwndList, nCurrentItem);
2159             /* FIXME: native uses "move button" cursor */
2160             return DL_COPYCURSOR;
2161         }
2162
2163         /* not over toolbar buttons list */
2164         if (nCurrentItem < 0)
2165         {
2166             POINT ptWindow = pDLI->ptCursor;
2167             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2168             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2169             /* over available buttons list? */
2170             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2171                 /* FIXME: native uses "move button" cursor */
2172                 return DL_COPYCURSOR;
2173         }
2174         /* clear drag arrow */
2175         DrawInsert(hwnd, hwndList, -1);
2176         return DL_STOPCURSOR;
2177     }
2178     case DL_DROPPED:
2179     {
2180         INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2181         INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2182         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2183         if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2184         {
2185             /* clear drag arrow */
2186             DrawInsert(hwnd, hwndList, -1);
2187             /* move item */
2188             TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2189         }
2190         /* not over toolbar buttons list */
2191         if (nIndexTo < 0)
2192         {
2193             POINT ptWindow = pDLI->ptCursor;
2194             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2195             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2196             /* over available buttons list? */
2197             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2198                 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2199         }
2200         break;
2201     }
2202     case DL_CANCELDRAG:
2203         /* Clear drag arrow */
2204         DrawInsert(hwnd, hwndList, -1);
2205         break;
2206     }
2207
2208     return 0;
2209 }
2210
2211 /* drag list notification function for available buttons list box */
2212 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2213                                                       const DRAGLISTINFO *pDLI)
2214 {
2215     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2216     switch (pDLI->uNotification)
2217     {
2218     case DL_BEGINDRAG:
2219         return TRUE;
2220     case DL_DRAGGING:
2221     {
2222         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2223         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2224         /* no dragging past last item (separator) */
2225         if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2226         {
2227             DrawInsert(hwnd, hwndList, nCurrentItem);
2228             /* FIXME: native uses "move button" cursor */
2229             return DL_COPYCURSOR;
2230         }
2231
2232         /* not over toolbar buttons list */
2233         if (nCurrentItem < 0)
2234         {
2235             POINT ptWindow = pDLI->ptCursor;
2236             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2237             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2238             /* over available buttons list? */
2239             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2240                 /* FIXME: native uses "move button" cursor */
2241                 return DL_COPYCURSOR;
2242         }
2243         /* clear drag arrow */
2244         DrawInsert(hwnd, hwndList, -1);
2245         return DL_STOPCURSOR;
2246     }
2247     case DL_DROPPED:
2248     {
2249         INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2250         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2251         INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2252         if ((nIndexTo >= 0) && (nIndexTo < nCount))
2253         {
2254             /* clear drag arrow */
2255             DrawInsert(hwnd, hwndList, -1);
2256             /* add item */
2257             TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2258         }
2259     }
2260     case DL_CANCELDRAG:
2261         /* Clear drag arrow */
2262         DrawInsert(hwnd, hwndList, -1);
2263         break;
2264     }
2265     return 0;
2266 }
2267
2268 extern UINT uDragListMessage;
2269
2270 /***********************************************************************
2271  * TOOLBAR_CustomizeDialogProc
2272  * This function implements the toolbar customization dialog.
2273  */
2274 static INT_PTR CALLBACK
2275 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2276 {
2277     PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2278     PCUSTOMBUTTON btnInfo;
2279     NMTOOLBARA nmtb;
2280     TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2281
2282     switch (uMsg)
2283     {
2284         case WM_INITDIALOG:
2285             custInfo = (PCUSTDLG_INFO)lParam;
2286             SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2287
2288             if (custInfo)
2289             {
2290                 WCHAR Buffer[256];
2291                 int i = 0;
2292                 int index;
2293                 NMTBINITCUSTOMIZE nmtbic;
2294
2295                 infoPtr = custInfo->tbInfo;
2296
2297                 /* send TBN_QUERYINSERT notification */
2298                 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2299
2300                 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2301                     return FALSE;
2302
2303                 nmtbic.hwndDialog = hwnd;
2304                 /* Send TBN_INITCUSTOMIZE notification */
2305                 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2306                     TBNRF_HIDEHELP)
2307                 {
2308                     TRACE("TBNRF_HIDEHELP requested\n");
2309                     ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2310                 }
2311
2312                 /* add items to 'toolbar buttons' list and check if removable */
2313                 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2314                 {
2315                     btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2316                     memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2317                     btnInfo->btn.fsStyle = BTNS_SEP;
2318                     btnInfo->bVirtual = FALSE;
2319                     LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2320
2321                     /* send TBN_QUERYDELETE notification */
2322                     btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2323
2324                     index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2325                     SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2326                 }
2327
2328                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2329
2330                 /* insert separator button into 'available buttons' list */
2331                 btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2332                 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2333                 btnInfo->btn.fsStyle = BTNS_SEP;
2334                 btnInfo->bVirtual = FALSE;
2335                 btnInfo->bRemovable = TRUE;
2336                 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2337                 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2338                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2339
2340                 /* insert all buttons into dsa */
2341                 for (i = 0;; i++)
2342                 {
2343                     /* send TBN_GETBUTTONINFO notification */
2344                     NMTOOLBARW nmtb;
2345                     nmtb.iItem = i;
2346                     nmtb.pszText = Buffer;
2347                     nmtb.cchText = 256;
2348
2349                     /* Clear previous button's text */
2350                     ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2351
2352                     if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2353                         break;
2354
2355                     TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2356                         nmtb.tbButton.fsStyle, i, 
2357                         nmtb.tbButton.idCommand,
2358                         nmtb.tbButton.iString,
2359                         nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2360                         : "");
2361
2362                     /* insert button into the apropriate list */
2363                     index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2364                     if (index == -1)
2365                     {
2366                         btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2367                         btnInfo->bVirtual = FALSE;
2368                         btnInfo->bRemovable = TRUE;
2369                     }
2370                     else
2371                     {
2372                         btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, 
2373                             IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2374                     }
2375
2376                     memcpy (&btnInfo->btn, &nmtb.tbButton, sizeof(TBBUTTON));
2377                     if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2378                     {
2379                         if (lstrlenW(nmtb.pszText))
2380                             lstrcpyW(btnInfo->text, nmtb.pszText);
2381                         else if (nmtb.tbButton.iString >= 0 && 
2382                             nmtb.tbButton.iString < infoPtr->nNumStrings)
2383                         {
2384                             lstrcpyW(btnInfo->text, 
2385                                 infoPtr->strings[nmtb.tbButton.iString]);
2386                         }
2387                     }
2388
2389                     if (index == -1)
2390                         TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2391                 }
2392
2393                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2394
2395                 /* select first item in the 'available' list */
2396                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2397
2398                 /* append 'virtual' separator button to the 'toolbar buttons' list */
2399                 btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2400                 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2401                 btnInfo->btn.fsStyle = BTNS_SEP;
2402                 btnInfo->bVirtual = TRUE;
2403                 btnInfo->bRemovable = FALSE;
2404                 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2405                 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2406                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2407
2408                 /* select last item in the 'toolbar' list */
2409                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2410                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2411
2412         MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2413         MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2414
2415                 /* set focus and disable buttons */
2416                 PostMessageW (hwnd, WM_USER, 0, 0);
2417             }
2418             return TRUE;
2419
2420         case WM_USER:
2421             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2422             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2423             EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2424             SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2425             return TRUE;
2426
2427         case WM_CLOSE:
2428             EndDialog(hwnd, FALSE);
2429             return TRUE;
2430
2431         case WM_COMMAND:
2432             switch (LOWORD(wParam))
2433             {
2434                 case IDC_TOOLBARBTN_LBOX:
2435                     if (HIWORD(wParam) == LBN_SELCHANGE)
2436                     {
2437                         PCUSTOMBUTTON btnInfo;
2438                         NMTOOLBARA nmtb;
2439                         int count;
2440                         int index;
2441
2442                         count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2443                         index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2444
2445                         /* send TBN_QUERYINSERT notification */
2446                         nmtb.iItem = index;
2447                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2448
2449                         /* get list box item */
2450                         btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2451
2452                         if (index == (count - 1))
2453                         {
2454                             /* last item (virtual separator) */
2455                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2456                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2457                         }
2458                         else if (index == (count - 2))
2459                         {
2460                             /* second last item (last non-virtual item) */
2461                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2462                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2463                         }
2464                         else if (index == 0)
2465                         {
2466                             /* first item */
2467                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2468                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2469                         }
2470                         else
2471                         {
2472                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2473                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2474                         }
2475
2476                         EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2477                     }
2478                     break;
2479
2480                 case IDC_MOVEUP_BTN:
2481                     {
2482                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2483                         TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2484                     }
2485                     break;
2486
2487                 case IDC_MOVEDN_BTN: /* move down */
2488                     {
2489                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2490                         TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2491                     }
2492                     break;
2493
2494                 case IDC_REMOVE_BTN: /* remove button */
2495                     {
2496                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2497
2498                         if (LB_ERR == index)
2499                                 break;
2500
2501                         TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2502                     }
2503                     break;
2504                 case IDC_HELP_BTN:
2505                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2506                         break;
2507                 case IDC_RESET_BTN:
2508                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2509                         break;
2510
2511                 case IDOK: /* Add button */
2512                     {
2513                         int index;
2514                         int indexto;
2515
2516                         index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2517                         indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2518
2519                         TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2520                     }
2521                     break;
2522
2523                 case IDCANCEL:
2524                     EndDialog(hwnd, FALSE);
2525                     break;
2526             }
2527             return TRUE;
2528
2529         case WM_DESTROY:
2530             {
2531                 int count;
2532                 int i;
2533
2534                 /* delete items from 'toolbar buttons' listbox*/
2535                 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2536                 for (i = 0; i < count; i++)
2537                 {
2538                     btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2539                     Free(btnInfo);
2540                     SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2541                 }
2542                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2543
2544
2545                 /* delete items from 'available buttons' listbox*/
2546                 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2547                 for (i = 0; i < count; i++)
2548                 {
2549                     btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2550                     Free(btnInfo);
2551                     SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2552                 }
2553                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2554             }
2555             return TRUE;
2556
2557         case WM_DRAWITEM:
2558             if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2559             {
2560                 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2561                 RECT rcButton;
2562                 RECT rcText;
2563                 HPEN hPen, hOldPen;
2564                 HBRUSH hOldBrush;
2565                 COLORREF oldText = 0;
2566                 COLORREF oldBk = 0;
2567
2568                 /* get item data */
2569                 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, (WPARAM)lpdis->itemID, 0);
2570                 if (btnInfo == NULL)
2571                 {
2572                     FIXME("btnInfo invalid!\n");
2573                     return TRUE;
2574                 }
2575
2576                 /* set colors and select objects */
2577                 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2578                 if (btnInfo->bVirtual)
2579                    oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2580                 else
2581                    oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2582                 hPen = CreatePen( PS_SOLID, 1,
2583                      GetSysColor( (lpdis->itemState & ODS_SELECTED)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2584                 hOldPen = SelectObject (lpdis->hDC, hPen );
2585                 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2586
2587                 /* fill background rectangle */
2588                 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2589                            lpdis->rcItem.right, lpdis->rcItem.bottom);
2590
2591                 /* calculate button and text rectangles */
2592                 CopyRect (&rcButton, &lpdis->rcItem);
2593                 InflateRect (&rcButton, -1, -1);
2594                 CopyRect (&rcText, &rcButton);
2595                 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2596                 rcText.left = rcButton.right + 2;
2597
2598                 /* draw focus rectangle */
2599                 if (lpdis->itemState & ODS_FOCUS)
2600                     DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2601
2602                 /* draw button */
2603                 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2604                     DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2605
2606                 /* draw image and text */
2607                 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2608                         HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, 
2609                                 btnInfo->btn.iBitmap));
2610                     ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap), 
2611                                 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2612                 }
2613                 DrawTextW (lpdis->hDC,  btnInfo->text, -1, &rcText,
2614                                DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2615
2616                 /* delete objects and reset colors */
2617                 SelectObject (lpdis->hDC, hOldBrush);
2618                 SelectObject (lpdis->hDC, hOldPen);
2619                 SetBkColor (lpdis->hDC, oldBk);
2620                 SetTextColor (lpdis->hDC, oldText);
2621                 DeleteObject( hPen );
2622                 return TRUE;
2623             }
2624             return FALSE;
2625
2626         case WM_MEASUREITEM:
2627             if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2628             {
2629                 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2630
2631                 lpmis->itemHeight = 15 + 8; /* default height */
2632
2633                 return TRUE;
2634             }
2635             return FALSE;
2636
2637         default:
2638             if (uDragListMessage && (uMsg == uDragListMessage))
2639             {
2640                 if (wParam == IDC_TOOLBARBTN_LBOX)
2641                 {
2642                     LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2643                         custInfo, hwnd, (DRAGLISTINFO *)lParam);
2644                     SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2645                     return TRUE;
2646                 }
2647                 else if (wParam == IDC_AVAILBTN_LBOX)
2648                 {
2649                     LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2650                         custInfo, hwnd, (DRAGLISTINFO *)lParam);
2651                     SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2652                     return TRUE;
2653                 }
2654             }
2655             return FALSE;
2656     }
2657 }
2658
2659 static BOOL
2660 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2661 {
2662     HBITMAP hbmLoad;
2663     INT nCountBefore = ImageList_GetImageCount(himlDef);
2664     INT nCountAfter;
2665     INT cxIcon, cyIcon;
2666     INT nAdded;
2667     INT nIndex;
2668
2669     TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2670     /* Add bitmaps to the default image list */
2671     if (bitmap->hInst == NULL)         /* a handle was passed */
2672         hbmLoad = (HBITMAP)CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2673     else
2674         hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2675
2676     /* enlarge the bitmap if needed */
2677     ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2678     if (bitmap->hInst != COMCTL32_hModule)
2679         COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2680     
2681     nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2682     DeleteObject(hbmLoad);
2683     if (nIndex == -1)
2684         return FALSE;
2685     
2686     nCountAfter = ImageList_GetImageCount(himlDef);
2687     nAdded =  nCountAfter - nCountBefore;
2688     if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2689     {
2690         ImageList_SetImageCount(himlDef, nCountBefore + 1);
2691     } else if (nAdded > (INT)bitmap->nButtons) {
2692         TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2693             nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2694     }
2695
2696     infoPtr->nNumBitmaps += nAdded;
2697     return TRUE;
2698 }
2699
2700 static void
2701 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2702 {
2703     HIMAGELIST himlDef;
2704     HIMAGELIST himlNew;
2705     INT cx, cy;
2706     INT i;
2707     
2708     himlDef = GETDEFIMAGELIST(infoPtr, 0);
2709     if (himlDef == NULL || himlDef != infoPtr->himlInt)
2710         return;
2711     if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2712         return;
2713     if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2714         return;
2715
2716     TRACE("Update icon size: %dx%d -> %dx%d\n",
2717         cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2718
2719     himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2720                                 ILC_COLORDDB|ILC_MASK, 8, 2);
2721     for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2722         TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2723     TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2724     infoPtr->himlInt = himlNew;
2725
2726     infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2727     ImageList_Destroy(himlDef);
2728 }
2729
2730 /***********************************************************************
2731  * TOOLBAR_AddBitmap:  Add the bitmaps to the default image list.
2732  *
2733  */
2734 static LRESULT
2735 TOOLBAR_AddBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
2736 {
2737     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2738     LPTBADDBITMAP lpAddBmp = (LPTBADDBITMAP)lParam;
2739     TBITMAP_INFO info;
2740     INT iSumButtons, i;
2741     HIMAGELIST himlDef;
2742
2743     TRACE("hwnd=%p wParam=%lx lParam=%lx\n", hwnd, wParam, lParam);
2744     if (!lpAddBmp)
2745         return -1;
2746
2747     if (lpAddBmp->hInst == HINST_COMMCTRL)
2748     {
2749         info.hInst = COMCTL32_hModule;
2750         switch (lpAddBmp->nID)
2751         {
2752             case IDB_STD_SMALL_COLOR:
2753                 info.nButtons = 15;
2754                 info.nID = IDB_STD_SMALL;
2755                 break;
2756             case IDB_STD_LARGE_COLOR:
2757                 info.nButtons = 15;
2758                 info.nID = IDB_STD_LARGE;
2759                 break;
2760             case IDB_VIEW_SMALL_COLOR:
2761                 info.nButtons = 12;
2762                 info.nID = IDB_VIEW_SMALL;
2763                 break;
2764             case IDB_VIEW_LARGE_COLOR:
2765                 info.nButtons = 12;
2766                 info.nID = IDB_VIEW_LARGE;
2767                 break;
2768             case IDB_HIST_SMALL_COLOR:
2769                 info.nButtons = 5;
2770                 info.nID = IDB_HIST_SMALL;
2771                 break;
2772             case IDB_HIST_LARGE_COLOR:
2773                 info.nButtons = 5;
2774                 info.nID = IDB_HIST_LARGE;
2775                 break;
2776             default:
2777                 return -1;
2778         }
2779
2780         TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2781
2782         /* Windows resize all the buttons to the size of a newly added standard image */
2783         if (lpAddBmp->nID & 1)
2784         {
2785             /* large icons: 24x24. Will make the button 31x30 */
2786             SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2787         }
2788         else
2789         {
2790             /* small icons: 16x16. Will make the buttons 23x22 */
2791             SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2792         }
2793
2794         TOOLBAR_CalcToolbar (hwnd);
2795     }
2796     else
2797     {
2798         info.nButtons = (INT)wParam;
2799         info.hInst = lpAddBmp->hInst;
2800         info.nID = lpAddBmp->nID;
2801         TRACE("adding %d bitmaps!\n", info.nButtons);
2802     }
2803     
2804     /* check if the bitmap is already loaded and compute iSumButtons */
2805     iSumButtons = 0;
2806     for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2807     {
2808         if (infoPtr->bitmaps[i].hInst == info.hInst &&
2809             infoPtr->bitmaps[i].nID == info.nID)
2810             return iSumButtons;
2811         iSumButtons += infoPtr->bitmaps[i].nButtons;
2812     }
2813
2814     if (!infoPtr->cimlDef) {
2815         /* create new default image list */
2816         TRACE ("creating default image list!\n");
2817
2818         himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2819                                     ILC_COLORDDB | ILC_MASK, info.nButtons, 2);
2820         TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2821         infoPtr->himlInt = himlDef;
2822     }
2823     else {
2824         himlDef = GETDEFIMAGELIST(infoPtr, 0);
2825     }
2826
2827     if (!himlDef) {
2828         WARN("No default image list available\n");
2829         return -1;
2830     }
2831
2832     if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2833         return -1;
2834
2835     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2836     infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2837     infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2838     infoPtr->nNumBitmapInfos++;
2839     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2840
2841     InvalidateRect(hwnd, NULL, TRUE);
2842     return iSumButtons;
2843 }
2844
2845
2846 static LRESULT
2847 TOOLBAR_AddButtonsT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
2848 {
2849     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2850     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2851     INT nOldButtons, nNewButtons, nAddButtons, nCount;
2852     BOOL fHasString = FALSE;
2853
2854     TRACE("adding %ld buttons (unicode=%d)!\n", wParam, fUnicode);
2855
2856     nAddButtons = (UINT)wParam;
2857     nOldButtons = infoPtr->nNumButtons;
2858     nNewButtons = nOldButtons + nAddButtons;
2859
2860     infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
2861     infoPtr->nNumButtons = nNewButtons;
2862
2863     /* insert new button data */
2864     for (nCount = 0; nCount < nAddButtons; nCount++) {
2865         TBUTTON_INFO *btnPtr = &infoPtr->buttons[nOldButtons+nCount];
2866         btnPtr->iBitmap   = lpTbb[nCount].iBitmap;
2867         btnPtr->idCommand = lpTbb[nCount].idCommand;
2868         btnPtr->fsState   = lpTbb[nCount].fsState;
2869         btnPtr->fsStyle   = lpTbb[nCount].fsStyle;
2870         btnPtr->dwData    = lpTbb[nCount].dwData;
2871         btnPtr->bHot      = FALSE;
2872         if(HIWORD(lpTbb[nCount].iString) && lpTbb[nCount].iString != -1)
2873         {
2874             if (fUnicode)
2875                 Str_SetPtrW ((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[nCount].iString );
2876             else
2877                 Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[nCount].iString);
2878             fHasString = TRUE;
2879         }
2880         else
2881             btnPtr->iString   = lpTbb[nCount].iString;
2882
2883         TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
2884     }
2885
2886     if (infoPtr->nNumStrings > 0 || fHasString)
2887         TOOLBAR_CalcToolbar(hwnd);
2888     else
2889         TOOLBAR_LayoutToolbar(hwnd);
2890     TOOLBAR_AutoSize (hwnd);
2891
2892     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
2893
2894     InvalidateRect(hwnd, NULL, TRUE);
2895
2896     return TRUE;
2897 }
2898
2899
2900 static LRESULT
2901 TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2902 {
2903 #define MAX_RESOURCE_STRING_LENGTH 512
2904     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2905     BOOL fFirstString = (infoPtr->nNumStrings == 0);
2906     INT nIndex = infoPtr->nNumStrings;
2907
2908     if ((wParam) && (HIWORD(lParam) == 0)) {
2909         WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2910         WCHAR delimiter;
2911         WCHAR *next_delim;
2912         WCHAR *p;
2913         INT len;
2914         TRACE("adding string from resource!\n");
2915
2916         len = LoadStringW ((HINSTANCE)wParam, (UINT)lParam,
2917                              szString, MAX_RESOURCE_STRING_LENGTH);
2918
2919         TRACE("len=%d %s\n", len, debugstr_w(szString));
2920         if (len == 0 || len == 1)
2921             return nIndex;
2922
2923         TRACE("Delimiter: 0x%x\n", *szString);
2924         delimiter = *szString;
2925         p = szString + 1;
2926
2927         while ((next_delim = strchrW(p, delimiter)) != NULL) {
2928             *next_delim = 0;
2929             if (next_delim + 1 >= szString + len)
2930             {
2931                 /* this may happen if delimiter == '\0' or if the last char is a
2932                  * delimiter (then it is ignored like the native does) */
2933                 break;
2934             }
2935
2936             infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2937             Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2938             infoPtr->nNumStrings++;
2939
2940             p = next_delim + 1;
2941         }
2942     }
2943     else {
2944         LPWSTR p = (LPWSTR)lParam;
2945         INT len;
2946
2947         if (p == NULL)
2948             return -1;
2949         TRACE("adding string(s) from array!\n");
2950         while (*p) {
2951             len = strlenW (p);
2952
2953             TRACE("len=%d %s\n", len, debugstr_w(p));
2954             infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2955             Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2956             infoPtr->nNumStrings++;
2957
2958             p += (len+1);
2959         }
2960     }
2961
2962     if (fFirstString)
2963         TOOLBAR_CalcToolbar(hwnd);
2964     return nIndex;
2965 }
2966
2967
2968 static LRESULT
2969 TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2970 {
2971     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2972     BOOL fFirstString = (infoPtr->nNumStrings == 0);
2973     LPSTR p;
2974     INT nIndex;
2975     INT len;
2976
2977     if ((wParam) && (HIWORD(lParam) == 0))  /* load from resources */
2978         return TOOLBAR_AddStringW(hwnd, wParam, lParam);
2979
2980     p = (LPSTR)lParam;
2981     if (p == NULL)
2982         return -1;
2983
2984     TRACE("adding string(s) from array!\n");
2985     nIndex = infoPtr->nNumStrings;
2986     while (*p) {
2987         len = strlen (p);
2988         TRACE("len=%d \"%s\"\n", len, p);
2989
2990         infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2991         Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
2992         infoPtr->nNumStrings++;
2993
2994         p += (len+1);
2995     }
2996
2997     if (fFirstString)
2998         TOOLBAR_CalcToolbar(hwnd);
2999     return nIndex;
3000 }
3001
3002
3003 static LRESULT
3004 TOOLBAR_AutoSize (HWND hwnd)
3005 {
3006     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3007     RECT parent_rect;
3008     HWND parent;
3009     INT  x, y;
3010     INT  cx, cy;
3011
3012     TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3013
3014     parent = GetParent (hwnd);
3015
3016     if (!parent || !infoPtr->bDoRedraw)
3017         return 0;
3018
3019     GetClientRect(parent, &parent_rect);
3020
3021     x = parent_rect.left;
3022     y = parent_rect.top;
3023
3024     TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3025
3026     cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3027     cx = parent_rect.right - parent_rect.left;
3028
3029     if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1))
3030     {
3031         TOOLBAR_LayoutToolbar(hwnd);
3032         InvalidateRect( hwnd, NULL, TRUE );
3033     }
3034
3035     if (!(infoPtr->dwStyle & CCS_NORESIZE))
3036     {
3037         RECT window_rect;
3038         UINT uPosFlags = SWP_NOZORDER;
3039
3040         if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3041         {
3042             GetWindowRect(hwnd, &window_rect);
3043             ScreenToClient(parent, (LPPOINT)&window_rect.left);
3044             y = window_rect.top;
3045         }
3046         if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3047         {
3048             GetWindowRect(hwnd, &window_rect);
3049             y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3050         }
3051
3052         if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3053             uPosFlags |= SWP_NOMOVE;
3054     
3055         if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3056             cy += GetSystemMetrics(SM_CYEDGE);
3057
3058         if (infoPtr->dwStyle & WS_BORDER)
3059         {
3060             x = y = 1; /* FIXME: this looks wrong */
3061             cy += GetSystemMetrics(SM_CYEDGE);
3062             cx += GetSystemMetrics(SM_CXEDGE);
3063         }
3064
3065         SetWindowPos(hwnd, NULL, x, y, cx, cy, uPosFlags);
3066     }
3067
3068     return 0;
3069 }
3070
3071
3072 static LRESULT
3073 TOOLBAR_ButtonCount (HWND hwnd, WPARAM wParam, LPARAM lParam)
3074 {
3075     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3076
3077     return infoPtr->nNumButtons;
3078 }
3079
3080
3081 static LRESULT
3082 TOOLBAR_ButtonStructSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
3083 {
3084     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3085
3086     infoPtr->dwStructSize = (DWORD)wParam;
3087
3088     return 0;
3089 }
3090
3091
3092 static LRESULT
3093 TOOLBAR_ChangeBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3094 {
3095     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3096     TBUTTON_INFO *btnPtr;
3097     INT nIndex;
3098
3099     TRACE("button %ld, iBitmap now %d\n", wParam, LOWORD(lParam));
3100
3101     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3102     if (nIndex == -1)
3103         return FALSE;
3104
3105     btnPtr = &infoPtr->buttons[nIndex];
3106     btnPtr->iBitmap = LOWORD(lParam);
3107
3108     /* we HAVE to erase the background, the new bitmap could be */
3109     /* transparent */
3110     InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3111
3112     return TRUE;
3113 }
3114
3115
3116 static LRESULT
3117 TOOLBAR_CheckButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3118 {
3119     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3120     TBUTTON_INFO *btnPtr;
3121     INT nIndex;
3122     INT nOldIndex = -1;
3123     BOOL bChecked = FALSE;
3124
3125     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3126
3127     TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", hwnd, nIndex, lParam);
3128
3129     if (nIndex == -1)
3130         return FALSE;
3131
3132     btnPtr = &infoPtr->buttons[nIndex];
3133
3134     bChecked = (btnPtr->fsState & TBSTATE_CHECKED) ? TRUE : FALSE;
3135
3136     if (LOWORD(lParam) == FALSE)
3137         btnPtr->fsState &= ~TBSTATE_CHECKED;
3138     else {
3139         if (btnPtr->fsStyle & BTNS_GROUP) {
3140             nOldIndex =
3141                 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3142             if (nOldIndex == nIndex)
3143                 return 0;
3144             if (nOldIndex != -1)
3145                 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3146         }
3147         btnPtr->fsState |= TBSTATE_CHECKED;
3148     }
3149
3150     if( bChecked != LOWORD(lParam) )
3151     {
3152         if (nOldIndex != -1)
3153             InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect, TRUE);
3154         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3155     }
3156
3157     /* FIXME: Send a WM_NOTIFY?? */
3158
3159     return TRUE;
3160 }
3161
3162
3163 static LRESULT
3164 TOOLBAR_CommandToIndex (HWND hwnd, WPARAM wParam, LPARAM lParam)
3165 {
3166     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3167
3168     return TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3169 }
3170
3171
3172 static LRESULT
3173 TOOLBAR_Customize (HWND hwnd)
3174 {
3175     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3176     CUSTDLG_INFO custInfo;
3177     LRESULT ret;
3178     LPCVOID template;
3179     HRSRC hRes;
3180     NMHDR nmhdr;
3181
3182     custInfo.tbInfo = infoPtr;
3183     custInfo.tbHwnd = hwnd;
3184
3185     /* send TBN_BEGINADJUST notification */
3186     TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3187
3188     if (!(hRes = FindResourceW (COMCTL32_hModule,
3189                                 MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3190                                 (LPWSTR)RT_DIALOG)))
3191         return FALSE;
3192
3193     if(!(template = (LPVOID)LoadResource (COMCTL32_hModule, hRes)))
3194         return FALSE;
3195
3196     ret = DialogBoxIndirectParamW ((HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE),
3197                                    (LPCDLGTEMPLATEW)template,
3198                                    hwnd,
3199                                    TOOLBAR_CustomizeDialogProc,
3200                                    (LPARAM)&custInfo);
3201
3202     /* send TBN_ENDADJUST notification */
3203     TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3204
3205     return ret;
3206 }
3207
3208
3209 static LRESULT
3210 TOOLBAR_DeleteButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3211 {
3212     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3213     INT nIndex = (INT)wParam;
3214     NMTOOLBARW nmtb;
3215     TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3216
3217     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3218         return FALSE;
3219
3220     memset(&nmtb, 0, sizeof(nmtb));
3221     nmtb.iItem = btnPtr->idCommand;
3222     nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3223     nmtb.tbButton.idCommand = btnPtr->idCommand;
3224     nmtb.tbButton.fsState = btnPtr->fsState;
3225     nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3226     nmtb.tbButton.dwData = btnPtr->dwData;
3227     nmtb.tbButton.iString = btnPtr->iString;
3228     TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3229
3230     TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3231
3232     if (infoPtr->nNumButtons == 1) {
3233         TRACE(" simple delete!\n");
3234         Free (infoPtr->buttons);
3235         infoPtr->buttons = NULL;
3236         infoPtr->nNumButtons = 0;
3237     }
3238     else {
3239         TBUTTON_INFO *oldButtons = infoPtr->buttons;
3240         TRACE("complex delete! [nIndex=%d]\n", nIndex);
3241
3242         infoPtr->nNumButtons--;
3243         infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3244         if (nIndex > 0) {
3245             memcpy (&infoPtr->buttons[0], &oldButtons[0],
3246                     nIndex * sizeof(TBUTTON_INFO));
3247         }
3248
3249         if (nIndex < infoPtr->nNumButtons) {
3250             memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3251                     (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3252         }
3253
3254         Free (oldButtons);
3255     }
3256
3257     TOOLBAR_LayoutToolbar(hwnd);
3258
3259     InvalidateRect (hwnd, NULL, TRUE);
3260
3261     return TRUE;
3262 }
3263
3264
3265 static LRESULT
3266 TOOLBAR_EnableButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3267 {
3268     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3269     TBUTTON_INFO *btnPtr;
3270     INT nIndex;
3271     DWORD bState;
3272
3273     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3274
3275     TRACE("hwnd=%p, btn index=%ld, lParam=0x%08lx\n", hwnd, wParam, lParam);
3276
3277     if (nIndex == -1)
3278         return FALSE;
3279
3280     btnPtr = &infoPtr->buttons[nIndex];
3281
3282     bState = btnPtr->fsState & TBSTATE_ENABLED;
3283
3284     /* update the toolbar button state */
3285     if(LOWORD(lParam) == FALSE) {
3286         btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3287     } else {
3288         btnPtr->fsState |= TBSTATE_ENABLED;
3289     }
3290
3291     /* redraw the button only if the state of the button changed */
3292     if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3293         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3294
3295     return TRUE;
3296 }
3297
3298
3299 static inline LRESULT
3300 TOOLBAR_GetAnchorHighlight (HWND hwnd)
3301 {
3302     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3303
3304     return infoPtr->bAnchor;
3305 }
3306
3307
3308 static LRESULT
3309 TOOLBAR_GetBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3310 {
3311     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3312     INT nIndex;
3313
3314     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3315     if (nIndex == -1)
3316         return -1;
3317
3318     return infoPtr->buttons[nIndex].iBitmap;
3319 }
3320
3321
3322 static inline LRESULT
3323 TOOLBAR_GetBitmapFlags (HWND hwnd, WPARAM wParam, LPARAM lParam)
3324 {
3325     return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3326 }
3327
3328
3329 static LRESULT
3330 TOOLBAR_GetButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3331 {
3332     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3333     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3334     INT nIndex = (INT)wParam;
3335     TBUTTON_INFO *btnPtr;
3336
3337     if (lpTbb == NULL)
3338         return FALSE;
3339
3340     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3341         return FALSE;
3342
3343     btnPtr = &infoPtr->buttons[nIndex];
3344     lpTbb->iBitmap   = btnPtr->iBitmap;
3345     lpTbb->idCommand = btnPtr->idCommand;
3346     lpTbb->fsState   = btnPtr->fsState;
3347     lpTbb->fsStyle   = btnPtr->fsStyle;
3348     lpTbb->bReserved[0] = 0;
3349     lpTbb->bReserved[1] = 0;
3350     lpTbb->dwData    = btnPtr->dwData;
3351     lpTbb->iString   = btnPtr->iString;
3352
3353     return TRUE;
3354 }
3355
3356
3357 static LRESULT
3358 TOOLBAR_GetButtonInfoT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL bUnicode)
3359 {
3360     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3361     /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3362     LPTBBUTTONINFOW lpTbInfo = (LPTBBUTTONINFOW)lParam;
3363     TBUTTON_INFO *btnPtr;
3364     INT nIndex;
3365
3366     if (lpTbInfo == NULL)
3367         return -1;
3368
3369     /* MSDN documents a iImageLabel field added in Vista but it is not present in
3370      * the headers and tests shows that even with comctl 6 Vista accepts only the
3371      * original TBBUTTONINFO size
3372      */
3373     if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3374     {
3375         WARN("Invalid button size\n");
3376         return -1;
3377     }
3378
3379     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
3380                                      lpTbInfo->dwMask & 0x80000000);
3381     if (nIndex == -1)
3382         return -1;
3383
3384     if (!(btnPtr = &infoPtr->buttons[nIndex])) return -1;
3385
3386     if (lpTbInfo->dwMask & TBIF_COMMAND)
3387         lpTbInfo->idCommand = btnPtr->idCommand;
3388     if (lpTbInfo->dwMask & TBIF_IMAGE)
3389         lpTbInfo->iImage = btnPtr->iBitmap;
3390     if (lpTbInfo->dwMask & TBIF_LPARAM)
3391         lpTbInfo->lParam = btnPtr->dwData;
3392     if (lpTbInfo->dwMask & TBIF_SIZE)
3393         lpTbInfo->cx = (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3394     if (lpTbInfo->dwMask & TBIF_STATE)
3395         lpTbInfo->fsState = btnPtr->fsState;
3396     if (lpTbInfo->dwMask & TBIF_STYLE)
3397         lpTbInfo->fsStyle = btnPtr->fsStyle;
3398     if (lpTbInfo->dwMask & TBIF_TEXT) {
3399         /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3400            can't use TOOLBAR_GetText here */
3401         if (HIWORD(btnPtr->iString) && (btnPtr->iString != -1)) {
3402             LPWSTR lpText = (LPWSTR)btnPtr->iString;
3403             if (bUnicode)
3404                 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3405             else
3406                 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3407         } else
3408             lpTbInfo->pszText[0] = '\0';
3409     }
3410     return nIndex;
3411 }
3412
3413
3414 static LRESULT
3415 TOOLBAR_GetButtonSize (HWND hwnd)
3416 {
3417     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3418
3419     return MAKELONG((WORD)infoPtr->nButtonWidth,
3420                     (WORD)infoPtr->nButtonHeight);
3421 }
3422
3423
3424 static LRESULT
3425 TOOLBAR_GetButtonTextA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3426 {
3427     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3428     INT nIndex;
3429     LPWSTR lpText;
3430
3431     if (lParam == 0)
3432         return -1;
3433
3434     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3435     if (nIndex == -1)
3436         return -1;
3437
3438     lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3439
3440     return WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3441                                 (LPSTR)lParam, 0x7fffffff, NULL, NULL ) - 1;
3442 }
3443
3444
3445 static LRESULT
3446 TOOLBAR_GetButtonTextW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3447 {
3448     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3449     INT nIndex;
3450     LPWSTR lpText;
3451     LRESULT ret = 0;
3452
3453     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3454     if (nIndex == -1)
3455         return -1;
3456
3457     lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3458
3459     if (lpText)
3460     {
3461         ret = strlenW (lpText);
3462
3463         if (lParam)
3464             strcpyW ((LPWSTR)lParam, lpText);
3465     }
3466
3467     return ret;
3468 }
3469
3470
3471 static LRESULT
3472 TOOLBAR_GetDisabledImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3473 {
3474     TRACE("hwnd=%p, wParam=%ld, lParam=0x%lx\n", hwnd, wParam, lParam);
3475     /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3476     return (LRESULT)GETDISIMAGELIST(TOOLBAR_GetInfoPtr (hwnd), wParam);
3477 }
3478
3479
3480 static inline LRESULT
3481 TOOLBAR_GetExtendedStyle (HWND hwnd)
3482 {
3483     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3484
3485     TRACE("\n");
3486
3487     return infoPtr->dwExStyle;
3488 }
3489
3490
3491 static LRESULT
3492 TOOLBAR_GetHotImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3493 {
3494     TRACE("hwnd=%p, wParam=%ld, lParam=0x%lx\n", hwnd, wParam, lParam);
3495     /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3496     return (LRESULT)GETHOTIMAGELIST(TOOLBAR_GetInfoPtr (hwnd), wParam);
3497 }
3498
3499
3500 static LRESULT
3501 TOOLBAR_GetHotItem (HWND hwnd)
3502 {
3503     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3504
3505     if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3506         return -1;
3507
3508     if (infoPtr->nHotItem < 0)
3509         return -1;
3510
3511     return (LRESULT)infoPtr->nHotItem;
3512 }
3513
3514
3515 static LRESULT
3516 TOOLBAR_GetDefImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3517 {
3518     TRACE("hwnd=%p, wParam=%ld, lParam=0x%lx\n", hwnd, wParam, lParam);
3519     /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3520     return (LRESULT) GETDEFIMAGELIST(TOOLBAR_GetInfoPtr(hwnd), wParam);
3521 }
3522
3523
3524 static LRESULT
3525 TOOLBAR_GetInsertMark (HWND hwnd, WPARAM wParam, LPARAM lParam)
3526 {
3527     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3528     TBINSERTMARK *lptbim = (TBINSERTMARK*)lParam;
3529
3530     TRACE("hwnd = %p, lptbim = %p\n", hwnd, lptbim);
3531
3532     *lptbim = infoPtr->tbim;
3533
3534     return 0;
3535 }
3536
3537
3538 static LRESULT
3539 TOOLBAR_GetInsertMarkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
3540 {
3541     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3542
3543     TRACE("hwnd = %p\n", hwnd);
3544
3545     return (LRESULT)infoPtr->clrInsertMark;
3546 }
3547
3548
3549 static LRESULT
3550 TOOLBAR_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
3551 {
3552     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3553     TBUTTON_INFO *btnPtr;
3554     LPRECT     lpRect;
3555     INT        nIndex;
3556
3557     nIndex = (INT)wParam;
3558     btnPtr = &infoPtr->buttons[nIndex];
3559     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3560         return FALSE;
3561     lpRect = (LPRECT)lParam;
3562     if (lpRect == NULL)
3563         return FALSE;
3564     if (btnPtr->fsState & TBSTATE_HIDDEN)
3565         return FALSE;
3566
3567     lpRect->left   = btnPtr->rect.left;
3568     lpRect->right  = btnPtr->rect.right;
3569     lpRect->bottom = btnPtr->rect.bottom;
3570     lpRect->top    = btnPtr->rect.top;
3571
3572     return TRUE;
3573 }
3574
3575
3576 static LRESULT
3577 TOOLBAR_GetMaxSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
3578 {
3579     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3580     LPSIZE lpSize = (LPSIZE)lParam;
3581
3582     if (lpSize == NULL)
3583         return FALSE;
3584
3585     lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3586     lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3587
3588     TRACE("maximum size %d x %d\n",
3589            infoPtr->rcBound.right - infoPtr->rcBound.left,
3590            infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3591
3592     return TRUE;
3593 }
3594
3595
3596 /* << TOOLBAR_GetObject >> */
3597
3598
3599 static LRESULT
3600 TOOLBAR_GetPadding (HWND hwnd)
3601 {
3602     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3603     DWORD oldPad;
3604
3605     oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3606     return (LRESULT) oldPad;
3607 }
3608
3609
3610 static LRESULT
3611 TOOLBAR_GetRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
3612 {
3613     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3614     TBUTTON_INFO *btnPtr;
3615     LPRECT     lpRect;
3616     INT        nIndex;
3617
3618     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3619     btnPtr = &infoPtr->buttons[nIndex];
3620     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3621         return FALSE;
3622     lpRect = (LPRECT)lParam;
3623     if (lpRect == NULL)
3624         return FALSE;
3625
3626     lpRect->left   = btnPtr->rect.left;
3627     lpRect->right  = btnPtr->rect.right;
3628     lpRect->bottom = btnPtr->rect.bottom;
3629     lpRect->top    = btnPtr->rect.top;
3630
3631     return TRUE;
3632 }
3633
3634
3635 static LRESULT
3636 TOOLBAR_GetRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
3637 {
3638     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3639
3640     return infoPtr->nRows;
3641 }
3642
3643
3644 static LRESULT
3645 TOOLBAR_GetState (HWND hwnd, WPARAM wParam, LPARAM lParam)
3646 {
3647     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3648     INT nIndex;
3649
3650     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3651     if (nIndex == -1)
3652         return -1;
3653
3654     return infoPtr->buttons[nIndex].fsState;
3655 }
3656
3657
3658 static LRESULT
3659 TOOLBAR_GetStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
3660 {
3661     return GetWindowLongW(hwnd, GWL_STYLE);
3662 }
3663
3664
3665 static LRESULT
3666 TOOLBAR_GetTextRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
3667 {
3668     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3669
3670     return infoPtr->nMaxTextRows;
3671 }
3672
3673
3674 static LRESULT
3675 TOOLBAR_GetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
3676 {
3677     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3678
3679     if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3680         TOOLBAR_TooltipCreateControl(infoPtr);
3681     return (LRESULT)infoPtr->hwndToolTip;
3682 }
3683
3684
3685 static LRESULT
3686 TOOLBAR_GetUnicodeFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
3687 {
3688     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3689
3690     TRACE("%s hwnd=%p\n",
3691            infoPtr->bUnicode ? "TRUE" : "FALSE", hwnd);
3692
3693     return infoPtr->bUnicode;
3694 }
3695
3696
3697 static inline LRESULT
3698 TOOLBAR_GetVersion (HWND hwnd)
3699 {
3700     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3701     return infoPtr->iVersion;
3702 }
3703
3704
3705 static LRESULT
3706 TOOLBAR_HideButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3707 {
3708     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3709     TBUTTON_INFO *btnPtr;
3710     INT nIndex;
3711
3712     TRACE("\n");
3713
3714     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3715     if (nIndex == -1)
3716         return FALSE;
3717
3718     btnPtr = &infoPtr->buttons[nIndex];
3719     if (LOWORD(lParam) == FALSE)
3720         btnPtr->fsState &= ~TBSTATE_HIDDEN;
3721     else
3722         btnPtr->fsState |= TBSTATE_HIDDEN;
3723
3724     TOOLBAR_LayoutToolbar (hwnd);
3725
3726     InvalidateRect (hwnd, NULL, TRUE);
3727
3728     return TRUE;
3729 }
3730
3731
3732 static inline LRESULT
3733 TOOLBAR_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
3734 {
3735     return TOOLBAR_InternalHitTest (hwnd, (LPPOINT)lParam);
3736 }
3737
3738
3739 static LRESULT
3740 TOOLBAR_Indeterminate (HWND hwnd, WPARAM wParam, LPARAM lParam)
3741 {
3742     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3743     TBUTTON_INFO *btnPtr;
3744     INT nIndex;
3745     DWORD oldState;
3746
3747     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3748     if (nIndex == -1)
3749         return FALSE;
3750
3751     btnPtr = &infoPtr->buttons[nIndex];
3752     oldState = btnPtr->fsState;
3753     if (LOWORD(lParam) == FALSE)
3754         btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3755     else
3756         btnPtr->fsState |= TBSTATE_INDETERMINATE;
3757
3758     if(oldState != btnPtr->fsState)
3759         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3760
3761     return TRUE;
3762 }
3763
3764
3765 static LRESULT
3766 TOOLBAR_InsertButtonT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
3767 {
3768     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3769     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3770     INT nIndex = (INT)wParam;
3771
3772     if (lpTbb == NULL)
3773         return FALSE;
3774
3775     TOOLBAR_DumpButton(infoPtr, (TBUTTON_INFO *)lpTbb, nIndex, FALSE);
3776
3777     if (nIndex == -1) {
3778        /* EPP: this seems to be an undocumented call (from my IE4)
3779         * I assume in that case that:
3780         * - index of insertion is at the end of existing buttons
3781         * I only see this happen with nIndex == -1, but it could have a special
3782         * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3783         */
3784         nIndex = infoPtr->nNumButtons;
3785
3786     } else if (nIndex < 0)
3787        return FALSE;
3788
3789     TRACE("inserting button index=%d\n", nIndex);
3790     if (nIndex > infoPtr->nNumButtons) {
3791         nIndex = infoPtr->nNumButtons;
3792         TRACE("adjust index=%d\n", nIndex);
3793     }
3794
3795     infoPtr->nNumButtons++;
3796     infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO) * infoPtr->nNumButtons);
3797     memmove(&infoPtr->buttons[nIndex+1], &infoPtr->buttons[nIndex],
3798             (infoPtr->nNumButtons - nIndex - 1) * sizeof(TBUTTON_INFO));
3799
3800     /* insert new button */
3801     infoPtr->buttons[nIndex].iBitmap   = lpTbb->iBitmap;
3802     infoPtr->buttons[nIndex].idCommand = lpTbb->idCommand;
3803     infoPtr->buttons[nIndex].fsState   = lpTbb->fsState;
3804     infoPtr->buttons[nIndex].fsStyle   = lpTbb->fsStyle;
3805     infoPtr->buttons[nIndex].dwData    = lpTbb->dwData;
3806     /* if passed string and not index, then add string */
3807     if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) {
3808         if (fUnicode)
3809             Str_SetPtrW((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPWSTR)lpTbb->iString);
3810         else
3811             Str_SetPtrAtoW((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPCSTR )lpTbb->iString);
3812     }
3813     else
3814         infoPtr->buttons[nIndex].iString   = lpTbb->iString;
3815
3816     TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[nIndex]);
3817
3818     if (infoPtr->nNumStrings > 0)
3819         TOOLBAR_CalcToolbar(hwnd);
3820     else
3821         TOOLBAR_LayoutToolbar(hwnd);
3822     TOOLBAR_AutoSize (hwnd);
3823
3824     InvalidateRect (hwnd, NULL, TRUE);
3825
3826     return TRUE;
3827 }
3828
3829 /* << TOOLBAR_InsertMarkHitTest >> */
3830
3831
3832 static LRESULT
3833 TOOLBAR_IsButtonChecked (HWND hwnd, WPARAM wParam, LPARAM lParam)
3834 {
3835     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3836     INT nIndex;
3837
3838     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3839     if (nIndex == -1)
3840         return -1;
3841
3842     return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3843 }
3844
3845
3846 static LRESULT
3847 TOOLBAR_IsButtonEnabled (HWND hwnd, WPARAM wParam, LPARAM lParam)
3848 {
3849     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3850     INT nIndex;
3851
3852     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3853     if (nIndex == -1)
3854         return -1;
3855
3856     return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3857 }
3858
3859
3860 static LRESULT
3861 TOOLBAR_IsButtonHidden (HWND hwnd, WPARAM wParam, LPARAM lParam)
3862 {
3863     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3864     INT nIndex;
3865
3866     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3867     if (nIndex == -1)
3868         return -1;
3869
3870     return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3871 }
3872
3873
3874 static LRESULT
3875 TOOLBAR_IsButtonHighlighted (HWND hwnd, WPARAM wParam, LPARAM lParam)
3876 {
3877     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3878     INT nIndex;
3879
3880     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3881     if (nIndex == -1)
3882         return -1;
3883
3884     return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3885 }
3886
3887
3888 static LRESULT
3889 TOOLBAR_IsButtonIndeterminate (HWND hwnd, WPARAM wParam, LPARAM lParam)
3890 {
3891     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3892     INT nIndex;
3893
3894     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3895     if (nIndex == -1)
3896         return -1;
3897
3898     return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3899 }
3900
3901
3902 static LRESULT
3903 TOOLBAR_IsButtonPressed (HWND hwnd, WPARAM wParam, LPARAM lParam)
3904 {
3905     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3906     INT nIndex;
3907
3908     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3909     if (nIndex == -1)
3910         return -1;
3911
3912     return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3913 }
3914
3915
3916 static LRESULT
3917 TOOLBAR_LoadImages (HWND hwnd, WPARAM wParam, LPARAM lParam)
3918 {
3919     TBADDBITMAP tbab;
3920     tbab.hInst = (HINSTANCE)lParam;
3921     tbab.nID = (UINT_PTR)wParam;
3922
3923     TRACE("hwnd = %p, hInst = %p, nID = %lu\n", hwnd, tbab.hInst, tbab.nID);
3924
3925     return TOOLBAR_AddBitmap(hwnd, 0, (LPARAM)&tbab);
3926 }
3927
3928
3929 static LRESULT
3930 TOOLBAR_MapAccelerator (HWND hwnd, WPARAM wParam, LPARAM lParam)
3931 {
3932     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3933     WCHAR wAccel = (WCHAR)wParam;
3934     UINT* pIDButton = (UINT*)lParam;
3935     WCHAR wszAccel[] = {'&',wAccel,0};
3936     int i;
3937     
3938     TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3939         hwnd, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3940     
3941     for (i = 0; i < infoPtr->nNumButtons; i++)
3942     {
3943         TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3944         if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3945             !(btnPtr->fsState & TBSTATE_HIDDEN))
3946         {
3947             int iLen = strlenW(wszAccel);
3948             LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3949             
3950             if (!lpszStr)
3951                 continue;
3952
3953             while (*lpszStr)
3954             {
3955                 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3956                 {
3957                     lpszStr += 2;
3958                     continue;
3959                 }
3960                 if (!strncmpiW(lpszStr, wszAccel, iLen))
3961                 {
3962                     *pIDButton = btnPtr->idCommand;
3963                     return TRUE;
3964                 }
3965                 lpszStr++;
3966             }
3967         }
3968     }
3969     return FALSE;
3970 }
3971
3972
3973 static LRESULT
3974 TOOLBAR_MarkButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3975 {
3976     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3977     INT nIndex;
3978     DWORD oldState;
3979     TBUTTON_INFO *btnPtr;
3980
3981     TRACE("hwnd = %p, wParam = %ld, lParam = 0x%08lx\n", hwnd, wParam, lParam);
3982
3983     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3984     if (nIndex == -1)
3985         return FALSE;
3986
3987     btnPtr = &infoPtr->buttons[nIndex];
3988     oldState = btnPtr->fsState;
3989
3990     if (LOWORD(lParam))
3991         btnPtr->fsState |= TBSTATE_MARKED;
3992     else
3993         btnPtr->fsState &= ~TBSTATE_MARKED;
3994
3995     if(oldState != btnPtr->fsState)
3996         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3997
3998     return TRUE;
3999 }
4000
4001
4002 /* fixes up an index of a button affected by a move */
4003 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
4004 {
4005     if (bMoveUp)
4006     {
4007         if (*pIndex > nIndex && *pIndex <= nMoveIndex)
4008             (*pIndex)--;
4009         else if (*pIndex == nIndex)
4010             *pIndex = nMoveIndex;
4011     }
4012     else
4013     {
4014         if (*pIndex >= nMoveIndex && *pIndex < nIndex)
4015             (*pIndex)++;
4016         else if (*pIndex == nIndex)
4017             *pIndex = nMoveIndex;
4018     }
4019 }
4020
4021
4022 static LRESULT
4023 TOOLBAR_MoveButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
4024 {
4025     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4026     INT nIndex;
4027     INT nCount;
4028     INT nMoveIndex = (INT)lParam;
4029     TBUTTON_INFO button;
4030
4031     TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", hwnd, wParam, lParam);
4032
4033     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, TRUE);
4034     if ((nIndex == -1) || (nMoveIndex < 0))
4035         return FALSE;
4036
4037     if (nMoveIndex > infoPtr->nNumButtons - 1)
4038         nMoveIndex = infoPtr->nNumButtons - 1;
4039
4040     button = infoPtr->buttons[nIndex];
4041
4042     /* move button right */
4043     if (nIndex < nMoveIndex)
4044     {
4045         nCount = nMoveIndex - nIndex;
4046         memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
4047         infoPtr->buttons[nMoveIndex] = button;
4048
4049         TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
4050         TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
4051         TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
4052         TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
4053     }
4054     else if (nIndex > nMoveIndex) /* move button left */
4055     {
4056         nCount = nIndex - nMoveIndex;
4057         memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
4058         infoPtr->buttons[nMoveIndex] = button;
4059
4060         TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
4061         TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
4062         TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
4063         TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
4064     }
4065
4066     TOOLBAR_LayoutToolbar(hwnd);
4067     TOOLBAR_AutoSize(hwnd);
4068     InvalidateRect(hwnd, NULL, TRUE);
4069
4070     return TRUE;
4071 }
4072
4073
4074 static LRESULT
4075 TOOLBAR_PressButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
4076 {
4077     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4078     TBUTTON_INFO *btnPtr;
4079     INT nIndex;
4080     DWORD oldState;
4081
4082     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
4083     if (nIndex == -1)
4084         return FALSE;
4085
4086     btnPtr = &infoPtr->buttons[nIndex];
4087     oldState = btnPtr->fsState;
4088     if (LOWORD(lParam) == FALSE)
4089         btnPtr->fsState &= ~TBSTATE_PRESSED;
4090     else
4091         btnPtr->fsState |= TBSTATE_PRESSED;
4092
4093     if(oldState != btnPtr->fsState)
4094         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
4095
4096     return TRUE;
4097 }
4098
4099 /* FIXME: there might still be some confusion her between number of buttons
4100  * and number of bitmaps */
4101 static LRESULT
4102 TOOLBAR_ReplaceBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
4103 {
4104     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4105     LPTBREPLACEBITMAP lpReplace = (LPTBREPLACEBITMAP) lParam;
4106     HBITMAP hBitmap;
4107     int i = 0, nOldButtons = 0, pos = 0;
4108     int nOldBitmaps, nNewBitmaps = 0;
4109     HIMAGELIST himlDef = 0;
4110
4111     TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4112           lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4113           lpReplace->nButtons);
4114
4115     if (lpReplace->hInstOld == HINST_COMMCTRL)
4116     {
4117         FIXME("changing standard bitmaps not implemented\n");
4118         return FALSE;
4119     }
4120     else if (lpReplace->hInstOld != 0)
4121         FIXME("resources not in the current module not implemented\n");
4122
4123     TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4124     for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4125         TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4126         TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4127         if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4128         {
4129             TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4130             nOldButtons = tbi->nButtons;
4131             tbi->nButtons = lpReplace->nButtons;
4132             tbi->hInst = lpReplace->hInstNew;
4133             tbi->nID = lpReplace->nIDNew;
4134             TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4135             break;
4136         }
4137         pos += tbi->nButtons;
4138     }
4139
4140     if (nOldButtons == 0)
4141     {
4142         WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4143         return FALSE;
4144     }
4145     
4146     /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4147     * bitmap
4148     */
4149     if (lpReplace->hInstNew)
4150         hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4151     else
4152         hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4153
4154     himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4155     nOldBitmaps = ImageList_GetImageCount(himlDef);
4156
4157     /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4158
4159     for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4160         ImageList_Remove(himlDef, i);
4161
4162     if (hBitmap)
4163     {
4164        ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4165        nNewBitmaps = ImageList_GetImageCount(himlDef);
4166        DeleteObject(hBitmap);
4167     }
4168
4169     infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4170
4171     TRACE(" pos %d  %d old bitmaps replaced by %d new ones.\n",
4172             pos, nOldBitmaps, nNewBitmaps);
4173
4174     InvalidateRect(hwnd, NULL, TRUE);
4175     return TRUE;
4176 }
4177
4178
4179 /* helper for TOOLBAR_SaveRestoreW */
4180 static BOOL
4181 TOOLBAR_Save(const TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4182 {
4183     FIXME("save to %s %s\n", debugstr_w(lpSave->pszSubKey),
4184         debugstr_w(lpSave->pszValueName));
4185
4186     return FALSE;
4187 }
4188
4189
4190 /* helper for TOOLBAR_Restore */
4191 static void
4192 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4193 {
4194     INT i;
4195
4196     for (i = 0; i < infoPtr->nNumButtons; i++)
4197     {
4198         TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4199     }
4200
4201     Free(infoPtr->buttons);
4202     infoPtr->buttons = NULL;
4203     infoPtr->nNumButtons = 0;
4204 }
4205
4206
4207 /* helper for TOOLBAR_SaveRestoreW */
4208 static BOOL
4209 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4210 {
4211     LONG res;
4212     HKEY hkey = NULL;
4213     BOOL ret = FALSE;
4214     DWORD dwType;
4215     DWORD dwSize = 0;
4216     NMTBRESTORE nmtbr;
4217
4218     /* restore toolbar information */
4219     TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4220         debugstr_w(lpSave->pszValueName));
4221
4222     memset(&nmtbr, 0, sizeof(nmtbr));
4223
4224     res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4225         KEY_QUERY_VALUE, &hkey);
4226     if (!res)
4227         res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4228             NULL, &dwSize);
4229     if (!res && dwType != REG_BINARY)
4230         res = ERROR_FILE_NOT_FOUND;
4231     if (!res)
4232     {
4233         nmtbr.pData = Alloc(dwSize);
4234         nmtbr.cbData = (UINT)dwSize;
4235         if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4236     }
4237     if (!res)
4238         res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4239             (LPBYTE)nmtbr.pData, &dwSize);
4240     if (!res)
4241     {
4242         nmtbr.pCurrent = nmtbr.pData;
4243         nmtbr.iItem = -1;
4244         nmtbr.cbBytesPerRecord = sizeof(DWORD);
4245         nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4246
4247         if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4248         {
4249             INT i;
4250
4251             /* remove all existing buttons as this function is designed to
4252              * restore the toolbar to a previously saved state */
4253             TOOLBAR_DeleteAllButtons(infoPtr);
4254
4255             for (i = 0; i < nmtbr.cButtons; i++)
4256             {
4257                 nmtbr.iItem = i;
4258                 nmtbr.tbButton.iBitmap = -1;
4259                 nmtbr.tbButton.fsState = 0;
4260                 nmtbr.tbButton.fsStyle = 0;
4261                 nmtbr.tbButton.idCommand = 0;
4262                 if (*nmtbr.pCurrent == (DWORD)-1)
4263                 {
4264                     /* separator */
4265                     nmtbr.tbButton.fsStyle = TBSTYLE_SEP;
4266                     nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4267                 }
4268                 else if (*nmtbr.pCurrent == (DWORD)-2)
4269                     /* hidden button */
4270                     nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4271                 else
4272                     nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4273
4274                 nmtbr.pCurrent++;
4275                 
4276                 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4277
4278                 /* can't contain real string as we don't know whether
4279                  * the client put an ANSI or Unicode string in there */
4280                 if (HIWORD(nmtbr.tbButton.iString))
4281                     nmtbr.tbButton.iString = 0;
4282
4283                 TOOLBAR_InsertButtonT(infoPtr->hwndSelf, -1,
4284                     (LPARAM)&nmtbr.tbButton, TRUE);
4285             }
4286
4287             /* do legacy notifications */
4288             if (infoPtr->iVersion < 5)
4289             {
4290                 /* FIXME: send TBN_BEGINADJUST */
4291                 FIXME("send TBN_GETBUTTONINFO for each button\n");
4292                 /* FIXME: send TBN_ENDADJUST */
4293             }
4294
4295             /* remove all uninitialised buttons
4296              * note: loop backwards to avoid having to fixup i on a
4297              * delete */
4298             for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4299                 if (infoPtr->buttons[i].iBitmap == -1)
4300                     TOOLBAR_DeleteButton(infoPtr->hwndSelf, i, 0);
4301
4302             /* only indicate success if at least one button survived */
4303             if (infoPtr->nNumButtons > 0) ret = TRUE;
4304         }
4305     }
4306     Free (nmtbr.pData);
4307     RegCloseKey(hkey);
4308
4309     return ret;
4310 }
4311
4312
4313 static LRESULT
4314 TOOLBAR_SaveRestoreW (HWND hwnd, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4315 {
4316     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4317
4318     if (lpSave == NULL) return 0;
4319
4320     if (wParam)
4321         return TOOLBAR_Save(infoPtr, lpSave);
4322     else
4323         return TOOLBAR_Restore(infoPtr, lpSave);
4324 }
4325
4326
4327 static LRESULT
4328 TOOLBAR_SaveRestoreA (HWND hwnd, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4329 {
4330     LPWSTR pszValueName = 0, pszSubKey = 0;
4331     TBSAVEPARAMSW SaveW;
4332     LRESULT result = 0;
4333     int len;
4334
4335     if (lpSave == NULL) return 0;
4336
4337     len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4338     pszSubKey = Alloc(len * sizeof(WCHAR));
4339     if (pszSubKey) goto exit;
4340     MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4341
4342     len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4343     pszValueName = Alloc(len * sizeof(WCHAR));
4344     if (!pszValueName) goto exit;
4345     MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4346
4347     SaveW.pszValueName = pszValueName;
4348     SaveW.pszSubKey = pszSubKey;
4349     SaveW.hkr = lpSave->hkr;
4350     result = TOOLBAR_SaveRestoreW(hwnd, wParam, &SaveW);
4351
4352 exit:
4353     Free (pszValueName);
4354     Free (pszSubKey);
4355
4356     return result;
4357 }
4358
4359
4360 static LRESULT
4361 TOOLBAR_SetAnchorHighlight (HWND hwnd, WPARAM wParam)
4362 {
4363     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4364     BOOL bOldAnchor = infoPtr->bAnchor;
4365
4366     TRACE("hwnd=%p, bAnchor = %s\n", hwnd, wParam ? "TRUE" : "FALSE");
4367
4368     infoPtr->bAnchor = (BOOL)wParam;
4369
4370     /* Native does not remove the hot effect from an already hot button */
4371
4372     return (LRESULT)bOldAnchor;
4373 }
4374
4375
4376 static LRESULT
4377 TOOLBAR_SetBitmapSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
4378 {
4379     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4380     HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4381
4382     TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", hwnd, wParam, lParam);
4383
4384     if (wParam != 0)
4385         FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4386
4387     if (LOWORD(lParam) == 0)
4388         lParam = MAKELPARAM(1, HIWORD(lParam));
4389
4390     if (HIWORD(lParam)==0)
4391         lParam = MAKELPARAM(LOWORD(lParam), 1);
4392
4393     if (infoPtr->nNumButtons > 0)
4394         WARN("%d buttons, undoc increase to bitmap size : %d-%d -> %d-%d\n",
4395              infoPtr->nNumButtons,
4396              infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
4397              LOWORD(lParam), HIWORD(lParam));
4398
4399     infoPtr->nBitmapWidth = (INT)LOWORD(lParam);
4400     infoPtr->nBitmapHeight = (INT)HIWORD(lParam);
4401
4402     if ((himlDef == infoPtr->himlInt) &&
4403         (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4404     {
4405         ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4406             infoPtr->nBitmapHeight);
4407     }
4408
4409     TOOLBAR_CalcToolbar(hwnd);
4410     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4411     return TRUE;
4412 }
4413
4414
4415 static LRESULT
4416 TOOLBAR_SetButtonInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam)
4417 {
4418     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4419     LPTBBUTTONINFOA lptbbi = (LPTBBUTTONINFOA)lParam;
4420     TBUTTON_INFO *btnPtr;
4421     INT nIndex;
4422     RECT oldBtnRect;
4423
4424     if (lptbbi == NULL)
4425         return FALSE;
4426     if (lptbbi->cbSize < sizeof(TBBUTTONINFOA))
4427         return FALSE;
4428
4429     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
4430                                      lptbbi->dwMask & 0x80000000);
4431     if (nIndex == -1)
4432         return FALSE;
4433
4434     btnPtr = &infoPtr->buttons[nIndex];
4435     if (lptbbi->dwMask & TBIF_COMMAND)
4436         btnPtr->idCommand = lptbbi->idCommand;
4437     if (lptbbi->dwMask & TBIF_IMAGE)
4438         btnPtr->iBitmap = lptbbi->iImage;
4439     if (lptbbi->dwMask & TBIF_LPARAM)
4440         btnPtr->dwData = lptbbi->lParam;
4441     if (lptbbi->dwMask & TBIF_SIZE)
4442         btnPtr->cx = lptbbi->cx;
4443     if (lptbbi->dwMask & TBIF_STATE)
4444         btnPtr->fsState = lptbbi->fsState;
4445     if (lptbbi->dwMask & TBIF_STYLE)
4446         btnPtr->fsStyle = lptbbi->fsStyle;
4447
4448     if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4449         if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1))
4450             /* iString is index, zero it to make Str_SetPtr succeed */
4451             btnPtr->iString=0;
4452
4453          Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
4454     }
4455
4456     /* save the button rect to see if we need to redraw the whole toolbar */
4457     oldBtnRect = btnPtr->rect;
4458     TOOLBAR_CalcToolbar(hwnd);
4459
4460     if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4461         InvalidateRect(hwnd, NULL, TRUE);
4462     else
4463         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
4464
4465     return TRUE;
4466 }
4467
4468
4469 static LRESULT
4470 TOOLBAR_SetButtonInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam)
4471 {
4472     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4473     LPTBBUTTONINFOW lptbbi = (LPTBBUTTONINFOW)lParam;
4474     TBUTTON_INFO *btnPtr;
4475     INT nIndex;
4476     RECT oldBtnRect;
4477
4478     if (lptbbi == NULL)
4479         return FALSE;
4480     if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4481         return FALSE;
4482
4483     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
4484                                      lptbbi->dwMask & 0x80000000);
4485     if (nIndex == -1)
4486         return FALSE;
4487
4488     btnPtr = &infoPtr->buttons[nIndex];
4489     if (lptbbi->dwMask & TBIF_COMMAND)
4490         btnPtr->idCommand = lptbbi->idCommand;
4491     if (lptbbi->dwMask & TBIF_IMAGE)
4492         btnPtr->iBitmap = lptbbi->iImage;
4493     if (lptbbi->dwMask & TBIF_LPARAM)
4494         btnPtr->dwData = lptbbi->lParam;
4495     if (lptbbi->dwMask & TBIF_SIZE)
4496         btnPtr->cx = lptbbi->cx;
4497     if (lptbbi->dwMask & TBIF_STATE)
4498         btnPtr->fsState = lptbbi->fsState;
4499     if (lptbbi->dwMask & TBIF_STYLE)
4500         btnPtr->fsStyle = lptbbi->fsStyle;
4501
4502     if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4503         if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1))
4504             /* iString is index, zero it to make Str_SetPtr succeed */
4505             btnPtr->iString=0;
4506         Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
4507     }
4508
4509     /* save the button rect to see if we need to redraw the whole toolbar */
4510     oldBtnRect = btnPtr->rect;
4511     TOOLBAR_CalcToolbar(hwnd);
4512
4513     if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4514         InvalidateRect(hwnd, NULL, TRUE);
4515     else
4516         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
4517
4518     return TRUE;
4519 }
4520
4521
4522 static LRESULT
4523 TOOLBAR_SetButtonSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
4524 {
4525     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4526     INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4527
4528     if ((cx < 0) || (cy < 0))
4529     {
4530         ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4531         return FALSE;
4532     }
4533
4534     TRACE("%p, cx = %d, cy = %d\n", hwnd, cx, cy);
4535
4536     /* The documentation claims you can only change the button size before
4537      * any button has been added. But this is wrong.
4538      * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4539      * it to the toolbar, and it checks that the return value is nonzero - mjm
4540      * Further testing shows that we must actually perform the change too.
4541      */
4542     /*
4543      * The documentation also does not mention that if 0 is supplied for
4544      * either size, the system changes it to the default of 24 wide and
4545      * 22 high. Demonstarted in ControlSpy Toolbar. GLA 3/02
4546      */
4547     if (cx == 0) cx = 24;
4548     if (cy == 0) cx = 22;
4549     
4550     cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4551     cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4552
4553     infoPtr->nButtonWidth = cx;
4554     infoPtr->nButtonHeight = cy;
4555     
4556     infoPtr->iTopMargin = default_top_margin(infoPtr);
4557     TOOLBAR_LayoutToolbar(hwnd);
4558     return TRUE;
4559 }
4560
4561
4562 static LRESULT
4563 TOOLBAR_SetButtonWidth (HWND hwnd, WPARAM wParam, LPARAM lParam)
4564 {
4565     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4566
4567     /* if setting to current values, ignore */
4568     if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4569         (infoPtr->cxMax == (short)HIWORD(lParam))) {
4570         TRACE("matches current width, min=%d, max=%d, no recalc\n",
4571               infoPtr->cxMin, infoPtr->cxMax);
4572         return TRUE;
4573     }
4574
4575     /* save new values */
4576     infoPtr->cxMin = (short)LOWORD(lParam);
4577     infoPtr->cxMax = (short)HIWORD(lParam);
4578
4579     /* otherwise we need to recalc the toolbar and in some cases
4580        recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4581        which doesn't actually draw - GA). */
4582     TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4583         infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4584
4585     TOOLBAR_CalcToolbar (hwnd);
4586
4587     InvalidateRect (hwnd, NULL, TRUE);
4588
4589     return TRUE;
4590 }
4591
4592
4593 static LRESULT
4594 TOOLBAR_SetCmdId (HWND hwnd, WPARAM wParam, LPARAM lParam)
4595 {
4596     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4597     INT nIndex = (INT)wParam;
4598
4599     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4600         return FALSE;
4601
4602     infoPtr->buttons[nIndex].idCommand = (INT)lParam;
4603
4604     if (infoPtr->hwndToolTip) {
4605
4606         FIXME("change tool tip!\n");
4607
4608     }
4609
4610     return TRUE;
4611 }
4612
4613
4614 static LRESULT
4615 TOOLBAR_SetDisabledImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4616 {
4617     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4618     HIMAGELIST himl = (HIMAGELIST)lParam;
4619     HIMAGELIST himlTemp;
4620     INT id = 0;
4621
4622     if (infoPtr->iVersion >= 5)
4623         id = wParam;
4624
4625     himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis, 
4626         &infoPtr->cimlDis, himl, id);
4627
4628     /* FIXME: redraw ? */
4629
4630     return (LRESULT)himlTemp;
4631 }
4632
4633
4634 static LRESULT
4635 TOOLBAR_SetDrawTextFlags (HWND hwnd, WPARAM wParam, LPARAM lParam)
4636 {
4637     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4638     DWORD dwTemp;
4639
4640     TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", hwnd, (DWORD)wParam, (DWORD)lParam);
4641
4642     dwTemp = infoPtr->dwDTFlags;
4643     infoPtr->dwDTFlags =
4644         (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4645
4646     return (LRESULT)dwTemp;
4647 }
4648
4649 /* This function differs a bit from what MSDN says it does:
4650  * 1. lParam contains extended style flags to OR with current style
4651  *  (MSDN isn't clear on the OR bit)
4652  * 2. wParam appears to contain extended style flags to be reset
4653  *  (MSDN says that this parameter is reserved)
4654  */
4655 static LRESULT
4656 TOOLBAR_SetExtendedStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
4657 {
4658     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4659     DWORD dwTemp;
4660
4661     dwTemp = infoPtr->dwExStyle;
4662     infoPtr->dwExStyle &= ~wParam;
4663     infoPtr->dwExStyle |= (DWORD)lParam;
4664
4665     TRACE("new style 0x%08x\n", infoPtr->dwExStyle);
4666
4667     if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4668         FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4669               (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4670
4671     TOOLBAR_CalcToolbar (hwnd);
4672
4673     TOOLBAR_AutoSize(hwnd);
4674
4675     InvalidateRect(hwnd, NULL, TRUE);
4676
4677     return (LRESULT)dwTemp;
4678 }
4679
4680
4681 static LRESULT
4682 TOOLBAR_SetHotImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4683 {
4684     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4685     HIMAGELIST himlTemp;
4686     HIMAGELIST himl = (HIMAGELIST)lParam;
4687     INT id = 0;
4688
4689     if (infoPtr->iVersion >= 5)
4690         id = wParam;
4691
4692     TRACE("hwnd = %p, himl = %p, id = %d\n", hwnd, himl, id);
4693
4694     himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot, 
4695         &infoPtr->cimlHot, himl, id);
4696
4697     /* FIXME: redraw ? */
4698
4699     return (LRESULT)himlTemp;
4700 }
4701
4702
4703 /* Makes previous hot button no longer hot, makes the specified
4704  * button hot and sends appropriate notifications. dwReason is one or
4705  * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4706  * NOTE 1: this function does not validate nHit
4707  * NOTE 2: the name of this function is completely made up and
4708  * not based on any documentation from Microsoft. */
4709 static void
4710 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4711 {
4712     if (infoPtr->nHotItem != nHit)
4713     {
4714         NMTBHOTITEM nmhotitem;
4715         TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4716
4717         nmhotitem.dwFlags = dwReason;
4718         if(infoPtr->nHotItem >= 0)
4719         {
4720             oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4721             nmhotitem.idOld = oldBtnPtr->idCommand;
4722         }
4723         else
4724         {
4725             nmhotitem.dwFlags |= HICF_ENTERING;
4726             nmhotitem.idOld = 0;
4727         }
4728
4729         if (nHit >= 0)
4730         {
4731             btnPtr = &infoPtr->buttons[nHit];
4732             nmhotitem.idNew = btnPtr->idCommand;
4733         }
4734         else
4735         {
4736             nmhotitem.dwFlags |= HICF_LEAVING;
4737             nmhotitem.idNew = 0;
4738         }
4739
4740         /* now change the hot and invalidate the old and new buttons - if the
4741          * parent agrees */
4742         if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4743         {
4744             if (oldBtnPtr) {
4745                 oldBtnPtr->bHot = FALSE;
4746                 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4747             }
4748             /* setting disabled buttons as hot fails even if the notify contains the button id */
4749             if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4750                 btnPtr->bHot = TRUE;
4751                 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4752                 infoPtr->nHotItem = nHit;
4753             }
4754             else
4755                 infoPtr->nHotItem = -1;            
4756         }
4757     }
4758 }
4759
4760 static LRESULT
4761 TOOLBAR_SetHotItem (HWND hwnd, WPARAM wParam)
4762 {
4763     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4764     INT nOldHotItem = infoPtr->nHotItem;
4765
4766     TRACE("hwnd = %p, nHit = %d\n", hwnd, (INT)wParam);
4767
4768     if ((INT)wParam > infoPtr->nNumButtons)
4769         return infoPtr->nHotItem;
4770     
4771     if ((INT)wParam < 0)
4772         wParam = -1;
4773
4774     /* NOTE: an application can still remove the hot item even if anchor
4775      * highlighting is enabled */
4776
4777     TOOLBAR_SetHotItemEx(infoPtr, wParam, HICF_OTHER);
4778
4779     if (nOldHotItem < 0)
4780         return -1;
4781
4782     return (LRESULT)nOldHotItem;
4783 }
4784
4785
4786 static LRESULT
4787 TOOLBAR_SetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4788 {
4789     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4790     HIMAGELIST himlTemp;
4791     HIMAGELIST himl = (HIMAGELIST)lParam;
4792     INT oldButtonWidth = infoPtr->nButtonWidth;
4793     INT i, id = 0;
4794
4795     if (infoPtr->iVersion >= 5)
4796         id = wParam;
4797
4798     himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef, 
4799         &infoPtr->cimlDef, himl, id);
4800
4801     infoPtr->nNumBitmaps = 0;
4802     for (i = 0; i < infoPtr->cimlDef; i++)
4803         infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4804
4805     if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4806             &infoPtr->nBitmapHeight))
4807     {
4808         infoPtr->nBitmapWidth = 1;
4809         infoPtr->nBitmapHeight = 1;
4810     }
4811     TOOLBAR_CalcToolbar(hwnd);
4812     if (infoPtr->nButtonWidth < oldButtonWidth)
4813         TOOLBAR_SetButtonSize(hwnd, 0, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4814
4815     TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4816           hwnd, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4817           infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4818
4819     InvalidateRect(hwnd, NULL, TRUE);
4820
4821     return (LRESULT)himlTemp;
4822 }
4823
4824
4825 static LRESULT
4826 TOOLBAR_SetIndent (HWND hwnd, WPARAM wParam, LPARAM lParam)
4827 {
4828     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4829
4830     infoPtr->nIndent = (INT)wParam;
4831
4832     TRACE("\n");
4833
4834     /* process only on indent changing */
4835     if(infoPtr->nIndent != (INT)wParam)
4836     {
4837         infoPtr->nIndent = (INT)wParam;
4838         TOOLBAR_CalcToolbar (hwnd);
4839         InvalidateRect(hwnd, NULL, FALSE);
4840     }
4841
4842     return TRUE;
4843 }
4844
4845
4846 static LRESULT
4847 TOOLBAR_SetInsertMark (HWND hwnd, WPARAM wParam, LPARAM lParam)
4848 {
4849     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4850     TBINSERTMARK *lptbim = (TBINSERTMARK*)lParam;
4851
4852     TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", hwnd, lptbim->iButton, lptbim->dwFlags);
4853
4854     if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4855     {
4856         FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4857         return 0;
4858     }
4859
4860     if ((lptbim->iButton == -1) || 
4861         ((lptbim->iButton < infoPtr->nNumButtons) &&
4862          (lptbim->iButton >= 0)))
4863     {
4864         infoPtr->tbim = *lptbim;
4865         /* FIXME: don't need to update entire toolbar */
4866         InvalidateRect(hwnd, NULL, TRUE);
4867     }
4868     else
4869         ERR("Invalid button index %d\n", lptbim->iButton);
4870
4871     return 0;
4872 }
4873
4874
4875 static LRESULT
4876 TOOLBAR_SetInsertMarkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
4877 {
4878     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4879
4880     infoPtr->clrInsertMark = (COLORREF)lParam;
4881
4882     /* FIXME: don't need to update entire toolbar */
4883     InvalidateRect(hwnd, NULL, TRUE);
4884
4885     return 0;
4886 }
4887
4888
4889 static LRESULT
4890 TOOLBAR_SetMaxTextRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
4891 {
4892     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4893
4894     infoPtr->nMaxTextRows = (INT)wParam;
4895
4896     TOOLBAR_CalcToolbar(hwnd);
4897     return TRUE;
4898 }
4899
4900
4901 /* MSDN gives slightly wrong info on padding.
4902  * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4903  * 2. It is not used to create a blank area between the edge of the button
4904  *    and the text or image if TBSTYLE_LIST is set. It is used to control
4905  *    the gap between the image and text. 
4906  * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used 
4907  *    to control the bottom and right borders [with the border being
4908  *    szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4909  *    is shared evenly on both sides of the button.
4910  * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4911  */
4912 static LRESULT
4913 TOOLBAR_SetPadding (HWND hwnd, WPARAM wParam, LPARAM lParam)
4914 {
4915     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4916     DWORD  oldPad;
4917
4918     oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4919     infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4920     infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4921     TRACE("cx=%d, cy=%d\n",
4922           infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4923     return (LRESULT) oldPad;
4924 }
4925
4926
4927 static LRESULT
4928 TOOLBAR_SetParent (HWND hwnd, WPARAM wParam, LPARAM lParam)
4929 {
4930     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4931     HWND hwndOldNotify;
4932
4933     TRACE("\n");
4934
4935     hwndOldNotify = infoPtr->hwndNotify;
4936     infoPtr->hwndNotify = (HWND)wParam;
4937
4938     return (LRESULT)hwndOldNotify;
4939 }
4940
4941
4942 static LRESULT
4943 TOOLBAR_SetRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
4944 {
4945     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4946     LPRECT lprc = (LPRECT)lParam;
4947     int rows = LOWORD(wParam);
4948     BOOL bLarger = HIWORD(wParam);
4949
4950     TRACE("\n");
4951
4952     TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4953
4954     if(infoPtr->nRows != rows)
4955     {
4956         TBUTTON_INFO *btnPtr = infoPtr->buttons;
4957         int curColumn = 0; /* Current column                      */
4958         int curRow    = 0; /* Current row                         */
4959         int hidden    = 0; /* Number of hidden buttons */
4960         int seps      = 0; /* Number of separators     */
4961         int idealWrap = 0; /* Ideal wrap point         */
4962         int i;
4963         BOOL wrap;
4964
4965         /*
4966            Calculate new size and wrap points - Under windows, setrows will
4967            change the dimensions if needed to show the number of requested
4968            rows (if CCS_NORESIZE is set), or will take up the whole window
4969            (if no CCS_NORESIZE).
4970
4971            Basic algorithum - If N buttons, and y rows requested, each row
4972            contains N/y buttons.
4973
4974            FIXME: Handling of separators not obvious from testing results
4975            FIXME: Take width of window into account?
4976          */
4977
4978         /* Loop through the buttons one by one counting key items  */
4979         for (i = 0; i < infoPtr->nNumButtons; i++ )
4980         {
4981             btnPtr[i].fsState &= ~TBSTATE_WRAP;
4982             if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4983                 hidden++;
4984             else if (btnPtr[i].fsStyle & BTNS_SEP)
4985                 seps++;
4986         }
4987
4988         /* FIXME: Separators make this quite complex */
4989         if (seps) FIXME("Separators unhandled\n");
4990
4991         /* Round up so more per line, ie less rows */
4992         idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / rows;
4993
4994         /* Calculate ideal wrap point if we are allowed to grow, but cannot
4995            achieve the requested number of rows. */
4996         if (bLarger && idealWrap > 1)
4997         {
4998             int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4999             int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
5000
5001             if (resRows < rows && moreRows > rows)
5002             {
5003                 idealWrap--;
5004                 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
5005             }
5006         }
5007
5008         curColumn = curRow = 0;
5009         wrap = FALSE;
5010         TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
5011               infoPtr->nNumButtons, hidden, rows);
5012
5013         for (i = 0; i < infoPtr->nNumButtons; i++ )
5014         {
5015             if (btnPtr[i].fsState & TBSTATE_HIDDEN)
5016                 continue;
5017
5018             /* Step on, wrap if necessary or flag next to wrap */
5019             if (!wrap) {
5020                 curColumn++;
5021             } else {
5022                 wrap = FALSE;
5023                 curColumn = 1;
5024                 curRow++;
5025             }
5026
5027             if (curColumn > (idealWrap-1)) {
5028                 wrap = TRUE;
5029                 btnPtr[i].fsState |= TBSTATE_WRAP;
5030             }
5031         }
5032
5033         TRACE("Result - %d rows\n", curRow + 1);
5034
5035         /* recalculate toolbar */
5036         TOOLBAR_CalcToolbar (hwnd);
5037
5038         /* Resize if necessary (Only if NORESIZE is set - odd, but basically
5039            if NORESIZE is NOT set, then the toolbar will always be resized to
5040            take up the whole window. With it set, sizing needs to be manual. */
5041         if (infoPtr->dwStyle & CCS_NORESIZE) {
5042             SetWindowPos(hwnd, NULL, 0, 0,
5043                          infoPtr->rcBound.right - infoPtr->rcBound.left,
5044                          infoPtr->rcBound.bottom - infoPtr->rcBound.top,
5045                          SWP_NOMOVE);
5046         }
5047
5048         /* repaint toolbar */
5049         InvalidateRect(hwnd, NULL, TRUE);
5050     }
5051
5052     /* return bounding rectangle */
5053     if (lprc) {
5054         lprc->left   = infoPtr->rcBound.left;
5055         lprc->right  = infoPtr->rcBound.right;
5056         lprc->top    = infoPtr->rcBound.top;
5057         lprc->bottom = infoPtr->rcBound.bottom;
5058     }
5059
5060     return 0;
5061 }
5062
5063
5064 static LRESULT
5065 TOOLBAR_SetState (HWND hwnd, WPARAM wParam, LPARAM lParam)
5066 {
5067     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5068     TBUTTON_INFO *btnPtr;
5069     INT nIndex;
5070
5071     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
5072     if (nIndex == -1)
5073         return FALSE;
5074
5075     btnPtr = &infoPtr->buttons[nIndex];
5076
5077     /* if hidden state has changed the invalidate entire window and recalc */
5078     if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
5079         btnPtr->fsState = LOWORD(lParam);
5080         TOOLBAR_CalcToolbar (hwnd);
5081         InvalidateRect(hwnd, 0, TRUE);
5082         return TRUE;
5083     }
5084
5085     /* process state changing if current state doesn't match new state */
5086     if(btnPtr->fsState != LOWORD(lParam))
5087     {
5088         btnPtr->fsState = LOWORD(lParam);
5089         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5090     }
5091
5092     return TRUE;
5093 }
5094
5095
5096 static LRESULT
5097 TOOLBAR_SetStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
5098 {
5099     SetWindowLongW(hwnd, GWL_STYLE, lParam);
5100
5101     return TRUE;
5102 }
5103
5104
5105 static inline LRESULT
5106 TOOLBAR_SetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
5107 {
5108     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5109
5110     TRACE("hwnd=%p, hwndTooltip=%p, lParam=0x%lx\n", hwnd, (HWND)wParam, lParam);
5111
5112     infoPtr->hwndToolTip = (HWND)wParam;
5113     return 0;
5114 }
5115
5116
5117 static LRESULT
5118 TOOLBAR_SetUnicodeFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
5119 {
5120     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5121     BOOL bTemp;
5122
5123     TRACE("%s hwnd=%p\n",
5124            ((BOOL)wParam) ? "TRUE" : "FALSE", hwnd);
5125
5126     bTemp = infoPtr->bUnicode;
5127     infoPtr->bUnicode = (BOOL)wParam;
5128
5129     return bTemp;
5130 }
5131
5132
5133 static LRESULT
5134 TOOLBAR_GetColorScheme (HWND hwnd, LPCOLORSCHEME lParam)
5135 {
5136     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5137
5138     lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5139                                comctl32_color.clrBtnHighlight :
5140                                infoPtr->clrBtnHighlight;
5141     lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5142                            comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5143     return 1;
5144 }
5145
5146
5147 static LRESULT
5148 TOOLBAR_SetColorScheme (HWND hwnd, const COLORSCHEME *lParam)
5149 {
5150     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5151
5152     TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5153           lParam->clrBtnHighlight, lParam->clrBtnShadow,
5154           infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5155
5156     infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5157     infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5158     InvalidateRect(hwnd, NULL, TRUE);
5159     return 0;
5160 }
5161
5162
5163 static LRESULT
5164 TOOLBAR_SetVersion (HWND hwnd, INT iVersion)
5165 {
5166     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5167     INT iOldVersion = infoPtr->iVersion;
5168
5169     infoPtr->iVersion = iVersion;
5170
5171     if (infoPtr->iVersion >= 5)
5172         TOOLBAR_SetUnicodeFormat(hwnd, (WPARAM)TRUE, (LPARAM)0);
5173
5174     return iOldVersion;
5175 }
5176
5177
5178 static LRESULT
5179 TOOLBAR_GetStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
5180 {
5181     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5182     WORD iString = HIWORD(wParam);
5183     WORD buffersize = LOWORD(wParam);
5184     LPSTR str = (LPSTR)lParam;
5185     LRESULT ret = -1;
5186
5187     TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", hwnd, iString, buffersize, str);
5188
5189     if (iString < infoPtr->nNumStrings)
5190     {
5191         ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5192
5193         TRACE("returning %s\n", debugstr_a(str));
5194     }
5195     else
5196         WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5197
5198     return ret;
5199 }
5200
5201
5202 static LRESULT
5203 TOOLBAR_GetStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
5204 {
5205     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5206     WORD iString = HIWORD(wParam);
5207     WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5208     LPWSTR str = (LPWSTR)lParam;
5209     LRESULT ret = -1;
5210
5211     TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", hwnd, iString, LOWORD(wParam), str);
5212
5213     if (iString < infoPtr->nNumStrings)
5214     {
5215         len = min(len, strlenW(infoPtr->strings[iString]));
5216         ret = (len+1)*sizeof(WCHAR);
5217         memcpy(str, infoPtr->strings[iString], ret);
5218         str[len] = '\0';
5219
5220         TRACE("returning %s\n", debugstr_w(str));
5221     }
5222     else
5223         ERR("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5224
5225     return ret;
5226 }
5227
5228 /* UNDOCUMENTED MESSAGE: This appears to set some kind of size. Perhaps it
5229  * is the maximum size of the toolbar? */
5230 static LRESULT TOOLBAR_Unkwn45D(HWND hwnd, WPARAM wParam, LPARAM lParam)
5231 {
5232     SIZE * pSize = (SIZE*)lParam;
5233     FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5234     return 0;
5235 }
5236
5237
5238 /* UNDOCUMENTED MESSAGE: This is an extended version of the
5239  * TB_SETHOTITEM message. It allows the caller to specify a reason why the
5240  * hot item changed (rather than just the HICF_OTHER that TB_SETHOTITEM
5241  * sends). */
5242 static LRESULT
5243 TOOLBAR_Unkwn45E (HWND hwnd, WPARAM wParam, LPARAM lParam)
5244 {
5245     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5246     INT nOldHotItem = infoPtr->nHotItem;
5247
5248     TRACE("old item=%d, new item=%d, flags=%08x\n",
5249           nOldHotItem, infoPtr->nHotItem, (DWORD)lParam);
5250
5251     if ((INT) wParam < 0 || (INT)wParam > infoPtr->nNumButtons)
5252         wParam = -1;
5253
5254     /* NOTE: an application can still remove the hot item even if anchor
5255      * highlighting is enabled */
5256
5257     TOOLBAR_SetHotItemEx(infoPtr, wParam, lParam);
5258
5259     GetFocus();
5260
5261     return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5262 }
5263
5264 /* UNDOCUMENTED MESSAGE: This sets the toolbar global iListGap parameter
5265  * which controls the amount of spacing between the image and the text
5266  * of buttons for TBSTYLE_LIST toolbars. */
5267 static LRESULT TOOLBAR_Unkwn460(HWND hwnd, WPARAM wParam, LPARAM lParam)
5268 {
5269     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5270
5271     TRACE("hwnd=%p iListGap=%ld\n", hwnd, wParam);
5272     
5273     if (lParam != 0)
5274         FIXME("lParam = 0x%08lx. Please report\n", lParam);
5275     
5276     infoPtr->iListGap = (INT)wParam;
5277
5278     InvalidateRect(hwnd, NULL, TRUE);
5279
5280     return 0;
5281 }
5282
5283 /* UNDOCUMENTED MESSAGE: This returns the number of maximum number
5284  * of image lists associated with the various states. */
5285 static LRESULT TOOLBAR_Unkwn462(HWND hwnd, WPARAM wParam, LPARAM lParam)
5286 {
5287     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5288
5289     TRACE("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5290
5291     return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5292 }
5293
5294 static LRESULT
5295 TOOLBAR_Unkwn463 (HWND hwnd, WPARAM wParam, LPARAM lParam)
5296 {
5297     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5298     LPSIZE lpsize = (LPSIZE)lParam;
5299
5300     if (lpsize == NULL)
5301         return FALSE;
5302
5303     /*
5304      * Testing shows the following:
5305      *   wParam    = 0 adjust cx value
5306      *             = 1 set cy value to max size.
5307      *   lParam    pointer to SIZE structure
5308      *
5309      */
5310     TRACE("[0463] wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5311           wParam, lParam, lpsize->cx, lpsize->cy);
5312
5313     switch(wParam) {
5314     case 0:
5315         if (lpsize->cx == -1) {
5316             /* **** this is wrong, native measures each button and sets it */
5317             lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5318         }
5319         else if(HIWORD(lpsize->cx)) {
5320             RECT rc;
5321             HWND hwndParent = GetParent(hwnd);
5322
5323             GetWindowRect(hwnd, &rc);
5324             MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5325             TRACE("mapped to (%d,%d)-(%d,%d)\n",
5326                 rc.left, rc.top, rc.right, rc.bottom);
5327             lpsize->cx = max(rc.right-rc.left,
5328                              infoPtr->rcBound.right - infoPtr->rcBound.left);
5329         }
5330         else {
5331             lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5332         }
5333         break;
5334     case 1:
5335         lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5336         break;
5337     default:
5338         ERR("Unknown wParam %ld for Toolbar message [0463]. Please report\n",
5339             wParam);
5340         return 0;
5341     }
5342     TRACE("[0463] set to -> 0x%08x 0x%08x\n",
5343           lpsize->cx, lpsize->cy);
5344     return 1;
5345 }
5346
5347 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5348 {
5349     FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5350
5351     InvalidateRect(hwnd, NULL, TRUE);
5352     return 1;
5353 }
5354
5355
5356 static LRESULT
5357 TOOLBAR_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
5358 {
5359     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5360     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
5361     LOGFONTW logFont;
5362
5363     TRACE("hwnd = %p\n", hwnd);
5364
5365     /* initialize info structure */
5366     infoPtr->nButtonWidth = 23;
5367     infoPtr->nButtonHeight = 22;
5368     infoPtr->nBitmapHeight = 16;
5369     infoPtr->nBitmapWidth = 16;
5370
5371     infoPtr->nMaxTextRows = 1;
5372     infoPtr->cxMin = -1;
5373     infoPtr->cxMax = -1;
5374     infoPtr->nNumBitmaps = 0;
5375     infoPtr->nNumStrings = 0;
5376
5377     infoPtr->bCaptured = FALSE;
5378     infoPtr->nButtonDown = -1;
5379     infoPtr->nButtonDrag = -1;
5380     infoPtr->nOldHit = -1;
5381     infoPtr->nHotItem = -1;
5382     infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
5383     infoPtr->dwDTFlags = (dwStyle & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
5384     infoPtr->bAnchor = FALSE; /* no anchor highlighting */
5385     infoPtr->bDragOutSent = FALSE;
5386     infoPtr->iVersion = 0;
5387     infoPtr->hwndSelf = hwnd;
5388     infoPtr->bDoRedraw = TRUE;
5389     infoPtr->clrBtnHighlight = CLR_DEFAULT;
5390     infoPtr->clrBtnShadow = CLR_DEFAULT;
5391     infoPtr->szPadding.cx = DEFPAD_CX;
5392     infoPtr->szPadding.cy = DEFPAD_CY;
5393     infoPtr->iListGap = DEFLISTGAP;
5394     infoPtr->iTopMargin = default_top_margin(infoPtr);
5395     infoPtr->dwStyle = dwStyle;
5396     infoPtr->tbim.iButton = -1;
5397     GetClientRect(hwnd, &infoPtr->client_rect);
5398     infoPtr->bUnicode = infoPtr->hwndNotify && 
5399         (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, (LPARAM)NF_REQUERY));
5400     infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5401
5402     SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5403     infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5404     
5405     OpenThemeData (hwnd, themeClass);
5406
5407     TOOLBAR_CheckStyle (hwnd, dwStyle);
5408
5409     return 0;
5410 }
5411
5412
5413 static LRESULT
5414 TOOLBAR_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
5415 {
5416     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5417
5418     /* delete tooltip control */
5419     if (infoPtr->hwndToolTip)
5420         DestroyWindow (infoPtr->hwndToolTip);
5421
5422     /* delete temporary buffer for tooltip text */
5423     Free (infoPtr->pszTooltipText);
5424     Free (infoPtr->bitmaps);            /* bitmaps list */
5425
5426     /* delete button data */
5427     Free (infoPtr->buttons);
5428
5429     /* delete strings */
5430     if (infoPtr->strings) {
5431         INT i;
5432         for (i = 0; i < infoPtr->nNumStrings; i++)
5433             if (infoPtr->strings[i])
5434                 Free (infoPtr->strings[i]);
5435
5436         Free (infoPtr->strings);
5437     }
5438
5439     /* destroy internal image list */
5440     if (infoPtr->himlInt)
5441         ImageList_Destroy (infoPtr->himlInt);
5442
5443         TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5444         TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5445         TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5446
5447     /* delete default font */
5448     DeleteObject (infoPtr->hDefaultFont);
5449         
5450     CloseThemeData (GetWindowTheme (hwnd));
5451
5452     /* free toolbar info data */
5453     Free (infoPtr);
5454     SetWindowLongPtrW (hwnd, 0, 0);
5455
5456     return 0;
5457 }
5458
5459
5460 static LRESULT
5461 TOOLBAR_EraseBackground (HWND hwnd, WPARAM wParam, LPARAM lParam)
5462 {
5463     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5464     NMTBCUSTOMDRAW tbcd;
5465     INT ret = FALSE;
5466     DWORD ntfret;
5467     HTHEME theme = GetWindowTheme (hwnd);
5468     DWORD dwEraseCustDraw = 0;
5469
5470     /* the app has told us not to redraw the toolbar */
5471     if (!infoPtr->bDoRedraw)
5472         return FALSE;
5473
5474     if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5475         ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5476         tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5477         tbcd.nmcd.hdc = (HDC)wParam;
5478         ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5479         dwEraseCustDraw = ntfret & 0xffff;
5480
5481         /* FIXME: in general the return flags *can* be or'ed together */
5482         switch (dwEraseCustDraw)
5483             {
5484             case CDRF_DODEFAULT:
5485                 break;
5486             case CDRF_SKIPDEFAULT:
5487                 return TRUE;
5488             default:
5489                 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5490                       hwnd, ntfret);
5491             }
5492     }
5493
5494     /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5495      * to my parent for processing.
5496      */
5497     if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5498         POINT pt, ptorig;
5499         HDC hdc = (HDC)wParam;
5500         HWND parent;
5501
5502         pt.x = 0;
5503         pt.y = 0;
5504         parent = GetParent(hwnd);
5505         MapWindowPoints(hwnd, parent, &pt, 1);
5506         OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5507         ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5508         SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5509     }
5510     if (!ret)
5511         ret = DefWindowProcW (hwnd, WM_ERASEBKGND, wParam, lParam);
5512
5513     if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5514         ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5515         tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5516         tbcd.nmcd.hdc = (HDC)wParam;
5517         ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5518         dwEraseCustDraw = ntfret & 0xffff;
5519         switch (dwEraseCustDraw)
5520             {
5521             case CDRF_DODEFAULT:
5522                 break;
5523             case CDRF_SKIPDEFAULT:
5524                 return TRUE;
5525             default:
5526                 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5527                       hwnd, ntfret);
5528             }
5529     }
5530     return ret;
5531 }
5532
5533
5534 static LRESULT
5535 TOOLBAR_GetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
5536 {
5537     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5538
5539     return (LRESULT)infoPtr->hFont;
5540 }
5541
5542
5543 static void
5544 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5545 {
5546     INT i;
5547     INT nNewHotItem = infoPtr->nHotItem;
5548
5549     for (i = 0; i < infoPtr->nNumButtons; i++)
5550     {
5551         /* did we wrap? */
5552         if ((nNewHotItem + iDirection < 0) ||
5553             (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5554         {
5555             NMTBWRAPHOTITEM nmtbwhi;
5556             nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5557             nmtbwhi.iDirection = iDirection;
5558             nmtbwhi.dwReason = dwReason;
5559     
5560             if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5561                 return;
5562         }
5563
5564         nNewHotItem += iDirection;
5565         nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5566
5567         if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5568             !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5569         {
5570             TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5571             break;
5572         }
5573     }
5574 }
5575
5576 static LRESULT
5577 TOOLBAR_KeyDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
5578 {
5579     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5580     NMKEY nmkey;
5581
5582     nmkey.nVKey = (UINT)wParam;
5583     nmkey.uFlags = HIWORD(lParam);
5584
5585     if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5586         return DefWindowProcW(hwnd, WM_KEYDOWN, wParam, lParam);
5587
5588     switch ((UINT)wParam)
5589     {
5590     case VK_LEFT:
5591     case VK_UP:
5592         TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5593         break;
5594     case VK_RIGHT:
5595     case VK_DOWN:
5596         TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5597         break;
5598     case VK_SPACE:
5599     case VK_RETURN:
5600         if ((infoPtr->nHotItem >= 0) &&
5601             (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5602         {
5603             SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5604                 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5605                 (LPARAM)hwnd);
5606         }
5607         break;
5608     }
5609
5610     return 0;
5611 }
5612
5613
5614 static LRESULT
5615 TOOLBAR_LButtonDblClk (HWND hwnd, WPARAM wParam, LPARAM lParam)
5616 {
5617     POINT pt;
5618     INT   nHit;
5619
5620     pt.x = (short)LOWORD(lParam);
5621     pt.y = (short)HIWORD(lParam);
5622     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5623
5624     if (nHit >= 0)
5625         TOOLBAR_LButtonDown (hwnd, wParam, lParam);
5626     else if (GetWindowLongW (hwnd, GWL_STYLE) & CCS_ADJUSTABLE)
5627         TOOLBAR_Customize (hwnd);
5628
5629     return 0;
5630 }
5631
5632
5633 static LRESULT
5634 TOOLBAR_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
5635 {
5636     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5637     TBUTTON_INFO *btnPtr;
5638     POINT pt;
5639     INT   nHit;
5640     NMTOOLBARA nmtb;
5641     NMMOUSE nmmouse;
5642     BOOL bDragKeyPressed;
5643
5644     TRACE("\n");
5645
5646     if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5647         bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5648     else
5649         bDragKeyPressed = (wParam & MK_SHIFT);
5650
5651     if (infoPtr->hwndToolTip)
5652         TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
5653                             WM_LBUTTONDOWN, wParam, lParam);
5654
5655     pt.x = (short)LOWORD(lParam);
5656     pt.y = (short)HIWORD(lParam);
5657     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5658
5659     btnPtr = &infoPtr->buttons[nHit];
5660
5661     if ((nHit >= 0) && bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5662     {
5663         infoPtr->nButtonDrag = nHit;
5664         SetCapture (hwnd);
5665         
5666         /* If drag cursor has not been loaded, load it.
5667          * Note: it doesn't need to be freed */
5668         if (!hCursorDrag)
5669             hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5670         SetCursor(hCursorDrag);
5671     }
5672     else if (nHit >= 0)
5673     {
5674         RECT arrowRect;
5675         infoPtr->nOldHit = nHit;
5676
5677         CopyRect(&arrowRect, &btnPtr->rect);
5678         arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5679
5680         /* for EX_DRAWDDARROWS style,  click must be in the drop-down arrow rect */
5681         if ((btnPtr->fsState & TBSTATE_ENABLED) && 
5682              ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5683               ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5684                ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5685                (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5686         {
5687             LRESULT res;
5688
5689             /* draw in pressed state */
5690             if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5691                 btnPtr->fsState |= TBSTATE_PRESSED;
5692             else
5693                 btnPtr->bDropDownPressed = TRUE;
5694             RedrawWindow(hwnd,&btnPtr->rect,0,
5695                         RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5696
5697             memset(&nmtb, 0, sizeof(nmtb));
5698             nmtb.iItem = btnPtr->idCommand;
5699             nmtb.rcButton = btnPtr->rect;
5700             res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5701                                   TBN_DROPDOWN);
5702             TRACE("TBN_DROPDOWN responded with %ld\n", res);
5703
5704             if (res != TBDDRET_TREATPRESSED)
5705             {
5706                 MSG msg;
5707
5708                 /* redraw button in unpressed state */
5709                 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5710                     btnPtr->fsState &= ~TBSTATE_PRESSED;
5711                 else
5712                     btnPtr->bDropDownPressed = FALSE;
5713                 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5714
5715                 /* find and set hot item
5716                  * NOTE: native doesn't do this, but that is a bug */
5717                 GetCursorPos(&pt);
5718                 ScreenToClient(hwnd, &pt);
5719                 nHit = TOOLBAR_InternalHitTest(hwnd, &pt);
5720                 if (!infoPtr->bAnchor || (nHit >= 0))
5721                     TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5722                 
5723                 /* remove any left mouse button down or double-click messages
5724                  * so that we can get a toggle effect on the button */
5725                 while (PeekMessageW(&msg, hwnd, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5726                        PeekMessageW(&msg, hwnd, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5727                     ;
5728
5729                 return 0;
5730             }
5731             /* otherwise drop through and process as pushed */
5732         }
5733         infoPtr->bCaptured = TRUE;
5734         infoPtr->nButtonDown = nHit;
5735         infoPtr->bDragOutSent = FALSE;
5736
5737         btnPtr->fsState |= TBSTATE_PRESSED;
5738
5739         TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5740
5741         if (btnPtr->fsState & TBSTATE_ENABLED)
5742             InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5743         UpdateWindow(hwnd);
5744         SetCapture (hwnd);
5745     }
5746
5747     if (nHit >=0)
5748     {
5749         memset(&nmtb, 0, sizeof(nmtb));
5750         nmtb.iItem = btnPtr->idCommand;
5751         TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5752     }
5753
5754     nmmouse.dwHitInfo = nHit;
5755
5756     /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5757     if (nHit < 0)
5758         nmmouse.dwItemSpec = -1;
5759     else
5760     {
5761         nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5762         nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5763     }
5764
5765     ClientToScreen(hwnd, &pt); 
5766     nmmouse.pt = pt;
5767
5768     if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5769         return DefWindowProcW(hwnd, WM_LBUTTONDOWN, wParam, lParam);
5770
5771     return 0;
5772 }
5773
5774 static LRESULT
5775 TOOLBAR_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
5776 {
5777     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5778     TBUTTON_INFO *btnPtr;
5779     POINT pt;
5780     INT   nHit;
5781     INT   nOldIndex = -1;
5782     BOOL  bSendMessage = TRUE;
5783     NMHDR hdr;
5784     NMMOUSE nmmouse;
5785     NMTOOLBARA nmtb;
5786
5787     if (infoPtr->hwndToolTip)
5788         TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
5789                             WM_LBUTTONUP, wParam, lParam);
5790
5791     pt.x = (short)LOWORD(lParam);
5792     pt.y = (short)HIWORD(lParam);
5793     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5794
5795     if (!infoPtr->bAnchor || (nHit >= 0))
5796         TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5797
5798     if (infoPtr->nButtonDrag >= 0) {
5799         RECT rcClient;
5800         NMHDR hdr;
5801
5802         btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5803         ReleaseCapture();
5804         /* reset cursor */
5805         SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5806
5807         GetClientRect(hwnd, &rcClient);
5808         if (PtInRect(&rcClient, pt))
5809         {
5810             INT nButton = -1;
5811             if (nHit >= 0)
5812                 nButton = nHit;
5813             else if (nHit < -1)
5814                 nButton = -nHit;
5815             else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5816                 nButton = -nHit;
5817
5818             if (nButton == infoPtr->nButtonDrag)
5819             {
5820                 /* if the button is moved sightly left and we have a
5821                  * separator there then remove it */
5822                 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5823                 {
5824                     if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5825                         TOOLBAR_DeleteButton(hwnd, nButton - 1, 0);
5826                 }
5827                 else /* else insert a separator before the dragged button */
5828                 {
5829                     TBBUTTON tbb;
5830                     memset(&tbb, 0, sizeof(tbb));
5831                     tbb.fsStyle = BTNS_SEP;
5832                     tbb.iString = -1;
5833                     TOOLBAR_InsertButtonT(hwnd, nButton, (LPARAM)&tbb, TRUE);
5834                 }
5835             }
5836             else
5837             {
5838                 if (nButton == -1)
5839                 {
5840                     if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5841                         TOOLBAR_MoveButton(hwnd, infoPtr->nButtonDrag, 0);
5842                     else
5843                         TOOLBAR_MoveButton(hwnd, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5844                 }
5845                 else
5846                     TOOLBAR_MoveButton(hwnd, infoPtr->nButtonDrag, nButton);
5847             }
5848         }
5849         else
5850         {
5851             TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5852             TOOLBAR_DeleteButton(hwnd, (WPARAM)infoPtr->nButtonDrag, 0);
5853         }
5854
5855         /* button under cursor changed so need to re-set hot item */
5856         TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5857         infoPtr->nButtonDrag = -1;
5858
5859         TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5860     }
5861     else if (infoPtr->nButtonDown >= 0) {
5862         btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5863         btnPtr->fsState &= ~TBSTATE_PRESSED;
5864
5865         if (btnPtr->fsStyle & BTNS_CHECK) {
5866                 if (btnPtr->fsStyle & BTNS_GROUP) {
5867                     nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5868                         nHit);
5869                     if (nOldIndex == nHit)
5870                         bSendMessage = FALSE;
5871                     if ((nOldIndex != nHit) &&
5872                         (nOldIndex != -1))
5873                         infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5874                     btnPtr->fsState |= TBSTATE_CHECKED;
5875                 }
5876                 else {
5877                     if (btnPtr->fsState & TBSTATE_CHECKED)
5878                         btnPtr->fsState &= ~TBSTATE_CHECKED;
5879                     else
5880                         btnPtr->fsState |= TBSTATE_CHECKED;
5881                 }
5882         }
5883
5884         if (nOldIndex != -1)
5885             InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect, TRUE);
5886
5887         /*
5888          * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5889          * that resets bCaptured and btn TBSTATE_PRESSED flags,
5890          * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5891          */
5892         if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5893             ReleaseCapture ();
5894         infoPtr->nButtonDown = -1;
5895
5896         /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5897         TOOLBAR_SendNotify ((NMHDR *) &hdr, infoPtr,
5898                         NM_RELEASEDCAPTURE);
5899
5900         /* native issues TBN_ENDDRAG here, if _LBUTTONDOWN issued the
5901          * TBN_BEGINDRAG
5902          */
5903         memset(&nmtb, 0, sizeof(nmtb));
5904         nmtb.iItem = btnPtr->idCommand;
5905         TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5906                         TBN_ENDDRAG);
5907
5908         if (btnPtr->fsState & TBSTATE_ENABLED)
5909         {
5910             SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5911               MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)hwnd);
5912
5913             /* In case we have just been destroyed... */
5914             if(!IsWindow(hwnd))
5915                 return 0;
5916         }
5917     }
5918
5919     /* !!! Undocumented - toolbar at 4.71 level and above sends
5920     * NM_CLICK with the NMMOUSE structure. */
5921     nmmouse.dwHitInfo = nHit;
5922
5923     if (nHit < 0)
5924         nmmouse.dwItemSpec = -1;
5925     else
5926     {
5927         nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5928         nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5929     }
5930
5931     ClientToScreen(hwnd, &pt); 
5932     nmmouse.pt = pt;
5933
5934     if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5935         return DefWindowProcW(hwnd, WM_LBUTTONUP, wParam, lParam);
5936
5937     return 0;
5938 }
5939
5940 static LRESULT
5941 TOOLBAR_RButtonUp( HWND hwnd, WPARAM wParam, LPARAM lParam)
5942 {
5943     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5944     INT nHit;
5945     NMMOUSE nmmouse;
5946     POINT pt;
5947
5948     pt.x = (short)LOWORD(lParam);
5949     pt.y = (short)HIWORD(lParam);
5950
5951     nHit = TOOLBAR_InternalHitTest(hwnd, &pt);
5952     nmmouse.dwHitInfo = nHit;
5953
5954     if (nHit < 0) {
5955         nmmouse.dwItemSpec = -1;
5956     } else {
5957         nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5958         nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5959     }
5960
5961     ClientToScreen(hwnd, &pt); 
5962     nmmouse.pt = pt;
5963
5964     if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5965         return DefWindowProcW(hwnd, WM_RBUTTONUP, wParam, lParam);
5966
5967     return 0;
5968 }
5969
5970 static LRESULT
5971 TOOLBAR_RButtonDblClk( HWND hwnd, WPARAM wParam, LPARAM lParam)
5972 {
5973     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5974     NMHDR nmhdr;
5975
5976     if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5977         return DefWindowProcW(hwnd, WM_RBUTTONDBLCLK, wParam, lParam);
5978
5979     return 0;
5980 }
5981
5982 static LRESULT
5983 TOOLBAR_CaptureChanged(HWND hwnd)
5984 {
5985     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5986     TBUTTON_INFO *btnPtr;
5987
5988     infoPtr->bCaptured = FALSE;
5989
5990     if (infoPtr->nButtonDown >= 0)
5991     {
5992         btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5993         btnPtr->fsState &= ~TBSTATE_PRESSED;
5994
5995         infoPtr->nOldHit = -1;
5996
5997         if (btnPtr->fsState & TBSTATE_ENABLED)
5998             InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5999     }
6000     return 0;
6001 }
6002
6003 static LRESULT
6004 TOOLBAR_MouseLeave (HWND hwnd, WPARAM wParam, LPARAM lParam)
6005 {
6006     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6007
6008     /* don't remove hot effects when in anchor highlighting mode or when a
6009      * drop-down button is pressed */
6010     if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
6011     {
6012         TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
6013         if (!hotBtnPtr->bDropDownPressed)
6014             TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
6015     }
6016
6017     if (infoPtr->nOldHit < 0)
6018       return TRUE;
6019
6020     /* If the last button we were over is depressed then make it not */
6021     /* depressed and redraw it */
6022     if(infoPtr->nOldHit == infoPtr->nButtonDown)
6023     {
6024       TBUTTON_INFO *btnPtr;
6025       RECT rc1;
6026
6027       btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
6028
6029       btnPtr->fsState &= ~TBSTATE_PRESSED;
6030
6031       rc1 = btnPtr->rect;
6032       InflateRect (&rc1, 1, 1);
6033       InvalidateRect (hwnd, &rc1, TRUE);
6034     }
6035
6036     if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
6037     {
6038         NMTOOLBARW nmt;
6039         ZeroMemory(&nmt, sizeof(nmt));
6040         nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
6041         TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
6042         infoPtr->bDragOutSent = TRUE;
6043     }
6044
6045     infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
6046
6047     return TRUE;
6048 }
6049
6050 static LRESULT
6051 TOOLBAR_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
6052 {
6053     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6054     POINT pt;
6055     TRACKMOUSEEVENT trackinfo;
6056     INT   nHit;
6057     TBUTTON_INFO *btnPtr;
6058     
6059     if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
6060         TOOLBAR_TooltipCreateControl(infoPtr);
6061     
6062     if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
6063         /* fill in the TRACKMOUSEEVENT struct */
6064         trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
6065         trackinfo.dwFlags = TME_QUERY;
6066
6067         /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
6068         _TrackMouseEvent(&trackinfo);
6069
6070         /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
6071         if(trackinfo.hwndTrack != hwnd || !(trackinfo.dwFlags & TME_LEAVE)) {
6072             trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
6073             trackinfo.hwndTrack = hwnd;
6074
6075             /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
6076             /* and can properly deactivate the hot toolbar button */
6077             _TrackMouseEvent(&trackinfo);
6078        }
6079     }
6080
6081     if (infoPtr->hwndToolTip)
6082         TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
6083                             WM_MOUSEMOVE, wParam, lParam);
6084
6085     pt.x = (short)LOWORD(lParam);
6086     pt.y = (short)HIWORD(lParam);
6087
6088     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
6089
6090     if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
6091         && (!infoPtr->bAnchor || (nHit >= 0)))
6092         TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE);
6093
6094     if (infoPtr->nOldHit != nHit)
6095     {
6096         if (infoPtr->bCaptured)
6097         {
6098             if (!infoPtr->bDragOutSent)
6099             {
6100                 NMTOOLBARW nmt;
6101                 ZeroMemory(&nmt, sizeof(nmt));
6102                 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
6103                 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
6104                 infoPtr->bDragOutSent = TRUE;
6105             }
6106
6107             btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
6108             if (infoPtr->nOldHit == infoPtr->nButtonDown) {
6109                 btnPtr->fsState &= ~TBSTATE_PRESSED;
6110                 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
6111             }
6112             else if (nHit == infoPtr->nButtonDown) {
6113                 btnPtr->fsState |= TBSTATE_PRESSED;
6114                 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
6115             }
6116             infoPtr->nOldHit = nHit;
6117         }
6118     }
6119
6120     return 0;
6121 }
6122
6123
6124 static inline LRESULT
6125 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
6126 {
6127 /*    if (wndPtr->dwStyle & CCS_NODIVIDER) */
6128         return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
6129 /*    else */
6130 /*      return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
6131 }
6132
6133
6134 static inline LRESULT
6135 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
6136 {
6137     if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6138         ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6139
6140     return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6141 }
6142
6143
6144 static LRESULT
6145 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, LPARAM lParam)
6146 {
6147     TOOLBAR_INFO *infoPtr;
6148     LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
6149     DWORD styleadd = 0;
6150
6151     /* allocate memory for info structure */
6152     infoPtr = (TOOLBAR_INFO *)Alloc (sizeof(TOOLBAR_INFO));
6153     SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6154
6155     /* paranoid!! */
6156     infoPtr->dwStructSize = sizeof(TBBUTTON);
6157     infoPtr->nRows = 1;
6158     infoPtr->nWidth = 0;
6159
6160     /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6161     if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6162         HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6163         SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6164     }
6165
6166     /* native control does:
6167      *    Get a lot of colors and brushes
6168      *    WM_NOTIFYFORMAT
6169      *    SystemParametersInfoW(0x1f, 0x3c, adr1, 0)
6170      *    CreateFontIndirectW(adr1)
6171      *    CreateBitmap(0x27, 0x24, 1, 1, 0)
6172      *    hdc = GetDC(toolbar)
6173      *    GetSystemMetrics(0x48)
6174      *    fnt2=CreateFontW(0xe, 0, 0, 0, 0x190, 0, 0, 0, 0, 2,
6175      *                     0, 0, 0, 0, "MARLETT")
6176      *    oldfnt = SelectObject(hdc, fnt2)
6177      *    GetCharWidthW(hdc, 0x36, 0x36, adr2)
6178      *    GetTextMetricsW(hdc, adr3)
6179      *    SelectObject(hdc, oldfnt)
6180      *    DeleteObject(fnt2)
6181      *    ReleaseDC(hdc)
6182      *    InvalidateRect(toolbar, 0, 1)
6183      *    SetWindowLongW(toolbar, 0, addr)
6184      *    SetWindowLongW(toolbar, -16, xxx)  **sometimes**
6185      *                                          WM_STYLECHANGING
6186      *                             CallWinEx   old         new
6187      *                       ie 1  0x56000a4c  0x46000a4c  0x56008a4d
6188      *                       ie 2  0x4600094c  0x4600094c  0x4600894d
6189      *                       ie 3  0x56000b4c  0x46000b4c  0x56008b4d
6190      *                      rebar  0x50008844  0x40008844  0x50008845
6191      *                      pager  0x50000844  0x40000844  0x50008845
6192      *                    IC35mgr  0x5400084e  **nochange**
6193      *           on entry to _NCCREATE         0x5400084e
6194      *                    rowlist  0x5400004e  **nochange**
6195      *           on entry to _NCCREATE         0x5400004e
6196      *
6197      */
6198
6199     /* I think the code below is a bug, but it is the way that the native
6200      * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6201      * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6202      * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6203      * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6204      * Somehow, the only cases of this seem to be MFC programs.
6205      *
6206      * Note also that the addition of _TRANSPARENT occurs *only* here. It
6207      * does not occur in the WM_STYLECHANGING routine.
6208      *    (Guy Albertelli   9/2001)
6209      *
6210      */
6211     if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
6212         && !(cs->style & TBSTYLE_TRANSPARENT))
6213         styleadd |= TBSTYLE_TRANSPARENT;
6214     if (!(cs->style & (CCS_TOP | CCS_NOMOVEY))) {
6215         styleadd |= CCS_TOP;   /* default to top */
6216         SetWindowLongW (hwnd, GWL_STYLE, cs->style | styleadd);
6217     }
6218
6219     return DefWindowProcW (hwnd, WM_NCCREATE, wParam, lParam);
6220 }
6221
6222
6223 static LRESULT
6224 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6225 {
6226     DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6227     RECT rcWindow;
6228     HDC hdc;
6229
6230     if (dwStyle & WS_MINIMIZE)
6231         return 0; /* Nothing to do */
6232
6233     DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6234
6235     if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6236         return 0;
6237
6238     if (!(dwStyle & CCS_NODIVIDER))
6239     {
6240         GetWindowRect (hwnd, &rcWindow);
6241         OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6242         if( dwStyle & WS_BORDER )
6243             OffsetRect (&rcWindow, 1, 1);
6244         DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6245     }
6246
6247     ReleaseDC( hwnd, hdc );
6248
6249     return 0;
6250 }
6251
6252
6253 /* handles requests from the tooltip control on what text to display */
6254 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6255 {
6256     int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6257
6258     TRACE("button index = %d\n", index);
6259
6260     Free (infoPtr->pszTooltipText);
6261     infoPtr->pszTooltipText = NULL;
6262
6263     if (index < 0)
6264         return 0;
6265
6266     if (infoPtr->bUnicode)
6267     {
6268         WCHAR wszBuffer[INFOTIPSIZE+1];
6269         NMTBGETINFOTIPW tbgit;
6270         unsigned int len; /* in chars */
6271
6272         wszBuffer[0] = '\0';
6273         wszBuffer[INFOTIPSIZE] = '\0';
6274
6275         tbgit.pszText = wszBuffer;
6276         tbgit.cchTextMax = INFOTIPSIZE;
6277         tbgit.iItem = lpnmtdi->hdr.idFrom;
6278         tbgit.lParam = infoPtr->buttons[index].dwData;
6279
6280         TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6281
6282         TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6283
6284         len = strlenW(tbgit.pszText);
6285         if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6286         {
6287             /* need to allocate temporary buffer in infoPtr as there
6288              * isn't enough space in buffer passed to us by the
6289              * tooltip control */
6290             infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6291             if (infoPtr->pszTooltipText)
6292             {
6293                 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6294                 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6295                 return 0;
6296             }
6297         }
6298         else if (len > 0)
6299         {
6300             memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6301             return 0;
6302         }
6303     }
6304     else
6305     {
6306         CHAR szBuffer[INFOTIPSIZE+1];
6307         NMTBGETINFOTIPA tbgit;
6308         unsigned int len; /* in chars */
6309
6310         szBuffer[0] = '\0';
6311         szBuffer[INFOTIPSIZE] = '\0';
6312
6313         tbgit.pszText = szBuffer;
6314         tbgit.cchTextMax = INFOTIPSIZE;
6315         tbgit.iItem = lpnmtdi->hdr.idFrom;
6316         tbgit.lParam = infoPtr->buttons[index].dwData;
6317
6318         TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6319
6320         TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6321
6322         len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6323         if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6324         {
6325             /* need to allocate temporary buffer in infoPtr as there
6326              * isn't enough space in buffer passed to us by the
6327              * tooltip control */
6328             infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6329             if (infoPtr->pszTooltipText)
6330             {
6331                 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6332                 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6333                 return 0;
6334             }
6335         }
6336         else if (len > 0)
6337         {
6338             MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6339                                 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6340             return 0;
6341         }
6342     }
6343
6344     /* if button has text, but it is not shown then automatically
6345      * use that text as tooltip */
6346     if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6347         !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6348     {
6349         LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6350         unsigned int len = pszText ? strlenW(pszText) : 0;
6351
6352         TRACE("using button hidden text %s\n", debugstr_w(pszText));
6353
6354         if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6355         {
6356             /* need to allocate temporary buffer in infoPtr as there
6357              * isn't enough space in buffer passed to us by the
6358              * tooltip control */
6359             infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6360             if (infoPtr->pszTooltipText)
6361             {
6362                 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6363                 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6364                 return 0;
6365             }
6366         }
6367         else if (len > 0)
6368         {
6369             memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6370             return 0;
6371         }
6372     }
6373
6374     TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6375
6376     /* last resort: send notification on to app */
6377     /* FIXME: find out what is really used here */
6378     return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)lpnmtdi);
6379 }
6380
6381
6382 static inline LRESULT
6383 TOOLBAR_Notify (HWND hwnd, WPARAM wParam, LPARAM lParam)
6384 {
6385     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6386     LPNMHDR lpnmh = (LPNMHDR)lParam;
6387
6388     switch (lpnmh->code)
6389     {
6390     case PGN_CALCSIZE:
6391     {
6392         LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
6393
6394         if (lppgc->dwFlag == PGF_CALCWIDTH) {
6395             lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6396             TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6397                   lppgc->iWidth);
6398         }
6399         else {
6400             lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6401             TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6402                   lppgc->iHeight);
6403         }
6404         return 0;
6405     }
6406
6407     case PGN_SCROLL:
6408     {
6409         LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lParam;
6410
6411         lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6412                           infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6413         TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6414               lppgs->iScroll, lppgs->iDir);
6415         return 0;
6416     }
6417
6418     case TTN_GETDISPINFOW:
6419         return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lParam);
6420
6421     case TTN_GETDISPINFOA:
6422         FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6423         return 0;
6424
6425     default:
6426         return 0;
6427     }
6428 }
6429
6430
6431 static LRESULT
6432 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6433 {
6434     LRESULT format;
6435
6436     TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6437
6438     if (lParam == NF_QUERY)
6439         return NFR_UNICODE;
6440
6441     if (lParam == NF_REQUERY) {
6442         format = SendMessageW(infoPtr->hwndNotify,
6443                          WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6444         if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6445             ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6446                 format);
6447             format = NFR_ANSI;
6448         }
6449         return format;
6450     }
6451     return 0;
6452 }
6453
6454
6455 static LRESULT
6456 TOOLBAR_Paint (HWND hwnd, WPARAM wParam)
6457 {
6458     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
6459     HDC hdc;
6460     PAINTSTRUCT ps;
6461
6462     /* fill ps.rcPaint with a default rect */
6463     memcpy(&(ps.rcPaint), &(infoPtr->rcBound), sizeof(infoPtr->rcBound));
6464
6465     hdc = wParam==0 ? BeginPaint(hwnd, &ps) : (HDC)wParam;
6466
6467     TRACE("psrect=(%d,%d)-(%d,%d)\n",
6468           ps.rcPaint.left, ps.rcPaint.top,
6469           ps.rcPaint.right, ps.rcPaint.bottom);
6470
6471     TOOLBAR_Refresh (hwnd, hdc, &ps);
6472     if (!wParam) EndPaint (hwnd, &ps);
6473
6474     return 0;
6475 }
6476
6477
6478 static LRESULT
6479 TOOLBAR_SetFocus (HWND hwnd, WPARAM wParam)
6480 {
6481     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6482
6483     TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6484
6485     /* make first item hot */
6486     if (infoPtr->nNumButtons > 0)
6487         TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6488
6489     return 0;
6490 }
6491
6492 static LRESULT
6493 TOOLBAR_SetFont(HWND hwnd, WPARAM wParam, LPARAM lParam)
6494 {
6495     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
6496     
6497     TRACE("font=%p redraw=%ld\n", (HFONT)wParam, lParam);
6498     
6499     if (wParam == 0)
6500         infoPtr->hFont = infoPtr->hDefaultFont;
6501     else
6502         infoPtr->hFont = (HFONT)wParam;
6503
6504     TOOLBAR_CalcToolbar(hwnd);
6505
6506     if (lParam)
6507         InvalidateRect(hwnd, NULL, TRUE);
6508     return 1;
6509 }
6510
6511 static LRESULT
6512 TOOLBAR_SetRedraw (HWND hwnd, WPARAM wParam, LPARAM lParam)
6513      /*****************************************************
6514       *
6515       * Function;
6516       *  Handles the WM_SETREDRAW message.
6517       *
6518       * Documentation:
6519       *  According to testing V4.71 of COMCTL32 returns the
6520       *  *previous* status of the redraw flag (either 0 or 1)
6521       *  instead of the MSDN documented value of 0 if handled.
6522       *  (For laughs see the "consistency" with same function
6523       *   in rebar.)
6524       *
6525       *****************************************************/
6526 {
6527     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6528     BOOL oldredraw = infoPtr->bDoRedraw;
6529
6530     TRACE("set to %s\n",
6531           (wParam) ? "TRUE" : "FALSE");
6532     infoPtr->bDoRedraw = (BOOL) wParam;
6533     if (wParam) {
6534         InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6535     }
6536     return (oldredraw) ? 1 : 0;
6537 }
6538
6539
6540 static LRESULT
6541 TOOLBAR_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
6542 {
6543     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6544
6545     TRACE("sizing toolbar!\n");
6546
6547     if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6548     {
6549         RECT delta_width, delta_height, client, dummy;
6550         DWORD min_x, max_x, min_y, max_y;
6551         TBUTTON_INFO *btnPtr;
6552         INT i;
6553
6554         GetClientRect(hwnd, &client);
6555         if(client.right > infoPtr->client_rect.right)
6556         {
6557             min_x = infoPtr->client_rect.right;
6558             max_x = client.right;
6559         }
6560         else
6561         {
6562             max_x = infoPtr->client_rect.right;
6563             min_x = client.right;
6564         }
6565         if(client.bottom > infoPtr->client_rect.bottom)
6566         {
6567             min_y = infoPtr->client_rect.bottom;
6568             max_y = client.bottom;
6569         }
6570         else
6571         {
6572             max_y = infoPtr->client_rect.bottom;
6573             min_y = client.bottom;
6574         }
6575
6576         SetRect(&delta_width, min_x, 0, max_x, min_y);
6577         SetRect(&delta_height, 0, min_y, max_x, max_y);
6578
6579         TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6580         btnPtr = infoPtr->buttons;
6581         for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6582             if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6583                 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6584                 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
6585     }
6586     GetClientRect(hwnd, &infoPtr->client_rect);
6587     TOOLBAR_AutoSize(hwnd);
6588     return 0;
6589 }
6590
6591
6592 static LRESULT
6593 TOOLBAR_StyleChanged (HWND hwnd, INT nType, const STYLESTRUCT *lpStyle)
6594 {
6595     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6596
6597     if (nType == GWL_STYLE)
6598     {
6599         DWORD dwOldStyle = infoPtr->dwStyle;
6600
6601         if (lpStyle->styleNew & TBSTYLE_LIST)
6602             infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
6603         else
6604             infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
6605
6606         TOOLBAR_CheckStyle (hwnd, lpStyle->styleNew);
6607
6608         TRACE("new style 0x%08x\n", lpStyle->styleNew);
6609
6610         infoPtr->dwStyle = lpStyle->styleNew;
6611
6612         if ((dwOldStyle ^ lpStyle->styleNew) & (TBSTYLE_WRAPABLE | CCS_VERT))
6613             TOOLBAR_LayoutToolbar(hwnd);
6614
6615         /* only resize if one of the CCS_* styles was changed */
6616         if ((dwOldStyle ^ lpStyle->styleNew) & COMMON_STYLES)
6617         {
6618             TOOLBAR_AutoSize (hwnd);
6619     
6620             InvalidateRect(hwnd, NULL, TRUE);
6621         }
6622     }
6623
6624     return 0;
6625 }
6626
6627
6628 static LRESULT
6629 TOOLBAR_SysColorChange (HWND hwnd)
6630 {
6631     COMCTL32_RefreshSysColors();
6632
6633     return 0;
6634 }
6635
6636
6637 /* update theme after a WM_THEMECHANGED message */
6638 static LRESULT theme_changed (HWND hwnd)
6639 {
6640     HTHEME theme = GetWindowTheme (hwnd);
6641     CloseThemeData (theme);
6642     OpenThemeData (hwnd, themeClass);
6643     return 0;
6644 }
6645
6646
6647 static LRESULT WINAPI
6648 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6649 {
6650     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6651
6652     TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6653           hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6654
6655     if (!infoPtr && (uMsg != WM_NCCREATE))
6656         return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6657
6658     switch (uMsg)
6659     {
6660         case TB_ADDBITMAP:
6661             return TOOLBAR_AddBitmap (hwnd, wParam, lParam);
6662
6663         case TB_ADDBUTTONSA:
6664             return TOOLBAR_AddButtonsT(hwnd, wParam, lParam, FALSE);
6665
6666         case TB_ADDBUTTONSW:
6667             return TOOLBAR_AddButtonsT(hwnd, wParam, lParam, TRUE);
6668
6669         case TB_ADDSTRINGA:
6670             return TOOLBAR_AddStringA (hwnd, wParam, lParam);
6671
6672         case TB_ADDSTRINGW:
6673             return TOOLBAR_AddStringW (hwnd, wParam, lParam);
6674
6675         case TB_AUTOSIZE:
6676             return TOOLBAR_AutoSize (hwnd);
6677
6678         case TB_BUTTONCOUNT:
6679             return TOOLBAR_ButtonCount (hwnd, wParam, lParam);
6680
6681         case TB_BUTTONSTRUCTSIZE:
6682             return TOOLBAR_ButtonStructSize (hwnd, wParam, lParam);
6683
6684         case TB_CHANGEBITMAP:
6685             return TOOLBAR_ChangeBitmap (hwnd, wParam, lParam);
6686
6687         case TB_CHECKBUTTON:
6688             return TOOLBAR_CheckButton (hwnd, wParam, lParam);
6689
6690         case TB_COMMANDTOINDEX:
6691             return TOOLBAR_CommandToIndex (hwnd, wParam, lParam);
6692
6693         case TB_CUSTOMIZE:
6694             return TOOLBAR_Customize (hwnd);
6695
6696         case TB_DELETEBUTTON:
6697             return TOOLBAR_DeleteButton (hwnd, wParam, lParam);
6698
6699         case TB_ENABLEBUTTON:
6700             return TOOLBAR_EnableButton (hwnd, wParam, lParam);
6701
6702         case TB_GETANCHORHIGHLIGHT:
6703             return TOOLBAR_GetAnchorHighlight (hwnd);
6704
6705         case TB_GETBITMAP:
6706             return TOOLBAR_GetBitmap (hwnd, wParam, lParam);
6707
6708         case TB_GETBITMAPFLAGS:
6709             return TOOLBAR_GetBitmapFlags (hwnd, wParam, lParam);
6710
6711         case TB_GETBUTTON:
6712             return TOOLBAR_GetButton (hwnd, wParam, lParam);
6713
6714         case TB_GETBUTTONINFOA:
6715             return TOOLBAR_GetButtonInfoT(hwnd, wParam, lParam, FALSE);
6716
6717         case TB_GETBUTTONINFOW:
6718             return TOOLBAR_GetButtonInfoT(hwnd, wParam, lParam, TRUE);
6719
6720         case TB_GETBUTTONSIZE:
6721             return TOOLBAR_GetButtonSize (hwnd);
6722
6723         case TB_GETBUTTONTEXTA:
6724             return TOOLBAR_GetButtonTextA (hwnd, wParam, lParam);
6725
6726         case TB_GETBUTTONTEXTW:
6727             return TOOLBAR_GetButtonTextW (hwnd, wParam, lParam);
6728
6729         case TB_GETDISABLEDIMAGELIST:
6730             return TOOLBAR_GetDisabledImageList (hwnd, wParam, lParam);
6731
6732         case TB_GETEXTENDEDSTYLE:
6733             return TOOLBAR_GetExtendedStyle (hwnd);
6734
6735         case TB_GETHOTIMAGELIST:
6736             return TOOLBAR_GetHotImageList (hwnd, wParam, lParam);
6737
6738         case TB_GETHOTITEM:
6739             return TOOLBAR_GetHotItem (hwnd);
6740
6741         case TB_GETIMAGELIST:
6742             return TOOLBAR_GetDefImageList (hwnd, wParam, lParam);
6743
6744         case TB_GETINSERTMARK:
6745             return TOOLBAR_GetInsertMark (hwnd, wParam, lParam);
6746
6747         case TB_GETINSERTMARKCOLOR:
6748             return TOOLBAR_GetInsertMarkColor (hwnd, wParam, lParam);
6749
6750         case TB_GETITEMRECT:
6751             return TOOLBAR_GetItemRect (hwnd, wParam, lParam);
6752
6753         case TB_GETMAXSIZE:
6754             return TOOLBAR_GetMaxSize (hwnd, wParam, lParam);
6755
6756 /*      case TB_GETOBJECT:                      */ /* 4.71 */
6757
6758         case TB_GETPADDING:
6759             return TOOLBAR_GetPadding (hwnd);
6760
6761         case TB_GETRECT:
6762             return TOOLBAR_GetRect (hwnd, wParam, lParam);
6763
6764         case TB_GETROWS:
6765             return TOOLBAR_GetRows (hwnd, wParam, lParam);
6766
6767         case TB_GETSTATE:
6768             return TOOLBAR_GetState (hwnd, wParam, lParam);
6769
6770         case TB_GETSTRINGA:
6771         return TOOLBAR_GetStringA (hwnd, wParam, lParam);
6772
6773         case TB_GETSTRINGW:
6774             return TOOLBAR_GetStringW (hwnd, wParam, lParam);
6775
6776         case TB_GETSTYLE:
6777             return TOOLBAR_GetStyle (hwnd, wParam, lParam);
6778
6779         case TB_GETTEXTROWS:
6780             return TOOLBAR_GetTextRows (hwnd, wParam, lParam);
6781
6782         case TB_GETTOOLTIPS:
6783             return TOOLBAR_GetToolTips (hwnd, wParam, lParam);
6784
6785         case TB_GETUNICODEFORMAT:
6786             return TOOLBAR_GetUnicodeFormat (hwnd, wParam, lParam);
6787
6788         case TB_HIDEBUTTON:
6789             return TOOLBAR_HideButton (hwnd, wParam, lParam);
6790
6791         case TB_HITTEST:
6792             return TOOLBAR_HitTest (hwnd, wParam, lParam);
6793
6794         case TB_INDETERMINATE:
6795             return TOOLBAR_Indeterminate (hwnd, wParam, lParam);
6796
6797         case TB_INSERTBUTTONA:
6798             return TOOLBAR_InsertButtonT(hwnd, wParam, lParam, FALSE);
6799
6800         case TB_INSERTBUTTONW:
6801             return TOOLBAR_InsertButtonT(hwnd, wParam, lParam, TRUE);
6802
6803 /*      case TB_INSERTMARKHITTEST:              */ /* 4.71 */
6804
6805         case TB_ISBUTTONCHECKED:
6806             return TOOLBAR_IsButtonChecked (hwnd, wParam, lParam);
6807
6808         case TB_ISBUTTONENABLED:
6809             return TOOLBAR_IsButtonEnabled (hwnd, wParam, lParam);
6810
6811         case TB_ISBUTTONHIDDEN:
6812             return TOOLBAR_IsButtonHidden (hwnd, wParam, lParam);
6813
6814         case TB_ISBUTTONHIGHLIGHTED:
6815             return TOOLBAR_IsButtonHighlighted (hwnd, wParam, lParam);
6816
6817         case TB_ISBUTTONINDETERMINATE:
6818             return TOOLBAR_IsButtonIndeterminate (hwnd, wParam, lParam);
6819
6820         case TB_ISBUTTONPRESSED:
6821             return TOOLBAR_IsButtonPressed (hwnd, wParam, lParam);
6822
6823         case TB_LOADIMAGES:
6824             return TOOLBAR_LoadImages (hwnd, wParam, lParam);
6825
6826         case TB_MAPACCELERATORA:
6827         case TB_MAPACCELERATORW:
6828             return TOOLBAR_MapAccelerator (hwnd, wParam, lParam);
6829
6830         case TB_MARKBUTTON:
6831             return TOOLBAR_MarkButton (hwnd, wParam, lParam);
6832
6833         case TB_MOVEBUTTON:
6834             return TOOLBAR_MoveButton (hwnd, wParam, lParam);
6835
6836         case TB_PRESSBUTTON:
6837             return TOOLBAR_PressButton (hwnd, wParam, lParam);
6838
6839         case TB_REPLACEBITMAP:
6840             return TOOLBAR_ReplaceBitmap (hwnd, wParam, lParam);
6841
6842         case TB_SAVERESTOREA:
6843             return TOOLBAR_SaveRestoreA (hwnd, wParam, (LPTBSAVEPARAMSA)lParam);
6844
6845         case TB_SAVERESTOREW:
6846             return TOOLBAR_SaveRestoreW (hwnd, wParam, (LPTBSAVEPARAMSW)lParam);
6847
6848         case TB_SETANCHORHIGHLIGHT:
6849             return TOOLBAR_SetAnchorHighlight (hwnd, wParam);
6850
6851         case TB_SETBITMAPSIZE:
6852             return TOOLBAR_SetBitmapSize (hwnd, wParam, lParam);
6853
6854         case TB_SETBUTTONINFOA:
6855             return TOOLBAR_SetButtonInfoA (hwnd, wParam, lParam);
6856
6857         case TB_SETBUTTONINFOW:
6858             return TOOLBAR_SetButtonInfoW (hwnd, wParam, lParam);
6859
6860         case TB_SETBUTTONSIZE:
6861             return TOOLBAR_SetButtonSize (hwnd, wParam, lParam);
6862
6863         case TB_SETBUTTONWIDTH:
6864             return TOOLBAR_SetButtonWidth (hwnd, wParam, lParam);
6865
6866         case TB_SETCMDID:
6867             return TOOLBAR_SetCmdId (hwnd, wParam, lParam);
6868
6869         case TB_SETDISABLEDIMAGELIST:
6870             return TOOLBAR_SetDisabledImageList (hwnd, wParam, lParam);
6871
6872         case TB_SETDRAWTEXTFLAGS:
6873             return TOOLBAR_SetDrawTextFlags (hwnd, wParam, lParam);
6874
6875         case TB_SETEXTENDEDSTYLE:
6876             return TOOLBAR_SetExtendedStyle (hwnd, wParam, lParam);
6877
6878         case TB_SETHOTIMAGELIST:
6879             return TOOLBAR_SetHotImageList (hwnd, wParam, lParam);
6880
6881         case TB_SETHOTITEM:
6882             return TOOLBAR_SetHotItem (hwnd, wParam);
6883
6884         case TB_SETIMAGELIST:
6885             return TOOLBAR_SetImageList (hwnd, wParam, lParam);
6886
6887         case TB_SETINDENT:
6888             return TOOLBAR_SetIndent (hwnd, wParam, lParam);
6889
6890         case TB_SETINSERTMARK:
6891             return TOOLBAR_SetInsertMark (hwnd, wParam, lParam);
6892
6893         case TB_SETINSERTMARKCOLOR:
6894             return TOOLBAR_SetInsertMarkColor (hwnd, wParam, lParam);
6895
6896         case TB_SETMAXTEXTROWS:
6897             return TOOLBAR_SetMaxTextRows (hwnd, wParam, lParam);
6898
6899         case TB_SETPADDING:
6900             return TOOLBAR_SetPadding (hwnd, wParam, lParam);
6901
6902         case TB_SETPARENT:
6903             return TOOLBAR_SetParent (hwnd, wParam, lParam);
6904
6905         case TB_SETROWS:
6906             return TOOLBAR_SetRows (hwnd, wParam, lParam);
6907
6908         case TB_SETSTATE:
6909             return TOOLBAR_SetState (hwnd, wParam, lParam);
6910
6911         case TB_SETSTYLE:
6912             return TOOLBAR_SetStyle (hwnd, wParam, lParam);
6913
6914         case TB_SETTOOLTIPS:
6915             return TOOLBAR_SetToolTips (hwnd, wParam, lParam);
6916
6917         case TB_SETUNICODEFORMAT:
6918             return TOOLBAR_SetUnicodeFormat (hwnd, wParam, lParam);
6919
6920         case TB_UNKWN45D:
6921             return TOOLBAR_Unkwn45D(hwnd, wParam, lParam);
6922
6923         case TB_UNKWN45E:
6924             return TOOLBAR_Unkwn45E (hwnd, wParam, lParam);
6925
6926         case TB_UNKWN460:
6927             return TOOLBAR_Unkwn460(hwnd, wParam, lParam);
6928
6929         case TB_UNKWN462:
6930             return TOOLBAR_Unkwn462(hwnd, wParam, lParam);
6931
6932         case TB_UNKWN463:
6933             return TOOLBAR_Unkwn463 (hwnd, wParam, lParam);
6934
6935         case TB_UNKWN464:
6936             return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6937
6938 /* Common Control Messages */
6939
6940 /*      case TB_GETCOLORSCHEME:                 */ /* identical to CCM_ */
6941         case CCM_GETCOLORSCHEME:
6942             return TOOLBAR_GetColorScheme (hwnd, (LPCOLORSCHEME)lParam);
6943
6944 /*      case TB_SETCOLORSCHEME:                 */ /* identical to CCM_ */
6945         case CCM_SETCOLORSCHEME:
6946             return TOOLBAR_SetColorScheme (hwnd, (LPCOLORSCHEME)lParam);
6947
6948         case CCM_GETVERSION:
6949             return TOOLBAR_GetVersion (hwnd);
6950
6951         case CCM_SETVERSION:
6952             return TOOLBAR_SetVersion (hwnd, (INT)wParam);
6953
6954
6955 /*      case WM_CHAR: */
6956
6957         case WM_CREATE:
6958             return TOOLBAR_Create (hwnd, wParam, lParam);
6959
6960         case WM_DESTROY:
6961           return TOOLBAR_Destroy (hwnd, wParam, lParam);
6962
6963         case WM_ERASEBKGND:
6964             return TOOLBAR_EraseBackground (hwnd, wParam, lParam);
6965
6966         case WM_GETFONT:
6967                 return TOOLBAR_GetFont (hwnd, wParam, lParam);
6968
6969         case WM_KEYDOWN:
6970             return TOOLBAR_KeyDown (hwnd, wParam, lParam);
6971
6972 /*      case WM_KILLFOCUS: */
6973
6974         case WM_LBUTTONDBLCLK:
6975             return TOOLBAR_LButtonDblClk (hwnd, wParam, lParam);
6976
6977         case WM_LBUTTONDOWN:
6978             return TOOLBAR_LButtonDown (hwnd, wParam, lParam);
6979
6980         case WM_LBUTTONUP:
6981             return TOOLBAR_LButtonUp (hwnd, wParam, lParam);
6982
6983         case WM_RBUTTONUP:
6984             return TOOLBAR_RButtonUp (hwnd, wParam, lParam);
6985
6986         case WM_RBUTTONDBLCLK:
6987             return TOOLBAR_RButtonDblClk (hwnd, wParam, lParam);
6988
6989         case WM_MOUSEMOVE:
6990             return TOOLBAR_MouseMove (hwnd, wParam, lParam);
6991
6992         case WM_MOUSELEAVE:
6993             return TOOLBAR_MouseLeave (hwnd, wParam, lParam);
6994
6995         case WM_CAPTURECHANGED:
6996             return TOOLBAR_CaptureChanged(hwnd);
6997
6998         case WM_NCACTIVATE:
6999             return TOOLBAR_NCActivate (hwnd, wParam, lParam);
7000
7001         case WM_NCCALCSIZE:
7002             return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
7003
7004         case WM_NCCREATE:
7005             return TOOLBAR_NCCreate (hwnd, wParam, lParam);
7006
7007         case WM_NCPAINT:
7008             return TOOLBAR_NCPaint (hwnd, wParam, lParam);
7009
7010         case WM_NOTIFY:
7011             return TOOLBAR_Notify (hwnd, wParam, lParam);
7012
7013         case WM_NOTIFYFORMAT:
7014             return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
7015
7016         case WM_PRINTCLIENT:
7017         case WM_PAINT:
7018             return TOOLBAR_Paint (hwnd, wParam);
7019
7020         case WM_SETFOCUS:
7021             return TOOLBAR_SetFocus (hwnd, wParam);
7022
7023         case WM_SETFONT:
7024             return TOOLBAR_SetFont(hwnd, wParam, lParam);
7025
7026         case WM_SETREDRAW:
7027             return TOOLBAR_SetRedraw (hwnd, wParam, lParam);
7028
7029         case WM_SIZE:
7030             return TOOLBAR_Size (hwnd, wParam, lParam);
7031
7032         case WM_STYLECHANGED:
7033             return TOOLBAR_StyleChanged (hwnd, (INT)wParam, (LPSTYLESTRUCT)lParam);
7034
7035         case WM_SYSCOLORCHANGE:
7036             return TOOLBAR_SysColorChange (hwnd);
7037             
7038         case WM_THEMECHANGED:
7039             return theme_changed (hwnd);
7040
7041 /*      case WM_WININICHANGE: */
7042
7043         case WM_CHARTOITEM:
7044         case WM_COMMAND:
7045         case WM_DRAWITEM:
7046         case WM_MEASUREITEM:
7047         case WM_VKEYTOITEM:
7048             return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
7049
7050         /* We see this in Outlook Express 5.x and just does DefWindowProc */
7051         case PGM_FORWARDMOUSE:
7052             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
7053
7054         default:
7055             if ((uMsg >= WM_USER) && (uMsg < WM_APP))
7056                 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
7057                      uMsg, wParam, lParam);
7058             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
7059     }
7060 }
7061
7062
7063 VOID
7064 TOOLBAR_Register (void)
7065 {
7066     WNDCLASSW wndClass;
7067
7068     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
7069     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
7070     wndClass.lpfnWndProc   = ToolbarWindowProc;
7071     wndClass.cbClsExtra    = 0;
7072     wndClass.cbWndExtra    = sizeof(TOOLBAR_INFO *);
7073     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
7074     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
7075     wndClass.lpszClassName = TOOLBARCLASSNAMEW;
7076
7077     RegisterClassW (&wndClass);
7078 }
7079
7080
7081 VOID
7082 TOOLBAR_Unregister (void)
7083 {
7084     UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
7085 }
7086
7087 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
7088 {
7089     HIMAGELIST himlold;
7090     PIMLENTRY c = NULL;
7091
7092     /* Check if the entry already exists */
7093     c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
7094
7095     /* If this is a new entry we must create it and insert into the array */
7096     if (!c)
7097     {
7098         PIMLENTRY *pnies;
7099
7100         c = (PIMLENTRY) Alloc(sizeof(IMLENTRY));
7101         c->id = id;
7102
7103         pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
7104         memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
7105         pnies[*cies] = c;
7106         (*cies)++;
7107
7108         Free(*pies);
7109         *pies = pnies;
7110     }
7111
7112     himlold = c->himl;
7113     c->himl = himl;
7114
7115     return himlold;
7116 }
7117
7118
7119 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
7120 {
7121     int i;
7122
7123     for (i = 0; i < *cies; i++)
7124         Free((*pies)[i]);
7125
7126     Free(*pies);
7127
7128     *cies = 0;
7129     *pies = NULL;
7130 }
7131
7132
7133 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
7134 {
7135     PIMLENTRY c = NULL;
7136
7137     if (pies != NULL)
7138     {
7139         int i;
7140
7141         for (i = 0; i < cies; i++)
7142         {
7143             if (pies[i]->id == id)
7144             {
7145                 c = pies[i];
7146                 break;
7147             }
7148         }
7149     }
7150
7151     return c;
7152 }
7153
7154
7155 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
7156 {
7157     HIMAGELIST himlDef = 0;
7158     PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
7159
7160     if (pie)
7161         himlDef = pie->himl;
7162
7163     return himlDef;
7164 }
7165
7166
7167 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
7168 {
7169     if (infoPtr->bUnicode)
7170         return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
7171     else
7172     {
7173         CHAR Buffer[256];
7174         NMTOOLBARA nmtba;
7175         BOOL bRet = FALSE;
7176
7177         nmtba.iItem = nmtb->iItem;
7178         nmtba.pszText = Buffer;
7179         nmtba.cchText = 256;
7180         ZeroMemory(nmtba.pszText, nmtba.cchText);
7181
7182         if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7183         {
7184             int ccht = strlen(nmtba.pszText);
7185             if (ccht)
7186                MultiByteToWideChar(CP_ACP, 0, (LPCSTR)nmtba.pszText, -1, 
7187                   nmtb->pszText, nmtb->cchText);
7188
7189             memcpy(&nmtb->tbButton, &nmtba.tbButton, sizeof(TBBUTTON));
7190             bRet = TRUE;
7191         }
7192
7193         return bRet;
7194     }
7195 }
7196
7197
7198 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, PCUSTOMBUTTON btnInfo)
7199 {
7200     NMTOOLBARW nmtb;
7201
7202     /* MSDN states that iItem is the index of the button, rather than the
7203      * command ID as used by every other NMTOOLBAR notification */
7204     nmtb.iItem = iItem;
7205     memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7206
7207     return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);
7208 }