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