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"
50 #include "wx/msw/private.h"
51 #include "wx/msw/dc.h"
54 #include "wx/msw/uxtheme.h"
57 // this define controls whether the code for button colours remapping (only
58 // useful for 16 or 256 colour images) is active at all, it's always turned off
59 // for CE where it doesn't compile (and is probably not needed anyhow) and may
60 // also be turned off for other systems if you always use 24bpp images and so
63 #define wxREMAP_BUTTON_COLOURS
64 #endif // !__WXWINCE__
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // these standard constants are not always defined in compilers headers
74 #define TBSTYLE_LIST 0x1000
75 #define TBSTYLE_FLAT 0x0800
78 #ifndef TBSTYLE_TRANSPARENT
79 #define TBSTYLE_TRANSPARENT 0x8000
82 #ifndef TBSTYLE_TOOLTIPS
83 #define TBSTYLE_TOOLTIPS 0x0100
88 #define TB_SETSTYLE (WM_USER + 56)
89 #define TB_GETSTYLE (WM_USER + 57)
93 #define TB_HITTEST (WM_USER + 69)
97 #define TB_GETMAXSIZE (WM_USER + 83)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
116 style ( wxNO_BORDER | wxTB_HORIZONTAL)
125 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
126 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
127 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
128 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class wxToolBarTool
: public wxToolBarToolBase
138 wxToolBarTool(wxToolBar
*tbar
,
140 const wxString
& label
,
141 const wxBitmap
& bmpNormal
,
142 const wxBitmap
& bmpDisabled
,
144 wxObject
*clientData
,
145 const wxString
& shortHelp
,
146 const wxString
& longHelp
)
147 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
148 clientData
, shortHelp
, longHelp
)
154 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
155 : wxToolBarToolBase(tbar
, control
, label
)
157 if ( IsControl() && !m_label
.empty() )
159 // create a control to render the control's label
160 m_staticText
= new wxStaticText
167 wxALIGN_CENTRE
| wxST_NO_AUTORESIZE
178 virtual ~wxToolBarTool()
183 virtual void SetLabel(const wxString
& label
)
185 if ( label
== m_label
)
188 wxToolBarToolBase::SetLabel(label
);
191 m_staticText
->SetLabel(label
);
193 // we need to update the label shown in the toolbar because it has a
194 // pointer to the internal buffer of the old label
196 // TODO: use TB_SETBUTTONINFO
199 wxStaticText
* GetStaticText()
201 wxASSERT_MSG( IsControl(),
202 _T("only makes sense for embedded control tools") );
207 // set/get the number of separators which we use to cover the space used by
208 // a control in the toolbar
209 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
210 size_t GetSeparatorsCount() const { return m_nSepCount
; }
214 wxStaticText
*m_staticText
;
216 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 // return the rectangle of the item at the given index
225 // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
226 // r.right or r.bottom with 0 to check for this
227 static RECT
wxGetTBItemRect(HWND hwnd
, int index
)
231 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
232 // only appeared in v4.70 of comctl32.dll
233 if ( !::SendMessage(hwnd
, TB_GETITEMRECT
, index
, (LPARAM
)&r
) )
235 wxLogLastError(wxT("TB_GETITEMRECT"));
246 // ============================================================================
248 // ============================================================================
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
255 const wxString
& label
,
256 const wxBitmap
& bmpNormal
,
257 const wxBitmap
& bmpDisabled
,
259 wxObject
*clientData
,
260 const wxString
& shortHelp
,
261 const wxString
& longHelp
)
263 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
264 clientData
, shortHelp
, longHelp
);
268 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
270 return new wxToolBarTool(this, control
, label
);
273 // ----------------------------------------------------------------------------
274 // wxToolBar construction
275 // ----------------------------------------------------------------------------
277 void wxToolBar::Init()
280 m_disabledImgList
= NULL
;
284 const wxSize size
= wxArtProvider::GetNativeSizeHint(wxART_TOOLBAR
);
285 m_defaultWidth
= size
.x
;
286 m_defaultHeight
= size
.y
;
291 bool wxToolBar::Create(wxWindow
*parent
,
296 const wxString
& name
)
298 // common initialisation
299 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
304 // MSW-specific initialisation
305 if ( !MSWCreateToolbar(pos
, size
) )
308 wxSetCCUnicodeFormat(GetHwnd());
310 // workaround for flat toolbar on Windows XP classic style: we have to set
311 // the style after creating the control; doing it at creation time doesn't work
313 if ( style
& wxTB_FLAT
)
315 LRESULT style
= GetMSWToolbarStyle();
317 if ( !(style
& TBSTYLE_FLAT
) )
318 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
320 #endif // wxUSE_UXTHEME
325 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
327 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
330 // toolbar-specific post initialisation
331 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
333 #ifdef TB_SETEXTENDEDSTYLE
334 if ( wxApp::GetComCtl32Version() >= 471 )
335 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE
, 0, TBSTYLE_EX_DRAWDDARROWS
);
341 void wxToolBar::Recreate()
343 const HWND hwndOld
= GetHwnd();
346 // we haven't been created yet, no need to recreate
350 // get the position and size before unsubclassing the old toolbar
351 const wxPoint pos
= GetPosition();
352 const wxSize size
= GetSize();
356 if ( !MSWCreateToolbar(pos
, size
) )
359 wxFAIL_MSG( _T("recreating the toolbar failed") );
364 // reparent all our children under the new toolbar
365 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
367 node
= node
->GetNext() )
369 wxWindow
*win
= node
->GetData();
370 if ( !win
->IsTopLevel() )
371 ::SetParent(GetHwndOf(win
), GetHwnd());
374 // only destroy the old toolbar now --
375 // after all the children had been reparented
376 ::DestroyWindow(hwndOld
);
378 // it is for the old bitmap control and can't be used with the new one
381 ::DeleteObject((HBITMAP
) m_hBitmap
);
385 if ( m_disabledImgList
)
387 delete m_disabledImgList
;
388 m_disabledImgList
= NULL
;
394 wxToolBar::~wxToolBar()
396 // we must refresh the frame size when the toolbar is deleted but the frame
397 // is not - otherwise toolbar leaves a hole in the place it used to occupy
398 SendSizeEventToParent();
401 ::DeleteObject((HBITMAP
) m_hBitmap
);
403 delete m_disabledImgList
;
406 wxSize
wxToolBar::DoGetBestSize() const
411 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
413 // maybe an old (< 0x400) Windows version? try to approximate the
414 // toolbar size ourselves
415 sizeBest
= GetToolSize();
416 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
417 sizeBest
.x
*= GetToolsCount();
419 // reverse horz and vertical components if necessary
423 sizeBest
.x
= sizeBest
.y
;
427 else // TB_GETMAXSIZE succeeded
429 // but it could still return an incorrect result due to what appears to
430 // be a bug in old comctl32.dll versions which don't handle controls in
431 // the toolbar correctly, so work around it (see SF patch 1902358)
432 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
434 // calculate the toolbar width in alternative way
435 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
436 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
438 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
439 if ( widthAlt
> size
.cx
)
443 sizeBest
.x
= size
.cx
;
444 sizeBest
.y
= size
.cy
;
449 // Without the extra height, DoGetBestSize can report a size that's
450 // smaller than the actual window, causing windows to overlap slightly
451 // in some circumstances, leading to missing borders (especially noticeable
453 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
458 CacheBestSize(sizeBest
);
463 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
465 // toolbars never have border, giving one to them results in broken
467 WXDWORD msStyle
= wxControl::MSWGetStyle
469 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
472 if ( !(style
& wxTB_NO_TOOLTIPS
) )
473 msStyle
|= TBSTYLE_TOOLTIPS
;
475 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
477 // static as it doesn't change during the program lifetime
478 static const int s_verComCtl
= wxApp::GetComCtl32Version();
480 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
481 // style with 6.00 (part of Windows XP) leads to the toolbar with
482 // incorrect background colour - and not using it still results in the
483 // correct (flat) toolbar, so don't use it there
484 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
485 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
487 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
488 msStyle
|= TBSTYLE_LIST
;
491 if ( style
& wxTB_NODIVIDER
)
492 msStyle
|= CCS_NODIVIDER
;
494 if ( style
& wxTB_NOALIGN
)
495 msStyle
|= CCS_NOPARENTALIGN
;
497 if ( style
& wxTB_VERTICAL
)
500 if( style
& wxTB_BOTTOM
)
501 msStyle
|= CCS_BOTTOM
;
503 if ( style
& wxTB_RIGHT
)
504 msStyle
|= CCS_RIGHT
;
509 // ----------------------------------------------------------------------------
510 // adding/removing tools
511 // ----------------------------------------------------------------------------
513 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
514 wxToolBarToolBase
* WXUNUSED(tool
))
516 // nothing special to do here - we really create the toolbar buttons in
518 InvalidateBestSize();
522 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
524 // the main difficulty we have here is with the controls in the toolbars:
525 // as we (sometimes) use several separators to cover up the space used by
526 // them, the indices are not the same for us and the toolbar
528 // first determine the position of the first button to delete: it may be
529 // different from pos if we use several separators to cover the space used
531 wxToolBarToolsList::compatibility_iterator node
;
532 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
534 wxToolBarToolBase
*tool2
= node
->GetData();
537 // let node point to the next node in the list
538 node
= node
->GetNext();
543 if ( tool2
->IsControl() )
544 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
547 // now determine the number of buttons to delete and the area taken by them
548 size_t nButtonsToDelete
= 1;
550 // get the size of the button we're going to delete
551 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
553 int width
= r
.right
- r
.left
;
555 if ( tool
->IsControl() )
557 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
558 width
*= nButtonsToDelete
;
561 // do delete all buttons
562 m_nButtons
-= nButtonsToDelete
;
563 while ( nButtonsToDelete
-- > 0 )
565 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
567 wxLogLastError(wxT("TB_DELETEBUTTON"));
573 // and finally reposition all the controls after this button (the toolbar
574 // takes care of all normal items)
575 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
577 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
578 if ( tool2
->IsControl() )
580 wxControl
* const control
= tool2
->GetControl();
583 control
->GetPosition(&x
, NULL
);
584 control
->Move(x
- width
, wxDefaultCoord
);
586 wxStaticText
* const staticText
= tool2
->GetStaticText();
588 staticText
->Move(x
- width
, wxDefaultCoord
);
592 InvalidateBestSize();
597 void wxToolBar::CreateDisabledImageList()
599 if (m_disabledImgList
!= NULL
)
601 delete m_disabledImgList
;
602 m_disabledImgList
= NULL
;
605 // as we can't use disabled image list with older versions of comctl32.dll,
606 // don't even bother creating it
607 if ( wxApp::GetComCtl32Version() >= 470 )
609 // search for the first disabled button img in the toolbar, if any
610 for ( wxToolBarToolsList::compatibility_iterator
611 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
613 wxToolBarToolBase
*tool
= node
->GetData();
614 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
615 if ( bmpDisabled
.Ok() )
617 m_disabledImgList
= new wxImageList
621 bmpDisabled
.GetMask() != NULL
,
628 // we don't have any disabled bitmaps
632 bool wxToolBar::Realize()
634 const size_t nTools
= GetToolsCount();
639 #ifdef wxREMAP_BUTTON_COLOURS
640 // don't change the values of these constants, they can be set from the
641 // user code via wxSystemOptions
650 // the user-specified option overrides anything, but if it wasn't set, only
651 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
652 // much worse after remapping
653 static const wxChar
*remapOption
= wxT("msw.remap");
654 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
655 ? wxSystemOptions::GetOptionInt(remapOption
)
656 : wxDisplayDepth() <= 8 ? Remap_Buttons
659 #endif // wxREMAP_BUTTON_COLOURS
661 // delete all old buttons, if any
662 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
664 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
666 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
670 // First, add the bitmap: we use one bitmap for all toolbar buttons
671 // ----------------------------------------------------------------
673 wxToolBarToolsList::compatibility_iterator node
;
677 if ( HasFlag(wxTB_NOICONS
) )
679 // no icons, don't leave space for them
683 else // do show icons
685 // if we already have a bitmap, we'll replace the existing one --
686 // otherwise we'll install a new one
687 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
689 sizeBmp
.x
= m_defaultWidth
;
690 sizeBmp
.y
= m_defaultHeight
;
692 const wxCoord totalBitmapWidth
= m_defaultWidth
*
693 wx_truncate_cast(wxCoord
, nTools
),
694 totalBitmapHeight
= m_defaultHeight
;
696 // Create a bitmap and copy all the tool bitmaps into it
697 wxMemoryDC dcAllButtons
;
698 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
699 dcAllButtons
.SelectObject(bitmap
);
701 #ifdef wxREMAP_BUTTON_COLOURS
702 if ( remapValue
!= Remap_TransparentBg
)
703 #endif // wxREMAP_BUTTON_COLOURS
705 // VZ: why do we hardcode grey colour for CE?
706 dcAllButtons
.SetBackground(wxBrush(
708 wxColour(0xc0, 0xc0, 0xc0)
709 #else // !__WXWINCE__
710 GetBackgroundColour()
711 #endif // __WXWINCE__/!__WXWINCE__
713 dcAllButtons
.Clear();
716 m_hBitmap
= bitmap
.GetHBITMAP();
717 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
719 #ifdef wxREMAP_BUTTON_COLOURS
720 if ( remapValue
== Remap_Bg
)
722 dcAllButtons
.SelectObject(wxNullBitmap
);
724 // Even if we're not remapping the bitmap
725 // content, we still have to remap the background.
726 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
727 totalBitmapWidth
, totalBitmapHeight
);
729 dcAllButtons
.SelectObject(bitmap
);
731 #endif // wxREMAP_BUTTON_COLOURS
733 // the button position
736 // the number of buttons (not separators)
739 CreateDisabledImageList();
740 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
742 wxToolBarToolBase
*tool
= node
->GetData();
743 if ( tool
->IsButton() )
745 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
747 const int w
= bmp
.GetWidth();
748 const int h
= bmp
.GetHeight();
752 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
753 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
755 // notice the last parameter: do use mask
756 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
760 wxFAIL_MSG( _T("invalid tool button bitmap") );
763 // also deal with disabled bitmap if we want to use them
764 if ( m_disabledImgList
)
766 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
767 #if wxUSE_IMAGE && wxUSE_WXDIB
768 if ( !bmpDisabled
.Ok() )
770 // no disabled bitmap specified but we still need to
771 // fill the space in the image list with something, so
772 // we grey out the normal bitmap
774 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
776 #ifdef wxREMAP_BUTTON_COLOURS
777 if ( remapValue
== Remap_Buttons
)
779 // we need to have light grey background colour for
780 // MapBitmap() to work correctly
781 for ( int y
= 0; y
< h
; y
++ )
783 for ( int x
= 0; x
< w
; x
++ )
785 if ( imgGreyed
.IsTransparent(x
, y
) )
786 imgGreyed
.SetRGB(x
, y
,
788 wxLIGHT_GREY
->Green(),
789 wxLIGHT_GREY
->Blue());
793 #endif // wxREMAP_BUTTON_COLOURS
795 bmpDisabled
= wxBitmap(imgGreyed
);
797 #endif // wxUSE_IMAGE
799 #ifdef wxREMAP_BUTTON_COLOURS
800 if ( remapValue
== Remap_Buttons
)
801 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
802 #endif // wxREMAP_BUTTON_COLOURS
804 m_disabledImgList
->Add(bmpDisabled
);
807 // still inc width and number of buttons because otherwise the
808 // subsequent buttons will all be shifted which is rather confusing
809 // (and like this you'd see immediately which bitmap was bad)
815 dcAllButtons
.SelectObject(wxNullBitmap
);
817 // don't delete this HBITMAP!
818 bitmap
.SetHBITMAP(0);
820 #ifdef wxREMAP_BUTTON_COLOURS
821 if ( remapValue
== Remap_Buttons
)
823 // Map to system colours
824 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
825 totalBitmapWidth
, totalBitmapHeight
);
827 #endif // wxREMAP_BUTTON_COLOURS
829 bool addBitmap
= true;
831 if ( oldToolBarBitmap
)
833 #ifdef TB_REPLACEBITMAP
834 if ( wxApp::GetComCtl32Version() >= 400 )
836 TBREPLACEBITMAP replaceBitmap
;
837 replaceBitmap
.hInstOld
= NULL
;
838 replaceBitmap
.hInstNew
= NULL
;
839 replaceBitmap
.nIDOld
= (UINT_PTR
)oldToolBarBitmap
;
840 replaceBitmap
.nIDNew
= (UINT_PTR
)hBitmap
;
841 replaceBitmap
.nButtons
= nButtons
;
842 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
843 0, (LPARAM
) &replaceBitmap
) )
845 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
848 ::DeleteObject(oldToolBarBitmap
);
854 #endif // TB_REPLACEBITMAP
856 // we can't replace the old bitmap, so we will add another one
857 // (awfully inefficient, but what else to do?) and shift the bitmap
858 // indices accordingly
861 bitmapId
= m_nButtons
;
865 if ( addBitmap
) // no old bitmap or we can't replace it
867 TBADDBITMAP addBitmap
;
869 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
870 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
871 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
873 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
877 // disable image lists are only supported in comctl32.dll 4.70+
878 if ( wxApp::GetComCtl32Version() >= 470 )
880 HIMAGELIST hil
= m_disabledImgList
881 ? GetHimagelistOf(m_disabledImgList
)
884 // notice that we set the image list even if don't have one right
885 // now as we could have it before and need to reset it in this case
886 HIMAGELIST oldImageList
= (HIMAGELIST
)
887 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
889 // delete previous image list if any
891 ::DeleteObject(oldImageList
);
895 // don't call SetToolBitmapSize() as we don't want to change the values of
896 // m_defaultWidth/Height
897 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
898 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
900 wxLogLastError(_T("TB_SETBITMAPSIZE"));
903 // Next add the buttons and separators
904 // -----------------------------------
906 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
908 // this array will hold the indices of all controls in the toolbar
909 wxArrayInt controlIds
;
911 bool lastWasRadio
= false;
913 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
915 wxToolBarToolBase
*tool
= node
->GetData();
917 // don't add separators to the vertical toolbar with old comctl32.dll
918 // versions as they didn't handle this properly
919 if ( IsVertical() && tool
->IsSeparator() &&
920 wxApp::GetComCtl32Version() <= 472 )
925 TBBUTTON
& button
= buttons
[i
];
927 wxZeroMemory(button
);
929 bool isRadio
= false;
930 switch ( tool
->GetStyle() )
932 case wxTOOL_STYLE_CONTROL
:
933 button
.idCommand
= tool
->GetId();
934 // fall through: create just a separator too
936 case wxTOOL_STYLE_SEPARATOR
:
937 button
.fsState
= TBSTATE_ENABLED
;
938 button
.fsStyle
= TBSTYLE_SEP
;
941 case wxTOOL_STYLE_BUTTON
:
942 if ( !HasFlag(wxTB_NOICONS
) )
943 button
.iBitmap
= bitmapId
;
945 if ( HasFlag(wxTB_TEXT
) )
947 const wxString
& label
= tool
->GetLabel();
948 if ( !label
.empty() )
949 button
.iString
= (INT_PTR
)label
.wx_str();
952 button
.idCommand
= tool
->GetId();
954 if ( tool
->IsEnabled() )
955 button
.fsState
|= TBSTATE_ENABLED
;
956 if ( tool
->IsToggled() )
957 button
.fsState
|= TBSTATE_CHECKED
;
959 switch ( tool
->GetKind() )
962 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
966 // the first item in the radio group is checked by
967 // default to be consistent with wxGTK and the menu
969 button
.fsState
|= TBSTATE_CHECKED
;
971 if (tool
->Toggle(true))
973 DoToggleTool(tool
, true);
976 else if ( tool
->IsToggled() )
978 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
979 int prevIndex
= i
- 1;
982 TBBUTTON
& prevButton
= buttons
[prevIndex
];
983 wxToolBarToolBase
*tool
= nodePrev
->GetData();
984 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
987 if ( tool
->Toggle(false) )
988 DoToggleTool(tool
, false);
990 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
991 nodePrev
= nodePrev
->GetPrevious();
1000 button
.fsStyle
= TBSTYLE_CHECK
;
1004 button
.fsStyle
= TBSTYLE_BUTTON
;
1007 case wxITEM_DROPDOWN
:
1008 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1012 wxFAIL_MSG( _T("unexpected toolbar button kind") );
1013 button
.fsStyle
= TBSTYLE_BUTTON
;
1021 lastWasRadio
= isRadio
;
1026 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
1028 wxLogLastError(wxT("TB_ADDBUTTONS"));
1033 // Deal with the controls finally
1034 // ------------------------------
1036 // adjust the controls size to fit nicely in the toolbar
1039 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
1041 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1043 // we calculate the running y coord for vertical toolbars so we need to
1044 // get the items size for all items but for the horizontal ones we
1045 // don't need to deal with the non controls
1046 bool isControl
= tool
->IsControl();
1047 if ( !isControl
&& !IsVertical() )
1050 const RECT r
= wxGetTBItemRect(GetHwnd(), index
);
1053 // can only be control if isVertical
1054 y
+= r
.bottom
- r
.top
;
1059 wxControl
*control
= tool
->GetControl();
1060 wxStaticText
* const staticText
= tool
->GetStaticText();
1062 wxSize size
= control
->GetSize();
1063 wxSize staticTextSize
;
1066 staticTextSize
= staticText
->GetSize();
1067 staticTextSize
.y
+= 3; // margin between control and its label
1070 // the position of the leftmost controls corner
1071 int left
= wxDefaultCoord
;
1073 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1074 #ifdef TB_SETBUTTONINFO
1075 // available in headers, now check whether it is available now
1076 // (during run-time)
1077 if ( wxApp::GetComCtl32Version() >= 471 )
1079 // set the (underlying) separators width to be that of the
1082 tbbi
.cbSize
= sizeof(tbbi
);
1083 tbbi
.dwMask
= TBIF_SIZE
;
1084 tbbi
.cx
= (WORD
)size
.x
;
1085 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1086 tool
->GetId(), (LPARAM
)&tbbi
) )
1088 // the id is probably invalid?
1089 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1093 #endif // comctl32.dll 4.71
1094 // TB_SETBUTTONINFO unavailable
1096 // try adding several separators to fit the controls width
1097 int widthSep
= r
.right
- r
.left
;
1103 tbb
.fsState
= TBSTATE_ENABLED
;
1104 tbb
.fsStyle
= TBSTYLE_SEP
;
1106 size_t nSeparators
= size
.x
/ widthSep
;
1107 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1109 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1110 index
, (LPARAM
)&tbb
) )
1112 wxLogLastError(wxT("TB_INSERTBUTTON"));
1118 // remember the number of separators we used - we'd have to
1119 // delete all of them later
1120 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1122 // adjust the controls width to exactly cover the separators
1123 size
.x
= (nSeparators
+ 1)*widthSep
;
1124 control
->SetSize(size
.x
, wxDefaultCoord
);
1127 // position the control itself correctly vertically centering it on the
1128 // icon area of the toolbar
1129 int height
= r
.bottom
- r
.top
- staticTextSize
.y
;
1131 int diff
= height
- size
.y
;
1132 if ( diff
< 0 || !HasFlag(wxTB_TEXT
) )
1134 // not enough room for the static text
1138 // recalculate height & diff without the staticText control
1139 height
= r
.bottom
- r
.top
;
1140 diff
= height
- size
.y
;
1143 // the control is too high, resize to fit
1144 control
->SetSize(wxDefaultCoord
, height
- 2);
1149 else // enough space for both the control and the label
1161 y
+= height
+ 2 * GetMargins().y
;
1163 else // horizontal toolbar
1165 if ( left
== wxDefaultCoord
)
1171 control
->Move(left
, top
+ (diff
+ 1) / 2);
1174 staticText
->Move(left
+ (size
.x
- staticTextSize
.x
)/2,
1175 r
.bottom
- staticTextSize
.y
);
1179 // the max index is the "real" number of buttons - i.e. counting even the
1180 // separators which we added just for aligning the controls
1183 if ( !IsVertical() )
1185 if ( m_maxRows
== 0 )
1186 // if not set yet, only one row
1189 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1191 // if not set yet, have one column
1193 SetRows(m_nButtons
);
1196 InvalidateBestSize();
1202 // ----------------------------------------------------------------------------
1204 // ----------------------------------------------------------------------------
1206 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
1208 // cast to signed is important as we compare this id with (signed) ints in
1209 // FindById() and without the cast we'd get a positive int from a
1210 // "negative" (i.e. > 32767) WORD
1211 const int id
= (signed short)id_
;
1213 wxToolBarToolBase
*tool
= FindById(id
);
1217 bool toggled
= false; // just to suppress warnings
1219 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1221 if ( tool
->CanBeToggled() )
1223 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1225 // ignore the event when a radio button is released, as this doesn't
1226 // seem to happen at all, and is handled otherwise
1227 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1230 tool
->Toggle(toggled
);
1231 UnToggleRadioGroup(tool
);
1234 // Without the two lines of code below, if the toolbar was repainted during
1235 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1236 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1237 // The Update() call bellow ensures that this won't happen, by repainting
1238 // invalidated areas of the toolbar immediately.
1240 // To complicate matters, the tool would be drawn in depressed state (this
1241 // code is called when mouse button is released, not pressed). That's not
1242 // ideal, having the tool pressed for the duration of OnLeftClick()
1243 // provides the user with useful visual clue that the app is busy reacting
1244 // to the event. So we manually put the tool into pressed state, handle the
1245 // event and then finally restore tool's original state.
1246 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
| TBSTATE_PRESSED
, 0));
1249 bool allowLeftClick
= OnLeftClick(id
, toggled
);
1251 // Restore the unpressed state. Enabled/toggled state might have been
1252 // changed since so take care of it.
1253 if (tool
->IsEnabled())
1254 state
|= TBSTATE_ENABLED
;
1256 state
&= ~TBSTATE_ENABLED
;
1257 if (tool
->IsToggled())
1258 state
|= TBSTATE_CHECKED
;
1260 state
&= ~TBSTATE_CHECKED
;
1261 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
, 0));
1263 // OnLeftClick() can veto the button state change - for buttons which
1264 // may be toggled only, of couse
1265 if ( !allowLeftClick
&& tool
->CanBeToggled() )
1268 tool
->Toggle(!toggled
);
1270 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1276 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1278 WXLPARAM
*WXUNUSED(result
))
1280 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1281 if ( hdr
->code
== TBN_DROPDOWN
)
1283 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1285 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tbhdr
->iItem
);
1286 if ( HandleWindowEvent(evt
) )
1288 // Event got handled, don't display default popup menu
1292 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1293 wxCHECK_MSG( tool
, false, _T("drop down message for unknown tool") );
1295 wxMenu
* const menu
= tool
->GetDropdownMenu();
1299 // Display popup menu below button
1300 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1302 PopupMenu(menu
, r
.left
, r
.bottom
);
1308 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1311 // First check if this applies to us
1313 // the tooltips control created by the toolbar is sometimes Unicode, even
1314 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1315 UINT code
= hdr
->code
;
1316 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1319 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1320 if ( toolTipWnd
!= hdr
->hwndFrom
)
1323 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1324 int id
= (int)ttText
->hdr
.idFrom
;
1326 wxToolBarToolBase
*tool
= FindById(id
);
1328 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1330 wxUnusedVar(lParam
);
1337 // ----------------------------------------------------------------------------
1339 // ----------------------------------------------------------------------------
1341 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1343 wxToolBarBase::SetToolBitmapSize(size
);
1345 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1348 void wxToolBar::SetRows(int nRows
)
1350 if ( nRows
== m_maxRows
)
1352 // avoid resizing the frame uselessly
1356 // TRUE in wParam means to create at least as many rows, FALSE -
1359 ::SendMessage(GetHwnd(), TB_SETROWS
,
1360 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1368 // The button size is bigger than the bitmap size
1369 wxSize
wxToolBar::GetToolSize() const
1371 // TB_GETBUTTONSIZE is supported from version 4.70
1372 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1373 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1374 && !defined (__DIGITALMARS__)
1375 if ( wxApp::GetComCtl32Version() >= 470 )
1377 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1379 return wxSize(LOWORD(dw
), HIWORD(dw
));
1382 #endif // comctl32.dll 4.70+
1385 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1390 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1393 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1395 for ( ; current
; current
= current
->GetNext() )
1398 return current
->GetData();
1400 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1401 size_t separators
= tool
->GetSeparatorsCount();
1403 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1404 // otherwise, skip as many items as the separator count, plus the
1406 index
-= separators
? separators
+ 1 : 1;
1412 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1417 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1419 // MBN: when the point ( x, y ) is close to the toolbar border
1420 // TB_HITTEST returns m_nButtons ( not -1 )
1421 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1422 // it's a separator or there is no tool at all there
1425 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1426 // we don't use the dummy separators hack
1427 #ifdef TB_SETBUTTONINFO
1428 if ( wxApp::GetComCtl32Version() >= 471 )
1430 return m_tools
.Item((size_t)index
)->GetData();
1433 #endif // TB_SETBUTTONINFO
1435 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1439 void wxToolBar::UpdateSize()
1441 wxPoint pos
= GetPosition();
1442 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1443 if (pos
!= GetPosition())
1446 // In case Realize is called after the initial display (IOW the programmer
1447 // may have rebuilt the toolbar) give the frame the option of resizing the
1448 // toolbar to full width again, but only if the parent is a frame and the
1449 // toolbar is managed by the frame. Otherwise assume that some other
1450 // layout mechanism is controlling the toolbar size and leave it alone.
1451 SendSizeEventToParent();
1454 // ----------------------------------------------------------------------------
1456 // ---------------------------------------------------------------------------
1458 // get the TBSTYLE of the given toolbar window
1459 long wxToolBar::GetMSWToolbarStyle() const
1461 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1464 void wxToolBar::SetWindowStyleFlag(long style
)
1466 // the style bits whose changes force us to recreate the toolbar
1467 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1469 const long styleOld
= GetWindowStyle();
1471 wxToolBarBase::SetWindowStyleFlag(style
);
1473 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1474 // also fatal as we'd then try to recreate the toolbar when it's just being
1476 if ( GetToolsCount() &&
1477 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1479 // to remove the text labels, simply re-realizing the toolbar is enough
1480 // but I don't know of any way to add the text to an existing toolbar
1481 // other than by recreating it entirely
1486 // ----------------------------------------------------------------------------
1488 // ----------------------------------------------------------------------------
1490 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1492 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1493 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1496 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1498 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1499 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1502 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1504 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1505 // without, so we really need to delete the button and recreate it here
1506 wxFAIL_MSG( _T("not implemented") );
1509 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1511 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1514 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1516 tool
->SetNormalBitmap(bitmap
);
1521 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1523 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1526 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1528 tool
->SetDisabledBitmap(bitmap
);
1533 // ----------------------------------------------------------------------------
1535 // ----------------------------------------------------------------------------
1537 // Responds to colour changes, and passes event on to children.
1538 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1540 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1542 // Remap the buttons
1545 // Relayout the toolbar
1546 int nrows
= m_maxRows
;
1547 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1552 // let the event propagate further
1556 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1558 if ( event
.Leaving() )
1562 OnMouseEnter(wxID_ANY
);
1570 if ( event
.RightDown() )
1572 // find the tool under the mouse
1573 wxCoord x
= 0, y
= 0;
1574 event
.GetPosition(&x
, &y
);
1576 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1577 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1585 // This handler is required to allow the toolbar to be set to a non-default
1586 // colour: for example, when it must blend in with a notebook page.
1587 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1589 RECT rect
= wxGetClientRect(GetHwnd());
1591 wxDC
*dc
= event
.GetDC();
1593 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
->GetImpl();
1594 HDC hdc
= GetHdcOf(*impl
);
1596 int majorVersion
, minorVersion
;
1597 wxGetOsVersion(& majorVersion
, & minorVersion
);
1600 // we may need to draw themed colour so that we appear correctly on
1601 // e.g. notebook page under XP with themes but only do it if the parent
1602 // draws themed background itself
1603 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1605 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1609 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1613 // it can also return S_FALSE which seems to simply say that it
1614 // didn't draw anything but no error really occurred
1616 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1620 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
1621 if ( !UseBgCol() && majorVersion
>= 6 )
1623 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1626 wxUxThemeHandle
hTheme(this, L
"REBAR");
1629 wxRect rect
= GetClientRect();
1630 wxCopyRectToRECT(rect
, r
);
1632 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, hdc
, 0, 0, & r
, NULL
);
1636 // it can also return S_FALSE which seems to simply say that it
1637 // didn't draw anything but no error really occurred
1639 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1643 #endif // wxUSE_UXTHEME
1645 // we need to always draw our background under XP, as otherwise it doesn't
1646 // appear correctly with some themes (e.g. Zune one)
1647 if ( majorVersion
== 5 ||
1648 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1650 // do draw our background
1652 // notice that this 'dumb' implementation may cause flicker for some of
1653 // the controls in which case they should intercept wxEraseEvent and
1654 // process it themselves somehow
1655 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1657 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1658 ::FillRect(hdc
, &rect
, hBrush
);
1660 else // we have no non default background colour
1662 // let the system do it for us
1667 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1669 // wait until we have some tools
1670 if ( !GetToolsCount() )
1673 // calculate our minor dimension ourselves - we're confusing the standard
1674 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1675 const RECT r
= wxGetTBItemRect(GetHwnd(), 0);
1683 w
= r
.right
- r
.left
;
1686 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1693 if (HasFlag( wxTB_FLAT
))
1694 h
= r
.bottom
- r
.top
- 3;
1696 h
= r
.bottom
- r
.top
;
1699 // FIXME: hardcoded separator line height...
1700 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1705 if ( MAKELPARAM(w
, h
) != lParam
)
1707 // size really changed
1711 // message processed
1715 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1717 // erase any dummy separators which were used
1718 // for aligning the controls if any here
1720 // first of all, are there any controls at all?
1721 wxToolBarToolsList::compatibility_iterator node
;
1722 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1724 if ( node
->GetData()->IsControl() )
1729 // no controls, nothing to erase
1732 wxSize clientSize
= GetClientSize();
1733 int majorVersion
, minorVersion
;
1734 wxGetOsVersion(& majorVersion
, & minorVersion
);
1736 // prepare the DC on which we'll be drawing
1737 wxClientDC
dc(this);
1738 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1739 dc
.SetPen(*wxTRANSPARENT_PEN
);
1742 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1743 // nothing to redraw anyhow
1747 wxCopyRECTToRect(r
, rectUpdate
);
1749 dc
.SetClippingRegion(rectUpdate
);
1751 // draw the toolbar tools, separators &c normally
1752 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1754 // for each control in the toolbar find all the separators intersecting it
1757 // NB: this is really the only way to do it as we don't know if a separator
1758 // corresponds to a control (i.e. is a dummy one) or a real one
1760 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1762 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1763 if ( tool
->IsControl() )
1765 // get the control rect in our client coords
1766 wxControl
*control
= tool
->GetControl();
1767 wxStaticText
*staticText
= tool
->GetStaticText();
1768 wxRect rectCtrl
= control
->GetRect();
1769 wxRect
rectStaticText(0,0,0,0);
1772 rectStaticText
= staticText
->GetRect();
1775 // iterate over all buttons
1777 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1778 for ( int n
= 0; n
< count
; n
++ )
1780 // is it a separator?
1781 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1784 wxLogDebug(_T("TB_GETBUTTON failed?"));
1789 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1792 // get the bounding rect of the separator
1793 RECT r
= wxGetTBItemRect(GetHwnd(), n
);
1797 // does it intersect the control?
1799 wxCopyRECTToRect(r
, rectItem
);
1800 if ( rectCtrl
.Intersects(rectItem
) || (staticText
&& rectStaticText
.Intersects(rectItem
)))
1802 // yes, do erase it!
1804 bool haveRefreshed
= false;
1807 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1809 // Don't use DrawThemeBackground
1811 else if ( !UseBgCol() && majorVersion
>= 6 )
1813 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1816 wxUxThemeHandle
hTheme(this, L
"REBAR");
1820 // Draw the whole background since the pattern may be position sensitive;
1821 // but clip it to the area of interest.
1823 r
.right
= clientSize
.x
;
1825 r
.bottom
= clientSize
.y
;
1827 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
1828 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, GetHdcOf(*impl
), 0, 0, & r
, & clipRect
);
1830 haveRefreshed
= true;
1836 dc
.DrawRectangle(rectItem
);
1839 if ( rectCtrl
.Intersects(rectItem
) )
1841 // Necessary in case we use a no-paint-on-size
1842 // style in the parent: the controls can disappear
1843 control
->Refresh(false);
1846 if ( staticText
&& rectStaticText
.Intersects(rectItem
) )
1848 // Necessary in case we use a no-paint-on-size
1849 // style in the parent: the controls can disappear
1850 staticText
->Refresh(false);
1859 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1861 wxCoord x
= GET_X_LPARAM(lParam
),
1862 y
= GET_Y_LPARAM(lParam
);
1863 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1865 // has the current tool changed?
1866 if ( tool
!= m_pInTool
)
1869 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1873 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1878 // we don't handle mouse moves, so always pass the message to
1879 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1880 HandleMouseMove(wParam
, lParam
);
1884 if ( HandleSize(wParam
, lParam
) )
1890 // refreshing the controls in the toolbar inside a composite window
1891 // results in an endless stream of WM_PAINT messages -- and seems
1892 // to be unnecessary anyhow as everything works just fine without
1893 // any special workarounds in this case
1894 if ( !IsDoubleBuffered() && HandlePaint(wParam
, lParam
) )
1897 #endif // __WXWINCE__
1900 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1903 // ----------------------------------------------------------------------------
1904 // private functions
1905 // ----------------------------------------------------------------------------
1907 #ifdef wxREMAP_BUTTON_COLOURS
1909 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1915 wxLogLastError(_T("CreateCompatibleDC"));
1920 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1924 wxLogLastError(_T("SelectObject"));
1929 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1931 for ( int i
= 0; i
< width
; i
++ )
1933 for ( int j
= 0; j
< height
; j
++ )
1935 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1937 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1939 COLORREF col
= cmap
[k
].from
;
1940 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1941 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1942 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1944 if ( cmap
[k
].to
!= pixel
)
1945 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1955 #endif // wxREMAP_BUTTON_COLOURS
1957 #endif // wxUSE_TOOLBAR