1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/wince/tbarwce.cpp
3 // Purpose: wxToolBar for Windows CE
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "tbarwce.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
44 #include "wx/toolbar.h"
46 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
50 #include "wx/msw/private.h"
58 #include "wx/msw/winundef.h"
60 #if defined(__MWERKS__) && defined(__WXMSW__)
61 // including <windef.h> for max definition doesn't seem
62 // to work using CodeWarrior 6 Windows. So we define it
63 // here. (Otherwise we get a undefined identifier 'max'
64 // later on in this file.) (Added by dimitri@shortcut.nl)
66 # define max(a,b) (((a) > (b)) ? (a) : (b))
71 // ----------------------------------------------------------------------------
72 // conditional compilation
73 // ----------------------------------------------------------------------------
75 // wxWindows previously always considered that toolbar buttons have light grey
76 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
77 // doesn't work with XPMs which then appear to have black background. To make
78 // this work, we must respect the bitmap masks - which we do now. This should
79 // be ok in any case, but to restore 100% compatible with the old version
80 // behaviour, you can set this to 0.
81 #define USE_BITMAP_MASKS 1
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // these standard constants are not always defined in compilers headers
91 #define TBSTYLE_LIST 0x1000
92 #define TBSTYLE_FLAT 0x0800
95 #ifndef TBSTYLE_TRANSPARENT
96 #define TBSTYLE_TRANSPARENT 0x8000
99 #ifndef TBSTYLE_TOOLTIPS
100 #define TBSTYLE_TOOLTIPS 0x0100
105 #define TB_SETSTYLE (WM_USER + 56)
106 #define TB_GETSTYLE (WM_USER + 57)
110 #define TB_HITTEST (WM_USER + 69)
113 // these values correspond to those used by comctl32.dll
114 #define DEFAULTBITMAPX 16
115 #define DEFAULTBITMAPY 15
116 #define DEFAULTBUTTONX 24
117 #define DEFAULTBUTTONY 24
118 #define DEFAULTBARHEIGHT 27
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
126 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
127 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
128 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class wxToolBarTool
: public wxToolBarToolBase
138 wxToolBarTool(wxToolBar
*tbar
,
140 const wxString
& label
,
141 const wxBitmap
& bmpNormal
,
142 const wxBitmap
& bmpDisabled
,
144 wxObject
*clientData
,
145 const wxString
& shortHelp
,
146 const wxString
& longHelp
)
147 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
148 clientData
, shortHelp
, longHelp
)
153 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
154 : wxToolBarToolBase(tbar
, control
)
159 virtual void SetLabel(const wxString
& label
)
161 if ( label
== m_label
)
164 wxToolBarToolBase::SetLabel(label
);
166 // we need to update the label shown in the toolbar because it has a
167 // pointer to the internal buffer of the old label
169 // TODO: use TB_SETBUTTONINFO
172 // set/get the number of separators which we use to cover the space used by
173 // a control in the toolbar
174 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
175 size_t GetSeparatorsCount() const { return m_nSepCount
; }
182 // ============================================================================
184 // ============================================================================
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
191 const wxString
& label
,
192 const wxBitmap
& bmpNormal
,
193 const wxBitmap
& bmpDisabled
,
195 wxObject
*clientData
,
196 const wxString
& shortHelp
,
197 const wxString
& longHelp
)
199 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
200 clientData
, shortHelp
, longHelp
);
203 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
205 return new wxToolBarTool(this, control
);
208 // ----------------------------------------------------------------------------
209 // wxToolBar construction
210 // ----------------------------------------------------------------------------
212 void wxToolBar::Init()
218 m_defaultWidth
= DEFAULTBITMAPX
;
219 m_defaultHeight
= DEFAULTBITMAPY
;
225 bool wxToolBar::Create(wxWindow
*parent
,
230 const wxString
& name
,
233 // common initialisation
234 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
237 // MSW-specific initialisation
238 if ( !MSWCreateToolbar(pos
, size
, menuBar
) )
241 // set up the colors and fonts
242 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
243 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
248 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
249 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
252 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
, wxMenuBar
* menuBar
)
256 m_menuBar
->SetToolBar(this);
258 // Create the menubar.
261 memset (&mbi
, 0, sizeof (SHMENUBARINFO
));
262 mbi
.cbSize
= sizeof (SHMENUBARINFO
);
263 mbi
.hwndParent
= (HWND
) GetParent()->GetHWND();
264 mbi
.nToolBarId
= 5000;
267 mbi
.dwFlags
= 0 ; // SHCMBF_EMPTYBAR;
268 mbi
.hInstRes
= wxGetInstance();
270 if (!SHCreateMenuBar(&mbi
))
272 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
276 SetHWND((WXHWND
) mbi
.hwndMB
);
278 if (!::SendMessage((HWND) GetHWND(), TB_DELETEBUTTON, 0, (LPARAM) 0))
280 wxLogLastError(wxT("TB_DELETEBUTTON"));
283 // install wxWindows window proc for this window
290 HMENU hMenu
= (HMENU
)::SendMessage(mbi
.hwndMB
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
294 memset(&tbButton
, 0, sizeof(TBBUTTON
));
295 tbButton
.iBitmap
= I_IMAGENONE
;
296 tbButton
.fsState
= TBSTATE_ENABLED
;
297 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
300 for (i
= 0; i
< menuBar
->GetMenuCount(); i
++)
302 HMENU hPopupMenu
= (HMENU
) menuBar
->GetMenu(i
)->GetHMenu() ;
303 tbButton
.dwData
= (DWORD
)hPopupMenu
;
304 wxString label
= wxStripMenuCodes(menuBar
->GetLabelTop(i
));
305 tbButton
.iString
= (int) label
.c_str();
307 tbButton
.idCommand
= NewControlId();
308 if (!::SendMessage((HWND
) GetHWND(), TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
))
310 wxLogLastError(wxT("TB_INSERTBUTTON"));
318 CommandBar_AddToolTips( hwndCB
, uNumSmallTips
,szSmallTips
);
320 CommandBar_AddBitmap(hwndCB
, HINST_COMMCTRL
, IDB_STD_SMALL_COLOR
,
322 // Add buttons in tbSTDButton to Commandbar
323 CommandBar_AddButtons(hwndCB
, sizeof(tbSTDButton
)/sizeof(TBBUTTON
),
329 void wxToolBar::Recreate()
332 const HWND hwndOld
= GetHwnd();
335 // we haven't been created yet, no need to recreate
339 // get the position and size before unsubclassing the old toolbar
340 const wxPoint pos
= GetPosition();
341 const wxSize size
= GetSize();
345 if ( !MSWCreateToolbar(pos
, size
) )
348 wxFAIL_MSG( _T("recreating the toolbar failed") );
353 // reparent all our children under the new toolbar
354 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
356 node
= node
->GetNext() )
358 wxWindow
*win
= node
->GetData();
359 if ( !win
->IsTopLevel() )
360 ::SetParent(GetHwndOf(win
), GetHwnd());
363 // only destroy the old toolbar now -- after all the children had been
365 ::DestroyWindow(hwndOld
);
367 // it is for the old bitmap control and can't be used with the new one
370 ::DeleteObject((HBITMAP
) m_hBitmap
);
379 wxToolBar::~wxToolBar()
381 // we must refresh the frame size when the toolbar is deleted but the frame
382 // is not - otherwise toolbar leaves a hole in the place it used to occupy
383 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
384 if ( frame
&& !frame
->IsBeingDeleted() )
386 frame
->SendSizeEvent();
391 ::DeleteObject((HBITMAP
) m_hBitmap
);
395 wxSize
wxToolBar::DoGetBestSize() const
397 wxSize sizeBest
= GetToolSize();
398 sizeBest
.x
*= GetToolsCount();
400 // reverse horz and vertical components if necessary
401 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
404 // Return HMENU for the menu associated with the commandbar
405 WXHMENU
wxToolBar::GetHMenu()
409 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
416 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
418 // toolbars never have border, giving one to them results in broken
420 WXDWORD msStyle
= wxControl::MSWGetStyle
422 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
425 // always include this one, it never hurts and setting it later only if we
426 // do have tooltips wouldn't work
427 msStyle
|= TBSTYLE_TOOLTIPS
;
429 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
431 // static as it doesn't change during the program lifetime
432 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
434 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
435 // style with 6.00 (part of Windows XP) leads to the toolbar with
436 // incorrect background colour - and not using it still results in the
437 // correct (flat) toolbar, so don't use it there
438 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
440 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
443 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
445 msStyle
|= TBSTYLE_LIST
;
449 if ( style
& wxTB_NODIVIDER
)
450 msStyle
|= CCS_NODIVIDER
;
452 if ( style
& wxTB_NOALIGN
)
453 msStyle
|= CCS_NOPARENTALIGN
;
455 if ( style
& wxTB_VERTICAL
)
461 // ----------------------------------------------------------------------------
462 // adding/removing tools
463 // ----------------------------------------------------------------------------
465 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
467 // nothing special to do here - we really create the toolbar buttons in
474 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
476 // the main difficulty we have here is with the controls in the toolbars:
477 // as we (sometimes) use several separators to cover up the space used by
478 // them, the indices are not the same for us and the toolbar
480 // first determine the position of the first button to delete: it may be
481 // different from pos if we use several separators to cover the space used
483 wxToolBarToolsList::compatibility_iterator node
;
484 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
486 wxToolBarToolBase
*tool2
= node
->GetData();
489 // let node point to the next node in the list
490 node
= node
->GetNext();
495 if ( tool2
->IsControl() )
497 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
501 // now determine the number of buttons to delete and the area taken by them
502 size_t nButtonsToDelete
= 1;
504 // get the size of the button we're going to delete
506 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
508 wxLogLastError(_T("TB_GETITEMRECT"));
511 int width
= r
.right
- r
.left
;
513 if ( tool
->IsControl() )
515 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
517 width
*= nButtonsToDelete
;
520 // do delete all buttons
521 m_nButtons
-= nButtonsToDelete
;
522 while ( nButtonsToDelete
-- > 0 )
524 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
526 wxLogLastError(wxT("TB_DELETEBUTTON"));
534 // and finally reposition all the controls after this button (the toolbar
535 // takes care of all normal items)
536 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
538 wxToolBarToolBase
*tool2
= node
->GetData();
539 if ( tool2
->IsControl() )
542 wxControl
*control
= tool2
->GetControl();
543 control
->GetPosition(&x
, NULL
);
544 control
->Move(x
- width
, -1);
551 bool wxToolBar::Realize()
553 const size_t nTools
= GetToolsCount();
560 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
563 // delete all old buttons, if any
564 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
566 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
568 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
573 // add the buttons and separators
574 // ------------------------------
576 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
578 // this array will hold the indices of all controls in the toolbar
579 wxArrayInt controlIds
;
581 bool lastWasRadio
= FALSE
;
583 wxToolBarToolsList::Node
* node
;
584 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
586 wxToolBarToolBase
*tool
= node
->GetData();
588 TBBUTTON
& button
= buttons
[i
];
590 wxZeroMemory(button
);
592 bool isRadio
= FALSE
;
593 switch ( tool
->GetStyle() )
595 case wxTOOL_STYLE_CONTROL
:
596 button
.idCommand
= tool
->GetId();
597 // fall through: create just a separator too
599 case wxTOOL_STYLE_SEPARATOR
:
600 button
.fsState
= TBSTATE_ENABLED
;
601 button
.fsStyle
= TBSTYLE_SEP
;
604 case wxTOOL_STYLE_BUTTON
:
607 if ( !HasFlag(wxTB_NOICONS
) )
608 button
.iBitmap
= bitmapId
;
610 if ( HasFlag(wxTB_TEXT
) )
612 const wxString
& label
= tool
->GetLabel();
613 if ( !label
.empty() )
615 button
.iString
= (int)label
.c_str();
619 button
.idCommand
= tool
->GetId();
621 if ( tool
->IsEnabled() )
622 button
.fsState
|= TBSTATE_ENABLED
;
623 if ( tool
->IsToggled() )
624 button
.fsState
|= TBSTATE_CHECKED
;
626 switch ( tool
->GetKind() )
629 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
633 // the first item in the radio group is checked by
634 // default to be consistent with wxGTK and the menu
636 button
.fsState
|= TBSTATE_CHECKED
;
645 button
.fsStyle
= TBSTYLE_CHECK
;
649 wxFAIL_MSG( _T("unexpected toolbar button kind") );
653 button
.fsStyle
= TBSTYLE_BUTTON
;
661 lastWasRadio
= isRadio
;
666 // Add buttons in tbSTDButton to Commandbar
667 if (!CommandBar_AddButtons(GetHwnd(), i
, buttons
))
669 wxLogLastError(wxT("CommandBar_AddButtons"));
675 // Deal with the controls finally
676 // ------------------------------
678 // adjust the controls size to fit nicely in the toolbar
681 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
683 wxToolBarToolBase
*tool
= node
->GetData();
685 // we calculate the running y coord for vertical toolbars so we need to
686 // get the items size for all items but for the horizontal ones we
687 // don't need to deal with the non controls
688 bool isControl
= tool
->IsControl();
689 if ( !isControl
&& !isVertical
)
692 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
693 // latter only appeared in v4.70 of comctl32.dll
695 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
696 index
, (LPARAM
)(LPRECT
)&r
) )
698 wxLogLastError(wxT("TB_GETITEMRECT"));
703 // can only be control if isVertical
704 y
+= r
.bottom
- r
.top
;
709 wxControl
*control
= tool
->GetControl();
711 wxSize size
= control
->GetSize();
713 // the position of the leftmost controls corner
716 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
717 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
718 // available in headers, now check whether it is available now
720 if ( wxTheApp
->GetComCtl32Version() >= 471 )
722 // set the (underlying) separators width to be that of the
725 tbbi
.cbSize
= sizeof(tbbi
);
726 tbbi
.dwMask
= TBIF_SIZE
;
728 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
729 tool
->GetId(), (LPARAM
)&tbbi
) )
731 // the id is probably invalid?
732 wxLogLastError(wxT("TB_SETBUTTONINFO"));
736 #endif // comctl32.dll 4.71
737 // TB_SETBUTTONINFO unavailable
739 // try adding several separators to fit the controls width
740 int widthSep
= r
.right
- r
.left
;
746 tbb
.fsState
= TBSTATE_ENABLED
;
747 tbb
.fsStyle
= TBSTYLE_SEP
;
749 size_t nSeparators
= size
.x
/ widthSep
;
750 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
752 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
753 index
, (LPARAM
)&tbb
) )
755 wxLogLastError(wxT("TB_INSERTBUTTON"));
761 // remember the number of separators we used - we'd have to
762 // delete all of them later
763 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
765 // adjust the controls width to exactly cover the separators
766 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
769 // position the control itself correctly vertically
770 int height
= r
.bottom
- r
.top
;
771 int diff
= height
- size
.y
;
774 // the control is too high, resize to fit
775 control
->SetSize(-1, height
- 2);
786 y
+= height
+ 2*GetMargins().y
;
788 else // horizontal toolbar
796 control
->Move(left
, top
+ (diff
+ 1) / 2);
799 // the max index is the "real" number of buttons - i.e. counting even the
800 // separators which we added just for aligning the controls
805 if ( m_maxRows
== 0 )
807 // if not set yet, only one row
811 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
813 if ( m_maxRows
== 0 )
815 // if not set yet, have one column
824 // ----------------------------------------------------------------------------
826 // ----------------------------------------------------------------------------
828 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
830 wxToolBarToolBase
*tool
= FindById((int)id
);
833 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
834 event
.SetEventObject(this);
838 return GetEventHandler()->ProcessEvent(event
);
841 if ( tool
->CanBeToggled() )
843 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
844 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
847 bool toggled
= tool
->IsToggled();
849 // avoid sending the event when a radio button is released, this is not
851 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
853 // OnLeftClick() can veto the button state change - for buttons which
854 // may be toggled only, of couse
855 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
859 tool
->SetToggle(toggled
);
861 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
868 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
870 WXLPARAM
*WXUNUSED(result
))
873 // First check if this applies to us
874 NMHDR
*hdr
= (NMHDR
*)lParam
;
876 // the tooltips control created by the toolbar is sometimes Unicode, even
877 // in an ANSI application - this seems to be a bug in comctl32.dll v5
878 UINT code
= hdr
->code
;
879 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
882 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
883 if ( toolTipWnd
!= hdr
->hwndFrom
)
886 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
887 int id
= (int)ttText
->hdr
.idFrom
;
889 wxToolBarToolBase
*tool
= FindById(id
);
893 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
899 // ----------------------------------------------------------------------------
901 // ----------------------------------------------------------------------------
903 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
905 wxToolBarBase::SetToolBitmapSize(size
);
907 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
910 void wxToolBar::SetRows(int nRows
)
912 if ( nRows
== m_maxRows
)
914 // avoid resizing the frame uselessly
918 // TRUE in wParam means to create at least as many rows, FALSE -
921 ::SendMessage(GetHwnd(), TB_SETROWS
,
922 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
930 // The button size is bigger than the bitmap size
931 wxSize
wxToolBar::GetToolSize() const
933 // TB_GETBUTTONSIZE is supported from version 4.70
934 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
935 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
936 if ( wxTheApp
->GetComCtl32Version() >= 470 )
938 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
940 return wxSize(LOWORD(dw
), HIWORD(dw
));
943 #endif // comctl32.dll 4.70+
946 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
951 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
954 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
956 for ( ; current
!= 0; current
= current
->GetNext() )
959 return current
->GetData();
961 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
962 size_t separators
= tool
->GetSeparatorsCount();
964 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
965 // otherwise, skip as many items as the separator count, plus the
967 index
-= separators
? separators
+ 1 : 1;
973 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
978 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
979 // MBN: when the point ( x, y ) is close to the toolbar border
980 // TB_HITTEST returns m_nButtons ( not -1 )
981 if ( index
< 0 || (size_t)index
>= m_nButtons
)
983 // it's a separator or there is no tool at all there
984 return (wxToolBarToolBase
*)NULL
;
987 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
988 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
989 if ( wxTheApp
->GetComCtl32Version() >= 471 )
991 return m_tools
.Item((size_t)index
)->GetData();
996 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1000 void wxToolBar::UpdateSize()
1002 // the toolbar size changed
1003 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1005 // we must also refresh the frame after the toolbar size (possibly) changed
1006 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1009 frame
->SendSizeEvent();
1013 // ----------------------------------------------------------------------------
1015 // ---------------------------------------------------------------------------
1017 void wxToolBar::SetWindowStyleFlag(long style
)
1019 // the style bits whose changes force us to recreate the toolbar
1020 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1022 const long styleOld
= GetWindowStyle();
1024 wxToolBarBase::SetWindowStyleFlag(style
);
1026 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1027 // also fatal as we'd then try to recreate the toolbar when it's just being
1029 if ( GetToolsCount() &&
1030 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1032 // to remove the text labels, simply re-realizing the toolbar is enough
1033 // but I don't know of any way to add the text to an existing toolbar
1034 // other than by recreating it entirely
1039 // ----------------------------------------------------------------------------
1041 // ----------------------------------------------------------------------------
1043 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1045 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1046 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1049 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1051 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1052 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1055 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1057 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1058 // without, so we really need to delete the button and recreate it here
1059 wxFAIL_MSG( _T("not implemented") );
1062 // ----------------------------------------------------------------------------
1064 // ----------------------------------------------------------------------------
1066 // Responds to colour changes, and passes event on to children.
1067 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1069 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1071 // Remap the buttons
1074 // Relayout the toolbar
1075 int nrows
= m_maxRows
;
1076 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1081 // let the event propagate further
1085 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1087 if (event
.Leaving() && m_pInTool
)
1094 if (event
.RightDown())
1096 // For now, we don't have an id. Later we could
1097 // try finding the tool.
1098 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1106 void wxToolBar::HandleMouseMove(WXWPARAM wParam
, WXLPARAM lParam
)
1108 wxCoord x
= GET_X_LPARAM(lParam
),
1109 y
= GET_Y_LPARAM(lParam
);
1110 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1112 // cursor left current tool
1113 if( tool
!= m_pInTool
&& !tool
)
1119 // cursor entered a tool
1120 if( tool
!= m_pInTool
&& tool
)
1123 OnMouseEnter( tool
->GetId() );
1127 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1133 if ( HandleSize(wParam
, lParam
) )
1138 // we don't handle mouse moves, so always pass the message to
1139 // wxControl::MSWWindowProc
1140 HandleMouseMove(wParam
, lParam
);
1144 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1147 // ----------------------------------------------------------------------------
1148 // private functions
1149 // ----------------------------------------------------------------------------
1151 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1157 wxLogLastError(_T("CreateCompatibleDC"));
1162 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1166 wxLogLastError(_T("SelectObject"));
1171 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1173 for ( int i
= 0; i
< width
; i
++ )
1175 for ( int j
= 0; j
< height
; j
++ )
1177 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1179 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1181 COLORREF col
= cmap
[k
].from
;
1182 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1183 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1184 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1186 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1195 // VZ: I leave here my attempts to map the bitmap to the system colours
1196 // faster by using BitBlt() even though it's broken currently - but
1197 // maybe someone else can finish it? It should be faster than iterating
1198 // over all pixels...
1200 MemoryHDC hdcMask
, hdcDst
;
1201 if ( !hdcMask
|| !hdcDst
)
1203 wxLogLastError(_T("CreateCompatibleDC"));
1208 // create the target bitmap
1209 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1212 wxLogLastError(_T("CreateCompatibleBitmap"));
1217 // create the monochrome mask bitmap
1218 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1221 wxLogLastError(_T("CreateBitmap(mono)"));
1223 ::DeleteObject(hbmpDst
);
1228 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1229 bmpInMask(hdcMask
, hbmpMask
);
1232 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1234 // create the mask for this colour
1235 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1236 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1238 // replace this colour with the target one in the dst bitmap
1239 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1240 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1242 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1245 MAKEROP4(PATCOPY
, SRCCOPY
));
1247 (void)::SelectObject(hdcDst
, hbrOld
);
1248 ::DeleteObject(hbr
);
1251 ::DeleteObject((HBITMAP
)bitmap
);
1253 return (WXHBITMAP
)hbmpDst
;
1257 #endif // wxUSE_TOOLBAR && Win95