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