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