kernelbase/tests: Use win_skip() for missing APIs.
[wine.git] / dlls / comctl32 / toolbar.c
blob02538eeb64981d5b3e2e74670e310a1ae94b3695
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 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%Ix, stringid=%Ix\n",
296 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap), bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
297 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
298 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n", btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
299 wine_dbgstr_rect(&bP->rect));
303 static void
304 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
306 INT i;
308 if (!TRACE_ON(toolbar))
309 return;
311 TRACE("toolbar %p at line %d, exStyle=%#lx, buttons=%d, bitmaps=%d, strings=%d, style=%#lx\n",
312 iP->hwndSelf, line, iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps, iP->nNumStrings, iP->dwStyle);
313 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
314 iP->hwndSelf, line, iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis, (iP->bDoRedraw) ? "TRUE" : "FALSE");
316 for (i = 0; i < iP->nNumButtons; ++i)
317 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
320 static inline BOOL
321 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
323 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
326 static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
328 if (!IS_INTRESOURCE( str ) && str != -1)
330 if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;
332 if (unicode)
333 Str_SetPtrW( (WCHAR **)&btn->iString, (WCHAR *)str );
334 else
335 Str_SetPtrAtoW( (WCHAR **)&btn->iString, (char *)str );
337 else
339 if (TOOLBAR_ButtonHasString( btn )) Free( (WCHAR *)btn->iString );
341 btn->iString = str;
345 static void set_stringT( TBUTTON_INFO *btn, const WCHAR *str, BOOL unicode )
347 if (IS_INTRESOURCE( (DWORD_PTR)str ) || (DWORD_PTR)str == -1) return;
348 set_string_index( btn, (DWORD_PTR)str, unicode );
351 static void free_string( TBUTTON_INFO *btn )
353 set_string_index( btn, 0, TRUE );
357 /***********************************************************************
358 * TOOLBAR_CheckStyle
360 * This function validates that the styles set are implemented and
361 * issues FIXMEs warning of possible problems. In a perfect world this
362 * function should be null.
364 static void
365 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
367 if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
368 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
372 static INT
373 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
375 if(!IsWindow(infoPtr->hwndSelf))
376 return 0; /* we have just been destroyed */
378 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
379 nmhdr->hwndFrom = infoPtr->hwndSelf;
380 nmhdr->code = code;
382 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
383 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
385 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
388 /***********************************************************************
389 * TOOLBAR_GetBitmapIndex
391 * This function returns the bitmap index associated with a button.
392 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
393 * is issued to retrieve the index.
395 static INT
396 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
398 INT ret = btnPtr->iBitmap;
400 if (ret == I_IMAGECALLBACK)
402 /* issue TBN_GETDISPINFO */
403 NMTBDISPINFOW nmgd;
405 memset(&nmgd, 0, sizeof(nmgd));
406 nmgd.idCommand = btnPtr->idCommand;
407 nmgd.lParam = btnPtr->dwData;
408 nmgd.dwMask = TBNF_IMAGE;
409 nmgd.iImage = -1;
410 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
411 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
412 if (nmgd.dwMask & TBNF_DI_SETITEM)
413 btnPtr->iBitmap = nmgd.iImage;
414 ret = nmgd.iImage;
415 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask %#lx, nNumBitmaps %d\n",
416 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
419 if (ret != I_IMAGENONE)
420 ret = GETIBITMAP(infoPtr, ret);
422 return ret;
426 static BOOL
427 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
429 HIMAGELIST himl;
430 INT id = GETHIMLID(infoPtr, index);
431 INT iBitmap = GETIBITMAP(infoPtr, index);
433 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
434 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
435 (index == I_IMAGECALLBACK))
436 return TRUE;
437 else
438 return FALSE;
442 static inline BOOL
443 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
445 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
446 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
450 /***********************************************************************
451 * TOOLBAR_GetImageListForDrawing
453 * This function validates the bitmap index (including I_IMAGECALLBACK
454 * functionality) and returns the corresponding image list.
456 static HIMAGELIST
457 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
458 IMAGE_LIST_TYPE imagelist, INT * index)
460 HIMAGELIST himl;
462 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
463 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
464 WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
465 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
466 return NULL;
469 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
470 if ((*index == I_IMAGECALLBACK) ||
471 (*index == I_IMAGENONE)) return NULL;
472 ERR("TBN_GETDISPINFO returned invalid index %d\n",
473 *index);
474 return NULL;
477 switch(imagelist)
479 case IMAGE_LIST_DEFAULT:
480 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
481 break;
482 case IMAGE_LIST_HOT:
483 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
484 break;
485 case IMAGE_LIST_DISABLED:
486 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
487 break;
488 default:
489 himl = NULL;
490 FIXME("Shouldn't reach here\n");
493 if (!himl)
494 TRACE("no image list\n");
496 return himl;
500 static void
501 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
503 RECT myrect;
504 COLORREF oldcolor, newcolor;
506 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
507 myrect.right = myrect.left + 1;
508 myrect.top = lpRect->top + 2;
509 myrect.bottom = lpRect->bottom - 2;
511 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
512 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
513 oldcolor = SetBkColor (hdc, newcolor);
514 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
516 myrect.left = myrect.right;
517 myrect.right = myrect.left + 1;
519 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
520 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
521 SetBkColor (hdc, newcolor);
522 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
524 SetBkColor (hdc, oldcolor);
528 /***********************************************************************
529 * TOOLBAR_DrawFlatHorizontalSeparator
531 * This function draws horizontal separator for toolbars having CCS_VERT style.
532 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
533 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
534 * are horizontal as opposed to the vertical separators for not dropdown
535 * type.
537 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
539 static void
540 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
541 const TOOLBAR_INFO *infoPtr)
543 RECT myrect;
544 COLORREF oldcolor, newcolor;
546 myrect.left = lpRect->left;
547 myrect.right = lpRect->right;
548 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
549 myrect.bottom = myrect.top + 1;
551 InflateRect (&myrect, -2, 0);
553 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
555 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
556 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
557 oldcolor = SetBkColor (hdc, newcolor);
558 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
560 myrect.top = myrect.bottom;
561 myrect.bottom = myrect.top + 1;
563 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
564 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
565 SetBkColor (hdc, newcolor);
566 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
568 SetBkColor (hdc, oldcolor);
572 static void
573 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
575 INT x, y;
576 HPEN hPen, hOldPen;
578 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
579 hOldPen = SelectObject ( hdc, hPen );
580 x = left + 2;
581 y = top;
582 MoveToEx (hdc, x, y, NULL);
583 LineTo (hdc, x+5, y++); x++;
584 MoveToEx (hdc, x, y, NULL);
585 LineTo (hdc, x+3, y++); x++;
586 MoveToEx (hdc, x, y, NULL);
587 LineTo (hdc, x+1, y);
588 SelectObject( hdc, hOldPen );
589 DeleteObject( hPen );
593 * Draw the text string for this button.
594 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
595 * is non-zero, so we can simply check himlDef to see if we have
596 * an image list
598 static void
599 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
600 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
602 HDC hdc = tbcd->nmcd.hdc;
603 HFONT hOldFont = 0;
604 COLORREF clrOld = 0;
605 COLORREF clrOldBk = 0;
606 int oldBkMode = 0;
607 UINT state = tbcd->nmcd.uItemState;
609 /* draw text */
610 if (lpText && infoPtr->nMaxTextRows > 0) {
611 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
612 wine_dbgstr_rect(rcText));
614 hOldFont = SelectObject (hdc, infoPtr->hFont);
615 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
616 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
618 else if (state & CDIS_DISABLED) {
619 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
620 OffsetRect (rcText, 1, 1);
621 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
622 SetTextColor (hdc, comctl32_color.clr3dShadow);
623 OffsetRect (rcText, -1, -1);
625 else if (state & CDIS_INDETERMINATE) {
626 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
628 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
629 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
630 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
631 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
633 else {
634 clrOld = SetTextColor (hdc, tbcd->clrText);
637 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
638 SetTextColor (hdc, clrOld);
639 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
641 SetBkColor (hdc, clrOldBk);
642 SetBkMode (hdc, oldBkMode);
644 SelectObject (hdc, hOldFont);
649 static void
650 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
652 HDC hdc = tbcd->nmcd.hdc;
653 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
654 COLORREF clrTextOld;
655 COLORREF clrBkOld;
656 INT cx = lpRect->right - lpRect->left;
657 INT cy = lpRect->bottom - lpRect->top;
658 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
659 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
660 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
661 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
662 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
663 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
664 SetBkColor(hdc, clrBkOld);
665 SetTextColor(hdc, clrTextOld);
666 SelectObject (hdc, hbr);
670 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
672 IMAGELISTDRAWPARAMS draw_params = { 0 };
673 INT cx, cy;
674 HBITMAP hbmMask, hbmImage;
675 HDC hdcMask, hdcImage;
677 ImageList_GetIconSize(himl, &cx, &cy);
679 draw_params.cbSize = sizeof(draw_params);
680 draw_params.himl = himl;
681 draw_params.i = index;
682 draw_params.hdcDst = hdc;
683 draw_params.x = x;
684 draw_params.y = y;
685 draw_params.cx = cx;
686 draw_params.cy = cy;
687 draw_params.rgbBk = CLR_NONE;
688 draw_params.rgbFg = CLR_NONE;
689 draw_params.fStyle = draw_flags;
690 draw_params.fState = ILS_NORMAL;
692 /* 32bpp image with alpha channel is converted to grayscale */
693 if (imagelist_has_alpha(himl, index))
695 draw_params.fState = ILS_SATURATE;
696 ImageList_DrawIndirect(&draw_params);
697 return;
700 /* Create src image */
701 hdcImage = CreateCompatibleDC(hdc);
702 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
703 SelectObject(hdcImage, hbmImage);
705 draw_params.x = 0;
706 draw_params.y = 0;
707 draw_params.rgbBk = RGB(0xff, 0xff, 0xff);
708 draw_params.rgbFg = RGB(0,0,0);
709 draw_params.hdcDst = hdcImage;
710 ImageList_DrawIndirect(&draw_params);
712 /* Create Mask */
713 hdcMask = CreateCompatibleDC(0);
714 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
715 SelectObject(hdcMask, hbmMask);
717 /* Remove the background and all white pixels */
718 draw_params.fStyle = ILD_MASK;
719 draw_params.hdcDst = hdcMask;
720 ImageList_DrawIndirect(&draw_params);
721 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
722 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
724 /* draw the new mask 'etched' to hdc */
725 SetBkColor(hdc, RGB(255, 255, 255));
726 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
727 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
728 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
729 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
730 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
732 /* Cleanup */
733 DeleteObject(hbmImage);
734 DeleteDC(hdcImage);
735 DeleteObject (hbmMask);
736 DeleteDC(hdcMask);
740 static UINT
741 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr, BOOL captured)
743 UINT retstate = 0;
745 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
746 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
747 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
748 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
749 retstate |= (btnPtr->bHot & !captured ) ? CDIS_HOT : 0;
750 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
751 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
752 return retstate;
755 /* draws the image on a toolbar button */
756 static void
757 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
758 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
760 HIMAGELIST himl = NULL;
761 BOOL draw_masked = FALSE;
762 INT index;
763 INT offset = 0;
764 UINT draw_flags = ILD_TRANSPARENT;
766 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
768 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
769 if (!himl)
771 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
772 draw_masked = TRUE;
775 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
776 ((tbcd->nmcd.uItemState & CDIS_HOT)
777 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme)))
779 /* if hot, attempt to draw with hot image list, if fails,
780 use default image list */
781 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
782 if (!himl)
783 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
785 else
786 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
788 if (!himl)
789 return;
791 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
792 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
793 offset = 1;
795 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
796 (tbcd->nmcd.uItemState & CDIS_MARKED))
797 draw_flags |= ILD_BLEND50;
799 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
800 index, himl, left, top, offset);
802 if (draw_masked)
803 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
804 else
805 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
808 /* draws a blank frame for a toolbar button */
809 static void
810 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
812 HDC hdc = tbcd->nmcd.hdc;
813 RECT rc = *rect;
814 /* if the state is disabled or indeterminate then the button
815 * cannot have an interactive look like pressed or hot */
816 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
817 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
818 BOOL pressed_look = !non_interactive_state &&
819 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
820 (tbcd->nmcd.uItemState & CDIS_CHECKED));
822 /* app don't want us to draw any edges */
823 if (dwItemCDFlag & TBCDRF_NOEDGES)
824 return;
826 if (infoPtr->dwStyle & TBSTYLE_FLAT)
828 if (pressed_look)
829 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
830 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
831 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
833 else
835 if (pressed_look)
836 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
837 else
838 DrawEdge (hdc, &rc, EDGE_RAISED,
839 BF_SOFT | BF_RECT | BF_MIDDLE);
843 static void
844 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
846 HDC hdc = tbcd->nmcd.hdc;
847 int offset = 0;
848 BOOL pressed = bDropDownPressed ||
849 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
851 if (infoPtr->dwStyle & TBSTYLE_FLAT)
853 if (pressed)
854 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
855 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
856 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
857 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
858 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
860 else
862 if (pressed)
863 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
864 else
865 DrawEdge (hdc, rcArrow, EDGE_RAISED,
866 BF_SOFT | BF_RECT | BF_MIDDLE);
869 if (pressed)
870 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
872 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
874 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
875 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
877 else
878 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
881 /* draws a complete toolbar button */
882 static void
883 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
885 DWORD dwStyle = infoPtr->dwStyle;
886 BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
887 BOOL drawSepDropDownArrow = hasDropDownArrow &&
888 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
889 RECT rc, rcArrow, rcBitmap, rcText;
890 LPWSTR lpText = NULL;
891 NMTBCUSTOMDRAW tbcd;
892 DWORD ntfret;
893 INT offset;
894 INT oldBkMode;
895 DWORD dwItemCustDraw;
896 DWORD dwItemCDFlag;
897 HTHEME theme = infoPtr->hTheme;
899 rc = btnPtr->rect;
900 rcArrow = rc;
902 /* separator - doesn't send NM_CUSTOMDRAW */
903 if (btnPtr->fsStyle & BTNS_SEP) {
904 if (theme)
906 DrawThemeBackground (theme, hdc,
907 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
908 &rc, NULL);
910 else
911 /* with the FLAT style, iBitmap is the width and has already */
912 /* been taken into consideration in calculating the width */
913 /* so now we need to draw the vertical separator */
914 /* empirical tests show that iBitmap can/will be non-zero */
915 /* when drawing the vertical bar... */
916 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
917 if (dwStyle & CCS_VERT) {
918 RECT rcsep = rc;
919 InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
920 TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
922 else
923 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
925 else if (btnPtr->fsStyle != BTNS_SEP) {
926 FIXME("Draw some kind of separator: fsStyle=%x\n",
927 btnPtr->fsStyle);
929 return;
932 /* get a pointer to the text */
933 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
935 if (hasDropDownArrow)
937 int right;
939 if (dwStyle & TBSTYLE_FLAT)
940 right = max(rc.left, rc.right - DDARROW_WIDTH);
941 else
942 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
944 if (drawSepDropDownArrow)
945 rc.right = right;
947 rcArrow.left = right;
950 /* copy text & bitmap rects after adjusting for drop-down arrow
951 * so that text & bitmap is centered in the rectangle not containing
952 * the arrow */
953 rcText = rc;
954 rcBitmap = rc;
956 /* Center the bitmap horizontally and vertically */
957 if (dwStyle & TBSTYLE_LIST)
959 if (lpText &&
960 infoPtr->nMaxTextRows > 0 &&
961 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
962 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
963 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
964 else
965 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
967 else
968 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
970 rcBitmap.top += infoPtr->szPadding.cy / 2;
972 TRACE("iBitmap=%d, start=(%ld,%ld) w=%d, h=%d\n", btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
973 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
974 TRACE("Text=%s\n", debugstr_w(lpText));
975 TRACE("iListGap=%d, padding = { %ld, %ld }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
977 /* calculate text position */
978 if (lpText)
980 InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
981 if (dwStyle & TBSTYLE_LIST)
983 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
985 else
987 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
988 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
989 else
990 rcText.top += infoPtr->szPadding.cy/2 + 2;
994 /* Initialize fields in all cases, because we use these later
995 * NOTE: applications can and do alter these to customize their
996 * toolbars */
997 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
998 tbcd.clrText = comctl32_color.clrBtnText;
999 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
1000 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
1001 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
1002 tbcd.clrMark = comctl32_color.clrHighlight;
1003 tbcd.clrHighlightHotTrack = 0;
1004 tbcd.nStringBkMode = TRANSPARENT;
1005 tbcd.nHLStringBkMode = OPAQUE;
1006 tbcd.rcText.left = 0;
1007 tbcd.rcText.top = 0;
1008 tbcd.rcText.right = rcText.right - rc.left;
1009 tbcd.rcText.bottom = rcText.bottom - rc.top;
1010 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr, infoPtr->bCaptured);
1011 tbcd.nmcd.hdc = hdc;
1012 tbcd.nmcd.rc = btnPtr->rect;
1013 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
1015 /* FIXME: what are these used for? */
1016 tbcd.hbrLines = 0;
1017 tbcd.hpenLines = 0;
1019 /* Issue Item Prepaint notify */
1020 dwItemCustDraw = 0;
1021 dwItemCDFlag = 0;
1022 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1024 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1025 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1026 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1027 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1028 /* reset these fields so the user can't alter the behaviour like native */
1029 tbcd.nmcd.hdc = hdc;
1030 tbcd.nmcd.rc = btnPtr->rect;
1032 dwItemCustDraw = ntfret & 0xffff;
1033 dwItemCDFlag = ntfret & 0xffff0000;
1034 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1035 return;
1036 /* save the only part of the rect that the user can change */
1037 rcText.right = tbcd.rcText.right + rc.left;
1038 rcText.bottom = tbcd.rcText.bottom + rc.top;
1041 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1042 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1043 OffsetRect(&rcText, 1, 1);
1045 if (!theme && !(tbcd.nmcd.uItemState & CDIS_HOT) &&
1046 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1047 TOOLBAR_DrawPattern (&rc, &tbcd);
1049 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || theme) && (tbcd.nmcd.uItemState & CDIS_HOT))
1051 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1053 COLORREF oldclr;
1055 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1056 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1057 if (hasDropDownArrow)
1058 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1059 SetBkColor(hdc, oldclr);
1063 if (theme)
1065 if (!(dwItemCDFlag & TBCDRF_NOBACKGROUND))
1067 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1068 int stateId = TS_NORMAL;
1070 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1071 stateId = TS_DISABLED;
1072 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1073 stateId = TS_PRESSED;
1074 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1075 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_CHECKED;
1076 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1077 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1078 stateId = TS_HOT;
1080 DrawThemeBackground(theme, hdc, partId, stateId, &rc, NULL);
1083 else
1084 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1086 if (drawSepDropDownArrow)
1088 if (theme)
1090 int stateId = TS_NORMAL;
1092 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1093 stateId = TS_DISABLED;
1094 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1095 stateId = TS_PRESSED;
1096 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1097 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_CHECKED;
1098 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1099 stateId = TS_HOT;
1101 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1102 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1104 else
1105 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1108 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1109 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1110 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1111 SetBkMode (hdc, oldBkMode);
1113 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1115 if (hasDropDownArrow && !drawSepDropDownArrow)
1117 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1119 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1120 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1122 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1124 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1125 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1127 else
1128 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1131 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1133 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1134 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1140 static void
1141 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1143 TBUTTON_INFO *btnPtr;
1144 INT i;
1145 RECT rcTemp, rcClient;
1146 NMTBCUSTOMDRAW tbcd;
1147 DWORD ntfret;
1148 DWORD dwBaseCustDraw;
1150 /* the app has told us not to redraw the toolbar */
1151 if (!infoPtr->bDoRedraw)
1152 return;
1154 /* if imagelist belongs to the app, it can be changed
1155 by the app after setting it */
1156 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1158 infoPtr->nNumBitmaps = 0;
1159 for (i = 0; i < infoPtr->cimlDef; i++)
1160 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1163 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1165 /* change the imagelist icon size if we manage the list and it is necessary */
1166 TOOLBAR_CheckImageListIconSize(infoPtr);
1168 /* Send initial notify */
1169 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1170 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1171 tbcd.nmcd.hdc = hdc;
1172 tbcd.nmcd.rc = ps->rcPaint;
1173 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1174 dwBaseCustDraw = ntfret & 0xffff;
1176 GetClientRect(infoPtr->hwndSelf, &rcClient);
1178 /* redraw necessary buttons */
1179 btnPtr = infoPtr->buttons;
1180 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1182 BOOL bDraw;
1183 if (!RectVisible(hdc, &btnPtr->rect))
1184 continue;
1185 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1187 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1188 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1190 else
1191 bDraw = TRUE;
1192 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1193 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1194 if (bDraw)
1195 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1198 /* draw insert mark if required */
1199 if (infoPtr->tbim.iButton != -1)
1201 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1202 RECT rcInsertMark;
1203 rcInsertMark.top = rcButton.top;
1204 rcInsertMark.bottom = rcButton.bottom;
1205 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1206 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1207 else
1208 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1209 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1212 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1214 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1215 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1216 tbcd.nmcd.hdc = hdc;
1217 tbcd.nmcd.rc = ps->rcPaint;
1218 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1222 /***********************************************************************
1223 * TOOLBAR_MeasureString
1225 * This function gets the width and height of a string in pixels. This
1226 * is done first by using GetTextExtentPoint to get the basic width
1227 * and height. The DrawText is called with DT_CALCRECT to get the exact
1228 * width. The reason is because the text may have more than one "&" (or
1229 * prefix characters as M$ likes to call them). The prefix character
1230 * indicates where the underline goes, except for the string "&&" which
1231 * is reduced to a single "&". GetTextExtentPoint does not process these
1232 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1234 static void
1235 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1236 HDC hdc, LPSIZE lpSize)
1238 RECT myrect;
1240 lpSize->cx = 0;
1241 lpSize->cy = 0;
1243 if (infoPtr->nMaxTextRows > 0 &&
1244 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1245 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1246 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1248 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1250 if(lpText != NULL) {
1251 /* first get size of all the text */
1252 GetTextExtentPoint32W (hdc, lpText, lstrlenW (lpText), lpSize);
1254 /* feed above size into the rectangle for DrawText */
1255 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1257 /* Use DrawText to get true size as drawn (less pesky "&") */
1258 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1259 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1260 DT_NOPREFIX : 0));
1262 /* feed back to caller */
1263 lpSize->cx = myrect.right;
1264 lpSize->cy = myrect.bottom;
1268 TRACE("string size %ld x %ld.\n", lpSize->cx, lpSize->cy);
1271 /***********************************************************************
1272 * TOOLBAR_CalcStrings
1274 * This function walks through each string and measures it and returns
1275 * the largest height and width to caller.
1277 static void
1278 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1280 TBUTTON_INFO *btnPtr;
1281 INT i;
1282 SIZE sz;
1283 HDC hdc;
1284 HFONT hOldFont;
1286 lpSize->cx = 0;
1287 lpSize->cy = 0;
1289 if (infoPtr->nMaxTextRows == 0)
1290 return;
1292 hdc = GetDC (infoPtr->hwndSelf);
1293 hOldFont = SelectObject (hdc, infoPtr->hFont);
1295 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1297 TEXTMETRICW tm;
1299 GetTextMetricsW(hdc, &tm);
1300 lpSize->cy = tm.tmHeight;
1303 btnPtr = infoPtr->buttons;
1304 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1305 if(TOOLBAR_GetText(infoPtr, btnPtr))
1307 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1308 if (sz.cx > lpSize->cx)
1309 lpSize->cx = sz.cx;
1310 if (sz.cy > lpSize->cy)
1311 lpSize->cy = sz.cy;
1315 SelectObject (hdc, hOldFont);
1316 ReleaseDC (infoPtr->hwndSelf, hdc);
1318 TRACE("max string size %ld x %ld\n", lpSize->cx, lpSize->cy);
1321 /***********************************************************************
1322 * TOOLBAR_WrapToolbar
1324 * This function walks through the buttons and separators in the
1325 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1326 * wrapping should occur based on the width of the toolbar window.
1327 * It does *not* calculate button placement itself. That task
1328 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1329 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1330 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1332 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1333 * vertical toolbar lists.
1336 static void
1337 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1339 TBUTTON_INFO *btnPtr;
1340 INT x, cx, i, j, width;
1341 BOOL bButtonWrap;
1343 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1344 /* no layout is necessary. Applications may use this style */
1345 /* to perform their own layout on the toolbar. */
1346 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1347 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1349 btnPtr = infoPtr->buttons;
1350 x = infoPtr->nIndent;
1351 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1353 bButtonWrap = FALSE;
1355 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1356 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1357 infoPtr->nIndent);
1359 for (i = 0; i < infoPtr->nNumButtons; i++ )
1361 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1363 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1364 continue;
1366 if (btnPtr[i].cx > 0)
1367 cx = btnPtr[i].cx;
1368 /* horizontal separators are treated as buttons for width */
1369 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1370 !(infoPtr->dwStyle & CCS_VERT))
1371 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1372 else
1373 cx = infoPtr->nButtonWidth;
1375 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1376 cx += DDARROW_WIDTH;
1378 /* Two or more adjacent separators form a separator group. */
1379 /* The first separator in a group should be wrapped to the */
1380 /* next row if the previous wrapping is on a button. */
1381 if( bButtonWrap &&
1382 (btnPtr[i].fsStyle & BTNS_SEP) &&
1383 (i + 1 < infoPtr->nNumButtons ) &&
1384 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1386 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1387 btnPtr[i].fsState |= TBSTATE_WRAP;
1388 x = infoPtr->nIndent;
1389 i++;
1390 bButtonWrap = FALSE;
1391 continue;
1394 /* The layout makes sure the bitmap is visible, but not the button. */
1395 /* Test added to also wrap after a button that starts a row but */
1396 /* is bigger than the area. - GA 8/01 */
1397 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1398 ((x == infoPtr->nIndent) && (cx > width)))
1400 BOOL bFound = FALSE;
1402 /* If the current button is a separator and not hidden, */
1403 /* go to the next until it reaches a non separator. */
1404 /* Wrap the last separator if it is before a button. */
1405 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1406 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1407 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1408 i < infoPtr->nNumButtons )
1410 i++;
1411 bFound = TRUE;
1414 if( bFound && i < infoPtr->nNumButtons )
1416 i--;
1417 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1418 i, btnPtr[i].fsStyle, x, cx);
1419 btnPtr[i].fsState |= TBSTATE_WRAP;
1420 x = infoPtr->nIndent;
1421 bButtonWrap = FALSE;
1422 continue;
1424 else if ( i >= infoPtr->nNumButtons)
1425 break;
1427 /* If the current button is not a separator, find the last */
1428 /* separator and wrap it. */
1429 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1431 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1432 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1434 bFound = TRUE;
1435 i = j;
1436 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1437 i, btnPtr[i].fsStyle, x, cx);
1438 x = infoPtr->nIndent;
1439 btnPtr[j].fsState |= TBSTATE_WRAP;
1440 bButtonWrap = FALSE;
1441 break;
1445 /* If no separator available for wrapping, wrap one of */
1446 /* non-hidden previous button. */
1447 if (!bFound)
1449 for ( j = i - 1;
1450 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1452 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1453 continue;
1455 bFound = TRUE;
1456 i = j;
1457 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1458 i, btnPtr[i].fsStyle, x, cx);
1459 x = infoPtr->nIndent;
1460 btnPtr[j].fsState |= TBSTATE_WRAP;
1461 bButtonWrap = TRUE;
1462 break;
1466 /* If all above failed, wrap the current button. */
1467 if (!bFound)
1469 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1470 i, btnPtr[i].fsStyle, x, cx);
1471 btnPtr[i].fsState |= TBSTATE_WRAP;
1472 x = infoPtr->nIndent;
1473 if (btnPtr[i].fsStyle & BTNS_SEP )
1474 bButtonWrap = FALSE;
1475 else
1476 bButtonWrap = TRUE;
1479 else {
1480 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1481 i, btnPtr[i].fsStyle, x, cx);
1482 x += cx;
1488 /***********************************************************************
1489 * TOOLBAR_MeasureButton
1491 * Calculates the width and height required for a button. Used in
1492 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1493 * the width of buttons that are autosized.
1495 * Note that it would have been rather elegant to use one piece of code for
1496 * both the laying out of the toolbar and for controlling where button parts
1497 * are drawn, but the native control has inconsistencies between the two that
1498 * prevent this from being effectively. These inconsistencies can be seen as
1499 * artefacts where parts of the button appear outside of the bounding button
1500 * rectangle.
1502 * There are several cases for the calculation of the button dimensions and
1503 * button part positioning:
1505 * List
1506 * ====
1508 * With Bitmap:
1510 * +--------------------------------------------------------+ ^
1511 * | ^ ^ | |
1512 * | | pad.cy / 2 | centered | |
1513 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1514 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1515 * | |<------------>| | | | |
1516 * | +--------------+ +------------+ | |
1517 * |<-------------------------------------->| | |
1518 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1519 * | szText.cx | |
1520 * +--------------------------------------------------------+ -
1521 * <-------------------------------------------------------->
1522 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1524 * Without Bitmap (I_IMAGENONE):
1526 * +-----------------------------------+ ^
1527 * | ^ | |
1528 * | | centered | | LISTPAD_CY +
1529 * | +------------+ | | szText.cy
1530 * | | Text | | |
1531 * | | | | |
1532 * | +------------+ | |
1533 * |<----------------->| | |
1534 * | cxedge |<-----------> | |
1535 * | szText.cx | |
1536 * +-----------------------------------+ -
1537 * <----------------------------------->
1538 * szText.cx + pad.cx
1540 * Without text:
1542 * +--------------------------------------+ ^
1543 * | ^ | |
1544 * | | padding.cy/2 | | DEFPAD_CY +
1545 * | +------------+ | | nBitmapHeight
1546 * | | Bitmap | | |
1547 * | | | | |
1548 * | +------------+ | |
1549 * |<------------------->| | |
1550 * | cxedge + iListGap/2 |<-----------> | |
1551 * | nBitmapWidth | |
1552 * +--------------------------------------+ -
1553 * <-------------------------------------->
1554 * 2*cxedge + nBitmapWidth + iListGap
1556 * Non-List
1557 * ========
1559 * With bitmap:
1561 * +-----------------------------------+ ^
1562 * | ^ | |
1563 * | | pad.cy / 2 | | nBitmapHeight +
1564 * | - | | szText.cy +
1565 * | +------------+ | | DEFPAD_CY + 1
1566 * | centered | Bitmap | | |
1567 * |<----------------->| | | |
1568 * | +------------+ | |
1569 * | ^ | |
1570 * | 1 | | |
1571 * | - | |
1572 * | centered +---------------+ | |
1573 * |<--------------->| Text | | |
1574 * | +---------------+ | |
1575 * +-----------------------------------+ -
1576 * <----------------------------------->
1577 * pad.cx + max(nBitmapWidth, szText.cx)
1579 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1581 * +---------------------------------------+ ^
1582 * | ^ | |
1583 * | | 2 + pad.cy / 2 | |
1584 * | - | | szText.cy +
1585 * | centered +-----------------+ | | pad.cy + 2
1586 * |<--------------->| Text | | |
1587 * | +-----------------+ | |
1588 * | | |
1589 * +---------------------------------------+ -
1590 * <--------------------------------------->
1591 * 2*cxedge + pad.cx + szText.cx
1593 * Without text:
1594 * As for with bitmaps, but with szText.cx zero.
1596 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1597 BOOL bHasBitmap, BOOL bValidImageList)
1599 SIZE sizeButton;
1600 if (infoPtr->dwStyle & TBSTYLE_LIST)
1602 /* set button height from bitmap / text height... */
1603 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1604 sizeString.cy);
1606 /* ... add on the necessary padding */
1607 if (bValidImageList)
1609 if (bHasBitmap)
1610 sizeButton.cy += DEFPAD_CY;
1611 else
1612 sizeButton.cy += LISTPAD_CY;
1614 else
1615 sizeButton.cy += infoPtr->szPadding.cy;
1617 /* calculate button width */
1618 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1619 infoPtr->nBitmapWidth + infoPtr->iListGap;
1620 if (sizeString.cx > 0)
1621 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1624 else
1626 if (bHasBitmap)
1628 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1629 if (sizeString.cy > 0)
1630 sizeButton.cy += 1 + sizeString.cy;
1631 sizeButton.cx = infoPtr->szPadding.cx +
1632 max(sizeString.cx, infoPtr->nBitmapWidth);
1634 else
1636 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1637 NONLIST_NOTEXT_OFFSET;
1638 sizeButton.cx = infoPtr->szPadding.cx +
1639 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1642 return sizeButton;
1646 /***********************************************************************
1647 * TOOLBAR_CalcToolbar
1649 * This function calculates button and separator placement. It first
1650 * calculates the button sizes, gets the toolbar window width and then
1651 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1652 * on. It assigns a new location to each item and sends this location to
1653 * the tooltip window if appropriate. Finally, it updates the rcBound
1654 * rect and calculates the new required toolbar window height.
1656 static void
1657 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1659 SIZE sizeString, sizeButton;
1660 BOOL validImageList = FALSE;
1662 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1664 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1666 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1667 validImageList = TRUE;
1668 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1669 infoPtr->nButtonWidth = sizeButton.cx;
1670 infoPtr->nButtonHeight = sizeButton.cy;
1671 infoPtr->iTopMargin = default_top_margin(infoPtr);
1673 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1674 infoPtr->nButtonWidth = infoPtr->cxMin;
1675 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1676 infoPtr->nButtonWidth = infoPtr->cxMax;
1678 TOOLBAR_LayoutToolbar(infoPtr);
1681 static void
1682 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1684 TBUTTON_INFO *btnPtr;
1685 SIZE sizeButton;
1686 INT i, nRows, nSepRows;
1687 INT x, y, cx, cy;
1688 BOOL bWrap;
1689 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1691 TOOLBAR_WrapToolbar(infoPtr);
1693 x = infoPtr->nIndent;
1694 y = infoPtr->iTopMargin;
1695 cx = infoPtr->nButtonWidth;
1696 cy = infoPtr->nButtonHeight;
1698 nRows = nSepRows = 0;
1700 infoPtr->rcBound.top = y;
1701 infoPtr->rcBound.left = x;
1702 infoPtr->rcBound.bottom = y + cy;
1703 infoPtr->rcBound.right = x;
1705 btnPtr = infoPtr->buttons;
1707 TRACE("cy=%d\n", cy);
1709 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1711 bWrap = FALSE;
1712 if (btnPtr->fsState & TBSTATE_HIDDEN)
1714 SetRectEmpty (&btnPtr->rect);
1715 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1716 continue;
1719 cy = infoPtr->nButtonHeight;
1721 if (btnPtr->fsStyle & BTNS_SEP) {
1722 if (infoPtr->dwStyle & CCS_VERT) {
1723 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1724 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1726 else
1727 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1728 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1730 else
1732 if (btnPtr->cx)
1733 cx = btnPtr->cx;
1734 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1736 SIZE sz;
1737 HDC hdc;
1738 HFONT hOldFont;
1740 hdc = GetDC (infoPtr->hwndSelf);
1741 hOldFont = SelectObject (hdc, infoPtr->hFont);
1743 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1745 SelectObject (hdc, hOldFont);
1746 ReleaseDC (infoPtr->hwndSelf, hdc);
1748 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1749 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1750 validImageList);
1751 cx = sizeButton.cx;
1753 else
1754 cx = infoPtr->nButtonWidth;
1756 /* if size has been set manually then don't add on extra space
1757 * for the drop down arrow */
1758 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1759 cx += DDARROW_WIDTH;
1761 if (btnPtr->fsState & TBSTATE_WRAP)
1762 bWrap = TRUE;
1764 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1766 if (infoPtr->rcBound.left > x)
1767 infoPtr->rcBound.left = x;
1768 if (infoPtr->rcBound.right < x + cx)
1769 infoPtr->rcBound.right = x + cx;
1770 if (infoPtr->rcBound.bottom < y + cy)
1771 infoPtr->rcBound.bottom = y + cy;
1773 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1775 /* btnPtr->nRow is zero based. The space between the rows is */
1776 /* also considered as a row. */
1777 btnPtr->nRow = nRows + nSepRows;
1779 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1780 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1781 x, y, x+cx, y+cy);
1783 if( bWrap )
1785 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1786 y += cy;
1787 else
1789 if ( !(infoPtr->dwStyle & CCS_VERT))
1790 y += cy + ( (btnPtr->cx > 0 ) ?
1791 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1792 else
1793 y += cy;
1795 /* nSepRows is used to calculate the extra height following */
1796 /* the last row. */
1797 nSepRows++;
1799 x = infoPtr->nIndent;
1801 /* Increment row number unless this is the last button */
1802 /* and it has Wrap set. */
1803 if (i != infoPtr->nNumButtons-1)
1804 nRows++;
1806 else
1807 x += cx;
1810 /* infoPtr->nRows is the number of rows on the toolbar */
1811 infoPtr->nRows = nRows + nSepRows + 1;
1813 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1817 static INT
1818 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1820 TBUTTON_INFO *btnPtr;
1821 INT i;
1823 if (button)
1824 *button = FALSE;
1826 btnPtr = infoPtr->buttons;
1827 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1828 if (btnPtr->fsState & TBSTATE_HIDDEN)
1829 continue;
1831 if (btnPtr->fsStyle & BTNS_SEP) {
1832 if (PtInRect (&btnPtr->rect, *lpPt)) {
1833 TRACE(" ON SEPARATOR %d\n", i);
1834 return -i;
1837 else {
1838 if (PtInRect (&btnPtr->rect, *lpPt)) {
1839 TRACE(" ON BUTTON %d\n", i);
1840 if (button)
1841 *button = TRUE;
1842 return i;
1847 TRACE(" NOWHERE\n");
1848 return TOOLBAR_NOWHERE;
1852 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1853 static BOOL
1854 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1856 INT nOldButtons, nNewButtons, iButton;
1857 BOOL fHasString = FALSE;
1859 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1860 iIndex = infoPtr->nNumButtons;
1862 nOldButtons = infoPtr->nNumButtons;
1863 nNewButtons = nOldButtons + nAddButtons;
1865 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1866 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1867 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1868 infoPtr->nNumButtons += nAddButtons;
1870 /* insert new buttons data */
1871 for (iButton = 0; iButton < nAddButtons; iButton++) {
1872 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1873 INT_PTR str;
1875 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1877 ZeroMemory(btnPtr, sizeof(*btnPtr));
1879 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1880 btnPtr->idCommand = lpTbb[iButton].idCommand;
1881 btnPtr->fsState = lpTbb[iButton].fsState;
1882 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1883 btnPtr->dwData = lpTbb[iButton].dwData;
1885 if (btnPtr->fsStyle & BTNS_SEP)
1886 str = -1;
1887 else
1888 str = lpTbb[iButton].iString;
1889 set_string_index( btnPtr, str, fUnicode );
1890 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1892 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1895 if (infoPtr->nNumStrings > 0 || fHasString)
1896 TOOLBAR_CalcToolbar(infoPtr);
1897 else
1898 TOOLBAR_LayoutToolbar(infoPtr);
1899 TOOLBAR_AutoSize(infoPtr);
1901 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1902 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1903 return TRUE;
1907 static INT
1908 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1910 TBUTTON_INFO *btnPtr;
1911 INT i;
1913 if (CommandIsIndex) {
1914 TRACE("command is really index command=%d\n", idCommand);
1915 if (idCommand >= infoPtr->nNumButtons) return -1;
1916 return idCommand;
1918 btnPtr = infoPtr->buttons;
1919 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1920 if (btnPtr->idCommand == idCommand) {
1921 TRACE("command=%d index=%d\n", idCommand, i);
1922 return i;
1925 TRACE("no index found for command=%d\n", idCommand);
1926 return -1;
1930 static INT
1931 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1933 TBUTTON_INFO *btnPtr;
1934 INT nRunIndex;
1936 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1937 return -1;
1939 /* check index button */
1940 btnPtr = &infoPtr->buttons[nIndex];
1941 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1942 if (btnPtr->fsState & TBSTATE_CHECKED)
1943 return nIndex;
1946 /* check previous buttons */
1947 nRunIndex = nIndex - 1;
1948 while (nRunIndex >= 0) {
1949 btnPtr = &infoPtr->buttons[nRunIndex];
1950 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1951 if (btnPtr->fsState & TBSTATE_CHECKED)
1952 return nRunIndex;
1954 else
1955 break;
1956 nRunIndex--;
1959 /* check next buttons */
1960 nRunIndex = nIndex + 1;
1961 while (nRunIndex < infoPtr->nNumButtons) {
1962 btnPtr = &infoPtr->buttons[nRunIndex];
1963 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1964 if (btnPtr->fsState & TBSTATE_CHECKED)
1965 return nRunIndex;
1967 else
1968 break;
1969 nRunIndex++;
1972 return -1;
1976 static VOID
1977 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1978 WPARAM wParam, LPARAM lParam)
1980 MSG msg;
1982 msg.hwnd = hwndMsg;
1983 msg.message = uMsg;
1984 msg.wParam = wParam;
1985 msg.lParam = lParam;
1986 msg.time = GetMessageTime ();
1987 msg.pt.x = (short)LOWORD(GetMessagePos ());
1988 msg.pt.y = (short)HIWORD(GetMessagePos ());
1990 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1993 static void
1994 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1996 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1997 TTTOOLINFOW ti;
1999 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
2000 ti.cbSize = sizeof (TTTOOLINFOW);
2001 ti.hwnd = infoPtr->hwndSelf;
2002 ti.uId = button->idCommand;
2003 ti.hinst = 0;
2004 ti.lpszText = LPSTR_TEXTCALLBACKW;
2005 /* ti.lParam = random value from the stack? */
2007 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
2008 0, (LPARAM)&ti);
2012 static void
2013 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2015 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
2016 TTTOOLINFOW ti;
2018 ZeroMemory(&ti, sizeof(ti));
2019 ti.cbSize = sizeof(ti);
2020 ti.hwnd = infoPtr->hwndSelf;
2021 ti.uId = button->idCommand;
2023 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2027 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2029 /* Set the toolTip only for non-hidden, non-separator button */
2030 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2032 TTTOOLINFOW ti;
2034 ZeroMemory(&ti, sizeof(ti));
2035 ti.cbSize = sizeof(ti);
2036 ti.hwnd = infoPtr->hwndSelf;
2037 ti.uId = button->idCommand;
2038 ti.rect = button->rect;
2039 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2043 /* Creates the tooltip control */
2044 static void
2045 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2047 int i;
2048 NMTOOLTIPSCREATED nmttc;
2050 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2051 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2052 infoPtr->hwndSelf, 0, 0, 0);
2054 if (!infoPtr->hwndToolTip)
2055 return;
2057 /* Send NM_TOOLTIPSCREATED notification */
2058 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2059 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2061 for (i = 0; i < infoPtr->nNumButtons; i++)
2063 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2064 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2068 /* keeps available button list box sorted by button id */
2069 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2071 int i;
2072 int count;
2073 PCUSTOMBUTTON btnInfo;
2074 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2076 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2078 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2080 /* position 0 is always separator */
2081 for (i = 1; i < count; i++)
2083 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2084 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2086 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2087 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2088 return;
2091 /* id higher than all others add to end */
2092 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2093 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2096 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2098 NMTOOLBARW nmtb;
2100 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2102 if (nIndexFrom == nIndexTo)
2103 return;
2105 /* MSDN states that iItem is the index of the button, rather than the
2106 * command ID as used by every other NMTOOLBAR notification */
2107 nmtb.iItem = nIndexFrom;
2108 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2110 PCUSTOMBUTTON btnInfo;
2111 NMHDR hdr;
2112 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2113 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2115 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2117 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2118 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2119 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2120 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2122 if (nIndexTo <= 0)
2123 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2124 else
2125 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2127 /* last item is always separator, so -2 instead of -1 */
2128 if (nIndexTo >= (count - 2))
2129 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2130 else
2131 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2133 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2134 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2136 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2140 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2142 NMTOOLBARW nmtb;
2144 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2146 /* MSDN states that iItem is the index of the button, rather than the
2147 * command ID as used by every other NMTOOLBAR notification */
2148 nmtb.iItem = nIndexAvail;
2149 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2151 PCUSTOMBUTTON btnInfo;
2152 NMHDR hdr;
2153 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2154 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2155 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2157 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2159 if (nIndexAvail != 0) /* index == 0 indicates separator */
2161 /* remove from 'available buttons' list */
2162 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2163 if (nIndexAvail == count-1)
2164 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2165 else
2166 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2168 else
2170 PCUSTOMBUTTON btnNew;
2172 /* duplicate 'separator' button */
2173 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2174 *btnNew = *btnInfo;
2175 btnInfo = btnNew;
2178 /* insert into 'toolbar button' list */
2179 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2180 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2182 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2184 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2188 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2190 PCUSTOMBUTTON btnInfo;
2191 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2193 TRACE("Remove: index %d\n", index);
2195 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2197 /* send TBN_QUERYDELETE notification */
2198 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2200 NMHDR hdr;
2202 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2203 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2205 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2207 /* insert into 'available button' list */
2208 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2209 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2210 else
2211 Free(btnInfo);
2213 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2217 /* drag list notification function for toolbar buttons list box */
2218 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2219 const DRAGLISTINFO *pDLI)
2221 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2222 switch (pDLI->uNotification)
2224 case DL_BEGINDRAG:
2226 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2227 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2228 /* no dragging for last item (separator) */
2229 if (nCurrentItem >= (nCount - 1)) return FALSE;
2230 return TRUE;
2232 case DL_DRAGGING:
2234 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2235 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2236 /* no dragging past last item (separator) */
2237 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2239 DrawInsert(hwnd, hwndList, nCurrentItem);
2240 /* FIXME: native uses "move button" cursor */
2241 return DL_COPYCURSOR;
2244 /* not over toolbar buttons list */
2245 if (nCurrentItem < 0)
2247 POINT ptWindow = pDLI->ptCursor;
2248 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2249 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2250 /* over available buttons list? */
2251 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2252 /* FIXME: native uses "move button" cursor */
2253 return DL_COPYCURSOR;
2255 /* clear drag arrow */
2256 DrawInsert(hwnd, hwndList, -1);
2257 return DL_STOPCURSOR;
2259 case DL_DROPPED:
2261 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2262 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2263 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2264 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2266 /* clear drag arrow */
2267 DrawInsert(hwnd, hwndList, -1);
2268 /* move item */
2269 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2271 /* not over toolbar buttons list */
2272 if (nIndexTo < 0)
2274 POINT ptWindow = pDLI->ptCursor;
2275 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2276 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2277 /* over available buttons list? */
2278 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2279 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2281 break;
2283 case DL_CANCELDRAG:
2284 /* Clear drag arrow */
2285 DrawInsert(hwnd, hwndList, -1);
2286 break;
2289 return 0;
2292 /* drag list notification function for available buttons list box */
2293 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2294 const DRAGLISTINFO *pDLI)
2296 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2297 switch (pDLI->uNotification)
2299 case DL_BEGINDRAG:
2300 return TRUE;
2301 case DL_DRAGGING:
2303 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2304 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2305 /* no dragging past last item (separator) */
2306 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2308 DrawInsert(hwnd, hwndList, nCurrentItem);
2309 /* FIXME: native uses "move button" cursor */
2310 return DL_COPYCURSOR;
2313 /* not over toolbar buttons list */
2314 if (nCurrentItem < 0)
2316 POINT ptWindow = pDLI->ptCursor;
2317 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2318 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2319 /* over available buttons list? */
2320 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2321 /* FIXME: native uses "move button" cursor */
2322 return DL_COPYCURSOR;
2324 /* clear drag arrow */
2325 DrawInsert(hwnd, hwndList, -1);
2326 return DL_STOPCURSOR;
2328 case DL_DROPPED:
2330 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2331 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2332 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2333 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2335 /* clear drag arrow */
2336 DrawInsert(hwnd, hwndList, -1);
2337 /* add item */
2338 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2341 case DL_CANCELDRAG:
2342 /* Clear drag arrow */
2343 DrawInsert(hwnd, hwndList, -1);
2344 break;
2346 return 0;
2349 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2351 /***********************************************************************
2352 * TOOLBAR_CustomizeDialogProc
2353 * This function implements the toolbar customization dialog.
2355 static INT_PTR CALLBACK
2356 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2358 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2359 PCUSTOMBUTTON btnInfo;
2360 NMTOOLBARA nmtb;
2361 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2363 switch (uMsg)
2365 case WM_INITDIALOG:
2366 custInfo = (PCUSTDLG_INFO)lParam;
2367 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2369 if (custInfo)
2371 WCHAR Buffer[256];
2372 int i = 0;
2373 int index;
2374 NMTBINITCUSTOMIZE nmtbic;
2376 infoPtr = custInfo->tbInfo;
2378 /* send TBN_QUERYINSERT notification */
2379 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2381 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2382 return FALSE;
2384 nmtbic.hwndDialog = hwnd;
2385 /* Send TBN_INITCUSTOMIZE notification */
2386 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2387 TBNRF_HIDEHELP)
2389 TRACE("TBNRF_HIDEHELP requested\n");
2390 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2393 /* add items to 'toolbar buttons' list and check if removable */
2394 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2396 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2397 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2398 btnInfo->btn.fsStyle = BTNS_SEP;
2399 btnInfo->bVirtual = FALSE;
2400 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2402 /* send TBN_QUERYDELETE notification */
2403 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2405 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2406 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2409 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2411 /* insert separator button into 'available buttons' list */
2412 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2413 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2414 btnInfo->btn.fsStyle = BTNS_SEP;
2415 btnInfo->bVirtual = FALSE;
2416 btnInfo->bRemovable = TRUE;
2417 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2418 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2419 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2421 /* insert all buttons into dsa */
2422 for (i = 0;; i++)
2424 /* send TBN_GETBUTTONINFO notification */
2425 NMTOOLBARW nmtb;
2426 nmtb.iItem = i;
2427 nmtb.pszText = Buffer;
2428 nmtb.cchText = 256;
2430 /* Clear previous button's text */
2431 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2433 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2434 break;
2436 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%Id) %s\n",
2437 nmtb.tbButton.fsStyle, i,
2438 nmtb.tbButton.idCommand,
2439 nmtb.tbButton.iString,
2440 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2441 : "");
2443 /* insert button into the appropriate list */
2444 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2445 if (index == -1)
2447 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2448 btnInfo->bVirtual = FALSE;
2449 btnInfo->bRemovable = TRUE;
2451 else
2453 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2454 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2457 btnInfo->btn = nmtb.tbButton;
2458 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2460 if (*nmtb.pszText)
2461 lstrcpyW(btnInfo->text, nmtb.pszText);
2462 else if (nmtb.tbButton.iString >= 0 &&
2463 nmtb.tbButton.iString < infoPtr->nNumStrings)
2465 lstrcpyW(btnInfo->text,
2466 infoPtr->strings[nmtb.tbButton.iString]);
2470 if (index == -1)
2471 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2474 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2476 /* select first item in the 'available' list */
2477 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2479 /* append 'virtual' separator button to the 'toolbar buttons' list */
2480 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2481 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2482 btnInfo->btn.fsStyle = BTNS_SEP;
2483 btnInfo->bVirtual = TRUE;
2484 btnInfo->bRemovable = FALSE;
2485 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2486 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2487 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2489 /* select last item in the 'toolbar' list */
2490 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2491 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2493 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2494 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2496 /* set focus and disable buttons */
2497 PostMessageW (hwnd, WM_USER, 0, 0);
2499 return TRUE;
2501 case WM_USER:
2502 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2503 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2504 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2505 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2506 return TRUE;
2508 case WM_CLOSE:
2509 EndDialog(hwnd, FALSE);
2510 return TRUE;
2512 case WM_COMMAND:
2513 switch (LOWORD(wParam))
2515 case IDC_TOOLBARBTN_LBOX:
2516 if (HIWORD(wParam) == LBN_SELCHANGE)
2518 PCUSTOMBUTTON btnInfo;
2519 NMTOOLBARA nmtb;
2520 int count;
2521 int index;
2523 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2524 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2526 /* send TBN_QUERYINSERT notification */
2527 nmtb.iItem = index;
2528 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2530 /* get list box item */
2531 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2533 if (index == (count - 1))
2535 /* last item (virtual separator) */
2536 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2537 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2539 else if (index == (count - 2))
2541 /* second last item (last non-virtual item) */
2542 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2543 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2545 else if (index == 0)
2547 /* first item */
2548 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2549 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2551 else
2553 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2554 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2557 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2559 break;
2561 case IDC_MOVEUP_BTN:
2563 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2564 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2566 break;
2568 case IDC_MOVEDN_BTN: /* move down */
2570 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2571 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2573 break;
2575 case IDC_REMOVE_BTN: /* remove button */
2577 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2579 if (LB_ERR == index)
2580 break;
2582 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2584 break;
2585 case IDC_HELP_BTN:
2586 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2587 break;
2588 case IDC_RESET_BTN:
2589 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2590 break;
2592 case IDOK: /* Add button */
2594 int index;
2595 int indexto;
2597 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2598 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2600 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2602 break;
2604 case IDCANCEL:
2605 EndDialog(hwnd, FALSE);
2606 break;
2608 return TRUE;
2610 case WM_DESTROY:
2612 int count;
2613 int i;
2615 /* delete items from 'toolbar buttons' listbox*/
2616 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2617 for (i = 0; i < count; i++)
2619 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2620 Free(btnInfo);
2621 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2623 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2626 /* delete items from 'available buttons' listbox*/
2627 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2628 for (i = 0; i < count; i++)
2630 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2631 Free(btnInfo);
2632 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2634 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2636 return TRUE;
2638 case WM_DRAWITEM:
2639 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2641 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2642 RECT rcButton;
2643 RECT rcText;
2644 HPEN hPen, hOldPen;
2645 HBRUSH hOldBrush;
2646 COLORREF oldText = 0;
2647 COLORREF oldBk = 0;
2649 /* get item data */
2650 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2651 if (btnInfo == NULL)
2653 FIXME("btnInfo invalid\n");
2654 return TRUE;
2657 /* set colors and select objects */
2658 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2659 if (btnInfo->bVirtual)
2660 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2661 else
2662 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2663 hPen = CreatePen( PS_SOLID, 1,
2664 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2665 hOldPen = SelectObject (lpdis->hDC, hPen );
2666 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2668 /* fill background rectangle */
2669 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2670 lpdis->rcItem.right, lpdis->rcItem.bottom);
2672 /* calculate button and text rectangles */
2673 rcButton = lpdis->rcItem;
2674 InflateRect (&rcButton, -1, -1);
2675 rcText = rcButton;
2676 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2677 rcText.left = rcButton.right + 2;
2679 /* draw focus rectangle */
2680 if (lpdis->itemState & ODS_FOCUS)
2681 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2683 /* draw button */
2684 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2685 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2687 /* draw image and text */
2688 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2689 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2690 btnInfo->btn.iBitmap));
2691 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2692 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2694 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2695 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2697 /* delete objects and reset colors */
2698 SelectObject (lpdis->hDC, hOldBrush);
2699 SelectObject (lpdis->hDC, hOldPen);
2700 SetBkColor (lpdis->hDC, oldBk);
2701 SetTextColor (lpdis->hDC, oldText);
2702 DeleteObject( hPen );
2703 return TRUE;
2705 return FALSE;
2707 case WM_MEASUREITEM:
2708 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2710 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2712 lpmis->itemHeight = 15 + 8; /* default height */
2714 return TRUE;
2716 return FALSE;
2718 default:
2719 if (uDragListMessage && (uMsg == uDragListMessage))
2721 if (wParam == IDC_TOOLBARBTN_LBOX)
2723 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2724 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2725 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2726 return TRUE;
2728 else if (wParam == IDC_AVAILBTN_LBOX)
2730 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2731 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2732 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2733 return TRUE;
2736 return FALSE;
2740 static BOOL
2741 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2743 HBITMAP hbmLoad;
2744 INT nCountBefore = ImageList_GetImageCount(himlDef);
2745 INT nCountAfter;
2746 INT cxIcon, cyIcon;
2747 INT nAdded;
2748 INT nIndex;
2750 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2751 /* Add bitmaps to the default image list */
2752 if (bitmap->hInst == NULL) /* a handle was passed */
2753 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2754 else if (bitmap->hInst == COMCTL32_hModule)
2755 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2756 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2757 else
2758 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2760 /* enlarge the bitmap if needed */
2761 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2762 if (bitmap->hInst != COMCTL32_hModule)
2763 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2765 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2766 DeleteObject(hbmLoad);
2767 if (nIndex == -1)
2768 return FALSE;
2770 nCountAfter = ImageList_GetImageCount(himlDef);
2771 nAdded = nCountAfter - nCountBefore;
2772 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2774 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2775 } else if (nAdded > (INT)bitmap->nButtons) {
2776 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2777 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2780 infoPtr->nNumBitmaps += nAdded;
2781 return TRUE;
2784 static void
2785 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2787 HIMAGELIST himlDef;
2788 HIMAGELIST himlNew;
2789 INT cx, cy;
2790 INT i;
2792 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2793 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2794 return;
2795 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2796 return;
2797 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2798 return;
2800 TRACE("Update icon size: %dx%d -> %dx%d\n",
2801 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2803 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2804 ILC_COLOR32|ILC_MASK, 8, 2);
2805 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2806 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2807 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2808 infoPtr->himlInt = himlNew;
2810 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2811 ImageList_Destroy(himlDef);
2814 /***********************************************************************
2815 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2818 static LRESULT
2819 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2821 TBITMAP_INFO info;
2822 INT iSumButtons, i;
2823 HIMAGELIST himlDef;
2825 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2826 if (!lpAddBmp)
2827 return -1;
2829 if (lpAddBmp->hInst == HINST_COMMCTRL)
2831 info.hInst = COMCTL32_hModule;
2832 switch (lpAddBmp->nID)
2834 case IDB_STD_SMALL_COLOR:
2835 case 2:
2836 info.nButtons = 15;
2837 info.nID = IDB_STD_SMALL;
2838 break;
2839 case IDB_STD_LARGE_COLOR:
2840 case 3:
2841 info.nButtons = 15;
2842 info.nID = IDB_STD_LARGE;
2843 break;
2844 case IDB_VIEW_SMALL_COLOR:
2845 case 6:
2846 info.nButtons = 12;
2847 info.nID = IDB_VIEW_SMALL;
2848 break;
2849 case IDB_VIEW_LARGE_COLOR:
2850 case 7:
2851 info.nButtons = 12;
2852 info.nID = IDB_VIEW_LARGE;
2853 break;
2854 case IDB_HIST_SMALL_COLOR:
2855 info.nButtons = 5;
2856 info.nID = IDB_HIST_SMALL;
2857 break;
2858 case IDB_HIST_LARGE_COLOR:
2859 info.nButtons = 5;
2860 info.nID = IDB_HIST_LARGE;
2861 break;
2862 default:
2863 WARN("unknown bitmap id, %Id\n", lpAddBmp->nID);
2864 return -1;
2867 TRACE ("adding %d internal bitmaps\n", info.nButtons);
2869 /* Windows resize all the buttons to the size of a newly added standard image */
2870 if (lpAddBmp->nID & 1)
2872 /* large icons: 24x24. Will make the button 31x30 */
2873 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2875 else
2877 /* small icons: 16x16. Will make the buttons 23x22 */
2878 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2881 TOOLBAR_CalcToolbar (infoPtr);
2883 else
2885 info.nButtons = count;
2886 info.hInst = lpAddBmp->hInst;
2887 info.nID = lpAddBmp->nID;
2888 TRACE("adding %d bitmaps\n", info.nButtons);
2891 /* check if the bitmap is already loaded and compute iSumButtons */
2892 iSumButtons = 0;
2893 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2895 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2896 infoPtr->bitmaps[i].nID == info.nID)
2897 return iSumButtons;
2898 iSumButtons += infoPtr->bitmaps[i].nButtons;
2901 if (!infoPtr->cimlDef) {
2902 /* create new default image list */
2903 TRACE ("creating default image list\n");
2905 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2906 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2907 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2908 infoPtr->himlInt = himlDef;
2910 else {
2911 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2914 if (!himlDef) {
2915 WARN("No default image list available\n");
2916 return -1;
2919 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2920 return -1;
2922 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2923 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2924 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2925 infoPtr->nNumBitmapInfos++;
2926 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2928 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2929 return iSumButtons;
2933 static LRESULT
2934 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2936 TRACE("adding %d buttons (unicode=%d)\n", nAddButtons, fUnicode);
2938 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2942 static LRESULT
2943 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2945 #define MAX_RESOURCE_STRING_LENGTH 512
2946 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2947 INT nIndex = infoPtr->nNumStrings;
2949 TRACE("%p, %Ix\n", hInstance, lParam);
2951 if (IS_INTRESOURCE(lParam)) {
2952 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2953 WCHAR delimiter;
2954 WCHAR *next_delim;
2955 HRSRC hrsrc;
2956 WCHAR *p;
2957 INT len;
2959 TRACE("adding string from resource\n");
2961 if (!hInstance) return -1;
2963 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2964 (LPWSTR)RT_STRING );
2965 if (!hrsrc)
2967 TRACE("string not found in resources\n");
2968 return -1;
2971 len = LoadStringW (hInstance, (UINT)lParam,
2972 szString, MAX_RESOURCE_STRING_LENGTH);
2974 TRACE("len=%d %s\n", len, debugstr_w(szString));
2975 if (len == 0 || len == 1)
2976 return nIndex;
2978 TRACE("delimiter: 0x%x\n", *szString);
2979 delimiter = *szString;
2980 p = szString + 1;
2982 while ((next_delim = wcschr(p, delimiter)) != NULL) {
2983 *next_delim = 0;
2984 if (next_delim + 1 >= szString + len)
2986 /* this may happen if delimiter == '\0' or if the last char is a
2987 * delimiter (then it is ignored like the native does) */
2988 break;
2991 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2992 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2993 infoPtr->nNumStrings++;
2995 p = next_delim + 1;
2998 else {
2999 LPWSTR p = (LPWSTR)lParam;
3000 INT len;
3002 if (p == NULL)
3003 return -1;
3004 TRACE("adding string(s) from array\n");
3005 while (*p) {
3006 len = lstrlenW (p);
3008 TRACE("len=%d %s\n", len, debugstr_w(p));
3009 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3010 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
3011 infoPtr->nNumStrings++;
3013 p += (len+1);
3017 if (fFirstString)
3018 TOOLBAR_CalcToolbar(infoPtr);
3019 return nIndex;
3023 static LRESULT
3024 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3026 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3027 LPSTR p;
3028 INT nIndex;
3029 INT len;
3031 TRACE("%p, %Ix\n", hInstance, lParam);
3033 if (IS_INTRESOURCE(lParam)) /* load from resources */
3034 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3036 p = (LPSTR)lParam;
3037 if (p == NULL)
3038 return -1;
3040 TRACE("adding string(s) from array\n");
3041 nIndex = infoPtr->nNumStrings;
3042 while (*p) {
3043 len = strlen (p);
3044 TRACE("len=%d \"%s\"\n", len, p);
3046 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3047 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3048 infoPtr->nNumStrings++;
3050 p += (len+1);
3053 if (fFirstString)
3054 TOOLBAR_CalcToolbar(infoPtr);
3055 return nIndex;
3059 static LRESULT
3060 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3062 TRACE("auto sizing, style=%#lx\n", infoPtr->dwStyle);
3063 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3065 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3067 RECT window_rect, client_rect, parent_rect, border;
3068 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3069 HWND parent;
3070 INT x, y, cx, cy;
3072 parent = GetParent (infoPtr->hwndSelf);
3074 if (!parent || !infoPtr->bDoRedraw)
3075 return 0;
3077 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3078 GetClientRect(infoPtr->hwndSelf, &client_rect);
3079 border = window_rect;
3080 MapWindowPoints(0, infoPtr->hwndSelf, (POINT *)&border, 2);
3081 border.right -= border.left + client_rect.right - client_rect.left;
3082 border.bottom -= border.top + client_rect.bottom - client_rect.top;
3084 GetClientRect(parent, &parent_rect);
3086 x = parent_rect.left;
3087 y = parent_rect.top;
3089 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3090 cx = parent_rect.right - parent_rect.left;
3092 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3094 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3095 y = window_rect.top;
3097 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3098 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3100 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3101 uPosFlags |= SWP_NOMOVE;
3103 if (!(infoPtr->dwStyle & CCS_NOMOVEY) && !(infoPtr->dwStyle & CCS_NODIVIDER))
3104 y += GetSystemMetrics(SM_CYEDGE);
3106 x += border.left;
3107 y += border.top;
3108 cx += border.right;
3109 cy += border.bottom;
3111 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3114 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3116 TOOLBAR_LayoutToolbar(infoPtr);
3117 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3120 return 0;
3124 static inline LRESULT
3125 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3127 return infoPtr->nNumButtons;
3131 static inline LRESULT
3132 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3134 infoPtr->dwStructSize = Size;
3136 return 0;
3140 static LRESULT
3141 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3143 TBUTTON_INFO *btnPtr;
3144 INT nIndex;
3146 TRACE("button %d, iBitmap now %d\n", Id, Index);
3148 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3149 if (nIndex == -1)
3150 return FALSE;
3152 btnPtr = &infoPtr->buttons[nIndex];
3153 btnPtr->iBitmap = Index;
3155 /* we HAVE to erase the background, the new bitmap could be */
3156 /* transparent */
3157 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3159 return TRUE;
3163 static LRESULT
3164 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3166 TBUTTON_INFO *btnPtr;
3167 INT nIndex;
3168 INT nOldIndex = -1;
3169 BOOL bChecked = FALSE;
3171 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3173 TRACE("hwnd %p, btn index %d, lParam %Ix\n", infoPtr->hwndSelf, nIndex, lParam);
3175 if (nIndex == -1)
3176 return FALSE;
3178 btnPtr = &infoPtr->buttons[nIndex];
3180 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3182 if (!LOWORD(lParam))
3183 btnPtr->fsState &= ~TBSTATE_CHECKED;
3184 else {
3185 if (btnPtr->fsStyle & BTNS_GROUP) {
3186 nOldIndex =
3187 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3188 if (nOldIndex == nIndex)
3189 return 0;
3190 if (nOldIndex != -1)
3191 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3193 btnPtr->fsState |= TBSTATE_CHECKED;
3196 if( bChecked != LOWORD(lParam) )
3198 if (nOldIndex != -1)
3199 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3200 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3203 /* FIXME: Send a WM_NOTIFY?? */
3205 return TRUE;
3209 static LRESULT
3210 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3212 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3216 static LRESULT
3217 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3219 CUSTDLG_INFO custInfo;
3220 LRESULT ret;
3221 NMHDR nmhdr;
3223 custInfo.tbInfo = infoPtr;
3224 custInfo.tbHwnd = infoPtr->hwndSelf;
3226 /* send TBN_BEGINADJUST notification */
3227 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3229 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3230 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3232 /* send TBN_ENDADJUST notification */
3233 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3235 return ret;
3239 static LRESULT
3240 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3242 NMTOOLBARW nmtb;
3243 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3245 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3246 return FALSE;
3248 memset(&nmtb, 0, sizeof(nmtb));
3249 nmtb.iItem = btnPtr->idCommand;
3250 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3251 nmtb.tbButton.idCommand = btnPtr->idCommand;
3252 nmtb.tbButton.fsState = btnPtr->fsState;
3253 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3254 nmtb.tbButton.dwData = btnPtr->dwData;
3255 nmtb.tbButton.iString = btnPtr->iString;
3256 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3258 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3260 infoPtr->nHotItem = -1;
3261 if (infoPtr->nNumButtons == 1) {
3262 TRACE(" simple delete\n");
3263 free_string( infoPtr->buttons );
3264 Free (infoPtr->buttons);
3265 infoPtr->buttons = NULL;
3266 infoPtr->nNumButtons = 0;
3268 else {
3269 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3270 TRACE("complex delete [nIndex=%d]\n", nIndex);
3272 infoPtr->nNumButtons--;
3273 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3274 if (nIndex > 0) {
3275 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3276 nIndex * sizeof(TBUTTON_INFO));
3279 if (nIndex < infoPtr->nNumButtons) {
3280 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3281 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3284 free_string( oldButtons + nIndex );
3285 Free (oldButtons);
3288 TOOLBAR_LayoutToolbar(infoPtr);
3290 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3292 return TRUE;
3296 static LRESULT
3297 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3299 TBUTTON_INFO *btnPtr;
3300 INT nIndex;
3301 DWORD bState;
3303 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3305 TRACE("hwnd %p, btn id %d, lParam %Ix\n", infoPtr->hwndSelf, Id, lParam);
3307 if (nIndex == -1)
3308 return FALSE;
3310 btnPtr = &infoPtr->buttons[nIndex];
3312 bState = btnPtr->fsState & TBSTATE_ENABLED;
3314 /* update the toolbar button state */
3315 if(!LOWORD(lParam)) {
3316 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3317 } else {
3318 btnPtr->fsState |= TBSTATE_ENABLED;
3321 /* redraw the button only if the state of the button changed */
3322 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3323 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3325 return TRUE;
3329 static inline LRESULT
3330 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3332 return infoPtr->bAnchor;
3336 static LRESULT
3337 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3339 INT nIndex;
3341 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3342 if (nIndex == -1)
3343 return -1;
3345 return infoPtr->buttons[nIndex].iBitmap;
3349 static inline LRESULT
3350 TOOLBAR_GetBitmapFlags (void)
3352 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3356 static LRESULT
3357 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3359 TBUTTON_INFO *btnPtr;
3361 if (lpTbb == NULL)
3362 return FALSE;
3364 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3365 return FALSE;
3367 btnPtr = &infoPtr->buttons[nIndex];
3368 lpTbb->iBitmap = btnPtr->iBitmap;
3369 lpTbb->idCommand = btnPtr->idCommand;
3370 lpTbb->fsState = btnPtr->fsState;
3371 lpTbb->fsStyle = btnPtr->fsStyle;
3372 lpTbb->bReserved[0] = 0;
3373 lpTbb->bReserved[1] = 0;
3374 lpTbb->dwData = btnPtr->dwData;
3375 lpTbb->iString = btnPtr->iString;
3377 return TRUE;
3381 static LRESULT
3382 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3384 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3385 TBUTTON_INFO *btnPtr;
3386 INT nIndex;
3388 if (lpTbInfo == NULL)
3389 return -1;
3391 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3392 * the headers and tests shows that even with comctl 6 Vista accepts only the
3393 * original TBBUTTONINFO size
3395 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3397 WARN("Invalid button size\n");
3398 return -1;
3401 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3402 if (nIndex == -1)
3403 return -1;
3405 btnPtr = &infoPtr->buttons[nIndex];
3406 if (lpTbInfo->dwMask & TBIF_COMMAND)
3407 lpTbInfo->idCommand = btnPtr->idCommand;
3408 if (lpTbInfo->dwMask & TBIF_IMAGE)
3409 lpTbInfo->iImage = btnPtr->iBitmap;
3410 if (lpTbInfo->dwMask & TBIF_LPARAM)
3411 lpTbInfo->lParam = btnPtr->dwData;
3412 if (lpTbInfo->dwMask & TBIF_SIZE)
3413 /* tests show that for separators TBIF_SIZE returns not calculated width,
3414 but cx property, that differs from 0 only if application have
3415 specifically set it */
3416 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3417 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3418 if (lpTbInfo->dwMask & TBIF_STATE)
3419 lpTbInfo->fsState = btnPtr->fsState;
3420 if (lpTbInfo->dwMask & TBIF_STYLE)
3421 lpTbInfo->fsStyle = btnPtr->fsStyle;
3422 if (lpTbInfo->dwMask & TBIF_TEXT) {
3423 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3424 can't use TOOLBAR_GetText here */
3425 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3426 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3427 if (bUnicode)
3428 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3429 else
3430 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3431 } else if (!bUnicode || lpTbInfo->pszText)
3432 lpTbInfo->pszText[0] = '\0';
3434 return nIndex;
3438 static inline LRESULT
3439 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3441 return MAKELONG((WORD)infoPtr->nButtonWidth,
3442 (WORD)infoPtr->nButtonHeight);
3446 static LRESULT
3447 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3449 INT nIndex;
3450 LPWSTR lpText;
3451 LRESULT ret = 0;
3453 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3454 if (nIndex == -1)
3455 return -1;
3457 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3459 if (isW)
3461 if (lpText)
3463 ret = lstrlenW (lpText);
3464 if (lpStr) lstrcpyW (lpStr, lpText);
3467 else
3468 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3469 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3470 return ret;
3474 static LRESULT
3475 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3477 TRACE("hwnd %p, wParam %Id\n", infoPtr->hwndSelf, wParam);
3478 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3479 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3483 static inline LRESULT
3484 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3486 TRACE("\n");
3488 return infoPtr->dwExStyle;
3492 static LRESULT
3493 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3495 TRACE("hwnd %p, wParam %Id\n", infoPtr->hwndSelf, wParam);
3496 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3497 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3501 static LRESULT
3502 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3504 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme))
3505 return -1;
3507 if (infoPtr->nHotItem < 0)
3508 return -1;
3510 return (LRESULT)infoPtr->nHotItem;
3514 static LRESULT
3515 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3517 TRACE("hwnd %p, wParam %Id\n", infoPtr->hwndSelf, wParam);
3518 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3519 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3523 static LRESULT
3524 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3526 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3528 *lptbim = infoPtr->tbim;
3530 return 0;
3534 static inline LRESULT
3535 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3537 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3539 return (LRESULT)infoPtr->clrInsertMark;
3543 static LRESULT
3544 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3546 TBUTTON_INFO *btnPtr;
3548 btnPtr = &infoPtr->buttons[nIndex];
3549 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3550 return FALSE;
3552 if (lpRect == NULL)
3553 return FALSE;
3554 if (btnPtr->fsState & TBSTATE_HIDDEN)
3555 return FALSE;
3557 lpRect->left = btnPtr->rect.left;
3558 lpRect->right = btnPtr->rect.right;
3559 lpRect->bottom = btnPtr->rect.bottom;
3560 lpRect->top = btnPtr->rect.top;
3562 return TRUE;
3566 static LRESULT
3567 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3569 if (lpSize == NULL)
3570 return FALSE;
3572 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3573 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3575 TRACE("maximum size %ld x %ld\n", infoPtr->rcBound.right - infoPtr->rcBound.left,
3576 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3578 return TRUE;
3582 /* << TOOLBAR_GetObject >> */
3585 static inline LRESULT
3586 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3588 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3592 static LRESULT
3593 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3595 TBUTTON_INFO *btnPtr;
3596 INT nIndex;
3598 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3599 btnPtr = &infoPtr->buttons[nIndex];
3600 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3601 return FALSE;
3603 if (lpRect == NULL)
3604 return FALSE;
3606 lpRect->left = btnPtr->rect.left;
3607 lpRect->right = btnPtr->rect.right;
3608 lpRect->bottom = btnPtr->rect.bottom;
3609 lpRect->top = btnPtr->rect.top;
3611 return TRUE;
3615 static inline LRESULT
3616 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3618 return infoPtr->nRows;
3622 static LRESULT
3623 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3625 INT nIndex;
3627 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3628 if (nIndex == -1)
3629 return -1;
3631 return infoPtr->buttons[nIndex].fsState;
3635 static inline LRESULT
3636 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3638 return infoPtr->dwStyle;
3642 static inline LRESULT
3643 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3645 return infoPtr->nMaxTextRows;
3649 static LRESULT
3650 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3652 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3653 TOOLBAR_TooltipCreateControl(infoPtr);
3654 return (LRESULT)infoPtr->hwndToolTip;
3658 static LRESULT
3659 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3661 TRACE("%s hwnd=%p\n",
3662 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3664 return infoPtr->bUnicode;
3668 static inline LRESULT
3669 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3671 return infoPtr->iVersion;
3675 static LRESULT
3676 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3678 TBUTTON_INFO *btnPtr;
3679 BYTE oldState;
3680 INT nIndex;
3682 TRACE("\n");
3684 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3685 if (nIndex == -1)
3686 return FALSE;
3688 btnPtr = &infoPtr->buttons[nIndex];
3689 oldState = btnPtr->fsState;
3691 if (fHide)
3692 btnPtr->fsState |= TBSTATE_HIDDEN;
3693 else
3694 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3696 if (oldState != btnPtr->fsState) {
3697 TOOLBAR_LayoutToolbar (infoPtr);
3698 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3701 return TRUE;
3705 static inline LRESULT
3706 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3708 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3712 static LRESULT
3713 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3715 TBUTTON_INFO *btnPtr;
3716 INT nIndex;
3717 DWORD oldState;
3719 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3720 if (nIndex == -1)
3721 return FALSE;
3723 btnPtr = &infoPtr->buttons[nIndex];
3724 oldState = btnPtr->fsState;
3726 if (fIndeterminate)
3727 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3728 else
3729 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3731 if(oldState != btnPtr->fsState)
3732 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3734 return TRUE;
3738 static LRESULT
3739 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3741 if (lpTbb == NULL)
3742 return FALSE;
3744 if (nIndex == -1) {
3745 /* EPP: this seems to be an undocumented call (from my IE4)
3746 * I assume in that case that:
3747 * - index of insertion is at the end of existing buttons
3748 * I only see this happen with nIndex == -1, but it could have a special
3749 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3751 nIndex = infoPtr->nNumButtons;
3753 } else if (nIndex < 0)
3754 return FALSE;
3756 TRACE("inserting button index=%d\n", nIndex);
3757 if (nIndex > infoPtr->nNumButtons) {
3758 nIndex = infoPtr->nNumButtons;
3759 TRACE("adjust index=%d\n", nIndex);
3762 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3765 /* << TOOLBAR_InsertMarkHitTest >> */
3768 static LRESULT
3769 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3771 INT nIndex;
3773 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3774 if (nIndex == -1)
3775 return -1;
3777 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3781 static LRESULT
3782 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3784 INT nIndex;
3786 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3787 if (nIndex == -1)
3788 return -1;
3790 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3794 static LRESULT
3795 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3797 INT nIndex;
3799 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3800 if (nIndex == -1)
3801 return -1;
3803 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3807 static LRESULT
3808 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3810 INT nIndex;
3812 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3813 if (nIndex == -1)
3814 return -1;
3816 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3820 static LRESULT
3821 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3823 INT nIndex;
3825 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3826 if (nIndex == -1)
3827 return -1;
3829 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3833 static LRESULT
3834 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3836 INT nIndex;
3838 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3839 if (nIndex == -1)
3840 return -1;
3842 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3846 static LRESULT
3847 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3849 TBADDBITMAP tbab;
3850 tbab.hInst = hInstance;
3851 tbab.nID = wParam;
3853 TRACE("hwnd = %p, hInst = %p, nID = %Iu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3855 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3859 static LRESULT
3860 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3862 WCHAR wszAccel[] = {'&',wAccel,0};
3863 int i;
3865 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3866 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3868 for (i = 0; i < infoPtr->nNumButtons; i++)
3870 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3871 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3872 !(btnPtr->fsState & TBSTATE_HIDDEN))
3874 int iLen = lstrlenW(wszAccel);
3875 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3877 if (!lpszStr)
3878 continue;
3880 while (*lpszStr)
3882 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3884 lpszStr += 2;
3885 continue;
3887 if (!wcsnicmp(lpszStr, wszAccel, iLen))
3889 *pIDButton = btnPtr->idCommand;
3890 return TRUE;
3892 lpszStr++;
3896 return FALSE;
3900 static LRESULT
3901 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3903 INT nIndex;
3904 DWORD oldState;
3905 TBUTTON_INFO *btnPtr;
3907 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3909 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3910 if (nIndex == -1)
3911 return FALSE;
3913 btnPtr = &infoPtr->buttons[nIndex];
3914 oldState = btnPtr->fsState;
3916 if (fMark)
3917 btnPtr->fsState |= TBSTATE_MARKED;
3918 else
3919 btnPtr->fsState &= ~TBSTATE_MARKED;
3921 if(oldState != btnPtr->fsState)
3922 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3924 return TRUE;
3928 /* fixes up an index of a button affected by a move */
3929 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3931 if (bMoveUp)
3933 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3934 (*pIndex)--;
3935 else if (*pIndex == nIndex)
3936 *pIndex = nMoveIndex;
3938 else
3940 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3941 (*pIndex)++;
3942 else if (*pIndex == nIndex)
3943 *pIndex = nMoveIndex;
3948 static LRESULT
3949 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3951 INT nIndex;
3952 INT nCount;
3953 TBUTTON_INFO button;
3955 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3957 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3958 if ((nIndex == -1) || (nMoveIndex < 0))
3959 return FALSE;
3961 if (nMoveIndex > infoPtr->nNumButtons - 1)
3962 nMoveIndex = infoPtr->nNumButtons - 1;
3964 button = infoPtr->buttons[nIndex];
3966 /* move button right */
3967 if (nIndex < nMoveIndex)
3969 nCount = nMoveIndex - nIndex;
3970 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3971 infoPtr->buttons[nMoveIndex] = button;
3973 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3974 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3975 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3976 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3978 else if (nIndex > nMoveIndex) /* move button left */
3980 nCount = nIndex - nMoveIndex;
3981 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3982 infoPtr->buttons[nMoveIndex] = button;
3984 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3985 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3986 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3987 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3990 TOOLBAR_LayoutToolbar(infoPtr);
3991 TOOLBAR_AutoSize(infoPtr);
3992 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3994 return TRUE;
3998 static LRESULT
3999 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
4001 TBUTTON_INFO *btnPtr;
4002 INT nIndex;
4003 DWORD oldState;
4005 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4006 if (nIndex == -1)
4007 return FALSE;
4009 btnPtr = &infoPtr->buttons[nIndex];
4010 oldState = btnPtr->fsState;
4012 if (fPress)
4013 btnPtr->fsState |= TBSTATE_PRESSED;
4014 else
4015 btnPtr->fsState &= ~TBSTATE_PRESSED;
4017 if(oldState != btnPtr->fsState)
4018 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4020 return TRUE;
4023 /* FIXME: there might still be some confusion her between number of buttons
4024 * and number of bitmaps */
4025 static LRESULT
4026 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
4028 HBITMAP hBitmap;
4029 int i = 0, nOldButtons = 0, pos = 0;
4030 int nOldBitmaps, nNewBitmaps = 0;
4031 HIMAGELIST himlDef = 0;
4033 TRACE("hInstOld %p nIDOld %Ix hInstNew %p nIDNew %Ix nButtons %x\n",
4034 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4035 lpReplace->nButtons);
4037 if (lpReplace->hInstOld == HINST_COMMCTRL)
4039 FIXME("changing standard bitmaps not implemented\n");
4040 return FALSE;
4042 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4043 FIXME("resources not in the current module not implemented\n");
4045 TRACE("To be replaced hInstOld %p nIDOld %Ix\n", lpReplace->hInstOld, lpReplace->nIDOld);
4046 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4047 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4048 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4049 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4051 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4052 nOldButtons = tbi->nButtons;
4053 tbi->nButtons = lpReplace->nButtons;
4054 tbi->hInst = lpReplace->hInstNew;
4055 tbi->nID = lpReplace->nIDNew;
4056 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4057 break;
4059 pos += tbi->nButtons;
4062 if (nOldButtons == 0)
4064 WARN("No hinst/bitmap found! hInst %p nID %Ix\n", lpReplace->hInstOld, lpReplace->nIDOld);
4065 return FALSE;
4068 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4069 * bitmap
4071 if (lpReplace->hInstNew)
4072 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4073 else
4074 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4076 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4077 nOldBitmaps = ImageList_GetImageCount(himlDef);
4079 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4081 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4082 ImageList_Remove(himlDef, i);
4084 if (hBitmap)
4086 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4087 nNewBitmaps = ImageList_GetImageCount(himlDef);
4088 DeleteObject(hBitmap);
4091 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4093 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4094 pos, nOldBitmaps, nNewBitmaps);
4096 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4097 return TRUE;
4101 /* helper for TOOLBAR_SaveRestoreW */
4102 static BOOL
4103 TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
4105 NMTBSAVE save;
4106 INT ret, i;
4107 BOOL alloced = FALSE;
4108 HKEY key;
4110 TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );
4112 memset( &save, 0, sizeof(save) );
4113 save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
4114 save.iItem = -1;
4115 save.cButtons = infoPtr->nNumButtons;
4116 save.tbButton.idCommand = -1;
4117 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4119 if (!save.pData)
4121 save.pData = Alloc( save.cbData );
4122 if (!save.pData) return FALSE;
4123 alloced = TRUE;
4125 if (!save.pCurrent) save.pCurrent = save.pData;
4127 for (i = 0; i < infoPtr->nNumButtons; i++)
4129 save.iItem = i;
4130 save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
4131 save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
4132 save.tbButton.fsState = infoPtr->buttons[i].fsState;
4133 save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
4134 memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
4135 save.tbButton.dwData = infoPtr->buttons[i].dwData;
4136 save.tbButton.iString = infoPtr->buttons[i].iString;
4138 *save.pCurrent++ = save.tbButton.idCommand;
4140 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4143 ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
4144 if (ret == ERROR_SUCCESS)
4146 ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
4147 RegCloseKey( key );
4150 if (alloced) Free( save.pData );
4151 return !ret;
4155 /* helper for TOOLBAR_Restore */
4156 static void
4157 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4159 INT i;
4161 for (i = 0; i < infoPtr->nNumButtons; i++)
4163 free_string( infoPtr->buttons + i );
4164 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4167 Free(infoPtr->buttons);
4168 infoPtr->buttons = NULL;
4169 infoPtr->nNumButtons = 0;
4173 /* helper for TOOLBAR_SaveRestoreW */
4174 static BOOL
4175 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4177 LONG res;
4178 HKEY hkey = NULL;
4179 BOOL ret = FALSE;
4180 DWORD dwType;
4181 DWORD dwSize = 0;
4182 NMTBRESTORE nmtbr;
4183 NMHDR hdr;
4185 /* restore toolbar information */
4186 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4187 debugstr_w(lpSave->pszValueName));
4189 memset(&nmtbr, 0, sizeof(nmtbr));
4191 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4192 KEY_QUERY_VALUE, &hkey);
4193 if (!res)
4194 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4195 NULL, &dwSize);
4196 if (!res && dwType != REG_BINARY)
4197 res = ERROR_FILE_NOT_FOUND;
4198 if (!res)
4200 nmtbr.pData = Alloc(dwSize);
4201 nmtbr.cbData = dwSize;
4202 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4204 if (!res)
4205 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4206 (LPBYTE)nmtbr.pData, &dwSize);
4207 if (!res)
4209 nmtbr.pCurrent = nmtbr.pData;
4210 nmtbr.iItem = -1;
4211 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4212 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4214 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4216 INT i, count = nmtbr.cButtons;
4218 /* remove all existing buttons as this function is designed to
4219 * restore the toolbar to a previously saved state */
4220 TOOLBAR_DeleteAllButtons(infoPtr);
4222 for (i = 0; i < count; i++)
4224 nmtbr.iItem = i;
4225 nmtbr.tbButton.iBitmap = -1;
4226 nmtbr.tbButton.fsState = 0;
4227 nmtbr.tbButton.fsStyle = 0;
4228 nmtbr.tbButton.dwData = 0;
4229 nmtbr.tbButton.iString = 0;
4231 if (*nmtbr.pCurrent & 0x80000000)
4233 /* separator */
4234 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4235 nmtbr.tbButton.idCommand = 0;
4236 nmtbr.tbButton.fsStyle = BTNS_SEP;
4237 if (*nmtbr.pCurrent != (DWORD)-1)
4238 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4240 else
4241 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4243 nmtbr.pCurrent++;
4245 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4247 /* All returned ptrs and -1 are ignored */
4248 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4249 nmtbr.tbButton.iString = 0;
4251 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4254 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
4255 for (i = 0; ; i++)
4257 NMTOOLBARW tb;
4258 TBBUTTONINFOW bi;
4259 WCHAR buf[128];
4260 UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
4261 INT idx;
4263 memset( &tb, 0, sizeof(tb) );
4264 tb.iItem = i;
4265 tb.cchText = ARRAY_SIZE(buf);
4266 tb.pszText = buf;
4268 /* Use the same struct for both A and W versions since the layout is the same. */
4269 if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
4270 break;
4272 idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
4273 if (idx == -1) continue;
4275 /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
4276 be an index or a ptr. Either way it is simply copied. There is no api to change
4277 the string index, so we set it manually. The other properties can be set with SetButtonInfo. */
4278 free_string( infoPtr->buttons + idx );
4279 infoPtr->buttons[idx].iString = tb.tbButton.iString;
4281 memset( &bi, 0, sizeof(bi) );
4282 bi.cbSize = sizeof(bi);
4283 bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
4284 bi.iImage = tb.tbButton.iBitmap;
4285 bi.fsState = tb.tbButton.fsState;
4286 bi.fsStyle = tb.tbButton.fsStyle;
4287 bi.lParam = tb.tbButton.dwData;
4289 TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4291 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4293 /* remove all uninitialised buttons
4294 * note: loop backwards to avoid having to fixup i on a
4295 * delete */
4296 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4297 if (infoPtr->buttons[i].iBitmap == -1)
4298 TOOLBAR_DeleteButton(infoPtr, i);
4300 /* only indicate success if at least one button survived */
4301 if (infoPtr->nNumButtons > 0) ret = TRUE;
4304 Free (nmtbr.pData);
4305 RegCloseKey(hkey);
4307 return ret;
4311 static LRESULT
4312 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4314 if (lpSave == NULL) return 0;
4316 if (wParam)
4317 return TOOLBAR_Save(infoPtr, lpSave);
4318 else
4319 return TOOLBAR_Restore(infoPtr, lpSave);
4323 static LRESULT
4324 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4326 LPWSTR pszValueName = 0, pszSubKey = 0;
4327 TBSAVEPARAMSW SaveW;
4328 LRESULT result = 0;
4329 int len;
4331 if (lpSave == NULL) return 0;
4333 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4334 pszSubKey = Alloc(len * sizeof(WCHAR));
4335 if (!pszSubKey) goto exit;
4336 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4338 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4339 pszValueName = Alloc(len * sizeof(WCHAR));
4340 if (!pszValueName) goto exit;
4341 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4343 SaveW.pszValueName = pszValueName;
4344 SaveW.pszSubKey = pszSubKey;
4345 SaveW.hkr = lpSave->hkr;
4346 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4348 exit:
4349 Free (pszValueName);
4350 Free (pszSubKey);
4352 return result;
4356 static LRESULT
4357 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4359 BOOL bOldAnchor = infoPtr->bAnchor;
4361 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4363 infoPtr->bAnchor = bAnchor;
4365 /* Native does not remove the hot effect from an already hot button */
4367 return (LRESULT)bOldAnchor;
4371 static LRESULT
4372 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4374 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4375 short width = (short)LOWORD(lParam);
4376 short height = (short)HIWORD(lParam);
4378 TRACE("hwnd %p, wParam %Id, size %d x %d\n", infoPtr->hwndSelf, wParam, width, height);
4380 if (wParam != 0)
4381 FIXME("wParam is %Id. Perhaps image list index?\n", wParam);
4383 /* 0 width or height is changed to 1 */
4384 if (width == 0)
4385 width = 1;
4386 if (height == 0)
4387 height = 1;
4389 if (infoPtr->nNumButtons > 0)
4390 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4391 infoPtr->nNumButtons,
4392 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4394 if (width < -1 || height < -1)
4396 /* Windows destroys the imagelist and seems to actually use negative
4397 * values to compute button sizes */
4398 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4399 return FALSE;
4402 /* width or height of -1 means no change */
4403 if (width != -1)
4404 infoPtr->nBitmapWidth = width;
4405 if (height != -1)
4406 infoPtr->nBitmapHeight = height;
4408 if ((himlDef == infoPtr->himlInt) &&
4409 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4411 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4412 infoPtr->nBitmapHeight);
4415 TOOLBAR_CalcToolbar(infoPtr);
4416 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4417 return TRUE;
4421 static LRESULT
4422 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4423 const TBBUTTONINFOW *lptbbi, BOOL isW)
4425 TBUTTON_INFO *btnPtr;
4426 INT nIndex;
4427 RECT oldBtnRect;
4429 if (lptbbi == NULL)
4430 return FALSE;
4431 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4432 return FALSE;
4434 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4435 if (nIndex == -1)
4436 return FALSE;
4438 btnPtr = &infoPtr->buttons[nIndex];
4439 if (lptbbi->dwMask & TBIF_COMMAND)
4440 btnPtr->idCommand = lptbbi->idCommand;
4441 if (lptbbi->dwMask & TBIF_IMAGE)
4442 btnPtr->iBitmap = lptbbi->iImage;
4443 if (lptbbi->dwMask & TBIF_LPARAM)
4444 btnPtr->dwData = lptbbi->lParam;
4445 if (lptbbi->dwMask & TBIF_SIZE)
4446 btnPtr->cx = lptbbi->cx;
4447 if (lptbbi->dwMask & TBIF_STATE)
4448 btnPtr->fsState = lptbbi->fsState;
4449 if (lptbbi->dwMask & TBIF_STYLE)
4450 btnPtr->fsStyle = lptbbi->fsStyle;
4452 if (lptbbi->dwMask & TBIF_TEXT)
4453 set_stringT( btnPtr, lptbbi->pszText, isW );
4455 /* save the button rect to see if we need to redraw the whole toolbar */
4456 oldBtnRect = btnPtr->rect;
4457 TOOLBAR_LayoutToolbar(infoPtr);
4459 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4460 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4461 else
4462 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4464 return TRUE;
4468 static LRESULT
4469 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4471 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4472 int top = default_top_margin(infoPtr);
4474 if ((cx < 0) || (cy < 0))
4476 ERR("invalid parameter %#Ix\n", lParam);
4477 return FALSE;
4480 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4482 /* The documentation claims you can only change the button size before
4483 * any button has been added. But this is wrong.
4484 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4485 * it to the toolbar, and it checks that the return value is nonzero - mjm
4486 * Further testing shows that we must actually perform the change too.
4489 * The documentation also does not mention that if 0 is supplied for
4490 * either size, the system changes it to the default of 24 wide and
4491 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4493 if (cx == 0) cx = 24;
4494 if (cy == 0) cy = 22;
4496 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4497 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4499 if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
4500 top != infoPtr->iTopMargin)
4502 infoPtr->nButtonWidth = cx;
4503 infoPtr->nButtonHeight = cy;
4504 infoPtr->iTopMargin = top;
4506 TOOLBAR_LayoutToolbar( infoPtr );
4507 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
4509 return TRUE;
4513 static LRESULT
4514 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4516 /* if setting to current values, ignore */
4517 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4518 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4519 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4520 infoPtr->cxMin, infoPtr->cxMax);
4521 return TRUE;
4524 /* save new values */
4525 infoPtr->cxMin = (short)LOWORD(lParam);
4526 infoPtr->cxMax = (short)HIWORD(lParam);
4528 /* otherwise we need to recalc the toolbar and in some cases
4529 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4530 which doesn't actually draw - GA). */
4531 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4532 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4534 TOOLBAR_CalcToolbar (infoPtr);
4536 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4538 return TRUE;
4542 static LRESULT
4543 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4545 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4546 return FALSE;
4548 infoPtr->buttons[nIndex].idCommand = nId;
4550 if (infoPtr->hwndToolTip) {
4552 FIXME("change tool tip\n");
4556 return TRUE;
4560 static LRESULT
4561 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4563 HIMAGELIST himlTemp;
4564 INT id = 0;
4566 if (infoPtr->iVersion >= 5)
4567 id = wParam;
4569 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4570 &infoPtr->cimlDis, himl, id);
4572 /* FIXME: redraw ? */
4574 return (LRESULT)himlTemp;
4578 static LRESULT
4579 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD flags)
4581 DWORD old_flags;
4583 TRACE("hwnd = %p, mask = %#lx, flags %#lx\n", infoPtr->hwndSelf, mask, flags);
4585 old_flags = infoPtr->dwDTFlags;
4586 infoPtr->dwDTFlags = (old_flags & ~mask) | (flags & mask);
4588 return (LRESULT)old_flags;
4591 /* This function differs a bit from what MSDN says it does:
4592 * 1. lParam contains extended style flags to OR with current style
4593 * (MSDN isn't clear on the OR bit)
4594 * 2. wParam appears to contain extended style flags to be reset
4595 * (MSDN says that this parameter is reserved)
4597 static LRESULT
4598 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4600 DWORD old_style = infoPtr->dwExStyle;
4602 TRACE("mask %#lx, style %#lx\n", mask, style);
4604 if (mask)
4605 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4606 else
4607 infoPtr->dwExStyle = style;
4609 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4610 FIXME("Unknown Toolbar Extended Style %#lx. Please report.\n", (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4612 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4613 TOOLBAR_CalcToolbar(infoPtr);
4614 else
4615 TOOLBAR_LayoutToolbar(infoPtr);
4617 TOOLBAR_AutoSize(infoPtr);
4618 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4620 return old_style;
4624 static LRESULT
4625 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4627 HIMAGELIST himlTemp;
4628 INT id = 0;
4630 if (infoPtr->iVersion >= 5)
4631 id = wParam;
4633 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4635 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4636 &infoPtr->cimlHot, himl, id);
4638 /* FIXME: redraw ? */
4640 return (LRESULT)himlTemp;
4644 /* Makes previous hot button no longer hot, makes the specified
4645 * button hot and sends appropriate notifications. dwReason is one or
4646 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4647 * NOTE 1: this function does not validate nHit
4648 * NOTE 2: the name of this function is completely made up and
4649 * not based on any documentation from Microsoft. */
4650 static void
4651 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4653 if (infoPtr->nHotItem != nHit)
4655 NMTBHOTITEM nmhotitem;
4656 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4658 nmhotitem.dwFlags = dwReason;
4659 if(infoPtr->nHotItem >= 0)
4661 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4662 nmhotitem.idOld = oldBtnPtr->idCommand;
4664 else
4666 nmhotitem.dwFlags |= HICF_ENTERING;
4667 nmhotitem.idOld = 0;
4670 if (nHit >= 0)
4672 btnPtr = &infoPtr->buttons[nHit];
4673 nmhotitem.idNew = btnPtr->idCommand;
4675 else
4677 nmhotitem.dwFlags |= HICF_LEAVING;
4678 nmhotitem.idNew = 0;
4681 /* now change the hot and invalidate the old and new buttons - if the
4682 * parent agrees */
4683 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4685 if (oldBtnPtr) {
4686 oldBtnPtr->bHot = FALSE;
4687 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4689 /* setting disabled buttons as hot fails even if the notify contains the button id */
4690 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4691 btnPtr->bHot = TRUE;
4692 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4693 infoPtr->nHotItem = nHit;
4695 else
4696 infoPtr->nHotItem = -1;
4701 static LRESULT
4702 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4704 INT nOldHotItem = infoPtr->nHotItem;
4706 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4708 if (nHotItem >= infoPtr->nNumButtons)
4709 return infoPtr->nHotItem;
4711 if (nHotItem < 0)
4712 nHotItem = -1;
4714 /* NOTE: an application can still remove the hot item even if anchor
4715 * highlighting is enabled */
4717 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4719 if (nOldHotItem < 0)
4720 return -1;
4722 return (LRESULT)nOldHotItem;
4726 static LRESULT
4727 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4729 HIMAGELIST himlTemp;
4730 INT oldButtonWidth = infoPtr->nButtonWidth;
4731 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4732 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4733 INT i, id = 0;
4735 if (infoPtr->iVersion >= 5)
4736 id = wParam;
4738 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4739 &infoPtr->cimlDef, himl, id);
4741 infoPtr->nNumBitmaps = 0;
4742 for (i = 0; i < infoPtr->cimlDef; i++)
4743 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4745 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4746 &infoPtr->nBitmapHeight))
4748 infoPtr->nBitmapWidth = 1;
4749 infoPtr->nBitmapHeight = 1;
4751 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4753 TOOLBAR_CalcToolbar(infoPtr);
4754 if (infoPtr->nButtonWidth < oldButtonWidth)
4755 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4758 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4759 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4760 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4762 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4764 return (LRESULT)himlTemp;
4768 static LRESULT
4769 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4771 infoPtr->nIndent = nIndent;
4773 TRACE("\n");
4775 /* process only on indent changing */
4776 if(infoPtr->nIndent != nIndent)
4778 infoPtr->nIndent = nIndent;
4779 TOOLBAR_CalcToolbar (infoPtr);
4780 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4783 return TRUE;
4787 static LRESULT
4788 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4790 TRACE("hwnd = %p, lptbim = { %d, %#lx}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4792 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4794 FIXME("Unrecognized flag(s): %#lx\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4795 return 0;
4798 if ((lptbim->iButton == -1) ||
4799 ((lptbim->iButton < infoPtr->nNumButtons) &&
4800 (lptbim->iButton >= 0)))
4802 infoPtr->tbim = *lptbim;
4803 /* FIXME: don't need to update entire toolbar */
4804 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4806 else
4807 ERR("Invalid button index %d\n", lptbim->iButton);
4809 return 0;
4813 static LRESULT
4814 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4816 infoPtr->clrInsertMark = clr;
4818 /* FIXME: don't need to update entire toolbar */
4819 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4821 return 0;
4825 static LRESULT
4826 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4828 infoPtr->nMaxTextRows = nMaxRows;
4830 TOOLBAR_CalcToolbar(infoPtr);
4831 return TRUE;
4835 /* MSDN gives slightly wrong info on padding.
4836 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4837 * 2. It is not used to create a blank area between the edge of the button
4838 * and the text or image if TBSTYLE_LIST is set. It is used to control
4839 * the gap between the image and text.
4840 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4841 * to control the bottom and right borders [with the border being
4842 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4843 * is shared evenly on both sides of the button.
4844 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4846 static LRESULT
4847 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4849 DWORD oldPad;
4851 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4852 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4853 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4854 TRACE("cx %ld, cy %ld\n", infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4855 return (LRESULT) oldPad;
4859 static LRESULT
4860 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4862 HWND hwndOldNotify;
4864 TRACE("\n");
4866 hwndOldNotify = infoPtr->hwndNotify;
4867 infoPtr->hwndNotify = hParent;
4869 return (LRESULT)hwndOldNotify;
4873 static LRESULT
4874 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4876 int rows = LOWORD(wParam);
4877 BOOL bLarger = HIWORD(wParam);
4879 TRACE("\n");
4881 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4883 if(infoPtr->nRows != rows)
4885 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4886 int curColumn = 0; /* Current column */
4887 int curRow = 0; /* Current row */
4888 int hidden = 0; /* Number of hidden buttons */
4889 int seps = 0; /* Number of separators */
4890 int idealWrap = 0; /* Ideal wrap point */
4891 int i;
4892 BOOL wrap;
4895 Calculate new size and wrap points - Under windows, setrows will
4896 change the dimensions if needed to show the number of requested
4897 rows (if CCS_NORESIZE is set), or will take up the whole window
4898 (if no CCS_NORESIZE).
4900 Basic algorithm - If N buttons, and y rows requested, each row
4901 contains N/y buttons.
4903 FIXME: Handling of separators not obvious from testing results
4904 FIXME: Take width of window into account?
4907 /* Loop through the buttons one by one counting key items */
4908 for (i = 0; i < infoPtr->nNumButtons; i++ )
4910 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4911 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4912 hidden++;
4913 else if (btnPtr[i].fsStyle & BTNS_SEP)
4914 seps++;
4917 /* FIXME: Separators make this quite complex */
4918 if (seps) FIXME("Separators unhandled\n");
4920 /* Round up so more per line, i.e., less rows */
4921 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4923 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4924 achieve the requested number of rows. */
4925 if (bLarger && idealWrap > 1)
4927 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4928 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4930 if (resRows < rows && moreRows > rows)
4932 idealWrap--;
4933 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4937 curColumn = curRow = 0;
4938 wrap = FALSE;
4939 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4940 infoPtr->nNumButtons, hidden, rows);
4942 for (i = 0; i < infoPtr->nNumButtons; i++ )
4944 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4945 continue;
4947 /* Step on, wrap if necessary or flag next to wrap */
4948 if (!wrap) {
4949 curColumn++;
4950 } else {
4951 wrap = FALSE;
4952 curColumn = 1;
4953 curRow++;
4956 if (curColumn > (idealWrap-1)) {
4957 wrap = TRUE;
4958 btnPtr[i].fsState |= TBSTATE_WRAP;
4962 TRACE("Result - %d rows\n", curRow + 1);
4964 /* recalculate toolbar */
4965 TOOLBAR_CalcToolbar (infoPtr);
4967 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4968 if NORESIZE is NOT set, then the toolbar will always be resized to
4969 take up the whole window. With it set, sizing needs to be manual. */
4970 if (infoPtr->dwStyle & CCS_NORESIZE) {
4971 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4972 infoPtr->rcBound.right - infoPtr->rcBound.left,
4973 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4974 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4977 /* repaint toolbar */
4978 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4981 /* return bounding rectangle */
4982 if (lprc) {
4983 lprc->left = infoPtr->rcBound.left;
4984 lprc->right = infoPtr->rcBound.right;
4985 lprc->top = infoPtr->rcBound.top;
4986 lprc->bottom = infoPtr->rcBound.bottom;
4989 return 0;
4993 static LRESULT
4994 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4996 TBUTTON_INFO *btnPtr;
4997 INT nIndex;
4999 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
5000 if (nIndex == -1)
5001 return FALSE;
5003 btnPtr = &infoPtr->buttons[nIndex];
5005 /* if hidden state has changed the invalidate entire window and recalc */
5006 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
5007 btnPtr->fsState = LOWORD(lParam);
5008 TOOLBAR_CalcToolbar (infoPtr);
5009 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
5010 return TRUE;
5013 /* process state changing if current state doesn't match new state */
5014 if(btnPtr->fsState != LOWORD(lParam))
5016 btnPtr->fsState = LOWORD(lParam);
5017 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5020 return TRUE;
5023 static inline void unwrap(TOOLBAR_INFO *info)
5025 int i;
5027 for (i = 0; i < info->nNumButtons; i++)
5028 info->buttons[i].fsState &= ~TBSTATE_WRAP;
5031 static LRESULT
5032 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
5034 DWORD dwOldStyle = infoPtr->dwStyle;
5036 TRACE("new style %#lx\n", style);
5038 if (style & TBSTYLE_LIST)
5039 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
5040 else
5041 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
5043 infoPtr->dwStyle = style;
5044 TOOLBAR_CheckStyle(infoPtr);
5046 if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5048 if (dwOldStyle & TBSTYLE_WRAPABLE)
5049 unwrap(infoPtr);
5050 TOOLBAR_CalcToolbar(infoPtr);
5052 else if ((dwOldStyle ^ style) & CCS_VERT)
5053 TOOLBAR_LayoutToolbar(infoPtr);
5055 /* only resize if one of the CCS_* styles was changed */
5056 if ((dwOldStyle ^ style) & COMMON_STYLES)
5058 TOOLBAR_AutoSize(infoPtr);
5059 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5062 return 0;
5066 static inline LRESULT
5067 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
5069 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5071 infoPtr->hwndToolTip = hwndTooltip;
5072 return 0;
5076 static LRESULT
5077 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
5079 BOOL bTemp;
5081 TRACE("%s hwnd=%p\n",
5082 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
5084 bTemp = infoPtr->bUnicode;
5085 infoPtr->bUnicode = (BOOL)wParam;
5087 return bTemp;
5091 static LRESULT
5092 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5094 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5095 comctl32_color.clrBtnHighlight :
5096 infoPtr->clrBtnHighlight;
5097 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5098 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5099 return 1;
5103 static LRESULT
5104 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5106 TRACE("new colors Hl=%#lx Shd=%#lx, old colors Hl=%#lx Shd=%#lx\n",
5107 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5108 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5110 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5111 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5112 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5113 return 0;
5117 static LRESULT
5118 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5120 INT iOldVersion = infoPtr->iVersion;
5122 infoPtr->iVersion = iVersion;
5124 if (infoPtr->iVersion >= 5)
5125 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5127 return iOldVersion;
5131 static LRESULT
5132 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
5134 WORD iString = HIWORD(wParam);
5135 WORD buffersize = LOWORD(wParam);
5136 LRESULT ret = -1;
5138 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
5140 if (iString < infoPtr->nNumStrings)
5142 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5143 ret--;
5145 TRACE("returning %s\n", debugstr_a(str));
5147 else
5148 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5150 return ret;
5154 static LRESULT
5155 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5157 WORD iString = HIWORD(wParam);
5158 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5159 LRESULT ret = -1;
5161 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5163 if (iString < infoPtr->nNumStrings)
5165 len = min(len, lstrlenW(infoPtr->strings[iString]));
5166 ret = (len+1)*sizeof(WCHAR);
5167 if (str)
5169 memcpy(str, infoPtr->strings[iString], ret);
5170 str[len] = '\0';
5172 ret = len;
5174 TRACE("returning %s\n", debugstr_w(str));
5176 else
5177 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5179 return ret;
5182 static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5184 SIZE * pSize = (SIZE*)lParam;
5185 FIXME("hwnd=%p, wParam=%Ix, size.cx=%ld, size.cy=%ld stub\n", hwnd, wParam, pSize->cx, pSize->cy);
5186 return 0;
5189 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5190 * caller to specify a reason why the hot item changed (rather than just the
5191 * HICF_OTHER that TB_SETHOTITEM sends). */
5192 static LRESULT
5193 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5195 INT nOldHotItem = infoPtr->nHotItem;
5197 TRACE("old item %d, new item %d, flags %Ix\n", nOldHotItem, nHotItem, lParam);
5199 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5200 nHotItem = -1;
5202 /* NOTE: an application can still remove the hot item even if anchor
5203 * highlighting is enabled */
5205 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5207 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5210 /* Sets the toolbar global iListGap parameter which controls the amount of
5211 * spacing between the image and the text of buttons for TBSTYLE_LIST
5212 * toolbars. */
5213 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5215 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5217 infoPtr->iListGap = iListGap;
5219 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5221 return 0;
5224 /* Returns the number of maximum number of image lists associated with the
5225 * various states. */
5226 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5228 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5230 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5233 static LRESULT
5234 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5236 LPSIZE lpsize = (LPSIZE)lParam;
5238 if (lpsize == NULL)
5239 return FALSE;
5242 * Testing shows the following:
5243 * wParam = 0 adjust cx value
5244 * = 1 set cy value to max size.
5245 * lParam pointer to SIZE structure
5248 TRACE("wParam %Id, lParam %Ix -> %lx, %lx\n", wParam, lParam, lpsize->cx, lpsize->cy);
5250 switch(wParam) {
5251 case 0:
5252 if (lpsize->cx == -1) {
5253 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5255 else if(HIWORD(lpsize->cx)) {
5256 RECT rc;
5257 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5259 GetWindowRect(infoPtr->hwndSelf, &rc);
5260 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5261 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5262 lpsize->cx = max(rc.right-rc.left,
5263 infoPtr->rcBound.right - infoPtr->rcBound.left);
5265 else {
5266 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5268 break;
5269 case 1:
5270 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5271 break;
5272 default:
5273 FIXME("Unknown wParam %Id\n", wParam);
5274 return 0;
5276 TRACE("set to -> %lu, %lu\n", lpsize->cx, lpsize->cy);
5277 return 1;
5280 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5282 FIXME("hwnd %p, wParam %Ix, lParam %Ix\n", hwnd, wParam, lParam);
5284 InvalidateRect(hwnd, NULL, TRUE);
5285 return 1;
5289 static LRESULT
5290 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5292 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5293 LOGFONTW logFont;
5295 TRACE("hwnd = %p, style = %#lx\n", hwnd, lpcs->style);
5297 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5298 GetClientRect(hwnd, &infoPtr->client_rect);
5299 infoPtr->bUnicode = infoPtr->hwndNotify &&
5300 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5301 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5303 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5304 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5305 infoPtr->hTheme = OpenThemeDataForDpi (NULL, themeClass, GetDpiForWindow (hwnd));
5307 TOOLBAR_CheckStyle (infoPtr);
5309 return 0;
5313 static LRESULT
5314 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5316 INT i;
5318 /* delete tooltip control */
5319 if (infoPtr->hwndToolTip)
5320 DestroyWindow (infoPtr->hwndToolTip);
5322 /* delete temporary buffer for tooltip text */
5323 Free (infoPtr->pszTooltipText);
5324 Free (infoPtr->bitmaps); /* bitmaps list */
5326 /* delete button data */
5327 for (i = 0; i < infoPtr->nNumButtons; i++)
5328 free_string( infoPtr->buttons + i );
5329 Free (infoPtr->buttons);
5331 /* delete strings */
5332 if (infoPtr->strings) {
5333 for (i = 0; i < infoPtr->nNumStrings; i++)
5334 Free (infoPtr->strings[i]);
5336 Free (infoPtr->strings);
5339 /* destroy internal image list */
5340 if (infoPtr->himlInt)
5341 ImageList_Destroy (infoPtr->himlInt);
5343 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5344 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5345 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5347 /* delete default font */
5348 DeleteObject (infoPtr->hDefaultFont);
5350 CloseThemeData (infoPtr->hTheme);
5352 /* free toolbar info data */
5353 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5354 Free (infoPtr);
5356 return 0;
5360 static LRESULT
5361 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5363 NMTBCUSTOMDRAW tbcd;
5364 INT ret = FALSE;
5365 DWORD ntfret;
5366 DWORD dwEraseCustDraw = 0;
5368 /* the app has told us not to redraw the toolbar */
5369 if (!infoPtr->bDoRedraw)
5370 return FALSE;
5372 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5373 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5374 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5375 tbcd.nmcd.hdc = (HDC)wParam;
5376 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5377 dwEraseCustDraw = ntfret & 0xffff;
5379 /* FIXME: in general the return flags *can* be or'ed together */
5380 switch (dwEraseCustDraw)
5382 case CDRF_DODEFAULT:
5383 break;
5384 case CDRF_SKIPDEFAULT:
5385 return TRUE;
5386 default:
5387 FIXME("[%p] response %ld not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5388 infoPtr->hwndSelf, ntfret);
5392 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5393 * to my parent for processing.
5395 if (infoPtr->hTheme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5396 POINT pt, ptorig;
5397 HDC hdc = (HDC)wParam;
5398 HWND parent;
5400 pt.x = 0;
5401 pt.y = 0;
5402 parent = GetParent(infoPtr->hwndSelf);
5403 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5404 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5405 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5406 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5408 if (!ret)
5409 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5411 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5412 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5413 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5414 tbcd.nmcd.hdc = (HDC)wParam;
5415 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5416 dwEraseCustDraw = ntfret & 0xffff;
5417 switch (dwEraseCustDraw)
5419 case CDRF_DODEFAULT:
5420 break;
5421 case CDRF_SKIPDEFAULT:
5422 return TRUE;
5423 default:
5424 FIXME("[%p] response %ld not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5425 infoPtr->hwndSelf, ntfret);
5428 return ret;
5432 static inline LRESULT
5433 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5435 return (LRESULT)infoPtr->hFont;
5439 static void
5440 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5442 INT i;
5443 INT nNewHotItem = infoPtr->nHotItem;
5445 for (i = 0; i < infoPtr->nNumButtons; i++)
5447 /* did we wrap? */
5448 if ((nNewHotItem + iDirection < 0) ||
5449 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5451 NMTBWRAPHOTITEM nmtbwhi;
5452 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5453 nmtbwhi.iDirection = iDirection;
5454 nmtbwhi.dwReason = dwReason;
5456 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5457 return;
5460 nNewHotItem += iDirection;
5461 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5463 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5464 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5466 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5467 break;
5472 static LRESULT
5473 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5475 NMKEY nmkey;
5477 nmkey.nVKey = (UINT)wParam;
5478 nmkey.uFlags = HIWORD(lParam);
5480 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5481 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5483 switch ((UINT)wParam)
5485 case VK_LEFT:
5486 case VK_UP:
5487 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5488 break;
5489 case VK_RIGHT:
5490 case VK_DOWN:
5491 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5492 break;
5493 case VK_SPACE:
5494 case VK_RETURN:
5495 if ((infoPtr->nHotItem >= 0) &&
5496 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5498 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5499 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5500 (LPARAM)infoPtr->hwndSelf);
5502 break;
5505 return 0;
5509 static LRESULT
5510 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5512 POINT pt;
5513 BOOL button;
5515 pt.x = (short)LOWORD(lParam);
5516 pt.y = (short)HIWORD(lParam);
5517 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5519 if (button)
5520 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5521 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5522 TOOLBAR_Customize (infoPtr);
5524 return 0;
5528 static LRESULT
5529 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5531 TBUTTON_INFO *btnPtr;
5532 POINT pt;
5533 INT nHit;
5534 NMTOOLBARA nmtb;
5535 NMMOUSE nmmouse;
5536 BOOL bDragKeyPressed;
5537 BOOL button;
5539 TRACE("\n");
5541 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5542 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5543 else
5544 bDragKeyPressed = (wParam & MK_SHIFT);
5546 if (infoPtr->hwndToolTip)
5547 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5548 WM_LBUTTONDOWN, wParam, lParam);
5550 pt.x = (short)LOWORD(lParam);
5551 pt.y = (short)HIWORD(lParam);
5552 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5554 if (button)
5556 btnPtr = &infoPtr->buttons[nHit];
5558 if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5560 infoPtr->nButtonDrag = nHit;
5561 SetCapture (infoPtr->hwndSelf);
5563 /* If drag cursor has not been loaded, load it.
5564 * Note: it doesn't need to be freed */
5565 if (!hCursorDrag)
5566 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5567 SetCursor(hCursorDrag);
5569 else
5571 RECT arrowRect;
5572 infoPtr->nOldHit = nHit;
5574 arrowRect = btnPtr->rect;
5575 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5577 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5578 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5579 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5580 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5581 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5582 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5584 LRESULT res;
5586 /* draw in pressed state */
5587 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5588 btnPtr->fsState |= TBSTATE_PRESSED;
5589 else
5590 btnPtr->bDropDownPressed = TRUE;
5591 RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5593 memset(&nmtb, 0, sizeof(nmtb));
5594 nmtb.iItem = btnPtr->idCommand;
5595 nmtb.rcButton = btnPtr->rect;
5596 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5597 TRACE("TBN_DROPDOWN responded with %Id\n", res);
5599 if (res != TBDDRET_TREATPRESSED)
5601 MSG msg;
5603 /* redraw button in unpressed state */
5604 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5605 btnPtr->fsState &= ~TBSTATE_PRESSED;
5606 else
5607 btnPtr->bDropDownPressed = FALSE;
5608 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5610 /* find and set hot item */
5611 GetCursorPos(&pt);
5612 ScreenToClient(infoPtr->hwndSelf, &pt);
5613 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5614 if (!infoPtr->bAnchor || button)
5615 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5617 /* remove any left mouse button down or double-click messages
5618 * so that we can get a toggle effect on the button */
5619 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5620 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5623 return 0;
5625 /* otherwise drop through and process as pushed */
5627 infoPtr->bCaptured = TRUE;
5628 infoPtr->nButtonDown = nHit;
5629 infoPtr->bDragOutSent = FALSE;
5631 btnPtr->fsState |= TBSTATE_PRESSED;
5633 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5635 if (btnPtr->fsState & TBSTATE_ENABLED)
5636 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5637 UpdateWindow(infoPtr->hwndSelf);
5638 SetCapture (infoPtr->hwndSelf);
5641 memset(&nmtb, 0, sizeof(nmtb));
5642 nmtb.iItem = btnPtr->idCommand;
5643 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5646 nmmouse.dwHitInfo = nHit;
5648 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5649 if (!button)
5650 nmmouse.dwItemSpec = -1;
5651 else
5653 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5654 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5657 ClientToScreen(infoPtr->hwndSelf, &pt);
5658 nmmouse.pt = pt;
5660 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5661 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5663 return 0;
5666 static LRESULT
5667 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5669 TBUTTON_INFO *btnPtr;
5670 POINT pt;
5671 INT nHit;
5672 INT nOldIndex = -1;
5673 NMHDR hdr;
5674 NMMOUSE nmmouse;
5675 NMTOOLBARA nmtb;
5676 BOOL button;
5678 if (infoPtr->hwndToolTip)
5679 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5680 WM_LBUTTONUP, wParam, lParam);
5682 pt.x = (short)LOWORD(lParam);
5683 pt.y = (short)HIWORD(lParam);
5684 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5686 if (!infoPtr->bAnchor || button)
5687 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5689 if (infoPtr->nButtonDrag >= 0) {
5690 RECT rcClient;
5691 NMHDR hdr;
5693 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5694 ReleaseCapture();
5695 /* reset cursor */
5696 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5698 GetClientRect(infoPtr->hwndSelf, &rcClient);
5699 if (PtInRect(&rcClient, pt))
5701 INT nButton = -1;
5702 if (nHit >= 0)
5703 nButton = nHit;
5704 else if (nHit < -1)
5705 nButton = -nHit;
5706 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5707 nButton = -nHit;
5709 if (nButton == infoPtr->nButtonDrag)
5711 /* if the button is moved slightly left and we have a
5712 * separator there then remove it */
5713 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5715 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5716 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5718 else /* else insert a separator before the dragged button */
5720 TBBUTTON tbb;
5721 memset(&tbb, 0, sizeof(tbb));
5722 tbb.fsStyle = BTNS_SEP;
5723 tbb.iString = -1;
5724 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5727 else
5729 if (nButton == -1)
5731 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5732 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5733 else
5734 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5736 else
5737 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5740 else
5742 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5743 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5746 /* button under cursor changed so need to re-set hot item */
5747 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5748 infoPtr->nButtonDrag = -1;
5750 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5752 else if (infoPtr->nButtonDown >= 0)
5754 BOOL was_clicked = nHit == infoPtr->nButtonDown;
5756 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5757 btnPtr->fsState &= ~TBSTATE_PRESSED;
5759 if (btnPtr->fsStyle & BTNS_CHECK) {
5760 if (btnPtr->fsStyle & BTNS_GROUP) {
5761 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5762 nHit);
5763 if ((nOldIndex != nHit) &&
5764 (nOldIndex != -1))
5765 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5766 btnPtr->fsState |= TBSTATE_CHECKED;
5768 else {
5769 if (btnPtr->fsState & TBSTATE_CHECKED)
5770 btnPtr->fsState &= ~TBSTATE_CHECKED;
5771 else
5772 btnPtr->fsState |= TBSTATE_CHECKED;
5776 if (nOldIndex != -1)
5777 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5780 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5781 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5782 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5784 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5785 ReleaseCapture ();
5786 infoPtr->nButtonDown = -1;
5788 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5789 TOOLBAR_SendNotify (&hdr, infoPtr,
5790 NM_RELEASEDCAPTURE);
5792 memset(&nmtb, 0, sizeof(nmtb));
5793 nmtb.iItem = btnPtr->idCommand;
5794 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5795 TBN_ENDDRAG);
5797 if (was_clicked && btnPtr->fsState & TBSTATE_ENABLED)
5799 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5800 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5802 /* In case we have just been destroyed... */
5803 if(!IsWindow(infoPtr->hwndSelf))
5804 return 0;
5808 /* !!! Undocumented - toolbar at 4.71 level and above sends
5809 * NM_CLICK with the NMMOUSE structure. */
5810 nmmouse.dwHitInfo = nHit;
5812 if (!button)
5813 nmmouse.dwItemSpec = -1;
5814 else
5816 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5817 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5820 ClientToScreen(infoPtr->hwndSelf, &pt);
5821 nmmouse.pt = pt;
5823 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5824 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5826 return 0;
5829 static LRESULT
5830 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5832 INT nHit;
5833 NMMOUSE nmmouse;
5834 POINT pt;
5835 BOOL button;
5837 pt.x = (short)LOWORD(lParam);
5838 pt.y = (short)HIWORD(lParam);
5840 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5841 nmmouse.dwHitInfo = nHit;
5843 if (!button) {
5844 nmmouse.dwItemSpec = -1;
5845 } else {
5846 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5847 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5850 ClientToScreen(infoPtr->hwndSelf, &pt);
5851 nmmouse.pt = pt;
5853 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5854 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5856 return 0;
5859 static LRESULT
5860 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5862 NMHDR nmhdr;
5864 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5865 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5867 return 0;
5870 static LRESULT
5871 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5873 TBUTTON_INFO *btnPtr;
5875 infoPtr->bCaptured = FALSE;
5877 if (infoPtr->nButtonDown >= 0)
5879 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5880 btnPtr->fsState &= ~TBSTATE_PRESSED;
5882 infoPtr->nOldHit = -1;
5884 if (btnPtr->fsState & TBSTATE_ENABLED)
5885 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5887 return 0;
5890 static LRESULT
5891 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5893 /* don't remove hot effects when in anchor highlighting mode or when a
5894 * drop-down button is pressed */
5895 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5897 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5898 if (!hotBtnPtr->bDropDownPressed)
5899 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5902 if (infoPtr->nOldHit < 0)
5903 return TRUE;
5905 /* If the last button we were over is depressed then make it not */
5906 /* depressed and redraw it */
5907 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5909 TBUTTON_INFO *btnPtr;
5910 RECT rc1;
5912 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5914 btnPtr->fsState &= ~TBSTATE_PRESSED;
5916 rc1 = btnPtr->rect;
5917 InflateRect (&rc1, 1, 1);
5918 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5921 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5923 NMTOOLBARW nmt;
5924 ZeroMemory(&nmt, sizeof(nmt));
5925 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5926 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5927 infoPtr->bDragOutSent = TRUE;
5930 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5932 return TRUE;
5935 static LRESULT
5936 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5938 POINT pt;
5939 TRACKMOUSEEVENT trackinfo;
5940 INT nHit;
5941 TBUTTON_INFO *btnPtr;
5942 BOOL button;
5944 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5945 TOOLBAR_TooltipCreateControl(infoPtr);
5947 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme) {
5948 /* fill in the TRACKMOUSEEVENT struct */
5949 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5950 trackinfo.dwFlags = TME_QUERY;
5952 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5953 _TrackMouseEvent(&trackinfo);
5955 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5956 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5957 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5958 trackinfo.hwndTrack = infoPtr->hwndSelf;
5960 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5961 /* and can properly deactivate the hot toolbar button */
5962 _TrackMouseEvent(&trackinfo);
5966 if (infoPtr->hwndToolTip)
5967 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5968 WM_MOUSEMOVE, wParam, lParam);
5970 pt.x = (short)LOWORD(lParam);
5971 pt.y = (short)HIWORD(lParam);
5973 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5975 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || infoPtr->hTheme) && (!infoPtr->bAnchor || button))
5976 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5978 if (infoPtr->nOldHit != nHit)
5980 if (infoPtr->bCaptured)
5982 if (!infoPtr->bDragOutSent)
5984 NMTOOLBARW nmt;
5985 ZeroMemory(&nmt, sizeof(nmt));
5986 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5987 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5988 infoPtr->bDragOutSent = TRUE;
5991 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5992 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5993 btnPtr->fsState &= ~TBSTATE_PRESSED;
5994 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5996 else if (nHit == infoPtr->nButtonDown) {
5997 btnPtr->fsState |= TBSTATE_PRESSED;
5998 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6000 infoPtr->nOldHit = nHit;
6004 return 0;
6008 static inline LRESULT
6009 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
6011 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
6012 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
6013 /* else */
6014 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
6018 static inline LRESULT
6019 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
6021 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6022 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6024 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6028 static LRESULT
6029 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
6031 TOOLBAR_INFO *infoPtr;
6032 DWORD styleadd = 0;
6034 /* allocate memory for info structure */
6035 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6036 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6038 /* paranoid!! */
6039 infoPtr->dwStructSize = sizeof(TBBUTTON);
6040 infoPtr->nRows = 1;
6042 /* initialize info structure */
6043 infoPtr->nButtonWidth = 23;
6044 infoPtr->nButtonHeight = 22;
6045 infoPtr->nBitmapHeight = 16;
6046 infoPtr->nBitmapWidth = 16;
6048 infoPtr->nMaxTextRows = 1;
6049 infoPtr->cxMin = -1;
6050 infoPtr->cxMax = -1;
6051 infoPtr->nNumBitmaps = 0;
6052 infoPtr->nNumStrings = 0;
6054 infoPtr->bCaptured = FALSE;
6055 infoPtr->nButtonDown = -1;
6056 infoPtr->nButtonDrag = -1;
6057 infoPtr->nOldHit = -1;
6058 infoPtr->nHotItem = -1;
6059 infoPtr->hwndNotify = lpcs->hwndParent;
6060 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6061 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
6062 infoPtr->bDragOutSent = FALSE;
6063 infoPtr->iVersion = 0;
6064 infoPtr->hwndSelf = hwnd;
6065 infoPtr->bDoRedraw = TRUE;
6066 infoPtr->clrBtnHighlight = CLR_DEFAULT;
6067 infoPtr->clrBtnShadow = CLR_DEFAULT;
6068 infoPtr->szPadding.cx = DEFPAD_CX;
6069 infoPtr->szPadding.cy = DEFPAD_CY;
6070 infoPtr->iListGap = DEFLISTGAP;
6071 infoPtr->iTopMargin = default_top_margin(infoPtr);
6072 infoPtr->dwStyle = lpcs->style;
6073 infoPtr->tbim.iButton = -1;
6075 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6076 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6077 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6078 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6081 /* I think the code below is a bug, but it is the way that the native
6082 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6083 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6084 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6085 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6086 * Somehow, the only cases of this seem to be MFC programs.
6088 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6089 * does not occur in the WM_STYLECHANGING routine.
6090 * (Guy Albertelli 9/2001)
6093 if ((infoPtr->dwStyle & TBSTYLE_FLAT) && !(lpcs->style & TBSTYLE_TRANSPARENT))
6094 styleadd |= TBSTYLE_TRANSPARENT;
6095 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6096 styleadd |= CCS_TOP; /* default to top */
6097 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6100 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
6104 static LRESULT
6105 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6107 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6108 RECT rcWindow;
6109 HDC hdc;
6111 if (dwStyle & WS_MINIMIZE)
6112 return 0; /* Nothing to do */
6114 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6116 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6117 return 0;
6119 if (!(dwStyle & CCS_NODIVIDER))
6121 GetWindowRect (hwnd, &rcWindow);
6122 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6123 if( dwStyle & WS_BORDER )
6124 InflateRect (&rcWindow, -1, -1);
6125 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6128 ReleaseDC( hwnd, hdc );
6130 return 0;
6134 /* handles requests from the tooltip control on what text to display */
6135 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6137 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6138 NMTTDISPINFOA nmtdi;
6139 unsigned int len;
6140 LRESULT ret;
6142 TRACE("button index = %d\n", index);
6144 Free (infoPtr->pszTooltipText);
6145 infoPtr->pszTooltipText = NULL;
6147 if (index < 0)
6148 return 0;
6150 if (infoPtr->bUnicode)
6152 WCHAR wszBuffer[INFOTIPSIZE+1];
6153 NMTBGETINFOTIPW tbgit;
6155 wszBuffer[0] = '\0';
6156 wszBuffer[INFOTIPSIZE] = '\0';
6158 tbgit.pszText = wszBuffer;
6159 tbgit.cchTextMax = INFOTIPSIZE;
6160 tbgit.iItem = lpnmtdi->hdr.idFrom;
6161 tbgit.lParam = infoPtr->buttons[index].dwData;
6163 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6165 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6167 len = tbgit.pszText ? lstrlenW(tbgit.pszText) : 0;
6168 if (len > ARRAY_SIZE(lpnmtdi->szText) - 1)
6170 /* need to allocate temporary buffer in infoPtr as there
6171 * isn't enough space in buffer passed to us by the
6172 * tooltip control */
6173 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6174 if (infoPtr->pszTooltipText)
6176 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6177 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6178 return 0;
6181 else if (len > 0)
6183 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6184 return 0;
6187 else
6189 CHAR szBuffer[INFOTIPSIZE+1];
6190 NMTBGETINFOTIPA tbgit;
6192 szBuffer[0] = '\0';
6193 szBuffer[INFOTIPSIZE] = '\0';
6195 tbgit.pszText = szBuffer;
6196 tbgit.cchTextMax = INFOTIPSIZE;
6197 tbgit.iItem = lpnmtdi->hdr.idFrom;
6198 tbgit.lParam = infoPtr->buttons[index].dwData;
6200 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6202 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6204 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6205 if (len > ARRAY_SIZE(lpnmtdi->szText))
6207 /* need to allocate temporary buffer in infoPtr as there
6208 * isn't enough space in buffer passed to us by the
6209 * tooltip control */
6210 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6211 if (infoPtr->pszTooltipText)
6213 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6214 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6215 return 0;
6218 else if (tbgit.pszText && tbgit.pszText[0])
6220 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, lpnmtdi->lpszText, ARRAY_SIZE(lpnmtdi->szText));
6221 return 0;
6225 /* if button has text, but it is not shown then automatically
6226 * use that text as tooltip */
6227 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6228 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6230 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6231 len = pszText ? lstrlenW(pszText) : 0;
6233 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6235 if (len > ARRAY_SIZE(lpnmtdi->szText) - 1)
6237 /* need to allocate temporary buffer in infoPtr as there
6238 * isn't enough space in buffer passed to us by the
6239 * tooltip control */
6240 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6241 if (infoPtr->pszTooltipText)
6243 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6244 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6245 return 0;
6248 else if (len > 0)
6250 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6251 return 0;
6255 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6257 /* Last resort, forward TTN_GETDISPINFO to the app:
6259 - NFR_UNICODE gets TTN_GETDISPINFOW, and TTN_GETDISPINFOA if -W returned no text;
6260 - NFR_ANSI gets only TTN_GETDISPINFOA.
6262 if (infoPtr->bUnicode)
6264 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6266 TRACE("TTN_GETDISPINFOW - got string %s\n", debugstr_w(lpnmtdi->lpszText));
6268 if (IS_INTRESOURCE(lpnmtdi->lpszText))
6269 return ret;
6271 if (lpnmtdi->lpszText && *lpnmtdi->lpszText)
6272 return ret;
6275 nmtdi.hdr.hwndFrom = lpnmtdi->hdr.hwndFrom;
6276 nmtdi.hdr.idFrom = lpnmtdi->hdr.idFrom;
6277 nmtdi.hdr.code = TTN_GETDISPINFOA;
6278 nmtdi.lpszText = nmtdi.szText;
6279 nmtdi.szText[0] = 0;
6280 nmtdi.hinst = lpnmtdi->hinst;
6281 nmtdi.uFlags = lpnmtdi->uFlags;
6282 nmtdi.lParam = lpnmtdi->lParam;
6284 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmtdi.hdr.idFrom, (LPARAM)&nmtdi);
6286 TRACE("TTN_GETDISPINFOA - got string %s\n", debugstr_a(nmtdi.lpszText));
6288 lpnmtdi->hinst = nmtdi.hinst;
6289 lpnmtdi->uFlags = nmtdi.uFlags;
6290 lpnmtdi->lParam = nmtdi.lParam;
6292 if (IS_INTRESOURCE(nmtdi.lpszText))
6294 lpnmtdi->lpszText = (WCHAR *)nmtdi.lpszText;
6295 return ret;
6298 if (!nmtdi.lpszText || !*nmtdi.lpszText)
6299 return ret;
6301 len = MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, NULL, 0);
6302 if (len > ARRAY_SIZE(lpnmtdi->szText))
6304 infoPtr->pszTooltipText = Alloc(len * sizeof(WCHAR));
6305 if (infoPtr->pszTooltipText)
6307 MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, infoPtr->pszTooltipText, len);
6308 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6309 return 0;
6312 else
6314 MultiByteToWideChar(CP_ACP, 0, nmtdi.lpszText, -1, lpnmtdi->lpszText, ARRAY_SIZE(nmtdi.szText));
6315 return 0;
6318 return ret;
6322 static inline LRESULT
6323 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6325 switch (lpnmh->code)
6327 case PGN_CALCSIZE:
6329 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6331 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6332 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6333 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6334 lppgc->iWidth);
6336 else {
6337 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6338 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6339 lppgc->iHeight);
6341 return 0;
6344 case PGN_SCROLL:
6346 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6348 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6349 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6350 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6351 lppgs->iScroll, lppgs->iDir);
6352 return 0;
6355 case TTN_GETDISPINFOW:
6356 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6358 case TTN_GETDISPINFOA:
6359 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6360 return 0;
6362 default:
6363 return 0;
6368 static LRESULT
6369 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6371 LRESULT format;
6373 TRACE("wParam %Ix, lParam %Ix\n", wParam, lParam);
6375 if (lParam == NF_QUERY)
6376 return NFR_UNICODE;
6378 if (lParam == NF_REQUERY) {
6379 format = SendMessageW(infoPtr->hwndNotify,
6380 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6381 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6382 ERR("wrong response to WM_NOTIFYFORMAT (%Id), assuming ANSI\n", format);
6383 format = NFR_ANSI;
6385 return format;
6387 return 0;
6391 static LRESULT
6392 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6394 HDC hdc;
6395 PAINTSTRUCT ps;
6397 /* fill ps.rcPaint with a default rect */
6398 ps.rcPaint = infoPtr->rcBound;
6400 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6402 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6404 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6405 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6407 return 0;
6411 static LRESULT
6412 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6414 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6416 /* make first item hot */
6417 if (infoPtr->nNumButtons > 0)
6418 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6420 return 0;
6423 static LRESULT
6424 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6426 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6428 if (hFont == 0)
6429 infoPtr->hFont = infoPtr->hDefaultFont;
6430 else
6431 infoPtr->hFont = hFont;
6433 TOOLBAR_CalcToolbar(infoPtr);
6435 if (Redraw)
6436 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6437 return 1;
6440 static LRESULT
6441 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6442 /*****************************************************
6444 * Function;
6445 * Handles the WM_SETREDRAW message.
6447 * Documentation:
6448 * According to testing V4.71 of COMCTL32 returns the
6449 * *previous* status of the redraw flag (either 0 or 1)
6450 * instead of the MSDN documented value of 0 if handled.
6451 * (For laughs see the "consistency" with same function
6452 * in rebar.)
6454 *****************************************************/
6456 BOOL oldredraw = infoPtr->bDoRedraw;
6458 TRACE("set to %s\n",
6459 (wParam) ? "TRUE" : "FALSE");
6460 infoPtr->bDoRedraw = (BOOL) wParam;
6461 if (wParam) {
6462 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6464 return (oldredraw) ? 1 : 0;
6468 static LRESULT
6469 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6471 TRACE("sizing toolbar\n");
6473 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6475 RECT delta_width, delta_height, client, dummy;
6476 DWORD min_x, max_x, min_y, max_y;
6477 TBUTTON_INFO *btnPtr;
6478 INT i;
6480 GetClientRect(infoPtr->hwndSelf, &client);
6481 if(client.right > infoPtr->client_rect.right)
6483 min_x = infoPtr->client_rect.right;
6484 max_x = client.right;
6486 else
6488 max_x = infoPtr->client_rect.right;
6489 min_x = client.right;
6491 if(client.bottom > infoPtr->client_rect.bottom)
6493 min_y = infoPtr->client_rect.bottom;
6494 max_y = client.bottom;
6496 else
6498 max_y = infoPtr->client_rect.bottom;
6499 min_y = client.bottom;
6502 SetRect(&delta_width, min_x, 0, max_x, min_y);
6503 SetRect(&delta_height, 0, min_y, max_x, max_y);
6505 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6506 btnPtr = infoPtr->buttons;
6507 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6508 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6509 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6510 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6512 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6513 TOOLBAR_AutoSize(infoPtr);
6514 return 0;
6518 static LRESULT
6519 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6521 if (nType == GWL_STYLE)
6522 return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6524 return 0;
6528 static LRESULT
6529 TOOLBAR_SysColorChange (void)
6531 COMCTL32_RefreshSysColors();
6533 return 0;
6537 /* update theme after a WM_THEMECHANGED message */
6538 static LRESULT theme_changed (TOOLBAR_INFO *infoPtr)
6540 CloseThemeData (infoPtr->hTheme);
6541 infoPtr->hTheme = OpenThemeDataForDpi (NULL, themeClass, GetDpiForWindow (infoPtr->hwndSelf));
6542 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
6543 return 0;
6547 static LRESULT WINAPI
6548 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6550 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6552 TRACE("hwnd %p, msg %x, wparam %Ix, lparam %Ix\n", hwnd, uMsg, wParam, lParam);
6554 if (!infoPtr && (uMsg != WM_NCCREATE))
6555 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6557 switch (uMsg)
6559 case TB_ADDBITMAP:
6560 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6562 case TB_ADDBUTTONSA:
6563 case TB_ADDBUTTONSW:
6564 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6565 uMsg == TB_ADDBUTTONSW);
6566 case TB_ADDSTRINGA:
6567 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6569 case TB_ADDSTRINGW:
6570 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6572 case TB_AUTOSIZE:
6573 return TOOLBAR_AutoSize (infoPtr);
6575 case TB_BUTTONCOUNT:
6576 return TOOLBAR_ButtonCount (infoPtr);
6578 case TB_BUTTONSTRUCTSIZE:
6579 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6581 case TB_CHANGEBITMAP:
6582 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6584 case TB_CHECKBUTTON:
6585 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6587 case TB_COMMANDTOINDEX:
6588 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6590 case TB_CUSTOMIZE:
6591 return TOOLBAR_Customize (infoPtr);
6593 case TB_DELETEBUTTON:
6594 return TOOLBAR_DeleteButton (infoPtr, wParam);
6596 case TB_ENABLEBUTTON:
6597 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6599 case TB_GETANCHORHIGHLIGHT:
6600 return TOOLBAR_GetAnchorHighlight (infoPtr);
6602 case TB_GETBITMAP:
6603 return TOOLBAR_GetBitmap (infoPtr, wParam);
6605 case TB_GETBITMAPFLAGS:
6606 return TOOLBAR_GetBitmapFlags ();
6608 case TB_GETBUTTON:
6609 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6611 case TB_GETBUTTONINFOA:
6612 case TB_GETBUTTONINFOW:
6613 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6614 uMsg == TB_GETBUTTONINFOW);
6615 case TB_GETBUTTONSIZE:
6616 return TOOLBAR_GetButtonSize (infoPtr);
6618 case TB_GETBUTTONTEXTA:
6619 case TB_GETBUTTONTEXTW:
6620 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6621 uMsg == TB_GETBUTTONTEXTW);
6623 case TB_GETDISABLEDIMAGELIST:
6624 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6626 case TB_GETEXTENDEDSTYLE:
6627 return TOOLBAR_GetExtendedStyle (infoPtr);
6629 case TB_GETHOTIMAGELIST:
6630 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6632 case TB_GETHOTITEM:
6633 return TOOLBAR_GetHotItem (infoPtr);
6635 case TB_GETIMAGELIST:
6636 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6638 case TB_GETINSERTMARK:
6639 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6641 case TB_GETINSERTMARKCOLOR:
6642 return TOOLBAR_GetInsertMarkColor (infoPtr);
6644 case TB_GETITEMRECT:
6645 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6647 case TB_GETMAXSIZE:
6648 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6650 /* case TB_GETOBJECT: */ /* 4.71 */
6652 case TB_GETPADDING:
6653 return TOOLBAR_GetPadding (infoPtr);
6655 case TB_GETRECT:
6656 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6658 case TB_GETROWS:
6659 return TOOLBAR_GetRows (infoPtr);
6661 case TB_GETSTATE:
6662 return TOOLBAR_GetState (infoPtr, wParam);
6664 case TB_GETSTRINGA:
6665 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6667 case TB_GETSTRINGW:
6668 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6670 case TB_GETSTYLE:
6671 return TOOLBAR_GetStyle (infoPtr);
6673 case TB_GETTEXTROWS:
6674 return TOOLBAR_GetTextRows (infoPtr);
6676 case TB_GETTOOLTIPS:
6677 return TOOLBAR_GetToolTips (infoPtr);
6679 case TB_GETUNICODEFORMAT:
6680 return TOOLBAR_GetUnicodeFormat (infoPtr);
6682 case TB_HIDEBUTTON:
6683 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6685 case TB_HITTEST:
6686 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6688 case TB_INDETERMINATE:
6689 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6691 case TB_INSERTBUTTONA:
6692 case TB_INSERTBUTTONW:
6693 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6694 uMsg == TB_INSERTBUTTONW);
6696 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6698 case TB_ISBUTTONCHECKED:
6699 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6701 case TB_ISBUTTONENABLED:
6702 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6704 case TB_ISBUTTONHIDDEN:
6705 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6707 case TB_ISBUTTONHIGHLIGHTED:
6708 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6710 case TB_ISBUTTONINDETERMINATE:
6711 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6713 case TB_ISBUTTONPRESSED:
6714 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6716 case TB_LOADIMAGES:
6717 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6719 case TB_MAPACCELERATORA:
6720 case TB_MAPACCELERATORW:
6721 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6723 case TB_MARKBUTTON:
6724 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6726 case TB_MOVEBUTTON:
6727 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6729 case TB_PRESSBUTTON:
6730 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6732 case TB_REPLACEBITMAP:
6733 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6735 case TB_SAVERESTOREA:
6736 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6738 case TB_SAVERESTOREW:
6739 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6741 case TB_SETANCHORHIGHLIGHT:
6742 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6744 case TB_SETBITMAPSIZE:
6745 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6747 case TB_SETBUTTONINFOA:
6748 case TB_SETBUTTONINFOW:
6749 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6750 uMsg == TB_SETBUTTONINFOW);
6751 case TB_SETBUTTONSIZE:
6752 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6754 case TB_SETBUTTONWIDTH:
6755 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6757 case TB_SETCMDID:
6758 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6760 case TB_SETDISABLEDIMAGELIST:
6761 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6763 case TB_SETDRAWTEXTFLAGS:
6764 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6766 case TB_SETEXTENDEDSTYLE:
6767 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6769 case TB_SETHOTIMAGELIST:
6770 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6772 case TB_SETHOTITEM:
6773 return TOOLBAR_SetHotItem (infoPtr, wParam);
6775 case TB_SETIMAGELIST:
6776 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6778 case TB_SETINDENT:
6779 return TOOLBAR_SetIndent (infoPtr, wParam);
6781 case TB_SETINSERTMARK:
6782 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6784 case TB_SETINSERTMARKCOLOR:
6785 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6787 case TB_SETMAXTEXTROWS:
6788 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6790 case TB_SETPADDING:
6791 return TOOLBAR_SetPadding (infoPtr, lParam);
6793 case TB_SETPARENT:
6794 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6796 case TB_SETROWS:
6797 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6799 case TB_SETSTATE:
6800 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6802 case TB_SETSTYLE:
6803 return TOOLBAR_SetStyle (infoPtr, lParam);
6805 case TB_SETTOOLTIPS:
6806 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6808 case TB_SETUNICODEFORMAT:
6809 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6811 case TB_SETBOUNDINGSIZE:
6812 return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6814 case TB_SETHOTITEM2:
6815 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6817 case TB_SETLISTGAP:
6818 return TOOLBAR_SetListGap(infoPtr, wParam);
6820 case TB_GETIMAGELISTCOUNT:
6821 return TOOLBAR_GetImageListCount(infoPtr);
6823 case TB_GETIDEALSIZE:
6824 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6826 case TB_UNKWN464:
6827 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6829 /* Common Control Messages */
6831 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6832 case CCM_GETCOLORSCHEME:
6833 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6835 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6836 case CCM_SETCOLORSCHEME:
6837 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6839 case CCM_GETVERSION:
6840 return TOOLBAR_GetVersion (infoPtr);
6842 case CCM_SETVERSION:
6843 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6846 /* case WM_CHAR: */
6848 case WM_CREATE:
6849 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6851 case WM_DESTROY:
6852 return TOOLBAR_Destroy (infoPtr);
6854 case WM_ERASEBKGND:
6855 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6857 case WM_GETFONT:
6858 return TOOLBAR_GetFont (infoPtr);
6860 case WM_KEYDOWN:
6861 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6863 /* case WM_KILLFOCUS: */
6865 case WM_LBUTTONDBLCLK:
6866 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6868 case WM_LBUTTONDOWN:
6869 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6871 case WM_LBUTTONUP:
6872 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6874 case WM_RBUTTONUP:
6875 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6877 case WM_RBUTTONDBLCLK:
6878 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6880 case WM_MOUSEMOVE:
6881 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6883 case WM_MOUSELEAVE:
6884 return TOOLBAR_MouseLeave (infoPtr);
6886 case WM_CAPTURECHANGED:
6887 if (hwnd == (HWND)lParam) return 0;
6888 return TOOLBAR_CaptureChanged(infoPtr);
6890 case WM_NCACTIVATE:
6891 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6893 case WM_NCCALCSIZE:
6894 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6896 case WM_NCCREATE:
6897 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6899 case WM_NCPAINT:
6900 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6902 case WM_NOTIFY:
6903 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6905 case WM_NOTIFYFORMAT:
6906 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6908 case WM_PRINTCLIENT:
6909 case WM_PAINT:
6910 return TOOLBAR_Paint (infoPtr, wParam);
6912 case WM_SETFOCUS:
6913 return TOOLBAR_SetFocus (infoPtr);
6915 case WM_SETFONT:
6916 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6918 case WM_SETREDRAW:
6919 return TOOLBAR_SetRedraw (infoPtr, wParam);
6921 case WM_SIZE:
6922 return TOOLBAR_Size (infoPtr);
6924 case WM_STYLECHANGED:
6925 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6927 case WM_SYSCOLORCHANGE:
6928 return TOOLBAR_SysColorChange ();
6930 case WM_THEMECHANGED:
6931 return theme_changed (infoPtr);
6933 /* case WM_WININICHANGE: */
6935 case WM_CHARTOITEM:
6936 case WM_COMMAND:
6937 case WM_DRAWITEM:
6938 case WM_MEASUREITEM:
6939 case WM_VKEYTOITEM:
6940 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6942 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6943 case PGM_FORWARDMOUSE:
6944 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6946 default:
6947 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6948 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
6949 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6954 VOID
6955 TOOLBAR_Register (void)
6957 WNDCLASSW wndClass;
6959 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6960 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6961 wndClass.lpfnWndProc = ToolbarWindowProc;
6962 wndClass.cbClsExtra = 0;
6963 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6964 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6965 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6966 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6968 RegisterClassW (&wndClass);
6972 VOID
6973 TOOLBAR_Unregister (void)
6975 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6978 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6980 HIMAGELIST himlold;
6981 PIMLENTRY c = NULL;
6983 /* Check if the entry already exists */
6984 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6986 /* Don't add new entry for NULL imagelist */
6987 if (!c && !himl)
6988 return NULL;
6990 /* If this is a new entry we must create it and insert into the array */
6991 if (!c)
6993 PIMLENTRY *pnies;
6995 c = Alloc(sizeof(IMLENTRY));
6996 c->id = id;
6998 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6999 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
7000 pnies[*cies] = c;
7001 (*cies)++;
7003 Free(*pies);
7004 *pies = pnies;
7007 himlold = c->himl;
7008 c->himl = himl;
7010 return himlold;
7014 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
7016 int i;
7018 for (i = 0; i < *cies; i++)
7019 Free((*pies)[i]);
7021 Free(*pies);
7023 *cies = 0;
7024 *pies = NULL;
7028 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
7030 PIMLENTRY c = NULL;
7032 if (pies != NULL)
7034 int i;
7036 for (i = 0; i < cies; i++)
7038 if (pies[i]->id == id)
7040 c = pies[i];
7041 break;
7046 return c;
7050 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
7052 HIMAGELIST himlDef = 0;
7053 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
7055 if (pie)
7056 himlDef = pie->himl;
7058 return himlDef;
7062 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
7064 if (infoPtr->bUnicode)
7065 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
7066 else
7068 CHAR Buffer[256];
7069 NMTOOLBARA nmtba;
7070 BOOL bRet = FALSE;
7072 nmtba.iItem = nmtb->iItem;
7073 nmtba.pszText = Buffer;
7074 nmtba.cchText = 256;
7075 ZeroMemory(nmtba.pszText, nmtba.cchText);
7077 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7079 int ccht = strlen(nmtba.pszText);
7080 if (ccht)
7081 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7082 nmtb->pszText, nmtb->cchText);
7084 nmtb->tbButton = nmtba.tbButton;
7085 bRet = TRUE;
7088 return bRet;
7093 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7095 NMTOOLBARW nmtb;
7097 /* MSDN states that iItem is the index of the button, rather than the
7098 * command ID as used by every other NMTOOLBAR notification */
7099 nmtb.iItem = iItem;
7100 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7102 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);