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
) )
239 // MSW-specific initialisation
240 if ( !MSWCreateToolbar(pos
, size
) )
243 wxSetCCUnicodeFormat(GetHwnd());
245 // workaround for flat toolbar on Windows XP classic style: we have to set
246 // the style after creating the control; doing it at creation time doesn't work
248 if ( style
& wxTB_FLAT
)
250 LRESULT style
= GetMSWToolbarStyle();
252 if ( !(style
& TBSTYLE_FLAT
) )
253 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
255 #endif // wxUSE_UXTHEME
260 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
262 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
265 // toolbar-specific post initialisation
266 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
271 void wxToolBar::Recreate()
273 const HWND hwndOld
= GetHwnd();
276 // we haven't been created yet, no need to recreate
280 // get the position and size before unsubclassing the old toolbar
281 const wxPoint pos
= GetPosition();
282 const wxSize size
= GetSize();
286 if ( !MSWCreateToolbar(pos
, size
) )
289 wxFAIL_MSG( _T("recreating the toolbar failed") );
294 // reparent all our children under the new toolbar
295 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
297 node
= node
->GetNext() )
299 wxWindow
*win
= node
->GetData();
300 if ( !win
->IsTopLevel() )
301 ::SetParent(GetHwndOf(win
), GetHwnd());
304 // only destroy the old toolbar now --
305 // after all the children had been reparented
306 ::DestroyWindow(hwndOld
);
308 // it is for the old bitmap control and can't be used with the new one
311 ::DeleteObject((HBITMAP
) m_hBitmap
);
315 if ( m_disabledImgList
)
317 delete m_disabledImgList
;
318 m_disabledImgList
= NULL
;
324 wxToolBar::~wxToolBar()
326 // we must refresh the frame size when the toolbar is deleted but the frame
327 // is not - otherwise toolbar leaves a hole in the place it used to occupy
328 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
329 if ( frame
&& !frame
->IsBeingDeleted() )
330 frame
->SendSizeEvent();
333 ::DeleteObject((HBITMAP
) m_hBitmap
);
335 delete m_disabledImgList
;
338 wxSize
wxToolBar::DoGetBestSize() const
343 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
345 // maybe an old (< 0x400) Windows version? try to approximate the
346 // toolbar size ourselves
347 sizeBest
= GetToolSize();
348 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
349 sizeBest
.x
*= GetToolsCount();
351 // reverse horz and vertical components if necessary
355 sizeBest
.x
= sizeBest
.y
;
361 sizeBest
.x
= size
.cx
;
362 sizeBest
.y
= size
.cy
;
365 CacheBestSize(sizeBest
);
370 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
372 // toolbars never have border, giving one to them results in broken
374 WXDWORD msStyle
= wxControl::MSWGetStyle
376 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
379 if ( !(style
& wxTB_NO_TOOLTIPS
) )
380 msStyle
|= TBSTYLE_TOOLTIPS
;
382 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
384 // static as it doesn't change during the program lifetime
385 static const int s_verComCtl
= wxApp::GetComCtl32Version();
387 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
388 // style with 6.00 (part of Windows XP) leads to the toolbar with
389 // incorrect background colour - and not using it still results in the
390 // correct (flat) toolbar, so don't use it there
391 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
392 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
394 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
395 msStyle
|= TBSTYLE_LIST
;
398 if ( style
& wxTB_NODIVIDER
)
399 msStyle
|= CCS_NODIVIDER
;
401 if ( style
& wxTB_NOALIGN
)
402 msStyle
|= CCS_NOPARENTALIGN
;
404 if ( style
& wxTB_VERTICAL
)
407 if( style
& wxTB_BOTTOM
)
408 msStyle
|= CCS_BOTTOM
;
410 if ( style
& wxTB_RIGHT
)
411 msStyle
|= CCS_RIGHT
;
416 // ----------------------------------------------------------------------------
417 // adding/removing tools
418 // ----------------------------------------------------------------------------
420 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
422 // nothing special to do here - we really create the toolbar buttons in
426 InvalidateBestSize();
430 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
432 // the main difficulty we have here is with the controls in the toolbars:
433 // as we (sometimes) use several separators to cover up the space used by
434 // them, the indices are not the same for us and the toolbar
436 // first determine the position of the first button to delete: it may be
437 // different from pos if we use several separators to cover the space used
439 wxToolBarToolsList::compatibility_iterator node
;
440 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
442 wxToolBarToolBase
*tool2
= node
->GetData();
445 // let node point to the next node in the list
446 node
= node
->GetNext();
451 if ( tool2
->IsControl() )
452 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
455 // now determine the number of buttons to delete and the area taken by them
456 size_t nButtonsToDelete
= 1;
458 // get the size of the button we're going to delete
460 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
462 wxLogLastError(_T("TB_GETITEMRECT"));
465 int width
= r
.right
- r
.left
;
467 if ( tool
->IsControl() )
469 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
470 width
*= nButtonsToDelete
;
471 tool
->GetControl()->Destroy();
474 // do delete all buttons
475 m_nButtons
-= nButtonsToDelete
;
476 while ( nButtonsToDelete
-- > 0 )
478 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
480 wxLogLastError(wxT("TB_DELETEBUTTON"));
488 // and finally reposition all the controls after this button (the toolbar
489 // takes care of all normal items)
490 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
492 wxToolBarToolBase
*tool2
= node
->GetData();
493 if ( tool2
->IsControl() )
496 wxControl
*control
= tool2
->GetControl();
497 control
->GetPosition(&x
, NULL
);
498 control
->Move(x
- width
, wxDefaultCoord
);
502 InvalidateBestSize();
507 void wxToolBar::CreateDisabledImageList()
509 if (m_disabledImgList
!= NULL
)
511 delete m_disabledImgList
;
512 m_disabledImgList
= NULL
;
515 // as we can't use disabled image list with older versions of comctl32.dll,
516 // don't even bother creating it
517 if ( wxApp::GetComCtl32Version() >= 470 )
519 // search for the first disabled button img in the toolbar, if any
520 for ( wxToolBarToolsList::compatibility_iterator
521 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
523 wxToolBarToolBase
*tool
= node
->GetData();
524 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
525 if ( bmpDisabled
.Ok() )
527 m_disabledImgList
= new wxImageList
531 bmpDisabled
.GetMask() != NULL
,
538 // we don't have any disabled bitmaps
542 bool wxToolBar::Realize()
544 const size_t nTools
= GetToolsCount();
549 #ifdef wxREMAP_BUTTON_COLOURS
550 // don't change the values of these constants, they can be set from the
551 // user code via wxSystemOptions
560 // the user-specified option overrides anything, but if it wasn't set, only
561 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
562 // much worse after remapping
563 static const wxChar
*remapOption
= wxT("msw.remap");
564 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
565 ? wxSystemOptions::GetOptionInt(remapOption
)
566 : wxDisplayDepth() <= 8 ? Remap_Buttons
569 #endif // wxREMAP_BUTTON_COLOURS
571 // delete all old buttons, if any
572 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
574 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
576 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
580 // First, add the bitmap: we use one bitmap for all toolbar buttons
581 // ----------------------------------------------------------------
583 wxToolBarToolsList::compatibility_iterator node
;
587 if ( HasFlag(wxTB_NOICONS
) )
589 // no icons, don't leave space for them
593 else // do show icons
595 // if we already have a bitmap, we'll replace the existing one --
596 // otherwise we'll install a new one
597 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
599 sizeBmp
.x
= m_defaultWidth
;
600 sizeBmp
.y
= m_defaultHeight
;
602 const wxCoord totalBitmapWidth
= m_defaultWidth
*
603 wx_truncate_cast(wxCoord
, nTools
),
604 totalBitmapHeight
= m_defaultHeight
;
606 // Create a bitmap and copy all the tool bitmaps into it
607 wxMemoryDC dcAllButtons
;
608 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
609 dcAllButtons
.SelectObject(bitmap
);
611 #ifdef wxREMAP_BUTTON_COLOURS
612 if ( remapValue
!= Remap_TransparentBg
)
613 #endif // wxREMAP_BUTTON_COLOURS
615 // VZ: why do we hardcode grey colour for CE?
616 dcAllButtons
.SetBackground(wxBrush(
618 wxColour(0xc0, 0xc0, 0xc0)
619 #else // !__WXWINCE__
620 GetBackgroundColour()
621 #endif // __WXWINCE__/!__WXWINCE__
623 dcAllButtons
.Clear();
626 m_hBitmap
= bitmap
.GetHBITMAP();
627 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
629 #ifdef wxREMAP_BUTTON_COLOURS
630 if ( remapValue
== Remap_Bg
)
632 dcAllButtons
.SelectObject(wxNullBitmap
);
634 // Even if we're not remapping the bitmap
635 // content, we still have to remap the background.
636 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
637 totalBitmapWidth
, totalBitmapHeight
);
639 dcAllButtons
.SelectObject(bitmap
);
641 #endif // wxREMAP_BUTTON_COLOURS
643 // the button position
646 // the number of buttons (not separators)
649 CreateDisabledImageList();
650 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
652 wxToolBarToolBase
*tool
= node
->GetData();
653 if ( tool
->IsButton() )
655 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
657 const int w
= bmp
.GetWidth();
658 const int h
= bmp
.GetHeight();
662 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
663 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
665 // notice the last parameter: do use mask
666 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
670 wxFAIL_MSG( _T("invalid tool button bitmap") );
673 // also deal with disabled bitmap if we want to use them
674 if ( m_disabledImgList
)
676 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
677 #if wxUSE_IMAGE && wxUSE_WXDIB
678 if ( !bmpDisabled
.Ok() )
680 // no disabled bitmap specified but we still need to
681 // fill the space in the image list with something, so
682 // we grey out the normal bitmap
684 wxCreateGreyedImage(bmp
.ConvertToImage(), imgGreyed
);
686 #ifdef wxREMAP_BUTTON_COLOURS
687 if ( remapValue
== Remap_Buttons
)
689 // we need to have light grey background colour for
690 // MapBitmap() to work correctly
691 for ( int y
= 0; y
< h
; y
++ )
693 for ( int x
= 0; x
< w
; x
++ )
695 if ( imgGreyed
.IsTransparent(x
, y
) )
696 imgGreyed
.SetRGB(x
, y
,
698 wxLIGHT_GREY
->Green(),
699 wxLIGHT_GREY
->Blue());
703 #endif // wxREMAP_BUTTON_COLOURS
705 bmpDisabled
= wxBitmap(imgGreyed
);
707 #endif // wxUSE_IMAGE
709 #ifdef wxREMAP_BUTTON_COLOURS
710 if ( remapValue
== Remap_Buttons
)
711 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
712 #endif // wxREMAP_BUTTON_COLOURS
714 m_disabledImgList
->Add(bmpDisabled
);
717 // still inc width and number of buttons because otherwise the
718 // subsequent buttons will all be shifted which is rather confusing
719 // (and like this you'd see immediately which bitmap was bad)
725 dcAllButtons
.SelectObject(wxNullBitmap
);
727 // don't delete this HBITMAP!
728 bitmap
.SetHBITMAP(0);
730 #ifdef wxREMAP_BUTTON_COLOURS
731 if ( remapValue
== Remap_Buttons
)
733 // Map to system colours
734 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
735 totalBitmapWidth
, totalBitmapHeight
);
737 #endif // wxREMAP_BUTTON_COLOURS
739 bool addBitmap
= true;
741 if ( oldToolBarBitmap
)
743 #ifdef TB_REPLACEBITMAP
744 if ( wxApp::GetComCtl32Version() >= 400 )
746 TBREPLACEBITMAP replaceBitmap
;
747 replaceBitmap
.hInstOld
= NULL
;
748 replaceBitmap
.hInstNew
= NULL
;
749 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
750 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
751 replaceBitmap
.nButtons
= nButtons
;
752 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
753 0, (LPARAM
) &replaceBitmap
) )
755 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
758 ::DeleteObject(oldToolBarBitmap
);
764 #endif // TB_REPLACEBITMAP
766 // we can't replace the old bitmap, so we will add another one
767 // (awfully inefficient, but what else to do?) and shift the bitmap
768 // indices accordingly
771 bitmapId
= m_nButtons
;
775 if ( addBitmap
) // no old bitmap or we can't replace it
777 TBADDBITMAP addBitmap
;
779 addBitmap
.nID
= (UINT
) hBitmap
;
780 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
781 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
783 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
787 // disable image lists are only supported in comctl32.dll 4.70+
788 if ( wxApp::GetComCtl32Version() >= 470 )
790 HIMAGELIST hil
= m_disabledImgList
791 ? GetHimagelistOf(m_disabledImgList
)
794 // notice that we set the image list even if don't have one right
795 // now as we could have it before and need to reset it in this case
796 HIMAGELIST oldImageList
= (HIMAGELIST
)
797 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
799 // delete previous image list if any
801 ::DeleteObject(oldImageList
);
805 // don't call SetToolBitmapSize() as we don't want to change the values of
806 // m_defaultWidth/Height
807 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
808 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
810 wxLogLastError(_T("TB_SETBITMAPSIZE"));
813 // Next add the buttons and separators
814 // -----------------------------------
816 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
818 // this array will hold the indices of all controls in the toolbar
819 wxArrayInt controlIds
;
821 bool lastWasRadio
= false;
823 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
825 wxToolBarToolBase
*tool
= node
->GetData();
827 // don't add separators to the vertical toolbar with old comctl32.dll
828 // versions as they didn't handle this properly
829 if ( IsVertical() && tool
->IsSeparator() &&
830 wxApp::GetComCtl32Version() <= 472 )
835 TBBUTTON
& button
= buttons
[i
];
837 wxZeroMemory(button
);
839 bool isRadio
= false;
840 switch ( tool
->GetStyle() )
842 case wxTOOL_STYLE_CONTROL
:
843 button
.idCommand
= tool
->GetId();
844 // fall through: create just a separator too
846 case wxTOOL_STYLE_SEPARATOR
:
847 button
.fsState
= TBSTATE_ENABLED
;
848 button
.fsStyle
= TBSTYLE_SEP
;
851 case wxTOOL_STYLE_BUTTON
:
852 if ( !HasFlag(wxTB_NOICONS
) )
853 button
.iBitmap
= bitmapId
;
855 if ( HasFlag(wxTB_TEXT
) )
857 const wxString
& label
= tool
->GetLabel();
858 if ( !label
.empty() )
859 button
.iString
= (int)label
.c_str();
862 button
.idCommand
= tool
->GetId();
864 if ( tool
->IsEnabled() )
865 button
.fsState
|= TBSTATE_ENABLED
;
866 if ( tool
->IsToggled() )
867 button
.fsState
|= TBSTATE_CHECKED
;
869 switch ( tool
->GetKind() )
872 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
876 // the first item in the radio group is checked by
877 // default to be consistent with wxGTK and the menu
879 button
.fsState
|= TBSTATE_CHECKED
;
881 if (tool
->Toggle(true))
883 DoToggleTool(tool
, true);
886 else if (tool
->IsToggled())
888 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
889 int prevIndex
= i
- 1;
892 TBBUTTON
& prevButton
= buttons
[prevIndex
];
893 wxToolBarToolBase
*tool
= nodePrev
->GetData();
894 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
897 if ( tool
->Toggle(false) )
898 DoToggleTool(tool
, false);
900 prevButton
.fsState
= TBSTATE_ENABLED
;
901 nodePrev
= nodePrev
->GetPrevious();
910 button
.fsStyle
= TBSTYLE_CHECK
;
914 button
.fsStyle
= TBSTYLE_BUTTON
;
918 wxFAIL_MSG( _T("unexpected toolbar button kind") );
919 button
.fsStyle
= TBSTYLE_BUTTON
;
927 lastWasRadio
= isRadio
;
932 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
934 wxLogLastError(wxT("TB_ADDBUTTONS"));
939 // Deal with the controls finally
940 // ------------------------------
942 // adjust the controls size to fit nicely in the toolbar
945 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
947 wxToolBarToolBase
*tool
= node
->GetData();
949 // we calculate the running y coord for vertical toolbars so we need to
950 // get the items size for all items but for the horizontal ones we
951 // don't need to deal with the non controls
952 bool isControl
= tool
->IsControl();
953 if ( !isControl
&& !IsVertical() )
956 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
957 // latter only appeared in v4.70 of comctl32.dll
959 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
960 index
, (LPARAM
)(LPRECT
)&r
) )
962 wxLogLastError(wxT("TB_GETITEMRECT"));
967 // can only be control if isVertical
968 y
+= r
.bottom
- r
.top
;
973 wxControl
*control
= tool
->GetControl();
974 wxSize size
= control
->GetSize();
976 // the position of the leftmost controls corner
977 int left
= wxDefaultCoord
;
979 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
980 #ifdef TB_SETBUTTONINFO
981 // available in headers, now check whether it is available now
983 if ( wxApp::GetComCtl32Version() >= 471 )
985 // set the (underlying) separators width to be that of the
988 tbbi
.cbSize
= sizeof(tbbi
);
989 tbbi
.dwMask
= TBIF_SIZE
;
990 tbbi
.cx
= (WORD
)size
.x
;
991 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
992 tool
->GetId(), (LPARAM
)&tbbi
) )
994 // the id is probably invalid?
995 wxLogLastError(wxT("TB_SETBUTTONINFO"));
999 #endif // comctl32.dll 4.71
1000 // TB_SETBUTTONINFO unavailable
1002 // try adding several separators to fit the controls width
1003 int widthSep
= r
.right
- r
.left
;
1009 tbb
.fsState
= TBSTATE_ENABLED
;
1010 tbb
.fsStyle
= TBSTYLE_SEP
;
1012 size_t nSeparators
= size
.x
/ widthSep
;
1013 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1015 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1016 index
, (LPARAM
)&tbb
) )
1018 wxLogLastError(wxT("TB_INSERTBUTTON"));
1024 // remember the number of separators we used - we'd have to
1025 // delete all of them later
1026 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1028 // adjust the controls width to exactly cover the separators
1029 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
1032 // position the control itself correctly vertically
1033 int height
= r
.bottom
- r
.top
;
1034 int diff
= height
- size
.y
;
1037 // the control is too high, resize to fit
1038 control
->SetSize(wxDefaultCoord
, height
- 2);
1049 y
+= height
+ 2 * GetMargins().y
;
1051 else // horizontal toolbar
1053 if ( left
== wxDefaultCoord
)
1059 control
->Move(left
, top
+ (diff
+ 1) / 2);
1062 // the max index is the "real" number of buttons - i.e. counting even the
1063 // separators which we added just for aligning the controls
1066 if ( !IsVertical() )
1068 if ( m_maxRows
== 0 )
1069 // if not set yet, only one row
1072 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1074 // if not set yet, have one column
1076 SetRows(m_nButtons
);
1079 InvalidateBestSize();
1085 // ----------------------------------------------------------------------------
1087 // ----------------------------------------------------------------------------
1089 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
1091 wxToolBarToolBase
*tool
= FindById((int)id
);
1095 bool toggled
= false; // just to suppress warnings
1097 if ( tool
->CanBeToggled() )
1099 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1100 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1102 // ignore the event when a radio button is released, as this doesn't
1103 // seem to happen at all, and is handled otherwise
1104 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1107 tool
->Toggle(toggled
);
1108 UnToggleRadioGroup(tool
);
1111 // OnLeftClick() can veto the button state change - for buttons which
1112 // may be toggled only, of couse
1113 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1116 tool
->Toggle(!toggled
);
1118 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1124 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1126 WXLPARAM
*WXUNUSED(result
))
1128 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1131 // First check if this applies to us
1132 NMHDR
*hdr
= (NMHDR
*)lParam
;
1134 // the tooltips control created by the toolbar is sometimes Unicode, even
1135 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1136 UINT code
= hdr
->code
;
1137 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1140 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1141 if ( toolTipWnd
!= hdr
->hwndFrom
)
1144 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1145 int id
= (int)ttText
->hdr
.idFrom
;
1147 wxToolBarToolBase
*tool
= FindById(id
);
1149 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1151 wxUnusedVar(lParam
);
1158 // ----------------------------------------------------------------------------
1160 // ----------------------------------------------------------------------------
1162 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1164 wxToolBarBase::SetToolBitmapSize(size
);
1166 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1169 void wxToolBar::SetRows(int nRows
)
1171 if ( nRows
== m_maxRows
)
1173 // avoid resizing the frame uselessly
1177 // TRUE in wParam means to create at least as many rows, FALSE -
1180 ::SendMessage(GetHwnd(), TB_SETROWS
,
1181 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1189 // The button size is bigger than the bitmap size
1190 wxSize
wxToolBar::GetToolSize() const
1192 // TB_GETBUTTONSIZE is supported from version 4.70
1193 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1194 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1195 && !defined (__DIGITALMARS__)
1196 if ( wxApp::GetComCtl32Version() >= 470 )
1198 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1200 return wxSize(LOWORD(dw
), HIWORD(dw
));
1203 #endif // comctl32.dll 4.70+
1206 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1211 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1214 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1216 for ( ; current
; current
= current
->GetNext() )
1219 return current
->GetData();
1221 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1222 size_t separators
= tool
->GetSeparatorsCount();
1224 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1225 // otherwise, skip as many items as the separator count, plus the
1227 index
-= separators
? separators
+ 1 : 1;
1233 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1238 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1240 // MBN: when the point ( x, y ) is close to the toolbar border
1241 // TB_HITTEST returns m_nButtons ( not -1 )
1242 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1243 // it's a separator or there is no tool at all there
1244 return (wxToolBarToolBase
*)NULL
;
1246 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1247 // we don't use the dummy separators hack
1248 #ifdef TB_SETBUTTONINFO
1249 if ( wxApp::GetComCtl32Version() >= 471 )
1251 return m_tools
.Item((size_t)index
)->GetData();
1254 #endif // TB_SETBUTTONINFO
1256 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1260 void wxToolBar::UpdateSize()
1262 wxPoint pos
= GetPosition();
1263 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1264 if (pos
!= GetPosition())
1267 // In case Realize is called after the initial display (IOW the programmer
1268 // may have rebuilt the toolbar) give the frame the option of resizing the
1269 // toolbar to full width again, but only if the parent is a frame and the
1270 // toolbar is managed by the frame. Otherwise assume that some other
1271 // layout mechanism is controlling the toolbar size and leave it alone.
1272 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1273 if ( frame
&& frame
->GetToolBar() == this )
1275 frame
->SendSizeEvent();
1279 // ----------------------------------------------------------------------------
1281 // ---------------------------------------------------------------------------
1283 // get the TBSTYLE of the given toolbar window
1284 long wxToolBar::GetMSWToolbarStyle() const
1286 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1289 void wxToolBar::SetWindowStyleFlag(long style
)
1291 // the style bits whose changes force us to recreate the toolbar
1292 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1294 const long styleOld
= GetWindowStyle();
1296 wxToolBarBase::SetWindowStyleFlag(style
);
1298 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1299 // also fatal as we'd then try to recreate the toolbar when it's just being
1301 if ( GetToolsCount() &&
1302 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1304 // to remove the text labels, simply re-realizing the toolbar is enough
1305 // but I don't know of any way to add the text to an existing toolbar
1306 // other than by recreating it entirely
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1317 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1318 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1321 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1323 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1324 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1327 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1329 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1330 // without, so we really need to delete the button and recreate it here
1331 wxFAIL_MSG( _T("not implemented") );
1334 // ----------------------------------------------------------------------------
1336 // ----------------------------------------------------------------------------
1338 // Responds to colour changes, and passes event on to children.
1339 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1341 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1343 // Remap the buttons
1346 // Relayout the toolbar
1347 int nrows
= m_maxRows
;
1348 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1353 // let the event propagate further
1357 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1359 if (event
.Leaving() && m_pInTool
)
1366 if ( event
.RightDown() )
1368 // find the tool under the mouse
1369 wxCoord x
= 0, y
= 0;
1370 event
.GetPosition(&x
, &y
);
1372 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1373 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1381 // This handler is required to allow the toolbar to be set to a non-default
1382 // colour: for example, when it must blend in with a notebook page.
1383 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1385 RECT rect
= wxGetClientRect(GetHwnd());
1386 HDC hdc
= GetHdcOf((*event
.GetDC()));
1389 // we may need to draw themed colour so that we appear correctly on
1390 // e.g. notebook page under XP with themes but only do it if the parent
1391 // draws themed background itself
1392 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1394 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1398 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1402 // it can also return S_FALSE which seems to simply say that it
1403 // didn't draw anything but no error really occurred
1405 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1408 #endif // wxUSE_UXTHEME
1410 if ( UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1412 // do draw our background
1414 // notice that this 'dumb' implementation may cause flicker for some of
1415 // the controls in which case they should intercept wxEraseEvent and
1416 // process it themselves somehow
1417 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1419 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1420 ::FillRect(hdc
, &rect
, hBrush
);
1422 else // we have no non default background colour
1424 // let the system do it for us
1429 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1431 // calculate our minor dimension ourselves - we're confusing the standard
1432 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1434 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1440 w
= r
.right
- r
.left
;
1443 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1450 if (HasFlag( wxTB_FLAT
))
1451 h
= r
.bottom
- r
.top
- 3;
1453 h
= r
.bottom
- r
.top
;
1456 // FIXME: hardcoded separator line height...
1457 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1462 if ( MAKELPARAM(w
, h
) != lParam
)
1464 // size really changed
1468 // message processed
1475 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1477 // erase any dummy separators which were used
1478 // for aligning the controls if any here
1480 // first of all, are there any controls at all?
1481 wxToolBarToolsList::compatibility_iterator node
;
1482 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1484 if ( node
->GetData()->IsControl() )
1489 // no controls, nothing to erase
1492 // prepare the DC on which we'll be drawing
1493 wxClientDC
dc(this);
1494 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1495 dc
.SetPen(*wxTRANSPARENT_PEN
);
1498 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1499 // nothing to redraw anyhow
1503 wxCopyRECTToRect(r
, rectUpdate
);
1505 dc
.SetClippingRegion(rectUpdate
);
1507 // draw the toolbar tools, separators &c normally
1508 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1510 // for each control in the toolbar find all the separators intersecting it
1513 // NB: this is really the only way to do it as we don't know if a separator
1514 // corresponds to a control (i.e. is a dummy one) or a real one
1516 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1518 wxToolBarToolBase
*tool
= node
->GetData();
1519 if ( tool
->IsControl() )
1521 // get the control rect in our client coords
1522 wxControl
*control
= tool
->GetControl();
1523 wxRect rectCtrl
= control
->GetRect();
1525 // iterate over all buttons
1527 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1528 for ( int n
= 0; n
< count
; n
++ )
1530 // is it a separator?
1531 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1534 wxLogDebug(_T("TB_GETBUTTON failed?"));
1539 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1542 // get the bounding rect of the separator
1544 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1547 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1552 // does it intersect the control?
1554 wxCopyRECTToRect(r
, rectItem
);
1555 if ( rectCtrl
.Intersects(rectItem
) )
1557 // yes, do erase it!
1558 dc
.DrawRectangle(rectItem
);
1560 // Necessary in case we use a no-paint-on-size
1561 // style in the parent: the controls can disappear
1562 control
->Refresh(false);
1571 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1573 wxCoord x
= GET_X_LPARAM(lParam
),
1574 y
= GET_Y_LPARAM(lParam
);
1575 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1577 // cursor left current tool
1578 if ( tool
!= m_pInTool
&& !tool
)
1584 // cursor entered a tool
1585 if ( tool
!= m_pInTool
&& tool
)
1588 OnMouseEnter( tool
->GetId() );
1592 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1597 // we don't handle mouse moves, so always pass the message to
1598 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1599 HandleMouseMove(wParam
, lParam
);
1603 if ( HandleSize(wParam
, lParam
) )
1609 if ( HandlePaint(wParam
, lParam
) )
1617 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1620 // ----------------------------------------------------------------------------
1621 // private functions
1622 // ----------------------------------------------------------------------------
1624 #ifdef wxREMAP_BUTTON_COLOURS
1626 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1632 wxLogLastError(_T("CreateCompatibleDC"));
1637 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1641 wxLogLastError(_T("SelectObject"));
1646 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1648 for ( int i
= 0; i
< width
; i
++ )
1650 for ( int j
= 0; j
< height
; j
++ )
1652 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1654 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1656 COLORREF col
= cmap
[k
].from
;
1657 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1658 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1659 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1661 if ( cmap
[k
].to
!= pixel
)
1662 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1672 #endif // wxREMAP_BUTTON_COLOURS
1674 #endif // wxUSE_TOOLBAR