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 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
401 if ( frame
&& !frame
->IsBeingDeleted() )
402 frame
->SendSizeEvent();
405 ::DeleteObject((HBITMAP
) m_hBitmap
);
407 delete m_disabledImgList
;
410 wxSize
wxToolBar::DoGetBestSize() const
415 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
417 // maybe an old (< 0x400) Windows version? try to approximate the
418 // toolbar size ourselves
419 sizeBest
= GetToolSize();
420 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
421 sizeBest
.x
*= GetToolsCount();
423 // reverse horz and vertical components if necessary
427 sizeBest
.x
= sizeBest
.y
;
431 else // TB_GETMAXSIZE succeeded
433 // but it could still return an incorrect result due to what appears to
434 // be a bug in old comctl32.dll versions which don't handle controls in
435 // the toolbar correctly, so work around it (see SF patch 1902358)
436 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
438 // calculate the toolbar width in alternative way
439 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
440 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
442 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
443 if ( widthAlt
> size
.cx
)
447 sizeBest
.x
= size
.cx
;
448 sizeBest
.y
= size
.cy
;
453 // Without the extra height, DoGetBestSize can report a size that's
454 // smaller than the actual window, causing windows to overlap slightly
455 // in some circumstances, leading to missing borders (especially noticeable
457 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
462 CacheBestSize(sizeBest
);
467 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
469 // toolbars never have border, giving one to them results in broken
471 WXDWORD msStyle
= wxControl::MSWGetStyle
473 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
476 if ( !(style
& wxTB_NO_TOOLTIPS
) )
477 msStyle
|= TBSTYLE_TOOLTIPS
;
479 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
481 // static as it doesn't change during the program lifetime
482 static const int s_verComCtl
= wxApp::GetComCtl32Version();
484 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
485 // style with 6.00 (part of Windows XP) leads to the toolbar with
486 // incorrect background colour - and not using it still results in the
487 // correct (flat) toolbar, so don't use it there
488 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
489 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
491 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
492 msStyle
|= TBSTYLE_LIST
;
495 if ( style
& wxTB_NODIVIDER
)
496 msStyle
|= CCS_NODIVIDER
;
498 if ( style
& wxTB_NOALIGN
)
499 msStyle
|= CCS_NOPARENTALIGN
;
501 if ( style
& wxTB_VERTICAL
)
504 if( style
& wxTB_BOTTOM
)
505 msStyle
|= CCS_BOTTOM
;
507 if ( style
& wxTB_RIGHT
)
508 msStyle
|= CCS_RIGHT
;
513 // ----------------------------------------------------------------------------
514 // adding/removing tools
515 // ----------------------------------------------------------------------------
517 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
518 wxToolBarToolBase
* WXUNUSED(tool
))
520 // nothing special to do here - we really create the toolbar buttons in
522 InvalidateBestSize();
526 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
528 // the main difficulty we have here is with the controls in the toolbars:
529 // as we (sometimes) use several separators to cover up the space used by
530 // them, the indices are not the same for us and the toolbar
532 // first determine the position of the first button to delete: it may be
533 // different from pos if we use several separators to cover the space used
535 wxToolBarToolsList::compatibility_iterator node
;
536 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
538 wxToolBarToolBase
*tool2
= node
->GetData();
541 // let node point to the next node in the list
542 node
= node
->GetNext();
547 if ( tool2
->IsControl() )
548 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
551 // now determine the number of buttons to delete and the area taken by them
552 size_t nButtonsToDelete
= 1;
554 // get the size of the button we're going to delete
555 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
557 int width
= r
.right
- r
.left
;
559 if ( tool
->IsControl() )
561 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
562 width
*= nButtonsToDelete
;
565 // do delete all buttons
566 m_nButtons
-= nButtonsToDelete
;
567 while ( nButtonsToDelete
-- > 0 )
569 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
571 wxLogLastError(wxT("TB_DELETEBUTTON"));
577 // and finally reposition all the controls after this button (the toolbar
578 // takes care of all normal items)
579 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
581 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
582 if ( tool2
->IsControl() )
584 wxControl
* const control
= tool2
->GetControl();
587 control
->GetPosition(&x
, NULL
);
588 control
->Move(x
- width
, wxDefaultCoord
);
590 wxStaticText
* const staticText
= tool2
->GetStaticText();
592 staticText
->Move(x
- width
, wxDefaultCoord
);
596 InvalidateBestSize();
601 void wxToolBar::CreateDisabledImageList()
603 if (m_disabledImgList
!= NULL
)
605 delete m_disabledImgList
;
606 m_disabledImgList
= NULL
;
609 // as we can't use disabled image list with older versions of comctl32.dll,
610 // don't even bother creating it
611 if ( wxApp::GetComCtl32Version() >= 470 )
613 // search for the first disabled button img in the toolbar, if any
614 for ( wxToolBarToolsList::compatibility_iterator
615 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
617 wxToolBarToolBase
*tool
= node
->GetData();
618 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
619 if ( bmpDisabled
.Ok() )
621 m_disabledImgList
= new wxImageList
625 bmpDisabled
.GetMask() != NULL
,
632 // we don't have any disabled bitmaps
636 bool wxToolBar::Realize()
638 const size_t nTools
= GetToolsCount();
643 #ifdef wxREMAP_BUTTON_COLOURS
644 // don't change the values of these constants, they can be set from the
645 // user code via wxSystemOptions
654 // the user-specified option overrides anything, but if it wasn't set, only
655 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
656 // much worse after remapping
657 static const wxChar
*remapOption
= wxT("msw.remap");
658 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
659 ? wxSystemOptions::GetOptionInt(remapOption
)
660 : wxDisplayDepth() <= 8 ? Remap_Buttons
663 #endif // wxREMAP_BUTTON_COLOURS
665 // delete all old buttons, if any
666 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
668 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
670 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
674 // First, add the bitmap: we use one bitmap for all toolbar buttons
675 // ----------------------------------------------------------------
677 wxToolBarToolsList::compatibility_iterator node
;
681 if ( HasFlag(wxTB_NOICONS
) )
683 // no icons, don't leave space for them
687 else // do show icons
689 // if we already have a bitmap, we'll replace the existing one --
690 // otherwise we'll install a new one
691 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
693 sizeBmp
.x
= m_defaultWidth
;
694 sizeBmp
.y
= m_defaultHeight
;
696 const wxCoord totalBitmapWidth
= m_defaultWidth
*
697 wx_truncate_cast(wxCoord
, nTools
),
698 totalBitmapHeight
= m_defaultHeight
;
700 // Create a bitmap and copy all the tool bitmaps into it
701 wxMemoryDC dcAllButtons
;
702 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
703 dcAllButtons
.SelectObject(bitmap
);
705 #ifdef wxREMAP_BUTTON_COLOURS
706 if ( remapValue
!= Remap_TransparentBg
)
707 #endif // wxREMAP_BUTTON_COLOURS
709 // VZ: why do we hardcode grey colour for CE?
710 dcAllButtons
.SetBackground(wxBrush(
712 wxColour(0xc0, 0xc0, 0xc0)
713 #else // !__WXWINCE__
714 GetBackgroundColour()
715 #endif // __WXWINCE__/!__WXWINCE__
717 dcAllButtons
.Clear();
720 m_hBitmap
= bitmap
.GetHBITMAP();
721 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
723 #ifdef wxREMAP_BUTTON_COLOURS
724 if ( remapValue
== Remap_Bg
)
726 dcAllButtons
.SelectObject(wxNullBitmap
);
728 // Even if we're not remapping the bitmap
729 // content, we still have to remap the background.
730 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
731 totalBitmapWidth
, totalBitmapHeight
);
733 dcAllButtons
.SelectObject(bitmap
);
735 #endif // wxREMAP_BUTTON_COLOURS
737 // the button position
740 // the number of buttons (not separators)
743 CreateDisabledImageList();
744 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
746 wxToolBarToolBase
*tool
= node
->GetData();
747 if ( tool
->IsButton() )
749 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
751 const int w
= bmp
.GetWidth();
752 const int h
= bmp
.GetHeight();
756 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
757 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
759 // notice the last parameter: do use mask
760 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
764 wxFAIL_MSG( _T("invalid tool button bitmap") );
767 // also deal with disabled bitmap if we want to use them
768 if ( m_disabledImgList
)
770 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
771 #if wxUSE_IMAGE && wxUSE_WXDIB
772 if ( !bmpDisabled
.Ok() )
774 // no disabled bitmap specified but we still need to
775 // fill the space in the image list with something, so
776 // we grey out the normal bitmap
778 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
780 #ifdef wxREMAP_BUTTON_COLOURS
781 if ( remapValue
== Remap_Buttons
)
783 // we need to have light grey background colour for
784 // MapBitmap() to work correctly
785 for ( int y
= 0; y
< h
; y
++ )
787 for ( int x
= 0; x
< w
; x
++ )
789 if ( imgGreyed
.IsTransparent(x
, y
) )
790 imgGreyed
.SetRGB(x
, y
,
792 wxLIGHT_GREY
->Green(),
793 wxLIGHT_GREY
->Blue());
797 #endif // wxREMAP_BUTTON_COLOURS
799 bmpDisabled
= wxBitmap(imgGreyed
);
801 #endif // wxUSE_IMAGE
803 #ifdef wxREMAP_BUTTON_COLOURS
804 if ( remapValue
== Remap_Buttons
)
805 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
806 #endif // wxREMAP_BUTTON_COLOURS
808 m_disabledImgList
->Add(bmpDisabled
);
811 // still inc width and number of buttons because otherwise the
812 // subsequent buttons will all be shifted which is rather confusing
813 // (and like this you'd see immediately which bitmap was bad)
819 dcAllButtons
.SelectObject(wxNullBitmap
);
821 // don't delete this HBITMAP!
822 bitmap
.SetHBITMAP(0);
824 #ifdef wxREMAP_BUTTON_COLOURS
825 if ( remapValue
== Remap_Buttons
)
827 // Map to system colours
828 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
829 totalBitmapWidth
, totalBitmapHeight
);
831 #endif // wxREMAP_BUTTON_COLOURS
833 bool addBitmap
= true;
835 if ( oldToolBarBitmap
)
837 #ifdef TB_REPLACEBITMAP
838 if ( wxApp::GetComCtl32Version() >= 400 )
840 TBREPLACEBITMAP replaceBitmap
;
841 replaceBitmap
.hInstOld
= NULL
;
842 replaceBitmap
.hInstNew
= NULL
;
843 replaceBitmap
.nIDOld
= (UINT_PTR
)oldToolBarBitmap
;
844 replaceBitmap
.nIDNew
= (UINT_PTR
)hBitmap
;
845 replaceBitmap
.nButtons
= nButtons
;
846 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
847 0, (LPARAM
) &replaceBitmap
) )
849 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
852 ::DeleteObject(oldToolBarBitmap
);
858 #endif // TB_REPLACEBITMAP
860 // we can't replace the old bitmap, so we will add another one
861 // (awfully inefficient, but what else to do?) and shift the bitmap
862 // indices accordingly
865 bitmapId
= m_nButtons
;
869 if ( addBitmap
) // no old bitmap or we can't replace it
871 TBADDBITMAP addBitmap
;
873 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
874 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
875 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
877 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
881 // disable image lists are only supported in comctl32.dll 4.70+
882 if ( wxApp::GetComCtl32Version() >= 470 )
884 HIMAGELIST hil
= m_disabledImgList
885 ? GetHimagelistOf(m_disabledImgList
)
888 // notice that we set the image list even if don't have one right
889 // now as we could have it before and need to reset it in this case
890 HIMAGELIST oldImageList
= (HIMAGELIST
)
891 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
893 // delete previous image list if any
895 ::DeleteObject(oldImageList
);
899 // don't call SetToolBitmapSize() as we don't want to change the values of
900 // m_defaultWidth/Height
901 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
902 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
904 wxLogLastError(_T("TB_SETBITMAPSIZE"));
907 // Next add the buttons and separators
908 // -----------------------------------
910 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 wxToolBarToolBase
*tool
= 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 button
.idCommand
= tool
->GetId();
938 // fall through: create just a separator too
940 case wxTOOL_STYLE_SEPARATOR
:
941 button
.fsState
= TBSTATE_ENABLED
;
942 button
.fsStyle
= TBSTYLE_SEP
;
945 case wxTOOL_STYLE_BUTTON
:
946 if ( !HasFlag(wxTB_NOICONS
) )
947 button
.iBitmap
= bitmapId
;
949 if ( HasFlag(wxTB_TEXT
) )
951 const wxString
& label
= tool
->GetLabel();
952 if ( !label
.empty() )
953 button
.iString
= (INT_PTR
)label
.wx_str();
956 button
.idCommand
= tool
->GetId();
958 if ( tool
->IsEnabled() )
959 button
.fsState
|= TBSTATE_ENABLED
;
960 if ( tool
->IsToggled() )
961 button
.fsState
|= TBSTATE_CHECKED
;
963 switch ( tool
->GetKind() )
966 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
970 // the first item in the radio group is checked by
971 // default to be consistent with wxGTK and the menu
973 button
.fsState
|= TBSTATE_CHECKED
;
975 if (tool
->Toggle(true))
977 DoToggleTool(tool
, true);
980 else if ( tool
->IsToggled() )
982 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
983 int prevIndex
= i
- 1;
986 TBBUTTON
& prevButton
= buttons
[prevIndex
];
987 wxToolBarToolBase
*tool
= nodePrev
->GetData();
988 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
991 if ( tool
->Toggle(false) )
992 DoToggleTool(tool
, false);
994 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
995 nodePrev
= nodePrev
->GetPrevious();
1004 button
.fsStyle
= TBSTYLE_CHECK
;
1008 button
.fsStyle
= TBSTYLE_BUTTON
;
1011 case wxITEM_DROPDOWN
:
1012 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1016 wxFAIL_MSG( _T("unexpected toolbar button kind") );
1017 button
.fsStyle
= TBSTYLE_BUTTON
;
1025 lastWasRadio
= isRadio
;
1030 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
1032 wxLogLastError(wxT("TB_ADDBUTTONS"));
1037 // Deal with the controls finally
1038 // ------------------------------
1040 // adjust the controls size to fit nicely in the toolbar
1043 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
1045 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1047 // we calculate the running y coord for vertical toolbars so we need to
1048 // get the items size for all items but for the horizontal ones we
1049 // don't need to deal with the non controls
1050 bool isControl
= tool
->IsControl();
1051 if ( !isControl
&& !IsVertical() )
1054 const RECT r
= wxGetTBItemRect(GetHwnd(), index
);
1057 // can only be control if isVertical
1058 y
+= r
.bottom
- r
.top
;
1063 wxControl
*control
= tool
->GetControl();
1064 wxStaticText
* const staticText
= tool
->GetStaticText();
1066 wxSize size
= control
->GetSize();
1067 wxSize staticTextSize
;
1070 staticTextSize
= staticText
->GetSize();
1071 staticTextSize
.y
+= 3; // margin between control and its label
1074 // the position of the leftmost controls corner
1075 int left
= wxDefaultCoord
;
1077 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1078 #ifdef TB_SETBUTTONINFO
1079 // available in headers, now check whether it is available now
1080 // (during run-time)
1081 if ( wxApp::GetComCtl32Version() >= 471 )
1083 // set the (underlying) separators width to be that of the
1086 tbbi
.cbSize
= sizeof(tbbi
);
1087 tbbi
.dwMask
= TBIF_SIZE
;
1088 tbbi
.cx
= (WORD
)size
.x
;
1089 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1090 tool
->GetId(), (LPARAM
)&tbbi
) )
1092 // the id is probably invalid?
1093 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1097 #endif // comctl32.dll 4.71
1098 // TB_SETBUTTONINFO unavailable
1100 // try adding several separators to fit the controls width
1101 int widthSep
= r
.right
- r
.left
;
1107 tbb
.fsState
= TBSTATE_ENABLED
;
1108 tbb
.fsStyle
= TBSTYLE_SEP
;
1110 size_t nSeparators
= size
.x
/ widthSep
;
1111 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1113 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1114 index
, (LPARAM
)&tbb
) )
1116 wxLogLastError(wxT("TB_INSERTBUTTON"));
1122 // remember the number of separators we used - we'd have to
1123 // delete all of them later
1124 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1126 // adjust the controls width to exactly cover the separators
1127 size
.x
= (nSeparators
+ 1)*widthSep
;
1128 control
->SetSize(size
.x
, wxDefaultCoord
);
1131 // position the control itself correctly vertically centering it on the
1132 // icon area of the toolbar
1133 int height
= r
.bottom
- r
.top
- staticTextSize
.y
;
1135 int diff
= height
- size
.y
;
1136 if ( diff
< 0 || !HasFlag(wxTB_TEXT
) )
1138 // not enough room for the static text
1142 // recalculate height & diff without the staticText control
1143 height
= r
.bottom
- r
.top
;
1144 diff
= height
- size
.y
;
1147 // the control is too high, resize to fit
1148 control
->SetSize(wxDefaultCoord
, height
- 2);
1153 else // enough space for both the control and the label
1165 y
+= height
+ 2 * GetMargins().y
;
1167 else // horizontal toolbar
1169 if ( left
== wxDefaultCoord
)
1175 control
->Move(left
, top
+ (diff
+ 1) / 2);
1178 staticText
->Move(left
+ (size
.x
- staticTextSize
.x
)/2,
1179 r
.bottom
- staticTextSize
.y
);
1183 // the max index is the "real" number of buttons - i.e. counting even the
1184 // separators which we added just for aligning the controls
1187 if ( !IsVertical() )
1189 if ( m_maxRows
== 0 )
1190 // if not set yet, only one row
1193 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1195 // if not set yet, have one column
1197 SetRows(m_nButtons
);
1200 InvalidateBestSize();
1206 // ----------------------------------------------------------------------------
1208 // ----------------------------------------------------------------------------
1210 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
1212 // cast to signed is important as we compare this id with (signed) ints in
1213 // FindById() and without the cast we'd get a positive int from a
1214 // "negative" (i.e. > 32767) WORD
1215 const int id
= (signed short)id_
;
1217 wxToolBarToolBase
*tool
= FindById(id
);
1221 bool toggled
= false; // just to suppress warnings
1223 if ( tool
->CanBeToggled() )
1225 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1226 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1228 // ignore the event when a radio button is released, as this doesn't
1229 // seem to happen at all, and is handled otherwise
1230 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1233 tool
->Toggle(toggled
);
1234 UnToggleRadioGroup(tool
);
1237 // OnLeftClick() can veto the button state change - for buttons which
1238 // may be toggled only, of couse
1239 if ( !OnLeftClick(id
, toggled
) && tool
->CanBeToggled() )
1242 tool
->Toggle(!toggled
);
1244 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1250 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1252 WXLPARAM
*WXUNUSED(result
))
1254 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1255 if ( hdr
->code
== TBN_DROPDOWN
)
1257 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1259 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tbhdr
->iItem
);
1260 if ( HandleWindowEvent(evt
) )
1262 // Event got handled, don't display default popup menu
1266 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1267 wxCHECK_MSG( tool
, false, _T("drop down message for unknown tool") );
1269 wxMenu
* const menu
= tool
->GetDropdownMenu();
1273 // Display popup menu below button
1274 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1276 PopupMenu(menu
, r
.left
, r
.bottom
);
1282 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1285 // First check if this applies to us
1287 // the tooltips control created by the toolbar is sometimes Unicode, even
1288 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1289 UINT code
= hdr
->code
;
1290 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1293 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1294 if ( toolTipWnd
!= hdr
->hwndFrom
)
1297 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1298 int id
= (int)ttText
->hdr
.idFrom
;
1300 wxToolBarToolBase
*tool
= FindById(id
);
1302 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1304 wxUnusedVar(lParam
);
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1317 wxToolBarBase::SetToolBitmapSize(size
);
1319 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1322 void wxToolBar::SetRows(int nRows
)
1324 if ( nRows
== m_maxRows
)
1326 // avoid resizing the frame uselessly
1330 // TRUE in wParam means to create at least as many rows, FALSE -
1333 ::SendMessage(GetHwnd(), TB_SETROWS
,
1334 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1342 // The button size is bigger than the bitmap size
1343 wxSize
wxToolBar::GetToolSize() const
1345 // TB_GETBUTTONSIZE is supported from version 4.70
1346 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1347 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1348 && !defined (__DIGITALMARS__)
1349 if ( wxApp::GetComCtl32Version() >= 470 )
1351 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1353 return wxSize(LOWORD(dw
), HIWORD(dw
));
1356 #endif // comctl32.dll 4.70+
1359 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1364 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1367 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1369 for ( ; current
; current
= current
->GetNext() )
1372 return current
->GetData();
1374 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1375 size_t separators
= tool
->GetSeparatorsCount();
1377 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1378 // otherwise, skip as many items as the separator count, plus the
1380 index
-= separators
? separators
+ 1 : 1;
1386 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1391 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1393 // MBN: when the point ( x, y ) is close to the toolbar border
1394 // TB_HITTEST returns m_nButtons ( not -1 )
1395 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1396 // it's a separator or there is no tool at all there
1397 return (wxToolBarToolBase
*)NULL
;
1399 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1400 // we don't use the dummy separators hack
1401 #ifdef TB_SETBUTTONINFO
1402 if ( wxApp::GetComCtl32Version() >= 471 )
1404 return m_tools
.Item((size_t)index
)->GetData();
1407 #endif // TB_SETBUTTONINFO
1409 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1413 void wxToolBar::UpdateSize()
1415 wxPoint pos
= GetPosition();
1416 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1417 if (pos
!= GetPosition())
1420 // In case Realize is called after the initial display (IOW the programmer
1421 // may have rebuilt the toolbar) give the frame the option of resizing the
1422 // toolbar to full width again, but only if the parent is a frame and the
1423 // toolbar is managed by the frame. Otherwise assume that some other
1424 // layout mechanism is controlling the toolbar size and leave it alone.
1425 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1426 if ( frame
&& frame
->GetToolBar() == this )
1428 frame
->SendSizeEvent();
1432 // ----------------------------------------------------------------------------
1434 // ---------------------------------------------------------------------------
1436 // get the TBSTYLE of the given toolbar window
1437 long wxToolBar::GetMSWToolbarStyle() const
1439 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1442 void wxToolBar::SetWindowStyleFlag(long style
)
1444 // the style bits whose changes force us to recreate the toolbar
1445 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1447 const long styleOld
= GetWindowStyle();
1449 wxToolBarBase::SetWindowStyleFlag(style
);
1451 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1452 // also fatal as we'd then try to recreate the toolbar when it's just being
1454 if ( GetToolsCount() &&
1455 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1457 // to remove the text labels, simply re-realizing the toolbar is enough
1458 // but I don't know of any way to add the text to an existing toolbar
1459 // other than by recreating it entirely
1464 // ----------------------------------------------------------------------------
1466 // ----------------------------------------------------------------------------
1468 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1470 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1471 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1474 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1476 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1477 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1480 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1482 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1483 // without, so we really need to delete the button and recreate it here
1484 wxFAIL_MSG( _T("not implemented") );
1487 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1489 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1492 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1494 tool
->SetNormalBitmap(bitmap
);
1499 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1501 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
1504 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1506 tool
->SetDisabledBitmap(bitmap
);
1511 // ----------------------------------------------------------------------------
1513 // ----------------------------------------------------------------------------
1515 // Responds to colour changes, and passes event on to children.
1516 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1518 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1520 // Remap the buttons
1523 // Relayout the toolbar
1524 int nrows
= m_maxRows
;
1525 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1530 // let the event propagate further
1534 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1536 if ( event
.Leaving() )
1540 OnMouseEnter(wxID_ANY
);
1548 if ( event
.RightDown() )
1550 // find the tool under the mouse
1551 wxCoord x
= 0, y
= 0;
1552 event
.GetPosition(&x
, &y
);
1554 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1555 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1563 // This handler is required to allow the toolbar to be set to a non-default
1564 // colour: for example, when it must blend in with a notebook page.
1565 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1567 RECT rect
= wxGetClientRect(GetHwnd());
1569 wxDC
*dc
= event
.GetDC();
1571 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
->GetImpl();
1572 HDC hdc
= GetHdcOf(*impl
);
1574 int majorVersion
, minorVersion
;
1575 wxGetOsVersion(& majorVersion
, & minorVersion
);
1578 // we may need to draw themed colour so that we appear correctly on
1579 // e.g. notebook page under XP with themes but only do it if the parent
1580 // draws themed background itself
1581 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1583 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1587 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1591 // it can also return S_FALSE which seems to simply say that it
1592 // didn't draw anything but no error really occurred
1594 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1598 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
1599 if ( !UseBgCol() && majorVersion
>= 6 )
1601 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1604 wxUxThemeHandle
hTheme(this, L
"REBAR");
1607 wxRect rect
= GetClientRect();
1608 wxCopyRectToRECT(rect
, r
);
1610 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, hdc
, 0, 0, & r
, NULL
);
1614 // it can also return S_FALSE which seems to simply say that it
1615 // didn't draw anything but no error really occurred
1617 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1621 #endif // wxUSE_UXTHEME
1623 // we need to always draw our background under XP, as otherwise it doesn't
1624 // appear correctly with some themes (e.g. Zune one)
1625 if ( majorVersion
== 5 ||
1626 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1628 // do draw our background
1630 // notice that this 'dumb' implementation may cause flicker for some of
1631 // the controls in which case they should intercept wxEraseEvent and
1632 // process it themselves somehow
1633 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1635 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1636 ::FillRect(hdc
, &rect
, hBrush
);
1638 else // we have no non default background colour
1640 // let the system do it for us
1645 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1647 // wait until we have some tools
1648 if ( !GetToolsCount() )
1651 // calculate our minor dimension ourselves - we're confusing the standard
1652 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1653 const RECT r
= wxGetTBItemRect(GetHwnd(), 0);
1661 w
= r
.right
- r
.left
;
1664 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1671 if (HasFlag( wxTB_FLAT
))
1672 h
= r
.bottom
- r
.top
- 3;
1674 h
= r
.bottom
- r
.top
;
1677 // FIXME: hardcoded separator line height...
1678 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1683 if ( MAKELPARAM(w
, h
) != lParam
)
1685 // size really changed
1689 // message processed
1693 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1695 // erase any dummy separators which were used
1696 // for aligning the controls if any here
1698 // first of all, are there any controls at all?
1699 wxToolBarToolsList::compatibility_iterator node
;
1700 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1702 if ( node
->GetData()->IsControl() )
1707 // no controls, nothing to erase
1710 wxSize clientSize
= GetClientSize();
1711 int majorVersion
, minorVersion
;
1712 wxGetOsVersion(& majorVersion
, & minorVersion
);
1714 // prepare the DC on which we'll be drawing
1715 wxClientDC
dc(this);
1716 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1717 dc
.SetPen(*wxTRANSPARENT_PEN
);
1720 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1721 // nothing to redraw anyhow
1725 wxCopyRECTToRect(r
, rectUpdate
);
1727 dc
.SetClippingRegion(rectUpdate
);
1729 // draw the toolbar tools, separators &c normally
1730 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1732 // for each control in the toolbar find all the separators intersecting it
1735 // NB: this is really the only way to do it as we don't know if a separator
1736 // corresponds to a control (i.e. is a dummy one) or a real one
1738 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1740 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1741 if ( tool
->IsControl() )
1743 // get the control rect in our client coords
1744 wxControl
*control
= tool
->GetControl();
1745 wxStaticText
*staticText
= tool
->GetStaticText();
1746 wxRect rectCtrl
= control
->GetRect();
1747 wxRect
rectStaticText(0,0,0,0);
1750 rectStaticText
= staticText
->GetRect();
1753 // iterate over all buttons
1755 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1756 for ( int n
= 0; n
< count
; n
++ )
1758 // is it a separator?
1759 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1762 wxLogDebug(_T("TB_GETBUTTON failed?"));
1767 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1770 // get the bounding rect of the separator
1771 RECT r
= wxGetTBItemRect(GetHwnd(), n
);
1775 // does it intersect the control?
1777 wxCopyRECTToRect(r
, rectItem
);
1778 if ( rectCtrl
.Intersects(rectItem
) || (staticText
&& rectStaticText
.Intersects(rectItem
)))
1780 // yes, do erase it!
1782 bool haveRefreshed
= false;
1785 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1787 // Don't use DrawThemeBackground
1789 else if ( !UseBgCol() && majorVersion
>= 6 )
1791 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1794 wxUxThemeHandle
hTheme(this, L
"REBAR");
1798 // Draw the whole background since the pattern may be position sensitive;
1799 // but clip it to the area of interest.
1801 r
.right
= clientSize
.x
;
1803 r
.bottom
= clientSize
.y
;
1805 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
1806 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, GetHdcOf(*impl
), 0, 0, & r
, & clipRect
);
1808 haveRefreshed
= true;
1814 dc
.DrawRectangle(rectItem
);
1817 if ( rectCtrl
.Intersects(rectItem
) )
1819 // Necessary in case we use a no-paint-on-size
1820 // style in the parent: the controls can disappear
1821 control
->Refresh(false);
1824 if ( staticText
&& rectStaticText
.Intersects(rectItem
) )
1826 // Necessary in case we use a no-paint-on-size
1827 // style in the parent: the controls can disappear
1828 staticText
->Refresh(false);
1837 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1839 wxCoord x
= GET_X_LPARAM(lParam
),
1840 y
= GET_Y_LPARAM(lParam
);
1841 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1843 // has the current tool changed?
1844 if ( tool
!= m_pInTool
)
1847 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1851 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1856 // we don't handle mouse moves, so always pass the message to
1857 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1858 HandleMouseMove(wParam
, lParam
);
1862 if ( HandleSize(wParam
, lParam
) )
1868 if ( HandlePaint(wParam
, lParam
) )
1876 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1879 // ----------------------------------------------------------------------------
1880 // private functions
1881 // ----------------------------------------------------------------------------
1883 #ifdef wxREMAP_BUTTON_COLOURS
1885 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1891 wxLogLastError(_T("CreateCompatibleDC"));
1896 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1900 wxLogLastError(_T("SelectObject"));
1905 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1907 for ( int i
= 0; i
< width
; i
++ )
1909 for ( int j
= 0; j
< height
; j
++ )
1911 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1913 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1915 COLORREF col
= cmap
[k
].from
;
1916 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1917 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1918 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1920 if ( cmap
[k
].to
!= pixel
)
1921 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1931 #endif // wxREMAP_BUTTON_COLOURS
1933 #endif // wxUSE_TOOLBAR