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