[COMCTL32] -Implement taking into account the contents margins from the theme when...
[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
1023 TRACE("iBitmap=%d, start=(%d,%d) w=%d, h=%d\n",
1024 btnPtr->iBitmap, rcBitmap.left, rcBitmap.top,
1025 infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
1026 TRACE("Text=%s\n", debugstr_w(lpText));
1027 TRACE("iListGap=%d, padding = { %d, %d }\n", infoPtr->iListGap, infoPtr->szPadding.cx, infoPtr->szPadding.cy);
1028
1029 /* calculate text position */
1030 if (lpText)
1031 {
1032 InflateRect(&rcText, -GetSystemMetrics(SM_CXEDGE), 0);
1033 if (dwStyle & TBSTYLE_LIST)
1034 {
1035 rcText.left += infoPtr->nBitmapWidth + infoPtr->iListGap + 2;
1036 }
1037 else
1038 {
1039 if (ImageList_GetImageCount(GETDEFIMAGELIST(infoPtr, 0)) > 0)
1040 rcText.top += infoPtr->szPadding.cy/2 + infoPtr->nBitmapHeight + 1;
1041 else
1042 rcText.top += infoPtr->szPadding.cy/2 + 2;
1043 }
1044 }
1045
1046 /* Initialize fields in all cases, because we use these later
1047 * NOTE: applications can and do alter these to customize their
1048 * toolbars */
1049 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1050 tbcd.clrText = comctl32_color.clrBtnText;
1051 tbcd.clrTextHighlight = comctl32_color.clrHighlightText;
1052 tbcd.clrBtnFace = comctl32_color.clrBtnFace;
1053 tbcd.clrBtnHighlight = comctl32_color.clrBtnHighlight;
1054 tbcd.clrMark = comctl32_color.clrHighlight;
1055 tbcd.clrHighlightHotTrack = 0;
1056 tbcd.nStringBkMode = TRANSPARENT;
1057 tbcd.nHLStringBkMode = OPAQUE;
1058 tbcd.rcText.left = 0;
1059 tbcd.rcText.top = 0;
1060 tbcd.rcText.right = rcText.right - rc.left;
1061 tbcd.rcText.bottom = rcText.bottom - rc.top;
1062 tbcd.nmcd.uItemState = TOOLBAR_TranslateState(btnPtr);
1063 tbcd.nmcd.hdc = hdc;
1064 tbcd.nmcd.rc = btnPtr->rect;
1065 tbcd.hbrMonoDither = COMCTL32_hPattern55AABrush;
1066
1067 /* FIXME: what are these used for? */
1068 tbcd.hbrLines = 0;
1069 tbcd.hpenLines = 0;
1070
1071 /* Issue Item Prepaint notify */
1072 dwItemCustDraw = 0;
1073 dwItemCDFlag = 0;
1074 if (dwBaseCustDraw & CDRF_NOTIFYITEMDRAW)
1075 {
1076 tbcd.nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
1077 tbcd.nmcd.dwItemSpec = btnPtr->idCommand;
1078 tbcd.nmcd.lItemlParam = btnPtr->dwData;
1079 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1080 /* reset these fields so the user can't alter the behaviour like native */
1081 tbcd.nmcd.hdc = hdc;
1082 tbcd.nmcd.rc = btnPtr->rect;
1083
1084 dwItemCustDraw = ntfret & 0xffff;
1085 dwItemCDFlag = ntfret & 0xffff0000;
1086 if (dwItemCustDraw & CDRF_SKIPDEFAULT)
1087 return;
1088 /* save the only part of the rect that the user can change */
1089 rcText.right = tbcd.rcText.right + rc.left;
1090 rcText.bottom = tbcd.rcText.bottom + rc.top;
1091 }
1092
1093 if (!(dwItemCDFlag & TBCDRF_NOOFFSET) &&
1094 (btnPtr->fsState & (TBSTATE_PRESSED | TBSTATE_CHECKED)))
1095 OffsetRect(&rcText, 1, 1);
1096
1097 if (!(tbcd.nmcd.uItemState & CDIS_HOT) &&
1098 ((tbcd.nmcd.uItemState & CDIS_CHECKED) || (tbcd.nmcd.uItemState & CDIS_INDETERMINATE)))
1099 TOOLBAR_DrawPattern (&rc, &tbcd);
1100
1101 if (((infoPtr->dwStyle & TBSTYLE_FLAT) || GetWindowTheme (infoPtr->hwndSelf))
1102 && (tbcd.nmcd.uItemState & CDIS_HOT))
1103 {
1104 if ( dwItemCDFlag & TBCDRF_HILITEHOTTRACK )
1105 {
1106 COLORREF oldclr;
1107
1108 oldclr = SetBkColor(hdc, tbcd.clrHighlightHotTrack);
1109 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, 0);
1110 if (hasDropDownArrow)
1111 ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, &rcArrow, NULL, 0, 0);
1112 SetBkColor(hdc, oldclr);
1113 }
1114 }
1115
1116 #ifdef __REACTOS__
1117 if (theme && !(dwItemCDFlag & TBCDRF_NOBACKGROUND))
1118 #else
1119 if (theme)
1120 #endif
1121 {
1122 int partId = drawSepDropDownArrow ? TP_SPLITBUTTON : TP_BUTTON;
1123 int stateId = TS_NORMAL;
1124
1125 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1126 stateId = TS_DISABLED;
1127 else if (tbcd.nmcd.uItemState & CDIS_SELECTED)
1128 stateId = TS_PRESSED;
1129 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1130 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1131 else if ((tbcd.nmcd.uItemState & CDIS_HOT)
1132 || (drawSepDropDownArrow && btnPtr->bDropDownPressed))
1133 stateId = TS_HOT;
1134
1135 DrawThemeBackground (theme, hdc, partId, stateId, &rc, NULL);
1136 }
1137
1138 #ifdef __REACTOS__
1139 if (!theme)
1140 #else
1141 else
1142 #endif
1143 TOOLBAR_DrawFrame(infoPtr, &tbcd, &rc, dwItemCDFlag);
1144
1145 if (drawSepDropDownArrow)
1146 {
1147 if (theme)
1148 {
1149 int stateId = TS_NORMAL;
1150
1151 if (tbcd.nmcd.uItemState & CDIS_DISABLED)
1152 stateId = TS_DISABLED;
1153 else if (btnPtr->bDropDownPressed || (tbcd.nmcd.uItemState & CDIS_SELECTED))
1154 stateId = TS_PRESSED;
1155 else if (tbcd.nmcd.uItemState & CDIS_CHECKED)
1156 stateId = (tbcd.nmcd.uItemState & CDIS_HOT) ? TS_HOTCHECKED : TS_HOT;
1157 else if (tbcd.nmcd.uItemState & CDIS_HOT)
1158 stateId = TS_HOT;
1159
1160 DrawThemeBackground (theme, hdc, TP_DROPDOWNBUTTON, stateId, &rcArrow, NULL);
1161 DrawThemeBackground (theme, hdc, TP_SPLITBUTTONDROPDOWN, stateId, &rcArrow, NULL);
1162 }
1163 else
1164 TOOLBAR_DrawSepDDArrow(infoPtr, &tbcd, &rcArrow, btnPtr->bDropDownPressed, dwItemCDFlag);
1165 }
1166
1167 oldBkMode = SetBkMode (hdc, tbcd.nStringBkMode);
1168 if (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) || (btnPtr->fsStyle & BTNS_SHOWTEXT))
1169 TOOLBAR_DrawString (infoPtr, &rcText, lpText, &tbcd, dwItemCDFlag);
1170 SetBkMode (hdc, oldBkMode);
1171
1172 TOOLBAR_DrawImage(infoPtr, btnPtr, rcBitmap.left, rcBitmap.top, &tbcd, dwItemCDFlag);
1173
1174 if (hasDropDownArrow && !drawSepDropDownArrow)
1175 {
1176 if (tbcd.nmcd.uItemState & (CDIS_DISABLED | CDIS_INDETERMINATE))
1177 {
1178 TOOLBAR_DrawArrow(hdc, rcArrow.left+1, rcArrow.top+1 + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnHighlight);
1179 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clr3dShadow);
1180 }
1181 else if (tbcd.nmcd.uItemState & (CDIS_SELECTED | CDIS_CHECKED))
1182 {
1183 offset = (dwItemCDFlag & TBCDRF_NOOFFSET) ? 0 : 1;
1184 TOOLBAR_DrawArrow(hdc, rcArrow.left + offset, rcArrow.top + offset + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1185 }
1186 else
1187 TOOLBAR_DrawArrow(hdc, rcArrow.left, rcArrow.top + (rcArrow.bottom - rcArrow.top - ARROW_HEIGHT) / 2, comctl32_color.clrBtnText);
1188 }
1189
1190 if (dwItemCustDraw & CDRF_NOTIFYPOSTPAINT)
1191 {
1192 tbcd.nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
1193 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1194 }
1195
1196 }
1197
1198
1199 static void
1200 TOOLBAR_Refresh (TOOLBAR_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1201 {
1202 TBUTTON_INFO *btnPtr;
1203 INT i;
1204 RECT rcTemp, rcClient;
1205 NMTBCUSTOMDRAW tbcd;
1206 DWORD ntfret;
1207 DWORD dwBaseCustDraw;
1208
1209 /* the app has told us not to redraw the toolbar */
1210 if (!infoPtr->bDoRedraw)
1211 return;
1212
1213 /* if imagelist belongs to the app, it can be changed
1214 by the app after setting it */
1215 if (GETDEFIMAGELIST(infoPtr, 0) != infoPtr->himlInt)
1216 {
1217 infoPtr->nNumBitmaps = 0;
1218 for (i = 0; i < infoPtr->cimlDef; i++)
1219 infoPtr->nNumBitmaps += ImageList_GetImageCount(infoPtr->himlDef[i]->himl);
1220 }
1221
1222 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1223
1224 /* change the imagelist icon size if we manage the list and it is necessary */
1225 TOOLBAR_CheckImageListIconSize(infoPtr);
1226
1227 /* Send initial notify */
1228 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1229 tbcd.nmcd.dwDrawStage = CDDS_PREPAINT;
1230 tbcd.nmcd.hdc = hdc;
1231 tbcd.nmcd.rc = ps->rcPaint;
1232 ntfret = TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1233 dwBaseCustDraw = ntfret & 0xffff;
1234
1235 GetClientRect(infoPtr->hwndSelf, &rcClient);
1236
1237 /* redraw necessary buttons */
1238 btnPtr = infoPtr->buttons;
1239 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++)
1240 {
1241 BOOL bDraw;
1242 if (!RectVisible(hdc, &btnPtr->rect))
1243 continue;
1244 if (infoPtr->dwExStyle & TBSTYLE_EX_HIDECLIPPEDBUTTONS)
1245 {
1246 IntersectRect(&rcTemp, &rcClient, &btnPtr->rect);
1247 bDraw = EqualRect(&rcTemp, &btnPtr->rect);
1248 }
1249 else
1250 bDraw = TRUE;
1251 bDraw &= IntersectRect(&rcTemp, &(ps->rcPaint), &(btnPtr->rect));
1252 bDraw = (btnPtr->fsState & TBSTATE_HIDDEN) ? FALSE : bDraw;
1253 if (bDraw)
1254 TOOLBAR_DrawButton(infoPtr, btnPtr, hdc, dwBaseCustDraw);
1255 }
1256
1257 /* draw insert mark if required */
1258 if (infoPtr->tbim.iButton != -1)
1259 {
1260 RECT rcButton = infoPtr->buttons[infoPtr->tbim.iButton].rect;
1261 RECT rcInsertMark;
1262 rcInsertMark.top = rcButton.top;
1263 rcInsertMark.bottom = rcButton.bottom;
1264 if (infoPtr->tbim.dwFlags & TBIMHT_AFTER)
1265 rcInsertMark.left = rcInsertMark.right = rcButton.right;
1266 else
1267 rcInsertMark.left = rcInsertMark.right = rcButton.left - INSERTMARK_WIDTH;
1268 COMCTL32_DrawInsertMark(hdc, &rcInsertMark, infoPtr->clrInsertMark, FALSE);
1269 }
1270
1271 if (dwBaseCustDraw & CDRF_NOTIFYPOSTPAINT)
1272 {
1273 ZeroMemory (&tbcd, sizeof(NMTBCUSTOMDRAW));
1274 tbcd.nmcd.dwDrawStage = CDDS_POSTPAINT;
1275 tbcd.nmcd.hdc = hdc;
1276 tbcd.nmcd.rc = ps->rcPaint;
1277 TOOLBAR_SendNotify(&tbcd.nmcd.hdr, infoPtr, NM_CUSTOMDRAW);
1278 }
1279 }
1280
1281 /***********************************************************************
1282 * TOOLBAR_MeasureString
1283 *
1284 * This function gets the width and height of a string in pixels. This
1285 * is done first by using GetTextExtentPoint to get the basic width
1286 * and height. The DrawText is called with DT_CALCRECT to get the exact
1287 * width. The reason is because the text may have more than one "&" (or
1288 * prefix characters as M$ likes to call them). The prefix character
1289 * indicates where the underline goes, except for the string "&&" which
1290 * is reduced to a single "&". GetTextExtentPoint does not process these
1291 * only DrawText does. Note that the BTNS_NOPREFIX is handled here.
1292 */
1293 static void
1294 TOOLBAR_MeasureString(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *btnPtr,
1295 HDC hdc, LPSIZE lpSize)
1296 {
1297 RECT myrect;
1298
1299 lpSize->cx = 0;
1300 lpSize->cy = 0;
1301
1302 if (infoPtr->nMaxTextRows > 0 &&
1303 !(btnPtr->fsState & TBSTATE_HIDDEN) &&
1304 (!(infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1305 (btnPtr->fsStyle & BTNS_SHOWTEXT)) )
1306 {
1307 LPWSTR lpText = TOOLBAR_GetText(infoPtr, btnPtr);
1308
1309 if(lpText != NULL) {
1310 /* first get size of all the text */
1311 GetTextExtentPoint32W (hdc, lpText, strlenW (lpText), lpSize);
1312
1313 /* feed above size into the rectangle for DrawText */
1314 SetRect(&myrect, 0, 0, lpSize->cx, lpSize->cy);
1315
1316 /* Use DrawText to get true size as drawn (less pesky "&") */
1317 DrawTextW (hdc, lpText, -1, &myrect, DT_VCENTER | DT_SINGLELINE |
1318 DT_CALCRECT | ((btnPtr->fsStyle & BTNS_NOPREFIX) ?
1319 DT_NOPREFIX : 0));
1320
1321 /* feed back to caller */
1322 lpSize->cx = myrect.right;
1323 lpSize->cy = myrect.bottom;
1324 }
1325 }
1326
1327 TRACE("string size %d x %d!\n", lpSize->cx, lpSize->cy);
1328 }
1329
1330 /***********************************************************************
1331 * TOOLBAR_CalcStrings
1332 *
1333 * This function walks through each string and measures it and returns
1334 * the largest height and width to caller.
1335 */
1336 static void
1337 TOOLBAR_CalcStrings (const TOOLBAR_INFO *infoPtr, LPSIZE lpSize)
1338 {
1339 TBUTTON_INFO *btnPtr;
1340 INT i;
1341 SIZE sz;
1342 HDC hdc;
1343 HFONT hOldFont;
1344
1345 lpSize->cx = 0;
1346 lpSize->cy = 0;
1347
1348 if (infoPtr->nMaxTextRows == 0)
1349 return;
1350
1351 hdc = GetDC (infoPtr->hwndSelf);
1352 hOldFont = SelectObject (hdc, infoPtr->hFont);
1353
1354 if (infoPtr->nNumButtons == 0 && infoPtr->nNumStrings > 0)
1355 {
1356 TEXTMETRICW tm;
1357
1358 GetTextMetricsW(hdc, &tm);
1359 lpSize->cy = tm.tmHeight;
1360 }
1361
1362 btnPtr = infoPtr->buttons;
1363 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1364 if(TOOLBAR_GetText(infoPtr, btnPtr))
1365 {
1366 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1367 if (sz.cx > lpSize->cx)
1368 lpSize->cx = sz.cx;
1369 if (sz.cy > lpSize->cy)
1370 lpSize->cy = sz.cy;
1371 }
1372 }
1373
1374 SelectObject (hdc, hOldFont);
1375 ReleaseDC (infoPtr->hwndSelf, hdc);
1376
1377 TRACE("max string size %d x %d!\n", lpSize->cx, lpSize->cy);
1378 }
1379
1380 /***********************************************************************
1381 * TOOLBAR_WrapToolbar
1382 *
1383 * This function walks through the buttons and separators in the
1384 * toolbar, and sets the TBSTATE_WRAP flag only on those items where
1385 * wrapping should occur based on the width of the toolbar window.
1386 * It does *not* calculate button placement itself. That task
1387 * takes place in TOOLBAR_CalcToolbar. If the program wants to manage
1388 * the toolbar wrapping on its own, it can use the TBSTYLE_WRAPABLE
1389 * flag, and set the TBSTATE_WRAP flags manually on the appropriate items.
1390 *
1391 * Note: TBSTYLE_WRAPABLE or TBSTYLE_EX_VERTICAL can be used also to allow
1392 * vertical toolbar lists.
1393 */
1394
1395 static void
1396 TOOLBAR_WrapToolbar(TOOLBAR_INFO *infoPtr)
1397 {
1398 TBUTTON_INFO *btnPtr;
1399 INT x, cx, i, j, width;
1400 BOOL bButtonWrap;
1401
1402 /* When the toolbar window style is not TBSTYLE_WRAPABLE, */
1403 /* no layout is necessary. Applications may use this style */
1404 /* to perform their own layout on the toolbar. */
1405 if( !(infoPtr->dwStyle & TBSTYLE_WRAPABLE) &&
1406 !(infoPtr->dwExStyle & TBSTYLE_EX_VERTICAL) ) return;
1407
1408 btnPtr = infoPtr->buttons;
1409 x = infoPtr->nIndent;
1410 width = infoPtr->client_rect.right - infoPtr->client_rect.left;
1411
1412 bButtonWrap = FALSE;
1413
1414 TRACE("start ButtonWidth=%d, BitmapWidth=%d, width=%d, nIndent=%d\n",
1415 infoPtr->nButtonWidth, infoPtr->nBitmapWidth, width,
1416 infoPtr->nIndent);
1417
1418 for (i = 0; i < infoPtr->nNumButtons; i++ )
1419 {
1420 btnPtr[i].fsState &= ~TBSTATE_WRAP;
1421
1422 if (btnPtr[i].fsState & TBSTATE_HIDDEN)
1423 continue;
1424
1425 if (btnPtr[i].cx > 0)
1426 cx = btnPtr[i].cx;
1427 /* horizontal separators are treated as buttons for width */
1428 else if ((btnPtr[i].fsStyle & BTNS_SEP) &&
1429 !(infoPtr->dwStyle & CCS_VERT))
1430 cx = (btnPtr[i].iBitmap > 0) ? btnPtr[i].iBitmap : SEPARATOR_WIDTH;
1431 else
1432 cx = infoPtr->nButtonWidth;
1433
1434 if (!btnPtr[i].cx && button_has_ddarrow( infoPtr, btnPtr + i ))
1435 cx += DDARROW_WIDTH;
1436
1437 /* Two or more adjacent separators form a separator group. */
1438 /* The first separator in a group should be wrapped to the */
1439 /* next row if the previous wrapping is on a button. */
1440 if( bButtonWrap &&
1441 (btnPtr[i].fsStyle & BTNS_SEP) &&
1442 (i + 1 < infoPtr->nNumButtons ) &&
1443 (btnPtr[i + 1].fsStyle & BTNS_SEP) )
1444 {
1445 TRACE("wrap point 1 btn %d style %02x\n", i, btnPtr[i].fsStyle);
1446 btnPtr[i].fsState |= TBSTATE_WRAP;
1447 x = infoPtr->nIndent;
1448 i++;
1449 bButtonWrap = FALSE;
1450 continue;
1451 }
1452
1453 /* The layout makes sure the bitmap is visible, but not the button. */
1454 /* Test added to also wrap after a button that starts a row but */
1455 /* is bigger than the area. - GA 8/01 */
1456 if ((x + cx - (infoPtr->nButtonWidth - infoPtr->nBitmapWidth) / 2 > width) ||
1457 ((x == infoPtr->nIndent) && (cx > width)))
1458 {
1459 BOOL bFound = FALSE;
1460
1461 /* If the current button is a separator and not hidden, */
1462 /* go to the next until it reaches a non separator. */
1463 /* Wrap the last separator if it is before a button. */
1464 while( ( ((btnPtr[i].fsStyle & BTNS_SEP) &&
1465 !(btnPtr[i].fsStyle & BTNS_DROPDOWN)) ||
1466 (btnPtr[i].fsState & TBSTATE_HIDDEN) ) &&
1467 i < infoPtr->nNumButtons )
1468 {
1469 i++;
1470 bFound = TRUE;
1471 }
1472
1473 if( bFound && i < infoPtr->nNumButtons )
1474 {
1475 i--;
1476 TRACE("wrap point 2 btn %d style %02x, x=%d, cx=%d\n",
1477 i, btnPtr[i].fsStyle, x, cx);
1478 btnPtr[i].fsState |= TBSTATE_WRAP;
1479 x = infoPtr->nIndent;
1480 bButtonWrap = FALSE;
1481 continue;
1482 }
1483 else if ( i >= infoPtr->nNumButtons)
1484 break;
1485
1486 /* If the current button is not a separator, find the last */
1487 /* separator and wrap it. */
1488 for ( j = i - 1; j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1489 {
1490 if ((btnPtr[j].fsStyle & BTNS_SEP) &&
1491 !(btnPtr[j].fsState & TBSTATE_HIDDEN))
1492 {
1493 bFound = TRUE;
1494 i = j;
1495 TRACE("wrap point 3 btn %d style %02x, x=%d, cx=%d\n",
1496 i, btnPtr[i].fsStyle, x, cx);
1497 x = infoPtr->nIndent;
1498 btnPtr[j].fsState |= TBSTATE_WRAP;
1499 bButtonWrap = FALSE;
1500 break;
1501 }
1502 }
1503
1504 /* If no separator available for wrapping, wrap one of */
1505 /* non-hidden previous button. */
1506 if (!bFound)
1507 {
1508 for ( j = i - 1;
1509 j >= 0 && !(btnPtr[j].fsState & TBSTATE_WRAP); j--)
1510 {
1511 if (btnPtr[j].fsState & TBSTATE_HIDDEN)
1512 continue;
1513
1514 bFound = TRUE;
1515 i = j;
1516 TRACE("wrap point 4 btn %d style %02x, x=%d, cx=%d\n",
1517 i, btnPtr[i].fsStyle, x, cx);
1518 x = infoPtr->nIndent;
1519 btnPtr[j].fsState |= TBSTATE_WRAP;
1520 bButtonWrap = TRUE;
1521 break;
1522 }
1523 }
1524
1525 /* If all above failed, wrap the current button. */
1526 if (!bFound)
1527 {
1528 TRACE("wrap point 5 btn %d style %02x, x=%d, cx=%d\n",
1529 i, btnPtr[i].fsStyle, x, cx);
1530 btnPtr[i].fsState |= TBSTATE_WRAP;
1531 x = infoPtr->nIndent;
1532 if (btnPtr[i].fsStyle & BTNS_SEP )
1533 bButtonWrap = FALSE;
1534 else
1535 bButtonWrap = TRUE;
1536 }
1537 }
1538 else {
1539 TRACE("wrap point 6 btn %d style %02x, x=%d, cx=%d\n",
1540 i, btnPtr[i].fsStyle, x, cx);
1541 x += cx;
1542 }
1543 }
1544 }
1545
1546
1547 /***********************************************************************
1548 * TOOLBAR_MeasureButton
1549 *
1550 * Calculates the width and height required for a button. Used in
1551 * TOOLBAR_CalcToolbar to set the all-button width and height and also for
1552 * the width of buttons that are autosized.
1553 *
1554 * Note that it would have been rather elegant to use one piece of code for
1555 * both the laying out of the toolbar and for controlling where button parts
1556 * are drawn, but the native control has inconsistencies between the two that
1557 * prevent this from being effectively. These inconsistencies can be seen as
1558 * artefacts where parts of the button appear outside of the bounding button
1559 * rectangle.
1560 *
1561 * There are several cases for the calculation of the button dimensions and
1562 * button part positioning:
1563 *
1564 * List
1565 * ====
1566 *
1567 * With Bitmap:
1568 *
1569 * +--------------------------------------------------------+ ^
1570 * | ^ ^ | |
1571 * | | pad.cy / 2 | centered | |
1572 * | pad.cx/2 + cxedge +--------------+ +------------+ | | DEFPAD_CY +
1573 * |<----------------->| nBitmapWidth | | Text | | | max(nBitmapHeight, szText.cy)
1574 * | |<------------>| | | | |
1575 * | +--------------+ +------------+ | |
1576 * |<-------------------------------------->| | |
1577 * | cxedge + iListGap + nBitmapWidth + 2 |<-----------> | |
1578 * | szText.cx | |
1579 * +--------------------------------------------------------+ -
1580 * <-------------------------------------------------------->
1581 * 2*cxedge + nBitmapWidth + iListGap + szText.cx + pad.cx
1582 *
1583 * Without Bitmap (I_IMAGENONE):
1584 *
1585 * +-----------------------------------+ ^
1586 * | ^ | |
1587 * | | centered | | LISTPAD_CY +
1588 * | +------------+ | | szText.cy
1589 * | | Text | | |
1590 * | | | | |
1591 * | +------------+ | |
1592 * |<----------------->| | |
1593 * | cxedge |<-----------> | |
1594 * | szText.cx | |
1595 * +-----------------------------------+ -
1596 * <----------------------------------->
1597 * szText.cx + pad.cx
1598 *
1599 * Without text:
1600 *
1601 * +--------------------------------------+ ^
1602 * | ^ | |
1603 * | | padding.cy/2 | | DEFPAD_CY +
1604 * | +------------+ | | nBitmapHeight
1605 * | | Bitmap | | |
1606 * | | | | |
1607 * | +------------+ | |
1608 * |<------------------->| | |
1609 * | cxedge + iListGap/2 |<-----------> | |
1610 * | nBitmapWidth | |
1611 * +--------------------------------------+ -
1612 * <-------------------------------------->
1613 * 2*cxedge + nBitmapWidth + iListGap
1614 *
1615 * Non-List
1616 * ========
1617 *
1618 * With bitmap:
1619 *
1620 * +-----------------------------------+ ^
1621 * | ^ | |
1622 * | | pad.cy / 2 | | nBitmapHeight +
1623 * | - | | szText.cy +
1624 * | +------------+ | | DEFPAD_CY + 1
1625 * | centered | Bitmap | | |
1626 * |<----------------->| | | |
1627 * | +------------+ | |
1628 * | ^ | |
1629 * | 1 | | |
1630 * | - | |
1631 * | centered +---------------+ | |
1632 * |<--------------->| Text | | |
1633 * | +---------------+ | |
1634 * +-----------------------------------+ -
1635 * <----------------------------------->
1636 * pad.cx + max(nBitmapWidth, szText.cx)
1637 *
1638 * Without bitmaps (NULL imagelist or ImageList_GetImageCount() = 0):
1639 *
1640 * +---------------------------------------+ ^
1641 * | ^ | |
1642 * | | 2 + pad.cy / 2 | |
1643 * | - | | szText.cy +
1644 * | centered +-----------------+ | | pad.cy + 2
1645 * |<--------------->| Text | | |
1646 * | +-----------------+ | |
1647 * | | |
1648 * +---------------------------------------+ -
1649 * <--------------------------------------->
1650 * 2*cxedge + pad.cx + szText.cx
1651 *
1652 * Without text:
1653 * As for with bitmaps, but with szText.cx zero.
1654 */
1655 static inline SIZE TOOLBAR_MeasureButton(const TOOLBAR_INFO *infoPtr, SIZE sizeString,
1656 BOOL bHasBitmap, BOOL bValidImageList)
1657 {
1658 SIZE sizeButton;
1659 if (infoPtr->dwStyle & TBSTYLE_LIST)
1660 {
1661 /* set button height from bitmap / text height... */
1662 sizeButton.cy = max((bHasBitmap ? infoPtr->nBitmapHeight : 0),
1663 sizeString.cy);
1664
1665 /* ... add on the necessary padding */
1666 if (bValidImageList)
1667 {
1668 #ifdef __REACTOS__
1669 sizeButton.cy += infoPtr->szPadding.cy;
1670 if (!bHasBitmap)
1671 #else
1672 if (bHasBitmap)
1673 sizeButton.cy += DEFPAD_CY;
1674 else
1675 #endif
1676 sizeButton.cy += LISTPAD_CY;
1677 }
1678 else
1679 sizeButton.cy += infoPtr->szPadding.cy;
1680
1681 /* calculate button width */
1682 sizeButton.cx = 2*GetSystemMetrics(SM_CXEDGE) +
1683 infoPtr->nBitmapWidth + infoPtr->iListGap;
1684 if (sizeString.cx > 0)
1685 sizeButton.cx += sizeString.cx + infoPtr->szPadding.cx;
1686
1687 }
1688 else
1689 {
1690 if (bHasBitmap)
1691 {
1692 #ifdef __REACTOS__
1693 sizeButton.cy = infoPtr->nBitmapHeight + infoPtr->szPadding.cy;
1694 #else
1695 sizeButton.cy = infoPtr->nBitmapHeight + DEFPAD_CY;
1696 #endif
1697 if (sizeString.cy > 0)
1698 sizeButton.cy += 1 + sizeString.cy;
1699 sizeButton.cx = infoPtr->szPadding.cx +
1700 max(sizeString.cx, infoPtr->nBitmapWidth);
1701 }
1702 else
1703 {
1704 sizeButton.cy = sizeString.cy + infoPtr->szPadding.cy +
1705 NONLIST_NOTEXT_OFFSET;
1706 sizeButton.cx = infoPtr->szPadding.cx +
1707 max(2*GetSystemMetrics(SM_CXEDGE) + sizeString.cx, infoPtr->nBitmapWidth);
1708 }
1709 }
1710
1711 #ifdef __REACTOS__
1712 sizeButton.cx += infoPtr->themeMargins.cxLeftWidth + infoPtr->themeMargins.cxRightWidth;
1713 sizeButton.cy += infoPtr->themeMargins.cyTopHeight + infoPtr->themeMargins.cyBottomHeight;
1714 #endif
1715
1716 return sizeButton;
1717 }
1718
1719
1720 /***********************************************************************
1721 * TOOLBAR_CalcToolbar
1722 *
1723 * This function calculates button and separator placement. It first
1724 * calculates the button sizes, gets the toolbar window width and then
1725 * calls TOOLBAR_WrapToolbar to determine which buttons we need to wrap
1726 * on. It assigns a new location to each item and sends this location to
1727 * the tooltip window if appropriate. Finally, it updates the rcBound
1728 * rect and calculates the new required toolbar window height.
1729 */
1730 static void
1731 TOOLBAR_CalcToolbar (TOOLBAR_INFO *infoPtr)
1732 {
1733 SIZE sizeString, sizeButton;
1734 BOOL validImageList = FALSE;
1735
1736 TOOLBAR_CalcStrings (infoPtr, &sizeString);
1737
1738 TOOLBAR_DumpToolbar (infoPtr, __LINE__);
1739
1740 if (TOOLBAR_IsValidImageList(infoPtr, 0))
1741 validImageList = TRUE;
1742 sizeButton = TOOLBAR_MeasureButton(infoPtr, sizeString, TRUE, validImageList);
1743 infoPtr->nButtonWidth = sizeButton.cx;
1744 infoPtr->nButtonHeight = sizeButton.cy;
1745 infoPtr->iTopMargin = default_top_margin(infoPtr);
1746
1747 if ( infoPtr->cxMin >= 0 && infoPtr->nButtonWidth < infoPtr->cxMin )
1748 infoPtr->nButtonWidth = infoPtr->cxMin;
1749 if ( infoPtr->cxMax > 0 && infoPtr->nButtonWidth > infoPtr->cxMax )
1750 infoPtr->nButtonWidth = infoPtr->cxMax;
1751
1752 TOOLBAR_LayoutToolbar(infoPtr);
1753 }
1754
1755 static void
1756 TOOLBAR_LayoutToolbar(TOOLBAR_INFO *infoPtr)
1757 {
1758 TBUTTON_INFO *btnPtr;
1759 SIZE sizeButton;
1760 INT i, nRows, nSepRows;
1761 INT x, y, cx, cy;
1762 BOOL bWrap;
1763 BOOL validImageList = TOOLBAR_IsValidImageList(infoPtr, 0);
1764
1765 TOOLBAR_WrapToolbar(infoPtr);
1766
1767 x = infoPtr->nIndent;
1768 y = infoPtr->iTopMargin;
1769 cx = infoPtr->nButtonWidth;
1770 cy = infoPtr->nButtonHeight;
1771
1772 nRows = nSepRows = 0;
1773
1774 infoPtr->rcBound.top = y;
1775 infoPtr->rcBound.left = x;
1776 infoPtr->rcBound.bottom = y + cy;
1777 infoPtr->rcBound.right = x;
1778
1779 btnPtr = infoPtr->buttons;
1780
1781 TRACE("cy=%d\n", cy);
1782
1783 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++ )
1784 {
1785 bWrap = FALSE;
1786 if (btnPtr->fsState & TBSTATE_HIDDEN)
1787 {
1788 SetRectEmpty (&btnPtr->rect);
1789 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1790 continue;
1791 }
1792
1793 cy = infoPtr->nButtonHeight;
1794
1795 if (btnPtr->fsStyle & BTNS_SEP) {
1796 if (infoPtr->dwStyle & CCS_VERT) {
1797 cy = (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1798 cx = (btnPtr->cx > 0) ? btnPtr->cx : infoPtr->nButtonWidth;
1799 }
1800 else
1801 cx = (btnPtr->cx > 0) ? btnPtr->cx :
1802 (btnPtr->iBitmap > 0) ? btnPtr->iBitmap : SEPARATOR_WIDTH;
1803 }
1804 else
1805 {
1806 if (btnPtr->cx)
1807 cx = btnPtr->cx;
1808 #ifdef __REACTOS__
1809 /* Revert Wine Commit 5b7b911 as it breaks Explorer Toolbar Buttons
1810 FIXME: Revisit this when the bug is fixed. CORE-9970 */
1811 else if ((infoPtr->dwExStyle & TBSTYLE_EX_MIXEDBUTTONS) ||
1812 (btnPtr->fsStyle & BTNS_AUTOSIZE))
1813 #else
1814 else if (btnPtr->fsStyle & BTNS_AUTOSIZE)
1815 #endif
1816 {
1817 SIZE sz;
1818 HDC hdc;
1819 HFONT hOldFont;
1820
1821 hdc = GetDC (infoPtr->hwndSelf);
1822 hOldFont = SelectObject (hdc, infoPtr->hFont);
1823
1824 TOOLBAR_MeasureString(infoPtr, btnPtr, hdc, &sz);
1825
1826 SelectObject (hdc, hOldFont);
1827 ReleaseDC (infoPtr->hwndSelf, hdc);
1828
1829 sizeButton = TOOLBAR_MeasureButton(infoPtr, sz,
1830 TOOLBAR_IsValidBitmapIndex(infoPtr, infoPtr->buttons[i].iBitmap),
1831 validImageList);
1832 cx = sizeButton.cx;
1833 }
1834 else
1835 cx = infoPtr->nButtonWidth;
1836
1837 /* if size has been set manually then don't add on extra space
1838 * for the drop down arrow */
1839 if (!btnPtr->cx && button_has_ddarrow( infoPtr, btnPtr ))
1840 cx += DDARROW_WIDTH;
1841 }
1842 if (btnPtr->fsState & TBSTATE_WRAP)
1843 bWrap = TRUE;
1844
1845 SetRect (&btnPtr->rect, x, y, x + cx, y + cy);
1846
1847 if (infoPtr->rcBound.left > x)
1848 infoPtr->rcBound.left = x;
1849 if (infoPtr->rcBound.right < x + cx)
1850 infoPtr->rcBound.right = x + cx;
1851 if (infoPtr->rcBound.bottom < y + cy)
1852 infoPtr->rcBound.bottom = y + cy;
1853
1854 TOOLBAR_TooltipSetRect(infoPtr, btnPtr);
1855
1856 /* btnPtr->nRow is zero based. The space between the rows is */
1857 /* also considered as a row. */
1858 btnPtr->nRow = nRows + nSepRows;
1859
1860 TRACE("button %d style=%x, bWrap=%d, nRows=%d, nSepRows=%d, btnrow=%d, (%d,%d)-(%d,%d)\n",
1861 i, btnPtr->fsStyle, bWrap, nRows, nSepRows, btnPtr->nRow,
1862 x, y, x+cx, y+cy);
1863
1864 if( bWrap )
1865 {
1866 if ( !(btnPtr->fsStyle & BTNS_SEP) )
1867 y += cy;
1868 else
1869 {
1870 if ( !(infoPtr->dwStyle & CCS_VERT))
1871 y += cy + ( (btnPtr->cx > 0 ) ?
1872 btnPtr->cx : SEPARATOR_WIDTH) * 2 /3;
1873 else
1874 y += cy;
1875
1876 /* nSepRows is used to calculate the extra height following */
1877 /* the last row. */
1878 nSepRows++;
1879 }
1880 x = infoPtr->nIndent;
1881
1882 /* Increment row number unless this is the last button */
1883 /* and it has Wrap set. */
1884 if (i != infoPtr->nNumButtons-1)
1885 nRows++;
1886 }
1887 else
1888 x += cx;
1889 }
1890
1891 /* infoPtr->nRows is the number of rows on the toolbar */
1892 infoPtr->nRows = nRows + nSepRows + 1;
1893
1894 TRACE("toolbar button width %d\n", infoPtr->nButtonWidth);
1895 }
1896
1897
1898 static INT
1899 TOOLBAR_InternalHitTest (const TOOLBAR_INFO *infoPtr, const POINT *lpPt, BOOL *button)
1900 {
1901 TBUTTON_INFO *btnPtr;
1902 INT i;
1903
1904 if (button)
1905 *button = FALSE;
1906
1907 btnPtr = infoPtr->buttons;
1908 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
1909 if (btnPtr->fsState & TBSTATE_HIDDEN)
1910 continue;
1911
1912 if (btnPtr->fsStyle & BTNS_SEP) {
1913 if (PtInRect (&btnPtr->rect, *lpPt)) {
1914 TRACE(" ON SEPARATOR %d!\n", i);
1915 return -i;
1916 }
1917 }
1918 else {
1919 if (PtInRect (&btnPtr->rect, *lpPt)) {
1920 TRACE(" ON BUTTON %d!\n", i);
1921 if (button)
1922 *button = TRUE;
1923 return i;
1924 }
1925 }
1926 }
1927
1928 TRACE(" NOWHERE!\n");
1929 return TOOLBAR_NOWHERE;
1930 }
1931
1932
1933 /* worker for TB_ADDBUTTONS and TB_INSERTBUTTON */
1934 static BOOL
1935 TOOLBAR_InternalInsertButtonsT(TOOLBAR_INFO *infoPtr, INT iIndex, UINT nAddButtons, const TBBUTTON *lpTbb, BOOL fUnicode)
1936 {
1937 INT nOldButtons, nNewButtons, iButton;
1938 BOOL fHasString = FALSE;
1939
1940 if (iIndex < 0) /* iIndex can be negative, what means adding at the end */
1941 iIndex = infoPtr->nNumButtons;
1942
1943 nOldButtons = infoPtr->nNumButtons;
1944 nNewButtons = nOldButtons + nAddButtons;
1945
1946 infoPtr->buttons = ReAlloc(infoPtr->buttons, sizeof(TBUTTON_INFO)*nNewButtons);
1947 memmove(&infoPtr->buttons[iIndex + nAddButtons], &infoPtr->buttons[iIndex],
1948 (nOldButtons - iIndex) * sizeof(TBUTTON_INFO));
1949 infoPtr->nNumButtons += nAddButtons;
1950
1951 /* insert new buttons data */
1952 for (iButton = 0; iButton < nAddButtons; iButton++) {
1953 TBUTTON_INFO *btnPtr = &infoPtr->buttons[iIndex + iButton];
1954 INT_PTR str;
1955
1956 TOOLBAR_DumpTBButton(lpTbb + iButton, fUnicode);
1957
1958 ZeroMemory(btnPtr, sizeof(*btnPtr));
1959
1960 btnPtr->iBitmap = lpTbb[iButton].iBitmap;
1961 btnPtr->idCommand = lpTbb[iButton].idCommand;
1962 btnPtr->fsState = lpTbb[iButton].fsState;
1963 btnPtr->fsStyle = lpTbb[iButton].fsStyle;
1964 btnPtr->dwData = lpTbb[iButton].dwData;
1965
1966 if (btnPtr->fsStyle & BTNS_SEP)
1967 str = -1;
1968 else
1969 str = lpTbb[iButton].iString;
1970 set_string_index( btnPtr, str, fUnicode );
1971 fHasString |= TOOLBAR_ButtonHasString( btnPtr );
1972
1973 TOOLBAR_TooltipAddTool(infoPtr, btnPtr);
1974 }
1975
1976 if (infoPtr->nNumStrings > 0 || fHasString)
1977 TOOLBAR_CalcToolbar(infoPtr);
1978 else
1979 TOOLBAR_LayoutToolbar(infoPtr);
1980 TOOLBAR_AutoSize(infoPtr);
1981
1982 TOOLBAR_DumpToolbar(infoPtr, __LINE__);
1983 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1984 return TRUE;
1985 }
1986
1987
1988 static INT
1989 TOOLBAR_GetButtonIndex (const TOOLBAR_INFO *infoPtr, INT idCommand, BOOL CommandIsIndex)
1990 {
1991 TBUTTON_INFO *btnPtr;
1992 INT i;
1993
1994 if (CommandIsIndex) {
1995 TRACE("command is really index command=%d\n", idCommand);
1996 if (idCommand >= infoPtr->nNumButtons) return -1;
1997 return idCommand;
1998 }
1999 btnPtr = infoPtr->buttons;
2000 for (i = 0; i < infoPtr->nNumButtons; i++, btnPtr++) {
2001 if (btnPtr->idCommand == idCommand) {
2002 TRACE("command=%d index=%d\n", idCommand, i);
2003 return i;
2004 }
2005 }
2006 TRACE("no index found for command=%d\n", idCommand);
2007 return -1;
2008 }
2009
2010
2011 static INT
2012 TOOLBAR_GetCheckedGroupButtonIndex (const TOOLBAR_INFO *infoPtr, INT nIndex)
2013 {
2014 TBUTTON_INFO *btnPtr;
2015 INT nRunIndex;
2016
2017 if ((nIndex < 0) || (nIndex > infoPtr->nNumButtons))
2018 return -1;
2019
2020 /* check index button */
2021 btnPtr = &infoPtr->buttons[nIndex];
2022 if ((btnPtr->fsStyle & BTNS_CHECKGROUP) == BTNS_CHECKGROUP) {
2023 if (btnPtr->fsState & TBSTATE_CHECKED)
2024 return nIndex;
2025 }
2026
2027 /* check previous buttons */
2028 nRunIndex = nIndex - 1;
2029 while (nRunIndex >= 0) {
2030 btnPtr = &infoPtr->buttons[nRunIndex];
2031 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
2032 if (btnPtr->fsState & TBSTATE_CHECKED)
2033 return nRunIndex;
2034 }
2035 else
2036 break;
2037 nRunIndex--;
2038 }
2039
2040 /* check next buttons */
2041 nRunIndex = nIndex + 1;
2042 while (nRunIndex < infoPtr->nNumButtons) {
2043 btnPtr = &infoPtr->buttons[nRunIndex];
2044 if ((btnPtr->fsStyle & BTNS_GROUP) == BTNS_GROUP) {
2045 if (btnPtr->fsState & TBSTATE_CHECKED)
2046 return nRunIndex;
2047 }
2048 else
2049 break;
2050 nRunIndex++;
2051 }
2052
2053 return -1;
2054 }
2055
2056
2057 static VOID
2058 TOOLBAR_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
2059 WPARAM wParam, LPARAM lParam)
2060 {
2061 MSG msg;
2062
2063 msg.hwnd = hwndMsg;
2064 msg.message = uMsg;
2065 msg.wParam = wParam;
2066 msg.lParam = lParam;
2067 msg.time = GetMessageTime ();
2068 msg.pt.x = (short)LOWORD(GetMessagePos ());
2069 msg.pt.y = (short)HIWORD(GetMessagePos ());
2070
2071 SendMessageW (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
2072 }
2073
2074
2075 static void
2076 TOOLBAR_TooltipAddTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2077 {
2078 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP)) {
2079 TTTOOLINFOW ti;
2080
2081 ZeroMemory(&ti, sizeof(TTTOOLINFOW));
2082 ti.cbSize = sizeof (TTTOOLINFOW);
2083 ti.hwnd = infoPtr->hwndSelf;
2084 ti.uId = button->idCommand;
2085 ti.hinst = 0;
2086 ti.lpszText = LPSTR_TEXTCALLBACKW;
2087 /* ti.lParam = random value from the stack? */
2088
2089 SendMessageW(infoPtr->hwndToolTip, TTM_ADDTOOLW,
2090 0, (LPARAM)&ti);
2091 }
2092 }
2093
2094 static void
2095 TOOLBAR_TooltipDelTool(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2096 {
2097 if ((infoPtr->hwndToolTip) && !(button->fsStyle & BTNS_SEP)) {
2098 TTTOOLINFOW ti;
2099
2100 ZeroMemory(&ti, sizeof(ti));
2101 ti.cbSize = sizeof(ti);
2102 ti.hwnd = infoPtr->hwndSelf;
2103 ti.uId = button->idCommand;
2104
2105 SendMessageW(infoPtr->hwndToolTip, TTM_DELTOOLW, 0, (LPARAM)&ti);
2106 }
2107 }
2108
2109 static void TOOLBAR_TooltipSetRect(const TOOLBAR_INFO *infoPtr, const TBUTTON_INFO *button)
2110 {
2111 /* Set the toolTip only for non-hidden, non-separator button */
2112 if (infoPtr->hwndToolTip && !(button->fsStyle & BTNS_SEP))
2113 {
2114 TTTOOLINFOW ti;
2115
2116 ZeroMemory(&ti, sizeof(ti));
2117 ti.cbSize = sizeof(ti);
2118 ti.hwnd = infoPtr->hwndSelf;
2119 ti.uId = button->idCommand;
2120 ti.rect = button->rect;
2121 SendMessageW(infoPtr->hwndToolTip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti);
2122 }
2123 }
2124
2125 /* Creates the tooltip control */
2126 static void
2127 TOOLBAR_TooltipCreateControl(TOOLBAR_INFO *infoPtr)
2128 {
2129 int i;
2130 NMTOOLTIPSCREATED nmttc;
2131
2132 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
2133 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2134 infoPtr->hwndSelf, 0, 0, 0);
2135
2136 if (!infoPtr->hwndToolTip)
2137 return;
2138
2139 /* Send NM_TOOLTIPSCREATED notification */
2140 nmttc.hwndToolTips = infoPtr->hwndToolTip;
2141 TOOLBAR_SendNotify(&nmttc.hdr, infoPtr, NM_TOOLTIPSCREATED);
2142
2143 for (i = 0; i < infoPtr->nNumButtons; i++)
2144 {
2145 TOOLBAR_TooltipAddTool(infoPtr, &infoPtr->buttons[i]);
2146 TOOLBAR_TooltipSetRect(infoPtr, &infoPtr->buttons[i]);
2147 }
2148 }
2149
2150 /* keeps available button list box sorted by button id */
2151 static void TOOLBAR_Cust_InsertAvailButton(HWND hwnd, PCUSTOMBUTTON btnInfoNew)
2152 {
2153 int i;
2154 int count;
2155 PCUSTOMBUTTON btnInfo;
2156 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2157
2158 TRACE("button %s, idCommand %d\n", debugstr_w(btnInfoNew->text), btnInfoNew->btn.idCommand);
2159
2160 count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2161
2162 /* position 0 is always separator */
2163 for (i = 1; i < count; i++)
2164 {
2165 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, i, 0);
2166 if (btnInfoNew->btn.idCommand < btnInfo->btn.idCommand)
2167 {
2168 i = SendMessageW(hwndAvail, LB_INSERTSTRING, i, 0);
2169 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2170 return;
2171 }
2172 }
2173 /* id higher than all others add to end */
2174 i = SendMessageW(hwndAvail, LB_ADDSTRING, 0, 0);
2175 SendMessageW(hwndAvail, LB_SETITEMDATA, i, (LPARAM)btnInfoNew);
2176 }
2177
2178 static void TOOLBAR_Cust_MoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexFrom, INT nIndexTo)
2179 {
2180 NMTOOLBARW nmtb;
2181
2182 TRACE("index from %d, index to %d\n", nIndexFrom, nIndexTo);
2183
2184 if (nIndexFrom == nIndexTo)
2185 return;
2186
2187 /* MSDN states that iItem is the index of the button, rather than the
2188 * command ID as used by every other NMTOOLBAR notification */
2189 nmtb.iItem = nIndexFrom;
2190 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2191 {
2192 PCUSTOMBUTTON btnInfo;
2193 NMHDR hdr;
2194 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2195 int count = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2196
2197 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, nIndexFrom, 0);
2198
2199 SendMessageW(hwndList, LB_DELETESTRING, nIndexFrom, 0);
2200 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2201 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2202 SendMessageW(hwndList, LB_SETCURSEL, nIndexTo, 0);
2203
2204 if (nIndexTo <= 0)
2205 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), FALSE);
2206 else
2207 EnableWindow(GetDlgItem(hwnd,IDC_MOVEUP_BTN), TRUE);
2208
2209 /* last item is always separator, so -2 instead of -1 */
2210 if (nIndexTo >= (count - 2))
2211 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), FALSE);
2212 else
2213 EnableWindow(GetDlgItem(hwnd,IDC_MOVEDN_BTN), TRUE);
2214
2215 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, nIndexFrom, 0);
2216 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2217
2218 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2219 }
2220 }
2221
2222 static void TOOLBAR_Cust_AddButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT nIndexAvail, INT nIndexTo)
2223 {
2224 NMTOOLBARW nmtb;
2225
2226 TRACE("Add: nIndexAvail %d, nIndexTo %d\n", nIndexAvail, nIndexTo);
2227
2228 /* MSDN states that iItem is the index of the button, rather than the
2229 * command ID as used by every other NMTOOLBAR notification */
2230 nmtb.iItem = nIndexAvail;
2231 if (TOOLBAR_SendNotify(&nmtb.hdr, custInfo->tbInfo, TBN_QUERYINSERT))
2232 {
2233 PCUSTOMBUTTON btnInfo;
2234 NMHDR hdr;
2235 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2236 HWND hwndAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2237 int count = SendMessageW(hwndAvail, LB_GETCOUNT, 0, 0);
2238
2239 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndAvail, LB_GETITEMDATA, nIndexAvail, 0);
2240
2241 if (nIndexAvail != 0) /* index == 0 indicates separator */
2242 {
2243 /* remove from 'available buttons' list */
2244 SendMessageW(hwndAvail, LB_DELETESTRING, nIndexAvail, 0);
2245 if (nIndexAvail == count-1)
2246 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail-1 , 0);
2247 else
2248 SendMessageW(hwndAvail, LB_SETCURSEL, nIndexAvail , 0);
2249 }
2250 else
2251 {
2252 PCUSTOMBUTTON btnNew;
2253
2254 /* duplicate 'separator' button */
2255 btnNew = Alloc(sizeof(CUSTOMBUTTON));
2256 *btnNew = *btnInfo;
2257 btnInfo = btnNew;
2258 }
2259
2260 /* insert into 'toolbar button' list */
2261 SendMessageW(hwndList, LB_INSERTSTRING, nIndexTo, 0);
2262 SendMessageW(hwndList, LB_SETITEMDATA, nIndexTo, (LPARAM)btnInfo);
2263
2264 SendMessageW(custInfo->tbHwnd, TB_INSERTBUTTONW, nIndexTo, (LPARAM)&(btnInfo->btn));
2265
2266 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2267 }
2268 }
2269
2270 static void TOOLBAR_Cust_RemoveButton(const CUSTDLG_INFO *custInfo, HWND hwnd, INT index)
2271 {
2272 PCUSTOMBUTTON btnInfo;
2273 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2274
2275 TRACE("Remove: index %d\n", index);
2276
2277 btnInfo = (PCUSTOMBUTTON)SendMessageW(hwndList, LB_GETITEMDATA, index, 0);
2278
2279 /* send TBN_QUERYDELETE notification */
2280 if (TOOLBAR_IsButtonRemovable(custInfo->tbInfo, index, btnInfo))
2281 {
2282 NMHDR hdr;
2283
2284 SendMessageW(hwndList, LB_DELETESTRING, index, 0);
2285 SendMessageW(hwndList, LB_SETCURSEL, index , 0);
2286
2287 SendMessageW(custInfo->tbHwnd, TB_DELETEBUTTON, index, 0);
2288
2289 /* insert into 'available button' list */
2290 if (!(btnInfo->btn.fsStyle & BTNS_SEP))
2291 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2292 else
2293 Free(btnInfo);
2294
2295 TOOLBAR_SendNotify(&hdr, custInfo->tbInfo, TBN_TOOLBARCHANGE);
2296 }
2297 }
2298
2299 /* drag list notification function for toolbar buttons list box */
2300 static LRESULT TOOLBAR_Cust_ToolbarDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2301 const DRAGLISTINFO *pDLI)
2302 {
2303 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2304 switch (pDLI->uNotification)
2305 {
2306 case DL_BEGINDRAG:
2307 {
2308 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2309 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2310 /* no dragging for last item (separator) */
2311 if (nCurrentItem >= (nCount - 1)) return FALSE;
2312 return TRUE;
2313 }
2314 case DL_DRAGGING:
2315 {
2316 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2317 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2318 /* no dragging past last item (separator) */
2319 if ((nCurrentItem >= 0) && (nCurrentItem < (nCount - 1)))
2320 {
2321 DrawInsert(hwnd, hwndList, nCurrentItem);
2322 /* FIXME: native uses "move button" cursor */
2323 return DL_COPYCURSOR;
2324 }
2325
2326 /* not over toolbar buttons list */
2327 if (nCurrentItem < 0)
2328 {
2329 POINT ptWindow = pDLI->ptCursor;
2330 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2331 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2332 /* over available buttons list? */
2333 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2334 /* FIXME: native uses "move button" cursor */
2335 return DL_COPYCURSOR;
2336 }
2337 /* clear drag arrow */
2338 DrawInsert(hwnd, hwndList, -1);
2339 return DL_STOPCURSOR;
2340 }
2341 case DL_DROPPED:
2342 {
2343 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2344 INT nIndexFrom = SendMessageW(hwndList, LB_GETCURSEL, 0, 0);
2345 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2346 if ((nIndexTo >= 0) && (nIndexTo < (nCount - 1)))
2347 {
2348 /* clear drag arrow */
2349 DrawInsert(hwnd, hwndList, -1);
2350 /* move item */
2351 TOOLBAR_Cust_MoveButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2352 }
2353 /* not over toolbar buttons list */
2354 if (nIndexTo < 0)
2355 {
2356 POINT ptWindow = pDLI->ptCursor;
2357 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2358 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2359 /* over available buttons list? */
2360 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2361 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, nIndexFrom);
2362 }
2363 break;
2364 }
2365 case DL_CANCELDRAG:
2366 /* Clear drag arrow */
2367 DrawInsert(hwnd, hwndList, -1);
2368 break;
2369 }
2370
2371 return 0;
2372 }
2373
2374 /* drag list notification function for available buttons list box */
2375 static LRESULT TOOLBAR_Cust_AvailDragListNotification(const CUSTDLG_INFO *custInfo, HWND hwnd,
2376 const DRAGLISTINFO *pDLI)
2377 {
2378 HWND hwndList = GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX);
2379 switch (pDLI->uNotification)
2380 {
2381 case DL_BEGINDRAG:
2382 return TRUE;
2383 case DL_DRAGGING:
2384 {
2385 INT nCurrentItem = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2386 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2387 /* no dragging past last item (separator) */
2388 if ((nCurrentItem >= 0) && (nCurrentItem < nCount))
2389 {
2390 DrawInsert(hwnd, hwndList, nCurrentItem);
2391 /* FIXME: native uses "move button" cursor */
2392 return DL_COPYCURSOR;
2393 }
2394
2395 /* not over toolbar buttons list */
2396 if (nCurrentItem < 0)
2397 {
2398 POINT ptWindow = pDLI->ptCursor;
2399 HWND hwndListAvail = GetDlgItem(hwnd, IDC_AVAILBTN_LBOX);
2400 MapWindowPoints(NULL, hwnd, &ptWindow, 1);
2401 /* over available buttons list? */
2402 if (ChildWindowFromPoint(hwnd, ptWindow) == hwndListAvail)
2403 /* FIXME: native uses "move button" cursor */
2404 return DL_COPYCURSOR;
2405 }
2406 /* clear drag arrow */
2407 DrawInsert(hwnd, hwndList, -1);
2408 return DL_STOPCURSOR;
2409 }
2410 case DL_DROPPED:
2411 {
2412 INT nIndexTo = LBItemFromPt(hwndList, pDLI->ptCursor, TRUE);
2413 INT nCount = SendMessageW(hwndList, LB_GETCOUNT, 0, 0);
2414 INT nIndexFrom = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2415 if ((nIndexTo >= 0) && (nIndexTo < nCount))
2416 {
2417 /* clear drag arrow */
2418 DrawInsert(hwnd, hwndList, -1);
2419 /* add item */
2420 TOOLBAR_Cust_AddButton(custInfo, hwnd, nIndexFrom, nIndexTo);
2421 }
2422 }
2423 case DL_CANCELDRAG:
2424 /* Clear drag arrow */
2425 DrawInsert(hwnd, hwndList, -1);
2426 break;
2427 }
2428 return 0;
2429 }
2430
2431 extern UINT uDragListMessage DECLSPEC_HIDDEN;
2432
2433 /***********************************************************************
2434 * TOOLBAR_CustomizeDialogProc
2435 * This function implements the toolbar customization dialog.
2436 */
2437 static INT_PTR CALLBACK
2438 TOOLBAR_CustomizeDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2439 {
2440 PCUSTDLG_INFO custInfo = (PCUSTDLG_INFO)GetWindowLongPtrW (hwnd, DWLP_USER);
2441 PCUSTOMBUTTON btnInfo;
2442 NMTOOLBARA nmtb;
2443 TOOLBAR_INFO *infoPtr = custInfo ? custInfo->tbInfo : NULL;
2444
2445 switch (uMsg)
2446 {
2447 case WM_INITDIALOG:
2448 custInfo = (PCUSTDLG_INFO)lParam;
2449 SetWindowLongPtrW (hwnd, DWLP_USER, (LONG_PTR)custInfo);
2450
2451 if (custInfo)
2452 {
2453 WCHAR Buffer[256];
2454 int i = 0;
2455 int index;
2456 NMTBINITCUSTOMIZE nmtbic;
2457
2458 infoPtr = custInfo->tbInfo;
2459
2460 /* send TBN_QUERYINSERT notification */
2461 nmtb.iItem = custInfo->tbInfo->nNumButtons;
2462
2463 if (!TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT))
2464 return FALSE;
2465
2466 nmtbic.hwndDialog = hwnd;
2467 /* Send TBN_INITCUSTOMIZE notification */
2468 if (TOOLBAR_SendNotify (&nmtbic.hdr, infoPtr, TBN_INITCUSTOMIZE) ==
2469 TBNRF_HIDEHELP)
2470 {
2471 TRACE("TBNRF_HIDEHELP requested\n");
2472 ShowWindow(GetDlgItem(hwnd, IDC_HELP_BTN), SW_HIDE);
2473 }
2474
2475 /* add items to 'toolbar buttons' list and check if removable */
2476 for (i = 0; i < custInfo->tbInfo->nNumButtons; i++)
2477 {
2478 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2479 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2480 btnInfo->btn.fsStyle = BTNS_SEP;
2481 btnInfo->bVirtual = FALSE;
2482 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2483
2484 /* send TBN_QUERYDELETE notification */
2485 btnInfo->bRemovable = TOOLBAR_IsButtonRemovable(infoPtr, i, btnInfo);
2486
2487 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, 0);
2488 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2489 }
2490
2491 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2492
2493 /* insert separator button into 'available buttons' list */
2494 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2495 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2496 btnInfo->btn.fsStyle = BTNS_SEP;
2497 btnInfo->bVirtual = FALSE;
2498 btnInfo->bRemovable = TRUE;
2499 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2500 index = (int)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2501 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2502
2503 /* insert all buttons into dsa */
2504 for (i = 0;; i++)
2505 {
2506 /* send TBN_GETBUTTONINFO notification */
2507 NMTOOLBARW nmtb;
2508 nmtb.iItem = i;
2509 nmtb.pszText = Buffer;
2510 nmtb.cchText = 256;
2511
2512 /* Clear previous button's text */
2513 ZeroMemory(nmtb.pszText, nmtb.cchText * sizeof(WCHAR));
2514
2515 if (!TOOLBAR_GetButtonInfo(infoPtr, &nmtb))
2516 break;
2517
2518 TRACE("WM_INITDIALOG style: %x iItem(%d) idCommand(%d) iString(%ld) %s\n",
2519 nmtb.tbButton.fsStyle, i,
2520 nmtb.tbButton.idCommand,
2521 nmtb.tbButton.iString,
2522 nmtb.tbButton.iString >= 0 ? debugstr_w(infoPtr->strings[nmtb.tbButton.iString])
2523 : "");
2524
2525 /* insert button into the appropriate list */
2526 index = TOOLBAR_GetButtonIndex (custInfo->tbInfo, nmtb.tbButton.idCommand, FALSE);
2527 if (index == -1)
2528 {
2529 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2530 btnInfo->bVirtual = FALSE;
2531 btnInfo->bRemovable = TRUE;
2532 }
2533 else
2534 {
2535 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd,
2536 IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2537 }
2538
2539 btnInfo->btn = nmtb.tbButton;
2540 if (!(nmtb.tbButton.fsStyle & BTNS_SEP))
2541 {
2542 if (lstrlenW(nmtb.pszText))
2543 lstrcpyW(btnInfo->text, nmtb.pszText);
2544 else if (nmtb.tbButton.iString >= 0 &&
2545 nmtb.tbButton.iString < infoPtr->nNumStrings)
2546 {
2547 lstrcpyW(btnInfo->text,
2548 infoPtr->strings[nmtb.tbButton.iString]);
2549 }
2550 }
2551
2552 if (index == -1)
2553 TOOLBAR_Cust_InsertAvailButton(hwnd, btnInfo);
2554 }
2555
2556 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMHEIGHT, 0, infoPtr->nBitmapHeight + 8);
2557
2558 /* select first item in the 'available' list */
2559 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETCURSEL, 0, 0);
2560
2561 /* append 'virtual' separator button to the 'toolbar buttons' list */
2562 btnInfo = Alloc(sizeof(CUSTOMBUTTON));
2563 memset (&btnInfo->btn, 0, sizeof(TBBUTTON));
2564 btnInfo->btn.fsStyle = BTNS_SEP;
2565 btnInfo->bVirtual = TRUE;
2566 btnInfo->bRemovable = FALSE;
2567 LoadStringW (COMCTL32_hModule, IDS_SEPARATOR, btnInfo->text, 64);
2568 index = (int)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_ADDSTRING, 0, (LPARAM)btnInfo);
2569 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, index, (LPARAM)btnInfo);
2570
2571 /* select last item in the 'toolbar' list */
2572 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETCURSEL, index, 0);
2573 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETTOPINDEX, index, 0);
2574
2575 MakeDragList(GetDlgItem(hwnd, IDC_TOOLBARBTN_LBOX));
2576 MakeDragList(GetDlgItem(hwnd, IDC_AVAILBTN_LBOX));
2577
2578 /* set focus and disable buttons */
2579 PostMessageW (hwnd, WM_USER, 0, 0);
2580 }
2581 return TRUE;
2582
2583 case WM_USER:
2584 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2585 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2586 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), FALSE);
2587 SetFocus (GetDlgItem (hwnd, IDC_TOOLBARBTN_LBOX));
2588 return TRUE;
2589
2590 case WM_CLOSE:
2591 EndDialog(hwnd, FALSE);
2592 return TRUE;
2593
2594 case WM_COMMAND:
2595 switch (LOWORD(wParam))
2596 {
2597 case IDC_TOOLBARBTN_LBOX:
2598 if (HIWORD(wParam) == LBN_SELCHANGE)
2599 {
2600 PCUSTOMBUTTON btnInfo;
2601 NMTOOLBARA nmtb;
2602 int count;
2603 int index;
2604
2605 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2606 index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2607
2608 /* send TBN_QUERYINSERT notification */
2609 nmtb.iItem = index;
2610 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_QUERYINSERT);
2611
2612 /* get list box item */
2613 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, index, 0);
2614
2615 if (index == (count - 1))
2616 {
2617 /* last item (virtual separator) */
2618 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2619 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2620 }
2621 else if (index == (count - 2))
2622 {
2623 /* second last item (last non-virtual item) */
2624 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2625 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), FALSE);
2626 }
2627 else if (index == 0)
2628 {
2629 /* first item */
2630 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), FALSE);
2631 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2632 }
2633 else
2634 {
2635 EnableWindow (GetDlgItem (hwnd,IDC_MOVEUP_BTN), TRUE);
2636 EnableWindow (GetDlgItem (hwnd,IDC_MOVEDN_BTN), TRUE);
2637 }
2638
2639 EnableWindow (GetDlgItem (hwnd,IDC_REMOVE_BTN), btnInfo->bRemovable);
2640 }
2641 break;
2642
2643 case IDC_MOVEUP_BTN:
2644 {
2645 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2646 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index-1);
2647 }
2648 break;
2649
2650 case IDC_MOVEDN_BTN: /* move down */
2651 {
2652 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2653 TOOLBAR_Cust_MoveButton(custInfo, hwnd, index, index+1);
2654 }
2655 break;
2656
2657 case IDC_REMOVE_BTN: /* remove button */
2658 {
2659 int index = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2660
2661 if (LB_ERR == index)
2662 break;
2663
2664 TOOLBAR_Cust_RemoveButton(custInfo, hwnd, index);
2665 }
2666 break;
2667 case IDC_HELP_BTN:
2668 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_CUSTHELP);
2669 break;
2670 case IDC_RESET_BTN:
2671 TOOLBAR_SendNotify(&nmtb.hdr, infoPtr, TBN_RESET);
2672 break;
2673
2674 case IDOK: /* Add button */
2675 {
2676 int index;
2677 int indexto;
2678
2679 index = SendDlgItemMessageW(hwnd, IDC_AVAILBTN_LBOX, LB_GETCURSEL, 0, 0);
2680 indexto = SendDlgItemMessageW(hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCURSEL, 0, 0);
2681
2682 TOOLBAR_Cust_AddButton(custInfo, hwnd, index, indexto);
2683 }
2684 break;
2685
2686 case IDCANCEL:
2687 EndDialog(hwnd, FALSE);
2688 break;
2689 }
2690 return TRUE;
2691
2692 case WM_DESTROY:
2693 {
2694 int count;
2695 int i;
2696
2697 /* delete items from 'toolbar buttons' listbox*/
2698 count = SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETCOUNT, 0, 0);
2699 for (i = 0; i < count; i++)
2700 {
2701 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_GETITEMDATA, i, 0);
2702 Free(btnInfo);
2703 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_SETITEMDATA, 0, 0);
2704 }
2705 SendDlgItemMessageW (hwnd, IDC_TOOLBARBTN_LBOX, LB_RESETCONTENT, 0, 0);
2706
2707
2708 /* delete items from 'available buttons' listbox*/
2709 count = SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETCOUNT, 0, 0);
2710 for (i = 0; i < count; i++)
2711 {
2712 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_GETITEMDATA, i, 0);
2713 Free(btnInfo);
2714 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_SETITEMDATA, i, 0);
2715 }
2716 SendDlgItemMessageW (hwnd, IDC_AVAILBTN_LBOX, LB_RESETCONTENT, 0, 0);
2717 }
2718 return TRUE;
2719
2720 case WM_DRAWITEM:
2721 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2722 {
2723 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
2724 RECT rcButton;
2725 RECT rcText;
2726 HPEN hPen, hOldPen;
2727 HBRUSH hOldBrush;
2728 COLORREF oldText = 0;
2729 COLORREF oldBk = 0;
2730
2731 /* get item data */
2732 btnInfo = (PCUSTOMBUTTON)SendDlgItemMessageW (hwnd, wParam, LB_GETITEMDATA, lpdis->itemID, 0);
2733 if (btnInfo == NULL)
2734 {
2735 FIXME("btnInfo invalid!\n");
2736 return TRUE;
2737 }
2738
2739 /* set colors and select objects */
2740 oldBk = SetBkColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2741 if (btnInfo->bVirtual)
2742 oldText = SetTextColor (lpdis->hDC, comctl32_color.clrGrayText);
2743 else
2744 oldText = SetTextColor (lpdis->hDC, (lpdis->itemState & ODS_FOCUS)?comctl32_color.clrHighlightText:comctl32_color.clrWindowText);
2745 hPen = CreatePen( PS_SOLID, 1,
2746 (lpdis->itemState & ODS_SELECTED)?comctl32_color.clrHighlight:comctl32_color.clrWindow);
2747 hOldPen = SelectObject (lpdis->hDC, hPen );
2748 hOldBrush = SelectObject (lpdis->hDC, GetSysColorBrush ((lpdis->itemState & ODS_FOCUS)?COLOR_HIGHLIGHT:COLOR_WINDOW));
2749
2750 /* fill background rectangle */
2751 Rectangle (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
2752 lpdis->rcItem.right, lpdis->rcItem.bottom);
2753
2754 /* calculate button and text rectangles */
2755 rcButton = lpdis->rcItem;
2756 InflateRect (&rcButton, -1, -1);
2757 rcText = rcButton;
2758 rcButton.right = rcButton.left + custInfo->tbInfo->nBitmapWidth + 6;
2759 rcText.left = rcButton.right + 2;
2760
2761 /* draw focus rectangle */
2762 if (lpdis->itemState & ODS_FOCUS)
2763 DrawFocusRect (lpdis->hDC, &lpdis->rcItem);
2764
2765 /* draw button */
2766 if (!(infoPtr->dwStyle & TBSTYLE_FLAT))
2767 DrawEdge (lpdis->hDC, &rcButton, EDGE_RAISED, BF_RECT|BF_MIDDLE|BF_SOFT);
2768
2769 /* draw image and text */
2770 if ((btnInfo->btn.fsStyle & BTNS_SEP) == 0) {
2771 HIMAGELIST himl = GETDEFIMAGELIST(infoPtr, GETHIMLID(infoPtr,
2772 btnInfo->btn.iBitmap));
2773 ImageList_Draw (himl, GETIBITMAP(infoPtr, btnInfo->btn.iBitmap),
2774 lpdis->hDC, rcButton.left+3, rcButton.top+3, ILD_NORMAL);
2775 }
2776 DrawTextW (lpdis->hDC, btnInfo->text, -1, &rcText,
2777 DT_LEFT | DT_VCENTER | DT_SINGLELINE);
2778
2779 /* delete objects and reset colors */
2780 SelectObject (lpdis->hDC, hOldBrush);
2781 SelectObject (lpdis->hDC, hOldPen);
2782 SetBkColor (lpdis->hDC, oldBk);
2783 SetTextColor (lpdis->hDC, oldText);
2784 DeleteObject( hPen );
2785 return TRUE;
2786 }
2787 return FALSE;
2788
2789 case WM_MEASUREITEM:
2790 if (wParam == IDC_AVAILBTN_LBOX || wParam == IDC_TOOLBARBTN_LBOX)
2791 {
2792 MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT*)lParam;
2793
2794 lpmis->itemHeight = 15 + 8; /* default height */
2795
2796 return TRUE;
2797 }
2798 return FALSE;
2799
2800 default:
2801 if (uDragListMessage && (uMsg == uDragListMessage))
2802 {
2803 if (wParam == IDC_TOOLBARBTN_LBOX)
2804 {
2805 LRESULT res = TOOLBAR_Cust_ToolbarDragListNotification(
2806 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2807 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2808 return TRUE;
2809 }
2810 else if (wParam == IDC_AVAILBTN_LBOX)
2811 {
2812 LRESULT res = TOOLBAR_Cust_AvailDragListNotification(
2813 custInfo, hwnd, (DRAGLISTINFO *)lParam);
2814 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, res);
2815 return TRUE;
2816 }
2817 }
2818 return FALSE;
2819 }
2820 }
2821
2822 static BOOL
2823 TOOLBAR_AddBitmapToImageList(TOOLBAR_INFO *infoPtr, HIMAGELIST himlDef, const TBITMAP_INFO *bitmap)
2824 {
2825 HBITMAP hbmLoad;
2826 INT nCountBefore = ImageList_GetImageCount(himlDef);
2827 INT nCountAfter;
2828 INT cxIcon, cyIcon;
2829 INT nAdded;
2830 INT nIndex;
2831
2832 TRACE("adding hInst=%p nID=%d nButtons=%d\n", bitmap->hInst, bitmap->nID, bitmap->nButtons);
2833 /* Add bitmaps to the default image list */
2834 if (bitmap->hInst == NULL) /* a handle was passed */
2835 hbmLoad = CopyImage(ULongToHandle(bitmap->nID), IMAGE_BITMAP, 0, 0, 0);
2836 else if (bitmap->hInst == COMCTL32_hModule)
2837 hbmLoad = LoadImageW( bitmap->hInst, MAKEINTRESOURCEW(bitmap->nID),
2838 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
2839 else
2840 hbmLoad = CreateMappedBitmap(bitmap->hInst, bitmap->nID, 0, NULL, 0);
2841
2842 /* enlarge the bitmap if needed */
2843 ImageList_GetIconSize(himlDef, &cxIcon, &cyIcon);
2844 if (bitmap->hInst != COMCTL32_hModule)
2845 COMCTL32_EnsureBitmapSize(&hbmLoad, cxIcon*(INT)bitmap->nButtons, cyIcon, comctl32_color.clrBtnFace);
2846
2847 nIndex = ImageList_AddMasked(himlDef, hbmLoad, comctl32_color.clrBtnFace);
2848 DeleteObject(hbmLoad);
2849 if (nIndex == -1)
2850 return FALSE;
2851
2852 nCountAfter = ImageList_GetImageCount(himlDef);
2853 nAdded = nCountAfter - nCountBefore;
2854 if (bitmap->nButtons == 0) /* wParam == 0 is special and means add only one image */
2855 {
2856 ImageList_SetImageCount(himlDef, nCountBefore + 1);
2857 } else if (nAdded > (INT)bitmap->nButtons) {
2858 TRACE("Added more images than wParam: Previous image number %i added %i while wParam %i. Images in list %i\n",
2859 nCountBefore, nAdded, bitmap->nButtons, nCountAfter);
2860 }
2861
2862 infoPtr->nNumBitmaps += nAdded;
2863 return TRUE;
2864 }
2865
2866 static void
2867 TOOLBAR_CheckImageListIconSize(TOOLBAR_INFO *infoPtr)
2868 {
2869 HIMAGELIST himlDef;
2870 HIMAGELIST himlNew;
2871 INT cx, cy;
2872 INT i;
2873
2874 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2875 if (himlDef == NULL || himlDef != infoPtr->himlInt)
2876 return;
2877 if (!ImageList_GetIconSize(himlDef, &cx, &cy))
2878 return;
2879 if (cx == infoPtr->nBitmapWidth && cy == infoPtr->nBitmapHeight)
2880 return;
2881
2882 TRACE("Update icon size: %dx%d -> %dx%d\n",
2883 cx, cy, infoPtr->nBitmapWidth, infoPtr->nBitmapHeight);
2884
2885 himlNew = ImageList_Create(infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2886 ILC_COLOR32|ILC_MASK, 8, 2);
2887 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2888 TOOLBAR_AddBitmapToImageList(infoPtr, himlNew, &infoPtr->bitmaps[i]);
2889 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlNew, 0);
2890 infoPtr->himlInt = himlNew;
2891
2892 infoPtr->nNumBitmaps -= ImageList_GetImageCount(himlDef);
2893 ImageList_Destroy(himlDef);
2894 }
2895
2896 /***********************************************************************
2897 * TOOLBAR_AddBitmap: Add the bitmaps to the default image list.
2898 *
2899 */
2900 static LRESULT
2901 TOOLBAR_AddBitmap (TOOLBAR_INFO *infoPtr, INT count, const TBADDBITMAP *lpAddBmp)
2902 {
2903 TBITMAP_INFO info;
2904 INT iSumButtons, i;
2905 HIMAGELIST himlDef;
2906
2907 TRACE("hwnd=%p count=%d lpAddBmp=%p\n", infoPtr->hwndSelf, count, lpAddBmp);
2908 if (!lpAddBmp)
2909 return -1;
2910
2911 if (lpAddBmp->hInst == HINST_COMMCTRL)
2912 {
2913 info.hInst = COMCTL32_hModule;
2914 switch (lpAddBmp->nID)
2915 {
2916 case IDB_STD_SMALL_COLOR:
2917 info.nButtons = 15;
2918 info.nID = IDB_STD_SMALL;
2919 break;
2920 case IDB_STD_LARGE_COLOR:
2921 info.nButtons = 15;
2922 info.nID = IDB_STD_LARGE;
2923 break;
2924 case IDB_VIEW_SMALL_COLOR:
2925 info.nButtons = 12;
2926 info.nID = IDB_VIEW_SMALL;
2927 break;
2928 case IDB_VIEW_LARGE_COLOR:
2929 info.nButtons = 12;
2930 info.nID = IDB_VIEW_LARGE;
2931 break;
2932 case IDB_HIST_SMALL_COLOR:
2933 info.nButtons = 5;
2934 info.nID = IDB_HIST_SMALL;
2935 break;
2936 case IDB_HIST_LARGE_COLOR:
2937 info.nButtons = 5;
2938 info.nID = IDB_HIST_LARGE;
2939 break;
2940 default:
2941 return -1;
2942 }
2943
2944 TRACE ("adding %d internal bitmaps!\n", info.nButtons);
2945
2946 /* Windows resize all the buttons to the size of a newly added standard image */
2947 if (lpAddBmp->nID & 1)
2948 {
2949 /* large icons: 24x24. Will make the button 31x30 */
2950 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
2951 }
2952 else
2953 {
2954 /* small icons: 16x16. Will make the buttons 23x22 */
2955 SendMessageW (infoPtr->hwndSelf, TB_SETBITMAPSIZE, 0, MAKELPARAM(16, 16));
2956 }
2957
2958 TOOLBAR_CalcToolbar (infoPtr);
2959 }
2960 else
2961 {
2962 info.nButtons = count;
2963 info.hInst = lpAddBmp->hInst;
2964 info.nID = lpAddBmp->nID;
2965 TRACE("adding %d bitmaps!\n", info.nButtons);
2966 }
2967
2968 /* check if the bitmap is already loaded and compute iSumButtons */
2969 iSumButtons = 0;
2970 for (i = 0; i < infoPtr->nNumBitmapInfos; i++)
2971 {
2972 if (infoPtr->bitmaps[i].hInst == info.hInst &&
2973 infoPtr->bitmaps[i].nID == info.nID)
2974 return iSumButtons;
2975 iSumButtons += infoPtr->bitmaps[i].nButtons;
2976 }
2977
2978 if (!infoPtr->cimlDef) {
2979 /* create new default image list */
2980 TRACE ("creating default image list!\n");
2981
2982 himlDef = ImageList_Create (infoPtr->nBitmapWidth, infoPtr->nBitmapHeight,
2983 ILC_COLOR32 | ILC_MASK, info.nButtons, 2);
2984 TOOLBAR_InsertImageList(&infoPtr->himlDef, &infoPtr->cimlDef, himlDef, 0);
2985 infoPtr->himlInt = himlDef;
2986 }
2987 else {
2988 himlDef = GETDEFIMAGELIST(infoPtr, 0);
2989 }
2990
2991 if (!himlDef) {
2992 WARN("No default image list available\n");
2993 return -1;
2994 }
2995
2996 if (!TOOLBAR_AddBitmapToImageList(infoPtr, himlDef, &info))
2997 return -1;
2998
2999 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
3000 infoPtr->bitmaps = ReAlloc(infoPtr->bitmaps, (infoPtr->nNumBitmapInfos + 1) * sizeof(TBITMAP_INFO));
3001 infoPtr->bitmaps[infoPtr->nNumBitmapInfos] = info;
3002 infoPtr->nNumBitmapInfos++;
3003 TRACE("Number of bitmap infos: %d\n", infoPtr->nNumBitmapInfos);
3004
3005 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3006 return iSumButtons;
3007 }
3008
3009
3010 static LRESULT
3011 TOOLBAR_AddButtonsT(TOOLBAR_INFO *infoPtr, INT nAddButtons, const TBBUTTON* lpTbb, BOOL fUnicode)
3012 {
3013 TRACE("adding %d buttons (unicode=%d)!\n", nAddButtons, fUnicode);
3014
3015 return TOOLBAR_InternalInsertButtonsT(infoPtr, -1, nAddButtons, lpTbb, fUnicode);
3016 }
3017
3018
3019 static LRESULT
3020 TOOLBAR_AddStringW (TOOLBAR_INFO *infoPtr, HINSTANCE hInstance, LPARAM lParam)
3021 {
3022 #define MAX_RESOURCE_STRING_LENGTH 512
3023 BOOL fFirstString = (infoPtr->nNumStrings == 0);
3024 INT nIndex = infoPtr->nNumStrings;
3025
3026 TRACE("%p, %lx\n", hInstance, lParam);
3027
3028 if (IS_INTRESOURCE(lParam)) {
3029 WCHAR szString[MAX_RESOURCE_STRING_LENGTH];
3030 WCHAR delimiter;
3031 WCHAR *next_delim;
3032 HRSRC hrsrc;
3033 WCHAR *p;
3034 INT len;
3035
3036 TRACE("adding string from resource\n");
3037
3038 if (!hInstance) return -1;
3039
3040 hrsrc = FindResourceW( hInstance, MAKEINTRESOURCEW((LOWORD(lParam) >> 4) + 1),
3041 (LPWSTR)RT_STRING );