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