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