~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/comctl32/toolbar.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 21  *
 22  * NOTES
 23  *
 24  * This code was audited for completeness against the documented features
 25  * of Comctl32.dll version 6.0 on Mar. 14, 2004, by Robert Shearman.
 26  * 
 27  * Unless otherwise noted, we believe this code to be complete, as per
 28  * the specification mentioned above.
 29  * If you discover missing features or bugs please note them below.
 30  * 
 31  * TODO:
 32  *   - Styles:
 33  *     - TBSTYLE_REGISTERDROP
 34  *     - TBSTYLE_EX_DOUBLEBUFFER
 35  *   - Messages:
 36  *     - TB_GETMETRICS
 37  *     - TB_GETOBJECT
 38  *     - TB_INSERTMARKHITTEST
 39  *     - TB_SAVERESTORE
 40  *     - TB_SETMETRICS
 41  *     - WM_WININICHANGE
 42  *   - Notifications:
 43  *     - NM_CHAR
 44  *     - TBN_GETOBJECT
 45  *     - TBN_SAVE
 46  *   - Button wrapping (under construction).
 47  *   - Fix TB_SETROWS and Separators.
 48  *   - iListGap custom draw support.
 49  *
 50  * Testing:
 51  *   - Run tests using Waite Group Windows95 API Bible Volume 2.
 52  *     The second cdrom contains executables addstr.exe, btncount.exe,
 53  *     btnstate.exe, butstrsz.exe, chkbtn.exe, chngbmp.exe, customiz.exe,
 54  *     enablebtn.exe, getbmp.exe, getbtn.exe, getflags.exe, hidebtn.exe,
 55  *     indetbtn.exe, insbtn.exe, pressbtn.exe, setbtnsz.exe, setcmdid.exe,
 56  *     setparnt.exe, setrows.exe, toolwnd.exe.
 57  *   - Microsoft's controlspy examples.
 58  *   - Charles Petzold's 'Programming Windows': gadgets.exe
 59  *
 60  *  Differences between MSDN and actual native control operation:
 61  *   1. MSDN says: "TBSTYLE_LIST: Creates a flat toolbar with button text
 62  *                  to the right of the bitmap. Otherwise, this style is
 63  *                  identical to TBSTYLE_FLAT."
 64  *      As implemented by both v4.71 and v5.80 of the native COMCTL32.DLL
 65  *      you can create a TBSTYLE_LIST without TBSTYLE_FLAT and the result
 66  *      is non-flat non-transparent buttons. Therefore TBSTYLE_LIST does
 67  *      *not* imply TBSTYLE_FLAT as documented.  (GA 8/2001)
 68  *
 69  */
 70 
 71 #include <stdarg.h>
 72 #include <string.h>
 73 
 74 #include "windef.h"
 75 #include "winbase.h"
 76 #include "winreg.h"
 77 #include "wingdi.h"
 78 #include "winuser.h"
 79 #include "wine/unicode.h"
 80 #include "winnls.h"
 81 #include "commctrl.h"
 82 #include "comctl32.h"
 83 #include "uxtheme.h"
 84 #include "tmschema.h"
 85 #include "wine/debug.h"
 86 
 87 WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
 88 
 89 static HCURSOR hCursorDrag = NULL;
 90 
 91 typedef struct
 92 {
 93     INT iBitmap;
 94     INT idCommand;
 95     BYTE  fsState;
 96     BYTE  fsStyle;
 97     BYTE  bHot;
 98     BYTE  bDropDownPressed;
 99     DWORD_PTR dwData;
100     INT_PTR iString;
101     INT nRow;
102     RECT rect;
103     INT cx; /* manually set size */
104 } TBUTTON_INFO;
105 
106 typedef struct
107 {
108     UINT nButtons;
109     HINSTANCE hInst;
110     UINT nID;
111 } TBITMAP_INFO;
112 
113 typedef struct
114 {
115     HIMAGELIST himl;
116     INT id;
117 } IMLENTRY, *PIMLENTRY;
118 
119 typedef struct
120 {
121     DWORD    dwStructSize;    /* size of TBBUTTON struct */
122     INT      nWidth;          /* width of the toolbar */
123     RECT     client_rect;
124     RECT     rcBound;         /* bounding rectangle */
125     INT      nButtonHeight;
126     INT      nButtonWidth;
127     INT      nBitmapHeight;
128     INT      nBitmapWidth;
129     INT      nIndent;
130     INT      nRows;           /* number of button rows */
131     INT      nMaxTextRows;    /* maximum number of text rows */
132     INT      cxMin;           /* minimum button width */
133     INT      cxMax;           /* maximum button width */
134     INT      nNumButtons;     /* number of buttons */
135     INT      nNumBitmaps;     /* number of bitmaps */
136     INT      nNumStrings;     /* number of strings */
137     INT      nNumBitmapInfos;
138     INT      nButtonDown;     /* toolbar button being pressed or -1 if none */
139     INT      nButtonDrag;     /* toolbar button being dragged or -1 if none */
140     INT      nOldHit;
141     INT      nHotItem;        /* index of the "hot" item */
142     SIZE     szPadding;       /* padding values around button */
143     INT      iTopMargin;      /* the top margin */
144     INT      iListGap;        /* default gap between text and image for toolbar with list style */
145     HFONT    hDefaultFont;
146     HFONT    hFont;           /* text font */
147     HIMAGELIST himlInt;       /* image list created internally */
148     PIMLENTRY *himlDef;       /* default image list array */
149     INT       cimlDef;        /* default image list array count */
150     PIMLENTRY *himlHot;       /* hot image list array */
151     INT       cimlHot;        /* hot image list array count */
152     PIMLENTRY *himlDis;       /* disabled image list array */
153     INT       cimlDis;        /* disabled image list array count */
154     HWND     hwndToolTip;     /* handle to tool tip control */
155     HWND     hwndNotify;      /* handle to the window that gets notifications */
156     HWND     hwndSelf;        /* my own handle */
157     BOOL     bAnchor;         /* anchor highlight enabled */
158     BOOL     bDoRedraw;       /* Redraw status */
159     BOOL     bDragOutSent;    /* has TBN_DRAGOUT notification been sent for this drag? */
160     BOOL     bUnicode;        /* Notifications are ASCII (FALSE) or Unicode (TRUE)? */
161     BOOL     bCaptured;       /* mouse captured? */
162     DWORD      dwStyle;       /* regular toolbar style */
163     DWORD      dwExStyle;     /* extended toolbar style */
164     DWORD      dwDTFlags;     /* DrawText flags */
165 
166     COLORREF   clrInsertMark;   /* insert mark color */
167     COLORREF   clrBtnHighlight; /* color for Flat Separator */
168     COLORREF   clrBtnShadow;    /* color for Flag Separator */
169     INT      iVersion;
170     LPWSTR   pszTooltipText;    /* temporary store for a string > 80 characters
171                                  * for TTN_GETDISPINFOW notification */
172     TBINSERTMARK  tbim;         /* info on insertion mark */
173     TBUTTON_INFO *buttons;      /* pointer to button array */
174     LPWSTR       *strings;      /* pointer to string array */
175     TBITMAP_INFO *bitmaps;
176 } TOOLBAR_INFO, *PTOOLBAR_INFO;
177 
178 
179 /* used by customization dialog */
180 typedef struct
181 {
182     PTOOLBAR_INFO tbInfo;
183     HWND          tbHwnd;
184 } CUSTDLG_INFO, *PCUSTDLG_INFO;
185 
186 typedef struct
187 {
188     TBBUTTON btn;
189     BOOL     bVirtual;
190     BOOL     bRemovable;
191     WCHAR    text[64];
192 } CUSTOMBUTTON, *PCUSTOMBUTTON;
193 
194 typedef enum
195 {
196     IMAGE_LIST_DEFAULT,
197     IMAGE_LIST_HOT,
198     IMAGE_LIST_DISABLED
199 } IMAGE_LIST_TYPE;
200 
201 #define SEPARATOR_WIDTH    8
202 #define TOP_BORDER         2
203 #define BOTTOM_BORDER      2
204 #define DDARROW_WIDTH      11
205 #define ARROW_HEIGHT       3
206 #define INSERTMARK_WIDTH   2
207 
208 #define DEFPAD_CX 7
209 #define DEFPAD_CY 6
210 #define DEFLISTGAP 4
211 
212 /* vertical padding used in list mode when image is present */
213 #define LISTPAD_CY 9
214 
215 /* how wide to treat the bitmap if it isn't present */
216 #define NONLIST_NOTEXT_OFFSET 2
217 
218 #define TOOLBAR_NOWHERE (-1)
219 
220 #define TOOLBAR_GetInfoPtr(hwnd) ((TOOLBAR_INFO *)GetWindowLongPtrW(hwnd,0))
221 #define TOOLBAR_HasText(x, y) (TOOLBAR_GetText(x, y) ? TRUE : FALSE)
222 #define TOOLBAR_HasDropDownArrows(exStyle) ((exStyle & TBSTYLE_EX_DRAWDDARROWS) ? TRUE : FALSE)
223 
224 /* Used to find undocumented extended styles */
225 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
226                         TBSTYLE_EX_UNDOC1 | \
227                         TBSTYLE_EX_MIXEDBUTTONS | \
228                         TBSTYLE_EX_DOUBLEBUFFER | \
229                         TBSTYLE_EX_HIDECLIPPEDBUTTONS)
230 
231 /* all of the CCS_ styles */
232 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
233                        CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
234 
235 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
236 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
237 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
238 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
239 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
240 
241 static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };
242 
243 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
244 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, PCUSTOMBUTTON btnInfo);
245 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
246 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
247 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
248 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
249 static LRESULT TOOLBAR_LButtonDown(HWND hwnd, WPARAM wParam, LPARAM lParam);
250 static void TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason);
251 static void TOOLBAR_LayoutToolbar(HWND hwnd);
252 static LRESULT TOOLBAR_AutoSize(HWND hwnd);
253 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
254 static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
255 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
256 
257 static LRESULT
258 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
259 
260 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
261 {
262     return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
263 }
264 
265 static LPWSTR
266 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
267 {
268     LPWSTR lpText = NULL;
269 
270     /* NOTE: iString == -1 is undocumented */
271     if ((HIWORD(btnPtr->iString) != 0) && (btnPtr->iString != -1))
272         lpText = (LPWSTR)btnPtr->iString;
273     else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
274         lpText = infoPtr->strings[btnPtr->iString];
275 
276     return lpText;
277 }
278 
279 static void
280 TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
281 {
282     TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx (%s)\n",
283           tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
284           (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
285 }
286 
287 static void
288 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
289 {
290     if (TRACE_ON(toolbar)){
291         TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
292               btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap), 
293               bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
294         TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
295         TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
296               btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
297               wine_dbgstr_rect(&bP->rect));
298     }
299 }
300 
301 
302 static void
303 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
304 {
305     if (TRACE_ON(toolbar)) {
306         INT i;
307 
308         TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
309               iP->hwndSelf, line,
310               iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
311               iP->nNumStrings, iP->dwStyle);
312         TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
313               iP->hwndSelf, line,
314               iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
315               (iP->bDoRedraw) ? "TRUE" : "FALSE");
316         for(i=0; i<iP->nNumButtons; i++) {
317             TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
318         }
319     }
320 }
321 
322 
323 /***********************************************************************
324 *               TOOLBAR_CheckStyle
325 *
326 * This function validates that the styles set are implemented and
327 * issues FIXME's warning of possible problems. In a perfect world this
328 * function should be null.
329 */
330 static void
331 TOOLBAR_CheckStyle (HWND hwnd, DWORD dwStyle)
332 {
333     if (dwStyle & TBSTYLE_REGISTERDROP)
334         FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", hwnd);
335 }
336 
337 
338 static INT
339 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
340 {
341         if(!IsWindow(infoPtr->hwndSelf))
342             return 0;   /* we have just been destroyed */
343 
344     nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
345     nmhdr->hwndFrom = infoPtr->hwndSelf;
346     nmhdr->code = code;
347 
348     TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
349           (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
350 
351     return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
352 }
353 
354 /***********************************************************************
355 *               TOOLBAR_GetBitmapIndex
356 *
357 * This function returns the bitmap index associated with a button.
358 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
359 * is issued to retrieve the index.
360 */
361 static INT
362 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
363 {
364     INT ret = btnPtr->iBitmap;
365 
366     if (ret == I_IMAGECALLBACK)
367     {
368         /* issue TBN_GETDISPINFO */
369         NMTBDISPINFOW nmgd;
370 
371         memset(&nmgd, 0, sizeof(nmgd));
372         nmgd.idCommand = btnPtr->idCommand;
373         nmgd.lParam = btnPtr->dwData;
374         nmgd.dwMask = TBNF_IMAGE;
375         nmgd.iImage = -1;
376         /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
377         TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
378         if (nmgd.dwMask & TBNF_DI_SETITEM)
379             btnPtr->iBitmap = nmgd.iImage;
380         ret = nmgd.iImage;
381         TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
382             ret, nmgd.dwMask, infoPtr->nNumBitmaps);
383     }
384 
385     if (ret != I_IMAGENONE)
386         ret = GETIBITMAP(infoPtr, ret);
387 
388     return ret;
389 }
390 
391 
392 static BOOL
393 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
394 {
395     HIMAGELIST himl;
396     INT id = GETHIMLID(infoPtr, index);
397     INT iBitmap = GETIBITMAP(infoPtr, index);
398 
399     if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
400         iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
401         (index == I_IMAGECALLBACK))
402       return TRUE;
403     else
404       return FALSE;
405 }
406 
407 
408 static inline BOOL
409 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
410 {
411     HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
412     return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
413 }
414 
415 
416 /***********************************************************************
417 *               TOOLBAR_GetImageListForDrawing
418 *
419 * This function validates the bitmap index (including I_IMAGECALLBACK
420 * functionality) and returns the corresponding image list.
421 */
422 static HIMAGELIST
423 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
424                                 IMAGE_LIST_TYPE imagelist, INT * index)
425 {
426     HIMAGELIST himl;
427 
428     if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
429         if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
430         ERR("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
431             HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
432         return NULL;
433     }
434 
435     if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
436         if ((*index == I_IMAGECALLBACK) ||
437             (*index == I_IMAGENONE)) return NULL;
438         ERR("TBN_GETDISPINFO returned invalid index %d\n",
439             *index);
440         return NULL;
441     }
442 
443     switch(imagelist)
444     {
445     case IMAGE_LIST_DEFAULT:
446         himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
447         break;
448     case IMAGE_LIST_HOT:
449         himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
450         break;
451     case IMAGE_LIST_DISABLED:
452         himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
453         break;
454     default:
455         himl = NULL;
456         FIXME("Shouldn't reach here\n");
457     }
458 
459     if (!himl)
460        TRACE("no image list\n");
461 
462     return himl;
463 }
464 
465 
466 static void
467 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
468 {
469     RECT myrect;
470     COLORREF oldcolor, newcolor;
471 
472     myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
473     myrect.right = myrect.left + 1;
474     myrect.top = lpRect->top + 2;
475     myrect.bottom = lpRect->bottom - 2;
476 
477     newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
478                 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
479     oldcolor = SetBkColor (hdc, newcolor);
480     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
481 
482     myrect.left = myrect.right;
483     myrect.right = myrect.left + 1;
484 
485     newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
486                 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
487     SetBkColor (hdc, newcolor);
488     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
489 
490     SetBkColor (hdc, oldcolor);
491 }
492 
493 
494 /***********************************************************************
495 *               TOOLBAR_DrawFlatHorizontalSeparator
496 *
497 * This function draws horizontal separator for toolbars having CCS_VERT style.
498 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
499 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
500 * are horizontal as opposed to the vertical separators for not dropdown
501 * type.
502 *
503 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
504 */
505 static void
506 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
507                              const TOOLBAR_INFO *infoPtr)
508 {
509     RECT myrect;
510     COLORREF oldcolor, newcolor;
511 
512     myrect.left = lpRect->left;
513     myrect.right = lpRect->right;
514     myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
515     myrect.bottom = myrect.top + 1;
516 
517     InflateRect (&myrect, -2, 0);
518 
519     TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
520 
521     newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
522                 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
523     oldcolor = SetBkColor (hdc, newcolor);
524     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
525 
526     myrect.top = myrect.bottom;
527     myrect.bottom = myrect.top + 1;
528 
529     newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
530                 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
531     SetBkColor (hdc, newcolor);
532     ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
533 
534     SetBkColor (hdc, oldcolor);
535 }
536 
537 
538 static void
539 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
540 {
541     INT x, y;
542     HPEN hPen, hOldPen;
543 
544     if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
545     hOldPen = SelectObject ( hdc, hPen );
546     x = left + 2;
547     y = top;
548     MoveToEx (hdc, x, y, NULL);
549     LineTo (hdc, x+5, y++); x++;
550     MoveToEx (hdc, x, y, NULL);
551     LineTo (hdc, x+3, y++); x++;
552     MoveToEx (hdc, x, y, NULL);
553     LineTo (hdc, x+1, y);
554     SelectObject( hdc, hOldPen );
555     DeleteObject( hPen );
556 }
557 
558 /*
559  * Draw the text string for this button.
560  * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
561  *      is non-zero, so we can simply check himlDef to see if we have
562  *      an image list
563  */
564 static void
565 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
566                     const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
567 {
568     HDC hdc = tbcd->nmcd.hdc;
569     HFONT  hOldFont = 0;
570     COLORREF clrOld = 0;
571     COLORREF clrOldBk = 0;
572     int oldBkMode = 0;
573     UINT state = tbcd->nmcd.uItemState;
574 
575     /* draw text */
576     if (lpText) {
577         TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
578               wine_dbgstr_rect(rcText));
579 
580         hOldFont = SelectObject (hdc, infoPtr->hFont);
581         if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
582             clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
583         }
584         else if (state & CDIS_DISABLED) {
585             clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
586             OffsetRect (rcText, 1, 1);
587             DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
588             SetTextColor (hdc, comctl32_color.clr3dShadow);
589             OffsetRect (rcText, -1, -1);
590         }
591         else if (state & CDIS_INDETERMINATE) {
592             clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
593         }
594         else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
595             clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
596             clrOldBk = SetBkColor (hdc, tbcd->clrMark);
597             oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
598         }
599         else {
600             clrOld = SetTextColor (hdc, tbcd->clrText);
601         }
602 
603         DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
604         SetTextColor (hdc, clrOld);
605         if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
606         {
607             SetBkColor (hdc, clrOldBk);
608             SetBkMode (hdc, oldBkMode);
609         }
610         SelectObject (hdc, hOldFont);
611     }
612 }
613 
614 
615 static void
616 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
617 {
618     HDC hdc = tbcd->nmcd.hdc;
619     HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
620     COLORREF clrTextOld;
621     COLORREF clrBkOld;
622     INT cx = lpRect->right - lpRect->left;
623     INT cy = lpRect->bottom - lpRect->top;
624     INT cxEdge = GetSystemMetrics(SM_CXEDGE);
625     INT cyEdge = GetSystemMetrics(SM_CYEDGE);
626     clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
627     clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
628     PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
629             cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
630     SetBkColor(hdc, clrBkOld);
631     SetTextColor(hdc, clrTextOld);
632     SelectObject (hdc, hbr);
633 }
634 
635 
636 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
637 {
638     INT cx, cy;
639     HBITMAP hbmMask, hbmImage;
640     HDC hdcMask, hdcImage;
641 
642     ImageList_GetIconSize(himl, &cx, &cy);
643 
644     /* Create src image */
645     hdcImage = CreateCompatibleDC(hdc);
646     hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
647     SelectObject(hdcImage, hbmImage);
648     ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
649                      RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
650 
651     /* Create Mask */
652     hdcMask = CreateCompatibleDC(0);
653     hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
654     SelectObject(hdcMask, hbmMask);
655 
656     /* Remove the background and all white pixels */
657     ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
658                      RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
659     SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
660     BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
661 
662     /* draw the new mask 'etched' to hdc */
663     SetBkColor(hdc, RGB(255, 255, 255));
664     SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
665     /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
666     BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
667     SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
668     BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
669 
670     /* Cleanup */
671     DeleteObject(hbmImage);
672     DeleteDC(hdcImage);
673     DeleteObject (hbmMask);
674     DeleteDC(hdcMask);
675 }
676 
677 
678 static UINT
679 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
680 {
681     UINT retstate = 0;
682 
683     retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED  : 0;
684     retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
685     retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
686     retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED   : 0;
687     retstate |= (btnPtr->bHot                     ) ? CDIS_HOT      : 0;
688     retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
689     /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
690     return retstate;
691 }
692 
693 /* draws the image on a toolbar button */
694 static void
695 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
696                   const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
697 {
698     HIMAGELIST himl = NULL;
699     BOOL draw_masked = FALSE;
700     INT index;
701     INT offset = 0;
702     UINT draw_flags = ILD_TRANSPARENT;
703 
704     if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
705     {
706         himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
707         if (!himl)
708         {
709             himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
710             draw_masked = TRUE;
711         }
712     }
713     else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
714       ((tbcd->nmcd.uItemState & CDIS_HOT) 
715       && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
716     {
717         /* if hot, attempt to draw with hot image list, if fails, 
718            use default image list */
719         himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
720         if (!himl)
721             himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
722         }
723     else
724         himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
725 
726     if (!himl)
727         return;
728 
729     if (!(dwItemCDFlag & TBCDRF_NOOFFSET) && 
730         (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
731         offset = 1;
732 
733     if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
734         (tbcd->nmcd.uItemState & CDIS_MARKED))
735         draw_flags |= ILD_BLEND50;
736 
737     TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
738       index, himl, left, top, offset);
739 
740     if (draw_masked)
741         TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
742     else
743         ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
744 }
745 
746 /* draws a blank frame for a toolbar button */
747 static void
748 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
749 {
750     HDC hdc = tbcd->nmcd.hdc;
751     RECT rc = tbcd->nmcd.rc;
752     /* if the state is disabled or indeterminate then the button
753      * cannot have an interactive look like pressed or hot */
754     BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
755                                  (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
756     BOOL pressed_look = !non_interactive_state &&
757                         ((tbcd->nmcd.uItemState & CDIS_SELECTED) || 
758                          (tbcd->nmcd.uItemState & CDIS_CHECKED));
759 
760     /* app don't want us to draw any edges */
761     if (dwItemCDFlag & TBCDRF_NOEDGES)
762         return;
763 
764     if (infoPtr->dwStyle & TBSTYLE_FLAT)
765     {
766         if (pressed_look)
767             DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
768         else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
769             DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
770     }
771     else
772     {
773         if (pressed_look)
774             DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
775         else
776             DrawEdge (hdc, &rc, EDGE_RAISED,
777               BF_SOFT | BF_RECT | BF_MIDDLE);
778     }
779 }
780 
781 static void
782 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
783 {
784     HDC hdc = tbcd->nmcd.hdc;
785     int offset = 0;
786     BOOL pressed = bDropDownPressed ||
787         (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
788 
789     if (infoPtr->dwStyle & TBSTYLE_FLAT)
790     {
791         if (pressed)
792             DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
793         else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
794                  !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
795                  !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
796             DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
797     }
798     else
799     {
800         if (pressed)
801             DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
802         else
803             DrawEdge (hdc, rcArrow, EDGE_RAISED,
804               BF_SOFT | BF_RECT | BF_MIDDLE);
805     }
806 
807     if (pressed)
808         offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
809 
810     if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
811     {
812         TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
813         TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
814     }
815     else
816         TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
817 }
818 
819 /* draws a complete toolbar button */
820 static void
821 TOOLBAR_DrawButton (HWND hwnd, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
822 {
823     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
824     DWORD dwStyle = infoPtr->dwStyle;
825     BOOL hasDropDownArrow = (TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) &&
826                             (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
827                             (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
828     BOOL drawSepDropDownArrow = hasDropDownArrow && 
829                                 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
830     RECT rc, rcArrow, rcBitmap, rcText;
831     LPWSTR lpText = NULL;
832     NMTBCUSTOMDRAW tbcd;
833     DWORD ntfret;
834     INT offset;
835     INT oldBkMode;
836     DWORD dwItemCustDraw;
837     DWORD dwItemCDFlag;
838     HTHEME theme = GetWindowTheme (hwnd);
839 
840     rc = btnPtr->rect;
841     CopyRect (&rcArrow, &rc);
842 
843     /* separator - doesn't send NM_CUSTOMDRAW */
844     if (btnPtr->fsStyle & BTNS_SEP) {
845         if (theme)
846         {
847             DrawThemeBackground (theme, hdc, 
848                 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0, 
849                 &rc, NULL);
850         }
851         else
852         /* with the FLAT style, iBitmap is the width and has already */
853         /* been taken into consideration in calculating the width    */
854         /* so now we need to draw the vertical separator             */
855         /* empirical tests show that iBitmap can/will be non-zero    */
856         /* when drawing the vertical bar...      */
857         if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
858             if (dwStyle & CCS_VERT)
859                TOOLBAR_DrawFlatHorizontalSeparator (&rc, hdc, infoPtr);
860             else
861                 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
862         }
863         else if (btnPtr->fsStyle != BTNS_SEP) {
864             FIXME("Draw some kind of separator: fsStyle=%x\n",
865                   btnPtr->fsStyle);
866         }
867         return;
868     }
869 
870     /* get a pointer to the text */
871     lpText = TOOLBAR_GetText(infoPtr, btnPtr);
872 
873     if (hasDropDownArrow)
874     {
875         int right;
876 
877         if (dwStyle & TBSTYLE_FLAT)
878             right = max(rc.left, rc.right - DDARROW_WIDTH);
879         else
880             right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
881 
882         if (drawSepDropDownArrow)
883            rc.right = right;
884 
885         rcArrow.left = right;
886     }
887 
888     /* copy text & bitmap rects after adjusting for drop-down arrow
889      * so that text & bitmap is centered in the rectangle not containing
890      * the arrow */
891     CopyRect(&rcText, &rc);
892     CopyRect(&rcBitmap, &rc);
893 
894     /* Center the bitmap horizontally and vertically */
895     if (dwStyle & TBSTYLE_LIST)
896     {
897         if (lpText &&
898             infoPtr->nMaxTextRows > 0 &&
899             (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
900             (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
901             rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
902         else
903             rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
904     }
905     else
906         rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
907 
908     rcBitmap.top += infoPtr->szPadding.cy / 2;
909 
910     TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
911       btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
912       infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
913     TRACE("Text=%s\n", debugstr_w(lpText));
914     TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
915 
916     /* calculate text position */
917     if (lpText)
918     {
919         rcText.left += GetSystemMetrics(SM_CXEDGE);
920         rcText.right -= GetSystemMetrics(SM_CXEDGE);
921         if (dwStyle & TBSTYLE_LIST)
922         {
923             rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
924         }
925         else
926         {
927             if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
928                 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
929             else
930                 rcText.top += infoPtr->szPadding.cy/2 + 2;
931         }
932     }
933 
934     /* Initialize fields in all cases, because we use these later
935      * NOTE: applications can and do alter these to customize their
936      * toolbars */
937     ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
938     tbcd.clrText = comctl32_color.clrBtnText;
939     tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
940     tbcd.clrBtnFace = comctl32_color.clrBtnFace;
941     tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
942     tbcd.clrMark = comctl32_color.clrHighlight;
943     tbcd.clrHighlightHotTrack = 0;
944     tbcd.nStringBkMode = TRANSPARENT;
945     tbcd.nHLStringBkMode = OPAQUE;
946     /* MSDN says that this is the text rectangle.
947      * But (why always a but) tracing of v5.7 of native shows
948      * that this is really a *relative* rectangle based on the
949      * the nmcd.rc. Also the left and top are always 0 ignoring
950      * any bitmap that might be present. */
951     tbcd.rcText.left = 0;
952     tbcd.rcText.top = 0;
953     tbcd.rcText.right = rcText.right - rc.left;
954     tbcd.rcText.bottom = rcText.bottom - rc.top;
955     tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
956     tbcd.nmcd.hdc = hdc;
957     tbcd.nmcd.rc = rc;
958     tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
959 
960     /* FIXME: what are these used for? */
961     tbcd.hbrLines = 0;
962     tbcd.hpenLines = 0;
963 
964     /* Issue Item Prepaint notify */
965     dwItemCustDraw = 0;
966     dwItemCDFlag = 0;
967     if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
968     {
969         tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
970         tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
971         tbcd.nmcd.lItemlParam = btnPtr->dwData;
972         ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
973         /* reset these fields so the user can't alter the behaviour like native */
974         tbcd.nmcd.hdc = hdc;
975         tbcd.nmcd.rc = rc;
976 
977         dwItemCustDraw = ntfret & 0xffff;
978         dwItemCDFlag = ntfret & 0xffff0000;
979         if (dwItemCustDraw & CDRF_SKIPDEFAULT)
980             return;
981         /* save the only part of the rect that the user can change */
982         rcText.right = tbcd.rcText.right + rc.left;
983         rcText.bottom = tbcd.rcText.bottom + rc.top;
984     }
985 
986     if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
987         (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
988         OffsetRect(&rcText, 1, 1);
989 
990     if (!(tbcd.nmcd.uItemState & CDIS_HOT) && 
991         ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
992         TOOLBAR_DrawPattern (&rc, &tbcd);
993 
994     if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) 
995         && (tbcd.nmcd.uItemState & CDIS_HOT))
996     {
997         if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
998         {
999             COLORREF oldclr;
1000 
1001             oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1002             ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1003             if (hasDropDownArrow)
1004                 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1005             SetBkColor(hdc, oldclr);
1006         }
1007     }
1008 
1009     if (theme)
1010     {
1011         int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1012         int stateId = TS_NORMAL;
1013         
1014         if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1015             stateId = TS_DISABLED;
1016         else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1017             stateId = TS_PRESSED;
1018         else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1019             stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1020         else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1021             || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1022             stateId = TS_HOT;
1023             
1024         DrawThemeBackground (theme, hdc, partId, stateId, &tbcd.nmcd.rc, NULL);
1025     }
1026     else
1027         TOOLBAR_DrawFrame(infoPtr, &tbcd, dwItemCDFlag);
1028 
1029     if (drawSepDropDownArrow)
1030     {
1031         if (theme)
1032         {
1033             int stateId = TS_NORMAL;
1034             
1035             if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1036                 stateId = TS_DISABLED;
1037             else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1038                 stateId = TS_PRESSED;
1039             else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1040                 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1041             else if (tbcd.nmcd.uItemState & CDIS_HOT)
1042                 stateId = TS_HOT;
1043                 
1044             DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1045             DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1046         }
1047         else
1048             TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1049     }
1050 
1051     oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1052     if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1053         TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1054     SetBkMode (hdc, oldBkMode);
1055 
1056     TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1057 
1058     if (hasDropDownArrow && !drawSepDropDownArrow)
1059     {
1060         if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1061         {
1062             TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1063             TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1064         }
1065         else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1066         {
1067             offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1068             TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1069         }
1070         else
1071             TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1072     }
1073 
1074     if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1075     {
1076         tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1077         TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1078     }
1079 
1080 }
1081 
1082 
1083 static void
1084 TOOLBAR_Refresh (HWND hwnd, HDC hdc, const PAINTSTRUCT *ps)
1085 {
1086     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1087     TBUTTON_INFO *btnPtr;
1088     INT i;
1089     RECT rcTemp, rcClient;
1090     NMTBCUSTOMDRAW tbcd;
1091     DWORD ntfret;
1092     DWORD dwBaseCustDraw;
1093 
1094     /* the app has told us not to redraw the toolbar */
1095     if (!infoPtr->bDoRedraw)
1096         return;
1097 
1098     /* if imagelist belongs to the app, it can be changed
1099        by the app after setting it */
1100     if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1101     {
1102         infoPtr->nNumBitmaps = 0;
1103         for (i = 0; i < infoPtr->cimlDef; i++)
1104             infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1105     }
1106 
1107     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1108 
1109     /* change the imagelist icon size if we manage the list and it is necessary */
1110     TOOLBAR_CheckImageListIconSize(infoPtr);
1111 
1112     /* Send initial notify */
1113     ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1114     tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1115     tbcd.nmcd.hdc = hdc;
1116     tbcd.nmcd.rc = ps->rcPaint;
1117     ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1118     dwBaseCustDraw = ntfret & 0xffff;
1119 
1120     GetClientRect(hwnd, &rcClient);
1121 
1122     /* redraw necessary buttons */
1123     btnPtr = infoPtr->buttons;
1124     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1125     {
1126         BOOL bDraw;
1127         if (!RectVisible(hdc, &btnPtr->rect))
1128             continue;
1129         if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1130         {
1131             IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1132             bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1133         }
1134         else
1135             bDraw = TRUE;
1136         bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1137         bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1138         if (bDraw)
1139             TOOLBAR_DrawButton(hwnd, btnPtr, hdc, dwBaseCustDraw);
1140     }
1141 
1142     /* draw insert mark if required */
1143     if (infoPtr->tbim.iButton != -1)
1144     {
1145         RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1146         RECT rcInsertMark;
1147         rcInsertMark.top = rcButton.top;
1148         rcInsertMark.bottom = rcButton.bottom;
1149         if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1150             rcInsertMark.left = rcInsertMark.right = rcButton.right;
1151         else
1152             rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1153         COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1154     }
1155 
1156     if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1157     {
1158         ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1159         tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1160         tbcd.nmcd.hdc = hdc;
1161         tbcd.nmcd.rc = ps->rcPaint;
1162         TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1163     }
1164 }
1165 
1166 /***********************************************************************
1167 *               TOOLBAR_MeasureString
1168 *
1169 * This function gets the width and height of a string in pixels. This
1170 * is done first by using GetTextExtentPoint to get the basic width
1171 * and height. The DrawText is called with DT_CALCRECT to get the exact
1172 * width. The reason is because the text may have more than one "&" (or
1173 * prefix characters as M$ likes to call them). The prefix character
1174 * indicates where the underline goes, except for the string "&&" which
1175 * is reduced to a single "&". GetTextExtentPoint does not process these
1176 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1177 */
1178 static void
1179 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1180                       HDC hdc, LPSIZE lpSize)
1181 {
1182     RECT myrect;
1183 
1184     lpSize->cx = 0;
1185     lpSize->cy = 0;
1186 
1187     if (infoPtr->nMaxTextRows > 0 &&
1188         !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1189         (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1190         (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1191     {
1192         LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1193 
1194         if(lpText != NULL) {
1195             /* first get size of all the text */
1196             GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1197 
1198             /* feed above size into the rectangle for DrawText */
1199             myrect.left = myrect.top = 0;
1200             myrect.right = lpSize->cx;
1201             myrect.bottom = lpSize->cy;
1202 
1203             /* Use DrawText to get true size as drawn (less pesky "&") */
1204             DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1205                    DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1206                                   DT_NOPREFIX : 0));
1207 
1208             /* feed back to caller  */
1209             lpSize->cx = myrect.right;
1210             lpSize->cy = myrect.bottom;
1211         }
1212     }
1213 
1214     TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1215 }
1216 
1217 /***********************************************************************
1218 *               TOOLBAR_CalcStrings
1219 *
1220 * This function walks through each string and measures it and returns
1221 * the largest height and width to caller.
1222 */
1223 static void
1224 TOOLBAR_CalcStrings (HWND hwnd, LPSIZE lpSize)
1225 {
1226     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1227     TBUTTON_INFO *btnPtr;
1228     INT i;
1229     SIZE sz;
1230     HDC hdc;
1231     HFONT hOldFont;
1232 
1233     lpSize->cx = 0;
1234     lpSize->cy = 0;
1235 
1236     if (infoPtr->nMaxTextRows == 0)
1237         return;
1238 
1239     hdc = GetDC (hwnd);
1240     hOldFont = SelectObject (hdc, infoPtr->hFont);
1241 
1242     if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1243     {
1244         TEXTMETRICW tm;
1245 
1246         GetTextMetricsW(hdc, &tm);
1247         lpSize->cy = tm.tmHeight;
1248     }
1249 
1250     btnPtr = infoPtr->buttons;
1251     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1252         if(TOOLBAR_HasText(infoPtr, btnPtr))
1253         {
1254             TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1255             if (sz.cx > lpSize->cx)
1256                 lpSize->cx = sz.cx;
1257             if (sz.cy > lpSize->cy)
1258                 lpSize->cy = sz.cy;
1259         }
1260     }
1261 
1262     SelectObject (hdc, hOldFont);
1263     ReleaseDC (hwnd, hdc);
1264 
1265     TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1266 }
1267 
1268 /***********************************************************************
1269 *               TOOLBAR_WrapToolbar
1270 *
1271 * This function walks through the buttons and separators in the
1272 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1273 * wrapping should occur based on the width of the toolbar window.
1274 * It does *not* calculate button placement itself.  That task
1275 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1276 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1277 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1278 *
1279 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_UNDOC1 can be used also to allow
1280 * vertical toolbar lists.
1281 */
1282 
1283 static void
1284 TOOLBAR_WrapToolbar( HWND hwnd, DWORD dwStyle )
1285 {
1286     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1287     TBUTTON_INFO *btnPtr;
1288     INT x, cx, i, j;
1289     RECT rc;
1290     BOOL bButtonWrap;
1291 
1292     /*  When the toolbar window style is not TBSTYLE_WRAPABLE,  */
1293     /*  no layout is necessary. Applications may use this style */
1294     /*  to perform their own layout on the toolbar.             */
1295     if( !(dwStyle & TBSTYLE_WRAPABLE) &&
1296         !(infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1) )  return;
1297 
1298     btnPtr = infoPtr->buttons;
1299     x  = infoPtr->nIndent;
1300 
1301     if (GetParent(hwnd))
1302     {
1303         /* this can get the parents width, to know how far we can extend
1304          * this toolbar.  We cannot use its height, as there may be multiple
1305          * toolbars in a rebar control
1306          */
1307         GetClientRect( GetParent(hwnd), &rc );
1308         infoPtr->nWidth = rc.right - rc.left;
1309     }
1310     else
1311     {
1312         GetWindowRect( hwnd, &rc );
1313         infoPtr->nWidth = rc.right - rc.left;
1314     }
1315 
1316     bButtonWrap = FALSE;
1317 
1318     TRACE("start ButtonWidth=%d, BitmapWidth=%d, nWidth=%d, nIndent=%d\n",
1319           infoPtr->nButtonWidth, infoPtr->nBitmapWidth, infoPtr->nWidth,
1320           infoPtr->nIndent);
1321 
1322     for (i = 0; i < infoPtr->nNumButtons; i++ )
1323     {
1324         btnPtr[i].fsState &= ~TBSTATE_WRAP;
1325 
1326         if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1327             continue;
1328 
1329         if (btnPtr[i].cx > 0)
1330             cx = btnPtr[i].cx;
1331         /* horizontal separators are treated as buttons for width    */
1332         else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1333             !(infoPtr->dwStyle & CCS_VERT))
1334             cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1335         else
1336             cx = infoPtr->nButtonWidth;
1337 
1338         /* Two or more adjacent separators form a separator group.   */
1339         /* The first separator in a group should be wrapped to the   */
1340         /* next row if the previous wrapping is on a button.         */
1341         if( bButtonWrap &&
1342                 (btnPtr[i].fsStyle & BTNS_SEP) &&
1343                 (i + 1 < infoPtr->nNumButtons ) &&
1344                 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1345         {
1346             TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1347             btnPtr[i].fsState |= TBSTATE_WRAP;
1348             x = infoPtr->nIndent;
1349             i++;
1350             bButtonWrap = FALSE;
1351             continue;
1352         }
1353 
1354         /* The layout makes sure the bitmap is visible, but not the button. */
1355         /* Test added to also wrap after a button that starts a row but     */
1356         /* is bigger than the area.  - GA  8/01                             */
1357         if (( x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2
1358            > infoPtr->nWidth ) ||
1359             ((x == infoPtr->nIndent) && (cx > infoPtr->nWidth)))
1360         {
1361             BOOL bFound = FALSE;
1362 
1363             /*  If the current button is a separator and not hidden,  */
1364             /*  go to the next until it reaches a non separator.      */
1365             /*  Wrap the last separator if it is before a button.     */
1366             while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1367                       !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1368                      (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1369                         i < infoPtr->nNumButtons )
1370             {
1371                 i++;
1372                 bFound = TRUE;
1373             }
1374 
1375             if( bFound && i < infoPtr->nNumButtons )
1376             {
1377                 i--;
1378                 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1379                       i, btnPtr[i].fsStyle, x, cx);
1380                 btnPtr[i].fsState |= TBSTATE_WRAP;
1381                 x = infoPtr->nIndent;
1382                 bButtonWrap = FALSE;
1383                 continue;
1384             }
1385             else if ( i >= infoPtr->nNumButtons)
1386                 break;
1387 
1388             /*  If the current button is not a separator, find the last  */
1389             /*  separator and wrap it.                                   */
1390             for ( j = i - 1; j >= 0  &&  !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1391             {
1392                 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1393                         !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1394                 {
1395                     bFound = TRUE;
1396                     i = j;
1397                     TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1398                           i, btnPtr[i].fsStyle, x, cx);
1399                     x = infoPtr->nIndent;
1400                     btnPtr[j].fsState |= TBSTATE_WRAP;
1401                     bButtonWrap = FALSE;
1402                     break;
1403                 }
1404             }
1405 
1406             /*  If no separator available for wrapping, wrap one of     */
1407             /*  non-hidden previous button.                             */
1408             if (!bFound)
1409             {
1410                 for ( j = i - 1;
1411                         j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1412                 {
1413                     if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1414                         continue;
1415 
1416                     bFound = TRUE;
1417                     i = j;
1418                     TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1419                           i, btnPtr[i].fsStyle, x, cx);
1420                     x = infoPtr->nIndent;
1421                     btnPtr[j].fsState |= TBSTATE_WRAP;
1422                     bButtonWrap = TRUE;
1423                     break;
1424                 }
1425             }
1426 
1427             /* If all above failed, wrap the current button. */
1428             if (!bFound)
1429             {
1430                 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1431                       i, btnPtr[i].fsStyle, x, cx);
1432                 btnPtr[i].fsState |= TBSTATE_WRAP;
1433                 x = infoPtr->nIndent;
1434                 if (btnPtr[i].fsStyle & BTNS_SEP )
1435                     bButtonWrap = FALSE;
1436                 else
1437                     bButtonWrap = TRUE;
1438             }
1439         }
1440         else {
1441             TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1442                   i, btnPtr[i].fsStyle, x, cx);
1443             x += cx;
1444         }
1445     }
1446 }
1447 
1448 
1449 /***********************************************************************
1450 *               TOOLBAR_MeasureButton
1451 *
1452 * Calculates the width and height required for a button. Used in
1453 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1454 * the width of buttons that are autosized.
1455 *
1456 * Note that it would have been rather elegant to use one piece of code for
1457 * both the laying out of the toolbar and for controlling where button parts
1458 * are drawn, but the native control has inconsistencies between the two that
1459 * prevent this from being effectively. These inconsistencies can be seen as
1460 * artefacts where parts of the button appear outside of the bounding button
1461 * rectangle.
1462 *
1463 * There are several cases for the calculation of the button dimensions and
1464 * button part positioning:
1465 *
1466 * List
1467 * ====
1468 *
1469 * With Bitmap:
1470 *
1471 * +--------------------------------------------------------+ ^
1472 * |                    ^                     ^             | |
1473 * |                    | pad.cy / 2          | centred     | |
1474 * | pad.cx/2 + cxedge +--------------+     +------------+  | | DEFPAD_CY +
1475 * |<----------------->| nBitmapWidth |     | Text       |  | | max(nBitmapHeight, szText.cy)
1476 * |                   |<------------>|     |            |  | |
1477 * |                   +--------------+     +------------+  | |
1478 * |<-------------------------------------->|               | |
1479 * |  cxedge + iListGap + nBitmapWidth + 2  |<----------->  | |
1480 * |                                           szText.cx    | |
1481 * +--------------------------------------------------------+ -
1482 * <-------------------------------------------------------->
1483 *  2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1484 *
1485 * Without Bitmap (I_IMAGENONE):
1486 *
1487 * +-----------------------------------+ ^
1488 * |                     ^             | |
1489 * |                     | centred     | | LISTPAD_CY +
1490 * |                   +------------+  | | szText.cy
1491 * |                   | Text       |  | |
1492 * |                   |            |  | |
1493 * |                   +------------+  | |
1494 * |<----------------->|               | |
1495 * |      cxedge       |<----------->  | |
1496 * |                      szText.cx    | |
1497 * +-----------------------------------+ -
1498 * <----------------------------------->
1499 *          szText.cx + pad.cx
1500 *
1501 * Without text:
1502 *
1503 * +--------------------------------------+ ^
1504 * |                       ^              | |
1505 * |                       | padding.cy/2 | | DEFPAD_CY +
1506 * |                     +------------+   | | nBitmapHeight
1507 * |                     | Bitmap     |   | |
1508 * |                     |            |   | |
1509 * |                     +------------+   | |
1510 * |<------------------->|                | |
1511 * | cxedge + iListGap/2 |<----------->   | |
1512 * |                       nBitmapWidth   | |
1513 * +--------------------------------------+ -
1514 * <-------------------------------------->
1515 *     2*cxedge + nBitmapWidth + iListGap
1516 *
1517 * Non-List
1518 * ========
1519 *
1520 * With bitmap:
1521 *
1522 * +-----------------------------------+ ^
1523 * |                     ^             | |
1524 * |                     | pad.cy / 2  | | nBitmapHeight +
1525 * |                     -             | | szText.cy +
1526 * |                   +------------+  | | DEFPAD_CY + 1
1527 * |    centred        |   Bitmap   |  | |
1528 * |<----------------->|            |  | |
1529 * |                   +------------+  | |
1530 * |                         ^         | |
1531 * |                       1 |         | |
1532 * |                         -         | |
1533 * |     centred     +---------------+ | |
1534 * |<--------------->|      Text     | | |
1535 * |                 +---------------+ | |
1536 * +-----------------------------------+ -
1537 * <----------------------------------->
1538 * pad.cx + max(nBitmapWidth, szText.cx)
1539 *
1540 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1541 *
1542 * +---------------------------------------+ ^
1543 * |                     ^                 | |
1544 * |                     | 2 + pad.cy / 2  | |
1545 * |                     -                 | | szText.cy +
1546 * |    centred      +-----------------+   | | pad.cy + 2
1547 * |<--------------->|   Text          |   | |
1548 * |                 +-----------------+   | |
1549 * |                                       | |
1550 * +---------------------------------------+ -
1551 * <--------------------------------------->
1552 *          2*cxedge + pad.cx + szText.cx
1553 *
1554 * Without text:
1555 *   As for with bitmaps, but with szText.cx zero.
1556 */
1557 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1558                                          BOOL bHasBitmap, BOOL bValidImageList)
1559 {
1560     SIZE sizeButton;
1561     if (infoPtr->dwStyle & TBSTYLE_LIST)
1562     {
1563         /* set button height from bitmap / text height... */
1564         sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1565             sizeString.cy);
1566 
1567         /* ... add on the necessary padding */
1568         if (bValidImageList)
1569         {
1570             if (bHasBitmap)
1571                 sizeButton.cy += DEFPAD_CY;
1572             else
1573                 sizeButton.cy += LISTPAD_CY;
1574         }
1575         else
1576             sizeButton.cy += infoPtr->szPadding.cy;
1577 
1578         /* calculate button width */
1579         sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1580             infoPtr->nBitmapWidth + infoPtr->iListGap;
1581         if (sizeString.cx > 0)
1582             sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1583 
1584     }
1585     else
1586     {
1587         if (bHasBitmap)
1588         {
1589             sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1590             if (sizeString.cy > 0)
1591                 sizeButton.cy += 1 + sizeString.cy;
1592             sizeButton.cx = infoPtr->szPadding.cx +
1593                 max(sizeString.cx, infoPtr->nBitmapWidth);
1594         }
1595         else
1596         {
1597             sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1598                 NONLIST_NOTEXT_OFFSET;
1599             sizeButton.cx = infoPtr->szPadding.cx +
1600                 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1601         }
1602     }
1603     return sizeButton;
1604 }
1605 
1606 
1607 /***********************************************************************
1608 *               TOOLBAR_CalcToolbar
1609 *
1610 * This function calculates button and separator placement. It first
1611 * calculates the button sizes, gets the toolbar window width and then
1612 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1613 * on. It assigns a new location to each item and sends this location to
1614 * the tooltip window if appropriate. Finally, it updates the rcBound
1615 * rect and calculates the new required toolbar window height.
1616 */
1617 static void
1618 TOOLBAR_CalcToolbar (HWND hwnd)
1619 {
1620     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1621     SIZE  sizeString, sizeButton;
1622     BOOL validImageList = FALSE;
1623 
1624     TOOLBAR_CalcStrings (hwnd, &sizeString);
1625 
1626     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1627 
1628     if (TOOLBAR_IsValidImageList(infoPtr, 0))
1629         validImageList = TRUE;
1630     sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1631     infoPtr->nButtonWidth = sizeButton.cx;
1632     infoPtr->nButtonHeight = sizeButton.cy;
1633     infoPtr->iTopMargin = default_top_margin(infoPtr);
1634 
1635     if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1636         infoPtr->nButtonWidth = infoPtr->cxMin;
1637     if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1638         infoPtr->nButtonWidth = infoPtr->cxMax;
1639 
1640     TOOLBAR_LayoutToolbar(hwnd);
1641 }
1642 
1643 static void
1644 TOOLBAR_LayoutToolbar(HWND hwnd)
1645 {
1646     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1647     TBUTTON_INFO *btnPtr;
1648     SIZE sizeButton;
1649     INT i, nRows, nSepRows;
1650     INT x, y, cx, cy;
1651     BOOL bWrap;
1652     BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1653     BOOL hasDropDownArrows = TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle);
1654 
1655     TOOLBAR_WrapToolbar(hwnd, infoPtr->dwStyle);
1656 
1657     x  = infoPtr->nIndent;
1658     y  = infoPtr->iTopMargin;
1659     cx = infoPtr->nButtonWidth;
1660     cy = infoPtr->nButtonHeight;
1661 
1662     nRows = nSepRows = 0;
1663 
1664     infoPtr->rcBound.top = y;
1665     infoPtr->rcBound.left = x;
1666     infoPtr->rcBound.bottom = y + cy;
1667     infoPtr->rcBound.right = x;
1668 
1669     btnPtr = infoPtr->buttons;
1670 
1671     TRACE("cy=%d\n", cy);
1672 
1673     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1674     {
1675         bWrap = FALSE;
1676         if (btnPtr->fsState & TBSTATE_HIDDEN)
1677         {
1678             SetRectEmpty (&btnPtr->rect);
1679             continue;
1680         }
1681 
1682         cy = infoPtr->nButtonHeight;
1683 
1684         if (btnPtr->fsStyle & BTNS_SEP) {
1685             if (infoPtr->dwStyle & CCS_VERT) {
1686                 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1687                 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nWidth;
1688             }
1689             else
1690                 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1691                     (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1692         }
1693         else
1694         {
1695             if (btnPtr->cx)
1696               cx = btnPtr->cx;
1697             else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || 
1698                 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1699             {
1700               SIZE sz;
1701               HDC hdc;
1702               HFONT hOldFont;
1703 
1704               hdc = GetDC (hwnd);
1705               hOldFont = SelectObject (hdc, infoPtr->hFont);
1706 
1707               TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1708 
1709               SelectObject (hdc, hOldFont);
1710               ReleaseDC (hwnd, hdc);
1711 
1712               sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1713                   TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1714                   validImageList);
1715               cx = sizeButton.cx;
1716             }
1717             else
1718               cx = infoPtr->nButtonWidth;
1719 
1720             /* if size has been set manually then don't add on extra space
1721              * for the drop down arrow */
1722             if (!btnPtr->cx && hasDropDownArrows && 
1723                 ((btnPtr->fsStyle & BTNS_DROPDOWN) || (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)))
1724               cx += DDARROW_WIDTH;
1725         }
1726         if (btnPtr->fsState & TBSTATE_WRAP )
1727                     bWrap = TRUE;
1728 
1729         SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1730 
1731         if (infoPtr->rcBound.left > x)
1732             infoPtr->rcBound.left = x;
1733         if (infoPtr->rcBound.right < x + cx)
1734             infoPtr->rcBound.right = x + cx;
1735         if (infoPtr->rcBound.bottom < y + cy)
1736             infoPtr->rcBound.bottom = y + cy;
1737 
1738         TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1739 
1740         /* btnPtr->nRow is zero based. The space between the rows is    */
1741         /* also considered as a row.                                    */
1742         btnPtr->nRow = nRows + nSepRows;
1743 
1744         TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1745               i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1746               x, y, x+cx, y+cy);
1747 
1748         if( bWrap )
1749         {
1750             if ( !(btnPtr->fsStyle & BTNS_SEP) )
1751                 y += cy;
1752             else
1753             {
1754                if ( !(infoPtr->dwStyle & CCS_VERT))
1755                     y += cy + ( (btnPtr->cx > 0 ) ?
1756                                 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1757                 else
1758                     y += cy;
1759 
1760                 /* nSepRows is used to calculate the extra height following  */
1761                 /* the last row.                                             */
1762                 nSepRows++;
1763             }
1764             x = infoPtr->nIndent;
1765 
1766             /* Increment row number unless this is the last button    */
1767             /* and it has Wrap set.                                   */
1768             if (i != infoPtr->nNumButtons-1)
1769                 nRows++;
1770         }
1771         else
1772             x += cx;
1773     }
1774 
1775     /* infoPtr->nRows is the number of rows on the toolbar */
1776     infoPtr->nRows = nRows + nSepRows + 1;
1777 
1778     TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1779 }
1780 
1781 
1782 static INT
1783 TOOLBAR_InternalHitTest (HWND hwnd, const POINT *lpPt)
1784 {
1785     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1786     TBUTTON_INFO *btnPtr;
1787     INT i;
1788 
1789     btnPtr = infoPtr->buttons;
1790     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1791         if (btnPtr->fsState & TBSTATE_HIDDEN)
1792             continue;
1793 
1794         if (btnPtr->fsStyle & BTNS_SEP) {
1795             if (PtInRect (&btnPtr->rect, *lpPt)) {
1796                 TRACE(" ON SEPARATOR %d!\n", i);
1797                 return -i;
1798             }
1799         }
1800         else {
1801             if (PtInRect (&btnPtr->rect, *lpPt)) {
1802                 TRACE(" ON BUTTON %d!\n", i);
1803                 return i;
1804             }
1805         }
1806     }
1807 
1808     TRACE(" NOWHERE!\n");
1809     return TOOLBAR_NOWHERE;
1810 }
1811 
1812 
1813 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1814 static BOOL
1815 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, TBBUTTON *lpTbb, BOOL fUnicode)
1816 {
1817     INT nOldButtons, nNewButtons, iButton;
1818     BOOL fHasString = FALSE;
1819 
1820     if (iIndex < 0)  /* iIndex can be negative, what means adding at the end */
1821         iIndex = infoPtr->nNumButtons;
1822 
1823     nOldButtons = infoPtr->nNumButtons;
1824     nNewButtons = nOldButtons + nAddButtons;
1825 
1826     infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1827     memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1828             (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1829     infoPtr->nNumButtons += nAddButtons;
1830 
1831     /* insert new buttons data */
1832     for (iButton = 0; iButton < nAddButtons; iButton++) {
1833         TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1834 
1835         TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1836 
1837         ZeroMemory(btnPtr, sizeof(*btnPtr));
1838 
1839         btnPtr->iBitmap   = lpTbb[iButton].iBitmap;
1840         btnPtr->idCommand = lpTbb[iButton].idCommand;
1841         btnPtr->fsState   = lpTbb[iButton].fsState;
1842         btnPtr->fsStyle   = lpTbb[iButton].fsStyle;
1843         btnPtr->dwData    = lpTbb[iButton].dwData;
1844         if(HIWORD(lpTbb[iButton].iString) && lpTbb[iButton].iString != -1)
1845         {
1846             if (fUnicode)
1847                 Str_SetPtrW((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[iButton].iString );
1848             else
1849                 Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[iButton].iString);
1850             fHasString = TRUE;
1851         }
1852         else
1853             btnPtr->iString   = lpTbb[iButton].iString;
1854 
1855         TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1856     }
1857 
1858     if (infoPtr->nNumStrings > 0 || fHasString)
1859         TOOLBAR_CalcToolbar(infoPtr->hwndSelf);
1860     else
1861         TOOLBAR_LayoutToolbar(infoPtr->hwndSelf);
1862     TOOLBAR_AutoSize(infoPtr->hwndSelf);
1863 
1864     TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1865     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1866     return TRUE;
1867 }
1868 
1869 
1870 static INT
1871 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1872 {
1873     TBUTTON_INFO *btnPtr;
1874     INT i;
1875 
1876     if (CommandIsIndex) {
1877         TRACE("command is really index command=%d\n", idCommand);
1878         if (idCommand >= infoPtr->nNumButtons) return -1;
1879         return idCommand;
1880     }
1881     btnPtr = infoPtr->buttons;
1882     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1883         if (btnPtr->idCommand == idCommand) {
1884             TRACE("command=%d index=%d\n", idCommand, i);
1885             return i;
1886         }
1887     }
1888     TRACE("no index found for command=%d\n", idCommand);
1889     return -1;
1890 }
1891 
1892 
1893 static INT
1894 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1895 {
1896     TBUTTON_INFO *btnPtr;
1897     INT nRunIndex;
1898 
1899     if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1900         return -1;
1901 
1902     /* check index button */
1903     btnPtr = &infoPtr->buttons[nIndex];
1904     if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1905         if (btnPtr->fsState & TBSTATE_CHECKED)
1906             return nIndex;
1907     }
1908 
1909     /* check previous buttons */
1910     nRunIndex = nIndex - 1;
1911     while (nRunIndex >= 0) {
1912         btnPtr = &infoPtr->buttons[nRunIndex];
1913         if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1914             if (btnPtr->fsState & TBSTATE_CHECKED)
1915                 return nRunIndex;
1916         }
1917         else
1918             break;
1919         nRunIndex--;
1920     }
1921 
1922     /* check next buttons */
1923     nRunIndex = nIndex + 1;
1924     while (nRunIndex < infoPtr->nNumButtons) {
1925         btnPtr = &infoPtr->buttons[nRunIndex];
1926         if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1927             if (btnPtr->fsState & TBSTATE_CHECKED)
1928                 return nRunIndex;
1929         }
1930         else
1931             break;
1932         nRunIndex++;
1933     }
1934 
1935     return -1;
1936 }
1937 
1938 
1939 static VOID
1940 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1941                     WPARAM wParam, LPARAM lParam)
1942 {
1943     MSG msg;
1944 
1945     msg.hwnd = hwndMsg;
1946     msg.message = uMsg;
1947     msg.wParam = wParam;
1948     msg.lParam = lParam;
1949     msg.time = GetMessageTime ();
1950     msg.pt.x = (short)LOWORD(GetMessagePos ());
1951     msg.pt.y = (short)HIWORD(GetMessagePos ());
1952 
1953     SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1954 }
1955 
1956 static void
1957 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1958 {
1959     if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1960         TTTOOLINFOW ti;
1961 
1962         ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1963         ti.cbSize   = sizeof (TTTOOLINFOW);
1964         ti.hwnd     = infoPtr->hwndSelf;
1965         ti.uId      = button->idCommand;
1966         ti.hinst    = 0;
1967         ti.lpszText = LPSTR_TEXTCALLBACKW;
1968         /* ti.lParam = random value from the stack? */
1969 
1970         SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1971             0, (LPARAM)&ti);
1972     }
1973 }
1974 
1975 static void
1976 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1977 {
1978     if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1979         TTTOOLINFOW ti;
1980 
1981         ZeroMemory(&ti, sizeof(ti));
1982         ti.cbSize   = sizeof(ti);
1983         ti.hwnd     = infoPtr->hwndSelf;
1984         ti.uId      = button->idCommand;
1985 
1986         SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
1987     }
1988 }
1989 
1990 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1991 {
1992     /* Set the toolTip only for non-hidden, non-separator button */
1993     if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
1994     {
1995         TTTOOLINFOW ti;
1996 
1997         ZeroMemory(&ti, sizeof(ti));
1998         ti.cbSize = sizeof(ti);
1999         ti.hwnd = infoPtr->hwndSelf;
2000         ti.uId = button->idCommand;
2001         ti.rect = button->rect;
2002         SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2003     }
2004 }
2005 
2006 /* Creates the tooltip control */
2007 static void
2008 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2009 {
2010     int i;
2011     NMTOOLTIPSCREATED nmttc;
2012 
2013     infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2014             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2015             infoPtr->hwndSelf, 0, 0, 0);
2016 
2017     if (!infoPtr->hwndToolTip)
2018         return;
2019 
2020     /* Send NM_TOOLTIPSCREATED notification */
2021     nmttc.hwndToolTips = infoPtr->hwndToolTip;
2022     TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2023 
2024     for (i = 0; i < infoPtr->nNumButtons; i++)
2025     {
2026         TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2027         TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2028     }
2029 }
2030 
2031 /* keeps available button list box sorted by button id */
2032 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2033 {
2034     int i;
2035     int count;
2036     PCUSTOMBUTTON btnInfo;
2037     HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2038 
2039     TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2040 
2041     count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2042 
2043     /* position 0 is always separator */
2044     for (i = 1; i < count; i++)
2045     {
2046         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2047         if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2048         {
2049             i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2050             SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2051             return;
2052         }
2053     }
2054     /* id higher than all others add to end */
2055     i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2056     SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2057 }
2058 
2059 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2060 {
2061     NMTOOLBARW nmtb;
2062 
2063         TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2064 
2065     if (nIndexFrom == nIndexTo)
2066         return;
2067 
2068     /* MSDN states that iItem is the index of the button, rather than the
2069      * command ID as used by every other NMTOOLBAR notification */
2070     nmtb.iItem = nIndexFrom;
2071     if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2072     {
2073         PCUSTOMBUTTON btnInfo;
2074         NMHDR hdr;
2075         HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2076         int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2077 
2078         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2079 
2080         SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2081         SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2082         SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2083         SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2084 
2085         if (nIndexTo <= 0)
2086             EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2087         else
2088             EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2089 
2090         /* last item is always separator, so -2 instead of -1 */
2091         if (nIndexTo >= (count - 2))
2092             EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2093         else
2094             EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2095 
2096         SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2097         SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2098 
2099         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2100     }
2101 }
2102 
2103 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2104 {
2105     NMTOOLBARW nmtb;
2106 
2107     TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2108 
2109     /* MSDN states that iItem is the index of the button, rather than the
2110      * command ID as used by every other NMTOOLBAR notification */
2111     nmtb.iItem = nIndexAvail;
2112     if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2113     {
2114         PCUSTOMBUTTON btnInfo;
2115         NMHDR hdr;
2116         HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2117         HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2118         int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2119 
2120         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2121 
2122         if (nIndexAvail != 0) /* index == 0 indicates separator */
2123         {
2124             /* remove from 'available buttons' list */
2125             SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2126             if (nIndexAvail == count-1)
2127                 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2128             else
2129                 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2130         }
2131         else
2132         {
2133             PCUSTOMBUTTON btnNew;
2134 
2135             /* duplicate 'separator' button */
2136             btnNew = Alloc(sizeof(CUSTOMBUTTON));
2137             *btnNew = *btnInfo;
2138             btnInfo = btnNew;
2139         }
2140 
2141         /* insert into 'toolbar button' list */
2142         SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2143         SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2144 
2145         SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2146 
2147         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2148     }
2149 }
2150 
2151 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2152 {
2153     PCUSTOMBUTTON btnInfo;
2154     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2155 
2156     TRACE("Remove: index %d\n", index);
2157 
2158     btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2159 
2160     /* send TBN_QUERYDELETE notification */
2161     if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2162     {
2163         NMHDR hdr;
2164 
2165         SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2166         SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2167 
2168         SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2169 
2170         /* insert into 'available button' list */
2171         if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2172             TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2173         else
2174             Free(btnInfo);
2175 
2176         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2177     }
2178 }
2179 
2180 /* drag list notification function for toolbar buttons list box */
2181 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2182                                                         const DRAGLISTINFO *pDLI)
2183 {
2184     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2185     switch (pDLI->uNotification)
2186     {
2187     case DL_BEGINDRAG:
2188     {
2189         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2190         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2191         /* no dragging for last item (separator) */
2192         if (nCurrentItem >= (nCount - 1)) return FALSE;
2193         return TRUE;
2194     }
2195     case DL_DRAGGING:
2196     {
2197         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2198         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2199         /* no dragging past last item (separator) */
2200         if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2201         {
2202             DrawInsert(hwnd, hwndList, nCurrentItem);
2203             /* FIXME: native uses "move button" cursor */
2204             return DL_COPYCURSOR;
2205         }
2206 
2207         /* not over toolbar buttons list */
2208         if (nCurrentItem < 0)
2209         {
2210             POINT ptWindow = pDLI->ptCursor;
2211             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2212             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2213             /* over available buttons list? */
2214             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2215                 /* FIXME: native uses "move button" cursor */
2216                 return DL_COPYCURSOR;
2217         }
2218         /* clear drag arrow */
2219         DrawInsert(hwnd, hwndList, -1);
2220         return DL_STOPCURSOR;
2221     }
2222     case DL_DROPPED:
2223     {
2224         INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2225         INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2226         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2227         if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2228         {
2229             /* clear drag arrow */
2230             DrawInsert(hwnd, hwndList, -1);
2231             /* move item */
2232             TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2233         }
2234         /* not over toolbar buttons list */
2235         if (nIndexTo < 0)
2236         {
2237             POINT ptWindow = pDLI->ptCursor;
2238             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2239             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2240             /* over available buttons list? */
2241             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2242                 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2243         }
2244         break;
2245     }
2246     case DL_CANCELDRAG:
2247         /* Clear drag arrow */
2248         DrawInsert(hwnd, hwndList, -1);
2249         break;
2250     }
2251 
2252     return 0;
2253 }
2254 
2255 /* drag list notification function for available buttons list box */
2256 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2257                                                       const DRAGLISTINFO *pDLI)
2258 {
2259     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2260     switch (pDLI->uNotification)
2261     {
2262     case DL_BEGINDRAG:
2263         return TRUE;
2264     case DL_DRAGGING:
2265     {
2266         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2267         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2268         /* no dragging past last item (separator) */
2269         if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2270         {
2271             DrawInsert(hwnd, hwndList, nCurrentItem);
2272             /* FIXME: native uses "move button" cursor */
2273             return DL_COPYCURSOR;
2274         }
2275 
2276         /* not over toolbar buttons list */
2277         if (nCurrentItem < 0)
2278         {
2279             POINT ptWindow = pDLI->ptCursor;
2280             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2281             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2282             /* over available buttons list? */
2283             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2284                 /* FIXME: native uses "move button" cursor */
2285                 return DL_COPYCURSOR;
2286         }
2287         /* clear drag arrow */
2288         DrawInsert(hwnd, hwndList, -1);
2289         return DL_STOPCURSOR;
2290     }
2291     case DL_DROPPED:
2292     {
2293         INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2294         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2295         INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2296         if ((nIndexTo >= 0) && (nIndexTo < nCount))
2297         {
2298             /* clear drag arrow */
2299             DrawInsert(hwnd, hwndList, -1);
2300             /* add item */
2301             TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2302         }
2303     }
2304     case DL_CANCELDRAG:
2305         /* Clear drag arrow */
2306         DrawInsert(hwnd, hwndList, -1);
2307         break;
2308     }
2309     return 0;
2310 }
2311 
2312 extern UINT uDragListMessage;
2313 
2314 /***********************************************************************
2315  * TOOLBAR_CustomizeDialogProc
2316  * This function implements the toolbar customization dialog.
2317  */
2318 static INT_PTR CALLBACK
2319 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2320 {
2321     PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2322     PCUSTOMBUTTON btnInfo;
2323     NMTOOLBARA nmtb;
2324     TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2325 
2326     switch (uMsg)
2327     {
2328         case WM_INITDIALOG:
2329             custInfo = (PCUSTDLG_INFO)lParam;
2330             SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2331 
2332             if (custInfo)
2333             {
2334                 WCHAR Buffer[256];
2335                 int i = 0;
2336                 int index;
2337                 NMTBINITCUSTOMIZE nmtbic;
2338 
2339                 infoPtr = custInfo->tbInfo;
2340 
2341                 /* send TBN_QUERYINSERT notification */
2342                 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2343 
2344                 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2345                     return FALSE;
2346 
2347                 nmtbic.hwndDialog = hwnd;
2348                 /* Send TBN_INITCUSTOMIZE notification */
2349                 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2350                     TBNRF_HIDEHELP)
2351                 {
2352                     TRACE("TBNRF_HIDEHELP requested\n");
2353                     ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2354                 }
2355 
2356                 /* add items to 'toolbar buttons' list and check if removable */
2357                 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2358                 {
2359                     btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2360                     memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2361                     btnInfo->btn.fsStyle = BTNS_SEP;
2362                     btnInfo->bVirtual = FALSE;
2363                     LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2364 
2365                     /* send TBN_QUERYDELETE notification */
2366                     btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2367 
2368                     index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2369                     SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2370                 }
2371 
2372                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2373 
2374                 /* insert separator button into 'available buttons' list */
2375                 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2376                 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2377                 btnInfo->btn.fsStyle = BTNS_SEP;
2378                 btnInfo->bVirtual = FALSE;
2379                 btnInfo->bRemovable = TRUE;
2380                 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2381                 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2382                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2383 
2384                 /* insert all buttons into dsa */
2385                 for (i = 0;; i++)
2386                 {
2387                     /* send TBN_GETBUTTONINFO notification */
2388                     NMTOOLBARW nmtb;
2389                     nmtb.iItem = i;
2390                     nmtb.pszText = Buffer;
2391                     nmtb.cchText = 256;
2392 
2393                     /* Clear previous button's text */
2394                     ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2395 
2396                     if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2397                         break;
2398 
2399                     TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2400                         nmtb.tbButton.fsStyle, i, 
2401                         nmtb.tbButton.idCommand,
2402                         nmtb.tbButton.iString,
2403                         nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2404                         : "");
2405 
2406                     /* insert button into the apropriate list */
2407                     index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2408                     if (index == -1)
2409                     {
2410                         btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2411                         btnInfo->bVirtual = FALSE;
2412                         btnInfo->bRemovable = TRUE;
2413                     }
2414                     else
2415                     {
2416                         btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, 
2417                             IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2418                     }
2419 
2420                     btnInfo->btn = nmtb.tbButton;
2421                     if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2422                     {
2423                         if (lstrlenW(nmtb.pszText))
2424                             lstrcpyW(btnInfo->text, nmtb.pszText);
2425                         else if (nmtb.tbButton.iString >= 0 && 
2426                             nmtb.tbButton.iString < infoPtr->nNumStrings)
2427                         {
2428                             lstrcpyW(btnInfo->text, 
2429                                 infoPtr->strings[nmtb.tbButton.iString]);
2430                         }
2431                     }
2432 
2433                     if (index == -1)
2434                         TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2435                 }
2436 
2437                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2438 
2439                 /* select first item in the 'available' list */
2440                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2441 
2442                 /* append 'virtual' separator button to the 'toolbar buttons' list */
2443                 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2444                 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2445                 btnInfo->btn.fsStyle = BTNS_SEP;
2446                 btnInfo->bVirtual = TRUE;
2447                 btnInfo->bRemovable = FALSE;
2448                 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2449                 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2450                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2451 
2452                 /* select last item in the 'toolbar' list */
2453                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2454                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2455 
2456                 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2457                 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2458 
2459                 /* set focus and disable buttons */
2460                 PostMessageW (hwnd, WM_USER, 0, 0);
2461             }
2462             return TRUE;
2463 
2464         case WM_USER:
2465             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2466             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2467             EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2468             SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2469             return TRUE;
2470 
2471         case WM_CLOSE:
2472             EndDialog(hwnd, FALSE);
2473             return TRUE;
2474 
2475         case WM_COMMAND:
2476             switch (LOWORD(wParam))
2477             {
2478                 case IDC_TOOLBARBTN_LBOX:
2479                     if (HIWORD(wParam) == LBN_SELCHANGE)
2480                     {
2481                         PCUSTOMBUTTON btnInfo;
2482                         NMTOOLBARA nmtb;
2483                         int count;
2484                         int index;
2485 
2486                         count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2487                         index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2488 
2489                         /* send TBN_QUERYINSERT notification */
2490                         nmtb.iItem = index;
2491                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2492 
2493                         /* get list box item */
2494                         btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2495 
2496                         if (index == (count - 1))
2497                         {
2498                             /* last item (virtual separator) */
2499                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2500                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2501                         }
2502                         else if (index == (count - 2))
2503                         {
2504                             /* second last item (last non-virtual item) */
2505                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2506                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2507                         }
2508                         else if (index == 0)
2509                         {
2510                             /* first item */
2511                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2512                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2513                         }
2514                         else
2515                         {
2516                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2517                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2518                         }
2519 
2520                         EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2521                     }
2522                     break;
2523 
2524                 case IDC_MOVEUP_BTN:
2525                     {
2526                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2527                         TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2528                     }
2529                     break;
2530 
2531                 case IDC_MOVEDN_BTN: /* move down */
2532                     {
2533                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2534                         TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2535                     }
2536                     break;
2537 
2538                 case IDC_REMOVE_BTN: /* remove button */
2539                     {
2540                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2541 
2542                         if (LB_ERR == index)
2543                                 break;
2544 
2545                         TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2546                     }
2547                     break;
2548                 case IDC_HELP_BTN:
2549                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2550                         break;
2551                 case IDC_RESET_BTN:
2552                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2553                         break;
2554 
2555                 case IDOK: /* Add button */
2556                     {
2557                         int index;
2558                         int indexto;
2559 
2560                         index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2561                         indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2562 
2563                         TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2564                     }
2565                     break;
2566 
2567                 case IDCANCEL:
2568                     EndDialog(hwnd, FALSE);
2569                     break;
2570             }
2571             return TRUE;
2572 
2573         case WM_DESTROY:
2574             {
2575                 int count;
2576                 int i;
2577 
2578                 /* delete items from 'toolbar buttons' listbox*/
2579                 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2580                 for (i = 0; i < count; i++)
2581                 {
2582                     btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2583                     Free(btnInfo);
2584                     SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2585                 }
2586                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2587 
2588 
2589                 /* delete items from 'available buttons' listbox*/
2590                 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2591                 for (i = 0; i < count; i++)
2592                 {
2593                     btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2594                     Free(btnInfo);
2595                     SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2596                 }
2597                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2598             }
2599             return TRUE;
2600 
2601         case WM_DRAWITEM:
2602             if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2603             {
2604                 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2605                 RECT rcButton;
2606                 RECT rcText;
2607                 HPEN hPen, hOldPen;
2608                 HBRUSH hOldBrush;
2609                 COLORREF oldText = 0;
2610                 COLORREF oldBk = 0;
2611 
2612                 /* get item data */
2613                 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, (WPARAM)lpdis->itemID, 0);
2614                 if (btnInfo == NULL)
2615                 {
2616                     FIXME("btnInfo invalid!\n");
2617                     return TRUE;
2618                 }
2619 
2620                 /* set colors and select objects */
2621                 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2622                 if (btnInfo->bVirtual)
2623                    oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2624                 else
2625                    oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2626                 hPen = CreatePen( PS_SOLID, 1,
2627                      GetSysColor( (lpdis->itemState & ODS_SELECTED)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2628                 hOldPen = SelectObject (lpdis->hDC, hPen );
2629                 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2630 
2631                 /* fill background rectangle */
2632                 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2633                            lpdis->rcItem.right, lpdis->rcItem.bottom);
2634 
2635                 /* calculate button and text rectangles */
2636                 CopyRect (&rcButton, &lpdis->rcItem);
2637                 InflateRect (&rcButton, -1, -1);
2638                 CopyRect (&rcText, &rcButton);
2639                 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2640                 rcText.left = rcButton.right + 2;
2641 
2642                 /* draw focus rectangle */
2643                 if (lpdis->itemState & ODS_FOCUS)
2644                     DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2645 
2646                 /* draw button */
2647                 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2648                     DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2649 
2650                 /* draw image and text */
2651                 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2652                         HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, 
2653                                 btnInfo->btn.iBitmap));
2654                     ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap), 
2655                                 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2656                 }
2657                 DrawTextW (lpdis->hDC,  btnInfo->text, -1, &rcText,
2658                                DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2659 
2660                 /* delete objects and reset colors */
2661                 SelectObject (lpdis->hDC, hOldBrush);
2662                 SelectObject (lpdis->hDC, hOldPen);
2663                 SetBkColor (lpdis->hDC, oldBk);
2664                 SetTextColor (lpdis->hDC, oldText);
2665                 DeleteObject( hPen );
2666                 return TRUE;
2667             }
2668             return FALSE;
2669 
2670         case WM_MEASUREITEM:
2671             if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2672             {
2673                 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2674 
2675                 lpmis->itemHeight = 15 + 8; /* default height */
2676 
2677                 return TRUE;
2678             }
2679             return FALSE;
2680 
2681         default:
2682             if (uDragListMessage && (uMsg == uDragListMessage))
2683             {
2684                 if (wParam == IDC_TOOLBARBTN_LBOX)
2685                 {
2686                     LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2687                         custInfo, hwnd, (DRAGLISTINFO *)lParam);
2688                     SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2689                     return TRUE;
2690                 }
2691                 else if (wParam == IDC_AVAILBTN_LBOX)
2692                 {
2693                     LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2694                         custInfo, hwnd, (DRAGLISTINFO *)lParam);
2695                     SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2696                     return TRUE;
2697                 }
2698             }
2699             return FALSE;
2700     }
2701 }
2702 
2703 static BOOL
2704 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2705 {
2706     HBITMAP hbmLoad;
2707     INT nCountBefore = ImageList_GetImageCount(himlDef);
2708     INT nCountAfter;
2709     INT cxIcon, cyIcon;
2710     INT nAdded;
2711     INT nIndex;
2712 
2713     TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2714     /* Add bitmaps to the default image list */
2715     if (bitmap->hInst == NULL)         /* a handle was passed */
2716         hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2717     else
2718         hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2719 
2720     /* enlarge the bitmap if needed */
2721     ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2722     if (bitmap->hInst != COMCTL32_hModule)
2723         COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2724     
2725     nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2726     DeleteObject(hbmLoad);
2727     if (nIndex == -1)
2728         return FALSE;
2729     
2730     nCountAfter = ImageList_GetImageCount(himlDef);
2731     nAdded =  nCountAfter - nCountBefore;
2732     if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2733     {
2734         ImageList_SetImageCount(himlDef, nCountBefore + 1);
2735     } else if (nAdded > (INT)bitmap->nButtons) {
2736         TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2737             nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2738     }
2739 
2740     infoPtr->nNumBitmaps += nAdded;
2741     return TRUE;
2742 }
2743 
2744 static void
2745 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2746 {
2747     HIMAGELIST himlDef;
2748     HIMAGELIST himlNew;
2749     INT cx, cy;
2750     INT i;
2751     
2752     himlDef = GETDEFIMAGELIST(infoPtr, 0);
2753     if (himlDef == NULL || himlDef != infoPtr->himlInt)
2754         return;
2755     if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2756         return;
2757     if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2758         return;
2759 
2760     TRACE("Update icon size: %dx%d -> %dx%d\n",
2761         cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2762 
2763     himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2764                                 ILC_COLORDDB|ILC_MASK, 8, 2);
2765     for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2766         TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2767     TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2768     infoPtr->himlInt = himlNew;
2769 
2770     infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2771     ImageList_Destroy(himlDef);
2772 }
2773 
2774 /***********************************************************************
2775  * TOOLBAR_AddBitmap:  Add the bitmaps to the default image list.
2776  *
2777  */
2778 static LRESULT
2779 TOOLBAR_AddBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
2780 {
2781     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2782     LPTBADDBITMAP lpAddBmp = (LPTBADDBITMAP)lParam;
2783     TBITMAP_INFO info;
2784     INT iSumButtons, i;
2785     HIMAGELIST himlDef;
2786 
2787     TRACE("hwnd=%p wParam=%lx lParam=%lx\n", hwnd, wParam, lParam);
2788     if (!lpAddBmp)
2789         return -1;
2790 
2791     if (lpAddBmp->hInst == HINST_COMMCTRL)
2792     {
2793         info.hInst = COMCTL32_hModule;
2794         switch (lpAddBmp->nID)
2795         {
2796             case IDB_STD_SMALL_COLOR:
2797                 info.nButtons = 15;
2798                 info.nID = IDB_STD_SMALL;
2799                 break;
2800             case IDB_STD_LARGE_COLOR:
2801                 info.nButtons = 15;
2802                 info.nID = IDB_STD_LARGE;
2803                 break;
2804             case IDB_VIEW_SMALL_COLOR:
2805                 info.nButtons = 12;
2806                 info.nID = IDB_VIEW_SMALL;
2807                 break;
2808             case IDB_VIEW_LARGE_COLOR:
2809                 info.nButtons = 12;
2810                 info.nID = IDB_VIEW_LARGE;
2811                 break;
2812             case IDB_HIST_SMALL_COLOR:
2813                 info.nButtons = 5;
2814                 info.nID = IDB_HIST_SMALL;
2815                 break;
2816             case IDB_HIST_LARGE_COLOR:
2817                 info.nButtons = 5;
2818                 info.nID = IDB_HIST_LARGE;
2819                 break;
2820             default:
2821                 return -1;
2822         }
2823 
2824         TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2825 
2826         /* Windows resize all the buttons to the size of a newly added standard image */
2827         if (lpAddBmp->nID & 1)
2828         {
2829             /* large icons: 24x24. Will make the button 31x30 */
2830             SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2831         }
2832         else
2833         {
2834             /* small icons: 16x16. Will make the buttons 23x22 */
2835             SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2836         }
2837 
2838         TOOLBAR_CalcToolbar (hwnd);
2839     }
2840     else
2841     {
2842         info.nButtons = (INT)wParam;
2843         info.hInst = lpAddBmp->hInst;
2844         info.nID = lpAddBmp->nID;
2845         TRACE("adding %d bitmaps!\n", info.nButtons);
2846     }
2847     
2848     /* check if the bitmap is already loaded and compute iSumButtons */
2849     iSumButtons = 0;
2850     for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2851     {
2852         if (infoPtr->bitmaps[i].hInst == info.hInst &&
2853             infoPtr->bitmaps[i].nID == info.nID)
2854             return iSumButtons;
2855         iSumButtons += infoPtr->bitmaps[i].nButtons;
2856     }
2857 
2858     if (!infoPtr->cimlDef) {
2859         /* create new default image list */
2860         TRACE ("creating default image list!\n");
2861 
2862         himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2863                                     ILC_COLORDDB | ILC_MASK, info.nButtons, 2);
2864         TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2865         infoPtr->himlInt = himlDef;
2866     }
2867     else {
2868         himlDef = GETDEFIMAGELIST(infoPtr, 0);
2869     }
2870 
2871     if (!himlDef) {
2872         WARN("No default image list available\n");
2873         return -1;
2874     }
2875 
2876     if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2877         return -1;
2878 
2879     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2880     infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2881     infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2882     infoPtr->nNumBitmapInfos++;
2883     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2884 
2885     InvalidateRect(hwnd, NULL, TRUE);
2886     return iSumButtons;
2887 }
2888 
2889 
2890 static LRESULT
2891 TOOLBAR_AddButtonsT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
2892 {
2893     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2894     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2895     INT nAddButtons = (UINT)wParam;
2896 
2897     TRACE("adding %ld buttons (unicode=%d)!\n", wParam, fUnicode);
2898 
2899     return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2900 }
2901 
2902 
2903 static LRESULT
2904 TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2905 {
2906 #define MAX_RESOURCE_STRING_LENGTH 512
2907     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2908     BOOL fFirstString = (infoPtr->nNumStrings == 0);
2909     INT nIndex = infoPtr->nNumStrings;
2910 
2911     if ((wParam) && (HIWORD(lParam) == 0)) {
2912         WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2913         WCHAR delimiter;
2914         WCHAR *next_delim;
2915         WCHAR *p;
2916         INT len;
2917         TRACE("adding string from resource!\n");
2918 
2919         len = LoadStringW ((HINSTANCE)wParam, (UINT)lParam,
2920                              szString, MAX_RESOURCE_STRING_LENGTH);
2921 
2922         TRACE("len=%d %s\n", len, debugstr_w(szString));
2923         if (len == 0 || len == 1)
2924             return nIndex;
2925 
2926         TRACE("Delimiter: 0x%x\n", *szString);
2927         delimiter = *szString;
2928         p = szString + 1;
2929 
2930         while ((next_delim = strchrW(p, delimiter)) != NULL) {
2931             *next_delim = 0;
2932             if (next_delim + 1 >= szString + len)
2933             {
2934                 /* this may happen if delimiter == '\0' or if the last char is a
2935                  * delimiter (then it is ignored like the native does) */
2936                 break;
2937             }
2938 
2939             infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2940             Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2941             infoPtr->nNumStrings++;
2942 
2943             p = next_delim + 1;
2944         }
2945     }
2946     else {
2947         LPWSTR p = (LPWSTR)lParam;
2948         INT len;
2949 
2950         if (p == NULL)
2951             return -1;
2952         TRACE("adding string(s) from array!\n");
2953         while (*p) {
2954             len = strlenW (p);
2955 
2956             TRACE("len=%d %s\n", len, debugstr_w(p));
2957             infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2958             Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2959             infoPtr->nNumStrings++;
2960 
2961             p += (len+1);
2962         }
2963     }
2964 
2965     if (fFirstString)
2966         TOOLBAR_CalcToolbar(hwnd);
2967     return nIndex;
2968 }
2969 
2970 
2971 static LRESULT
2972 TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2973 {
2974     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2975     BOOL fFirstString = (infoPtr->nNumStrings == 0);
2976     LPSTR p;
2977     INT nIndex;
2978     INT len;
2979 
2980     if ((wParam) && (HIWORD(lParam) == 0))  /* load from resources */
2981         return TOOLBAR_AddStringW(hwnd, wParam, lParam);
2982 
2983     p = (LPSTR)lParam;
2984     if (p == NULL)
2985         return -1;
2986 
2987     TRACE("adding string(s) from array!\n");
2988     nIndex = infoPtr->nNumStrings;
2989     while (*p) {
2990         len = strlen (p);
2991         TRACE("len=%d \"%s\"\n", len, p);
2992 
2993         infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2994         Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
2995         infoPtr->nNumStrings++;
2996 
2997         p += (len+1);
2998     }
2999 
3000     if (fFirstString)
3001         TOOLBAR_CalcToolbar(hwnd);
3002     return nIndex;
3003 }
3004 
3005 
3006 static LRESULT
3007 TOOLBAR_AutoSize (HWND hwnd)
3008 {
3009     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3010     RECT parent_rect;
3011     HWND parent;
3012     INT  x, y;
3013     INT  cx, cy;
3014 
3015     TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3016 
3017     parent = GetParent (hwnd);
3018 
3019     if (!parent || !infoPtr->bDoRedraw)
3020         return 0;
3021 
3022     GetClientRect(parent, &parent_rect);
3023 
3024     x = parent_rect.left;
3025     y = parent_rect.top;
3026 
3027     TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3028 
3029     cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3030     cx = parent_rect.right - parent_rect.left;
3031 
3032     if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1))
3033     {
3034         TOOLBAR_LayoutToolbar(hwnd);
3035         InvalidateRect( hwnd, NULL, TRUE );
3036     }
3037 
3038     if (!(infoPtr->dwStyle & CCS_NORESIZE))
3039     {
3040         RECT window_rect;
3041         UINT uPosFlags = SWP_NOZORDER;
3042 
3043         if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3044         {
3045             GetWindowRect(hwnd, &window_rect);
3046             ScreenToClient(parent, (LPPOINT)&window_rect.left);
3047             y = window_rect.top;
3048         }
3049         if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3050         {
3051             GetWindowRect(hwnd, &window_rect);
3052             y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3053         }
3054 
3055         if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3056             uPosFlags |= SWP_NOMOVE;
3057     
3058         if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3059             cy += GetSystemMetrics(SM_CYEDGE);
3060 
3061         if (infoPtr->dwStyle & WS_BORDER)
3062         {
3063             x = y = 1; /* FIXME: this looks wrong */
3064             cy += GetSystemMetrics(SM_CYEDGE);
3065             cx += GetSystemMetrics(SM_CXEDGE);
3066         }
3067 
3068         SetWindowPos(hwnd, NULL, x, y, cx, cy, uPosFlags);
3069     }
3070 
3071     return 0;
3072 }
3073 
3074 
3075 static LRESULT
3076 TOOLBAR_ButtonCount (HWND hwnd)
3077 {
3078     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3079 
3080     return infoPtr->nNumButtons;
3081 }
3082 
3083 
3084 static LRESULT
3085 TOOLBAR_ButtonStructSize (HWND hwnd, WPARAM wParam)
3086 {
3087     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3088 
3089     infoPtr->dwStructSize = (DWORD)wParam;
3090 
3091     return 0;
3092 }
3093 
3094 
3095 static LRESULT
3096 TOOLBAR_ChangeBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3097 {
3098     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3099     TBUTTON_INFO *btnPtr;
3100     INT nIndex;
3101 
3102     TRACE("button %ld, iBitmap now %d\n", wParam, LOWORD(lParam));
3103 
3104     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3105     if (nIndex == -1)
3106         return FALSE;
3107 
3108     btnPtr = &infoPtr->buttons[nIndex];
3109     btnPtr->iBitmap = LOWORD(lParam);
3110 
3111     /* we HAVE to erase the background, the new bitmap could be */
3112     /* transparent */
3113     InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3114 
3115     return TRUE;
3116 }
3117 
3118 
3119 static LRESULT
3120 TOOLBAR_CheckButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3121 {
3122     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3123     TBUTTON_INFO *btnPtr;
3124     INT nIndex;
3125     INT nOldIndex = -1;
3126     BOOL bChecked = FALSE;
3127 
3128     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3129 
3130     TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", hwnd, nIndex, lParam);
3131 
3132     if (nIndex == -1)
3133         return FALSE;
3134 
3135     btnPtr = &infoPtr->buttons[nIndex];
3136 
3137     bChecked = (btnPtr->fsState & TBSTATE_CHECKED) ? TRUE : FALSE;
3138 
3139     if (LOWORD(lParam) == FALSE)
3140         btnPtr->fsState &= ~TBSTATE_CHECKED;
3141     else {
3142         if (btnPtr->fsStyle & BTNS_GROUP) {
3143             nOldIndex =
3144                 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3145             if (nOldIndex == nIndex)
3146                 return 0;
3147             if (nOldIndex != -1)
3148                 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3149         }
3150         btnPtr->fsState |= TBSTATE_CHECKED;
3151     }
3152 
3153     if( bChecked != LOWORD(lParam) )
3154     {
3155         if (nOldIndex != -1)
3156             InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect, TRUE);
3157         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3158     }
3159 
3160     /* FIXME: Send a WM_NOTIFY?? */
3161 
3162     return TRUE;
3163 }
3164 
3165 
3166 static LRESULT
3167 TOOLBAR_CommandToIndex (HWND hwnd, WPARAM wParam)
3168 {
3169     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3170 
3171     return TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3172 }
3173 
3174 
3175 static LRESULT
3176 TOOLBAR_Customize (HWND hwnd)
3177 {
3178     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3179     CUSTDLG_INFO custInfo;
3180     LRESULT ret;
3181     LPCVOID template;
3182     HRSRC hRes;
3183     NMHDR nmhdr;
3184 
3185     custInfo.tbInfo = infoPtr;
3186     custInfo.tbHwnd = hwnd;
3187 
3188     /* send TBN_BEGINADJUST notification */
3189     TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3190 
3191     if (!(hRes = FindResourceW (COMCTL32_hModule,
3192                                 MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3193                                 (LPWSTR)RT_DIALOG)))
3194         return FALSE;
3195 
3196     if(!(template = LoadResource (COMCTL32_hModule, hRes)))
3197         return FALSE;
3198 
3199     ret = DialogBoxIndirectParamW ((HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE),
3200                                    template, hwnd, TOOLBAR_CustomizeDialogProc,
3201                                    (LPARAM)&custInfo);
3202 
3203     /* send TBN_ENDADJUST notification */
3204     TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3205 
3206     return ret;
3207 }
3208 
3209 
3210 static LRESULT
3211 TOOLBAR_DeleteButton (HWND hwnd, WPARAM wParam)
3212 {
3213     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3214     INT nIndex = (INT)wParam;
3215     NMTOOLBARW nmtb;
3216     TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3217 
3218     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3219         return FALSE;
3220 
3221     memset(&nmtb, 0, sizeof(nmtb));
3222     nmtb.iItem = btnPtr->idCommand;
3223     nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3224     nmtb.tbButton.idCommand = btnPtr->idCommand;
3225     nmtb.tbButton.fsState = btnPtr->fsState;
3226     nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3227     nmtb.tbButton.dwData = btnPtr->dwData;
3228     nmtb.tbButton.iString = btnPtr->iString;
3229     TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3230 
3231     TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3232 
3233     if (infoPtr->nNumButtons == 1) {
3234         TRACE(" simple delete!\n");
3235         Free (infoPtr->buttons);
3236         infoPtr->buttons = NULL;
3237         infoPtr->nNumButtons = 0;
3238     }
3239     else {
3240         TBUTTON_INFO *oldButtons = infoPtr->buttons;
3241         TRACE("complex delete! [nIndex=%d]\n", nIndex);
3242 
3243         infoPtr->nNumButtons--;
3244         infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3245         if (nIndex > 0) {
3246             memcpy (&infoPtr->buttons[0], &oldButtons[0],
3247                     nIndex * sizeof(TBUTTON_INFO));
3248         }
3249 
3250         if (nIndex < infoPtr->nNumButtons) {
3251             memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3252                     (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3253         }
3254 
3255         Free (oldButtons);
3256     }
3257 
3258     TOOLBAR_LayoutToolbar(hwnd);
3259 
3260     InvalidateRect (hwnd, NULL, TRUE);
3261 
3262     return TRUE;
3263 }
3264 
3265 
3266 static LRESULT
3267 TOOLBAR_EnableButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3268 {
3269     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3270     TBUTTON_INFO *btnPtr;
3271     INT nIndex;
3272     DWORD bState;
3273 
3274     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3275 
3276     TRACE("hwnd=%p, btn index=%ld, lParam=0x%08lx\n", hwnd, wParam, lParam);
3277 
3278     if (nIndex == -1)
3279         return FALSE;
3280 
3281     btnPtr = &infoPtr->buttons[nIndex];
3282 
3283     bState = btnPtr->fsState & TBSTATE_ENABLED;
3284 
3285     /* update the toolbar button state */
3286     if(LOWORD(lParam) == FALSE) {
3287         btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3288     } else {
3289         btnPtr->fsState |= TBSTATE_ENABLED;
3290     }
3291 
3292     /* redraw the button only if the state of the button changed */
3293     if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3294         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3295 
3296     return TRUE;
3297 }
3298 
3299 
3300 static inline LRESULT
3301 TOOLBAR_GetAnchorHighlight (HWND hwnd)
3302 {
3303     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3304 
3305     return infoPtr->bAnchor;
3306 }
3307 
3308 
3309 static LRESULT
3310 TOOLBAR_GetBitmap (HWND hwnd, WPARAM wParam)
3311 {
3312     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3313     INT nIndex;
3314 
3315     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3316     if (nIndex == -1)
3317         return -1;
3318 
3319     return infoPtr->buttons[nIndex].iBitmap;
3320 }
3321 
3322 
3323 static inline LRESULT
3324 TOOLBAR_GetBitmapFlags (void)
3325 {
3326     return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3327 }
3328 
3329 
3330 static LRESULT
3331 TOOLBAR_GetButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3332 {
3333     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3334     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3335     INT nIndex = (INT)wParam;
3