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