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