]> git.saurik.com Git - wxWidgets.git/blob - src/msw/toolbar.cpp
No real changes, just removed unnecessary SetInitialSize() call.
[wxWidgets.git] / src / msw / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
28
29 #include "wx/toolbar.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/dynarray.h"
34 #include "wx/frame.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/settings.h"
38 #include "wx/bitmap.h"
39 #include "wx/dcmemory.h"
40 #include "wx/control.h"
41 #include "wx/app.h" // for GetComCtl32Version
42 #include "wx/image.h"
43 #include "wx/stattext.h"
44 #endif
45
46 #include "wx/artprov.h"
47 #include "wx/sysopt.h"
48 #include "wx/dcclient.h"
49 #include "wx/scopedarray.h"
50
51 #include "wx/msw/private.h"
52 #include "wx/msw/dc.h"
53
54 #if wxUSE_UXTHEME
55 #include "wx/msw/uxtheme.h"
56 #endif
57
58 // this define controls whether the code for button colours remapping (only
59 // useful for 16 or 256 colour images) is active at all, it's always turned off
60 // for CE where it doesn't compile (and is probably not needed anyhow) and may
61 // also be turned off for other systems if you always use 24bpp images and so
62 // never need it
63 #ifndef __WXWINCE__
64 #define wxREMAP_BUTTON_COLOURS
65 #endif // !__WXWINCE__
66
67 // ----------------------------------------------------------------------------
68 // constants
69 // ----------------------------------------------------------------------------
70
71 // these standard constants are not always defined in compilers headers
72
73 // Styles
74 #ifndef TBSTYLE_FLAT
75 #define TBSTYLE_LIST 0x1000
76 #define TBSTYLE_FLAT 0x0800
77 #endif
78
79 #ifndef TBSTYLE_TRANSPARENT
80 #define TBSTYLE_TRANSPARENT 0x8000
81 #endif
82
83 #ifndef TBSTYLE_TOOLTIPS
84 #define TBSTYLE_TOOLTIPS 0x0100
85 #endif
86
87 // Messages
88 #ifndef TB_GETSTYLE
89 #define TB_SETSTYLE (WM_USER + 56)
90 #define TB_GETSTYLE (WM_USER + 57)
91 #endif
92
93 #ifndef TB_HITTEST
94 #define TB_HITTEST (WM_USER + 69)
95 #endif
96
97 #ifndef TB_GETMAXSIZE
98 #define TB_GETMAXSIZE (WM_USER + 83)
99 #endif
100
101 // ----------------------------------------------------------------------------
102 // wxWin macros
103 // ----------------------------------------------------------------------------
104
105 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
106
107 /*
108 TOOLBAR PROPERTIES
109 tool
110 bitmap
111 bitmap2
112 tooltip
113 longhelp
114 radio (bool)
115 toggle (bool)
116 separator
117 style ( wxNO_BORDER | wxTB_HORIZONTAL)
118 bitmapsize
119 margins
120 packing
121 separation
122
123 dontattachtoframe
124 */
125
126 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
127 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
128 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
129 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground)
130 END_EVENT_TABLE()
131
132 // ----------------------------------------------------------------------------
133 // private classes
134 // ----------------------------------------------------------------------------
135
136 class wxToolBarTool : public wxToolBarToolBase
137 {
138 public:
139 wxToolBarTool(wxToolBar *tbar,
140 int id,
141 const wxString& label,
142 const wxBitmap& bmpNormal,
143 const wxBitmap& bmpDisabled,
144 wxItemKind kind,
145 wxObject *clientData,
146 const wxString& shortHelp,
147 const wxString& longHelp)
148 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
149 clientData, shortHelp, longHelp)
150 {
151 m_nSepCount = 0;
152 m_staticText = 0;
153 }
154
155 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
156 : wxToolBarToolBase(tbar, control, label)
157 {
158 if ( IsControl() && !m_label.empty() )
159 {
160 // create a control to render the control's label
161 m_staticText = new wxStaticText
162 (
163 m_tbar,
164 wxID_ANY,
165 m_label,
166 wxDefaultPosition,
167 wxDefaultSize,
168 wxALIGN_CENTRE | wxST_NO_AUTORESIZE
169 );
170 }
171 else // no label
172 {
173 m_staticText = NULL;
174 }
175
176 m_nSepCount = 1;
177 }
178
179 virtual ~wxToolBarTool()
180 {
181 delete m_staticText;
182 }
183
184 virtual void SetLabel(const wxString& label)
185 {
186 if ( label == m_label )
187 return;
188
189 wxToolBarToolBase::SetLabel(label);
190
191 if ( m_staticText )
192 m_staticText->SetLabel(label);
193
194 // we need to update the label shown in the toolbar because it has a
195 // pointer to the internal buffer of the old label
196 //
197 // TODO: use TB_SETBUTTONINFO
198 }
199
200 wxStaticText* GetStaticText()
201 {
202 wxASSERT_MSG( IsControl(),
203 wxT("only makes sense for embedded control tools") );
204
205 return m_staticText;
206 }
207
208 // set/get the number of separators which we use to cover the space used by
209 // a control in the toolbar
210 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
211 size_t GetSeparatorsCount() const { return m_nSepCount; }
212
213 private:
214 size_t m_nSepCount;
215 wxStaticText *m_staticText;
216
217 wxDECLARE_NO_COPY_CLASS(wxToolBarTool);
218 };
219
220 // ----------------------------------------------------------------------------
221 // helper functions
222 // ----------------------------------------------------------------------------
223
224 // return the rectangle of the item at the given index
225 //
226 // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
227 // r.right or r.bottom with 0 to check for this
228 static RECT wxGetTBItemRect(HWND hwnd, int index)
229 {
230 RECT r;
231
232 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
233 // only appeared in v4.70 of comctl32.dll
234 if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) )
235 {
236 wxLogLastError(wxT("TB_GETITEMRECT"));
237
238 r.top =
239 r.left =
240 r.right =
241 r.bottom = 0;
242 }
243
244 return r;
245 }
246
247 // ============================================================================
248 // implementation
249 // ============================================================================
250
251 // ----------------------------------------------------------------------------
252 // wxToolBarTool
253 // ----------------------------------------------------------------------------
254
255 wxToolBarToolBase *wxToolBar::CreateTool(int id,
256 const wxString& label,
257 const wxBitmap& bmpNormal,
258 const wxBitmap& bmpDisabled,
259 wxItemKind kind,
260 wxObject *clientData,
261 const wxString& shortHelp,
262 const wxString& longHelp)
263 {
264 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
265 clientData, shortHelp, longHelp);
266 }
267
268 wxToolBarToolBase *
269 wxToolBar::CreateTool(wxControl *control, const wxString& label)
270 {
271 return new wxToolBarTool(this, control, label);
272 }
273
274 // ----------------------------------------------------------------------------
275 // wxToolBar construction
276 // ----------------------------------------------------------------------------
277
278 void wxToolBar::Init()
279 {
280 m_hBitmap = 0;
281 m_disabledImgList = NULL;
282
283 m_nButtons = 0;
284
285 // even though modern Windows applications typically use 24*24 (or even
286 // 32*32) size for their bitmaps, the native control itself still uses the
287 // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so
288 // default to it so that we don't call SetToolBitmapSize() unnecessarily in
289 // wxToolBarBase::AdjustToolBitmapSize()
290 m_defaultWidth = 16;
291 m_defaultHeight = 15;
292
293 m_pInTool = NULL;
294 }
295
296 bool wxToolBar::Create(wxWindow *parent,
297 wxWindowID id,
298 const wxPoint& pos,
299 const wxSize& size,
300 long style,
301 const wxString& name)
302 {
303 // common initialisation
304 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
305 return false;
306
307 FixupStyle();
308
309 // MSW-specific initialisation
310 if ( !MSWCreateToolbar(pos, size) )
311 return false;
312
313 wxSetCCUnicodeFormat(GetHwnd());
314
315 // workaround for flat toolbar on Windows XP classic style: we have to set
316 // the style after creating the control; doing it at creation time doesn't work
317 #if wxUSE_UXTHEME
318 if ( style & wxTB_FLAT )
319 {
320 LRESULT style = GetMSWToolbarStyle();
321
322 if ( !(style & TBSTYLE_FLAT) )
323 ::SendMessage(GetHwnd(), TB_SETSTYLE, 0, style | TBSTYLE_FLAT);
324 }
325 #endif // wxUSE_UXTHEME
326
327 return true;
328 }
329
330 bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size)
331 {
332 if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) )
333 return false;
334
335 // toolbar-specific post initialisation
336 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
337
338 #ifdef TB_SETEXTENDEDSTYLE
339 if ( wxApp::GetComCtl32Version() >= 471 )
340 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
341 #endif
342
343 return true;
344 }
345
346 void wxToolBar::Recreate()
347 {
348 const HWND hwndOld = GetHwnd();
349 if ( !hwndOld )
350 {
351 // we haven't been created yet, no need to recreate
352 return;
353 }
354
355 // get the position and size before unsubclassing the old toolbar
356 const wxPoint pos = GetPosition();
357 const wxSize size = GetSize();
358
359 UnsubclassWin();
360
361 if ( !MSWCreateToolbar(pos, size) )
362 {
363 // what can we do?
364 wxFAIL_MSG( wxT("recreating the toolbar failed") );
365
366 return;
367 }
368
369 // reparent all our children under the new toolbar
370 for ( wxWindowList::compatibility_iterator node = m_children.GetFirst();
371 node;
372 node = node->GetNext() )
373 {
374 wxWindow *win = node->GetData();
375 if ( !win->IsTopLevel() )
376 ::SetParent(GetHwndOf(win), GetHwnd());
377 }
378
379 // only destroy the old toolbar now --
380 // after all the children had been reparented
381 ::DestroyWindow(hwndOld);
382
383 // it is for the old bitmap control and can't be used with the new one
384 if ( m_hBitmap )
385 {
386 ::DeleteObject((HBITMAP) m_hBitmap);
387 m_hBitmap = 0;
388 }
389
390 if ( m_disabledImgList )
391 {
392 delete m_disabledImgList;
393 m_disabledImgList = NULL;
394 }
395
396 Realize();
397 }
398
399 wxToolBar::~wxToolBar()
400 {
401 // we must refresh the frame size when the toolbar is deleted but the frame
402 // is not - otherwise toolbar leaves a hole in the place it used to occupy
403 SendSizeEventToParent();
404
405 if ( m_hBitmap )
406 ::DeleteObject((HBITMAP) m_hBitmap);
407
408 delete m_disabledImgList;
409 }
410
411 wxSize wxToolBar::DoGetBestSize() const
412 {
413 wxSize sizeBest;
414
415 SIZE size;
416 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) )
417 {
418 // maybe an old (< 0x400) Windows version? try to approximate the
419 // toolbar size ourselves
420 sizeBest = GetToolSize();
421 sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders
422 sizeBest.x *= GetToolsCount();
423
424 // reverse horz and vertical components if necessary
425 if ( IsVertical() )
426 {
427 int t = sizeBest.x;
428 sizeBest.x = sizeBest.y;
429 sizeBest.y = t;
430 }
431 }
432 else // TB_GETMAXSIZE succeeded
433 {
434 // but it could still return an incorrect result due to what appears to
435 // be a bug in old comctl32.dll versions which don't handle controls in
436 // the toolbar correctly, so work around it (see SF patch 1902358)
437 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
438 {
439 // calculate the toolbar width in alternative way
440 const RECT rcFirst = wxGetTBItemRect(GetHwnd(), 0);
441 const RECT rcLast = wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
442
443 const int widthAlt = rcLast.right - rcFirst.left;
444 if ( widthAlt > size.cx )
445 size.cx = widthAlt;
446 }
447
448 sizeBest.x = size.cx;
449 sizeBest.y = size.cy;
450 }
451
452 if ( !IsVertical() )
453 {
454 // Without the extra height, DoGetBestSize can report a size that's
455 // smaller than the actual window, causing windows to overlap slightly
456 // in some circumstances, leading to missing borders (especially noticeable
457 // in AUI layouts).
458 if (!(GetWindowStyle() & wxTB_NODIVIDER))
459 sizeBest.y += 2;
460 sizeBest.y ++;
461 }
462
463 CacheBestSize(sizeBest);
464
465 return sizeBest;
466 }
467
468 WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const
469 {
470 // toolbars never have border, giving one to them results in broken
471 // appearance
472 WXDWORD msStyle = wxControl::MSWGetStyle
473 (
474 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
475 );
476
477 if ( !(style & wxTB_NO_TOOLTIPS) )
478 msStyle |= TBSTYLE_TOOLTIPS;
479
480 if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) )
481 {
482 // static as it doesn't change during the program lifetime
483 static const int s_verComCtl = wxApp::GetComCtl32Version();
484
485 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
486 // style with 6.00 (part of Windows XP) leads to the toolbar with
487 // incorrect background colour - and not using it still results in the
488 // correct (flat) toolbar, so don't use it there
489 if ( s_verComCtl > 400 && s_verComCtl < 600 )
490 msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
491
492 if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT )
493 msStyle |= TBSTYLE_LIST;
494 }
495
496 if ( style & wxTB_NODIVIDER )
497 msStyle |= CCS_NODIVIDER;
498
499 if ( style & wxTB_NOALIGN )
500 msStyle |= CCS_NOPARENTALIGN;
501
502 if ( style & wxTB_VERTICAL )
503 msStyle |= CCS_VERT;
504
505 if( style & wxTB_BOTTOM )
506 msStyle |= CCS_BOTTOM;
507
508 if ( style & wxTB_RIGHT )
509 msStyle |= CCS_RIGHT;
510
511 return msStyle;
512 }
513
514 // ----------------------------------------------------------------------------
515 // adding/removing tools
516 // ----------------------------------------------------------------------------
517
518 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
519 wxToolBarToolBase * WXUNUSED(tool))
520 {
521 // nothing special to do here - we really create the toolbar buttons in
522 // Realize() later
523 InvalidateBestSize();
524 return true;
525 }
526
527 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
528 {
529 // the main difficulty we have here is with the controls in the toolbars:
530 // as we (sometimes) use several separators to cover up the space used by
531 // them, the indices are not the same for us and the toolbar
532
533 // first determine the position of the first button to delete: it may be
534 // different from pos if we use several separators to cover the space used
535 // by a control
536 wxToolBarToolsList::compatibility_iterator node;
537 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
538 {
539 wxToolBarToolBase *tool2 = node->GetData();
540 if ( tool2 == tool )
541 {
542 // let node point to the next node in the list
543 node = node->GetNext();
544
545 break;
546 }
547
548 if ( tool2->IsControl() )
549 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
550 }
551
552 // now determine the number of buttons to delete and the area taken by them
553 size_t nButtonsToDelete = 1;
554
555 // get the size of the button we're going to delete
556 const RECT r = wxGetTBItemRect(GetHwnd(), pos);
557
558 int width = r.right - r.left;
559
560 if ( tool->IsControl() )
561 {
562 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
563 width *= nButtonsToDelete;
564 }
565
566 // do delete all buttons
567 m_nButtons -= nButtonsToDelete;
568 while ( nButtonsToDelete-- > 0 )
569 {
570 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
571 {
572 wxLogLastError(wxT("TB_DELETEBUTTON"));
573
574 return false;
575 }
576 }
577
578 // and finally reposition all the controls after this button (the toolbar
579 // takes care of all normal items)
580 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
581 {
582 wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData();
583 if ( tool2->IsControl() )
584 {
585 wxControl * const control = tool2->GetControl();
586
587 int x;
588 control->GetPosition(&x, NULL);
589 control->Move(x - width, wxDefaultCoord);
590
591 wxStaticText * const staticText = tool2->GetStaticText();
592 if ( staticText )
593 staticText->Move(x - width, wxDefaultCoord);
594 }
595 }
596
597 InvalidateBestSize();
598
599 return true;
600 }
601
602 void wxToolBar::CreateDisabledImageList()
603 {
604 if (m_disabledImgList != NULL)
605 {
606 delete m_disabledImgList;
607 m_disabledImgList = NULL;
608 }
609
610 // as we can't use disabled image list with older versions of comctl32.dll,
611 // don't even bother creating it
612 if ( wxApp::GetComCtl32Version() >= 470 )
613 {
614 // search for the first disabled button img in the toolbar, if any
615 for ( wxToolBarToolsList::compatibility_iterator
616 node = m_tools.GetFirst(); node; node = node->GetNext() )
617 {
618 wxToolBarToolBase *tool = node->GetData();
619 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
620 if ( bmpDisabled.Ok() )
621 {
622 m_disabledImgList = new wxImageList
623 (
624 m_defaultWidth,
625 m_defaultHeight,
626 bmpDisabled.GetMask() != NULL,
627 GetToolsCount()
628 );
629 break;
630 }
631 }
632
633 // we don't have any disabled bitmaps
634 }
635 }
636
637 bool wxToolBar::Realize()
638 {
639 if ( !wxToolBarBase::Realize() )
640 return false;
641
642 const size_t nTools = GetToolsCount();
643
644 #ifdef wxREMAP_BUTTON_COLOURS
645 // don't change the values of these constants, they can be set from the
646 // user code via wxSystemOptions
647 enum
648 {
649 Remap_None = -1,
650 Remap_Bg,
651 Remap_Buttons,
652 Remap_TransparentBg
653 };
654
655 // the user-specified option overrides anything, but if it wasn't set, only
656 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
657 // much worse after remapping
658 static const wxChar *remapOption = wxT("msw.remap");
659 const int remapValue = wxSystemOptions::HasOption(remapOption)
660 ? wxSystemOptions::GetOptionInt(remapOption)
661 : wxDisplayDepth() <= 8 ? Remap_Buttons
662 : Remap_None;
663
664 #endif // wxREMAP_BUTTON_COLOURS
665
666 // delete all old buttons, if any
667 for ( size_t pos = 0; pos < m_nButtons; pos++ )
668 {
669 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
670 {
671 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
672 }
673 }
674
675 // First, add the bitmap: we use one bitmap for all toolbar buttons
676 // ----------------------------------------------------------------
677
678 wxToolBarToolsList::compatibility_iterator node;
679 int bitmapId = 0;
680
681 if ( !HasFlag(wxTB_NOICONS) )
682 {
683 // if we already have a bitmap, we'll replace the existing one --
684 // otherwise we'll install a new one
685 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
686
687 const wxCoord totalBitmapWidth = m_defaultWidth *
688 wx_truncate_cast(wxCoord, nTools),
689 totalBitmapHeight = m_defaultHeight;
690
691 // Create a bitmap and copy all the tool bitmaps into it
692 wxMemoryDC dcAllButtons;
693 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
694 dcAllButtons.SelectObject(bitmap);
695
696 #ifdef wxREMAP_BUTTON_COLOURS
697 if ( remapValue != Remap_TransparentBg )
698 #endif // wxREMAP_BUTTON_COLOURS
699 {
700 // VZ: why do we hardcode grey colour for CE?
701 dcAllButtons.SetBackground(wxBrush(
702 #ifdef __WXWINCE__
703 wxColour(0xc0, 0xc0, 0xc0)
704 #else // !__WXWINCE__
705 GetBackgroundColour()
706 #endif // __WXWINCE__/!__WXWINCE__
707 ));
708 dcAllButtons.Clear();
709 }
710
711 m_hBitmap = bitmap.GetHBITMAP();
712 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
713
714 #ifdef wxREMAP_BUTTON_COLOURS
715 if ( remapValue == Remap_Bg )
716 {
717 dcAllButtons.SelectObject(wxNullBitmap);
718
719 // Even if we're not remapping the bitmap
720 // content, we still have to remap the background.
721 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
722 totalBitmapWidth, totalBitmapHeight);
723
724 dcAllButtons.SelectObject(bitmap);
725 }
726 #endif // wxREMAP_BUTTON_COLOURS
727
728 // the button position
729 wxCoord x = 0;
730
731 // the number of buttons (not separators)
732 int nButtons = 0;
733
734 CreateDisabledImageList();
735 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
736 {
737 wxToolBarToolBase *tool = node->GetData();
738 if ( tool->IsButton() )
739 {
740 const wxBitmap& bmp = tool->GetNormalBitmap();
741
742 const int w = bmp.GetWidth();
743 const int h = bmp.GetHeight();
744
745 if ( bmp.Ok() )
746 {
747 int xOffset = wxMax(0, (m_defaultWidth - w)/2);
748 int yOffset = wxMax(0, (m_defaultHeight - h)/2);
749
750 // notice the last parameter: do use mask
751 dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true);
752 }
753 else
754 {
755 wxFAIL_MSG( wxT("invalid tool button bitmap") );
756 }
757
758 // also deal with disabled bitmap if we want to use them
759 if ( m_disabledImgList )
760 {
761 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
762 #if wxUSE_IMAGE && wxUSE_WXDIB
763 if ( !bmpDisabled.Ok() )
764 {
765 // no disabled bitmap specified but we still need to
766 // fill the space in the image list with something, so
767 // we grey out the normal bitmap
768 wxImage
769 imgGreyed = bmp.ConvertToImage().ConvertToGreyscale();
770
771 #ifdef wxREMAP_BUTTON_COLOURS
772 if ( remapValue == Remap_Buttons )
773 {
774 // we need to have light grey background colour for
775 // MapBitmap() to work correctly
776 for ( int y = 0; y < h; y++ )
777 {
778 for ( int x = 0; x < w; x++ )
779 {
780 if ( imgGreyed.IsTransparent(x, y) )
781 imgGreyed.SetRGB(x, y,
782 wxLIGHT_GREY->Red(),
783 wxLIGHT_GREY->Green(),
784 wxLIGHT_GREY->Blue());
785 }
786 }
787 }
788 #endif // wxREMAP_BUTTON_COLOURS
789
790 bmpDisabled = wxBitmap(imgGreyed);
791 }
792 #endif // wxUSE_IMAGE
793
794 #ifdef wxREMAP_BUTTON_COLOURS
795 if ( remapValue == Remap_Buttons )
796 MapBitmap(bmpDisabled.GetHBITMAP(), w, h);
797 #endif // wxREMAP_BUTTON_COLOURS
798
799 m_disabledImgList->Add(bmpDisabled);
800 }
801
802 // still inc width and number of buttons because otherwise the
803 // subsequent buttons will all be shifted which is rather confusing
804 // (and like this you'd see immediately which bitmap was bad)
805 x += m_defaultWidth;
806 nButtons++;
807 }
808 }
809
810 dcAllButtons.SelectObject(wxNullBitmap);
811
812 // don't delete this HBITMAP!
813 bitmap.SetHBITMAP(0);
814
815 #ifdef wxREMAP_BUTTON_COLOURS
816 if ( remapValue == Remap_Buttons )
817 {
818 // Map to system colours
819 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
820 totalBitmapWidth, totalBitmapHeight);
821 }
822 #endif // wxREMAP_BUTTON_COLOURS
823
824 bool addBitmap = true;
825
826 if ( oldToolBarBitmap )
827 {
828 #ifdef TB_REPLACEBITMAP
829 if ( wxApp::GetComCtl32Version() >= 400 )
830 {
831 TBREPLACEBITMAP replaceBitmap;
832 replaceBitmap.hInstOld = NULL;
833 replaceBitmap.hInstNew = NULL;
834 replaceBitmap.nIDOld = (UINT_PTR)oldToolBarBitmap;
835 replaceBitmap.nIDNew = (UINT_PTR)hBitmap;
836 replaceBitmap.nButtons = nButtons;
837 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
838 0, (LPARAM) &replaceBitmap) )
839 {
840 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
841 }
842
843 ::DeleteObject(oldToolBarBitmap);
844
845 // already done
846 addBitmap = false;
847 }
848 else
849 #endif // TB_REPLACEBITMAP
850 {
851 // we can't replace the old bitmap, so we will add another one
852 // (awfully inefficient, but what else to do?) and shift the bitmap
853 // indices accordingly
854 addBitmap = true;
855
856 bitmapId = m_nButtons;
857 }
858 }
859
860 if ( addBitmap ) // no old bitmap or we can't replace it
861 {
862 TBADDBITMAP addBitmap;
863 addBitmap.hInst = 0;
864 addBitmap.nID = (UINT_PTR)hBitmap;
865 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
866 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
867 {
868 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
869 }
870 }
871
872 // disable image lists are only supported in comctl32.dll 4.70+
873 if ( wxApp::GetComCtl32Version() >= 470 )
874 {
875 HIMAGELIST hil = m_disabledImgList
876 ? GetHimagelistOf(m_disabledImgList)
877 : 0;
878
879 // notice that we set the image list even if don't have one right
880 // now as we could have it before and need to reset it in this case
881 HIMAGELIST oldImageList = (HIMAGELIST)
882 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil);
883
884 // delete previous image list if any
885 if ( oldImageList )
886 ::DeleteObject(oldImageList);
887 }
888 }
889
890
891 // Next add the buttons and separators
892 // -----------------------------------
893
894 wxScopedArray<TBBUTTON> buttons(new TBBUTTON[nTools]);
895
896 // this array will hold the indices of all controls in the toolbar
897 wxArrayInt controlIds;
898
899 bool lastWasRadio = false;
900 int i = 0;
901 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
902 {
903 wxToolBarToolBase *tool = node->GetData();
904
905 // don't add separators to the vertical toolbar with old comctl32.dll
906 // versions as they didn't handle this properly
907 if ( IsVertical() && tool->IsSeparator() &&
908 wxApp::GetComCtl32Version() <= 472 )
909 {
910 continue;
911 }
912
913 TBBUTTON& button = buttons[i];
914
915 wxZeroMemory(button);
916
917 bool isRadio = false;
918 switch ( tool->GetStyle() )
919 {
920 case wxTOOL_STYLE_CONTROL:
921 button.idCommand = tool->GetId();
922 // fall through: create just a separator too
923
924 case wxTOOL_STYLE_SEPARATOR:
925 button.fsState = TBSTATE_ENABLED;
926 button.fsStyle = TBSTYLE_SEP;
927 break;
928
929 case wxTOOL_STYLE_BUTTON:
930 if ( !HasFlag(wxTB_NOICONS) )
931 button.iBitmap = bitmapId;
932
933 if ( HasFlag(wxTB_TEXT) )
934 {
935 const wxString& label = tool->GetLabel();
936 if ( !label.empty() )
937 button.iString = (INT_PTR)label.wx_str();
938 }
939
940 button.idCommand = tool->GetId();
941
942 if ( tool->IsEnabled() )
943 button.fsState |= TBSTATE_ENABLED;
944 if ( tool->IsToggled() )
945 button.fsState |= TBSTATE_CHECKED;
946
947 switch ( tool->GetKind() )
948 {
949 case wxITEM_RADIO:
950 button.fsStyle = TBSTYLE_CHECKGROUP;
951
952 if ( !lastWasRadio )
953 {
954 // the first item in the radio group is checked by
955 // default to be consistent with wxGTK and the menu
956 // radio items
957 button.fsState |= TBSTATE_CHECKED;
958
959 if (tool->Toggle(true))
960 {
961 DoToggleTool(tool, true);
962 }
963 }
964 else if ( tool->IsToggled() )
965 {
966 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
967 int prevIndex = i - 1;
968 while ( nodePrev )
969 {
970 TBBUTTON& prevButton = buttons[prevIndex];
971 wxToolBarToolBase *tool = nodePrev->GetData();
972 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
973 break;
974
975 if ( tool->Toggle(false) )
976 DoToggleTool(tool, false);
977
978 prevButton.fsState &= ~TBSTATE_CHECKED;
979 nodePrev = nodePrev->GetPrevious();
980 prevIndex--;
981 }
982 }
983
984 isRadio = true;
985 break;
986
987 case wxITEM_CHECK:
988 button.fsStyle = TBSTYLE_CHECK;
989 break;
990
991 case wxITEM_NORMAL:
992 button.fsStyle = TBSTYLE_BUTTON;
993 break;
994
995 case wxITEM_DROPDOWN:
996 button.fsStyle = TBSTYLE_DROPDOWN;
997 break;
998
999 default:
1000 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
1001 button.fsStyle = TBSTYLE_BUTTON;
1002 break;
1003 }
1004
1005 bitmapId++;
1006 break;
1007 }
1008
1009 lastWasRadio = isRadio;
1010
1011 i++;
1012 }
1013
1014 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, i, (LPARAM)buttons.get()) )
1015 {
1016 wxLogLastError(wxT("TB_ADDBUTTONS"));
1017 }
1018
1019
1020 // Deal with the controls finally
1021 // ------------------------------
1022
1023 // adjust the controls size to fit nicely in the toolbar
1024 int y = 0;
1025 size_t index = 0;
1026 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
1027 {
1028 wxToolBarTool *tool = (wxToolBarTool*)node->GetData();
1029
1030 // we calculate the running y coord for vertical toolbars so we need to
1031 // get the items size for all items but for the horizontal ones we
1032 // don't need to deal with the non controls
1033 bool isControl = tool->IsControl();
1034 if ( !isControl && !IsVertical() )
1035 continue;
1036
1037 const RECT r = wxGetTBItemRect(GetHwnd(), index);
1038 if ( !isControl )
1039 {
1040 // can only be control if isVertical
1041 y += r.bottom - r.top;
1042
1043 continue;
1044 }
1045
1046 wxControl *control = tool->GetControl();
1047 wxStaticText * const staticText = tool->GetStaticText();
1048
1049 wxSize size = control->GetSize();
1050 wxSize staticTextSize;
1051 if ( staticText )
1052 {
1053 staticTextSize = staticText->GetSize();
1054 staticTextSize.y += 3; // margin between control and its label
1055 }
1056
1057 // the position of the leftmost controls corner
1058 int left = wxDefaultCoord;
1059
1060 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1061 #ifdef TB_SETBUTTONINFO
1062 // available in headers, now check whether it is available now
1063 // (during run-time)
1064 if ( wxApp::GetComCtl32Version() >= 471 )
1065 {
1066 // set the (underlying) separators width to be that of the
1067 // control
1068 TBBUTTONINFO tbbi;
1069 tbbi.cbSize = sizeof(tbbi);
1070 tbbi.dwMask = TBIF_SIZE;
1071 tbbi.cx = (WORD)size.x;
1072 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1073 tool->GetId(), (LPARAM)&tbbi) )
1074 {
1075 // the id is probably invalid?
1076 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1077 }
1078 }
1079 else
1080 #endif // comctl32.dll 4.71
1081 // TB_SETBUTTONINFO unavailable
1082 {
1083 // try adding several separators to fit the controls width
1084 int widthSep = r.right - r.left;
1085 left = r.left;
1086
1087 TBBUTTON tbb;
1088 wxZeroMemory(tbb);
1089 tbb.idCommand = 0;
1090 tbb.fsState = TBSTATE_ENABLED;
1091 tbb.fsStyle = TBSTYLE_SEP;
1092
1093 size_t nSeparators = size.x / widthSep;
1094 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
1095 {
1096 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
1097 index, (LPARAM)&tbb) )
1098 {
1099 wxLogLastError(wxT("TB_INSERTBUTTON"));
1100 }
1101
1102 index++;
1103 }
1104
1105 // remember the number of separators we used - we'd have to
1106 // delete all of them later
1107 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
1108
1109 // adjust the controls width to exactly cover the separators
1110 size.x = (nSeparators + 1)*widthSep;
1111 control->SetSize(size.x, wxDefaultCoord);
1112 }
1113
1114 // position the control itself correctly vertically centering it on the
1115 // icon area of the toolbar
1116 int height = r.bottom - r.top - staticTextSize.y;
1117
1118 int diff = height - size.y;
1119 if ( diff < 0 || !HasFlag(wxTB_TEXT) )
1120 {
1121 // not enough room for the static text
1122 if ( staticText )
1123 staticText->Hide();
1124
1125 // recalculate height & diff without the staticText control
1126 height = r.bottom - r.top;
1127 diff = height - size.y;
1128 if ( diff < 0 )
1129 {
1130 // the control is too high, resize to fit
1131 control->SetSize(wxDefaultCoord, height - 2);
1132
1133 diff = 2;
1134 }
1135 }
1136 else // enough space for both the control and the label
1137 {
1138 if ( staticText )
1139 staticText->Show();
1140 }
1141
1142 int top;
1143 if ( IsVertical() )
1144 {
1145 left = 0;
1146 top = y;
1147
1148 y += height + 2 * GetMargins().y;
1149 }
1150 else // horizontal toolbar
1151 {
1152 if ( left == wxDefaultCoord )
1153 left = r.left;
1154
1155 top = r.top;
1156 }
1157
1158 control->Move(left, top + (diff + 1) / 2);
1159 if ( staticText )
1160 {
1161 staticText->Move(left + (size.x - staticTextSize.x)/2,
1162 r.bottom - staticTextSize.y);
1163 }
1164 }
1165
1166 // the max index is the "real" number of buttons - i.e. counting even the
1167 // separators which we added just for aligning the controls
1168 m_nButtons = index;
1169
1170 if ( !IsVertical() )
1171 {
1172 if ( m_maxRows == 0 )
1173 // if not set yet, only one row
1174 SetRows(1);
1175 }
1176 else if ( m_nButtons > 0 ) // vertical non empty toolbar
1177 {
1178 // if not set yet, have one column
1179 m_maxRows = 1;
1180 SetRows(m_nButtons);
1181 }
1182
1183 InvalidateBestSize();
1184 UpdateSize();
1185
1186 return true;
1187 }
1188
1189 // ----------------------------------------------------------------------------
1190 // message handlers
1191 // ----------------------------------------------------------------------------
1192
1193 bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_)
1194 {
1195 // cast to signed is important as we compare this id with (signed) ints in
1196 // FindById() and without the cast we'd get a positive int from a
1197 // "negative" (i.e. > 32767) WORD
1198 const int id = (signed short)id_;
1199
1200 wxToolBarToolBase *tool = FindById(id);
1201 if ( !tool )
1202 return false;
1203
1204 bool toggled = false; // just to suppress warnings
1205
1206 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
1207
1208 if ( tool->CanBeToggled() )
1209 {
1210 toggled = (state & TBSTATE_CHECKED) != 0;
1211
1212 // ignore the event when a radio button is released, as this doesn't
1213 // seem to happen at all, and is handled otherwise
1214 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
1215 return true;
1216
1217 tool->Toggle(toggled);
1218 UnToggleRadioGroup(tool);
1219 }
1220
1221 // Without the two lines of code below, if the toolbar was repainted during
1222 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1223 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1224 // The Update() call bellow ensures that this won't happen, by repainting
1225 // invalidated areas of the toolbar immediately.
1226 //
1227 // To complicate matters, the tool would be drawn in depressed state (this
1228 // code is called when mouse button is released, not pressed). That's not
1229 // ideal, having the tool pressed for the duration of OnLeftClick()
1230 // provides the user with useful visual clue that the app is busy reacting
1231 // to the event. So we manually put the tool into pressed state, handle the
1232 // event and then finally restore tool's original state.
1233 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0));
1234 Update();
1235
1236 bool allowLeftClick = OnLeftClick(id, toggled);
1237
1238 // Restore the unpressed state. Enabled/toggled state might have been
1239 // changed since so take care of it.
1240 if (tool->IsEnabled())
1241 state |= TBSTATE_ENABLED;
1242 else
1243 state &= ~TBSTATE_ENABLED;
1244 if (tool->IsToggled())
1245 state |= TBSTATE_CHECKED;
1246 else
1247 state &= ~TBSTATE_CHECKED;
1248 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0));
1249
1250 // OnLeftClick() can veto the button state change - for buttons which
1251 // may be toggled only, of couse
1252 if ( !allowLeftClick && tool->CanBeToggled() )
1253 {
1254 // revert back
1255 tool->Toggle(!toggled);
1256
1257 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1258 }
1259
1260 return true;
1261 }
1262
1263 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
1264 WXLPARAM lParam,
1265 WXLPARAM *WXUNUSED(result))
1266 {
1267 LPNMHDR hdr = (LPNMHDR)lParam;
1268 if ( hdr->code == TBN_DROPDOWN )
1269 {
1270 LPNMTOOLBAR tbhdr = (LPNMTOOLBAR)lParam;
1271
1272 wxCommandEvent evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, tbhdr->iItem);
1273 if ( HandleWindowEvent(evt) )
1274 {
1275 // Event got handled, don't display default popup menu
1276 return false;
1277 }
1278
1279 const wxToolBarToolBase * const tool = FindById(tbhdr->iItem);
1280 wxCHECK_MSG( tool, false, wxT("drop down message for unknown tool") );
1281
1282 wxMenu * const menu = tool->GetDropdownMenu();
1283 if ( !menu )
1284 return false;
1285
1286 // Display popup menu below button
1287 const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem));
1288 if ( r.right )
1289 PopupMenu(menu, r.left, r.bottom);
1290
1291 return true;
1292 }
1293
1294
1295 if( !HasFlag(wxTB_NO_TOOLTIPS) )
1296 {
1297 #if wxUSE_TOOLTIPS
1298 // First check if this applies to us
1299
1300 // the tooltips control created by the toolbar is sometimes Unicode, even
1301 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1302 UINT code = hdr->code;
1303 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1304 return false;
1305
1306 HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0);
1307 if ( toolTipWnd != hdr->hwndFrom )
1308 return false;
1309
1310 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1311 int id = (int)ttText->hdr.idFrom;
1312
1313 wxToolBarToolBase *tool = FindById(id);
1314 if ( tool )
1315 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
1316 #else
1317 wxUnusedVar(lParam);
1318 #endif
1319 }
1320
1321 return false;
1322 }
1323
1324 // ----------------------------------------------------------------------------
1325 // toolbar geometry
1326 // ----------------------------------------------------------------------------
1327
1328 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1329 {
1330 wxToolBarBase::SetToolBitmapSize(size);
1331
1332 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
1333 }
1334
1335 void wxToolBar::SetRows(int nRows)
1336 {
1337 if ( nRows == m_maxRows )
1338 {
1339 // avoid resizing the frame uselessly
1340 return;
1341 }
1342
1343 // TRUE in wParam means to create at least as many rows, FALSE -
1344 // at most as many
1345 RECT rect;
1346 ::SendMessage(GetHwnd(), TB_SETROWS,
1347 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1348 (LPARAM) &rect);
1349
1350 m_maxRows = nRows;
1351
1352 UpdateSize();
1353 }
1354
1355 // The button size is bigger than the bitmap size
1356 wxSize wxToolBar::GetToolSize() const
1357 {
1358 // TB_GETBUTTONSIZE is supported from version 4.70
1359 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1360 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1361 && !defined (__DIGITALMARS__)
1362 if ( wxApp::GetComCtl32Version() >= 470 )
1363 {
1364 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1365
1366 return wxSize(LOWORD(dw), HIWORD(dw));
1367 }
1368 else
1369 #endif // comctl32.dll 4.70+
1370 {
1371 // defaults
1372 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1373 }
1374 }
1375
1376 static
1377 wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1378 size_t index )
1379 {
1380 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
1381
1382 for ( ; current ; current = current->GetNext() )
1383 {
1384 if ( index == 0 )
1385 return current->GetData();
1386
1387 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1388 size_t separators = tool->GetSeparatorsCount();
1389
1390 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1391 // otherwise, skip as many items as the separator count, plus the
1392 // control itself
1393 index -= separators ? separators + 1 : 1;
1394 }
1395
1396 return 0;
1397 }
1398
1399 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1400 {
1401 POINT pt;
1402 pt.x = x;
1403 pt.y = y;
1404 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
1405
1406 // MBN: when the point ( x, y ) is close to the toolbar border
1407 // TB_HITTEST returns m_nButtons ( not -1 )
1408 if ( index < 0 || (size_t)index >= m_nButtons )
1409 // it's a separator or there is no tool at all there
1410 return NULL;
1411
1412 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1413 // we don't use the dummy separators hack
1414 #ifdef TB_SETBUTTONINFO
1415 if ( wxApp::GetComCtl32Version() >= 471 )
1416 {
1417 return m_tools.Item((size_t)index)->GetData();
1418 }
1419 else
1420 #endif // TB_SETBUTTONINFO
1421 {
1422 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1423 }
1424 }
1425
1426 void wxToolBar::UpdateSize()
1427 {
1428 wxPoint pos = GetPosition();
1429 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
1430 if (pos != GetPosition())
1431 Move(pos);
1432
1433 // In case Realize is called after the initial display (IOW the programmer
1434 // may have rebuilt the toolbar) give the frame the option of resizing the
1435 // toolbar to full width again, but only if the parent is a frame and the
1436 // toolbar is managed by the frame. Otherwise assume that some other
1437 // layout mechanism is controlling the toolbar size and leave it alone.
1438 SendSizeEventToParent();
1439 }
1440
1441 // ----------------------------------------------------------------------------
1442 // toolbar styles
1443 // ---------------------------------------------------------------------------
1444
1445 // get the TBSTYLE of the given toolbar window
1446 long wxToolBar::GetMSWToolbarStyle() const
1447 {
1448 return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L);
1449 }
1450
1451 void wxToolBar::SetWindowStyleFlag(long style)
1452 {
1453 // the style bits whose changes force us to recreate the toolbar
1454 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
1455
1456 const long styleOld = GetWindowStyle();
1457
1458 wxToolBarBase::SetWindowStyleFlag(style);
1459
1460 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1461 // also fatal as we'd then try to recreate the toolbar when it's just being
1462 // created
1463 if ( GetToolsCount() &&
1464 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1465 {
1466 // to remove the text labels, simply re-realizing the toolbar is enough
1467 // but I don't know of any way to add the text to an existing toolbar
1468 // other than by recreating it entirely
1469 Recreate();
1470 }
1471 }
1472
1473 // ----------------------------------------------------------------------------
1474 // tool state
1475 // ----------------------------------------------------------------------------
1476
1477 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
1478 {
1479 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1480 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
1481 }
1482
1483 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
1484 {
1485 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1486 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
1487 }
1488
1489 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1490 {
1491 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1492 // without, so we really need to delete the button and recreate it here
1493 wxFAIL_MSG( wxT("not implemented") );
1494 }
1495
1496 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1497 {
1498 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1499 if ( tool )
1500 {
1501 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1502
1503 tool->SetNormalBitmap(bitmap);
1504 Realize();
1505 }
1506 }
1507
1508 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1509 {
1510 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1511 if ( tool )
1512 {
1513 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1514
1515 tool->SetDisabledBitmap(bitmap);
1516 Realize();
1517 }
1518 }
1519
1520 // ----------------------------------------------------------------------------
1521 // event handlers
1522 // ----------------------------------------------------------------------------
1523
1524 // Responds to colour changes, and passes event on to children.
1525 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
1526 {
1527 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
1528
1529 // Remap the buttons
1530 Realize();
1531
1532 // Relayout the toolbar
1533 int nrows = m_maxRows;
1534 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1535 SetRows(nrows);
1536
1537 Refresh();
1538
1539 // let the event propagate further
1540 event.Skip();
1541 }
1542
1543 void wxToolBar::OnMouseEvent(wxMouseEvent& event)
1544 {
1545 if ( event.Leaving() )
1546 {
1547 if ( m_pInTool )
1548 {
1549 OnMouseEnter(wxID_ANY);
1550 m_pInTool = NULL;
1551 }
1552
1553 event.Skip();
1554 return;
1555 }
1556
1557 if ( event.RightDown() )
1558 {
1559 // find the tool under the mouse
1560 wxCoord x = 0, y = 0;
1561 event.GetPosition(&x, &y);
1562
1563 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1564 OnRightClick(tool ? tool->GetId() : -1, x, y);
1565 }
1566 else
1567 {
1568 event.Skip();
1569 }
1570 }
1571
1572 // This handler is required to allow the toolbar to be set to a non-default
1573 // colour: for example, when it must blend in with a notebook page.
1574 void wxToolBar::OnEraseBackground(wxEraseEvent& event)
1575 {
1576 RECT rect = wxGetClientRect(GetHwnd());
1577
1578 wxDC *dc = event.GetDC();
1579 if (!dc) return;
1580 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc->GetImpl();
1581 HDC hdc = GetHdcOf(*impl);
1582
1583 int majorVersion, minorVersion;
1584 wxGetOsVersion(& majorVersion, & minorVersion);
1585
1586 #if wxUSE_UXTHEME
1587 // we may need to draw themed colour so that we appear correctly on
1588 // e.g. notebook page under XP with themes but only do it if the parent
1589 // draws themed background itself
1590 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1591 {
1592 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1593 if ( theme )
1594 {
1595 HRESULT
1596 hr = theme->DrawThemeParentBackground(GetHwnd(), hdc, &rect);
1597 if ( hr == S_OK )
1598 return;
1599
1600 // it can also return S_FALSE which seems to simply say that it
1601 // didn't draw anything but no error really occurred
1602 if ( FAILED(hr) )
1603 {
1604 wxLogApiError(wxT("DrawThemeParentBackground(toolbar)"), hr);
1605 }
1606 }
1607 }
1608
1609 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
1610 if ( !UseBgCol() && majorVersion >= 6 )
1611 {
1612 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1613 if ( theme )
1614 {
1615 wxUxThemeHandle hTheme(this, L"REBAR");
1616
1617 RECT r;
1618 wxRect rect = GetClientRect();
1619 wxCopyRectToRECT(rect, r);
1620
1621 HRESULT hr = theme->DrawThemeBackground(hTheme, hdc, 0, 0, & r, NULL);
1622 if ( hr == S_OK )
1623 return;
1624
1625 // it can also return S_FALSE which seems to simply say that it
1626 // didn't draw anything but no error really occurred
1627 if ( FAILED(hr) )
1628 {
1629 wxLogApiError(wxT("DrawThemeParentBackground(toolbar)"), hr);
1630 }
1631 }
1632 }
1633
1634 #endif // wxUSE_UXTHEME
1635
1636 // we need to always draw our background under XP, as otherwise it doesn't
1637 // appear correctly with some themes (e.g. Zune one)
1638 if ( majorVersion == 5 ||
1639 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT) )
1640 {
1641 // do draw our background
1642 //
1643 // notice that this 'dumb' implementation may cause flicker for some of
1644 // the controls in which case they should intercept wxEraseEvent and
1645 // process it themselves somehow
1646 AutoHBRUSH hBrush(wxColourToRGB(GetBackgroundColour()));
1647
1648 wxCHANGE_HDC_MAP_MODE(hdc, MM_TEXT);
1649 ::FillRect(hdc, &rect, hBrush);
1650 }
1651 else // we have no non default background colour
1652 {
1653 // let the system do it for us
1654 event.Skip();
1655 }
1656 }
1657
1658 bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1659 {
1660 // wait until we have some tools
1661 if ( !GetToolsCount() )
1662 return false;
1663
1664 // calculate our minor dimension ourselves - we're confusing the standard
1665 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1666 const RECT r = wxGetTBItemRect(GetHwnd(), 0);
1667 if ( !r.right )
1668 return false;
1669
1670 int w, h;
1671
1672 if ( IsVertical() )
1673 {
1674 w = r.right - r.left;
1675 if ( m_maxRows )
1676 {
1677 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
1678 }
1679 h = HIWORD(lParam);
1680 }
1681 else
1682 {
1683 w = LOWORD(lParam);
1684 if (HasFlag( wxTB_FLAT ))
1685 h = r.bottom - r.top - 3;
1686 else
1687 h = r.bottom - r.top;
1688 if ( m_maxRows )
1689 {
1690 // FIXME: hardcoded separator line height...
1691 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1692 h *= m_maxRows;
1693 }
1694 }
1695
1696 if ( MAKELPARAM(w, h) != lParam )
1697 {
1698 // size really changed
1699 SetSize(w, h);
1700 }
1701
1702 // message processed
1703 return true;
1704 }
1705
1706 #ifndef __WXWINCE__
1707 bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1708 {
1709 // erase any dummy separators which were used
1710 // for aligning the controls if any here
1711
1712 // first of all, are there any controls at all?
1713 wxToolBarToolsList::compatibility_iterator node;
1714 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1715 {
1716 if ( node->GetData()->IsControl() )
1717 break;
1718 }
1719
1720 if ( !node )
1721 // no controls, nothing to erase
1722 return false;
1723
1724 wxSize clientSize = GetClientSize();
1725 int majorVersion, minorVersion;
1726 wxGetOsVersion(& majorVersion, & minorVersion);
1727
1728 // prepare the DC on which we'll be drawing
1729 wxClientDC dc(this);
1730 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
1731 dc.SetPen(*wxTRANSPARENT_PEN);
1732
1733 RECT r;
1734 if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) )
1735 // nothing to redraw anyhow
1736 return false;
1737
1738 wxRect rectUpdate;
1739 wxCopyRECTToRect(r, rectUpdate);
1740
1741 dc.SetClippingRegion(rectUpdate);
1742
1743 // draw the toolbar tools, separators &c normally
1744 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1745
1746 // for each control in the toolbar find all the separators intersecting it
1747 // and erase them
1748 //
1749 // NB: this is really the only way to do it as we don't know if a separator
1750 // corresponds to a control (i.e. is a dummy one) or a real one
1751 // otherwise
1752 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1753 {
1754 wxToolBarTool *tool = (wxToolBarTool*)node->GetData();
1755 if ( tool->IsControl() )
1756 {
1757 // get the control rect in our client coords
1758 wxControl *control = tool->GetControl();
1759 wxStaticText *staticText = tool->GetStaticText();
1760 wxRect rectCtrl = control->GetRect();
1761 wxRect rectStaticText(0,0,0,0);
1762 if ( staticText )
1763 {
1764 rectStaticText = staticText->GetRect();
1765 }
1766
1767 // iterate over all buttons
1768 TBBUTTON tbb;
1769 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1770 for ( int n = 0; n < count; n++ )
1771 {
1772 // is it a separator?
1773 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1774 n, (LPARAM)&tbb) )
1775 {
1776 wxLogDebug(wxT("TB_GETBUTTON failed?"));
1777
1778 continue;
1779 }
1780
1781 if ( tbb.fsStyle != TBSTYLE_SEP )
1782 continue;
1783
1784 // get the bounding rect of the separator
1785 RECT r = wxGetTBItemRect(GetHwnd(), n);
1786 if ( !r.right )
1787 continue;
1788
1789 // does it intersect the control?
1790 wxRect rectItem;
1791 wxCopyRECTToRect(r, rectItem);
1792 if ( rectCtrl.Intersects(rectItem) || (staticText && rectStaticText.Intersects(rectItem)))
1793 {
1794 // yes, do erase it!
1795
1796 bool haveRefreshed = false;
1797
1798 #if wxUSE_UXTHEME
1799 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1800 {
1801 // Don't use DrawThemeBackground
1802 }
1803 else if ( !UseBgCol() && majorVersion >= 6 )
1804 {
1805 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1806 if ( theme )
1807 {
1808 wxUxThemeHandle hTheme(this, L"REBAR");
1809
1810 RECT clipRect = r;
1811
1812 // Draw the whole background since the pattern may be position sensitive;
1813 // but clip it to the area of interest.
1814 r.left = 0;
1815 r.right = clientSize.x;
1816 r.top = 0;
1817 r.bottom = clientSize.y;
1818
1819 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
1820 HRESULT hr = theme->DrawThemeBackground(hTheme, GetHdcOf(*impl), 0, 0, & r, & clipRect);
1821 if ( hr == S_OK )
1822 haveRefreshed = true;
1823 }
1824 }
1825 #endif // wxUSE_UXTHEME
1826
1827 if (!haveRefreshed)
1828 dc.DrawRectangle(rectItem);
1829 }
1830
1831 if ( rectCtrl.Intersects(rectItem) )
1832 {
1833 // Necessary in case we use a no-paint-on-size
1834 // style in the parent: the controls can disappear
1835 control->Refresh(false);
1836 }
1837
1838 if ( staticText && rectStaticText.Intersects(rectItem) )
1839 {
1840 // Necessary in case we use a no-paint-on-size
1841 // style in the parent: the controls can disappear
1842 staticText->Refresh(false);
1843 }
1844 }
1845 }
1846 }
1847
1848 return true;
1849 }
1850 #endif // __WXWINCE__
1851
1852 void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1853 {
1854 wxCoord x = GET_X_LPARAM(lParam),
1855 y = GET_Y_LPARAM(lParam);
1856 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1857
1858 // has the current tool changed?
1859 if ( tool != m_pInTool )
1860 {
1861 m_pInTool = tool;
1862 OnMouseEnter(tool ? tool->GetId() : wxID_ANY);
1863 }
1864 }
1865
1866 WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1867 {
1868 switch ( nMsg )
1869 {
1870 case WM_MOUSEMOVE:
1871 // we don't handle mouse moves, so always pass the message to
1872 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1873 HandleMouseMove(wParam, lParam);
1874 break;
1875
1876 case WM_SIZE:
1877 if ( HandleSize(wParam, lParam) )
1878 return 0;
1879 break;
1880
1881 #ifndef __WXWINCE__
1882 case WM_PAINT:
1883 // refreshing the controls in the toolbar inside a composite window
1884 // results in an endless stream of WM_PAINT messages -- and seems
1885 // to be unnecessary anyhow as everything works just fine without
1886 // any special workarounds in this case
1887 if ( !IsDoubleBuffered() && HandlePaint(wParam, lParam) )
1888 return 0;
1889 break;
1890 #endif // __WXWINCE__
1891 }
1892
1893 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
1894 }
1895
1896 // ----------------------------------------------------------------------------
1897 // private functions
1898 // ----------------------------------------------------------------------------
1899
1900 #ifdef wxREMAP_BUTTON_COLOURS
1901
1902 WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
1903 {
1904 MemoryHDC hdcMem;
1905
1906 if ( !hdcMem )
1907 {
1908 wxLogLastError(wxT("CreateCompatibleDC"));
1909
1910 return bitmap;
1911 }
1912
1913 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
1914
1915 if ( !bmpInHDC )
1916 {
1917 wxLogLastError(wxT("SelectObject"));
1918
1919 return bitmap;
1920 }
1921
1922 wxCOLORMAP *cmap = wxGetStdColourMap();
1923
1924 for ( int i = 0; i < width; i++ )
1925 {
1926 for ( int j = 0; j < height; j++ )
1927 {
1928 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1929
1930 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
1931 {
1932 COLORREF col = cmap[k].from;
1933 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1934 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1935 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1936 {
1937 if ( cmap[k].to != pixel )
1938 ::SetPixel(hdcMem, i, j, cmap[k].to);
1939 break;
1940 }
1941 }
1942 }
1943 }
1944
1945 return bitmap;
1946 }
1947
1948 #endif // wxREMAP_BUTTON_COLOURS
1949
1950 #endif // wxUSE_TOOLBAR