1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/taskbar.cpp
3 // Purpose: wxTaskBarIcon
7 // Copyright: (c) 2004 Ryan Norton
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/taskbar.h"
18 #include "wx/dcmemory.h"
20 #include "wx/toplevel.h"
24 #include "wx/osx/private.h"
26 class wxTaskBarIconImpl
29 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
30 virtual ~wxTaskBarIconImpl();
32 virtual bool IsIconInstalled() const = 0;
33 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
) = 0;
34 virtual bool RemoveIcon() = 0;
35 virtual bool PopupMenu(wxMenu
*menu
) = 0;
37 wxMenu
* CreatePopupMenu()
38 { return m_parent
->CreatePopupMenu(); }
40 wxTaskBarIcon
*m_parent
;
41 class wxTaskBarIconWindow
*m_menuEventWindow
;
43 wxDECLARE_NO_COPY_CLASS(wxTaskBarIconImpl
);
46 //-----------------------------------------------------------------------------
48 // wxTaskBarIconWindow
50 // Event handler for menus
51 // NB: Since wxWindows in Mac HAVE to have parents we need this to be
52 // a top level window...
53 //-----------------------------------------------------------------------------
55 class wxTaskBarIconWindow
: public wxTopLevelWindow
58 wxTaskBarIconWindow(wxTaskBarIconImpl
*impl
)
59 : wxTopLevelWindow(NULL
, wxID_ANY
, wxEmptyString
), m_impl(impl
)
63 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
) );
66 void OnMenuEvent(wxCommandEvent
& event
)
68 m_impl
->m_parent
->ProcessEvent(event
);
72 wxTaskBarIconImpl
*m_impl
;
75 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
78 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
79 virtual ~wxDockTaskBarIcon();
81 virtual bool IsIconInstalled() const;
82 virtual bool SetIcon(const wxIcon
& icon
, const wxString
& tooltip
);
83 virtual bool RemoveIcon();
84 virtual bool PopupMenu(wxMenu
*menu
);
86 wxMenu
* DoCreatePopupMenu();
88 EventHandlerRef m_eventHandlerRef
;
89 EventHandlerUPP m_eventupp
;
90 wxWindow
*m_eventWindow
;
92 MenuRef m_theLastMenu
;
96 // Forward declarations for utility functions for dock implementation
97 pascal OSStatus
wxDockEventHandler(
98 EventHandlerCallRef inHandlerCallRef
,
99 EventRef inEvent
, void* pData
);
100 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
);
103 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
107 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
109 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
110 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
114 wxTaskBarIconImpl::~wxTaskBarIconImpl()
116 delete m_menuEventWindow
;
119 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123 // OS X Dock implementation of wxTaskBarIcon using Carbon
124 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126 //-----------------------------------------------------------------------------
127 // wxDockEventHandler
129 // This is the global Mac/Carbon event handler for the dock.
130 // We need this for two reasons:
131 // 1) To handle wxTaskBarIcon menu events (see below for why)
132 // 2) To handle events from the dock when it requests a menu
133 //-----------------------------------------------------------------------------
135 wxDockEventHandler(EventHandlerCallRef
WXUNUSED(inHandlerCallRef
),
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 OSStatus err
= eventNotHandledErr
;
146 // Handle wxTaskBar menu events (note that this is a global event handler
147 // so it will actually get called by all commands/menus)
148 if ((eventClass
== kEventClassCommand
) && (eventKind
== kEventCommandProcess
|| eventKind
== kEventCommandUpdateStatus
))
150 // if we have no taskbar menu quickly pass it back to wxApp
151 if (pTB
->m_pMenu
!= NULL
)
153 // This is the real reason why we need this. Normally menus
154 // get handled in wxMacAppEventHandler
155 // However, in the case of a taskbar menu call
156 // command.menu.menuRef IS NULL!
157 // Which causes the wxApp handler just to skip it.
159 // get the HICommand from the event
161 if (GetEventParameter(inEvent
, kEventParamDirectObject
,
162 typeHICommand
, NULL
,sizeof(HICommand
), NULL
, &command
) == noErr
)
164 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
166 // NOTE: menuRef is generally used here for submenus, as
167 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
168 // just the top level wxTaskBar menu
169 MenuItemIndex menuItemIndex
;
171 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
173 // the next command is only successful if it was a command from the taskbar menu
174 // otherwise we pass it on
175 if (GetIndMenuItemWithCommandID(taskbarMenuRef
,command
.commandID
,
176 1, &menuRef
, &menuItemIndex
) == noErr
)
178 wxMenu
* itemMenu
= wxFindMenuFromMacMenu( menuRef
) ;
179 int id
= wxMacCommandToId( command
.commandID
) ;
180 wxMenuItem
*item
= NULL
;
182 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
183 GetMenuItemRefCon( menuRef
, menuItemIndex
, (URefCon
*) &item
);
185 if (item
&& itemMenu
)
187 if ( eventKind
== kEventCommandProcess
)
189 if ( itemMenu
->HandleCommandProcess( item
) )
192 else if ( eventKind
== kEventCommandUpdateStatus
)
194 if ( itemMenu
->HandleCommandUpdateStatus( item
) )
200 } //end if noErr on getting HICommand from event
202 else if ((eventClass
== kEventClassApplication
) && (eventKind
== kEventAppGetDockTileMenu
))
204 // process the right click events
205 // NB: This may result in double or even triple-creation of the menus
206 // We need to do this for 2.4 compat, however
207 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
, NULL
);
208 pTB
->m_parent
->ProcessEvent(downevt
);
210 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
, NULL
);
211 pTB
->m_parent
->ProcessEvent(upevt
);
214 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
218 // note to self - a MenuRef *is* a MenuHandle
219 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
221 // When SetEventParameter is called it will decrement
222 // the reference count of the menu - we need to make
223 // sure it stays around in the wxMenu class here
226 // set the actual dock menu
227 err
= SetEventParameter(
228 inEvent
, kEventParamMenuRef
,
229 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
237 //-----------------------------------------------------------------------------
240 // Performs a top-to-bottom copy of the input menu and all of its
243 // This is mostly needed for 2.4 compatibility. However wxPython and others
244 // still use this way of setting the taskbarmenu.
245 //-----------------------------------------------------------------------------
246 wxMenu
* wxDeepCopyMenu( wxMenu
*menu
)
251 // NB: Here we have to perform a deep copy of the menu,
252 // copying each and every menu item from menu to m_pMenu.
253 // Other implementations use wxWindow::PopupMenu here,
254 // which idle execution until the user selects something,
255 // but since the Mac handles this internally, we can't -
256 // and have no way at all to idle it while the dock menu
257 // is being shown before menu goes out of scope (it may
258 // not be on the heap, and may expire right after this function
259 // is done - we need it to last until the carbon event is triggered -
260 // that's when the user right clicks).
262 // Also, since there is no equal (assignment) operator
263 // on either wxMenu or wxMenuItem, we have to do all the
264 // dirty work ourselves.
266 // perform a deep copy of the menu
267 wxMenuItemList
& theList
= menu
->GetMenuItems();
268 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
270 // create the main menu
271 wxMenu
*m_pMenu
= new wxMenu(menu
->GetTitle());
273 while (theNode
!= NULL
)
275 wxMenuItem
* theItem
= theNode
->GetData();
278 m_pMenu
, // parent menu
279 theItem
->GetId(), // id
280 theItem
->GetItemLabel(), // text label
281 theItem
->GetHelp(), // status bar help string
282 theItem
->GetKind(), // menu flags - checkable, separator, etc.
283 wxDeepCopyMenu(theItem
->GetSubMenu()) )); // submenu
285 theNode
= theNode
->GetNext();
291 //-----------------------------------------------------------------------------
292 // wxDockTaskBarIcon ctor
294 // Initializes the dock implementation of wxTaskBarIcon.
296 // Here we create some Mac-specific event handlers and UPPs.
297 //-----------------------------------------------------------------------------
298 wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon
* parent
)
299 : wxTaskBarIconImpl(parent
),
300 m_eventHandlerRef(NULL
), m_pMenu(NULL
),
301 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
303 // register the events that will return the dock menu
304 EventTypeSpec tbEventList
[] =
306 { kEventClassCommand
, kEventProcessCommand
},
307 { kEventClassCommand
, kEventCommandUpdateStatus
},
308 { kEventClassApplication
, kEventAppGetDockTileMenu
}
311 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
312 wxASSERT(m_eventupp
!= NULL
);
314 OSStatus err
= InstallApplicationEventHandler(
316 GetEventTypeCount(tbEventList
), tbEventList
,
317 this, &m_eventHandlerRef
);
321 //-----------------------------------------------------------------------------
322 // wxDockTaskBarIcon Destructor
324 // Cleans up mac events and restores the old icon to the dock
325 //-----------------------------------------------------------------------------
326 wxDockTaskBarIcon::~wxDockTaskBarIcon()
328 // clean up event handler and event UPP
329 RemoveEventHandler(m_eventHandlerRef
);
330 DisposeEventHandlerUPP(m_eventupp
);
332 // restore old icon and menu to the dock
336 //-----------------------------------------------------------------------------
337 // wxDockTaskBarIcon::DoCreatePopupMenu
339 // Helper function that handles a request from the dock event handler
340 // to get the menu for the dock
341 //-----------------------------------------------------------------------------
342 wxMenu
* wxDockTaskBarIcon::DoCreatePopupMenu()
344 // get the menu from the parent
345 wxMenu
* theNewMenu
= CreatePopupMenu();
350 m_pMenu
= theNewMenu
;
351 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
354 // the return here can be one of three things
355 // (in order of priority):
356 // 1) User passed a menu from CreatePopupMenu override
357 // 2) menu sent to and copied from PopupMenu
358 // 3) If neither (1) or (2), then NULL
363 //-----------------------------------------------------------------------------
364 // wxDockTaskBarIcon::IsIconInstalled
366 // Returns whether or not the dock is not using the default image
367 //-----------------------------------------------------------------------------
368 bool wxDockTaskBarIcon::IsIconInstalled() const
373 //-----------------------------------------------------------------------------
374 // wxDockTaskBarIcon::SetIcon
376 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
377 //-----------------------------------------------------------------------------
378 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& WXUNUSED(tooltip
))
380 // convert the wxIcon into a wxBitmap so we can perform some
381 // wxBitmap operations with it
382 wxBitmap
bmp( icon
);
383 wxASSERT( bmp
.IsOk() );
385 // get the CGImageRef for the wxBitmap:
386 // OSX builds only, but then the dock only exists in OSX
387 CGImageRef pImage
= (CGImageRef
) bmp
.CreateCGImage();
388 wxASSERT( pImage
!= NULL
);
390 // actually set the dock image
391 OSStatus err
= SetApplicationDockTileImage( pImage
);
394 // free the CGImage, now that it's referenced by the dock
396 CGImageRelease( pImage
);
398 bool success
= (err
== noErr
);
399 m_iconAdded
= success
;
404 //-----------------------------------------------------------------------------
405 // wxDockTaskBarIcon::RemoveIcon
407 // Restores the old image for the dock via RestoreApplicationDockTileImage
408 //-----------------------------------------------------------------------------
409 bool wxDockTaskBarIcon::RemoveIcon()
413 // restore old icon to the dock
414 OSStatus err
= RestoreApplicationDockTileImage();
417 // restore the old menu to the dock
418 SetApplicationDockTileMenu( m_theLastMenu
);
420 bool success
= (err
== noErr
);
421 m_iconAdded
= !success
;
426 //-----------------------------------------------------------------------------
427 // wxDockTaskBarIcon::PopupMenu
429 // 2.4 and wxPython method that "pops of the menu in the taskbar".
431 // In reality because of the way the dock menu works in carbon
432 // we just save the menu, and if the user didn't override CreatePopupMenu
433 // return the menu passed here, thus sort of getting the same effect.
434 //-----------------------------------------------------------------------------
435 bool wxDockTaskBarIcon::PopupMenu(wxMenu
*menu
)
437 wxASSERT(menu
!= NULL
);
441 // start copy of menu
442 m_pMenu
= wxDeepCopyMenu(menu
);
445 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
450 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
454 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
456 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
458 //-----------------------------------------------------------------------------
459 // wxTaskBarIcon Constructor
461 // Creates the backend
463 // Note that we only support DOCK currently as others require cocoa and
464 // also some require hacks and other such things. (MenuExtras are
465 // actually separate programs that also require a special undocumented id
466 // hack and other such fun stuff).
467 //-----------------------------------------------------------------------------
468 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType
WXUNUSED_UNLESS_DEBUG(nType
))
472 wxT("Only the wxTBI_DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
474 m_impl
= new wxDockTaskBarIcon(this);
477 //-----------------------------------------------------------------------------
478 // wxTaskBarIcon Destructor
480 // Destroys the backend
481 //-----------------------------------------------------------------------------
482 wxTaskBarIcon::~wxTaskBarIcon()
487 //-----------------------------------------------------------------------------
488 // wxTaskBarIcon::SetIcon
489 // wxTaskBarIcon::RemoveIcon
490 // wxTaskBarIcon::PopupMenu
492 // Just calls the backend version of the said function.
493 //-----------------------------------------------------------------------------
494 bool wxTaskBarIcon::IsIconInstalled() const
495 { return m_impl
->IsIconInstalled(); }
497 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
498 { return m_impl
->SetIcon(icon
, tooltip
); }
500 bool wxTaskBarIcon::RemoveIcon()
501 { return m_impl
->RemoveIcon(); }
503 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
504 { return m_impl
->PopupMenu(menu
); }
506 #endif // wxUSE_TASKBARICON