1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTaskBarIcon - OSX implementation
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 //=============================================================================
14 //=============================================================================
16 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
22 #ifdef wxHAS_TASK_BAR_ICON
24 #include "wx/mac/private.h"
26 #include "wx/taskbar.h"
29 #include "wx/dcmemory.h"
31 //-----------------------------------------------------------------------------
35 // Superclass of wxTaskBarIcon implementations
36 //-----------------------------------------------------------------------------
38 class wxTaskBarIconImpl
41 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
42 virtual ~wxTaskBarIconImpl();
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;
49 wxMenu
* CreatePopupMenu()
50 { return m_parent
->CreatePopupMenu(); }
52 wxTaskBarIcon
* m_parent
;
53 class wxTaskBarIconWindow
* m_menuEventWindow
;
56 //-----------------------------------------------------------------------------
58 // wxTaskBarIconWindow
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 //-----------------------------------------------------------------------------
65 class wxTaskBarIconWindow
: public wxTopLevelWindow
68 wxTaskBarIconWindow(wxTaskBarIconImpl
* impl
)
69 : wxTopLevelWindow(NULL
, -1, wxT("")), m_impl(impl
)
71 Connect(-1, wxEVT_COMMAND_MENU_SELECTED
,
72 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
)
76 void OnMenuEvent(wxCommandEvent
& event
)
78 m_impl
->m_parent
->ProcessEvent(event
);
82 wxTaskBarIconImpl
* m_impl
;
85 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
94 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
95 virtual ~wxDockTaskBarIcon();
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
);
102 wxMenu
* DoCreatePopupMenu();
104 EventHandlerRef m_eventHandlerRef
;
105 EventHandlerUPP m_eventupp
;
106 wxWindow
* m_eventWindow
;
108 MenuRef m_theLastMenu
;
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
);
118 //=============================================================================
122 //=============================================================================
124 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
128 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130 //-----------------------------------------------------------------------------
131 // wxTaskBarIconImpl Constructor
133 // Initializes members and creates the event window
134 //-----------------------------------------------------------------------------
135 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
136 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
140 //-----------------------------------------------------------------------------
141 // wxTaskBarIconImpl Destructor
143 // Cleans up the event window
144 //-----------------------------------------------------------------------------
145 wxTaskBarIconImpl::~wxTaskBarIconImpl()
147 delete m_menuEventWindow
;
150 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
154 // OS X DOCK implementation of wxTaskBarIcon using carbon
155 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 // wxDockEventHandler
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
)
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
);
173 // Handle wxTaskBar menu events (note that this is a global event handler
174 // so it will actually get called by all commands/menus)
176 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
))
178 // if we have no taskbar menu quickly pass it back to wxApp
179 if (pTB
->m_pMenu
== NULL
)
180 return eventNotHandledErr
;
183 // This is the real reason why we need this. Normally menus
184 // get handled in wxMacAppEventHandler
186 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
187 // EventRef event, void *data)
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.
193 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
194 OSStatus result
= eventNotHandledErr
;
197 // get the HICommand from the event
199 err
= GetEventParameter(inEvent
, kEventParamDirectObject
,
201 sizeof(HICommand
), NULL
, &command
);
204 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
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
210 MenuItemIndex menuItemIndex
;
213 err
= GetIndMenuItemWithCommandID(taskbarMenuRef
,
215 1, &menuRef
, &menuItemIndex
);
218 MenuCommand id
= command
.commandID
;
219 wxMenuItem
* item
= NULL
;
221 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
222 GetMenuItemRefCon(menuRef
, menuItemIndex
, (UInt32
*) &item
);
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() ) ;
231 // send the wxEvent to the wxMenu
232 item
->GetMenu()->SendEvent(id
,
233 item
->IsCheckable() ?
234 item
->IsChecked() : -1
236 err
= noErr
; // successfully handled the event
239 } //end if noErr on getting HICommand from event
241 // return whether we handled the event or not
245 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
246 // otherwise something is truly funky
247 wxASSERT(eventClass
== kEventClassApplication
&&
248 eventKind
== kEventAppGetDockTileMenu
);
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
);
256 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
257 pTB
->m_parent
->ProcessEvent(upevt
);
260 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
262 OSStatus err
= eventNotHandledErr
;
266 // note to self - a MenuRef *is* a MenuHandle
267 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
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
274 // set the actual dock menu
275 err
= SetEventParameter(inEvent
, kEventParamMenuRef
,
276 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
277 wxASSERT(err
== noErr
);
283 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 // Performs a top-to-bottom copy of the input menu and all of its
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
)
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).
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.
314 // perform a deep copy of the menu
315 wxMenuItemList
& theList
= menu
->GetMenuItems();
316 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
318 // create the main menu
319 wxMenu
* m_pMenu
= new wxMenu(menu
->GetTitle());
321 while (theNode
!= NULL
)
323 wxMenuItem
* theItem
= theNode
->GetData();
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
333 theNode
= theNode
->GetNext();
339 //-----------------------------------------------------------------------------
340 // wxDockTaskBarIcon ctor
342 // Initializes the dock implementation of wxTaskBarIcon.
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)
351 // register the events that will return the dock menu
352 EventTypeSpec tbEventList
[] =
354 { kEventClassCommand
, kEventProcessCommand
},
355 { kEventClassApplication
, kEventAppGetDockTileMenu
}
358 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
359 wxASSERT(m_eventupp
!= NULL
);
364 InstallApplicationEventHandler(
366 GetEventTypeCount(tbEventList
), tbEventList
,
367 this, &m_eventHandlerRef
);
368 wxASSERT( err
== noErr
);
371 //-----------------------------------------------------------------------------
372 // wxDockTaskBarIcon Destructor
374 // Cleans up mac events and restores the old icon to the dock
375 //-----------------------------------------------------------------------------
376 wxDockTaskBarIcon::~wxDockTaskBarIcon()
378 // clean up event handler and event UPP
379 RemoveEventHandler(m_eventHandlerRef
);
380 DisposeEventHandlerUPP(m_eventupp
);
382 // restore old icon and menu to the dock
386 //-----------------------------------------------------------------------------
387 // wxDockTaskBarIcon::DoCreatePopupMenu
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()
394 // get the menu from the parent
395 wxMenu
* theNewMenu
= CreatePopupMenu();
401 m_pMenu
= theNewMenu
;
402 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
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
414 //-----------------------------------------------------------------------------
415 // wxDockTaskBarIcon::IsIconInstalled
417 // Returns whether or not the dock is not using the default image
418 //-----------------------------------------------------------------------------
419 bool wxDockTaskBarIcon::IsIconInstalled() const
424 //-----------------------------------------------------------------------------
425 // wxDockTaskBarIcon::SetIcon
427 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
428 //-----------------------------------------------------------------------------
429 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
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() );
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
);
441 // actually set the dock image
442 OSStatus err
= SetApplicationDockTileImage( pImage
);
443 wxASSERT( err
== noErr
);
445 // free the CGImage, now that it's referenced by the dock
447 CGImageRelease( pImage
);
449 return m_iconAdded
= (err
== noErr
);
452 //-----------------------------------------------------------------------------
453 // wxDockTaskBarIcon::RemoveIcon
455 // Restores the old image for the dock via RestoreApplicationDockTileImage
456 //-----------------------------------------------------------------------------
457 bool wxDockTaskBarIcon::RemoveIcon()
465 // restore old icon to the dock
466 OSStatus err
= RestoreApplicationDockTileImage();
467 wxASSERT(err
== noErr
);
469 // restore the old menu to the dock
470 SetApplicationDockTileMenu(m_theLastMenu
);
472 return !(m_iconAdded
= !(err
== noErr
));
475 //-----------------------------------------------------------------------------
476 // wxDockTaskBarIcon::PopupMenu
478 // 2.4 and wxPython method that "pops of the menu in the taskbar".
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
)
486 wxASSERT(menu
!= NULL
);
492 m_pMenu
= wxDeepCopyMenu(menu
);
495 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
500 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
504 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
506 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
508 //-----------------------------------------------------------------------------
509 // wxTaskBarIcon Constructor
511 // Creates the backend
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
)
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);
526 //-----------------------------------------------------------------------------
527 // wxTaskBarIcon Destructor
529 // Destroys the backend
530 //-----------------------------------------------------------------------------
531 wxTaskBarIcon::~wxTaskBarIcon()
536 //-----------------------------------------------------------------------------
537 // wxTaskBarIcon::SetIcon
538 // wxTaskBarIcon::RemoveIcon
539 // wxTaskBarIcon::PopupMenu
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
); }
552 #endif //wxHAS_TASK_BAR_ICON