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