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 // workaround for flat toolbar on Windows XP classic style: we have to set
238 // the style after creating the control; doing it at creation time doesn't work
240 if ( style
& wxTB_FLAT
)
242 LRESULT style
= ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
244 if ( !(style
& TBSTYLE_FLAT
) )
245 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
247 #endif // wxUSE_UXTHEME
252 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
254 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
257 // toolbar-specific post initialisation
258 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
263 void wxToolBar::Recreate()
265 const HWND hwndOld
= GetHwnd();
268 // we haven't been created yet, no need to recreate
272 // get the position and size before unsubclassing the old toolbar
273 const wxPoint pos
= GetPosition();
274 const wxSize size
= GetSize();
278 if ( !MSWCreateToolbar(pos
, size
) )
281 wxFAIL_MSG( _T("recreating the toolbar failed") );
286 // reparent all our children under the new toolbar
287 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
289 node
= node
->GetNext() )
291 wxWindow
*win
= node
->GetData();
292 if ( !win
->IsTopLevel() )
293 ::SetParent(GetHwndOf(win
), GetHwnd());
296 // only destroy the old toolbar now --
297 // after all the children had been reparented
298 ::DestroyWindow(hwndOld
);
300 // it is for the old bitmap control and can't be used with the new one
303 ::DeleteObject((HBITMAP
) m_hBitmap
);
307 if ( m_disabledImgList
)
309 delete m_disabledImgList
;
310 m_disabledImgList
= NULL
;
316 wxToolBar::~wxToolBar()
318 // we must refresh the frame size when the toolbar is deleted but the frame
319 // is not - otherwise toolbar leaves a hole in the place it used to occupy
320 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
321 if ( frame
&& !frame
->IsBeingDeleted() )
322 frame
->SendSizeEvent();
325 ::DeleteObject((HBITMAP
) m_hBitmap
);
327 delete m_disabledImgList
;
330 wxSize
wxToolBar::DoGetBestSize() const
335 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
337 // maybe an old (< 0x400) Windows version? try to approximate the
338 // toolbar size ourselves
339 sizeBest
= GetToolSize();
340 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
341 sizeBest
.x
*= GetToolsCount();
343 // reverse horz and vertical components if necessary
344 if ( HasFlag(wxTB_VERTICAL
) )
347 sizeBest
.x
= sizeBest
.y
;
353 sizeBest
.x
= size
.cx
;
354 sizeBest
.y
= size
.cy
;
357 CacheBestSize(sizeBest
);
362 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
364 // toolbars never have border, giving one to them results in broken
366 WXDWORD msStyle
= wxControl::MSWGetStyle
368 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
371 // always include this one, it never hurts and setting it later
372 // only if we do have tooltips wouldn't work
373 msStyle
|= TBSTYLE_TOOLTIPS
;
375 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
377 // static as it doesn't change during the program lifetime
378 static int s_verComCtl
= wxApp::GetComCtl32Version();
380 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
381 // style with 6.00 (part of Windows XP) leads to the toolbar with
382 // incorrect background colour - and not using it still results in the
383 // correct (flat) toolbar, so don't use it there
384 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
385 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
387 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
388 msStyle
|= TBSTYLE_LIST
;
391 if ( style
& wxTB_NODIVIDER
)
392 msStyle
|= CCS_NODIVIDER
;
394 if ( style
& wxTB_NOALIGN
)
395 msStyle
|= CCS_NOPARENTALIGN
;
397 if ( style
& wxTB_VERTICAL
)
403 // ----------------------------------------------------------------------------
404 // adding/removing tools
405 // ----------------------------------------------------------------------------
407 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
409 // nothing special to do here - we really create the toolbar buttons in
413 InvalidateBestSize();
417 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
419 // the main difficulty we have here is with the controls in the toolbars:
420 // as we (sometimes) use several separators to cover up the space used by
421 // them, the indices are not the same for us and the toolbar
423 // first determine the position of the first button to delete: it may be
424 // different from pos if we use several separators to cover the space used
426 wxToolBarToolsList::compatibility_iterator node
;
427 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
429 wxToolBarToolBase
*tool2
= node
->GetData();
432 // let node point to the next node in the list
433 node
= node
->GetNext();
438 if ( tool2
->IsControl() )
439 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
442 // now determine the number of buttons to delete and the area taken by them
443 size_t nButtonsToDelete
= 1;
445 // get the size of the button we're going to delete
447 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
449 wxLogLastError(_T("TB_GETITEMRECT"));
452 int width
= r
.right
- r
.left
;
454 if ( tool
->IsControl() )
456 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
457 width
*= nButtonsToDelete
;
460 // do delete all buttons
461 m_nButtons
-= nButtonsToDelete
;
462 while ( nButtonsToDelete
-- > 0 )
464 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
466 wxLogLastError(wxT("TB_DELETEBUTTON"));
474 // and finally reposition all the controls after this button (the toolbar
475 // takes care of all normal items)
476 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
478 wxToolBarToolBase
*tool2
= node
->GetData();
479 if ( tool2
->IsControl() )
482 wxControl
*control
= tool2
->GetControl();
483 control
->GetPosition(&x
, NULL
);
484 control
->Move(x
- width
, wxDefaultCoord
);
488 InvalidateBestSize();
493 void wxToolBar::CreateDisabledImageList()
495 if (m_disabledImgList
!= NULL
)
497 delete m_disabledImgList
;
498 m_disabledImgList
= NULL
;
501 // as we can't use disabled image list with older versions of comctl32.dll,
502 // don't even bother creating it
503 if ( wxTheApp
->GetComCtl32Version() >= 470 )
505 // search for the first disabled button img in the toolbar, if any
506 for ( wxToolBarToolsList::compatibility_iterator
507 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
509 wxToolBarToolBase
*tool
= node
->GetData();
510 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
511 if ( bmpDisabled
.Ok() )
513 m_disabledImgList
= new wxImageList
517 bmpDisabled
.GetMask() != NULL
,
524 // we don't have any disabled bitmaps
528 bool wxToolBar::Realize()
530 const size_t nTools
= GetToolsCount();
535 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
537 bool doRemap
, doRemapBg
, doTransparent
;
538 doRemapBg
= doRemap
= doTransparent
= false;
541 int remapValue
= (-1);
542 const wxChar
*remapOptionStr
= wxT("msw.remap");
543 if (wxSystemOptions::HasOption( remapOptionStr
))
544 remapValue
= wxSystemOptions::GetOptionInt( remapOptionStr
);
546 doTransparent
= (remapValue
== 2);
549 doRemap
= (remapValue
!= 0);
550 doRemapBg
= !doRemap
;
554 // delete all old buttons, if any
555 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
557 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
559 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
563 // First, add the bitmap: we use one bitmap for all toolbar buttons
564 // ----------------------------------------------------------------
566 wxToolBarToolsList::compatibility_iterator node
;
570 if ( HasFlag(wxTB_NOICONS
) )
572 // no icons, don't leave space for them
576 else // do show icons
578 // if we already have a bitmap, we'll replace the existing one --
579 // otherwise we'll install a new one
580 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
582 sizeBmp
.x
= m_defaultWidth
;
583 sizeBmp
.y
= m_defaultHeight
;
585 const wxCoord totalBitmapWidth
= m_defaultWidth
*
586 wx_truncate_cast(wxCoord
, nTools
),
587 totalBitmapHeight
= m_defaultHeight
;
589 // Create a bitmap and copy all the tool bitmaps into it
590 wxMemoryDC dcAllButtons
;
591 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
592 dcAllButtons
.SelectObject(bitmap
);
596 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
598 dcAllButtons
.SetBackground(wxBrush(GetBackgroundColour()));
600 dcAllButtons
.SetBackground(wxBrush(wxColour(192,192,192)));
602 dcAllButtons
.Clear();
604 m_hBitmap
= bitmap
.GetHBITMAP();
605 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
610 dcAllButtons
.SelectObject(wxNullBitmap
);
612 // Even if we're not remapping the bitmap
613 // content, we still have to remap the background.
614 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
615 totalBitmapWidth
, totalBitmapHeight
);
617 dcAllButtons
.SelectObject(bitmap
);
619 #endif // !__WXWINCE__
621 // the button position
624 // the number of buttons (not separators)
627 CreateDisabledImageList();
628 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
630 wxToolBarToolBase
*tool
= node
->GetData();
631 if ( tool
->IsButton() )
633 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
635 const int w
= bmp
.GetWidth();
636 const int h
= bmp
.GetHeight();
640 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
641 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
643 // notice the last parameter: do use mask
644 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
648 wxFAIL_MSG( _T("invalid tool button bitmap") );
651 // also deal with disabled bitmap if we want to use them
652 if ( m_disabledImgList
)
654 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
655 #if wxUSE_IMAGE && wxUSE_WXDIB
656 if ( !bmpDisabled
.Ok() )
658 // no disabled bitmap specified but we still need to
659 // fill the space in the image list with something, so
660 // we grey out the normal bitmap
662 wxCreateGreyedImage(bmp
.ConvertToImage(), imgGreyed
);
666 // we need to have light grey background colour for
667 // MapBitmap() to work correctly
668 for ( int y
= 0; y
< h
; y
++ )
670 for ( int x
= 0; x
< w
; x
++ )
672 if ( imgGreyed
.IsTransparent(x
, y
) )
673 imgGreyed
.SetRGB(x
, y
,
675 wxLIGHT_GREY
->Green(),
676 wxLIGHT_GREY
->Blue());
681 bmpDisabled
= wxBitmap(imgGreyed
);
683 #endif // wxUSE_IMAGE
686 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
688 m_disabledImgList
->Add(bmpDisabled
);
691 // still inc width and number of buttons because otherwise the
692 // subsequent buttons will all be shifted which is rather confusing
693 // (and like this you'd see immediately which bitmap was bad)
699 dcAllButtons
.SelectObject(wxNullBitmap
);
701 // don't delete this HBITMAP!
702 bitmap
.SetHBITMAP(0);
706 // Map to system colours
707 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
708 totalBitmapWidth
, totalBitmapHeight
);
711 bool addBitmap
= true;
713 if ( oldToolBarBitmap
)
715 #ifdef TB_REPLACEBITMAP
716 if ( wxApp::GetComCtl32Version() >= 400 )
718 TBREPLACEBITMAP replaceBitmap
;
719 replaceBitmap
.hInstOld
= NULL
;
720 replaceBitmap
.hInstNew
= NULL
;
721 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
722 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
723 replaceBitmap
.nButtons
= nButtons
;
724 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
725 0, (LPARAM
) &replaceBitmap
) )
727 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
730 ::DeleteObject(oldToolBarBitmap
);
736 #endif // TB_REPLACEBITMAP
738 // we can't replace the old bitmap, so we will add another one
739 // (awfully inefficient, but what else to do?) and shift the bitmap
740 // indices accordingly
743 bitmapId
= m_nButtons
;
747 if ( addBitmap
) // no old bitmap or we can't replace it
749 TBADDBITMAP addBitmap
;
751 addBitmap
.nID
= (UINT
) hBitmap
;
752 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
753 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
755 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
759 if ( m_disabledImgList
)
761 HIMAGELIST oldImageList
= (HIMAGELIST
)
762 ::SendMessage(GetHwnd(),
763 TB_SETDISABLEDIMAGELIST
,
765 (LPARAM
)GetHimagelistOf(m_disabledImgList
));
767 // delete previous image list if any
769 ::DeleteObject( oldImageList
);
773 // don't call SetToolBitmapSize() as we don't want to change the values of
774 // m_defaultWidth/Height
775 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
776 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
778 wxLogLastError(_T("TB_SETBITMAPSIZE"));
781 // Next add the buttons and separators
782 // -----------------------------------
784 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
786 // this array will hold the indices of all controls in the toolbar
787 wxArrayInt controlIds
;
789 bool lastWasRadio
= false;
791 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
793 wxToolBarToolBase
*tool
= node
->GetData();
795 // don't add separators to the vertical toolbar with old comctl32.dll
796 // versions as they didn't handle this properly
797 if ( isVertical
&& tool
->IsSeparator() &&
798 wxApp::GetComCtl32Version() <= 472 )
803 TBBUTTON
& button
= buttons
[i
];
805 wxZeroMemory(button
);
807 bool isRadio
= false;
808 switch ( tool
->GetStyle() )
810 case wxTOOL_STYLE_CONTROL
:
811 button
.idCommand
= tool
->GetId();
812 // fall through: create just a separator too
814 case wxTOOL_STYLE_SEPARATOR
:
815 button
.fsState
= TBSTATE_ENABLED
;
816 button
.fsStyle
= TBSTYLE_SEP
;
819 case wxTOOL_STYLE_BUTTON
:
820 if ( !HasFlag(wxTB_NOICONS
) )
821 button
.iBitmap
= bitmapId
;
823 if ( HasFlag(wxTB_TEXT
) )
825 const wxString
& label
= tool
->GetLabel();
826 if ( !label
.empty() )
827 button
.iString
= (int)label
.c_str();
830 button
.idCommand
= tool
->GetId();
832 if ( tool
->IsEnabled() )
833 button
.fsState
|= TBSTATE_ENABLED
;
834 if ( tool
->IsToggled() )
835 button
.fsState
|= TBSTATE_CHECKED
;
837 switch ( tool
->GetKind() )
840 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
844 // the first item in the radio group is checked by
845 // default to be consistent with wxGTK and the menu
847 button
.fsState
|= TBSTATE_CHECKED
;
849 if (tool
->Toggle(true))
851 DoToggleTool(tool
, true);
854 else if (tool
->IsToggled())
856 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
857 int prevIndex
= i
- 1;
860 TBBUTTON
& prevButton
= buttons
[prevIndex
];
861 wxToolBarToolBase
*tool
= nodePrev
->GetData();
862 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
865 if ( tool
->Toggle(false) )
866 DoToggleTool(tool
, false);
868 prevButton
.fsState
= TBSTATE_ENABLED
;
869 nodePrev
= nodePrev
->GetPrevious();
878 button
.fsStyle
= TBSTYLE_CHECK
;
882 button
.fsStyle
= TBSTYLE_BUTTON
;
886 wxFAIL_MSG( _T("unexpected toolbar button kind") );
887 button
.fsStyle
= TBSTYLE_BUTTON
;
895 lastWasRadio
= isRadio
;
900 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
902 wxLogLastError(wxT("TB_ADDBUTTONS"));
907 // Deal with the controls finally
908 // ------------------------------
910 // adjust the controls size to fit nicely in the toolbar
913 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
915 wxToolBarToolBase
*tool
= node
->GetData();
917 // we calculate the running y coord for vertical toolbars so we need to
918 // get the items size for all items but for the horizontal ones we
919 // don't need to deal with the non controls
920 bool isControl
= tool
->IsControl();
921 if ( !isControl
&& !isVertical
)
924 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
925 // latter only appeared in v4.70 of comctl32.dll
927 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
928 index
, (LPARAM
)(LPRECT
)&r
) )
930 wxLogLastError(wxT("TB_GETITEMRECT"));
935 // can only be control if isVertical
936 y
+= r
.bottom
- r
.top
;
941 wxControl
*control
= tool
->GetControl();
942 wxSize size
= control
->GetSize();
944 // the position of the leftmost controls corner
945 int left
= wxDefaultCoord
;
947 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
948 #ifdef TB_SETBUTTONINFO
949 // available in headers, now check whether it is available now
951 if ( wxApp::GetComCtl32Version() >= 471 )
953 // set the (underlying) separators width to be that of the
956 tbbi
.cbSize
= sizeof(tbbi
);
957 tbbi
.dwMask
= TBIF_SIZE
;
958 tbbi
.cx
= (WORD
)size
.x
;
959 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
960 tool
->GetId(), (LPARAM
)&tbbi
) )
962 // the id is probably invalid?
963 wxLogLastError(wxT("TB_SETBUTTONINFO"));
967 #endif // comctl32.dll 4.71
968 // TB_SETBUTTONINFO unavailable
970 // try adding several separators to fit the controls width
971 int widthSep
= r
.right
- r
.left
;
977 tbb
.fsState
= TBSTATE_ENABLED
;
978 tbb
.fsStyle
= TBSTYLE_SEP
;
980 size_t nSeparators
= size
.x
/ widthSep
;
981 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
983 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
984 index
, (LPARAM
)&tbb
) )
986 wxLogLastError(wxT("TB_INSERTBUTTON"));
992 // remember the number of separators we used - we'd have to
993 // delete all of them later
994 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
996 // adjust the controls width to exactly cover the separators
997 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
1000 // position the control itself correctly vertically
1001 int height
= r
.bottom
- r
.top
;
1002 int diff
= height
- size
.y
;
1005 // the control is too high, resize to fit
1006 control
->SetSize(wxDefaultCoord
, height
- 2);
1017 y
+= height
+ 2 * GetMargins().y
;
1019 else // horizontal toolbar
1021 if ( left
== wxDefaultCoord
)
1027 control
->Move(left
, top
+ (diff
+ 1) / 2);
1030 // the max index is the "real" number of buttons - i.e. counting even the
1031 // separators which we added just for aligning the controls
1036 if ( m_maxRows
== 0 )
1037 // if not set yet, only one row
1040 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1042 if ( m_maxRows
== 0 )
1043 // if not set yet, have one column
1044 SetRows(m_nButtons
);
1047 InvalidateBestSize();
1053 // ----------------------------------------------------------------------------
1055 // ----------------------------------------------------------------------------
1057 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
1059 wxToolBarToolBase
*tool
= FindById((int)id
);
1063 bool toggled
= false; // just to suppress warnings
1065 if ( tool
->CanBeToggled() )
1067 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1068 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1070 // ignore the event when a radio button is released, as this doesn't
1071 // seem to happen at all, and is handled otherwise
1072 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1075 tool
->Toggle(toggled
);
1076 UnToggleRadioGroup(tool
);
1079 // OnLeftClick() can veto the button state change - for buttons which
1080 // may be toggled only, of couse
1081 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1084 tool
->Toggle(!toggled
);
1086 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1092 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1094 WXLPARAM
*WXUNUSED(result
))
1097 // First check if this applies to us
1098 NMHDR
*hdr
= (NMHDR
*)lParam
;
1100 // the tooltips control created by the toolbar is sometimes Unicode, even
1101 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1102 UINT code
= hdr
->code
;
1103 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1106 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1107 if ( toolTipWnd
!= hdr
->hwndFrom
)
1110 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1111 int id
= (int)ttText
->hdr
.idFrom
;
1113 wxToolBarToolBase
*tool
= FindById(id
);
1117 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1119 wxUnusedVar(lParam
);
1125 // ----------------------------------------------------------------------------
1127 // ----------------------------------------------------------------------------
1129 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1131 wxToolBarBase::SetToolBitmapSize(size
);
1133 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1136 void wxToolBar::SetRows(int nRows
)
1138 if ( nRows
== m_maxRows
)
1140 // avoid resizing the frame uselessly
1144 // TRUE in wParam means to create at least as many rows, FALSE -
1147 ::SendMessage(GetHwnd(), TB_SETROWS
,
1148 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1156 // The button size is bigger than the bitmap size
1157 wxSize
wxToolBar::GetToolSize() const
1159 // TB_GETBUTTONSIZE is supported from version 4.70
1160 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1161 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1162 && !defined (__DIGITALMARS__)
1163 if ( wxApp::GetComCtl32Version() >= 470 )
1165 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1167 return wxSize(LOWORD(dw
), HIWORD(dw
));
1170 #endif // comctl32.dll 4.70+
1173 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1178 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1181 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1183 for ( ; current
; current
= current
->GetNext() )
1186 return current
->GetData();
1188 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1189 size_t separators
= tool
->GetSeparatorsCount();
1191 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1192 // otherwise, skip as many items as the separator count, plus the
1194 index
-= separators
? separators
+ 1 : 1;
1200 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1205 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1207 // MBN: when the point ( x, y ) is close to the toolbar border
1208 // TB_HITTEST returns m_nButtons ( not -1 )
1209 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1210 // it's a separator or there is no tool at all there
1211 return (wxToolBarToolBase
*)NULL
;
1213 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1214 // we don't use the dummy separators hack
1215 #ifdef TB_SETBUTTONINFO
1216 if ( wxApp::GetComCtl32Version() >= 471 )
1218 return m_tools
.Item((size_t)index
)->GetData();
1221 #endif // TB_SETBUTTONINFO
1223 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1227 void wxToolBar::UpdateSize()
1229 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1231 // In case Realize is called after the initial display (IOW the programmer
1232 // may have rebuilt the toolbar) give the frame the option of resizing the
1233 // toolbar to full width again, but only if the parent is a frame and the
1234 // toolbar is managed by the frame. Otherwise assume that some other
1235 // layout mechanism is controlling the toolbar size and leave it alone.
1236 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1237 if ( frame
&& frame
->GetToolBar() == this )
1239 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 RECT rect
= wxGetClientRect(GetHwnd());
1344 HDC hdc
= GetHdcOf((*event
.GetDC()));
1348 // do draw our background
1350 // notice that this 'dumb' implementation may cause flicker for some of
1351 // the controls in which case they should intercept wxEraseEvent and
1352 // process it themselves somehow
1353 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1355 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1356 ::FillRect(hdc
, &rect
, hBrush
);
1358 else // we have no non default background colour
1361 // we may need to draw themed colour so that we appear correctly on
1362 // e.g. notebook page under XP with themes but only do it if the parent
1363 // draws themed background itself
1364 if ( !GetParent()->UseBgCol() )
1366 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1370 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1374 // it can also return S_FALSE which seems to simply say that it
1375 // didn't draw anything but no error really occurred
1377 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1380 #endif // wxUSE_UXTHEME
1387 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1389 // calculate our minor dimension ourselves - we're confusing the standard
1390 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1392 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1396 if ( GetWindowStyle() & wxTB_VERTICAL
)
1398 w
= r
.right
- r
.left
;
1401 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1408 if (HasFlag( wxTB_FLAT
))
1409 h
= r
.bottom
- r
.top
- 3;
1411 h
= r
.bottom
- r
.top
;
1414 // FIXME: hardcoded separator line height...
1415 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1420 if ( MAKELPARAM(w
, h
) != lParam
)
1422 // size really changed
1426 // message processed
1433 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1435 // erase any dummy separators which were used
1436 // for aligning the controls if any here
1438 // first of all, are there any controls at all?
1439 wxToolBarToolsList::compatibility_iterator node
;
1440 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1442 if ( node
->GetData()->IsControl() )
1447 // no controls, nothing to erase
1450 // prepare the DC on which we'll be drawing
1451 wxClientDC
dc(this);
1452 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1453 dc
.SetPen(*wxTRANSPARENT_PEN
);
1456 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1457 // nothing to redraw anyhow
1461 wxCopyRECTToRect(r
, rectUpdate
);
1463 dc
.SetClippingRegion(rectUpdate
);
1465 // draw the toolbar tools, separators &c normally
1466 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1468 // for each control in the toolbar find all the separators intersecting it
1471 // NB: this is really the only way to do it as we don't know if a separator
1472 // corresponds to a control (i.e. is a dummy one) or a real one
1474 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1476 wxToolBarToolBase
*tool
= node
->GetData();
1477 if ( tool
->IsControl() )
1479 // get the control rect in our client coords
1480 wxControl
*control
= tool
->GetControl();
1481 wxRect rectCtrl
= control
->GetRect();
1483 // iterate over all buttons
1485 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1486 for ( int n
= 0; n
< count
; n
++ )
1488 // is it a separator?
1489 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1492 wxLogDebug(_T("TB_GETBUTTON failed?"));
1497 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1500 // get the bounding rect of the separator
1502 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1505 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1510 // does it intersect the control?
1512 wxCopyRECTToRect(r
, rectItem
);
1513 if ( rectCtrl
.Intersects(rectItem
) )
1515 // yes, do erase it!
1516 dc
.DrawRectangle(rectItem
);
1518 // Necessary in case we use a no-paint-on-size
1519 // style in the parent: the controls can disappear
1520 control
->Refresh(false);
1529 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1531 wxCoord x
= GET_X_LPARAM(lParam
),
1532 y
= GET_Y_LPARAM(lParam
);
1533 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1535 // cursor left current tool
1536 if ( tool
!= m_pInTool
&& !tool
)
1542 // cursor entered a tool
1543 if ( tool
!= m_pInTool
&& tool
)
1546 OnMouseEnter( tool
->GetId() );
1550 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1555 // we don't handle mouse moves, so always pass the message to
1556 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1557 HandleMouseMove(wParam
, lParam
);
1561 if ( HandleSize(wParam
, lParam
) )
1567 if ( HandlePaint(wParam
, lParam
) )
1575 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1578 // ----------------------------------------------------------------------------
1579 // private functions
1580 // ----------------------------------------------------------------------------
1582 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1588 wxLogLastError(_T("CreateCompatibleDC"));
1593 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1597 wxLogLastError(_T("SelectObject"));
1602 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1604 for ( int i
= 0; i
< width
; i
++ )
1606 for ( int j
= 0; j
< height
; j
++ )
1608 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1610 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1612 COLORREF col
= cmap
[k
].from
;
1613 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1614 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1615 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1617 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1626 // VZ: I leave here my attempts to map the bitmap to the system colours
1627 // faster by using BitBlt() even though it's broken currently - but
1628 // maybe someone else can finish it? It should be faster than iterating
1629 // over all pixels...
1631 MemoryHDC hdcMask
, hdcDst
;
1632 if ( !hdcMask
|| !hdcDst
)
1634 wxLogLastError(_T("CreateCompatibleDC"));
1639 // create the target bitmap
1640 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1643 wxLogLastError(_T("CreateCompatibleBitmap"));
1648 // create the monochrome mask bitmap
1649 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1652 wxLogLastError(_T("CreateBitmap(mono)"));
1654 ::DeleteObject(hbmpDst
);
1659 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1660 bmpInMask(hdcMask
, hbmpMask
);
1663 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1665 // create the mask for this colour
1666 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1667 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1669 // replace this colour with the target one in the dst bitmap
1670 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1671 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1673 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1676 MAKEROP4(PATCOPY
, SRCCOPY
));
1678 (void)::SelectObject(hdcDst
, hbrOld
);
1679 ::DeleteObject(hbr
);
1682 ::DeleteObject((HBITMAP
)bitmap
);
1684 return (WXHBITMAP
)hbmpDst
;
1688 #endif // wxUSE_TOOLBAR