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