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/dcmemory.h"
40 #include "wx/control.h"
41 #include "wx/app.h" // for GetComCtl32Version
43 #include "wx/stattext.h"
46 #include "wx/artprov.h"
47 #include "wx/sysopt.h"
48 #include "wx/dcclient.h"
49 #include "wx/scopedarray.h"
51 #include "wx/msw/private.h"
52 #include "wx/msw/dc.h"
55 #include "wx/msw/uxtheme.h"
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
64 #define wxREMAP_BUTTON_COLOURS
65 #endif // !__WXWINCE__
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // these standard constants are not always defined in compilers headers
75 #define TBSTYLE_LIST 0x1000
76 #define TBSTYLE_FLAT 0x0800
79 #ifndef TBSTYLE_TRANSPARENT
80 #define TBSTYLE_TRANSPARENT 0x8000
83 #ifndef TBSTYLE_TOOLTIPS
84 #define TBSTYLE_TOOLTIPS 0x0100
89 #define TB_SETSTYLE (WM_USER + 56)
90 #define TB_GETSTYLE (WM_USER + 57)
94 #define TB_HITTEST (WM_USER + 69)
98 #define TB_GETMAXSIZE (WM_USER + 83)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
117 style ( wxNO_BORDER | wxTB_HORIZONTAL)
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
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 class wxToolBarTool
: public wxToolBarToolBase
139 wxToolBarTool(wxToolBar
*tbar
,
141 const wxString
& label
,
142 const wxBitmap
& bmpNormal
,
143 const wxBitmap
& bmpDisabled
,
145 wxObject
*clientData
,
146 const wxString
& shortHelp
,
147 const wxString
& longHelp
)
148 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
149 clientData
, shortHelp
, longHelp
)
155 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
156 : wxToolBarToolBase(tbar
, control
, label
)
158 if ( IsControl() && !m_label
.empty() )
160 // create a control to render the control's label
161 m_staticText
= new wxStaticText
168 wxALIGN_CENTRE
| wxST_NO_AUTORESIZE
179 virtual ~wxToolBarTool()
184 virtual void SetLabel(const wxString
& label
)
186 if ( label
== m_label
)
189 wxToolBarToolBase::SetLabel(label
);
192 m_staticText
->SetLabel(label
);
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
197 // TODO: use TB_SETBUTTONINFO
200 wxStaticText
* GetStaticText()
202 wxASSERT_MSG( IsControl(),
203 wxT("only makes sense for embedded control tools") );
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
; }
213 // we need ids for the spacers which we want to modify later on, this
214 // function will allocate a valid/unique id for a spacer if not done yet
217 if ( m_id
== wxID_SEPARATOR
)
218 m_id
= wxWindow::NewControlId();
221 // this method is used for controls only and offsets the control by the
222 // given amount (in pixels) in horizontal direction
223 void MoveBy(int offset
)
225 wxControl
* const control
= GetControl();
227 control
->Move(control
->GetPosition().x
+ offset
, wxDefaultCoord
);
231 m_staticText
->Move(m_staticText
->GetPosition().x
+ offset
,
238 wxStaticText
*m_staticText
;
240 wxDECLARE_NO_COPY_CLASS(wxToolBarTool
);
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 // return the rectangle of the item at the given index
249 // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
250 // r.right or r.bottom with 0 to check for this
251 static RECT
wxGetTBItemRect(HWND hwnd
, int index
)
255 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
256 // only appeared in v4.70 of comctl32.dll
257 if ( !::SendMessage(hwnd
, TB_GETITEMRECT
, index
, (LPARAM
)&r
) )
259 wxLogLastError(wxT("TB_GETITEMRECT"));
270 // ============================================================================
272 // ============================================================================
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
279 const wxString
& label
,
280 const wxBitmap
& bmpNormal
,
281 const wxBitmap
& bmpDisabled
,
283 wxObject
*clientData
,
284 const wxString
& shortHelp
,
285 const wxString
& longHelp
)
287 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
288 clientData
, shortHelp
, longHelp
);
292 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
294 return new wxToolBarTool(this, control
, label
);
297 // ----------------------------------------------------------------------------
298 // wxToolBar construction
299 // ----------------------------------------------------------------------------
301 void wxToolBar::Init()
304 m_disabledImgList
= NULL
;
307 m_totalFixedSize
= 0;
309 // even though modern Windows applications typically use 24*24 (or even
310 // 32*32) size for their bitmaps, the native control itself still uses the
311 // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so
312 // default to it so that we don't call SetToolBitmapSize() unnecessarily in
313 // wxToolBarBase::AdjustToolBitmapSize()
315 m_defaultHeight
= 15;
320 bool wxToolBar::Create(wxWindow
*parent
,
325 const wxString
& name
)
327 // common initialisation
328 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
333 // MSW-specific initialisation
334 if ( !MSWCreateToolbar(pos
, size
) )
337 wxSetCCUnicodeFormat(GetHwnd());
339 // workaround for flat toolbar on Windows XP classic style: we have to set
340 // the style after creating the control; doing it at creation time doesn't work
342 if ( style
& wxTB_FLAT
)
344 LRESULT style
= GetMSWToolbarStyle();
346 if ( !(style
& TBSTYLE_FLAT
) )
347 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
349 #endif // wxUSE_UXTHEME
354 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
356 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
359 // toolbar-specific post initialisation
360 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
362 #ifdef TB_SETEXTENDEDSTYLE
363 if ( wxApp::GetComCtl32Version() >= 471 )
364 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE
, 0, TBSTYLE_EX_DRAWDDARROWS
);
370 void wxToolBar::Recreate()
372 const HWND hwndOld
= GetHwnd();
375 // we haven't been created yet, no need to recreate
379 // get the position and size before unsubclassing the old toolbar
380 const wxPoint pos
= GetPosition();
381 const wxSize size
= GetSize();
385 if ( !MSWCreateToolbar(pos
, size
) )
388 wxFAIL_MSG( wxT("recreating the toolbar failed") );
393 // reparent all our children under the new toolbar
394 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
396 node
= node
->GetNext() )
398 wxWindow
*win
= node
->GetData();
399 if ( !win
->IsTopLevel() )
400 ::SetParent(GetHwndOf(win
), GetHwnd());
403 // only destroy the old toolbar now --
404 // after all the children had been reparented
405 ::DestroyWindow(hwndOld
);
407 // it is for the old bitmap control and can't be used with the new one
410 ::DeleteObject((HBITMAP
) m_hBitmap
);
414 if ( m_disabledImgList
)
416 delete m_disabledImgList
;
417 m_disabledImgList
= NULL
;
423 wxToolBar::~wxToolBar()
425 // we must refresh the frame size when the toolbar is deleted but the frame
426 // is not - otherwise toolbar leaves a hole in the place it used to occupy
427 SendSizeEventToParent();
430 ::DeleteObject((HBITMAP
) m_hBitmap
);
432 delete m_disabledImgList
;
435 wxSize
wxToolBar::DoGetBestSize() const
440 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
442 // maybe an old (< 0x400) Windows version? try to approximate the
443 // toolbar size ourselves
444 sizeBest
= GetToolSize();
445 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
446 sizeBest
.x
*= GetToolsCount();
448 // reverse horz and vertical components if necessary
452 sizeBest
.x
= sizeBest
.y
;
456 else // TB_GETMAXSIZE succeeded
458 // but it could still return an incorrect result due to what appears to
459 // be a bug in old comctl32.dll versions which don't handle controls in
460 // the toolbar correctly, so work around it (see SF patch 1902358)
461 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
463 // calculate the toolbar width in alternative way
464 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
465 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
467 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
468 if ( widthAlt
> size
.cx
)
472 sizeBest
.x
= size
.cx
;
473 sizeBest
.y
= size
.cy
;
478 // Without the extra height, DoGetBestSize can report a size that's
479 // smaller than the actual window, causing windows to overlap slightly
480 // in some circumstances, leading to missing borders (especially noticeable
482 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
487 CacheBestSize(sizeBest
);
492 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
494 // toolbars never have border, giving one to them results in broken
496 WXDWORD msStyle
= wxControl::MSWGetStyle
498 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
501 if ( !(style
& wxTB_NO_TOOLTIPS
) )
502 msStyle
|= TBSTYLE_TOOLTIPS
;
504 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
506 // static as it doesn't change during the program lifetime
507 static const int s_verComCtl
= wxApp::GetComCtl32Version();
509 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
510 // style with 6.00 (part of Windows XP) leads to the toolbar with
511 // incorrect background colour - and not using it still results in the
512 // correct (flat) toolbar, so don't use it there
513 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
514 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
516 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
517 msStyle
|= TBSTYLE_LIST
;
520 if ( style
& wxTB_NODIVIDER
)
521 msStyle
|= CCS_NODIVIDER
;
523 if ( style
& wxTB_NOALIGN
)
524 msStyle
|= CCS_NOPARENTALIGN
;
526 if ( style
& wxTB_VERTICAL
)
529 if( style
& wxTB_BOTTOM
)
530 msStyle
|= CCS_BOTTOM
;
532 if ( style
& wxTB_RIGHT
)
533 msStyle
|= CCS_RIGHT
;
538 // ----------------------------------------------------------------------------
539 // adding/removing tools
540 // ----------------------------------------------------------------------------
542 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
543 wxToolBarToolBase
* WXUNUSED(tool
))
545 // nothing special to do here - we really create the toolbar buttons in
547 InvalidateBestSize();
551 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
553 // the main difficulty we have here is with the controls in the toolbars:
554 // as we (sometimes) use several separators to cover up the space used by
555 // them, the indices are not the same for us and the toolbar
557 // first determine the position of the first button to delete: it may be
558 // different from pos if we use several separators to cover the space used
560 wxToolBarToolsList::compatibility_iterator node
;
561 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
563 wxToolBarToolBase
*tool2
= node
->GetData();
566 // let node point to the next node in the list
567 node
= node
->GetNext();
572 if ( tool2
->IsControl() )
573 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
576 // now determine the number of buttons to delete and the area taken by them
577 size_t nButtonsToDelete
= 1;
579 // get the size of the button we're going to delete
580 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
582 int width
= r
.right
- r
.left
;
584 if ( tool
->IsControl() )
586 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
587 width
*= nButtonsToDelete
;
590 // do delete all buttons
591 m_nButtons
-= nButtonsToDelete
;
592 while ( nButtonsToDelete
-- > 0 )
594 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
596 wxLogLastError(wxT("TB_DELETEBUTTON"));
602 // and finally reposition all the controls after this button (the toolbar
603 // takes care of all normal items)
604 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
606 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
607 if ( tool2
->IsControl() )
609 tool2
->MoveBy(-width
);
613 InvalidateBestSize();
618 void wxToolBar::CreateDisabledImageList()
620 if (m_disabledImgList
!= NULL
)
622 delete m_disabledImgList
;
623 m_disabledImgList
= NULL
;
626 // as we can't use disabled image list with older versions of comctl32.dll,
627 // don't even bother creating it
628 if ( wxApp::GetComCtl32Version() >= 470 )
630 // search for the first disabled button img in the toolbar, if any
631 for ( wxToolBarToolsList::compatibility_iterator
632 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
634 wxToolBarToolBase
*tool
= node
->GetData();
635 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
636 if ( bmpDisabled
.Ok() )
638 m_disabledImgList
= new wxImageList
642 bmpDisabled
.GetMask() != NULL
,
649 // we don't have any disabled bitmaps
653 bool wxToolBar::Realize()
655 if ( !wxToolBarBase::Realize() )
658 const size_t nTools
= GetToolsCount();
660 #ifdef wxREMAP_BUTTON_COLOURS
661 // don't change the values of these constants, they can be set from the
662 // user code via wxSystemOptions
671 // the user-specified option overrides anything, but if it wasn't set, only
672 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
673 // much worse after remapping
674 static const wxChar
*remapOption
= wxT("msw.remap");
675 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
676 ? wxSystemOptions::GetOptionInt(remapOption
)
677 : wxDisplayDepth() <= 8 ? Remap_Buttons
680 #endif // wxREMAP_BUTTON_COLOURS
682 // delete all old buttons, if any
683 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
685 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
687 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
691 // First, add the bitmap: we use one bitmap for all toolbar buttons
692 // ----------------------------------------------------------------
694 wxToolBarToolsList::compatibility_iterator node
;
697 if ( !HasFlag(wxTB_NOICONS
) )
699 // if we already have a bitmap, we'll replace the existing one --
700 // otherwise we'll install a new one
701 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
703 const wxCoord totalBitmapWidth
= m_defaultWidth
*
704 wx_truncate_cast(wxCoord
, nTools
),
705 totalBitmapHeight
= m_defaultHeight
;
707 // Create a bitmap and copy all the tool bitmaps into it
708 wxMemoryDC dcAllButtons
;
709 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
710 dcAllButtons
.SelectObject(bitmap
);
712 #ifdef wxREMAP_BUTTON_COLOURS
713 if ( remapValue
!= Remap_TransparentBg
)
714 #endif // wxREMAP_BUTTON_COLOURS
716 // VZ: why do we hardcode grey colour for CE?
717 dcAllButtons
.SetBackground(wxBrush(
719 wxColour(0xc0, 0xc0, 0xc0)
720 #else // !__WXWINCE__
721 GetBackgroundColour()
722 #endif // __WXWINCE__/!__WXWINCE__
724 dcAllButtons
.Clear();
727 m_hBitmap
= bitmap
.GetHBITMAP();
728 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
730 #ifdef wxREMAP_BUTTON_COLOURS
731 if ( remapValue
== Remap_Bg
)
733 dcAllButtons
.SelectObject(wxNullBitmap
);
735 // Even if we're not remapping the bitmap
736 // content, we still have to remap the background.
737 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
738 totalBitmapWidth
, totalBitmapHeight
);
740 dcAllButtons
.SelectObject(bitmap
);
742 #endif // wxREMAP_BUTTON_COLOURS
744 // the button position
747 // the number of buttons (not separators)
750 CreateDisabledImageList();
751 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
753 wxToolBarToolBase
*tool
= node
->GetData();
754 if ( tool
->IsButton() )
756 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
758 const int w
= bmp
.GetWidth();
759 const int h
= bmp
.GetHeight();
763 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
764 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
766 // notice the last parameter: do use mask
767 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
771 wxFAIL_MSG( wxT("invalid tool button bitmap") );
774 // also deal with disabled bitmap if we want to use them
775 if ( m_disabledImgList
)
777 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
778 #if wxUSE_IMAGE && wxUSE_WXDIB
779 if ( !bmpDisabled
.Ok() )
781 // no disabled bitmap specified but we still need to
782 // fill the space in the image list with something, so
783 // we grey out the normal bitmap
785 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
787 #ifdef wxREMAP_BUTTON_COLOURS
788 if ( remapValue
== Remap_Buttons
)
790 // we need to have light grey background colour for
791 // MapBitmap() to work correctly
792 for ( int y
= 0; y
< h
; y
++ )
794 for ( int x
= 0; x
< w
; x
++ )
796 if ( imgGreyed
.IsTransparent(x
, y
) )
797 imgGreyed
.SetRGB(x
, y
,
799 wxLIGHT_GREY
->Green(),
800 wxLIGHT_GREY
->Blue());
804 #endif // wxREMAP_BUTTON_COLOURS
806 bmpDisabled
= wxBitmap(imgGreyed
);
808 #endif // wxUSE_IMAGE
810 #ifdef wxREMAP_BUTTON_COLOURS
811 if ( remapValue
== Remap_Buttons
)
812 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
813 #endif // wxREMAP_BUTTON_COLOURS
815 m_disabledImgList
->Add(bmpDisabled
);
818 // still inc width and number of buttons because otherwise the
819 // subsequent buttons will all be shifted which is rather confusing
820 // (and like this you'd see immediately which bitmap was bad)
826 dcAllButtons
.SelectObject(wxNullBitmap
);
828 // don't delete this HBITMAP!
829 bitmap
.SetHBITMAP(0);
831 #ifdef wxREMAP_BUTTON_COLOURS
832 if ( remapValue
== Remap_Buttons
)
834 // Map to system colours
835 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
836 totalBitmapWidth
, totalBitmapHeight
);
838 #endif // wxREMAP_BUTTON_COLOURS
840 bool addBitmap
= true;
842 if ( oldToolBarBitmap
)
844 #ifdef TB_REPLACEBITMAP
845 if ( wxApp::GetComCtl32Version() >= 400 )
847 TBREPLACEBITMAP replaceBitmap
;
848 replaceBitmap
.hInstOld
= NULL
;
849 replaceBitmap
.hInstNew
= NULL
;
850 replaceBitmap
.nIDOld
= (UINT_PTR
)oldToolBarBitmap
;
851 replaceBitmap
.nIDNew
= (UINT_PTR
)hBitmap
;
852 replaceBitmap
.nButtons
= nButtons
;
853 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
854 0, (LPARAM
) &replaceBitmap
) )
856 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
859 ::DeleteObject(oldToolBarBitmap
);
865 #endif // TB_REPLACEBITMAP
867 // we can't replace the old bitmap, so we will add another one
868 // (awfully inefficient, but what else to do?) and shift the bitmap
869 // indices accordingly
872 bitmapId
= m_nButtons
;
876 if ( addBitmap
) // no old bitmap or we can't replace it
878 TBADDBITMAP addBitmap
;
880 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
881 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
882 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
884 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
888 // disable image lists are only supported in comctl32.dll 4.70+
889 if ( wxApp::GetComCtl32Version() >= 470 )
891 HIMAGELIST hil
= m_disabledImgList
892 ? GetHimagelistOf(m_disabledImgList
)
895 // notice that we set the image list even if don't have one right
896 // now as we could have it before and need to reset it in this case
897 HIMAGELIST oldImageList
= (HIMAGELIST
)
898 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
900 // delete previous image list if any
902 ::DeleteObject(oldImageList
);
907 // Next add the buttons and separators
908 // -----------------------------------
910 wxScopedArray
<TBBUTTON
> buttons(new TBBUTTON
[nTools
]);
912 // this array will hold the indices of all controls in the toolbar
913 wxArrayInt controlIds
;
915 bool lastWasRadio
= false;
917 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
919 wxToolBarTool
*tool
= static_cast<wxToolBarTool
*>(node
->GetData());
921 // don't add separators to the vertical toolbar with old comctl32.dll
922 // versions as they didn't handle this properly
923 if ( IsVertical() && tool
->IsSeparator() &&
924 wxApp::GetComCtl32Version() <= 472 )
929 TBBUTTON
& button
= buttons
[i
];
931 wxZeroMemory(button
);
933 bool isRadio
= false;
934 switch ( tool
->GetStyle() )
936 case wxTOOL_STYLE_CONTROL
:
937 case wxTOOL_STYLE_SEPARATOR
:
938 if ( tool
->IsStretchableSpace() )
940 // we're going to modify the size of this button later and
941 // so we need a valid id for it and not wxID_SEPARATOR
942 // which is used by spacers by default
943 tool
->AllocSpacerId();
946 button
.idCommand
= tool
->GetId();
947 button
.fsState
= TBSTATE_ENABLED
;
948 button
.fsStyle
= TBSTYLE_SEP
;
951 case wxTOOL_STYLE_BUTTON
:
952 if ( !HasFlag(wxTB_NOICONS
) )
953 button
.iBitmap
= bitmapId
;
955 if ( HasFlag(wxTB_TEXT
) )
957 const wxString
& label
= tool
->GetLabel();
958 if ( !label
.empty() )
959 button
.iString
= (INT_PTR
)label
.wx_str();
962 button
.idCommand
= tool
->GetId();
964 if ( tool
->IsEnabled() )
965 button
.fsState
|= TBSTATE_ENABLED
;
966 if ( tool
->IsToggled() )
967 button
.fsState
|= TBSTATE_CHECKED
;
969 switch ( tool
->GetKind() )
972 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
976 // the first item in the radio group is checked by
977 // default to be consistent with wxGTK and the menu
979 button
.fsState
|= TBSTATE_CHECKED
;
981 if (tool
->Toggle(true))
983 DoToggleTool(tool
, true);
986 else if ( tool
->IsToggled() )
988 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
989 int prevIndex
= i
- 1;
992 TBBUTTON
& prevButton
= buttons
[prevIndex
];
993 wxToolBarToolBase
*tool
= nodePrev
->GetData();
994 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
997 if ( tool
->Toggle(false) )
998 DoToggleTool(tool
, false);
1000 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
1001 nodePrev
= nodePrev
->GetPrevious();
1010 button
.fsStyle
= TBSTYLE_CHECK
;
1014 button
.fsStyle
= TBSTYLE_BUTTON
;
1017 case wxITEM_DROPDOWN
:
1018 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1022 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
1023 button
.fsStyle
= TBSTYLE_BUTTON
;
1031 lastWasRadio
= isRadio
;
1036 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, i
, (LPARAM
)buttons
.get()) )
1038 wxLogLastError(wxT("TB_ADDBUTTONS"));
1042 // Adjust controls and stretchable spaces
1043 // --------------------------------------
1045 // adjust the controls size to fit nicely in the toolbar and compute its
1046 // total size while doing it
1047 m_totalFixedSize
= 0;
1049 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1051 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1053 const RECT r
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1055 if ( !tool
->IsControl() )
1058 m_totalFixedSize
+= r
.bottom
- r
.top
;
1060 m_totalFixedSize
+= r
.right
- r
.left
;
1067 // don't embed controls in the vertical toolbar, this doesn't look
1068 // good and wxGTK doesn't do it neither (and the code below can't
1069 // deal with this case)
1073 wxControl
* const control
= tool
->GetControl();
1074 wxStaticText
* const staticText
= tool
->GetStaticText();
1076 wxSize size
= control
->GetSize();
1077 wxSize staticTextSize
;
1080 staticTextSize
= staticText
->GetSize();
1081 staticTextSize
.y
+= 3; // margin between control and its label
1084 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1085 #ifdef TB_SETBUTTONINFO
1086 // available in headers, now check whether it is available now
1087 // (during run-time)
1088 if ( wxApp::GetComCtl32Version() >= 471 )
1090 // set the (underlying) separators width to be that of the
1093 tbbi
.cbSize
= sizeof(tbbi
);
1094 tbbi
.dwMask
= TBIF_SIZE
;
1095 tbbi
.cx
= (WORD
)size
.x
;
1096 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1097 tool
->GetId(), (LPARAM
)&tbbi
) )
1099 // the id is probably invalid?
1100 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1104 #endif // comctl32.dll 4.71
1105 // TB_SETBUTTONINFO unavailable
1107 // try adding several separators to fit the controls width
1108 int widthSep
= r
.right
- r
.left
;
1113 tbb
.fsState
= TBSTATE_ENABLED
;
1114 tbb
.fsStyle
= TBSTYLE_SEP
;
1116 size_t nSeparators
= size
.x
/ widthSep
;
1117 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1119 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1120 toolIndex
, (LPARAM
)&tbb
) )
1122 wxLogLastError(wxT("TB_INSERTBUTTON"));
1128 // remember the number of separators we used - we'd have to
1129 // delete all of them later
1130 tool
->SetSeparatorsCount(nSeparators
);
1132 // adjust the controls width to exactly cover the separators
1133 size
.x
= (nSeparators
+ 1)*widthSep
;
1134 control
->SetSize(size
.x
, wxDefaultCoord
);
1137 // position the control itself correctly vertically centering it on the
1138 // icon area of the toolbar
1139 int height
= r
.bottom
- r
.top
- staticTextSize
.y
;
1141 int diff
= height
- size
.y
;
1142 if ( diff
< 0 || !HasFlag(wxTB_TEXT
) )
1144 // not enough room for the static text
1148 // recalculate height & diff without the staticText control
1149 height
= r
.bottom
- r
.top
;
1150 diff
= height
- size
.y
;
1153 // the control is too high, resize to fit
1154 control
->SetSize(wxDefaultCoord
, height
- 2);
1159 else // enough space for both the control and the label
1165 control
->Move(r
.left
, r
.top
+ (diff
+ 1) / 2);
1168 staticText
->Move(r
.left
+ (size
.x
- staticTextSize
.x
)/2,
1169 r
.bottom
- staticTextSize
.y
);
1172 m_totalFixedSize
+= size
.x
;
1175 // the max index is the "real" number of buttons - i.e. counting even the
1176 // separators which we added just for aligning the controls
1177 m_nButtons
= toolIndex
;
1179 if ( !IsVertical() )
1181 if ( m_maxRows
== 0 )
1182 // if not set yet, only one row
1185 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1187 // if not set yet, have one column
1189 SetRows(m_nButtons
);
1192 InvalidateBestSize();
1198 void wxToolBar::UpdateStretchableSpacersSize()
1200 // we can't resize the spacers if TB_SETBUTTONINFO is not supported
1201 if ( wxApp::GetComCtl32Version() < 471 )
1204 // check if we have any stretchable spacers in the first place
1205 unsigned numSpaces
= 0;
1206 wxToolBarToolsList::compatibility_iterator node
;
1207 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1209 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1210 if ( tool
->IsStretchableSpace() )
1217 // we do, adjust their size: either distribute the extra size among them or
1218 // reduce their size if there is not enough place for all tools
1219 const int totalSize
= IsVertical() ? GetClientSize().y
: GetClientSize().x
;
1220 const int extraSize
= totalSize
- m_totalFixedSize
;
1221 const int sizeSpacer
= extraSize
> 0 ? extraSize
/ numSpaces
: 0;
1223 // the last spacer should consume all remaining space if we have too much
1224 // of it (which can be greater than sizeSpacer because of the rounding)
1225 const int sizeLastSpacer
= extraSize
> 0
1226 ? extraSize
- (numSpaces
- 1)*sizeSpacer
1229 // cumulated offset by which we need to move all the following controls to
1230 // the right: while the toolbar takes care of the normal items, we must
1231 // move the controls manually ourselves to ensure they remain at the
1235 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1237 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1239 if ( tool
->IsControl() && offset
)
1241 tool
->MoveBy(offset
);
1246 if ( !tool
->IsStretchableSpace() )
1249 const RECT rcOld
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1251 WinStruct
<TBBUTTONINFO
> tbbi
;
1252 tbbi
.dwMask
= TBIF_SIZE
;
1253 tbbi
.cx
= --numSpaces
? sizeSpacer
: sizeLastSpacer
;
1255 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1256 tool
->GetId(), (LPARAM
)&tbbi
) )
1258 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1262 // we successfully resized this one, move all the controls after it
1263 // by the corresponding amount (may be positive or negative)
1264 offset
+= tbbi
.cx
- (rcOld
.right
- rcOld
.left
);
1269 // ----------------------------------------------------------------------------
1271 // ----------------------------------------------------------------------------
1273 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
1275 // cast to signed is important as we compare this id with (signed) ints in
1276 // FindById() and without the cast we'd get a positive int from a
1277 // "negative" (i.e. > 32767) WORD
1278 const int id
= (signed short)id_
;
1280 wxToolBarToolBase
*tool
= FindById(id
);
1284 bool toggled
= false; // just to suppress warnings
1286 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1288 if ( tool
->CanBeToggled() )
1290 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1292 // ignore the event when a radio button is released, as this doesn't
1293 // seem to happen at all, and is handled otherwise
1294 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1297 tool
->Toggle(toggled
);
1298 UnToggleRadioGroup(tool
);
1301 // Without the two lines of code below, if the toolbar was repainted during
1302 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1303 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1304 // The Update() call bellow ensures that this won't happen, by repainting
1305 // invalidated areas of the toolbar immediately.
1307 // To complicate matters, the tool would be drawn in depressed state (this
1308 // code is called when mouse button is released, not pressed). That's not
1309 // ideal, having the tool pressed for the duration of OnLeftClick()
1310 // provides the user with useful visual clue that the app is busy reacting
1311 // to the event. So we manually put the tool into pressed state, handle the
1312 // event and then finally restore tool's original state.
1313 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
| TBSTATE_PRESSED
, 0));
1316 bool allowLeftClick
= OnLeftClick(id
, toggled
);
1318 // Restore the unpressed state. Enabled/toggled state might have been
1319 // changed since so take care of it.
1320 if (tool
->IsEnabled())
1321 state
|= TBSTATE_ENABLED
;
1323 state
&= ~TBSTATE_ENABLED
;
1324 if (tool
->IsToggled())
1325 state
|= TBSTATE_CHECKED
;
1327 state
&= ~TBSTATE_CHECKED
;
1328 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
, 0));
1330 // OnLeftClick() can veto the button state change - for buttons which
1331 // may be toggled only, of couse
1332 if ( !allowLeftClick
&& tool
->CanBeToggled() )
1335 tool
->Toggle(!toggled
);
1337 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1343 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1345 WXLPARAM
*WXUNUSED(result
))
1347 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1348 if ( hdr
->code
== TBN_DROPDOWN
)
1350 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1352 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tbhdr
->iItem
);
1353 if ( HandleWindowEvent(evt
) )
1355 // Event got handled, don't display default popup menu
1359 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1360 wxCHECK_MSG( tool
, false, wxT("drop down message for unknown tool") );
1362 wxMenu
* const menu
= tool
->GetDropdownMenu();
1366 // Display popup menu below button
1367 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1369 PopupMenu(menu
, r
.left
, r
.bottom
);
1375 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1378 // First check if this applies to us
1380 // the tooltips control created by the toolbar is sometimes Unicode, even
1381 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1382 UINT code
= hdr
->code
;
1383 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1386 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1387 if ( toolTipWnd
!= hdr
->hwndFrom
)
1390 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1391 int id
= (int)ttText
->hdr
.idFrom
;
1393 wxToolBarToolBase
*tool
= FindById(id
);
1395 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1397 wxUnusedVar(lParam
);
1404 // ----------------------------------------------------------------------------
1406 // ----------------------------------------------------------------------------
1408 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1410 wxToolBarBase::SetToolBitmapSize(size
);
1412 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1415 void wxToolBar::SetRows(int nRows
)
1417 if ( nRows
== m_maxRows
)
1419 // avoid resizing the frame uselessly
1423 // TRUE in wParam means to create at least as many rows, FALSE -
1426 ::SendMessage(GetHwnd(), TB_SETROWS
,
1427 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1435 // The button size is bigger than the bitmap size
1436 wxSize
wxToolBar::GetToolSize() const
1438 // TB_GETBUTTONSIZE is supported from version 4.70
1439 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1440 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1441 && !defined (__DIGITALMARS__)
1442 if ( wxApp::GetComCtl32Version() >= 470 )
1444 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1446 return wxSize(LOWORD(dw
), HIWORD(dw
));
1449 #endif // comctl32.dll 4.70+
1452 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1457 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1460 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1462 for ( ; current
; current
= current
->GetNext() )
1465 return current
->GetData();
1467 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1468 size_t separators
= tool
->GetSeparatorsCount();
1470 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1471 // otherwise, skip as many items as the separator count, plus the
1473 index
-= separators
? separators
+ 1 : 1;
1479 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1484 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1486 // MBN: when the point ( x, y ) is close to the toolbar border
1487 // TB_HITTEST returns m_nButtons ( not -1 )
1488 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1489 // it's a separator or there is no tool at all there
1492 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1493 // we don't use the dummy separators hack
1494 #ifdef TB_SETBUTTONINFO
1495 if ( wxApp::GetComCtl32Version() >= 471 )
1497 return m_tools
.Item((size_t)index
)->GetData();
1500 #endif // TB_SETBUTTONINFO
1502 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1506 void wxToolBar::UpdateSize()
1508 wxPoint pos
= GetPosition();
1509 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1510 if (pos
!= GetPosition())
1513 // In case Realize is called after the initial display (IOW the programmer
1514 // may have rebuilt the toolbar) give the frame the option of resizing the
1515 // toolbar to full width again, but only if the parent is a frame and the
1516 // toolbar is managed by the frame. Otherwise assume that some other
1517 // layout mechanism is controlling the toolbar size and leave it alone.
1518 SendSizeEventToParent();
1521 // ----------------------------------------------------------------------------
1523 // ---------------------------------------------------------------------------
1525 // get the TBSTYLE of the given toolbar window
1526 long wxToolBar::GetMSWToolbarStyle() const
1528 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1531 void wxToolBar::SetWindowStyleFlag(long style
)
1533 // the style bits whose changes force us to recreate the toolbar
1534 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1536 const long styleOld
= GetWindowStyle();
1538 wxToolBarBase::SetWindowStyleFlag(style
);
1540 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1541 // also fatal as we'd then try to recreate the toolbar when it's just being
1543 if ( GetToolsCount() &&
1544 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1546 // to remove the text labels, simply re-realizing the toolbar is enough
1547 // but I don't know of any way to add the text to an existing toolbar
1548 // other than by recreating it entirely
1553 // ----------------------------------------------------------------------------
1555 // ----------------------------------------------------------------------------
1557 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1559 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1560 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1563 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1565 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1566 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1569 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1571 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1572 // without, so we really need to delete the button and recreate it here
1573 wxFAIL_MSG( wxT("not implemented") );
1576 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1578 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1581 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1583 tool
->SetNormalBitmap(bitmap
);
1588 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1590 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1593 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1595 tool
->SetDisabledBitmap(bitmap
);
1600 // ----------------------------------------------------------------------------
1602 // ----------------------------------------------------------------------------
1604 // Responds to colour changes, and passes event on to children.
1605 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1607 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1609 // Remap the buttons
1612 // Relayout the toolbar
1613 int nrows
= m_maxRows
;
1614 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1619 // let the event propagate further
1623 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1625 if ( event
.Leaving() )
1629 OnMouseEnter(wxID_ANY
);
1637 if ( event
.RightDown() )
1639 // find the tool under the mouse
1640 wxCoord x
= 0, y
= 0;
1641 event
.GetPosition(&x
, &y
);
1643 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1644 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1652 // This handler is required to allow the toolbar to be set to a non-default
1653 // colour: for example, when it must blend in with a notebook page.
1654 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1656 RECT rect
= wxGetClientRect(GetHwnd());
1658 wxDC
*dc
= event
.GetDC();
1659 HDC hdc
= GetHdcOf(*dc
);
1662 // we may need to draw themed colour so that we appear correctly on
1663 // e.g. notebook page under XP with themes but only do it if the parent
1664 // draws themed background itself
1665 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1667 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1671 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1675 // it can also return S_FALSE which seems to simply say that it
1676 // didn't draw anything but no error really occurred
1679 wxLogApiError(wxT("DrawThemeParentBackground(toolbar)"), hr
);
1684 if ( MSWEraseRect(*dc
) )
1686 #endif // wxUSE_UXTHEME
1688 // we need to always draw our background under XP, as otherwise it doesn't
1689 // appear correctly with some themes (e.g. Zune one)
1690 if ( wxGetWinVersion() == wxWinVersion_XP
||
1691 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1693 // do draw our background
1695 // notice that this 'dumb' implementation may cause flicker for some of
1696 // the controls in which case they should intercept wxEraseEvent and
1697 // process it themselves somehow
1698 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1700 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1701 ::FillRect(hdc
, &rect
, hBrush
);
1703 else // we have no non-default background colour
1705 // let the system do it for us
1710 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1712 // wait until we have some tools
1713 if ( !GetToolsCount() )
1716 // calculate our minor dimension ourselves - we're confusing the standard
1717 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1718 const RECT r
= wxGetTBItemRect(GetHwnd(), 0);
1726 w
= r
.right
- r
.left
;
1729 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1736 if (HasFlag( wxTB_FLAT
))
1737 h
= r
.bottom
- r
.top
- 3;
1739 h
= r
.bottom
- r
.top
;
1742 // FIXME: hardcoded separator line height...
1743 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1748 if ( MAKELPARAM(w
, h
) != lParam
)
1750 // size really changed
1754 UpdateStretchableSpacersSize();
1756 // message processed
1762 bool wxToolBar::MSWEraseRect(wxDC
& dc
, const wxRect
*rectItem
)
1764 // erase the given rectangle to hide the separator
1766 // themed background doesn't look well under XP so only draw it for Vista
1768 if ( !UseBgCol() && wxGetWinVersion() >= wxWinVersion_Vista
)
1770 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1773 wxUxThemeHandle
hTheme(this, L
"REBAR");
1775 // Draw the whole background since the pattern may be position
1776 // sensitive; but clip it to the area of interest.
1778 wxCopyRectToRECT(GetClientSize(), rcTotal
);
1782 wxCopyRectToRECT(*rectItem
, rcItem
);
1784 HRESULT hr
= theme
->DrawThemeBackground
1790 rectItem
? &rcItem
: NULL
1795 // it can also return S_FALSE which seems to simply say that it
1796 // didn't draw anything but no error really occurred
1799 wxLogApiError(wxT("DrawThemeBackground(toolbar)"), hr
);
1803 #endif // wxUSE_UXTHEME
1805 // this is a bit peculiar but we may simply do nothing here if no rectItem
1806 // is specified (and hence we need to erase everything) as this only
1807 // happens when we're called from OnEraseBackground() and in this case we
1808 // may simply return false to let the systems default background erasing to
1811 dc
.DrawRectangle(*rectItem
);
1816 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1818 // erase any dummy separators which were used only for reserving space in
1819 // the toolbar (either for a control or just for a stretchable space)
1821 // first of all, are there any controls at all?
1822 wxToolBarToolsList::compatibility_iterator node
;
1823 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1825 wxToolBarToolBase
* const tool
= node
->GetData();
1826 if ( tool
->IsControl() || tool
->IsStretchableSpace() )
1832 // no controls, nothing to erase
1836 // prepare the DC on which we'll be drawing
1837 wxClientDC
dc(this);
1838 dc
.SetBrush(GetBackgroundColour());
1839 dc
.SetPen(*wxTRANSPARENT_PEN
);
1842 if ( !::GetUpdateRect(GetHwnd(), &rcUpdate
, FALSE
) )
1844 // nothing to redraw anyhow
1848 const wxRect rectUpdate
= wxRectFromRECT(rcUpdate
);
1849 dc
.SetClippingRegion(rectUpdate
);
1851 // draw the toolbar tools, separators &c normally
1852 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1854 // for each control in the toolbar find all the separators intersecting it
1857 // NB: this is really the only way to do it as we don't know if a separator
1858 // corresponds to a control (i.e. is a dummy one) or a real one
1861 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1863 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1864 if ( tool
->IsControl() )
1866 // get the control rect in our client coords
1867 wxControl
*control
= tool
->GetControl();
1868 wxStaticText
*staticText
= tool
->GetStaticText();
1869 wxRect rectCtrl
= control
->GetRect();
1870 wxRect rectStaticText
;
1872 rectStaticText
= staticText
->GetRect();
1874 if ( !rectCtrl
.Intersects(rectUpdate
) &&
1875 (!staticText
|| !rectStaticText
.Intersects(rectUpdate
)) )
1878 // iterate over all buttons to find all separators intersecting
1881 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1882 for ( int n
= 0; n
< count
; n
++ )
1884 // is it a separator?
1885 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1888 wxLogDebug(wxT("TB_GETBUTTON failed?"));
1893 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1896 // get the bounding rect of the separator
1897 RECT r
= wxGetTBItemRect(GetHwnd(), n
);
1901 const wxRect rectItem
= wxRectFromRECT(r
);
1903 // does it intersect the update region at all?
1904 if ( !rectUpdate
.Intersects(rectItem
) )
1907 // does it intersect the control itself or its label?
1909 // if it does, refresh it so it's redrawn on top of the
1911 if ( rectCtrl
.Intersects(rectItem
) )
1912 control
->Refresh(false);
1913 else if ( staticText
&& rectStaticText
.Intersects(rectItem
) )
1914 staticText
->Refresh(false);
1918 MSWEraseRect(dc
, &rectItem
);
1921 else if ( tool
->IsStretchableSpace() )
1924 rectItem
= wxRectFromRECT(wxGetTBItemRect(GetHwnd(), toolIndex
));
1926 if ( rectUpdate
.Intersects(rectItem
) )
1927 MSWEraseRect(dc
, &rectItem
);
1933 #endif // __WXWINCE__
1935 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1937 wxCoord x
= GET_X_LPARAM(lParam
),
1938 y
= GET_Y_LPARAM(lParam
);
1939 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1941 // has the current tool changed?
1942 if ( tool
!= m_pInTool
)
1945 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1949 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1954 // we don't handle mouse moves, so always pass the message to
1955 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1956 HandleMouseMove(wParam
, lParam
);
1960 if ( HandleSize(wParam
, lParam
) )
1966 // refreshing the controls in the toolbar inside a composite window
1967 // results in an endless stream of WM_PAINT messages -- and seems
1968 // to be unnecessary anyhow as everything works just fine without
1969 // any special workarounds in this case
1970 if ( !IsDoubleBuffered() && HandlePaint(wParam
, lParam
) )
1973 #endif // __WXWINCE__
1976 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1979 // ----------------------------------------------------------------------------
1980 // private functions
1981 // ----------------------------------------------------------------------------
1983 #ifdef wxREMAP_BUTTON_COLOURS
1985 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1991 wxLogLastError(wxT("CreateCompatibleDC"));
1996 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
2000 wxLogLastError(wxT("SelectObject"));
2005 wxCOLORMAP
*cmap
= wxGetStdColourMap();
2007 for ( int i
= 0; i
< width
; i
++ )
2009 for ( int j
= 0; j
< height
; j
++ )
2011 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
2013 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
2015 COLORREF col
= cmap
[k
].from
;
2016 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
2017 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
2018 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
2020 if ( cmap
[k
].to
!= pixel
)
2021 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
2031 #endif // wxREMAP_BUTTON_COLOURS
2033 #endif // wxUSE_TOOLBAR