winex11: Remove support for owner-displayed clipboard formats.
[wine.git] / dlls / comctl32 / toolbar.c
blob422845c5f84e1d86ead61814f306c901668e297d
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 "wine/unicode.h"
80 #include "winnls.h"
81 #include "commctrl.h"
82 #include "comctl32.h"
83 #include "uxtheme.h"
84 #include "vssym32.h"
85 #include "wine/debug.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
89 static HCURSOR hCursorDrag = NULL;
91 typedef struct
93 INT iBitmap;
94 INT idCommand;
95 BYTE fsState;
96 BYTE fsStyle;
97 BOOL bHot;
98 BOOL bDropDownPressed;
99 DWORD_PTR dwData;
100 INT_PTR iString;
101 INT nRow;
102 RECT rect;
103 INT cx; /* manually set size */
104 } TBUTTON_INFO;
106 typedef struct
108 UINT nButtons;
109 HINSTANCE hInst;
110 UINT nID;
111 } TBITMAP_INFO;
113 typedef struct
115 HIMAGELIST himl;
116 INT id;
117 } IMLENTRY, *PIMLENTRY;
119 typedef struct
121 DWORD dwStructSize; /* size of TBBUTTON struct */
122 RECT client_rect;
123 RECT rcBound; /* bounding rectangle */
124 INT nButtonHeight;
125 INT nButtonWidth;
126 INT nBitmapHeight;
127 INT nBitmapWidth;
128 INT nIndent;
129 INT nRows; /* number of button rows */
130 INT nMaxTextRows; /* maximum number of text rows */
131 INT cxMin; /* minimum button width */
132 INT cxMax; /* maximum button width */
133 INT nNumButtons; /* number of buttons */
134 INT nNumBitmaps; /* number of bitmaps */
135 INT nNumStrings; /* number of strings */
136 INT nNumBitmapInfos;
137 INT nButtonDown; /* toolbar button being pressed or -1 if none */
138 INT nButtonDrag; /* toolbar button being dragged or -1 if none */
139 INT nOldHit;
140 INT nHotItem; /* index of the "hot" item */
141 SIZE szPadding; /* padding values around button */
142 INT iTopMargin; /* the top margin */
143 INT iListGap; /* default gap between text and image for toolbar with list style */
144 HFONT hDefaultFont;
145 HFONT hFont; /* text font */
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 ASCII (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[] = { 'T','o','o','l','b','a','r',0 };
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=%08lx, stringid=0x%08lx (%s)\n",
288 tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
289 (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
292 static void
293 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
295 if (TRACE_ON(toolbar)){
296 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
297 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
298 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
299 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
300 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
301 btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
302 wine_dbgstr_rect(&bP->rect));
307 static void
308 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
310 if (TRACE_ON(toolbar)) {
311 INT i;
313 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
314 iP->hwndSelf, line,
315 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
316 iP->nNumStrings, iP->dwStyle);
317 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
318 iP->hwndSelf, line,
319 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
320 (iP->bDoRedraw) ? "TRUE" : "FALSE");
321 for(i=0; i<iP->nNumButtons; i++) {
322 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
327 static inline BOOL
328 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
330 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
333 static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
335 if (!IS_INTRESOURCE( str ) && str != -1)
337 if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;
339 if (unicode)
340 Str_SetPtrW( (WCHAR **)&btn->iString, (WCHAR *)str );
341 else
342 Str_SetPtrAtoW( (WCHAR **)&btn->iString, (char *)str );
344 else
346 if (TOOLBAR_ButtonHasString( btn )) Free( (WCHAR *)btn->iString );
348 btn->iString = str;
352 static void set_stringT( TBUTTON_INFO *btn, const WCHAR *str, BOOL unicode )
354 if (IS_INTRESOURCE( (DWORD_PTR)str ) || (DWORD_PTR)str == -1) return;
355 set_string_index( btn, (DWORD_PTR)str, unicode );
358 static void free_string( TBUTTON_INFO *btn )
360 set_string_index( btn, 0, TRUE );
364 /***********************************************************************
365 * TOOLBAR_CheckStyle
367 * This function validates that the styles set are implemented and
368 * issues FIXMEs warning of possible problems. In a perfect world this
369 * function should be null.
371 static void
372 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
374 if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
375 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
379 static INT
380 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
382 if(!IsWindow(infoPtr->hwndSelf))
383 return 0; /* we have just been destroyed */
385 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
386 nmhdr->hwndFrom = infoPtr->hwndSelf;
387 nmhdr->code = code;
389 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
390 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
392 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
395 /***********************************************************************
396 * TOOLBAR_GetBitmapIndex
398 * This function returns the bitmap index associated with a button.
399 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
400 * is issued to retrieve the index.
402 static INT
403 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
405 INT ret = btnPtr->iBitmap;
407 if (ret == I_IMAGECALLBACK)
409 /* issue TBN_GETDISPINFO */
410 NMTBDISPINFOW nmgd;
412 memset(&nmgd, 0, sizeof(nmgd));
413 nmgd.idCommand = btnPtr->idCommand;
414 nmgd.lParam = btnPtr->dwData;
415 nmgd.dwMask = TBNF_IMAGE;
416 nmgd.iImage = -1;
417 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
418 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
419 if (nmgd.dwMask & TBNF_DI_SETITEM)
420 btnPtr->iBitmap = nmgd.iImage;
421 ret = nmgd.iImage;
422 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
423 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
426 if (ret != I_IMAGENONE)
427 ret = GETIBITMAP(infoPtr, ret);
429 return ret;
433 static BOOL
434 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
436 HIMAGELIST himl;
437 INT id = GETHIMLID(infoPtr, index);
438 INT iBitmap = GETIBITMAP(infoPtr, index);
440 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
441 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
442 (index == I_IMAGECALLBACK))
443 return TRUE;
444 else
445 return FALSE;
449 static inline BOOL
450 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
452 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
453 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
457 /***********************************************************************
458 * TOOLBAR_GetImageListForDrawing
460 * This function validates the bitmap index (including I_IMAGECALLBACK
461 * functionality) and returns the corresponding image list.
463 static HIMAGELIST
464 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
465 IMAGE_LIST_TYPE imagelist, INT * index)
467 HIMAGELIST himl;
469 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
470 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
471 WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
472 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
473 return NULL;
476 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
477 if ((*index == I_IMAGECALLBACK) ||
478 (*index == I_IMAGENONE)) return NULL;
479 ERR("TBN_GETDISPINFO returned invalid index %d\n",
480 *index);
481 return NULL;
484 switch(imagelist)
486 case IMAGE_LIST_DEFAULT:
487 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
488 break;
489 case IMAGE_LIST_HOT:
490 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
491 break;
492 case IMAGE_LIST_DISABLED:
493 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
494 break;
495 default:
496 himl = NULL;
497 FIXME("Shouldn't reach here\n");
500 if (!himl)
501 TRACE("no image list\n");
503 return himl;
507 static void
508 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
510 RECT myrect;
511 COLORREF oldcolor, newcolor;
513 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
514 myrect.right = myrect.left + 1;
515 myrect.top = lpRect->top + 2;
516 myrect.bottom = lpRect->bottom - 2;
518 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
519 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
520 oldcolor = SetBkColor (hdc, newcolor);
521 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
523 myrect.left = myrect.right;
524 myrect.right = myrect.left + 1;
526 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
527 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
528 SetBkColor (hdc, newcolor);
529 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
531 SetBkColor (hdc, oldcolor);
535 /***********************************************************************
536 * TOOLBAR_DrawFlatHorizontalSeparator
538 * This function draws horizontal separator for toolbars having CCS_VERT style.
539 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
540 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
541 * are horizontal as opposed to the vertical separators for not dropdown
542 * type.
544 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
546 static void
547 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
548 const TOOLBAR_INFO *infoPtr)
550 RECT myrect;
551 COLORREF oldcolor, newcolor;
553 myrect.left = lpRect->left;
554 myrect.right = lpRect->right;
555 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
556 myrect.bottom = myrect.top + 1;
558 InflateRect (&myrect, -2, 0);
560 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
562 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
563 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
564 oldcolor = SetBkColor (hdc, newcolor);
565 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
567 myrect.top = myrect.bottom;
568 myrect.bottom = myrect.top + 1;
570 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
571 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
572 SetBkColor (hdc, newcolor);
573 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
575 SetBkColor (hdc, oldcolor);
579 static void
580 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
582 INT x, y;
583 HPEN hPen, hOldPen;
585 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
586 hOldPen = SelectObject ( hdc, hPen );
587 x = left + 2;
588 y = top;
589 MoveToEx (hdc, x, y, NULL);
590 LineTo (hdc, x+5, y++); x++;
591 MoveToEx (hdc, x, y, NULL);
592 LineTo (hdc, x+3, y++); x++;
593 MoveToEx (hdc, x, y, NULL);
594 LineTo (hdc, x+1, y);
595 SelectObject( hdc, hOldPen );
596 DeleteObject( hPen );
600 * Draw the text string for this button.
601 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
602 * is non-zero, so we can simply check himlDef to see if we have
603 * an image list
605 static void
606 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
607 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
609 HDC hdc = tbcd->nmcd.hdc;
610 HFONT hOldFont = 0;
611 COLORREF clrOld = 0;
612 COLORREF clrOldBk = 0;
613 int oldBkMode = 0;
614 UINT state = tbcd->nmcd.uItemState;
616 /* draw text */
617 if (lpText && infoPtr->nMaxTextRows > 0) {
618 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
619 wine_dbgstr_rect(rcText));
621 hOldFont = SelectObject (hdc, infoPtr->hFont);
622 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
623 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
625 else if (state & CDIS_DISABLED) {
626 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
627 OffsetRect (rcText, 1, 1);
628 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
629 SetTextColor (hdc, comctl32_color.clr3dShadow);
630 OffsetRect (rcText, -1, -1);
632 else if (state & CDIS_INDETERMINATE) {
633 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
635 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
636 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
637 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
638 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
640 else {
641 clrOld = SetTextColor (hdc, tbcd->clrText);
644 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
645 SetTextColor (hdc, clrOld);
646 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
648 SetBkColor (hdc, clrOldBk);
649 SetBkMode (hdc, oldBkMode);
651 SelectObject (hdc, hOldFont);
656 static void
657 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
659 HDC hdc = tbcd->nmcd.hdc;
660 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
661 COLORREF clrTextOld;
662 COLORREF clrBkOld;
663 INT cx = lpRect->right - lpRect->left;
664 INT cy = lpRect->bottom - lpRect->top;
665 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
666 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
667 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
668 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
669 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
670 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
671 SetBkColor(hdc, clrBkOld);
672 SetTextColor(hdc, clrTextOld);
673 SelectObject (hdc, hbr);
677 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
679 INT cx, cy;
680 HBITMAP hbmMask, hbmImage;
681 HDC hdcMask, hdcImage;
683 ImageList_GetIconSize(himl, &cx, &cy);
685 /* Create src image */
686 hdcImage = CreateCompatibleDC(hdc);
687 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
688 SelectObject(hdcImage, hbmImage);
689 ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
690 RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
692 /* Create Mask */
693 hdcMask = CreateCompatibleDC(0);
694 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
695 SelectObject(hdcMask, hbmMask);
697 /* Remove the background and all white pixels */
698 ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
699 RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
700 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
701 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
703 /* draw the new mask 'etched' to hdc */
704 SetBkColor(hdc, RGB(255, 255, 255));
705 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
706 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
707 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
708 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
709 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
711 /* Cleanup */
712 DeleteObject(hbmImage);
713 DeleteDC(hdcImage);
714 DeleteObject (hbmMask);
715 DeleteDC(hdcMask);
719 static UINT
720 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
722 UINT retstate = 0;
724 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
725 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
726 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
727 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
728 retstate |= (btnPtr->bHot ) ? CDIS_HOT : 0;
729 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
730 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
731 return retstate;
734 /* draws the image on a toolbar button */
735 static void
736 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
737 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
739 HIMAGELIST himl = NULL;
740 BOOL draw_masked = FALSE;
741 INT index;
742 INT offset = 0;
743 UINT draw_flags = ILD_TRANSPARENT;
745 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
747 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
748 if (!himl)
750 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
751 draw_masked = TRUE;
754 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
755 ((tbcd->nmcd.uItemState & CDIS_HOT)
756 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
758 /* if hot, attempt to draw with hot image list, if fails,
759 use default image list */
760 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
761 if (!himl)
762 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
764 else
765 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
767 if (!himl)
768 return;
770 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
771 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
772 offset = 1;
774 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
775 (tbcd->nmcd.uItemState & CDIS_MARKED))
776 draw_flags |= ILD_BLEND50;
778 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
779 index, himl, left, top, offset);
781 if (draw_masked)
782 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
783 else
784 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
787 /* draws a blank frame for a toolbar button */
788 static void
789 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
791 HDC hdc = tbcd->nmcd.hdc;
792 RECT rc = *rect;
793 /* if the state is disabled or indeterminate then the button
794 * cannot have an interactive look like pressed or hot */
795 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
796 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
797 BOOL pressed_look = !non_interactive_state &&
798 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
799 (tbcd->nmcd.uItemState & CDIS_CHECKED));
801 /* app don't want us to draw any edges */
802 if (dwItemCDFlag & TBCDRF_NOEDGES)
803 return;
805 if (infoPtr->dwStyle & TBSTYLE_FLAT)
807 if (pressed_look)
808 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
809 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
810 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
812 else
814 if (pressed_look)
815 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
816 else
817 DrawEdge (hdc, &rc, EDGE_RAISED,
818 BF_SOFT | BF_RECT | BF_MIDDLE);
822 static void
823 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
825 HDC hdc = tbcd->nmcd.hdc;
826 int offset = 0;
827 BOOL pressed = bDropDownPressed ||
828 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
830 if (infoPtr->dwStyle & TBSTYLE_FLAT)
832 if (pressed)
833 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
834 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
835 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
836 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
837 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
839 else
841 if (pressed)
842 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
843 else
844 DrawEdge (hdc, rcArrow, EDGE_RAISED,
845 BF_SOFT | BF_RECT | BF_MIDDLE);
848 if (pressed)
849 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
851 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
853 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
854 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
856 else
857 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
860 /* draws a complete toolbar button */
861 static void
862 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
864 DWORD dwStyle = infoPtr->dwStyle;
865 BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
866 BOOL drawSepDropDownArrow = hasDropDownArrow &&
867 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
868 RECT rc, rcArrow, rcBitmap, rcText;
869 LPWSTR lpText = NULL;
870 NMTBCUSTOMDRAW tbcd;
871 DWORD ntfret;
872 INT offset;
873 INT oldBkMode;
874 DWORD dwItemCustDraw;
875 DWORD dwItemCDFlag;
876 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
878 rc = btnPtr->rect;
879 rcArrow = rc;
881 /* separator - doesn't send NM_CUSTOMDRAW */
882 if (btnPtr->fsStyle & BTNS_SEP) {
883 if (theme)
885 DrawThemeBackground (theme, hdc,
886 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
887 &rc, NULL);
889 else
890 /* with the FLAT style, iBitmap is the width and has already */
891 /* been taken into consideration in calculating the width */
892 /* so now we need to draw the vertical separator */
893 /* empirical tests show that iBitmap can/will be non-zero */
894 /* when drawing the vertical bar... */
895 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
896 if (dwStyle & CCS_VERT) {
897 RECT rcsep = rc;
898 InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
899 TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
901 else
902 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
904 else if (btnPtr->fsStyle != BTNS_SEP) {
905 FIXME("Draw some kind of separator: fsStyle=%x\n",
906 btnPtr->fsStyle);
908 return;
911 /* get a pointer to the text */
912 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
914 if (hasDropDownArrow)
916 int right;
918 if (dwStyle & TBSTYLE_FLAT)
919 right = max(rc.left, rc.right - DDARROW_WIDTH);
920 else
921 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
923 if (drawSepDropDownArrow)
924 rc.right = right;
926 rcArrow.left = right;
929 /* copy text & bitmap rects after adjusting for drop-down arrow
930 * so that text & bitmap is centered in the rectangle not containing
931 * the arrow */
932 rcText = rc;
933 rcBitmap = rc;
935 /* Center the bitmap horizontally and vertically */
936 if (dwStyle & TBSTYLE_LIST)
938 if (lpText &&
939 infoPtr->nMaxTextRows > 0 &&
940 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
941 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
942 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
943 else
944 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
946 else
947 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
949 rcBitmap.top += infoPtr->szPadding.cy / 2;
951 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
952 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
953 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
954 TRACE("Text=%s\n", debugstr_w(lpText));
955 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
957 /* calculate text position */
958 if (lpText)
960 InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
961 if (dwStyle & TBSTYLE_LIST)
963 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
965 else
967 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
968 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
969 else
970 rcText.top += infoPtr->szPadding.cy/2 + 2;
974 /* Initialize fields in all cases, because we use these later
975 * NOTE: applications can and do alter these to customize their
976 * toolbars */
977 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
978 tbcd.clrText = comctl32_color.clrBtnText;
979 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
980 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
981 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
982 tbcd.clrMark = comctl32_color.clrHighlight;
983 tbcd.clrHighlightHotTrack = 0;
984 tbcd.nStringBkMode = TRANSPARENT;
985 tbcd.nHLStringBkMode = OPAQUE;
986 tbcd.rcText.left = 0;
987 tbcd.rcText.top = 0;
988 tbcd.rcText.right = rcText.right - rc.left;
989 tbcd.rcText.bottom = rcText.bottom - rc.top;
990 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
991 tbcd.nmcd.hdc = hdc;
992 tbcd.nmcd.rc = btnPtr->rect;
993 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
995 /* FIXME: what are these used for? */
996 tbcd.hbrLines = 0;
997 tbcd.hpenLines = 0;
999 /* Issue Item Prepaint notify */
1000 dwItemCustDraw = 0;
1001 dwItemCDFlag = 0;
1002 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1004 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1005 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1006 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1007 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1008 /* reset these fields so the user can't alter the behaviour like native */
1009 tbcd.nmcd.hdc = hdc;
1010 tbcd.nmcd.rc = btnPtr->rect;
1012 dwItemCustDraw = ntfret & 0xffff;
1013 dwItemCDFlag = ntfret & 0xffff0000;
1014 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1015 return;
1016 /* save the only part of the rect that the user can change */
1017 rcText.right = tbcd.rcText.right + rc.left;
1018 rcText.bottom = tbcd.rcText.bottom + rc.top;
1021 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1022 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1023 OffsetRect(&rcText, 1, 1);
1025 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
1026 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1027 TOOLBAR_DrawPattern (&rc, &tbcd);
1029 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
1030 && (tbcd.nmcd.uItemState & CDIS_HOT))
1032 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1034 COLORREF oldclr;
1036 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1037 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1038 if (hasDropDownArrow)
1039 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1040 SetBkColor(hdc, oldclr);
1044 if (theme)
1046 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1047 int stateId = TS_NORMAL;
1049 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1050 stateId = TS_DISABLED;
1051 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1052 stateId = TS_PRESSED;
1053 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1054 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1055 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1056 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1057 stateId = TS_HOT;
1059 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1061 else
1062 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1064 if (drawSepDropDownArrow)
1066 if (theme)
1068 int stateId = TS_NORMAL;
1070 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1071 stateId = TS_DISABLED;
1072 else if (btnPtr->bDropDownPressed || (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_HOT;
1076 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1077 stateId = TS_HOT;
1079 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1080 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1082 else
1083 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1086 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1087 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1088 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1089 SetBkMode (hdc, oldBkMode);
1091 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1093 if (hasDropDownArrow && !drawSepDropDownArrow)
1095 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1097 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1098 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1100 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1102 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1103 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1105 else
1106 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1109 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1111 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1112 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1118 static void
1119 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1121 TBUTTON_INFO *btnPtr;
1122 INT i;
1123 RECT rcTemp, rcClient;
1124 NMTBCUSTOMDRAW tbcd;
1125 DWORD ntfret;
1126 DWORD dwBaseCustDraw;
1128 /* the app has told us not to redraw the toolbar */
1129 if (!infoPtr->bDoRedraw)
1130 return;
1132 /* if imagelist belongs to the app, it can be changed
1133 by the app after setting it */
1134 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1136 infoPtr->nNumBitmaps = 0;
1137 for (i = 0; i < infoPtr->cimlDef; i++)
1138 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1141 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1143 /* change the imagelist icon size if we manage the list and it is necessary */
1144 TOOLBAR_CheckImageListIconSize(infoPtr);
1146 /* Send initial notify */
1147 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1148 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1149 tbcd.nmcd.hdc = hdc;
1150 tbcd.nmcd.rc = ps->rcPaint;
1151 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1152 dwBaseCustDraw = ntfret & 0xffff;
1154 GetClientRect(infoPtr->hwndSelf, &rcClient);
1156 /* redraw necessary buttons */
1157 btnPtr = infoPtr->buttons;
1158 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1160 BOOL bDraw;
1161 if (!RectVisible(hdc, &btnPtr->rect))
1162 continue;
1163 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1165 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1166 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1168 else
1169 bDraw = TRUE;
1170 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1171 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1172 if (bDraw)
1173 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1176 /* draw insert mark if required */
1177 if (infoPtr->tbim.iButton != -1)
1179 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1180 RECT rcInsertMark;
1181 rcInsertMark.top = rcButton.top;
1182 rcInsertMark.bottom = rcButton.bottom;
1183 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1184 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1185 else
1186 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1187 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1190 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1192 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1193 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1194 tbcd.nmcd.hdc = hdc;
1195 tbcd.nmcd.rc = ps->rcPaint;
1196 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1200 /***********************************************************************
1201 * TOOLBAR_MeasureString
1203 * This function gets the width and height of a string in pixels. This
1204 * is done first by using GetTextExtentPoint to get the basic width
1205 * and height. The DrawText is called with DT_CALCRECT to get the exact
1206 * width. The reason is because the text may have more than one "&" (or
1207 * prefix characters as M$ likes to call them). The prefix character
1208 * indicates where the underline goes, except for the string "&&" which
1209 * is reduced to a single "&". GetTextExtentPoint does not process these
1210 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1212 static void
1213 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1214 HDC hdc, LPSIZE lpSize)
1216 RECT myrect;
1218 lpSize->cx = 0;
1219 lpSize->cy = 0;
1221 if (infoPtr->nMaxTextRows > 0 &&
1222 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1223 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1224 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1226 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1228 if(lpText != NULL) {
1229 /* first get size of all the text */
1230 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1232 /* feed above size into the rectangle for DrawText */
1233 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1235 /* Use DrawText to get true size as drawn (less pesky "&") */
1236 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1237 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1238 DT_NOPREFIX : 0));
1240 /* feed back to caller */
1241 lpSize->cx = myrect.right;
1242 lpSize->cy = myrect.bottom;
1246 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1249 /***********************************************************************
1250 * TOOLBAR_CalcStrings
1252 * This function walks through each string and measures it and returns
1253 * the largest height and width to caller.
1255 static void
1256 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1258 TBUTTON_INFO *btnPtr;
1259 INT i;
1260 SIZE sz;
1261 HDC hdc;
1262 HFONT hOldFont;
1264 lpSize->cx = 0;
1265 lpSize->cy = 0;
1267 if (infoPtr->nMaxTextRows == 0)
1268 return;
1270 hdc = GetDC (infoPtr->hwndSelf);
1271 hOldFont = SelectObject (hdc, infoPtr->hFont);
1273 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1275 TEXTMETRICW tm;
1277 GetTextMetricsW(hdc, &tm);
1278 lpSize->cy = tm.tmHeight;
1281 btnPtr = infoPtr->buttons;
1282 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1283 if(TOOLBAR_GetText(infoPtr, btnPtr))
1285 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1286 if (sz.cx > lpSize->cx)
1287 lpSize->cx = sz.cx;
1288 if (sz.cy > lpSize->cy)
1289 lpSize->cy = sz.cy;
1293 SelectObject (hdc, hOldFont);
1294 ReleaseDC (infoPtr->hwndSelf, hdc);
1296 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1299 /***********************************************************************
1300 * TOOLBAR_WrapToolbar
1302 * This function walks through the buttons and separators in the
1303 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1304 * wrapping should occur based on the width of the toolbar window.
1305 * It does *not* calculate button placement itself. That task
1306 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1307 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1308 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1310 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1311 * vertical toolbar lists.
1314 static void
1315 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1317 TBUTTON_INFO *btnPtr;
1318 INT x, cx, i, j, width;
1319 BOOL bButtonWrap;
1321 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1322 /* no layout is necessary. Applications may use this style */
1323 /* to perform their own layout on the toolbar. */
1324 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1325 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1327 btnPtr = infoPtr->buttons;
1328 x = infoPtr->nIndent;
1329 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1331 bButtonWrap = FALSE;
1333 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1334 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1335 infoPtr->nIndent);
1337 for (i = 0; i < infoPtr->nNumButtons; i++ )
1339 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1341 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1342 continue;
1344 if (btnPtr[i].cx > 0)
1345 cx = btnPtr[i].cx;
1346 /* horizontal separators are treated as buttons for width */
1347 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1348 !(infoPtr->dwStyle & CCS_VERT))
1349 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1350 else
1351 cx = infoPtr->nButtonWidth;
1353 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1354 cx += DDARROW_WIDTH;
1356 /* Two or more adjacent separators form a separator group. */
1357 /* The first separator in a group should be wrapped to the */
1358 /* next row if the previous wrapping is on a button. */
1359 if( bButtonWrap &&
1360 (btnPtr[i].fsStyle & BTNS_SEP) &&
1361 (i + 1 < infoPtr->nNumButtons ) &&
1362 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1364 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1365 btnPtr[i].fsState |= TBSTATE_WRAP;
1366 x = infoPtr->nIndent;
1367 i++;
1368 bButtonWrap = FALSE;
1369 continue;
1372 /* The layout makes sure the bitmap is visible, but not the button. */
1373 /* Test added to also wrap after a button that starts a row but */
1374 /* is bigger than the area. - GA 8/01 */
1375 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1376 ((x == infoPtr->nIndent) && (cx > width)))
1378 BOOL bFound = FALSE;
1380 /* If the current button is a separator and not hidden, */
1381 /* go to the next until it reaches a non separator. */
1382 /* Wrap the last separator if it is before a button. */
1383 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1384 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1385 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1386 i < infoPtr->nNumButtons )
1388 i++;
1389 bFound = TRUE;
1392 if( bFound && i < infoPtr->nNumButtons )
1394 i--;
1395 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1396 i, btnPtr[i].fsStyle, x, cx);
1397 btnPtr[i].fsState |= TBSTATE_WRAP;
1398 x = infoPtr->nIndent;
1399 bButtonWrap = FALSE;
1400 continue;
1402 else if ( i >= infoPtr->nNumButtons)
1403 break;
1405 /* If the current button is not a separator, find the last */
1406 /* separator and wrap it. */
1407 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1409 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1410 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1412 bFound = TRUE;
1413 i = j;
1414 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1415 i, btnPtr[i].fsStyle, x, cx);
1416 x = infoPtr->nIndent;
1417 btnPtr[j].fsState |= TBSTATE_WRAP;
1418 bButtonWrap = FALSE;
1419 break;
1423 /* If no separator available for wrapping, wrap one of */
1424 /* non-hidden previous button. */
1425 if (!bFound)
1427 for ( j = i - 1;
1428 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1430 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1431 continue;
1433 bFound = TRUE;
1434 i = j;
1435 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1436 i, btnPtr[i].fsStyle, x, cx);
1437 x = infoPtr->nIndent;
1438 btnPtr[j].fsState |= TBSTATE_WRAP;
1439 bButtonWrap = TRUE;
1440 break;
1444 /* If all above failed, wrap the current button. */
1445 if (!bFound)
1447 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1448 i, btnPtr[i].fsStyle, x, cx);
1449 btnPtr[i].fsState |= TBSTATE_WRAP;
1450 x = infoPtr->nIndent;
1451 if (btnPtr[i].fsStyle & BTNS_SEP )
1452 bButtonWrap = FALSE;
1453 else
1454 bButtonWrap = TRUE;
1457 else {
1458 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1459 i, btnPtr[i].fsStyle, x, cx);
1460 x += cx;
1466 /***********************************************************************
1467 * TOOLBAR_MeasureButton
1469 * Calculates the width and height required for a button. Used in
1470 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1471 * the width of buttons that are autosized.
1473 * Note that it would have been rather elegant to use one piece of code for
1474 * both the laying out of the toolbar and for controlling where button parts
1475 * are drawn, but the native control has inconsistencies between the two that
1476 * prevent this from being effectively. These inconsistencies can be seen as
1477 * artefacts where parts of the button appear outside of the bounding button
1478 * rectangle.
1480 * There are several cases for the calculation of the button dimensions and
1481 * button part positioning:
1483 * List
1484 * ====
1486 * With Bitmap:
1488 * +--------------------------------------------------------+ ^
1489 * | ^ ^ | |
1490 * | | pad.cy / 2 | centered | |
1491 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1492 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1493 * | |<------------>| | | | |
1494 * | +--------------+ +------------+ | |
1495 * |<-------------------------------------->| | |
1496 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1497 * | szText.cx | |
1498 * +--------------------------------------------------------+ -
1499 * <-------------------------------------------------------->
1500 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1502 * Without Bitmap (I_IMAGENONE):
1504 * +-----------------------------------+ ^
1505 * | ^ | |
1506 * | | centered | | LISTPAD_CY +
1507 * | +------------+ | | szText.cy
1508 * | | Text | | |
1509 * | | | | |
1510 * | +------------+ | |
1511 * |<----------------->| | |
1512 * | cxedge |<-----------> | |
1513 * | szText.cx | |
1514 * +-----------------------------------+ -
1515 * <----------------------------------->
1516 * szText.cx + pad.cx
1518 * Without text:
1520 * +--------------------------------------+ ^
1521 * | ^ | |
1522 * | | padding.cy/2 | | DEFPAD_CY +
1523 * | +------------+ | | nBitmapHeight
1524 * | | Bitmap | | |
1525 * | | | | |
1526 * | +------------+ | |
1527 * |<------------------->| | |
1528 * | cxedge + iListGap/2 |<-----------> | |
1529 * | nBitmapWidth | |
1530 * +--------------------------------------+ -
1531 * <-------------------------------------->
1532 * 2*cxedge + nBitmapWidth + iListGap
1534 * Non-List
1535 * ========
1537 * With bitmap:
1539 * +-----------------------------------+ ^
1540 * | ^ | |
1541 * | | pad.cy / 2 | | nBitmapHeight +
1542 * | - | | szText.cy +
1543 * | +------------+ | | DEFPAD_CY + 1
1544 * | centered | Bitmap | | |
1545 * |<----------------->| | | |
1546 * | +------------+ | |
1547 * | ^ | |
1548 * | 1 | | |
1549 * | - | |
1550 * | centered +---------------+ | |
1551 * |<--------------->| Text | | |
1552 * | +---------------+ | |
1553 * +-----------------------------------+ -
1554 * <----------------------------------->
1555 * pad.cx + max(nBitmapWidth, szText.cx)
1557 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1559 * +---------------------------------------+ ^
1560 * | ^ | |
1561 * | | 2 + pad.cy / 2 | |
1562 * | - | | szText.cy +
1563 * | centered +-----------------+ | | pad.cy + 2
1564 * |<--------------->| Text | | |
1565 * | +-----------------+ | |
1566 * | | |
1567 * +---------------------------------------+ -
1568 * <--------------------------------------->
1569 * 2*cxedge + pad.cx + szText.cx
1571 * Without text:
1572 * As for with bitmaps, but with szText.cx zero.
1574 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1575 BOOL bHasBitmap, BOOL bValidImageList)
1577 SIZE sizeButton;
1578 if (infoPtr->dwStyle & TBSTYLE_LIST)
1580 /* set button height from bitmap / text height... */
1581 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1582 sizeString.cy);
1584 /* ... add on the necessary padding */
1585 if (bValidImageList)
1587 if (bHasBitmap)
1588 sizeButton.cy += DEFPAD_CY;
1589 else
1590 sizeButton.cy += LISTPAD_CY;
1592 else
1593 sizeButton.cy += infoPtr->szPadding.cy;
1595 /* calculate button width */
1596 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1597 infoPtr->nBitmapWidth + infoPtr->iListGap;
1598 if (sizeString.cx > 0)
1599 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1602 else
1604 if (bHasBitmap)
1606 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1607 if (sizeString.cy > 0)
1608 sizeButton.cy += 1 + sizeString.cy;
1609 sizeButton.cx = infoPtr->szPadding.cx +
1610 max(sizeString.cx, infoPtr->nBitmapWidth);
1612 else
1614 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1615 NONLIST_NOTEXT_OFFSET;
1616 sizeButton.cx = infoPtr->szPadding.cx +
1617 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1620 return sizeButton;
1624 /***********************************************************************
1625 * TOOLBAR_CalcToolbar
1627 * This function calculates button and separator placement. It first
1628 * calculates the button sizes, gets the toolbar window width and then
1629 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1630 * on. It assigns a new location to each item and sends this location to
1631 * the tooltip window if appropriate. Finally, it updates the rcBound
1632 * rect and calculates the new required toolbar window height.
1634 static void
1635 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1637 SIZE sizeString, sizeButton;
1638 BOOL validImageList = FALSE;
1640 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1642 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1644 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1645 validImageList = TRUE;
1646 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1647 infoPtr->nButtonWidth = sizeButton.cx;
1648 infoPtr->nButtonHeight = sizeButton.cy;
1649 infoPtr->iTopMargin = default_top_margin(infoPtr);
1651 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1652 infoPtr->nButtonWidth = infoPtr->cxMin;
1653 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1654 infoPtr->nButtonWidth = infoPtr->cxMax;
1656 TOOLBAR_LayoutToolbar(infoPtr);
1659 static void
1660 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1662 TBUTTON_INFO *btnPtr;
1663 SIZE sizeButton;
1664 INT i, nRows, nSepRows;
1665 INT x, y, cx, cy;
1666 BOOL bWrap;
1667 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1669 TOOLBAR_WrapToolbar(infoPtr);
1671 x = infoPtr->nIndent;
1672 y = infoPtr->iTopMargin;
1673 cx = infoPtr->nButtonWidth;
1674 cy = infoPtr->nButtonHeight;
1676 nRows = nSepRows = 0;
1678 infoPtr->rcBound.top = y;
1679 infoPtr->rcBound.left = x;
1680 infoPtr->rcBound.bottom = y + cy;
1681 infoPtr->rcBound.right = x;
1683 btnPtr = infoPtr->buttons;
1685 TRACE("cy=%d\n", cy);
1687 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1689 bWrap = FALSE;
1690 if (btnPtr->fsState & TBSTATE_HIDDEN)
1692 SetRectEmpty (&btnPtr->rect);
1693 continue;
1696 cy = infoPtr->nButtonHeight;
1698 if (btnPtr->fsStyle & BTNS_SEP) {
1699 if (infoPtr->dwStyle & CCS_VERT) {
1700 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1701 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1703 else
1704 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1705 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1707 else
1709 if (btnPtr->cx)
1710 cx = btnPtr->cx;
1711 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1713 SIZE sz;
1714 HDC hdc;
1715 HFONT hOldFont;
1717 hdc = GetDC (infoPtr->hwndSelf);
1718 hOldFont = SelectObject (hdc, infoPtr->hFont);
1720 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1722 SelectObject (hdc, hOldFont);
1723 ReleaseDC (infoPtr->hwndSelf, hdc);
1725 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1726 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1727 validImageList);
1728 cx = sizeButton.cx;
1730 else
1731 cx = infoPtr->nButtonWidth;
1733 /* if size has been set manually then don't add on extra space
1734 * for the drop down arrow */
1735 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1736 cx += DDARROW_WIDTH;
1738 if (btnPtr->fsState & TBSTATE_WRAP)
1739 bWrap = TRUE;
1741 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1743 if (infoPtr->rcBound.left > x)
1744 infoPtr->rcBound.left = x;
1745 if (infoPtr->rcBound.right < x + cx)
1746 infoPtr->rcBound.right = x + cx;
1747 if (infoPtr->rcBound.bottom < y + cy)
1748 infoPtr->rcBound.bottom = y + cy;
1750 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1752 /* btnPtr->nRow is zero based. The space between the rows is */
1753 /* also considered as a row. */
1754 btnPtr->nRow = nRows + nSepRows;
1756 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1757 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1758 x, y, x+cx, y+cy);
1760 if( bWrap )
1762 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1763 y += cy;
1764 else
1766 if ( !(infoPtr->dwStyle & CCS_VERT))
1767 y += cy + ( (btnPtr->cx > 0 ) ?
1768 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1769 else
1770 y += cy;
1772 /* nSepRows is used to calculate the extra height following */
1773 /* the last row. */
1774 nSepRows++;
1776 x = infoPtr->nIndent;
1778 /* Increment row number unless this is the last button */
1779 /* and it has Wrap set. */
1780 if (i != infoPtr->nNumButtons-1)
1781 nRows++;
1783 else
1784 x += cx;
1787 /* infoPtr->nRows is the number of rows on the toolbar */
1788 infoPtr->nRows = nRows + nSepRows + 1;
1790 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1794 static INT
1795 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1797 TBUTTON_INFO *btnPtr;
1798 INT i;
1800 if (button)
1801 *button = FALSE;
1803 btnPtr = infoPtr->buttons;
1804 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1805 if (btnPtr->fsState & TBSTATE_HIDDEN)
1806 continue;
1808 if (btnPtr->fsStyle & BTNS_SEP) {
1809 if (PtInRect (&btnPtr->rect, *lpPt)) {
1810 TRACE(" ON SEPARATOR %d!\n", i);
1811 return -i;
1814 else {
1815 if (PtInRect (&btnPtr->rect, *lpPt)) {
1816 TRACE(" ON BUTTON %d!\n", i);
1817 if (button)
1818 *button = TRUE;
1819 return i;
1824 TRACE(" NOWHERE!\n");
1825 return TOOLBAR_NOWHERE;
1829 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1830 static BOOL
1831 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1833 INT nOldButtons, nNewButtons, iButton;
1834 BOOL fHasString = FALSE;
1836 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1837 iIndex = infoPtr->nNumButtons;
1839 nOldButtons = infoPtr->nNumButtons;
1840 nNewButtons = nOldButtons + nAddButtons;
1842 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1843 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1844 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1845 infoPtr->nNumButtons += nAddButtons;
1847 /* insert new buttons data */
1848 for (iButton = 0; iButton < nAddButtons; iButton++) {
1849 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1850 INT_PTR str;
1852 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1854 ZeroMemory(btnPtr, sizeof(*btnPtr));
1856 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1857 btnPtr->idCommand = lpTbb[iButton].idCommand;
1858 btnPtr->fsState = lpTbb[iButton].fsState;
1859 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1860 btnPtr->dwData = lpTbb[iButton].dwData;
1862 if (btnPtr->fsStyle & BTNS_SEP)
1863 str = -1;
1864 else
1865 str = lpTbb[iButton].iString;
1866 set_string_index( btnPtr, str, fUnicode );
1867 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1869 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1872 if (infoPtr->nNumStrings > 0 || fHasString)
1873 TOOLBAR_CalcToolbar(infoPtr);
1874 else
1875 TOOLBAR_LayoutToolbar(infoPtr);
1876 TOOLBAR_AutoSize(infoPtr);
1878 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1879 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1880 return TRUE;
1884 static INT
1885 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1887 TBUTTON_INFO *btnPtr;
1888 INT i;
1890 if (CommandIsIndex) {
1891 TRACE("command is really index command=%d\n", idCommand);
1892 if (idCommand >= infoPtr->nNumButtons) return -1;
1893 return idCommand;
1895 btnPtr = infoPtr->buttons;
1896 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1897 if (btnPtr->idCommand == idCommand) {
1898 TRACE("command=%d index=%d\n", idCommand, i);
1899 return i;
1902 TRACE("no index found for command=%d\n", idCommand);
1903 return -1;
1907 static INT
1908 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1910 TBUTTON_INFO *btnPtr;
1911 INT nRunIndex;
1913 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1914 return -1;
1916 /* check index button */
1917 btnPtr = &infoPtr->buttons[nIndex];
1918 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1919 if (btnPtr->fsState & TBSTATE_CHECKED)
1920 return nIndex;
1923 /* check previous buttons */
1924 nRunIndex = nIndex - 1;
1925 while (nRunIndex >= 0) {
1926 btnPtr = &infoPtr->buttons[nRunIndex];
1927 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1928 if (btnPtr->fsState & TBSTATE_CHECKED)
1929 return nRunIndex;
1931 else
1932 break;
1933 nRunIndex--;
1936 /* check next buttons */
1937 nRunIndex = nIndex + 1;
1938 while (nRunIndex < infoPtr->nNumButtons) {
1939 btnPtr = &infoPtr->buttons[nRunIndex];
1940 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1941 if (btnPtr->fsState & TBSTATE_CHECKED)
1942 return nRunIndex;
1944 else
1945 break;
1946 nRunIndex++;
1949 return -1;
1953 static VOID
1954 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1955 WPARAM wParam, LPARAM lParam)
1957 MSG msg;
1959 msg.hwnd = hwndMsg;
1960 msg.message = uMsg;
1961 msg.wParam = wParam;
1962 msg.lParam = lParam;
1963 msg.time = GetMessageTime ();
1964 msg.pt.x = (short)LOWORD(GetMessagePos ());
1965 msg.pt.y = (short)HIWORD(GetMessagePos ());
1967 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1970 static void
1971 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1973 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1974 TTTOOLINFOW ti;
1976 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1977 ti.cbSize = sizeof (TTTOOLINFOW);
1978 ti.hwnd = infoPtr->hwndSelf;
1979 ti.uId = button->idCommand;
1980 ti.hinst = 0;
1981 ti.lpszText = LPSTR_TEXTCALLBACKW;
1982 /* ti.lParam = random value from the stack? */
1984 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1985 0, (LPARAM)&ti);
1989 static void
1990 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1992 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1993 TTTOOLINFOW ti;
1995 ZeroMemory(&ti, sizeof(ti));
1996 ti.cbSize = sizeof(ti);
1997 ti.hwnd = infoPtr->hwndSelf;
1998 ti.uId = button->idCommand;
2000 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2004 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2006 /* Set the toolTip only for non-hidden, non-separator button */
2007 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2009 TTTOOLINFOW ti;
2011 ZeroMemory(&ti, sizeof(ti));
2012 ti.cbSize = sizeof(ti);
2013 ti.hwnd = infoPtr->hwndSelf;
2014 ti.uId = button->idCommand;
2015 ti.rect = button->rect;
2016 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2020 /* Creates the tooltip control */
2021 static void
2022 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2024 int i;
2025 NMTOOLTIPSCREATED nmttc;
2027 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2028 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2029 infoPtr->hwndSelf, 0, 0, 0);
2031 if (!infoPtr->hwndToolTip)
2032 return;
2034 /* Send NM_TOOLTIPSCREATED notification */
2035 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2036 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2038 for (i = 0; i < infoPtr->nNumButtons; i++)
2040 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2041 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2045 /* keeps available button list box sorted by button id */
2046 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2048 int i;
2049 int count;
2050 PCUSTOMBUTTON btnInfo;
2051 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2053 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2055 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2057 /* position 0 is always separator */
2058 for (i = 1; i < count; i++)
2060 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2061 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2063 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2064 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2065 return;
2068 /* id higher than all others add to end */
2069 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2070 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2073 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2075 NMTOOLBARW nmtb;
2077 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2079 if (nIndexFrom == nIndexTo)
2080 return;
2082 /* MSDN states that iItem is the index of the button, rather than the
2083 * command ID as used by every other NMTOOLBAR notification */
2084 nmtb.iItem = nIndexFrom;
2085 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2087 PCUSTOMBUTTON btnInfo;
2088 NMHDR hdr;
2089 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2090 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2092 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2094 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2095 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2096 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2097 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2099 if (nIndexTo <= 0)
2100 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2101 else
2102 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2104 /* last item is always separator, so -2 instead of -1 */
2105 if (nIndexTo >= (count - 2))
2106 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2107 else
2108 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2110 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2111 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2113 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2117 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2119 NMTOOLBARW nmtb;
2121 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2123 /* MSDN states that iItem is the index of the button, rather than the
2124 * command ID as used by every other NMTOOLBAR notification */
2125 nmtb.iItem = nIndexAvail;
2126 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2128 PCUSTOMBUTTON btnInfo;
2129 NMHDR hdr;
2130 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2131 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2132 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2134 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2136 if (nIndexAvail != 0) /* index == 0 indicates separator */
2138 /* remove from 'available buttons' list */
2139 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2140 if (nIndexAvail == count-1)
2141 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2142 else
2143 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2145 else
2147 PCUSTOMBUTTON btnNew;
2149 /* duplicate 'separator' button */
2150 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2151 *btnNew = *btnInfo;
2152 btnInfo = btnNew;
2155 /* insert into 'toolbar button' list */
2156 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2157 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2159 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2161 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2165 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2167 PCUSTOMBUTTON btnInfo;
2168 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2170 TRACE("Remove: index %d\n", index);
2172 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2174 /* send TBN_QUERYDELETE notification */
2175 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2177 NMHDR hdr;
2179 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2180 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2182 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2184 /* insert into 'available button' list */
2185 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2186 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2187 else
2188 Free(btnInfo);
2190 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2194 /* drag list notification function for toolbar buttons list box */
2195 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2196 const DRAGLISTINFO *pDLI)
2198 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2199 switch (pDLI->uNotification)
2201 case DL_BEGINDRAG:
2203 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2204 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2205 /* no dragging for last item (separator) */
2206 if (nCurrentItem >= (nCount - 1)) return FALSE;
2207 return TRUE;
2209 case DL_DRAGGING:
2211 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2212 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2213 /* no dragging past last item (separator) */
2214 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2216 DrawInsert(hwnd, hwndList, nCurrentItem);
2217 /* FIXME: native uses "move button" cursor */
2218 return DL_COPYCURSOR;
2221 /* not over toolbar buttons list */
2222 if (nCurrentItem < 0)
2224 POINT ptWindow = pDLI->ptCursor;
2225 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2226 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2227 /* over available buttons list? */
2228 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2229 /* FIXME: native uses "move button" cursor */
2230 return DL_COPYCURSOR;
2232 /* clear drag arrow */
2233 DrawInsert(hwnd, hwndList, -1);
2234 return DL_STOPCURSOR;
2236 case DL_DROPPED:
2238 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2239 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2240 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2241 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2243 /* clear drag arrow */
2244 DrawInsert(hwnd, hwndList, -1);
2245 /* move item */
2246 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2248 /* not over toolbar buttons list */
2249 if (nIndexTo < 0)
2251 POINT ptWindow = pDLI->ptCursor;
2252 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2253 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2254 /* over available buttons list? */
2255 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2256 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2258 break;
2260 case DL_CANCELDRAG:
2261 /* Clear drag arrow */
2262 DrawInsert(hwnd, hwndList, -1);
2263 break;
2266 return 0;
2269 /* drag list notification function for available buttons list box */
2270 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2271 const DRAGLISTINFO *pDLI)
2273 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2274 switch (pDLI->uNotification)
2276 case DL_BEGINDRAG:
2277 return TRUE;
2278 case DL_DRAGGING:
2280 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2281 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2282 /* no dragging past last item (separator) */
2283 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2285 DrawInsert(hwnd, hwndList, nCurrentItem);
2286 /* FIXME: native uses "move button" cursor */
2287 return DL_COPYCURSOR;
2290 /* not over toolbar buttons list */
2291 if (nCurrentItem < 0)
2293 POINT ptWindow = pDLI->ptCursor;
2294 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2295 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2296 /* over available buttons list? */
2297 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2298 /* FIXME: native uses "move button" cursor */
2299 return DL_COPYCURSOR;
2301 /* clear drag arrow */
2302 DrawInsert(hwnd, hwndList, -1);
2303 return DL_STOPCURSOR;
2305 case DL_DROPPED:
2307 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2308 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2309 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2310 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2312 /* clear drag arrow */
2313 DrawInsert(hwnd, hwndList, -1);
2314 /* add item */
2315 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2318 case DL_CANCELDRAG:
2319 /* Clear drag arrow */
2320 DrawInsert(hwnd, hwndList, -1);
2321 break;
2323 return 0;
2326 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2328 /***********************************************************************
2329 * TOOLBAR_CustomizeDialogProc
2330 * This function implements the toolbar customization dialog.
2332 static INT_PTR CALLBACK
2333 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2335 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2336 PCUSTOMBUTTON btnInfo;
2337 NMTOOLBARA nmtb;
2338 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2340 switch (uMsg)
2342 case WM_INITDIALOG:
2343 custInfo = (PCUSTDLG_INFO)lParam;
2344 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2346 if (custInfo)
2348 WCHAR Buffer[256];
2349 int i = 0;
2350 int index;
2351 NMTBINITCUSTOMIZE nmtbic;
2353 infoPtr = custInfo->tbInfo;
2355 /* send TBN_QUERYINSERT notification */
2356 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2358 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2359 return FALSE;
2361 nmtbic.hwndDialog = hwnd;
2362 /* Send TBN_INITCUSTOMIZE notification */
2363 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2364 TBNRF_HIDEHELP)
2366 TRACE("TBNRF_HIDEHELP requested\n");
2367 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2370 /* add items to 'toolbar buttons' list and check if removable */
2371 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2373 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2374 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2375 btnInfo->btn.fsStyle = BTNS_SEP;
2376 btnInfo->bVirtual = FALSE;
2377 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2379 /* send TBN_QUERYDELETE notification */
2380 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2382 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2383 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2386 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2388 /* insert separator button into 'available buttons' list */
2389 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2390 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2391 btnInfo->btn.fsStyle = BTNS_SEP;
2392 btnInfo->bVirtual = FALSE;
2393 btnInfo->bRemovable = TRUE;
2394 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2395 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2396 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2398 /* insert all buttons into dsa */
2399 for (i = 0;; i++)
2401 /* send TBN_GETBUTTONINFO notification */
2402 NMTOOLBARW nmtb;
2403 nmtb.iItem = i;
2404 nmtb.pszText = Buffer;
2405 nmtb.cchText = 256;
2407 /* Clear previous button's text */
2408 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2410 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2411 break;
2413 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2414 nmtb.tbButton.fsStyle, i,
2415 nmtb.tbButton.idCommand,
2416 nmtb.tbButton.iString,
2417 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2418 : "");
2420 /* insert button into the appropriate list */
2421 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2422 if (index == -1)
2424 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2425 btnInfo->bVirtual = FALSE;
2426 btnInfo->bRemovable = TRUE;
2428 else
2430 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2431 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2434 btnInfo->btn = nmtb.tbButton;
2435 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2437 if (lstrlenW(nmtb.pszText))
2438 lstrcpyW(btnInfo->text, nmtb.pszText);
2439 else if (nmtb.tbButton.iString >= 0 &&
2440 nmtb.tbButton.iString < infoPtr->nNumStrings)
2442 lstrcpyW(btnInfo->text,
2443 infoPtr->strings[nmtb.tbButton.iString]);
2447 if (index == -1)
2448 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2451 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2453 /* select first item in the 'available' list */
2454 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2456 /* append 'virtual' separator button to the 'toolbar buttons' list */
2457 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2458 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2459 btnInfo->btn.fsStyle = BTNS_SEP;
2460 btnInfo->bVirtual = TRUE;
2461 btnInfo->bRemovable = FALSE;
2462 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2463 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2464 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2466 /* select last item in the 'toolbar' list */
2467 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2468 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2470 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2471 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2473 /* set focus and disable buttons */
2474 PostMessageW (hwnd, WM_USER, 0, 0);
2476 return TRUE;
2478 case WM_USER:
2479 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2480 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2481 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2482 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2483 return TRUE;
2485 case WM_CLOSE:
2486 EndDialog(hwnd, FALSE);
2487 return TRUE;
2489 case WM_COMMAND:
2490 switch (LOWORD(wParam))
2492 case IDC_TOOLBARBTN_LBOX:
2493 if (HIWORD(wParam) == LBN_SELCHANGE)
2495 PCUSTOMBUTTON btnInfo;
2496 NMTOOLBARA nmtb;
2497 int count;
2498 int index;
2500 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2501 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2503 /* send TBN_QUERYINSERT notification */
2504 nmtb.iItem = index;
2505 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2507 /* get list box item */
2508 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2510 if (index == (count - 1))
2512 /* last item (virtual separator) */
2513 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2514 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2516 else if (index == (count - 2))
2518 /* second last item (last non-virtual item) */
2519 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2520 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2522 else if (index == 0)
2524 /* first item */
2525 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2526 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2528 else
2530 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2531 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2534 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2536 break;
2538 case IDC_MOVEUP_BTN:
2540 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2541 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2543 break;
2545 case IDC_MOVEDN_BTN: /* move down */
2547 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2548 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2550 break;
2552 case IDC_REMOVE_BTN: /* remove button */
2554 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2556 if (LB_ERR == index)
2557 break;
2559 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2561 break;
2562 case IDC_HELP_BTN:
2563 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2564 break;
2565 case IDC_RESET_BTN:
2566 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2567 break;
2569 case IDOK: /* Add button */
2571 int index;
2572 int indexto;
2574 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2575 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2577 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2579 break;
2581 case IDCANCEL:
2582 EndDialog(hwnd, FALSE);
2583 break;
2585 return TRUE;
2587 case WM_DESTROY:
2589 int count;
2590 int i;
2592 /* delete items from 'toolbar buttons' listbox*/
2593 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2594 for (i = 0; i < count; i++)
2596 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2597 Free(btnInfo);
2598 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2600 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2603 /* delete items from 'available buttons' listbox*/
2604 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2605 for (i = 0; i < count; i++)
2607 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2608 Free(btnInfo);
2609 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2611 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2613 return TRUE;
2615 case WM_DRAWITEM:
2616 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2618 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2619 RECT rcButton;
2620 RECT rcText;
2621 HPEN hPen, hOldPen;
2622 HBRUSH hOldBrush;
2623 COLORREF oldText = 0;
2624 COLORREF oldBk = 0;
2626 /* get item data */
2627 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2628 if (btnInfo == NULL)
2630 FIXME("btnInfo invalid!\n");
2631 return TRUE;
2634 /* set colors and select objects */
2635 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2636 if (btnInfo->bVirtual)
2637 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2638 else
2639 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2640 hPen = CreatePen( PS_SOLID, 1,
2641 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2642 hOldPen = SelectObject (lpdis->hDC, hPen );
2643 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2645 /* fill background rectangle */
2646 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2647 lpdis->rcItem.right, lpdis->rcItem.bottom);
2649 /* calculate button and text rectangles */
2650 rcButton = lpdis->rcItem;
2651 InflateRect (&rcButton, -1, -1);
2652 rcText = rcButton;
2653 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2654 rcText.left = rcButton.right + 2;
2656 /* draw focus rectangle */
2657 if (lpdis->itemState & ODS_FOCUS)
2658 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2660 /* draw button */
2661 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2662 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2664 /* draw image and text */
2665 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2666 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2667 btnInfo->btn.iBitmap));
2668 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2669 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2671 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2672 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2674 /* delete objects and reset colors */
2675 SelectObject (lpdis->hDC, hOldBrush);
2676 SelectObject (lpdis->hDC, hOldPen);
2677 SetBkColor (lpdis->hDC, oldBk);
2678 SetTextColor (lpdis->hDC, oldText);
2679 DeleteObject( hPen );
2680 return TRUE;
2682 return FALSE;
2684 case WM_MEASUREITEM:
2685 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2687 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2689 lpmis->itemHeight = 15 + 8; /* default height */
2691 return TRUE;
2693 return FALSE;
2695 default:
2696 if (uDragListMessage && (uMsg == uDragListMessage))
2698 if (wParam == IDC_TOOLBARBTN_LBOX)
2700 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2701 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2702 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2703 return TRUE;
2705 else if (wParam == IDC_AVAILBTN_LBOX)
2707 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2708 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2709 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2710 return TRUE;
2713 return FALSE;
2717 static BOOL
2718 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2720 HBITMAP hbmLoad;
2721 INT nCountBefore = ImageList_GetImageCount(himlDef);
2722 INT nCountAfter;
2723 INT cxIcon, cyIcon;
2724 INT nAdded;
2725 INT nIndex;
2727 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2728 /* Add bitmaps to the default image list */
2729 if (bitmap->hInst == NULL) /* a handle was passed */
2730 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2731 else if (bitmap->hInst == COMCTL32_hModule)
2732 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2733 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2734 else
2735 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2737 /* enlarge the bitmap if needed */
2738 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2739 if (bitmap->hInst != COMCTL32_hModule)
2740 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2742 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2743 DeleteObject(hbmLoad);
2744 if (nIndex == -1)
2745 return FALSE;
2747 nCountAfter = ImageList_GetImageCount(himlDef);
2748 nAdded = nCountAfter - nCountBefore;
2749 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2751 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2752 } else if (nAdded > (INT)bitmap->nButtons) {
2753 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2754 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2757 infoPtr->nNumBitmaps += nAdded;
2758 return TRUE;
2761 static void
2762 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2764 HIMAGELIST himlDef;
2765 HIMAGELIST himlNew;
2766 INT cx, cy;
2767 INT i;
2769 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2770 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2771 return;
2772 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2773 return;
2774 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2775 return;
2777 TRACE("Update icon size: %dx%d -> %dx%d\n",
2778 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2780 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2781 ILC_COLOR32|ILC_MASK, 8, 2);
2782 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2783 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2784 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2785 infoPtr->himlInt = himlNew;
2787 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2788 ImageList_Destroy(himlDef);
2791 /***********************************************************************
2792 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2795 static LRESULT
2796 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2798 TBITMAP_INFO info;
2799 INT iSumButtons, i;
2800 HIMAGELIST himlDef;
2802 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2803 if (!lpAddBmp)
2804 return -1;
2806 if (lpAddBmp->hInst == HINST_COMMCTRL)
2808 info.hInst = COMCTL32_hModule;
2809 switch (lpAddBmp->nID)
2811 case IDB_STD_SMALL_COLOR:
2812 info.nButtons = 15;
2813 info.nID = IDB_STD_SMALL;
2814 break;
2815 case IDB_STD_LARGE_COLOR:
2816 info.nButtons = 15;
2817 info.nID = IDB_STD_LARGE;
2818 break;
2819 case IDB_VIEW_SMALL_COLOR:
2820 info.nButtons = 12;
2821 info.nID = IDB_VIEW_SMALL;
2822 break;
2823 case IDB_VIEW_LARGE_COLOR:
2824 info.nButtons = 12;
2825 info.nID = IDB_VIEW_LARGE;
2826 break;
2827 case IDB_HIST_SMALL_COLOR:
2828 info.nButtons = 5;
2829 info.nID = IDB_HIST_SMALL;
2830 break;
2831 case IDB_HIST_LARGE_COLOR:
2832 info.nButtons = 5;
2833 info.nID = IDB_HIST_LARGE;
2834 break;
2835 default:
2836 return -1;
2839 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2841 /* Windows resize all the buttons to the size of a newly added standard image */
2842 if (lpAddBmp->nID & 1)
2844 /* large icons: 24x24. Will make the button 31x30 */
2845 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2847 else
2849 /* small icons: 16x16. Will make the buttons 23x22 */
2850 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2853 TOOLBAR_CalcToolbar (infoPtr);
2855 else
2857 info.nButtons = count;
2858 info.hInst = lpAddBmp->hInst;
2859 info.nID = lpAddBmp->nID;
2860 TRACE("adding %d bitmaps!\n", info.nButtons);
2863 /* check if the bitmap is already loaded and compute iSumButtons */
2864 iSumButtons = 0;
2865 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2867 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2868 infoPtr->bitmaps[i].nID == info.nID)
2869 return iSumButtons;
2870 iSumButtons += infoPtr->bitmaps[i].nButtons;
2873 if (!infoPtr->cimlDef) {
2874 /* create new default image list */
2875 TRACE ("creating default image list!\n");
2877 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2878 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2879 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2880 infoPtr->himlInt = himlDef;
2882 else {
2883 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2886 if (!himlDef) {
2887 WARN("No default image list available\n");
2888 return -1;
2891 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2892 return -1;
2894 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2895 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2896 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2897 infoPtr->nNumBitmapInfos++;
2898 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2900 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2901 return iSumButtons;
2905 static LRESULT
2906 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2908 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
2910 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2914 static LRESULT
2915 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2917 #define MAX_RESOURCE_STRING_LENGTH 512
2918 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2919 INT nIndex = infoPtr->nNumStrings;
2921 TRACE("%p, %lx\n", hInstance, lParam);
2923 if (IS_INTRESOURCE(lParam)) {
2924 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2925 WCHAR delimiter;
2926 WCHAR *next_delim;
2927 HRSRC hrsrc;
2928 WCHAR *p;
2929 INT len;
2931 TRACE("adding string from resource\n");
2933 if (!hInstance) return -1;
2935 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2936 (LPWSTR)RT_STRING );
2937 if (!hrsrc)
2939 TRACE("string not found in resources\n");
2940 return -1;
2943 len = LoadStringW (hInstance, (UINT)lParam,
2944 szString, MAX_RESOURCE_STRING_LENGTH);
2946 TRACE("len=%d %s\n", len, debugstr_w(szString));
2947 if (len == 0 || len == 1)
2948 return nIndex;
2950 TRACE("delimiter: 0x%x\n", *szString);
2951 delimiter = *szString;
2952 p = szString + 1;
2954 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2955 *next_delim = 0;
2956 if (next_delim + 1 >= szString + len)
2958 /* this may happen if delimiter == '\0' or if the last char is a
2959 * delimiter (then it is ignored like the native does) */
2960 break;
2963 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2964 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2965 infoPtr->nNumStrings++;
2967 p = next_delim + 1;
2970 else {
2971 LPWSTR p = (LPWSTR)lParam;
2972 INT len;
2974 if (p == NULL)
2975 return -1;
2976 TRACE("adding string(s) from array\n");
2977 while (*p) {
2978 len = strlenW (p);
2980 TRACE("len=%d %s\n", len, debugstr_w(p));
2981 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2982 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2983 infoPtr->nNumStrings++;
2985 p += (len+1);
2989 if (fFirstString)
2990 TOOLBAR_CalcToolbar(infoPtr);
2991 return nIndex;
2995 static LRESULT
2996 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2998 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2999 LPSTR p;
3000 INT nIndex;
3001 INT len;
3003 TRACE("%p, %lx\n", hInstance, lParam);
3005 if (IS_INTRESOURCE(lParam)) /* load from resources */
3006 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3008 p = (LPSTR)lParam;
3009 if (p == NULL)
3010 return -1;
3012 TRACE("adding string(s) from array\n");
3013 nIndex = infoPtr->nNumStrings;
3014 while (*p) {
3015 len = strlen (p);
3016 TRACE("len=%d \"%s\"\n", len, p);
3018 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3019 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3020 infoPtr->nNumStrings++;
3022 p += (len+1);
3025 if (fFirstString)
3026 TOOLBAR_CalcToolbar(infoPtr);
3027 return nIndex;
3031 static LRESULT
3032 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3034 TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3035 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3037 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3039 RECT window_rect, parent_rect;
3040 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3041 HWND parent;
3042 INT x, y, cx, cy;
3044 parent = GetParent (infoPtr->hwndSelf);
3046 if (!parent || !infoPtr->bDoRedraw)
3047 return 0;
3049 GetClientRect(parent, &parent_rect);
3051 x = parent_rect.left;
3052 y = parent_rect.top;
3054 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3055 cx = parent_rect.right - parent_rect.left;
3057 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3059 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3060 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3061 y = window_rect.top;
3063 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3065 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3066 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3069 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3070 uPosFlags |= SWP_NOMOVE;
3072 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3073 cy += GetSystemMetrics(SM_CYEDGE);
3075 if (infoPtr->dwStyle & WS_BORDER)
3077 cx += 2 * GetSystemMetrics(SM_CXBORDER);
3078 cy += 2 * GetSystemMetrics(SM_CYBORDER);
3081 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3084 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3086 TOOLBAR_LayoutToolbar(infoPtr);
3087 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3090 return 0;
3094 static inline LRESULT
3095 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3097 return infoPtr->nNumButtons;
3101 static inline LRESULT
3102 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3104 infoPtr->dwStructSize = Size;
3106 return 0;
3110 static LRESULT
3111 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3113 TBUTTON_INFO *btnPtr;
3114 INT nIndex;
3116 TRACE("button %d, iBitmap now %d\n", Id, Index);
3118 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3119 if (nIndex == -1)
3120 return FALSE;
3122 btnPtr = &infoPtr->buttons[nIndex];
3123 btnPtr->iBitmap = Index;
3125 /* we HAVE to erase the background, the new bitmap could be */
3126 /* transparent */
3127 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3129 return TRUE;
3133 static LRESULT
3134 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3136 TBUTTON_INFO *btnPtr;
3137 INT nIndex;
3138 INT nOldIndex = -1;
3139 BOOL bChecked = FALSE;
3141 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3143 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3145 if (nIndex == -1)
3146 return FALSE;
3148 btnPtr = &infoPtr->buttons[nIndex];
3150 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3152 if (!LOWORD(lParam))
3153 btnPtr->fsState &= ~TBSTATE_CHECKED;
3154 else {
3155 if (btnPtr->fsStyle & BTNS_GROUP) {
3156 nOldIndex =
3157 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3158 if (nOldIndex == nIndex)
3159 return 0;
3160 if (nOldIndex != -1)
3161 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3163 btnPtr->fsState |= TBSTATE_CHECKED;
3166 if( bChecked != LOWORD(lParam) )
3168 if (nOldIndex != -1)
3169 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3170 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3173 /* FIXME: Send a WM_NOTIFY?? */
3175 return TRUE;
3179 static LRESULT
3180 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3182 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3186 static LRESULT
3187 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3189 CUSTDLG_INFO custInfo;
3190 LRESULT ret;
3191 NMHDR nmhdr;
3193 custInfo.tbInfo = infoPtr;
3194 custInfo.tbHwnd = infoPtr->hwndSelf;
3196 /* send TBN_BEGINADJUST notification */
3197 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3199 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3200 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3202 /* send TBN_ENDADJUST notification */
3203 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3205 return ret;
3209 static LRESULT
3210 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3212 NMTOOLBARW nmtb;
3213 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3215 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3216 return FALSE;
3218 memset(&nmtb, 0, sizeof(nmtb));
3219 nmtb.iItem = btnPtr->idCommand;
3220 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3221 nmtb.tbButton.idCommand = btnPtr->idCommand;
3222 nmtb.tbButton.fsState = btnPtr->fsState;
3223 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3224 nmtb.tbButton.dwData = btnPtr->dwData;
3225 nmtb.tbButton.iString = btnPtr->iString;
3226 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3228 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3230 if (infoPtr->nNumButtons == 1) {
3231 TRACE(" simple delete!\n");
3232 free_string( infoPtr->buttons );
3233 Free (infoPtr->buttons);
3234 infoPtr->buttons = NULL;
3235 infoPtr->nNumButtons = 0;
3237 else {
3238 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3239 TRACE("complex delete! [nIndex=%d]\n", nIndex);
3241 infoPtr->nNumButtons--;
3242 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3243 if (nIndex > 0) {
3244 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3245 nIndex * sizeof(TBUTTON_INFO));
3248 if (nIndex < infoPtr->nNumButtons) {
3249 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3250 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3253 free_string( oldButtons + nIndex );
3254 Free (oldButtons);
3257 TOOLBAR_LayoutToolbar(infoPtr);
3259 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3261 return TRUE;
3265 static LRESULT
3266 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3268 TBUTTON_INFO *btnPtr;
3269 INT nIndex;
3270 DWORD bState;
3272 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3274 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3276 if (nIndex == -1)
3277 return FALSE;
3279 btnPtr = &infoPtr->buttons[nIndex];
3281 bState = btnPtr->fsState & TBSTATE_ENABLED;
3283 /* update the toolbar button state */
3284 if(!LOWORD(lParam)) {
3285 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3286 } else {
3287 btnPtr->fsState |= TBSTATE_ENABLED;
3290 /* redraw the button only if the state of the button changed */
3291 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3292 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3294 return TRUE;
3298 static inline LRESULT
3299 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3301 return infoPtr->bAnchor;
3305 static LRESULT
3306 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3308 INT nIndex;
3310 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3311 if (nIndex == -1)
3312 return -1;
3314 return infoPtr->buttons[nIndex].iBitmap;
3318 static inline LRESULT
3319 TOOLBAR_GetBitmapFlags (void)
3321 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3325 static LRESULT
3326 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3328 TBUTTON_INFO *btnPtr;
3330 if (lpTbb == NULL)
3331 return FALSE;
3333 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3334 return FALSE;
3336 btnPtr = &infoPtr->buttons[nIndex];
3337 lpTbb->iBitmap = btnPtr->iBitmap;
3338 lpTbb->idCommand = btnPtr->idCommand;
3339 lpTbb->fsState = btnPtr->fsState;
3340 lpTbb->fsStyle = btnPtr->fsStyle;
3341 lpTbb->bReserved[0] = 0;
3342 lpTbb->bReserved[1] = 0;
3343 lpTbb->dwData = btnPtr->dwData;
3344 lpTbb->iString = btnPtr->iString;
3346 return TRUE;
3350 static LRESULT
3351 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3353 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3354 TBUTTON_INFO *btnPtr;
3355 INT nIndex;
3357 if (lpTbInfo == NULL)
3358 return -1;
3360 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3361 * the headers and tests shows that even with comctl 6 Vista accepts only the
3362 * original TBBUTTONINFO size
3364 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3366 WARN("Invalid button size\n");
3367 return -1;
3370 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3371 if (nIndex == -1)
3372 return -1;
3374 btnPtr = &infoPtr->buttons[nIndex];
3375 if (lpTbInfo->dwMask & TBIF_COMMAND)
3376 lpTbInfo->idCommand = btnPtr->idCommand;
3377 if (lpTbInfo->dwMask & TBIF_IMAGE)
3378 lpTbInfo->iImage = btnPtr->iBitmap;
3379 if (lpTbInfo->dwMask & TBIF_LPARAM)
3380 lpTbInfo->lParam = btnPtr->dwData;
3381 if (lpTbInfo->dwMask & TBIF_SIZE)
3382 /* tests show that for separators TBIF_SIZE returns not calculated width,
3383 but cx property, that differs from 0 only if application have
3384 specifically set it */
3385 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3386 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3387 if (lpTbInfo->dwMask & TBIF_STATE)
3388 lpTbInfo->fsState = btnPtr->fsState;
3389 if (lpTbInfo->dwMask & TBIF_STYLE)
3390 lpTbInfo->fsStyle = btnPtr->fsStyle;
3391 if (lpTbInfo->dwMask & TBIF_TEXT) {
3392 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3393 can't use TOOLBAR_GetText here */
3394 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3395 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3396 if (bUnicode)
3397 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3398 else
3399 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3400 } else
3401 lpTbInfo->pszText[0] = '\0';
3403 return nIndex;
3407 static inline LRESULT
3408 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3410 return MAKELONG((WORD)infoPtr->nButtonWidth,
3411 (WORD)infoPtr->nButtonHeight);
3415 static LRESULT
3416 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3418 INT nIndex;
3419 LPWSTR lpText;
3420 LRESULT ret = 0;
3422 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3423 if (nIndex == -1)
3424 return -1;
3426 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3428 if (isW)
3430 if (lpText)
3432 ret = strlenW (lpText);
3433 if (lpStr) strcpyW (lpStr, lpText);
3436 else
3437 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3438 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3439 return ret;
3443 static LRESULT
3444 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3446 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3447 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3448 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3452 static inline LRESULT
3453 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3455 TRACE("\n");
3457 return infoPtr->dwExStyle;
3461 static LRESULT
3462 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3464 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3465 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3466 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3470 static LRESULT
3471 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3473 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3474 return -1;
3476 if (infoPtr->nHotItem < 0)
3477 return -1;
3479 return (LRESULT)infoPtr->nHotItem;
3483 static LRESULT
3484 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3486 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3487 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3488 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3492 static LRESULT
3493 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3495 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3497 *lptbim = infoPtr->tbim;
3499 return 0;
3503 static inline LRESULT
3504 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3506 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3508 return (LRESULT)infoPtr->clrInsertMark;
3512 static LRESULT
3513 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3515 TBUTTON_INFO *btnPtr;
3517 btnPtr = &infoPtr->buttons[nIndex];
3518 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3519 return FALSE;
3521 if (lpRect == NULL)
3522 return FALSE;
3523 if (btnPtr->fsState & TBSTATE_HIDDEN)
3524 return FALSE;
3526 lpRect->left = btnPtr->rect.left;
3527 lpRect->right = btnPtr->rect.right;
3528 lpRect->bottom = btnPtr->rect.bottom;
3529 lpRect->top = btnPtr->rect.top;
3531 return TRUE;
3535 static LRESULT
3536 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3538 if (lpSize == NULL)
3539 return FALSE;
3541 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3542 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3544 TRACE("maximum size %d x %d\n",
3545 infoPtr->rcBound.right - infoPtr->rcBound.left,
3546 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3548 return TRUE;
3552 /* << TOOLBAR_GetObject >> */
3555 static inline LRESULT
3556 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3558 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3562 static LRESULT
3563 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3565 TBUTTON_INFO *btnPtr;
3566 INT nIndex;
3568 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3569 btnPtr = &infoPtr->buttons[nIndex];
3570 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3571 return FALSE;
3573 if (lpRect == NULL)
3574 return FALSE;
3576 lpRect->left = btnPtr->rect.left;
3577 lpRect->right = btnPtr->rect.right;
3578 lpRect->bottom = btnPtr->rect.bottom;
3579 lpRect->top = btnPtr->rect.top;
3581 return TRUE;
3585 static inline LRESULT
3586 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3588 return infoPtr->nRows;
3592 static LRESULT
3593 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3595 INT nIndex;
3597 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3598 if (nIndex == -1)
3599 return -1;
3601 return infoPtr->buttons[nIndex].fsState;
3605 static inline LRESULT
3606 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3608 return infoPtr->dwStyle;
3612 static inline LRESULT
3613 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3615 return infoPtr->nMaxTextRows;
3619 static LRESULT
3620 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3622 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3623 TOOLBAR_TooltipCreateControl(infoPtr);
3624 return (LRESULT)infoPtr->hwndToolTip;
3628 static LRESULT
3629 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3631 TRACE("%s hwnd=%p\n",
3632 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3634 return infoPtr->bUnicode;
3638 static inline LRESULT
3639 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3641 return infoPtr->iVersion;
3645 static LRESULT
3646 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3648 TBUTTON_INFO *btnPtr;
3649 BYTE oldState;
3650 INT nIndex;
3652 TRACE("\n");
3654 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3655 if (nIndex == -1)
3656 return FALSE;
3658 btnPtr = &infoPtr->buttons[nIndex];
3659 oldState = btnPtr->fsState;
3661 if (fHide)
3662 btnPtr->fsState |= TBSTATE_HIDDEN;
3663 else
3664 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3666 if (oldState != btnPtr->fsState) {
3667 TOOLBAR_LayoutToolbar (infoPtr);
3668 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3671 return TRUE;
3675 static inline LRESULT
3676 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3678 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3682 static LRESULT
3683 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3685 TBUTTON_INFO *btnPtr;
3686 INT nIndex;
3687 DWORD oldState;
3689 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3690 if (nIndex == -1)
3691 return FALSE;
3693 btnPtr = &infoPtr->buttons[nIndex];
3694 oldState = btnPtr->fsState;
3696 if (fIndeterminate)
3697 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3698 else
3699 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3701 if(oldState != btnPtr->fsState)
3702 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3704 return TRUE;
3708 static LRESULT
3709 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3711 if (lpTbb == NULL)
3712 return FALSE;
3714 if (nIndex == -1) {
3715 /* EPP: this seems to be an undocumented call (from my IE4)
3716 * I assume in that case that:
3717 * - index of insertion is at the end of existing buttons
3718 * I only see this happen with nIndex == -1, but it could have a special
3719 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3721 nIndex = infoPtr->nNumButtons;
3723 } else if (nIndex < 0)
3724 return FALSE;
3726 TRACE("inserting button index=%d\n", nIndex);
3727 if (nIndex > infoPtr->nNumButtons) {
3728 nIndex = infoPtr->nNumButtons;
3729 TRACE("adjust index=%d\n", nIndex);
3732 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3735 /* << TOOLBAR_InsertMarkHitTest >> */
3738 static LRESULT
3739 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3741 INT nIndex;
3743 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3744 if (nIndex == -1)
3745 return -1;
3747 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3751 static LRESULT
3752 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3754 INT nIndex;
3756 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3757 if (nIndex == -1)
3758 return -1;
3760 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3764 static LRESULT
3765 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3767 INT nIndex;
3769 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3770 if (nIndex == -1)
3771 return -1;
3773 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3777 static LRESULT
3778 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3780 INT nIndex;
3782 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3783 if (nIndex == -1)
3784 return -1;
3786 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3790 static LRESULT
3791 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3793 INT nIndex;
3795 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3796 if (nIndex == -1)
3797 return -1;
3799 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3803 static LRESULT
3804 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3806 INT nIndex;
3808 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3809 if (nIndex == -1)
3810 return -1;
3812 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3816 static LRESULT
3817 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3819 TBADDBITMAP tbab;
3820 tbab.hInst = hInstance;
3821 tbab.nID = wParam;
3823 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3825 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3829 static LRESULT
3830 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3832 WCHAR wszAccel[] = {'&',wAccel,0};
3833 int i;
3835 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3836 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3838 for (i = 0; i < infoPtr->nNumButtons; i++)
3840 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3841 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3842 !(btnPtr->fsState & TBSTATE_HIDDEN))
3844 int iLen = strlenW(wszAccel);
3845 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3847 if (!lpszStr)
3848 continue;
3850 while (*lpszStr)
3852 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3854 lpszStr += 2;
3855 continue;
3857 if (!strncmpiW(lpszStr, wszAccel, iLen))
3859 *pIDButton = btnPtr->idCommand;
3860 return TRUE;
3862 lpszStr++;
3866 return FALSE;
3870 static LRESULT
3871 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3873 INT nIndex;
3874 DWORD oldState;
3875 TBUTTON_INFO *btnPtr;
3877 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3879 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3880 if (nIndex == -1)
3881 return FALSE;
3883 btnPtr = &infoPtr->buttons[nIndex];
3884 oldState = btnPtr->fsState;
3886 if (fMark)
3887 btnPtr->fsState |= TBSTATE_MARKED;
3888 else
3889 btnPtr->fsState &= ~TBSTATE_MARKED;
3891 if(oldState != btnPtr->fsState)
3892 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3894 return TRUE;
3898 /* fixes up an index of a button affected by a move */
3899 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3901 if (bMoveUp)
3903 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3904 (*pIndex)--;
3905 else if (*pIndex == nIndex)
3906 *pIndex = nMoveIndex;
3908 else
3910 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3911 (*pIndex)++;
3912 else if (*pIndex == nIndex)
3913 *pIndex = nMoveIndex;
3918 static LRESULT
3919 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3921 INT nIndex;
3922 INT nCount;
3923 TBUTTON_INFO button;
3925 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3927 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3928 if ((nIndex == -1) || (nMoveIndex < 0))
3929 return FALSE;
3931 if (nMoveIndex > infoPtr->nNumButtons - 1)
3932 nMoveIndex = infoPtr->nNumButtons - 1;
3934 button = infoPtr->buttons[nIndex];
3936 /* move button right */
3937 if (nIndex < nMoveIndex)
3939 nCount = nMoveIndex - nIndex;
3940 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3941 infoPtr->buttons[nMoveIndex] = button;
3943 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3944 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3945 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3946 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3948 else if (nIndex > nMoveIndex) /* move button left */
3950 nCount = nIndex - nMoveIndex;
3951 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3952 infoPtr->buttons[nMoveIndex] = button;
3954 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3955 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3956 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3957 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3960 TOOLBAR_LayoutToolbar(infoPtr);
3961 TOOLBAR_AutoSize(infoPtr);
3962 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3964 return TRUE;
3968 static LRESULT
3969 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
3971 TBUTTON_INFO *btnPtr;
3972 INT nIndex;
3973 DWORD oldState;
3975 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3976 if (nIndex == -1)
3977 return FALSE;
3979 btnPtr = &infoPtr->buttons[nIndex];
3980 oldState = btnPtr->fsState;
3982 if (fPress)
3983 btnPtr->fsState |= TBSTATE_PRESSED;
3984 else
3985 btnPtr->fsState &= ~TBSTATE_PRESSED;
3987 if(oldState != btnPtr->fsState)
3988 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3990 return TRUE;
3993 /* FIXME: there might still be some confusion her between number of buttons
3994 * and number of bitmaps */
3995 static LRESULT
3996 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
3998 HBITMAP hBitmap;
3999 int i = 0, nOldButtons = 0, pos = 0;
4000 int nOldBitmaps, nNewBitmaps = 0;
4001 HIMAGELIST himlDef = 0;
4003 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4004 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4005 lpReplace->nButtons);
4007 if (lpReplace->hInstOld == HINST_COMMCTRL)
4009 FIXME("changing standard bitmaps not implemented\n");
4010 return FALSE;
4012 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4013 FIXME("resources not in the current module not implemented\n");
4015 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4016 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4017 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4018 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4019 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4021 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4022 nOldButtons = tbi->nButtons;
4023 tbi->nButtons = lpReplace->nButtons;
4024 tbi->hInst = lpReplace->hInstNew;
4025 tbi->nID = lpReplace->nIDNew;
4026 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4027 break;
4029 pos += tbi->nButtons;
4032 if (nOldButtons == 0)
4034 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4035 return FALSE;
4038 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4039 * bitmap
4041 if (lpReplace->hInstNew)
4042 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4043 else
4044 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4046 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4047 nOldBitmaps = ImageList_GetImageCount(himlDef);
4049 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4051 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4052 ImageList_Remove(himlDef, i);
4054 if (hBitmap)
4056 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4057 nNewBitmaps = ImageList_GetImageCount(himlDef);
4058 DeleteObject(hBitmap);
4061 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4063 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4064 pos, nOldBitmaps, nNewBitmaps);
4066 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4067 return TRUE;
4071 /* helper for TOOLBAR_SaveRestoreW */
4072 static BOOL
4073 TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
4075 NMTBSAVE save;
4076 INT ret, i;
4077 BOOL alloced = FALSE;
4078 HKEY key;
4080 TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );
4082 memset( &save, 0, sizeof(save) );
4083 save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
4084 save.iItem = -1;
4085 save.cButtons = infoPtr->nNumButtons;
4086 save.tbButton.idCommand = -1;
4087 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4089 if (!save.pData)
4091 save.pData = Alloc( save.cbData );
4092 if (!save.pData) return FALSE;
4093 alloced = TRUE;
4095 if (!save.pCurrent) save.pCurrent = save.pData;
4097 for (i = 0; i < infoPtr->nNumButtons; i++)
4099 save.iItem = i;
4100 save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
4101 save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
4102 save.tbButton.fsState = infoPtr->buttons[i].fsState;
4103 save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
4104 memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
4105 save.tbButton.dwData = infoPtr->buttons[i].dwData;
4106 save.tbButton.iString = infoPtr->buttons[i].iString;
4108 *save.pCurrent++ = save.tbButton.idCommand;
4110 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4113 ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
4114 if (ret == ERROR_SUCCESS)
4116 ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
4117 RegCloseKey( key );
4120 if (alloced) Free( save.pData );
4121 return !ret;
4125 /* helper for TOOLBAR_Restore */
4126 static void
4127 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4129 INT i;
4131 for (i = 0; i < infoPtr->nNumButtons; i++)
4133 free_string( infoPtr->buttons + i );
4134 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4137 Free(infoPtr->buttons);
4138 infoPtr->buttons = NULL;
4139 infoPtr->nNumButtons = 0;
4143 /* helper for TOOLBAR_SaveRestoreW */
4144 static BOOL
4145 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4147 LONG res;
4148 HKEY hkey = NULL;
4149 BOOL ret = FALSE;
4150 DWORD dwType;
4151 DWORD dwSize = 0;
4152 NMTBRESTORE nmtbr;
4153 NMHDR hdr;
4155 /* restore toolbar information */
4156 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4157 debugstr_w(lpSave->pszValueName));
4159 memset(&nmtbr, 0, sizeof(nmtbr));
4161 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4162 KEY_QUERY_VALUE, &hkey);
4163 if (!res)
4164 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4165 NULL, &dwSize);
4166 if (!res && dwType != REG_BINARY)
4167 res = ERROR_FILE_NOT_FOUND;
4168 if (!res)
4170 nmtbr.pData = Alloc(dwSize);
4171 nmtbr.cbData = dwSize;
4172 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4174 if (!res)
4175 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4176 (LPBYTE)nmtbr.pData, &dwSize);
4177 if (!res)
4179 nmtbr.pCurrent = nmtbr.pData;
4180 nmtbr.iItem = -1;
4181 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4182 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4184 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4186 INT i, count = nmtbr.cButtons;
4188 /* remove all existing buttons as this function is designed to
4189 * restore the toolbar to a previously saved state */
4190 TOOLBAR_DeleteAllButtons(infoPtr);
4192 for (i = 0; i < count; i++)
4194 nmtbr.iItem = i;
4195 nmtbr.tbButton.iBitmap = -1;
4196 nmtbr.tbButton.fsState = 0;
4197 nmtbr.tbButton.fsStyle = 0;
4198 nmtbr.tbButton.dwData = 0;
4199 nmtbr.tbButton.iString = 0;
4201 if (*nmtbr.pCurrent & 0x80000000)
4203 /* separator */
4204 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4205 nmtbr.tbButton.idCommand = 0;
4206 nmtbr.tbButton.fsStyle = BTNS_SEP;
4207 if (*nmtbr.pCurrent != (DWORD)-1)
4208 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4210 else
4211 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4213 nmtbr.pCurrent++;
4215 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4217 /* All returned ptrs and -1 are ignored */
4218 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4219 nmtbr.tbButton.iString = 0;
4221 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4224 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
4225 for (i = 0; ; i++)
4227 NMTOOLBARW tb;
4228 TBBUTTONINFOW bi;
4229 WCHAR buf[128];
4230 UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
4231 INT idx;
4233 memset( &tb, 0, sizeof(tb) );
4234 tb.iItem = i;
4235 tb.cchText = sizeof(buf) / sizeof(buf[0]);
4236 tb.pszText = buf;
4238 /* Use the same struct for both A and W versions since the layout is the same. */
4239 if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
4240 break;
4242 idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
4243 if (idx == -1) continue;
4245 /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
4246 be an index or a ptr. Either way it is simply copied. There is no api to change
4247 the string index, so we set it manually. The other properties can be set with SetButtonInfo. */
4248 free_string( infoPtr->buttons + idx );
4249 infoPtr->buttons[idx].iString = tb.tbButton.iString;
4251 memset( &bi, 0, sizeof(bi) );
4252 bi.cbSize = sizeof(bi);
4253 bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
4254 bi.iImage = tb.tbButton.iBitmap;
4255 bi.fsState = tb.tbButton.fsState;
4256 bi.fsStyle = tb.tbButton.fsStyle;
4257 bi.lParam = tb.tbButton.dwData;
4259 TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4261 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4263 /* remove all uninitialised buttons
4264 * note: loop backwards to avoid having to fixup i on a
4265 * delete */
4266 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4267 if (infoPtr->buttons[i].iBitmap == -1)
4268 TOOLBAR_DeleteButton(infoPtr, i);
4270 /* only indicate success if at least one button survived */
4271 if (infoPtr->nNumButtons > 0) ret = TRUE;
4274 Free (nmtbr.pData);
4275 RegCloseKey(hkey);
4277 return ret;
4281 static LRESULT
4282 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4284 if (lpSave == NULL) return 0;
4286 if (wParam)
4287 return TOOLBAR_Save(infoPtr, lpSave);
4288 else
4289 return TOOLBAR_Restore(infoPtr, lpSave);
4293 static LRESULT
4294 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4296 LPWSTR pszValueName = 0, pszSubKey = 0;
4297 TBSAVEPARAMSW SaveW;
4298 LRESULT result = 0;
4299 int len;
4301 if (lpSave == NULL) return 0;
4303 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4304 pszSubKey = Alloc(len * sizeof(WCHAR));
4305 if (!pszSubKey) goto exit;
4306 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4308 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4309 pszValueName = Alloc(len * sizeof(WCHAR));
4310 if (!pszValueName) goto exit;
4311 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4313 SaveW.pszValueName = pszValueName;
4314 SaveW.pszSubKey = pszSubKey;
4315 SaveW.hkr = lpSave->hkr;
4316 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4318 exit:
4319 Free (pszValueName);
4320 Free (pszSubKey);
4322 return result;
4326 static LRESULT
4327 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4329 BOOL bOldAnchor = infoPtr->bAnchor;
4331 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4333 infoPtr->bAnchor = bAnchor;
4335 /* Native does not remove the hot effect from an already hot button */
4337 return (LRESULT)bOldAnchor;
4341 static LRESULT
4342 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4344 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4345 short width = (short)LOWORD(lParam);
4346 short height = (short)HIWORD(lParam);
4348 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", infoPtr->hwndSelf, wParam, lParam);
4350 if (wParam != 0)
4351 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4353 /* 0 width or height is changed to 1 */
4354 if (width == 0)
4355 width = 1;
4356 if (height == 0)
4357 height = 1;
4359 if (infoPtr->nNumButtons > 0)
4360 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4361 infoPtr->nNumButtons,
4362 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4364 if (width < -1 || height < -1)
4366 /* Windows destroys the imagelist and seems to actually use negative
4367 * values to compute button sizes */
4368 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4369 return FALSE;
4372 /* width or height of -1 means no change */
4373 if (width != -1)
4374 infoPtr->nBitmapWidth = width;
4375 if (height != -1)
4376 infoPtr->nBitmapHeight = height;
4378 if ((himlDef == infoPtr->himlInt) &&
4379 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4381 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4382 infoPtr->nBitmapHeight);
4385 TOOLBAR_CalcToolbar(infoPtr);
4386 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4387 return TRUE;
4391 static LRESULT
4392 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4393 const TBBUTTONINFOW *lptbbi, BOOL isW)
4395 TBUTTON_INFO *btnPtr;
4396 INT nIndex;
4397 RECT oldBtnRect;
4399 if (lptbbi == NULL)
4400 return FALSE;
4401 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4402 return FALSE;
4404 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4405 if (nIndex == -1)
4406 return FALSE;
4408 btnPtr = &infoPtr->buttons[nIndex];
4409 if (lptbbi->dwMask & TBIF_COMMAND)
4410 btnPtr->idCommand = lptbbi->idCommand;
4411 if (lptbbi->dwMask & TBIF_IMAGE)
4412 btnPtr->iBitmap = lptbbi->iImage;
4413 if (lptbbi->dwMask & TBIF_LPARAM)
4414 btnPtr->dwData = lptbbi->lParam;
4415 if (lptbbi->dwMask & TBIF_SIZE)
4416 btnPtr->cx = lptbbi->cx;
4417 if (lptbbi->dwMask & TBIF_STATE)
4418 btnPtr->fsState = lptbbi->fsState;
4419 if (lptbbi->dwMask & TBIF_STYLE)
4420 btnPtr->fsStyle = lptbbi->fsStyle;
4422 if (lptbbi->dwMask & TBIF_TEXT)
4423 set_stringT( btnPtr, lptbbi->pszText, isW );
4425 /* save the button rect to see if we need to redraw the whole toolbar */
4426 oldBtnRect = btnPtr->rect;
4427 TOOLBAR_LayoutToolbar(infoPtr);
4429 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4430 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4431 else
4432 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4434 return TRUE;
4438 static LRESULT
4439 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4441 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4442 int top = default_top_margin(infoPtr);
4444 if ((cx < 0) || (cy < 0))
4446 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4447 return FALSE;
4450 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4452 /* The documentation claims you can only change the button size before
4453 * any button has been added. But this is wrong.
4454 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4455 * it to the toolbar, and it checks that the return value is nonzero - mjm
4456 * Further testing shows that we must actually perform the change too.
4459 * The documentation also does not mention that if 0 is supplied for
4460 * either size, the system changes it to the default of 24 wide and
4461 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4463 if (cx == 0) cx = 24;
4464 if (cy == 0) cy = 22;
4466 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4467 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4469 if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
4470 top != infoPtr->iTopMargin)
4472 infoPtr->nButtonWidth = cx;
4473 infoPtr->nButtonHeight = cy;
4474 infoPtr->iTopMargin = top;
4476 TOOLBAR_LayoutToolbar( infoPtr );
4477 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
4479 return TRUE;
4483 static LRESULT
4484 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4486 /* if setting to current values, ignore */
4487 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4488 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4489 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4490 infoPtr->cxMin, infoPtr->cxMax);
4491 return TRUE;
4494 /* save new values */
4495 infoPtr->cxMin = (short)LOWORD(lParam);
4496 infoPtr->cxMax = (short)HIWORD(lParam);
4498 /* otherwise we need to recalc the toolbar and in some cases
4499 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4500 which doesn't actually draw - GA). */
4501 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4502 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4504 TOOLBAR_CalcToolbar (infoPtr);
4506 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4508 return TRUE;
4512 static LRESULT
4513 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4515 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4516 return FALSE;
4518 infoPtr->buttons[nIndex].idCommand = nId;
4520 if (infoPtr->hwndToolTip) {
4522 FIXME("change tool tip!\n");
4526 return TRUE;
4530 static LRESULT
4531 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4533 HIMAGELIST himlTemp;
4534 INT id = 0;
4536 if (infoPtr->iVersion >= 5)
4537 id = wParam;
4539 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4540 &infoPtr->cimlDis, himl, id);
4542 /* FIXME: redraw ? */
4544 return (LRESULT)himlTemp;
4548 static LRESULT
4549 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4551 DWORD dwTemp;
4553 TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", infoPtr->hwndSelf, (DWORD)wParam, (DWORD)lParam);
4555 dwTemp = infoPtr->dwDTFlags;
4556 infoPtr->dwDTFlags =
4557 (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4559 return (LRESULT)dwTemp;
4562 /* This function differs a bit from what MSDN says it does:
4563 * 1. lParam contains extended style flags to OR with current style
4564 * (MSDN isn't clear on the OR bit)
4565 * 2. wParam appears to contain extended style flags to be reset
4566 * (MSDN says that this parameter is reserved)
4568 static LRESULT
4569 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4571 DWORD old_style = infoPtr->dwExStyle;
4573 TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
4575 if (mask)
4576 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4577 else
4578 infoPtr->dwExStyle = style;
4580 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4581 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4582 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4584 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4585 TOOLBAR_CalcToolbar(infoPtr);
4586 else
4587 TOOLBAR_LayoutToolbar(infoPtr);
4589 TOOLBAR_AutoSize(infoPtr);
4590 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4592 return old_style;
4596 static LRESULT
4597 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4599 HIMAGELIST himlTemp;
4600 INT id = 0;
4602 if (infoPtr->iVersion >= 5)
4603 id = wParam;
4605 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4607 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4608 &infoPtr->cimlHot, himl, id);
4610 /* FIXME: redraw ? */
4612 return (LRESULT)himlTemp;
4616 /* Makes previous hot button no longer hot, makes the specified
4617 * button hot and sends appropriate notifications. dwReason is one or
4618 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4619 * NOTE 1: this function does not validate nHit
4620 * NOTE 2: the name of this function is completely made up and
4621 * not based on any documentation from Microsoft. */
4622 static void
4623 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4625 if (infoPtr->nHotItem != nHit)
4627 NMTBHOTITEM nmhotitem;
4628 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4630 nmhotitem.dwFlags = dwReason;
4631 if(infoPtr->nHotItem >= 0)
4633 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4634 nmhotitem.idOld = oldBtnPtr->idCommand;
4636 else
4638 nmhotitem.dwFlags |= HICF_ENTERING;
4639 nmhotitem.idOld = 0;
4642 if (nHit >= 0)
4644 btnPtr = &infoPtr->buttons[nHit];
4645 nmhotitem.idNew = btnPtr->idCommand;
4647 else
4649 nmhotitem.dwFlags |= HICF_LEAVING;
4650 nmhotitem.idNew = 0;
4653 /* now change the hot and invalidate the old and new buttons - if the
4654 * parent agrees */
4655 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4657 if (oldBtnPtr) {
4658 oldBtnPtr->bHot = FALSE;
4659 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4661 /* setting disabled buttons as hot fails even if the notify contains the button id */
4662 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4663 btnPtr->bHot = TRUE;
4664 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4665 infoPtr->nHotItem = nHit;
4667 else
4668 infoPtr->nHotItem = -1;
4673 static LRESULT
4674 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4676 INT nOldHotItem = infoPtr->nHotItem;
4678 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4680 if (nHotItem >= infoPtr->nNumButtons)
4681 return infoPtr->nHotItem;
4683 if (nHotItem < 0)
4684 nHotItem = -1;
4686 /* NOTE: an application can still remove the hot item even if anchor
4687 * highlighting is enabled */
4689 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4691 if (nOldHotItem < 0)
4692 return -1;
4694 return (LRESULT)nOldHotItem;
4698 static LRESULT
4699 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4701 HIMAGELIST himlTemp;
4702 INT oldButtonWidth = infoPtr->nButtonWidth;
4703 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4704 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4705 INT i, id = 0;
4707 if (infoPtr->iVersion >= 5)
4708 id = wParam;
4710 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4711 &infoPtr->cimlDef, himl, id);
4713 infoPtr->nNumBitmaps = 0;
4714 for (i = 0; i < infoPtr->cimlDef; i++)
4715 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4717 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4718 &infoPtr->nBitmapHeight))
4720 infoPtr->nBitmapWidth = 1;
4721 infoPtr->nBitmapHeight = 1;
4723 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4725 TOOLBAR_CalcToolbar(infoPtr);
4726 if (infoPtr->nButtonWidth < oldButtonWidth)
4727 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4730 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4731 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4732 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4734 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4736 return (LRESULT)himlTemp;
4740 static LRESULT
4741 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4743 infoPtr->nIndent = nIndent;
4745 TRACE("\n");
4747 /* process only on indent changing */
4748 if(infoPtr->nIndent != nIndent)
4750 infoPtr->nIndent = nIndent;
4751 TOOLBAR_CalcToolbar (infoPtr);
4752 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4755 return TRUE;
4759 static LRESULT
4760 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4762 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4764 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4766 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4767 return 0;
4770 if ((lptbim->iButton == -1) ||
4771 ((lptbim->iButton < infoPtr->nNumButtons) &&
4772 (lptbim->iButton >= 0)))
4774 infoPtr->tbim = *lptbim;
4775 /* FIXME: don't need to update entire toolbar */
4776 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4778 else
4779 ERR("Invalid button index %d\n", lptbim->iButton);
4781 return 0;
4785 static LRESULT
4786 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4788 infoPtr->clrInsertMark = clr;
4790 /* FIXME: don't need to update entire toolbar */
4791 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4793 return 0;
4797 static LRESULT
4798 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4800 infoPtr->nMaxTextRows = nMaxRows;
4802 TOOLBAR_CalcToolbar(infoPtr);
4803 return TRUE;
4807 /* MSDN gives slightly wrong info on padding.
4808 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4809 * 2. It is not used to create a blank area between the edge of the button
4810 * and the text or image if TBSTYLE_LIST is set. It is used to control
4811 * the gap between the image and text.
4812 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4813 * to control the bottom and right borders [with the border being
4814 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4815 * is shared evenly on both sides of the button.
4816 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4818 static LRESULT
4819 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4821 DWORD oldPad;
4823 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4824 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4825 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4826 TRACE("cx=%d, cy=%d\n",
4827 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4828 return (LRESULT) oldPad;
4832 static LRESULT
4833 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4835 HWND hwndOldNotify;
4837 TRACE("\n");
4839 hwndOldNotify = infoPtr->hwndNotify;
4840 infoPtr->hwndNotify = hParent;
4842 return (LRESULT)hwndOldNotify;
4846 static LRESULT
4847 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4849 int rows = LOWORD(wParam);
4850 BOOL bLarger = HIWORD(wParam);
4852 TRACE("\n");
4854 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4856 if(infoPtr->nRows != rows)
4858 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4859 int curColumn = 0; /* Current column */
4860 int curRow = 0; /* Current row */
4861 int hidden = 0; /* Number of hidden buttons */
4862 int seps = 0; /* Number of separators */
4863 int idealWrap = 0; /* Ideal wrap point */
4864 int i;
4865 BOOL wrap;
4868 Calculate new size and wrap points - Under windows, setrows will
4869 change the dimensions if needed to show the number of requested
4870 rows (if CCS_NORESIZE is set), or will take up the whole window
4871 (if no CCS_NORESIZE).
4873 Basic algorithm - If N buttons, and y rows requested, each row
4874 contains N/y buttons.
4876 FIXME: Handling of separators not obvious from testing results
4877 FIXME: Take width of window into account?
4880 /* Loop through the buttons one by one counting key items */
4881 for (i = 0; i < infoPtr->nNumButtons; i++ )
4883 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4884 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4885 hidden++;
4886 else if (btnPtr[i].fsStyle & BTNS_SEP)
4887 seps++;
4890 /* FIXME: Separators make this quite complex */
4891 if (seps) FIXME("Separators unhandled\n");
4893 /* Round up so more per line, i.e., less rows */
4894 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4896 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4897 achieve the requested number of rows. */
4898 if (bLarger && idealWrap > 1)
4900 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4901 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4903 if (resRows < rows && moreRows > rows)
4905 idealWrap--;
4906 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4910 curColumn = curRow = 0;
4911 wrap = FALSE;
4912 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4913 infoPtr->nNumButtons, hidden, rows);
4915 for (i = 0; i < infoPtr->nNumButtons; i++ )
4917 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4918 continue;
4920 /* Step on, wrap if necessary or flag next to wrap */
4921 if (!wrap) {
4922 curColumn++;
4923 } else {
4924 wrap = FALSE;
4925 curColumn = 1;
4926 curRow++;
4929 if (curColumn > (idealWrap-1)) {
4930 wrap = TRUE;
4931 btnPtr[i].fsState |= TBSTATE_WRAP;
4935 TRACE("Result - %d rows\n", curRow + 1);
4937 /* recalculate toolbar */
4938 TOOLBAR_CalcToolbar (infoPtr);
4940 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4941 if NORESIZE is NOT set, then the toolbar will always be resized to
4942 take up the whole window. With it set, sizing needs to be manual. */
4943 if (infoPtr->dwStyle & CCS_NORESIZE) {
4944 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4945 infoPtr->rcBound.right - infoPtr->rcBound.left,
4946 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4947 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4950 /* repaint toolbar */
4951 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4954 /* return bounding rectangle */
4955 if (lprc) {
4956 lprc->left = infoPtr->rcBound.left;
4957 lprc->right = infoPtr->rcBound.right;
4958 lprc->top = infoPtr->rcBound.top;
4959 lprc->bottom = infoPtr->rcBound.bottom;
4962 return 0;
4966 static LRESULT
4967 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4969 TBUTTON_INFO *btnPtr;
4970 INT nIndex;
4972 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4973 if (nIndex == -1)
4974 return FALSE;
4976 btnPtr = &infoPtr->buttons[nIndex];
4978 /* if hidden state has changed the invalidate entire window and recalc */
4979 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4980 btnPtr->fsState = LOWORD(lParam);
4981 TOOLBAR_CalcToolbar (infoPtr);
4982 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4983 return TRUE;
4986 /* process state changing if current state doesn't match new state */
4987 if(btnPtr->fsState != LOWORD(lParam))
4989 btnPtr->fsState = LOWORD(lParam);
4990 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4993 return TRUE;
4996 static inline void unwrap(TOOLBAR_INFO *info)
4998 int i;
5000 for (i = 0; i < info->nNumButtons; i++)
5001 info->buttons[i].fsState &= ~TBSTATE_WRAP;
5004 static LRESULT
5005 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
5007 DWORD dwOldStyle = infoPtr->dwStyle;
5009 TRACE("new style 0x%08x\n", style);
5011 if (style & TBSTYLE_LIST)
5012 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
5013 else
5014 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
5016 infoPtr->dwStyle = style;
5017 TOOLBAR_CheckStyle(infoPtr);
5019 if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5021 if (dwOldStyle & TBSTYLE_WRAPABLE)
5022 unwrap(infoPtr);
5023 TOOLBAR_CalcToolbar(infoPtr);
5025 else if ((dwOldStyle ^ style) & CCS_VERT)
5026 TOOLBAR_LayoutToolbar(infoPtr);
5028 /* only resize if one of the CCS_* styles was changed */
5029 if ((dwOldStyle ^ style) & COMMON_STYLES)
5031 TOOLBAR_AutoSize(infoPtr);
5032 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5035 return 0;
5039 static inline LRESULT
5040 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
5042 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5044 infoPtr->hwndToolTip = hwndTooltip;
5045 return 0;
5049 static LRESULT
5050 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
5052 BOOL bTemp;
5054 TRACE("%s hwnd=%p\n",
5055 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
5057 bTemp = infoPtr->bUnicode;
5058 infoPtr->bUnicode = (BOOL)wParam;
5060 return bTemp;
5064 static LRESULT
5065 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5067 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5068 comctl32_color.clrBtnHighlight :
5069 infoPtr->clrBtnHighlight;
5070 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5071 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5072 return 1;
5076 static LRESULT
5077 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5079 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5080 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5081 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5083 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5084 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5085 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5086 return 0;
5090 static LRESULT
5091 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5093 INT iOldVersion = infoPtr->iVersion;
5095 infoPtr->iVersion = iVersion;
5097 if (infoPtr->iVersion >= 5)
5098 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5100 return iOldVersion;
5104 static LRESULT
5105 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
5107 WORD iString = HIWORD(wParam);
5108 WORD buffersize = LOWORD(wParam);
5109 LRESULT ret = -1;
5111 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
5113 if (iString < infoPtr->nNumStrings)
5115 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5116 ret--;
5118 TRACE("returning %s\n", debugstr_a(str));
5120 else
5121 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5123 return ret;
5127 static LRESULT
5128 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5130 WORD iString = HIWORD(wParam);
5131 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5132 LRESULT ret = -1;
5134 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5136 if (iString < infoPtr->nNumStrings)
5138 len = min(len, strlenW(infoPtr->strings[iString]));
5139 ret = (len+1)*sizeof(WCHAR);
5140 if (str)
5142 memcpy(str, infoPtr->strings[iString], ret);
5143 str[len] = '\0';
5145 ret = len;
5147 TRACE("returning %s\n", debugstr_w(str));
5149 else
5150 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5152 return ret;
5155 static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5157 SIZE * pSize = (SIZE*)lParam;
5158 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5159 return 0;
5162 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5163 * caller to specify a reason why the hot item changed (rather than just the
5164 * HICF_OTHER that TB_SETHOTITEM sends). */
5165 static LRESULT
5166 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5168 INT nOldHotItem = infoPtr->nHotItem;
5170 TRACE("old item=%d, new item=%d, flags=%08x\n",
5171 nOldHotItem, nHotItem, (DWORD)lParam);
5173 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5174 nHotItem = -1;
5176 /* NOTE: an application can still remove the hot item even if anchor
5177 * highlighting is enabled */
5179 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5181 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5184 /* Sets the toolbar global iListGap parameter which controls the amount of
5185 * spacing between the image and the text of buttons for TBSTYLE_LIST
5186 * toolbars. */
5187 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5189 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5191 infoPtr->iListGap = iListGap;
5193 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5195 return 0;
5198 /* Returns the number of maximum number of image lists associated with the
5199 * various states. */
5200 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5202 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5204 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5207 static LRESULT
5208 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5210 LPSIZE lpsize = (LPSIZE)lParam;
5212 if (lpsize == NULL)
5213 return FALSE;
5216 * Testing shows the following:
5217 * wParam = 0 adjust cx value
5218 * = 1 set cy value to max size.
5219 * lParam pointer to SIZE structure
5222 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5223 wParam, lParam, lpsize->cx, lpsize->cy);
5225 switch(wParam) {
5226 case 0:
5227 if (lpsize->cx == -1) {
5228 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5230 else if(HIWORD(lpsize->cx)) {
5231 RECT rc;
5232 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5234 GetWindowRect(infoPtr->hwndSelf, &rc);
5235 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5236 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5237 lpsize->cx = max(rc.right-rc.left,
5238 infoPtr->rcBound.right - infoPtr->rcBound.left);
5240 else {
5241 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5243 break;
5244 case 1:
5245 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5246 break;
5247 default:
5248 FIXME("Unknown wParam %ld\n", wParam);
5249 return 0;
5251 TRACE("set to -> 0x%08x 0x%08x\n",
5252 lpsize->cx, lpsize->cy);
5253 return 1;
5256 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5258 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5260 InvalidateRect(hwnd, NULL, TRUE);
5261 return 1;
5265 static LRESULT
5266 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5268 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5269 LOGFONTW logFont;
5271 TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
5273 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5274 GetClientRect(hwnd, &infoPtr->client_rect);
5275 infoPtr->bUnicode = infoPtr->hwndNotify &&
5276 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5277 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5279 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5280 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5282 OpenThemeData (hwnd, themeClass);
5284 TOOLBAR_CheckStyle (infoPtr);
5286 return 0;
5290 static LRESULT
5291 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5293 INT i;
5295 /* delete tooltip control */
5296 if (infoPtr->hwndToolTip)
5297 DestroyWindow (infoPtr->hwndToolTip);
5299 /* delete temporary buffer for tooltip text */
5300 Free (infoPtr->pszTooltipText);
5301 Free (infoPtr->bitmaps); /* bitmaps list */
5303 /* delete button data */
5304 for (i = 0; i < infoPtr->nNumButtons; i++)
5305 free_string( infoPtr->buttons + i );
5306 Free (infoPtr->buttons);
5308 /* delete strings */
5309 if (infoPtr->strings) {
5310 for (i = 0; i < infoPtr->nNumStrings; i++)
5311 Free (infoPtr->strings[i]);
5313 Free (infoPtr->strings);
5316 /* destroy internal image list */
5317 if (infoPtr->himlInt)
5318 ImageList_Destroy (infoPtr->himlInt);
5320 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5321 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5322 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5324 /* delete default font */
5325 DeleteObject (infoPtr->hDefaultFont);
5327 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
5329 /* free toolbar info data */
5330 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5331 Free (infoPtr);
5333 return 0;
5337 static LRESULT
5338 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5340 NMTBCUSTOMDRAW tbcd;
5341 INT ret = FALSE;
5342 DWORD ntfret;
5343 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5344 DWORD dwEraseCustDraw = 0;
5346 /* the app has told us not to redraw the toolbar */
5347 if (!infoPtr->bDoRedraw)
5348 return FALSE;
5350 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5351 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5352 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5353 tbcd.nmcd.hdc = (HDC)wParam;
5354 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5355 dwEraseCustDraw = ntfret & 0xffff;
5357 /* FIXME: in general the return flags *can* be or'ed together */
5358 switch (dwEraseCustDraw)
5360 case CDRF_DODEFAULT:
5361 break;
5362 case CDRF_SKIPDEFAULT:
5363 return TRUE;
5364 default:
5365 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5366 infoPtr->hwndSelf, ntfret);
5370 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5371 * to my parent for processing.
5373 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5374 POINT pt, ptorig;
5375 HDC hdc = (HDC)wParam;
5376 HWND parent;
5378 pt.x = 0;
5379 pt.y = 0;
5380 parent = GetParent(infoPtr->hwndSelf);
5381 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5382 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5383 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5384 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5386 if (!ret)
5387 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5389 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5390 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5391 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5392 tbcd.nmcd.hdc = (HDC)wParam;
5393 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5394 dwEraseCustDraw = ntfret & 0xffff;
5395 switch (dwEraseCustDraw)
5397 case CDRF_DODEFAULT:
5398 break;
5399 case CDRF_SKIPDEFAULT:
5400 return TRUE;
5401 default:
5402 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5403 infoPtr->hwndSelf, ntfret);
5406 return ret;
5410 static inline LRESULT
5411 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5413 return (LRESULT)infoPtr->hFont;
5417 static void
5418 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5420 INT i;
5421 INT nNewHotItem = infoPtr->nHotItem;
5423 for (i = 0; i < infoPtr->nNumButtons; i++)
5425 /* did we wrap? */
5426 if ((nNewHotItem + iDirection < 0) ||
5427 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5429 NMTBWRAPHOTITEM nmtbwhi;
5430 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5431 nmtbwhi.iDirection = iDirection;
5432 nmtbwhi.dwReason = dwReason;
5434 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5435 return;
5438 nNewHotItem += iDirection;
5439 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5441 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5442 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5444 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5445 break;
5450 static LRESULT
5451 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5453 NMKEY nmkey;
5455 nmkey.nVKey = (UINT)wParam;
5456 nmkey.uFlags = HIWORD(lParam);
5458 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5459 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5461 switch ((UINT)wParam)
5463 case VK_LEFT:
5464 case VK_UP:
5465 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5466 break;
5467 case VK_RIGHT:
5468 case VK_DOWN:
5469 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5470 break;
5471 case VK_SPACE:
5472 case VK_RETURN:
5473 if ((infoPtr->nHotItem >= 0) &&
5474 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5476 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5477 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5478 (LPARAM)infoPtr->hwndSelf);
5480 break;
5483 return 0;
5487 static LRESULT
5488 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5490 POINT pt;
5491 BOOL button;
5493 pt.x = (short)LOWORD(lParam);
5494 pt.y = (short)HIWORD(lParam);
5495 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5497 if (button)
5498 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5499 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5500 TOOLBAR_Customize (infoPtr);
5502 return 0;
5506 static LRESULT
5507 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5509 TBUTTON_INFO *btnPtr;
5510 POINT pt;
5511 INT nHit;
5512 NMTOOLBARA nmtb;
5513 NMMOUSE nmmouse;
5514 BOOL bDragKeyPressed;
5515 BOOL button;
5517 TRACE("\n");
5519 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5520 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5521 else
5522 bDragKeyPressed = (wParam & MK_SHIFT);
5524 if (infoPtr->hwndToolTip)
5525 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5526 WM_LBUTTONDOWN, wParam, lParam);
5528 pt.x = (short)LOWORD(lParam);
5529 pt.y = (short)HIWORD(lParam);
5530 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5532 if (button)
5534 btnPtr = &infoPtr->buttons[nHit];
5536 if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5538 infoPtr->nButtonDrag = nHit;
5539 SetCapture (infoPtr->hwndSelf);
5541 /* If drag cursor has not been loaded, load it.
5542 * Note: it doesn't need to be freed */
5543 if (!hCursorDrag)
5544 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5545 SetCursor(hCursorDrag);
5547 else
5549 RECT arrowRect;
5550 infoPtr->nOldHit = nHit;
5552 arrowRect = btnPtr->rect;
5553 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5555 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5556 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5557 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5558 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5559 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5560 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5562 LRESULT res;
5564 /* draw in pressed state */
5565 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5566 btnPtr->fsState |= TBSTATE_PRESSED;
5567 else
5568 btnPtr->bDropDownPressed = TRUE;
5569 RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5571 memset(&nmtb, 0, sizeof(nmtb));
5572 nmtb.iItem = btnPtr->idCommand;
5573 nmtb.rcButton = btnPtr->rect;
5574 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5575 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5577 if (res != TBDDRET_TREATPRESSED)
5579 MSG msg;
5581 /* redraw button in unpressed state */
5582 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5583 btnPtr->fsState &= ~TBSTATE_PRESSED;
5584 else
5585 btnPtr->bDropDownPressed = FALSE;
5586 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5588 /* find and set hot item */
5589 GetCursorPos(&pt);
5590 ScreenToClient(infoPtr->hwndSelf, &pt);
5591 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5592 if (!infoPtr->bAnchor || button)
5593 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5595 /* remove any left mouse button down or double-click messages
5596 * so that we can get a toggle effect on the button */
5597 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5598 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5601 return 0;
5603 /* otherwise drop through and process as pushed */
5605 infoPtr->bCaptured = TRUE;
5606 infoPtr->nButtonDown = nHit;
5607 infoPtr->bDragOutSent = FALSE;
5609 btnPtr->fsState |= TBSTATE_PRESSED;
5611 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5613 if (btnPtr->fsState & TBSTATE_ENABLED)
5614 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5615 UpdateWindow(infoPtr->hwndSelf);
5616 SetCapture (infoPtr->hwndSelf);
5619 memset(&nmtb, 0, sizeof(nmtb));
5620 nmtb.iItem = btnPtr->idCommand;
5621 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5624 nmmouse.dwHitInfo = nHit;
5626 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5627 if (!button)
5628 nmmouse.dwItemSpec = -1;
5629 else
5631 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5632 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5635 ClientToScreen(infoPtr->hwndSelf, &pt);
5636 nmmouse.pt = pt;
5638 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5639 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5641 return 0;
5644 static LRESULT
5645 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5647 TBUTTON_INFO *btnPtr;
5648 POINT pt;
5649 INT nHit;
5650 INT nOldIndex = -1;
5651 NMHDR hdr;
5652 NMMOUSE nmmouse;
5653 NMTOOLBARA nmtb;
5654 BOOL button;
5656 if (infoPtr->hwndToolTip)
5657 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5658 WM_LBUTTONUP, wParam, lParam);
5660 pt.x = (short)LOWORD(lParam);
5661 pt.y = (short)HIWORD(lParam);
5662 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5664 if (!infoPtr->bAnchor || button)
5665 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5667 if (infoPtr->nButtonDrag >= 0) {
5668 RECT rcClient;
5669 NMHDR hdr;
5671 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5672 ReleaseCapture();
5673 /* reset cursor */
5674 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5676 GetClientRect(infoPtr->hwndSelf, &rcClient);
5677 if (PtInRect(&rcClient, pt))
5679 INT nButton = -1;
5680 if (nHit >= 0)
5681 nButton = nHit;
5682 else if (nHit < -1)
5683 nButton = -nHit;
5684 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5685 nButton = -nHit;
5687 if (nButton == infoPtr->nButtonDrag)
5689 /* if the button is moved sightly left and we have a
5690 * separator there then remove it */
5691 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5693 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5694 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5696 else /* else insert a separator before the dragged button */
5698 TBBUTTON tbb;
5699 memset(&tbb, 0, sizeof(tbb));
5700 tbb.fsStyle = BTNS_SEP;
5701 tbb.iString = -1;
5702 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5705 else
5707 if (nButton == -1)
5709 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5710 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5711 else
5712 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5714 else
5715 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5718 else
5720 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5721 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5724 /* button under cursor changed so need to re-set hot item */
5725 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5726 infoPtr->nButtonDrag = -1;
5728 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5730 else if (infoPtr->nButtonDown >= 0) {
5731 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5732 btnPtr->fsState &= ~TBSTATE_PRESSED;
5734 if (btnPtr->fsStyle & BTNS_CHECK) {
5735 if (btnPtr->fsStyle & BTNS_GROUP) {
5736 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5737 nHit);
5738 if ((nOldIndex != nHit) &&
5739 (nOldIndex != -1))
5740 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5741 btnPtr->fsState |= TBSTATE_CHECKED;
5743 else {
5744 if (btnPtr->fsState & TBSTATE_CHECKED)
5745 btnPtr->fsState &= ~TBSTATE_CHECKED;
5746 else
5747 btnPtr->fsState |= TBSTATE_CHECKED;
5751 if (nOldIndex != -1)
5752 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5755 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5756 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5757 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5759 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5760 ReleaseCapture ();
5761 infoPtr->nButtonDown = -1;
5763 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5764 TOOLBAR_SendNotify (&hdr, infoPtr,
5765 NM_RELEASEDCAPTURE);
5767 memset(&nmtb, 0, sizeof(nmtb));
5768 nmtb.iItem = btnPtr->idCommand;
5769 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5770 TBN_ENDDRAG);
5772 if (btnPtr->fsState & TBSTATE_ENABLED)
5774 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5775 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5777 /* In case we have just been destroyed... */
5778 if(!IsWindow(infoPtr->hwndSelf))
5779 return 0;
5783 /* !!! Undocumented - toolbar at 4.71 level and above sends
5784 * NM_CLICK with the NMMOUSE structure. */
5785 nmmouse.dwHitInfo = nHit;
5787 if (!button)
5788 nmmouse.dwItemSpec = -1;
5789 else
5791 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5792 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5795 ClientToScreen(infoPtr->hwndSelf, &pt);
5796 nmmouse.pt = pt;
5798 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5799 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5801 return 0;
5804 static LRESULT
5805 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5807 INT nHit;
5808 NMMOUSE nmmouse;
5809 POINT pt;
5810 BOOL button;
5812 pt.x = (short)LOWORD(lParam);
5813 pt.y = (short)HIWORD(lParam);
5815 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5816 nmmouse.dwHitInfo = nHit;
5818 if (!button) {
5819 nmmouse.dwItemSpec = -1;
5820 } else {
5821 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5822 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5825 ClientToScreen(infoPtr->hwndSelf, &pt);
5826 nmmouse.pt = pt;
5828 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5829 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5831 return 0;
5834 static LRESULT
5835 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5837 NMHDR nmhdr;
5839 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5840 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5842 return 0;
5845 static LRESULT
5846 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5848 TBUTTON_INFO *btnPtr;
5850 infoPtr->bCaptured = FALSE;
5852 if (infoPtr->nButtonDown >= 0)
5854 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5855 btnPtr->fsState &= ~TBSTATE_PRESSED;
5857 infoPtr->nOldHit = -1;
5859 if (btnPtr->fsState & TBSTATE_ENABLED)
5860 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5862 return 0;
5865 static LRESULT
5866 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5868 /* don't remove hot effects when in anchor highlighting mode or when a
5869 * drop-down button is pressed */
5870 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5872 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5873 if (!hotBtnPtr->bDropDownPressed)
5874 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5877 if (infoPtr->nOldHit < 0)
5878 return TRUE;
5880 /* If the last button we were over is depressed then make it not */
5881 /* depressed and redraw it */
5882 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5884 TBUTTON_INFO *btnPtr;
5885 RECT rc1;
5887 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5889 btnPtr->fsState &= ~TBSTATE_PRESSED;
5891 rc1 = btnPtr->rect;
5892 InflateRect (&rc1, 1, 1);
5893 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5896 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5898 NMTOOLBARW nmt;
5899 ZeroMemory(&nmt, sizeof(nmt));
5900 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5901 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5902 infoPtr->bDragOutSent = TRUE;
5905 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5907 return TRUE;
5910 static LRESULT
5911 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5913 POINT pt;
5914 TRACKMOUSEEVENT trackinfo;
5915 INT nHit;
5916 TBUTTON_INFO *btnPtr;
5917 BOOL button;
5919 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5920 TOOLBAR_TooltipCreateControl(infoPtr);
5922 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5923 /* fill in the TRACKMOUSEEVENT struct */
5924 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5925 trackinfo.dwFlags = TME_QUERY;
5927 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5928 _TrackMouseEvent(&trackinfo);
5930 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5931 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5932 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5933 trackinfo.hwndTrack = infoPtr->hwndSelf;
5935 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5936 /* and can properly deactivate the hot toolbar button */
5937 _TrackMouseEvent(&trackinfo);
5941 if (infoPtr->hwndToolTip)
5942 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5943 WM_MOUSEMOVE, wParam, lParam);
5945 pt.x = (short)LOWORD(lParam);
5946 pt.y = (short)HIWORD(lParam);
5948 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5950 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5951 && (!infoPtr->bAnchor || button))
5952 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5954 if (infoPtr->nOldHit != nHit)
5956 if (infoPtr->bCaptured)
5958 if (!infoPtr->bDragOutSent)
5960 NMTOOLBARW nmt;
5961 ZeroMemory(&nmt, sizeof(nmt));
5962 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5963 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5964 infoPtr->bDragOutSent = TRUE;
5967 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5968 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5969 btnPtr->fsState &= ~TBSTATE_PRESSED;
5970 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5972 else if (nHit == infoPtr->nButtonDown) {
5973 btnPtr->fsState |= TBSTATE_PRESSED;
5974 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5976 infoPtr->nOldHit = nHit;
5980 return 0;
5984 static inline LRESULT
5985 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5987 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
5988 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5989 /* else */
5990 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5994 static inline LRESULT
5995 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
5997 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
5998 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6000 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6004 static LRESULT
6005 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
6007 TOOLBAR_INFO *infoPtr;
6008 DWORD styleadd = 0;
6010 /* allocate memory for info structure */
6011 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6012 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6014 /* paranoid!! */
6015 infoPtr->dwStructSize = sizeof(TBBUTTON);
6016 infoPtr->nRows = 1;
6018 /* initialize info structure */
6019 infoPtr->nButtonWidth = 23;
6020 infoPtr->nButtonHeight = 22;
6021 infoPtr->nBitmapHeight = 16;
6022 infoPtr->nBitmapWidth = 16;
6024 infoPtr->nMaxTextRows = 1;
6025 infoPtr->cxMin = -1;
6026 infoPtr->cxMax = -1;
6027 infoPtr->nNumBitmaps = 0;
6028 infoPtr->nNumStrings = 0;
6030 infoPtr->bCaptured = FALSE;
6031 infoPtr->nButtonDown = -1;
6032 infoPtr->nButtonDrag = -1;
6033 infoPtr->nOldHit = -1;
6034 infoPtr->nHotItem = -1;
6035 infoPtr->hwndNotify = lpcs->hwndParent;
6036 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6037 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
6038 infoPtr->bDragOutSent = FALSE;
6039 infoPtr->iVersion = 0;
6040 infoPtr->hwndSelf = hwnd;
6041 infoPtr->bDoRedraw = TRUE;
6042 infoPtr->clrBtnHighlight = CLR_DEFAULT;
6043 infoPtr->clrBtnShadow = CLR_DEFAULT;
6044 infoPtr->szPadding.cx = DEFPAD_CX;
6045 infoPtr->szPadding.cy = DEFPAD_CY;
6046 infoPtr->iListGap = DEFLISTGAP;
6047 infoPtr->iTopMargin = default_top_margin(infoPtr);
6048 infoPtr->dwStyle = lpcs->style;
6049 infoPtr->tbim.iButton = -1;
6051 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6052 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6053 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6054 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6057 /* I think the code below is a bug, but it is the way that the native
6058 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6059 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6060 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6061 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6062 * Somehow, the only cases of this seem to be MFC programs.
6064 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6065 * does not occur in the WM_STYLECHANGING routine.
6066 * (Guy Albertelli 9/2001)
6069 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
6070 && !(lpcs->style & TBSTYLE_TRANSPARENT))
6071 styleadd |= TBSTYLE_TRANSPARENT;
6072 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6073 styleadd |= CCS_TOP; /* default to top */
6074 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6077 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
6081 static LRESULT
6082 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6084 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6085 RECT rcWindow;
6086 HDC hdc;
6088 if (dwStyle & WS_MINIMIZE)
6089 return 0; /* Nothing to do */
6091 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6093 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6094 return 0;
6096 if (!(dwStyle & CCS_NODIVIDER))
6098 GetWindowRect (hwnd, &rcWindow);
6099 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6100 if( dwStyle & WS_BORDER )
6101 InflateRect (&rcWindow, -1, -1);
6102 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6105 ReleaseDC( hwnd, hdc );
6107 return 0;
6111 /* handles requests from the tooltip control on what text to display */
6112 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6114 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6116 TRACE("button index = %d\n", index);
6118 Free (infoPtr->pszTooltipText);
6119 infoPtr->pszTooltipText = NULL;
6121 if (index < 0)
6122 return 0;
6124 if (infoPtr->bUnicode)
6126 WCHAR wszBuffer[INFOTIPSIZE+1];
6127 NMTBGETINFOTIPW tbgit;
6128 unsigned int len; /* in chars */
6130 wszBuffer[0] = '\0';
6131 wszBuffer[INFOTIPSIZE] = '\0';
6133 tbgit.pszText = wszBuffer;
6134 tbgit.cchTextMax = INFOTIPSIZE;
6135 tbgit.iItem = lpnmtdi->hdr.idFrom;
6136 tbgit.lParam = infoPtr->buttons[index].dwData;
6138 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6140 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6142 len = strlenW(tbgit.pszText);
6143 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6145 /* need to allocate temporary buffer in infoPtr as there
6146 * isn't enough space in buffer passed to us by the
6147 * tooltip control */
6148 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6149 if (infoPtr->pszTooltipText)
6151 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6152 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6153 return 0;
6156 else if (len > 0)
6158 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6159 return 0;
6162 else
6164 CHAR szBuffer[INFOTIPSIZE+1];
6165 NMTBGETINFOTIPA tbgit;
6166 unsigned int len; /* in chars */
6168 szBuffer[0] = '\0';
6169 szBuffer[INFOTIPSIZE] = '\0';
6171 tbgit.pszText = szBuffer;
6172 tbgit.cchTextMax = INFOTIPSIZE;
6173 tbgit.iItem = lpnmtdi->hdr.idFrom;
6174 tbgit.lParam = infoPtr->buttons[index].dwData;
6176 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6178 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6180 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6181 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6183 /* need to allocate temporary buffer in infoPtr as there
6184 * isn't enough space in buffer passed to us by the
6185 * tooltip control */
6186 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6187 if (infoPtr->pszTooltipText)
6189 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6190 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6191 return 0;
6194 else if (tbgit.pszText && tbgit.pszText[0])
6196 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6197 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6198 return 0;
6202 /* if button has text, but it is not shown then automatically
6203 * use that text as tooltip */
6204 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6205 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6207 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6208 unsigned int len = pszText ? strlenW(pszText) : 0;
6210 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6212 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6214 /* need to allocate temporary buffer in infoPtr as there
6215 * isn't enough space in buffer passed to us by the
6216 * tooltip control */
6217 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6218 if (infoPtr->pszTooltipText)
6220 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6221 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6222 return 0;
6225 else if (len > 0)
6227 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6228 return 0;
6232 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6234 /* last resort: send notification on to app */
6235 /* FIXME: find out what is really used here */
6236 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6240 static inline LRESULT
6241 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6243 switch (lpnmh->code)
6245 case PGN_CALCSIZE:
6247 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6249 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6250 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6251 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6252 lppgc->iWidth);
6254 else {
6255 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6256 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6257 lppgc->iHeight);
6259 return 0;
6262 case PGN_SCROLL:
6264 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6266 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6267 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6268 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6269 lppgs->iScroll, lppgs->iDir);
6270 return 0;
6273 case TTN_GETDISPINFOW:
6274 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6276 case TTN_GETDISPINFOA:
6277 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6278 return 0;
6280 default:
6281 return 0;
6286 static LRESULT
6287 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6289 LRESULT format;
6291 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6293 if (lParam == NF_QUERY)
6294 return NFR_UNICODE;
6296 if (lParam == NF_REQUERY) {
6297 format = SendMessageW(infoPtr->hwndNotify,
6298 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6299 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6300 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6301 format);
6302 format = NFR_ANSI;
6304 return format;
6306 return 0;
6310 static LRESULT
6311 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6313 HDC hdc;
6314 PAINTSTRUCT ps;
6316 /* fill ps.rcPaint with a default rect */
6317 ps.rcPaint = infoPtr->rcBound;
6319 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6321 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6323 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6324 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6326 return 0;
6330 static LRESULT
6331 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6333 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6335 /* make first item hot */
6336 if (infoPtr->nNumButtons > 0)
6337 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6339 return 0;
6342 static LRESULT
6343 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6345 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6347 if (hFont == 0)
6348 infoPtr->hFont = infoPtr->hDefaultFont;
6349 else
6350 infoPtr->hFont = hFont;
6352 TOOLBAR_CalcToolbar(infoPtr);
6354 if (Redraw)
6355 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6356 return 1;
6359 static LRESULT
6360 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6361 /*****************************************************
6363 * Function;
6364 * Handles the WM_SETREDRAW message.
6366 * Documentation:
6367 * According to testing V4.71 of COMCTL32 returns the
6368 * *previous* status of the redraw flag (either 0 or 1)
6369 * instead of the MSDN documented value of 0 if handled.
6370 * (For laughs see the "consistency" with same function
6371 * in rebar.)
6373 *****************************************************/
6375 BOOL oldredraw = infoPtr->bDoRedraw;
6377 TRACE("set to %s\n",
6378 (wParam) ? "TRUE" : "FALSE");
6379 infoPtr->bDoRedraw = (BOOL) wParam;
6380 if (wParam) {
6381 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6383 return (oldredraw) ? 1 : 0;
6387 static LRESULT
6388 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6390 TRACE("sizing toolbar!\n");
6392 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6394 RECT delta_width, delta_height, client, dummy;
6395 DWORD min_x, max_x, min_y, max_y;
6396 TBUTTON_INFO *btnPtr;
6397 INT i;
6399 GetClientRect(infoPtr->hwndSelf, &client);
6400 if(client.right > infoPtr->client_rect.right)
6402 min_x = infoPtr->client_rect.right;
6403 max_x = client.right;
6405 else
6407 max_x = infoPtr->client_rect.right;
6408 min_x = client.right;
6410 if(client.bottom > infoPtr->client_rect.bottom)
6412 min_y = infoPtr->client_rect.bottom;
6413 max_y = client.bottom;
6415 else
6417 max_y = infoPtr->client_rect.bottom;
6418 min_y = client.bottom;
6421 SetRect(&delta_width, min_x, 0, max_x, min_y);
6422 SetRect(&delta_height, 0, min_y, max_x, max_y);
6424 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6425 btnPtr = infoPtr->buttons;
6426 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6427 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6428 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6429 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6431 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6432 TOOLBAR_AutoSize(infoPtr);
6433 return 0;
6437 static LRESULT
6438 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6440 if (nType == GWL_STYLE)
6441 return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6443 return 0;
6447 static LRESULT
6448 TOOLBAR_SysColorChange (void)
6450 COMCTL32_RefreshSysColors();
6452 return 0;
6456 /* update theme after a WM_THEMECHANGED message */
6457 static LRESULT theme_changed (HWND hwnd)
6459 HTHEME theme = GetWindowTheme (hwnd);
6460 CloseThemeData (theme);
6461 OpenThemeData (hwnd, themeClass);
6462 return 0;
6466 static LRESULT WINAPI
6467 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6469 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6471 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6472 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6474 if (!infoPtr && (uMsg != WM_NCCREATE))
6475 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6477 switch (uMsg)
6479 case TB_ADDBITMAP:
6480 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6482 case TB_ADDBUTTONSA:
6483 case TB_ADDBUTTONSW:
6484 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6485 uMsg == TB_ADDBUTTONSW);
6486 case TB_ADDSTRINGA:
6487 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6489 case TB_ADDSTRINGW:
6490 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6492 case TB_AUTOSIZE:
6493 return TOOLBAR_AutoSize (infoPtr);
6495 case TB_BUTTONCOUNT:
6496 return TOOLBAR_ButtonCount (infoPtr);
6498 case TB_BUTTONSTRUCTSIZE:
6499 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6501 case TB_CHANGEBITMAP:
6502 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6504 case TB_CHECKBUTTON:
6505 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6507 case TB_COMMANDTOINDEX:
6508 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6510 case TB_CUSTOMIZE:
6511 return TOOLBAR_Customize (infoPtr);
6513 case TB_DELETEBUTTON:
6514 return TOOLBAR_DeleteButton (infoPtr, wParam);
6516 case TB_ENABLEBUTTON:
6517 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6519 case TB_GETANCHORHIGHLIGHT:
6520 return TOOLBAR_GetAnchorHighlight (infoPtr);
6522 case TB_GETBITMAP:
6523 return TOOLBAR_GetBitmap (infoPtr, wParam);
6525 case TB_GETBITMAPFLAGS:
6526 return TOOLBAR_GetBitmapFlags ();
6528 case TB_GETBUTTON:
6529 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6531 case TB_GETBUTTONINFOA:
6532 case TB_GETBUTTONINFOW:
6533 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6534 uMsg == TB_GETBUTTONINFOW);
6535 case TB_GETBUTTONSIZE:
6536 return TOOLBAR_GetButtonSize (infoPtr);
6538 case TB_GETBUTTONTEXTA:
6539 case TB_GETBUTTONTEXTW:
6540 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6541 uMsg == TB_GETBUTTONTEXTW);
6543 case TB_GETDISABLEDIMAGELIST:
6544 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6546 case TB_GETEXTENDEDSTYLE:
6547 return TOOLBAR_GetExtendedStyle (infoPtr);
6549 case TB_GETHOTIMAGELIST:
6550 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6552 case TB_GETHOTITEM:
6553 return TOOLBAR_GetHotItem (infoPtr);
6555 case TB_GETIMAGELIST:
6556 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6558 case TB_GETINSERTMARK:
6559 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6561 case TB_GETINSERTMARKCOLOR:
6562 return TOOLBAR_GetInsertMarkColor (infoPtr);
6564 case TB_GETITEMRECT:
6565 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6567 case TB_GETMAXSIZE:
6568 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6570 /* case TB_GETOBJECT: */ /* 4.71 */
6572 case TB_GETPADDING:
6573 return TOOLBAR_GetPadding (infoPtr);
6575 case TB_GETRECT:
6576 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6578 case TB_GETROWS:
6579 return TOOLBAR_GetRows (infoPtr);
6581 case TB_GETSTATE:
6582 return TOOLBAR_GetState (infoPtr, wParam);
6584 case TB_GETSTRINGA:
6585 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6587 case TB_GETSTRINGW:
6588 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6590 case TB_GETSTYLE:
6591 return TOOLBAR_GetStyle (infoPtr);
6593 case TB_GETTEXTROWS:
6594 return TOOLBAR_GetTextRows (infoPtr);
6596 case TB_GETTOOLTIPS:
6597 return TOOLBAR_GetToolTips (infoPtr);
6599 case TB_GETUNICODEFORMAT:
6600 return TOOLBAR_GetUnicodeFormat (infoPtr);
6602 case TB_HIDEBUTTON:
6603 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6605 case TB_HITTEST:
6606 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6608 case TB_INDETERMINATE:
6609 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6611 case TB_INSERTBUTTONA:
6612 case TB_INSERTBUTTONW:
6613 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6614 uMsg == TB_INSERTBUTTONW);
6616 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6618 case TB_ISBUTTONCHECKED:
6619 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6621 case TB_ISBUTTONENABLED:
6622 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6624 case TB_ISBUTTONHIDDEN:
6625 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6627 case TB_ISBUTTONHIGHLIGHTED:
6628 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6630 case TB_ISBUTTONINDETERMINATE:
6631 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6633 case TB_ISBUTTONPRESSED:
6634 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6636 case TB_LOADIMAGES:
6637 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6639 case TB_MAPACCELERATORA:
6640 case TB_MAPACCELERATORW:
6641 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6643 case TB_MARKBUTTON:
6644 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6646 case TB_MOVEBUTTON:
6647 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6649 case TB_PRESSBUTTON:
6650 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6652 case TB_REPLACEBITMAP:
6653 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6655 case TB_SAVERESTOREA:
6656 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6658 case TB_SAVERESTOREW:
6659 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6661 case TB_SETANCHORHIGHLIGHT:
6662 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6664 case TB_SETBITMAPSIZE:
6665 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6667 case TB_SETBUTTONINFOA:
6668 case TB_SETBUTTONINFOW:
6669 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6670 uMsg == TB_SETBUTTONINFOW);
6671 case TB_SETBUTTONSIZE:
6672 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6674 case TB_SETBUTTONWIDTH:
6675 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6677 case TB_SETCMDID:
6678 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6680 case TB_SETDISABLEDIMAGELIST:
6681 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6683 case TB_SETDRAWTEXTFLAGS:
6684 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6686 case TB_SETEXTENDEDSTYLE:
6687 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6689 case TB_SETHOTIMAGELIST:
6690 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6692 case TB_SETHOTITEM:
6693 return TOOLBAR_SetHotItem (infoPtr, wParam);
6695 case TB_SETIMAGELIST:
6696 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6698 case TB_SETINDENT:
6699 return TOOLBAR_SetIndent (infoPtr, wParam);
6701 case TB_SETINSERTMARK:
6702 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6704 case TB_SETINSERTMARKCOLOR:
6705 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6707 case TB_SETMAXTEXTROWS:
6708 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6710 case TB_SETPADDING:
6711 return TOOLBAR_SetPadding (infoPtr, lParam);
6713 case TB_SETPARENT:
6714 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6716 case TB_SETROWS:
6717 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6719 case TB_SETSTATE:
6720 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6722 case TB_SETSTYLE:
6723 return TOOLBAR_SetStyle (infoPtr, lParam);
6725 case TB_SETTOOLTIPS:
6726 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6728 case TB_SETUNICODEFORMAT:
6729 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6731 case TB_SETBOUNDINGSIZE:
6732 return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6734 case TB_SETHOTITEM2:
6735 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6737 case TB_SETLISTGAP:
6738 return TOOLBAR_SetListGap(infoPtr, wParam);
6740 case TB_GETIMAGELISTCOUNT:
6741 return TOOLBAR_GetImageListCount(infoPtr);
6743 case TB_GETIDEALSIZE:
6744 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6746 case TB_UNKWN464:
6747 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6749 /* Common Control Messages */
6751 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6752 case CCM_GETCOLORSCHEME:
6753 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6755 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6756 case CCM_SETCOLORSCHEME:
6757 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6759 case CCM_GETVERSION:
6760 return TOOLBAR_GetVersion (infoPtr);
6762 case CCM_SETVERSION:
6763 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6766 /* case WM_CHAR: */
6768 case WM_CREATE:
6769 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6771 case WM_DESTROY:
6772 return TOOLBAR_Destroy (infoPtr);
6774 case WM_ERASEBKGND:
6775 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6777 case WM_GETFONT:
6778 return TOOLBAR_GetFont (infoPtr);
6780 case WM_KEYDOWN:
6781 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6783 /* case WM_KILLFOCUS: */
6785 case WM_LBUTTONDBLCLK:
6786 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6788 case WM_LBUTTONDOWN:
6789 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6791 case WM_LBUTTONUP:
6792 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6794 case WM_RBUTTONUP:
6795 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6797 case WM_RBUTTONDBLCLK:
6798 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6800 case WM_MOUSEMOVE:
6801 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6803 case WM_MOUSELEAVE:
6804 return TOOLBAR_MouseLeave (infoPtr);
6806 case WM_CAPTURECHANGED:
6807 if (hwnd == (HWND)lParam) return 0;
6808 return TOOLBAR_CaptureChanged(infoPtr);
6810 case WM_NCACTIVATE:
6811 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6813 case WM_NCCALCSIZE:
6814 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6816 case WM_NCCREATE:
6817 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6819 case WM_NCPAINT:
6820 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6822 case WM_NOTIFY:
6823 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6825 case WM_NOTIFYFORMAT:
6826 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6828 case WM_PRINTCLIENT:
6829 case WM_PAINT:
6830 return TOOLBAR_Paint (infoPtr, wParam);
6832 case WM_SETFOCUS:
6833 return TOOLBAR_SetFocus (infoPtr);
6835 case WM_SETFONT:
6836 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6838 case WM_SETREDRAW:
6839 return TOOLBAR_SetRedraw (infoPtr, wParam);
6841 case WM_SIZE:
6842 return TOOLBAR_Size (infoPtr);
6844 case WM_STYLECHANGED:
6845 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6847 case WM_SYSCOLORCHANGE:
6848 return TOOLBAR_SysColorChange ();
6850 case WM_THEMECHANGED:
6851 return theme_changed (hwnd);
6853 /* case WM_WININICHANGE: */
6855 case WM_CHARTOITEM:
6856 case WM_COMMAND:
6857 case WM_DRAWITEM:
6858 case WM_MEASUREITEM:
6859 case WM_VKEYTOITEM:
6860 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6862 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6863 case PGM_FORWARDMOUSE:
6864 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6866 default:
6867 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6868 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6869 uMsg, wParam, lParam);
6870 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6875 VOID
6876 TOOLBAR_Register (void)
6878 WNDCLASSW wndClass;
6880 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6881 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6882 wndClass.lpfnWndProc = ToolbarWindowProc;
6883 wndClass.cbClsExtra = 0;
6884 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6885 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6886 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6887 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6889 RegisterClassW (&wndClass);
6893 VOID
6894 TOOLBAR_Unregister (void)
6896 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6899 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6901 HIMAGELIST himlold;
6902 PIMLENTRY c = NULL;
6904 /* Check if the entry already exists */
6905 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6907 /* If this is a new entry we must create it and insert into the array */
6908 if (!c)
6910 PIMLENTRY *pnies;
6912 c = Alloc(sizeof(IMLENTRY));
6913 c->id = id;
6915 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6916 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
6917 pnies[*cies] = c;
6918 (*cies)++;
6920 Free(*pies);
6921 *pies = pnies;
6924 himlold = c->himl;
6925 c->himl = himl;
6927 return himlold;
6931 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
6933 int i;
6935 for (i = 0; i < *cies; i++)
6936 Free((*pies)[i]);
6938 Free(*pies);
6940 *cies = 0;
6941 *pies = NULL;
6945 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6947 PIMLENTRY c = NULL;
6949 if (pies != NULL)
6951 int i;
6953 for (i = 0; i < cies; i++)
6955 if (pies[i]->id == id)
6957 c = pies[i];
6958 break;
6963 return c;
6967 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6969 HIMAGELIST himlDef = 0;
6970 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6972 if (pie)
6973 himlDef = pie->himl;
6975 return himlDef;
6979 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6981 if (infoPtr->bUnicode)
6982 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6983 else
6985 CHAR Buffer[256];
6986 NMTOOLBARA nmtba;
6987 BOOL bRet = FALSE;
6989 nmtba.iItem = nmtb->iItem;
6990 nmtba.pszText = Buffer;
6991 nmtba.cchText = 256;
6992 ZeroMemory(nmtba.pszText, nmtba.cchText);
6994 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
6996 int ccht = strlen(nmtba.pszText);
6997 if (ccht)
6998 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
6999 nmtb->pszText, nmtb->cchText);
7001 nmtb->tbButton = nmtba.tbButton;
7002 bRet = TRUE;
7005 return bRet;
7010 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7012 NMTOOLBARW nmtb;
7014 /* MSDN states that iItem is the index of the button, rather than the
7015 * command ID as used by every other NMTOOLBAR notification */
7016 nmtb.iItem = iItem;
7017 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7019 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);