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"
48 #include "wx/msw/private.h"
51 #include "wx/msw/uxtheme.h"
54 // include <commctrl.h> "properly"
55 #include "wx/msw/wrapcctl.h"
57 #include "wx/app.h" // for GetComCtl32Version
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // these standard constants are not always defined in compilers headers
67 #define TBSTYLE_LIST 0x1000
68 #define TBSTYLE_FLAT 0x0800
71 #ifndef TBSTYLE_TRANSPARENT
72 #define TBSTYLE_TRANSPARENT 0x8000
75 #ifndef TBSTYLE_TOOLTIPS
76 #define TBSTYLE_TOOLTIPS 0x0100
81 #define TB_SETSTYLE (WM_USER + 56)
82 #define TB_GETSTYLE (WM_USER + 57)
86 #define TB_HITTEST (WM_USER + 69)
90 #define TB_GETMAXSIZE (WM_USER + 83)
93 // these values correspond to those used by comctl32.dll
94 #define DEFAULTBITMAPX 16
95 #define DEFAULTBITMAPY 15
96 #define DEFAULTBUTTONX 24
97 #define DEFAULTBUTTONY 24
98 #define DEFAULTBARHEIGHT 27
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
116 style ( wxNO_BORDER | wxTB_HORIZONTAL)
125 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
126 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
127 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
128 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class wxToolBarTool
: public wxToolBarToolBase
138 wxToolBarTool(wxToolBar
*tbar
,
140 const wxString
& label
,
141 const wxBitmap
& bmpNormal
,
142 const wxBitmap
& bmpDisabled
,
144 wxObject
*clientData
,
145 const wxString
& shortHelp
,
146 const wxString
& longHelp
)
147 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
148 clientData
, shortHelp
, longHelp
)
153 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
154 : wxToolBarToolBase(tbar
, control
)
159 virtual void SetLabel(const wxString
& label
)
161 if ( label
== m_label
)
164 wxToolBarToolBase::SetLabel(label
);
166 // we need to update the label shown in the toolbar because it has a
167 // pointer to the internal buffer of the old label
169 // TODO: use TB_SETBUTTONINFO
172 // set/get the number of separators which we use to cover the space used by
173 // a control in the toolbar
174 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
175 size_t GetSeparatorsCount() const { return m_nSepCount
; }
180 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
184 // ============================================================================
186 // ============================================================================
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
193 const wxString
& label
,
194 const wxBitmap
& bmpNormal
,
195 const wxBitmap
& bmpDisabled
,
197 wxObject
*clientData
,
198 const wxString
& shortHelp
,
199 const wxString
& longHelp
)
201 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
202 clientData
, shortHelp
, longHelp
);
205 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
207 return new wxToolBarTool(this, control
);
210 // ----------------------------------------------------------------------------
211 // wxToolBar construction
212 // ----------------------------------------------------------------------------
214 void wxToolBar::Init()
217 m_disabledImgList
= NULL
;
221 m_defaultWidth
= DEFAULTBITMAPX
;
222 m_defaultHeight
= DEFAULTBITMAPY
;
227 bool wxToolBar::Create(wxWindow
*parent
,
232 const wxString
& name
)
234 // common initialisation
235 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
238 // MSW-specific initialisation
239 if ( !MSWCreateToolbar(pos
, size
) )
242 wxSetCCUnicodeFormat(GetHwnd());
244 // set up the colors and fonts
245 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
246 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
248 // workaround for flat toolbar on Windows XP classic style
250 if ( style
& wxTB_FLAT
)
252 // Testing for an active theme appears to be unnecessary (see comments in patch 1204217).
253 // Disabling the test brings back separator lines.
254 // However, the separators can look ugly and distracting, especially between controls,
255 // so I'm restoring the test and removing the separators again - JACS
257 wxUxThemeEngine
*p
= wxUxThemeEngine::Get();
258 if ( !p
|| !p
->IsThemeActive() )
261 DWORD dwToolbarStyle
;
263 dwToolbarStyle
= (DWORD
)::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L );
265 if ((dwToolbarStyle
& TBSTYLE_FLAT
) == 0)
267 dwToolbarStyle
|= TBSTYLE_FLAT
;
268 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, (LPARAM
)dwToolbarStyle
);
277 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
279 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
282 // toolbar-specific post initialisation
283 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
288 void wxToolBar::Recreate()
290 const HWND hwndOld
= GetHwnd();
293 // we haven't been created yet, no need to recreate
297 // get the position and size before unsubclassing the old toolbar
298 const wxPoint pos
= GetPosition();
299 const wxSize size
= GetSize();
303 if ( !MSWCreateToolbar(pos
, size
) )
306 wxFAIL_MSG( _T("recreating the toolbar failed") );
311 // reparent all our children under the new toolbar
312 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
314 node
= node
->GetNext() )
316 wxWindow
*win
= node
->GetData();
317 if ( !win
->IsTopLevel() )
318 ::SetParent(GetHwndOf(win
), GetHwnd());
321 // only destroy the old toolbar now -- after all the children had been
323 ::DestroyWindow(hwndOld
);
325 // it is for the old bitmap control and can't be used with the new one
328 ::DeleteObject((HBITMAP
) m_hBitmap
);
332 if ( m_disabledImgList
)
334 delete m_disabledImgList
;
335 m_disabledImgList
= NULL
;
342 wxToolBar::~wxToolBar()
344 // we must refresh the frame size when the toolbar is deleted but the frame
345 // is not - otherwise toolbar leaves a hole in the place it used to occupy
346 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
347 if ( frame
&& !frame
->IsBeingDeleted() )
349 frame
->SendSizeEvent();
354 ::DeleteObject((HBITMAP
) m_hBitmap
);
357 delete m_disabledImgList
;
360 wxSize
wxToolBar::DoGetBestSize() const
365 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
367 // maybe an old (< 0x400) Windows version? try to approximate the
368 // toolbar size ourselves
369 sizeBest
= GetToolSize();
370 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
371 sizeBest
.x
*= GetToolsCount();
373 // reverse horz and vertical components if necessary
374 if ( HasFlag(wxTB_VERTICAL
) )
377 sizeBest
.x
= sizeBest
.y
;
383 sizeBest
.x
= size
.cx
;
384 sizeBest
.y
= size
.cy
;
387 CacheBestSize(sizeBest
);
391 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
393 // toolbars never have border, giving one to them results in broken
395 WXDWORD msStyle
= wxControl::MSWGetStyle
397 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
400 // always include this one, it never hurts and setting it later only if we
401 // do have tooltips wouldn't work
402 msStyle
|= TBSTYLE_TOOLTIPS
;
404 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
406 // static as it doesn't change during the program lifetime
407 static int s_verComCtl
= wxApp::GetComCtl32Version();
409 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
410 // style with 6.00 (part of Windows XP) leads to the toolbar with
411 // incorrect background colour - and not using it still results in the
412 // correct (flat) toolbar, so don't use it there
413 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
415 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
418 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
420 msStyle
|= TBSTYLE_LIST
;
424 if ( style
& wxTB_NODIVIDER
)
425 msStyle
|= CCS_NODIVIDER
;
427 if ( style
& wxTB_NOALIGN
)
428 msStyle
|= CCS_NOPARENTALIGN
;
430 if ( style
& wxTB_VERTICAL
)
436 // ----------------------------------------------------------------------------
437 // adding/removing tools
438 // ----------------------------------------------------------------------------
440 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
442 // nothing special to do here - we really create the toolbar buttons in
446 InvalidateBestSize();
450 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
452 // the main difficulty we have here is with the controls in the toolbars:
453 // as we (sometimes) use several separators to cover up the space used by
454 // them, the indices are not the same for us and the toolbar
456 // first determine the position of the first button to delete: it may be
457 // different from pos if we use several separators to cover the space used
459 wxToolBarToolsList::compatibility_iterator node
;
460 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
462 wxToolBarToolBase
*tool2
= node
->GetData();
465 // let node point to the next node in the list
466 node
= node
->GetNext();
471 if ( tool2
->IsControl() )
473 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
477 // now determine the number of buttons to delete and the area taken by them
478 size_t nButtonsToDelete
= 1;
480 // get the size of the button we're going to delete
482 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
484 wxLogLastError(_T("TB_GETITEMRECT"));
487 int width
= r
.right
- r
.left
;
489 if ( tool
->IsControl() )
491 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
493 width
*= nButtonsToDelete
;
496 // do delete all buttons
497 m_nButtons
-= nButtonsToDelete
;
498 while ( nButtonsToDelete
-- > 0 )
500 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
502 wxLogLastError(wxT("TB_DELETEBUTTON"));
510 // and finally reposition all the controls after this button (the toolbar
511 // takes care of all normal items)
512 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
514 wxToolBarToolBase
*tool2
= node
->GetData();
515 if ( tool2
->IsControl() )
518 wxControl
*control
= tool2
->GetControl();
519 control
->GetPosition(&x
, NULL
);
520 control
->Move(x
- width
, wxDefaultCoord
);
524 InvalidateBestSize();
528 void wxToolBar::CreateDisabledImageList()
530 // as we can't use disabled image list with older versions of comctl32.dll,
531 // don't even bother creating it
532 if ( wxTheApp
->GetComCtl32Version() >= 470 )
534 // search for the first disabled button img in the toolbar, if any
535 for ( wxToolBarToolsList::compatibility_iterator
536 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
538 wxToolBarToolBase
*tool
= node
->GetData();
539 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
540 if ( bmpDisabled
.Ok() )
542 m_disabledImgList
= new wxImageList
546 bmpDisabled
.GetMask() != NULL
,
553 // we don't have any disabled bitmaps
556 m_disabledImgList
= NULL
;
559 bool wxToolBar::Realize()
561 const size_t nTools
= GetToolsCount();
568 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
570 bool doRemap
, doRemapBg
, doTransparent
;
574 doTransparent
= false;
576 if (wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 2)
578 doRemapBg
= doRemap
= false;
579 doTransparent
= true;
582 { doRemap
= !wxSystemOptions::HasOption(wxT("msw.remap"))
583 || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1;
584 doRemapBg
= !doRemap
;
585 doTransparent
= false;
589 // delete all old buttons, if any
590 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
592 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
594 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
598 // First, add the bitmap: we use one bitmap for all toolbar buttons
599 // ----------------------------------------------------------------
601 wxToolBarToolsList::compatibility_iterator node
;
605 if ( HasFlag(wxTB_NOICONS
) )
607 // no icons, don't leave space for them
611 else // do show icons
613 // if we already have a bitmap, we'll replace the existing one --
614 // otherwise we'll install a new one
615 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
617 sizeBmp
.x
= m_defaultWidth
;
618 sizeBmp
.y
= m_defaultHeight
;
620 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
621 totalBitmapHeight
= m_defaultHeight
;
623 // Create a bitmap and copy all the tool bitmaps to it
624 wxMemoryDC dcAllButtons
;
625 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
626 dcAllButtons
.SelectObject(bitmap
);
628 dcAllButtons
.SetBackground(wxBrush(wxColour(192,192,192)));
631 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
633 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
635 dcAllButtons
.Clear();
637 m_hBitmap
= bitmap
.GetHBITMAP();
638 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
643 dcAllButtons
.SelectObject(wxNullBitmap
);
645 // Even if we're not remapping the bitmap
646 // content, we still have to remap the background.
647 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
648 totalBitmapWidth
, totalBitmapHeight
);
650 dcAllButtons
.SelectObject(bitmap
);
654 #endif // !__WXWINCE__
656 // the button position
659 // the number of buttons (not separators)
662 CreateDisabledImageList();
663 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
665 wxToolBarToolBase
*tool
= node
->GetData();
666 if ( tool
->IsButton() )
668 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
670 const int w
= bmp
.GetWidth();
671 const int h
= bmp
.GetHeight();
675 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
676 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
678 // notice the last parameter: do use mask
679 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
683 wxFAIL_MSG( _T("invalid tool button bitmap") );
686 // also deal with disabled bitmap if we want to use them
687 if ( m_disabledImgList
)
689 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
691 if ( !bmpDisabled
.Ok() )
693 // no disabled bitmap specified but we still need to
694 // fill the space in the image list with something, so
695 // we grey out the normal bitmap
697 wxCreateGreyedImage(bmp
.ConvertToImage(), imgGreyed
);
699 // we need to have light grey background colour for
700 // MapBitmap() to work correctly
701 for ( int y
= 0; y
< h
; y
++ )
703 for ( int x
= 0; x
< w
; x
++ )
705 if ( imgGreyed
.IsTransparent(x
, y
) )
706 imgGreyed
.SetRGB(x
, y
,
708 wxLIGHT_GREY
->Green(),
709 wxLIGHT_GREY
->Blue());
713 bmpDisabled
= wxBitmap(imgGreyed
);
715 #endif // wxUSE_IMAGE
717 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
719 m_disabledImgList
->Add(bmpDisabled
);
722 // still inc width and number of buttons because otherwise the
723 // subsequent buttons will all be shifted which is rather confusing
724 // (and like this you'd see immediately which bitmap was bad)
730 dcAllButtons
.SelectObject(wxNullBitmap
);
732 // don't delete this HBITMAP!
733 bitmap
.SetHBITMAP(0);
737 // Map to system colours
738 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
739 totalBitmapWidth
, totalBitmapHeight
);
744 bool addBitmap
= true;
746 if ( oldToolBarBitmap
)
748 #ifdef TB_REPLACEBITMAP
749 if ( wxApp::GetComCtl32Version() >= 400 )
751 TBREPLACEBITMAP replaceBitmap
;
752 replaceBitmap
.hInstOld
= NULL
;
753 replaceBitmap
.hInstNew
= NULL
;
754 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
755 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
756 replaceBitmap
.nButtons
= nButtons
;
757 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
758 0, (LPARAM
) &replaceBitmap
) )
760 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
763 ::DeleteObject(oldToolBarBitmap
);
769 #endif // TB_REPLACEBITMAP
771 // we can't replace the old bitmap, so we will add another one
772 // (awfully inefficient, but what else to do?) and shift the bitmap
773 // indices accordingly
776 bitmapId
= m_nButtons
;
780 if ( addBitmap
) // no old bitmap or we can't replace it
782 TBADDBITMAP addBitmap
;
784 addBitmap
.nID
= (UINT
) hBitmap
;
785 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
786 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
788 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
792 if ( m_disabledImgList
)
794 HIMAGELIST oldImageList
= (HIMAGELIST
)
795 ::SendMessage(GetHwnd(),
796 TB_SETDISABLEDIMAGELIST
,
798 (LPARAM
)GetHimagelistOf(m_disabledImgList
));
800 // delete previous image list if any
802 ::DeleteObject( oldImageList
);
806 // don't call SetToolBitmapSize() as we don't want to change the values of
807 // m_defaultWidth/Height
808 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
809 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
811 wxLogLastError(_T("TB_SETBITMAPSIZE"));
814 // Next add the buttons and separators
815 // -----------------------------------
817 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
819 // this array will hold the indices of all controls in the toolbar
820 wxArrayInt controlIds
;
822 bool lastWasRadio
= false;
824 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
826 wxToolBarToolBase
*tool
= node
->GetData();
828 // don't add separators to the vertical toolbar with old comctl32.dll
829 // versions as they didn't handle this properly
830 if ( isVertical
&& tool
->IsSeparator() &&
831 wxApp::GetComCtl32Version() <= 472 )
837 TBBUTTON
& button
= buttons
[i
];
839 wxZeroMemory(button
);
841 bool isRadio
= false;
842 switch ( tool
->GetStyle() )
844 case wxTOOL_STYLE_CONTROL
:
845 button
.idCommand
= tool
->GetId();
846 // fall through: create just a separator too
848 case wxTOOL_STYLE_SEPARATOR
:
849 button
.fsState
= TBSTATE_ENABLED
;
850 button
.fsStyle
= TBSTYLE_SEP
;
853 case wxTOOL_STYLE_BUTTON
:
854 if ( !HasFlag(wxTB_NOICONS
) )
855 button
.iBitmap
= bitmapId
;
857 if ( HasFlag(wxTB_TEXT
) )
859 const wxString
& label
= tool
->GetLabel();
860 if ( !label
.empty() )
862 button
.iString
= (int)label
.c_str();
866 button
.idCommand
= tool
->GetId();
868 if ( tool
->IsEnabled() )
869 button
.fsState
|= TBSTATE_ENABLED
;
870 if ( tool
->IsToggled() )
871 button
.fsState
|= TBSTATE_CHECKED
;
873 switch ( tool
->GetKind() )
876 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
880 // the first item in the radio group is checked by
881 // default to be consistent with wxGTK and the menu
883 button
.fsState
|= TBSTATE_CHECKED
;
885 if (tool
->Toggle(true))
887 DoToggleTool(tool
, true);
890 else if (tool
->IsToggled())
892 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
893 int prevIndex
= i
- 1;
896 TBBUTTON
& prevButton
= buttons
[prevIndex
];
897 wxToolBarToolBase
*tool
= nodePrev
->GetData();
898 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
901 if ( tool
->Toggle(false) )
903 DoToggleTool(tool
, false);
905 prevButton
.fsState
= TBSTATE_ENABLED
;
906 nodePrev
= nodePrev
->GetPrevious();
915 button
.fsStyle
= TBSTYLE_CHECK
;
919 wxFAIL_MSG( _T("unexpected toolbar button kind") );
923 button
.fsStyle
= TBSTYLE_BUTTON
;
930 lastWasRadio
= isRadio
;
935 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
937 wxLogLastError(wxT("TB_ADDBUTTONS"));
942 // Deal with the controls finally
943 // ------------------------------
945 // adjust the controls size to fit nicely in the toolbar
948 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
950 wxToolBarToolBase
*tool
= node
->GetData();
952 // we calculate the running y coord for vertical toolbars so we need to
953 // get the items size for all items but for the horizontal ones we
954 // don't need to deal with the non controls
955 bool isControl
= tool
->IsControl();
956 if ( !isControl
&& !isVertical
)
959 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
960 // latter only appeared in v4.70 of comctl32.dll
962 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
963 index
, (LPARAM
)(LPRECT
)&r
) )
965 wxLogLastError(wxT("TB_GETITEMRECT"));
970 // can only be control if isVertical
971 y
+= r
.bottom
- r
.top
;
976 wxControl
*control
= tool
->GetControl();
978 wxSize size
= control
->GetSize();
980 // the position of the leftmost controls corner
981 int left
= wxDefaultCoord
;
983 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
984 #ifdef TB_SETBUTTONINFO
985 // available in headers, now check whether it is available now
987 if ( wxApp::GetComCtl32Version() >= 471 )
989 // set the (underlying) separators width to be that of the
992 tbbi
.cbSize
= sizeof(tbbi
);
993 tbbi
.dwMask
= TBIF_SIZE
;
994 tbbi
.cx
= (WORD
)size
.x
;
995 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
996 tool
->GetId(), (LPARAM
)&tbbi
) )
998 // the id is probably invalid?
999 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1003 #endif // comctl32.dll 4.71
1004 // TB_SETBUTTONINFO unavailable
1006 // try adding several separators to fit the controls width
1007 int widthSep
= r
.right
- r
.left
;
1013 tbb
.fsState
= TBSTATE_ENABLED
;
1014 tbb
.fsStyle
= TBSTYLE_SEP
;
1016 size_t nSeparators
= size
.x
/ widthSep
;
1017 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1019 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1020 index
, (LPARAM
)&tbb
) )
1022 wxLogLastError(wxT("TB_INSERTBUTTON"));
1028 // remember the number of separators we used - we'd have to
1029 // delete all of them later
1030 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1032 // adjust the controls width to exactly cover the separators
1033 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
1036 // position the control itself correctly vertically
1037 int height
= r
.bottom
- r
.top
;
1038 int diff
= height
- size
.y
;
1041 // the control is too high, resize to fit
1042 control
->SetSize(wxDefaultCoord
, height
- 2);
1053 y
+= height
+ 2*GetMargins().y
;
1055 else // horizontal toolbar
1057 if ( left
== wxDefaultCoord
)
1063 control
->Move(left
, top
+ (diff
+ 1) / 2);
1066 // the max index is the "real" number of buttons - i.e. counting even the
1067 // separators which we added just for aligning the controls
1072 if ( m_maxRows
== 0 )
1074 // if not set yet, only one row
1078 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1080 if ( m_maxRows
== 0 )
1082 // if not set yet, have one column
1083 SetRows(m_nButtons
);
1087 InvalidateBestSize();
1091 // ----------------------------------------------------------------------------
1093 // ----------------------------------------------------------------------------
1095 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
1097 wxToolBarToolBase
*tool
= FindById((int)id
);
1101 bool toggled
= false; // just to suppress warnings
1103 if ( tool
->CanBeToggled() )
1105 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1106 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1108 // ignore the event when a radio button is released, as this doesn't
1109 // seem to happen at all, and is handled otherwise
1110 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1113 tool
->Toggle(toggled
);
1114 UnToggleRadioGroup(tool
);
1117 // OnLeftClick() can veto the button state change - for buttons which
1118 // may be toggled only, of couse
1119 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1122 tool
->Toggle(!toggled
);
1124 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1130 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1132 WXLPARAM
*WXUNUSED(result
))
1135 // First check if this applies to us
1136 NMHDR
*hdr
= (NMHDR
*)lParam
;
1138 // the tooltips control created by the toolbar is sometimes Unicode, even
1139 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1140 UINT code
= hdr
->code
;
1141 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1144 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1145 if ( toolTipWnd
!= hdr
->hwndFrom
)
1148 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1149 int id
= (int)ttText
->hdr
.idFrom
;
1151 wxToolBarToolBase
*tool
= FindById(id
);
1155 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1157 wxUnusedVar(lParam
);
1163 // ----------------------------------------------------------------------------
1165 // ----------------------------------------------------------------------------
1167 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1169 wxToolBarBase::SetToolBitmapSize(size
);
1171 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1174 void wxToolBar::SetRows(int nRows
)
1176 if ( nRows
== m_maxRows
)
1178 // avoid resizing the frame uselessly
1182 // TRUE in wParam means to create at least as many rows, FALSE -
1185 ::SendMessage(GetHwnd(), TB_SETROWS
,
1186 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1194 // The button size is bigger than the bitmap size
1195 wxSize
wxToolBar::GetToolSize() const
1197 // TB_GETBUTTONSIZE is supported from version 4.70
1198 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1199 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1200 && !defined (__DIGITALMARS__)
1201 if ( wxApp::GetComCtl32Version() >= 470 )
1203 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1205 return wxSize(LOWORD(dw
), HIWORD(dw
));
1208 #endif // comctl32.dll 4.70+
1211 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1216 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1219 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1221 for ( ; current
; current
= current
->GetNext() )
1224 return current
->GetData();
1226 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1227 size_t separators
= tool
->GetSeparatorsCount();
1229 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1230 // otherwise, skip as many items as the separator count, plus the
1232 index
-= separators
? separators
+ 1 : 1;
1238 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1243 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1244 // MBN: when the point ( x, y ) is close to the toolbar border
1245 // TB_HITTEST returns m_nButtons ( not -1 )
1246 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1248 // it's a separator or there is no tool at all there
1249 return (wxToolBarToolBase
*)NULL
;
1252 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1253 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1254 if ( wxApp::GetComCtl32Version() >= 471 )
1256 return m_tools
.Item((size_t)index
)->GetData();
1261 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1265 void wxToolBar::UpdateSize()
1267 // the toolbar size changed
1268 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1270 // we must also refresh the frame after the toolbar size (possibly) changed
1271 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1274 frame
->SendSizeEvent();
1278 // ----------------------------------------------------------------------------
1280 // ---------------------------------------------------------------------------
1282 void wxToolBar::SetWindowStyleFlag(long style
)
1284 // the style bits whose changes force us to recreate the toolbar
1285 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1287 const long styleOld
= GetWindowStyle();
1289 wxToolBarBase::SetWindowStyleFlag(style
);
1291 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1292 // also fatal as we'd then try to recreate the toolbar when it's just being
1294 if ( GetToolsCount() &&
1295 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1297 // to remove the text labels, simply re-realizing the toolbar is enough
1298 // but I don't know of any way to add the text to an existing toolbar
1299 // other than by recreating it entirely
1304 // ----------------------------------------------------------------------------
1306 // ----------------------------------------------------------------------------
1308 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1310 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1311 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1314 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1316 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1317 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1320 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1322 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1323 // without, so we really need to delete the button and recreate it here
1324 wxFAIL_MSG( _T("not implemented") );
1327 // ----------------------------------------------------------------------------
1329 // ----------------------------------------------------------------------------
1331 // Responds to colour changes, and passes event on to children.
1332 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1334 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1336 // Remap the buttons
1339 // Relayout the toolbar
1340 int nrows
= m_maxRows
;
1341 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1346 // let the event propagate further
1350 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1352 if (event
.Leaving() && m_pInTool
)
1359 if ( event
.RightDown() )
1361 // find the tool under the mouse
1363 event
.GetPosition(&x
, &y
);
1365 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1366 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1374 // This handler is required to allow the toolbar to be set to a non-default
1375 // colour: for example, when it must blend in with a notebook page.
1376 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1378 wxColour bgCol
= GetBackgroundColour();
1385 // notice that this 'dumb' implementation may cause flicker for some of the
1386 // controls in which case they should intercept wxEraseEvent and process it
1387 // themselves somehow
1390 ::GetClientRect(GetHwnd(), &rect
);
1392 HBRUSH hBrush
= ::CreateSolidBrush(wxColourToRGB(bgCol
));
1394 HDC hdc
= GetHdcOf((*event
.GetDC()));
1397 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1400 ::FillRect(hdc
, &rect
, hBrush
);
1401 ::DeleteObject(hBrush
);
1404 ::SetMapMode(hdc
, mode
);
1408 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1410 // calculate our minor dimension ourselves - we're confusing the standard
1411 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1413 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1417 if ( GetWindowStyle() & wxTB_VERTICAL
)
1419 w
= r
.right
- r
.left
;
1422 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1429 if (HasFlag( wxTB_FLAT
))
1430 h
= r
.bottom
- r
.top
- 3;
1432 h
= r
.bottom
- r
.top
;
1435 // FIXME: hardcoded separator line height...
1436 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1441 if ( MAKELPARAM(w
, h
) != lParam
)
1443 // size really changed
1447 // message processed
1454 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1456 // erase any dummy separators which we used for aligning the controls if
1459 // first of all, do we have any controls at all?
1460 wxToolBarToolsList::compatibility_iterator node
;
1461 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1463 if ( node
->GetData()->IsControl() )
1469 // no controls, nothing to erase
1473 // prepare the DC on which we'll be drawing
1474 wxClientDC
dc(this);
1475 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1476 dc
.SetPen(*wxTRANSPARENT_PEN
);
1479 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1481 // nothing to redraw anyhow
1486 wxCopyRECTToRect(r
, rectUpdate
);
1488 dc
.SetClippingRegion(rectUpdate
);
1490 // draw the toolbar tools, separators &c normally
1491 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1493 // for each control in the toolbar find all the separators intersecting it
1496 // NB: this is really the only way to do it as we don't know if a separator
1497 // corresponds to a control (i.e. is a dummy one) or a real one
1499 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1501 wxToolBarToolBase
*tool
= node
->GetData();
1502 if ( tool
->IsControl() )
1504 // get the control rect in our client coords
1505 wxControl
*control
= tool
->GetControl();
1506 wxRect rectCtrl
= control
->GetRect();
1508 // iterate over all buttons
1510 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1511 for ( int n
= 0; n
< count
; n
++ )
1513 // is it a separator?
1514 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1517 wxLogDebug(_T("TB_GETBUTTON failed?"));
1522 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1525 // get the bounding rect of the separator
1527 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1530 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1535 // does it intersect the control?
1537 wxCopyRECTToRect(r
, rectItem
);
1538 if ( rectCtrl
.Intersects(rectItem
) )
1540 // yes, do erase it!
1541 dc
.DrawRectangle(rectItem
);
1543 // Necessary in case we use a no-paint-on-size
1544 // style in the parent: the controls can disappear
1545 control
->Refresh(false);
1554 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1556 wxCoord x
= GET_X_LPARAM(lParam
),
1557 y
= GET_Y_LPARAM(lParam
);
1558 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1560 // cursor left current tool
1561 if( tool
!= m_pInTool
&& !tool
)
1567 // cursor entered a tool
1568 if( tool
!= m_pInTool
&& tool
)
1571 OnMouseEnter( tool
->GetId() );
1575 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1580 // we don't handle mouse moves, so always pass the message to
1581 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1582 HandleMouseMove(wParam
, lParam
);
1586 if ( HandleSize(wParam
, lParam
) )
1592 if ( HandlePaint(wParam
, lParam
) )
1597 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1600 // ----------------------------------------------------------------------------
1601 // private functions
1602 // ----------------------------------------------------------------------------
1604 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1610 wxLogLastError(_T("CreateCompatibleDC"));
1615 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1619 wxLogLastError(_T("SelectObject"));
1624 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1626 for ( int i
= 0; i
< width
; i
++ )
1628 for ( int j
= 0; j
< height
; j
++ )
1630 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1632 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1634 COLORREF col
= cmap
[k
].from
;
1635 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1636 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1637 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1639 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1648 // VZ: I leave here my attempts to map the bitmap to the system colours
1649 // faster by using BitBlt() even though it's broken currently - but
1650 // maybe someone else can finish it? It should be faster than iterating
1651 // over all pixels...
1653 MemoryHDC hdcMask
, hdcDst
;
1654 if ( !hdcMask
|| !hdcDst
)
1656 wxLogLastError(_T("CreateCompatibleDC"));
1661 // create the target bitmap
1662 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1665 wxLogLastError(_T("CreateCompatibleBitmap"));
1670 // create the monochrome mask bitmap
1671 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1674 wxLogLastError(_T("CreateBitmap(mono)"));
1676 ::DeleteObject(hbmpDst
);
1681 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1682 bmpInMask(hdcMask
, hbmpMask
);
1685 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1687 // create the mask for this colour
1688 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1689 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1691 // replace this colour with the target one in the dst bitmap
1692 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1693 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1695 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1698 MAKEROP4(PATCOPY
, SRCCOPY
));
1700 (void)::SelectObject(hdcDst
, hbrOld
);
1701 ::DeleteObject(hbr
);
1704 ::DeleteObject((HBITMAP
)bitmap
);
1706 return (WXHBITMAP
)hbmpDst
;
1710 #endif // wxUSE_TOOLBAR && Win95