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"
22 #include "wx/mac/private.h"
28 class wxTaskBarIconImpl
31 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
32 virtual ~wxTaskBarIconImpl();
34 virtual bool IsIconInstalled() const = 0;
35 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
) = 0;
36 virtual bool RemoveIcon() = 0;
37 virtual bool PopupMenu(wxMenu
*menu
) = 0;
39 wxMenu
* CreatePopupMenu()
40 { return m_parent
->CreatePopupMenu(); }
42 wxTaskBarIcon
*m_parent
;
43 class wxTaskBarIconWindow
*m_menuEventWindow
;
45 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl
)
48 //-----------------------------------------------------------------------------
50 // wxTaskBarIconWindow
52 // Event handler for menus
53 // NB: Since wxWindows in Mac HAVE to have parents we need this to be
54 // a top level window...
55 //-----------------------------------------------------------------------------
57 class wxTaskBarIconWindow
: public wxTopLevelWindow
60 wxTaskBarIconWindow(wxTaskBarIconImpl
*impl
)
61 : wxTopLevelWindow(NULL
, wxID_ANY
, wxEmptyString
), m_impl(impl
)
64 -1, wxEVT_COMMAND_MENU_SELECTED
,
65 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
) );
68 void OnMenuEvent(wxCommandEvent
& event
)
70 m_impl
->m_parent
->ProcessEvent(event
);
74 wxTaskBarIconImpl
*m_impl
;
77 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
80 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
81 virtual ~wxDockTaskBarIcon();
83 virtual bool IsIconInstalled() const;
84 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
);
85 virtual bool RemoveIcon();
86 virtual bool PopupMenu(wxMenu
*menu
);
88 wxMenu
* DoCreatePopupMenu();
90 EventHandlerRef m_eventHandlerRef
;
91 EventHandlerUPP m_eventupp
;
92 wxWindow
*m_eventWindow
;
94 MenuRef m_theLastMenu
;
98 // Forward declarations for utility functions for dock implementation
99 pascal OSStatus
wxDockEventHandler(
100 EventHandlerCallRef inHandlerCallRef
,
101 EventRef inEvent
, void* pData
);
102 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
);
105 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
109 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
111 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
112 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
116 wxTaskBarIconImpl::~wxTaskBarIconImpl()
118 delete m_menuEventWindow
;
121 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125 // OS X Dock implementation of wxTaskBarIcon using Carbon
126 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
128 //-----------------------------------------------------------------------------
129 // wxDockEventHandler
131 // This is the global Mac/Carbon event handler for the dock.
132 // We need this for two reasons:
133 // 1) To handle wxTaskBarIcon menu events (see below for why)
134 // 2) To handle events from the dock when it requests a menu
135 //-----------------------------------------------------------------------------
136 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
137 EventRef inEvent
, void *pData
)
139 // Get the parameters we want from the event
140 wxDockTaskBarIcon
* pTB
= (wxDockTaskBarIcon
*) pData
;
141 const UInt32 eventClass
= GetEventClass(inEvent
);
142 const UInt32 eventKind
= GetEventKind(inEvent
);
144 // Handle wxTaskBar menu events (note that this is a global event handler
145 // so it will actually get called by all commands/menus)
146 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
))
148 // if we have no taskbar menu quickly pass it back to wxApp
149 if (pTB
->m_pMenu
== NULL
)
150 return eventNotHandledErr
;
152 // This is the real reason why we need this. Normally menus
153 // get handled in wxMacAppEventHandler
155 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
156 // EventRef event, void *data)
158 // However, in the case of a taskbar menu call
159 // command.menu.menuRef IS NULL!
160 // Which causes the wxApp handler just to skip it.
161 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
164 // get the HICommand from the event
166 err
= GetEventParameter(
167 inEvent
, kEventParamDirectObject
,
169 sizeof(HICommand
), NULL
, &command
);
172 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
174 // NOTE: menuRef is generally used here for submenus, as
175 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
176 // just the top level wxTaskBar menu
177 MenuItemIndex menuItemIndex
;
180 err
= GetIndMenuItemWithCommandID(
183 1, &menuRef
, &menuItemIndex
);
186 MenuCommand id
= command
.commandID
;
187 wxMenuItem
*item
= NULL
;
189 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
190 GetMenuItemRefCon( menuRef
, menuItemIndex
, (UInt32
*) &item
);
194 // Handle items that are checkable
195 // FIXME: Doesn't work (at least on 10.2)!
196 if (item
->IsCheckable())
197 item
->Check( !item
->IsChecked() );
199 // send the wxEvent to the wxMenu
200 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 );
202 // successfully handled the event
206 } //end if noErr on getting HICommand from event
208 // return whether we handled the event or not
212 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
213 // otherwise something is truly funky
214 wxASSERT(eventClass
== kEventClassApplication
&&
215 eventKind
== kEventAppGetDockTileMenu
);
217 // process the right click events
218 // NB: This may result in double or even triple-creation of the menus
219 // We need to do this for 2.4 compat, however
220 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
, NULL
);
221 pTB
->m_parent
->ProcessEvent(downevt
);
223 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
, NULL
);
224 pTB
->m_parent
->ProcessEvent(upevt
);
227 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
229 OSStatus err
= eventNotHandledErr
;
233 // note to self - a MenuRef *is* a MenuHandle
234 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
236 // When SetEventParameter is called it will decrement
237 // the reference count of the menu - we need to make
238 // sure it stays around in the wxMenu class here
241 // set the actual dock menu
242 err
= SetEventParameter(
243 inEvent
, kEventParamMenuRef
,
244 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
251 //-----------------------------------------------------------------------------
254 // Performs a top-to-bottom copy of the input menu and all of its
257 // This is mostly needed for 2.4 compatability. However wxPython and others
258 // still use this way of setting the taskbarmenu.
259 //-----------------------------------------------------------------------------
260 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
)
265 // NB: Here we have to perform a deep copy of the menu,
266 // copying each and every menu item from menu to m_pMenu.
267 // Other implementations use wxWindow::PopupMenu here,
268 // which idle execution until the user selects something,
269 // but since the Mac handles this internally, we can't -
270 // and have no way at all to idle it while the dock menu
271 // is being shown before menu goes out of scope (it may
272 // not be on the heap, and may expire right after this function
273 // is done - we need it to last until the carbon event is triggered -
274 // that's when the user right clicks).
276 // Also, since there is no equal (assignment) operator
277 // on either wxMenu or wxMenuItem, we have to do all the
278 // dirty work ourselves.
280 // perform a deep copy of the menu
281 wxMenuItemList
& theList
= menu
->GetMenuItems();
282 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
284 // create the main menu
285 wxMenu
*m_pMenu
= new wxMenu(menu
->GetTitle());
287 while (theNode
!= NULL
)
289 wxMenuItem
* theItem
= theNode
->GetData();
292 m_pMenu
, // parent menu
293 theItem
->GetId(), // id
294 theItem
->GetText(), // text label
295 theItem
->GetHelp(), // status bar help string
296 theItem
->GetKind(), // menu flags - checkable, separator, etc.
297 wxDeepCopyMenu(theItem
->GetSubMenu()) )); // submenu
299 theNode
= theNode
->GetNext();
305 //-----------------------------------------------------------------------------
306 // wxDockTaskBarIcon ctor
308 // Initializes the dock implementation of wxTaskBarIcon.
310 // Here we create some Mac-specific event handlers and UPPs.
311 //-----------------------------------------------------------------------------
312 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon
* parent
)
313 : wxTaskBarIconImpl(parent
),
314 m_eventHandlerRef(NULL
), m_pMenu(NULL
),
315 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
317 // register the events that will return the dock menu
318 EventTypeSpec tbEventList
[] =
320 { kEventClassCommand
, kEventProcessCommand
},
321 { kEventClassApplication
, kEventAppGetDockTileMenu
}
324 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
325 wxASSERT(m_eventupp
!= NULL
);
327 OSStatus err
= InstallApplicationEventHandler(
329 GetEventTypeCount(tbEventList
), tbEventList
,
330 this, &m_eventHandlerRef
);
334 //-----------------------------------------------------------------------------
335 // wxDockTaskBarIcon Destructor
337 // Cleans up mac events and restores the old icon to the dock
338 //-----------------------------------------------------------------------------
339 wxDockTaskBarIcon::~wxDockTaskBarIcon()
341 // clean up event handler and event UPP
342 RemoveEventHandler(m_eventHandlerRef
);
343 DisposeEventHandlerUPP(m_eventupp
);
345 // restore old icon and menu to the dock
349 //-----------------------------------------------------------------------------
350 // wxDockTaskBarIcon::DoCreatePopupMenu
352 // Helper function that handles a request from the dock event handler
353 // to get the menu for the dock
354 //-----------------------------------------------------------------------------
355 wxMenu
* wxDockTaskBarIcon::DoCreatePopupMenu()
357 // get the menu from the parent
358 wxMenu
* theNewMenu
= CreatePopupMenu();
364 m_pMenu
= theNewMenu
;
365 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
368 // the return here can be one of three things
369 // (in order of priority):
370 // 1) User passed a menu from CreatePopupMenu override
371 // 2) menu sent to and copied from PopupMenu
372 // 3) If neither (1) or (2), then NULL
377 //-----------------------------------------------------------------------------
378 // wxDockTaskBarIcon::IsIconInstalled
380 // Returns whether or not the dock is not using the default image
381 //-----------------------------------------------------------------------------
382 bool wxDockTaskBarIcon::IsIconInstalled() const
387 //-----------------------------------------------------------------------------
388 // wxDockTaskBarIcon::SetIcon
390 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
391 //-----------------------------------------------------------------------------
392 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
394 // convert the wxIcon into a wxBitmap so we can perform some
395 // wxBitmap operations with it
396 wxBitmap
bmp( icon
);
397 wxASSERT( bmp
.Ok() );
399 // get the CGImageRef for the wxBitmap:
400 // OSX builds only, but then the dock only exists in OSX
401 CGImageRef pImage
= (CGImageRef
) bmp
.CGImageCreate();
402 wxASSERT( pImage
!= NULL
);
404 // actually set the dock image
405 OSStatus err
= SetApplicationDockTileImage( pImage
);
408 // free the CGImage, now that it's referenced by the dock
410 CGImageRelease( pImage
);
412 bool success
= (err
== noErr
);
413 m_iconAdded
= success
;
418 //-----------------------------------------------------------------------------
419 // wxDockTaskBarIcon::RemoveIcon
421 // Restores the old image for the dock via RestoreApplicationDockTileImage
422 //-----------------------------------------------------------------------------
423 bool wxDockTaskBarIcon::RemoveIcon()
431 // restore old icon to the dock
432 OSStatus err
= RestoreApplicationDockTileImage();
435 // restore the old menu to the dock
436 SetApplicationDockTileMenu( m_theLastMenu
);
438 bool success
= (err
== noErr
);
439 m_iconAdded
= !success
;
444 //-----------------------------------------------------------------------------
445 // wxDockTaskBarIcon::PopupMenu
447 // 2.4 and wxPython method that "pops of the menu in the taskbar".
449 // In reality because of the way the dock menu works in carbon
450 // we just save the menu, and if the user didn't override CreatePopupMenu
451 // return the menu passed here, thus sort of getting the same effect.
452 //-----------------------------------------------------------------------------
453 bool wxDockTaskBarIcon::PopupMenu(wxMenu
*menu
)
455 wxASSERT(menu
!= NULL
);
460 // start copy of menu
461 m_pMenu
= wxDeepCopyMenu(menu
);
464 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
469 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
473 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
475 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
477 //-----------------------------------------------------------------------------
478 // wxTaskBarIcon Constructor
480 // Creates the backend
482 // Note that we only support DOCK currently as others require cocoa and
483 // also some require hacks and other such things. (MenuExtras are
484 // actually seperate programs that also require a special undocumented id
485 // hack and other such fun stuff).
486 //-----------------------------------------------------------------------------
487 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType
)
491 wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
493 m_impl
= new wxDockTaskBarIcon(this);
496 //-----------------------------------------------------------------------------
497 // wxTaskBarIcon Destructor
499 // Destroys the backend
500 //-----------------------------------------------------------------------------
501 wxTaskBarIcon::~wxTaskBarIcon()
506 //-----------------------------------------------------------------------------
507 // wxTaskBarIcon::SetIcon
508 // wxTaskBarIcon::RemoveIcon
509 // wxTaskBarIcon::PopupMenu
511 // Just calls the backend version of the said function.
512 //-----------------------------------------------------------------------------
513 bool wxTaskBarIcon::IsIconInstalled() const
514 { return m_impl
->IsIconInstalled(); }
516 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
517 { return m_impl
->SetIcon(icon
, tooltip
); }
519 bool wxTaskBarIcon::RemoveIcon()
520 { return m_impl
->RemoveIcon(); }
522 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
523 { return m_impl
->PopupMenu(menu
); }
525 #endif // wxHAS_TASK_BAR_ICON