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(_WIN32_WCE) || (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !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
)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 class wxToolBarTool
: public wxToolBarToolBase
136 wxToolBarTool(wxToolBar
*tbar
,
138 const wxString
& label
,
139 const wxBitmap
& bmpNormal
,
140 const wxBitmap
& bmpDisabled
,
142 wxObject
*clientData
,
143 const wxString
& shortHelp
,
144 const wxString
& longHelp
)
145 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
146 clientData
, shortHelp
, longHelp
)
151 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
152 : wxToolBarToolBase(tbar
, control
)
157 virtual void SetLabel(const wxString
& label
)
159 if ( label
== m_label
)
162 wxToolBarToolBase::SetLabel(label
);
164 // we need to update the label shown in the toolbar because it has a
165 // pointer to the internal buffer of the old label
167 // TODO: use TB_SETBUTTONINFO
170 // set/get the number of separators which we use to cover the space used by
171 // a control in the toolbar
172 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
173 size_t GetSeparatorsCount() const { return m_nSepCount
; }
178 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
182 // ============================================================================
184 // ============================================================================
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
191 const wxString
& label
,
192 const wxBitmap
& bmpNormal
,
193 const wxBitmap
& bmpDisabled
,
195 wxObject
*clientData
,
196 const wxString
& shortHelp
,
197 const wxString
& longHelp
)
199 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
200 clientData
, shortHelp
, longHelp
);
203 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
205 return new wxToolBarTool(this, control
);
208 // ----------------------------------------------------------------------------
209 // wxToolBar construction
210 // ----------------------------------------------------------------------------
212 void wxToolBar::Init()
218 m_defaultWidth
= DEFAULTBITMAPX
;
219 m_defaultHeight
= DEFAULTBITMAPY
;
224 bool wxToolBar::Create(wxWindow
*parent
,
229 const wxString
& name
)
231 // common initialisation
232 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
235 // MSW-specific initialisation
236 if ( !MSWCreateToolbar(pos
, size
) )
239 wxSetCCUnicodeFormat(GetHwnd());
241 // set up the colors and fonts
242 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
243 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
245 // workaround for flat toolbar on Windows XP classic style
247 if ( style
& wxTB_FLAT
)
249 wxUxThemeEngine
*p
= wxUxThemeEngine::Get();
250 if ( !p
|| !p
->IsThemeActive() )
252 DWORD dwToolbarStyle
;
254 dwToolbarStyle
= (DWORD
)::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L );
256 if ((dwToolbarStyle
& TBSTYLE_FLAT
) == 0)
258 dwToolbarStyle
|= TBSTYLE_FLAT
;
259 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, (LPARAM
)dwToolbarStyle
);
268 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
270 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
273 // toolbar-specific post initialisation
274 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
279 void wxToolBar::Recreate()
281 const HWND hwndOld
= GetHwnd();
284 // we haven't been created yet, no need to recreate
288 // get the position and size before unsubclassing the old toolbar
289 const wxPoint pos
= GetPosition();
290 const wxSize size
= GetSize();
294 if ( !MSWCreateToolbar(pos
, size
) )
297 wxFAIL_MSG( _T("recreating the toolbar failed") );
302 // reparent all our children under the new toolbar
303 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
305 node
= node
->GetNext() )
307 wxWindow
*win
= node
->GetData();
308 if ( !win
->IsTopLevel() )
309 ::SetParent(GetHwndOf(win
), GetHwnd());
312 // only destroy the old toolbar now -- after all the children had been
314 ::DestroyWindow(hwndOld
);
316 // it is for the old bitmap control and can't be used with the new one
319 ::DeleteObject((HBITMAP
) m_hBitmap
);
327 wxToolBar::~wxToolBar()
329 // we must refresh the frame size when the toolbar is deleted but the frame
330 // is not - otherwise toolbar leaves a hole in the place it used to occupy
331 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
332 if ( frame
&& !frame
->IsBeingDeleted() )
334 frame
->SendSizeEvent();
339 ::DeleteObject((HBITMAP
) m_hBitmap
);
343 wxSize
wxToolBar::DoGetBestSize() const
348 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
350 // maybe an old (< 0x400) Windows version? try to approximate the
351 // toolbar size ourselves
352 sizeBest
= GetToolSize();
353 sizeBest
.x
*= GetToolsCount();
355 // reverse horz and vertical components if necessary
356 if ( HasFlag(wxTB_VERTICAL
) )
359 sizeBest
.x
= sizeBest
.y
;
365 sizeBest
.x
= size
.cx
;
366 sizeBest
.y
= size
.cy
;
372 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
374 // toolbars never have border, giving one to them results in broken
376 WXDWORD msStyle
= wxControl::MSWGetStyle
378 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
381 // always include this one, it never hurts and setting it later only if we
382 // do have tooltips wouldn't work
383 msStyle
|= TBSTYLE_TOOLTIPS
;
385 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
387 // static as it doesn't change during the program lifetime
388 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
390 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
391 // style with 6.00 (part of Windows XP) leads to the toolbar with
392 // incorrect background colour - and not using it still results in the
393 // correct (flat) toolbar, so don't use it there
394 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
396 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
399 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
401 msStyle
|= TBSTYLE_LIST
;
405 if ( style
& wxTB_NODIVIDER
)
406 msStyle
|= CCS_NODIVIDER
;
408 if ( style
& wxTB_NOALIGN
)
409 msStyle
|= CCS_NOPARENTALIGN
;
411 if ( style
& wxTB_VERTICAL
)
417 // ----------------------------------------------------------------------------
418 // adding/removing tools
419 // ----------------------------------------------------------------------------
421 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
423 // nothing special to do here - we really create the toolbar buttons in
427 InvalidateBestSize();
431 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
433 // the main difficulty we have here is with the controls in the toolbars:
434 // as we (sometimes) use several separators to cover up the space used by
435 // them, the indices are not the same for us and the toolbar
437 // first determine the position of the first button to delete: it may be
438 // different from pos if we use several separators to cover the space used
440 wxToolBarToolsList::compatibility_iterator node
;
441 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
443 wxToolBarToolBase
*tool2
= node
->GetData();
446 // let node point to the next node in the list
447 node
= node
->GetNext();
452 if ( tool2
->IsControl() )
454 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
458 // now determine the number of buttons to delete and the area taken by them
459 size_t nButtonsToDelete
= 1;
461 // get the size of the button we're going to delete
463 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
465 wxLogLastError(_T("TB_GETITEMRECT"));
468 int width
= r
.right
- r
.left
;
470 if ( tool
->IsControl() )
472 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
474 width
*= nButtonsToDelete
;
477 // do delete all buttons
478 m_nButtons
-= nButtonsToDelete
;
479 while ( nButtonsToDelete
-- > 0 )
481 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
483 wxLogLastError(wxT("TB_DELETEBUTTON"));
491 // and finally reposition all the controls after this button (the toolbar
492 // takes care of all normal items)
493 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
495 wxToolBarToolBase
*tool2
= node
->GetData();
496 if ( tool2
->IsControl() )
499 wxControl
*control
= tool2
->GetControl();
500 control
->GetPosition(&x
, NULL
);
501 control
->Move(x
- width
, wxDefaultCoord
);
505 InvalidateBestSize();
509 bool wxToolBar::Realize()
511 const size_t nTools
= GetToolsCount();
518 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
520 bool doRemap
, doRemapBg
, doTransparent
;
521 if (wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 2)
523 doRemapBg
= doRemap
= false;
524 doTransparent
= true;
527 { doRemap
= !wxSystemOptions::HasOption(wxT("msw.remap"))
528 || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1;
529 doRemapBg
= !doRemap
;
530 doTransparent
= false;
533 // delete all old buttons, if any
534 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
536 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
538 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
542 // First, add the bitmap: we use one bitmap for all toolbar buttons
543 // ----------------------------------------------------------------
545 wxToolBarToolsList::compatibility_iterator node
;
549 if ( HasFlag(wxTB_NOICONS
) )
551 // no icons, don't leave space for them
555 else // do show icons
557 // if we already have a bitmap, we'll replace the existing one --
558 // otherwise we'll install a new one
559 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
561 sizeBmp
.x
= m_defaultWidth
;
562 sizeBmp
.y
= m_defaultHeight
;
564 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
565 totalBitmapHeight
= m_defaultHeight
;
567 // Create a bitmap and copy all the tool bitmaps to it
568 wxMemoryDC dcAllButtons
;
569 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
570 dcAllButtons
.SelectObject(bitmap
);
572 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
574 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
575 dcAllButtons
.Clear();
577 m_hBitmap
= bitmap
.GetHBITMAP();
578 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
582 dcAllButtons
.SelectObject(wxNullBitmap
);
584 // Even if we're not remapping the bitmap
585 // content, we still have to remap the background.
586 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
587 totalBitmapWidth
, totalBitmapHeight
);
589 dcAllButtons
.SelectObject(bitmap
);
592 // the button position
595 // the number of buttons (not separators)
598 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
600 wxToolBarToolBase
*tool
= node
->GetData();
601 if ( tool
->IsButton() )
603 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
606 int xOffset
= wxMax(0, (m_defaultWidth
- bmp
.GetWidth())/2);
607 int yOffset
= wxMax(0, (m_defaultHeight
- bmp
.GetHeight())/2);
608 // notice the last parameter: do use mask
609 dcAllButtons
.DrawBitmap(bmp
, x
+xOffset
, yOffset
, true);
613 wxFAIL_MSG( _T("invalid tool button bitmap") );
616 // still inc width and number of buttons because otherwise the
617 // subsequent buttons will all be shifted which is rather confusing
618 // (and like this you'd see immediately which bitmap was bad)
624 dcAllButtons
.SelectObject(wxNullBitmap
);
626 // don't delete this HBITMAP!
627 bitmap
.SetHBITMAP(0);
631 // Map to system colours
632 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
633 totalBitmapWidth
, totalBitmapHeight
);
636 bool addBitmap
= true;
638 if ( oldToolBarBitmap
)
640 #ifdef TB_REPLACEBITMAP
641 if ( wxTheApp
->GetComCtl32Version() >= 400 )
643 TBREPLACEBITMAP replaceBitmap
;
644 replaceBitmap
.hInstOld
= NULL
;
645 replaceBitmap
.hInstNew
= NULL
;
646 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
647 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
648 replaceBitmap
.nButtons
= nButtons
;
649 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
650 0, (LPARAM
) &replaceBitmap
) )
652 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
655 ::DeleteObject(oldToolBarBitmap
);
661 #endif // TB_REPLACEBITMAP
663 // we can't replace the old bitmap, so we will add another one
664 // (awfully inefficient, but what else to do?) and shift the bitmap
665 // indices accordingly
668 bitmapId
= m_nButtons
;
672 if ( addBitmap
) // no old bitmap or we can't replace it
674 TBADDBITMAP addBitmap
;
676 addBitmap
.nID
= (UINT
) hBitmap
;
677 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
678 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
680 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
685 // don't call SetToolBitmapSize() as we don't want to change the values of
686 // m_defaultWidth/Height
687 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
688 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
690 wxLogLastError(_T("TB_SETBITMAPSIZE"));
693 // Next add the buttons and separators
694 // -----------------------------------
696 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
698 // this array will hold the indices of all controls in the toolbar
699 wxArrayInt controlIds
;
701 bool lastWasRadio
= false;
703 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
705 wxToolBarToolBase
*tool
= node
->GetData();
707 // don't add separators to the vertical toolbar with old comctl32.dll
708 // versions as they didn't handle this properly
709 if ( isVertical
&& tool
->IsSeparator() &&
710 wxTheApp
->GetComCtl32Version() <= 472 )
716 TBBUTTON
& button
= buttons
[i
];
718 wxZeroMemory(button
);
720 bool isRadio
= false;
721 switch ( tool
->GetStyle() )
723 case wxTOOL_STYLE_CONTROL
:
724 button
.idCommand
= tool
->GetId();
725 // fall through: create just a separator too
727 case wxTOOL_STYLE_SEPARATOR
:
728 button
.fsState
= TBSTATE_ENABLED
;
729 button
.fsStyle
= TBSTYLE_SEP
;
732 case wxTOOL_STYLE_BUTTON
:
733 if ( !HasFlag(wxTB_NOICONS
) )
734 button
.iBitmap
= bitmapId
;
736 if ( HasFlag(wxTB_TEXT
) )
738 const wxString
& label
= tool
->GetLabel();
739 if ( !label
.empty() )
741 button
.iString
= (int)label
.c_str();
745 button
.idCommand
= tool
->GetId();
747 if ( tool
->IsEnabled() )
748 button
.fsState
|= TBSTATE_ENABLED
;
749 if ( tool
->IsToggled() )
750 button
.fsState
|= TBSTATE_CHECKED
;
752 switch ( tool
->GetKind() )
755 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
759 // the first item in the radio group is checked by
760 // default to be consistent with wxGTK and the menu
762 button
.fsState
|= TBSTATE_CHECKED
;
771 button
.fsStyle
= TBSTYLE_CHECK
;
775 wxFAIL_MSG( _T("unexpected toolbar button kind") );
779 button
.fsStyle
= TBSTYLE_BUTTON
;
786 lastWasRadio
= isRadio
;
791 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
793 wxLogLastError(wxT("TB_ADDBUTTONS"));
798 // Deal with the controls finally
799 // ------------------------------
801 // adjust the controls size to fit nicely in the toolbar
804 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
806 wxToolBarToolBase
*tool
= node
->GetData();
808 // we calculate the running y coord for vertical toolbars so we need to
809 // get the items size for all items but for the horizontal ones we
810 // don't need to deal with the non controls
811 bool isControl
= tool
->IsControl();
812 if ( !isControl
&& !isVertical
)
815 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
816 // latter only appeared in v4.70 of comctl32.dll
818 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
819 index
, (LPARAM
)(LPRECT
)&r
) )
821 wxLogLastError(wxT("TB_GETITEMRECT"));
826 // can only be control if isVertical
827 y
+= r
.bottom
- r
.top
;
832 wxControl
*control
= tool
->GetControl();
834 wxSize size
= control
->GetSize();
836 // the position of the leftmost controls corner
837 int left
= wxDefaultCoord
;
839 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
840 #ifdef TB_SETBUTTONINFO
841 // available in headers, now check whether it is available now
843 if ( wxTheApp
->GetComCtl32Version() >= 471 )
845 // set the (underlying) separators width to be that of the
848 tbbi
.cbSize
= sizeof(tbbi
);
849 tbbi
.dwMask
= TBIF_SIZE
;
850 tbbi
.cx
= (WORD
)size
.x
;
851 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
852 tool
->GetId(), (LPARAM
)&tbbi
) )
854 // the id is probably invalid?
855 wxLogLastError(wxT("TB_SETBUTTONINFO"));
859 #endif // comctl32.dll 4.71
860 // TB_SETBUTTONINFO unavailable
862 // try adding several separators to fit the controls width
863 int widthSep
= r
.right
- r
.left
;
869 tbb
.fsState
= TBSTATE_ENABLED
;
870 tbb
.fsStyle
= TBSTYLE_SEP
;
872 size_t nSeparators
= size
.x
/ widthSep
;
873 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
875 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
876 index
, (LPARAM
)&tbb
) )
878 wxLogLastError(wxT("TB_INSERTBUTTON"));
884 // remember the number of separators we used - we'd have to
885 // delete all of them later
886 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
888 // adjust the controls width to exactly cover the separators
889 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
892 // position the control itself correctly vertically
893 int height
= r
.bottom
- r
.top
;
894 int diff
= height
- size
.y
;
897 // the control is too high, resize to fit
898 control
->SetSize(wxDefaultCoord
, height
- 2);
909 y
+= height
+ 2*GetMargins().y
;
911 else // horizontal toolbar
913 if ( left
== wxDefaultCoord
)
919 control
->Move(left
, top
+ (diff
+ 1) / 2);
922 // the max index is the "real" number of buttons - i.e. counting even the
923 // separators which we added just for aligning the controls
928 if ( m_maxRows
== 0 )
930 // if not set yet, only one row
934 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
936 if ( m_maxRows
== 0 )
938 // if not set yet, have one column
943 InvalidateBestSize();
947 // ----------------------------------------------------------------------------
949 // ----------------------------------------------------------------------------
951 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
953 wxToolBarToolBase
*tool
= FindById((int)id
);
957 bool toggled
= false; // just to suppress warnings
959 if ( tool
->CanBeToggled() )
961 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
962 toggled
= (state
& TBSTATE_CHECKED
) != 0;
964 // ignore the event when a radio button is released, as this doesn't seem to
965 // happen at all, and is handled otherwise
966 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
969 tool
->Toggle(toggled
);
970 UnToggleRadioGroup(tool
);
973 // OnLeftClick() can veto the button state change - for buttons which
974 // may be toggled only, of couse
975 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
978 tool
->Toggle(!toggled
);
980 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
986 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
988 WXLPARAM
*WXUNUSED(result
))
991 // First check if this applies to us
992 NMHDR
*hdr
= (NMHDR
*)lParam
;
994 // the tooltips control created by the toolbar is sometimes Unicode, even
995 // in an ANSI application - this seems to be a bug in comctl32.dll v5
996 UINT code
= hdr
->code
;
997 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1000 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1001 if ( toolTipWnd
!= hdr
->hwndFrom
)
1004 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1005 int id
= (int)ttText
->hdr
.idFrom
;
1007 wxToolBarToolBase
*tool
= FindById(id
);
1011 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1017 // ----------------------------------------------------------------------------
1019 // ----------------------------------------------------------------------------
1021 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1023 wxToolBarBase::SetToolBitmapSize(size
);
1025 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1028 void wxToolBar::SetRows(int nRows
)
1030 if ( nRows
== m_maxRows
)
1032 // avoid resizing the frame uselessly
1036 // TRUE in wParam means to create at least as many rows, FALSE -
1039 ::SendMessage(GetHwnd(), TB_SETROWS
,
1040 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1048 // The button size is bigger than the bitmap size
1049 wxSize
wxToolBar::GetToolSize() const
1051 // TB_GETBUTTONSIZE is supported from version 4.70
1052 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1053 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1054 && !defined (__DIGITALMARS__)
1055 if ( wxTheApp
->GetComCtl32Version() >= 470 )
1057 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1059 return wxSize(LOWORD(dw
), HIWORD(dw
));
1062 #endif // comctl32.dll 4.70+
1065 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1070 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1073 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1075 for ( ; current
!= 0; current
= current
->GetNext() )
1078 return current
->GetData();
1080 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1081 size_t separators
= tool
->GetSeparatorsCount();
1083 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1084 // otherwise, skip as many items as the separator count, plus the
1086 index
-= separators
? separators
+ 1 : 1;
1092 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1097 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1098 // MBN: when the point ( x, y ) is close to the toolbar border
1099 // TB_HITTEST returns m_nButtons ( not -1 )
1100 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1102 // it's a separator or there is no tool at all there
1103 return (wxToolBarToolBase
*)NULL
;
1106 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1107 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1108 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1110 return m_tools
.Item((size_t)index
)->GetData();
1115 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1119 void wxToolBar::UpdateSize()
1121 // the toolbar size changed
1122 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1124 // we must also refresh the frame after the toolbar size (possibly) changed
1125 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1128 frame
->SendSizeEvent();
1132 // ----------------------------------------------------------------------------
1134 // ---------------------------------------------------------------------------
1136 void wxToolBar::SetWindowStyleFlag(long style
)
1138 // the style bits whose changes force us to recreate the toolbar
1139 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1141 const long styleOld
= GetWindowStyle();
1143 wxToolBarBase::SetWindowStyleFlag(style
);
1145 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1146 // also fatal as we'd then try to recreate the toolbar when it's just being
1148 if ( GetToolsCount() &&
1149 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1151 // to remove the text labels, simply re-realizing the toolbar is enough
1152 // but I don't know of any way to add the text to an existing toolbar
1153 // other than by recreating it entirely
1158 // ----------------------------------------------------------------------------
1160 // ----------------------------------------------------------------------------
1162 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1164 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1165 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1168 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1170 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1171 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1174 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1176 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1177 // without, so we really need to delete the button and recreate it here
1178 wxFAIL_MSG( _T("not implemented") );
1181 // ----------------------------------------------------------------------------
1183 // ----------------------------------------------------------------------------
1185 // Responds to colour changes, and passes event on to children.
1186 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1188 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1190 // Remap the buttons
1193 // Relayout the toolbar
1194 int nrows
= m_maxRows
;
1195 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1200 // let the event propagate further
1204 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1206 if (event
.Leaving() && m_pInTool
)
1213 if (event
.RightDown())
1215 // For now, we don't have an id. Later we could
1216 // try finding the tool.
1217 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1225 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1227 // calculate our minor dimension ourselves - we're confusing the standard
1228 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1230 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1234 if ( GetWindowStyle() & wxTB_VERTICAL
)
1236 w
= r
.right
- r
.left
;
1239 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1246 if (HasFlag( wxTB_FLAT
))
1247 h
= r
.bottom
- r
.top
- 3;
1249 h
= r
.bottom
- r
.top
;
1252 // FIXME: hardcoded separator line height...
1253 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1258 if ( MAKELPARAM(w
, h
) != lParam
)
1260 // size really changed
1264 // message processed
1271 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1273 // erase any dummy separators which we used for aligning the controls if
1276 // first of all, do we have any controls at all?
1277 wxToolBarToolsList::compatibility_iterator node
;
1278 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1280 if ( node
->GetData()->IsControl() )
1286 // no controls, nothing to erase
1290 // prepare the DC on which we'll be drawing
1291 wxClientDC
dc(this);
1292 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1293 dc
.SetPen(*wxTRANSPARENT_PEN
);
1296 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1298 // nothing to redraw anyhow
1303 wxCopyRECTToRect(r
, rectUpdate
);
1305 dc
.SetClippingRegion(rectUpdate
);
1307 // draw the toolbar tools, separators &c normally
1308 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1310 // for each control in the toolbar find all the separators intersecting it
1313 // NB: this is really the only way to do it as we don't know if a separator
1314 // corresponds to a control (i.e. is a dummy one) or a real one
1316 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1318 wxToolBarToolBase
*tool
= node
->GetData();
1319 if ( tool
->IsControl() )
1321 // get the control rect in our client coords
1322 wxControl
*control
= tool
->GetControl();
1323 wxRect rectCtrl
= control
->GetRect();
1325 // iterate over all buttons
1327 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1328 for ( int n
= 0; n
< count
; n
++ )
1330 // is it a separator?
1331 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1334 wxLogDebug(_T("TB_GETBUTTON failed?"));
1339 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1342 // get the bounding rect of the separator
1344 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1347 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1352 // does it intersect the control?
1354 wxCopyRECTToRect(r
, rectItem
);
1355 if ( rectCtrl
.Intersects(rectItem
) )
1357 // yes, do erase it!
1358 dc
.DrawRectangle(rectItem
);
1360 // Necessary in case we use a no-paint-on-size
1361 // style in the parent: the controls can disappear
1362 control
->Refresh(false);
1371 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1373 wxCoord x
= GET_X_LPARAM(lParam
),
1374 y
= GET_Y_LPARAM(lParam
);
1375 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1377 // cursor left current tool
1378 if( tool
!= m_pInTool
&& !tool
)
1384 // cursor entered a tool
1385 if( tool
!= m_pInTool
&& tool
)
1388 OnMouseEnter( tool
->GetId() );
1392 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1397 if ( HandleSize(wParam
, lParam
) )
1402 // we don't handle mouse moves, so always pass the message to
1403 // wxControl::MSWWindowProc
1404 HandleMouseMove(wParam
, lParam
);
1408 if ( HandlePaint(wParam
, lParam
) )
1412 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1415 // ----------------------------------------------------------------------------
1416 // private functions
1417 // ----------------------------------------------------------------------------
1419 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1425 wxLogLastError(_T("CreateCompatibleDC"));
1430 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1434 wxLogLastError(_T("SelectObject"));
1439 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1441 for ( int i
= 0; i
< width
; i
++ )
1443 for ( int j
= 0; j
< height
; j
++ )
1445 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1447 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1449 COLORREF col
= cmap
[k
].from
;
1450 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1451 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1452 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1454 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1463 // VZ: I leave here my attempts to map the bitmap to the system colours
1464 // faster by using BitBlt() even though it's broken currently - but
1465 // maybe someone else can finish it? It should be faster than iterating
1466 // over all pixels...
1468 MemoryHDC hdcMask
, hdcDst
;
1469 if ( !hdcMask
|| !hdcDst
)
1471 wxLogLastError(_T("CreateCompatibleDC"));
1476 // create the target bitmap
1477 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1480 wxLogLastError(_T("CreateCompatibleBitmap"));
1485 // create the monochrome mask bitmap
1486 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1489 wxLogLastError(_T("CreateBitmap(mono)"));
1491 ::DeleteObject(hbmpDst
);
1496 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1497 bmpInMask(hdcMask
, hbmpMask
);
1500 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1502 // create the mask for this colour
1503 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1504 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1506 // replace this colour with the target one in the dst bitmap
1507 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1508 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1510 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1513 MAKEROP4(PATCOPY
, SRCCOPY
));
1515 (void)::SelectObject(hdcDst
, hbrOld
);
1516 ::DeleteObject(hbr
);
1519 ::DeleteObject((HBITMAP
)bitmap
);
1521 return (WXHBITMAP
)hbmpDst
;
1525 #endif // wxUSE_TOOLBAR && Win95