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