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