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