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