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