1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 // Use the WinCE-specific toolbar only if we're either compiling
28 // with a WinCE earlier than 4, or we wish to emulate a PocketPC-style UI
29 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
31 #include "wx/toolbar.h"
34 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
35 #include "wx/dynarray.h"
39 #include "wx/settings.h"
40 #include "wx/bitmap.h"
41 #include "wx/dcmemory.h"
42 #include "wx/control.h"
45 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
49 #include "wx/msw/private.h"
55 #if defined(WINCE_WITHOUT_COMMANDBAR)
58 #include "wx/msw/wince/missing.h"
60 #include "wx/msw/winundef.h"
62 #if !defined(__SMARTPHONE__)
64 ///////////// This implementation is for PocketPC.
65 ///////////// See later for the Smartphone dummy toolbar class.
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxToolMenuBar
, wxToolBar
)
73 BEGIN_EVENT_TABLE(wxToolMenuBar
, wxToolBar
)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 class wxToolMenuBarTool
: public wxToolBarToolBase
83 wxToolMenuBarTool(wxToolBar
*tbar
,
85 const wxString
& label
,
86 const wxBitmap
& bmpNormal
,
87 const wxBitmap
& bmpDisabled
,
90 const wxString
& shortHelp
,
91 const wxString
& longHelp
)
92 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
93 clientData
, shortHelp
, longHelp
)
99 wxToolMenuBarTool(wxToolBar
*tbar
, wxControl
*control
)
100 : wxToolBarToolBase(tbar
, control
)
106 virtual void SetLabel(const wxString
& label
)
108 if ( label
== m_label
)
111 wxToolBarToolBase::SetLabel(label
);
113 // we need to update the label shown in the toolbar because it has a
114 // pointer to the internal buffer of the old label
116 // TODO: use TB_SETBUTTONINFO
119 // set/get the number of separators which we use to cover the space used by
120 // a control in the toolbar
121 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
122 size_t GetSeparatorsCount() const { return m_nSepCount
; }
124 void SetBitmapIndex(int idx
) { m_bitmapIndex
= idx
; }
125 int GetBitmapIndex() const { return m_bitmapIndex
; }
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 wxToolBarToolBase
*wxToolMenuBar::CreateTool(int id
,
142 const wxString
& label
,
143 const wxBitmap
& bmpNormal
,
144 const wxBitmap
& bmpDisabled
,
146 wxObject
*clientData
,
147 const wxString
& shortHelp
,
148 const wxString
& longHelp
)
150 return new wxToolMenuBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
151 clientData
, shortHelp
, longHelp
);
154 wxToolBarToolBase
*wxToolMenuBar::CreateTool(wxControl
*control
)
156 return new wxToolMenuBarTool(this, control
);
159 // ----------------------------------------------------------------------------
160 // wxToolBar construction
161 // ----------------------------------------------------------------------------
163 void wxToolMenuBar::Init()
171 bool wxToolMenuBar::Create(wxWindow
*parent
,
176 const wxString
& name
,
179 // common initialisation
180 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
183 // MSW-specific initialisation
184 if ( !MSWCreateToolbar(pos
, size
, menuBar
) )
187 // set up the colors and fonts
188 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
189 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
194 bool wxToolMenuBar::MSWCreateToolbar(const wxPoint
& WXUNUSED(pos
), const wxSize
& WXUNUSED(size
), wxMenuBar
* menuBar
)
198 m_menuBar
->SetToolBar(this);
200 #if defined(WINCE_WITHOUT_COMMANDBAR)
201 // Create the menubar.
204 memset (&mbi
, 0, sizeof (SHMENUBARINFO
));
205 mbi
.cbSize
= sizeof (SHMENUBARINFO
);
206 mbi
.hwndParent
= (HWND
) GetParent()->GetHWND();
207 #ifdef __SMARTPHONE__
208 mbi
.nToolBarId
= 5002;
210 mbi
.nToolBarId
= 5000;
214 mbi
.dwFlags
= 0 ; // SHCMBF_EMPTYBAR;
215 mbi
.hInstRes
= wxGetInstance();
217 if (!SHCreateMenuBar(&mbi
))
219 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
223 SetHWND((WXHWND
) mbi
.hwndMB
);
225 HWND hWnd
= CommandBar_Create(wxGetInstance(), (HWND
) GetParent()->GetHWND(), GetId());
226 SetHWND((WXHWND
) hWnd
);
229 // install wxWidgets window proc for this window
238 void wxToolMenuBar::Recreate()
243 wxToolMenuBar::~wxToolMenuBar()
246 GetMenuBar()->SetToolBar(NULL
);
249 // Return HMENU for the menu associated with the commandbar
250 WXHMENU
wxToolMenuBar::GetHMenu()
252 #if defined(__HANDHELDPC__)
257 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
264 // ----------------------------------------------------------------------------
265 // adding/removing tools
266 // ----------------------------------------------------------------------------
268 bool wxToolMenuBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
270 // nothing special to do here - we really create the toolbar buttons in
277 bool wxToolMenuBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
279 // the main difficulty we have here is with the controls in the toolbars:
280 // as we (sometimes) use several separators to cover up the space used by
281 // them, the indices are not the same for us and the toolbar
283 // first determine the position of the first button to delete: it may be
284 // different from pos if we use several separators to cover the space used
286 wxToolBarToolsList::compatibility_iterator node
;
287 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
289 wxToolBarToolBase
*tool2
= node
->GetData();
292 // let node point to the next node in the list
293 node
= node
->GetNext();
298 if ( tool2
->IsControl() )
300 pos
+= ((wxToolMenuBarTool
*)tool2
)->GetSeparatorsCount() - 1;
304 // now determine the number of buttons to delete and the area taken by them
305 size_t nButtonsToDelete
= 1;
307 // get the size of the button we're going to delete
309 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
311 wxLogLastError(_T("TB_GETITEMRECT"));
314 int width
= r
.right
- r
.left
;
316 if ( tool
->IsControl() )
318 nButtonsToDelete
= ((wxToolMenuBarTool
*)tool
)->GetSeparatorsCount();
320 width
*= nButtonsToDelete
;
323 // do delete all buttons
324 m_nButtons
-= nButtonsToDelete
;
325 while ( nButtonsToDelete
-- > 0 )
327 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
329 wxLogLastError(wxT("TB_DELETEBUTTON"));
337 // and finally reposition all the controls after this button (the toolbar
338 // takes care of all normal items)
339 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
341 wxToolBarToolBase
*tool2
= node
->GetData();
342 if ( tool2
->IsControl() )
345 wxControl
*control
= tool2
->GetControl();
346 control
->GetPosition(&x
, NULL
);
347 control
->Move(x
- width
, wxDefaultCoord
);
354 bool wxToolMenuBar::Realize()
356 const size_t nTools
= GetToolsCount();
364 // delete all old buttons, if any
365 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
367 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
369 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
374 bool lastWasRadio
= false;
375 wxToolBarToolsList::Node
* node
;
376 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
378 wxToolMenuBarTool
*tool
= (wxToolMenuBarTool
*) node
->GetData();
380 TBBUTTON buttons
[1] ;
382 TBBUTTON
& button
= buttons
[0];
384 wxZeroMemory(button
);
386 bool isRadio
= false;
387 switch ( tool
->GetStyle() )
389 case wxTOOL_STYLE_CONTROL
:
390 button
.idCommand
= tool
->GetId();
391 // fall through: create just a separator too
392 // TODO: controls are not yet supported on wxToolMenuBar.
394 case wxTOOL_STYLE_SEPARATOR
:
395 button
.fsState
= TBSTATE_ENABLED
;
396 button
.fsStyle
= TBSTYLE_SEP
;
399 case wxTOOL_STYLE_BUTTON
:
401 if ( HasFlag(wxTB_TEXT
) )
403 const wxString
& label
= tool
->GetLabel();
404 if ( !label
.empty() )
406 button
.iString
= (int)label
.c_str();
410 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
412 wxBitmap bmpToUse
= bmp
;
414 if (bmp
.GetWidth() < 16 || bmp
.GetHeight() < 16 || bmp
.GetMask() != NULL
)
418 memDC
.SelectObject(b
);
419 wxColour col
= wxColour(192,192,192);
420 memDC
.SetBackground(wxBrush(col
));
422 int x
= (16 - bmp
.GetWidth())/2;
423 int y
= (16 - bmp
.GetHeight())/2;
424 memDC
.DrawBitmap(bmp
, x
, y
, true);
425 memDC
.SelectObject(wxNullBitmap
);
428 tool
->SetNormalBitmap(b
);
434 n
= ::CommandBar_AddBitmap( (HWND
) GetHWND(), NULL
, (int) (HBITMAP
) bmpToUse
.GetHBITMAP(),
438 button
.idCommand
= tool
->GetId();
441 if ( tool
->IsEnabled() )
442 button
.fsState
|= TBSTATE_ENABLED
;
443 if ( tool
->IsToggled() )
444 button
.fsState
|= TBSTATE_CHECKED
;
446 switch ( tool
->GetKind() )
449 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
453 // the first item in the radio group is checked by
454 // default to be consistent with wxGTK and the menu
456 button
.fsState
|= TBSTATE_CHECKED
;
465 button
.fsStyle
= TBSTYLE_CHECK
;
469 wxFAIL_MSG( _T("unexpected toolbar button kind") );
473 button
.fsStyle
= TBSTYLE_BUTTON
;
478 if ( !::CommandBar_AddButtons( (HWND
) GetHWND(), 1, buttons
) )
480 wxFAIL_MSG( wxT("Could not add toolbar button."));
483 lastWasRadio
= isRadio
;
489 bool wxToolMenuBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
491 wxToolBarToolBase
*tool
= FindById((int)id
);
494 bool checked
= false;
497 wxMenuItem
*item
= m_menuBar
->FindItem(id
);
498 if ( item
&& item
->IsCheckable() )
501 checked
= item
->IsChecked();
505 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
506 event
.SetEventObject(this);
508 event
.SetInt(checked
);
510 return GetEventHandler()->ProcessEvent(event
);
513 if ( tool
->CanBeToggled() )
515 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
516 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
519 bool toggled
= tool
->IsToggled();
521 // avoid sending the event when a radio button is released, this is not
523 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
525 // OnLeftClick() can veto the button state change - for buttons which
526 // may be toggled only, of couse
527 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
531 tool
->SetToggle(toggled
);
533 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
540 WXLRESULT
wxToolMenuBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
548 // we don't handle mouse moves, so always pass the message to
549 // wxControl::MSWWindowProc
550 HandleMouseMove(wParam
, lParam
);
557 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
563 ////////////// For Smartphone
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
571 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
574 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
575 const wxString
& label
,
576 const wxBitmap
& bmpNormal
,
577 const wxBitmap
& bmpDisabled
,
579 wxObject
*clientData
,
580 const wxString
& shortHelp
,
581 const wxString
& longHelp
)
583 return new wxToolBarToolBase(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
584 clientData
, shortHelp
, longHelp
);
587 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
589 return new wxToolBarToolBase(this, control
);
592 bool wxToolBar::Create(wxWindow
*parent
,
593 wxWindowID
WXUNUSED(id
),
594 const wxPoint
& WXUNUSED(pos
),
595 const wxSize
& WXUNUSED(size
),
597 const wxString
& name
)
599 // TODO: we may need to make this a dummy hidden window to
600 // satisfy other parts of wxWidgets.
602 parent
->AddChild(this);
604 SetWindowStyle(style
);
610 // ----------------------------------------------------------------------------
611 // adding/removing tools
612 // ----------------------------------------------------------------------------
614 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
620 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
626 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
)) const
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 void wxToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
639 void wxToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
643 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
645 wxFAIL_MSG( _T("not implemented") );
651 #endif // wxUSE_TOOLBAR