1 /////////////////////////////////////////////////////////////////////////
2 // File: src/cocoa/taskbar.mm
3 // Purpose: Implements wxTaskBarIcon class
4 // Author: David Elliott
7 // RCS-ID: $Id: taskbar.mm 35650 2005-09-23 12:56:45Z MR $
8 // Copyright: (c) 2004 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
13 #ifdef wxUSE_TASKBARICON
19 #include "wx/dcclient.h"
22 #include "wx/taskbar.h"
24 #import <AppKit/NSApplication.h>
25 #import <AppKit/NSImage.h>
26 #import <AppKit/NSMenu.h>
27 #import <AppKit/NSMenuItem.h>
28 #import <AppKit/NSStatusBar.h>
29 #import <AppKit/NSStatusItem.h>
30 #import <AppKit/NSView.h>
31 #import <Foundation/NSArray.h>
32 #import <Foundation/NSEnumerator.h>
34 #import <AppKit/NSEvent.h>
35 #import <AppKit/NSWindow.h>
36 #import <AppKit/NSGraphicsContext.h>
38 #include "wx/cocoa/autorelease.h"
40 // A category for methods that are only present in Panther's SDK
41 @interface NSStatusItem(wxNSStatusItemPrePantherCompatibility)
42 - (void)popUpStatusItemMenu:(NSMenu *)menu;
45 class wxTaskBarIconWindow;
47 // ============================================================================
49 // Base class for the various Cocoa implementations.
50 // ============================================================================
51 class wxTaskBarIconImpl
54 wxTaskBarIconImpl(wxTaskBarIcon *taskBarIcon)
55 : m_taskBarIcon(taskBarIcon)
58 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString) = 0;
59 virtual bool RemoveIcon() = 0;
60 virtual bool PopupMenu(wxMenu *menu) = 0;
61 virtual ~wxTaskBarIconImpl();
62 inline wxTaskBarIcon* GetTaskBarIcon() { return m_taskBarIcon; }
63 wxMenu * CreatePopupMenu()
64 { return m_taskBarIcon->CreatePopupMenu(); }
66 DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl)
69 wxTaskBarIcon *m_taskBarIcon;
70 wxTaskBarIconWindow *m_iconWindow;
75 // ============================================================================
76 // wxTaskBarIconDockImpl
77 // An implementation using the Dock icon.
78 // ============================================================================
79 class wxTaskBarIconDockImpl: public wxTaskBarIconImpl
82 wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon);
83 virtual ~wxTaskBarIconDockImpl();
84 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString);
85 virtual bool RemoveIcon();
86 virtual bool PopupMenu(wxMenu *menu);
88 static WX_NSMenu CocoaGetDockNSMenu();
90 WX_NSMenu CocoaDoGetDockNSMenu();
91 WX_NSImage m_originalDockIcon;
92 // There can be only one Dock icon, so make sure we keep it that way
93 static wxTaskBarIconDockImpl *sm_dockIcon;
95 wxTaskBarIconDockImpl();
98 // ============================================================================
99 // wxTaskBarIconCustomStatusItemImpl
100 // An implementation using an NSStatusItem with a custom NSView
101 // ============================================================================
102 class wxTaskBarIconCustomStatusItemImpl: public wxTaskBarIconImpl
105 wxTaskBarIconCustomStatusItemImpl(wxTaskBarIcon *taskBarIcon);
106 virtual ~wxTaskBarIconCustomStatusItemImpl();
107 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString);
108 virtual bool RemoveIcon();
109 virtual bool PopupMenu(wxMenu *menu);
111 NSStatusItem *m_cocoaNSStatusItem;
113 wxTaskBarIconCustomStatusItemImpl();
116 // ============================================================================
117 // wxTaskBarIconWindow
118 // Used by all implementations to forward events from the wxMenu
119 // ============================================================================
120 class wxTaskBarIconWindow: public wxWindow
122 DECLARE_EVENT_TABLE()
124 wxTaskBarIconWindow(wxTaskBarIconImpl *taskBarIconImpl)
126 , m_taskBarIconImpl(taskBarIconImpl)
127 { wxASSERT(m_taskBarIconImpl); }
129 void OnMenuEvent(wxCommandEvent& event);
131 wxTaskBarIconImpl *m_taskBarIconImpl;
134 // ============================================================================
135 // wxTaskBarIconWindowCustom
136 // Used by the CustomStatusIcon implementation for the custom NSView.
137 // ============================================================================
138 class wxTaskBarIconWindowCustom: public wxTaskBarIconWindow
140 DECLARE_EVENT_TABLE()
142 wxTaskBarIconWindowCustom(wxTaskBarIconImpl *taskBarIconImpl)
143 : wxTaskBarIconWindow(taskBarIconImpl)
145 void SetIcon(const wxIcon& icon)
147 void OnMouseEvent(wxMouseEvent &event);
148 void OnPaint(wxPaintEvent &event);
153 // ============================================================================
154 // wxTaskBarIcon implementation
156 // ============================================================================
157 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
159 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType iconType)
162 m_impl = new wxTaskBarIconDockImpl(this);
163 else if(iconType == CUSTOM_STATUSITEM)
164 m_impl = new wxTaskBarIconCustomStatusItemImpl(this);
167 wxFAIL_MSG(wxT("Invalid wxTaskBarIcon type"));
171 wxTaskBarIcon::~wxTaskBarIcon()
178 bool wxTaskBarIcon::IsIconInstalled() const
183 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
185 return m_impl->SetIcon(icon,tooltip);
188 bool wxTaskBarIcon::RemoveIcon()
190 return m_impl->RemoveIcon();
193 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
195 return m_impl->PopupMenu(menu);
198 // ============================================================================
200 // ============================================================================
202 wxTaskBarIconImpl::~wxTaskBarIconImpl()
204 // wxAutoNSAutoreleasePool pool;
208 // ============================================================================
209 // wxTaskBarIconDockImpl
210 // ============================================================================
211 wxTaskBarIconDockImpl *wxTaskBarIconDockImpl::sm_dockIcon = NULL;
213 wxTaskBarIconDockImpl::wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon)
214 : wxTaskBarIconImpl(taskBarIcon)
216 m_originalDockIcon = nil;
217 wxASSERT_MSG(!sm_dockIcon, wxT("You should never have more than one dock icon!"));
221 wxTaskBarIconDockImpl::~wxTaskBarIconDockImpl()
223 // wxAutoNSAutoreleasePool pool;
224 if(sm_dockIcon == this)
228 WX_NSMenu wxTaskBarIconDockImpl::CocoaGetDockNSMenu()
231 return sm_dockIcon->CocoaDoGetDockNSMenu();
235 WX_NSMenu wxTaskBarIconDockImpl::CocoaDoGetDockNSMenu()
237 wxMenu *dockMenu = CreatePopupMenu();
241 m_iconWindow = new wxTaskBarIconWindow(this);
242 dockMenu->SetInvokingWindow(m_iconWindow);
243 dockMenu->UpdateUI();
244 //dockMenu->SetCocoaDeletes(true);
245 return (WX_NSMenu)dockMenu->GetHMenu();
248 bool wxTaskBarIconDockImpl::SetIcon(const wxIcon& icon, const wxString& tooltip)
250 wxAutoNSAutoreleasePool pool;
251 m_originalDockIcon = [[[NSApplication sharedApplication] applicationIconImage] retain];
252 //[[NSApplication sharedApplication] setApplicationIconImage:icon.GetNSImage()];
256 bool wxTaskBarIconDockImpl::RemoveIcon()
258 [[NSApplication sharedApplication] setApplicationIconImage:m_originalDockIcon];
259 [m_originalDockIcon release];
263 bool wxTaskBarIconDockImpl::PopupMenu(wxMenu *menu)
265 wxFAIL_MSG(wxT("You cannot force the Dock icon menu to popup"));
270 // ============================================================================
271 // wxTaskBarIconCustomStatusItemImpl
272 // ============================================================================
273 wxTaskBarIconCustomStatusItemImpl::wxTaskBarIconCustomStatusItemImpl(wxTaskBarIcon *taskBarIcon)
274 : wxTaskBarIconImpl(taskBarIcon)
276 m_cocoaNSStatusItem = nil;
279 wxTaskBarIconCustomStatusItemImpl::~wxTaskBarIconCustomStatusItemImpl()
283 bool wxTaskBarIconCustomStatusItemImpl::SetIcon(const wxIcon& icon, const wxString& tooltip)
285 wxAutoNSAutoreleasePool pool;
286 if(!m_cocoaNSStatusItem)
288 m_cocoaNSStatusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
289 [m_cocoaNSStatusItem retain];
292 m_iconWindow= new wxTaskBarIconWindowCustom(this);
293 static_cast<wxTaskBarIconWindowCustom*>(m_iconWindow)->SetIcon(icon);
294 // FIXME: no less than 10 because most icon types don't work yet
295 // and this allows us to see how task bar icons would work
296 [(NSView*)m_iconWindow->GetHandle() setFrame:NSMakeRect(0.0,0.0,wxMax(10,icon.GetWidth()),[[NSStatusBar systemStatusBar] thickness])];
297 [m_cocoaNSStatusItem setView:(NSView*)m_iconWindow->GetHandle()];
301 bool wxTaskBarIconCustomStatusItemImpl::RemoveIcon()
303 [m_cocoaNSStatusItem release];
304 m_cocoaNSStatusItem = nil;
310 bool wxTaskBarIconCustomStatusItemImpl::PopupMenu(wxMenu *menu)
313 menu->SetInvokingWindow(m_iconWindow);
316 if([m_cocoaNSStatusItem respondsToSelector:@selector(popUpStatusItemMenu:)])
318 [m_cocoaNSStatusItem popUpStatusItemMenu:(NSMenu*)menu->GetHMenu()];
321 { // pretty good fake for OS X < 10.3
322 NSEvent *nsevent = [NSEvent mouseEventWithType:NSLeftMouseDown
323 location:NSMakePoint(-1.0,-4.0) modifierFlags:0 timestamp:0
324 windowNumber:[[(NSView*)m_iconWindow->GetHandle() window] windowNumber]
325 context:[NSGraphicsContext currentContext]
326 eventNumber:0 clickCount:1 pressure:0.0];
327 [NSMenu popUpContextMenu:menu->GetHMenu() withEvent:nsevent forView:(NSView*)m_iconWindow->GetHandle()];
329 menu->SetInvokingWindow(NULL);
333 // ============================================================================
334 // wxTaskBarIconWindow
335 // ============================================================================
336 BEGIN_EVENT_TABLE(wxTaskBarIconWindow, wxWindow)
337 EVT_MENU(-1, wxTaskBarIconWindow::OnMenuEvent)
340 void wxTaskBarIconWindow::OnMenuEvent(wxCommandEvent &event)
342 m_taskBarIconImpl->GetTaskBarIcon()->ProcessEvent(event);
345 // ============================================================================
346 // wxTaskBarIconWindowCustom
347 // ============================================================================
348 BEGIN_EVENT_TABLE(wxTaskBarIconWindowCustom, wxTaskBarIconWindow)
349 EVT_MOUSE_EVENTS(wxTaskBarIconWindowCustom::OnMouseEvent)
350 EVT_PAINT(wxTaskBarIconWindowCustom::OnPaint)
353 void wxTaskBarIconWindowCustom::OnMouseEvent(wxMouseEvent &event)
355 wxEventType tbEventType = 0;
356 if(event.GetEventType() == wxEVT_MOTION)
357 tbEventType = wxEVT_TASKBAR_MOVE;
358 else if(event.GetEventType() == wxEVT_LEFT_DOWN)
359 tbEventType = wxEVT_TASKBAR_LEFT_DOWN;
360 else if(event.GetEventType() == wxEVT_LEFT_UP)
361 tbEventType = wxEVT_TASKBAR_LEFT_UP;
362 else if(event.GetEventType() == wxEVT_RIGHT_DOWN)
363 tbEventType = wxEVT_TASKBAR_RIGHT_DOWN;
364 else if(event.GetEventType() == wxEVT_RIGHT_UP)
365 tbEventType = wxEVT_TASKBAR_RIGHT_UP;
366 else if(event.GetEventType() == wxEVT_LEFT_DCLICK)
367 tbEventType = wxEVT_TASKBAR_LEFT_DCLICK;
368 else if(event.GetEventType() == wxEVT_RIGHT_DCLICK)
369 tbEventType = wxEVT_TASKBAR_RIGHT_DCLICK;
372 wxTaskBarIconEvent tbiEvent(tbEventType,m_taskBarIconImpl->GetTaskBarIcon());
373 m_taskBarIconImpl->GetTaskBarIcon()->ProcessEvent(tbiEvent);
376 void wxTaskBarIconWindowCustom::OnPaint(wxPaintEvent &event)
379 // FIXME: This is a temporary hack until we can see real icons
380 dc.SetBackground(wxBrush(*wxBLUE));
382 dc.DrawIcon(m_icon,0,0);
385 #endif //def wxHAS_TASK_BAR_ICON