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