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