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