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
;
55 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl
)
58 //-----------------------------------------------------------------------------
60 // wxTaskBarIconWindow
62 // Event handler for menus
63 // NB: Since wxWindows in mac HAVE to have parents we need this to be
64 // a top level window...
65 //-----------------------------------------------------------------------------
67 class wxTaskBarIconWindow
: public wxTopLevelWindow
70 wxTaskBarIconWindow(wxTaskBarIconImpl
* impl
)
71 : wxTopLevelWindow(NULL
, -1, wxT("")), m_impl(impl
)
73 Connect(-1, wxEVT_COMMAND_MENU_SELECTED
,
74 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
)
78 void OnMenuEvent(wxCommandEvent
& event
)
80 m_impl
->m_parent
->ProcessEvent(event
);
84 wxTaskBarIconImpl
* m_impl
;
87 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
93 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
96 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
97 virtual ~wxDockTaskBarIcon();
99 virtual bool IsIconInstalled() const;
100 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
);
101 virtual bool RemoveIcon();
102 virtual bool PopupMenu(wxMenu
*menu
);
104 wxMenu
* DoCreatePopupMenu();
106 EventHandlerRef m_eventHandlerRef
;
107 EventHandlerUPP m_eventupp
;
108 wxWindow
* m_eventWindow
;
110 MenuRef m_theLastMenu
;
114 // Forward declarations for utility functions for dock implementation
115 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
116 EventRef inEvent
, void* pData
);
117 wxMenu
* wxDeepCopyMenu(wxMenu
* menu
);
120 //=============================================================================
124 //=============================================================================
126 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
132 //-----------------------------------------------------------------------------
133 // wxTaskBarIconImpl Constructor
135 // Initializes members and creates the event window
136 //-----------------------------------------------------------------------------
137 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
138 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
142 //-----------------------------------------------------------------------------
143 // wxTaskBarIconImpl Destructor
145 // Cleans up the event window
146 //-----------------------------------------------------------------------------
147 wxTaskBarIconImpl::~wxTaskBarIconImpl()
149 delete m_menuEventWindow
;
152 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
156 // OS X DOCK implementation of wxTaskBarIcon using carbon
157 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 // wxDockEventHandler
162 // This is the global mac/carbon event handler for the dock.
163 // We need this for two reasons:
164 // 1) To handle wxTaskBarIcon menu events (see below for why)
165 // 2) To handle events from the dock when it requests a menu
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
168 EventRef inEvent
, void* pData
)
170 // Get the parameters we want from the event
171 wxDockTaskBarIcon
* pTB
= (wxDockTaskBarIcon
*) pData
;
172 const UInt32 eventClass
= GetEventClass(inEvent
);
173 const UInt32 eventKind
= GetEventKind(inEvent
);
175 // Handle wxTaskBar menu events (note that this is a global event handler
176 // so it will actually get called by all commands/menus)
178 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
))
180 // if we have no taskbar menu quickly pass it back to wxApp
181 if (pTB
->m_pMenu
== NULL
)
182 return eventNotHandledErr
;
185 // This is the real reason why we need this. Normally menus
186 // get handled in wxMacAppEventHandler
188 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
189 // EventRef event, void *data)
191 // However, in the case of a taskbar menu call
192 // command.menu.menuRef IS NULL!
193 // Which causes the wxApp handler just to skip it.
195 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
198 // get the HICommand from the event
200 err
= GetEventParameter(inEvent
, kEventParamDirectObject
,
202 sizeof(HICommand
), NULL
, &command
);
205 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
207 // NOTE: menuRef is generally used here for submenus, as
208 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
209 // just the top level wxTaskBar menu
211 MenuItemIndex menuItemIndex
;
214 err
= GetIndMenuItemWithCommandID(taskbarMenuRef
,
216 1, &menuRef
, &menuItemIndex
);
219 MenuCommand id
= command
.commandID
;
220 wxMenuItem
* item
= NULL
;
222 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
223 GetMenuItemRefCon(menuRef
, menuItemIndex
, (UInt32
*) &item
);
227 // Handle items that are checkable
228 // FIXME: Doesn't work (at least on 10.2)!
229 if (item
->IsCheckable())
230 item
->Check( !item
->IsChecked() ) ;
232 // send the wxEvent to the wxMenu
233 item
->GetMenu()->SendEvent(id
,
234 item
->IsCheckable() ?
235 item
->IsChecked() : -1
237 err
= noErr
; // successfully handled the event
240 } //end if noErr on getting HICommand from event
242 // return whether we handled the event or not
246 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
247 // otherwise something is truly funky
248 wxASSERT(eventClass
== kEventClassApplication
&&
249 eventKind
== kEventAppGetDockTileMenu
);
251 // process the right click events
252 // NB: This may result in double or even triple-creation of the menus
253 // We need to do this for 2.4 compat, however
254 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
,NULL
);
255 pTB
->m_parent
->ProcessEvent(downevt
);
257 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
258 pTB
->m_parent
->ProcessEvent(upevt
);
261 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
263 OSStatus err
= eventNotHandledErr
;
267 // note to self - a MenuRef *is* a MenuHandle
268 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
270 // When SetEventParameter is called it will decrement
271 // the reference count of the menu - we need to make
272 // sure it stays around in the wxMenu class here
275 // set the actual dock menu
276 err
= SetEventParameter(inEvent
, kEventParamMenuRef
,
277 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
278 wxASSERT(err
== noErr
);
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 // Performs a top-to-bottom copy of the input menu and all of its
290 // This is mostly needed for 2.4 compatability. However wxPython and others
291 // still use this way of setting the taskbarmenu.
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 wxMenu
* wxDeepCopyMenu(wxMenu
* menu
)
299 // NB: Here we have to perform a deep copy of the menu,
300 // copying each and every menu item from menu to m_pMenu.
301 // Other implementations use wxWindow::PopupMenu here,
302 // which idle execution until the user selects something,
303 // but since the mac handles this internally, we can't -
304 // and have no way at all to idle it while the dock menu
305 // is being shown before menu goes out of scope (it may
306 // not be on the heap, and may expire right after this function
307 // is done - we need it to last until the carbon event is triggered -
308 // that's when the user right clicks).
310 // Also, since there is no equal (assignment) operator
311 // on either wxMenu or wxMenuItem, we have to do all the
312 // dirty work ourselves.
315 // perform a deep copy of the menu
316 wxMenuItemList
& theList
= menu
->GetMenuItems();
317 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
319 // create the main menu
320 wxMenu
* m_pMenu
= new wxMenu(menu
->GetTitle());
322 while (theNode
!= NULL
)
324 wxMenuItem
* theItem
= theNode
->GetData();
327 m_pMenu
, // parent menu
328 theItem
->GetId(), // id
329 theItem
->GetText(), // text label
330 theItem
->GetHelp(), // status bar help string
331 theItem
->GetKind(), // menu flags - checkable, separator, etc.
332 wxDeepCopyMenu(theItem
->GetSubMenu()) // submenu
334 theNode
= theNode
->GetNext();
340 //-----------------------------------------------------------------------------
341 // wxDockTaskBarIcon ctor
343 // Initializes the dock implementation of wxTaskBarIcon.
345 // Here we create some mac-specific event handlers and UPPs.
346 //-----------------------------------------------------------------------------
347 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon
* parent
)
348 : wxTaskBarIconImpl(parent
),
349 m_eventHandlerRef(NULL
), m_pMenu(NULL
),
350 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
352 // register the events that will return the dock menu
353 EventTypeSpec tbEventList
[] =
355 { kEventClassCommand
, kEventProcessCommand
},
356 { kEventClassApplication
, kEventAppGetDockTileMenu
}
359 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
360 wxASSERT(m_eventupp
!= NULL
);
365 InstallApplicationEventHandler(
367 GetEventTypeCount(tbEventList
), tbEventList
,
368 this, &m_eventHandlerRef
);
369 wxASSERT( err
== noErr
);
372 //-----------------------------------------------------------------------------
373 // wxDockTaskBarIcon Destructor
375 // Cleans up mac events and restores the old icon to the dock
376 //-----------------------------------------------------------------------------
377 wxDockTaskBarIcon::~wxDockTaskBarIcon()
379 // clean up event handler and event UPP
380 RemoveEventHandler(m_eventHandlerRef
);
381 DisposeEventHandlerUPP(m_eventupp
);
383 // restore old icon and menu to the dock
387 //-----------------------------------------------------------------------------
388 // wxDockTaskBarIcon::DoCreatePopupMenu
390 // Helper function that handles a request from the dock event handler
391 // to get the menu for the dock
392 //-----------------------------------------------------------------------------
393 wxMenu
* wxDockTaskBarIcon::DoCreatePopupMenu()
395 // get the menu from the parent
396 wxMenu
* theNewMenu
= CreatePopupMenu();
402 m_pMenu
= theNewMenu
;
403 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
406 // the return here can be one of three things
407 // (in order of priority):
408 // 1) User passed a menu from CreatePopupMenu override
409 // 2) menu sent to and copied from PopupMenu
410 // 3) If neither (1) or (2), then NULL
415 //-----------------------------------------------------------------------------
416 // wxDockTaskBarIcon::IsIconInstalled
418 // Returns whether or not the dock is not using the default image
419 //-----------------------------------------------------------------------------
420 bool wxDockTaskBarIcon::IsIconInstalled() const
425 //-----------------------------------------------------------------------------
426 // wxDockTaskBarIcon::SetIcon
428 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
429 //-----------------------------------------------------------------------------
430 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
432 // convert the wxIcon into a wxBitmap so we can perform some
433 // wxBitmap operations with it
434 wxBitmap
bmp( icon
) ;
435 wxASSERT( bmp
.Ok() );
437 // get the CGImageRef for the wxBitmap:
438 // OSX builds only, but then the dock only exists in OSX
439 CGImageRef pImage
= (CGImageRef
) bmp
.CGImageCreate();
440 wxASSERT( pImage
!= NULL
);
442 // actually set the dock image
443 OSStatus err
= SetApplicationDockTileImage( pImage
);
444 wxASSERT( err
== noErr
);
446 // free the CGImage, now that it's referenced by the dock
448 CGImageRelease( pImage
);
450 bool success
= (err
== noErr
);
451 m_iconAdded
= success
;
456 //-----------------------------------------------------------------------------
457 // wxDockTaskBarIcon::RemoveIcon
459 // Restores the old image for the dock via RestoreApplicationDockTileImage
460 //-----------------------------------------------------------------------------
461 bool wxDockTaskBarIcon::RemoveIcon()
469 // restore old icon to the dock
470 OSStatus err
= RestoreApplicationDockTileImage();
471 wxASSERT(err
== noErr
);
473 // restore the old menu to the dock
474 SetApplicationDockTileMenu(m_theLastMenu
);
476 bool success
= (err
== noErr
);
477 m_iconAdded
= !success
;
482 //-----------------------------------------------------------------------------
483 // wxDockTaskBarIcon::PopupMenu
485 // 2.4 and wxPython method that "pops of the menu in the taskbar".
487 // In reality because of the way the dock menu works in carbon
488 // we just save the menu, and if the user didn't override CreatePopupMenu
489 // return the menu passed here, thus sort of getting the same effect.
490 //-----------------------------------------------------------------------------
491 bool wxDockTaskBarIcon::PopupMenu(wxMenu
*menu
)
493 wxASSERT(menu
!= NULL
);
499 m_pMenu
= wxDeepCopyMenu(menu
);
502 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
507 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
511 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
513 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
515 //-----------------------------------------------------------------------------
516 // wxTaskBarIcon Constructor
518 // Creates the backend
520 // Note that we only support DOCK currently as others require cocoa and
521 // also some require hacks and other such things. (MenuExtras are
522 // actually seperate programs that also require a special undocumented id
523 // hack and other such fun stuff).
524 //-----------------------------------------------------------------------------
525 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType
)
527 wxASSERT_MSG(nType
== DOCK
,
528 wxT("Only the DOCK implementation of wxTaskBarIcon")
529 wxT("on mac carbon is currently supported!"));
530 m_impl
= new wxDockTaskBarIcon(this);
533 //-----------------------------------------------------------------------------
534 // wxTaskBarIcon Destructor
536 // Destroys the backend
537 //-----------------------------------------------------------------------------
538 wxTaskBarIcon::~wxTaskBarIcon()
543 //-----------------------------------------------------------------------------
544 // wxTaskBarIcon::SetIcon
545 // wxTaskBarIcon::RemoveIcon
546 // wxTaskBarIcon::PopupMenu
548 // Just calls the backend version of the said function.
549 //-----------------------------------------------------------------------------
550 bool wxTaskBarIcon::IsIconInstalled() const
551 { return m_impl
->IsIconInstalled(); }
552 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
553 { return m_impl
->SetIcon(icon
, tooltip
); }
554 bool wxTaskBarIcon::RemoveIcon()
555 { return m_impl
->RemoveIcon(); }
556 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
557 { return m_impl
->PopupMenu(menu
); }
559 #endif //wxHAS_TASK_BAR_ICON