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