]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wince/tbarwce.cpp
wxDisplay support for wxWinCE port.
[wxWidgets.git] / src / msw / wince / tbarwce.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
495 event.SetEventObject(this);
496 event.SetId(id);
497 event.SetInt(id);
498
499 return GetEventHandler()->ProcessEvent(event);
500 }
501
502 if ( tool->CanBeToggled() )
503 {
504 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
505 tool->Toggle((state & TBSTATE_CHECKED) != 0);
506 }
507
508 bool toggled = tool->IsToggled();
509
510 // avoid sending the event when a radio button is released, this is not
511 // interesting
512 if ( !tool->CanBeToggled() || tool->GetKind() != wxITEM_RADIO || toggled )
513 {
514 // OnLeftClick() can veto the button state change - for buttons which
515 // may be toggled only, of couse
516 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
517 {
518 // revert back
519 toggled = !toggled;
520 tool->SetToggle(toggled);
521
522 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0));
523 }
524 }
525
526 return true;
527 }
528
529 WXLRESULT wxToolMenuBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
530 {
531 switch ( nMsg )
532 {
533 case WM_SIZE:
534 break;
535
536 case WM_MOUSEMOVE:
537 // we don't handle mouse moves, so always pass the message to
538 // wxControl::MSWWindowProc
539 HandleMouseMove(wParam, lParam);
540 break;
541
542 case WM_PAINT:
543 break;
544 }
545
546 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
547 }
548
549
550 #else
551
552 ////////////// For Smartphone
553
554 // ----------------------------------------------------------------------------
555 // Event table
556 // ----------------------------------------------------------------------------
557
558 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
559
560 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
561 END_EVENT_TABLE()
562
563 wxToolBarToolBase *wxToolBar::CreateTool(int id,
564 const wxString& label,
565 const wxBitmap& bmpNormal,
566 const wxBitmap& bmpDisabled,
567 wxItemKind kind,
568 wxObject *clientData,
569 const wxString& shortHelp,
570 const wxString& longHelp)
571 {
572 return new wxToolBarToolBase(this, id, label, bmpNormal, bmpDisabled, kind,
573 clientData, shortHelp, longHelp);
574 }
575
576 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
577 {
578 return new wxToolBarToolBase(this, control);
579 }
580
581 bool wxToolBar::Create(wxWindow *parent,
582 wxWindowID WXUNUSED(id),
583 const wxPoint& WXUNUSED(pos),
584 const wxSize& WXUNUSED(size),
585 long style,
586 const wxString& name)
587 {
588 // TODO: we may need to make this a dummy hidden window to
589 // satisfy other parts of wxWidgets.
590
591 parent->AddChild(this);
592
593 SetWindowStyle(style);
594 SetName(name);
595
596 return true;
597 }
598
599 // ----------------------------------------------------------------------------
600 // adding/removing tools
601 // ----------------------------------------------------------------------------
602
603 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
604 {
605 tool->Attach(this);
606 return true;
607 }
608
609 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
610 {
611 tool->Detach();
612 return true;
613 }
614
615 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) const
616 {
617 return NULL;
618 }
619
620 // ----------------------------------------------------------------------------
621 // tool state
622 // ----------------------------------------------------------------------------
623
624 void wxToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
625 {
626 }
627
628 void wxToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
629 {
630 }
631
632 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
633 {
634 wxFAIL_MSG( _T("not implemented") );
635 }
636
637 #endif
638 // !__SMARTPHONE__
639
640
641
642 #endif // wxUSE_TOOLBAR && Win95
643