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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/dynarray.h"
32 #include "wx/settings.h"
33 #include "wx/bitmap.h"
34 #include "wx/dcmemory.h"
35 #include "wx/control.h"
38 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
40 #include "wx/toolbar.h"
41 #include "wx/sysopt.h"
44 #include "wx/msw/private.h"
47 #include "wx/msw/uxtheme.h"
50 // include <commctrl.h> "properly"
51 #include "wx/msw/wrapcctl.h"
53 #include "wx/app.h" // for GetComCtl32Version
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // these standard constants are not always defined in compilers headers
63 #define TBSTYLE_LIST 0x1000
64 #define TBSTYLE_FLAT 0x0800
67 #ifndef TBSTYLE_TRANSPARENT
68 #define TBSTYLE_TRANSPARENT 0x8000
71 #ifndef TBSTYLE_TOOLTIPS
72 #define TBSTYLE_TOOLTIPS 0x0100
77 #define TB_SETSTYLE (WM_USER + 56)
78 #define TB_GETSTYLE (WM_USER + 57)
82 #define TB_HITTEST (WM_USER + 69)
86 #define TB_GETMAXSIZE (WM_USER + 83)
89 // these values correspond to those used by comctl32.dll
90 #define DEFAULTBITMAPX 16
91 #define DEFAULTBITMAPY 15
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
109 style ( wxNO_BORDER | wxTB_HORIZONTAL)
118 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
119 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
120 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
121 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class wxToolBarTool
: public wxToolBarToolBase
131 wxToolBarTool(wxToolBar
*tbar
,
133 const wxString
& label
,
134 const wxBitmap
& bmpNormal
,
135 const wxBitmap
& bmpDisabled
,
137 wxObject
*clientData
,
138 const wxString
& shortHelp
,
139 const wxString
& longHelp
)
140 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
141 clientData
, shortHelp
, longHelp
)
146 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
147 : wxToolBarToolBase(tbar
, control
)
152 virtual void SetLabel(const wxString
& label
)
154 if ( label
== m_label
)
157 wxToolBarToolBase::SetLabel(label
);
159 // we need to update the label shown in the toolbar because it has a
160 // pointer to the internal buffer of the old label
162 // TODO: use TB_SETBUTTONINFO
165 // set/get the number of separators which we use to cover the space used by
166 // a control in the toolbar
167 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
168 size_t GetSeparatorsCount() const { return m_nSepCount
; }
173 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
177 // ============================================================================
179 // ============================================================================
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
186 const wxString
& label
,
187 const wxBitmap
& bmpNormal
,
188 const wxBitmap
& bmpDisabled
,
190 wxObject
*clientData
,
191 const wxString
& shortHelp
,
192 const wxString
& longHelp
)
194 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
195 clientData
, shortHelp
, longHelp
);
198 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
200 return new wxToolBarTool(this, control
);
203 // ----------------------------------------------------------------------------
204 // wxToolBar construction
205 // ----------------------------------------------------------------------------
207 void wxToolBar::Init()
210 m_disabledImgList
= NULL
;
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_BTNFACE
));
239 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
241 // workaround for flat toolbar on Windows XP classic style: we have to set
242 // the style after creating the control; doing it at creation time doesn't work
244 if ( style
& wxTB_FLAT
)
246 LRESULT style
= ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
248 if ( !(style
& TBSTYLE_FLAT
) )
249 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
251 #endif // wxUSE_UXTHEME
256 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
258 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
261 // toolbar-specific post initialisation
262 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
267 void wxToolBar::Recreate()
269 const HWND hwndOld
= GetHwnd();
272 // we haven't been created yet, no need to recreate
276 // get the position and size before unsubclassing the old toolbar
277 const wxPoint pos
= GetPosition();
278 const wxSize size
= GetSize();
282 if ( !MSWCreateToolbar(pos
, size
) )
285 wxFAIL_MSG( _T("recreating the toolbar failed") );
290 // reparent all our children under the new toolbar
291 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
293 node
= node
->GetNext() )
295 wxWindow
*win
= node
->GetData();
296 if ( !win
->IsTopLevel() )
297 ::SetParent(GetHwndOf(win
), GetHwnd());
300 // only destroy the old toolbar now --
301 // after all the children had been reparented
302 ::DestroyWindow(hwndOld
);
304 // it is for the old bitmap control and can't be used with the new one
307 ::DeleteObject((HBITMAP
) m_hBitmap
);
311 if ( m_disabledImgList
)
313 delete m_disabledImgList
;
314 m_disabledImgList
= NULL
;
320 wxToolBar::~wxToolBar()
322 // we must refresh the frame size when the toolbar is deleted but the frame
323 // is not - otherwise toolbar leaves a hole in the place it used to occupy
324 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
325 if ( frame
&& !frame
->IsBeingDeleted() )
326 frame
->SendSizeEvent();
329 ::DeleteObject((HBITMAP
) m_hBitmap
);
331 delete m_disabledImgList
;
334 wxSize
wxToolBar::DoGetBestSize() const
339 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
341 // maybe an old (< 0x400) Windows version? try to approximate the
342 // toolbar size ourselves
343 sizeBest
= GetToolSize();
344 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
345 sizeBest
.x
*= GetToolsCount();
347 // reverse horz and vertical components if necessary
348 if ( HasFlag(wxTB_VERTICAL
) )
351 sizeBest
.x
= sizeBest
.y
;
357 sizeBest
.x
= size
.cx
;
358 sizeBest
.y
= size
.cy
;
361 CacheBestSize(sizeBest
);
366 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
368 // toolbars never have border, giving one to them results in broken
370 WXDWORD msStyle
= wxControl::MSWGetStyle
372 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
375 // always include this one, it never hurts and setting it later
376 // only if we do have tooltips wouldn't work
377 msStyle
|= TBSTYLE_TOOLTIPS
;
379 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
381 // static as it doesn't change during the program lifetime
382 static int s_verComCtl
= wxApp::GetComCtl32Version();
384 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
385 // style with 6.00 (part of Windows XP) leads to the toolbar with
386 // incorrect background colour - and not using it still results in the
387 // correct (flat) toolbar, so don't use it there
388 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
389 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
391 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
392 msStyle
|= TBSTYLE_LIST
;
395 if ( style
& wxTB_NODIVIDER
)
396 msStyle
|= CCS_NODIVIDER
;
398 if ( style
& wxTB_NOALIGN
)
399 msStyle
|= CCS_NOPARENTALIGN
;
401 if ( style
& wxTB_VERTICAL
)
407 // ----------------------------------------------------------------------------
408 // adding/removing tools
409 // ----------------------------------------------------------------------------
411 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
413 // nothing special to do here - we really create the toolbar buttons in
417 InvalidateBestSize();
421 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
423 // the main difficulty we have here is with the controls in the toolbars:
424 // as we (sometimes) use several separators to cover up the space used by
425 // them, the indices are not the same for us and the toolbar
427 // first determine the position of the first button to delete: it may be
428 // different from pos if we use several separators to cover the space used
430 wxToolBarToolsList::compatibility_iterator node
;
431 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
433 wxToolBarToolBase
*tool2
= node
->GetData();
436 // let node point to the next node in the list
437 node
= node
->GetNext();
442 if ( tool2
->IsControl() )
443 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
446 // now determine the number of buttons to delete and the area taken by them
447 size_t nButtonsToDelete
= 1;
449 // get the size of the button we're going to delete
451 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
453 wxLogLastError(_T("TB_GETITEMRECT"));
456 int width
= r
.right
- r
.left
;
458 if ( tool
->IsControl() )
460 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
461 width
*= nButtonsToDelete
;
464 // do delete all buttons
465 m_nButtons
-= nButtonsToDelete
;
466 while ( nButtonsToDelete
-- > 0 )
468 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
470 wxLogLastError(wxT("TB_DELETEBUTTON"));
478 // and finally reposition all the controls after this button (the toolbar
479 // takes care of all normal items)
480 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
482 wxToolBarToolBase
*tool2
= node
->GetData();
483 if ( tool2
->IsControl() )
486 wxControl
*control
= tool2
->GetControl();
487 control
->GetPosition(&x
, NULL
);
488 control
->Move(x
- width
, wxDefaultCoord
);
492 InvalidateBestSize();
497 void wxToolBar::CreateDisabledImageList()
499 if (m_disabledImgList
!= NULL
)
501 delete m_disabledImgList
;
502 m_disabledImgList
= NULL
;
505 // as we can't use disabled image list with older versions of comctl32.dll,
506 // don't even bother creating it
507 if ( wxTheApp
->GetComCtl32Version() >= 470 )
509 // search for the first disabled button img in the toolbar, if any
510 for ( wxToolBarToolsList::compatibility_iterator
511 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
513 wxToolBarToolBase
*tool
= node
->GetData();
514 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
515 if ( bmpDisabled
.Ok() )
517 m_disabledImgList
= new wxImageList
521 bmpDisabled
.GetMask() != NULL
,
528 // we don't have any disabled bitmaps
532 bool wxToolBar::Realize()
534 const size_t nTools
= GetToolsCount();
539 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
541 bool doRemap
, doRemapBg
, doTransparent
;
542 doRemapBg
= doRemap
= doTransparent
= false;
545 int remapValue
= (-1);
546 const wxChar
*remapOptionStr
= wxT("msw.remap");
547 if (wxSystemOptions::HasOption( remapOptionStr
))
548 remapValue
= wxSystemOptions::GetOptionInt( remapOptionStr
);
550 doTransparent
= (remapValue
== 2);
553 doRemap
= (remapValue
!= 0);
554 doRemapBg
= !doRemap
;
558 // delete all old buttons, if any
559 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
561 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
563 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
567 // First, add the bitmap: we use one bitmap for all toolbar buttons
568 // ----------------------------------------------------------------
570 wxToolBarToolsList::compatibility_iterator node
;
574 if ( HasFlag(wxTB_NOICONS
) )
576 // no icons, don't leave space for them
580 else // do show icons
582 // if we already have a bitmap, we'll replace the existing one --
583 // otherwise we'll install a new one
584 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
586 sizeBmp
.x
= m_defaultWidth
;
587 sizeBmp
.y
= m_defaultHeight
;
589 const wxCoord totalBitmapWidth
= m_defaultWidth
*
590 wx_truncate_cast(wxCoord
, nTools
),
591 totalBitmapHeight
= m_defaultHeight
;
593 // Create a bitmap and copy all the tool bitmaps into it
594 wxMemoryDC dcAllButtons
;
595 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
596 dcAllButtons
.SelectObject(bitmap
);
600 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
602 dcAllButtons
.SetBackground(wxBrush(GetBackgroundColour()));
604 dcAllButtons
.SetBackground(wxBrush(wxColour(192,192,192)));
606 dcAllButtons
.Clear();
608 m_hBitmap
= bitmap
.GetHBITMAP();
609 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
614 dcAllButtons
.SelectObject(wxNullBitmap
);
616 // Even if we're not remapping the bitmap
617 // content, we still have to remap the background.
618 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
619 totalBitmapWidth
, totalBitmapHeight
);
621 dcAllButtons
.SelectObject(bitmap
);
623 #endif // !__WXWINCE__
625 // the button position
628 // the number of buttons (not separators)
631 CreateDisabledImageList();
632 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
634 wxToolBarToolBase
*tool
= node
->GetData();
635 if ( tool
->IsButton() )
637 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
639 const int w
= bmp
.GetWidth();
640 const int h
= bmp
.GetHeight();
644 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
645 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
647 // notice the last parameter: do use mask
648 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
652 wxFAIL_MSG( _T("invalid tool button bitmap") );
655 // also deal with disabled bitmap if we want to use them
656 if ( m_disabledImgList
)
658 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
660 if ( !bmpDisabled
.Ok() )
662 // no disabled bitmap specified but we still need to
663 // fill the space in the image list with something, so
664 // we grey out the normal bitmap
666 wxCreateGreyedImage(bmp
.ConvertToImage(), imgGreyed
);
670 // we need to have light grey background colour for
671 // MapBitmap() to work correctly
672 for ( int y
= 0; y
< h
; y
++ )
674 for ( int x
= 0; x
< w
; x
++ )
676 if ( imgGreyed
.IsTransparent(x
, y
) )
677 imgGreyed
.SetRGB(x
, y
,
679 wxLIGHT_GREY
->Green(),
680 wxLIGHT_GREY
->Blue());
685 bmpDisabled
= wxBitmap(imgGreyed
);
687 #endif // wxUSE_IMAGE
690 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
692 m_disabledImgList
->Add(bmpDisabled
);
695 // still inc width and number of buttons because otherwise the
696 // subsequent buttons will all be shifted which is rather confusing
697 // (and like this you'd see immediately which bitmap was bad)
703 dcAllButtons
.SelectObject(wxNullBitmap
);
705 // don't delete this HBITMAP!
706 bitmap
.SetHBITMAP(0);
710 // Map to system colours
711 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
712 totalBitmapWidth
, totalBitmapHeight
);
715 bool addBitmap
= true;
717 if ( oldToolBarBitmap
)
719 #ifdef TB_REPLACEBITMAP
720 if ( wxApp::GetComCtl32Version() >= 400 )
722 TBREPLACEBITMAP replaceBitmap
;
723 replaceBitmap
.hInstOld
= NULL
;
724 replaceBitmap
.hInstNew
= NULL
;
725 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
726 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
727 replaceBitmap
.nButtons
= nButtons
;
728 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
729 0, (LPARAM
) &replaceBitmap
) )
731 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
734 ::DeleteObject(oldToolBarBitmap
);
740 #endif // TB_REPLACEBITMAP
742 // we can't replace the old bitmap, so we will add another one
743 // (awfully inefficient, but what else to do?) and shift the bitmap
744 // indices accordingly
747 bitmapId
= m_nButtons
;
751 if ( addBitmap
) // no old bitmap or we can't replace it
753 TBADDBITMAP addBitmap
;
755 addBitmap
.nID
= (UINT
) hBitmap
;
756 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
757 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
759 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
763 if ( m_disabledImgList
)
765 HIMAGELIST oldImageList
= (HIMAGELIST
)
766 ::SendMessage(GetHwnd(),
767 TB_SETDISABLEDIMAGELIST
,
769 (LPARAM
)GetHimagelistOf(m_disabledImgList
));
771 // delete previous image list if any
773 ::DeleteObject( oldImageList
);
777 // don't call SetToolBitmapSize() as we don't want to change the values of
778 // m_defaultWidth/Height
779 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
780 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
782 wxLogLastError(_T("TB_SETBITMAPSIZE"));
785 // Next add the buttons and separators
786 // -----------------------------------
788 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
790 // this array will hold the indices of all controls in the toolbar
791 wxArrayInt controlIds
;
793 bool lastWasRadio
= false;
795 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
797 wxToolBarToolBase
*tool
= node
->GetData();
799 // don't add separators to the vertical toolbar with old comctl32.dll
800 // versions as they didn't handle this properly
801 if ( isVertical
&& tool
->IsSeparator() &&
802 wxApp::GetComCtl32Version() <= 472 )
807 TBBUTTON
& button
= buttons
[i
];
809 wxZeroMemory(button
);
811 bool isRadio
= false;
812 switch ( tool
->GetStyle() )
814 case wxTOOL_STYLE_CONTROL
:
815 button
.idCommand
= tool
->GetId();
816 // fall through: create just a separator too
818 case wxTOOL_STYLE_SEPARATOR
:
819 button
.fsState
= TBSTATE_ENABLED
;
820 button
.fsStyle
= TBSTYLE_SEP
;
823 case wxTOOL_STYLE_BUTTON
:
824 if ( !HasFlag(wxTB_NOICONS
) )
825 button
.iBitmap
= bitmapId
;
827 if ( HasFlag(wxTB_TEXT
) )
829 const wxString
& label
= tool
->GetLabel();
830 if ( !label
.empty() )
831 button
.iString
= (int)label
.c_str();
834 button
.idCommand
= tool
->GetId();
836 if ( tool
->IsEnabled() )
837 button
.fsState
|= TBSTATE_ENABLED
;
838 if ( tool
->IsToggled() )
839 button
.fsState
|= TBSTATE_CHECKED
;
841 switch ( tool
->GetKind() )
844 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
848 // the first item in the radio group is checked by
849 // default to be consistent with wxGTK and the menu
851 button
.fsState
|= TBSTATE_CHECKED
;
853 if (tool
->Toggle(true))
855 DoToggleTool(tool
, true);
858 else if (tool
->IsToggled())
860 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
861 int prevIndex
= i
- 1;
864 TBBUTTON
& prevButton
= buttons
[prevIndex
];
865 wxToolBarToolBase
*tool
= nodePrev
->GetData();
866 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
869 if ( tool
->Toggle(false) )
870 DoToggleTool(tool
, false);
872 prevButton
.fsState
= TBSTATE_ENABLED
;
873 nodePrev
= nodePrev
->GetPrevious();
882 button
.fsStyle
= TBSTYLE_CHECK
;
886 button
.fsStyle
= TBSTYLE_BUTTON
;
890 wxFAIL_MSG( _T("unexpected toolbar button kind") );
891 button
.fsStyle
= TBSTYLE_BUTTON
;
899 lastWasRadio
= isRadio
;
904 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
906 wxLogLastError(wxT("TB_ADDBUTTONS"));
911 // Deal with the controls finally
912 // ------------------------------
914 // adjust the controls size to fit nicely in the toolbar
917 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
919 wxToolBarToolBase
*tool
= node
->GetData();
921 // we calculate the running y coord for vertical toolbars so we need to
922 // get the items size for all items but for the horizontal ones we
923 // don't need to deal with the non controls
924 bool isControl
= tool
->IsControl();
925 if ( !isControl
&& !isVertical
)
928 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
929 // latter only appeared in v4.70 of comctl32.dll
931 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
932 index
, (LPARAM
)(LPRECT
)&r
) )
934 wxLogLastError(wxT("TB_GETITEMRECT"));
939 // can only be control if isVertical
940 y
+= r
.bottom
- r
.top
;
945 wxControl
*control
= tool
->GetControl();
946 wxSize size
= control
->GetSize();
948 // the position of the leftmost controls corner
949 int left
= wxDefaultCoord
;
951 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
952 #ifdef TB_SETBUTTONINFO
953 // available in headers, now check whether it is available now
955 if ( wxApp::GetComCtl32Version() >= 471 )
957 // set the (underlying) separators width to be that of the
960 tbbi
.cbSize
= sizeof(tbbi
);
961 tbbi
.dwMask
= TBIF_SIZE
;
962 tbbi
.cx
= (WORD
)size
.x
;
963 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
964 tool
->GetId(), (LPARAM
)&tbbi
) )
966 // the id is probably invalid?
967 wxLogLastError(wxT("TB_SETBUTTONINFO"));
971 #endif // comctl32.dll 4.71
972 // TB_SETBUTTONINFO unavailable
974 // try adding several separators to fit the controls width
975 int widthSep
= r
.right
- r
.left
;
981 tbb
.fsState
= TBSTATE_ENABLED
;
982 tbb
.fsStyle
= TBSTYLE_SEP
;
984 size_t nSeparators
= size
.x
/ widthSep
;
985 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
987 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
988 index
, (LPARAM
)&tbb
) )
990 wxLogLastError(wxT("TB_INSERTBUTTON"));
996 // remember the number of separators we used - we'd have to
997 // delete all of them later
998 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1000 // adjust the controls width to exactly cover the separators
1001 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
1004 // position the control itself correctly vertically
1005 int height
= r
.bottom
- r
.top
;
1006 int diff
= height
- size
.y
;
1009 // the control is too high, resize to fit
1010 control
->SetSize(wxDefaultCoord
, height
- 2);
1021 y
+= height
+ 2 * GetMargins().y
;
1023 else // horizontal toolbar
1025 if ( left
== wxDefaultCoord
)
1031 control
->Move(left
, top
+ (diff
+ 1) / 2);
1034 // the max index is the "real" number of buttons - i.e. counting even the
1035 // separators which we added just for aligning the controls
1040 if ( m_maxRows
== 0 )
1041 // if not set yet, only one row
1044 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1046 if ( m_maxRows
== 0 )
1047 // if not set yet, have one column
1048 SetRows(m_nButtons
);
1051 InvalidateBestSize();
1052 SetBestFittingSize();
1058 // ----------------------------------------------------------------------------
1060 // ----------------------------------------------------------------------------
1062 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
1064 wxToolBarToolBase
*tool
= FindById((int)id
);
1068 bool toggled
= false; // just to suppress warnings
1070 if ( tool
->CanBeToggled() )
1072 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1073 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1075 // ignore the event when a radio button is released, as this doesn't
1076 // seem to happen at all, and is handled otherwise
1077 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1080 tool
->Toggle(toggled
);
1081 UnToggleRadioGroup(tool
);
1084 // OnLeftClick() can veto the button state change - for buttons which
1085 // may be toggled only, of couse
1086 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1089 tool
->Toggle(!toggled
);
1091 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1097 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1099 WXLPARAM
*WXUNUSED(result
))
1102 // First check if this applies to us
1103 NMHDR
*hdr
= (NMHDR
*)lParam
;
1105 // the tooltips control created by the toolbar is sometimes Unicode, even
1106 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1107 UINT code
= hdr
->code
;
1108 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1111 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1112 if ( toolTipWnd
!= hdr
->hwndFrom
)
1115 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1116 int id
= (int)ttText
->hdr
.idFrom
;
1118 wxToolBarToolBase
*tool
= FindById(id
);
1122 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1124 wxUnusedVar(lParam
);
1130 // ----------------------------------------------------------------------------
1132 // ----------------------------------------------------------------------------
1134 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1136 wxToolBarBase::SetToolBitmapSize(size
);
1138 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1141 void wxToolBar::SetRows(int nRows
)
1143 if ( nRows
== m_maxRows
)
1145 // avoid resizing the frame uselessly
1149 // TRUE in wParam means to create at least as many rows, FALSE -
1152 ::SendMessage(GetHwnd(), TB_SETROWS
,
1153 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1161 // The button size is bigger than the bitmap size
1162 wxSize
wxToolBar::GetToolSize() const
1164 // TB_GETBUTTONSIZE is supported from version 4.70
1165 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1166 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1167 && !defined (__DIGITALMARS__)
1168 if ( wxApp::GetComCtl32Version() >= 470 )
1170 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1172 return wxSize(LOWORD(dw
), HIWORD(dw
));
1175 #endif // comctl32.dll 4.70+
1178 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1183 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1186 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1188 for ( ; current
; current
= current
->GetNext() )
1191 return current
->GetData();
1193 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1194 size_t separators
= tool
->GetSeparatorsCount();
1196 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1197 // otherwise, skip as many items as the separator count, plus the
1199 index
-= separators
? separators
+ 1 : 1;
1205 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1210 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1212 // MBN: when the point ( x, y ) is close to the toolbar border
1213 // TB_HITTEST returns m_nButtons ( not -1 )
1214 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1215 // it's a separator or there is no tool at all there
1216 return (wxToolBarToolBase
*)NULL
;
1218 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1219 // we don't use the dummy separators hack
1220 #ifdef TB_SETBUTTONINFO
1221 if ( wxApp::GetComCtl32Version() >= 471 )
1223 return m_tools
.Item((size_t)index
)->GetData();
1226 #endif // TB_SETBUTTONINFO
1228 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1232 void wxToolBar::UpdateSize()
1234 // the toolbar size changed
1235 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1237 // we must also refresh the frame after the toolbar size (possibly) changed
1238 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1240 frame
->SendSizeEvent();
1243 // ----------------------------------------------------------------------------
1245 // ---------------------------------------------------------------------------
1247 void wxToolBar::SetWindowStyleFlag(long style
)
1249 // the style bits whose changes force us to recreate the toolbar
1250 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1252 const long styleOld
= GetWindowStyle();
1254 wxToolBarBase::SetWindowStyleFlag(style
);
1256 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1257 // also fatal as we'd then try to recreate the toolbar when it's just being
1259 if ( GetToolsCount() &&
1260 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1262 // to remove the text labels, simply re-realizing the toolbar is enough
1263 // but I don't know of any way to add the text to an existing toolbar
1264 // other than by recreating it entirely
1269 // ----------------------------------------------------------------------------
1271 // ----------------------------------------------------------------------------
1273 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1275 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1276 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1279 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1281 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1282 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1285 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1287 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1288 // without, so we really need to delete the button and recreate it here
1289 wxFAIL_MSG( _T("not implemented") );
1292 // ----------------------------------------------------------------------------
1294 // ----------------------------------------------------------------------------
1296 // Responds to colour changes, and passes event on to children.
1297 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1299 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1301 // Remap the buttons
1304 // Relayout the toolbar
1305 int nrows
= m_maxRows
;
1306 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1311 // let the event propagate further
1315 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1317 if (event
.Leaving() && m_pInTool
)
1324 if ( event
.RightDown() )
1326 // find the tool under the mouse
1328 event
.GetPosition(&x
, &y
);
1330 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1331 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1339 // This handler is required to allow the toolbar to be set to a non-default
1340 // colour: for example, when it must blend in with a notebook page.
1341 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1343 wxColour bgCol
= GetBackgroundColour();
1350 // notice that this 'dumb' implementation may cause flicker for some of the
1351 // controls in which case they should intercept wxEraseEvent and process it
1352 // themselves somehow
1355 ::GetClientRect(GetHwnd(), &rect
);
1357 HBRUSH hBrush
= ::CreateSolidBrush(wxColourToRGB(bgCol
));
1359 HDC hdc
= GetHdcOf((*event
.GetDC()));
1362 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1365 ::FillRect(hdc
, &rect
, hBrush
);
1366 ::DeleteObject(hBrush
);
1369 ::SetMapMode(hdc
, mode
);
1373 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1375 // calculate our minor dimension ourselves - we're confusing the standard
1376 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1378 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1382 if ( GetWindowStyle() & wxTB_VERTICAL
)
1384 w
= r
.right
- r
.left
;
1387 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1394 if (HasFlag( wxTB_FLAT
))
1395 h
= r
.bottom
- r
.top
- 3;
1397 h
= r
.bottom
- r
.top
;
1400 // FIXME: hardcoded separator line height...
1401 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1406 if ( MAKELPARAM(w
, h
) != lParam
)
1408 // size really changed
1412 // message processed
1419 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1421 // erase any dummy separators which were used
1422 // for aligning the controls if any here
1424 // first of all, are there any controls at all?
1425 wxToolBarToolsList::compatibility_iterator node
;
1426 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1428 if ( node
->GetData()->IsControl() )
1433 // no controls, nothing to erase
1436 // prepare the DC on which we'll be drawing
1437 wxClientDC
dc(this);
1438 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1439 dc
.SetPen(*wxTRANSPARENT_PEN
);
1442 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1443 // nothing to redraw anyhow
1447 wxCopyRECTToRect(r
, rectUpdate
);
1449 dc
.SetClippingRegion(rectUpdate
);
1451 // draw the toolbar tools, separators &c normally
1452 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1454 // for each control in the toolbar find all the separators intersecting it
1457 // NB: this is really the only way to do it as we don't know if a separator
1458 // corresponds to a control (i.e. is a dummy one) or a real one
1460 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1462 wxToolBarToolBase
*tool
= node
->GetData();
1463 if ( tool
->IsControl() )
1465 // get the control rect in our client coords
1466 wxControl
*control
= tool
->GetControl();
1467 wxRect rectCtrl
= control
->GetRect();
1469 // iterate over all buttons
1471 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1472 for ( int n
= 0; n
< count
; n
++ )
1474 // is it a separator?
1475 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1478 wxLogDebug(_T("TB_GETBUTTON failed?"));
1483 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1486 // get the bounding rect of the separator
1488 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1491 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1496 // does it intersect the control?
1498 wxCopyRECTToRect(r
, rectItem
);
1499 if ( rectCtrl
.Intersects(rectItem
) )
1501 // yes, do erase it!
1502 dc
.DrawRectangle(rectItem
);
1504 // Necessary in case we use a no-paint-on-size
1505 // style in the parent: the controls can disappear
1506 control
->Refresh(false);
1515 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1517 wxCoord x
= GET_X_LPARAM(lParam
),
1518 y
= GET_Y_LPARAM(lParam
);
1519 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1521 // cursor left current tool
1522 if ( tool
!= m_pInTool
&& !tool
)
1528 // cursor entered a tool
1529 if ( tool
!= m_pInTool
&& tool
)
1532 OnMouseEnter( tool
->GetId() );
1536 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1541 // we don't handle mouse moves, so always pass the message to
1542 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1543 HandleMouseMove(wParam
, lParam
);
1547 if ( HandleSize(wParam
, lParam
) )
1553 if ( HandlePaint(wParam
, lParam
) )
1561 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1564 // ----------------------------------------------------------------------------
1565 // private functions
1566 // ----------------------------------------------------------------------------
1568 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1574 wxLogLastError(_T("CreateCompatibleDC"));
1579 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1583 wxLogLastError(_T("SelectObject"));
1588 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1590 for ( int i
= 0; i
< width
; i
++ )
1592 for ( int j
= 0; j
< height
; j
++ )
1594 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1596 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1598 COLORREF col
= cmap
[k
].from
;
1599 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1600 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1601 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1603 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1612 // VZ: I leave here my attempts to map the bitmap to the system colours
1613 // faster by using BitBlt() even though it's broken currently - but
1614 // maybe someone else can finish it? It should be faster than iterating
1615 // over all pixels...
1617 MemoryHDC hdcMask
, hdcDst
;
1618 if ( !hdcMask
|| !hdcDst
)
1620 wxLogLastError(_T("CreateCompatibleDC"));
1625 // create the target bitmap
1626 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1629 wxLogLastError(_T("CreateCompatibleBitmap"));
1634 // create the monochrome mask bitmap
1635 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1638 wxLogLastError(_T("CreateBitmap(mono)"));
1640 ::DeleteObject(hbmpDst
);
1645 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1646 bmpInMask(hdcMask
, hbmpMask
);
1649 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1651 // create the mask for this colour
1652 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1653 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1655 // replace this colour with the target one in the dst bitmap
1656 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1657 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1659 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1662 MAKEROP4(PATCOPY
, SRCCOPY
));
1664 (void)::SelectObject(hdcDst
, hbrOld
);
1665 ::DeleteObject(hbr
);
1668 ::DeleteObject((HBITMAP
)bitmap
);
1670 return (WXHBITMAP
)hbmpDst
;
1674 #endif // wxUSE_TOOLBAR