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
292 void wxToolBar::Recreate()
295 const HWND hwndOld
= GetHwnd();
298 // we haven't been created yet, no need to recreate
302 // get the position and size before unsubclassing the old toolbar
303 const wxPoint pos
= GetPosition();
304 const wxSize size
= GetSize();
308 if ( !MSWCreateToolbar(pos
, size
) )
311 wxFAIL_MSG( _T("recreating the toolbar failed") );
316 // reparent all our children under the new toolbar
317 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
319 node
= node
->GetNext() )
321 wxWindow
*win
= node
->GetData();
322 if ( !win
->IsTopLevel() )
323 ::SetParent(GetHwndOf(win
), GetHwnd());
326 // only destroy the old toolbar now -- after all the children had been
328 ::DestroyWindow(hwndOld
);
330 // it is for the old bitmap control and can't be used with the new one
333 ::DeleteObject((HBITMAP
) m_hBitmap
);
342 wxToolBar::~wxToolBar()
345 GetMenuBar()->SetToolBar(NULL
);
347 // we must refresh the frame size when the toolbar is deleted but the frame
348 // is not - otherwise toolbar leaves a hole in the place it used to occupy
349 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
350 if ( frame
&& !frame
->IsBeingDeleted() )
352 frame
->SendSizeEvent();
357 ::DeleteObject((HBITMAP
) m_hBitmap
);
361 wxSize
wxToolBar::DoGetBestSize() const
363 wxSize sizeBest
= GetToolSize();
364 sizeBest
.x
*= GetToolsCount();
366 // reverse horz and vertical components if necessary
367 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
370 // Return HMENU for the menu associated with the commandbar
371 WXHMENU
wxToolBar::GetHMenu()
375 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
382 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
384 // toolbars never have border, giving one to them results in broken
386 WXDWORD msStyle
= wxControl::MSWGetStyle
388 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
391 // always include this one, it never hurts and setting it later only if we
392 // do have tooltips wouldn't work
393 msStyle
|= TBSTYLE_TOOLTIPS
;
395 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
397 // static as it doesn't change during the program lifetime
398 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
400 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
401 // style with 6.00 (part of Windows XP) leads to the toolbar with
402 // incorrect background colour - and not using it still results in the
403 // correct (flat) toolbar, so don't use it there
404 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
406 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
409 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
411 msStyle
|= TBSTYLE_LIST
;
415 if ( style
& wxTB_NODIVIDER
)
416 msStyle
|= CCS_NODIVIDER
;
418 if ( style
& wxTB_NOALIGN
)
419 msStyle
|= CCS_NOPARENTALIGN
;
421 if ( style
& wxTB_VERTICAL
)
427 // ----------------------------------------------------------------------------
428 // adding/removing tools
429 // ----------------------------------------------------------------------------
431 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
433 // nothing special to do here - we really create the toolbar buttons in
440 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
442 // the main difficulty we have here is with the controls in the toolbars:
443 // as we (sometimes) use several separators to cover up the space used by
444 // them, the indices are not the same for us and the toolbar
446 // first determine the position of the first button to delete: it may be
447 // different from pos if we use several separators to cover the space used
449 wxToolBarToolsList::compatibility_iterator node
;
450 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
452 wxToolBarToolBase
*tool2
= node
->GetData();
455 // let node point to the next node in the list
456 node
= node
->GetNext();
461 if ( tool2
->IsControl() )
463 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
467 // now determine the number of buttons to delete and the area taken by them
468 size_t nButtonsToDelete
= 1;
470 // get the size of the button we're going to delete
472 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
474 wxLogLastError(_T("TB_GETITEMRECT"));
477 int width
= r
.right
- r
.left
;
479 if ( tool
->IsControl() )
481 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
483 width
*= nButtonsToDelete
;
486 // do delete all buttons
487 m_nButtons
-= nButtonsToDelete
;
488 while ( nButtonsToDelete
-- > 0 )
490 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
492 wxLogLastError(wxT("TB_DELETEBUTTON"));
500 // and finally reposition all the controls after this button (the toolbar
501 // takes care of all normal items)
502 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
504 wxToolBarToolBase
*tool2
= node
->GetData();
505 if ( tool2
->IsControl() )
508 wxControl
*control
= tool2
->GetControl();
509 control
->GetPosition(&x
, NULL
);
510 control
->Move(x
- width
, -1);
517 struct wxToolBarIdMapping
523 static wxToolBarIdMapping sm_ToolBarIdMappingArray
[] =
525 { wxID_COPY
, STD_COPY
},
526 { wxID_CUT
, STD_CUT
},
527 { wxID_FIND
, STD_FIND
},
528 { wxID_PASTE
, STD_PASTE
},
529 { wxID_NEW
, STD_FILENEW
},
530 { wxID_OPEN
, STD_FILEOPEN
},
531 { wxID_SAVE
, STD_FILESAVE
},
532 { wxID_PRINT
, STD_PRINT
},
533 { wxID_PREVIEW
, STD_PRINTPRE
},
534 { wxID_UNDO
, STD_UNDO
},
535 { wxID_REDO
, STD_REDOW
},
536 { wxID_HELP
, STD_HELP
},
537 { wxID_DELETE
, STD_DELETE
},
538 { wxID_REPLACE
, STD_REPLACE
},
539 { wxID_PROPERTIES
, STD_PROPERTIES
},
540 { wxID_VIEW_DETAILS
, VIEW_DETAILS
},
541 { wxID_VIEW_SORTDATE
, VIEW_SORTDATE
},
542 { wxID_VIEW_LARGEICONS
, VIEW_LARGEICONS
},
543 { wxID_VIEW_SORTNAME
, VIEW_SORTNAME
},
544 { wxID_VIEW_LIST
, VIEW_LIST
},
545 { wxID_VIEW_SORTSIZE
, VIEW_SORTSIZE
},
546 { wxID_VIEW_SMALLICONS
, VIEW_SMALLICONS
},
547 { wxID_VIEW_SORTTYPE
, VIEW_SORTTYPE
},
551 static int wxFindIdForWinceId(int id
)
556 if (sm_ToolBarIdMappingArray
[i
].m_winceId
== 0)
558 else if (sm_ToolBarIdMappingArray
[i
].m_winceId
== id
)
559 return sm_ToolBarIdMappingArray
[i
].m_wxwinId
;
565 static int wxFindIdForwxWinId(int id
)
570 if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== 0)
572 else if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== id
)
573 return sm_ToolBarIdMappingArray
[i
].m_winceId
;
580 bool wxToolBar::Realize()
582 const size_t nTools
= GetToolsCount();
589 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
592 // delete all old buttons, if any
593 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
595 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
597 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
602 // add the buttons and separators
603 // ------------------------------
605 // Use standard buttons
606 CommandBar_AddBitmap((HWND
) GetHWND(), HINST_COMMCTRL
,
607 IDB_STD_SMALL_COLOR
, 0, 16, 16);
609 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
611 // this array will hold the indices of all controls in the toolbar
612 wxArrayInt controlIds
;
614 bool lastWasRadio
= FALSE
;
616 wxToolBarToolsList::Node
* node
;
617 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
619 wxToolBarToolBase
*tool
= node
->GetData();
621 bool processedThis
= TRUE
;
623 TBBUTTON
& button
= buttons
[i
];
625 wxZeroMemory(button
);
627 bool isRadio
= FALSE
;
628 switch ( tool
->GetStyle() )
630 case wxTOOL_STYLE_CONTROL
:
631 button
.idCommand
= tool
->GetId();
632 // fall through: create just a separator too
634 case wxTOOL_STYLE_SEPARATOR
:
635 button
.fsState
= TBSTATE_ENABLED
;
636 button
.fsStyle
= TBSTYLE_SEP
;
639 case wxTOOL_STYLE_BUTTON
:
640 // if ( !HasFlag(wxTB_NOICONS) )
641 // button.iBitmap = bitmapId;
643 if ( HasFlag(wxTB_TEXT
) )
645 const wxString
& label
= tool
->GetLabel();
646 if ( !label
.empty() )
648 button
.iString
= (int)label
.c_str();
652 int winceId
= wxFindIdForwxWinId(tool
->GetId());
655 button
.idCommand
= tool
->GetId();
656 // if ( !HasFlag(wxTB_NOICONS) )
657 button
.iBitmap
= winceId
;
660 processedThis
= FALSE
;
662 if ( tool
->IsEnabled() )
663 button
.fsState
|= TBSTATE_ENABLED
;
664 if ( tool
->IsToggled() )
665 button
.fsState
|= TBSTATE_CHECKED
;
667 switch ( tool
->GetKind() )
670 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
674 // the first item in the radio group is checked by
675 // default to be consistent with wxGTK and the menu
677 button
.fsState
|= TBSTATE_CHECKED
;
686 button
.fsStyle
= TBSTYLE_CHECK
;
690 wxFAIL_MSG( _T("unexpected toolbar button kind") );
694 button
.fsStyle
= TBSTYLE_BUTTON
;
701 lastWasRadio
= isRadio
;
707 // Add buttons to Commandbar
708 if (!CommandBar_AddButtons(GetHwnd(), i
, buttons
))
710 wxLogLastError(wxT("CommandBar_AddButtons"));
716 // Deal with the controls finally
717 // ------------------------------
719 // adjust the controls size to fit nicely in the toolbar
722 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
724 wxToolBarToolBase
*tool
= node
->GetData();
726 // we calculate the running y coord for vertical toolbars so we need to
727 // get the items size for all items but for the horizontal ones we
728 // don't need to deal with the non controls
729 bool isControl
= tool
->IsControl();
730 if ( !isControl
&& !isVertical
)
733 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
734 // latter only appeared in v4.70 of comctl32.dll
736 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
737 index
, (LPARAM
)(LPRECT
)&r
) )
739 wxLogLastError(wxT("TB_GETITEMRECT"));
744 // can only be control if isVertical
745 y
+= r
.bottom
- r
.top
;
750 wxControl
*control
= tool
->GetControl();
752 wxSize size
= control
->GetSize();
754 // the position of the leftmost controls corner
757 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
758 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
759 // available in headers, now check whether it is available now
761 if ( wxTheApp
->GetComCtl32Version() >= 471 )
763 // set the (underlying) separators width to be that of the
766 tbbi
.cbSize
= sizeof(tbbi
);
767 tbbi
.dwMask
= TBIF_SIZE
;
769 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
770 tool
->GetId(), (LPARAM
)&tbbi
) )
772 // the id is probably invalid?
773 wxLogLastError(wxT("TB_SETBUTTONINFO"));
777 #endif // comctl32.dll 4.71
778 // TB_SETBUTTONINFO unavailable
780 // try adding several separators to fit the controls width
781 int widthSep
= r
.right
- r
.left
;
787 tbb
.fsState
= TBSTATE_ENABLED
;
788 tbb
.fsStyle
= TBSTYLE_SEP
;
790 size_t nSeparators
= size
.x
/ widthSep
;
791 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
793 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
794 index
, (LPARAM
)&tbb
) )
796 wxLogLastError(wxT("TB_INSERTBUTTON"));
802 // remember the number of separators we used - we'd have to
803 // delete all of them later
804 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
806 // adjust the controls width to exactly cover the separators
807 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
810 // position the control itself correctly vertically
811 int height
= r
.bottom
- r
.top
;
812 int diff
= height
- size
.y
;
815 // the control is too high, resize to fit
816 control
->SetSize(-1, height
- 2);
827 y
+= height
+ 2*GetMargins().y
;
829 else // horizontal toolbar
837 control
->Move(left
, top
+ (diff
+ 1) / 2);
840 // the max index is the "real" number of buttons - i.e. counting even the
841 // separators which we added just for aligning the controls
846 if ( m_maxRows
== 0 )
848 // if not set yet, only one row
852 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
854 if ( m_maxRows
== 0 )
856 // if not set yet, have one column
865 // ----------------------------------------------------------------------------
867 // ----------------------------------------------------------------------------
869 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
871 wxToolBarToolBase
*tool
= FindById((int)id
);
874 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
875 event
.SetEventObject(this);
879 return GetEventHandler()->ProcessEvent(event
);
882 if ( tool
->CanBeToggled() )
884 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
885 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
888 bool toggled
= tool
->IsToggled();
890 // avoid sending the event when a radio button is released, this is not
892 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
894 // OnLeftClick() can veto the button state change - for buttons which
895 // may be toggled only, of couse
896 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
900 tool
->SetToggle(toggled
);
902 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
909 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
911 WXLPARAM
*WXUNUSED(result
))
914 // First check if this applies to us
915 NMHDR
*hdr
= (NMHDR
*)lParam
;
917 // the tooltips control created by the toolbar is sometimes Unicode, even
918 // in an ANSI application - this seems to be a bug in comctl32.dll v5
919 UINT code
= hdr
->code
;
920 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
923 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
924 if ( toolTipWnd
!= hdr
->hwndFrom
)
927 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
928 int id
= (int)ttText
->hdr
.idFrom
;
930 wxToolBarToolBase
*tool
= FindById(id
);
934 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
944 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
946 wxToolBarBase::SetToolBitmapSize(size
);
948 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
951 void wxToolBar::SetRows(int nRows
)
953 if ( nRows
== m_maxRows
)
955 // avoid resizing the frame uselessly
959 // TRUE in wParam means to create at least as many rows, FALSE -
962 ::SendMessage(GetHwnd(), TB_SETROWS
,
963 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
971 // The button size is bigger than the bitmap size
972 wxSize
wxToolBar::GetToolSize() const
974 // TB_GETBUTTONSIZE is supported from version 4.70
975 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
976 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
977 if ( wxTheApp
->GetComCtl32Version() >= 470 )
979 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
981 return wxSize(LOWORD(dw
), HIWORD(dw
));
984 #endif // comctl32.dll 4.70+
987 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
992 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
995 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
997 for ( ; current
!= 0; current
= current
->GetNext() )
1000 return current
->GetData();
1002 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1003 size_t separators
= tool
->GetSeparatorsCount();
1005 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1006 // otherwise, skip as many items as the separator count, plus the
1008 index
-= separators
? separators
+ 1 : 1;
1014 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1019 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1020 // MBN: when the point ( x, y ) is close to the toolbar border
1021 // TB_HITTEST returns m_nButtons ( not -1 )
1022 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1024 // it's a separator or there is no tool at all there
1025 return (wxToolBarToolBase
*)NULL
;
1028 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1029 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1030 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1032 return m_tools
.Item((size_t)index
)->GetData();
1037 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1041 void wxToolBar::UpdateSize()
1043 // the toolbar size changed
1044 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1046 // we must also refresh the frame after the toolbar size (possibly) changed
1047 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1050 frame
->SendSizeEvent();
1054 // ----------------------------------------------------------------------------
1056 // ---------------------------------------------------------------------------
1058 void wxToolBar::SetWindowStyleFlag(long style
)
1060 // the style bits whose changes force us to recreate the toolbar
1061 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1063 const long styleOld
= GetWindowStyle();
1065 wxToolBarBase::SetWindowStyleFlag(style
);
1067 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1068 // also fatal as we'd then try to recreate the toolbar when it's just being
1070 if ( GetToolsCount() &&
1071 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1073 // to remove the text labels, simply re-realizing the toolbar is enough
1074 // but I don't know of any way to add the text to an existing toolbar
1075 // other than by recreating it entirely
1080 // ----------------------------------------------------------------------------
1082 // ----------------------------------------------------------------------------
1084 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1086 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1087 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1090 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1092 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1093 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1096 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1098 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1099 // without, so we really need to delete the button and recreate it here
1100 wxFAIL_MSG( _T("not implemented") );
1103 // ----------------------------------------------------------------------------
1105 // ----------------------------------------------------------------------------
1107 // Responds to colour changes, and passes event on to children.
1108 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1110 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1112 // Remap the buttons
1115 // Relayout the toolbar
1116 int nrows
= m_maxRows
;
1117 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1122 // let the event propagate further
1126 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1128 if (event
.Leaving() && m_pInTool
)
1135 if (event
.RightDown())
1137 // For now, we don't have an id. Later we could
1138 // try finding the tool.
1139 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1147 void wxToolBar::HandleMouseMove(WXWPARAM wParam
, WXLPARAM lParam
)
1149 wxCoord x
= GET_X_LPARAM(lParam
),
1150 y
= GET_Y_LPARAM(lParam
);
1151 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1153 // cursor left current tool
1154 if( tool
!= m_pInTool
&& !tool
)
1160 // cursor entered a tool
1161 if( tool
!= m_pInTool
&& tool
)
1164 OnMouseEnter( tool
->GetId() );
1168 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1174 if ( HandleSize(wParam
, lParam
) )
1179 // we don't handle mouse moves, so always pass the message to
1180 // wxControl::MSWWindowProc
1181 HandleMouseMove(wParam
, lParam
);
1185 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1188 // ----------------------------------------------------------------------------
1189 // private functions
1190 // ----------------------------------------------------------------------------
1192 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1198 wxLogLastError(_T("CreateCompatibleDC"));
1203 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1207 wxLogLastError(_T("SelectObject"));
1212 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1214 for ( int i
= 0; i
< width
; i
++ )
1216 for ( int j
= 0; j
< height
; j
++ )
1218 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1220 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1222 COLORREF col
= cmap
[k
].from
;
1223 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1224 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1225 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1227 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1236 // VZ: I leave here my attempts to map the bitmap to the system colours
1237 // faster by using BitBlt() even though it's broken currently - but
1238 // maybe someone else can finish it? It should be faster than iterating
1239 // over all pixels...
1241 MemoryHDC hdcMask
, hdcDst
;
1242 if ( !hdcMask
|| !hdcDst
)
1244 wxLogLastError(_T("CreateCompatibleDC"));
1249 // create the target bitmap
1250 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1253 wxLogLastError(_T("CreateCompatibleBitmap"));
1258 // create the monochrome mask bitmap
1259 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1262 wxLogLastError(_T("CreateBitmap(mono)"));
1264 ::DeleteObject(hbmpDst
);
1269 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1270 bmpInMask(hdcMask
, hbmpMask
);
1273 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1275 // create the mask for this colour
1276 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1277 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1279 // replace this colour with the target one in the dst bitmap
1280 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1281 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1283 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1286 MAKEROP4(PATCOPY
, SRCCOPY
));
1288 (void)::SelectObject(hdcDst
, hbrOld
);
1289 ::DeleteObject(hbr
);
1292 ::DeleteObject((HBITMAP
)bitmap
);
1294 return (WXHBITMAP
)hbmpDst
;
1298 #endif // wxUSE_TOOLBAR && Win95