1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tbar95.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
45 #include "wx/sysopt.h"
47 #include "wx/msw/private.h"
50 #include "wx/msw/uxtheme.h"
53 // this define controls whether the code for button colours remapping (only
54 // useful for 16 or 256 colour images) is active at all, it's always turned off
55 // for CE where it doesn't compile (and is probably not needed anyhow) and may
56 // also be turned off for other systems if you always use 24bpp images and so
59 #define wxREMAP_BUTTON_COLOURS
60 #endif // !__WXWINCE__
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // these standard constants are not always defined in compilers headers
70 #define TBSTYLE_LIST 0x1000
71 #define TBSTYLE_FLAT 0x0800
74 #ifndef TBSTYLE_TRANSPARENT
75 #define TBSTYLE_TRANSPARENT 0x8000
78 #ifndef TBSTYLE_TOOLTIPS
79 #define TBSTYLE_TOOLTIPS 0x0100
84 #define TB_SETSTYLE (WM_USER + 56)
85 #define TB_GETSTYLE (WM_USER + 57)
89 #define TB_HITTEST (WM_USER + 69)
93 #define TB_GETMAXSIZE (WM_USER + 83)
96 // these values correspond to those used by comctl32.dll
97 #define DEFAULTBITMAPX 16
98 #define DEFAULTBITMAPY 15
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
)
153 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
154 : wxToolBarToolBase(tbar
, control
)
159 virtual void SetLabel(const wxString
& label
)
161 if ( label
== m_label
)
164 wxToolBarToolBase::SetLabel(label
);
166 // we need to update the label shown in the toolbar because it has a
167 // pointer to the internal buffer of the old label
169 // TODO: use TB_SETBUTTONINFO
172 // set/get the number of separators which we use to cover the space used by
173 // a control in the toolbar
174 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
175 size_t GetSeparatorsCount() const { return m_nSepCount
; }
180 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
183 // ============================================================================
185 // ============================================================================
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
192 const wxString
& label
,
193 const wxBitmap
& bmpNormal
,
194 const wxBitmap
& bmpDisabled
,
196 wxObject
*clientData
,
197 const wxString
& shortHelp
,
198 const wxString
& longHelp
)
200 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
201 clientData
, shortHelp
, longHelp
);
204 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
206 return new wxToolBarTool(this, control
);
209 // ----------------------------------------------------------------------------
210 // wxToolBar construction
211 // ----------------------------------------------------------------------------
213 void wxToolBar::Init()
216 m_disabledImgList
= NULL
;
220 m_defaultWidth
= DEFAULTBITMAPX
;
221 m_defaultHeight
= DEFAULTBITMAPY
;
226 bool wxToolBar::Create(wxWindow
*parent
,
231 const wxString
& name
)
233 // common initialisation
234 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
237 // MSW-specific initialisation
238 if ( !MSWCreateToolbar(pos
, size
) )
241 wxSetCCUnicodeFormat(GetHwnd());
243 // workaround for flat toolbar on Windows XP classic style: we have to set
244 // the style after creating the control; doing it at creation time doesn't work
246 if ( style
& wxTB_FLAT
)
248 LRESULT style
= GetMSWToolbarStyle();
250 if ( !(style
& TBSTYLE_FLAT
) )
251 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
253 #endif // wxUSE_UXTHEME
258 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
260 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
263 // toolbar-specific post initialisation
264 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
269 void wxToolBar::Recreate()
271 const HWND hwndOld
= GetHwnd();
274 // we haven't been created yet, no need to recreate
278 // get the position and size before unsubclassing the old toolbar
279 const wxPoint pos
= GetPosition();
280 const wxSize size
= GetSize();
284 if ( !MSWCreateToolbar(pos
, size
) )
287 wxFAIL_MSG( _T("recreating the toolbar failed") );
292 // reparent all our children under the new toolbar
293 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
295 node
= node
->GetNext() )
297 wxWindow
*win
= node
->GetData();
298 if ( !win
->IsTopLevel() )
299 ::SetParent(GetHwndOf(win
), GetHwnd());
302 // only destroy the old toolbar now --
303 // after all the children had been reparented
304 ::DestroyWindow(hwndOld
);
306 // it is for the old bitmap control and can't be used with the new one
309 ::DeleteObject((HBITMAP
) m_hBitmap
);
313 if ( m_disabledImgList
)
315 delete m_disabledImgList
;
316 m_disabledImgList
= NULL
;
322 wxToolBar::~wxToolBar()
324 // we must refresh the frame size when the toolbar is deleted but the frame
325 // is not - otherwise toolbar leaves a hole in the place it used to occupy
326 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
327 if ( frame
&& !frame
->IsBeingDeleted() )
328 frame
->SendSizeEvent();
331 ::DeleteObject((HBITMAP
) m_hBitmap
);
333 delete m_disabledImgList
;
336 wxSize
wxToolBar::DoGetBestSize() const
341 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
343 // maybe an old (< 0x400) Windows version? try to approximate the
344 // toolbar size ourselves
345 sizeBest
= GetToolSize();
346 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
347 sizeBest
.x
*= GetToolsCount();
349 // reverse horz and vertical components if necessary
350 if ( HasFlag(wxTB_VERTICAL
) )
353 sizeBest
.x
= sizeBest
.y
;
359 sizeBest
.x
= size
.cx
;
360 sizeBest
.y
= size
.cy
;
363 CacheBestSize(sizeBest
);
368 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
370 // toolbars never have border, giving one to them results in broken
372 WXDWORD msStyle
= wxControl::MSWGetStyle
374 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
377 if ( !(style
& wxTB_NO_TOOLTIPS
) )
378 msStyle
|= TBSTYLE_TOOLTIPS
;
380 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
382 // static as it doesn't change during the program lifetime
383 static const int s_verComCtl
= wxApp::GetComCtl32Version();
385 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
386 // style with 6.00 (part of Windows XP) leads to the toolbar with
387 // incorrect background colour - and not using it still results in the
388 // correct (flat) toolbar, so don't use it there
389 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
390 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
392 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
393 msStyle
|= TBSTYLE_LIST
;
396 if ( style
& wxTB_NODIVIDER
)
397 msStyle
|= CCS_NODIVIDER
;
399 if ( style
& wxTB_NOALIGN
)
400 msStyle
|= CCS_NOPARENTALIGN
;
402 if ( style
& wxTB_VERTICAL
)
408 // ----------------------------------------------------------------------------
409 // adding/removing tools
410 // ----------------------------------------------------------------------------
412 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
414 // nothing special to do here - we really create the toolbar buttons in
418 InvalidateBestSize();
422 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
424 // the main difficulty we have here is with the controls in the toolbars:
425 // as we (sometimes) use several separators to cover up the space used by
426 // them, the indices are not the same for us and the toolbar
428 // first determine the position of the first button to delete: it may be
429 // different from pos if we use several separators to cover the space used
431 wxToolBarToolsList::compatibility_iterator node
;
432 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
434 wxToolBarToolBase
*tool2
= node
->GetData();
437 // let node point to the next node in the list
438 node
= node
->GetNext();
443 if ( tool2
->IsControl() )
444 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
447 // now determine the number of buttons to delete and the area taken by them
448 size_t nButtonsToDelete
= 1;
450 // get the size of the button we're going to delete
452 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
454 wxLogLastError(_T("TB_GETITEMRECT"));
457 int width
= r
.right
- r
.left
;
459 if ( tool
->IsControl() )
461 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
462 width
*= nButtonsToDelete
;
463 tool
->GetControl()->Destroy();
466 // do delete all buttons
467 m_nButtons
-= nButtonsToDelete
;
468 while ( nButtonsToDelete
-- > 0 )
470 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
472 wxLogLastError(wxT("TB_DELETEBUTTON"));
480 // and finally reposition all the controls after this button (the toolbar
481 // takes care of all normal items)
482 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
484 wxToolBarToolBase
*tool2
= node
->GetData();
485 if ( tool2
->IsControl() )
488 wxControl
*control
= tool2
->GetControl();
489 control
->GetPosition(&x
, NULL
);
490 control
->Move(x
- width
, wxDefaultCoord
);
494 InvalidateBestSize();
499 void wxToolBar::CreateDisabledImageList()
501 if (m_disabledImgList
!= NULL
)
503 delete m_disabledImgList
;
504 m_disabledImgList
= NULL
;
507 // as we can't use disabled image list with older versions of comctl32.dll,
508 // don't even bother creating it
509 if ( wxApp::GetComCtl32Version() >= 470 )
511 // search for the first disabled button img in the toolbar, if any
512 for ( wxToolBarToolsList::compatibility_iterator
513 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
515 wxToolBarToolBase
*tool
= node
->GetData();
516 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
517 if ( bmpDisabled
.Ok() )
519 m_disabledImgList
= new wxImageList
523 bmpDisabled
.GetMask() != NULL
,
530 // we don't have any disabled bitmaps
534 bool wxToolBar::Realize()
536 const size_t nTools
= GetToolsCount();
541 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
543 #ifdef wxREMAP_BUTTON_COLOURS
544 // don't change the values of these constants, they can be set from the
545 // user code via wxSystemOptions
554 // the user-specified option overrides anything, but if it wasn't set, only
555 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
556 // much worse after remapping
557 static const wxChar
*remapOption
= wxT("msw.remap");
558 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
559 ? wxSystemOptions::GetOptionInt(remapOption
)
560 : wxDisplayDepth() <= 8 ? Remap_Buttons
563 #endif // wxREMAP_BUTTON_COLOURS
565 // delete all old buttons, if any
566 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
568 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
570 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
574 // First, add the bitmap: we use one bitmap for all toolbar buttons
575 // ----------------------------------------------------------------
577 wxToolBarToolsList::compatibility_iterator node
;
581 if ( HasFlag(wxTB_NOICONS
) )
583 // no icons, don't leave space for them
587 else // do show icons
589 // if we already have a bitmap, we'll replace the existing one --
590 // otherwise we'll install a new one
591 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
593 sizeBmp
.x
= m_defaultWidth
;
594 sizeBmp
.y
= m_defaultHeight
;
596 const wxCoord totalBitmapWidth
= m_defaultWidth
*
597 wx_truncate_cast(wxCoord
, nTools
),
598 totalBitmapHeight
= m_defaultHeight
;
600 // Create a bitmap and copy all the tool bitmaps into it
601 wxMemoryDC dcAllButtons
;
602 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
603 dcAllButtons
.SelectObject(bitmap
);
605 #ifdef wxREMAP_BUTTON_COLOURS
606 if ( remapValue
!= Remap_TransparentBg
)
607 #endif // wxREMAP_BUTTON_COLOURS
609 // VZ: why do we hardcode grey colour for CE?
610 dcAllButtons
.SetBackground(wxBrush(
612 wxColour(0xc0, 0xc0, 0xc0)
613 #else // !__WXWINCE__
614 GetBackgroundColour()
615 #endif // __WXWINCE__/!__WXWINCE__
617 dcAllButtons
.Clear();
620 m_hBitmap
= bitmap
.GetHBITMAP();
621 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
623 #ifdef wxREMAP_BUTTON_COLOURS
624 if ( remapValue
== Remap_Bg
)
626 dcAllButtons
.SelectObject(wxNullBitmap
);
628 // Even if we're not remapping the bitmap
629 // content, we still have to remap the background.
630 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
631 totalBitmapWidth
, totalBitmapHeight
);
633 dcAllButtons
.SelectObject(bitmap
);
635 #endif // wxREMAP_BUTTON_COLOURS
637 // the button position
640 // the number of buttons (not separators)
643 CreateDisabledImageList();
644 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
646 wxToolBarToolBase
*tool
= node
->GetData();
647 if ( tool
->IsButton() )
649 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
651 const int w
= bmp
.GetWidth();
652 const int h
= bmp
.GetHeight();
656 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
657 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
659 // notice the last parameter: do use mask
660 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
664 wxFAIL_MSG( _T("invalid tool button bitmap") );
667 // also deal with disabled bitmap if we want to use them
668 if ( m_disabledImgList
)
670 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
671 #if wxUSE_IMAGE && wxUSE_WXDIB
672 if ( !bmpDisabled
.Ok() )
674 // no disabled bitmap specified but we still need to
675 // fill the space in the image list with something, so
676 // we grey out the normal bitmap
678 wxCreateGreyedImage(bmp
.ConvertToImage(), imgGreyed
);
680 #ifdef wxREMAP_BUTTON_COLOURS
681 if ( remapValue
== Remap_Buttons
)
683 // we need to have light grey background colour for
684 // MapBitmap() to work correctly
685 for ( int y
= 0; y
< h
; y
++ )
687 for ( int x
= 0; x
< w
; x
++ )
689 if ( imgGreyed
.IsTransparent(x
, y
) )
690 imgGreyed
.SetRGB(x
, y
,
692 wxLIGHT_GREY
->Green(),
693 wxLIGHT_GREY
->Blue());
697 #endif // wxREMAP_BUTTON_COLOURS
699 bmpDisabled
= wxBitmap(imgGreyed
);
701 #endif // wxUSE_IMAGE
703 #ifdef wxREMAP_BUTTON_COLOURS
704 if ( remapValue
== Remap_Buttons
)
705 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
706 #endif // wxREMAP_BUTTON_COLOURS
708 m_disabledImgList
->Add(bmpDisabled
);
711 // still inc width and number of buttons because otherwise the
712 // subsequent buttons will all be shifted which is rather confusing
713 // (and like this you'd see immediately which bitmap was bad)
719 dcAllButtons
.SelectObject(wxNullBitmap
);
721 // don't delete this HBITMAP!
722 bitmap
.SetHBITMAP(0);
724 #ifdef wxREMAP_BUTTON_COLOURS
725 if ( remapValue
== Remap_Buttons
)
727 // Map to system colours
728 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
729 totalBitmapWidth
, totalBitmapHeight
);
731 #endif // wxREMAP_BUTTON_COLOURS
733 bool addBitmap
= true;
735 if ( oldToolBarBitmap
)
737 #ifdef TB_REPLACEBITMAP
738 if ( wxApp::GetComCtl32Version() >= 400 )
740 TBREPLACEBITMAP replaceBitmap
;
741 replaceBitmap
.hInstOld
= NULL
;
742 replaceBitmap
.hInstNew
= NULL
;
743 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
744 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
745 replaceBitmap
.nButtons
= nButtons
;
746 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
747 0, (LPARAM
) &replaceBitmap
) )
749 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
752 ::DeleteObject(oldToolBarBitmap
);
758 #endif // TB_REPLACEBITMAP
760 // we can't replace the old bitmap, so we will add another one
761 // (awfully inefficient, but what else to do?) and shift the bitmap
762 // indices accordingly
765 bitmapId
= m_nButtons
;
769 if ( addBitmap
) // no old bitmap or we can't replace it
771 TBADDBITMAP addBitmap
;
773 addBitmap
.nID
= (UINT
) hBitmap
;
774 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
775 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
777 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
781 if ( m_disabledImgList
)
783 HIMAGELIST oldImageList
= (HIMAGELIST
)
784 ::SendMessage(GetHwnd(),
785 TB_SETDISABLEDIMAGELIST
,
787 (LPARAM
)GetHimagelistOf(m_disabledImgList
));
789 // delete previous image list if any
791 ::DeleteObject( oldImageList
);
795 // don't call SetToolBitmapSize() as we don't want to change the values of
796 // m_defaultWidth/Height
797 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
798 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
800 wxLogLastError(_T("TB_SETBITMAPSIZE"));
803 // Next add the buttons and separators
804 // -----------------------------------
806 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
808 // this array will hold the indices of all controls in the toolbar
809 wxArrayInt controlIds
;
811 bool lastWasRadio
= false;
813 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
815 wxToolBarToolBase
*tool
= node
->GetData();
817 // don't add separators to the vertical toolbar with old comctl32.dll
818 // versions as they didn't handle this properly
819 if ( isVertical
&& tool
->IsSeparator() &&
820 wxApp::GetComCtl32Version() <= 472 )
825 TBBUTTON
& button
= buttons
[i
];
827 wxZeroMemory(button
);
829 bool isRadio
= false;
830 switch ( tool
->GetStyle() )
832 case wxTOOL_STYLE_CONTROL
:
833 button
.idCommand
= tool
->GetId();
834 // fall through: create just a separator too
836 case wxTOOL_STYLE_SEPARATOR
:
837 button
.fsState
= TBSTATE_ENABLED
;
838 button
.fsStyle
= TBSTYLE_SEP
;
841 case wxTOOL_STYLE_BUTTON
:
842 if ( !HasFlag(wxTB_NOICONS
) )
843 button
.iBitmap
= bitmapId
;
845 if ( HasFlag(wxTB_TEXT
) )
847 const wxString
& label
= tool
->GetLabel();
848 if ( !label
.empty() )
849 button
.iString
= (int)label
.c_str();
852 button
.idCommand
= tool
->GetId();
854 if ( tool
->IsEnabled() )
855 button
.fsState
|= TBSTATE_ENABLED
;
856 if ( tool
->IsToggled() )
857 button
.fsState
|= TBSTATE_CHECKED
;
859 switch ( tool
->GetKind() )
862 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
866 // the first item in the radio group is checked by
867 // default to be consistent with wxGTK and the menu
869 button
.fsState
|= TBSTATE_CHECKED
;
871 if (tool
->Toggle(true))
873 DoToggleTool(tool
, true);
876 else if (tool
->IsToggled())
878 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
879 int prevIndex
= i
- 1;
882 TBBUTTON
& prevButton
= buttons
[prevIndex
];
883 wxToolBarToolBase
*tool
= nodePrev
->GetData();
884 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
887 if ( tool
->Toggle(false) )
888 DoToggleTool(tool
, false);
890 prevButton
.fsState
= TBSTATE_ENABLED
;
891 nodePrev
= nodePrev
->GetPrevious();
900 button
.fsStyle
= TBSTYLE_CHECK
;
904 button
.fsStyle
= TBSTYLE_BUTTON
;
908 wxFAIL_MSG( _T("unexpected toolbar button kind") );
909 button
.fsStyle
= TBSTYLE_BUTTON
;
917 lastWasRadio
= isRadio
;
922 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
924 wxLogLastError(wxT("TB_ADDBUTTONS"));
929 // Deal with the controls finally
930 // ------------------------------
932 // adjust the controls size to fit nicely in the toolbar
935 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
937 wxToolBarToolBase
*tool
= node
->GetData();
939 // we calculate the running y coord for vertical toolbars so we need to
940 // get the items size for all items but for the horizontal ones we
941 // don't need to deal with the non controls
942 bool isControl
= tool
->IsControl();
943 if ( !isControl
&& !isVertical
)
946 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
947 // latter only appeared in v4.70 of comctl32.dll
949 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
950 index
, (LPARAM
)(LPRECT
)&r
) )
952 wxLogLastError(wxT("TB_GETITEMRECT"));
957 // can only be control if isVertical
958 y
+= r
.bottom
- r
.top
;
963 wxControl
*control
= tool
->GetControl();
964 wxSize size
= control
->GetSize();
966 // the position of the leftmost controls corner
967 int left
= wxDefaultCoord
;
969 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
970 #ifdef TB_SETBUTTONINFO
971 // available in headers, now check whether it is available now
973 if ( wxApp::GetComCtl32Version() >= 471 )
975 // set the (underlying) separators width to be that of the
978 tbbi
.cbSize
= sizeof(tbbi
);
979 tbbi
.dwMask
= TBIF_SIZE
;
980 tbbi
.cx
= (WORD
)size
.x
;
981 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
982 tool
->GetId(), (LPARAM
)&tbbi
) )
984 // the id is probably invalid?
985 wxLogLastError(wxT("TB_SETBUTTONINFO"));
989 #endif // comctl32.dll 4.71
990 // TB_SETBUTTONINFO unavailable
992 // try adding several separators to fit the controls width
993 int widthSep
= r
.right
- r
.left
;
999 tbb
.fsState
= TBSTATE_ENABLED
;
1000 tbb
.fsStyle
= TBSTYLE_SEP
;
1002 size_t nSeparators
= size
.x
/ widthSep
;
1003 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1005 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1006 index
, (LPARAM
)&tbb
) )
1008 wxLogLastError(wxT("TB_INSERTBUTTON"));
1014 // remember the number of separators we used - we'd have to
1015 // delete all of them later
1016 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1018 // adjust the controls width to exactly cover the separators
1019 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
1022 // position the control itself correctly vertically
1023 int height
= r
.bottom
- r
.top
;
1024 int diff
= height
- size
.y
;
1027 // the control is too high, resize to fit
1028 control
->SetSize(wxDefaultCoord
, height
- 2);
1039 y
+= height
+ 2 * GetMargins().y
;
1041 else // horizontal toolbar
1043 if ( left
== wxDefaultCoord
)
1049 control
->Move(left
, top
+ (diff
+ 1) / 2);
1052 // the max index is the "real" number of buttons - i.e. counting even the
1053 // separators which we added just for aligning the controls
1058 if ( m_maxRows
== 0 )
1059 // if not set yet, only one row
1062 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1064 if ( m_maxRows
== 0 )
1065 // if not set yet, have one column
1066 SetRows(m_nButtons
);
1069 InvalidateBestSize();
1075 // ----------------------------------------------------------------------------
1077 // ----------------------------------------------------------------------------
1079 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
1081 wxToolBarToolBase
*tool
= FindById((int)id
);
1085 bool toggled
= false; // just to suppress warnings
1087 if ( tool
->CanBeToggled() )
1089 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1090 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1092 // ignore the event when a radio button is released, as this doesn't
1093 // seem to happen at all, and is handled otherwise
1094 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1097 tool
->Toggle(toggled
);
1098 UnToggleRadioGroup(tool
);
1101 // OnLeftClick() can veto the button state change - for buttons which
1102 // may be toggled only, of couse
1103 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1106 tool
->Toggle(!toggled
);
1108 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1114 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1116 WXLPARAM
*WXUNUSED(result
))
1118 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1121 // First check if this applies to us
1122 NMHDR
*hdr
= (NMHDR
*)lParam
;
1124 // the tooltips control created by the toolbar is sometimes Unicode, even
1125 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1126 UINT code
= hdr
->code
;
1127 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1130 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1131 if ( toolTipWnd
!= hdr
->hwndFrom
)
1134 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1135 int id
= (int)ttText
->hdr
.idFrom
;
1137 wxToolBarToolBase
*tool
= FindById(id
);
1139 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1141 wxUnusedVar(lParam
);
1148 // ----------------------------------------------------------------------------
1150 // ----------------------------------------------------------------------------
1152 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1154 wxToolBarBase::SetToolBitmapSize(size
);
1156 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1159 void wxToolBar::SetRows(int nRows
)
1161 if ( nRows
== m_maxRows
)
1163 // avoid resizing the frame uselessly
1167 // TRUE in wParam means to create at least as many rows, FALSE -
1170 ::SendMessage(GetHwnd(), TB_SETROWS
,
1171 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1179 // The button size is bigger than the bitmap size
1180 wxSize
wxToolBar::GetToolSize() const
1182 // TB_GETBUTTONSIZE is supported from version 4.70
1183 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1184 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1185 && !defined (__DIGITALMARS__)
1186 if ( wxApp::GetComCtl32Version() >= 470 )
1188 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1190 return wxSize(LOWORD(dw
), HIWORD(dw
));
1193 #endif // comctl32.dll 4.70+
1196 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1201 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1204 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1206 for ( ; current
; current
= current
->GetNext() )
1209 return current
->GetData();
1211 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1212 size_t separators
= tool
->GetSeparatorsCount();
1214 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1215 // otherwise, skip as many items as the separator count, plus the
1217 index
-= separators
? separators
+ 1 : 1;
1223 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1228 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1230 // MBN: when the point ( x, y ) is close to the toolbar border
1231 // TB_HITTEST returns m_nButtons ( not -1 )
1232 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1233 // it's a separator or there is no tool at all there
1234 return (wxToolBarToolBase
*)NULL
;
1236 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1237 // we don't use the dummy separators hack
1238 #ifdef TB_SETBUTTONINFO
1239 if ( wxApp::GetComCtl32Version() >= 471 )
1241 return m_tools
.Item((size_t)index
)->GetData();
1244 #endif // TB_SETBUTTONINFO
1246 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1250 void wxToolBar::UpdateSize()
1252 wxPoint pos
= GetPosition();
1253 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1254 if (pos
!= GetPosition())
1257 // In case Realize is called after the initial display (IOW the programmer
1258 // may have rebuilt the toolbar) give the frame the option of resizing the
1259 // toolbar to full width again, but only if the parent is a frame and the
1260 // toolbar is managed by the frame. Otherwise assume that some other
1261 // layout mechanism is controlling the toolbar size and leave it alone.
1262 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1263 if ( frame
&& frame
->GetToolBar() == this )
1265 frame
->SendSizeEvent();
1269 // ----------------------------------------------------------------------------
1271 // ---------------------------------------------------------------------------
1273 // get the TBSTYLE of the given toolbar window
1274 long wxToolBar::GetMSWToolbarStyle() const
1276 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1279 void wxToolBar::SetWindowStyleFlag(long style
)
1281 // the style bits whose changes force us to recreate the toolbar
1282 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1284 const long styleOld
= GetWindowStyle();
1286 wxToolBarBase::SetWindowStyleFlag(style
);
1288 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1289 // also fatal as we'd then try to recreate the toolbar when it's just being
1291 if ( GetToolsCount() &&
1292 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1294 // to remove the text labels, simply re-realizing the toolbar is enough
1295 // but I don't know of any way to add the text to an existing toolbar
1296 // other than by recreating it entirely
1301 // ----------------------------------------------------------------------------
1303 // ----------------------------------------------------------------------------
1305 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1307 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1308 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1311 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1313 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1314 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1317 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1319 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1320 // without, so we really need to delete the button and recreate it here
1321 wxFAIL_MSG( _T("not implemented") );
1324 // ----------------------------------------------------------------------------
1326 // ----------------------------------------------------------------------------
1328 // Responds to colour changes, and passes event on to children.
1329 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1331 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1333 // Remap the buttons
1336 // Relayout the toolbar
1337 int nrows
= m_maxRows
;
1338 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1343 // let the event propagate further
1347 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1349 if (event
.Leaving() && m_pInTool
)
1356 if ( event
.RightDown() )
1358 // find the tool under the mouse
1359 wxCoord x
= 0, y
= 0;
1360 event
.GetPosition(&x
, &y
);
1362 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1363 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1371 // This handler is required to allow the toolbar to be set to a non-default
1372 // colour: for example, when it must blend in with a notebook page.
1373 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1375 RECT rect
= wxGetClientRect(GetHwnd());
1376 HDC hdc
= GetHdcOf((*event
.GetDC()));
1379 // we may need to draw themed colour so that we appear correctly on
1380 // e.g. notebook page under XP with themes but only do it if the parent
1381 // draws themed background itself
1382 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1384 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1388 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1392 // it can also return S_FALSE which seems to simply say that it
1393 // didn't draw anything but no error really occurred
1395 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1398 #endif // wxUSE_UXTHEME
1400 if ( UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1402 // do draw our background
1404 // notice that this 'dumb' implementation may cause flicker for some of
1405 // the controls in which case they should intercept wxEraseEvent and
1406 // process it themselves somehow
1407 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1409 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1410 ::FillRect(hdc
, &rect
, hBrush
);
1412 else // we have no non default background colour
1414 // let the system do it for us
1419 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1421 // calculate our minor dimension ourselves - we're confusing the standard
1422 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1424 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1428 if ( GetWindowStyle() & wxTB_VERTICAL
)
1430 w
= r
.right
- r
.left
;
1433 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1440 if (HasFlag( wxTB_FLAT
))
1441 h
= r
.bottom
- r
.top
- 3;
1443 h
= r
.bottom
- r
.top
;
1446 // FIXME: hardcoded separator line height...
1447 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1452 if ( MAKELPARAM(w
, h
) != lParam
)
1454 // size really changed
1458 // message processed
1465 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1467 // erase any dummy separators which were used
1468 // for aligning the controls if any here
1470 // first of all, are there any controls at all?
1471 wxToolBarToolsList::compatibility_iterator node
;
1472 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1474 if ( node
->GetData()->IsControl() )
1479 // no controls, nothing to erase
1482 // prepare the DC on which we'll be drawing
1483 wxClientDC
dc(this);
1484 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1485 dc
.SetPen(*wxTRANSPARENT_PEN
);
1488 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1489 // nothing to redraw anyhow
1493 wxCopyRECTToRect(r
, rectUpdate
);
1495 dc
.SetClippingRegion(rectUpdate
);
1497 // draw the toolbar tools, separators &c normally
1498 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1500 // for each control in the toolbar find all the separators intersecting it
1503 // NB: this is really the only way to do it as we don't know if a separator
1504 // corresponds to a control (i.e. is a dummy one) or a real one
1506 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1508 wxToolBarToolBase
*tool
= node
->GetData();
1509 if ( tool
->IsControl() )
1511 // get the control rect in our client coords
1512 wxControl
*control
= tool
->GetControl();
1513 wxRect rectCtrl
= control
->GetRect();
1515 // iterate over all buttons
1517 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1518 for ( int n
= 0; n
< count
; n
++ )
1520 // is it a separator?
1521 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1524 wxLogDebug(_T("TB_GETBUTTON failed?"));
1529 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1532 // get the bounding rect of the separator
1534 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1537 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1542 // does it intersect the control?
1544 wxCopyRECTToRect(r
, rectItem
);
1545 if ( rectCtrl
.Intersects(rectItem
) )
1547 // yes, do erase it!
1548 dc
.DrawRectangle(rectItem
);
1550 // Necessary in case we use a no-paint-on-size
1551 // style in the parent: the controls can disappear
1552 control
->Refresh(false);
1561 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1563 wxCoord x
= GET_X_LPARAM(lParam
),
1564 y
= GET_Y_LPARAM(lParam
);
1565 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1567 // cursor left current tool
1568 if ( tool
!= m_pInTool
&& !tool
)
1574 // cursor entered a tool
1575 if ( tool
!= m_pInTool
&& tool
)
1578 OnMouseEnter( tool
->GetId() );
1582 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1587 // we don't handle mouse moves, so always pass the message to
1588 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1589 HandleMouseMove(wParam
, lParam
);
1593 if ( HandleSize(wParam
, lParam
) )
1599 if ( HandlePaint(wParam
, lParam
) )
1607 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1610 // ----------------------------------------------------------------------------
1611 // private functions
1612 // ----------------------------------------------------------------------------
1614 #ifdef wxREMAP_BUTTON_COLOURS
1616 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1622 wxLogLastError(_T("CreateCompatibleDC"));
1627 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1631 wxLogLastError(_T("SelectObject"));
1636 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1638 for ( int i
= 0; i
< width
; i
++ )
1640 for ( int j
= 0; j
< height
; j
++ )
1642 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1644 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1646 COLORREF col
= cmap
[k
].from
;
1647 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1648 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1649 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1651 if ( cmap
[k
].to
!= pixel
)
1652 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1662 #endif // wxREMAP_BUTTON_COLOURS
1664 #endif // wxUSE_TOOLBAR