user32: Pass the text length explicitly to EDIT_EM_ReplaceSel.
[wine.git] / dlls / comctl32 / toolbar.c
blob8a035979d2899ed756df509766fb61b519ecd090
1 /*
2 * Toolbar control
4 * Copyright 1998,1999 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2004 Robert Shearman
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTES
24 * This code was audited for completeness against the documented features
25 * of Comctl32.dll version 6.0 on Mar. 14, 2004, by Robert Shearman.
27 * Unless otherwise noted, we believe this code to be complete, as per
28 * the specification mentioned above.
29 * If you discover missing features or bugs please note them below.
31 * TODO:
32 * - Styles:
33 * - TBSTYLE_REGISTERDROP
34 * - TBSTYLE_EX_DOUBLEBUFFER
35 * - Messages:
36 * - TB_GETMETRICS
37 * - TB_GETOBJECT
38 * - TB_INSERTMARKHITTEST
39 * - TB_SAVERESTORE
40 * - TB_SETMETRICS
41 * - WM_WININICHANGE
42 * - Notifications:
43 * - NM_CHAR
44 * - TBN_GETOBJECT
45 * - TBN_SAVE
46 * - Button wrapping (under construction).
47 * - Fix TB_SETROWS and Separators.
48 * - iListGap custom draw support.
50 * Testing:
51 * - Run tests using Waite Group Windows95 API Bible Volume 2.
52 * The second cdrom contains executables addstr.exe, btncount.exe,
53 * btnstate.exe, butstrsz.exe, chkbtn.exe, chngbmp.exe, customiz.exe,
54 * enablebtn.exe, getbmp.exe, getbtn.exe, getflags.exe, hidebtn.exe,
55 * indetbtn.exe, insbtn.exe, pressbtn.exe, setbtnsz.exe, setcmdid.exe,
56 * setparnt.exe, setrows.exe, toolwnd.exe.
57 * - Microsoft's controlspy examples.
58 * - Charles Petzold's 'Programming Windows': gadgets.exe
60 * Differences between MSDN and actual native control operation:
61 * 1. MSDN says: "TBSTYLE_LIST: Creates a flat toolbar with button text
62 * to the right of the bitmap. Otherwise, this style is
63 * identical to TBSTYLE_FLAT."
64 * As implemented by both v4.71 and v5.80 of the native COMCTL32.DLL
65 * you can create a TBSTYLE_LIST without TBSTYLE_FLAT and the result
66 * is non-flat non-transparent buttons. Therefore TBSTYLE_LIST does
67 * *not* imply TBSTYLE_FLAT as documented. (GA 8/2001)
71 #include <stdarg.h>
72 #include <string.h>
74 #include "windef.h"
75 #include "winbase.h"
76 #include "winreg.h"
77 #include "wingdi.h"
78 #include "winuser.h"
79 #include "wine/unicode.h"
80 #include "winnls.h"
81 #include "commctrl.h"
82 #include "comctl32.h"
83 #include "uxtheme.h"
84 #include "vssym32.h"
85 #include "wine/debug.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
89 static HCURSOR hCursorDrag = NULL;
91 typedef struct
93 INT iBitmap;
94 INT idCommand;
95 BYTE fsState;
96 BYTE fsStyle;
97 BOOL bHot;
98 BOOL bDropDownPressed;
99 DWORD_PTR dwData;
100 INT_PTR iString;
101 INT nRow;
102 RECT rect;
103 INT cx; /* manually set size */
104 } TBUTTON_INFO;
106 typedef struct
108 UINT nButtons;
109 HINSTANCE hInst;
110 UINT nID;
111 } TBITMAP_INFO;
113 typedef struct
115 HIMAGELIST himl;
116 INT id;
117 } IMLENTRY, *PIMLENTRY;
119 typedef struct
121 DWORD dwStructSize; /* size of TBBUTTON struct */
122 RECT client_rect;
123 RECT rcBound; /* bounding rectangle */
124 INT nButtonHeight;
125 INT nButtonWidth;
126 INT nBitmapHeight;
127 INT nBitmapWidth;
128 INT nIndent;
129 INT nRows; /* number of button rows */
130 INT nMaxTextRows; /* maximum number of text rows */
131 INT cxMin; /* minimum button width */
132 INT cxMax; /* maximum button width */
133 INT nNumButtons; /* number of buttons */
134 INT nNumBitmaps; /* number of bitmaps */
135 INT nNumStrings; /* number of strings */
136 INT nNumBitmapInfos;
137 INT nButtonDown; /* toolbar button being pressed or -1 if none */
138 INT nButtonDrag; /* toolbar button being dragged or -1 if none */
139 INT nOldHit;
140 INT nHotItem; /* index of the "hot" item */
141 SIZE szPadding; /* padding values around button */
142 INT iTopMargin; /* the top margin */
143 INT iListGap; /* default gap between text and image for toolbar with list style */
144 HFONT hDefaultFont;
145 HFONT hFont; /* text font */
146 HIMAGELIST himlInt; /* image list created internally */
147 PIMLENTRY *himlDef; /* default image list array */
148 INT cimlDef; /* default image list array count */
149 PIMLENTRY *himlHot; /* hot image list array */
150 INT cimlHot; /* hot image list array count */
151 PIMLENTRY *himlDis; /* disabled image list array */
152 INT cimlDis; /* disabled image list array count */
153 HWND hwndToolTip; /* handle to tool tip control */
154 HWND hwndNotify; /* handle to the window that gets notifications */
155 HWND hwndSelf; /* my own handle */
156 BOOL bAnchor; /* anchor highlight enabled */
157 BOOL bDoRedraw; /* Redraw status */
158 BOOL bDragOutSent; /* has TBN_DRAGOUT notification been sent for this drag? */
159 BOOL bUnicode; /* Notifications are ASCII (FALSE) or Unicode (TRUE)? */
160 BOOL bCaptured; /* mouse captured? */
161 DWORD dwStyle; /* regular toolbar style */
162 DWORD dwExStyle; /* extended toolbar style */
163 DWORD dwDTFlags; /* DrawText flags */
165 COLORREF clrInsertMark; /* insert mark color */
166 COLORREF clrBtnHighlight; /* color for Flat Separator */
167 COLORREF clrBtnShadow; /* color for Flag Separator */
168 INT iVersion;
169 LPWSTR pszTooltipText; /* temporary store for a string > 80 characters
170 * for TTN_GETDISPINFOW notification */
171 TBINSERTMARK tbim; /* info on insertion mark */
172 TBUTTON_INFO *buttons; /* pointer to button array */
173 LPWSTR *strings; /* pointer to string array */
174 TBITMAP_INFO *bitmaps;
175 } TOOLBAR_INFO, *PTOOLBAR_INFO;
178 /* used by customization dialog */
179 typedef struct
181 PTOOLBAR_INFO tbInfo;
182 HWND tbHwnd;
183 } CUSTDLG_INFO, *PCUSTDLG_INFO;
185 typedef struct
187 TBBUTTON btn;
188 BOOL bVirtual;
189 BOOL bRemovable;
190 WCHAR text[64];
191 } CUSTOMBUTTON, *PCUSTOMBUTTON;
193 typedef enum
195 IMAGE_LIST_DEFAULT,
196 IMAGE_LIST_HOT,
197 IMAGE_LIST_DISABLED
198 } IMAGE_LIST_TYPE;
200 #define SEPARATOR_WIDTH 8
201 #define TOP_BORDER 2
202 #define BOTTOM_BORDER 2
203 #define DDARROW_WIDTH 11
204 #define ARROW_HEIGHT 3
205 #define INSERTMARK_WIDTH 2
207 #define DEFPAD_CX 7
208 #define DEFPAD_CY 6
209 #define DEFLISTGAP 4
211 /* vertical padding used in list mode when image is present */
212 #define LISTPAD_CY 9
214 /* how wide to treat the bitmap if it isn't present */
215 #define NONLIST_NOTEXT_OFFSET 2
217 #define TOOLBAR_NOWHERE (-1)
219 /* Used to find undocumented extended styles */
220 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
221 TBSTYLE_EX_VERTICAL | \
222 TBSTYLE_EX_MIXEDBUTTONS | \
223 TBSTYLE_EX_DOUBLEBUFFER | \
224 TBSTYLE_EX_HIDECLIPPEDBUTTONS)
226 /* all of the CCS_ styles */
227 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
228 CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
230 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
231 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
232 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
233 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
234 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
236 static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };
238 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
239 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
240 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
241 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
242 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
243 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
244 static LRESULT TOOLBAR_LButtonDown(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
245 static void TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr);
246 static LRESULT TOOLBAR_AutoSize(TOOLBAR_INFO *infoPtr);
247 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
248 static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
249 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
250 static LRESULT TOOLBAR_SetButtonInfo(TOOLBAR_INFO *infoPtr, INT Id,
251 const TBBUTTONINFOW *lptbbi, BOOL isW);
254 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
256 return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
259 static inline BOOL TOOLBAR_HasDropDownArrows(DWORD exStyle)
261 return (exStyle & TBSTYLE_EX_DRAWDDARROWS) != 0;
264 static inline BOOL button_has_ddarrow(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
266 return (TOOLBAR_HasDropDownArrows( infoPtr->dwExStyle ) && (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
267 (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
270 static LPWSTR
271 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
273 LPWSTR lpText = NULL;
275 /* NOTE: iString == -1 is undocumented */
276 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
277 lpText = (LPWSTR)btnPtr->iString;
278 else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
279 lpText = infoPtr->strings[btnPtr->iString];
281 return lpText;
284 static void
285 TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
287 TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx (%s)\n",
288 tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
289 (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
292 static void
293 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
295 if (TRACE_ON(toolbar)){
296 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
297 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
298 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
299 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
300 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
301 btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
302 wine_dbgstr_rect(&bP->rect));
307 static void
308 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
310 if (TRACE_ON(toolbar)) {
311 INT i;
313 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
314 iP->hwndSelf, line,
315 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
316 iP->nNumStrings, iP->dwStyle);
317 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
318 iP->hwndSelf, line,
319 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
320 (iP->bDoRedraw) ? "TRUE" : "FALSE");
321 for(i=0; i<iP->nNumButtons; i++) {
322 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
327 static inline BOOL
328 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
330 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
333 static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
335 if (!IS_INTRESOURCE( str ) && str != -1)
337 if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;
339 if (unicode)
340 Str_SetPtrW( (WCHAR **)&btn->iString, (WCHAR *)str );
341 else
342 Str_SetPtrAtoW( (WCHAR **)&btn->iString, (char *)str );
344 else
346 if (TOOLBAR_ButtonHasString( btn )) Free( (WCHAR *)btn->iString );
348 btn->iString = str;
352 static void set_stringT( TBUTTON_INFO *btn, const WCHAR *str, BOOL unicode )
354 if (IS_INTRESOURCE( (DWORD_PTR)str ) || (DWORD_PTR)str == -1) return;
355 set_string_index( btn, (DWORD_PTR)str, unicode );
358 static void free_string( TBUTTON_INFO *btn )
360 set_string_index( btn, 0, TRUE );
364 /***********************************************************************
365 * TOOLBAR_CheckStyle
367 * This function validates that the styles set are implemented and
368 * issues FIXMEs warning of possible problems. In a perfect world this
369 * function should be null.
371 static void
372 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
374 if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
375 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
379 static INT
380 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
382 if(!IsWindow(infoPtr->hwndSelf))
383 return 0; /* we have just been destroyed */
385 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
386 nmhdr->hwndFrom = infoPtr->hwndSelf;
387 nmhdr->code = code;
389 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
390 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
392 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
395 /***********************************************************************
396 * TOOLBAR_GetBitmapIndex
398 * This function returns the bitmap index associated with a button.
399 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
400 * is issued to retrieve the index.
402 static INT
403 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
405 INT ret = btnPtr->iBitmap;
407 if (ret == I_IMAGECALLBACK)
409 /* issue TBN_GETDISPINFO */
410 NMTBDISPINFOW nmgd;
412 memset(&nmgd, 0, sizeof(nmgd));
413 nmgd.idCommand = btnPtr->idCommand;
414 nmgd.lParam = btnPtr->dwData;
415 nmgd.dwMask = TBNF_IMAGE;
416 nmgd.iImage = -1;
417 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
418 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
419 if (nmgd.dwMask & TBNF_DI_SETITEM)
420 btnPtr->iBitmap = nmgd.iImage;
421 ret = nmgd.iImage;
422 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
423 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
426 if (ret != I_IMAGENONE)
427 ret = GETIBITMAP(infoPtr, ret);
429 return ret;
433 static BOOL
434 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
436 HIMAGELIST himl;
437 INT id = GETHIMLID(infoPtr, index);
438 INT iBitmap = GETIBITMAP(infoPtr, index);
440 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
441 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
442 (index == I_IMAGECALLBACK))
443 return TRUE;
444 else
445 return FALSE;
449 static inline BOOL
450 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
452 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
453 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
457 /***********************************************************************
458 * TOOLBAR_GetImageListForDrawing
460 * This function validates the bitmap index (including I_IMAGECALLBACK
461 * functionality) and returns the corresponding image list.
463 static HIMAGELIST
464 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
465 IMAGE_LIST_TYPE imagelist, INT * index)
467 HIMAGELIST himl;
469 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
470 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
471 WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
472 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
473 return NULL;
476 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
477 if ((*index == I_IMAGECALLBACK) ||
478 (*index == I_IMAGENONE)) return NULL;
479 ERR("TBN_GETDISPINFO returned invalid index %d\n",
480 *index);
481 return NULL;
484 switch(imagelist)
486 case IMAGE_LIST_DEFAULT:
487 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
488 break;
489 case IMAGE_LIST_HOT:
490 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
491 break;
492 case IMAGE_LIST_DISABLED:
493 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
494 break;
495 default:
496 himl = NULL;
497 FIXME("Shouldn't reach here\n");
500 if (!himl)
501 TRACE("no image list\n");
503 return himl;
507 static void
508 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
510 RECT myrect;
511 COLORREF oldcolor, newcolor;
513 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
514 myrect.right = myrect.left + 1;
515 myrect.top = lpRect->top + 2;
516 myrect.bottom = lpRect->bottom - 2;
518 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
519 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
520 oldcolor = SetBkColor (hdc, newcolor);
521 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
523 myrect.left = myrect.right;
524 myrect.right = myrect.left + 1;
526 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
527 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
528 SetBkColor (hdc, newcolor);
529 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
531 SetBkColor (hdc, oldcolor);
535 /***********************************************************************
536 * TOOLBAR_DrawFlatHorizontalSeparator
538 * This function draws horizontal separator for toolbars having CCS_VERT style.
539 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
540 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
541 * are horizontal as opposed to the vertical separators for not dropdown
542 * type.
544 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
546 static void
547 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
548 const TOOLBAR_INFO *infoPtr)
550 RECT myrect;
551 COLORREF oldcolor, newcolor;
553 myrect.left = lpRect->left;
554 myrect.right = lpRect->right;
555 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
556 myrect.bottom = myrect.top + 1;
558 InflateRect (&myrect, -2, 0);
560 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
562 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
563 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
564 oldcolor = SetBkColor (hdc, newcolor);
565 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
567 myrect.top = myrect.bottom;
568 myrect.bottom = myrect.top + 1;
570 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
571 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
572 SetBkColor (hdc, newcolor);
573 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
575 SetBkColor (hdc, oldcolor);
579 static void
580 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
582 INT x, y;
583 HPEN hPen, hOldPen;
585 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
586 hOldPen = SelectObject ( hdc, hPen );
587 x = left + 2;
588 y = top;
589 MoveToEx (hdc, x, y, NULL);
590 LineTo (hdc, x+5, y++); x++;
591 MoveToEx (hdc, x, y, NULL);
592 LineTo (hdc, x+3, y++); x++;
593 MoveToEx (hdc, x, y, NULL);
594 LineTo (hdc, x+1, y);
595 SelectObject( hdc, hOldPen );
596 DeleteObject( hPen );
600 * Draw the text string for this button.
601 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
602 * is non-zero, so we can simply check himlDef to see if we have
603 * an image list
605 static void
606 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
607 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
609 HDC hdc = tbcd->nmcd.hdc;
610 HFONT hOldFont = 0;
611 COLORREF clrOld = 0;
612 COLORREF clrOldBk = 0;
613 int oldBkMode = 0;
614 UINT state = tbcd->nmcd.uItemState;
616 /* draw text */
617 if (lpText && infoPtr->nMaxTextRows > 0) {
618 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
619 wine_dbgstr_rect(rcText));
621 hOldFont = SelectObject (hdc, infoPtr->hFont);
622 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
623 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
625 else if (state & CDIS_DISABLED) {
626 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
627 OffsetRect (rcText, 1, 1);
628 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
629 SetTextColor (hdc, comctl32_color.clr3dShadow);
630 OffsetRect (rcText, -1, -1);
632 else if (state & CDIS_INDETERMINATE) {
633 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
635 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
636 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
637 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
638 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
640 else {
641 clrOld = SetTextColor (hdc, tbcd->clrText);
644 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
645 SetTextColor (hdc, clrOld);
646 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
648 SetBkColor (hdc, clrOldBk);
649 SetBkMode (hdc, oldBkMode);
651 SelectObject (hdc, hOldFont);
656 static void
657 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
659 HDC hdc = tbcd->nmcd.hdc;
660 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
661 COLORREF clrTextOld;
662 COLORREF clrBkOld;
663 INT cx = lpRect->right - lpRect->left;
664 INT cy = lpRect->bottom - lpRect->top;
665 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
666 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
667 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
668 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
669 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
670 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
671 SetBkColor(hdc, clrBkOld);
672 SetTextColor(hdc, clrTextOld);
673 SelectObject (hdc, hbr);
677 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
679 INT cx, cy;
680 HBITMAP hbmMask, hbmImage;
681 HDC hdcMask, hdcImage;
683 ImageList_GetIconSize(himl, &cx, &cy);
685 /* Create src image */
686 hdcImage = CreateCompatibleDC(hdc);
687 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
688 SelectObject(hdcImage, hbmImage);
689 ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
690 RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
692 /* Create Mask */
693 hdcMask = CreateCompatibleDC(0);
694 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
695 SelectObject(hdcMask, hbmMask);
697 /* Remove the background and all white pixels */
698 ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
699 RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
700 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
701 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
703 /* draw the new mask 'etched' to hdc */
704 SetBkColor(hdc, RGB(255, 255, 255));
705 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
706 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
707 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
708 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
709 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
711 /* Cleanup */
712 DeleteObject(hbmImage);
713 DeleteDC(hdcImage);
714 DeleteObject (hbmMask);
715 DeleteDC(hdcMask);
719 static UINT
720 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
722 UINT retstate = 0;
724 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
725 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
726 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
727 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
728 retstate |= (btnPtr->bHot ) ? CDIS_HOT : 0;
729 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
730 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
731 return retstate;
734 /* draws the image on a toolbar button */
735 static void
736 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
737 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
739 HIMAGELIST himl = NULL;
740 BOOL draw_masked = FALSE;
741 INT index;
742 INT offset = 0;
743 UINT draw_flags = ILD_TRANSPARENT;
745 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
747 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
748 if (!himl)
750 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
751 draw_masked = TRUE;
754 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
755 ((tbcd->nmcd.uItemState & CDIS_HOT)
756 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
758 /* if hot, attempt to draw with hot image list, if fails,
759 use default image list */
760 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
761 if (!himl)
762 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
764 else
765 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
767 if (!himl)
768 return;
770 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
771 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
772 offset = 1;
774 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
775 (tbcd->nmcd.uItemState & CDIS_MARKED))
776 draw_flags |= ILD_BLEND50;
778 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
779 index, himl, left, top, offset);
781 if (draw_masked)
782 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
783 else
784 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
787 /* draws a blank frame for a toolbar button */
788 static void
789 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
791 HDC hdc = tbcd->nmcd.hdc;
792 RECT rc = *rect;
793 /* if the state is disabled or indeterminate then the button
794 * cannot have an interactive look like pressed or hot */
795 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
796 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
797 BOOL pressed_look = !non_interactive_state &&
798 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
799 (tbcd->nmcd.uItemState & CDIS_CHECKED));
801 /* app don't want us to draw any edges */
802 if (dwItemCDFlag & TBCDRF_NOEDGES)
803 return;
805 if (infoPtr->dwStyle & TBSTYLE_FLAT)
807 if (pressed_look)
808 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
809 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
810 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
812 else
814 if (pressed_look)
815 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
816 else
817 DrawEdge (hdc, &rc, EDGE_RAISED,
818 BF_SOFT | BF_RECT | BF_MIDDLE);
822 static void
823 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
825 HDC hdc = tbcd->nmcd.hdc;
826 int offset = 0;
827 BOOL pressed = bDropDownPressed ||
828 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
830 if (infoPtr->dwStyle & TBSTYLE_FLAT)
832 if (pressed)
833 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
834 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
835 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
836 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
837 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
839 else
841 if (pressed)
842 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
843 else
844 DrawEdge (hdc, rcArrow, EDGE_RAISED,
845 BF_SOFT | BF_RECT | BF_MIDDLE);
848 if (pressed)
849 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
851 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
853 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
854 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
856 else
857 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
860 /* draws a complete toolbar button */
861 static void
862 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
864 DWORD dwStyle = infoPtr->dwStyle;
865 BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
866 BOOL drawSepDropDownArrow = hasDropDownArrow &&
867 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
868 RECT rc, rcArrow, rcBitmap, rcText;
869 LPWSTR lpText = NULL;
870 NMTBCUSTOMDRAW tbcd;
871 DWORD ntfret;
872 INT offset;
873 INT oldBkMode;
874 DWORD dwItemCustDraw;
875 DWORD dwItemCDFlag;
876 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
878 rc = btnPtr->rect;
879 rcArrow = rc;
881 /* separator - doesn't send NM_CUSTOMDRAW */
882 if (btnPtr->fsStyle & BTNS_SEP) {
883 if (theme)
885 DrawThemeBackground (theme, hdc,
886 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
887 &rc, NULL);
889 else
890 /* with the FLAT style, iBitmap is the width and has already */
891 /* been taken into consideration in calculating the width */
892 /* so now we need to draw the vertical separator */
893 /* empirical tests show that iBitmap can/will be non-zero */
894 /* when drawing the vertical bar... */
895 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
896 if (dwStyle & CCS_VERT) {
897 RECT rcsep = rc;
898 InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
899 TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
901 else
902 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
904 else if (btnPtr->fsStyle != BTNS_SEP) {
905 FIXME("Draw some kind of separator: fsStyle=%x\n",
906 btnPtr->fsStyle);
908 return;
911 /* get a pointer to the text */
912 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
914 if (hasDropDownArrow)
916 int right;
918 if (dwStyle & TBSTYLE_FLAT)
919 right = max(rc.left, rc.right - DDARROW_WIDTH);
920 else
921 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
923 if (drawSepDropDownArrow)
924 rc.right = right;
926 rcArrow.left = right;
929 /* copy text & bitmap rects after adjusting for drop-down arrow
930 * so that text & bitmap is centered in the rectangle not containing
931 * the arrow */
932 rcText = rc;
933 rcBitmap = rc;
935 /* Center the bitmap horizontally and vertically */
936 if (dwStyle & TBSTYLE_LIST)
938 if (lpText &&
939 infoPtr->nMaxTextRows > 0 &&
940 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
941 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
942 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
943 else
944 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
946 else
947 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
949 rcBitmap.top += infoPtr->szPadding.cy / 2;
951 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
952 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
953 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
954 TRACE("Text=%s\n", debugstr_w(lpText));
955 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
957 /* calculate text position */
958 if (lpText)
960 InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
961 if (dwStyle & TBSTYLE_LIST)
963 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
965 else
967 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
968 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
969 else
970 rcText.top += infoPtr->szPadding.cy/2 + 2;
974 /* Initialize fields in all cases, because we use these later
975 * NOTE: applications can and do alter these to customize their
976 * toolbars */
977 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
978 tbcd.clrText = comctl32_color.clrBtnText;
979 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
980 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
981 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
982 tbcd.clrMark = comctl32_color.clrHighlight;
983 tbcd.clrHighlightHotTrack = 0;
984 tbcd.nStringBkMode = TRANSPARENT;
985 tbcd.nHLStringBkMode = OPAQUE;
986 tbcd.rcText.left = 0;
987 tbcd.rcText.top = 0;
988 tbcd.rcText.right = rcText.right - rc.left;
989 tbcd.rcText.bottom = rcText.bottom - rc.top;
990 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
991 tbcd.nmcd.hdc = hdc;
992 tbcd.nmcd.rc = btnPtr->rect;
993 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
995 /* FIXME: what are these used for? */
996 tbcd.hbrLines = 0;
997 tbcd.hpenLines = 0;
999 /* Issue Item Prepaint notify */
1000 dwItemCustDraw = 0;
1001 dwItemCDFlag = 0;
1002 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1004 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1005 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1006 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1007 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1008 /* reset these fields so the user can't alter the behaviour like native */
1009 tbcd.nmcd.hdc = hdc;
1010 tbcd.nmcd.rc = btnPtr->rect;
1012 dwItemCustDraw = ntfret & 0xffff;
1013 dwItemCDFlag = ntfret & 0xffff0000;
1014 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1015 return;
1016 /* save the only part of the rect that the user can change */
1017 rcText.right = tbcd.rcText.right + rc.left;
1018 rcText.bottom = tbcd.rcText.bottom + rc.top;
1021 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1022 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1023 OffsetRect(&rcText, 1, 1);
1025 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
1026 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1027 TOOLBAR_DrawPattern (&rc, &tbcd);
1029 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
1030 && (tbcd.nmcd.uItemState & CDIS_HOT))
1032 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1034 COLORREF oldclr;
1036 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1037 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1038 if (hasDropDownArrow)
1039 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1040 SetBkColor(hdc, oldclr);
1044 if (theme)
1046 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1047 int stateId = TS_NORMAL;
1049 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1050 stateId = TS_DISABLED;
1051 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1052 stateId = TS_PRESSED;
1053 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1054 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1055 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1056 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1057 stateId = TS_HOT;
1059 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1061 else
1062 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1064 if (drawSepDropDownArrow)
1066 if (theme)
1068 int stateId = TS_NORMAL;
1070 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1071 stateId = TS_DISABLED;
1072 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1073 stateId = TS_PRESSED;
1074 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1075 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1076 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1077 stateId = TS_HOT;
1079 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1080 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1082 else
1083 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1086 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1087 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1088 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1089 SetBkMode (hdc, oldBkMode);
1091 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1093 if (hasDropDownArrow && !drawSepDropDownArrow)
1095 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1097 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1098 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1100 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1102 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1103 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1105 else
1106 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1109 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1111 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1112 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1118 static void
1119 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1121 TBUTTON_INFO *btnPtr;
1122 INT i;
1123 RECT rcTemp, rcClient;
1124 NMTBCUSTOMDRAW tbcd;
1125 DWORD ntfret;
1126 DWORD dwBaseCustDraw;
1128 /* the app has told us not to redraw the toolbar */
1129 if (!infoPtr->bDoRedraw)
1130 return;
1132 /* if imagelist belongs to the app, it can be changed
1133 by the app after setting it */
1134 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1136 infoPtr->nNumBitmaps = 0;
1137 for (i = 0; i < infoPtr->cimlDef; i++)
1138 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1141 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1143 /* change the imagelist icon size if we manage the list and it is necessary */
1144 TOOLBAR_CheckImageListIconSize(infoPtr);
1146 /* Send initial notify */
1147 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1148 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1149 tbcd.nmcd.hdc = hdc;
1150 tbcd.nmcd.rc = ps->rcPaint;
1151 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1152 dwBaseCustDraw = ntfret & 0xffff;
1154 GetClientRect(infoPtr->hwndSelf, &rcClient);
1156 /* redraw necessary buttons */
1157 btnPtr = infoPtr->buttons;
1158 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1160 BOOL bDraw;
1161 if (!RectVisible(hdc, &btnPtr->rect))
1162 continue;
1163 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1165 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1166 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1168 else
1169 bDraw = TRUE;
1170 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1171 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1172 if (bDraw)
1173 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1176 /* draw insert mark if required */
1177 if (infoPtr->tbim.iButton != -1)
1179 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1180 RECT rcInsertMark;
1181 rcInsertMark.top = rcButton.top;
1182 rcInsertMark.bottom = rcButton.bottom;
1183 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1184 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1185 else
1186 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1187 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1190 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1192 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1193 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1194 tbcd.nmcd.hdc = hdc;
1195 tbcd.nmcd.rc = ps->rcPaint;
1196 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1200 /***********************************************************************
1201 * TOOLBAR_MeasureString
1203 * This function gets the width and height of a string in pixels. This
1204 * is done first by using GetTextExtentPoint to get the basic width
1205 * and height. The DrawText is called with DT_CALCRECT to get the exact
1206 * width. The reason is because the text may have more than one "&" (or
1207 * prefix characters as M$ likes to call them). The prefix character
1208 * indicates where the underline goes, except for the string "&&" which
1209 * is reduced to a single "&". GetTextExtentPoint does not process these
1210 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1212 static void
1213 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1214 HDC hdc, LPSIZE lpSize)
1216 RECT myrect;
1218 lpSize->cx = 0;
1219 lpSize->cy = 0;
1221 if (infoPtr->nMaxTextRows > 0 &&
1222 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1223 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1224 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1226 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1228 if(lpText != NULL) {
1229 /* first get size of all the text */
1230 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1232 /* feed above size into the rectangle for DrawText */
1233 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1235 /* Use DrawText to get true size as drawn (less pesky "&") */
1236 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1237 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1238 DT_NOPREFIX : 0));
1240 /* feed back to caller */
1241 lpSize->cx = myrect.right;
1242 lpSize->cy = myrect.bottom;
1246 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1249 /***********************************************************************
1250 * TOOLBAR_CalcStrings
1252 * This function walks through each string and measures it and returns
1253 * the largest height and width to caller.
1255 static void
1256 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1258 TBUTTON_INFO *btnPtr;
1259 INT i;
1260 SIZE sz;
1261 HDC hdc;
1262 HFONT hOldFont;
1264 lpSize->cx = 0;
1265 lpSize->cy = 0;
1267 if (infoPtr->nMaxTextRows == 0)
1268 return;
1270 hdc = GetDC (infoPtr->hwndSelf);
1271 hOldFont = SelectObject (hdc, infoPtr->hFont);
1273 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1275 TEXTMETRICW tm;
1277 GetTextMetricsW(hdc, &tm);
1278 lpSize->cy = tm.tmHeight;
1281 btnPtr = infoPtr->buttons;
1282 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1283 if(TOOLBAR_GetText(infoPtr, btnPtr))
1285 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1286 if (sz.cx > lpSize->cx)
1287 lpSize->cx = sz.cx;
1288 if (sz.cy > lpSize->cy)
1289 lpSize->cy = sz.cy;
1293 SelectObject (hdc, hOldFont);
1294 ReleaseDC (infoPtr->hwndSelf, hdc);
1296 TRACE("max string size %d x %d\n", lpSize->cx, lpSize->cy);
1299 /***********************************************************************
1300 * TOOLBAR_WrapToolbar
1302 * This function walks through the buttons and separators in the
1303 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1304 * wrapping should occur based on the width of the toolbar window.
1305 * It does *not* calculate button placement itself. That task
1306 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1307 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1308 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1310 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1311 * vertical toolbar lists.
1314 static void
1315 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1317 TBUTTON_INFO *btnPtr;
1318 INT x, cx, i, j, width;
1319 BOOL bButtonWrap;
1321 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1322 /* no layout is necessary. Applications may use this style */
1323 /* to perform their own layout on the toolbar. */
1324 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1325 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1327 btnPtr = infoPtr->buttons;
1328 x = infoPtr->nIndent;
1329 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1331 bButtonWrap = FALSE;
1333 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1334 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1335 infoPtr->nIndent);
1337 for (i = 0; i < infoPtr->nNumButtons; i++ )
1339 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1341 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1342 continue;
1344 if (btnPtr[i].cx > 0)
1345 cx = btnPtr[i].cx;
1346 /* horizontal separators are treated as buttons for width */
1347 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1348 !(infoPtr->dwStyle & CCS_VERT))
1349 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1350 else
1351 cx = infoPtr->nButtonWidth;
1353 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1354 cx += DDARROW_WIDTH;
1356 /* Two or more adjacent separators form a separator group. */
1357 /* The first separator in a group should be wrapped to the */
1358 /* next row if the previous wrapping is on a button. */
1359 if( bButtonWrap &&
1360 (btnPtr[i].fsStyle & BTNS_SEP) &&
1361 (i + 1 < infoPtr->nNumButtons ) &&
1362 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1364 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1365 btnPtr[i].fsState |= TBSTATE_WRAP;
1366 x = infoPtr->nIndent;
1367 i++;
1368 bButtonWrap = FALSE;
1369 continue;
1372 /* The layout makes sure the bitmap is visible, but not the button. */
1373 /* Test added to also wrap after a button that starts a row but */
1374 /* is bigger than the area. - GA 8/01 */
1375 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1376 ((x == infoPtr->nIndent) && (cx > width)))
1378 BOOL bFound = FALSE;
1380 /* If the current button is a separator and not hidden, */
1381 /* go to the next until it reaches a non separator. */
1382 /* Wrap the last separator if it is before a button. */
1383 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1384 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1385 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1386 i < infoPtr->nNumButtons )
1388 i++;
1389 bFound = TRUE;
1392 if( bFound && i < infoPtr->nNumButtons )
1394 i--;
1395 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1396 i, btnPtr[i].fsStyle, x, cx);
1397 btnPtr[i].fsState |= TBSTATE_WRAP;
1398 x = infoPtr->nIndent;
1399 bButtonWrap = FALSE;
1400 continue;
1402 else if ( i >= infoPtr->nNumButtons)
1403 break;
1405 /* If the current button is not a separator, find the last */
1406 /* separator and wrap it. */
1407 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1409 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1410 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1412 bFound = TRUE;
1413 i = j;
1414 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1415 i, btnPtr[i].fsStyle, x, cx);
1416 x = infoPtr->nIndent;
1417 btnPtr[j].fsState |= TBSTATE_WRAP;
1418 bButtonWrap = FALSE;
1419 break;
1423 /* If no separator available for wrapping, wrap one of */
1424 /* non-hidden previous button. */
1425 if (!bFound)
1427 for ( j = i - 1;
1428 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1430 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1431 continue;
1433 bFound = TRUE;
1434 i = j;
1435 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1436 i, btnPtr[i].fsStyle, x, cx);
1437 x = infoPtr->nIndent;
1438 btnPtr[j].fsState |= TBSTATE_WRAP;
1439 bButtonWrap = TRUE;
1440 break;
1444 /* If all above failed, wrap the current button. */
1445 if (!bFound)
1447 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1448 i, btnPtr[i].fsStyle, x, cx);
1449 btnPtr[i].fsState |= TBSTATE_WRAP;
1450 x = infoPtr->nIndent;
1451 if (btnPtr[i].fsStyle & BTNS_SEP )
1452 bButtonWrap = FALSE;
1453 else
1454 bButtonWrap = TRUE;
1457 else {
1458 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1459 i, btnPtr[i].fsStyle, x, cx);
1460 x += cx;
1466 /***********************************************************************
1467 * TOOLBAR_MeasureButton
1469 * Calculates the width and height required for a button. Used in
1470 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1471 * the width of buttons that are autosized.
1473 * Note that it would have been rather elegant to use one piece of code for
1474 * both the laying out of the toolbar and for controlling where button parts
1475 * are drawn, but the native control has inconsistencies between the two that
1476 * prevent this from being effectively. These inconsistencies can be seen as
1477 * artefacts where parts of the button appear outside of the bounding button
1478 * rectangle.
1480 * There are several cases for the calculation of the button dimensions and
1481 * button part positioning:
1483 * List
1484 * ====
1486 * With Bitmap:
1488 * +--------------------------------------------------------+ ^
1489 * | ^ ^ | |
1490 * | | pad.cy / 2 | centered | |
1491 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1492 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1493 * | |<------------>| | | | |
1494 * | +--------------+ +------------+ | |
1495 * |<-------------------------------------->| | |
1496 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1497 * | szText.cx | |
1498 * +--------------------------------------------------------+ -
1499 * <-------------------------------------------------------->
1500 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1502 * Without Bitmap (I_IMAGENONE):
1504 * +-----------------------------------+ ^
1505 * | ^ | |
1506 * | | centered | | LISTPAD_CY +
1507 * | +------------+ | | szText.cy
1508 * | | Text | | |
1509 * | | | | |
1510 * | +------------+ | |
1511 * |<----------------->| | |
1512 * | cxedge |<-----------> | |
1513 * | szText.cx | |
1514 * +-----------------------------------+ -
1515 * <----------------------------------->
1516 * szText.cx + pad.cx
1518 * Without text:
1520 * +--------------------------------------+ ^
1521 * | ^ | |
1522 * | | padding.cy/2 | | DEFPAD_CY +
1523 * | +------------+ | | nBitmapHeight
1524 * | | Bitmap | | |
1525 * | | | | |
1526 * | +------------+ | |
1527 * |<------------------->| | |
1528 * | cxedge + iListGap/2 |<-----------> | |
1529 * | nBitmapWidth | |
1530 * +--------------------------------------+ -
1531 * <-------------------------------------->
1532 * 2*cxedge + nBitmapWidth + iListGap
1534 * Non-List
1535 * ========
1537 * With bitmap:
1539 * +-----------------------------------+ ^
1540 * | ^ | |
1541 * | | pad.cy / 2 | | nBitmapHeight +
1542 * | - | | szText.cy +
1543 * | +------------+ | | DEFPAD_CY + 1
1544 * | centered | Bitmap | | |
1545 * |<----------------->| | | |
1546 * | +------------+ | |
1547 * | ^ | |
1548 * | 1 | | |
1549 * | - | |
1550 * | centered +---------------+ | |
1551 * |<--------------->| Text | | |
1552 * | +---------------+ | |
1553 * +-----------------------------------+ -
1554 * <----------------------------------->
1555 * pad.cx + max(nBitmapWidth, szText.cx)
1557 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1559 * +---------------------------------------+ ^
1560 * | ^ | |
1561 * | | 2 + pad.cy / 2 | |
1562 * | - | | szText.cy +
1563 * | centered +-----------------+ | | pad.cy + 2
1564 * |<--------------->| Text | | |
1565 * | +-----------------+ | |
1566 * | | |
1567 * +---------------------------------------+ -
1568 * <--------------------------------------->
1569 * 2*cxedge + pad.cx + szText.cx
1571 * Without text:
1572 * As for with bitmaps, but with szText.cx zero.
1574 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1575 BOOL bHasBitmap, BOOL bValidImageList)
1577 SIZE sizeButton;
1578 if (infoPtr->dwStyle & TBSTYLE_LIST)
1580 /* set button height from bitmap / text height... */
1581 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1582 sizeString.cy);
1584 /* ... add on the necessary padding */
1585 if (bValidImageList)
1587 if (bHasBitmap)
1588 sizeButton.cy += DEFPAD_CY;
1589 else
1590 sizeButton.cy += LISTPAD_CY;
1592 else
1593 sizeButton.cy += infoPtr->szPadding.cy;
1595 /* calculate button width */
1596 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1597 infoPtr->nBitmapWidth + infoPtr->iListGap;
1598 if (sizeString.cx > 0)
1599 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1602 else
1604 if (bHasBitmap)
1606 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1607 if (sizeString.cy > 0)
1608 sizeButton.cy += 1 + sizeString.cy;
1609 sizeButton.cx = infoPtr->szPadding.cx +
1610 max(sizeString.cx, infoPtr->nBitmapWidth);
1612 else
1614 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1615 NONLIST_NOTEXT_OFFSET;
1616 sizeButton.cx = infoPtr->szPadding.cx +
1617 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1620 return sizeButton;
1624 /***********************************************************************
1625 * TOOLBAR_CalcToolbar
1627 * This function calculates button and separator placement. It first
1628 * calculates the button sizes, gets the toolbar window width and then
1629 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1630 * on. It assigns a new location to each item and sends this location to
1631 * the tooltip window if appropriate. Finally, it updates the rcBound
1632 * rect and calculates the new required toolbar window height.
1634 static void
1635 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1637 SIZE sizeString, sizeButton;
1638 BOOL validImageList = FALSE;
1640 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1642 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1644 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1645 validImageList = TRUE;
1646 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1647 infoPtr->nButtonWidth = sizeButton.cx;
1648 infoPtr->nButtonHeight = sizeButton.cy;
1649 infoPtr->iTopMargin = default_top_margin(infoPtr);
1651 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1652 infoPtr->nButtonWidth = infoPtr->cxMin;
1653 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1654 infoPtr->nButtonWidth = infoPtr->cxMax;
1656 TOOLBAR_LayoutToolbar(infoPtr);
1659 static void
1660 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1662 TBUTTON_INFO *btnPtr;
1663 SIZE sizeButton;
1664 INT i, nRows, nSepRows;
1665 INT x, y, cx, cy;
1666 BOOL bWrap;
1667 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1669 TOOLBAR_WrapToolbar(infoPtr);
1671 x = infoPtr->nIndent;
1672 y = infoPtr->iTopMargin;
1673 cx = infoPtr->nButtonWidth;
1674 cy = infoPtr->nButtonHeight;
1676 nRows = nSepRows = 0;
1678 infoPtr->rcBound.top = y;
1679 infoPtr->rcBound.left = x;
1680 infoPtr->rcBound.bottom = y + cy;
1681 infoPtr->rcBound.right = x;
1683 btnPtr = infoPtr->buttons;
1685 TRACE("cy=%d\n", cy);
1687 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1689 bWrap = FALSE;
1690 if (btnPtr->fsState & TBSTATE_HIDDEN)
1692 SetRectEmpty (&btnPtr->rect);
1693 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
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 rcButton = lpdis->rcItem;
2652 InflateRect (&rcButton, -1, -1);
2653 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 case 2:
2814 info.nButtons = 15;
2815 info.nID = IDB_STD_SMALL;
2816 break;
2817 case IDB_STD_LARGE_COLOR:
2818 case 3:
2819 info.nButtons = 15;
2820 info.nID = IDB_STD_LARGE;
2821 break;
2822 case IDB_VIEW_SMALL_COLOR:
2823 case 6:
2824 info.nButtons = 12;
2825 info.nID = IDB_VIEW_SMALL;
2826 break;
2827 case IDB_VIEW_LARGE_COLOR:
2828 case 7:
2829 info.nButtons = 12;
2830 info.nID = IDB_VIEW_LARGE;
2831 break;
2832 case IDB_HIST_SMALL_COLOR:
2833 info.nButtons = 5;
2834 info.nID = IDB_HIST_SMALL;
2835 break;
2836 case IDB_HIST_LARGE_COLOR:
2837 info.nButtons = 5;
2838 info.nID = IDB_HIST_LARGE;
2839 break;
2840 default:
2841 WARN("unknown bitmap id, %ld\n", lpAddBmp->nID);
2842 return -1;
2845 TRACE ("adding %d internal bitmaps\n", info.nButtons);
2847 /* Windows resize all the buttons to the size of a newly added standard image */
2848 if (lpAddBmp->nID & 1)
2850 /* large icons: 24x24. Will make the button 31x30 */
2851 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2853 else
2855 /* small icons: 16x16. Will make the buttons 23x22 */
2856 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2859 TOOLBAR_CalcToolbar (infoPtr);
2861 else
2863 info.nButtons = count;
2864 info.hInst = lpAddBmp->hInst;
2865 info.nID = lpAddBmp->nID;
2866 TRACE("adding %d bitmaps\n", info.nButtons);
2869 /* check if the bitmap is already loaded and compute iSumButtons */
2870 iSumButtons = 0;
2871 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2873 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2874 infoPtr->bitmaps[i].nID == info.nID)
2875 return iSumButtons;
2876 iSumButtons += infoPtr->bitmaps[i].nButtons;
2879 if (!infoPtr->cimlDef) {
2880 /* create new default image list */
2881 TRACE ("creating default image list\n");
2883 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2884 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2885 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2886 infoPtr->himlInt = himlDef;
2888 else {
2889 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2892 if (!himlDef) {
2893 WARN("No default image list available\n");
2894 return -1;
2897 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2898 return -1;
2900 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2901 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2902 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2903 infoPtr->nNumBitmapInfos++;
2904 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2906 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2907 return iSumButtons;
2911 static LRESULT
2912 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2914 TRACE("adding %d buttons (unicode=%d)\n", nAddButtons, fUnicode);
2916 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2920 static LRESULT
2921 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2923 #define MAX_RESOURCE_STRING_LENGTH 512
2924 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2925 INT nIndex = infoPtr->nNumStrings;
2927 TRACE("%p, %lx\n", hInstance, lParam);
2929 if (IS_INTRESOURCE(lParam)) {
2930 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2931 WCHAR delimiter;
2932 WCHAR *next_delim;
2933 HRSRC hrsrc;
2934 WCHAR *p;
2935 INT len;
2937 TRACE("adding string from resource\n");
2939 if (!hInstance) return -1;
2941 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2942 (LPWSTR)RT_STRING );
2943 if (!hrsrc)
2945 TRACE("string not found in resources\n");
2946 return -1;
2949 len = LoadStringW (hInstance, (UINT)lParam,
2950 szString, MAX_RESOURCE_STRING_LENGTH);
2952 TRACE("len=%d %s\n", len, debugstr_w(szString));
2953 if (len == 0 || len == 1)
2954 return nIndex;
2956 TRACE("delimiter: 0x%x\n", *szString);
2957 delimiter = *szString;
2958 p = szString + 1;
2960 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2961 *next_delim = 0;
2962 if (next_delim + 1 >= szString + len)
2964 /* this may happen if delimiter == '\0' or if the last char is a
2965 * delimiter (then it is ignored like the native does) */
2966 break;
2969 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2970 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2971 infoPtr->nNumStrings++;
2973 p = next_delim + 1;
2976 else {
2977 LPWSTR p = (LPWSTR)lParam;
2978 INT len;
2980 if (p == NULL)
2981 return -1;
2982 TRACE("adding string(s) from array\n");
2983 while (*p) {
2984 len = strlenW (p);
2986 TRACE("len=%d %s\n", len, debugstr_w(p));
2987 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2988 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2989 infoPtr->nNumStrings++;
2991 p += (len+1);
2995 if (fFirstString)
2996 TOOLBAR_CalcToolbar(infoPtr);
2997 return nIndex;
3001 static LRESULT
3002 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3004 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3005 LPSTR p;
3006 INT nIndex;
3007 INT len;
3009 TRACE("%p, %lx\n", hInstance, lParam);
3011 if (IS_INTRESOURCE(lParam)) /* load from resources */
3012 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
3014 p = (LPSTR)lParam;
3015 if (p == NULL)
3016 return -1;
3018 TRACE("adding string(s) from array\n");
3019 nIndex = infoPtr->nNumStrings;
3020 while (*p) {
3021 len = strlen (p);
3022 TRACE("len=%d \"%s\"\n", len, p);
3024 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3025 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3026 infoPtr->nNumStrings++;
3028 p += (len+1);
3031 if (fFirstString)
3032 TOOLBAR_CalcToolbar(infoPtr);
3033 return nIndex;
3037 static LRESULT
3038 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3040 TRACE("auto sizing, style=%#x\n", infoPtr->dwStyle);
3041 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3043 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3045 RECT window_rect, parent_rect;
3046 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3047 HWND parent;
3048 INT x, y, cx, cy;
3050 parent = GetParent (infoPtr->hwndSelf);
3052 if (!parent || !infoPtr->bDoRedraw)
3053 return 0;
3055 GetClientRect(parent, &parent_rect);
3057 x = parent_rect.left;
3058 y = parent_rect.top;
3060 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3061 cx = parent_rect.right - parent_rect.left;
3063 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3065 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3066 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3067 y = window_rect.top;
3069 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3071 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3072 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3075 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3076 uPosFlags |= SWP_NOMOVE;
3078 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3079 cy += GetSystemMetrics(SM_CYEDGE);
3081 if (infoPtr->dwStyle & WS_BORDER)
3083 cx += 2 * GetSystemMetrics(SM_CXBORDER);
3084 cy += 2 * GetSystemMetrics(SM_CYBORDER);
3087 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3090 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3092 TOOLBAR_LayoutToolbar(infoPtr);
3093 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3096 return 0;
3100 static inline LRESULT
3101 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3103 return infoPtr->nNumButtons;
3107 static inline LRESULT
3108 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3110 infoPtr->dwStructSize = Size;
3112 return 0;
3116 static LRESULT
3117 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3119 TBUTTON_INFO *btnPtr;
3120 INT nIndex;
3122 TRACE("button %d, iBitmap now %d\n", Id, Index);
3124 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3125 if (nIndex == -1)
3126 return FALSE;
3128 btnPtr = &infoPtr->buttons[nIndex];
3129 btnPtr->iBitmap = Index;
3131 /* we HAVE to erase the background, the new bitmap could be */
3132 /* transparent */
3133 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3135 return TRUE;
3139 static LRESULT
3140 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3142 TBUTTON_INFO *btnPtr;
3143 INT nIndex;
3144 INT nOldIndex = -1;
3145 BOOL bChecked = FALSE;
3147 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3149 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3151 if (nIndex == -1)
3152 return FALSE;
3154 btnPtr = &infoPtr->buttons[nIndex];
3156 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3158 if (!LOWORD(lParam))
3159 btnPtr->fsState &= ~TBSTATE_CHECKED;
3160 else {
3161 if (btnPtr->fsStyle & BTNS_GROUP) {
3162 nOldIndex =
3163 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3164 if (nOldIndex == nIndex)
3165 return 0;
3166 if (nOldIndex != -1)
3167 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3169 btnPtr->fsState |= TBSTATE_CHECKED;
3172 if( bChecked != LOWORD(lParam) )
3174 if (nOldIndex != -1)
3175 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3176 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3179 /* FIXME: Send a WM_NOTIFY?? */
3181 return TRUE;
3185 static LRESULT
3186 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3188 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3192 static LRESULT
3193 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3195 CUSTDLG_INFO custInfo;
3196 LRESULT ret;
3197 NMHDR nmhdr;
3199 custInfo.tbInfo = infoPtr;
3200 custInfo.tbHwnd = infoPtr->hwndSelf;
3202 /* send TBN_BEGINADJUST notification */
3203 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3205 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3206 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3208 /* send TBN_ENDADJUST notification */
3209 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3211 return ret;
3215 static LRESULT
3216 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3218 NMTOOLBARW nmtb;
3219 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3221 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3222 return FALSE;
3224 memset(&nmtb, 0, sizeof(nmtb));
3225 nmtb.iItem = btnPtr->idCommand;
3226 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3227 nmtb.tbButton.idCommand = btnPtr->idCommand;
3228 nmtb.tbButton.fsState = btnPtr->fsState;
3229 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3230 nmtb.tbButton.dwData = btnPtr->dwData;
3231 nmtb.tbButton.iString = btnPtr->iString;
3232 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3234 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3236 if (infoPtr->nNumButtons == 1) {
3237 TRACE(" simple delete\n");
3238 free_string( infoPtr->buttons );
3239 Free (infoPtr->buttons);
3240 infoPtr->buttons = NULL;
3241 infoPtr->nNumButtons = 0;
3243 else {
3244 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3245 TRACE("complex delete [nIndex=%d]\n", nIndex);
3247 infoPtr->nNumButtons--;
3248 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3249 if (nIndex > 0) {
3250 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3251 nIndex * sizeof(TBUTTON_INFO));
3254 if (nIndex < infoPtr->nNumButtons) {
3255 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3256 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3259 free_string( oldButtons + nIndex );
3260 Free (oldButtons);
3263 TOOLBAR_LayoutToolbar(infoPtr);
3265 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3267 return TRUE;
3271 static LRESULT
3272 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3274 TBUTTON_INFO *btnPtr;
3275 INT nIndex;
3276 DWORD bState;
3278 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3280 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3282 if (nIndex == -1)
3283 return FALSE;
3285 btnPtr = &infoPtr->buttons[nIndex];
3287 bState = btnPtr->fsState & TBSTATE_ENABLED;
3289 /* update the toolbar button state */
3290 if(!LOWORD(lParam)) {
3291 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3292 } else {
3293 btnPtr->fsState |= TBSTATE_ENABLED;
3296 /* redraw the button only if the state of the button changed */
3297 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3298 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3300 return TRUE;
3304 static inline LRESULT
3305 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3307 return infoPtr->bAnchor;
3311 static LRESULT
3312 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3314 INT nIndex;
3316 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3317 if (nIndex == -1)
3318 return -1;
3320 return infoPtr->buttons[nIndex].iBitmap;
3324 static inline LRESULT
3325 TOOLBAR_GetBitmapFlags (void)
3327 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3331 static LRESULT
3332 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3334 TBUTTON_INFO *btnPtr;
3336 if (lpTbb == NULL)
3337 return FALSE;
3339 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3340 return FALSE;
3342 btnPtr = &infoPtr->buttons[nIndex];
3343 lpTbb->iBitmap = btnPtr->iBitmap;
3344 lpTbb->idCommand = btnPtr->idCommand;
3345 lpTbb->fsState = btnPtr->fsState;
3346 lpTbb->fsStyle = btnPtr->fsStyle;
3347 lpTbb->bReserved[0] = 0;
3348 lpTbb->bReserved[1] = 0;
3349 lpTbb->dwData = btnPtr->dwData;
3350 lpTbb->iString = btnPtr->iString;
3352 return TRUE;
3356 static LRESULT
3357 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3359 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3360 TBUTTON_INFO *btnPtr;
3361 INT nIndex;
3363 if (lpTbInfo == NULL)
3364 return -1;
3366 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3367 * the headers and tests shows that even with comctl 6 Vista accepts only the
3368 * original TBBUTTONINFO size
3370 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3372 WARN("Invalid button size\n");
3373 return -1;
3376 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3377 if (nIndex == -1)
3378 return -1;
3380 btnPtr = &infoPtr->buttons[nIndex];
3381 if (lpTbInfo->dwMask & TBIF_COMMAND)
3382 lpTbInfo->idCommand = btnPtr->idCommand;
3383 if (lpTbInfo->dwMask & TBIF_IMAGE)
3384 lpTbInfo->iImage = btnPtr->iBitmap;
3385 if (lpTbInfo->dwMask & TBIF_LPARAM)
3386 lpTbInfo->lParam = btnPtr->dwData;
3387 if (lpTbInfo->dwMask & TBIF_SIZE)
3388 /* tests show that for separators TBIF_SIZE returns not calculated width,
3389 but cx property, that differs from 0 only if application have
3390 specifically set it */
3391 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3392 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3393 if (lpTbInfo->dwMask & TBIF_STATE)
3394 lpTbInfo->fsState = btnPtr->fsState;
3395 if (lpTbInfo->dwMask & TBIF_STYLE)
3396 lpTbInfo->fsStyle = btnPtr->fsStyle;
3397 if (lpTbInfo->dwMask & TBIF_TEXT) {
3398 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3399 can't use TOOLBAR_GetText here */
3400 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3401 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3402 if (bUnicode)
3403 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3404 else
3405 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3406 } else if (!bUnicode || lpTbInfo->pszText)
3407 lpTbInfo->pszText[0] = '\0';
3409 return nIndex;
3413 static inline LRESULT
3414 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3416 return MAKELONG((WORD)infoPtr->nButtonWidth,
3417 (WORD)infoPtr->nButtonHeight);
3421 static LRESULT
3422 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3424 INT nIndex;
3425 LPWSTR lpText;
3426 LRESULT ret = 0;
3428 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3429 if (nIndex == -1)
3430 return -1;
3432 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3434 if (isW)
3436 if (lpText)
3438 ret = strlenW (lpText);
3439 if (lpStr) strcpyW (lpStr, lpText);
3442 else
3443 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3444 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3445 return ret;
3449 static LRESULT
3450 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3452 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3453 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3454 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3458 static inline LRESULT
3459 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3461 TRACE("\n");
3463 return infoPtr->dwExStyle;
3467 static LRESULT
3468 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3470 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3471 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3472 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3476 static LRESULT
3477 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3479 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3480 return -1;
3482 if (infoPtr->nHotItem < 0)
3483 return -1;
3485 return (LRESULT)infoPtr->nHotItem;
3489 static LRESULT
3490 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3492 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3493 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3494 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3498 static LRESULT
3499 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3501 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3503 *lptbim = infoPtr->tbim;
3505 return 0;
3509 static inline LRESULT
3510 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3512 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3514 return (LRESULT)infoPtr->clrInsertMark;
3518 static LRESULT
3519 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3521 TBUTTON_INFO *btnPtr;
3523 btnPtr = &infoPtr->buttons[nIndex];
3524 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3525 return FALSE;
3527 if (lpRect == NULL)
3528 return FALSE;
3529 if (btnPtr->fsState & TBSTATE_HIDDEN)
3530 return FALSE;
3532 lpRect->left = btnPtr->rect.left;
3533 lpRect->right = btnPtr->rect.right;
3534 lpRect->bottom = btnPtr->rect.bottom;
3535 lpRect->top = btnPtr->rect.top;
3537 return TRUE;
3541 static LRESULT
3542 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3544 if (lpSize == NULL)
3545 return FALSE;
3547 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3548 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3550 TRACE("maximum size %d x %d\n",
3551 infoPtr->rcBound.right - infoPtr->rcBound.left,
3552 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3554 return TRUE;
3558 /* << TOOLBAR_GetObject >> */
3561 static inline LRESULT
3562 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3564 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3568 static LRESULT
3569 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3571 TBUTTON_INFO *btnPtr;
3572 INT nIndex;
3574 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3575 btnPtr = &infoPtr->buttons[nIndex];
3576 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3577 return FALSE;
3579 if (lpRect == NULL)
3580 return FALSE;
3582 lpRect->left = btnPtr->rect.left;
3583 lpRect->right = btnPtr->rect.right;
3584 lpRect->bottom = btnPtr->rect.bottom;
3585 lpRect->top = btnPtr->rect.top;
3587 return TRUE;
3591 static inline LRESULT
3592 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3594 return infoPtr->nRows;
3598 static LRESULT
3599 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3601 INT nIndex;
3603 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3604 if (nIndex == -1)
3605 return -1;
3607 return infoPtr->buttons[nIndex].fsState;
3611 static inline LRESULT
3612 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3614 return infoPtr->dwStyle;
3618 static inline LRESULT
3619 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3621 return infoPtr->nMaxTextRows;
3625 static LRESULT
3626 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3628 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3629 TOOLBAR_TooltipCreateControl(infoPtr);
3630 return (LRESULT)infoPtr->hwndToolTip;
3634 static LRESULT
3635 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3637 TRACE("%s hwnd=%p\n",
3638 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3640 return infoPtr->bUnicode;
3644 static inline LRESULT
3645 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3647 return infoPtr->iVersion;
3651 static LRESULT
3652 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3654 TBUTTON_INFO *btnPtr;
3655 BYTE oldState;
3656 INT nIndex;
3658 TRACE("\n");
3660 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3661 if (nIndex == -1)
3662 return FALSE;
3664 btnPtr = &infoPtr->buttons[nIndex];
3665 oldState = btnPtr->fsState;
3667 if (fHide)
3668 btnPtr->fsState |= TBSTATE_HIDDEN;
3669 else
3670 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3672 if (oldState != btnPtr->fsState) {
3673 TOOLBAR_LayoutToolbar (infoPtr);
3674 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3677 return TRUE;
3681 static inline LRESULT
3682 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3684 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3688 static LRESULT
3689 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3691 TBUTTON_INFO *btnPtr;
3692 INT nIndex;
3693 DWORD oldState;
3695 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3696 if (nIndex == -1)
3697 return FALSE;
3699 btnPtr = &infoPtr->buttons[nIndex];
3700 oldState = btnPtr->fsState;
3702 if (fIndeterminate)
3703 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3704 else
3705 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3707 if(oldState != btnPtr->fsState)
3708 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3710 return TRUE;
3714 static LRESULT
3715 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3717 if (lpTbb == NULL)
3718 return FALSE;
3720 if (nIndex == -1) {
3721 /* EPP: this seems to be an undocumented call (from my IE4)
3722 * I assume in that case that:
3723 * - index of insertion is at the end of existing buttons
3724 * I only see this happen with nIndex == -1, but it could have a special
3725 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3727 nIndex = infoPtr->nNumButtons;
3729 } else if (nIndex < 0)
3730 return FALSE;
3732 TRACE("inserting button index=%d\n", nIndex);
3733 if (nIndex > infoPtr->nNumButtons) {
3734 nIndex = infoPtr->nNumButtons;
3735 TRACE("adjust index=%d\n", nIndex);
3738 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3741 /* << TOOLBAR_InsertMarkHitTest >> */
3744 static LRESULT
3745 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3747 INT nIndex;
3749 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3750 if (nIndex == -1)
3751 return -1;
3753 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3757 static LRESULT
3758 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3760 INT nIndex;
3762 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3763 if (nIndex == -1)
3764 return -1;
3766 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3770 static LRESULT
3771 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3773 INT nIndex;
3775 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3776 if (nIndex == -1)
3777 return -1;
3779 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3783 static LRESULT
3784 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3786 INT nIndex;
3788 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3789 if (nIndex == -1)
3790 return -1;
3792 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3796 static LRESULT
3797 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3799 INT nIndex;
3801 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3802 if (nIndex == -1)
3803 return -1;
3805 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3809 static LRESULT
3810 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3812 INT nIndex;
3814 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3815 if (nIndex == -1)
3816 return -1;
3818 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3822 static LRESULT
3823 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3825 TBADDBITMAP tbab;
3826 tbab.hInst = hInstance;
3827 tbab.nID = wParam;
3829 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3831 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3835 static LRESULT
3836 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3838 WCHAR wszAccel[] = {'&',wAccel,0};
3839 int i;
3841 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3842 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3844 for (i = 0; i < infoPtr->nNumButtons; i++)
3846 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3847 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3848 !(btnPtr->fsState & TBSTATE_HIDDEN))
3850 int iLen = strlenW(wszAccel);
3851 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3853 if (!lpszStr)
3854 continue;
3856 while (*lpszStr)
3858 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3860 lpszStr += 2;
3861 continue;
3863 if (!strncmpiW(lpszStr, wszAccel, iLen))
3865 *pIDButton = btnPtr->idCommand;
3866 return TRUE;
3868 lpszStr++;
3872 return FALSE;
3876 static LRESULT
3877 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3879 INT nIndex;
3880 DWORD oldState;
3881 TBUTTON_INFO *btnPtr;
3883 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3885 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3886 if (nIndex == -1)
3887 return FALSE;
3889 btnPtr = &infoPtr->buttons[nIndex];
3890 oldState = btnPtr->fsState;
3892 if (fMark)
3893 btnPtr->fsState |= TBSTATE_MARKED;
3894 else
3895 btnPtr->fsState &= ~TBSTATE_MARKED;
3897 if(oldState != btnPtr->fsState)
3898 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3900 return TRUE;
3904 /* fixes up an index of a button affected by a move */
3905 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3907 if (bMoveUp)
3909 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3910 (*pIndex)--;
3911 else if (*pIndex == nIndex)
3912 *pIndex = nMoveIndex;
3914 else
3916 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3917 (*pIndex)++;
3918 else if (*pIndex == nIndex)
3919 *pIndex = nMoveIndex;
3924 static LRESULT
3925 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3927 INT nIndex;
3928 INT nCount;
3929 TBUTTON_INFO button;
3931 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3933 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3934 if ((nIndex == -1) || (nMoveIndex < 0))
3935 return FALSE;
3937 if (nMoveIndex > infoPtr->nNumButtons - 1)
3938 nMoveIndex = infoPtr->nNumButtons - 1;
3940 button = infoPtr->buttons[nIndex];
3942 /* move button right */
3943 if (nIndex < nMoveIndex)
3945 nCount = nMoveIndex - nIndex;
3946 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3947 infoPtr->buttons[nMoveIndex] = button;
3949 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3950 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3951 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3952 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3954 else if (nIndex > nMoveIndex) /* move button left */
3956 nCount = nIndex - nMoveIndex;
3957 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3958 infoPtr->buttons[nMoveIndex] = button;
3960 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3961 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3962 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3963 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3966 TOOLBAR_LayoutToolbar(infoPtr);
3967 TOOLBAR_AutoSize(infoPtr);
3968 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3970 return TRUE;
3974 static LRESULT
3975 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
3977 TBUTTON_INFO *btnPtr;
3978 INT nIndex;
3979 DWORD oldState;
3981 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3982 if (nIndex == -1)
3983 return FALSE;
3985 btnPtr = &infoPtr->buttons[nIndex];
3986 oldState = btnPtr->fsState;
3988 if (fPress)
3989 btnPtr->fsState |= TBSTATE_PRESSED;
3990 else
3991 btnPtr->fsState &= ~TBSTATE_PRESSED;
3993 if(oldState != btnPtr->fsState)
3994 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3996 return TRUE;
3999 /* FIXME: there might still be some confusion her between number of buttons
4000 * and number of bitmaps */
4001 static LRESULT
4002 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
4004 HBITMAP hBitmap;
4005 int i = 0, nOldButtons = 0, pos = 0;
4006 int nOldBitmaps, nNewBitmaps = 0;
4007 HIMAGELIST himlDef = 0;
4009 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4010 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4011 lpReplace->nButtons);
4013 if (lpReplace->hInstOld == HINST_COMMCTRL)
4015 FIXME("changing standard bitmaps not implemented\n");
4016 return FALSE;
4018 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4019 FIXME("resources not in the current module not implemented\n");
4021 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4022 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4023 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4024 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4025 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4027 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4028 nOldButtons = tbi->nButtons;
4029 tbi->nButtons = lpReplace->nButtons;
4030 tbi->hInst = lpReplace->hInstNew;
4031 tbi->nID = lpReplace->nIDNew;
4032 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4033 break;
4035 pos += tbi->nButtons;
4038 if (nOldButtons == 0)
4040 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4041 return FALSE;
4044 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4045 * bitmap
4047 if (lpReplace->hInstNew)
4048 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4049 else
4050 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4052 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4053 nOldBitmaps = ImageList_GetImageCount(himlDef);
4055 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4057 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4058 ImageList_Remove(himlDef, i);
4060 if (hBitmap)
4062 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4063 nNewBitmaps = ImageList_GetImageCount(himlDef);
4064 DeleteObject(hBitmap);
4067 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4069 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4070 pos, nOldBitmaps, nNewBitmaps);
4072 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4073 return TRUE;
4077 /* helper for TOOLBAR_SaveRestoreW */
4078 static BOOL
4079 TOOLBAR_Save(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *params)
4081 NMTBSAVE save;
4082 INT ret, i;
4083 BOOL alloced = FALSE;
4084 HKEY key;
4086 TRACE( "save to %s %s\n", debugstr_w(params->pszSubKey), debugstr_w(params->pszValueName) );
4088 memset( &save, 0, sizeof(save) );
4089 save.cbData = infoPtr->nNumButtons * sizeof(DWORD);
4090 save.iItem = -1;
4091 save.cButtons = infoPtr->nNumButtons;
4092 save.tbButton.idCommand = -1;
4093 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4095 if (!save.pData)
4097 save.pData = Alloc( save.cbData );
4098 if (!save.pData) return FALSE;
4099 alloced = TRUE;
4101 if (!save.pCurrent) save.pCurrent = save.pData;
4103 for (i = 0; i < infoPtr->nNumButtons; i++)
4105 save.iItem = i;
4106 save.tbButton.iBitmap = infoPtr->buttons[i].iBitmap;
4107 save.tbButton.idCommand = infoPtr->buttons[i].idCommand;
4108 save.tbButton.fsState = infoPtr->buttons[i].fsState;
4109 save.tbButton.fsStyle = infoPtr->buttons[i].fsStyle;
4110 memset( save.tbButton.bReserved, 0, sizeof(save.tbButton.bReserved) );
4111 save.tbButton.dwData = infoPtr->buttons[i].dwData;
4112 save.tbButton.iString = infoPtr->buttons[i].iString;
4114 *save.pCurrent++ = save.tbButton.idCommand;
4116 TOOLBAR_SendNotify( &save.hdr, infoPtr, TBN_SAVE );
4119 ret = RegCreateKeyW( params->hkr, params->pszSubKey, &key );
4120 if (ret == ERROR_SUCCESS)
4122 ret = RegSetValueExW( key, params->pszValueName, 0, REG_BINARY, (BYTE *)save.pData, save.cbData );
4123 RegCloseKey( key );
4126 if (alloced) Free( save.pData );
4127 return !ret;
4131 /* helper for TOOLBAR_Restore */
4132 static void
4133 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4135 INT i;
4137 for (i = 0; i < infoPtr->nNumButtons; i++)
4139 free_string( infoPtr->buttons + i );
4140 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4143 Free(infoPtr->buttons);
4144 infoPtr->buttons = NULL;
4145 infoPtr->nNumButtons = 0;
4149 /* helper for TOOLBAR_SaveRestoreW */
4150 static BOOL
4151 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4153 LONG res;
4154 HKEY hkey = NULL;
4155 BOOL ret = FALSE;
4156 DWORD dwType;
4157 DWORD dwSize = 0;
4158 NMTBRESTORE nmtbr;
4159 NMHDR hdr;
4161 /* restore toolbar information */
4162 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4163 debugstr_w(lpSave->pszValueName));
4165 memset(&nmtbr, 0, sizeof(nmtbr));
4167 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4168 KEY_QUERY_VALUE, &hkey);
4169 if (!res)
4170 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4171 NULL, &dwSize);
4172 if (!res && dwType != REG_BINARY)
4173 res = ERROR_FILE_NOT_FOUND;
4174 if (!res)
4176 nmtbr.pData = Alloc(dwSize);
4177 nmtbr.cbData = dwSize;
4178 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4180 if (!res)
4181 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4182 (LPBYTE)nmtbr.pData, &dwSize);
4183 if (!res)
4185 nmtbr.pCurrent = nmtbr.pData;
4186 nmtbr.iItem = -1;
4187 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4188 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4190 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4192 INT i, count = nmtbr.cButtons;
4194 /* remove all existing buttons as this function is designed to
4195 * restore the toolbar to a previously saved state */
4196 TOOLBAR_DeleteAllButtons(infoPtr);
4198 for (i = 0; i < count; i++)
4200 nmtbr.iItem = i;
4201 nmtbr.tbButton.iBitmap = -1;
4202 nmtbr.tbButton.fsState = 0;
4203 nmtbr.tbButton.fsStyle = 0;
4204 nmtbr.tbButton.dwData = 0;
4205 nmtbr.tbButton.iString = 0;
4207 if (*nmtbr.pCurrent & 0x80000000)
4209 /* separator */
4210 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4211 nmtbr.tbButton.idCommand = 0;
4212 nmtbr.tbButton.fsStyle = BTNS_SEP;
4213 if (*nmtbr.pCurrent != (DWORD)-1)
4214 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4216 else
4217 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4219 nmtbr.pCurrent++;
4221 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4223 /* All returned ptrs and -1 are ignored */
4224 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4225 nmtbr.tbButton.iString = 0;
4227 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4230 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_BEGINADJUST );
4231 for (i = 0; ; i++)
4233 NMTOOLBARW tb;
4234 TBBUTTONINFOW bi;
4235 WCHAR buf[128];
4236 UINT code = infoPtr->bUnicode ? TBN_GETBUTTONINFOW : TBN_GETBUTTONINFOA;
4237 INT idx;
4239 memset( &tb, 0, sizeof(tb) );
4240 tb.iItem = i;
4241 tb.cchText = sizeof(buf) / sizeof(buf[0]);
4242 tb.pszText = buf;
4244 /* Use the same struct for both A and W versions since the layout is the same. */
4245 if (!TOOLBAR_SendNotify( &tb.hdr, infoPtr, code ))
4246 break;
4248 idx = TOOLBAR_GetButtonIndex( infoPtr, tb.tbButton.idCommand, FALSE );
4249 if (idx == -1) continue;
4251 /* tb.pszText is ignored - the string comes from tb.tbButton.iString, which may
4252 be an index or a ptr. Either way it is simply copied. There is no api to change
4253 the string index, so we set it manually. The other properties can be set with SetButtonInfo. */
4254 free_string( infoPtr->buttons + idx );
4255 infoPtr->buttons[idx].iString = tb.tbButton.iString;
4257 memset( &bi, 0, sizeof(bi) );
4258 bi.cbSize = sizeof(bi);
4259 bi.dwMask = TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_LPARAM;
4260 bi.iImage = tb.tbButton.iBitmap;
4261 bi.fsState = tb.tbButton.fsState;
4262 bi.fsStyle = tb.tbButton.fsStyle;
4263 bi.lParam = tb.tbButton.dwData;
4265 TOOLBAR_SetButtonInfo( infoPtr, tb.tbButton.idCommand, &bi, TRUE );
4267 TOOLBAR_SendNotify( &hdr, infoPtr, TBN_ENDADJUST );
4269 /* remove all uninitialised buttons
4270 * note: loop backwards to avoid having to fixup i on a
4271 * delete */
4272 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4273 if (infoPtr->buttons[i].iBitmap == -1)
4274 TOOLBAR_DeleteButton(infoPtr, i);
4276 /* only indicate success if at least one button survived */
4277 if (infoPtr->nNumButtons > 0) ret = TRUE;
4280 Free (nmtbr.pData);
4281 RegCloseKey(hkey);
4283 return ret;
4287 static LRESULT
4288 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4290 if (lpSave == NULL) return 0;
4292 if (wParam)
4293 return TOOLBAR_Save(infoPtr, lpSave);
4294 else
4295 return TOOLBAR_Restore(infoPtr, lpSave);
4299 static LRESULT
4300 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4302 LPWSTR pszValueName = 0, pszSubKey = 0;
4303 TBSAVEPARAMSW SaveW;
4304 LRESULT result = 0;
4305 int len;
4307 if (lpSave == NULL) return 0;
4309 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4310 pszSubKey = Alloc(len * sizeof(WCHAR));
4311 if (!pszSubKey) goto exit;
4312 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4314 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4315 pszValueName = Alloc(len * sizeof(WCHAR));
4316 if (!pszValueName) goto exit;
4317 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4319 SaveW.pszValueName = pszValueName;
4320 SaveW.pszSubKey = pszSubKey;
4321 SaveW.hkr = lpSave->hkr;
4322 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4324 exit:
4325 Free (pszValueName);
4326 Free (pszSubKey);
4328 return result;
4332 static LRESULT
4333 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4335 BOOL bOldAnchor = infoPtr->bAnchor;
4337 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4339 infoPtr->bAnchor = bAnchor;
4341 /* Native does not remove the hot effect from an already hot button */
4343 return (LRESULT)bOldAnchor;
4347 static LRESULT
4348 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4350 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4351 short width = (short)LOWORD(lParam);
4352 short height = (short)HIWORD(lParam);
4354 TRACE("hwnd=%p, wParam=%ld, size %d x %d\n", infoPtr->hwndSelf, wParam, width, height);
4356 if (wParam != 0)
4357 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4359 /* 0 width or height is changed to 1 */
4360 if (width == 0)
4361 width = 1;
4362 if (height == 0)
4363 height = 1;
4365 if (infoPtr->nNumButtons > 0)
4366 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4367 infoPtr->nNumButtons,
4368 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4370 if (width < -1 || height < -1)
4372 /* Windows destroys the imagelist and seems to actually use negative
4373 * values to compute button sizes */
4374 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4375 return FALSE;
4378 /* width or height of -1 means no change */
4379 if (width != -1)
4380 infoPtr->nBitmapWidth = width;
4381 if (height != -1)
4382 infoPtr->nBitmapHeight = height;
4384 if ((himlDef == infoPtr->himlInt) &&
4385 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4387 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4388 infoPtr->nBitmapHeight);
4391 TOOLBAR_CalcToolbar(infoPtr);
4392 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4393 return TRUE;
4397 static LRESULT
4398 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4399 const TBBUTTONINFOW *lptbbi, BOOL isW)
4401 TBUTTON_INFO *btnPtr;
4402 INT nIndex;
4403 RECT oldBtnRect;
4405 if (lptbbi == NULL)
4406 return FALSE;
4407 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4408 return FALSE;
4410 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4411 if (nIndex == -1)
4412 return FALSE;
4414 btnPtr = &infoPtr->buttons[nIndex];
4415 if (lptbbi->dwMask & TBIF_COMMAND)
4416 btnPtr->idCommand = lptbbi->idCommand;
4417 if (lptbbi->dwMask & TBIF_IMAGE)
4418 btnPtr->iBitmap = lptbbi->iImage;
4419 if (lptbbi->dwMask & TBIF_LPARAM)
4420 btnPtr->dwData = lptbbi->lParam;
4421 if (lptbbi->dwMask & TBIF_SIZE)
4422 btnPtr->cx = lptbbi->cx;
4423 if (lptbbi->dwMask & TBIF_STATE)
4424 btnPtr->fsState = lptbbi->fsState;
4425 if (lptbbi->dwMask & TBIF_STYLE)
4426 btnPtr->fsStyle = lptbbi->fsStyle;
4428 if (lptbbi->dwMask & TBIF_TEXT)
4429 set_stringT( btnPtr, lptbbi->pszText, isW );
4431 /* save the button rect to see if we need to redraw the whole toolbar */
4432 oldBtnRect = btnPtr->rect;
4433 TOOLBAR_LayoutToolbar(infoPtr);
4435 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4436 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4437 else
4438 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4440 return TRUE;
4444 static LRESULT
4445 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4447 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4448 int top = default_top_margin(infoPtr);
4450 if ((cx < 0) || (cy < 0))
4452 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4453 return FALSE;
4456 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4458 /* The documentation claims you can only change the button size before
4459 * any button has been added. But this is wrong.
4460 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4461 * it to the toolbar, and it checks that the return value is nonzero - mjm
4462 * Further testing shows that we must actually perform the change too.
4465 * The documentation also does not mention that if 0 is supplied for
4466 * either size, the system changes it to the default of 24 wide and
4467 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4469 if (cx == 0) cx = 24;
4470 if (cy == 0) cy = 22;
4472 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4473 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4475 if (cx != infoPtr->nButtonWidth || cy != infoPtr->nButtonHeight ||
4476 top != infoPtr->iTopMargin)
4478 infoPtr->nButtonWidth = cx;
4479 infoPtr->nButtonHeight = cy;
4480 infoPtr->iTopMargin = top;
4482 TOOLBAR_LayoutToolbar( infoPtr );
4483 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
4485 return TRUE;
4489 static LRESULT
4490 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4492 /* if setting to current values, ignore */
4493 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4494 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4495 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4496 infoPtr->cxMin, infoPtr->cxMax);
4497 return TRUE;
4500 /* save new values */
4501 infoPtr->cxMin = (short)LOWORD(lParam);
4502 infoPtr->cxMax = (short)HIWORD(lParam);
4504 /* otherwise we need to recalc the toolbar and in some cases
4505 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4506 which doesn't actually draw - GA). */
4507 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4508 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4510 TOOLBAR_CalcToolbar (infoPtr);
4512 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4514 return TRUE;
4518 static LRESULT
4519 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4521 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4522 return FALSE;
4524 infoPtr->buttons[nIndex].idCommand = nId;
4526 if (infoPtr->hwndToolTip) {
4528 FIXME("change tool tip\n");
4532 return TRUE;
4536 static LRESULT
4537 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4539 HIMAGELIST himlTemp;
4540 INT id = 0;
4542 if (infoPtr->iVersion >= 5)
4543 id = wParam;
4545 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4546 &infoPtr->cimlDis, himl, id);
4548 /* FIXME: redraw ? */
4550 return (LRESULT)himlTemp;
4554 static LRESULT
4555 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD flags)
4557 DWORD old_flags;
4559 TRACE("hwnd = %p, mask = 0x%08x, flags = 0x%08x\n", infoPtr->hwndSelf, mask, flags);
4561 old_flags = infoPtr->dwDTFlags;
4562 infoPtr->dwDTFlags = (old_flags & ~mask) | (flags & mask);
4564 return (LRESULT)old_flags;
4567 /* This function differs a bit from what MSDN says it does:
4568 * 1. lParam contains extended style flags to OR with current style
4569 * (MSDN isn't clear on the OR bit)
4570 * 2. wParam appears to contain extended style flags to be reset
4571 * (MSDN says that this parameter is reserved)
4573 static LRESULT
4574 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4576 DWORD old_style = infoPtr->dwExStyle;
4578 TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
4580 if (mask)
4581 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4582 else
4583 infoPtr->dwExStyle = style;
4585 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4586 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4587 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4589 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4590 TOOLBAR_CalcToolbar(infoPtr);
4591 else
4592 TOOLBAR_LayoutToolbar(infoPtr);
4594 TOOLBAR_AutoSize(infoPtr);
4595 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4597 return old_style;
4601 static LRESULT
4602 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4604 HIMAGELIST himlTemp;
4605 INT id = 0;
4607 if (infoPtr->iVersion >= 5)
4608 id = wParam;
4610 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4612 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4613 &infoPtr->cimlHot, himl, id);
4615 /* FIXME: redraw ? */
4617 return (LRESULT)himlTemp;
4621 /* Makes previous hot button no longer hot, makes the specified
4622 * button hot and sends appropriate notifications. dwReason is one or
4623 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4624 * NOTE 1: this function does not validate nHit
4625 * NOTE 2: the name of this function is completely made up and
4626 * not based on any documentation from Microsoft. */
4627 static void
4628 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4630 if (infoPtr->nHotItem != nHit)
4632 NMTBHOTITEM nmhotitem;
4633 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4635 nmhotitem.dwFlags = dwReason;
4636 if(infoPtr->nHotItem >= 0)
4638 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4639 nmhotitem.idOld = oldBtnPtr->idCommand;
4641 else
4643 nmhotitem.dwFlags |= HICF_ENTERING;
4644 nmhotitem.idOld = 0;
4647 if (nHit >= 0)
4649 btnPtr = &infoPtr->buttons[nHit];
4650 nmhotitem.idNew = btnPtr->idCommand;
4652 else
4654 nmhotitem.dwFlags |= HICF_LEAVING;
4655 nmhotitem.idNew = 0;
4658 /* now change the hot and invalidate the old and new buttons - if the
4659 * parent agrees */
4660 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4662 if (oldBtnPtr) {
4663 oldBtnPtr->bHot = FALSE;
4664 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4666 /* setting disabled buttons as hot fails even if the notify contains the button id */
4667 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4668 btnPtr->bHot = TRUE;
4669 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4670 infoPtr->nHotItem = nHit;
4672 else
4673 infoPtr->nHotItem = -1;
4678 static LRESULT
4679 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4681 INT nOldHotItem = infoPtr->nHotItem;
4683 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4685 if (nHotItem >= infoPtr->nNumButtons)
4686 return infoPtr->nHotItem;
4688 if (nHotItem < 0)
4689 nHotItem = -1;
4691 /* NOTE: an application can still remove the hot item even if anchor
4692 * highlighting is enabled */
4694 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4696 if (nOldHotItem < 0)
4697 return -1;
4699 return (LRESULT)nOldHotItem;
4703 static LRESULT
4704 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4706 HIMAGELIST himlTemp;
4707 INT oldButtonWidth = infoPtr->nButtonWidth;
4708 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4709 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4710 INT i, id = 0;
4712 if (infoPtr->iVersion >= 5)
4713 id = wParam;
4715 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4716 &infoPtr->cimlDef, himl, id);
4718 infoPtr->nNumBitmaps = 0;
4719 for (i = 0; i < infoPtr->cimlDef; i++)
4720 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4722 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4723 &infoPtr->nBitmapHeight))
4725 infoPtr->nBitmapWidth = 1;
4726 infoPtr->nBitmapHeight = 1;
4728 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4730 TOOLBAR_CalcToolbar(infoPtr);
4731 if (infoPtr->nButtonWidth < oldButtonWidth)
4732 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4735 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4736 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4737 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4739 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4741 return (LRESULT)himlTemp;
4745 static LRESULT
4746 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4748 infoPtr->nIndent = nIndent;
4750 TRACE("\n");
4752 /* process only on indent changing */
4753 if(infoPtr->nIndent != nIndent)
4755 infoPtr->nIndent = nIndent;
4756 TOOLBAR_CalcToolbar (infoPtr);
4757 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4760 return TRUE;
4764 static LRESULT
4765 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4767 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4769 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4771 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4772 return 0;
4775 if ((lptbim->iButton == -1) ||
4776 ((lptbim->iButton < infoPtr->nNumButtons) &&
4777 (lptbim->iButton >= 0)))
4779 infoPtr->tbim = *lptbim;
4780 /* FIXME: don't need to update entire toolbar */
4781 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4783 else
4784 ERR("Invalid button index %d\n", lptbim->iButton);
4786 return 0;
4790 static LRESULT
4791 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4793 infoPtr->clrInsertMark = clr;
4795 /* FIXME: don't need to update entire toolbar */
4796 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4798 return 0;
4802 static LRESULT
4803 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4805 infoPtr->nMaxTextRows = nMaxRows;
4807 TOOLBAR_CalcToolbar(infoPtr);
4808 return TRUE;
4812 /* MSDN gives slightly wrong info on padding.
4813 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4814 * 2. It is not used to create a blank area between the edge of the button
4815 * and the text or image if TBSTYLE_LIST is set. It is used to control
4816 * the gap between the image and text.
4817 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4818 * to control the bottom and right borders [with the border being
4819 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4820 * is shared evenly on both sides of the button.
4821 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4823 static LRESULT
4824 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4826 DWORD oldPad;
4828 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4829 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4830 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4831 TRACE("cx=%d, cy=%d\n",
4832 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4833 return (LRESULT) oldPad;
4837 static LRESULT
4838 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4840 HWND hwndOldNotify;
4842 TRACE("\n");
4844 hwndOldNotify = infoPtr->hwndNotify;
4845 infoPtr->hwndNotify = hParent;
4847 return (LRESULT)hwndOldNotify;
4851 static LRESULT
4852 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4854 int rows = LOWORD(wParam);
4855 BOOL bLarger = HIWORD(wParam);
4857 TRACE("\n");
4859 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4861 if(infoPtr->nRows != rows)
4863 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4864 int curColumn = 0; /* Current column */
4865 int curRow = 0; /* Current row */
4866 int hidden = 0; /* Number of hidden buttons */
4867 int seps = 0; /* Number of separators */
4868 int idealWrap = 0; /* Ideal wrap point */
4869 int i;
4870 BOOL wrap;
4873 Calculate new size and wrap points - Under windows, setrows will
4874 change the dimensions if needed to show the number of requested
4875 rows (if CCS_NORESIZE is set), or will take up the whole window
4876 (if no CCS_NORESIZE).
4878 Basic algorithm - If N buttons, and y rows requested, each row
4879 contains N/y buttons.
4881 FIXME: Handling of separators not obvious from testing results
4882 FIXME: Take width of window into account?
4885 /* Loop through the buttons one by one counting key items */
4886 for (i = 0; i < infoPtr->nNumButtons; i++ )
4888 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4889 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4890 hidden++;
4891 else if (btnPtr[i].fsStyle & BTNS_SEP)
4892 seps++;
4895 /* FIXME: Separators make this quite complex */
4896 if (seps) FIXME("Separators unhandled\n");
4898 /* Round up so more per line, i.e., less rows */
4899 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4901 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4902 achieve the requested number of rows. */
4903 if (bLarger && idealWrap > 1)
4905 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4906 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4908 if (resRows < rows && moreRows > rows)
4910 idealWrap--;
4911 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4915 curColumn = curRow = 0;
4916 wrap = FALSE;
4917 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4918 infoPtr->nNumButtons, hidden, rows);
4920 for (i = 0; i < infoPtr->nNumButtons; i++ )
4922 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4923 continue;
4925 /* Step on, wrap if necessary or flag next to wrap */
4926 if (!wrap) {
4927 curColumn++;
4928 } else {
4929 wrap = FALSE;
4930 curColumn = 1;
4931 curRow++;
4934 if (curColumn > (idealWrap-1)) {
4935 wrap = TRUE;
4936 btnPtr[i].fsState |= TBSTATE_WRAP;
4940 TRACE("Result - %d rows\n", curRow + 1);
4942 /* recalculate toolbar */
4943 TOOLBAR_CalcToolbar (infoPtr);
4945 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4946 if NORESIZE is NOT set, then the toolbar will always be resized to
4947 take up the whole window. With it set, sizing needs to be manual. */
4948 if (infoPtr->dwStyle & CCS_NORESIZE) {
4949 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4950 infoPtr->rcBound.right - infoPtr->rcBound.left,
4951 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4952 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4955 /* repaint toolbar */
4956 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4959 /* return bounding rectangle */
4960 if (lprc) {
4961 lprc->left = infoPtr->rcBound.left;
4962 lprc->right = infoPtr->rcBound.right;
4963 lprc->top = infoPtr->rcBound.top;
4964 lprc->bottom = infoPtr->rcBound.bottom;
4967 return 0;
4971 static LRESULT
4972 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4974 TBUTTON_INFO *btnPtr;
4975 INT nIndex;
4977 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4978 if (nIndex == -1)
4979 return FALSE;
4981 btnPtr = &infoPtr->buttons[nIndex];
4983 /* if hidden state has changed the invalidate entire window and recalc */
4984 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4985 btnPtr->fsState = LOWORD(lParam);
4986 TOOLBAR_CalcToolbar (infoPtr);
4987 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4988 return TRUE;
4991 /* process state changing if current state doesn't match new state */
4992 if(btnPtr->fsState != LOWORD(lParam))
4994 btnPtr->fsState = LOWORD(lParam);
4995 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4998 return TRUE;
5001 static inline void unwrap(TOOLBAR_INFO *info)
5003 int i;
5005 for (i = 0; i < info->nNumButtons; i++)
5006 info->buttons[i].fsState &= ~TBSTATE_WRAP;
5009 static LRESULT
5010 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
5012 DWORD dwOldStyle = infoPtr->dwStyle;
5014 TRACE("new style 0x%08x\n", style);
5016 if (style & TBSTYLE_LIST)
5017 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
5018 else
5019 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
5021 infoPtr->dwStyle = style;
5022 TOOLBAR_CheckStyle(infoPtr);
5024 if ((dwOldStyle ^ style) & TBSTYLE_WRAPABLE)
5026 if (dwOldStyle & TBSTYLE_WRAPABLE)
5027 unwrap(infoPtr);
5028 TOOLBAR_CalcToolbar(infoPtr);
5030 else if ((dwOldStyle ^ style) & CCS_VERT)
5031 TOOLBAR_LayoutToolbar(infoPtr);
5033 /* only resize if one of the CCS_* styles was changed */
5034 if ((dwOldStyle ^ style) & COMMON_STYLES)
5036 TOOLBAR_AutoSize(infoPtr);
5037 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5040 return 0;
5044 static inline LRESULT
5045 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
5047 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
5049 infoPtr->hwndToolTip = hwndTooltip;
5050 return 0;
5054 static LRESULT
5055 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
5057 BOOL bTemp;
5059 TRACE("%s hwnd=%p\n",
5060 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
5062 bTemp = infoPtr->bUnicode;
5063 infoPtr->bUnicode = (BOOL)wParam;
5065 return bTemp;
5069 static LRESULT
5070 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
5072 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5073 comctl32_color.clrBtnHighlight :
5074 infoPtr->clrBtnHighlight;
5075 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5076 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5077 return 1;
5081 static LRESULT
5082 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
5084 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5085 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5086 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5088 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5089 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5090 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5091 return 0;
5095 static LRESULT
5096 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
5098 INT iOldVersion = infoPtr->iVersion;
5100 infoPtr->iVersion = iVersion;
5102 if (infoPtr->iVersion >= 5)
5103 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
5105 return iOldVersion;
5109 static LRESULT
5110 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
5112 WORD iString = HIWORD(wParam);
5113 WORD buffersize = LOWORD(wParam);
5114 LRESULT ret = -1;
5116 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
5118 if (iString < infoPtr->nNumStrings)
5120 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5121 ret--;
5123 TRACE("returning %s\n", debugstr_a(str));
5125 else
5126 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5128 return ret;
5132 static LRESULT
5133 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5135 WORD iString = HIWORD(wParam);
5136 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5137 LRESULT ret = -1;
5139 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5141 if (iString < infoPtr->nNumStrings)
5143 len = min(len, strlenW(infoPtr->strings[iString]));
5144 ret = (len+1)*sizeof(WCHAR);
5145 if (str)
5147 memcpy(str, infoPtr->strings[iString], ret);
5148 str[len] = '\0';
5150 ret = len;
5152 TRACE("returning %s\n", debugstr_w(str));
5154 else
5155 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5157 return ret;
5160 static LRESULT TOOLBAR_SetBoundingSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
5162 SIZE * pSize = (SIZE*)lParam;
5163 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub\n", hwnd, wParam, pSize->cx, pSize->cy);
5164 return 0;
5167 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5168 * caller to specify a reason why the hot item changed (rather than just the
5169 * HICF_OTHER that TB_SETHOTITEM sends). */
5170 static LRESULT
5171 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5173 INT nOldHotItem = infoPtr->nHotItem;
5175 TRACE("old item=%d, new item=%d, flags=%08x\n",
5176 nOldHotItem, nHotItem, (DWORD)lParam);
5178 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5179 nHotItem = -1;
5181 /* NOTE: an application can still remove the hot item even if anchor
5182 * highlighting is enabled */
5184 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5186 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5189 /* Sets the toolbar global iListGap parameter which controls the amount of
5190 * spacing between the image and the text of buttons for TBSTYLE_LIST
5191 * toolbars. */
5192 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5194 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5196 infoPtr->iListGap = iListGap;
5198 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5200 return 0;
5203 /* Returns the number of maximum number of image lists associated with the
5204 * various states. */
5205 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5207 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5209 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5212 static LRESULT
5213 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5215 LPSIZE lpsize = (LPSIZE)lParam;
5217 if (lpsize == NULL)
5218 return FALSE;
5221 * Testing shows the following:
5222 * wParam = 0 adjust cx value
5223 * = 1 set cy value to max size.
5224 * lParam pointer to SIZE structure
5227 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5228 wParam, lParam, lpsize->cx, lpsize->cy);
5230 switch(wParam) {
5231 case 0:
5232 if (lpsize->cx == -1) {
5233 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5235 else if(HIWORD(lpsize->cx)) {
5236 RECT rc;
5237 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5239 GetWindowRect(infoPtr->hwndSelf, &rc);
5240 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5241 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5242 lpsize->cx = max(rc.right-rc.left,
5243 infoPtr->rcBound.right - infoPtr->rcBound.left);
5245 else {
5246 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5248 break;
5249 case 1:
5250 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5251 break;
5252 default:
5253 FIXME("Unknown wParam %ld\n", wParam);
5254 return 0;
5256 TRACE("set to -> 0x%08x 0x%08x\n",
5257 lpsize->cx, lpsize->cy);
5258 return 1;
5261 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5263 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5265 InvalidateRect(hwnd, NULL, TRUE);
5266 return 1;
5270 static LRESULT
5271 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5273 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5274 LOGFONTW logFont;
5276 TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
5278 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5279 GetClientRect(hwnd, &infoPtr->client_rect);
5280 infoPtr->bUnicode = infoPtr->hwndNotify &&
5281 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5282 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5284 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5285 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5287 OpenThemeData (hwnd, themeClass);
5289 TOOLBAR_CheckStyle (infoPtr);
5291 return 0;
5295 static LRESULT
5296 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5298 INT i;
5300 /* delete tooltip control */
5301 if (infoPtr->hwndToolTip)
5302 DestroyWindow (infoPtr->hwndToolTip);
5304 /* delete temporary buffer for tooltip text */
5305 Free (infoPtr->pszTooltipText);
5306 Free (infoPtr->bitmaps); /* bitmaps list */
5308 /* delete button data */
5309 for (i = 0; i < infoPtr->nNumButtons; i++)
5310 free_string( infoPtr->buttons + i );
5311 Free (infoPtr->buttons);
5313 /* delete strings */
5314 if (infoPtr->strings) {
5315 for (i = 0; i < infoPtr->nNumStrings; i++)
5316 Free (infoPtr->strings[i]);
5318 Free (infoPtr->strings);
5321 /* destroy internal image list */
5322 if (infoPtr->himlInt)
5323 ImageList_Destroy (infoPtr->himlInt);
5325 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5326 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5327 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5329 /* delete default font */
5330 DeleteObject (infoPtr->hDefaultFont);
5332 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
5334 /* free toolbar info data */
5335 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5336 Free (infoPtr);
5338 return 0;
5342 static LRESULT
5343 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5345 NMTBCUSTOMDRAW tbcd;
5346 INT ret = FALSE;
5347 DWORD ntfret;
5348 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5349 DWORD dwEraseCustDraw = 0;
5351 /* the app has told us not to redraw the toolbar */
5352 if (!infoPtr->bDoRedraw)
5353 return FALSE;
5355 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5356 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5357 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5358 tbcd.nmcd.hdc = (HDC)wParam;
5359 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5360 dwEraseCustDraw = ntfret & 0xffff;
5362 /* FIXME: in general the return flags *can* be or'ed together */
5363 switch (dwEraseCustDraw)
5365 case CDRF_DODEFAULT:
5366 break;
5367 case CDRF_SKIPDEFAULT:
5368 return TRUE;
5369 default:
5370 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5371 infoPtr->hwndSelf, ntfret);
5375 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5376 * to my parent for processing.
5378 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5379 POINT pt, ptorig;
5380 HDC hdc = (HDC)wParam;
5381 HWND parent;
5383 pt.x = 0;
5384 pt.y = 0;
5385 parent = GetParent(infoPtr->hwndSelf);
5386 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5387 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5388 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5389 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5391 if (!ret)
5392 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5394 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5395 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5396 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5397 tbcd.nmcd.hdc = (HDC)wParam;
5398 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5399 dwEraseCustDraw = ntfret & 0xffff;
5400 switch (dwEraseCustDraw)
5402 case CDRF_DODEFAULT:
5403 break;
5404 case CDRF_SKIPDEFAULT:
5405 return TRUE;
5406 default:
5407 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5408 infoPtr->hwndSelf, ntfret);
5411 return ret;
5415 static inline LRESULT
5416 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5418 return (LRESULT)infoPtr->hFont;
5422 static void
5423 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5425 INT i;
5426 INT nNewHotItem = infoPtr->nHotItem;
5428 for (i = 0; i < infoPtr->nNumButtons; i++)
5430 /* did we wrap? */
5431 if ((nNewHotItem + iDirection < 0) ||
5432 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5434 NMTBWRAPHOTITEM nmtbwhi;
5435 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5436 nmtbwhi.iDirection = iDirection;
5437 nmtbwhi.dwReason = dwReason;
5439 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5440 return;
5443 nNewHotItem += iDirection;
5444 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5446 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5447 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5449 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5450 break;
5455 static LRESULT
5456 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5458 NMKEY nmkey;
5460 nmkey.nVKey = (UINT)wParam;
5461 nmkey.uFlags = HIWORD(lParam);
5463 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5464 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5466 switch ((UINT)wParam)
5468 case VK_LEFT:
5469 case VK_UP:
5470 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5471 break;
5472 case VK_RIGHT:
5473 case VK_DOWN:
5474 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5475 break;
5476 case VK_SPACE:
5477 case VK_RETURN:
5478 if ((infoPtr->nHotItem >= 0) &&
5479 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5481 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5482 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5483 (LPARAM)infoPtr->hwndSelf);
5485 break;
5488 return 0;
5492 static LRESULT
5493 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5495 POINT pt;
5496 BOOL button;
5498 pt.x = (short)LOWORD(lParam);
5499 pt.y = (short)HIWORD(lParam);
5500 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5502 if (button)
5503 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5504 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5505 TOOLBAR_Customize (infoPtr);
5507 return 0;
5511 static LRESULT
5512 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5514 TBUTTON_INFO *btnPtr;
5515 POINT pt;
5516 INT nHit;
5517 NMTOOLBARA nmtb;
5518 NMMOUSE nmmouse;
5519 BOOL bDragKeyPressed;
5520 BOOL button;
5522 TRACE("\n");
5524 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5525 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5526 else
5527 bDragKeyPressed = (wParam & MK_SHIFT);
5529 if (infoPtr->hwndToolTip)
5530 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5531 WM_LBUTTONDOWN, wParam, lParam);
5533 pt.x = (short)LOWORD(lParam);
5534 pt.y = (short)HIWORD(lParam);
5535 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5537 if (button)
5539 btnPtr = &infoPtr->buttons[nHit];
5541 if (bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5543 infoPtr->nButtonDrag = nHit;
5544 SetCapture (infoPtr->hwndSelf);
5546 /* If drag cursor has not been loaded, load it.
5547 * Note: it doesn't need to be freed */
5548 if (!hCursorDrag)
5549 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5550 SetCursor(hCursorDrag);
5552 else
5554 RECT arrowRect;
5555 infoPtr->nOldHit = nHit;
5557 arrowRect = btnPtr->rect;
5558 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5560 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5561 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5562 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5563 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5564 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5565 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5567 LRESULT res;
5569 /* draw in pressed state */
5570 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5571 btnPtr->fsState |= TBSTATE_PRESSED;
5572 else
5573 btnPtr->bDropDownPressed = TRUE;
5574 RedrawWindow(infoPtr->hwndSelf, &btnPtr->rect, 0, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5576 memset(&nmtb, 0, sizeof(nmtb));
5577 nmtb.iItem = btnPtr->idCommand;
5578 nmtb.rcButton = btnPtr->rect;
5579 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr, TBN_DROPDOWN);
5580 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5582 if (res != TBDDRET_TREATPRESSED)
5584 MSG msg;
5586 /* redraw button in unpressed state */
5587 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5588 btnPtr->fsState &= ~TBSTATE_PRESSED;
5589 else
5590 btnPtr->bDropDownPressed = FALSE;
5591 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5593 /* find and set hot item */
5594 GetCursorPos(&pt);
5595 ScreenToClient(infoPtr->hwndSelf, &pt);
5596 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5597 if (!infoPtr->bAnchor || button)
5598 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5600 /* remove any left mouse button down or double-click messages
5601 * so that we can get a toggle effect on the button */
5602 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5603 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5606 return 0;
5608 /* otherwise drop through and process as pushed */
5610 infoPtr->bCaptured = TRUE;
5611 infoPtr->nButtonDown = nHit;
5612 infoPtr->bDragOutSent = FALSE;
5614 btnPtr->fsState |= TBSTATE_PRESSED;
5616 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5618 if (btnPtr->fsState & TBSTATE_ENABLED)
5619 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5620 UpdateWindow(infoPtr->hwndSelf);
5621 SetCapture (infoPtr->hwndSelf);
5624 memset(&nmtb, 0, sizeof(nmtb));
5625 nmtb.iItem = btnPtr->idCommand;
5626 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5629 nmmouse.dwHitInfo = nHit;
5631 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5632 if (!button)
5633 nmmouse.dwItemSpec = -1;
5634 else
5636 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5637 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5640 ClientToScreen(infoPtr->hwndSelf, &pt);
5641 nmmouse.pt = pt;
5643 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5644 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5646 return 0;
5649 static LRESULT
5650 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5652 TBUTTON_INFO *btnPtr;
5653 POINT pt;
5654 INT nHit;
5655 INT nOldIndex = -1;
5656 NMHDR hdr;
5657 NMMOUSE nmmouse;
5658 NMTOOLBARA nmtb;
5659 BOOL button;
5661 if (infoPtr->hwndToolTip)
5662 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5663 WM_LBUTTONUP, wParam, lParam);
5665 pt.x = (short)LOWORD(lParam);
5666 pt.y = (short)HIWORD(lParam);
5667 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5669 if (!infoPtr->bAnchor || button)
5670 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5672 if (infoPtr->nButtonDrag >= 0) {
5673 RECT rcClient;
5674 NMHDR hdr;
5676 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5677 ReleaseCapture();
5678 /* reset cursor */
5679 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5681 GetClientRect(infoPtr->hwndSelf, &rcClient);
5682 if (PtInRect(&rcClient, pt))
5684 INT nButton = -1;
5685 if (nHit >= 0)
5686 nButton = nHit;
5687 else if (nHit < -1)
5688 nButton = -nHit;
5689 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5690 nButton = -nHit;
5692 if (nButton == infoPtr->nButtonDrag)
5694 /* if the button is moved sightly left and we have a
5695 * separator there then remove it */
5696 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5698 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5699 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5701 else /* else insert a separator before the dragged button */
5703 TBBUTTON tbb;
5704 memset(&tbb, 0, sizeof(tbb));
5705 tbb.fsStyle = BTNS_SEP;
5706 tbb.iString = -1;
5707 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5710 else
5712 if (nButton == -1)
5714 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5715 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5716 else
5717 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5719 else
5720 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5723 else
5725 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5726 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5729 /* button under cursor changed so need to re-set hot item */
5730 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5731 infoPtr->nButtonDrag = -1;
5733 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5735 else if (infoPtr->nButtonDown >= 0) {
5736 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5737 btnPtr->fsState &= ~TBSTATE_PRESSED;
5739 if (btnPtr->fsStyle & BTNS_CHECK) {
5740 if (btnPtr->fsStyle & BTNS_GROUP) {
5741 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5742 nHit);
5743 if ((nOldIndex != nHit) &&
5744 (nOldIndex != -1))
5745 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5746 btnPtr->fsState |= TBSTATE_CHECKED;
5748 else {
5749 if (btnPtr->fsState & TBSTATE_CHECKED)
5750 btnPtr->fsState &= ~TBSTATE_CHECKED;
5751 else
5752 btnPtr->fsState |= TBSTATE_CHECKED;
5756 if (nOldIndex != -1)
5757 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5760 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5761 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5762 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5764 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5765 ReleaseCapture ();
5766 infoPtr->nButtonDown = -1;
5768 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5769 TOOLBAR_SendNotify (&hdr, infoPtr,
5770 NM_RELEASEDCAPTURE);
5772 memset(&nmtb, 0, sizeof(nmtb));
5773 nmtb.iItem = btnPtr->idCommand;
5774 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5775 TBN_ENDDRAG);
5777 if (btnPtr->fsState & TBSTATE_ENABLED)
5779 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5780 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5782 /* In case we have just been destroyed... */
5783 if(!IsWindow(infoPtr->hwndSelf))
5784 return 0;
5788 /* !!! Undocumented - toolbar at 4.71 level and above sends
5789 * NM_CLICK with the NMMOUSE structure. */
5790 nmmouse.dwHitInfo = nHit;
5792 if (!button)
5793 nmmouse.dwItemSpec = -1;
5794 else
5796 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5797 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5800 ClientToScreen(infoPtr->hwndSelf, &pt);
5801 nmmouse.pt = pt;
5803 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5804 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5806 return 0;
5809 static LRESULT
5810 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5812 INT nHit;
5813 NMMOUSE nmmouse;
5814 POINT pt;
5815 BOOL button;
5817 pt.x = (short)LOWORD(lParam);
5818 pt.y = (short)HIWORD(lParam);
5820 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5821 nmmouse.dwHitInfo = nHit;
5823 if (!button) {
5824 nmmouse.dwItemSpec = -1;
5825 } else {
5826 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5827 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5830 ClientToScreen(infoPtr->hwndSelf, &pt);
5831 nmmouse.pt = pt;
5833 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5834 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5836 return 0;
5839 static LRESULT
5840 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5842 NMHDR nmhdr;
5844 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5845 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5847 return 0;
5850 static LRESULT
5851 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5853 TBUTTON_INFO *btnPtr;
5855 infoPtr->bCaptured = FALSE;
5857 if (infoPtr->nButtonDown >= 0)
5859 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5860 btnPtr->fsState &= ~TBSTATE_PRESSED;
5862 infoPtr->nOldHit = -1;
5864 if (btnPtr->fsState & TBSTATE_ENABLED)
5865 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5867 return 0;
5870 static LRESULT
5871 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5873 /* don't remove hot effects when in anchor highlighting mode or when a
5874 * drop-down button is pressed */
5875 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5877 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5878 if (!hotBtnPtr->bDropDownPressed)
5879 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5882 if (infoPtr->nOldHit < 0)
5883 return TRUE;
5885 /* If the last button we were over is depressed then make it not */
5886 /* depressed and redraw it */
5887 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5889 TBUTTON_INFO *btnPtr;
5890 RECT rc1;
5892 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5894 btnPtr->fsState &= ~TBSTATE_PRESSED;
5896 rc1 = btnPtr->rect;
5897 InflateRect (&rc1, 1, 1);
5898 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5901 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5903 NMTOOLBARW nmt;
5904 ZeroMemory(&nmt, sizeof(nmt));
5905 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5906 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5907 infoPtr->bDragOutSent = TRUE;
5910 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5912 return TRUE;
5915 static LRESULT
5916 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5918 POINT pt;
5919 TRACKMOUSEEVENT trackinfo;
5920 INT nHit;
5921 TBUTTON_INFO *btnPtr;
5922 BOOL button;
5924 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5925 TOOLBAR_TooltipCreateControl(infoPtr);
5927 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5928 /* fill in the TRACKMOUSEEVENT struct */
5929 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5930 trackinfo.dwFlags = TME_QUERY;
5932 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5933 _TrackMouseEvent(&trackinfo);
5935 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5936 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5937 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5938 trackinfo.hwndTrack = infoPtr->hwndSelf;
5940 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5941 /* and can properly deactivate the hot toolbar button */
5942 _TrackMouseEvent(&trackinfo);
5946 if (infoPtr->hwndToolTip)
5947 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5948 WM_MOUSEMOVE, wParam, lParam);
5950 pt.x = (short)LOWORD(lParam);
5951 pt.y = (short)HIWORD(lParam);
5953 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5955 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5956 && (!infoPtr->bAnchor || button))
5957 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5959 if (infoPtr->nOldHit != nHit)
5961 if (infoPtr->bCaptured)
5963 if (!infoPtr->bDragOutSent)
5965 NMTOOLBARW nmt;
5966 ZeroMemory(&nmt, sizeof(nmt));
5967 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5968 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5969 infoPtr->bDragOutSent = TRUE;
5972 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5973 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5974 btnPtr->fsState &= ~TBSTATE_PRESSED;
5975 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5977 else if (nHit == infoPtr->nButtonDown) {
5978 btnPtr->fsState |= TBSTATE_PRESSED;
5979 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5981 infoPtr->nOldHit = nHit;
5985 return 0;
5989 static inline LRESULT
5990 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5992 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
5993 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5994 /* else */
5995 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5999 static inline LRESULT
6000 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
6002 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6003 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6005 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6009 static LRESULT
6010 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
6012 TOOLBAR_INFO *infoPtr;
6013 DWORD styleadd = 0;
6015 /* allocate memory for info structure */
6016 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
6017 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6019 /* paranoid!! */
6020 infoPtr->dwStructSize = sizeof(TBBUTTON);
6021 infoPtr->nRows = 1;
6023 /* initialize info structure */
6024 infoPtr->nButtonWidth = 23;
6025 infoPtr->nButtonHeight = 22;
6026 infoPtr->nBitmapHeight = 16;
6027 infoPtr->nBitmapWidth = 16;
6029 infoPtr->nMaxTextRows = 1;
6030 infoPtr->cxMin = -1;
6031 infoPtr->cxMax = -1;
6032 infoPtr->nNumBitmaps = 0;
6033 infoPtr->nNumStrings = 0;
6035 infoPtr->bCaptured = FALSE;
6036 infoPtr->nButtonDown = -1;
6037 infoPtr->nButtonDrag = -1;
6038 infoPtr->nOldHit = -1;
6039 infoPtr->nHotItem = -1;
6040 infoPtr->hwndNotify = lpcs->hwndParent;
6041 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
6042 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
6043 infoPtr->bDragOutSent = FALSE;
6044 infoPtr->iVersion = 0;
6045 infoPtr->hwndSelf = hwnd;
6046 infoPtr->bDoRedraw = TRUE;
6047 infoPtr->clrBtnHighlight = CLR_DEFAULT;
6048 infoPtr->clrBtnShadow = CLR_DEFAULT;
6049 infoPtr->szPadding.cx = DEFPAD_CX;
6050 infoPtr->szPadding.cy = DEFPAD_CY;
6051 infoPtr->iListGap = DEFLISTGAP;
6052 infoPtr->iTopMargin = default_top_margin(infoPtr);
6053 infoPtr->dwStyle = lpcs->style;
6054 infoPtr->tbim.iButton = -1;
6056 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6057 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6058 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6059 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6062 /* I think the code below is a bug, but it is the way that the native
6063 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6064 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6065 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6066 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6067 * Somehow, the only cases of this seem to be MFC programs.
6069 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6070 * does not occur in the WM_STYLECHANGING routine.
6071 * (Guy Albertelli 9/2001)
6074 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
6075 && !(lpcs->style & TBSTYLE_TRANSPARENT))
6076 styleadd |= TBSTYLE_TRANSPARENT;
6077 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
6078 styleadd |= CCS_TOP; /* default to top */
6079 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
6082 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
6086 static LRESULT
6087 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6089 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6090 RECT rcWindow;
6091 HDC hdc;
6093 if (dwStyle & WS_MINIMIZE)
6094 return 0; /* Nothing to do */
6096 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6098 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6099 return 0;
6101 if (!(dwStyle & CCS_NODIVIDER))
6103 GetWindowRect (hwnd, &rcWindow);
6104 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6105 if( dwStyle & WS_BORDER )
6106 InflateRect (&rcWindow, -1, -1);
6107 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6110 ReleaseDC( hwnd, hdc );
6112 return 0;
6116 /* handles requests from the tooltip control on what text to display */
6117 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6119 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6121 TRACE("button index = %d\n", index);
6123 Free (infoPtr->pszTooltipText);
6124 infoPtr->pszTooltipText = NULL;
6126 if (index < 0)
6127 return 0;
6129 if (infoPtr->bUnicode)
6131 WCHAR wszBuffer[INFOTIPSIZE+1];
6132 NMTBGETINFOTIPW tbgit;
6133 unsigned int len; /* in chars */
6135 wszBuffer[0] = '\0';
6136 wszBuffer[INFOTIPSIZE] = '\0';
6138 tbgit.pszText = wszBuffer;
6139 tbgit.cchTextMax = INFOTIPSIZE;
6140 tbgit.iItem = lpnmtdi->hdr.idFrom;
6141 tbgit.lParam = infoPtr->buttons[index].dwData;
6143 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6145 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6147 len = strlenW(tbgit.pszText);
6148 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6150 /* need to allocate temporary buffer in infoPtr as there
6151 * isn't enough space in buffer passed to us by the
6152 * tooltip control */
6153 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6154 if (infoPtr->pszTooltipText)
6156 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6157 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6158 return 0;
6161 else if (len > 0)
6163 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6164 return 0;
6167 else
6169 CHAR szBuffer[INFOTIPSIZE+1];
6170 NMTBGETINFOTIPA tbgit;
6171 unsigned int len; /* in chars */
6173 szBuffer[0] = '\0';
6174 szBuffer[INFOTIPSIZE] = '\0';
6176 tbgit.pszText = szBuffer;
6177 tbgit.cchTextMax = INFOTIPSIZE;
6178 tbgit.iItem = lpnmtdi->hdr.idFrom;
6179 tbgit.lParam = infoPtr->buttons[index].dwData;
6181 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6183 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6185 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6186 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6188 /* need to allocate temporary buffer in infoPtr as there
6189 * isn't enough space in buffer passed to us by the
6190 * tooltip control */
6191 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6192 if (infoPtr->pszTooltipText)
6194 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6195 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6196 return 0;
6199 else if (tbgit.pszText && tbgit.pszText[0])
6201 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6202 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6203 return 0;
6207 /* if button has text, but it is not shown then automatically
6208 * use that text as tooltip */
6209 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6210 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6212 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6213 unsigned int len = pszText ? strlenW(pszText) : 0;
6215 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6217 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6219 /* need to allocate temporary buffer in infoPtr as there
6220 * isn't enough space in buffer passed to us by the
6221 * tooltip control */
6222 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6223 if (infoPtr->pszTooltipText)
6225 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6226 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6227 return 0;
6230 else if (len > 0)
6232 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6233 return 0;
6237 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6239 /* last resort: send notification on to app */
6240 /* FIXME: find out what is really used here */
6241 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6245 static inline LRESULT
6246 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6248 switch (lpnmh->code)
6250 case PGN_CALCSIZE:
6252 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6254 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6255 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6256 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6257 lppgc->iWidth);
6259 else {
6260 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6261 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6262 lppgc->iHeight);
6264 return 0;
6267 case PGN_SCROLL:
6269 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6271 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6272 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6273 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6274 lppgs->iScroll, lppgs->iDir);
6275 return 0;
6278 case TTN_GETDISPINFOW:
6279 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6281 case TTN_GETDISPINFOA:
6282 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6283 return 0;
6285 default:
6286 return 0;
6291 static LRESULT
6292 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6294 LRESULT format;
6296 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6298 if (lParam == NF_QUERY)
6299 return NFR_UNICODE;
6301 if (lParam == NF_REQUERY) {
6302 format = SendMessageW(infoPtr->hwndNotify,
6303 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6304 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6305 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6306 format);
6307 format = NFR_ANSI;
6309 return format;
6311 return 0;
6315 static LRESULT
6316 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6318 HDC hdc;
6319 PAINTSTRUCT ps;
6321 /* fill ps.rcPaint with a default rect */
6322 ps.rcPaint = infoPtr->rcBound;
6324 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6326 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6328 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6329 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6331 return 0;
6335 static LRESULT
6336 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6338 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6340 /* make first item hot */
6341 if (infoPtr->nNumButtons > 0)
6342 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6344 return 0;
6347 static LRESULT
6348 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6350 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6352 if (hFont == 0)
6353 infoPtr->hFont = infoPtr->hDefaultFont;
6354 else
6355 infoPtr->hFont = hFont;
6357 TOOLBAR_CalcToolbar(infoPtr);
6359 if (Redraw)
6360 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6361 return 1;
6364 static LRESULT
6365 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6366 /*****************************************************
6368 * Function;
6369 * Handles the WM_SETREDRAW message.
6371 * Documentation:
6372 * According to testing V4.71 of COMCTL32 returns the
6373 * *previous* status of the redraw flag (either 0 or 1)
6374 * instead of the MSDN documented value of 0 if handled.
6375 * (For laughs see the "consistency" with same function
6376 * in rebar.)
6378 *****************************************************/
6380 BOOL oldredraw = infoPtr->bDoRedraw;
6382 TRACE("set to %s\n",
6383 (wParam) ? "TRUE" : "FALSE");
6384 infoPtr->bDoRedraw = (BOOL) wParam;
6385 if (wParam) {
6386 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6388 return (oldredraw) ? 1 : 0;
6392 static LRESULT
6393 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6395 TRACE("sizing toolbar\n");
6397 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6399 RECT delta_width, delta_height, client, dummy;
6400 DWORD min_x, max_x, min_y, max_y;
6401 TBUTTON_INFO *btnPtr;
6402 INT i;
6404 GetClientRect(infoPtr->hwndSelf, &client);
6405 if(client.right > infoPtr->client_rect.right)
6407 min_x = infoPtr->client_rect.right;
6408 max_x = client.right;
6410 else
6412 max_x = infoPtr->client_rect.right;
6413 min_x = client.right;
6415 if(client.bottom > infoPtr->client_rect.bottom)
6417 min_y = infoPtr->client_rect.bottom;
6418 max_y = client.bottom;
6420 else
6422 max_y = infoPtr->client_rect.bottom;
6423 min_y = client.bottom;
6426 SetRect(&delta_width, min_x, 0, max_x, min_y);
6427 SetRect(&delta_height, 0, min_y, max_x, max_y);
6429 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6430 btnPtr = infoPtr->buttons;
6431 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6432 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6433 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6434 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6436 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6437 TOOLBAR_AutoSize(infoPtr);
6438 return 0;
6442 static LRESULT
6443 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6445 if (nType == GWL_STYLE)
6446 return TOOLBAR_SetStyle(infoPtr, lpStyle->styleNew);
6448 return 0;
6452 static LRESULT
6453 TOOLBAR_SysColorChange (void)
6455 COMCTL32_RefreshSysColors();
6457 return 0;
6461 /* update theme after a WM_THEMECHANGED message */
6462 static LRESULT theme_changed (HWND hwnd)
6464 HTHEME theme = GetWindowTheme (hwnd);
6465 CloseThemeData (theme);
6466 OpenThemeData (hwnd, themeClass);
6467 return 0;
6471 static LRESULT WINAPI
6472 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6474 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6476 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6477 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6479 if (!infoPtr && (uMsg != WM_NCCREATE))
6480 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6482 switch (uMsg)
6484 case TB_ADDBITMAP:
6485 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6487 case TB_ADDBUTTONSA:
6488 case TB_ADDBUTTONSW:
6489 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6490 uMsg == TB_ADDBUTTONSW);
6491 case TB_ADDSTRINGA:
6492 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6494 case TB_ADDSTRINGW:
6495 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6497 case TB_AUTOSIZE:
6498 return TOOLBAR_AutoSize (infoPtr);
6500 case TB_BUTTONCOUNT:
6501 return TOOLBAR_ButtonCount (infoPtr);
6503 case TB_BUTTONSTRUCTSIZE:
6504 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6506 case TB_CHANGEBITMAP:
6507 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6509 case TB_CHECKBUTTON:
6510 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6512 case TB_COMMANDTOINDEX:
6513 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6515 case TB_CUSTOMIZE:
6516 return TOOLBAR_Customize (infoPtr);
6518 case TB_DELETEBUTTON:
6519 return TOOLBAR_DeleteButton (infoPtr, wParam);
6521 case TB_ENABLEBUTTON:
6522 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6524 case TB_GETANCHORHIGHLIGHT:
6525 return TOOLBAR_GetAnchorHighlight (infoPtr);
6527 case TB_GETBITMAP:
6528 return TOOLBAR_GetBitmap (infoPtr, wParam);
6530 case TB_GETBITMAPFLAGS:
6531 return TOOLBAR_GetBitmapFlags ();
6533 case TB_GETBUTTON:
6534 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6536 case TB_GETBUTTONINFOA:
6537 case TB_GETBUTTONINFOW:
6538 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6539 uMsg == TB_GETBUTTONINFOW);
6540 case TB_GETBUTTONSIZE:
6541 return TOOLBAR_GetButtonSize (infoPtr);
6543 case TB_GETBUTTONTEXTA:
6544 case TB_GETBUTTONTEXTW:
6545 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6546 uMsg == TB_GETBUTTONTEXTW);
6548 case TB_GETDISABLEDIMAGELIST:
6549 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6551 case TB_GETEXTENDEDSTYLE:
6552 return TOOLBAR_GetExtendedStyle (infoPtr);
6554 case TB_GETHOTIMAGELIST:
6555 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6557 case TB_GETHOTITEM:
6558 return TOOLBAR_GetHotItem (infoPtr);
6560 case TB_GETIMAGELIST:
6561 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6563 case TB_GETINSERTMARK:
6564 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6566 case TB_GETINSERTMARKCOLOR:
6567 return TOOLBAR_GetInsertMarkColor (infoPtr);
6569 case TB_GETITEMRECT:
6570 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6572 case TB_GETMAXSIZE:
6573 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6575 /* case TB_GETOBJECT: */ /* 4.71 */
6577 case TB_GETPADDING:
6578 return TOOLBAR_GetPadding (infoPtr);
6580 case TB_GETRECT:
6581 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6583 case TB_GETROWS:
6584 return TOOLBAR_GetRows (infoPtr);
6586 case TB_GETSTATE:
6587 return TOOLBAR_GetState (infoPtr, wParam);
6589 case TB_GETSTRINGA:
6590 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6592 case TB_GETSTRINGW:
6593 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6595 case TB_GETSTYLE:
6596 return TOOLBAR_GetStyle (infoPtr);
6598 case TB_GETTEXTROWS:
6599 return TOOLBAR_GetTextRows (infoPtr);
6601 case TB_GETTOOLTIPS:
6602 return TOOLBAR_GetToolTips (infoPtr);
6604 case TB_GETUNICODEFORMAT:
6605 return TOOLBAR_GetUnicodeFormat (infoPtr);
6607 case TB_HIDEBUTTON:
6608 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6610 case TB_HITTEST:
6611 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6613 case TB_INDETERMINATE:
6614 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6616 case TB_INSERTBUTTONA:
6617 case TB_INSERTBUTTONW:
6618 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6619 uMsg == TB_INSERTBUTTONW);
6621 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6623 case TB_ISBUTTONCHECKED:
6624 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6626 case TB_ISBUTTONENABLED:
6627 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6629 case TB_ISBUTTONHIDDEN:
6630 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6632 case TB_ISBUTTONHIGHLIGHTED:
6633 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6635 case TB_ISBUTTONINDETERMINATE:
6636 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6638 case TB_ISBUTTONPRESSED:
6639 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6641 case TB_LOADIMAGES:
6642 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6644 case TB_MAPACCELERATORA:
6645 case TB_MAPACCELERATORW:
6646 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6648 case TB_MARKBUTTON:
6649 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6651 case TB_MOVEBUTTON:
6652 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6654 case TB_PRESSBUTTON:
6655 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6657 case TB_REPLACEBITMAP:
6658 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6660 case TB_SAVERESTOREA:
6661 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6663 case TB_SAVERESTOREW:
6664 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6666 case TB_SETANCHORHIGHLIGHT:
6667 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6669 case TB_SETBITMAPSIZE:
6670 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6672 case TB_SETBUTTONINFOA:
6673 case TB_SETBUTTONINFOW:
6674 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6675 uMsg == TB_SETBUTTONINFOW);
6676 case TB_SETBUTTONSIZE:
6677 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6679 case TB_SETBUTTONWIDTH:
6680 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6682 case TB_SETCMDID:
6683 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6685 case TB_SETDISABLEDIMAGELIST:
6686 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6688 case TB_SETDRAWTEXTFLAGS:
6689 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6691 case TB_SETEXTENDEDSTYLE:
6692 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6694 case TB_SETHOTIMAGELIST:
6695 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6697 case TB_SETHOTITEM:
6698 return TOOLBAR_SetHotItem (infoPtr, wParam);
6700 case TB_SETIMAGELIST:
6701 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6703 case TB_SETINDENT:
6704 return TOOLBAR_SetIndent (infoPtr, wParam);
6706 case TB_SETINSERTMARK:
6707 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6709 case TB_SETINSERTMARKCOLOR:
6710 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6712 case TB_SETMAXTEXTROWS:
6713 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6715 case TB_SETPADDING:
6716 return TOOLBAR_SetPadding (infoPtr, lParam);
6718 case TB_SETPARENT:
6719 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6721 case TB_SETROWS:
6722 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6724 case TB_SETSTATE:
6725 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6727 case TB_SETSTYLE:
6728 return TOOLBAR_SetStyle (infoPtr, lParam);
6730 case TB_SETTOOLTIPS:
6731 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6733 case TB_SETUNICODEFORMAT:
6734 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6736 case TB_SETBOUNDINGSIZE:
6737 return TOOLBAR_SetBoundingSize(hwnd, wParam, lParam);
6739 case TB_SETHOTITEM2:
6740 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6742 case TB_SETLISTGAP:
6743 return TOOLBAR_SetListGap(infoPtr, wParam);
6745 case TB_GETIMAGELISTCOUNT:
6746 return TOOLBAR_GetImageListCount(infoPtr);
6748 case TB_GETIDEALSIZE:
6749 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6751 case TB_UNKWN464:
6752 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6754 /* Common Control Messages */
6756 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6757 case CCM_GETCOLORSCHEME:
6758 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6760 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6761 case CCM_SETCOLORSCHEME:
6762 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6764 case CCM_GETVERSION:
6765 return TOOLBAR_GetVersion (infoPtr);
6767 case CCM_SETVERSION:
6768 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6771 /* case WM_CHAR: */
6773 case WM_CREATE:
6774 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6776 case WM_DESTROY:
6777 return TOOLBAR_Destroy (infoPtr);
6779 case WM_ERASEBKGND:
6780 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6782 case WM_GETFONT:
6783 return TOOLBAR_GetFont (infoPtr);
6785 case WM_KEYDOWN:
6786 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6788 /* case WM_KILLFOCUS: */
6790 case WM_LBUTTONDBLCLK:
6791 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6793 case WM_LBUTTONDOWN:
6794 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6796 case WM_LBUTTONUP:
6797 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6799 case WM_RBUTTONUP:
6800 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6802 case WM_RBUTTONDBLCLK:
6803 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6805 case WM_MOUSEMOVE:
6806 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6808 case WM_MOUSELEAVE:
6809 return TOOLBAR_MouseLeave (infoPtr);
6811 case WM_CAPTURECHANGED:
6812 if (hwnd == (HWND)lParam) return 0;
6813 return TOOLBAR_CaptureChanged(infoPtr);
6815 case WM_NCACTIVATE:
6816 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6818 case WM_NCCALCSIZE:
6819 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6821 case WM_NCCREATE:
6822 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6824 case WM_NCPAINT:
6825 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6827 case WM_NOTIFY:
6828 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6830 case WM_NOTIFYFORMAT:
6831 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6833 case WM_PRINTCLIENT:
6834 case WM_PAINT:
6835 return TOOLBAR_Paint (infoPtr, wParam);
6837 case WM_SETFOCUS:
6838 return TOOLBAR_SetFocus (infoPtr);
6840 case WM_SETFONT:
6841 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6843 case WM_SETREDRAW:
6844 return TOOLBAR_SetRedraw (infoPtr, wParam);
6846 case WM_SIZE:
6847 return TOOLBAR_Size (infoPtr);
6849 case WM_STYLECHANGED:
6850 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6852 case WM_SYSCOLORCHANGE:
6853 return TOOLBAR_SysColorChange ();
6855 case WM_THEMECHANGED:
6856 return theme_changed (hwnd);
6858 /* case WM_WININICHANGE: */
6860 case WM_CHARTOITEM:
6861 case WM_COMMAND:
6862 case WM_DRAWITEM:
6863 case WM_MEASUREITEM:
6864 case WM_VKEYTOITEM:
6865 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6867 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6868 case PGM_FORWARDMOUSE:
6869 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6871 default:
6872 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6873 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6874 uMsg, wParam, lParam);
6875 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6880 VOID
6881 TOOLBAR_Register (void)
6883 WNDCLASSW wndClass;
6885 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6886 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6887 wndClass.lpfnWndProc = ToolbarWindowProc;
6888 wndClass.cbClsExtra = 0;
6889 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6890 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6891 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6892 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6894 RegisterClassW (&wndClass);
6898 VOID
6899 TOOLBAR_Unregister (void)
6901 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6904 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6906 HIMAGELIST himlold;
6907 PIMLENTRY c = NULL;
6909 /* Check if the entry already exists */
6910 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6912 /* Don't add new entry for NULL imagelist */
6913 if (!c && !himl)
6914 return NULL;
6916 /* If this is a new entry we must create it and insert into the array */
6917 if (!c)
6919 PIMLENTRY *pnies;
6921 c = Alloc(sizeof(IMLENTRY));
6922 c->id = id;
6924 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6925 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
6926 pnies[*cies] = c;
6927 (*cies)++;
6929 Free(*pies);
6930 *pies = pnies;
6933 himlold = c->himl;
6934 c->himl = himl;
6936 return himlold;
6940 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
6942 int i;
6944 for (i = 0; i < *cies; i++)
6945 Free((*pies)[i]);
6947 Free(*pies);
6949 *cies = 0;
6950 *pies = NULL;
6954 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6956 PIMLENTRY c = NULL;
6958 if (pies != NULL)
6960 int i;
6962 for (i = 0; i < cies; i++)
6964 if (pies[i]->id == id)
6966 c = pies[i];
6967 break;
6972 return c;
6976 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6978 HIMAGELIST himlDef = 0;
6979 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6981 if (pie)
6982 himlDef = pie->himl;
6984 return himlDef;
6988 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6990 if (infoPtr->bUnicode)
6991 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6992 else
6994 CHAR Buffer[256];
6995 NMTOOLBARA nmtba;
6996 BOOL bRet = FALSE;
6998 nmtba.iItem = nmtb->iItem;
6999 nmtba.pszText = Buffer;
7000 nmtba.cchText = 256;
7001 ZeroMemory(nmtba.pszText, nmtba.cchText);
7003 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7005 int ccht = strlen(nmtba.pszText);
7006 if (ccht)
7007 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
7008 nmtb->pszText, nmtb->cchText);
7010 nmtb->tbButton = nmtba.tbButton;
7011 bRet = TRUE;
7014 return bRet;
7019 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
7021 NMTOOLBARW nmtb;
7023 /* MSDN states that iItem is the index of the button, rather than the
7024 * command ID as used by every other NMTOOLBAR notification */
7025 nmtb.iItem = iItem;
7026 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7028 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);