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