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