]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/taskbar.cpp
wxTabCtrl off build fix.
[wxWidgets.git] / src / mac / carbon / taskbar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/taskbar.cpp
3 // Purpose: wxTaskBarIcon
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 09/25/2004
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef wxHAS_TASK_BAR_ICON
15
16 #include "wx/taskbar.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/dcmemory.h"
20 #endif
21
22 #include "wx/mac/private.h"
23
24 #include "wx/menu.h"
25 #include "wx/icon.h"
26
27
28 class wxTaskBarIconImpl
29 {
30 public:
31 wxTaskBarIconImpl(wxTaskBarIcon* parent);
32 virtual ~wxTaskBarIconImpl();
33
34 virtual bool IsIconInstalled() const = 0;
35 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip) = 0;
36 virtual bool RemoveIcon() = 0;
37 virtual bool PopupMenu(wxMenu *menu) = 0;
38
39 wxMenu * CreatePopupMenu()
40 { return m_parent->CreatePopupMenu(); }
41
42 wxTaskBarIcon *m_parent;
43 class wxTaskBarIconWindow *m_menuEventWindow;
44
45 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl)
46 };
47
48 //-----------------------------------------------------------------------------
49 //
50 // wxTaskBarIconWindow
51 //
52 // Event handler for menus
53 // NB: Since wxWindows in Mac HAVE to have parents we need this to be
54 // a top level window...
55 //-----------------------------------------------------------------------------
56
57 class wxTaskBarIconWindow : public wxTopLevelWindow
58 {
59 public:
60 wxTaskBarIconWindow(wxTaskBarIconImpl *impl)
61 : wxTopLevelWindow(NULL, wxID_ANY, wxEmptyString), m_impl(impl)
62 {
63 Connect(
64 -1, wxEVT_COMMAND_MENU_SELECTED,
65 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent) );
66 }
67
68 void OnMenuEvent(wxCommandEvent& event)
69 {
70 m_impl->m_parent->ProcessEvent(event);
71 }
72
73 private:
74 wxTaskBarIconImpl *m_impl;
75 };
76
77 class wxDockTaskBarIcon : public wxTaskBarIconImpl
78 {
79 public:
80 wxDockTaskBarIcon(wxTaskBarIcon* parent);
81 virtual ~wxDockTaskBarIcon();
82
83 virtual bool IsIconInstalled() const;
84 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip);
85 virtual bool RemoveIcon();
86 virtual bool PopupMenu(wxMenu *menu);
87
88 wxMenu* DoCreatePopupMenu();
89
90 EventHandlerRef m_eventHandlerRef;
91 EventHandlerUPP m_eventupp;
92 wxWindow *m_eventWindow;
93 wxMenu *m_pMenu;
94 MenuRef m_theLastMenu;
95 bool m_iconAdded;
96 };
97
98 // Forward declarations for utility functions for dock implementation
99 pascal OSStatus wxDockEventHandler(
100 EventHandlerCallRef inHandlerCallRef,
101 EventRef inEvent, void* pData );
102 wxMenu * wxDeepCopyMenu( wxMenu *menu );
103
104
105 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
106 //
107 // wxTaskBarIconImpl
108 //
109 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
110
111 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* parent)
112 : m_parent(parent), m_menuEventWindow(new wxTaskBarIconWindow(this))
113 {
114 }
115
116 wxTaskBarIconImpl::~wxTaskBarIconImpl()
117 {
118 delete m_menuEventWindow;
119 }
120
121 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122 //
123 // wxDockTaskBarIcon
124 //
125 // OS X Dock implementation of wxTaskBarIcon using Carbon
126 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127
128 //-----------------------------------------------------------------------------
129 // wxDockEventHandler
130 //
131 // This is the global Mac/Carbon event handler for the dock.
132 // We need this for two reasons:
133 // 1) To handle wxTaskBarIcon menu events (see below for why)
134 // 2) To handle events from the dock when it requests a menu
135 //-----------------------------------------------------------------------------
136 pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef,
137 EventRef inEvent, void *pData )
138 {
139 // Get the parameters we want from the event
140 wxDockTaskBarIcon* pTB = (wxDockTaskBarIcon*) pData;
141 const UInt32 eventClass = GetEventClass(inEvent);
142 const UInt32 eventKind = GetEventKind(inEvent);
143
144 // Handle wxTaskBar menu events (note that this is a global event handler
145 // so it will actually get called by all commands/menus)
146 if ((eventClass == kEventClassCommand) && (eventKind == kEventCommandProcess))
147 {
148 // if we have no taskbar menu quickly pass it back to wxApp
149 if (pTB->m_pMenu == NULL)
150 return eventNotHandledErr;
151
152 // This is the real reason why we need this. Normally menus
153 // get handled in wxMacAppEventHandler
154 //
155 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
156 // EventRef event, void *data)
157 //
158 // However, in the case of a taskbar menu call
159 // command.menu.menuRef IS NULL!
160 // Which causes the wxApp handler just to skip it.
161 MenuRef taskbarMenuRef = MAC_WXHMENU(pTB->m_pMenu->GetHMenu());
162 OSStatus err;
163
164 // get the HICommand from the event
165 HICommand command;
166 err = GetEventParameter(
167 inEvent, kEventParamDirectObject,
168 typeHICommand, NULL,
169 sizeof(HICommand), NULL, &command );
170 if (err == noErr)
171 {
172 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
173 //
174 // NOTE: menuRef is generally used here for submenus, as
175 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
176 // just the top level wxTaskBar menu
177 MenuItemIndex menuItemIndex;
178 MenuRef menuRef;
179
180 err = GetIndMenuItemWithCommandID(
181 taskbarMenuRef,
182 command.commandID,
183 1, &menuRef, &menuItemIndex );
184 if (err == noErr)
185 {
186 MenuCommand id = command.commandID;
187 wxMenuItem *item = NULL;
188
189 if (id != 0) // get the wxMenuItem reference from the MenuRef
190 GetMenuItemRefCon( menuRef, menuItemIndex, (UInt32*) &item );
191
192 if (item)
193 {
194 // Handle items that are checkable
195 // FIXME: Doesn't work (at least on 10.2)!
196 if (item->IsCheckable())
197 item->Check( !item->IsChecked() );
198
199 // send the wxEvent to the wxMenu
200 item->GetMenu()->SendEvent( id, item->IsCheckable() ? item->IsChecked() : -1 );
201
202 // successfully handled the event
203 err = noErr;
204 }
205 }
206 } //end if noErr on getting HICommand from event
207
208 // return whether we handled the event or not
209 return err;
210 }
211
212 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
213 // otherwise something is truly funky
214 wxASSERT(eventClass == kEventClassApplication &&
215 eventKind == kEventAppGetDockTileMenu);
216
217 // process the right click events
218 // NB: This may result in double or even triple-creation of the menus
219 // We need to do this for 2.4 compat, however
220 wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN, NULL);
221 pTB->m_parent->ProcessEvent(downevt);
222
223 wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP, NULL);
224 pTB->m_parent->ProcessEvent(upevt);
225
226 // create popup menu
227 wxMenu* menu = pTB->DoCreatePopupMenu();
228
229 OSStatus err = eventNotHandledErr;
230
231 if (menu != NULL)
232 {
233 // note to self - a MenuRef *is* a MenuHandle
234 MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu());
235
236 // When SetEventParameter is called it will decrement
237 // the reference count of the menu - we need to make
238 // sure it stays around in the wxMenu class here
239 RetainMenu(hMenu);
240
241 // set the actual dock menu
242 err = SetEventParameter(
243 inEvent, kEventParamMenuRef,
244 typeMenuRef, sizeof(MenuRef), &hMenu );
245 verify_noerr( err );
246 }
247
248 return err;
249 }
250
251 //-----------------------------------------------------------------------------
252 // wxDeepCopyMenu
253 //
254 // Performs a top-to-bottom copy of the input menu and all of its
255 // submenus.
256 //
257 // This is mostly needed for 2.4 compatability. However wxPython and others
258 // still use this way of setting the taskbarmenu.
259 //-----------------------------------------------------------------------------
260 wxMenu * wxDeepCopyMenu( wxMenu *menu )
261 {
262 if (menu == NULL)
263 return NULL;
264
265 // NB: Here we have to perform a deep copy of the menu,
266 // copying each and every menu item from menu to m_pMenu.
267 // Other implementations use wxWindow::PopupMenu here,
268 // which idle execution until the user selects something,
269 // but since the Mac handles this internally, we can't -
270 // and have no way at all to idle it while the dock menu
271 // is being shown before menu goes out of scope (it may
272 // not be on the heap, and may expire right after this function
273 // is done - we need it to last until the carbon event is triggered -
274 // that's when the user right clicks).
275 //
276 // Also, since there is no equal (assignment) operator
277 // on either wxMenu or wxMenuItem, we have to do all the
278 // dirty work ourselves.
279
280 // perform a deep copy of the menu
281 wxMenuItemList& theList = menu->GetMenuItems();
282 wxMenuItemList::compatibility_iterator theNode = theList.GetFirst();
283
284 // create the main menu
285 wxMenu *m_pMenu = new wxMenu(menu->GetTitle());
286
287 while (theNode != NULL)
288 {
289 wxMenuItem* theItem = theNode->GetData();
290 m_pMenu->Append(
291 new wxMenuItem(
292 m_pMenu, // parent menu
293 theItem->GetId(), // id
294 theItem->GetText(), // text label
295 theItem->GetHelp(), // status bar help string
296 theItem->GetKind(), // menu flags - checkable, separator, etc.
297 wxDeepCopyMenu(theItem->GetSubMenu()) )); // submenu
298
299 theNode = theNode->GetNext();
300 }
301
302 return m_pMenu;
303 }
304
305 //-----------------------------------------------------------------------------
306 // wxDockTaskBarIcon ctor
307 //
308 // Initializes the dock implementation of wxTaskBarIcon.
309 //
310 // Here we create some Mac-specific event handlers and UPPs.
311 //-----------------------------------------------------------------------------
312 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon* parent)
313 : wxTaskBarIconImpl(parent),
314 m_eventHandlerRef(NULL), m_pMenu(NULL),
315 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
316 {
317 // register the events that will return the dock menu
318 EventTypeSpec tbEventList[] =
319 {
320 { kEventClassCommand, kEventProcessCommand },
321 { kEventClassApplication, kEventAppGetDockTileMenu }
322 };
323
324 m_eventupp = NewEventHandlerUPP(wxDockEventHandler);
325 wxASSERT(m_eventupp != NULL);
326
327 OSStatus err = InstallApplicationEventHandler(
328 m_eventupp,
329 GetEventTypeCount(tbEventList), tbEventList,
330 this, &m_eventHandlerRef);
331 verify_noerr( err );
332 }
333
334 //-----------------------------------------------------------------------------
335 // wxDockTaskBarIcon Destructor
336 //
337 // Cleans up mac events and restores the old icon to the dock
338 //-----------------------------------------------------------------------------
339 wxDockTaskBarIcon::~wxDockTaskBarIcon()
340 {
341 // clean up event handler and event UPP
342 RemoveEventHandler(m_eventHandlerRef);
343 DisposeEventHandlerUPP(m_eventupp);
344
345 // restore old icon and menu to the dock
346 RemoveIcon();
347 }
348
349 //-----------------------------------------------------------------------------
350 // wxDockTaskBarIcon::DoCreatePopupMenu
351 //
352 // Helper function that handles a request from the dock event handler
353 // to get the menu for the dock
354 //-----------------------------------------------------------------------------
355 wxMenu * wxDockTaskBarIcon::DoCreatePopupMenu()
356 {
357 // get the menu from the parent
358 wxMenu* theNewMenu = CreatePopupMenu();
359
360 if (theNewMenu)
361 {
362 if (m_pMenu)
363 delete m_pMenu;
364 m_pMenu = theNewMenu;
365 m_pMenu->SetInvokingWindow(m_menuEventWindow);
366 }
367
368 // the return here can be one of three things
369 // (in order of priority):
370 // 1) User passed a menu from CreatePopupMenu override
371 // 2) menu sent to and copied from PopupMenu
372 // 3) If neither (1) or (2), then NULL
373 //
374 return m_pMenu;
375 }
376
377 //-----------------------------------------------------------------------------
378 // wxDockTaskBarIcon::IsIconInstalled
379 //
380 // Returns whether or not the dock is not using the default image
381 //-----------------------------------------------------------------------------
382 bool wxDockTaskBarIcon::IsIconInstalled() const
383 {
384 return m_iconAdded;
385 }
386
387 //-----------------------------------------------------------------------------
388 // wxDockTaskBarIcon::SetIcon
389 //
390 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
391 //-----------------------------------------------------------------------------
392 bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
393 {
394 // convert the wxIcon into a wxBitmap so we can perform some
395 // wxBitmap operations with it
396 wxBitmap bmp( icon );
397 wxASSERT( bmp.Ok() );
398
399 // get the CGImageRef for the wxBitmap:
400 // OSX builds only, but then the dock only exists in OSX
401 CGImageRef pImage = (CGImageRef) bmp.CGImageCreate();
402 wxASSERT( pImage != NULL );
403
404 // actually set the dock image
405 OSStatus err = SetApplicationDockTileImage( pImage );
406 verify_noerr( err );
407
408 // free the CGImage, now that it's referenced by the dock
409 if (pImage != NULL)
410 CGImageRelease( pImage );
411
412 bool success = (err == noErr);
413 m_iconAdded = success;
414
415 return success;
416 }
417
418 //-----------------------------------------------------------------------------
419 // wxDockTaskBarIcon::RemoveIcon
420 //
421 // Restores the old image for the dock via RestoreApplicationDockTileImage
422 //-----------------------------------------------------------------------------
423 bool wxDockTaskBarIcon::RemoveIcon()
424 {
425 if (m_pMenu)
426 {
427 delete m_pMenu;
428 m_pMenu = NULL;
429 }
430
431 // restore old icon to the dock
432 OSStatus err = RestoreApplicationDockTileImage();
433 verify_noerr( err );
434
435 // restore the old menu to the dock
436 SetApplicationDockTileMenu( m_theLastMenu );
437
438 bool success = (err == noErr);
439 m_iconAdded = !success;
440
441 return success;
442 }
443
444 //-----------------------------------------------------------------------------
445 // wxDockTaskBarIcon::PopupMenu
446 //
447 // 2.4 and wxPython method that "pops of the menu in the taskbar".
448 //
449 // In reality because of the way the dock menu works in carbon
450 // we just save the menu, and if the user didn't override CreatePopupMenu
451 // return the menu passed here, thus sort of getting the same effect.
452 //-----------------------------------------------------------------------------
453 bool wxDockTaskBarIcon::PopupMenu(wxMenu *menu)
454 {
455 wxASSERT(menu != NULL);
456
457 if (m_pMenu)
458 delete m_pMenu;
459
460 // start copy of menu
461 m_pMenu = wxDeepCopyMenu(menu);
462
463 // finish up
464 m_pMenu->SetInvokingWindow(m_menuEventWindow);
465
466 return true;
467 }
468
469 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
470 //
471 // wxTaskBarIcon
472 //
473 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
474
475 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
476
477 //-----------------------------------------------------------------------------
478 // wxTaskBarIcon Constructor
479 //
480 // Creates the backend
481 //
482 // Note that we only support DOCK currently as others require cocoa and
483 // also some require hacks and other such things. (MenuExtras are
484 // actually seperate programs that also require a special undocumented id
485 // hack and other such fun stuff).
486 //-----------------------------------------------------------------------------
487 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType)
488 {
489 wxASSERT_MSG(
490 nType == DOCK,
491 wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
492
493 m_impl = new wxDockTaskBarIcon(this);
494 }
495
496 //-----------------------------------------------------------------------------
497 // wxTaskBarIcon Destructor
498 //
499 // Destroys the backend
500 //-----------------------------------------------------------------------------
501 wxTaskBarIcon::~wxTaskBarIcon()
502 {
503 delete m_impl;
504 }
505
506 //-----------------------------------------------------------------------------
507 // wxTaskBarIcon::SetIcon
508 // wxTaskBarIcon::RemoveIcon
509 // wxTaskBarIcon::PopupMenu
510 //
511 // Just calls the backend version of the said function.
512 //-----------------------------------------------------------------------------
513 bool wxTaskBarIcon::IsIconInstalled() const
514 { return m_impl->IsIconInstalled(); }
515
516 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
517 { return m_impl->SetIcon(icon, tooltip); }
518
519 bool wxTaskBarIcon::RemoveIcon()
520 { return m_impl->RemoveIcon(); }
521
522 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
523 { return m_impl->PopupMenu(menu); }
524
525 #endif // wxHAS_TASK_BAR_ICON