]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/taskbar.cpp
use wx-style header and commets; fix indentation to be 4 spaces; move Doxygen comment...
[wxWidgets.git] / src / mac / carbon / taskbar.cpp
CommitLineData
d806d30a 1///////////////////////////////////////////////////////////////////////////////
51c4d2a5 2// Name: src/mac/carbon/taskbar.cpp
f38924e8 3// Purpose: wxTaskBarIcon
607667fd
RN
4// Author: Ryan Norton
5// Modified by:
6// Created: 09/25/2004
7// RCS-ID: $Id$
8// Copyright: (c) 2004 Ryan Norton
9// Licence: wxWindows licence
d806d30a
DS
10///////////////////////////////////////////////////////////////////////////////
11
607667fd
RN
12#include "wx/wxprec.h"
13
4f167b46 14#if wxUSE_TASKBARICON
607667fd 15
f38924e8
WS
16#include "wx/taskbar.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/dcmemory.h"
3b3dc801 20 #include "wx/menu.h"
aba610e7 21 #include "wx/toplevel.h"
923d28da 22 #include "wx/icon.h"
f38924e8
WS
23#endif
24
607667fd
RN
25#include "wx/mac/private.h"
26
fe224552 27class wxTaskBarIconImpl
d806d30a
DS
28{
29public:
30 wxTaskBarIconImpl(wxTaskBarIcon* parent);
31 virtual ~wxTaskBarIconImpl();
32
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;
f9f9c8fc 37
51c4d2a5 38 wxMenu * CreatePopupMenu()
d806d30a 39 { return m_parent->CreatePopupMenu(); }
f9f9c8fc 40
51c4d2a5
DS
41 wxTaskBarIcon *m_parent;
42 class wxTaskBarIconWindow *m_menuEventWindow;
f9f9c8fc
DS
43
44 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl)
d806d30a
DS
45};
46
47//-----------------------------------------------------------------------------
48//
49// wxTaskBarIconWindow
50//
51// Event handler for menus
51c4d2a5 52// NB: Since wxWindows in Mac HAVE to have parents we need this to be
d806d30a
DS
53// a top level window...
54//-----------------------------------------------------------------------------
55
56class wxTaskBarIconWindow : public wxTopLevelWindow
57{
58public:
51c4d2a5 59 wxTaskBarIconWindow(wxTaskBarIconImpl *impl)
f38924e8 60 : wxTopLevelWindow(NULL, wxID_ANY, wxEmptyString), m_impl(impl)
d806d30a 61 {
51c4d2a5
DS
62 Connect(
63 -1, wxEVT_COMMAND_MENU_SELECTED,
64 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent) );
d806d30a
DS
65 }
66
fe224552 67 void OnMenuEvent(wxCommandEvent& event)
d806d30a
DS
68 {
69 m_impl->m_parent->ProcessEvent(event);
70 }
71
72private:
51c4d2a5 73 wxTaskBarIconImpl *m_impl;
d806d30a
DS
74};
75
d806d30a
DS
76class wxDockTaskBarIcon : public wxTaskBarIconImpl
77{
78public:
79 wxDockTaskBarIcon(wxTaskBarIcon* parent);
80 virtual ~wxDockTaskBarIcon();
30efba0a 81
d806d30a
DS
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);
86
87 wxMenu* DoCreatePopupMenu();
88
89 EventHandlerRef m_eventHandlerRef;
90 EventHandlerUPP m_eventupp;
51c4d2a5
DS
91 wxWindow *m_eventWindow;
92 wxMenu *m_pMenu;
d806d30a
DS
93 MenuRef m_theLastMenu;
94 bool m_iconAdded;
d806d30a
DS
95};
96
97// Forward declarations for utility functions for dock implementation
51c4d2a5
DS
98pascal OSStatus wxDockEventHandler(
99 EventHandlerCallRef inHandlerCallRef,
100 EventRef inEvent, void* pData );
101wxMenu * wxDeepCopyMenu( wxMenu *menu );
fe224552 102
d806d30a 103
d806d30a
DS
104//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105//
106// wxTaskBarIconImpl
107//
108//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
109
fe224552 110wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* parent)
d806d30a 111 : m_parent(parent), m_menuEventWindow(new wxTaskBarIconWindow(this))
607667fd 112{
d806d30a
DS
113}
114
d806d30a
DS
115wxTaskBarIconImpl::~wxTaskBarIconImpl()
116{
117 delete m_menuEventWindow;
118}
119
120//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121//
122// wxDockTaskBarIcon
123//
51c4d2a5 124// OS X Dock implementation of wxTaskBarIcon using Carbon
d806d30a 125//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
607667fd 126
51c4d2a5 127//-----------------------------------------------------------------------------
d806d30a
DS
128// wxDockEventHandler
129//
51c4d2a5 130// This is the global Mac/Carbon event handler for the dock.
d806d30a
DS
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
51c4d2a5 134//-----------------------------------------------------------------------------
89954433
VZ
135pascal OSStatus
136wxDockEventHandler(EventHandlerCallRef WXUNUSED(inHandlerCallRef),
137 EventRef inEvent,
138 void *pData)
d806d30a
DS
139{
140 // Get the parameters we want from the event
141 wxDockTaskBarIcon* pTB = (wxDockTaskBarIcon*) pData;
607667fd
RN
142 const UInt32 eventClass = GetEventClass(inEvent);
143 const UInt32 eventKind = GetEventKind(inEvent);
6239ee05
SC
144
145 OSStatus err = eventNotHandledErr;
30efba0a 146
d806d30a
DS
147 // Handle wxTaskBar menu events (note that this is a global event handler
148 // so it will actually get called by all commands/menus)
6239ee05 149 if ((eventClass == kEventClassCommand) && (eventKind == kEventCommandProcess || eventKind == kEventCommandUpdateStatus ))
607667fd 150 {
d806d30a 151 // if we have no taskbar menu quickly pass it back to wxApp
6239ee05 152 if (pTB->m_pMenu != NULL)
fe224552 153 {
6239ee05
SC
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.
159
160 // get the HICommand from the event
161 HICommand command;
162 if (GetEventParameter(inEvent, kEventParamDirectObject,
163 typeHICommand, NULL,sizeof(HICommand), NULL, &command ) == noErr)
d806d30a 164 {
6239ee05
SC
165 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
166 //
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;
171 MenuRef menuRef;
172 MenuRef taskbarMenuRef = MAC_WXHMENU(pTB->m_pMenu->GetHMenu());
173
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)
d806d30a 178 {
6239ee05
SC
179 wxMenu* itemMenu = wxFindMenuFromMacMenu( menuRef ) ;
180 int id = wxMacCommandToId( command.commandID ) ;
181 wxMenuItem *item = NULL;
182
183 if (id != 0) // get the wxMenuItem reference from the MenuRef
184 GetMenuItemRefCon( menuRef, menuItemIndex, (URefCon*) &item );
185
186 if (item && itemMenu )
187 {
188 if ( eventKind == kEventCommandProcess )
189 err = itemMenu->MacHandleCommandProcess( item, id );
190 else if ( eventKind == kEventCommandUpdateStatus )
191 err = itemMenu->MacHandleCommandUpdateStatus( item, id );
192 }
d806d30a 193 }
af7e08a4 194 }
fe224552 195 } //end if noErr on getting HICommand from event
607667fd 196 }
6239ee05
SC
197 else if ((eventClass == kEventClassApplication) && (eventKind == kEventAppGetDockTileMenu ))
198 {
199 // process the right click events
200 // NB: This may result in double or even triple-creation of the menus
201 // We need to do this for 2.4 compat, however
202 wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN, NULL);
203 pTB->m_parent->ProcessEvent(downevt);
d806d30a 204
6239ee05
SC
205 wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP, NULL);
206 pTB->m_parent->ProcessEvent(upevt);
a45beea1 207
6239ee05
SC
208 // create popup menu
209 wxMenu* menu = pTB->DoCreatePopupMenu();
30efba0a 210
6239ee05
SC
211 if (menu != NULL)
212 {
213 // note to self - a MenuRef *is* a MenuHandle
214 MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu());
215
216 // When SetEventParameter is called it will decrement
217 // the reference count of the menu - we need to make
218 // sure it stays around in the wxMenu class here
219 CFRetain(hMenu);
220
221 // set the actual dock menu
222 err = SetEventParameter(
223 inEvent, kEventParamMenuRef,
224 typeMenuRef, sizeof(MenuRef), &hMenu );
225 verify_noerr( err );
226 }
af7e08a4 227 }
30efba0a
DS
228
229 return err;
607667fd
RN
230}
231
51c4d2a5 232//-----------------------------------------------------------------------------
d806d30a
DS
233// wxDeepCopyMenu
234//
235// Performs a top-to-bottom copy of the input menu and all of its
236// submenus.
237//
238// This is mostly needed for 2.4 compatability. However wxPython and others
239// still use this way of setting the taskbarmenu.
51c4d2a5
DS
240//-----------------------------------------------------------------------------
241wxMenu * wxDeepCopyMenu( wxMenu *menu )
d806d30a 242{
51c4d2a5 243 if (menu == NULL)
d806d30a
DS
244 return NULL;
245
d806d30a
DS
246 // NB: Here we have to perform a deep copy of the menu,
247 // copying each and every menu item from menu to m_pMenu.
fe224552 248 // Other implementations use wxWindow::PopupMenu here,
d806d30a 249 // which idle execution until the user selects something,
51c4d2a5 250 // but since the Mac handles this internally, we can't -
d806d30a
DS
251 // and have no way at all to idle it while the dock menu
252 // is being shown before menu goes out of scope (it may
253 // not be on the heap, and may expire right after this function
fe224552 254 // is done - we need it to last until the carbon event is triggered -
d806d30a
DS
255 // that's when the user right clicks).
256 //
fe224552 257 // Also, since there is no equal (assignment) operator
d806d30a
DS
258 // on either wxMenu or wxMenuItem, we have to do all the
259 // dirty work ourselves.
d806d30a
DS
260
261 // perform a deep copy of the menu
262 wxMenuItemList& theList = menu->GetMenuItems();
263 wxMenuItemList::compatibility_iterator theNode = theList.GetFirst();
30efba0a 264
d806d30a 265 // create the main menu
51c4d2a5 266 wxMenu *m_pMenu = new wxMenu(menu->GetTitle());
d806d30a
DS
267
268 while (theNode != NULL)
269 {
270 wxMenuItem* theItem = theNode->GetData();
30efba0a
DS
271 m_pMenu->Append(
272 new wxMenuItem(
273 m_pMenu, // parent menu
274 theItem->GetId(), // id
c2edb907 275 theItem->GetItemLabel(), // text label
30efba0a
DS
276 theItem->GetHelp(), // status bar help string
277 theItem->GetKind(), // menu flags - checkable, separator, etc.
51c4d2a5
DS
278 wxDeepCopyMenu(theItem->GetSubMenu()) )); // submenu
279
d806d30a
DS
280 theNode = theNode->GetNext();
281 }
30efba0a 282
d806d30a
DS
283 return m_pMenu;
284}
607667fd 285
d806d30a 286//-----------------------------------------------------------------------------
30efba0a 287// wxDockTaskBarIcon ctor
d806d30a
DS
288//
289// Initializes the dock implementation of wxTaskBarIcon.
290//
51c4d2a5 291// Here we create some Mac-specific event handlers and UPPs.
d806d30a 292//-----------------------------------------------------------------------------
fe224552 293wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon* parent)
d806d30a 294 : wxTaskBarIconImpl(parent),
fe224552
VZ
295 m_eventHandlerRef(NULL), m_pMenu(NULL),
296 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
607667fd 297{
d806d30a 298 // register the events that will return the dock menu
30efba0a
DS
299 EventTypeSpec tbEventList[] =
300 {
301 { kEventClassCommand, kEventProcessCommand },
6239ee05 302 { kEventClassCommand, kEventCommandUpdateStatus },
30efba0a
DS
303 { kEventClassApplication, kEventAppGetDockTileMenu }
304 };
305
d806d30a
DS
306 m_eventupp = NewEventHandlerUPP(wxDockEventHandler);
307 wxASSERT(m_eventupp != NULL);
30efba0a 308
51c4d2a5 309 OSStatus err = InstallApplicationEventHandler(
d806d30a 310 m_eventupp,
fe224552 311 GetEventTypeCount(tbEventList), tbEventList,
d806d30a 312 this, &m_eventHandlerRef);
51c4d2a5 313 verify_noerr( err );
607667fd 314}
cc3388ae 315
d806d30a
DS
316//-----------------------------------------------------------------------------
317// wxDockTaskBarIcon Destructor
318//
319// Cleans up mac events and restores the old icon to the dock
320//-----------------------------------------------------------------------------
321wxDockTaskBarIcon::~wxDockTaskBarIcon()
607667fd 322{
d806d30a
DS
323 // clean up event handler and event UPP
324 RemoveEventHandler(m_eventHandlerRef);
325 DisposeEventHandlerUPP(m_eventupp);
af7e08a4 326
d806d30a 327 // restore old icon and menu to the dock
fe224552 328 RemoveIcon();
607667fd
RN
329}
330
d806d30a
DS
331//-----------------------------------------------------------------------------
332// wxDockTaskBarIcon::DoCreatePopupMenu
333//
fe224552 334// Helper function that handles a request from the dock event handler
d806d30a
DS
335// to get the menu for the dock
336//-----------------------------------------------------------------------------
51c4d2a5 337wxMenu * wxDockTaskBarIcon::DoCreatePopupMenu()
607667fd 338{
d806d30a 339 // get the menu from the parent
af7e08a4 340 wxMenu* theNewMenu = CreatePopupMenu();
30efba0a 341
af7e08a4
RN
342 if (theNewMenu)
343 {
d806d30a
DS
344 if (m_pMenu)
345 delete m_pMenu;
af7e08a4 346 m_pMenu = theNewMenu;
d806d30a 347 m_pMenu->SetInvokingWindow(m_menuEventWindow);
af7e08a4 348 }
30efba0a 349
fe224552 350 // the return here can be one of three things
d806d30a
DS
351 // (in order of priority):
352 // 1) User passed a menu from CreatePopupMenu override
353 // 2) menu sent to and copied from PopupMenu
354 // 3) If neither (1) or (2), then NULL
355 //
607667fd
RN
356 return m_pMenu;
357}
358
d806d30a
DS
359//-----------------------------------------------------------------------------
360// wxDockTaskBarIcon::IsIconInstalled
361//
362// Returns whether or not the dock is not using the default image
363//-----------------------------------------------------------------------------
364bool wxDockTaskBarIcon::IsIconInstalled() const
fe224552
VZ
365{
366 return m_iconAdded;
d806d30a
DS
367}
368
369//-----------------------------------------------------------------------------
370// wxDockTaskBarIcon::SetIcon
371//
372// Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
373//-----------------------------------------------------------------------------
89954433 374bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& WXUNUSED(tooltip))
607667fd 375{
30efba0a
DS
376 // convert the wxIcon into a wxBitmap so we can perform some
377 // wxBitmap operations with it
51c4d2a5 378 wxBitmap bmp( icon );
f9f9c8fc 379 wxASSERT( bmp.Ok() );
20b69855 380
30efba0a
DS
381 // get the CGImageRef for the wxBitmap:
382 // OSX builds only, but then the dock only exists in OSX
968c951f 383 CGImageRef pImage = (CGImageRef) bmp.CreateCGImage();
30efba0a 384 wxASSERT( pImage != NULL );
d806d30a 385
30efba0a
DS
386 // actually set the dock image
387 OSStatus err = SetApplicationDockTileImage( pImage );
51c4d2a5 388 verify_noerr( err );
30efba0a
DS
389
390 // free the CGImage, now that it's referenced by the dock
739e35e4 391 if (pImage != NULL)
30efba0a
DS
392 CGImageRelease( pImage );
393
f9f9c8fc
DS
394 bool success = (err == noErr);
395 m_iconAdded = success;
396
397 return success;
607667fd 398}
30efba0a 399
d806d30a
DS
400//-----------------------------------------------------------------------------
401// wxDockTaskBarIcon::RemoveIcon
402//
403// Restores the old image for the dock via RestoreApplicationDockTileImage
404//-----------------------------------------------------------------------------
405bool wxDockTaskBarIcon::RemoveIcon()
607667fd 406{
d806d30a 407 if (m_pMenu)
af7e08a4
RN
408 {
409 delete m_pMenu;
410 m_pMenu = NULL;
411 }
30efba0a 412
d806d30a
DS
413 // restore old icon to the dock
414 OSStatus err = RestoreApplicationDockTileImage();
51c4d2a5 415 verify_noerr( err );
30efba0a 416
d806d30a 417 // restore the old menu to the dock
51c4d2a5 418 SetApplicationDockTileMenu( m_theLastMenu );
607667fd 419
f9f9c8fc
DS
420 bool success = (err == noErr);
421 m_iconAdded = !success;
422
423 return success;
607667fd 424}
f9f9c8fc 425
d806d30a
DS
426//-----------------------------------------------------------------------------
427// wxDockTaskBarIcon::PopupMenu
428//
429// 2.4 and wxPython method that "pops of the menu in the taskbar".
430//
fe224552 431// In reality because of the way the dock menu works in carbon
d806d30a
DS
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//-----------------------------------------------------------------------------
435bool wxDockTaskBarIcon::PopupMenu(wxMenu *menu)
607667fd 436{
af7e08a4
RN
437 wxASSERT(menu != NULL);
438
607667fd
RN
439 if (m_pMenu)
440 delete m_pMenu;
f9f9c8fc 441
51c4d2a5 442 // start copy of menu
d806d30a 443 m_pMenu = wxDeepCopyMenu(menu);
f9f9c8fc 444
51c4d2a5 445 // finish up
d806d30a
DS
446 m_pMenu->SetInvokingWindow(m_menuEventWindow);
447
448 return true;
449}
fe224552 450
d806d30a
DS
451//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
452//
453// wxTaskBarIcon
454//
455//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fe224552 456
d806d30a 457IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
fe224552 458
d806d30a
DS
459//-----------------------------------------------------------------------------
460// wxTaskBarIcon Constructor
461//
462// Creates the backend
463//
464// Note that we only support DOCK currently as others require cocoa and
465// also some require hacks and other such things. (MenuExtras are
fe224552 466// actually seperate programs that also require a special undocumented id
d806d30a
DS
467// hack and other such fun stuff).
468//-----------------------------------------------------------------------------
469wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType)
470{
51c4d2a5
DS
471 wxASSERT_MSG(
472 nType == DOCK,
473 wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") );
474
d806d30a 475 m_impl = new wxDockTaskBarIcon(this);
607667fd
RN
476}
477
d806d30a
DS
478//-----------------------------------------------------------------------------
479// wxTaskBarIcon Destructor
480//
481// Destroys the backend
482//-----------------------------------------------------------------------------
483wxTaskBarIcon::~wxTaskBarIcon()
b14ba1f1 484{
d806d30a 485 delete m_impl;
b14ba1f1
RN
486}
487
d806d30a
DS
488//-----------------------------------------------------------------------------
489// wxTaskBarIcon::SetIcon
490// wxTaskBarIcon::RemoveIcon
491// wxTaskBarIcon::PopupMenu
492//
493// Just calls the backend version of the said function.
494//-----------------------------------------------------------------------------
495bool wxTaskBarIcon::IsIconInstalled() const
496{ return m_impl->IsIconInstalled(); }
51c4d2a5 497
d806d30a
DS
498bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
499{ return m_impl->SetIcon(icon, tooltip); }
51c4d2a5 500
d806d30a 501bool wxTaskBarIcon::RemoveIcon()
f9f9c8fc 502{ return m_impl->RemoveIcon(); }
51c4d2a5 503
d806d30a
DS
504bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
505{ return m_impl->PopupMenu(menu); }
506
4f167b46 507#endif // wxUSE_TASKBARICON