[COMCTL32]
[reactos.git] / reactos / dll / win32 / comctl32 / toolbar.c
1 /*
2 * Toolbar control
3 *
4 * Copyright 1998,1999 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2004 Robert Shearman
7 *
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.
12 *
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.
17 *
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
21 *
22 * NOTES
23 *
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.
26 *
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.
30 *
31 * TODO:
32 * - Styles:
33 * - TBSTYLE_REGISTERDROP
34 * - TBSTYLE_EX_DOUBLEBUFFER
35 * - Messages:
36 * - TB_GETOBJECT
37 * - TB_INSERTMARKHITTEST
38 * - TB_SAVERESTORE
39 * - WM_WININICHANGE
40 * - Notifications:
41 * - NM_CHAR
42 * - TBN_GETOBJECT
43 * - TBN_SAVE
44 * - Button wrapping (under construction).
45 * - Fix TB_SETROWS and Separators.
46 * - iListGap custom draw support.
47 *
48 * Testing:
49 * - Run tests using Waite Group Windows95 API Bible Volume 2.
50 * The second cdrom contains executables addstr.exe, btncount.exe,
51 * btnstate.exe, butstrsz.exe, chkbtn.exe, chngbmp.exe, customiz.exe,
52 * enablebtn.exe, getbmp.exe, getbtn.exe, getflags.exe, hidebtn.exe,
53 * indetbtn.exe, insbtn.exe, pressbtn.exe, setbtnsz.exe, setcmdid.exe,
54 * setparnt.exe, setrows.exe, toolwnd.exe.
55 * - Microsoft's controlspy examples.
56 * - Charles Petzold's 'Programming Windows': gadgets.exe
57 *
58 * Differences between MSDN and actual native control operation:
59 * 1. MSDN says: "TBSTYLE_LIST: Creates a flat toolbar with button text
60 * to the right of the bitmap. Otherwise, this style is
61 * identical to TBSTYLE_FLAT."
62 * As implemented by both v4.71 and v5.80 of the native COMCTL32.DLL
63 * you can create a TBSTYLE_LIST without TBSTYLE_FLAT and the result
64 * is non-flat non-transparent buttons. Therefore TBSTYLE_LIST does
65 * *not* imply TBSTYLE_FLAT as documented. (GA 8/2001)
66 *
67 */
68
69 #include "comctl32.h"
70
71 WINE_DEFAULT_DEBUG_CHANNEL(toolbar);
72
73 static HCURSOR hCursorDrag = NULL;
74
75 typedef struct
76 {
77 INT iBitmap;
78 INT idCommand;
79 BYTE fsState;
80 BYTE fsStyle;
81 BOOL bHot;
82 BOOL bDropDownPressed;
83 DWORD_PTR dwData;
84 INT_PTR iString;
85 INT nRow;
86 RECT rect;
87 INT cx; /* manually set size */
88 } TBUTTON_INFO;
89
90 typedef struct
91 {
92 UINT nButtons;
93 HINSTANCE hInst;
94 UINT nID;
95 } TBITMAP_INFO;
96
97 typedef struct
98 {
99 HIMAGELIST himl;
100 INT id;
101 } IMLENTRY, *PIMLENTRY;
102
103 typedef struct
104 {
105 DWORD dwStructSize; /* size of TBBUTTON struct */
106 RECT client_rect;
107 RECT rcBound; /* bounding rectangle */
108 INT nButtonHeight;
109 INT nButtonWidth;
110 INT nBitmapHeight;
111 INT nBitmapWidth;
112 INT nIndent;
113 INT nRows; /* number of button rows */
114 INT nMaxTextRows; /* maximum number of text rows */
115 INT cxMin; /* minimum button width */
116 INT cxMax; /* maximum button width */
117 INT nNumButtons; /* number of buttons */
118 INT nNumBitmaps; /* number of bitmaps */
119 INT nNumStrings; /* number of strings */
120 INT nNumBitmapInfos;
121 INT nButtonDown; /* toolbar button being pressed or -1 if none */
122 INT nButtonDrag; /* toolbar button being dragged or -1 if none */
123 INT nOldHit;
124 INT nHotItem; /* index of the "hot" item */
125 SIZE szPadding; /* padding values around button */
126 #ifdef __REACTOS__
127 SIZE szBarPadding; /* padding values around the toolbar (NOT USED BUT STORED) */
128 SIZE szSpacing; /* spacing values between buttons */
129 #endif
130 INT iTopMargin; /* the top margin */
131 INT iListGap; /* default gap between text and image for toolbar with list style */
132 HFONT hDefaultFont;
133 HFONT hFont; /* text font */
134 HIMAGELIST himlInt; /* image list created internally */
135 PIMLENTRY *himlDef; /* default image list array */
136 INT cimlDef; /* default image list array count */
137 PIMLENTRY *himlHot; /* hot image list array */
138 INT cimlHot; /* hot image list array count */
139 PIMLENTRY *himlDis; /* disabled image list array */
140 INT cimlDis; /* disabled image list array count */
141 HWND hwndToolTip; /* handle to tool tip control */
142 HWND hwndNotify; /* handle to the window that gets notifications */
143 HWND hwndSelf; /* my own handle */
144 BOOL bAnchor; /* anchor highlight enabled */
145 BOOL bDoRedraw; /* Redraw status */
146 BOOL bDragOutSent; /* has TBN_DRAGOUT notification been sent for this drag? */
147 BOOL bUnicode; /* Notifications are ASCII (FALSE) or Unicode (TRUE)? */
148 BOOL bCaptured; /* mouse captured? */
149 DWORD dwStyle; /* regular toolbar style */
150 DWORD dwExStyle; /* extended toolbar style */
151 DWORD dwDTFlags; /* DrawText flags */
152
153 COLORREF clrInsertMark; /* insert mark color */
154 COLORREF clrBtnHighlight; /* color for Flat Separator */
155 COLORREF clrBtnShadow; /* color for Flag Separator */
156 INT iVersion;
157 LPWSTR pszTooltipText; /* temporary store for a string > 80 characters
158 * for TTN_GETDISPINFOW notification */
159 TBINSERTMARK tbim; /* info on insertion mark */
160 TBUTTON_INFO *buttons; /* pointer to button array */
161 LPWSTR *strings; /* pointer to string array */
162 TBITMAP_INFO *bitmaps;
163 } TOOLBAR_INFO, *PTOOLBAR_INFO;
164
165
166 /* used by customization dialog */
167 typedef struct
168 {
169 PTOOLBAR_INFO tbInfo;
170 HWND tbHwnd;
171 } CUSTDLG_INFO, *PCUSTDLG_INFO;
172
173 typedef struct
174 {
175 TBBUTTON btn;
176 BOOL bVirtual;
177 BOOL bRemovable;
178 WCHAR text[64];
179 } CUSTOMBUTTON, *PCUSTOMBUTTON;
180
181 typedef enum
182 {
183 IMAGE_LIST_DEFAULT,
184 IMAGE_LIST_HOT,
185 IMAGE_LIST_DISABLED
186 } IMAGE_LIST_TYPE;
187
188 #define SEPARATOR_WIDTH 8
189 #define TOP_BORDER 2
190 #define BOTTOM_BORDER 2
191 #define DDARROW_WIDTH 11
192 #define ARROW_HEIGHT 3
193 #define INSERTMARK_WIDTH 2
194
195 /* default padding inside a button */
196 #define DEFPAD_CX 7
197 #define DEFPAD_CY 6
198
199 #ifdef __REACTOS__
200 /* default space between buttons and between rows */
201 #define DEFSPACE_CX 7
202 #define DEFSPACE_CY 6
203 #endif
204
205 #define DEFLISTGAP 4
206
207 /* vertical padding used in list mode when image is present */
208 #ifdef __REACTOS__
209 #define LISTPAD_CY 2
210 #else
211 #define LISTPAD_CY 9
212 #endif
213
214 /* how wide to treat the bitmap if it isn't present */
215 #define NONLIST_NOTEXT_OFFSET 2
216
217 #define TOOLBAR_NOWHERE (-1)
218
219 /* Used to find undocumented extended styles */
220 #define TBSTYLE_EX_ALL (TBSTYLE_EX_DRAWDDARROWS | \
221 TBSTYLE_EX_VERTICAL | \
222 TBSTYLE_EX_MIXEDBUTTONS | \
223 TBSTYLE_EX_DOUBLEBUFFER | \
224 TBSTYLE_EX_HIDECLIPPEDBUTTONS)
225
226 /* all of the CCS_ styles */
227 #define COMMON_STYLES (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM|CCS_NORESIZE| \
228 CCS_NOPARENTALIGN|CCS_ADJUSTABLE|CCS_NODIVIDER|CCS_VERT)
229
230 #define GETIBITMAP(infoPtr, i) (infoPtr->iVersion >= 5 ? LOWORD(i) : i)
231 #define GETHIMLID(infoPtr, i) (infoPtr->iVersion >= 5 ? HIWORD(i) : 0)
232 #define GETDEFIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDef, infoPtr->cimlDef, id)
233 #define GETHOTIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlHot, infoPtr->cimlHot, id)
234 #define GETDISIMAGELIST(infoPtr, id) TOOLBAR_GetImageList(infoPtr->himlDis, infoPtr->cimlDis, id)
235
236 static const WCHAR themeClass[] = { 'T','o','o','l','b','a','r',0 };
237
238 static BOOL TOOLBAR_GetButtonInfo(const TOOLBAR_INFO *infoPtr, NMTOOLBARW *nmtb);
239 static BOOL TOOLBAR_IsButtonRemovable(const TOOLBAR_INFO *infoPtr, int iItem, const CUSTOMBUTTON *btnInfo);
240 static HIMAGELIST TOOLBAR_GetImageList(const PIMLENTRY *pies, INT cies, INT id);
241 static PIMLENTRY TOOLBAR_GetImageListEntry(const PIMLENTRY *pies, INT cies, INT id);
242 static VOID TOOLBAR_DeleteImageList(PIMLENTRY **pies, INT *cies);
243 static HIMAGELIST TOOLBAR_InsertImageList(PIMLENTRY **pies, INT *cies, HIMAGELIST himl, INT id);
244 static LRESULT TOOLBAR_LButtonDown(TOOLBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam);
245 static void TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr);
246 static LRESULT TOOLBAR_AutoSize(TOOLBAR_INFO *infoPtr);
247 static void TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr);
248 static void TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
249 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button);
250 static LRESULT TOOLBAR_SetButtonInfo(TOOLBAR_INFO *infoPtr, INT Id,
251 const TBBUTTONINFOW *lptbbi, BOOL isW);
252
253
254 static inline int default_top_margin(const TOOLBAR_INFO *infoPtr)
255 {
256 #ifndef __REACTOS__
257 return (infoPtr->dwStyle & TBSTYLE_FLAT ? 0 : TOP_BORDER);
258 #else /* r65766 */
259 /* This is the behaviour in comctl32 v6 */
260 return 0;
261 #endif
262 }
263
264 static inline BOOL TOOLBAR_HasDropDownArrows(DWORD exStyle)
265 {
266 return (exStyle & TBSTYLE_EX_DRAWDDARROWS) != 0;
267 }
268
269 static inline BOOL button_has_ddarrow(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
270 {
271 return (TOOLBAR_HasDropDownArrows( infoPtr->dwExStyle ) && (btnPtr->fsStyle & BTNS_DROPDOWN)) ||
272 (btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
273 }
274
275 static LPWSTR
276 TOOLBAR_GetText(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr)
277 {
278 LPWSTR lpText = NULL;
279
280 /* NOTE: iString == -1 is undocumented */
281 if (!IS_INTRESOURCE(btnPtr->iString) && (btnPtr->iString != -1))
282 lpText = (LPWSTR)btnPtr->iString;
283 else if ((btnPtr->iString >= 0) && (btnPtr->iString < infoPtr->nNumStrings))
284 lpText = infoPtr->strings[btnPtr->iString];
285
286 return lpText;
287 }
288
289 static void
290 TOOLBAR_DumpTBButton(const TBBUTTON *tbb, BOOL fUnicode)
291 {
292 TRACE("TBBUTTON: id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx (%s)\n",
293 tbb->idCommand,tbb->iBitmap, tbb->fsState, tbb->fsStyle, tbb->dwData, tbb->iString,
294 (fUnicode ? wine_dbgstr_w((LPWSTR)tbb->iString) : wine_dbgstr_a((LPSTR)tbb->iString)));
295 }
296
297 static void
298 TOOLBAR_DumpButton(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *bP, INT btn_num)
299 {
300 if (TRACE_ON(toolbar)){
301 TRACE("button %d id %d, bitmap=%d, state=%02x, style=%02x, data=%08lx, stringid=0x%08lx\n",
302 btn_num, bP->idCommand, GETIBITMAP(infoPtr, bP->iBitmap),
303 bP->fsState, bP->fsStyle, bP->dwData, bP->iString);
304 TRACE("string %s\n", debugstr_w(TOOLBAR_GetText(infoPtr,bP)));
305 TRACE("button %d id %d, hot=%s, row=%d, rect=(%s)\n",
306 btn_num, bP->idCommand, (bP->bHot) ? "TRUE":"FALSE", bP->nRow,
307 wine_dbgstr_rect(&bP->rect));
308 }
309 }
310
311
312 static void
313 TOOLBAR_DumpToolbar(const TOOLBAR_INFO *iP, INT line)
314 {
315 if (TRACE_ON(toolbar)) {
316 INT i;
317
318 TRACE("toolbar %p at line %d, exStyle=%08x, buttons=%d, bitmaps=%d, strings=%d, style=%08x\n",
319 iP->hwndSelf, line,
320 iP->dwExStyle, iP->nNumButtons, iP->nNumBitmaps,
321 iP->nNumStrings, iP->dwStyle);
322 TRACE("toolbar %p at line %d, himlInt=%p, himlDef=%p, himlHot=%p, himlDis=%p, redrawable=%s\n",
323 iP->hwndSelf, line,
324 iP->himlInt, iP->himlDef, iP->himlHot, iP->himlDis,
325 (iP->bDoRedraw) ? "TRUE" : "FALSE");
326 for(i=0; i<iP->nNumButtons; i++) {
327 TOOLBAR_DumpButton(iP, &iP->buttons[i], i);
328 }
329 }
330 }
331
332 static inline BOOL
333 TOOLBAR_ButtonHasString(const TBUTTON_INFO *btnPtr)
334 {
335 return HIWORD(btnPtr->iString) && btnPtr->iString != -1;
336 }
337
338 static void set_string_index( TBUTTON_INFO *btn, INT_PTR str, BOOL unicode )
339 {
340 if (!IS_INTRESOURCE( str ) && str != -1)
341 {
342 if (!TOOLBAR_ButtonHasString( btn )) btn->iString = 0;
343
344 if (unicode)
345 Str_SetPtrW( (WCHAR **)&btn->iString, (WCHAR *)str );
346 else
347 Str_SetPtrAtoW( (WCHAR **)&btn->iString, (char *)str );
348 }
349 else
350 {
351 if (TOOLBAR_ButtonHasString( btn )) Free( (WCHAR *)btn->iString );
352
353 btn->iString = str;
354 }
355 }
356
357 static void set_stringT( TBUTTON_INFO *btn, const WCHAR *str, BOOL unicode )
358 {
359 if (IS_INTRESOURCE( (DWORD_PTR)str ) || (DWORD_PTR)str == -1) return;
360 set_string_index( btn, (DWORD_PTR)str, unicode );
361 }
362
363 static void free_string( TBUTTON_INFO *btn )
364 {
365 set_string_index( btn, 0, TRUE );
366
367 }
368
369 /***********************************************************************
370 * TOOLBAR_CheckStyle
371 *
372 * This function validates that the styles set are implemented and
373 * issues FIXMEs warning of possible problems. In a perfect world this
374 * function should be null.
375 */
376 static void
377 TOOLBAR_CheckStyle (const TOOLBAR_INFO *infoPtr)
378 {
379 if (infoPtr->dwStyle & TBSTYLE_REGISTERDROP)
380 FIXME("[%p] TBSTYLE_REGISTERDROP not implemented\n", infoPtr->hwndSelf);
381 }
382
383
384 static INT
385 TOOLBAR_SendNotify (NMHDR *nmhdr, const TOOLBAR_INFO *infoPtr, UINT code)
386 {
387 if(!IsWindow(infoPtr->hwndSelf))
388 return 0; /* we have just been destroyed */
389
390 nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
391 nmhdr->hwndFrom = infoPtr->hwndSelf;
392 nmhdr->code = code;
393
394 TRACE("to window %p, code=%08x, %s\n", infoPtr->hwndNotify, code,
395 (infoPtr->bUnicode) ? "via Unicode" : "via ANSI");
396
397 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
398 }
399
400 /***********************************************************************
401 * TOOLBAR_GetBitmapIndex
402 *
403 * This function returns the bitmap index associated with a button.
404 * If the button specifies I_IMAGECALLBACK, then the TBN_GETDISPINFO
405 * is issued to retrieve the index.
406 */
407 static INT
408 TOOLBAR_GetBitmapIndex(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr)
409 {
410 INT ret = btnPtr->iBitmap;
411
412 if (ret == I_IMAGECALLBACK)
413 {
414 /* issue TBN_GETDISPINFO */
415 NMTBDISPINFOW nmgd;
416
417 memset(&nmgd, 0, sizeof(nmgd));
418 nmgd.idCommand = btnPtr->idCommand;
419 nmgd.lParam = btnPtr->dwData;
420 nmgd.dwMask = TBNF_IMAGE;
421 nmgd.iImage = -1;
422 /* Windows also send TBN_GETDISPINFOW even if the control is ANSI */
423 TOOLBAR_SendNotify(&nmgd.hdr, infoPtr, TBN_GETDISPINFOW);
424 if (nmgd.dwMask & TBNF_DI_SETITEM)
425 btnPtr->iBitmap = nmgd.iImage;
426 ret = nmgd.iImage;
427 TRACE("TBN_GETDISPINFO returned bitmap id %d, mask=%08x, nNumBitmaps=%d\n",
428 ret, nmgd.dwMask, infoPtr->nNumBitmaps);
429 }
430
431 if (ret != I_IMAGENONE)
432 ret = GETIBITMAP(infoPtr, ret);
433
434 return ret;
435 }
436
437
438 static BOOL
439 TOOLBAR_IsValidBitmapIndex(const TOOLBAR_INFO *infoPtr, INT index)
440 {
441 HIMAGELIST himl;
442 INT id = GETHIMLID(infoPtr, index);
443 INT iBitmap = GETIBITMAP(infoPtr, index);
444
445 if (((himl = GETDEFIMAGELIST(infoPtr, id)) &&
446 iBitmap >= 0 && iBitmap < ImageList_GetImageCount(himl)) ||
447 (index == I_IMAGECALLBACK))
448 return TRUE;
449 else
450 return FALSE;
451 }
452
453
454 static inline BOOL
455 TOOLBAR_IsValidImageList(const TOOLBAR_INFO *infoPtr, INT index)
456 {
457 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, index));
458 return (himl != NULL) && (ImageList_GetImageCount(himl) > 0);
459 }
460
461
462 /***********************************************************************
463 * TOOLBAR_GetImageListForDrawing
464 *
465 * This function validates the bitmap index (including I_IMAGECALLBACK
466 * functionality) and returns the corresponding image list.
467 */
468 static HIMAGELIST
469 TOOLBAR_GetImageListForDrawing (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr,
470 IMAGE_LIST_TYPE imagelist, INT * index)
471 {
472 HIMAGELIST himl;
473
474 if (!TOOLBAR_IsValidBitmapIndex(infoPtr,btnPtr->iBitmap)) {
475 if (btnPtr->iBitmap == I_IMAGENONE) return NULL;
476 WARN("bitmap for ID %d, index %d is not valid, number of bitmaps in imagelist: %d\n",
477 HIWORD(btnPtr->iBitmap), LOWORD(btnPtr->iBitmap), infoPtr->nNumBitmaps);
478 return NULL;
479 }
480
481 if ((*index = TOOLBAR_GetBitmapIndex(infoPtr, btnPtr)) < 0) {
482 if ((*index == I_IMAGECALLBACK) ||
483 (*index == I_IMAGENONE)) return NULL;
484 ERR("TBN_GETDISPINFO returned invalid index %d\n",
485 *index);
486 return NULL;
487 }
488
489 switch(imagelist)
490 {
491 case IMAGE_LIST_DEFAULT:
492 himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
493 break;
494 case IMAGE_LIST_HOT:
495 himl = GETHOTIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
496 break;
497 case IMAGE_LIST_DISABLED:
498 himl = GETDISIMAGELIST(infoPtr, GETHIMLID(infoPtr, btnPtr->iBitmap));
499 break;
500 default:
501 himl = NULL;
502 FIXME("Shouldn't reach here\n");
503 }
504
505 if (!himl)
506 TRACE("no image list\n");
507
508 return himl;
509 }
510
511
512 static void
513 TOOLBAR_DrawFlatSeparator (const RECT *lpRect, HDC hdc, const TOOLBAR_INFO *infoPtr)
514 {
515 RECT myrect;
516 COLORREF oldcolor, newcolor;
517
518 myrect.left = (lpRect->left + lpRect->right) / 2 - 1;
519 myrect.right = myrect.left + 1;
520 myrect.top = lpRect->top + 2;
521 myrect.bottom = lpRect->bottom - 2;
522
523 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
524 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
525 oldcolor = SetBkColor (hdc, newcolor);
526 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
527
528 myrect.left = myrect.right;
529 myrect.right = myrect.left + 1;
530
531 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
532 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
533 SetBkColor (hdc, newcolor);
534 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
535
536 SetBkColor (hdc, oldcolor);
537 }
538
539
540 /***********************************************************************
541 * TOOLBAR_DrawFlatHorizontalSeparator
542 *
543 * This function draws horizontal separator for toolbars having CCS_VERT style.
544 * In this case, the separator is a pixel high line of COLOR_BTNSHADOW,
545 * followed by a pixel high line of COLOR_BTNHIGHLIGHT. These separators
546 * are horizontal as opposed to the vertical separators for not dropdown
547 * type.
548 *
549 * FIXME: It is possible that the height of each line is really SM_CYBORDER.
550 */
551 static void
552 TOOLBAR_DrawFlatHorizontalSeparator (const RECT *lpRect, HDC hdc,
553 const TOOLBAR_INFO *infoPtr)
554 {
555 RECT myrect;
556 COLORREF oldcolor, newcolor;
557
558 myrect.left = lpRect->left;
559 myrect.right = lpRect->right;
560 myrect.top = lpRect->top + (lpRect->bottom - lpRect->top - 2)/2;
561 myrect.bottom = myrect.top + 1;
562
563 InflateRect (&myrect, -2, 0);
564
565 TRACE("rect=(%s)\n", wine_dbgstr_rect(&myrect));
566
567 newcolor = (infoPtr->clrBtnShadow == CLR_DEFAULT) ?
568 comctl32_color.clrBtnShadow : infoPtr->clrBtnShadow;
569 oldcolor = SetBkColor (hdc, newcolor);
570 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
571
572 myrect.top = myrect.bottom;
573 myrect.bottom = myrect.top + 1;
574
575 newcolor = (infoPtr->clrBtnHighlight == CLR_DEFAULT) ?
576 comctl32_color.clrBtnHighlight : infoPtr->clrBtnHighlight;
577 SetBkColor (hdc, newcolor);
578 ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &myrect, 0, 0, 0);
579
580 SetBkColor (hdc, oldcolor);
581 }
582
583
584 static void
585 TOOLBAR_DrawArrow (HDC hdc, INT left, INT top, COLORREF clr)
586 {
587 INT x, y;
588 HPEN hPen, hOldPen;
589
590 if (!(hPen = CreatePen( PS_SOLID, 1, clr))) return;
591 hOldPen = SelectObject ( hdc, hPen );
592 x = left + 2;
593 y = top;
594 MoveToEx (hdc, x, y, NULL);
595 LineTo (hdc, x+5, y++); x++;
596 MoveToEx (hdc, x, y, NULL);
597 LineTo (hdc, x+3, y++); x++;
598 MoveToEx (hdc, x, y, NULL);
599 LineTo (hdc, x+1, y);
600 SelectObject( hdc, hOldPen );
601 DeleteObject( hPen );
602 }
603
604 /*
605 * Draw the text string for this button.
606 * note: infoPtr->himlDis *SHOULD* be non-zero when infoPtr->himlDef
607 * is non-zero, so we can simply check himlDef to see if we have
608 * an image list
609 */
610 static void
611 TOOLBAR_DrawString (const TOOLBAR_INFO *infoPtr, RECT *rcText, LPCWSTR lpText,
612 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
613 {
614 HDC hdc = tbcd->nmcd.hdc;
615 HFONT hOldFont = 0;
616 COLORREF clrOld = 0;
617 COLORREF clrOldBk = 0;
618 int oldBkMode = 0;
619 UINT state = tbcd->nmcd.uItemState;
620
621 /* draw text */
622 if (lpText && infoPtr->nMaxTextRows > 0) {
623 TRACE("string=%s rect=(%s)\n", debugstr_w(lpText),
624 wine_dbgstr_rect(rcText));
625
626 hOldFont = SelectObject (hdc, infoPtr->hFont);
627 if ((state & CDIS_HOT) && (dwItemCDFlag & TBCDRF_HILITEHOTTRACK )) {
628 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
629 }
630 else if (state & CDIS_DISABLED) {
631 clrOld = SetTextColor (hdc, tbcd->clrBtnHighlight);
632 OffsetRect (rcText, 1, 1);
633 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
634 SetTextColor (hdc, comctl32_color.clr3dShadow);
635 OffsetRect (rcText, -1, -1);
636 }
637 else if (state & CDIS_INDETERMINATE) {
638 clrOld = SetTextColor (hdc, comctl32_color.clr3dShadow);
639 }
640 else if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK)) {
641 clrOld = SetTextColor (hdc, tbcd->clrTextHighlight);
642 clrOldBk = SetBkColor (hdc, tbcd->clrMark);
643 oldBkMode = SetBkMode (hdc, tbcd->nHLStringBkMode);
644 }
645 else {
646 clrOld = SetTextColor (hdc, tbcd->clrText);
647 }
648
649 DrawTextW (hdc, lpText, -1, rcText, infoPtr->dwDTFlags);
650 SetTextColor (hdc, clrOld);
651 if ((state & CDIS_MARKED) && !(dwItemCDFlag & TBCDRF_NOMARK))
652 {
653 SetBkColor (hdc, clrOldBk);
654 SetBkMode (hdc, oldBkMode);
655 }
656 SelectObject (hdc, hOldFont);
657 }
658 }
659
660
661 static void
662 TOOLBAR_DrawPattern (const RECT *lpRect, const NMTBCUSTOMDRAW *tbcd)
663 {
664 HDC hdc = tbcd->nmcd.hdc;
665 HBRUSH hbr = SelectObject (hdc, tbcd->hbrMonoDither);
666 COLORREF clrTextOld;
667 COLORREF clrBkOld;
668 INT cx = lpRect->right - lpRect->left;
669 INT cy = lpRect->bottom - lpRect->top;
670 INT cxEdge = GetSystemMetrics(SM_CXEDGE);
671 INT cyEdge = GetSystemMetrics(SM_CYEDGE);
672 clrTextOld = SetTextColor(hdc, tbcd->clrBtnHighlight);
673 clrBkOld = SetBkColor(hdc, tbcd->clrBtnFace);
674 PatBlt (hdc, lpRect->left + cxEdge, lpRect->top + cyEdge,
675 cx - (2 * cxEdge), cy - (2 * cyEdge), PATCOPY);
676 SetBkColor(hdc, clrBkOld);
677 SetTextColor(hdc, clrTextOld);
678 SelectObject (hdc, hbr);
679 }
680
681
682 static void TOOLBAR_DrawMasked(HIMAGELIST himl, int index, HDC hdc, INT x, INT y, UINT draw_flags)
683 {
684 INT cx, cy;
685 HBITMAP hbmMask, hbmImage;
686 HDC hdcMask, hdcImage;
687
688 ImageList_GetIconSize(himl, &cx, &cy);
689
690 /* Create src image */
691 hdcImage = CreateCompatibleDC(hdc);
692 hbmImage = CreateCompatibleBitmap(hdc, cx, cy);
693 SelectObject(hdcImage, hbmImage);
694 ImageList_DrawEx(himl, index, hdcImage, 0, 0, cx, cy,
695 RGB(0xff, 0xff, 0xff), RGB(0,0,0), draw_flags);
696
697 /* Create Mask */
698 hdcMask = CreateCompatibleDC(0);
699 hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
700 SelectObject(hdcMask, hbmMask);
701
702 /* Remove the background and all white pixels */
703 ImageList_DrawEx(himl, index, hdcMask, 0, 0, cx, cy,
704 RGB(0xff, 0xff, 0xff), RGB(0,0,0), ILD_MASK);
705 SetBkColor(hdcImage, RGB(0xff, 0xff, 0xff));
706 BitBlt(hdcMask, 0, 0, cx, cy, hdcImage, 0, 0, NOTSRCERASE);
707
708 /* draw the new mask 'etched' to hdc */
709 SetBkColor(hdc, RGB(255, 255, 255));
710 SelectObject(hdc, GetSysColorBrush(COLOR_3DHILIGHT));
711 /* E20746 op code is (Dst ^ (Src & (Pat ^ Dst))) */
712 BitBlt(hdc, x + 1, y + 1, cx, cy, hdcMask, 0, 0, 0xE20746);
713 SelectObject(hdc, GetSysColorBrush(COLOR_3DSHADOW));
714 BitBlt(hdc, x, y, cx, cy, hdcMask, 0, 0, 0xE20746);
715
716 /* Cleanup */
717 DeleteObject(hbmImage);
718 DeleteDC(hdcImage);
719 DeleteObject (hbmMask);
720 DeleteDC(hdcMask);
721 }
722
723
724 static UINT
725 TOOLBAR_TranslateState(const TBUTTON_INFO *btnPtr)
726 {
727 UINT retstate = 0;
728
729 retstate |= (btnPtr->fsState & TBSTATE_CHECKED) ? CDIS_CHECKED : 0;
730 retstate |= (btnPtr->fsState & TBSTATE_PRESSED) ? CDIS_SELECTED : 0;
731 retstate |= (btnPtr->fsState & TBSTATE_ENABLED) ? 0 : CDIS_DISABLED;
732 retstate |= (btnPtr->fsState & TBSTATE_MARKED ) ? CDIS_MARKED : 0;
733 retstate |= (btnPtr->bHot ) ? CDIS_HOT : 0;
734 retstate |= ((btnPtr->fsState & (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) == (TBSTATE_ENABLED|TBSTATE_INDETERMINATE)) ? CDIS_INDETERMINATE : 0;
735 /* NOTE: we don't set CDIS_GRAYED, CDIS_FOCUS, CDIS_DEFAULT */
736 return retstate;
737 }
738
739 /* draws the image on a toolbar button */
740 static void
741 TOOLBAR_DrawImage(const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, INT left, INT top,
742 const NMTBCUSTOMDRAW *tbcd, DWORD dwItemCDFlag)
743 {
744 HIMAGELIST himl = NULL;
745 BOOL draw_masked = FALSE, draw_desaturated = FALSE;
746 INT index;
747 INT offset = 0;
748 UINT draw_flags = ILD_TRANSPARENT;
749 #ifdef __REACTOS__
750 IMAGEINFO info = {0};
751 BITMAP bm = {0};
752 #endif
753
754 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
755 {
756 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DISABLED, &index);
757 if (!himl)
758 {
759 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
760
761 #ifdef __REACTOS__
762 ImageList_GetImageInfo(himl, index, &info);
763 GetObjectW(info.hbmImage, sizeof(bm), &bm);
764
765 if (bm.bmBitsPixel == 32)
766 {
767 draw_desaturated = TRUE;
768 }
769 else
770 {
771 draw_masked = TRUE;
772 }
773 #else
774 draw_masked = TRUE;
775 #endif
776 }
777 }
778 else if (tbcd->nmcd.uItemState & CDIS_CHECKED ||
779 ((tbcd->nmcd.uItemState & CDIS_HOT)
780 && ((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))))
781 {
782 /* if hot, attempt to draw with hot image list, if fails,
783 use default image list */
784 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_HOT, &index);
785 if (!himl)
786 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
787 }
788 else
789 himl = TOOLBAR_GetImageListForDrawing(infoPtr, btnPtr, IMAGE_LIST_DEFAULT, &index);
790
791 if (!himl)
792 return;
793
794 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
795 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED)))
796 offset = 1;
797
798 if (!(dwItemCDFlag & TBCDRF_NOMARK) &&
799 (tbcd->nmcd.uItemState & CDIS_MARKED))
800 draw_flags |= ILD_BLEND50;
801
802 TRACE("drawing index=%d, himl=%p, left=%d, top=%d, offset=%d\n",
803 index, himl, left, top, offset);
804
805 if (draw_masked)
806 {
807 /* code path for drawing flat disabled icons without alpha channel */
808 TOOLBAR_DrawMasked (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
809 }
810 else if (draw_desaturated)
811 {
812 /* code path for drawing disabled, alpha-blended (32bpp) icons */
813 IMAGELISTDRAWPARAMS imldp = {0};
814
815 imldp.cbSize = sizeof(imldp);
816 imldp.himl = himl;
817 imldp.i = index;
818 imldp.hdcDst = tbcd->nmcd.hdc,
819 imldp.x = offset + left;
820 imldp.y = offset + top;
821 imldp.rgbBk = CLR_NONE;
822 imldp.rgbFg = CLR_DEFAULT;
823 imldp.fStyle = ILD_TRANSPARENT;
824 imldp.fState = ILS_ALPHA | ILS_SATURATE;
825 imldp.Frame = 192;
826
827 ImageList_DrawIndirect (&imldp);
828 }
829 else
830 {
831 /* code path for drawing standard icons as-is */
832 ImageList_Draw (himl, index, tbcd->nmcd.hdc, left + offset, top + offset, draw_flags);
833 }
834 }
835
836 /* draws a blank frame for a toolbar button */
837 static void
838 TOOLBAR_DrawFrame(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, const RECT *rect, DWORD dwItemCDFlag)
839 {
840 HDC hdc = tbcd->nmcd.hdc;
841 RECT rc = *rect;
842 /* if the state is disabled or indeterminate then the button
843 * cannot have an interactive look like pressed or hot */
844 BOOL non_interactive_state = (tbcd->nmcd.uItemState & CDIS_DISABLED) ||
845 (tbcd->nmcd.uItemState & CDIS_INDETERMINATE);
846 BOOL pressed_look = !non_interactive_state &&
847 ((tbcd->nmcd.uItemState & CDIS_SELECTED) ||
848 (tbcd->nmcd.uItemState & CDIS_CHECKED));
849
850 /* app don't want us to draw any edges */
851 if (dwItemCDFlag & TBCDRF_NOEDGES)
852 return;
853
854 if (infoPtr->dwStyle & TBSTYLE_FLAT)
855 {
856 if (pressed_look)
857 DrawEdge (hdc, &rc, BDR_SUNKENOUTER, BF_RECT);
858 else if ((tbcd->nmcd.uItemState & CDIS_HOT) && !non_interactive_state)
859 DrawEdge (hdc, &rc, BDR_RAISEDINNER, BF_RECT);
860 }
861 else
862 {
863 if (pressed_look)
864 DrawEdge (hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
865 else
866 DrawEdge (hdc, &rc, EDGE_RAISED,
867 BF_SOFT | BF_RECT | BF_MIDDLE);
868 }
869 }
870
871 static void
872 TOOLBAR_DrawSepDDArrow(const TOOLBAR_INFO *infoPtr, const NMTBCUSTOMDRAW *tbcd, RECT *rcArrow, BOOL bDropDownPressed, DWORD dwItemCDFlag)
873 {
874 HDC hdc = tbcd->nmcd.hdc;
875 int offset = 0;
876 BOOL pressed = bDropDownPressed ||
877 (tbcd->nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED));
878
879 if (infoPtr->dwStyle & TBSTYLE_FLAT)
880 {
881 if (pressed)
882 DrawEdge (hdc, rcArrow, BDR_SUNKENOUTER, BF_RECT);
883 else if ( (tbcd->nmcd.uItemState & CDIS_HOT) &&
884 !(tbcd->nmcd.uItemState & CDIS_DISABLED) &&
885 !(tbcd->nmcd.uItemState & CDIS_INDETERMINATE))
886 DrawEdge (hdc, rcArrow, BDR_RAISEDINNER, BF_RECT);
887 }
888 else
889 {
890 if (pressed)
891 DrawEdge (hdc, rcArrow, EDGE_SUNKEN, BF_RECT | BF_MIDDLE);
892 else
893 DrawEdge (hdc, rcArrow, EDGE_RAISED,
894 BF_SOFT | BF_RECT | BF_MIDDLE);
895 }
896
897 if (pressed)
898 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
899
900 if (tbcd->nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
901 {
902 TOOLBAR_DrawArrow(hdc, rcArrow->left+1, rcArrow->top+1 + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
903 TOOLBAR_DrawArrow(hdc, rcArrow->left, rcArrow->top + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
904 }
905 else
906 TOOLBAR_DrawArrow(hdc, rcArrow->left + offset, rcArrow->top + offset + (rcArrow->bottom - rcArrow->top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
907 }
908
909 /* draws a complete toolbar button */
910 static void
911 TOOLBAR_DrawButton (const TOOLBAR_INFO *infoPtr, TBUTTON_INFO *btnPtr, HDC hdc, DWORD dwBaseCustDraw)
912 {
913 DWORD dwStyle = infoPtr->dwStyle;
914 BOOL hasDropDownArrow = button_has_ddarrow( infoPtr, btnPtr );
915 BOOL drawSepDropDownArrow = hasDropDownArrow &&
916 (~btnPtr->fsStyle & BTNS_WHOLEDROPDOWN);
917 RECT rc, rcArrow, rcBitmap, rcText;
918 LPWSTR lpText = NULL;
919 NMTBCUSTOMDRAW tbcd;
920 DWORD ntfret;
921 INT offset;
922 INT oldBkMode;
923 DWORD dwItemCustDraw;
924 DWORD dwItemCDFlag;
925 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
926
927 rc = btnPtr->rect;
928 rcArrow = rc;
929
930 /* separator - doesn't send NM_CUSTOMDRAW */
931 if (btnPtr->fsStyle & BTNS_SEP) {
932 if (theme)
933 {
934 DrawThemeBackground (theme, hdc,
935 (dwStyle & CCS_VERT) ? TP_SEPARATORVERT : TP_SEPARATOR, 0,
936 &rc, NULL);
937 }
938 else
939 /* with the FLAT style, iBitmap is the width and has already */
940 /* been taken into consideration in calculating the width */
941 /* so now we need to draw the vertical separator */
942 /* empirical tests show that iBitmap can/will be non-zero */
943 /* when drawing the vertical bar... */
944 if ((dwStyle & TBSTYLE_FLAT) /* && (btnPtr->iBitmap == 0) */) {
945 if (dwStyle & CCS_VERT) {
946 RECT rcsep = rc;
947 InflateRect(&rcsep, -infoPtr->szPadding.cx, -infoPtr->szPadding.cy);
948 TOOLBAR_DrawFlatHorizontalSeparator (&rcsep, hdc, infoPtr);
949 }
950 else {
951 TOOLBAR_DrawFlatSeparator (&rc, hdc, infoPtr);
952 }
953 }
954 else if (btnPtr->fsStyle != BTNS_SEP) {
955 FIXME("Draw some kind of separator: fsStyle=%x\n",
956 btnPtr->fsStyle);
957 }
958 return;
959 }
960
961 /* get a pointer to the text */
962 lpText = TOOLBAR_GetText(infoPtr, btnPtr);
963
964 if (hasDropDownArrow)
965 {
966 int right;
967
968 if (dwStyle & TBSTYLE_FLAT)
969 right = max(rc.left, rc.right - DDARROW_WIDTH);
970 else
971 right = max(rc.left, rc.right - DDARROW_WIDTH - 2);
972
973 if (drawSepDropDownArrow)
974 rc.right = right;
975
976 rcArrow.left = right;
977 }
978
979 /* copy text & bitmap rects after adjusting for drop-down arrow
980 * so that text & bitmap is centered in the rectangle not containing
981 * the arrow */
982 rcText = rc;
983 rcBitmap = rc;
984
985 /* Center the bitmap horizontally and vertically */
986 if (dwStyle & TBSTYLE_LIST)
987 {
988 if (lpText &&
989 infoPtr->nMaxTextRows > 0 &&
990 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
991 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
992 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->szPadding.cx / 2;
993 else
994 rcBitmap.left += GetSystemMetrics(SM_CXEDGE) + infoPtr->iListGap / 2;
995 }
996 else
997 rcBitmap.left += ((rc.right - rc.left) - infoPtr->nBitmapWidth) / 2;
998
999 rcBitmap.top += infoPtr->szPadding.cy / 2;
1000
1001 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
1002 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
1003 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
1004 TRACE("Text=%s\n", debugstr_w(lpText));
1005 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
1006
1007 /* calculate text position */
1008 if (lpText)
1009 {
1010 InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
1011 if (dwStyle & TBSTYLE_LIST)
1012 {
1013 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
1014 }
1015 else
1016 {
1017 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
1018 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
1019 else
1020 rcText.top += infoPtr->szPadding.cy/2 + 2;
1021 }
1022 }
1023
1024 /* Initialize fields in all cases, because we use these later
1025 * NOTE: applications can and do alter these to customize their
1026 * toolbars */
1027 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1028 tbcd.clrText = comctl32_color.clrBtnText;
1029 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
1030 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
1031 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
1032 tbcd.clrMark = comctl32_color.clrHighlight;
1033 tbcd.clrHighlightHotTrack = 0;
1034 tbcd.nStringBkMode = TRANSPARENT;
1035 tbcd.nHLStringBkMode = OPAQUE;
1036 tbcd.rcText.left = 0;
1037 tbcd.rcText.top = 0;
1038 tbcd.rcText.right = rcText.right - rc.left;
1039 tbcd.rcText.bottom = rcText.bottom - rc.top;
1040 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
1041 tbcd.nmcd.hdc = hdc;
1042 tbcd.nmcd.rc = btnPtr->rect;
1043 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
1044
1045 /* FIXME: what are these used for? */
1046 tbcd.hbrLines = 0;
1047 tbcd.hpenLines = 0;
1048
1049 /* Issue Item Prepaint notify */
1050 dwItemCustDraw = 0;
1051 dwItemCDFlag = 0;
1052 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1053 {
1054 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1055 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1056 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1057 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1058 /* reset these fields so the user can't alter the behaviour like native */
1059 tbcd.nmcd.hdc = hdc;
1060 tbcd.nmcd.rc = btnPtr->rect;
1061
1062 dwItemCustDraw = ntfret & 0xffff;
1063 dwItemCDFlag = ntfret & 0xffff0000;
1064 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1065 return;
1066 /* save the only part of the rect that the user can change */
1067 rcText.right = tbcd.rcText.right + rc.left;
1068 rcText.bottom = tbcd.rcText.bottom + rc.top;
1069 }
1070
1071 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1072 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1073 OffsetRect(&rcText, 1, 1);
1074
1075 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
1076 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1077 TOOLBAR_DrawPattern (&rc, &tbcd);
1078
1079 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
1080 && (tbcd.nmcd.uItemState & CDIS_HOT))
1081 {
1082 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1083 {
1084 COLORREF oldclr;
1085
1086 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1087 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1088 if (hasDropDownArrow)
1089 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1090 SetBkColor(hdc, oldclr);
1091 }
1092 }
1093
1094 #ifdef __REACTOS__
1095 if (theme && !(dwItemCDFlag & TBCDRF_NOBACKGROUND))
1096 #else
1097 if (theme)
1098 #endif
1099 {
1100 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1101 int stateId = TS_NORMAL;
1102
1103 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1104 stateId = TS_DISABLED;
1105 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1106 stateId = TS_PRESSED;
1107 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1108 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1109 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1110 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1111 stateId = TS_HOT;
1112
1113 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1114 }
1115
1116 #ifdef __REACTOS__
1117 if (!theme)
1118 #else
1119 else
1120 #endif
1121 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1122
1123 if (drawSepDropDownArrow)
1124 {
1125 if (theme)
1126 {
1127 int stateId = TS_NORMAL;
1128
1129 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1130 stateId = TS_DISABLED;
1131 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1132 stateId = TS_PRESSED;
1133 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1134 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1135 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1136 stateId = TS_HOT;
1137
1138 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1139 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1140 }
1141 else
1142 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1143 }
1144
1145 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1146 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1147 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1148 SetBkMode (hdc, oldBkMode);
1149
1150 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1151
1152 if (hasDropDownArrow && !drawSepDropDownArrow)
1153 {
1154 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1155 {
1156 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1157 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1158 }
1159 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1160 {
1161 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1162 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1163 }
1164 else
1165 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1166 }
1167
1168 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1169 {
1170 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1171 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1172 }
1173
1174 }
1175
1176
1177 static void
1178 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1179 {
1180 TBUTTON_INFO *btnPtr;
1181 INT i;
1182 RECT rcTemp, rcClient;
1183 NMTBCUSTOMDRAW tbcd;
1184 DWORD ntfret;
1185 DWORD dwBaseCustDraw;
1186
1187 /* the app has told us not to redraw the toolbar */
1188 if (!infoPtr->bDoRedraw)
1189 return;
1190
1191 /* if imagelist belongs to the app, it can be changed
1192 by the app after setting it */
1193 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1194 {
1195 infoPtr->nNumBitmaps = 0;
1196 for (i = 0; i < infoPtr->cimlDef; i++)
1197 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1198 }
1199
1200 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1201
1202 /* change the imagelist icon size if we manage the list and it is necessary */
1203 TOOLBAR_CheckImageListIconSize(infoPtr);
1204
1205 /* Send initial notify */
1206 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1207 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1208 tbcd.nmcd.hdc = hdc;
1209 tbcd.nmcd.rc = ps->rcPaint;
1210 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1211 dwBaseCustDraw = ntfret & 0xffff;
1212
1213 GetClientRect(infoPtr->hwndSelf, &rcClient);
1214
1215 /* redraw necessary buttons */
1216 btnPtr = infoPtr->buttons;
1217 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1218 {
1219 BOOL bDraw;
1220 if (!RectVisible(hdc, &btnPtr->rect))
1221 continue;
1222 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1223 {
1224 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1225 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1226 }
1227 else
1228 bDraw = TRUE;
1229 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1230 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1231 if (bDraw)
1232 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1233 }
1234
1235 /* draw insert mark if required */
1236 if (infoPtr->tbim.iButton != -1)
1237 {
1238 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1239 RECT rcInsertMark;
1240 rcInsertMark.top = rcButton.top;
1241 rcInsertMark.bottom = rcButton.bottom;
1242 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1243 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1244 else
1245 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1246 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1247 }
1248
1249 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1250 {
1251 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1252 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1253 tbcd.nmcd.hdc = hdc;
1254 tbcd.nmcd.rc = ps->rcPaint;
1255 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1256 }
1257 }
1258
1259 /***********************************************************************
1260 * TOOLBAR_MeasureString
1261 *
1262 * This function gets the width and height of a string in pixels. This
1263 * is done first by using GetTextExtentPoint to get the basic width
1264 * and height. The DrawText is called with DT_CALCRECT to get the exact
1265 * width. The reason is because the text may have more than one "&" (or
1266 * prefix characters as M$ likes to call them). The prefix character
1267 * indicates where the underline goes, except for the string "&&" which
1268 * is reduced to a single "&". GetTextExtentPoint does not process these
1269 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1270 */
1271 static void
1272 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1273 HDC hdc, LPSIZE lpSize)
1274 {
1275 RECT myrect;
1276
1277 lpSize->cx = 0;
1278 lpSize->cy = 0;
1279
1280 if (infoPtr->nMaxTextRows > 0 &&
1281 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1282 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1283 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1284 {
1285 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1286
1287 if(lpText != NULL) {
1288 /* first get size of all the text */
1289 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1290
1291 /* feed above size into the rectangle for DrawText */
1292 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1293
1294 /* Use DrawText to get true size as drawn (less pesky "&") */
1295 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1296 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1297 DT_NOPREFIX : 0));
1298
1299 /* feed back to caller */
1300 lpSize->cx = myrect.right;
1301 lpSize->cy = myrect.bottom;
1302 }
1303 }
1304
1305 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1306 }
1307
1308 /***********************************************************************
1309 * TOOLBAR_CalcStrings
1310 *
1311 * This function walks through each string and measures it and returns
1312 * the largest height and width to caller.
1313 */
1314 static void
1315 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1316 {
1317 TBUTTON_INFO *btnPtr;
1318 INT i;
1319 SIZE sz;
1320 HDC hdc;
1321 HFONT hOldFont;
1322
1323 lpSize->cx = 0;
1324 lpSize->cy = 0;
1325
1326 if (infoPtr->nMaxTextRows == 0)
1327 return;
1328
1329 hdc = GetDC (infoPtr->hwndSelf);
1330 hOldFont = SelectObject (hdc, infoPtr->hFont);
1331
1332 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1333 {
1334 TEXTMETRICW tm;
1335
1336 GetTextMetricsW(hdc, &tm);
1337 lpSize->cy = tm.tmHeight;
1338 }
1339
1340 btnPtr = infoPtr->buttons;
1341 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1342 if(TOOLBAR_GetText(infoPtr, btnPtr))
1343 {
1344 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1345 if (sz.cx > lpSize->cx)
1346 lpSize->cx = sz.cx;
1347 if (sz.cy > lpSize->cy)
1348 lpSize->cy = sz.cy;
1349 }
1350 }
1351
1352 SelectObject (hdc, hOldFont);
1353 ReleaseDC (infoPtr->hwndSelf, hdc);
1354
1355 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1356 }
1357
1358 /***********************************************************************
1359 * TOOLBAR_WrapToolbar
1360 *
1361 * This function walks through the buttons and separators in the
1362 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1363 * wrapping should occur based on the width of the toolbar window.
1364 * It does *not* calculate button placement itself. That task
1365 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1366 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1367 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1368 *
1369 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1370 * vertical toolbar lists.
1371 */
1372
1373 static void
1374 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1375 {
1376 TBUTTON_INFO *btnPtr;
1377 INT x, cx, i, j, width;
1378 BOOL bButtonWrap;
1379
1380 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1381 /* no layout is necessary. Applications may use this style */
1382 /* to perform their own layout on the toolbar. */
1383 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1384 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1385
1386 btnPtr = infoPtr->buttons;
1387 x = infoPtr->nIndent;
1388 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1389
1390 bButtonWrap = FALSE;
1391
1392 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1393 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1394 infoPtr->nIndent);
1395
1396 for (i = 0; i < infoPtr->nNumButtons; i++ )
1397 {
1398 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1399
1400 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1401 continue;
1402
1403 if (btnPtr[i].cx > 0)
1404 cx = btnPtr[i].cx;
1405 /* horizontal separators are treated as buttons for width */
1406 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1407 !(infoPtr->dwStyle & CCS_VERT))
1408 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1409 else
1410 cx = infoPtr->nButtonWidth;
1411
1412 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1413 cx += DDARROW_WIDTH;
1414
1415 /* Two or more adjacent separators form a separator group. */
1416 /* The first separator in a group should be wrapped to the */
1417 /* next row if the previous wrapping is on a button. */
1418 if( bButtonWrap &&
1419 (btnPtr[i].fsStyle & BTNS_SEP) &&
1420 (i + 1 < infoPtr->nNumButtons ) &&
1421 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1422 {
1423 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1424 btnPtr[i].fsState |= TBSTATE_WRAP;
1425 x = infoPtr->nIndent;
1426 i++;
1427 bButtonWrap = FALSE;
1428 continue;
1429 }
1430
1431 /* The layout makes sure the bitmap is visible, but not the button. */
1432 /* Test added to also wrap after a button that starts a row but */
1433 /* is bigger than the area. - GA 8/01 */
1434 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1435 ((x == infoPtr->nIndent) && (cx > width)))
1436 {
1437 BOOL bFound = FALSE;
1438
1439 /* If the current button is a separator and not hidden, */
1440 /* go to the next until it reaches a non separator. */
1441 /* Wrap the last separator if it is before a button. */
1442 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1443 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1444 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1445 i < infoPtr->nNumButtons )
1446 {
1447 i++;
1448 bFound = TRUE;
1449 }
1450
1451 if( bFound && i < infoPtr->nNumButtons )
1452 {
1453 i--;
1454 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1455 i, btnPtr[i].fsStyle, x, cx);
1456 btnPtr[i].fsState |= TBSTATE_WRAP;
1457 x = infoPtr->nIndent;
1458 bButtonWrap = FALSE;
1459 continue;
1460 }
1461 else if ( i >= infoPtr->nNumButtons)
1462 break;
1463
1464 /* If the current button is not a separator, find the last */
1465 /* separator and wrap it. */
1466 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1467 {
1468 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1469 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1470 {
1471 bFound = TRUE;
1472 i = j;
1473 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1474 i, btnPtr[i].fsStyle, x, cx);
1475 x = infoPtr->nIndent;
1476 btnPtr[j].fsState |= TBSTATE_WRAP;
1477 bButtonWrap = FALSE;
1478 break;
1479 }
1480 }
1481
1482 /* If no separator available for wrapping, wrap one of */
1483 /* non-hidden previous button. */
1484 if (!bFound)
1485 {
1486 for ( j = i - 1;
1487 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1488 {
1489 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1490 continue;
1491
1492 bFound = TRUE;
1493 i = j;
1494 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1495 i, btnPtr[i].fsStyle, x, cx);
1496 x = infoPtr->nIndent;
1497 btnPtr[j].fsState |= TBSTATE_WRAP;
1498 bButtonWrap = TRUE;
1499 break;
1500 }
1501 }
1502
1503 /* If all above failed, wrap the current button. */
1504 if (!bFound)
1505 {
1506 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1507 i, btnPtr[i].fsStyle, x, cx);
1508 btnPtr[i].fsState |= TBSTATE_WRAP;
1509 x = infoPtr->nIndent;
1510 if (btnPtr[i].fsStyle & BTNS_SEP )
1511 bButtonWrap = FALSE;
1512 else
1513 bButtonWrap = TRUE;
1514 }
1515 }
1516 else {
1517 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1518 i, btnPtr[i].fsStyle, x, cx);
1519 x += cx;
1520 }
1521 }
1522 }
1523
1524
1525 /***********************************************************************
1526 * TOOLBAR_MeasureButton
1527 *
1528 * Calculates the width and height required for a button. Used in
1529 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1530 * the width of buttons that are autosized.
1531 *
1532 * Note that it would have been rather elegant to use one piece of code for
1533 * both the laying out of the toolbar and for controlling where button parts
1534 * are drawn, but the native control has inconsistencies between the two that
1535 * prevent this from being effectively. These inconsistencies can be seen as
1536 * artefacts where parts of the button appear outside of the bounding button
1537 * rectangle.
1538 *
1539 * There are several cases for the calculation of the button dimensions and
1540 * button part positioning:
1541 *
1542 * List
1543 * ====
1544 *
1545 * With Bitmap:
1546 *
1547 * +--------------------------------------------------------+ ^
1548 * | ^ ^ | |
1549 * | | pad.cy / 2 | centered | |
1550 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1551 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1552 * | |<------------>| | | | |
1553 * | +--------------+ +------------+ | |
1554 * |<-------------------------------------->| | |
1555 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1556 * | szText.cx | |
1557 * +--------------------------------------------------------+ -
1558 * <-------------------------------------------------------->
1559 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1560 *
1561 * Without Bitmap (I_IMAGENONE):
1562 *
1563 * +-----------------------------------+ ^
1564 * | ^ | |
1565 * | | centered | | LISTPAD_CY +
1566 * | +------------+ | | szText.cy
1567 * | | Text | | |
1568 * | | | | |
1569 * | +------------+ | |
1570 * |<----------------->| | |
1571 * | cxedge |<-----------> | |
1572 * | szText.cx | |
1573 * +-----------------------------------+ -
1574 * <----------------------------------->
1575 * szText.cx + pad.cx
1576 *
1577 * Without text:
1578 *
1579 * +--------------------------------------+ ^
1580 * | ^ | |
1581 * | | padding.cy/2 | | DEFPAD_CY +
1582 * | +------------+ | | nBitmapHeight
1583 * | | Bitmap | | |
1584 * | | | | |
1585 * | +------------+ | |
1586 * |<------------------->| | |
1587 * | cxedge + iListGap/2 |<-----------> | |
1588 * | nBitmapWidth | |
1589 * +--------------------------------------+ -
1590 * <-------------------------------------->
1591 * 2*cxedge + nBitmapWidth + iListGap
1592 *
1593 * Non-List
1594 * ========
1595 *
1596 * With bitmap:
1597 *
1598 * +-----------------------------------+ ^
1599 * | ^ | |
1600 * | | pad.cy / 2 | | nBitmapHeight +
1601 * | - | | szText.cy +
1602 * | +------------+ | | DEFPAD_CY + 1
1603 * | centered | Bitmap | | |
1604 * |<----------------->| | | |
1605 * | +------------+ | |
1606 * | ^ | |
1607 * | 1 | | |
1608 * | - | |
1609 * | centered +---------------+ | |
1610 * |<--------------->| Text | | |
1611 * | +---------------+ | |
1612 * +-----------------------------------+ -
1613 * <----------------------------------->
1614 * pad.cx + max(nBitmapWidth, szText.cx)
1615 *
1616 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1617 *
1618 * +---------------------------------------+ ^
1619 * | ^ | |
1620 * | | 2 + pad.cy / 2 | |
1621 * | - | | szText.cy +
1622 * | centered +-----------------+ | | pad.cy + 2
1623 * |<--------------->| Text | | |
1624 * | +-----------------+ | |
1625 * | | |
1626 * +---------------------------------------+ -
1627 * <--------------------------------------->
1628 * 2*cxedge + pad.cx + szText.cx
1629 *
1630 * Without text:
1631 * As for with bitmaps, but with szText.cx zero.
1632 */
1633 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1634 BOOL bHasBitmap, BOOL bValidImageList)
1635 {
1636 SIZE sizeButton;
1637 if (infoPtr->dwStyle & TBSTYLE_LIST)
1638 {
1639 /* set button height from bitmap / text height... */
1640 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1641 sizeString.cy);
1642
1643 /* ... add on the necessary padding */
1644 if (bValidImageList)
1645 {
1646 #ifdef __REACTOS__
1647 sizeButton.cy += infoPtr->szPadding.cy;
1648 if (!bHasBitmap)
1649 #else
1650 if (bHasBitmap)
1651 sizeButton.cy += DEFPAD_CY;
1652 else
1653 #endif
1654 sizeButton.cy += LISTPAD_CY;
1655 }
1656 else
1657 sizeButton.cy += infoPtr->szPadding.cy;
1658
1659 /* calculate button width */
1660 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1661 infoPtr->nBitmapWidth + infoPtr->iListGap;
1662 if (sizeString.cx > 0)
1663 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1664
1665 }
1666 else
1667 {
1668 if (bHasBitmap)
1669 {
1670 #ifdef __REACTOS__
1671 sizeButton.cy = infoPtr->nBitmapHeight + infoPtr->szPadding.cy;
1672 #else
1673 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1674 #endif
1675 if (sizeString.cy > 0)
1676 sizeButton.cy += 1 + sizeString.cy;
1677 sizeButton.cx = infoPtr->szPadding.cx +
1678 max(sizeString.cx, infoPtr->nBitmapWidth);
1679 }
1680 else
1681 {
1682 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1683 NONLIST_NOTEXT_OFFSET;
1684 sizeButton.cx = infoPtr->szPadding.cx +
1685 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1686 }
1687 }
1688 return sizeButton;
1689 }
1690
1691
1692 /***********************************************************************
1693 * TOOLBAR_CalcToolbar
1694 *
1695 * This function calculates button and separator placement. It first
1696 * calculates the button sizes, gets the toolbar window width and then
1697 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1698 * on. It assigns a new location to each item and sends this location to
1699 * the tooltip window if appropriate. Finally, it updates the rcBound
1700 * rect and calculates the new required toolbar window height.
1701 */
1702 static void
1703 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1704 {
1705 SIZE sizeString, sizeButton;
1706 BOOL validImageList = FALSE;
1707
1708 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1709
1710 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1711
1712 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1713 validImageList = TRUE;
1714 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1715 infoPtr->nButtonWidth = sizeButton.cx;
1716 infoPtr->nButtonHeight = sizeButton.cy;
1717 infoPtr->iTopMargin = default_top_margin(infoPtr);
1718
1719 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1720 infoPtr->nButtonWidth = infoPtr->cxMin;
1721 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1722 infoPtr->nButtonWidth = infoPtr->cxMax;
1723
1724 TOOLBAR_LayoutToolbar(infoPtr);
1725 }
1726
1727 static void
1728 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1729 {
1730 TBUTTON_INFO *btnPtr;
1731 SIZE sizeButton;
1732 INT i, nRows, nSepRows;
1733 INT x, y, cx, cy;
1734 BOOL bWrap;
1735 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1736
1737 TOOLBAR_WrapToolbar(infoPtr);
1738
1739 x = infoPtr->nIndent;
1740 y = infoPtr->iTopMargin;
1741 cx = infoPtr->nButtonWidth;
1742 cy = infoPtr->nButtonHeight;
1743
1744 nRows = nSepRows = 0;
1745
1746 infoPtr->rcBound.top = y;
1747 infoPtr->rcBound.left = x;
1748 infoPtr->rcBound.bottom = y + cy;
1749 infoPtr->rcBound.right = x;
1750
1751 btnPtr = infoPtr->buttons;
1752
1753 TRACE("cy=%d\n", cy);
1754
1755 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1756 {
1757 bWrap = FALSE;
1758 if (btnPtr->fsState & TBSTATE_HIDDEN)
1759 {
1760 SetRectEmpty (&btnPtr->rect);
1761 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1762 continue;
1763 }
1764
1765 cy = infoPtr->nButtonHeight;
1766
1767 if (btnPtr->fsStyle & BTNS_SEP) {
1768 if (infoPtr->dwStyle & CCS_VERT) {
1769 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1770 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1771 }
1772 else
1773 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1774 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1775 }
1776 else
1777 {
1778 if (btnPtr->cx)
1779 cx = btnPtr->cx;
1780 #ifdef __REACTOS__
1781 /* Revert Wine Commit 5b7b911 as it breaks Explorer Toolbar Buttons
1782 FIXME: Revisit this when the bug is fixed. CORE-9970 */
1783 else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1784 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1785 #else
1786 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1787 #endif
1788 {
1789 SIZE sz;
1790 HDC hdc;
1791 HFONT hOldFont;
1792
1793 hdc = GetDC (infoPtr->hwndSelf);
1794 hOldFont = SelectObject (hdc, infoPtr->hFont);
1795
1796 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1797
1798 SelectObject (hdc, hOldFont);
1799 ReleaseDC (infoPtr->hwndSelf, hdc);
1800
1801 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1802 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1803 validImageList);
1804 cx = sizeButton.cx;
1805 }
1806 else
1807 cx = infoPtr->nButtonWidth;
1808
1809 /* if size has been set manually then don't add on extra space
1810 * for the drop down arrow */
1811 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1812 cx += DDARROW_WIDTH;
1813 }
1814 if (btnPtr->fsState & TBSTATE_WRAP)
1815 bWrap = TRUE;
1816
1817 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1818
1819 if (infoPtr->rcBound.left > x)
1820 infoPtr->rcBound.left = x;
1821 if (infoPtr->rcBound.right < x + cx)
1822 infoPtr->rcBound.right = x + cx;
1823 if (infoPtr->rcBound.bottom < y + cy)
1824 infoPtr->rcBound.bottom = y + cy;
1825
1826 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1827
1828 /* btnPtr->nRow is zero based. The space between the rows is */
1829 /* also considered as a row. */
1830 btnPtr->nRow = nRows + nSepRows;
1831
1832 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1833 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1834 x, y, x+cx, y+cy);
1835
1836 if( bWrap )
1837 {
1838 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1839 y += cy;
1840 else
1841 {
1842 if ( !(infoPtr->dwStyle & CCS_VERT))
1843 y += cy + ( (btnPtr->cx > 0 ) ?
1844 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1845 else
1846 y += cy;
1847
1848 /* nSepRows is used to calculate the extra height following */
1849 /* the last row. */
1850 nSepRows++;
1851 }
1852 x = infoPtr->nIndent;
1853
1854 /* Increment row number unless this is the last button */
1855 /* and it has Wrap set. */
1856 if (i != infoPtr->nNumButtons-1)
1857 nRows++;
1858 }
1859 else
1860 x += cx;
1861 }
1862
1863 /* infoPtr->nRows is the number of rows on the toolbar */
1864 infoPtr->nRows = nRows + nSepRows + 1;
1865
1866 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1867 }
1868
1869
1870 static INT
1871 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1872 {
1873 TBUTTON_INFO *btnPtr;
1874 INT i;
1875
1876 if (button)
1877 *button = FALSE;
1878
1879 btnPtr = infoPtr->buttons;
1880 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1881 if (btnPtr->fsState & TBSTATE_HIDDEN)
1882 continue;
1883
1884 if (btnPtr->fsStyle & BTNS_SEP) {
1885 if (PtInRect (&btnPtr->rect, *lpPt)) {
1886 TRACE(" ON SEPARATOR %d!\n", i);
1887 return -i;
1888 }
1889 }
1890 else {
1891 if (PtInRect (&btnPtr->rect, *lpPt)) {
1892 TRACE(" ON BUTTON %d!\n", i);
1893 if (button)
1894 *button = TRUE;
1895 return i;
1896 }
1897 }
1898 }
1899
1900 TRACE(" NOWHERE!\n");
1901 return TOOLBAR_NOWHERE;
1902 }
1903
1904
1905 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1906 static BOOL
1907 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1908 {
1909 INT nOldButtons, nNewButtons, iButton;
1910 BOOL fHasString = FALSE;
1911
1912 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1913 iIndex = infoPtr->nNumButtons;
1914
1915 nOldButtons = infoPtr->nNumButtons;
1916 nNewButtons = nOldButtons + nAddButtons;
1917
1918 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1919 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1920 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1921 infoPtr->nNumButtons += nAddButtons;
1922
1923 /* insert new buttons data */
1924 for (iButton = 0; iButton < nAddButtons; iButton++) {
1925 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1926 INT_PTR str;
1927
1928 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1929
1930 ZeroMemory(btnPtr, sizeof(*btnPtr));
1931
1932 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1933 btnPtr->idCommand = lpTbb[iButton].idCommand;
1934 btnPtr->fsState = lpTbb[iButton].fsState;
1935 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1936 btnPtr->dwData = lpTbb[iButton].dwData;
1937
1938 if (btnPtr->fsStyle & BTNS_SEP)
1939 str = -1;
1940 else
1941 str = lpTbb[iButton].iString;
1942 set_string_index( btnPtr, str, fUnicode );
1943 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1944
1945 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1946 }
1947
1948 if (infoPtr->nNumStrings > 0 || fHasString)
1949 TOOLBAR_CalcToolbar(infoPtr);
1950 else
1951 TOOLBAR_LayoutToolbar(infoPtr);
1952 TOOLBAR_AutoSize(infoPtr);
1953
1954 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1955 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1956 return TRUE;
1957 }
1958
1959
1960 static INT
1961 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1962 {
1963 TBUTTON_INFO *btnPtr;
1964 INT i;
1965
1966 if (CommandIsIndex) {
1967 TRACE("command is really index command=%d\n", idCommand);
1968 if (idCommand >= infoPtr->nNumButtons) return -1;
1969 return idCommand;
1970 }
1971 btnPtr = infoPtr->buttons;
1972 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1973 if (btnPtr->idCommand == idCommand) {
1974 TRACE("command=%d index=%d\n", idCommand, i);
1975 return i;
1976 }
1977 }
1978 TRACE("no index found for command=%d\n", idCommand);
1979 return -1;
1980 }
1981
1982
1983 static INT
1984 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
1985 {
1986 TBUTTON_INFO *btnPtr;
1987 INT nRunIndex;
1988
1989 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
1990 return -1;
1991
1992 /* check index button */
1993 btnPtr = &infoPtr->buttons[nIndex];
1994 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
1995 if (btnPtr->fsState & TBSTATE_CHECKED)
1996 return nIndex;
1997 }
1998
1999 /* check previous buttons */
2000 nRunIndex = nIndex - 1;
2001 while (nRunIndex >= 0) {
2002 btnPtr = &infoPtr->buttons[nRunIndex];
2003 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
2004 if (btnPtr->fsState & TBSTATE_CHECKED)
2005 return nRunIndex;
2006 }
2007 else
2008 break;
2009 nRunIndex--;
2010 }
2011
2012 /* check next buttons */
2013 nRunIndex = nIndex + 1;
2014 while (nRunIndex < infoPtr->nNumButtons) {
2015 btnPtr = &infoPtr->buttons[nRunIndex];
2016 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
2017 if (btnPtr->fsState & TBSTATE_CHECKED)
2018 return nRunIndex;
2019 }
2020 else
2021 break;
2022 nRunIndex++;
2023 }
2024
2025 return -1;
2026 }
2027
2028
2029 static VOID
2030 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
2031 WPARAM wParam, LPARAM lParam)
2032 {
2033 MSG msg;
2034
2035 msg.hwnd = hwndMsg;
2036 msg.message = uMsg;
2037 msg.wParam = wParam;
2038 msg.lParam = lParam;
2039 msg.time = GetMessageTime ();
2040 msg.pt.x = (short)LOWORD(GetMessagePos ());
2041 msg.pt.y = (short)HIWORD(GetMessagePos ());
2042
2043 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
2044 }
2045
2046 #ifdef __REACTOS__
2047 static LRESULT
2048 TOOLBAR_ThemeChanged(HWND hwnd)
2049 {
2050 HTHEME theme = GetWindowTheme(hwnd);
2051 CloseThemeData(theme);
2052 OpenThemeData(hwnd, themeClass);
2053 return 0;
2054 }
2055 #endif
2056
2057 static void
2058 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2059 {
2060 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
2061 TTTOOLINFOW ti;
2062
2063 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
2064 ti.cbSize = sizeof (TTTOOLINFOW);
2065 ti.hwnd = infoPtr->hwndSelf;
2066 ti.uId = button->idCommand;
2067 ti.hinst = 0;
2068 ti.lpszText = LPSTR_TEXTCALLBACKW;
2069 /* ti.lParam = random value from the stack? */
2070
2071 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
2072 0, (LPARAM)&ti);
2073 }
2074 }
2075
2076 static void
2077 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2078 {
2079 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
2080 TTTOOLINFOW ti;
2081
2082 ZeroMemory(&ti, sizeof(ti));
2083 ti.cbSize = sizeof(ti);
2084 ti.hwnd = infoPtr->hwndSelf;
2085 ti.uId = button->idCommand;
2086
2087 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2088 }
2089 }
2090
2091 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2092 {
2093 /* Set the toolTip only for non-hidden, non-separator button */
2094 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2095 {
2096 TTTOOLINFOW ti;
2097
2098 ZeroMemory(&ti, sizeof(ti));
2099 ti.cbSize = sizeof(ti);
2100 ti.hwnd = infoPtr->hwndSelf;
2101 ti.uId = button->idCommand;
2102 ti.rect = button->rect;
2103 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2104 }
2105 }
2106
2107 /* Creates the tooltip control */
2108 static void
2109 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2110 {
2111 int i;
2112 NMTOOLTIPSCREATED nmttc;
2113
2114 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2115 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2116 infoPtr->hwndSelf, 0, 0, 0);
2117
2118 if (!infoPtr->hwndToolTip)
2119 return;
2120
2121 /* Send NM_TOOLTIPSCREATED notification */
2122 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2123 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2124
2125 for (i = 0; i < infoPtr->nNumButtons; i++)
2126 {
2127 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2128 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2129 }
2130 }
2131
2132 /* keeps available button list box sorted by button id */
2133 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2134 {
2135 int i;
2136 int count;
2137 PCUSTOMBUTTON btnInfo;
2138 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2139
2140 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2141
2142 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2143
2144 /* position 0 is always separator */
2145 for (i = 1; i < count; i++)
2146 {
2147 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2148 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2149 {
2150 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2151 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2152 return;
2153 }
2154 }
2155 /* id higher than all others add to end */
2156 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2157 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2158 }
2159
2160 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2161 {
2162 NMTOOLBARW nmtb;
2163
2164 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2165
2166 if (nIndexFrom == nIndexTo)
2167 return;
2168
2169 /* MSDN states that iItem is the index of the button, rather than the
2170 * command ID as used by every other NMTOOLBAR notification */
2171 nmtb.iItem = nIndexFrom;
2172 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2173 {
2174 PCUSTOMBUTTON btnInfo;
2175 NMHDR hdr;
2176 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2177 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2178
2179 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2180
2181 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2182 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2183 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2184 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2185
2186 if (nIndexTo <= 0)
2187 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2188 else
2189 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2190
2191 /* last item is always separator, so -2 instead of -1 */
2192 if (nIndexTo >= (count - 2))
2193 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2194 else
2195 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2196
2197 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2198 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2199
2200 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2201 }
2202 }
2203
2204 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2205 {
2206 NMTOOLBARW nmtb;
2207
2208 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2209
2210 /* MSDN states that iItem is the index of the button, rather than the
2211 * command ID as used by every other NMTOOLBAR notification */
2212 nmtb.iItem = nIndexAvail;
2213 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2214 {
2215 PCUSTOMBUTTON btnInfo;
2216 NMHDR hdr;
2217 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2218 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2219 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2220
2221 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2222
2223 if (nIndexAvail != 0) /* index == 0 indicates separator */
2224 {
2225 /* remove from 'available buttons' list */
2226 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2227 if (nIndexAvail == count-1)
2228 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2229 else
2230 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2231 }
2232 else
2233 {
2234 PCUSTOMBUTTON btnNew;
2235
2236 /* duplicate 'separator' button */
2237 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2238 *btnNew = *btnInfo;
2239 btnInfo = btnNew;
2240 }
2241
2242 /* insert into 'toolbar button' list */
2243 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2244 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2245
2246 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2247
2248 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2249 }
2250 }
2251
2252 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2253 {
2254 PCUSTOMBUTTON btnInfo;
2255 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2256
2257 TRACE("Remove: index %d\n", index);
2258
2259 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2260
2261 /* send TBN_QUERYDELETE notification */
2262 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2263 {
2264 NMHDR hdr;
2265
2266 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2267 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2268
2269 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2270
2271 /* insert into 'available button' list */
2272 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2273 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2274 else
2275 Free(btnInfo);
2276
2277 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2278 }
2279 }
2280
2281 /* drag list notification function for toolbar buttons list box */
2282 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2283 const DRAGLISTINFO *pDLI)
2284 {
2285 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2286 switch (pDLI->uNotification)
2287 {
2288 case DL_BEGINDRAG:
2289 {
2290 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2291 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2292 /* no dragging for last item (separator) */
2293 if (nCurrentItem >= (nCount - 1)) return FALSE;
2294 return TRUE;
2295 }
2296 case DL_DRAGGING:
2297 {
2298 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2299 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2300 /* no dragging past last item (separator) */
2301 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2302 {
2303 DrawInsert(hwnd, hwndList, nCurrentItem);
2304 /* FIXME: native uses "move button" cursor */
2305 return DL_COPYCURSOR;
2306 }
2307
2308 /* not over toolbar buttons list */
2309 if (nCurrentItem < 0)
2310 {
2311 POINT ptWindow = pDLI->ptCursor;
2312 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2313 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2314 /* over available buttons list? */
2315 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2316 /* FIXME: native uses "move button" cursor */
2317 return DL_COPYCURSOR;
2318 }
2319 /* clear drag arrow */
2320 DrawInsert(hwnd, hwndList, -1);
2321 return DL_STOPCURSOR;
2322 }
2323 case DL_DROPPED:
2324 {
2325 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2326 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2327 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2328 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2329 {
2330 /* clear drag arrow */
2331 DrawInsert(hwnd, hwndList, -1);
2332 /* move item */
2333 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2334 }
2335 /* not over toolbar buttons list */
2336 if (nIndexTo < 0)
2337 {
2338 POINT ptWindow = pDLI->ptCursor;
2339 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2340 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2341 /* over available buttons list? */
2342 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2343 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2344 }
2345 break;
2346 }
2347 case DL_CANCELDRAG:
2348 /* Clear drag arrow */
2349 DrawInsert(hwnd, hwndList, -1);
2350 break;
2351 }
2352
2353 return 0;
2354 }
2355
2356 /* drag list notification function for available buttons list box */
2357 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2358 const DRAGLISTINFO *pDLI)
2359 {
2360 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2361 switch (pDLI->uNotification)
2362 {
2363 case DL_BEGINDRAG:
2364 return TRUE;
2365 case DL_DRAGGING:
2366 {
2367 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2368 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2369 /* no dragging past last item (separator) */
2370 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2371 {
2372 DrawInsert(hwnd, hwndList, nCurrentItem);
2373 /* FIXME: native uses "move button" cursor */
2374 return DL_COPYCURSOR;
2375 }
2376
2377 /* not over toolbar buttons list */
2378 if (nCurrentItem < 0)
2379 {
2380 POINT ptWindow = pDLI->ptCursor;
2381 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2382 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2383 /* over available buttons list? */
2384 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2385 /* FIXME: native uses "move button" cursor */
2386 return DL_COPYCURSOR;
2387 }
2388 /* clear drag arrow */
2389 DrawInsert(hwnd, hwndList, -1);
2390 return DL_STOPCURSOR;
2391 }
2392 case DL_DROPPED:
2393 {
2394 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2395 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2396 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2397 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2398 {
2399 /* clear drag arrow */
2400 DrawInsert(hwnd, hwndList, -1);
2401 /* add item */
2402 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2403 }
2404 }
2405 case DL_CANCELDRAG:
2406 /* Clear drag arrow */
2407 DrawInsert(hwnd, hwndList, -1);
2408 break;
2409 }
2410 return 0;
2411 }
2412
2413 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2414
2415 /***********************************************************************
2416 * TOOLBAR_CustomizeDialogProc
2417 * This function implements the toolbar customization dialog.
2418 */
2419 static INT_PTR CALLBACK
2420 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2421 {
2422 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2423 PCUSTOMBUTTON btnInfo;
2424 NMTOOLBARA nmtb;
2425 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2426
2427 switch (uMsg)
2428 {
2429 case WM_INITDIALOG:
2430 custInfo = (PCUSTDLG_INFO)lParam;
2431 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2432
2433 if (custInfo)
2434 {
2435 WCHAR Buffer[256];
2436 int i = 0;
2437 int index;
2438 NMTBINITCUSTOMIZE nmtbic;
2439
2440 infoPtr = custInfo->tbInfo;
2441
2442 /* send TBN_QUERYINSERT notification */
2443 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2444
2445 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2446 return FALSE;
2447
2448 nmtbic.hwndDialog = hwnd;
2449 /* Send TBN_INITCUSTOMIZE notification */
2450 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2451 TBNRF_HIDEHELP)
2452 {
2453 TRACE("TBNRF_HIDEHELP requested\n");
2454 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2455 }
2456
2457 /* add items to 'toolbar buttons' list and check if removable */
2458 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2459 {
2460 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2461 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2462 btnInfo->btn.fsStyle = BTNS_SEP;
2463 btnInfo->bVirtual = FALSE;
2464 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2465
2466 /* send TBN_QUERYDELETE notification */
2467 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2468
2469 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2470 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2471 }
2472
2473 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2474
2475 /* insert separator button into 'available buttons' list */
2476 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2477 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2478 btnInfo->btn.fsStyle = BTNS_SEP;
2479 btnInfo->bVirtual = FALSE;
2480 btnInfo->bRemovable = TRUE;
2481 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2482 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2483 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2484
2485 /* insert all buttons into dsa */
2486 for (i = 0;; i++)
2487 {
2488 /* send TBN_GETBUTTONINFO notification */
2489 NMTOOLBARW nmtb;
2490 nmtb.iItem = i;
2491 nmtb.pszText = Buffer;
2492 nmtb.cchText = 256;
2493
2494 /* Clear previous button's text */
2495 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2496
2497 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2498 break;
2499
2500 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2501 nmtb.tbButton.fsStyle, i,
2502 nmtb.tbButton.idCommand,
2503 nmtb.tbButton.iString,
2504 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2505 : "");
2506
2507 /* insert button into the appropriate list */
2508 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2509 if (index == -1)
2510 {
2511 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2512 btnInfo->bVirtual = FALSE;
2513 btnInfo->bRemovable = TRUE;
2514 }
2515 else
2516 {
2517 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2518 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2519 }
2520
2521 btnInfo->btn = nmtb.tbButton;
2522 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2523 {
2524 if (lstrlenW(nmtb.pszText))
2525 lstrcpyW(btnInfo->text, nmtb.pszText);
2526 else if (nmtb.tbButton.iString >= 0 &&
2527 nmtb.tbButton.iString < infoPtr->nNumStrings)
2528 {
2529 lstrcpyW(btnInfo->text,
2530 infoPtr->strings[nmtb.tbButton.iString]);
2531 }
2532 }
2533
2534 if (index == -1)
2535 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2536 }
2537
2538 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2539
2540 /* select first item in the 'available' list */
2541 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2542
2543 /* append 'virtual' separator button to the 'toolbar buttons' list */
2544 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2545 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2546 btnInfo->btn.fsStyle = BTNS_SEP;
2547 btnInfo->bVirtual = TRUE;
2548 btnInfo->bRemovable = FALSE;
2549 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2550 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2551 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2552
2553 /* select last item in the 'toolbar' list */
2554 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2555 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2556
2557 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2558 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2559
2560 /* set focus and disable buttons */
2561 PostMessageW (hwnd, WM_USER, 0, 0);
2562 }
2563 return TRUE;
2564
2565 case WM_USER:
2566 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2567 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2568 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2569 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2570 return TRUE;
2571
2572 case WM_CLOSE:
2573 EndDialog(hwnd, FALSE);
2574 return TRUE;
2575
2576 case WM_COMMAND:
2577 switch (LOWORD(wParam))
2578 {
2579 case IDC_TOOLBARBTN_LBOX:
2580 if (HIWORD(wParam) == LBN_SELCHANGE)
2581 {
2582 PCUSTOMBUTTON btnInfo;
2583 NMTOOLBARA nmtb;
2584 int count;
2585 int index;
2586
2587 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2588 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2589
2590 /* send TBN_QUERYINSERT notification */
2591 nmtb.iItem = index;
2592 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2593
2594 /* get list box item */
2595 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2596
2597 if (index == (count - 1))
2598 {
2599 /* last item (virtual separator) */
2600 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2601 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2602 }
2603 else if (index == (count - 2))
2604 {
2605 /* second last item (last non-virtual item) */
2606 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2607 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2608 }
2609 else if (index == 0)
2610 {
2611 /* first item */
2612 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2613 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2614 }
2615 else
2616 {
2617 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2618 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2619 }
2620
2621 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2622 }
2623 break;
2624
2625 case IDC_MOVEUP_BTN:
2626 {
2627 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2628 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2629 }
2630 break;
2631
2632 case IDC_MOVEDN_BTN: /* move down */
2633 {
2634 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2635 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2636 }
2637 break;
2638
2639 case IDC_REMOVE_BTN: /* remove button */
2640 {
2641 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2642
2643 if (LB_ERR == index)
2644 break;
2645
2646 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2647 }
2648 break;
2649 case IDC_HELP_BTN:
2650 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2651 break;
2652 case IDC_RESET_BTN:
2653 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2654 break;
2655
2656 case IDOK: /* Add button */
2657 {
2658 int index;
2659 int indexto;
2660
2661 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2662 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2663
2664 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2665 }
2666 break;
2667
2668 case IDCANCEL:
2669 EndDialog(hwnd, FALSE);
2670 break;
2671 }
2672 return TRUE;
2673
2674 case WM_DESTROY:
2675 {
2676 int count;
2677 int i;
2678
2679 /* delete items from 'toolbar buttons' listbox*/
2680 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2681 for (i = 0; i < count; i++)
2682 {
2683 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2684 Free(btnInfo);
2685 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2686 }
2687 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2688
2689
2690 /* delete items from 'available buttons' listbox*/
2691 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2692 for (i = 0; i < count; i++)
2693 {
2694 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2695 Free(btnInfo);
2696 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2697 }
2698 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2699 }
2700 return TRUE;
2701
2702 case WM_DRAWITEM:
2703 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2704 {
2705 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2706 RECT rcButton;
2707 RECT rcText;
2708 HPEN hPen, hOldPen;
2709 HBRUSH hOldBrush;
2710 COLORREF oldText = 0;
2711 COLORREF oldBk = 0;
2712
2713 /* get item data */
2714 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2715 if (btnInfo == NULL)
2716 {
2717 FIXME("btnInfo invalid!\n");
2718 return TRUE;
2719 }
2720
2721 /* set colors and select objects */
2722 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2723 if (btnInfo->bVirtual)
2724 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2725 else
2726 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2727 hPen = CreatePen( PS_SOLID, 1,
2728 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2729 hOldPen = SelectObject (lpdis->hDC, hPen );
2730 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2731
2732 /* fill background rectangle */
2733 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2734 lpdis->rcItem.right, lpdis->rcItem.bottom);
2735
2736 /* calculate button and text rectangles */
2737 rcButton = lpdis->rcItem;
2738 InflateRect (&rcButton, -1, -1);
2739 rcText = rcButton;
2740 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2741 rcText.left = rcButton.right + 2;
2742
2743 /* draw focus rectangle */
2744 if (lpdis->itemState & ODS_FOCUS)
2745 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2746
2747 /* draw button */
2748 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2749 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2750
2751 /* draw image and text */
2752 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2753 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2754 btnInfo->btn.iBitmap));
2755 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2756 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2757 }
2758 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2759 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2760
2761 /* delete objects and reset colors */
2762 SelectObject (lpdis->hDC, hOldBrush);
2763 SelectObject (lpdis->hDC, hOldPen);
2764 SetBkColor (lpdis->hDC, oldBk);
2765 SetTextColor (lpdis->hDC, oldText);
2766 DeleteObject( hPen );
2767 return TRUE;
2768 }
2769 return FALSE;
2770
2771 case WM_MEASUREITEM:
2772 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2773 {
2774 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2775
2776 lpmis->itemHeight = 15 + 8; /* default height */
2777
2778 return TRUE;
2779 }
2780 return FALSE;
2781
2782 default:
2783 if (uDragListMessage && (uMsg == uDragListMessage))
2784 {
2785 if (wParam == IDC_TOOLBARBTN_LBOX)
2786 {
2787 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2788 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2789 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2790 return TRUE;
2791 }
2792 else if (wParam == IDC_AVAILBTN_LBOX)
2793 {
2794 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2795 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2796 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2797 return TRUE;
2798 }
2799 }
2800 return FALSE;
2801 }
2802 }
2803
2804 static BOOL
2805 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2806 {
2807 HBITMAP hbmLoad;
2808 INT nCountBefore = ImageList_GetImageCount(himlDef);
2809 INT nCountAfter;
2810 INT cxIcon, cyIcon;
2811 INT nAdded;
2812 INT nIndex;
2813
2814 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2815 /* Add bitmaps to the default image list */
2816 if (bitmap->hInst == NULL) /* a handle was passed */
2817 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2818 else if (bitmap->hInst == COMCTL32_hModule)
2819 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2820 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2821 else
2822 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2823
2824 /* enlarge the bitmap if needed */
2825 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2826 if (bitmap->hInst != COMCTL32_hModule)
2827 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2828
2829 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2830 DeleteObject(hbmLoad);
2831 if (nIndex == -1)
2832 return FALSE;
2833
2834 nCountAfter = ImageList_GetImageCount(himlDef);
2835 nAdded = nCountAfter - nCountBefore;
2836 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2837 {
2838 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2839 } else if (nAdded > (INT)bitmap->nButtons) {
2840 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2841 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2842 }
2843
2844 infoPtr->nNumBitmaps += nAdded;
2845 return TRUE;
2846 }
2847
2848 static void
2849 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2850 {
2851 HIMAGELIST himlDef;
2852 HIMAGELIST himlNew;
2853 INT cx, cy;
2854 INT i;
2855
2856 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2857 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2858 return;
2859 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2860 return;
2861 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2862 return;
2863
2864 TRACE("Update icon size: %dx%d -> %dx%d\n",
2865 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2866
2867 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2868 ILC_COLOR32|ILC_MASK, 8, 2);
2869 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2870 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2871 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2872 infoPtr->himlInt = himlNew;
2873
2874 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2875 ImageList_Destroy(himlDef);
2876 }
2877
2878 /***********************************************************************
2879 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2880 *
2881 */
2882 static LRESULT
2883 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2884 {
2885 TBITMAP_INFO info;
2886 INT iSumButtons, i;
2887 HIMAGELIST himlDef;
2888
2889 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2890 if (!lpAddBmp)
2891 return -1;
2892
2893 if (lpAddBmp->hInst == HINST_COMMCTRL)
2894 {
2895 info.hInst = COMCTL32_hModule;
2896 switch (lpAddBmp->nID)
2897 {
2898 case IDB_STD_SMALL_COLOR:
2899 info.nButtons = 15;
2900 info.nID = IDB_STD_SMALL;
2901 break;
2902 case IDB_STD_LARGE_COLOR:
2903 info.nButtons = 15;
2904 info.nID = IDB_STD_LARGE;
2905 break;
2906 case IDB_VIEW_SMALL_COLOR:
2907 info.nButtons = 12;
2908 info.nID = IDB_VIEW_SMALL;
2909 break;
2910 case IDB_VIEW_LARGE_COLOR:
2911 info.nButtons = 12;
2912 info.nID = IDB_VIEW_LARGE;
2913 break;
2914 case IDB_HIST_SMALL_COLOR:
2915 info.nButtons = 5;
2916 info.nID = IDB_HIST_SMALL;
2917 break;
2918 case IDB_HIST_LARGE_COLOR:
2919 info.nButtons = 5;
2920 info.nID = IDB_HIST_LARGE;
2921 break;
2922 default:
2923 return -1;
2924 }
2925
2926 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2927
2928 /* Windows resize all the buttons to the size of a newly added standard image */
2929 if (lpAddBmp->nID & 1)
2930 {
2931 /* large icons: 24x24. Will make the button 31x30 */
2932 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2933 }
2934 else
2935 {
2936 /* small icons: 16x16. Will make the buttons 23x22 */
2937 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2938 }
2939
2940 TOOLBAR_CalcToolbar (infoPtr);
2941 }
2942 else
2943 {
2944 info.nButtons = count;
2945 info.hInst = lpAddBmp->hInst;
2946 info.nID = lpAddBmp->nID;
2947 TRACE("adding %d bitmaps!\n", info.nButtons);
2948 }
2949
2950 /* check if the bitmap is already loaded and compute iSumButtons */
2951 iSumButtons = 0;
2952 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2953 {
2954 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2955 infoPtr->bitmaps[i].nID == info.nID)
2956 return iSumButtons;
2957 iSumButtons += infoPtr->bitmaps[i].nButtons;
2958 }
2959
2960 if (!infoPtr->cimlDef) {
2961 /* create new default image list */
2962 TRACE ("creating default image list!\n");
2963
2964 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2965 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2966 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2967 infoPtr->himlInt = himlDef;
2968 }
2969 else {
2970 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2971 }
2972
2973 if (!himlDef) {
2974 WARN("No default image list available\n");
2975 return -1;
2976 }
2977
2978 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2979 return -1;
2980
2981 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2982 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
2983 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
2984 infoPtr->nNumBitmapInfos++;
2985 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
2986
2987 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2988 return iSumButtons;
2989 }
2990
2991
2992 static LRESULT
2993 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
2994 {
2995 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
2996
2997 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
2998 }
2999
3000
3001 static LRESULT
3002 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3003 {
3004 #define MAX_RESOURCE_STRING_LENGTH 512
3005 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3006 INT nIndex = infoPtr->nNumStrings;
3007
3008 TRACE("%p, %lx\n", hInstance, lParam);
3009
3010 if (IS_INTRESOURCE(lParam)) {
3011 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
3012 WCHAR delimiter;
3013 WCHAR *next_delim;
3014 HRSRC hrsrc;
3015 WCHAR *p;
3016 INT len;
3017
3018 TRACE("adding string from resource\n");
3019
3020 if (!hInstance) return -1;
3021
3022 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
3023 (LPWSTR)RT_STRING );
3024 if (!hrsrc)
3025 {
3026 TRACE("string not found in resources\n");
3027 return -1;
3028 }
3029
3030 len = LoadStringW (hInstance, (UINT)lParam,
3031 szString, MAX_RESOURCE_STRING_LENGTH);
3032
3033 TRACE("len=%d %s\n", len, debugstr_w(szString));
3034 if (len == 0 || len == 1)
3035 return nIndex;
3036
3037 TRACE("delimiter: 0x%x\n", *szString);
3038 delimiter = *szString;
3039 p = szString + 1;
3040
3041 while ((next_delim = strchrW(p, delimiter)) != NULL) {
3042 *next_deli