1 /////////////////////////////////////////////////////////////////////////
2 // File: src/osx/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"
19 #include "wx/dcclient.h"
22 #include "wx/taskbar.h"
24 #include "wx/osx/private.h"
26 // A category for methods that are only present in Panther's SDK
27 @interface NSStatusItem(wxNSStatusItemPrePantherCompatibility)
28 - (void)popUpStatusItemMenu:(NSMenu *)menu;
31 class wxTaskBarIconWindow;
33 // ============================================================================
35 // Base class for the various Cocoa implementations.
36 // ============================================================================
37 class wxTaskBarIconImpl
40 wxTaskBarIconImpl(wxTaskBarIcon *taskBarIcon)
41 : m_taskBarIcon(taskBarIcon)
44 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString) = 0;
45 virtual bool RemoveIcon() = 0;
46 virtual bool PopupMenu(wxMenu *menu) = 0;
47 virtual ~wxTaskBarIconImpl();
48 inline wxTaskBarIcon* GetTaskBarIcon() { return m_taskBarIcon; }
49 wxMenu * CreatePopupMenu()
50 { return m_taskBarIcon->CreatePopupMenu(); }
52 wxDECLARE_NO_COPY_CLASS(wxTaskBarIconImpl);
55 wxTaskBarIcon *m_taskBarIcon;
56 wxTaskBarIconWindow *m_iconWindow;
61 // ============================================================================
62 // wxTaskBarIconDockImpl
63 // An implementation using the Dock icon.
64 // ============================================================================
65 class wxTaskBarIconDockImpl: public wxTaskBarIconImpl
68 wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon);
69 virtual ~wxTaskBarIconDockImpl();
70 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString);
71 virtual bool RemoveIcon();
72 virtual bool PopupMenu(wxMenu *menu);
74 static WX_NSMenu CocoaGetDockNSMenu();
76 WX_NSMenu CocoaDoGetDockNSMenu();
77 WX_NSImage m_originalDockIcon;
78 // There can be only one Dock icon, so make sure we keep it that way
79 static wxTaskBarIconDockImpl *sm_dockIcon;
81 wxTaskBarIconDockImpl();
84 // ============================================================================
85 // wxTaskBarIconCustomStatusItemImpl
86 // An implementation using an NSStatusItem with a custom NSView
87 // ============================================================================
88 class wxTaskBarIconCustomStatusItemImpl: public wxTaskBarIconImpl
91 wxTaskBarIconCustomStatusItemImpl(wxTaskBarIcon *taskBarIcon);
92 virtual ~wxTaskBarIconCustomStatusItemImpl();
93 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString);
94 virtual bool RemoveIcon();
95 virtual bool PopupMenu(wxMenu *menu);
97 NSStatusItem *m_cocoaNSStatusItem;
99 wxTaskBarIconCustomStatusItemImpl();
102 // ============================================================================
103 // wxTaskBarIconWindow
104 // Used by all implementations to forward events from the wxMenu
105 // ============================================================================
106 class wxTaskBarIconWindow: public wxWindow
108 DECLARE_EVENT_TABLE()
110 wxTaskBarIconWindow(wxTaskBarIconImpl *taskBarIconImpl)
112 , m_taskBarIconImpl(taskBarIconImpl)
113 { wxASSERT(m_taskBarIconImpl); }
115 void OnMenuEvent(wxCommandEvent& event);
117 wxTaskBarIconImpl *m_taskBarIconImpl;
120 // ============================================================================
121 // wxTaskBarIconWindowCustom
122 // Used by the CustomStatusIcon implementation for the custom NSView.
123 // ============================================================================
124 class wxTaskBarIconWindowCustom: public wxTaskBarIconWindow
126 DECLARE_EVENT_TABLE()
128 wxTaskBarIconWindowCustom(wxTaskBarIconImpl *taskBarIconImpl)
129 : wxTaskBarIconWindow(taskBarIconImpl)
131 void SetIcon(const wxIcon& icon)
133 void OnMouseEvent(wxMouseEvent &event);
134 void OnPaint(wxPaintEvent &event);
139 // ============================================================================
140 // wxTaskBarIcon implementation
142 // ============================================================================
143 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
145 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType iconType)
148 m_impl = new wxTaskBarIconDockImpl(this);
149 else if(iconType == CUSTOM_STATUSITEM)
150 m_impl = new wxTaskBarIconCustomStatusItemImpl(this);
153 wxFAIL_MSG(wxT("Invalid wxTaskBarIcon type"));
157 wxTaskBarIcon::~wxTaskBarIcon()
164 bool wxTaskBarIcon::IsIconInstalled() const
169 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
171 return m_impl->SetIcon(icon,tooltip);
174 bool wxTaskBarIcon::RemoveIcon()
176 return m_impl->RemoveIcon();
179 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
181 return m_impl->PopupMenu(menu);
184 // ============================================================================
186 // ============================================================================
188 wxTaskBarIconImpl::~wxTaskBarIconImpl()
190 // wxAutoNSAutoreleasePool pool;
194 // ============================================================================
195 // wxTaskBarIconDockImpl
196 // ============================================================================
197 wxTaskBarIconDockImpl *wxTaskBarIconDockImpl::sm_dockIcon = NULL;
199 wxTaskBarIconDockImpl::wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon)
200 : wxTaskBarIconImpl(taskBarIcon)
202 m_originalDockIcon = nil;
203 wxASSERT_MSG(!sm_dockIcon, wxT("You should never have more than one dock icon!"));
207 wxTaskBarIconDockImpl::~wxTaskBarIconDockImpl()
209 // wxAutoNSAutoreleasePool pool;
210 if(sm_dockIcon == this)
214 WX_NSMenu wxTaskBarIconDockImpl::CocoaGetDockNSMenu()
217 return sm_dockIcon->CocoaDoGetDockNSMenu();
221 WX_NSMenu wxTaskBarIconDockImpl::CocoaDoGetDockNSMenu()
223 wxMenu *dockMenu = CreatePopupMenu();
227 m_iconWindow = new wxTaskBarIconWindow(this);
228 dockMenu->SetInvokingWindow(m_iconWindow);
229 dockMenu->UpdateUI();
230 //dockMenu->SetCocoaDeletes(true);
231 return (WX_NSMenu)dockMenu->GetHMenu();
234 bool wxTaskBarIconDockImpl::SetIcon(const wxIcon& WXUNUSED(icon), const wxString& WXUNUSED(tooltip))
236 wxMacAutoreleasePool pool;
237 m_originalDockIcon = [[[NSApplication sharedApplication] applicationIconImage] retain];
238 //[[NSApplication sharedApplication] setApplicationIconImage:icon.GetNSImage()];
242 bool wxTaskBarIconDockImpl::RemoveIcon()
244 [[NSApplication sharedApplication] setApplicationIconImage:m_originalDockIcon];
245 [m_originalDockIcon release];
249 bool wxTaskBarIconDockImpl::PopupMenu(wxMenu *WXUNUSED(menu))
251 wxFAIL_MSG(wxT("You cannot force the Dock icon menu to popup"));
256 // ============================================================================
257 // wxTaskBarIconCustomStatusItemImpl
258 // ============================================================================
259 wxTaskBarIconCustomStatusItemImpl::wxTaskBarIconCustomStatusItemImpl(wxTaskBarIcon *taskBarIcon)
260 : wxTaskBarIconImpl(taskBarIcon)
262 m_cocoaNSStatusItem = nil;
265 wxTaskBarIconCustomStatusItemImpl::~wxTaskBarIconCustomStatusItemImpl()
269 bool wxTaskBarIconCustomStatusItemImpl::SetIcon(const wxIcon& icon, const wxString& WXUNUSED(tooltip))
271 wxMacAutoreleasePool pool;
272 if(!m_cocoaNSStatusItem)
274 m_cocoaNSStatusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
275 [m_cocoaNSStatusItem retain];
278 m_iconWindow= new wxTaskBarIconWindowCustom(this);
279 static_cast<wxTaskBarIconWindowCustom*>(m_iconWindow)->SetIcon(icon);
280 // FIXME: no less than 10 because most icon types don't work yet
281 // and this allows us to see how task bar icons would work
282 [(NSView*)m_iconWindow->GetHandle() setFrame:NSMakeRect(0.0,0.0,wxMax(10,icon.GetWidth()),[[NSStatusBar systemStatusBar] thickness])];
283 [m_cocoaNSStatusItem setView:(NSView*)m_iconWindow->GetHandle()];
287 bool wxTaskBarIconCustomStatusItemImpl::RemoveIcon()
289 [m_cocoaNSStatusItem release];
290 m_cocoaNSStatusItem = nil;
296 bool wxTaskBarIconCustomStatusItemImpl::PopupMenu(wxMenu *menu)
299 menu->SetInvokingWindow(m_iconWindow);
302 if([m_cocoaNSStatusItem respondsToSelector:@selector(popUpStatusItemMenu:)])
304 [m_cocoaNSStatusItem popUpStatusItemMenu:(NSMenu*)menu->GetHMenu()];
307 { // pretty good fake for OS X < 10.3
308 NSEvent *nsevent = [NSEvent mouseEventWithType:NSLeftMouseDown
309 location:NSMakePoint(-1.0,-4.0) modifierFlags:0 timestamp:0
310 windowNumber:[[(NSView*)m_iconWindow->GetHandle() window] windowNumber]
311 context:[NSGraphicsContext currentContext]
312 eventNumber:0 clickCount:1 pressure:0.0];
313 [NSMenu popUpContextMenu:menu->GetHMenu() withEvent:nsevent forView:(NSView*)m_iconWindow->GetHandle()];
315 menu->SetInvokingWindow(NULL);
319 // ============================================================================
320 // wxTaskBarIconWindow
321 // ============================================================================
322 BEGIN_EVENT_TABLE(wxTaskBarIconWindow, wxWindow)
323 EVT_MENU(-1, wxTaskBarIconWindow::OnMenuEvent)
326 void wxTaskBarIconWindow::OnMenuEvent(wxCommandEvent &event)
328 m_taskBarIconImpl->GetTaskBarIcon()->ProcessEvent(event);
331 // ============================================================================
332 // wxTaskBarIconWindowCustom
333 // ============================================================================
334 BEGIN_EVENT_TABLE(wxTaskBarIconWindowCustom, wxTaskBarIconWindow)
335 EVT_MOUSE_EVENTS(wxTaskBarIconWindowCustom::OnMouseEvent)
336 EVT_PAINT(wxTaskBarIconWindowCustom::OnPaint)
339 void wxTaskBarIconWindowCustom::OnMouseEvent(wxMouseEvent &event)
341 wxEventType tbEventType = 0;
342 if(event.GetEventType() == wxEVT_MOTION)
343 tbEventType = wxEVT_TASKBAR_MOVE;
344 else if(event.GetEventType() == wxEVT_LEFT_DOWN)
345 tbEventType = wxEVT_TASKBAR_LEFT_DOWN;
346 else if(event.GetEventType() == wxEVT_LEFT_UP)
347 tbEventType = wxEVT_TASKBAR_LEFT_UP;
348 else if(event.GetEventType() == wxEVT_RIGHT_DOWN)
349 tbEventType = wxEVT_TASKBAR_RIGHT_DOWN;
350 else if(event.GetEventType() == wxEVT_RIGHT_UP)
351 tbEventType = wxEVT_TASKBAR_RIGHT_UP;
352 else if(event.GetEventType() == wxEVT_LEFT_DCLICK)
353 tbEventType = wxEVT_TASKBAR_LEFT_DCLICK;
354 else if(event.GetEventType() == wxEVT_RIGHT_DCLICK)
355 tbEventType = wxEVT_TASKBAR_RIGHT_DCLICK;
358 wxTaskBarIconEvent tbiEvent(tbEventType,m_taskBarIconImpl->GetTaskBarIcon());
359 m_taskBarIconImpl->GetTaskBarIcon()->ProcessEvent(tbiEvent);
362 void wxTaskBarIconWindowCustom::OnPaint(wxPaintEvent &WXUNUSED(event))
365 // FIXME: This is a temporary hack until we can see real icons
366 dc.SetBackground(wxBrush(*wxBLUE));
368 dc.DrawIcon(m_icon,0,0);
371 #endif //def wxHAS_TASK_BAR_ICON