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
;
458 tool
->GetControl()->Destroy();
461 // do delete all buttons
462 m_nButtons
-= nButtonsToDelete
;
463 while ( nButtonsToDelete
-- > 0 )
465 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
467 wxLogLastError(wxT("TB_DELETEBUTTON"));
475 // and finally reposition all the controls after this button (the toolbar
476 // takes care of all normal items)
477 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
479 wxToolBarToolBase
*tool2
= node
->GetData();
480 if ( tool2
->IsControl() )
483 wxControl
*control
= tool2
->GetControl();
484 control
->GetPosition(&x
, NULL
);
485 control
->Move(x
- width
, wxDefaultCoord
);
489 InvalidateBestSize();
494 void wxToolBar::CreateDisabledImageList()
496 if (m_disabledImgList
!= NULL
)
498 delete m_disabledImgList
;
499 m_disabledImgList
= NULL
;
502 // as we can't use disabled image list with older versions of comctl32.dll,
503 // don't even bother creating it
504 if ( wxTheApp
->GetComCtl32Version() >= 470 )
506 // search for the first disabled button img in the toolbar, if any
507 for ( wxToolBarToolsList::compatibility_iterator
508 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
510 wxToolBarToolBase
*tool
= node
->GetData();
511 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
512 if ( bmpDisabled
.Ok() )
514 m_disabledImgList
= new wxImageList
518 bmpDisabled
.GetMask() != NULL
,
525 // we don't have any disabled bitmaps
529 bool wxToolBar::Realize()
531 const size_t nTools
= GetToolsCount();
536 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
538 bool doRemap
, doRemapBg
, doTransparent
;
539 doRemapBg
= doRemap
= doTransparent
= false;
542 int remapValue
= (-1);
543 const wxChar
*remapOptionStr
= wxT("msw.remap");
544 if (wxSystemOptions::HasOption( remapOptionStr
))
545 remapValue
= wxSystemOptions::GetOptionInt( remapOptionStr
);
547 doTransparent
= (remapValue
== 2);
550 doRemap
= (remapValue
!= 0);
551 doRemapBg
= !doRemap
;
555 // delete all old buttons, if any
556 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
558 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
560 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
564 // First, add the bitmap: we use one bitmap for all toolbar buttons
565 // ----------------------------------------------------------------
567 wxToolBarToolsList::compatibility_iterator node
;
571 if ( HasFlag(wxTB_NOICONS
) )
573 // no icons, don't leave space for them
577 else // do show icons
579 // if we already have a bitmap, we'll replace the existing one --
580 // otherwise we'll install a new one
581 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
583 sizeBmp
.x
= m_defaultWidth
;
584 sizeBmp
.y
= m_defaultHeight
;
586 const wxCoord totalBitmapWidth
= m_defaultWidth
*
587 wx_truncate_cast(wxCoord
, nTools
),
588 totalBitmapHeight
= m_defaultHeight
;
590 // Create a bitmap and copy all the tool bitmaps into it
591 wxMemoryDC dcAllButtons
;
592 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
593 dcAllButtons
.SelectObject(bitmap
);
597 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
599 dcAllButtons
.SetBackground(wxBrush(GetBackgroundColour()));
601 dcAllButtons
.SetBackground(wxBrush(wxColour(192,192,192)));
603 dcAllButtons
.Clear();
605 m_hBitmap
= bitmap
.GetHBITMAP();
606 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
611 dcAllButtons
.SelectObject(wxNullBitmap
);
613 // Even if we're not remapping the bitmap
614 // content, we still have to remap the background.
615 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
616 totalBitmapWidth
, totalBitmapHeight
);
618 dcAllButtons
.SelectObject(bitmap
);
620 #endif // !__WXWINCE__
622 // the button position
625 // the number of buttons (not separators)
628 CreateDisabledImageList();
629 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
631 wxToolBarToolBase
*tool
= node
->GetData();
632 if ( tool
->IsButton() )
634 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
636 const int w
= bmp
.GetWidth();
637 const int h
= bmp
.GetHeight();
641 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
642 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
644 // notice the last parameter: do use mask
645 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
649 wxFAIL_MSG( _T("invalid tool button bitmap") );
652 // also deal with disabled bitmap if we want to use them
653 if ( m_disabledImgList
)
655 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
656 #if wxUSE_IMAGE && wxUSE_WXDIB
657 if ( !bmpDisabled
.Ok() )
659 // no disabled bitmap specified but we still need to
660 // fill the space in the image list with something, so
661 // we grey out the normal bitmap
663 wxCreateGreyedImage(bmp
.ConvertToImage(), imgGreyed
);
667 // we need to have light grey background colour for
668 // MapBitmap() to work correctly
669 for ( int y
= 0; y
< h
; y
++ )
671 for ( int x
= 0; x
< w
; x
++ )
673 if ( imgGreyed
.IsTransparent(x
, y
) )
674 imgGreyed
.SetRGB(x
, y
,
676 wxLIGHT_GREY
->Green(),
677 wxLIGHT_GREY
->Blue());
682 bmpDisabled
= wxBitmap(imgGreyed
);
684 #endif // wxUSE_IMAGE
687 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
689 m_disabledImgList
->Add(bmpDisabled
);
692 // still inc width and number of buttons because otherwise the
693 // subsequent buttons will all be shifted which is rather confusing
694 // (and like this you'd see immediately which bitmap was bad)
700 dcAllButtons
.SelectObject(wxNullBitmap
);
702 // don't delete this HBITMAP!
703 bitmap
.SetHBITMAP(0);
707 // Map to system colours
708 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
709 totalBitmapWidth
, totalBitmapHeight
);
712 bool addBitmap
= true;
714 if ( oldToolBarBitmap
)
716 #ifdef TB_REPLACEBITMAP
717 if ( wxApp::GetComCtl32Version() >= 400 )
719 TBREPLACEBITMAP replaceBitmap
;
720 replaceBitmap
.hInstOld
= NULL
;
721 replaceBitmap
.hInstNew
= NULL
;
722 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
723 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
724 replaceBitmap
.nButtons
= nButtons
;
725 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
726 0, (LPARAM
) &replaceBitmap
) )
728 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
731 ::DeleteObject(oldToolBarBitmap
);
737 #endif // TB_REPLACEBITMAP
739 // we can't replace the old bitmap, so we will add another one
740 // (awfully inefficient, but what else to do?) and shift the bitmap
741 // indices accordingly
744 bitmapId
= m_nButtons
;
748 if ( addBitmap
) // no old bitmap or we can't replace it
750 TBADDBITMAP addBitmap
;
752 addBitmap
.nID
= (UINT
) hBitmap
;
753 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
754 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
756 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
760 if ( m_disabledImgList
)
762 HIMAGELIST oldImageList
= (HIMAGELIST
)
763 ::SendMessage(GetHwnd(),
764 TB_SETDISABLEDIMAGELIST
,
766 (LPARAM
)GetHimagelistOf(m_disabledImgList
));
768 // delete previous image list if any
770 ::DeleteObject( oldImageList
);
774 // don't call SetToolBitmapSize() as we don't want to change the values of
775 // m_defaultWidth/Height
776 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
777 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
779 wxLogLastError(_T("TB_SETBITMAPSIZE"));
782 // Next add the buttons and separators
783 // -----------------------------------
785 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
787 // this array will hold the indices of all controls in the toolbar
788 wxArrayInt controlIds
;
790 bool lastWasRadio
= false;
792 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
794 wxToolBarToolBase
*tool
= node
->GetData();
796 // don't add separators to the vertical toolbar with old comctl32.dll
797 // versions as they didn't handle this properly
798 if ( isVertical
&& tool
->IsSeparator() &&
799 wxApp::GetComCtl32Version() <= 472 )
804 TBBUTTON
& button
= buttons
[i
];
806 wxZeroMemory(button
);
808 bool isRadio
= false;
809 switch ( tool
->GetStyle() )
811 case wxTOOL_STYLE_CONTROL
:
812 button
.idCommand
= tool
->GetId();
813 // fall through: create just a separator too
815 case wxTOOL_STYLE_SEPARATOR
:
816 button
.fsState
= TBSTATE_ENABLED
;
817 button
.fsStyle
= TBSTYLE_SEP
;
820 case wxTOOL_STYLE_BUTTON
:
821 if ( !HasFlag(wxTB_NOICONS
) )
822 button
.iBitmap
= bitmapId
;
824 if ( HasFlag(wxTB_TEXT
) )
826 const wxString
& label
= tool
->GetLabel();
827 if ( !label
.empty() )
828 button
.iString
= (int)label
.c_str();
831 button
.idCommand
= tool
->GetId();
833 if ( tool
->IsEnabled() )
834 button
.fsState
|= TBSTATE_ENABLED
;
835 if ( tool
->IsToggled() )
836 button
.fsState
|= TBSTATE_CHECKED
;
838 switch ( tool
->GetKind() )
841 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
845 // the first item in the radio group is checked by
846 // default to be consistent with wxGTK and the menu
848 button
.fsState
|= TBSTATE_CHECKED
;
850 if (tool
->Toggle(true))
852 DoToggleTool(tool
, true);
855 else if (tool
->IsToggled())
857 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
858 int prevIndex
= i
- 1;
861 TBBUTTON
& prevButton
= buttons
[prevIndex
];
862 wxToolBarToolBase
*tool
= nodePrev
->GetData();
863 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
866 if ( tool
->Toggle(false) )
867 DoToggleTool(tool
, false);
869 prevButton
.fsState
= TBSTATE_ENABLED
;
870 nodePrev
= nodePrev
->GetPrevious();
879 button
.fsStyle
= TBSTYLE_CHECK
;
883 button
.fsStyle
= TBSTYLE_BUTTON
;
887 wxFAIL_MSG( _T("unexpected toolbar button kind") );
888 button
.fsStyle
= TBSTYLE_BUTTON
;
896 lastWasRadio
= isRadio
;
901 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
903 wxLogLastError(wxT("TB_ADDBUTTONS"));
908 // Deal with the controls finally
909 // ------------------------------
911 // adjust the controls size to fit nicely in the toolbar
914 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
916 wxToolBarToolBase
*tool
= node
->GetData();
918 // we calculate the running y coord for vertical toolbars so we need to
919 // get the items size for all items but for the horizontal ones we
920 // don't need to deal with the non controls
921 bool isControl
= tool
->IsControl();
922 if ( !isControl
&& !isVertical
)
925 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
926 // latter only appeared in v4.70 of comctl32.dll
928 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
929 index
, (LPARAM
)(LPRECT
)&r
) )
931 wxLogLastError(wxT("TB_GETITEMRECT"));
936 // can only be control if isVertical
937 y
+= r
.bottom
- r
.top
;
942 wxControl
*control
= tool
->GetControl();
943 wxSize size
= control
->GetSize();
945 // the position of the leftmost controls corner
946 int left
= wxDefaultCoord
;
948 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
949 #ifdef TB_SETBUTTONINFO
950 // available in headers, now check whether it is available now
952 if ( wxApp::GetComCtl32Version() >= 471 )
954 // set the (underlying) separators width to be that of the
957 tbbi
.cbSize
= sizeof(tbbi
);
958 tbbi
.dwMask
= TBIF_SIZE
;
959 tbbi
.cx
= (WORD
)size
.x
;
960 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
961 tool
->GetId(), (LPARAM
)&tbbi
) )
963 // the id is probably invalid?
964 wxLogLastError(wxT("TB_SETBUTTONINFO"));
968 #endif // comctl32.dll 4.71
969 // TB_SETBUTTONINFO unavailable
971 // try adding several separators to fit the controls width
972 int widthSep
= r
.right
- r
.left
;
978 tbb
.fsState
= TBSTATE_ENABLED
;
979 tbb
.fsStyle
= TBSTYLE_SEP
;
981 size_t nSeparators
= size
.x
/ widthSep
;
982 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
984 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
985 index
, (LPARAM
)&tbb
) )
987 wxLogLastError(wxT("TB_INSERTBUTTON"));
993 // remember the number of separators we used - we'd have to
994 // delete all of them later
995 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
997 // adjust the controls width to exactly cover the separators
998 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
1001 // position the control itself correctly vertically
1002 int height
= r
.bottom
- r
.top
;
1003 int diff
= height
- size
.y
;
1006 // the control is too high, resize to fit
1007 control
->SetSize(wxDefaultCoord
, height
- 2);
1018 y
+= height
+ 2 * GetMargins().y
;
1020 else // horizontal toolbar
1022 if ( left
== wxDefaultCoord
)
1028 control
->Move(left
, top
+ (diff
+ 1) / 2);
1031 // the max index is the "real" number of buttons - i.e. counting even the
1032 // separators which we added just for aligning the controls
1037 if ( m_maxRows
== 0 )
1038 // if not set yet, only one row
1041 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1043 if ( m_maxRows
== 0 )
1044 // if not set yet, have one column
1045 SetRows(m_nButtons
);
1048 InvalidateBestSize();
1054 // ----------------------------------------------------------------------------
1056 // ----------------------------------------------------------------------------
1058 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
1060 wxToolBarToolBase
*tool
= FindById((int)id
);
1064 bool toggled
= false; // just to suppress warnings
1066 if ( tool
->CanBeToggled() )
1068 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1069 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1071 // ignore the event when a radio button is released, as this doesn't
1072 // seem to happen at all, and is handled otherwise
1073 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1076 tool
->Toggle(toggled
);
1077 UnToggleRadioGroup(tool
);
1080 // OnLeftClick() can veto the button state change - for buttons which
1081 // may be toggled only, of couse
1082 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1085 tool
->Toggle(!toggled
);
1087 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1093 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1095 WXLPARAM
*WXUNUSED(result
))
1098 // First check if this applies to us
1099 NMHDR
*hdr
= (NMHDR
*)lParam
;
1101 // the tooltips control created by the toolbar is sometimes Unicode, even
1102 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1103 UINT code
= hdr
->code
;
1104 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1107 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1108 if ( toolTipWnd
!= hdr
->hwndFrom
)
1111 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1112 int id
= (int)ttText
->hdr
.idFrom
;
1114 wxToolBarToolBase
*tool
= FindById(id
);
1118 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1120 wxUnusedVar(lParam
);
1126 // ----------------------------------------------------------------------------
1128 // ----------------------------------------------------------------------------
1130 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1132 wxToolBarBase::SetToolBitmapSize(size
);
1134 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1137 void wxToolBar::SetRows(int nRows
)
1139 if ( nRows
== m_maxRows
)
1141 // avoid resizing the frame uselessly
1145 // TRUE in wParam means to create at least as many rows, FALSE -
1148 ::SendMessage(GetHwnd(), TB_SETROWS
,
1149 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1157 // The button size is bigger than the bitmap size
1158 wxSize
wxToolBar::GetToolSize() const
1160 // TB_GETBUTTONSIZE is supported from version 4.70
1161 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1162 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1163 && !defined (__DIGITALMARS__)
1164 if ( wxApp::GetComCtl32Version() >= 470 )
1166 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1168 return wxSize(LOWORD(dw
), HIWORD(dw
));
1171 #endif // comctl32.dll 4.70+
1174 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1179 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1182 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1184 for ( ; current
; current
= current
->GetNext() )
1187 return current
->GetData();
1189 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1190 size_t separators
= tool
->GetSeparatorsCount();
1192 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1193 // otherwise, skip as many items as the separator count, plus the
1195 index
-= separators
? separators
+ 1 : 1;
1201 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1206 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1208 // MBN: when the point ( x, y ) is close to the toolbar border
1209 // TB_HITTEST returns m_nButtons ( not -1 )
1210 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1211 // it's a separator or there is no tool at all there
1212 return (wxToolBarToolBase
*)NULL
;
1214 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1215 // we don't use the dummy separators hack
1216 #ifdef TB_SETBUTTONINFO
1217 if ( wxApp::GetComCtl32Version() >= 471 )
1219 return m_tools
.Item((size_t)index
)->GetData();
1222 #endif // TB_SETBUTTONINFO
1224 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1228 void wxToolBar::UpdateSize()
1230 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1232 // In case Realize is called after the initial display (IOW the programmer
1233 // may have rebuilt the toolbar) give the frame the option of resizing the
1234 // toolbar to full width again, but only if the parent is a frame and the
1235 // toolbar is managed by the frame. Otherwise assume that some other
1236 // layout mechanism is controlling the toolbar size and leave it alone.
1237 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1238 if ( frame
&& frame
->GetToolBar() == this )
1240 frame
->SendSizeEvent();
1244 // ----------------------------------------------------------------------------
1246 // ---------------------------------------------------------------------------
1248 void wxToolBar::SetWindowStyleFlag(long style
)
1250 // the style bits whose changes force us to recreate the toolbar
1251 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1253 const long styleOld
= GetWindowStyle();
1255 wxToolBarBase::SetWindowStyleFlag(style
);
1257 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1258 // also fatal as we'd then try to recreate the toolbar when it's just being
1260 if ( GetToolsCount() &&
1261 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1263 // to remove the text labels, simply re-realizing the toolbar is enough
1264 // but I don't know of any way to add the text to an existing toolbar
1265 // other than by recreating it entirely
1270 // ----------------------------------------------------------------------------
1272 // ----------------------------------------------------------------------------
1274 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1276 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1277 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1280 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1282 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1283 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1286 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1288 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1289 // without, so we really need to delete the button and recreate it here
1290 wxFAIL_MSG( _T("not implemented") );
1293 // ----------------------------------------------------------------------------
1295 // ----------------------------------------------------------------------------
1297 // Responds to colour changes, and passes event on to children.
1298 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1300 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1302 // Remap the buttons
1305 // Relayout the toolbar
1306 int nrows
= m_maxRows
;
1307 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1312 // let the event propagate further
1316 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1318 if (event
.Leaving() && m_pInTool
)
1325 if ( event
.RightDown() )
1327 // find the tool under the mouse
1328 wxCoord x
= 0, y
= 0;
1329 event
.GetPosition(&x
, &y
);
1331 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1332 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1340 // This handler is required to allow the toolbar to be set to a non-default
1341 // colour: for example, when it must blend in with a notebook page.
1342 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1344 RECT rect
= wxGetClientRect(GetHwnd());
1345 HDC hdc
= GetHdcOf((*event
.GetDC()));
1349 // do draw our background
1351 // notice that this 'dumb' implementation may cause flicker for some of
1352 // the controls in which case they should intercept wxEraseEvent and
1353 // process it themselves somehow
1354 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1356 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1357 ::FillRect(hdc
, &rect
, hBrush
);
1359 else // we have no non default background colour
1362 // we may need to draw themed colour so that we appear correctly on
1363 // e.g. notebook page under XP with themes but only do it if the parent
1364 // draws themed background itself
1365 if ( !GetParent()->UseBgCol() )
1367 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1371 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1375 // it can also return S_FALSE which seems to simply say that it
1376 // didn't draw anything but no error really occurred
1378 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1381 #endif // wxUSE_UXTHEME
1388 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1390 // calculate our minor dimension ourselves - we're confusing the standard
1391 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1393 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1397 if ( GetWindowStyle() & wxTB_VERTICAL
)
1399 w
= r
.right
- r
.left
;
1402 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1409 if (HasFlag( wxTB_FLAT
))
1410 h
= r
.bottom
- r
.top
- 3;
1412 h
= r
.bottom
- r
.top
;
1415 // FIXME: hardcoded separator line height...
1416 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1421 if ( MAKELPARAM(w
, h
) != lParam
)
1423 // size really changed
1427 // message processed
1434 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1436 // erase any dummy separators which were used
1437 // for aligning the controls if any here
1439 // first of all, are there any controls at all?
1440 wxToolBarToolsList::compatibility_iterator node
;
1441 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1443 if ( node
->GetData()->IsControl() )
1448 // no controls, nothing to erase
1451 // prepare the DC on which we'll be drawing
1452 wxClientDC
dc(this);
1453 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1454 dc
.SetPen(*wxTRANSPARENT_PEN
);
1457 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1458 // nothing to redraw anyhow
1462 wxCopyRECTToRect(r
, rectUpdate
);
1464 dc
.SetClippingRegion(rectUpdate
);
1466 // draw the toolbar tools, separators &c normally
1467 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1469 // for each control in the toolbar find all the separators intersecting it
1472 // NB: this is really the only way to do it as we don't know if a separator
1473 // corresponds to a control (i.e. is a dummy one) or a real one
1475 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1477 wxToolBarToolBase
*tool
= node
->GetData();
1478 if ( tool
->IsControl() )
1480 // get the control rect in our client coords
1481 wxControl
*control
= tool
->GetControl();
1482 wxRect rectCtrl
= control
->GetRect();
1484 // iterate over all buttons
1486 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1487 for ( int n
= 0; n
< count
; n
++ )
1489 // is it a separator?
1490 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1493 wxLogDebug(_T("TB_GETBUTTON failed?"));
1498 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1501 // get the bounding rect of the separator
1503 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1506 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1511 // does it intersect the control?
1513 wxCopyRECTToRect(r
, rectItem
);
1514 if ( rectCtrl
.Intersects(rectItem
) )
1516 // yes, do erase it!
1517 dc
.DrawRectangle(rectItem
);
1519 // Necessary in case we use a no-paint-on-size
1520 // style in the parent: the controls can disappear
1521 control
->Refresh(false);
1530 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1532 wxCoord x
= GET_X_LPARAM(lParam
),
1533 y
= GET_Y_LPARAM(lParam
);
1534 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1536 // cursor left current tool
1537 if ( tool
!= m_pInTool
&& !tool
)
1543 // cursor entered a tool
1544 if ( tool
!= m_pInTool
&& tool
)
1547 OnMouseEnter( tool
->GetId() );
1551 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1556 // we don't handle mouse moves, so always pass the message to
1557 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1558 HandleMouseMove(wParam
, lParam
);
1562 if ( HandleSize(wParam
, lParam
) )
1568 if ( HandlePaint(wParam
, lParam
) )
1576 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1579 // ----------------------------------------------------------------------------
1580 // private functions
1581 // ----------------------------------------------------------------------------
1583 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1589 wxLogLastError(_T("CreateCompatibleDC"));
1594 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1598 wxLogLastError(_T("SelectObject"));
1603 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1605 for ( int i
= 0; i
< width
; i
++ )
1607 for ( int j
= 0; j
< height
; j
++ )
1609 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1611 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1613 COLORREF col
= cmap
[k
].from
;
1614 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1615 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1616 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1618 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1627 // VZ: I leave here my attempts to map the bitmap to the system colours
1628 // faster by using BitBlt() even though it's broken currently - but
1629 // maybe someone else can finish it? It should be faster than iterating
1630 // over all pixels...
1632 MemoryHDC hdcMask
, hdcDst
;
1633 if ( !hdcMask
|| !hdcDst
)
1635 wxLogLastError(_T("CreateCompatibleDC"));
1640 // create the target bitmap
1641 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1644 wxLogLastError(_T("CreateCompatibleBitmap"));
1649 // create the monochrome mask bitmap
1650 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1653 wxLogLastError(_T("CreateBitmap(mono)"));
1655 ::DeleteObject(hbmpDst
);
1660 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1661 bmpInMask(hdcMask
, hbmpMask
);
1664 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1666 // create the mask for this colour
1667 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1668 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1670 // replace this colour with the target one in the dst bitmap
1671 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1672 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1674 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1677 MAKEROP4(PATCOPY
, SRCCOPY
));
1679 (void)::SelectObject(hdcDst
, hbrOld
);
1680 ::DeleteObject(hbr
);
1683 ::DeleteObject((HBITMAP
)bitmap
);
1685 return (WXHBITMAP
)hbmpDst
;
1689 #endif // wxUSE_TOOLBAR