]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/taskbar.cpp
declare lists as exported, seems to be necessary in VC5 DLL builds and shouldn't...
[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
fe224552 38class wxTaskBarIconImpl
d806d30a
DS
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:
fe224552
VZ
70 wxTaskBarIconWindow(wxTaskBarIconImpl* impl)
71 : wxTopLevelWindow(NULL, -1, wxT("")), m_impl(impl)
d806d30a 72 {
fe224552 73 Connect(-1, wxEVT_COMMAND_MENU_SELECTED,
d806d30a
DS
74 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent)
75 );
76 }
77
fe224552 78 void OnMenuEvent(wxCommandEvent& event)
d806d30a
DS
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);
fe224552 118
d806d30a
DS
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//-----------------------------------------------------------------------------
fe224552 137wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* parent)
d806d30a 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 //
fe224552 188 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
d806d30a
DS
189 // EventRef event, void *data)
190 //
fe224552 191 // However, in the case of a taskbar menu call
d806d30a
DS
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());
af7e08a4 196 OSErr err;
30efba0a 197
d806d30a
DS
198 // get the HICommand from the event
199 HICommand command;
200 err = GetEventParameter(inEvent, kEventParamDirectObject,
fe224552 201 typeHICommand, NULL,
d806d30a
DS
202 sizeof(HICommand), NULL, &command);
203 if (err == noErr)
fe224552 204 {
d806d30a
DS
205 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
206 //
fe224552
VZ
207 // NOTE: menuRef is generally used here for submenus, as
208 // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass
d806d30a
DS
209 // just the top level wxTaskBar menu
210 //
211 MenuItemIndex menuItemIndex;
212 MenuRef menuRef;
213
fe224552
VZ
214 err = GetIndMenuItemWithCommandID(taskbarMenuRef,
215 command.commandID,
d806d30a
DS
216 1, &menuRef, &menuItemIndex);
217 if (err == noErr)
218 {
219 MenuCommand id = command.commandID;
220 wxMenuItem* item = NULL;
30efba0a 221
d806d30a
DS
222 if (id != 0) // get the wxMenuItem reference from the MenuRef
223 GetMenuItemRefCon(menuRef, menuItemIndex, (UInt32*) &item);
30efba0a 224
d806d30a
DS
225 if (item)
226 {
227 // Handle items that are checkable
228 // FIXME: Doesn't work (at least on 10.2)!
229 if (item->IsCheckable())
230 item->Check( !item->IsChecked() ) ;
607667fd 231
d806d30a 232 // send the wxEvent to the wxMenu
fe224552
VZ
233 item->GetMenu()->SendEvent(id,
234 item->IsCheckable() ?
235 item->IsChecked() : -1
d806d30a
DS
236 );
237 err = noErr; // successfully handled the event
238 }
af7e08a4 239 }
fe224552 240 } //end if noErr on getting HICommand from event
af7e08a4 241
30efba0a
DS
242 // return whether we handled the event or not
243 return err;
607667fd 244 }
d806d30a
DS
245
246 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
fe224552
VZ
247 // otherwise something is truly funky
248 wxASSERT(eventClass == kEventClassApplication &&
d806d30a
DS
249 eventKind == kEventAppGetDockTileMenu);
250
251 // process the right click events
252 // NB: This may result in double or even triple-creation of the menus
253 // We need to do this for 2.4 compat, however
254 wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN,NULL);
255 pTB->m_parent->ProcessEvent(downevt);
256
257 wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP,NULL);
258 pTB->m_parent->ProcessEvent(upevt);
af7e08a4 259
30efba0a 260 // create popup menu
a45beea1 261 wxMenu* menu = pTB->DoCreatePopupMenu();
a45beea1 262
30efba0a
DS
263 OSStatus err = eventNotHandledErr;
264
265 if (menu != NULL)
a45beea1 266 {
30efba0a 267 // note to self - a MenuRef *is* a MenuHandle
a45beea1
RN
268 MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu());
269
30efba0a 270 // When SetEventParameter is called it will decrement
d806d30a
DS
271 // the reference count of the menu - we need to make
272 // sure it stays around in the wxMenu class here
a45beea1 273 RetainMenu(hMenu);
607667fd 274
d806d30a 275 // set the actual dock menu
fe224552 276 err = SetEventParameter(inEvent, kEventParamMenuRef,
d806d30a
DS
277 typeMenuRef, sizeof(MenuRef), &hMenu);
278 wxASSERT(err == noErr);
af7e08a4 279 }
30efba0a
DS
280
281 return err;
607667fd
RN
282}
283
d806d30a
DS
284//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285// wxDeepCopyMenu
286//
287// Performs a top-to-bottom copy of the input menu and all of its
288// submenus.
289//
290// This is mostly needed for 2.4 compatability. However wxPython and others
291// still use this way of setting the taskbarmenu.
292//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293wxMenu* wxDeepCopyMenu(wxMenu* menu)
294{
295 if (!menu)
296 return NULL;
297
298 //
299 // NB: Here we have to perform a deep copy of the menu,
300 // copying each and every menu item from menu to m_pMenu.
fe224552 301 // Other implementations use wxWindow::PopupMenu here,
d806d30a 302 // which idle execution until the user selects something,
fe224552 303 // but since the mac handles this internally, we can't -
d806d30a
DS
304 // and have no way at all to idle it while the dock menu
305 // is being shown before menu goes out of scope (it may
306 // not be on the heap, and may expire right after this function
fe224552 307 // is done - we need it to last until the carbon event is triggered -
d806d30a
DS
308 // that's when the user right clicks).
309 //
fe224552 310 // Also, since there is no equal (assignment) operator
d806d30a
DS
311 // on either wxMenu or wxMenuItem, we have to do all the
312 // dirty work ourselves.
313 //
314
315 // perform a deep copy of the menu
316 wxMenuItemList& theList = menu->GetMenuItems();
317 wxMenuItemList::compatibility_iterator theNode = theList.GetFirst();
30efba0a 318
d806d30a
DS
319 // create the main menu
320 wxMenu* m_pMenu = new wxMenu(menu->GetTitle());
321
322 while (theNode != NULL)
323 {
324 wxMenuItem* theItem = theNode->GetData();
30efba0a
DS
325 m_pMenu->Append(
326 new wxMenuItem(
327 m_pMenu, // parent menu
328 theItem->GetId(), // id
329 theItem->GetText(), // text label
330 theItem->GetHelp(), // status bar help string
331 theItem->GetKind(), // menu flags - checkable, separator, etc.
332 wxDeepCopyMenu(theItem->GetSubMenu()) // submenu
333 ));
d806d30a
DS
334 theNode = theNode->GetNext();
335 }
30efba0a 336
d806d30a
DS
337 return m_pMenu;
338}
607667fd 339
d806d30a 340//-----------------------------------------------------------------------------
30efba0a 341// wxDockTaskBarIcon ctor
d806d30a
DS
342//
343// Initializes the dock implementation of wxTaskBarIcon.
344//
345// Here we create some mac-specific event handlers and UPPs.
346//-----------------------------------------------------------------------------
fe224552 347wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon* parent)
d806d30a 348 : wxTaskBarIconImpl(parent),
fe224552
VZ
349 m_eventHandlerRef(NULL), m_pMenu(NULL),
350 m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false)
607667fd 351{
d806d30a 352 // register the events that will return the dock menu
30efba0a
DS
353 EventTypeSpec tbEventList[] =
354 {
355 { kEventClassCommand, kEventProcessCommand },
356 { kEventClassApplication, kEventAppGetDockTileMenu }
357 };
358
d806d30a
DS
359 m_eventupp = NewEventHandlerUPP(wxDockEventHandler);
360 wxASSERT(m_eventupp != NULL);
30efba0a 361
cc3388ae 362#ifdef __WXDEBUG__
e768548a 363 OSStatus err =
cc3388ae 364#endif
e768548a 365 InstallApplicationEventHandler(
d806d30a 366 m_eventupp,
fe224552 367 GetEventTypeCount(tbEventList), tbEventList,
d806d30a 368 this, &m_eventHandlerRef);
30efba0a 369 wxASSERT( err == noErr );
607667fd 370}
cc3388ae 371
d806d30a
DS
372//-----------------------------------------------------------------------------
373// wxDockTaskBarIcon Destructor
374//
375// Cleans up mac events and restores the old icon to the dock
376//-----------------------------------------------------------------------------
377wxDockTaskBarIcon::~wxDockTaskBarIcon()
607667fd 378{
d806d30a
DS
379 // clean up event handler and event UPP
380 RemoveEventHandler(m_eventHandlerRef);
381 DisposeEventHandlerUPP(m_eventupp);
af7e08a4 382
d806d30a 383 // restore old icon and menu to the dock
fe224552 384 RemoveIcon();
607667fd
RN
385}
386
d806d30a
DS
387//-----------------------------------------------------------------------------
388// wxDockTaskBarIcon::DoCreatePopupMenu
389//
fe224552 390// Helper function that handles a request from the dock event handler
d806d30a
DS
391// to get the menu for the dock
392//-----------------------------------------------------------------------------
393wxMenu* wxDockTaskBarIcon::DoCreatePopupMenu()
607667fd 394{
d806d30a 395 // get the menu from the parent
af7e08a4 396 wxMenu* theNewMenu = CreatePopupMenu();
30efba0a 397
af7e08a4
RN
398 if (theNewMenu)
399 {
d806d30a
DS
400 if (m_pMenu)
401 delete m_pMenu;
af7e08a4 402 m_pMenu = theNewMenu;
d806d30a 403 m_pMenu->SetInvokingWindow(m_menuEventWindow);
af7e08a4 404 }
30efba0a 405
fe224552 406 // the return here can be one of three things
d806d30a
DS
407 // (in order of priority):
408 // 1) User passed a menu from CreatePopupMenu override
409 // 2) menu sent to and copied from PopupMenu
410 // 3) If neither (1) or (2), then NULL
411 //
607667fd
RN
412 return m_pMenu;
413}
414
d806d30a
DS
415//-----------------------------------------------------------------------------
416// wxDockTaskBarIcon::IsIconInstalled
417//
418// Returns whether or not the dock is not using the default image
419//-----------------------------------------------------------------------------
420bool wxDockTaskBarIcon::IsIconInstalled() const
fe224552
VZ
421{
422 return m_iconAdded;
d806d30a
DS
423}
424
425//-----------------------------------------------------------------------------
426// wxDockTaskBarIcon::SetIcon
427//
428// Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
429//-----------------------------------------------------------------------------
430bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
607667fd 431{
30efba0a
DS
432 // convert the wxIcon into a wxBitmap so we can perform some
433 // wxBitmap operations with it
20b69855 434 wxBitmap bmp( icon ) ;
f9f9c8fc 435 wxASSERT( bmp.Ok() );
20b69855 436
30efba0a
DS
437 // get the CGImageRef for the wxBitmap:
438 // OSX builds only, but then the dock only exists in OSX
fe224552 439 CGImageRef pImage = (CGImageRef) bmp.CGImageCreate();
30efba0a 440 wxASSERT( pImage != NULL );
d806d30a 441
30efba0a
DS
442 // actually set the dock image
443 OSStatus err = SetApplicationDockTileImage( pImage );
444 wxASSERT( err == noErr );
445
446 // free the CGImage, now that it's referenced by the dock
739e35e4 447 if (pImage != NULL)
30efba0a
DS
448 CGImageRelease( pImage );
449
f9f9c8fc
DS
450 bool success = (err == noErr);
451 m_iconAdded = success;
452
453 return success;
607667fd 454}
30efba0a 455
d806d30a
DS
456//-----------------------------------------------------------------------------
457// wxDockTaskBarIcon::RemoveIcon
458//
459// Restores the old image for the dock via RestoreApplicationDockTileImage
460//-----------------------------------------------------------------------------
461bool wxDockTaskBarIcon::RemoveIcon()
607667fd 462{
d806d30a 463 if (m_pMenu)
af7e08a4
RN
464 {
465 delete m_pMenu;
466 m_pMenu = NULL;
467 }
30efba0a 468
d806d30a
DS
469 // restore old icon to the dock
470 OSStatus err = RestoreApplicationDockTileImage();
471 wxASSERT(err == noErr);
30efba0a 472
d806d30a
DS
473 // restore the old menu to the dock
474 SetApplicationDockTileMenu(m_theLastMenu);
607667fd 475
f9f9c8fc
DS
476 bool success = (err == noErr);
477 m_iconAdded = !success;
478
479 return success;
607667fd 480}
f9f9c8fc 481
d806d30a
DS
482//-----------------------------------------------------------------------------
483// wxDockTaskBarIcon::PopupMenu
484//
485// 2.4 and wxPython method that "pops of the menu in the taskbar".
486//
fe224552 487// In reality because of the way the dock menu works in carbon
d806d30a
DS
488// we just save the menu, and if the user didn't override CreatePopupMenu
489// return the menu passed here, thus sort of getting the same effect.
490//-----------------------------------------------------------------------------
491bool wxDockTaskBarIcon::PopupMenu(wxMenu *menu)
607667fd 492{
af7e08a4
RN
493 wxASSERT(menu != NULL);
494
607667fd
RN
495 if (m_pMenu)
496 delete m_pMenu;
f9f9c8fc 497
d806d30a
DS
498 //start copy of menu
499 m_pMenu = wxDeepCopyMenu(menu);
f9f9c8fc 500
d806d30a
DS
501 //finish up
502 m_pMenu->SetInvokingWindow(m_menuEventWindow);
503
504 return true;
505}
fe224552 506
d806d30a
DS
507//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
508//
509// wxTaskBarIcon
510//
511//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fe224552 512
d806d30a 513IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
fe224552 514
d806d30a
DS
515//-----------------------------------------------------------------------------
516// wxTaskBarIcon Constructor
517//
518// Creates the backend
519//
520// Note that we only support DOCK currently as others require cocoa and
521// also some require hacks and other such things. (MenuExtras are
fe224552 522// actually seperate programs that also require a special undocumented id
d806d30a
DS
523// hack and other such fun stuff).
524//-----------------------------------------------------------------------------
525wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType)
526{
fe224552 527 wxASSERT_MSG(nType == DOCK,
d806d30a
DS
528 wxT("Only the DOCK implementation of wxTaskBarIcon")
529 wxT("on mac carbon is currently supported!"));
530 m_impl = new wxDockTaskBarIcon(this);
607667fd
RN
531}
532
d806d30a
DS
533//-----------------------------------------------------------------------------
534// wxTaskBarIcon Destructor
535//
536// Destroys the backend
537//-----------------------------------------------------------------------------
538wxTaskBarIcon::~wxTaskBarIcon()
b14ba1f1 539{
d806d30a 540 delete m_impl;
b14ba1f1
RN
541}
542
d806d30a
DS
543//-----------------------------------------------------------------------------
544// wxTaskBarIcon::SetIcon
545// wxTaskBarIcon::RemoveIcon
546// wxTaskBarIcon::PopupMenu
547//
548// Just calls the backend version of the said function.
549//-----------------------------------------------------------------------------
550bool wxTaskBarIcon::IsIconInstalled() const
551{ return m_impl->IsIconInstalled(); }
552bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
553{ return m_impl->SetIcon(icon, tooltip); }
554bool wxTaskBarIcon::RemoveIcon()
f9f9c8fc 555{ return m_impl->RemoveIcon(); }
d806d30a
DS
556bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
557{ return m_impl->PopupMenu(menu); }
558
f2641bc2 559#endif //wxHAS_TASK_BAR_ICON