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"
60 #include "wx/msw/wince/missing.h"
62 #include "wx/msw/winundef.h"
64 #if defined(__MWERKS__) && defined(__WXMSW__)
65 // including <windef.h> for max definition doesn't seem
66 // to work using CodeWarrior 6 Windows. So we define it
67 // here. (Otherwise we get a undefined identifier 'max'
68 // later on in this file.) (Added by dimitri@shortcut.nl)
70 # define max(a,b) (((a) > (b)) ? (a) : (b))
75 // ----------------------------------------------------------------------------
76 // conditional compilation
77 // ----------------------------------------------------------------------------
79 // wxWindows previously always considered that toolbar buttons have light grey
80 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
81 // doesn't work with XPMs which then appear to have black background. To make
82 // this work, we must respect the bitmap masks - which we do now. This should
83 // be ok in any case, but to restore 100% compatible with the old version
84 // behaviour, you can set this to 0.
85 #define USE_BITMAP_MASKS 1
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // these standard constants are not always defined in compilers headers
95 #define TBSTYLE_LIST 0x1000
96 #define TBSTYLE_FLAT 0x0800
99 #ifndef TBSTYLE_TRANSPARENT
100 #define TBSTYLE_TRANSPARENT 0x8000
103 #ifndef TBSTYLE_TOOLTIPS
104 #define TBSTYLE_TOOLTIPS 0x0100
109 #define TB_SETSTYLE (WM_USER + 56)
110 #define TB_GETSTYLE (WM_USER + 57)
114 #define TB_HITTEST (WM_USER + 69)
117 // these values correspond to those used by comctl32.dll
118 #define DEFAULTBITMAPX 16
119 #define DEFAULTBITMAPY 15
120 #define DEFAULTBUTTONX 24
121 #define DEFAULTBUTTONY 24
122 #define DEFAULTBARHEIGHT 27
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
130 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
131 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
132 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 class wxToolBarTool
: public wxToolBarToolBase
142 wxToolBarTool(wxToolBar
*tbar
,
144 const wxString
& label
,
145 const wxBitmap
& bmpNormal
,
146 const wxBitmap
& bmpDisabled
,
148 wxObject
*clientData
,
149 const wxString
& shortHelp
,
150 const wxString
& longHelp
)
151 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
152 clientData
, shortHelp
, longHelp
)
157 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
158 : wxToolBarToolBase(tbar
, control
)
163 virtual void SetLabel(const wxString
& label
)
165 if ( label
== m_label
)
168 wxToolBarToolBase::SetLabel(label
);
170 // we need to update the label shown in the toolbar because it has a
171 // pointer to the internal buffer of the old label
173 // TODO: use TB_SETBUTTONINFO
176 // set/get the number of separators which we use to cover the space used by
177 // a control in the toolbar
178 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
179 size_t GetSeparatorsCount() const { return m_nSepCount
; }
186 // ============================================================================
188 // ============================================================================
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
195 const wxString
& label
,
196 const wxBitmap
& bmpNormal
,
197 const wxBitmap
& bmpDisabled
,
199 wxObject
*clientData
,
200 const wxString
& shortHelp
,
201 const wxString
& longHelp
)
203 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
204 clientData
, shortHelp
, longHelp
);
207 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
209 return new wxToolBarTool(this, control
);
212 // ----------------------------------------------------------------------------
213 // wxToolBar construction
214 // ----------------------------------------------------------------------------
216 void wxToolBar::Init()
222 m_defaultWidth
= DEFAULTBITMAPX
;
223 m_defaultHeight
= DEFAULTBITMAPY
;
229 bool wxToolBar::Create(wxWindow
*parent
,
234 const wxString
& name
,
237 // common initialisation
238 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
241 // MSW-specific initialisation
242 if ( !MSWCreateToolbar(pos
, size
, menuBar
) )
245 // set up the colors and fonts
246 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
247 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
252 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
253 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
256 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
, wxMenuBar
* menuBar
)
260 m_menuBar
->SetToolBar(this);
262 #if _WIN32_WCE >= 400
263 HWND hWnd
= CommandBar_Create(wxGetInstance(), (HWND
) GetParent()->GetHWND(), GetId());
264 SetHWND((WXHWND
) hWnd
);
266 // Create the menubar.
269 memset (&mbi
, 0, sizeof (SHMENUBARINFO
));
270 mbi
.cbSize
= sizeof (SHMENUBARINFO
);
271 mbi
.hwndParent
= (HWND
) GetParent()->GetHWND();
273 mbi
.nToolBarId
= 5002;
275 mbi
.nToolBarId
= 5000;
279 mbi
.dwFlags
= 0 ; // SHCMBF_EMPTYBAR;
280 mbi
.hInstRes
= wxGetInstance();
282 if (!SHCreateMenuBar(&mbi
))
284 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
288 SetHWND((WXHWND
) mbi
.hwndMB
);
291 // install wxWindows window proc for this window
300 void wxToolBar::Recreate()
303 const HWND hwndOld
= GetHwnd();
306 // we haven't been created yet, no need to recreate
310 // get the position and size before unsubclassing the old toolbar
311 const wxPoint pos
= GetPosition();
312 const wxSize size
= GetSize();
316 if ( !MSWCreateToolbar(pos
, size
) )
319 wxFAIL_MSG( _T("recreating the toolbar failed") );
324 // reparent all our children under the new toolbar
325 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
327 node
= node
->GetNext() )
329 wxWindow
*win
= node
->GetData();
330 if ( !win
->IsTopLevel() )
331 ::SetParent(GetHwndOf(win
), GetHwnd());
334 // only destroy the old toolbar now -- after all the children had been
336 ::DestroyWindow(hwndOld
);
338 // it is for the old bitmap control and can't be used with the new one
341 ::DeleteObject((HBITMAP
) m_hBitmap
);
350 wxToolBar::~wxToolBar()
353 GetMenuBar()->SetToolBar(NULL
);
355 // we must refresh the frame size when the toolbar is deleted but the frame
356 // is not - otherwise toolbar leaves a hole in the place it used to occupy
357 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
358 if ( frame
&& !frame
->IsBeingDeleted() )
360 frame
->SendSizeEvent();
365 ::DeleteObject((HBITMAP
) m_hBitmap
);
369 wxSize
wxToolBar::DoGetBestSize() const
371 wxSize sizeBest
= GetToolSize();
372 sizeBest
.x
*= GetToolsCount();
374 // reverse horz and vertical components if necessary
375 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
378 // Return HMENU for the menu associated with the commandbar
379 WXHMENU
wxToolBar::GetHMenu()
383 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
390 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
392 // toolbars never have border, giving one to them results in broken
394 WXDWORD msStyle
= wxControl::MSWGetStyle
396 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
399 // always include this one, it never hurts and setting it later only if we
400 // do have tooltips wouldn't work
401 msStyle
|= TBSTYLE_TOOLTIPS
;
403 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
405 // static as it doesn't change during the program lifetime
406 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
408 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
409 // style with 6.00 (part of Windows XP) leads to the toolbar with
410 // incorrect background colour - and not using it still results in the
411 // correct (flat) toolbar, so don't use it there
412 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
414 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
417 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
419 msStyle
|= TBSTYLE_LIST
;
423 if ( style
& wxTB_NODIVIDER
)
424 msStyle
|= CCS_NODIVIDER
;
426 if ( style
& wxTB_NOALIGN
)
427 msStyle
|= CCS_NOPARENTALIGN
;
429 if ( style
& wxTB_VERTICAL
)
435 // ----------------------------------------------------------------------------
436 // adding/removing tools
437 // ----------------------------------------------------------------------------
439 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
441 // nothing special to do here - we really create the toolbar buttons in
448 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
450 // the main difficulty we have here is with the controls in the toolbars:
451 // as we (sometimes) use several separators to cover up the space used by
452 // them, the indices are not the same for us and the toolbar
454 // first determine the position of the first button to delete: it may be
455 // different from pos if we use several separators to cover the space used
457 wxToolBarToolsList::compatibility_iterator node
;
458 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
460 wxToolBarToolBase
*tool2
= node
->GetData();
463 // let node point to the next node in the list
464 node
= node
->GetNext();
469 if ( tool2
->IsControl() )
471 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
475 // now determine the number of buttons to delete and the area taken by them
476 size_t nButtonsToDelete
= 1;
478 // get the size of the button we're going to delete
480 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
482 wxLogLastError(_T("TB_GETITEMRECT"));
485 int width
= r
.right
- r
.left
;
487 if ( tool
->IsControl() )
489 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
491 width
*= nButtonsToDelete
;
494 // do delete all buttons
495 m_nButtons
-= nButtonsToDelete
;
496 while ( nButtonsToDelete
-- > 0 )
498 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
500 wxLogLastError(wxT("TB_DELETEBUTTON"));
508 // and finally reposition all the controls after this button (the toolbar
509 // takes care of all normal items)
510 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
512 wxToolBarToolBase
*tool2
= node
->GetData();
513 if ( tool2
->IsControl() )
516 wxControl
*control
= tool2
->GetControl();
517 control
->GetPosition(&x
, NULL
);
518 control
->Move(x
- width
, -1);
525 struct wxToolBarIdMapping
531 static wxToolBarIdMapping sm_ToolBarIdMappingArray
[] =
533 { wxID_COPY
, STD_COPY
},
534 { wxID_CUT
, STD_CUT
},
535 { wxID_FIND
, STD_FIND
},
536 { wxID_PASTE
, STD_PASTE
},
537 { wxID_NEW
, STD_FILENEW
},
538 { wxID_OPEN
, STD_FILEOPEN
},
539 { wxID_SAVE
, STD_FILESAVE
},
540 { wxID_PRINT
, STD_PRINT
},
541 { wxID_PREVIEW
, STD_PRINTPRE
},
542 { wxID_UNDO
, STD_UNDO
},
543 { wxID_REDO
, STD_REDOW
},
544 { wxID_HELP
, STD_HELP
},
545 { wxID_DELETE
, STD_DELETE
},
546 { wxID_REPLACE
, STD_REPLACE
},
547 { wxID_PROPERTIES
, STD_PROPERTIES
},
548 { wxID_VIEW_DETAILS
, VIEW_DETAILS
},
549 { wxID_VIEW_SORTDATE
, VIEW_SORTDATE
},
550 { wxID_VIEW_LARGEICONS
, VIEW_LARGEICONS
},
551 { wxID_VIEW_SORTNAME
, VIEW_SORTNAME
},
552 { wxID_VIEW_LIST
, VIEW_LIST
},
553 { wxID_VIEW_SORTSIZE
, VIEW_SORTSIZE
},
554 { wxID_VIEW_SMALLICONS
, VIEW_SMALLICONS
},
555 { wxID_VIEW_SORTTYPE
, VIEW_SORTTYPE
},
559 static int wxFindIdForWinceId(int id
)
564 if (sm_ToolBarIdMappingArray
[i
].m_winceId
== 0)
566 else if (sm_ToolBarIdMappingArray
[i
].m_winceId
== id
)
567 return sm_ToolBarIdMappingArray
[i
].m_wxwinId
;
573 static int wxFindIdForwxWinId(int id
)
578 if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== 0)
580 else if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== id
)
581 return sm_ToolBarIdMappingArray
[i
].m_winceId
;
588 bool wxToolBar::Realize()
590 const size_t nTools
= GetToolsCount();
597 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
600 // delete all old buttons, if any
601 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
603 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
605 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
610 // add the buttons and separators
611 // ------------------------------
613 // Use standard buttons
614 CommandBar_AddBitmap((HWND
) GetHWND(), HINST_COMMCTRL
,
615 IDB_STD_SMALL_COLOR
, 0, 16, 16);
617 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
619 // this array will hold the indices of all controls in the toolbar
620 wxArrayInt controlIds
;
622 bool lastWasRadio
= FALSE
;
624 wxToolBarToolsList::Node
* node
;
625 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
627 wxToolBarToolBase
*tool
= node
->GetData();
629 bool processedThis
= TRUE
;
631 TBBUTTON
& button
= buttons
[i
];
633 wxZeroMemory(button
);
635 bool isRadio
= FALSE
;
636 switch ( tool
->GetStyle() )
638 case wxTOOL_STYLE_CONTROL
:
639 button
.idCommand
= tool
->GetId();
640 // fall through: create just a separator too
642 case wxTOOL_STYLE_SEPARATOR
:
643 button
.fsState
= TBSTATE_ENABLED
;
644 button
.fsStyle
= TBSTYLE_SEP
;
647 case wxTOOL_STYLE_BUTTON
:
648 // if ( !HasFlag(wxTB_NOICONS) )
649 // button.iBitmap = bitmapId;
651 if ( HasFlag(wxTB_TEXT
) )
653 const wxString
& label
= tool
->GetLabel();
654 if ( !label
.empty() )
656 button
.iString
= (int)label
.c_str();
660 int winceId
= wxFindIdForwxWinId(tool
->GetId());
663 button
.idCommand
= tool
->GetId();
664 // if ( !HasFlag(wxTB_NOICONS) )
665 button
.iBitmap
= winceId
;
668 processedThis
= FALSE
;
670 if ( tool
->IsEnabled() )
671 button
.fsState
|= TBSTATE_ENABLED
;
672 if ( tool
->IsToggled() )
673 button
.fsState
|= TBSTATE_CHECKED
;
675 switch ( tool
->GetKind() )
678 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
682 // the first item in the radio group is checked by
683 // default to be consistent with wxGTK and the menu
685 button
.fsState
|= TBSTATE_CHECKED
;
694 button
.fsStyle
= TBSTYLE_CHECK
;
698 wxFAIL_MSG( _T("unexpected toolbar button kind") );
702 button
.fsStyle
= TBSTYLE_BUTTON
;
709 lastWasRadio
= isRadio
;
715 // Add buttons to Commandbar
716 if (!CommandBar_AddButtons(GetHwnd(), i
, buttons
))
718 wxLogLastError(wxT("CommandBar_AddButtons"));
724 // Deal with the controls finally
725 // ------------------------------
727 // adjust the controls size to fit nicely in the toolbar
730 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
732 wxToolBarToolBase
*tool
= node
->GetData();
734 // we calculate the running y coord for vertical toolbars so we need to
735 // get the items size for all items but for the horizontal ones we
736 // don't need to deal with the non controls
737 bool isControl
= tool
->IsControl();
738 if ( !isControl
&& !isVertical
)
741 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
742 // latter only appeared in v4.70 of comctl32.dll
744 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
745 index
, (LPARAM
)(LPRECT
)&r
) )
747 wxLogLastError(wxT("TB_GETITEMRECT"));
752 // can only be control if isVertical
753 y
+= r
.bottom
- r
.top
;
758 wxControl
*control
= tool
->GetControl();
760 wxSize size
= control
->GetSize();
762 // the position of the leftmost controls corner
765 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
766 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
767 // available in headers, now check whether it is available now
769 if ( wxTheApp
->GetComCtl32Version() >= 471 )
771 // set the (underlying) separators width to be that of the
774 tbbi
.cbSize
= sizeof(tbbi
);
775 tbbi
.dwMask
= TBIF_SIZE
;
777 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
778 tool
->GetId(), (LPARAM
)&tbbi
) )
780 // the id is probably invalid?
781 wxLogLastError(wxT("TB_SETBUTTONINFO"));
785 #endif // comctl32.dll 4.71
786 // TB_SETBUTTONINFO unavailable
788 // try adding several separators to fit the controls width
789 int widthSep
= r
.right
- r
.left
;
795 tbb
.fsState
= TBSTATE_ENABLED
;
796 tbb
.fsStyle
= TBSTYLE_SEP
;
798 size_t nSeparators
= size
.x
/ widthSep
;
799 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
801 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
802 index
, (LPARAM
)&tbb
) )
804 wxLogLastError(wxT("TB_INSERTBUTTON"));
810 // remember the number of separators we used - we'd have to
811 // delete all of them later
812 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
814 // adjust the controls width to exactly cover the separators
815 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
818 // position the control itself correctly vertically
819 int height
= r
.bottom
- r
.top
;
820 int diff
= height
- size
.y
;
823 // the control is too high, resize to fit
824 control
->SetSize(-1, height
- 2);
835 y
+= height
+ 2*GetMargins().y
;
837 else // horizontal toolbar
845 control
->Move(left
, top
+ (diff
+ 1) / 2);
848 // the max index is the "real" number of buttons - i.e. counting even the
849 // separators which we added just for aligning the controls
854 if ( m_maxRows
== 0 )
856 // if not set yet, only one row
860 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
862 if ( m_maxRows
== 0 )
864 // if not set yet, have one column
873 // ----------------------------------------------------------------------------
875 // ----------------------------------------------------------------------------
877 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
879 wxToolBarToolBase
*tool
= FindById((int)id
);
882 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
883 event
.SetEventObject(this);
887 return GetEventHandler()->ProcessEvent(event
);
890 if ( tool
->CanBeToggled() )
892 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
893 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
896 bool toggled
= tool
->IsToggled();
898 // avoid sending the event when a radio button is released, this is not
900 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
902 // OnLeftClick() can veto the button state change - for buttons which
903 // may be toggled only, of couse
904 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
908 tool
->SetToggle(toggled
);
910 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
917 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
919 WXLPARAM
*WXUNUSED(result
))
922 // First check if this applies to us
923 NMHDR
*hdr
= (NMHDR
*)lParam
;
925 // the tooltips control created by the toolbar is sometimes Unicode, even
926 // in an ANSI application - this seems to be a bug in comctl32.dll v5
927 UINT code
= hdr
->code
;
928 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
931 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
932 if ( toolTipWnd
!= hdr
->hwndFrom
)
935 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
936 int id
= (int)ttText
->hdr
.idFrom
;
938 wxToolBarToolBase
*tool
= FindById(id
);
942 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
948 // ----------------------------------------------------------------------------
950 // ----------------------------------------------------------------------------
952 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
954 wxToolBarBase::SetToolBitmapSize(size
);
956 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
959 void wxToolBar::SetRows(int nRows
)
961 if ( nRows
== m_maxRows
)
963 // avoid resizing the frame uselessly
967 // TRUE in wParam means to create at least as many rows, FALSE -
970 ::SendMessage(GetHwnd(), TB_SETROWS
,
971 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
979 // The button size is bigger than the bitmap size
980 wxSize
wxToolBar::GetToolSize() const
982 // TB_GETBUTTONSIZE is supported from version 4.70
983 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
984 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
985 if ( wxTheApp
->GetComCtl32Version() >= 470 )
987 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
989 return wxSize(LOWORD(dw
), HIWORD(dw
));
992 #endif // comctl32.dll 4.70+
995 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1000 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1003 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1005 for ( ; current
!= 0; current
= current
->GetNext() )
1008 return current
->GetData();
1010 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1011 size_t separators
= tool
->GetSeparatorsCount();
1013 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1014 // otherwise, skip as many items as the separator count, plus the
1016 index
-= separators
? separators
+ 1 : 1;
1022 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1027 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1028 // MBN: when the point ( x, y ) is close to the toolbar border
1029 // TB_HITTEST returns m_nButtons ( not -1 )
1030 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1032 // it's a separator or there is no tool at all there
1033 return (wxToolBarToolBase
*)NULL
;
1036 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1037 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1038 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1040 return m_tools
.Item((size_t)index
)->GetData();
1045 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1049 void wxToolBar::UpdateSize()
1051 // the toolbar size changed
1052 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1054 // we must also refresh the frame after the toolbar size (possibly) changed
1055 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1058 frame
->SendSizeEvent();
1062 // ----------------------------------------------------------------------------
1064 // ---------------------------------------------------------------------------
1066 void wxToolBar::SetWindowStyleFlag(long style
)
1068 // the style bits whose changes force us to recreate the toolbar
1069 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1071 const long styleOld
= GetWindowStyle();
1073 wxToolBarBase::SetWindowStyleFlag(style
);
1075 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1076 // also fatal as we'd then try to recreate the toolbar when it's just being
1078 if ( GetToolsCount() &&
1079 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1081 // to remove the text labels, simply re-realizing the toolbar is enough
1082 // but I don't know of any way to add the text to an existing toolbar
1083 // other than by recreating it entirely
1088 // ----------------------------------------------------------------------------
1090 // ----------------------------------------------------------------------------
1092 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1094 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1095 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1098 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1100 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1101 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1104 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1106 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1107 // without, so we really need to delete the button and recreate it here
1108 wxFAIL_MSG( _T("not implemented") );
1111 // ----------------------------------------------------------------------------
1113 // ----------------------------------------------------------------------------
1115 // Responds to colour changes, and passes event on to children.
1116 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1118 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1120 // Remap the buttons
1123 // Relayout the toolbar
1124 int nrows
= m_maxRows
;
1125 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1130 // let the event propagate further
1134 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1136 if (event
.Leaving() && m_pInTool
)
1143 if (event
.RightDown())
1145 // For now, we don't have an id. Later we could
1146 // try finding the tool.
1147 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1155 void wxToolBar::HandleMouseMove(WXWPARAM wParam
, WXLPARAM lParam
)
1157 wxCoord x
= GET_X_LPARAM(lParam
),
1158 y
= GET_Y_LPARAM(lParam
);
1159 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1161 // cursor left current tool
1162 if( tool
!= m_pInTool
&& !tool
)
1168 // cursor entered a tool
1169 if( tool
!= m_pInTool
&& tool
)
1172 OnMouseEnter( tool
->GetId() );
1176 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1182 if ( HandleSize(wParam
, lParam
) )
1187 // we don't handle mouse moves, so always pass the message to
1188 // wxControl::MSWWindowProc
1189 HandleMouseMove(wParam
, lParam
);
1193 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1196 // ----------------------------------------------------------------------------
1197 // private functions
1198 // ----------------------------------------------------------------------------
1200 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1206 wxLogLastError(_T("CreateCompatibleDC"));
1211 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1215 wxLogLastError(_T("SelectObject"));
1220 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1222 for ( int i
= 0; i
< width
; i
++ )
1224 for ( int j
= 0; j
< height
; j
++ )
1226 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1228 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1230 COLORREF col
= cmap
[k
].from
;
1231 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1232 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1233 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1235 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1244 // VZ: I leave here my attempts to map the bitmap to the system colours
1245 // faster by using BitBlt() even though it's broken currently - but
1246 // maybe someone else can finish it? It should be faster than iterating
1247 // over all pixels...
1249 MemoryHDC hdcMask
, hdcDst
;
1250 if ( !hdcMask
|| !hdcDst
)
1252 wxLogLastError(_T("CreateCompatibleDC"));
1257 // create the target bitmap
1258 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1261 wxLogLastError(_T("CreateCompatibleBitmap"));
1266 // create the monochrome mask bitmap
1267 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1270 wxLogLastError(_T("CreateBitmap(mono)"));
1272 ::DeleteObject(hbmpDst
);
1277 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1278 bmpInMask(hdcMask
, hbmpMask
);
1281 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1283 // create the mask for this colour
1284 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1285 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1287 // replace this colour with the target one in the dst bitmap
1288 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1289 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1291 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1294 MAKEROP4(PATCOPY
, SRCCOPY
));
1296 (void)::SelectObject(hdcDst
, hbrOld
);
1297 ::DeleteObject(hbr
);
1300 ::DeleteObject((HBITMAP
)bitmap
);
1302 return (WXHBITMAP
)hbmpDst
;
1306 #endif // wxUSE_TOOLBAR && Win95