]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wince/tbarwce.cpp
added missing #includes
[wxWidgets.git] / src / msw / wince / tbarwce.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/tbarwce.cpp
3 // Purpose: wxToolBar for Windows CE
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2003-07-12
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
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__))
30
31 #include "wx/toolbar.h"
32
33 #ifndef WX_PRECOMP
34 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
35 #include "wx/dynarray.h"
36 #include "wx/frame.h"
37 #include "wx/log.h"
38 #include "wx/intl.h"
39 #include "wx/settings.h"
40 #include "wx/bitmap.h"
41 #include "wx/dcmemory.h"
42 #include "wx/control.h"
43 #endif
44
45 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
46 #include "malloc.h"
47 #endif
48
49 #include "wx/msw/private.h"
50 #include <windows.h>
51 #include <windowsx.h>
52 #include <tchar.h>
53 #include <ole2.h>
54 #include <shellapi.h>
55 #if defined(WINCE_WITHOUT_COMMANDBAR)
56 #include <aygshell.h>
57 #endif
58 #include "wx/msw/wince/missing.h"
59
60 #include "wx/msw/winundef.h"
61
62 #if !defined(__SMARTPHONE__)
63
64 ///////////// This implementation is for PocketPC.
65 ///////////// See later for the Smartphone dummy toolbar class.
66
67 // ----------------------------------------------------------------------------
68 // Event table
69 // ----------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxToolMenuBar, wxToolBar)
72
73 BEGIN_EVENT_TABLE(wxToolMenuBar, wxToolBar)
74 END_EVENT_TABLE()
75
76 // ----------------------------------------------------------------------------
77 // private classes
78 // ----------------------------------------------------------------------------
79
80 class wxToolMenuBarTool : public wxToolBarToolBase
81 {
82 public:
83 wxToolMenuBarTool(wxToolBar *tbar,
84 int id,
85 const wxString& label,
86 const wxBitmap& bmpNormal,
87 const wxBitmap& bmpDisabled,
88 wxItemKind kind,
89 wxObject *clientData,
90 const wxString& shortHelp,
91 const wxString& longHelp)
92 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
93 clientData, shortHelp, longHelp)
94 {
95 m_nSepCount = 0;
96 m_bitmapIndex = -1;
97 }
98
99 wxToolMenuBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
100 : wxToolBarToolBase(tbar, control, label)
101 {
102 m_nSepCount = 1;
103 m_bitmapIndex = -1;
104 }
105
106 virtual void SetLabel(const wxString& label)
107 {
108 if ( label == m_label )
109 return;
110
111 wxToolBarToolBase::SetLabel(label);
112
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
115 //
116 // TODO: use TB_SETBUTTONINFO
117 }
118
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; }
123
124 void SetBitmapIndex(int idx) { m_bitmapIndex = idx; }
125 int GetBitmapIndex() const { return m_bitmapIndex; }
126
127 private:
128 size_t m_nSepCount;
129 int m_bitmapIndex;
130 };
131
132
133 // ============================================================================
134 // implementation
135 // ============================================================================
136
137 // ----------------------------------------------------------------------------
138 // wxToolBarTool
139 // ----------------------------------------------------------------------------
140
141 wxToolBarToolBase *wxToolMenuBar::CreateTool(int id,
142 const wxString& label,
143 const wxBitmap& bmpNormal,
144 const wxBitmap& bmpDisabled,
145 wxItemKind kind,
146 wxObject *clientData,
147 const wxString& shortHelp,
148 const wxString& longHelp)
149 {
150 return new wxToolMenuBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
151 clientData, shortHelp, longHelp);
152 }
153
154 wxToolBarToolBase *
155 wxToolMenuBar::CreateTool(wxControl *control, const wxString& label)
156 {
157 return new wxToolMenuBarTool(this, control, label);
158 }
159
160 // ----------------------------------------------------------------------------
161 // wxToolBar construction
162 // ----------------------------------------------------------------------------
163
164 void wxToolMenuBar::Init()
165 {
166 wxToolBar::Init();
167
168 m_nButtons = 0;
169 m_menuBar = NULL;
170 }
171
172 bool wxToolMenuBar::Create(wxWindow *parent,
173 wxWindowID id,
174 const wxPoint& pos,
175 const wxSize& size,
176 long style,
177 const wxString& name,
178 wxMenuBar* menuBar)
179 {
180 // common initialisation
181 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
182 return false;
183
184 // MSW-specific initialisation
185 if ( !MSWCreateToolbar(pos, size, menuBar) )
186 return false;
187
188 // set up the colors and fonts
189 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
190 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
191
192 return true;
193 }
194
195 bool wxToolMenuBar::MSWCreateToolbar(const wxPoint& WXUNUSED(pos), const wxSize& WXUNUSED(size), wxMenuBar* menuBar)
196 {
197 SetMenuBar(menuBar);
198 if (m_menuBar)
199 m_menuBar->SetToolBar(this);
200
201 #if defined(WINCE_WITHOUT_COMMANDBAR)
202 // Create the menubar.
203 SHMENUBARINFO mbi;
204
205 memset (&mbi, 0, sizeof (SHMENUBARINFO));
206 mbi.cbSize = sizeof (SHMENUBARINFO);
207 mbi.hwndParent = (HWND) GetParent()->GetHWND();
208 #ifdef __SMARTPHONE__
209 mbi.nToolBarId = 5002;
210 #else
211 mbi.nToolBarId = 5000;
212 #endif
213 mbi.nBmpId = 0;
214 mbi.cBmpImages = 0;
215 mbi.dwFlags = 0 ; // SHCMBF_EMPTYBAR;
216 mbi.hInstRes = wxGetInstance();
217
218 if (!SHCreateMenuBar(&mbi))
219 {
220 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
221 return false;
222 }
223
224 SetHWND((WXHWND) mbi.hwndMB);
225 #else
226 HWND hWnd = CommandBar_Create(wxGetInstance(), (HWND) GetParent()->GetHWND(), GetId());
227 SetHWND((WXHWND) hWnd);
228 #endif
229
230 // install wxWidgets window proc for this window
231 SubclassWin(m_hWnd);
232
233 if (menuBar)
234 menuBar->Create();
235
236 return true;
237 }
238
239 void wxToolMenuBar::Recreate()
240 {
241 // TODO
242 }
243
244 wxToolMenuBar::~wxToolMenuBar()
245 {
246 if (GetMenuBar())
247 GetMenuBar()->SetToolBar(NULL);
248 }
249
250 // Return HMENU for the menu associated with the commandbar
251 WXHMENU wxToolMenuBar::GetHMenu()
252 {
253 #if defined(__HANDHELDPC__)
254 return 0;
255 #else
256 if (GetHWND())
257 {
258 return (WXHMENU) (HMENU)::SendMessage((HWND) GetHWND(), SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
259 }
260 else
261 return 0;
262 #endif
263 }
264
265 // ----------------------------------------------------------------------------
266 // adding/removing tools
267 // ----------------------------------------------------------------------------
268
269 bool wxToolMenuBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
270 {
271 // nothing special to do here - we really create the toolbar buttons in
272 // Realize() later
273 tool->Attach(this);
274
275 return true;
276 }
277
278 bool wxToolMenuBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
279 {
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
283
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
286 // by a control
287 wxToolBarToolsList::compatibility_iterator node;
288 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
289 {
290 wxToolBarToolBase *tool2 = node->GetData();
291 if ( tool2 == tool )
292 {
293 // let node point to the next node in the list
294 node = node->GetNext();
295
296 break;
297 }
298
299 if ( tool2->IsControl() )
300 {
301 pos += ((wxToolMenuBarTool *)tool2)->GetSeparatorsCount() - 1;
302 }
303 }
304
305 // now determine the number of buttons to delete and the area taken by them
306 size_t nButtonsToDelete = 1;
307
308 // get the size of the button we're going to delete
309 RECT r;
310 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) )
311 {
312 wxLogLastError(_T("TB_GETITEMRECT"));
313 }
314
315 int width = r.right - r.left;
316
317 if ( tool->IsControl() )
318 {
319 nButtonsToDelete = ((wxToolMenuBarTool *)tool)->GetSeparatorsCount();
320
321 width *= nButtonsToDelete;
322 }
323
324 // do delete all buttons
325 m_nButtons -= nButtonsToDelete;
326 while ( nButtonsToDelete-- > 0 )
327 {
328 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
329 {
330 wxLogLastError(wxT("TB_DELETEBUTTON"));
331
332 return false;
333 }
334 }
335
336 tool->Detach();
337
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() )
341 {
342 wxToolBarToolBase *tool2 = node->GetData();
343 if ( tool2->IsControl() )
344 {
345 int x;
346 wxControl *control = tool2->GetControl();
347 control->GetPosition(&x, NULL);
348 control->Move(x - width, wxDefaultCoord);
349 }
350 }
351
352 return true;
353 }
354
355 bool wxToolMenuBar::Realize()
356 {
357 const size_t nTools = GetToolsCount();
358 if ( nTools == 0 )
359 {
360 // nothing to do
361 return true;
362 }
363
364 #if 0
365 // delete all old buttons, if any
366 for ( size_t pos = 0; pos < m_nButtons; pos++ )
367 {
368 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
369 {
370 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
371 }
372 }
373 #endif // 0
374
375 bool lastWasRadio = false;
376 wxToolBarToolsList::Node* node;
377 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
378 {
379 wxToolMenuBarTool *tool = (wxToolMenuBarTool*) node->GetData();
380
381 TBBUTTON buttons[1] ;
382
383 TBBUTTON& button = buttons[0];
384
385 wxZeroMemory(button);
386
387 bool isRadio = false;
388 switch ( tool->GetStyle() )
389 {
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.
394
395 case wxTOOL_STYLE_SEPARATOR:
396 button.fsState = TBSTATE_ENABLED;
397 button.fsStyle = TBSTYLE_SEP;
398 break;
399
400 case wxTOOL_STYLE_BUTTON:
401
402 if ( HasFlag(wxTB_TEXT) )
403 {
404 const wxString& label = tool->GetLabel();
405 if ( !label.empty() )
406 {
407 button.iString = (int)label.c_str();
408 }
409 }
410
411 const wxBitmap& bmp = tool->GetNormalBitmap();
412
413 wxBitmap bmpToUse = bmp;
414
415 if (bmp.GetWidth() < 16 || bmp.GetHeight() < 16 || bmp.GetMask() != NULL)
416 {
417 wxMemoryDC memDC;
418 wxBitmap b(16,16);
419 memDC.SelectObject(b);
420 wxColour col = wxColour(192,192,192);
421 memDC.SetBackground(wxBrush(col));
422 memDC.Clear();
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);
427
428 bmpToUse = b;
429 tool->SetNormalBitmap(b);
430 }
431
432 int n = 0;
433 if ( bmpToUse.Ok() )
434 {
435 n = ::CommandBar_AddBitmap( (HWND) GetHWND(), NULL, (int) (HBITMAP) bmpToUse.GetHBITMAP(),
436 1, 16, 16 );
437 }
438
439 button.idCommand = tool->GetId();
440 button.iBitmap = n;
441
442 if ( tool->IsEnabled() )
443 button.fsState |= TBSTATE_ENABLED;
444 if ( tool->IsToggled() )
445 button.fsState |= TBSTATE_CHECKED;
446
447 switch ( tool->GetKind() )
448 {
449 case wxITEM_RADIO:
450 button.fsStyle = TBSTYLE_CHECKGROUP;
451
452 if ( !lastWasRadio )
453 {
454 // the first item in the radio group is checked by
455 // default to be consistent with wxGTK and the menu
456 // radio items
457 button.fsState |= TBSTATE_CHECKED;
458
459 tool->Toggle(true);
460 }
461
462 isRadio = true;
463 break;
464
465 case wxITEM_CHECK:
466 button.fsStyle = TBSTYLE_CHECK;
467 break;
468
469 default:
470 wxFAIL_MSG( _T("unexpected toolbar button kind") );
471 // fall through
472
473 case wxITEM_NORMAL:
474 button.fsStyle = TBSTYLE_BUTTON;
475 }
476 break;
477 }
478
479 if ( !::CommandBar_AddButtons( (HWND) GetHWND(), 1, buttons ) )
480 {
481 wxFAIL_MSG( wxT("Could not add toolbar button."));
482 }
483
484 lastWasRadio = isRadio;
485 }
486
487 return true;
488 }
489
490 bool wxToolMenuBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
491 {
492 wxToolBarToolBase *tool = FindById((int)id);
493 if ( !tool )
494 {
495 bool checked = false;
496 if ( m_menuBar )
497 {
498 wxMenuItem *item = m_menuBar->FindItem(id);
499 if ( item && item->IsCheckable() )
500 {
501 item->Toggle();
502 checked = item->IsChecked();
503 }
504 }
505
506 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
507 event.SetEventObject(this);
508 event.SetId(id);
509 event.SetInt(checked);
510
511 return GetEventHandler()->ProcessEvent(event);
512 }
513
514 if ( tool->CanBeToggled() )
515 {
516 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
517 tool->Toggle((state & TBSTATE_CHECKED) != 0);
518 }
519
520 bool toggled = tool->IsToggled();
521
522 // avoid sending the event when a radio button is released, this is not
523 // interesting
524 if ( !tool->CanBeToggled() || tool->GetKind() != wxITEM_RADIO || toggled )
525 {
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() )
529 {
530 // revert back
531 toggled = !toggled;
532 tool->SetToggle(toggled);
533
534 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0));
535 }
536 }
537
538 return true;
539 }
540
541 WXLRESULT wxToolMenuBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
542 {
543 switch ( nMsg )
544 {
545 case WM_SIZE:
546 break;
547
548 case WM_MOUSEMOVE:
549 // we don't handle mouse moves, so always pass the message to
550 // wxControl::MSWWindowProc
551 HandleMouseMove(wParam, lParam);
552 break;
553
554 case WM_PAINT:
555 break;
556 }
557
558 return MSWDefWindowProc(nMsg, wParam, lParam);
559 }
560
561
562 #else
563
564 ////////////// For Smartphone
565
566 // ----------------------------------------------------------------------------
567 // Event table
568 // ----------------------------------------------------------------------------
569
570 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
571
572 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
573 END_EVENT_TABLE()
574
575 wxToolBarToolBase *wxToolBar::CreateTool(int id,
576 const wxString& label,
577 const wxBitmap& bmpNormal,
578 const wxBitmap& bmpDisabled,
579 wxItemKind kind,
580 wxObject *clientData,
581 const wxString& shortHelp,
582 const wxString& longHelp)
583 {
584 return new wxToolBarToolBase(this, id, label, bmpNormal, bmpDisabled, kind,
585 clientData, shortHelp, longHelp);
586 }
587
588 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
589 {
590 return new wxToolBarToolBase(this, control);
591 }
592
593 bool wxToolBar::Create(wxWindow *parent,
594 wxWindowID WXUNUSED(id),
595 const wxPoint& WXUNUSED(pos),
596 const wxSize& WXUNUSED(size),
597 long style,
598 const wxString& name)
599 {
600 // TODO: we may need to make this a dummy hidden window to
601 // satisfy other parts of wxWidgets.
602
603 parent->AddChild(this);
604
605 SetWindowStyle(style);
606 SetName(name);
607
608 return true;
609 }
610
611 // ----------------------------------------------------------------------------
612 // adding/removing tools
613 // ----------------------------------------------------------------------------
614
615 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
616 {
617 tool->Attach(this);
618 return true;
619 }
620
621 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
622 {
623 tool->Detach();
624 return true;
625 }
626
627 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) const
628 {
629 return NULL;
630 }
631
632 // ----------------------------------------------------------------------------
633 // tool state
634 // ----------------------------------------------------------------------------
635
636 void wxToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
637 {
638 }
639
640 void wxToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
641 {
642 }
643
644 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
645 {
646 wxFAIL_MSG( _T("not implemented") );
647 }
648
649 #endif
650 // !__SMARTPHONE__
651
652 #endif // wxUSE_TOOLBAR