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