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

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

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