wined3d: Use wined3d_bit_scan() in context_preload_textures().
[wine.git] / dlls / comctl32 / toolbar.c
blob2ae70ad4f3ea43b15c32f82df1daf51ae27d6479
1 /*
2 * Toolbar control
4 * Copyright 1998,1999 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2004 Robert Shearman
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.
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.
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
22 * NOTES
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.
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.
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.
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
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)
71 #include <stdarg.h>
72 #include <string.h>
74 #include "windef.h"
75 #include "winbase.h"
76 #include "winreg.h"
77 #include "wingdi.h"
78 #include "winuser.h"
79 #include "winnls.h"
80 #include "commctrl.h"
81 #include "comctl32.h"
82 #include "uxtheme.h"
83 #include "vssym32.h"
84 #include "wine/debug.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
88 static HCURSOR hCursorDrag = NULL;
90 typedef struct
92 INT iBitmap;
93 INT idCommand;
94 BYTE fsState;
95 BYTE fsStyle;
96 BOOL bHot;
97 BOOL bDropDownPressed;
98 DWORD_PTR dwData;
99 INT_PTR iString;
100 INT nRow;
101 RECT rect;
102 INT cx; /* manually set size */
103 } TBUTTON_INFO;
105 typedef struct
107 UINT nButtons;
108 HINSTANCE hInst;
109 UINT nID;
110 } TBITMAP_INFO;
112 typedef struct
114 HIMAGELIST himl;
115 INT id;
116 } IMLENTRY, *PIMLENTRY;
118 typedef struct
120 DWORD dwStructSize; /* size of TBBUTTON struct */
121 RECT client_rect;
122 RECT rcBound; /* bounding rectangle */
123 INT nButtonHeight;
124 INT nButtonWidth;
125 INT nBitmapHeight;
126 INT nBitmapWidth;
127 INT nIndent;
128 INT nRows; /* number of button rows */
129 INT nMaxTextRows; /* maximum number of text rows */
130 INT cxMin; /* minimum button width */
131 INT cxMax; /* maximum button width */
132 INT nNumButtons; /* number of buttons */
133 INT nNumBitmaps; /* number of bitmaps */
134 INT nNumStrings; /* number of strings */
135 INT nNumBitmapInfos;
136 INT nButtonDown; /* toolbar button being pressed or -1 if none */
137 INT nButtonDrag; /* toolbar button being dragged or -1 if none */
138 INT nOldHit;
139 INT nHotItem; /* index of the "hot" item */
140 SIZE szPadding; /* padding values around button */
141 INT iTopMargin; /* the top margin */
142 INT iListGap; /* default gap between text and image for toolbar with list style */
143 HFONT hDefaultFont;
144 HFONT hFont; /* text font */
145 HTHEME hTheme; /* theme */
146 HIMAGELIST himlInt; /* image list created internally */
147 PIMLENTRY *himlDef; /* default image list array */
148 INT cimlDef; /* default image list array count */
149 PIMLENTRY *himlHot; /* hot image list array */
150 INT cimlHot; /* hot image list array count */
151 PIMLENTRY *himlDis; /* disabled image list array */
152 INT cimlDis; /* disabled image list array count */
153 HWND hwndToolTip; /* handle to tool tip control */
154 HWND hwndNotify; /* handle to the window that gets notifications */
155 HWND hwndSelf; /* my own handle */
156 BOOL bAnchor; /* anchor highlight enabled */
157 BOOL bDoRedraw; /* Redraw status */
158 BOOL bDragOutSent; /* has TBN_DRAGOUT notification been sent for this drag? */
159 BOOL bUnicode; /* Notifications are ANSI (FALSE) or Unicode (TRUE)? */
160 BOOL bCaptured; /* mouse captured? */
161 DWORD dwStyle; /* regular toolbar style */
162 DWORD dwExStyle; /* extended toolbar style */
163 DWORD dwDTFlags; /* DrawText flags */
165 COLORREF clrInsertMark; /* insert mark color */
166 COLORREF clrBtnHighlight; /* color for Flat Separator */
167 COLORREF clrBtnShadow; /* color for Flag Separator */
168 INT iVersion;
169 LPWSTR pszTooltipText; /* temporary store for a string > 80 characters
170 * for TTN_GETDISPINFOW notification */
171 TBINSERTMARK tbim; /* info on insertion mark */
172 TBUTTON_INFO *buttons; /* pointer to button array */
173 LPWSTR *strings; /* pointer to string array */
174 TBITMAP_INFO *bitmaps;
175 } TOOLBAR_INFO, *PTOOLBAR_INFO;
178 /* used by customization dialog */
179 typedef struct
181 PTOOLBAR_INFO tbInfo;
182 HWND tbHwnd;
183 } CUSTDLG_INFO, *PCUSTDLG_INFO;
185 typedef struct
187 TBBUTTON btn;
188 BOOL bVirtual;
189 BOOL bRemovable;
190 WCHAR text[64];
191 } CUSTOMBUTTON, *PCUSTOMBUTTON;
193 typedef enum
195 IMAGE_LIST_DEFAULT,
196 IMAGE_LIST_HOT,
197 IMAGE_LIST_DISABLED
198 } IMAGE_LIST_TYPE;
200 #define SEPARATOR_WIDTH 8
201 #define TOP_BORDER 2
202 #define BOTTOM_BORDER 2
203 #define DDARROW_WIDTH 11
204 #define ARROW_HEIGHT 3
205 #define INSERTMARK_WIDTH 2
207 #define DEFPAD_CX 7
208 #define DEFPAD_CY 6
209 #define DEFLISTGAP 4
211 /* vertical padding used in list mode when image is present */
212 #define LISTPAD_CY 9
214 /* how wide to treat the bitmap if it isn't present */
215 #define NONLIST_NOTEXT_OFFSET 2
217 #define TOOLBAR_NOWHERE (-1)
219 /* Used to find undocumented extended styles */
220 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
221 TBSTYLE_EX_VERTICAL | \
222 TBSTYLE_EX_MIXEDBUTTONS | \
223 TBSTYLE_EX_DOUBLEBUFFER | \
224 TBSTYLE_EX_HIDECLIPPEDBUTTONS)
226 /* all of the CCS_ styles */
227 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
228 CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
230 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
231 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
232 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
233 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
234 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
236 static const WCHAR themeClass[] = L"Toolbar";
238 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
239 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
240 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
241 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
242 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
243 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
244 static LRESULT TOOLBAR_LButtonDown(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
245 static void TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr);
246 static LRESULT TOOLBAR_AutoSize(TOOLBAR_INFO *infoPtr);
247 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
248 static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
249 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
250 static LRESULT TOOLBAR_SetButtonInfo(TOOLBAR_INFO *infoPtr, INT Id,
251 const TBBUTTONINFOW *lptbbi, BOOL isW);
254 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
256 return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
259 static inline BOOL TOOLBAR_HasDropDownArrows(DWORD exStyle)
261 return (exStyle & TBSTYLE_EX_DRAWDDARROWS) != 0;
264 static inline BOOL button_has_ddarrow(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
266 return (TOOLBAR_HasDropDownArrows( infoPtr->dwExStyle ) && (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
267 (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
270 static LPWSTR
271 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
273 LPWSTR lpText = NULL;
275 /* NOTE: iString == -1 is undocumented */
276 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
277 lpText = (LPWSTR)btnPtr->iString;
278 else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
279 lpText = infoPtr->strings[btnPtr->iString];
281 return lpText;
284 static void
285 TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
287 TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%p, stringid=%p (%s)\n", tbb->idCommand,
288 tbb->iBitmap, tbb->fsState, tbb->fsStyle, (void *)tbb->dwData, (void *)tbb->iString,
289 tbb->iString != -1 ? (fUnicode ? debugstr_w((LPWSTR)tbb->iString) : debugstr_a((LPSTR)tbb->iString)) : "");
292 static void
293 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
295 if (TRACE_ON(toolbar)){
296 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
297 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
298 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
299 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
300 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
301 btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
302 wine_dbgstr_rect(&bP->rect));
307 static void
308 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
310 if (TRACE_ON(toolbar)) {
311 INT i;
313 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
314 iP->hwndSelf, line,
315 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
316 iP->nNumStrings, iP->dwStyle);
317 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
318 iP->hwndSelf, line,
319 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
320 (iP->bDoRedraw) ? "TRUE" : "FALSE");
321 for(i=0; i<iP->nNumButtons; i++) {
322 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
327 static inline BOOL
328 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
330 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
333 static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
335 if (!IS_INTRESOURCE( str ) && str != -1)
337 if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;
339 if (unicode)
340 Str_SetPtrW( (WCHAR **)&btn->iString, (WCHAR *)str );
341 else
342 Str_SetPtrAtoW( (WCHAR **)&btn->iString, (char *)str );
344 else
346 if (TOOLBAR_ButtonHasString( btn )) Free( (WCHAR *)btn->iString );
348 btn->iString = str;
352 static void set_stringT( TBUTTON_INFO *btn, const WCHAR *str, BOOL unicode )
354 if (IS_INTRESOURCE( (DWORD_PTR)str ) || (DWORD_PTR)str == -1) return;
355 set_string_index( btn, (DWORD_PTR)str, unicode );
358 static void free_string( TBUTTON_INFO *btn )
360 set_string_index( btn, 0, TRUE );
364 /***********************************************************************
365 * TOOLBAR_CheckStyle
367 * This function validates that the styles set are implemented and
368 * issues FIXMEs warning of possible problems. In a perfect world this
369 * function should be null.
371 static void
372 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
374 if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
375 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
379 static INT
380 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
382 if(!IsWindow(infoPtr->hwndSelf))
383 return 0; /* we have just been destroyed */
385 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
386 nmhdr->hwndFrom = infoPtr->hwndSelf;
387 nmhdr->code = code;
389 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
390 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
392 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
395 /***********************************************************************
396 * TOOLBAR_GetBitmapIndex
398 * This function returns the bitmap index associated with a button.
399 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
400 * is issued to retrieve the index.
402 static INT
403 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
405 INT ret = btnPtr->iBitmap;
407 if (ret == I_IMAGECALLBACK)
409 /* issue TBN_GETDISPINFO */
410 NMTBDISPINFOW nmgd;
412 memset(&nmgd, 0, sizeof(nmgd));
413 nmgd.idCommand = btnPtr->idCommand;
414 nmgd.lParam = btnPtr->dwData;
415 nmgd.dwMask = TBNF_IMAGE;
416 nmgd.iImage = -1;
417 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
418 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
419 if (nmgd.dwMask & TBNF_DI_SETITEM)
420 btnPtr->iBitmap = nmgd.iImage;
421 ret = nmgd.iImage;
422 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
423 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
426 if (ret != I_IMAGENONE)
427 ret = GETIBITMAP(infoPtr, ret);
429 return ret;
433 static BOOL
434 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
436 HIMAGELIST himl;
437 INT id = GETHIMLID(infoPtr, index);
438 INT iBitmap = GETIBITMAP(infoPtr, index);
440 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
441 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
442 (index == I_IMAGECALLBACK))
443 return TRUE;
444 else
445 return FALSE;
449 static inline BOOL
450 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
452 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
453 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
457 /***********************************************************************
458 * TOOLBAR_GetImageListForDrawing
460 * This function validates the bitmap index (including I_IMAGECALLBACK
461 * functionality) and returns the corresponding image list.
463 static HIMAGELIST
464 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
465 IMAGE_LIST_TYPE imagelist, INT * index)
467 HIMAGELIST himl;
469 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
470 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
471 WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
472 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
473 return NULL;
476 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
477 if ((*index == I_IMAGECALLBACK) ||
478 (*index == I_IMAGENONE)) return NULL;
479 ERR("TBN_GETDISPINFO returned invalid index %d\n",
480 *index);
481 return NULL;
484 switch(imagelist)
486 case IMAGE_LIST_DEFAULT:
487 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
488 break;
489 case IMAGE_LIST_HOT:
490 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
491 break;
492 case IMAGE_LIST_DISABLED:
493 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
494 break;
495 default:
496 himl = NULL;
497 FIXME("Shouldn't reach here\n");
500 if (!himl)
501 TRACE("no image list\n");
503 return himl;
507 static void
508 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
510 RECT myrect;
511 COLORREF oldcolor, newcolor;
513 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
514 myrect.right = myrect.left + 1;
515 myrect.top = lpRect->top + 2;
516 myrect.bottom = lpRect->bottom - 2;
518 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
519 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
520 oldcolor = SetBkColor (hdc, newcolor);
521 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
523 myrect.left = myrect.right;
524 myrect.right = myrect.left + 1;
526 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
527 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
528 SetBkColor (hdc, newcolor);
529 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
531 SetBkColor (hdc, oldcolor);
535 /***********************************************************************
536 * TOOLBAR_DrawFlatHorizontalSeparator
538 * This function draws horizontal separator for toolbars having CCS_VERT style.
539 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
540 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
541 * are horizontal as opposed to the vertical separators for not dropdown
542 * type.
544 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
546 static void
547 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
548 const TOOLBAR_INFO *infoPtr)
550 RECT myrect;
551 COLORREF oldcolor, newcolor;
553 myrect.left = lpRect->left;
554 myrect.right = lpRect->right;
555 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
556 myrect.bottom = myrect.top + 1;
558 InflateRect (&myrect, -2, 0);
560 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
562 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
563 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
564 oldcolor = SetBkColor (hdc, newcolor);
565 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
567 myrect.top = myrect.bottom;
568 myrect.bottom = myrect.top + 1;
570 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
571 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
572 SetBkColor (hdc, newcolor);
573 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
575 SetBkColor (hdc, oldcolor);
579 static void
580 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
582 INT x, y;
583 HPEN hPen, hOldPen;
585 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
586 hOldPen = SelectObject ( hdc, hPen );
587 x = left + 2;
588 y = top;
589 MoveToEx (hdc, x, y, NULL);
590 LineTo (hdc, x+5, y++); x++;
591 MoveToEx (hdc, x, y, NULL);
592 LineTo (hdc, x+3, y++); x++;
593 MoveToEx (hdc, x, y, NULL);
594 LineTo (hdc, x+1, y);
595 SelectObject( hdc, hOldPen );
596 DeleteObject( hPen );
600 * Draw the text string for this button.
601 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
602 * is non-zero, so we can simply check himlDef to see if we have
603 * an image list
605 static void
606 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
607 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
609 HDC hdc = tbcd->nmcd.hdc;
610 HFONT hOldFont = 0;
611 COLORREF clrOld = 0;
612 COLORREF clrOldBk = 0;
613 int oldBkMode = 0;
614 UINT state = tbcd->nmcd.uItemState;
616 /* draw text */
617 if (lpText && infoPtr->nMaxTextRows > 0) {
618 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
619 wine_dbgstr_rect(rcText));
621 hOldFont = SelectObject (hdc, infoPtr->hFont);
622 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
623 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
625 else if (state & CDIS_DISABLED) {
626 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
627 OffsetRect (rcText, 1, 1);
628 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
629 SetTextColor (hdc, comctl32_color.clr3dShadow);
630 OffsetRect (rcText, -1, -1);
632 else if (state & CDIS_INDETERMINATE) {
633 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
635 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
636 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
637 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
638 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
640 else {
641 clrOld = SetTextColor (hdc, tbcd->clrText);
644 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
645 SetTextColor (hdc, clrOld);
646 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
648 SetBkColor (hdc, clrOldBk);
649 SetBkMode (hdc, oldBkMode);
651 SelectObject (hdc, hOldFont);
656 static void
657 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
659 HDC hdc = tbcd->nmcd.hdc;
660 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
661 COLORREF clrTextOld;
662 COLORREF clrBkOld;
663 INT cx = lpRect->right - lpRect->left;
664 INT cy = lpRect->bottom - lpRect->top;
665 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
666 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
667 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
668 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
669 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
670 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
671 SetBkColor(hdc, clrBkOld);
672 SetTextColor(hdc, clrTextOld);
673 SelectObject (hdc, hbr);
677 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
679 IMAGELISTDRAWPARAMS draw_params = { 0 };
680 INT cx, cy;
681 HBITMAP hbmMask, hbmImage;
682 HDC hdcMask, hdcImage;
684 ImageList_GetIconSize(himl, &cx, &cy);
686 draw_params.cbSize = sizeof(draw_params);
687 draw_params.himl = himl;
688 draw_params.i = index;
689 draw_params.hdcDst = hdc;
690 draw_params.x = x;
691 draw_params.y = y;
692 draw_params.cx = cx;
693 draw_params.cy = cy;
694 draw_params.rgbBk = CLR_NONE;
695 draw_params.rgbFg = CLR_NONE;
696 draw_params.fStyle = draw_flags;
697 draw_params.fState = ILS_NORMAL;
699 /* 32bpp image with alpha channel is converted to grayscale */
700 if (imagelist_has_alpha(himl, index))
702 draw_params.fState = ILS_SATURATE;
703 ImageList_DrawIndirect(&draw_params);
704 return;
707 /* Create src image */
708 hdcImage = CreateCompatibleDC(hdc);
709 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
710 SelectObject(hdcImage, hbmImage);
712 draw_params.x = 0;
713 draw_params.y = 0;
714 draw_params.rgbBk = RGB(0xff, 0xff, 0xff);
715 draw_params.rgbFg = RGB(0,0,0);
716 draw_params.hdcDst = hdcImage;
717 ImageList_DrawIndirect(&draw_params);
719 /* Create Mask */
720 hdcMask = CreateCompatibleDC(0);
721 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
722 SelectObject(hdcMask, hbmMask);
724 /* Remove the background and all white pixels */
725 draw_params.fStyle = ILD_MASK;
726 draw_params.hdcDst = hdcMask;
727 ImageList_DrawIndirect(&draw_params);
728 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
729 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
731 /* draw the new mask 'etched' to hdc */
732 SetBkColor(hdc, RGB(255, 255, 255));
733 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
734 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
735 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
736 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
737 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
739 /* Cleanup */
740 DeleteObject(hbmImage);
741 DeleteDC(hdcImage);
742 DeleteObject (hbmMask);
743 DeleteDC(hdcMask);
747 static UINT
748 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr, BOOL captured)
750 UINT retstate = 0;
752 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
753 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
754 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
755 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
756 retstate |= (btnPtr->bHot & !captured ) ? CDIS_HOT : 0;
757 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
758 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
759 return retstate;
762 /* draws the image on a toolbar button */
763 static void
764 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
765 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
767 HIMAGELIST himl = NULL;
768 BOOL draw_masked = FALSE;
769 INT index;
770 INT offset = 0;
771 UINT draw_flags = ILD_TRANSPARENT;
773 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
775 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
776 if (!himl)
778 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
779 draw_masked = TRUE;
782 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
783 ((tbcd->nmcd.uItemState & CDIS_HOT)
784 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme)))
786 /* if hot, attempt to draw with hot image list, if fails,
787 use default image list */
788 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
789 if (!himl)
790 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
792 else
793 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
795 if (!himl)
796 return;
798 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
799 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
800 offset = 1;
802 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
803 (tbcd->nmcd.uItemState & CDIS_MARKED))
804 draw_flags |= ILD_BLEND50;
806 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
807 index, himl, left, top, offset);
809 if (draw_masked)
810 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
811 else
812 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
815 /* draws a blank frame for a toolbar button */
816 static void
817 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
819 HDC hdc = tbcd->nmcd.hdc;
820 RECT rc = *rect;
821 /* if the state is disabled or indeterminate then the button
822 * cannot have an interactive look like pressed or hot */
823 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
824 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
825 BOOL pressed_look = !non_interactive_state &&
826 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
827 (tbcd->nmcd.uItemState & CDIS_CHECKED));
829 /* app don't want us to draw any edges */
830 if (dwItemCDFlag & TBCDRF_NOEDGES)
831 return;
833 if (infoPtr->dwStyle & TBSTYLE_FLAT)
835 if (pressed_look)
836 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
837 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
838 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
840 else
842 if (pressed_look)
843 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
844 else
845 DrawEdge (hdc, &rc, EDGE_RAISED,
846 BF_SOFT | BF_RECT | BF_MIDDLE);
850 static void
851 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
853 HDC hdc = tbcd->nmcd.hdc;
854 int offset = 0;
855 BOOL pressed = bDropDownPressed ||
856 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
858 if (infoPtr->dwStyle & TBSTYLE_FLAT)
860 if (pressed)
861 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
862 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
863 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
864 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
865 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
867 else
869 if (pressed)
870 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
871 else
872 DrawEdge (hdc, rcArrow, EDGE_RAISED,
873 BF_SOFT | BF_RECT | BF_MIDDLE);
876 if (pressed)
877 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
879 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
881 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
882 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
884 else
885 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
888 /* draws a complete toolbar button */
889 static void
890 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
892 DWORD dwStyle = infoPtr->dwStyle;
893 BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
894 BOOL drawSepDropDownArrow = hasDropDownArrow &&
895 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
896 RECT rc, rcArrow, rcBitmap, rcText;
897 LPWSTR lpText = NULL;
898 NMTBCUSTOMDRAW tbcd;
899 DWORD ntfret;
900 INT offset;
901 INT oldBkMode;
902 DWORD dwItemCustDraw;
903 DWORD dwItemCDFlag;
904 HTHEME theme = infoPtr->hTheme;
906 rc = btnPtr->rect;
907 rcArrow = rc;
909 /* separator - doesn't send NM_CUSTOMDRAW */
910 if (btnPtr->fsStyle & BTNS_SEP) {
911 if (theme)
913 DrawThemeBackground (theme, hdc,
914 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
915 &rc, NULL);
917 else
918 /* with the FLAT style, iBitmap is the width and has already */
919 /* been taken into consideration in calculating the width */
920 /* so now we need to draw the vertical separator */
921 /* empirical tests show that iBitmap can/will be non-zero */
922 /* when drawing the vertical bar... */
923 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
924 if (dwStyle & CCS_VERT) {
925 RECT rcsep = rc;
926 InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
927 TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
929 else
930 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
932 else if (btnPtr->fsStyle != BTNS_SEP) {
933 FIXME("Draw some kind of separator: fsStyle=%x\n",
934 btnPtr->fsStyle);
936 return;
939 /* get a pointer to the text */
940 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
942 if (hasDropDownArrow)
944 int right;
946 if (dwStyle & TBSTYLE_FLAT)
947 right = max(rc.left, rc.right - DDARROW_WIDTH);
948 else
949 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
951 if (drawSepDropDownArrow)
952 rc.right = right;
954 rcArrow.left = right;
957 /* copy text & bitmap rects after adjusting for drop-down arrow
958 * so that text & bitmap is centered in the rectangle not containing
959 * the arrow */
960 rcText = rc;
961 rcBitmap = rc;
963 /* Center the bitmap horizontally and vertically */
964 if (dwStyle & TBSTYLE_LIST)
966 if (lpText &&
967 infoPtr->nMaxTextRows > 0 &&
968 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
969 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
970 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
971 else
972 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
974 else
975 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
977 rcBitmap.top += infoPtr->szPadding.cy / 2;
979 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
980 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
981 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
982 TRACE("Text=%s\n", debugstr_w(lpText));
983 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
985 /* calculate text position */
986 if (lpText)
988 InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
989 if (dwStyle & TBSTYLE_LIST)
991 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
993 else
995 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
996 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
997 else
998 rcText.top += infoPtr->szPadding.cy/2 + 2;
1002 /* Initialize fields in all cases, because we use these later
1003 * NOTE: applications can and do alter these to customize their
1004 * toolbars */
1005 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1006 tbcd.clrText = comctl32_color.clrBtnText;
1007 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
1008 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
1009 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
1010 tbcd.clrMark = comctl32_color.clrHighlight;
1011 tbcd.clrHighlightHotTrack = 0;
1012 tbcd.nStringBkMode = TRANSPARENT;
1013 tbcd.nHLStringBkMode = OPAQUE;
1014 tbcd.rcText.left = 0;
1015 tbcd.rcText.top = 0;
1016 tbcd.rcText.right = rcText.right - rc.left;
1017 tbcd.rcText.bottom = rcText.bottom - rc.top;
1018 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr, infoPtr->bCaptured);
1019 tbcd.nmcd.hdc = hdc;
1020 tbcd.nmcd.rc = btnPtr->rect;
1021 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
1023 /* FIXME: what are these used for? */
1024 tbcd.hbrLines = 0;
1025 tbcd.hpenLines = 0;
1027 /* Issue Item Prepaint notify */
1028 dwItemCustDraw = 0;
1029 dwItemCDFlag = 0;
1030 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1032 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1033 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1034 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1035 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1036 /* reset these fields so the user can't alter the behaviour like native */
1037 tbcd.nmcd.hdc = hdc;
1038 tbcd.nmcd.rc = btnPtr->rect;
1040 dwItemCustDraw = ntfret & 0xffff;
1041 dwItemCDFlag = ntfret & 0xffff0000;
1042 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1043 return;
1044 /* save the only part of the rect that the user can change */
1045 rcText.right = tbcd.rcText.right + rc.left;
1046 rcText.bottom = tbcd.rcText.bottom + rc.top;
1049 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1050 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1051 OffsetRect(&rcText, 1, 1);
1053 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
1054 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1055 TOOLBAR_DrawPattern (&rc, &tbcd);
1057 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || theme) && (tbcd.nmcd.uItemState & CDIS_HOT))
1059 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1061 COLORREF oldclr;
1063 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1064 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1065 if (hasDropDownArrow)
1066 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1067 SetBkColor(hdc, oldclr);
1071 if (theme)
1073 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1074 int stateId = TS_NORMAL;
1076 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1077 stateId = TS_DISABLED;
1078 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1079 stateId = TS_PRESSED;
1080 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1081 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_CHECKED;
1082 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1083 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1084 stateId = TS_HOT;
1086 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1088 else
1089 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1091 if (drawSepDropDownArrow)
1093 if (theme)
1095 int stateId = TS_NORMAL;
1097 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1098 stateId = TS_DISABLED;
1099 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1100 stateId = TS_PRESSED;
1101 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1102 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_CHECKED;
1103 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1104 stateId = TS_HOT;
1106 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1107 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1109 else
1110 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1113 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1114 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1115 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1116 SetBkMode (hdc, oldBkMode);
1118 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1120 if (hasDropDownArrow && !drawSepDropDownArrow)
1122 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1124 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1125 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1127 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1129 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1130 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1132 else
1133 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1136 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1138 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1139 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1145 static void
1146 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1148 TBUTTON_INFO *btnPtr;
1149 INT i;
1150 RECT rcTemp, rcClient;
1151 NMTBCUSTOMDRAW tbcd;
1152 DWORD ntfret;
1153 DWORD dwBaseCustDraw;
1155 /* the app has told us not to redraw the toolbar */
1156 if (!infoPtr->bDoRedraw)
1157 return;
1159 /* if imagelist belongs to the app, it can be changed
1160 by the app after setting it */
1161 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1163 infoPtr->nNumBitmaps = 0;
1164 for (i = 0; i < infoPtr->cimlDef; i++)
1165 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1168 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1170 /* change the imagelist icon size if we manage the list and it is necessary */
1171 TOOLBAR_CheckImageListIconSize(infoPtr);
1173 /* Send initial notify */
1174 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1175 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1176 tbcd.nmcd.hdc = hdc;
1177 tbcd.nmcd.rc = ps->rcPaint;
1178 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1179 dwBaseCustDraw = ntfret & 0xffff;
1181 GetClientRect(infoPtr->hwndSelf, &rcClient);
1183 /* redraw necessary buttons */
1184 btnPtr = infoPtr->buttons;
1185 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1187 BOOL bDraw;
1188 if (!RectVisible(hdc, &btnPtr->rect))
1189 continue;
1190 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1192 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1193 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1195 else
1196 bDraw = TRUE;
1197 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1198 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1199 if (bDraw)
1200 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1203 /* draw insert mark if required */
1204 if (infoPtr->tbim.iButton != -1)
1206 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1207 RECT rcInsertMark;
1208 rcInsertMark.top = rcButton.top;
1209 rcInsertMark.bottom = rcButton.bottom;
1210 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1211 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1212 else
1213 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1214 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1217 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1219 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1220 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1221 tbcd.nmcd.hdc = hdc;
1222 tbcd.nmcd.rc = ps->rcPaint;
1223 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1227 /***********************************************************************
1228 * TOOLBAR_MeasureString
1230 * This function gets the width and height of a string in pixels. This
1231 * is done first by using GetTextExtentPoint to get the basic width
1232 * and height. The DrawText is called with DT_CALCRECT to get the exact
1233 * width. The reason is because the text may have more than one "&" (or
1234 * prefix characters as M$ likes to call them). The prefix character
1235 * indicates where the underline goes, except for the string "&&" which
1236 * is reduced to a single "&". GetTextExtentPoint does not process these
1237 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1239 static void
1240 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1241 HDC hdc, LPSIZE lpSize)
1243 RECT myrect;
1245 lpSize->cx = 0;
1246 lpSize->cy = 0;
1248 if (infoPtr->nMaxTextRows > 0 &&
1249 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1250 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1251 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1253 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1255 if(lpText != NULL) {
1256 /* first get size of all the text */
1257 GetTextExtentPoint32W (hdc, lpText, lstrlenW (lpText), lpSize);
1259 /* feed above size into the rectangle for DrawText */
1260 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1262 /* Use DrawText to get true size as drawn (less pesky "&") */
1263 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1264 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1265 DT_NOPREFIX : 0));
1267 /* feed back to caller */
1268 lpSize->cx = myrect.right;
1269 lpSize->cy = myrect.bottom;
1273 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1276 /***********************************************************************
1277 * TOOLBAR_CalcStrings
1279 * This function walks through each string and measures it and returns
1280 * the largest height and width to caller.
1282 static void
1283 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1285 TBUTTON_INFO *btnPtr;
1286 INT i;
1287 SIZE sz;
1288 HDC hdc;
1289 HFONT hOldFont;
1291 lpSize->cx = 0;
1292 lpSize->cy = 0;
1294 if (infoPtr->nMaxTextRows == 0)
1295 return;
1297 hdc = GetDC (infoPtr->hwndSelf);
1298 hOldFont = SelectObject (hdc, infoPtr->hFont);
1300 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1302 TEXTMETRICW tm;
1304 GetTextMetricsW(hdc, &tm);
1305 lpSize->cy = tm.tmHeight;
1308 btnPtr = infoPtr->buttons;
1309 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1310 if(TOOLBAR_GetText(infoPtr, btnPtr))
1312 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1313 if (sz.cx > lpSize->cx)
1314 lpSize->cx = sz.cx;
1315 if (sz.cy > lpSize->cy)
1316 lpSize->cy = sz.cy;
1320 SelectObject (hdc, hOldFont);
1321 ReleaseDC (infoPtr->hwndSelf, hdc);
1323 TRACE("max string size %d x %d\n", lpSize->cx, lpSize->cy);
1326 /***********************************************************************
1327 * TOOLBAR_WrapToolbar
1329 * This function walks through the buttons and separators in the
1330 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1331 * wrapping should occur based on the width of the toolbar window.
1332 * It does *not* calculate button placement itself. That task
1333 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1334 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1335 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1337 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1338 * vertical toolbar lists.
1341 static void
1342 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1344 TBUTTON_INFO *btnPtr;
1345 INT x, cx, i, j, width;
1346 BOOL bButtonWrap;
1348 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1349 /* no layout is necessary. Applications may use this style */
1350 /* to perform their own layout on the toolbar. */
1351 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1352 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1354 btnPtr = infoPtr->buttons;
1355 x = infoPtr->nIndent;
1356 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1358 bButtonWrap = FALSE;
1360 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1361 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1362 infoPtr->nIndent);
1364 for (i = 0; i < infoPtr->nNumButtons; i++ )
1366 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1368 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1369 continue;
1371 if (btnPtr[i].cx > 0)
1372 cx = btnPtr[i].cx;
1373 /* horizontal separators are treated as buttons for width */
1374 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1375 !(infoPtr->dwStyle & CCS_VERT))
1376 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1377 else
1378 cx = infoPtr->nButtonWidth;
1380 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1381 cx += DDARROW_WIDTH;
1383 /* Two or more adjacent separators form a separator group. */
1384 /* The first separator in a group should be wrapped to the */
1385 /* next row if the previous wrapping is on a button. */
1386 if( bButtonWrap &&
1387 (btnPtr[i].fsStyle & BTNS_SEP) &&
1388 (i + 1 < infoPtr->nNumButtons ) &&
1389 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1391 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1392 btnPtr[i].fsState |= TBSTATE_WRAP;
1393 x = infoPtr->nIndent;
1394 i++;
1395 bButtonWrap = FALSE;
1396 continue;
1399 /* The layout makes sure the bitmap is visible, but not the button. */
1400 /* Test added to also wrap after a button that starts a row but */
1401 /* is bigger than the area. - GA 8/01 */
1402 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1403 ((x == infoPtr->nIndent) && (cx > width)))
1405 BOOL bFound = FALSE;
1407 /* If the current button is a separator and not hidden, */
1408 /* go to the next until it reaches a non separator. */
1409 /* Wrap the last separator if it is before a button. */
1410 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1411 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1412 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1413 i < infoPtr->nNumButtons )
1415 i++;
1416 bFound = TRUE;
1419 if( bFound && i < infoPtr->nNumButtons )
1421 i--;
1422 TRACE("wrap point 2 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 bButtonWrap = FALSE;
1427 continue;
1429 else if ( i >= infoPtr->nNumButtons)
1430 break;
1432 /* If the current button is not a separator, find the last */
1433 /* separator and wrap it. */
1434 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1436 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1437 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1439 bFound = TRUE;
1440 i = j;
1441 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1442 i, btnPtr[i].fsStyle, x, cx);
1443 x = infoPtr->nIndent;
1444 btnPtr[j].fsState |= TBSTATE_WRAP;
1445 bButtonWrap = FALSE;
1446 break;
1450 /* If no separator available for wrapping, wrap one of */
1451 /* non-hidden previous button. */
1452 if (!bFound)
1454 for ( j = i - 1;
1455 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1457 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1458 continue;
1460 bFound = TRUE;
1461 i = j;
1462 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1463 i, btnPtr[i].fsStyle, x, cx);
1464 x = infoPtr->nIndent;
1465 btnPtr[j].fsState |= TBSTATE_WRAP;
1466 bButtonWrap = TRUE;
1467 break;
1471 /* If all above failed, wrap the current button. */
1472 if (!bFound)
1474 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1475 i, btnPtr[i].fsStyle, x, cx);
1476 btnPtr[i].fsState |= TBSTATE_WRAP;
1477 x = infoPtr->nIndent;
1478 if (btnPtr[i].fsStyle & BTNS_SEP )
1479 bButtonWrap = FALSE;
1480 else
1481 bButtonWrap = TRUE;
1484 else {
1485 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1486 i, btnPtr[i].fsStyle, x, cx);
1487 x += cx;
1493 /***********************************************************************
1494 * TOOLBAR_MeasureButton
1496 * Calculates the width and height required for a button. Used in
1497 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1498 * the width of buttons that are autosized.
1500 * Note that it would have been rather elegant to use one piece of code for
1501 * both the laying out of the toolbar and for controlling where button parts
1502 * are drawn, but the native control has inconsistencies between the two that
1503 * prevent this from being effectively. These inconsistencies can be seen as
1504 * artefacts where parts of the button appear outside of the bounding button
1505 * rectangle.
1507 * There are several cases for the calculation of the button dimensions and
1508 * button part positioning:
1510 * List
1511 * ====
1513 * With Bitmap:
1515 * +--------------------------------------------------------+ ^
1516 * | ^ ^ | |
1517 * | | pad.cy / 2 | centered | |
1518 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1519 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1520 * | |<------------>| | | | |
1521 * | +--------------+ +------------+ | |
1522 * |<-------------------------------------->| | |
1523 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1524 * | szText.cx | |
1525 * +--------------------------------------------------------+ -
1526 * <-------------------------------------------------------->
1527 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1529 * Without Bitmap (I_IMAGENONE):
1531 * +-----------------------------------+ ^
1532 * | ^ | |
1533 * | | centered | | LISTPAD_CY +
1534 * | +------------+ | | szText.cy
1535 * | | Text | | |
1536 * | | | | |
1537 * | +------------+ | |
1538 * |<----------------->| | |
1539 * | cxedge |<-----------> | |
1540 * | szText.cx | |
1541 * +-----------------------------------+ -
1542 * <----------------------------------->
1543 * szText.cx + pad.cx
1545 * Without text:
1547 * +--------------------------------------+ ^
1548 * | ^ | |
1549 * | | padding.cy/2 | | DEFPAD_CY +
1550 * | +------------+ | | nBitmapHeight
1551 * | | Bitmap | | |
1552 * | | | | |
1553 * | +------------+ | |
1554 * |<------------------->| | |
1555 * | cxedge + iListGap/2 |<-----------> | |
1556 * | nBitmapWidth | |
1557 * +--------------------------------------+ -
1558 * <-------------------------------------->
1559 * 2*cxedge + nBitmapWidth + iListGap
1561 * Non-List
1562 * ========
1564 * With bitmap:
1566 * +-----------------------------------+ ^
1567 * | ^ | |
1568 * | | pad.cy / 2 | | nBitmapHeight +
1569 * | - | | szText.cy +
1570 * | +------------+ | | DEFPAD_CY + 1
1571 * | centered | Bitmap | | |
1572 * |<----------------->| | | |
1573 * | +------------+ | |
1574 * | ^ | |
1575 * | 1 | | |
1576 * | - | |
1577 * | centered +---------------+ | |
1578 * |<--------------->| Text | | |
1579 * | +---------------+ | |
1580 * +-----------------------------------+ -
1581 * <----------------------------------->
1582 * pad.cx + max(nBitmapWidth, szText.cx)
1584 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1586 * +---------------------------------------+ ^
1587 * | ^ | |
1588 * | | 2 + pad.cy / 2 | |
1589 * | - | | szText.cy +
1590 * | centered +-----------------+ | | pad.cy + 2
1591 * |<--------------->| Text | | |
1592 * | +-----------------+ | |
1593 * | | |
1594 * +---------------------------------------+ -
1595 * <--------------------------------------->
1596 * 2*cxedge + pad.cx + szText.cx
1598 * Without text:
1599 * As for with bitmaps, but with szText.cx zero.
1601 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1602 BOOL bHasBitmap, BOOL bValidImageList)
1604 SIZE sizeButton;
1605 if (infoPtr->dwStyle & TBSTYLE_LIST)
1607 /* set button height from bitmap / text height... */
1608 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1609 sizeString.cy);
1611 /* ... add on the necessary padding */
1612 if (bValidImageList)
1614 if (bHasBitmap)
1615 sizeButton.cy += DEFPAD_CY;
1616 else
1617 sizeButton.cy += LISTPAD_CY;
1619 else
1620 sizeButton.cy += infoPtr->szPadding.cy;
1622 /* calculate button width */
1623 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1624 infoPtr->nBitmapWidth + infoPtr->iListGap;
1625 if (sizeString.cx > 0)
1626 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1629 else
1631 if (bHasBitmap)
1633 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1634 if (sizeString.cy > 0)
1635 sizeButton.cy += 1 + sizeString.cy;
1636 sizeButton.cx = infoPtr->szPadding.cx +
1637 max(sizeString.cx, infoPtr->nBitmapWidth);
1639 else
1641 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1642 NONLIST_NOTEXT_OFFSET;
1643 sizeButton.cx = infoPtr->szPadding.cx +
1644 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1647 return sizeButton;
1651 /***********************************************************************
1652 * TOOLBAR_CalcToolbar
1654 * This function calculates button and separator placement. It first
1655 * calculates the button sizes, gets the toolbar window width and then
1656 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1657 * on. It assigns a new location to each item and sends this location to
1658 * the tooltip window if appropriate. Finally, it updates the rcBound
1659 * rect and calculates the new required toolbar window height.
1661 static void
1662 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1664 SIZE sizeString, sizeButton;
1665 BOOL validImageList = FALSE;
1667 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1669 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1671 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1672 validImageList = TRUE;
1673 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1674 infoPtr->nButtonWidth = sizeButton.cx;
1675 infoPtr->nButtonHeight = sizeButton.cy;
1676 infoPtr->iTopMargin = default_top_margin(infoPtr);
1678 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1679 infoPtr->nButtonWidth = infoPtr->cxMin;
1680 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1681 infoPtr->nButtonWidth = infoPtr->cxMax;
1683 TOOLBAR_LayoutToolbar(infoPtr);
1686 static void
1687 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1689 TBUTTON_INFO *btnPtr;
1690 SIZE sizeButton;
1691 INT i, nRows, nSepRows;
1692 INT x, y, cx, cy;
1693 BOOL bWrap;
1694 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1696 TOOLBAR_WrapToolbar(infoPtr);
1698 x = infoPtr->nIndent;
1699 y = infoPtr->iTopMargin;
1700 cx = infoPtr->nButtonWidth;
1701 cy = infoPtr->nButtonHeight;
1703 nRows = nSepRows = 0;
1705 infoPtr->rcBound.top = y;
1706 infoPtr->rcBound.left = x;
1707 infoPtr->rcBound.bottom = y + cy;
1708 infoPtr->rcBound.right = x;
1710 btnPtr = infoPtr->buttons;
1712 TRACE("cy=%d\n", cy);
1714 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1716 bWrap = FALSE;
1717 if (btnPtr->fsState & TBSTATE_HIDDEN)
1719 SetRectEmpty (&btnPtr->rect);
1720 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1721 continue;
1724 cy = infoPtr->nButtonHeight;
1726 if (btnPtr->fsStyle & BTNS_SEP) {
1727 if (infoPtr->dwStyle & CCS_VERT) {
1728 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1729 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1731 else
1732 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1733 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1735 else
1737 if (btnPtr->cx)
1738 cx = btnPtr->cx;
1739 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1741 SIZE sz;
1742 HDC hdc;
1743 HFONT hOldFont;
1745 hdc = GetDC (infoPtr->hwndSelf);
1746 hOldFont = SelectObject (hdc, infoPtr->hFont);
1748 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1750 SelectObject (hdc, hOldFont);
1751 ReleaseDC (infoPtr->hwndSelf, hdc);
1753 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1754 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1755 validImageList);
1756 cx = sizeButton.cx;
1758 else
1759 cx = infoPtr->nButtonWidth;
1761 /* if size has been set manually then don't add on extra space
1762 * for the drop down arrow */
1763 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1764 cx += DDARROW_WIDTH;
1766 if (btnPtr->fsState & TBSTATE_WRAP)
1767 bWrap = TRUE;
1769 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1771 if (infoPtr->rcBound.left > x)
1772 infoPtr->rcBound.left = x;
1773 if (infoPtr->rcBound.right < x + cx)
1774 infoPtr->rcBound.right = x + cx;
1775 if (infoPtr->rcBound.bottom < y + cy)
1776 infoPtr->rcBound.bottom = y + cy;
1778 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1780 /* btnPtr->nRow is zero based. The space between the rows is */
1781 /* also considered as a row. */
1782 btnPtr->nRow = nRows + nSepRows;
1784 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1785 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1786 x, y, x+cx, y+cy);
1788 if( bWrap )
1790 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1791 y += cy;
1792 else
1794 if ( !(infoPtr->dwStyle & CCS_VERT))
1795 y += cy + ( (btnPtr->cx > 0 ) ?
1796 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1797 else
1798 y += cy;
1800 /* nSepRows is used to calculate the extra height following */
1801 /* the last row. */
1802 nSepRows++;
1804 x = infoPtr->nIndent;
1806 /* Increment row number unless this is the last button */
1807 /* and it has Wrap set. */
1808 if (i != infoPtr->nNumButtons-1)
1809 nRows++;
1811 else
1812 x += cx;
1815 /* infoPtr->nRows is the number of rows on the toolbar */
1816 infoPtr->nRows = nRows + nSepRows + 1;
1818 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1822 static INT
1823 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1825 TBUTTON_INFO *btnPtr;
1826 INT i;
1828 if (button)
1829 *button = FALSE;
1831 btnPtr = infoPtr->buttons;
1832 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1833 if (btnPtr->fsState & TBSTATE_HIDDEN)
1834 continue;
1836 if (btnPtr->fsStyle & BTNS_SEP) {
1837 if (PtInRect (&btnPtr->rect, *lpPt)) {
1838 TRACE(" ON SEPARATOR %d\n", i);
1839 return -i;
1842 else {
1843 if (PtInRect (&btnPtr->rect, *lpPt)) {
1844 TRACE(" ON BUTTON %d\n", i);
1845 if (button)
1846 *button = TRUE;
1847 return i;
1852 TRACE(" NOWHERE\n");
1853 return TOOLBAR_NOWHERE;
1857 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1858 static BOOL
1859 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1861 INT nOldButtons, nNewButtons, iButton;
1862 BOOL fHasString = FALSE;
1864 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1865 iIndex = infoPtr->nNumButtons;
1867 nOldButtons = infoPtr->nNumButtons;
1868 nNewButtons = nOldButtons + nAddButtons;
1870 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1871 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1872 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1873 infoPtr->nNumButtons += nAddButtons;
1875 /* insert new buttons data */
1876 for (iButton = 0; iButton < nAddButtons; iButton++) {
1877 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1878 INT_PTR str;
1880 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1882 ZeroMemory(btnPtr, sizeof(*btnPtr));
1884 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1885 btnPtr->idCommand = lpTbb[iButton].idCommand;
1886 btnPtr->fsState = lpTbb[iButton].fsState;
1887 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1888 btnPtr->dwData = lpTbb[iButton].dwData;
1890 if (btnPtr->fsStyle & BTNS_SEP)
1891 str = -1;
1892 else
1893 str = lpTbb[iButton].iString;
1894 set_string_index( btnPtr, str, fUnicode );
1895 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1897 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1900 if (infoPtr->nNumStrings > 0 || fHasString)
1901 TOOLBAR_CalcToolbar(infoPtr);
1902 else
1903 TOOLBAR_LayoutToolbar(infoPtr);
1904 TOOLBAR_AutoSize(infoPtr);
1906 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1907 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1908 return TRUE;
1912 static INT
1913 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1915 TBUTTON_INFO *btnPtr;
1916 INT i;
1918 if (CommandIsIndex) {
1919 TRACE("command is really index command=%d\n", idCommand);
1920 if (idCommand >= infoPtr->nNumButtons) return -1;
1921 return idCommand;
1923 btnPtr = infoPtr->buttons;
1924 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1925 if (btnPtr->idCommand == idCommand) {
1926 TRACE("command=%d index=%d\n", idCommand, i);
1927 return i;
1930 TRACE("no index found for command=%d\n", idCommand);
1931 return -1;
1935 static INT
1936 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1938 TBUTTON_INFO *btnPtr;
1939 INT nRunIndex;
1941 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1942 return -1;
1944 /* check index button */
1945 btnPtr = &infoPtr->buttons[nIndex];
1946 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1947 if (btnPtr->fsState & TBSTATE_CHECKED)
1948 return nIndex;
1951 /* check previous buttons */
1952 nRunIndex = nIndex - 1;
1953 while (nRunIndex >= 0) {
1954 btnPtr = &infoPtr->buttons[nRunIndex];
1955 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1956 if (btnPtr->fsState & TBSTATE_CHECKED)
1957 return nRunIndex;
1959 else
1960 break;
1961 nRunIndex--;
1964 /* check next buttons */
1965 nRunIndex = nIndex + 1;
1966 while (nRunIndex < infoPtr->nNumButtons) {
1967 btnPtr = &infoPtr->buttons[nRunIndex];
1968 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1969 if (btnPtr->fsState & TBSTATE_CHECKED)
1970 return nRunIndex;
1972 else
1973 break;
1974 nRunIndex++;
1977 return -1;
1981 static VOID
1982 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1983 WPARAM wParam, LPARAM lParam)
1985 MSG msg;
1987 msg.hwnd = hwndMsg;
1988 msg.message = uMsg;
1989 msg.wParam = wParam;
1990 msg.lParam = lParam;
1991 msg.time = GetMessageTime ();
1992 msg.pt.x = (short)LOWORD(GetMessagePos ());
1993 msg.pt.y = (short)HIWORD(GetMessagePos ());
1995 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1998 static void
1999 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2001 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
2002 TTTOOLINFOW ti;
2004 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
2005 ti.cbSize = sizeof (TTTOOLINFOW);
2006 ti.hwnd = infoPtr->hwndSelf;
2007 ti.uId = button->idCommand;
2008 ti.hinst = 0;
2009 ti.lpszText = LPSTR_TEXTCALLBACKW;
2010 /* ti.lParam = random value from the stack? */
2012 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
2013 0, (LPARAM)&ti);
2017 static void
2018 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2020 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
2021 TTTOOLINFOW ti;
2023 ZeroMemory(&ti, sizeof(ti));
2024 ti.cbSize = sizeof(ti);
2025 ti.hwnd = infoPtr->hwndSelf;
2026 ti.uId = button->idCommand;
2028 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2032 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2034 /* Set the toolTip only for non-hidden, non-separator button */
2035 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2037 TTTOOLINFOW ti;
2039 ZeroMemory(&ti, sizeof(ti));
2040 ti.cbSize = sizeof(ti);
2041 ti.hwnd = infoPtr->hwndSelf;
2042 ti.uId = button->idCommand;
2043 ti.rect = button->rect;
2044 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2048 /* Creates the tooltip control */
2049 static void
2050 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2052 int i;
2053 NMTOOLTIPSCREATED nmttc;
2055 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2056 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2057 infoPtr->hwndSelf, 0, 0, 0);
2059 if (!infoPtr->hwndToolTip)
2060 return;
2062 /* Send NM_TOOLTIPSCREATED notification */
2063 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2064 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2066 for (i = 0; i < infoPtr->nNumButtons; i++)
2068 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2069 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2073 /* keeps available button list box sorted by button id */
2074 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2076 int i;
2077 int count;
2078 PCUSTOMBUTTON btnInfo;
2079 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2081 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2083 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2085 /* position 0 is always separator */
2086 for (i = 1; i < count; i++)
2088 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2089 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2091 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2092 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2093 return;
2096 /* id higher than all others add to end */
2097 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2098 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2101 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2103 NMTOOLBARW nmtb;
2105 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2107 if (nIndexFrom == nIndexTo)
2108 return;
2110 /* MSDN states that iItem is the index of the button, rather than the
2111 * command ID as used by every other NMTOOLBAR notification */
2112 nmtb.iItem = nIndexFrom;
2113 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2115 PCUSTOMBUTTON btnInfo;
2116 NMHDR hdr;
2117 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2118 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2120 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2122 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2123 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2124 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2125 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2127 if (nIndexTo <= 0)
2128 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2129 else
2130 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2132 /* last item is always separator, so -2 instead of -1 */
2133 if (nIndexTo >= (count - 2))
2134 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2135 else
2136 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2138 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2139 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2141 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2145 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2147 NMTOOLBARW nmtb;
2149 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2151 /* MSDN states that iItem is the index of the button, rather than the
2152 * command ID as used by every other NMTOOLBAR notification */
2153 nmtb.iItem = nIndexAvail;
2154 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2156 PCUSTOMBUTTON btnInfo;
2157 NMHDR hdr;
2158 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2159 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2160 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2162 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2164 if (nIndexAvail != 0) /* index == 0 indicates separator */
2166 /* remove from 'available buttons' list */
2167 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2168 if (nIndexAvail == count-1)
2169 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2170 else
2171 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2173 else
2175 PCUSTOMBUTTON btnNew;
2177 /* duplicate 'separator' button */
2178 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2179 *btnNew = *btnInfo;
2180 btnInfo = btnNew;
2183 /* insert into 'toolbar button' list */
2184 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2185 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2187 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2189 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2193 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2195 PCUSTOMBUTTON btnInfo;
2196 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2198 TRACE("Remove: index %d\n", index);
2200 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2202 /* send TBN_QUERYDELETE notification */
2203 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2205 NMHDR hdr;
2207 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2208 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2210 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2212 /* insert into 'available button' list */
2213 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2214 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2215 else
2216 Free(btnInfo);
2218 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2222 /* drag list notification function for toolbar buttons list box */
2223 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2224 const DRAGLISTINFO *pDLI)
2226 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2227 switch (pDLI->uNotification)
2229 case DL_BEGINDRAG:
2231 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2232 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2233 /* no dragging for last item (separator) */
2234 if (nCurrentItem >= (nCount - 1)) return FALSE;
2235 return TRUE;
2237 case DL_DRAGGING:
2239 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2240 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2241 /* no dragging past last item (separator) */
2242 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2244 DrawInsert(hwnd, hwndList, nCurrentItem);
2245 /* FIXME: native uses "move button" cursor */
2246 return DL_COPYCURSOR;
2249 /* not over toolbar buttons list */
2250 if (nCurrentItem < 0)
2252 POINT ptWindow = pDLI->ptCursor;
2253 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2254 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2255 /* over available buttons list? */
2256 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2257 /* FIXME: native uses "move button" cursor */
2258 return DL_COPYCURSOR;
2260 /* clear drag arrow */
2261 DrawInsert(hwnd, hwndList, -1);
2262 return DL_STOPCURSOR;
2264 case DL_DROPPED:
2266 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2267 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2268 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2269 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2271 /* clear drag arrow */
2272 DrawInsert(hwnd, hwndList, -1);
2273 /* move item */
2274 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2276 /* not over toolbar buttons list */
2277 if (nIndexTo < 0)
2279 POINT ptWindow = pDLI->ptCursor;
2280 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2281 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2282 /* over available buttons list? */
2283 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2284 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2286 break;
2288 case DL_CANCELDRAG:
2289 /* Clear drag arrow */
2290 DrawInsert(hwnd, hwndList, -1);
2291 break;
2294 return 0;
2297 /* drag list notification function for available buttons list box */
2298 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2299 const DRAGLISTINFO *pDLI)
2301 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2302 switch (pDLI->uNotification)
2304 case DL_BEGINDRAG:
2305 return TRUE;
2306 case DL_DRAGGING:
2308 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2309 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2310 /* no dragging past last item (separator) */
2311 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2313 DrawInsert(hwnd, hwndList, nCurrentItem);
2314 /* FIXME: native uses "move button" cursor */
2315 return DL_COPYCURSOR;
2318 /* not over toolbar buttons list */
2319 if (nCurrentItem < 0)
2321 POINT ptWindow = pDLI->ptCursor;
2322 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2323 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2324 /* over available buttons list? */
2325 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2326 /* FIXME: native uses "move button" cursor */
2327 return DL_COPYCURSOR;
2329 /* clear drag arrow */
2330 DrawInsert(hwnd, hwndList, -1);
2331 return DL_STOPCURSOR;
2333 case DL_DROPPED:
2335 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2336 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2337 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2338 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2340 /* clear drag arrow */
2341 DrawInsert(hwnd, hwndList, -1);
2342 /* add item */
2343 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2346 case DL_CANCELDRAG:
2347 /* Clear drag arrow */
2348 DrawInsert(hwnd, hwndList, -1);
2349 break;
2351 return 0;
2354 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2356 /***********************************************************************
2357 * TOOLBAR_CustomizeDialogProc
2358 * This function implements the toolbar customization dialog.
2360 static INT_PTR CALLBACK
2361 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2363 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2364 PCUSTOMBUTTON btnInfo;
2365 NMTOOLBARA nmtb;
2366 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2368 switch (uMsg)
2370 case WM_INITDIALOG:
2371 custInfo = (PCUSTDLG_INFO)lParam;
2372 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2374 if (custInfo)
2376 WCHAR Buffer[256];
2377 int i = 0;
2378 int index;
2379 NMTBINITCUSTOMIZE nmtbic;
2381 infoPtr = custInfo->tbInfo;
2383 /* send TBN_QUERYINSERT notification */
2384 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2386 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2387 return FALSE;
2389 nmtbic.hwndDialog = hwnd;
2390 /* Send TBN_INITCUSTOMIZE notification */
2391 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2392 TBNRF_HIDEHELP)
2394 TRACE("TBNRF_HIDEHELP requested\n");
2395 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2398 /* add items to 'toolbar buttons' list and check if removable */
2399 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2401 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2402 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2403 btnInfo->btn.fsStyle = BTNS_SEP;
2404 btnInfo->bVirtual = FALSE;
2405 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2407 /* send TBN_QUERYDELETE notification */
2408 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2410 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2411 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2414 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2416 /* insert separator button into 'available buttons' list */
2417 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2418 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2419 btnInfo->btn.fsStyle = BTNS_SEP;
2420 btnInfo->bVirtual = FALSE;
2421 btnInfo->bRemovable = TRUE;
2422 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2423 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2424 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2426 /* insert all buttons into dsa */
2427 for (i = 0;; i++)
2429 /* send TBN_GETBUTTONINFO notification */
2430 NMTOOLBARW nmtb;
2431 nmtb.iItem = i;
2432 nmtb.pszText = Buffer;
2433 nmtb.cchText = 256;
2435 /* Clear previous button's text */
2436 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2438 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2439 break;
2441 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2442 nmtb.tbButton.fsStyle, i,
2443 nmtb.tbButton.idCommand,
2444 nmtb.tbButton.iString,
2445 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2446 : "");
2448 /* insert button into the appropriate list */
2449 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2450 if (index == -1)
2452 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2453 btnInfo->bVirtual = FALSE;
2454 btnInfo->bRemovable = TRUE;
2456 else
2458 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2459 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2462 btnInfo->btn = nmtb.tbButton;
2463 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2465 if (*nmtb.pszText)
2466 lstrcpyW(btnInfo->text, nmtb.pszText);
2467 else if (nmtb.tbButton.iString >= 0 &&
2468 nmtb.tbButton.iString < infoPtr->nNumStrings)
2470 lstrcpyW(btnInfo->text,
2471 infoPtr->strings[nmtb.tbButton.iString]);
2475 if (index == -1)
2476 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2479 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2481 /* select first item in the 'available' list */
2482 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2484 /* append 'virtual' separator button to the 'toolbar buttons' list */
2485 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2486 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2487 btnInfo->btn.fsStyle = BTNS_SEP;
2488 btnInfo->bVirtual = TRUE;
2489 btnInfo->bRemovable = FALSE;
2490 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2491 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2492 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2494 /* select last item in the 'toolbar' list */
2495 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2496 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2498 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2499 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2501 /* set focus and disable buttons */
2502 PostMessageW (hwnd, WM_USER, 0, 0);
2504 return TRUE;
2506 case WM_USER:
2507 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2508 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2509 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2510 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2511 return TRUE;
2513 case WM_CLOSE:
2514 EndDialog(hwnd, FALSE);
2515 return TRUE;
2517 case WM_COMMAND:
2518 switch (LOWORD(wParam))
2520 case IDC_TOOLBARBTN_LBOX:
2521 if (HIWORD(wParam) == LBN_SELCHANGE)
2523 PCUSTOMBUTTON btnInfo;
2524 NMTOOLBARA nmtb;
2525 int count;
2526 int index;
2528 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2529 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2531 /* send TBN_QUERYINSERT notification */
2532 nmtb.iItem = index;
2533 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2535 /* get list box item */
2536 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2538 if (index == (count - 1))
2540 /* last item (virtual separator) */
2541 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2542 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2544 else if (index == (count - 2))
2546 /* second last item (last non-virtual item) */
2547 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2548 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2550 else if (index == 0)
2552 /* first item */
2553 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2554 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2556 else
2558 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2559 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2562 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2564 break;
2566 case IDC_MOVEUP_BTN:
2568 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2569 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2571 break;
2573 case IDC_MOVEDN_BTN: /* move down */
2575 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2576 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2578 break;
2580 case IDC_REMOVE_BTN: /* remove button */
2582 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2584 if (LB_ERR == index)
2585 break;
2587 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2589 break;
2590 case IDC_HELP_BTN:
2591 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2592 break;
2593 case IDC_RESET_BTN:
2594 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2595 break;
2597 case IDOK: /* Add button */
2599 int index;
2600 int indexto;
2602 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2603 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2605 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2607 break;
2609 case IDCANCEL:
2610 EndDialog(hwnd, FALSE);
2611 break;
2613 return TRUE;
2615 case WM_DESTROY:
2617 int count;
2618 int i;
2620 /* delete items from 'toolbar buttons' listbox*/
2621 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2622 for (i = 0; i < count; i++)
2624 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2625 Free(btnInfo);
2626 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2628 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2631 /* delete items from 'available buttons' listbox*/
2632 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2633 for (i = 0; i < count; i++)
2635 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2636 Free(btnInfo);
2637 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2639 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2641 return TRUE;
2643 case WM_DRAWITEM:
2644 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2646 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2647 RECT rcButton;
2648 RECT rcText;
2649 HPEN hPen, hOldPen;
2650 HBRUSH hOldBrush;
2651 COLORREF oldText = 0;
2652 COLORREF oldBk = 0;
2654 /* get item data */
2655 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2656 if (btnInfo == NULL)
2658 FIXME("btnInfo invalid\n");
2659 return TRUE;
2662 /* set colors and select objects */
2663 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2664 if (btnInfo->bVirtual)
2665 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2666 else
2667 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2668 hPen = CreatePen( PS_SOLID, 1,
2669 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2670 hOldPen = SelectObject (lpdis->hDC, hPen );
2671 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2673 /* fill background rectangle */
2674 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2675 lpdis->rcItem.right, lpdis->rcItem.bottom);
2677 /* calculate button and text rectangles */
2678 rcButton = lpdis->rcItem;
2679 InflateRect (&rcButton, -1, -1);
2680 rcText = rcButton;
2681 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2682 rcText.left = rcButton.right + 2;
2684 /* draw focus rectangle */
2685 if (lpdis->itemState & ODS_FOCUS)
2686 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2688 /* draw button */
2689 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2690 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2692 /* draw image and text */
2693 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2694 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2695 btnInfo->btn.iBitmap));
2696 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2697 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2699 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2700 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2702 /* delete objects and reset colors */
2703 SelectObject (lpdis->hDC, hOldBrush);
2704 SelectObject (lpdis->hDC, hOldPen);
2705 SetBkColor (lpdis->hDC, oldBk);
2706 SetTextColor (lpdis->hDC, oldText);
2707 DeleteObject( hPen );
2708 return TRUE;
2710 return FALSE;
2712 case WM_MEASUREITEM:
2713 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2715 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2717 lpmis->itemHeight = 15 + 8; /* default height */
2719 return TRUE;
2721 return FALSE;
2723 default:
2724 if (uDragListMessage && (uMsg == uDragListMessage))
2726 if (wParam == IDC_TOOLBARBTN_LBOX)
2728 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2729 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2730 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2731 return TRUE;
2733 else if (wParam == IDC_AVAILBTN_LBOX)
2735 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2736 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2737 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2738 return TRUE;
2741 return FALSE;
2745 static BOOL
2746 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2748 HBITMAP hbmLoad;
2749 INT nCountBefore = ImageList_GetImageCount(himlDef);
2750 INT nCountAfter;
2751 INT cxIcon, cyIcon;
2752 INT nAdded;
2753 INT nIndex;
2755 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2756 /* Add bitmaps to the default image list */
2757 if (bitmap->hInst == NULL) /* a handle was passed */
2758 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2759 else if (bitmap->hInst == COMCTL32_hModule)
2760 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2761 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2762 else
2763 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2765 /* enlarge the bitmap if needed */
2766 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2767 if (bitmap->hInst != COMCTL32_hModule)
2768 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2770 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2771 DeleteObject(hbmLoad);
2772 if (nIndex == -1)
2773 return FALSE;
2775 nCountAfter = ImageList_GetImageCount(himlDef);
2776 nAdded = nCountAfter - nCountBefore;
2777 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2779 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2780 } else if (nAdded > (INT)bitmap->nButtons) {
2781 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2782 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2785 infoPtr->nNumBitmaps += nAdded;
2786 return TRUE;
2789 static void
2790 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2792 HIMAGELIST himlDef;
2793 HIMAGELIST himlNew;
2794 INT cx, cy;
2795 INT i;
2797 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2798 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2799 return;
2800 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2801 return;
2802 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2803 return;
2805 TRACE("Update icon size: %dx%d -> %dx%d\n",
2806 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2808 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2809 ILC_COLOR32|ILC_MASK, 8, 2);
2810 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2811 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2812 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2813 infoPtr->himlInt = himlNew;
2815 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2816 ImageList_Destroy(himlDef);
2819 /***********************************************************************
2820 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2823 static LRESULT
2824 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2826 TBITMAP_INFO info;
2827 INT iSumButtons, i;
2828 HIMAGELIST himlDef;
2830 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2831 if (!lpAddBmp)
2832 return -1;
2834 if (lpAddBmp->hInst == HINST_COMMCTRL)
2836 info.hInst = COMCTL32_hModule;
2837 switch (lpAddBmp->nID)
2839 case IDB_STD_SMALL_COLOR:
2840 case 2:
2841 info.nButtons = 15;
2842 info.nID = IDB_STD_SMALL;
2843 break;
2844 case IDB_STD_LARGE_COLOR:
2845 case 3:
2846 info.nButtons = 15;
2847 info.nID = IDB_STD_LARGE;
2848 break;
2849 case IDB_VIEW_SMALL_COLOR:
2850 case 6:
2851 info.nButtons = 12;
2852 info.nID = IDB_VIEW_SMALL;
2853 break;
2854 case IDB_VIEW_LARGE_COLOR:
2855 case 7:
2856 info.nButtons = 12;
2857 info.nID = IDB_VIEW_LARGE;
2858 break;
2859 case IDB_HIST_SMALL_COLOR:
2860 info.nButtons = 5;
2861 info.nID = IDB_HIST_SMALL;
2862 break;
2863 case IDB_HIST_LARGE_COLOR:
2864 info.nButtons = 5;
2865 info.nID = IDB_HIST_LARGE;
2866 break;
2867 default:
2868 WARN("unknown bitmap id, %ld\n", lpAddBmp->nID);
2869 return -1;
2872 TRACE ("adding %d internal bitmaps\n", info.nButtons);
2874 /* Windows resize all the buttons to the size of a newly added standard image */
2875 if (lpAddBmp->nID & 1)
2877 /* large icons: 24x24. Will make the button 31x30 */
2878 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2880 else
2882 /* small icons: 16x16. Will make the buttons 23x22 */
2883 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2886 TOOLBAR_CalcToolbar (infoPtr);
2888 else
2890 info.nButtons = count;
2891 info.hInst = lpAddBmp->hInst;
2892 info.nID = lpAddBmp->nID;
2893 TRACE("adding %d bitmaps\n", info.nButtons);
2896 /* check if the bitmap is already loaded and compute iSumButtons */
2897 iSumButtons = 0;
2898 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2900 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2901 infoPtr->bitmaps[i].nID == info.nID)
2902 return iSumButtons;
2903 iSumButtons += infoPtr->bitmaps[i].nButtons;
2906 if (!infoPtr->cimlDef) {
2907 /* create new default image list */
2908 TRACE ("creating default image list\n");
2910 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2911 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2912 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2913 infoPtr->himlInt = himlDef;
2915 else {
2916 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2919 if (!himlDef) {
2920 WARN("No default image list available\n");
2921 return -1;
2924 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2925 return -1;
2927 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2928 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2929 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2930 infoPtr->nNumBitmapInfos++;
2931 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2933 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2934 return iSumButtons;
2938 static LRESULT
2939 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2941 TRACE("adding %d buttons (unicode=%d)\n", nAddButtons, fUnicode);
2943 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2947 static LRESULT
2948 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2950 #define MAX_RESOURCE_STRING_LENGTH 512
2951 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2952 INT nIndex = infoPtr->nNumStrings;
2954 TRACE("%p, %lx\n", hInstance, lParam);
2956 if (IS_INTRESOURCE(lParam)) {
2957 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2958 WCHAR delimiter;
2959 WCHAR *next_delim;
2960 HRSRC hrsrc;
2961 WCHAR *p;
2962 INT len;
2964 TRACE("adding string from resource\n");
2966 if (!hInstance) return -1;
2968 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2969 (LPWSTR)RT_STRING );
2970 if (!hrsrc)
2972 TRACE("string not found in resources\n");
2973 return -1;
2976 len = LoadStringW (hInstance, (UINT)lParam,
2977 szString, MAX_RESOURCE_STRING_LENGTH);
2979 TRACE("len=%d %s\n", len, debugstr_w(szString));
2980 if (len == 0 || len == 1)
2981 return nIndex;
2983 TRACE("delimiter: 0x%x\n", *szString);
2984 delimiter = *szString;
2985 p = szString + 1;
2987 while ((next_delim = wcschr(p, delimiter)) != NULL) {
2988 *next_delim = 0;
2989 if (next_delim + 1 >= szString + len)
2991 /* this may happen if delimiter == '\0' or if the last char is a
2992 * delimiter (then it is ignored like the native does) */
2993 break;
2996 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2997 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2998 infoPtr->nNumStrings++;
3000 p = next_delim + 1;
3003 else {
3004 LPWSTR p = (LPWSTR)lParam;
3005 INT len;
3007 if (p == NULL)
3008 return -1;
3009 TRACE("adding string(s) from array\n");
3010 while (*p) {
3011 len = lstrlenW (p);
3013 TRACE("len=%d %s\n", len, debugstr_w(p));
3014 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3015 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
3016 infoPtr->nNumStrings++;
3018 p += (len+1);
3022 if (fFirstString)
3023 TOOLBAR_CalcToolbar(infoPtr);
3024 return nIndex;
3028 static LRESULT
3029 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3031 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3032 LPSTR p;
3033 INT nIndex;
3034 INT len;
3036 TRACE("%p, %lx\n", hInstance, lParam);
3038 if (IS_INTRESOURCE(lParam)) /* load from resources */
3039 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3041 p = (LPSTR)lParam;
3042 if (p == NULL)
3043 return -1;
3045 TRACE("adding string(s) from array\n");
3046 nIndex = infoPtr->nNumStrings;
3047 while (*p) {
3048 len = strlen (p);
3049 TRACE("len=%d \"%s\"\n", len, p);
3051 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3052 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3053 infoPtr->nNumStrings++;
3055 p += (len+1);
3058 if (fFirstString)
3059 TOOLBAR_CalcToolbar(infoPtr);
3060 return nIndex;
3064 static LRESULT
3065 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3067 TRACE("auto sizing, style=%#x\n", infoPtr->dwStyle);
3068 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3070 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3072 RECT window_rect, client_rect, parent_rect, border;
3073 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3074 HWND parent;
3075 INT x, y, cx, cy;
3077 parent = GetParent (infoPtr->hwndSelf);
3079 if (!parent || !infoPtr->bDoRedraw)
3080 return 0;
3082 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3083 GetClientRect(infoPtr->hwndSelf, &client_rect);
3084 border = window_rect;
3085 MapWindowPoints(0, infoPtr->hwndSelf, (POINT *)&border, 2);
3086 border.right -= border.left + client_rect.right - client_rect.left;
3087 border.bottom -= border.top + client_rect.bottom - client_rect.top;
3089 GetClientRect(parent, &parent_rect);
3091 x = parent_rect.left;
3092 y = parent_rect.top;
3094 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3095 cx = parent_rect.right - parent_rect.left;
3097 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3099 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3100 y = window_rect.top;
3102 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3103 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3105 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3106 uPosFlags |= SWP_NOMOVE;
3108 if (!(infoPtr->dwStyle & CCS_NOMOVEY) && !(infoPtr->dwStyle & CCS_NODIVIDER))
3109 y += GetSystemMetrics(SM_CYEDGE);
3111 x += border.left;
3112 y += border.top;
3113 cx += border.right;
3114 cy += border.bottom;
3116 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3119 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3121 TOOLBAR_LayoutToolbar(infoPtr);
3122 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3125 return 0;
3129 static inline LRESULT
3130 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3132 return infoPtr->nNumButtons;
3136 static inline LRESULT
3137 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3139 infoPtr->dwStructSize = Size;
3141 return 0;
3145 static LRESULT
3146 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3148 TBUTTON_INFO *btnPtr;
3149 INT nIndex;
3151 TRACE("button %d, iBitmap now %d\n", Id, Index);
3153 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3154 if (nIndex == -1)
3155 return FALSE;
3157 btnPtr = &infoPtr->buttons[nIndex];
3158 btnPtr->iBitmap = Index;
3160 /* we HAVE to erase the background, the new bitmap could be */
3161 /* transparent */
3162 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3164 return TRUE;
3168 static LRESULT
3169 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3171 TBUTTON_INFO *btnPtr;
3172 INT nIndex;
3173 INT nOldIndex = -1;
3174 BOOL bChecked = FALSE;
3176 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3178 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3180 if (nIndex == -1)
3181 return FALSE;
3183 btnPtr = &infoPtr->buttons[nIndex];
3185 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3187 if (!LOWORD(lParam))
3188 btnPtr->fsState &= ~TBSTATE_CHECKED;
3189 else {
3190 if (btnPtr->fsStyle & BTNS_GROUP) {
3191 nOldIndex =
3192 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3193 if (nOldIndex == nIndex)
3194 return 0;
3195 if (nOldIndex != -1)
3196 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3198 btnPtr->fsState |= TBSTATE_CHECKED;
3201 if( bChecked != LOWORD(lParam) )
3203 if (nOldIndex != -1)
3204 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3205 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3208 /* FIXME: Send a WM_NOTIFY?? */
3210 return TRUE;
3214 static LRESULT
3215 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3217 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3221 static LRESULT
3222 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3224 CUSTDLG_INFO custInfo;
3225 LRESULT ret;
3226 NMHDR nmhdr;
3228 custInfo.tbInfo = infoPtr;
3229 custInfo.tbHwnd = infoPtr->hwndSelf;
3231 /* send TBN_BEGINADJUST notification */
3232 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3234 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3235 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3237 /* send TBN_ENDADJUST notification */
3238 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3240 return ret;
3244 static LRESULT
3245 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3247 NMTOOLBARW nmtb;
3248 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3250 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3251 return FALSE;
3253 memset(&nmtb, 0, sizeof(nmtb));
3254 nmtb.iItem = btnPtr->idCommand;
3255 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3256 nmtb.tbButton.idCommand = btnPtr->idCommand;
3257 nmtb.tbButton.fsState = btnPtr->fsState;
3258 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3259 nmtb.tbButton.dwData = btnPtr->dwData;
3260 nmtb.tbButton.iString = btnPtr->iString;
3261 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3263 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3265 infoPtr->nHotItem = -1;
3266 if (infoPtr->nNumButtons == 1) {
3267 TRACE(" simple delete\n");
3268 free_string( infoPtr->buttons );
3269 Free (infoPtr->buttons);
3270 infoPtr->buttons = NULL;
3271 infoPtr->nNumButtons = 0;
3273 else {
3274 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3275 TRACE("complex delete [nIndex=%d]\n", nIndex);
3277 infoPtr->nNumButtons--;
3278 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3279 if (nIndex > 0) {
3280 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3281 nIndex * sizeof(TBUTTON_INFO));
3284 if (nIndex < infoPtr->nNumButtons) {
3285 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3286 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3289 free_string( oldButtons + nIndex );
3290 Free (oldButtons);
3293 TOOLBAR_LayoutToolbar(infoPtr);
3295 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3297 return TRUE;
3301 static LRESULT
3302 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3304 TBUTTON_INFO *btnPtr;
3305 INT nIndex;
3306 DWORD bState;
3308 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3310 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3312 if (nIndex == -1)
3313 return FALSE;
3315 btnPtr = &infoPtr->buttons[nIndex];
3317 bState = btnPtr->fsState & TBSTATE_ENABLED;
3319 /* update the toolbar button state */
3320 if(!LOWORD(lParam)) {
3321 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3322 } else {
3323 btnPtr->fsState |= TBSTATE_ENABLED;
3326 /* redraw the button only if the state of the button changed */
3327 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3328 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3330 return TRUE;
3334 static inline LRESULT
3335 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3337 return infoPtr->bAnchor;
3341 static LRESULT
3342 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3344 INT nIndex;
3346 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3347 if (nIndex == -1)
3348 return -1;
3350 return infoPtr->buttons[nIndex].iBitmap;
3354 static inline LRESULT
3355 TOOLBAR_GetBitmapFlags (void)
3357 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3361 static LRESULT
3362 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3364 TBUTTON_INFO *btnPtr;
3366 if (lpTbb == NULL)
3367 return FALSE;
3369 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3370 return FALSE;
3372 btnPtr = &infoPtr->buttons[nIndex];
3373 lpTbb->iBitmap = btnPtr->iBitmap;
3374 lpTbb->idCommand = btnPtr->idCommand;
3375 lpTbb->fsState = btnPtr->fsState;
3376 lpTbb->fsStyle = btnPtr->fsStyle;
3377 lpTbb->bReserved[0] = 0;
3378 lpTbb->bReserved[1] = 0;
3379 lpTbb->dwData = btnPtr->dwData;
3380 lpTbb->iString = btnPtr->iString;
3382 return TRUE;
3386 static LRESULT
3387 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3389 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3390 TBUTTON_INFO *btnPtr;
3391 INT nIndex;
3393 if (lpTbInfo == NULL)
3394 return -1;
3396 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3397 * the headers and tests shows that even with comctl 6 Vista accepts only the
3398 * original TBBUTTONINFO size
3400 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3402 WARN("Invalid button size\n");
3403 return -1;
3406 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3407 if (nIndex == -1)
3408 return -1;
3410 btnPtr = &infoPtr->buttons[nIndex];
3411 if (lpTbInfo->dwMask & TBIF_COMMAND)
3412 lpTbInfo->idCommand = btnPtr->idCommand;
3413 if (lpTbInfo->dwMask & TBIF_IMAGE)
3414 lpTbInfo->iImage = btnPtr->iBitmap;
3415 if (lpTbInfo->dwMask & TBIF_LPARAM)
3416 lpTbInfo->lParam = btnPtr->dwData;
3417 if (lpTbInfo->dwMask & TBIF_SIZE)
3418 /* tests show that for separators TBIF_SIZE returns not calculated width,
3419 but cx property, that differs from 0 only if application have
3420 specifically set it */
3421 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3422 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3423 if (lpTbInfo->dwMask & TBIF_STATE)
3424 lpTbInfo->fsState = btnPtr->fsState;
3425 if (lpTbInfo->dwMask & TBIF_STYLE)
3426 lpTbInfo->fsStyle = btnPtr->fsStyle;
3427 if (lpTbInfo->dwMask & TBIF_TEXT) {
3428 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3429 can't use TOOLBAR_GetText here */
3430 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3431 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3432 if (bUnicode)
3433 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3434 else
3435 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3436 } else if (!bUnicode || lpTbInfo->pszText)
3437 lpTbInfo->pszText[0] = '\0';
3439 return nIndex;
3443 static inline LRESULT
3444 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3446 return MAKELONG((WORD)infoPtr->nButtonWidth,
3447 (WORD)infoPtr->nButtonHeight);
3451 static LRESULT
3452 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3454 INT nIndex;
3455 LPWSTR lpText;
3456 LRESULT ret = 0;
3458 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3459 if (nIndex == -1)
3460 return -1;
3462 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3464 if (isW)
3466 if (lpText)
3468 ret = lstrlenW (lpText);
3469 if (lpStr) lstrcpyW (lpStr, lpText);
3472 else
3473 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3474 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3475 return ret;
3479 static LRESULT
3480 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3482 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3483 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3484 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3488 static inline LRESULT
3489 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3491 TRACE("\n");
3493 return infoPtr->dwExStyle;
3497 static LRESULT
3498 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3500 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3501 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3502 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3506 static LRESULT
3507 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3509 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme))
3510 return -1;
3512 if (infoPtr->nHotItem < 0)
3513 return -1;
3515 return (LRESULT)infoPtr->nHotItem;
3519 static LRESULT
3520 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3522 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3523 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3524 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3528 static LRESULT
3529 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3531 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3533 *lptbim = infoPtr->tbim;
3535 return 0;
3539 static inline LRESULT
3540 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3542 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3544 return (LRESULT)infoPtr->clrInsertMark;
3548 static LRESULT
3549 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3551 TBUTTON_INFO *btnPtr;
3553 btnPtr = &infoPtr->buttons[nIndex];
3554 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3555 return FALSE;
3557 if (lpRect == NULL)
3558 return FALSE;
3559 if (btnPtr->fsState & TBSTATE_HIDDEN)
3560 return FALSE;
3562 lpRect->left = btnPtr->rect.left;
3563 lpRect->right = btnPtr->rect.right;
3564 lpRect->bottom = btnPtr->rect.bottom;
3565 lpRect->top = btnPtr->rect.top;
3567 return TRUE;
3571 static LRESULT
3572 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3574 if (lpSize == NULL)
3575 return FALSE;
3577 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3578 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3580 TRACE("maximum size %d x %d\n",
3581 infoPtr->rcBound.right - infoPtr->rcBound.left,
3582 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3584 return TRUE;
3588 /* << TOOLBAR_GetObject >> */
3591 static inline LRESULT
3592 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3594 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3598 static LRESULT
3599 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3601 TBUTTON_INFO *btnPtr;
3602 INT nIndex;
3604 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3605 btnPtr = &infoPtr->buttons[nIndex];
3606 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3607 return FALSE;
3609 if (lpRect == NULL)
3610 return FALSE;
3612 lpRect->left = btnPtr->rect.left;
3613 lpRect->right = btnPtr->rect.right;
3614 lpRect->bottom = btnPtr->rect.bottom;
3615 lpRect->top = btnPtr->rect.top;
3617 return TRUE;
3621 static inline LRESULT
3622 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3624 return infoPtr->nRows;
3628 static LRESULT
3629 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3631 INT nIndex;
3633 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3634 if (nIndex == -1)
3635 return -1;
3637 return infoPtr->buttons[nIndex].fsState;
3641 static inline LRESULT
3642 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3644 return infoPtr->dwStyle;
3648 static inline LRESULT
3649 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3651 return infoPtr->nMaxTextRows;
3655 static LRESULT
3656 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3658 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3659 TOOLBAR_TooltipCreateControl(infoPtr);
3660 return (LRESULT)infoPtr->hwndToolTip;
3664 static LRESULT
3665 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3667 TRACE("%s hwnd=%p\n",
3668 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3670 return infoPtr->bUnicode;
3674 static inline LRESULT
3675 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3677 return infoPtr->iVersion;
3681 static LRESULT
3682 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3684 TBUTTON_INFO *btnPtr;
3685 BYTE oldState;
3686 INT nIndex;
3688 TRACE("\n");
3690 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3691 if (nIndex == -1)
3692 return FALSE;
3694 btnPtr = &infoPtr->buttons[nIndex];
3695 oldState = btnPtr->fsState;
3697 if (fHide)
3698 btnPtr->fsState |= TBSTATE_HIDDEN;
3699 else
3700 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3702 if (oldState != btnPtr->fsState) {
3703 TOOLBAR_LayoutToolbar (infoPtr);
3704 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3707 return TRUE;
3711 static inline LRESULT
3712 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3714 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3718 static LRESULT
3719 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3721 TBUTTON_INFO *btnPtr;
3722 INT nIndex;
3723 DWORD oldState;
3725 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3726 if (nIndex == -1)
3727 return FALSE;
3729 btnPtr = &infoPtr->buttons[nIndex];
3730 oldState = btnPtr->fsState;
3732 if (fIndeterminate)
3733 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3734 else
3735 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3737 if(oldState != btnPtr->fsState)
3738 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3740 return TRUE;
3744 static LRESULT
3745 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3747 if (lpTbb == NULL)
3748 return FALSE;
3750 if (nIndex == -1) {
3751 /* EPP: this seems to be an undocumented call (from my IE4)
3752 * I assume in that case that:
3753 * - index of insertion is at the end of existing buttons
3754 * I only see this happen with nIndex == -1, but it could have a special
3755 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3757 nIndex = infoPtr->nNumButtons;
3759 } else if (nIndex < 0)
3760 return FALSE;
3762 TRACE("inserting button index=%d\n", nIndex);
3763 if (nIndex > infoPtr->nNumButtons) {
3764 nIndex = infoPtr->nNumButtons;
3765 TRACE("adjust index=%d\n", nIndex);
3768 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3771 /* << TOOLBAR_InsertMarkHitTest >> */
3774 static LRESULT
3775 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3777 INT nIndex;
3779 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3780 if (nIndex == -1)
3781 return -1;
3783 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3787 static LRESULT
3788 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3790 INT nIndex;
3792 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3793 if (nIndex == -1)
3794 return -1;
3796 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3800 static LRESULT
3801 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3803 INT nIndex;
3805 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3806 if (nIndex == -1)
3807 return -1;
3809 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3813 static LRESULT
3814 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3816 INT nIndex;
3818 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3819 if (nIndex == -1)
3820 return -1;
3822 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3826 static LRESULT
3827 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3829 INT nIndex;
3831 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3832 if (nIndex == -1)
3833 return -1;
3835 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3839 static LRESULT
3840 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3842 INT nIndex;
3844 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3845 if (nIndex == -1)
3846 return -1;
3848 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3852 static LRESULT
3853 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3855 TBADDBITMAP tbab;
3856 tbab.hInst = hInstance;
3857 tbab.nID = wParam;
3859 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3861 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3865 static LRESULT
3866 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3868 WCHAR wszAccel[] = {'&',wAccel,0};
3869 int i;
3871 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3872 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3874 for (i = 0; i < infoPtr->nNumButtons; i++)
3876 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3877 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3878 !(btnPtr->fsState & TBSTATE_HIDDEN))
3880 int iLen = lstrlenW(wszAccel);
3881 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3883 if (!lpszStr)
3884 continue;
3886 while (*lpszStr)
3888 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3890 lpszStr += 2;
3891 continue;
3893 if (!wcsnicmp(lpszStr, wszAccel, iLen))
3895 *pIDButton = btnPtr->idCommand;
3896 return TRUE;
3898 lpszStr++;
3902 return FALSE;
3906 static LRESULT
3907 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3909 INT nIndex;
3910 DWORD oldState;
3911 TBUTTON_INFO *btnPtr;
3913 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3915 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3916 if (nIndex == -1)
3917 return FALSE;
3919 btnPtr = &infoPtr->buttons[nIndex];
3920 oldState = btnPtr->fsState;
3922 if (fMark)
3923 btnPtr->fsState |= TBSTATE_MARKED;
3924 else
3925 btnPtr->fsState &= ~TBSTATE_MARKED;
3927 if(oldState != btnPtr->fsState)
3928 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3930 return TRUE;
3934 /* fixes up an index of a button affected by a move */
3935 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3937 if (bMoveUp)
3939 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3940 (*pIndex)--;
3941 else if (*pIndex == nIndex)
3942 *pIndex = nMoveIndex;
3944 else
3946 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3947 (*pIndex)++;
3948 else if (*pIndex == nIndex)
3949 *pIndex = nMoveIndex;
3954 static LRESULT
3955 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3957 INT nIndex;
3958 INT nCount;
3959 TBUTTON_INFO button;
3961 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3963 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3964 if ((nIndex == -1) || (nMoveIndex < 0))
3965 return FALSE;
3967 if (nMoveIndex > infoPtr->nNumButtons - 1)
3968 nMoveIndex = infoPtr->nNumButtons - 1;
3970 button = infoPtr->buttons[nIndex];
3972 /* move button right */
3973 if (nIndex < nMoveIndex)
3975 nCount = nMoveIndex - nIndex;
3976 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3977 infoPtr->buttons[nMoveIndex] = button;
3979 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3980 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3981 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3982 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3984 else if (nIndex > nMoveIndex) /* move button left */
3986 nCount = nIndex - nMoveIndex;
3987 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3988 infoPtr->buttons[nMoveIndex] = button;
3990 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3991 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3992 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3993 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3996 TOOLBAR_LayoutToolbar(infoPtr);
3997 TOOLBAR_AutoSize(infoPtr);
3998 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4000 return TRUE;
4004 static LRESULT
4005 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
4007 TBUTTON_INFO *btnPtr;
4008 INT nIndex;
4009 DWORD oldState;
4011 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4012 if (nIndex == -1)
4013 return FALSE;
4015 btnPtr = &infoPtr->buttons[nIndex];
4016 oldState = btnPtr->fsState;
4018 if (fPress)
4019 btnPtr->fsState |= TBSTATE_PRESSED;
4020 else
4021 btnPtr->fsState &= ~TBSTATE_PRESSED;
4023 if(oldState != btnPtr->fsState)
4024 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4026 return TRUE;
4029 /* FIXME: there might still be some confusion her between number of buttons
4030 * and number of bitmaps */
4031 static LRESULT
4032 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
4034 HBITMAP hBitmap;
4035 int i = 0, nOldButtons = 0, pos = 0;
4036 int nOldBitmaps, nNewBitmaps = 0;
4037 HIMAGELIST himlDef = 0;
4039 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4040 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4041 lpReplace->nButtons);
4043 if (lpReplace->hInstOld == HINST_COMMCTRL)
4045 FIXME("changing standard bitmaps not implemented\n");
4046 return FALSE;
4048 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4049 FIXME("resources not in the current module not implemented\n");
4051 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4052 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4053 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4054 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4055 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4057 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4058 nOldButtons = tbi->nButtons;
4059 tbi->nButtons = lpReplace->nButtons;
4060 tbi->hInst = lpReplace->hInstNew;
4061 tbi->nID = lpReplace->nIDNew;
4062 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4063 break;
4065 pos += tbi->nButtons;
4068 if (nOldButtons == 0)
4070 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4071 return FALSE;
4074 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4075 * bitmap
4077 if (lpReplace->hInstNew)
4078 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4079 else
4080 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4082 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4083 nOldBitmaps = ImageList_GetImageCount(himlDef);
4085 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4087 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4088 ImageList_Remove(himlDef, i);
4090 if (hBitmap)
4092 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4093 nNewBitmaps = ImageList_GetImageCount(himlDef);
4094 DeleteObject(hBitmap);
4097 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4099 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4100 pos, nOldBitmaps, nNewBitmaps);
4102 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4103 return TRUE;
4107 /* helper for TOOLBAR_SaveRestoreW */
4108 static BOOL
4109 TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
4111 NMTBSAVE save;
4112 INT ret, i;
4113 BOOL alloced = FALSE;
4114 HKEY key;
4116 TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );
4118 memset( &save, 0, sizeof(save) );
4119 save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
4120 save.iItem = -1;
4121 save.cButtons = infoPtr->nNumButtons;
4122 save.tbButton.idCommand = -1;
4123 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4125 if (!save.pData)
4127 save.pData = Alloc( save.cbData );
4128 if (!save.pData) return FALSE;
4129 alloced = TRUE;
4131 if (!save.pCurrent) save.pCurrent = save.pData;
4133 for (i = 0; i < infoPtr->nNumButtons; i++)
4135 save.iItem = i;
4136 save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
4137 save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
4138 save.tbButton.fsState = infoPtr->buttons[i].fsState;
4139 save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
4140 memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
4141 save.tbButton.dwData = infoPtr->buttons[i].dwData;
4142 save.tbButton.iString = infoPtr->buttons[i].iString;
4144 *save.pCurrent++ = save.tbButton.idCommand;
4146 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4149 ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
4150 if (ret == ERROR_SUCCESS)
4152 ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
4153 RegCloseKey( key );
4156 if (alloced) Free( save.pData );
4157 return !ret;
4161 /* helper for TOOLBAR_Restore */
4162 static void
4163 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4165 INT i;
4167 for (i = 0; i < infoPtr->nNumButtons; i++)
4169 free_string( infoPtr->buttons + i );
4170 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4173 Free(infoPtr->buttons);
4174 infoPtr->buttons = NULL;
4175 infoPtr->nNumButtons = 0;
4179 /* helper for TOOLBAR_SaveRestoreW */
4180 static BOOL
4181 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4183 LONG res;
4184 HKEY hkey = NULL;
4185 BOOL ret = FALSE;
4186 DWORD dwType;
4187 DWORD dwSize = 0;
4188 NMTBRESTORE nmtbr;
4189 NMHDR hdr;
4191 /* restore toolbar information */
4192 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4193 debugstr_w(lpSave->pszValueName));
4195 memset(&nmtbr, 0, sizeof(nmtbr));
4197 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4198 KEY_QUERY_VALUE, &hkey);
4199 if (!res)
4200 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4201 NULL, &dwSize);
4202 if (!res && dwType != REG_BINARY)
4203 res = ERROR_FILE_NOT_FOUND;
4204 if (!res)
4206 nmtbr.pData = Alloc(dwSize);
4207 nmtbr.cbData = dwSize;
4208 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4210 if (!res)
4211 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4212 (LPBYTE)nmtbr.pData, &dwSize);
4213 if (!res)
4215 nmtbr.pCurrent = nmtbr.pData;
4216 nmtbr.iItem = -1;
4217 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4218 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4220 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4222 INT i, count = nmtbr.cButtons;
4224 /* remove all existing buttons as this function is designed to
4225 * restore the toolbar to a previously saved state */
4226 TOOLBAR_DeleteAllButtons(infoPtr);
4228 for (i = 0; i < count; i++)
4230 nmtbr.iItem = i;
4231 nmtbr.tbButton.iBitmap = -1;
4232 nmtbr.tbButton.fsState = 0;
4233 nmtbr.tbButton.fsStyle = 0;
4234 nmtbr.tbButton.dwData = 0;
4235 nmtbr.tbButton.iString = 0;
4237 if (*nmtbr.pCurrent & 0x80000000)
4239 /* separator */
4240 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4241 nmtbr.tbButton.idCommand = 0;
4242 nmtbr.tbButton.fsStyle = BTNS_SEP;
4243 if (*nmtbr.pCurrent != (DWORD)-1)
4244 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4246 else
4247 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4249 nmtbr.pCurrent++;
4251 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4253 /* All returned ptrs and -1 are ignored */
4254 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4255 nmtbr.tbButton.iString = 0;
4257 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4260 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
4261 for (i = 0; ; i++)
4263 NMTOOLBARW tb;
4264 TBBUTTONINFOW bi;
4265 WCHAR buf[128];
4266 UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
4267 INT idx;
4269 memset( &tb, 0, sizeof(tb) );
4270 tb.iItem = i;
4271 tb.cchText = ARRAY_SIZE(buf);
4272 tb.pszText = buf;
4274 /* Use the same struct for both A and W versions since the layout is the same. */
4275 if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
4276 break;
4278 idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
4279 if (idx == -1) continue;
4281 /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
4282 be an index or a ptr. Either way it is simply copied. There is no api to change
4283 the string index, so we set it manually. The other properties can be set with SetButtonInfo. */
4284 free_string( infoPtr->buttons + idx );
4285 infoPtr->buttons[idx].iString = tb.tbButton.iString;
4287 memset( &bi, 0, sizeof(bi) );
4288 bi.cbSize = sizeof(bi);
4289 bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
4290 bi.iImage = tb.tbButton.iBitmap;
4291 bi.fsState = tb.tbButton.fsState;
4292 bi.fsStyle = tb.tbButton.fsStyle;
4293 bi.lParam = tb.tbButton.dwData;
4295 TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4297 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4299 /* remove all uninitialised buttons
4300 * note: loop backwards to avoid having to fixup i on a
4301 * delete */
4302 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4303 if (infoPtr->buttons[i].iBitmap == -1)
4304 TOOLBAR_DeleteButton(infoPtr, i);
4306 /* only indicate success if at least one button survived */
4307 if (infoPtr->nNumButtons > 0) ret = TRUE;
4310 Free (nmtbr.pData);
4311 RegCloseKey(hkey);
4313 return ret;
4317 static LRESULT
4318 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4320 if (lpSave == NULL) return 0;
4322 if (wParam)
4323 return TOOLBAR_Save(infoPtr, lpSave);
4324 else
4325 return TOOLBAR_Restore(infoPtr, lpSave);
4329 static LRESULT
4330 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4332 LPWSTR pszValueName = 0, pszSubKey = 0;
4333 TBSAVEPARAMSW SaveW;
4334 LRESULT result = 0;
4335 int len;
4337 if (lpSave == NULL) return 0;
4339 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4340 pszSubKey = Alloc(len * sizeof(WCHAR));
4341 if (!pszSubKey) goto exit;
4342 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4344 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4345 pszValueName = Alloc(len * sizeof(WCHAR));
4346 if (!pszValueName) goto exit;
4347 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4349 SaveW.pszValueName = pszValueName;
4350 SaveW.pszSubKey = pszSubKey;
4351 SaveW.hkr = lpSave->hkr;
4352 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4354 exit:
4355 Free (pszValueName);
4356 Free (pszSubKey);
4358 return result;
4362 static LRESULT
4363 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4365 BOOL bOldAnchor = infoPtr->bAnchor;
4367 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4369 infoPtr->bAnchor = bAnchor;
4371 /* Native does not remove the hot effect from an already hot button */
4373 return (LRESULT)bOldAnchor;
4377 static LRESULT
4378 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4380 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4381 short width = (short)LOWORD(lParam);
4382 short height = (short)HIWORD(lParam);
4384 TRACE("hwnd=%p, wParam=%ld, size %d x %d\n", infoPtr->hwndSelf, wParam, width, height);
4386 if (wParam != 0)
4387 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4389 /* 0 width or height is changed to 1 */
4390 if (width == 0)
4391 width = 1;
4392 if (height == 0)
4393 height = 1;
4395 if (infoPtr->nNumButtons > 0)
4396 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4397 infoPtr->nNumButtons,
4398 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4400 if (width < -1 || height < -1)
4402 /* Windows destroys the imagelist and seems to actually use negative
4403 * values to compute button sizes */
4404 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4405 return FALSE;
4408 /* width or height of -1 means no change */
4409 if (width != -1)
4410 infoPtr->nBitmapWidth = width;
4411 if (height != -1)
4412 infoPtr->nBitmapHeight = height;
4414 if ((himlDef == infoPtr->himlInt) &&
4415 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4417 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4418 infoPtr->nBitmapHeight);
4421 TOOLBAR_CalcToolbar(infoPtr);
4422 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4423 return TRUE;
4427 static LRESULT
4428 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4429 const TBBUTTONINFOW *lptbbi, BOOL isW)
4431 TBUTTON_INFO *btnPtr;
4432 INT nIndex;
4433 RECT oldBtnRect;
4435 if (lptbbi == NULL)
4436 return FALSE;
4437 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4438 return FALSE;
4440 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4441 if (nIndex == -1)
4442 return FALSE;
4444 btnPtr = &infoPtr->buttons[nIndex];
4445 if (lptbbi->dwMask & TBIF_COMMAND)
4446 btnPtr->idCommand = lptbbi->idCommand;
4447 if (lptbbi->dwMask & TBIF_IMAGE)
4448 btnPtr->iBitmap = lptbbi->iImage;
4449 if (lptbbi->dwMask & TBIF_LPARAM)
4450 btnPtr->dwData = lptbbi->lParam;
4451 if (lptbbi->dwMask & TBIF_SIZE)
4452 btnPtr->cx = lptbbi->cx;
4453 if (lptbbi->dwMask & TBIF_STATE)
4454 btnPtr->fsState = lptbbi->fsState;
4455 if (lptbbi->dwMask & TBIF_STYLE)
4456 btnPtr->fsStyle = lptbbi->fsStyle;
4458 if (lptbbi->dwMask & TBIF_TEXT)
4459 set_stringT( btnPtr, lptbbi->pszText, isW );
4461 /* save the button rect to see if we need to redraw the whole toolbar */
4462 oldBtnRect = btnPtr->rect;
4463 TOOLBAR_LayoutToolbar(infoPtr);
4465 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4466 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4467 else
4468 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4470 return TRUE;
4474 static LRESULT
4475 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4477 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4478 int top = default_top_margin(infoPtr);
4480 if ((cx < 0) || (cy < 0))
4482 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4483 return FALSE;
4486 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4488 /* The documentation claims you can only change the button size before
4489 * any button has been added. But this is wrong.
4490 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4491 * it to the toolbar, and it checks that the return value is nonzero - mjm
4492 * Further testing shows that we must actually perform the change too.
4495 * The documentation also does not mention that if 0 is supplied for
4496 * either size, the system changes it to the default of 24 wide and
4497 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4499 if (cx == 0) cx = 24;
4500 if (cy == 0) cy = 22;
4502 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4503 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4505 if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
4506 top != infoPtr->iTopMargin)
4508 infoPtr->nButtonWidth = cx;
4509 infoPtr->nButtonHeight = cy;
4510 infoPtr->iTopMargin = top;
4512 TOOLBAR_LayoutToolbar( infoPtr );
4513 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
4515 return TRUE;
4519 static LRESULT
4520 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4522 /* if setting to current values, ignore */
4523 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4524 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4525 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4526 infoPtr->cxMin, infoPtr->cxMax);
4527 return TRUE;
4530 /* save new values */
4531 infoPtr->cxMin = (short)LOWORD(lParam);
4532 infoPtr->cxMax = (short)HIWORD(lParam);
4534 /* otherwise we need to recalc the toolbar and in some cases
4535 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4536 which doesn't actually draw - GA). */
4537 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4538 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4540 TOOLBAR_CalcToolbar (infoPtr);
4542 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4544 return TRUE;
4548 static LRESULT
4549 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4551 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4552 return FALSE;
4554 infoPtr->buttons[nIndex].idCommand = nId;
4556 if (infoPtr->hwndToolTip) {
4558 FIXME("change tool tip\n");
4562 return TRUE;
4566 static LRESULT
4567 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4569 HIMAGELIST himlTemp;
4570 INT id = 0;
4572 if (infoPtr->iVersion >= 5)
4573 id = wParam;
4575 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4576 &infoPtr->cimlDis, himl, id);
4578 /* FIXME: redraw ? */
4580 return (LRESULT)himlTemp;
4584 static LRESULT
4585 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD flags)
4587 DWORD old_flags;
4589 TRACE("hwnd = %p, mask = 0x%08x, flags = 0x%08x\n", infoPtr->hwndSelf, mask, flags);
4591 old_flags = infoPtr->dwDTFlags;
4592 infoPtr->dwDTFlags = (old_flags & ~mask) | (flags & mask);
4594 return (LRESULT)old_flags;
4597 /* This function differs a bit from what MSDN says it does:
4598 * 1. lParam contains extended style flags to OR with current style
4599 * (MSDN isn't clear on the OR bit)
4600 * 2. wParam appears to contain extended style flags to be reset
4601 * (MSDN says that this parameter is reserved)
4603 static LRESULT
4604 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4606 DWORD old_style = infoPtr->dwExStyle;
4608 TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
4610 if (mask)
4611 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4612 else
4613 infoPtr->dwExStyle = style;
4615 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4616 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4617 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4619 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4620 TOOLBAR_CalcToolbar(infoPtr);
4621 else
4622 TOOLBAR_LayoutToolbar(infoPtr);
4624 TOOLBAR_AutoSize(infoPtr);
4625 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4627 return old_style;
4631 static LRESULT
4632 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4634 HIMAGELIST himlTemp;
4635 INT id = 0;
4637 if (infoPtr->iVersion >= 5)
4638 id = wParam;
4640 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4642 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4643 &infoPtr->cimlHot, himl, id);
4645 /* FIXME: redraw ? */
4647 return (LRESULT)himlTemp;
4651 /* Makes previous hot button no longer hot, makes the specified
4652 * button hot and sends appropriate notifications. dwReason is one or
4653 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4654 * NOTE 1: this function does not validate nHit
4655 * NOTE 2: the name of this function is completely made up and
4656 * not based on any documentation from Microsoft. */
4657 static void
4658 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4660 if (infoPtr->nHotItem != nHit)
4662 NMTBHOTITEM nmhotitem;
4663 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4665 nmhotitem.dwFlags = dwReason;
4666 if(infoPtr->nHotItem >= 0)
4668 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4669 nmhotitem.idOld = oldBtnPtr->idCommand;
4671 else
4673 nmhotitem.dwFlags |= HICF_ENTERING;
4674 nmhotitem.idOld = 0;
4677 if (nHit >= 0)
4679 btnPtr = &infoPtr->buttons[nHit];
4680 nmhotitem.idNew = btnPtr->idCommand;
4682 else
4684 nmhotitem.dwFlags |= HICF_LEAVING;
4685 nmhotitem.idNew = 0;
4688 /* now change the hot and invalidate the old and new buttons - if the
4689 * parent agrees */
4690 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4692 if (oldBtnPtr) {
4693 oldBtnPtr->bHot = FALSE;
4694 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4696 /* setting disabled buttons as hot fails even if the notify contains the button id */
4697 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4698 btnPtr->bHot = TRUE;
4699 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4700 infoPtr->nHotItem = nHit;
4702 else
4703 infoPtr->nHotItem = -1;
4708 static LRESULT
4709 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4711 INT nOldHotItem = infoPtr->nHotItem;
4713 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4715 if (nHotItem >= infoPtr->nNumButtons)
4716 return infoPtr->nHotItem;
4718 if (nHotItem < 0)
4719 nHotItem = -1;
4721 /* NOTE: an application can still remove the hot item even if anchor
4722 * highlighting is enabled */
4724 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4726 if (nOldHotItem < 0)
4727 return -1;
4729 return (LRESULT)nOldHotItem;
4733 static LRESULT
4734 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4736 HIMAGELIST himlTemp;
4737 INT oldButtonWidth = infoPtr->nButtonWidth;
4738 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4739 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4740 INT i, id = 0;
4742 if (infoPtr->iVersion >= 5)
4743 id = wParam;
4745 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4746 &infoPtr->cimlDef, himl, id);
4748 infoPtr->nNumBitmaps = 0;
4749 for (i = 0; i < infoPtr->cimlDef; i++)
4750 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4752 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4753 &infoPtr->nBitmapHeight))
4755 infoPtr->nBitmapWidth = 1;
4756 infoPtr->nBitmapHeight = 1;
4758 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4760 TOOLBAR_CalcToolbar(infoPtr);
4761 if (infoPtr->nButtonWidth < oldButtonWidth)
4762 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4765 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4766 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4767 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4769 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4771 return (LRESULT)himlTemp;
4775 static LRESULT
4776 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4778 infoPtr->nIndent = nIndent;
4780 TRACE("\n");
4782 /* process only on indent changing */
4783 if(infoPtr->nIndent != nIndent)
4785 infoPtr->nIndent = nIndent;
4786 TOOLBAR_CalcToolbar (infoPtr);
4787 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4790 return TRUE;
4794 static LRESULT
4795 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4797 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4799 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4801 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4802 return 0;
4805 if ((lptbim->iButton == -1) ||
4806 ((lptbim->iButton < infoPtr->nNumButtons) &&
4807 (lptbim->iButton >= 0)))
4809 infoPtr->tbim = *lptbim;
4810 /* FIXME: don't need to update entire toolbar */
4811 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4813 else
4814 ERR("Invalid button index %d\n", lptbim->iButton);
4816 return 0;
4820 static LRESULT
4821 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4823 infoPtr->clrInsertMark = clr;
4825 /* FIXME: don't need to update entire toolbar */
4826 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4828 return 0;
4832 static LRESULT
4833 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4835 infoPtr->nMaxTextRows = nMaxRows;
4837 TOOLBAR_CalcToolbar(infoPtr);
4838 return TRUE;
4842 /* MSDN gives slightly wrong info on padding.
4843 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4844 * 2. It is not used to create a blank area between the edge of the button
4845 * and the text or image if TBSTYLE_LIST is set. It is used to control
4846 * the gap between the image and text.
4847 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4848 * to control the bottom and right borders [with the border being
4849 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4850 * is shared evenly on both sides of the button.
4851 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4853 static LRESULT
4854 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4856 DWORD oldPad;
4858 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4859 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4860 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4861 TRACE("cx=%d, cy=%d\n",
4862 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4863 return (LRESULT) oldPad;
4867 static LRESULT
4868 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4870 HWND hwndOldNotify;
4872 TRACE("\n");
4874 hwndOldNotify = infoPtr->hwndNotify;
4875 infoPtr->hwndNotify = hParent;
4877 return (LRESULT)hwndOldNotify;
4881 static LRESULT
4882 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4884 int rows = LOWORD(wParam);
4885 BOOL bLarger = HIWORD(wParam);
4887 TRACE("\n");
4889 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4891 if(infoPtr->nRows != rows)
4893 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4894 int curColumn = 0; /* Current column */
4895 int curRow = 0; /* Current row */
4896 int hidden = 0; /* Number of hidden buttons */
4897 int seps = 0; /* Number of separators */
4898 int idealWrap = 0; /* Ideal wrap point */
4899 int i;
4900 BOOL wrap;
4903 Calculate new size and wrap points - Under windows, setrows will
4904 change the dimensions if needed to show the number of requested
4905 rows (if CCS_NORESIZE is set), or will take up the whole window
4906 (if no CCS_NORESIZE).
4908 Basic algorithm - If N buttons, and y rows requested, each row
4909 contains N/y buttons.
4911 FIXME: Handling of separators not obvious from testing results
4912 FIXME: Take width of window into account?
4915 /* Loop through the buttons one by one counting key items */
4916 for (i = 0; i < infoPtr->nNumButtons; i++ )
4918 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4919 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4920 hidden++;
4921 else if (btnPtr[i].fsStyle & BTNS_SEP)
4922 seps++;
4925 /* FIXME: Separators make this quite complex */
4926 if (seps) FIXME("Separators unhandled\n");
4928 /* Round up so more per line, i.e., less rows */
4929 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4931 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4932 achieve the requested number of rows. */
4933 if (bLarger && idealWrap > 1)
4935 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4936 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4938 if (resRows < rows && moreRows > rows)
4940 idealWrap--;
4941 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4945 curColumn = curRow = 0;
4946 wrap = FALSE;
4947 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4948 infoPtr->nNumButtons, hidden, rows);
4950 for (i = 0; i < infoPtr->nNumButtons; i++ )
4952 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4953 continue;
4955 /* Step on, wrap if necessary or flag next to wrap */
4956 if (!wrap) {
4957 curColumn++;
4958 } else {
4959 wrap = FALSE;
4960 curColumn = 1;
4961 curRow++;
4964 if (curColumn > (idealWrap-1)) {
4965 wrap = TRUE;
4966 btnPtr[i].fsState |= TBSTATE_WRAP;
4970 TRACE("Result - %d rows\n", curRow + 1);
4972 /* recalculate toolbar */
4973 TOOLBAR_CalcToolbar (infoPtr);
4975 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4976 if NORESIZE is NOT set, then the toolbar will always be resized to
4977 take up the whole window. With it set, sizing needs to be manual. */
4978 if (infoPtr->dwStyle & CCS_NORESIZE) {
4979 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4980 infoPtr->rcBound.right - infoPtr->rcBound.left,
4981 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4982 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4985 /* repaint toolbar */
4986 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4989 /* return bounding rectangle */
4990 if (lprc) {
4991 lprc->left = infoPtr->rcBound.left;
4992 lprc->right = infoPtr->rcBound.right;
4993 lprc->top = infoPtr->rcBound.top;
4994 lprc->bottom = infoPtr->rcBound.bottom;
4997 return 0;
5001 static LRESULT
5002 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
5004 TBUTTON_INFO *btnPtr;
5005 INT nIndex;
5007 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
5008 if (nIndex == -1)
5009 return FALSE;
5011 btnPtr = &infoPtr->buttons[nIndex];
5013 /* if hidden state has changed the invalidate entire window and recalc */
5014 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
5015 btnPtr->fsState = LOWORD(lParam);
5016 TOOLBAR_CalcToolbar (infoPtr);
5017 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
5018 return TRUE;
5021 /* process state changing if current state doesn't match new state */
5022 if(btnPtr->fsState != LOWORD(lParam))
5024 btnPtr->fsState = LOWORD(lParam);
5025 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5028 return TRUE;
5031 static inline void unwrap(TOOLBAR_INFO *info)
5033 int i;
5035 for (i = 0; i < info->nNumButtons; i++)
5036 info->buttons[i].fsState &= ~TBSTATE_WRAP;
5039 static LRESULT
5040 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
5042 DWORD dwOldStyle = infoPtr->dwStyle;
5044 TRACE("new style 0x%08x\n", style);
5046 if (style & TBSTYLE_LIST)
5047 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
5048 else
5049 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
5051 infoPtr->dwStyle = style;
5052 TOOLBAR_CheckStyle(infoPtr);
5054 if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5056 if (dwOldStyle & TBSTYLE_WRAPABLE)
5057 unwrap(infoPtr);
5058 TOOLBAR_CalcToolbar(infoPtr);
5060 else if ((dwOldStyle ^ style) & CCS_VERT)
5061 TOOLBAR_LayoutToolbar(infoPtr);
5063 /* only resize if one of the CCS_* styles was changed */
5064 if ((dwOldStyle ^ style) & COMMON_STYLES)
5066 TOOLBAR_AutoSize(infoPtr);
5067 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5070 return 0;
5074 static inline LRESULT
5075 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
5077 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5079 infoPtr->hwndToolTip = hwndTooltip;
5080 return 0;
5084 static LRESULT
5085 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
5087 BOOL bTemp;
5089 TRACE("%s hwnd=%p\n",
5090 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
5092 bTemp = infoPtr->bUnicode;
5093 infoPtr->bUnicode = (BOOL)wParam;
5095 return bTemp;
5099 static LRESULT
5100 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5102 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5103 comctl32_color.clrBtnHighlight :
5104 infoPtr->clrBtnHighlight;
5105 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5106 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5107 return 1;
5111 static LRESULT
5112 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5114 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5115 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5116 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5118 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5119 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5120 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5121 return 0;
5125 static LRESULT
5126 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5128 INT iOldVersion = infoPtr->iVersion;
5130 infoPtr->iVersion = iVersion;
5132 if (infoPtr->iVersion >= 5)
5133 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5135 return iOldVersion;
5139 static LRESULT
5140 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
5142 WORD iString = HIWORD(wParam);
5143 WORD buffersize = LOWORD(wParam);
5144 LRESULT ret = -1;
5146 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
5148 if (iString < infoPtr->nNumStrings)
5150 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5151 ret--;
5153 TRACE("returning %s\n", debugstr_a(str));
5155 else
5156 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5158 return ret;
5162 static LRESULT
5163 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5165 WORD iString = HIWORD(wParam);
5166 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5167 LRESULT ret = -1;
5169 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5171 if (iString < infoPtr->nNumStrings)
5173 len = min(len, lstrlenW(infoPtr->strings[iString]));
5174 ret = (len+1)*sizeof(WCHAR);
5175 if (str)
5177 memcpy(str, infoPtr->strings[iString], ret);
5178 str[len] = '\0';
5180 ret = len;
5182 TRACE("returning %s\n", debugstr_w(str));
5184 else
5185 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5187 return ret;
5190 static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5192 SIZE * pSize = (SIZE*)lParam;
5193 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub\n", hwnd, wParam, pSize->cx, pSize->cy);
5194 return 0;
5197 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5198 * caller to specify a reason why the hot item changed (rather than just the
5199 * HICF_OTHER that TB_SETHOTITEM sends). */
5200 static LRESULT
5201 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5203 INT nOldHotItem = infoPtr->nHotItem;
5205 TRACE("old item=%d, new item=%d, flags=%08x\n",
5206 nOldHotItem, nHotItem, (DWORD)lParam);
5208 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5209 nHotItem = -1;
5211 /* NOTE: an application can still remove the hot item even if anchor
5212 * highlighting is enabled */
5214 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5216 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5219 /* Sets the toolbar global iListGap parameter which controls the amount of
5220 * spacing between the image and the text of buttons for TBSTYLE_LIST
5221 * toolbars. */
5222 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5224 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5226 infoPtr->iListGap = iListGap;
5228 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5230 return 0;
5233 /* Returns the number of maximum number of image lists associated with the
5234 * various states. */
5235 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5237 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5239 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5242 static LRESULT
5243 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5245 LPSIZE lpsize = (LPSIZE)lParam;
5247 if (lpsize == NULL)
5248 return FALSE;
5251 * Testing shows the following:
5252 * wParam = 0 adjust cx value
5253 * = 1 set cy value to max size.
5254 * lParam pointer to SIZE structure
5257 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5258 wParam, lParam, lpsize->cx, lpsize->cy);
5260 switch(wParam) {
5261 case 0:
5262 if (lpsize->cx == -1) {
5263 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5265 else if(HIWORD(lpsize->cx)) {
5266 RECT rc;
5267 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5269 GetWindowRect(infoPtr->hwndSelf, &rc);
5270 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5271 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5272 lpsize->cx = max(rc.right-rc.left,
5273 infoPtr->rcBound.right - infoPtr->rcBound.left);
5275 else {
5276 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5278 break;
5279 case 1:
5280 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5281 break;
5282 default:
5283 FIXME("Unknown wParam %ld\n", wParam);
5284 return 0;
5286 TRACE("set to -> 0x%08x 0x%08x\n",
5287 lpsize->cx, lpsize->cy);
5288 return 1;
5291 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5293 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5295 InvalidateRect(hwnd, NULL, TRUE);
5296 return 1;
5300 static LRESULT
5301 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5303 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5304 LOGFONTW logFont;
5306 TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
5308 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5309 GetClientRect(hwnd, &infoPtr->client_rect);
5310 infoPtr->bUnicode = infoPtr->hwndNotify &&
5311 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5312 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5314 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5315 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5316 infoPtr->hTheme = OpenThemeDataForDpi (NULL, themeClass, GetDpiForWindow (hwnd));
5318 TOOLBAR_CheckStyle (infoPtr);
5320 return 0;
5324 static LRESULT
5325 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5327 INT i;
5329 /* delete tooltip control */
5330 if (infoPtr->hwndToolTip)
5331 DestroyWindow (infoPtr->hwndToolTip);
5333 /* delete temporary buffer for tooltip text */
5334 Free (infoPtr->pszTooltipText);
5335 Free (infoPtr->bitmaps); /* bitmaps list */
5337 /* delete button data */
5338 for (i = 0; i < infoPtr->nNumButtons; i++)
5339 free_string( infoPtr->buttons + i );
5340 Free (infoPtr->buttons);
5342 /* delete strings */
5343 if (infoPtr->strings) {
5344 for (i = 0; i < infoPtr->nNumStrings; i++)
5345 Free (infoPtr->strings[i]);
5347 Free (infoPtr->strings);
5350 /* destroy internal image list */
5351 if (infoPtr->himlInt)
5352 ImageList_Destroy (infoPtr->himlInt);
5354 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5355 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5356 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5358 /* delete default font */
5359 DeleteObject (infoPtr->hDefaultFont);
5361 CloseThemeData (infoPtr->hTheme);
5363 /* free toolbar info data */
5364 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5365 Free (infoPtr);
5367 return 0;
5371 static LRESULT
5372 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5374 NMTBCUSTOMDRAW tbcd;
5375 INT ret = FALSE;
5376 DWORD ntfret;
5377 DWORD dwEraseCustDraw = 0;
5379 /* the app has told us not to redraw the toolbar */
5380 if (!infoPtr->bDoRedraw)
5381 return FALSE;
5383 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5384 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5385 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5386 tbcd.nmcd.hdc = (HDC)wParam;
5387 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5388 dwEraseCustDraw = ntfret & 0xffff;
5390 /* FIXME: in general the return flags *can* be or'ed together */
5391 switch (dwEraseCustDraw)
5393 case CDRF_DODEFAULT:
5394 break;
5395 case CDRF_SKIPDEFAULT:
5396 return TRUE;
5397 default:
5398 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5399 infoPtr->hwndSelf, ntfret);
5403 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5404 * to my parent for processing.
5406 if (infoPtr->hTheme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5407 POINT pt, ptorig;
5408 HDC hdc = (HDC)wParam;
5409 HWND parent;
5411 pt.x = 0;
5412 pt.y = 0;
5413 parent = GetParent(infoPtr->hwndSelf);
5414 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5415 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5416 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5417 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5419 if (!ret)
5420 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5422 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5423 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5424 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5425 tbcd.nmcd.hdc = (HDC)wParam;
5426 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5427 dwEraseCustDraw = ntfret & 0xffff;
5428 switch (dwEraseCustDraw)
5430 case CDRF_DODEFAULT:
5431 break;
5432 case CDRF_SKIPDEFAULT:
5433 return TRUE;
5434 default:
5435 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5436 infoPtr->hwndSelf, ntfret);
5439 return ret;
5443 static inline LRESULT
5444 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5446 return (LRESULT)infoPtr->hFont;
5450 static void
5451 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5453 INT i;
5454 INT nNewHotItem = infoPtr->nHotItem;
5456 for (i = 0; i < infoPtr->nNumButtons; i++)
5458 /* did we wrap? */
5459 if ((nNewHotItem + iDirection < 0) ||
5460 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5462 NMTBWRAPHOTITEM nmtbwhi;
5463 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5464 nmtbwhi.iDirection = iDirection;
5465 nmtbwhi.dwReason = dwReason;
5467 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5468 return;
5471 nNewHotItem += iDirection;
5472 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5474 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5475 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5477 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5478 break;
5483 static LRESULT
5484 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5486 NMKEY nmkey;
5488 nmkey.nVKey = (UINT)wParam;
5489 nmkey.uFlags = HIWORD(lParam);
5491 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5492 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5494 switch ((UINT)wParam)
5496 case VK_LEFT:
5497 case VK_UP:
5498 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5499 break;
5500 case VK_RIGHT:
5501 case VK_DOWN:
5502 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5503 break;
5504 case VK_SPACE:
5505 case VK_RETURN:
5506 if ((infoPtr->nHotItem >= 0) &&
5507 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5509 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5510 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5511 (LPARAM)infoPtr->hwndSelf);
5513 break;
5516 return 0;
5520 static LRESULT
5521 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5523 POINT pt;
5524 BOOL button;
5526 pt.x = (short)LOWORD(lParam);
5527 pt.y = (short)HIWORD(lParam);
5528 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5530 if (button)
5531 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5532 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5533 TOOLBAR_Customize (infoPtr);
5535 return 0;
5539 static LRESULT
5540 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5542 TBUTTON_INFO *btnPtr;
5543 POINT pt;
5544 INT nHit;
5545 NMTOOLBARA nmtb;
5546 NMMOUSE nmmouse;
5547 BOOL bDragKeyPressed;
5548 BOOL button;
5550 TRACE("\n");
5552 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5553 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5554 else
5555 bDragKeyPressed = (wParam & MK_SHIFT);
5557 if (infoPtr->hwndToolTip)
5558 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5559 WM_LBUTTONDOWN, wParam, lParam);
5561 pt.x = (short)LOWORD(lParam);
5562 pt.y = (short)HIWORD(lParam);
5563 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5565 if (button)
5567 btnPtr = &infoPtr->buttons[nHit];
5569 if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5571 infoPtr->nButtonDrag = nHit;
5572 SetCapture (infoPtr->hwndSelf);
5574 /* If drag cursor has not been loaded, load it.
5575 * Note: it doesn't need to be freed */
5576 if (!hCursorDrag)
5577 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5578 SetCursor(hCursorDrag);
5580 else
5582 RECT arrowRect;
5583 infoPtr->nOldHit = nHit;
5585 arrowRect = btnPtr->rect;
5586 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5588 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5589 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5590 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5591 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5592 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5593 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5595 LRESULT res;
5597 /* draw in pressed state */
5598 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5599 btnPtr->fsState |= TBSTATE_PRESSED;
5600 else
5601 btnPtr->bDropDownPressed = TRUE;
5602 RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5604 memset(&nmtb, 0, sizeof(nmtb));
5605 nmtb.iItem = btnPtr->idCommand;
5606 nmtb.rcButton = btnPtr->rect;
5607 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5608 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5610 if (res != TBDDRET_TREATPRESSED)
5612 MSG msg;
5614 /* redraw button in unpressed state */
5615 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5616 btnPtr->fsState &= ~TBSTATE_PRESSED;
5617 else
5618 btnPtr->bDropDownPressed = FALSE;
5619 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5621 /* find and set hot item */
5622 GetCursorPos(&pt);
5623 ScreenToClient(infoPtr->hwndSelf, &pt);
5624 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5625 if (!infoPtr->bAnchor || button)
5626 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5628 /* remove any left mouse button down or double-click messages
5629 * so that we can get a toggle effect on the button */
5630 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5631 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5634 return 0;
5636 /* otherwise drop through and process as pushed */
5638 infoPtr->bCaptured = TRUE;
5639 infoPtr->nButtonDown = nHit;
5640 infoPtr->bDragOutSent = FALSE;
5642 btnPtr->fsState |= TBSTATE_PRESSED;
5644 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5646 if (btnPtr->fsState & TBSTATE_ENABLED)
5647 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5648 UpdateWindow(infoPtr->hwndSelf);
5649 SetCapture (infoPtr->hwndSelf);
5652 memset(&nmtb, 0, sizeof(nmtb));
5653 nmtb.iItem = btnPtr->idCommand;
5654 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5657 nmmouse.dwHitInfo = nHit;
5659 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5660 if (!button)
5661 nmmouse.dwItemSpec = -1;
5662 else
5664 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5665 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5668 ClientToScreen(infoPtr->hwndSelf, &pt);
5669 nmmouse.pt = pt;
5671 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5672 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5674 return 0;
5677 static LRESULT
5678 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5680 TBUTTON_INFO *btnPtr;
5681 POINT pt;
5682 INT nHit;
5683 INT nOldIndex = -1;
5684 NMHDR hdr;
5685 NMMOUSE nmmouse;
5686 NMTOOLBARA nmtb;
5687 BOOL button;
5689 if (infoPtr->hwndToolTip)
5690 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5691 WM_LBUTTONUP, wParam, lParam);
5693 pt.x = (short)LOWORD(lParam);
5694 pt.y = (short)HIWORD(lParam);
5695 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5697 if (!infoPtr->bAnchor || button)
5698 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5700 if (infoPtr->nButtonDrag >= 0) {
5701 RECT rcClient;
5702 NMHDR hdr;
5704 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5705 ReleaseCapture();
5706 /* reset cursor */
5707 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5709 GetClientRect(infoPtr->hwndSelf, &rcClient);
5710 if (PtInRect(&rcClient, pt))
5712 INT nButton = -1;
5713 if (nHit >= 0)
5714 nButton = nHit;
5715 else if (nHit < -1)
5716 nButton = -nHit;
5717 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5718 nButton = -nHit;
5720 if (nButton == infoPtr->nButtonDrag)
5722 /* if the button is moved slightly left and we have a
5723 * separator there then remove it */
5724 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5726 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5727 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5729 else /* else insert a separator before the dragged button */
5731 TBBUTTON tbb;
5732 memset(&tbb, 0, sizeof(tbb));
5733 tbb.fsStyle = BTNS_SEP;
5734 tbb.iString = -1;
5735 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5738 else
5740 if (nButton == -1)
5742 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5743 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5744 else
5745 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5747 else
5748 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5751 else
5753 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5754 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5757 /* button under cursor changed so need to re-set hot item */
5758 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5759 infoPtr->nButtonDrag = -1;
5761 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5763 else if (infoPtr->nButtonDown >= 0)
5765 BOOL was_clicked = nHit == infoPtr->nButtonDown;
5767 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5768 btnPtr->fsState &= ~TBSTATE_PRESSED;
5770 if (btnPtr->fsStyle & BTNS_CHECK) {
5771 if (btnPtr->fsStyle & BTNS_GROUP) {
5772 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5773 nHit);
5774 if ((nOldIndex != nHit) &&
5775 (nOldIndex != -1))
5776 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5777 btnPtr->fsState |= TBSTATE_CHECKED;
5779 else {
5780 if (btnPtr->fsState & TBSTATE_CHECKED)
5781 btnPtr->fsState &= ~TBSTATE_CHECKED;
5782 else
5783 btnPtr->fsState |= TBSTATE_CHECKED;
5787 if (nOldIndex != -1)
5788 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5791 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5792 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5793 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5795 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5796 ReleaseCapture ();
5797 infoPtr->nButtonDown = -1;
5799 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5800 TOOLBAR_SendNotify (&hdr, infoPtr,
5801 NM_RELEASEDCAPTURE);
5803 memset(&nmtb, 0, sizeof(nmtb));
5804 nmtb.iItem = btnPtr->idCommand;
5805 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5806 TBN_ENDDRAG);
5808 if (was_clicked && btnPtr->fsState & TBSTATE_ENABLED)
5810 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5811 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5813 /* In case we have just been destroyed... */
5814 if(!IsWindow(infoPtr->hwndSelf))
5815 return 0;
5819 /* !!! Undocumented - toolbar at 4.71 level and above sends
5820 * NM_CLICK with the NMMOUSE structure. */
5821 nmmouse.dwHitInfo = nHit;
5823 if (!button)
5824 nmmouse.dwItemSpec = -1;
5825 else
5827 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5828 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5831 ClientToScreen(infoPtr->hwndSelf, &pt);
5832 nmmouse.pt = pt;
5834 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5835 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5837 return 0;
5840 static LRESULT
5841 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5843 INT nHit;
5844 NMMOUSE nmmouse;
5845 POINT pt;
5846 BOOL button;
5848 pt.x = (short)LOWORD(lParam);
5849 pt.y = (short)HIWORD(lParam);
5851 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5852 nmmouse.dwHitInfo = nHit;
5854 if (!button) {
5855 nmmouse.dwItemSpec = -1;
5856 } else {
5857 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5858 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5861 ClientToScreen(infoPtr->hwndSelf, &pt);
5862 nmmouse.pt = pt;
5864 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5865 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5867 return 0;
5870 static LRESULT
5871 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5873 NMHDR nmhdr;
5875 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5876 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5878 return 0;
5881 static LRESULT
5882 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5884 TBUTTON_INFO *btnPtr;
5886 infoPtr->bCaptured = FALSE;
5888 if (infoPtr->nButtonDown >= 0)
5890 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5891 btnPtr->fsState &= ~TBSTATE_PRESSED;
5893 infoPtr->nOldHit = -1;
5895 if (btnPtr->fsState & TBSTATE_ENABLED)
5896 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5898 return 0;
5901 static LRESULT
5902 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5904 /* don't remove hot effects when in anchor highlighting mode or when a
5905 * drop-down button is pressed */
5906 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5908 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5909 if (!hotBtnPtr->bDropDownPressed)
5910 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5913 if (infoPtr->nOldHit < 0)
5914 return TRUE;
5916 /* If the last button we were over is depressed then make it not */
5917 /* depressed and redraw it */
5918 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5920 TBUTTON_INFO *btnPtr;
5921 RECT rc1;
5923 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5925 btnPtr->fsState &= ~TBSTATE_PRESSED;
5927 rc1 = btnPtr->rect;
5928 InflateRect (&rc1, 1, 1);
5929 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5932 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5934 NMTOOLBARW nmt;
5935 ZeroMemory(&nmt, sizeof(nmt));
5936 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5937 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5938 infoPtr->bDragOutSent = TRUE;
5941 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5943 return TRUE;
5946 static LRESULT
5947 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5949 POINT pt;
5950 TRACKMOUSEEVENT trackinfo;
5951 INT nHit;
5952 TBUTTON_INFO *btnPtr;
5953 BOOL button;
5955 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5956 TOOLBAR_TooltipCreateControl(infoPtr);
5958 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme) {
5959 /* fill in the TRACKMOUSEEVENT struct */
5960 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5961 trackinfo.dwFlags = TME_QUERY;
5963 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5964 _TrackMouseEvent(&trackinfo);
5966 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5967 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5968 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5969 trackinfo.hwndTrack = infoPtr->hwndSelf;
5971 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5972 /* and can properly deactivate the hot toolbar button */
5973 _TrackMouseEvent(&trackinfo);
5977 if (infoPtr->hwndToolTip)
5978 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5979 WM_MOUSEMOVE, wParam, lParam);
5981 pt.x = (short)LOWORD(lParam);
5982 pt.y = (short)HIWORD(lParam);
5984 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5986 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme) && (!infoPtr->bAnchor || button))
5987 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5989 if (infoPtr->nOldHit != nHit)
5991 if (infoPtr->bCaptured)
5993 if (!infoPtr->bDragOutSent)
5995 NMTOOLBARW nmt;
5996 ZeroMemory(&nmt, sizeof(nmt));
5997 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5998 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5999 infoPtr->bDragOutSent = TRUE;
6002 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
6003 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
6004 btnPtr->fsState &= ~TBSTATE_PRESSED;
6005 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6007 else if (nHit == infoPtr->nButtonDown) {
6008 btnPtr->fsState |= TBSTATE_PRESSED;
6009 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6011 infoPtr->nOldHit = nHit;
6015 return 0;
6019 static inline LRESULT
6020 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
6022 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
6023 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
6024 /* else */
6025 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
6029 static inline LRESULT
6030 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
6032 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6033 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6035 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6039 static LRESULT
6040 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
6042 TOOLBAR_INFO *infoPtr;
6043 DWORD styleadd = 0;
6045 /* allocate memory for info structure */
6046 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6047 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6049 /* paranoid!! */
6050 infoPtr->dwStructSize = sizeof(TBBUTTON);
6051 infoPtr->nRows = 1;
6053 /* initialize info structure */
6054 infoPtr->nButtonWidth = 23;
6055 infoPtr->nButtonHeight = 22;
6056 infoPtr->nBitmapHeight = 16;
6057 infoPtr->nBitmapWidth = 16;
6059 infoPtr->nMaxTextRows = 1;
6060 infoPtr->cxMin = -1;
6061 infoPtr->cxMax = -1;
6062 infoPtr->nNumBitmaps = 0;
6063 infoPtr->nNumStrings = 0;
6065 infoPtr->bCaptured = FALSE;
6066 infoPtr->nButtonDown = -1;
6067 infoPtr->nButtonDrag = -1;
6068 infoPtr->nOldHit = -1;
6069 infoPtr->nHotItem = -1;
6070 infoPtr->hwndNotify = lpcs->hwndParent;
6071 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6072 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
6073 infoPtr->bDragOutSent = FALSE;
6074 infoPtr->iVersion = 0;
6075 infoPtr->hwndSelf = hwnd;
6076 infoPtr->bDoRedraw = TRUE;
6077 infoPtr->clrBtnHighlight = CLR_DEFAULT;
6078 infoPtr->clrBtnShadow = CLR_DEFAULT;
6079 infoPtr->szPadding.cx = DEFPAD_CX;
6080 infoPtr->szPadding.cy = DEFPAD_CY;
6081 infoPtr->iListGap = DEFLISTGAP;
6082 infoPtr->iTopMargin = default_top_margin(infoPtr);
6083 infoPtr->dwStyle = lpcs->style;
6084 infoPtr->tbim.iButton = -1;
6086 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6087 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6088 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6089 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6092 /* I think the code below is a bug, but it is the way that the native
6093 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6094 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6095 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6096 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6097 * Somehow, the only cases of this seem to be MFC programs.
6099 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6100 * does not occur in the WM_STYLECHANGING routine.
6101 * (Guy Albertelli 9/2001)
6104 if ((infoPtr->dwStyle & TBSTYLE_FLAT) && !(lpcs->style & TBSTYLE_TRANSPARENT))
6105 styleadd |= TBSTYLE_TRANSPARENT;
6106 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6107 styleadd |= CCS_TOP; /* default to top */
6108 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6111 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
6115 static LRESULT
6116 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6118 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6119 RECT rcWindow;
6120 HDC hdc;
6122 if (dwStyle & WS_MINIMIZE)
6123 return 0; /* Nothing to do */
6125 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6127 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6128 return 0;
6130 if (!(dwStyle & CCS_NODIVIDER))
6132 GetWindowRect (hwnd, &rcWindow);
6133 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6134 if( dwStyle & WS_BORDER )
6135 InflateRect (&rcWindow, -1, -1);
6136 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6139 ReleaseDC( hwnd, hdc );
6141 return 0;
6145 /* handles requests from the tooltip control on what text to display */
6146 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6148 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6149 NMTTDISPINFOA nmtdi;
6150 unsigned int len;
6151 LRESULT ret;
6153 TRACE("button index = %d\n", index);
6155 Free (infoPtr->pszTooltipText);
6156 infoPtr->pszTooltipText = NULL;
6158 if (index < 0)
6159 return 0;
6161 if (infoPtr->bUnicode)
6163 WCHAR wszBuffer[INFOTIPSIZE+1];
6164 NMTBGETINFOTIPW tbgit;
6166 wszBuffer[0] = '\0';
6167 wszBuffer[INFOTIPSIZE] = '\0';
6169 tbgit.pszText = wszBuffer;
6170 tbgit.cchTextMax = INFOTIPSIZE;
6171 tbgit.iItem = lpnmtdi->hdr.idFrom;
6172 tbgit.lParam = infoPtr->buttons[index].dwData;
6174 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6176 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6178 len = tbgit.pszText ? lstrlenW(tbgit.pszText) : 0;
6179 if (len > ARRAY_SIZE(lpnmtdi->szText) - 1)
6181 /* need to allocate temporary buffer in infoPtr as there
6182 * isn't enough space in buffer passed to us by the
6183 * tooltip control */
6184 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6185 if (infoPtr->pszTooltipText)
6187 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6188 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6189 return 0;
6192 else if (len > 0)
6194 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6195 return 0;
6198 else
6200 CHAR szBuffer[INFOTIPSIZE+1];
6201 NMTBGETINFOTIPA tbgit;
6203 szBuffer[0] = '\0';
6204 szBuffer[INFOTIPSIZE] = '\0';
6206 tbgit.pszText = szBuffer;
6207 tbgit.cchTextMax = INFOTIPSIZE;
6208 tbgit.iItem = lpnmtdi->hdr.idFrom;
6209 tbgit.lParam = infoPtr->buttons[index].dwData;
6211 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6213 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6215 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6216 if (len > ARRAY_SIZE(lpnmtdi->szText))
6218 /* need to allocate temporary buffer in infoPtr as there
6219 * isn't enough space in buffer passed to us by the
6220 * tooltip control */
6221 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6222 if (infoPtr->pszTooltipText)
6224 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6225 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6226 return 0;
6229 else if (tbgit.pszText && tbgit.pszText[0])
6231 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, lpnmtdi->lpszText, ARRAY_SIZE(lpnmtdi->szText));
6232 return 0;
6236 /* if button has text, but it is not shown then automatically
6237 * use that text as tooltip */
6238 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6239 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6241 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6242 len = pszText ? lstrlenW(pszText) : 0;
6244 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6246 if (len > ARRAY_SIZE(lpnmtdi->szText) - 1)
6248 /* need to allocate temporary buffer in infoPtr as there
6249 * isn't enough space in buffer passed to us by the
6250 * tooltip control */
6251 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6252 if (infoPtr->pszTooltipText)
6254 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6255 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6256 return 0;
6259 else if (len > 0)
6261 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6262 return 0;
6266 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6268 /* Last resort, forward TTN_GETDISPINFO to the app:
6270 - NFR_UNICODE gets TTN_GETDISPINFOW, and TTN_GETDISPINFOA if -W returned no text;
6271 - NFR_ANSI gets only TTN_GETDISPINFOA.
6273 if (infoPtr->bUnicode)
6275 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6277 TRACE("TTN_GETDISPINFOW - got string %s\n", debugstr_w(lpnmtdi->lpszText));
6279 if (IS_INTRESOURCE(lpnmtdi->lpszText))
6280 return ret;
6282 if (lpnmtdi->lpszText && *lpnmtdi->lpszText)
6283 return ret;
6286 nmtdi.hdr.hwndFrom = lpnmtdi->hdr.hwndFrom;
6287 nmtdi.hdr.idFrom = lpnmtdi->hdr.idFrom;
6288 nmtdi.hdr.code = TTN_GETDISPINFOA;
6289 nmtdi.lpszText = nmtdi.szText;
6290 nmtdi.szText[0] = 0;
6291 nmtdi.hinst = lpnmtdi->hinst;
6292 nmtdi.uFlags = lpnmtdi->uFlags;
6293 nmtdi.lParam = lpnmtdi->lParam;
6295 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmtdi.hdr.idFrom, (LPARAM)&nmtdi);
6297 TRACE("TTN_GETDISPINFOA - got string %s\n", debugstr_a(nmtdi.lpszText));
6299 lpnmtdi->hinst = nmtdi.hinst;
6300 lpnmtdi->uFlags = nmtdi.uFlags;
6301 lpnmtdi->lParam = nmtdi.lParam;
6303 if (IS_INTRESOURCE(nmtdi.lpszText))
6305 lpnmtdi->lpszText = (WCHAR *)nmtdi.lpszText;
6306 return ret;
6309 if (!nmtdi.lpszText || !*nmtdi.lpszText)
6310 return ret;
6312 len = MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, NULL, 0);
6313 if (len > ARRAY_SIZE(lpnmtdi->szText))
6315 infoPtr->pszTooltipText = Alloc(len * sizeof(WCHAR));
6316 if (infoPtr->pszTooltipText)
6318 MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, infoPtr->pszTooltipText, len);
6319 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6320 return 0;
6323 else
6325 MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, lpnmtdi->lpszText, ARRAY_SIZE(nmtdi.szText));
6326 return 0;
6329 return ret;
6333 static inline LRESULT
6334 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6336 switch (lpnmh->code)
6338 case PGN_CALCSIZE:
6340 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6342 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6343 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6344 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6345 lppgc->iWidth);
6347 else {
6348 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6349 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6350 lppgc->iHeight);
6352 return 0;
6355 case PGN_SCROLL:
6357 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6359 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6360 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6361 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6362 lppgs->iScroll, lppgs->iDir);
6363 return 0;
6366 case TTN_GETDISPINFOW:
6367 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6369 case TTN_GETDISPINFOA:
6370 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6371 return 0;
6373 default:
6374 return 0;
6379 static LRESULT
6380 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6382 LRESULT format;
6384 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6386 if (lParam == NF_QUERY)
6387 return NFR_UNICODE;
6389 if (lParam == NF_REQUERY) {
6390 format = SendMessageW(infoPtr->hwndNotify,
6391 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6392 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6393 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6394 format);
6395 format = NFR_ANSI;
6397 return format;
6399 return 0;
6403 static LRESULT
6404 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6406 HDC hdc;
6407 PAINTSTRUCT ps;
6409 /* fill ps.rcPaint with a default rect */
6410 ps.rcPaint = infoPtr->rcBound;
6412 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6414 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6416 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6417 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6419 return 0;
6423 static LRESULT
6424 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6426 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6428 /* make first item hot */
6429 if (infoPtr->nNumButtons > 0)
6430 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6432 return 0;
6435 static LRESULT
6436 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6438 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6440 if (hFont == 0)
6441 infoPtr->hFont = infoPtr->hDefaultFont;
6442 else
6443 infoPtr->hFont = hFont;
6445 TOOLBAR_CalcToolbar(infoPtr);
6447 if (Redraw)
6448 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6449 return 1;
6452 static LRESULT
6453 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6454 /*****************************************************
6456 * Function;
6457 * Handles the WM_SETREDRAW message.
6459 * Documentation:
6460 * According to testing V4.71 of COMCTL32 returns the
6461 * *previous* status of the redraw flag (either 0 or 1)
6462 * instead of the MSDN documented value of 0 if handled.
6463 * (For laughs see the "consistency" with same function
6464 * in rebar.)
6466 *****************************************************/
6468 BOOL oldredraw = infoPtr->bDoRedraw;
6470 TRACE("set to %s\n",
6471 (wParam) ? "TRUE" : "FALSE");
6472 infoPtr->bDoRedraw = (BOOL) wParam;
6473 if (wParam) {
6474 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6476 return (oldredraw) ? 1 : 0;
6480 static LRESULT
6481 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6483 TRACE("sizing toolbar\n");
6485 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6487 RECT delta_width, delta_height, client, dummy;
6488 DWORD min_x, max_x, min_y, max_y;
6489 TBUTTON_INFO *btnPtr;
6490 INT i;
6492 GetClientRect(infoPtr->hwndSelf, &client);
6493 if(client.right > infoPtr->client_rect.right)
6495 min_x = infoPtr->client_rect.right;
6496 max_x = client.right;
6498 else
6500 max_x = infoPtr->client_rect.right;
6501 min_x = client.right;
6503 if(client.bottom > infoPtr->client_rect.bottom)
6505 min_y = infoPtr->client_rect.bottom;
6506 max_y = client.bottom;
6508 else
6510 max_y = infoPtr->client_rect.bottom;
6511 min_y = client.bottom;
6514 SetRect(&delta_width, min_x, 0, max_x, min_y);
6515 SetRect(&delta_height, 0, min_y, max_x, max_y);
6517 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6518 btnPtr = infoPtr->buttons;
6519 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6520 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6521 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6522 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6524 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6525 TOOLBAR_AutoSize(infoPtr);
6526 return 0;
6530 static LRESULT
6531 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6533 if (nType == GWL_STYLE)
6534 return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6536 return 0;
6540 static LRESULT
6541 TOOLBAR_SysColorChange (void)
6543 COMCTL32_RefreshSysColors();
6545 return 0;
6549 /* update theme after a WM_THEMECHANGED message */
6550 static LRESULT theme_changed (TOOLBAR_INFO *infoPtr)
6552 CloseThemeData (infoPtr->hTheme);
6553 infoPtr->hTheme = OpenThemeDataForDpi (NULL, themeClass, GetDpiForWindow (infoPtr->hwndSelf));
6554 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
6555 return 0;
6559 static LRESULT WINAPI
6560 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6562 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6564 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6565 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6567 if (!infoPtr && (uMsg != WM_NCCREATE))
6568 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6570 switch (uMsg)
6572 case TB_ADDBITMAP:
6573 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6575 case TB_ADDBUTTONSA:
6576 case TB_ADDBUTTONSW:
6577 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6578 uMsg == TB_ADDBUTTONSW);
6579 case TB_ADDSTRINGA:
6580 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6582 case TB_ADDSTRINGW:
6583 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6585 case TB_AUTOSIZE:
6586 return TOOLBAR_AutoSize (infoPtr);
6588 case TB_BUTTONCOUNT:
6589 return TOOLBAR_ButtonCount (infoPtr);
6591 case TB_BUTTONSTRUCTSIZE:
6592 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6594 case TB_CHANGEBITMAP:
6595 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6597 case TB_CHECKBUTTON:
6598 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6600 case TB_COMMANDTOINDEX:
6601 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6603 case TB_CUSTOMIZE:
6604 return TOOLBAR_Customize (infoPtr);
6606 case TB_DELETEBUTTON:
6607 return TOOLBAR_DeleteButton (infoPtr, wParam);
6609 case TB_ENABLEBUTTON:
6610 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6612 case TB_GETANCHORHIGHLIGHT:
6613 return TOOLBAR_GetAnchorHighlight (infoPtr);
6615 case TB_GETBITMAP:
6616 return TOOLBAR_GetBitmap (infoPtr, wParam);
6618 case TB_GETBITMAPFLAGS:
6619 return TOOLBAR_GetBitmapFlags ();
6621 case TB_GETBUTTON:
6622 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6624 case TB_GETBUTTONINFOA:
6625 case TB_GETBUTTONINFOW:
6626 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6627 uMsg == TB_GETBUTTONINFOW);
6628 case TB_GETBUTTONSIZE:
6629 return TOOLBAR_GetButtonSize (infoPtr);
6631 case TB_GETBUTTONTEXTA:
6632 case TB_GETBUTTONTEXTW:
6633 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6634 uMsg == TB_GETBUTTONTEXTW);
6636 case TB_GETDISABLEDIMAGELIST:
6637 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6639 case TB_GETEXTENDEDSTYLE:
6640 return TOOLBAR_GetExtendedStyle (infoPtr);
6642 case TB_GETHOTIMAGELIST:
6643 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6645 case TB_GETHOTITEM:
6646 return TOOLBAR_GetHotItem (infoPtr);
6648 case TB_GETIMAGELIST:
6649 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6651 case TB_GETINSERTMARK:
6652 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6654 case TB_GETINSERTMARKCOLOR:
6655 return TOOLBAR_GetInsertMarkColor (infoPtr);
6657 case TB_GETITEMRECT:
6658 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6660 case TB_GETMAXSIZE:
6661 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6663 /* case TB_GETOBJECT: */ /* 4.71 */
6665 case TB_GETPADDING:
6666 return TOOLBAR_GetPadding (infoPtr);
6668 case TB_GETRECT:
6669 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6671 case TB_GETROWS:
6672 return TOOLBAR_GetRows (infoPtr);
6674 case TB_GETSTATE:
6675 return TOOLBAR_GetState (infoPtr, wParam);
6677 case TB_GETSTRINGA:
6678 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6680 case TB_GETSTRINGW:
6681 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6683 case TB_GETSTYLE:
6684 return TOOLBAR_GetStyle (infoPtr);
6686 case TB_GETTEXTROWS:
6687 return TOOLBAR_GetTextRows (infoPtr);
6689 case TB_GETTOOLTIPS:
6690 return TOOLBAR_GetToolTips (infoPtr);
6692 case TB_GETUNICODEFORMAT:
6693 return TOOLBAR_GetUnicodeFormat (infoPtr);
6695 case TB_HIDEBUTTON:
6696 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6698 case TB_HITTEST:
6699 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6701 case TB_INDETERMINATE:
6702 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6704 case TB_INSERTBUTTONA:
6705 case TB_INSERTBUTTONW:
6706 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6707 uMsg == TB_INSERTBUTTONW);
6709 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6711 case TB_ISBUTTONCHECKED:
6712 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6714 case TB_ISBUTTONENABLED:
6715 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6717 case TB_ISBUTTONHIDDEN:
6718 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6720 case TB_ISBUTTONHIGHLIGHTED:
6721 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6723 case TB_ISBUTTONINDETERMINATE:
6724 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6726 case TB_ISBUTTONPRESSED:
6727 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6729 case TB_LOADIMAGES:
6730 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6732 case TB_MAPACCELERATORA:
6733 case TB_MAPACCELERATORW:
6734 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6736 case TB_MARKBUTTON:
6737 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6739 case TB_MOVEBUTTON:
6740 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6742 case TB_PRESSBUTTON:
6743 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6745 case TB_REPLACEBITMAP:
6746 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6748 case TB_SAVERESTOREA:
6749 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6751 case TB_SAVERESTOREW:
6752 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6754 case TB_SETANCHORHIGHLIGHT:
6755 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6757 case TB_SETBITMAPSIZE:
6758 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6760 case TB_SETBUTTONINFOA:
6761 case TB_SETBUTTONINFOW:
6762 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6763 uMsg == TB_SETBUTTONINFOW);
6764 case TB_SETBUTTONSIZE:
6765 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6767 case TB_SETBUTTONWIDTH:
6768 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6770 case TB_SETCMDID:
6771 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6773 case TB_SETDISABLEDIMAGELIST:
6774 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6776 case TB_SETDRAWTEXTFLAGS:
6777 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6779 case TB_SETEXTENDEDSTYLE:
6780 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6782 case TB_SETHOTIMAGELIST:
6783 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6785 case TB_SETHOTITEM:
6786 return TOOLBAR_SetHotItem (infoPtr, wParam);
6788 case TB_SETIMAGELIST:
6789 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6791 case TB_SETINDENT:
6792 return TOOLBAR_SetIndent (infoPtr, wParam);
6794 case TB_SETINSERTMARK:
6795 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6797 case TB_SETINSERTMARKCOLOR:
6798 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6800 case TB_SETMAXTEXTROWS:
6801 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6803 case TB_SETPADDING:
6804 return TOOLBAR_SetPadding (infoPtr, lParam);
6806 case TB_SETPARENT:
6807 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6809 case TB_SETROWS:
6810 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6812 case TB_SETSTATE:
6813 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6815 case TB_SETSTYLE:
6816 return TOOLBAR_SetStyle (infoPtr, lParam);
6818 case TB_SETTOOLTIPS:
6819 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6821 case TB_SETUNICODEFORMAT:
6822 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6824 case TB_SETBOUNDINGSIZE:
6825 return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6827 case TB_SETHOTITEM2:
6828 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6830 case TB_SETLISTGAP:
6831 return TOOLBAR_SetListGap(infoPtr, wParam);
6833 case TB_GETIMAGELISTCOUNT:
6834 return TOOLBAR_GetImageListCount(infoPtr);
6836 case TB_GETIDEALSIZE:
6837 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6839 case TB_UNKWN464:
6840 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6842 /* Common Control Messages */
6844 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6845 case CCM_GETCOLORSCHEME:
6846 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6848 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6849 case CCM_SETCOLORSCHEME:
6850 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6852 case CCM_GETVERSION:
6853 return TOOLBAR_GetVersion (infoPtr);
6855 case CCM_SETVERSION:
6856 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6859 /* case WM_CHAR: */
6861 case WM_CREATE:
6862 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6864 case WM_DESTROY:
6865 return TOOLBAR_Destroy (infoPtr);
6867 case WM_ERASEBKGND:
6868 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6870 case WM_GETFONT:
6871 return TOOLBAR_GetFont (infoPtr);
6873 case WM_KEYDOWN:
6874 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6876 /* case WM_KILLFOCUS: */
6878 case WM_LBUTTONDBLCLK:
6879 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6881 case WM_LBUTTONDOWN:
6882 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6884 case WM_LBUTTONUP:
6885 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6887 case WM_RBUTTONUP:
6888 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6890 case WM_RBUTTONDBLCLK:
6891 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6893 case WM_MOUSEMOVE:
6894 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6896 case WM_MOUSELEAVE:
6897 return TOOLBAR_MouseLeave (infoPtr);
6899 case WM_CAPTURECHANGED:
6900 if (hwnd == (HWND)lParam) return 0;
6901 return TOOLBAR_CaptureChanged(infoPtr);
6903 case WM_NCACTIVATE:
6904 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6906 case WM_NCCALCSIZE:
6907 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6909 case WM_NCCREATE:
6910 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6912 case WM_NCPAINT:
6913 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6915 case WM_NOTIFY:
6916 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6918 case WM_NOTIFYFORMAT:
6919 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6921 case WM_PRINTCLIENT:
6922 case WM_PAINT:
6923 return TOOLBAR_Paint (infoPtr, wParam);
6925 case WM_SETFOCUS:
6926 return TOOLBAR_SetFocus (infoPtr);
6928 case WM_SETFONT:
6929 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6931 case WM_SETREDRAW:
6932 return TOOLBAR_SetRedraw (infoPtr, wParam);
6934 case WM_SIZE:
6935 return TOOLBAR_Size (infoPtr);
6937 case WM_STYLECHANGED:
6938 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6940 case WM_SYSCOLORCHANGE:
6941 return TOOLBAR_SysColorChange ();
6943 case WM_THEMECHANGED:
6944 return theme_changed (infoPtr);
6946 /* case WM_WININICHANGE: */
6948 case WM_CHARTOITEM:
6949 case WM_COMMAND:
6950 case WM_DRAWITEM:
6951 case WM_MEASUREITEM:
6952 case WM_VKEYTOITEM:
6953 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6955 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6956 case PGM_FORWARDMOUSE:
6957 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6959 default:
6960 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6961 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6962 uMsg, wParam, lParam);
6963 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6968 VOID
6969 TOOLBAR_Register (void)
6971 WNDCLASSW wndClass;
6973 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6974 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6975 wndClass.lpfnWndProc = ToolbarWindowProc;
6976 wndClass.cbClsExtra = 0;
6977 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6978 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6979 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6980 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6982 RegisterClassW (&wndClass);
6986 VOID
6987 TOOLBAR_Unregister (void)
6989 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6992 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6994 HIMAGELIST himlold;
6995 PIMLENTRY c = NULL;
6997 /* Check if the entry already exists */
6998 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
7000 /* Don't add new entry for NULL imagelist */
7001 if (!c && !himl)
7002 return NULL;
7004 /* If this is a new entry we must create it and insert into the array */
7005 if (!c)
7007 PIMLENTRY *pnies;
7009 c = Alloc(sizeof(IMLENTRY));
7010 c->id = id;
7012 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
7013 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
7014 pnies[*cies] = c;
7015 (*cies)++;
7017 Free(*pies);
7018 *pies = pnies;
7021 himlold = c->himl;
7022 c->himl = himl;
7024 return himlold;
7028 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
7030 int i;
7032 for (i = 0; i < *cies; i++)
7033 Free((*pies)[i]);
7035 Free(*pies);
7037 *cies = 0;
7038 *pies = NULL;
7042 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
7044 PIMLENTRY c = NULL;
7046 if (pies != NULL)
7048 int i;
7050 for (i = 0; i < cies; i++)
7052 if (pies[i]->id == id)
7054 c = pies[i];
7055 break;
7060 return c;
7064 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
7066 HIMAGELIST himlDef = 0;
7067 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
7069 if (pie)
7070 himlDef = pie->himl;
7072 return himlDef;
7076 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
7078 if (infoPtr->bUnicode)
7079 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
7080 else
7082 CHAR Buffer[256];
7083 NMTOOLBARA nmtba;
7084 BOOL bRet = FALSE;
7086 nmtba.iItem = nmtb->iItem;
7087 nmtba.pszText = Buffer;
7088 nmtba.cchText = 256;
7089 ZeroMemory(nmtba.pszText, nmtba.cchText);
7091 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7093 int ccht = strlen(nmtba.pszText);
7094 if (ccht)
7095 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7096 nmtb->pszText, nmtb->cchText);
7098 nmtb->tbButton = nmtba.tbButton;
7099 bRet = TRUE;
7102 return bRet;
7107 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7109 NMTOOLBARW nmtb;
7111 /* MSDN states that iItem is the index of the button, rather than the
7112 * command ID as used by every other NMTOOLBAR notification */
7113 nmtb.iItem = iItem;
7114 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7116 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);