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