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