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