~ [ 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_DrawDDFlatSeparator
496 *
497 * This function draws the separator that was flagged as BTNS_DROPDOWN.
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_DrawDDFlatSeparator (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 (btnPtr->fsStyle & BTNS_DROPDOWN)
859                 TOOLBAR_DrawDDFlatSeparator (&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         /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1330         /* it is the actual width of the separator. This is used for */
1331         /* custom controls in toolbars.                              */
1332         /*                                                           */
1333         /* BTNS_DROPDOWN separators are treated as buttons for    */
1334         /* width.  - GA 8/01                                         */
1335         if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1336             !(btnPtr[i].fsStyle & BTNS_DROPDOWN))
1337             cx = (btnPtr[i].iBitmap > 0) ?
1338                         btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1339         else
1340             cx = (btnPtr[i].cx) ? btnPtr[i].cx : infoPtr->nButtonWidth;
1341 
1342         /* Two or more adjacent separators form a separator group.   */
1343         /* The first separator in a group should be wrapped to the   */
1344         /* next row if the previous wrapping is on a button.         */
1345         if( bButtonWrap &&
1346                 (btnPtr[i].fsStyle & BTNS_SEP) &&
1347                 (i + 1 < infoPtr->nNumButtons ) &&
1348                 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1349         {
1350             TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1351             btnPtr[i].fsState |= TBSTATE_WRAP;
1352             x = infoPtr->nIndent;
1353             i++;
1354             bButtonWrap = FALSE;
1355             continue;
1356         }
1357 
1358         /* The layout makes sure the bitmap is visible, but not the button. */
1359         /* Test added to also wrap after a button that starts a row but     */
1360         /* is bigger than the area.  - GA  8/01                             */
1361         if (( x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2
1362            > infoPtr->nWidth ) ||
1363             ((x == infoPtr->nIndent) && (cx > infoPtr->nWidth)))
1364         {
1365             BOOL bFound = FALSE;
1366 
1367             /*  If the current button is a separator and not hidden,  */
1368             /*  go to the next until it reaches a non separator.      */
1369             /*  Wrap the last separator if it is before a button.     */
1370             while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1371                       !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1372                      (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1373                         i < infoPtr->nNumButtons )
1374             {
1375                 i++;
1376                 bFound = TRUE;
1377             }
1378 
1379             if( bFound && i < infoPtr->nNumButtons )
1380             {
1381                 i--;
1382                 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1383                       i, btnPtr[i].fsStyle, x, cx);
1384                 btnPtr[i].fsState |= TBSTATE_WRAP;
1385                 x = infoPtr->nIndent;
1386                 bButtonWrap = FALSE;
1387                 continue;
1388             }
1389             else if ( i >= infoPtr->nNumButtons)
1390                 break;
1391 
1392             /*  If the current button is not a separator, find the last  */
1393             /*  separator and wrap it.                                   */
1394             for ( j = i - 1; j >= 0  &&  !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1395             {
1396                 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1397                         !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1398                 {
1399                     bFound = TRUE;
1400                     i = j;
1401                     TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1402                           i, btnPtr[i].fsStyle, x, cx);
1403                     x = infoPtr->nIndent;
1404                     btnPtr[j].fsState |= TBSTATE_WRAP;
1405                     bButtonWrap = FALSE;
1406                     break;
1407                 }
1408             }
1409 
1410             /*  If no separator available for wrapping, wrap one of     */
1411             /*  non-hidden previous button.                             */
1412             if (!bFound)
1413             {
1414                 for ( j = i - 1;
1415                         j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1416                 {
1417                     if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1418                         continue;
1419 
1420                     bFound = TRUE;
1421                     i = j;
1422                     TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1423                           i, btnPtr[i].fsStyle, x, cx);
1424                     x = infoPtr->nIndent;
1425                     btnPtr[j].fsState |= TBSTATE_WRAP;
1426                     bButtonWrap = TRUE;
1427                     break;
1428                 }
1429             }
1430 
1431             /* If all above failed, wrap the current button. */
1432             if (!bFound)
1433             {
1434                 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1435                       i, btnPtr[i].fsStyle, x, cx);
1436                 btnPtr[i].fsState |= TBSTATE_WRAP;
1437                 x = infoPtr->nIndent;
1438                 if (btnPtr[i].fsStyle & BTNS_SEP )
1439                     bButtonWrap = FALSE;
1440                 else
1441                     bButtonWrap = TRUE;
1442             }
1443         }
1444         else {
1445             TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1446                   i, btnPtr[i].fsStyle, x, cx);
1447             x += cx;
1448         }
1449     }
1450 }
1451 
1452 
1453 /***********************************************************************
1454 *               TOOLBAR_MeasureButton
1455 *
1456 * Calculates the width and height required for a button. Used in
1457 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1458 * the width of buttons that are autosized.
1459 *
1460 * Note that it would have been rather elegant to use one piece of code for
1461 * both the laying out of the toolbar and for controlling where button parts
1462 * are drawn, but the native control has inconsistencies between the two that
1463 * prevent this from being effectively. These inconsistencies can be seen as
1464 * artefacts where parts of the button appear outside of the bounding button
1465 * rectangle.
1466 *
1467 * There are several cases for the calculation of the button dimensions and
1468 * button part positioning:
1469 *
1470 * List
1471 * ====
1472 *
1473 * With Bitmap:
1474 *
1475 * +--------------------------------------------------------+ ^
1476 * |                    ^                     ^             | |
1477 * |                    | pad.cy / 2          | centred     | |
1478 * | pad.cx/2 + cxedge +--------------+     +------------+  | | DEFPAD_CY +
1479 * |<----------------->| nBitmapWidth |     | Text       |  | | max(nBitmapHeight, szText.cy)
1480 * |                   |<------------>|     |            |  | |
1481 * |                   +--------------+     +------------+  | |
1482 * |<-------------------------------------->|               | |
1483 * |  cxedge + iListGap + nBitmapWidth + 2  |<----------->  | |
1484 * |                                           szText.cx    | |
1485 * +--------------------------------------------------------+ -
1486 * <-------------------------------------------------------->
1487 *  2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1488 *
1489 * Without Bitmap (I_IMAGENONE):
1490 *
1491 * +-----------------------------------+ ^
1492 * |                     ^             | |
1493 * |                     | centred     | | LISTPAD_CY +
1494 * |                   +------------+  | | szText.cy
1495 * |                   | Text       |  | |
1496 * |                   |            |  | |
1497 * |                   +------------+  | |
1498 * |<----------------->|               | |
1499 * |      cxedge       |<----------->  | |
1500 * |                      szText.cx    | |
1501 * +-----------------------------------+ -
1502 * <----------------------------------->
1503 *          szText.cx + pad.cx
1504 *
1505 * Without text:
1506 *
1507 * +--------------------------------------+ ^
1508 * |                       ^              | |
1509 * |                       | padding.cy/2 | | DEFPAD_CY +
1510 * |                     +------------+   | | nBitmapHeight
1511 * |                     | Bitmap     |   | |
1512 * |                     |            |   | |
1513 * |                     +------------+   | |
1514 * |<------------------->|                | |
1515 * | cxedge + iListGap/2 |<----------->   | |
1516 * |                       nBitmapWidth   | |
1517 * +--------------------------------------+ -
1518 * <-------------------------------------->
1519 *     2*cxedge + nBitmapWidth + iListGap
1520 *
1521 * Non-List
1522 * ========
1523 *
1524 * With bitmap:
1525 *
1526 * +-----------------------------------+ ^
1527 * |                     ^             | |
1528 * |                     | pad.cy / 2  | | nBitmapHeight +
1529 * |                     -             | | szText.cy +
1530 * |                   +------------+  | | DEFPAD_CY + 1
1531 * |    centred        |   Bitmap   |  | |
1532 * |<----------------->|            |  | |
1533 * |                   +------------+  | |
1534 * |                         ^         | |
1535 * |                       1 |         | |
1536 * |                         -         | |
1537 * |     centred     +---------------+ | |
1538 * |<--------------->|      Text     | | |
1539 * |                 +---------------+ | |
1540 * +-----------------------------------+ -
1541 * <----------------------------------->
1542 * pad.cx + max(nBitmapWidth, szText.cx)
1543 *
1544 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1545 *
1546 * +---------------------------------------+ ^
1547 * |                     ^                 | |
1548 * |                     | 2 + pad.cy / 2  | |
1549 * |                     -                 | | szText.cy +
1550 * |    centred      +-----------------+   | | pad.cy + 2
1551 * |<--------------->|   Text          |   | |
1552 * |                 +-----------------+   | |
1553 * |                                       | |
1554 * +---------------------------------------+ -
1555 * <--------------------------------------->
1556 *          2*cxedge + pad.cx + szText.cx
1557 *
1558 * Without text:
1559 *   As for with bitmaps, but with szText.cx zero.
1560 */
1561 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1562                                          BOOL bHasBitmap, BOOL bValidImageList)
1563 {
1564     SIZE sizeButton;
1565     if (infoPtr->dwStyle & TBSTYLE_LIST)
1566     {
1567         /* set button height from bitmap / text height... */
1568         sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1569             sizeString.cy);
1570 
1571         /* ... add on the necessary padding */
1572         if (bValidImageList)
1573         {
1574             if (bHasBitmap)
1575                 sizeButton.cy += DEFPAD_CY;
1576             else
1577                 sizeButton.cy += LISTPAD_CY;
1578         }
1579         else
1580             sizeButton.cy += infoPtr->szPadding.cy;
1581 
1582         /* calculate button width */
1583         sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1584             infoPtr->nBitmapWidth + infoPtr->iListGap;
1585         if (sizeString.cx > 0)
1586             sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1587 
1588     }
1589     else
1590     {
1591         if (bHasBitmap)
1592         {
1593             sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1594             if (sizeString.cy > 0)
1595                 sizeButton.cy += 1 + sizeString.cy;
1596             sizeButton.cx = infoPtr->szPadding.cx +
1597                 max(sizeString.cx, infoPtr->nBitmapWidth);
1598         }
1599         else
1600         {
1601             sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1602                 NONLIST_NOTEXT_OFFSET;
1603             sizeButton.cx = infoPtr->szPadding.cx +
1604                 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1605         }
1606     }
1607     return sizeButton;
1608 }
1609 
1610 
1611 /***********************************************************************
1612 *               TOOLBAR_CalcToolbar
1613 *
1614 * This function calculates button and separator placement. It first
1615 * calculates the button sizes, gets the toolbar window width and then
1616 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1617 * on. It assigns a new location to each item and sends this location to
1618 * the tooltip window if appropriate. Finally, it updates the rcBound
1619 * rect and calculates the new required toolbar window height.
1620 */
1621 static void
1622 TOOLBAR_CalcToolbar (HWND hwnd)
1623 {
1624     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1625     SIZE  sizeString, sizeButton;
1626     BOOL validImageList = FALSE;
1627 
1628     TOOLBAR_CalcStrings (hwnd, &sizeString);
1629 
1630     TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1631 
1632     if (TOOLBAR_IsValidImageList(infoPtr, 0))
1633         validImageList = TRUE;
1634     sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1635     infoPtr->nButtonWidth = sizeButton.cx;
1636     infoPtr->nButtonHeight = sizeButton.cy;
1637     infoPtr->iTopMargin = default_top_margin(infoPtr);
1638 
1639     if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1640         infoPtr->nButtonWidth = infoPtr->cxMin;
1641     if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1642         infoPtr->nButtonWidth = infoPtr->cxMax;
1643 
1644     TOOLBAR_LayoutToolbar(hwnd);
1645 }
1646 
1647 static void
1648 TOOLBAR_LayoutToolbar(HWND hwnd)
1649 {
1650     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1651     TBUTTON_INFO *btnPtr;
1652     SIZE sizeButton;
1653     INT i, nRows, nSepRows;
1654     INT x, y, cx, cy;
1655     BOOL bWrap;
1656     BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1657     BOOL hasDropDownArrows = TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle);
1658 
1659     TOOLBAR_WrapToolbar(hwnd, infoPtr->dwStyle);
1660 
1661     x  = infoPtr->nIndent;
1662     y  = infoPtr->iTopMargin;
1663     cx = infoPtr->nButtonWidth;
1664     cy = infoPtr->nButtonHeight;
1665 
1666     nRows = nSepRows = 0;
1667 
1668     infoPtr->rcBound.top = y;
1669     infoPtr->rcBound.left = x;
1670     infoPtr->rcBound.bottom = y + cy;
1671     infoPtr->rcBound.right = x;
1672 
1673     btnPtr = infoPtr->buttons;
1674 
1675     TRACE("cy=%d\n", cy);
1676 
1677     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1678     {
1679         bWrap = FALSE;
1680         if (btnPtr->fsState & TBSTATE_HIDDEN)
1681         {
1682             SetRectEmpty (&btnPtr->rect);
1683             continue;
1684         }
1685 
1686         cy = infoPtr->nButtonHeight;
1687 
1688         /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1689         /* it is the actual width of the separator. This is used for */
1690         /* custom controls in toolbars.                              */
1691         if (btnPtr->fsStyle & BTNS_SEP) {
1692             if (btnPtr->fsStyle & BTNS_DROPDOWN) {
1693                 cy = (btnPtr->iBitmap > 0) ?
1694                      btnPtr->iBitmap : SEPARATOR_WIDTH;
1695                 cx = infoPtr->nButtonWidth;
1696             }
1697             else
1698                 cx = (btnPtr->iBitmap > 0) ?
1699                      btnPtr->iBitmap : SEPARATOR_WIDTH;
1700         }
1701         else
1702         {
1703             if (btnPtr->cx)
1704               cx = btnPtr->cx;
1705             else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || 
1706                 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1707             {
1708               SIZE sz;
1709               HDC hdc;
1710               HFONT hOldFont;
1711 
1712               hdc = GetDC (hwnd);
1713               hOldFont = SelectObject (hdc, infoPtr->hFont);
1714 
1715               TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1716 
1717               SelectObject (hdc, hOldFont);
1718               ReleaseDC (hwnd, hdc);
1719 
1720               sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1721                   TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1722                   validImageList);
1723               cx = sizeButton.cx;
1724             }
1725             else
1726               cx = infoPtr->nButtonWidth;
1727 
1728             /* if size has been set manually then don't add on extra space
1729              * for the drop down arrow */
1730             if (!btnPtr->cx && hasDropDownArrows && 
1731                 ((btnPtr->fsStyle & BTNS_DROPDOWN) || (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)))
1732               cx += DDARROW_WIDTH;
1733         }
1734         if (btnPtr->fsState & TBSTATE_WRAP )
1735                     bWrap = TRUE;
1736 
1737         SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1738 
1739         if (infoPtr->rcBound.left > x)
1740             infoPtr->rcBound.left = x;
1741         if (infoPtr->rcBound.right < x + cx)
1742             infoPtr->rcBound.right = x + cx;
1743         if (infoPtr->rcBound.bottom < y + cy)
1744             infoPtr->rcBound.bottom = y + cy;
1745 
1746         TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1747 
1748         /* btnPtr->nRow is zero based. The space between the rows is    */
1749         /* also considered as a row.                                    */
1750         btnPtr->nRow = nRows + nSepRows;
1751 
1752         TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1753               i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1754               x, y, x+cx, y+cy);
1755 
1756         if( bWrap )
1757         {
1758             if ( !(btnPtr->fsStyle & BTNS_SEP) )
1759                 y += cy;
1760             else
1761             {
1762                 /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1763                 /* it is the actual width of the separator. This is used for */
1764                 /* custom controls in toolbars.                              */
1765                 if ( !(btnPtr->fsStyle & BTNS_DROPDOWN))
1766                     y += cy + ( (btnPtr->iBitmap > 0 ) ?
1767                                 btnPtr->iBitmap : SEPARATOR_WIDTH) * 2 /3;
1768                 else
1769                     y += cy;
1770 
1771                 /* nSepRows is used to calculate the extra height following  */
1772                 /* the last row.                                             */
1773                 nSepRows++;
1774             }
1775             x = infoPtr->nIndent;
1776 
1777             /* Increment row number unless this is the last button    */
1778             /* and it has Wrap set.                                   */
1779             if (i != infoPtr->nNumButtons-1)
1780                 nRows++;
1781         }
1782         else
1783             x += cx;
1784     }
1785 
1786     /* infoPtr->nRows is the number of rows on the toolbar */
1787     infoPtr->nRows = nRows + nSepRows + 1;
1788 
1789     TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1790 }
1791 
1792 
1793 static INT
1794 TOOLBAR_InternalHitTest (HWND hwnd, const POINT *lpPt)
1795 {
1796     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1797     TBUTTON_INFO *btnPtr;
1798     INT i;
1799 
1800     btnPtr = infoPtr->buttons;
1801     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1802         if (btnPtr->fsState & TBSTATE_HIDDEN)
1803             continue;
1804 
1805         if (btnPtr->fsStyle & BTNS_SEP) {
1806             if (PtInRect (&btnPtr->rect, *lpPt)) {
1807                 TRACE(" ON SEPARATOR %d!\n", i);
1808                 return -i;
1809             }
1810         }
1811         else {
1812             if (PtInRect (&btnPtr->rect, *lpPt)) {
1813                 TRACE(" ON BUTTON %d!\n", i);
1814                 return i;
1815             }
1816         }
1817     }
1818 
1819     TRACE(" NOWHERE!\n");
1820     return TOOLBAR_NOWHERE;
1821 }
1822 
1823 
1824 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1825 static BOOL
1826 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, TBBUTTON *lpTbb, BOOL fUnicode)
1827 {
1828     INT nOldButtons, nNewButtons, iButton;
1829     BOOL fHasString = FALSE;
1830 
1831     if (iIndex < 0)  /* iIndex can be negative, what means adding at the end */
1832         iIndex = infoPtr->nNumButtons;
1833 
1834     nOldButtons = infoPtr->nNumButtons;
1835     nNewButtons = nOldButtons + nAddButtons;
1836 
1837     infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1838     memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1839             (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1840     infoPtr->nNumButtons += nAddButtons;
1841 
1842     /* insert new buttons data */
1843     for (iButton = 0; iButton < nAddButtons; iButton++) {
1844         TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1845 
1846         TOOLBAR_DumpTBButton(lpTbb, fUnicode);
1847 
1848         ZeroMemory(btnPtr, sizeof(*btnPtr));
1849         btnPtr->iBitmap   = lpTbb[iButton].iBitmap;
1850         btnPtr->idCommand = lpTbb[iButton].idCommand;
1851         btnPtr->fsState   = lpTbb[iButton].fsState;
1852         btnPtr->fsStyle   = lpTbb[iButton].fsStyle;
1853         btnPtr->dwData    = lpTbb[iButton].dwData;
1854         if(HIWORD(lpTbb[iButton].iString) && lpTbb[iButton].iString != -1)
1855         {
1856             if (fUnicode)
1857                 Str_SetPtrW((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[iButton].iString );
1858             else
1859                 Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[iButton].iString);
1860             fHasString = TRUE;
1861         }
1862         else
1863             btnPtr->iString   = lpTbb[iButton].iString;
1864 
1865         TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1866     }
1867 
1868     if (infoPtr->nNumStrings > 0 || fHasString)
1869         TOOLBAR_CalcToolbar(infoPtr->hwndSelf);
1870     else
1871         TOOLBAR_LayoutToolbar(infoPtr->hwndSelf);
1872     TOOLBAR_AutoSize(infoPtr->hwndSelf);
1873 
1874     TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1875     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1876     return TRUE;
1877 }
1878 
1879 
1880 static INT
1881 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1882 {
1883     TBUTTON_INFO *btnPtr;
1884     INT i;
1885 
1886     if (CommandIsIndex) {
1887         TRACE("command is really index command=%d\n", idCommand);
1888         if (idCommand >= infoPtr->nNumButtons) return -1;
1889         return idCommand;
1890     }
1891     btnPtr = infoPtr->buttons;
1892     for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1893         if (btnPtr->idCommand == idCommand) {
1894             TRACE("command=%d index=%d\n", idCommand, i);
1895             return i;
1896         }
1897     }
1898     TRACE("no index found for command=%d\n", idCommand);
1899     return -1;
1900 }
1901 
1902 
1903 static INT
1904 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1905 {
1906     TBUTTON_INFO *btnPtr;
1907     INT nRunIndex;
1908 
1909     if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1910         return -1;
1911 
1912     /* check index button */
1913     btnPtr = &infoPtr->buttons[nIndex];
1914     if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1915         if (btnPtr->fsState & TBSTATE_CHECKED)
1916             return nIndex;
1917     }
1918 
1919     /* check previous buttons */
1920     nRunIndex = nIndex - 1;
1921     while (nRunIndex >= 0) {
1922         btnPtr = &infoPtr->buttons[nRunIndex];
1923         if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1924             if (btnPtr->fsState & TBSTATE_CHECKED)
1925                 return nRunIndex;
1926         }
1927         else
1928             break;
1929         nRunIndex--;
1930     }
1931 
1932     /* check next buttons */
1933     nRunIndex = nIndex + 1;
1934     while (nRunIndex < infoPtr->nNumButtons) {
1935         btnPtr = &infoPtr->buttons[nRunIndex];
1936         if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1937             if (btnPtr->fsState & TBSTATE_CHECKED)
1938                 return nRunIndex;
1939         }
1940         else
1941             break;
1942         nRunIndex++;
1943     }
1944 
1945     return -1;
1946 }
1947 
1948 
1949 static VOID
1950 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1951                     WPARAM wParam, LPARAM lParam)
1952 {
1953     MSG msg;
1954 
1955     msg.hwnd = hwndMsg;
1956     msg.message = uMsg;
1957     msg.wParam = wParam;
1958     msg.lParam = lParam;
1959     msg.time = GetMessageTime ();
1960     msg.pt.x = (short)LOWORD(GetMessagePos ());
1961     msg.pt.y = (short)HIWORD(GetMessagePos ());
1962 
1963     SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1964 }
1965 
1966 static void
1967 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1968 {
1969     if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1970         TTTOOLINFOW ti;
1971 
1972         ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1973         ti.cbSize   = sizeof (TTTOOLINFOW);
1974         ti.hwnd     = infoPtr->hwndSelf;
1975         ti.uId      = button->idCommand;
1976         ti.hinst    = 0;
1977         ti.lpszText = LPSTR_TEXTCALLBACKW;
1978         /* ti.lParam = random value from the stack? */
1979 
1980         SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1981             0, (LPARAM)&ti);
1982     }
1983 }
1984 
1985 static void
1986 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1987 {
1988     if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1989         TTTOOLINFOW ti;
1990 
1991         ZeroMemory(&ti, sizeof(ti));
1992         ti.cbSize   = sizeof(ti);
1993         ti.hwnd     = infoPtr->hwndSelf;
1994         ti.uId      = button->idCommand;
1995 
1996         SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
1997     }
1998 }
1999 
2000 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2001 {
2002     /* Set the toolTip only for non-hidden, non-separator button */
2003     if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2004     {
2005         TTTOOLINFOW ti;
2006 
2007         ZeroMemory(&ti, sizeof(ti));
2008         ti.cbSize = sizeof(ti);
2009         ti.hwnd = infoPtr->hwndSelf;
2010         ti.uId = button->idCommand;
2011         ti.rect = button->rect;
2012         SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2013     }
2014 }
2015 
2016 /* Creates the tooltip control */
2017 static void
2018 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2019 {
2020     int i;
2021     NMTOOLTIPSCREATED nmttc;
2022 
2023     infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2024             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2025             infoPtr->hwndSelf, 0, 0, 0);
2026 
2027     if (!infoPtr->hwndToolTip)
2028         return;
2029 
2030     /* Send NM_TOOLTIPSCREATED notification */
2031     nmttc.hwndToolTips = infoPtr->hwndToolTip;
2032     TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2033 
2034     for (i = 0; i < infoPtr->nNumButtons; i++)
2035     {
2036         TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2037         TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2038     }
2039 }
2040 
2041 /* keeps available button list box sorted by button id */
2042 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2043 {
2044     int i;
2045     int count;
2046     PCUSTOMBUTTON btnInfo;
2047     HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2048 
2049     TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2050 
2051     count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2052 
2053     /* position 0 is always separator */
2054     for (i = 1; i < count; i++)
2055     {
2056         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2057         if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2058         {
2059             i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2060             SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2061             return;
2062         }
2063     }
2064     /* id higher than all others add to end */
2065     i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2066     SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2067 }
2068 
2069 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2070 {
2071     NMTOOLBARW nmtb;
2072 
2073         TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2074 
2075     if (nIndexFrom == nIndexTo)
2076         return;
2077 
2078     /* MSDN states that iItem is the index of the button, rather than the
2079      * command ID as used by every other NMTOOLBAR notification */
2080     nmtb.iItem = nIndexFrom;
2081     if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2082     {
2083         PCUSTOMBUTTON btnInfo;
2084         NMHDR hdr;
2085         HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2086         int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2087 
2088         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2089 
2090         SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2091         SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2092         SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2093         SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2094 
2095         if (nIndexTo <= 0)
2096             EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2097         else
2098             EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2099 
2100         /* last item is always separator, so -2 instead of -1 */
2101         if (nIndexTo >= (count - 2))
2102             EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2103         else
2104             EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2105 
2106         SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2107         SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2108 
2109         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2110     }
2111 }
2112 
2113 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2114 {
2115     NMTOOLBARW nmtb;
2116 
2117     TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2118 
2119     /* MSDN states that iItem is the index of the button, rather than the
2120      * command ID as used by every other NMTOOLBAR notification */
2121     nmtb.iItem = nIndexAvail;
2122     if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2123     {
2124         PCUSTOMBUTTON btnInfo;
2125         NMHDR hdr;
2126         HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2127         HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2128         int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2129 
2130         btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2131 
2132         if (nIndexAvail != 0) /* index == 0 indicates separator */
2133         {
2134             /* remove from 'available buttons' list */
2135             SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2136             if (nIndexAvail == count-1)
2137                 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2138             else
2139                 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2140         }
2141         else
2142         {
2143             PCUSTOMBUTTON btnNew;
2144 
2145             /* duplicate 'separator' button */
2146             btnNew = Alloc(sizeof(CUSTOMBUTTON));
2147             *btnNew = *btnInfo;
2148             btnInfo = btnNew;
2149         }
2150 
2151         /* insert into 'toolbar button' list */
2152         SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2153         SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2154 
2155         SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2156 
2157         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2158     }
2159 }
2160 
2161 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2162 {
2163     PCUSTOMBUTTON btnInfo;
2164     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2165 
2166     TRACE("Remove: index %d\n", index);
2167 
2168     btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2169 
2170     /* send TBN_QUERYDELETE notification */
2171     if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2172     {
2173         NMHDR hdr;
2174 
2175         SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2176         SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2177 
2178         SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2179 
2180         /* insert into 'available button' list */
2181         if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2182             TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2183         else
2184             Free(btnInfo);
2185 
2186         TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2187     }
2188 }
2189 
2190 /* drag list notification function for toolbar buttons list box */
2191 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2192                                                         const DRAGLISTINFO *pDLI)
2193 {
2194     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2195     switch (pDLI->uNotification)
2196     {
2197     case DL_BEGINDRAG:
2198     {
2199         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2200         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2201         /* no dragging for last item (separator) */
2202         if (nCurrentItem >= (nCount - 1)) return FALSE;
2203         return TRUE;
2204     }
2205     case DL_DRAGGING:
2206     {
2207         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2208         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2209         /* no dragging past last item (separator) */
2210         if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2211         {
2212             DrawInsert(hwnd, hwndList, nCurrentItem);
2213             /* FIXME: native uses "move button" cursor */
2214             return DL_COPYCURSOR;
2215         }
2216 
2217         /* not over toolbar buttons list */
2218         if (nCurrentItem < 0)
2219         {
2220             POINT ptWindow = pDLI->ptCursor;
2221             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2222             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2223             /* over available buttons list? */
2224             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2225                 /* FIXME: native uses "move button" cursor */
2226                 return DL_COPYCURSOR;
2227         }
2228         /* clear drag arrow */
2229         DrawInsert(hwnd, hwndList, -1);
2230         return DL_STOPCURSOR;
2231     }
2232     case DL_DROPPED:
2233     {
2234         INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2235         INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2236         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2237         if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2238         {
2239             /* clear drag arrow */
2240             DrawInsert(hwnd, hwndList, -1);
2241             /* move item */
2242             TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2243         }
2244         /* not over toolbar buttons list */
2245         if (nIndexTo < 0)
2246         {
2247             POINT ptWindow = pDLI->ptCursor;
2248             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2249             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2250             /* over available buttons list? */
2251             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2252                 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2253         }
2254         break;
2255     }
2256     case DL_CANCELDRAG:
2257         /* Clear drag arrow */
2258         DrawInsert(hwnd, hwndList, -1);
2259         break;
2260     }
2261 
2262     return 0;
2263 }
2264 
2265 /* drag list notification function for available buttons list box */
2266 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2267                                                       const DRAGLISTINFO *pDLI)
2268 {
2269     HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2270     switch (pDLI->uNotification)
2271     {
2272     case DL_BEGINDRAG:
2273         return TRUE;
2274     case DL_DRAGGING:
2275     {
2276         INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2277         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2278         /* no dragging past last item (separator) */
2279         if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2280         {
2281             DrawInsert(hwnd, hwndList, nCurrentItem);
2282             /* FIXME: native uses "move button" cursor */
2283             return DL_COPYCURSOR;
2284         }
2285 
2286         /* not over toolbar buttons list */
2287         if (nCurrentItem < 0)
2288         {
2289             POINT ptWindow = pDLI->ptCursor;
2290             HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2291             MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2292             /* over available buttons list? */
2293             if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2294                 /* FIXME: native uses "move button" cursor */
2295                 return DL_COPYCURSOR;
2296         }
2297         /* clear drag arrow */
2298         DrawInsert(hwnd, hwndList, -1);
2299         return DL_STOPCURSOR;
2300     }
2301     case DL_DROPPED:
2302     {
2303         INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2304         INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2305         INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2306         if ((nIndexTo >= 0) && (nIndexTo < nCount))
2307         {
2308             /* clear drag arrow */
2309             DrawInsert(hwnd, hwndList, -1);
2310             /* add item */
2311             TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2312         }
2313     }
2314     case DL_CANCELDRAG:
2315         /* Clear drag arrow */
2316         DrawInsert(hwnd, hwndList, -1);
2317         break;
2318     }
2319     return 0;
2320 }
2321 
2322 extern UINT uDragListMessage;
2323 
2324 /***********************************************************************
2325  * TOOLBAR_CustomizeDialogProc
2326  * This function implements the toolbar customization dialog.
2327  */
2328 static INT_PTR CALLBACK
2329 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2330 {
2331     PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2332     PCUSTOMBUTTON btnInfo;
2333     NMTOOLBARA nmtb;
2334     TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2335 
2336     switch (uMsg)
2337     {
2338         case WM_INITDIALOG:
2339             custInfo = (PCUSTDLG_INFO)lParam;
2340             SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2341 
2342             if (custInfo)
2343             {
2344                 WCHAR Buffer[256];
2345                 int i = 0;
2346                 int index;
2347                 NMTBINITCUSTOMIZE nmtbic;
2348 
2349                 infoPtr = custInfo->tbInfo;
2350 
2351                 /* send TBN_QUERYINSERT notification */
2352                 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2353 
2354                 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2355                     return FALSE;
2356 
2357                 nmtbic.hwndDialog = hwnd;
2358                 /* Send TBN_INITCUSTOMIZE notification */
2359                 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2360                     TBNRF_HIDEHELP)
2361                 {
2362                     TRACE("TBNRF_HIDEHELP requested\n");
2363                     ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2364                 }
2365 
2366                 /* add items to 'toolbar buttons' list and check if removable */
2367                 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2368                 {
2369                     btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2370                     memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2371                     btnInfo->btn.fsStyle = BTNS_SEP;
2372                     btnInfo->bVirtual = FALSE;
2373                     LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2374 
2375                     /* send TBN_QUERYDELETE notification */
2376                     btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2377 
2378                     index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2379                     SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2380                 }
2381 
2382                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2383 
2384                 /* insert separator button into 'available buttons' list */
2385                 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2386                 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2387                 btnInfo->btn.fsStyle = BTNS_SEP;
2388                 btnInfo->bVirtual = FALSE;
2389                 btnInfo->bRemovable = TRUE;
2390                 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2391                 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2392                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2393 
2394                 /* insert all buttons into dsa */
2395                 for (i = 0;; i++)
2396                 {
2397                     /* send TBN_GETBUTTONINFO notification */
2398                     NMTOOLBARW nmtb;
2399                     nmtb.iItem = i;
2400                     nmtb.pszText = Buffer;
2401                     nmtb.cchText = 256;
2402 
2403                     /* Clear previous button's text */
2404                     ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2405 
2406                     if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2407                         break;
2408 
2409                     TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2410                         nmtb.tbButton.fsStyle, i, 
2411                         nmtb.tbButton.idCommand,
2412                         nmtb.tbButton.iString,
2413                         nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2414                         : "");
2415 
2416                     /* insert button into the apropriate list */
2417                     index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2418                     if (index == -1)
2419                     {
2420                         btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2421                         btnInfo->bVirtual = FALSE;
2422                         btnInfo->bRemovable = TRUE;
2423                     }
2424                     else
2425                     {
2426                         btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, 
2427                             IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2428                     }
2429 
2430                     btnInfo->btn = nmtb.tbButton;
2431                     if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2432                     {
2433                         if (lstrlenW(nmtb.pszText))
2434                             lstrcpyW(btnInfo->text, nmtb.pszText);
2435                         else if (nmtb.tbButton.iString >= 0 && 
2436                             nmtb.tbButton.iString < infoPtr->nNumStrings)
2437                         {
2438                             lstrcpyW(btnInfo->text, 
2439                                 infoPtr->strings[nmtb.tbButton.iString]);
2440                         }
2441                     }
2442 
2443                     if (index == -1)
2444                         TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2445                 }
2446 
2447                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2448 
2449                 /* select first item in the 'available' list */
2450                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2451 
2452                 /* append 'virtual' separator button to the 'toolbar buttons' list */
2453                 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2454                 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2455                 btnInfo->btn.fsStyle = BTNS_SEP;
2456                 btnInfo->bVirtual = TRUE;
2457                 btnInfo->bRemovable = FALSE;
2458                 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2459                 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2460                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2461 
2462                 /* select last item in the 'toolbar' list */
2463                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2464                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2465 
2466                 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2467                 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2468 
2469                 /* set focus and disable buttons */
2470                 PostMessageW (hwnd, WM_USER, 0, 0);
2471             }
2472             return TRUE;
2473 
2474         case WM_USER:
2475             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2476             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2477             EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2478             SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2479             return TRUE;
2480 
2481         case WM_CLOSE:
2482             EndDialog(hwnd, FALSE);
2483             return TRUE;
2484 
2485         case WM_COMMAND:
2486             switch (LOWORD(wParam))
2487             {
2488                 case IDC_TOOLBARBTN_LBOX:
2489                     if (HIWORD(wParam) == LBN_SELCHANGE)
2490                     {
2491                         PCUSTOMBUTTON btnInfo;
2492                         NMTOOLBARA nmtb;
2493                         int count;
2494                         int index;
2495 
2496                         count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2497                         index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2498 
2499                         /* send TBN_QUERYINSERT notification */
2500                         nmtb.iItem = index;
2501                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2502 
2503                         /* get list box item */
2504                         btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2505 
2506                         if (index == (count - 1))
2507                         {
2508                             /* last item (virtual separator) */
2509                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2510                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2511                         }
2512                         else if (index == (count - 2))
2513                         {
2514                             /* second last item (last non-virtual item) */
2515                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2516                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2517                         }
2518                         else if (index == 0)
2519                         {
2520                             /* first item */
2521                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2522                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2523                         }
2524                         else
2525                         {
2526                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2527                             EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2528                         }
2529 
2530                         EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2531                     }
2532                     break;
2533 
2534                 case IDC_MOVEUP_BTN:
2535                     {
2536                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2537                         TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2538                     }
2539                     break;
2540 
2541                 case IDC_MOVEDN_BTN: /* move down */
2542                     {
2543                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2544                         TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2545                     }
2546                     break;
2547 
2548                 case IDC_REMOVE_BTN: /* remove button */
2549                     {
2550                         int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2551 
2552                         if (LB_ERR == index)
2553                                 break;
2554 
2555                         TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2556                     }
2557                     break;
2558                 case IDC_HELP_BTN:
2559                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2560                         break;
2561                 case IDC_RESET_BTN:
2562                         TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2563                         break;
2564 
2565                 case IDOK: /* Add button */
2566                     {
2567                         int index;
2568                         int indexto;
2569 
2570                         index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2571                         indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2572 
2573                         TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2574                     }
2575                     break;
2576 
2577                 case IDCANCEL:
2578                     EndDialog(hwnd, FALSE);
2579                     break;
2580             }
2581             return TRUE;
2582 
2583         case WM_DESTROY:
2584             {
2585                 int count;
2586                 int i;
2587 
2588                 /* delete items from 'toolbar buttons' listbox*/
2589                 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2590                 for (i = 0; i < count; i++)
2591                 {
2592                     btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2593                     Free(btnInfo);
2594                     SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2595                 }
2596                 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2597 
2598 
2599                 /* delete items from 'available buttons' listbox*/
2600                 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2601                 for (i = 0; i < count; i++)
2602                 {
2603                     btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2604                     Free(btnInfo);
2605                     SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2606                 }
2607                 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2608             }
2609             return TRUE;
2610 
2611         case WM_DRAWITEM:
2612             if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2613             {
2614                 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2615                 RECT rcButton;
2616                 RECT rcText;
2617                 HPEN hPen, hOldPen;
2618                 HBRUSH hOldBrush;
2619                 COLORREF oldText = 0;
2620                 COLORREF oldBk = 0;
2621 
2622                 /* get item data */
2623                 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, (WPARAM)lpdis->itemID, 0);
2624                 if (btnInfo == NULL)
2625                 {
2626                     FIXME("btnInfo invalid!\n");
2627                     return TRUE;
2628                 }
2629 
2630                 /* set colors and select objects */
2631                 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2632                 if (btnInfo->bVirtual)
2633                    oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2634                 else
2635                    oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2636                 hPen = CreatePen( PS_SOLID, 1,
2637                      GetSysColor( (lpdis->itemState & ODS_SELECTED)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2638                 hOldPen = SelectObject (lpdis->hDC, hPen );
2639                 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2640 
2641                 /* fill background rectangle */
2642                 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2643                            lpdis->rcItem.right, lpdis->rcItem.bottom);
2644 
2645                 /* calculate button and text rectangles */
2646                 CopyRect (&rcButton, &lpdis->rcItem);
2647                 InflateRect (&rcButton, -1, -1);
2648                 CopyRect (&rcText, &rcButton);
2649                 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2650                 rcText.left = rcButton.right + 2;
2651 
2652                 /* draw focus rectangle */
2653                 if (lpdis->itemState & ODS_FOCUS)
2654                     DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2655 
2656                 /* draw button */
2657                 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2658                     DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2659 
2660                 /* draw image and text */
2661                 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2662                         HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, 
2663                                 btnInfo->btn.iBitmap));
2664                     ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap), 
2665                                 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2666                 }
2667                 DrawTextW (lpdis->hDC,  btnInfo->text, -1, &rcText,
2668                                DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2669 
2670                 /* delete objects and reset colors */
2671                 SelectObject (lpdis->hDC, hOldBrush);
2672                 SelectObject (lpdis->hDC, hOldPen);
2673                 SetBkColor (lpdis->hDC, oldBk);
2674                 SetTextColor (lpdis->hDC, oldText);
2675                 DeleteObject( hPen );
2676                 return TRUE;
2677             }
2678             return FALSE;
2679 
2680         case WM_MEASUREITEM:
2681             if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2682             {
2683                 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2684 
2685                 lpmis->itemHeight = 15 + 8; /* default height */
2686 
2687                 return TRUE;
2688             }
2689             return FALSE;
2690 
2691         default:
2692             if (uDragListMessage && (uMsg == uDragListMessage))
2693             {
2694                 if (wParam == IDC_TOOLBARBTN_LBOX)
2695                 {
2696                     LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2697                         custInfo, hwnd, (DRAGLISTINFO *)lParam);
2698                     SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2699                     return TRUE;
2700                 }
2701                 else if (wParam == IDC_AVAILBTN_LBOX)
2702                 {
2703                     LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2704                         custInfo, hwnd, (DRAGLISTINFO *)lParam);
2705                     SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2706                     return TRUE;
2707                 }
2708             }
2709             return FALSE;
2710     }
2711 }
2712 
2713 static BOOL
2714 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2715 {
2716     HBITMAP hbmLoad;
2717     INT nCountBefore = ImageList_GetImageCount(himlDef);
2718     INT nCountAfter;
2719     INT cxIcon, cyIcon;
2720     INT nAdded;
2721     INT nIndex;
2722 
2723     TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2724     /* Add bitmaps to the default image list */
2725     if (bitmap->hInst == NULL)         /* a handle was passed */
2726         hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2727     else
2728         hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2729 
2730     /* enlarge the bitmap if needed */
2731     ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2732     if (bitmap->hInst != COMCTL32_hModule)
2733         COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2734     
2735     nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2736     DeleteObject(hbmLoad);
2737     if (nIndex == -1)
2738         return FALSE;
2739     
2740     nCountAfter = ImageList_GetImageCount(himlDef);
2741     nAdded =  nCountAfter - nCountBefore;
2742     if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2743     {
2744         ImageList_SetImageCount(himlDef, nCountBefore + 1);
2745     } else if (nAdded > (INT)bitmap->nButtons) {
2746         TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2747             nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2748     }
2749 
2750     infoPtr->nNumBitmaps += nAdded;
2751     return TRUE;
2752 }
2753 
2754 static void
2755 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2756 {
2757     HIMAGELIST himlDef;
2758     HIMAGELIST himlNew;
2759     INT cx, cy;
2760     INT i;
2761     
2762     himlDef = GETDEFIMAGELIST(infoPtr, 0);
2763     if (himlDef == NULL || himlDef != infoPtr->himlInt)
2764         return;
2765     if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2766         return;
2767     if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2768         return;
2769 
2770     TRACE("Update icon size: %dx%d -> %dx%d\n",
2771         cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2772 
2773     himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2774                                 ILC_COLORDDB|ILC_MASK, 8, 2);
2775     for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2776         TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2777     TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2778     infoPtr->himlInt = himlNew;
2779 
2780     infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2781     ImageList_Destroy(himlDef);
2782 }
2783 
2784 /***********************************************************************
2785  * TOOLBAR_AddBitmap:  Add the bitmaps to the default image list.
2786  *
2787  */
2788 static LRESULT
2789 TOOLBAR_AddBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
2790 {
2791     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2792     LPTBADDBITMAP lpAddBmp = (LPTBADDBITMAP)lParam;
2793     TBITMAP_INFO info;
2794     INT iSumButtons, i;
2795     HIMAGELIST himlDef;
2796 
2797     TRACE("hwnd=%p wParam=%lx lParam=%lx\n", hwnd, wParam, lParam);
2798     if (!lpAddBmp)
2799         return -1;
2800 
2801     if (lpAddBmp->hInst == HINST_COMMCTRL)
2802     {
2803         info.hInst = COMCTL32_hModule;
2804         switch (lpAddBmp->nID)
2805         {
2806             case IDB_STD_SMALL_COLOR:
2807                 info.nButtons = 15;
2808                 info.nID = IDB_STD_SMALL;
2809                 break;
2810             case IDB_STD_LARGE_COLOR:
2811                 info.nButtons = 15;
2812                 info.nID = IDB_STD_LARGE;
2813                 break;
2814             case IDB_VIEW_SMALL_COLOR:
2815                 info.nButtons = 12;
2816                 info.nID = IDB_VIEW_SMALL;
2817                 break;
2818             case IDB_VIEW_LARGE_COLOR:
2819                 info.nButtons = 12;
2820                 info.nID = IDB_VIEW_LARGE;
2821                 break;
2822             case IDB_HIST_SMALL_COLOR:
2823                 info.nButtons = 5;
2824                 info.nID = IDB_HIST_SMALL;
2825                 break;
2826             case IDB_HIST_LARGE_COLOR:
2827                 info.nButtons = 5;
2828                 info.nID = IDB_HIST_LARGE;
2829                 break;
2830             default:
2831                 return -1;
2832         }
2833 
2834         TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2835 
2836         /* Windows resize all the buttons to the size of a newly added standard image */
2837         if (lpAddBmp->nID & 1)
2838         {
2839             /* large icons: 24x24. Will make the button 31x30 */
2840             SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2841         }
2842         else
2843         {
2844             /* small icons: 16x16. Will make the buttons 23x22 */
2845             SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2846         }
2847 
2848         TOOLBAR_CalcToolbar (hwnd);
2849     }
2850     else
2851     {
2852         info.nButtons = (INT)wParam;
2853         info.hInst = lpAddBmp->hInst;
2854         info.nID = lpAddBmp->nID;
2855         TRACE("adding %d bitmaps!\n", info.nButtons);
2856     }
2857     
2858     /* check if the bitmap is already loaded and compute iSumButtons */
2859     iSumButtons = 0;
2860     for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2861     {
2862         if (infoPtr->bitmaps[i].hInst == info.hInst &&
2863             infoPtr->bitmaps[i].nID == info.nID)
2864             return iSumButtons;
2865         iSumButtons += infoPtr->bitmaps[i].nButtons;
2866     }
2867 
2868     if (!infoPtr->cimlDef) {
2869         /* create new default image list */
2870         TRACE ("creating default image list!\n");
2871 
2872         himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2873                                     ILC_COLORDDB | ILC_MASK, info.nButtons, 2);
2874         TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2875         infoPtr->himlInt = himlDef;
2876     }
2877     else {
2878         himlDef = GETDEFIMAGELIST(infoPtr, 0);
2879     }
2880 
2881     if (!himlDef) {
2882         WARN("No default image list available\n");
2883         return -1;
2884     }
2885 
2886     if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2887         return -1;
2888 
2889     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2890     infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2891     infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2892     infoPtr->nNumBitmapInfos++;
2893     TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2894 
2895     InvalidateRect(hwnd, NULL, TRUE);
2896     return iSumButtons;
2897 }
2898 
2899 
2900 static LRESULT
2901 TOOLBAR_AddButtonsT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
2902 {
2903     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2904     LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2905     INT nAddButtons = (UINT)wParam;
2906 
2907     TRACE("adding %ld buttons (unicode=%d)!\n", wParam, fUnicode);
2908 
2909     return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2910 }
2911 
2912 
2913 static LRESULT
2914 TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2915 {
2916 #define MAX_RESOURCE_STRING_LENGTH 512
2917     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2918     BOOL fFirstString = (infoPtr->nNumStrings == 0);
2919     INT nIndex = infoPtr->nNumStrings;
2920 
2921     if ((wParam) && (HIWORD(lParam) == 0)) {
2922         WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2923         WCHAR delimiter;
2924         WCHAR *next_delim;
2925         WCHAR *p;
2926         INT len;
2927         TRACE("adding string from resource!\n");
2928 
2929         len = LoadStringW ((HINSTANCE)wParam, (UINT)lParam,
2930                              szString, MAX_RESOURCE_STRING_LENGTH);
2931 
2932         TRACE("len=%d %s\n", len, debugstr_w(szString));
2933         if (len == 0 || len == 1)
2934             return nIndex;
2935 
2936         TRACE("Delimiter: 0x%x\n", *szString);
2937         delimiter = *szString;
2938         p = szString + 1;
2939 
2940         while ((next_delim = strchrW(p, delimiter)) != NULL) {
2941             *next_delim = 0;
2942             if (next_delim + 1 >= szString + len)
2943             {
2944                 /* this may happen if delimiter == '\0' or if the last char is a
2945                  * delimiter (then it is ignored like the native does) */
2946                 break;
2947             }
2948 
2949             infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2950             Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2951             infoPtr->nNumStrings++;
2952 
2953             p = next_delim + 1;
2954         }
2955     }
2956     else {
2957         LPWSTR p = (LPWSTR)lParam;
2958         INT len;
2959 
2960         if (p == NULL)
2961             return -1;
2962         TRACE("adding string(s) from array!\n");
2963         while (*p) {
2964             len = strlenW (p);
2965 
2966             TRACE("len=%d %s\n", len, debugstr_w(p));
2967             infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2968             Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2969             infoPtr->nNumStrings++;
2970 
2971             p += (len+1);
2972         }
2973     }
2974 
2975     if (fFirstString)
2976         TOOLBAR_CalcToolbar(hwnd);
2977     return nIndex;
2978 }
2979 
2980 
2981 static LRESULT
2982 TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2983 {
2984     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2985     BOOL fFirstString = (infoPtr->nNumStrings == 0);
2986     LPSTR p;
2987     INT nIndex;
2988     INT len;
2989 
2990     if ((wParam) && (HIWORD(lParam) == 0))  /* load from resources */
2991         return TOOLBAR_AddStringW(hwnd, wParam, lParam);
2992 
2993     p = (LPSTR)lParam;
2994     if (p == NULL)
2995         return -1;
2996 
2997     TRACE("adding string(s) from array!\n");
2998     nIndex = infoPtr->nNumStrings;
2999     while (*p) {
3000         len = strlen (p);
3001         TRACE("len=%d \"%s\"\n", len, p);
3002 
3003         infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3004         Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3005         infoPtr->nNumStrings++;
3006 
3007         p += (len+1);
3008     }
3009 
3010     if (fFirstString)
3011         TOOLBAR_CalcToolbar(hwnd);
3012     return nIndex;
3013 }
3014 
3015 
3016 static LRESULT
3017 TOOLBAR_AutoSize (HWND hwnd)
3018 {
3019     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3020     RECT parent_rect;
3021     HWND parent;
3022     INT  x, y;
3023     INT  cx, cy;
3024 
3025     TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3026 
3027     parent = GetParent (hwnd);
3028 
3029     if (!parent || !infoPtr->bDoRedraw)
3030         return 0;
3031 
3032     GetClientRect(parent, &parent_rect);
3033 
3034     x = parent_rect.left;
3035     y = parent_rect.top;
3036 
3037     TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3038 
3039     cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3040     cx = parent_rect.right - parent_rect.left;
3041 
3042     if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1))
3043     {
3044         TOOLBAR_LayoutToolbar(hwnd);
3045         InvalidateRect( hwnd, NULL, TRUE );
3046     }
3047 
3048     if (!(infoPtr->dwStyle & CCS_NORESIZE))
3049     {
3050         RECT window_rect;
3051         UINT uPosFlags = SWP_NOZORDER;
3052 
3053         if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3054         {
3055             GetWindowRect(hwnd, &window_rect);
3056             ScreenToClient(parent, (LPPOINT)&window_rect.left);
3057             y = window_rect.top;
3058         }
3059         if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3060         {
3061             GetWindowRect(hwnd, &window_rect);
3062             y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3063         }
3064 
3065         if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3066             uPosFlags |= SWP_NOMOVE;
3067     
3068         if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3069             cy += GetSystemMetrics(SM_CYEDGE);
3070 
3071         if (infoPtr->dwStyle & WS_BORDER)
3072         {
3073             x = y = 1; /* FIXME: this looks wrong */
3074             cy += GetSystemMetrics(SM_CYEDGE);
3075             cx += GetSystemMetrics(SM_CXEDGE);
3076         }
3077 
3078         SetWindowPos(hwnd, NULL, x, y, cx, cy, uPosFlags);
3079     }
3080 
3081     return 0;
3082 }
3083 
3084 
3085 static LRESULT
3086 TOOLBAR_ButtonCount (HWND hwnd)
3087 {
3088     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3089 
3090     return infoPtr->nNumButtons;
3091 }
3092 
3093 
3094 static LRESULT
3095 TOOLBAR_ButtonStructSize (HWND hwnd, WPARAM wParam)
3096 {
3097     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3098 
3099     infoPtr->dwStructSize = (DWORD)wParam;
3100 
3101     return 0;
3102 }
3103 
3104 
3105 static LRESULT
3106 TOOLBAR_ChangeBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3107 {
3108     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3109     TBUTTON_INFO *btnPtr;
3110     INT nIndex;
3111 
3112     TRACE("button %ld, iBitmap now %d\n", wParam, LOWORD(lParam));
3113 
3114     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3115     if (nIndex == -1)
3116         return FALSE;
3117 
3118     btnPtr = &infoPtr->buttons[nIndex];
3119     btnPtr->iBitmap = LOWORD(lParam);
3120 
3121     /* we HAVE to erase the background, the new bitmap could be */
3122     /* transparent */
3123     InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3124 
3125     return TRUE;
3126 }
3127 
3128 
3129 static LRESULT
3130 TOOLBAR_CheckButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3131 {
3132     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3133     TBUTTON_INFO *btnPtr;
3134     INT nIndex;
3135     INT nOldIndex = -1;
3136     BOOL bChecked = FALSE;
3137 
3138     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3139 
3140     TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", hwnd, nIndex, lParam);
3141 
3142     if (nIndex == -1)
3143         return FALSE;
3144 
3145     btnPtr = &infoPtr->buttons[nIndex];
3146 
3147     bChecked = (btnPtr->fsState & TBSTATE_CHECKED) ? TRUE : FALSE;
3148 
3149     if (LOWORD(lParam) == FALSE)
3150         btnPtr->fsState &= ~TBSTATE_CHECKED;
3151     else {
3152         if (btnPtr->fsStyle & BTNS_GROUP) {
3153             nOldIndex =
3154                 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3155             if (nOldIndex == nIndex)
3156                 return 0;
3157             if (nOldIndex != -1)
3158                 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3159         }
3160         btnPtr->fsState |= TBSTATE_CHECKED;
3161     }
3162 
3163     if( bChecked != LOWORD(lParam) )
3164     {
3165         if (nOldIndex != -1)
3166             InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect, TRUE);
3167         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3168     }
3169 
3170     /* FIXME: Send a WM_NOTIFY?? */
3171 
3172     return TRUE;
3173 }
3174 
3175 
3176 static LRESULT
3177 TOOLBAR_CommandToIndex (HWND hwnd, WPARAM wParam)
3178 {
3179     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3180 
3181     return TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3182 }
3183 
3184 
3185 static LRESULT
3186 TOOLBAR_Customize (HWND hwnd)
3187 {
3188     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3189     CUSTDLG_INFO custInfo;
3190     LRESULT ret;
3191     LPCVOID template;
3192     HRSRC hRes;
3193     NMHDR nmhdr;
3194 
3195     custInfo.tbInfo = infoPtr;
3196     custInfo.tbHwnd = hwnd;
3197 
3198     /* send TBN_BEGINADJUST notification */
3199     TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3200 
3201     if (!(hRes = FindResourceW (COMCTL32_hModule,
3202                                 MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3203                                 (LPWSTR)RT_DIALOG)))
3204         return FALSE;
3205 
3206     if(!(template = LoadResource (COMCTL32_hModule, hRes)))
3207         return FALSE;
3208 
3209     ret = DialogBoxIndirectParamW ((HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE),
3210                                    template, hwnd, TOOLBAR_CustomizeDialogProc,
3211                                    (LPARAM)&custInfo);
3212 
3213     /* send TBN_ENDADJUST notification */
3214     TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3215 
3216     return ret;
3217 }
3218 
3219 
3220 static LRESULT
3221 TOOLBAR_DeleteButton (HWND hwnd, WPARAM wParam)
3222 {
3223     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3224     INT nIndex = (INT)wParam;
3225     NMTOOLBARW nmtb;
3226     TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3227 
3228     if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3229         return FALSE;
3230 
3231     memset(&nmtb, 0, sizeof(nmtb));
3232     nmtb.iItem = btnPtr->idCommand;
3233     nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3234     nmtb.tbButton.idCommand = btnPtr->idCommand;
3235     nmtb.tbButton.fsState = btnPtr->fsState;
3236     nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3237     nmtb.tbButton.dwData = btnPtr->dwData;
3238     nmtb.tbButton.iString = btnPtr->iString;
3239     TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3240 
3241     TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3242 
3243     if (infoPtr->nNumButtons == 1) {
3244         TRACE(" simple delete!\n");
3245         Free (infoPtr->buttons);
3246         infoPtr->buttons = NULL;
3247         infoPtr->nNumButtons = 0;
3248     }
3249     else {
3250         TBUTTON_INFO *oldButtons = infoPtr->buttons;
3251         TRACE("complex delete! [nIndex=%d]\n", nIndex);
3252 
3253         infoPtr->nNumButtons--;
3254         infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3255         if (nIndex > 0) {
3256             memcpy (&infoPtr->buttons[0], &oldButtons[0],
3257                     nIndex * sizeof(TBUTTON_INFO));
3258         }
3259 
3260         if (nIndex < infoPtr->nNumButtons) {
3261             memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3262                     (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3263         }
3264 
3265         Free (oldButtons);
3266     }
3267 
3268     TOOLBAR_LayoutToolbar(hwnd);
3269 
3270     InvalidateRect (hwnd, NULL, TRUE);
3271 
3272     return TRUE;
3273 }
3274 
3275 
3276 static LRESULT
3277 TOOLBAR_EnableButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3278 {
3279     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3280     TBUTTON_INFO *btnPtr;
3281     INT nIndex;
3282     DWORD bState;
3283 
3284     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3285 
3286     TRACE("hwnd=%p, btn index=%ld, lParam=0x%08lx\n", hwnd, wParam, lParam);
3287 
3288     if (nIndex == -1)
3289         return FALSE;
3290 
3291     btnPtr = &infoPtr->buttons[nIndex];
3292 
3293     bState = btnPtr->fsState & TBSTATE_ENABLED;
3294 
3295     /* update the toolbar button state */
3296     if(LOWORD(lParam) == FALSE) {
3297         btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3298     } else {
3299         btnPtr->fsState |= TBSTATE_ENABLED;
3300     }
3301 
3302     /* redraw the button only if the state of the button changed */
3303     if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3304         InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3305 
3306     return TRUE;
3307 }
3308 
3309 
3310 static inline LRESULT
3311 TOOLBAR_GetAnchorHighlight (HWND hwnd)
3312 {
3313     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3314 
3315     return infoPtr->bAnchor;
3316 }
3317 
3318 
3319 static LRESULT
3320 TOOLBAR_GetBitmap (HWND hwnd, WPARAM wParam)
3321 {
3322     TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3323     INT nIndex;
3324 
3325     nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3326     if (nIndex == -1)
3327         return -1;
3328 
3329     return infoPtr->buttons[nIndex].iBitmap;
3330 }
3331 
3332 
3333 static inline LRESULT
3334 TOOLBAR_GetBitmapFlags (void)
3335 {
3336     return (GetDeviceCaps (0,