save old text color during a call of DrawCaptionTempW
[wine/kumbayo.git] / dlls / comctl32 / toolbar.c
blob8d710f0d86bdf0acd55d03691b227453c35779cf
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_GetInfoPtr(hwnd) ((TOOLBAR_INFO *)GetWindowLongPtrW(hwnd,0))
221 #define TOOLBAR_HasText(x, y) (TOOLBAR_GetText(x, y) ? TRUE : FALSE)
222 #define TOOLBAR_HasDropDownArrows(exStyle) ((exStyle & TBSTYLE_EX_DRAWDDARROWS) ? TRUE : FALSE)
224 /* Used to find undocumented extended styles */
225 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
226 TBSTYLE_EX_UNDOC1 | \
227 TBSTYLE_EX_MIXEDBUTTONS | \
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, PCUSTOMBUTTON 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(HWND hwnd, WPARAM wParam, LPARAM lParam);
249 static void TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason);
250 static void TOOLBAR_LayoutToolbar(HWND hwnd);
251 static LRESULT TOOLBAR_AutoSize(HWND hwnd);
252 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
253 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
255 static LRESULT
256 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
258 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
260 return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
263 static LPWSTR
264 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
266 LPWSTR lpText = NULL;
268 /* NOTE: iString == -1 is undocumented */
269 if ((HIWORD(btnPtr->iString) != 0) && (btnPtr->iString != -1))
270 lpText = (LPWSTR)btnPtr->iString;
271 else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
272 lpText = infoPtr->strings[btnPtr->iString];
274 return lpText;
277 static void
278 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num, BOOL internal)
280 if (TRACE_ON(toolbar)){
281 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
282 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
283 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
284 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
285 if (internal)
286 TRACE("button %d id %d, hot=%s, row=%d, rect=(%d,%d)-(%d,%d)\n",
287 btn_num, bP->idCommand,
288 (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
289 bP->rect.left, bP->rect.top,
290 bP->rect.right, bP->rect.bottom);
295 static void
296 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
298 if (TRACE_ON(toolbar)) {
299 INT i;
301 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
302 iP->hwndSelf, line,
303 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
304 iP->nNumStrings, iP->dwStyle);
305 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
306 iP->hwndSelf, line,
307 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
308 (iP->bDoRedraw) ? "TRUE" : "FALSE");
309 for(i=0; i<iP->nNumButtons; i++) {
310 TOOLBAR_DumpButton(iP, &iP->buttons[i], i, TRUE);
316 /***********************************************************************
317 * TOOLBAR_CheckStyle
319 * This function validates that the styles set are implemented and
320 * issues FIXME's warning of possible problems. In a perfect world this
321 * function should be null.
323 static void
324 TOOLBAR_CheckStyle (HWND hwnd, DWORD dwStyle)
326 if (dwStyle & TBSTYLE_REGISTERDROP)
327 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", hwnd);
331 static INT
332 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
334 if(!IsWindow(infoPtr->hwndSelf))
335 return 0; /* we have just been destroyed */
337 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
338 nmhdr->hwndFrom = infoPtr->hwndSelf;
339 nmhdr->code = code;
341 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
342 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
344 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
347 /***********************************************************************
348 * TOOLBAR_GetBitmapIndex
350 * This function returns the bitmap index associated with a button.
351 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
352 * is issued to retrieve the index.
354 static INT
355 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
357 INT ret = btnPtr->iBitmap;
359 if (ret == I_IMAGECALLBACK)
361 /* issue TBN_GETDISPINFO */
362 NMTBDISPINFOW nmgd;
364 memset(&nmgd, 0, sizeof(nmgd));
365 nmgd.idCommand = btnPtr->idCommand;
366 nmgd.lParam = btnPtr->dwData;
367 nmgd.dwMask = TBNF_IMAGE;
368 nmgd.iImage = -1;
369 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
370 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
371 if (nmgd.dwMask & TBNF_DI_SETITEM)
372 btnPtr->iBitmap = nmgd.iImage;
373 ret = nmgd.iImage;
374 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
375 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
378 if (ret != I_IMAGENONE)
379 ret = GETIBITMAP(infoPtr, ret);
381 return ret;
385 static BOOL
386 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
388 HIMAGELIST himl;
389 INT id = GETHIMLID(infoPtr, index);
390 INT iBitmap = GETIBITMAP(infoPtr, index);
392 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
393 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
394 (index == I_IMAGECALLBACK))
395 return TRUE;
396 else
397 return FALSE;
401 static inline BOOL
402 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
404 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
405 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
409 /***********************************************************************
410 * TOOLBAR_GetImageListForDrawing
412 * This function validates the bitmap index (including I_IMAGECALLBACK
413 * functionality) and returns the corresponding image list.
415 static HIMAGELIST
416 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
417 IMAGE_LIST_TYPE imagelist, INT * index)
419 HIMAGELIST himl;
421 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
422 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
423 ERR("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
424 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
425 return NULL;
428 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
429 if ((*index == I_IMAGECALLBACK) ||
430 (*index == I_IMAGENONE)) return NULL;
431 ERR("TBN_GETDISPINFO returned invalid index %d\n",
432 *index);
433 return NULL;
436 switch(imagelist)
438 case IMAGE_LIST_DEFAULT:
439 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
440 break;
441 case IMAGE_LIST_HOT:
442 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
443 break;
444 case IMAGE_LIST_DISABLED:
445 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
446 break;
447 default:
448 himl = NULL;
449 FIXME("Shouldn't reach here\n");
452 if (!himl)
453 TRACE("no image list\n");
455 return himl;
459 static void
460 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
462 RECT myrect;
463 COLORREF oldcolor, newcolor;
465 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
466 myrect.right = myrect.left + 1;
467 myrect.top = lpRect->top + 2;
468 myrect.bottom = lpRect->bottom - 2;
470 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
471 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
472 oldcolor = SetBkColor (hdc, newcolor);
473 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
475 myrect.left = myrect.right;
476 myrect.right = myrect.left + 1;
478 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
479 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
480 SetBkColor (hdc, newcolor);
481 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
483 SetBkColor (hdc, oldcolor);
487 /***********************************************************************
488 * TOOLBAR_DrawDDFlatSeparator
490 * This function draws the separator that was flagged as BTNS_DROPDOWN.
491 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
492 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
493 * are horizontal as opposed to the vertical separators for not dropdown
494 * type.
496 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
498 static void
499 TOOLBAR_DrawDDFlatSeparator (const RECT *lpRect, HDC hdc, const TBUTTON_INFO *btnPtr,
500 const TOOLBAR_INFO *infoPtr)
502 RECT myrect;
503 COLORREF oldcolor, newcolor;
505 myrect.left = lpRect->left;
506 myrect.right = lpRect->right;
507 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
508 myrect.bottom = myrect.top + 1;
510 InflateRect (&myrect, -2, 0);
512 TRACE("rect=(%d,%d)-(%d,%d)\n",
513 myrect.left, myrect.top, myrect.right, myrect.bottom);
515 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
516 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
517 oldcolor = SetBkColor (hdc, newcolor);
518 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
520 myrect.top = myrect.bottom;
521 myrect.bottom = myrect.top + 1;
523 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
524 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
525 SetBkColor (hdc, newcolor);
526 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
528 SetBkColor (hdc, oldcolor);
532 static void
533 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
535 INT x, y;
536 HPEN hPen, hOldPen;
538 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
539 hOldPen = SelectObject ( hdc, hPen );
540 x = left + 2;
541 y = top;
542 MoveToEx (hdc, x, y, NULL);
543 LineTo (hdc, x+5, y++); x++;
544 MoveToEx (hdc, x, y, NULL);
545 LineTo (hdc, x+3, y++); x++;
546 MoveToEx (hdc, x, y, NULL);
547 LineTo (hdc, x+1, y++);
548 SelectObject( hdc, hOldPen );
549 DeleteObject( hPen );
553 * Draw the text string for this button.
554 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
555 * is non-zero, so we can simply check himlDef to see if we have
556 * an image list
558 static void
559 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
560 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
562 HDC hdc = tbcd->nmcd.hdc;
563 HFONT hOldFont = 0;
564 COLORREF clrOld = 0;
565 COLORREF clrOldBk = 0;
566 int oldBkMode = 0;
567 UINT state = tbcd->nmcd.uItemState;
569 /* draw text */
570 if (lpText) {
571 TRACE("string=%s rect=(%d,%d)-(%d,%d)\n", debugstr_w(lpText),
572 rcText->left, rcText->top, rcText->right, rcText->bottom);
574 hOldFont = SelectObject (hdc, infoPtr->hFont);
575 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
576 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
578 else if (state & CDIS_DISABLED) {
579 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
580 OffsetRect (rcText, 1, 1);
581 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
582 SetTextColor (hdc, comctl32_color.clr3dShadow);
583 OffsetRect (rcText, -1, -1);
585 else if (state & CDIS_INDETERMINATE) {
586 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
588 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
589 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
590 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
591 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
593 else {
594 clrOld = SetTextColor (hdc, tbcd->clrText);
597 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
598 SetTextColor (hdc, clrOld);
599 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
601 SetBkColor (hdc, clrOldBk);
602 SetBkMode (hdc, oldBkMode);
604 SelectObject (hdc, hOldFont);
609 static void
610 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
612 HDC hdc = tbcd->nmcd.hdc;
613 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
614 COLORREF clrTextOld;
615 COLORREF clrBkOld;
616 INT cx = lpRect->right - lpRect->left;
617 INT cy = lpRect->bottom - lpRect->top;
618 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
619 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
620 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
621 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
622 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
623 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
624 SetBkColor(hdc, clrBkOld);
625 SetTextColor(hdc, clrTextOld);
626 SelectObject (hdc, hbr);
630 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
632 INT cx, cy;
633 HBITMAP hbmMask, hbmImage;
634 HDC hdcMask, hdcImage;
636 ImageList_GetIconSize(himl, &cx, &cy);
638 /* Create src image */
639 hdcImage = CreateCompatibleDC(hdc);
640 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
641 SelectObject(hdcImage, hbmImage);
642 ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
643 RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
645 /* Create Mask */
646 hdcMask = CreateCompatibleDC(0);
647 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
648 SelectObject(hdcMask, hbmMask);
650 /* Remove the background and all white pixels */
651 ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
652 RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
653 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
654 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
656 /* draw the new mask 'etched' to hdc */
657 SetBkColor(hdc, RGB(255, 255, 255));
658 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
659 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
660 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
661 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
662 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
664 /* Cleanup */
665 DeleteObject(hbmImage);
666 DeleteDC(hdcImage);
667 DeleteObject (hbmMask);
668 DeleteDC(hdcMask);
672 static UINT
673 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
675 UINT retstate = 0;
677 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
678 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
679 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
680 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
681 retstate |= (btnPtr->bHot ) ? CDIS_HOT : 0;
682 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
683 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
684 return retstate;
687 /* draws the image on a toolbar button */
688 static void
689 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
690 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
692 HIMAGELIST himl = NULL;
693 BOOL draw_masked = FALSE;
694 INT index;
695 INT offset = 0;
696 UINT draw_flags = ILD_TRANSPARENT;
698 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
700 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
701 if (!himl)
703 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
704 draw_masked = TRUE;
707 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
708 ((tbcd->nmcd.uItemState & CDIS_HOT)
709 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
711 /* if hot, attempt to draw with hot image list, if fails,
712 use default image list */
713 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
714 if (!himl)
715 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
717 else
718 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
720 if (!himl)
721 return;
723 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
724 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
725 offset = 1;
727 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
728 (tbcd->nmcd.uItemState & CDIS_MARKED))
729 draw_flags |= ILD_BLEND50;
731 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
732 index, himl, left, top, offset);
734 if (draw_masked)
735 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
736 else
737 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
740 /* draws a blank frame for a toolbar button */
741 static void
742 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
744 HDC hdc = tbcd->nmcd.hdc;
745 RECT rc = tbcd->nmcd.rc;
746 /* if the state is disabled or indeterminate then the button
747 * cannot have an interactive look like pressed or hot */
748 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
749 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
750 BOOL pressed_look = !non_interactive_state &&
751 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
752 (tbcd->nmcd.uItemState & CDIS_CHECKED));
754 /* app don't want us to draw any edges */
755 if (dwItemCDFlag & TBCDRF_NOEDGES)
756 return;
758 if (infoPtr->dwStyle & TBSTYLE_FLAT)
760 if (pressed_look)
761 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
762 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
763 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
765 else
767 if (pressed_look)
768 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
769 else
770 DrawEdge (hdc, &rc, EDGE_RAISED,
771 BF_SOFT | BF_RECT | BF_MIDDLE);
775 static void
776 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
778 HDC hdc = tbcd->nmcd.hdc;
779 int offset = 0;
780 BOOL pressed = bDropDownPressed ||
781 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
783 if (infoPtr->dwStyle & TBSTYLE_FLAT)
785 if (pressed)
786 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
787 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
788 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
789 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
790 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
792 else
794 if (pressed)
795 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
796 else
797 DrawEdge (hdc, rcArrow, EDGE_RAISED,
798 BF_SOFT | BF_RECT | BF_MIDDLE);
801 if (pressed)
802 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
804 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
806 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
807 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
809 else
810 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
813 /* draws a complete toolbar button */
814 static void
815 TOOLBAR_DrawButton (HWND hwnd, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
817 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
818 DWORD dwStyle = infoPtr->dwStyle;
819 BOOL hasDropDownArrow = (TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) &&
820 (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
821 (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
822 BOOL drawSepDropDownArrow = hasDropDownArrow &&
823 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
824 RECT rc, rcArrow, rcBitmap, rcText;
825 LPWSTR lpText = NULL;
826 NMTBCUSTOMDRAW tbcd;
827 DWORD ntfret;
828 INT offset;
829 INT oldBkMode;
830 DWORD dwItemCustDraw;
831 DWORD dwItemCDFlag;
832 HTHEME theme = GetWindowTheme (hwnd);
834 rc = btnPtr->rect;
835 CopyRect (&rcArrow, &rc);
837 /* separator - doesn't send NM_CUSTOMDRAW */
838 if (btnPtr->fsStyle & BTNS_SEP) {
839 if (theme)
841 DrawThemeBackground (theme, hdc,
842 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
843 &rc, NULL);
845 else
846 /* with the FLAT style, iBitmap is the width and has already */
847 /* been taken into consideration in calculating the width */
848 /* so now we need to draw the vertical separator */
849 /* empirical tests show that iBitmap can/will be non-zero */
850 /* when drawing the vertical bar... */
851 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
852 if (btnPtr->fsStyle & BTNS_DROPDOWN)
853 TOOLBAR_DrawDDFlatSeparator (&rc, hdc, btnPtr, infoPtr);
854 else
855 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
857 else if (btnPtr->fsStyle != BTNS_SEP) {
858 FIXME("Draw some kind of separator: fsStyle=%x\n",
859 btnPtr->fsStyle);
861 return;
864 /* get a pointer to the text */
865 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
867 if (hasDropDownArrow)
869 int right;
871 if (dwStyle & TBSTYLE_FLAT)
872 right = max(rc.left, rc.right - DDARROW_WIDTH);
873 else
874 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
876 if (drawSepDropDownArrow)
877 rc.right = right;
879 rcArrow.left = right;
882 /* copy text & bitmap rects after adjusting for drop-down arrow
883 * so that text & bitmap is centred in the rectangle not containing
884 * the arrow */
885 CopyRect(&rcText, &rc);
886 CopyRect(&rcBitmap, &rc);
888 /* Center the bitmap horizontally and vertically */
889 if (dwStyle & TBSTYLE_LIST)
891 if (lpText &&
892 infoPtr->nMaxTextRows > 0 &&
893 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
894 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
895 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
896 else
897 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
899 else
900 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
902 rcBitmap.top += infoPtr->szPadding.cy / 2;
904 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
905 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
906 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
907 TRACE("Text=%s\n", debugstr_w(lpText));
908 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
910 /* calculate text position */
911 if (lpText)
913 rcText.left += GetSystemMetrics(SM_CXEDGE);
914 rcText.right -= GetSystemMetrics(SM_CXEDGE);
915 if (dwStyle & TBSTYLE_LIST)
917 if (TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap))
918 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
920 else
922 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
923 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
924 else
925 rcText.top += infoPtr->szPadding.cy/2 + 2;
929 /* Initialize fields in all cases, because we use these later
930 * NOTE: applications can and do alter these to customize their
931 * toolbars */
932 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
933 tbcd.clrText = comctl32_color.clrBtnText;
934 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
935 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
936 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
937 tbcd.clrMark = comctl32_color.clrHighlight;
938 tbcd.clrHighlightHotTrack = 0;
939 tbcd.nStringBkMode = TRANSPARENT;
940 tbcd.nHLStringBkMode = OPAQUE;
941 /* MSDN says that this is the text rectangle.
942 * But (why always a but) tracing of v5.7 of native shows
943 * that this is really a *relative* rectangle based on the
944 * the nmcd.rc. Also the left and top are always 0 ignoring
945 * any bitmap that might be present. */
946 tbcd.rcText.left = 0;
947 tbcd.rcText.top = 0;
948 tbcd.rcText.right = rcText.right - rc.left;
949 tbcd.rcText.bottom = rcText.bottom - rc.top;
950 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
951 tbcd.nmcd.hdc = hdc;
952 tbcd.nmcd.rc = rc;
953 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
955 /* FIXME: what are these used for? */
956 tbcd.hbrLines = 0;
957 tbcd.hpenLines = 0;
959 /* Issue Item Prepaint notify */
960 dwItemCustDraw = 0;
961 dwItemCDFlag = 0;
962 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
964 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
965 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
966 tbcd.nmcd.lItemlParam = btnPtr->dwData;
967 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
968 /* reset these fields so the user can't alter the behaviour like native */
969 tbcd.nmcd.hdc = hdc;
970 tbcd.nmcd.rc = rc;
972 dwItemCustDraw = ntfret & 0xffff;
973 dwItemCDFlag = ntfret & 0xffff0000;
974 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
975 return;
976 /* save the only part of the rect that the user can change */
977 rcText.right = tbcd.rcText.right + rc.left;
978 rcText.bottom = tbcd.rcText.bottom + rc.top;
981 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
982 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
983 OffsetRect(&rcText, 1, 1);
985 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
986 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
987 TOOLBAR_DrawPattern (&rc, &tbcd);
989 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
990 && (tbcd.nmcd.uItemState & CDIS_HOT))
992 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
994 COLORREF oldclr;
996 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
997 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
998 if (hasDropDownArrow)
999 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1000 SetBkColor(hdc, oldclr);
1004 if (theme)
1006 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1007 int stateId = TS_NORMAL;
1009 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1010 stateId = TS_DISABLED;
1011 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1012 stateId = TS_PRESSED;
1013 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1014 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1015 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1016 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1017 stateId = TS_HOT;
1019 DrawThemeBackground (theme, hdc, partId, stateId, &tbcd.nmcd.rc, NULL);
1021 else
1022 TOOLBAR_DrawFrame(infoPtr, &tbcd, dwItemCDFlag);
1024 if (drawSepDropDownArrow)
1026 if (theme)
1028 int stateId = TS_NORMAL;
1030 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1031 stateId = TS_DISABLED;
1032 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1033 stateId = TS_PRESSED;
1034 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1035 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1036 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1037 stateId = TS_HOT;
1039 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1040 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1042 else
1043 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1046 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1047 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1048 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1049 SetBkMode (hdc, oldBkMode);
1051 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1053 if (hasDropDownArrow && !drawSepDropDownArrow)
1055 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1057 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1058 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1060 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1062 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1063 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1065 else
1066 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1069 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1071 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1072 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1078 static void
1079 TOOLBAR_Refresh (HWND hwnd, HDC hdc, const PAINTSTRUCT *ps)
1081 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1082 TBUTTON_INFO *btnPtr;
1083 INT i;
1084 RECT rcTemp, rcClient;
1085 NMTBCUSTOMDRAW tbcd;
1086 DWORD ntfret;
1087 DWORD dwBaseCustDraw;
1089 /* the app has told us not to redraw the toolbar */
1090 if (!infoPtr->bDoRedraw)
1091 return;
1093 /* if imagelist belongs to the app, it can be changed
1094 by the app after setting it */
1095 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1097 infoPtr->nNumBitmaps = 0;
1098 for (i = 0; i < infoPtr->cimlDef; i++)
1099 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1102 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1104 /* change the imagelist icon size if we manage the list and it is necessary */
1105 TOOLBAR_CheckImageListIconSize(infoPtr);
1107 /* Send initial notify */
1108 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1109 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1110 tbcd.nmcd.hdc = hdc;
1111 tbcd.nmcd.rc = ps->rcPaint;
1112 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1113 dwBaseCustDraw = ntfret & 0xffff;
1115 GetClientRect(hwnd, &rcClient);
1117 /* redraw necessary buttons */
1118 btnPtr = infoPtr->buttons;
1119 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1121 BOOL bDraw;
1122 if (!RectVisible(hdc, &btnPtr->rect))
1123 continue;
1124 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1126 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1127 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1129 else
1130 bDraw = TRUE;
1131 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1132 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1133 if (bDraw)
1134 TOOLBAR_DrawButton(hwnd, btnPtr, hdc, dwBaseCustDraw);
1137 /* draw insert mark if required */
1138 if (infoPtr->tbim.iButton != -1)
1140 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1141 RECT rcInsertMark;
1142 rcInsertMark.top = rcButton.top;
1143 rcInsertMark.bottom = rcButton.bottom;
1144 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1145 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1146 else
1147 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1148 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1151 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1153 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1154 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1155 tbcd.nmcd.hdc = hdc;
1156 tbcd.nmcd.rc = ps->rcPaint;
1157 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1161 /***********************************************************************
1162 * TOOLBAR_MeasureString
1164 * This function gets the width and height of a string in pixels. This
1165 * is done first by using GetTextExtentPoint to get the basic width
1166 * and height. The DrawText is called with DT_CALCRECT to get the exact
1167 * width. The reason is because the text may have more than one "&" (or
1168 * prefix characters as M$ likes to call them). The prefix character
1169 * indicates where the underline goes, except for the string "&&" which
1170 * is reduced to a single "&". GetTextExtentPoint does not process these
1171 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1173 static void
1174 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1175 HDC hdc, LPSIZE lpSize)
1177 RECT myrect;
1179 lpSize->cx = 0;
1180 lpSize->cy = 0;
1182 if (infoPtr->nMaxTextRows > 0 &&
1183 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1184 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1185 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1187 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1189 if(lpText != NULL) {
1190 /* first get size of all the text */
1191 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1193 /* feed above size into the rectangle for DrawText */
1194 myrect.left = myrect.top = 0;
1195 myrect.right = lpSize->cx;
1196 myrect.bottom = lpSize->cy;
1198 /* Use DrawText to get true size as drawn (less pesky "&") */
1199 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1200 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1201 DT_NOPREFIX : 0));
1203 /* feed back to caller */
1204 lpSize->cx = myrect.right;
1205 lpSize->cy = myrect.bottom;
1209 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1212 /***********************************************************************
1213 * TOOLBAR_CalcStrings
1215 * This function walks through each string and measures it and returns
1216 * the largest height and width to caller.
1218 static void
1219 TOOLBAR_CalcStrings (HWND hwnd, LPSIZE lpSize)
1221 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1222 TBUTTON_INFO *btnPtr;
1223 INT i;
1224 SIZE sz;
1225 HDC hdc;
1226 HFONT hOldFont;
1228 lpSize->cx = 0;
1229 lpSize->cy = 0;
1231 if (infoPtr->nMaxTextRows == 0)
1232 return;
1234 hdc = GetDC (hwnd);
1235 hOldFont = SelectObject (hdc, infoPtr->hFont);
1237 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1239 TEXTMETRICW tm;
1241 GetTextMetricsW(hdc, &tm);
1242 lpSize->cy = tm.tmHeight;
1245 btnPtr = infoPtr->buttons;
1246 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1247 if(TOOLBAR_HasText(infoPtr, btnPtr))
1249 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1250 if (sz.cx > lpSize->cx)
1251 lpSize->cx = sz.cx;
1252 if (sz.cy > lpSize->cy)
1253 lpSize->cy = sz.cy;
1257 SelectObject (hdc, hOldFont);
1258 ReleaseDC (hwnd, hdc);
1260 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1263 /***********************************************************************
1264 * TOOLBAR_WrapToolbar
1266 * This function walks through the buttons and separators in the
1267 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1268 * wrapping should occur based on the width of the toolbar window.
1269 * It does *not* calculate button placement itself. That task
1270 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1271 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1272 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1274 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_UNDOC1 can be used also to allow
1275 * vertical toolbar lists.
1278 static void
1279 TOOLBAR_WrapToolbar( HWND hwnd, DWORD dwStyle )
1281 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1282 TBUTTON_INFO *btnPtr;
1283 INT x, cx, i, j;
1284 RECT rc;
1285 BOOL bWrap, bButtonWrap;
1287 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1288 /* no layout is necessary. Applications may use this style */
1289 /* to perform their own layout on the toolbar. */
1290 if( !(dwStyle & TBSTYLE_WRAPABLE) &&
1291 !(infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1) ) return;
1293 btnPtr = infoPtr->buttons;
1294 x = infoPtr->nIndent;
1296 if (GetParent(hwnd))
1298 /* this can get the parents width, to know how far we can extend
1299 * this toolbar. We cannot use its height, as there may be multiple
1300 * toolbars in a rebar control
1302 GetClientRect( GetParent(hwnd), &rc );
1303 infoPtr->nWidth = rc.right - rc.left;
1305 else
1307 GetWindowRect( hwnd, &rc );
1308 infoPtr->nWidth = rc.right - rc.left;
1311 bButtonWrap = FALSE;
1313 TRACE("start ButtonWidth=%d, BitmapWidth=%d, nWidth=%d, nIndent=%d\n",
1314 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, infoPtr->nWidth,
1315 infoPtr->nIndent);
1317 for (i = 0; i < infoPtr->nNumButtons; i++ )
1319 bWrap = FALSE;
1320 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1322 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1323 continue;
1325 /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1326 /* it is the actual width of the separator. This is used for */
1327 /* custom controls in toolbars. */
1328 /* */
1329 /* BTNS_DROPDOWN separators are treated as buttons for */
1330 /* width. - GA 8/01 */
1331 if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1332 !(btnPtr[i].fsStyle & BTNS_DROPDOWN))
1333 cx = (btnPtr[i].iBitmap > 0) ?
1334 btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1335 else
1336 cx = infoPtr->nButtonWidth;
1338 /* Two or more adjacent separators form a separator group. */
1339 /* The first separator in a group should be wrapped to the */
1340 /* next row if the previous wrapping is on a button. */
1341 if( bButtonWrap &&
1342 (btnPtr[i].fsStyle & BTNS_SEP) &&
1343 (i + 1 < infoPtr->nNumButtons ) &&
1344 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1346 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1347 btnPtr[i].fsState |= TBSTATE_WRAP;
1348 x = infoPtr->nIndent;
1349 i++;
1350 bButtonWrap = FALSE;
1351 continue;
1354 /* The layout makes sure the bitmap is visible, but not the button. */
1355 /* Test added to also wrap after a button that starts a row but */
1356 /* is bigger than the area. - GA 8/01 */
1357 if (( x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2
1358 > infoPtr->nWidth ) ||
1359 ((x == infoPtr->nIndent) && (cx > infoPtr->nWidth)))
1361 BOOL bFound = FALSE;
1363 /* If the current button is a separator and not hidden, */
1364 /* go to the next until it reaches a non separator. */
1365 /* Wrap the last separator if it is before a button. */
1366 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1367 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1368 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1369 i < infoPtr->nNumButtons )
1371 i++;
1372 bFound = TRUE;
1375 if( bFound && i < infoPtr->nNumButtons )
1377 i--;
1378 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1379 i, btnPtr[i].fsStyle, x, cx);
1380 btnPtr[i].fsState |= TBSTATE_WRAP;
1381 x = infoPtr->nIndent;
1382 bButtonWrap = FALSE;
1383 continue;
1385 else if ( i >= infoPtr->nNumButtons)
1386 break;
1388 /* If the current button is not a separator, find the last */
1389 /* separator and wrap it. */
1390 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1392 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1393 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1395 bFound = TRUE;
1396 i = j;
1397 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1398 i, btnPtr[i].fsStyle, x, cx);
1399 x = infoPtr->nIndent;
1400 btnPtr[j].fsState |= TBSTATE_WRAP;
1401 bButtonWrap = FALSE;
1402 break;
1406 /* If no separator available for wrapping, wrap one of */
1407 /* non-hidden previous button. */
1408 if (!bFound)
1410 for ( j = i - 1;
1411 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1413 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1414 continue;
1416 bFound = TRUE;
1417 i = j;
1418 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1419 i, btnPtr[i].fsStyle, x, cx);
1420 x = infoPtr->nIndent;
1421 btnPtr[j].fsState |= TBSTATE_WRAP;
1422 bButtonWrap = TRUE;
1423 break;
1427 /* If all above failed, wrap the current button. */
1428 if (!bFound)
1430 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1431 i, btnPtr[i].fsStyle, x, cx);
1432 btnPtr[i].fsState |= TBSTATE_WRAP;
1433 bFound = TRUE;
1434 x = infoPtr->nIndent;
1435 if (btnPtr[i].fsStyle & BTNS_SEP )
1436 bButtonWrap = FALSE;
1437 else
1438 bButtonWrap = TRUE;
1441 else {
1442 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1443 i, btnPtr[i].fsStyle, x, cx);
1444 x += cx;
1450 /***********************************************************************
1451 * TOOLBAR_MeasureButton
1453 * Calculates the width and height required for a button. Used in
1454 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1455 * the width of buttons that are autosized.
1457 * Note that it would have been rather elegant to use one piece of code for
1458 * both the laying out of the toolbar and for controlling where button parts
1459 * are drawn, but the native control has inconsistencies between the two that
1460 * prevent this from being effectively. These inconsistencies can be seen as
1461 * artefacts where parts of the button appear outside of the bounding button
1462 * rectangle.
1464 * There are several cases for the calculation of the button dimensions and
1465 * button part positioning:
1467 * List
1468 * ====
1470 * With Bitmap:
1472 * +--------------------------------------------------------+ ^
1473 * | ^ ^ | |
1474 * | | pad.cy / 2 | centred | |
1475 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1476 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1477 * | |<------------>| | | | |
1478 * | +--------------+ +------------+ | |
1479 * |<-------------------------------------->| | |
1480 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1481 * | szText.cx | |
1482 * +--------------------------------------------------------+ -
1483 * <-------------------------------------------------------->
1484 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1486 * Without Bitmap (I_IMAGENONE):
1488 * +-----------------------------------+ ^
1489 * | ^ | |
1490 * | | centred | | LISTPAD_CY +
1491 * | +------------+ | | szText.cy
1492 * | | Text | | |
1493 * | | | | |
1494 * | +------------+ | |
1495 * |<----------------->| | |
1496 * | cxedge |<-----------> | |
1497 * | szText.cx | |
1498 * +-----------------------------------+ -
1499 * <----------------------------------->
1500 * szText.cx + pad.cx
1502 * Without text:
1504 * +--------------------------------------+ ^
1505 * | ^ | |
1506 * | | padding.cy/2 | | DEFPAD_CY +
1507 * | +------------+ | | nBitmapHeight
1508 * | | Bitmap | | |
1509 * | | | | |
1510 * | +------------+ | |
1511 * |<------------------->| | |
1512 * | cxedge + iListGap/2 |<-----------> | |
1513 * | nBitmapWidth | |
1514 * +--------------------------------------+ -
1515 * <-------------------------------------->
1516 * 2*cxedge + nBitmapWidth + iListGap
1518 * Non-List
1519 * ========
1521 * With bitmap:
1523 * +-----------------------------------+ ^
1524 * | ^ | |
1525 * | | pad.cy / 2 | | nBitmapHeight +
1526 * | - | | szText.cy +
1527 * | +------------+ | | DEFPAD_CY + 1
1528 * | centred | Bitmap | | |
1529 * |<----------------->| | | |
1530 * | +------------+ | |
1531 * | ^ | |
1532 * | 1 | | |
1533 * | - | |
1534 * | centred +---------------+ | |
1535 * |<--------------->| Text | | |
1536 * | +---------------+ | |
1537 * +-----------------------------------+ -
1538 * <----------------------------------->
1539 * pad.cx + max(nBitmapWidth, szText.cx)
1541 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1543 * +---------------------------------------+ ^
1544 * | ^ | |
1545 * | | 2 + pad.cy / 2 | |
1546 * | - | | szText.cy +
1547 * | centred +-----------------+ | | pad.cy + 2
1548 * |<--------------->| Text | | |
1549 * | +-----------------+ | |
1550 * | | |
1551 * +---------------------------------------+ -
1552 * <--------------------------------------->
1553 * 2*cxedge + pad.cx + szText.cx
1555 * Without text:
1556 * As for with bitmaps, but with szText.cx zero.
1558 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1559 BOOL bHasBitmap, BOOL bValidImageList)
1561 SIZE sizeButton;
1562 if (infoPtr->dwStyle & TBSTYLE_LIST)
1564 /* set button height from bitmap / text height... */
1565 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1566 sizeString.cy);
1568 /* ... add on the necessary padding */
1569 if (bValidImageList)
1571 if (bHasBitmap)
1572 sizeButton.cy += DEFPAD_CY;
1573 else
1574 sizeButton.cy += LISTPAD_CY;
1576 else
1577 sizeButton.cy += infoPtr->szPadding.cy;
1579 /* calculate button width */
1580 if (bHasBitmap)
1582 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1583 infoPtr->nBitmapWidth + infoPtr->iListGap;
1584 if (sizeString.cx > 0)
1585 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1587 else
1588 sizeButton.cx = sizeString.cx + infoPtr->szPadding.cx;
1590 else
1592 if (bHasBitmap)
1594 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1595 if (sizeString.cy > 0)
1596 sizeButton.cy += 1 + sizeString.cy;
1597 sizeButton.cx = infoPtr->szPadding.cx +
1598 max(sizeString.cx, infoPtr->nBitmapWidth);
1600 else
1602 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1603 NONLIST_NOTEXT_OFFSET;
1604 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1605 infoPtr->szPadding.cx + sizeString.cx;
1608 return sizeButton;
1612 /***********************************************************************
1613 * TOOLBAR_CalcToolbar
1615 * This function calculates button and separator placement. It first
1616 * calculates the button sizes, gets the toolbar window width and then
1617 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1618 * on. It assigns a new location to each item and sends this location to
1619 * the tooltip window if appropriate. Finally, it updates the rcBound
1620 * rect and calculates the new required toolbar window height.
1622 static void
1623 TOOLBAR_CalcToolbar (HWND hwnd)
1625 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1626 SIZE sizeString, sizeButton;
1627 BOOL validImageList = FALSE;
1629 TOOLBAR_CalcStrings (hwnd, &sizeString);
1631 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1633 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1634 validImageList = TRUE;
1635 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1636 infoPtr->nButtonWidth = sizeButton.cx;
1637 infoPtr->nButtonHeight = sizeButton.cy;
1638 infoPtr->iTopMargin = default_top_margin(infoPtr);
1640 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1641 infoPtr->nButtonWidth = infoPtr->cxMin;
1642 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1643 infoPtr->nButtonWidth = infoPtr->cxMax;
1645 TOOLBAR_LayoutToolbar(hwnd);
1648 static void
1649 TOOLBAR_LayoutToolbar(HWND hwnd)
1651 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
1652 TBUTTON_INFO *btnPtr;
1653 SIZE sizeButton;
1654 INT i, nRows, nSepRows;
1655 INT x, y, cx, cy;
1656 BOOL bWrap;
1657 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1658 BOOL hasDropDownArrows = TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle);
1660 TOOLBAR_WrapToolbar(hwnd, infoPtr->dwStyle);
1662 x = infoPtr->nIndent;
1663 y = infoPtr->iTopMargin;
1664 cx = infoPtr->nButtonWidth;
1665 cy = infoPtr->nButtonHeight;
1667 nRows = nSepRows = 0;
1669 infoPtr->rcBound.top = y;
1670 infoPtr->rcBound.left = x;
1671 infoPtr->rcBound.bottom = y + cy;
1672 infoPtr->rcBound.right = x;
1674 btnPtr = infoPtr->buttons;
1676 TRACE("cy=%d\n", cy);
1678 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1680 bWrap = FALSE;
1681 if (btnPtr->fsState & TBSTATE_HIDDEN)
1683 SetRectEmpty (&btnPtr->rect);
1684 continue;
1687 cy = infoPtr->nButtonHeight;
1689 /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1690 /* it is the actual width of the separator. This is used for */
1691 /* custom controls in toolbars. */
1692 if (btnPtr->fsStyle & BTNS_SEP) {
1693 if (btnPtr->fsStyle & BTNS_DROPDOWN) {
1694 cy = (btnPtr->iBitmap > 0) ?
1695 btnPtr->iBitmap : SEPARATOR_WIDTH;
1696 cx = infoPtr->nButtonWidth;
1698 else
1699 cx = (btnPtr->iBitmap > 0) ?
1700 btnPtr->iBitmap : SEPARATOR_WIDTH;
1702 else
1704 if (btnPtr->cx)
1705 cx = btnPtr->cx;
1706 else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1707 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1709 SIZE sz;
1710 HDC hdc;
1711 HFONT hOldFont;
1713 hdc = GetDC (hwnd);
1714 hOldFont = SelectObject (hdc, infoPtr->hFont);
1716 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1718 SelectObject (hdc, hOldFont);
1719 ReleaseDC (hwnd, hdc);
1721 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1722 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1723 validImageList);
1724 cx = sizeButton.cx;
1726 else
1727 cx = infoPtr->nButtonWidth;
1729 /* if size has been set manually then don't add on extra space
1730 * for the drop down arrow */
1731 if (!btnPtr->cx && hasDropDownArrows &&
1732 ((btnPtr->fsStyle & BTNS_DROPDOWN) || (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)))
1733 cx += DDARROW_WIDTH;
1735 if (btnPtr->fsState & TBSTATE_WRAP )
1736 bWrap = TRUE;
1738 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1740 if (infoPtr->rcBound.left > x)
1741 infoPtr->rcBound.left = x;
1742 if (infoPtr->rcBound.right < x + cx)
1743 infoPtr->rcBound.right = x + cx;
1744 if (infoPtr->rcBound.bottom < y + cy)
1745 infoPtr->rcBound.bottom = y + cy;
1747 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1749 /* btnPtr->nRow is zero based. The space between the rows is */
1750 /* also considered as a row. */
1751 btnPtr->nRow = nRows + nSepRows;
1753 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1754 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1755 x, y, x+cx, y+cy);
1757 if( bWrap )
1759 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1760 y += cy;
1761 else
1763 /* UNDOCUMENTED: If a separator has a non zero bitmap index, */
1764 /* it is the actual width of the separator. This is used for */
1765 /* custom controls in toolbars. */
1766 if ( !(btnPtr->fsStyle & BTNS_DROPDOWN))
1767 y += cy + ( (btnPtr->iBitmap > 0 ) ?
1768 btnPtr->iBitmap : SEPARATOR_WIDTH) * 2 /3;
1769 else
1770 y += cy;
1772 /* nSepRows is used to calculate the extra height follwoing */
1773 /* the last row. */
1774 nSepRows++;
1776 x = infoPtr->nIndent;
1778 /* Increment row number unless this is the last button */
1779 /* and it has Wrap set. */
1780 if (i != infoPtr->nNumButtons-1)
1781 nRows++;
1783 else
1784 x += cx;
1787 /* infoPtr->nRows is the number of rows on the toolbar */
1788 infoPtr->nRows = nRows + nSepRows + 1;
1790 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1794 static INT
1795 TOOLBAR_InternalHitTest (HWND hwnd, const POINT *lpPt)
1797 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
1798 TBUTTON_INFO *btnPtr;
1799 INT i;
1801 btnPtr = infoPtr->buttons;
1802 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1803 if (btnPtr->fsState & TBSTATE_HIDDEN)
1804 continue;
1806 if (btnPtr->fsStyle & BTNS_SEP) {
1807 if (PtInRect (&btnPtr->rect, *lpPt)) {
1808 TRACE(" ON SEPARATOR %d!\n", i);
1809 return -i;
1812 else {
1813 if (PtInRect (&btnPtr->rect, *lpPt)) {
1814 TRACE(" ON BUTTON %d!\n", i);
1815 return i;
1820 TRACE(" NOWHERE!\n");
1821 return TOOLBAR_NOWHERE;
1825 static INT
1826 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1828 TBUTTON_INFO *btnPtr;
1829 INT i;
1831 if (CommandIsIndex) {
1832 TRACE("command is really index command=%d\n", idCommand);
1833 if (idCommand >= infoPtr->nNumButtons) return -1;
1834 return idCommand;
1836 btnPtr = infoPtr->buttons;
1837 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1838 if (btnPtr->idCommand == idCommand) {
1839 TRACE("command=%d index=%d\n", idCommand, i);
1840 return i;
1843 TRACE("no index found for command=%d\n", idCommand);
1844 return -1;
1848 static INT
1849 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1851 TBUTTON_INFO *btnPtr;
1852 INT nRunIndex;
1854 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1855 return -1;
1857 /* check index button */
1858 btnPtr = &infoPtr->buttons[nIndex];
1859 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1860 if (btnPtr->fsState & TBSTATE_CHECKED)
1861 return nIndex;
1864 /* check previous buttons */
1865 nRunIndex = nIndex - 1;
1866 while (nRunIndex >= 0) {
1867 btnPtr = &infoPtr->buttons[nRunIndex];
1868 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1869 if (btnPtr->fsState & TBSTATE_CHECKED)
1870 return nRunIndex;
1872 else
1873 break;
1874 nRunIndex--;
1877 /* check next buttons */
1878 nRunIndex = nIndex + 1;
1879 while (nRunIndex < infoPtr->nNumButtons) {
1880 btnPtr = &infoPtr->buttons[nRunIndex];
1881 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
1882 if (btnPtr->fsState & TBSTATE_CHECKED)
1883 return nRunIndex;
1885 else
1886 break;
1887 nRunIndex++;
1890 return -1;
1894 static VOID
1895 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
1896 WPARAM wParam, LPARAM lParam)
1898 MSG msg;
1900 msg.hwnd = hwndMsg;
1901 msg.message = uMsg;
1902 msg.wParam = wParam;
1903 msg.lParam = lParam;
1904 msg.time = GetMessageTime ();
1905 msg.pt.x = (short)LOWORD(GetMessagePos ());
1906 msg.pt.y = (short)HIWORD(GetMessagePos ());
1908 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
1911 static void
1912 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1914 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
1915 TTTOOLINFOW ti;
1917 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
1918 ti.cbSize = sizeof (TTTOOLINFOW);
1919 ti.hwnd = infoPtr->hwndSelf;
1920 ti.uId = button->idCommand;
1921 ti.hinst = 0;
1922 ti.lpszText = LPSTR_TEXTCALLBACKW;
1923 /* ti.lParam = random value from the stack? */
1925 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
1926 0, (LPARAM)&ti);
1930 static void
1931 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1933 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
1934 TTTOOLINFOW ti;
1936 ZeroMemory(&ti, sizeof(ti));
1937 ti.cbSize = sizeof(ti);
1938 ti.hwnd = infoPtr->hwndSelf;
1939 ti.uId = button->idCommand;
1941 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
1945 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
1947 /* Set the toolTip only for non-hidden, non-separator button */
1948 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
1950 TTTOOLINFOW ti;
1952 ZeroMemory(&ti, sizeof(ti));
1953 ti.cbSize = sizeof(ti);
1954 ti.hwnd = infoPtr->hwndSelf;
1955 ti.uId = button->idCommand;
1956 ti.rect = button->rect;
1957 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
1961 /* Creates the tooltip control */
1962 static void
1963 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
1965 int i;
1966 NMTOOLTIPSCREATED nmttc;
1968 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
1969 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1970 infoPtr->hwndSelf, 0, 0, 0);
1972 if (!infoPtr->hwndToolTip)
1973 return;
1975 /* Send NM_TOOLTIPSCREATED notification */
1976 nmttc.hwndToolTips = infoPtr->hwndToolTip;
1977 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
1979 for (i = 0; i < infoPtr->nNumButtons; i++)
1981 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
1982 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
1986 /* keeps available button list box sorted by button id */
1987 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
1989 int i;
1990 int count;
1991 PCUSTOMBUTTON btnInfo;
1992 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
1994 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
1996 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
1998 /* position 0 is always separator */
1999 for (i = 1; i < count; i++)
2001 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2002 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2004 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2005 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2006 return;
2009 /* id higher than all others add to end */
2010 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2011 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2014 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2016 NMTOOLBARW nmtb;
2018 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2020 if (nIndexFrom == nIndexTo)
2021 return;
2023 /* MSDN states that iItem is the index of the button, rather than the
2024 * command ID as used by every other NMTOOLBAR notification */
2025 nmtb.iItem = nIndexFrom;
2026 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2028 PCUSTOMBUTTON btnInfo;
2029 NMHDR hdr;
2030 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2031 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2033 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2035 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2036 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2037 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2038 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2040 if (nIndexTo <= 0)
2041 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2042 else
2043 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2045 /* last item is always separator, so -2 instead of -1 */
2046 if (nIndexTo >= (count - 2))
2047 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2048 else
2049 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2051 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2052 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2054 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2058 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2060 NMTOOLBARW nmtb;
2062 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
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 = nIndexAvail;
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 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2073 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2075 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2077 if (nIndexAvail != 0) /* index == 0 indicates separator */
2079 /* remove from 'available buttons' list */
2080 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2081 if (nIndexAvail == count-1)
2082 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2083 else
2084 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2086 else
2088 PCUSTOMBUTTON btnNew;
2090 /* duplicate 'separator' button */
2091 btnNew = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2092 memcpy(btnNew, btnInfo, sizeof(CUSTOMBUTTON));
2093 btnInfo = btnNew;
2096 /* insert into 'toolbar button' list */
2097 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2098 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2100 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2102 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2106 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2108 PCUSTOMBUTTON btnInfo;
2109 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2111 TRACE("Remove: index %d\n", index);
2113 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2115 /* send TBN_QUERYDELETE notification */
2116 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2118 NMHDR hdr;
2120 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2121 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2123 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2125 /* insert into 'available button' list */
2126 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2127 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2128 else
2129 Free(btnInfo);
2131 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2135 /* drag list notification function for toolbar buttons list box */
2136 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2137 const DRAGLISTINFO *pDLI)
2139 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2140 switch (pDLI->uNotification)
2142 case DL_BEGINDRAG:
2144 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2145 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2146 /* no dragging for last item (separator) */
2147 if (nCurrentItem >= (nCount - 1)) return FALSE;
2148 return TRUE;
2150 case DL_DRAGGING:
2152 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2153 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2154 /* no dragging past last item (separator) */
2155 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2157 DrawInsert(hwnd, hwndList, nCurrentItem);
2158 /* FIXME: native uses "move button" cursor */
2159 return DL_COPYCURSOR;
2162 /* not over toolbar buttons list */
2163 if (nCurrentItem < 0)
2165 POINT ptWindow = pDLI->ptCursor;
2166 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2167 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2168 /* over available buttons list? */
2169 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2170 /* FIXME: native uses "move button" cursor */
2171 return DL_COPYCURSOR;
2173 /* clear drag arrow */
2174 DrawInsert(hwnd, hwndList, -1);
2175 return DL_STOPCURSOR;
2177 case DL_DROPPED:
2179 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2180 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2181 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2182 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2184 /* clear drag arrow */
2185 DrawInsert(hwnd, hwndList, -1);
2186 /* move item */
2187 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2189 /* not over toolbar buttons list */
2190 if (nIndexTo < 0)
2192 POINT ptWindow = pDLI->ptCursor;
2193 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2194 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2195 /* over available buttons list? */
2196 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2197 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2199 break;
2201 case DL_CANCELDRAG:
2202 /* Clear drag arrow */
2203 DrawInsert(hwnd, hwndList, -1);
2204 break;
2207 return 0;
2210 /* drag list notification function for available buttons list box */
2211 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2212 const DRAGLISTINFO *pDLI)
2214 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2215 switch (pDLI->uNotification)
2217 case DL_BEGINDRAG:
2218 return TRUE;
2219 case DL_DRAGGING:
2221 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2222 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2223 /* no dragging past last item (separator) */
2224 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2226 DrawInsert(hwnd, hwndList, nCurrentItem);
2227 /* FIXME: native uses "move button" cursor */
2228 return DL_COPYCURSOR;
2231 /* not over toolbar buttons list */
2232 if (nCurrentItem < 0)
2234 POINT ptWindow = pDLI->ptCursor;
2235 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2236 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2237 /* over available buttons list? */
2238 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2239 /* FIXME: native uses "move button" cursor */
2240 return DL_COPYCURSOR;
2242 /* clear drag arrow */
2243 DrawInsert(hwnd, hwndList, -1);
2244 return DL_STOPCURSOR;
2246 case DL_DROPPED:
2248 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2249 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2250 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2251 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2253 /* clear drag arrow */
2254 DrawInsert(hwnd, hwndList, -1);
2255 /* add item */
2256 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2259 case DL_CANCELDRAG:
2260 /* Clear drag arrow */
2261 DrawInsert(hwnd, hwndList, -1);
2262 break;
2264 return 0;
2267 extern UINT uDragListMessage;
2269 /***********************************************************************
2270 * TOOLBAR_CustomizeDialogProc
2271 * This function implements the toolbar customization dialog.
2273 static INT_PTR CALLBACK
2274 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2276 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2277 PCUSTOMBUTTON btnInfo;
2278 NMTOOLBARA nmtb;
2279 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2281 switch (uMsg)
2283 case WM_INITDIALOG:
2284 custInfo = (PCUSTDLG_INFO)lParam;
2285 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2287 if (custInfo)
2289 WCHAR Buffer[256];
2290 int i = 0;
2291 int index;
2292 NMTBINITCUSTOMIZE nmtbic;
2294 infoPtr = custInfo->tbInfo;
2296 /* send TBN_QUERYINSERT notification */
2297 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2299 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2300 return FALSE;
2302 nmtbic.hwndDialog = hwnd;
2303 /* Send TBN_INITCUSTOMIZE notification */
2304 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2305 TBNRF_HIDEHELP)
2307 TRACE("TBNRF_HIDEHELP requested\n");
2308 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2311 /* add items to 'toolbar buttons' list and check if removable */
2312 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2314 btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2315 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2316 btnInfo->btn.fsStyle = BTNS_SEP;
2317 btnInfo->bVirtual = FALSE;
2318 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2320 /* send TBN_QUERYDELETE notification */
2321 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2323 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2324 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2327 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2329 /* insert separator button into 'available buttons' list */
2330 btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2331 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2332 btnInfo->btn.fsStyle = BTNS_SEP;
2333 btnInfo->bVirtual = FALSE;
2334 btnInfo->bRemovable = TRUE;
2335 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2336 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2337 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2339 /* insert all buttons into dsa */
2340 for (i = 0;; i++)
2342 /* send TBN_GETBUTTONINFO notification */
2343 NMTOOLBARW nmtb;
2344 nmtb.iItem = i;
2345 nmtb.pszText = Buffer;
2346 nmtb.cchText = 256;
2348 /* Clear previous button's text */
2349 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2351 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2352 break;
2354 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2355 nmtb.tbButton.fsStyle, i,
2356 nmtb.tbButton.idCommand,
2357 nmtb.tbButton.iString,
2358 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2359 : "");
2361 /* insert button into the apropriate list */
2362 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2363 if (index == -1)
2365 btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2366 btnInfo->bVirtual = FALSE;
2367 btnInfo->bRemovable = TRUE;
2369 else
2371 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2372 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2375 memcpy (&btnInfo->btn, &nmtb.tbButton, sizeof(TBBUTTON));
2376 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2378 if (lstrlenW(nmtb.pszText))
2379 lstrcpyW(btnInfo->text, nmtb.pszText);
2380 else if (nmtb.tbButton.iString >= 0 &&
2381 nmtb.tbButton.iString < infoPtr->nNumStrings)
2383 lstrcpyW(btnInfo->text,
2384 infoPtr->strings[nmtb.tbButton.iString]);
2388 if (index == -1)
2389 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2392 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2394 /* select first item in the 'available' list */
2395 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2397 /* append 'virtual' separator button to the 'toolbar buttons' list */
2398 btnInfo = (PCUSTOMBUTTON)Alloc(sizeof(CUSTOMBUTTON));
2399 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2400 btnInfo->btn.fsStyle = BTNS_SEP;
2401 btnInfo->bVirtual = TRUE;
2402 btnInfo->bRemovable = FALSE;
2403 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2404 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2405 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2407 /* select last item in the 'toolbar' list */
2408 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2409 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2411 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2412 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2414 /* set focus and disable buttons */
2415 PostMessageW (hwnd, WM_USER, 0, 0);
2417 return TRUE;
2419 case WM_USER:
2420 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2421 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2422 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2423 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2424 return TRUE;
2426 case WM_CLOSE:
2427 EndDialog(hwnd, FALSE);
2428 return TRUE;
2430 case WM_COMMAND:
2431 switch (LOWORD(wParam))
2433 case IDC_TOOLBARBTN_LBOX:
2434 if (HIWORD(wParam) == LBN_SELCHANGE)
2436 PCUSTOMBUTTON btnInfo;
2437 NMTOOLBARA nmtb;
2438 int count;
2439 int index;
2441 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2442 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2444 /* send TBN_QUERYINSERT notification */
2445 nmtb.iItem = index;
2446 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2448 /* get list box item */
2449 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2451 if (index == (count - 1))
2453 /* last item (virtual separator) */
2454 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2455 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2457 else if (index == (count - 2))
2459 /* second last item (last non-virtual item) */
2460 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2461 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2463 else if (index == 0)
2465 /* first item */
2466 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2467 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2469 else
2471 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2472 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2475 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2477 break;
2479 case IDC_MOVEUP_BTN:
2481 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2482 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2484 break;
2486 case IDC_MOVEDN_BTN: /* move down */
2488 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2489 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2491 break;
2493 case IDC_REMOVE_BTN: /* remove button */
2495 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2497 if (LB_ERR == index)
2498 break;
2500 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2502 break;
2503 case IDC_HELP_BTN:
2504 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2505 break;
2506 case IDC_RESET_BTN:
2507 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2508 break;
2510 case IDOK: /* Add button */
2512 int index;
2513 int indexto;
2515 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2516 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2518 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2520 break;
2522 case IDCANCEL:
2523 EndDialog(hwnd, FALSE);
2524 break;
2526 return TRUE;
2528 case WM_DESTROY:
2530 int count;
2531 int i;
2533 /* delete items from 'toolbar buttons' listbox*/
2534 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2535 for (i = 0; i < count; i++)
2537 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2538 Free(btnInfo);
2539 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2541 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2544 /* delete items from 'available buttons' listbox*/
2545 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2546 for (i = 0; i < count; i++)
2548 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2549 Free(btnInfo);
2550 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2552 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2554 return TRUE;
2556 case WM_DRAWITEM:
2557 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2559 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2560 RECT rcButton;
2561 RECT rcText;
2562 HPEN hPen, hOldPen;
2563 HBRUSH hOldBrush;
2564 COLORREF oldText = 0;
2565 COLORREF oldBk = 0;
2567 /* get item data */
2568 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, (WPARAM)lpdis->itemID, 0);
2569 if (btnInfo == NULL)
2571 FIXME("btnInfo invalid!\n");
2572 return TRUE;
2575 /* set colors and select objects */
2576 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2577 if (btnInfo->bVirtual)
2578 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2579 else
2580 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2581 hPen = CreatePen( PS_SOLID, 1,
2582 GetSysColor( (lpdis->itemState & ODS_SELECTED)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2583 hOldPen = SelectObject (lpdis->hDC, hPen );
2584 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2586 /* fill background rectangle */
2587 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2588 lpdis->rcItem.right, lpdis->rcItem.bottom);
2590 /* calculate button and text rectangles */
2591 CopyRect (&rcButton, &lpdis->rcItem);
2592 InflateRect (&rcButton, -1, -1);
2593 CopyRect (&rcText, &rcButton);
2594 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2595 rcText.left = rcButton.right + 2;
2597 /* draw focus rectangle */
2598 if (lpdis->itemState & ODS_FOCUS)
2599 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2601 /* draw button */
2602 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2603 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2605 /* draw image and text */
2606 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2607 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2608 btnInfo->btn.iBitmap));
2609 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2610 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2612 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2613 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2615 /* delete objects and reset colors */
2616 SelectObject (lpdis->hDC, hOldBrush);
2617 SelectObject (lpdis->hDC, hOldPen);
2618 SetBkColor (lpdis->hDC, oldBk);
2619 SetTextColor (lpdis->hDC, oldText);
2620 DeleteObject( hPen );
2621 return TRUE;
2623 return FALSE;
2625 case WM_MEASUREITEM:
2626 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2628 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2630 lpmis->itemHeight = 15 + 8; /* default height */
2632 return TRUE;
2634 return FALSE;
2636 default:
2637 if (uDragListMessage && (uMsg == uDragListMessage))
2639 if (wParam == IDC_TOOLBARBTN_LBOX)
2641 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2642 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2643 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2644 return TRUE;
2646 else if (wParam == IDC_AVAILBTN_LBOX)
2648 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2649 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2650 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2651 return TRUE;
2654 return FALSE;
2658 static BOOL
2659 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2661 HBITMAP hbmLoad;
2662 INT nCountBefore = ImageList_GetImageCount(himlDef);
2663 INT nCountAfter;
2664 INT cxIcon, cyIcon;
2665 INT nAdded;
2666 INT nIndex;
2668 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2669 /* Add bitmaps to the default image list */
2670 if (bitmap->hInst == NULL) /* a handle was passed */
2671 hbmLoad = (HBITMAP)CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2672 else
2673 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2675 /* enlarge the bitmap if needed */
2676 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2677 if (bitmap->hInst != COMCTL32_hModule)
2678 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2680 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2681 DeleteObject(hbmLoad);
2682 if (nIndex == -1)
2683 return FALSE;
2685 nCountAfter = ImageList_GetImageCount(himlDef);
2686 nAdded = nCountAfter - nCountBefore;
2687 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2689 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2690 } else if (nAdded > (INT)bitmap->nButtons) {
2691 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2692 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2695 infoPtr->nNumBitmaps += nAdded;
2696 return TRUE;
2699 static void
2700 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2702 HIMAGELIST himlDef;
2703 HIMAGELIST himlNew;
2704 INT cx, cy;
2705 INT i;
2707 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2708 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2709 return;
2710 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2711 return;
2712 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2713 return;
2715 TRACE("Update icon size: %dx%d -> %dx%d\n",
2716 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2718 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2719 ILC_COLORDDB|ILC_MASK, 8, 2);
2720 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2721 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2722 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2723 infoPtr->himlInt = himlNew;
2725 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2726 ImageList_Destroy(himlDef);
2729 /***********************************************************************
2730 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2733 static LRESULT
2734 TOOLBAR_AddBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
2736 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2737 LPTBADDBITMAP lpAddBmp = (LPTBADDBITMAP)lParam;
2738 TBITMAP_INFO info;
2739 INT iSumButtons, i;
2740 HIMAGELIST himlDef;
2742 TRACE("hwnd=%p wParam=%lx lParam=%lx\n", hwnd, wParam, lParam);
2743 if (!lpAddBmp)
2744 return -1;
2746 if (lpAddBmp->hInst == HINST_COMMCTRL)
2748 info.hInst = COMCTL32_hModule;
2749 switch (lpAddBmp->nID)
2751 case IDB_STD_SMALL_COLOR:
2752 info.nButtons = 15;
2753 info.nID = IDB_STD_SMALL;
2754 break;
2755 case IDB_STD_LARGE_COLOR:
2756 info.nButtons = 15;
2757 info.nID = IDB_STD_LARGE;
2758 break;
2759 case IDB_VIEW_SMALL_COLOR:
2760 info.nButtons = 12;
2761 info.nID = IDB_VIEW_SMALL;
2762 break;
2763 case IDB_VIEW_LARGE_COLOR:
2764 info.nButtons = 12;
2765 info.nID = IDB_VIEW_LARGE;
2766 break;
2767 case IDB_HIST_SMALL_COLOR:
2768 info.nButtons = 5;
2769 info.nID = IDB_HIST_SMALL;
2770 break;
2771 case IDB_HIST_LARGE_COLOR:
2772 info.nButtons = 5;
2773 info.nID = IDB_HIST_LARGE;
2774 break;
2775 default:
2776 return -1;
2779 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2781 /* Windows resize all the buttons to the size of a newly added standard image */
2782 if (lpAddBmp->nID & 1)
2784 /* large icons: 24x24. Will make the button 31x30 */
2785 SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2787 else
2789 /* small icons: 16x16. Will make the buttons 23x22 */
2790 SendMessageW (hwnd, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2793 TOOLBAR_CalcToolbar (hwnd);
2795 else
2797 info.nButtons = (INT)wParam;
2798 info.hInst = lpAddBmp->hInst;
2799 info.nID = lpAddBmp->nID;
2800 TRACE("adding %d bitmaps!\n", info.nButtons);
2803 /* check if the bitmap is already loaded and compute iSumButtons */
2804 iSumButtons = 0;
2805 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2807 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2808 infoPtr->bitmaps[i].nID == info.nID)
2809 return iSumButtons;
2810 iSumButtons += infoPtr->bitmaps[i].nButtons;
2813 if (!infoPtr->cimlDef) {
2814 /* create new default image list */
2815 TRACE ("creating default image list!\n");
2817 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2818 ILC_COLORDDB | ILC_MASK, info.nButtons, 2);
2819 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2820 infoPtr->himlInt = himlDef;
2822 else {
2823 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2826 if (!himlDef) {
2827 WARN("No default image list available\n");
2828 return -1;
2831 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2832 return -1;
2834 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2835 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2836 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2837 infoPtr->nNumBitmapInfos++;
2838 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2840 InvalidateRect(hwnd, NULL, TRUE);
2841 return iSumButtons;
2845 static LRESULT
2846 TOOLBAR_AddButtonsT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
2848 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2849 LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
2850 INT nOldButtons, nNewButtons, nAddButtons, nCount;
2851 BOOL fHasString = FALSE;
2853 TRACE("adding %ld buttons (unicode=%d)!\n", wParam, fUnicode);
2855 nAddButtons = (UINT)wParam;
2856 nOldButtons = infoPtr->nNumButtons;
2857 nNewButtons = nOldButtons + nAddButtons;
2859 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
2860 infoPtr->nNumButtons = nNewButtons;
2862 /* insert new button data */
2863 for (nCount = 0; nCount < nAddButtons; nCount++) {
2864 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nOldButtons+nCount];
2865 btnPtr->iBitmap = lpTbb[nCount].iBitmap;
2866 btnPtr->idCommand = lpTbb[nCount].idCommand;
2867 btnPtr->fsState = lpTbb[nCount].fsState;
2868 btnPtr->fsStyle = lpTbb[nCount].fsStyle;
2869 btnPtr->dwData = lpTbb[nCount].dwData;
2870 btnPtr->bHot = FALSE;
2871 if(HIWORD(lpTbb[nCount].iString) && lpTbb[nCount].iString != -1)
2873 if (fUnicode)
2874 Str_SetPtrW ((LPWSTR*)&btnPtr->iString, (LPWSTR)lpTbb[nCount].iString );
2875 else
2876 Str_SetPtrAtoW((LPWSTR*)&btnPtr->iString, (LPSTR)lpTbb[nCount].iString);
2877 fHasString = TRUE;
2879 else
2880 btnPtr->iString = lpTbb[nCount].iString;
2882 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
2885 if (infoPtr->nNumStrings > 0 || fHasString)
2886 TOOLBAR_CalcToolbar(hwnd);
2887 else
2888 TOOLBAR_LayoutToolbar(hwnd);
2889 TOOLBAR_AutoSize (hwnd);
2891 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
2893 InvalidateRect(hwnd, NULL, TRUE);
2895 return TRUE;
2899 static LRESULT
2900 TOOLBAR_AddStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2902 #define MAX_RESOURCE_STRING_LENGTH 512
2903 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2904 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2905 INT nIndex = infoPtr->nNumStrings;
2907 if ((wParam) && (HIWORD(lParam) == 0)) {
2908 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
2909 WCHAR delimiter;
2910 WCHAR *next_delim;
2911 WCHAR *p;
2912 INT len;
2913 TRACE("adding string from resource!\n");
2915 len = LoadStringW ((HINSTANCE)wParam, (UINT)lParam,
2916 szString, MAX_RESOURCE_STRING_LENGTH);
2918 TRACE("len=%d %s\n", len, debugstr_w(szString));
2919 if (len == 0 || len == 1)
2920 return nIndex;
2922 TRACE("Delimiter: 0x%x\n", *szString);
2923 delimiter = *szString;
2924 p = szString + 1;
2926 while ((next_delim = strchrW(p, delimiter)) != NULL) {
2927 *next_delim = 0;
2928 if (next_delim + 1 >= szString + len)
2930 /* this may happen if delimiter == '\0' or if the last char is a
2931 * delimiter (then it is ignored like the native does) */
2932 break;
2935 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2936 Str_SetPtrW(&infoPtr->strings[infoPtr->nNumStrings], p);
2937 infoPtr->nNumStrings++;
2939 p = next_delim + 1;
2942 else {
2943 LPWSTR p = (LPWSTR)lParam;
2944 INT len;
2946 if (p == NULL)
2947 return -1;
2948 TRACE("adding string(s) from array!\n");
2949 while (*p) {
2950 len = strlenW (p);
2952 TRACE("len=%d %s\n", len, debugstr_w(p));
2953 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2954 Str_SetPtrW (&infoPtr->strings[infoPtr->nNumStrings], p);
2955 infoPtr->nNumStrings++;
2957 p += (len+1);
2961 if (fFirstString)
2962 TOOLBAR_CalcToolbar(hwnd);
2963 return nIndex;
2967 static LRESULT
2968 TOOLBAR_AddStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2970 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
2971 BOOL fFirstString = (infoPtr->nNumStrings == 0);
2972 LPSTR p;
2973 INT nIndex;
2974 INT len;
2976 if ((wParam) && (HIWORD(lParam) == 0)) /* load from resources */
2977 return TOOLBAR_AddStringW(hwnd, wParam, lParam);
2979 p = (LPSTR)lParam;
2980 if (p == NULL)
2981 return -1;
2983 TRACE("adding string(s) from array!\n");
2984 nIndex = infoPtr->nNumStrings;
2985 while (*p) {
2986 len = strlen (p);
2987 TRACE("len=%d \"%s\"\n", len, p);
2989 infoPtr->strings = ReAlloc(infoPtr->strings, sizeof(LPWSTR)*(infoPtr->nNumStrings+1));
2990 Str_SetPtrAtoW(&infoPtr->strings[infoPtr->nNumStrings], p);
2991 infoPtr->nNumStrings++;
2993 p += (len+1);
2996 if (fFirstString)
2997 TOOLBAR_CalcToolbar(hwnd);
2998 return nIndex;
3002 static LRESULT
3003 TOOLBAR_AutoSize (HWND hwnd)
3005 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3006 RECT parent_rect;
3007 HWND parent;
3008 INT x, y;
3009 INT cx, cy;
3011 TRACE("auto sizing, style=%x!\n", infoPtr->dwStyle);
3013 parent = GetParent (hwnd);
3015 if (!parent || !infoPtr->bDoRedraw)
3016 return 0;
3018 GetClientRect(parent, &parent_rect);
3020 x = parent_rect.left;
3021 y = parent_rect.top;
3023 TRACE("nRows: %d, infoPtr->nButtonHeight: %d\n", infoPtr->nRows, infoPtr->nButtonHeight);
3025 cy = TOP_BORDER + infoPtr->nRows * infoPtr->nButtonHeight + BOTTOM_BORDER;
3026 cx = parent_rect.right - parent_rect.left;
3028 if ((infoPtr->dwStyle & TBSTYLE_WRAPABLE) || (infoPtr->dwExStyle & TBSTYLE_EX_UNDOC1))
3030 TOOLBAR_LayoutToolbar(hwnd);
3031 InvalidateRect( hwnd, NULL, TRUE );
3034 if (!(infoPtr->dwStyle & CCS_NORESIZE))
3036 RECT window_rect;
3037 UINT uPosFlags = SWP_NOZORDER;
3039 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_NOMOVEY)
3041 GetWindowRect(hwnd, &window_rect);
3042 ScreenToClient(parent, (LPPOINT)&window_rect.left);
3043 y = window_rect.top;
3045 if ((infoPtr->dwStyle & CCS_BOTTOM) == CCS_BOTTOM)
3047 GetWindowRect(hwnd, &window_rect);
3048 y = parent_rect.bottom - ( window_rect.bottom - window_rect.top);
3051 if (infoPtr->dwStyle & CCS_NOPARENTALIGN)
3052 uPosFlags |= SWP_NOMOVE;
3054 if (!(infoPtr->dwStyle & CCS_NODIVIDER))
3055 cy += GetSystemMetrics(SM_CYEDGE);
3057 if (infoPtr->dwStyle & WS_BORDER)
3059 x = y = 1; /* FIXME: this looks wrong */
3060 cy += GetSystemMetrics(SM_CYEDGE);
3061 cx += GetSystemMetrics(SM_CXEDGE);
3064 SetWindowPos(hwnd, NULL, x, y, cx, cy, uPosFlags);
3067 return 0;
3071 static LRESULT
3072 TOOLBAR_ButtonCount (HWND hwnd, WPARAM wParam, LPARAM lParam)
3074 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3076 return infoPtr->nNumButtons;
3080 static LRESULT
3081 TOOLBAR_ButtonStructSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
3083 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3085 infoPtr->dwStructSize = (DWORD)wParam;
3087 return 0;
3091 static LRESULT
3092 TOOLBAR_ChangeBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3094 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3095 TBUTTON_INFO *btnPtr;
3096 INT nIndex;
3098 TRACE("button %ld, iBitmap now %d\n", wParam, LOWORD(lParam));
3100 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3101 if (nIndex == -1)
3102 return FALSE;
3104 btnPtr = &infoPtr->buttons[nIndex];
3105 btnPtr->iBitmap = LOWORD(lParam);
3107 /* we HAVE to erase the background, the new bitmap could be */
3108 /* transparent */
3109 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3111 return TRUE;
3115 static LRESULT
3116 TOOLBAR_CheckButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3118 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3119 TBUTTON_INFO *btnPtr;
3120 INT nIndex;
3121 INT nOldIndex = -1;
3122 BOOL bChecked = FALSE;
3124 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3126 TRACE("hwnd=%p, btn index=%d, lParam=0x%08lx\n", hwnd, nIndex, lParam);
3128 if (nIndex == -1)
3129 return FALSE;
3131 btnPtr = &infoPtr->buttons[nIndex];
3133 bChecked = (btnPtr->fsState & TBSTATE_CHECKED) ? TRUE : FALSE;
3135 if (LOWORD(lParam) == FALSE)
3136 btnPtr->fsState &= ~TBSTATE_CHECKED;
3137 else {
3138 if (btnPtr->fsStyle & BTNS_GROUP) {
3139 nOldIndex =
3140 TOOLBAR_GetCheckedGroupButtonIndex (infoPtr, nIndex);
3141 if (nOldIndex == nIndex)
3142 return 0;
3143 if (nOldIndex != -1)
3144 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
3146 btnPtr->fsState |= TBSTATE_CHECKED;
3149 if( bChecked != LOWORD(lParam) )
3151 if (nOldIndex != -1)
3152 InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect, TRUE);
3153 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3156 /* FIXME: Send a WM_NOTIFY?? */
3158 return TRUE;
3162 static LRESULT
3163 TOOLBAR_CommandToIndex (HWND hwnd, WPARAM wParam, LPARAM lParam)
3165 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3167 return TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3171 static LRESULT
3172 TOOLBAR_Customize (HWND hwnd)
3174 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3175 CUSTDLG_INFO custInfo;
3176 LRESULT ret;
3177 LPCVOID template;
3178 HRSRC hRes;
3179 NMHDR nmhdr;
3181 custInfo.tbInfo = infoPtr;
3182 custInfo.tbHwnd = hwnd;
3184 /* send TBN_BEGINADJUST notification */
3185 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_BEGINADJUST);
3187 if (!(hRes = FindResourceW (COMCTL32_hModule,
3188 MAKEINTRESOURCEW(IDD_TBCUSTOMIZE),
3189 (LPWSTR)RT_DIALOG)))
3190 return FALSE;
3192 if(!(template = LoadResource (COMCTL32_hModule, hRes)))
3193 return FALSE;
3195 ret = DialogBoxIndirectParamW ((HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE),
3196 (LPCDLGTEMPLATEW)template,
3197 hwnd,
3198 TOOLBAR_CustomizeDialogProc,
3199 (LPARAM)&custInfo);
3201 /* send TBN_ENDADJUST notification */
3202 TOOLBAR_SendNotify (&nmhdr, infoPtr, TBN_ENDADJUST);
3204 return ret;
3208 static LRESULT
3209 TOOLBAR_DeleteButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3211 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3212 INT nIndex = (INT)wParam;
3213 NMTOOLBARW nmtb;
3214 TBUTTON_INFO *btnPtr = &infoPtr->buttons[nIndex];
3216 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3217 return FALSE;
3219 memset(&nmtb, 0, sizeof(nmtb));
3220 nmtb.iItem = btnPtr->idCommand;
3221 nmtb.tbButton.iBitmap = btnPtr->iBitmap;
3222 nmtb.tbButton.idCommand = btnPtr->idCommand;
3223 nmtb.tbButton.fsState = btnPtr->fsState;
3224 nmtb.tbButton.fsStyle = btnPtr->fsStyle;
3225 nmtb.tbButton.dwData = btnPtr->dwData;
3226 nmtb.tbButton.iString = btnPtr->iString;
3227 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_DELETINGBUTTON);
3229 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[nIndex]);
3231 if (infoPtr->nNumButtons == 1) {
3232 TRACE(" simple delete!\n");
3233 Free (infoPtr->buttons);
3234 infoPtr->buttons = NULL;
3235 infoPtr->nNumButtons = 0;
3237 else {
3238 TBUTTON_INFO *oldButtons = infoPtr->buttons;
3239 TRACE("complex delete! [nIndex=%d]\n", nIndex);
3241 infoPtr->nNumButtons--;
3242 infoPtr->buttons = Alloc (sizeof (TBUTTON_INFO) * infoPtr->nNumButtons);
3243 if (nIndex > 0) {
3244 memcpy (&infoPtr->buttons[0], &oldButtons[0],
3245 nIndex * sizeof(TBUTTON_INFO));
3248 if (nIndex < infoPtr->nNumButtons) {
3249 memcpy (&infoPtr->buttons[nIndex], &oldButtons[nIndex+1],
3250 (infoPtr->nNumButtons - nIndex) * sizeof(TBUTTON_INFO));
3253 Free (oldButtons);
3256 TOOLBAR_LayoutToolbar(hwnd);
3258 InvalidateRect (hwnd, NULL, TRUE);
3260 return TRUE;
3264 static LRESULT
3265 TOOLBAR_EnableButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3267 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3268 TBUTTON_INFO *btnPtr;
3269 INT nIndex;
3270 DWORD bState;
3272 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3274 TRACE("hwnd=%p, btn index=%ld, lParam=0x%08lx\n", hwnd, wParam, lParam);
3276 if (nIndex == -1)
3277 return FALSE;
3279 btnPtr = &infoPtr->buttons[nIndex];
3281 bState = btnPtr->fsState & TBSTATE_ENABLED;
3283 /* update the toolbar button state */
3284 if(LOWORD(lParam) == FALSE) {
3285 btnPtr->fsState &= ~(TBSTATE_ENABLED | TBSTATE_PRESSED);
3286 } else {
3287 btnPtr->fsState |= TBSTATE_ENABLED;
3290 /* redraw the button only if the state of the button changed */
3291 if(bState != (btnPtr->fsState & TBSTATE_ENABLED))
3292 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3294 return TRUE;
3298 static inline LRESULT
3299 TOOLBAR_GetAnchorHighlight (HWND hwnd)
3301 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3303 return infoPtr->bAnchor;
3307 static LRESULT
3308 TOOLBAR_GetBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
3310 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3311 INT nIndex;
3313 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3314 if (nIndex == -1)
3315 return -1;
3317 return infoPtr->buttons[nIndex].iBitmap;
3321 static inline LRESULT
3322 TOOLBAR_GetBitmapFlags (HWND hwnd, WPARAM wParam, LPARAM lParam)
3324 return (GetDeviceCaps (0, LOGPIXELSX) >= 120) ? TBBF_LARGE : 0;
3328 static LRESULT
3329 TOOLBAR_GetButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3331 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3332 LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3333 INT nIndex = (INT)wParam;
3334 TBUTTON_INFO *btnPtr;
3336 if (lpTbb == NULL)
3337 return FALSE;
3339 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3340 return FALSE;
3342 btnPtr = &infoPtr->buttons[nIndex];
3343 lpTbb->iBitmap = btnPtr->iBitmap;
3344 lpTbb->idCommand = btnPtr->idCommand;
3345 lpTbb->fsState = btnPtr->fsState;
3346 lpTbb->fsStyle = btnPtr->fsStyle;
3347 lpTbb->bReserved[0] = 0;
3348 lpTbb->bReserved[1] = 0;
3349 lpTbb->dwData = btnPtr->dwData;
3350 lpTbb->iString = btnPtr->iString;
3352 return TRUE;
3356 static LRESULT
3357 TOOLBAR_GetButtonInfoT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL bUnicode)
3359 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3360 /* TBBUTTONINFOW and TBBUTTONINFOA have the same layout*/
3361 LPTBBUTTONINFOW lpTbInfo = (LPTBBUTTONINFOW)lParam;
3362 TBUTTON_INFO *btnPtr;
3363 INT nIndex;
3365 if (lpTbInfo == NULL)
3366 return -1;
3368 /* MSDN documents a iImageLabel field added in Vista but it is not present in
3369 * the headers and tests shows that even with comctl 6 Vista accepts only the
3370 * original TBBUTTONINFO size
3372 if (lpTbInfo->cbSize != sizeof(TBBUTTONINFOW))
3374 WARN("Invalid button size\n");
3375 return -1;
3378 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
3379 lpTbInfo->dwMask & 0x80000000);
3380 if (nIndex == -1)
3381 return -1;
3383 if (!(btnPtr = &infoPtr->buttons[nIndex])) return -1;
3385 if (lpTbInfo->dwMask & TBIF_COMMAND)
3386 lpTbInfo->idCommand = btnPtr->idCommand;
3387 if (lpTbInfo->dwMask & TBIF_IMAGE)
3388 lpTbInfo->iImage = btnPtr->iBitmap;
3389 if (lpTbInfo->dwMask & TBIF_LPARAM)
3390 lpTbInfo->lParam = btnPtr->dwData;
3391 if (lpTbInfo->dwMask & TBIF_SIZE)
3392 lpTbInfo->cx = (WORD)(btnPtr->rect.right - btnPtr->rect.left);
3393 if (lpTbInfo->dwMask & TBIF_STATE)
3394 lpTbInfo->fsState = btnPtr->fsState;
3395 if (lpTbInfo->dwMask & TBIF_STYLE)
3396 lpTbInfo->fsStyle = btnPtr->fsStyle;
3397 if (lpTbInfo->dwMask & TBIF_TEXT) {
3398 /* TB_GETBUTTONINFO doesn't retrieve text from the string list, so we
3399 can't use TOOLBAR_GetText here */
3400 if (HIWORD(btnPtr->iString) && (btnPtr->iString != -1)) {
3401 LPWSTR lpText = (LPWSTR)btnPtr->iString;
3402 if (bUnicode)
3403 Str_GetPtrW(lpText, lpTbInfo->pszText, lpTbInfo->cchText);
3404 else
3405 Str_GetPtrWtoA(lpText, (LPSTR)lpTbInfo->pszText, lpTbInfo->cchText);
3406 } else
3407 lpTbInfo->pszText[0] = '\0';
3409 return nIndex;
3413 static LRESULT
3414 TOOLBAR_GetButtonSize (HWND hwnd)
3416 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3418 return MAKELONG((WORD)infoPtr->nButtonWidth,
3419 (WORD)infoPtr->nButtonHeight);
3423 static LRESULT
3424 TOOLBAR_GetButtonTextA (HWND hwnd, WPARAM wParam, LPARAM lParam)
3426 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3427 INT nIndex;
3428 LPWSTR lpText;
3430 if (lParam == 0)
3431 return -1;
3433 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3434 if (nIndex == -1)
3435 return -1;
3437 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3439 return WideCharToMultiByte( CP_ACP, 0, lpText, -1,
3440 (LPSTR)lParam, 0x7fffffff, NULL, NULL ) - 1;
3444 static LRESULT
3445 TOOLBAR_GetButtonTextW (HWND hwnd, WPARAM wParam, LPARAM lParam)
3447 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3448 INT nIndex;
3449 LPWSTR lpText;
3450 LRESULT ret = 0;
3452 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3453 if (nIndex == -1)
3454 return -1;
3456 lpText = TOOLBAR_GetText(infoPtr,&infoPtr->buttons[nIndex]);
3458 if (lpText)
3460 ret = strlenW (lpText);
3462 if (lParam)
3463 strcpyW ((LPWSTR)lParam, lpText);
3466 return ret;
3470 static LRESULT
3471 TOOLBAR_GetDisabledImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3473 TRACE("hwnd=%p, wParam=%ld, lParam=0x%lx\n", hwnd, wParam, lParam);
3474 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3475 return (LRESULT)GETDISIMAGELIST(TOOLBAR_GetInfoPtr (hwnd), wParam);
3479 static inline LRESULT
3480 TOOLBAR_GetExtendedStyle (HWND hwnd)
3482 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3484 TRACE("\n");
3486 return infoPtr->dwExStyle;
3490 static LRESULT
3491 TOOLBAR_GetHotImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3493 TRACE("hwnd=%p, wParam=%ld, lParam=0x%lx\n", hwnd, wParam, lParam);
3494 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3495 return (LRESULT)GETHOTIMAGELIST(TOOLBAR_GetInfoPtr (hwnd), wParam);
3499 static LRESULT
3500 TOOLBAR_GetHotItem (HWND hwnd)
3502 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3504 if (!((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)))
3505 return -1;
3507 if (infoPtr->nHotItem < 0)
3508 return -1;
3510 return (LRESULT)infoPtr->nHotItem;
3514 static LRESULT
3515 TOOLBAR_GetDefImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
3517 TRACE("hwnd=%p, wParam=%ld, lParam=0x%lx\n", hwnd, wParam, lParam);
3518 /* UNDOCUMENTED: wParam is actually the ID of the image list to return */
3519 return (LRESULT) GETDEFIMAGELIST(TOOLBAR_GetInfoPtr(hwnd), wParam);
3523 static LRESULT
3524 TOOLBAR_GetInsertMark (HWND hwnd, WPARAM wParam, LPARAM lParam)
3526 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3527 TBINSERTMARK *lptbim = (TBINSERTMARK*)lParam;
3529 TRACE("hwnd = %p, lptbim = %p\n", hwnd, lptbim);
3531 *lptbim = infoPtr->tbim;
3533 return 0;
3537 static LRESULT
3538 TOOLBAR_GetInsertMarkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
3540 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3542 TRACE("hwnd = %p\n", hwnd);
3544 return (LRESULT)infoPtr->clrInsertMark;
3548 static LRESULT
3549 TOOLBAR_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
3551 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3552 TBUTTON_INFO *btnPtr;
3553 LPRECT lpRect;
3554 INT nIndex;
3556 nIndex = (INT)wParam;
3557 btnPtr = &infoPtr->buttons[nIndex];
3558 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3559 return FALSE;
3560 lpRect = (LPRECT)lParam;
3561 if (lpRect == NULL)
3562 return FALSE;
3563 if (btnPtr->fsState & TBSTATE_HIDDEN)
3564 return FALSE;
3566 lpRect->left = btnPtr->rect.left;
3567 lpRect->right = btnPtr->rect.right;
3568 lpRect->bottom = btnPtr->rect.bottom;
3569 lpRect->top = btnPtr->rect.top;
3571 return TRUE;
3575 static LRESULT
3576 TOOLBAR_GetMaxSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
3578 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3579 LPSIZE lpSize = (LPSIZE)lParam;
3581 if (lpSize == NULL)
3582 return FALSE;
3584 lpSize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
3585 lpSize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
3587 TRACE("maximum size %d x %d\n",
3588 infoPtr->rcBound.right - infoPtr->rcBound.left,
3589 infoPtr->rcBound.bottom - infoPtr->rcBound.top);
3591 return TRUE;
3595 /* << TOOLBAR_GetObject >> */
3598 static LRESULT
3599 TOOLBAR_GetPadding (HWND hwnd)
3601 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3602 DWORD oldPad;
3604 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
3605 return (LRESULT) oldPad;
3609 static LRESULT
3610 TOOLBAR_GetRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
3612 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3613 TBUTTON_INFO *btnPtr;
3614 LPRECT lpRect;
3615 INT nIndex;
3617 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3618 btnPtr = &infoPtr->buttons[nIndex];
3619 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
3620 return FALSE;
3621 lpRect = (LPRECT)lParam;
3622 if (lpRect == NULL)
3623 return FALSE;
3625 lpRect->left = btnPtr->rect.left;
3626 lpRect->right = btnPtr->rect.right;
3627 lpRect->bottom = btnPtr->rect.bottom;
3628 lpRect->top = btnPtr->rect.top;
3630 return TRUE;
3634 static LRESULT
3635 TOOLBAR_GetRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
3637 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3639 return infoPtr->nRows;
3643 static LRESULT
3644 TOOLBAR_GetState (HWND hwnd, WPARAM wParam, LPARAM lParam)
3646 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3647 INT nIndex;
3649 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3650 if (nIndex == -1)
3651 return -1;
3653 return infoPtr->buttons[nIndex].fsState;
3657 static LRESULT
3658 TOOLBAR_GetStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
3660 return GetWindowLongW(hwnd, GWL_STYLE);
3664 static LRESULT
3665 TOOLBAR_GetTextRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
3667 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3669 return infoPtr->nMaxTextRows;
3673 static LRESULT
3674 TOOLBAR_GetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
3676 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3678 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
3679 TOOLBAR_TooltipCreateControl(infoPtr);
3680 return (LRESULT)infoPtr->hwndToolTip;
3684 static LRESULT
3685 TOOLBAR_GetUnicodeFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
3687 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3689 TRACE("%s hwnd=%p\n",
3690 infoPtr->bUnicode ? "TRUE" : "FALSE", hwnd);
3692 return infoPtr->bUnicode;
3696 static inline LRESULT
3697 TOOLBAR_GetVersion (HWND hwnd)
3699 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3700 return infoPtr->iVersion;
3704 static LRESULT
3705 TOOLBAR_HideButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3707 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3708 TBUTTON_INFO *btnPtr;
3709 INT nIndex;
3711 TRACE("\n");
3713 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3714 if (nIndex == -1)
3715 return FALSE;
3717 btnPtr = &infoPtr->buttons[nIndex];
3718 if (LOWORD(lParam) == FALSE)
3719 btnPtr->fsState &= ~TBSTATE_HIDDEN;
3720 else
3721 btnPtr->fsState |= TBSTATE_HIDDEN;
3723 TOOLBAR_LayoutToolbar (hwnd);
3725 InvalidateRect (hwnd, NULL, TRUE);
3727 return TRUE;
3731 static inline LRESULT
3732 TOOLBAR_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
3734 return TOOLBAR_InternalHitTest (hwnd, (LPPOINT)lParam);
3738 static LRESULT
3739 TOOLBAR_Indeterminate (HWND hwnd, WPARAM wParam, LPARAM lParam)
3741 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3742 TBUTTON_INFO *btnPtr;
3743 INT nIndex;
3744 DWORD oldState;
3746 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3747 if (nIndex == -1)
3748 return FALSE;
3750 btnPtr = &infoPtr->buttons[nIndex];
3751 oldState = btnPtr->fsState;
3752 if (LOWORD(lParam) == FALSE)
3753 btnPtr->fsState &= ~TBSTATE_INDETERMINATE;
3754 else
3755 btnPtr->fsState |= TBSTATE_INDETERMINATE;
3757 if(oldState != btnPtr->fsState)
3758 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3760 return TRUE;
3764 static LRESULT
3765 TOOLBAR_InsertButtonT(HWND hwnd, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
3767 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3768 LPTBBUTTON lpTbb = (LPTBBUTTON)lParam;
3769 INT nIndex = (INT)wParam;
3771 if (lpTbb == NULL)
3772 return FALSE;
3774 TOOLBAR_DumpButton(infoPtr, (TBUTTON_INFO *)lpTbb, nIndex, FALSE);
3776 if (nIndex == -1) {
3777 /* EPP: this seems to be an undocumented call (from my IE4)
3778 * I assume in that case that:
3779 * - index of insertion is at the end of existing buttons
3780 * I only see this happen with nIndex == -1, but it could have a special
3781 * meaning (like -nIndex (or ~nIndex) to get the real position of insertion).
3783 nIndex = infoPtr->nNumButtons;
3785 } else if (nIndex < 0)
3786 return FALSE;
3788 TRACE("inserting button index=%d\n", nIndex);
3789 if (nIndex > infoPtr->nNumButtons) {
3790 nIndex = infoPtr->nNumButtons;
3791 TRACE("adjust index=%d\n", nIndex);
3794 infoPtr->nNumButtons++;
3795 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO) * infoPtr->nNumButtons);
3796 memmove(&infoPtr->buttons[nIndex+1], &infoPtr->buttons[nIndex],
3797 (infoPtr->nNumButtons - nIndex - 1) * sizeof(TBUTTON_INFO));
3799 /* insert new button */
3800 infoPtr->buttons[nIndex].iBitmap = lpTbb->iBitmap;
3801 infoPtr->buttons[nIndex].idCommand = lpTbb->idCommand;
3802 infoPtr->buttons[nIndex].fsState = lpTbb->fsState;
3803 infoPtr->buttons[nIndex].fsStyle = lpTbb->fsStyle;
3804 infoPtr->buttons[nIndex].dwData = lpTbb->dwData;
3805 /* if passed string and not index, then add string */
3806 if(HIWORD(lpTbb->iString) && lpTbb->iString!=-1) {
3807 if (fUnicode)
3808 Str_SetPtrW((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPWSTR)lpTbb->iString);
3809 else
3810 Str_SetPtrAtoW((LPWSTR *)&infoPtr->buttons[nIndex].iString, (LPCSTR )lpTbb->iString);
3812 else
3813 infoPtr->buttons[nIndex].iString = lpTbb->iString;
3815 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[nIndex]);
3817 if (infoPtr->nNumStrings > 0)
3818 TOOLBAR_CalcToolbar(hwnd);
3819 else
3820 TOOLBAR_LayoutToolbar(hwnd);
3821 TOOLBAR_AutoSize (hwnd);
3823 InvalidateRect (hwnd, NULL, TRUE);
3825 return TRUE;
3828 /* << TOOLBAR_InsertMarkHitTest >> */
3831 static LRESULT
3832 TOOLBAR_IsButtonChecked (HWND hwnd, WPARAM wParam, LPARAM lParam)
3834 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3835 INT nIndex;
3837 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3838 if (nIndex == -1)
3839 return -1;
3841 return (infoPtr->buttons[nIndex].fsState & TBSTATE_CHECKED);
3845 static LRESULT
3846 TOOLBAR_IsButtonEnabled (HWND hwnd, WPARAM wParam, LPARAM lParam)
3848 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3849 INT nIndex;
3851 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3852 if (nIndex == -1)
3853 return -1;
3855 return (infoPtr->buttons[nIndex].fsState & TBSTATE_ENABLED);
3859 static LRESULT
3860 TOOLBAR_IsButtonHidden (HWND hwnd, WPARAM wParam, LPARAM lParam)
3862 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3863 INT nIndex;
3865 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3866 if (nIndex == -1)
3867 return -1;
3869 return (infoPtr->buttons[nIndex].fsState & TBSTATE_HIDDEN);
3873 static LRESULT
3874 TOOLBAR_IsButtonHighlighted (HWND hwnd, WPARAM wParam, LPARAM lParam)
3876 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3877 INT nIndex;
3879 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3880 if (nIndex == -1)
3881 return -1;
3883 return (infoPtr->buttons[nIndex].fsState & TBSTATE_MARKED);
3887 static LRESULT
3888 TOOLBAR_IsButtonIndeterminate (HWND hwnd, WPARAM wParam, LPARAM lParam)
3890 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3891 INT nIndex;
3893 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3894 if (nIndex == -1)
3895 return -1;
3897 return (infoPtr->buttons[nIndex].fsState & TBSTATE_INDETERMINATE);
3901 static LRESULT
3902 TOOLBAR_IsButtonPressed (HWND hwnd, WPARAM wParam, LPARAM lParam)
3904 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3905 INT nIndex;
3907 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3908 if (nIndex == -1)
3909 return -1;
3911 return (infoPtr->buttons[nIndex].fsState & TBSTATE_PRESSED);
3915 static LRESULT
3916 TOOLBAR_LoadImages (HWND hwnd, WPARAM wParam, LPARAM lParam)
3918 TBADDBITMAP tbab;
3919 tbab.hInst = (HINSTANCE)lParam;
3920 tbab.nID = wParam;
3922 TRACE("hwnd = %p, hInst = %p, nID = %lu\n", hwnd, tbab.hInst, tbab.nID);
3924 return TOOLBAR_AddBitmap(hwnd, 0, (LPARAM)&tbab);
3928 static LRESULT
3929 TOOLBAR_MapAccelerator (HWND hwnd, WPARAM wParam, LPARAM lParam)
3931 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3932 WCHAR wAccel = (WCHAR)wParam;
3933 UINT* pIDButton = (UINT*)lParam;
3934 WCHAR wszAccel[] = {'&',wAccel,0};
3935 int i;
3937 TRACE("hwnd = %p, wAccel = %x(%s), pIDButton = %p\n",
3938 hwnd, wAccel, debugstr_wn(&wAccel,1), pIDButton);
3940 for (i = 0; i < infoPtr->nNumButtons; i++)
3942 TBUTTON_INFO *btnPtr = infoPtr->buttons+i;
3943 if (!(btnPtr->fsStyle & BTNS_NOPREFIX) &&
3944 !(btnPtr->fsState & TBSTATE_HIDDEN))
3946 int iLen = strlenW(wszAccel);
3947 LPCWSTR lpszStr = TOOLBAR_GetText(infoPtr, btnPtr);
3949 if (!lpszStr)
3950 continue;
3952 while (*lpszStr)
3954 if ((lpszStr[0] == '&') && (lpszStr[1] == '&'))
3956 lpszStr += 2;
3957 continue;
3959 if (!strncmpiW(lpszStr, wszAccel, iLen))
3961 *pIDButton = btnPtr->idCommand;
3962 return TRUE;
3964 lpszStr++;
3968 return FALSE;
3972 static LRESULT
3973 TOOLBAR_MarkButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
3975 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
3976 INT nIndex;
3977 DWORD oldState;
3978 TBUTTON_INFO *btnPtr;
3980 TRACE("hwnd = %p, wParam = %ld, lParam = 0x%08lx\n", hwnd, wParam, lParam);
3982 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
3983 if (nIndex == -1)
3984 return FALSE;
3986 btnPtr = &infoPtr->buttons[nIndex];
3987 oldState = btnPtr->fsState;
3989 if (LOWORD(lParam))
3990 btnPtr->fsState |= TBSTATE_MARKED;
3991 else
3992 btnPtr->fsState &= ~TBSTATE_MARKED;
3994 if(oldState != btnPtr->fsState)
3995 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
3997 return TRUE;
4001 /* fixes up an index of a button affected by a move */
4002 static inline void TOOLBAR_MoveFixupIndex(INT* pIndex, INT nIndex, INT nMoveIndex, BOOL bMoveUp)
4004 if (bMoveUp)
4006 if (*pIndex > nIndex && *pIndex <= nMoveIndex)
4007 (*pIndex)--;
4008 else if (*pIndex == nIndex)
4009 *pIndex = nMoveIndex;
4011 else
4013 if (*pIndex >= nMoveIndex && *pIndex < nIndex)
4014 (*pIndex)++;
4015 else if (*pIndex == nIndex)
4016 *pIndex = nMoveIndex;
4021 static LRESULT
4022 TOOLBAR_MoveButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
4024 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4025 INT nIndex;
4026 INT nCount;
4027 INT nMoveIndex = (INT)lParam;
4028 TBUTTON_INFO button;
4030 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", hwnd, wParam, lParam);
4032 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, TRUE);
4033 if ((nIndex == -1) || (nMoveIndex < 0))
4034 return FALSE;
4036 if (nMoveIndex > infoPtr->nNumButtons - 1)
4037 nMoveIndex = infoPtr->nNumButtons - 1;
4039 button = infoPtr->buttons[nIndex];
4041 /* move button right */
4042 if (nIndex < nMoveIndex)
4044 nCount = nMoveIndex - nIndex;
4045 memmove(&infoPtr->buttons[nIndex], &infoPtr->buttons[nIndex+1], nCount*sizeof(TBUTTON_INFO));
4046 infoPtr->buttons[nMoveIndex] = button;
4048 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, TRUE);
4049 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, TRUE);
4050 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, TRUE);
4051 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, TRUE);
4053 else if (nIndex > nMoveIndex) /* move button left */
4055 nCount = nIndex - nMoveIndex;
4056 memmove(&infoPtr->buttons[nMoveIndex+1], &infoPtr->buttons[nMoveIndex], nCount*sizeof(TBUTTON_INFO));
4057 infoPtr->buttons[nMoveIndex] = button;
4059 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDown, nIndex, nMoveIndex, FALSE);
4060 TOOLBAR_MoveFixupIndex(&infoPtr->nButtonDrag, nIndex, nMoveIndex, FALSE);
4061 TOOLBAR_MoveFixupIndex(&infoPtr->nOldHit, nIndex, nMoveIndex, FALSE);
4062 TOOLBAR_MoveFixupIndex(&infoPtr->nHotItem, nIndex, nMoveIndex, FALSE);
4065 TOOLBAR_LayoutToolbar(hwnd);
4066 TOOLBAR_AutoSize(hwnd);
4067 InvalidateRect(hwnd, NULL, TRUE);
4069 return TRUE;
4073 static LRESULT
4074 TOOLBAR_PressButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
4076 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4077 TBUTTON_INFO *btnPtr;
4078 INT nIndex;
4079 DWORD oldState;
4081 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
4082 if (nIndex == -1)
4083 return FALSE;
4085 btnPtr = &infoPtr->buttons[nIndex];
4086 oldState = btnPtr->fsState;
4087 if (LOWORD(lParam) == FALSE)
4088 btnPtr->fsState &= ~TBSTATE_PRESSED;
4089 else
4090 btnPtr->fsState |= TBSTATE_PRESSED;
4092 if(oldState != btnPtr->fsState)
4093 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
4095 return TRUE;
4098 /* FIXME: there might still be some confusion her between number of buttons
4099 * and number of bitmaps */
4100 static LRESULT
4101 TOOLBAR_ReplaceBitmap (HWND hwnd, WPARAM wParam, LPARAM lParam)
4103 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4104 LPTBREPLACEBITMAP lpReplace = (LPTBREPLACEBITMAP) lParam;
4105 HBITMAP hBitmap;
4106 int i = 0, nOldButtons = 0, pos = 0;
4107 int nOldBitmaps, nNewBitmaps = 0;
4108 HIMAGELIST himlDef = 0;
4110 TRACE("hInstOld %p nIDOld %lx hInstNew %p nIDNew %lx nButtons %x\n",
4111 lpReplace->hInstOld, lpReplace->nIDOld, lpReplace->hInstNew, lpReplace->nIDNew,
4112 lpReplace->nButtons);
4114 if (lpReplace->hInstOld == HINST_COMMCTRL)
4116 FIXME("changing standard bitmaps not implemented\n");
4117 return FALSE;
4119 else if (lpReplace->hInstOld != 0)
4120 FIXME("resources not in the current module not implemented\n");
4122 TRACE("To be replaced hInstOld %p nIDOld %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4123 for (i = 0; i < infoPtr->nNumBitmapInfos; i++) {
4124 TBITMAP_INFO *tbi = &infoPtr->bitmaps[i];
4125 TRACE("tbimapinfo %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4126 if (tbi->hInst == lpReplace->hInstOld && tbi->nID == lpReplace->nIDOld)
4128 TRACE("Found: nButtons %d hInst %p nID %x\n", tbi->nButtons, tbi->hInst, tbi->nID);
4129 nOldButtons = tbi->nButtons;
4130 tbi->nButtons = lpReplace->nButtons;
4131 tbi->hInst = lpReplace->hInstNew;
4132 tbi->nID = lpReplace->nIDNew;
4133 TRACE("tbimapinfo changed %d hInstOld %p nIDOld %x\n", i, tbi->hInst, tbi->nID);
4134 break;
4136 pos += tbi->nButtons;
4139 if (nOldButtons == 0)
4141 WARN("No hinst/bitmap found! hInst %p nID %lx\n", lpReplace->hInstOld, lpReplace->nIDOld);
4142 return FALSE;
4145 /* copy the bitmap before adding it as ImageList_AddMasked modifies the
4146 * bitmap
4148 if (lpReplace->hInstNew)
4149 hBitmap = LoadBitmapW(lpReplace->hInstNew,(LPWSTR)lpReplace->nIDNew);
4150 else
4151 hBitmap = CopyImage((HBITMAP)lpReplace->nIDNew, IMAGE_BITMAP, 0, 0, 0);
4153 himlDef = GETDEFIMAGELIST(infoPtr, 0); /* fixme: correct? */
4154 nOldBitmaps = ImageList_GetImageCount(himlDef);
4156 /* ImageList_Replace(GETDEFIMAGELIST(), pos, hBitmap, NULL); */
4158 for (i = pos + nOldBitmaps - 1; i >= pos; i--)
4159 ImageList_Remove(himlDef, i);
4161 if (hBitmap)
4163 ImageList_AddMasked (himlDef, hBitmap, comctl32_color.clrBtnFace);
4164 nNewBitmaps = ImageList_GetImageCount(himlDef);
4165 DeleteObject(hBitmap);
4168 infoPtr->nNumBitmaps = infoPtr->nNumBitmaps - nOldBitmaps + nNewBitmaps;
4170 TRACE(" pos %d %d old bitmaps replaced by %d new ones.\n",
4171 pos, nOldBitmaps, nNewBitmaps);
4173 InvalidateRect(hwnd, NULL, TRUE);
4174 return TRUE;
4178 /* helper for TOOLBAR_SaveRestoreW */
4179 static BOOL
4180 TOOLBAR_Save(const TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4182 FIXME("save to %s %s\n", debugstr_w(lpSave->pszSubKey),
4183 debugstr_w(lpSave->pszValueName));
4185 return FALSE;
4189 /* helper for TOOLBAR_Restore */
4190 static void
4191 TOOLBAR_DeleteAllButtons(TOOLBAR_INFO *infoPtr)
4193 INT i;
4195 for (i = 0; i < infoPtr->nNumButtons; i++)
4197 TOOLBAR_TooltipDelTool(infoPtr, &infoPtr->buttons[i]);
4200 Free(infoPtr->buttons);
4201 infoPtr->buttons = NULL;
4202 infoPtr->nNumButtons = 0;
4206 /* helper for TOOLBAR_SaveRestoreW */
4207 static BOOL
4208 TOOLBAR_Restore(TOOLBAR_INFO *infoPtr, const TBSAVEPARAMSW *lpSave)
4210 LONG res;
4211 HKEY hkey = NULL;
4212 BOOL ret = FALSE;
4213 DWORD dwType;
4214 DWORD dwSize = 0;
4215 NMTBRESTORE nmtbr;
4217 /* restore toolbar information */
4218 TRACE("restore from %s %s\n", debugstr_w(lpSave->pszSubKey),
4219 debugstr_w(lpSave->pszValueName));
4221 memset(&nmtbr, 0, sizeof(nmtbr));
4223 res = RegOpenKeyExW(lpSave->hkr, lpSave->pszSubKey, 0,
4224 KEY_QUERY_VALUE, &hkey);
4225 if (!res)
4226 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4227 NULL, &dwSize);
4228 if (!res && dwType != REG_BINARY)
4229 res = ERROR_FILE_NOT_FOUND;
4230 if (!res)
4232 nmtbr.pData = Alloc(dwSize);
4233 nmtbr.cbData = dwSize;
4234 if (!nmtbr.pData) res = ERROR_OUTOFMEMORY;
4236 if (!res)
4237 res = RegQueryValueExW(hkey, lpSave->pszValueName, NULL, &dwType,
4238 (LPBYTE)nmtbr.pData, &dwSize);
4239 if (!res)
4241 nmtbr.pCurrent = nmtbr.pData;
4242 nmtbr.iItem = -1;
4243 nmtbr.cbBytesPerRecord = sizeof(DWORD);
4244 nmtbr.cButtons = nmtbr.cbData / nmtbr.cbBytesPerRecord;
4246 if (!TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE))
4248 INT i;
4250 /* remove all existing buttons as this function is designed to
4251 * restore the toolbar to a previously saved state */
4252 TOOLBAR_DeleteAllButtons(infoPtr);
4254 for (i = 0; i < nmtbr.cButtons; i++)
4256 nmtbr.iItem = i;
4257 nmtbr.tbButton.iBitmap = -1;
4258 nmtbr.tbButton.fsState = 0;
4259 nmtbr.tbButton.fsStyle = 0;
4260 nmtbr.tbButton.idCommand = 0;
4261 if (*nmtbr.pCurrent == (DWORD)-1)
4263 /* separator */
4264 nmtbr.tbButton.fsStyle = TBSTYLE_SEP;
4265 nmtbr.tbButton.iBitmap = SEPARATOR_WIDTH;
4267 else if (*nmtbr.pCurrent == (DWORD)-2)
4268 /* hidden button */
4269 nmtbr.tbButton.fsState = TBSTATE_HIDDEN;
4270 else
4271 nmtbr.tbButton.idCommand = (int)*nmtbr.pCurrent;
4273 nmtbr.pCurrent++;
4275 TOOLBAR_SendNotify(&nmtbr.hdr, infoPtr, TBN_RESTORE);
4277 /* can't contain real string as we don't know whether
4278 * the client put an ANSI or Unicode string in there */
4279 if (HIWORD(nmtbr.tbButton.iString))
4280 nmtbr.tbButton.iString = 0;
4282 TOOLBAR_InsertButtonT(infoPtr->hwndSelf, -1,
4283 (LPARAM)&nmtbr.tbButton, TRUE);
4286 /* do legacy notifications */
4287 if (infoPtr->iVersion < 5)
4289 /* FIXME: send TBN_BEGINADJUST */
4290 FIXME("send TBN_GETBUTTONINFO for each button\n");
4291 /* FIXME: send TBN_ENDADJUST */
4294 /* remove all uninitialised buttons
4295 * note: loop backwards to avoid having to fixup i on a
4296 * delete */
4297 for (i = infoPtr->nNumButtons - 1; i >= 0; i--)
4298 if (infoPtr->buttons[i].iBitmap == -1)
4299 TOOLBAR_DeleteButton(infoPtr->hwndSelf, i, 0);
4301 /* only indicate success if at least one button survived */
4302 if (infoPtr->nNumButtons > 0) ret = TRUE;
4305 Free (nmtbr.pData);
4306 RegCloseKey(hkey);
4308 return ret;
4312 static LRESULT
4313 TOOLBAR_SaveRestoreW (HWND hwnd, WPARAM wParam, const TBSAVEPARAMSW *lpSave)
4315 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4317 if (lpSave == NULL) return 0;
4319 if (wParam)
4320 return TOOLBAR_Save(infoPtr, lpSave);
4321 else
4322 return TOOLBAR_Restore(infoPtr, lpSave);
4326 static LRESULT
4327 TOOLBAR_SaveRestoreA (HWND hwnd, WPARAM wParam, const TBSAVEPARAMSA *lpSave)
4329 LPWSTR pszValueName = 0, pszSubKey = 0;
4330 TBSAVEPARAMSW SaveW;
4331 LRESULT result = 0;
4332 int len;
4334 if (lpSave == NULL) return 0;
4336 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, NULL, 0);
4337 pszSubKey = Alloc(len * sizeof(WCHAR));
4338 if (pszSubKey) goto exit;
4339 MultiByteToWideChar(CP_ACP, 0, lpSave->pszSubKey, -1, pszSubKey, len);
4341 len = MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, NULL, 0);
4342 pszValueName = Alloc(len * sizeof(WCHAR));
4343 if (!pszValueName) goto exit;
4344 MultiByteToWideChar(CP_ACP, 0, lpSave->pszValueName, -1, pszValueName, len);
4346 SaveW.pszValueName = pszValueName;
4347 SaveW.pszSubKey = pszSubKey;
4348 SaveW.hkr = lpSave->hkr;
4349 result = TOOLBAR_SaveRestoreW(hwnd, wParam, &SaveW);
4351 exit:
4352 Free (pszValueName);
4353 Free (pszSubKey);
4355 return result;
4359 static LRESULT
4360 TOOLBAR_SetAnchorHighlight (HWND hwnd, WPARAM wParam)
4362 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4363 BOOL bOldAnchor = infoPtr->bAnchor;
4365 TRACE("hwnd=%p, bAnchor = %s\n", hwnd, wParam ? "TRUE" : "FALSE");
4367 infoPtr->bAnchor = (BOOL)wParam;
4369 /* Native does not remove the hot effect from an already hot button */
4371 return (LRESULT)bOldAnchor;
4375 static LRESULT
4376 TOOLBAR_SetBitmapSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
4378 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4379 HIMAGELIST himlDef = GETDEFIMAGELIST(infoPtr, 0);
4381 TRACE("hwnd=%p, wParam=%ld, lParam=%ld\n", hwnd, wParam, lParam);
4383 if (wParam != 0)
4384 FIXME("wParam is %ld. Perhaps image list index?\n", wParam);
4386 if (LOWORD(lParam) == 0)
4387 lParam = MAKELPARAM(1, HIWORD(lParam));
4389 if (HIWORD(lParam)==0)
4390 lParam = MAKELPARAM(LOWORD(lParam), 1);
4392 if (infoPtr->nNumButtons > 0)
4393 WARN("%d buttons, undoc increase to bitmap size : %d-%d -> %d-%d\n",
4394 infoPtr->nNumButtons,
4395 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
4396 LOWORD(lParam), HIWORD(lParam));
4398 infoPtr->nBitmapWidth = (INT)LOWORD(lParam);
4399 infoPtr->nBitmapHeight = (INT)HIWORD(lParam);
4401 if ((himlDef == infoPtr->himlInt) &&
4402 (ImageList_GetImageCount(infoPtr->himlInt) == 0))
4404 ImageList_SetIconSize(infoPtr->himlInt, infoPtr->nBitmapWidth,
4405 infoPtr->nBitmapHeight);
4408 TOOLBAR_CalcToolbar(hwnd);
4409 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
4410 return TRUE;
4414 static LRESULT
4415 TOOLBAR_SetButtonInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam)
4417 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4418 LPTBBUTTONINFOA lptbbi = (LPTBBUTTONINFOA)lParam;
4419 TBUTTON_INFO *btnPtr;
4420 INT nIndex;
4421 RECT oldBtnRect;
4423 if (lptbbi == NULL)
4424 return FALSE;
4425 if (lptbbi->cbSize < sizeof(TBBUTTONINFOA))
4426 return FALSE;
4428 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
4429 lptbbi->dwMask & 0x80000000);
4430 if (nIndex == -1)
4431 return FALSE;
4433 btnPtr = &infoPtr->buttons[nIndex];
4434 if (lptbbi->dwMask & TBIF_COMMAND)
4435 btnPtr->idCommand = lptbbi->idCommand;
4436 if (lptbbi->dwMask & TBIF_IMAGE)
4437 btnPtr->iBitmap = lptbbi->iImage;
4438 if (lptbbi->dwMask & TBIF_LPARAM)
4439 btnPtr->dwData = lptbbi->lParam;
4440 if (lptbbi->dwMask & TBIF_SIZE)
4441 btnPtr->cx = lptbbi->cx;
4442 if (lptbbi->dwMask & TBIF_STATE)
4443 btnPtr->fsState = lptbbi->fsState;
4444 if (lptbbi->dwMask & TBIF_STYLE)
4445 btnPtr->fsStyle = lptbbi->fsStyle;
4447 if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4448 if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1))
4449 /* iString is index, zero it to make Str_SetPtr succeed */
4450 btnPtr->iString=0;
4452 Str_SetPtrAtoW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
4455 /* save the button rect to see if we need to redraw the whole toolbar */
4456 oldBtnRect = btnPtr->rect;
4457 TOOLBAR_CalcToolbar(hwnd);
4459 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4460 InvalidateRect(hwnd, NULL, TRUE);
4461 else
4462 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
4464 return TRUE;
4468 static LRESULT
4469 TOOLBAR_SetButtonInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam)
4471 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4472 LPTBBUTTONINFOW lptbbi = (LPTBBUTTONINFOW)lParam;
4473 TBUTTON_INFO *btnPtr;
4474 INT nIndex;
4475 RECT oldBtnRect;
4477 if (lptbbi == NULL)
4478 return FALSE;
4479 if (lptbbi->cbSize < sizeof(TBBUTTONINFOW))
4480 return FALSE;
4482 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam,
4483 lptbbi->dwMask & 0x80000000);
4484 if (nIndex == -1)
4485 return FALSE;
4487 btnPtr = &infoPtr->buttons[nIndex];
4488 if (lptbbi->dwMask & TBIF_COMMAND)
4489 btnPtr->idCommand = lptbbi->idCommand;
4490 if (lptbbi->dwMask & TBIF_IMAGE)
4491 btnPtr->iBitmap = lptbbi->iImage;
4492 if (lptbbi->dwMask & TBIF_LPARAM)
4493 btnPtr->dwData = lptbbi->lParam;
4494 if (lptbbi->dwMask & TBIF_SIZE)
4495 btnPtr->cx = lptbbi->cx;
4496 if (lptbbi->dwMask & TBIF_STATE)
4497 btnPtr->fsState = lptbbi->fsState;
4498 if (lptbbi->dwMask & TBIF_STYLE)
4499 btnPtr->fsStyle = lptbbi->fsStyle;
4501 if ((lptbbi->dwMask & TBIF_TEXT) && ((INT_PTR)lptbbi->pszText != -1)) {
4502 if ((HIWORD(btnPtr->iString) == 0) || (btnPtr->iString == -1))
4503 /* iString is index, zero it to make Str_SetPtr succeed */
4504 btnPtr->iString=0;
4505 Str_SetPtrW ((LPWSTR *)&btnPtr->iString, lptbbi->pszText);
4508 /* save the button rect to see if we need to redraw the whole toolbar */
4509 oldBtnRect = btnPtr->rect;
4510 TOOLBAR_CalcToolbar(hwnd);
4512 if (!EqualRect(&oldBtnRect, &btnPtr->rect))
4513 InvalidateRect(hwnd, NULL, TRUE);
4514 else
4515 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
4517 return TRUE;
4521 static LRESULT
4522 TOOLBAR_SetButtonSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
4524 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4525 INT cx = (short)LOWORD(lParam), cy = (short)HIWORD(lParam);
4527 if ((cx < 0) || (cy < 0))
4529 ERR("invalid parameter 0x%08x\n", (DWORD)lParam);
4530 return FALSE;
4533 TRACE("%p, cx = %d, cy = %d\n", hwnd, cx, cy);
4535 /* The documentation claims you can only change the button size before
4536 * any button has been added. But this is wrong.
4537 * WINZIP32.EXE (ver 8) calls this on one of its buttons after adding
4538 * it to the toolbar, and it checks that the return value is nonzero - mjm
4539 * Further testing shows that we must actually perform the change too.
4542 * The documentation also does not mention that if 0 is supplied for
4543 * either size, the system changes it to the default of 24 wide and
4544 * 22 high. Demonstarted in ControlSpy Toolbar. GLA 3/02
4546 if (cx == 0) cx = 24;
4547 if (cy == 0) cx = 22;
4549 cx = max(cx, infoPtr->szPadding.cx + infoPtr->nBitmapWidth);
4550 cy = max(cy, infoPtr->szPadding.cy + infoPtr->nBitmapHeight);
4552 infoPtr->nButtonWidth = cx;
4553 infoPtr->nButtonHeight = cy;
4555 infoPtr->iTopMargin = default_top_margin(infoPtr);
4556 TOOLBAR_LayoutToolbar(hwnd);
4557 return TRUE;
4561 static LRESULT
4562 TOOLBAR_SetButtonWidth (HWND hwnd, WPARAM wParam, LPARAM lParam)
4564 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4566 /* if setting to current values, ignore */
4567 if ((infoPtr->cxMin == (short)LOWORD(lParam)) &&
4568 (infoPtr->cxMax == (short)HIWORD(lParam))) {
4569 TRACE("matches current width, min=%d, max=%d, no recalc\n",
4570 infoPtr->cxMin, infoPtr->cxMax);
4571 return TRUE;
4574 /* save new values */
4575 infoPtr->cxMin = (short)LOWORD(lParam);
4576 infoPtr->cxMax = (short)HIWORD(lParam);
4578 /* otherwise we need to recalc the toolbar and in some cases
4579 recalc the bounding rectangle (does DrawText w/ DT_CALCRECT
4580 which doesn't actually draw - GA). */
4581 TRACE("number of buttons %d, cx=%d, cy=%d, recalcing\n",
4582 infoPtr->nNumButtons, infoPtr->cxMin, infoPtr->cxMax);
4584 TOOLBAR_CalcToolbar (hwnd);
4586 InvalidateRect (hwnd, NULL, TRUE);
4588 return TRUE;
4592 static LRESULT
4593 TOOLBAR_SetCmdId (HWND hwnd, WPARAM wParam, LPARAM lParam)
4595 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4596 INT nIndex = (INT)wParam;
4598 if ((nIndex < 0) || (nIndex >= infoPtr->nNumButtons))
4599 return FALSE;
4601 infoPtr->buttons[nIndex].idCommand = (INT)lParam;
4603 if (infoPtr->hwndToolTip) {
4605 FIXME("change tool tip!\n");
4609 return TRUE;
4613 static LRESULT
4614 TOOLBAR_SetDisabledImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4616 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4617 HIMAGELIST himl = (HIMAGELIST)lParam;
4618 HIMAGELIST himlTemp;
4619 INT id = 0;
4621 if (infoPtr->iVersion >= 5)
4622 id = wParam;
4624 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDis,
4625 &infoPtr->cimlDis, himl, id);
4627 /* FIXME: redraw ? */
4629 return (LRESULT)himlTemp;
4633 static LRESULT
4634 TOOLBAR_SetDrawTextFlags (HWND hwnd, WPARAM wParam, LPARAM lParam)
4636 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4637 DWORD dwTemp;
4639 TRACE("hwnd = %p, dwMask = 0x%08x, dwDTFlags = 0x%08x\n", hwnd, (DWORD)wParam, (DWORD)lParam);
4641 dwTemp = infoPtr->dwDTFlags;
4642 infoPtr->dwDTFlags =
4643 (infoPtr->dwDTFlags & (DWORD)wParam) | (DWORD)lParam;
4645 return (LRESULT)dwTemp;
4648 /* This function differs a bit from what MSDN says it does:
4649 * 1. lParam contains extended style flags to OR with current style
4650 * (MSDN isn't clear on the OR bit)
4651 * 2. wParam appears to contain extended style flags to be reset
4652 * (MSDN says that this parameter is reserved)
4654 static LRESULT
4655 TOOLBAR_SetExtendedStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
4657 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4658 DWORD dwTemp;
4660 dwTemp = infoPtr->dwExStyle;
4661 infoPtr->dwExStyle &= ~wParam;
4662 infoPtr->dwExStyle |= (DWORD)lParam;
4664 TRACE("new style 0x%08x\n", infoPtr->dwExStyle);
4666 if (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL)
4667 FIXME("Unknown Toolbar Extended Style 0x%08x. Please report.\n",
4668 (infoPtr->dwExStyle & ~TBSTYLE_EX_ALL));
4670 TOOLBAR_CalcToolbar (hwnd);
4672 TOOLBAR_AutoSize(hwnd);
4674 InvalidateRect(hwnd, NULL, TRUE);
4676 return (LRESULT)dwTemp;
4680 static LRESULT
4681 TOOLBAR_SetHotImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4683 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4684 HIMAGELIST himlTemp;
4685 HIMAGELIST himl = (HIMAGELIST)lParam;
4686 INT id = 0;
4688 if (infoPtr->iVersion >= 5)
4689 id = wParam;
4691 TRACE("hwnd = %p, himl = %p, id = %d\n", hwnd, himl, id);
4693 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlHot,
4694 &infoPtr->cimlHot, himl, id);
4696 /* FIXME: redraw ? */
4698 return (LRESULT)himlTemp;
4702 /* Makes previous hot button no longer hot, makes the specified
4703 * button hot and sends appropriate notifications. dwReason is one or
4704 * more HICF_ flags. Specify nHit < 0 to make no buttons hot.
4705 * NOTE 1: this function does not validate nHit
4706 * NOTE 2: the name of this function is completely made up and
4707 * not based on any documentation from Microsoft. */
4708 static void
4709 TOOLBAR_SetHotItemEx (TOOLBAR_INFO *infoPtr, INT nHit, DWORD dwReason)
4711 if (infoPtr->nHotItem != nHit)
4713 NMTBHOTITEM nmhotitem;
4714 TBUTTON_INFO *btnPtr = NULL, *oldBtnPtr = NULL;
4716 nmhotitem.dwFlags = dwReason;
4717 if(infoPtr->nHotItem >= 0)
4719 oldBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
4720 nmhotitem.idOld = oldBtnPtr->idCommand;
4722 else
4724 nmhotitem.dwFlags |= HICF_ENTERING;
4725 nmhotitem.idOld = 0;
4728 if (nHit >= 0)
4730 btnPtr = &infoPtr->buttons[nHit];
4731 nmhotitem.idNew = btnPtr->idCommand;
4733 else
4735 nmhotitem.dwFlags |= HICF_LEAVING;
4736 nmhotitem.idNew = 0;
4739 /* now change the hot and invalidate the old and new buttons - if the
4740 * parent agrees */
4741 if (!TOOLBAR_SendNotify(&nmhotitem.hdr, infoPtr, TBN_HOTITEMCHANGE))
4743 if (oldBtnPtr) {
4744 oldBtnPtr->bHot = FALSE;
4745 InvalidateRect(infoPtr->hwndSelf, &oldBtnPtr->rect, TRUE);
4747 /* setting disabled buttons as hot fails even if the notify contains the button id */
4748 if (btnPtr && (btnPtr->fsState & TBSTATE_ENABLED)) {
4749 btnPtr->bHot = TRUE;
4750 InvalidateRect(infoPtr->hwndSelf, &btnPtr->rect, TRUE);
4751 infoPtr->nHotItem = nHit;
4753 else
4754 infoPtr->nHotItem = -1;
4759 static LRESULT
4760 TOOLBAR_SetHotItem (HWND hwnd, WPARAM wParam)
4762 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
4763 INT nOldHotItem = infoPtr->nHotItem;
4765 TRACE("hwnd = %p, nHit = %d\n", hwnd, (INT)wParam);
4767 if ((INT)wParam > infoPtr->nNumButtons)
4768 return infoPtr->nHotItem;
4770 if ((INT)wParam < 0)
4771 wParam = -1;
4773 /* NOTE: an application can still remove the hot item even if anchor
4774 * highlighting is enabled */
4776 TOOLBAR_SetHotItemEx(infoPtr, wParam, HICF_OTHER);
4778 if (nOldHotItem < 0)
4779 return -1;
4781 return (LRESULT)nOldHotItem;
4785 static LRESULT
4786 TOOLBAR_SetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
4788 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4789 HIMAGELIST himlTemp;
4790 HIMAGELIST himl = (HIMAGELIST)lParam;
4791 INT oldButtonWidth = infoPtr->nButtonWidth;
4792 INT i, id = 0;
4794 if (infoPtr->iVersion >= 5)
4795 id = wParam;
4797 himlTemp = TOOLBAR_InsertImageList(&infoPtr->himlDef,
4798 &infoPtr->cimlDef, himl, id);
4800 infoPtr->nNumBitmaps = 0;
4801 for (i = 0; i < infoPtr->cimlDef; i++)
4802 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
4804 if (!ImageList_GetIconSize(himl, &infoPtr->nBitmapWidth,
4805 &infoPtr->nBitmapHeight))
4807 infoPtr->nBitmapWidth = 1;
4808 infoPtr->nBitmapHeight = 1;
4810 TOOLBAR_CalcToolbar(hwnd);
4811 if (infoPtr->nButtonWidth < oldButtonWidth)
4812 TOOLBAR_SetButtonSize(hwnd, 0, MAKELONG(oldButtonWidth, infoPtr->nButtonHeight));
4814 TRACE("hwnd %p, new himl=%p, id = %d, count=%d, bitmap w=%d, h=%d\n",
4815 hwnd, infoPtr->himlDef, id, infoPtr->nNumBitmaps,
4816 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
4818 InvalidateRect(hwnd, NULL, TRUE);
4820 return (LRESULT)himlTemp;
4824 static LRESULT
4825 TOOLBAR_SetIndent (HWND hwnd, WPARAM wParam, LPARAM lParam)
4827 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4829 infoPtr->nIndent = (INT)wParam;
4831 TRACE("\n");
4833 /* process only on indent changing */
4834 if(infoPtr->nIndent != (INT)wParam)
4836 infoPtr->nIndent = (INT)wParam;
4837 TOOLBAR_CalcToolbar (hwnd);
4838 InvalidateRect(hwnd, NULL, FALSE);
4841 return TRUE;
4845 static LRESULT
4846 TOOLBAR_SetInsertMark (HWND hwnd, WPARAM wParam, LPARAM lParam)
4848 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4849 TBINSERTMARK *lptbim = (TBINSERTMARK*)lParam;
4851 TRACE("hwnd = %p, lptbim = { %d, 0x%08x}\n", hwnd, lptbim->iButton, lptbim->dwFlags);
4853 if ((lptbim->dwFlags & ~TBIMHT_AFTER) != 0)
4855 FIXME("Unrecognized flag(s): 0x%08x\n", (lptbim->dwFlags & ~TBIMHT_AFTER));
4856 return 0;
4859 if ((lptbim->iButton == -1) ||
4860 ((lptbim->iButton < infoPtr->nNumButtons) &&
4861 (lptbim->iButton >= 0)))
4863 infoPtr->tbim = *lptbim;
4864 /* FIXME: don't need to update entire toolbar */
4865 InvalidateRect(hwnd, NULL, TRUE);
4867 else
4868 ERR("Invalid button index %d\n", lptbim->iButton);
4870 return 0;
4874 static LRESULT
4875 TOOLBAR_SetInsertMarkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
4877 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4879 infoPtr->clrInsertMark = (COLORREF)lParam;
4881 /* FIXME: don't need to update entire toolbar */
4882 InvalidateRect(hwnd, NULL, TRUE);
4884 return 0;
4888 static LRESULT
4889 TOOLBAR_SetMaxTextRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
4891 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4893 infoPtr->nMaxTextRows = (INT)wParam;
4895 TOOLBAR_CalcToolbar(hwnd);
4896 return TRUE;
4900 /* MSDN gives slightly wrong info on padding.
4901 * 1. It is not only used on buttons with the BTNS_AUTOSIZE style
4902 * 2. It is not used to create a blank area between the edge of the button
4903 * and the text or image if TBSTYLE_LIST is set. It is used to control
4904 * the gap between the image and text.
4905 * 3. It is not applied to both sides. If TBSTYLE_LIST is set it is used
4906 * to control the bottom and right borders [with the border being
4907 * szPadding.cx - (GetSystemMetrics(SM_CXEDGE)+1)], otherwise the padding
4908 * is shared evenly on both sides of the button.
4909 * See blueprints in comments above TOOLBAR_MeasureButton for more info.
4911 static LRESULT
4912 TOOLBAR_SetPadding (HWND hwnd, WPARAM wParam, LPARAM lParam)
4914 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4915 DWORD oldPad;
4917 oldPad = MAKELONG(infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4918 infoPtr->szPadding.cx = min(LOWORD((DWORD)lParam), GetSystemMetrics(SM_CXEDGE));
4919 infoPtr->szPadding.cy = min(HIWORD((DWORD)lParam), GetSystemMetrics(SM_CYEDGE));
4920 TRACE("cx=%d, cy=%d\n",
4921 infoPtr->szPadding.cx, infoPtr->szPadding.cy);
4922 return (LRESULT) oldPad;
4926 static LRESULT
4927 TOOLBAR_SetParent (HWND hwnd, WPARAM wParam, LPARAM lParam)
4929 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4930 HWND hwndOldNotify;
4932 TRACE("\n");
4934 hwndOldNotify = infoPtr->hwndNotify;
4935 infoPtr->hwndNotify = (HWND)wParam;
4937 return (LRESULT)hwndOldNotify;
4941 static LRESULT
4942 TOOLBAR_SetRows (HWND hwnd, WPARAM wParam, LPARAM lParam)
4944 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
4945 LPRECT lprc = (LPRECT)lParam;
4946 int rows = LOWORD(wParam);
4947 BOOL bLarger = HIWORD(wParam);
4949 TRACE("\n");
4951 TRACE("Setting rows to %d (%d)\n", rows, bLarger);
4953 if(infoPtr->nRows != rows)
4955 TBUTTON_INFO *btnPtr = infoPtr->buttons;
4956 int curColumn = 0; /* Current column */
4957 int curRow = 0; /* Current row */
4958 int hidden = 0; /* Number of hidden buttons */
4959 int seps = 0; /* Number of separators */
4960 int idealWrap = 0; /* Ideal wrap point */
4961 int i;
4962 BOOL wrap;
4965 Calculate new size and wrap points - Under windows, setrows will
4966 change the dimensions if needed to show the number of requested
4967 rows (if CCS_NORESIZE is set), or will take up the whole window
4968 (if no CCS_NORESIZE).
4970 Basic algorithum - If N buttons, and y rows requested, each row
4971 contains N/y buttons.
4973 FIXME: Handling of separators not obvious from testing results
4974 FIXME: Take width of window into account?
4977 /* Loop through the buttons one by one counting key items */
4978 for (i = 0; i < infoPtr->nNumButtons; i++ )
4980 btnPtr[i].fsState &= ~TBSTATE_WRAP;
4981 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
4982 hidden++;
4983 else if (btnPtr[i].fsStyle & BTNS_SEP)
4984 seps++;
4987 /* FIXME: Separators make this quite complex */
4988 if (seps) FIXME("Separators unhandled\n");
4990 /* Round up so more per line, ie less rows */
4991 idealWrap = (infoPtr->nNumButtons - hidden + (rows-1)) / rows;
4993 /* Calculate ideal wrap point if we are allowed to grow, but cannot
4994 achieve the requested number of rows. */
4995 if (bLarger && idealWrap > 1)
4997 int resRows = (infoPtr->nNumButtons + (idealWrap-1)) / idealWrap;
4998 int moreRows = (infoPtr->nNumButtons + (idealWrap-2)) / (idealWrap-1);
5000 if (resRows < rows && moreRows > rows)
5002 idealWrap--;
5003 TRACE("Changing idealWrap due to bLarger (now %d)\n", idealWrap);
5007 curColumn = curRow = 0;
5008 wrap = FALSE;
5009 TRACE("Trying to wrap at %d (%d,%d,%d)\n", idealWrap,
5010 infoPtr->nNumButtons, hidden, rows);
5012 for (i = 0; i < infoPtr->nNumButtons; i++ )
5014 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
5015 continue;
5017 /* Step on, wrap if necessary or flag next to wrap */
5018 if (!wrap) {
5019 curColumn++;
5020 } else {
5021 wrap = FALSE;
5022 curColumn = 1;
5023 curRow++;
5026 if (curColumn > (idealWrap-1)) {
5027 wrap = TRUE;
5028 btnPtr[i].fsState |= TBSTATE_WRAP;
5032 TRACE("Result - %d rows\n", curRow + 1);
5034 /* recalculate toolbar */
5035 TOOLBAR_CalcToolbar (hwnd);
5037 /* Resize if necessary (Only if NORESIZE is set - odd, but basically
5038 if NORESIZE is NOT set, then the toolbar will always be resized to
5039 take up the whole window. With it set, sizing needs to be manual. */
5040 if (infoPtr->dwStyle & CCS_NORESIZE) {
5041 SetWindowPos(hwnd, NULL, 0, 0,
5042 infoPtr->rcBound.right - infoPtr->rcBound.left,
5043 infoPtr->rcBound.bottom - infoPtr->rcBound.top,
5044 SWP_NOMOVE);
5047 /* repaint toolbar */
5048 InvalidateRect(hwnd, NULL, TRUE);
5051 /* return bounding rectangle */
5052 if (lprc) {
5053 lprc->left = infoPtr->rcBound.left;
5054 lprc->right = infoPtr->rcBound.right;
5055 lprc->top = infoPtr->rcBound.top;
5056 lprc->bottom = infoPtr->rcBound.bottom;
5059 return 0;
5063 static LRESULT
5064 TOOLBAR_SetState (HWND hwnd, WPARAM wParam, LPARAM lParam)
5066 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5067 TBUTTON_INFO *btnPtr;
5068 INT nIndex;
5070 nIndex = TOOLBAR_GetButtonIndex (infoPtr, (INT)wParam, FALSE);
5071 if (nIndex == -1)
5072 return FALSE;
5074 btnPtr = &infoPtr->buttons[nIndex];
5076 /* if hidden state has changed the invalidate entire window and recalc */
5077 if ((btnPtr->fsState & TBSTATE_HIDDEN) != (LOWORD(lParam) & TBSTATE_HIDDEN)) {
5078 btnPtr->fsState = LOWORD(lParam);
5079 TOOLBAR_CalcToolbar (hwnd);
5080 InvalidateRect(hwnd, 0, TRUE);
5081 return TRUE;
5084 /* process state changing if current state doesn't match new state */
5085 if(btnPtr->fsState != LOWORD(lParam))
5087 btnPtr->fsState = LOWORD(lParam);
5088 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5091 return TRUE;
5095 static LRESULT
5096 TOOLBAR_SetStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
5098 SetWindowLongW(hwnd, GWL_STYLE, lParam);
5100 return TRUE;
5104 static inline LRESULT
5105 TOOLBAR_SetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
5107 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5109 TRACE("hwnd=%p, hwndTooltip=%p, lParam=0x%lx\n", hwnd, (HWND)wParam, lParam);
5111 infoPtr->hwndToolTip = (HWND)wParam;
5112 return 0;
5116 static LRESULT
5117 TOOLBAR_SetUnicodeFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
5119 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5120 BOOL bTemp;
5122 TRACE("%s hwnd=%p\n",
5123 ((BOOL)wParam) ? "TRUE" : "FALSE", hwnd);
5125 bTemp = infoPtr->bUnicode;
5126 infoPtr->bUnicode = (BOOL)wParam;
5128 return bTemp;
5132 static LRESULT
5133 TOOLBAR_GetColorScheme (HWND hwnd, LPCOLORSCHEME lParam)
5135 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5137 lParam->clrBtnHighlight = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
5138 comctl32_color.clrBtnHighlight :
5139 infoPtr->clrBtnHighlight;
5140 lParam->clrBtnShadow = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
5141 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
5142 return 1;
5146 static LRESULT
5147 TOOLBAR_SetColorScheme (HWND hwnd, const COLORSCHEME *lParam)
5149 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5151 TRACE("new colors Hl=%x Shd=%x, old colors Hl=%x Shd=%x\n",
5152 lParam->clrBtnHighlight, lParam->clrBtnShadow,
5153 infoPtr->clrBtnHighlight, infoPtr->clrBtnShadow);
5155 infoPtr->clrBtnHighlight = lParam->clrBtnHighlight;
5156 infoPtr->clrBtnShadow = lParam->clrBtnShadow;
5157 InvalidateRect(hwnd, NULL, TRUE);
5158 return 0;
5162 static LRESULT
5163 TOOLBAR_SetVersion (HWND hwnd, INT iVersion)
5165 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5166 INT iOldVersion = infoPtr->iVersion;
5168 infoPtr->iVersion = iVersion;
5170 if (infoPtr->iVersion >= 5)
5171 TOOLBAR_SetUnicodeFormat(hwnd, (WPARAM)TRUE, (LPARAM)0);
5173 return iOldVersion;
5177 static LRESULT
5178 TOOLBAR_GetStringA (HWND hwnd, WPARAM wParam, LPARAM lParam)
5180 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5181 WORD iString = HIWORD(wParam);
5182 WORD buffersize = LOWORD(wParam);
5183 LPSTR str = (LPSTR)lParam;
5184 LRESULT ret = -1;
5186 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", hwnd, iString, buffersize, str);
5188 if (iString < infoPtr->nNumStrings)
5190 ret = WideCharToMultiByte(CP_ACP, 0, infoPtr->strings[iString], -1, str, buffersize, NULL, NULL);
5192 TRACE("returning %s\n", debugstr_a(str));
5194 else
5195 WARN("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5197 return ret;
5201 static LRESULT
5202 TOOLBAR_GetStringW (HWND hwnd, WPARAM wParam, LPARAM lParam)
5204 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5205 WORD iString = HIWORD(wParam);
5206 WORD len = LOWORD(wParam)/sizeof(WCHAR) - 1;
5207 LPWSTR str = (LPWSTR)lParam;
5208 LRESULT ret = -1;
5210 TRACE("hwnd=%p, iString=%d, buffersize=%d, string=%p\n", hwnd, iString, LOWORD(wParam), str);
5212 if (iString < infoPtr->nNumStrings)
5214 len = min(len, strlenW(infoPtr->strings[iString]));
5215 ret = (len+1)*sizeof(WCHAR);
5216 memcpy(str, infoPtr->strings[iString], ret);
5217 str[len] = '\0';
5219 TRACE("returning %s\n", debugstr_w(str));
5221 else
5222 ERR("String index %d out of range (largest is %d)\n", iString, infoPtr->nNumStrings - 1);
5224 return ret;
5227 /* UNDOCUMENTED MESSAGE: This appears to set some kind of size. Perhaps it
5228 * is the maximum size of the toolbar? */
5229 static LRESULT TOOLBAR_Unkwn45D(HWND hwnd, WPARAM wParam, LPARAM lParam)
5231 SIZE * pSize = (SIZE*)lParam;
5232 FIXME("hwnd=%p, wParam=0x%08lx, size.cx=%d, size.cy=%d stub!\n", hwnd, wParam, pSize->cx, pSize->cy);
5233 return 0;
5237 /* UNDOCUMENTED MESSAGE: This is an extended version of the
5238 * TB_SETHOTITEM message. It allows the caller to specify a reason why the
5239 * hot item changed (rather than just the HICF_OTHER that TB_SETHOTITEM
5240 * sends). */
5241 static LRESULT
5242 TOOLBAR_Unkwn45E (HWND hwnd, WPARAM wParam, LPARAM lParam)
5244 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5245 INT nOldHotItem = infoPtr->nHotItem;
5247 TRACE("old item=%d, new item=%d, flags=%08x\n",
5248 nOldHotItem, infoPtr->nHotItem, (DWORD)lParam);
5250 if ((INT) wParam < 0 || (INT)wParam > infoPtr->nNumButtons)
5251 wParam = -1;
5253 /* NOTE: an application can still remove the hot item even if anchor
5254 * highlighting is enabled */
5256 TOOLBAR_SetHotItemEx(infoPtr, wParam, lParam);
5258 GetFocus();
5260 return (nOldHotItem < 0) ? -1 : (LRESULT)nOldHotItem;
5263 /* UNDOCUMENTED MESSAGE: This sets the toolbar global iListGap parameter
5264 * which controls the amount of spacing between the image and the text
5265 * of buttons for TBSTYLE_LIST toolbars. */
5266 static LRESULT TOOLBAR_Unkwn460(HWND hwnd, WPARAM wParam, LPARAM lParam)
5268 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5270 TRACE("hwnd=%p iListGap=%ld\n", hwnd, wParam);
5272 if (lParam != 0)
5273 FIXME("lParam = 0x%08lx. Please report\n", lParam);
5275 infoPtr->iListGap = (INT)wParam;
5277 InvalidateRect(hwnd, NULL, TRUE);
5279 return 0;
5282 /* UNDOCUMENTED MESSAGE: This returns the number of maximum number
5283 * of image lists associated with the various states. */
5284 static LRESULT TOOLBAR_Unkwn462(HWND hwnd, WPARAM wParam, LPARAM lParam)
5286 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
5288 TRACE("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5290 return max(infoPtr->cimlDef, max(infoPtr->cimlHot, infoPtr->cimlDis));
5293 static LRESULT
5294 TOOLBAR_Unkwn463 (HWND hwnd, WPARAM wParam, LPARAM lParam)
5296 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5297 LPSIZE lpsize = (LPSIZE)lParam;
5299 if (lpsize == NULL)
5300 return FALSE;
5303 * Testing shows the following:
5304 * wParam = 0 adjust cx value
5305 * = 1 set cy value to max size.
5306 * lParam pointer to SIZE structure
5309 TRACE("[0463] wParam %ld, lParam 0x%08lx -> 0x%08x 0x%08x\n",
5310 wParam, lParam, lpsize->cx, lpsize->cy);
5312 switch(wParam) {
5313 case 0:
5314 if (lpsize->cx == -1) {
5315 /* **** this is wrong, native measures each button and sets it */
5316 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5318 else if(HIWORD(lpsize->cx)) {
5319 RECT rc;
5320 HWND hwndParent = GetParent(hwnd);
5322 GetWindowRect(hwnd, &rc);
5323 MapWindowPoints(0, hwndParent, (LPPOINT)&rc, 2);
5324 TRACE("mapped to (%d,%d)-(%d,%d)\n",
5325 rc.left, rc.top, rc.right, rc.bottom);
5326 lpsize->cx = max(rc.right-rc.left,
5327 infoPtr->rcBound.right - infoPtr->rcBound.left);
5329 else {
5330 lpsize->cx = infoPtr->rcBound.right - infoPtr->rcBound.left;
5332 break;
5333 case 1:
5334 lpsize->cy = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
5335 break;
5336 default:
5337 ERR("Unknown wParam %ld for Toolbar message [0463]. Please report\n",
5338 wParam);
5339 return 0;
5341 TRACE("[0463] set to -> 0x%08x 0x%08x\n",
5342 lpsize->cx, lpsize->cy);
5343 return 1;
5346 static LRESULT TOOLBAR_Unkwn464(HWND hwnd, WPARAM wParam, LPARAM lParam)
5348 FIXME("hwnd=%p wParam %08lx lParam %08lx\n", hwnd, wParam, lParam);
5350 InvalidateRect(hwnd, NULL, TRUE);
5351 return 1;
5355 static LRESULT
5356 TOOLBAR_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
5358 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5359 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
5360 LOGFONTW logFont;
5362 TRACE("hwnd = %p\n", hwnd);
5364 /* initialize info structure */
5365 infoPtr->nButtonWidth = 23;
5366 infoPtr->nButtonHeight = 22;
5367 infoPtr->nBitmapHeight = 16;
5368 infoPtr->nBitmapWidth = 16;
5370 infoPtr->nMaxTextRows = 1;
5371 infoPtr->cxMin = -1;
5372 infoPtr->cxMax = -1;
5373 infoPtr->nNumBitmaps = 0;
5374 infoPtr->nNumStrings = 0;
5376 infoPtr->bCaptured = FALSE;
5377 infoPtr->nButtonDown = -1;
5378 infoPtr->nButtonDrag = -1;
5379 infoPtr->nOldHit = -1;
5380 infoPtr->nHotItem = -1;
5381 // infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
5382 infoPtr->dwDTFlags = (dwStyle & TBSTYLE_LIST) ? DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS: DT_CENTER | DT_END_ELLIPSIS;
5383 infoPtr->bAnchor = FALSE; /* no anchor highlighting */
5384 infoPtr->bDragOutSent = FALSE;
5385 infoPtr->iVersion = 0;
5386 infoPtr->hwndSelf = hwnd;
5387 infoPtr->bDoRedraw = TRUE;
5388 infoPtr->clrBtnHighlight = CLR_DEFAULT;
5389 infoPtr->clrBtnShadow = CLR_DEFAULT;
5390 infoPtr->szPadding.cx = DEFPAD_CX;
5391 infoPtr->szPadding.cy = DEFPAD_CY;
5392 infoPtr->iListGap = DEFLISTGAP;
5393 infoPtr->iTopMargin = default_top_margin(infoPtr);
5394 infoPtr->dwStyle = dwStyle;
5395 infoPtr->tbim.iButton = -1;
5396 GetClientRect(hwnd, &infoPtr->client_rect);
5397 // infoPtr->bUnicode = infoPtr->hwndNotify &&
5398 // (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwnd, (LPARAM)NF_REQUERY));
5399 infoPtr->hwndToolTip = NULL; /* if needed the tooltip control will be created after a WM_MOUSEMOVE */
5401 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
5402 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW (&logFont);
5404 OpenThemeData (hwnd, themeClass);
5406 TOOLBAR_CheckStyle (hwnd, dwStyle);
5408 return 0;
5412 static LRESULT
5413 TOOLBAR_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
5415 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5417 /* delete tooltip control */
5418 if (infoPtr->hwndToolTip)
5419 DestroyWindow (infoPtr->hwndToolTip);
5421 /* delete temporary buffer for tooltip text */
5422 Free (infoPtr->pszTooltipText);
5423 Free (infoPtr->bitmaps); /* bitmaps list */
5425 /* delete button data */
5426 Free (infoPtr->buttons);
5428 /* delete strings */
5429 if (infoPtr->strings) {
5430 INT i;
5431 for (i = 0; i < infoPtr->nNumStrings; i++)
5432 Free (infoPtr->strings[i]);
5434 Free (infoPtr->strings);
5437 /* destroy internal image list */
5438 if (infoPtr->himlInt)
5439 ImageList_Destroy (infoPtr->himlInt);
5441 TOOLBAR_DeleteImageList(&infoPtr->himlDef, &infoPtr->cimlDef);
5442 TOOLBAR_DeleteImageList(&infoPtr->himlDis, &infoPtr->cimlDis);
5443 TOOLBAR_DeleteImageList(&infoPtr->himlHot, &infoPtr->cimlHot);
5445 /* delete default font */
5446 DeleteObject (infoPtr->hDefaultFont);
5448 CloseThemeData (GetWindowTheme (hwnd));
5450 /* free toolbar info data */
5451 Free (infoPtr);
5452 SetWindowLongPtrW (hwnd, 0, 0);
5454 return 0;
5458 static LRESULT
5459 TOOLBAR_EraseBackground (HWND hwnd, WPARAM wParam, LPARAM lParam)
5461 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5462 NMTBCUSTOMDRAW tbcd;
5463 INT ret = FALSE;
5464 DWORD ntfret;
5465 HTHEME theme = GetWindowTheme (hwnd);
5466 DWORD dwEraseCustDraw = 0;
5468 /* the app has told us not to redraw the toolbar */
5469 if (!infoPtr->bDoRedraw)
5470 return FALSE;
5472 if (infoPtr->dwStyle & TBSTYLE_CUSTOMERASE) {
5473 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5474 tbcd.nmcd.dwDrawStage = CDDS_PREERASE;
5475 tbcd.nmcd.hdc = (HDC)wParam;
5476 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5477 dwEraseCustDraw = ntfret & 0xffff;
5479 /* FIXME: in general the return flags *can* be or'ed together */
5480 switch (dwEraseCustDraw)
5482 case CDRF_DODEFAULT:
5483 break;
5484 case CDRF_SKIPDEFAULT:
5485 return TRUE;
5486 default:
5487 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_PREERASE)\n",
5488 hwnd, ntfret);
5492 /* If the toolbar is "transparent" then pass the WM_ERASEBKGND up
5493 * to my parent for processing.
5495 if (theme || (infoPtr->dwStyle & TBSTYLE_TRANSPARENT)) {
5496 POINT pt, ptorig;
5497 HDC hdc = (HDC)wParam;
5498 HWND parent;
5500 pt.x = 0;
5501 pt.y = 0;
5502 parent = GetParent(hwnd);
5503 MapWindowPoints(hwnd, parent, &pt, 1);
5504 OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
5505 ret = SendMessageW (parent, WM_ERASEBKGND, wParam, lParam);
5506 SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
5508 if (!ret)
5509 ret = DefWindowProcW (hwnd, WM_ERASEBKGND, wParam, lParam);
5511 if (dwEraseCustDraw & CDRF_NOTIFYPOSTERASE) {
5512 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
5513 tbcd.nmcd.dwDrawStage = CDDS_POSTERASE;
5514 tbcd.nmcd.hdc = (HDC)wParam;
5515 ntfret = TOOLBAR_SendNotify (&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
5516 dwEraseCustDraw = ntfret & 0xffff;
5517 switch (dwEraseCustDraw)
5519 case CDRF_DODEFAULT:
5520 break;
5521 case CDRF_SKIPDEFAULT:
5522 return TRUE;
5523 default:
5524 FIXME("[%p] response %d not handled to NM_CUSTOMDRAW (CDDS_POSTERASE)\n",
5525 hwnd, ntfret);
5528 return ret;
5532 static LRESULT
5533 TOOLBAR_GetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
5535 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5537 return (LRESULT)infoPtr->hFont;
5541 static void
5542 TOOLBAR_SetRelativeHotItem(TOOLBAR_INFO *infoPtr, INT iDirection, DWORD dwReason)
5544 INT i;
5545 INT nNewHotItem = infoPtr->nHotItem;
5547 for (i = 0; i < infoPtr->nNumButtons; i++)
5549 /* did we wrap? */
5550 if ((nNewHotItem + iDirection < 0) ||
5551 (nNewHotItem + iDirection >= infoPtr->nNumButtons))
5553 NMTBWRAPHOTITEM nmtbwhi;
5554 nmtbwhi.idNew = infoPtr->buttons[nNewHotItem].idCommand;
5555 nmtbwhi.iDirection = iDirection;
5556 nmtbwhi.dwReason = dwReason;
5558 if (TOOLBAR_SendNotify(&nmtbwhi.hdr, infoPtr, TBN_WRAPHOTITEM))
5559 return;
5562 nNewHotItem += iDirection;
5563 nNewHotItem = (nNewHotItem + infoPtr->nNumButtons) % infoPtr->nNumButtons;
5565 if ((infoPtr->buttons[nNewHotItem].fsState & TBSTATE_ENABLED) &&
5566 !(infoPtr->buttons[nNewHotItem].fsStyle & BTNS_SEP))
5568 TOOLBAR_SetHotItemEx(infoPtr, nNewHotItem, dwReason);
5569 break;
5574 static LRESULT
5575 TOOLBAR_KeyDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
5577 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5578 NMKEY nmkey;
5580 nmkey.nVKey = (UINT)wParam;
5581 nmkey.uFlags = HIWORD(lParam);
5583 if (TOOLBAR_SendNotify(&nmkey.hdr, infoPtr, NM_KEYDOWN))
5584 return DefWindowProcW(hwnd, WM_KEYDOWN, wParam, lParam);
5586 switch ((UINT)wParam)
5588 case VK_LEFT:
5589 case VK_UP:
5590 TOOLBAR_SetRelativeHotItem(infoPtr, -1, HICF_ARROWKEYS);
5591 break;
5592 case VK_RIGHT:
5593 case VK_DOWN:
5594 TOOLBAR_SetRelativeHotItem(infoPtr, 1, HICF_ARROWKEYS);
5595 break;
5596 case VK_SPACE:
5597 case VK_RETURN:
5598 if ((infoPtr->nHotItem >= 0) &&
5599 (infoPtr->buttons[infoPtr->nHotItem].fsState & TBSTATE_ENABLED))
5601 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5602 MAKEWPARAM(infoPtr->buttons[infoPtr->nHotItem].idCommand, BN_CLICKED),
5603 (LPARAM)hwnd);
5605 break;
5608 return 0;
5612 static LRESULT
5613 TOOLBAR_LButtonDblClk (HWND hwnd, WPARAM wParam, LPARAM lParam)
5615 POINT pt;
5616 INT nHit;
5618 pt.x = (short)LOWORD(lParam);
5619 pt.y = (short)HIWORD(lParam);
5620 nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5622 if (nHit >= 0)
5623 TOOLBAR_LButtonDown (hwnd, wParam, lParam);
5624 else if (GetWindowLongW (hwnd, GWL_STYLE) & CCS_ADJUSTABLE)
5625 TOOLBAR_Customize (hwnd);
5627 return 0;
5631 static LRESULT
5632 TOOLBAR_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
5634 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5635 TBUTTON_INFO *btnPtr;
5636 POINT pt;
5637 INT nHit;
5638 NMTOOLBARA nmtb;
5639 NMMOUSE nmmouse;
5640 BOOL bDragKeyPressed;
5642 TRACE("\n");
5644 if (infoPtr->dwStyle & TBSTYLE_ALTDRAG)
5645 bDragKeyPressed = (GetKeyState(VK_MENU) < 0);
5646 else
5647 bDragKeyPressed = (wParam & MK_SHIFT);
5649 if (infoPtr->hwndToolTip)
5650 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
5651 WM_LBUTTONDOWN, wParam, lParam);
5653 pt.x = (short)LOWORD(lParam);
5654 pt.y = (short)HIWORD(lParam);
5655 nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5657 btnPtr = &infoPtr->buttons[nHit];
5659 if ((nHit >= 0) && bDragKeyPressed && (infoPtr->dwStyle & CCS_ADJUSTABLE))
5661 infoPtr->nButtonDrag = nHit;
5662 SetCapture (hwnd);
5664 /* If drag cursor has not been loaded, load it.
5665 * Note: it doesn't need to be freed */
5666 if (!hCursorDrag)
5667 hCursorDrag = LoadCursorW(COMCTL32_hModule, (LPCWSTR)IDC_MOVEBUTTON);
5668 SetCursor(hCursorDrag);
5670 else if (nHit >= 0)
5672 RECT arrowRect;
5673 infoPtr->nOldHit = nHit;
5675 CopyRect(&arrowRect, &btnPtr->rect);
5676 arrowRect.left = max(btnPtr->rect.left, btnPtr->rect.right - DDARROW_WIDTH);
5678 /* for EX_DRAWDDARROWS style, click must be in the drop-down arrow rect */
5679 if ((btnPtr->fsState & TBSTATE_ENABLED) &&
5680 ((btnPtr->fsStyle & BTNS_WHOLEDROPDOWN) ||
5681 ((btnPtr->fsStyle & BTNS_DROPDOWN) &&
5682 ((TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle) && PtInRect(&arrowRect, pt)) ||
5683 (!TOOLBAR_HasDropDownArrows(infoPtr->dwExStyle))))))
5685 LRESULT res;
5687 /* draw in pressed state */
5688 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5689 btnPtr->fsState |= TBSTATE_PRESSED;
5690 else
5691 btnPtr->bDropDownPressed = TRUE;
5692 RedrawWindow(hwnd,&btnPtr->rect,0,
5693 RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
5695 memset(&nmtb, 0, sizeof(nmtb));
5696 nmtb.iItem = btnPtr->idCommand;
5697 nmtb.rcButton = btnPtr->rect;
5698 res = TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5699 TBN_DROPDOWN);
5700 TRACE("TBN_DROPDOWN responded with %ld\n", res);
5702 if (res != TBDDRET_TREATPRESSED)
5704 MSG msg;
5706 /* redraw button in unpressed state */
5707 if (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN)
5708 btnPtr->fsState &= ~TBSTATE_PRESSED;
5709 else
5710 btnPtr->bDropDownPressed = FALSE;
5711 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5713 /* find and set hot item
5714 * NOTE: native doesn't do this, but that is a bug */
5715 GetCursorPos(&pt);
5716 ScreenToClient(hwnd, &pt);
5717 nHit = TOOLBAR_InternalHitTest(hwnd, &pt);
5718 if (!infoPtr->bAnchor || (nHit >= 0))
5719 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5721 /* remove any left mouse button down or double-click messages
5722 * so that we can get a toggle effect on the button */
5723 while (PeekMessageW(&msg, hwnd, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE) ||
5724 PeekMessageW(&msg, hwnd, WM_LBUTTONDBLCLK, WM_LBUTTONDBLCLK, PM_REMOVE))
5727 return 0;
5729 /* otherwise drop through and process as pushed */
5731 infoPtr->bCaptured = TRUE;
5732 infoPtr->nButtonDown = nHit;
5733 infoPtr->bDragOutSent = FALSE;
5735 btnPtr->fsState |= TBSTATE_PRESSED;
5737 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5739 if (btnPtr->fsState & TBSTATE_ENABLED)
5740 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5741 UpdateWindow(hwnd);
5742 SetCapture (hwnd);
5745 if (nHit >=0)
5747 memset(&nmtb, 0, sizeof(nmtb));
5748 nmtb.iItem = btnPtr->idCommand;
5749 TOOLBAR_SendNotify((NMHDR *)&nmtb, infoPtr, TBN_BEGINDRAG);
5752 nmmouse.dwHitInfo = nHit;
5754 /* !!! Undocumented - sends NM_LDOWN with the NMMOUSE structure. */
5755 if (nHit < 0)
5756 nmmouse.dwItemSpec = -1;
5757 else
5759 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5760 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5763 ClientToScreen(hwnd, &pt);
5764 nmmouse.pt = pt;
5766 if (!TOOLBAR_SendNotify(&nmmouse.hdr, infoPtr, NM_LDOWN))
5767 return DefWindowProcW(hwnd, WM_LBUTTONDOWN, wParam, lParam);
5769 return 0;
5772 static LRESULT
5773 TOOLBAR_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
5775 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5776 TBUTTON_INFO *btnPtr;
5777 POINT pt;
5778 INT nHit;
5779 INT nOldIndex = -1;
5780 BOOL bSendMessage = TRUE;
5781 NMHDR hdr;
5782 NMMOUSE nmmouse;
5783 NMTOOLBARA nmtb;
5785 if (infoPtr->hwndToolTip)
5786 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
5787 WM_LBUTTONUP, wParam, lParam);
5789 pt.x = (short)LOWORD(lParam);
5790 pt.y = (short)HIWORD(lParam);
5791 nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
5793 if (!infoPtr->bAnchor || (nHit >= 0))
5794 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5796 if (infoPtr->nButtonDrag >= 0) {
5797 RECT rcClient;
5798 NMHDR hdr;
5800 btnPtr = &infoPtr->buttons[infoPtr->nButtonDrag];
5801 ReleaseCapture();
5802 /* reset cursor */
5803 SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
5805 GetClientRect(hwnd, &rcClient);
5806 if (PtInRect(&rcClient, pt))
5808 INT nButton = -1;
5809 if (nHit >= 0)
5810 nButton = nHit;
5811 else if (nHit < -1)
5812 nButton = -nHit;
5813 else if ((nHit == -1) && PtInRect(&infoPtr->buttons[-nHit].rect, pt))
5814 nButton = -nHit;
5816 if (nButton == infoPtr->nButtonDrag)
5818 /* if the button is moved sightly left and we have a
5819 * separator there then remove it */
5820 if (pt.x < (btnPtr->rect.left + (btnPtr->rect.right - btnPtr->rect.left)/2))
5822 if ((nButton > 0) && (infoPtr->buttons[nButton-1].fsStyle & BTNS_SEP))
5823 TOOLBAR_DeleteButton(hwnd, nButton - 1, 0);
5825 else /* else insert a separator before the dragged button */
5827 TBBUTTON tbb;
5828 memset(&tbb, 0, sizeof(tbb));
5829 tbb.fsStyle = BTNS_SEP;
5830 tbb.iString = -1;
5831 TOOLBAR_InsertButtonT(hwnd, nButton, (LPARAM)&tbb, TRUE);
5834 else
5836 if (nButton == -1)
5838 if ((infoPtr->nNumButtons > 0) && (pt.x < infoPtr->buttons[0].rect.left))
5839 TOOLBAR_MoveButton(hwnd, infoPtr->nButtonDrag, 0);
5840 else
5841 TOOLBAR_MoveButton(hwnd, infoPtr->nButtonDrag, infoPtr->nNumButtons);
5843 else
5844 TOOLBAR_MoveButton(hwnd, infoPtr->nButtonDrag, nButton);
5847 else
5849 TRACE("button %d dragged out of toolbar\n", infoPtr->nButtonDrag);
5850 TOOLBAR_DeleteButton(hwnd, (WPARAM)infoPtr->nButtonDrag, 0);
5853 /* button under cursor changed so need to re-set hot item */
5854 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE | HICF_LMOUSE);
5855 infoPtr->nButtonDrag = -1;
5857 TOOLBAR_SendNotify(&hdr, infoPtr, TBN_TOOLBARCHANGE);
5859 else if (infoPtr->nButtonDown >= 0) {
5860 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5861 btnPtr->fsState &= ~TBSTATE_PRESSED;
5863 if (btnPtr->fsStyle & BTNS_CHECK) {
5864 if (btnPtr->fsStyle & BTNS_GROUP) {
5865 nOldIndex = TOOLBAR_GetCheckedGroupButtonIndex (infoPtr,
5866 nHit);
5867 if (nOldIndex == nHit)
5868 bSendMessage = FALSE;
5869 if ((nOldIndex != nHit) &&
5870 (nOldIndex != -1))
5871 infoPtr->buttons[nOldIndex].fsState &= ~TBSTATE_CHECKED;
5872 btnPtr->fsState |= TBSTATE_CHECKED;
5874 else {
5875 if (btnPtr->fsState & TBSTATE_CHECKED)
5876 btnPtr->fsState &= ~TBSTATE_CHECKED;
5877 else
5878 btnPtr->fsState |= TBSTATE_CHECKED;
5882 if (nOldIndex != -1)
5883 InvalidateRect(hwnd, &infoPtr->buttons[nOldIndex].rect, TRUE);
5886 * now we can ReleaseCapture, which triggers CAPTURECHANGED msg,
5887 * that resets bCaptured and btn TBSTATE_PRESSED flags,
5888 * and obliterates nButtonDown and nOldHit (see TOOLBAR_CaptureChanged)
5890 if ((infoPtr->bCaptured) && (infoPtr->nButtonDown >= 0))
5891 ReleaseCapture ();
5892 infoPtr->nButtonDown = -1;
5894 /* Issue NM_RELEASEDCAPTURE to parent to let him know it is released */
5895 TOOLBAR_SendNotify (&hdr, infoPtr,
5896 NM_RELEASEDCAPTURE);
5898 /* native issues TBN_ENDDRAG here, if _LBUTTONDOWN issued the
5899 * TBN_BEGINDRAG
5901 memset(&nmtb, 0, sizeof(nmtb));
5902 nmtb.iItem = btnPtr->idCommand;
5903 TOOLBAR_SendNotify ((NMHDR *) &nmtb, infoPtr,
5904 TBN_ENDDRAG);
5906 if (btnPtr->fsState & TBSTATE_ENABLED)
5908 SendMessageW (infoPtr->hwndNotify, WM_COMMAND,
5909 MAKEWPARAM(infoPtr->buttons[nHit].idCommand, BN_CLICKED), (LPARAM)hwnd);
5911 /* In case we have just been destroyed... */
5912 if(!IsWindow(hwnd))
5913 return 0;
5917 /* !!! Undocumented - toolbar at 4.71 level and above sends
5918 * NM_CLICK with the NMMOUSE structure. */
5919 nmmouse.dwHitInfo = nHit;
5921 if (nHit < 0)
5922 nmmouse.dwItemSpec = -1;
5923 else
5925 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5926 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5929 ClientToScreen(hwnd, &pt);
5930 nmmouse.pt = pt;
5932 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_CLICK))
5933 return DefWindowProcW(hwnd, WM_LBUTTONUP, wParam, lParam);
5935 return 0;
5938 static LRESULT
5939 TOOLBAR_RButtonUp( HWND hwnd, WPARAM wParam, LPARAM lParam)
5941 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5942 INT nHit;
5943 NMMOUSE nmmouse;
5944 POINT pt;
5946 pt.x = (short)LOWORD(lParam);
5947 pt.y = (short)HIWORD(lParam);
5949 nHit = TOOLBAR_InternalHitTest(hwnd, &pt);
5950 nmmouse.dwHitInfo = nHit;
5952 if (nHit < 0) {
5953 nmmouse.dwItemSpec = -1;
5954 } else {
5955 nmmouse.dwItemSpec = infoPtr->buttons[nmmouse.dwHitInfo].idCommand;
5956 nmmouse.dwItemData = infoPtr->buttons[nmmouse.dwHitInfo].dwData;
5959 ClientToScreen(hwnd, &pt);
5960 nmmouse.pt = pt;
5962 if (!TOOLBAR_SendNotify((LPNMHDR)&nmmouse, infoPtr, NM_RCLICK))
5963 return DefWindowProcW(hwnd, WM_RBUTTONUP, wParam, lParam);
5965 return 0;
5968 static LRESULT
5969 TOOLBAR_RButtonDblClk( HWND hwnd, WPARAM wParam, LPARAM lParam)
5971 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5972 NMHDR nmhdr;
5974 if (!TOOLBAR_SendNotify(&nmhdr, infoPtr, NM_RDBLCLK))
5975 return DefWindowProcW(hwnd, WM_RBUTTONDBLCLK, wParam, lParam);
5977 return 0;
5980 static LRESULT
5981 TOOLBAR_CaptureChanged(HWND hwnd)
5983 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
5984 TBUTTON_INFO *btnPtr;
5986 infoPtr->bCaptured = FALSE;
5988 if (infoPtr->nButtonDown >= 0)
5990 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
5991 btnPtr->fsState &= ~TBSTATE_PRESSED;
5993 infoPtr->nOldHit = -1;
5995 if (btnPtr->fsState & TBSTATE_ENABLED)
5996 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
5998 return 0;
6001 static LRESULT
6002 TOOLBAR_MouseLeave (HWND hwnd, WPARAM wParam, LPARAM lParam)
6004 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6006 /* don't remove hot effects when in anchor highlighting mode or when a
6007 * drop-down button is pressed */
6008 if (infoPtr->nHotItem >= 0 && !infoPtr->bAnchor)
6010 TBUTTON_INFO *hotBtnPtr = &infoPtr->buttons[infoPtr->nHotItem];
6011 if (!hotBtnPtr->bDropDownPressed)
6012 TOOLBAR_SetHotItemEx(infoPtr, TOOLBAR_NOWHERE, HICF_MOUSE);
6015 if (infoPtr->nOldHit < 0)
6016 return TRUE;
6018 /* If the last button we were over is depressed then make it not */
6019 /* depressed and redraw it */
6020 if(infoPtr->nOldHit == infoPtr->nButtonDown)
6022 TBUTTON_INFO *btnPtr;
6023 RECT rc1;
6025 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
6027 btnPtr->fsState &= ~TBSTATE_PRESSED;
6029 rc1 = btnPtr->rect;
6030 InflateRect (&rc1, 1, 1);
6031 InvalidateRect (hwnd, &rc1, TRUE);
6034 if (infoPtr->bCaptured && !infoPtr->bDragOutSent)
6036 NMTOOLBARW nmt;
6037 ZeroMemory(&nmt, sizeof(nmt));
6038 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
6039 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
6040 infoPtr->bDragOutSent = TRUE;
6043 infoPtr->nOldHit = -1; /* reset the old hit index as we've left the toolbar */
6045 return TRUE;
6048 static LRESULT
6049 TOOLBAR_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
6051 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6052 POINT pt;
6053 TRACKMOUSEEVENT trackinfo;
6054 INT nHit;
6055 TBUTTON_INFO *btnPtr;
6057 if ((infoPtr->dwStyle & TBSTYLE_TOOLTIPS) && (infoPtr->hwndToolTip == NULL))
6058 TOOLBAR_TooltipCreateControl(infoPtr);
6060 if ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf)) {
6061 /* fill in the TRACKMOUSEEVENT struct */
6062 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
6063 trackinfo.dwFlags = TME_QUERY;
6065 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
6066 _TrackMouseEvent(&trackinfo);
6068 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
6069 if(trackinfo.hwndTrack != hwnd || !(trackinfo.dwFlags & TME_LEAVE)) {
6070 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
6071 trackinfo.hwndTrack = hwnd;
6073 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
6074 /* and can properly deactivate the hot toolbar button */
6075 _TrackMouseEvent(&trackinfo);
6079 if (infoPtr->hwndToolTip)
6080 TOOLBAR_RelayEvent (infoPtr->hwndToolTip, hwnd,
6081 WM_MOUSEMOVE, wParam, lParam);
6083 pt.x = (short)LOWORD(lParam);
6084 pt.y = (short)HIWORD(lParam);
6086 nHit = TOOLBAR_InternalHitTest (hwnd, &pt);
6088 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
6089 && (!infoPtr->bAnchor || (nHit >= 0)))
6090 TOOLBAR_SetHotItemEx(infoPtr, nHit, HICF_MOUSE);
6092 if (infoPtr->nOldHit != nHit)
6094 if (infoPtr->bCaptured)
6096 if (!infoPtr->bDragOutSent)
6098 NMTOOLBARW nmt;
6099 ZeroMemory(&nmt, sizeof(nmt));
6100 nmt.iItem = infoPtr->buttons[infoPtr->nButtonDown].idCommand;
6101 TOOLBAR_SendNotify(&nmt.hdr, infoPtr, TBN_DRAGOUT);
6102 infoPtr->bDragOutSent = TRUE;
6105 btnPtr = &infoPtr->buttons[infoPtr->nButtonDown];
6106 if (infoPtr->nOldHit == infoPtr->nButtonDown) {
6107 btnPtr->fsState &= ~TBSTATE_PRESSED;
6108 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
6110 else if (nHit == infoPtr->nButtonDown) {
6111 btnPtr->fsState |= TBSTATE_PRESSED;
6112 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
6114 infoPtr->nOldHit = nHit;
6118 return 0;
6122 static inline LRESULT
6123 TOOLBAR_NCActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
6125 /* if (wndPtr->dwStyle & CCS_NODIVIDER) */
6126 return DefWindowProcW (hwnd, WM_NCACTIVATE, wParam, lParam);
6127 /* else */
6128 /* return TOOLBAR_NCPaint (wndPtr, wParam, lParam); */
6132 static inline LRESULT
6133 TOOLBAR_NCCalcSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
6135 if (!(GetWindowLongW(hwnd, GWL_STYLE) & CCS_NODIVIDER))
6136 ((LPRECT)lParam)->top += GetSystemMetrics(SM_CYEDGE);
6138 return DefWindowProcW (hwnd, WM_NCCALCSIZE, wParam, lParam);
6142 static LRESULT
6143 TOOLBAR_NCCreate (HWND hwnd, WPARAM wParam, LPARAM lParam)
6145 TOOLBAR_INFO *infoPtr;
6146 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
6147 DWORD styleadd = 0;
6149 /* allocate memory for info structure */
6150 infoPtr = (TOOLBAR_INFO *)Alloc (sizeof(TOOLBAR_INFO));
6151 SetWindowLongPtrW (hwnd, 0, (LONG_PTR)infoPtr);
6153 /* paranoid!! */
6154 infoPtr->dwStructSize = sizeof(TBBUTTON);
6155 infoPtr->nRows = 1;
6156 infoPtr->nWidth = 0;
6158 /* fix instance handle, if the toolbar was created by CreateToolbarEx() */
6159 if (!GetWindowLongPtrW (hwnd, GWLP_HINSTANCE)) {
6160 HINSTANCE hInst = (HINSTANCE)GetWindowLongPtrW (GetParent (hwnd), GWLP_HINSTANCE);
6161 SetWindowLongPtrW (hwnd, GWLP_HINSTANCE, (LONG_PTR)hInst);
6164 /* native control does:
6165 * Get a lot of colors and brushes
6166 * WM_NOTIFYFORMAT
6167 * SystemParametersInfoW(0x1f, 0x3c, adr1, 0)
6168 * CreateFontIndirectW(adr1)
6169 * CreateBitmap(0x27, 0x24, 1, 1, 0)
6170 * hdc = GetDC(toolbar)
6171 * GetSystemMetrics(0x48)
6172 * fnt2=CreateFontW(0xe, 0, 0, 0, 0x190, 0, 0, 0, 0, 2,
6173 * 0, 0, 0, 0, "MARLETT")
6174 * oldfnt = SelectObject(hdc, fnt2)
6175 * GetCharWidthW(hdc, 0x36, 0x36, adr2)
6176 * GetTextMetricsW(hdc, adr3)
6177 * SelectObject(hdc, oldfnt)
6178 * DeleteObject(fnt2)
6179 * ReleaseDC(hdc)
6180 * InvalidateRect(toolbar, 0, 1)
6181 * SetWindowLongW(toolbar, 0, addr)
6182 * SetWindowLongW(toolbar, -16, xxx) **sometimes**
6183 * WM_STYLECHANGING
6184 * CallWinEx old new
6185 * ie 1 0x56000a4c 0x46000a4c 0x56008a4d
6186 * ie 2 0x4600094c 0x4600094c 0x4600894d
6187 * ie 3 0x56000b4c 0x46000b4c 0x56008b4d
6188 * rebar 0x50008844 0x40008844 0x50008845
6189 * pager 0x50000844 0x40000844 0x50008845
6190 * IC35mgr 0x5400084e **nochange**
6191 * on entry to _NCCREATE 0x5400084e
6192 * rowlist 0x5400004e **nochange**
6193 * on entry to _NCCREATE 0x5400004e
6197 /* I think the code below is a bug, but it is the way that the native
6198 * controls seem to work. The effect is that if the user of TBSTYLE_FLAT
6199 * forgets to specify TBSTYLE_TRANSPARENT but does specify either
6200 * CCS_TOP or CCS_BOTTOM (_NOMOVEY and _TOP), then the control
6201 * does *not* set TBSTYLE_TRANSPARENT even though it should!!!!
6202 * Somehow, the only cases of this seem to be MFC programs.
6204 * Note also that the addition of _TRANSPARENT occurs *only* here. It
6205 * does not occur in the WM_STYLECHANGING routine.
6206 * (Guy Albertelli 9/2001)
6209 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
6210 && !(cs->style & TBSTYLE_TRANSPARENT))
6211 styleadd |= TBSTYLE_TRANSPARENT;
6212 if (!(cs->style & (CCS_TOP | CCS_NOMOVEY))) {
6213 styleadd |= CCS_TOP; /* default to top */
6214 SetWindowLongW (hwnd, GWL_STYLE, cs->style | styleadd);
6217 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
6219 infoPtr->bUnicode = infoPtr->hwndNotify &&
6220 (NFR_UNICODE == SendMessageW(hwnd, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndNotify, (LPARAM)NF_QUERY/*NF_REQUERY*/));
6222 return DefWindowProcW (hwnd, WM_NCCREATE, wParam, lParam);
6226 static LRESULT
6227 TOOLBAR_NCPaint (HWND hwnd, WPARAM wParam, LPARAM lParam)
6229 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
6230 RECT rcWindow;
6231 HDC hdc;
6233 if (dwStyle & WS_MINIMIZE)
6234 return 0; /* Nothing to do */
6236 DefWindowProcW (hwnd, WM_NCPAINT, wParam, lParam);
6238 if (!(hdc = GetDCEx (hwnd, 0, DCX_USESTYLE | DCX_WINDOW)))
6239 return 0;
6241 if (!(dwStyle & CCS_NODIVIDER))
6243 GetWindowRect (hwnd, &rcWindow);
6244 OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
6245 if( dwStyle & WS_BORDER )
6246 OffsetRect (&rcWindow, 1, 1);
6247 DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_TOP);
6250 ReleaseDC( hwnd, hdc );
6252 return 0;
6256 /* handles requests from the tooltip control on what text to display */
6257 static LRESULT TOOLBAR_TTGetDispInfo (TOOLBAR_INFO *infoPtr, NMTTDISPINFOW *lpnmtdi)
6259 int index = TOOLBAR_GetButtonIndex(infoPtr, lpnmtdi->hdr.idFrom, FALSE);
6261 TRACE("button index = %d\n", index);
6263 Free (infoPtr->pszTooltipText);
6264 infoPtr->pszTooltipText = NULL;
6266 if (index < 0)
6267 return 0;
6269 if (infoPtr->bUnicode)
6271 WCHAR wszBuffer[INFOTIPSIZE+1];
6272 NMTBGETINFOTIPW tbgit;
6273 unsigned int len; /* in chars */
6275 wszBuffer[0] = '\0';
6276 wszBuffer[INFOTIPSIZE] = '\0';
6278 tbgit.pszText = wszBuffer;
6279 tbgit.cchTextMax = INFOTIPSIZE;
6280 tbgit.iItem = lpnmtdi->hdr.idFrom;
6281 tbgit.lParam = infoPtr->buttons[index].dwData;
6283 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPW);
6285 TRACE("TBN_GETINFOTIPW - got string %s\n", debugstr_w(tbgit.pszText));
6287 len = strlenW(tbgit.pszText);
6288 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6290 /* need to allocate temporary buffer in infoPtr as there
6291 * isn't enough space in buffer passed to us by the
6292 * tooltip control */
6293 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6294 if (infoPtr->pszTooltipText)
6296 memcpy(infoPtr->pszTooltipText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6297 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6298 return 0;
6301 else if (len > 0)
6303 memcpy(lpnmtdi->lpszText, tbgit.pszText, (len+1)*sizeof(WCHAR));
6304 return 0;
6307 else
6309 CHAR szBuffer[INFOTIPSIZE+1];
6310 NMTBGETINFOTIPA tbgit;
6311 unsigned int len; /* in chars */
6313 szBuffer[0] = '\0';
6314 szBuffer[INFOTIPSIZE] = '\0';
6316 tbgit.pszText = szBuffer;
6317 tbgit.cchTextMax = INFOTIPSIZE;
6318 tbgit.iItem = lpnmtdi->hdr.idFrom;
6319 tbgit.lParam = infoPtr->buttons[index].dwData;
6321 TOOLBAR_SendNotify(&tbgit.hdr, infoPtr, TBN_GETINFOTIPA);
6323 TRACE("TBN_GETINFOTIPA - got string %s\n", debugstr_a(tbgit.pszText));
6325 len = MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, NULL, 0);
6326 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]))
6328 /* need to allocate temporary buffer in infoPtr as there
6329 * isn't enough space in buffer passed to us by the
6330 * tooltip control */
6331 infoPtr->pszTooltipText = Alloc(len*sizeof(WCHAR));
6332 if (infoPtr->pszTooltipText)
6334 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1, infoPtr->pszTooltipText, len);
6335 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6336 return 0;
6339 else if (tbgit.pszText[0])
6341 MultiByteToWideChar(CP_ACP, 0, tbgit.pszText, -1,
6342 lpnmtdi->lpszText, sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0]));
6343 return 0;
6347 /* if button has text, but it is not shown then automatically
6348 * use that text as tooltip */
6349 if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) &&
6350 !(infoPtr->buttons[index].fsStyle & BTNS_SHOWTEXT))
6352 LPWSTR pszText = TOOLBAR_GetText(infoPtr, &infoPtr->buttons[index]);
6353 unsigned int len = pszText ? strlenW(pszText) : 0;
6355 TRACE("using button hidden text %s\n", debugstr_w(pszText));
6357 if (len > sizeof(lpnmtdi->szText)/sizeof(lpnmtdi->szText[0])-1)
6359 /* need to allocate temporary buffer in infoPtr as there
6360 * isn't enough space in buffer passed to us by the
6361 * tooltip control */
6362 infoPtr->pszTooltipText = Alloc((len+1)*sizeof(WCHAR));
6363 if (infoPtr->pszTooltipText)
6365 memcpy(infoPtr->pszTooltipText, pszText, (len+1)*sizeof(WCHAR));
6366 lpnmtdi->lpszText = infoPtr->pszTooltipText;
6367 return 0;
6370 else if (len > 0)
6372 memcpy(lpnmtdi->lpszText, pszText, (len+1)*sizeof(WCHAR));
6373 return 0;
6377 TRACE("Sending tooltip notification to %p\n", infoPtr->hwndNotify);
6379 /* last resort: send notification on to app */
6380 /* FIXME: find out what is really used here */
6381 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmtdi->hdr.idFrom, (LPARAM)lpnmtdi);
6385 static inline LRESULT
6386 TOOLBAR_Notify (HWND hwnd, WPARAM wParam, LPARAM lParam)
6388 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6389 LPNMHDR lpnmh = (LPNMHDR)lParam;
6391 switch (lpnmh->code)
6393 case PGN_CALCSIZE:
6395 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
6397 if (lppgc->dwFlag == PGF_CALCWIDTH) {
6398 lppgc->iWidth = infoPtr->rcBound.right - infoPtr->rcBound.left;
6399 TRACE("processed PGN_CALCSIZE, returning horz size = %d\n",
6400 lppgc->iWidth);
6402 else {
6403 lppgc->iHeight = infoPtr->rcBound.bottom - infoPtr->rcBound.top;
6404 TRACE("processed PGN_CALCSIZE, returning vert size = %d\n",
6405 lppgc->iHeight);
6407 return 0;
6410 case PGN_SCROLL:
6412 LPNMPGSCROLL lppgs = (LPNMPGSCROLL)lParam;
6414 lppgs->iScroll = (lppgs->iDir & (PGF_SCROLLLEFT | PGF_SCROLLRIGHT)) ?
6415 infoPtr->nButtonWidth : infoPtr->nButtonHeight;
6416 TRACE("processed PGN_SCROLL, returning scroll=%d, dir=%d\n",
6417 lppgs->iScroll, lppgs->iDir);
6418 return 0;
6421 case TTN_GETDISPINFOW:
6422 return TOOLBAR_TTGetDispInfo(infoPtr, (LPNMTTDISPINFOW)lParam);
6424 case TTN_GETDISPINFOA:
6425 FIXME("TTN_GETDISPINFOA - should not be received; please report\n");
6426 return 0;
6428 default:
6429 return 0;
6434 static LRESULT
6435 TOOLBAR_NotifyFormat(const TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
6437 LRESULT format;
6439 TRACE("wParam = 0x%lx, lParam = 0x%08lx\n", wParam, lParam);
6441 if (lParam == NF_QUERY)
6442 return NFR_UNICODE;
6444 if (lParam == NF_REQUERY) {
6445 format = SendMessageW(infoPtr->hwndNotify,
6446 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
6447 if ((format != NFR_ANSI) && (format != NFR_UNICODE)) {
6448 ERR("wrong response to WM_NOTIFYFORMAT (%ld), assuming ANSI\n",
6449 format);
6450 format = NFR_ANSI;
6452 return format;
6454 return 0;
6458 static LRESULT
6459 TOOLBAR_Paint (HWND hwnd, WPARAM wParam)
6461 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
6462 HDC hdc;
6463 PAINTSTRUCT ps;
6465 /* fill ps.rcPaint with a default rect */
6466 memcpy(&(ps.rcPaint), &(infoPtr->rcBound), sizeof(infoPtr->rcBound));
6468 hdc = wParam==0 ? BeginPaint(hwnd, &ps) : (HDC)wParam;
6470 TRACE("psrect=(%d,%d)-(%d,%d)\n",
6471 ps.rcPaint.left, ps.rcPaint.top,
6472 ps.rcPaint.right, ps.rcPaint.bottom);
6474 TOOLBAR_Refresh (hwnd, hdc, &ps);
6475 if (!wParam) EndPaint (hwnd, &ps);
6477 return 0;
6481 static LRESULT
6482 TOOLBAR_SetFocus (HWND hwnd, WPARAM wParam)
6484 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6486 TRACE("nHotItem = %d\n", infoPtr->nHotItem);
6488 /* make first item hot */
6489 if (infoPtr->nNumButtons > 0)
6490 TOOLBAR_SetHotItemEx(infoPtr, 0, HICF_OTHER);
6492 return 0;
6495 static LRESULT
6496 TOOLBAR_SetFont(HWND hwnd, WPARAM wParam, LPARAM lParam)
6498 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr(hwnd);
6500 TRACE("font=%p redraw=%ld\n", (HFONT)wParam, lParam);
6502 if (wParam == 0)
6503 infoPtr->hFont = infoPtr->hDefaultFont;
6504 else
6505 infoPtr->hFont = (HFONT)wParam;
6507 TOOLBAR_CalcToolbar(hwnd);
6509 if (lParam)
6510 InvalidateRect(hwnd, NULL, TRUE);
6511 return 1;
6514 static LRESULT
6515 TOOLBAR_SetRedraw (HWND hwnd, WPARAM wParam, LPARAM lParam)
6516 /*****************************************************
6518 * Function;
6519 * Handles the WM_SETREDRAW message.
6521 * Documentation:
6522 * According to testing V4.71 of COMCTL32 returns the
6523 * *previous* status of the redraw flag (either 0 or 1)
6524 * instead of the MSDN documented value of 0 if handled.
6525 * (For laughs see the "consistency" with same function
6526 * in rebar.)
6528 *****************************************************/
6530 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6531 BOOL oldredraw = infoPtr->bDoRedraw;
6533 TRACE("set to %s\n",
6534 (wParam) ? "TRUE" : "FALSE");
6535 infoPtr->bDoRedraw = (BOOL) wParam;
6536 if (wParam) {
6537 InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
6539 return (oldredraw) ? 1 : 0;
6543 static LRESULT
6544 TOOLBAR_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
6546 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6548 TRACE("sizing toolbar!\n");
6550 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
6552 RECT delta_width, delta_height, client, dummy;
6553 DWORD min_x, max_x, min_y, max_y;
6554 TBUTTON_INFO *btnPtr;
6555 INT i;
6557 GetClientRect(hwnd, &client);
6558 if(client.right > infoPtr->client_rect.right)
6560 min_x = infoPtr->client_rect.right;
6561 max_x = client.right;
6563 else
6565 max_x = infoPtr->client_rect.right;
6566 min_x = client.right;
6568 if(client.bottom > infoPtr->client_rect.bottom)
6570 min_y = infoPtr->client_rect.bottom;
6571 max_y = client.bottom;
6573 else
6575 max_y = infoPtr->client_rect.bottom;
6576 min_y = client.bottom;
6579 SetRect(&delta_width, min_x, 0, max_x, min_y);
6580 SetRect(&delta_height, 0, min_y, max_x, max_y);
6582 TRACE("delta_width %s delta_height %s\n", wine_dbgstr_rect(&delta_width), wine_dbgstr_rect(&delta_height));
6583 btnPtr = infoPtr->buttons;
6584 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
6585 if(IntersectRect(&dummy, &delta_width, &btnPtr->rect) ||
6586 IntersectRect(&dummy, &delta_height, &btnPtr->rect))
6587 InvalidateRect(hwnd, &btnPtr->rect, TRUE);
6589 GetClientRect(hwnd, &infoPtr->client_rect);
6590 TOOLBAR_AutoSize(hwnd);
6591 return 0;
6595 static LRESULT
6596 TOOLBAR_StyleChanged (HWND hwnd, INT nType, const STYLESTRUCT *lpStyle)
6598 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6600 if (nType == GWL_STYLE)
6602 DWORD dwOldStyle = infoPtr->dwStyle;
6604 if (lpStyle->styleNew & TBSTYLE_LIST)
6605 infoPtr->dwDTFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
6606 else
6607 infoPtr->dwDTFlags = DT_CENTER | DT_END_ELLIPSIS;
6609 TOOLBAR_CheckStyle (hwnd, lpStyle->styleNew);
6611 TRACE("new style 0x%08x\n", lpStyle->styleNew);
6613 infoPtr->dwStyle = lpStyle->styleNew;
6615 if ((dwOldStyle ^ lpStyle->styleNew) & (TBSTYLE_WRAPABLE | CCS_VERT))
6616 TOOLBAR_LayoutToolbar(hwnd);
6618 /* only resize if one of the CCS_* styles was changed */
6619 if ((dwOldStyle ^ lpStyle->styleNew) & COMMON_STYLES)
6621 TOOLBAR_AutoSize (hwnd);
6623 InvalidateRect(hwnd, NULL, TRUE);
6627 return 0;
6631 static LRESULT
6632 TOOLBAR_SysColorChange (HWND hwnd)
6634 COMCTL32_RefreshSysColors();
6636 return 0;
6640 /* update theme after a WM_THEMECHANGED message */
6641 static LRESULT theme_changed (HWND hwnd)
6643 HTHEME theme = GetWindowTheme (hwnd);
6644 CloseThemeData (theme);
6645 OpenThemeData (hwnd, themeClass);
6646 return 0;
6650 static LRESULT WINAPI
6651 ToolbarWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6653 TOOLBAR_INFO *infoPtr = TOOLBAR_GetInfoPtr (hwnd);
6655 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
6656 hwnd, uMsg, /* SPY_GetMsgName(uMsg), */ wParam, lParam);
6658 if (!infoPtr && (uMsg != WM_NCCREATE))
6659 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
6661 switch (uMsg)
6663 case TB_ADDBITMAP:
6664 return TOOLBAR_AddBitmap (hwnd, wParam, lParam);
6666 case TB_ADDBUTTONSA:
6667 return TOOLBAR_AddButtonsT(hwnd, wParam, lParam, FALSE);
6669 case TB_ADDBUTTONSW:
6670 return TOOLBAR_AddButtonsT(hwnd, wParam, lParam, TRUE);
6672 case TB_ADDSTRINGA:
6673 return TOOLBAR_AddStringA (hwnd, wParam, lParam);
6675 case TB_ADDSTRINGW:
6676 return TOOLBAR_AddStringW (hwnd, wParam, lParam);
6678 case TB_AUTOSIZE:
6679 return TOOLBAR_AutoSize (hwnd);
6681 case TB_BUTTONCOUNT:
6682 return TOOLBAR_ButtonCount (hwnd, wParam, lParam);
6684 case TB_BUTTONSTRUCTSIZE:
6685 return TOOLBAR_ButtonStructSize (hwnd, wParam, lParam);
6687 case TB_CHANGEBITMAP:
6688 return TOOLBAR_ChangeBitmap (hwnd, wParam, lParam);
6690 case TB_CHECKBUTTON:
6691 return TOOLBAR_CheckButton (hwnd, wParam, lParam);
6693 case TB_COMMANDTOINDEX:
6694 return TOOLBAR_CommandToIndex (hwnd, wParam, lParam);
6696 case TB_CUSTOMIZE:
6697 return TOOLBAR_Customize (hwnd);
6699 case TB_DELETEBUTTON:
6700 return TOOLBAR_DeleteButton (hwnd, wParam, lParam);
6702 case TB_ENABLEBUTTON:
6703 return TOOLBAR_EnableButton (hwnd, wParam, lParam);
6705 case TB_GETANCHORHIGHLIGHT:
6706 return TOOLBAR_GetAnchorHighlight (hwnd);
6708 case TB_GETBITMAP:
6709 return TOOLBAR_GetBitmap (hwnd, wParam, lParam);
6711 case TB_GETBITMAPFLAGS:
6712 return TOOLBAR_GetBitmapFlags (hwnd, wParam, lParam);
6714 case TB_GETBUTTON:
6715 return TOOLBAR_GetButton (hwnd, wParam, lParam);
6717 case TB_GETBUTTONINFOA:
6718 return TOOLBAR_GetButtonInfoT(hwnd, wParam, lParam, FALSE);
6720 case TB_GETBUTTONINFOW:
6721 return TOOLBAR_GetButtonInfoT(hwnd, wParam, lParam, TRUE);
6723 case TB_GETBUTTONSIZE:
6724 return TOOLBAR_GetButtonSize (hwnd);
6726 case TB_GETBUTTONTEXTA:
6727 return TOOLBAR_GetButtonTextA (hwnd, wParam, lParam);
6729 case TB_GETBUTTONTEXTW:
6730 return TOOLBAR_GetButtonTextW (hwnd, wParam, lParam);
6732 case TB_GETDISABLEDIMAGELIST:
6733 return TOOLBAR_GetDisabledImageList (hwnd, wParam, lParam);
6735 case TB_GETEXTENDEDSTYLE:
6736 return TOOLBAR_GetExtendedStyle (hwnd);
6738 case TB_GETHOTIMAGELIST:
6739 return TOOLBAR_GetHotImageList (hwnd, wParam, lParam);
6741 case TB_GETHOTITEM:
6742 return TOOLBAR_GetHotItem (hwnd);
6744 case TB_GETIMAGELIST:
6745 return TOOLBAR_GetDefImageList (hwnd, wParam, lParam);
6747 case TB_GETINSERTMARK:
6748 return TOOLBAR_GetInsertMark (hwnd, wParam, lParam);
6750 case TB_GETINSERTMARKCOLOR:
6751 return TOOLBAR_GetInsertMarkColor (hwnd, wParam, lParam);
6753 case TB_GETITEMRECT:
6754 return TOOLBAR_GetItemRect (hwnd, wParam, lParam);
6756 case TB_GETMAXSIZE:
6757 return TOOLBAR_GetMaxSize (hwnd, wParam, lParam);
6759 /* case TB_GETOBJECT: */ /* 4.71 */
6761 case TB_GETPADDING:
6762 return TOOLBAR_GetPadding (hwnd);
6764 case TB_GETRECT:
6765 return TOOLBAR_GetRect (hwnd, wParam, lParam);
6767 case TB_GETROWS:
6768 return TOOLBAR_GetRows (hwnd, wParam, lParam);
6770 case TB_GETSTATE:
6771 return TOOLBAR_GetState (hwnd, wParam, lParam);
6773 case TB_GETSTRINGA:
6774 return TOOLBAR_GetStringA (hwnd, wParam, lParam);
6776 case TB_GETSTRINGW:
6777 return TOOLBAR_GetStringW (hwnd, wParam, lParam);
6779 case TB_GETSTYLE:
6780 return TOOLBAR_GetStyle (hwnd, wParam, lParam);
6782 case TB_GETTEXTROWS:
6783 return TOOLBAR_GetTextRows (hwnd, wParam, lParam);
6785 case TB_GETTOOLTIPS:
6786 return TOOLBAR_GetToolTips (hwnd, wParam, lParam);
6788 case TB_GETUNICODEFORMAT:
6789 return TOOLBAR_GetUnicodeFormat (hwnd, wParam, lParam);
6791 case TB_HIDEBUTTON:
6792 return TOOLBAR_HideButton (hwnd, wParam, lParam);
6794 case TB_HITTEST:
6795 return TOOLBAR_HitTest (hwnd, wParam, lParam);
6797 case TB_INDETERMINATE:
6798 return TOOLBAR_Indeterminate (hwnd, wParam, lParam);
6800 case TB_INSERTBUTTONA:
6801 return TOOLBAR_InsertButtonT(hwnd, wParam, lParam, FALSE);
6803 case TB_INSERTBUTTONW:
6804 return TOOLBAR_InsertButtonT(hwnd, wParam, lParam, TRUE);
6806 /* case TB_INSERTMARKHITTEST: */ /* 4.71 */
6808 case TB_ISBUTTONCHECKED:
6809 return TOOLBAR_IsButtonChecked (hwnd, wParam, lParam);
6811 case TB_ISBUTTONENABLED:
6812 return TOOLBAR_IsButtonEnabled (hwnd, wParam, lParam);
6814 case TB_ISBUTTONHIDDEN:
6815 return TOOLBAR_IsButtonHidden (hwnd, wParam, lParam);
6817 case TB_ISBUTTONHIGHLIGHTED:
6818 return TOOLBAR_IsButtonHighlighted (hwnd, wParam, lParam);
6820 case TB_ISBUTTONINDETERMINATE:
6821 return TOOLBAR_IsButtonIndeterminate (hwnd, wParam, lParam);
6823 case TB_ISBUTTONPRESSED:
6824 return TOOLBAR_IsButtonPressed (hwnd, wParam, lParam);
6826 case TB_LOADIMAGES:
6827 return TOOLBAR_LoadImages (hwnd, wParam, lParam);
6829 case TB_MAPACCELERATORA:
6830 case TB_MAPACCELERATORW:
6831 return TOOLBAR_MapAccelerator (hwnd, wParam, lParam);
6833 case TB_MARKBUTTON:
6834 return TOOLBAR_MarkButton (hwnd, wParam, lParam);
6836 case TB_MOVEBUTTON:
6837 return TOOLBAR_MoveButton (hwnd, wParam, lParam);
6839 case TB_PRESSBUTTON:
6840 return TOOLBAR_PressButton (hwnd, wParam, lParam);
6842 case TB_REPLACEBITMAP:
6843 return TOOLBAR_ReplaceBitmap (hwnd, wParam, lParam);
6845 case TB_SAVERESTOREA:
6846 return TOOLBAR_SaveRestoreA (hwnd, wParam, (LPTBSAVEPARAMSA)lParam);
6848 case TB_SAVERESTOREW:
6849 return TOOLBAR_SaveRestoreW (hwnd, wParam, (LPTBSAVEPARAMSW)lParam);
6851 case TB_SETANCHORHIGHLIGHT:
6852 return TOOLBAR_SetAnchorHighlight (hwnd, wParam);
6854 case TB_SETBITMAPSIZE:
6855 return TOOLBAR_SetBitmapSize (hwnd, wParam, lParam);
6857 case TB_SETBUTTONINFOA:
6858 return TOOLBAR_SetButtonInfoA (hwnd, wParam, lParam);
6860 case TB_SETBUTTONINFOW:
6861 return TOOLBAR_SetButtonInfoW (hwnd, wParam, lParam);
6863 case TB_SETBUTTONSIZE:
6864 return TOOLBAR_SetButtonSize (hwnd, wParam, lParam);
6866 case TB_SETBUTTONWIDTH:
6867 return TOOLBAR_SetButtonWidth (hwnd, wParam, lParam);
6869 case TB_SETCMDID:
6870 return TOOLBAR_SetCmdId (hwnd, wParam, lParam);
6872 case TB_SETDISABLEDIMAGELIST:
6873 return TOOLBAR_SetDisabledImageList (hwnd, wParam, lParam);
6875 case TB_SETDRAWTEXTFLAGS:
6876 return TOOLBAR_SetDrawTextFlags (hwnd, wParam, lParam);
6878 case TB_SETEXTENDEDSTYLE:
6879 return TOOLBAR_SetExtendedStyle (hwnd, wParam, lParam);
6881 case TB_SETHOTIMAGELIST:
6882 return TOOLBAR_SetHotImageList (hwnd, wParam, lParam);
6884 case TB_SETHOTITEM:
6885 return TOOLBAR_SetHotItem (hwnd, wParam);
6887 case TB_SETIMAGELIST:
6888 return TOOLBAR_SetImageList (hwnd, wParam, lParam);
6890 case TB_SETINDENT:
6891 return TOOLBAR_SetIndent (hwnd, wParam, lParam);
6893 case TB_SETINSERTMARK:
6894 return TOOLBAR_SetInsertMark (hwnd, wParam, lParam);
6896 case TB_SETINSERTMARKCOLOR:
6897 return TOOLBAR_SetInsertMarkColor (hwnd, wParam, lParam);
6899 case TB_SETMAXTEXTROWS:
6900 return TOOLBAR_SetMaxTextRows (hwnd, wParam, lParam);
6902 case TB_SETPADDING:
6903 return TOOLBAR_SetPadding (hwnd, wParam, lParam);
6905 case TB_SETPARENT:
6906 return TOOLBAR_SetParent (hwnd, wParam, lParam);
6908 case TB_SETROWS:
6909 return TOOLBAR_SetRows (hwnd, wParam, lParam);
6911 case TB_SETSTATE:
6912 return TOOLBAR_SetState (hwnd, wParam, lParam);
6914 case TB_SETSTYLE:
6915 return TOOLBAR_SetStyle (hwnd, wParam, lParam);
6917 case TB_SETTOOLTIPS:
6918 return TOOLBAR_SetToolTips (hwnd, wParam, lParam);
6920 case TB_SETUNICODEFORMAT:
6921 return TOOLBAR_SetUnicodeFormat (hwnd, wParam, lParam);
6923 case TB_UNKWN45D:
6924 return TOOLBAR_Unkwn45D(hwnd, wParam, lParam);
6926 case TB_UNKWN45E:
6927 return TOOLBAR_Unkwn45E (hwnd, wParam, lParam);
6929 case TB_UNKWN460:
6930 return TOOLBAR_Unkwn460(hwnd, wParam, lParam);
6932 case TB_UNKWN462:
6933 return TOOLBAR_Unkwn462(hwnd, wParam, lParam);
6935 case TB_UNKWN463:
6936 return TOOLBAR_Unkwn463 (hwnd, wParam, lParam);
6938 case TB_UNKWN464:
6939 return TOOLBAR_Unkwn464(hwnd, wParam, lParam);
6941 /* Common Control Messages */
6943 /* case TB_GETCOLORSCHEME: */ /* identical to CCM_ */
6944 case CCM_GETCOLORSCHEME:
6945 return TOOLBAR_GetColorScheme (hwnd, (LPCOLORSCHEME)lParam);
6947 /* case TB_SETCOLORSCHEME: */ /* identical to CCM_ */
6948 case CCM_SETCOLORSCHEME:
6949 return TOOLBAR_SetColorScheme (hwnd, (LPCOLORSCHEME)lParam);
6951 case CCM_GETVERSION:
6952 return TOOLBAR_GetVersion (hwnd);
6954 case CCM_SETVERSION:
6955 return TOOLBAR_SetVersion (hwnd, (INT)wParam);
6958 /* case WM_CHAR: */
6960 case WM_CREATE:
6961 return TOOLBAR_Create (hwnd, wParam, lParam);
6963 case WM_DESTROY:
6964 return TOOLBAR_Destroy (hwnd, wParam, lParam);
6966 case WM_ERASEBKGND:
6967 return TOOLBAR_EraseBackground (hwnd, wParam, lParam);
6969 case WM_GETFONT:
6970 return TOOLBAR_GetFont (hwnd, wParam, lParam);
6972 case WM_KEYDOWN:
6973 return TOOLBAR_KeyDown (hwnd, wParam, lParam);
6975 /* case WM_KILLFOCUS: */
6977 case WM_LBUTTONDBLCLK:
6978 return TOOLBAR_LButtonDblClk (hwnd, wParam, lParam);
6980 case WM_LBUTTONDOWN:
6981 return TOOLBAR_LButtonDown (hwnd, wParam, lParam);
6983 case WM_LBUTTONUP:
6984 return TOOLBAR_LButtonUp (hwnd, wParam, lParam);
6986 case WM_RBUTTONUP:
6987 return TOOLBAR_RButtonUp (hwnd, wParam, lParam);
6989 case WM_RBUTTONDBLCLK:
6990 return TOOLBAR_RButtonDblClk (hwnd, wParam, lParam);
6992 case WM_MOUSEMOVE:
6993 return TOOLBAR_MouseMove (hwnd, wParam, lParam);
6995 case WM_MOUSELEAVE:
6996 return TOOLBAR_MouseLeave (hwnd, wParam, lParam);
6998 case WM_CAPTURECHANGED:
6999 return TOOLBAR_CaptureChanged(hwnd);
7001 case WM_NCACTIVATE:
7002 return TOOLBAR_NCActivate (hwnd, wParam, lParam);
7004 case WM_NCCALCSIZE:
7005 return TOOLBAR_NCCalcSize (hwnd, wParam, lParam);
7007 case WM_NCCREATE:
7008 return TOOLBAR_NCCreate (hwnd, wParam, lParam);
7010 case WM_NCPAINT:
7011 return TOOLBAR_NCPaint (hwnd, wParam, lParam);
7013 case WM_NOTIFY:
7014 return TOOLBAR_Notify (hwnd, wParam, lParam);
7016 case WM_NOTIFYFORMAT:
7017 return TOOLBAR_NotifyFormat (infoPtr, wParam, lParam);
7019 case WM_PRINTCLIENT:
7020 case WM_PAINT:
7021 return TOOLBAR_Paint (hwnd, wParam);
7023 case WM_SETFOCUS:
7024 return TOOLBAR_SetFocus (hwnd, wParam);
7026 case WM_SETFONT:
7027 return TOOLBAR_SetFont(hwnd, wParam, lParam);
7029 case WM_SETREDRAW:
7030 return TOOLBAR_SetRedraw (hwnd, wParam, lParam);
7032 case WM_SIZE:
7033 return TOOLBAR_Size (hwnd, wParam, lParam);
7035 case WM_STYLECHANGED:
7036 return TOOLBAR_StyleChanged (hwnd, (INT)wParam, (LPSTYLESTRUCT)lParam);
7038 case WM_SYSCOLORCHANGE:
7039 return TOOLBAR_SysColorChange (hwnd);
7041 case WM_THEMECHANGED:
7042 return theme_changed (hwnd);
7044 /* case WM_WININICHANGE: */
7046 case WM_CHARTOITEM:
7047 case WM_COMMAND:
7048 case WM_DRAWITEM:
7049 case WM_MEASUREITEM:
7050 case WM_VKEYTOITEM:
7051 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
7053 /* We see this in Outlook Express 5.x and just does DefWindowProc */
7054 case PGM_FORWARDMOUSE:
7055 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
7057 default:
7058 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
7059 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
7060 uMsg, wParam, lParam);
7061 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
7066 VOID
7067 TOOLBAR_Register (void)
7069 WNDCLASSW wndClass;
7071 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
7072 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
7073 wndClass.lpfnWndProc = ToolbarWindowProc;
7074 wndClass.cbClsExtra = 0;
7075 wndClass.cbWndExtra = sizeof(TOOLBAR_INFO *);
7076 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
7077 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
7078 wndClass.lpszClassName = TOOLBARCLASSNAMEW;
7080 RegisterClassW (&wndClass);
7084 VOID
7085 TOOLBAR_Unregister (void)
7087 UnregisterClassW (TOOLBARCLASSNAMEW, NULL);
7090 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id)
7092 HIMAGELIST himlold;
7093 PIMLENTRY c = NULL;
7095 /* Check if the entry already exists */
7096 c = TOOLBAR_GetImageListEntry(*pies, *cies, id);
7098 /* If this is a new entry we must create it and insert into the array */
7099 if (!c)
7101 PIMLENTRY *pnies;
7103 c = (PIMLENTRY) Alloc(sizeof(IMLENTRY));
7104 c->id = id;
7106 pnies = Alloc((*cies + 1) * sizeof(PIMLENTRY));
7107 memcpy(pnies, *pies, ((*cies) * sizeof(PIMLENTRY)));
7108 pnies[*cies] = c;
7109 (*cies)++;
7111 Free(*pies);
7112 *pies = pnies;
7115 himlold = c->himl;
7116 c->himl = himl;
7118 return himlold;
7122 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies)
7124 int i;
7126 for (i = 0; i < *cies; i++)
7127 Free((*pies)[i]);
7129 Free(*pies);
7131 *cies = 0;
7132 *pies = NULL;
7136 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id)
7138 PIMLENTRY c = NULL;
7140 if (pies != NULL)
7142 int i;
7144 for (i = 0; i < cies; i++)
7146 if (pies[i]->id == id)
7148 c = pies[i];
7149 break;
7154 return c;
7158 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id)
7160 HIMAGELIST himlDef = 0;
7161 PIMLENTRY pie = TOOLBAR_GetImageListEntry(pies, cies, id);
7163 if (pie)
7164 himlDef = pie->himl;
7166 return himlDef;
7170 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb)
7172 if (infoPtr->bUnicode)
7173 return TOOLBAR_SendNotify(&nmtb->hdr, infoPtr, TBN_GETBUTTONINFOW);
7174 else
7176 CHAR Buffer[256];
7177 NMTOOLBARA nmtba;
7178 BOOL bRet = FALSE;
7180 nmtba.iItem = nmtb->iItem;
7181 nmtba.pszText = Buffer;
7182 nmtba.cchText = 256;
7183 ZeroMemory(nmtba.pszText, nmtba.cchText);
7185 if (TOOLBAR_SendNotify(&nmtba.hdr, infoPtr, TBN_GETBUTTONINFOA))
7187 int ccht = strlen(nmtba.pszText);
7188 if (ccht)
7189 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)nmtba.pszText, -1,
7190 nmtb->pszText, nmtb->cchText);
7192 memcpy(&nmtb->tbButton, &nmtba.tbButton, sizeof(TBBUTTON));
7193 bRet = TRUE;
7196 return bRet;
7201 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, PCUSTOMBUTTON btnInfo)
7203 NMTOOLBARW nmtb;
7205 /* MSDN states that iItem is the index of the button, rather than the
7206 * command ID as used by every other NMTOOLBAR notification */
7207 nmtb.iItem = iItem;
7208 memcpy(&nmtb.tbButton, &btnInfo->btn, sizeof(TBBUTTON));
7210 return TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYDELETE);