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 // Use the WinCE-specific toolbar only if we're either compiling
43 // with a WinCE earlier than 4, or we wish to emulate a PocketPC-style UI
44 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (_WIN32_WCE < 400 || wxUSE_POCKETPC_UI)
46 #include "wx/toolbar.h"
48 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
52 #include "wx/msw/private.h"
62 #include "wx/msw/wince/missing.h"
64 #include "wx/msw/winundef.h"
66 #if defined(__MWERKS__) && defined(__WXMSW__)
67 // including <windef.h> for max definition doesn't seem
68 // to work using CodeWarrior 6 Windows. So we define it
69 // here. (Otherwise we get a undefined identifier 'max'
70 // later on in this file.) (Added by dimitri@shortcut.nl)
72 # define max(a,b) (((a) > (b)) ? (a) : (b))
77 // ----------------------------------------------------------------------------
78 // conditional compilation
79 // ----------------------------------------------------------------------------
81 // wxWindows previously always considered that toolbar buttons have light grey
82 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
83 // doesn't work with XPMs which then appear to have black background. To make
84 // this work, we must respect the bitmap masks - which we do now. This should
85 // be ok in any case, but to restore 100% compatible with the old version
86 // behaviour, you can set this to 0.
87 #define USE_BITMAP_MASKS 1
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // these standard constants are not always defined in compilers headers
97 #define TBSTYLE_LIST 0x1000
98 #define TBSTYLE_FLAT 0x0800
101 #ifndef TBSTYLE_TRANSPARENT
102 #define TBSTYLE_TRANSPARENT 0x8000
105 #ifndef TBSTYLE_TOOLTIPS
106 #define TBSTYLE_TOOLTIPS 0x0100
111 #define TB_SETSTYLE (WM_USER + 56)
112 #define TB_GETSTYLE (WM_USER + 57)
116 #define TB_HITTEST (WM_USER + 69)
119 // these values correspond to those used by comctl32.dll
120 #define DEFAULTBITMAPX 16
121 #define DEFAULTBITMAPY 15
122 #define DEFAULTBUTTONX 24
123 #define DEFAULTBUTTONY 24
124 #define DEFAULTBARHEIGHT 27
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
132 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
133 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
134 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 class wxToolBarTool
: public wxToolBarToolBase
144 wxToolBarTool(wxToolBar
*tbar
,
146 const wxString
& label
,
147 const wxBitmap
& bmpNormal
,
148 const wxBitmap
& bmpDisabled
,
150 wxObject
*clientData
,
151 const wxString
& shortHelp
,
152 const wxString
& longHelp
)
153 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
154 clientData
, shortHelp
, longHelp
)
159 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
160 : wxToolBarToolBase(tbar
, control
)
165 virtual void SetLabel(const wxString
& label
)
167 if ( label
== m_label
)
170 wxToolBarToolBase::SetLabel(label
);
172 // we need to update the label shown in the toolbar because it has a
173 // pointer to the internal buffer of the old label
175 // TODO: use TB_SETBUTTONINFO
178 // set/get the number of separators which we use to cover the space used by
179 // a control in the toolbar
180 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
181 size_t GetSeparatorsCount() const { return m_nSepCount
; }
188 // ============================================================================
190 // ============================================================================
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
197 const wxString
& label
,
198 const wxBitmap
& bmpNormal
,
199 const wxBitmap
& bmpDisabled
,
201 wxObject
*clientData
,
202 const wxString
& shortHelp
,
203 const wxString
& longHelp
)
205 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
206 clientData
, shortHelp
, longHelp
);
209 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
211 return new wxToolBarTool(this, control
);
214 // ----------------------------------------------------------------------------
215 // wxToolBar construction
216 // ----------------------------------------------------------------------------
218 void wxToolBar::Init()
224 m_defaultWidth
= DEFAULTBITMAPX
;
225 m_defaultHeight
= DEFAULTBITMAPY
;
231 bool wxToolBar::Create(wxWindow
*parent
,
236 const wxString
& name
,
239 // common initialisation
240 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
243 // MSW-specific initialisation
244 if ( !MSWCreateToolbar(pos
, size
, menuBar
) )
247 // set up the colors and fonts
248 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
249 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
254 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
255 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
258 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
, wxMenuBar
* menuBar
)
262 m_menuBar
->SetToolBar(this);
264 #if _WIN32_WCE >= 400
265 HWND hWnd
= CommandBar_Create(wxGetInstance(), (HWND
) GetParent()->GetHWND(), GetId());
266 SetHWND((WXHWND
) hWnd
);
268 // Create the menubar.
271 memset (&mbi
, 0, sizeof (SHMENUBARINFO
));
272 mbi
.cbSize
= sizeof (SHMENUBARINFO
);
273 mbi
.hwndParent
= (HWND
) GetParent()->GetHWND();
275 mbi
.nToolBarId
= 5002;
277 mbi
.nToolBarId
= 5000;
281 mbi
.dwFlags
= 0 ; // SHCMBF_EMPTYBAR;
282 mbi
.hInstRes
= wxGetInstance();
284 if (!SHCreateMenuBar(&mbi
))
286 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
290 SetHWND((WXHWND
) mbi
.hwndMB
);
293 // install wxWindows window proc for this window
302 void wxToolBar::Recreate()
305 const HWND hwndOld
= GetHwnd();
308 // we haven't been created yet, no need to recreate
312 // get the position and size before unsubclassing the old toolbar
313 const wxPoint pos
= GetPosition();
314 const wxSize size
= GetSize();
318 if ( !MSWCreateToolbar(pos
, size
) )
321 wxFAIL_MSG( _T("recreating the toolbar failed") );
326 // reparent all our children under the new toolbar
327 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
329 node
= node
->GetNext() )
331 wxWindow
*win
= node
->GetData();
332 if ( !win
->IsTopLevel() )
333 ::SetParent(GetHwndOf(win
), GetHwnd());
336 // only destroy the old toolbar now -- after all the children had been
338 ::DestroyWindow(hwndOld
);
340 // it is for the old bitmap control and can't be used with the new one
343 ::DeleteObject((HBITMAP
) m_hBitmap
);
352 wxToolBar::~wxToolBar()
355 GetMenuBar()->SetToolBar(NULL
);
357 // we must refresh the frame size when the toolbar is deleted but the frame
358 // is not - otherwise toolbar leaves a hole in the place it used to occupy
359 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
360 if ( frame
&& !frame
->IsBeingDeleted() )
362 frame
->SendSizeEvent();
367 ::DeleteObject((HBITMAP
) m_hBitmap
);
371 wxSize
wxToolBar::DoGetBestSize() const
373 wxSize sizeBest
= GetToolSize();
374 sizeBest
.x
*= GetToolsCount();
376 // reverse horz and vertical components if necessary
377 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
380 // Return HMENU for the menu associated with the commandbar
381 WXHMENU
wxToolBar::GetHMenu()
385 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
392 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
394 // toolbars never have border, giving one to them results in broken
396 WXDWORD msStyle
= wxControl::MSWGetStyle
398 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
401 // always include this one, it never hurts and setting it later only if we
402 // do have tooltips wouldn't work
403 msStyle
|= TBSTYLE_TOOLTIPS
;
405 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
407 // static as it doesn't change during the program lifetime
408 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
410 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
411 // style with 6.00 (part of Windows XP) leads to the toolbar with
412 // incorrect background colour - and not using it still results in the
413 // correct (flat) toolbar, so don't use it there
414 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
416 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
419 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
421 msStyle
|= TBSTYLE_LIST
;
425 if ( style
& wxTB_NODIVIDER
)
426 msStyle
|= CCS_NODIVIDER
;
428 if ( style
& wxTB_NOALIGN
)
429 msStyle
|= CCS_NOPARENTALIGN
;
431 if ( style
& wxTB_VERTICAL
)
437 // ----------------------------------------------------------------------------
438 // adding/removing tools
439 // ----------------------------------------------------------------------------
441 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
443 // nothing special to do here - we really create the toolbar buttons in
450 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
452 // the main difficulty we have here is with the controls in the toolbars:
453 // as we (sometimes) use several separators to cover up the space used by
454 // them, the indices are not the same for us and the toolbar
456 // first determine the position of the first button to delete: it may be
457 // different from pos if we use several separators to cover the space used
459 wxToolBarToolsList::compatibility_iterator node
;
460 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
462 wxToolBarToolBase
*tool2
= node
->GetData();
465 // let node point to the next node in the list
466 node
= node
->GetNext();
471 if ( tool2
->IsControl() )
473 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
477 // now determine the number of buttons to delete and the area taken by them
478 size_t nButtonsToDelete
= 1;
480 // get the size of the button we're going to delete
482 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
484 wxLogLastError(_T("TB_GETITEMRECT"));
487 int width
= r
.right
- r
.left
;
489 if ( tool
->IsControl() )
491 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
493 width
*= nButtonsToDelete
;
496 // do delete all buttons
497 m_nButtons
-= nButtonsToDelete
;
498 while ( nButtonsToDelete
-- > 0 )
500 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
502 wxLogLastError(wxT("TB_DELETEBUTTON"));
510 // and finally reposition all the controls after this button (the toolbar
511 // takes care of all normal items)
512 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
514 wxToolBarToolBase
*tool2
= node
->GetData();
515 if ( tool2
->IsControl() )
518 wxControl
*control
= tool2
->GetControl();
519 control
->GetPosition(&x
, NULL
);
520 control
->Move(x
- width
, -1);
527 struct wxToolBarIdMapping
533 static wxToolBarIdMapping sm_ToolBarIdMappingArray
[] =
535 { wxID_COPY
, STD_COPY
},
536 { wxID_CUT
, STD_CUT
},
537 { wxID_FIND
, STD_FIND
},
538 { wxID_PASTE
, STD_PASTE
},
539 { wxID_NEW
, STD_FILENEW
},
540 { wxID_OPEN
, STD_FILEOPEN
},
541 { wxID_SAVE
, STD_FILESAVE
},
542 { wxID_PRINT
, STD_PRINT
},
543 { wxID_PREVIEW
, STD_PRINTPRE
},
544 { wxID_UNDO
, STD_UNDO
},
545 { wxID_REDO
, STD_REDOW
},
546 { wxID_HELP
, STD_HELP
},
547 { wxID_DELETE
, STD_DELETE
},
548 { wxID_REPLACE
, STD_REPLACE
},
549 { wxID_PROPERTIES
, STD_PROPERTIES
},
550 { wxID_VIEW_DETAILS
, VIEW_DETAILS
},
551 { wxID_VIEW_SORTDATE
, VIEW_SORTDATE
},
552 { wxID_VIEW_LARGEICONS
, VIEW_LARGEICONS
},
553 { wxID_VIEW_SORTNAME
, VIEW_SORTNAME
},
554 { wxID_VIEW_LIST
, VIEW_LIST
},
555 { wxID_VIEW_SORTSIZE
, VIEW_SORTSIZE
},
556 { wxID_VIEW_SMALLICONS
, VIEW_SMALLICONS
},
557 { wxID_VIEW_SORTTYPE
, VIEW_SORTTYPE
},
561 static int wxFindIdForWinceId(int id
)
566 if (sm_ToolBarIdMappingArray
[i
].m_winceId
== 0)
568 else if (sm_ToolBarIdMappingArray
[i
].m_winceId
== id
)
569 return sm_ToolBarIdMappingArray
[i
].m_wxwinId
;
575 static int wxFindIdForwxWinId(int id
)
580 if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== 0)
582 else if (sm_ToolBarIdMappingArray
[i
].m_wxwinId
== id
)
583 return sm_ToolBarIdMappingArray
[i
].m_winceId
;
590 bool wxToolBar::Realize()
592 const size_t nTools
= GetToolsCount();
599 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
602 // delete all old buttons, if any
603 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
605 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
607 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
612 // add the buttons and separators
613 // ------------------------------
615 // Use standard buttons
616 CommandBar_AddBitmap((HWND
) GetHWND(), HINST_COMMCTRL
,
617 IDB_STD_SMALL_COLOR
, 0, 16, 16);
619 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
621 // this array will hold the indices of all controls in the toolbar
622 wxArrayInt controlIds
;
624 bool lastWasRadio
= FALSE
;
626 wxToolBarToolsList::Node
* node
;
627 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
629 wxToolBarToolBase
*tool
= node
->GetData();
631 bool processedThis
= TRUE
;
633 TBBUTTON
& button
= buttons
[i
];
635 wxZeroMemory(button
);
637 bool isRadio
= FALSE
;
638 switch ( tool
->GetStyle() )
640 case wxTOOL_STYLE_CONTROL
:
641 button
.idCommand
= tool
->GetId();
642 // fall through: create just a separator too
644 case wxTOOL_STYLE_SEPARATOR
:
645 button
.fsState
= TBSTATE_ENABLED
;
646 button
.fsStyle
= TBSTYLE_SEP
;
649 case wxTOOL_STYLE_BUTTON
:
650 // if ( !HasFlag(wxTB_NOICONS) )
651 // button.iBitmap = bitmapId;
653 if ( HasFlag(wxTB_TEXT
) )
655 const wxString
& label
= tool
->GetLabel();
656 if ( !label
.empty() )
658 button
.iString
= (int)label
.c_str();
662 int winceId
= wxFindIdForwxWinId(tool
->GetId());
665 button
.idCommand
= tool
->GetId();
666 // if ( !HasFlag(wxTB_NOICONS) )
667 button
.iBitmap
= winceId
;
670 processedThis
= FALSE
;
672 if ( tool
->IsEnabled() )
673 button
.fsState
|= TBSTATE_ENABLED
;
674 if ( tool
->IsToggled() )
675 button
.fsState
|= TBSTATE_CHECKED
;
677 switch ( tool
->GetKind() )
680 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
684 // the first item in the radio group is checked by
685 // default to be consistent with wxGTK and the menu
687 button
.fsState
|= TBSTATE_CHECKED
;
696 button
.fsStyle
= TBSTYLE_CHECK
;
700 wxFAIL_MSG( _T("unexpected toolbar button kind") );
704 button
.fsStyle
= TBSTYLE_BUTTON
;
711 lastWasRadio
= isRadio
;
717 // Add buttons to Commandbar
718 if (!CommandBar_AddButtons(GetHwnd(), i
, buttons
))
720 wxLogLastError(wxT("CommandBar_AddButtons"));
726 // Deal with the controls finally
727 // ------------------------------
729 // adjust the controls size to fit nicely in the toolbar
732 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
734 wxToolBarToolBase
*tool
= node
->GetData();
736 // we calculate the running y coord for vertical toolbars so we need to
737 // get the items size for all items but for the horizontal ones we
738 // don't need to deal with the non controls
739 bool isControl
= tool
->IsControl();
740 if ( !isControl
&& !isVertical
)
743 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
744 // latter only appeared in v4.70 of comctl32.dll
746 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
747 index
, (LPARAM
)(LPRECT
)&r
) )
749 wxLogLastError(wxT("TB_GETITEMRECT"));
754 // can only be control if isVertical
755 y
+= r
.bottom
- r
.top
;
760 wxControl
*control
= tool
->GetControl();
762 wxSize size
= control
->GetSize();
764 // the position of the leftmost controls corner
767 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
768 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
769 // available in headers, now check whether it is available now
771 if ( wxTheApp
->GetComCtl32Version() >= 471 )
773 // set the (underlying) separators width to be that of the
776 tbbi
.cbSize
= sizeof(tbbi
);
777 tbbi
.dwMask
= TBIF_SIZE
;
779 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
780 tool
->GetId(), (LPARAM
)&tbbi
) )
782 // the id is probably invalid?
783 wxLogLastError(wxT("TB_SETBUTTONINFO"));
787 #endif // comctl32.dll 4.71
788 // TB_SETBUTTONINFO unavailable
790 // try adding several separators to fit the controls width
791 int widthSep
= r
.right
- r
.left
;
797 tbb
.fsState
= TBSTATE_ENABLED
;
798 tbb
.fsStyle
= TBSTYLE_SEP
;
800 size_t nSeparators
= size
.x
/ widthSep
;
801 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
803 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
804 index
, (LPARAM
)&tbb
) )
806 wxLogLastError(wxT("TB_INSERTBUTTON"));
812 // remember the number of separators we used - we'd have to
813 // delete all of them later
814 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
816 // adjust the controls width to exactly cover the separators
817 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
820 // position the control itself correctly vertically
821 int height
= r
.bottom
- r
.top
;
822 int diff
= height
- size
.y
;
825 // the control is too high, resize to fit
826 control
->SetSize(-1, height
- 2);
837 y
+= height
+ 2*GetMargins().y
;
839 else // horizontal toolbar
847 control
->Move(left
, top
+ (diff
+ 1) / 2);
850 // the max index is the "real" number of buttons - i.e. counting even the
851 // separators which we added just for aligning the controls
856 if ( m_maxRows
== 0 )
858 // if not set yet, only one row
862 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
864 if ( m_maxRows
== 0 )
866 // if not set yet, have one column
875 // ----------------------------------------------------------------------------
877 // ----------------------------------------------------------------------------
879 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
881 wxToolBarToolBase
*tool
= FindById((int)id
);
884 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
885 event
.SetEventObject(this);
889 return GetEventHandler()->ProcessEvent(event
);
892 if ( tool
->CanBeToggled() )
894 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
895 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
898 bool toggled
= tool
->IsToggled();
900 // avoid sending the event when a radio button is released, this is not
902 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
904 // OnLeftClick() can veto the button state change - for buttons which
905 // may be toggled only, of couse
906 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
910 tool
->SetToggle(toggled
);
912 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
919 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
921 WXLPARAM
*WXUNUSED(result
))
924 // First check if this applies to us
925 NMHDR
*hdr
= (NMHDR
*)lParam
;
927 // the tooltips control created by the toolbar is sometimes Unicode, even
928 // in an ANSI application - this seems to be a bug in comctl32.dll v5
929 UINT code
= hdr
->code
;
930 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
933 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
934 if ( toolTipWnd
!= hdr
->hwndFrom
)
937 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
938 int id
= (int)ttText
->hdr
.idFrom
;
940 wxToolBarToolBase
*tool
= FindById(id
);
944 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
950 // ----------------------------------------------------------------------------
952 // ----------------------------------------------------------------------------
954 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
956 wxToolBarBase::SetToolBitmapSize(size
);
958 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
961 void wxToolBar::SetRows(int nRows
)
963 if ( nRows
== m_maxRows
)
965 // avoid resizing the frame uselessly
969 // TRUE in wParam means to create at least as many rows, FALSE -
972 ::SendMessage(GetHwnd(), TB_SETROWS
,
973 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
981 // The button size is bigger than the bitmap size
982 wxSize
wxToolBar::GetToolSize() const
984 // TB_GETBUTTONSIZE is supported from version 4.70
985 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
986 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
987 if ( wxTheApp
->GetComCtl32Version() >= 470 )
989 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
991 return wxSize(LOWORD(dw
), HIWORD(dw
));
994 #endif // comctl32.dll 4.70+
997 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1002 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1005 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1007 for ( ; current
!= 0; current
= current
->GetNext() )
1010 return current
->GetData();
1012 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1013 size_t separators
= tool
->GetSeparatorsCount();
1015 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1016 // otherwise, skip as many items as the separator count, plus the
1018 index
-= separators
? separators
+ 1 : 1;
1024 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1029 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1030 // MBN: when the point ( x, y ) is close to the toolbar border
1031 // TB_HITTEST returns m_nButtons ( not -1 )
1032 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1034 // it's a separator or there is no tool at all there
1035 return (wxToolBarToolBase
*)NULL
;
1038 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1039 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1040 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1042 return m_tools
.Item((size_t)index
)->GetData();
1047 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1051 void wxToolBar::UpdateSize()
1053 // the toolbar size changed
1054 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1056 // we must also refresh the frame after the toolbar size (possibly) changed
1057 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1060 frame
->SendSizeEvent();
1064 // ----------------------------------------------------------------------------
1066 // ---------------------------------------------------------------------------
1068 void wxToolBar::SetWindowStyleFlag(long style
)
1070 // the style bits whose changes force us to recreate the toolbar
1071 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1073 const long styleOld
= GetWindowStyle();
1075 wxToolBarBase::SetWindowStyleFlag(style
);
1077 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1078 // also fatal as we'd then try to recreate the toolbar when it's just being
1080 if ( GetToolsCount() &&
1081 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1083 // to remove the text labels, simply re-realizing the toolbar is enough
1084 // but I don't know of any way to add the text to an existing toolbar
1085 // other than by recreating it entirely
1090 // ----------------------------------------------------------------------------
1092 // ----------------------------------------------------------------------------
1094 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1096 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1097 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1100 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1102 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1103 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1106 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1108 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1109 // without, so we really need to delete the button and recreate it here
1110 wxFAIL_MSG( _T("not implemented") );
1113 // ----------------------------------------------------------------------------
1115 // ----------------------------------------------------------------------------
1117 // Responds to colour changes, and passes event on to children.
1118 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1120 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1122 // Remap the buttons
1125 // Relayout the toolbar
1126 int nrows
= m_maxRows
;
1127 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1132 // let the event propagate further
1136 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1138 if (event
.Leaving() && m_pInTool
)
1145 if (event
.RightDown())
1147 // For now, we don't have an id. Later we could
1148 // try finding the tool.
1149 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1157 void wxToolBar::HandleMouseMove(WXWPARAM wParam
, WXLPARAM lParam
)
1159 wxCoord x
= GET_X_LPARAM(lParam
),
1160 y
= GET_Y_LPARAM(lParam
);
1161 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1163 // cursor left current tool
1164 if( tool
!= m_pInTool
&& !tool
)
1170 // cursor entered a tool
1171 if( tool
!= m_pInTool
&& tool
)
1174 OnMouseEnter( tool
->GetId() );
1178 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1184 if ( HandleSize(wParam
, lParam
) )
1189 // we don't handle mouse moves, so always pass the message to
1190 // wxControl::MSWWindowProc
1191 HandleMouseMove(wParam
, lParam
);
1195 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1198 // ----------------------------------------------------------------------------
1199 // private functions
1200 // ----------------------------------------------------------------------------
1202 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1208 wxLogLastError(_T("CreateCompatibleDC"));
1213 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1217 wxLogLastError(_T("SelectObject"));
1222 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1224 for ( int i
= 0; i
< width
; i
++ )
1226 for ( int j
= 0; j
< height
; j
++ )
1228 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1230 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1232 COLORREF col
= cmap
[k
].from
;
1233 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1234 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1235 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1237 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1246 // VZ: I leave here my attempts to map the bitmap to the system colours
1247 // faster by using BitBlt() even though it's broken currently - but
1248 // maybe someone else can finish it? It should be faster than iterating
1249 // over all pixels...
1251 MemoryHDC hdcMask
, hdcDst
;
1252 if ( !hdcMask
|| !hdcDst
)
1254 wxLogLastError(_T("CreateCompatibleDC"));
1259 // create the target bitmap
1260 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1263 wxLogLastError(_T("CreateCompatibleBitmap"));
1268 // create the monochrome mask bitmap
1269 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1272 wxLogLastError(_T("CreateBitmap(mono)"));
1274 ::DeleteObject(hbmpDst
);
1279 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1280 bmpInMask(hdcMask
, hbmpMask
);
1283 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1285 // create the mask for this colour
1286 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1287 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1289 // replace this colour with the target one in the dst bitmap
1290 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1291 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1293 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1296 MAKEROP4(PATCOPY
, SRCCOPY
));
1298 (void)::SelectObject(hdcDst
, hbrOld
);
1299 ::DeleteObject(hbr
);
1302 ::DeleteObject((HBITMAP
)bitmap
);
1304 return (WXHBITMAP
)hbmpDst
;
1308 #endif // wxUSE_TOOLBAR && Win95