1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/taskbar.cpp
3 // Purpose: wxTaskBarIcon
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/taskbar.h"
19 #include "wx/dcmemory.h"
21 #include "wx/toplevel.h"
25 #include "wx/osx/private.h"
27 class wxTaskBarIconImpl
30 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
31 virtual ~wxTaskBarIconImpl();
33 virtual bool IsIconInstalled() const = 0;
34 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
) = 0;
35 virtual bool RemoveIcon() = 0;
36 virtual bool PopupMenu(wxMenu
*menu
) = 0;
38 wxMenu
* CreatePopupMenu()
39 { return m_parent
->CreatePopupMenu(); }
41 wxTaskBarIcon
*m_parent
;
42 class wxTaskBarIconWindow
*m_menuEventWindow
;
44 wxDECLARE_NO_COPY_CLASS(wxTaskBarIconImpl
);
47 //-----------------------------------------------------------------------------
49 // wxTaskBarIconWindow
51 // Event handler for menus
52 // NB: Since wxWindows in Mac HAVE to have parents we need this to be
53 // a top level window...
54 //-----------------------------------------------------------------------------
56 class wxTaskBarIconWindow
: public wxTopLevelWindow
59 wxTaskBarIconWindow(wxTaskBarIconImpl
*impl
)
60 : wxTopLevelWindow(NULL
, wxID_ANY
, wxEmptyString
), m_impl(impl
)
63 -1, wxEVT_COMMAND_MENU_SELECTED
,
64 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
) );
67 void OnMenuEvent(wxCommandEvent
& event
)
69 m_impl
->m_parent
->ProcessEvent(event
);
73 wxTaskBarIconImpl
*m_impl
;
76 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
79 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
80 virtual ~wxDockTaskBarIcon();
82 virtual bool IsIconInstalled() const;
83 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
);
84 virtual bool RemoveIcon();
85 virtual bool PopupMenu(wxMenu
*menu
);
87 wxMenu
* DoCreatePopupMenu();
89 EventHandlerRef m_eventHandlerRef
;
90 EventHandlerUPP m_eventupp
;
91 wxWindow
*m_eventWindow
;
93 MenuRef m_theLastMenu
;
97 // Forward declarations for utility functions for dock implementation
98 pascal OSStatus
wxDockEventHandler(
99 EventHandlerCallRef inHandlerCallRef
,
100 EventRef inEvent
, void* pData
);
101 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
);
104 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
108 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
110 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
111 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
115 wxTaskBarIconImpl::~wxTaskBarIconImpl()
117 delete m_menuEventWindow
;
120 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124 // OS X Dock implementation of wxTaskBarIcon using Carbon
125 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127 //-----------------------------------------------------------------------------
128 // wxDockEventHandler
130 // This is the global Mac/Carbon event handler for the dock.
131 // We need this for two reasons:
132 // 1) To handle wxTaskBarIcon menu events (see below for why)
133 // 2) To handle events from the dock when it requests a menu
134 //-----------------------------------------------------------------------------
136 wxDockEventHandler(EventHandlerCallRef
WXUNUSED(inHandlerCallRef
),
140 // Get the parameters we want from the event
141 wxDockTaskBarIcon
* pTB
= (wxDockTaskBarIcon
*) pData
;
142 const UInt32 eventClass
= GetEventClass(inEvent
);
143 const UInt32 eventKind
= GetEventKind(inEvent
);
145 OSStatus err
= eventNotHandledErr
;
147 // Handle wxTaskBar menu events (note that this is a global event handler
148 // so it will actually get called by all commands/menus)
149 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
|| eventKind
== kEventCommandUpdateStatus
))
151 // if we have no taskbar menu quickly pass it back to wxApp
152 if (pTB
->m_pMenu
!= NULL
)
154 // This is the real reason why we need this. Normally menus
155 // get handled in wxMacAppEventHandler
156 // However, in the case of a taskbar menu call
157 // command.menu.menuRef IS NULL!
158 // Which causes the wxApp handler just to skip it.
160 // get the HICommand from the event
162 if (GetEventParameter(inEvent
, kEventParamDirectObject
,
163 typeHICommand
, NULL
,sizeof(HICommand
), NULL
, &command
) == noErr
)
165 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
167 // NOTE: menuRef is generally used here for submenus, as
168 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
169 // just the top level wxTaskBar menu
170 MenuItemIndex menuItemIndex
;
172 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
174 // the next command is only successful if it was a command from the taskbar menu
175 // otherwise we pass it on
176 if (GetIndMenuItemWithCommandID(taskbarMenuRef
,command
.commandID
,
177 1, &menuRef
, &menuItemIndex
) == noErr
)
179 wxMenu
* itemMenu
= wxFindMenuFromMacMenu( menuRef
) ;
180 int id
= wxMacCommandToId( command
.commandID
) ;
181 wxMenuItem
*item
= NULL
;
183 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
184 GetMenuItemRefCon( menuRef
, menuItemIndex
, (URefCon
*) &item
);
186 if (item
&& itemMenu
)
188 if ( eventKind
== kEventCommandProcess
)
190 if ( itemMenu
->HandleCommandProcess( item
) )
193 else if ( eventKind
== kEventCommandUpdateStatus
)
195 if ( itemMenu
->HandleCommandUpdateStatus( item
) )
201 } //end if noErr on getting HICommand from event
203 else if ((eventClass
== kEventClassApplication
) && (eventKind
== kEventAppGetDockTileMenu
))
205 // process the right click events
206 // NB: This may result in double or even triple-creation of the menus
207 // We need to do this for 2.4 compat, however
208 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
, NULL
);
209 pTB
->m_parent
->ProcessEvent(downevt
);
211 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
, NULL
);
212 pTB
->m_parent
->ProcessEvent(upevt
);
215 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
219 // note to self - a MenuRef *is* a MenuHandle
220 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
222 // When SetEventParameter is called it will decrement
223 // the reference count of the menu - we need to make
224 // sure it stays around in the wxMenu class here
227 // set the actual dock menu
228 err
= SetEventParameter(
229 inEvent
, kEventParamMenuRef
,
230 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
238 //-----------------------------------------------------------------------------
241 // Performs a top-to-bottom copy of the input menu and all of its
244 // This is mostly needed for 2.4 compatability. However wxPython and others
245 // still use this way of setting the taskbarmenu.
246 //-----------------------------------------------------------------------------
247 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
)
252 // NB: Here we have to perform a deep copy of the menu,
253 // copying each and every menu item from menu to m_pMenu.
254 // Other implementations use wxWindow::PopupMenu here,
255 // which idle execution until the user selects something,
256 // but since the Mac handles this internally, we can't -
257 // and have no way at all to idle it while the dock menu
258 // is being shown before menu goes out of scope (it may
259 // not be on the heap, and may expire right after this function
260 // is done - we need it to last until the carbon event is triggered -
261 // that's when the user right clicks).
263 // Also, since there is no equal (assignment) operator
264 // on either wxMenu or wxMenuItem, we have to do all the
265 // dirty work ourselves.
267 // perform a deep copy of the menu
268 wxMenuItemList
& theList
= menu
->GetMenuItems();
269 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
271 // create the main menu
272 wxMenu
*m_pMenu
= new wxMenu(menu
->GetTitle());
274 while (theNode
!= NULL
)
276 wxMenuItem
* theItem
= theNode
->GetData();
279 m_pMenu
, // parent menu
280 theItem
->GetId(), // id
281 theItem
->GetItemLabel(), // text label
282 theItem
->GetHelp(), // status bar help string
283 theItem
->GetKind(), // menu flags - checkable, separator, etc.
284 wxDeepCopyMenu(theItem
->GetSubMenu()) )); // submenu
286 theNode
= theNode
->GetNext();
292 //-----------------------------------------------------------------------------
293 // wxDockTaskBarIcon ctor
295 // Initializes the dock implementation of wxTaskBarIcon.
297 // Here we create some Mac-specific event handlers and UPPs.
298 //-----------------------------------------------------------------------------
299 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon
* parent
)
300 : wxTaskBarIconImpl(parent
),
301 m_eventHandlerRef(NULL
), m_pMenu(NULL
),
302 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
304 // register the events that will return the dock menu
305 EventTypeSpec tbEventList
[] =
307 { kEventClassCommand
, kEventProcessCommand
},
308 { kEventClassCommand
, kEventCommandUpdateStatus
},
309 { kEventClassApplication
, kEventAppGetDockTileMenu
}
312 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
313 wxASSERT(m_eventupp
!= NULL
);
315 OSStatus err
= InstallApplicationEventHandler(
317 GetEventTypeCount(tbEventList
), tbEventList
,
318 this, &m_eventHandlerRef
);
322 //-----------------------------------------------------------------------------
323 // wxDockTaskBarIcon Destructor
325 // Cleans up mac events and restores the old icon to the dock
326 //-----------------------------------------------------------------------------
327 wxDockTaskBarIcon::~wxDockTaskBarIcon()
329 // clean up event handler and event UPP
330 RemoveEventHandler(m_eventHandlerRef
);
331 DisposeEventHandlerUPP(m_eventupp
);
333 // restore old icon and menu to the dock
337 //-----------------------------------------------------------------------------
338 // wxDockTaskBarIcon::DoCreatePopupMenu
340 // Helper function that handles a request from the dock event handler
341 // to get the menu for the dock
342 //-----------------------------------------------------------------------------
343 wxMenu
* wxDockTaskBarIcon::DoCreatePopupMenu()
345 // get the menu from the parent
346 wxMenu
* theNewMenu
= CreatePopupMenu();
351 m_pMenu
= theNewMenu
;
352 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
355 // the return here can be one of three things
356 // (in order of priority):
357 // 1) User passed a menu from CreatePopupMenu override
358 // 2) menu sent to and copied from PopupMenu
359 // 3) If neither (1) or (2), then NULL
364 //-----------------------------------------------------------------------------
365 // wxDockTaskBarIcon::IsIconInstalled
367 // Returns whether or not the dock is not using the default image
368 //-----------------------------------------------------------------------------
369 bool wxDockTaskBarIcon::IsIconInstalled() const
374 //-----------------------------------------------------------------------------
375 // wxDockTaskBarIcon::SetIcon
377 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
378 //-----------------------------------------------------------------------------
379 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& WXUNUSED(tooltip
))
381 // convert the wxIcon into a wxBitmap so we can perform some
382 // wxBitmap operations with it
383 wxBitmap
bmp( icon
);
384 wxASSERT( bmp
.Ok() );
386 // get the CGImageRef for the wxBitmap:
387 // OSX builds only, but then the dock only exists in OSX
388 CGImageRef pImage
= (CGImageRef
) bmp
.CreateCGImage();
389 wxASSERT( pImage
!= NULL
);
391 // actually set the dock image
392 OSStatus err
= SetApplicationDockTileImage( pImage
);
395 // free the CGImage, now that it's referenced by the dock
397 CGImageRelease( pImage
);
399 bool success
= (err
== noErr
);
400 m_iconAdded
= success
;
405 //-----------------------------------------------------------------------------
406 // wxDockTaskBarIcon::RemoveIcon
408 // Restores the old image for the dock via RestoreApplicationDockTileImage
409 //-----------------------------------------------------------------------------
410 bool wxDockTaskBarIcon::RemoveIcon()
418 // restore old icon to the dock
419 OSStatus err
= RestoreApplicationDockTileImage();
422 // restore the old menu to the dock
423 SetApplicationDockTileMenu( m_theLastMenu
);
425 bool success
= (err
== noErr
);
426 m_iconAdded
= !success
;
431 //-----------------------------------------------------------------------------
432 // wxDockTaskBarIcon::PopupMenu
434 // 2.4 and wxPython method that "pops of the menu in the taskbar".
436 // In reality because of the way the dock menu works in carbon
437 // we just save the menu, and if the user didn't override CreatePopupMenu
438 // return the menu passed here, thus sort of getting the same effect.
439 //-----------------------------------------------------------------------------
440 bool wxDockTaskBarIcon::PopupMenu(wxMenu
*menu
)
442 wxASSERT(menu
!= NULL
);
446 // start copy of menu
447 m_pMenu
= wxDeepCopyMenu(menu
);
450 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
455 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
459 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
461 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
463 //-----------------------------------------------------------------------------
464 // wxTaskBarIcon Constructor
466 // Creates the backend
468 // Note that we only support DOCK currently as others require cocoa and
469 // also some require hacks and other such things. (MenuExtras are
470 // actually seperate programs that also require a special undocumented id
471 // hack and other such fun stuff).
472 //-----------------------------------------------------------------------------
473 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType
WXUNUSED_UNLESS_DEBUG(nType
))
477 wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
479 m_impl
= new wxDockTaskBarIcon(this);
482 //-----------------------------------------------------------------------------
483 // wxTaskBarIcon Destructor
485 // Destroys the backend
486 //-----------------------------------------------------------------------------
487 wxTaskBarIcon::~wxTaskBarIcon()
492 //-----------------------------------------------------------------------------
493 // wxTaskBarIcon::SetIcon
494 // wxTaskBarIcon::RemoveIcon
495 // wxTaskBarIcon::PopupMenu
497 // Just calls the backend version of the said function.
498 //-----------------------------------------------------------------------------
499 bool wxTaskBarIcon::IsIconInstalled() const
500 { return m_impl
->IsIconInstalled(); }
502 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
503 { return m_impl
->SetIcon(icon
, tooltip
); }
505 bool wxTaskBarIcon::RemoveIcon()
506 { return m_impl
->RemoveIcon(); }
508 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
509 { return m_impl
->PopupMenu(menu
); }
511 #endif // wxUSE_TASKBARICON