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