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