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)
88 // these values correspond to those used by comctl32.dll
89 #define DEFAULTBITMAPX 16
90 #define DEFAULTBITMAPY 15
91 #define DEFAULTBUTTONX 24
92 #define DEFAULTBUTTONY 24
93 #define DEFAULTBARHEIGHT 27
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
111 style ( wxNO_BORDER | wxTB_HORIZONTAL)
120 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
121 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
122 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 class wxToolBarTool
: public wxToolBarToolBase
132 wxToolBarTool(wxToolBar
*tbar
,
134 const wxString
& label
,
135 const wxBitmap
& bmpNormal
,
136 const wxBitmap
& bmpDisabled
,
138 wxObject
*clientData
,
139 const wxString
& shortHelp
,
140 const wxString
& longHelp
)
141 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
142 clientData
, shortHelp
, longHelp
)
147 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
148 : wxToolBarToolBase(tbar
, control
)
153 virtual void SetLabel(const wxString
& label
)
155 if ( label
== m_label
)
158 wxToolBarToolBase::SetLabel(label
);
160 // we need to update the label shown in the toolbar because it has a
161 // pointer to the internal buffer of the old label
163 // TODO: use TB_SETBUTTONINFO
166 // set/get the number of separators which we use to cover the space used by
167 // a control in the toolbar
168 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
169 size_t GetSeparatorsCount() const { return m_nSepCount
; }
174 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
178 // ============================================================================
180 // ============================================================================
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
187 const wxString
& label
,
188 const wxBitmap
& bmpNormal
,
189 const wxBitmap
& bmpDisabled
,
191 wxObject
*clientData
,
192 const wxString
& shortHelp
,
193 const wxString
& longHelp
)
195 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
196 clientData
, shortHelp
, longHelp
);
199 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
201 return new wxToolBarTool(this, control
);
204 // ----------------------------------------------------------------------------
205 // wxToolBar construction
206 // ----------------------------------------------------------------------------
208 void wxToolBar::Init()
214 m_defaultWidth
= DEFAULTBITMAPX
;
215 m_defaultHeight
= DEFAULTBITMAPY
;
220 bool wxToolBar::Create(wxWindow
*parent
,
225 const wxString
& name
)
227 // common initialisation
228 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
231 // MSW-specific initialisation
232 if ( !MSWCreateToolbar(pos
, size
) )
235 wxSetCCUnicodeFormat(GetHwnd());
237 // set up the colors and fonts
238 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
239 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
241 // workaround for flat toolbar on Windows XP classic style
243 if ( style
& wxTB_FLAT
)
245 wxUxThemeEngine
*p
= wxUxThemeEngine::Get();
246 if ( !p
|| !p
->IsThemeActive() )
248 DWORD dwToolbarStyle
;
250 dwToolbarStyle
= (DWORD
)::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L );
252 if ((dwToolbarStyle
& TBSTYLE_FLAT
) == 0)
254 dwToolbarStyle
|= TBSTYLE_FLAT
;
255 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, (LPARAM
)dwToolbarStyle
);
264 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
266 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
269 // toolbar-specific post initialisation
270 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
275 void wxToolBar::Recreate()
277 const HWND hwndOld
= GetHwnd();
280 // we haven't been created yet, no need to recreate
284 // get the position and size before unsubclassing the old toolbar
285 const wxPoint pos
= GetPosition();
286 const wxSize size
= GetSize();
290 if ( !MSWCreateToolbar(pos
, size
) )
293 wxFAIL_MSG( _T("recreating the toolbar failed") );
298 // reparent all our children under the new toolbar
299 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
301 node
= node
->GetNext() )
303 wxWindow
*win
= node
->GetData();
304 if ( !win
->IsTopLevel() )
305 ::SetParent(GetHwndOf(win
), GetHwnd());
308 // only destroy the old toolbar now -- after all the children had been
310 ::DestroyWindow(hwndOld
);
312 // it is for the old bitmap control and can't be used with the new one
315 ::DeleteObject((HBITMAP
) m_hBitmap
);
323 wxToolBar::~wxToolBar()
325 // we must refresh the frame size when the toolbar is deleted but the frame
326 // is not - otherwise toolbar leaves a hole in the place it used to occupy
327 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
328 if ( frame
&& !frame
->IsBeingDeleted() )
330 frame
->SendSizeEvent();
335 ::DeleteObject((HBITMAP
) m_hBitmap
);
339 wxSize
wxToolBar::DoGetBestSize() const
341 wxSize sizeBest
= GetToolSize();
342 sizeBest
.x
*= GetToolsCount();
344 // reverse horz and vertical components if necessary
345 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
348 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
350 // toolbars never have border, giving one to them results in broken
352 WXDWORD msStyle
= wxControl::MSWGetStyle
354 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
357 // always include this one, it never hurts and setting it later only if we
358 // do have tooltips wouldn't work
359 msStyle
|= TBSTYLE_TOOLTIPS
;
361 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
363 // static as it doesn't change during the program lifetime
364 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
366 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
367 // style with 6.00 (part of Windows XP) leads to the toolbar with
368 // incorrect background colour - and not using it still results in the
369 // correct (flat) toolbar, so don't use it there
370 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
372 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
375 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
377 msStyle
|= TBSTYLE_LIST
;
381 if ( style
& wxTB_NODIVIDER
)
382 msStyle
|= CCS_NODIVIDER
;
384 if ( style
& wxTB_NOALIGN
)
385 msStyle
|= CCS_NOPARENTALIGN
;
387 if ( style
& wxTB_VERTICAL
)
393 // ----------------------------------------------------------------------------
394 // adding/removing tools
395 // ----------------------------------------------------------------------------
397 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
399 // nothing special to do here - we really create the toolbar buttons in
403 InvalidateBestSize();
407 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
409 // the main difficulty we have here is with the controls in the toolbars:
410 // as we (sometimes) use several separators to cover up the space used by
411 // them, the indices are not the same for us and the toolbar
413 // first determine the position of the first button to delete: it may be
414 // different from pos if we use several separators to cover the space used
416 wxToolBarToolsList::compatibility_iterator node
;
417 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
419 wxToolBarToolBase
*tool2
= node
->GetData();
422 // let node point to the next node in the list
423 node
= node
->GetNext();
428 if ( tool2
->IsControl() )
430 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
434 // now determine the number of buttons to delete and the area taken by them
435 size_t nButtonsToDelete
= 1;
437 // get the size of the button we're going to delete
439 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
441 wxLogLastError(_T("TB_GETITEMRECT"));
444 int width
= r
.right
- r
.left
;
446 if ( tool
->IsControl() )
448 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
450 width
*= nButtonsToDelete
;
453 // do delete all buttons
454 m_nButtons
-= nButtonsToDelete
;
455 while ( nButtonsToDelete
-- > 0 )
457 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
459 wxLogLastError(wxT("TB_DELETEBUTTON"));
467 // and finally reposition all the controls after this button (the toolbar
468 // takes care of all normal items)
469 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
471 wxToolBarToolBase
*tool2
= node
->GetData();
472 if ( tool2
->IsControl() )
475 wxControl
*control
= tool2
->GetControl();
476 control
->GetPosition(&x
, NULL
);
477 control
->Move(x
- width
, wxDefaultCoord
);
481 InvalidateBestSize();
485 bool wxToolBar::Realize()
487 const size_t nTools
= GetToolsCount();
494 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
496 bool doRemap
, doRemapBg
, doTransparent
;
497 if (wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 2)
499 doRemapBg
= doRemap
= false;
500 doTransparent
= true;
503 { doRemap
= !wxSystemOptions::HasOption(wxT("msw.remap"))
504 || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1;
505 doRemapBg
= !doRemap
;
506 doTransparent
= false;
509 // delete all old buttons, if any
510 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
512 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
514 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
518 // First, add the bitmap: we use one bitmap for all toolbar buttons
519 // ----------------------------------------------------------------
521 wxToolBarToolsList::compatibility_iterator node
;
525 if ( HasFlag(wxTB_NOICONS
) )
527 // no icons, don't leave space for them
531 else // do show icons
533 // if we already have a bitmap, we'll replace the existing one --
534 // otherwise we'll install a new one
535 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
537 sizeBmp
.x
= m_defaultWidth
;
538 sizeBmp
.y
= m_defaultHeight
;
540 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
541 totalBitmapHeight
= m_defaultHeight
;
543 // Create a bitmap and copy all the tool bitmaps to it
544 wxMemoryDC dcAllButtons
;
545 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
546 dcAllButtons
.SelectObject(bitmap
);
548 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
550 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
551 dcAllButtons
.Clear();
553 m_hBitmap
= bitmap
.GetHBITMAP();
554 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
558 dcAllButtons
.SelectObject(wxNullBitmap
);
560 // Even if we're not remapping the bitmap
561 // content, we still have to remap the background.
562 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
563 totalBitmapWidth
, totalBitmapHeight
);
565 dcAllButtons
.SelectObject(bitmap
);
568 // the button position
571 // the number of buttons (not separators)
574 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
576 wxToolBarToolBase
*tool
= node
->GetData();
577 if ( tool
->IsButton() )
579 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
582 int xOffset
= wxMax(0, (m_defaultWidth
- bmp
.GetWidth())/2);
583 int yOffset
= wxMax(0, (m_defaultHeight
- bmp
.GetHeight())/2);
584 // notice the last parameter: do use mask
585 dcAllButtons
.DrawBitmap(bmp
, x
+xOffset
, yOffset
, true);
589 wxFAIL_MSG( _T("invalid tool button bitmap") );
592 // still inc width and number of buttons because otherwise the
593 // subsequent buttons will all be shifted which is rather confusing
594 // (and like this you'd see immediately which bitmap was bad)
600 dcAllButtons
.SelectObject(wxNullBitmap
);
602 // don't delete this HBITMAP!
603 bitmap
.SetHBITMAP(0);
607 // Map to system colours
608 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
609 totalBitmapWidth
, totalBitmapHeight
);
612 bool addBitmap
= true;
614 if ( oldToolBarBitmap
)
616 #ifdef TB_REPLACEBITMAP
617 if ( wxTheApp
->GetComCtl32Version() >= 400 )
619 TBREPLACEBITMAP replaceBitmap
;
620 replaceBitmap
.hInstOld
= NULL
;
621 replaceBitmap
.hInstNew
= NULL
;
622 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
623 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
624 replaceBitmap
.nButtons
= nButtons
;
625 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
626 0, (LPARAM
) &replaceBitmap
) )
628 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
631 ::DeleteObject(oldToolBarBitmap
);
637 #endif // TB_REPLACEBITMAP
639 // we can't replace the old bitmap, so we will add another one
640 // (awfully inefficient, but what else to do?) and shift the bitmap
641 // indices accordingly
644 bitmapId
= m_nButtons
;
648 if ( addBitmap
) // no old bitmap or we can't replace it
650 TBADDBITMAP addBitmap
;
652 addBitmap
.nID
= (UINT
) hBitmap
;
653 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
654 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
656 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
661 // don't call SetToolBitmapSize() as we don't want to change the values of
662 // m_defaultWidth/Height
663 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
664 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
666 wxLogLastError(_T("TB_SETBITMAPSIZE"));
669 // Next add the buttons and separators
670 // -----------------------------------
672 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
674 // this array will hold the indices of all controls in the toolbar
675 wxArrayInt controlIds
;
677 bool lastWasRadio
= false;
679 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
681 wxToolBarToolBase
*tool
= node
->GetData();
683 // don't add separators to the vertical toolbar with old comctl32.dll
684 // versions as they didn't handle this properly
685 if ( isVertical
&& tool
->IsSeparator() &&
686 wxTheApp
->GetComCtl32Version() <= 472 )
692 TBBUTTON
& button
= buttons
[i
];
694 wxZeroMemory(button
);
696 bool isRadio
= false;
697 switch ( tool
->GetStyle() )
699 case wxTOOL_STYLE_CONTROL
:
700 button
.idCommand
= tool
->GetId();
701 // fall through: create just a separator too
703 case wxTOOL_STYLE_SEPARATOR
:
704 button
.fsState
= TBSTATE_ENABLED
;
705 button
.fsStyle
= TBSTYLE_SEP
;
708 case wxTOOL_STYLE_BUTTON
:
709 if ( !HasFlag(wxTB_NOICONS
) )
710 button
.iBitmap
= bitmapId
;
712 if ( HasFlag(wxTB_TEXT
) )
714 const wxString
& label
= tool
->GetLabel();
715 if ( !label
.empty() )
717 button
.iString
= (int)label
.c_str();
721 button
.idCommand
= tool
->GetId();
723 if ( tool
->IsEnabled() )
724 button
.fsState
|= TBSTATE_ENABLED
;
725 if ( tool
->IsToggled() )
726 button
.fsState
|= TBSTATE_CHECKED
;
728 switch ( tool
->GetKind() )
731 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
735 // the first item in the radio group is checked by
736 // default to be consistent with wxGTK and the menu
738 button
.fsState
|= TBSTATE_CHECKED
;
747 button
.fsStyle
= TBSTYLE_CHECK
;
751 wxFAIL_MSG( _T("unexpected toolbar button kind") );
755 button
.fsStyle
= TBSTYLE_BUTTON
;
762 lastWasRadio
= isRadio
;
767 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
769 wxLogLastError(wxT("TB_ADDBUTTONS"));
774 // Deal with the controls finally
775 // ------------------------------
777 // adjust the controls size to fit nicely in the toolbar
780 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
782 wxToolBarToolBase
*tool
= node
->GetData();
784 // we calculate the running y coord for vertical toolbars so we need to
785 // get the items size for all items but for the horizontal ones we
786 // don't need to deal with the non controls
787 bool isControl
= tool
->IsControl();
788 if ( !isControl
&& !isVertical
)
791 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
792 // latter only appeared in v4.70 of comctl32.dll
794 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
795 index
, (LPARAM
)(LPRECT
)&r
) )
797 wxLogLastError(wxT("TB_GETITEMRECT"));
802 // can only be control if isVertical
803 y
+= r
.bottom
- r
.top
;
808 wxControl
*control
= tool
->GetControl();
810 wxSize size
= control
->GetSize();
812 // the position of the leftmost controls corner
813 int left
= wxDefaultCoord
;
815 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
816 #ifdef TB_SETBUTTONINFO
817 // available in headers, now check whether it is available now
819 if ( wxTheApp
->GetComCtl32Version() >= 471 )
821 // set the (underlying) separators width to be that of the
824 tbbi
.cbSize
= sizeof(tbbi
);
825 tbbi
.dwMask
= TBIF_SIZE
;
826 tbbi
.cx
= (WORD
)size
.x
;
827 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
828 tool
->GetId(), (LPARAM
)&tbbi
) )
830 // the id is probably invalid?
831 wxLogLastError(wxT("TB_SETBUTTONINFO"));
835 #endif // comctl32.dll 4.71
836 // TB_SETBUTTONINFO unavailable
838 // try adding several separators to fit the controls width
839 int widthSep
= r
.right
- r
.left
;
845 tbb
.fsState
= TBSTATE_ENABLED
;
846 tbb
.fsStyle
= TBSTYLE_SEP
;
848 size_t nSeparators
= size
.x
/ widthSep
;
849 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
851 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
852 index
, (LPARAM
)&tbb
) )
854 wxLogLastError(wxT("TB_INSERTBUTTON"));
860 // remember the number of separators we used - we'd have to
861 // delete all of them later
862 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
864 // adjust the controls width to exactly cover the separators
865 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
868 // position the control itself correctly vertically
869 int height
= r
.bottom
- r
.top
;
870 int diff
= height
- size
.y
;
873 // the control is too high, resize to fit
874 control
->SetSize(wxDefaultCoord
, height
- 2);
885 y
+= height
+ 2*GetMargins().y
;
887 else // horizontal toolbar
889 if ( left
== wxDefaultCoord
)
895 control
->Move(left
, top
+ (diff
+ 1) / 2);
898 // the max index is the "real" number of buttons - i.e. counting even the
899 // separators which we added just for aligning the controls
904 if ( m_maxRows
== 0 )
906 // if not set yet, only one row
910 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
912 if ( m_maxRows
== 0 )
914 // if not set yet, have one column
919 InvalidateBestSize();
923 // ----------------------------------------------------------------------------
925 // ----------------------------------------------------------------------------
927 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
929 wxToolBarToolBase
*tool
= FindById((int)id
);
933 bool toggled
= false; // just to suppress warnings
935 if ( tool
->CanBeToggled() )
937 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
938 toggled
= (state
& TBSTATE_CHECKED
) != 0;
940 // ignore the event when a radio button is released, as this doesn't seem to
941 // happen at all, and is handled otherwise
942 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
945 tool
->Toggle(toggled
);
946 UnToggleRadioGroup(tool
);
949 // OnLeftClick() can veto the button state change - for buttons which
950 // may be toggled only, of couse
951 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
954 tool
->Toggle(!toggled
);
956 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
962 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
964 WXLPARAM
*WXUNUSED(result
))
967 // First check if this applies to us
968 NMHDR
*hdr
= (NMHDR
*)lParam
;
970 // the tooltips control created by the toolbar is sometimes Unicode, even
971 // in an ANSI application - this seems to be a bug in comctl32.dll v5
972 UINT code
= hdr
->code
;
973 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
976 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
977 if ( toolTipWnd
!= hdr
->hwndFrom
)
980 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
981 int id
= (int)ttText
->hdr
.idFrom
;
983 wxToolBarToolBase
*tool
= FindById(id
);
987 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
993 // ----------------------------------------------------------------------------
995 // ----------------------------------------------------------------------------
997 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
999 wxToolBarBase::SetToolBitmapSize(size
);
1001 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1004 void wxToolBar::SetRows(int nRows
)
1006 if ( nRows
== m_maxRows
)
1008 // avoid resizing the frame uselessly
1012 // TRUE in wParam means to create at least as many rows, FALSE -
1015 ::SendMessage(GetHwnd(), TB_SETROWS
,
1016 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1024 // The button size is bigger than the bitmap size
1025 wxSize
wxToolBar::GetToolSize() const
1027 // TB_GETBUTTONSIZE is supported from version 4.70
1028 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1029 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1030 && !defined (__DIGITALMARS__)
1031 if ( wxTheApp
->GetComCtl32Version() >= 470 )
1033 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1035 return wxSize(LOWORD(dw
), HIWORD(dw
));
1038 #endif // comctl32.dll 4.70+
1041 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1046 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1049 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1051 for ( ; current
!= 0; current
= current
->GetNext() )
1054 return current
->GetData();
1056 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1057 size_t separators
= tool
->GetSeparatorsCount();
1059 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1060 // otherwise, skip as many items as the separator count, plus the
1062 index
-= separators
? separators
+ 1 : 1;
1068 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1073 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1074 // MBN: when the point ( x, y ) is close to the toolbar border
1075 // TB_HITTEST returns m_nButtons ( not -1 )
1076 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1078 // it's a separator or there is no tool at all there
1079 return (wxToolBarToolBase
*)NULL
;
1082 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1083 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1084 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1086 return m_tools
.Item((size_t)index
)->GetData();
1091 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1095 void wxToolBar::UpdateSize()
1097 // the toolbar size changed
1098 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1100 // we must also refresh the frame after the toolbar size (possibly) changed
1101 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1104 frame
->SendSizeEvent();
1108 // ----------------------------------------------------------------------------
1110 // ---------------------------------------------------------------------------
1112 void wxToolBar::SetWindowStyleFlag(long style
)
1114 // the style bits whose changes force us to recreate the toolbar
1115 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1117 const long styleOld
= GetWindowStyle();
1119 wxToolBarBase::SetWindowStyleFlag(style
);
1121 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1122 // also fatal as we'd then try to recreate the toolbar when it's just being
1124 if ( GetToolsCount() &&
1125 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1127 // to remove the text labels, simply re-realizing the toolbar is enough
1128 // but I don't know of any way to add the text to an existing toolbar
1129 // other than by recreating it entirely
1134 // ----------------------------------------------------------------------------
1136 // ----------------------------------------------------------------------------
1138 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1140 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1141 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1144 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1146 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1147 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1150 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1152 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1153 // without, so we really need to delete the button and recreate it here
1154 wxFAIL_MSG( _T("not implemented") );
1157 // ----------------------------------------------------------------------------
1159 // ----------------------------------------------------------------------------
1161 // Responds to colour changes, and passes event on to children.
1162 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1164 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1166 // Remap the buttons
1169 // Relayout the toolbar
1170 int nrows
= m_maxRows
;
1171 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1176 // let the event propagate further
1180 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1182 if (event
.Leaving() && m_pInTool
)
1189 if (event
.RightDown())
1191 // For now, we don't have an id. Later we could
1192 // try finding the tool.
1193 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1201 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1203 // calculate our minor dimension ourselves - we're confusing the standard
1204 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1206 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1210 if ( GetWindowStyle() & wxTB_VERTICAL
)
1212 w
= r
.right
- r
.left
;
1215 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1222 if (HasFlag( wxTB_FLAT
))
1223 h
= r
.bottom
- r
.top
- 3;
1225 h
= r
.bottom
- r
.top
;
1228 // FIXME: hardcoded separator line height...
1229 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1234 if ( MAKELPARAM(w
, h
) != lParam
)
1236 // size really changed
1240 // message processed
1247 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1249 // erase any dummy separators which we used for aligning the controls if
1252 // first of all, do we have any controls at all?
1253 wxToolBarToolsList::compatibility_iterator node
;
1254 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1256 if ( node
->GetData()->IsControl() )
1262 // no controls, nothing to erase
1266 // prepare the DC on which we'll be drawing
1267 wxClientDC
dc(this);
1268 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1269 dc
.SetPen(*wxTRANSPARENT_PEN
);
1272 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1274 // nothing to redraw anyhow
1279 wxCopyRECTToRect(r
, rectUpdate
);
1281 dc
.SetClippingRegion(rectUpdate
);
1283 // draw the toolbar tools, separators &c normally
1284 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1286 // for each control in the toolbar find all the separators intersecting it
1289 // NB: this is really the only way to do it as we don't know if a separator
1290 // corresponds to a control (i.e. is a dummy one) or a real one
1292 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1294 wxToolBarToolBase
*tool
= node
->GetData();
1295 if ( tool
->IsControl() )
1297 // get the control rect in our client coords
1298 wxControl
*control
= tool
->GetControl();
1299 wxRect rectCtrl
= control
->GetRect();
1301 // iterate over all buttons
1303 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1304 for ( int n
= 0; n
< count
; n
++ )
1306 // is it a separator?
1307 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1310 wxLogDebug(_T("TB_GETBUTTON failed?"));
1315 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1318 // get the bounding rect of the separator
1320 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1323 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1328 // does it intersect the control?
1330 wxCopyRECTToRect(r
, rectItem
);
1331 if ( rectCtrl
.Intersects(rectItem
) )
1333 // yes, do erase it!
1334 dc
.DrawRectangle(rectItem
);
1336 // Necessary in case we use a no-paint-on-size
1337 // style in the parent: the controls can disappear
1338 control
->Refresh(false);
1347 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1349 wxCoord x
= GET_X_LPARAM(lParam
),
1350 y
= GET_Y_LPARAM(lParam
);
1351 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1353 // cursor left current tool
1354 if( tool
!= m_pInTool
&& !tool
)
1360 // cursor entered a tool
1361 if( tool
!= m_pInTool
&& tool
)
1364 OnMouseEnter( tool
->GetId() );
1368 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1373 if ( HandleSize(wParam
, lParam
) )
1378 // we don't handle mouse moves, so always pass the message to
1379 // wxControl::MSWWindowProc
1380 HandleMouseMove(wParam
, lParam
);
1384 if ( HandlePaint(wParam
, lParam
) )
1388 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1391 // ----------------------------------------------------------------------------
1392 // private functions
1393 // ----------------------------------------------------------------------------
1395 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1401 wxLogLastError(_T("CreateCompatibleDC"));
1406 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1410 wxLogLastError(_T("SelectObject"));
1415 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1417 for ( int i
= 0; i
< width
; i
++ )
1419 for ( int j
= 0; j
< height
; j
++ )
1421 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1423 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1425 COLORREF col
= cmap
[k
].from
;
1426 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1427 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1428 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1430 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1439 // VZ: I leave here my attempts to map the bitmap to the system colours
1440 // faster by using BitBlt() even though it's broken currently - but
1441 // maybe someone else can finish it? It should be faster than iterating
1442 // over all pixels...
1444 MemoryHDC hdcMask
, hdcDst
;
1445 if ( !hdcMask
|| !hdcDst
)
1447 wxLogLastError(_T("CreateCompatibleDC"));
1452 // create the target bitmap
1453 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1456 wxLogLastError(_T("CreateCompatibleBitmap"));
1461 // create the monochrome mask bitmap
1462 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1465 wxLogLastError(_T("CreateBitmap(mono)"));
1467 ::DeleteObject(hbmpDst
);
1472 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1473 bmpInMask(hdcMask
, hbmpMask
);
1476 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1478 // create the mask for this colour
1479 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1480 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1482 // replace this colour with the target one in the dst bitmap
1483 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1484 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1486 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1489 MAKEROP4(PATCOPY
, SRCCOPY
));
1491 (void)::SelectObject(hdcDst
, hbrOld
);
1492 ::DeleteObject(hbr
);
1495 ::DeleteObject((HBITMAP
)bitmap
);
1497 return (WXHBITMAP
)hbmpDst
;
1501 #endif // wxUSE_TOOLBAR && Win95