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