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