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