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