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