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