1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/toolbar.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
29 #include "wx/toolbar.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/dynarray.h"
37 #include "wx/settings.h"
38 #include "wx/bitmap.h"
39 #include "wx/region.h"
40 #include "wx/dcmemory.h"
41 #include "wx/control.h"
42 #include "wx/app.h" // for GetComCtl32Version
44 #include "wx/stattext.h"
47 #include "wx/artprov.h"
48 #include "wx/sysopt.h"
49 #include "wx/dcclient.h"
50 #include "wx/scopedarray.h"
52 #include "wx/msw/private.h"
53 #include "wx/msw/dc.h"
56 #include "wx/msw/uxtheme.h"
59 // this define controls whether the code for button colours remapping (only
60 // useful for 16 or 256 colour images) is active at all, it's always turned off
61 // for CE where it doesn't compile (and is probably not needed anyhow) and may
62 // also be turned off for other systems if you always use 24bpp images and so
65 #define wxREMAP_BUTTON_COLOURS
66 #endif // !__WXWINCE__
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // these standard constants are not always defined in compilers headers
76 #define TBSTYLE_LIST 0x1000
77 #define TBSTYLE_FLAT 0x0800
80 #ifndef TBSTYLE_TRANSPARENT
81 #define TBSTYLE_TRANSPARENT 0x8000
84 #ifndef TBSTYLE_TOOLTIPS
85 #define TBSTYLE_TOOLTIPS 0x0100
90 #define TB_SETSTYLE (WM_USER + 56)
91 #define TB_GETSTYLE (WM_USER + 57)
95 #define TB_HITTEST (WM_USER + 69)
99 #define TB_GETMAXSIZE (WM_USER + 83)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
118 style ( wxNO_BORDER | wxTB_HORIZONTAL)
127 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
128 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
129 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
130 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 class wxToolBarTool
: public wxToolBarToolBase
140 wxToolBarTool(wxToolBar
*tbar
,
142 const wxString
& label
,
143 const wxBitmap
& bmpNormal
,
144 const wxBitmap
& bmpDisabled
,
146 wxObject
*clientData
,
147 const wxString
& shortHelp
,
148 const wxString
& longHelp
)
149 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
150 clientData
, shortHelp
, longHelp
)
156 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
157 : wxToolBarToolBase(tbar
, control
, label
)
159 if ( IsControl() && !m_label
.empty() )
161 // create a control to render the control's label
162 m_staticText
= new wxStaticText
169 wxALIGN_CENTRE
| wxST_NO_AUTORESIZE
180 virtual ~wxToolBarTool()
185 virtual void SetLabel(const wxString
& label
)
187 if ( label
== m_label
)
190 wxToolBarToolBase::SetLabel(label
);
193 m_staticText
->SetLabel(label
);
195 // we need to update the label shown in the toolbar because it has a
196 // pointer to the internal buffer of the old label
198 // TODO: use TB_SETBUTTONINFO
201 wxStaticText
* GetStaticText()
203 wxASSERT_MSG( IsControl(),
204 wxT("only makes sense for embedded control tools") );
209 // set/get the number of separators which we use to cover the space used by
210 // a control in the toolbar
211 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
212 size_t GetSeparatorsCount() const { return m_nSepCount
; }
214 // we need ids for the spacers which we want to modify later on, this
215 // function will allocate a valid/unique id for a spacer if not done yet
218 if ( m_id
== wxID_SEPARATOR
)
219 m_id
= wxWindow::NewControlId();
222 // this method is used for controls only and offsets the control by the
223 // given amount (in pixels) in horizontal direction
224 void MoveBy(int offset
)
226 wxControl
* const control
= GetControl();
228 control
->Move(control
->GetPosition().x
+ offset
, wxDefaultCoord
);
232 m_staticText
->Move(m_staticText
->GetPosition().x
+ offset
,
239 wxStaticText
*m_staticText
;
241 wxDECLARE_NO_COPY_CLASS(wxToolBarTool
);
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 // return the rectangle of the item at the given index
250 // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
251 // r.right or r.bottom with 0 to check for this
252 static RECT
wxGetTBItemRect(HWND hwnd
, int index
)
256 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
257 // only appeared in v4.70 of comctl32.dll
258 if ( !::SendMessage(hwnd
, TB_GETITEMRECT
, index
, (LPARAM
)&r
) )
260 wxLogLastError(wxT("TB_GETITEMRECT"));
271 // ============================================================================
273 // ============================================================================
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
280 const wxString
& label
,
281 const wxBitmap
& bmpNormal
,
282 const wxBitmap
& bmpDisabled
,
284 wxObject
*clientData
,
285 const wxString
& shortHelp
,
286 const wxString
& longHelp
)
288 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
289 clientData
, shortHelp
, longHelp
);
293 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
295 return new wxToolBarTool(this, control
, label
);
298 // ----------------------------------------------------------------------------
299 // wxToolBar construction
300 // ----------------------------------------------------------------------------
302 void wxToolBar::Init()
305 m_disabledImgList
= NULL
;
308 m_totalFixedSize
= 0;
310 // even though modern Windows applications typically use 24*24 (or even
311 // 32*32) size for their bitmaps, the native control itself still uses the
312 // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so
313 // default to it so that we don't call SetToolBitmapSize() unnecessarily in
314 // wxToolBarBase::AdjustToolBitmapSize()
316 m_defaultHeight
= 15;
321 bool wxToolBar::Create(wxWindow
*parent
,
326 const wxString
& name
)
328 // common initialisation
329 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
334 // MSW-specific initialisation
335 if ( !MSWCreateToolbar(pos
, size
) )
338 wxSetCCUnicodeFormat(GetHwnd());
340 // we always erase our background on WM_PAINT so there is no need to do it
341 // in WM_ERASEBKGND too (by default this won't be done but if the toolbar
342 // has a non default background colour, then it would be used in both
343 // places resulting in flicker)
344 if (wxApp::GetComCtl32Version() >= 600)
346 SetBackgroundStyle(wxBG_STYLE_PAINT
);
352 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
354 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
357 // toolbar-specific post initialisation
358 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
360 #ifdef TB_SETEXTENDEDSTYLE
361 if ( wxApp::GetComCtl32Version() >= 471 )
362 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE
, 0, TBSTYLE_EX_DRAWDDARROWS
);
368 void wxToolBar::Recreate()
370 const HWND hwndOld
= GetHwnd();
373 // we haven't been created yet, no need to recreate
377 // get the position and size before unsubclassing the old toolbar
378 const wxPoint pos
= GetPosition();
379 const wxSize size
= GetSize();
383 if ( !MSWCreateToolbar(pos
, size
) )
386 wxFAIL_MSG( wxT("recreating the toolbar failed") );
391 // reparent all our children under the new toolbar
392 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
394 node
= node
->GetNext() )
396 wxWindow
*win
= node
->GetData();
397 if ( !win
->IsTopLevel() )
398 ::SetParent(GetHwndOf(win
), GetHwnd());
401 // only destroy the old toolbar now --
402 // after all the children had been reparented
403 ::DestroyWindow(hwndOld
);
405 // it is for the old bitmap control and can't be used with the new one
408 ::DeleteObject((HBITMAP
) m_hBitmap
);
412 wxDELETE(m_disabledImgList
);
417 wxToolBar::~wxToolBar()
419 // we must refresh the frame size when the toolbar is deleted but the frame
420 // is not - otherwise toolbar leaves a hole in the place it used to occupy
421 SendSizeEventToParent();
424 ::DeleteObject((HBITMAP
) m_hBitmap
);
426 delete m_disabledImgList
;
429 wxSize
wxToolBar::DoGetBestSize() const
434 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
436 // maybe an old (< 0x400) Windows version? try to approximate the
437 // toolbar size ourselves
438 sizeBest
= GetToolSize();
439 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
440 sizeBest
.x
*= GetToolsCount();
442 // reverse horz and vertical components if necessary
446 sizeBest
.x
= sizeBest
.y
;
450 else // TB_GETMAXSIZE succeeded
452 // but it could still return an incorrect result due to what appears to
453 // be a bug in old comctl32.dll versions which don't handle controls in
454 // the toolbar correctly, so work around it (see SF patch 1902358)
455 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
457 // calculate the toolbar width in alternative way
458 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
459 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
461 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
462 if ( widthAlt
> size
.cx
)
466 sizeBest
.x
= size
.cx
;
467 sizeBest
.y
= size
.cy
;
472 // Without the extra height, DoGetBestSize can report a size that's
473 // smaller than the actual window, causing windows to overlap slightly
474 // in some circumstances, leading to missing borders (especially noticeable
476 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
481 CacheBestSize(sizeBest
);
486 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
488 // toolbars never have border, giving one to them results in broken
490 WXDWORD msStyle
= wxControl::MSWGetStyle
492 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
495 if ( !(style
& wxTB_NO_TOOLTIPS
) )
496 msStyle
|= TBSTYLE_TOOLTIPS
;
498 if ( style
& wxTB_FLAT
&& wxApp::GetComCtl32Version() > 400 )
499 msStyle
|= TBSTYLE_FLAT
;
501 if ( style
& wxTB_HORZ_LAYOUT
&& wxApp::GetComCtl32Version() >= 470 )
502 msStyle
|= TBSTYLE_LIST
;
504 if ( style
& wxTB_NODIVIDER
)
505 msStyle
|= CCS_NODIVIDER
;
507 if ( style
& wxTB_NOALIGN
)
508 msStyle
|= CCS_NOPARENTALIGN
;
510 if ( style
& wxTB_VERTICAL
)
513 if( style
& wxTB_BOTTOM
)
514 msStyle
|= CCS_BOTTOM
;
516 if ( style
& wxTB_RIGHT
)
517 msStyle
|= CCS_RIGHT
;
519 // always use TBSTYLE_TRANSPARENT because the background is not drawn
520 // correctly without it in all themes and, for whatever reason, the control
521 // also flickers horribly when it is resized if this style is not used
523 // note that this is implicitly enabled by the native toolbar itself when
524 // TBSTYLE_FLAT is used (i.e. it's impossible to use TBSTYLE_FLAT without
525 // TBSTYLE_TRANSPARENT) but turn it on explicitly in any case
526 msStyle
|= TBSTYLE_TRANSPARENT
;
531 // ----------------------------------------------------------------------------
532 // adding/removing tools
533 // ----------------------------------------------------------------------------
535 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
536 wxToolBarToolBase
* WXUNUSED(tool
))
538 // nothing special to do here - we really create the toolbar buttons in
540 InvalidateBestSize();
544 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
546 // the main difficulty we have here is with the controls in the toolbars:
547 // as we (sometimes) use several separators to cover up the space used by
548 // them, the indices are not the same for us and the toolbar
550 // first determine the position of the first button to delete: it may be
551 // different from pos if we use several separators to cover the space used
553 wxToolBarToolsList::compatibility_iterator node
;
554 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
556 wxToolBarToolBase
*tool2
= node
->GetData();
559 // let node point to the next node in the list
560 node
= node
->GetNext();
565 if ( tool2
->IsControl() )
566 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
569 // now determine the number of buttons to delete and the area taken by them
570 size_t nButtonsToDelete
= 1;
572 // get the size of the button we're going to delete
573 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
575 int delta
= IsVertical() ? r
.bottom
- r
.top
: r
.right
- r
.left
;
577 if ( tool
->IsControl() )
579 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
582 delta
*= nButtonsToDelete
;
585 m_totalFixedSize
-= delta
;
587 // do delete all buttons
588 m_nButtons
-= nButtonsToDelete
;
589 while ( nButtonsToDelete
-- > 0 )
591 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
593 wxLogLastError(wxT("TB_DELETEBUTTON"));
599 // and finally rearrange the tools
601 // search for any stretch spacers before the removed tool
602 bool hasPrecedingStrechables
= false;
603 for ( wxToolBarToolsList::compatibility_iterator nodeStch
= m_tools
.GetFirst();
604 nodeStch
!= node
; nodeStch
= nodeStch
->GetNext() )
606 if ( ((wxToolBarTool
*)nodeStch
->GetData())->IsStretchable() )
608 hasPrecedingStrechables
= true;
613 if ( hasPrecedingStrechables
)
615 // if the removed tool is preceded by stretch spacers
616 // just redistribute the space
617 UpdateStretchableSpacersSize();
621 // reposition all the controls after this button but before any
622 // stretch spacer (the toolbar takes care of all normal items)
623 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
625 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
627 if ( tool2
->IsControl() )
629 tool2
->MoveBy(-delta
);
632 // if a stretch spacer is found just redistribute the available space
633 else if ( tool2
->IsStretchable() )
635 UpdateStretchableSpacersSize();
641 InvalidateBestSize();
646 void wxToolBar::CreateDisabledImageList()
648 wxDELETE(m_disabledImgList
);
650 // as we can't use disabled image list with older versions of comctl32.dll,
651 // don't even bother creating it
652 if ( wxApp::GetComCtl32Version() >= 470 )
654 // search for the first disabled button img in the toolbar, if any
655 for ( wxToolBarToolsList::compatibility_iterator
656 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
658 wxToolBarToolBase
*tool
= node
->GetData();
659 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
660 if ( bmpDisabled
.IsOk() )
662 const wxSize sizeBitmap
= bmpDisabled
.GetSize();
663 m_disabledImgList
= new wxImageList
667 bmpDisabled
.GetMask() != NULL
,
674 // we don't have any disabled bitmaps
678 bool wxToolBar::Realize()
680 if ( !wxToolBarBase::Realize() )
683 const size_t nTools
= GetToolsCount();
685 #ifdef wxREMAP_BUTTON_COLOURS
686 // don't change the values of these constants, they can be set from the
687 // user code via wxSystemOptions
696 // the user-specified option overrides anything, but if it wasn't set, only
697 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
698 // much worse after remapping
699 static const wxChar
*remapOption
= wxT("msw.remap");
700 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
701 ? wxSystemOptions::GetOptionInt(remapOption
)
702 : wxDisplayDepth() <= 8 ? Remap_Buttons
705 #endif // wxREMAP_BUTTON_COLOURS
707 // delete all old buttons, if any
708 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
710 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
712 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
716 // First, add the bitmap: we use one bitmap for all toolbar buttons
717 // ----------------------------------------------------------------
719 wxToolBarToolsList::compatibility_iterator node
;
722 if ( !HasFlag(wxTB_NOICONS
) )
724 // if we already have a bitmap, we'll replace the existing one --
725 // otherwise we'll install a new one
726 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
728 const wxCoord totalBitmapWidth
= m_defaultWidth
*
729 wx_truncate_cast(wxCoord
, nTools
),
730 totalBitmapHeight
= m_defaultHeight
;
732 // Create a bitmap and copy all the tool bitmaps into it
733 wxMemoryDC dcAllButtons
;
734 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
735 dcAllButtons
.SelectObject(bitmap
);
737 #ifdef wxREMAP_BUTTON_COLOURS
738 if ( remapValue
!= Remap_TransparentBg
)
739 #endif // wxREMAP_BUTTON_COLOURS
741 // VZ: why do we hardcode grey colour for CE?
742 dcAllButtons
.SetBackground(wxBrush(
744 wxColour(0xc0, 0xc0, 0xc0)
745 #else // !__WXWINCE__
746 GetBackgroundColour()
747 #endif // __WXWINCE__/!__WXWINCE__
749 dcAllButtons
.Clear();
752 m_hBitmap
= bitmap
.GetHBITMAP();
753 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
755 #ifdef wxREMAP_BUTTON_COLOURS
756 if ( remapValue
== Remap_Bg
)
758 dcAllButtons
.SelectObject(wxNullBitmap
);
760 // Even if we're not remapping the bitmap
761 // content, we still have to remap the background.
762 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
763 totalBitmapWidth
, totalBitmapHeight
);
765 dcAllButtons
.SelectObject(bitmap
);
767 #endif // wxREMAP_BUTTON_COLOURS
769 // the button position
772 // the number of buttons (not separators)
775 CreateDisabledImageList();
776 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
778 wxToolBarToolBase
*tool
= node
->GetData();
779 if ( tool
->IsButton() )
781 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
783 const int w
= bmp
.GetWidth();
784 const int h
= bmp
.GetHeight();
788 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
789 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
791 // notice the last parameter: do use mask
792 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
796 wxFAIL_MSG( wxT("invalid tool button bitmap") );
799 // also deal with disabled bitmap if we want to use them
800 if ( m_disabledImgList
)
802 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
803 #if wxUSE_IMAGE && wxUSE_WXDIB
804 if ( !bmpDisabled
.IsOk() )
806 // no disabled bitmap specified but we still need to
807 // fill the space in the image list with something, so
808 // we grey out the normal bitmap
810 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
812 #ifdef wxREMAP_BUTTON_COLOURS
813 if ( remapValue
== Remap_Buttons
)
815 // we need to have light grey background colour for
816 // MapBitmap() to work correctly
817 for ( int y
= 0; y
< h
; y
++ )
819 for ( int x
= 0; x
< w
; x
++ )
821 if ( imgGreyed
.IsTransparent(x
, y
) )
822 imgGreyed
.SetRGB(x
, y
,
824 wxLIGHT_GREY
->Green(),
825 wxLIGHT_GREY
->Blue());
829 #endif // wxREMAP_BUTTON_COLOURS
831 bmpDisabled
= wxBitmap(imgGreyed
);
833 #endif // wxUSE_IMAGE
835 #ifdef wxREMAP_BUTTON_COLOURS
836 if ( remapValue
== Remap_Buttons
)
837 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
838 #endif // wxREMAP_BUTTON_COLOURS
840 m_disabledImgList
->Add(bmpDisabled
);
843 // still inc width and number of buttons because otherwise the
844 // subsequent buttons will all be shifted which is rather confusing
845 // (and like this you'd see immediately which bitmap was bad)
851 dcAllButtons
.SelectObject(wxNullBitmap
);
853 // don't delete this HBITMAP!
854 bitmap
.SetHBITMAP(0);
856 #ifdef wxREMAP_BUTTON_COLOURS
857 if ( remapValue
== Remap_Buttons
)
859 // Map to system colours
860 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
861 totalBitmapWidth
, totalBitmapHeight
);
863 #endif // wxREMAP_BUTTON_COLOURS
865 bool addBitmap
= true;
867 if ( oldToolBarBitmap
)
869 #ifdef TB_REPLACEBITMAP
870 if ( wxApp::GetComCtl32Version() >= 400 )
872 TBREPLACEBITMAP replaceBitmap
;
873 replaceBitmap
.hInstOld
= NULL
;
874 replaceBitmap
.hInstNew
= NULL
;
875 replaceBitmap
.nIDOld
= (UINT_PTR
)oldToolBarBitmap
;
876 replaceBitmap
.nIDNew
= (UINT_PTR
)hBitmap
;
877 replaceBitmap
.nButtons
= nButtons
;
878 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
879 0, (LPARAM
) &replaceBitmap
) )
881 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
884 ::DeleteObject(oldToolBarBitmap
);
890 #endif // TB_REPLACEBITMAP
892 // we can't replace the old bitmap, so we will add another one
893 // (awfully inefficient, but what else to do?) and shift the bitmap
894 // indices accordingly
897 bitmapId
= m_nButtons
;
901 if ( addBitmap
) // no old bitmap or we can't replace it
903 TBADDBITMAP addBitmap
;
905 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
906 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
907 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
909 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
913 // disable image lists are only supported in comctl32.dll 4.70+
914 if ( wxApp::GetComCtl32Version() >= 470 )
916 HIMAGELIST hil
= m_disabledImgList
917 ? GetHimagelistOf(m_disabledImgList
)
920 // notice that we set the image list even if don't have one right
921 // now as we could have it before and need to reset it in this case
922 HIMAGELIST oldImageList
= (HIMAGELIST
)
923 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
925 // delete previous image list if any
927 ::DeleteObject(oldImageList
);
932 // Next add the buttons and separators
933 // -----------------------------------
935 wxScopedArray
<TBBUTTON
> buttons(new TBBUTTON
[nTools
]);
937 // this array will hold the indices of all controls in the toolbar
938 wxArrayInt controlIds
;
940 bool lastWasRadio
= false;
942 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
944 wxToolBarTool
*tool
= static_cast<wxToolBarTool
*>(node
->GetData());
946 // don't add separators to the vertical toolbar with old comctl32.dll
947 // versions as they didn't handle this properly
948 if ( IsVertical() && tool
->IsSeparator() &&
949 wxApp::GetComCtl32Version() <= 472 )
954 TBBUTTON
& button
= buttons
[i
];
956 wxZeroMemory(button
);
958 bool isRadio
= false;
959 switch ( tool
->GetStyle() )
961 case wxTOOL_STYLE_CONTROL
:
962 case wxTOOL_STYLE_SEPARATOR
:
963 if ( tool
->IsStretchableSpace() )
965 // we're going to modify the size of this button later and
966 // so we need a valid id for it and not wxID_SEPARATOR
967 // which is used by spacers by default
968 tool
->AllocSpacerId();
970 // also set the number of separators so that the logic in
971 // HandlePaint() works correctly
972 tool
->SetSeparatorsCount(1);
975 button
.idCommand
= tool
->GetId();
976 button
.fsState
= TBSTATE_ENABLED
;
977 button
.fsStyle
= TBSTYLE_SEP
;
980 case wxTOOL_STYLE_BUTTON
:
981 if ( !HasFlag(wxTB_NOICONS
) )
982 button
.iBitmap
= bitmapId
;
984 if ( HasFlag(wxTB_TEXT
) )
986 const wxString
& label
= tool
->GetLabel();
987 if ( !label
.empty() )
988 button
.iString
= (INT_PTR
) wxMSW_CONV_LPCTSTR(label
);
991 button
.idCommand
= tool
->GetId();
993 if ( tool
->IsEnabled() )
994 button
.fsState
|= TBSTATE_ENABLED
;
995 if ( tool
->IsToggled() )
996 button
.fsState
|= TBSTATE_CHECKED
;
998 switch ( tool
->GetKind() )
1001 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
1003 if ( !lastWasRadio
)
1005 // the first item in the radio group is checked by
1006 // default to be consistent with wxGTK and the menu
1008 button
.fsState
|= TBSTATE_CHECKED
;
1010 if (tool
->Toggle(true))
1012 DoToggleTool(tool
, true);
1015 else if ( tool
->IsToggled() )
1017 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
1018 int prevIndex
= i
- 1;
1021 TBBUTTON
& prevButton
= buttons
[prevIndex
];
1022 wxToolBarToolBase
*tool
= nodePrev
->GetData();
1023 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
1026 if ( tool
->Toggle(false) )
1027 DoToggleTool(tool
, false);
1029 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
1030 nodePrev
= nodePrev
->GetPrevious();
1039 button
.fsStyle
= TBSTYLE_CHECK
;
1043 button
.fsStyle
= TBSTYLE_BUTTON
;
1046 case wxITEM_DROPDOWN
:
1047 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1051 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
1052 button
.fsStyle
= TBSTYLE_BUTTON
;
1056 // Instead of using fixed widths for all buttons, size them
1057 // automatically according to the size of their bitmap and text
1058 // label, if present. This particularly matters for toolbars
1059 // with the wxTB_HORZ_LAYOUT style: they look hideously ugly
1060 // without autosizing when the labels have even slightly
1061 // different lengths.
1062 button
.fsStyle
|= TBSTYLE_AUTOSIZE
;
1068 lastWasRadio
= isRadio
;
1073 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, i
, (LPARAM
)buttons
.get()) )
1075 wxLogLastError(wxT("TB_ADDBUTTONS"));
1079 // Adjust controls and stretchable spaces
1080 // --------------------------------------
1082 // adjust the controls size to fit nicely in the toolbar and compute its
1083 // total size while doing it
1084 m_totalFixedSize
= 0;
1086 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1088 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1090 const RECT r
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1092 if ( !tool
->IsControl() )
1095 m_totalFixedSize
+= r
.bottom
- r
.top
;
1097 m_totalFixedSize
+= r
.right
- r
.left
;
1104 // don't embed controls in the vertical toolbar, this doesn't look
1105 // good and wxGTK doesn't do it neither (and the code below can't
1106 // deal with this case)
1110 wxControl
* const control
= tool
->GetControl();
1111 wxStaticText
* const staticText
= tool
->GetStaticText();
1113 wxSize size
= control
->GetSize();
1114 wxSize staticTextSize
;
1117 staticTextSize
= staticText
->GetSize();
1118 staticTextSize
.y
+= 3; // margin between control and its label
1121 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1122 #ifdef TB_SETBUTTONINFO
1123 // available in headers, now check whether it is available now
1124 // (during run-time)
1125 if ( wxApp::GetComCtl32Version() >= 471 )
1127 // set the (underlying) separators width to be that of the
1130 tbbi
.cbSize
= sizeof(tbbi
);
1131 tbbi
.dwMask
= TBIF_SIZE
;
1132 tbbi
.cx
= (WORD
)size
.x
;
1133 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1134 tool
->GetId(), (LPARAM
)&tbbi
) )
1136 // the id is probably invalid?
1137 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1141 #endif // comctl32.dll 4.71
1142 // TB_SETBUTTONINFO unavailable
1144 // try adding several separators to fit the controls width
1145 int widthSep
= r
.right
- r
.left
;
1150 tbb
.fsState
= TBSTATE_ENABLED
;
1151 tbb
.fsStyle
= TBSTYLE_SEP
;
1153 size_t nSeparators
= size
.x
/ widthSep
;
1154 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1156 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1157 toolIndex
, (LPARAM
)&tbb
) )
1159 wxLogLastError(wxT("TB_INSERTBUTTON"));
1165 // remember the number of separators we used - we'd have to
1166 // delete all of them later
1167 tool
->SetSeparatorsCount(nSeparators
);
1169 // adjust the controls width to exactly cover the separators
1170 size
.x
= (nSeparators
+ 1)*widthSep
;
1171 control
->SetSize(size
.x
, wxDefaultCoord
);
1174 // position the control itself correctly vertically centering it on the
1175 // icon area of the toolbar
1176 int height
= r
.bottom
- r
.top
- staticTextSize
.y
;
1178 int diff
= height
- size
.y
;
1179 if ( diff
< 0 || !HasFlag(wxTB_TEXT
) )
1181 // not enough room for the static text
1185 // recalculate height & diff without the staticText control
1186 height
= r
.bottom
- r
.top
;
1187 diff
= height
- size
.y
;
1190 // the control is too high, resize to fit
1191 control
->SetSize(wxDefaultCoord
, height
- 2);
1196 else // enough space for both the control and the label
1202 control
->Move(r
.left
, r
.top
+ (diff
+ 1) / 2);
1205 staticText
->Move(r
.left
+ (size
.x
- staticTextSize
.x
)/2,
1206 r
.bottom
- staticTextSize
.y
);
1209 m_totalFixedSize
+= size
.x
;
1212 // the max index is the "real" number of buttons - i.e. counting even the
1213 // separators which we added just for aligning the controls
1214 m_nButtons
= toolIndex
;
1216 if ( !IsVertical() )
1218 if ( m_maxRows
== 0 )
1219 // if not set yet, only one row
1222 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1224 // if not set yet, have one column
1226 SetRows(m_nButtons
);
1229 InvalidateBestSize();
1235 void wxToolBar::UpdateStretchableSpacersSize()
1237 #ifdef TB_SETBUTTONINFO
1238 // we can't resize the spacers if TB_SETBUTTONINFO is not supported (we
1239 // could try to do it with multiple separators as for the controls but this
1240 // is too painful and it just doesn't seem to be worth doing for the
1242 if ( wxApp::GetComCtl32Version() < 471 )
1245 // check if we have any stretchable spacers in the first place
1246 unsigned numSpaces
= 0;
1247 wxToolBarToolsList::compatibility_iterator node
;
1248 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1250 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1251 if ( tool
->IsStretchableSpace() )
1258 // we do, adjust their size: either distribute the extra size among them or
1259 // reduce their size if there is not enough place for all tools
1260 const int totalSize
= IsVertical() ? GetClientSize().y
: GetClientSize().x
;
1261 const int extraSize
= totalSize
- m_totalFixedSize
;
1262 const int sizeSpacer
= extraSize
> 0 ? extraSize
/ numSpaces
: 1;
1264 // the last spacer should consume all remaining space if we have too much
1265 // of it (which can be greater than sizeSpacer because of the rounding)
1266 const int sizeLastSpacer
= extraSize
> 0
1267 ? extraSize
- (numSpaces
- 1)*sizeSpacer
1270 // cumulated offset by which we need to move all the following controls to
1271 // the right: while the toolbar takes care of the normal items, we must
1272 // move the controls manually ourselves to ensure they remain at the
1276 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1278 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1280 if ( tool
->IsControl() && offset
)
1282 tool
->MoveBy(offset
);
1287 if ( !tool
->IsStretchableSpace() )
1290 const RECT rcOld
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1292 WinStruct
<TBBUTTONINFO
> tbbi
;
1293 tbbi
.dwMask
= TBIF_SIZE
;
1294 tbbi
.cx
= --numSpaces
? sizeSpacer
: sizeLastSpacer
;
1296 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1297 tool
->GetId(), (LPARAM
)&tbbi
) )
1299 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1303 // we successfully resized this one, move all the controls after it
1304 // by the corresponding amount (may be positive or negative)
1305 offset
+= tbbi
.cx
- (rcOld
.right
- rcOld
.left
);
1308 #endif // TB_SETBUTTONINFO
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
1317 // cast to signed is important as we compare this id with (signed) ints in
1318 // FindById() and without the cast we'd get a positive int from a
1319 // "negative" (i.e. > 32767) WORD
1320 const int id
= (signed short)id_
;
1322 wxToolBarToolBase
*tool
= FindById(id
);
1326 bool toggled
= false; // just to suppress warnings
1328 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1330 if ( tool
->CanBeToggled() )
1332 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1334 // ignore the event when a radio button is released, as this doesn't
1335 // seem to happen at all, and is handled otherwise
1336 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1339 tool
->Toggle(toggled
);
1340 UnToggleRadioGroup(tool
);
1343 // Without the two lines of code below, if the toolbar was repainted during
1344 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1345 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1346 // The Update() call below ensures that this won't happen, by repainting
1347 // invalidated areas of the toolbar immediately.
1349 // To complicate matters, the tool would be drawn in depressed state (this
1350 // code is called when mouse button is released, not pressed). That's not
1351 // ideal, having the tool pressed for the duration of OnLeftClick()
1352 // provides the user with useful visual clue that the app is busy reacting
1353 // to the event. So we manually put the tool into pressed state, handle the
1354 // event and then finally restore tool's original state.
1355 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
| TBSTATE_PRESSED
, 0));
1358 bool allowLeftClick
= OnLeftClick(id
, toggled
);
1360 // Restore the unpressed state. Enabled/toggled state might have been
1361 // changed since so take care of it.
1362 if (tool
->IsEnabled())
1363 state
|= TBSTATE_ENABLED
;
1365 state
&= ~TBSTATE_ENABLED
;
1366 if (tool
->IsToggled())
1367 state
|= TBSTATE_CHECKED
;
1369 state
&= ~TBSTATE_CHECKED
;
1370 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
, 0));
1372 // OnLeftClick() can veto the button state change - for buttons which
1373 // may be toggled only, of course.
1374 if ( !allowLeftClick
&& tool
->CanBeToggled() )
1377 tool
->Toggle(!toggled
);
1379 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1385 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1387 WXLPARAM
*WXUNUSED(result
))
1389 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1390 if ( hdr
->code
== TBN_DROPDOWN
)
1392 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1394 wxCommandEvent
evt(wxEVT_TOOL_DROPDOWN
, tbhdr
->iItem
);
1395 if ( HandleWindowEvent(evt
) )
1397 // Event got handled, don't display default popup menu
1401 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1402 wxCHECK_MSG( tool
, false, wxT("drop down message for unknown tool") );
1404 wxMenu
* const menu
= tool
->GetDropdownMenu();
1408 // Display popup menu below button
1409 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1411 PopupMenu(menu
, r
.left
, r
.bottom
);
1417 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1420 // First check if this applies to us
1422 // the tooltips control created by the toolbar is sometimes Unicode, even
1423 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1424 UINT code
= hdr
->code
;
1425 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1428 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1429 if ( toolTipWnd
!= hdr
->hwndFrom
)
1432 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1433 int id
= (int)ttText
->hdr
.idFrom
;
1435 wxToolBarToolBase
*tool
= FindById(id
);
1437 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1439 wxUnusedVar(lParam
);
1446 // ----------------------------------------------------------------------------
1448 // ----------------------------------------------------------------------------
1450 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1452 wxToolBarBase::SetToolBitmapSize(size
);
1454 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1457 void wxToolBar::SetRows(int nRows
)
1459 if ( nRows
== m_maxRows
)
1461 // avoid resizing the frame uselessly
1465 // TRUE in wParam means to create at least as many rows, FALSE -
1468 ::SendMessage(GetHwnd(), TB_SETROWS
,
1469 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1477 // The button size is bigger than the bitmap size
1478 wxSize
wxToolBar::GetToolSize() const
1480 // TB_GETBUTTONSIZE is supported from version 4.70
1481 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1482 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1483 && !defined (__DIGITALMARS__)
1484 if ( wxApp::GetComCtl32Version() >= 470 )
1486 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1488 return wxSize(LOWORD(dw
), HIWORD(dw
));
1491 #endif // comctl32.dll 4.70+
1494 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1499 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1502 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1504 for ( ; current
; current
= current
->GetNext() )
1507 return current
->GetData();
1509 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1510 size_t separators
= tool
->GetSeparatorsCount();
1512 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1513 // otherwise, skip as many items as the separator count, plus the
1515 index
-= separators
? separators
+ 1 : 1;
1521 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1526 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1528 // MBN: when the point ( x, y ) is close to the toolbar border
1529 // TB_HITTEST returns m_nButtons ( not -1 )
1530 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1531 // it's a separator or there is no tool at all there
1534 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1535 // we don't use the dummy separators hack
1536 #ifdef TB_SETBUTTONINFO
1537 if ( wxApp::GetComCtl32Version() >= 471 )
1539 return m_tools
.Item((size_t)index
)->GetData();
1542 #endif // TB_SETBUTTONINFO
1544 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1548 void wxToolBar::UpdateSize()
1550 wxPoint pos
= GetPosition();
1551 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1552 if (pos
!= GetPosition())
1555 // In case Realize is called after the initial display (IOW the programmer
1556 // may have rebuilt the toolbar) give the frame the option of resizing the
1557 // toolbar to full width again, but only if the parent is a frame and the
1558 // toolbar is managed by the frame. Otherwise assume that some other
1559 // layout mechanism is controlling the toolbar size and leave it alone.
1560 SendSizeEventToParent();
1563 // ----------------------------------------------------------------------------
1565 // ---------------------------------------------------------------------------
1567 // get the TBSTYLE of the given toolbar window
1568 long wxToolBar::GetMSWToolbarStyle() const
1570 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1573 void wxToolBar::SetWindowStyleFlag(long style
)
1575 // the style bits whose changes force us to recreate the toolbar
1576 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1578 const long styleOld
= GetWindowStyle();
1580 wxToolBarBase::SetWindowStyleFlag(style
);
1582 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1583 // also fatal as we'd then try to recreate the toolbar when it's just being
1585 if ( GetToolsCount() &&
1586 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1588 // to remove the text labels, simply re-realizing the toolbar is enough
1589 // but I don't know of any way to add the text to an existing toolbar
1590 // other than by recreating it entirely
1595 // ----------------------------------------------------------------------------
1597 // ----------------------------------------------------------------------------
1599 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1601 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1602 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1605 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1607 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1608 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1611 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1613 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1614 // without, so we really need to delete the button and recreate it here
1615 wxFAIL_MSG( wxT("not implemented") );
1618 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1620 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1623 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1625 tool
->SetNormalBitmap(bitmap
);
1630 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1632 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1635 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1637 tool
->SetDisabledBitmap(bitmap
);
1642 // ----------------------------------------------------------------------------
1644 // ----------------------------------------------------------------------------
1646 // Responds to colour changes, and passes event on to children.
1647 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1649 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1651 // Remap the buttons
1654 // Relayout the toolbar
1655 int nrows
= m_maxRows
;
1656 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1661 // let the event propagate further
1665 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1667 if ( event
.Leaving() )
1671 OnMouseEnter(wxID_ANY
);
1679 if ( event
.RightDown() )
1681 // find the tool under the mouse
1682 wxCoord x
= 0, y
= 0;
1683 event
.GetPosition(&x
, &y
);
1685 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1686 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1694 // This handler is needed to fix problems with painting the background of
1695 // toolbar icons with comctl32.dll < 6.0.
1696 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1698 #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
1699 MSWDoEraseBackground(event
.GetDC()->GetHDC());
1700 #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
1703 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1705 // wait until we have some tools
1706 if ( !GetToolsCount() )
1709 // calculate our minor dimension ourselves - we're confusing the standard
1710 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1711 const RECT r
= wxGetTBItemRect(GetHwnd(), 0);
1719 w
= r
.right
- r
.left
;
1722 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1729 if (HasFlag( wxTB_FLAT
))
1730 h
= r
.bottom
- r
.top
- 3;
1732 h
= r
.bottom
- r
.top
;
1735 // FIXME: hardcoded separator line height...
1736 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1741 if ( MAKELPARAM(w
, h
) != lParam
)
1743 // size really changed
1747 UpdateStretchableSpacersSize();
1749 // message processed
1753 #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
1755 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1757 // we must prevent the dummy separators corresponding to controls or
1758 // stretchable spaces from being seen: we used to do it by painting over
1759 // them but this, unsurprisingly, resulted in a lot of flicker so now we
1760 // prevent the toolbar from painting them at all
1762 // compute the region containing all dummy separators which we don't want
1764 wxRegion rgnDummySeps
;
1765 const wxRect rectTotal
= GetClientRect();
1767 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1769 node
= node
->GetNext() )
1771 wxToolBarTool
* const
1772 tool
= static_cast<wxToolBarTool
*>(node
->GetData());
1774 if ( tool
->IsControl() || tool
->IsStretchableSpace() )
1776 const size_t numSeps
= tool
->GetSeparatorsCount();
1777 for ( size_t n
= 0; n
< numSeps
; n
++, toolIndex
++ )
1779 // for some reason TB_GETITEMRECT returns a rectangle 1 pixel
1780 // shorter than the full window size (at least under Windows 7)
1781 // but we need to erase the full width/height below
1782 RECT rcItem
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1786 rcItem
.right
= rectTotal
.width
;
1791 rcItem
.bottom
= rectTotal
.height
;
1794 rgnDummySeps
.Union(wxRectFromRECT(rcItem
));
1799 // normal tools never correspond to more than one native button
1804 if ( rgnDummySeps
.IsOk() )
1806 // exclude the area occupied by the controls and stretchable spaces
1807 // from the update region to prevent the toolbar from drawing
1809 if ( !::ValidateRgn(GetHwnd(), GetHrgnOf(rgnDummySeps
)) )
1811 wxLogLastError(wxT("ValidateRgn()"));
1815 // still let the native control draw everything else normally but set up a
1816 // hook to be able to process the next WM_ERASEBKGND sent to our parent
1817 // because toolbar will ask it to erase its background from its WM_PAINT
1818 // handler (when using TBSTYLE_TRANSPARENT which we do always use)
1820 // installing hook is not completely trivial as all kinds of strange
1821 // situations are possible: sometimes we can be called recursively from
1822 // inside the native toolbar WM_PAINT handler so the hook might already be
1823 // installed and sometimes the native toolbar might not send WM_ERASEBKGND
1824 // to the parent at all for whatever reason, so deal with all these cases
1825 wxWindow
* const parent
= GetParent();
1826 const bool hadHook
= parent
->MSWHasEraseBgHook();
1828 GetParent()->MSWSetEraseBgHook(this);
1830 MSWDefWindowProc(WM_PAINT
, wParam
, lParam
);
1833 GetParent()->MSWSetEraseBgHook(NULL
);
1836 if ( rgnDummySeps
.IsOk() )
1838 // erase the dummy separators region ourselves now as nobody painted
1840 WindowHDC
hdc(GetHwnd());
1841 ::SelectClipRgn(hdc
, GetHrgnOf(rgnDummySeps
));
1842 MSWDoEraseBackground(hdc
);
1848 WXHBRUSH
wxToolBar::MSWGetToolbarBgBrush()
1850 // we conservatively use a solid brush here but we could also use a themed
1851 // brush by using DrawThemeBackground() to create a bitmap brush (it'd need
1852 // to be invalidated whenever the toolbar is resized and, also, correctly
1853 // aligned using SetBrushOrgEx() before each use -- there is code for doing
1854 // this in wxNotebook already so it'd need to be refactored into wxWindow)
1856 // however inasmuch as there is a default background for the toolbar at all
1857 // (and this is not a trivial question as different applications use very
1858 // different colours), it seems to be a solid one and using REBAR
1859 // background brush as we used to do before doesn't look good at all under
1860 // Windows 7 (and probably Vista too), so for now we just keep it simple
1862 colBg
= m_hasBgCol
? GetBackgroundColour()
1863 : wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
1865 brush
= wxTheBrushList
->FindOrCreateBrush(colBg
);
1867 return brush
? static_cast<WXHBRUSH
>(brush
->GetResourceHandle()) : 0;
1870 WXHBRUSH
wxToolBar::MSWGetBgBrushForChild(WXHDC hDC
, wxWindowMSW
*child
)
1872 WXHBRUSH hbr
= wxToolBarBase::MSWGetBgBrushForChild(hDC
, child
);
1876 // the base class version only returns a brush for erasing children
1877 // background if we have a non-default background colour but as the toolbar
1878 // doesn't erase its own background by default, we need to always do it for
1879 // (semi-)transparent children
1880 if ( child
->GetParent() == this && child
->HasTransparentBackground() )
1881 return MSWGetToolbarBgBrush();
1886 void wxToolBar::MSWDoEraseBackground(WXHDC hDC
)
1888 wxFillRect(GetHwnd(), (HDC
)hDC
, (HBRUSH
)MSWGetToolbarBgBrush());
1891 bool wxToolBar::MSWEraseBgHook(WXHDC hDC
)
1893 // toolbar WM_PAINT handler offsets the DC origin before sending
1894 // WM_ERASEBKGND to the parent but as we handle it in the toolbar itself,
1895 // we need to reset it back
1898 if ( !::SetWindowOrgEx(hdc
, 0, 0, &ptOldOrg
) )
1900 wxLogLastError(wxT("SetWindowOrgEx(tbar-bg-hdc)"));
1904 MSWDoEraseBackground(hDC
);
1906 ::SetWindowOrgEx(hdc
, ptOldOrg
.x
, ptOldOrg
.y
, NULL
);
1911 #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
1913 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1915 wxCoord x
= GET_X_LPARAM(lParam
),
1916 y
= GET_Y_LPARAM(lParam
);
1917 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1919 // has the current tool changed?
1920 if ( tool
!= m_pInTool
)
1923 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1927 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1932 // we don't handle mouse moves, so always pass the message to
1933 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1934 HandleMouseMove(wParam
, lParam
);
1938 if ( HandleSize(wParam
, lParam
) )
1942 #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
1944 // refreshing the controls in the toolbar inside a composite window
1945 // results in an endless stream of WM_PAINT messages -- and seems
1946 // to be unnecessary anyhow as everything works just fine without
1947 // any special workarounds in this case
1948 if ( !IsDoubleBuffered() && HandlePaint(wParam
, lParam
) )
1951 #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
1954 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1957 // ----------------------------------------------------------------------------
1958 // private functions
1959 // ----------------------------------------------------------------------------
1961 #ifdef wxREMAP_BUTTON_COLOURS
1963 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1969 wxLogLastError(wxT("CreateCompatibleDC"));
1974 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1978 wxLogLastError(wxT("SelectObject"));
1983 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1985 for ( int i
= 0; i
< width
; i
++ )
1987 for ( int j
= 0; j
< height
; j
++ )
1989 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1991 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1993 COLORREF col
= cmap
[k
].from
;
1994 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1995 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1996 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1998 if ( cmap
[k
].to
!= pixel
)
1999 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
2009 #endif // wxREMAP_BUTTON_COLOURS
2011 #endif // wxUSE_TOOLBAR