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