1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/taskbar.cpp
3 // Purpose: wxTaskBarIcon
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #ifdef wxHAS_TASK_BAR_ICON
16 #include "wx/mac/private.h"
18 #include "wx/taskbar.h"
21 #include "wx/dcmemory.h"
24 class wxTaskBarIconImpl
27 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
28 virtual ~wxTaskBarIconImpl();
30 virtual bool IsIconInstalled() const = 0;
31 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
) = 0;
32 virtual bool RemoveIcon() = 0;
33 virtual bool PopupMenu(wxMenu
*menu
) = 0;
35 wxMenu
* CreatePopupMenu()
36 { return m_parent
->CreatePopupMenu(); }
38 wxTaskBarIcon
*m_parent
;
39 class wxTaskBarIconWindow
*m_menuEventWindow
;
41 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl
)
44 //-----------------------------------------------------------------------------
46 // wxTaskBarIconWindow
48 // Event handler for menus
49 // NB: Since wxWindows in Mac HAVE to have parents we need this to be
50 // a top level window...
51 //-----------------------------------------------------------------------------
53 class wxTaskBarIconWindow
: public wxTopLevelWindow
56 wxTaskBarIconWindow(wxTaskBarIconImpl
*impl
)
57 : wxTopLevelWindow(NULL
, -1, wxT("")), m_impl(impl
)
60 -1, wxEVT_COMMAND_MENU_SELECTED
,
61 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
) );
64 void OnMenuEvent(wxCommandEvent
& event
)
66 m_impl
->m_parent
->ProcessEvent(event
);
70 wxTaskBarIconImpl
*m_impl
;
73 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
76 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
77 virtual ~wxDockTaskBarIcon();
79 virtual bool IsIconInstalled() const;
80 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
);
81 virtual bool RemoveIcon();
82 virtual bool PopupMenu(wxMenu
*menu
);
84 wxMenu
* DoCreatePopupMenu();
86 EventHandlerRef m_eventHandlerRef
;
87 EventHandlerUPP m_eventupp
;
88 wxWindow
*m_eventWindow
;
90 MenuRef m_theLastMenu
;
94 // Forward declarations for utility functions for dock implementation
95 pascal OSStatus
wxDockEventHandler(
96 EventHandlerCallRef inHandlerCallRef
,
97 EventRef inEvent
, void* pData
);
98 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
);
101 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
107 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
108 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
112 wxTaskBarIconImpl::~wxTaskBarIconImpl()
114 delete m_menuEventWindow
;
117 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121 // OS X Dock implementation of wxTaskBarIcon using Carbon
122 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124 //-----------------------------------------------------------------------------
125 // wxDockEventHandler
127 // This is the global Mac/Carbon event handler for the dock.
128 // We need this for two reasons:
129 // 1) To handle wxTaskBarIcon menu events (see below for why)
130 // 2) To handle events from the dock when it requests a menu
131 //-----------------------------------------------------------------------------
132 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
133 EventRef inEvent
, void *pData
)
135 // Get the parameters we want from the event
136 wxDockTaskBarIcon
* pTB
= (wxDockTaskBarIcon
*) pData
;
137 const UInt32 eventClass
= GetEventClass(inEvent
);
138 const UInt32 eventKind
= GetEventKind(inEvent
);
140 // Handle wxTaskBar menu events (note that this is a global event handler
141 // so it will actually get called by all commands/menus)
142 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
))
144 // if we have no taskbar menu quickly pass it back to wxApp
145 if (pTB
->m_pMenu
== NULL
)
146 return eventNotHandledErr
;
148 // This is the real reason why we need this. Normally menus
149 // get handled in wxMacAppEventHandler
151 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
152 // EventRef event, void *data)
154 // However, in the case of a taskbar menu call
155 // command.menu.menuRef IS NULL!
156 // Which causes the wxApp handler just to skip it.
157 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
160 // get the HICommand from the event
162 err
= GetEventParameter(
163 inEvent
, kEventParamDirectObject
,
165 sizeof(HICommand
), NULL
, &command
);
168 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
170 // NOTE: menuRef is generally used here for submenus, as
171 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
172 // just the top level wxTaskBar menu
173 MenuItemIndex menuItemIndex
;
176 err
= GetIndMenuItemWithCommandID(
179 1, &menuRef
, &menuItemIndex
);
182 MenuCommand id
= command
.commandID
;
183 wxMenuItem
*item
= NULL
;
185 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
186 GetMenuItemRefCon( menuRef
, menuItemIndex
, (UInt32
*) &item
);
190 // Handle items that are checkable
191 // FIXME: Doesn't work (at least on 10.2)!
192 if (item
->IsCheckable())
193 item
->Check( !item
->IsChecked() );
195 // send the wxEvent to the wxMenu
196 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 );
198 // successfully handled the event
202 } //end if noErr on getting HICommand from event
204 // return whether we handled the event or not
208 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
209 // otherwise something is truly funky
210 wxASSERT(eventClass
== kEventClassApplication
&&
211 eventKind
== kEventAppGetDockTileMenu
);
213 // process the right click events
214 // NB: This may result in double or even triple-creation of the menus
215 // We need to do this for 2.4 compat, however
216 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
, NULL
);
217 pTB
->m_parent
->ProcessEvent(downevt
);
219 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
, NULL
);
220 pTB
->m_parent
->ProcessEvent(upevt
);
223 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
225 OSStatus err
= eventNotHandledErr
;
229 // note to self - a MenuRef *is* a MenuHandle
230 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
232 // When SetEventParameter is called it will decrement
233 // the reference count of the menu - we need to make
234 // sure it stays around in the wxMenu class here
237 // set the actual dock menu
238 err
= SetEventParameter(
239 inEvent
, kEventParamMenuRef
,
240 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
247 //-----------------------------------------------------------------------------
250 // Performs a top-to-bottom copy of the input menu and all of its
253 // This is mostly needed for 2.4 compatability. However wxPython and others
254 // still use this way of setting the taskbarmenu.
255 //-----------------------------------------------------------------------------
256 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
)
261 // NB: Here we have to perform a deep copy of the menu,
262 // copying each and every menu item from menu to m_pMenu.
263 // Other implementations use wxWindow::PopupMenu here,
264 // which idle execution until the user selects something,
265 // but since the Mac handles this internally, we can't -
266 // and have no way at all to idle it while the dock menu
267 // is being shown before menu goes out of scope (it may
268 // not be on the heap, and may expire right after this function
269 // is done - we need it to last until the carbon event is triggered -
270 // that's when the user right clicks).
272 // Also, since there is no equal (assignment) operator
273 // on either wxMenu or wxMenuItem, we have to do all the
274 // dirty work ourselves.
276 // perform a deep copy of the menu
277 wxMenuItemList
& theList
= menu
->GetMenuItems();
278 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
280 // create the main menu
281 wxMenu
*m_pMenu
= new wxMenu(menu
->GetTitle());
283 while (theNode
!= NULL
)
285 wxMenuItem
* theItem
= theNode
->GetData();
288 m_pMenu
, // parent menu
289 theItem
->GetId(), // id
290 theItem
->GetText(), // text label
291 theItem
->GetHelp(), // status bar help string
292 theItem
->GetKind(), // menu flags - checkable, separator, etc.
293 wxDeepCopyMenu(theItem
->GetSubMenu()) )); // submenu
295 theNode
= theNode
->GetNext();
301 //-----------------------------------------------------------------------------
302 // wxDockTaskBarIcon ctor
304 // Initializes the dock implementation of wxTaskBarIcon.
306 // Here we create some Mac-specific event handlers and UPPs.
307 //-----------------------------------------------------------------------------
308 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon
* parent
)
309 : wxTaskBarIconImpl(parent
),
310 m_eventHandlerRef(NULL
), m_pMenu(NULL
),
311 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
313 // register the events that will return the dock menu
314 EventTypeSpec tbEventList
[] =
316 { kEventClassCommand
, kEventProcessCommand
},
317 { kEventClassApplication
, kEventAppGetDockTileMenu
}
320 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
321 wxASSERT(m_eventupp
!= NULL
);
323 OSStatus err
= InstallApplicationEventHandler(
325 GetEventTypeCount(tbEventList
), tbEventList
,
326 this, &m_eventHandlerRef
);
330 //-----------------------------------------------------------------------------
331 // wxDockTaskBarIcon Destructor
333 // Cleans up mac events and restores the old icon to the dock
334 //-----------------------------------------------------------------------------
335 wxDockTaskBarIcon::~wxDockTaskBarIcon()
337 // clean up event handler and event UPP
338 RemoveEventHandler(m_eventHandlerRef
);
339 DisposeEventHandlerUPP(m_eventupp
);
341 // restore old icon and menu to the dock
345 //-----------------------------------------------------------------------------
346 // wxDockTaskBarIcon::DoCreatePopupMenu
348 // Helper function that handles a request from the dock event handler
349 // to get the menu for the dock
350 //-----------------------------------------------------------------------------
351 wxMenu
* wxDockTaskBarIcon::DoCreatePopupMenu()
353 // get the menu from the parent
354 wxMenu
* theNewMenu
= CreatePopupMenu();
360 m_pMenu
= theNewMenu
;
361 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
364 // the return here can be one of three things
365 // (in order of priority):
366 // 1) User passed a menu from CreatePopupMenu override
367 // 2) menu sent to and copied from PopupMenu
368 // 3) If neither (1) or (2), then NULL
373 //-----------------------------------------------------------------------------
374 // wxDockTaskBarIcon::IsIconInstalled
376 // Returns whether or not the dock is not using the default image
377 //-----------------------------------------------------------------------------
378 bool wxDockTaskBarIcon::IsIconInstalled() const
383 //-----------------------------------------------------------------------------
384 // wxDockTaskBarIcon::SetIcon
386 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
387 //-----------------------------------------------------------------------------
388 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
390 // convert the wxIcon into a wxBitmap so we can perform some
391 // wxBitmap operations with it
392 wxBitmap
bmp( icon
);
393 wxASSERT( bmp
.Ok() );
395 // get the CGImageRef for the wxBitmap:
396 // OSX builds only, but then the dock only exists in OSX
397 CGImageRef pImage
= (CGImageRef
) bmp
.CGImageCreate();
398 wxASSERT( pImage
!= NULL
);
400 // actually set the dock image
401 OSStatus err
= SetApplicationDockTileImage( pImage
);
404 // free the CGImage, now that it's referenced by the dock
406 CGImageRelease( pImage
);
408 bool success
= (err
== noErr
);
409 m_iconAdded
= success
;
414 //-----------------------------------------------------------------------------
415 // wxDockTaskBarIcon::RemoveIcon
417 // Restores the old image for the dock via RestoreApplicationDockTileImage
418 //-----------------------------------------------------------------------------
419 bool wxDockTaskBarIcon::RemoveIcon()
427 // restore old icon to the dock
428 OSStatus err
= RestoreApplicationDockTileImage();
431 // restore the old menu to the dock
432 SetApplicationDockTileMenu( m_theLastMenu
);
434 bool success
= (err
== noErr
);
435 m_iconAdded
= !success
;
440 //-----------------------------------------------------------------------------
441 // wxDockTaskBarIcon::PopupMenu
443 // 2.4 and wxPython method that "pops of the menu in the taskbar".
445 // In reality because of the way the dock menu works in carbon
446 // we just save the menu, and if the user didn't override CreatePopupMenu
447 // return the menu passed here, thus sort of getting the same effect.
448 //-----------------------------------------------------------------------------
449 bool wxDockTaskBarIcon::PopupMenu(wxMenu
*menu
)
451 wxASSERT(menu
!= NULL
);
456 // start copy of menu
457 m_pMenu
= wxDeepCopyMenu(menu
);
460 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
465 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
469 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
471 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
473 //-----------------------------------------------------------------------------
474 // wxTaskBarIcon Constructor
476 // Creates the backend
478 // Note that we only support DOCK currently as others require cocoa and
479 // also some require hacks and other such things. (MenuExtras are
480 // actually seperate programs that also require a special undocumented id
481 // hack and other such fun stuff).
482 //-----------------------------------------------------------------------------
483 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType
)
487 wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
489 m_impl
= new wxDockTaskBarIcon(this);
492 //-----------------------------------------------------------------------------
493 // wxTaskBarIcon Destructor
495 // Destroys the backend
496 //-----------------------------------------------------------------------------
497 wxTaskBarIcon::~wxTaskBarIcon()
502 //-----------------------------------------------------------------------------
503 // wxTaskBarIcon::SetIcon
504 // wxTaskBarIcon::RemoveIcon
505 // wxTaskBarIcon::PopupMenu
507 // Just calls the backend version of the said function.
508 //-----------------------------------------------------------------------------
509 bool wxTaskBarIcon::IsIconInstalled() const
510 { return m_impl
->IsIconInstalled(); }
512 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
513 { return m_impl
->SetIcon(icon
, tooltip
); }
515 bool wxTaskBarIcon::RemoveIcon()
516 { return m_impl
->RemoveIcon(); }
518 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
519 { return m_impl
->PopupMenu(menu
); }
521 #endif // wxHAS_TASK_BAR_ICON