1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTaskBarIcon OSX Implementation
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 //=============================================================================
14 //=============================================================================
16 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
22 #ifdef wxHAS_TASK_BAR_ICON
24 #include "wx/mac/private.h"
26 #include "wx/taskbar.h"
29 #include "wx/dcmemory.h"
31 //-----------------------------------------------------------------------------
35 // Superclass of wxTaskBarIcon implementations
36 //-----------------------------------------------------------------------------
38 class wxTaskBarIconImpl
41 wxTaskBarIconImpl(wxTaskBarIcon
* parent
);
42 virtual ~wxTaskBarIconImpl();
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;
49 wxMenu
* CreatePopupMenu()
50 { return m_parent
->CreatePopupMenu(); }
52 wxTaskBarIcon
* m_parent
;
53 class wxTaskBarIconWindow
* m_menuEventWindow
;
56 //-----------------------------------------------------------------------------
58 // wxTaskBarIconWindow
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 //-----------------------------------------------------------------------------
65 class wxTaskBarIconWindow
: public wxTopLevelWindow
68 wxTaskBarIconWindow(wxTaskBarIconImpl
* impl
)
69 : wxTopLevelWindow(NULL
, -1, wxT("")), m_impl(impl
)
71 Connect(-1, wxEVT_COMMAND_MENU_SELECTED
,
72 wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent
)
76 void OnMenuEvent(wxCommandEvent
& event
)
78 m_impl
->m_parent
->ProcessEvent(event
);
82 wxTaskBarIconImpl
* m_impl
;
85 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 class wxDockTaskBarIcon
: public wxTaskBarIconImpl
94 wxDockTaskBarIcon(wxTaskBarIcon
* parent
);
95 virtual ~wxDockTaskBarIcon();
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
);
102 wxMenu
* DoCreatePopupMenu();
104 EventHandlerRef m_eventHandlerRef
;
105 EventHandlerUPP m_eventupp
;
107 MenuRef m_theLastMenu
;
109 wxWindow
* m_eventWindow
;
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
);
118 //=============================================================================
122 //=============================================================================
124 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
128 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130 //-----------------------------------------------------------------------------
131 // wxTaskBarIconImpl Constructor
133 // Initializes members and creates the event window
134 //-----------------------------------------------------------------------------
135 wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon
* parent
)
136 : m_parent(parent
), m_menuEventWindow(new wxTaskBarIconWindow(this))
140 //-----------------------------------------------------------------------------
141 // wxTaskBarIconImpl Destructor
143 // Cleans up the event window
144 //-----------------------------------------------------------------------------
145 wxTaskBarIconImpl::~wxTaskBarIconImpl()
147 delete m_menuEventWindow
;
150 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
154 // OS X DOCK implementation of wxTaskBarIcon using carbon
155 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 // wxDockEventHandler
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
)
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
);
174 // Handle wxTaskBar menu events (note that this is a global event handler
175 // so it will actually get called by all commands/menus)
177 if (eventClass
== kEventClassCommand
&& eventKind
== kEventCommandProcess
)
179 // if we have no taskbar menu quickly pass it back to wxApp
182 return eventNotHandledErr
;
186 // This is the real reason why we need this. Normally menus
187 // get handled in wxMacAppEventHandler
189 // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler,
190 // EventRef event, void *data)
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.
196 MenuRef taskbarMenuRef
= MAC_WXHMENU(pTB
->m_pMenu
->GetHMenu());
197 OSStatus result
= eventNotHandledErr
;
200 // get the HICommand from the event
202 err
= GetEventParameter(inEvent
, kEventParamDirectObject
,
204 sizeof(HICommand
), NULL
, &command
);
208 // Obtain the REAL menuRef and the menuItemIndex in the real menuRef
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
214 MenuItemIndex menuItemIndex
;
217 err
= GetIndMenuItemWithCommandID(taskbarMenuRef
,
219 1, &menuRef
, &menuItemIndex
);
222 MenuCommand id
= command
.commandID
;
223 wxMenuItem
* item
= NULL
;
225 if (id
!= 0) // get the wxMenuItem reference from the MenuRef
226 GetMenuItemRefCon(menuRef
, menuItemIndex
, (UInt32
*) &item
);
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() ) ;
235 // send the wxEvent to the wxMenu
236 item
->GetMenu()->SendEvent(id
,
237 item
->IsCheckable() ?
238 item
->IsChecked() : -1
240 err
= noErr
; // successfully handled the event
243 } //end if noErr on getting HICommand from event
245 return err
; // return whether we handled the event or not
248 // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here,
249 // otherwise something is truly funky
250 wxASSERT(eventClass
== kEventClassApplication
&&
251 eventKind
== kEventAppGetDockTileMenu
);
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
);
259 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
260 pTB
->m_parent
->ProcessEvent(upevt
);
263 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
265 OSStatus err
= noErr
;
269 //note to self - a MenuRef IS A MenuHandle
270 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
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
277 // set the actual dock menu
278 err
= SetEventParameter(inEvent
, kEventParamMenuRef
,
279 typeMenuRef
, sizeof(MenuRef
), &hMenu
);
280 wxASSERT(err
== noErr
);
285 return eventNotHandledErr
;
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 // Performs a top-to-bottom copy of the input menu and all of its
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
)
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).
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.
319 // perform a deep copy of the menu
320 wxMenuItemList
& theList
= menu
->GetMenuItems();
321 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
323 // create the main menu
324 wxMenu
* m_pMenu
= new wxMenu(menu
->GetTitle());
326 while (theNode
!= NULL
)
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
336 theNode
= theNode
->GetNext();
342 //-----------------------------------------------------------------------------
343 // wxDockTaskBarIcon Constructor
345 // Initializes the dock implementation of wxTaskBarIcon.
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)
354 // register the events that will return the dock menu
355 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
356 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
358 m_eventupp
= NewEventHandlerUPP(wxDockEventHandler
);
359 wxASSERT(m_eventupp
!= NULL
);
364 InstallApplicationEventHandler(
366 GetEventTypeCount(tbEventList
), tbEventList
,
367 this, &m_eventHandlerRef
);
368 wxASSERT(err
== noErr
);
371 //-----------------------------------------------------------------------------
372 // wxDockTaskBarIcon Destructor
374 // Cleans up mac events and restores the old icon to the dock
375 //-----------------------------------------------------------------------------
376 wxDockTaskBarIcon::~wxDockTaskBarIcon()
378 // clean up event handler and event UPP
379 RemoveEventHandler(m_eventHandlerRef
);
380 DisposeEventHandlerUPP(m_eventupp
);
382 // restore old icon and menu to the dock
386 //-----------------------------------------------------------------------------
387 // wxDockTaskBarIcon::DoCreatePopupMenu
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()
394 // get the menu from the parent
395 wxMenu
* theNewMenu
= CreatePopupMenu();
401 m_pMenu
= theNewMenu
;
402 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
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
414 //-----------------------------------------------------------------------------
415 // wxDockTaskBarIcon::IsIconInstalled
417 // Returns whether or not the dock is not using the default image
418 //-----------------------------------------------------------------------------
419 bool wxDockTaskBarIcon::IsIconInstalled() const
424 //-----------------------------------------------------------------------------
425 // wxDockTaskBarIcon::SetIcon
427 // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage
428 //-----------------------------------------------------------------------------
429 bool wxDockTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
431 wxBitmap
bmp( icon
) ;
432 OSStatus err
= noErr
;
436 #if 0 // is always available under OSX now -- crashes on 10.2 in CFRetain() - RN
437 pImage
= (CGImageRef
) bmp
.CGImageCreate() ;
441 iconport
= bmp
.GetHBITMAP( &maskport
) ;
445 // Make a mask with no transparent pixels
446 wxBitmap
mbmp(icon
.GetWidth(), icon
.GetHeight());
448 dc
.SelectObject(mbmp
);
449 dc
.SetBackground(*wxBLACK_BRUSH
);
451 dc
.SelectObject(wxNullBitmap
);
452 bmp
.SetMask( new wxMask(mbmp
, *wxWHITE
) ) ;
453 iconport
= bmp
.GetHBITMAP( &maskport
) ;
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
)),
465 wxASSERT(pImage
!= NULL
);
466 err
= SetApplicationDockTileImage(pImage
);
471 CGImageRelease(pImage
);
473 return m_iconAdded
= err
== noErr
;
476 //-----------------------------------------------------------------------------
477 // wxDockTaskBarIcon::RemoveIcon
479 // Restores the old image for the dock via RestoreApplicationDockTileImage
480 //-----------------------------------------------------------------------------
481 bool wxDockTaskBarIcon::RemoveIcon()
489 // restore old icon to the dock
490 OSStatus err
= RestoreApplicationDockTileImage();
491 wxASSERT(err
== noErr
);
493 // restore the old menu to the dock
494 SetApplicationDockTileMenu(m_theLastMenu
);
496 return !(m_iconAdded
= !(err
== noErr
));
499 //-----------------------------------------------------------------------------
500 // wxDockTaskBarIcon::PopupMenu
502 // 2.4 and wxPython method that "pops of the menu in the taskbar".
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
)
510 wxASSERT(menu
!= NULL
);
516 m_pMenu
= wxDeepCopyMenu(menu
);
519 m_pMenu
->SetInvokingWindow(m_menuEventWindow
);
524 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
528 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
530 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
532 //-----------------------------------------------------------------------------
533 // wxTaskBarIcon Constructor
535 // Creates the backend
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
)
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);
550 //-----------------------------------------------------------------------------
551 // wxTaskBarIcon Destructor
553 // Destroys the backend
554 //-----------------------------------------------------------------------------
555 wxTaskBarIcon::~wxTaskBarIcon()
560 //-----------------------------------------------------------------------------
561 // wxTaskBarIcon::SetIcon
562 // wxTaskBarIcon::RemoveIcon
563 // wxTaskBarIcon::PopupMenu
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
); }
576 #endif //wxHAS_TASK_BAR_ICON