wined3d: Only set GL_DEPTH_TEXTURE_MODE_ARB if ARB_depth_texture is supported.
[wine.git] / dlls / comctl32 / toolbar.c
blob9121266306a453619c5cdf6d2e6bfa6b322fe2d1
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 CopyRect (&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 CopyRect(&rcText, &rc);
933 CopyRect(&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 rcText.left += GetSystemMetrics(SM_CXEDGE);
961 rcText.right -= GetSystemMetrics(SM_CXEDGE);
962 if (dwStyle & TBSTYLE_LIST)
964 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
966 else
968 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
969 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
970 else
971 rcText.top += infoPtr->szPadding.cy/2 + 2;
975 /* Initialize fields in all cases, because we use these later
976 * NOTE: applications can and do alter these to customize their
977 * toolbars */
978 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
979 tbcd.clrText = comctl32_color.clrBtnText;
980 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
981 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
982 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
983 tbcd.clrMark = comctl32_color.clrHighlight;
984 tbcd.clrHighlightHotTrack = 0;
985 tbcd.nStringBkMode = TRANSPARENT;
986 tbcd.nHLStringBkMode = OPAQUE;
987 tbcd.rcText.left = 0;
988 tbcd.rcText.top = 0;
989 tbcd.rcText.right = rcText.right - rc.left;
990 tbcd.rcText.bottom = rcText.bottom - rc.top;
991 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
992 tbcd.nmcd.hdc = hdc;
993 tbcd.nmcd.rc = btnPtr->rect;
994 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
996 /* FIXME: what are these used for? */
997 tbcd.hbrLines = 0;
998 tbcd.hpenLines = 0;
1000 /* Issue Item Prepaint notify */
1001 dwItemCustDraw = 0;
1002 dwItemCDFlag = 0;
1003 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1005 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1006 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1007 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1008 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1009 /* reset these fields so the user can't alter the behaviour like native */
1010 tbcd.nmcd.hdc = hdc;
1011 tbcd.nmcd.rc = btnPtr->rect;
1013 dwItemCustDraw = ntfret & 0xffff;
1014 dwItemCDFlag = ntfret & 0xffff0000;
1015 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1016 return;
1017 /* save the only part of the rect that the user can change */
1018 rcText.right = tbcd.rcText.right + rc.left;
1019 rcText.bottom = tbcd.rcText.bottom + rc.top;
1022 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1023 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1024 OffsetRect(&rcText, 1, 1);
1026 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
1027 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1028 TOOLBAR_DrawPattern (&rc, &tbcd);
1030 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
1031 && (tbcd.nmcd.uItemState & CDIS_HOT))
1033 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1035 COLORREF oldclr;
1037 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1038 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1039 if (hasDropDownArrow)
1040 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1041 SetBkColor(hdc, oldclr);
1045 if (theme)
1047 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1048 int stateId = TS_NORMAL;
1050 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1051 stateId = TS_DISABLED;
1052 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1053 stateId = TS_PRESSED;
1054 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1055 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1056 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1057 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1058 stateId = TS_HOT;
1060 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1062 else
1063 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1065 if (drawSepDropDownArrow)
1067 if (theme)
1069 int stateId = TS_NORMAL;
1071 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1072 stateId = TS_DISABLED;
1073 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1074 stateId = TS_PRESSED;
1075 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1076 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1077 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1078 stateId = TS_HOT;
1080 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1081 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1083 else
1084 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1087 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1088 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1089 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1090 SetBkMode (hdc, oldBkMode);
1092 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1094 if (hasDropDownArrow && !drawSepDropDownArrow)
1096 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1098 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1099 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1101 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1103 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1104 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1106 else
1107 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1110 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1112 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1113 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1119 static void
1120 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1122 TBUTTON_INFO *btnPtr;
1123 INT i;
1124 RECT rcTemp, rcClient;
1125 NMTBCUSTOMDRAW tbcd;
1126 DWORD ntfret;
1127 DWORD dwBaseCustDraw;
1129 /* the app has told us not to redraw the toolbar */
1130 if (!infoPtr->bDoRedraw)
1131 return;
1133 /* if imagelist belongs to the app, it can be changed
1134 by the app after setting it */
1135 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1137 infoPtr->nNumBitmaps = 0;
1138 for (i = 0; i < infoPtr->cimlDef; i++)
1139 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1142 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1144 /* change the imagelist icon size if we manage the list and it is necessary */
1145 TOOLBAR_CheckImageListIconSize(infoPtr);
1147 /* Send initial notify */
1148 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1149 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1150 tbcd.nmcd.hdc = hdc;
1151 tbcd.nmcd.rc = ps->rcPaint;
1152 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1153 dwBaseCustDraw = ntfret & 0xffff;
1155 GetClientRect(infoPtr->hwndSelf, &rcClient);
1157 /* redraw necessary buttons */
1158 btnPtr = infoPtr->buttons;
1159 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1161 BOOL bDraw;
1162 if (!RectVisible(hdc, &btnPtr->rect))
1163 continue;
1164 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1166 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1167 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1169 else
1170 bDraw = TRUE;
1171 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1172 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1173 if (bDraw)
1174 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1177 /* draw insert mark if required */
1178 if (infoPtr->tbim.iButton != -1)
1180 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1181 RECT rcInsertMark;
1182 rcInsertMark.top = rcButton.top;
1183 rcInsertMark.bottom = rcButton.bottom;
1184 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1185 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1186 else
1187 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1188 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1191 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1193 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1194 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1195 tbcd.nmcd.hdc = hdc;
1196 tbcd.nmcd.rc = ps->rcPaint;
1197 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1201 /***********************************************************************
1202 * TOOLBAR_MeasureString
1204 * This function gets the width and height of a string in pixels. This
1205 * is done first by using GetTextExtentPoint to get the basic width
1206 * and height. The DrawText is called with DT_CALCRECT to get the exact
1207 * width. The reason is because the text may have more than one "&" (or
1208 * prefix characters as M$ likes to call them). The prefix character
1209 * indicates where the underline goes, except for the string "&&" which
1210 * is reduced to a single "&". GetTextExtentPoint does not process these
1211 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1213 static void
1214 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1215 HDC hdc, LPSIZE lpSize)
1217 RECT myrect;
1219 lpSize->cx = 0;
1220 lpSize->cy = 0;
1222 if (infoPtr->nMaxTextRows > 0 &&
1223 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1224 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1225 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1227 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1229 if(lpText != NULL) {
1230 /* first get size of all the text */
1231 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1233 /* feed above size into the rectangle for DrawText */
1234 myrect.left = myrect.top = 0;
1235 myrect.right = lpSize->cx;
1236 myrect.bottom = lpSize->cy;
1238 /* Use DrawText to get true size as drawn (less pesky "&") */
1239 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1240 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1241 DT_NOPREFIX : 0));
1243 /* feed back to caller */
1244 lpSize->cx = myrect.right;
1245 lpSize->cy = myrect.bottom;
1249 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1252 /***********************************************************************
1253 * TOOLBAR_CalcStrings
1255 * This function walks through each string and measures it and returns
1256 * the largest height and width to caller.
1258 static void
1259 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1261 TBUTTON_INFO *btnPtr;
1262 INT i;
1263 SIZE sz;
1264 HDC hdc;
1265 HFONT hOldFont;
1267 lpSize->cx = 0;
1268 lpSize->cy = 0;
1270 if (infoPtr->nMaxTextRows == 0)
1271 return;
1273 hdc = GetDC (infoPtr->hwndSelf);
1274 hOldFont = SelectObject (hdc, infoPtr->hFont);
1276 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1278 TEXTMETRICW tm;
1280 GetTextMetricsW(hdc, &tm);
1281 lpSize->cy = tm.tmHeight;
1284 btnPtr = infoPtr->buttons;
1285 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1286 if(TOOLBAR_GetText(infoPtr, btnPtr))
1288 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1289 if (sz.cx > lpSize->cx)
1290 lpSize->cx = sz.cx;
1291 if (sz.cy > lpSize->cy)
1292 lpSize->cy = sz.cy;
1296 SelectObject (hdc, hOldFont);
1297 ReleaseDC (infoPtr->hwndSelf, hdc);
1299 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1302 /***********************************************************************
1303 * TOOLBAR_WrapToolbar
1305 * This function walks through the buttons and separators in the
1306 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1307 * wrapping should occur based on the width of the toolbar window.
1308 * It does *not* calculate button placement itself. That task
1309 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1310 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1311 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1313 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1314 * vertical toolbar lists.
1317 static void
1318 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1320 TBUTTON_INFO *btnPtr;
1321 INT x, cx, i, j, width;
1322 BOOL bButtonWrap;
1324 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1325 /* no layout is necessary. Applications may use this style */
1326 /* to perform their own layout on the toolbar. */
1327 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1328 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1330 btnPtr = infoPtr->buttons;
1331 x = infoPtr->nIndent;
1332 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1334 bButtonWrap = FALSE;
1336 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1337 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1338 infoPtr->nIndent);
1340 for (i = 0; i < infoPtr->nNumButtons; i++ )
1342 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1344 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1345 continue;
1347 if (btnPtr[i].cx > 0)
1348 cx = btnPtr[i].cx;
1349 /* horizontal separators are treated as buttons for width */
1350 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1351 !(infoPtr->dwStyle & CCS_VERT))
1352 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1353 else
1354 cx = infoPtr->nButtonWidth;
1356 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1357 cx += DDARROW_WIDTH;
1359 /* Two or more adjacent separators form a separator group. */
1360 /* The first separator in a group should be wrapped to the */
1361 /* next row if the previous wrapping is on a button. */
1362 if( bButtonWrap &&
1363 (btnPtr[i].fsStyle & BTNS_SEP) &&
1364 (i + 1 < infoPtr->nNumButtons ) &&
1365 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1367 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1368 btnPtr[i].fsState |= TBSTATE_WRAP;
1369 x = infoPtr->nIndent;
1370 i++;
1371 bButtonWrap = FALSE;
1372 continue;
1375 /* The layout makes sure the bitmap is visible, but not the button. */
1376 /* Test added to also wrap after a button that starts a row but */
1377 /* is bigger than the area. - GA 8/01 */
1378 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1379 ((x == infoPtr->nIndent) && (cx > width)))
1381 BOOL bFound = FALSE;
1383 /* If the current button is a separator and not hidden, */
1384 /* go to the next until it reaches a non separator. */
1385 /* Wrap the last separator if it is before a button. */
1386 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1387 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1388 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1389 i < infoPtr->nNumButtons )
1391 i++;
1392 bFound = TRUE;
1395 if( bFound && i < infoPtr->nNumButtons )
1397 i--;
1398 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1399 i, btnPtr[i].fsStyle, x, cx);
1400 btnPtr[i].fsState |= TBSTATE_WRAP;
1401 x = infoPtr->nIndent;
1402 bButtonWrap = FALSE;
1403 continue;
1405 else if ( i >= infoPtr->nNumButtons)
1406 break;
1408 /* If the current button is not a separator, find the last */
1409 /* separator and wrap it. */
1410 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1412 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1413 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1415 bFound = TRUE;
1416 i = j;
1417 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1418 i, btnPtr[i].fsStyle, x, cx);
1419 x = infoPtr->nIndent;
1420 btnPtr[j].fsState |= TBSTATE_WRAP;
1421 bButtonWrap = FALSE;
1422 break;
1426 /* If no separator available for wrapping, wrap one of */
1427 /* non-hidden previous button. */
1428 if (!bFound)
1430 for ( j = i - 1;
1431 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1433 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1434 continue;
1436 bFound = TRUE;
1437 i = j;
1438 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1439 i, btnPtr[i].fsStyle, x, cx);
1440 x = infoPtr->nIndent;
1441 btnPtr[j].fsState |= TBSTATE_WRAP;
1442 bButtonWrap = TRUE;
1443 break;
1447 /* If all above failed, wrap the current button. */
1448 if (!bFound)
1450 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1451 i, btnPtr[i].fsStyle, x, cx);
1452 btnPtr[i].fsState |= TBSTATE_WRAP;
1453 x = infoPtr->nIndent;
1454 if (btnPtr[i].fsStyle & BTNS_SEP )
1455 bButtonWrap = FALSE;
1456 else
1457 bButtonWrap = TRUE;
1460 else {
1461 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1462 i, btnPtr[i].fsStyle, x, cx);
1463 x += cx;
1469 /***********************************************************************
1470 * TOOLBAR_MeasureButton
1472 * Calculates the width and height required for a button. Used in
1473 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1474 * the width of buttons that are autosized.
1476 * Note that it would have been rather elegant to use one piece of code for
1477 * both the laying out of the toolbar and for controlling where button parts
1478 * are drawn, but the native control has inconsistencies between the two that
1479 * prevent this from being effectively. These inconsistencies can be seen as
1480 * artefacts where parts of the button appear outside of the bounding button
1481 * rectangle.
1483 * There are several cases for the calculation of the button dimensions and
1484 * button part positioning:
1486 * List
1487 * ====
1489 * With Bitmap:
1491 * +--------------------------------------------------------+ ^
1492 * | ^ ^ | |
1493 * | | pad.cy / 2 | centered | |
1494 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1495 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1496 * | |<------------>| | | | |
1497 * | +--------------+ +------------+ | |
1498 * |<-------------------------------------->| | |
1499 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1500 * | szText.cx | |
1501 * +--------------------------------------------------------+ -
1502 * <-------------------------------------------------------->
1503 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1505 * Without Bitmap (I_IMAGENONE):
1507 * +-----------------------------------+ ^
1508 * | ^ | |
1509 * | | centered | | LISTPAD_CY +
1510 * | +------------+ | | szText.cy
1511 * | | Text | | |
1512 * | | | | |
1513 * | +------------+ | |
1514 * |<----------------->| | |
1515 * | cxedge |<-----------> | |
1516 * | szText.cx | |
1517 * +-----------------------------------+ -
1518 * <----------------------------------->
1519 * szText.cx + pad.cx
1521 * Without text:
1523 * +--------------------------------------+ ^
1524 * | ^ | |
1525 * | | padding.cy/2 | | DEFPAD_CY +
1526 * | +------------+ | | nBitmapHeight
1527 * | | Bitmap | | |
1528 * | | | | |
1529 * | +------------+ | |
1530 * |<------------------->| | |
1531 * | cxedge + iListGap/2 |<-----------> | |
1532 * | nBitmapWidth | |
1533 * +--------------------------------------+ -
1534 * <-------------------------------------->
1535 * 2*cxedge + nBitmapWidth + iListGap
1537 * Non-List
1538 * ========
1540 * With bitmap:
1542 * +-----------------------------------+ ^
1543 * | ^ | |
1544 * | | pad.cy / 2 | | nBitmapHeight +
1545 * | - | | szText.cy +
1546 * | +------------+ | | DEFPAD_CY + 1
1547 * | centered | Bitmap | | |
1548 * |<----------------->| | | |
1549 * | +------------+ | |
1550 * | ^ | |
1551 * | 1 | | |
1552 * | - | |
1553 * | centered +---------------+ | |
1554 * |<--------------->| Text | | |
1555 * | +---------------+ | |
1556 * +-----------------------------------+ -
1557 * <----------------------------------->
1558 * pad.cx + max(nBitmapWidth, szText.cx)
1560 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1562 * +---------------------------------------+ ^
1563 * | ^ | |
1564 * | | 2 + pad.cy / 2 | |
1565 * | - | | szText.cy +
1566 * | centered +-----------------+ | | pad.cy + 2
1567 * |<--------------->| Text | | |
1568 * | +-----------------+ | |
1569 * | | |
1570 * +---------------------------------------+ -
1571 * <--------------------------------------->
1572 * 2*cxedge + pad.cx + szText.cx
1574 * Without text:
1575 * As for with bitmaps, but with szText.cx zero.
1577 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1578 BOOL bHasBitmap, BOOL bValidImageList)
1580 SIZE sizeButton;
1581 if (infoPtr->dwStyle & TBSTYLE_LIST)
1583 /* set button height from bitmap / text height... */
1584 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1585 sizeString.cy);
1587 /* ... add on the necessary padding */
1588 if (bValidImageList)
1590 if (bHasBitmap)
1591 sizeButton.cy += DEFPAD_CY;
1592 else
1593 sizeButton.cy += LISTPAD_CY;
1595 else
1596 sizeButton.cy += infoPtr->szPadding.cy;
1598 /* calculate button width */
1599 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1600 infoPtr->nBitmapWidth + infoPtr->iListGap;
1601 if (sizeString.cx > 0)
1602 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1605 else
1607 if (bHasBitmap)
1609 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1610 if (sizeString.cy > 0)
1611 sizeButton.cy += 1 + sizeString.cy;
1612 sizeButton.cx = infoPtr->szPadding.cx +
1613 max(sizeString.cx, infoPtr->nBitmapWidth);
1615 else
1617 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1618 NONLIST_NOTEXT_OFFSET;
1619 sizeButton.cx = infoPtr->szPadding.cx +
1620 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1623 return sizeButton;
1627 /***********************************************************************
1628 * TOOLBAR_CalcToolbar
1630 * This function calculates button and separator placement. It first
1631 * calculates the button sizes, gets the toolbar window width and then
1632 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1633 * on. It assigns a new location to each item and sends this location to
1634 * the tooltip window if appropriate. Finally, it updates the rcBound
1635 * rect and calculates the new required toolbar window height.
1637 static void
1638 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1640 SIZE sizeString, sizeButton;
1641 BOOL validImageList = FALSE;
1643 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1645 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1647 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1648 validImageList = TRUE;
1649 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1650 infoPtr->nButtonWidth = sizeButton.cx;
1651 infoPtr->nButtonHeight = sizeButton.cy;
1652 infoPtr->iTopMargin = default_top_margin(infoPtr);
1654 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1655 infoPtr->nButtonWidth = infoPtr->cxMin;
1656 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1657 infoPtr->nButtonWidth = infoPtr->cxMax;
1659 TOOLBAR_LayoutToolbar(infoPtr);
1662 static void
1663 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1665 TBUTTON_INFO *btnPtr;
1666 SIZE sizeButton;
1667 INT i, nRows, nSepRows;
1668 INT x, y, cx, cy;
1669 BOOL bWrap;
1670 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1672 TOOLBAR_WrapToolbar(infoPtr);
1674 x = infoPtr->nIndent;
1675 y = infoPtr->iTopMargin;
1676 cx = infoPtr->nButtonWidth;
1677 cy = infoPtr->nButtonHeight;
1679 nRows = nSepRows = 0;
1681 infoPtr->rcBound.top = y;
1682 infoPtr->rcBound.left = x;
1683 infoPtr->rcBound.bottom = y + cy;
1684 infoPtr->rcBound.right = x;
1686 btnPtr = infoPtr->buttons;
1688 TRACE("cy=%d\n", cy);
1690 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1692 bWrap = FALSE;
1693 if (btnPtr->fsState & TBSTATE_HIDDEN)
1695 SetRectEmpty (&btnPtr->rect);
1696 continue;
1699 cy = infoPtr->nButtonHeight;
1701 if (btnPtr->fsStyle & BTNS_SEP) {
1702 if (infoPtr->dwStyle & CCS_VERT) {
1703 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1704 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1706 else
1707 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1708 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1710 else
1712 if (btnPtr->cx)
1713 cx = btnPtr->cx;
1714 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1716 SIZE sz;
1717 HDC hdc;
1718 HFONT hOldFont;
1720 hdc = GetDC (infoPtr->hwndSelf);
1721 hOldFont = SelectObject (hdc, infoPtr->hFont);
1723 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1725 SelectObject (hdc, hOldFont);
1726 ReleaseDC (infoPtr->hwndSelf, hdc);
1728 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1729 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1730 validImageList);
1731 cx = sizeButton.cx;
1733 else
1734 cx = infoPtr->nButtonWidth;
1736 /* if size has been set manually then don't add on extra space
1737 * for the drop down arrow */
1738 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1739 cx += DDARROW_WIDTH;
1741 if (btnPtr->fsState & TBSTATE_WRAP)
1742 bWrap = TRUE;
1744 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1746 if (infoPtr->rcBound.left > x)
1747 infoPtr->rcBound.left = x;
1748 if (infoPtr->rcBound.right < x + cx)
1749 infoPtr->rcBound.right = x + cx;
1750 if (infoPtr->rcBound.bottom < y + cy)
1751 infoPtr->rcBound.bottom = y + cy;
1753 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1755 /* btnPtr->nRow is zero based. The space between the rows is */
1756 /* also considered as a row. */
1757 btnPtr->nRow = nRows + nSepRows;
1759 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1760 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1761 x, y, x+cx, y+cy);
1763 if( bWrap )
1765 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1766 y += cy;
1767 else
1769 if ( !(infoPtr->dwStyle & CCS_VERT))
1770 y += cy + ( (btnPtr->cx > 0 ) ?
1771 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1772 else
1773 y += cy;
1775 /* nSepRows is used to calculate the extra height following */
1776 /* the last row. */
1777 nSepRows++;
1779 x = infoPtr->nIndent;
1781 /* Increment row number unless this is the last button */
1782 /* and it has Wrap set. */
1783 if (i != infoPtr->nNumButtons-1)
1784 nRows++;
1786 else
1787 x += cx;
1790 /* infoPtr->nRows is the number of rows on the toolbar */
1791 infoPtr->nRows = nRows + nSepRows + 1;
1793 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1797 static INT
1798 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1800 TBUTTON_INFO *btnPtr;
1801 INT i;
1803 if (button)
1804 *button = FALSE;
1806 btnPtr = infoPtr->buttons;
1807 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1808 if (btnPtr->fsState & TBSTATE_HIDDEN)
1809 continue;
1811 if (btnPtr->fsStyle & BTNS_SEP) {
1812 if (PtInRect (&btnPtr->rect, *lpPt)) {
1813 TRACE(" ON SEPARATOR %d!\n", i);
1814 return -i;
1817 else {
1818 if (PtInRect (&btnPtr->rect, *lpPt)) {
1819 TRACE(" ON BUTTON %d!\n", i);
1820 if (button)
1821 *button = TRUE;
1822 return i;
1827 TRACE(" NOWHERE!\n");
1828 return TOOLBAR_NOWHERE;
1832 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1833 static BOOL
1834 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1836 INT nOldButtons, nNewButtons, iButton;
1837 BOOL fHasString = FALSE;
1839 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1840 iIndex = infoPtr->nNumButtons;
1842 nOldButtons = infoPtr->nNumButtons;
1843 nNewButtons = nOldButtons + nAddButtons;
1845 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1846 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1847 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1848 infoPtr->nNumButtons += nAddButtons;
1850 /* insert new buttons data */
1851 for (iButton = 0; iButton < nAddButtons; iButton++) {
1852 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1853 INT_PTR str;
1855 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1857 ZeroMemory(btnPtr, sizeof(*btnPtr));
1859 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1860 btnPtr->idCommand = lpTbb[iButton].idCommand;
1861 btnPtr->fsState = lpTbb[iButton].fsState;
1862 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1863 btnPtr->dwData = lpTbb[iButton].dwData;
1865 if (btnPtr->fsStyle & BTNS_SEP)
1866 str = -1;
1867 else
1868 str = lpTbb[iButton].iString;
1869 set_string_index( btnPtr, str, fUnicode );
1870 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1872 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1875 if (infoPtr->nNumStrings > 0 || fHasString)
1876 TOOLBAR_CalcToolbar(infoPtr);
1877 else
1878 TOOLBAR_LayoutToolbar(infoPtr);
1879 TOOLBAR_AutoSize(infoPtr);
1881 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1882 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1883 return TRUE;
1887 static INT
1888 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1890 TBUTTON_INFO *btnPtr;
1891 INT i;
1893 if (CommandIsIndex) {
1894 TRACE("command is really index command=%d\n", idCommand);
1895 if (idCommand >= infoPtr->nNumButtons) return -1;
1896 return idCommand;
1898 btnPtr = infoPtr->buttons;
1899 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1900 if (btnPtr->idCommand == idCommand) {
1901 TRACE("command=%d index=%d\n", idCommand, i);
1902 return i;
1905 TRACE("no index found for command=%d\n", idCommand);
1906 return -1;
1910 static INT
1911 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1913 TBUTTON_INFO *btnPtr;
1914 INT nRunIndex;
1916 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1917 return -1;
1919 /* check index button */
1920 btnPtr = &infoPtr->buttons[nIndex];
1921 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1922 if (btnPtr->fsState & TBSTATE_CHECKED)
1923 return nIndex;
1926 /* check previous buttons */
1927 nRunIndex = nIndex - 1;
1928 while (nRunIndex >= 0) {
1929 btnPtr = &infoPtr->buttons[nRunIndex];
1930 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1931 if (btnPtr->fsState & TBSTATE_CHECKED)
1932 return nRunIndex;
1934 else
1935 break;
1936 nRunIndex--;
1939 /* check next buttons */
1940 nRunIndex = nIndex + 1;
1941 while (nRunIndex < infoPtr->nNumButtons) {
1942 btnPtr = &infoPtr->buttons[nRunIndex];
1943 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1944 if (btnPtr->fsState & TBSTATE_CHECKED)
1945 return nRunIndex;
1947 else
1948 break;
1949 nRunIndex++;
1952 return -1;
1956 static VOID
1957 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1958 WPARAM wParam, LPARAM lParam)
1960 MSG msg;
1962 msg.hwnd = hwndMsg;
1963 msg.message = uMsg;
1964 msg.wParam = wParam;
1965 msg.lParam = lParam;
1966 msg.time = GetMessageTime ();
1967 msg.pt.x = (short)LOWORD(GetMessagePos ());
1968 msg.pt.y = (short)HIWORD(GetMessagePos ());
1970 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1973 static void
1974 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1976 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1977 TTTOOLINFOW ti;
1979 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1980 ti.cbSize = sizeof (TTTOOLINFOW);
1981 ti.hwnd = infoPtr->hwndSelf;
1982 ti.uId = button->idCommand;
1983 ti.hinst = 0;
1984 ti.lpszText = LPSTR_TEXTCALLBACKW;
1985 /* ti.lParam = random value from the stack? */
1987 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1988 0, (LPARAM)&ti);
1992 static void
1993 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1995 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1996 TTTOOLINFOW ti;
1998 ZeroMemory(&ti, sizeof(ti));
1999 ti.cbSize = sizeof(ti);
2000 ti.hwnd = infoPtr->hwndSelf;
2001 ti.uId = button->idCommand;
2003 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2007 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2009 /* Set the toolTip only for non-hidden, non-separator button */
2010 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2012 TTTOOLINFOW ti;
2014 ZeroMemory(&ti, sizeof(ti));
2015 ti.cbSize = sizeof(ti);
2016 ti.hwnd = infoPtr->hwndSelf;
2017 ti.uId = button->idCommand;
2018 ti.rect = button->rect;
2019 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2023 /* Creates the tooltip control */
2024 static void
2025 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2027 int i;
2028 NMTOOLTIPSCREATED nmttc;
2030 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2031 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2032 infoPtr->hwndSelf, 0, 0, 0);
2034 if (!infoPtr->hwndToolTip)
2035 return;
2037 /* Send NM_TOOLTIPSCREATED notification */
2038 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2039 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2041 for (i = 0; i < infoPtr->nNumButtons; i++)
2043 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2044 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2048 /* keeps available button list box sorted by button id */
2049 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2051 int i;
2052 int count;
2053 PCUSTOMBUTTON btnInfo;
2054 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2056 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2058 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2060 /* position 0 is always separator */
2061 for (i = 1; i < count; i++)
2063 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2064 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2066 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2067 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2068 return;
2071 /* id higher than all others add to end */
2072 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2073 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2076 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2078 NMTOOLBARW nmtb;
2080 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2082 if (nIndexFrom == nIndexTo)
2083 return;
2085 /* MSDN states that iItem is the index of the button, rather than the
2086 * command ID as used by every other NMTOOLBAR notification */
2087 nmtb.iItem = nIndexFrom;
2088 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2090 PCUSTOMBUTTON btnInfo;
2091 NMHDR hdr;
2092 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2093 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2095 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2097 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2098 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2099 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2100 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2102 if (nIndexTo <= 0)
2103 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2104 else
2105 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2107 /* last item is always separator, so -2 instead of -1 */
2108 if (nIndexTo >= (count - 2))
2109 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2110 else
2111 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2113 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2114 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2116 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2120 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2122 NMTOOLBARW nmtb;
2124 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2126 /* MSDN states that iItem is the index of the button, rather than the
2127 * command ID as used by every other NMTOOLBAR notification */
2128 nmtb.iItem = nIndexAvail;
2129 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2131 PCUSTOMBUTTON btnInfo;
2132 NMHDR hdr;
2133 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2134 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2135 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2137 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2139 if (nIndexAvail != 0) /* index == 0 indicates separator */
2141 /* remove from 'available buttons' list */
2142 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2143 if (nIndexAvail == count-1)
2144 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2145 else
2146 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2148 else
2150 PCUSTOMBUTTON btnNew;
2152 /* duplicate 'separator' button */
2153 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2154 *btnNew = *btnInfo;
2155 btnInfo = btnNew;
2158 /* insert into 'toolbar button' list */
2159 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2160 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2162 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2164 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2168 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2170 PCUSTOMBUTTON btnInfo;
2171 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2173 TRACE("Remove: index %d\n", index);
2175 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2177 /* send TBN_QUERYDELETE notification */
2178 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2180 NMHDR hdr;
2182 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2183 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2185 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2187 /* insert into 'available button' list */
2188 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2189 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2190 else
2191 Free(btnInfo);
2193 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2197 /* drag list notification function for toolbar buttons list box */
2198 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2199 const DRAGLISTINFO *pDLI)
2201 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2202 switch (pDLI->uNotification)
2204 case DL_BEGINDRAG:
2206 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2207 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2208 /* no dragging for last item (separator) */
2209 if (nCurrentItem >= (nCount - 1)) return FALSE;
2210 return TRUE;
2212 case DL_DRAGGING:
2214 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2215 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2216 /* no dragging past last item (separator) */
2217 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2219 DrawInsert(hwnd, hwndList, nCurrentItem);
2220 /* FIXME: native uses "move button" cursor */
2221 return DL_COPYCURSOR;
2224 /* not over toolbar buttons list */
2225 if (nCurrentItem < 0)
2227 POINT ptWindow = pDLI->ptCursor;
2228 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2229 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2230 /* over available buttons list? */
2231 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2232 /* FIXME: native uses "move button" cursor */
2233 return DL_COPYCURSOR;
2235 /* clear drag arrow */
2236 DrawInsert(hwnd, hwndList, -1);
2237 return DL_STOPCURSOR;
2239 case DL_DROPPED:
2241 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2242 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2243 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2244 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2246 /* clear drag arrow */
2247 DrawInsert(hwnd, hwndList, -1);
2248 /* move item */
2249 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2251 /* not over toolbar buttons list */
2252 if (nIndexTo < 0)
2254 POINT ptWindow = pDLI->ptCursor;
2255 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2256 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2257 /* over available buttons list? */
2258 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2259 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2261 break;
2263 case DL_CANCELDRAG:
2264 /* Clear drag arrow */
2265 DrawInsert(hwnd, hwndList, -1);
2266 break;
2269 return 0;
2272 /* drag list notification function for available buttons list box */
2273 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2274 const DRAGLISTINFO *pDLI)
2276 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2277 switch (pDLI->uNotification)
2279 case DL_BEGINDRAG:
2280 return TRUE;
2281 case DL_DRAGGING:
2283 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2284 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2285 /* no dragging past last item (separator) */
2286 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2288 DrawInsert(hwnd, hwndList, nCurrentItem);
2289 /* FIXME: native uses "move button" cursor */
2290 return DL_COPYCURSOR;
2293 /* not over toolbar buttons list */
2294 if (nCurrentItem < 0)
2296 POINT ptWindow = pDLI->ptCursor;
2297 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2298 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2299 /* over available buttons list? */
2300 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2301 /* FIXME: native uses "move button" cursor */
2302 return DL_COPYCURSOR;
2304 /* clear drag arrow */
2305 DrawInsert(hwnd, hwndList, -1);
2306 return DL_STOPCURSOR;
2308 case DL_DROPPED:
2310 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2311 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2312 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2313 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2315 /* clear drag arrow */
2316 DrawInsert(hwnd, hwndList, -1);
2317 /* add item */
2318 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2321 case DL_CANCELDRAG:
2322 /* Clear drag arrow */
2323 DrawInsert(hwnd, hwndList, -1);
2324 break;
2326 return 0;
2329 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2331 /***********************************************************************
2332 * TOOLBAR_CustomizeDialogProc
2333 * This function implements the toolbar customization dialog.
2335 static INT_PTR CALLBACK
2336 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2338 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2339 PCUSTOMBUTTON btnInfo;
2340 NMTOOLBARA nmtb;
2341 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2343 switch (uMsg)
2345 case WM_INITDIALOG:
2346 custInfo = (PCUSTDLG_INFO)lParam;
2347 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2349 if (custInfo)
2351 WCHAR Buffer[256];
2352 int i = 0;
2353 int index;
2354 NMTBINITCUSTOMIZE nmtbic;
2356 infoPtr = custInfo->tbInfo;
2358 /* send TBN_QUERYINSERT notification */
2359 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2361 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2362 return FALSE;
2364 nmtbic.hwndDialog = hwnd;
2365 /* Send TBN_INITCUSTOMIZE notification */
2366 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2367 TBNRF_HIDEHELP)
2369 TRACE("TBNRF_HIDEHELP requested\n");
2370 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2373 /* add items to 'toolbar buttons' list and check if removable */
2374 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2376 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2377 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2378 btnInfo->btn.fsStyle = BTNS_SEP;
2379 btnInfo->bVirtual = FALSE;
2380 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2382 /* send TBN_QUERYDELETE notification */
2383 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2385 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2386 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2389 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2391 /* insert separator button into 'available buttons' list */
2392 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2393 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2394 btnInfo->btn.fsStyle = BTNS_SEP;
2395 btnInfo->bVirtual = FALSE;
2396 btnInfo->bRemovable = TRUE;
2397 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2398 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2399 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2401 /* insert all buttons into dsa */
2402 for (i = 0;; i++)
2404 /* send TBN_GETBUTTONINFO notification */
2405 NMTOOLBARW nmtb;
2406 nmtb.iItem = i;
2407 nmtb.pszText = Buffer;
2408 nmtb.cchText = 256;
2410 /* Clear previous button's text */
2411 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2413 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2414 break;
2416 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2417 nmtb.tbButton.fsStyle, i,
2418 nmtb.tbButton.idCommand,
2419 nmtb.tbButton.iString,
2420 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2421 : "");
2423 /* insert button into the appropriate list */
2424 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2425 if (index == -1)
2427 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2428 btnInfo->bVirtual = FALSE;
2429 btnInfo->bRemovable = TRUE;
2431 else
2433 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2434 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2437 btnInfo->btn = nmtb.tbButton;
2438 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2440 if (lstrlenW(nmtb.pszText))
2441 lstrcpyW(btnInfo->text, nmtb.pszText);
2442 else if (nmtb.tbButton.iString >= 0 &&
2443 nmtb.tbButton.iString < infoPtr->nNumStrings)
2445 lstrcpyW(btnInfo->text,
2446 infoPtr->strings[nmtb.tbButton.iString]);
2450 if (index == -1)
2451 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2454 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2456 /* select first item in the 'available' list */
2457 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2459 /* append 'virtual' separator button to the 'toolbar buttons' list */
2460 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2461 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2462 btnInfo->btn.fsStyle = BTNS_SEP;
2463 btnInfo->bVirtual = TRUE;
2464 btnInfo->bRemovable = FALSE;
2465 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2466 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2467 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2469 /* select last item in the 'toolbar' list */
2470 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2471 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2473 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2474 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2476 /* set focus and disable buttons */
2477 PostMessageW (hwnd, WM_USER, 0, 0);
2479 return TRUE;
2481 case WM_USER:
2482 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2483 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2484 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2485 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2486 return TRUE;
2488 case WM_CLOSE:
2489 EndDialog(hwnd, FALSE);
2490 return TRUE;
2492 case WM_COMMAND:
2493 switch (LOWORD(wParam))
2495 case IDC_TOOLBARBTN_LBOX:
2496 if (HIWORD(wParam) == LBN_SELCHANGE)
2498 PCUSTOMBUTTON btnInfo;
2499 NMTOOLBARA nmtb;
2500 int count;
2501 int index;
2503 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2504 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2506 /* send TBN_QUERYINSERT notification */
2507 nmtb.iItem = index;
2508 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2510 /* get list box item */
2511 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2513 if (index == (count - 1))
2515 /* last item (virtual separator) */
2516 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2517 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2519 else if (index == (count - 2))
2521 /* second last item (last non-virtual item) */
2522 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2523 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2525 else if (index == 0)
2527 /* first item */
2528 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2529 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2531 else
2533 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2534 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2537 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2539 break;
2541 case IDC_MOVEUP_BTN:
2543 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2544 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2546 break;
2548 case IDC_MOVEDN_BTN: /* move down */
2550 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2551 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2553 break;
2555 case IDC_REMOVE_BTN: /* remove button */
2557 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2559 if (LB_ERR == index)
2560 break;
2562 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2564 break;
2565 case IDC_HELP_BTN:
2566 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2567 break;
2568 case IDC_RESET_BTN:
2569 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2570 break;
2572 case IDOK: /* Add button */
2574 int index;
2575 int indexto;
2577 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2578 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2580 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2582 break;
2584 case IDCANCEL:
2585 EndDialog(hwnd, FALSE);
2586 break;
2588 return TRUE;
2590 case WM_DESTROY:
2592 int count;
2593 int i;
2595 /* delete items from 'toolbar buttons' listbox*/
2596 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2597 for (i = 0; i < count; i++)
2599 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2600 Free(btnInfo);
2601 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2603 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2606 /* delete items from 'available buttons' listbox*/
2607 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2608 for (i = 0; i < count; i++)
2610 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2611 Free(btnInfo);
2612 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2614 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2616 return TRUE;
2618 case WM_DRAWITEM:
2619 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2621 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2622 RECT rcButton;
2623 RECT rcText;
2624 HPEN hPen, hOldPen;
2625 HBRUSH hOldBrush;
2626 COLORREF oldText = 0;
2627 COLORREF oldBk = 0;
2629 /* get item data */
2630 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2631 if (btnInfo == NULL)
2633 FIXME("btnInfo invalid!\n");
2634 return TRUE;
2637 /* set colors and select objects */
2638 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2639 if (btnInfo->bVirtual)
2640 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2641 else
2642 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2643 hPen = CreatePen( PS_SOLID, 1,
2644 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2645 hOldPen = SelectObject (lpdis->hDC, hPen );
2646 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2648 /* fill background rectangle */
2649 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2650 lpdis->rcItem.right, lpdis->rcItem.bottom);
2652 /* calculate button and text rectangles */
2653 CopyRect (&rcButton, &lpdis->rcItem);
2654 InflateRect (&rcButton, -1, -1);
2655 CopyRect (&rcText, &rcButton);
2656 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2657 rcText.left = rcButton.right + 2;
2659 /* draw focus rectangle */
2660 if (lpdis->itemState & ODS_FOCUS)
2661 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2663 /* draw button */
2664 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2665 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2667 /* draw image and text */
2668 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2669 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2670 btnInfo->btn.iBitmap));
2671 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2672 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2674 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2675 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2677 /* delete objects and reset colors */
2678 SelectObject (lpdis->hDC, hOldBrush);
2679 SelectObject (lpdis->hDC, hOldPen);
2680 SetBkColor (lpdis->hDC, oldBk);
2681 SetTextColor (lpdis->hDC, oldText);
2682 DeleteObject( hPen );
2683 return TRUE;
2685 return FALSE;
2687 case WM_MEASUREITEM:
2688 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2690 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2692 lpmis->itemHeight = 15 + 8; /* default height */
2694 return TRUE;
2696 return FALSE;
2698 default:
2699 if (uDragListMessage && (uMsg == uDragListMessage))
2701 if (wParam == IDC_TOOLBARBTN_LBOX)
2703 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2704 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2705 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2706 return TRUE;
2708 else if (wParam == IDC_AVAILBTN_LBOX)
2710 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2711 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2712 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2713 return TRUE;
2716 return FALSE;
2720 static BOOL
2721 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2723 HBITMAP hbmLoad;
2724 INT nCountBefore = ImageList_GetImageCount(himlDef);
2725 INT nCountAfter;
2726 INT cxIcon, cyIcon;
2727 INT nAdded;
2728 INT nIndex;
2730 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2731 /* Add bitmaps to the default image list */
2732 if (bitmap->hInst == NULL) /* a handle was passed */
2733 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2734 else if (bitmap->hInst == COMCTL32_hModule)
2735 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2736 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2737 else
2738 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2740 /* enlarge the bitmap if needed */
2741 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2742 if (bitmap->hInst != COMCTL32_hModule)
2743 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2745 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2746 DeleteObject(hbmLoad);
2747 if (nIndex == -1)
2748 return FALSE;
2750 nCountAfter = ImageList_GetImageCount(himlDef);
2751 nAdded = nCountAfter - nCountBefore;
2752 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2754 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2755 } else if (nAdded > (INT)bitmap->nButtons) {
2756 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2757 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2760 infoPtr->nNumBitmaps += nAdded;
2761 return TRUE;
2764 static void
2765 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2767 HIMAGELIST himlDef;
2768 HIMAGELIST himlNew;
2769 INT cx, cy;
2770 INT i;
2772 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2773 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2774 return;
2775 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2776 return;
2777 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2778 return;
2780 TRACE("Update icon size: %dx%d -> %dx%d\n",
2781 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2783 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2784 ILC_COLOR32|ILC_MASK, 8, 2);
2785 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2786 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2787 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2788 infoPtr->himlInt = himlNew;
2790 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2791 ImageList_Destroy(himlDef);
2794 /***********************************************************************
2795 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2798 static LRESULT
2799 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2801 TBITMAP_INFO info;
2802 INT iSumButtons, i;
2803 HIMAGELIST himlDef;
2805 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2806 if (!lpAddBmp)
2807 return -1;
2809 if (lpAddBmp->hInst == HINST_COMMCTRL)
2811 info.hInst = COMCTL32_hModule;
2812 switch (lpAddBmp->nID)
2814 case IDB_STD_SMALL_COLOR:
2815 info.nButtons = 15;
2816 info.nID = IDB_STD_SMALL;
2817 break;
2818 case IDB_STD_LARGE_COLOR:
2819 info.nButtons = 15;
2820 info.nID = IDB_STD_LARGE;
2821 break;
2822 case IDB_VIEW_SMALL_COLOR:
2823 info.nButtons = 12;
2824 info.nID = IDB_VIEW_SMALL;
2825 break;
2826 case IDB_VIEW_LARGE_COLOR:
2827 info.nButtons = 12;
2828 info.nID = IDB_VIEW_LARGE;
2829 break;
2830 case IDB_HIST_SMALL_COLOR:
2831 info.nButtons = 5;
2832 info.nID = IDB_HIST_SMALL;
2833 break;
2834 case IDB_HIST_LARGE_COLOR:
2835 info.nButtons = 5;
2836 info.nID = IDB_HIST_LARGE;
2837 break;
2838 default:
2839 return -1;
2842 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2844 /* Windows resize all the buttons to the size of a newly added standard image */
2845 if (lpAddBmp->nID & 1)
2847 /* large icons: 24x24. Will make the button 31x30 */
2848 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2850 else
2852 /* small icons: 16x16. Will make the buttons 23x22 */
2853 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2856 TOOLBAR_CalcToolbar (infoPtr);
2858 else
2860 info.nButtons = count;
2861 info.hInst = lpAddBmp->hInst;
2862 info.nID = lpAddBmp->nID;
2863 TRACE("adding %d bitmaps!\n", info.nButtons);
2866 /* check if the bitmap is already loaded and compute iSumButtons */
2867 iSumButtons = 0;
2868 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2870 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2871 infoPtr->bitmaps[i].nID == info.nID)
2872 return iSumButtons;
2873 iSumButtons += infoPtr->bitmaps[i].nButtons;
2876 if (!infoPtr->cimlDef) {
2877 /* create new default image list */
2878 TRACE ("creating default image list!\n");
2880 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2881 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2882 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2883 infoPtr->himlInt = himlDef;
2885 else {
2886 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2889 if (!himlDef) {
2890 WARN("No default image list available\n");
2891 return -1;
2894 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2895 return -1;
2897 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2898 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2899 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2900 infoPtr->nNumBitmapInfos++;
2901 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2903 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2904 return iSumButtons;
2908 static LRESULT
2909 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2911 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
2913 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2917 static LRESULT
2918 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2920 #define MAX_RESOURCE_STRING_LENGTH 512
2921 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2922 INT nIndex = infoPtr->nNumStrings;
2924 TRACE("%p, %lx\n", hInstance, lParam);
2926 if (IS_INTRESOURCE(lParam)) {
2927 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2928 WCHAR delimiter;
2929 WCHAR *next_delim;
2930 HRSRC hrsrc;
2931 WCHAR *p;
2932 INT len;
2934 TRACE("adding string from resource\n");
2936 if (!hInstance) return -1;
2938 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2939 (LPWSTR)RT_STRING );
2940 if (!hrsrc)
2942 TRACE("string not found in resources\n");
2943 return -1;
2946 len = LoadStringW (hInstance, (UINT)lParam,
2947 szString, MAX_RESOURCE_STRING_LENGTH);
2949 TRACE("len=%d %s\n", len, debugstr_w(szString));
2950 if (len == 0 || len == 1)
2951 return nIndex;
2953 TRACE("delimiter: 0x%x\n", *szString);
2954 delimiter = *szString;
2955 p = szString + 1;
2957 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2958 *next_delim = 0;
2959 if (next_delim + 1 >= szString + len)
2961 /* this may happen if delimiter == '\0' or if the last char is a
2962 * delimiter (then it is ignored like the native does) */
2963 break;
2966 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2967 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2968 infoPtr->nNumStrings++;
2970 p = next_delim + 1;
2973 else {
2974 LPWSTR p = (LPWSTR)lParam;
2975 INT len;
2977 if (p == NULL)
2978 return -1;
2979 TRACE("adding string(s) from array\n");
2980 while (*p) {
2981 len = strlenW (p);
2983 TRACE("len=%d %s\n", len, debugstr_w(p));
2984 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2985 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2986 infoPtr->nNumStrings++;
2988 p += (len+1);
2992 if (fFirstString)
2993 TOOLBAR_CalcToolbar(infoPtr);
2994 return nIndex;
2998 static LRESULT
2999 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3001 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3002 LPSTR p;
3003 INT nIndex;
3004 INT len;
3006 TRACE("%p, %lx\n", hInstance, lParam);
3008 if (IS_INTRESOURCE(lParam)) /* load from resources */
3009 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3011 p = (LPSTR)lParam;
3012 if (p == NULL)
3013 return -1;
3015 TRACE("adding string(s) from array\n");
3016 nIndex = infoPtr->nNumStrings;
3017 while (*p) {
3018 len = strlen (p);
3019 TRACE("len=%d \"%s\"\n", len, p);
3021 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3022 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3023 infoPtr->nNumStrings++;
3025 p += (len+1);
3028 if (fFirstString)
3029 TOOLBAR_CalcToolbar(infoPtr);
3030 return nIndex;
3034 static LRESULT
3035 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3037 TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3038 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3040 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3042 RECT window_rect, parent_rect;
3043 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3044 HWND parent;
3045 INT x, y, cx, cy;
3047 parent = GetParent (infoPtr->hwndSelf);
3049 if (!parent || !infoPtr->bDoRedraw)
3050 return 0;
3052 GetClientRect(parent, &parent_rect);
3054 x = parent_rect.left;
3055 y = parent_rect.top;
3057 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3058 cx = parent_rect.right - parent_rect.left;
3060 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3062 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3063 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3064 y = window_rect.top;
3066 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3068 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3069 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3072 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3073 uPosFlags |= SWP_NOMOVE;
3075 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3076 cy += GetSystemMetrics(SM_CYEDGE);
3078 if (infoPtr->dwStyle & WS_BORDER)
3080 cx += 2 * GetSystemMetrics(SM_CXBORDER);
3081 cy += 2 * GetSystemMetrics(SM_CYBORDER);
3084 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3087 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3089 TOOLBAR_LayoutToolbar(infoPtr);
3090 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3093 return 0;
3097 static inline LRESULT
3098 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3100 return infoPtr->nNumButtons;
3104 static inline LRESULT
3105 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3107 infoPtr->dwStructSize = Size;
3109 return 0;
3113 static LRESULT
3114 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3116 TBUTTON_INFO *btnPtr;
3117 INT nIndex;
3119 TRACE("button %d, iBitmap now %d\n", Id, Index);
3121 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3122 if (nIndex == -1)
3123 return FALSE;
3125 btnPtr = &infoPtr->buttons[nIndex];
3126 btnPtr->iBitmap = Index;
3128 /* we HAVE to erase the background, the new bitmap could be */
3129 /* transparent */
3130 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3132 return TRUE;
3136 static LRESULT
3137 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3139 TBUTTON_INFO *btnPtr;
3140 INT nIndex;
3141 INT nOldIndex = -1;
3142 BOOL bChecked = FALSE;
3144 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3146 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3148 if (nIndex == -1)
3149 return FALSE;
3151 btnPtr = &infoPtr->buttons[nIndex];
3153 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3155 if (LOWORD(lParam) == FALSE)
3156 btnPtr->fsState &= ~TBSTATE_CHECKED;
3157 else {
3158 if (btnPtr->fsStyle & BTNS_GROUP) {
3159 nOldIndex =
3160 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3161 if (nOldIndex == nIndex)
3162 return 0;
3163 if (nOldIndex != -1)
3164 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3166 btnPtr->fsState |= TBSTATE_CHECKED;
3169 if( bChecked != LOWORD(lParam) )
3171 if (nOldIndex != -1)
3172 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3173 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3176 /* FIXME: Send a WM_NOTIFY?? */
3178 return TRUE;
3182 static LRESULT
3183 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3185 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3189 static LRESULT
3190 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3192 CUSTDLG_INFO custInfo;
3193 LRESULT ret;
3194 NMHDR nmhdr;
3196 custInfo.tbInfo = infoPtr;
3197 custInfo.tbHwnd = infoPtr->hwndSelf;
3199 /* send TBN_BEGINADJUST notification */
3200 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3202 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3203 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3205 /* send TBN_ENDADJUST notification */
3206 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3208 return ret;
3212 static LRESULT
3213 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3215 NMTOOLBARW nmtb;
3216 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3218 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3219 return FALSE;
3221 memset(&nmtb, 0, sizeof(nmtb));
3222 nmtb.iItem = btnPtr->idCommand;
3223 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3224 nmtb.tbButton.idCommand = btnPtr->idCommand;
3225 nmtb.tbButton.fsState = btnPtr->fsState;
3226 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3227 nmtb.tbButton.dwData = btnPtr->dwData;
3228 nmtb.tbButton.iString = btnPtr->iString;
3229 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3231 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3233 if (infoPtr->nNumButtons == 1) {
3234 TRACE(" simple delete!\n");
3235 free_string( infoPtr->buttons );
3236 Free (infoPtr->buttons);
3237 infoPtr->buttons = NULL;
3238 infoPtr->nNumButtons = 0;
3240 else {
3241 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3242 TRACE("complex delete! [nIndex=%d]\n", nIndex);
3244 infoPtr->nNumButtons--;
3245 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3246 if (nIndex > 0) {
3247 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3248 nIndex * sizeof(TBUTTON_INFO));
3251 if (nIndex < infoPtr->nNumButtons) {
3252 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3253 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3256 free_string( oldButtons + nIndex );
3257 Free (oldButtons);
3260 TOOLBAR_LayoutToolbar(infoPtr);
3262 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3264 return TRUE;
3268 static LRESULT
3269 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3271 TBUTTON_INFO *btnPtr;
3272 INT nIndex;
3273 DWORD bState;
3275 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3277 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3279 if (nIndex == -1)
3280 return FALSE;
3282 btnPtr = &infoPtr->buttons[nIndex];
3284 bState = btnPtr->fsState & TBSTATE_ENABLED;
3286 /* update the toolbar button state */
3287 if(LOWORD(lParam) == FALSE) {
3288 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3289 } else {
3290 btnPtr->fsState |= TBSTATE_ENABLED;
3293 /* redraw the button only if the state of the button changed */
3294 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3295 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3297 return TRUE;
3301 static inline LRESULT
3302 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3304 return infoPtr->bAnchor;
3308 static LRESULT
3309 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3311 INT nIndex;
3313 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3314 if (nIndex == -1)
3315 return -1;
3317 return infoPtr->buttons[nIndex].iBitmap;
3321 static inline LRESULT
3322 TOOLBAR_GetBitmapFlags (void)
3324 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3328 static LRESULT
3329 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3331 TBUTTON_INFO *btnPtr;
3333 if (lpTbb == NULL)
3334 return FALSE;
3336 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3337 return FALSE;
3339 btnPtr = &infoPtr->buttons[nIndex];
3340 lpTbb->iBitmap = btnPtr->iBitmap;
3341 lpTbb->idCommand = btnPtr->idCommand;
3342 lpTbb->fsState = btnPtr->fsState;
3343 lpTbb->fsStyle = btnPtr->fsStyle;
3344 lpTbb->bReserved[0] = 0;
3345 lpTbb->bReserved[1] = 0;
3346 lpTbb->dwData = btnPtr->dwData;
3347 lpTbb->iString = btnPtr->iString;
3349 return TRUE;
3353 static LRESULT
3354 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3356 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3357 TBUTTON_INFO *btnPtr;
3358 INT nIndex;
3360 if (lpTbInfo == NULL)
3361 return -1;
3363 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3364 * the headers and tests shows that even with comctl 6 Vista accepts only the
3365 * original TBBUTTONINFO size
3367 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3369 WARN("Invalid button size\n");
3370 return -1;
3373 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3374 if (nIndex == -1)
3375 return -1;
3377 btnPtr = &infoPtr->buttons[nIndex];
3378 if (lpTbInfo->dwMask & TBIF_COMMAND)
3379 lpTbInfo->idCommand = btnPtr->idCommand;
3380 if (lpTbInfo->dwMask & TBIF_IMAGE)
3381 lpTbInfo->iImage = btnPtr->iBitmap;
3382 if (lpTbInfo->dwMask & TBIF_LPARAM)
3383 lpTbInfo->lParam = btnPtr->dwData;
3384 if (lpTbInfo->dwMask & TBIF_SIZE)
3385 /* tests show that for separators TBIF_SIZE returns not calculated width,
3386 but cx property, that differs from 0 only if application have
3387 specifically set it */
3388 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3389 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3390 if (lpTbInfo->dwMask & TBIF_STATE)
3391 lpTbInfo->fsState = btnPtr->fsState;
3392 if (lpTbInfo->dwMask & TBIF_STYLE)
3393 lpTbInfo->fsStyle = btnPtr->fsStyle;
3394 if (lpTbInfo->dwMask & TBIF_TEXT) {
3395 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3396 can't use TOOLBAR_GetText here */
3397 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3398 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3399 if (bUnicode)
3400 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3401 else
3402 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3403 } else
3404 lpTbInfo->pszText[0] = '\0';
3406 return nIndex;
3410 static inline LRESULT
3411 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3413 return MAKELONG((WORD)infoPtr->nButtonWidth,
3414 (WORD)infoPtr->nButtonHeight);
3418 static LRESULT
3419 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3421 INT nIndex;
3422 LPWSTR lpText;
3423 LRESULT ret = 0;
3425 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3426 if (nIndex == -1)
3427 return -1;
3429 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3431 if (isW)
3433 if (lpText)
3435 ret = strlenW (lpText);
3436 if (lpStr) strcpyW (lpStr, lpText);
3439 else
3440 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3441 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3442 return ret;
3446 static LRESULT
3447 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3449 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3450 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3451 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3455 static inline LRESULT
3456 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3458 TRACE("\n");
3460 return infoPtr->dwExStyle;
3464 static LRESULT
3465 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3467 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3468 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3469 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3473 static LRESULT
3474 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3476 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3477 return -1;
3479 if (infoPtr->nHotItem < 0)
3480 return -1;
3482 return (LRESULT)infoPtr->nHotItem;
3486 static LRESULT
3487 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3489 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3490 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3491 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3495 static LRESULT
3496 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3498 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3500 *lptbim = infoPtr->tbim;
3502 return 0;
3506 static inline LRESULT
3507 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3509 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3511 return (LRESULT)infoPtr->clrInsertMark;
3515 static LRESULT
3516 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3518 TBUTTON_INFO *btnPtr;
3520 btnPtr = &infoPtr->buttons[nIndex];
3521 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3522 return FALSE;
3524 if (lpRect == NULL)
3525 return FALSE;
3526 if (btnPtr->fsState & TBSTATE_HIDDEN)
3527 return FALSE;
3529 lpRect->left = btnPtr->rect.left;
3530 lpRect->right = btnPtr->rect.right;
3531 lpRect->bottom = btnPtr->rect.bottom;
3532 lpRect->top = btnPtr->rect.top;
3534 return TRUE;
3538 static LRESULT
3539 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3541 if (lpSize == NULL)
3542 return FALSE;
3544 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3545 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3547 TRACE("maximum size %d x %d\n",
3548 infoPtr->rcBound.right - infoPtr->rcBound.left,
3549 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3551 return TRUE;
3555 /* << TOOLBAR_GetObject >> */
3558 static inline LRESULT
3559 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3561 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3565 static LRESULT
3566 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3568 TBUTTON_INFO *btnPtr;
3569 INT nIndex;
3571 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3572 btnPtr = &infoPtr->buttons[nIndex];
3573 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3574 return FALSE;
3576 if (lpRect == NULL)
3577 return FALSE;
3579 lpRect->left = btnPtr->rect.left;
3580 lpRect->right = btnPtr->rect.right;
3581 lpRect->bottom = btnPtr->rect.bottom;
3582 lpRect->top = btnPtr->rect.top;
3584 return TRUE;
3588 static inline LRESULT
3589 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3591 return infoPtr->nRows;
3595 static LRESULT
3596 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3598 INT nIndex;
3600 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3601 if (nIndex == -1)
3602 return -1;
3604 return infoPtr->buttons[nIndex].fsState;
3608 static inline LRESULT
3609 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3611 return infoPtr->dwStyle;
3615 static inline LRESULT
3616 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3618 return infoPtr->nMaxTextRows;
3622 static LRESULT
3623 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3625 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3626 TOOLBAR_TooltipCreateControl(infoPtr);
3627 return (LRESULT)infoPtr->hwndToolTip;
3631 static LRESULT
3632 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3634 TRACE("%s hwnd=%p\n",
3635 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3637 return infoPtr->bUnicode;
3641 static inline LRESULT
3642 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3644 return infoPtr->iVersion;
3648 static LRESULT
3649 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3651 TBUTTON_INFO *btnPtr;
3652 BYTE oldState;
3653 INT nIndex;
3655 TRACE("\n");
3657 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3658 if (nIndex == -1)
3659 return FALSE;
3661 btnPtr = &infoPtr->buttons[nIndex];
3662 oldState = btnPtr->fsState;
3664 if (fHide)
3665 btnPtr->fsState |= TBSTATE_HIDDEN;
3666 else
3667 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3669 if (oldState != btnPtr->fsState) {
3670 TOOLBAR_LayoutToolbar (infoPtr);
3671 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3674 return TRUE;
3678 static inline LRESULT
3679 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3681 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3685 static LRESULT
3686 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3688 TBUTTON_INFO *btnPtr;
3689 INT nIndex;
3690 DWORD oldState;
3692 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3693 if (nIndex == -1)
3694 return FALSE;
3696 btnPtr = &infoPtr->buttons[nIndex];
3697 oldState = btnPtr->fsState;
3699 if (fIndeterminate)
3700 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3701 else
3702 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3704 if(oldState != btnPtr->fsState)
3705 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3707 return TRUE;
3711 static LRESULT
3712 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3714 if (lpTbb == NULL)
3715 return FALSE;
3717 if (nIndex == -1) {
3718 /* EPP: this seems to be an undocumented call (from my IE4)
3719 * I assume in that case that:
3720 * - index of insertion is at the end of existing buttons
3721 * I only see this happen with nIndex == -1, but it could have a special
3722 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3724 nIndex = infoPtr->nNumButtons;
3726 } else if (nIndex < 0)
3727 return FALSE;
3729 TRACE("inserting button index=%d\n", nIndex);
3730 if (nIndex > infoPtr->nNumButtons) {
3731 nIndex = infoPtr->nNumButtons;
3732 TRACE("adjust index=%d\n", nIndex);
3735 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3738 /* << TOOLBAR_InsertMarkHitTest >> */
3741 static LRESULT
3742 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3744 INT nIndex;
3746 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3747 if (nIndex == -1)
3748 return -1;
3750 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3754 static LRESULT
3755 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3757 INT nIndex;
3759 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3760 if (nIndex == -1)
3761 return -1;
3763 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3767 static LRESULT
3768 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3770 INT nIndex;
3772 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3773 if (nIndex == -1)
3774 return -1;
3776 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3780 static LRESULT
3781 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3783 INT nIndex;
3785 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3786 if (nIndex == -1)
3787 return -1;
3789 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3793 static LRESULT
3794 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3796 INT nIndex;
3798 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3799 if (nIndex == -1)
3800 return -1;
3802 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3806 static LRESULT
3807 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3809 INT nIndex;
3811 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3812 if (nIndex == -1)
3813 return -1;
3815 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3819 static LRESULT
3820 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3822 TBADDBITMAP tbab;
3823 tbab.hInst = hInstance;
3824 tbab.nID = wParam;
3826 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3828 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3832 static LRESULT
3833 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3835 WCHAR wszAccel[] = {'&',wAccel,0};
3836 int i;
3838 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3839 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3841 for (i = 0; i < infoPtr->nNumButtons; i++)
3843 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3844 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3845 !(btnPtr->fsState & TBSTATE_HIDDEN))
3847 int iLen = strlenW(wszAccel);
3848 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3850 if (!lpszStr)
3851 continue;
3853 while (*lpszStr)
3855 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3857 lpszStr += 2;
3858 continue;
3860 if (!strncmpiW(lpszStr, wszAccel, iLen))
3862 *pIDButton = btnPtr->idCommand;
3863 return TRUE;
3865 lpszStr++;
3869 return FALSE;
3873 static LRESULT
3874 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3876 INT nIndex;
3877 DWORD oldState;
3878 TBUTTON_INFO *btnPtr;
3880 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3882 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3883 if (nIndex == -1)
3884 return FALSE;
3886 btnPtr = &infoPtr->buttons[nIndex];
3887 oldState = btnPtr->fsState;
3889 if (fMark)
3890 btnPtr->fsState |= TBSTATE_MARKED;
3891 else
3892 btnPtr->fsState &= ~TBSTATE_MARKED;
3894 if(oldState != btnPtr->fsState)
3895 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3897 return TRUE;
3901 /* fixes up an index of a button affected by a move */
3902 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3904 if (bMoveUp)
3906 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3907 (*pIndex)--;
3908 else if (*pIndex == nIndex)
3909 *pIndex = nMoveIndex;
3911 else
3913 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3914 (*pIndex)++;
3915 else if (*pIndex == nIndex)
3916 *pIndex = nMoveIndex;
3921 static LRESULT
3922 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3924 INT nIndex;
3925 INT nCount;
3926 TBUTTON_INFO button;
3928 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3930 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3931 if ((nIndex == -1) || (nMoveIndex < 0))
3932 return FALSE;
3934 if (nMoveIndex > infoPtr->nNumButtons - 1)
3935 nMoveIndex = infoPtr->nNumButtons - 1;
3937 button = infoPtr->buttons[nIndex];
3939 /* move button right */
3940 if (nIndex < nMoveIndex)
3942 nCount = nMoveIndex - nIndex;
3943 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3944 infoPtr->buttons[nMoveIndex] = button;
3946 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3947 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3948 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3949 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3951 else if (nIndex > nMoveIndex) /* move button left */
3953 nCount = nIndex - nMoveIndex;
3954 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3955 infoPtr->buttons[nMoveIndex] = button;
3957 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3958 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3959 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3960 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3963 TOOLBAR_LayoutToolbar(infoPtr);
3964 TOOLBAR_AutoSize(infoPtr);
3965 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3967 return TRUE;
3971 static LRESULT
3972 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
3974 TBUTTON_INFO *btnPtr;
3975 INT nIndex;
3976 DWORD oldState;
3978 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3979 if (nIndex == -1)
3980 return FALSE;
3982 btnPtr = &infoPtr->buttons[nIndex];
3983 oldState = btnPtr->fsState;
3985 if (fPress)
3986 btnPtr->fsState |= TBSTATE_PRESSED;
3987 else
3988 btnPtr->fsState &= ~TBSTATE_PRESSED;
3990 if(oldState != btnPtr->fsState)
3991 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3993 return TRUE;
3996 /* FIXME: there might still be some confusion her between number of buttons
3997 * and number of bitmaps */
3998 static LRESULT
3999 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
4001 HBITMAP hBitmap;
4002 int i = 0, nOldButtons = 0, pos = 0;
4003 int nOldBitmaps, nNewBitmaps = 0;
4004 HIMAGELIST himlDef = 0;
4006 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4007 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4008 lpReplace->nButtons);
4010 if (lpReplace->hInstOld == HINST_COMMCTRL)
4012 FIXME("changing standard bitmaps not implemented\n");
4013 return FALSE;
4015 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4016 FIXME("resources not in the current module not implemented\n");
4018 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4019 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4020 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4021 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4022 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4024 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4025 nOldButtons = tbi->nButtons;
4026 tbi->nButtons = lpReplace->nButtons;
4027 tbi->hInst = lpReplace->hInstNew;
4028 tbi->nID = lpReplace->nIDNew;
4029 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4030 break;
4032 pos += tbi->nButtons;
4035 if (nOldButtons == 0)
4037 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4038 return FALSE;
4041 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4042 * bitmap
4044 if (lpReplace->hInstNew)
4045 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4046 else
4047 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4049 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4050 nOldBitmaps = ImageList_GetImageCount(himlDef);
4052 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4054 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4055 ImageList_Remove(himlDef, i);
4057 if (hBitmap)
4059 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4060 nNewBitmaps = ImageList_GetImageCount(himlDef);
4061 DeleteObject(hBitmap);
4064 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4066 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4067 pos, nOldBitmaps, nNewBitmaps);
4069 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4070 return TRUE;
4074 /* helper for TOOLBAR_SaveRestoreW */
4075 static BOOL
4076 TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
4078 NMTBSAVE save;
4079 INT ret, i;
4080 BOOL alloced = FALSE;
4081 HKEY key;
4083 TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );
4085 memset( &save, 0, sizeof(save) );
4086 save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
4087 save.iItem = -1;
4088 save.cButtons = infoPtr->nNumButtons;
4089 save.tbButton.idCommand = -1;
4090 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4092 if (!save.pData)
4094 save.pData = Alloc( save.cbData );
4095 if (!save.pData) return FALSE;
4096 alloced = TRUE;
4098 if (!save.pCurrent) save.pCurrent = save.pData;
4100 for (i = 0; i < infoPtr->nNumButtons; i++)
4102 save.iItem = i;
4103 save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
4104 save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
4105 save.tbButton.fsState = infoPtr->buttons[i].fsState;
4106 save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
4107 memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
4108 save.tbButton.dwData = infoPtr->buttons[i].dwData;
4109 save.tbButton.iString = infoPtr->buttons[i].iString;
4111 *save.pCurrent++ = save.tbButton.idCommand;
4113 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4116 ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
4117 if (ret == ERROR_SUCCESS)
4119 ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
4120 RegCloseKey( key );
4123 if (alloced) Free( save.pData );
4124 return !ret;
4128 /* helper for TOOLBAR_Restore */
4129 static void
4130 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4132 INT i;
4134 for (i = 0; i < infoPtr->nNumButtons; i++)
4136 free_string( infoPtr->buttons + i );
4137 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4140 Free(infoPtr->buttons);
4141 infoPtr->buttons = NULL;
4142 infoPtr->nNumButtons = 0;
4146 /* helper for TOOLBAR_SaveRestoreW */
4147 static BOOL
4148 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4150 LONG res;
4151 HKEY hkey = NULL;
4152 BOOL ret = FALSE;
4153 DWORD dwType;
4154 DWORD dwSize = 0;
4155 NMTBRESTORE nmtbr;
4156 NMHDR hdr;
4158 /* restore toolbar information */
4159 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4160 debugstr_w(lpSave->pszValueName));
4162 memset(&nmtbr, 0, sizeof(nmtbr));
4164 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4165 KEY_QUERY_VALUE, &hkey);
4166 if (!res)
4167 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4168 NULL, &dwSize);
4169 if (!res && dwType != REG_BINARY)
4170 res = ERROR_FILE_NOT_FOUND;
4171 if (!res)
4173 nmtbr.pData = Alloc(dwSize);
4174 nmtbr.cbData = dwSize;
4175 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4177 if (!res)
4178 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4179 (LPBYTE)nmtbr.pData, &dwSize);
4180 if (!res)
4182 nmtbr.pCurrent = nmtbr.pData;
4183 nmtbr.iItem = -1;
4184 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4185 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4187 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4189 INT i, count = nmtbr.cButtons;
4191 /* remove all existing buttons as this function is designed to
4192 * restore the toolbar to a previously saved state */
4193 TOOLBAR_DeleteAllButtons(infoPtr);
4195 for (i = 0; i < count; i++)
4197 nmtbr.iItem = i;
4198 nmtbr.tbButton.iBitmap = -1;
4199 nmtbr.tbButton.fsState = 0;
4200 nmtbr.tbButton.fsStyle = 0;
4201 nmtbr.tbButton.dwData = 0;
4202 nmtbr.tbButton.iString = 0;
4204 if (*nmtbr.pCurrent & 0x80000000)
4206 /* separator */
4207 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4208 nmtbr.tbButton.idCommand = 0;
4209 nmtbr.tbButton.fsStyle = BTNS_SEP;
4210 if (*nmtbr.pCurrent != (DWORD)-1)
4211 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4213 else
4214 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4216 nmtbr.pCurrent++;
4218 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4220 /* All returned ptrs and -1 are ignored */
4221 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4222 nmtbr.tbButton.iString = 0;
4224 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4227 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
4228 for (i = 0; ; i++)
4230 NMTOOLBARW tb;
4231 TBBUTTONINFOW bi;
4232 WCHAR buf[128];
4233 UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
4234 INT idx;
4236 memset( &tb, 0, sizeof(tb) );
4237 tb.iItem = i;
4238 tb.cchText = sizeof(buf) / sizeof(buf[0]);
4239 tb.pszText = buf;
4241 /* Use the same struct for both A and W versions since the layout is the same. */
4242 if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
4243 break;
4245 idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
4246 if (idx == -1) continue;
4248 /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
4249 be an index or a ptr. Either way it is simply copied. There is no api to change
4250 the string index, so we set it manually. The other properties can be set with SetButtonInfo. */
4251 free_string( infoPtr->buttons + idx );
4252 infoPtr->buttons[idx].iString = tb.tbButton.iString;
4254 memset( &bi, 0, sizeof(bi) );
4255 bi.cbSize = sizeof(bi);
4256 bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
4257 bi.iImage = tb.tbButton.iBitmap;
4258 bi.fsState = tb.tbButton.fsState;
4259 bi.fsStyle = tb.tbButton.fsStyle;
4260 bi.lParam = tb.tbButton.dwData;
4262 TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4264 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4266 /* remove all uninitialised buttons
4267 * note: loop backwards to avoid having to fixup i on a
4268 * delete */
4269 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4270 if (infoPtr->buttons[i].iBitmap == -1)
4271 TOOLBAR_DeleteButton(infoPtr, i);
4273 /* only indicate success if at least one button survived */
4274 if (infoPtr->nNumButtons > 0) ret = TRUE;
4277 Free (nmtbr.pData);
4278 RegCloseKey(hkey);
4280 return ret;
4284 static LRESULT
4285 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4287 if (lpSave == NULL) return 0;
4289 if (wParam)
4290 return TOOLBAR_Save(infoPtr, lpSave);
4291 else
4292 return TOOLBAR_Restore(infoPtr, lpSave);
4296 static LRESULT
4297 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4299 LPWSTR pszValueName = 0, pszSubKey = 0;
4300 TBSAVEPARAMSW SaveW;
4301 LRESULT result = 0;
4302 int len;
4304 if (lpSave == NULL) return 0;
4306 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4307 pszSubKey = Alloc(len * sizeof(WCHAR));
4308 if (!pszSubKey) goto exit;
4309 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4311 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4312 pszValueName = Alloc(len * sizeof(WCHAR));
4313 if (!pszValueName) goto exit;
4314 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4316 SaveW.pszValueName = pszValueName;
4317 SaveW.pszSubKey = pszSubKey;
4318 SaveW.hkr = lpSave->hkr;
4319 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4321 exit:
4322 Free (pszValueName);
4323 Free (pszSubKey);
4325 return result;
4329 static LRESULT
4330 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4332 BOOL bOldAnchor = infoPtr->bAnchor;
4334 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4336 infoPtr->bAnchor = bAnchor;
4338 /* Native does not remove the hot effect from an already hot button */
4340 return (LRESULT)bOldAnchor;
4344 static LRESULT
4345 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4347 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4348 short width = (short)LOWORD(lParam);
4349 short height = (short)HIWORD(lParam);
4351 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", infoPtr->hwndSelf, wParam, lParam);
4353 if (wParam != 0)
4354 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4356 /* 0 width or height is changed to 1 */
4357 if (width == 0)
4358 width = 1;
4359 if (height == 0)
4360 height = 1;
4362 if (infoPtr->nNumButtons > 0)
4363 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4364 infoPtr->nNumButtons,
4365 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4367 if (width < -1 || height < -1)
4369 /* Windows destroys the imagelist and seems to actually use negative
4370 * values to compute button sizes */
4371 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4372 return FALSE;
4375 /* width or height of -1 means no change */
4376 if (width != -1)
4377 infoPtr->nBitmapWidth = width;
4378 if (height != -1)
4379 infoPtr->nBitmapHeight = height;
4381 if ((himlDef == infoPtr->himlInt) &&
4382 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4384 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4385 infoPtr->nBitmapHeight);
4388 TOOLBAR_CalcToolbar(infoPtr);
4389 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4390 return TRUE;
4394 static LRESULT
4395 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4396 const TBBUTTONINFOW *lptbbi, BOOL isW)
4398 TBUTTON_INFO *btnPtr;
4399 INT nIndex;
4400 RECT oldBtnRect;
4402 if (lptbbi == NULL)
4403 return FALSE;
4404 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4405 return FALSE;
4407 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4408 if (nIndex == -1)
4409 return FALSE;
4411 btnPtr = &infoPtr->buttons[nIndex];
4412 if (lptbbi->dwMask & TBIF_COMMAND)
4413 btnPtr->idCommand = lptbbi->idCommand;
4414 if (lptbbi->dwMask & TBIF_IMAGE)
4415 btnPtr->iBitmap = lptbbi->iImage;
4416 if (lptbbi->dwMask & TBIF_LPARAM)
4417 btnPtr->dwData = lptbbi->lParam;
4418 if (lptbbi->dwMask & TBIF_SIZE)
4419 btnPtr->cx = lptbbi->cx;
4420 if (lptbbi->dwMask & TBIF_STATE)
4421 btnPtr->fsState = lptbbi->fsState;
4422 if (lptbbi->dwMask & TBIF_STYLE)
4423 btnPtr->fsStyle = lptbbi->fsStyle;
4425 if (lptbbi->dwMask & TBIF_TEXT)
4426 set_stringT( btnPtr, lptbbi->pszText, isW );
4428 /* save the button rect to see if we need to redraw the whole toolbar */
4429 oldBtnRect = btnPtr->rect;
4430 TOOLBAR_LayoutToolbar(infoPtr);
4432 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4433 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4434 else
4435 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4437 return TRUE;
4441 static LRESULT
4442 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4444 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4445 int top = default_top_margin(infoPtr);
4447 if ((cx < 0) || (cy < 0))
4449 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4450 return FALSE;
4453 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4455 /* The documentation claims you can only change the button size before
4456 * any button has been added. But this is wrong.
4457 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4458 * it to the toolbar, and it checks that the return value is nonzero - mjm
4459 * Further testing shows that we must actually perform the change too.
4462 * The documentation also does not mention that if 0 is supplied for
4463 * either size, the system changes it to the default of 24 wide and
4464 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4466 if (cx == 0) cx = 24;
4467 if (cy == 0) cy = 22;
4469 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4470 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4472 if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
4473 top != infoPtr->iTopMargin)
4475 infoPtr->nButtonWidth = cx;
4476 infoPtr->nButtonHeight = cy;
4477 infoPtr->iTopMargin = top;
4479 TOOLBAR_LayoutToolbar( infoPtr );
4480 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
4482 return TRUE;
4486 static LRESULT
4487 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4489 /* if setting to current values, ignore */
4490 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4491 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4492 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4493 infoPtr->cxMin, infoPtr->cxMax);
4494 return TRUE;
4497 /* save new values */
4498 infoPtr->cxMin = (short)LOWORD(lParam);
4499 infoPtr->cxMax = (short)HIWORD(lParam);
4501 /* otherwise we need to recalc the toolbar and in some cases
4502 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4503 which doesn't actually draw - GA). */
4504 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4505 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4507 TOOLBAR_CalcToolbar (infoPtr);
4509 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4511 return TRUE;
4515 static LRESULT
4516 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4518 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4519 return FALSE;
4521 infoPtr->buttons[nIndex].idCommand = nId;
4523 if (infoPtr->hwndToolTip) {
4525 FIXME("change tool tip!\n");
4529 return TRUE;
4533 static LRESULT
4534 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4536 HIMAGELIST himlTemp;
4537 INT id = 0;
4539 if (infoPtr->iVersion >= 5)
4540 id = wParam;
4542 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4543 &infoPtr->cimlDis, himl, id);
4545 /* FIXME: redraw ? */
4547 return (LRESULT)himlTemp;
4551 static LRESULT
4552 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4554 DWORD dwTemp;
4556 TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", infoPtr->hwndSelf, (DWORD)wParam, (DWORD)lParam);
4558 dwTemp = infoPtr->dwDTFlags;
4559 infoPtr->dwDTFlags =
4560 (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4562 return (LRESULT)dwTemp;
4565 /* This function differs a bit from what MSDN says it does:
4566 * 1. lParam contains extended style flags to OR with current style
4567 * (MSDN isn't clear on the OR bit)
4568 * 2. wParam appears to contain extended style flags to be reset
4569 * (MSDN says that this parameter is reserved)
4571 static LRESULT
4572 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4574 DWORD old_style = infoPtr->dwExStyle;
4576 TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
4578 if (mask)
4579 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4580 else
4581 infoPtr->dwExStyle = style;
4583 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4584 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4585 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4587 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4588 TOOLBAR_CalcToolbar(infoPtr);
4589 else
4590 TOOLBAR_LayoutToolbar(infoPtr);
4592 TOOLBAR_AutoSize(infoPtr);
4593 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4595 return old_style;
4599 static LRESULT
4600 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4602 HIMAGELIST himlTemp;
4603 INT id = 0;
4605 if (infoPtr->iVersion >= 5)
4606 id = wParam;
4608 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4610 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4611 &infoPtr->cimlHot, himl, id);
4613 /* FIXME: redraw ? */
4615 return (LRESULT)himlTemp;
4619 /* Makes previous hot button no longer hot, makes the specified
4620 * button hot and sends appropriate notifications. dwReason is one or
4621 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4622 * NOTE 1: this function does not validate nHit
4623 * NOTE 2: the name of this function is completely made up and
4624 * not based on any documentation from Microsoft. */
4625 static void
4626 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4628 if (infoPtr->nHotItem != nHit)
4630 NMTBHOTITEM nmhotitem;
4631 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4633 nmhotitem.dwFlags = dwReason;
4634 if(infoPtr->nHotItem >= 0)
4636 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4637 nmhotitem.idOld = oldBtnPtr->idCommand;
4639 else
4641 nmhotitem.dwFlags |= HICF_ENTERING;
4642 nmhotitem.idOld = 0;
4645 if (nHit >= 0)
4647 btnPtr = &infoPtr->buttons[nHit];
4648 nmhotitem.idNew = btnPtr->idCommand;
4650 else
4652 nmhotitem.dwFlags |= HICF_LEAVING;
4653 nmhotitem.idNew = 0;
4656 /* now change the hot and invalidate the old and new buttons - if the
4657 * parent agrees */
4658 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4660 if (oldBtnPtr) {
4661 oldBtnPtr->bHot = FALSE;
4662 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4664 /* setting disabled buttons as hot fails even if the notify contains the button id */
4665 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4666 btnPtr->bHot = TRUE;
4667 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4668 infoPtr->nHotItem = nHit;
4670 else
4671 infoPtr->nHotItem = -1;
4676 static LRESULT
4677 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4679 INT nOldHotItem = infoPtr->nHotItem;
4681 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4683 if (nHotItem >= infoPtr->nNumButtons)
4684 return infoPtr->nHotItem;
4686 if (nHotItem < 0)
4687 nHotItem = -1;
4689 /* NOTE: an application can still remove the hot item even if anchor
4690 * highlighting is enabled */
4692 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4694 if (nOldHotItem < 0)
4695 return -1;
4697 return (LRESULT)nOldHotItem;
4701 static LRESULT
4702 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4704 HIMAGELIST himlTemp;
4705 INT oldButtonWidth = infoPtr->nButtonWidth;
4706 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4707 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4708 INT i, id = 0;
4710 if (infoPtr->iVersion >= 5)
4711 id = wParam;
4713 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4714 &infoPtr->cimlDef, himl, id);
4716 infoPtr->nNumBitmaps = 0;
4717 for (i = 0; i < infoPtr->cimlDef; i++)
4718 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4720 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4721 &infoPtr->nBitmapHeight))
4723 infoPtr->nBitmapWidth = 1;
4724 infoPtr->nBitmapHeight = 1;
4726 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4728 TOOLBAR_CalcToolbar(infoPtr);
4729 if (infoPtr->nButtonWidth < oldButtonWidth)
4730 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4733 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4734 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4735 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4737 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4739 return (LRESULT)himlTemp;
4743 static LRESULT
4744 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4746 infoPtr->nIndent = nIndent;
4748 TRACE("\n");
4750 /* process only on indent changing */
4751 if(infoPtr->nIndent != nIndent)
4753 infoPtr->nIndent = nIndent;
4754 TOOLBAR_CalcToolbar (infoPtr);
4755 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4758 return TRUE;
4762 static LRESULT
4763 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4765 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4767 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4769 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4770 return 0;
4773 if ((lptbim->iButton == -1) ||
4774 ((lptbim->iButton < infoPtr->nNumButtons) &&
4775 (lptbim->iButton >= 0)))
4777 infoPtr->tbim = *lptbim;
4778 /* FIXME: don't need to update entire toolbar */
4779 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4781 else
4782 ERR("Invalid button index %d\n", lptbim->iButton);
4784 return 0;
4788 static LRESULT
4789 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4791 infoPtr->clrInsertMark = clr;
4793 /* FIXME: don't need to update entire toolbar */
4794 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4796 return 0;
4800 static LRESULT
4801 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4803 infoPtr->nMaxTextRows = nMaxRows;
4805 TOOLBAR_CalcToolbar(infoPtr);
4806 return TRUE;
4810 /* MSDN gives slightly wrong info on padding.
4811 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4812 * 2. It is not used to create a blank area between the edge of the button
4813 * and the text or image if TBSTYLE_LIST is set. It is used to control
4814 * the gap between the image and text.
4815 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4816 * to control the bottom and right borders [with the border being
4817 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4818 * is shared evenly on both sides of the button.
4819 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4821 static LRESULT
4822 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4824 DWORD oldPad;
4826 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4827 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4828 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4829 TRACE("cx=%d, cy=%d\n",
4830 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4831 return (LRESULT) oldPad;
4835 static LRESULT
4836 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4838 HWND hwndOldNotify;
4840 TRACE("\n");
4842 hwndOldNotify = infoPtr->hwndNotify;
4843 infoPtr->hwndNotify = hParent;
4845 return (LRESULT)hwndOldNotify;
4849 static LRESULT
4850 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4852 int rows = LOWORD(wParam);
4853 BOOL bLarger = HIWORD(wParam);
4855 TRACE("\n");
4857 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4859 if(infoPtr->nRows != rows)
4861 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4862 int curColumn = 0; /* Current column */
4863 int curRow = 0; /* Current row */
4864 int hidden = 0; /* Number of hidden buttons */
4865 int seps = 0; /* Number of separators */
4866 int idealWrap = 0; /* Ideal wrap point */
4867 int i;
4868 BOOL wrap;
4871 Calculate new size and wrap points - Under windows, setrows will
4872 change the dimensions if needed to show the number of requested
4873 rows (if CCS_NORESIZE is set), or will take up the whole window
4874 (if no CCS_NORESIZE).
4876 Basic algorithm - If N buttons, and y rows requested, each row
4877 contains N/y buttons.
4879 FIXME: Handling of separators not obvious from testing results
4880 FIXME: Take width of window into account?
4883 /* Loop through the buttons one by one counting key items */
4884 for (i = 0; i < infoPtr->nNumButtons; i++ )
4886 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4887 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4888 hidden++;
4889 else if (btnPtr[i].fsStyle & BTNS_SEP)
4890 seps++;
4893 /* FIXME: Separators make this quite complex */
4894 if (seps) FIXME("Separators unhandled\n");
4896 /* Round up so more per line, i.e., less rows */
4897 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4899 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4900 achieve the requested number of rows. */
4901 if (bLarger && idealWrap > 1)
4903 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4904 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4906 if (resRows < rows && moreRows > rows)
4908 idealWrap--;
4909 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4913 curColumn = curRow = 0;
4914 wrap = FALSE;
4915 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4916 infoPtr->nNumButtons, hidden, rows);
4918 for (i = 0; i < infoPtr->nNumButtons; i++ )
4920 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4921 continue;
4923 /* Step on, wrap if necessary or flag next to wrap */
4924 if (!wrap) {
4925 curColumn++;
4926 } else {
4927 wrap = FALSE;
4928 curColumn = 1;
4929 curRow++;
4932 if (curColumn > (idealWrap-1)) {
4933 wrap = TRUE;
4934 btnPtr[i].fsState |= TBSTATE_WRAP;
4938 TRACE("Result - %d rows\n", curRow + 1);
4940 /* recalculate toolbar */
4941 TOOLBAR_CalcToolbar (infoPtr);
4943 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4944 if NORESIZE is NOT set, then the toolbar will always be resized to
4945 take up the whole window. With it set, sizing needs to be manual. */
4946 if (infoPtr->dwStyle & CCS_NORESIZE) {
4947 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4948 infoPtr->rcBound.right - infoPtr->rcBound.left,
4949 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4950 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4953 /* repaint toolbar */
4954 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4957 /* return bounding rectangle */
4958 if (lprc) {
4959 lprc->left = infoPtr->rcBound.left;
4960 lprc->right = infoPtr->rcBound.right;
4961 lprc->top = infoPtr->rcBound.top;
4962 lprc->bottom = infoPtr->rcBound.bottom;
4965 return 0;
4969 static LRESULT
4970 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4972 TBUTTON_INFO *btnPtr;
4973 INT nIndex;
4975 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4976 if (nIndex == -1)
4977 return FALSE;
4979 btnPtr = &infoPtr->buttons[nIndex];
4981 /* if hidden state has changed the invalidate entire window and recalc */
4982 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4983 btnPtr->fsState = LOWORD(lParam);
4984 TOOLBAR_CalcToolbar (infoPtr);
4985 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4986 return TRUE;
4989 /* process state changing if current state doesn't match new state */
4990 if(btnPtr->fsState != LOWORD(lParam))
4992 btnPtr->fsState = LOWORD(lParam);
4993 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4996 return TRUE;
4999 static inline void unwrap(TOOLBAR_INFO *info)
5001 int i;
5003 for (i = 0; i < info->nNumButtons; i++)
5004 info->buttons[i].fsState &= ~TBSTATE_WRAP;
5007 static LRESULT
5008 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
5010 DWORD dwOldStyle = infoPtr->dwStyle;
5012 TRACE("new style 0x%08x\n", style);
5014 if (style & TBSTYLE_LIST)
5015 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
5016 else
5017 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
5019 infoPtr->dwStyle = style;
5020 TOOLBAR_CheckStyle(infoPtr);
5022 if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5024 if (dwOldStyle & TBSTYLE_WRAPABLE)
5025 unwrap(infoPtr);
5026 TOOLBAR_CalcToolbar(infoPtr);
5028 else if ((dwOldStyle ^ style) & CCS_VERT)
5029 TOOLBAR_LayoutToolbar(infoPtr);
5031 /* only resize if one of the CCS_* styles was changed */
5032 if ((dwOldStyle ^ style) & COMMON_STYLES)
5034 TOOLBAR_AutoSize(infoPtr);
5035 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5038 return 0;
5042 static inline LRESULT
5043 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
5045 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5047 infoPtr->hwndToolTip = hwndTooltip;
5048 return 0;
5052 static LRESULT
5053 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
5055 BOOL bTemp;
5057 TRACE("%s hwnd=%p\n",
5058 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
5060 bTemp = infoPtr->bUnicode;
5061 infoPtr->bUnicode = (BOOL)wParam;
5063 return bTemp;
5067 static LRESULT
5068 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5070 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5071 comctl32_color.clrBtnHighlight :
5072 infoPtr->clrBtnHighlight;
5073 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5074 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5075 return 1;
5079 static LRESULT
5080 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5082 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5083 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5084 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5086 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5087 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5088 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5089 return 0;
5093 static LRESULT
5094 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5096 INT iOldVersion = infoPtr->iVersion;
5098 infoPtr->iVersion = iVersion;
5100 if (infoPtr->iVersion >= 5)
5101 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5103 return iOldVersion;
5107 static LRESULT
5108 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
5110 WORD iString = HIWORD(wParam);
5111 WORD buffersize = LOWORD(wParam);
5112 LRESULT ret = -1;
5114 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
5116 if (iString < infoPtr->nNumStrings)
5118 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5119 ret--;
5121 TRACE("returning %s\n", debugstr_a(str));
5123 else
5124 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5126 return ret;
5130 static LRESULT
5131 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5133 WORD iString = HIWORD(wParam);
5134 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5135 LRESULT ret = -1;
5137 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5139 if (iString < infoPtr->nNumStrings)
5141 len = min(len, strlenW(infoPtr->strings[iString]));
5142 ret = (len+1)*sizeof(WCHAR);
5143 if (str)
5145 memcpy(str, infoPtr->strings[iString], ret);
5146 str[len] = '\0';
5148 ret = len;
5150 TRACE("returning %s\n", debugstr_w(str));
5152 else
5153 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5155 return ret;
5158 static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5160 SIZE * pSize = (SIZE*)lParam;
5161 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5162 return 0;
5165 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5166 * caller to specify a reason why the hot item changed (rather than just the
5167 * HICF_OTHER that TB_SETHOTITEM sends). */
5168 static LRESULT
5169 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5171 INT nOldHotItem = infoPtr->nHotItem;
5173 TRACE("old item=%d, new item=%d, flags=%08x\n",
5174 nOldHotItem, nHotItem, (DWORD)lParam);
5176 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5177 nHotItem = -1;
5179 /* NOTE: an application can still remove the hot item even if anchor
5180 * highlighting is enabled */
5182 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5184 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5187 /* Sets the toolbar global iListGap parameter which controls the amount of
5188 * spacing between the image and the text of buttons for TBSTYLE_LIST
5189 * toolbars. */
5190 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5192 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5194 infoPtr->iListGap = iListGap;
5196 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5198 return 0;
5201 /* Returns the number of maximum number of image lists associated with the
5202 * various states. */
5203 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5205 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5207 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5210 static LRESULT
5211 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5213 LPSIZE lpsize = (LPSIZE)lParam;
5215 if (lpsize == NULL)
5216 return FALSE;
5219 * Testing shows the following:
5220 * wParam = 0 adjust cx value
5221 * = 1 set cy value to max size.
5222 * lParam pointer to SIZE structure
5225 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5226 wParam, lParam, lpsize->cx, lpsize->cy);
5228 switch(wParam) {
5229 case 0:
5230 if (lpsize->cx == -1) {
5231 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5233 else if(HIWORD(lpsize->cx)) {
5234 RECT rc;
5235 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5237 GetWindowRect(infoPtr->hwndSelf, &rc);
5238 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5239 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5240 lpsize->cx = max(rc.right-rc.left,
5241 infoPtr->rcBound.right - infoPtr->rcBound.left);
5243 else {
5244 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5246 break;
5247 case 1:
5248 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5249 break;
5250 default:
5251 FIXME("Unknown wParam %ld\n", wParam);
5252 return 0;
5254 TRACE("set to -> 0x%08x 0x%08x\n",
5255 lpsize->cx, lpsize->cy);
5256 return 1;
5259 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5261 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5263 InvalidateRect(hwnd, NULL, TRUE);
5264 return 1;
5268 static LRESULT
5269 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5271 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5272 LOGFONTW logFont;
5274 TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
5276 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5277 GetClientRect(hwnd, &infoPtr->client_rect);
5278 infoPtr->bUnicode = infoPtr->hwndNotify &&
5279 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5280 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5282 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5283 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5285 OpenThemeData (hwnd, themeClass);
5287 TOOLBAR_CheckStyle (infoPtr);
5289 return 0;
5293 static LRESULT
5294 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5296 INT i;
5298 /* delete tooltip control */
5299 if (infoPtr->hwndToolTip)
5300 DestroyWindow (infoPtr->hwndToolTip);
5302 /* delete temporary buffer for tooltip text */
5303 Free (infoPtr->pszTooltipText);
5304 Free (infoPtr->bitmaps); /* bitmaps list */
5306 /* delete button data */
5307 for (i = 0; i < infoPtr->nNumButtons; i++)
5308 free_string( infoPtr->buttons + i );
5309 Free (infoPtr->buttons);
5311 /* delete strings */
5312 if (infoPtr->strings) {
5313 for (i = 0; i < infoPtr->nNumStrings; i++)
5314 Free (infoPtr->strings[i]);
5316 Free (infoPtr->strings);
5319 /* destroy internal image list */
5320 if (infoPtr->himlInt)
5321 ImageList_Destroy (infoPtr->himlInt);
5323 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5324 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5325 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5327 /* delete default font */
5328 DeleteObject (infoPtr->hDefaultFont);
5330 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
5332 /* free toolbar info data */
5333 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5334 Free (infoPtr);
5336 return 0;
5340 static LRESULT
5341 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5343 NMTBCUSTOMDRAW tbcd;
5344 INT ret = FALSE;
5345 DWORD ntfret;
5346 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5347 DWORD dwEraseCustDraw = 0;
5349 /* the app has told us not to redraw the toolbar */
5350 if (!infoPtr->bDoRedraw)
5351 return FALSE;
5353 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5354 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5355 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5356 tbcd.nmcd.hdc = (HDC)wParam;
5357 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5358 dwEraseCustDraw = ntfret & 0xffff;
5360 /* FIXME: in general the return flags *can* be or'ed together */
5361 switch (dwEraseCustDraw)
5363 case CDRF_DODEFAULT:
5364 break;
5365 case CDRF_SKIPDEFAULT:
5366 return TRUE;
5367 default:
5368 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5369 infoPtr->hwndSelf, ntfret);
5373 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5374 * to my parent for processing.
5376 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5377 POINT pt, ptorig;
5378 HDC hdc = (HDC)wParam;
5379 HWND parent;
5381 pt.x = 0;
5382 pt.y = 0;
5383 parent = GetParent(infoPtr->hwndSelf);
5384 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5385 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5386 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5387 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5389 if (!ret)
5390 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5392 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5393 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5394 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5395 tbcd.nmcd.hdc = (HDC)wParam;
5396 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5397 dwEraseCustDraw = ntfret & 0xffff;
5398 switch (dwEraseCustDraw)
5400 case CDRF_DODEFAULT:
5401 break;
5402 case CDRF_SKIPDEFAULT:
5403 return TRUE;
5404 default:
5405 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5406 infoPtr->hwndSelf, ntfret);
5409 return ret;
5413 static inline LRESULT
5414 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5416 return (LRESULT)infoPtr->hFont;
5420 static void
5421 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5423 INT i;
5424 INT nNewHotItem = infoPtr->nHotItem;
5426 for (i = 0; i < infoPtr->nNumButtons; i++)
5428 /* did we wrap? */
5429 if ((nNewHotItem + iDirection < 0) ||
5430 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5432 NMTBWRAPHOTITEM nmtbwhi;
5433 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5434 nmtbwhi.iDirection = iDirection;
5435 nmtbwhi.dwReason = dwReason;
5437 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5438 return;
5441 nNewHotItem += iDirection;
5442 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5444 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5445 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5447 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5448 break;
5453 static LRESULT
5454 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5456 NMKEY nmkey;
5458 nmkey.nVKey = (UINT)wParam;
5459 nmkey.uFlags = HIWORD(lParam);
5461 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5462 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5464 switch ((UINT)wParam)
5466 case VK_LEFT:
5467 case VK_UP:
5468 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5469 break;
5470 case VK_RIGHT:
5471 case VK_DOWN:
5472 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5473 break;
5474 case VK_SPACE:
5475 case VK_RETURN:
5476 if ((infoPtr->nHotItem >= 0) &&
5477 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5479 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5480 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5481 (LPARAM)infoPtr->hwndSelf);
5483 break;
5486 return 0;
5490 static LRESULT
5491 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5493 POINT pt;
5494 BOOL button;
5496 pt.x = (short)LOWORD(lParam);
5497 pt.y = (short)HIWORD(lParam);
5498 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5500 if (button)
5501 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5502 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5503 TOOLBAR_Customize (infoPtr);
5505 return 0;
5509 static LRESULT
5510 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5512 TBUTTON_INFO *btnPtr;
5513 POINT pt;
5514 INT nHit;
5515 NMTOOLBARA nmtb;
5516 NMMOUSE nmmouse;
5517 BOOL bDragKeyPressed;
5518 BOOL button;
5520 TRACE("\n");
5522 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5523 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5524 else
5525 bDragKeyPressed = (wParam & MK_SHIFT);
5527 if (infoPtr->hwndToolTip)
5528 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5529 WM_LBUTTONDOWN, wParam, lParam);
5531 pt.x = (short)LOWORD(lParam);
5532 pt.y = (short)HIWORD(lParam);
5533 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5535 if (button)
5537 btnPtr = &infoPtr->buttons[nHit];
5539 if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5541 infoPtr->nButtonDrag = nHit;
5542 SetCapture (infoPtr->hwndSelf);
5544 /* If drag cursor has not been loaded, load it.
5545 * Note: it doesn't need to be freed */
5546 if (!hCursorDrag)
5547 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5548 SetCursor(hCursorDrag);
5550 else
5552 RECT arrowRect;
5553 infoPtr->nOldHit = nHit;
5555 CopyRect(&arrowRect, &btnPtr->rect);
5556 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5558 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5559 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5560 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5561 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5562 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5563 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5565 LRESULT res;
5567 /* draw in pressed state */
5568 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5569 btnPtr->fsState |= TBSTATE_PRESSED;
5570 else
5571 btnPtr->bDropDownPressed = TRUE;
5572 RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5574 memset(&nmtb, 0, sizeof(nmtb));
5575 nmtb.iItem = btnPtr->idCommand;
5576 nmtb.rcButton = btnPtr->rect;
5577 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5578 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5580 if (res != TBDDRET_TREATPRESSED)
5582 MSG msg;
5584 /* redraw button in unpressed state */
5585 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5586 btnPtr->fsState &= ~TBSTATE_PRESSED;
5587 else
5588 btnPtr->bDropDownPressed = FALSE;
5589 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5591 /* find and set hot item */
5592 GetCursorPos(&pt);
5593 ScreenToClient(infoPtr->hwndSelf, &pt);
5594 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5595 if (!infoPtr->bAnchor || button)
5596 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5598 /* remove any left mouse button down or double-click messages
5599 * so that we can get a toggle effect on the button */
5600 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5601 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5604 return 0;
5606 /* otherwise drop through and process as pushed */
5608 infoPtr->bCaptured = TRUE;
5609 infoPtr->nButtonDown = nHit;
5610 infoPtr->bDragOutSent = FALSE;
5612 btnPtr->fsState |= TBSTATE_PRESSED;
5614 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5616 if (btnPtr->fsState & TBSTATE_ENABLED)
5617 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5618 UpdateWindow(infoPtr->hwndSelf);
5619 SetCapture (infoPtr->hwndSelf);
5622 memset(&nmtb, 0, sizeof(nmtb));
5623 nmtb.iItem = btnPtr->idCommand;
5624 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5627 nmmouse.dwHitInfo = nHit;
5629 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5630 if (!button)
5631 nmmouse.dwItemSpec = -1;
5632 else
5634 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5635 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5638 ClientToScreen(infoPtr->hwndSelf, &pt);
5639 nmmouse.pt = pt;
5641 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5642 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5644 return 0;
5647 static LRESULT
5648 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5650 TBUTTON_INFO *btnPtr;
5651 POINT pt;
5652 INT nHit;
5653 INT nOldIndex = -1;
5654 NMHDR hdr;
5655 NMMOUSE nmmouse;
5656 NMTOOLBARA nmtb;
5657 BOOL button;
5659 if (infoPtr->hwndToolTip)
5660 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5661 WM_LBUTTONUP, wParam, lParam);
5663 pt.x = (short)LOWORD(lParam);
5664 pt.y = (short)HIWORD(lParam);
5665 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5667 if (!infoPtr->bAnchor || button)
5668 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5670 if (infoPtr->nButtonDrag >= 0) {
5671 RECT rcClient;
5672 NMHDR hdr;
5674 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5675 ReleaseCapture();
5676 /* reset cursor */
5677 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5679 GetClientRect(infoPtr->hwndSelf, &rcClient);
5680 if (PtInRect(&rcClient, pt))
5682 INT nButton = -1;
5683 if (nHit >= 0)
5684 nButton = nHit;
5685 else if (nHit < -1)
5686 nButton = -nHit;
5687 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5688 nButton = -nHit;
5690 if (nButton == infoPtr->nButtonDrag)
5692 /* if the button is moved sightly left and we have a
5693 * separator there then remove it */
5694 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5696 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5697 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5699 else /* else insert a separator before the dragged button */
5701 TBBUTTON tbb;
5702 memset(&tbb, 0, sizeof(tbb));
5703 tbb.fsStyle = BTNS_SEP;
5704 tbb.iString = -1;
5705 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5708 else
5710 if (nButton == -1)
5712 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5713 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5714 else
5715 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5717 else
5718 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5721 else
5723 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5724 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5727 /* button under cursor changed so need to re-set hot item */
5728 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5729 infoPtr->nButtonDrag = -1;
5731 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5733 else if (infoPtr->nButtonDown >= 0) {
5734 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5735 btnPtr->fsState &= ~TBSTATE_PRESSED;
5737 if (btnPtr->fsStyle & BTNS_CHECK) {
5738 if (btnPtr->fsStyle & BTNS_GROUP) {
5739 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5740 nHit);
5741 if ((nOldIndex != nHit) &&
5742 (nOldIndex != -1))
5743 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5744 btnPtr->fsState |= TBSTATE_CHECKED;
5746 else {
5747 if (btnPtr->fsState & TBSTATE_CHECKED)
5748 btnPtr->fsState &= ~TBSTATE_CHECKED;
5749 else
5750 btnPtr->fsState |= TBSTATE_CHECKED;
5754 if (nOldIndex != -1)
5755 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5758 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5759 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5760 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5762 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5763 ReleaseCapture ();
5764 infoPtr->nButtonDown = -1;
5766 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5767 TOOLBAR_SendNotify (&hdr, infoPtr,
5768 NM_RELEASEDCAPTURE);
5770 memset(&nmtb, 0, sizeof(nmtb));
5771 nmtb.iItem = btnPtr->idCommand;
5772 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5773 TBN_ENDDRAG);
5775 if (btnPtr->fsState & TBSTATE_ENABLED)
5777 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5778 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5780 /* In case we have just been destroyed... */
5781 if(!IsWindow(infoPtr->hwndSelf))
5782 return 0;
5786 /* !!! Undocumented - toolbar at 4.71 level and above sends
5787 * NM_CLICK with the NMMOUSE structure. */
5788 nmmouse.dwHitInfo = nHit;
5790 if (!button)
5791 nmmouse.dwItemSpec = -1;
5792 else
5794 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5795 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5798 ClientToScreen(infoPtr->hwndSelf, &pt);
5799 nmmouse.pt = pt;
5801 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5802 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5804 return 0;
5807 static LRESULT
5808 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5810 INT nHit;
5811 NMMOUSE nmmouse;
5812 POINT pt;
5813 BOOL button;
5815 pt.x = (short)LOWORD(lParam);
5816 pt.y = (short)HIWORD(lParam);
5818 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5819 nmmouse.dwHitInfo = nHit;
5821 if (!button) {
5822 nmmouse.dwItemSpec = -1;
5823 } else {
5824 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5825 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5828 ClientToScreen(infoPtr->hwndSelf, &pt);
5829 nmmouse.pt = pt;
5831 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5832 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5834 return 0;
5837 static LRESULT
5838 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5840 NMHDR nmhdr;
5842 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5843 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5845 return 0;
5848 static LRESULT
5849 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5851 TBUTTON_INFO *btnPtr;
5853 infoPtr->bCaptured = FALSE;
5855 if (infoPtr->nButtonDown >= 0)
5857 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5858 btnPtr->fsState &= ~TBSTATE_PRESSED;
5860 infoPtr->nOldHit = -1;
5862 if (btnPtr->fsState & TBSTATE_ENABLED)
5863 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5865 return 0;
5868 static LRESULT
5869 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5871 /* don't remove hot effects when in anchor highlighting mode or when a
5872 * drop-down button is pressed */
5873 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5875 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5876 if (!hotBtnPtr->bDropDownPressed)
5877 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5880 if (infoPtr->nOldHit < 0)
5881 return TRUE;
5883 /* If the last button we were over is depressed then make it not */
5884 /* depressed and redraw it */
5885 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5887 TBUTTON_INFO *btnPtr;
5888 RECT rc1;
5890 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5892 btnPtr->fsState &= ~TBSTATE_PRESSED;
5894 rc1 = btnPtr->rect;
5895 InflateRect (&rc1, 1, 1);
5896 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5899 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5901 NMTOOLBARW nmt;
5902 ZeroMemory(&nmt, sizeof(nmt));
5903 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5904 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5905 infoPtr->bDragOutSent = TRUE;
5908 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5910 return TRUE;
5913 static LRESULT
5914 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5916 POINT pt;
5917 TRACKMOUSEEVENT trackinfo;
5918 INT nHit;
5919 TBUTTON_INFO *btnPtr;
5920 BOOL button;
5922 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5923 TOOLBAR_TooltipCreateControl(infoPtr);
5925 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5926 /* fill in the TRACKMOUSEEVENT struct */
5927 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5928 trackinfo.dwFlags = TME_QUERY;
5930 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5931 _TrackMouseEvent(&trackinfo);
5933 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5934 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5935 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5936 trackinfo.hwndTrack = infoPtr->hwndSelf;
5938 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5939 /* and can properly deactivate the hot toolbar button */
5940 _TrackMouseEvent(&trackinfo);
5944 if (infoPtr->hwndToolTip)
5945 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5946 WM_MOUSEMOVE, wParam, lParam);
5948 pt.x = (short)LOWORD(lParam);
5949 pt.y = (short)HIWORD(lParam);
5951 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5953 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5954 && (!infoPtr->bAnchor || button))
5955 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5957 if (infoPtr->nOldHit != nHit)
5959 if (infoPtr->bCaptured)
5961 if (!infoPtr->bDragOutSent)
5963 NMTOOLBARW nmt;
5964 ZeroMemory(&nmt, sizeof(nmt));
5965 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5966 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5967 infoPtr->bDragOutSent = TRUE;
5970 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5971 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5972 btnPtr->fsState &= ~TBSTATE_PRESSED;
5973 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5975 else if (nHit == infoPtr->nButtonDown) {
5976 btnPtr->fsState |= TBSTATE_PRESSED;
5977 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5979 infoPtr->nOldHit = nHit;
5983 return 0;
5987 static inline LRESULT
5988 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5990 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
5991 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5992 /* else */
5993 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5997 static inline LRESULT
5998 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
6000 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6001 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6003 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6007 static LRESULT
6008 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
6010 TOOLBAR_INFO *infoPtr;
6011 DWORD styleadd = 0;
6013 /* allocate memory for info structure */
6014 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6015 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6017 /* paranoid!! */
6018 infoPtr->dwStructSize = sizeof(TBBUTTON);
6019 infoPtr->nRows = 1;
6021 /* initialize info structure */
6022 infoPtr->nButtonWidth = 23;
6023 infoPtr->nButtonHeight = 22;
6024 infoPtr->nBitmapHeight = 16;
6025 infoPtr->nBitmapWidth = 16;
6027 infoPtr->nMaxTextRows = 1;
6028 infoPtr->cxMin = -1;
6029 infoPtr->cxMax = -1;
6030 infoPtr->nNumBitmaps = 0;
6031 infoPtr->nNumStrings = 0;
6033 infoPtr->bCaptured = FALSE;
6034 infoPtr->nButtonDown = -1;
6035 infoPtr->nButtonDrag = -1;
6036 infoPtr->nOldHit = -1;
6037 infoPtr->nHotItem = -1;
6038 infoPtr->hwndNotify = lpcs->hwndParent;
6039 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6040 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
6041 infoPtr->bDragOutSent = FALSE;
6042 infoPtr->iVersion = 0;
6043 infoPtr->hwndSelf = hwnd;
6044 infoPtr->bDoRedraw = TRUE;
6045 infoPtr->clrBtnHighlight = CLR_DEFAULT;
6046 infoPtr->clrBtnShadow = CLR_DEFAULT;
6047 infoPtr->szPadding.cx = DEFPAD_CX;
6048 infoPtr->szPadding.cy = DEFPAD_CY;
6049 infoPtr->iListGap = DEFLISTGAP;
6050 infoPtr->iTopMargin = default_top_margin(infoPtr);
6051 infoPtr->dwStyle = lpcs->style;
6052 infoPtr->tbim.iButton = -1;
6054 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6055 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6056 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6057 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6060 /* I think the code below is a bug, but it is the way that the native
6061 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6062 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6063 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6064 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6065 * Somehow, the only cases of this seem to be MFC programs.
6067 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6068 * does not occur in the WM_STYLECHANGING routine.
6069 * (Guy Albertelli 9/2001)
6072 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
6073 && !(lpcs->style & TBSTYLE_TRANSPARENT))
6074 styleadd |= TBSTYLE_TRANSPARENT;
6075 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6076 styleadd |= CCS_TOP; /* default to top */
6077 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6080 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
6084 static LRESULT
6085 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6087 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6088 RECT rcWindow;
6089 HDC hdc;
6091 if (dwStyle & WS_MINIMIZE)
6092 return 0; /* Nothing to do */
6094 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6096 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6097 return 0;
6099 if (!(dwStyle & CCS_NODIVIDER))
6101 GetWindowRect (hwnd, &rcWindow);
6102 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6103 if( dwStyle & WS_BORDER )
6104 InflateRect (&rcWindow, -1, -1);
6105 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6108 ReleaseDC( hwnd, hdc );
6110 return 0;
6114 /* handles requests from the tooltip control on what text to display */
6115 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6117 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6119 TRACE("button index = %d\n", index);
6121 Free (infoPtr->pszTooltipText);
6122 infoPtr->pszTooltipText = NULL;
6124 if (index < 0)
6125 return 0;
6127 if (infoPtr->bUnicode)
6129 WCHAR wszBuffer[INFOTIPSIZE+1];
6130 NMTBGETINFOTIPW tbgit;
6131 unsigned int len; /* in chars */
6133 wszBuffer[0] = '\0';
6134 wszBuffer[INFOTIPSIZE] = '\0';
6136 tbgit.pszText = wszBuffer;
6137 tbgit.cchTextMax = INFOTIPSIZE;
6138 tbgit.iItem = lpnmtdi->hdr.idFrom;
6139 tbgit.lParam = infoPtr->buttons[index].dwData;
6141 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6143 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6145 len = strlenW(tbgit.pszText);
6146 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6148 /* need to allocate temporary buffer in infoPtr as there
6149 * isn't enough space in buffer passed to us by the
6150 * tooltip control */
6151 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6152 if (infoPtr->pszTooltipText)
6154 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6155 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6156 return 0;
6159 else if (len > 0)
6161 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6162 return 0;
6165 else
6167 CHAR szBuffer[INFOTIPSIZE+1];
6168 NMTBGETINFOTIPA tbgit;
6169 unsigned int len; /* in chars */
6171 szBuffer[0] = '\0';
6172 szBuffer[INFOTIPSIZE] = '\0';
6174 tbgit.pszText = szBuffer;
6175 tbgit.cchTextMax = INFOTIPSIZE;
6176 tbgit.iItem = lpnmtdi->hdr.idFrom;
6177 tbgit.lParam = infoPtr->buttons[index].dwData;
6179 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6181 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6183 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6184 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6186 /* need to allocate temporary buffer in infoPtr as there
6187 * isn't enough space in buffer passed to us by the
6188 * tooltip control */
6189 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6190 if (infoPtr->pszTooltipText)
6192 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6193 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6194 return 0;
6197 else if (tbgit.pszText && tbgit.pszText[0])
6199 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6200 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6201 return 0;
6205 /* if button has text, but it is not shown then automatically
6206 * use that text as tooltip */
6207 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6208 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6210 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6211 unsigned int len = pszText ? strlenW(pszText) : 0;
6213 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6215 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6217 /* need to allocate temporary buffer in infoPtr as there
6218 * isn't enough space in buffer passed to us by the
6219 * tooltip control */
6220 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6221 if (infoPtr->pszTooltipText)
6223 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6224 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6225 return 0;
6228 else if (len > 0)
6230 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6231 return 0;
6235 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6237 /* last resort: send notification on to app */
6238 /* FIXME: find out what is really used here */
6239 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6243 static inline LRESULT
6244 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6246 switch (lpnmh->code)
6248 case PGN_CALCSIZE:
6250 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6252 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6253 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6254 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6255 lppgc->iWidth);
6257 else {
6258 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6259 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6260 lppgc->iHeight);
6262 return 0;
6265 case PGN_SCROLL:
6267 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6269 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6270 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6271 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6272 lppgs->iScroll, lppgs->iDir);
6273 return 0;
6276 case TTN_GETDISPINFOW:
6277 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6279 case TTN_GETDISPINFOA:
6280 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6281 return 0;
6283 default:
6284 return 0;
6289 static LRESULT
6290 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6292 LRESULT format;
6294 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6296 if (lParam == NF_QUERY)
6297 return NFR_UNICODE;
6299 if (lParam == NF_REQUERY) {
6300 format = SendMessageW(infoPtr->hwndNotify,
6301 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6302 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6303 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6304 format);
6305 format = NFR_ANSI;
6307 return format;
6309 return 0;
6313 static LRESULT
6314 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6316 HDC hdc;
6317 PAINTSTRUCT ps;
6319 /* fill ps.rcPaint with a default rect */
6320 ps.rcPaint = infoPtr->rcBound;
6322 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6324 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6326 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6327 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6329 return 0;
6333 static LRESULT
6334 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6336 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6338 /* make first item hot */
6339 if (infoPtr->nNumButtons > 0)
6340 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6342 return 0;
6345 static LRESULT
6346 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6348 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6350 if (hFont == 0)
6351 infoPtr->hFont = infoPtr->hDefaultFont;
6352 else
6353 infoPtr->hFont = hFont;
6355 TOOLBAR_CalcToolbar(infoPtr);
6357 if (Redraw)
6358 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6359 return 1;
6362 static LRESULT
6363 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6364 /*****************************************************
6366 * Function;
6367 * Handles the WM_SETREDRAW message.
6369 * Documentation:
6370 * According to testing V4.71 of COMCTL32 returns the
6371 * *previous* status of the redraw flag (either 0 or 1)
6372 * instead of the MSDN documented value of 0 if handled.
6373 * (For laughs see the "consistency" with same function
6374 * in rebar.)
6376 *****************************************************/
6378 BOOL oldredraw = infoPtr->bDoRedraw;
6380 TRACE("set to %s\n",
6381 (wParam) ? "TRUE" : "FALSE");
6382 infoPtr->bDoRedraw = (BOOL) wParam;
6383 if (wParam) {
6384 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6386 return (oldredraw) ? 1 : 0;
6390 static LRESULT
6391 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6393 TRACE("sizing toolbar!\n");
6395 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6397 RECT delta_width, delta_height, client, dummy;
6398 DWORD min_x, max_x, min_y, max_y;
6399 TBUTTON_INFO *btnPtr;
6400 INT i;
6402 GetClientRect(infoPtr->hwndSelf, &client);
6403 if(client.right > infoPtr->client_rect.right)
6405 min_x = infoPtr->client_rect.right;
6406 max_x = client.right;
6408 else
6410 max_x = infoPtr->client_rect.right;
6411 min_x = client.right;
6413 if(client.bottom > infoPtr->client_rect.bottom)
6415 min_y = infoPtr->client_rect.bottom;
6416 max_y = client.bottom;
6418 else
6420 max_y = infoPtr->client_rect.bottom;
6421 min_y = client.bottom;
6424 SetRect(&delta_width, min_x, 0, max_x, min_y);
6425 SetRect(&delta_height, 0, min_y, max_x, max_y);
6427 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6428 btnPtr = infoPtr->buttons;
6429 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6430 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6431 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6432 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6434 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6435 TOOLBAR_AutoSize(infoPtr);
6436 return 0;
6440 static LRESULT
6441 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6443 if (nType == GWL_STYLE)
6444 return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6446 return 0;
6450 static LRESULT
6451 TOOLBAR_SysColorChange (void)
6453 COMCTL32_RefreshSysColors();
6455 return 0;
6459 /* update theme after a WM_THEMECHANGED message */
6460 static LRESULT theme_changed (HWND hwnd)
6462 HTHEME theme = GetWindowTheme (hwnd);
6463 CloseThemeData (theme);
6464 OpenThemeData (hwnd, themeClass);
6465 return 0;
6469 static LRESULT WINAPI
6470 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6472 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6474 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6475 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6477 if (!infoPtr && (uMsg != WM_NCCREATE))
6478 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6480 switch (uMsg)
6482 case TB_ADDBITMAP:
6483 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6485 case TB_ADDBUTTONSA:
6486 case TB_ADDBUTTONSW:
6487 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6488 uMsg == TB_ADDBUTTONSW);
6489 case TB_ADDSTRINGA:
6490 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6492 case TB_ADDSTRINGW:
6493 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6495 case TB_AUTOSIZE:
6496 return TOOLBAR_AutoSize (infoPtr);
6498 case TB_BUTTONCOUNT:
6499 return TOOLBAR_ButtonCount (infoPtr);
6501 case TB_BUTTONSTRUCTSIZE:
6502 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6504 case TB_CHANGEBITMAP:
6505 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6507 case TB_CHECKBUTTON:
6508 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6510 case TB_COMMANDTOINDEX:
6511 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6513 case TB_CUSTOMIZE:
6514 return TOOLBAR_Customize (infoPtr);
6516 case TB_DELETEBUTTON:
6517 return TOOLBAR_DeleteButton (infoPtr, wParam);
6519 case TB_ENABLEBUTTON:
6520 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6522 case TB_GETANCHORHIGHLIGHT:
6523 return TOOLBAR_GetAnchorHighlight (infoPtr);
6525 case TB_GETBITMAP:
6526 return TOOLBAR_GetBitmap (infoPtr, wParam);
6528 case TB_GETBITMAPFLAGS:
6529 return TOOLBAR_GetBitmapFlags ();
6531 case TB_GETBUTTON:
6532 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6534 case TB_GETBUTTONINFOA:
6535 case TB_GETBUTTONINFOW:
6536 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6537 uMsg == TB_GETBUTTONINFOW);
6538 case TB_GETBUTTONSIZE:
6539 return TOOLBAR_GetButtonSize (infoPtr);
6541 case TB_GETBUTTONTEXTA:
6542 case TB_GETBUTTONTEXTW:
6543 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6544 uMsg == TB_GETBUTTONTEXTW);
6546 case TB_GETDISABLEDIMAGELIST:
6547 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6549 case TB_GETEXTENDEDSTYLE:
6550 return TOOLBAR_GetExtendedStyle (infoPtr);
6552 case TB_GETHOTIMAGELIST:
6553 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6555 case TB_GETHOTITEM:
6556 return TOOLBAR_GetHotItem (infoPtr);
6558 case TB_GETIMAGELIST:
6559 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6561 case TB_GETINSERTMARK:
6562 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6564 case TB_GETINSERTMARKCOLOR:
6565 return TOOLBAR_GetInsertMarkColor (infoPtr);
6567 case TB_GETITEMRECT:
6568 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6570 case TB_GETMAXSIZE:
6571 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6573 /* case TB_GETOBJECT: */ /* 4.71 */
6575 case TB_GETPADDING:
6576 return TOOLBAR_GetPadding (infoPtr);
6578 case TB_GETRECT:
6579 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6581 case TB_GETROWS:
6582 return TOOLBAR_GetRows (infoPtr);
6584 case TB_GETSTATE:
6585 return TOOLBAR_GetState (infoPtr, wParam);
6587 case TB_GETSTRINGA:
6588 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6590 case TB_GETSTRINGW:
6591 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6593 case TB_GETSTYLE:
6594 return TOOLBAR_GetStyle (infoPtr);
6596 case TB_GETTEXTROWS:
6597 return TOOLBAR_GetTextRows (infoPtr);
6599 case TB_GETTOOLTIPS:
6600 return TOOLBAR_GetToolTips (infoPtr);
6602 case TB_GETUNICODEFORMAT:
6603 return TOOLBAR_GetUnicodeFormat (infoPtr);
6605 case TB_HIDEBUTTON:
6606 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6608 case TB_HITTEST:
6609 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6611 case TB_INDETERMINATE:
6612 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6614 case TB_INSERTBUTTONA:
6615 case TB_INSERTBUTTONW:
6616 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6617 uMsg == TB_INSERTBUTTONW);
6619 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6621 case TB_ISBUTTONCHECKED:
6622 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6624 case TB_ISBUTTONENABLED:
6625 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6627 case TB_ISBUTTONHIDDEN:
6628 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6630 case TB_ISBUTTONHIGHLIGHTED:
6631 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6633 case TB_ISBUTTONINDETERMINATE:
6634 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6636 case TB_ISBUTTONPRESSED:
6637 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6639 case TB_LOADIMAGES:
6640 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6642 case TB_MAPACCELERATORA:
6643 case TB_MAPACCELERATORW:
6644 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6646 case TB_MARKBUTTON:
6647 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6649 case TB_MOVEBUTTON:
6650 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6652 case TB_PRESSBUTTON:
6653 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6655 case TB_REPLACEBITMAP:
6656 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6658 case TB_SAVERESTOREA:
6659 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6661 case TB_SAVERESTOREW:
6662 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6664 case TB_SETANCHORHIGHLIGHT:
6665 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6667 case TB_SETBITMAPSIZE:
6668 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6670 case TB_SETBUTTONINFOA:
6671 case TB_SETBUTTONINFOW:
6672 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6673 uMsg == TB_SETBUTTONINFOW);
6674 case TB_SETBUTTONSIZE:
6675 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6677 case TB_SETBUTTONWIDTH:
6678 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6680 case TB_SETCMDID:
6681 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6683 case TB_SETDISABLEDIMAGELIST:
6684 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6686 case TB_SETDRAWTEXTFLAGS:
6687 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6689 case TB_SETEXTENDEDSTYLE:
6690 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6692 case TB_SETHOTIMAGELIST:
6693 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6695 case TB_SETHOTITEM:
6696 return TOOLBAR_SetHotItem (infoPtr, wParam);
6698 case TB_SETIMAGELIST:
6699 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6701 case TB_SETINDENT:
6702 return TOOLBAR_SetIndent (infoPtr, wParam);
6704 case TB_SETINSERTMARK:
6705 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6707 case TB_SETINSERTMARKCOLOR:
6708 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6710 case TB_SETMAXTEXTROWS:
6711 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6713 case TB_SETPADDING:
6714 return TOOLBAR_SetPadding (infoPtr, lParam);
6716 case TB_SETPARENT:
6717 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6719 case TB_SETROWS:
6720 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6722 case TB_SETSTATE:
6723 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6725 case TB_SETSTYLE:
6726 return TOOLBAR_SetStyle (infoPtr, lParam);
6728 case TB_SETTOOLTIPS:
6729 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6731 case TB_SETUNICODEFORMAT:
6732 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6734 case TB_SETBOUNDINGSIZE:
6735 return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6737 case TB_SETHOTITEM2:
6738 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6740 case TB_SETLISTGAP:
6741 return TOOLBAR_SetListGap(infoPtr, wParam);
6743 case TB_GETIMAGELISTCOUNT:
6744 return TOOLBAR_GetImageListCount(infoPtr);
6746 case TB_GETIDEALSIZE:
6747 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6749 case TB_UNKWN464:
6750 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6752 /* Common Control Messages */
6754 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6755 case CCM_GETCOLORSCHEME:
6756 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6758 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6759 case CCM_SETCOLORSCHEME:
6760 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6762 case CCM_GETVERSION:
6763 return TOOLBAR_GetVersion (infoPtr);
6765 case CCM_SETVERSION:
6766 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6769 /* case WM_CHAR: */
6771 case WM_CREATE:
6772 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6774 case WM_DESTROY:
6775 return TOOLBAR_Destroy (infoPtr);
6777 case WM_ERASEBKGND:
6778 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6780 case WM_GETFONT:
6781 return TOOLBAR_GetFont (infoPtr);
6783 case WM_KEYDOWN:
6784 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6786 /* case WM_KILLFOCUS: */
6788 case WM_LBUTTONDBLCLK:
6789 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6791 case WM_LBUTTONDOWN:
6792 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6794 case WM_LBUTTONUP:
6795 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6797 case WM_RBUTTONUP:
6798 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6800 case WM_RBUTTONDBLCLK:
6801 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6803 case WM_MOUSEMOVE:
6804 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6806 case WM_MOUSELEAVE:
6807 return TOOLBAR_MouseLeave (infoPtr);
6809 case WM_CAPTURECHANGED:
6810 return TOOLBAR_CaptureChanged(infoPtr);
6812 case WM_NCACTIVATE:
6813 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6815 case WM_NCCALCSIZE:
6816 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6818 case WM_NCCREATE:
6819 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6821 case WM_NCPAINT:
6822 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6824 case WM_NOTIFY:
6825 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6827 case WM_NOTIFYFORMAT:
6828 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6830 case WM_PRINTCLIENT:
6831 case WM_PAINT:
6832 return TOOLBAR_Paint (infoPtr, wParam);
6834 case WM_SETFOCUS:
6835 return TOOLBAR_SetFocus (infoPtr);
6837 case WM_SETFONT:
6838 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6840 case WM_SETREDRAW:
6841 return TOOLBAR_SetRedraw (infoPtr, wParam);
6843 case WM_SIZE:
6844 return TOOLBAR_Size (infoPtr);
6846 case WM_STYLECHANGED:
6847 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6849 case WM_SYSCOLORCHANGE:
6850 return TOOLBAR_SysColorChange ();
6852 case WM_THEMECHANGED:
6853 return theme_changed (hwnd);
6855 /* case WM_WININICHANGE: */
6857 case WM_CHARTOITEM:
6858 case WM_COMMAND:
6859 case WM_DRAWITEM:
6860 case WM_MEASUREITEM:
6861 case WM_VKEYTOITEM:
6862 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6864 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6865 case PGM_FORWARDMOUSE:
6866 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6868 default:
6869 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6870 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6871 uMsg, wParam, lParam);
6872 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6877 VOID
6878 TOOLBAR_Register (void)
6880 WNDCLASSW wndClass;
6882 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6883 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6884 wndClass.lpfnWndProc = ToolbarWindowProc;
6885 wndClass.cbClsExtra = 0;
6886 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6887 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6888 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6889 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6891 RegisterClassW (&wndClass);
6895 VOID
6896 TOOLBAR_Unregister (void)
6898 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6901 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6903 HIMAGELIST himlold;
6904 PIMLENTRY c = NULL;
6906 /* Check if the entry already exists */
6907 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6909 /* If this is a new entry we must create it and insert into the array */
6910 if (!c)
6912 PIMLENTRY *pnies;
6914 c = Alloc(sizeof(IMLENTRY));
6915 c->id = id;
6917 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6918 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
6919 pnies[*cies] = c;
6920 (*cies)++;
6922 Free(*pies);
6923 *pies = pnies;
6926 himlold = c->himl;
6927 c->himl = himl;
6929 return himlold;
6933 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
6935 int i;
6937 for (i = 0; i < *cies; i++)
6938 Free((*pies)[i]);
6940 Free(*pies);
6942 *cies = 0;
6943 *pies = NULL;
6947 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6949 PIMLENTRY c = NULL;
6951 if (pies != NULL)
6953 int i;
6955 for (i = 0; i < cies; i++)
6957 if (pies[i]->id == id)
6959 c = pies[i];
6960 break;
6965 return c;
6969 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6971 HIMAGELIST himlDef = 0;
6972 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6974 if (pie)
6975 himlDef = pie->himl;
6977 return himlDef;
6981 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6983 if (infoPtr->bUnicode)
6984 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6985 else
6987 CHAR Buffer[256];
6988 NMTOOLBARA nmtba;
6989 BOOL bRet = FALSE;
6991 nmtba.iItem = nmtb->iItem;
6992 nmtba.pszText = Buffer;
6993 nmtba.cchText = 256;
6994 ZeroMemory(nmtba.pszText, nmtba.cchText);
6996 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
6998 int ccht = strlen(nmtba.pszText);
6999 if (ccht)
7000 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7001 nmtb->pszText, nmtb->cchText);
7003 nmtb->tbButton = nmtba.tbButton;
7004 bRet = TRUE;
7007 return bRet;
7012 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7014 NMTOOLBARW nmtb;
7016 /* MSDN states that iItem is the index of the button, rather than the
7017 * command ID as used by every other NMTOOLBAR notification */
7018 nmtb.iItem = iItem;
7019 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7021 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);