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
, const wxString
& label
)
100 : wxToolBarToolBase(tbar
, control
, label
)
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
);
155 wxToolMenuBar::CreateTool(wxControl
*control
, const wxString
& label
)
157 return new wxToolMenuBarTool(this, control
, label
);
160 // ----------------------------------------------------------------------------
161 // wxToolBar construction
162 // ----------------------------------------------------------------------------
164 void wxToolMenuBar::Init()
172 bool wxToolMenuBar::Create(wxWindow
*parent
,
177 const wxString
& name
,
180 // common initialisation
181 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
184 // MSW-specific initialisation
185 if ( !MSWCreateToolbar(pos
, size
, menuBar
) )
188 // set up the colors and fonts
189 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
190 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
195 bool wxToolMenuBar::MSWCreateToolbar(const wxPoint
& WXUNUSED(pos
), const wxSize
& WXUNUSED(size
), wxMenuBar
* menuBar
)
199 m_menuBar
->SetToolBar(this);
201 #if defined(WINCE_WITHOUT_COMMANDBAR)
202 // Create the menubar.
205 memset (&mbi
, 0, sizeof (SHMENUBARINFO
));
206 mbi
.cbSize
= sizeof (SHMENUBARINFO
);
207 mbi
.hwndParent
= (HWND
) GetParent()->GetHWND();
208 #ifdef __SMARTPHONE__
209 mbi
.nToolBarId
= 5002;
211 mbi
.nToolBarId
= 5000;
215 mbi
.dwFlags
= 0 ; // SHCMBF_EMPTYBAR;
216 mbi
.hInstRes
= wxGetInstance();
218 if (!SHCreateMenuBar(&mbi
))
220 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
224 SetHWND((WXHWND
) mbi
.hwndMB
);
226 HWND hWnd
= CommandBar_Create(wxGetInstance(), (HWND
) GetParent()->GetHWND(), GetId());
227 SetHWND((WXHWND
) hWnd
);
230 // install wxWidgets window proc for this window
239 void wxToolMenuBar::Recreate()
244 wxToolMenuBar::~wxToolMenuBar()
247 GetMenuBar()->SetToolBar(NULL
);
250 // Return HMENU for the menu associated with the commandbar
251 WXHMENU
wxToolMenuBar::GetHMenu()
253 #if defined(__HANDHELDPC__)
258 return (WXHMENU
) (HMENU
)::SendMessage((HWND
) GetHWND(), SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
265 // ----------------------------------------------------------------------------
266 // adding/removing tools
267 // ----------------------------------------------------------------------------
269 bool wxToolMenuBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
271 // nothing special to do here - we really create the toolbar buttons in
278 bool wxToolMenuBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
280 // the main difficulty we have here is with the controls in the toolbars:
281 // as we (sometimes) use several separators to cover up the space used by
282 // them, the indices are not the same for us and the toolbar
284 // first determine the position of the first button to delete: it may be
285 // different from pos if we use several separators to cover the space used
287 wxToolBarToolsList::compatibility_iterator node
;
288 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
290 wxToolBarToolBase
*tool2
= node
->GetData();
293 // let node point to the next node in the list
294 node
= node
->GetNext();
299 if ( tool2
->IsControl() )
301 pos
+= ((wxToolMenuBarTool
*)tool2
)->GetSeparatorsCount() - 1;
305 // now determine the number of buttons to delete and the area taken by them
306 size_t nButtonsToDelete
= 1;
308 // get the size of the button we're going to delete
310 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
312 wxLogLastError(_T("TB_GETITEMRECT"));
315 int width
= r
.right
- r
.left
;
317 if ( tool
->IsControl() )
319 nButtonsToDelete
= ((wxToolMenuBarTool
*)tool
)->GetSeparatorsCount();
321 width
*= nButtonsToDelete
;
324 // do delete all buttons
325 m_nButtons
-= nButtonsToDelete
;
326 while ( nButtonsToDelete
-- > 0 )
328 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
330 wxLogLastError(wxT("TB_DELETEBUTTON"));
338 // and finally reposition all the controls after this button (the toolbar
339 // takes care of all normal items)
340 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
342 wxToolBarToolBase
*tool2
= node
->GetData();
343 if ( tool2
->IsControl() )
346 wxControl
*control
= tool2
->GetControl();
347 control
->GetPosition(&x
, NULL
);
348 control
->Move(x
- width
, wxDefaultCoord
);
355 bool wxToolMenuBar::Realize()
357 const size_t nTools
= GetToolsCount();
365 // delete all old buttons, if any
366 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
368 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
370 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
375 bool lastWasRadio
= false;
376 wxToolBarToolsList::Node
* node
;
377 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
379 wxToolMenuBarTool
*tool
= (wxToolMenuBarTool
*) node
->GetData();
381 TBBUTTON buttons
[1] ;
383 TBBUTTON
& button
= buttons
[0];
385 wxZeroMemory(button
);
387 bool isRadio
= false;
388 switch ( tool
->GetStyle() )
390 case wxTOOL_STYLE_CONTROL
:
391 button
.idCommand
= tool
->GetId();
392 // fall through: create just a separator too
393 // TODO: controls are not yet supported on wxToolMenuBar.
395 case wxTOOL_STYLE_SEPARATOR
:
396 button
.fsState
= TBSTATE_ENABLED
;
397 button
.fsStyle
= TBSTYLE_SEP
;
400 case wxTOOL_STYLE_BUTTON
:
402 if ( HasFlag(wxTB_TEXT
) )
404 const wxString
& label
= tool
->GetLabel();
405 if ( !label
.empty() )
407 button
.iString
= (int)label
.c_str();
411 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
413 wxBitmap bmpToUse
= bmp
;
415 if (bmp
.GetWidth() < 16 || bmp
.GetHeight() < 16 || bmp
.GetMask() != NULL
)
419 memDC
.SelectObject(b
);
420 wxColour col
= wxColour(192,192,192);
421 memDC
.SetBackground(wxBrush(col
));
423 int x
= (16 - bmp
.GetWidth())/2;
424 int y
= (16 - bmp
.GetHeight())/2;
425 memDC
.DrawBitmap(bmp
, x
, y
, true);
426 memDC
.SelectObject(wxNullBitmap
);
429 tool
->SetNormalBitmap(b
);
435 n
= ::CommandBar_AddBitmap( (HWND
) GetHWND(), NULL
, (int) (HBITMAP
) bmpToUse
.GetHBITMAP(),
439 button
.idCommand
= tool
->GetId();
442 if ( tool
->IsEnabled() )
443 button
.fsState
|= TBSTATE_ENABLED
;
444 if ( tool
->IsToggled() )
445 button
.fsState
|= TBSTATE_CHECKED
;
447 switch ( tool
->GetKind() )
450 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
454 // the first item in the radio group is checked by
455 // default to be consistent with wxGTK and the menu
457 button
.fsState
|= TBSTATE_CHECKED
;
466 button
.fsStyle
= TBSTYLE_CHECK
;
470 wxFAIL_MSG( _T("unexpected toolbar button kind") );
474 button
.fsStyle
= TBSTYLE_BUTTON
;
479 if ( !::CommandBar_AddButtons( (HWND
) GetHWND(), 1, buttons
) )
481 wxFAIL_MSG( wxT("Could not add toolbar button."));
484 lastWasRadio
= isRadio
;
490 bool wxToolMenuBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
492 wxToolBarToolBase
*tool
= FindById((int)id
);
495 bool checked
= false;
498 wxMenuItem
*item
= m_menuBar
->FindItem(id
);
499 if ( item
&& item
->IsCheckable() )
502 checked
= item
->IsChecked();
506 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
507 event
.SetEventObject(this);
509 event
.SetInt(checked
);
511 return GetEventHandler()->ProcessEvent(event
);
514 if ( tool
->CanBeToggled() )
516 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
517 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
520 bool toggled
= tool
->IsToggled();
522 // avoid sending the event when a radio button is released, this is not
524 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
526 // OnLeftClick() can veto the button state change - for buttons which
527 // may be toggled only, of couse
528 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
532 tool
->SetToggle(toggled
);
534 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
541 WXLRESULT
wxToolMenuBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
549 // we don't handle mouse moves, so always pass the message to
550 // wxControl::MSWWindowProc
551 HandleMouseMove(wParam
, lParam
);
558 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
564 ////////////// For Smartphone
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
572 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
575 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
576 const wxString
& label
,
577 const wxBitmap
& bmpNormal
,
578 const wxBitmap
& bmpDisabled
,
580 wxObject
*clientData
,
581 const wxString
& shortHelp
,
582 const wxString
& longHelp
)
584 return new wxToolBarToolBase(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
585 clientData
, shortHelp
, longHelp
);
588 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
590 return new wxToolBarToolBase(this, control
);
593 bool wxToolBar::Create(wxWindow
*parent
,
594 wxWindowID
WXUNUSED(id
),
595 const wxPoint
& WXUNUSED(pos
),
596 const wxSize
& WXUNUSED(size
),
598 const wxString
& name
)
600 // TODO: we may need to make this a dummy hidden window to
601 // satisfy other parts of wxWidgets.
603 parent
->AddChild(this);
605 SetWindowStyle(style
);
611 // ----------------------------------------------------------------------------
612 // adding/removing tools
613 // ----------------------------------------------------------------------------
615 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
621 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
627 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
)) const
632 // ----------------------------------------------------------------------------
634 // ----------------------------------------------------------------------------
636 void wxToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
640 void wxToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
644 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
646 wxFAIL_MSG( _T("not implemented") );
652 #endif // wxUSE_TOOLBAR