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