d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / comctl32 / toolbar.c
bloba1b7966c9809f3d12820f3864d12278100a4c5aa
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 INT nWidth; /* width of the toolbar */
123 RECT client_rect;
124 RECT rcBound; /* bounding rectangle */
125 INT nButtonHeight;
126 INT nButtonWidth;
127 INT nBitmapHeight;
128 INT nBitmapWidth;
129 INT nIndent;
130 INT nRows; /* number of button rows */
131 INT nMaxTextRows; /* maximum number of text rows */
132 INT cxMin; /* minimum button width */
133 INT cxMax; /* maximum button width */
134 INT nNumButtons; /* number of buttons */
135 INT nNumBitmaps; /* number of bitmaps */
136 INT nNumStrings; /* number of strings */
137 INT nNumBitmapInfos;
138 INT nButtonDown; /* toolbar button being pressed or -1 if none */
139 INT nButtonDrag; /* toolbar button being dragged or -1 if none */
140 INT nOldHit;
141 INT nHotItem; /* index of the "hot" item */
142 SIZE szPadding; /* padding values around button */
143 INT iTopMargin; /* the top margin */
144 INT iListGap; /* default gap between text and image for toolbar with list style */
145 HFONT hDefaultFont;
146 HFONT hFont; /* text font */
147 HIMAGELIST himlInt; /* image list created internally */
148 PIMLENTRY *himlDef; /* default image list array */
149 INT cimlDef; /* default image list array count */
150 PIMLENTRY *himlHot; /* hot image list array */
151 INT cimlHot; /* hot image list array count */
152 PIMLENTRY *himlDis; /* disabled image list array */
153 INT cimlDis; /* disabled image list array count */
154 HWND hwndToolTip; /* handle to tool tip control */
155 HWND hwndNotify; /* handle to the window that gets notifications */
156 HWND hwndSelf; /* my own handle */
157 BOOL bAnchor; /* anchor highlight enabled */
158 BOOL bDoRedraw; /* Redraw status */
159 BOOL bDragOutSent; /* has TBN_DRAGOUT notification been sent for this drag? */
160 BOOL bUnicode; /* Notifications are ASCII (FALSE) or Unicode (TRUE)? */
161 BOOL bCaptured; /* mouse captured? */
162 DWORD dwStyle; /* regular toolbar style */
163 DWORD dwExStyle; /* extended toolbar style */
164 DWORD dwDTFlags; /* DrawText flags */
166 COLORREF clrInsertMark; /* insert mark color */
167 COLORREF clrBtnHighlight; /* color for Flat Separator */
168 COLORREF clrBtnShadow; /* color for Flag Separator */
169 INT iVersion;
170 LPWSTR pszTooltipText; /* temporary store for a string > 80 characters
171 * for TTN_GETDISPINFOW notification */
172 TBINSERTMARK tbim; /* info on insertion mark */
173 TBUTTON_INFO *buttons; /* pointer to button array */
174 LPWSTR *strings; /* pointer to string array */
175 TBITMAP_INFO *bitmaps;
176 } TOOLBAR_INFO, *PTOOLBAR_INFO;
179 /* used by customization dialog */
180 typedef struct
182 PTOOLBAR_INFO tbInfo;
183 HWND tbHwnd;
184 } CUSTDLG_INFO, *PCUSTDLG_INFO;
186 typedef struct
188 TBBUTTON btn;
189 BOOL bVirtual;
190 BOOL bRemovable;
191 WCHAR text[64];
192 } CUSTOMBUTTON, *PCUSTOMBUTTON;
194 typedef enum
196 IMAGE_LIST_DEFAULT,
197 IMAGE_LIST_HOT,
198 IMAGE_LIST_DISABLED
199 } IMAGE_LIST_TYPE;
201 #define SEPARATOR_WIDTH 8
202 #define TOP_BORDER 2
203 #define BOTTOM_BORDER 2
204 #define DDARROW_WIDTH 11
205 #define ARROW_HEIGHT 3
206 #define INSERTMARK_WIDTH 2
208 #define DEFPAD_CX 7
209 #define DEFPAD_CY 6
210 #define DEFLISTGAP 4
212 /* vertical padding used in list mode when image is present */
213 #define LISTPAD_CY 9
215 /* how wide to treat the bitmap if it isn't present */
216 #define NONLIST_NOTEXT_OFFSET 2
218 #define TOOLBAR_NOWHERE (-1)
220 /* Used to find undocumented extended styles */
221 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
222 TBSTYLE_EX_VERTICAL | \
223 TBSTYLE_EX_MIXEDBUTTONS | \
224 TBSTYLE_EX_DOUBLEBUFFER | \
225 TBSTYLE_EX_HIDECLIPPEDBUTTONS)
227 /* all of the CCS_ styles */
228 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
229 CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
231 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
232 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
233 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
234 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
235 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
237 static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };
239 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
240 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
241 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
242 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
243 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
244 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
245 static LRESULT TOOLBAR_LButtonDown(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
246 static void TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr);
247 static LRESULT TOOLBAR_AutoSize(TOOLBAR_INFO *infoPtr);
248 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
249 static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
250 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
253 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
255 return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
258 static inline BOOL TOOLBAR_HasDropDownArrows(DWORD exStyle)
260 return (exStyle & TBSTYLE_EX_DRAWDDARROWS) != 0;
263 static LPWSTR
264 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
266 LPWSTR lpText = NULL;
268 /* NOTE: iString == -1 is undocumented */
269 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
270 lpText = (LPWSTR)btnPtr->iString;
271 else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
272 lpText = infoPtr->strings[btnPtr->iString];
274 return lpText;
277 static void
278 TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
280 TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx (%s)\n",
281 tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
282 (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
285 static void
286 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
288 if (TRACE_ON(toolbar)){
289 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
290 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
291 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
292 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
293 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
294 btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
295 wine_dbgstr_rect(&bP->rect));
300 static void
301 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
303 if (TRACE_ON(toolbar)) {
304 INT i;
306 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
307 iP->hwndSelf, line,
308 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
309 iP->nNumStrings, iP->dwStyle);
310 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
311 iP->hwndSelf, line,
312 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
313 (iP->bDoRedraw) ? "TRUE" : "FALSE");
314 for(i=0; i<iP->nNumButtons; i++) {
315 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
320 static inline BOOL
321 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
323 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
326 /***********************************************************************
327 * TOOLBAR_CheckStyle
329 * This function validates that the styles set are implemented and
330 * issues FIXMEs warning of possible problems. In a perfect world this
331 * function should be null.
333 static void
334 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
336 if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
337 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
341 static INT
342 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
344 if(!IsWindow(infoPtr->hwndSelf))
345 return 0; /* we have just been destroyed */
347 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
348 nmhdr->hwndFrom = infoPtr->hwndSelf;
349 nmhdr->code = code;
351 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
352 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
354 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
357 /***********************************************************************
358 * TOOLBAR_GetBitmapIndex
360 * This function returns the bitmap index associated with a button.
361 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
362 * is issued to retrieve the index.
364 static INT
365 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
367 INT ret = btnPtr->iBitmap;
369 if (ret == I_IMAGECALLBACK)
371 /* issue TBN_GETDISPINFO */
372 NMTBDISPINFOW nmgd;
374 memset(&nmgd, 0, sizeof(nmgd));
375 nmgd.idCommand = btnPtr->idCommand;
376 nmgd.lParam = btnPtr->dwData;
377 nmgd.dwMask = TBNF_IMAGE;
378 nmgd.iImage = -1;
379 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
380 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
381 if (nmgd.dwMask & TBNF_DI_SETITEM)
382 btnPtr->iBitmap = nmgd.iImage;
383 ret = nmgd.iImage;
384 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
385 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
388 if (ret != I_IMAGENONE)
389 ret = GETIBITMAP(infoPtr, ret);
391 return ret;
395 static BOOL
396 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
398 HIMAGELIST himl;
399 INT id = GETHIMLID(infoPtr, index);
400 INT iBitmap = GETIBITMAP(infoPtr, index);
402 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
403 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
404 (index == I_IMAGECALLBACK))
405 return TRUE;
406 else
407 return FALSE;
411 static inline BOOL
412 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
414 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
415 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
419 /***********************************************************************
420 * TOOLBAR_GetImageListForDrawing
422 * This function validates the bitmap index (including I_IMAGECALLBACK
423 * functionality) and returns the corresponding image list.
425 static HIMAGELIST
426 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
427 IMAGE_LIST_TYPE imagelist, INT * index)
429 HIMAGELIST himl;
431 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
432 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
433 WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
434 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
435 return NULL;
438 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
439 if ((*index == I_IMAGECALLBACK) ||
440 (*index == I_IMAGENONE)) return NULL;
441 ERR("TBN_GETDISPINFO returned invalid index %d\n",
442 *index);
443 return NULL;
446 switch(imagelist)
448 case IMAGE_LIST_DEFAULT:
449 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
450 break;
451 case IMAGE_LIST_HOT:
452 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
453 break;
454 case IMAGE_LIST_DISABLED:
455 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
456 break;
457 default:
458 himl = NULL;
459 FIXME("Shouldn't reach here\n");
462 if (!himl)
463 TRACE("no image list\n");
465 return himl;
469 static void
470 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
472 RECT myrect;
473 COLORREF oldcolor, newcolor;
475 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
476 myrect.right = myrect.left + 1;
477 myrect.top = lpRect->top + 2;
478 myrect.bottom = lpRect->bottom - 2;
480 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
481 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
482 oldcolor = SetBkColor (hdc, newcolor);
483 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
485 myrect.left = myrect.right;
486 myrect.right = myrect.left + 1;
488 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
489 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
490 SetBkColor (hdc, newcolor);
491 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
493 SetBkColor (hdc, oldcolor);
497 /***********************************************************************
498 * TOOLBAR_DrawFlatHorizontalSeparator
500 * This function draws horizontal separator for toolbars having CCS_VERT style.
501 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
502 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
503 * are horizontal as opposed to the vertical separators for not dropdown
504 * type.
506 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
508 static void
509 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
510 const TOOLBAR_INFO *infoPtr)
512 RECT myrect;
513 COLORREF oldcolor, newcolor;
515 myrect.left = lpRect->left;
516 myrect.right = lpRect->right;
517 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
518 myrect.bottom = myrect.top + 1;
520 InflateRect (&myrect, -2, 0);
522 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
524 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
525 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
526 oldcolor = SetBkColor (hdc, newcolor);
527 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
529 myrect.top = myrect.bottom;
530 myrect.bottom = myrect.top + 1;
532 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
533 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
534 SetBkColor (hdc, newcolor);
535 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
537 SetBkColor (hdc, oldcolor);
541 static void
542 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
544 INT x, y;
545 HPEN hPen, hOldPen;
547 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
548 hOldPen = SelectObject ( hdc, hPen );
549 x = left + 2;
550 y = top;
551 MoveToEx (hdc, x, y, NULL);
552 LineTo (hdc, x+5, y++); x++;
553 MoveToEx (hdc, x, y, NULL);
554 LineTo (hdc, x+3, y++); x++;
555 MoveToEx (hdc, x, y, NULL);
556 LineTo (hdc, x+1, y);
557 SelectObject( hdc, hOldPen );
558 DeleteObject( hPen );
562 * Draw the text string for this button.
563 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
564 * is non-zero, so we can simply check himlDef to see if we have
565 * an image list
567 static void
568 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
569 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
571 HDC hdc = tbcd->nmcd.hdc;
572 HFONT hOldFont = 0;
573 COLORREF clrOld = 0;
574 COLORREF clrOldBk = 0;
575 int oldBkMode = 0;
576 UINT state = tbcd->nmcd.uItemState;
578 /* draw text */
579 if (lpText && infoPtr->nMaxTextRows > 0) {
580 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
581 wine_dbgstr_rect(rcText));
583 hOldFont = SelectObject (hdc, infoPtr->hFont);
584 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
585 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
587 else if (state & CDIS_DISABLED) {
588 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
589 OffsetRect (rcText, 1, 1);
590 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
591 SetTextColor (hdc, comctl32_color.clr3dShadow);
592 OffsetRect (rcText, -1, -1);
594 else if (state & CDIS_INDETERMINATE) {
595 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
597 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
598 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
599 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
600 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
602 else {
603 clrOld = SetTextColor (hdc, tbcd->clrText);
606 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
607 SetTextColor (hdc, clrOld);
608 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
610 SetBkColor (hdc, clrOldBk);
611 SetBkMode (hdc, oldBkMode);
613 SelectObject (hdc, hOldFont);
618 static void
619 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
621 HDC hdc = tbcd->nmcd.hdc;
622 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
623 COLORREF clrTextOld;
624 COLORREF clrBkOld;
625 INT cx = lpRect->right - lpRect->left;
626 INT cy = lpRect->bottom - lpRect->top;
627 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
628 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
629 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
630 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
631 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
632 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
633 SetBkColor(hdc, clrBkOld);
634 SetTextColor(hdc, clrTextOld);
635 SelectObject (hdc, hbr);
639 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
641 INT cx, cy;
642 HBITMAP hbmMask, hbmImage;
643 HDC hdcMask, hdcImage;
645 ImageList_GetIconSize(himl, &cx, &cy);
647 /* Create src image */
648 hdcImage = CreateCompatibleDC(hdc);
649 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
650 SelectObject(hdcImage, hbmImage);
651 ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
652 RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
654 /* Create Mask */
655 hdcMask = CreateCompatibleDC(0);
656 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
657 SelectObject(hdcMask, hbmMask);
659 /* Remove the background and all white pixels */
660 ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
661 RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
662 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
663 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
665 /* draw the new mask 'etched' to hdc */
666 SetBkColor(hdc, RGB(255, 255, 255));
667 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
668 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
669 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
670 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
671 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
673 /* Cleanup */
674 DeleteObject(hbmImage);
675 DeleteDC(hdcImage);
676 DeleteObject (hbmMask);
677 DeleteDC(hdcMask);
681 static UINT
682 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
684 UINT retstate = 0;
686 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
687 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
688 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
689 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
690 retstate |= (btnPtr->bHot ) ? CDIS_HOT : 0;
691 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
692 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
693 return retstate;
696 /* draws the image on a toolbar button */
697 static void
698 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
699 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
701 HIMAGELIST himl = NULL;
702 BOOL draw_masked = FALSE;
703 INT index;
704 INT offset = 0;
705 UINT draw_flags = ILD_TRANSPARENT;
707 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
709 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
710 if (!himl)
712 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
713 draw_masked = TRUE;
716 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
717 ((tbcd->nmcd.uItemState & CDIS_HOT)
718 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
720 /* if hot, attempt to draw with hot image list, if fails,
721 use default image list */
722 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
723 if (!himl)
724 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
726 else
727 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
729 if (!himl)
730 return;
732 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
733 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
734 offset = 1;
736 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
737 (tbcd->nmcd.uItemState & CDIS_MARKED))
738 draw_flags |= ILD_BLEND50;
740 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
741 index, himl, left, top, offset);
743 if (draw_masked)
744 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
745 else
746 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
749 /* draws a blank frame for a toolbar button */
750 static void
751 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
753 HDC hdc = tbcd->nmcd.hdc;
754 RECT rc = *rect;
755 /* if the state is disabled or indeterminate then the button
756 * cannot have an interactive look like pressed or hot */
757 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
758 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
759 BOOL pressed_look = !non_interactive_state &&
760 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
761 (tbcd->nmcd.uItemState & CDIS_CHECKED));
763 /* app don't want us to draw any edges */
764 if (dwItemCDFlag & TBCDRF_NOEDGES)
765 return;
767 if (infoPtr->dwStyle & TBSTYLE_FLAT)
769 if (pressed_look)
770 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
771 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
772 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
774 else
776 if (pressed_look)
777 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
778 else
779 DrawEdge (hdc, &rc, EDGE_RAISED,
780 BF_SOFT | BF_RECT | BF_MIDDLE);
784 static void
785 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
787 HDC hdc = tbcd->nmcd.hdc;
788 int offset = 0;
789 BOOL pressed = bDropDownPressed ||
790 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
792 if (infoPtr->dwStyle & TBSTYLE_FLAT)
794 if (pressed)
795 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
796 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
797 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
798 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
799 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
801 else
803 if (pressed)
804 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
805 else
806 DrawEdge (hdc, rcArrow, EDGE_RAISED,
807 BF_SOFT | BF_RECT | BF_MIDDLE);
810 if (pressed)
811 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
813 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
815 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
816 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
818 else
819 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
822 /* draws a complete toolbar button */
823 static void
824 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
826 DWORD dwStyle = infoPtr->dwStyle;
827 BOOL hasDropDownArrow = (TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) &&
828 (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
829 (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
830 BOOL drawSepDropDownArrow = hasDropDownArrow &&
831 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
832 RECT rc, rcArrow, rcBitmap, rcText;
833 LPWSTR lpText = NULL;
834 NMTBCUSTOMDRAW tbcd;
835 DWORD ntfret;
836 INT offset;
837 INT oldBkMode;
838 DWORD dwItemCustDraw;
839 DWORD dwItemCDFlag;
840 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
842 rc = btnPtr->rect;
843 CopyRect (&rcArrow, &rc);
845 /* separator - doesn't send NM_CUSTOMDRAW */
846 if (btnPtr->fsStyle & BTNS_SEP) {
847 if (theme)
849 DrawThemeBackground (theme, hdc,
850 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
851 &rc, NULL);
853 else
854 /* with the FLAT style, iBitmap is the width and has already */
855 /* been taken into consideration in calculating the width */
856 /* so now we need to draw the vertical separator */
857 /* empirical tests show that iBitmap can/will be non-zero */
858 /* when drawing the vertical bar... */
859 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
860 if (dwStyle & CCS_VERT) {
861 RECT rcsep = rc;
862 InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
863 TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
865 else
866 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
868 else if (btnPtr->fsStyle != BTNS_SEP) {
869 FIXME("Draw some kind of separator: fsStyle=%x\n",
870 btnPtr->fsStyle);
872 return;
875 /* get a pointer to the text */
876 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
878 if (hasDropDownArrow)
880 int right;
882 if (dwStyle & TBSTYLE_FLAT)
883 right = max(rc.left, rc.right - DDARROW_WIDTH);
884 else
885 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
887 if (drawSepDropDownArrow)
888 rc.right = right;
890 rcArrow.left = right;
893 /* copy text & bitmap rects after adjusting for drop-down arrow
894 * so that text & bitmap is centered in the rectangle not containing
895 * the arrow */
896 CopyRect(&rcText, &rc);
897 CopyRect(&rcBitmap, &rc);
899 /* Center the bitmap horizontally and vertically */
900 if (dwStyle & TBSTYLE_LIST)
902 if (lpText &&
903 infoPtr->nMaxTextRows > 0 &&
904 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
905 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
906 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
907 else
908 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
910 else
911 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
913 rcBitmap.top += infoPtr->szPadding.cy / 2;
915 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
916 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
917 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
918 TRACE("Text=%s\n", debugstr_w(lpText));
919 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
921 /* calculate text position */
922 if (lpText)
924 rcText.left += GetSystemMetrics(SM_CXEDGE);
925 rcText.right -= GetSystemMetrics(SM_CXEDGE);
926 if (dwStyle & TBSTYLE_LIST)
928 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
930 else
932 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
933 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
934 else
935 rcText.top += infoPtr->szPadding.cy/2 + 2;
939 /* Initialize fields in all cases, because we use these later
940 * NOTE: applications can and do alter these to customize their
941 * toolbars */
942 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
943 tbcd.clrText = comctl32_color.clrBtnText;
944 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
945 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
946 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
947 tbcd.clrMark = comctl32_color.clrHighlight;
948 tbcd.clrHighlightHotTrack = 0;
949 tbcd.nStringBkMode = TRANSPARENT;
950 tbcd.nHLStringBkMode = OPAQUE;
951 tbcd.rcText.left = 0;
952 tbcd.rcText.top = 0;
953 tbcd.rcText.right = rcText.right - rc.left;
954 tbcd.rcText.bottom = rcText.bottom - rc.top;
955 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
956 tbcd.nmcd.hdc = hdc;
957 tbcd.nmcd.rc = btnPtr->rect;
958 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
960 /* FIXME: what are these used for? */
961 tbcd.hbrLines = 0;
962 tbcd.hpenLines = 0;
964 /* Issue Item Prepaint notify */
965 dwItemCustDraw = 0;
966 dwItemCDFlag = 0;
967 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
969 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
970 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
971 tbcd.nmcd.lItemlParam = btnPtr->dwData;
972 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
973 /* reset these fields so the user can't alter the behaviour like native */
974 tbcd.nmcd.hdc = hdc;
975 tbcd.nmcd.rc = btnPtr->rect;
977 dwItemCustDraw = ntfret & 0xffff;
978 dwItemCDFlag = ntfret & 0xffff0000;
979 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
980 return;
981 /* save the only part of the rect that the user can change */
982 rcText.right = tbcd.rcText.right + rc.left;
983 rcText.bottom = tbcd.rcText.bottom + rc.top;
986 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
987 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
988 OffsetRect(&rcText, 1, 1);
990 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
991 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
992 TOOLBAR_DrawPattern (&rc, &tbcd);
994 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
995 && (tbcd.nmcd.uItemState & CDIS_HOT))
997 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
999 COLORREF oldclr;
1001 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1002 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1003 if (hasDropDownArrow)
1004 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1005 SetBkColor(hdc, oldclr);
1009 if (theme)
1011 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1012 int stateId = TS_NORMAL;
1014 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1015 stateId = TS_DISABLED;
1016 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1017 stateId = TS_PRESSED;
1018 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1019 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1020 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1021 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1022 stateId = TS_HOT;
1024 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1026 else
1027 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1029 if (drawSepDropDownArrow)
1031 if (theme)
1033 int stateId = TS_NORMAL;
1035 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1036 stateId = TS_DISABLED;
1037 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1038 stateId = TS_PRESSED;
1039 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1040 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1041 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1042 stateId = TS_HOT;
1044 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1045 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1047 else
1048 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1051 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1052 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1053 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1054 SetBkMode (hdc, oldBkMode);
1056 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1058 if (hasDropDownArrow && !drawSepDropDownArrow)
1060 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1062 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1063 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1065 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1067 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1068 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1070 else
1071 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1074 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1076 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1077 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1083 static void
1084 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1086 TBUTTON_INFO *btnPtr;
1087 INT i;
1088 RECT rcTemp, rcClient;
1089 NMTBCUSTOMDRAW tbcd;
1090 DWORD ntfret;
1091 DWORD dwBaseCustDraw;
1093 /* the app has told us not to redraw the toolbar */
1094 if (!infoPtr->bDoRedraw)
1095 return;
1097 /* if imagelist belongs to the app, it can be changed
1098 by the app after setting it */
1099 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1101 infoPtr->nNumBitmaps = 0;
1102 for (i = 0; i < infoPtr->cimlDef; i++)
1103 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1106 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1108 /* change the imagelist icon size if we manage the list and it is necessary */
1109 TOOLBAR_CheckImageListIconSize(infoPtr);
1111 /* Send initial notify */
1112 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1113 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1114 tbcd.nmcd.hdc = hdc;
1115 tbcd.nmcd.rc = ps->rcPaint;
1116 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1117 dwBaseCustDraw = ntfret & 0xffff;
1119 GetClientRect(infoPtr->hwndSelf, &rcClient);
1121 /* redraw necessary buttons */
1122 btnPtr = infoPtr->buttons;
1123 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1125 BOOL bDraw;
1126 if (!RectVisible(hdc, &btnPtr->rect))
1127 continue;
1128 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1130 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1131 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1133 else
1134 bDraw = TRUE;
1135 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1136 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1137 if (bDraw)
1138 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1141 /* draw insert mark if required */
1142 if (infoPtr->tbim.iButton != -1)
1144 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1145 RECT rcInsertMark;
1146 rcInsertMark.top = rcButton.top;
1147 rcInsertMark.bottom = rcButton.bottom;
1148 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1149 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1150 else
1151 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1152 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1155 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1157 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1158 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1159 tbcd.nmcd.hdc = hdc;
1160 tbcd.nmcd.rc = ps->rcPaint;
1161 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1165 /***********************************************************************
1166 * TOOLBAR_MeasureString
1168 * This function gets the width and height of a string in pixels. This
1169 * is done first by using GetTextExtentPoint to get the basic width
1170 * and height. The DrawText is called with DT_CALCRECT to get the exact
1171 * width. The reason is because the text may have more than one "&" (or
1172 * prefix characters as M$ likes to call them). The prefix character
1173 * indicates where the underline goes, except for the string "&&" which
1174 * is reduced to a single "&". GetTextExtentPoint does not process these
1175 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1177 static void
1178 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1179 HDC hdc, LPSIZE lpSize)
1181 RECT myrect;
1183 lpSize->cx = 0;
1184 lpSize->cy = 0;
1186 if (infoPtr->nMaxTextRows > 0 &&
1187 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1188 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1189 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1191 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1193 if(lpText != NULL) {
1194 /* first get size of all the text */
1195 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1197 /* feed above size into the rectangle for DrawText */
1198 myrect.left = myrect.top = 0;
1199 myrect.right = lpSize->cx;
1200 myrect.bottom = lpSize->cy;
1202 /* Use DrawText to get true size as drawn (less pesky "&") */
1203 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1204 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1205 DT_NOPREFIX : 0));
1207 /* feed back to caller */
1208 lpSize->cx = myrect.right;
1209 lpSize->cy = myrect.bottom;
1213 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1216 /***********************************************************************
1217 * TOOLBAR_CalcStrings
1219 * This function walks through each string and measures it and returns
1220 * the largest height and width to caller.
1222 static void
1223 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1225 TBUTTON_INFO *btnPtr;
1226 INT i;
1227 SIZE sz;
1228 HDC hdc;
1229 HFONT hOldFont;
1231 lpSize->cx = 0;
1232 lpSize->cy = 0;
1234 if (infoPtr->nMaxTextRows == 0)
1235 return;
1237 hdc = GetDC (infoPtr->hwndSelf);
1238 hOldFont = SelectObject (hdc, infoPtr->hFont);
1240 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1242 TEXTMETRICW tm;
1244 GetTextMetricsW(hdc, &tm);
1245 lpSize->cy = tm.tmHeight;
1248 btnPtr = infoPtr->buttons;
1249 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1250 if(TOOLBAR_GetText(infoPtr, btnPtr))
1252 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1253 if (sz.cx > lpSize->cx)
1254 lpSize->cx = sz.cx;
1255 if (sz.cy > lpSize->cy)
1256 lpSize->cy = sz.cy;
1260 SelectObject (hdc, hOldFont);
1261 ReleaseDC (infoPtr->hwndSelf, hdc);
1263 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1266 /***********************************************************************
1267 * TOOLBAR_WrapToolbar
1269 * This function walks through the buttons and separators in the
1270 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1271 * wrapping should occur based on the width of the toolbar window.
1272 * It does *not* calculate button placement itself. That task
1273 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1274 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1275 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1277 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1278 * vertical toolbar lists.
1281 static void
1282 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1284 TBUTTON_INFO *btnPtr;
1285 INT x, cx, i, j;
1286 RECT rc;
1287 BOOL bButtonWrap;
1289 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1290 /* no layout is necessary. Applications may use this style */
1291 /* to perform their own layout on the toolbar. */
1292 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1293 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1295 btnPtr = infoPtr->buttons;
1296 x = infoPtr->nIndent;
1298 if (GetParent(infoPtr->hwndSelf))
1300 /* this can get the parents width, to know how far we can extend
1301 * this toolbar. We cannot use its height, as there may be multiple
1302 * toolbars in a rebar control
1304 GetClientRect( GetParent(infoPtr->hwndSelf), &rc );
1305 infoPtr->nWidth = rc.right - rc.left;
1307 else
1309 GetWindowRect( infoPtr->hwndSelf, &rc );
1310 infoPtr->nWidth = rc.right - rc.left;
1313 bButtonWrap = FALSE;
1315 TRACE("start ButtonWidth=%d, BitmapWidth=%d, nWidth=%d, nIndent=%d\n",
1316 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, infoPtr->nWidth,
1317 infoPtr->nIndent);
1319 for (i = 0; i < infoPtr->nNumButtons; i++ )
1321 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1323 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1324 continue;
1326 if (btnPtr[i].cx > 0)
1327 cx = btnPtr[i].cx;
1328 /* horizontal separators are treated as buttons for width */
1329 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1330 !(infoPtr->dwStyle & CCS_VERT))
1331 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1332 else
1333 cx = infoPtr->nButtonWidth;
1335 /* Two or more adjacent separators form a separator group. */
1336 /* The first separator in a group should be wrapped to the */
1337 /* next row if the previous wrapping is on a button. */
1338 if( bButtonWrap &&
1339 (btnPtr[i].fsStyle & BTNS_SEP) &&
1340 (i + 1 < infoPtr->nNumButtons ) &&
1341 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1343 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1344 btnPtr[i].fsState |= TBSTATE_WRAP;
1345 x = infoPtr->nIndent;
1346 i++;
1347 bButtonWrap = FALSE;
1348 continue;
1351 /* The layout makes sure the bitmap is visible, but not the button. */
1352 /* Test added to also wrap after a button that starts a row but */
1353 /* is bigger than the area. - GA 8/01 */
1354 if (( x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2
1355 > infoPtr->nWidth ) ||
1356 ((x == infoPtr->nIndent) && (cx > infoPtr->nWidth)))
1358 BOOL bFound = FALSE;
1360 /* If the current button is a separator and not hidden, */
1361 /* go to the next until it reaches a non separator. */
1362 /* Wrap the last separator if it is before a button. */
1363 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1364 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1365 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1366 i < infoPtr->nNumButtons )
1368 i++;
1369 bFound = TRUE;
1372 if( bFound && i < infoPtr->nNumButtons )
1374 i--;
1375 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1376 i, btnPtr[i].fsStyle, x, cx);
1377 btnPtr[i].fsState |= TBSTATE_WRAP;
1378 x = infoPtr->nIndent;
1379 bButtonWrap = FALSE;
1380 continue;
1382 else if ( i >= infoPtr->nNumButtons)
1383 break;
1385 /* If the current button is not a separator, find the last */
1386 /* separator and wrap it. */
1387 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1389 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1390 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1392 bFound = TRUE;
1393 i = j;
1394 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1395 i, btnPtr[i].fsStyle, x, cx);
1396 x = infoPtr->nIndent;
1397 btnPtr[j].fsState |= TBSTATE_WRAP;
1398 bButtonWrap = FALSE;
1399 break;
1403 /* If no separator available for wrapping, wrap one of */
1404 /* non-hidden previous button. */
1405 if (!bFound)
1407 for ( j = i - 1;
1408 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1410 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1411 continue;
1413 bFound = TRUE;
1414 i = j;
1415 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1416 i, btnPtr[i].fsStyle, x, cx);
1417 x = infoPtr->nIndent;
1418 btnPtr[j].fsState |= TBSTATE_WRAP;
1419 bButtonWrap = TRUE;
1420 break;
1424 /* If all above failed, wrap the current button. */
1425 if (!bFound)
1427 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1428 i, btnPtr[i].fsStyle, x, cx);
1429 btnPtr[i].fsState |= TBSTATE_WRAP;
1430 x = infoPtr->nIndent;
1431 if (btnPtr[i].fsStyle & BTNS_SEP )
1432 bButtonWrap = FALSE;
1433 else
1434 bButtonWrap = TRUE;
1437 else {
1438 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1439 i, btnPtr[i].fsStyle, x, cx);
1440 x += cx;
1446 /***********************************************************************
1447 * TOOLBAR_MeasureButton
1449 * Calculates the width and height required for a button. Used in
1450 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1451 * the width of buttons that are autosized.
1453 * Note that it would have been rather elegant to use one piece of code for
1454 * both the laying out of the toolbar and for controlling where button parts
1455 * are drawn, but the native control has inconsistencies between the two that
1456 * prevent this from being effectively. These inconsistencies can be seen as
1457 * artefacts where parts of the button appear outside of the bounding button
1458 * rectangle.
1460 * There are several cases for the calculation of the button dimensions and
1461 * button part positioning:
1463 * List
1464 * ====
1466 * With Bitmap:
1468 * +--------------------------------------------------------+ ^
1469 * | ^ ^ | |
1470 * | | pad.cy / 2 | centered | |
1471 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1472 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1473 * | |<------------>| | | | |
1474 * | +--------------+ +------------+ | |
1475 * |<-------------------------------------->| | |
1476 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1477 * | szText.cx | |
1478 * +--------------------------------------------------------+ -
1479 * <-------------------------------------------------------->
1480 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1482 * Without Bitmap (I_IMAGENONE):
1484 * +-----------------------------------+ ^
1485 * | ^ | |
1486 * | | centered | | LISTPAD_CY +
1487 * | +------------+ | | szText.cy
1488 * | | Text | | |
1489 * | | | | |
1490 * | +------------+ | |
1491 * |<----------------->| | |
1492 * | cxedge |<-----------> | |
1493 * | szText.cx | |
1494 * +-----------------------------------+ -
1495 * <----------------------------------->
1496 * szText.cx + pad.cx
1498 * Without text:
1500 * +--------------------------------------+ ^
1501 * | ^ | |
1502 * | | padding.cy/2 | | DEFPAD_CY +
1503 * | +------------+ | | nBitmapHeight
1504 * | | Bitmap | | |
1505 * | | | | |
1506 * | +------------+ | |
1507 * |<------------------->| | |
1508 * | cxedge + iListGap/2 |<-----------> | |
1509 * | nBitmapWidth | |
1510 * +--------------------------------------+ -
1511 * <-------------------------------------->
1512 * 2*cxedge + nBitmapWidth + iListGap
1514 * Non-List
1515 * ========
1517 * With bitmap:
1519 * +-----------------------------------+ ^
1520 * | ^ | |
1521 * | | pad.cy / 2 | | nBitmapHeight +
1522 * | - | | szText.cy +
1523 * | +------------+ | | DEFPAD_CY + 1
1524 * | centered | Bitmap | | |
1525 * |<----------------->| | | |
1526 * | +------------+ | |
1527 * | ^ | |
1528 * | 1 | | |
1529 * | - | |
1530 * | centered +---------------+ | |
1531 * |<--------------->| Text | | |
1532 * | +---------------+ | |
1533 * +-----------------------------------+ -
1534 * <----------------------------------->
1535 * pad.cx + max(nBitmapWidth, szText.cx)
1537 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1539 * +---------------------------------------+ ^
1540 * | ^ | |
1541 * | | 2 + pad.cy / 2 | |
1542 * | - | | szText.cy +
1543 * | centered +-----------------+ | | pad.cy + 2
1544 * |<--------------->| Text | | |
1545 * | +-----------------+ | |
1546 * | | |
1547 * +---------------------------------------+ -
1548 * <--------------------------------------->
1549 * 2*cxedge + pad.cx + szText.cx
1551 * Without text:
1552 * As for with bitmaps, but with szText.cx zero.
1554 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1555 BOOL bHasBitmap, BOOL bValidImageList)
1557 SIZE sizeButton;
1558 if (infoPtr->dwStyle & TBSTYLE_LIST)
1560 /* set button height from bitmap / text height... */
1561 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1562 sizeString.cy);
1564 /* ... add on the necessary padding */
1565 if (bValidImageList)
1567 if (bHasBitmap)
1568 sizeButton.cy += DEFPAD_CY;
1569 else
1570 sizeButton.cy += LISTPAD_CY;
1572 else
1573 sizeButton.cy += infoPtr->szPadding.cy;
1575 /* calculate button width */
1576 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1577 infoPtr->nBitmapWidth + infoPtr->iListGap;
1578 if (sizeString.cx > 0)
1579 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1582 else
1584 if (bHasBitmap)
1586 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1587 if (sizeString.cy > 0)
1588 sizeButton.cy += 1 + sizeString.cy;
1589 sizeButton.cx = infoPtr->szPadding.cx +
1590 max(sizeString.cx, infoPtr->nBitmapWidth);
1592 else
1594 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1595 NONLIST_NOTEXT_OFFSET;
1596 sizeButton.cx = infoPtr->szPadding.cx +
1597 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1600 return sizeButton;
1604 /***********************************************************************
1605 * TOOLBAR_CalcToolbar
1607 * This function calculates button and separator placement. It first
1608 * calculates the button sizes, gets the toolbar window width and then
1609 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1610 * on. It assigns a new location to each item and sends this location to
1611 * the tooltip window if appropriate. Finally, it updates the rcBound
1612 * rect and calculates the new required toolbar window height.
1614 static void
1615 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1617 SIZE sizeString, sizeButton;
1618 BOOL validImageList = FALSE;
1620 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1622 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1624 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1625 validImageList = TRUE;
1626 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1627 infoPtr->nButtonWidth = sizeButton.cx;
1628 infoPtr->nButtonHeight = sizeButton.cy;
1629 infoPtr->iTopMargin = default_top_margin(infoPtr);
1631 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1632 infoPtr->nButtonWidth = infoPtr->cxMin;
1633 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1634 infoPtr->nButtonWidth = infoPtr->cxMax;
1636 TOOLBAR_LayoutToolbar(infoPtr);
1639 static void
1640 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1642 TBUTTON_INFO *btnPtr;
1643 SIZE sizeButton;
1644 INT i, nRows, nSepRows;
1645 INT x, y, cx, cy;
1646 BOOL bWrap;
1647 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1648 BOOL hasDropDownArrows = TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle);
1650 TOOLBAR_WrapToolbar(infoPtr);
1652 x = infoPtr->nIndent;
1653 y = infoPtr->iTopMargin;
1654 cx = infoPtr->nButtonWidth;
1655 cy = infoPtr->nButtonHeight;
1657 nRows = nSepRows = 0;
1659 infoPtr->rcBound.top = y;
1660 infoPtr->rcBound.left = x;
1661 infoPtr->rcBound.bottom = y + cy;
1662 infoPtr->rcBound.right = x;
1664 btnPtr = infoPtr->buttons;
1666 TRACE("cy=%d\n", cy);
1668 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1670 bWrap = FALSE;
1671 if (btnPtr->fsState & TBSTATE_HIDDEN)
1673 SetRectEmpty (&btnPtr->rect);
1674 continue;
1677 cy = infoPtr->nButtonHeight;
1679 if (btnPtr->fsStyle & BTNS_SEP) {
1680 if (infoPtr->dwStyle & CCS_VERT) {
1681 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1682 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1684 else
1685 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1686 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1688 else
1690 if (btnPtr->cx)
1691 cx = btnPtr->cx;
1692 else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1693 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1695 SIZE sz;
1696 HDC hdc;
1697 HFONT hOldFont;
1699 hdc = GetDC (infoPtr->hwndSelf);
1700 hOldFont = SelectObject (hdc, infoPtr->hFont);
1702 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1704 SelectObject (hdc, hOldFont);
1705 ReleaseDC (infoPtr->hwndSelf, hdc);
1707 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1708 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1709 validImageList);
1710 cx = sizeButton.cx;
1712 else
1713 cx = infoPtr->nButtonWidth;
1715 /* if size has been set manually then don't add on extra space
1716 * for the drop down arrow */
1717 if (!btnPtr->cx && hasDropDownArrows &&
1718 ((btnPtr->fsStyle & BTNS_DROPDOWN) || (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)))
1719 cx += DDARROW_WIDTH;
1721 if (btnPtr->fsState & TBSTATE_WRAP )
1722 bWrap = TRUE;
1724 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1726 if (infoPtr->rcBound.left > x)
1727 infoPtr->rcBound.left = x;
1728 if (infoPtr->rcBound.right < x + cx)
1729 infoPtr->rcBound.right = x + cx;
1730 if (infoPtr->rcBound.bottom < y + cy)
1731 infoPtr->rcBound.bottom = y + cy;
1733 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1735 /* btnPtr->nRow is zero based. The space between the rows is */
1736 /* also considered as a row. */
1737 btnPtr->nRow = nRows + nSepRows;
1739 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1740 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1741 x, y, x+cx, y+cy);
1743 if( bWrap )
1745 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1746 y += cy;
1747 else
1749 if ( !(infoPtr->dwStyle & CCS_VERT))
1750 y += cy + ( (btnPtr->cx > 0 ) ?
1751 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1752 else
1753 y += cy;
1755 /* nSepRows is used to calculate the extra height following */
1756 /* the last row. */
1757 nSepRows++;
1759 x = infoPtr->nIndent;
1761 /* Increment row number unless this is the last button */
1762 /* and it has Wrap set. */
1763 if (i != infoPtr->nNumButtons-1)
1764 nRows++;
1766 else
1767 x += cx;
1770 /* infoPtr->nRows is the number of rows on the toolbar */
1771 infoPtr->nRows = nRows + nSepRows + 1;
1773 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1777 static INT
1778 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1780 TBUTTON_INFO *btnPtr;
1781 INT i;
1783 if (button)
1784 *button = FALSE;
1786 btnPtr = infoPtr->buttons;
1787 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1788 if (btnPtr->fsState & TBSTATE_HIDDEN)
1789 continue;
1791 if (btnPtr->fsStyle & BTNS_SEP) {
1792 if (PtInRect (&btnPtr->rect, *lpPt)) {
1793 TRACE(" ON SEPARATOR %d!\n", i);
1794 return -i;
1797 else {
1798 if (PtInRect (&btnPtr->rect, *lpPt)) {
1799 TRACE(" ON BUTTON %d!\n", i);
1800 if (button)
1801 *button = TRUE;
1802 return i;
1807 TRACE(" NOWHERE!\n");
1808 return TOOLBAR_NOWHERE;
1812 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1813 static BOOL
1814 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1816 INT nOldButtons, nNewButtons, iButton;
1817 BOOL fHasString = FALSE;
1819 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1820 iIndex = infoPtr->nNumButtons;
1822 nOldButtons = infoPtr->nNumButtons;
1823 nNewButtons = nOldButtons + nAddButtons;
1825 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1826 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1827 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1828 infoPtr->nNumButtons += nAddButtons;
1830 /* insert new buttons data */
1831 for (iButton = 0; iButton < nAddButtons; iButton++) {
1832 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1834 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1836 ZeroMemory(btnPtr, sizeof(*btnPtr));
1838 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1839 btnPtr->idCommand = lpTbb[iButton].idCommand;
1840 btnPtr->fsState = lpTbb[iButton].fsState;
1841 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1842 btnPtr->dwData = lpTbb[iButton].dwData;
1843 if (btnPtr->fsStyle & BTNS_SEP)
1844 btnPtr->iString = -1;
1845 else if(!IS_INTRESOURCE(lpTbb[iButton].iString) && lpTbb[iButton].iString != -1)
1847 if (fUnicode)
1848 Str_SetPtrW((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[iButton].iString );
1849 else
1850 Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[iButton].iString);
1851 fHasString = TRUE;
1853 else
1854 btnPtr->iString = lpTbb[iButton].iString;
1856 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1859 if (infoPtr->nNumStrings > 0 || fHasString)
1860 TOOLBAR_CalcToolbar(infoPtr);
1861 else
1862 TOOLBAR_LayoutToolbar(infoPtr);
1863 TOOLBAR_AutoSize(infoPtr);
1865 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1866 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1867 return TRUE;
1871 static INT
1872 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1874 TBUTTON_INFO *btnPtr;
1875 INT i;
1877 if (CommandIsIndex) {
1878 TRACE("command is really index command=%d\n", idCommand);
1879 if (idCommand >= infoPtr->nNumButtons) return -1;
1880 return idCommand;
1882 btnPtr = infoPtr->buttons;
1883 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1884 if (btnPtr->idCommand == idCommand) {
1885 TRACE("command=%d index=%d\n", idCommand, i);
1886 return i;
1889 TRACE("no index found for command=%d\n", idCommand);
1890 return -1;
1894 static INT
1895 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1897 TBUTTON_INFO *btnPtr;
1898 INT nRunIndex;
1900 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1901 return -1;
1903 /* check index button */
1904 btnPtr = &infoPtr->buttons[nIndex];
1905 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1906 if (btnPtr->fsState & TBSTATE_CHECKED)
1907 return nIndex;
1910 /* check previous buttons */
1911 nRunIndex = nIndex - 1;
1912 while (nRunIndex >= 0) {
1913 btnPtr = &infoPtr->buttons[nRunIndex];
1914 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1915 if (btnPtr->fsState & TBSTATE_CHECKED)
1916 return nRunIndex;
1918 else
1919 break;
1920 nRunIndex--;
1923 /* check next buttons */
1924 nRunIndex = nIndex + 1;
1925 while (nRunIndex < infoPtr->nNumButtons) {
1926 btnPtr = &infoPtr->buttons[nRunIndex];
1927 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1928 if (btnPtr->fsState & TBSTATE_CHECKED)
1929 return nRunIndex;
1931 else
1932 break;
1933 nRunIndex++;
1936 return -1;
1940 static VOID
1941 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1942 WPARAM wParam, LPARAM lParam)
1944 MSG msg;
1946 msg.hwnd = hwndMsg;
1947 msg.message = uMsg;
1948 msg.wParam = wParam;
1949 msg.lParam = lParam;
1950 msg.time = GetMessageTime ();
1951 msg.pt.x = (short)LOWORD(GetMessagePos ());
1952 msg.pt.y = (short)HIWORD(GetMessagePos ());
1954 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1957 static void
1958 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1960 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1961 TTTOOLINFOW ti;
1963 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1964 ti.cbSize = sizeof (TTTOOLINFOW);
1965 ti.hwnd = infoPtr->hwndSelf;
1966 ti.uId = button->idCommand;
1967 ti.hinst = 0;
1968 ti.lpszText = LPSTR_TEXTCALLBACKW;
1969 /* ti.lParam = random value from the stack? */
1971 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1972 0, (LPARAM)&ti);
1976 static void
1977 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1979 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1980 TTTOOLINFOW ti;
1982 ZeroMemory(&ti, sizeof(ti));
1983 ti.cbSize = sizeof(ti);
1984 ti.hwnd = infoPtr->hwndSelf;
1985 ti.uId = button->idCommand;
1987 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
1991 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1993 /* Set the toolTip only for non-hidden, non-separator button */
1994 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
1996 TTTOOLINFOW ti;
1998 ZeroMemory(&ti, sizeof(ti));
1999 ti.cbSize = sizeof(ti);
2000 ti.hwnd = infoPtr->hwndSelf;
2001 ti.uId = button->idCommand;
2002 ti.rect = button->rect;
2003 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2007 /* Creates the tooltip control */
2008 static void
2009 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2011 int i;
2012 NMTOOLTIPSCREATED nmttc;
2014 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2015 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2016 infoPtr->hwndSelf, 0, 0, 0);
2018 if (!infoPtr->hwndToolTip)
2019 return;
2021 /* Send NM_TOOLTIPSCREATED notification */
2022 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2023 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2025 for (i = 0; i < infoPtr->nNumButtons; i++)
2027 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2028 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2032 /* keeps available button list box sorted by button id */
2033 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2035 int i;
2036 int count;
2037 PCUSTOMBUTTON btnInfo;
2038 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2040 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2042 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2044 /* position 0 is always separator */
2045 for (i = 1; i < count; i++)
2047 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2048 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2050 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2051 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2052 return;
2055 /* id higher than all others add to end */
2056 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2057 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2060 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2062 NMTOOLBARW nmtb;
2064 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2066 if (nIndexFrom == nIndexTo)
2067 return;
2069 /* MSDN states that iItem is the index of the button, rather than the
2070 * command ID as used by every other NMTOOLBAR notification */
2071 nmtb.iItem = nIndexFrom;
2072 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2074 PCUSTOMBUTTON btnInfo;
2075 NMHDR hdr;
2076 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2077 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2079 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2081 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2082 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2083 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2084 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2086 if (nIndexTo <= 0)
2087 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2088 else
2089 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2091 /* last item is always separator, so -2 instead of -1 */
2092 if (nIndexTo >= (count - 2))
2093 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2094 else
2095 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2097 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2098 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2100 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2104 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2106 NMTOOLBARW nmtb;
2108 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2110 /* MSDN states that iItem is the index of the button, rather than the
2111 * command ID as used by every other NMTOOLBAR notification */
2112 nmtb.iItem = nIndexAvail;
2113 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2115 PCUSTOMBUTTON btnInfo;
2116 NMHDR hdr;
2117 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2118 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2119 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2121 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2123 if (nIndexAvail != 0) /* index == 0 indicates separator */
2125 /* remove from 'available buttons' list */
2126 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2127 if (nIndexAvail == count-1)
2128 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2129 else
2130 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2132 else
2134 PCUSTOMBUTTON btnNew;
2136 /* duplicate 'separator' button */
2137 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2138 *btnNew = *btnInfo;
2139 btnInfo = btnNew;
2142 /* insert into 'toolbar button' list */
2143 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2144 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2146 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2148 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2152 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2154 PCUSTOMBUTTON btnInfo;
2155 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2157 TRACE("Remove: index %d\n", index);
2159 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2161 /* send TBN_QUERYDELETE notification */
2162 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2164 NMHDR hdr;
2166 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2167 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2169 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2171 /* insert into 'available button' list */
2172 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2173 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2174 else
2175 Free(btnInfo);
2177 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2181 /* drag list notification function for toolbar buttons list box */
2182 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2183 const DRAGLISTINFO *pDLI)
2185 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2186 switch (pDLI->uNotification)
2188 case DL_BEGINDRAG:
2190 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2191 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2192 /* no dragging for last item (separator) */
2193 if (nCurrentItem >= (nCount - 1)) return FALSE;
2194 return TRUE;
2196 case DL_DRAGGING:
2198 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2199 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2200 /* no dragging past last item (separator) */
2201 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2203 DrawInsert(hwnd, hwndList, nCurrentItem);
2204 /* FIXME: native uses "move button" cursor */
2205 return DL_COPYCURSOR;
2208 /* not over toolbar buttons list */
2209 if (nCurrentItem < 0)
2211 POINT ptWindow = pDLI->ptCursor;
2212 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2213 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2214 /* over available buttons list? */
2215 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2216 /* FIXME: native uses "move button" cursor */
2217 return DL_COPYCURSOR;
2219 /* clear drag arrow */
2220 DrawInsert(hwnd, hwndList, -1);
2221 return DL_STOPCURSOR;
2223 case DL_DROPPED:
2225 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2226 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2227 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2228 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2230 /* clear drag arrow */
2231 DrawInsert(hwnd, hwndList, -1);
2232 /* move item */
2233 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2235 /* not over toolbar buttons list */
2236 if (nIndexTo < 0)
2238 POINT ptWindow = pDLI->ptCursor;
2239 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2240 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2241 /* over available buttons list? */
2242 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2243 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2245 break;
2247 case DL_CANCELDRAG:
2248 /* Clear drag arrow */
2249 DrawInsert(hwnd, hwndList, -1);
2250 break;
2253 return 0;
2256 /* drag list notification function for available buttons list box */
2257 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2258 const DRAGLISTINFO *pDLI)
2260 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2261 switch (pDLI->uNotification)
2263 case DL_BEGINDRAG:
2264 return TRUE;
2265 case DL_DRAGGING:
2267 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2268 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2269 /* no dragging past last item (separator) */
2270 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2272 DrawInsert(hwnd, hwndList, nCurrentItem);
2273 /* FIXME: native uses "move button" cursor */
2274 return DL_COPYCURSOR;
2277 /* not over toolbar buttons list */
2278 if (nCurrentItem < 0)
2280 POINT ptWindow = pDLI->ptCursor;
2281 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2282 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2283 /* over available buttons list? */
2284 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2285 /* FIXME: native uses "move button" cursor */
2286 return DL_COPYCURSOR;
2288 /* clear drag arrow */
2289 DrawInsert(hwnd, hwndList, -1);
2290 return DL_STOPCURSOR;
2292 case DL_DROPPED:
2294 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2295 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2296 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2297 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2299 /* clear drag arrow */
2300 DrawInsert(hwnd, hwndList, -1);
2301 /* add item */
2302 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2305 case DL_CANCELDRAG:
2306 /* Clear drag arrow */
2307 DrawInsert(hwnd, hwndList, -1);
2308 break;
2310 return 0;
2313 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2315 /***********************************************************************
2316 * TOOLBAR_CustomizeDialogProc
2317 * This function implements the toolbar customization dialog.
2319 static INT_PTR CALLBACK
2320 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2322 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2323 PCUSTOMBUTTON btnInfo;
2324 NMTOOLBARA nmtb;
2325 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2327 switch (uMsg)
2329 case WM_INITDIALOG:
2330 custInfo = (PCUSTDLG_INFO)lParam;
2331 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2333 if (custInfo)
2335 WCHAR Buffer[256];
2336 int i = 0;
2337 int index;
2338 NMTBINITCUSTOMIZE nmtbic;
2340 infoPtr = custInfo->tbInfo;
2342 /* send TBN_QUERYINSERT notification */
2343 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2345 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2346 return FALSE;
2348 nmtbic.hwndDialog = hwnd;
2349 /* Send TBN_INITCUSTOMIZE notification */
2350 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2351 TBNRF_HIDEHELP)
2353 TRACE("TBNRF_HIDEHELP requested\n");
2354 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2357 /* add items to 'toolbar buttons' list and check if removable */
2358 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2360 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2361 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2362 btnInfo->btn.fsStyle = BTNS_SEP;
2363 btnInfo->bVirtual = FALSE;
2364 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2366 /* send TBN_QUERYDELETE notification */
2367 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2369 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2370 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2373 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2375 /* insert separator button into 'available buttons' list */
2376 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2377 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2378 btnInfo->btn.fsStyle = BTNS_SEP;
2379 btnInfo->bVirtual = FALSE;
2380 btnInfo->bRemovable = TRUE;
2381 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2382 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2383 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2385 /* insert all buttons into dsa */
2386 for (i = 0;; i++)
2388 /* send TBN_GETBUTTONINFO notification */
2389 NMTOOLBARW nmtb;
2390 nmtb.iItem = i;
2391 nmtb.pszText = Buffer;
2392 nmtb.cchText = 256;
2394 /* Clear previous button's text */
2395 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2397 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2398 break;
2400 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2401 nmtb.tbButton.fsStyle, i,
2402 nmtb.tbButton.idCommand,
2403 nmtb.tbButton.iString,
2404 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2405 : "");
2407 /* insert button into the appropriate list */
2408 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2409 if (index == -1)
2411 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2412 btnInfo->bVirtual = FALSE;
2413 btnInfo->bRemovable = TRUE;
2415 else
2417 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2418 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2421 btnInfo->btn = nmtb.tbButton;
2422 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2424 if (lstrlenW(nmtb.pszText))
2425 lstrcpyW(btnInfo->text, nmtb.pszText);
2426 else if (nmtb.tbButton.iString >= 0 &&
2427 nmtb.tbButton.iString < infoPtr->nNumStrings)
2429 lstrcpyW(btnInfo->text,
2430 infoPtr->strings[nmtb.tbButton.iString]);
2434 if (index == -1)
2435 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2438 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2440 /* select first item in the 'available' list */
2441 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2443 /* append 'virtual' separator button to the 'toolbar buttons' list */
2444 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2445 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2446 btnInfo->btn.fsStyle = BTNS_SEP;
2447 btnInfo->bVirtual = TRUE;
2448 btnInfo->bRemovable = FALSE;
2449 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2450 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2451 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2453 /* select last item in the 'toolbar' list */
2454 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2455 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2457 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2458 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2460 /* set focus and disable buttons */
2461 PostMessageW (hwnd, WM_USER, 0, 0);
2463 return TRUE;
2465 case WM_USER:
2466 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2467 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2468 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2469 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2470 return TRUE;
2472 case WM_CLOSE:
2473 EndDialog(hwnd, FALSE);
2474 return TRUE;
2476 case WM_COMMAND:
2477 switch (LOWORD(wParam))
2479 case IDC_TOOLBARBTN_LBOX:
2480 if (HIWORD(wParam) == LBN_SELCHANGE)
2482 PCUSTOMBUTTON btnInfo;
2483 NMTOOLBARA nmtb;
2484 int count;
2485 int index;
2487 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2488 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2490 /* send TBN_QUERYINSERT notification */
2491 nmtb.iItem = index;
2492 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2494 /* get list box item */
2495 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2497 if (index == (count - 1))
2499 /* last item (virtual separator) */
2500 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2501 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2503 else if (index == (count - 2))
2505 /* second last item (last non-virtual item) */
2506 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2507 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2509 else if (index == 0)
2511 /* first item */
2512 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2513 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2515 else
2517 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2518 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2521 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2523 break;
2525 case IDC_MOVEUP_BTN:
2527 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2528 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2530 break;
2532 case IDC_MOVEDN_BTN: /* move down */
2534 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2535 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2537 break;
2539 case IDC_REMOVE_BTN: /* remove button */
2541 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2543 if (LB_ERR == index)
2544 break;
2546 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2548 break;
2549 case IDC_HELP_BTN:
2550 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2551 break;
2552 case IDC_RESET_BTN:
2553 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2554 break;
2556 case IDOK: /* Add button */
2558 int index;
2559 int indexto;
2561 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2562 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2564 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2566 break;
2568 case IDCANCEL:
2569 EndDialog(hwnd, FALSE);
2570 break;
2572 return TRUE;
2574 case WM_DESTROY:
2576 int count;
2577 int i;
2579 /* delete items from 'toolbar buttons' listbox*/
2580 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2581 for (i = 0; i < count; i++)
2583 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2584 Free(btnInfo);
2585 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2587 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2590 /* delete items from 'available buttons' listbox*/
2591 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2592 for (i = 0; i < count; i++)
2594 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2595 Free(btnInfo);
2596 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2598 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2600 return TRUE;
2602 case WM_DRAWITEM:
2603 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2605 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2606 RECT rcButton;
2607 RECT rcText;
2608 HPEN hPen, hOldPen;
2609 HBRUSH hOldBrush;
2610 COLORREF oldText = 0;
2611 COLORREF oldBk = 0;
2613 /* get item data */
2614 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2615 if (btnInfo == NULL)
2617 FIXME("btnInfo invalid!\n");
2618 return TRUE;
2621 /* set colors and select objects */
2622 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2623 if (btnInfo->bVirtual)
2624 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2625 else
2626 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2627 hPen = CreatePen( PS_SOLID, 1,
2628 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2629 hOldPen = SelectObject (lpdis->hDC, hPen );
2630 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2632 /* fill background rectangle */
2633 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2634 lpdis->rcItem.right, lpdis->rcItem.bottom);
2636 /* calculate button and text rectangles */
2637 CopyRect (&rcButton, &lpdis->rcItem);
2638 InflateRect (&rcButton, -1, -1);
2639 CopyRect (&rcText, &rcButton);
2640 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2641 rcText.left = rcButton.right + 2;
2643 /* draw focus rectangle */
2644 if (lpdis->itemState & ODS_FOCUS)
2645 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2647 /* draw button */
2648 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2649 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2651 /* draw image and text */
2652 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2653 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2654 btnInfo->btn.iBitmap));
2655 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2656 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2658 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2659 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2661 /* delete objects and reset colors */
2662 SelectObject (lpdis->hDC, hOldBrush);
2663 SelectObject (lpdis->hDC, hOldPen);
2664 SetBkColor (lpdis->hDC, oldBk);
2665 SetTextColor (lpdis->hDC, oldText);
2666 DeleteObject( hPen );
2667 return TRUE;
2669 return FALSE;
2671 case WM_MEASUREITEM:
2672 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2674 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2676 lpmis->itemHeight = 15 + 8; /* default height */
2678 return TRUE;
2680 return FALSE;
2682 default:
2683 if (uDragListMessage && (uMsg == uDragListMessage))
2685 if (wParam == IDC_TOOLBARBTN_LBOX)
2687 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2688 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2689 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2690 return TRUE;
2692 else if (wParam == IDC_AVAILBTN_LBOX)
2694 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2695 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2696 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2697 return TRUE;
2700 return FALSE;
2704 static BOOL
2705 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2707 HBITMAP hbmLoad;
2708 INT nCountBefore = ImageList_GetImageCount(himlDef);
2709 INT nCountAfter;
2710 INT cxIcon, cyIcon;
2711 INT nAdded;
2712 INT nIndex;
2714 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2715 /* Add bitmaps to the default image list */
2716 if (bitmap->hInst == NULL) /* a handle was passed */
2717 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2718 else if (bitmap->hInst == COMCTL32_hModule)
2719 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2720 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2721 else
2722 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2724 /* enlarge the bitmap if needed */
2725 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2726 if (bitmap->hInst != COMCTL32_hModule)
2727 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2729 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2730 DeleteObject(hbmLoad);
2731 if (nIndex == -1)
2732 return FALSE;
2734 nCountAfter = ImageList_GetImageCount(himlDef);
2735 nAdded = nCountAfter - nCountBefore;
2736 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2738 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2739 } else if (nAdded > (INT)bitmap->nButtons) {
2740 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2741 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2744 infoPtr->nNumBitmaps += nAdded;
2745 return TRUE;
2748 static void
2749 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2751 HIMAGELIST himlDef;
2752 HIMAGELIST himlNew;
2753 INT cx, cy;
2754 INT i;
2756 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2757 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2758 return;
2759 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2760 return;
2761 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2762 return;
2764 TRACE("Update icon size: %dx%d -> %dx%d\n",
2765 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2767 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2768 ILC_COLOR32|ILC_MASK, 8, 2);
2769 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2770 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2771 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2772 infoPtr->himlInt = himlNew;
2774 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2775 ImageList_Destroy(himlDef);
2778 /***********************************************************************
2779 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2782 static LRESULT
2783 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2785 TBITMAP_INFO info;
2786 INT iSumButtons, i;
2787 HIMAGELIST himlDef;
2789 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2790 if (!lpAddBmp)
2791 return -1;
2793 if (lpAddBmp->hInst == HINST_COMMCTRL)
2795 info.hInst = COMCTL32_hModule;
2796 switch (lpAddBmp->nID)
2798 case IDB_STD_SMALL_COLOR:
2799 info.nButtons = 15;
2800 info.nID = IDB_STD_SMALL;
2801 break;
2802 case IDB_STD_LARGE_COLOR:
2803 info.nButtons = 15;
2804 info.nID = IDB_STD_LARGE;
2805 break;
2806 case IDB_VIEW_SMALL_COLOR:
2807 info.nButtons = 12;
2808 info.nID = IDB_VIEW_SMALL;
2809 break;
2810 case IDB_VIEW_LARGE_COLOR:
2811 info.nButtons = 12;
2812 info.nID = IDB_VIEW_LARGE;
2813 break;
2814 case IDB_HIST_SMALL_COLOR:
2815 info.nButtons = 5;
2816 info.nID = IDB_HIST_SMALL;
2817 break;
2818 case IDB_HIST_LARGE_COLOR:
2819 info.nButtons = 5;
2820 info.nID = IDB_HIST_LARGE;
2821 break;
2822 default:
2823 return -1;
2826 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2828 /* Windows resize all the buttons to the size of a newly added standard image */
2829 if (lpAddBmp->nID & 1)
2831 /* large icons: 24x24. Will make the button 31x30 */
2832 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2834 else
2836 /* small icons: 16x16. Will make the buttons 23x22 */
2837 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2840 TOOLBAR_CalcToolbar (infoPtr);
2842 else
2844 info.nButtons = count;
2845 info.hInst = lpAddBmp->hInst;
2846 info.nID = lpAddBmp->nID;
2847 TRACE("adding %d bitmaps!\n", info.nButtons);
2850 /* check if the bitmap is already loaded and compute iSumButtons */
2851 iSumButtons = 0;
2852 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2854 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2855 infoPtr->bitmaps[i].nID == info.nID)
2856 return iSumButtons;
2857 iSumButtons += infoPtr->bitmaps[i].nButtons;
2860 if (!infoPtr->cimlDef) {
2861 /* create new default image list */
2862 TRACE ("creating default image list!\n");
2864 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2865 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2866 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2867 infoPtr->himlInt = himlDef;
2869 else {
2870 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2873 if (!himlDef) {
2874 WARN("No default image list available\n");
2875 return -1;
2878 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2879 return -1;
2881 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2882 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2883 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2884 infoPtr->nNumBitmapInfos++;
2885 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2887 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2888 return iSumButtons;
2892 static LRESULT
2893 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2895 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
2897 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2901 static LRESULT
2902 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2904 #define MAX_RESOURCE_STRING_LENGTH 512
2905 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2906 INT nIndex = infoPtr->nNumStrings;
2908 TRACE("%p, %lx\n", hInstance, lParam);
2910 if (IS_INTRESOURCE(lParam)) {
2911 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2912 WCHAR delimiter;
2913 WCHAR *next_delim;
2914 HRSRC hrsrc;
2915 WCHAR *p;
2916 INT len;
2918 TRACE("adding string from resource\n");
2920 if (!hInstance) return -1;
2922 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
2923 (LPWSTR)RT_STRING );
2924 if (!hrsrc)
2926 TRACE("string not found in resources\n");
2927 return -1;
2930 len = LoadStringW (hInstance, (UINT)lParam,
2931 szString, MAX_RESOURCE_STRING_LENGTH);
2933 TRACE("len=%d %s\n", len, debugstr_w(szString));
2934 if (len == 0 || len == 1)
2935 return nIndex;
2937 TRACE("delimiter: 0x%x\n", *szString);
2938 delimiter = *szString;
2939 p = szString + 1;
2941 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2942 *next_delim = 0;
2943 if (next_delim + 1 >= szString + len)
2945 /* this may happen if delimiter == '\0' or if the last char is a
2946 * delimiter (then it is ignored like the native does) */
2947 break;
2950 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2951 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2952 infoPtr->nNumStrings++;
2954 p = next_delim + 1;
2957 else {
2958 LPWSTR p = (LPWSTR)lParam;
2959 INT len;
2961 if (p == NULL)
2962 return -1;
2963 TRACE("adding string(s) from array\n");
2964 while (*p) {
2965 len = strlenW (p);
2967 TRACE("len=%d %s\n", len, debugstr_w(p));
2968 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2969 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2970 infoPtr->nNumStrings++;
2972 p += (len+1);
2976 if (fFirstString)
2977 TOOLBAR_CalcToolbar(infoPtr);
2978 return nIndex;
2982 static LRESULT
2983 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2985 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2986 LPSTR p;
2987 INT nIndex;
2988 INT len;
2990 TRACE("%p, %lx\n", hInstance, lParam);
2992 if (IS_INTRESOURCE(lParam)) /* load from resources */
2993 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
2995 p = (LPSTR)lParam;
2996 if (p == NULL)
2997 return -1;
2999 TRACE("adding string(s) from array\n");
3000 nIndex = infoPtr->nNumStrings;
3001 while (*p) {
3002 len = strlen (p);
3003 TRACE("len=%d \"%s\"\n", len, p);
3005 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
3006 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
3007 infoPtr->nNumStrings++;
3009 p += (len+1);
3012 if (fFirstString)
3013 TOOLBAR_CalcToolbar(infoPtr);
3014 return nIndex;
3018 static LRESULT
3019 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3021 RECT parent_rect;
3022 HWND parent;
3023 INT x, y;
3024 INT cx, cy;
3026 TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3028 parent = GetParent (infoPtr->hwndSelf);
3030 if (!parent || !infoPtr->bDoRedraw)
3031 return 0;
3033 GetClientRect(parent, &parent_rect);
3035 x = parent_rect.left;
3036 y = parent_rect.top;
3038 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3040 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3041 cx = parent_rect.right - parent_rect.left;
3043 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL))
3045 TOOLBAR_LayoutToolbar(infoPtr);
3046 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3049 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3051 RECT window_rect;
3052 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3054 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3056 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3057 MapWindowPoints( 0, parent, (POINT *)&window_rect, 2 );
3058 y = window_rect.top;
3060 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3062 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3063 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3066 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3067 uPosFlags |= SWP_NOMOVE;
3069 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3070 cy += GetSystemMetrics(SM_CYEDGE);
3072 if (infoPtr->dwStyle & WS_BORDER)
3074 cx += 2 * GetSystemMetrics(SM_CXBORDER);
3075 cy += 2 * GetSystemMetrics(SM_CYBORDER);
3078 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3081 return 0;
3085 static inline LRESULT
3086 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3088 return infoPtr->nNumButtons;
3092 static inline LRESULT
3093 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3095 infoPtr->dwStructSize = Size;
3097 return 0;
3101 static LRESULT
3102 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3104 TBUTTON_INFO *btnPtr;
3105 INT nIndex;
3107 TRACE("button %d, iBitmap now %d\n", Id, Index);
3109 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3110 if (nIndex == -1)
3111 return FALSE;
3113 btnPtr = &infoPtr->buttons[nIndex];
3114 btnPtr->iBitmap = Index;
3116 /* we HAVE to erase the background, the new bitmap could be */
3117 /* transparent */
3118 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3120 return TRUE;
3124 static LRESULT
3125 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3127 TBUTTON_INFO *btnPtr;
3128 INT nIndex;
3129 INT nOldIndex = -1;
3130 BOOL bChecked = FALSE;
3132 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3134 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3136 if (nIndex == -1)
3137 return FALSE;
3139 btnPtr = &infoPtr->buttons[nIndex];
3141 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) != 0;
3143 if (LOWORD(lParam) == FALSE)
3144 btnPtr->fsState &= ~TBSTATE_CHECKED;
3145 else {
3146 if (btnPtr->fsStyle & BTNS_GROUP) {
3147 nOldIndex =
3148 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3149 if (nOldIndex == nIndex)
3150 return 0;
3151 if (nOldIndex != -1)
3152 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3154 btnPtr->fsState |= TBSTATE_CHECKED;
3157 if( bChecked != LOWORD(lParam) )
3159 if (nOldIndex != -1)
3160 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3161 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3164 /* FIXME: Send a WM_NOTIFY?? */
3166 return TRUE;
3170 static LRESULT
3171 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3173 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3177 static LRESULT
3178 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3180 CUSTDLG_INFO custInfo;
3181 LRESULT ret;
3182 NMHDR nmhdr;
3184 custInfo.tbInfo = infoPtr;
3185 custInfo.tbHwnd = infoPtr->hwndSelf;
3187 /* send TBN_BEGINADJUST notification */
3188 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3190 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3191 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3193 /* send TBN_ENDADJUST notification */
3194 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3196 return ret;
3200 static LRESULT
3201 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3203 NMTOOLBARW nmtb;
3204 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3206 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3207 return FALSE;
3209 memset(&nmtb, 0, sizeof(nmtb));
3210 nmtb.iItem = btnPtr->idCommand;
3211 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3212 nmtb.tbButton.idCommand = btnPtr->idCommand;
3213 nmtb.tbButton.fsState = btnPtr->fsState;
3214 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3215 nmtb.tbButton.dwData = btnPtr->dwData;
3216 nmtb.tbButton.iString = btnPtr->iString;
3217 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3219 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3221 if (infoPtr->nNumButtons == 1) {
3222 TRACE(" simple delete!\n");
3223 if (TOOLBAR_ButtonHasString(infoPtr->buttons))
3224 Free((LPWSTR)infoPtr->buttons[0].iString);
3225 Free (infoPtr->buttons);
3226 infoPtr->buttons = NULL;
3227 infoPtr->nNumButtons = 0;
3229 else {
3230 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3231 TRACE("complex delete! [nIndex=%d]\n", nIndex);
3233 infoPtr->nNumButtons--;
3234 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3235 if (nIndex > 0) {
3236 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3237 nIndex * sizeof(TBUTTON_INFO));
3240 if (nIndex < infoPtr->nNumButtons) {
3241 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3242 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3245 if (TOOLBAR_ButtonHasString(&oldButtons[nIndex]))
3246 Free((LPWSTR)oldButtons[nIndex].iString);
3247 Free (oldButtons);
3250 TOOLBAR_LayoutToolbar(infoPtr);
3252 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3254 return TRUE;
3258 static LRESULT
3259 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3261 TBUTTON_INFO *btnPtr;
3262 INT nIndex;
3263 DWORD bState;
3265 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3267 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3269 if (nIndex == -1)
3270 return FALSE;
3272 btnPtr = &infoPtr->buttons[nIndex];
3274 bState = btnPtr->fsState & TBSTATE_ENABLED;
3276 /* update the toolbar button state */
3277 if(LOWORD(lParam) == FALSE) {
3278 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3279 } else {
3280 btnPtr->fsState |= TBSTATE_ENABLED;
3283 /* redraw the button only if the state of the button changed */
3284 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3285 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3287 return TRUE;
3291 static inline LRESULT
3292 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3294 return infoPtr->bAnchor;
3298 static LRESULT
3299 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3301 INT nIndex;
3303 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3304 if (nIndex == -1)
3305 return -1;
3307 return infoPtr->buttons[nIndex].iBitmap;
3311 static inline LRESULT
3312 TOOLBAR_GetBitmapFlags (void)
3314 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3318 static LRESULT
3319 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3321 TBUTTON_INFO *btnPtr;
3323 if (lpTbb == NULL)
3324 return FALSE;
3326 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3327 return FALSE;
3329 btnPtr = &infoPtr->buttons[nIndex];
3330 lpTbb->iBitmap = btnPtr->iBitmap;
3331 lpTbb->idCommand = btnPtr->idCommand;
3332 lpTbb->fsState = btnPtr->fsState;
3333 lpTbb->fsStyle = btnPtr->fsStyle;
3334 lpTbb->bReserved[0] = 0;
3335 lpTbb->bReserved[1] = 0;
3336 lpTbb->dwData = btnPtr->dwData;
3337 lpTbb->iString = btnPtr->iString;
3339 return TRUE;
3343 static LRESULT
3344 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3346 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3347 TBUTTON_INFO *btnPtr;
3348 INT nIndex;
3350 if (lpTbInfo == NULL)
3351 return -1;
3353 /* MSDN documents an iImageLabel field added in Vista but it is not present in
3354 * the headers and tests shows that even with comctl 6 Vista accepts only the
3355 * original TBBUTTONINFO size
3357 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3359 WARN("Invalid button size\n");
3360 return -1;
3363 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3364 if (nIndex == -1)
3365 return -1;
3367 if (!(btnPtr = &infoPtr->buttons[nIndex])) return -1;
3369 if (lpTbInfo->dwMask & TBIF_COMMAND)
3370 lpTbInfo->idCommand = btnPtr->idCommand;
3371 if (lpTbInfo->dwMask & TBIF_IMAGE)
3372 lpTbInfo->iImage = btnPtr->iBitmap;
3373 if (lpTbInfo->dwMask & TBIF_LPARAM)
3374 lpTbInfo->lParam = btnPtr->dwData;
3375 if (lpTbInfo->dwMask & TBIF_SIZE)
3376 /* tests show that for separators TBIF_SIZE returns not calculated width,
3377 but cx property, that differs from 0 only if application have
3378 specifically set it */
3379 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3380 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3381 if (lpTbInfo->dwMask & TBIF_STATE)
3382 lpTbInfo->fsState = btnPtr->fsState;
3383 if (lpTbInfo->dwMask & TBIF_STYLE)
3384 lpTbInfo->fsStyle = btnPtr->fsStyle;
3385 if (lpTbInfo->dwMask & TBIF_TEXT) {
3386 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3387 can't use TOOLBAR_GetText here */
3388 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3389 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3390 if (bUnicode)
3391 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3392 else
3393 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3394 } else
3395 lpTbInfo->pszText[0] = '\0';
3397 return nIndex;
3401 static inline LRESULT
3402 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3404 return MAKELONG((WORD)infoPtr->nButtonWidth,
3405 (WORD)infoPtr->nButtonHeight);
3409 static LRESULT
3410 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3412 INT nIndex;
3413 LPWSTR lpText;
3414 LRESULT ret = 0;
3416 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3417 if (nIndex == -1)
3418 return -1;
3420 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3422 if (isW)
3424 if (lpText)
3426 ret = strlenW (lpText);
3427 if (lpStr) strcpyW (lpStr, lpText);
3430 else
3431 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3432 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3433 return ret;
3437 static LRESULT
3438 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3440 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3441 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3442 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3446 static inline LRESULT
3447 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3449 TRACE("\n");
3451 return infoPtr->dwExStyle;
3455 static LRESULT
3456 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3458 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3459 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3460 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3464 static LRESULT
3465 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3467 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3468 return -1;
3470 if (infoPtr->nHotItem < 0)
3471 return -1;
3473 return (LRESULT)infoPtr->nHotItem;
3477 static LRESULT
3478 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3480 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3481 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3482 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3486 static LRESULT
3487 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3489 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3491 *lptbim = infoPtr->tbim;
3493 return 0;
3497 static inline LRESULT
3498 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3500 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3502 return (LRESULT)infoPtr->clrInsertMark;
3506 static LRESULT
3507 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3509 TBUTTON_INFO *btnPtr;
3511 btnPtr = &infoPtr->buttons[nIndex];
3512 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3513 return FALSE;
3515 if (lpRect == NULL)
3516 return FALSE;
3517 if (btnPtr->fsState & TBSTATE_HIDDEN)
3518 return FALSE;
3520 lpRect->left = btnPtr->rect.left;
3521 lpRect->right = btnPtr->rect.right;
3522 lpRect->bottom = btnPtr->rect.bottom;
3523 lpRect->top = btnPtr->rect.top;
3525 return TRUE;
3529 static LRESULT
3530 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3532 if (lpSize == NULL)
3533 return FALSE;
3535 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3536 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3538 TRACE("maximum size %d x %d\n",
3539 infoPtr->rcBound.right - infoPtr->rcBound.left,
3540 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3542 return TRUE;
3546 /* << TOOLBAR_GetObject >> */
3549 static inline LRESULT
3550 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3552 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3556 static LRESULT
3557 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3559 TBUTTON_INFO *btnPtr;
3560 INT nIndex;
3562 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3563 btnPtr = &infoPtr->buttons[nIndex];
3564 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3565 return FALSE;
3567 if (lpRect == NULL)
3568 return FALSE;
3570 lpRect->left = btnPtr->rect.left;
3571 lpRect->right = btnPtr->rect.right;
3572 lpRect->bottom = btnPtr->rect.bottom;
3573 lpRect->top = btnPtr->rect.top;
3575 return TRUE;
3579 static inline LRESULT
3580 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3582 return infoPtr->nRows;
3586 static LRESULT
3587 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3589 INT nIndex;
3591 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3592 if (nIndex == -1)
3593 return -1;
3595 return infoPtr->buttons[nIndex].fsState;
3599 static inline LRESULT
3600 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3602 return infoPtr->dwStyle;
3606 static inline LRESULT
3607 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3609 return infoPtr->nMaxTextRows;
3613 static LRESULT
3614 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3616 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3617 TOOLBAR_TooltipCreateControl(infoPtr);
3618 return (LRESULT)infoPtr->hwndToolTip;
3622 static LRESULT
3623 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3625 TRACE("%s hwnd=%p\n",
3626 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3628 return infoPtr->bUnicode;
3632 static inline LRESULT
3633 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3635 return infoPtr->iVersion;
3639 static LRESULT
3640 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3642 TBUTTON_INFO *btnPtr;
3643 BYTE oldState;
3644 INT nIndex;
3646 TRACE("\n");
3648 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3649 if (nIndex == -1)
3650 return FALSE;
3652 btnPtr = &infoPtr->buttons[nIndex];
3653 oldState = btnPtr->fsState;
3655 if (fHide)
3656 btnPtr->fsState |= TBSTATE_HIDDEN;
3657 else
3658 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3660 if (oldState != btnPtr->fsState) {
3661 TOOLBAR_LayoutToolbar (infoPtr);
3662 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3665 return TRUE;
3669 static inline LRESULT
3670 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3672 return TOOLBAR_InternalHitTest (infoPtr, lpPt, NULL);
3676 static LRESULT
3677 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3679 TBUTTON_INFO *btnPtr;
3680 INT nIndex;
3681 DWORD oldState;
3683 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3684 if (nIndex == -1)
3685 return FALSE;
3687 btnPtr = &infoPtr->buttons[nIndex];
3688 oldState = btnPtr->fsState;
3690 if (fIndeterminate)
3691 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3692 else
3693 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3695 if(oldState != btnPtr->fsState)
3696 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3698 return TRUE;
3702 static LRESULT
3703 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3705 if (lpTbb == NULL)
3706 return FALSE;
3708 if (nIndex == -1) {
3709 /* EPP: this seems to be an undocumented call (from my IE4)
3710 * I assume in that case that:
3711 * - index of insertion is at the end of existing buttons
3712 * I only see this happen with nIndex == -1, but it could have a special
3713 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3715 nIndex = infoPtr->nNumButtons;
3717 } else if (nIndex < 0)
3718 return FALSE;
3720 TRACE("inserting button index=%d\n", nIndex);
3721 if (nIndex > infoPtr->nNumButtons) {
3722 nIndex = infoPtr->nNumButtons;
3723 TRACE("adjust index=%d\n", nIndex);
3726 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3729 /* << TOOLBAR_InsertMarkHitTest >> */
3732 static LRESULT
3733 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3735 INT nIndex;
3737 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3738 if (nIndex == -1)
3739 return -1;
3741 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3745 static LRESULT
3746 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3748 INT nIndex;
3750 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3751 if (nIndex == -1)
3752 return -1;
3754 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3758 static LRESULT
3759 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3761 INT nIndex;
3763 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3764 if (nIndex == -1)
3765 return -1;
3767 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3771 static LRESULT
3772 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3774 INT nIndex;
3776 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3777 if (nIndex == -1)
3778 return -1;
3780 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3784 static LRESULT
3785 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3787 INT nIndex;
3789 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3790 if (nIndex == -1)
3791 return -1;
3793 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3797 static LRESULT
3798 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3800 INT nIndex;
3802 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3803 if (nIndex == -1)
3804 return -1;
3806 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3810 static LRESULT
3811 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3813 TBADDBITMAP tbab;
3814 tbab.hInst = hInstance;
3815 tbab.nID = wParam;
3817 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3819 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3823 static LRESULT
3824 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3826 WCHAR wszAccel[] = {'&',wAccel,0};
3827 int i;
3829 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3830 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3832 for (i = 0; i < infoPtr->nNumButtons; i++)
3834 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3835 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3836 !(btnPtr->fsState & TBSTATE_HIDDEN))
3838 int iLen = strlenW(wszAccel);
3839 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3841 if (!lpszStr)
3842 continue;
3844 while (*lpszStr)
3846 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3848 lpszStr += 2;
3849 continue;
3851 if (!strncmpiW(lpszStr, wszAccel, iLen))
3853 *pIDButton = btnPtr->idCommand;
3854 return TRUE;
3856 lpszStr++;
3860 return FALSE;
3864 static LRESULT
3865 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3867 INT nIndex;
3868 DWORD oldState;
3869 TBUTTON_INFO *btnPtr;
3871 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3873 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3874 if (nIndex == -1)
3875 return FALSE;
3877 btnPtr = &infoPtr->buttons[nIndex];
3878 oldState = btnPtr->fsState;
3880 if (fMark)
3881 btnPtr->fsState |= TBSTATE_MARKED;
3882 else
3883 btnPtr->fsState &= ~TBSTATE_MARKED;
3885 if(oldState != btnPtr->fsState)
3886 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3888 return TRUE;
3892 /* fixes up an index of a button affected by a move */
3893 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3895 if (bMoveUp)
3897 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3898 (*pIndex)--;
3899 else if (*pIndex == nIndex)
3900 *pIndex = nMoveIndex;
3902 else
3904 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3905 (*pIndex)++;
3906 else if (*pIndex == nIndex)
3907 *pIndex = nMoveIndex;
3912 static LRESULT
3913 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3915 INT nIndex;
3916 INT nCount;
3917 TBUTTON_INFO button;
3919 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3921 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3922 if ((nIndex == -1) || (nMoveIndex < 0))
3923 return FALSE;
3925 if (nMoveIndex > infoPtr->nNumButtons - 1)
3926 nMoveIndex = infoPtr->nNumButtons - 1;
3928 button = infoPtr->buttons[nIndex];
3930 /* move button right */
3931 if (nIndex < nMoveIndex)
3933 nCount = nMoveIndex - nIndex;
3934 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3935 infoPtr->buttons[nMoveIndex] = button;
3937 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3938 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3939 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3940 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3942 else if (nIndex > nMoveIndex) /* move button left */
3944 nCount = nIndex - nMoveIndex;
3945 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3946 infoPtr->buttons[nMoveIndex] = button;
3948 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3949 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3950 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3951 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3954 TOOLBAR_LayoutToolbar(infoPtr);
3955 TOOLBAR_AutoSize(infoPtr);
3956 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3958 return TRUE;
3962 static LRESULT
3963 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
3965 TBUTTON_INFO *btnPtr;
3966 INT nIndex;
3967 DWORD oldState;
3969 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3970 if (nIndex == -1)
3971 return FALSE;
3973 btnPtr = &infoPtr->buttons[nIndex];
3974 oldState = btnPtr->fsState;
3976 if (fPress)
3977 btnPtr->fsState |= TBSTATE_PRESSED;
3978 else
3979 btnPtr->fsState &= ~TBSTATE_PRESSED;
3981 if(oldState != btnPtr->fsState)
3982 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3984 return TRUE;
3987 /* FIXME: there might still be some confusion her between number of buttons
3988 * and number of bitmaps */
3989 static LRESULT
3990 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
3992 HBITMAP hBitmap;
3993 int i = 0, nOldButtons = 0, pos = 0;
3994 int nOldBitmaps, nNewBitmaps = 0;
3995 HIMAGELIST himlDef = 0;
3997 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
3998 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
3999 lpReplace->nButtons);
4001 if (lpReplace->hInstOld == HINST_COMMCTRL)
4003 FIXME("changing standard bitmaps not implemented\n");
4004 return FALSE;
4006 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
4007 FIXME("resources not in the current module not implemented\n");
4009 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4010 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4011 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4012 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4013 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4015 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4016 nOldButtons = tbi->nButtons;
4017 tbi->nButtons = lpReplace->nButtons;
4018 tbi->hInst = lpReplace->hInstNew;
4019 tbi->nID = lpReplace->nIDNew;
4020 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4021 break;
4023 pos += tbi->nButtons;
4026 if (nOldButtons == 0)
4028 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4029 return FALSE;
4032 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4033 * bitmap
4035 if (lpReplace->hInstNew)
4036 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4037 else
4038 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4040 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4041 nOldBitmaps = ImageList_GetImageCount(himlDef);
4043 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4045 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4046 ImageList_Remove(himlDef, i);
4048 if (hBitmap)
4050 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4051 nNewBitmaps = ImageList_GetImageCount(himlDef);
4052 DeleteObject(hBitmap);
4055 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4057 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4058 pos, nOldBitmaps, nNewBitmaps);
4060 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4061 return TRUE;
4065 /* helper for TOOLBAR_SaveRestoreW */
4066 static BOOL
4067 TOOLBAR_Save(const TBSAVEPARAMSW *lpSave)
4069 FIXME("save to %s %s\n", debugstr_w(lpSave->pszSubKey),
4070 debugstr_w(lpSave->pszValueName));
4072 return FALSE;
4076 /* helper for TOOLBAR_Restore */
4077 static void
4078 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4080 INT i;
4082 for (i = 0; i < infoPtr->nNumButtons; i++)
4084 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4087 Free(infoPtr->buttons);
4088 infoPtr->buttons = NULL;
4089 infoPtr->nNumButtons = 0;
4093 /* helper for TOOLBAR_SaveRestoreW */
4094 static BOOL
4095 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4097 LONG res;
4098 HKEY hkey = NULL;
4099 BOOL ret = FALSE;
4100 DWORD dwType;
4101 DWORD dwSize = 0;
4102 NMTBRESTORE nmtbr;
4104 /* restore toolbar information */
4105 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4106 debugstr_w(lpSave->pszValueName));
4108 memset(&nmtbr, 0, sizeof(nmtbr));
4110 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4111 KEY_QUERY_VALUE, &hkey);
4112 if (!res)
4113 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4114 NULL, &dwSize);
4115 if (!res && dwType != REG_BINARY)
4116 res = ERROR_FILE_NOT_FOUND;
4117 if (!res)
4119 nmtbr.pData = Alloc(dwSize);
4120 nmtbr.cbData = dwSize;
4121 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4123 if (!res)
4124 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4125 (LPBYTE)nmtbr.pData, &dwSize);
4126 if (!res)
4128 nmtbr.pCurrent = nmtbr.pData;
4129 nmtbr.iItem = -1;
4130 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4131 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4133 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4135 INT i;
4137 /* remove all existing buttons as this function is designed to
4138 * restore the toolbar to a previously saved state */
4139 TOOLBAR_DeleteAllButtons(infoPtr);
4141 for (i = 0; i < nmtbr.cButtons; i++)
4143 nmtbr.iItem = i;
4144 nmtbr.tbButton.iBitmap = -1;
4145 nmtbr.tbButton.fsState = 0;
4146 nmtbr.tbButton.fsStyle = 0;
4147 nmtbr.tbButton.idCommand = 0;
4148 if (*nmtbr.pCurrent == (DWORD)-1)
4150 /* separator */
4151 nmtbr.tbButton.fsStyle = TBSTYLE_SEP;
4152 /* when inserting separators, iBitmap controls its size.
4153 0 sets default size (width) */
4154 nmtbr.tbButton.iBitmap = 0;
4156 else if (*nmtbr.pCurrent == (DWORD)-2)
4157 /* hidden button */
4158 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4159 else
4160 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4162 nmtbr.pCurrent++;
4164 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4166 /* can't contain real string as we don't know whether
4167 * the client put an ANSI or Unicode string in there */
4168 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4169 nmtbr.tbButton.iString = 0;
4171 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4174 /* do legacy notifications */
4175 if (infoPtr->iVersion < 5)
4177 /* FIXME: send TBN_BEGINADJUST */
4178 FIXME("send TBN_GETBUTTONINFO for each button\n");
4179 /* FIXME: send TBN_ENDADJUST */
4182 /* remove all uninitialised buttons
4183 * note: loop backwards to avoid having to fixup i on a
4184 * delete */
4185 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4186 if (infoPtr->buttons[i].iBitmap == -1)
4187 TOOLBAR_DeleteButton(infoPtr, i);
4189 /* only indicate success if at least one button survived */
4190 if (infoPtr->nNumButtons > 0) ret = TRUE;
4193 Free (nmtbr.pData);
4194 RegCloseKey(hkey);
4196 return ret;
4200 static LRESULT
4201 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4203 if (lpSave == NULL) return 0;
4205 if (wParam)
4206 return TOOLBAR_Save(lpSave);
4207 else
4208 return TOOLBAR_Restore(infoPtr, lpSave);
4212 static LRESULT
4213 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4215 LPWSTR pszValueName = 0, pszSubKey = 0;
4216 TBSAVEPARAMSW SaveW;
4217 LRESULT result = 0;
4218 int len;
4220 if (lpSave == NULL) return 0;
4222 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4223 pszSubKey = Alloc(len * sizeof(WCHAR));
4224 if (pszSubKey) goto exit;
4225 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4227 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4228 pszValueName = Alloc(len * sizeof(WCHAR));
4229 if (!pszValueName) goto exit;
4230 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4232 SaveW.pszValueName = pszValueName;
4233 SaveW.pszSubKey = pszSubKey;
4234 SaveW.hkr = lpSave->hkr;
4235 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4237 exit:
4238 Free (pszValueName);
4239 Free (pszSubKey);
4241 return result;
4245 static LRESULT
4246 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4248 BOOL bOldAnchor = infoPtr->bAnchor;
4250 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4252 infoPtr->bAnchor = bAnchor;
4254 /* Native does not remove the hot effect from an already hot button */
4256 return (LRESULT)bOldAnchor;
4260 static LRESULT
4261 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4263 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4264 short width = (short)LOWORD(lParam);
4265 short height = (short)HIWORD(lParam);
4267 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", infoPtr->hwndSelf, wParam, lParam);
4269 if (wParam != 0)
4270 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4272 /* 0 width or height is changed to 1 */
4273 if (width == 0)
4274 width = 1;
4275 if (height == 0)
4276 height = 1;
4278 if (infoPtr->nNumButtons > 0)
4279 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4280 infoPtr->nNumButtons,
4281 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4283 if (width < -1 || height < -1)
4285 /* Windows destroys the imagelist and seems to actually use negative
4286 * values to compute button sizes */
4287 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4288 return FALSE;
4291 /* width or height of -1 means no change */
4292 if (width != -1)
4293 infoPtr->nBitmapWidth = width;
4294 if (height != -1)
4295 infoPtr->nBitmapHeight = height;
4297 if ((himlDef == infoPtr->himlInt) &&
4298 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4300 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4301 infoPtr->nBitmapHeight);
4304 TOOLBAR_CalcToolbar(infoPtr);
4305 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4306 return TRUE;
4310 static LRESULT
4311 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4312 const TBBUTTONINFOW *lptbbi, BOOL isW)
4314 TBUTTON_INFO *btnPtr;
4315 INT nIndex;
4316 RECT oldBtnRect;
4318 if (lptbbi == NULL)
4319 return FALSE;
4320 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4321 return FALSE;
4323 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4324 if (nIndex == -1)
4325 return FALSE;
4327 btnPtr = &infoPtr->buttons[nIndex];
4328 if (lptbbi->dwMask & TBIF_COMMAND)
4329 btnPtr->idCommand = lptbbi->idCommand;
4330 if (lptbbi->dwMask & TBIF_IMAGE)
4331 btnPtr->iBitmap = lptbbi->iImage;
4332 if (lptbbi->dwMask & TBIF_LPARAM)
4333 btnPtr->dwData = lptbbi->lParam;
4334 if (lptbbi->dwMask & TBIF_SIZE)
4335 btnPtr->cx = lptbbi->cx;
4336 if (lptbbi->dwMask & TBIF_STATE)
4337 btnPtr->fsState = lptbbi->fsState;
4338 if (lptbbi->dwMask & TBIF_STYLE)
4339 btnPtr->fsStyle = lptbbi->fsStyle;
4341 if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4342 /* iString is index, zero it to make Str_SetPtr succeed */
4343 if (!TOOLBAR_ButtonHasString(btnPtr)) btnPtr->iString = 0;
4345 if (isW)
4346 Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
4347 else
4348 Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, (LPSTR)lptbbi->pszText);
4351 /* save the button rect to see if we need to redraw the whole toolbar */
4352 oldBtnRect = btnPtr->rect;
4353 TOOLBAR_LayoutToolbar(infoPtr);
4355 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4356 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4357 else
4358 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4360 return TRUE;
4364 static LRESULT
4365 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4367 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4369 if ((cx < 0) || (cy < 0))
4371 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4372 return FALSE;
4375 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4377 /* The documentation claims you can only change the button size before
4378 * any button has been added. But this is wrong.
4379 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4380 * it to the toolbar, and it checks that the return value is nonzero - mjm
4381 * Further testing shows that we must actually perform the change too.
4384 * The documentation also does not mention that if 0 is supplied for
4385 * either size, the system changes it to the default of 24 wide and
4386 * 22 high. Demonstrated in ControlSpy Toolbar. GLA 3/02
4388 if (cx == 0) cx = 24;
4389 if (cy == 0) cy = 22;
4391 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4392 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4394 infoPtr->nButtonWidth = cx;
4395 infoPtr->nButtonHeight = cy;
4397 infoPtr->iTopMargin = default_top_margin(infoPtr);
4398 TOOLBAR_LayoutToolbar(infoPtr);
4399 return TRUE;
4403 static LRESULT
4404 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4406 /* if setting to current values, ignore */
4407 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4408 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4409 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4410 infoPtr->cxMin, infoPtr->cxMax);
4411 return TRUE;
4414 /* save new values */
4415 infoPtr->cxMin = (short)LOWORD(lParam);
4416 infoPtr->cxMax = (short)HIWORD(lParam);
4418 /* otherwise we need to recalc the toolbar and in some cases
4419 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4420 which doesn't actually draw - GA). */
4421 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4422 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4424 TOOLBAR_CalcToolbar (infoPtr);
4426 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4428 return TRUE;
4432 static LRESULT
4433 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4435 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4436 return FALSE;
4438 infoPtr->buttons[nIndex].idCommand = nId;
4440 if (infoPtr->hwndToolTip) {
4442 FIXME("change tool tip!\n");
4446 return TRUE;
4450 static LRESULT
4451 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4453 HIMAGELIST himlTemp;
4454 INT id = 0;
4456 if (infoPtr->iVersion >= 5)
4457 id = wParam;
4459 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4460 &infoPtr->cimlDis, himl, id);
4462 /* FIXME: redraw ? */
4464 return (LRESULT)himlTemp;
4468 static LRESULT
4469 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4471 DWORD dwTemp;
4473 TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", infoPtr->hwndSelf, (DWORD)wParam, (DWORD)lParam);
4475 dwTemp = infoPtr->dwDTFlags;
4476 infoPtr->dwDTFlags =
4477 (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4479 return (LRESULT)dwTemp;
4482 /* This function differs a bit from what MSDN says it does:
4483 * 1. lParam contains extended style flags to OR with current style
4484 * (MSDN isn't clear on the OR bit)
4485 * 2. wParam appears to contain extended style flags to be reset
4486 * (MSDN says that this parameter is reserved)
4488 static LRESULT
4489 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, DWORD mask, DWORD style)
4491 DWORD old_style = infoPtr->dwExStyle;
4493 TRACE("mask=0x%08x, style=0x%08x\n", mask, style);
4495 if (mask)
4496 infoPtr->dwExStyle = (old_style & ~mask) | (style & mask);
4497 else
4498 infoPtr->dwExStyle = style;
4500 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4501 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4502 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4504 if ((old_style ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4505 TOOLBAR_CalcToolbar(infoPtr);
4506 else
4507 TOOLBAR_LayoutToolbar(infoPtr);
4509 TOOLBAR_AutoSize(infoPtr);
4510 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4512 return old_style;
4516 static LRESULT
4517 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4519 HIMAGELIST himlTemp;
4520 INT id = 0;
4522 if (infoPtr->iVersion >= 5)
4523 id = wParam;
4525 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4527 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4528 &infoPtr->cimlHot, himl, id);
4530 /* FIXME: redraw ? */
4532 return (LRESULT)himlTemp;
4536 /* Makes previous hot button no longer hot, makes the specified
4537 * button hot and sends appropriate notifications. dwReason is one or
4538 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4539 * NOTE 1: this function does not validate nHit
4540 * NOTE 2: the name of this function is completely made up and
4541 * not based on any documentation from Microsoft. */
4542 static void
4543 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4545 if (infoPtr->nHotItem != nHit)
4547 NMTBHOTITEM nmhotitem;
4548 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4550 nmhotitem.dwFlags = dwReason;
4551 if(infoPtr->nHotItem >= 0)
4553 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4554 nmhotitem.idOld = oldBtnPtr->idCommand;
4556 else
4558 nmhotitem.dwFlags |= HICF_ENTERING;
4559 nmhotitem.idOld = 0;
4562 if (nHit >= 0)
4564 btnPtr = &infoPtr->buttons[nHit];
4565 nmhotitem.idNew = btnPtr->idCommand;
4567 else
4569 nmhotitem.dwFlags |= HICF_LEAVING;
4570 nmhotitem.idNew = 0;
4573 /* now change the hot and invalidate the old and new buttons - if the
4574 * parent agrees */
4575 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4577 if (oldBtnPtr) {
4578 oldBtnPtr->bHot = FALSE;
4579 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4581 /* setting disabled buttons as hot fails even if the notify contains the button id */
4582 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4583 btnPtr->bHot = TRUE;
4584 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4585 infoPtr->nHotItem = nHit;
4587 else
4588 infoPtr->nHotItem = -1;
4593 static LRESULT
4594 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4596 INT nOldHotItem = infoPtr->nHotItem;
4598 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4600 if (nHotItem >= infoPtr->nNumButtons)
4601 return infoPtr->nHotItem;
4603 if (nHotItem < 0)
4604 nHotItem = -1;
4606 /* NOTE: an application can still remove the hot item even if anchor
4607 * highlighting is enabled */
4609 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4611 if (nOldHotItem < 0)
4612 return -1;
4614 return (LRESULT)nOldHotItem;
4618 static LRESULT
4619 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4621 HIMAGELIST himlTemp;
4622 INT oldButtonWidth = infoPtr->nButtonWidth;
4623 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4624 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4625 INT i, id = 0;
4627 if (infoPtr->iVersion >= 5)
4628 id = wParam;
4630 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4631 &infoPtr->cimlDef, himl, id);
4633 infoPtr->nNumBitmaps = 0;
4634 for (i = 0; i < infoPtr->cimlDef; i++)
4635 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4637 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4638 &infoPtr->nBitmapHeight))
4640 infoPtr->nBitmapWidth = 1;
4641 infoPtr->nBitmapHeight = 1;
4643 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4645 TOOLBAR_CalcToolbar(infoPtr);
4646 if (infoPtr->nButtonWidth < oldButtonWidth)
4647 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4650 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4651 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4652 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4654 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4656 return (LRESULT)himlTemp;
4660 static LRESULT
4661 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4663 infoPtr->nIndent = nIndent;
4665 TRACE("\n");
4667 /* process only on indent changing */
4668 if(infoPtr->nIndent != nIndent)
4670 infoPtr->nIndent = nIndent;
4671 TOOLBAR_CalcToolbar (infoPtr);
4672 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4675 return TRUE;
4679 static LRESULT
4680 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4682 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4684 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4686 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4687 return 0;
4690 if ((lptbim->iButton == -1) ||
4691 ((lptbim->iButton < infoPtr->nNumButtons) &&
4692 (lptbim->iButton >= 0)))
4694 infoPtr->tbim = *lptbim;
4695 /* FIXME: don't need to update entire toolbar */
4696 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4698 else
4699 ERR("Invalid button index %d\n", lptbim->iButton);
4701 return 0;
4705 static LRESULT
4706 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4708 infoPtr->clrInsertMark = clr;
4710 /* FIXME: don't need to update entire toolbar */
4711 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4713 return 0;
4717 static LRESULT
4718 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4720 infoPtr->nMaxTextRows = nMaxRows;
4722 TOOLBAR_CalcToolbar(infoPtr);
4723 return TRUE;
4727 /* MSDN gives slightly wrong info on padding.
4728 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4729 * 2. It is not used to create a blank area between the edge of the button
4730 * and the text or image if TBSTYLE_LIST is set. It is used to control
4731 * the gap between the image and text.
4732 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4733 * to control the bottom and right borders [with the border being
4734 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4735 * is shared evenly on both sides of the button.
4736 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4738 static LRESULT
4739 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4741 DWORD oldPad;
4743 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4744 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4745 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4746 TRACE("cx=%d, cy=%d\n",
4747 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4748 return (LRESULT) oldPad;
4752 static LRESULT
4753 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4755 HWND hwndOldNotify;
4757 TRACE("\n");
4759 hwndOldNotify = infoPtr->hwndNotify;
4760 infoPtr->hwndNotify = hParent;
4762 return (LRESULT)hwndOldNotify;
4766 static LRESULT
4767 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4769 int rows = LOWORD(wParam);
4770 BOOL bLarger = HIWORD(wParam);
4772 TRACE("\n");
4774 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4776 if(infoPtr->nRows != rows)
4778 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4779 int curColumn = 0; /* Current column */
4780 int curRow = 0; /* Current row */
4781 int hidden = 0; /* Number of hidden buttons */
4782 int seps = 0; /* Number of separators */
4783 int idealWrap = 0; /* Ideal wrap point */
4784 int i;
4785 BOOL wrap;
4788 Calculate new size and wrap points - Under windows, setrows will
4789 change the dimensions if needed to show the number of requested
4790 rows (if CCS_NORESIZE is set), or will take up the whole window
4791 (if no CCS_NORESIZE).
4793 Basic algorithm - If N buttons, and y rows requested, each row
4794 contains N/y buttons.
4796 FIXME: Handling of separators not obvious from testing results
4797 FIXME: Take width of window into account?
4800 /* Loop through the buttons one by one counting key items */
4801 for (i = 0; i < infoPtr->nNumButtons; i++ )
4803 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4804 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4805 hidden++;
4806 else if (btnPtr[i].fsStyle & BTNS_SEP)
4807 seps++;
4810 /* FIXME: Separators make this quite complex */
4811 if (seps) FIXME("Separators unhandled\n");
4813 /* Round up so more per line, i.e., less rows */
4814 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4816 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4817 achieve the requested number of rows. */
4818 if (bLarger && idealWrap > 1)
4820 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4821 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4823 if (resRows < rows && moreRows > rows)
4825 idealWrap--;
4826 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4830 curColumn = curRow = 0;
4831 wrap = FALSE;
4832 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4833 infoPtr->nNumButtons, hidden, rows);
4835 for (i = 0; i < infoPtr->nNumButtons; i++ )
4837 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4838 continue;
4840 /* Step on, wrap if necessary or flag next to wrap */
4841 if (!wrap) {
4842 curColumn++;
4843 } else {
4844 wrap = FALSE;
4845 curColumn = 1;
4846 curRow++;
4849 if (curColumn > (idealWrap-1)) {
4850 wrap = TRUE;
4851 btnPtr[i].fsState |= TBSTATE_WRAP;
4855 TRACE("Result - %d rows\n", curRow + 1);
4857 /* recalculate toolbar */
4858 TOOLBAR_CalcToolbar (infoPtr);
4860 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4861 if NORESIZE is NOT set, then the toolbar will always be resized to
4862 take up the whole window. With it set, sizing needs to be manual. */
4863 if (infoPtr->dwStyle & CCS_NORESIZE) {
4864 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4865 infoPtr->rcBound.right - infoPtr->rcBound.left,
4866 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4867 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4870 /* repaint toolbar */
4871 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4874 /* return bounding rectangle */
4875 if (lprc) {
4876 lprc->left = infoPtr->rcBound.left;
4877 lprc->right = infoPtr->rcBound.right;
4878 lprc->top = infoPtr->rcBound.top;
4879 lprc->bottom = infoPtr->rcBound.bottom;
4882 return 0;
4886 static LRESULT
4887 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4889 TBUTTON_INFO *btnPtr;
4890 INT nIndex;
4892 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4893 if (nIndex == -1)
4894 return FALSE;
4896 btnPtr = &infoPtr->buttons[nIndex];
4898 /* if hidden state has changed the invalidate entire window and recalc */
4899 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4900 btnPtr->fsState = LOWORD(lParam);
4901 TOOLBAR_CalcToolbar (infoPtr);
4902 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4903 return TRUE;
4906 /* process state changing if current state doesn't match new state */
4907 if(btnPtr->fsState != LOWORD(lParam))
4909 btnPtr->fsState = LOWORD(lParam);
4910 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4913 return TRUE;
4917 static LRESULT
4918 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, DWORD style)
4920 infoPtr->dwStyle = style;
4921 return 0;
4925 static inline LRESULT
4926 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
4928 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
4930 infoPtr->hwndToolTip = hwndTooltip;
4931 return 0;
4935 static LRESULT
4936 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
4938 BOOL bTemp;
4940 TRACE("%s hwnd=%p\n",
4941 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
4943 bTemp = infoPtr->bUnicode;
4944 infoPtr->bUnicode = (BOOL)wParam;
4946 return bTemp;
4950 static LRESULT
4951 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
4953 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
4954 comctl32_color.clrBtnHighlight :
4955 infoPtr->clrBtnHighlight;
4956 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
4957 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
4958 return 1;
4962 static LRESULT
4963 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
4965 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
4966 lParam->clrBtnHighlight, lParam->clrBtnShadow,
4967 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
4969 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
4970 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
4971 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4972 return 0;
4976 static LRESULT
4977 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
4979 INT iOldVersion = infoPtr->iVersion;
4981 infoPtr->iVersion = iVersion;
4983 if (infoPtr->iVersion >= 5)
4984 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
4986 return iOldVersion;
4990 static LRESULT
4991 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
4993 WORD iString = HIWORD(wParam);
4994 WORD buffersize = LOWORD(wParam);
4995 LRESULT ret = -1;
4997 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
4999 if (iString < infoPtr->nNumStrings)
5001 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5002 ret--;
5004 TRACE("returning %s\n", debugstr_a(str));
5006 else
5007 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5009 return ret;
5013 static LRESULT
5014 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
5016 WORD iString = HIWORD(wParam);
5017 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5018 LRESULT ret = -1;
5020 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5022 if (iString < infoPtr->nNumStrings)
5024 len = min(len, strlenW(infoPtr->strings[iString]));
5025 ret = (len+1)*sizeof(WCHAR);
5026 if (str)
5028 memcpy(str, infoPtr->strings[iString], ret);
5029 str[len] = '\0';
5031 ret = len;
5033 TRACE("returning %s\n", debugstr_w(str));
5035 else
5036 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5038 return ret;
5041 /* UNDOCUMENTED MESSAGE: This appears to set some kind of size. Perhaps it
5042 * is the maximum size of the toolbar? */
5043 static LRESULT TOOLBAR_Unkwn45D(HWND hwnd, WPARAM wParam, LPARAM lParam)
5045 SIZE * pSize = (SIZE*)lParam;
5046 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5047 return 0;
5051 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5052 * caller to specify a reason why the hot item changed (rather than just the
5053 * HICF_OTHER that TB_SETHOTITEM sends). */
5054 static LRESULT
5055 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5057 INT nOldHotItem = infoPtr->nHotItem;
5059 TRACE("old item=%d, new item=%d, flags=%08x\n",
5060 nOldHotItem, nHotItem, (DWORD)lParam);
5062 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5063 nHotItem = -1;
5065 /* NOTE: an application can still remove the hot item even if anchor
5066 * highlighting is enabled */
5068 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5070 GetFocus();
5072 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5075 /* Sets the toolbar global iListGap parameter which controls the amount of
5076 * spacing between the image and the text of buttons for TBSTYLE_LIST
5077 * toolbars. */
5078 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5080 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5082 infoPtr->iListGap = iListGap;
5084 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5086 return 0;
5089 /* Returns the number of maximum number of image lists associated with the
5090 * various states. */
5091 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5093 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5095 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5098 static LRESULT
5099 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5101 LPSIZE lpsize = (LPSIZE)lParam;
5103 if (lpsize == NULL)
5104 return FALSE;
5107 * Testing shows the following:
5108 * wParam = 0 adjust cx value
5109 * = 1 set cy value to max size.
5110 * lParam pointer to SIZE structure
5113 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5114 wParam, lParam, lpsize->cx, lpsize->cy);
5116 switch(wParam) {
5117 case 0:
5118 if (lpsize->cx == -1) {
5119 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5121 else if(HIWORD(lpsize->cx)) {
5122 RECT rc;
5123 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5125 GetWindowRect(infoPtr->hwndSelf, &rc);
5126 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5127 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5128 lpsize->cx = max(rc.right-rc.left,
5129 infoPtr->rcBound.right - infoPtr->rcBound.left);
5131 else {
5132 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5134 break;
5135 case 1:
5136 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5137 break;
5138 default:
5139 FIXME("Unknown wParam %ld\n", wParam);
5140 return 0;
5142 TRACE("set to -> 0x%08x 0x%08x\n",
5143 lpsize->cx, lpsize->cy);
5144 return 1;
5147 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5149 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5151 InvalidateRect(hwnd, NULL, TRUE);
5152 return 1;
5156 static LRESULT
5157 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5159 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5160 LOGFONTW logFont;
5162 TRACE("hwnd = %p, style=0x%08x\n", hwnd, lpcs->style);
5164 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5165 GetClientRect(hwnd, &infoPtr->client_rect);
5166 infoPtr->bUnicode = infoPtr->hwndNotify &&
5167 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5168 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5170 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5171 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5173 OpenThemeData (hwnd, themeClass);
5175 TOOLBAR_CheckStyle (infoPtr);
5177 return 0;
5181 static LRESULT
5182 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5184 INT i;
5186 /* delete tooltip control */
5187 if (infoPtr->hwndToolTip)
5188 DestroyWindow (infoPtr->hwndToolTip);
5190 /* delete temporary buffer for tooltip text */
5191 Free (infoPtr->pszTooltipText);
5192 Free (infoPtr->bitmaps); /* bitmaps list */
5194 /* delete button data */
5195 for (i = 0; i < infoPtr->nNumButtons; i++)
5196 if (TOOLBAR_ButtonHasString(&infoPtr->buttons[i]))
5197 Free ((LPWSTR)infoPtr->buttons[i].iString);
5198 Free (infoPtr->buttons);
5200 /* delete strings */
5201 if (infoPtr->strings) {
5202 for (i = 0; i < infoPtr->nNumStrings; i++)
5203 Free (infoPtr->strings[i]);
5205 Free (infoPtr->strings);
5208 /* destroy internal image list */
5209 if (infoPtr->himlInt)
5210 ImageList_Destroy (infoPtr->himlInt);
5212 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5213 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5214 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5216 /* delete default font */
5217 DeleteObject (infoPtr->hDefaultFont);
5219 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
5221 /* free toolbar info data */
5222 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5223 Free (infoPtr);
5225 return 0;
5229 static LRESULT
5230 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5232 NMTBCUSTOMDRAW tbcd;
5233 INT ret = FALSE;
5234 DWORD ntfret;
5235 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5236 DWORD dwEraseCustDraw = 0;
5238 /* the app has told us not to redraw the toolbar */
5239 if (!infoPtr->bDoRedraw)
5240 return FALSE;
5242 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5243 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5244 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5245 tbcd.nmcd.hdc = (HDC)wParam;
5246 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5247 dwEraseCustDraw = ntfret & 0xffff;
5249 /* FIXME: in general the return flags *can* be or'ed together */
5250 switch (dwEraseCustDraw)
5252 case CDRF_DODEFAULT:
5253 break;
5254 case CDRF_SKIPDEFAULT:
5255 return TRUE;
5256 default:
5257 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5258 infoPtr->hwndSelf, ntfret);
5262 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5263 * to my parent for processing.
5265 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5266 POINT pt, ptorig;
5267 HDC hdc = (HDC)wParam;
5268 HWND parent;
5270 pt.x = 0;
5271 pt.y = 0;
5272 parent = GetParent(infoPtr->hwndSelf);
5273 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5274 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5275 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5276 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5278 if (!ret)
5279 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5281 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5282 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5283 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5284 tbcd.nmcd.hdc = (HDC)wParam;
5285 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5286 dwEraseCustDraw = ntfret & 0xffff;
5287 switch (dwEraseCustDraw)
5289 case CDRF_DODEFAULT:
5290 break;
5291 case CDRF_SKIPDEFAULT:
5292 return TRUE;
5293 default:
5294 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5295 infoPtr->hwndSelf, ntfret);
5298 return ret;
5302 static inline LRESULT
5303 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5305 return (LRESULT)infoPtr->hFont;
5309 static void
5310 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5312 INT i;
5313 INT nNewHotItem = infoPtr->nHotItem;
5315 for (i = 0; i < infoPtr->nNumButtons; i++)
5317 /* did we wrap? */
5318 if ((nNewHotItem + iDirection < 0) ||
5319 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5321 NMTBWRAPHOTITEM nmtbwhi;
5322 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5323 nmtbwhi.iDirection = iDirection;
5324 nmtbwhi.dwReason = dwReason;
5326 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5327 return;
5330 nNewHotItem += iDirection;
5331 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5333 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5334 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5336 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5337 break;
5342 static LRESULT
5343 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5345 NMKEY nmkey;
5347 nmkey.nVKey = (UINT)wParam;
5348 nmkey.uFlags = HIWORD(lParam);
5350 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5351 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5353 switch ((UINT)wParam)
5355 case VK_LEFT:
5356 case VK_UP:
5357 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5358 break;
5359 case VK_RIGHT:
5360 case VK_DOWN:
5361 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5362 break;
5363 case VK_SPACE:
5364 case VK_RETURN:
5365 if ((infoPtr->nHotItem >= 0) &&
5366 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5368 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5369 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5370 (LPARAM)infoPtr->hwndSelf);
5372 break;
5375 return 0;
5379 static LRESULT
5380 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5382 POINT pt;
5383 BOOL button;
5385 pt.x = (short)LOWORD(lParam);
5386 pt.y = (short)HIWORD(lParam);
5387 TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5389 if (button)
5390 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5391 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5392 TOOLBAR_Customize (infoPtr);
5394 return 0;
5398 static LRESULT
5399 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5401 TBUTTON_INFO *btnPtr;
5402 POINT pt;
5403 INT nHit;
5404 NMTOOLBARA nmtb;
5405 NMMOUSE nmmouse;
5406 BOOL bDragKeyPressed;
5407 BOOL button;
5409 TRACE("\n");
5411 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5412 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5413 else
5414 bDragKeyPressed = (wParam & MK_SHIFT);
5416 if (infoPtr->hwndToolTip)
5417 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5418 WM_LBUTTONDOWN, wParam, lParam);
5420 pt.x = (short)LOWORD(lParam);
5421 pt.y = (short)HIWORD(lParam);
5422 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5424 if (button)
5425 btnPtr = &infoPtr->buttons[nHit];
5427 if (button && bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5429 infoPtr->nButtonDrag = nHit;
5430 SetCapture (infoPtr->hwndSelf);
5432 /* If drag cursor has not been loaded, load it.
5433 * Note: it doesn't need to be freed */
5434 if (!hCursorDrag)
5435 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5436 SetCursor(hCursorDrag);
5438 else if (button)
5440 RECT arrowRect;
5441 infoPtr->nOldHit = nHit;
5443 CopyRect(&arrowRect, &btnPtr->rect);
5444 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5446 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5447 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5448 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5449 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5450 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5451 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5453 LRESULT res;
5455 /* draw in pressed state */
5456 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5457 btnPtr->fsState |= TBSTATE_PRESSED;
5458 else
5459 btnPtr->bDropDownPressed = TRUE;
5460 RedrawWindow(infoPtr->hwndSelf,&btnPtr->rect,0,
5461 RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5463 memset(&nmtb, 0, sizeof(nmtb));
5464 nmtb.iItem = btnPtr->idCommand;
5465 nmtb.rcButton = btnPtr->rect;
5466 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5467 TBN_DROPDOWN);
5468 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5470 if (res != TBDDRET_TREATPRESSED)
5472 MSG msg;
5474 /* redraw button in unpressed state */
5475 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5476 btnPtr->fsState &= ~TBSTATE_PRESSED;
5477 else
5478 btnPtr->bDropDownPressed = FALSE;
5479 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5481 /* find and set hot item */
5482 GetCursorPos(&pt);
5483 ScreenToClient(infoPtr->hwndSelf, &pt);
5484 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5485 if (!infoPtr->bAnchor || button)
5486 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5488 /* remove any left mouse button down or double-click messages
5489 * so that we can get a toggle effect on the button */
5490 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5491 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5494 return 0;
5496 /* otherwise drop through and process as pushed */
5498 infoPtr->bCaptured = TRUE;
5499 infoPtr->nButtonDown = nHit;
5500 infoPtr->bDragOutSent = FALSE;
5502 btnPtr->fsState |= TBSTATE_PRESSED;
5504 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5506 if (btnPtr->fsState & TBSTATE_ENABLED)
5507 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5508 UpdateWindow(infoPtr->hwndSelf);
5509 SetCapture (infoPtr->hwndSelf);
5512 if (button)
5514 memset(&nmtb, 0, sizeof(nmtb));
5515 nmtb.iItem = btnPtr->idCommand;
5516 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5519 nmmouse.dwHitInfo = nHit;
5521 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5522 if (!button)
5523 nmmouse.dwItemSpec = -1;
5524 else
5526 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5527 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5530 ClientToScreen(infoPtr->hwndSelf, &pt);
5531 nmmouse.pt = pt;
5533 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5534 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5536 return 0;
5539 static LRESULT
5540 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5542 TBUTTON_INFO *btnPtr;
5543 POINT pt;
5544 INT nHit;
5545 INT nOldIndex = -1;
5546 NMHDR hdr;
5547 NMMOUSE nmmouse;
5548 NMTOOLBARA nmtb;
5549 BOOL button;
5551 if (infoPtr->hwndToolTip)
5552 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5553 WM_LBUTTONUP, wParam, lParam);
5555 pt.x = (short)LOWORD(lParam);
5556 pt.y = (short)HIWORD(lParam);
5557 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5559 if (!infoPtr->bAnchor || button)
5560 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5562 if (infoPtr->nButtonDrag >= 0) {
5563 RECT rcClient;
5564 NMHDR hdr;
5566 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5567 ReleaseCapture();
5568 /* reset cursor */
5569 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5571 GetClientRect(infoPtr->hwndSelf, &rcClient);
5572 if (PtInRect(&rcClient, pt))
5574 INT nButton = -1;
5575 if (nHit >= 0)
5576 nButton = nHit;
5577 else if (nHit < -1)
5578 nButton = -nHit;
5579 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5580 nButton = -nHit;
5582 if (nButton == infoPtr->nButtonDrag)
5584 /* if the button is moved sightly left and we have a
5585 * separator there then remove it */
5586 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5588 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5589 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5591 else /* else insert a separator before the dragged button */
5593 TBBUTTON tbb;
5594 memset(&tbb, 0, sizeof(tbb));
5595 tbb.fsStyle = BTNS_SEP;
5596 tbb.iString = -1;
5597 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5600 else
5602 if (nButton == -1)
5604 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5605 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5606 else
5607 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5609 else
5610 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5613 else
5615 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5616 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5619 /* button under cursor changed so need to re-set hot item */
5620 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE | HICF_LMOUSE);
5621 infoPtr->nButtonDrag = -1;
5623 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5625 else if (infoPtr->nButtonDown >= 0) {
5626 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5627 btnPtr->fsState &= ~TBSTATE_PRESSED;
5629 if (btnPtr->fsStyle & BTNS_CHECK) {
5630 if (btnPtr->fsStyle & BTNS_GROUP) {
5631 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5632 nHit);
5633 if ((nOldIndex != nHit) &&
5634 (nOldIndex != -1))
5635 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5636 btnPtr->fsState |= TBSTATE_CHECKED;
5638 else {
5639 if (btnPtr->fsState & TBSTATE_CHECKED)
5640 btnPtr->fsState &= ~TBSTATE_CHECKED;
5641 else
5642 btnPtr->fsState |= TBSTATE_CHECKED;
5646 if (nOldIndex != -1)
5647 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5650 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5651 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5652 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5654 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5655 ReleaseCapture ();
5656 infoPtr->nButtonDown = -1;
5658 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5659 TOOLBAR_SendNotify (&hdr, infoPtr,
5660 NM_RELEASEDCAPTURE);
5662 memset(&nmtb, 0, sizeof(nmtb));
5663 nmtb.iItem = btnPtr->idCommand;
5664 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5665 TBN_ENDDRAG);
5667 if (btnPtr->fsState & TBSTATE_ENABLED)
5669 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5670 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5672 /* In case we have just been destroyed... */
5673 if(!IsWindow(infoPtr->hwndSelf))
5674 return 0;
5678 /* !!! Undocumented - toolbar at 4.71 level and above sends
5679 * NM_CLICK with the NMMOUSE structure. */
5680 nmmouse.dwHitInfo = nHit;
5682 if (!button)
5683 nmmouse.dwItemSpec = -1;
5684 else
5686 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5687 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5690 ClientToScreen(infoPtr->hwndSelf, &pt);
5691 nmmouse.pt = pt;
5693 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5694 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5696 return 0;
5699 static LRESULT
5700 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5702 INT nHit;
5703 NMMOUSE nmmouse;
5704 POINT pt;
5705 BOOL button;
5707 pt.x = (short)LOWORD(lParam);
5708 pt.y = (short)HIWORD(lParam);
5710 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt, &button);
5711 nmmouse.dwHitInfo = nHit;
5713 if (!button) {
5714 nmmouse.dwItemSpec = -1;
5715 } else {
5716 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5717 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5720 ClientToScreen(infoPtr->hwndSelf, &pt);
5721 nmmouse.pt = pt;
5723 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5724 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5726 return 0;
5729 static LRESULT
5730 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5732 NMHDR nmhdr;
5734 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5735 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5737 return 0;
5740 static LRESULT
5741 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5743 TBUTTON_INFO *btnPtr;
5745 infoPtr->bCaptured = FALSE;
5747 if (infoPtr->nButtonDown >= 0)
5749 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5750 btnPtr->fsState &= ~TBSTATE_PRESSED;
5752 infoPtr->nOldHit = -1;
5754 if (btnPtr->fsState & TBSTATE_ENABLED)
5755 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5757 return 0;
5760 static LRESULT
5761 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5763 /* don't remove hot effects when in anchor highlighting mode or when a
5764 * drop-down button is pressed */
5765 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5767 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5768 if (!hotBtnPtr->bDropDownPressed)
5769 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5772 if (infoPtr->nOldHit < 0)
5773 return TRUE;
5775 /* If the last button we were over is depressed then make it not */
5776 /* depressed and redraw it */
5777 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5779 TBUTTON_INFO *btnPtr;
5780 RECT rc1;
5782 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5784 btnPtr->fsState &= ~TBSTATE_PRESSED;
5786 rc1 = btnPtr->rect;
5787 InflateRect (&rc1, 1, 1);
5788 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5791 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5793 NMTOOLBARW nmt;
5794 ZeroMemory(&nmt, sizeof(nmt));
5795 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5796 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5797 infoPtr->bDragOutSent = TRUE;
5800 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5802 return TRUE;
5805 static LRESULT
5806 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5808 POINT pt;
5809 TRACKMOUSEEVENT trackinfo;
5810 INT nHit;
5811 TBUTTON_INFO *btnPtr;
5812 BOOL button;
5814 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5815 TOOLBAR_TooltipCreateControl(infoPtr);
5817 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5818 /* fill in the TRACKMOUSEEVENT struct */
5819 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5820 trackinfo.dwFlags = TME_QUERY;
5822 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5823 _TrackMouseEvent(&trackinfo);
5825 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5826 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5827 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5828 trackinfo.hwndTrack = infoPtr->hwndSelf;
5830 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5831 /* and can properly deactivate the hot toolbar button */
5832 _TrackMouseEvent(&trackinfo);
5836 if (infoPtr->hwndToolTip)
5837 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5838 WM_MOUSEMOVE, wParam, lParam);
5840 pt.x = (short)LOWORD(lParam);
5841 pt.y = (short)HIWORD(lParam);
5843 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt, &button);
5845 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5846 && (!infoPtr->bAnchor || button))
5847 TOOLBAR_SetHotItemEx(infoPtr, button ? nHit : TOOLBAR_NOWHERE, HICF_MOUSE);
5849 if (infoPtr->nOldHit != nHit)
5851 if (infoPtr->bCaptured)
5853 if (!infoPtr->bDragOutSent)
5855 NMTOOLBARW nmt;
5856 ZeroMemory(&nmt, sizeof(nmt));
5857 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5858 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5859 infoPtr->bDragOutSent = TRUE;
5862 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5863 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5864 btnPtr->fsState &= ~TBSTATE_PRESSED;
5865 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5867 else if (nHit == infoPtr->nButtonDown) {
5868 btnPtr->fsState |= TBSTATE_PRESSED;
5869 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5871 infoPtr->nOldHit = nHit;
5875 return 0;
5879 static inline LRESULT
5880 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5882 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
5883 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5884 /* else */
5885 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5889 static inline LRESULT
5890 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
5892 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
5893 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
5895 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
5899 static LRESULT
5900 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
5902 TOOLBAR_INFO *infoPtr;
5903 DWORD styleadd = 0;
5905 /* allocate memory for info structure */
5906 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
5907 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
5909 /* paranoid!! */
5910 infoPtr->dwStructSize = sizeof(TBBUTTON);
5911 infoPtr->nRows = 1;
5912 infoPtr->nWidth = 0;
5914 /* initialize info structure */
5915 infoPtr->nButtonWidth = 23;
5916 infoPtr->nButtonHeight = 22;
5917 infoPtr->nBitmapHeight = 16;
5918 infoPtr->nBitmapWidth = 16;
5920 infoPtr->nMaxTextRows = 1;
5921 infoPtr->cxMin = -1;
5922 infoPtr->cxMax = -1;
5923 infoPtr->nNumBitmaps = 0;
5924 infoPtr->nNumStrings = 0;
5926 infoPtr->bCaptured = FALSE;
5927 infoPtr->nButtonDown = -1;
5928 infoPtr->nButtonDrag = -1;
5929 infoPtr->nOldHit = -1;
5930 infoPtr->nHotItem = -1;
5931 infoPtr->hwndNotify = lpcs->hwndParent;
5932 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
5933 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
5934 infoPtr->bDragOutSent = FALSE;
5935 infoPtr->iVersion = 0;
5936 infoPtr->hwndSelf = hwnd;
5937 infoPtr->bDoRedraw = TRUE;
5938 infoPtr->clrBtnHighlight = CLR_DEFAULT;
5939 infoPtr->clrBtnShadow = CLR_DEFAULT;
5940 infoPtr->szPadding.cx = DEFPAD_CX;
5941 infoPtr->szPadding.cy = DEFPAD_CY;
5942 infoPtr->iListGap = DEFLISTGAP;
5943 infoPtr->iTopMargin = default_top_margin(infoPtr);
5944 infoPtr->dwStyle = lpcs->style;
5945 infoPtr->tbim.iButton = -1;
5947 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
5948 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
5949 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
5950 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
5953 /* I think the code below is a bug, but it is the way that the native
5954 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
5955 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
5956 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
5957 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
5958 * Somehow, the only cases of this seem to be MFC programs.
5960 * Note also that the addition of _TRANSPARENT occurs *only* here. It
5961 * does not occur in the WM_STYLECHANGING routine.
5962 * (Guy Albertelli 9/2001)
5965 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5966 && !(lpcs->style & TBSTYLE_TRANSPARENT))
5967 styleadd |= TBSTYLE_TRANSPARENT;
5968 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
5969 styleadd |= CCS_TOP; /* default to top */
5970 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
5973 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
5977 static LRESULT
5978 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
5980 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5981 RECT rcWindow;
5982 HDC hdc;
5984 if (dwStyle & WS_MINIMIZE)
5985 return 0; /* Nothing to do */
5987 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
5989 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
5990 return 0;
5992 if (!(dwStyle & CCS_NODIVIDER))
5994 GetWindowRect (hwnd, &rcWindow);
5995 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
5996 if( dwStyle & WS_BORDER )
5997 InflateRect (&rcWindow, -1, -1);
5998 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6001 ReleaseDC( hwnd, hdc );
6003 return 0;
6007 /* handles requests from the tooltip control on what text to display */
6008 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6010 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6012 TRACE("button index = %d\n", index);
6014 Free (infoPtr->pszTooltipText);
6015 infoPtr->pszTooltipText = NULL;
6017 if (index < 0)
6018 return 0;
6020 if (infoPtr->bUnicode)
6022 WCHAR wszBuffer[INFOTIPSIZE+1];
6023 NMTBGETINFOTIPW tbgit;
6024 unsigned int len; /* in chars */
6026 wszBuffer[0] = '\0';
6027 wszBuffer[INFOTIPSIZE] = '\0';
6029 tbgit.pszText = wszBuffer;
6030 tbgit.cchTextMax = INFOTIPSIZE;
6031 tbgit.iItem = lpnmtdi->hdr.idFrom;
6032 tbgit.lParam = infoPtr->buttons[index].dwData;
6034 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6036 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6038 len = strlenW(tbgit.pszText);
6039 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6041 /* need to allocate temporary buffer in infoPtr as there
6042 * isn't enough space in buffer passed to us by the
6043 * tooltip control */
6044 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6045 if (infoPtr->pszTooltipText)
6047 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6048 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6049 return 0;
6052 else if (len > 0)
6054 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6055 return 0;
6058 else
6060 CHAR szBuffer[INFOTIPSIZE+1];
6061 NMTBGETINFOTIPA tbgit;
6062 unsigned int len; /* in chars */
6064 szBuffer[0] = '\0';
6065 szBuffer[INFOTIPSIZE] = '\0';
6067 tbgit.pszText = szBuffer;
6068 tbgit.cchTextMax = INFOTIPSIZE;
6069 tbgit.iItem = lpnmtdi->hdr.idFrom;
6070 tbgit.lParam = infoPtr->buttons[index].dwData;
6072 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6074 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6076 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6077 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6079 /* need to allocate temporary buffer in infoPtr as there
6080 * isn't enough space in buffer passed to us by the
6081 * tooltip control */
6082 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6083 if (infoPtr->pszTooltipText)
6085 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6086 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6087 return 0;
6090 else if (tbgit.pszText && tbgit.pszText[0])
6092 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6093 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6094 return 0;
6098 /* if button has text, but it is not shown then automatically
6099 * use that text as tooltip */
6100 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6101 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6103 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6104 unsigned int len = pszText ? strlenW(pszText) : 0;
6106 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6108 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6110 /* need to allocate temporary buffer in infoPtr as there
6111 * isn't enough space in buffer passed to us by the
6112 * tooltip control */
6113 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6114 if (infoPtr->pszTooltipText)
6116 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6117 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6118 return 0;
6121 else if (len > 0)
6123 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6124 return 0;
6128 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6130 /* last resort: send notification on to app */
6131 /* FIXME: find out what is really used here */
6132 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6136 static inline LRESULT
6137 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6139 switch (lpnmh->code)
6141 case PGN_CALCSIZE:
6143 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6145 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6146 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6147 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6148 lppgc->iWidth);
6150 else {
6151 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6152 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6153 lppgc->iHeight);
6155 return 0;
6158 case PGN_SCROLL:
6160 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6162 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6163 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6164 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6165 lppgs->iScroll, lppgs->iDir);
6166 return 0;
6169 case TTN_GETDISPINFOW:
6170 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6172 case TTN_GETDISPINFOA:
6173 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6174 return 0;
6176 default:
6177 return 0;
6182 static LRESULT
6183 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6185 LRESULT format;
6187 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6189 if (lParam == NF_QUERY)
6190 return NFR_UNICODE;
6192 if (lParam == NF_REQUERY) {
6193 format = SendMessageW(infoPtr->hwndNotify,
6194 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6195 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6196 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6197 format);
6198 format = NFR_ANSI;
6200 return format;
6202 return 0;
6206 static LRESULT
6207 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6209 HDC hdc;
6210 PAINTSTRUCT ps;
6212 /* fill ps.rcPaint with a default rect */
6213 ps.rcPaint = infoPtr->rcBound;
6215 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6217 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6219 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6220 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6222 return 0;
6226 static LRESULT
6227 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6229 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6231 /* make first item hot */
6232 if (infoPtr->nNumButtons > 0)
6233 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6235 return 0;
6238 static LRESULT
6239 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6241 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6243 if (hFont == 0)
6244 infoPtr->hFont = infoPtr->hDefaultFont;
6245 else
6246 infoPtr->hFont = hFont;
6248 TOOLBAR_CalcToolbar(infoPtr);
6250 if (Redraw)
6251 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6252 return 1;
6255 static LRESULT
6256 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6257 /*****************************************************
6259 * Function;
6260 * Handles the WM_SETREDRAW message.
6262 * Documentation:
6263 * According to testing V4.71 of COMCTL32 returns the
6264 * *previous* status of the redraw flag (either 0 or 1)
6265 * instead of the MSDN documented value of 0 if handled.
6266 * (For laughs see the "consistency" with same function
6267 * in rebar.)
6269 *****************************************************/
6271 BOOL oldredraw = infoPtr->bDoRedraw;
6273 TRACE("set to %s\n",
6274 (wParam) ? "TRUE" : "FALSE");
6275 infoPtr->bDoRedraw = (BOOL) wParam;
6276 if (wParam) {
6277 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6279 return (oldredraw) ? 1 : 0;
6283 static LRESULT
6284 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6286 TRACE("sizing toolbar!\n");
6288 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6290 RECT delta_width, delta_height, client, dummy;
6291 DWORD min_x, max_x, min_y, max_y;
6292 TBUTTON_INFO *btnPtr;
6293 INT i;
6295 GetClientRect(infoPtr->hwndSelf, &client);
6296 if(client.right > infoPtr->client_rect.right)
6298 min_x = infoPtr->client_rect.right;
6299 max_x = client.right;
6301 else
6303 max_x = infoPtr->client_rect.right;
6304 min_x = client.right;
6306 if(client.bottom > infoPtr->client_rect.bottom)
6308 min_y = infoPtr->client_rect.bottom;
6309 max_y = client.bottom;
6311 else
6313 max_y = infoPtr->client_rect.bottom;
6314 min_y = client.bottom;
6317 SetRect(&delta_width, min_x, 0, max_x, min_y);
6318 SetRect(&delta_height, 0, min_y, max_x, max_y);
6320 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6321 btnPtr = infoPtr->buttons;
6322 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6323 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6324 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6325 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6327 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6328 TOOLBAR_AutoSize(infoPtr);
6329 return 0;
6333 static LRESULT
6334 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6336 if (nType == GWL_STYLE)
6338 DWORD dwOldStyle = infoPtr->dwStyle;
6340 if (lpStyle->styleNew & TBSTYLE_LIST)
6341 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
6342 else
6343 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
6345 TRACE("new style 0x%08x\n", lpStyle->styleNew);
6347 infoPtr->dwStyle = lpStyle->styleNew;
6348 TOOLBAR_CheckStyle (infoPtr);
6350 if ((dwOldStyle ^ lpStyle->styleNew) & (TBSTYLE_WRAPABLE | CCS_VERT))
6351 TOOLBAR_LayoutToolbar(infoPtr);
6353 /* only resize if one of the CCS_* styles was changed */
6354 if ((dwOldStyle ^ lpStyle->styleNew) & COMMON_STYLES)
6356 TOOLBAR_AutoSize (infoPtr);
6358 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6362 return 0;
6366 static LRESULT
6367 TOOLBAR_SysColorChange (void)
6369 COMCTL32_RefreshSysColors();
6371 return 0;
6375 /* update theme after a WM_THEMECHANGED message */
6376 static LRESULT theme_changed (HWND hwnd)
6378 HTHEME theme = GetWindowTheme (hwnd);
6379 CloseThemeData (theme);
6380 OpenThemeData (hwnd, themeClass);
6381 return 0;
6385 static LRESULT WINAPI
6386 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6388 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6390 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6391 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6393 if (!infoPtr && (uMsg != WM_NCCREATE))
6394 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6396 switch (uMsg)
6398 case TB_ADDBITMAP:
6399 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6401 case TB_ADDBUTTONSA:
6402 case TB_ADDBUTTONSW:
6403 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6404 uMsg == TB_ADDBUTTONSW);
6405 case TB_ADDSTRINGA:
6406 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6408 case TB_ADDSTRINGW:
6409 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6411 case TB_AUTOSIZE:
6412 return TOOLBAR_AutoSize (infoPtr);
6414 case TB_BUTTONCOUNT:
6415 return TOOLBAR_ButtonCount (infoPtr);
6417 case TB_BUTTONSTRUCTSIZE:
6418 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6420 case TB_CHANGEBITMAP:
6421 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6423 case TB_CHECKBUTTON:
6424 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6426 case TB_COMMANDTOINDEX:
6427 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6429 case TB_CUSTOMIZE:
6430 return TOOLBAR_Customize (infoPtr);
6432 case TB_DELETEBUTTON:
6433 return TOOLBAR_DeleteButton (infoPtr, wParam);
6435 case TB_ENABLEBUTTON:
6436 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6438 case TB_GETANCHORHIGHLIGHT:
6439 return TOOLBAR_GetAnchorHighlight (infoPtr);
6441 case TB_GETBITMAP:
6442 return TOOLBAR_GetBitmap (infoPtr, wParam);
6444 case TB_GETBITMAPFLAGS:
6445 return TOOLBAR_GetBitmapFlags ();
6447 case TB_GETBUTTON:
6448 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6450 case TB_GETBUTTONINFOA:
6451 case TB_GETBUTTONINFOW:
6452 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6453 uMsg == TB_GETBUTTONINFOW);
6454 case TB_GETBUTTONSIZE:
6455 return TOOLBAR_GetButtonSize (infoPtr);
6457 case TB_GETBUTTONTEXTA:
6458 case TB_GETBUTTONTEXTW:
6459 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6460 uMsg == TB_GETBUTTONTEXTW);
6462 case TB_GETDISABLEDIMAGELIST:
6463 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6465 case TB_GETEXTENDEDSTYLE:
6466 return TOOLBAR_GetExtendedStyle (infoPtr);
6468 case TB_GETHOTIMAGELIST:
6469 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6471 case TB_GETHOTITEM:
6472 return TOOLBAR_GetHotItem (infoPtr);
6474 case TB_GETIMAGELIST:
6475 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6477 case TB_GETINSERTMARK:
6478 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6480 case TB_GETINSERTMARKCOLOR:
6481 return TOOLBAR_GetInsertMarkColor (infoPtr);
6483 case TB_GETITEMRECT:
6484 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6486 case TB_GETMAXSIZE:
6487 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6489 /* case TB_GETOBJECT: */ /* 4.71 */
6491 case TB_GETPADDING:
6492 return TOOLBAR_GetPadding (infoPtr);
6494 case TB_GETRECT:
6495 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6497 case TB_GETROWS:
6498 return TOOLBAR_GetRows (infoPtr);
6500 case TB_GETSTATE:
6501 return TOOLBAR_GetState (infoPtr, wParam);
6503 case TB_GETSTRINGA:
6504 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6506 case TB_GETSTRINGW:
6507 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6509 case TB_GETSTYLE:
6510 return TOOLBAR_GetStyle (infoPtr);
6512 case TB_GETTEXTROWS:
6513 return TOOLBAR_GetTextRows (infoPtr);
6515 case TB_GETTOOLTIPS:
6516 return TOOLBAR_GetToolTips (infoPtr);
6518 case TB_GETUNICODEFORMAT:
6519 return TOOLBAR_GetUnicodeFormat (infoPtr);
6521 case TB_HIDEBUTTON:
6522 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6524 case TB_HITTEST:
6525 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6527 case TB_INDETERMINATE:
6528 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6530 case TB_INSERTBUTTONA:
6531 case TB_INSERTBUTTONW:
6532 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6533 uMsg == TB_INSERTBUTTONW);
6535 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6537 case TB_ISBUTTONCHECKED:
6538 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6540 case TB_ISBUTTONENABLED:
6541 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6543 case TB_ISBUTTONHIDDEN:
6544 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6546 case TB_ISBUTTONHIGHLIGHTED:
6547 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6549 case TB_ISBUTTONINDETERMINATE:
6550 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6552 case TB_ISBUTTONPRESSED:
6553 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6555 case TB_LOADIMAGES:
6556 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6558 case TB_MAPACCELERATORA:
6559 case TB_MAPACCELERATORW:
6560 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6562 case TB_MARKBUTTON:
6563 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6565 case TB_MOVEBUTTON:
6566 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6568 case TB_PRESSBUTTON:
6569 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6571 case TB_REPLACEBITMAP:
6572 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6574 case TB_SAVERESTOREA:
6575 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6577 case TB_SAVERESTOREW:
6578 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6580 case TB_SETANCHORHIGHLIGHT:
6581 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6583 case TB_SETBITMAPSIZE:
6584 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6586 case TB_SETBUTTONINFOA:
6587 case TB_SETBUTTONINFOW:
6588 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6589 uMsg == TB_SETBUTTONINFOW);
6590 case TB_SETBUTTONSIZE:
6591 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6593 case TB_SETBUTTONWIDTH:
6594 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6596 case TB_SETCMDID:
6597 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6599 case TB_SETDISABLEDIMAGELIST:
6600 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6602 case TB_SETDRAWTEXTFLAGS:
6603 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6605 case TB_SETEXTENDEDSTYLE:
6606 return TOOLBAR_SetExtendedStyle (infoPtr, wParam, lParam);
6608 case TB_SETHOTIMAGELIST:
6609 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6611 case TB_SETHOTITEM:
6612 return TOOLBAR_SetHotItem (infoPtr, wParam);
6614 case TB_SETIMAGELIST:
6615 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6617 case TB_SETINDENT:
6618 return TOOLBAR_SetIndent (infoPtr, wParam);
6620 case TB_SETINSERTMARK:
6621 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6623 case TB_SETINSERTMARKCOLOR:
6624 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6626 case TB_SETMAXTEXTROWS:
6627 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6629 case TB_SETPADDING:
6630 return TOOLBAR_SetPadding (infoPtr, lParam);
6632 case TB_SETPARENT:
6633 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6635 case TB_SETROWS:
6636 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6638 case TB_SETSTATE:
6639 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6641 case TB_SETSTYLE:
6642 return TOOLBAR_SetStyle (infoPtr, lParam);
6644 case TB_SETTOOLTIPS:
6645 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6647 case TB_SETUNICODEFORMAT:
6648 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6650 case TB_UNKWN45D:
6651 return TOOLBAR_Unkwn45D(hwnd, wParam, lParam);
6653 case TB_SETHOTITEM2:
6654 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6656 case TB_SETLISTGAP:
6657 return TOOLBAR_SetListGap(infoPtr, wParam);
6659 case TB_GETIMAGELISTCOUNT:
6660 return TOOLBAR_GetImageListCount(infoPtr);
6662 case TB_GETIDEALSIZE:
6663 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6665 case TB_UNKWN464:
6666 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6668 /* Common Control Messages */
6670 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6671 case CCM_GETCOLORSCHEME:
6672 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6674 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6675 case CCM_SETCOLORSCHEME:
6676 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6678 case CCM_GETVERSION:
6679 return TOOLBAR_GetVersion (infoPtr);
6681 case CCM_SETVERSION:
6682 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6685 /* case WM_CHAR: */
6687 case WM_CREATE:
6688 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6690 case WM_DESTROY:
6691 return TOOLBAR_Destroy (infoPtr);
6693 case WM_ERASEBKGND:
6694 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6696 case WM_GETFONT:
6697 return TOOLBAR_GetFont (infoPtr);
6699 case WM_KEYDOWN:
6700 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6702 /* case WM_KILLFOCUS: */
6704 case WM_LBUTTONDBLCLK:
6705 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6707 case WM_LBUTTONDOWN:
6708 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6710 case WM_LBUTTONUP:
6711 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6713 case WM_RBUTTONUP:
6714 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6716 case WM_RBUTTONDBLCLK:
6717 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6719 case WM_MOUSEMOVE:
6720 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6722 case WM_MOUSELEAVE:
6723 return TOOLBAR_MouseLeave (infoPtr);
6725 case WM_CAPTURECHANGED:
6726 return TOOLBAR_CaptureChanged(infoPtr);
6728 case WM_NCACTIVATE:
6729 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6731 case WM_NCCALCSIZE:
6732 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6734 case WM_NCCREATE:
6735 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6737 case WM_NCPAINT:
6738 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6740 case WM_NOTIFY:
6741 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6743 case WM_NOTIFYFORMAT:
6744 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6746 case WM_PRINTCLIENT:
6747 case WM_PAINT:
6748 return TOOLBAR_Paint (infoPtr, wParam);
6750 case WM_SETFOCUS:
6751 return TOOLBAR_SetFocus (infoPtr);
6753 case WM_SETFONT:
6754 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6756 case WM_SETREDRAW:
6757 return TOOLBAR_SetRedraw (infoPtr, wParam);
6759 case WM_SIZE:
6760 return TOOLBAR_Size (infoPtr);
6762 case WM_STYLECHANGED:
6763 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6765 case WM_SYSCOLORCHANGE:
6766 return TOOLBAR_SysColorChange ();
6768 case WM_THEMECHANGED:
6769 return theme_changed (hwnd);
6771 /* case WM_WININICHANGE: */
6773 case WM_CHARTOITEM:
6774 case WM_COMMAND:
6775 case WM_DRAWITEM:
6776 case WM_MEASUREITEM:
6777 case WM_VKEYTOITEM:
6778 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6780 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6781 case PGM_FORWARDMOUSE:
6782 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6784 default:
6785 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6786 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6787 uMsg, wParam, lParam);
6788 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6793 VOID
6794 TOOLBAR_Register (void)
6796 WNDCLASSW wndClass;
6798 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6799 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6800 wndClass.lpfnWndProc = ToolbarWindowProc;
6801 wndClass.cbClsExtra = 0;
6802 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6803 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6804 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6805 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6807 RegisterClassW (&wndClass);
6811 VOID
6812 TOOLBAR_Unregister (void)
6814 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6817 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6819 HIMAGELIST himlold;
6820 PIMLENTRY c = NULL;
6822 /* Check if the entry already exists */
6823 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6825 /* If this is a new entry we must create it and insert into the array */
6826 if (!c)
6828 PIMLENTRY *pnies;
6830 c = Alloc(sizeof(IMLENTRY));
6831 c->id = id;
6833 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6834 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
6835 pnies[*cies] = c;
6836 (*cies)++;
6838 Free(*pies);
6839 *pies = pnies;
6842 himlold = c->himl;
6843 c->himl = himl;
6845 return himlold;
6849 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
6851 int i;
6853 for (i = 0; i < *cies; i++)
6854 Free((*pies)[i]);
6856 Free(*pies);
6858 *cies = 0;
6859 *pies = NULL;
6863 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6865 PIMLENTRY c = NULL;
6867 if (pies != NULL)
6869 int i;
6871 for (i = 0; i < cies; i++)
6873 if (pies[i]->id == id)
6875 c = pies[i];
6876 break;
6881 return c;
6885 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6887 HIMAGELIST himlDef = 0;
6888 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6890 if (pie)
6891 himlDef = pie->himl;
6893 return himlDef;
6897 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6899 if (infoPtr->bUnicode)
6900 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6901 else
6903 CHAR Buffer[256];
6904 NMTOOLBARA nmtba;
6905 BOOL bRet = FALSE;
6907 nmtba.iItem = nmtb->iItem;
6908 nmtba.pszText = Buffer;
6909 nmtba.cchText = 256;
6910 ZeroMemory(nmtba.pszText, nmtba.cchText);
6912 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
6914 int ccht = strlen(nmtba.pszText);
6915 if (ccht)
6916 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
6917 nmtb->pszText, nmtb->cchText);
6919 nmtb->tbButton = nmtba.tbButton;
6920 bRet = TRUE;
6923 return bRet;
6928 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
6930 NMTOOLBARW nmtb;
6932 /* MSDN states that iItem is the index of the button, rather than the
6933 * command ID as used by every other NMTOOLBAR notification */
6934 nmtb.iItem = iItem;
6935 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
6937 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);