wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / comctl32 / toolbar.c
blob9b41732c28979397ad3dd6397f7a7880309ef5df
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 "tmschema.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 BYTE bHot;
98 BYTE 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 #define TOOLBAR_HasText(x, y) (TOOLBAR_GetText(x, y) ? TRUE : FALSE)
221 #define TOOLBAR_HasDropDownArrows(exStyle) ((exStyle & TBSTYLE_EX_DRAWDDARROWS) ? TRUE : FALSE)
223 /* Used to find undocumented extended styles */
224 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
225 TBSTYLE_EX_UNDOC1 | \
226 TBSTYLE_EX_MIXEDBUTTONS | \
227 TBSTYLE_EX_DOUBLEBUFFER | \
228 TBSTYLE_EX_HIDECLIPPEDBUTTONS)
230 /* all of the CCS_ styles */
231 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
232 CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
234 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
235 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
236 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
237 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
238 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
240 static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };
242 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
243 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
244 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
245 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
246 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
247 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
248 static LRESULT TOOLBAR_LButtonDown(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
249 static void TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr);
250 static LRESULT TOOLBAR_AutoSize(TOOLBAR_INFO *infoPtr);
251 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
252 static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
253 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
256 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
258 return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
261 static LPWSTR
262 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
264 LPWSTR lpText = NULL;
266 /* NOTE: iString == -1 is undocumented */
267 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
268 lpText = (LPWSTR)btnPtr->iString;
269 else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
270 lpText = infoPtr->strings[btnPtr->iString];
272 return lpText;
275 static void
276 TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
278 TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx (%s)\n",
279 tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
280 (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
283 static void
284 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
286 if (TRACE_ON(toolbar)){
287 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
288 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
289 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
290 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
291 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
292 btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
293 wine_dbgstr_rect(&bP->rect));
298 static void
299 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
301 if (TRACE_ON(toolbar)) {
302 INT i;
304 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
305 iP->hwndSelf, line,
306 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
307 iP->nNumStrings, iP->dwStyle);
308 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
309 iP->hwndSelf, line,
310 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
311 (iP->bDoRedraw) ? "TRUE" : "FALSE");
312 for(i=0; i<iP->nNumButtons; i++) {
313 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
318 static inline BOOL
319 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
321 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
324 /***********************************************************************
325 * TOOLBAR_CheckStyle
327 * This function validates that the styles set are implemented and
328 * issues FIXME's warning of possible problems. In a perfect world this
329 * function should be null.
331 static void
332 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr, DWORD dwStyle)
334 if (dwStyle & TBSTYLE_REGISTERDROP)
335 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
339 static INT
340 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
342 if(!IsWindow(infoPtr->hwndSelf))
343 return 0; /* we have just been destroyed */
345 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
346 nmhdr->hwndFrom = infoPtr->hwndSelf;
347 nmhdr->code = code;
349 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
350 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
352 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
355 /***********************************************************************
356 * TOOLBAR_GetBitmapIndex
358 * This function returns the bitmap index associated with a button.
359 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
360 * is issued to retrieve the index.
362 static INT
363 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
365 INT ret = btnPtr->iBitmap;
367 if (ret == I_IMAGECALLBACK)
369 /* issue TBN_GETDISPINFO */
370 NMTBDISPINFOW nmgd;
372 memset(&nmgd, 0, sizeof(nmgd));
373 nmgd.idCommand = btnPtr->idCommand;
374 nmgd.lParam = btnPtr->dwData;
375 nmgd.dwMask = TBNF_IMAGE;
376 nmgd.iImage = -1;
377 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
378 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
379 if (nmgd.dwMask & TBNF_DI_SETITEM)
380 btnPtr->iBitmap = nmgd.iImage;
381 ret = nmgd.iImage;
382 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
383 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
386 if (ret != I_IMAGENONE)
387 ret = GETIBITMAP(infoPtr, ret);
389 return ret;
393 static BOOL
394 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
396 HIMAGELIST himl;
397 INT id = GETHIMLID(infoPtr, index);
398 INT iBitmap = GETIBITMAP(infoPtr, index);
400 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
401 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
402 (index == I_IMAGECALLBACK))
403 return TRUE;
404 else
405 return FALSE;
409 static inline BOOL
410 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
412 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
413 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
417 /***********************************************************************
418 * TOOLBAR_GetImageListForDrawing
420 * This function validates the bitmap index (including I_IMAGECALLBACK
421 * functionality) and returns the corresponding image list.
423 static HIMAGELIST
424 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
425 IMAGE_LIST_TYPE imagelist, INT * index)
427 HIMAGELIST himl;
429 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
430 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
431 ERR("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
432 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
433 return NULL;
436 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
437 if ((*index == I_IMAGECALLBACK) ||
438 (*index == I_IMAGENONE)) return NULL;
439 ERR("TBN_GETDISPINFO returned invalid index %d\n",
440 *index);
441 return NULL;
444 switch(imagelist)
446 case IMAGE_LIST_DEFAULT:
447 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
448 break;
449 case IMAGE_LIST_HOT:
450 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
451 break;
452 case IMAGE_LIST_DISABLED:
453 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
454 break;
455 default:
456 himl = NULL;
457 FIXME("Shouldn't reach here\n");
460 if (!himl)
461 TRACE("no image list\n");
463 return himl;
467 static void
468 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
470 RECT myrect;
471 COLORREF oldcolor, newcolor;
473 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
474 myrect.right = myrect.left + 1;
475 myrect.top = lpRect->top + 2;
476 myrect.bottom = lpRect->bottom - 2;
478 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
479 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
480 oldcolor = SetBkColor (hdc, newcolor);
481 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
483 myrect.left = myrect.right;
484 myrect.right = myrect.left + 1;
486 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
487 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
488 SetBkColor (hdc, newcolor);
489 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
491 SetBkColor (hdc, oldcolor);
495 /***********************************************************************
496 * TOOLBAR_DrawFlatHorizontalSeparator
498 * This function draws horizontal separator for toolbars having CCS_VERT style.
499 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
500 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
501 * are horizontal as opposed to the vertical separators for not dropdown
502 * type.
504 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
506 static void
507 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
508 const TOOLBAR_INFO *infoPtr)
510 RECT myrect;
511 COLORREF oldcolor, newcolor;
513 myrect.left = lpRect->left;
514 myrect.right = lpRect->right;
515 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
516 myrect.bottom = myrect.top + 1;
518 InflateRect (&myrect, -2, 0);
520 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
522 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
523 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
524 oldcolor = SetBkColor (hdc, newcolor);
525 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
527 myrect.top = myrect.bottom;
528 myrect.bottom = myrect.top + 1;
530 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
531 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
532 SetBkColor (hdc, newcolor);
533 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
535 SetBkColor (hdc, oldcolor);
539 static void
540 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
542 INT x, y;
543 HPEN hPen, hOldPen;
545 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
546 hOldPen = SelectObject ( hdc, hPen );
547 x = left + 2;
548 y = top;
549 MoveToEx (hdc, x, y, NULL);
550 LineTo (hdc, x+5, y++); x++;
551 MoveToEx (hdc, x, y, NULL);
552 LineTo (hdc, x+3, y++); x++;
553 MoveToEx (hdc, x, y, NULL);
554 LineTo (hdc, x+1, y);
555 SelectObject( hdc, hOldPen );
556 DeleteObject( hPen );
560 * Draw the text string for this button.
561 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
562 * is non-zero, so we can simply check himlDef to see if we have
563 * an image list
565 static void
566 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
567 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
569 HDC hdc = tbcd->nmcd.hdc;
570 HFONT hOldFont = 0;
571 COLORREF clrOld = 0;
572 COLORREF clrOldBk = 0;
573 int oldBkMode = 0;
574 UINT state = tbcd->nmcd.uItemState;
576 /* draw text */
577 if (lpText) {
578 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
579 wine_dbgstr_rect(rcText));
581 hOldFont = SelectObject (hdc, infoPtr->hFont);
582 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
583 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
585 else if (state & CDIS_DISABLED) {
586 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
587 OffsetRect (rcText, 1, 1);
588 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
589 SetTextColor (hdc, comctl32_color.clr3dShadow);
590 OffsetRect (rcText, -1, -1);
592 else if (state & CDIS_INDETERMINATE) {
593 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
595 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
596 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
597 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
598 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
600 else {
601 clrOld = SetTextColor (hdc, tbcd->clrText);
604 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
605 SetTextColor (hdc, clrOld);
606 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
608 SetBkColor (hdc, clrOldBk);
609 SetBkMode (hdc, oldBkMode);
611 SelectObject (hdc, hOldFont);
616 static void
617 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
619 HDC hdc = tbcd->nmcd.hdc;
620 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
621 COLORREF clrTextOld;
622 COLORREF clrBkOld;
623 INT cx = lpRect->right - lpRect->left;
624 INT cy = lpRect->bottom - lpRect->top;
625 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
626 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
627 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
628 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
629 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
630 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
631 SetBkColor(hdc, clrBkOld);
632 SetTextColor(hdc, clrTextOld);
633 SelectObject (hdc, hbr);
637 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
639 INT cx, cy;
640 HBITMAP hbmMask, hbmImage;
641 HDC hdcMask, hdcImage;
643 ImageList_GetIconSize(himl, &cx, &cy);
645 /* Create src image */
646 hdcImage = CreateCompatibleDC(hdc);
647 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
648 SelectObject(hdcImage, hbmImage);
649 ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
650 RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
652 /* Create Mask */
653 hdcMask = CreateCompatibleDC(0);
654 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
655 SelectObject(hdcMask, hbmMask);
657 /* Remove the background and all white pixels */
658 ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
659 RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
660 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
661 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
663 /* draw the new mask 'etched' to hdc */
664 SetBkColor(hdc, RGB(255, 255, 255));
665 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
666 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
667 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
668 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
669 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
671 /* Cleanup */
672 DeleteObject(hbmImage);
673 DeleteDC(hdcImage);
674 DeleteObject (hbmMask);
675 DeleteDC(hdcMask);
679 static UINT
680 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
682 UINT retstate = 0;
684 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
685 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
686 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
687 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
688 retstate |= (btnPtr->bHot ) ? CDIS_HOT : 0;
689 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
690 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
691 return retstate;
694 /* draws the image on a toolbar button */
695 static void
696 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
697 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
699 HIMAGELIST himl = NULL;
700 BOOL draw_masked = FALSE;
701 INT index;
702 INT offset = 0;
703 UINT draw_flags = ILD_TRANSPARENT;
705 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
707 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
708 if (!himl)
710 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
711 draw_masked = TRUE;
714 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
715 ((tbcd->nmcd.uItemState & CDIS_HOT)
716 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
718 /* if hot, attempt to draw with hot image list, if fails,
719 use default image list */
720 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
721 if (!himl)
722 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
724 else
725 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
727 if (!himl)
728 return;
730 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
731 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
732 offset = 1;
734 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
735 (tbcd->nmcd.uItemState & CDIS_MARKED))
736 draw_flags |= ILD_BLEND50;
738 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
739 index, himl, left, top, offset);
741 if (draw_masked)
742 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
743 else
744 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
747 /* draws a blank frame for a toolbar button */
748 static void
749 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
751 HDC hdc = tbcd->nmcd.hdc;
752 RECT rc = tbcd->nmcd.rc;
753 /* if the state is disabled or indeterminate then the button
754 * cannot have an interactive look like pressed or hot */
755 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
756 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
757 BOOL pressed_look = !non_interactive_state &&
758 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
759 (tbcd->nmcd.uItemState & CDIS_CHECKED));
761 /* app don't want us to draw any edges */
762 if (dwItemCDFlag & TBCDRF_NOEDGES)
763 return;
765 if (infoPtr->dwStyle & TBSTYLE_FLAT)
767 if (pressed_look)
768 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
769 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
770 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
772 else
774 if (pressed_look)
775 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
776 else
777 DrawEdge (hdc, &rc, EDGE_RAISED,
778 BF_SOFT | BF_RECT | BF_MIDDLE);
782 static void
783 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
785 HDC hdc = tbcd->nmcd.hdc;
786 int offset = 0;
787 BOOL pressed = bDropDownPressed ||
788 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
790 if (infoPtr->dwStyle & TBSTYLE_FLAT)
792 if (pressed)
793 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
794 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
795 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
796 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
797 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
799 else
801 if (pressed)
802 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
803 else
804 DrawEdge (hdc, rcArrow, EDGE_RAISED,
805 BF_SOFT | BF_RECT | BF_MIDDLE);
808 if (pressed)
809 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
811 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
813 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
814 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
816 else
817 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
820 /* draws a complete toolbar button */
821 static void
822 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
824 DWORD dwStyle = infoPtr->dwStyle;
825 BOOL hasDropDownArrow = (TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) &&
826 (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
827 (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
828 BOOL drawSepDropDownArrow = hasDropDownArrow &&
829 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
830 RECT rc, rcArrow, rcBitmap, rcText;
831 LPWSTR lpText = NULL;
832 NMTBCUSTOMDRAW tbcd;
833 DWORD ntfret;
834 INT offset;
835 INT oldBkMode;
836 DWORD dwItemCustDraw;
837 DWORD dwItemCDFlag;
838 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
840 rc = btnPtr->rect;
841 CopyRect (&rcArrow, &rc);
843 /* separator - doesn't send NM_CUSTOMDRAW */
844 if (btnPtr->fsStyle & BTNS_SEP) {
845 if (theme)
847 DrawThemeBackground (theme, hdc,
848 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
849 &rc, NULL);
851 else
852 /* with the FLAT style, iBitmap is the width and has already */
853 /* been taken into consideration in calculating the width */
854 /* so now we need to draw the vertical separator */
855 /* empirical tests show that iBitmap can/will be non-zero */
856 /* when drawing the vertical bar... */
857 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
858 if (dwStyle & CCS_VERT)
859 TOOLBAR_DrawFlatHorizontalSeparator (&rc, hdc, infoPtr);
860 else
861 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
863 else if (btnPtr->fsStyle != BTNS_SEP) {
864 FIXME("Draw some kind of separator: fsStyle=%x\n",
865 btnPtr->fsStyle);
867 return;
870 /* get a pointer to the text */
871 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
873 if (hasDropDownArrow)
875 int right;
877 if (dwStyle & TBSTYLE_FLAT)
878 right = max(rc.left, rc.right - DDARROW_WIDTH);
879 else
880 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
882 if (drawSepDropDownArrow)
883 rc.right = right;
885 rcArrow.left = right;
888 /* copy text & bitmap rects after adjusting for drop-down arrow
889 * so that text & bitmap is centered in the rectangle not containing
890 * the arrow */
891 CopyRect(&rcText, &rc);
892 CopyRect(&rcBitmap, &rc);
894 /* Center the bitmap horizontally and vertically */
895 if (dwStyle & TBSTYLE_LIST)
897 if (lpText &&
898 infoPtr->nMaxTextRows > 0 &&
899 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
900 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
901 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
902 else
903 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
905 else
906 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
908 rcBitmap.top += infoPtr->szPadding.cy / 2;
910 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
911 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
912 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
913 TRACE("Text=%s\n", debugstr_w(lpText));
914 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
916 /* calculate text position */
917 if (lpText)
919 rcText.left += GetSystemMetrics(SM_CXEDGE);
920 rcText.right -= GetSystemMetrics(SM_CXEDGE);
921 if (dwStyle & TBSTYLE_LIST)
923 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
925 else
927 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
928 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
929 else
930 rcText.top += infoPtr->szPadding.cy/2 + 2;
934 /* Initialize fields in all cases, because we use these later
935 * NOTE: applications can and do alter these to customize their
936 * toolbars */
937 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
938 tbcd.clrText = comctl32_color.clrBtnText;
939 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
940 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
941 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
942 tbcd.clrMark = comctl32_color.clrHighlight;
943 tbcd.clrHighlightHotTrack = 0;
944 tbcd.nStringBkMode = TRANSPARENT;
945 tbcd.nHLStringBkMode = OPAQUE;
946 /* MSDN says that this is the text rectangle.
947 * But (why always a but) tracing of v5.7 of native shows
948 * that this is really a *relative* rectangle based on the
949 * the nmcd.rc. Also the left and top are always 0 ignoring
950 * any bitmap that might be present. */
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 = rc;
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 = rc;
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, &tbcd.nmcd.rc, NULL);
1026 else
1027 TOOLBAR_DrawFrame(infoPtr, &tbcd, 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_HasText(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_UNDOC1 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_UNDOC1) ) 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 | centred | |
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 * | | centred | | 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 * | centred | Bitmap | | |
1525 * |<----------------->| | | |
1526 * | +------------+ | |
1527 * | ^ | |
1528 * | 1 | | |
1529 * | - | |
1530 * | centred +---------------+ | |
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 * | centred +-----------------+ | | 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->nWidth;
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)
1780 TBUTTON_INFO *btnPtr;
1781 INT i;
1783 btnPtr = infoPtr->buttons;
1784 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1785 if (btnPtr->fsState & TBSTATE_HIDDEN)
1786 continue;
1788 if (btnPtr->fsStyle & BTNS_SEP) {
1789 if (PtInRect (&btnPtr->rect, *lpPt)) {
1790 TRACE(" ON SEPARATOR %d!\n", i);
1791 return -i;
1794 else {
1795 if (PtInRect (&btnPtr->rect, *lpPt)) {
1796 TRACE(" ON BUTTON %d!\n", i);
1797 return i;
1802 TRACE(" NOWHERE!\n");
1803 return TOOLBAR_NOWHERE;
1807 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1808 static BOOL
1809 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1811 INT nOldButtons, nNewButtons, iButton;
1812 BOOL fHasString = FALSE;
1814 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1815 iIndex = infoPtr->nNumButtons;
1817 nOldButtons = infoPtr->nNumButtons;
1818 nNewButtons = nOldButtons + nAddButtons;
1820 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1821 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1822 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1823 infoPtr->nNumButtons += nAddButtons;
1825 /* insert new buttons data */
1826 for (iButton = 0; iButton < nAddButtons; iButton++) {
1827 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1829 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1831 ZeroMemory(btnPtr, sizeof(*btnPtr));
1833 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1834 btnPtr->idCommand = lpTbb[iButton].idCommand;
1835 btnPtr->fsState = lpTbb[iButton].fsState;
1836 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1837 btnPtr->dwData = lpTbb[iButton].dwData;
1838 if (btnPtr->fsStyle & BTNS_SEP)
1839 btnPtr->iString = -1;
1840 else if(!IS_INTRESOURCE(lpTbb[iButton].iString) && lpTbb[iButton].iString != -1)
1842 if (fUnicode)
1843 Str_SetPtrW((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[iButton].iString );
1844 else
1845 Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[iButton].iString);
1846 fHasString = TRUE;
1848 else
1849 btnPtr->iString = lpTbb[iButton].iString;
1851 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1854 if (infoPtr->nNumStrings > 0 || fHasString)
1855 TOOLBAR_CalcToolbar(infoPtr);
1856 else
1857 TOOLBAR_LayoutToolbar(infoPtr);
1858 TOOLBAR_AutoSize(infoPtr);
1860 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1861 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1862 return TRUE;
1866 static INT
1867 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1869 TBUTTON_INFO *btnPtr;
1870 INT i;
1872 if (CommandIsIndex) {
1873 TRACE("command is really index command=%d\n", idCommand);
1874 if (idCommand >= infoPtr->nNumButtons) return -1;
1875 return idCommand;
1877 btnPtr = infoPtr->buttons;
1878 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1879 if (btnPtr->idCommand == idCommand) {
1880 TRACE("command=%d index=%d\n", idCommand, i);
1881 return i;
1884 TRACE("no index found for command=%d\n", idCommand);
1885 return -1;
1889 static INT
1890 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1892 TBUTTON_INFO *btnPtr;
1893 INT nRunIndex;
1895 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1896 return -1;
1898 /* check index button */
1899 btnPtr = &infoPtr->buttons[nIndex];
1900 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1901 if (btnPtr->fsState & TBSTATE_CHECKED)
1902 return nIndex;
1905 /* check previous buttons */
1906 nRunIndex = nIndex - 1;
1907 while (nRunIndex >= 0) {
1908 btnPtr = &infoPtr->buttons[nRunIndex];
1909 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1910 if (btnPtr->fsState & TBSTATE_CHECKED)
1911 return nRunIndex;
1913 else
1914 break;
1915 nRunIndex--;
1918 /* check next buttons */
1919 nRunIndex = nIndex + 1;
1920 while (nRunIndex < infoPtr->nNumButtons) {
1921 btnPtr = &infoPtr->buttons[nRunIndex];
1922 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1923 if (btnPtr->fsState & TBSTATE_CHECKED)
1924 return nRunIndex;
1926 else
1927 break;
1928 nRunIndex++;
1931 return -1;
1935 static VOID
1936 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1937 WPARAM wParam, LPARAM lParam)
1939 MSG msg;
1941 msg.hwnd = hwndMsg;
1942 msg.message = uMsg;
1943 msg.wParam = wParam;
1944 msg.lParam = lParam;
1945 msg.time = GetMessageTime ();
1946 msg.pt.x = (short)LOWORD(GetMessagePos ());
1947 msg.pt.y = (short)HIWORD(GetMessagePos ());
1949 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1952 static void
1953 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1955 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1956 TTTOOLINFOW ti;
1958 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1959 ti.cbSize = sizeof (TTTOOLINFOW);
1960 ti.hwnd = infoPtr->hwndSelf;
1961 ti.uId = button->idCommand;
1962 ti.hinst = 0;
1963 ti.lpszText = LPSTR_TEXTCALLBACKW;
1964 /* ti.lParam = random value from the stack? */
1966 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1967 0, (LPARAM)&ti);
1971 static void
1972 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1974 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1975 TTTOOLINFOW ti;
1977 ZeroMemory(&ti, sizeof(ti));
1978 ti.cbSize = sizeof(ti);
1979 ti.hwnd = infoPtr->hwndSelf;
1980 ti.uId = button->idCommand;
1982 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
1986 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1988 /* Set the toolTip only for non-hidden, non-separator button */
1989 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
1991 TTTOOLINFOW ti;
1993 ZeroMemory(&ti, sizeof(ti));
1994 ti.cbSize = sizeof(ti);
1995 ti.hwnd = infoPtr->hwndSelf;
1996 ti.uId = button->idCommand;
1997 ti.rect = button->rect;
1998 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2002 /* Creates the tooltip control */
2003 static void
2004 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2006 int i;
2007 NMTOOLTIPSCREATED nmttc;
2009 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2010 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2011 infoPtr->hwndSelf, 0, 0, 0);
2013 if (!infoPtr->hwndToolTip)
2014 return;
2016 /* Send NM_TOOLTIPSCREATED notification */
2017 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2018 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2020 for (i = 0; i < infoPtr->nNumButtons; i++)
2022 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2023 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2027 /* keeps available button list box sorted by button id */
2028 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2030 int i;
2031 int count;
2032 PCUSTOMBUTTON btnInfo;
2033 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2035 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2037 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2039 /* position 0 is always separator */
2040 for (i = 1; i < count; i++)
2042 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2043 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2045 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2046 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2047 return;
2050 /* id higher than all others add to end */
2051 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2052 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2055 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2057 NMTOOLBARW nmtb;
2059 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2061 if (nIndexFrom == nIndexTo)
2062 return;
2064 /* MSDN states that iItem is the index of the button, rather than the
2065 * command ID as used by every other NMTOOLBAR notification */
2066 nmtb.iItem = nIndexFrom;
2067 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2069 PCUSTOMBUTTON btnInfo;
2070 NMHDR hdr;
2071 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2072 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2074 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2076 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2077 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2078 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2079 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2081 if (nIndexTo <= 0)
2082 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2083 else
2084 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2086 /* last item is always separator, so -2 instead of -1 */
2087 if (nIndexTo >= (count - 2))
2088 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2089 else
2090 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2092 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2093 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2095 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2099 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2101 NMTOOLBARW nmtb;
2103 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2105 /* MSDN states that iItem is the index of the button, rather than the
2106 * command ID as used by every other NMTOOLBAR notification */
2107 nmtb.iItem = nIndexAvail;
2108 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2110 PCUSTOMBUTTON btnInfo;
2111 NMHDR hdr;
2112 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2113 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2114 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2116 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2118 if (nIndexAvail != 0) /* index == 0 indicates separator */
2120 /* remove from 'available buttons' list */
2121 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2122 if (nIndexAvail == count-1)
2123 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2124 else
2125 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2127 else
2129 PCUSTOMBUTTON btnNew;
2131 /* duplicate 'separator' button */
2132 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2133 *btnNew = *btnInfo;
2134 btnInfo = btnNew;
2137 /* insert into 'toolbar button' list */
2138 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2139 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2141 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2143 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2147 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2149 PCUSTOMBUTTON btnInfo;
2150 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2152 TRACE("Remove: index %d\n", index);
2154 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2156 /* send TBN_QUERYDELETE notification */
2157 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2159 NMHDR hdr;
2161 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2162 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2164 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2166 /* insert into 'available button' list */
2167 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2168 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2169 else
2170 Free(btnInfo);
2172 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2176 /* drag list notification function for toolbar buttons list box */
2177 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2178 const DRAGLISTINFO *pDLI)
2180 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2181 switch (pDLI->uNotification)
2183 case DL_BEGINDRAG:
2185 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2186 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2187 /* no dragging for last item (separator) */
2188 if (nCurrentItem >= (nCount - 1)) return FALSE;
2189 return TRUE;
2191 case DL_DRAGGING:
2193 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2194 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2195 /* no dragging past last item (separator) */
2196 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2198 DrawInsert(hwnd, hwndList, nCurrentItem);
2199 /* FIXME: native uses "move button" cursor */
2200 return DL_COPYCURSOR;
2203 /* not over toolbar buttons list */
2204 if (nCurrentItem < 0)
2206 POINT ptWindow = pDLI->ptCursor;
2207 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2208 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2209 /* over available buttons list? */
2210 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2211 /* FIXME: native uses "move button" cursor */
2212 return DL_COPYCURSOR;
2214 /* clear drag arrow */
2215 DrawInsert(hwnd, hwndList, -1);
2216 return DL_STOPCURSOR;
2218 case DL_DROPPED:
2220 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2221 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2222 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2223 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2225 /* clear drag arrow */
2226 DrawInsert(hwnd, hwndList, -1);
2227 /* move item */
2228 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2230 /* not over toolbar buttons list */
2231 if (nIndexTo < 0)
2233 POINT ptWindow = pDLI->ptCursor;
2234 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2235 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2236 /* over available buttons list? */
2237 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2238 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2240 break;
2242 case DL_CANCELDRAG:
2243 /* Clear drag arrow */
2244 DrawInsert(hwnd, hwndList, -1);
2245 break;
2248 return 0;
2251 /* drag list notification function for available buttons list box */
2252 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2253 const DRAGLISTINFO *pDLI)
2255 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2256 switch (pDLI->uNotification)
2258 case DL_BEGINDRAG:
2259 return TRUE;
2260 case DL_DRAGGING:
2262 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2263 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2264 /* no dragging past last item (separator) */
2265 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2267 DrawInsert(hwnd, hwndList, nCurrentItem);
2268 /* FIXME: native uses "move button" cursor */
2269 return DL_COPYCURSOR;
2272 /* not over toolbar buttons list */
2273 if (nCurrentItem < 0)
2275 POINT ptWindow = pDLI->ptCursor;
2276 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2277 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2278 /* over available buttons list? */
2279 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2280 /* FIXME: native uses "move button" cursor */
2281 return DL_COPYCURSOR;
2283 /* clear drag arrow */
2284 DrawInsert(hwnd, hwndList, -1);
2285 return DL_STOPCURSOR;
2287 case DL_DROPPED:
2289 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2290 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2291 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2292 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2294 /* clear drag arrow */
2295 DrawInsert(hwnd, hwndList, -1);
2296 /* add item */
2297 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2300 case DL_CANCELDRAG:
2301 /* Clear drag arrow */
2302 DrawInsert(hwnd, hwndList, -1);
2303 break;
2305 return 0;
2308 extern UINT uDragListMessage;
2310 /***********************************************************************
2311 * TOOLBAR_CustomizeDialogProc
2312 * This function implements the toolbar customization dialog.
2314 static INT_PTR CALLBACK
2315 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2317 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2318 PCUSTOMBUTTON btnInfo;
2319 NMTOOLBARA nmtb;
2320 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2322 switch (uMsg)
2324 case WM_INITDIALOG:
2325 custInfo = (PCUSTDLG_INFO)lParam;
2326 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2328 if (custInfo)
2330 WCHAR Buffer[256];
2331 int i = 0;
2332 int index;
2333 NMTBINITCUSTOMIZE nmtbic;
2335 infoPtr = custInfo->tbInfo;
2337 /* send TBN_QUERYINSERT notification */
2338 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2340 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2341 return FALSE;
2343 nmtbic.hwndDialog = hwnd;
2344 /* Send TBN_INITCUSTOMIZE notification */
2345 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2346 TBNRF_HIDEHELP)
2348 TRACE("TBNRF_HIDEHELP requested\n");
2349 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2352 /* add items to 'toolbar buttons' list and check if removable */
2353 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2355 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2356 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2357 btnInfo->btn.fsStyle = BTNS_SEP;
2358 btnInfo->bVirtual = FALSE;
2359 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2361 /* send TBN_QUERYDELETE notification */
2362 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2364 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2365 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2368 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2370 /* insert separator button into 'available buttons' list */
2371 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2372 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2373 btnInfo->btn.fsStyle = BTNS_SEP;
2374 btnInfo->bVirtual = FALSE;
2375 btnInfo->bRemovable = TRUE;
2376 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2377 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2378 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2380 /* insert all buttons into dsa */
2381 for (i = 0;; i++)
2383 /* send TBN_GETBUTTONINFO notification */
2384 NMTOOLBARW nmtb;
2385 nmtb.iItem = i;
2386 nmtb.pszText = Buffer;
2387 nmtb.cchText = 256;
2389 /* Clear previous button's text */
2390 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2392 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2393 break;
2395 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2396 nmtb.tbButton.fsStyle, i,
2397 nmtb.tbButton.idCommand,
2398 nmtb.tbButton.iString,
2399 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2400 : "");
2402 /* insert button into the apropriate list */
2403 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2404 if (index == -1)
2406 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2407 btnInfo->bVirtual = FALSE;
2408 btnInfo->bRemovable = TRUE;
2410 else
2412 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2413 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2416 btnInfo->btn = nmtb.tbButton;
2417 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2419 if (lstrlenW(nmtb.pszText))
2420 lstrcpyW(btnInfo->text, nmtb.pszText);
2421 else if (nmtb.tbButton.iString >= 0 &&
2422 nmtb.tbButton.iString < infoPtr->nNumStrings)
2424 lstrcpyW(btnInfo->text,
2425 infoPtr->strings[nmtb.tbButton.iString]);
2429 if (index == -1)
2430 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2433 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2435 /* select first item in the 'available' list */
2436 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2438 /* append 'virtual' separator button to the 'toolbar buttons' list */
2439 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2440 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2441 btnInfo->btn.fsStyle = BTNS_SEP;
2442 btnInfo->bVirtual = TRUE;
2443 btnInfo->bRemovable = FALSE;
2444 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2445 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2446 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2448 /* select last item in the 'toolbar' list */
2449 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2450 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2452 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2453 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2455 /* set focus and disable buttons */
2456 PostMessageW (hwnd, WM_USER, 0, 0);
2458 return TRUE;
2460 case WM_USER:
2461 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2462 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2463 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2464 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2465 return TRUE;
2467 case WM_CLOSE:
2468 EndDialog(hwnd, FALSE);
2469 return TRUE;
2471 case WM_COMMAND:
2472 switch (LOWORD(wParam))
2474 case IDC_TOOLBARBTN_LBOX:
2475 if (HIWORD(wParam) == LBN_SELCHANGE)
2477 PCUSTOMBUTTON btnInfo;
2478 NMTOOLBARA nmtb;
2479 int count;
2480 int index;
2482 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2483 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2485 /* send TBN_QUERYINSERT notification */
2486 nmtb.iItem = index;
2487 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2489 /* get list box item */
2490 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2492 if (index == (count - 1))
2494 /* last item (virtual separator) */
2495 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2496 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2498 else if (index == (count - 2))
2500 /* second last item (last non-virtual item) */
2501 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2502 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2504 else if (index == 0)
2506 /* first item */
2507 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2508 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2510 else
2512 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2513 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2516 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2518 break;
2520 case IDC_MOVEUP_BTN:
2522 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2523 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2525 break;
2527 case IDC_MOVEDN_BTN: /* move down */
2529 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2530 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2532 break;
2534 case IDC_REMOVE_BTN: /* remove button */
2536 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2538 if (LB_ERR == index)
2539 break;
2541 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2543 break;
2544 case IDC_HELP_BTN:
2545 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2546 break;
2547 case IDC_RESET_BTN:
2548 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2549 break;
2551 case IDOK: /* Add button */
2553 int index;
2554 int indexto;
2556 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2557 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2559 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2561 break;
2563 case IDCANCEL:
2564 EndDialog(hwnd, FALSE);
2565 break;
2567 return TRUE;
2569 case WM_DESTROY:
2571 int count;
2572 int i;
2574 /* delete items from 'toolbar buttons' listbox*/
2575 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2576 for (i = 0; i < count; i++)
2578 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2579 Free(btnInfo);
2580 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2582 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2585 /* delete items from 'available buttons' listbox*/
2586 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2587 for (i = 0; i < count; i++)
2589 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2590 Free(btnInfo);
2591 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2593 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2595 return TRUE;
2597 case WM_DRAWITEM:
2598 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2600 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2601 RECT rcButton;
2602 RECT rcText;
2603 HPEN hPen, hOldPen;
2604 HBRUSH hOldBrush;
2605 COLORREF oldText = 0;
2606 COLORREF oldBk = 0;
2608 /* get item data */
2609 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2610 if (btnInfo == NULL)
2612 FIXME("btnInfo invalid!\n");
2613 return TRUE;
2616 /* set colors and select objects */
2617 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2618 if (btnInfo->bVirtual)
2619 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2620 else
2621 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2622 hPen = CreatePen( PS_SOLID, 1,
2623 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2624 hOldPen = SelectObject (lpdis->hDC, hPen );
2625 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2627 /* fill background rectangle */
2628 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2629 lpdis->rcItem.right, lpdis->rcItem.bottom);
2631 /* calculate button and text rectangles */
2632 CopyRect (&rcButton, &lpdis->rcItem);
2633 InflateRect (&rcButton, -1, -1);
2634 CopyRect (&rcText, &rcButton);
2635 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2636 rcText.left = rcButton.right + 2;
2638 /* draw focus rectangle */
2639 if (lpdis->itemState & ODS_FOCUS)
2640 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2642 /* draw button */
2643 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2644 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2646 /* draw image and text */
2647 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2648 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2649 btnInfo->btn.iBitmap));
2650 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2651 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2653 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2654 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2656 /* delete objects and reset colors */
2657 SelectObject (lpdis->hDC, hOldBrush);
2658 SelectObject (lpdis->hDC, hOldPen);
2659 SetBkColor (lpdis->hDC, oldBk);
2660 SetTextColor (lpdis->hDC, oldText);
2661 DeleteObject( hPen );
2662 return TRUE;
2664 return FALSE;
2666 case WM_MEASUREITEM:
2667 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2669 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2671 lpmis->itemHeight = 15 + 8; /* default height */
2673 return TRUE;
2675 return FALSE;
2677 default:
2678 if (uDragListMessage && (uMsg == uDragListMessage))
2680 if (wParam == IDC_TOOLBARBTN_LBOX)
2682 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2683 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2684 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2685 return TRUE;
2687 else if (wParam == IDC_AVAILBTN_LBOX)
2689 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2690 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2691 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2692 return TRUE;
2695 return FALSE;
2699 static BOOL
2700 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2702 HBITMAP hbmLoad;
2703 INT nCountBefore = ImageList_GetImageCount(himlDef);
2704 INT nCountAfter;
2705 INT cxIcon, cyIcon;
2706 INT nAdded;
2707 INT nIndex;
2709 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2710 /* Add bitmaps to the default image list */
2711 if (bitmap->hInst == NULL) /* a handle was passed */
2712 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2713 else if (bitmap->hInst == COMCTL32_hModule)
2714 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2715 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2716 else
2717 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2719 /* enlarge the bitmap if needed */
2720 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2721 if (bitmap->hInst != COMCTL32_hModule)
2722 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2724 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2725 DeleteObject(hbmLoad);
2726 if (nIndex == -1)
2727 return FALSE;
2729 nCountAfter = ImageList_GetImageCount(himlDef);
2730 nAdded = nCountAfter - nCountBefore;
2731 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2733 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2734 } else if (nAdded > (INT)bitmap->nButtons) {
2735 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2736 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2739 infoPtr->nNumBitmaps += nAdded;
2740 return TRUE;
2743 static void
2744 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2746 HIMAGELIST himlDef;
2747 HIMAGELIST himlNew;
2748 INT cx, cy;
2749 INT i;
2751 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2752 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2753 return;
2754 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2755 return;
2756 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2757 return;
2759 TRACE("Update icon size: %dx%d -> %dx%d\n",
2760 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2762 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2763 ILC_COLOR32|ILC_MASK, 8, 2);
2764 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2765 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2766 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2767 infoPtr->himlInt = himlNew;
2769 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2770 ImageList_Destroy(himlDef);
2773 /***********************************************************************
2774 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2777 static LRESULT
2778 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2780 TBITMAP_INFO info;
2781 INT iSumButtons, i;
2782 HIMAGELIST himlDef;
2784 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2785 if (!lpAddBmp)
2786 return -1;
2788 if (lpAddBmp->hInst == HINST_COMMCTRL)
2790 info.hInst = COMCTL32_hModule;
2791 switch (lpAddBmp->nID)
2793 case IDB_STD_SMALL_COLOR:
2794 info.nButtons = 15;
2795 info.nID = IDB_STD_SMALL;
2796 break;
2797 case IDB_STD_LARGE_COLOR:
2798 info.nButtons = 15;
2799 info.nID = IDB_STD_LARGE;
2800 break;
2801 case IDB_VIEW_SMALL_COLOR:
2802 info.nButtons = 12;
2803 info.nID = IDB_VIEW_SMALL;
2804 break;
2805 case IDB_VIEW_LARGE_COLOR:
2806 info.nButtons = 12;
2807 info.nID = IDB_VIEW_LARGE;
2808 break;
2809 case IDB_HIST_SMALL_COLOR:
2810 info.nButtons = 5;
2811 info.nID = IDB_HIST_SMALL;
2812 break;
2813 case IDB_HIST_LARGE_COLOR:
2814 info.nButtons = 5;
2815 info.nID = IDB_HIST_LARGE;
2816 break;
2817 default:
2818 return -1;
2821 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2823 /* Windows resize all the buttons to the size of a newly added standard image */
2824 if (lpAddBmp->nID & 1)
2826 /* large icons: 24x24. Will make the button 31x30 */
2827 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2829 else
2831 /* small icons: 16x16. Will make the buttons 23x22 */
2832 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2835 TOOLBAR_CalcToolbar (infoPtr);
2837 else
2839 info.nButtons = count;
2840 info.hInst = lpAddBmp->hInst;
2841 info.nID = lpAddBmp->nID;
2842 TRACE("adding %d bitmaps!\n", info.nButtons);
2845 /* check if the bitmap is already loaded and compute iSumButtons */
2846 iSumButtons = 0;
2847 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2849 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2850 infoPtr->bitmaps[i].nID == info.nID)
2851 return iSumButtons;
2852 iSumButtons += infoPtr->bitmaps[i].nButtons;
2855 if (!infoPtr->cimlDef) {
2856 /* create new default image list */
2857 TRACE ("creating default image list!\n");
2859 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2860 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2861 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2862 infoPtr->himlInt = himlDef;
2864 else {
2865 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2868 if (!himlDef) {
2869 WARN("No default image list available\n");
2870 return -1;
2873 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2874 return -1;
2876 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2877 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2878 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2879 infoPtr->nNumBitmapInfos++;
2880 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2882 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2883 return iSumButtons;
2887 static LRESULT
2888 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2890 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
2892 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2896 static LRESULT
2897 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2899 #define MAX_RESOURCE_STRING_LENGTH 512
2900 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2901 INT nIndex = infoPtr->nNumStrings;
2903 if (hInstance && IS_INTRESOURCE(lParam)) {
2904 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2905 WCHAR delimiter;
2906 WCHAR *next_delim;
2907 WCHAR *p;
2908 INT len;
2909 TRACE("adding string from resource!\n");
2911 len = LoadStringW (hInstance, (UINT)lParam,
2912 szString, MAX_RESOURCE_STRING_LENGTH);
2914 TRACE("len=%d %s\n", len, debugstr_w(szString));
2915 if (len == 0 || len == 1)
2916 return nIndex;
2918 TRACE("Delimiter: 0x%x\n", *szString);
2919 delimiter = *szString;
2920 p = szString + 1;
2922 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2923 *next_delim = 0;
2924 if (next_delim + 1 >= szString + len)
2926 /* this may happen if delimiter == '\0' or if the last char is a
2927 * delimiter (then it is ignored like the native does) */
2928 break;
2931 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2932 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2933 infoPtr->nNumStrings++;
2935 p = next_delim + 1;
2938 else {
2939 LPWSTR p = (LPWSTR)lParam;
2940 INT len;
2942 if (p == NULL)
2943 return -1;
2944 TRACE("adding string(s) from array!\n");
2945 while (*p) {
2946 len = strlenW (p);
2948 TRACE("len=%d %s\n", len, debugstr_w(p));
2949 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2950 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2951 infoPtr->nNumStrings++;
2953 p += (len+1);
2957 if (fFirstString)
2958 TOOLBAR_CalcToolbar(infoPtr);
2959 return nIndex;
2963 static LRESULT
2964 TOOLBAR_AddStringA (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
2966 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2967 LPSTR p;
2968 INT nIndex;
2969 INT len;
2971 if (hInstance && IS_INTRESOURCE(lParam)) /* load from resources */
2972 return TOOLBAR_AddStringW(infoPtr, hInstance, lParam);
2974 p = (LPSTR)lParam;
2975 if (p == NULL)
2976 return -1;
2978 TRACE("adding string(s) from array!\n");
2979 nIndex = infoPtr->nNumStrings;
2980 while (*p) {
2981 len = strlen (p);
2982 TRACE("len=%d \"%s\"\n", len, p);
2984 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2985 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
2986 infoPtr->nNumStrings++;
2988 p += (len+1);
2991 if (fFirstString)
2992 TOOLBAR_CalcToolbar(infoPtr);
2993 return nIndex;
2997 static LRESULT
2998 TOOLBAR_AutoSize (TOOLBAR_INFO *infoPtr)
3000 RECT parent_rect;
3001 HWND parent;
3002 INT x, y;
3003 INT cx, cy;
3005 TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3007 parent = GetParent (infoPtr->hwndSelf);
3009 if (!parent || !infoPtr->bDoRedraw)
3010 return 0;
3012 GetClientRect(parent, &parent_rect);
3014 x = parent_rect.left;
3015 y = parent_rect.top;
3017 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3019 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3020 cx = parent_rect.right - parent_rect.left;
3022 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1))
3024 TOOLBAR_LayoutToolbar(infoPtr);
3025 InvalidateRect( infoPtr->hwndSelf, NULL, TRUE );
3028 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3030 RECT window_rect;
3031 UINT uPosFlags = SWP_NOZORDER | SWP_NOACTIVATE;
3033 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3035 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3036 ScreenToClient(parent, (LPPOINT)&window_rect.left);
3037 y = window_rect.top;
3039 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3041 GetWindowRect(infoPtr->hwndSelf, &window_rect);
3042 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3045 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3046 uPosFlags |= SWP_NOMOVE;
3048 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3049 cy += GetSystemMetrics(SM_CYEDGE);
3051 if (infoPtr->dwStyle & WS_BORDER)
3053 cy += 2 * GetSystemMetrics(SM_CXBORDER);
3054 cx += 2 * GetSystemMetrics(SM_CYBORDER);
3057 SetWindowPos(infoPtr->hwndSelf, NULL, x, y, cx, cy, uPosFlags);
3060 return 0;
3064 static inline LRESULT
3065 TOOLBAR_ButtonCount (const TOOLBAR_INFO *infoPtr)
3067 return infoPtr->nNumButtons;
3071 static inline LRESULT
3072 TOOLBAR_ButtonStructSize (TOOLBAR_INFO *infoPtr, DWORD Size)
3074 infoPtr->dwStructSize = Size;
3076 return 0;
3080 static LRESULT
3081 TOOLBAR_ChangeBitmap (TOOLBAR_INFO *infoPtr, INT Id, INT Index)
3083 TBUTTON_INFO *btnPtr;
3084 INT nIndex;
3086 TRACE("button %d, iBitmap now %d\n", Id, Index);
3088 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3089 if (nIndex == -1)
3090 return FALSE;
3092 btnPtr = &infoPtr->buttons[nIndex];
3093 btnPtr->iBitmap = Index;
3095 /* we HAVE to erase the background, the new bitmap could be */
3096 /* transparent */
3097 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3099 return TRUE;
3103 static LRESULT
3104 TOOLBAR_CheckButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3106 TBUTTON_INFO *btnPtr;
3107 INT nIndex;
3108 INT nOldIndex = -1;
3109 BOOL bChecked = FALSE;
3111 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3113 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, nIndex, lParam);
3115 if (nIndex == -1)
3116 return FALSE;
3118 btnPtr = &infoPtr->buttons[nIndex];
3120 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) ? TRUE : FALSE;
3122 if (LOWORD(lParam) == FALSE)
3123 btnPtr->fsState &= ~TBSTATE_CHECKED;
3124 else {
3125 if (btnPtr->fsStyle & BTNS_GROUP) {
3126 nOldIndex =
3127 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3128 if (nOldIndex == nIndex)
3129 return 0;
3130 if (nOldIndex != -1)
3131 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3133 btnPtr->fsState |= TBSTATE_CHECKED;
3136 if( bChecked != LOWORD(lParam) )
3138 if (nOldIndex != -1)
3139 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
3140 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3143 /* FIXME: Send a WM_NOTIFY?? */
3145 return TRUE;
3149 static LRESULT
3150 TOOLBAR_CommandToIndex (const TOOLBAR_INFO *infoPtr, INT Id)
3152 return TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3156 static LRESULT
3157 TOOLBAR_Customize (TOOLBAR_INFO *infoPtr)
3159 CUSTDLG_INFO custInfo;
3160 LRESULT ret;
3161 NMHDR nmhdr;
3163 custInfo.tbInfo = infoPtr;
3164 custInfo.tbHwnd = infoPtr->hwndSelf;
3166 /* send TBN_BEGINADJUST notification */
3167 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3169 ret = DialogBoxParamW (COMCTL32_hModule, MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3170 infoPtr->hwndSelf, TOOLBAR_CustomizeDialogProc, (LPARAM)&custInfo);
3172 /* send TBN_ENDADJUST notification */
3173 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3175 return ret;
3179 static LRESULT
3180 TOOLBAR_DeleteButton (TOOLBAR_INFO *infoPtr, INT nIndex)
3182 NMTOOLBARW nmtb;
3183 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3185 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3186 return FALSE;
3188 memset(&nmtb, 0, sizeof(nmtb));
3189 nmtb.iItem = btnPtr->idCommand;
3190 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3191 nmtb.tbButton.idCommand = btnPtr->idCommand;
3192 nmtb.tbButton.fsState = btnPtr->fsState;
3193 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3194 nmtb.tbButton.dwData = btnPtr->dwData;
3195 nmtb.tbButton.iString = btnPtr->iString;
3196 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3198 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3200 if (infoPtr->nNumButtons == 1) {
3201 TRACE(" simple delete!\n");
3202 if (TOOLBAR_ButtonHasString(infoPtr->buttons))
3203 Free((LPWSTR)infoPtr->buttons[0].iString);
3204 Free (infoPtr->buttons);
3205 infoPtr->buttons = NULL;
3206 infoPtr->nNumButtons = 0;
3208 else {
3209 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3210 TRACE("complex delete! [nIndex=%d]\n", nIndex);
3212 infoPtr->nNumButtons--;
3213 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3214 if (nIndex > 0) {
3215 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3216 nIndex * sizeof(TBUTTON_INFO));
3219 if (nIndex < infoPtr->nNumButtons) {
3220 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3221 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3224 if (TOOLBAR_ButtonHasString(&oldButtons[nIndex]))
3225 Free((LPWSTR)oldButtons[nIndex].iString);
3226 Free (oldButtons);
3229 TOOLBAR_LayoutToolbar(infoPtr);
3231 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3233 return TRUE;
3237 static LRESULT
3238 TOOLBAR_EnableButton (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
3240 TBUTTON_INFO *btnPtr;
3241 INT nIndex;
3242 DWORD bState;
3244 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3246 TRACE("hwnd=%p, btn id=%d, lParam=0x%08lx\n", infoPtr->hwndSelf, Id, lParam);
3248 if (nIndex == -1)
3249 return FALSE;
3251 btnPtr = &infoPtr->buttons[nIndex];
3253 bState = btnPtr->fsState & TBSTATE_ENABLED;
3255 /* update the toolbar button state */
3256 if(LOWORD(lParam) == FALSE) {
3257 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3258 } else {
3259 btnPtr->fsState |= TBSTATE_ENABLED;
3262 /* redraw the button only if the state of the button changed */
3263 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3264 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3266 return TRUE;
3270 static inline LRESULT
3271 TOOLBAR_GetAnchorHighlight (const TOOLBAR_INFO *infoPtr)
3273 return infoPtr->bAnchor;
3277 static LRESULT
3278 TOOLBAR_GetBitmap (const TOOLBAR_INFO *infoPtr, INT Id)
3280 INT nIndex;
3282 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3283 if (nIndex == -1)
3284 return -1;
3286 return infoPtr->buttons[nIndex].iBitmap;
3290 static inline LRESULT
3291 TOOLBAR_GetBitmapFlags (void)
3293 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3297 static LRESULT
3298 TOOLBAR_GetButton (const TOOLBAR_INFO *infoPtr, INT nIndex, TBBUTTON *lpTbb)
3300 TBUTTON_INFO *btnPtr;
3302 if (lpTbb == NULL)
3303 return FALSE;
3305 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3306 return FALSE;
3308 btnPtr = &infoPtr->buttons[nIndex];
3309 lpTbb->iBitmap = btnPtr->iBitmap;
3310 lpTbb->idCommand = btnPtr->idCommand;
3311 lpTbb->fsState = btnPtr->fsState;
3312 lpTbb->fsStyle = btnPtr->fsStyle;
3313 lpTbb->bReserved[0] = 0;
3314 lpTbb->bReserved[1] = 0;
3315 lpTbb->dwData = btnPtr->dwData;
3316 lpTbb->iString = btnPtr->iString;
3318 return TRUE;
3322 static LRESULT
3323 TOOLBAR_GetButtonInfoT(const TOOLBAR_INFO *infoPtr, INT Id, LPTBBUTTONINFOW lpTbInfo, BOOL bUnicode)
3325 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3326 TBUTTON_INFO *btnPtr;
3327 INT nIndex;
3329 if (lpTbInfo == NULL)
3330 return -1;
3332 /* MSDN documents a iImageLabel field added in Vista but it is not present in
3333 * the headers and tests shows that even with comctl 6 Vista accepts only the
3334 * original TBBUTTONINFO size
3336 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3338 WARN("Invalid button size\n");
3339 return -1;
3342 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lpTbInfo->dwMask & TBIF_BYINDEX);
3343 if (nIndex == -1)
3344 return -1;
3346 if (!(btnPtr = &infoPtr->buttons[nIndex])) return -1;
3348 if (lpTbInfo->dwMask & TBIF_COMMAND)
3349 lpTbInfo->idCommand = btnPtr->idCommand;
3350 if (lpTbInfo->dwMask & TBIF_IMAGE)
3351 lpTbInfo->iImage = btnPtr->iBitmap;
3352 if (lpTbInfo->dwMask & TBIF_LPARAM)
3353 lpTbInfo->lParam = btnPtr->dwData;
3354 if (lpTbInfo->dwMask & TBIF_SIZE)
3355 /* tests show that for separators TBIF_SIZE returns not calculated width,
3356 but cx property, that differs from 0 only if application have
3357 specifically set it */
3358 lpTbInfo->cx = (btnPtr->fsStyle & BTNS_SEP)
3359 ? btnPtr->cx : (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3360 if (lpTbInfo->dwMask & TBIF_STATE)
3361 lpTbInfo->fsState = btnPtr->fsState;
3362 if (lpTbInfo->dwMask & TBIF_STYLE)
3363 lpTbInfo->fsStyle = btnPtr->fsStyle;
3364 if (lpTbInfo->dwMask & TBIF_TEXT) {
3365 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3366 can't use TOOLBAR_GetText here */
3367 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1)) {
3368 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3369 if (bUnicode)
3370 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3371 else
3372 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3373 } else
3374 lpTbInfo->pszText[0] = '\0';
3376 return nIndex;
3380 static inline LRESULT
3381 TOOLBAR_GetButtonSize (const TOOLBAR_INFO *infoPtr)
3383 return MAKELONG((WORD)infoPtr->nButtonWidth,
3384 (WORD)infoPtr->nButtonHeight);
3388 static LRESULT
3389 TOOLBAR_GetButtonText (const TOOLBAR_INFO *infoPtr, INT Id, LPWSTR lpStr, BOOL isW)
3391 INT nIndex;
3392 LPWSTR lpText;
3393 LRESULT ret = 0;
3395 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3396 if (nIndex == -1)
3397 return -1;
3399 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3401 if (isW)
3403 if (lpText)
3405 ret = strlenW (lpText);
3406 if (lpStr) strcpyW (lpStr, lpText);
3409 else
3410 ret = WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3411 (LPSTR)lpStr, lpStr ? 0x7fffffff : 0, NULL, NULL ) - 1;
3412 return ret;
3416 static LRESULT
3417 TOOLBAR_GetDisabledImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3419 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3420 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3421 return (LRESULT)GETDISIMAGELIST(infoPtr, wParam);
3425 static inline LRESULT
3426 TOOLBAR_GetExtendedStyle (const TOOLBAR_INFO *infoPtr)
3428 TRACE("\n");
3430 return infoPtr->dwExStyle;
3434 static LRESULT
3435 TOOLBAR_GetHotImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3437 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3438 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3439 return (LRESULT)GETHOTIMAGELIST(infoPtr, wParam);
3443 static LRESULT
3444 TOOLBAR_GetHotItem (const TOOLBAR_INFO *infoPtr)
3446 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3447 return -1;
3449 if (infoPtr->nHotItem < 0)
3450 return -1;
3452 return (LRESULT)infoPtr->nHotItem;
3456 static LRESULT
3457 TOOLBAR_GetDefImageList (const TOOLBAR_INFO *infoPtr, WPARAM wParam)
3459 TRACE("hwnd=%p, wParam=%ld\n", infoPtr->hwndSelf, wParam);
3460 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3461 return (LRESULT) GETDEFIMAGELIST(infoPtr, wParam);
3465 static LRESULT
3466 TOOLBAR_GetInsertMark (const TOOLBAR_INFO *infoPtr, TBINSERTMARK *lptbim)
3468 TRACE("hwnd = %p, lptbim = %p\n", infoPtr->hwndSelf, lptbim);
3470 *lptbim = infoPtr->tbim;
3472 return 0;
3476 static inline LRESULT
3477 TOOLBAR_GetInsertMarkColor (const TOOLBAR_INFO *infoPtr)
3479 TRACE("hwnd = %p\n", infoPtr->hwndSelf);
3481 return (LRESULT)infoPtr->clrInsertMark;
3485 static LRESULT
3486 TOOLBAR_GetItemRect (const TOOLBAR_INFO *infoPtr, INT nIndex, LPRECT lpRect)
3488 TBUTTON_INFO *btnPtr;
3490 btnPtr = &infoPtr->buttons[nIndex];
3491 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3492 return FALSE;
3494 if (lpRect == NULL)
3495 return FALSE;
3496 if (btnPtr->fsState & TBSTATE_HIDDEN)
3497 return FALSE;
3499 lpRect->left = btnPtr->rect.left;
3500 lpRect->right = btnPtr->rect.right;
3501 lpRect->bottom = btnPtr->rect.bottom;
3502 lpRect->top = btnPtr->rect.top;
3504 return TRUE;
3508 static LRESULT
3509 TOOLBAR_GetMaxSize (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
3511 if (lpSize == NULL)
3512 return FALSE;
3514 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3515 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3517 TRACE("maximum size %d x %d\n",
3518 infoPtr->rcBound.right - infoPtr->rcBound.left,
3519 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3521 return TRUE;
3525 /* << TOOLBAR_GetObject >> */
3528 static inline LRESULT
3529 TOOLBAR_GetPadding (const TOOLBAR_INFO *infoPtr)
3531 return MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3535 static LRESULT
3536 TOOLBAR_GetRect (const TOOLBAR_INFO *infoPtr, INT Id, LPRECT lpRect)
3538 TBUTTON_INFO *btnPtr;
3539 INT nIndex;
3541 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3542 btnPtr = &infoPtr->buttons[nIndex];
3543 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3544 return FALSE;
3546 if (lpRect == NULL)
3547 return FALSE;
3549 lpRect->left = btnPtr->rect.left;
3550 lpRect->right = btnPtr->rect.right;
3551 lpRect->bottom = btnPtr->rect.bottom;
3552 lpRect->top = btnPtr->rect.top;
3554 return TRUE;
3558 static inline LRESULT
3559 TOOLBAR_GetRows (const TOOLBAR_INFO *infoPtr)
3561 return infoPtr->nRows;
3565 static LRESULT
3566 TOOLBAR_GetState (const TOOLBAR_INFO *infoPtr, INT Id)
3568 INT nIndex;
3570 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3571 if (nIndex == -1)
3572 return -1;
3574 return infoPtr->buttons[nIndex].fsState;
3578 static inline LRESULT
3579 TOOLBAR_GetStyle (const TOOLBAR_INFO *infoPtr)
3581 return infoPtr->dwStyle;
3585 static inline LRESULT
3586 TOOLBAR_GetTextRows (const TOOLBAR_INFO *infoPtr)
3588 return infoPtr->nMaxTextRows;
3592 static LRESULT
3593 TOOLBAR_GetToolTips (TOOLBAR_INFO *infoPtr)
3595 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3596 TOOLBAR_TooltipCreateControl(infoPtr);
3597 return (LRESULT)infoPtr->hwndToolTip;
3601 static LRESULT
3602 TOOLBAR_GetUnicodeFormat (const TOOLBAR_INFO *infoPtr)
3604 TRACE("%s hwnd=%p\n",
3605 infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
3607 return infoPtr->bUnicode;
3611 static inline LRESULT
3612 TOOLBAR_GetVersion (const TOOLBAR_INFO *infoPtr)
3614 return infoPtr->iVersion;
3618 static LRESULT
3619 TOOLBAR_HideButton (TOOLBAR_INFO *infoPtr, INT Id, BOOL fHide)
3621 TBUTTON_INFO *btnPtr;
3622 BYTE oldState;
3623 INT nIndex;
3625 TRACE("\n");
3627 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3628 if (nIndex == -1)
3629 return FALSE;
3631 btnPtr = &infoPtr->buttons[nIndex];
3632 oldState = btnPtr->fsState;
3634 if (fHide)
3635 btnPtr->fsState |= TBSTATE_HIDDEN;
3636 else
3637 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3639 if (oldState != btnPtr->fsState) {
3640 TOOLBAR_LayoutToolbar (infoPtr);
3641 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
3644 return TRUE;
3648 static inline LRESULT
3649 TOOLBAR_HitTest (const TOOLBAR_INFO *infoPtr, const POINT* lpPt)
3651 return TOOLBAR_InternalHitTest (infoPtr, lpPt);
3655 static LRESULT
3656 TOOLBAR_Indeterminate (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fIndeterminate)
3658 TBUTTON_INFO *btnPtr;
3659 INT nIndex;
3660 DWORD oldState;
3662 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3663 if (nIndex == -1)
3664 return FALSE;
3666 btnPtr = &infoPtr->buttons[nIndex];
3667 oldState = btnPtr->fsState;
3669 if (fIndeterminate)
3670 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3671 else
3672 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3674 if(oldState != btnPtr->fsState)
3675 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3677 return TRUE;
3681 static LRESULT
3682 TOOLBAR_InsertButtonT(TOOLBAR_INFO *infoPtr, INT nIndex, const TBBUTTON *lpTbb, BOOL fUnicode)
3684 if (lpTbb == NULL)
3685 return FALSE;
3687 if (nIndex == -1) {
3688 /* EPP: this seems to be an undocumented call (from my IE4)
3689 * I assume in that case that:
3690 * - index of insertion is at the end of existing buttons
3691 * I only see this happen with nIndex == -1, but it could have a special
3692 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3694 nIndex = infoPtr->nNumButtons;
3696 } else if (nIndex < 0)
3697 return FALSE;
3699 TRACE("inserting button index=%d\n", nIndex);
3700 if (nIndex > infoPtr->nNumButtons) {
3701 nIndex = infoPtr->nNumButtons;
3702 TRACE("adjust index=%d\n", nIndex);
3705 return TOOLBAR_InternalInsertButtonsT(infoPtr, nIndex, 1, lpTbb, fUnicode);
3708 /* << TOOLBAR_InsertMarkHitTest >> */
3711 static LRESULT
3712 TOOLBAR_IsButtonChecked (const TOOLBAR_INFO *infoPtr, INT Id)
3714 INT nIndex;
3716 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3717 if (nIndex == -1)
3718 return -1;
3720 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3724 static LRESULT
3725 TOOLBAR_IsButtonEnabled (const TOOLBAR_INFO *infoPtr, INT Id)
3727 INT nIndex;
3729 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3730 if (nIndex == -1)
3731 return -1;
3733 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3737 static LRESULT
3738 TOOLBAR_IsButtonHidden (const TOOLBAR_INFO *infoPtr, INT Id)
3740 INT nIndex;
3742 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3743 if (nIndex == -1)
3744 return -1;
3746 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3750 static LRESULT
3751 TOOLBAR_IsButtonHighlighted (const TOOLBAR_INFO *infoPtr, INT Id)
3753 INT nIndex;
3755 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3756 if (nIndex == -1)
3757 return -1;
3759 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3763 static LRESULT
3764 TOOLBAR_IsButtonIndeterminate (const TOOLBAR_INFO *infoPtr, INT Id)
3766 INT nIndex;
3768 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3769 if (nIndex == -1)
3770 return -1;
3772 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3776 static LRESULT
3777 TOOLBAR_IsButtonPressed (const TOOLBAR_INFO *infoPtr, INT Id)
3779 INT nIndex;
3781 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3782 if (nIndex == -1)
3783 return -1;
3785 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3789 static LRESULT
3790 TOOLBAR_LoadImages (TOOLBAR_INFO *infoPtr, WPARAM wParam, HINSTANCE hInstance)
3792 TBADDBITMAP tbab;
3793 tbab.hInst = hInstance;
3794 tbab.nID = wParam;
3796 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", infoPtr->hwndSelf, tbab.hInst, tbab.nID);
3798 return TOOLBAR_AddBitmap(infoPtr, 0, &tbab);
3802 static LRESULT
3803 TOOLBAR_MapAccelerator (const TOOLBAR_INFO *infoPtr, WCHAR wAccel, UINT *pIDButton)
3805 WCHAR wszAccel[] = {'&',wAccel,0};
3806 int i;
3808 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3809 infoPtr->hwndSelf, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3811 for (i = 0; i < infoPtr->nNumButtons; i++)
3813 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3814 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3815 !(btnPtr->fsState & TBSTATE_HIDDEN))
3817 int iLen = strlenW(wszAccel);
3818 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3820 if (!lpszStr)
3821 continue;
3823 while (*lpszStr)
3825 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3827 lpszStr += 2;
3828 continue;
3830 if (!strncmpiW(lpszStr, wszAccel, iLen))
3832 *pIDButton = btnPtr->idCommand;
3833 return TRUE;
3835 lpszStr++;
3839 return FALSE;
3843 static LRESULT
3844 TOOLBAR_MarkButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fMark)
3846 INT nIndex;
3847 DWORD oldState;
3848 TBUTTON_INFO *btnPtr;
3850 TRACE("hwnd = %p, Id = %d, fMark = 0%d\n", infoPtr->hwndSelf, Id, fMark);
3852 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3853 if (nIndex == -1)
3854 return FALSE;
3856 btnPtr = &infoPtr->buttons[nIndex];
3857 oldState = btnPtr->fsState;
3859 if (fMark)
3860 btnPtr->fsState |= TBSTATE_MARKED;
3861 else
3862 btnPtr->fsState &= ~TBSTATE_MARKED;
3864 if(oldState != btnPtr->fsState)
3865 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3867 return TRUE;
3871 /* fixes up an index of a button affected by a move */
3872 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
3874 if (bMoveUp)
3876 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
3877 (*pIndex)--;
3878 else if (*pIndex == nIndex)
3879 *pIndex = nMoveIndex;
3881 else
3883 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
3884 (*pIndex)++;
3885 else if (*pIndex == nIndex)
3886 *pIndex = nMoveIndex;
3891 static LRESULT
3892 TOOLBAR_MoveButton (TOOLBAR_INFO *infoPtr, INT Id, INT nMoveIndex)
3894 INT nIndex;
3895 INT nCount;
3896 TBUTTON_INFO button;
3898 TRACE("hwnd=%p, Id=%d, nMoveIndex=%d\n", infoPtr->hwndSelf, Id, nMoveIndex);
3900 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, TRUE);
3901 if ((nIndex == -1) || (nMoveIndex < 0))
3902 return FALSE;
3904 if (nMoveIndex > infoPtr->nNumButtons - 1)
3905 nMoveIndex = infoPtr->nNumButtons - 1;
3907 button = infoPtr->buttons[nIndex];
3909 /* move button right */
3910 if (nIndex < nMoveIndex)
3912 nCount = nMoveIndex - nIndex;
3913 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
3914 infoPtr->buttons[nMoveIndex] = button;
3916 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
3917 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
3918 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
3919 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
3921 else if (nIndex > nMoveIndex) /* move button left */
3923 nCount = nIndex - nMoveIndex;
3924 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
3925 infoPtr->buttons[nMoveIndex] = button;
3927 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
3928 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
3929 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
3930 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
3933 TOOLBAR_LayoutToolbar(infoPtr);
3934 TOOLBAR_AutoSize(infoPtr);
3935 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3937 return TRUE;
3941 static LRESULT
3942 TOOLBAR_PressButton (const TOOLBAR_INFO *infoPtr, INT Id, BOOL fPress)
3944 TBUTTON_INFO *btnPtr;
3945 INT nIndex;
3946 DWORD oldState;
3948 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
3949 if (nIndex == -1)
3950 return FALSE;
3952 btnPtr = &infoPtr->buttons[nIndex];
3953 oldState = btnPtr->fsState;
3955 if (fPress)
3956 btnPtr->fsState |= TBSTATE_PRESSED;
3957 else
3958 btnPtr->fsState &= ~TBSTATE_PRESSED;
3960 if(oldState != btnPtr->fsState)
3961 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
3963 return TRUE;
3966 /* FIXME: there might still be some confusion her between number of buttons
3967 * and number of bitmaps */
3968 static LRESULT
3969 TOOLBAR_ReplaceBitmap (TOOLBAR_INFO *infoPtr, const TBREPLACEBITMAP *lpReplace)
3971 HBITMAP hBitmap;
3972 int i = 0, nOldButtons = 0, pos = 0;
3973 int nOldBitmaps, nNewBitmaps = 0;
3974 HIMAGELIST himlDef = 0;
3976 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
3977 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
3978 lpReplace->nButtons);
3980 if (lpReplace->hInstOld == HINST_COMMCTRL)
3982 FIXME("changing standard bitmaps not implemented\n");
3983 return FALSE;
3985 else if (lpReplace->hInstOld != 0 && lpReplace->hInstOld != lpReplace->hInstNew)
3986 FIXME("resources not in the current module not implemented\n");
3988 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
3989 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
3990 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
3991 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
3992 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
3994 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
3995 nOldButtons = tbi->nButtons;
3996 tbi->nButtons = lpReplace->nButtons;
3997 tbi->hInst = lpReplace->hInstNew;
3998 tbi->nID = lpReplace->nIDNew;
3999 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4000 break;
4002 pos += tbi->nButtons;
4005 if (nOldButtons == 0)
4007 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4008 return FALSE;
4011 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4012 * bitmap
4014 if (lpReplace->hInstNew)
4015 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4016 else
4017 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4019 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4020 nOldBitmaps = ImageList_GetImageCount(himlDef);
4022 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4024 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4025 ImageList_Remove(himlDef, i);
4027 if (hBitmap)
4029 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4030 nNewBitmaps = ImageList_GetImageCount(himlDef);
4031 DeleteObject(hBitmap);
4034 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4036 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4037 pos, nOldBitmaps, nNewBitmaps);
4039 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4040 return TRUE;
4044 /* helper for TOOLBAR_SaveRestoreW */
4045 static BOOL
4046 TOOLBAR_Save(const TBSAVEPARAMSW *lpSave)
4048 FIXME("save to %s %s\n", debugstr_w(lpSave->pszSubKey),
4049 debugstr_w(lpSave->pszValueName));
4051 return FALSE;
4055 /* helper for TOOLBAR_Restore */
4056 static void
4057 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4059 INT i;
4061 for (i = 0; i < infoPtr->nNumButtons; i++)
4063 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4066 Free(infoPtr->buttons);
4067 infoPtr->buttons = NULL;
4068 infoPtr->nNumButtons = 0;
4072 /* helper for TOOLBAR_SaveRestoreW */
4073 static BOOL
4074 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4076 LONG res;
4077 HKEY hkey = NULL;
4078 BOOL ret = FALSE;
4079 DWORD dwType;
4080 DWORD dwSize = 0;
4081 NMTBRESTORE nmtbr;
4083 /* restore toolbar information */
4084 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4085 debugstr_w(lpSave->pszValueName));
4087 memset(&nmtbr, 0, sizeof(nmtbr));
4089 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4090 KEY_QUERY_VALUE, &hkey);
4091 if (!res)
4092 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4093 NULL, &dwSize);
4094 if (!res && dwType != REG_BINARY)
4095 res = ERROR_FILE_NOT_FOUND;
4096 if (!res)
4098 nmtbr.pData = Alloc(dwSize);
4099 nmtbr.cbData = dwSize;
4100 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4102 if (!res)
4103 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4104 (LPBYTE)nmtbr.pData, &dwSize);
4105 if (!res)
4107 nmtbr.pCurrent = nmtbr.pData;
4108 nmtbr.iItem = -1;
4109 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4110 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4112 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4114 INT i;
4116 /* remove all existing buttons as this function is designed to
4117 * restore the toolbar to a previously saved state */
4118 TOOLBAR_DeleteAllButtons(infoPtr);
4120 for (i = 0; i < nmtbr.cButtons; i++)
4122 nmtbr.iItem = i;
4123 nmtbr.tbButton.iBitmap = -1;
4124 nmtbr.tbButton.fsState = 0;
4125 nmtbr.tbButton.fsStyle = 0;
4126 nmtbr.tbButton.idCommand = 0;
4127 if (*nmtbr.pCurrent == (DWORD)-1)
4129 /* separator */
4130 nmtbr.tbButton.fsStyle = TBSTYLE_SEP;
4131 /* when inserting separators, iBitmap controls it's size.
4132 0 sets default size (width) */
4133 nmtbr.tbButton.iBitmap = 0;
4135 else if (*nmtbr.pCurrent == (DWORD)-2)
4136 /* hidden button */
4137 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4138 else
4139 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4141 nmtbr.pCurrent++;
4143 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4145 /* can't contain real string as we don't know whether
4146 * the client put an ANSI or Unicode string in there */
4147 if (!IS_INTRESOURCE(nmtbr.tbButton.iString))
4148 nmtbr.tbButton.iString = 0;
4150 TOOLBAR_InsertButtonT(infoPtr, -1, &nmtbr.tbButton, TRUE);
4153 /* do legacy notifications */
4154 if (infoPtr->iVersion < 5)
4156 /* FIXME: send TBN_BEGINADJUST */
4157 FIXME("send TBN_GETBUTTONINFO for each button\n");
4158 /* FIXME: send TBN_ENDADJUST */
4161 /* remove all uninitialised buttons
4162 * note: loop backwards to avoid having to fixup i on a
4163 * delete */
4164 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4165 if (infoPtr->buttons[i].iBitmap == -1)
4166 TOOLBAR_DeleteButton(infoPtr, i);
4168 /* only indicate success if at least one button survived */
4169 if (infoPtr->nNumButtons > 0) ret = TRUE;
4172 Free (nmtbr.pData);
4173 RegCloseKey(hkey);
4175 return ret;
4179 static LRESULT
4180 TOOLBAR_SaveRestoreW (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4182 if (lpSave == NULL) return 0;
4184 if (wParam)
4185 return TOOLBAR_Save(lpSave);
4186 else
4187 return TOOLBAR_Restore(infoPtr, lpSave);
4191 static LRESULT
4192 TOOLBAR_SaveRestoreA (TOOLBAR_INFO *infoPtr, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4194 LPWSTR pszValueName = 0, pszSubKey = 0;
4195 TBSAVEPARAMSW SaveW;
4196 LRESULT result = 0;
4197 int len;
4199 if (lpSave == NULL) return 0;
4201 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4202 pszSubKey = Alloc(len * sizeof(WCHAR));
4203 if (pszSubKey) goto exit;
4204 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4206 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4207 pszValueName = Alloc(len * sizeof(WCHAR));
4208 if (!pszValueName) goto exit;
4209 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4211 SaveW.pszValueName = pszValueName;
4212 SaveW.pszSubKey = pszSubKey;
4213 SaveW.hkr = lpSave->hkr;
4214 result = TOOLBAR_SaveRestoreW(infoPtr, wParam, &SaveW);
4216 exit:
4217 Free (pszValueName);
4218 Free (pszSubKey);
4220 return result;
4224 static LRESULT
4225 TOOLBAR_SetAnchorHighlight (TOOLBAR_INFO *infoPtr, BOOL bAnchor)
4227 BOOL bOldAnchor = infoPtr->bAnchor;
4229 TRACE("hwnd=%p, bAnchor = %s\n", infoPtr->hwndSelf, bAnchor ? "TRUE" : "FALSE");
4231 infoPtr->bAnchor = bAnchor;
4233 /* Native does not remove the hot effect from an already hot button */
4235 return (LRESULT)bOldAnchor;
4239 static LRESULT
4240 TOOLBAR_SetBitmapSize (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4242 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4243 short width = (short)LOWORD(lParam);
4244 short height = (short)HIWORD(lParam);
4246 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", infoPtr->hwndSelf, wParam, lParam);
4248 if (wParam != 0)
4249 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4251 /* 0 width or height is changed to 1 */
4252 if (width == 0)
4253 width = 1;
4254 if (height == 0)
4255 height = 1;
4257 if (infoPtr->nNumButtons > 0)
4258 TRACE("%d buttons, undoc change to bitmap size : %d-%d -> %d-%d\n",
4259 infoPtr->nNumButtons,
4260 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight, width, height);
4262 if (width < -1 || height < -1)
4264 /* Windows destroys the imagelist and seems to actually use negative
4265 * values to compute button sizes */
4266 FIXME("Negative bitmap sizes not supported (%d, %d)\n", width, height);
4267 return FALSE;
4270 /* width or height of -1 means no change */
4271 if (width != -1)
4272 infoPtr->nBitmapWidth = width;
4273 if (height != -1)
4274 infoPtr->nBitmapHeight = height;
4276 if ((himlDef == infoPtr->himlInt) &&
4277 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4279 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4280 infoPtr->nBitmapHeight);
4283 TOOLBAR_CalcToolbar(infoPtr);
4284 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4285 return TRUE;
4289 static LRESULT
4290 TOOLBAR_SetButtonInfo (TOOLBAR_INFO *infoPtr, INT Id,
4291 const TBBUTTONINFOW *lptbbi, BOOL isW)
4293 TBUTTON_INFO *btnPtr;
4294 INT nIndex;
4295 RECT oldBtnRect;
4297 if (lptbbi == NULL)
4298 return FALSE;
4299 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4300 return FALSE;
4302 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, lptbbi->dwMask & TBIF_BYINDEX);
4303 if (nIndex == -1)
4304 return FALSE;
4306 btnPtr = &infoPtr->buttons[nIndex];
4307 if (lptbbi->dwMask & TBIF_COMMAND)
4308 btnPtr->idCommand = lptbbi->idCommand;
4309 if (lptbbi->dwMask & TBIF_IMAGE)
4310 btnPtr->iBitmap = lptbbi->iImage;
4311 if (lptbbi->dwMask & TBIF_LPARAM)
4312 btnPtr->dwData = lptbbi->lParam;
4313 if (lptbbi->dwMask & TBIF_SIZE)
4314 btnPtr->cx = lptbbi->cx;
4315 if (lptbbi->dwMask & TBIF_STATE)
4316 btnPtr->fsState = lptbbi->fsState;
4317 if (lptbbi->dwMask & TBIF_STYLE)
4318 btnPtr->fsStyle = lptbbi->fsStyle;
4320 if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4321 /* iString is index, zero it to make Str_SetPtr succeed */
4322 if (!TOOLBAR_ButtonHasString(btnPtr)) btnPtr->iString = 0;
4324 if (isW)
4325 Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
4326 else
4327 Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, (LPSTR)lptbbi->pszText);
4330 /* save the button rect to see if we need to redraw the whole toolbar */
4331 oldBtnRect = btnPtr->rect;
4332 TOOLBAR_LayoutToolbar(infoPtr);
4334 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4335 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4336 else
4337 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4339 return TRUE;
4343 static LRESULT
4344 TOOLBAR_SetButtonSize (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4346 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4348 if ((cx < 0) || (cy < 0))
4350 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4351 return FALSE;
4354 TRACE("%p, cx = %d, cy = %d\n", infoPtr->hwndSelf, cx, cy);
4356 /* The documentation claims you can only change the button size before
4357 * any button has been added. But this is wrong.
4358 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4359 * it to the toolbar, and it checks that the return value is nonzero - mjm
4360 * Further testing shows that we must actually perform the change too.
4363 * The documentation also does not mention that if 0 is supplied for
4364 * either size, the system changes it to the default of 24 wide and
4365 * 22 high. Demonstarted in ControlSpy Toolbar. GLA 3/02
4367 if (cx == 0) cx = 24;
4368 if (cy == 0) cy = 22;
4370 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4371 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4373 infoPtr->nButtonWidth = cx;
4374 infoPtr->nButtonHeight = cy;
4376 infoPtr->iTopMargin = default_top_margin(infoPtr);
4377 TOOLBAR_LayoutToolbar(infoPtr);
4378 return TRUE;
4382 static LRESULT
4383 TOOLBAR_SetButtonWidth (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4385 /* if setting to current values, ignore */
4386 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4387 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4388 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4389 infoPtr->cxMin, infoPtr->cxMax);
4390 return TRUE;
4393 /* save new values */
4394 infoPtr->cxMin = (short)LOWORD(lParam);
4395 infoPtr->cxMax = (short)HIWORD(lParam);
4397 /* otherwise we need to recalc the toolbar and in some cases
4398 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4399 which doesn't actually draw - GA). */
4400 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4401 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4403 TOOLBAR_CalcToolbar (infoPtr);
4405 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
4407 return TRUE;
4411 static LRESULT
4412 TOOLBAR_SetCmdId (TOOLBAR_INFO *infoPtr, INT nIndex, INT nId)
4414 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4415 return FALSE;
4417 infoPtr->buttons[nIndex].idCommand = nId;
4419 if (infoPtr->hwndToolTip) {
4421 FIXME("change tool tip!\n");
4425 return TRUE;
4429 static LRESULT
4430 TOOLBAR_SetDisabledImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4432 HIMAGELIST himlTemp;
4433 INT id = 0;
4435 if (infoPtr->iVersion >= 5)
4436 id = wParam;
4438 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4439 &infoPtr->cimlDis, himl, id);
4441 /* FIXME: redraw ? */
4443 return (LRESULT)himlTemp;
4447 static LRESULT
4448 TOOLBAR_SetDrawTextFlags (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4450 DWORD dwTemp;
4452 TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", infoPtr->hwndSelf, (DWORD)wParam, (DWORD)lParam);
4454 dwTemp = infoPtr->dwDTFlags;
4455 infoPtr->dwDTFlags =
4456 (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4458 return (LRESULT)dwTemp;
4461 /* This function differs a bit from what MSDN says it does:
4462 * 1. lParam contains extended style flags to OR with current style
4463 * (MSDN isn't clear on the OR bit)
4464 * 2. wParam appears to contain extended style flags to be reset
4465 * (MSDN says that this parameter is reserved)
4467 static LRESULT
4468 TOOLBAR_SetExtendedStyle (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4470 DWORD dwOldStyle;
4472 dwOldStyle = infoPtr->dwExStyle;
4473 infoPtr->dwExStyle = (DWORD)lParam;
4475 TRACE("new style 0x%08x\n", infoPtr->dwExStyle);
4477 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4478 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4479 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4481 if ((dwOldStyle ^ infoPtr->dwExStyle) & TBSTYLE_EX_MIXEDBUTTONS)
4482 TOOLBAR_CalcToolbar(infoPtr);
4483 else
4484 TOOLBAR_LayoutToolbar(infoPtr);
4486 TOOLBAR_AutoSize(infoPtr);
4487 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4489 return (LRESULT)dwOldStyle;
4493 static LRESULT
4494 TOOLBAR_SetHotImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4496 HIMAGELIST himlTemp;
4497 INT id = 0;
4499 if (infoPtr->iVersion >= 5)
4500 id = wParam;
4502 TRACE("hwnd = %p, himl = %p, id = %d\n", infoPtr->hwndSelf, himl, id);
4504 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4505 &infoPtr->cimlHot, himl, id);
4507 /* FIXME: redraw ? */
4509 return (LRESULT)himlTemp;
4513 /* Makes previous hot button no longer hot, makes the specified
4514 * button hot and sends appropriate notifications. dwReason is one or
4515 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4516 * NOTE 1: this function does not validate nHit
4517 * NOTE 2: the name of this function is completely made up and
4518 * not based on any documentation from Microsoft. */
4519 static void
4520 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4522 if (infoPtr->nHotItem != nHit)
4524 NMTBHOTITEM nmhotitem;
4525 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4527 nmhotitem.dwFlags = dwReason;
4528 if(infoPtr->nHotItem >= 0)
4530 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4531 nmhotitem.idOld = oldBtnPtr->idCommand;
4533 else
4535 nmhotitem.dwFlags |= HICF_ENTERING;
4536 nmhotitem.idOld = 0;
4539 if (nHit >= 0)
4541 btnPtr = &infoPtr->buttons[nHit];
4542 nmhotitem.idNew = btnPtr->idCommand;
4544 else
4546 nmhotitem.dwFlags |= HICF_LEAVING;
4547 nmhotitem.idNew = 0;
4550 /* now change the hot and invalidate the old and new buttons - if the
4551 * parent agrees */
4552 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4554 if (oldBtnPtr) {
4555 oldBtnPtr->bHot = FALSE;
4556 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4558 /* setting disabled buttons as hot fails even if the notify contains the button id */
4559 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4560 btnPtr->bHot = TRUE;
4561 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4562 infoPtr->nHotItem = nHit;
4564 else
4565 infoPtr->nHotItem = -1;
4570 static LRESULT
4571 TOOLBAR_SetHotItem (TOOLBAR_INFO *infoPtr, INT nHotItem)
4573 INT nOldHotItem = infoPtr->nHotItem;
4575 TRACE("hwnd = %p, nHotItem = %d\n", infoPtr->hwndSelf, nHotItem);
4577 if (nHotItem >= infoPtr->nNumButtons)
4578 return infoPtr->nHotItem;
4580 if (nHotItem < 0)
4581 nHotItem = -1;
4583 /* NOTE: an application can still remove the hot item even if anchor
4584 * highlighting is enabled */
4586 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, HICF_OTHER);
4588 if (nOldHotItem < 0)
4589 return -1;
4591 return (LRESULT)nOldHotItem;
4595 static LRESULT
4596 TOOLBAR_SetImageList (TOOLBAR_INFO *infoPtr, WPARAM wParam, HIMAGELIST himl)
4598 HIMAGELIST himlTemp;
4599 INT oldButtonWidth = infoPtr->nButtonWidth;
4600 INT oldBitmapWidth = infoPtr->nBitmapWidth;
4601 INT oldBitmapHeight = infoPtr->nBitmapHeight;
4602 INT i, id = 0;
4604 if (infoPtr->iVersion >= 5)
4605 id = wParam;
4607 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4608 &infoPtr->cimlDef, himl, id);
4610 infoPtr->nNumBitmaps = 0;
4611 for (i = 0; i < infoPtr->cimlDef; i++)
4612 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4614 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4615 &infoPtr->nBitmapHeight))
4617 infoPtr->nBitmapWidth = 1;
4618 infoPtr->nBitmapHeight = 1;
4620 if ((oldBitmapWidth != infoPtr->nBitmapWidth) || (oldBitmapHeight != infoPtr->nBitmapHeight))
4622 TOOLBAR_CalcToolbar(infoPtr);
4623 if (infoPtr->nButtonWidth < oldButtonWidth)
4624 TOOLBAR_SetButtonSize(infoPtr, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4627 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4628 infoPtr->hwndSelf, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4629 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4631 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4633 return (LRESULT)himlTemp;
4637 static LRESULT
4638 TOOLBAR_SetIndent (TOOLBAR_INFO *infoPtr, INT nIndent)
4640 infoPtr->nIndent = nIndent;
4642 TRACE("\n");
4644 /* process only on indent changing */
4645 if(infoPtr->nIndent != nIndent)
4647 infoPtr->nIndent = nIndent;
4648 TOOLBAR_CalcToolbar (infoPtr);
4649 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4652 return TRUE;
4656 static LRESULT
4657 TOOLBAR_SetInsertMark (TOOLBAR_INFO *infoPtr, const TBINSERTMARK *lptbim)
4659 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", infoPtr->hwndSelf, lptbim->iButton, lptbim->dwFlags);
4661 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4663 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4664 return 0;
4667 if ((lptbim->iButton == -1) ||
4668 ((lptbim->iButton < infoPtr->nNumButtons) &&
4669 (lptbim->iButton >= 0)))
4671 infoPtr->tbim = *lptbim;
4672 /* FIXME: don't need to update entire toolbar */
4673 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4675 else
4676 ERR("Invalid button index %d\n", lptbim->iButton);
4678 return 0;
4682 static LRESULT
4683 TOOLBAR_SetInsertMarkColor (TOOLBAR_INFO *infoPtr, COLORREF clr)
4685 infoPtr->clrInsertMark = clr;
4687 /* FIXME: don't need to update entire toolbar */
4688 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4690 return 0;
4694 static LRESULT
4695 TOOLBAR_SetMaxTextRows (TOOLBAR_INFO *infoPtr, INT nMaxRows)
4697 infoPtr->nMaxTextRows = nMaxRows;
4699 TOOLBAR_CalcToolbar(infoPtr);
4700 return TRUE;
4704 /* MSDN gives slightly wrong info on padding.
4705 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4706 * 2. It is not used to create a blank area between the edge of the button
4707 * and the text or image if TBSTYLE_LIST is set. It is used to control
4708 * the gap between the image and text.
4709 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4710 * to control the bottom and right borders [with the border being
4711 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4712 * is shared evenly on both sides of the button.
4713 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4715 static LRESULT
4716 TOOLBAR_SetPadding (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4718 DWORD oldPad;
4720 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4721 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4722 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4723 TRACE("cx=%d, cy=%d\n",
4724 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4725 return (LRESULT) oldPad;
4729 static LRESULT
4730 TOOLBAR_SetParent (TOOLBAR_INFO *infoPtr, HWND hParent)
4732 HWND hwndOldNotify;
4734 TRACE("\n");
4736 hwndOldNotify = infoPtr->hwndNotify;
4737 infoPtr->hwndNotify = hParent;
4739 return (LRESULT)hwndOldNotify;
4743 static LRESULT
4744 TOOLBAR_SetRows (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPRECT lprc)
4746 int rows = LOWORD(wParam);
4747 BOOL bLarger = HIWORD(wParam);
4749 TRACE("\n");
4751 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4753 if(infoPtr->nRows != rows)
4755 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4756 int curColumn = 0; /* Current column */
4757 int curRow = 0; /* Current row */
4758 int hidden = 0; /* Number of hidden buttons */
4759 int seps = 0; /* Number of separators */
4760 int idealWrap = 0; /* Ideal wrap point */
4761 int i;
4762 BOOL wrap;
4765 Calculate new size and wrap points - Under windows, setrows will
4766 change the dimensions if needed to show the number of requested
4767 rows (if CCS_NORESIZE is set), or will take up the whole window
4768 (if no CCS_NORESIZE).
4770 Basic algorithm - If N buttons, and y rows requested, each row
4771 contains N/y buttons.
4773 FIXME: Handling of separators not obvious from testing results
4774 FIXME: Take width of window into account?
4777 /* Loop through the buttons one by one counting key items */
4778 for (i = 0; i < infoPtr->nNumButtons; i++ )
4780 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4781 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4782 hidden++;
4783 else if (btnPtr[i].fsStyle & BTNS_SEP)
4784 seps++;
4787 /* FIXME: Separators make this quite complex */
4788 if (seps) FIXME("Separators unhandled\n");
4790 /* Round up so more per line, i.e., less rows */
4791 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / (rows ? rows : 1);
4793 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4794 achieve the requested number of rows. */
4795 if (bLarger && idealWrap > 1)
4797 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4798 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
4800 if (resRows < rows && moreRows > rows)
4802 idealWrap--;
4803 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
4807 curColumn = curRow = 0;
4808 wrap = FALSE;
4809 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
4810 infoPtr->nNumButtons, hidden, rows);
4812 for (i = 0; i < infoPtr->nNumButtons; i++ )
4814 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4815 continue;
4817 /* Step on, wrap if necessary or flag next to wrap */
4818 if (!wrap) {
4819 curColumn++;
4820 } else {
4821 wrap = FALSE;
4822 curColumn = 1;
4823 curRow++;
4826 if (curColumn > (idealWrap-1)) {
4827 wrap = TRUE;
4828 btnPtr[i].fsState |= TBSTATE_WRAP;
4832 TRACE("Result - %d rows\n", curRow + 1);
4834 /* recalculate toolbar */
4835 TOOLBAR_CalcToolbar (infoPtr);
4837 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
4838 if NORESIZE is NOT set, then the toolbar will always be resized to
4839 take up the whole window. With it set, sizing needs to be manual. */
4840 if (infoPtr->dwStyle & CCS_NORESIZE) {
4841 SetWindowPos(infoPtr->hwndSelf, NULL, 0, 0,
4842 infoPtr->rcBound.right - infoPtr->rcBound.left,
4843 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
4844 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
4847 /* repaint toolbar */
4848 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4851 /* return bounding rectangle */
4852 if (lprc) {
4853 lprc->left = infoPtr->rcBound.left;
4854 lprc->right = infoPtr->rcBound.right;
4855 lprc->top = infoPtr->rcBound.top;
4856 lprc->bottom = infoPtr->rcBound.bottom;
4859 return 0;
4863 static LRESULT
4864 TOOLBAR_SetState (TOOLBAR_INFO *infoPtr, INT Id, LPARAM lParam)
4866 TBUTTON_INFO *btnPtr;
4867 INT nIndex;
4869 nIndex = TOOLBAR_GetButtonIndex (infoPtr, Id, FALSE);
4870 if (nIndex == -1)
4871 return FALSE;
4873 btnPtr = &infoPtr->buttons[nIndex];
4875 /* if hidden state has changed the invalidate entire window and recalc */
4876 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
4877 btnPtr->fsState = LOWORD(lParam);
4878 TOOLBAR_CalcToolbar (infoPtr);
4879 InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
4880 return TRUE;
4883 /* process state changing if current state doesn't match new state */
4884 if(btnPtr->fsState != LOWORD(lParam))
4886 btnPtr->fsState = LOWORD(lParam);
4887 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4890 return TRUE;
4894 static LRESULT
4895 TOOLBAR_SetStyle (TOOLBAR_INFO *infoPtr, LPARAM lParam)
4897 SetWindowLongW(infoPtr->hwndSelf, GWL_STYLE, lParam);
4899 return TRUE;
4903 static inline LRESULT
4904 TOOLBAR_SetToolTips (TOOLBAR_INFO *infoPtr, HWND hwndTooltip)
4906 TRACE("hwnd=%p, hwndTooltip=%p\n", infoPtr->hwndSelf, hwndTooltip);
4908 infoPtr->hwndToolTip = hwndTooltip;
4909 return 0;
4913 static LRESULT
4914 TOOLBAR_SetUnicodeFormat (TOOLBAR_INFO *infoPtr, WPARAM wParam)
4916 BOOL bTemp;
4918 TRACE("%s hwnd=%p\n",
4919 ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf);
4921 bTemp = infoPtr->bUnicode;
4922 infoPtr->bUnicode = (BOOL)wParam;
4924 return bTemp;
4928 static LRESULT
4929 TOOLBAR_GetColorScheme (const TOOLBAR_INFO *infoPtr, LPCOLORSCHEME lParam)
4931 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
4932 comctl32_color.clrBtnHighlight :
4933 infoPtr->clrBtnHighlight;
4934 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
4935 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
4936 return 1;
4940 static LRESULT
4941 TOOLBAR_SetColorScheme (TOOLBAR_INFO *infoPtr, const COLORSCHEME *lParam)
4943 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
4944 lParam->clrBtnHighlight, lParam->clrBtnShadow,
4945 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
4947 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
4948 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
4949 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
4950 return 0;
4954 static LRESULT
4955 TOOLBAR_SetVersion (TOOLBAR_INFO *infoPtr, INT iVersion)
4957 INT iOldVersion = infoPtr->iVersion;
4959 infoPtr->iVersion = iVersion;
4961 if (infoPtr->iVersion >= 5)
4962 TOOLBAR_SetUnicodeFormat(infoPtr, TRUE);
4964 return iOldVersion;
4968 static LRESULT
4969 TOOLBAR_GetStringA (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPSTR str)
4971 WORD iString = HIWORD(wParam);
4972 WORD buffersize = LOWORD(wParam);
4973 LRESULT ret = -1;
4975 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, buffersize, str);
4977 if (iString < infoPtr->nNumStrings)
4979 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
4980 ret--;
4982 TRACE("returning %s\n", debugstr_a(str));
4984 else
4985 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
4987 return ret;
4991 static LRESULT
4992 TOOLBAR_GetStringW (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPWSTR str)
4994 WORD iString = HIWORD(wParam);
4995 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
4996 LRESULT ret = -1;
4998 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", infoPtr->hwndSelf, iString, LOWORD(wParam), str);
5000 if (iString < infoPtr->nNumStrings)
5002 len = min(len, strlenW(infoPtr->strings[iString]));
5003 ret = (len+1)*sizeof(WCHAR);
5004 if (str)
5006 memcpy(str, infoPtr->strings[iString], ret);
5007 str[len] = '\0';
5009 ret = len;
5011 TRACE("returning %s\n", debugstr_w(str));
5013 else
5014 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5016 return ret;
5019 /* UNDOCUMENTED MESSAGE: This appears to set some kind of size. Perhaps it
5020 * is the maximum size of the toolbar? */
5021 static LRESULT TOOLBAR_Unkwn45D(HWND hwnd, WPARAM wParam, LPARAM lParam)
5023 SIZE * pSize = (SIZE*)lParam;
5024 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5025 return 0;
5029 /* This is an extended version of the TB_SETHOTITEM message. It allows the
5030 * caller to specify a reason why the hot item changed (rather than just the
5031 * HICF_OTHER that TB_SETHOTITEM sends). */
5032 static LRESULT
5033 TOOLBAR_SetHotItem2 (TOOLBAR_INFO *infoPtr, INT nHotItem, LPARAM lParam)
5035 INT nOldHotItem = infoPtr->nHotItem;
5037 TRACE("old item=%d, new item=%d, flags=%08x\n",
5038 nOldHotItem, nHotItem, (DWORD)lParam);
5040 if (nHotItem < 0 || nHotItem > infoPtr->nNumButtons)
5041 nHotItem = -1;
5043 /* NOTE: an application can still remove the hot item even if anchor
5044 * highlighting is enabled */
5046 TOOLBAR_SetHotItemEx(infoPtr, nHotItem, lParam);
5048 GetFocus();
5050 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5053 /* Sets the toolbar global iListGap parameter which controls the amount of
5054 * spacing between the image and the text of buttons for TBSTYLE_LIST
5055 * toolbars. */
5056 static LRESULT TOOLBAR_SetListGap(TOOLBAR_INFO *infoPtr, INT iListGap)
5058 TRACE("hwnd=%p iListGap=%d\n", infoPtr->hwndSelf, iListGap);
5060 infoPtr->iListGap = iListGap;
5062 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
5064 return 0;
5067 /* Returns the number of maximum number of image lists associated with the
5068 * various states. */
5069 static LRESULT TOOLBAR_GetImageListCount(const TOOLBAR_INFO *infoPtr)
5071 TRACE("hwnd=%p\n", infoPtr->hwndSelf);
5073 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5076 static LRESULT
5077 TOOLBAR_GetIdealSize (const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5079 LPSIZE lpsize = (LPSIZE)lParam;
5081 if (lpsize == NULL)
5082 return FALSE;
5085 * Testing shows the following:
5086 * wParam = 0 adjust cx value
5087 * = 1 set cy value to max size.
5088 * lParam pointer to SIZE structure
5091 TRACE("wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5092 wParam, lParam, lpsize->cx, lpsize->cy);
5094 switch(wParam) {
5095 case 0:
5096 if (lpsize->cx == -1) {
5097 /* **** this is wrong, native measures each button and sets it */
5098 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5100 else if(HIWORD(lpsize->cx)) {
5101 RECT rc;
5102 HWND hwndParent = GetParent(infoPtr->hwndSelf);
5104 GetWindowRect(infoPtr->hwndSelf, &rc);
5105 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5106 TRACE("mapped to (%s)\n", wine_dbgstr_rect(&rc));
5107 lpsize->cx = max(rc.right-rc.left,
5108 infoPtr->rcBound.right - infoPtr->rcBound.left);
5110 else {
5111 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5113 break;
5114 case 1:
5115 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5116 break;
5117 default:
5118 FIXME("Unknown wParam %ld\n", wParam);
5119 return 0;
5121 TRACE("set to -> 0x%08x 0x%08x\n",
5122 lpsize->cx, lpsize->cy);
5123 return 1;
5126 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5128 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5130 InvalidateRect(hwnd, NULL, TRUE);
5131 return 1;
5135 static LRESULT
5136 TOOLBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
5138 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
5139 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
5140 LOGFONTW logFont;
5142 TRACE("hwnd = %p\n", hwnd);
5144 infoPtr->dwStyle = dwStyle;
5145 GetClientRect(hwnd, &infoPtr->client_rect);
5146 infoPtr->bUnicode = infoPtr->hwndNotify &&
5147 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_REQUERY));
5148 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5150 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5151 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5153 OpenThemeData (hwnd, themeClass);
5155 TOOLBAR_CheckStyle (infoPtr, dwStyle);
5157 return 0;
5161 static LRESULT
5162 TOOLBAR_Destroy (TOOLBAR_INFO *infoPtr)
5164 INT i;
5166 /* delete tooltip control */
5167 if (infoPtr->hwndToolTip)
5168 DestroyWindow (infoPtr->hwndToolTip);
5170 /* delete temporary buffer for tooltip text */
5171 Free (infoPtr->pszTooltipText);
5172 Free (infoPtr->bitmaps); /* bitmaps list */
5174 /* delete button data */
5175 for (i = 0; i < infoPtr->nNumButtons; i++)
5176 if (TOOLBAR_ButtonHasString(&infoPtr->buttons[i]))
5177 Free ((LPWSTR)infoPtr->buttons[i].iString);
5178 Free (infoPtr->buttons);
5180 /* delete strings */
5181 if (infoPtr->strings) {
5182 for (i = 0; i < infoPtr->nNumStrings; i++)
5183 Free (infoPtr->strings[i]);
5185 Free (infoPtr->strings);
5188 /* destroy internal image list */
5189 if (infoPtr->himlInt)
5190 ImageList_Destroy (infoPtr->himlInt);
5192 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5193 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5194 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5196 /* delete default font */
5197 DeleteObject (infoPtr->hDefaultFont);
5199 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
5201 /* free toolbar info data */
5202 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
5203 Free (infoPtr);
5205 return 0;
5209 static LRESULT
5210 TOOLBAR_EraseBackground (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5212 NMTBCUSTOMDRAW tbcd;
5213 INT ret = FALSE;
5214 DWORD ntfret;
5215 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
5216 DWORD dwEraseCustDraw = 0;
5218 /* the app has told us not to redraw the toolbar */
5219 if (!infoPtr->bDoRedraw)
5220 return FALSE;
5222 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5223 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5224 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5225 tbcd.nmcd.hdc = (HDC)wParam;
5226 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5227 dwEraseCustDraw = ntfret & 0xffff;
5229 /* FIXME: in general the return flags *can* be or'ed together */
5230 switch (dwEraseCustDraw)
5232 case CDRF_DODEFAULT:
5233 break;
5234 case CDRF_SKIPDEFAULT:
5235 return TRUE;
5236 default:
5237 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5238 infoPtr->hwndSelf, ntfret);
5242 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5243 * to my parent for processing.
5245 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5246 POINT pt, ptorig;
5247 HDC hdc = (HDC)wParam;
5248 HWND parent;
5250 pt.x = 0;
5251 pt.y = 0;
5252 parent = GetParent(infoPtr->hwndSelf);
5253 MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
5254 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5255 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5256 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5258 if (!ret)
5259 ret = DefWindowProcW (infoPtr->hwndSelf, WM_ERASEBKGND, wParam, lParam);
5261 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5262 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5263 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5264 tbcd.nmcd.hdc = (HDC)wParam;
5265 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5266 dwEraseCustDraw = ntfret & 0xffff;
5267 switch (dwEraseCustDraw)
5269 case CDRF_DODEFAULT:
5270 break;
5271 case CDRF_SKIPDEFAULT:
5272 return TRUE;
5273 default:
5274 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5275 infoPtr->hwndSelf, ntfret);
5278 return ret;
5282 static inline LRESULT
5283 TOOLBAR_GetFont (const TOOLBAR_INFO *infoPtr)
5285 return (LRESULT)infoPtr->hFont;
5289 static void
5290 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5292 INT i;
5293 INT nNewHotItem = infoPtr->nHotItem;
5295 for (i = 0; i < infoPtr->nNumButtons; i++)
5297 /* did we wrap? */
5298 if ((nNewHotItem + iDirection < 0) ||
5299 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5301 NMTBWRAPHOTITEM nmtbwhi;
5302 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5303 nmtbwhi.iDirection = iDirection;
5304 nmtbwhi.dwReason = dwReason;
5306 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5307 return;
5310 nNewHotItem += iDirection;
5311 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5313 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5314 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5316 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5317 break;
5322 static LRESULT
5323 TOOLBAR_KeyDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5325 NMKEY nmkey;
5327 nmkey.nVKey = (UINT)wParam;
5328 nmkey.uFlags = HIWORD(lParam);
5330 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5331 return DefWindowProcW(infoPtr->hwndSelf, WM_KEYDOWN, wParam, lParam);
5333 switch ((UINT)wParam)
5335 case VK_LEFT:
5336 case VK_UP:
5337 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5338 break;
5339 case VK_RIGHT:
5340 case VK_DOWN:
5341 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5342 break;
5343 case VK_SPACE:
5344 case VK_RETURN:
5345 if ((infoPtr->nHotItem >= 0) &&
5346 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5348 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5349 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5350 (LPARAM)infoPtr->hwndSelf);
5352 break;
5355 return 0;
5359 static LRESULT
5360 TOOLBAR_LButtonDblClk (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5362 POINT pt;
5363 INT nHit;
5365 pt.x = (short)LOWORD(lParam);
5366 pt.y = (short)HIWORD(lParam);
5367 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt);
5369 if (nHit >= 0)
5370 TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
5371 else if (infoPtr->dwStyle & CCS_ADJUSTABLE)
5372 TOOLBAR_Customize (infoPtr);
5374 return 0;
5378 static LRESULT
5379 TOOLBAR_LButtonDown (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5381 TBUTTON_INFO *btnPtr;
5382 POINT pt;
5383 INT nHit;
5384 NMTOOLBARA nmtb;
5385 NMMOUSE nmmouse;
5386 BOOL bDragKeyPressed;
5388 TRACE("\n");
5390 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5391 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5392 else
5393 bDragKeyPressed = (wParam & MK_SHIFT);
5395 if (infoPtr->hwndToolTip)
5396 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5397 WM_LBUTTONDOWN, wParam, lParam);
5399 pt.x = (short)LOWORD(lParam);
5400 pt.y = (short)HIWORD(lParam);
5401 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt);
5403 btnPtr = &infoPtr->buttons[nHit];
5405 if ((nHit >= 0) && bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5407 infoPtr->nButtonDrag = nHit;
5408 SetCapture (infoPtr->hwndSelf);
5410 /* If drag cursor has not been loaded, load it.
5411 * Note: it doesn't need to be freed */
5412 if (!hCursorDrag)
5413 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5414 SetCursor(hCursorDrag);
5416 else if (nHit >= 0)
5418 RECT arrowRect;
5419 infoPtr->nOldHit = nHit;
5421 CopyRect(&arrowRect, &btnPtr->rect);
5422 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5424 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5425 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5426 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5427 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5428 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5429 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5431 LRESULT res;
5433 /* draw in pressed state */
5434 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5435 btnPtr->fsState |= TBSTATE_PRESSED;
5436 else
5437 btnPtr->bDropDownPressed = TRUE;
5438 RedrawWindow(infoPtr->hwndSelf,&btnPtr->rect,0,
5439 RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5441 memset(&nmtb, 0, sizeof(nmtb));
5442 nmtb.iItem = btnPtr->idCommand;
5443 nmtb.rcButton = btnPtr->rect;
5444 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5445 TBN_DROPDOWN);
5446 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5448 if (res != TBDDRET_TREATPRESSED)
5450 MSG msg;
5452 /* redraw button in unpressed state */
5453 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5454 btnPtr->fsState &= ~TBSTATE_PRESSED;
5455 else
5456 btnPtr->bDropDownPressed = FALSE;
5457 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5459 /* find and set hot item
5460 * NOTE: native doesn't do this, but that is a bug */
5461 GetCursorPos(&pt);
5462 ScreenToClient(infoPtr->hwndSelf, &pt);
5463 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt);
5464 if (!infoPtr->bAnchor || (nHit >= 0))
5465 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5467 /* remove any left mouse button down or double-click messages
5468 * so that we can get a toggle effect on the button */
5469 while (PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5470 PeekMessageW(&msg, infoPtr->hwndSelf, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5473 return 0;
5475 /* otherwise drop through and process as pushed */
5477 infoPtr->bCaptured = TRUE;
5478 infoPtr->nButtonDown = nHit;
5479 infoPtr->bDragOutSent = FALSE;
5481 btnPtr->fsState |= TBSTATE_PRESSED;
5483 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5485 if (btnPtr->fsState & TBSTATE_ENABLED)
5486 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5487 UpdateWindow(infoPtr->hwndSelf);
5488 SetCapture (infoPtr->hwndSelf);
5491 if (nHit >=0)
5493 memset(&nmtb, 0, sizeof(nmtb));
5494 nmtb.iItem = btnPtr->idCommand;
5495 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5498 nmmouse.dwHitInfo = nHit;
5500 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5501 if (nHit < 0)
5502 nmmouse.dwItemSpec = -1;
5503 else
5505 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5506 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5509 ClientToScreen(infoPtr->hwndSelf, &pt);
5510 nmmouse.pt = pt;
5512 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5513 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONDOWN, wParam, lParam);
5515 return 0;
5518 static LRESULT
5519 TOOLBAR_LButtonUp (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5521 TBUTTON_INFO *btnPtr;
5522 POINT pt;
5523 INT nHit;
5524 INT nOldIndex = -1;
5525 NMHDR hdr;
5526 NMMOUSE nmmouse;
5527 NMTOOLBARA nmtb;
5529 if (infoPtr->hwndToolTip)
5530 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5531 WM_LBUTTONUP, wParam, lParam);
5533 pt.x = (short)LOWORD(lParam);
5534 pt.y = (short)HIWORD(lParam);
5535 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt);
5537 if (!infoPtr->bAnchor || (nHit >= 0))
5538 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5540 if (infoPtr->nButtonDrag >= 0) {
5541 RECT rcClient;
5542 NMHDR hdr;
5544 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5545 ReleaseCapture();
5546 /* reset cursor */
5547 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5549 GetClientRect(infoPtr->hwndSelf, &rcClient);
5550 if (PtInRect(&rcClient, pt))
5552 INT nButton = -1;
5553 if (nHit >= 0)
5554 nButton = nHit;
5555 else if (nHit < -1)
5556 nButton = -nHit;
5557 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5558 nButton = -nHit;
5560 if (nButton == infoPtr->nButtonDrag)
5562 /* if the button is moved sightly left and we have a
5563 * separator there then remove it */
5564 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5566 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5567 TOOLBAR_DeleteButton(infoPtr, nButton - 1);
5569 else /* else insert a separator before the dragged button */
5571 TBBUTTON tbb;
5572 memset(&tbb, 0, sizeof(tbb));
5573 tbb.fsStyle = BTNS_SEP;
5574 tbb.iString = -1;
5575 TOOLBAR_InsertButtonT(infoPtr, nButton, &tbb, TRUE);
5578 else
5580 if (nButton == -1)
5582 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5583 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, 0);
5584 else
5585 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5587 else
5588 TOOLBAR_MoveButton(infoPtr, infoPtr->nButtonDrag, nButton);
5591 else
5593 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5594 TOOLBAR_DeleteButton(infoPtr, infoPtr->nButtonDrag);
5597 /* button under cursor changed so need to re-set hot item */
5598 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5599 infoPtr->nButtonDrag = -1;
5601 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5603 else if (infoPtr->nButtonDown >= 0) {
5604 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5605 btnPtr->fsState &= ~TBSTATE_PRESSED;
5607 if (btnPtr->fsStyle & BTNS_CHECK) {
5608 if (btnPtr->fsStyle & BTNS_GROUP) {
5609 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5610 nHit);
5611 if ((nOldIndex != nHit) &&
5612 (nOldIndex != -1))
5613 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5614 btnPtr->fsState |= TBSTATE_CHECKED;
5616 else {
5617 if (btnPtr->fsState & TBSTATE_CHECKED)
5618 btnPtr->fsState &= ~TBSTATE_CHECKED;
5619 else
5620 btnPtr->fsState |= TBSTATE_CHECKED;
5624 if (nOldIndex != -1)
5625 InvalidateRect(infoPtr->hwndSelf, &infoPtr->buttons[nOldIndex].rect, TRUE);
5628 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5629 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5630 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5632 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5633 ReleaseCapture ();
5634 infoPtr->nButtonDown = -1;
5636 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5637 TOOLBAR_SendNotify (&hdr, infoPtr,
5638 NM_RELEASEDCAPTURE);
5640 /* native issues TBN_ENDDRAG here, if _LBUTTONDOWN issued the
5641 * TBN_BEGINDRAG
5643 memset(&nmtb, 0, sizeof(nmtb));
5644 nmtb.iItem = btnPtr->idCommand;
5645 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5646 TBN_ENDDRAG);
5648 if (btnPtr->fsState & TBSTATE_ENABLED)
5650 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5651 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)infoPtr->hwndSelf);
5653 /* In case we have just been destroyed... */
5654 if(!IsWindow(infoPtr->hwndSelf))
5655 return 0;
5659 /* !!! Undocumented - toolbar at 4.71 level and above sends
5660 * NM_CLICK with the NMMOUSE structure. */
5661 nmmouse.dwHitInfo = nHit;
5663 if (nHit < 0)
5664 nmmouse.dwItemSpec = -1;
5665 else
5667 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5668 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5671 ClientToScreen(infoPtr->hwndSelf, &pt);
5672 nmmouse.pt = pt;
5674 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5675 return DefWindowProcW(infoPtr->hwndSelf, WM_LBUTTONUP, wParam, lParam);
5677 return 0;
5680 static LRESULT
5681 TOOLBAR_RButtonUp(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5683 INT nHit;
5684 NMMOUSE nmmouse;
5685 POINT pt;
5687 pt.x = (short)LOWORD(lParam);
5688 pt.y = (short)HIWORD(lParam);
5690 nHit = TOOLBAR_InternalHitTest(infoPtr, &pt);
5691 nmmouse.dwHitInfo = nHit;
5693 if (nHit < 0) {
5694 nmmouse.dwItemSpec = -1;
5695 } else {
5696 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5697 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5700 ClientToScreen(infoPtr->hwndSelf, &pt);
5701 nmmouse.pt = pt;
5703 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5704 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONUP, wParam, lParam);
5706 return 0;
5709 static LRESULT
5710 TOOLBAR_RButtonDblClk( TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5712 NMHDR nmhdr;
5714 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5715 return DefWindowProcW(infoPtr->hwndSelf, WM_RBUTTONDBLCLK, wParam, lParam);
5717 return 0;
5720 static LRESULT
5721 TOOLBAR_CaptureChanged(TOOLBAR_INFO *infoPtr)
5723 TBUTTON_INFO *btnPtr;
5725 infoPtr->bCaptured = FALSE;
5727 if (infoPtr->nButtonDown >= 0)
5729 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5730 btnPtr->fsState &= ~TBSTATE_PRESSED;
5732 infoPtr->nOldHit = -1;
5734 if (btnPtr->fsState & TBSTATE_ENABLED)
5735 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5737 return 0;
5740 static LRESULT
5741 TOOLBAR_MouseLeave (TOOLBAR_INFO *infoPtr)
5743 /* don't remove hot effects when in anchor highlighting mode or when a
5744 * drop-down button is pressed */
5745 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
5747 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
5748 if (!hotBtnPtr->bDropDownPressed)
5749 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
5752 if (infoPtr->nOldHit < 0)
5753 return TRUE;
5755 /* If the last button we were over is depressed then make it not */
5756 /* depressed and redraw it */
5757 if(infoPtr->nOldHit == infoPtr->nButtonDown)
5759 TBUTTON_INFO *btnPtr;
5760 RECT rc1;
5762 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5764 btnPtr->fsState &= ~TBSTATE_PRESSED;
5766 rc1 = btnPtr->rect;
5767 InflateRect (&rc1, 1, 1);
5768 InvalidateRect (infoPtr->hwndSelf, &rc1, TRUE);
5771 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
5773 NMTOOLBARW nmt;
5774 ZeroMemory(&nmt, sizeof(nmt));
5775 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5776 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5777 infoPtr->bDragOutSent = TRUE;
5780 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
5782 return TRUE;
5785 static LRESULT
5786 TOOLBAR_MouseMove (TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5788 POINT pt;
5789 TRACKMOUSEEVENT trackinfo;
5790 INT nHit;
5791 TBUTTON_INFO *btnPtr;
5793 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
5794 TOOLBAR_TooltipCreateControl(infoPtr);
5796 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
5797 /* fill in the TRACKMOUSEEVENT struct */
5798 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5799 trackinfo.dwFlags = TME_QUERY;
5801 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5802 _TrackMouseEvent(&trackinfo);
5804 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5805 if(trackinfo.hwndTrack != infoPtr->hwndSelf || !(trackinfo.dwFlags & TME_LEAVE)) {
5806 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5807 trackinfo.hwndTrack = infoPtr->hwndSelf;
5809 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5810 /* and can properly deactivate the hot toolbar button */
5811 _TrackMouseEvent(&trackinfo);
5815 if (infoPtr->hwndToolTip)
5816 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, infoPtr->hwndSelf,
5817 WM_MOUSEMOVE, wParam, lParam);
5819 pt.x = (short)LOWORD(lParam);
5820 pt.y = (short)HIWORD(lParam);
5822 nHit = TOOLBAR_InternalHitTest (infoPtr, &pt);
5824 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5825 && (!infoPtr->bAnchor || (nHit >= 0)))
5826 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE);
5828 if (infoPtr->nOldHit != nHit)
5830 if (infoPtr->bCaptured)
5832 if (!infoPtr->bDragOutSent)
5834 NMTOOLBARW nmt;
5835 ZeroMemory(&nmt, sizeof(nmt));
5836 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
5837 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
5838 infoPtr->bDragOutSent = TRUE;
5841 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5842 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
5843 btnPtr->fsState &= ~TBSTATE_PRESSED;
5844 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5846 else if (nHit == infoPtr->nButtonDown) {
5847 btnPtr->fsState |= TBSTATE_PRESSED;
5848 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
5850 infoPtr->nOldHit = nHit;
5854 return 0;
5858 static inline LRESULT
5859 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
5861 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
5862 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
5863 /* else */
5864 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
5868 static inline LRESULT
5869 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
5871 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
5872 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
5874 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
5878 static LRESULT
5879 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
5881 TOOLBAR_INFO *infoPtr;
5882 DWORD styleadd = 0;
5884 /* allocate memory for info structure */
5885 infoPtr = Alloc (sizeof(TOOLBAR_INFO));
5886 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
5888 /* paranoid!! */
5889 infoPtr->dwStructSize = sizeof(TBBUTTON);
5890 infoPtr->nRows = 1;
5891 infoPtr->nWidth = 0;
5893 /* initialize info structure */
5894 infoPtr->nButtonWidth = 23;
5895 infoPtr->nButtonHeight = 22;
5896 infoPtr->nBitmapHeight = 16;
5897 infoPtr->nBitmapWidth = 16;
5899 infoPtr->nMaxTextRows = 1;
5900 infoPtr->cxMin = -1;
5901 infoPtr->cxMax = -1;
5902 infoPtr->nNumBitmaps = 0;
5903 infoPtr->nNumStrings = 0;
5905 infoPtr->bCaptured = FALSE;
5906 infoPtr->nButtonDown = -1;
5907 infoPtr->nButtonDrag = -1;
5908 infoPtr->nOldHit = -1;
5909 infoPtr->nHotItem = -1;
5910 infoPtr->hwndNotify = lpcs->hwndParent;
5911 infoPtr->dwDTFlags = (lpcs->style & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
5912 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
5913 infoPtr->bDragOutSent = FALSE;
5914 infoPtr->iVersion = 0;
5915 infoPtr->hwndSelf = hwnd;
5916 infoPtr->bDoRedraw = TRUE;
5917 infoPtr->clrBtnHighlight = CLR_DEFAULT;
5918 infoPtr->clrBtnShadow = CLR_DEFAULT;
5919 infoPtr->szPadding.cx = DEFPAD_CX;
5920 infoPtr->szPadding.cy = DEFPAD_CY;
5921 infoPtr->iListGap = DEFLISTGAP;
5922 infoPtr->iTopMargin = default_top_margin(infoPtr);
5923 infoPtr->dwStyle = lpcs->style;
5924 infoPtr->tbim.iButton = -1;
5926 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
5927 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
5928 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
5929 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
5932 /* native control does:
5933 * Get a lot of colors and brushes
5934 * WM_NOTIFYFORMAT
5935 * SystemParametersInfoW(0x1f, 0x3c, adr1, 0)
5936 * CreateFontIndirectW(adr1)
5937 * CreateBitmap(0x27, 0x24, 1, 1, 0)
5938 * hdc = GetDC(toolbar)
5939 * GetSystemMetrics(0x48)
5940 * fnt2=CreateFontW(0xe, 0, 0, 0, 0x190, 0, 0, 0, 0, 2,
5941 * 0, 0, 0, 0, "MARLETT")
5942 * oldfnt = SelectObject(hdc, fnt2)
5943 * GetCharWidthW(hdc, 0x36, 0x36, adr2)
5944 * GetTextMetricsW(hdc, adr3)
5945 * SelectObject(hdc, oldfnt)
5946 * DeleteObject(fnt2)
5947 * ReleaseDC(hdc)
5948 * InvalidateRect(toolbar, 0, 1)
5949 * SetWindowLongW(toolbar, 0, addr)
5950 * SetWindowLongW(toolbar, -16, xxx) **sometimes**
5951 * WM_STYLECHANGING
5952 * CallWinEx old new
5953 * ie 1 0x56000a4c 0x46000a4c 0x56008a4d
5954 * ie 2 0x4600094c 0x4600094c 0x4600894d
5955 * ie 3 0x56000b4c 0x46000b4c 0x56008b4d
5956 * rebar 0x50008844 0x40008844 0x50008845
5957 * pager 0x50000844 0x40000844 0x50008845
5958 * IC35mgr 0x5400084e **nochange**
5959 * on entry to _NCCREATE 0x5400084e
5960 * rowlist 0x5400004e **nochange**
5961 * on entry to _NCCREATE 0x5400004e
5965 /* I think the code below is a bug, but it is the way that the native
5966 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
5967 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
5968 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
5969 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
5970 * Somehow, the only cases of this seem to be MFC programs.
5972 * Note also that the addition of _TRANSPARENT occurs *only* here. It
5973 * does not occur in the WM_STYLECHANGING routine.
5974 * (Guy Albertelli 9/2001)
5977 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
5978 && !(lpcs->style & TBSTYLE_TRANSPARENT))
5979 styleadd |= TBSTYLE_TRANSPARENT;
5980 if (!(lpcs->style & (CCS_TOP | CCS_NOMOVEY))) {
5981 styleadd |= CCS_TOP; /* default to top */
5982 SetWindowLongW (hwnd, GWL_STYLE, lpcs->style | styleadd);
5985 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
5989 static LRESULT
5990 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
5992 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
5993 RECT rcWindow;
5994 HDC hdc;
5996 if (dwStyle & WS_MINIMIZE)
5997 return 0; /* Nothing to do */
5999 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6001 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6002 return 0;
6004 if (!(dwStyle & CCS_NODIVIDER))
6006 GetWindowRect (hwnd, &rcWindow);
6007 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6008 if( dwStyle & WS_BORDER )
6009 InflateRect (&rcWindow, -1, -1);
6010 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6013 ReleaseDC( hwnd, hdc );
6015 return 0;
6019 /* handles requests from the tooltip control on what text to display */
6020 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6022 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6024 TRACE("button index = %d\n", index);
6026 Free (infoPtr->pszTooltipText);
6027 infoPtr->pszTooltipText = NULL;
6029 if (index < 0)
6030 return 0;
6032 if (infoPtr->bUnicode)
6034 WCHAR wszBuffer[INFOTIPSIZE+1];
6035 NMTBGETINFOTIPW tbgit;
6036 unsigned int len; /* in chars */
6038 wszBuffer[0] = '\0';
6039 wszBuffer[INFOTIPSIZE] = '\0';
6041 tbgit.pszText = wszBuffer;
6042 tbgit.cchTextMax = INFOTIPSIZE;
6043 tbgit.iItem = lpnmtdi->hdr.idFrom;
6044 tbgit.lParam = infoPtr->buttons[index].dwData;
6046 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6048 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6050 len = strlenW(tbgit.pszText);
6051 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6053 /* need to allocate temporary buffer in infoPtr as there
6054 * isn't enough space in buffer passed to us by the
6055 * tooltip control */
6056 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6057 if (infoPtr->pszTooltipText)
6059 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6060 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6061 return 0;
6064 else if (len > 0)
6066 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6067 return 0;
6070 else
6072 CHAR szBuffer[INFOTIPSIZE+1];
6073 NMTBGETINFOTIPA tbgit;
6074 unsigned int len; /* in chars */
6076 szBuffer[0] = '\0';
6077 szBuffer[INFOTIPSIZE] = '\0';
6079 tbgit.pszText = szBuffer;
6080 tbgit.cchTextMax = INFOTIPSIZE;
6081 tbgit.iItem = lpnmtdi->hdr.idFrom;
6082 tbgit.lParam = infoPtr->buttons[index].dwData;
6084 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6086 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6088 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6089 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6091 /* need to allocate temporary buffer in infoPtr as there
6092 * isn't enough space in buffer passed to us by the
6093 * tooltip control */
6094 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6095 if (infoPtr->pszTooltipText)
6097 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6098 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6099 return 0;
6102 else if (tbgit.pszText && tbgit.pszText[0])
6104 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6105 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6106 return 0;
6110 /* if button has text, but it is not shown then automatically
6111 * use that text as tooltip */
6112 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6113 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6115 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6116 unsigned int len = pszText ? strlenW(pszText) : 0;
6118 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6120 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6122 /* need to allocate temporary buffer in infoPtr as there
6123 * isn't enough space in buffer passed to us by the
6124 * tooltip control */
6125 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6126 if (infoPtr->pszTooltipText)
6128 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6129 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6130 return 0;
6133 else if (len > 0)
6135 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6136 return 0;
6140 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6142 /* last resort: send notification on to app */
6143 /* FIXME: find out what is really used here */
6144 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6148 static inline LRESULT
6149 TOOLBAR_Notify (TOOLBAR_INFO *infoPtr, LPNMHDR lpnmh)
6151 switch (lpnmh->code)
6153 case PGN_CALCSIZE:
6155 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lpnmh;
6157 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6158 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6159 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6160 lppgc->iWidth);
6162 else {
6163 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6164 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6165 lppgc->iHeight);
6167 return 0;
6170 case PGN_SCROLL:
6172 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lpnmh;
6174 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6175 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6176 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6177 lppgs->iScroll, lppgs->iDir);
6178 return 0;
6181 case TTN_GETDISPINFOW:
6182 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lpnmh);
6184 case TTN_GETDISPINFOA:
6185 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6186 return 0;
6188 default:
6189 return 0;
6194 static LRESULT
6195 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6197 LRESULT format;
6199 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6201 if (lParam == NF_QUERY)
6202 return NFR_UNICODE;
6204 if (lParam == NF_REQUERY) {
6205 format = SendMessageW(infoPtr->hwndNotify,
6206 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6207 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6208 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6209 format);
6210 format = NFR_ANSI;
6212 return format;
6214 return 0;
6218 static LRESULT
6219 TOOLBAR_Paint (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6221 HDC hdc;
6222 PAINTSTRUCT ps;
6224 /* fill ps.rcPaint with a default rect */
6225 ps.rcPaint = infoPtr->rcBound;
6227 hdc = wParam==0 ? BeginPaint(infoPtr->hwndSelf, &ps) : (HDC)wParam;
6229 TRACE("psrect=(%s)\n", wine_dbgstr_rect(&ps.rcPaint));
6231 TOOLBAR_Refresh (infoPtr, hdc, &ps);
6232 if (!wParam) EndPaint (infoPtr->hwndSelf, &ps);
6234 return 0;
6238 static LRESULT
6239 TOOLBAR_SetFocus (TOOLBAR_INFO *infoPtr)
6241 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6243 /* make first item hot */
6244 if (infoPtr->nNumButtons > 0)
6245 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6247 return 0;
6250 static LRESULT
6251 TOOLBAR_SetFont(TOOLBAR_INFO *infoPtr, HFONT hFont, WORD Redraw)
6253 TRACE("font=%p redraw=%d\n", hFont, Redraw);
6255 if (hFont == 0)
6256 infoPtr->hFont = infoPtr->hDefaultFont;
6257 else
6258 infoPtr->hFont = hFont;
6260 TOOLBAR_CalcToolbar(infoPtr);
6262 if (Redraw)
6263 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6264 return 1;
6267 static LRESULT
6268 TOOLBAR_SetRedraw (TOOLBAR_INFO *infoPtr, WPARAM wParam)
6269 /*****************************************************
6271 * Function;
6272 * Handles the WM_SETREDRAW message.
6274 * Documentation:
6275 * According to testing V4.71 of COMCTL32 returns the
6276 * *previous* status of the redraw flag (either 0 or 1)
6277 * instead of the MSDN documented value of 0 if handled.
6278 * (For laughs see the "consistency" with same function
6279 * in rebar.)
6281 *****************************************************/
6283 BOOL oldredraw = infoPtr->bDoRedraw;
6285 TRACE("set to %s\n",
6286 (wParam) ? "TRUE" : "FALSE");
6287 infoPtr->bDoRedraw = (BOOL) wParam;
6288 if (wParam) {
6289 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6291 return (oldredraw) ? 1 : 0;
6295 static LRESULT
6296 TOOLBAR_Size (TOOLBAR_INFO *infoPtr)
6298 TRACE("sizing toolbar!\n");
6300 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6302 RECT delta_width, delta_height, client, dummy;
6303 DWORD min_x, max_x, min_y, max_y;
6304 TBUTTON_INFO *btnPtr;
6305 INT i;
6307 GetClientRect(infoPtr->hwndSelf, &client);
6308 if(client.right > infoPtr->client_rect.right)
6310 min_x = infoPtr->client_rect.right;
6311 max_x = client.right;
6313 else
6315 max_x = infoPtr->client_rect.right;
6316 min_x = client.right;
6318 if(client.bottom > infoPtr->client_rect.bottom)
6320 min_y = infoPtr->client_rect.bottom;
6321 max_y = client.bottom;
6323 else
6325 max_y = infoPtr->client_rect.bottom;
6326 min_y = client.bottom;
6329 SetRect(&delta_width, min_x, 0, max_x, min_y);
6330 SetRect(&delta_height, 0, min_y, max_x, max_y);
6332 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6333 btnPtr = infoPtr->buttons;
6334 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6335 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6336 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6337 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
6339 GetClientRect(infoPtr->hwndSelf, &infoPtr->client_rect);
6340 TOOLBAR_AutoSize(infoPtr);
6341 return 0;
6345 static LRESULT
6346 TOOLBAR_StyleChanged (TOOLBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
6348 if (nType == GWL_STYLE)
6350 DWORD dwOldStyle = infoPtr->dwStyle;
6352 if (lpStyle->styleNew & TBSTYLE_LIST)
6353 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
6354 else
6355 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
6357 TOOLBAR_CheckStyle (infoPtr, lpStyle->styleNew);
6359 TRACE("new style 0x%08x\n", lpStyle->styleNew);
6361 infoPtr->dwStyle = lpStyle->styleNew;
6363 if ((dwOldStyle ^ lpStyle->styleNew) & (TBSTYLE_WRAPABLE | CCS_VERT))
6364 TOOLBAR_LayoutToolbar(infoPtr);
6366 /* only resize if one of the CCS_* styles was changed */
6367 if ((dwOldStyle ^ lpStyle->styleNew) & COMMON_STYLES)
6369 TOOLBAR_AutoSize (infoPtr);
6371 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
6375 return 0;
6379 static LRESULT
6380 TOOLBAR_SysColorChange (void)
6382 COMCTL32_RefreshSysColors();
6384 return 0;
6388 /* update theme after a WM_THEMECHANGED message */
6389 static LRESULT theme_changed (HWND hwnd)
6391 HTHEME theme = GetWindowTheme (hwnd);
6392 CloseThemeData (theme);
6393 OpenThemeData (hwnd, themeClass);
6394 return 0;
6398 static LRESULT WINAPI
6399 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6401 TOOLBAR_INFO *infoPtr = (TOOLBAR_INFO *)GetWindowLongPtrW(hwnd, 0);
6403 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6404 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6406 if (!infoPtr && (uMsg != WM_NCCREATE))
6407 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6409 switch (uMsg)
6411 case TB_ADDBITMAP:
6412 return TOOLBAR_AddBitmap (infoPtr, (INT)wParam, (TBADDBITMAP*)lParam);
6414 case TB_ADDBUTTONSA:
6415 case TB_ADDBUTTONSW:
6416 return TOOLBAR_AddButtonsT (infoPtr, wParam, (LPTBBUTTON)lParam,
6417 uMsg == TB_ADDBUTTONSW);
6418 case TB_ADDSTRINGA:
6419 return TOOLBAR_AddStringA (infoPtr, (HINSTANCE)wParam, lParam);
6421 case TB_ADDSTRINGW:
6422 return TOOLBAR_AddStringW (infoPtr, (HINSTANCE)wParam, lParam);
6424 case TB_AUTOSIZE:
6425 return TOOLBAR_AutoSize (infoPtr);
6427 case TB_BUTTONCOUNT:
6428 return TOOLBAR_ButtonCount (infoPtr);
6430 case TB_BUTTONSTRUCTSIZE:
6431 return TOOLBAR_ButtonStructSize (infoPtr, wParam);
6433 case TB_CHANGEBITMAP:
6434 return TOOLBAR_ChangeBitmap (infoPtr, wParam, LOWORD(lParam));
6436 case TB_CHECKBUTTON:
6437 return TOOLBAR_CheckButton (infoPtr, wParam, lParam);
6439 case TB_COMMANDTOINDEX:
6440 return TOOLBAR_CommandToIndex (infoPtr, wParam);
6442 case TB_CUSTOMIZE:
6443 return TOOLBAR_Customize (infoPtr);
6445 case TB_DELETEBUTTON:
6446 return TOOLBAR_DeleteButton (infoPtr, wParam);
6448 case TB_ENABLEBUTTON:
6449 return TOOLBAR_EnableButton (infoPtr, wParam, lParam);
6451 case TB_GETANCHORHIGHLIGHT:
6452 return TOOLBAR_GetAnchorHighlight (infoPtr);
6454 case TB_GETBITMAP:
6455 return TOOLBAR_GetBitmap (infoPtr, wParam);
6457 case TB_GETBITMAPFLAGS:
6458 return TOOLBAR_GetBitmapFlags ();
6460 case TB_GETBUTTON:
6461 return TOOLBAR_GetButton (infoPtr, wParam, (TBBUTTON*)lParam);
6463 case TB_GETBUTTONINFOA:
6464 case TB_GETBUTTONINFOW:
6465 return TOOLBAR_GetButtonInfoT (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6466 uMsg == TB_GETBUTTONINFOW);
6467 case TB_GETBUTTONSIZE:
6468 return TOOLBAR_GetButtonSize (infoPtr);
6470 case TB_GETBUTTONTEXTA:
6471 case TB_GETBUTTONTEXTW:
6472 return TOOLBAR_GetButtonText (infoPtr, wParam, (LPWSTR)lParam,
6473 uMsg == TB_GETBUTTONTEXTW);
6475 case TB_GETDISABLEDIMAGELIST:
6476 return TOOLBAR_GetDisabledImageList (infoPtr, wParam);
6478 case TB_GETEXTENDEDSTYLE:
6479 return TOOLBAR_GetExtendedStyle (infoPtr);
6481 case TB_GETHOTIMAGELIST:
6482 return TOOLBAR_GetHotImageList (infoPtr, wParam);
6484 case TB_GETHOTITEM:
6485 return TOOLBAR_GetHotItem (infoPtr);
6487 case TB_GETIMAGELIST:
6488 return TOOLBAR_GetDefImageList (infoPtr, wParam);
6490 case TB_GETINSERTMARK:
6491 return TOOLBAR_GetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6493 case TB_GETINSERTMARKCOLOR:
6494 return TOOLBAR_GetInsertMarkColor (infoPtr);
6496 case TB_GETITEMRECT:
6497 return TOOLBAR_GetItemRect (infoPtr, wParam, (LPRECT)lParam);
6499 case TB_GETMAXSIZE:
6500 return TOOLBAR_GetMaxSize (infoPtr, (LPSIZE)lParam);
6502 /* case TB_GETOBJECT: */ /* 4.71 */
6504 case TB_GETPADDING:
6505 return TOOLBAR_GetPadding (infoPtr);
6507 case TB_GETRECT:
6508 return TOOLBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
6510 case TB_GETROWS:
6511 return TOOLBAR_GetRows (infoPtr);
6513 case TB_GETSTATE:
6514 return TOOLBAR_GetState (infoPtr, wParam);
6516 case TB_GETSTRINGA:
6517 return TOOLBAR_GetStringA (infoPtr, wParam, (LPSTR)lParam);
6519 case TB_GETSTRINGW:
6520 return TOOLBAR_GetStringW (infoPtr, wParam, (LPWSTR)lParam);
6522 case TB_GETSTYLE:
6523 return TOOLBAR_GetStyle (infoPtr);
6525 case TB_GETTEXTROWS:
6526 return TOOLBAR_GetTextRows (infoPtr);
6528 case TB_GETTOOLTIPS:
6529 return TOOLBAR_GetToolTips (infoPtr);
6531 case TB_GETUNICODEFORMAT:
6532 return TOOLBAR_GetUnicodeFormat (infoPtr);
6534 case TB_HIDEBUTTON:
6535 return TOOLBAR_HideButton (infoPtr, wParam, LOWORD(lParam));
6537 case TB_HITTEST:
6538 return TOOLBAR_HitTest (infoPtr, (LPPOINT)lParam);
6540 case TB_INDETERMINATE:
6541 return TOOLBAR_Indeterminate (infoPtr, wParam, LOWORD(lParam));
6543 case TB_INSERTBUTTONA:
6544 case TB_INSERTBUTTONW:
6545 return TOOLBAR_InsertButtonT(infoPtr, wParam, (TBBUTTON*)lParam,
6546 uMsg == TB_INSERTBUTTONW);
6548 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6550 case TB_ISBUTTONCHECKED:
6551 return TOOLBAR_IsButtonChecked (infoPtr, wParam);
6553 case TB_ISBUTTONENABLED:
6554 return TOOLBAR_IsButtonEnabled (infoPtr, wParam);
6556 case TB_ISBUTTONHIDDEN:
6557 return TOOLBAR_IsButtonHidden (infoPtr, wParam);
6559 case TB_ISBUTTONHIGHLIGHTED:
6560 return TOOLBAR_IsButtonHighlighted (infoPtr, wParam);
6562 case TB_ISBUTTONINDETERMINATE:
6563 return TOOLBAR_IsButtonIndeterminate (infoPtr, wParam);
6565 case TB_ISBUTTONPRESSED:
6566 return TOOLBAR_IsButtonPressed (infoPtr, wParam);
6568 case TB_LOADIMAGES:
6569 return TOOLBAR_LoadImages (infoPtr, wParam, (HINSTANCE)lParam);
6571 case TB_MAPACCELERATORA:
6572 case TB_MAPACCELERATORW:
6573 return TOOLBAR_MapAccelerator (infoPtr, wParam, (UINT*)lParam);
6575 case TB_MARKBUTTON:
6576 return TOOLBAR_MarkButton (infoPtr, wParam, LOWORD(lParam));
6578 case TB_MOVEBUTTON:
6579 return TOOLBAR_MoveButton (infoPtr, wParam, lParam);
6581 case TB_PRESSBUTTON:
6582 return TOOLBAR_PressButton (infoPtr, wParam, LOWORD(lParam));
6584 case TB_REPLACEBITMAP:
6585 return TOOLBAR_ReplaceBitmap (infoPtr, (LPTBREPLACEBITMAP)lParam);
6587 case TB_SAVERESTOREA:
6588 return TOOLBAR_SaveRestoreA (infoPtr, wParam, (LPTBSAVEPARAMSA)lParam);
6590 case TB_SAVERESTOREW:
6591 return TOOLBAR_SaveRestoreW (infoPtr, wParam, (LPTBSAVEPARAMSW)lParam);
6593 case TB_SETANCHORHIGHLIGHT:
6594 return TOOLBAR_SetAnchorHighlight (infoPtr, (BOOL)wParam);
6596 case TB_SETBITMAPSIZE:
6597 return TOOLBAR_SetBitmapSize (infoPtr, wParam, lParam);
6599 case TB_SETBUTTONINFOA:
6600 case TB_SETBUTTONINFOW:
6601 return TOOLBAR_SetButtonInfo (infoPtr, wParam, (LPTBBUTTONINFOW)lParam,
6602 uMsg == TB_SETBUTTONINFOW);
6603 case TB_SETBUTTONSIZE:
6604 return TOOLBAR_SetButtonSize (infoPtr, lParam);
6606 case TB_SETBUTTONWIDTH:
6607 return TOOLBAR_SetButtonWidth (infoPtr, lParam);
6609 case TB_SETCMDID:
6610 return TOOLBAR_SetCmdId (infoPtr, wParam, lParam);
6612 case TB_SETDISABLEDIMAGELIST:
6613 return TOOLBAR_SetDisabledImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6615 case TB_SETDRAWTEXTFLAGS:
6616 return TOOLBAR_SetDrawTextFlags (infoPtr, wParam, lParam);
6618 case TB_SETEXTENDEDSTYLE:
6619 return TOOLBAR_SetExtendedStyle (infoPtr, lParam);
6621 case TB_SETHOTIMAGELIST:
6622 return TOOLBAR_SetHotImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6624 case TB_SETHOTITEM:
6625 return TOOLBAR_SetHotItem (infoPtr, wParam);
6627 case TB_SETIMAGELIST:
6628 return TOOLBAR_SetImageList (infoPtr, wParam, (HIMAGELIST)lParam);
6630 case TB_SETINDENT:
6631 return TOOLBAR_SetIndent (infoPtr, wParam);
6633 case TB_SETINSERTMARK:
6634 return TOOLBAR_SetInsertMark (infoPtr, (TBINSERTMARK*)lParam);
6636 case TB_SETINSERTMARKCOLOR:
6637 return TOOLBAR_SetInsertMarkColor (infoPtr, lParam);
6639 case TB_SETMAXTEXTROWS:
6640 return TOOLBAR_SetMaxTextRows (infoPtr, wParam);
6642 case TB_SETPADDING:
6643 return TOOLBAR_SetPadding (infoPtr, lParam);
6645 case TB_SETPARENT:
6646 return TOOLBAR_SetParent (infoPtr, (HWND)wParam);
6648 case TB_SETROWS:
6649 return TOOLBAR_SetRows (infoPtr, wParam, (LPRECT)lParam);
6651 case TB_SETSTATE:
6652 return TOOLBAR_SetState (infoPtr, wParam, lParam);
6654 case TB_SETSTYLE:
6655 return TOOLBAR_SetStyle (infoPtr, lParam);
6657 case TB_SETTOOLTIPS:
6658 return TOOLBAR_SetToolTips (infoPtr, (HWND)wParam);
6660 case TB_SETUNICODEFORMAT:
6661 return TOOLBAR_SetUnicodeFormat (infoPtr, wParam);
6663 case TB_UNKWN45D:
6664 return TOOLBAR_Unkwn45D(hwnd, wParam, lParam);
6666 case TB_SETHOTITEM2:
6667 return TOOLBAR_SetHotItem2 (infoPtr, wParam, lParam);
6669 case TB_SETLISTGAP:
6670 return TOOLBAR_SetListGap(infoPtr, wParam);
6672 case TB_GETIMAGELISTCOUNT:
6673 return TOOLBAR_GetImageListCount(infoPtr);
6675 case TB_GETIDEALSIZE:
6676 return TOOLBAR_GetIdealSize (infoPtr, wParam, lParam);
6678 case TB_UNKWN464:
6679 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6681 /* Common Control Messages */
6683 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6684 case CCM_GETCOLORSCHEME:
6685 return TOOLBAR_GetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6687 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6688 case CCM_SETCOLORSCHEME:
6689 return TOOLBAR_SetColorScheme (infoPtr, (LPCOLORSCHEME)lParam);
6691 case CCM_GETVERSION:
6692 return TOOLBAR_GetVersion (infoPtr);
6694 case CCM_SETVERSION:
6695 return TOOLBAR_SetVersion (infoPtr, (INT)wParam);
6698 /* case WM_CHAR: */
6700 case WM_CREATE:
6701 return TOOLBAR_Create (hwnd, (CREATESTRUCTW*)lParam);
6703 case WM_DESTROY:
6704 return TOOLBAR_Destroy (infoPtr);
6706 case WM_ERASEBKGND:
6707 return TOOLBAR_EraseBackground (infoPtr, wParam, lParam);
6709 case WM_GETFONT:
6710 return TOOLBAR_GetFont (infoPtr);
6712 case WM_KEYDOWN:
6713 return TOOLBAR_KeyDown (infoPtr, wParam, lParam);
6715 /* case WM_KILLFOCUS: */
6717 case WM_LBUTTONDBLCLK:
6718 return TOOLBAR_LButtonDblClk (infoPtr, wParam, lParam);
6720 case WM_LBUTTONDOWN:
6721 return TOOLBAR_LButtonDown (infoPtr, wParam, lParam);
6723 case WM_LBUTTONUP:
6724 return TOOLBAR_LButtonUp (infoPtr, wParam, lParam);
6726 case WM_RBUTTONUP:
6727 return TOOLBAR_RButtonUp (infoPtr, wParam, lParam);
6729 case WM_RBUTTONDBLCLK:
6730 return TOOLBAR_RButtonDblClk (infoPtr, wParam, lParam);
6732 case WM_MOUSEMOVE:
6733 return TOOLBAR_MouseMove (infoPtr, wParam, lParam);
6735 case WM_MOUSELEAVE:
6736 return TOOLBAR_MouseLeave (infoPtr);
6738 case WM_CAPTURECHANGED:
6739 return TOOLBAR_CaptureChanged(infoPtr);
6741 case WM_NCACTIVATE:
6742 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
6744 case WM_NCCALCSIZE:
6745 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
6747 case WM_NCCREATE:
6748 return TOOLBAR_NCCreate (hwnd, wParam, (CREATESTRUCTW*)lParam);
6750 case WM_NCPAINT:
6751 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
6753 case WM_NOTIFY:
6754 return TOOLBAR_Notify (infoPtr, (LPNMHDR)lParam);
6756 case WM_NOTIFYFORMAT:
6757 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
6759 case WM_PRINTCLIENT:
6760 case WM_PAINT:
6761 return TOOLBAR_Paint (infoPtr, wParam);
6763 case WM_SETFOCUS:
6764 return TOOLBAR_SetFocus (infoPtr);
6766 case WM_SETFONT:
6767 return TOOLBAR_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
6769 case WM_SETREDRAW:
6770 return TOOLBAR_SetRedraw (infoPtr, wParam);
6772 case WM_SIZE:
6773 return TOOLBAR_Size (infoPtr);
6775 case WM_STYLECHANGED:
6776 return TOOLBAR_StyleChanged (infoPtr, (INT)wParam, (LPSTYLESTRUCT)lParam);
6778 case WM_SYSCOLORCHANGE:
6779 return TOOLBAR_SysColorChange ();
6781 case WM_THEMECHANGED:
6782 return theme_changed (hwnd);
6784 /* case WM_WININICHANGE: */
6786 case WM_CHARTOITEM:
6787 case WM_COMMAND:
6788 case WM_DRAWITEM:
6789 case WM_MEASUREITEM:
6790 case WM_VKEYTOITEM:
6791 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
6793 /* We see this in Outlook Express 5.x and just does DefWindowProc */
6794 case PGM_FORWARDMOUSE:
6795 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6797 default:
6798 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
6799 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
6800 uMsg, wParam, lParam);
6801 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
6806 VOID
6807 TOOLBAR_Register (void)
6809 WNDCLASSW wndClass;
6811 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
6812 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
6813 wndClass.lpfnWndProc = ToolbarWindowProc;
6814 wndClass.cbClsExtra = 0;
6815 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
6816 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
6817 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
6818 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
6820 RegisterClassW (&wndClass);
6824 VOID
6825 TOOLBAR_Unregister (void)
6827 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
6830 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
6832 HIMAGELIST himlold;
6833 PIMLENTRY c = NULL;
6835 /* Check if the entry already exists */
6836 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
6838 /* If this is a new entry we must create it and insert into the array */
6839 if (!c)
6841 PIMLENTRY *pnies;
6843 c = Alloc(sizeof(IMLENTRY));
6844 c->id = id;
6846 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
6847 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
6848 pnies[*cies] = c;
6849 (*cies)++;
6851 Free(*pies);
6852 *pies = pnies;
6855 himlold = c->himl;
6856 c->himl = himl;
6858 return himlold;
6862 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
6864 int i;
6866 for (i = 0; i < *cies; i++)
6867 Free((*pies)[i]);
6869 Free(*pies);
6871 *cies = 0;
6872 *pies = NULL;
6876 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
6878 PIMLENTRY c = NULL;
6880 if (pies != NULL)
6882 int i;
6884 for (i = 0; i < cies; i++)
6886 if (pies[i]->id == id)
6888 c = pies[i];
6889 break;
6894 return c;
6898 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
6900 HIMAGELIST himlDef = 0;
6901 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
6903 if (pie)
6904 himlDef = pie->himl;
6906 return himlDef;
6910 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
6912 if (infoPtr->bUnicode)
6913 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
6914 else
6916 CHAR Buffer[256];
6917 NMTOOLBARA nmtba;
6918 BOOL bRet = FALSE;
6920 nmtba.iItem = nmtb->iItem;
6921 nmtba.pszText = Buffer;
6922 nmtba.cchText = 256;
6923 ZeroMemory(nmtba.pszText, nmtba.cchText);
6925 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
6927 int ccht = strlen(nmtba.pszText);
6928 if (ccht)
6929 MultiByteToWideChar(CP_ACP, 0, nmtba.pszText, -1,
6930 nmtb->pszText, nmtb->cchText);
6932 nmtb->tbButton = nmtba.tbButton;
6933 bRet = TRUE;
6936 return bRet;
6941 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo)
6943 NMTOOLBARW nmtb;
6945 /* MSDN states that iItem is the index of the button, rather than the
6946 * command ID as used by every other NMTOOLBAR notification */
6947 nmtb.iItem = iItem;
6948 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
6950 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);