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