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