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