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