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