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/sysopt.h"
47 #include "wx/dcclient.h"
49 #include "wx/msw/private.h"
50 #include "wx/msw/dc.h"
53 #include "wx/msw/uxtheme.h"
56 // this define controls whether the code for button colours remapping (only
57 // useful for 16 or 256 colour images) is active at all, it's always turned off
58 // for CE where it doesn't compile (and is probably not needed anyhow) and may
59 // also be turned off for other systems if you always use 24bpp images and so
62 #define wxREMAP_BUTTON_COLOURS
63 #endif // !__WXWINCE__
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // these standard constants are not always defined in compilers headers
73 #define TBSTYLE_LIST 0x1000
74 #define TBSTYLE_FLAT 0x0800
77 #ifndef TBSTYLE_TRANSPARENT
78 #define TBSTYLE_TRANSPARENT 0x8000
81 #ifndef TBSTYLE_TOOLTIPS
82 #define TBSTYLE_TOOLTIPS 0x0100
87 #define TB_SETSTYLE (WM_USER + 56)
88 #define TB_GETSTYLE (WM_USER + 57)
92 #define TB_HITTEST (WM_USER + 69)
96 #define TB_GETMAXSIZE (WM_USER + 83)
99 // these values correspond to those used by comctl32.dll
100 #define DEFAULTBITMAPX 16
101 #define DEFAULTBITMAPY 15
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
119 style ( wxNO_BORDER | wxTB_HORIZONTAL)
128 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
129 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
130 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
131 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 class wxToolBarTool
: public wxToolBarToolBase
141 wxToolBarTool(wxToolBar
*tbar
,
143 const wxString
& label
,
144 const wxBitmap
& bmpNormal
,
145 const wxBitmap
& bmpDisabled
,
147 wxObject
*clientData
,
148 const wxString
& shortHelp
,
149 const wxString
& longHelp
)
150 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
151 clientData
, shortHelp
, longHelp
)
157 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
158 : wxToolBarToolBase(tbar
, control
, label
)
160 if ( IsControl() && !m_label
.empty() )
162 // create a control to render the control's label
163 m_staticText
= new wxStaticText
170 wxALIGN_CENTRE
| wxST_NO_AUTORESIZE
181 virtual ~wxToolBarTool()
186 virtual void SetLabel(const wxString
& label
)
188 if ( label
== m_label
)
191 wxToolBarToolBase::SetLabel(label
);
194 m_staticText
->SetLabel(label
);
196 // we need to update the label shown in the toolbar because it has a
197 // pointer to the internal buffer of the old label
199 // TODO: use TB_SETBUTTONINFO
202 wxStaticText
* GetStaticText()
204 wxASSERT_MSG( IsControl(),
205 _T("only makes sense for embedded control tools") );
210 // set/get the number of separators which we use to cover the space used by
211 // a control in the toolbar
212 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
213 size_t GetSeparatorsCount() const { return m_nSepCount
; }
217 wxStaticText
*m_staticText
;
219 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 // return the rectangle of the item at the given index
228 // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
229 // r.right or r.bottom with 0 to check for this
230 static RECT
wxGetTBItemRect(HWND hwnd
, int index
)
234 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
235 // only appeared in v4.70 of comctl32.dll
236 if ( !::SendMessage(hwnd
, TB_GETITEMRECT
, index
, (LPARAM
)&r
) )
238 wxLogLastError(wxT("TB_GETITEMRECT"));
249 // ============================================================================
251 // ============================================================================
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
258 const wxString
& label
,
259 const wxBitmap
& bmpNormal
,
260 const wxBitmap
& bmpDisabled
,
262 wxObject
*clientData
,
263 const wxString
& shortHelp
,
264 const wxString
& longHelp
)
266 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
267 clientData
, shortHelp
, longHelp
);
271 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
273 return new wxToolBarTool(this, control
, label
);
276 // ----------------------------------------------------------------------------
277 // wxToolBar construction
278 // ----------------------------------------------------------------------------
280 void wxToolBar::Init()
283 m_disabledImgList
= NULL
;
287 m_defaultWidth
= DEFAULTBITMAPX
;
288 m_defaultHeight
= DEFAULTBITMAPY
;
293 bool wxToolBar::Create(wxWindow
*parent
,
298 const wxString
& name
)
300 // common initialisation
301 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
306 // MSW-specific initialisation
307 if ( !MSWCreateToolbar(pos
, size
) )
310 wxSetCCUnicodeFormat(GetHwnd());
312 // workaround for flat toolbar on Windows XP classic style: we have to set
313 // the style after creating the control; doing it at creation time doesn't work
315 if ( style
& wxTB_FLAT
)
317 LRESULT style
= GetMSWToolbarStyle();
319 if ( !(style
& TBSTYLE_FLAT
) )
320 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
322 #endif // wxUSE_UXTHEME
327 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
329 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
332 // toolbar-specific post initialisation
333 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
335 #ifdef TB_SETEXTENDEDSTYLE
336 if ( wxApp::GetComCtl32Version() >= 471 )
337 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE
, 0, TBSTYLE_EX_DRAWDDARROWS
);
343 void wxToolBar::Recreate()
345 const HWND hwndOld
= GetHwnd();
348 // we haven't been created yet, no need to recreate
352 // get the position and size before unsubclassing the old toolbar
353 const wxPoint pos
= GetPosition();
354 const wxSize size
= GetSize();
358 if ( !MSWCreateToolbar(pos
, size
) )
361 wxFAIL_MSG( _T("recreating the toolbar failed") );
366 // reparent all our children under the new toolbar
367 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
369 node
= node
->GetNext() )
371 wxWindow
*win
= node
->GetData();
372 if ( !win
->IsTopLevel() )
373 ::SetParent(GetHwndOf(win
), GetHwnd());
376 // only destroy the old toolbar now --
377 // after all the children had been reparented
378 ::DestroyWindow(hwndOld
);
380 // it is for the old bitmap control and can't be used with the new one
383 ::DeleteObject((HBITMAP
) m_hBitmap
);
387 if ( m_disabledImgList
)
389 delete m_disabledImgList
;
390 m_disabledImgList
= NULL
;
396 wxToolBar::~wxToolBar()
398 // we must refresh the frame size when the toolbar is deleted but the frame
399 // is not - otherwise toolbar leaves a hole in the place it used to occupy
400 SendSizeEventToParent();
403 ::DeleteObject((HBITMAP
) m_hBitmap
);
405 delete m_disabledImgList
;
408 wxSize
wxToolBar::DoGetBestSize() const
413 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
415 // maybe an old (< 0x400) Windows version? try to approximate the
416 // toolbar size ourselves
417 sizeBest
= GetToolSize();
418 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
419 sizeBest
.x
*= GetToolsCount();
421 // reverse horz and vertical components if necessary
425 sizeBest
.x
= sizeBest
.y
;
429 else // TB_GETMAXSIZE succeeded
431 // but it could still return an incorrect result due to what appears to
432 // be a bug in old comctl32.dll versions which don't handle controls in
433 // the toolbar correctly, so work around it (see SF patch 1902358)
434 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
436 // calculate the toolbar width in alternative way
437 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
438 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
440 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
441 if ( widthAlt
> size
.cx
)
445 sizeBest
.x
= size
.cx
;
446 sizeBest
.y
= size
.cy
;
451 // Without the extra height, DoGetBestSize can report a size that's
452 // smaller than the actual window, causing windows to overlap slightly
453 // in some circumstances, leading to missing borders (especially noticeable
455 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
460 CacheBestSize(sizeBest
);
465 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
467 // toolbars never have border, giving one to them results in broken
469 WXDWORD msStyle
= wxControl::MSWGetStyle
471 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
474 if ( !(style
& wxTB_NO_TOOLTIPS
) )
475 msStyle
|= TBSTYLE_TOOLTIPS
;
477 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
479 // static as it doesn't change during the program lifetime
480 static const int s_verComCtl
= wxApp::GetComCtl32Version();
482 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
483 // style with 6.00 (part of Windows XP) leads to the toolbar with
484 // incorrect background colour - and not using it still results in the
485 // correct (flat) toolbar, so don't use it there
486 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
487 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
489 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
490 msStyle
|= TBSTYLE_LIST
;
493 if ( style
& wxTB_NODIVIDER
)
494 msStyle
|= CCS_NODIVIDER
;
496 if ( style
& wxTB_NOALIGN
)
497 msStyle
|= CCS_NOPARENTALIGN
;
499 if ( style
& wxTB_VERTICAL
)
502 if( style
& wxTB_BOTTOM
)
503 msStyle
|= CCS_BOTTOM
;
505 if ( style
& wxTB_RIGHT
)
506 msStyle
|= CCS_RIGHT
;
511 // ----------------------------------------------------------------------------
512 // adding/removing tools
513 // ----------------------------------------------------------------------------
515 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
516 wxToolBarToolBase
* WXUNUSED(tool
))
518 // nothing special to do here - we really create the toolbar buttons in
520 InvalidateBestSize();
524 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
526 // the main difficulty we have here is with the controls in the toolbars:
527 // as we (sometimes) use several separators to cover up the space used by
528 // them, the indices are not the same for us and the toolbar
530 // first determine the position of the first button to delete: it may be
531 // different from pos if we use several separators to cover the space used
533 wxToolBarToolsList::compatibility_iterator node
;
534 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
536 wxToolBarToolBase
*tool2
= node
->GetData();
539 // let node point to the next node in the list
540 node
= node
->GetNext();
545 if ( tool2
->IsControl() )
546 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
549 // now determine the number of buttons to delete and the area taken by them
550 size_t nButtonsToDelete
= 1;
552 // get the size of the button we're going to delete
553 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
555 int width
= r
.right
- r
.left
;
557 if ( tool
->IsControl() )
559 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
560 width
*= nButtonsToDelete
;
563 // do delete all buttons
564 m_nButtons
-= nButtonsToDelete
;
565 while ( nButtonsToDelete
-- > 0 )
567 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
569 wxLogLastError(wxT("TB_DELETEBUTTON"));
575 // and finally reposition all the controls after this button (the toolbar
576 // takes care of all normal items)
577 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
579 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
580 if ( tool2
->IsControl() )
582 wxControl
* const control
= tool2
->GetControl();
585 control
->GetPosition(&x
, NULL
);
586 control
->Move(x
- width
, wxDefaultCoord
);
588 wxStaticText
* const staticText
= tool2
->GetStaticText();
590 staticText
->Move(x
- width
, wxDefaultCoord
);
594 InvalidateBestSize();
599 void wxToolBar::CreateDisabledImageList()
601 if (m_disabledImgList
!= NULL
)
603 delete m_disabledImgList
;
604 m_disabledImgList
= NULL
;
607 // as we can't use disabled image list with older versions of comctl32.dll,
608 // don't even bother creating it
609 if ( wxApp::GetComCtl32Version() >= 470 )
611 // search for the first disabled button img in the toolbar, if any
612 for ( wxToolBarToolsList::compatibility_iterator
613 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
615 wxToolBarToolBase
*tool
= node
->GetData();
616 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
617 if ( bmpDisabled
.Ok() )
619 m_disabledImgList
= new wxImageList
623 bmpDisabled
.GetMask() != NULL
,
630 // we don't have any disabled bitmaps
634 bool wxToolBar::Realize()
636 const size_t nTools
= GetToolsCount();
641 #ifdef wxREMAP_BUTTON_COLOURS
642 // don't change the values of these constants, they can be set from the
643 // user code via wxSystemOptions
652 // the user-specified option overrides anything, but if it wasn't set, only
653 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
654 // much worse after remapping
655 static const wxChar
*remapOption
= wxT("msw.remap");
656 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
657 ? wxSystemOptions::GetOptionInt(remapOption
)
658 : wxDisplayDepth() <= 8 ? Remap_Buttons
661 #endif // wxREMAP_BUTTON_COLOURS
663 // delete all old buttons, if any
664 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
666 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
668 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
672 // First, add the bitmap: we use one bitmap for all toolbar buttons
673 // ----------------------------------------------------------------
675 wxToolBarToolsList::compatibility_iterator node
;
679 if ( HasFlag(wxTB_NOICONS
) )
681 // no icons, don't leave space for them
685 else // do show icons
687 // if we already have a bitmap, we'll replace the existing one --
688 // otherwise we'll install a new one
689 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
691 sizeBmp
.x
= m_defaultWidth
;
692 sizeBmp
.y
= m_defaultHeight
;
694 const wxCoord totalBitmapWidth
= m_defaultWidth
*
695 wx_truncate_cast(wxCoord
, nTools
),
696 totalBitmapHeight
= m_defaultHeight
;
698 // Create a bitmap and copy all the tool bitmaps into it
699 wxMemoryDC dcAllButtons
;
700 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
701 dcAllButtons
.SelectObject(bitmap
);
703 #ifdef wxREMAP_BUTTON_COLOURS
704 if ( remapValue
!= Remap_TransparentBg
)
705 #endif // wxREMAP_BUTTON_COLOURS
707 // VZ: why do we hardcode grey colour for CE?
708 dcAllButtons
.SetBackground(wxBrush(
710 wxColour(0xc0, 0xc0, 0xc0)
711 #else // !__WXWINCE__
712 GetBackgroundColour()
713 #endif // __WXWINCE__/!__WXWINCE__
715 dcAllButtons
.Clear();
718 m_hBitmap
= bitmap
.GetHBITMAP();
719 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
721 #ifdef wxREMAP_BUTTON_COLOURS
722 if ( remapValue
== Remap_Bg
)
724 dcAllButtons
.SelectObject(wxNullBitmap
);
726 // Even if we're not remapping the bitmap
727 // content, we still have to remap the background.
728 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
729 totalBitmapWidth
, totalBitmapHeight
);
731 dcAllButtons
.SelectObject(bitmap
);
733 #endif // wxREMAP_BUTTON_COLOURS
735 // the button position
738 // the number of buttons (not separators)
741 CreateDisabledImageList();
742 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
744 wxToolBarToolBase
*tool
= node
->GetData();
745 if ( tool
->IsButton() )
747 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
749 const int w
= bmp
.GetWidth();
750 const int h
= bmp
.GetHeight();
754 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
755 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
757 // notice the last parameter: do use mask
758 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
762 wxFAIL_MSG( _T("invalid tool button bitmap") );
765 // also deal with disabled bitmap if we want to use them
766 if ( m_disabledImgList
)
768 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
769 #if wxUSE_IMAGE && wxUSE_WXDIB
770 if ( !bmpDisabled
.Ok() )
772 // no disabled bitmap specified but we still need to
773 // fill the space in the image list with something, so
774 // we grey out the normal bitmap
776 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
778 #ifdef wxREMAP_BUTTON_COLOURS
779 if ( remapValue
== Remap_Buttons
)
781 // we need to have light grey background colour for
782 // MapBitmap() to work correctly
783 for ( int y
= 0; y
< h
; y
++ )
785 for ( int x
= 0; x
< w
; x
++ )
787 if ( imgGreyed
.IsTransparent(x
, y
) )
788 imgGreyed
.SetRGB(x
, y
,
790 wxLIGHT_GREY
->Green(),
791 wxLIGHT_GREY
->Blue());
795 #endif // wxREMAP_BUTTON_COLOURS
797 bmpDisabled
= wxBitmap(imgGreyed
);
799 #endif // wxUSE_IMAGE
801 #ifdef wxREMAP_BUTTON_COLOURS
802 if ( remapValue
== Remap_Buttons
)
803 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
804 #endif // wxREMAP_BUTTON_COLOURS
806 m_disabledImgList
->Add(bmpDisabled
);
809 // still inc width and number of buttons because otherwise the
810 // subsequent buttons will all be shifted which is rather confusing
811 // (and like this you'd see immediately which bitmap was bad)
817 dcAllButtons
.SelectObject(wxNullBitmap
);
819 // don't delete this HBITMAP!
820 bitmap
.SetHBITMAP(0);
822 #ifdef wxREMAP_BUTTON_COLOURS
823 if ( remapValue
== Remap_Buttons
)
825 // Map to system colours
826 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
827 totalBitmapWidth
, totalBitmapHeight
);
829 #endif // wxREMAP_BUTTON_COLOURS
831 bool addBitmap
= true;
833 if ( oldToolBarBitmap
)
835 #ifdef TB_REPLACEBITMAP
836 if ( wxApp::GetComCtl32Version() >= 400 )
838 TBREPLACEBITMAP replaceBitmap
;
839 replaceBitmap
.hInstOld
= NULL
;
840 replaceBitmap
.hInstNew
= NULL
;
841 replaceBitmap
.nIDOld
= (UINT_PTR
)oldToolBarBitmap
;
842 replaceBitmap
.nIDNew
= (UINT_PTR
)hBitmap
;
843 replaceBitmap
.nButtons
= nButtons
;
844 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
845 0, (LPARAM
) &replaceBitmap
) )
847 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
850 ::DeleteObject(oldToolBarBitmap
);
856 #endif // TB_REPLACEBITMAP
858 // we can't replace the old bitmap, so we will add another one
859 // (awfully inefficient, but what else to do?) and shift the bitmap
860 // indices accordingly
863 bitmapId
= m_nButtons
;
867 if ( addBitmap
) // no old bitmap or we can't replace it
869 TBADDBITMAP addBitmap
;
871 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
872 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
873 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
875 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
879 // disable image lists are only supported in comctl32.dll 4.70+
880 if ( wxApp::GetComCtl32Version() >= 470 )
882 HIMAGELIST hil
= m_disabledImgList
883 ? GetHimagelistOf(m_disabledImgList
)
886 // notice that we set the image list even if don't have one right
887 // now as we could have it before and need to reset it in this case
888 HIMAGELIST oldImageList
= (HIMAGELIST
)
889 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
891 // delete previous image list if any
893 ::DeleteObject(oldImageList
);
897 // don't call SetToolBitmapSize() as we don't want to change the values of
898 // m_defaultWidth/Height
899 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
900 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
902 wxLogLastError(_T("TB_SETBITMAPSIZE"));
905 // Next add the buttons and separators
906 // -----------------------------------
908 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
910 // this array will hold the indices of all controls in the toolbar
911 wxArrayInt controlIds
;
913 bool lastWasRadio
= false;
915 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
917 wxToolBarToolBase
*tool
= node
->GetData();
919 // don't add separators to the vertical toolbar with old comctl32.dll
920 // versions as they didn't handle this properly
921 if ( IsVertical() && tool
->IsSeparator() &&
922 wxApp::GetComCtl32Version() <= 472 )
927 TBBUTTON
& button
= buttons
[i
];
929 wxZeroMemory(button
);
931 bool isRadio
= false;
932 switch ( tool
->GetStyle() )
934 case wxTOOL_STYLE_CONTROL
:
935 button
.idCommand
= tool
->GetId();
936 // fall through: create just a separator too
938 case wxTOOL_STYLE_SEPARATOR
:
939 button
.fsState
= TBSTATE_ENABLED
;
940 button
.fsStyle
= TBSTYLE_SEP
;
943 case wxTOOL_STYLE_BUTTON
:
944 if ( !HasFlag(wxTB_NOICONS
) )
945 button
.iBitmap
= bitmapId
;
947 if ( HasFlag(wxTB_TEXT
) )
949 const wxString
& label
= tool
->GetLabel();
950 if ( !label
.empty() )
951 button
.iString
= (INT_PTR
)label
.wx_str();
954 button
.idCommand
= tool
->GetId();
956 if ( tool
->IsEnabled() )
957 button
.fsState
|= TBSTATE_ENABLED
;
958 if ( tool
->IsToggled() )
959 button
.fsState
|= TBSTATE_CHECKED
;
961 switch ( tool
->GetKind() )
964 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
968 // the first item in the radio group is checked by
969 // default to be consistent with wxGTK and the menu
971 button
.fsState
|= TBSTATE_CHECKED
;
973 if (tool
->Toggle(true))
975 DoToggleTool(tool
, true);
978 else if ( tool
->IsToggled() )
980 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
981 int prevIndex
= i
- 1;
984 TBBUTTON
& prevButton
= buttons
[prevIndex
];
985 wxToolBarToolBase
*tool
= nodePrev
->GetData();
986 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
989 if ( tool
->Toggle(false) )
990 DoToggleTool(tool
, false);
992 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
993 nodePrev
= nodePrev
->GetPrevious();
1002 button
.fsStyle
= TBSTYLE_CHECK
;
1006 button
.fsStyle
= TBSTYLE_BUTTON
;
1009 case wxITEM_DROPDOWN
:
1010 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1014 wxFAIL_MSG( _T("unexpected toolbar button kind") );
1015 button
.fsStyle
= TBSTYLE_BUTTON
;
1023 lastWasRadio
= isRadio
;
1028 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
1030 wxLogLastError(wxT("TB_ADDBUTTONS"));
1035 // Deal with the controls finally
1036 // ------------------------------
1038 // adjust the controls size to fit nicely in the toolbar
1041 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
1043 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1045 // we calculate the running y coord for vertical toolbars so we need to
1046 // get the items size for all items but for the horizontal ones we
1047 // don't need to deal with the non controls
1048 bool isControl
= tool
->IsControl();
1049 if ( !isControl
&& !IsVertical() )
1052 const RECT r
= wxGetTBItemRect(GetHwnd(), index
);
1055 // can only be control if isVertical
1056 y
+= r
.bottom
- r
.top
;
1061 wxControl
*control
= tool
->GetControl();
1062 wxStaticText
* const staticText
= tool
->GetStaticText();
1064 wxSize size
= control
->GetSize();
1065 wxSize staticTextSize
;
1068 staticTextSize
= staticText
->GetSize();
1069 staticTextSize
.y
+= 3; // margin between control and its label
1072 // the position of the leftmost controls corner
1073 int left
= wxDefaultCoord
;
1075 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1076 #ifdef TB_SETBUTTONINFO
1077 // available in headers, now check whether it is available now
1078 // (during run-time)
1079 if ( wxApp::GetComCtl32Version() >= 471 )
1081 // set the (underlying) separators width to be that of the
1084 tbbi
.cbSize
= sizeof(tbbi
);
1085 tbbi
.dwMask
= TBIF_SIZE
;
1086 tbbi
.cx
= (WORD
)size
.x
;
1087 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1088 tool
->GetId(), (LPARAM
)&tbbi
) )
1090 // the id is probably invalid?
1091 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1095 #endif // comctl32.dll 4.71
1096 // TB_SETBUTTONINFO unavailable
1098 // try adding several separators to fit the controls width
1099 int widthSep
= r
.right
- r
.left
;
1105 tbb
.fsState
= TBSTATE_ENABLED
;
1106 tbb
.fsStyle
= TBSTYLE_SEP
;
1108 size_t nSeparators
= size
.x
/ widthSep
;
1109 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1111 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1112 index
, (LPARAM
)&tbb
) )
1114 wxLogLastError(wxT("TB_INSERTBUTTON"));
1120 // remember the number of separators we used - we'd have to
1121 // delete all of them later
1122 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1124 // adjust the controls width to exactly cover the separators
1125 size
.x
= (nSeparators
+ 1)*widthSep
;
1126 control
->SetSize(size
.x
, wxDefaultCoord
);
1129 // position the control itself correctly vertically centering it on the
1130 // icon area of the toolbar
1131 int height
= r
.bottom
- r
.top
- staticTextSize
.y
;
1133 int diff
= height
- size
.y
;
1134 if ( diff
< 0 || !HasFlag(wxTB_TEXT
) )
1136 // not enough room for the static text
1140 // recalculate height & diff without the staticText control
1141 height
= r
.bottom
- r
.top
;
1142 diff
= height
- size
.y
;
1145 // the control is too high, resize to fit
1146 control
->SetSize(wxDefaultCoord
, height
- 2);
1151 else // enough space for both the control and the label
1163 y
+= height
+ 2 * GetMargins().y
;
1165 else // horizontal toolbar
1167 if ( left
== wxDefaultCoord
)
1173 control
->Move(left
, top
+ (diff
+ 1) / 2);
1176 staticText
->Move(left
+ (size
.x
- staticTextSize
.x
)/2,
1177 r
.bottom
- staticTextSize
.y
);
1181 // the max index is the "real" number of buttons - i.e. counting even the
1182 // separators which we added just for aligning the controls
1185 if ( !IsVertical() )
1187 if ( m_maxRows
== 0 )
1188 // if not set yet, only one row
1191 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1193 // if not set yet, have one column
1195 SetRows(m_nButtons
);
1198 InvalidateBestSize();
1204 // ----------------------------------------------------------------------------
1206 // ----------------------------------------------------------------------------
1208 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
1210 // cast to signed is important as we compare this id with (signed) ints in
1211 // FindById() and without the cast we'd get a positive int from a
1212 // "negative" (i.e. > 32767) WORD
1213 const int id
= (signed short)id_
;
1215 wxToolBarToolBase
*tool
= FindById(id
);
1219 bool toggled
= false; // just to suppress warnings
1221 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1223 if ( tool
->CanBeToggled() )
1225 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1227 // ignore the event when a radio button is released, as this doesn't
1228 // seem to happen at all, and is handled otherwise
1229 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1232 tool
->Toggle(toggled
);
1233 UnToggleRadioGroup(tool
);
1236 // Without the two lines of code below, if the toolbar was repainted during
1237 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1238 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1239 // The Update() call bellow ensures that this won't happen, by repainting
1240 // invalidated areas of the toolbar immediately.
1242 // To complicate matters, the tool would be drawn in depressed state (this
1243 // code is called when mouse button is released, not pressed). That's not
1244 // ideal, having the tool pressed for the duration of OnLeftClick()
1245 // provides the user with useful visual clue that the app is busy reacting
1246 // to the event. So we manually put the tool into pressed state, handle the
1247 // event and then finally restore tool's original state.
1248 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
| TBSTATE_PRESSED
, 0));
1251 bool allowLeftClick
= OnLeftClick(id
, toggled
);
1253 // Restore the unpressed state. Enabled/toggled state might have been
1254 // changed since so take care of it.
1255 if (tool
->IsEnabled())
1256 state
|= TBSTATE_ENABLED
;
1258 state
&= ~TBSTATE_ENABLED
;
1259 if (tool
->IsToggled())
1260 state
|= TBSTATE_CHECKED
;
1262 state
&= ~TBSTATE_CHECKED
;
1263 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
, 0));
1265 // OnLeftClick() can veto the button state change - for buttons which
1266 // may be toggled only, of couse
1267 if ( !allowLeftClick
&& tool
->CanBeToggled() )
1270 tool
->Toggle(!toggled
);
1272 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1278 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1280 WXLPARAM
*WXUNUSED(result
))
1282 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1283 if ( hdr
->code
== TBN_DROPDOWN
)
1285 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1287 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tbhdr
->iItem
);
1288 if ( HandleWindowEvent(evt
) )
1290 // Event got handled, don't display default popup menu
1294 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1295 wxCHECK_MSG( tool
, false, _T("drop down message for unknown tool") );
1297 wxMenu
* const menu
= tool
->GetDropdownMenu();
1301 // Display popup menu below button
1302 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1304 PopupMenu(menu
, r
.left
, r
.bottom
);
1310 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1313 // First check if this applies to us
1315 // the tooltips control created by the toolbar is sometimes Unicode, even
1316 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1317 UINT code
= hdr
->code
;
1318 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1321 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1322 if ( toolTipWnd
!= hdr
->hwndFrom
)
1325 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1326 int id
= (int)ttText
->hdr
.idFrom
;
1328 wxToolBarToolBase
*tool
= FindById(id
);
1330 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1332 wxUnusedVar(lParam
);
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1343 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1345 wxToolBarBase::SetToolBitmapSize(size
);
1347 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1350 void wxToolBar::SetRows(int nRows
)
1352 if ( nRows
== m_maxRows
)
1354 // avoid resizing the frame uselessly
1358 // TRUE in wParam means to create at least as many rows, FALSE -
1361 ::SendMessage(GetHwnd(), TB_SETROWS
,
1362 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1370 // The button size is bigger than the bitmap size
1371 wxSize
wxToolBar::GetToolSize() const
1373 // TB_GETBUTTONSIZE is supported from version 4.70
1374 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1375 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1376 && !defined (__DIGITALMARS__)
1377 if ( wxApp::GetComCtl32Version() >= 470 )
1379 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1381 return wxSize(LOWORD(dw
), HIWORD(dw
));
1384 #endif // comctl32.dll 4.70+
1387 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1392 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1395 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1397 for ( ; current
; current
= current
->GetNext() )
1400 return current
->GetData();
1402 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1403 size_t separators
= tool
->GetSeparatorsCount();
1405 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1406 // otherwise, skip as many items as the separator count, plus the
1408 index
-= separators
? separators
+ 1 : 1;
1414 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1419 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1421 // MBN: when the point ( x, y ) is close to the toolbar border
1422 // TB_HITTEST returns m_nButtons ( not -1 )
1423 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1424 // it's a separator or there is no tool at all there
1425 return (wxToolBarToolBase
*)NULL
;
1427 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1428 // we don't use the dummy separators hack
1429 #ifdef TB_SETBUTTONINFO
1430 if ( wxApp::GetComCtl32Version() >= 471 )
1432 return m_tools
.Item((size_t)index
)->GetData();
1435 #endif // TB_SETBUTTONINFO
1437 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1441 void wxToolBar::UpdateSize()
1443 wxPoint pos
= GetPosition();
1444 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1445 if (pos
!= GetPosition())
1448 // In case Realize is called after the initial display (IOW the programmer
1449 // may have rebuilt the toolbar) give the frame the option of resizing the
1450 // toolbar to full width again, but only if the parent is a frame and the
1451 // toolbar is managed by the frame. Otherwise assume that some other
1452 // layout mechanism is controlling the toolbar size and leave it alone.
1453 SendSizeEventToParent();
1456 // ----------------------------------------------------------------------------
1458 // ---------------------------------------------------------------------------
1460 // get the TBSTYLE of the given toolbar window
1461 long wxToolBar::GetMSWToolbarStyle() const
1463 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1466 void wxToolBar::SetWindowStyleFlag(long style
)
1468 // the style bits whose changes force us to recreate the toolbar
1469 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1471 const long styleOld
= GetWindowStyle();
1473 wxToolBarBase::SetWindowStyleFlag(style
);
1475 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1476 // also fatal as we'd then try to recreate the toolbar when it's just being
1478 if ( GetToolsCount() &&
1479 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1481 // to remove the text labels, simply re-realizing the toolbar is enough
1482 // but I don't know of any way to add the text to an existing toolbar
1483 // other than by recreating it entirely
1488 // ----------------------------------------------------------------------------
1490 // ----------------------------------------------------------------------------
1492 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1494 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1495 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1498 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1500 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1501 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1504 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1506 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1507 // without, so we really need to delete the button and recreate it here
1508 wxFAIL_MSG( _T("not implemented") );
1511 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1513 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1516 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1518 tool
->SetNormalBitmap(bitmap
);
1523 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1525 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1528 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1530 tool
->SetDisabledBitmap(bitmap
);
1535 // ----------------------------------------------------------------------------
1537 // ----------------------------------------------------------------------------
1539 // Responds to colour changes, and passes event on to children.
1540 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1542 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1544 // Remap the buttons
1547 // Relayout the toolbar
1548 int nrows
= m_maxRows
;
1549 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1554 // let the event propagate further
1558 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1560 if ( event
.Leaving() )
1564 OnMouseEnter(wxID_ANY
);
1572 if ( event
.RightDown() )
1574 // find the tool under the mouse
1575 wxCoord x
= 0, y
= 0;
1576 event
.GetPosition(&x
, &y
);
1578 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1579 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1587 // This handler is required to allow the toolbar to be set to a non-default
1588 // colour: for example, when it must blend in with a notebook page.
1589 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1591 RECT rect
= wxGetClientRect(GetHwnd());
1593 wxDC
*dc
= event
.GetDC();
1595 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
->GetImpl();
1596 HDC hdc
= GetHdcOf(*impl
);
1598 int majorVersion
, minorVersion
;
1599 wxGetOsVersion(& majorVersion
, & minorVersion
);
1602 // we may need to draw themed colour so that we appear correctly on
1603 // e.g. notebook page under XP with themes but only do it if the parent
1604 // draws themed background itself
1605 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1607 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1611 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1615 // it can also return S_FALSE which seems to simply say that it
1616 // didn't draw anything but no error really occurred
1618 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1622 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
1623 if ( !UseBgCol() && majorVersion
>= 6 )
1625 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1628 wxUxThemeHandle
hTheme(this, L
"REBAR");
1631 wxRect rect
= GetClientRect();
1632 wxCopyRectToRECT(rect
, r
);
1634 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, hdc
, 0, 0, & r
, NULL
);
1638 // it can also return S_FALSE which seems to simply say that it
1639 // didn't draw anything but no error really occurred
1641 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1645 #endif // wxUSE_UXTHEME
1647 // we need to always draw our background under XP, as otherwise it doesn't
1648 // appear correctly with some themes (e.g. Zune one)
1649 if ( majorVersion
== 5 ||
1650 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1652 // do draw our background
1654 // notice that this 'dumb' implementation may cause flicker for some of
1655 // the controls in which case they should intercept wxEraseEvent and
1656 // process it themselves somehow
1657 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1659 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1660 ::FillRect(hdc
, &rect
, hBrush
);
1662 else // we have no non default background colour
1664 // let the system do it for us
1669 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1671 // wait until we have some tools
1672 if ( !GetToolsCount() )
1675 // calculate our minor dimension ourselves - we're confusing the standard
1676 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1677 const RECT r
= wxGetTBItemRect(GetHwnd(), 0);
1685 w
= r
.right
- r
.left
;
1688 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1695 if (HasFlag( wxTB_FLAT
))
1696 h
= r
.bottom
- r
.top
- 3;
1698 h
= r
.bottom
- r
.top
;
1701 // FIXME: hardcoded separator line height...
1702 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1707 if ( MAKELPARAM(w
, h
) != lParam
)
1709 // size really changed
1713 // message processed
1717 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1719 // erase any dummy separators which were used
1720 // for aligning the controls if any here
1722 // first of all, are there any controls at all?
1723 wxToolBarToolsList::compatibility_iterator node
;
1724 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1726 if ( node
->GetData()->IsControl() )
1731 // no controls, nothing to erase
1734 wxSize clientSize
= GetClientSize();
1735 int majorVersion
, minorVersion
;
1736 wxGetOsVersion(& majorVersion
, & minorVersion
);
1738 // prepare the DC on which we'll be drawing
1739 wxClientDC
dc(this);
1740 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1741 dc
.SetPen(*wxTRANSPARENT_PEN
);
1744 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1745 // nothing to redraw anyhow
1749 wxCopyRECTToRect(r
, rectUpdate
);
1751 dc
.SetClippingRegion(rectUpdate
);
1753 // draw the toolbar tools, separators &c normally
1754 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1756 // for each control in the toolbar find all the separators intersecting it
1759 // NB: this is really the only way to do it as we don't know if a separator
1760 // corresponds to a control (i.e. is a dummy one) or a real one
1762 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1764 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1765 if ( tool
->IsControl() )
1767 // get the control rect in our client coords
1768 wxControl
*control
= tool
->GetControl();
1769 wxStaticText
*staticText
= tool
->GetStaticText();
1770 wxRect rectCtrl
= control
->GetRect();
1771 wxRect
rectStaticText(0,0,0,0);
1774 rectStaticText
= staticText
->GetRect();
1777 // iterate over all buttons
1779 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1780 for ( int n
= 0; n
< count
; n
++ )
1782 // is it a separator?
1783 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1786 wxLogDebug(_T("TB_GETBUTTON failed?"));
1791 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1794 // get the bounding rect of the separator
1795 RECT r
= wxGetTBItemRect(GetHwnd(), n
);
1799 // does it intersect the control?
1801 wxCopyRECTToRect(r
, rectItem
);
1802 if ( rectCtrl
.Intersects(rectItem
) || (staticText
&& rectStaticText
.Intersects(rectItem
)))
1804 // yes, do erase it!
1806 bool haveRefreshed
= false;
1809 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1811 // Don't use DrawThemeBackground
1813 else if ( !UseBgCol() && majorVersion
>= 6 )
1815 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1818 wxUxThemeHandle
hTheme(this, L
"REBAR");
1822 // Draw the whole background since the pattern may be position sensitive;
1823 // but clip it to the area of interest.
1825 r
.right
= clientSize
.x
;
1827 r
.bottom
= clientSize
.y
;
1829 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
1830 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, GetHdcOf(*impl
), 0, 0, & r
, & clipRect
);
1832 haveRefreshed
= true;
1838 dc
.DrawRectangle(rectItem
);
1841 if ( rectCtrl
.Intersects(rectItem
) )
1843 // Necessary in case we use a no-paint-on-size
1844 // style in the parent: the controls can disappear
1845 control
->Refresh(false);
1848 if ( staticText
&& rectStaticText
.Intersects(rectItem
) )
1850 // Necessary in case we use a no-paint-on-size
1851 // style in the parent: the controls can disappear
1852 staticText
->Refresh(false);
1861 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1863 wxCoord x
= GET_X_LPARAM(lParam
),
1864 y
= GET_Y_LPARAM(lParam
);
1865 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1867 // has the current tool changed?
1868 if ( tool
!= m_pInTool
)
1871 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1875 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1880 // we don't handle mouse moves, so always pass the message to
1881 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1882 HandleMouseMove(wParam
, lParam
);
1886 if ( HandleSize(wParam
, lParam
) )
1892 // refreshing the controls in the toolbar inside a composite window
1893 // results in an endless stream of WM_PAINT messages -- and seems
1894 // to be unnecessary anyhow as everything works just fine without
1895 // any special workarounds in this case
1896 if ( !IsDoubleBuffered() && HandlePaint(wParam
, lParam
) )
1899 #endif // __WXWINCE__
1902 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1905 // ----------------------------------------------------------------------------
1906 // private functions
1907 // ----------------------------------------------------------------------------
1909 #ifdef wxREMAP_BUTTON_COLOURS
1911 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1917 wxLogLastError(_T("CreateCompatibleDC"));
1922 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1926 wxLogLastError(_T("SelectObject"));
1931 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1933 for ( int i
= 0; i
< width
; i
++ )
1935 for ( int j
= 0; j
< height
; j
++ )
1937 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1939 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1941 COLORREF col
= cmap
[k
].from
;
1942 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1943 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1944 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1946 if ( cmap
[k
].to
!= pixel
)
1947 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1957 #endif // wxREMAP_BUTTON_COLOURS
1959 #endif // wxUSE_TOOLBAR