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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
59 #include "wx/msw/winundef.h"
61 #if defined(__MWERKS__) && defined(__WXMSW__)
62 // including <windef.h> for max definition doesn't seem
63 // to work using CodeWarrior 6 Windows. So we define it
64 // here. (Otherwise we get a undefined identifier 'max'
65 // later on in this file.) (Added by dimitri@shortcut.nl)
67 # define max(a,b) (((a) > (b)) ? (a) : (b))
72 // ----------------------------------------------------------------------------
73 // conditional compilation
74 // ----------------------------------------------------------------------------
76 // wxWindows previously always considered that toolbar buttons have light grey
77 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
78 // doesn't work with XPMs which then appear to have black background. To make
79 // this work, we must respect the bitmap masks - which we do now. This should
80 // be ok in any case, but to restore 100% compatible with the old version
81 // behaviour, you can set this to 0.
82 #define USE_BITMAP_MASKS 1
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // these standard constants are not always defined in compilers headers
92 #define TBSTYLE_LIST 0x1000
93 #define TBSTYLE_FLAT 0x0800
96 #ifndef TBSTYLE_TRANSPARENT
97 #define TBSTYLE_TRANSPARENT 0x8000
100 #ifndef TBSTYLE_TOOLTIPS
101 #define TBSTYLE_TOOLTIPS 0x0100
106 #define TB_SETSTYLE (WM_USER + 56)
107 #define TB_GETSTYLE (WM_USER + 57)
111 #define TB_HITTEST (WM_USER + 69)
114 // these values correspond to those used by comctl32.dll
115 #define DEFAULTBITMAPX 16
116 #define DEFAULTBITMAPY 15
117 #define DEFAULTBUTTONX 24
118 #define DEFAULTBUTTONY 24
119 #define DEFAULTBARHEIGHT 27
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
127 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
128 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
129 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 class wxToolBarTool
: public wxToolBarToolBase
139 wxToolBarTool(wxToolBar
*tbar
,
141 const wxString
& label
,
142 const wxBitmap
& bmpNormal
,
143 const wxBitmap
& bmpDisabled
,
145 wxObject
*clientData
,
146 const wxString
& shortHelp
,
147 const wxString
& longHelp
)
148 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
149 clientData
, shortHelp
, longHelp
)
154 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
155 : wxToolBarToolBase(tbar
, control
)
160 virtual void SetLabel(const wxString
& label
)
162 if ( label
== m_label
)
165 wxToolBarToolBase::SetLabel(label
);
167 // we need to update the label shown in the toolbar because it has a
168 // pointer to the internal buffer of the old label
170 // TODO: use TB_SETBUTTONINFO
173 // set/get the number of separators which we use to cover the space used by
174 // a control in the toolbar
175 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
176 size_t GetSeparatorsCount() const { return m_nSepCount
; }
183 // ============================================================================
185 // ============================================================================
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
192 const wxString
& label
,
193 const wxBitmap
& bmpNormal
,
194 const wxBitmap
& bmpDisabled
,
196 wxObject
*clientData
,
197 const wxString
& shortHelp
,
198 const wxString
& longHelp
)
200 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
201 clientData
, shortHelp
, longHelp
);
204 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
206 return new wxToolBarTool(this, control
);
209 // ----------------------------------------------------------------------------
210 // wxToolBar construction
211 // ----------------------------------------------------------------------------
213 void wxToolBar::Init()
219 m_defaultWidth
= DEFAULTBITMAPX
;
220 m_defaultHeight
= DEFAULTBITMAPY
;
226 bool wxToolBar::Create(wxWindow
*parent
,
231 const wxString
& name
,
234 // common initialisation
235 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
238 // MSW-specific initialisation
239 if ( !MSWCreateToolbar(pos
, size
, menuBar
) )
242 // set up the colors and fonts
243 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
244 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
249 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
250 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
253 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
, wxMenuBar
* menuBar
)
257 m_menuBar
->SetToolBar(this);
259 // Create the menubar.
262 memset (&mbi
, 0, sizeof (SHMENUBARINFO
));
263 mbi
.cbSize
= sizeof (SHMENUBARINFO
);
264 mbi
.hwndParent
= (HWND
) GetParent()->GetHWND();
266 mbi
.nToolBarId
= 5002;
268 mbi
.nToolBarId
= 5000;
272 mbi
.dwFlags
= 0 ; // SHCMBF_EMPTYBAR;
273 mbi
.hInstRes
= wxGetInstance();
275 if (!SHCreateMenuBar(&mbi
))
277 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
281 SetHWND((WXHWND
) mbi
.hwndMB
);
283 if (!::SendMessage((HWND) GetHWND(), TB_DELETEBUTTON, 0, (LPARAM) 0))
285 wxLogLastError(wxT("TB_DELETEBUTTON"));
288 // install wxWindows window proc for this window
297 void wxToolBar::Recreate()
300 const HWND hwndOld
= GetHwnd();
303 // we haven't been created yet, no need to recreate
307 // get the position and size before unsubclassing the old toolbar
308 const wxPoint pos
= GetPosition();
309 const wxSize size
= GetSize();
313 if ( !MSWCreateToolbar(pos
, size
) )
316 wxFAIL_MSG( _T("recreating the toolbar failed") );
321 // reparent all our children under the new toolbar
322 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
324 node
= node
->GetNext() )
326 wxWindow
*win
= node
->GetData();
327 if ( !win
->IsTopLevel() )
328 ::SetParent(GetHwndOf(win
), GetHwnd());
331 // only destroy the old toolbar now -- after all the children had been
333 ::DestroyWindow(hwndOld
);
335 // it is for the old bitmap control and can't be used with the new one
338 ::DeleteObject((HBITMAP
) m_hBitmap
);
347 wxToolBar::~wxToolBar()
350 GetMenuBar()->SetToolBar(NULL
);
352 // we must refresh the frame size when the toolbar is deleted but the frame
353 // is not - otherwise toolbar leaves a hole in the place it used to occupy
354 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
355 if ( frame
&& !frame
->IsBeingDeleted() )
357 frame
->SendSizeEvent();
362 ::DeleteObject((HBITMAP
) m_hBitmap
);
366 wxSize
wxToolBar::DoGetBestSize() const
368 wxSize sizeBest
= GetToolSize();
369 sizeBest
.x
*= GetToolsCount();
371 // reverse horz and vertical components if necessary
372 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
375 // Return HMENU for the menu associated with the commandbar
376 WXHMENU
wxToolBar::GetHMenu()
380 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
387 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
389 // toolbars never have border, giving one to them results in broken
391 WXDWORD msStyle
= wxControl::MSWGetStyle
393 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
396 // always include this one, it never hurts and setting it later only if we
397 // do have tooltips wouldn't work
398 msStyle
|= TBSTYLE_TOOLTIPS
;
400 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
402 // static as it doesn't change during the program lifetime
403 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
405 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
406 // style with 6.00 (part of Windows XP) leads to the toolbar with
407 // incorrect background colour - and not using it still results in the
408 // correct (flat) toolbar, so don't use it there
409 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
411 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
414 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
416 msStyle
|= TBSTYLE_LIST
;
420 if ( style
& wxTB_NODIVIDER
)
421 msStyle
|= CCS_NODIVIDER
;
423 if ( style
& wxTB_NOALIGN
)
424 msStyle
|= CCS_NOPARENTALIGN
;
426 if ( style
& wxTB_VERTICAL
)
432 // ----------------------------------------------------------------------------
433 // adding/removing tools
434 // ----------------------------------------------------------------------------
436 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
438 // nothing special to do here - we really create the toolbar buttons in
445 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
447 // the main difficulty we have here is with the controls in the toolbars:
448 // as we (sometimes) use several separators to cover up the space used by
449 // them, the indices are not the same for us and the toolbar
451 // first determine the position of the first button to delete: it may be
452 // different from pos if we use several separators to cover the space used
454 wxToolBarToolsList::compatibility_iterator node
;
455 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
457 wxToolBarToolBase
*tool2
= node
->GetData();
460 // let node point to the next node in the list
461 node
= node
->GetNext();
466 if ( tool2
->IsControl() )
468 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
472 // now determine the number of buttons to delete and the area taken by them
473 size_t nButtonsToDelete
= 1;
475 // get the size of the button we're going to delete
477 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
479 wxLogLastError(_T("TB_GETITEMRECT"));
482 int width
= r
.right
- r
.left
;
484 if ( tool
->IsControl() )
486 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
488 width
*= nButtonsToDelete
;
491 // do delete all buttons
492 m_nButtons
-= nButtonsToDelete
;
493 while ( nButtonsToDelete
-- > 0 )
495 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
497 wxLogLastError(wxT("TB_DELETEBUTTON"));
505 // and finally reposition all the controls after this button (the toolbar
506 // takes care of all normal items)
507 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
509 wxToolBarToolBase
*tool2
= node
->GetData();
510 if ( tool2
->IsControl() )
513 wxControl
*control
= tool2
->GetControl();
514 control
->GetPosition(&x
, NULL
);
515 control
->Move(x
- width
, -1);
522 struct wxToolBarIdMapping
528 static wxToolBarIdMapping sm_ToolBarIdMappingArray
[] =
530 { wxID_COPY
, STD_COPY
},
531 { wxID_CUT
, STD_CUT
},
532 { wxID_FIND
, STD_FIND
},
533 { wxID_PASTE
, STD_PASTE
},
534 { wxID_NEW
, STD_FILENEW
},
535 { wxID_OPEN
, STD_FILEOPEN
},
536 { wxID_SAVE
, STD_FILESAVE
},
537 { wxID_PRINT
, STD_PRINT
},
538 { wxID_PREVIEW
, STD_PRINTPRE
},
539 { wxID_UNDO
, STD_UNDO
},
540 { wxID_REDO
, STD_REDOW
},
541 { wxID_HELP
, STD_HELP
},
542 { wxID_DELETE
, STD_DELETE
},
543 { wxID_REPLACE
, STD_REPLACE
},
544 { wxID_PROPERTIES
, STD_PROPERTIES
},
545 { wxID_VIEW_DETAILS
, VIEW_DETAILS
},
546 { wxID_VIEW_SORTDATE
, VIEW_SORTDATE
},
547 { wxID_VIEW_LARGEICONS
, VIEW_LARGEICONS
},
548 { wxID_VIEW_SORTNAME
, VIEW_SORTNAME
},
549 { wxID_VIEW_LIST
, VIEW_LIST
},
550 { wxID_VIEW_SORTSIZE
, VIEW_SORTSIZE
},
551 { wxID_VIEW_SMALLICONS
, VIEW_SMALLICONS
},
552 { wxID_VIEW_SORTTYPE
, VIEW_SORTTYPE
},
556 static int wxFindIdForWinceId(int id
)
561 if (sm_ToolBarIdMappingArray
[i
].m_winceId
== 0)
563 else if (sm_ToolBarIdMappingArray
[i
].m_winceId
== id
)
564 return sm_ToolBarIdMappingArray
[i
].m_wxwinId
;
570 static int wxFindIdForwxWinId(int id
)
575 if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== 0)
577 else if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== id
)
578 return sm_ToolBarIdMappingArray
[i
].m_winceId
;
585 bool wxToolBar::Realize()
587 const size_t nTools
= GetToolsCount();
594 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
597 // delete all old buttons, if any
598 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
600 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
602 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
607 // add the buttons and separators
608 // ------------------------------
610 // Use standard buttons
611 CommandBar_AddBitmap((HWND
) GetHWND(), HINST_COMMCTRL
,
612 IDB_STD_SMALL_COLOR
, 0, 16, 16);
614 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
616 // this array will hold the indices of all controls in the toolbar
617 wxArrayInt controlIds
;
619 bool lastWasRadio
= FALSE
;
621 wxToolBarToolsList::Node
* node
;
622 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
624 wxToolBarToolBase
*tool
= node
->GetData();
626 bool processedThis
= TRUE
;
628 TBBUTTON
& button
= buttons
[i
];
630 wxZeroMemory(button
);
632 bool isRadio
= FALSE
;
633 switch ( tool
->GetStyle() )
635 case wxTOOL_STYLE_CONTROL
:
636 button
.idCommand
= tool
->GetId();
637 // fall through: create just a separator too
639 case wxTOOL_STYLE_SEPARATOR
:
640 button
.fsState
= TBSTATE_ENABLED
;
641 button
.fsStyle
= TBSTYLE_SEP
;
644 case wxTOOL_STYLE_BUTTON
:
645 // if ( !HasFlag(wxTB_NOICONS) )
646 // button.iBitmap = bitmapId;
648 if ( HasFlag(wxTB_TEXT
) )
650 const wxString
& label
= tool
->GetLabel();
651 if ( !label
.empty() )
653 button
.iString
= (int)label
.c_str();
657 int winceId
= wxFindIdForwxWinId(tool
->GetId());
660 button
.idCommand
= tool
->GetId();
661 // if ( !HasFlag(wxTB_NOICONS) )
662 button
.iBitmap
= winceId
;
665 processedThis
= FALSE
;
667 if ( tool
->IsEnabled() )
668 button
.fsState
|= TBSTATE_ENABLED
;
669 if ( tool
->IsToggled() )
670 button
.fsState
|= TBSTATE_CHECKED
;
672 switch ( tool
->GetKind() )
675 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
679 // the first item in the radio group is checked by
680 // default to be consistent with wxGTK and the menu
682 button
.fsState
|= TBSTATE_CHECKED
;
691 button
.fsStyle
= TBSTYLE_CHECK
;
695 wxFAIL_MSG( _T("unexpected toolbar button kind") );
699 button
.fsStyle
= TBSTYLE_BUTTON
;
706 lastWasRadio
= isRadio
;
712 // Add buttons to Commandbar
713 if (!CommandBar_AddButtons(GetHwnd(), i
, buttons
))
715 wxLogLastError(wxT("CommandBar_AddButtons"));
721 // Deal with the controls finally
722 // ------------------------------
724 // adjust the controls size to fit nicely in the toolbar
727 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
729 wxToolBarToolBase
*tool
= node
->GetData();
731 // we calculate the running y coord for vertical toolbars so we need to
732 // get the items size for all items but for the horizontal ones we
733 // don't need to deal with the non controls
734 bool isControl
= tool
->IsControl();
735 if ( !isControl
&& !isVertical
)
738 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
739 // latter only appeared in v4.70 of comctl32.dll
741 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
742 index
, (LPARAM
)(LPRECT
)&r
) )
744 wxLogLastError(wxT("TB_GETITEMRECT"));
749 // can only be control if isVertical
750 y
+= r
.bottom
- r
.top
;
755 wxControl
*control
= tool
->GetControl();
757 wxSize size
= control
->GetSize();
759 // the position of the leftmost controls corner
762 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
763 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
764 // available in headers, now check whether it is available now
766 if ( wxTheApp
->GetComCtl32Version() >= 471 )
768 // set the (underlying) separators width to be that of the
771 tbbi
.cbSize
= sizeof(tbbi
);
772 tbbi
.dwMask
= TBIF_SIZE
;
774 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
775 tool
->GetId(), (LPARAM
)&tbbi
) )
777 // the id is probably invalid?
778 wxLogLastError(wxT("TB_SETBUTTONINFO"));
782 #endif // comctl32.dll 4.71
783 // TB_SETBUTTONINFO unavailable
785 // try adding several separators to fit the controls width
786 int widthSep
= r
.right
- r
.left
;
792 tbb
.fsState
= TBSTATE_ENABLED
;
793 tbb
.fsStyle
= TBSTYLE_SEP
;
795 size_t nSeparators
= size
.x
/ widthSep
;
796 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
798 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
799 index
, (LPARAM
)&tbb
) )
801 wxLogLastError(wxT("TB_INSERTBUTTON"));
807 // remember the number of separators we used - we'd have to
808 // delete all of them later
809 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
811 // adjust the controls width to exactly cover the separators
812 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
815 // position the control itself correctly vertically
816 int height
= r
.bottom
- r
.top
;
817 int diff
= height
- size
.y
;
820 // the control is too high, resize to fit
821 control
->SetSize(-1, height
- 2);
832 y
+= height
+ 2*GetMargins().y
;
834 else // horizontal toolbar
842 control
->Move(left
, top
+ (diff
+ 1) / 2);
845 // the max index is the "real" number of buttons - i.e. counting even the
846 // separators which we added just for aligning the controls
851 if ( m_maxRows
== 0 )
853 // if not set yet, only one row
857 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
859 if ( m_maxRows
== 0 )
861 // if not set yet, have one column
870 // ----------------------------------------------------------------------------
872 // ----------------------------------------------------------------------------
874 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
876 wxToolBarToolBase
*tool
= FindById((int)id
);
879 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
880 event
.SetEventObject(this);
884 return GetEventHandler()->ProcessEvent(event
);
887 if ( tool
->CanBeToggled() )
889 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
890 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
893 bool toggled
= tool
->IsToggled();
895 // avoid sending the event when a radio button is released, this is not
897 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
899 // OnLeftClick() can veto the button state change - for buttons which
900 // may be toggled only, of couse
901 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
905 tool
->SetToggle(toggled
);
907 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
914 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
916 WXLPARAM
*WXUNUSED(result
))
919 // First check if this applies to us
920 NMHDR
*hdr
= (NMHDR
*)lParam
;
922 // the tooltips control created by the toolbar is sometimes Unicode, even
923 // in an ANSI application - this seems to be a bug in comctl32.dll v5
924 UINT code
= hdr
->code
;
925 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
928 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
929 if ( toolTipWnd
!= hdr
->hwndFrom
)
932 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
933 int id
= (int)ttText
->hdr
.idFrom
;
935 wxToolBarToolBase
*tool
= FindById(id
);
939 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
945 // ----------------------------------------------------------------------------
947 // ----------------------------------------------------------------------------
949 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
951 wxToolBarBase::SetToolBitmapSize(size
);
953 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
956 void wxToolBar::SetRows(int nRows
)
958 if ( nRows
== m_maxRows
)
960 // avoid resizing the frame uselessly
964 // TRUE in wParam means to create at least as many rows, FALSE -
967 ::SendMessage(GetHwnd(), TB_SETROWS
,
968 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
976 // The button size is bigger than the bitmap size
977 wxSize
wxToolBar::GetToolSize() const
979 // TB_GETBUTTONSIZE is supported from version 4.70
980 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
981 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
982 if ( wxTheApp
->GetComCtl32Version() >= 470 )
984 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
986 return wxSize(LOWORD(dw
), HIWORD(dw
));
989 #endif // comctl32.dll 4.70+
992 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
997 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1000 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1002 for ( ; current
!= 0; current
= current
->GetNext() )
1005 return current
->GetData();
1007 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1008 size_t separators
= tool
->GetSeparatorsCount();
1010 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1011 // otherwise, skip as many items as the separator count, plus the
1013 index
-= separators
? separators
+ 1 : 1;
1019 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1024 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1025 // MBN: when the point ( x, y ) is close to the toolbar border
1026 // TB_HITTEST returns m_nButtons ( not -1 )
1027 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1029 // it's a separator or there is no tool at all there
1030 return (wxToolBarToolBase
*)NULL
;
1033 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1034 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1035 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1037 return m_tools
.Item((size_t)index
)->GetData();
1042 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1046 void wxToolBar::UpdateSize()
1048 // the toolbar size changed
1049 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1051 // we must also refresh the frame after the toolbar size (possibly) changed
1052 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1055 frame
->SendSizeEvent();
1059 // ----------------------------------------------------------------------------
1061 // ---------------------------------------------------------------------------
1063 void wxToolBar::SetWindowStyleFlag(long style
)
1065 // the style bits whose changes force us to recreate the toolbar
1066 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1068 const long styleOld
= GetWindowStyle();
1070 wxToolBarBase::SetWindowStyleFlag(style
);
1072 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1073 // also fatal as we'd then try to recreate the toolbar when it's just being
1075 if ( GetToolsCount() &&
1076 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1078 // to remove the text labels, simply re-realizing the toolbar is enough
1079 // but I don't know of any way to add the text to an existing toolbar
1080 // other than by recreating it entirely
1085 // ----------------------------------------------------------------------------
1087 // ----------------------------------------------------------------------------
1089 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1091 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1092 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1095 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1097 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1098 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1101 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1103 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1104 // without, so we really need to delete the button and recreate it here
1105 wxFAIL_MSG( _T("not implemented") );
1108 // ----------------------------------------------------------------------------
1110 // ----------------------------------------------------------------------------
1112 // Responds to colour changes, and passes event on to children.
1113 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1115 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1117 // Remap the buttons
1120 // Relayout the toolbar
1121 int nrows
= m_maxRows
;
1122 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1127 // let the event propagate further
1131 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1133 if (event
.Leaving() && m_pInTool
)
1140 if (event
.RightDown())
1142 // For now, we don't have an id. Later we could
1143 // try finding the tool.
1144 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1152 void wxToolBar::HandleMouseMove(WXWPARAM wParam
, WXLPARAM lParam
)
1154 wxCoord x
= GET_X_LPARAM(lParam
),
1155 y
= GET_Y_LPARAM(lParam
);
1156 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1158 // cursor left current tool
1159 if( tool
!= m_pInTool
&& !tool
)
1165 // cursor entered a tool
1166 if( tool
!= m_pInTool
&& tool
)
1169 OnMouseEnter( tool
->GetId() );
1173 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1179 if ( HandleSize(wParam
, lParam
) )
1184 // we don't handle mouse moves, so always pass the message to
1185 // wxControl::MSWWindowProc
1186 HandleMouseMove(wParam
, lParam
);
1190 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1193 // ----------------------------------------------------------------------------
1194 // private functions
1195 // ----------------------------------------------------------------------------
1197 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1203 wxLogLastError(_T("CreateCompatibleDC"));
1208 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1212 wxLogLastError(_T("SelectObject"));
1217 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1219 for ( int i
= 0; i
< width
; i
++ )
1221 for ( int j
= 0; j
< height
; j
++ )
1223 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1225 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1227 COLORREF col
= cmap
[k
].from
;
1228 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1229 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1230 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1232 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1241 // VZ: I leave here my attempts to map the bitmap to the system colours
1242 // faster by using BitBlt() even though it's broken currently - but
1243 // maybe someone else can finish it? It should be faster than iterating
1244 // over all pixels...
1246 MemoryHDC hdcMask
, hdcDst
;
1247 if ( !hdcMask
|| !hdcDst
)
1249 wxLogLastError(_T("CreateCompatibleDC"));
1254 // create the target bitmap
1255 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1258 wxLogLastError(_T("CreateCompatibleBitmap"));
1263 // create the monochrome mask bitmap
1264 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1267 wxLogLastError(_T("CreateBitmap(mono)"));
1269 ::DeleteObject(hbmpDst
);
1274 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1275 bmpInMask(hdcMask
, hbmpMask
);
1278 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1280 // create the mask for this colour
1281 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1282 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1284 // replace this colour with the target one in the dst bitmap
1285 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1286 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1288 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1291 MAKEROP4(PATCOPY
, SRCCOPY
));
1293 (void)::SelectObject(hdcDst
, hbrOld
);
1294 ::DeleteObject(hbr
);
1297 ::DeleteObject((HBITMAP
)bitmap
);
1299 return (WXHBITMAP
)hbmpDst
;
1303 #endif // wxUSE_TOOLBAR && Win95