dwrite/tests: Remove unused functions (Clang).
[wine.git] / dlls / comctl32 / toolbar.c
blob9307fd46d0889f711ebbe55a8518ae658c3d659a
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 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1236 /* Use DrawText to get true size as drawn (less pesky "&") */
1237 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1238 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1239 DT_NOPREFIX : 0));
1241 /* feed back to caller */
1242 lpSize->cx = myrect.right;
1243 lpSize->cy = myrect.bottom;
1247 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1250 /***********************************************************************
1251 * TOOLBAR_CalcStrings
1253 * This function walks through each string and measures it and returns
1254 * the largest height and width to caller.
1256 static void
1257 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1259 TBUTTON_INFO *btnPtr;
1260 INT i;
1261 SIZE sz;
1262 HDC hdc;
1263 HFONT hOldFont;
1265 lpSize->cx = 0;
1266 lpSize->cy = 0;
1268 if (infoPtr->nMaxTextRows == 0)
1269 return;
1271 hdc = GetDC (infoPtr->hwndSelf);
1272 hOldFont = SelectObject (hdc, infoPtr->hFont);
1274 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1276 TEXTMETRICW tm;
1278 GetTextMetricsW(hdc, &tm);
1279 lpSize->cy = tm.tmHeight;
1282 btnPtr = infoPtr->buttons;
1283 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1284 if(TOOLBAR_GetText(infoPtr, btnPtr))
1286 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1287 if (sz.cx > lpSize->cx)
1288 lpSize->cx = sz.cx;
1289 if (sz.cy > lpSize->cy)
1290 lpSize->cy = sz.cy;
1294 SelectObject (hdc, hOldFont);
1295 ReleaseDC (infoPtr->hwndSelf, hdc);
1297 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1300 /***********************************************************************
1301 * TOOLBAR_WrapToolbar
1303 * This function walks through the buttons and separators in the
1304 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1305 * wrapping should occur based on the width of the toolbar window.
1306 * It does *not* calculate button placement itself. That task
1307 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1308 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1309 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1311 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1312 * vertical toolbar lists.
1315 static void
1316 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1318 TBUTTON_INFO *btnPtr;
1319 INT x, cx, i, j, width;
1320 BOOL bButtonWrap;
1322 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1323 /* no layout is necessary. Applications may use this style */
1324 /* to perform their own layout on the toolbar. */
1325 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1326 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1328 btnPtr = infoPtr->buttons;
1329 x = infoPtr->nIndent;
1330 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1332 bButtonWrap = FALSE;
1334 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1335 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1336 infoPtr->nIndent);
1338 for (i = 0; i < infoPtr->nNumButtons; i++ )
1340 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1342 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1343 continue;
1345 if (btnPtr[i].cx > 0)
1346 cx = btnPtr[i].cx;
1347 /* horizontal separators are treated as buttons for width */
1348 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1349 !(infoPtr->dwStyle & CCS_VERT))
1350 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1351 else
1352 cx = infoPtr->nButtonWidth;
1354 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1355 cx += DDARROW_WIDTH;
1357 /* Two or more adjacent separators form a separator group. */
1358 /* The first separator in a group should be wrapped to the */
1359 /* next row if the previous wrapping is on a button. */
1360 if( bButtonWrap &&
1361 (btnPtr[i].fsStyle & BTNS_SEP) &&
1362 (i + 1 < infoPtr->nNumButtons ) &&
1363 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1365 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1366 btnPtr[i].fsState |= TBSTATE_WRAP;
1367 x = infoPtr->nIndent;
1368 i++;
1369 bButtonWrap = FALSE;
1370 continue;
1373 /* The layout makes sure the bitmap is visible, but not the button. */
1374 /* Test added to also wrap after a button that starts a row but */
1375 /* is bigger than the area. - GA 8/01 */
1376 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1377 ((x == infoPtr->nIndent) && (cx > width)))
1379 BOOL bFound = FALSE;
1381 /* If the current button is a separator and not hidden, */
1382 /* go to the next until it reaches a non separator. */
1383 /* Wrap the last separator if it is before a button. */
1384 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1385 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1386 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1387 i < infoPtr->nNumButtons )
1389 i++;
1390 bFound = TRUE;
1393 if( bFound && i < infoPtr->nNumButtons )
1395 i--;
1396 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1397 i, btnPtr[i].fsStyle, x, cx);
1398 btnPtr[i].fsState |= TBSTATE_WRAP;
1399 x = infoPtr->nIndent;
1400 bButtonWrap = FALSE;
1401 continue;
1403 else if ( i >= infoPtr->nNumButtons)
1404 break;
1406 /* If the current button is not a separator, find the last */
1407 /* separator and wrap it. */
1408 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1410 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1411 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1413 bFound = TRUE;
1414 i = j;
1415 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1416 i, btnPtr[i].fsStyle, x, cx);
1417 x = infoPtr->nIndent;
1418 btnPtr[j].fsState |= TBSTATE_WRAP;
1419 bButtonWrap = FALSE;
1420 break;
1424 /* If no separator available for wrapping, wrap one of */
1425 /* non-hidden previous button. */
1426 if (!bFound)
1428 for ( j = i - 1;
1429 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1431 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1432 continue;
1434 bFound = TRUE;
1435 i = j;
1436 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1437 i, btnPtr[i].fsStyle, x, cx);
1438 x = infoPtr->nIndent;
1439 btnPtr[j].fsState |= TBSTATE_WRAP;
1440 bButtonWrap = TRUE;
1441 break;
1445 /* If all above failed, wrap the current button. */
1446 if (!bFound)
1448 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1449 i, btnPtr[i].fsStyle, x, cx);
1450 btnPtr[i].fsState |= TBSTATE_WRAP;
1451 x = infoPtr->nIndent;
1452 if (btnPtr[i].fsStyle & BTNS_SEP )
1453 bButtonWrap = FALSE;
1454 else
1455 bButtonWrap = TRUE;
1458 else {
1459 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1460 i, btnPtr[i].fsStyle, x, cx);
1461 x += cx;
1467 /***********************************************************************
1468 * TOOLBAR_MeasureButton
1470 * Calculates the width and height required for a button. Used in
1471 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1472 * the width of buttons that are autosized.
1474 * Note that it would have been rather elegant to use one piece of code for
1475 * both the laying out of the toolbar and for controlling where button parts
1476 * are drawn, but the native control has inconsistencies between the two that
1477 * prevent this from being effectively. These inconsistencies can be seen as
1478 * artefacts where parts of the button appear outside of the bounding button
1479 * rectangle.
1481 * There are several cases for the calculation of the button dimensions and
1482 * button part positioning:
1484 * List
1485 * ====
1487 * With Bitmap:
1489 * +--------------------------------------------------------+ ^
1490 * | ^ ^ | |
1491 * | | pad.cy / 2 | centered | |
1492 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1493 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1494 * | |<------------>| | | | |
1495 * | +--------------+ +------------+ | |
1496 * |<-------------------------------------->| | |
1497 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1498 * | szText.cx | |
1499 * +--------------------------------------------------------+ -
1500 * <-------------------------------------------------------->
1501 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1503 * Without Bitmap (I_IMAGENONE):
1505 * +-----------------------------------+ ^
1506 * | ^ | |
1507 * | | centered | | LISTPAD_CY +
1508 * | +------------+ | | szText.cy
1509 * | | Text | | |
1510 * | | | | |
1511 * | +------------+ | |
1512 * |<----------------->| | |
1513 * | cxedge |<-----------> | |
1514 * | szText.cx | |
1515 * +-----------------------------------+ -
1516 * <----------------------------------->
1517 * szText.cx + pad.cx
1519 * Without text:
1521 * +--------------------------------------+ ^
1522 * | ^ | |
1523 * | | padding.cy/2 | | DEFPAD_CY +
1524 * | +------------+ | | nBitmapHeight
1525 * | | Bitmap | | |
1526 * | | | | |
1527 * | +------------+ | |
1528 * |<------------------->| | |
1529 * | cxedge + iListGap/2 |<-----------> | |
1530 * | nBitmapWidth | |
1531 * +--------------------------------------+ -
1532 * <-------------------------------------->
1533 * 2*cxedge + nBitmapWidth + iListGap
1535 * Non-List
1536 * ========
1538 * With bitmap:
1540 * +-----------------------------------+ ^
1541 * | ^ | |
1542 * | | pad.cy / 2 | | nBitmapHeight +
1543 * | - | | szText.cy +
1544 * | +------------+ | | DEFPAD_CY + 1
1545 * | centered | Bitmap | | |
1546 * |<----------------->| | | |
1547 * | +------------+ | |
1548 * | ^ | |
1549 * | 1 | | |
1550 * | - | |
1551 * | centered +---------------+ | |
1552 * |<--------------->| Text | | |
1553 * | +---------------+ | |
1554 * +-----------------------------------+ -
1555 * <----------------------------------->
1556 * pad.cx + max(nBitmapWidth, szText.cx)
1558 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1560 * +---------------------------------------+ ^
1561 * | ^ | |
1562 * | | 2 + pad.cy / 2 | |
1563 * | - | | szText.cy +
1564 * | centered +-----------------+ | | pad.cy + 2
1565 * |<--------------->| Text | | |
1566 * | +-----------------+ | |
1567 * | | |
1568 * +---------------------------------------+ -
1569 * <--------------------------------------->
1570 * 2*cxedge + pad.cx + szText.cx
1572 * Without text:
1573 * As for with bitmaps, but with szText.cx zero.
1575 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1576 BOOL bHasBitmap, BOOL bValidImageList)
1578 SIZE sizeButton;
1579 if (infoPtr->dwStyle & TBSTYLE_LIST)
1581 /* set button height from bitmap / text height... */
1582 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1583 sizeString.cy);
1585 /* ... add on the necessary padding */
1586 if (bValidImageList)
1588 if (bHasBitmap)
1589 sizeButton.cy += DEFPAD_CY;
1590 else
1591 sizeButton.cy += LISTPAD_CY;
1593 else
1594 sizeButton.cy += infoPtr->szPadding.cy;
1596 /* calculate button width */
1597 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1598 infoPtr->nBitmapWidth + infoPtr->iListGap;
1599 if (sizeString.cx > 0)
1600 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1603 else
1605 if (bHasBitmap)
1607 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1608 if (sizeString.cy > 0)
1609 sizeButton.cy += 1 + sizeString.cy;
1610 sizeButton.cx = infoPtr->szPadding.cx +
1611 max(sizeString.cx, infoPtr->nBitmapWidth);
1613 else
1615 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1616 NONLIST_NOTEXT_OFFSET;
1617 sizeButton.cx = infoPtr->szPadding.cx +
1618 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1621 return sizeButton;
1625 /***********************************************************************
1626 * TOOLBAR_CalcToolbar
1628 * This function calculates button and separator placement. It first
1629 * calculates the button sizes, gets the toolbar window width and then
1630 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1631 * on. It assigns a new location to each item and sends this location to
1632 * the tooltip window if appropriate. Finally, it updates the rcBound
1633 * rect and calculates the new required toolbar window height.
1635 static void
1636 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1638 SIZE sizeString, sizeButton;
1639 BOOL validImageList = FALSE;
1641 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1643 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1645 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1646 validImageList = TRUE;
1647 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1648 infoPtr->nButtonWidth = sizeButton.cx;
1649 infoPtr->nButtonHeight = sizeButton.cy;
1650 infoPtr->iTopMargin = default_top_margin(infoPtr);
1652 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1653 infoPtr->nButtonWidth = infoPtr->cxMin;
1654 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1655 infoPtr->nButtonWidth = infoPtr->cxMax;
1657 TOOLBAR_LayoutToolbar(infoPtr);
1660 static void
1661 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1663 TBUTTON_INFO *btnPtr;
1664 SIZE sizeButton;
1665 INT i, nRows, nSepRows;
1666 INT x, y, cx, cy;
1667 BOOL bWrap;
1668 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1670 TOOLBAR_WrapToolbar(infoPtr);
1672 x = infoPtr->nIndent;
1673 y = infoPtr->iTopMargin;
1674 cx = infoPtr->nButtonWidth;
1675 cy = infoPtr->nButtonHeight;
1677 nRows = nSepRows = 0;
1679 infoPtr->rcBound.top = y;
1680 infoPtr->rcBound.left = x;
1681 infoPtr->rcBound.bottom = y + cy;
1682 infoPtr->rcBound.right = x;
1684 btnPtr = infoPtr->buttons;
1686 TRACE("cy=%d\n", cy);
1688 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1690 bWrap = FALSE;
1691 if (btnPtr->fsState & TBSTATE_HIDDEN)
1693 SetRectEmpty (&btnPtr->rect);
1694 continue;
1697 cy = infoPtr->nButtonHeight;
1699 if (btnPtr->fsStyle & BTNS_SEP) {
1700 if (infoPtr->dwStyle & CCS_VERT) {
1701 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1702 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1704 else
1705 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1706 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1708 else
1710 if (btnPtr->cx)
1711 cx = btnPtr->cx;
1712 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1714 SIZE sz;
1715 HDC hdc;
1716 HFONT hOldFont;
1718 hdc = GetDC (infoPtr->hwndSelf);
1719 hOldFont = SelectObject (hdc, infoPtr->hFont);
1721 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1723 SelectObject (hdc, hOldFont);
1724 ReleaseDC (infoPtr->hwndSelf, hdc);
1726 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1727 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1728 validImageList);
1729 cx = sizeButton.cx;
1731 else
1732 cx = infoPtr->nButtonWidth;
1734 /* if size has been set manually then don't add on extra space
1735 * for the drop down arrow */
1736 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1737 cx += DDARROW_WIDTH;
1739 if (btnPtr->fsState & TBSTATE_WRAP)
1740 bWrap = TRUE;
1742 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1744 if (infoPtr->rcBound.left > x)
1745 infoPtr->rcBound.left = x;
1746 if (infoPtr->rcBound.right < x + cx)
1747 infoPtr->rcBound.right = x + cx;
1748 if (infoPtr->rcBound.bottom < y + cy)
1749 infoPtr->rcBound.bottom = y + cy;
1751 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1753 /* btnPtr->nRow is zero based. The space between the rows is */
1754 /* also considered as a row. */
1755 btnPtr->nRow = nRows + nSepRows;
1757 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1758 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1759 x, y, x+cx, y+cy);
1761 if( bWrap )
1763 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1764 y += cy;
1765 else
1767 if ( !(infoPtr->dwStyle & CCS_VERT))
1768 y += cy + ( (btnPtr->cx > 0 ) ?
1769 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1770 else
1771 y += cy;
1773 /* nSepRows is used to calculate the extra height following */
1774 /* the last row. */
1775 nSepRows++;
1777 x = infoPtr->nIndent;
1779 /* Increment row number unless this is the last button */
1780 /* and it has Wrap set. */
1781 if (i != infoPtr->nNumButtons-1)
1782 nRows++;
1784 else
1785 x += cx;
1788 /* infoPtr->nRows is the number of rows on the toolbar */
1789 infoPtr->nRows = nRows + nSepRows + 1;
1791 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1795 static INT
1796 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1798 TBUTTON_INFO *btnPtr;
1799 INT i;
1801 if (button)
1802 *button = FALSE;
1804 btnPtr = infoPtr->buttons;
1805 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1806 if (btnPtr->fsState & TBSTATE_HIDDEN)
1807 continue;
1809 if (btnPtr->fsStyle & BTNS_SEP) {
1810 if (PtInRect (&btnPtr->rect, *lpPt)) {
1811 TRACE(" ON SEPARATOR %d!\n", i);
1812 return -i;
1815 else {
1816 if (PtInRect (&btnPtr->rect, *lpPt)) {
1817 TRACE(" ON BUTTON %d!\n", i);
1818 if (button)
1819 *button = TRUE;
1820 return i;
1825 TRACE(" NOWHERE!\n");
1826 return TOOLBAR_NOWHERE;
1830 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1831 static BOOL
1832 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1834 INT nOldButtons, nNewButtons, iButton;
1835 BOOL fHasString = FALSE;
1837 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1838 iIndex = infoPtr->nNumButtons;
1840 nOldButtons = infoPtr->nNumButtons;
1841 nNewButtons = nOldButtons + nAddButtons;
1843 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1844 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1845 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1846 infoPtr->nNumButtons += nAddButtons;
1848 /* insert new buttons data */
1849 for (iButton = 0; iButton < nAddButtons; iButton++) {
1850 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1851 INT_PTR str;
1853 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1855 ZeroMemory(btnPtr, sizeof(*btnPtr));
1857 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1858 btnPtr->idCommand = lpTbb[iButton].idCommand;
1859 btnPtr->fsState = lpTbb[iButton].fsState;
1860 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1861 btnPtr->dwData = lpTbb[iButton].dwData;
1863 if (btnPtr->fsStyle & BTNS_SEP)
1864 str = -1;
1865 else
1866 str = lpTbb[iButton].iString;
1867 set_string_index( btnPtr, str, fUnicode );
1868 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1870 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1873 if (infoPtr->nNumStrings > 0 || fHasString)
1874 TOOLBAR_CalcToolbar(infoPtr);
1875 else
1876 TOOLBAR_LayoutToolbar(infoPtr);
1877 TOOLBAR_AutoSize(infoPtr);
1879 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1880 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1881 return TRUE;
1885 static INT
1886 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1888 TBUTTON_INFO *btnPtr;
1889 INT i;
1891 if (CommandIsIndex) {
1892 TRACE("command is really index command=%d\n", idCommand);
1893 if (idCommand >= infoPtr->nNumButtons) return -1;
1894 return idCommand;
1896 btnPtr = infoPtr->buttons;
1897 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1898 if (btnPtr->idCommand == idCommand) {
1899 TRACE("command=%d index=%d\n", idCommand, i);
1900 return i;
1903 TRACE("no index found for command=%d\n", idCommand);
1904 return -1;
1908 static INT
1909 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1911 TBUTTON_INFO *btnPtr;
1912 INT nRunIndex;
1914 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1915 return -1;
1917 /* check index button */
1918 btnPtr = &infoPtr->buttons[nIndex];
1919 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1920 if (btnPtr->fsState & TBSTATE_CHECKED)
1921 return nIndex;
1924 /* check previous buttons */
1925 nRunIndex = nIndex - 1;
1926 while (nRunIndex >= 0) {
1927 btnPtr = &infoPtr->buttons[nRunIndex];
1928 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1929 if (btnPtr->fsState & TBSTATE_CHECKED)
1930 return nRunIndex;
1932 else
1933 break;
1934 nRunIndex--;
1937 /* check next buttons */
1938 nRunIndex = nIndex + 1;
1939 while (nRunIndex < infoPtr->nNumButtons) {
1940 btnPtr = &infoPtr->buttons[nRunIndex];
1941 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1942 if (btnPtr->fsState & TBSTATE_CHECKED)
1943 return nRunIndex;
1945 else
1946 break;
1947 nRunIndex++;
1950 return -1;
1954 static VOID
1955 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1956 WPARAM wParam, LPARAM lParam)
1958 MSG msg;
1960 msg.hwnd = hwndMsg;
1961 msg.message = uMsg;
1962 msg.wParam = wParam;
1963 msg.lParam = lParam;
1964 msg.time = GetMessageTime ();
1965 msg.pt.x = (short)LOWORD(GetMessagePos ());
1966 msg.pt.y = (short)HIWORD(GetMessagePos ());
1968 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1971 static void
1972 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1974 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1975 TTTOOLINFOW ti;
1977 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1978 ti.cbSize = sizeof (TTTOOLINFOW);
1979 ti.hwnd = infoPtr->hwndSelf;
1980 ti.uId = button->idCommand;
1981 ti.hinst = 0;
1982 ti.lpszText = LPSTR_TEXTCALLBACKW;
1983 /* ti.lParam = random value from the stack? */
1985 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1986 0, (LPARAM)&ti);
1990 static void
1991 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1993 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1994 TTTOOLINFOW ti;
1996 ZeroMemory(&ti, sizeof(ti));
1997 ti.cbSize = sizeof(ti);
1998 ti.hwnd = infoPtr->hwndSelf;
1999 ti.uId = button->idCommand;
2001 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2005 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2007 /* Set the toolTip only for non-hidden, non-separator button */
2008 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2010 TTTOOLINFOW ti;
2012 ZeroMemory(&ti, sizeof(ti));
2013 ti.cbSize = sizeof(ti);
2014 ti.hwnd = infoPtr->hwndSelf;
2015 ti.uId = button->idCommand;
2016 ti.rect = button->rect;
2017 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2021 /* Creates the tooltip control */
2022 static void
2023 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2025 int i;
2026 NMTOOLTIPSCREATED nmttc;
2028 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2029 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2030 infoPtr->hwndSelf, 0, 0, 0);
2032 if (!infoPtr->hwndToolTip)
2033 return;
2035 /* Send NM_TOOLTIPSCREATED notification */
2036 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2037 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2039 for (i = 0; i < infoPtr->nNumButtons; i++)
2041 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2042 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2046 /* keeps available button list box sorted by button id */
2047 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2049 int i;
2050 int count;
2051 PCUSTOMBUTTON btnInfo;
2052 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2054 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2056 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2058 /* position 0 is always separator */
2059 for (i = 1; i < count; i++)
2061 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2062 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2064 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2065 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2066 return;
2069 /* id higher than all others add to end */
2070 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2071 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2074 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2076 NMTOOLBARW nmtb;
2078 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2080 if (nIndexFrom == nIndexTo)
2081 return;
2083 /* MSDN states that iItem is the index of the button, rather than the
2084 * command ID as used by every other NMTOOLBAR notification */
2085 nmtb.iItem = nIndexFrom;
2086 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2088 PCUSTOMBUTTON btnInfo;
2089 NMHDR hdr;
2090 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2091 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2093 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2095 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2096 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2097 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2098 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2100 if (nIndexTo <= 0)
2101 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2102 else
2103 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2105 /* last item is always separator, so -2 instead of -1 */
2106 if (nIndexTo >= (count - 2))
2107 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2108 else
2109 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2111 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2112 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2114 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2118 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2120 NMTOOLBARW nmtb;
2122 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2124 /* MSDN states that iItem is the index of the button, rather than the
2125 * command ID as used by every other NMTOOLBAR notification */
2126 nmtb.iItem = nIndexAvail;
2127 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2129 PCUSTOMBUTTON btnInfo;
2130 NMHDR hdr;
2131 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2132 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2133 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2135 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2137 if (nIndexAvail != 0) /* index == 0 indicates separator */
2139 /* remove from 'available buttons' list */
2140 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2141 if (nIndexAvail == count-1)
2142 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2143 else
2144 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2146 else
2148 PCUSTOMBUTTON btnNew;
2150 /* duplicate 'separator' button */
2151 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2152 *btnNew = *btnInfo;
2153 btnInfo = btnNew;
2156 /* insert into 'toolbar button' list */
2157 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2158 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2160 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2162 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2166 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2168 PCUSTOMBUTTON btnInfo;
2169 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2171 TRACE("Remove: index %d\n", index);
2173 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2175 /* send TBN_QUERYDELETE notification */
2176 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2178 NMHDR hdr;
2180 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2181 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2183 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2185 /* insert into 'available button' list */
2186 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2187 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2188 else
2189 Free(btnInfo);
2191 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2195 /* drag list notification function for toolbar buttons list box */
2196 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2197 const DRAGLISTINFO *pDLI)
2199 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2200 switch (pDLI->uNotification)
2202 case DL_BEGINDRAG:
2204 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2205 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2206 /* no dragging for last item (separator) */
2207 if (nCurrentItem >= (nCount - 1)) return FALSE;
2208 return TRUE;
2210 case DL_DRAGGING:
2212 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2213 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2214 /* no dragging past last item (separator) */
2215 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2217 DrawInsert(hwnd, hwndList, nCurrentItem);
2218 /* FIXME: native uses "move button" cursor */
2219 return DL_COPYCURSOR;
2222 /* not over toolbar buttons list */
2223 if (nCurrentItem < 0)
2225 POINT ptWindow = pDLI->ptCursor;
2226 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2227 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2228 /* over available buttons list? */
2229 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2230 /* FIXME: native uses "move button" cursor */
2231 return DL_COPYCURSOR;
2233 /* clear drag arrow */
2234 DrawInsert(hwnd, hwndList, -1);
2235 return DL_STOPCURSOR;
2237 case DL_DROPPED:
2239 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2240 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2241 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2242 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2244 /* clear drag arrow */
2245 DrawInsert(hwnd, hwndList, -1);
2246 /* move item */
2247 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2249 /* not over toolbar buttons list */
2250 if (nIndexTo < 0)
2252 POINT ptWindow = pDLI->ptCursor;
2253 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2254 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2255 /* over available buttons list? */
2256 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2257 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2259 break;
2261 case DL_CANCELDRAG:
2262 /* Clear drag arrow */
2263 DrawInsert(hwnd, hwndList, -1);
2264 break;
2267 return 0;
2270 /* drag list notification function for available buttons list box */
2271 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2272 const DRAGLISTINFO *pDLI)
2274 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2275 switch (pDLI->uNotification)
2277 case DL_BEGINDRAG:
2278 return TRUE;
2279 case DL_DRAGGING:
2281 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2282 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2283 /* no dragging past last item (separator) */
2284 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2286 DrawInsert(hwnd, hwndList, nCurrentItem);
2287 /* FIXME: native uses "move button" cursor */
2288 return DL_COPYCURSOR;
2291 /* not over toolbar buttons list */
2292 if (nCurrentItem < 0)
2294 POINT ptWindow = pDLI->ptCursor;
2295 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2296 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2297 /* over available buttons list? */
2298 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2299 /* FIXME: native uses "move button" cursor */
2300 return DL_COPYCURSOR;
2302 /* clear drag arrow */
2303 DrawInsert(hwnd, hwndList, -1);
2304 return DL_STOPCURSOR;
2306 case DL_DROPPED:
2308 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2309 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2310 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2311 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2313 /* clear drag arrow */
2314 DrawInsert(hwnd, hwndList, -1);
2315 /* add item */
2316 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2319 case DL_CANCELDRAG:
2320 /* Clear drag arrow */
2321 DrawInsert(hwnd, hwndList, -1);
2322 break;
2324 return 0;
2327 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2329 /***********************************************************************
2330 * TOOLBAR_CustomizeDialogProc
2331 * This function implements the toolbar customization dialog.
2333 static INT_PTR CALLBACK
2334 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2336 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2337 PCUSTOMBUTTON btnInfo;
2338 NMTOOLBARA nmtb;
2339 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2341 switch (uMsg)
2343 case WM_INITDIALOG:
2344 custInfo = (PCUSTDLG_INFO)lParam;
2345 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2347 if (custInfo)
2349 WCHAR Buffer[256];
2350 int i = 0;
2351 int index;
2352 NMTBINITCUSTOMIZE nmtbic;
2354 infoPtr = custInfo->tbInfo;
2356 /* send TBN_QUERYINSERT notification */
2357 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2359 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2360 return FALSE;
2362 nmtbic.hwndDialog = hwnd;
2363 /* Send TBN_INITCUSTOMIZE notification */
2364 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2365 TBNRF_HIDEHELP)
2367 TRACE("TBNRF_HIDEHELP requested\n");
2368 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2371 /* add items to 'toolbar buttons' list and check if removable */
2372 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2374 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2375 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2376 btnInfo->btn.fsStyle = BTNS_SEP;
2377 btnInfo->bVirtual = FALSE;
2378 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2380 /* send TBN_QUERYDELETE notification */
2381 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2383 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2384 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2387 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2389 /* insert separator button into 'available buttons' list */
2390 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2391 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2392 btnInfo->btn.fsStyle = BTNS_SEP;
2393 btnInfo->bVirtual = FALSE;
2394 btnInfo->bRemovable = TRUE;
2395 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2396 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2397 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2399 /* insert all buttons into dsa */
2400 for (i = 0;; i++)
2402 /* send TBN_GETBUTTONINFO notification */
2403 NMTOOLBARW nmtb;
2404 nmtb.iItem = i;
2405 nmtb.pszText = Buffer;
2406 nmtb.cchText = 256;
2408 /* Clear previous button's text */
2409 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2411 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2412 break;
2414 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2415 nmtb.tbButton.fsStyle, i,
2416 nmtb.tbButton.idCommand,
2417 nmtb.tbButton.iString,
2418 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2419 : "");
2421 /* insert button into the appropriate list */
2422 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2423 if (index == -1)
2425 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2426 btnInfo->bVirtual = FALSE;
2427 btnInfo->bRemovable = TRUE;
2429 else
2431 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2432 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2435 btnInfo->btn = nmtb.tbButton;
2436 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2438 if (lstrlenW(nmtb.pszText))
2439 lstrcpyW(btnInfo->text, nmtb.pszText);
2440 else if (nmtb.tbButton.iString >= 0 &&
2441 nmtb.tbButton.iString < infoPtr->nNumStrings)
2443 lstrcpyW(btnInfo->text,
2444 infoPtr->strings[nmtb.tbButton.iString]);
2448 if (index == -1)
2449 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2452 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2454 /* select first item in the 'available' list */
2455 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2457 /* append 'virtual' separator button to the 'toolbar buttons' list */
2458 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2459 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2460 btnInfo->btn.fsStyle = BTNS_SEP;
2461 btnInfo->bVirtual = TRUE;
2462 btnInfo->bRemovable = FALSE;
2463 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2464 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2465 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2467 /* select last item in the 'toolbar' list */
2468 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2469 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2471 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2472 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2474 /* set focus and disable buttons */
2475 PostMessageW (hwnd, WM_USER, 0, 0);
2477 return TRUE;
2479 case WM_USER:
2480 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2481 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2482 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2483 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2484 return TRUE;
2486 case WM_CLOSE:
2487 EndDialog(hwnd, FALSE);
2488 return TRUE;
2490 case WM_COMMAND:
2491 switch (LOWORD(wParam))
2493 case IDC_TOOLBARBTN_LBOX:
2494 if (HIWORD(wParam) == LBN_SELCHANGE)
2496 PCUSTOMBUTTON btnInfo;
2497 NMTOOLBARA nmtb;
2498 int count;
2499 int index;
2501 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2502 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2504 /* send TBN_QUERYINSERT notification */
2505 nmtb.iItem = index;
2506 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2508 /* get list box item */
2509 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2511 if (index == (count - 1))
2513 /* last item (virtual separator) */
2514 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2515 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2517 else if (index == (count - 2))
2519 /* second last item (last non-virtual item) */
2520 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2521 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2523 else if (index == 0)
2525 /* first item */
2526 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2527 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2529 else
2531 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2532 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2535 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2537 break;
2539 case IDC_MOVEUP_BTN:
2541 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2542 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2544 break;
2546 case IDC_MOVEDN_BTN: /* move down */
2548 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2549 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2551 break;
2553 case IDC_REMOVE_BTN: /* remove button */
2555 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2557 if (LB_ERR == index)
2558 break;
2560 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2562 break;
2563 case IDC_HELP_BTN:
2564 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2565 break;
2566 case IDC_RESET_BTN:
2567 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2568 break;
2570 case IDOK: /* Add button */
2572 int index;
2573 int indexto;
2575 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2576 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2578 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2580 break;
2582 case IDCANCEL:
2583 EndDialog(hwnd, FALSE);
2584 break;
2586 return TRUE;
2588 case WM_DESTROY:
2590 int count;
2591 int i;
2593 /* delete items from 'toolbar buttons' listbox*/
2594 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2595 for (i = 0; i < count; i++)
2597 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2598 Free(btnInfo);
2599 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2601 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2604 /* delete items from 'available buttons' listbox*/
2605 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2606 for (i = 0; i < count; i++)
2608 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2609 Free(btnInfo);
2610 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2612 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2614 return TRUE;
2616 case WM_DRAWITEM:
2617 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2619 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2620 RECT rcButton;
2621 RECT rcText;
2622 HPEN hPen, hOldPen;
2623 HBRUSH hOldBrush;
2624 COLORREF oldText = 0;
2625 COLORREF oldBk = 0;
2627 /* get item data */
2628 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2629 if (btnInfo == NULL)
2631 FIXME("btnInfo invalid!\n");
2632 return TRUE;
2635 /* set colors and select objects */
2636 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2637 if (btnInfo->bVirtual)
2638 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2639 else
2640 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2641 hPen = CreatePen( PS_SOLID, 1,
2642 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2643 hOldPen = SelectObject (lpdis->hDC, hPen );
2644 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2646 /* fill background rectangle */
2647 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2648 lpdis->rcItem.right, lpdis->rcItem.bottom);
2650 /* calculate button and text rectangles */
2651 CopyRect (&rcButton, &lpdis->rcItem);
2652 InflateRect (&rcButton, -1, -1);
2653 CopyRect (&rcText, &rcButton);
2654 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2655 rcText.left = rcButton.right + 2;
2657 /* draw focus rectangle */
2658 if (lpdis->itemState & ODS_FOCUS)
2659 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2661 /* draw button */
2662 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2663 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2665 /* draw image and text */
2666 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2667 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2668 btnInfo->btn.iBitmap));
2669 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2670 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2672 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2673 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2675 /* delete objects and reset colors */
2676 SelectObject (lpdis->hDC, hOldBrush);
2677 SelectObject (lpdis->hDC, hOldPen);
2678 SetBkColor (lpdis->hDC, oldBk);
2679 SetTextColor (lpdis->hDC, oldText);
2680 DeleteObject( hPen );
2681 return TRUE;
2683 return FALSE;
2685 case WM_MEASUREITEM:
2686 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2688 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2690 lpmis->itemHeight = 15 + 8; /* default height */
2692 return TRUE;
2694 return FALSE;
2696 default:
2697 if (uDragListMessage && (uMsg == uDragListMessage))
2699 if (wParam == IDC_TOOLBARBTN_LBOX)
2701 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2702 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2703 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2704 return TRUE;
2706 else if (wParam == IDC_AVAILBTN_LBOX)
2708 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2709 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2710 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2711 return TRUE;
2714 return FALSE;
2718 static BOOL
2719 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2721 HBITMAP hbmLoad;
2722 INT nCountBefore = ImageList_GetImageCount(himlDef);
2723 INT nCountAfter;
2724 INT cxIcon, cyIcon;
2725 INT nAdded;
2726 INT nIndex;
2728 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2729 /* Add bitmaps to the default image list */
2730 if (bitmap->hInst == NULL) /* a handle was passed */
2731 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2732 else if (bitmap->hInst == COMCTL32_hModule)
2733 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2734 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2735 else
2736 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2738 /* enlarge the bitmap if needed */
2739 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2740 if (bitmap->hInst != COMCTL32_hModule)
2741 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2743 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2744 DeleteObject(hbmLoad);
2745 if (nIndex == -1)
2746 return FALSE;
2748 nCountAfter = ImageList_GetImageCount(himlDef);
2749 nAdded = nCountAfter - nCountBefore;
2750 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2752 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2753 } else if (nAdded > (INT)bitmap->nButtons) {
2754 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2755 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2758 infoPtr->nNumBitmaps += nAdded;
2759 return TRUE;
2762 static void
2763 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2765 HIMAGELIST himlDef;
2766 HIMAGELIST himlNew;
2767 INT cx, cy;
2768 INT i;
2770 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2771 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2772 return;
2773 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2774 return;
2775 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2776 return;
2778 TRACE("Update icon size: %dx%d -> %dx%d\n",
2779 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2781 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2782 ILC_COLOR32|ILC_MASK, 8, 2);
2783 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2784 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2785 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2786 infoPtr->himlInt = himlNew;
2788 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2789 ImageList_Destroy(himlDef);
2792 /***********************************************************************
2793 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2796 static LRESULT
2797 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2799 TBITMAP_INFO info;
2800 INT iSumButtons, i;
2801 HIMAGELIST himlDef;
2803 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2804 if (!lpAddBmp)
2805 return -1;
2807 if (lpAddBmp->hInst == HINST_COMMCTRL)
2809 info.hInst = COMCTL32_hModule;
2810 switch (lpAddBmp->nID)
2812 case IDB_STD_SMALL_COLOR:
2813 info.nButtons = 15;
2814 info.nID = IDB_STD_SMALL;
2815 break;
2816 case IDB_STD_LARGE_COLOR:
2817 info.nButtons = 15;
2818 info.nID = IDB_STD_LARGE;
2819 break;
2820 case IDB_VIEW_SMALL_COLOR:
2821 info.nButtons = 12;
2822 info.nID = IDB_VIEW_SMALL;
2823 break;
2824 case IDB_VIEW_LARGE_COLOR:
2825 info.nButtons = 12;
2826 info.nID = IDB_VIEW_LARGE;
2827 break;
2828 case IDB_HIST_SMALL_COLOR:
2829 info.nButtons = 5;
2830 info.nID = IDB_HIST_SMALL;
2831 break;
2832 case IDB_HIST_LARGE_COLOR:
2833 info.nButtons = 5;
2834 info.nID = IDB_HIST_LARGE;
2835 break;
2836 default:
2837 return -1;
2840 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2842 /* Windows resize all the buttons to the size of a newly added standard image */
2843 if (lpAddBmp->nID & 1)
2845 /* large icons: 24x24. Will make the button 31x30 */
2846 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2848 else
2850 /* small icons: 16x16. Will make the buttons 23x22 */
2851 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2854 TOOLBAR_CalcToolbar (infoPtr);
2856 else
2858 info.nButtons = count;
2859 info.hInst = lpAddBmp->hInst;
2860 info.nID = lpAddBmp->nID;
2861 TRACE("adding %d bitmaps!\n", info.nButtons);
2864 /* check if the bitmap is already loaded and compute iSumButtons */
2865 iSumButtons = 0;
2866 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2868 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2869 infoPtr->bitmaps[i].nID == info.nID)
2870 return iSumButtons;
2871 iSumButtons += infoPtr->bitmaps[i].nButtons;
2874 if (!infoPtr->cimlDef) {
2875 /* create new default image list */
2876 TRACE ("creating default image list!\n");
2878 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2879 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2880 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2881 infoPtr->himlInt = himlDef;
2883 else {
2884 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2887 if (!himlDef) {
2888 WARN("No default image list available\n");
2889 return -1;
2892 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2893 return -1;
2895 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2896 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2897 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2898 infoPtr->nNumBitmapInfos++;
2899 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2901 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2902 return iSumButtons;
2906 static LRESULT
2907 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2909 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
2911 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2915 static LRESULT
2916 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2918 #define MAX_RESOURCE_STRING_LENGTH 512
2919 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2920 INT nIndex = infoPtr->nNumStrings;
2922 TRACE("%p, %lx\n", hInstance, lParam);
2924 if (IS_INTRESOURCE(lParam)) {
2925 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2926 WCHAR delimiter;
2927 WCHAR *next_delim;
2928 HRSRC hrsrc;
2929 WCHAR *p;
2930 INT len;
2932 TRACE("adding string from resource\n");
2934 if (!hInstance) return -1;
2936 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2937 (LPWSTR)RT_STRING );
2938 if (!hrsrc)
2940 TRACE("string not found in resources\n");
2941 return -1;
2944 len = LoadStringW (hInstance, (UINT)lParam,
2945 szString, MAX_RESOURCE_STRING_LENGTH);
2947 TRACE("len=%d %s\n", len, debugstr_w(szString));
2948 if (len == 0 || len == 1)
2949 return nIndex;
2951 TRACE("delimiter: 0x%x\n", *szString);
2952 delimiter = *szString;
2953 p = szString + 1;
2955 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2956 *next_delim = 0;
2957 if (next_delim + 1 >= szString + len)
2959 /* this may happen if delimiter == '\0' or if the last char is a
2960 * delimiter (then it is ignored like the native does) */
2961 break;
2964 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2965 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2966 infoPtr->nNumStrings++;
2968 p = next_delim + 1;
2971 else {
2972 LPWSTR p = (LPWSTR)lParam;
2973 INT len;
2975 if (p == NULL)
2976 return -1;
2977 TRACE("adding string(s) from array\n");
2978 while (*p) {
2979 len = strlenW (p);
2981 TRACE("len=%d %s\n", len, debugstr_w(p));
2982 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2983 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2984 infoPtr->nNumStrings++;
2986 p += (len+1);
2990 if (fFirstString)
2991 TOOLBAR_CalcToolbar(infoPtr);
2992 return nIndex;
2996 static LRESULT
2997 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2999 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3000 LPSTR p;
3001 INT nIndex;
3002 INT len;
3004 TRACE("%p, %lx\n", hInstance, lParam);
3006 if (IS_INTRESOURCE(lParam)) /* load from resources */
3007 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3009 p = (LPSTR)lParam;
3010 if (p == NULL)
3011 return -1;
3013 TRACE("adding string(s) from array\n");
3014 nIndex = infoPtr->nNumStrings;
3015 while (*p) {
3016 len = strlen (p);
3017 TRACE("len=%d \"%s\"\n", len, p);
3019 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3020 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3021 infoPtr->nNumStrings++;
3023 p += (len+1);
3026 if (fFirstString)
3027 TOOLBAR_CalcToolbar(infoPtr);
3028 return nIndex;
3032 static LRESULT
3033 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3035 TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3036 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3038 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3040 RECT window_rect, parent_rect;
3041 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3042 HWND parent;
3043 INT x, y, cx, cy;
3045 parent = GetParent (infoPtr->hwndSelf);
3047 if (!parent || !infoPtr->bDoRedraw)
3048 return 0;
3050 GetClientRect(parent, &parent_rect);
3052 x = parent_rect.left;
3053 y = parent_rect.top;
3055 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3056 cx = parent_rect.right - parent_rect.left;
3058 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3060 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3061 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3062 y = window_rect.top;
3064 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3066 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3067 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3070 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3071 uPosFlags |= SWP_NOMOVE;
3073 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3074 cy += GetSystemMetrics(SM_CYEDGE);
3076 if (infoPtr->dwStyle & WS_BORDER)
3078 cx += 2 * GetSystemMetrics(SM_CXBORDER);
3079 cy += 2 * GetSystemMetrics(SM_CYBORDER);
3082 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3085 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3087 TOOLBAR_LayoutToolbar(infoPtr);
3088 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3091 return 0;
3095 static inline LRESULT
3096 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3098 return infoPtr->nNumButtons;
3102 static inline LRESULT
3103 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3105 infoPtr->dwStructSize = Size;
3107 return 0;
3111 static LRESULT
3112 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3114 TBUTTON_INFO *btnPtr;
3115 INT nIndex;
3117 TRACE("button %d, iBitmap now %d\n", Id, Index);
3119 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3120 if (nIndex == -1)
3121 return FALSE;
3123 btnPtr = &infoPtr->buttons[nIndex];
3124 btnPtr->iBitmap = Index;
3126 /* we HAVE to erase the background, the new bitmap could be */
3127 /* transparent */
3128 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3130 return TRUE;
3134 static LRESULT
3135 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3137 TBUTTON_INFO *btnPtr;
3138 INT nIndex;
3139 INT nOldIndex = -1;
3140 BOOL bChecked = FALSE;
3142 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3144 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3146 if (nIndex == -1)
3147 return FALSE;
3149 btnPtr = &infoPtr->buttons[nIndex];
3151 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3153 if (!LOWORD(lParam))
3154 btnPtr->fsState &= ~TBSTATE_CHECKED;
3155 else {
3156 if (btnPtr->fsStyle & BTNS_GROUP) {
3157 nOldIndex =
3158 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3159 if (nOldIndex == nIndex)
3160 return 0;
3161 if (nOldIndex != -1)
3162 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3164 btnPtr->fsState |= TBSTATE_CHECKED;
3167 if( bChecked != LOWORD(lParam) )
3169 if (nOldIndex != -1)
3170 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3171 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3174 /* FIXME: Send a WM_NOTIFY?? */
3176 return TRUE;
3180 static LRESULT
3181 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3183 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3187 static LRESULT
3188 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3190 CUSTDLG_INFO custInfo;
3191 LRESULT ret;
3192 NMHDR nmhdr;
3194 custInfo.tbInfo = infoPtr;
3195 custInfo.tbHwnd = infoPtr->hwndSelf;
3197 /* send TBN_BEGINADJUST notification */
3198 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3200 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3201 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3203 /* send TBN_ENDADJUST notification */
3204 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3206 return ret;
3210 static LRESULT
3211 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3213 NMTOOLBARW nmtb;
3214 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3216 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3217 return FALSE;
3219 memset(&nmtb, 0, sizeof(nmtb));
3220 nmtb.iItem = btnPtr->idCommand;
3221 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3222 nmtb.tbButton.idCommand = btnPtr->idCommand;
3223 nmtb.tbButton.fsState = btnPtr->fsState;
3224 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3225 nmtb.tbButton.dwData = btnPtr->dwData;
3226 nmtb.tbButton.iString = btnPtr->iString;
3227 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3229 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3231 if (infoPtr->nNumButtons == 1) {
3232 TRACE(" simple delete!\n");
3233 free_string( infoPtr->buttons );
3234 Free (infoPtr->buttons);
3235 infoPtr->buttons = NULL;
3236 infoPtr->nNumButtons = 0;
3238 else {
3239 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3240 TRACE("complex delete! [nIndex=%d]\n", nIndex);
3242 infoPtr->nNumButtons--;
3243 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3244 if (nIndex > 0) {
3245 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3246 nIndex * sizeof(TBUTTON_INFO));
3249 if (nIndex < infoPtr->nNumButtons) {
3250 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3251 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3254 free_string( oldButtons + nIndex );
3255 Free (oldButtons);
3258 TOOLBAR_LayoutToolbar(infoPtr);
3260 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3262 return TRUE;
3266 static LRESULT
3267 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3269 TBUTTON_INFO *btnPtr;
3270 INT nIndex;
3271 DWORD bState;
3273 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3275 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3277 if (nIndex == -1)
3278 return FALSE;
3280 btnPtr = &infoPtr->buttons[nIndex];
3282 bState = btnPtr->fsState & TBSTATE_ENABLED;
3284 /* update the toolbar button state */
3285 if(!LOWORD(lParam)) {
3286 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3287 } else {
3288 btnPtr->fsState |= TBSTATE_ENABLED;
3291 /* redraw the button only if the state of the button changed */
3292 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3293 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3295 return TRUE;
3299 static inline LRESULT
3300 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3302 return infoPtr->bAnchor;
3306 static LRESULT
3307 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3309 INT nIndex;
3311 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3312 if (nIndex == -1)
3313 return -1;
3315 return infoPtr->buttons[nIndex].iBitmap;
3319 static inline LRESULT
3320 TOOLBAR_GetBitmapFlags (void)
3322 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3326 static LRESULT
3327 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3329 TBUTTON_INFO *btnPtr;
3331 if (lpTbb == NULL)
3332 return FALSE;
3334 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3335 return FALSE;
3337 btnPtr = &infoPtr->buttons[nIndex];
3338 lpTbb->iBitmap = btnPtr->iBitmap;
3339 lpTbb->idCommand = btnPtr->idCommand;
3340 lpTbb->fsState = btnPtr->fsState;
3341 lpTbb->fsStyle = btnPtr->fsStyle;
3342 lpTbb->bReserved[0] = 0;
3343 lpTbb->bReserved[1] = 0;
3344 lpTbb->dwData = btnPtr->dwData;
3345 lpTbb->iString = btnPtr->iString;
3347 return TRUE;
3351 static LRESULT
3352 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3354 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3355 TBUTTON_INFO *btnPtr;
3356 INT nIndex;
3358 if (lpTbInfo == NULL)
3359 return -1;
3361 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3362 * the headers and tests shows that even with comctl 6 Vista accepts only the
3363 * original TBBUTTONINFO size
3365 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3367 WARN("Invalid button size\n");
3368 return -1;
3371 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3372 if (nIndex == -1)
3373 return -1;
3375 btnPtr = &infoPtr->buttons[nIndex];
3376 if (lpTbInfo->dwMask & TBIF_COMMAND)
3377 lpTbInfo->idCommand = btnPtr->idCommand;
3378 if (lpTbInfo->dwMask & TBIF_IMAGE)
3379 lpTbInfo->iImage = btnPtr->iBitmap;
3380 if (lpTbInfo->dwMask & TBIF_LPARAM)
3381 lpTbInfo->lParam = btnPtr->dwData;
3382 if (lpTbInfo->dwMask & TBIF_SIZE)
3383 /* tests show that for separators TBIF_SIZE returns not calculated width,
3384 but cx property, that differs from 0 only if application have
3385 specifically set it */
3386 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3387 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3388 if (lpTbInfo->dwMask & TBIF_STATE)
3389 lpTbInfo->fsState = btnPtr->fsState;
3390 if (lpTbInfo->dwMask & TBIF_STYLE)
3391 lpTbInfo->fsStyle = btnPtr->fsStyle;
3392 if (lpTbInfo->dwMask & TBIF_TEXT) {
3393 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3394 can't use TOOLBAR_GetText here */
3395 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3396 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3397 if (bUnicode)
3398 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3399 else
3400 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3401 } else
3402 lpTbInfo->pszText[0] = '\0';
3404 return nIndex;
3408 static inline LRESULT
3409 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3411 return MAKELONG((WORD)infoPtr->nButtonWidth,
3412 (WORD)infoPtr->nButtonHeight);
3416 static LRESULT
3417 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3419 INT nIndex;
3420 LPWSTR lpText;
3421 LRESULT ret = 0;
3423 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3424 if (nIndex == -1)
3425 return -1;
3427 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3429 if (isW)
3431 if (lpText)
3433 ret = strlenW (lpText);
3434 if (lpStr) strcpyW (lpStr, lpText);
3437 else
3438 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3439 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3440 return ret;
3444 static LRESULT
3445 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3447 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3448 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3449 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3453 static inline LRESULT
3454 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3456 TRACE("\n");
3458 return infoPtr->dwExStyle;
3462 static LRESULT
3463 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3465 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3466 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3467 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3471 static LRESULT
3472 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3474 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3475 return -1;
3477 if (infoPtr->nHotItem < 0)
3478 return -1;
3480 return (LRESULT)infoPtr->nHotItem;
3484 static LRESULT
3485 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3487 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3488 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3489 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3493 static LRESULT
3494 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3496 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3498 *lptbim = infoPtr->tbim;
3500 return 0;
3504 static inline LRESULT
3505 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3507 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3509 return (LRESULT)infoPtr->clrInsertMark;
3513 static LRESULT
3514 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3516 TBUTTON_INFO *btnPtr;
3518 btnPtr = &infoPtr->buttons[nIndex];
3519 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3520 return FALSE;
3522 if (lpRect == NULL)
3523 return FALSE;
3524 if (btnPtr->fsState & TBSTATE_HIDDEN)
3525 return FALSE;
3527 lpRect->left = btnPtr->rect.left;
3528 lpRect->right = btnPtr->rect.right;
3529 lpRect->bottom = btnPtr->rect.bottom;
3530 lpRect->top = btnPtr->rect.top;
3532 return TRUE;
3536 static LRESULT
3537 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3539 if (lpSize == NULL)
3540 return FALSE;
3542 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3543 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3545 TRACE("maximum size %d x %d\n",
3546 infoPtr->rcBound.right - infoPtr->rcBound.left,
3547 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3549 return TRUE;
3553 /* << TOOLBAR_GetObject >> */
3556 static inline LRESULT
3557 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3559 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3563 static LRESULT
3564 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3566 TBUTTON_INFO *btnPtr;
3567 INT nIndex;
3569 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3570 btnPtr = &infoPtr->buttons[nIndex];
3571 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3572 return FALSE;
3574 if (lpRect == NULL)
3575 return FALSE;
3577 lpRect->left = btnPtr->rect.left;
3578 lpRect->right = btnPtr->rect.right;
3579 lpRect->bottom = btnPtr->rect.bottom;
3580 lpRect->top = btnPtr->rect.top;
3582 return TRUE;
3586 static inline LRESULT
3587 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3589 return infoPtr->nRows;
3593 static LRESULT
3594 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3596 INT nIndex;
3598 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3599 if (nIndex == -1)
3600 return -1;
3602 return infoPtr->buttons[nIndex].fsState;
3606 static inline LRESULT
3607 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3609 return infoPtr->dwStyle;
3613 static inline LRESULT
3614 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3616 return infoPtr->nMaxTextRows;
3620 static LRESULT
3621 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3623 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3624 TOOLBAR_TooltipCreateControl(infoPtr);
3625 return (LRESULT)infoPtr->hwndToolTip;
3629 static LRESULT
3630 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3632 TRACE("%s hwnd=%p\n",
3633 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3635 return infoPtr->bUnicode;
3639 static inline LRESULT
3640 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3642 return infoPtr->iVersion;
3646 static LRESULT
3647 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3649 TBUTTON_INFO *btnPtr;
3650 BYTE oldState;
3651 INT nIndex;
3653 TRACE("\n");
3655 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3656 if (nIndex == -1)
3657 return FALSE;
3659 btnPtr = &infoPtr->buttons[nIndex];
3660 oldState = btnPtr->fsState;
3662 if (fHide)
3663 btnPtr->fsState |= TBSTATE_HIDDEN;
3664 else
3665 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3667 if (oldState != btnPtr->fsState) {
3668 TOOLBAR_LayoutToolbar (infoPtr);
3669 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3672 return TRUE;
3676 static inline LRESULT
3677 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3679 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3683 static LRESULT
3684 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3686 TBUTTON_INFO *btnPtr;
3687 INT nIndex;
3688 DWORD oldState;
3690 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3691 if (nIndex == -1)
3692 return FALSE;
3694 btnPtr = &infoPtr->buttons[nIndex];
3695 oldState = btnPtr->fsState;
3697 if (fIndeterminate)
3698 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3699 else
3700 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3702 if(oldState != btnPtr->fsState)
3703 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3705 return TRUE;
3709 static LRESULT
3710 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3712 if (lpTbb == NULL)
3713 return FALSE;
3715 if (nIndex == -1) {
3716 /* EPP: this seems to be an undocumented call (from my IE4)
3717 * I assume in that case that:
3718 * - index of insertion is at the end of existing buttons
3719 * I only see this happen with nIndex == -1, but it could have a special
3720 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3722 nIndex = infoPtr->nNumButtons;
3724 } else if (nIndex < 0)
3725 return FALSE;
3727 TRACE("inserting button index=%d\n", nIndex);
3728 if (nIndex > infoPtr->nNumButtons) {
3729 nIndex = infoPtr->nNumButtons;
3730 TRACE("adjust index=%d\n", nIndex);
3733 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3736 /* << TOOLBAR_InsertMarkHitTest >> */
3739 static LRESULT
3740 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3742 INT nIndex;
3744 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3745 if (nIndex == -1)
3746 return -1;
3748 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3752 static LRESULT
3753 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3755 INT nIndex;
3757 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3758 if (nIndex == -1)
3759 return -1;
3761 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3765 static LRESULT
3766 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3768 INT nIndex;
3770 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3771 if (nIndex == -1)
3772 return -1;
3774 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3778 static LRESULT
3779 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3781 INT nIndex;
3783 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3784 if (nIndex == -1)
3785 return -1;
3787 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3791 static LRESULT
3792 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3794 INT nIndex;
3796 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3797 if (nIndex == -1)
3798 return -1;
3800 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3804 static LRESULT
3805 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3807 INT nIndex;
3809 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3810 if (nIndex == -1)
3811 return -1;
3813 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3817 static LRESULT
3818 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3820 TBADDBITMAP tbab;
3821 tbab.hInst = hInstance;
3822 tbab.nID = wParam;
3824 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3826 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3830 static LRESULT
3831 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3833 WCHAR wszAccel[] = {'&',wAccel,0};
3834 int i;
3836 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3837 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3839 for (i = 0; i < infoPtr->nNumButtons; i++)
3841 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3842 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3843 !(btnPtr->fsState & TBSTATE_HIDDEN))
3845 int iLen = strlenW(wszAccel);
3846 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3848 if (!lpszStr)
3849 continue;
3851 while (*lpszStr)
3853 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3855 lpszStr += 2;
3856 continue;
3858 if (!strncmpiW(lpszStr, wszAccel, iLen))
3860 *pIDButton = btnPtr->idCommand;
3861 return TRUE;
3863 lpszStr++;
3867 return FALSE;
3871 static LRESULT
3872 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3874 INT nIndex;
3875 DWORD oldState;
3876 TBUTTON_INFO *btnPtr;
3878 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3880 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3881 if (nIndex == -1)
3882 return FALSE;
3884 btnPtr = &infoPtr->buttons[nIndex];
3885 oldState = btnPtr->fsState;
3887 if (fMark)
3888 btnPtr->fsState |= TBSTATE_MARKED;
3889 else
3890 btnPtr->fsState &= ~TBSTATE_MARKED;
3892 if(oldState != btnPtr->fsState)
3893 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3895 return TRUE;
3899 /* fixes up an index of a button affected by a move */
3900 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3902 if (bMoveUp)
3904 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3905 (*pIndex)--;
3906 else if (*pIndex == nIndex)
3907 *pIndex = nMoveIndex;
3909 else
3911 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3912 (*pIndex)++;
3913 else if (*pIndex == nIndex)
3914 *pIndex = nMoveIndex;
3919 static LRESULT
3920 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3922 INT nIndex;
3923 INT nCount;
3924 TBUTTON_INFO button;
3926 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3928 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3929 if ((nIndex == -1) || (nMoveIndex < 0))
3930 return FALSE;
3932 if (nMoveIndex > infoPtr->nNumButtons - 1)
3933 nMoveIndex = infoPtr->nNumButtons - 1;
3935 button = infoPtr->buttons[nIndex];
3937 /* move button right */
3938 if (nIndex < nMoveIndex)
3940 nCount = nMoveIndex - nIndex;
3941 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3942 infoPtr->buttons[nMoveIndex] = button;
3944 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3945 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3946 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3947 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3949 else if (nIndex > nMoveIndex) /* move button left */
3951 nCount = nIndex - nMoveIndex;
3952 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3953 infoPtr->buttons[nMoveIndex] = button;
3955 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3956 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3957 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3958 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3961 TOOLBAR_LayoutToolbar(infoPtr);
3962 TOOLBAR_AutoSize(infoPtr);
3963 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3965 return TRUE;
3969 static LRESULT
3970 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
3972 TBUTTON_INFO *btnPtr;
3973 INT nIndex;
3974 DWORD oldState;
3976 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3977 if (nIndex == -1)
3978 return FALSE;
3980 btnPtr = &infoPtr->buttons[nIndex];
3981 oldState = btnPtr->fsState;
3983 if (fPress)
3984 btnPtr->fsState |= TBSTATE_PRESSED;
3985 else
3986 btnPtr->fsState &= ~TBSTATE_PRESSED;
3988 if(oldState != btnPtr->fsState)
3989 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3991 return TRUE;
3994 /* FIXME: there might still be some confusion her between number of buttons
3995 * and number of bitmaps */
3996 static LRESULT
3997 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
3999 HBITMAP hBitmap;
4000 int i = 0, nOldButtons = 0, pos = 0;
4001 int nOldBitmaps, nNewBitmaps = 0;
4002 HIMAGELIST himlDef = 0;
4004 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4005 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4006 lpReplace->nButtons);
4008 if (lpReplace->hInstOld == HINST_COMMCTRL)
4010 FIXME("changing standard bitmaps not implemented\n");
4011 return FALSE;
4013 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4014 FIXME("resources not in the current module not implemented\n");
4016 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4017 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4018 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4019 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4020 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4022 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4023 nOldButtons = tbi->nButtons;
4024 tbi->nButtons = lpReplace->nButtons;
4025 tbi->hInst = lpReplace->hInstNew;
4026 tbi->nID = lpReplace->nIDNew;
4027 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4028 break;
4030 pos += tbi->nButtons;
4033 if (nOldButtons == 0)
4035 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4036 return FALSE;
4039 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4040 * bitmap
4042 if (lpReplace->hInstNew)
4043 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4044 else
4045 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4047 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4048 nOldBitmaps = ImageList_GetImageCount(himlDef);
4050 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4052 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4053 ImageList_Remove(himlDef, i);
4055 if (hBitmap)
4057 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4058 nNewBitmaps = ImageList_GetImageCount(himlDef);
4059 DeleteObject(hBitmap);
4062 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4064 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4065 pos, nOldBitmaps, nNewBitmaps);
4067 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4068 return TRUE;
4072 /* helper for TOOLBAR_SaveRestoreW */
4073 static BOOL
4074 TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
4076 NMTBSAVE save;
4077 INT ret, i;
4078 BOOL alloced = FALSE;
4079 HKEY key;
4081 TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );
4083 memset( &save, 0, sizeof(save) );
4084 save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
4085 save.iItem = -1;
4086 save.cButtons = infoPtr->nNumButtons;
4087 save.tbButton.idCommand = -1;
4088 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4090 if (!save.pData)
4092 save.pData = Alloc( save.cbData );
4093 if (!save.pData) return FALSE;
4094 alloced = TRUE;
4096 if (!save.pCurrent) save.pCurrent = save.pData;
4098 for (i = 0; i < infoPtr->nNumButtons; i++)
4100 save.iItem = i;
4101 save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
4102 save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
4103 save.tbButton.fsState = infoPtr->buttons[i].fsState;
4104 save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
4105 memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
4106 save.tbButton.dwData = infoPtr->buttons[i].dwData;
4107 save.tbButton.iString = infoPtr->buttons[i].iString;
4109 *save.pCurrent++ = save.tbButton.idCommand;
4111 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4114 ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
4115 if (ret == ERROR_SUCCESS)
4117 ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
4118 RegCloseKey( key );
4121 if (alloced) Free( save.pData );
4122 return !ret;
4126 /* helper for TOOLBAR_Restore */
4127 static void
4128 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4130 INT i;
4132 for (i = 0; i < infoPtr->nNumButtons; i++)
4134 free_string( infoPtr->buttons + i );
4135 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4138 Free(infoPtr->buttons);
4139 infoPtr->buttons = NULL;
4140 infoPtr->nNumButtons = 0;
4144 /* helper for TOOLBAR_SaveRestoreW */
4145 static BOOL
4146 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4148 LONG res;
4149 HKEY hkey = NULL;
4150 BOOL ret = FALSE;
4151 DWORD dwType;
4152 DWORD dwSize = 0;
4153 NMTBRESTORE nmtbr;
4154 NMHDR hdr;
4156 /* restore toolbar information */
4157 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4158 debugstr_w(lpSave->pszValueName));
4160 memset(&nmtbr, 0, sizeof(nmtbr));
4162 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4163 KEY_QUERY_VALUE, &hkey);
4164 if (!res)
4165 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4166 NULL, &dwSize);
4167 if (!res && dwType != REG_BINARY)
4168 res = ERROR_FILE_NOT_FOUND;
4169 if (!res)
4171 nmtbr.pData = Alloc(dwSize);
4172 nmtbr.cbData = dwSize;
4173 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4175 if (!res)
4176 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4177 (LPBYTE)nmtbr.pData, &dwSize);
4178 if (!res)
4180 nmtbr.pCurrent = nmtbr.pData;
4181 nmtbr.iItem = -1;
4182 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4183 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4185 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4187 INT i, count = nmtbr.cButtons;
4189 /* remove all existing buttons as this function is designed to
4190 * restore the toolbar to a previously saved state */
4191 TOOLBAR_DeleteAllButtons(infoPtr);
4193 for (i = 0; i < count; i++)
4195 nmtbr.iItem = i;
4196 nmtbr.tbButton.iBitmap = -1;
4197 nmtbr.tbButton.fsState = 0;
4198 nmtbr.tbButton.fsStyle = 0;
4199 nmtbr.tbButton.dwData = 0;
4200 nmtbr.tbButton.iString = 0;
4202 if (*nmtbr.pCurrent & 0x80000000)
4204 /* separator */
4205 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4206 nmtbr.tbButton.idCommand = 0;
4207 nmtbr.tbButton.fsStyle = BTNS_SEP;
4208 if (*nmtbr.pCurrent != (DWORD)-1)
4209 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4211 else
4212 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4214 nmtbr.pCurrent++;
4216 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4218 /* All returned ptrs and -1 are ignored */
4219 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4220 nmtbr.tbButton.iString = 0;
4222 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4225 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
4226 for (i = 0; ; i++)
4228 NMTOOLBARW tb;
4229 TBBUTTONINFOW bi;
4230 WCHAR buf[128];
4231 UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
4232 INT idx;
4234 memset( &tb, 0, sizeof(tb) );
4235 tb.iItem = i;
4236 tb.cchText = sizeof(buf) / sizeof(buf[0]);
4237 tb.pszText = buf;
4239 /* Use the same struct for both A and W versions since the layout is the same. */
4240 if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
4241 break;
4243 idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
4244 if (idx == -1) continue;
4246 /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
4247 be an index or a ptr. Either way it is simply copied. There is no api to change
4248 the string index, so we set it manually. The other properties can be set with SetButtonInfo. */
4249 free_string( infoPtr->buttons + idx );
4250 infoPtr->buttons[idx].iString = tb.tbButton.iString;
4252 memset( &bi, 0, sizeof(bi) );
4253 bi.cbSize = sizeof(bi);
4254 bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
4255 bi.iImage = tb.tbButton.iBitmap;
4256 bi.fsState = tb.tbButton.fsState;
4257 bi.fsStyle = tb.tbButton.fsStyle;
4258 bi.lParam = tb.tbButton.dwData;
4260 TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4262 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4264 /* remove all uninitialised buttons
4265 * note: loop backwards to avoid having to fixup i on a
4266 * delete */
4267 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4268 if (infoPtr->buttons[i].iBitmap == -1)
4269 TOOLBAR_DeleteButton(infoPtr, i);
4271 /* only indicate success if at least one button survived */
4272 if (infoPtr->nNumButtons > 0) ret = TRUE;
4275 Free (nmtbr.pData);
4276 RegCloseKey(hkey);
4278 return ret;
4282 static LRESULT
4283 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4285 if (lpSave == NULL) return 0;
4287 if (wParam)
4288 return TOOLBAR_Save(infoPtr, lpSave);
4289 else
4290 return TOOLBAR_Restore(infoPtr, lpSave);
4294 static LRESULT
4295 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4297 LPWSTR pszValueName = 0, pszSubKey = 0;
4298 TBSAVEPARAMSW SaveW;
4299 LRESULT result = 0;
4300 int len;
4302 if (lpSave == NULL) return 0;
4304 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4305 pszSubKey = Alloc(len * sizeof(WCHAR));
4306 if (!pszSubKey) goto exit;
4307 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4309 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4310 pszValueName = Alloc(len * sizeof(WCHAR));
4311 if (!pszValueName) goto exit;
4312 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4314 SaveW.pszValueName = pszValueName;
4315 SaveW.pszSubKey = pszSubKey;
4316 SaveW.hkr = lpSave->hkr;
4317 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4319 exit:
4320 Free (pszValueName);
4321 Free (pszSubKey);
4323 return result;
4327 static LRESULT
4328 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4330 BOOL bOldAnchor = infoPtr->bAnchor;
4332 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4334 infoPtr->bAnchor = bAnchor;
4336 /* Native does not remove the hot effect from an already hot button */
4338 return (LRESULT)bOldAnchor;
4342 static LRESULT
4343 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4345 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4346 short width = (short)LOWORD(lParam);
4347 short height = (short)HIWORD(lParam);
4349 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", infoPtr->hwndSelf, wParam, lParam);
4351 if (wParam != 0)
4352 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4354 /* 0 width or height is changed to 1 */
4355 if (width == 0)
4356 width = 1;
4357 if (height == 0)
4358 height = 1;
4360 if (infoPtr->nNumButtons > 0)
4361 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4362 infoPtr->nNumButtons,
4363 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4365 if (width < -1 || height < -1)
4367 /* Windows destroys the imagelist and seems to actually use negative
4368 * values to compute button sizes */
4369 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4370 return FALSE;
4373 /* width or height of -1 means no change */
4374 if (width != -1)
4375 infoPtr->nBitmapWidth = width;
4376 if (height != -1)
4377 infoPtr->nBitmapHeight = height;
4379 if ((himlDef == infoPtr->himlInt) &&
4380 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4382 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4383 infoPtr->nBitmapHeight);
4386 TOOLBAR_CalcToolbar(infoPtr);
4387 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4388 return TRUE;
4392 static LRESULT
4393 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4394 const TBBUTTONINFOW *lptbbi, BOOL isW)
4396 TBUTTON_INFO *btnPtr;
4397 INT nIndex;
4398 RECT oldBtnRect;
4400 if (lptbbi == NULL)
4401 return FALSE;
4402 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4403 return FALSE;
4405 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4406 if (nIndex == -1)
4407 return FALSE;
4409 btnPtr = &infoPtr->buttons[nIndex];
4410 if (lptbbi->dwMask & TBIF_COMMAND)
4411 btnPtr->idCommand = lptbbi->idCommand;
4412 if (lptbbi->dwMask & TBIF_IMAGE)
4413 btnPtr->iBitmap = lptbbi->iImage;
4414 if (lptbbi->dwMask & TBIF_LPARAM)
4415 btnPtr->dwData = lptbbi->lParam;
4416 if (lptbbi->dwMask & TBIF_SIZE)
4417 btnPtr->cx = lptbbi->cx;
4418 if (lptbbi->dwMask & TBIF_STATE)
4419 btnPtr->fsState = lptbbi->fsState;
4420 if (lptbbi->dwMask & TBIF_STYLE)
4421 btnPtr->fsStyle = lptbbi->fsStyle;
4423 if (lptbbi->dwMask & TBIF_TEXT)
4424 set_stringT( btnPtr, lptbbi->pszText, isW );
4426 /* save the button rect to see if we need to redraw the whole toolbar */
4427 oldBtnRect = btnPtr->rect;
4428 TOOLBAR_LayoutToolbar(infoPtr);
4430 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4431 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4432 else
4433 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4435 return TRUE;
4439 static LRESULT
4440 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4442 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4443 int top = default_top_margin(infoPtr);
4445 if ((cx < 0) || (cy < 0))
4447 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4448 return FALSE;
4451 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4453 /* The documentation claims you can only change the button size before
4454 * any button has been added. But this is wrong.
4455 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4456 * it to the toolbar, and it checks that the return value is nonzero - mjm
4457 * Further testing shows that we must actually perform the change too.
4460 * The documentation also does not mention that if 0 is supplied for
4461 * either size, the system changes it to the default of 24 wide and
4462 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4464 if (cx == 0) cx = 24;
4465 if (cy == 0) cy = 22;
4467 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4468 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4470 if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
4471 top != infoPtr->iTopMargin)
4473 infoPtr->nButtonWidth = cx;
4474 infoPtr->nButtonHeight = cy;
4475 infoPtr->iTopMargin = top;
4477 TOOLBAR_LayoutToolbar( infoPtr );
4478 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
4480 return TRUE;
4484 static LRESULT
4485 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4487 /* if setting to current values, ignore */
4488 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4489 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4490 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4491 infoPtr->cxMin, infoPtr->cxMax);
4492 return TRUE;
4495 /* save new values */
4496 infoPtr->cxMin = (short)LOWORD(lParam);
4497 infoPtr->cxMax = (short)HIWORD(lParam);
4499 /* otherwise we need to recalc the toolbar and in some cases
4500 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4501 which doesn't actually draw - GA). */
4502 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4503 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4505 TOOLBAR_CalcToolbar (infoPtr);
4507 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4509 return TRUE;
4513 static LRESULT
4514 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4516 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4517 return FALSE;
4519 infoPtr->buttons[nIndex].idCommand = nId;
4521 if (infoPtr->hwndToolTip) {
4523 FIXME("change tool tip!\n");
4527 return TRUE;
4531 static LRESULT
4532 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4534 HIMAGELIST himlTemp;
4535 INT id = 0;
4537 if (infoPtr->iVersion >= 5)
4538 id = wParam;
4540 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4541 &infoPtr->cimlDis, himl, id);
4543 /* FIXME: redraw ? */
4545 return (LRESULT)himlTemp;
4549 static LRESULT
4550 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4552 DWORD dwTemp;
4554 TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", infoPtr->hwndSelf, (DWORD)wParam, (DWORD)lParam);
4556 dwTemp = infoPtr->dwDTFlags;
4557 infoPtr->dwDTFlags =
4558 (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4560 return (LRESULT)dwTemp;
4563 /* This function differs a bit from what MSDN says it does:
4564 * 1. lParam contains extended style flags to OR with current style
4565 * (MSDN isn't clear on the OR bit)
4566 * 2. wParam appears to contain extended style flags to be reset
4567 * (MSDN says that this parameter is reserved)
4569 static LRESULT
4570 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4572 DWORD old_style = infoPtr->dwExStyle;
4574 TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
4576 if (mask)
4577 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4578 else
4579 infoPtr->dwExStyle = style;
4581 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4582 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4583 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4585 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4586 TOOLBAR_CalcToolbar(infoPtr);
4587 else
4588 TOOLBAR_LayoutToolbar(infoPtr);
4590 TOOLBAR_AutoSize(infoPtr);
4591 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4593 return old_style;
4597 static LRESULT
4598 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4600 HIMAGELIST himlTemp;
4601 INT id = 0;
4603 if (infoPtr->iVersion >= 5)
4604 id = wParam;
4606 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4608 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4609 &infoPtr->cimlHot, himl, id);
4611 /* FIXME: redraw ? */
4613 return (LRESULT)himlTemp;
4617 /* Makes previous hot button no longer hot, makes the specified
4618 * button hot and sends appropriate notifications. dwReason is one or
4619 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4620 * NOTE 1: this function does not validate nHit
4621 * NOTE 2: the name of this function is completely made up and
4622 * not based on any documentation from Microsoft. */
4623 static void
4624 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4626 if (infoPtr->nHotItem != nHit)
4628 NMTBHOTITEM nmhotitem;
4629 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4631 nmhotitem.dwFlags = dwReason;
4632 if(infoPtr->nHotItem >= 0)
4634 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4635 nmhotitem.idOld = oldBtnPtr->idCommand;
4637 else
4639 nmhotitem.dwFlags |= HICF_ENTERING;
4640 nmhotitem.idOld = 0;
4643 if (nHit >= 0)
4645 btnPtr = &infoPtr->buttons[nHit];
4646 nmhotitem.idNew = btnPtr->idCommand;
4648 else
4650 nmhotitem.dwFlags |= HICF_LEAVING;
4651 nmhotitem.idNew = 0;
4654 /* now change the hot and invalidate the old and new buttons - if the
4655 * parent agrees */
4656 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4658 if (oldBtnPtr) {
4659 oldBtnPtr->bHot = FALSE;
4660 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4662 /* setting disabled buttons as hot fails even if the notify contains the button id */
4663 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4664 btnPtr->bHot = TRUE;
4665 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4666 infoPtr->nHotItem = nHit;
4668 else
4669 infoPtr->nHotItem = -1;
4674 static LRESULT
4675 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4677 INT nOldHotItem = infoPtr->nHotItem;
4679 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4681 if (nHotItem >= infoPtr->nNumButtons)
4682 return infoPtr->nHotItem;
4684 if (nHotItem < 0)
4685 nHotItem = -1;
4687 /* NOTE: an application can still remove the hot item even if anchor
4688 * highlighting is enabled */
4690 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4692 if (nOldHotItem < 0)
4693 return -1;
4695 return (LRESULT)nOldHotItem;
4699 static LRESULT
4700 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4702 HIMAGELIST himlTemp;
4703 INT oldButtonWidth = infoPtr->nButtonWidth;
4704 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4705 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4706 INT i, id = 0;
4708 if (infoPtr->iVersion >= 5)
4709 id = wParam;
4711 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4712 &infoPtr->cimlDef, himl, id);
4714 infoPtr->nNumBitmaps = 0;
4715 for (i = 0; i < infoPtr->cimlDef; i++)
4716 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4718 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4719 &infoPtr->nBitmapHeight))
4721 infoPtr->nBitmapWidth = 1;
4722 infoPtr->nBitmapHeight = 1;
4724 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4726 TOOLBAR_CalcToolbar(infoPtr);
4727 if (infoPtr->nButtonWidth < oldButtonWidth)
4728 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4731 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4732 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4733 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4735 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4737 return (LRESULT)himlTemp;
4741 static LRESULT
4742 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4744 infoPtr->nIndent = nIndent;
4746 TRACE("\n");
4748 /* process only on indent changing */
4749 if(infoPtr->nIndent != nIndent)
4751 infoPtr->nIndent = nIndent;
4752 TOOLBAR_CalcToolbar (infoPtr);
4753 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4756 return TRUE;
4760 static LRESULT
4761 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4763 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4765 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4767 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4768 return 0;
4771 if ((lptbim->iButton == -1) ||
4772 ((lptbim->iButton < infoPtr->nNumButtons) &&
4773 (lptbim->iButton >= 0)))
4775 infoPtr->tbim = *lptbim;
4776 /* FIXME: don't need to update entire toolbar */
4777 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4779 else
4780 ERR("Invalid button index %d\n", lptbim->iButton);
4782 return 0;
4786 static LRESULT
4787 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4789 infoPtr->clrInsertMark = clr;
4791 /* FIXME: don't need to update entire toolbar */
4792 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4794 return 0;
4798 static LRESULT
4799 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4801 infoPtr->nMaxTextRows = nMaxRows;
4803 TOOLBAR_CalcToolbar(infoPtr);
4804 return TRUE;
4808 /* MSDN gives slightly wrong info on padding.
4809 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4810 * 2. It is not used to create a blank area between the edge of the button
4811 * and the text or image if TBSTYLE_LIST is set. It is used to control
4812 * the gap between the image and text.
4813 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4814 * to control the bottom and right borders [with the border being
4815 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4816 * is shared evenly on both sides of the button.
4817 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4819 static LRESULT
4820 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4822 DWORD oldPad;
4824 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4825 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4826 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4827 TRACE("cx=%d, cy=%d\n",
4828 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4829 return (LRESULT) oldPad;
4833 static LRESULT
4834 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4836 HWND hwndOldNotify;
4838 TRACE("\n");
4840 hwndOldNotify = infoPtr->hwndNotify;
4841 infoPtr->hwndNotify = hParent;
4843 return (LRESULT)hwndOldNotify;
4847 static LRESULT
4848 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4850 int rows = LOWORD(wParam);
4851 BOOL bLarger = HIWORD(wParam);
4853 TRACE("\n");
4855 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4857 if(infoPtr->nRows != rows)
4859 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4860 int curColumn = 0; /* Current column */
4861 int curRow = 0; /* Current row */
4862 int hidden = 0; /* Number of hidden buttons */
4863 int seps = 0; /* Number of separators */
4864 int idealWrap = 0; /* Ideal wrap point */
4865 int i;
4866 BOOL wrap;
4869 Calculate new size and wrap points - Under windows, setrows will
4870 change the dimensions if needed to show the number of requested
4871 rows (if CCS_NORESIZE is set), or will take up the whole window
4872 (if no CCS_NORESIZE).
4874 Basic algorithm - If N buttons, and y rows requested, each row
4875 contains N/y buttons.
4877 FIXME: Handling of separators not obvious from testing results
4878 FIXME: Take width of window into account?
4881 /* Loop through the buttons one by one counting key items */
4882 for (i = 0; i < infoPtr->nNumButtons; i++ )
4884 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4885 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4886 hidden++;
4887 else if (btnPtr[i].fsStyle & BTNS_SEP)
4888 seps++;
4891 /* FIXME: Separators make this quite complex */
4892 if (seps) FIXME("Separators unhandled\n");
4894 /* Round up so more per line, i.e., less rows */
4895 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4897 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4898 achieve the requested number of rows. */
4899 if (bLarger && idealWrap > 1)
4901 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4902 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4904 if (resRows < rows && moreRows > rows)
4906 idealWrap--;
4907 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4911 curColumn = curRow = 0;
4912 wrap = FALSE;
4913 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4914 infoPtr->nNumButtons, hidden, rows);
4916 for (i = 0; i < infoPtr->nNumButtons; i++ )
4918 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4919 continue;
4921 /* Step on, wrap if necessary or flag next to wrap */
4922 if (!wrap) {
4923 curColumn++;
4924 } else {
4925 wrap = FALSE;
4926 curColumn = 1;
4927 curRow++;
4930 if (curColumn > (idealWrap-1)) {
4931 wrap = TRUE;
4932 btnPtr[i].fsState |= TBSTATE_WRAP;
4936 TRACE("Result - %d rows\n", curRow + 1);
4938 /* recalculate toolbar */
4939 TOOLBAR_CalcToolbar (infoPtr);
4941 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4942 if NORESIZE is NOT set, then the toolbar will always be resized to
4943 take up the whole window. With it set, sizing needs to be manual. */
4944 if (infoPtr->dwStyle & CCS_NORESIZE) {
4945 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4946 infoPtr->rcBound.right - infoPtr->rcBound.left,
4947 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4948 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4951 /* repaint toolbar */
4952 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4955 /* return bounding rectangle */
4956 if (lprc) {
4957 lprc->left = infoPtr->rcBound.left;
4958 lprc->right = infoPtr->rcBound.right;
4959 lprc->top = infoPtr->rcBound.top;
4960 lprc->bottom = infoPtr->rcBound.bottom;
4963 return 0;
4967 static LRESULT
4968 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4970 TBUTTON_INFO *btnPtr;
4971 INT nIndex;
4973 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4974 if (nIndex == -1)
4975 return FALSE;
4977 btnPtr = &infoPtr->buttons[nIndex];
4979 /* if hidden state has changed the invalidate entire window and recalc */
4980 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4981 btnPtr->fsState = LOWORD(lParam);
4982 TOOLBAR_CalcToolbar (infoPtr);
4983 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4984 return TRUE;
4987 /* process state changing if current state doesn't match new state */
4988 if(btnPtr->fsState != LOWORD(lParam))
4990 btnPtr->fsState = LOWORD(lParam);
4991 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4994 return TRUE;
4997 static inline void unwrap(TOOLBAR_INFO *info)
4999 int i;
5001 for (i = 0; i < info->nNumButtons; i++)
5002 info->buttons[i].fsState &= ~TBSTATE_WRAP;
5005 static LRESULT
5006 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
5008 DWORD dwOldStyle = infoPtr->dwStyle;
5010 TRACE("new style 0x%08x\n", style);
5012 if (style & TBSTYLE_LIST)
5013 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
5014 else
5015 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
5017 infoPtr->dwStyle = style;
5018 TOOLBAR_CheckStyle(infoPtr);
5020 if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5022 if (dwOldStyle & TBSTYLE_WRAPABLE)
5023 unwrap(infoPtr);
5024 TOOLBAR_CalcToolbar(infoPtr);
5026 else if ((dwOldStyle ^ style) & CCS_VERT)
5027 TOOLBAR_LayoutToolbar(infoPtr);
5029 /* only resize if one of the CCS_* styles was changed */
5030 if ((dwOldStyle ^ style) & COMMON_STYLES)
5032 TOOLBAR_AutoSize(infoPtr);
5033 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5036 return 0;
5040 static inline LRESULT
5041 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
5043 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5045 infoPtr->hwndToolTip = hwndTooltip;
5046 return 0;
5050 static LRESULT
5051 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
5053 BOOL bTemp;
5055 TRACE("%s hwnd=%p\n",
5056 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
5058 bTemp = infoPtr->bUnicode;
5059 infoPtr->bUnicode = (BOOL)wParam;
5061 return bTemp;
5065 static LRESULT
5066 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5068 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5069 comctl32_color.clrBtnHighlight :
5070 infoPtr->clrBtnHighlight;
5071 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5072 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5073 return 1;
5077 static LRESULT
5078 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5080 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5081 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5082 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5084 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5085 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5086 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5087 return 0;
5091 static LRESULT
5092 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5094 INT iOldVersion = infoPtr->iVersion;
5096 infoPtr->iVersion = iVersion;
5098 if (infoPtr->iVersion >= 5)
5099 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5101 return iOldVersion;
5105 static LRESULT
5106 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
5108 WORD iString = HIWORD(wParam);
5109 WORD buffersize = LOWORD(wParam);
5110 LRESULT ret = -1;
5112 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
5114 if (iString < infoPtr->nNumStrings)
5116 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5117 ret--;
5119 TRACE("returning %s\n", debugstr_a(str));
5121 else
5122 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5124 return ret;
5128 static LRESULT
5129 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5131 WORD iString = HIWORD(wParam);
5132 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5133 LRESULT ret = -1;
5135 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5137 if (iString < infoPtr->nNumStrings)
5139 len = min(len, strlenW(infoPtr->strings[iString]));
5140 ret = (len+1)*sizeof(WCHAR);
5141 if (str)
5143 memcpy(str, infoPtr->strings[iString], ret);
5144 str[len] = '\0';
5146 ret = len;
5148 TRACE("returning %s\n", debugstr_w(str));
5150 else
5151 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5153 return ret;
5156 static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5158 SIZE * pSize = (SIZE*)lParam;
5159 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5160 return 0;
5163 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5164 * caller to specify a reason why the hot item changed (rather than just the
5165 * HICF_OTHER that TB_SETHOTITEM sends). */
5166 static LRESULT
5167 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5169 INT nOldHotItem = infoPtr->nHotItem;
5171 TRACE("old item=%d, new item=%d, flags=%08x\n",
5172 nOldHotItem, nHotItem, (DWORD)lParam);
5174 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5175 nHotItem = -1;
5177 /* NOTE: an application can still remove the hot item even if anchor
5178 * highlighting is enabled */
5180 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5182 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5185 /* Sets the toolbar global iListGap parameter which controls the amount of
5186 * spacing between the image and the text of buttons for TBSTYLE_LIST
5187 * toolbars. */
5188 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5190 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5192 infoPtr->iListGap = iListGap;
5194 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5196 return 0;
5199 /* Returns the number of maximum number of image lists associated with the
5200 * various states. */
5201 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5203 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5205 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5208 static LRESULT
5209 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5211 LPSIZE lpsize = (LPSIZE)lParam;
5213 if (lpsize == NULL)
5214 return FALSE;
5217 * Testing shows the following:
5218 * wParam = 0 adjust cx value
5219 * = 1 set cy value to max size.
5220 * lParam pointer to SIZE structure
5223 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5224 wParam, lParam, lpsize->cx, lpsize->cy);
5226 switch(wParam) {
5227 case 0:
5228 if (lpsize->cx == -1) {
5229 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5231 else if(HIWORD(lpsize->cx)) {
5232 RECT rc;
5233 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5235 GetWindowRect(infoPtr->hwndSelf, &rc);
5236 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5237 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5238 lpsize->cx = max(rc.right-rc.left,
5239 infoPtr->rcBound.right - infoPtr->rcBound.left);
5241 else {
5242 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5244 break;
5245 case 1:
5246 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5247 break;
5248 default:
5249 FIXME("Unknown wParam %ld\n", wParam);
5250 return 0;
5252 TRACE("set to -> 0x%08x 0x%08x\n",
5253 lpsize->cx, lpsize->cy);
5254 return 1;
5257 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5259 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5261 InvalidateRect(hwnd, NULL, TRUE);
5262 return 1;
5266 static LRESULT
5267 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5269 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5270 LOGFONTW logFont;
5272 TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
5274 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5275 GetClientRect(hwnd, &infoPtr->client_rect);
5276 infoPtr->bUnicode = infoPtr->hwndNotify &&
5277 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5278 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5280 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5281 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5283 OpenThemeData (hwnd, themeClass);
5285 TOOLBAR_CheckStyle (infoPtr);
5287 return 0;
5291 static LRESULT
5292 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5294 INT i;
5296 /* delete tooltip control */
5297 if (infoPtr->hwndToolTip)
5298 DestroyWindow (infoPtr->hwndToolTip);
5300 /* delete temporary buffer for tooltip text */
5301 Free (infoPtr->pszTooltipText);
5302 Free (infoPtr->bitmaps); /* bitmaps list */
5304 /* delete button data */
5305 for (i = 0; i < infoPtr->nNumButtons; i++)
5306 free_string( infoPtr->buttons + i );
5307 Free (infoPtr->buttons);
5309 /* delete strings */
5310 if (infoPtr->strings) {
5311 for (i = 0; i < infoPtr->nNumStrings; i++)
5312 Free (infoPtr->strings[i]);
5314 Free (infoPtr->strings);
5317 /* destroy internal image list */
5318 if (infoPtr->himlInt)
5319 ImageList_Destroy (infoPtr->himlInt);
5321 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5322 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5323 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5325 /* delete default font */
5326 DeleteObject (infoPtr->hDefaultFont);
5328 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
5330 /* free toolbar info data */
5331 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5332 Free (infoPtr);
5334 return 0;
5338 static LRESULT
5339 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5341 NMTBCUSTOMDRAW tbcd;
5342 INT ret = FALSE;
5343 DWORD ntfret;
5344 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5345 DWORD dwEraseCustDraw = 0;
5347 /* the app has told us not to redraw the toolbar */
5348 if (!infoPtr->bDoRedraw)
5349 return FALSE;
5351 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5352 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5353 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5354 tbcd.nmcd.hdc = (HDC)wParam;
5355 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5356 dwEraseCustDraw = ntfret & 0xffff;
5358 /* FIXME: in general the return flags *can* be or'ed together */
5359 switch (dwEraseCustDraw)
5361 case CDRF_DODEFAULT:
5362 break;
5363 case CDRF_SKIPDEFAULT:
5364 return TRUE;
5365 default:
5366 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5367 infoPtr->hwndSelf, ntfret);
5371 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5372 * to my parent for processing.
5374 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5375 POINT pt, ptorig;
5376 HDC hdc = (HDC)wParam;
5377 HWND parent;
5379 pt.x = 0;
5380 pt.y = 0;
5381 parent = GetParent(infoPtr->hwndSelf);
5382 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5383 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5384 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5385 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5387 if (!ret)
5388 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5390 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5391 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5392 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5393 tbcd.nmcd.hdc = (HDC)wParam;
5394 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5395 dwEraseCustDraw = ntfret & 0xffff;
5396 switch (dwEraseCustDraw)
5398 case CDRF_DODEFAULT:
5399 break;
5400 case CDRF_SKIPDEFAULT:
5401 return TRUE;
5402 default:
5403 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5404 infoPtr->hwndSelf, ntfret);
5407 return ret;
5411 static inline LRESULT
5412 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5414 return (LRESULT)infoPtr->hFont;
5418 static void
5419 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5421 INT i;
5422 INT nNewHotItem = infoPtr->nHotItem;
5424 for (i = 0; i < infoPtr->nNumButtons; i++)
5426 /* did we wrap? */
5427 if ((nNewHotItem + iDirection < 0) ||
5428 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5430 NMTBWRAPHOTITEM nmtbwhi;
5431 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5432 nmtbwhi.iDirection = iDirection;
5433 nmtbwhi.dwReason = dwReason;
5435 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5436 return;
5439 nNewHotItem += iDirection;
5440 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5442 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5443 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5445 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5446 break;
5451 static LRESULT
5452 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5454 NMKEY nmkey;
5456 nmkey.nVKey = (UINT)wParam;
5457 nmkey.uFlags = HIWORD(lParam);
5459 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5460 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5462 switch ((UINT)wParam)
5464 case VK_LEFT:
5465 case VK_UP:
5466 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5467 break;
5468 case VK_RIGHT:
5469 case VK_DOWN:
5470 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5471 break;
5472 case VK_SPACE:
5473 case VK_RETURN:
5474 if ((infoPtr->nHotItem >= 0) &&
5475 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5477 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5478 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5479 (LPARAM)infoPtr->hwndSelf);
5481 break;
5484 return 0;
5488 static LRESULT
5489 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5491 POINT pt;
5492 BOOL button;
5494 pt.x = (short)LOWORD(lParam);
5495 pt.y = (short)HIWORD(lParam);
5496 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5498 if (button)
5499 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5500 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5501 TOOLBAR_Customize (infoPtr);
5503 return 0;
5507 static LRESULT
5508 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5510 TBUTTON_INFO *btnPtr;
5511 POINT pt;
5512 INT nHit;
5513 NMTOOLBARA nmtb;
5514 NMMOUSE nmmouse;
5515 BOOL bDragKeyPressed;
5516 BOOL button;
5518 TRACE("\n");
5520 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5521 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5522 else
5523 bDragKeyPressed = (wParam & MK_SHIFT);
5525 if (infoPtr->hwndToolTip)
5526 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5527 WM_LBUTTONDOWN, wParam, lParam);
5529 pt.x = (short)LOWORD(lParam);
5530 pt.y = (short)HIWORD(lParam);
5531 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5533 if (button)
5535 btnPtr = &infoPtr->buttons[nHit];
5537 if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5539 infoPtr->nButtonDrag = nHit;
5540 SetCapture (infoPtr->hwndSelf);
5542 /* If drag cursor has not been loaded, load it.
5543 * Note: it doesn't need to be freed */
5544 if (!hCursorDrag)
5545 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5546 SetCursor(hCursorDrag);
5548 else
5550 RECT arrowRect;
5551 infoPtr->nOldHit = nHit;
5553 CopyRect(&arrowRect, &btnPtr->rect);
5554 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5556 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5557 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5558 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5559 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5560 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5561 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5563 LRESULT res;
5565 /* draw in pressed state */
5566 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5567 btnPtr->fsState |= TBSTATE_PRESSED;
5568 else
5569 btnPtr->bDropDownPressed = TRUE;
5570 RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5572 memset(&nmtb, 0, sizeof(nmtb));
5573 nmtb.iItem = btnPtr->idCommand;
5574 nmtb.rcButton = btnPtr->rect;
5575 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5576 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5578 if (res != TBDDRET_TREATPRESSED)
5580 MSG msg;
5582 /* redraw button in unpressed state */
5583 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5584 btnPtr->fsState &= ~TBSTATE_PRESSED;
5585 else
5586 btnPtr->bDropDownPressed = FALSE;
5587 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5589 /* find and set hot item */
5590 GetCursorPos(&pt);
5591 ScreenToClient(infoPtr->hwndSelf, &pt);
5592 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5593 if (!infoPtr->bAnchor || button)
5594 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5596 /* remove any left mouse button down or double-click messages
5597 * so that we can get a toggle effect on the button */
5598 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5599 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5602 return 0;
5604 /* otherwise drop through and process as pushed */
5606 infoPtr->bCaptured = TRUE;
5607 infoPtr->nButtonDown = nHit;
5608 infoPtr->bDragOutSent = FALSE;
5610 btnPtr->fsState |= TBSTATE_PRESSED;
5612 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5614 if (btnPtr->fsState & TBSTATE_ENABLED)
5615 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5616 UpdateWindow(infoPtr->hwndSelf);
5617 SetCapture (infoPtr->hwndSelf);
5620 memset(&nmtb, 0, sizeof(nmtb));
5621 nmtb.iItem = btnPtr->idCommand;
5622 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5625 nmmouse.dwHitInfo = nHit;
5627 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5628 if (!button)
5629 nmmouse.dwItemSpec = -1;
5630 else
5632 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5633 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5636 ClientToScreen(infoPtr->hwndSelf, &pt);
5637 nmmouse.pt = pt;
5639 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5640 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5642 return 0;
5645 static LRESULT
5646 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5648 TBUTTON_INFO *btnPtr;
5649 POINT pt;
5650 INT nHit;
5651 INT nOldIndex = -1;
5652 NMHDR hdr;
5653 NMMOUSE nmmouse;
5654 NMTOOLBARA nmtb;
5655 BOOL button;
5657 if (infoPtr->hwndToolTip)
5658 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5659 WM_LBUTTONUP, wParam, lParam);
5661 pt.x = (short)LOWORD(lParam);
5662 pt.y = (short)HIWORD(lParam);
5663 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5665 if (!infoPtr->bAnchor || button)
5666 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5668 if (infoPtr->nButtonDrag >= 0) {
5669 RECT rcClient;
5670 NMHDR hdr;
5672 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5673 ReleaseCapture();
5674 /* reset cursor */
5675 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5677 GetClientRect(infoPtr->hwndSelf, &rcClient);
5678 if (PtInRect(&rcClient, pt))
5680 INT nButton = -1;
5681 if (nHit >= 0)
5682 nButton = nHit;
5683 else if (nHit < -1)
5684 nButton = -nHit;
5685 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5686 nButton = -nHit;
5688 if (nButton == infoPtr->nButtonDrag)
5690 /* if the button is moved sightly left and we have a
5691 * separator there then remove it */
5692 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5694 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5695 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5697 else /* else insert a separator before the dragged button */
5699 TBBUTTON tbb;
5700 memset(&tbb, 0, sizeof(tbb));
5701 tbb.fsStyle = BTNS_SEP;
5702 tbb.iString = -1;
5703 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5706 else
5708 if (nButton == -1)
5710 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5711 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5712 else
5713 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5715 else
5716 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5719 else
5721 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5722 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5725 /* button under cursor changed so need to re-set hot item */
5726 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5727 infoPtr->nButtonDrag = -1;
5729 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5731 else if (infoPtr->nButtonDown >= 0) {
5732 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5733 btnPtr->fsState &= ~TBSTATE_PRESSED;
5735 if (btnPtr->fsStyle & BTNS_CHECK) {
5736 if (btnPtr->fsStyle & BTNS_GROUP) {
5737 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5738 nHit);
5739 if ((nOldIndex != nHit) &&
5740 (nOldIndex != -1))
5741 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5742 btnPtr->fsState |= TBSTATE_CHECKED;
5744 else {
5745 if (btnPtr->fsState & TBSTATE_CHECKED)
5746 btnPtr->fsState &= ~TBSTATE_CHECKED;
5747 else
5748 btnPtr->fsState |= TBSTATE_CHECKED;
5752 if (nOldIndex != -1)
5753 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5756 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5757 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5758 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5760 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5761 ReleaseCapture ();
5762 infoPtr->nButtonDown = -1;
5764 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5765 TOOLBAR_SendNotify (&hdr, infoPtr,
5766 NM_RELEASEDCAPTURE);
5768 memset(&nmtb, 0, sizeof(nmtb));
5769 nmtb.iItem = btnPtr->idCommand;
5770 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5771 TBN_ENDDRAG);
5773 if (btnPtr->fsState & TBSTATE_ENABLED)
5775 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5776 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5778 /* In case we have just been destroyed... */
5779 if(!IsWindow(infoPtr->hwndSelf))
5780 return 0;
5784 /* !!! Undocumented - toolbar at 4.71 level and above sends
5785 * NM_CLICK with the NMMOUSE structure. */
5786 nmmouse.dwHitInfo = nHit;
5788 if (!button)
5789 nmmouse.dwItemSpec = -1;
5790 else
5792 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5793 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5796 ClientToScreen(infoPtr->hwndSelf, &pt);
5797 nmmouse.pt = pt;
5799 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5800 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5802 return 0;
5805 static LRESULT
5806 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5808 INT nHit;
5809 NMMOUSE nmmouse;
5810 POINT pt;
5811 BOOL button;
5813 pt.x = (short)LOWORD(lParam);
5814 pt.y = (short)HIWORD(lParam);
5816 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5817 nmmouse.dwHitInfo = nHit;
5819 if (!button) {
5820 nmmouse.dwItemSpec = -1;
5821 } else {
5822 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5823 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5826 ClientToScreen(infoPtr->hwndSelf, &pt);
5827 nmmouse.pt = pt;
5829 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5830 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5832 return 0;
5835 static LRESULT
5836 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5838 NMHDR nmhdr;
5840 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5841 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5843 return 0;
5846 static LRESULT
5847 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5849 TBUTTON_INFO *btnPtr;
5851 infoPtr->bCaptured = FALSE;
5853 if (infoPtr->nButtonDown >= 0)
5855 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5856 btnPtr->fsState &= ~TBSTATE_PRESSED;
5858 infoPtr->nOldHit = -1;
5860 if (btnPtr->fsState & TBSTATE_ENABLED)
5861 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5863 return 0;
5866 static LRESULT
5867 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5869 /* don't remove hot effects when in anchor highlighting mode or when a
5870 * drop-down button is pressed */
5871 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5873 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5874 if (!hotBtnPtr->bDropDownPressed)
5875 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5878 if (infoPtr->nOldHit < 0)
5879 return TRUE;
5881 /* If the last button we were over is depressed then make it not */
5882 /* depressed and redraw it */
5883 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5885 TBUTTON_INFO *btnPtr;
5886 RECT rc1;
5888 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5890 btnPtr->fsState &= ~TBSTATE_PRESSED;
5892 rc1 = btnPtr->rect;
5893 InflateRect (&rc1, 1, 1);
5894 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5897 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5899 NMTOOLBARW nmt;
5900 ZeroMemory(&nmt, sizeof(nmt));
5901 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5902 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5903 infoPtr->bDragOutSent = TRUE;
5906 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5908 return TRUE;
5911 static LRESULT
5912 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5914 POINT pt;
5915 TRACKMOUSEEVENT trackinfo;
5916 INT nHit;
5917 TBUTTON_INFO *btnPtr;
5918 BOOL button;
5920 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5921 TOOLBAR_TooltipCreateControl(infoPtr);
5923 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5924 /* fill in the TRACKMOUSEEVENT struct */
5925 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5926 trackinfo.dwFlags = TME_QUERY;
5928 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5929 _TrackMouseEvent(&trackinfo);
5931 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5932 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5933 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5934 trackinfo.hwndTrack = infoPtr->hwndSelf;
5936 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5937 /* and can properly deactivate the hot toolbar button */
5938 _TrackMouseEvent(&trackinfo);
5942 if (infoPtr->hwndToolTip)
5943 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5944 WM_MOUSEMOVE, wParam, lParam);
5946 pt.x = (short)LOWORD(lParam);
5947 pt.y = (short)HIWORD(lParam);
5949 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5951 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5952 && (!infoPtr->bAnchor || button))
5953 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5955 if (infoPtr->nOldHit != nHit)
5957 if (infoPtr->bCaptured)
5959 if (!infoPtr->bDragOutSent)
5961 NMTOOLBARW nmt;
5962 ZeroMemory(&nmt, sizeof(nmt));
5963 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5964 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5965 infoPtr->bDragOutSent = TRUE;
5968 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5969 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5970 btnPtr->fsState &= ~TBSTATE_PRESSED;
5971 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5973 else if (nHit == infoPtr->nButtonDown) {
5974 btnPtr->fsState |= TBSTATE_PRESSED;
5975 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5977 infoPtr->nOldHit = nHit;
5981 return 0;
5985 static inline LRESULT
5986 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5988 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
5989 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5990 /* else */
5991 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5995 static inline LRESULT
5996 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
5998 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
5999 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6001 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6005 static LRESULT
6006 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
6008 TOOLBAR_INFO *infoPtr;
6009 DWORD styleadd = 0;
6011 /* allocate memory for info structure */
6012 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6013 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6015 /* paranoid!! */
6016 infoPtr->dwStructSize = sizeof(TBBUTTON);
6017 infoPtr->nRows = 1;
6019 /* initialize info structure */
6020 infoPtr->nButtonWidth = 23;
6021 infoPtr->nButtonHeight = 22;
6022 infoPtr->nBitmapHeight = 16;
6023 infoPtr->nBitmapWidth = 16;
6025 infoPtr->nMaxTextRows = 1;
6026 infoPtr->cxMin = -1;
6027 infoPtr->cxMax = -1;
6028 infoPtr->nNumBitmaps = 0;
6029 infoPtr->nNumStrings = 0;
6031 infoPtr->bCaptured = FALSE;
6032 infoPtr->nButtonDown = -1;
6033 infoPtr->nButtonDrag = -1;
6034 infoPtr->nOldHit = -1;
6035 infoPtr->nHotItem = -1;
6036 infoPtr->hwndNotify = lpcs->hwndParent;
6037 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6038 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
6039 infoPtr->bDragOutSent = FALSE;
6040 infoPtr->iVersion = 0;
6041 infoPtr->hwndSelf = hwnd;
6042 infoPtr->bDoRedraw = TRUE;
6043 infoPtr->clrBtnHighlight = CLR_DEFAULT;
6044 infoPtr->clrBtnShadow = CLR_DEFAULT;
6045 infoPtr->szPadding.cx = DEFPAD_CX;
6046 infoPtr->szPadding.cy = DEFPAD_CY;
6047 infoPtr->iListGap = DEFLISTGAP;
6048 infoPtr->iTopMargin = default_top_margin(infoPtr);
6049 infoPtr->dwStyle = lpcs->style;
6050 infoPtr->tbim.iButton = -1;
6052 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6053 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6054 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6055 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6058 /* I think the code below is a bug, but it is the way that the native
6059 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6060 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6061 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6062 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6063 * Somehow, the only cases of this seem to be MFC programs.
6065 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6066 * does not occur in the WM_STYLECHANGING routine.
6067 * (Guy Albertelli 9/2001)
6070 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
6071 && !(lpcs->style & TBSTYLE_TRANSPARENT))
6072 styleadd |= TBSTYLE_TRANSPARENT;
6073 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6074 styleadd |= CCS_TOP; /* default to top */
6075 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6078 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
6082 static LRESULT
6083 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6085 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6086 RECT rcWindow;
6087 HDC hdc;
6089 if (dwStyle & WS_MINIMIZE)
6090 return 0; /* Nothing to do */
6092 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6094 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6095 return 0;
6097 if (!(dwStyle & CCS_NODIVIDER))
6099 GetWindowRect (hwnd, &rcWindow);
6100 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6101 if( dwStyle & WS_BORDER )
6102 InflateRect (&rcWindow, -1, -1);
6103 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6106 ReleaseDC( hwnd, hdc );
6108 return 0;
6112 /* handles requests from the tooltip control on what text to display */
6113 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6115 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6117 TRACE("button index = %d\n", index);
6119 Free (infoPtr->pszTooltipText);
6120 infoPtr->pszTooltipText = NULL;
6122 if (index < 0)
6123 return 0;
6125 if (infoPtr->bUnicode)
6127 WCHAR wszBuffer[INFOTIPSIZE+1];
6128 NMTBGETINFOTIPW tbgit;
6129 unsigned int len; /* in chars */
6131 wszBuffer[0] = '\0';
6132 wszBuffer[INFOTIPSIZE] = '\0';
6134 tbgit.pszText = wszBuffer;
6135 tbgit.cchTextMax = INFOTIPSIZE;
6136 tbgit.iItem = lpnmtdi->hdr.idFrom;
6137 tbgit.lParam = infoPtr->buttons[index].dwData;
6139 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6141 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6143 len = strlenW(tbgit.pszText);
6144 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6146 /* need to allocate temporary buffer in infoPtr as there
6147 * isn't enough space in buffer passed to us by the
6148 * tooltip control */
6149 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6150 if (infoPtr->pszTooltipText)
6152 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6153 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6154 return 0;
6157 else if (len > 0)
6159 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6160 return 0;
6163 else
6165 CHAR szBuffer[INFOTIPSIZE+1];
6166 NMTBGETINFOTIPA tbgit;
6167 unsigned int len; /* in chars */
6169 szBuffer[0] = '\0';
6170 szBuffer[INFOTIPSIZE] = '\0';
6172 tbgit.pszText = szBuffer;
6173 tbgit.cchTextMax = INFOTIPSIZE;
6174 tbgit.iItem = lpnmtdi->hdr.idFrom;
6175 tbgit.lParam = infoPtr->buttons[index].dwData;
6177 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6179 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6181 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6182 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6184 /* need to allocate temporary buffer in infoPtr as there
6185 * isn't enough space in buffer passed to us by the
6186 * tooltip control */
6187 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6188 if (infoPtr->pszTooltipText)
6190 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6191 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6192 return 0;
6195 else if (tbgit.pszText && tbgit.pszText[0])
6197 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6198 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6199 return 0;
6203 /* if button has text, but it is not shown then automatically
6204 * use that text as tooltip */
6205 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6206 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6208 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6209 unsigned int len = pszText ? strlenW(pszText) : 0;
6211 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6213 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6215 /* need to allocate temporary buffer in infoPtr as there
6216 * isn't enough space in buffer passed to us by the
6217 * tooltip control */
6218 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6219 if (infoPtr->pszTooltipText)
6221 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6222 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6223 return 0;
6226 else if (len > 0)
6228 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6229 return 0;
6233 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6235 /* last resort: send notification on to app */
6236 /* FIXME: find out what is really used here */
6237 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6241 static inline LRESULT
6242 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6244 switch (lpnmh->code)
6246 case PGN_CALCSIZE:
6248 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6250 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6251 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6252 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6253 lppgc->iWidth);
6255 else {
6256 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6257 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6258 lppgc->iHeight);
6260 return 0;
6263 case PGN_SCROLL:
6265 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6267 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6268 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6269 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6270 lppgs->iScroll, lppgs->iDir);
6271 return 0;
6274 case TTN_GETDISPINFOW:
6275 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6277 case TTN_GETDISPINFOA:
6278 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6279 return 0;
6281 default:
6282 return 0;
6287 static LRESULT
6288 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6290 LRESULT format;
6292 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6294 if (lParam == NF_QUERY)
6295 return NFR_UNICODE;
6297 if (lParam == NF_REQUERY) {
6298 format = SendMessageW(infoPtr->hwndNotify,
6299 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6300 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6301 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6302 format);
6303 format = NFR_ANSI;
6305 return format;
6307 return 0;
6311 static LRESULT
6312 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6314 HDC hdc;
6315 PAINTSTRUCT ps;
6317 /* fill ps.rcPaint with a default rect */
6318 ps.rcPaint = infoPtr->rcBound;
6320 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6322 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6324 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6325 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6327 return 0;
6331 static LRESULT
6332 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6334 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6336 /* make first item hot */
6337 if (infoPtr->nNumButtons > 0)
6338 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6340 return 0;
6343 static LRESULT
6344 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6346 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6348 if (hFont == 0)
6349 infoPtr->hFont = infoPtr->hDefaultFont;
6350 else
6351 infoPtr->hFont = hFont;
6353 TOOLBAR_CalcToolbar(infoPtr);
6355 if (Redraw)
6356 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6357 return 1;
6360 static LRESULT
6361 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6362 /*****************************************************
6364 * Function;
6365 * Handles the WM_SETREDRAW message.
6367 * Documentation:
6368 * According to testing V4.71 of COMCTL32 returns the
6369 * *previous* status of the redraw flag (either 0 or 1)
6370 * instead of the MSDN documented value of 0 if handled.
6371 * (For laughs see the "consistency" with same function
6372 * in rebar.)
6374 *****************************************************/
6376 BOOL oldredraw = infoPtr->bDoRedraw;
6378 TRACE("set to %s\n",
6379 (wParam) ? "TRUE" : "FALSE");
6380 infoPtr->bDoRedraw = (BOOL) wParam;
6381 if (wParam) {
6382 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6384 return (oldredraw) ? 1 : 0;
6388 static LRESULT
6389 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6391 TRACE("sizing toolbar!\n");
6393 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6395 RECT delta_width, delta_height, client, dummy;
6396 DWORD min_x, max_x, min_y, max_y;
6397 TBUTTON_INFO *btnPtr;
6398 INT i;
6400 GetClientRect(infoPtr->hwndSelf, &client);
6401 if(client.right > infoPtr->client_rect.right)
6403 min_x = infoPtr->client_rect.right;
6404 max_x = client.right;
6406 else
6408 max_x = infoPtr->client_rect.right;
6409 min_x = client.right;
6411 if(client.bottom > infoPtr->client_rect.bottom)
6413 min_y = infoPtr->client_rect.bottom;
6414 max_y = client.bottom;
6416 else
6418 max_y = infoPtr->client_rect.bottom;
6419 min_y = client.bottom;
6422 SetRect(&delta_width, min_x, 0, max_x, min_y);
6423 SetRect(&delta_height, 0, min_y, max_x, max_y);
6425 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6426 btnPtr = infoPtr->buttons;
6427 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6428 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6429 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6430 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6432 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6433 TOOLBAR_AutoSize(infoPtr);
6434 return 0;
6438 static LRESULT
6439 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6441 if (nType == GWL_STYLE)
6442 return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6444 return 0;
6448 static LRESULT
6449 TOOLBAR_SysColorChange (void)
6451 COMCTL32_RefreshSysColors();
6453 return 0;
6457 /* update theme after a WM_THEMECHANGED message */
6458 static LRESULT theme_changed (HWND hwnd)
6460 HTHEME theme = GetWindowTheme (hwnd);
6461 CloseThemeData (theme);
6462 OpenThemeData (hwnd, themeClass);
6463 return 0;
6467 static LRESULT WINAPI
6468 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6470 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6472 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6473 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6475 if (!infoPtr && (uMsg != WM_NCCREATE))
6476 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6478 switch (uMsg)
6480 case TB_ADDBITMAP:
6481 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6483 case TB_ADDBUTTONSA:
6484 case TB_ADDBUTTONSW:
6485 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6486 uMsg == TB_ADDBUTTONSW);
6487 case TB_ADDSTRINGA:
6488 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6490 case TB_ADDSTRINGW:
6491 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6493 case TB_AUTOSIZE:
6494 return TOOLBAR_AutoSize (infoPtr);
6496 case TB_BUTTONCOUNT:
6497 return TOOLBAR_ButtonCount (infoPtr);
6499 case TB_BUTTONSTRUCTSIZE:
6500 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6502 case TB_CHANGEBITMAP:
6503 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6505 case TB_CHECKBUTTON:
6506 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6508 case TB_COMMANDTOINDEX:
6509 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6511 case TB_CUSTOMIZE:
6512 return TOOLBAR_Customize (infoPtr);
6514 case TB_DELETEBUTTON:
6515 return TOOLBAR_DeleteButton (infoPtr, wParam);
6517 case TB_ENABLEBUTTON:
6518 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6520 case TB_GETANCHORHIGHLIGHT:
6521 return TOOLBAR_GetAnchorHighlight (infoPtr);
6523 case TB_GETBITMAP:
6524 return TOOLBAR_GetBitmap (infoPtr, wParam);
6526 case TB_GETBITMAPFLAGS:
6527 return TOOLBAR_GetBitmapFlags ();
6529 case TB_GETBUTTON:
6530 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6532 case TB_GETBUTTONINFOA:
6533 case TB_GETBUTTONINFOW:
6534 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6535 uMsg == TB_GETBUTTONINFOW);
6536 case TB_GETBUTTONSIZE:
6537 return TOOLBAR_GetButtonSize (infoPtr);
6539 case TB_GETBUTTONTEXTA:
6540 case TB_GETBUTTONTEXTW:
6541 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6542 uMsg == TB_GETBUTTONTEXTW);
6544 case TB_GETDISABLEDIMAGELIST:
6545 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6547 case TB_GETEXTENDEDSTYLE:
6548 return TOOLBAR_GetExtendedStyle (infoPtr);
6550 case TB_GETHOTIMAGELIST:
6551 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6553 case TB_GETHOTITEM:
6554 return TOOLBAR_GetHotItem (infoPtr);
6556 case TB_GETIMAGELIST:
6557 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6559 case TB_GETINSERTMARK:
6560 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6562 case TB_GETINSERTMARKCOLOR:
6563 return TOOLBAR_GetInsertMarkColor (infoPtr);
6565 case TB_GETITEMRECT:
6566 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6568 case TB_GETMAXSIZE:
6569 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6571 /* case TB_GETOBJECT: */ /* 4.71 */
6573 case TB_GETPADDING:
6574 return TOOLBAR_GetPadding (infoPtr);
6576 case TB_GETRECT:
6577 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6579 case TB_GETROWS:
6580 return TOOLBAR_GetRows (infoPtr);
6582 case TB_GETSTATE:
6583 return TOOLBAR_GetState (infoPtr, wParam);
6585 case TB_GETSTRINGA:
6586 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6588 case TB_GETSTRINGW:
6589 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6591 case TB_GETSTYLE:
6592 return TOOLBAR_GetStyle (infoPtr);
6594 case TB_GETTEXTROWS:
6595 return TOOLBAR_GetTextRows (infoPtr);
6597 case TB_GETTOOLTIPS:
6598 return TOOLBAR_GetToolTips (infoPtr);
6600 case TB_GETUNICODEFORMAT:
6601 return TOOLBAR_GetUnicodeFormat (infoPtr);
6603 case TB_HIDEBUTTON:
6604 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6606 case TB_HITTEST:
6607 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6609 case TB_INDETERMINATE:
6610 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6612 case TB_INSERTBUTTONA:
6613 case TB_INSERTBUTTONW:
6614 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6615 uMsg == TB_INSERTBUTTONW);
6617 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6619 case TB_ISBUTTONCHECKED:
6620 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6622 case TB_ISBUTTONENABLED:
6623 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6625 case TB_ISBUTTONHIDDEN:
6626 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6628 case TB_ISBUTTONHIGHLIGHTED:
6629 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6631 case TB_ISBUTTONINDETERMINATE:
6632 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6634 case TB_ISBUTTONPRESSED:
6635 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6637 case TB_LOADIMAGES:
6638 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6640 case TB_MAPACCELERATORA:
6641 case TB_MAPACCELERATORW:
6642 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6644 case TB_MARKBUTTON:
6645 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6647 case TB_MOVEBUTTON:
6648 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6650 case TB_PRESSBUTTON:
6651 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6653 case TB_REPLACEBITMAP:
6654 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6656 case TB_SAVERESTOREA:
6657 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6659 case TB_SAVERESTOREW:
6660 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6662 case TB_SETANCHORHIGHLIGHT:
6663 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6665 case TB_SETBITMAPSIZE:
6666 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6668 case TB_SETBUTTONINFOA:
6669 case TB_SETBUTTONINFOW:
6670 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6671 uMsg == TB_SETBUTTONINFOW);
6672 case TB_SETBUTTONSIZE:
6673 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6675 case TB_SETBUTTONWIDTH:
6676 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6678 case TB_SETCMDID:
6679 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6681 case TB_SETDISABLEDIMAGELIST:
6682 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6684 case TB_SETDRAWTEXTFLAGS:
6685 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6687 case TB_SETEXTENDEDSTYLE:
6688 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6690 case TB_SETHOTIMAGELIST:
6691 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6693 case TB_SETHOTITEM:
6694 return TOOLBAR_SetHotItem (infoPtr, wParam);
6696 case TB_SETIMAGELIST:
6697 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6699 case TB_SETINDENT:
6700 return TOOLBAR_SetIndent (infoPtr, wParam);
6702 case TB_SETINSERTMARK:
6703 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6705 case TB_SETINSERTMARKCOLOR:
6706 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6708 case TB_SETMAXTEXTROWS:
6709 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6711 case TB_SETPADDING:
6712 return TOOLBAR_SetPadding (infoPtr, lParam);
6714 case TB_SETPARENT:
6715 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6717 case TB_SETROWS:
6718 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6720 case TB_SETSTATE:
6721 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6723 case TB_SETSTYLE:
6724 return TOOLBAR_SetStyle (infoPtr, lParam);
6726 case TB_SETTOOLTIPS:
6727 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6729 case TB_SETUNICODEFORMAT:
6730 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6732 case TB_SETBOUNDINGSIZE:
6733 return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6735 case TB_SETHOTITEM2:
6736 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6738 case TB_SETLISTGAP:
6739 return TOOLBAR_SetListGap(infoPtr, wParam);
6741 case TB_GETIMAGELISTCOUNT:
6742 return TOOLBAR_GetImageListCount(infoPtr);
6744 case TB_GETIDEALSIZE:
6745 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6747 case TB_UNKWN464:
6748 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6750 /* Common Control Messages */
6752 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6753 case CCM_GETCOLORSCHEME:
6754 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6756 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6757 case CCM_SETCOLORSCHEME:
6758 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6760 case CCM_GETVERSION:
6761 return TOOLBAR_GetVersion (infoPtr);
6763 case CCM_SETVERSION:
6764 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6767 /* case WM_CHAR: */
6769 case WM_CREATE:
6770 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6772 case WM_DESTROY:
6773 return TOOLBAR_Destroy (infoPtr);
6775 case WM_ERASEBKGND:
6776 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6778 case WM_GETFONT:
6779 return TOOLBAR_GetFont (infoPtr);
6781 case WM_KEYDOWN:
6782 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6784 /* case WM_KILLFOCUS: */
6786 case WM_LBUTTONDBLCLK:
6787 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6789 case WM_LBUTTONDOWN:
6790 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6792 case WM_LBUTTONUP:
6793 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6795 case WM_RBUTTONUP:
6796 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6798 case WM_RBUTTONDBLCLK:
6799 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6801 case WM_MOUSEMOVE:
6802 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6804 case WM_MOUSELEAVE:
6805 return TOOLBAR_MouseLeave (infoPtr);
6807 case WM_CAPTURECHANGED:
6808 if (hwnd == (HWND)lParam) return 0;
6809 return TOOLBAR_CaptureChanged(infoPtr);
6811 case WM_NCACTIVATE:
6812 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6814 case WM_NCCALCSIZE:
6815 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6817 case WM_NCCREATE:
6818 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6820 case WM_NCPAINT:
6821 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6823 case WM_NOTIFY:
6824 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6826 case WM_NOTIFYFORMAT:
6827 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6829 case WM_PRINTCLIENT:
6830 case WM_PAINT:
6831 return TOOLBAR_Paint (infoPtr, wParam);
6833 case WM_SETFOCUS:
6834 return TOOLBAR_SetFocus (infoPtr);
6836 case WM_SETFONT:
6837 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6839 case WM_SETREDRAW:
6840 return TOOLBAR_SetRedraw (infoPtr, wParam);
6842 case WM_SIZE:
6843 return TOOLBAR_Size (infoPtr);
6845 case WM_STYLECHANGED:
6846 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6848 case WM_SYSCOLORCHANGE:
6849 return TOOLBAR_SysColorChange ();
6851 case WM_THEMECHANGED:
6852 return theme_changed (hwnd);
6854 /* case WM_WININICHANGE: */
6856 case WM_CHARTOITEM:
6857 case WM_COMMAND:
6858 case WM_DRAWITEM:
6859 case WM_MEASUREITEM:
6860 case WM_VKEYTOITEM:
6861 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6863 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6864 case PGM_FORWARDMOUSE:
6865 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6867 default:
6868 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6869 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6870 uMsg, wParam, lParam);
6871 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6876 VOID
6877 TOOLBAR_Register (void)
6879 WNDCLASSW wndClass;
6881 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6882 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6883 wndClass.lpfnWndProc = ToolbarWindowProc;
6884 wndClass.cbClsExtra = 0;
6885 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6886 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6887 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6888 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6890 RegisterClassW (&wndClass);
6894 VOID
6895 TOOLBAR_Unregister (void)
6897 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6900 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6902 HIMAGELIST himlold;
6903 PIMLENTRY c = NULL;
6905 /* Check if the entry already exists */
6906 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6908 /* If this is a new entry we must create it and insert into the array */
6909 if (!c)
6911 PIMLENTRY *pnies;
6913 c = Alloc(sizeof(IMLENTRY));
6914 c->id = id;
6916 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6917 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
6918 pnies[*cies] = c;
6919 (*cies)++;
6921 Free(*pies);
6922 *pies = pnies;
6925 himlold = c->himl;
6926 c->himl = himl;
6928 return himlold;
6932 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
6934 int i;
6936 for (i = 0; i < *cies; i++)
6937 Free((*pies)[i]);
6939 Free(*pies);
6941 *cies = 0;
6942 *pies = NULL;
6946 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6948 PIMLENTRY c = NULL;
6950 if (pies != NULL)
6952 int i;
6954 for (i = 0; i < cies; i++)
6956 if (pies[i]->id == id)
6958 c = pies[i];
6959 break;
6964 return c;
6968 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6970 HIMAGELIST himlDef = 0;
6971 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6973 if (pie)
6974 himlDef = pie->himl;
6976 return himlDef;
6980 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6982 if (infoPtr->bUnicode)
6983 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6984 else
6986 CHAR Buffer[256];
6987 NMTOOLBARA nmtba;
6988 BOOL bRet = FALSE;
6990 nmtba.iItem = nmtb->iItem;
6991 nmtba.pszText = Buffer;
6992 nmtba.cchText = 256;
6993 ZeroMemory(nmtba.pszText, nmtba.cchText);
6995 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
6997 int ccht = strlen(nmtba.pszText);
6998 if (ccht)
6999 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7000 nmtb->pszText, nmtb->cchText);
7002 nmtb->tbButton = nmtba.tbButton;
7003 bRet = TRUE;
7006 return bRet;
7011 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7013 NMTOOLBARW nmtb;
7015 /* MSDN states that iItem is the index of the button, rather than the
7016 * command ID as used by every other NMTOOLBAR notification */
7017 nmtb.iItem = iItem;
7018 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7020 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);