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