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