]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/wince/tbarwce.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / msw / wince / tbarwce.cpp
... / ...
CommitLineData
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// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26// Use the WinCE-specific toolbar only if we're either compiling
27// with a WinCE earlier than 4, or we wish to emulate a PocketPC-style UI
28#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
29
30#include "wx/toolbar.h"
31
32#ifndef WX_PRECOMP
33 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
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__)
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#if defined(WINCE_WITHOUT_COMMANDBAR)
55 #include <aygshell.h>
56 #include "wx/msw/wince/resources.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
71IMPLEMENT_DYNAMIC_CLASS(wxToolMenuBar, wxToolBar)
72
73BEGIN_EVENT_TABLE(wxToolMenuBar, wxToolBar)
74END_EVENT_TABLE()
75
76// ----------------------------------------------------------------------------
77// private classes
78// ----------------------------------------------------------------------------
79
80class wxToolMenuBarTool : public wxToolBarToolBase
81{
82public:
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
127private:
128 size_t m_nSepCount;
129 int m_bitmapIndex;
130};
131
132
133// ============================================================================
134// implementation
135// ============================================================================
136
137// ----------------------------------------------------------------------------
138// wxToolBarTool
139// ----------------------------------------------------------------------------
140
141wxToolBarToolBase *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
154wxToolBarToolBase *
155wxToolMenuBar::CreateTool(wxControl *control, const wxString& label)
156{
157 return new wxToolMenuBarTool(this, control, label);
158}
159
160// ----------------------------------------------------------------------------
161// wxToolBar construction
162// ----------------------------------------------------------------------------
163
164void wxToolMenuBar::Init()
165{
166 wxToolBar::Init();
167
168 m_nButtons = 0;
169 m_menuBar = NULL;
170}
171
172bool 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
195bool wxToolMenuBar::MSWCreateToolbar(const wxPoint& WXUNUSED(pos),
196 const wxSize& WXUNUSED(size),
197 wxMenuBar *menuBar)
198{
199 SetMenuBar(menuBar);
200 if (m_menuBar)
201 m_menuBar->SetToolBar(this);
202
203 HWND hwndParent = GetHwndOf(GetParent());
204 wxCHECK_MSG( hwndParent, false, wxT("should have valid parent HWND") );
205
206#if defined(WINCE_WITHOUT_COMMANDBAR)
207 // create the menubar.
208 WinStruct<SHMENUBARINFO> mbi;
209
210 mbi.hwndParent = hwndParent;
211 mbi.nToolBarId = wxIDM_SHMENU;
212 mbi.hInstRes = wxGetInstance();
213
214 if ( !SHCreateMenuBar(&mbi) )
215 {
216 wxFAIL_MSG( wxT("SHCreateMenuBar failed") );
217 return false;
218 }
219
220 SetHWND((WXHWND) mbi.hwndMB);
221#else
222 HWND hWnd = CommandBar_Create(wxGetInstance(), hwndParent, GetId());
223 SetHWND((WXHWND) hWnd);
224#endif
225
226 // install wxWidgets window proc for this window
227 SubclassWin(m_hWnd);
228
229 if (menuBar)
230 menuBar->Create();
231
232 return true;
233}
234
235void wxToolMenuBar::Recreate()
236{
237 // TODO
238}
239
240wxToolMenuBar::~wxToolMenuBar()
241{
242 if (GetMenuBar())
243 GetMenuBar()->SetToolBar(NULL);
244}
245
246// Return HMENU for the menu associated with the commandbar
247WXHMENU wxToolMenuBar::GetHMenu()
248{
249#if !defined(__HANDHELDPC__)
250 if (GetHWND())
251 {
252 return (WXHMENU)::SendMessage(GetHwnd(), SHCMBM_GETMENU, 0, 0);
253 }
254#endif
255
256 return NULL;
257}
258
259// ----------------------------------------------------------------------------
260// adding/removing tools
261// ----------------------------------------------------------------------------
262
263bool wxToolMenuBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
264{
265 // nothing special to do here - we really create the toolbar buttons in
266 // Realize() later
267 tool->Attach(this);
268
269 return true;
270}
271
272bool wxToolMenuBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
273{
274 // Skip over the menus
275 if (GetMenuBar())
276 pos += GetMenuBar()->GetMenuCount();
277
278 // the main difficulty we have here is with the controls in the toolbars:
279 // as we (sometimes) use several separators to cover up the space used by
280 // them, the indices are not the same for us and the toolbar
281
282 // first determine the position of the first button to delete: it may be
283 // different from pos if we use several separators to cover the space used
284 // by a control
285 wxToolBarToolsList::compatibility_iterator node;
286 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
287 {
288 wxToolBarToolBase *tool2 = node->GetData();
289 if ( tool2 == tool )
290 {
291 // let node point to the next node in the list
292 node = node->GetNext();
293
294 break;
295 }
296
297 if ( tool2->IsControl() )
298 {
299 pos += ((wxToolMenuBarTool *)tool2)->GetSeparatorsCount() - 1;
300 }
301 }
302
303 // now determine the number of buttons to delete and the area taken by them
304 size_t nButtonsToDelete = 1;
305
306 // get the size of the button we're going to delete
307 RECT r;
308 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) )
309 {
310 wxLogLastError(wxT("TB_GETITEMRECT"));
311 }
312
313 int width = r.right - r.left;
314
315 if ( tool->IsControl() )
316 {
317 nButtonsToDelete = ((wxToolMenuBarTool *)tool)->GetSeparatorsCount();
318
319 width *= nButtonsToDelete;
320 }
321
322 // do delete all buttons
323 m_nButtons -= nButtonsToDelete;
324 while ( nButtonsToDelete-- > 0 )
325 {
326 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
327 {
328 wxLogLastError(wxT("TB_DELETEBUTTON"));
329
330 return false;
331 }
332 }
333
334 tool->Detach();
335
336 // and finally reposition all the controls after this button (the toolbar
337 // takes care of all normal items)
338 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
339 {
340 wxToolBarToolBase *tool2 = node->GetData();
341 if ( tool2->IsControl() )
342 {
343 int x;
344 wxControl *control = tool2->GetControl();
345 control->GetPosition(&x, NULL);
346 control->Move(x - width, wxDefaultCoord);
347 }
348 }
349
350 return true;
351}
352
353bool wxToolMenuBar::Realize()
354{
355 const size_t nTools = GetToolsCount();
356 if ( nTools == 0 )
357 {
358 // nothing to do
359 return true;
360 }
361
362#if 0
363 // delete all old buttons, if any
364 for ( size_t pos = 0; pos < m_nButtons; pos++ )
365 {
366 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
367 {
368 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
369 }
370 }
371#endif // 0
372
373 bool lastWasRadio = false;
374 wxToolBarToolsList::compatibility_iterator node;
375 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
376 {
377 wxToolMenuBarTool *tool = (wxToolMenuBarTool*) node->GetData();
378
379 TBBUTTON buttons[1] ;
380
381 TBBUTTON& button = buttons[0];
382
383 wxZeroMemory(button);
384
385 bool isRadio = false;
386 switch ( tool->GetStyle() )
387 {
388 case wxTOOL_STYLE_CONTROL:
389 button.idCommand = tool->GetId();
390 // fall through: create just a separator too
391 // TODO: controls are not yet supported on wxToolMenuBar.
392
393 case wxTOOL_STYLE_SEPARATOR:
394 button.fsState = TBSTATE_ENABLED;
395 button.fsStyle = TBSTYLE_SEP;
396 break;
397
398 case wxTOOL_STYLE_BUTTON:
399
400 if ( HasFlag(wxTB_TEXT) )
401 {
402 const wxString& label = tool->GetLabel();
403 if ( !label.empty() )
404 {
405 button.iString = (int) wxMSW_CONV_LPCTSTR(label);
406 }
407 }
408
409 const wxBitmap& bmp = tool->GetNormalBitmap();
410
411 wxBitmap bmpToUse = bmp;
412
413 if (bmp.GetWidth() < 16 || bmp.GetHeight() < 16 || bmp.GetMask() != NULL)
414 {
415 wxMemoryDC memDC;
416 wxBitmap b(16,16);
417 memDC.SelectObject(b);
418 wxColour col = wxColour(192,192,192);
419 memDC.SetBackground(wxBrush(col));
420 memDC.Clear();
421 int x = (16 - bmp.GetWidth())/2;
422 int y = (16 - bmp.GetHeight())/2;
423 memDC.DrawBitmap(bmp, x, y, true);
424 memDC.SelectObject(wxNullBitmap);
425
426 bmpToUse = b;
427 tool->SetNormalBitmap(b);
428 }
429
430 int n = 0;
431 if ( bmpToUse.IsOk() )
432 {
433 n = ::CommandBar_AddBitmap( (HWND) GetHWND(), NULL, (int) (HBITMAP) bmpToUse.GetHBITMAP(),
434 1, 16, 16 );
435 }
436
437 button.idCommand = tool->GetId();
438 button.iBitmap = n;
439
440 if ( tool->IsEnabled() )
441 button.fsState |= TBSTATE_ENABLED;
442 if ( tool->IsToggled() )
443 button.fsState |= TBSTATE_CHECKED;
444
445 switch ( tool->GetKind() )
446 {
447 case wxITEM_RADIO:
448 button.fsStyle = TBSTYLE_CHECKGROUP;
449
450 if ( !lastWasRadio )
451 {
452 // the first item in the radio group is checked by
453 // default to be consistent with wxGTK and the menu
454 // radio items
455 button.fsState |= TBSTATE_CHECKED;
456
457 tool->Toggle(true);
458 }
459
460 isRadio = true;
461 break;
462
463 case wxITEM_CHECK:
464 button.fsStyle = TBSTYLE_CHECK;
465 break;
466
467 default:
468 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
469 // fall through
470
471 case wxITEM_NORMAL:
472 button.fsStyle = TBSTYLE_BUTTON;
473 }
474 break;
475 }
476
477 if ( !::CommandBar_AddButtons( (HWND) GetHWND(), 1, buttons ) )
478 {
479 wxFAIL_MSG( wxT("Could not add toolbar button."));
480 }
481
482 lastWasRadio = isRadio;
483 }
484
485 return true;
486}
487
488bool wxToolMenuBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_)
489{
490 const int id = (signed short)id_;
491
492 wxToolBarToolBase *tool = FindById(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_MENU);
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 course.
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
541WXLRESULT 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
570IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
571
572BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
573END_EVENT_TABLE()
574
575wxToolBarToolBase *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
588wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
589{
590 return new wxToolBarToolBase(this, control);
591}
592
593bool 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
615bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
616{
617 tool->Attach(this);
618 return true;
619}
620
621bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
622{
623 tool->Detach();
624 return true;
625}
626
627wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) const
628{
629 return NULL;
630}
631
632// ----------------------------------------------------------------------------
633// tool state
634// ----------------------------------------------------------------------------
635
636void wxToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
637{
638}
639
640void wxToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
641{
642}
643
644void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
645{
646 wxFAIL_MSG( wxT("not implemented") );
647}
648
649#endif
650 // !__SMARTPHONE__
651
652#endif // wxUSE_TOOLBAR