1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "tbar95.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dynarray.h"
36 #include "wx/settings.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
39 #include "wx/control.h"
42 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (!defined(_WIN32_WCE) || (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)))
44 #include "wx/toolbar.h"
45 #include "wx/sysopt.h"
47 #include "wx/msw/private.h"
50 #include "wx/msw/uxtheme.h"
53 // include <commctrl.h> "properly"
54 #include "wx/msw/wrapcctl.h"
56 #include "wx/app.h" // for GetComCtl32Version
58 // ----------------------------------------------------------------------------
59 // conditional compilation
60 // ----------------------------------------------------------------------------
62 // wxWidgets previously always considered that toolbar buttons have light grey
63 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
64 // doesn't work with XPMs which then appear to have black background. To make
65 // this work, we must respect the bitmap masks - which we do now. This should
66 // be ok in any case, but to restore 100% compatible with the old version
67 // behaviour, you can set this to 0.
68 #define USE_BITMAP_MASKS 1
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // these standard constants are not always defined in compilers headers
78 #define TBSTYLE_LIST 0x1000
79 #define TBSTYLE_FLAT 0x0800
82 #ifndef TBSTYLE_TRANSPARENT
83 #define TBSTYLE_TRANSPARENT 0x8000
86 #ifndef TBSTYLE_TOOLTIPS
87 #define TBSTYLE_TOOLTIPS 0x0100
92 #define TB_SETSTYLE (WM_USER + 56)
93 #define TB_GETSTYLE (WM_USER + 57)
97 #define TB_HITTEST (WM_USER + 69)
100 // these values correspond to those used by comctl32.dll
101 #define DEFAULTBITMAPX 16
102 #define DEFAULTBITMAPY 15
103 #define DEFAULTBUTTONX 24
104 #define DEFAULTBUTTONY 24
105 #define DEFAULTBARHEIGHT 27
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
123 style ( wxNO_BORDER | wxTB_HORIZONTAL)
132 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
133 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
134 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 class wxToolBarTool
: public wxToolBarToolBase
144 wxToolBarTool(wxToolBar
*tbar
,
146 const wxString
& label
,
147 const wxBitmap
& bmpNormal
,
148 const wxBitmap
& bmpDisabled
,
150 wxObject
*clientData
,
151 const wxString
& shortHelp
,
152 const wxString
& longHelp
)
153 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
154 clientData
, shortHelp
, longHelp
)
159 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
160 : wxToolBarToolBase(tbar
, control
)
165 virtual void SetLabel(const wxString
& label
)
167 if ( label
== m_label
)
170 wxToolBarToolBase::SetLabel(label
);
172 // we need to update the label shown in the toolbar because it has a
173 // pointer to the internal buffer of the old label
175 // TODO: use TB_SETBUTTONINFO
178 // set/get the number of separators which we use to cover the space used by
179 // a control in the toolbar
180 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
181 size_t GetSeparatorsCount() const { return m_nSepCount
; }
186 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
190 // ============================================================================
192 // ============================================================================
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
199 const wxString
& label
,
200 const wxBitmap
& bmpNormal
,
201 const wxBitmap
& bmpDisabled
,
203 wxObject
*clientData
,
204 const wxString
& shortHelp
,
205 const wxString
& longHelp
)
207 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
208 clientData
, shortHelp
, longHelp
);
211 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
213 return new wxToolBarTool(this, control
);
216 // ----------------------------------------------------------------------------
217 // wxToolBar construction
218 // ----------------------------------------------------------------------------
220 void wxToolBar::Init()
226 m_defaultWidth
= DEFAULTBITMAPX
;
227 m_defaultHeight
= DEFAULTBITMAPY
;
232 bool wxToolBar::Create(wxWindow
*parent
,
237 const wxString
& name
)
239 // common initialisation
240 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
243 // MSW-specific initialisation
244 if ( !MSWCreateToolbar(pos
, size
) )
247 wxSetCCUnicodeFormat(GetHwnd());
249 // set up the colors and fonts
250 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
251 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
253 // workaround for flat toolbar on Windows XP classic style
255 if ( style
& wxTB_FLAT
)
257 wxUxThemeEngine
*p
= wxUxThemeEngine::Get();
258 if ( !p
|| !p
->IsThemeActive() )
260 DWORD dwToolbarStyle
;
262 dwToolbarStyle
= (DWORD
)::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L );
264 if ((dwToolbarStyle
& TBSTYLE_FLAT
) == 0)
266 dwToolbarStyle
|= TBSTYLE_FLAT
;
267 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, (LPARAM
)dwToolbarStyle
);
276 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
278 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
281 // toolbar-specific post initialisation
282 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
287 void wxToolBar::Recreate()
289 const HWND hwndOld
= GetHwnd();
292 // we haven't been created yet, no need to recreate
296 // get the position and size before unsubclassing the old toolbar
297 const wxPoint pos
= GetPosition();
298 const wxSize size
= GetSize();
302 if ( !MSWCreateToolbar(pos
, size
) )
305 wxFAIL_MSG( _T("recreating the toolbar failed") );
310 // reparent all our children under the new toolbar
311 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
313 node
= node
->GetNext() )
315 wxWindow
*win
= node
->GetData();
316 if ( !win
->IsTopLevel() )
317 ::SetParent(GetHwndOf(win
), GetHwnd());
320 // only destroy the old toolbar now -- after all the children had been
322 ::DestroyWindow(hwndOld
);
324 // it is for the old bitmap control and can't be used with the new one
327 ::DeleteObject((HBITMAP
) m_hBitmap
);
335 wxToolBar::~wxToolBar()
337 // we must refresh the frame size when the toolbar is deleted but the frame
338 // is not - otherwise toolbar leaves a hole in the place it used to occupy
339 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
340 if ( frame
&& !frame
->IsBeingDeleted() )
342 frame
->SendSizeEvent();
347 ::DeleteObject((HBITMAP
) m_hBitmap
);
351 wxSize
wxToolBar::DoGetBestSize() const
353 wxSize sizeBest
= GetToolSize();
354 sizeBest
.x
*= GetToolsCount();
356 // reverse horz and vertical components if necessary
357 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
360 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
362 // toolbars never have border, giving one to them results in broken
364 WXDWORD msStyle
= wxControl::MSWGetStyle
366 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
369 // always include this one, it never hurts and setting it later only if we
370 // do have tooltips wouldn't work
371 msStyle
|= TBSTYLE_TOOLTIPS
;
373 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
375 // static as it doesn't change during the program lifetime
376 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
378 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
379 // style with 6.00 (part of Windows XP) leads to the toolbar with
380 // incorrect background colour - and not using it still results in the
381 // correct (flat) toolbar, so don't use it there
382 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
384 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
387 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
389 msStyle
|= TBSTYLE_LIST
;
393 if ( style
& wxTB_NODIVIDER
)
394 msStyle
|= CCS_NODIVIDER
;
396 if ( style
& wxTB_NOALIGN
)
397 msStyle
|= CCS_NOPARENTALIGN
;
399 if ( style
& wxTB_VERTICAL
)
405 // ----------------------------------------------------------------------------
406 // adding/removing tools
407 // ----------------------------------------------------------------------------
409 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
411 // nothing special to do here - we really create the toolbar buttons in
415 InvalidateBestSize();
419 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
421 // the main difficulty we have here is with the controls in the toolbars:
422 // as we (sometimes) use several separators to cover up the space used by
423 // them, the indices are not the same for us and the toolbar
425 // first determine the position of the first button to delete: it may be
426 // different from pos if we use several separators to cover the space used
428 wxToolBarToolsList::compatibility_iterator node
;
429 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
431 wxToolBarToolBase
*tool2
= node
->GetData();
434 // let node point to the next node in the list
435 node
= node
->GetNext();
440 if ( tool2
->IsControl() )
442 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();
462 width
*= nButtonsToDelete
;
465 // do delete all buttons
466 m_nButtons
-= nButtonsToDelete
;
467 while ( nButtonsToDelete
-- > 0 )
469 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
471 wxLogLastError(wxT("TB_DELETEBUTTON"));
479 // and finally reposition all the controls after this button (the toolbar
480 // takes care of all normal items)
481 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
483 wxToolBarToolBase
*tool2
= node
->GetData();
484 if ( tool2
->IsControl() )
487 wxControl
*control
= tool2
->GetControl();
488 control
->GetPosition(&x
, NULL
);
489 control
->Move(x
- width
, wxDefaultCoord
);
493 InvalidateBestSize();
497 bool wxToolBar::Realize()
499 const size_t nTools
= GetToolsCount();
506 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
508 bool doRemap
, doRemapBg
, doTransparent
;
509 if (wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 2)
511 doRemapBg
= doRemap
= false;
512 doTransparent
= true;
515 { doRemap
= !wxSystemOptions::HasOption(wxT("msw.remap"))
516 || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1;
517 doRemapBg
= !doRemap
;
518 doTransparent
= false;
521 // delete all old buttons, if any
522 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
524 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
526 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
530 // First, add the bitmap: we use one bitmap for all toolbar buttons
531 // ----------------------------------------------------------------
533 wxToolBarToolsList::compatibility_iterator node
;
537 if ( HasFlag(wxTB_NOICONS
) )
539 // no icons, don't leave space for them
543 else // do show icons
545 // if we already have a bitmap, we'll replace the existing one --
546 // otherwise we'll install a new one
547 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
549 sizeBmp
.x
= m_defaultWidth
;
550 sizeBmp
.y
= m_defaultHeight
;
552 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
553 totalBitmapHeight
= m_defaultHeight
;
555 // Create a bitmap and copy all the tool bitmaps to it
557 wxMemoryDC dcAllButtons
;
558 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
559 dcAllButtons
.SelectObject(bitmap
);
561 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
563 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
564 dcAllButtons
.Clear();
566 m_hBitmap
= bitmap
.GetHBITMAP();
567 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
568 #else // !USE_BITMAP_MASKS
569 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
574 wxLogLastError(_T("CreateCompatibleBitmap"));
579 m_hBitmap
= (WXHBITMAP
)hBitmap
;
582 SelectInHDC
hdcSelector(memoryDC
, hBitmap
);
585 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
590 dcAllButtons
.SelectObject(wxNullBitmap
);
593 // Even if we're not remapping the bitmap
594 // content, we still have to remap the background.
595 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
596 totalBitmapWidth
, totalBitmapHeight
);
599 dcAllButtons
.SelectObject(bitmap
);
603 // the button position
606 // the number of buttons (not separators)
609 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
611 wxToolBarToolBase
*tool
= node
->GetData();
612 if ( tool
->IsButton() )
614 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
617 int xOffset
= wxMax(0, (m_defaultWidth
- bmp
.GetWidth())/2);
618 int yOffset
= wxMax(0, (m_defaultHeight
- bmp
.GetHeight())/2);
620 // notice the last parameter: do use mask
621 dcAllButtons
.DrawBitmap(bmp
, x
+xOffset
, yOffset
, true);
622 #else // !USE_BITMAP_MASKS
623 SelectInHDC
hdcSelector2(memoryDC2
, GetHbitmapOf(bmp
));
624 if ( !BitBlt(memoryDC
,
625 x
+xOffset
, yOffset
, m_defaultWidth
, m_defaultHeight
,
629 wxLogLastError(wxT("BitBlt"));
631 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
635 wxFAIL_MSG( _T("invalid tool button bitmap") );
638 // still inc width and number of buttons because otherwise the
639 // subsequent buttons will all be shifted which is rather confusing
640 // (and like this you'd see immediately which bitmap was bad)
647 dcAllButtons
.SelectObject(wxNullBitmap
);
649 // don't delete this HBITMAP!
650 bitmap
.SetHBITMAP(0);
651 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
655 // Map to system colours
656 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
657 totalBitmapWidth
, totalBitmapHeight
);
660 bool addBitmap
= true;
662 if ( oldToolBarBitmap
)
664 #ifdef TB_REPLACEBITMAP
665 if ( wxTheApp
->GetComCtl32Version() >= 400 )
667 TBREPLACEBITMAP replaceBitmap
;
668 replaceBitmap
.hInstOld
= NULL
;
669 replaceBitmap
.hInstNew
= NULL
;
670 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
671 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
672 replaceBitmap
.nButtons
= nButtons
;
673 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
674 0, (LPARAM
) &replaceBitmap
) )
676 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
679 ::DeleteObject(oldToolBarBitmap
);
685 #endif // TB_REPLACEBITMAP
687 // we can't replace the old bitmap, so we will add another one
688 // (awfully inefficient, but what else to do?) and shift the bitmap
689 // indices accordingly
692 bitmapId
= m_nButtons
;
696 if ( addBitmap
) // no old bitmap or we can't replace it
698 TBADDBITMAP addBitmap
;
700 addBitmap
.nID
= (UINT
) hBitmap
;
701 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
702 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
704 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
709 // don't call SetToolBitmapSize() as we don't want to change the values of
710 // m_defaultWidth/Height
711 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
712 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
714 wxLogLastError(_T("TB_SETBITMAPSIZE"));
717 // Next add the buttons and separators
718 // -----------------------------------
720 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
722 // this array will hold the indices of all controls in the toolbar
723 wxArrayInt controlIds
;
725 bool lastWasRadio
= false;
727 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
729 wxToolBarToolBase
*tool
= node
->GetData();
731 // don't add separators to the vertical toolbar with old comctl32.dll
732 // versions as they didn't handle this properly
733 if ( isVertical
&& tool
->IsSeparator() &&
734 wxTheApp
->GetComCtl32Version() <= 472 )
740 TBBUTTON
& button
= buttons
[i
];
742 wxZeroMemory(button
);
744 bool isRadio
= false;
745 switch ( tool
->GetStyle() )
747 case wxTOOL_STYLE_CONTROL
:
748 button
.idCommand
= tool
->GetId();
749 // fall through: create just a separator too
751 case wxTOOL_STYLE_SEPARATOR
:
752 button
.fsState
= TBSTATE_ENABLED
;
753 button
.fsStyle
= TBSTYLE_SEP
;
756 case wxTOOL_STYLE_BUTTON
:
757 if ( !HasFlag(wxTB_NOICONS
) )
758 button
.iBitmap
= bitmapId
;
760 if ( HasFlag(wxTB_TEXT
) )
762 const wxString
& label
= tool
->GetLabel();
763 if ( !label
.empty() )
765 button
.iString
= (int)label
.c_str();
769 button
.idCommand
= tool
->GetId();
771 if ( tool
->IsEnabled() )
772 button
.fsState
|= TBSTATE_ENABLED
;
773 if ( tool
->IsToggled() )
774 button
.fsState
|= TBSTATE_CHECKED
;
776 switch ( tool
->GetKind() )
779 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
783 // the first item in the radio group is checked by
784 // default to be consistent with wxGTK and the menu
786 button
.fsState
|= TBSTATE_CHECKED
;
795 button
.fsStyle
= TBSTYLE_CHECK
;
799 wxFAIL_MSG( _T("unexpected toolbar button kind") );
803 button
.fsStyle
= TBSTYLE_BUTTON
;
810 lastWasRadio
= isRadio
;
815 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
817 wxLogLastError(wxT("TB_ADDBUTTONS"));
822 // Deal with the controls finally
823 // ------------------------------
825 // adjust the controls size to fit nicely in the toolbar
828 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
830 wxToolBarToolBase
*tool
= node
->GetData();
832 // we calculate the running y coord for vertical toolbars so we need to
833 // get the items size for all items but for the horizontal ones we
834 // don't need to deal with the non controls
835 bool isControl
= tool
->IsControl();
836 if ( !isControl
&& !isVertical
)
839 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
840 // latter only appeared in v4.70 of comctl32.dll
842 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
843 index
, (LPARAM
)(LPRECT
)&r
) )
845 wxLogLastError(wxT("TB_GETITEMRECT"));
850 // can only be control if isVertical
851 y
+= r
.bottom
- r
.top
;
856 wxControl
*control
= tool
->GetControl();
858 wxSize size
= control
->GetSize();
860 // the position of the leftmost controls corner
861 int left
= wxDefaultCoord
;
863 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
864 #ifdef TB_SETBUTTONINFO
865 // available in headers, now check whether it is available now
867 if ( wxTheApp
->GetComCtl32Version() >= 471 )
869 // set the (underlying) separators width to be that of the
872 tbbi
.cbSize
= sizeof(tbbi
);
873 tbbi
.dwMask
= TBIF_SIZE
;
875 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
876 tool
->GetId(), (LPARAM
)&tbbi
) )
878 // the id is probably invalid?
879 wxLogLastError(wxT("TB_SETBUTTONINFO"));
883 #endif // comctl32.dll 4.71
884 // TB_SETBUTTONINFO unavailable
886 // try adding several separators to fit the controls width
887 int widthSep
= r
.right
- r
.left
;
893 tbb
.fsState
= TBSTATE_ENABLED
;
894 tbb
.fsStyle
= TBSTYLE_SEP
;
896 size_t nSeparators
= size
.x
/ widthSep
;
897 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
899 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
900 index
, (LPARAM
)&tbb
) )
902 wxLogLastError(wxT("TB_INSERTBUTTON"));
908 // remember the number of separators we used - we'd have to
909 // delete all of them later
910 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
912 // adjust the controls width to exactly cover the separators
913 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
916 // position the control itself correctly vertically
917 int height
= r
.bottom
- r
.top
;
918 int diff
= height
- size
.y
;
921 // the control is too high, resize to fit
922 control
->SetSize(wxDefaultCoord
, height
- 2);
933 y
+= height
+ 2*GetMargins().y
;
935 else // horizontal toolbar
937 if ( left
== wxDefaultCoord
)
943 control
->Move(left
, top
+ (diff
+ 1) / 2);
946 // the max index is the "real" number of buttons - i.e. counting even the
947 // separators which we added just for aligning the controls
952 if ( m_maxRows
== 0 )
954 // if not set yet, only one row
958 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
960 if ( m_maxRows
== 0 )
962 // if not set yet, have one column
967 InvalidateBestSize();
971 // ----------------------------------------------------------------------------
973 // ----------------------------------------------------------------------------
975 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
977 wxToolBarToolBase
*tool
= FindById((int)id
);
981 bool toggled
= false; // just to suppress warnings
983 if ( tool
->CanBeToggled() )
985 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
986 toggled
= (state
& TBSTATE_CHECKED
) != 0;
988 // ignore the event when a radio button is released, as this doesn't seem to
989 // happen at all, and is handled otherwise
990 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
993 tool
->Toggle(toggled
);
994 UnToggleRadioGroup(tool
);
997 // OnLeftClick() can veto the button state change - for buttons which
998 // may be toggled only, of couse
999 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
1002 tool
->Toggle(!toggled
);
1004 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
1010 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1012 WXLPARAM
*WXUNUSED(result
))
1015 // First check if this applies to us
1016 NMHDR
*hdr
= (NMHDR
*)lParam
;
1018 // the tooltips control created by the toolbar is sometimes Unicode, even
1019 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1020 UINT code
= hdr
->code
;
1021 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1024 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1025 if ( toolTipWnd
!= hdr
->hwndFrom
)
1028 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1029 int id
= (int)ttText
->hdr
.idFrom
;
1031 wxToolBarToolBase
*tool
= FindById(id
);
1035 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1041 // ----------------------------------------------------------------------------
1043 // ----------------------------------------------------------------------------
1045 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1047 wxToolBarBase::SetToolBitmapSize(size
);
1049 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1052 void wxToolBar::SetRows(int nRows
)
1054 if ( nRows
== m_maxRows
)
1056 // avoid resizing the frame uselessly
1060 // TRUE in wParam means to create at least as many rows, FALSE -
1063 ::SendMessage(GetHwnd(), TB_SETROWS
,
1064 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1072 // The button size is bigger than the bitmap size
1073 wxSize
wxToolBar::GetToolSize() const
1075 // TB_GETBUTTONSIZE is supported from version 4.70
1076 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1077 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1078 && !defined (__DIGITALMARS__)
1079 if ( wxTheApp
->GetComCtl32Version() >= 470 )
1081 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1083 return wxSize(LOWORD(dw
), HIWORD(dw
));
1086 #endif // comctl32.dll 4.70+
1089 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1094 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1097 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1099 for ( ; current
!= 0; current
= current
->GetNext() )
1102 return current
->GetData();
1104 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1105 size_t separators
= tool
->GetSeparatorsCount();
1107 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1108 // otherwise, skip as many items as the separator count, plus the
1110 index
-= separators
? separators
+ 1 : 1;
1116 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1121 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1122 // MBN: when the point ( x, y ) is close to the toolbar border
1123 // TB_HITTEST returns m_nButtons ( not -1 )
1124 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1126 // it's a separator or there is no tool at all there
1127 return (wxToolBarToolBase
*)NULL
;
1130 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1131 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1132 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1134 return m_tools
.Item((size_t)index
)->GetData();
1139 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1143 void wxToolBar::UpdateSize()
1145 // the toolbar size changed
1146 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1148 // we must also refresh the frame after the toolbar size (possibly) changed
1149 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1152 frame
->SendSizeEvent();
1156 // ----------------------------------------------------------------------------
1158 // ---------------------------------------------------------------------------
1160 void wxToolBar::SetWindowStyleFlag(long style
)
1162 // the style bits whose changes force us to recreate the toolbar
1163 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1165 const long styleOld
= GetWindowStyle();
1167 wxToolBarBase::SetWindowStyleFlag(style
);
1169 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1170 // also fatal as we'd then try to recreate the toolbar when it's just being
1172 if ( GetToolsCount() &&
1173 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1175 // to remove the text labels, simply re-realizing the toolbar is enough
1176 // but I don't know of any way to add the text to an existing toolbar
1177 // other than by recreating it entirely
1182 // ----------------------------------------------------------------------------
1184 // ----------------------------------------------------------------------------
1186 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1188 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1189 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1192 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1194 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1195 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1198 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1200 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1201 // without, so we really need to delete the button and recreate it here
1202 wxFAIL_MSG( _T("not implemented") );
1205 // ----------------------------------------------------------------------------
1207 // ----------------------------------------------------------------------------
1209 // Responds to colour changes, and passes event on to children.
1210 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1212 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1214 // Remap the buttons
1217 // Relayout the toolbar
1218 int nrows
= m_maxRows
;
1219 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1224 // let the event propagate further
1228 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1230 if (event
.Leaving() && m_pInTool
)
1237 if (event
.RightDown())
1239 // For now, we don't have an id. Later we could
1240 // try finding the tool.
1241 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1249 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1251 // calculate our minor dimension ourselves - we're confusing the standard
1252 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1254 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1258 if ( GetWindowStyle() & wxTB_VERTICAL
)
1260 w
= r
.right
- r
.left
;
1263 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1270 if (HasFlag( wxTB_FLAT
))
1271 h
= r
.bottom
- r
.top
- 3;
1273 h
= r
.bottom
- r
.top
;
1276 // FIXME: hardcoded separator line height...
1277 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1282 if ( MAKELPARAM(w
, h
) != lParam
)
1284 // size really changed
1288 // message processed
1295 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1297 // erase any dummy separators which we used for aligning the controls if
1300 // first of all, do we have any controls at all?
1301 wxToolBarToolsList::compatibility_iterator node
;
1302 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1304 if ( node
->GetData()->IsControl() )
1310 // no controls, nothing to erase
1314 // prepare the DC on which we'll be drawing
1315 wxClientDC
dc(this);
1316 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1317 dc
.SetPen(*wxTRANSPARENT_PEN
);
1320 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1322 // nothing to redraw anyhow
1327 wxCopyRECTToRect(r
, rectUpdate
);
1329 dc
.SetClippingRegion(rectUpdate
);
1331 // draw the toolbar tools, separators &c normally
1332 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1334 // for each control in the toolbar find all the separators intersecting it
1337 // NB: this is really the only way to do it as we don't know if a separator
1338 // corresponds to a control (i.e. is a dummy one) or a real one
1340 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1342 wxToolBarToolBase
*tool
= node
->GetData();
1343 if ( tool
->IsControl() )
1345 // get the control rect in our client coords
1346 wxControl
*control
= tool
->GetControl();
1347 wxRect rectCtrl
= control
->GetRect();
1349 // iterate over all buttons
1351 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1352 for ( int n
= 0; n
< count
; n
++ )
1354 // is it a separator?
1355 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1358 wxLogDebug(_T("TB_GETBUTTON failed?"));
1363 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1366 // get the bounding rect of the separator
1368 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1371 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1376 // does it intersect the control?
1378 wxCopyRECTToRect(r
, rectItem
);
1379 if ( rectCtrl
.Intersects(rectItem
) )
1381 // yes, do erase it!
1382 dc
.DrawRectangle(rectItem
);
1384 // Necessary in case we use a no-paint-on-size
1385 // style in the parent: the controls can disappear
1386 control
->Refresh(false);
1395 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1397 wxCoord x
= GET_X_LPARAM(lParam
),
1398 y
= GET_Y_LPARAM(lParam
);
1399 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1401 // cursor left current tool
1402 if( tool
!= m_pInTool
&& !tool
)
1408 // cursor entered a tool
1409 if( tool
!= m_pInTool
&& tool
)
1412 OnMouseEnter( tool
->GetId() );
1416 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1421 if ( HandleSize(wParam
, lParam
) )
1426 // we don't handle mouse moves, so always pass the message to
1427 // wxControl::MSWWindowProc
1428 HandleMouseMove(wParam
, lParam
);
1432 if ( HandlePaint(wParam
, lParam
) )
1436 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1439 // ----------------------------------------------------------------------------
1440 // private functions
1441 // ----------------------------------------------------------------------------
1443 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1449 wxLogLastError(_T("CreateCompatibleDC"));
1454 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1458 wxLogLastError(_T("SelectObject"));
1463 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1465 for ( int i
= 0; i
< width
; i
++ )
1467 for ( int j
= 0; j
< height
; j
++ )
1469 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1471 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1473 COLORREF col
= cmap
[k
].from
;
1474 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1475 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1476 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1478 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1487 // VZ: I leave here my attempts to map the bitmap to the system colours
1488 // faster by using BitBlt() even though it's broken currently - but
1489 // maybe someone else can finish it? It should be faster than iterating
1490 // over all pixels...
1492 MemoryHDC hdcMask
, hdcDst
;
1493 if ( !hdcMask
|| !hdcDst
)
1495 wxLogLastError(_T("CreateCompatibleDC"));
1500 // create the target bitmap
1501 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1504 wxLogLastError(_T("CreateCompatibleBitmap"));
1509 // create the monochrome mask bitmap
1510 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1513 wxLogLastError(_T("CreateBitmap(mono)"));
1515 ::DeleteObject(hbmpDst
);
1520 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1521 bmpInMask(hdcMask
, hbmpMask
);
1524 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1526 // create the mask for this colour
1527 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1528 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1530 // replace this colour with the target one in the dst bitmap
1531 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1532 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1534 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1537 MAKEROP4(PATCOPY
, SRCCOPY
));
1539 (void)::SelectObject(hdcDst
, hbrOld
);
1540 ::DeleteObject(hbr
);
1543 ::DeleteObject((HBITMAP
)bitmap
);
1545 return (WXHBITMAP
)hbmpDst
;
1549 #endif // wxUSE_TOOLBAR && Win95