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