1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "tbar95.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dynarray.h"
36 #include "wx/settings.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
39 #include "wx/control.h"
42 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
44 #include "wx/toolbar.h"
45 #include "wx/sysopt.h"
47 #include "wx/msw/private.h"
50 #include "wx/msw/uxtheme.h"
53 // include <commctrl.h> "properly"
54 #include "wx/msw/wrapcctl.h"
56 #include "wx/app.h" // for GetComCtl32Version
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // these standard constants are not always defined in compilers headers
66 #define TBSTYLE_LIST 0x1000
67 #define TBSTYLE_FLAT 0x0800
70 #ifndef TBSTYLE_TRANSPARENT
71 #define TBSTYLE_TRANSPARENT 0x8000
74 #ifndef TBSTYLE_TOOLTIPS
75 #define TBSTYLE_TOOLTIPS 0x0100
80 #define TB_SETSTYLE (WM_USER + 56)
81 #define TB_GETSTYLE (WM_USER + 57)
85 #define TB_HITTEST (WM_USER + 69)
89 #define TB_GETMAXSIZE (WM_USER + 83)
92 // these values correspond to those used by comctl32.dll
93 #define DEFAULTBITMAPX 16
94 #define DEFAULTBITMAPY 15
95 #define DEFAULTBUTTONX 24
96 #define DEFAULTBUTTONY 24
97 #define DEFAULTBARHEIGHT 27
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
115 style ( wxNO_BORDER | wxTB_HORIZONTAL)
124 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
125 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
126 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
127 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 class wxToolBarTool
: public wxToolBarToolBase
137 wxToolBarTool(wxToolBar
*tbar
,
139 const wxString
& label
,
140 const wxBitmap
& bmpNormal
,
141 const wxBitmap
& bmpDisabled
,
143 wxObject
*clientData
,
144 const wxString
& shortHelp
,
145 const wxString
& longHelp
)
146 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
147 clientData
, shortHelp
, longHelp
)
152 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
153 : wxToolBarToolBase(tbar
, control
)
158 virtual void SetLabel(const wxString
& label
)
160 if ( label
== m_label
)
163 wxToolBarToolBase::SetLabel(label
);
165 // we need to update the label shown in the toolbar because it has a
166 // pointer to the internal buffer of the old label
168 // TODO: use TB_SETBUTTONINFO
171 // set/get the number of separators which we use to cover the space used by
172 // a control in the toolbar
173 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
174 size_t GetSeparatorsCount() const { return m_nSepCount
; }
179 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()
219 m_defaultWidth
= DEFAULTBITMAPX
;
220 m_defaultHeight
= DEFAULTBITMAPY
;
225 bool wxToolBar::Create(wxWindow
*parent
,
230 const wxString
& name
)
232 // common initialisation
233 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
236 // MSW-specific initialisation
237 if ( !MSWCreateToolbar(pos
, size
) )
240 wxSetCCUnicodeFormat(GetHwnd());
242 // set up the colors and fonts
243 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
244 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
246 // workaround for flat toolbar on Windows XP classic style
248 if ( style
& wxTB_FLAT
)
250 wxUxThemeEngine
*p
= wxUxThemeEngine::Get();
251 if ( !p
|| !p
->IsThemeActive() )
253 DWORD dwToolbarStyle
;
255 dwToolbarStyle
= (DWORD
)::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L );
257 if ((dwToolbarStyle
& TBSTYLE_FLAT
) == 0)
259 dwToolbarStyle
|= TBSTYLE_FLAT
;
260 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, (LPARAM
)dwToolbarStyle
);
269 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
271 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
274 // toolbar-specific post initialisation
275 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
280 void wxToolBar::Recreate()
282 const HWND hwndOld
= GetHwnd();
285 // we haven't been created yet, no need to recreate
289 // get the position and size before unsubclassing the old toolbar
290 const wxPoint pos
= GetPosition();
291 const wxSize size
= GetSize();
295 if ( !MSWCreateToolbar(pos
, size
) )
298 wxFAIL_MSG( _T("recreating the toolbar failed") );
303 // reparent all our children under the new toolbar
304 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
306 node
= node
->GetNext() )
308 wxWindow
*win
= node
->GetData();
309 if ( !win
->IsTopLevel() )
310 ::SetParent(GetHwndOf(win
), GetHwnd());
313 // only destroy the old toolbar now -- after all the children had been
315 ::DestroyWindow(hwndOld
);
317 // it is for the old bitmap control and can't be used with the new one
320 ::DeleteObject((HBITMAP
) m_hBitmap
);
328 wxToolBar::~wxToolBar()
330 // we must refresh the frame size when the toolbar is deleted but the frame
331 // is not - otherwise toolbar leaves a hole in the place it used to occupy
332 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
333 if ( frame
&& !frame
->IsBeingDeleted() )
335 frame
->SendSizeEvent();
340 ::DeleteObject((HBITMAP
) m_hBitmap
);
344 wxSize
wxToolBar::DoGetBestSize() const
349 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
351 // maybe an old (< 0x400) Windows version? try to approximate the
352 // toolbar size ourselves
353 sizeBest
= GetToolSize();
354 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
355 sizeBest
.x
*= GetToolsCount();
357 // reverse horz and vertical components if necessary
358 if ( HasFlag(wxTB_VERTICAL
) )
361 sizeBest
.x
= sizeBest
.y
;
367 sizeBest
.x
= size
.cx
;
368 sizeBest
.y
= size
.cy
;
374 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
376 // toolbars never have border, giving one to them results in broken
378 WXDWORD msStyle
= wxControl::MSWGetStyle
380 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
383 // always include this one, it never hurts and setting it later only if we
384 // do have tooltips wouldn't work
385 msStyle
|= TBSTYLE_TOOLTIPS
;
387 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
389 // static as it doesn't change during the program lifetime
390 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
392 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
393 // style with 6.00 (part of Windows XP) leads to the toolbar with
394 // incorrect background colour - and not using it still results in the
395 // correct (flat) toolbar, so don't use it there
396 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
398 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
401 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
403 msStyle
|= TBSTYLE_LIST
;
407 if ( style
& wxTB_NODIVIDER
)
408 msStyle
|= CCS_NODIVIDER
;
410 if ( style
& wxTB_NOALIGN
)
411 msStyle
|= CCS_NOPARENTALIGN
;
413 if ( style
& wxTB_VERTICAL
)
419 // ----------------------------------------------------------------------------
420 // adding/removing tools
421 // ----------------------------------------------------------------------------
423 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
425 // nothing special to do here - we really create the toolbar buttons in
429 InvalidateBestSize();
433 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
435 // the main difficulty we have here is with the controls in the toolbars:
436 // as we (sometimes) use several separators to cover up the space used by
437 // them, the indices are not the same for us and the toolbar
439 // first determine the position of the first button to delete: it may be
440 // different from pos if we use several separators to cover the space used
442 wxToolBarToolsList::compatibility_iterator node
;
443 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
445 wxToolBarToolBase
*tool2
= node
->GetData();
448 // let node point to the next node in the list
449 node
= node
->GetNext();
454 if ( tool2
->IsControl() )
456 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
460 // now determine the number of buttons to delete and the area taken by them
461 size_t nButtonsToDelete
= 1;
463 // get the size of the button we're going to delete
465 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
467 wxLogLastError(_T("TB_GETITEMRECT"));
470 int width
= r
.right
- r
.left
;
472 if ( tool
->IsControl() )
474 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
476 width
*= nButtonsToDelete
;
479 // do delete all buttons
480 m_nButtons
-= nButtonsToDelete
;
481 while ( nButtonsToDelete
-- > 0 )
483 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
485 wxLogLastError(wxT("TB_DELETEBUTTON"));
493 // and finally reposition all the controls after this button (the toolbar
494 // takes care of all normal items)
495 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
497 wxToolBarToolBase
*tool2
= node
->GetData();
498 if ( tool2
->IsControl() )
501 wxControl
*control
= tool2
->GetControl();
502 control
->GetPosition(&x
, NULL
);
503 control
->Move(x
- width
, wxDefaultCoord
);
507 InvalidateBestSize();
511 bool wxToolBar::Realize()
513 const size_t nTools
= GetToolsCount();
520 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
522 bool doRemap
, doRemapBg
, doTransparent
;
526 doTransparent
= false;
528 if (wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 2)
530 doRemapBg
= doRemap
= false;
531 doTransparent
= true;
534 { doRemap
= !wxSystemOptions::HasOption(wxT("msw.remap"))
535 || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1;
536 doRemapBg
= !doRemap
;
537 doTransparent
= false;
541 // delete all old buttons, if any
542 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
544 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
546 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
550 // First, add the bitmap: we use one bitmap for all toolbar buttons
551 // ----------------------------------------------------------------
553 wxToolBarToolsList::compatibility_iterator node
;
557 if ( HasFlag(wxTB_NOICONS
) )
559 // no icons, don't leave space for them
563 else // do show icons
565 // if we already have a bitmap, we'll replace the existing one --
566 // otherwise we'll install a new one
567 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
569 sizeBmp
.x
= m_defaultWidth
;
570 sizeBmp
.y
= m_defaultHeight
;
572 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
573 totalBitmapHeight
= m_defaultHeight
;
575 // Create a bitmap and copy all the tool bitmaps to it
576 wxMemoryDC dcAllButtons
;
577 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
578 dcAllButtons
.SelectObject(bitmap
);
580 dcAllButtons
.SetBackground(wxBrush(wxColour(192,192,192)));
583 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
585 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
587 dcAllButtons
.Clear();
589 m_hBitmap
= bitmap
.GetHBITMAP();
590 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
595 dcAllButtons
.SelectObject(wxNullBitmap
);
597 // Even if we're not remapping the bitmap
598 // content, we still have to remap the background.
599 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
600 totalBitmapWidth
, totalBitmapHeight
);
602 dcAllButtons
.SelectObject(bitmap
);
606 // the button position
609 // the number of buttons (not separators)
612 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
614 wxToolBarToolBase
*tool
= node
->GetData();
615 if ( tool
->IsButton() )
617 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
620 int xOffset
= wxMax(0, (m_defaultWidth
- bmp
.GetWidth())/2);
621 int yOffset
= wxMax(0, (m_defaultHeight
- bmp
.GetHeight())/2);
622 // notice the last parameter: do use mask
623 dcAllButtons
.DrawBitmap(bmp
, x
+xOffset
, yOffset
, true);
627 wxFAIL_MSG( _T("invalid tool button bitmap") );
630 // still inc width and number of buttons because otherwise the
631 // subsequent buttons will all be shifted which is rather confusing
632 // (and like this you'd see immediately which bitmap was bad)
638 dcAllButtons
.SelectObject(wxNullBitmap
);
640 // don't delete this HBITMAP!
641 bitmap
.SetHBITMAP(0);
645 // Map to system colours
646 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
647 totalBitmapWidth
, totalBitmapHeight
);
650 bool addBitmap
= true;
652 if ( oldToolBarBitmap
)
654 #ifdef TB_REPLACEBITMAP
655 if ( wxTheApp
->GetComCtl32Version() >= 400 )
657 TBREPLACEBITMAP replaceBitmap
;
658 replaceBitmap
.hInstOld
= NULL
;
659 replaceBitmap
.hInstNew
= NULL
;
660 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
661 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
662 replaceBitmap
.nButtons
= nButtons
;
663 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
664 0, (LPARAM
) &replaceBitmap
) )
666 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
669 ::DeleteObject(oldToolBarBitmap
);
675 #endif // TB_REPLACEBITMAP
677 // we can't replace the old bitmap, so we will add another one
678 // (awfully inefficient, but what else to do?) and shift the bitmap
679 // indices accordingly
682 bitmapId
= m_nButtons
;
686 if ( addBitmap
) // no old bitmap or we can't replace it
688 TBADDBITMAP addBitmap
;
690 addBitmap
.nID
= (UINT
) hBitmap
;
691 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
692 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
694 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
699 // don't call SetToolBitmapSize() as we don't want to change the values of
700 // m_defaultWidth/Height
701 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
702 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
704 wxLogLastError(_T("TB_SETBITMAPSIZE"));
707 // Next add the buttons and separators
708 // -----------------------------------
710 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
712 // this array will hold the indices of all controls in the toolbar
713 wxArrayInt controlIds
;
715 bool lastWasRadio
= false;
717 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
719 wxToolBarToolBase
*tool
= node
->GetData();
721 // don't add separators to the vertical toolbar with old comctl32.dll
722 // versions as they didn't handle this properly
723 if ( isVertical
&& tool
->IsSeparator() &&
724 wxTheApp
->GetComCtl32Version() <= 472 )
730 TBBUTTON
& button
= buttons
[i
];
732 wxZeroMemory(button
);
734 bool isRadio
= false;
735 switch ( tool
->GetStyle() )
737 case wxTOOL_STYLE_CONTROL
:
738 button
.idCommand
= tool
->GetId();
739 // fall through: create just a separator too
741 case wxTOOL_STYLE_SEPARATOR
:
742 button
.fsState
= TBSTATE_ENABLED
;
743 button
.fsStyle
= TBSTYLE_SEP
;
746 case wxTOOL_STYLE_BUTTON
:
747 if ( !HasFlag(wxTB_NOICONS
) )
748 button
.iBitmap
= bitmapId
;
750 if ( HasFlag(wxTB_TEXT
) )
752 const wxString
& label
= tool
->GetLabel();
753 if ( !label
.empty() )
755 button
.iString
= (int)label
.c_str();
759 button
.idCommand
= tool
->GetId();
761 if ( tool
->IsEnabled() )
762 button
.fsState
|= TBSTATE_ENABLED
;
763 if ( tool
->IsToggled() )
764 button
.fsState
|= TBSTATE_CHECKED
;
766 switch ( tool
->GetKind() )
769 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
773 // the first item in the radio group is checked by
774 // default to be consistent with wxGTK and the menu
776 button
.fsState
|= TBSTATE_CHECKED
;
785 button
.fsStyle
= TBSTYLE_CHECK
;
789 wxFAIL_MSG( _T("unexpected toolbar button kind") );
793 button
.fsStyle
= TBSTYLE_BUTTON
;
800 lastWasRadio
= isRadio
;
805 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
807 wxLogLastError(wxT("TB_ADDBUTTONS"));
812 // Deal with the controls finally
813 // ------------------------------
815 // adjust the controls size to fit nicely in the toolbar
818 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
820 wxToolBarToolBase
*tool
= node
->GetData();
822 // we calculate the running y coord for vertical toolbars so we need to
823 // get the items size for all items but for the horizontal ones we
824 // don't need to deal with the non controls
825 bool isControl
= tool
->IsControl();
826 if ( !isControl
&& !isVertical
)
829 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
830 // latter only appeared in v4.70 of comctl32.dll
832 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
833 index
, (LPARAM
)(LPRECT
)&r
) )
835 wxLogLastError(wxT("TB_GETITEMRECT"));
840 // can only be control if isVertical
841 y
+= r
.bottom
- r
.top
;
846 wxControl
*control
= tool
->GetControl();
848 wxSize size
= control
->GetSize();
850 // the position of the leftmost controls corner
851 int left
= wxDefaultCoord
;
853 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
854 #ifdef TB_SETBUTTONINFO
855 // available in headers, now check whether it is available now
857 if ( wxTheApp
->GetComCtl32Version() >= 471 )
859 // set the (underlying) separators width to be that of the
862 tbbi
.cbSize
= sizeof(tbbi
);
863 tbbi
.dwMask
= TBIF_SIZE
;
864 tbbi
.cx
= (WORD
)size
.x
;
865 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
866 tool
->GetId(), (LPARAM
)&tbbi
) )
868 // the id is probably invalid?
869 wxLogLastError(wxT("TB_SETBUTTONINFO"));
873 #endif // comctl32.dll 4.71
874 // TB_SETBUTTONINFO unavailable
876 // try adding several separators to fit the controls width
877 int widthSep
= r
.right
- r
.left
;
883 tbb
.fsState
= TBSTATE_ENABLED
;
884 tbb
.fsStyle
= TBSTYLE_SEP
;
886 size_t nSeparators
= size
.x
/ widthSep
;
887 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
889 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
890 index
, (LPARAM
)&tbb
) )
892 wxLogLastError(wxT("TB_INSERTBUTTON"));
898 // remember the number of separators we used - we'd have to
899 // delete all of them later
900 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
902 // adjust the controls width to exactly cover the separators
903 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
906 // position the control itself correctly vertically
907 int height
= r
.bottom
- r
.top
;
908 int diff
= height
- size
.y
;
911 // the control is too high, resize to fit
912 control
->SetSize(wxDefaultCoord
, height
- 2);
923 y
+= height
+ 2*GetMargins().y
;
925 else // horizontal toolbar
927 if ( left
== wxDefaultCoord
)
933 control
->Move(left
, top
+ (diff
+ 1) / 2);
936 // the max index is the "real" number of buttons - i.e. counting even the
937 // separators which we added just for aligning the controls
942 if ( m_maxRows
== 0 )
944 // if not set yet, only one row
948 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
950 if ( m_maxRows
== 0 )
952 // if not set yet, have one column
957 InvalidateBestSize();
961 // ----------------------------------------------------------------------------
963 // ----------------------------------------------------------------------------
965 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
967 wxToolBarToolBase
*tool
= FindById((int)id
);
971 bool toggled
= false; // just to suppress warnings
973 if ( tool
->CanBeToggled() )
975 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
976 toggled
= (state
& TBSTATE_CHECKED
) != 0;
978 // ignore the event when a radio button is released, as this doesn't
979 // seem to happen at all, and is handled otherwise
980 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
983 tool
->Toggle(toggled
);
984 UnToggleRadioGroup(tool
);
987 // OnLeftClick() can veto the button state change - for buttons which
988 // may be toggled only, of couse
989 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
992 tool
->Toggle(!toggled
);
994 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1000 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1002 WXLPARAM
*WXUNUSED(result
))
1005 // First check if this applies to us
1006 NMHDR
*hdr
= (NMHDR
*)lParam
;
1008 // the tooltips control created by the toolbar is sometimes Unicode, even
1009 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1010 UINT code
= hdr
->code
;
1011 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1014 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1015 if ( toolTipWnd
!= hdr
->hwndFrom
)
1018 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1019 int id
= (int)ttText
->hdr
.idFrom
;
1021 wxToolBarToolBase
*tool
= FindById(id
);
1025 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1027 wxUnusedVar(lParam
);
1033 // ----------------------------------------------------------------------------
1035 // ----------------------------------------------------------------------------
1037 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1039 wxToolBarBase::SetToolBitmapSize(size
);
1041 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1044 void wxToolBar::SetRows(int nRows
)
1046 if ( nRows
== m_maxRows
)
1048 // avoid resizing the frame uselessly
1052 // TRUE in wParam means to create at least as many rows, FALSE -
1055 ::SendMessage(GetHwnd(), TB_SETROWS
,
1056 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1064 // The button size is bigger than the bitmap size
1065 wxSize
wxToolBar::GetToolSize() const
1067 // TB_GETBUTTONSIZE is supported from version 4.70
1068 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1069 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1070 && !defined (__DIGITALMARS__)
1071 if ( wxTheApp
->GetComCtl32Version() >= 470 )
1073 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1075 return wxSize(LOWORD(dw
), HIWORD(dw
));
1078 #endif // comctl32.dll 4.70+
1081 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1086 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1089 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1091 for ( ; current
!= 0; current
= current
->GetNext() )
1094 return current
->GetData();
1096 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1097 size_t separators
= tool
->GetSeparatorsCount();
1099 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1100 // otherwise, skip as many items as the separator count, plus the
1102 index
-= separators
? separators
+ 1 : 1;
1108 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1113 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1114 // MBN: when the point ( x, y ) is close to the toolbar border
1115 // TB_HITTEST returns m_nButtons ( not -1 )
1116 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1118 // it's a separator or there is no tool at all there
1119 return (wxToolBarToolBase
*)NULL
;
1122 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1123 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1124 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1126 return m_tools
.Item((size_t)index
)->GetData();
1131 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1135 void wxToolBar::UpdateSize()
1137 // the toolbar size changed
1138 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1140 // we must also refresh the frame after the toolbar size (possibly) changed
1141 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1144 frame
->SendSizeEvent();
1148 // ----------------------------------------------------------------------------
1150 // ---------------------------------------------------------------------------
1152 void wxToolBar::SetWindowStyleFlag(long style
)
1154 // the style bits whose changes force us to recreate the toolbar
1155 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1157 const long styleOld
= GetWindowStyle();
1159 wxToolBarBase::SetWindowStyleFlag(style
);
1161 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1162 // also fatal as we'd then try to recreate the toolbar when it's just being
1164 if ( GetToolsCount() &&
1165 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1167 // to remove the text labels, simply re-realizing the toolbar is enough
1168 // but I don't know of any way to add the text to an existing toolbar
1169 // other than by recreating it entirely
1174 // ----------------------------------------------------------------------------
1176 // ----------------------------------------------------------------------------
1178 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1180 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1181 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1184 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1186 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1187 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1190 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1192 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1193 // without, so we really need to delete the button and recreate it here
1194 wxFAIL_MSG( _T("not implemented") );
1197 // ----------------------------------------------------------------------------
1199 // ----------------------------------------------------------------------------
1201 // Responds to colour changes, and passes event on to children.
1202 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1204 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1206 // Remap the buttons
1209 // Relayout the toolbar
1210 int nrows
= m_maxRows
;
1211 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1216 // let the event propagate further
1220 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1222 if (event
.Leaving() && m_pInTool
)
1229 if ( event
.RightDown() )
1231 // find the tool under the mouse
1233 event
.GetPosition(&x
, &y
);
1235 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1236 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1244 // This handler is required to allow the toolbar to be set to a non-default
1245 // colour: for example, when it must blend in with a notebook page.
1246 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1248 wxColour bgCol
= GetBackgroundColour();
1255 // notice that this 'dumb' implementation may cause flicker for some of the
1256 // controls in which case they should intercept wxEraseEvent and process it
1257 // themselves somehow
1260 ::GetClientRect(GetHwnd(), &rect
);
1262 HBRUSH hBrush
= ::CreateSolidBrush(wxColourToRGB(bgCol
));
1264 HDC hdc
= GetHdcOf((*event
.GetDC()));
1267 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1270 ::FillRect(hdc
, &rect
, hBrush
);
1271 ::DeleteObject(hBrush
);
1274 ::SetMapMode(hdc
, mode
);
1278 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1280 // calculate our minor dimension ourselves - we're confusing the standard
1281 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1283 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1287 if ( GetWindowStyle() & wxTB_VERTICAL
)
1289 w
= r
.right
- r
.left
;
1292 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1299 if (HasFlag( wxTB_FLAT
))
1300 h
= r
.bottom
- r
.top
- 3;
1302 h
= r
.bottom
- r
.top
;
1305 // FIXME: hardcoded separator line height...
1306 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1311 if ( MAKELPARAM(w
, h
) != lParam
)
1313 // size really changed
1317 // message processed
1324 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1326 // erase any dummy separators which we used for aligning the controls if
1329 // first of all, do we have any controls at all?
1330 wxToolBarToolsList::compatibility_iterator node
;
1331 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1333 if ( node
->GetData()->IsControl() )
1339 // no controls, nothing to erase
1343 // prepare the DC on which we'll be drawing
1344 wxClientDC
dc(this);
1345 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1346 dc
.SetPen(*wxTRANSPARENT_PEN
);
1349 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1351 // nothing to redraw anyhow
1356 wxCopyRECTToRect(r
, rectUpdate
);
1358 dc
.SetClippingRegion(rectUpdate
);
1360 // draw the toolbar tools, separators &c normally
1361 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1363 // for each control in the toolbar find all the separators intersecting it
1366 // NB: this is really the only way to do it as we don't know if a separator
1367 // corresponds to a control (i.e. is a dummy one) or a real one
1369 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1371 wxToolBarToolBase
*tool
= node
->GetData();
1372 if ( tool
->IsControl() )
1374 // get the control rect in our client coords
1375 wxControl
*control
= tool
->GetControl();
1376 wxRect rectCtrl
= control
->GetRect();
1378 // iterate over all buttons
1380 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1381 for ( int n
= 0; n
< count
; n
++ )
1383 // is it a separator?
1384 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1387 wxLogDebug(_T("TB_GETBUTTON failed?"));
1392 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1395 // get the bounding rect of the separator
1397 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1400 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1405 // does it intersect the control?
1407 wxCopyRECTToRect(r
, rectItem
);
1408 if ( rectCtrl
.Intersects(rectItem
) )
1410 // yes, do erase it!
1411 dc
.DrawRectangle(rectItem
);
1413 // Necessary in case we use a no-paint-on-size
1414 // style in the parent: the controls can disappear
1415 control
->Refresh(false);
1424 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1426 wxCoord x
= GET_X_LPARAM(lParam
),
1427 y
= GET_Y_LPARAM(lParam
);
1428 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1430 // cursor left current tool
1431 if( tool
!= m_pInTool
&& !tool
)
1437 // cursor entered a tool
1438 if( tool
!= m_pInTool
&& tool
)
1441 OnMouseEnter( tool
->GetId() );
1445 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1450 // we don't handle mouse moves, so always pass the message to
1451 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1452 HandleMouseMove(wParam
, lParam
);
1456 if ( HandleSize(wParam
, lParam
) )
1462 if ( HandlePaint(wParam
, lParam
) )
1467 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1470 // ----------------------------------------------------------------------------
1471 // private functions
1472 // ----------------------------------------------------------------------------
1474 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1480 wxLogLastError(_T("CreateCompatibleDC"));
1485 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1489 wxLogLastError(_T("SelectObject"));
1494 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1496 for ( int i
= 0; i
< width
; i
++ )
1498 for ( int j
= 0; j
< height
; j
++ )
1500 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1502 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1504 COLORREF col
= cmap
[k
].from
;
1505 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1506 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1507 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1509 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1518 // VZ: I leave here my attempts to map the bitmap to the system colours
1519 // faster by using BitBlt() even though it's broken currently - but
1520 // maybe someone else can finish it? It should be faster than iterating
1521 // over all pixels...
1523 MemoryHDC hdcMask
, hdcDst
;
1524 if ( !hdcMask
|| !hdcDst
)
1526 wxLogLastError(_T("CreateCompatibleDC"));
1531 // create the target bitmap
1532 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1535 wxLogLastError(_T("CreateCompatibleBitmap"));
1540 // create the monochrome mask bitmap
1541 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1544 wxLogLastError(_T("CreateBitmap(mono)"));
1546 ::DeleteObject(hbmpDst
);
1551 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1552 bmpInMask(hdcMask
, hbmpMask
);
1555 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1557 // create the mask for this colour
1558 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1559 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1561 // replace this colour with the target one in the dst bitmap
1562 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1563 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1565 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1568 MAKEROP4(PATCOPY
, SRCCOPY
));
1570 (void)::SelectObject(hdcDst
, hbrOld
);
1571 ::DeleteObject(hbr
);
1574 ::DeleteObject((HBITMAP
)bitmap
);
1576 return (WXHBITMAP
)hbmpDst
;
1580 #endif // wxUSE_TOOLBAR && Win95