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