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/taskbar.h"
19 #include "wx/dcmemory.h"
21 #include "wx/toplevel.h"
24 #include "wx/mac/private.h"
29 class wxTaskBarIconImpl
32 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
33 virtual ~wxTaskBarIconImpl();
35 virtual bool IsIconInstalled() const = 0;
36 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
) = 0;
37 virtual bool RemoveIcon() = 0;
38 virtual bool PopupMenu(wxMenu
*menu
) = 0;
40 wxMenu
* CreatePopupMenu()
41 { return m_parent
->CreatePopupMenu(); }
43 wxTaskBarIcon
*m_parent
;
44 class wxTaskBarIconWindow
*m_menuEventWindow
;
46 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl
)
49 //-----------------------------------------------------------------------------
51 // wxTaskBarIconWindow
53 // Event handler for menus
54 // NB: Since wxWindows in Mac HAVE to have parents we need this to be
55 // a top level window...
56 //-----------------------------------------------------------------------------
58 class wxTaskBarIconWindow
: public wxTopLevelWindow
61 wxTaskBarIconWindow(wxTaskBarIconImpl
*impl
)
62 : wxTopLevelWindow(NULL
, wxID_ANY
, wxEmptyString
), m_impl(impl
)
65 -1, wxEVT_COMMAND_MENU_SELECTED
,
66 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
) );
69 void OnMenuEvent(wxCommandEvent
& event
)
71 m_impl
->m_parent
->ProcessEvent(event
);
75 wxTaskBarIconImpl
*m_impl
;
78 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
81 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
82 virtual ~wxDockTaskBarIcon();
84 virtual bool IsIconInstalled() const;
85 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
);
86 virtual bool RemoveIcon();
87 virtual bool PopupMenu(wxMenu
*menu
);
89 wxMenu
* DoCreatePopupMenu();
91 EventHandlerRef m_eventHandlerRef
;
92 EventHandlerUPP m_eventupp
;
93 wxWindow
*m_eventWindow
;
95 MenuRef m_theLastMenu
;
99 // Forward declarations for utility functions for dock implementation
100 pascal OSStatus
wxDockEventHandler(
101 EventHandlerCallRef inHandlerCallRef
,
102 EventRef inEvent
, void* pData
);
103 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
);
106 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
110 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
112 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
113 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
117 wxTaskBarIconImpl::~wxTaskBarIconImpl()
119 delete m_menuEventWindow
;
122 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126 // OS X Dock implementation of wxTaskBarIcon using Carbon
127 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
129 //-----------------------------------------------------------------------------
130 // wxDockEventHandler
132 // This is the global Mac/Carbon event handler for the dock.
133 // We need this for two reasons:
134 // 1) To handle wxTaskBarIcon menu events (see below for why)
135 // 2) To handle events from the dock when it requests a menu
136 //-----------------------------------------------------------------------------
137 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
138 EventRef inEvent
, void *pData
)
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 // Handle wxTaskBar menu events (note that this is a global event handler
146 // so it will actually get called by all commands/menus)
147 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
))
149 // if we have no taskbar menu quickly pass it back to wxApp
150 if (pTB
->m_pMenu
== NULL
)
151 return eventNotHandledErr
;
153 // This is the real reason why we need this. Normally menus
154 // get handled in wxMacAppEventHandler
156 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
157 // EventRef event, void *data)
159 // However, in the case of a taskbar menu call
160 // command.menu.menuRef IS NULL!
161 // Which causes the wxApp handler just to skip it.
162 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
165 // get the HICommand from the event
167 err
= GetEventParameter(
168 inEvent
, kEventParamDirectObject
,
170 sizeof(HICommand
), NULL
, &command
);
173 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
175 // NOTE: menuRef is generally used here for submenus, as
176 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
177 // just the top level wxTaskBar menu
178 MenuItemIndex menuItemIndex
;
181 err
= GetIndMenuItemWithCommandID(
184 1, &menuRef
, &menuItemIndex
);
187 MenuCommand id
= command
.commandID
;
188 wxMenuItem
*item
= NULL
;
190 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
191 GetMenuItemRefCon( menuRef
, menuItemIndex
, (UInt32
*) &item
);
195 // Handle items that are checkable
196 // FIXME: Doesn't work (at least on 10.2)!
197 if (item
->IsCheckable())
198 item
->Check( !item
->IsChecked() );
200 // send the wxEvent to the wxMenu
201 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 );
203 // successfully handled the event
207 } //end if noErr on getting HICommand from event
209 // return whether we handled the event or not
213 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
214 // otherwise something is truly funky
215 wxASSERT(eventClass
== kEventClassApplication
&&
216 eventKind
== kEventAppGetDockTileMenu
);
218 // process the right click events
219 // NB: This may result in double or even triple-creation of the menus
220 // We need to do this for 2.4 compat, however
221 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
, NULL
);
222 pTB
->m_parent
->ProcessEvent(downevt
);
224 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
, NULL
);
225 pTB
->m_parent
->ProcessEvent(upevt
);
228 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
230 OSStatus err
= eventNotHandledErr
;
234 // note to self - a MenuRef *is* a MenuHandle
235 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
237 // When SetEventParameter is called it will decrement
238 // the reference count of the menu - we need to make
239 // sure it stays around in the wxMenu class here
242 // set the actual dock menu
243 err
= SetEventParameter(
244 inEvent
, kEventParamMenuRef
,
245 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
252 //-----------------------------------------------------------------------------
255 // Performs a top-to-bottom copy of the input menu and all of its
258 // This is mostly needed for 2.4 compatability. However wxPython and others
259 // still use this way of setting the taskbarmenu.
260 //-----------------------------------------------------------------------------
261 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
)
266 // NB: Here we have to perform a deep copy of the menu,
267 // copying each and every menu item from menu to m_pMenu.
268 // Other implementations use wxWindow::PopupMenu here,
269 // which idle execution until the user selects something,
270 // but since the Mac handles this internally, we can't -
271 // and have no way at all to idle it while the dock menu
272 // is being shown before menu goes out of scope (it may
273 // not be on the heap, and may expire right after this function
274 // is done - we need it to last until the carbon event is triggered -
275 // that's when the user right clicks).
277 // Also, since there is no equal (assignment) operator
278 // on either wxMenu or wxMenuItem, we have to do all the
279 // dirty work ourselves.
281 // perform a deep copy of the menu
282 wxMenuItemList
& theList
= menu
->GetMenuItems();
283 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
285 // create the main menu
286 wxMenu
*m_pMenu
= new wxMenu(menu
->GetTitle());
288 while (theNode
!= NULL
)
290 wxMenuItem
* theItem
= theNode
->GetData();
293 m_pMenu
, // parent menu
294 theItem
->GetId(), // id
295 theItem
->GetText(), // text label
296 theItem
->GetHelp(), // status bar help string
297 theItem
->GetKind(), // menu flags - checkable, separator, etc.
298 wxDeepCopyMenu(theItem
->GetSubMenu()) )); // submenu
300 theNode
= theNode
->GetNext();
306 //-----------------------------------------------------------------------------
307 // wxDockTaskBarIcon ctor
309 // Initializes the dock implementation of wxTaskBarIcon.
311 // Here we create some Mac-specific event handlers and UPPs.
312 //-----------------------------------------------------------------------------
313 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon
* parent
)
314 : wxTaskBarIconImpl(parent
),
315 m_eventHandlerRef(NULL
), m_pMenu(NULL
),
316 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
318 // register the events that will return the dock menu
319 EventTypeSpec tbEventList
[] =
321 { kEventClassCommand
, kEventProcessCommand
},
322 { kEventClassApplication
, kEventAppGetDockTileMenu
}
325 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
326 wxASSERT(m_eventupp
!= NULL
);
328 OSStatus err
= InstallApplicationEventHandler(
330 GetEventTypeCount(tbEventList
), tbEventList
,
331 this, &m_eventHandlerRef
);
335 //-----------------------------------------------------------------------------
336 // wxDockTaskBarIcon Destructor
338 // Cleans up mac events and restores the old icon to the dock
339 //-----------------------------------------------------------------------------
340 wxDockTaskBarIcon::~wxDockTaskBarIcon()
342 // clean up event handler and event UPP
343 RemoveEventHandler(m_eventHandlerRef
);
344 DisposeEventHandlerUPP(m_eventupp
);
346 // restore old icon and menu to the dock
350 //-----------------------------------------------------------------------------
351 // wxDockTaskBarIcon::DoCreatePopupMenu
353 // Helper function that handles a request from the dock event handler
354 // to get the menu for the dock
355 //-----------------------------------------------------------------------------
356 wxMenu
* wxDockTaskBarIcon::DoCreatePopupMenu()
358 // get the menu from the parent
359 wxMenu
* theNewMenu
= CreatePopupMenu();
365 m_pMenu
= theNewMenu
;
366 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
369 // the return here can be one of three things
370 // (in order of priority):
371 // 1) User passed a menu from CreatePopupMenu override
372 // 2) menu sent to and copied from PopupMenu
373 // 3) If neither (1) or (2), then NULL
378 //-----------------------------------------------------------------------------
379 // wxDockTaskBarIcon::IsIconInstalled
381 // Returns whether or not the dock is not using the default image
382 //-----------------------------------------------------------------------------
383 bool wxDockTaskBarIcon::IsIconInstalled() const
388 //-----------------------------------------------------------------------------
389 // wxDockTaskBarIcon::SetIcon
391 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
392 //-----------------------------------------------------------------------------
393 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
395 // convert the wxIcon into a wxBitmap so we can perform some
396 // wxBitmap operations with it
397 wxBitmap
bmp( icon
);
398 wxASSERT( bmp
.Ok() );
400 // get the CGImageRef for the wxBitmap:
401 // OSX builds only, but then the dock only exists in OSX
402 CGImageRef pImage
= (CGImageRef
) bmp
.CGImageCreate();
403 wxASSERT( pImage
!= NULL
);
405 // actually set the dock image
406 OSStatus err
= SetApplicationDockTileImage( pImage
);
409 // free the CGImage, now that it's referenced by the dock
411 CGImageRelease( pImage
);
413 bool success
= (err
== noErr
);
414 m_iconAdded
= success
;
419 //-----------------------------------------------------------------------------
420 // wxDockTaskBarIcon::RemoveIcon
422 // Restores the old image for the dock via RestoreApplicationDockTileImage
423 //-----------------------------------------------------------------------------
424 bool wxDockTaskBarIcon::RemoveIcon()
432 // restore old icon to the dock
433 OSStatus err
= RestoreApplicationDockTileImage();
436 // restore the old menu to the dock
437 SetApplicationDockTileMenu( m_theLastMenu
);
439 bool success
= (err
== noErr
);
440 m_iconAdded
= !success
;
445 //-----------------------------------------------------------------------------
446 // wxDockTaskBarIcon::PopupMenu
448 // 2.4 and wxPython method that "pops of the menu in the taskbar".
450 // In reality because of the way the dock menu works in carbon
451 // we just save the menu, and if the user didn't override CreatePopupMenu
452 // return the menu passed here, thus sort of getting the same effect.
453 //-----------------------------------------------------------------------------
454 bool wxDockTaskBarIcon::PopupMenu(wxMenu
*menu
)
456 wxASSERT(menu
!= NULL
);
461 // start copy of menu
462 m_pMenu
= wxDeepCopyMenu(menu
);
465 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
470 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
474 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
476 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
478 //-----------------------------------------------------------------------------
479 // wxTaskBarIcon Constructor
481 // Creates the backend
483 // Note that we only support DOCK currently as others require cocoa and
484 // also some require hacks and other such things. (MenuExtras are
485 // actually seperate programs that also require a special undocumented id
486 // hack and other such fun stuff).
487 //-----------------------------------------------------------------------------
488 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType
)
492 wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
494 m_impl
= new wxDockTaskBarIcon(this);
497 //-----------------------------------------------------------------------------
498 // wxTaskBarIcon Destructor
500 // Destroys the backend
501 //-----------------------------------------------------------------------------
502 wxTaskBarIcon::~wxTaskBarIcon()
507 //-----------------------------------------------------------------------------
508 // wxTaskBarIcon::SetIcon
509 // wxTaskBarIcon::RemoveIcon
510 // wxTaskBarIcon::PopupMenu
512 // Just calls the backend version of the said function.
513 //-----------------------------------------------------------------------------
514 bool wxTaskBarIcon::IsIconInstalled() const
515 { return m_impl
->IsIconInstalled(); }
517 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
518 { return m_impl
->SetIcon(icon
, tooltip
); }
520 bool wxTaskBarIcon::RemoveIcon()
521 { return m_impl
->RemoveIcon(); }
523 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
524 { return m_impl
->PopupMenu(menu
); }
526 #endif // wxHAS_TASK_BAR_ICON