- Add support for multiple image lists.
[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=(%d,%d)-(%d,%d)\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=(%d,%d)-(%d,%d)\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=(%d,%d)-(%d,%d)\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=(%d,%d) 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 == (HINSTANCE)0)
2229     {
2230         nIndex =
2231             ImageList_AddMasked (himlDef, (HBITMAP)lpAddBmp->nID,
2232                                  CLR_DEFAULT);
2233     }
2234     else if (lpAddBmp->hInst == HINST_COMMCTRL)
2235     {
2236         /* Add system bitmaps */
2237         switch (lpAddBmp->nID)
2238     {
2239             case IDB_STD_SMALL_COLOR:
2240                 hbmLoad = LoadBitmapA (COMCTL32_hModule,
2241                                        MAKEINTRESOURCEA(IDB_STD_SMALL));
2242                 nIndex = ImageList_AddMasked (himlDef,
2243                                               hbmLoad, CLR_DEFAULT);
2244                 DeleteObject (hbmLoad);
2245                 break;
2246
2247             case IDB_STD_LARGE_COLOR:
2248                 hbmLoad = LoadBitmapA (COMCTL32_hModule,
2249                                        MAKEINTRESOURCEA(IDB_STD_LARGE));
2250                 nIndex = ImageList_AddMasked (himlDef,
2251                                               hbmLoad, CLR_DEFAULT);
2252                 DeleteObject (hbmLoad);
2253                 break;
2254
2255             case IDB_VIEW_SMALL_COLOR:
2256                 hbmLoad = LoadBitmapA (COMCTL32_hModule,
2257                                        MAKEINTRESOURCEA(IDB_VIEW_SMALL));
2258                 nIndex = ImageList_AddMasked (himlDef,
2259                                               hbmLoad, CLR_DEFAULT);
2260                 DeleteObject (hbmLoad);
2261                 break;
2262
2263             case IDB_VIEW_LARGE_COLOR:
2264                 hbmLoad = LoadBitmapA (COMCTL32_hModule,
2265                                        MAKEINTRESOURCEA(IDB_VIEW_LARGE));
2266                 nIndex = ImageList_AddMasked (himlDef,
2267                                               hbmLoad, CLR_DEFAULT);
2268                 DeleteObject (hbmLoad);
2269                 break;
2270
2271             case IDB_HIST_SMALL_COLOR:
2272                 hbmLoad = LoadBitmapA (COMCTL32_hModule,
2273                                        MAKEINTRESOURCEA(IDB_HIST_SMALL));
2274                 nIndex = ImageList_AddMasked (himlDef,
2275                                               hbmLoad, CLR_DEFAULT);
2276                 DeleteObject (hbmLoad);
2277                 break;
2278
2279             case IDB_HIST_LARGE_COLOR:
2280                 hbmLoad = LoadBitmapA (COMCTL32_hModule,
2281                                        MAKEINTRESOURCEA(IDB_HIST_LARGE));
2282                 nIndex = ImageList_AddMasked (himlDef,
2283                                               hbmLoad, CLR_DEFAULT);
2284                 DeleteObject (hbmLoad);
2285                 break;
2286
2287             default:
2288         nIndex = ImageList_GetImageCount (himlDef);
2289                 ERR ("invalid imagelist!\n");
2290                 break;
2291         }
2292     }
2293     else
2294     {
2295         hbmLoad = LoadBitmapA (lpAddBmp->hInst, (LPSTR)lpAddBmp->nID);
2296         nIndex = ImageList_AddMasked (himlDef, hbmLoad, CLR_DEFAULT);
2297         DeleteObject (hbmLoad);
2298     }
2299
2300     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2301
2302     if (infoPtr->nNumBitmapInfos == 0)
2303     {
2304         infoPtr->bitmaps = COMCTL32_Alloc(sizeof(TBITMAP_INFO));
2305     }
2306     else
2307     {
2308         TBITMAP_INFO *oldBitmaps = infoPtr->bitmaps;
2309         infoPtr->bitmaps = COMCTL32_Alloc((infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2310         memcpy(&infoPtr->bitmaps[0], &oldBitmaps[0], infoPtr->nNumBitmapInfos);
2311     }
2312
2313     infoPtr->bitmaps[infoPtr->nNumBitmapInfos].nButtons = nButtons;
2314     infoPtr->bitmaps[infoPtr->nNumBitmapInfos].hInst = lpAddBmp->hInst;
2315     infoPtr->bitmaps[infoPtr->nNumBitmapInfos].nID = lpAddBmp->nID;
2316
2317     infoPtr->nNumBitmapInfos++;
2318     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2319
2320     if (nIndex != -1)
2321     {
2322        INT imagecount = ImageList_GetImageCount(himlDef);
2323
2324        if (infoPtr->nNumBitmaps + nButtons != imagecount)
2325        {
2326          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",
2327               infoPtr->nNumBitmaps, nCount, imagecount - nCount,
2328               infoPtr->nNumBitmaps+nButtons,imagecount);
2329
2330          infoPtr->nNumBitmaps = imagecount;
2331        }
2332        else
2333          infoPtr->nNumBitmaps += nButtons;
2334     }
2335
2336     InvalidateRect(hwnd, NULL, FALSE);
2337
2338     return nIndex;
2339 }
2340
2341
2342 static LRESULT
2343 TOOLBAR_AddButtonsA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2344 {
2345     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2346     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2347     INT nOldButtons, nNewButtons, nAddButtons, nCount;
2348
2349     TRACE("adding %d buttons!\n", wParam);
2350
2351     nAddButtons = (UINT)wParam;
2352     nOldButtons = infoPtr->nNumButtons;
2353     nNewButtons = nOldButtons + nAddButtons;
2354
2355     if (infoPtr->nNumButtons == 0) {
2356         infoPtr->buttons =
2357             COMCTL32_Alloc (sizeof(TBUTTON_INFO) * nNewButtons);
2358     }
2359     else {
2360         TBUTTON_INFO *oldButtons = infoPtr->buttons;
2361         infoPtr->buttons =
2362             COMCTL32_Alloc (sizeof(TBUTTON_INFO) * nNewButtons);
2363         memcpy (&infoPtr->buttons[0], &oldButtons[0],
2364                 nOldButtons * sizeof(TBUTTON_INFO));
2365         COMCTL32_Free (oldButtons);
2366     }
2367
2368     infoPtr->nNumButtons = nNewButtons;
2369
2370     /* insert new button data */
2371     for (nCount = 0; nCount < nAddButtons; nCount++) {
2372         TBUTTON_INFO *btnPtr = &infoPtr->buttons[nOldButtons+nCount];
2373         btnPtr->iBitmap   = lpTbb[nCount].iBitmap;
2374         btnPtr->idCommand = lpTbb[nCount].idCommand;
2375         btnPtr->fsState   = lpTbb[nCount].fsState;
2376         btnPtr->fsStyle   = lpTbb[nCount].fsStyle;
2377         btnPtr->dwData    = lpTbb[nCount].dwData;
2378         btnPtr->iString   = lpTbb[nCount].iString;
2379         btnPtr->bHot      = FALSE;
2380
2381         if ((infoPtr->hwndToolTip) && !(btnPtr->fsStyle & TBSTYLE_SEP)) {
2382             TTTOOLINFOA ti;
2383
2384             ZeroMemory (&ti, sizeof(TTTOOLINFOA));
2385             ti.cbSize   = sizeof (TTTOOLINFOA);
2386             ti.hwnd     = hwnd;
2387             ti.uId      = btnPtr->idCommand;
2388             ti.hinst    = 0;
2389             ti.lpszText = LPSTR_TEXTCALLBACKA;
2390
2391             SendMessageA (infoPtr->hwndToolTip, TTM_ADDTOOLA,
2392                             0, (LPARAM)&ti);
2393         }
2394     }
2395
2396     TOOLBAR_CalcToolbar (hwnd);
2397
2398     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
2399
2400     InvalidateRect(hwnd, NULL, FALSE);
2401
2402     return TRUE;
2403 }
2404
2405
2406 static LRESULT
2407 TOOLBAR_AddButtonsW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2408 {
2409     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2410     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2411     INT nOldButtons, nNewButtons, nAddButtons, nCount;
2412
2413     TRACE("adding %d buttons!\n", wParam);
2414
2415     nAddButtons = (UINT)wParam;
2416     nOldButtons = infoPtr->nNumButtons;
2417     nNewButtons = nOldButtons + nAddButtons;
2418
2419     if (infoPtr->nNumButtons == 0) {
2420         infoPtr->buttons =
2421             COMCTL32_Alloc (sizeof(TBUTTON_INFO) * nNewButtons);
2422     }
2423     else {
2424         TBUTTON_INFO *oldButtons = infoPtr->buttons;
2425         infoPtr->buttons =
2426             COMCTL32_Alloc (sizeof(TBUTTON_INFO) * nNewButtons);
2427         memcpy (&infoPtr->buttons[0], &oldButtons[0],
2428                 nOldButtons * sizeof(TBUTTON_INFO));
2429         COMCTL32_Free (oldButtons);
2430     }
2431
2432     infoPtr->nNumButtons = nNewButtons;
2433
2434     /* insert new button data */
2435     for (nCount = 0; nCount < nAddButtons; nCount++) {
2436         TBUTTON_INFO *btnPtr = &infoPtr->buttons[nOldButtons+nCount];
2437         btnPtr->iBitmap   = lpTbb[nCount].iBitmap;
2438         btnPtr->idCommand = lpTbb[nCount].idCommand;
2439         btnPtr->fsState   = lpTbb[nCount].fsState;
2440         btnPtr->fsStyle   = lpTbb[nCount].fsStyle;
2441         btnPtr->dwData    = lpTbb[nCount].dwData;
2442         btnPtr->iString   = lpTbb[nCount].iString;
2443         btnPtr->bHot      = FALSE;
2444
2445         if ((infoPtr->hwndToolTip) && !(btnPtr->fsStyle & TBSTYLE_SEP)) {
2446             TTTOOLINFOW ti;
2447
2448             ZeroMemory (&ti, sizeof(TTTOOLINFOW));
2449             ti.cbSize   = sizeof (TTTOOLINFOW);
2450             ti.hwnd     = hwnd;
2451             ti.uId      = btnPtr->idCommand;
2452             ti.hinst    = 0;
2453             ti.lpszText = LPSTR_TEXTCALLBACKW;
2454             ti.lParam   = lParam;
2455
2456             SendMessageW (infoPtr->hwndToolTip, TTM_ADDTOOLW,
2457                             0, (LPARAM)&ti);
2458         }
2459     }
2460
2461     TOOLBAR_CalcToolbar (hwnd);
2462
2463     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
2464
2465     InvalidateRect(hwnd, NULL, FALSE);
2466
2467     return TRUE;
2468 }
2469
2470
2471 static LRESULT
2472 TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2473 {
2474     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2475     INT nIndex;
2476
2477     if ((wParam) && (HIWORD(lParam) == 0)) {
2478         char szString[256];
2479         INT len;
2480         TRACE("adding string from resource!\n");
2481
2482         len = LoadStringA ((HINSTANCE)wParam, (UINT)lParam,
2483                              szString, 256);
2484
2485         TRACE("len=%d \"%s\"\n", len, szString);
2486         nIndex = infoPtr->nNumStrings;
2487         if (infoPtr->nNumStrings == 0) {
2488             infoPtr->strings =
2489                 COMCTL32_Alloc (sizeof(LPWSTR));
2490         }
2491         else {
2492             LPWSTR *oldStrings = infoPtr->strings;
2493             infoPtr->strings =
2494                 COMCTL32_Alloc (sizeof(LPWSTR) * (infoPtr->nNumStrings + 1));
2495             memcpy (&infoPtr->strings[0], &oldStrings[0],
2496                     sizeof(LPWSTR) * infoPtr->nNumStrings);
2497             COMCTL32_Free (oldStrings);
2498         }
2499
2500         /*COMCTL32_Alloc zeros out the allocated memory*/
2501         Str_SetPtrAtoW (&infoPtr->strings[infoPtr->nNumStrings], szString );
2502         infoPtr->nNumStrings++;
2503     }
2504     else {
2505         LPSTR p = (LPSTR)lParam;
2506         INT len;
2507
2508         if (p == NULL)
2509             return -1;
2510         TRACE("adding string(s) from array!\n");
2511
2512         nIndex = infoPtr->nNumStrings;
2513         while (*p) {
2514             len = strlen (p);
2515             TRACE("len=%d \"%s\"\n", len, p);
2516
2517             if (infoPtr->nNumStrings == 0) {
2518                 infoPtr->strings =
2519                     COMCTL32_Alloc (sizeof(LPWSTR));
2520             }
2521             else {
2522                 LPWSTR *oldStrings = infoPtr->strings;
2523                 infoPtr->strings =
2524                     COMCTL32_Alloc (sizeof(LPWSTR) * (infoPtr->nNumStrings + 1));
2525                 memcpy (&infoPtr->strings[0], &oldStrings[0],
2526                         sizeof(LPWSTR) * infoPtr->nNumStrings);
2527                 COMCTL32_Free (oldStrings);
2528             }
2529
2530             Str_SetPtrAtoW (&infoPtr->strings[infoPtr->nNumStrings], p );
2531             infoPtr->nNumStrings++;
2532
2533             p += (len+1);
2534         }
2535     }
2536
2537     return nIndex;
2538 }
2539
2540
2541 static LRESULT
2542 TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2543 {
2544 #define MAX_RESOURCE_STRING_LENGTH 512
2545     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2546     INT nIndex;
2547
2548     if ((wParam) && (HIWORD(lParam) == 0)) {
2549         WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2550         INT len;
2551         TRACE("adding string from resource!\n");
2552
2553         len = LoadStringW ((HINSTANCE)wParam, (UINT)lParam,
2554                              szString, MAX_RESOURCE_STRING_LENGTH);
2555
2556         TRACE("len=%d %s\n", len, debugstr_w(szString));
2557         TRACE("First char: 0x%x\n", *szString);
2558         if (szString[0] == L'|')
2559         {
2560             PWSTR p = szString + 1;
2561
2562             nIndex = infoPtr->nNumStrings;
2563             while (*p != L'|' && *p != L'\0') {
2564                 PWSTR np;
2565
2566                 if (infoPtr->nNumStrings == 0) {
2567                     infoPtr->strings = COMCTL32_Alloc (sizeof(LPWSTR));
2568                 }
2569                 else
2570                 {
2571                     LPWSTR *oldStrings = infoPtr->strings;
2572                     infoPtr->strings = COMCTL32_Alloc(sizeof(LPWSTR) * (infoPtr->nNumStrings + 1));
2573                     memcpy(&infoPtr->strings[0], &oldStrings[0],
2574                            sizeof(LPWSTR) * infoPtr->nNumStrings);
2575                     COMCTL32_Free(oldStrings);
2576                 }
2577
2578                 np=COMCTL32_StrChrW (p, L'|');
2579                 if (np!=NULL) {
2580                     len = np - p;
2581                     np++;
2582                 } else {
2583                     len = strlenW(p);
2584                     np = p + len;
2585                 }
2586                 TRACE("len=%d %s\n", len, debugstr_w(p));
2587                 infoPtr->strings[infoPtr->nNumStrings] =
2588                     COMCTL32_Alloc (sizeof(WCHAR)*(len+1));
2589                 lstrcpynW (infoPtr->strings[infoPtr->nNumStrings], p, len+1);
2590                 infoPtr->nNumStrings++;
2591
2592                 p = np;
2593             }
2594         }
2595         else
2596         {
2597             nIndex = infoPtr->nNumStrings;
2598             if (infoPtr->nNumStrings == 0) {
2599                 infoPtr->strings =
2600                     COMCTL32_Alloc (sizeof(LPWSTR));
2601             }
2602             else {
2603                 LPWSTR *oldStrings = infoPtr->strings;
2604                 infoPtr->strings =
2605                     COMCTL32_Alloc (sizeof(LPWSTR) * (infoPtr->nNumStrings + 1));
2606                 memcpy (&infoPtr->strings[0], &oldStrings[0],
2607                         sizeof(LPWSTR) * infoPtr->nNumStrings);
2608                 COMCTL32_Free (oldStrings);
2609             }
2610
2611             Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], szString);
2612             infoPtr->nNumStrings++;
2613         }
2614     }
2615     else {
2616         LPWSTR p = (LPWSTR)lParam;
2617         INT len;
2618
2619         if (p == NULL)
2620             return -1;
2621         TRACE("adding string(s) from array!\n");
2622         nIndex = infoPtr->nNumStrings;
2623         while (*p) {
2624             len = strlenW (p);
2625
2626             TRACE("len=%d %s\n", len, debugstr_w(p));
2627             if (infoPtr->nNumStrings == 0) {
2628                 infoPtr->strings =
2629                     COMCTL32_Alloc (sizeof(LPWSTR));
2630             }
2631             else {
2632                 LPWSTR *oldStrings = infoPtr->strings;
2633                 infoPtr->strings =
2634                     COMCTL32_Alloc (sizeof(LPWSTR) * (infoPtr->nNumStrings + 1));
2635                 memcpy (&infoPtr->strings[0], &oldStrings[0],
2636                         sizeof(LPWSTR) * infoPtr->nNumStrings);
2637                 COMCTL32_Free (oldStrings);
2638             }
2639
2640             Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2641             infoPtr->nNumStrings++;
2642
2643             p += (len+1);
2644         }
2645     }
2646
2647     return nIndex;
2648 }
2649
2650
2651 static LRESULT
2652 TOOLBAR_AutoSize (HWND hwnd)
2653 {
2654     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2655     DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
2656     RECT parent_rect;
2657     RECT window_rect;
2658     HWND parent;
2659     INT  x, y;
2660     INT  cx, cy;
2661     UINT uPosFlags = SWP_NOZORDER;
2662
2663     TRACE("resize forced, style=%lx!\n", dwStyle);
2664
2665     parent = GetParent (hwnd);
2666     GetClientRect(parent, &parent_rect);
2667
2668     x = parent_rect.left;
2669     y = parent_rect.top;
2670
2671     /* FIXME: we should be able to early out if nothing */
2672     /* has changed with nWidth != parent_rect width */
2673
2674     if (dwStyle & CCS_NORESIZE) {
2675         uPosFlags |= (SWP_NOSIZE | SWP_NOMOVE);
2676         cx = 0;
2677         cy = 0;
2678         TOOLBAR_CalcToolbar (hwnd);
2679     }
2680     else {
2681         infoPtr->nWidth = parent_rect.right - parent_rect.left;
2682         TOOLBAR_CalcToolbar (hwnd);
2683         InvalidateRect( hwnd, NULL, TRUE );
2684         cy = infoPtr->nHeight;
2685         cx = infoPtr->nWidth;
2686
2687         if (dwStyle & CCS_NOMOVEY) {
2688                 GetWindowRect(hwnd, &window_rect);
2689                 ScreenToClient(parent, (LPPOINT)&window_rect.left);
2690                 y = window_rect.top;
2691         }
2692     }
2693
2694     if (dwStyle & CCS_NOPARENTALIGN)
2695         uPosFlags |= SWP_NOMOVE;
2696
2697     if (!(dwStyle & CCS_NODIVIDER))
2698         cy += GetSystemMetrics(SM_CYEDGE);
2699
2700     if (dwStyle & WS_BORDER)
2701     {
2702         x = y = 1;
2703         cy += GetSystemMetrics(SM_CYEDGE);
2704         cx += GetSystemMetrics(SM_CYEDGE);
2705     }
2706
2707     infoPtr->bAutoSize = TRUE;
2708     SetWindowPos (hwnd, HWND_TOP, parent_rect.left - x, parent_rect.top - y,
2709                         cx, cy, uPosFlags);
2710     /* The following line makes sure that the infoPtr->bAutoSize is turned off after
2711      * the setwindowpos calls */
2712     infoPtr->bAutoSize = FALSE;
2713
2714     return 0;
2715 }
2716
2717
2718 static LRESULT
2719 TOOLBAR_ButtonCount (HWND hwnd, WPARAM wParam, LPARAM lParam)
2720 {
2721     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2722
2723     return infoPtr->nNumButtons;
2724 }
2725
2726
2727 static LRESULT
2728 TOOLBAR_ButtonStructSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
2729 {
2730     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2731
2732     if (infoPtr == NULL) {
2733         ERR("(%p, 0x%x, 0x%lx)\n", hwnd, wParam, lParam);
2734         ERR("infoPtr == NULL!\n");
2735         return 0;
2736     }
2737
2738     infoPtr->dwStructSize = (DWORD)wParam;
2739
2740     return 0;
2741 }
2742
2743
2744 static LRESULT
2745 TOOLBAR_ChangeBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
2746 {
2747     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2748     TBUTTON_INFO *btnPtr;
2749     INT nIndex;
2750
2751     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
2752     if (nIndex == -1)
2753         return FALSE;
2754
2755     btnPtr = &infoPtr->buttons[nIndex];
2756     btnPtr->iBitmap = LOWORD(lParam);
2757
2758     /* we HAVE to erase the background, the new bitmap could be */
2759     /* transparent */
2760     InvalidateRect(hwnd, &btnPtr->rect, TRUE);
2761
2762     return TRUE;
2763 }
2764
2765
2766 static LRESULT
2767 TOOLBAR_CheckButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
2768 {
2769     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2770     TBUTTON_INFO *btnPtr;
2771     INT nIndex;
2772     INT nOldIndex = -1;
2773     BOOL bChecked = FALSE;
2774
2775     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
2776     if (nIndex == -1)
2777         return FALSE;
2778
2779     btnPtr = &infoPtr->buttons[nIndex];
2780
2781     if (!(btnPtr->fsStyle & TBSTYLE_CHECK))
2782         return FALSE;
2783
2784     bChecked = (btnPtr->fsState & TBSTATE_CHECKED) ? TRUE : FALSE;
2785
2786     if (LOWORD(lParam) == FALSE)
2787         btnPtr->fsState &= ~TBSTATE_CHECKED;
2788     else {
2789         if (btnPtr->fsStyle & TBSTYLE_GROUP) {
2790             nOldIndex =
2791                 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
2792             if (nOldIndex == nIndex)
2793                 return 0;
2794             if (nOldIndex != -1)
2795                 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
2796         }
2797         btnPtr->fsState |= TBSTATE_CHECKED;
2798     }
2799
2800     if( bChecked != LOWORD(lParam) )
2801     {
2802         if (nOldIndex != -1)
2803         {
2804             InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect,
2805                 TOOLBAR_HasText(infoPtr, &infoPtr->buttons[nOldIndex]));
2806         }
2807         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
2808     }
2809
2810     /* FIXME: Send a WM_NOTIFY?? */
2811
2812     return TRUE;
2813 }
2814
2815
2816 static LRESULT
2817 TOOLBAR_CommandToIndex (HWND hwnd, WPARAM wParam, LPARAM lParam)
2818 {
2819     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2820
2821     return TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
2822 }
2823
2824
2825 static LRESULT
2826 TOOLBAR_Customize (HWND hwnd)
2827 {
2828     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2829     CUSTDLG_INFO custInfo;
2830     LRESULT ret;
2831     LPCVOID template;
2832     HRSRC hRes;
2833     NMHDR nmhdr;
2834
2835     custInfo.tbInfo = infoPtr;
2836     custInfo.tbHwnd = hwnd;
2837
2838     /* send TBN_BEGINADJUST notification */
2839     TOOLBAR_SendNotify ((NMHDR *) &nmhdr, infoPtr,
2840                     TBN_BEGINADJUST);
2841
2842     if (!(hRes = FindResourceA (COMCTL32_hModule,
2843                                 MAKEINTRESOURCEA(IDD_TBCUSTOMIZE),
2844                                 RT_DIALOGA)))
2845         return FALSE;
2846
2847     if(!(template = (LPVOID)LoadResource (COMCTL32_hModule, hRes)))
2848         return FALSE;
2849
2850     ret = DialogBoxIndirectParamA ((HINSTANCE)GetWindowLongA(hwnd, GWL_HINSTANCE),
2851                                    (LPDLGTEMPLATEA)template,
2852                                    hwnd,
2853                                    TOOLBAR_CustomizeDialogProc,
2854                                    (LPARAM)&custInfo);
2855
2856     /* send TBN_ENDADJUST notification */
2857     TOOLBAR_SendNotify ((NMHDR *) &nmhdr, infoPtr,
2858                     TBN_ENDADJUST);
2859
2860     return ret;
2861 }
2862
2863
2864 static LRESULT
2865 TOOLBAR_DeleteButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
2866 {
2867     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2868     INT nIndex = (INT)wParam;
2869
2870     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
2871         return FALSE;
2872
2873     if ((infoPtr->hwndToolTip) &&
2874         !(infoPtr->buttons[nIndex].fsStyle & TBSTYLE_SEP)) {
2875         TTTOOLINFOA ti;
2876
2877         ZeroMemory (&ti, sizeof(TTTOOLINFOA));
2878         ti.cbSize   = sizeof (TTTOOLINFOA);
2879         ti.hwnd     = hwnd;
2880         ti.uId      = infoPtr->buttons[nIndex].idCommand;
2881
2882         SendMessageA (infoPtr->hwndToolTip, TTM_DELTOOLA, 0, (LPARAM)&ti);
2883     }
2884
2885     if (infoPtr->nNumButtons == 1) {
2886         TRACE(" simple delete!\n");
2887         COMCTL32_Free (infoPtr->buttons);
2888         infoPtr->buttons = NULL;
2889         infoPtr->nNumButtons = 0;
2890     }
2891     else {
2892         TBUTTON_INFO *oldButtons = infoPtr->buttons;
2893         TRACE("complex delete! [nIndex=%d]\n", nIndex);
2894
2895         infoPtr->nNumButtons--;
2896         infoPtr->buttons = COMCTL32_Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
2897         if (nIndex > 0) {
2898             memcpy (&infoPtr->buttons[0], &oldButtons[0],
2899                     nIndex * sizeof(TBUTTON_INFO));
2900         }
2901
2902         if (nIndex < infoPtr->nNumButtons) {
2903             memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
2904                     (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
2905         }
2906
2907         COMCTL32_Free (oldButtons);
2908     }
2909
2910     TOOLBAR_CalcToolbar (hwnd);
2911
2912     InvalidateRect (hwnd, NULL, TRUE);
2913
2914     return TRUE;
2915 }
2916
2917
2918 static LRESULT
2919 TOOLBAR_EnableButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
2920 {
2921     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2922     TBUTTON_INFO *btnPtr;
2923     INT nIndex;
2924     DWORD bState;
2925
2926     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
2927     if (nIndex == -1)
2928         return FALSE;
2929
2930     btnPtr = &infoPtr->buttons[nIndex];
2931
2932     bState = btnPtr->fsState & TBSTATE_ENABLED;
2933
2934     /* update the toolbar button state */
2935     if(LOWORD(lParam) == FALSE) {
2936         btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
2937     } else {
2938         btnPtr->fsState |= TBSTATE_ENABLED;
2939     }
2940
2941     /* redraw the button only if the state of the button changed */
2942     if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
2943     {
2944         InvalidateRect(hwnd, &btnPtr->rect,
2945             TOOLBAR_HasText(infoPtr, btnPtr));
2946     }
2947
2948     return TRUE;
2949 }
2950
2951
2952 static inline LRESULT
2953 TOOLBAR_GetAnchorHighlight (HWND hwnd)
2954 {
2955     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2956
2957     return infoPtr->bAnchor;
2958 }
2959
2960
2961 static LRESULT
2962 TOOLBAR_GetBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
2963 {
2964     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2965     INT nIndex;
2966
2967     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
2968     if (nIndex == -1)
2969         return -1;
2970
2971     return infoPtr->buttons[nIndex].iBitmap;
2972 }
2973
2974
2975 static inline LRESULT
2976 TOOLBAR_GetBitmapFlags (HWND hwnd, WPARAM wParam, LPARAM lParam)
2977 {
2978     return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
2979 }
2980
2981
2982 static LRESULT
2983 TOOLBAR_GetButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
2984 {
2985     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2986     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2987     INT nIndex = (INT)wParam;
2988     TBUTTON_INFO *btnPtr;
2989
2990     if (infoPtr == NULL)
2991         return FALSE;
2992
2993     if (lpTbb == NULL)
2994         return FALSE;
2995
2996     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
2997         return FALSE;
2998
2999     btnPtr = &infoPtr->buttons[nIndex];
3000     lpTbb->iBitmap   = btnPtr->iBitmap;
3001     lpTbb->idCommand = btnPtr->idCommand;
3002     lpTbb->fsState   = btnPtr->fsState;
3003     lpTbb->fsStyle   = btnPtr->fsStyle;
3004     lpTbb->dwData    = btnPtr->dwData;
3005     lpTbb->iString   = btnPtr->iString;
3006
3007     return TRUE;
3008 }
3009
3010
3011 static LRESULT
3012 TOOLBAR_GetButtonInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3013 {
3014     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3015     LPTBBUTTONINFOA lpTbInfo = (LPTBBUTTONINFOA)lParam;
3016     TBUTTON_INFO *btnPtr;
3017     INT nIndex;
3018
3019     if (infoPtr == NULL)
3020         return -1;
3021     if (lpTbInfo == NULL)
3022         return -1;
3023     if (lpTbInfo->cbSize < sizeof(TBBUTTONINFOA))
3024         return -1;
3025
3026     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
3027                                      lpTbInfo->dwMask & 0x80000000);
3028     if (nIndex == -1)
3029         return -1;
3030
3031     if (!(btnPtr = &infoPtr->buttons[nIndex])) return -1;
3032
3033     if (lpTbInfo->dwMask & TBIF_COMMAND)
3034         lpTbInfo->idCommand = btnPtr->idCommand;
3035     if (lpTbInfo->dwMask & TBIF_IMAGE)
3036         lpTbInfo->iImage = btnPtr->iBitmap;
3037     if (lpTbInfo->dwMask & TBIF_LPARAM)
3038         lpTbInfo->lParam = btnPtr->dwData;
3039     if (lpTbInfo->dwMask & TBIF_SIZE)
3040         lpTbInfo->cx = (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3041     if (lpTbInfo->dwMask & TBIF_STATE)
3042         lpTbInfo->fsState = btnPtr->fsState;
3043     if (lpTbInfo->dwMask & TBIF_STYLE)
3044         lpTbInfo->fsStyle = btnPtr->fsStyle;
3045      if (lpTbInfo->dwMask & TBIF_TEXT) {
3046          LPWSTR lpText = TOOLBAR_GetText(infoPtr,btnPtr);
3047          Str_GetPtrWtoA (lpText, lpTbInfo->pszText,lpTbInfo->cchText);
3048          }
3049     return nIndex;
3050 }
3051
3052
3053 static LRESULT
3054 TOOLBAR_GetButtonInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3055 {
3056     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3057     LPTBBUTTONINFOW lpTbInfo = (LPTBBUTTONINFOW)lParam;
3058     TBUTTON_INFO *btnPtr;
3059     INT nIndex;
3060
3061     if (infoPtr == NULL)
3062         return -1;
3063     if (lpTbInfo == NULL)
3064         return -1;
3065     if (lpTbInfo->cbSize < sizeof(TBBUTTONINFOW))
3066         return -1;
3067
3068     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
3069                                      lpTbInfo->dwMask & 0x80000000);
3070     if (nIndex == -1)
3071         return -1;
3072
3073     btnPtr = &infoPtr->buttons[nIndex];
3074
3075     if(!btnPtr)
3076         return -1;
3077
3078     if (lpTbInfo->dwMask & TBIF_COMMAND)
3079         lpTbInfo->idCommand = btnPtr->idCommand;
3080     if (lpTbInfo->dwMask & TBIF_IMAGE)
3081         lpTbInfo->iImage = btnPtr->iBitmap;
3082     if (lpTbInfo->dwMask & TBIF_LPARAM)
3083         lpTbInfo->lParam = btnPtr->dwData;
3084     if (lpTbInfo->dwMask & TBIF_SIZE)
3085         lpTbInfo->cx = (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3086     if (lpTbInfo->dwMask & TBIF_STATE)
3087         lpTbInfo->fsState = btnPtr->fsState;
3088     if (lpTbInfo->dwMask & TBIF_STYLE)
3089         lpTbInfo->fsStyle = btnPtr->fsStyle;
3090     if (lpTbInfo->dwMask & TBIF_TEXT) {
3091         LPWSTR lpText = TOOLBAR_GetText(infoPtr,btnPtr);
3092         Str_GetPtrW (lpText,lpTbInfo->pszText,lpTbInfo->cchText);
3093     }
3094
3095     return nIndex;
3096 }
3097
3098
3099 static LRESULT
3100 TOOLBAR_GetButtonSize (HWND hwnd)
3101 {
3102     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3103
3104     if (infoPtr->nNumButtons > 0)
3105         return MAKELONG((WORD)infoPtr->nButtonWidth,
3106                         (WORD)infoPtr->nButtonHeight);
3107     else
3108         return MAKELONG(8,7);
3109 }
3110
3111
3112 static LRESULT
3113 TOOLBAR_GetButtonTextA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3114 {
3115     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3116     INT nIndex;
3117     LPWSTR lpText;
3118
3119     if (lParam == 0)
3120         return -1;
3121
3122     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3123     if (nIndex == -1)
3124         return -1;
3125
3126     lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3127
3128     return WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3129                                 (LPSTR)lParam, 0x7fffffff, NULL, NULL ) - 1;
3130 }
3131
3132
3133 static LRESULT
3134 TOOLBAR_GetButtonTextW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3135 {
3136     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3137     INT nIndex;
3138     LPWSTR lpText;
3139
3140     if (lParam == 0)
3141         return -1;
3142
3143     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3144     if (nIndex == -1)
3145         return -1;
3146
3147     lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3148
3149     strcpyW ((LPWSTR)lParam, lpText);
3150
3151     return strlenW (lpText);
3152 }
3153
3154
3155 static LRESULT
3156 TOOLBAR_GetDisabledImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3157 {
3158     return (LRESULT)GETDISIMAGELIST(TOOLBAR_GetInfoPtr (hwnd), 0);
3159 }
3160
3161
3162 inline static LRESULT
3163 TOOLBAR_GetExtendedStyle (HWND hwnd)
3164 {
3165     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3166
3167     return infoPtr->dwExStyle;
3168 }
3169
3170
3171 static LRESULT
3172 TOOLBAR_GetHotImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3173 {
3174     return (LRESULT)GETHOTIMAGELIST(TOOLBAR_GetInfoPtr (hwnd), 0);
3175 }
3176
3177
3178 static LRESULT
3179 TOOLBAR_GetHotItem (HWND hwnd)
3180 {
3181     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3182
3183     if (!(GetWindowLongA (hwnd, GWL_STYLE) & TBSTYLE_FLAT))
3184         return -1;
3185
3186     if (infoPtr->nHotItem < 0)
3187         return -1;
3188
3189     return (LRESULT)infoPtr->nHotItem;
3190 }
3191
3192
3193 static LRESULT
3194 TOOLBAR_GetDefImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3195 {
3196     return (LRESULT) GETDEFIMAGELIST(TOOLBAR_GetInfoPtr(hwnd), 0);
3197 }
3198
3199
3200 /* << TOOLBAR_GetInsertMark >> */
3201 /* << TOOLBAR_GetInsertMarkColor >> */
3202
3203
3204 static LRESULT
3205 TOOLBAR_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
3206 {
3207     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3208     TBUTTON_INFO *btnPtr;
3209     LPRECT     lpRect;
3210     INT        nIndex;
3211
3212     if (infoPtr == NULL)
3213         return FALSE;
3214     nIndex = (INT)wParam;
3215     btnPtr = &infoPtr->buttons[nIndex];
3216     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3217         return FALSE;
3218     lpRect = (LPRECT)lParam;
3219     if (lpRect == NULL)
3220         return FALSE;
3221     if (btnPtr->fsState & TBSTATE_HIDDEN)
3222         return FALSE;
3223
3224     lpRect->left   = btnPtr->rect.left;
3225     lpRect->right  = btnPtr->rect.right;
3226     lpRect->bottom = btnPtr->rect.bottom;
3227     lpRect->top    = btnPtr->rect.top;
3228
3229     return TRUE;
3230 }
3231
3232
3233 static LRESULT
3234 TOOLBAR_GetMaxSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
3235 {
3236     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3237     LPSIZE lpSize = (LPSIZE)lParam;
3238
3239     if (lpSize == NULL)
3240         return FALSE;
3241
3242     lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3243     lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3244
3245     TRACE("maximum size %d x %d\n",
3246            infoPtr->rcBound.right - infoPtr->rcBound.left,
3247            infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3248
3249     return TRUE;
3250 }
3251
3252
3253 /* << TOOLBAR_GetObject >> */
3254
3255
3256 static LRESULT
3257 TOOLBAR_GetPadding (HWND hwnd)
3258 {
3259     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3260     DWORD oldPad;
3261
3262     oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3263     return (LRESULT) oldPad;
3264 }
3265
3266
3267 static LRESULT
3268 TOOLBAR_GetRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
3269 {
3270     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3271     TBUTTON_INFO *btnPtr;
3272     LPRECT     lpRect;
3273     INT        nIndex;
3274
3275     if (infoPtr == NULL)
3276         return FALSE;
3277     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3278     btnPtr = &infoPtr->buttons[nIndex];
3279     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3280         return FALSE;
3281     lpRect = (LPRECT)lParam;
3282     if (lpRect == NULL)
3283         return FALSE;
3284
3285     lpRect->left   = btnPtr->rect.left;
3286     lpRect->right  = btnPtr->rect.right;
3287     lpRect->bottom = btnPtr->rect.bottom;
3288     lpRect->top    = btnPtr->rect.top;
3289
3290     return TRUE;
3291 }
3292
3293
3294 static LRESULT
3295 TOOLBAR_GetRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
3296 {
3297     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3298
3299     if (GetWindowLongA (hwnd, GWL_STYLE) & TBSTYLE_WRAPABLE)
3300         return infoPtr->nRows;
3301     else
3302         return 1;
3303 }
3304
3305
3306 static LRESULT
3307 TOOLBAR_GetState (HWND hwnd, WPARAM wParam, LPARAM lParam)
3308 {
3309     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3310     INT nIndex;
3311
3312     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3313     if (nIndex == -1)
3314         return -1;
3315
3316     return infoPtr->buttons[nIndex].fsState;
3317 }
3318
3319
3320 static LRESULT
3321 TOOLBAR_GetStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
3322 {
3323     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3324     INT nIndex;
3325
3326     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3327     if (nIndex == -1)
3328         return -1;
3329
3330     return infoPtr->buttons[nIndex].fsStyle;
3331 }
3332
3333
3334 static LRESULT
3335 TOOLBAR_GetTextRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
3336 {
3337     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3338
3339     if (infoPtr == NULL)
3340         return 0;
3341
3342     return infoPtr->nMaxTextRows;
3343 }
3344
3345
3346 static LRESULT
3347 TOOLBAR_GetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
3348 {
3349     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3350
3351     if (infoPtr == NULL)
3352         return 0;
3353     return (LRESULT)infoPtr->hwndToolTip;
3354 }
3355
3356
3357 static LRESULT
3358 TOOLBAR_GetUnicodeFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
3359 {
3360     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3361
3362     TRACE("%s hwnd=%p stub!\n",
3363            infoPtr->bUnicode ? "TRUE" : "FALSE", hwnd);
3364
3365     return infoPtr->bUnicode;
3366 }
3367
3368
3369 inline static LRESULT
3370 TOOLBAR_GetVersion (HWND hwnd)
3371 {
3372     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3373     return infoPtr->iVersion;
3374 }
3375
3376
3377 static LRESULT
3378 TOOLBAR_HideButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3379 {
3380     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3381     TBUTTON_INFO *btnPtr;
3382     INT nIndex;
3383
3384     TRACE("\n");
3385
3386     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3387     if (nIndex == -1)
3388         return FALSE;
3389
3390     btnPtr = &infoPtr->buttons[nIndex];
3391     if (LOWORD(lParam) == FALSE)
3392         btnPtr->fsState &= ~TBSTATE_HIDDEN;
3393     else
3394         btnPtr->fsState |= TBSTATE_HIDDEN;
3395
3396     TOOLBAR_CalcToolbar (hwnd);
3397
3398     InvalidateRect (hwnd, NULL, TRUE);
3399
3400     return TRUE;
3401 }
3402
3403
3404 inline static LRESULT
3405 TOOLBAR_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
3406 {
3407     return TOOLBAR_InternalHitTest (hwnd, (LPPOINT)lParam);
3408 }
3409
3410
3411 static LRESULT
3412 TOOLBAR_Indeterminate (HWND hwnd, WPARAM wParam, LPARAM lParam)
3413 {
3414     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3415     TBUTTON_INFO *btnPtr;
3416     INT nIndex;
3417
3418     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3419     if (nIndex == -1)
3420         return FALSE;
3421
3422     btnPtr = &infoPtr->buttons[nIndex];
3423     if (LOWORD(lParam) == FALSE)
3424         btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3425     else
3426         btnPtr->fsState |= TBSTATE_INDETERMINATE;
3427
3428     InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr, btnPtr));
3429
3430     return TRUE;
3431 }
3432
3433
3434 static LRESULT
3435 TOOLBAR_InsertButtonA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3436 {
3437     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3438     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3439     INT nIndex = (INT)wParam;
3440     TBUTTON_INFO *oldButtons;
3441
3442     if (lpTbb == NULL)
3443         return FALSE;
3444
3445     TOOLBAR_DumpButton(infoPtr, (TBUTTON_INFO *)lpTbb, nIndex, FALSE);
3446
3447     if (nIndex == -1) {
3448        /* EPP: this seems to be an undocumented call (from my IE4)
3449         * I assume in that case that:
3450         * - lpTbb->iString is a string pointer (not a string index in strings[] table
3451         * - index of insertion is at the end of existing buttons
3452         * I only see this happen with nIndex == -1, but it could have a special
3453         * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3454         */
3455         nIndex = infoPtr->nNumButtons;
3456
3457     } else if (nIndex < 0)
3458        return FALSE;
3459
3460     /* If the string passed is not an index, assume address of string
3461        and do our own AddString */
3462     if ((HIWORD(lpTbb->iString) != 0) && (lpTbb->iString != -1)) {
3463         LPSTR ptr;
3464         INT len;
3465
3466         TRACE("string %s passed instead of index, adding string\n",
3467               debugstr_a((LPSTR)lpTbb->iString));
3468         len = strlen((LPSTR)lpTbb->iString) + 2;
3469         ptr = COMCTL32_Alloc(len);
3470         strcpy(ptr, (LPSTR)lpTbb->iString);
3471         ptr[len - 1] = 0; /* ended by two '\0' */
3472         lpTbb->iString = TOOLBAR_AddStringA(hwnd, 0, (LPARAM)ptr);
3473         COMCTL32_Free(ptr);
3474     }
3475
3476     TRACE("inserting button index=%d\n", nIndex);
3477     if (nIndex > infoPtr->nNumButtons) {
3478         nIndex = infoPtr->nNumButtons;
3479         TRACE("adjust index=%d\n", nIndex);
3480     }
3481
3482     oldButtons = infoPtr->buttons;
3483     infoPtr->nNumButtons++;
3484     infoPtr->buttons = COMCTL32_Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3485     /* pre insert copy */
3486     if (nIndex > 0) {
3487         memcpy (&infoPtr->buttons[0], &oldButtons[0],
3488                 nIndex * sizeof(TBUTTON_INFO));
3489     }
3490
3491     /* insert new button */
3492     infoPtr->buttons[nIndex].iBitmap   = lpTbb->iBitmap;
3493     infoPtr->buttons[nIndex].idCommand = lpTbb->idCommand;
3494     infoPtr->buttons[nIndex].fsState   = lpTbb->fsState;
3495     infoPtr->buttons[nIndex].fsStyle   = lpTbb->fsStyle;
3496     infoPtr->buttons[nIndex].dwData    = lpTbb->dwData;
3497     /* if passed string and not index, then add string */
3498     if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) {
3499         Str_SetPtrAtoW ((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPCSTR )lpTbb->iString);
3500     }
3501     else
3502         infoPtr->buttons[nIndex].iString   = lpTbb->iString;
3503
3504     if ((infoPtr->hwndToolTip) && !(lpTbb->fsStyle & TBSTYLE_SEP)) {
3505         TTTOOLINFOA ti;
3506
3507         ZeroMemory (&ti, sizeof(TTTOOLINFOA));
3508         ti.cbSize   = sizeof (TTTOOLINFOA);
3509         ti.hwnd     = hwnd;
3510         ti.uId      = lpTbb->idCommand;
3511         ti.hinst    = 0;
3512         ti.lpszText = LPSTR_TEXTCALLBACKA;
3513
3514         SendMessageA (infoPtr->hwndToolTip, TTM_ADDTOOLA,
3515                         0, (LPARAM)&ti);
3516     }
3517
3518     /* post insert copy */
3519     if (nIndex < infoPtr->nNumButtons - 1) {
3520         memcpy (&infoPtr->buttons[nIndex+1], &oldButtons[nIndex],
3521                 (infoPtr->nNumButtons - nIndex - 1) * sizeof(TBUTTON_INFO));
3522     }
3523
3524     COMCTL32_Free (oldButtons);
3525
3526     TOOLBAR_CalcToolbar (hwnd);
3527
3528     InvalidateRect (hwnd, NULL, TRUE);
3529
3530     return TRUE;
3531 }
3532
3533
3534 static LRESULT
3535 TOOLBAR_InsertButtonW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3536 {
3537     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3538     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3539     INT nIndex = (INT)wParam;
3540     TBUTTON_INFO *oldButtons;
3541
3542     if (lpTbb == NULL)
3543         return FALSE;
3544
3545     TOOLBAR_DumpButton(infoPtr, (TBUTTON_INFO *)lpTbb, nIndex, FALSE);
3546
3547     if (nIndex == -1) {
3548        /* EPP: this seems to be an undocumented call (from my IE4)
3549         * I assume in that case that:
3550         * - lpTbb->iString is a string pointer (not a string index in strings[] table
3551         * - index of insertion is at the end of existing buttons
3552         * I only see this happen with nIndex == -1, but it could have a special
3553         * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3554         */
3555         nIndex = infoPtr->nNumButtons;
3556
3557     } else if (nIndex < 0)
3558        return FALSE;
3559
3560     /* If the string passed is not an index, assume address of string
3561        and do our own AddString */
3562     if ((HIWORD(lpTbb->iString) != 0) && (lpTbb->iString != -1)) {
3563         LPWSTR ptr;
3564         INT len;
3565
3566         TRACE("string %s passed instead of index, adding string\n",
3567               debugstr_w((LPWSTR)lpTbb->iString));
3568         len = strlenW((LPWSTR)lpTbb->iString) + 2;
3569         ptr = COMCTL32_Alloc(len*sizeof(WCHAR));
3570         strcpyW(ptr, (LPWSTR)lpTbb->iString);
3571         ptr[len - 1] = 0; /* ended by two '\0' */
3572         lpTbb->iString = TOOLBAR_AddStringW(hwnd, 0, (LPARAM)ptr);
3573         COMCTL32_Free(ptr);
3574     }
3575
3576     TRACE("inserting button index=%d\n", nIndex);
3577     if (nIndex > infoPtr->nNumButtons) {
3578         nIndex = infoPtr->nNumButtons;
3579         TRACE("adjust index=%d\n", nIndex);
3580     }
3581
3582     oldButtons = infoPtr->buttons;
3583     infoPtr->nNumButtons++;
3584     infoPtr->buttons = COMCTL32_Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3585     /* pre insert copy */
3586     if (nIndex > 0) {
3587         memcpy (&infoPtr->buttons[0], &oldButtons[0],
3588                 nIndex * sizeof(TBUTTON_INFO));
3589     }
3590
3591     /* insert new button */
3592     infoPtr->buttons[nIndex].iBitmap   = lpTbb->iBitmap;
3593     infoPtr->buttons[nIndex].idCommand = lpTbb->idCommand;
3594     infoPtr->buttons[nIndex].fsState   = lpTbb->fsState;
3595     infoPtr->buttons[nIndex].fsStyle   = lpTbb->fsStyle;
3596     infoPtr->buttons[nIndex].dwData    = lpTbb->dwData;
3597     /* if passed string and not index, then add string */
3598     if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) {
3599         Str_SetPtrW ((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPWSTR)lpTbb->iString);
3600     }
3601     else
3602         infoPtr->buttons[nIndex].iString   = lpTbb->iString;
3603
3604     if ((infoPtr->hwndToolTip) && !(lpTbb->fsStyle & TBSTYLE_SEP)) {
3605         TTTOOLINFOW ti;
3606
3607         ZeroMemory (&ti, sizeof(TTTOOLINFOW));
3608         ti.cbSize   = sizeof (TTTOOLINFOW);
3609         ti.hwnd     = hwnd;
3610         ti.uId      = lpTbb->idCommand;
3611         ti.hinst    = 0;
3612         ti.lpszText = LPSTR_TEXTCALLBACKW;
3613
3614         SendMessageW (infoPtr->hwndToolTip, TTM_ADDTOOLW,
3615                         0, (LPARAM)&ti);
3616     }
3617
3618     /* post insert copy */
3619     if (nIndex < infoPtr->nNumButtons - 1) {
3620         memcpy (&infoPtr->buttons[nIndex+1], &oldButtons[nIndex],
3621                 (infoPtr->nNumButtons - nIndex - 1) * sizeof(TBUTTON_INFO));
3622     }
3623
3624     COMCTL32_Free (oldButtons);
3625
3626     TOOLBAR_CalcToolbar (hwnd);
3627
3628     InvalidateRect (hwnd, NULL, TRUE);
3629
3630     return TRUE;
3631 }
3632
3633
3634 /* << TOOLBAR_InsertMarkHitTest >> */
3635
3636
3637 static LRESULT
3638 TOOLBAR_IsButtonChecked (HWND hwnd, WPARAM wParam, LPARAM lParam)
3639 {
3640     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3641     INT nIndex;
3642
3643     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3644     if (nIndex == -1)
3645         return FALSE;
3646
3647     return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3648 }
3649
3650
3651 static LRESULT
3652 TOOLBAR_IsButtonEnabled (HWND hwnd, WPARAM wParam, LPARAM lParam)
3653 {
3654     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3655     INT nIndex;
3656
3657     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3658     if (nIndex == -1)
3659         return FALSE;
3660
3661     return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3662 }
3663
3664
3665 static LRESULT
3666 TOOLBAR_IsButtonHidden (HWND hwnd, WPARAM wParam, LPARAM lParam)
3667 {
3668     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3669     INT nIndex;
3670
3671     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3672     if (nIndex == -1)
3673         return TRUE;
3674
3675     return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3676 }
3677
3678
3679 static LRESULT
3680 TOOLBAR_IsButtonHighlighted (HWND hwnd, WPARAM wParam, LPARAM lParam)
3681 {
3682     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3683     INT nIndex;
3684
3685     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3686     if (nIndex == -1)
3687         return FALSE;
3688
3689     return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3690 }
3691
3692
3693 static LRESULT
3694 TOOLBAR_IsButtonIndeterminate (HWND hwnd, WPARAM wParam, LPARAM lParam)
3695 {
3696     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3697     INT nIndex;
3698
3699     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3700     if (nIndex == -1)
3701         return FALSE;
3702
3703     return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3704 }
3705
3706
3707 static LRESULT
3708 TOOLBAR_IsButtonPressed (HWND hwnd, WPARAM wParam, LPARAM lParam)
3709 {
3710     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3711     INT nIndex;
3712
3713     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3714     if (nIndex == -1)
3715         return FALSE;
3716
3717     return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3718 }
3719
3720
3721 /* << TOOLBAR_LoadImages >> */
3722 /* << TOOLBAR_MapAccelerator >> */
3723 /* << TOOLBAR_MarkButton >> */
3724 /* << TOOLBAR_MoveButton >> */
3725
3726
3727 static LRESULT
3728 TOOLBAR_PressButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3729 {
3730     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3731     TBUTTON_INFO *btnPtr;
3732     INT nIndex;
3733
3734     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3735     if (nIndex == -1)
3736         return FALSE;
3737
3738     btnPtr = &infoPtr->buttons[nIndex];
3739     if (LOWORD(lParam) == FALSE)
3740         btnPtr->fsState &= ~TBSTATE_PRESSED;
3741     else
3742         btnPtr->fsState |= TBSTATE_PRESSED;
3743
3744     InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr, btnPtr));
3745
3746     return TRUE;
3747 }
3748
3749
3750 static LRESULT
3751 TOOLBAR_ReplaceBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3752 {
3753     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3754     LPTBREPLACEBITMAP lpReplace = (LPTBREPLACEBITMAP) lParam;
3755     HBITMAP hBitmap;
3756     int i = 0, nOldButtons = 0, pos = 0;
3757     HIMAGELIST himlDef = 0;
3758
3759     TRACE("hInstOld %p nIDOld %x hInstNew %p nIDNew %x nButtons %x\n",
3760           lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
3761           lpReplace->nButtons);
3762
3763     if (lpReplace->hInstOld == HINST_COMMCTRL)
3764     {
3765         FIXME("changing standard bitmaps not implemented\n");
3766         return FALSE;
3767     }
3768     else if (lpReplace->hInstOld != 0)
3769     {
3770         FIXME("resources not in the current module not implemented\n");
3771         return FALSE;
3772     }
3773     else
3774     {
3775         hBitmap = (HBITMAP) lpReplace->nIDNew;
3776     }
3777
3778     TRACE("To be replaced hInstOld %p nIDOld %x\n", lpReplace->hInstOld, lpReplace->nIDOld);
3779     for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
3780         TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
3781         TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
3782         if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
3783         {
3784             TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
3785             nOldButtons = tbi->nButtons;
3786             tbi->nButtons = lpReplace->nButtons;
3787             tbi->hInst = lpReplace->hInstNew;
3788             tbi->nID = lpReplace->nIDNew;
3789             TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
3790             break;
3791         }
3792         pos += tbi->nButtons;
3793     }
3794
3795     if (nOldButtons == 0)
3796     {
3797         WARN("No hinst/bitmap found! hInst %p nID %x\n", lpReplace->hInstOld, lpReplace->nIDOld);
3798         return FALSE;
3799     }
3800
3801     infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldButtons + lpReplace->nButtons;
3802
3803     /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
3804
3805
3806         himlDef = GETDEFIMAGELIST(infoPtr, 0);
3807     for (i = pos + nOldButtons - 1; i >= pos; i--) {
3808         ImageList_Remove(himlDef, i);
3809     }
3810
3811     ImageList_AddMasked(himlDef, hBitmap, CLR_DEFAULT);
3812
3813     InvalidateRect(hwnd, NULL, FALSE);
3814
3815     return TRUE;
3816 }
3817
3818 static LRESULT
3819 TOOLBAR_SaveRestoreA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3820 {
3821 #if 0
3822     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3823     LPTBSAVEPARAMSA lpSave = (LPTBSAVEPARAMSA)lParam;
3824
3825     if (lpSave == NULL) return 0;
3826
3827     if ((BOOL)wParam) {
3828         /* save toolbar information */
3829         FIXME("save to \"%s\" \"%s\"\n",
3830                lpSave->pszSubKey, lpSave->pszValueName);
3831
3832
3833     }
3834     else {
3835         /* restore toolbar information */
3836
3837         FIXME("restore from \"%s\" \"%s\"\n",
3838                lpSave->pszSubKey, lpSave->pszValueName);
3839
3840
3841     }
3842 #endif
3843
3844     return 0;
3845 }
3846
3847
3848 static LRESULT
3849 TOOLBAR_SaveRestoreW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3850 {
3851 #if 0
3852     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3853     LPTBSAVEPARAMSW lpSave = (LPTBSAVEPARAMSW)lParam;
3854
3855     if (lpSave == NULL)
3856         return 0;
3857
3858     if ((BOOL)wParam) {
3859         /* save toolbar information */
3860         FIXME("save to \"%s\" \"%s\"\n",
3861                lpSave->pszSubKey, lpSave->pszValueName);
3862
3863
3864     }
3865     else {
3866         /* restore toolbar information */
3867
3868         FIXME("restore from \"%s\" \"%s\"\n",
3869                lpSave->pszSubKey, lpSave->pszValueName);
3870
3871
3872     }
3873 #endif
3874
3875     return 0;
3876 }
3877
3878
3879 static LRESULT
3880 TOOLBAR_SetAnchorHighlight (HWND hwnd, WPARAM wParam)
3881 {
3882     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3883     BOOL bOldAnchor = infoPtr->bAnchor;
3884
3885     infoPtr->bAnchor = (BOOL)wParam;
3886
3887     return (LRESULT)bOldAnchor;
3888 }
3889
3890
3891 static LRESULT
3892 TOOLBAR_SetBitmapSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
3893 {
3894     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3895     HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
3896
3897     if ((LOWORD(lParam) <= 0) || (HIWORD(lParam)<=0))
3898         return FALSE;
3899
3900     if (infoPtr->nNumButtons > 0)
3901         WARN("%d buttons, undoc increase to bitmap size : %d-%d -> %d-%d\n",
3902              infoPtr->nNumButtons,
3903              infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
3904              LOWORD(lParam), HIWORD(lParam));
3905
3906     infoPtr->nBitmapWidth = (INT)LOWORD(lParam);
3907     infoPtr->nBitmapHeight = (INT)HIWORD(lParam);
3908
3909     /* uses image list internals directly */
3910     if (himlDef) {
3911         himlDef->cx = infoPtr->nBitmapWidth;
3912         himlDef->cy = infoPtr->nBitmapHeight;
3913     }
3914
3915     return TRUE;
3916 }
3917
3918
3919 static LRESULT
3920 TOOLBAR_SetButtonInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3921 {
3922     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3923     LPTBBUTTONINFOA lptbbi = (LPTBBUTTONINFOA)lParam;
3924     TBUTTON_INFO *btnPtr;
3925     INT nIndex;
3926
3927     if (lptbbi == NULL)
3928         return FALSE;
3929     if (lptbbi->cbSize < sizeof(TBBUTTONINFOA))
3930         return FALSE;
3931
3932     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
3933                                      lptbbi->dwMask & 0x80000000);
3934     if (nIndex == -1)
3935         return FALSE;
3936
3937     btnPtr = &infoPtr->buttons[nIndex];
3938     if (lptbbi->dwMask & TBIF_COMMAND)
3939         btnPtr->idCommand = lptbbi->idCommand;
3940     if (lptbbi->dwMask & TBIF_IMAGE)
3941         btnPtr->iBitmap = lptbbi->iImage;
3942     if (lptbbi->dwMask & TBIF_LPARAM)
3943         btnPtr->dwData = lptbbi->lParam;
3944 /*    if (lptbbi->dwMask & TBIF_SIZE) */
3945 /*      btnPtr->cx = lptbbi->cx; */
3946     if (lptbbi->dwMask & TBIF_STATE)
3947         btnPtr->fsState = lptbbi->fsState;
3948     if (lptbbi->dwMask & TBIF_STYLE)
3949         btnPtr->fsStyle = lptbbi->fsStyle;
3950
3951     if ((lptbbi->dwMask & TBIF_TEXT) && ((INT)lptbbi->pszText != -1)) {
3952         if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1))
3953             /* iString is index, zero it to make Str_SetPtr succeed */
3954             btnPtr->iString=0;
3955
3956          Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
3957     }
3958     return TRUE;
3959 }
3960
3961
3962 static LRESULT
3963 TOOLBAR_SetButtonInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3964 {
3965     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3966     LPTBBUTTONINFOW lptbbi = (LPTBBUTTONINFOW)lParam;
3967     TBUTTON_INFO *btnPtr;
3968     INT nIndex;
3969
3970     if (lptbbi == NULL)
3971         return FALSE;
3972     if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
3973         return FALSE;
3974
3975     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
3976                                      lptbbi->dwMask & 0x80000000);
3977     if (nIndex == -1)
3978         return FALSE;
3979
3980     btnPtr = &infoPtr->buttons[nIndex];
3981     if (lptbbi->dwMask & TBIF_COMMAND)
3982         btnPtr->idCommand = lptbbi->idCommand;
3983     if (lptbbi->dwMask & TBIF_IMAGE)
3984         btnPtr->iBitmap = lptbbi->iImage;
3985     if (lptbbi->dwMask & TBIF_LPARAM)
3986         btnPtr->dwData = lptbbi->lParam;
3987 /*    if (lptbbi->dwMask & TBIF_SIZE) */
3988 /*      btnPtr->cx = lptbbi->cx; */
3989     if (lptbbi->dwMask & TBIF_STATE)
3990         btnPtr->fsState = lptbbi->fsState;
3991     if (lptbbi->dwMask & TBIF_STYLE)
3992         btnPtr->fsStyle = lptbbi->fsStyle;
3993
3994     if ((lptbbi->dwMask & TBIF_TEXT) && ((INT)lptbbi->pszText != -1)) {
3995         if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1))
3996             /* iString is index, zero it to make Str_SetPtr succeed */
3997             btnPtr->iString=0;
3998         Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
3999     }
4000     return TRUE;
4001 }
4002
4003
4004 static LRESULT
4005 TOOLBAR_SetButtonSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
4006 {
4007     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4008     INT cx = LOWORD(lParam), cy = HIWORD(lParam);
4009
4010     if ((cx < 0) || (cy < 0))
4011     {
4012         ERR("invalid parameter 0x%08lx\n", (DWORD)lParam);
4013         return FALSE;
4014     }
4015
4016     /* The documentation claims you can only change the button size before
4017      * any button has been added. But this is wrong.
4018      * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4019      * it to the toolbar, and it checks that the return value is nonzero - mjm
4020      * Further testing shows that we must actually perform the change too.
4021      */
4022     /*
4023      * The documentation also does not mention that if 0 is supplied for
4024      * either size, the system changes it to the default of 24 wide and
4025      * 22 high. Demonstarted in ControlSpy Toolbar. GLA 3/02
4026      */
4027     infoPtr->nButtonWidth = (cx) ? cx : 24;
4028     infoPtr->nButtonHeight = (cy) ? cy : 22;
4029     return TRUE;
4030 }
4031
4032
4033 static LRESULT
4034 TOOLBAR_SetButtonWidth (HWND hwnd, WPARAM wParam, LPARAM lParam)
4035 {
4036     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4037
4038     if (infoPtr == NULL) {
4039         TRACE("Toolbar not initialized yet?????\n");
4040         return FALSE;
4041     }
4042
4043     /* if setting to current values, ignore */
4044     if ((infoPtr->cxMin == (INT)LOWORD(lParam)) &&
4045         (infoPtr->cxMax == (INT)HIWORD(lParam))) {
4046         TRACE("matches current width, min=%d, max=%d, no recalc\n",
4047               infoPtr->cxMin, infoPtr->cxMax);
4048         return TRUE;
4049     }
4050
4051     /* save new values */
4052     infoPtr->cxMin = (INT)LOWORD(lParam);
4053     infoPtr->cxMax = (INT)HIWORD(lParam);
4054
4055     /* if both values are 0 then we are done */
4056     if (lParam == 0) {
4057         TRACE("setting both min and max to 0, norecalc\n");
4058         return TRUE;
4059     }
4060
4061     /* otherwise we need to recalc the toolbar and in some cases
4062        recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4063        which doesn't actually draw - GA). */
4064     TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4065         infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4066
4067     TOOLBAR_CalcToolbar (hwnd);
4068
4069     InvalidateRect (hwnd, NULL, TRUE);
4070
4071     return TRUE;
4072 }
4073
4074
4075 static LRESULT
4076 TOOLBAR_SetCmdId (HWND hwnd, WPARAM wParam, LPARAM lParam)
4077 {
4078     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4079     INT nIndex = (INT)wParam;
4080
4081     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4082         return FALSE;
4083
4084     infoPtr->buttons[nIndex].idCommand = (INT)lParam;
4085
4086     if (infoPtr->hwndToolTip) {
4087
4088         FIXME("change tool tip!\n");
4089
4090     }
4091
4092     return TRUE;
4093 }
4094
4095
4096 static LRESULT
4097 TOOLBAR_SetDisabledImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4098 {
4099     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4100     HIMAGELIST himl = (HIMAGELIST)lParam;
4101     HIMAGELIST himlTemp;
4102     INT id = 0;
4103
4104     if (infoPtr->iVersion >= 5)
4105         id = wParam;
4106
4107     himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis, 
4108         &infoPtr->cimlDis, himl, id);
4109
4110     /* FIXME: redraw ? */
4111
4112     return (LRESULT)himlTemp;
4113 }
4114
4115
4116 static LRESULT
4117 TOOLBAR_SetDrawTextFlags (HWND hwnd, WPARAM wParam, LPARAM lParam)
4118 {
4119     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4120     DWORD dwTemp;
4121
4122     dwTemp = infoPtr->dwDTFlags;
4123     infoPtr->dwDTFlags =
4124         (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4125
4126     return (LRESULT)dwTemp;
4127 }
4128
4129 static LRESULT
4130 TOOLBAR_SetExtendedStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
4131 {
4132     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4133     DWORD dwTemp;
4134
4135     dwTemp = infoPtr->dwExStyle;
4136     infoPtr->dwExStyle = (DWORD)lParam;
4137
4138     if (infoPtr->dwExStyle & (TBSTYLE_EX_MIXEDBUTTONS |
4139                               TBSTYLE_EX_HIDECLIPPEDBUTTONS)) {
4140         FIXME("Extended style not implemented %s %s\n",
4141               (infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ?
4142               "TBSTYLE_EX_MIXEDBUTTONS" : "",
4143               (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS) ?
4144               "TBSTYLE_EX_HIDECLIPPEDBUTTONS" : "");
4145     }
4146
4147     if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4148         FIXME("Unknown Toolbar Extended Style 0x%08lx. Please report.\n",
4149               (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4150
4151     return (LRESULT)dwTemp;
4152 }
4153
4154
4155 static LRESULT
4156 TOOLBAR_SetHotImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4157 {
4158     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4159     HIMAGELIST himlTemp;
4160     HIMAGELIST himl = (HIMAGELIST)lParam;
4161     INT id = 0;
4162
4163     if (infoPtr->iVersion >= 5)
4164         id = wParam;
4165
4166     himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot, 
4167         &infoPtr->cimlHot, himl, id);
4168
4169     /* FIXME: redraw ? */
4170
4171     return (LRESULT)himlTemp;
4172 }
4173
4174
4175 static LRESULT
4176 TOOLBAR_SetHotItem (HWND hwnd, WPARAM wParam)
4177 {
4178     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4179     INT nOldHotItem = infoPtr->nHotItem;
4180     TBUTTON_INFO *btnPtr;
4181
4182     if ((INT) wParam < 0 || (INT)wParam > infoPtr->nNumButtons)
4183         wParam = -2;
4184
4185     if (GetWindowLongA (hwnd, GWL_STYLE) & TBSTYLE_FLAT)
4186     {
4187
4188         infoPtr->nHotItem = (INT)wParam;
4189         if ((INT)wParam >=0)
4190         {
4191             btnPtr = &infoPtr->buttons[(INT)wParam];
4192             btnPtr->bHot = TRUE;
4193                 InvalidateRect (hwnd, &btnPtr->rect,
4194                     TOOLBAR_HasText(infoPtr, btnPtr));
4195         }
4196         if (nOldHotItem>=0)
4197         {
4198             btnPtr = &infoPtr->buttons[nOldHotItem];
4199             btnPtr->bHot = FALSE;
4200                 InvalidateRect (hwnd, &btnPtr->rect,
4201                     TOOLBAR_HasText(infoPtr, btnPtr));
4202         }
4203     }
4204
4205     if (nOldHotItem < 0)
4206         return -1;
4207
4208     return (LRESULT)nOldHotItem;
4209 }
4210
4211
4212 static LRESULT
4213 TOOLBAR_SetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4214 {
4215     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4216     HIMAGELIST himlTemp;
4217     HIMAGELIST himl = (HIMAGELIST)lParam;
4218     INT i, id = 0;
4219
4220     if (infoPtr->iVersion >= 5)
4221         id = wParam;
4222
4223     himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef, 
4224         &infoPtr->cimlDef, himl, id);
4225
4226     infoPtr->nNumBitmaps = 0;
4227     for (i = 0; i < infoPtr->cimlDef; i++)
4228         infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4229
4230     ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4231                           &infoPtr->nBitmapHeight);
4232     TRACE("hwnd %p, new himl=%08x, count=%d, bitmap w=%d, h=%d\n",
4233           hwnd, (INT)infoPtr->himlDef, infoPtr->nNumBitmaps,
4234           infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4235
4236     /* FIXME: redraw ? */
4237     InvalidateRect(hwnd, NULL, TRUE);
4238
4239     return (LRESULT)himlTemp;
4240 }
4241
4242
4243 static LRESULT
4244 TOOLBAR_SetIndent (HWND hwnd, WPARAM wParam, LPARAM lParam)
4245 {
4246     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4247
4248     infoPtr->nIndent = (INT)wParam;
4249
4250     TRACE("\n");
4251
4252     /* process only on indent changing */
4253     if(infoPtr->nIndent != (INT)wParam)
4254     {
4255         infoPtr->nIndent = (INT)wParam;
4256         TOOLBAR_CalcToolbar (hwnd);
4257         InvalidateRect(hwnd, NULL, FALSE);
4258     }
4259
4260     return TRUE;
4261 }
4262
4263
4264 /* << TOOLBAR_SetInsertMark >> */
4265
4266
4267 static LRESULT
4268 TOOLBAR_SetInsertMarkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
4269 {
4270     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4271
4272     infoPtr->clrInsertMark = (COLORREF)lParam;
4273
4274     /* FIXME : redraw ??*/
4275
4276     return 0;
4277 }
4278
4279
4280 static LRESULT
4281 TOOLBAR_SetMaxTextRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
4282 {
4283     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4284
4285     if (infoPtr == NULL)
4286         return FALSE;
4287
4288     infoPtr->nMaxTextRows = (INT)wParam;
4289
4290     return TRUE;
4291 }
4292
4293
4294 static LRESULT
4295 TOOLBAR_SetPadding (HWND hwnd, WPARAM wParam, LPARAM lParam)
4296 {
4297     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4298     DWORD  oldPad;
4299
4300     oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4301     infoPtr->szPadding.cx = LOWORD((DWORD)lParam);
4302     infoPtr->szPadding.cy = HIWORD((DWORD)lParam);
4303     FIXME("stub - nothing done with values, cx=%ld, cy=%ld\n",
4304           infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4305     return (LRESULT) oldPad;
4306 }
4307
4308
4309 static LRESULT
4310 TOOLBAR_SetParent (HWND hwnd, WPARAM wParam, LPARAM lParam)
4311 {
4312     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4313     HWND hwndOldNotify;
4314
4315     TRACE("\n");
4316
4317     if (infoPtr == NULL)
4318         return 0;
4319     hwndOldNotify = infoPtr->hwndNotify;
4320     infoPtr->hwndNotify = (HWND)wParam;
4321
4322     return (LRESULT)hwndOldNotify;
4323 }
4324
4325
4326 static LRESULT
4327 TOOLBAR_SetRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
4328 {
4329     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4330     LPRECT lprc = (LPRECT)lParam;
4331
4332     TRACE("\n");
4333
4334     if (LOWORD(wParam) > 1) {
4335         FIXME("multiple rows not supported!\n");
4336     }
4337
4338     if(infoPtr->nRows != LOWORD(wParam))
4339     {
4340         infoPtr->nRows = LOWORD(wParam);
4341
4342         /* recalculate toolbar */
4343         TOOLBAR_CalcToolbar (hwnd);
4344
4345         /* repaint toolbar */
4346         InvalidateRect(hwnd, NULL, FALSE);
4347     }
4348
4349     /* return bounding rectangle */
4350     if (lprc) {
4351         lprc->left   = infoPtr->rcBound.left;
4352         lprc->right  = infoPtr->rcBound.right;
4353         lprc->top    = infoPtr->rcBound.top;
4354         lprc->bottom = infoPtr->rcBound.bottom;
4355     }
4356
4357     return 0;
4358 }
4359
4360
4361 static LRESULT
4362 TOOLBAR_SetState (HWND hwnd, WPARAM wParam, LPARAM lParam)
4363 {
4364     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4365     TBUTTON_INFO *btnPtr;
4366     INT nIndex;
4367
4368     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
4369     if (nIndex == -1)
4370         return FALSE;
4371
4372     btnPtr = &infoPtr->buttons[nIndex];
4373
4374     /* if hidden state has changed the invalidate entire window and recalc */
4375     if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4376         btnPtr->fsState = LOWORD(lParam);
4377         TOOLBAR_CalcToolbar (hwnd);
4378         InvalidateRect(hwnd, 0, TOOLBAR_HasText(infoPtr, btnPtr));
4379         return TRUE;
4380     }
4381
4382     /* process state changing if current state doesn't match new state */
4383     if(btnPtr->fsState != LOWORD(lParam))
4384     {
4385         btnPtr->fsState = LOWORD(lParam);
4386         InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr,
4387             btnPtr));
4388     }
4389
4390     return TRUE;
4391 }
4392
4393
4394 static LRESULT
4395 TOOLBAR_SetStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
4396 {
4397     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4398     TBUTTON_INFO *btnPtr;
4399     INT nIndex;
4400
4401     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
4402     if (nIndex == -1)
4403         return FALSE;
4404
4405     btnPtr = &infoPtr->buttons[nIndex];
4406
4407     /* process style change if current style doesn't match new style */
4408     if(btnPtr->fsStyle != LOWORD(lParam))
4409     {
4410         btnPtr->fsStyle = LOWORD(lParam);
4411         InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr,
4412             btnPtr));
4413
4414         if (infoPtr->hwndToolTip) {
4415             FIXME("change tool tip!\n");
4416         }
4417     }
4418
4419     return TRUE;
4420 }
4421
4422
4423 inline static LRESULT
4424 TOOLBAR_SetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
4425 {
4426     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4427
4428     if (infoPtr == NULL)
4429         return 0;
4430     infoPtr->hwndToolTip = (HWND)wParam;
4431     return 0;
4432 }
4433
4434
4435 static LRESULT
4436 TOOLBAR_SetUnicodeFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
4437 {
4438     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4439     BOOL bTemp;
4440
4441     TRACE("%s hwnd=%p stub!\n",
4442            ((BOOL)wParam) ? "TRUE" : "FALSE", hwnd);
4443
4444     bTemp = infoPtr->bUnicode;
4445     infoPtr->bUnicode = (BOOL)wParam;
4446
4447     return bTemp;
4448 }
4449
4450
4451 static LRESULT
4452 TOOLBAR_GetColorScheme (HWND hwnd, LPCOLORSCHEME lParam)
4453 {
4454     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4455
4456     lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
4457                                comctl32_color.clrBtnHighlight :
4458                                infoPtr->clrBtnHighlight;
4459     lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
4460                            comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
4461     return 1;
4462 }
4463
4464
4465 static LRESULT
4466 TOOLBAR_SetColorScheme (HWND hwnd, LPCOLORSCHEME lParam)
4467 {
4468     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4469
4470     TRACE("new colors Hl=%lx Shd=%lx, old colors Hl=%lx Shd=%lx\n",
4471           lParam->clrBtnHighlight, lParam->clrBtnShadow,
4472           infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
4473
4474     infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
4475     infoPtr->clrBtnShadow = lParam->clrBtnShadow;
4476     InvalidateRect(hwnd, 0, 0);
4477     return 0;
4478 }
4479
4480
4481 static LRESULT
4482 TOOLBAR_SetVersion (HWND hwnd, INT iVersion)
4483 {
4484     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4485     INT iOldVersion = infoPtr->iVersion;
4486
4487     infoPtr->iVersion = iVersion;
4488
4489     if (infoPtr->iVersion >= 5)
4490         TOOLBAR_SetUnicodeFormat(hwnd, (WPARAM)TRUE, (LPARAM)0);
4491
4492     return iOldVersion;
4493 }
4494
4495
4496 /*********************************************************************/
4497 /*                                                                   */
4498 /* This is undocumented and appears to be a "Super" TB_SETHOTITEM    */
4499 /* without the restriction of TBSTYLE_FLAT. This implementation is   */
4500 /* based on relay traces of the native control and IE 5.5            */
4501 /*                                                                   */
4502 /*********************************************************************/
4503 static LRESULT
4504 TOOLBAR_Unkwn45E (HWND hwnd, WPARAM wParam, LPARAM lParam)
4505 {
4506     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4507     INT nOldHotItem = infoPtr->nHotItem;
4508     TBUTTON_INFO *btnPtr;
4509     INT no_hi = 0;
4510     NMTBHOTITEM nmhotitem;
4511
4512     if ((INT) wParam < 0 || (INT)wParam > infoPtr->nNumButtons)
4513         wParam = -2;
4514
4515     infoPtr->nHotItem = (INT)wParam;
4516     if (nOldHotItem != infoPtr->nHotItem) {
4517         nmhotitem.dwFlags = (DWORD)lParam;
4518         if ( !(nmhotitem.dwFlags & HICF_ENTERING) )
4519             nmhotitem.idOld = (nOldHotItem >= 0) ?
4520                 infoPtr->buttons[nOldHotItem].idCommand : 0;
4521         if ( !(nmhotitem.dwFlags & HICF_LEAVING) )
4522             nmhotitem.idNew = (infoPtr->nHotItem >= 0) ?
4523                 infoPtr->buttons[infoPtr->nHotItem].idCommand : 0;
4524         no_hi = TOOLBAR_SendNotify((NMHDR*)&nmhotitem, infoPtr, TBN_HOTITEMCHANGE);
4525     }
4526     if ((INT)wParam >=0) {
4527         btnPtr = &infoPtr->buttons[(INT)wParam];
4528         btnPtr->bHot = (no_hi) ? FALSE : TRUE;
4529         InvalidateRect (hwnd, &btnPtr->rect,
4530                         TOOLBAR_HasText(infoPtr, btnPtr));
4531     }
4532     if (nOldHotItem>=0) {
4533         btnPtr = &infoPtr->buttons[nOldHotItem];
4534         btnPtr->bHot = FALSE;
4535         InvalidateRect (hwnd, &btnPtr->rect,
4536                         TOOLBAR_HasText(infoPtr, btnPtr));
4537     }
4538     GetFocus();
4539     TRACE("old item=%d, new item=%d, flags=%08lx, notify=%d\n",
4540           nOldHotItem, infoPtr->nHotItem, (DWORD)lParam, no_hi);
4541
4542     if (nOldHotItem < 0)
4543         return -1;
4544
4545     return (LRESULT)nOldHotItem;
4546 }
4547
4548
4549 static LRESULT
4550 TOOLBAR_Unkwn463 (HWND hwnd, WPARAM wParam, LPARAM lParam)
4551 {
4552     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4553     LPSIZE lpsize = (LPSIZE)lParam;
4554
4555     if (lpsize == NULL)
4556         return FALSE;
4557
4558     /*
4559      * Testing shows the following:
4560      *   wParam    = 0 adjust cx value
4561      *             = 1 set cy value to max size.
4562      *   lParam    pointer to SIZE structure
4563      *
4564      */
4565     TRACE("[0463] wParam %d, lParam 0x%08lx -> 0x%08lx 0x%08lx\n",
4566           wParam, lParam, lpsize->cx, lpsize->cy);
4567
4568     switch(wParam) {
4569     case 0:
4570         if (lpsize->cx == -1) {
4571             /* **** this is wrong, native measures each button and sets it */
4572             lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
4573         }
4574         else if(HIWORD(lpsize->cx)) {
4575             RECT rc;
4576             HWND hwndParent = GetParent(hwnd);
4577
4578             InvalidateRect(hwnd, 0, 1);
4579             GetWindowRect(hwnd, &rc);
4580             MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
4581             TRACE("mapped to (%d,%d)-(%d,%d)\n",
4582                 rc.left, rc.top, rc.right, rc.bottom);
4583             lpsize->cx = max(rc.right-rc.left,
4584                              infoPtr->rcBound.right - infoPtr->rcBound.left);
4585         }
4586         else {
4587             lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
4588         }
4589         break;
4590     case 1:
4591         lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
4592         /* lpsize->cy = infoPtr->nHeight; */
4593         break;
4594     default:
4595         ERR("Unknown wParam %d for Toolbar message [0463]. Please report\n",
4596             wParam);
4597         return 0;
4598     }
4599     TRACE("[0463] set to -> 0x%08lx 0x%08lx\n",
4600           lpsize->cx, lpsize->cy);
4601     return 1;
4602 }
4603
4604
4605 static LRESULT
4606 TOOLBAR_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
4607 {
4608     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4609     DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
4610     LOGFONTA logFont;
4611
4612     /* initialize info structure */
4613     infoPtr->nButtonHeight = 22;
4614     infoPtr->nButtonWidth = 24;
4615     infoPtr->nBitmapHeight = 15;
4616     infoPtr->nBitmapWidth = 16;
4617
4618     infoPtr->nHeight = infoPtr->nButtonHeight + TOP_BORDER + BOTTOM_BORDER;
4619     infoPtr->nRows = 1;
4620     infoPtr->nMaxTextRows = 1;
4621     infoPtr->cxMin = -1;
4622     infoPtr->cxMax = -1;
4623     infoPtr->nNumBitmaps = 0;
4624     infoPtr->nNumStrings = 0;
4625
4626     infoPtr->bCaptured = FALSE;
4627     infoPtr->bUnicode = IsWindowUnicode (hwnd);
4628     infoPtr->nButtonDown = -1;
4629     infoPtr->nOldHit = -1;
4630     infoPtr->nHotItem = -2; /* It has to be initially different from nOldHit */
4631     infoPtr->hwndNotify = GetParent (hwnd);
4632     infoPtr->bTransparent = (dwStyle & TBSTYLE_TRANSPARENT);
4633     infoPtr->bBtnTranspnt = (dwStyle & (TBSTYLE_FLAT | TBSTYLE_LIST));
4634     infoPtr->dwDTFlags = (dwStyle & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE : DT_CENTER;
4635     infoPtr->bAnchor = FALSE; /* no anchor highlighting */
4636     infoPtr->iVersion = 0;
4637     infoPtr->hwndSelf = hwnd;
4638     infoPtr->bDoRedraw = TRUE;
4639     infoPtr->clrBtnHighlight = CLR_DEFAULT;
4640     infoPtr->clrBtnShadow = CLR_DEFAULT;
4641     infoPtr->szPadding.cx = 7;
4642     infoPtr->szPadding.cy = 6;
4643     TOOLBAR_NotifyFormat(infoPtr, (WPARAM)hwnd, (LPARAM)NF_REQUERY);
4644
4645     SystemParametersInfoA (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
4646     infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectA (&logFont);
4647
4648     if (dwStyle & TBSTYLE_TOOLTIPS) {
4649         /* Create tooltip control */
4650         infoPtr->hwndToolTip =
4651             CreateWindowExA (0, TOOLTIPS_CLASSA, NULL, 0,
4652                                CW_USEDEFAULT, CW_USEDEFAULT,
4653                                CW_USEDEFAULT, CW_USEDEFAULT,
4654                                hwnd, 0, 0, 0);
4655
4656         /* Send NM_TOOLTIPSCREATED notification */
4657         if (infoPtr->hwndToolTip) {
4658             NMTOOLTIPSCREATED nmttc;
4659
4660             nmttc.hwndToolTips = infoPtr->hwndToolTip;
4661
4662             TOOLBAR_SendNotify ((NMHDR *) &nmttc, infoPtr,
4663                             NM_TOOLTIPSCREATED);
4664         }
4665     }
4666
4667     TOOLBAR_CheckStyle (hwnd, dwStyle);
4668
4669     TOOLBAR_CalcToolbar(hwnd);
4670
4671     return 0;
4672 }
4673
4674
4675 static LRESULT
4676 TOOLBAR_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
4677 {
4678     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4679
4680     /* delete tooltip control */
4681     if (infoPtr->hwndToolTip)
4682         DestroyWindow (infoPtr->hwndToolTip);
4683
4684     /* delete button data */
4685     if (infoPtr->buttons)
4686         COMCTL32_Free (infoPtr->buttons);
4687
4688     /* delete strings */
4689     if (infoPtr->strings) {
4690         INT i;
4691         for (i = 0; i < infoPtr->nNumStrings; i++)
4692             if (infoPtr->strings[i])
4693                 COMCTL32_Free (infoPtr->strings[i]);
4694
4695         COMCTL32_Free (infoPtr->strings);
4696     }
4697
4698     /* destroy internal image list */
4699     if (infoPtr->himlInt)
4700         ImageList_Destroy (infoPtr->himlInt);
4701
4702         TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
4703         TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
4704         TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
4705
4706     /* delete default font */
4707     if (infoPtr->hFont)
4708         DeleteObject (infoPtr->hDefaultFont);
4709
4710     /* free toolbar info data */
4711     COMCTL32_Free (infoPtr);
4712     SetWindowLongA (hwnd, 0, 0);
4713
4714     return 0;
4715 }
4716
4717
4718 static LRESULT
4719 TOOLBAR_EraseBackground (HWND hwnd, WPARAM wParam, LPARAM lParam)
4720 {
4721     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4722     DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
4723     NMTBCUSTOMDRAW tbcd;
4724     INT ret = FALSE;
4725     DWORD ntfret;
4726
4727     if (dwStyle & TBSTYLE_CUSTOMERASE) {
4728         ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
4729         tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
4730         tbcd.nmcd.hdc = (HDC)wParam;
4731         ntfret = TOOLBAR_SendNotify ((NMHDR *)&tbcd, infoPtr, NM_CUSTOMDRAW);
4732         infoPtr->dwBaseCustDraw = ntfret & 0xffff;
4733
4734         /* FIXME: in general the return flags *can* be or'ed together */
4735         switch (infoPtr->dwBaseCustDraw)
4736             {
4737             case CDRF_DODEFAULT:
4738                 break;
4739             case CDRF_SKIPDEFAULT:
4740                 return TRUE;
4741             default:
4742                 FIXME("[%p] response %ld not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
4743                       hwnd, ntfret);
4744             }
4745     }
4746
4747     /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
4748      * to my parent for processing.
4749      */
4750     if (infoPtr->bTransparent) {
4751         POINT pt, ptorig;
4752         HDC hdc = (HDC)wParam;
4753         HWND parent;
4754
4755         pt.x = 0;
4756         pt.y = 0;
4757         parent = GetParent(hwnd);
4758         MapWindowPoints(hwnd, parent, &pt, 1);
4759         OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
4760         ret = SendMessageA (parent, WM_ERASEBKGND, wParam, lParam);
4761         SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
4762     }
4763     if (!ret)
4764         ret = DefWindowProcA (hwnd, WM_ERASEBKGND, wParam, lParam);
4765
4766     if ((dwStyle & TBSTYLE_CUSTOMERASE) &&
4767         (infoPtr->dwBaseCustDraw & CDRF_NOTIFYPOSTERASE)) {
4768         ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
4769         tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
4770         tbcd.nmcd.hdc = (HDC)wParam;
4771         ntfret = TOOLBAR_SendNotify ((NMHDR *)&tbcd, infoPtr, NM_CUSTOMDRAW);
4772         infoPtr->dwBaseCustDraw = ntfret & 0xffff;
4773         switch (infoPtr->dwBaseCustDraw)
4774             {
4775             case CDRF_DODEFAULT:
4776                 break;
4777             case CDRF_SKIPDEFAULT:
4778                 return TRUE;
4779             default:
4780                 FIXME("[%p] response %ld not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
4781                       hwnd, ntfret);
4782             }
4783     }
4784     return ret;
4785 }
4786
4787
4788 static LRESULT
4789 TOOLBAR_GetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
4790 {
4791     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4792
4793     return (LRESULT)infoPtr->hFont;
4794 }
4795
4796
4797 static LRESULT
4798 TOOLBAR_LButtonDblClk (HWND hwnd, WPARAM wParam, LPARAM lParam)
4799 {
4800     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4801     TBUTTON_INFO *btnPtr;
4802     POINT pt;
4803     INT   nHit;
4804
4805     pt.x = (INT)LOWORD(lParam);
4806     pt.y = (INT)HIWORD(lParam);
4807     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
4808
4809     if (nHit >= 0) {
4810         btnPtr = &infoPtr->buttons[nHit];
4811         if (!(btnPtr->fsState & TBSTATE_ENABLED))
4812             return 0;
4813         SetCapture (hwnd);
4814         infoPtr->bCaptured = TRUE;
4815         infoPtr->nButtonDown = nHit;
4816
4817         btnPtr->fsState |= TBSTATE_PRESSED;
4818
4819         InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr,
4820             btnPtr));
4821     }
4822     else if (GetWindowLongA (hwnd, GWL_STYLE) & CCS_ADJUSTABLE)
4823         TOOLBAR_Customize (hwnd);
4824
4825     return 0;
4826 }
4827
4828
4829 static LRESULT
4830 TOOLBAR_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
4831 {
4832     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4833     TBUTTON_INFO *btnPtr;
4834     POINT pt;
4835     INT   nHit;
4836     NMTOOLBARA nmtb;
4837
4838     if (infoPtr->hwndToolTip)
4839         TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
4840                             WM_LBUTTONDOWN, wParam, lParam);
4841
4842     pt.x = (INT)LOWORD(lParam);
4843     pt.y = (INT)HIWORD(lParam);
4844     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
4845
4846     if (nHit >= 0) {
4847         RECT arrowRect;
4848         btnPtr = &infoPtr->buttons[nHit];
4849         infoPtr->nOldHit = nHit;
4850
4851         CopyRect(&arrowRect, &btnPtr->rect);
4852         arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
4853
4854         /* for EX_DRAWDDARROWS style,  click must be in the drop-down arrow rect */
4855         if ((btnPtr->fsState & TBSTATE_ENABLED) && (btnPtr->fsStyle & TBSTYLE_DROPDOWN) &&
4856              ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
4857               (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))
4858         {
4859             LRESULT res;
4860             /*
4861              * this time we must force a Redraw, so the btn is
4862              * painted down before CaptureChanged repaints it up
4863              */
4864             RedrawWindow(hwnd,&btnPtr->rect,0,
4865                         RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
4866
4867             nmtb.iItem = btnPtr->idCommand;
4868             memset(&nmtb.tbButton, 0, sizeof(TBBUTTON));
4869             nmtb.cchText = 0;
4870             nmtb.pszText = 0;
4871             memset(&nmtb.rcButton, 0, sizeof(RECT));
4872             res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
4873                                   TBN_DROPDOWN);
4874             if (res != TBDDRET_TREATPRESSED)
4875                 /* ??? guess  (GA)  */
4876                 return 0;
4877             /* otherwise drop through and process as pushed */
4878         }
4879         /* SetCapture (hwnd); */
4880         infoPtr->bCaptured = TRUE;
4881         infoPtr->nButtonDown = nHit;
4882
4883         btnPtr->fsState |= TBSTATE_PRESSED;
4884         btnPtr->bHot = FALSE;
4885
4886         if (btnPtr->fsState & TBSTATE_ENABLED)
4887             InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr, btnPtr));
4888         UpdateWindow(hwnd);
4889         SetCapture (hwnd);
4890
4891         /* native issues the TBN_BEGINDRAG here */
4892         nmtb.iItem = btnPtr->idCommand;
4893         nmtb.tbButton.iBitmap = btnPtr->iBitmap;
4894         nmtb.tbButton.idCommand = btnPtr->idCommand;
4895         nmtb.tbButton.fsState = btnPtr->fsState;
4896         nmtb.tbButton.fsStyle = btnPtr->fsStyle;
4897         nmtb.tbButton.dwData = btnPtr->dwData;
4898         nmtb.tbButton.iString = btnPtr->iString;
4899         nmtb.cchText = 0;  /* !!! not correct */
4900         nmtb.pszText = 0;  /* !!! not correct */
4901         TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
4902                         TBN_BEGINDRAG);
4903     }
4904
4905     return 0;
4906 }
4907
4908 static LRESULT
4909 TOOLBAR_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
4910 {
4911     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4912     TBUTTON_INFO *btnPtr;
4913     POINT pt;
4914     INT   nHit;
4915     INT   nOldIndex = -1;
4916     BOOL  bSendMessage = TRUE;
4917     NMHDR hdr;
4918     NMMOUSE nmmouse;
4919     NMTOOLBARA nmtb;
4920
4921     if (infoPtr->hwndToolTip)
4922         TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
4923                             WM_LBUTTONUP, wParam, lParam);
4924
4925     pt.x = (INT)LOWORD(lParam);
4926     pt.y = (INT)HIWORD(lParam);
4927     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
4928
4929     /* restore hot effect to hot button disabled by TOOLBAR_LButtonDown() */
4930     /* if the cursor is still inside of the toolbar */
4931     if((infoPtr->nHotItem >= 0) && (nHit != -1))
4932         infoPtr->buttons[infoPtr->nHotItem].bHot = TRUE;
4933
4934         btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
4935         btnPtr->fsState &= ~TBSTATE_PRESSED;
4936
4937         if (btnPtr->fsStyle & TBSTYLE_CHECK) {
4938                 if (btnPtr->fsStyle & TBSTYLE_GROUP) {
4939                     nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
4940                         nHit);
4941                     if (nOldIndex == nHit)
4942                         bSendMessage = FALSE;
4943                     if ((nOldIndex != nHit) &&
4944                         (nOldIndex != -1))
4945                         infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
4946                     btnPtr->fsState |= TBSTATE_CHECKED;
4947                 }
4948                 else {
4949                     if (btnPtr->fsState & TBSTATE_CHECKED)
4950                         btnPtr->fsState &= ~TBSTATE_CHECKED;
4951                     else
4952                         btnPtr->fsState |= TBSTATE_CHECKED;
4953                 }
4954         }
4955
4956         if (nOldIndex != -1)
4957         {
4958             InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect,
4959                 TOOLBAR_HasText(infoPtr, &infoPtr->buttons[nOldIndex]));
4960         }
4961
4962         /*
4963          * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
4964          * that resets bCaptured and btn TBSTATE_PRESSED flags,
4965          * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
4966          */
4967         if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
4968             ReleaseCapture ();
4969         infoPtr->nButtonDown = -1;
4970
4971         /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
4972         TOOLBAR_SendNotify ((NMHDR *) &hdr, infoPtr,
4973                         NM_RELEASEDCAPTURE);
4974
4975         /* native issues TBN_ENDDRAG here, if _LBUTTONDOWN issued the
4976          * TBN_BEGINDRAG
4977          */
4978         nmtb.iItem = btnPtr->idCommand;
4979         nmtb.tbButton.iBitmap = btnPtr->iBitmap;
4980         nmtb.tbButton.idCommand = btnPtr->idCommand;
4981         nmtb.tbButton.fsState = btnPtr->fsState;
4982         nmtb.tbButton.fsStyle = btnPtr->fsStyle;
4983         nmtb.tbButton.dwData = btnPtr->dwData;
4984         nmtb.tbButton.iString = btnPtr->iString;
4985         nmtb.cchText = 0;  /* !!! not correct */
4986         nmtb.pszText = 0;  /* !!! not correct */
4987         TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
4988                         TBN_ENDDRAG);
4989
4990         if (btnPtr->fsState & TBSTATE_ENABLED)
4991         {
4992             SendMessageA (infoPtr->hwndNotify, WM_COMMAND,
4993               MAKEWPARAM(infoPtr->buttons[nHit].idCommand, 0), (LPARAM)hwnd);
4994
4995             /* !!! Undocumented - toolbar at 4.71 level and above sends
4996             * either NMRCLICK or NM_CLICK with the NMMOUSE structure.
4997             * Only NM_RCLICK is documented.
4998             */
4999             nmmouse.dwItemSpec = btnPtr->idCommand;
5000             nmmouse.dwItemData = btnPtr->dwData;
5001             TOOLBAR_SendNotify ((NMHDR *) &nmmouse, infoPtr, NM_CLICK);
5002         }
5003     return 0;
5004 }
5005
5006 static LRESULT
5007 TOOLBAR_CaptureChanged(HWND hwnd)
5008 {
5009     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5010     TBUTTON_INFO *btnPtr;
5011
5012     infoPtr->bCaptured = FALSE;
5013
5014     if (infoPtr->nButtonDown >= 0)
5015     {
5016         btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5017         btnPtr->fsState &= ~TBSTATE_PRESSED;
5018
5019         infoPtr->nOldHit = -1;
5020
5021         if (btnPtr->fsState & TBSTATE_ENABLED)
5022             InvalidateRect(hwnd, &btnPtr->rect, TOOLBAR_HasText(infoPtr,
5023               btnPtr));
5024     }
5025     return 0;
5026 }
5027
5028 static LRESULT
5029 TOOLBAR_MouseLeave (HWND hwnd, WPARAM wParam, LPARAM lParam)
5030 {
5031     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5032     TBUTTON_INFO *hotBtnPtr, *btnPtr;
5033     RECT rc1;
5034
5035     if (infoPtr->nOldHit < 0)
5036       return TRUE;
5037
5038     hotBtnPtr = &infoPtr->buttons[infoPtr->nOldHit];
5039
5040     /* Redraw the button if the last button we were over is the hot button and it
5041        is enabled */
5042     if((infoPtr->nOldHit == infoPtr->nHotItem) && (hotBtnPtr->fsState & TBSTATE_ENABLED))
5043     {
5044         hotBtnPtr->bHot = FALSE;
5045         rc1 = hotBtnPtr->rect;
5046         InflateRect (&rc1, 1, 1);
5047         InvalidateRect (hwnd, &rc1, TOOLBAR_HasText(infoPtr,
5048             hotBtnPtr));
5049     }
5050
5051     /* If the last button we were over is depressed then make it not */
5052     /* depressed and redraw it */
5053     if(infoPtr->nOldHit == infoPtr->nButtonDown)
5054     {
5055       btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5056
5057       btnPtr->fsState &= ~TBSTATE_PRESSED;
5058
5059       rc1 = hotBtnPtr->rect;
5060       InflateRect (&rc1, 1, 1);
5061       InvalidateRect (hwnd, &rc1, TRUE);
5062     }
5063
5064     infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5065     infoPtr->nHotItem = -2; /* It has to be initially different from nOldHit */
5066
5067     return TRUE;
5068 }
5069
5070 static LRESULT
5071 TOOLBAR_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
5072 {
5073     TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
5074     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5075     POINT pt;
5076     INT   nHit;
5077     TRACKMOUSEEVENT trackinfo;
5078     NMTBHOTITEM nmhotitem;
5079
5080     /* fill in the TRACKMOUSEEVENT struct */
5081     trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5082     trackinfo.dwFlags = TME_QUERY;
5083     trackinfo.hwndTrack = hwnd;
5084     trackinfo.dwHoverTime = HOVER_DEFAULT;
5085
5086     /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5087     _TrackMouseEvent(&trackinfo);
5088
5089     /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5090     if(!(trackinfo.dwFlags & TME_LEAVE)) {
5091         trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5092
5093         /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5094         /* and can properly deactivate the hot toolbar button */
5095         _TrackMouseEvent(&trackinfo);
5096    }
5097
5098     if (infoPtr->hwndToolTip)
5099         TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
5100                             WM_MOUSEMOVE, wParam, lParam);
5101
5102     pt.x = (INT)LOWORD(lParam);
5103     pt.y = (INT)HIWORD(lParam);
5104
5105     nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5106
5107     if (infoPtr->nOldHit != nHit)
5108     {
5109         /* Remove the effect of an old hot button if the button was enabled and was
5110            drawn with the hot button effect */
5111         if(infoPtr->nOldHit >= 0 && infoPtr->nOldHit == infoPtr->nHotItem &&
5112                 (infoPtr->buttons[infoPtr->nOldHit].fsState & TBSTATE_ENABLED))
5113         {
5114             oldBtnPtr = &infoPtr->buttons[infoPtr->nOldHit];
5115             oldBtnPtr->bHot = FALSE;
5116         }
5117
5118         /* It's not a separator or in nowhere. It's a hot button. */
5119         if (nHit >= 0)
5120         {
5121             btnPtr = &infoPtr->buttons[nHit];
5122
5123             infoPtr->nHotItem = nHit;
5124
5125             /* only enabled buttons show hot effect */
5126             if(infoPtr->buttons[nHit].fsState & TBSTATE_ENABLED)
5127             {
5128                 btnPtr->bHot = TRUE;
5129             }
5130         }
5131
5132         nmhotitem.dwFlags = HICF_MOUSE;
5133         if (oldBtnPtr)
5134             nmhotitem.idOld = oldBtnPtr->idCommand;
5135         else
5136             nmhotitem.dwFlags |= HICF_ENTERING;
5137         if (btnPtr)
5138             nmhotitem.idNew = btnPtr->idCommand;
5139         else
5140             nmhotitem.dwFlags |= HICF_LEAVING;
5141         TOOLBAR_SendNotify((NMHDR*)&nmhotitem, infoPtr, TBN_HOTITEMCHANGE);
5142
5143         /* now invalidate the old and new buttons so they will be painted */
5144         if (oldBtnPtr)
5145             InvalidateRect (hwnd, &oldBtnPtr->rect,
5146                             TOOLBAR_HasText(infoPtr, oldBtnPtr));
5147         if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED))
5148             InvalidateRect(hwnd, &btnPtr->rect,
5149                            TOOLBAR_HasText(infoPtr, btnPtr));
5150
5151         if (infoPtr->bCaptured) {
5152             btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5153             if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5154                 btnPtr->fsState &= ~TBSTATE_PRESSED;
5155                 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5156             }
5157             else if (nHit == infoPtr->nButtonDown) {
5158                 btnPtr->fsState |= TBSTATE_PRESSED;
5159                 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5160             }
5161         }
5162         infoPtr->nOldHit = nHit;
5163     }
5164     return 0;
5165 }
5166
5167
5168 inline static LRESULT
5169 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5170 {
5171 /*    if (wndPtr->dwStyle & CCS_NODIVIDER) */
5172         return DefWindowProcA (hwnd, WM_NCACTIVATE, wParam, lParam);
5173 /*    else */
5174 /*      return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5175 }
5176
5177
5178 inline static LRESULT
5179 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
5180 {
5181     if (!(GetWindowLongA (hwnd, GWL_STYLE) & CCS_NODIVIDER))
5182         ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
5183
5184     return DefWindowProcA (hwnd, WM_NCCALCSIZE, wParam, lParam);
5185 }
5186
5187
5188 static LRESULT
5189 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5190 {
5191     TOOLBAR_INFO *infoPtr;
5192     LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
5193     DWORD styleadd = 0;
5194
5195     /* allocate memory for info structure */
5196     infoPtr = (TOOLBAR_INFO *)COMCTL32_Alloc (sizeof(TOOLBAR_INFO));
5197     SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
5198
5199     /* paranoid!! */
5200     infoPtr->dwStructSize = sizeof(TBBUTTON);
5201
5202     /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
5203     if (!GetWindowLongA (hwnd, GWL_HINSTANCE)) {
5204         HINSTANCE hInst = (HINSTANCE)GetWindowLongA (GetParent (hwnd), GWL_HINSTANCE);
5205         SetWindowLongA (hwnd, GWL_HINSTANCE, (DWORD)hInst);
5206     }
5207
5208     /* native control does:
5209      *    Get a lot of colors and brushes
5210      *    WM_NOTIFYFORMAT
5211      *    SystemParametersInfoA(0x1f, 0x3c, adr1, 0)
5212      *    CreateFontIndirectA(adr1)
5213      *    CreateBitmap(0x27, 0x24, 1, 1, 0)
5214      *    hdc = GetDC(toolbar)
5215      *    GetSystemMetrics(0x48)
5216      *    fnt2=CreateFontA(0xe, 0, 0, 0, 0x190, 0, 0, 0, 0, 2,
5217      *                     0, 0, 0, 0, "MARLETT")
5218      *    oldfnt = SelectObject(hdc, fnt2)
5219      *    GetCharWidthA(hdc, 0x36, 0x36, adr2)
5220      *    GetTextMetricsA(hdc, adr3)
5221      *    SelectObject(hdc, oldfnt)
5222      *    DeleteObject(fnt2)
5223      *    ReleaseDC(hdc)
5224      *    InvalidateRect(toolbar, 0, 1)
5225      *    SetWindowLongA(toolbar, 0, addr)
5226      *    SetWindowLongA(toolbar, -16, xxx)  **sometimes**
5227      *                                          WM_STYLECHANGING
5228      *                             CallWinEx   old         new
5229      *                       ie 1  0x56000a4c  0x46000a4c  0x56008a4d
5230      *                       ie 2  0x4600094c  0x4600094c  0x4600894d
5231      *                       ie 3  0x56000b4c  0x46000b4c  0x56008b4d
5232      *                      rebar  0x50008844  0x40008844  0x50008845
5233      *                      pager  0x50000844  0x40000844  0x50008845
5234      *                    IC35mgr  0x5400084e  **nochange**
5235      *           on entry to _NCCREATE         0x5400084e
5236      *                    rowlist  0x5400004e  **nochange**
5237      *           on entry to _NCCREATE         0x5400004e
5238      *
5239      */
5240
5241     /* I think the code below is a bug, but it is the way that the native
5242      * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
5243      * forgets to specify TBSTYLE_TRANSPARENT but does specify either
5244      * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
5245      * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
5246      * Some how, the only cases of this seem to be MFC programs.
5247      *
5248      * Note also that the addition of _TRANSPARENT occurs *only* here. It
5249      * does not occur in the WM_STYLECHANGING routine.
5250      *    (Guy Albertelli   9/2001)
5251      *
5252      */
5253     if ((cs->style & TBSTYLE_FLAT) && !(cs->style & TBSTYLE_TRANSPARENT))
5254         styleadd |= TBSTYLE_TRANSPARENT;
5255     if (!(cs->style & (CCS_TOP | CCS_NOMOVEY))) {
5256         styleadd |= CCS_TOP;   /* default to top */
5257         SetWindowLongA (hwnd, GWL_STYLE, cs->style | styleadd);
5258     }
5259
5260     return DefWindowProcA (hwnd, WM_NCCREATE, wParam, lParam);
5261 }
5262
5263
5264 static LRESULT
5265 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
5266 {
5267     DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
5268     RECT rcWindow;
5269     HDC hdc;
5270
5271     if (dwStyle & WS_MINIMIZE)
5272         return 0; /* Nothing to do */
5273
5274     DefWindowProcA (hwnd, WM_NCPAINT, wParam, lParam);
5275
5276     if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
5277         return 0;
5278
5279     if (!(dwStyle & CCS_NODIVIDER))
5280     {
5281         GetWindowRect (hwnd, &rcWindow);
5282         OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
5283         if( dwStyle & WS_BORDER )
5284             OffsetRect (&rcWindow, 1, 1);
5285         DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
5286     }
5287
5288     ReleaseDC( hwnd, hdc );
5289
5290     return 0;
5291 }
5292
5293
5294 inline static LRESULT
5295 TOOLBAR_Notify (HWND hwnd, WPARAM wParam, LPARAM lParam)
5296 {
5297     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5298     LPNMHDR lpnmh = (LPNMHDR)lParam;
5299
5300     if (lpnmh->code == PGN_CALCSIZE) {
5301         LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5302
5303         if (lppgc->dwFlag == PGF_CALCWIDTH) {
5304             lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
5305             TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
5306                   lppgc->iWidth);
5307         }
5308         else {
5309             lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5310             TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
5311                   lppgc->iHeight);
5312         }
5313         return 0;
5314     }
5315
5316     if (lpnmh->code == PGN_SCROLL) {
5317         LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lParam;
5318
5319         lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
5320                           infoPtr->nButtonWidth : infoPtr->nButtonHeight;
5321         TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
5322               lppgs->iScroll, lppgs->iDir);
5323         return 0;
5324     }
5325
5326
5327     TRACE("passing WM_NOTIFY!\n");
5328
5329     if ((infoPtr->hwndToolTip) && (lpnmh->hwndFrom == infoPtr->hwndToolTip)) {
5330         if (infoPtr->bNtfUnicode)
5331             return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
5332                                  wParam, lParam);
5333         else
5334             return SendMessageA (infoPtr->hwndNotify, WM_NOTIFY,
5335                                  wParam, lParam);
5336
5337 #if 0
5338         if (lpnmh->code == TTN_GETDISPINFOA) {
5339             LPNMTTDISPINFOA lpdi = (LPNMTTDISPINFOA)lParam;
5340
5341             FIXME("retrieving ASCII string\n");
5342
5343         }
5344         else if (lpnmh->code == TTN_GETDISPINFOW) {
5345             LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5346
5347             FIXME("retrieving UNICODE string\n");
5348
5349         }
5350 #endif
5351     }
5352
5353     return 0;
5354 }
5355
5356
5357 static LRESULT
5358 TOOLBAR_NotifyFormatFake(HWND hwnd, WPARAM wParam, LPARAM lParam)
5359 {
5360     /* remove this routine when Toolbar is improved to pass infoPtr
5361      * around instead of hwnd.
5362      */
5363     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5364     return TOOLBAR_NotifyFormat(infoPtr, wParam, lParam);
5365 }
5366
5367
5368 static LRESULT
5369 TOOLBAR_NotifyFormat(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5370 {
5371     INT i;
5372
5373     if (lParam == NF_REQUERY) {
5374         i = SendMessageA(GetParent(infoPtr->hwndSelf),
5375                          WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
5376         if ((i < NFR_ANSI) || (i > NFR_UNICODE)) {
5377             ERR("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n",
5378                 i);
5379             i = NFR_ANSI;
5380         }
5381         infoPtr->bNtfUnicode = (i == NFR_UNICODE) ? 1 : 0;
5382         return (LRESULT)i;
5383     }
5384     return (LRESULT)((infoPtr->bUnicode) ? NFR_UNICODE : NFR_ANSI);
5385 }
5386
5387
5388 static LRESULT
5389 TOOLBAR_Paint (HWND hwnd, WPARAM wParam)
5390 {
5391     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5392     HDC hdc;
5393     PAINTSTRUCT ps;
5394
5395     /* fill ps.rcPaint with a default rect */
5396     memcpy(&(ps.rcPaint), &(infoPtr->rcBound), sizeof(infoPtr->rcBound));
5397
5398     hdc = wParam==0 ? BeginPaint(hwnd, &ps) : (HDC)wParam;
5399
5400     TRACE("psrect=(%d,%d)-(%d,%d)\n",
5401           ps.rcPaint.left, ps.rcPaint.top,
5402           ps.rcPaint.right, ps.rcPaint.bottom);
5403
5404     TOOLBAR_Refresh (hwnd, hdc, &ps);
5405     if (!wParam) EndPaint (hwnd, &ps);
5406
5407     return 0;
5408 }
5409
5410
5411 static LRESULT
5412 TOOLBAR_SetRedraw (HWND hwnd, WPARAM wParam, LPARAM lParam)
5413      /*****************************************************
5414       *
5415       * Function;
5416       *  Handles the WM_SETREDRAW message.
5417       *
5418       * Documentation:
5419       *  According to testing V4.71 of COMCTL32 returns the
5420       *  *previous* status of the redraw flag (either 0 or 1)
5421       *  instead of the MSDN documented value of 0 if handled.
5422       *  (For laughs see the "consistancy" with same function
5423       *   in rebar.)
5424       *
5425       *****************************************************/
5426 {
5427     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5428     BOOL oldredraw = infoPtr->bDoRedraw;
5429
5430     TRACE("set to %s\n",
5431           (wParam) ? "TRUE" : "FALSE");
5432     infoPtr->bDoRedraw = (BOOL) wParam;
5433     if (wParam) {
5434         InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
5435     }
5436     return (oldredraw) ? 1 : 0;
5437 }
5438
5439
5440 static LRESULT
5441 TOOLBAR_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
5442 {
5443     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5444     DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
5445     RECT parent_rect;
5446     RECT window_rect;
5447     HWND parent;
5448     INT  x, y;
5449     INT  cx, cy;
5450     INT  flags;
5451     UINT uPosFlags = 0;
5452
5453     /* Resize deadlock check */
5454     if (infoPtr->bAutoSize) {
5455         infoPtr->bAutoSize = FALSE;
5456         return 0;
5457     }
5458
5459     /* FIXME: optimize to only update size if the new size doesn't */
5460     /* match the current size */
5461
5462     flags = (INT) wParam;
5463
5464     /* FIXME for flags =
5465      * SIZE_MAXIMIZED, SIZE_MAXSHOW, SIZE_MINIMIZED
5466      */
5467
5468     TRACE("sizing toolbar!\n");
5469
5470     if (flags == SIZE_RESTORED) {
5471         /* width and height don't apply */
5472         parent = GetParent (hwnd);
5473         GetClientRect(parent, &parent_rect);
5474         x = parent_rect.left;
5475         y = parent_rect.top;
5476
5477         if (dwStyle & CCS_NORESIZE) {
5478             uPosFlags |= (SWP_NOSIZE | SWP_NOMOVE);
5479
5480             /*
5481              * this sets the working width of the toolbar, and
5482              * Calc Toolbar will not adjust it, only the height
5483              */
5484             infoPtr->nWidth = parent_rect.right - parent_rect.left;
5485             cy = infoPtr->nHeight;
5486             cx = infoPtr->nWidth;
5487             TOOLBAR_CalcToolbar (hwnd);
5488             infoPtr->nWidth = cx;
5489             infoPtr->nHeight = cy;
5490         }
5491         else {
5492             infoPtr->nWidth = parent_rect.right - parent_rect.left;
5493             TOOLBAR_CalcToolbar (hwnd);
5494             cy = infoPtr->nHeight;
5495             cx = infoPtr->nWidth;
5496
5497             if (dwStyle & CCS_NOMOVEY) {
5498                 GetWindowRect(hwnd, &window_rect);
5499                 ScreenToClient(parent, (LPPOINT)&window_rect.left);
5500                 y = window_rect.top;
5501             }
5502         }
5503
5504         if (dwStyle & CCS_NOPARENTALIGN) {
5505             uPosFlags |= SWP_NOMOVE;
5506             cy = infoPtr->nHeight;
5507             cx = infoPtr->nWidth;
5508         }
5509
5510         if (!(dwStyle & CCS_NODIVIDER))
5511             cy += GetSystemMetrics(SM_CYEDGE);
5512
5513         if (dwStyle & WS_BORDER)
5514         {
5515             x = y = 1;
5516             cy += GetSystemMetrics(SM_CYEDGE);
5517             cx += GetSystemMetrics(SM_CYEDGE);
5518         }
5519
5520         SetWindowPos (hwnd, 0, parent_rect.left - x, parent_rect.top - y,
5521                         cx, cy, uPosFlags | SWP_NOZORDER);
5522     }
5523     return 0;
5524 }
5525
5526
5527 static LRESULT
5528 TOOLBAR_StyleChanged (HWND hwnd, INT nType, LPSTYLESTRUCT lpStyle)
5529 {
5530     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5531
5532     if (nType == GWL_STYLE) {
5533         if (lpStyle->styleNew & TBSTYLE_LIST) {
5534             infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
5535         }
5536         else {
5537             infoPtr->dwDTFlags = DT_CENTER;
5538         }
5539         infoPtr->bTransparent = (lpStyle->styleNew & TBSTYLE_TRANSPARENT);
5540         infoPtr->bBtnTranspnt = (lpStyle->styleNew &
5541                                  (TBSTYLE_FLAT | TBSTYLE_LIST));
5542         TOOLBAR_CheckStyle (hwnd, lpStyle->styleNew);
5543     }
5544
5545     TOOLBAR_AutoSize (hwnd);
5546
5547     InvalidateRect(hwnd, NULL, FALSE);
5548
5549     return 0;
5550 }
5551
5552
5553 static LRESULT
5554 TOOLBAR_SysColorChange (HWND hwnd)
5555 {
5556     COMCTL32_RefreshSysColors();
5557
5558     return 0;
5559 }
5560
5561
5562
5563 static LRESULT WINAPI
5564 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5565 {
5566     TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n",
5567           hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
5568
5569     if (!TOOLBAR_GetInfoPtr(hwnd) && (uMsg != WM_NCCREATE))
5570         return DefWindowProcA( hwnd, uMsg, wParam, lParam );
5571
5572     switch (uMsg)
5573     {
5574         case TB_ADDBITMAP:
5575             return TOOLBAR_AddBitmap (hwnd, wParam, lParam);
5576
5577         case TB_ADDBUTTONSA:
5578             return TOOLBAR_AddButtonsA (hwnd, wParam, lParam);
5579
5580         case TB_ADDBUTTONSW:
5581             return TOOLBAR_AddButtonsW (hwnd, wParam, lParam);
5582
5583         case TB_ADDSTRINGA:
5584             return TOOLBAR_AddStringA (hwnd, wParam, lParam);
5585
5586         case TB_ADDSTRINGW:
5587             return TOOLBAR_AddStringW (hwnd, wParam, lParam);
5588
5589         case TB_AUTOSIZE:
5590             return TOOLBAR_AutoSize (hwnd);
5591
5592         case TB_BUTTONCOUNT:
5593             return TOOLBAR_ButtonCount (hwnd, wParam, lParam);
5594
5595         case TB_BUTTONSTRUCTSIZE:
5596             return TOOLBAR_ButtonStructSize (hwnd, wParam, lParam);
5597
5598         case TB_CHANGEBITMAP:
5599             return TOOLBAR_ChangeBitmap (hwnd, wParam, lParam);
5600
5601         case TB_CHECKBUTTON:
5602             return TOOLBAR_CheckButton (hwnd, wParam, lParam);
5603
5604         case TB_COMMANDTOINDEX:
5605             return TOOLBAR_CommandToIndex (hwnd, wParam, lParam);
5606
5607         case TB_CUSTOMIZE:
5608             return TOOLBAR_Customize (hwnd);
5609
5610         case TB_DELETEBUTTON:
5611             return TOOLBAR_DeleteButton (hwnd, wParam, lParam);
5612
5613         case TB_ENABLEBUTTON:
5614             return TOOLBAR_EnableButton (hwnd, wParam, lParam);
5615
5616         case TB_GETANCHORHIGHLIGHT:
5617             return TOOLBAR_GetAnchorHighlight (hwnd);
5618
5619         case TB_GETBITMAP:
5620             return TOOLBAR_GetBitmap (hwnd, wParam, lParam);
5621
5622         case TB_GETBITMAPFLAGS:
5623             return TOOLBAR_GetBitmapFlags (hwnd, wParam, lParam);
5624
5625         case TB_GETBUTTON:
5626             return TOOLBAR_GetButton (hwnd, wParam, lParam);
5627
5628         case TB_GETBUTTONINFOA:
5629             return TOOLBAR_GetButtonInfoA (hwnd, wParam, lParam);
5630
5631         case TB_GETBUTTONINFOW:
5632             return TOOLBAR_GetButtonInfoW (hwnd, wParam, lParam);
5633
5634         case TB_GETBUTTONSIZE:
5635             return TOOLBAR_GetButtonSize (hwnd);
5636
5637         case TB_GETBUTTONTEXTA:
5638             return TOOLBAR_GetButtonTextA (hwnd, wParam, lParam);
5639
5640         case TB_GETBUTTONTEXTW:
5641             return TOOLBAR_GetButtonTextW (hwnd, wParam, lParam);
5642
5643         case TB_GETDISABLEDIMAGELIST:
5644             return TOOLBAR_GetDisabledImageList (hwnd, wParam, lParam);
5645
5646         case TB_GETEXTENDEDSTYLE:
5647             return TOOLBAR_GetExtendedStyle (hwnd);
5648
5649         case TB_GETHOTIMAGELIST:
5650             return TOOLBAR_GetHotImageList (hwnd, wParam, lParam);
5651
5652         case TB_GETHOTITEM:
5653             return TOOLBAR_GetHotItem (hwnd);
5654
5655         case TB_GETIMAGELIST:
5656             return TOOLBAR_GetDefImageList (hwnd, wParam, lParam);
5657
5658 /*      case TB_GETINSERTMARK:                  */ /* 4.71 */
5659 /*      case TB_GETINSERTMARKCOLOR:             */ /* 4.71 */
5660
5661         case TB_GETITEMRECT:
5662             return TOOLBAR_GetItemRect (hwnd, wParam, lParam);
5663
5664         case TB_GETMAXSIZE:
5665             return TOOLBAR_GetMaxSize (hwnd, wParam, lParam);
5666
5667 /*      case TB_GETOBJECT:                      */ /* 4.71 */
5668
5669         case TB_GETPADDING:
5670             return TOOLBAR_GetPadding (hwnd);
5671
5672         case TB_GETRECT:
5673             return TOOLBAR_GetRect (hwnd, wParam, lParam);
5674
5675         case TB_GETROWS:
5676             return TOOLBAR_GetRows (hwnd, wParam, lParam);
5677
5678         case TB_GETSTATE:
5679             return TOOLBAR_GetState (hwnd, wParam, lParam);
5680
5681         case TB_GETSTYLE:
5682             return TOOLBAR_GetStyle (hwnd, wParam, lParam);
5683
5684         case TB_GETTEXTROWS:
5685             return TOOLBAR_GetTextRows (hwnd, wParam, lParam);
5686
5687         case TB_GETTOOLTIPS:
5688             return TOOLBAR_GetToolTips (hwnd, wParam, lParam);
5689
5690         case TB_GETUNICODEFORMAT:
5691             return TOOLBAR_GetUnicodeFormat (hwnd, wParam, lParam);
5692
5693         case TB_HIDEBUTTON:
5694             return TOOLBAR_HideButton (hwnd, wParam, lParam);
5695
5696         case TB_HITTEST:
5697             return TOOLBAR_HitTest (hwnd, wParam, lParam);
5698
5699         case TB_INDETERMINATE:
5700             return TOOLBAR_Indeterminate (hwnd, wParam, lParam);
5701
5702         case TB_INSERTBUTTONA:
5703             return TOOLBAR_InsertButtonA (hwnd, wParam, lParam);
5704
5705         case TB_INSERTBUTTONW:
5706             return TOOLBAR_InsertButtonW (hwnd, wParam, lParam);
5707
5708 /*      case TB_INSERTMARKHITTEST:              */ /* 4.71 */
5709
5710         case TB_ISBUTTONCHECKED:
5711             return TOOLBAR_IsButtonChecked (hwnd, wParam, lParam);
5712
5713         case TB_ISBUTTONENABLED:
5714             return TOOLBAR_IsButtonEnabled (hwnd, wParam, lParam);
5715
5716         case TB_ISBUTTONHIDDEN:
5717             return TOOLBAR_IsButtonHidden (hwnd, wParam, lParam);
5718
5719         case TB_ISBUTTONHIGHLIGHTED:
5720             return TOOLBAR_IsButtonHighlighted (hwnd, wParam, lParam);
5721
5722         case TB_ISBUTTONINDETERMINATE:
5723             return TOOLBAR_IsButtonIndeterminate (hwnd, wParam, lParam);
5724
5725         case TB_ISBUTTONPRESSED:
5726             return TOOLBAR_IsButtonPressed (hwnd, wParam, lParam);
5727
5728         case TB_LOADIMAGES:                        /* 4.70 */
5729             FIXME("missing standard imagelists\n");
5730             return 0;
5731
5732 /*      case TB_MAPACCELERATORA:                */ /* 4.71 */
5733 /*      case TB_MAPACCELERATORW:                */ /* 4.71 */
5734 /*      case TB_MARKBUTTON:                     */ /* 4.71 */
5735 /*      case TB_MOVEBUTTON:                     */ /* 4.71 */
5736
5737         case TB_PRESSBUTTON:
5738             return TOOLBAR_PressButton (hwnd, wParam, lParam);
5739
5740         case TB_REPLACEBITMAP:
5741             return TOOLBAR_ReplaceBitmap (hwnd, wParam, lParam);
5742
5743         case TB_SAVERESTOREA:
5744             return TOOLBAR_SaveRestoreA (hwnd, wParam, lParam);
5745
5746         case TB_SAVERESTOREW:
5747             return TOOLBAR_SaveRestoreW (hwnd, wParam, lParam);
5748
5749         case TB_SETANCHORHIGHLIGHT:
5750             return TOOLBAR_SetAnchorHighlight (hwnd, wParam);
5751
5752         case TB_SETBITMAPSIZE:
5753             return TOOLBAR_SetBitmapSize (hwnd, wParam, lParam);
5754
5755         case TB_SETBUTTONINFOA:
5756             return TOOLBAR_SetButtonInfoA (hwnd, wParam, lParam);
5757
5758         case TB_SETBUTTONINFOW:
5759             return TOOLBAR_SetButtonInfoW (hwnd, wParam, lParam);
5760
5761         case TB_SETBUTTONSIZE:
5762             return TOOLBAR_SetButtonSize (hwnd, wParam, lParam);
5763
5764         case TB_SETBUTTONWIDTH:
5765             return TOOLBAR_SetButtonWidth (hwnd, wParam, lParam);
5766
5767         case TB_SETCMDID:
5768             return TOOLBAR_SetCmdId (hwnd, wParam, lParam);
5769
5770         case TB_SETDISABLEDIMAGELIST:
5771             return TOOLBAR_SetDisabledImageList (hwnd, wParam, lParam);
5772
5773         case TB_SETDRAWTEXTFLAGS:
5774             return TOOLBAR_SetDrawTextFlags (hwnd, wParam, lParam);
5775
5776         case TB_SETEXTENDEDSTYLE:
5777             return TOOLBAR_SetExtendedStyle (hwnd, wParam, lParam);
5778
5779         case TB_SETHOTIMAGELIST:
5780             return TOOLBAR_SetHotImageList (hwnd, wParam, lParam);
5781
5782         case TB_SETHOTITEM:
5783             return TOOLBAR_SetHotItem (hwnd, wParam);
5784
5785         case TB_SETIMAGELIST:
5786             return TOOLBAR_SetImageList (hwnd, wParam, lParam);
5787
5788         case TB_SETINDENT:
5789             return TOOLBAR_SetIndent (hwnd, wParam, lParam);
5790
5791 /*      case TB_SETINSERTMARK:                  */ /* 4.71 */
5792
5793         case TB_SETINSERTMARKCOLOR:
5794             return TOOLBAR_SetInsertMarkColor (hwnd, wParam, lParam);
5795
5796         case TB_SETMAXTEXTROWS:
5797             return TOOLBAR_SetMaxTextRows (hwnd, wParam, lParam);
5798
5799         case TB_SETPADDING:
5800             return TOOLBAR_SetPadding (hwnd, wParam, lParam);
5801
5802         case TB_SETPARENT:
5803             return TOOLBAR_SetParent (hwnd, wParam, lParam);
5804
5805         case TB_SETROWS:
5806             return TOOLBAR_SetRows (hwnd, wParam, lParam);
5807
5808         case TB_SETSTATE:
5809             return TOOLBAR_SetState (hwnd, wParam, lParam);
5810
5811         case TB_SETSTYLE:
5812             return TOOLBAR_SetStyle (hwnd, wParam, lParam);
5813
5814         case TB_SETTOOLTIPS:
5815             return TOOLBAR_SetToolTips (hwnd, wParam, lParam);
5816
5817         case TB_SETUNICODEFORMAT:
5818             return TOOLBAR_SetUnicodeFormat (hwnd, wParam, lParam);
5819
5820         case TB_UNKWN45E:
5821             return TOOLBAR_Unkwn45E (hwnd, wParam, lParam);
5822
5823         case TB_UNKWN463:
5824             return TOOLBAR_Unkwn463 (hwnd, wParam, lParam);
5825
5826
5827 /* Common Control Messages */
5828
5829 /*      case TB_GETCOLORSCHEME:                 */ /* identical to CCM_ */
5830         case CCM_GETCOLORSCHEME:
5831             return TOOLBAR_GetColorScheme (hwnd, (LPCOLORSCHEME)lParam);
5832
5833 /*      case TB_SETCOLORSCHEME:                 */ /* identical to CCM_ */
5834         case CCM_SETCOLORSCHEME:
5835             return TOOLBAR_SetColorScheme (hwnd, (LPCOLORSCHEME)lParam);
5836
5837         case CCM_GETVERSION:
5838             return TOOLBAR_GetVersion (hwnd);
5839
5840         case CCM_SETVERSION:
5841             return TOOLBAR_SetVersion (hwnd, (INT)wParam);
5842
5843
5844 /*      case WM_CHAR: */
5845
5846         case WM_CREATE:
5847             return TOOLBAR_Create (hwnd, wParam, lParam);
5848
5849         case WM_DESTROY:
5850           return TOOLBAR_Destroy (hwnd, wParam, lParam);
5851
5852         case WM_ERASEBKGND:
5853             return TOOLBAR_EraseBackground (hwnd, wParam, lParam);
5854
5855         case WM_GETFONT:
5856                 return TOOLBAR_GetFont (hwnd, wParam, lParam);
5857
5858 /*      case WM_KEYDOWN: */
5859 /*      case WM_KILLFOCUS: */
5860
5861         case WM_LBUTTONDBLCLK:
5862             return TOOLBAR_LButtonDblClk (hwnd, wParam, lParam);
5863
5864         case WM_LBUTTONDOWN:
5865             return TOOLBAR_LButtonDown (hwnd, wParam, lParam);
5866
5867         case WM_LBUTTONUP:
5868             return TOOLBAR_LButtonUp (hwnd, wParam, lParam);
5869
5870         case WM_MOUSEMOVE:
5871             return TOOLBAR_MouseMove (hwnd, wParam, lParam);
5872
5873         case WM_MOUSELEAVE:
5874             return TOOLBAR_MouseLeave (hwnd, wParam, lParam);
5875
5876         case WM_CAPTURECHANGED:
5877             return TOOLBAR_CaptureChanged(hwnd);
5878
5879         case WM_NCACTIVATE:
5880             return TOOLBAR_NCActivate (hwnd, wParam, lParam);
5881
5882         case WM_NCCALCSIZE:
5883             return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
5884
5885         case WM_NCCREATE:
5886             return TOOLBAR_NCCreate (hwnd, wParam, lParam);
5887
5888         case WM_NCPAINT:
5889             return TOOLBAR_NCPaint (hwnd, wParam, lParam);
5890
5891         case WM_NOTIFY:
5892             return TOOLBAR_Notify (hwnd, wParam, lParam);
5893
5894         case WM_NOTIFYFORMAT:
5895             TOOLBAR_NotifyFormatFake (hwnd, wParam, lParam);
5896
5897         case WM_PAINT:
5898             return TOOLBAR_Paint (hwnd, wParam);
5899
5900         case WM_SETREDRAW:
5901             return TOOLBAR_SetRedraw (hwnd, wParam, lParam);
5902
5903         case WM_SIZE:
5904             return TOOLBAR_Size (hwnd, wParam, lParam);
5905
5906         case WM_STYLECHANGED:
5907             return TOOLBAR_StyleChanged (hwnd, (INT)wParam, (LPSTYLESTRUCT)lParam);
5908
5909         case WM_SYSCOLORCHANGE:
5910             return TOOLBAR_SysColorChange (hwnd);
5911
5912 /*      case WM_WININICHANGE: */
5913
5914         case WM_CHARTOITEM:
5915         case WM_COMMAND:
5916         case WM_DRAWITEM:
5917         case WM_MEASUREITEM:
5918         case WM_VKEYTOITEM:
5919             {
5920                 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5921                 if(infoPtr != NULL)
5922                     return SendMessageA (infoPtr->hwndNotify, uMsg, wParam, lParam);
5923                 else
5924                     return SendMessageA (GetParent (hwnd), uMsg, wParam, lParam);
5925             }
5926
5927         /* We see this in Outlook Express 5.x and just does DefWindowProc */
5928         case PGM_FORWARDMOUSE:
5929             return DefWindowProcA (hwnd, uMsg, wParam, lParam);
5930
5931         default:
5932             if ((uMsg >= WM_USER) && (uMsg < WM_APP))
5933                 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
5934                      uMsg, wParam, lParam);
5935             return DefWindowProcA (hwnd, uMsg, wParam, lParam);
5936     }
5937     return 0;
5938 }
5939
5940
5941 VOID
5942 TOOLBAR_Register (void)
5943 {
5944     WNDCLASSA wndClass;
5945
5946     ZeroMemory (&wndClass, sizeof(WNDCLASSA));
5947     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
5948     wndClass.lpfnWndProc   = (WNDPROC)ToolbarWindowProc;
5949     wndClass.cbClsExtra    = 0;
5950     wndClass.cbWndExtra    = sizeof(TOOLBAR_INFO *);
5951     wndClass.hCursor       = LoadCursorA (0, IDC_ARROWA);
5952     wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
5953     wndClass.lpszClassName = TOOLBARCLASSNAMEA;
5954
5955     RegisterClassA (&wndClass);
5956 }
5957
5958
5959 VOID
5960 TOOLBAR_Unregister (void)
5961 {
5962     UnregisterClassA (TOOLBARCLASSNAMEA, (HINSTANCE)NULL);
5963 }
5964
5965 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
5966 {
5967     HIMAGELIST himlold;
5968     PIMLENTRY c = NULL;
5969
5970     /* Check if the entry already exists */
5971     c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
5972
5973     /* If this is a new entry we must create it and insert into the array */
5974     if (!c)
5975     {
5976         PIMLENTRY *pnies;
5977
5978         c = (PIMLENTRY) COMCTL32_Alloc(sizeof(IMLENTRY));
5979         c->id = id;
5980
5981         pnies = COMCTL32_Alloc((*cies + 1) * sizeof(PIMLENTRY));
5982         memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
5983         pnies[*cies] = c;
5984         (*cies)++;
5985
5986         COMCTL32_Free(*pies);
5987         *pies = pnies;
5988     }
5989
5990     himlold = c->himl;
5991     c->himl = himl;
5992
5993     return himlold;
5994 }
5995
5996
5997 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
5998 {
5999     int i;
6000
6001     for (i = 0; i < *cies; i++)
6002         COMCTL32_Free((*pies)[i]);
6003
6004     COMCTL32_Free(*pies);
6005
6006     *cies = 0;
6007     *pies = NULL;
6008 }
6009
6010
6011 static PIMLENTRY TOOLBAR_GetImageListEntry(PIMLENTRY *pies, INT cies, INT id)
6012 {
6013     PIMLENTRY c = NULL;
6014
6015     if (pies != NULL)
6016     {
6017         int i;
6018
6019         for (i = 0; i < cies; i++)
6020         {
6021             if (pies[i]->id == id)
6022             {
6023                 c = pies[i];
6024                 break;
6025             }
6026         }
6027     }
6028
6029     return c;
6030 }
6031
6032
6033 static HIMAGELIST TOOLBAR_GetImageList(PIMLENTRY *pies, INT cies, INT id)
6034 {
6035     HIMAGELIST himlDef = 0;
6036     PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6037
6038     if (pie)
6039         himlDef = pie->himl;
6040
6041     return himlDef;
6042 }
6043
6044
6045 static BOOL TOOLBAR_GetButtonInfo(TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6046 {
6047     if (infoPtr->bUnicode)
6048         return TOOLBAR_SendNotify ((NMHDR *) nmtb, infoPtr, TBN_GETBUTTONINFOW);
6049     else
6050     {
6051         CHAR Buffer[256];
6052         NMTOOLBARA nmtba;
6053         BOOL bRet = FALSE;
6054
6055         nmtba.iItem = nmtb->iItem;
6056         nmtba.pszText = Buffer;
6057         nmtba.cchText = 256;
6058         ZeroMemory(nmtba.pszText, nmtba.cchText);
6059
6060         if (TOOLBAR_SendNotify ((NMHDR *) &nmtba, infoPtr, TBN_GETBUTTONINFOA))
6061         {
6062             int ccht = strlen(nmtba.pszText);
6063             if (ccht)
6064                MultiByteToWideChar(CP_ACP, 0, (LPCSTR)nmtba.pszText, -1, 
6065                   nmtb->pszText, nmtb->cchText);
6066
6067             memcpy(&nmtb->tbButton, &nmtba.tbButton, sizeof(TBBUTTON));
6068             bRet = TRUE;
6069         }
6070
6071         return bRet;
6072     }
6073 }
6074
6075
6076 static BOOL TOOLBAR_IsButtonRemovable(TOOLBAR_INFO *infoPtr,
6077         int iItem, PCUSTOMBUTTON btnInfo)
6078 {
6079     NMTOOLBARA nmtb;
6080
6081     nmtb.iItem = iItem;
6082     memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
6083
6084     return TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_QUERYDELETE);
6085 }