]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/taskbar.mm
Applied Blit system options optimization to StretchBlit
[wxWidgets.git] / src / osx / cocoa / taskbar.mm
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////
2// File: src/osx/cocoa/taskbar.mm
3// Purpose: Implements wxTaskBarIcon class
4// Author: David Elliott, Stefan Csomor
5// Modified by:
6// Created: 2004/01/24
7// RCS-ID: $Id$
8// Copyright: (c) 2004 David Elliott, Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13#if wxUSE_TASKBARICON
14
15#ifndef WX_PRECOMP
16 #include "wx/toplevel.h"
17 #include "wx/menu.h"
18 #include "wx/icon.h"
19 #include "wx/log.h"
20 #include "wx/dcclient.h"
21#endif
22
23#include "wx/taskbar.h"
24
25#include "wx/osx/private.h"
26
27class wxTaskBarIconWindow;
28
29//-----------------------------------------------------------------------------
30//
31// wxTaskBarIconWindow
32//
33// Event handler for menus
34// NB: Since wxWindows in Mac HAVE to have parents we need this to be
35// a top level window...
36//-----------------------------------------------------------------------------
37
38class wxTaskBarIconWindow : public wxTopLevelWindow
39{
40public:
41 wxTaskBarIconWindow(wxTaskBarIconImpl *impl);
42
43 void OnMenuEvent(wxCommandEvent& event);
44 void OnUpdateUIEvent(wxUpdateUIEvent& event);
45
46private:
47 wxTaskBarIconImpl *m_impl;
48 DECLARE_EVENT_TABLE()
49};
50
51// ============================================================================
52// wxTaskBarIconImpl
53// Base class for the various Cocoa implementations.
54// ============================================================================
55class wxTaskBarIconImpl
56{
57public:
58 wxTaskBarIconImpl(wxTaskBarIcon *taskBarIcon);
59
60 virtual bool IsStatusItem() const { return false; }
61
62 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString) = 0;
63 virtual bool RemoveIcon() = 0;
64
65 bool IsIconInstalled() const { return m_icon.IsOk(); }
66
67 virtual bool PopupMenu(wxMenu *menu) = 0;
68 virtual ~wxTaskBarIconImpl();
69 inline wxTaskBarIcon* GetTaskBarIcon() { return m_taskBarIcon; }
70 wxMenu * CreatePopupMenu()
71 { return m_taskBarIcon->CreatePopupMenu(); }
72
73 wxDECLARE_NO_COPY_CLASS(wxTaskBarIconImpl);
74
75protected:
76 wxTaskBarIcon *m_taskBarIcon;
77 wxBitmap m_icon;
78 wxTaskBarIconWindow *m_eventWindow;
79private:
80 wxTaskBarIconImpl();
81};
82
83// ============================================================================
84// wxTaskBarIconDockImpl
85// An implementation using the Dock icon.
86// ============================================================================
87class wxTaskBarIconDockImpl: public wxTaskBarIconImpl
88{
89public:
90 wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon);
91 virtual ~wxTaskBarIconDockImpl();
92 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString);
93 virtual bool RemoveIcon();
94 virtual bool PopupMenu(wxMenu *menu);
95
96 static WX_NSMenu OSXGetDockHMenu();
97protected:
98 WX_NSMenu OSXDoGetDockHMenu();
99 // There can be only one Dock icon, so make sure we keep it that way
100 static wxTaskBarIconDockImpl *sm_dockIcon;
101private:
102 wxTaskBarIconDockImpl();
103 wxMenu *m_pMenu;
104};
105
106class wxTaskBarIconCustomStatusItemImpl;
107
108@interface wxOSXStatusItemTarget : NSObject
109{
110 wxTaskBarIconCustomStatusItemImpl* impl;
111}
112@end
113
114// ============================================================================
115// wxTaskBarIconCustomStatusItemImpl
116// An implementation using an NSStatusItem with a custom NSView
117// ============================================================================
118class wxTaskBarIconCustomStatusItemImpl: public wxTaskBarIconImpl
119{
120public:
121 wxTaskBarIconCustomStatusItemImpl(wxTaskBarIcon *taskBarIcon);
122 virtual ~wxTaskBarIconCustomStatusItemImpl();
123
124 virtual bool IsStatusItem() const { return true; }
125
126 virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip = wxEmptyString);
127 virtual bool RemoveIcon();
128 virtual bool PopupMenu(wxMenu *menu);
129protected:
130 NSStatusItem *m_statusItem;
131 wxOSXStatusItemTarget *m_target;
132private:
133 wxTaskBarIconCustomStatusItemImpl();
134};
135
136// ============================================================================
137// wxTaskBarIcon implementation
138// The facade class.
139// ============================================================================
140IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
141
142wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType iconType)
143{
144 if(iconType == DOCK)
145 m_impl = new wxTaskBarIconDockImpl(this);
146 else if(iconType == CUSTOM_STATUSITEM)
147 m_impl = new wxTaskBarIconCustomStatusItemImpl(this);
148 else
149 { m_impl = NULL;
150 wxFAIL_MSG(wxT("Invalid wxTaskBarIcon type"));
151 }
152}
153
154wxTaskBarIcon::~wxTaskBarIcon()
155{
156 if ( m_impl )
157 {
158 if ( m_impl->IsIconInstalled() )
159 m_impl->RemoveIcon();
160 delete m_impl;
161 m_impl = NULL;
162 }
163}
164
165bool wxTaskBarIcon::OSXIsStatusItem()
166{
167 if ( m_impl )
168 return m_impl->IsStatusItem();
169
170 return false;
171}
172
173// Operations
174
175bool wxTaskBarIcon::IsIconInstalled() const
176{
177 if ( m_impl )
178 return m_impl->IsIconInstalled();
179
180 return false;
181}
182
183bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
184{
185 if ( m_impl )
186 return m_impl->SetIcon(icon,tooltip);
187
188 return false;
189}
190
191bool wxTaskBarIcon::RemoveIcon()
192{
193 if ( m_impl )
194 return m_impl->RemoveIcon();
195
196 return false;
197}
198
199bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
200{
201 if ( m_impl )
202 return m_impl->PopupMenu(menu);
203
204 return false;
205}
206
207// ============================================================================
208// wxTaskBarIconImpl
209// ============================================================================
210
211wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* taskBarIcon)
212 : m_taskBarIcon(taskBarIcon), m_eventWindow(new wxTaskBarIconWindow(this))
213{
214}
215
216wxTaskBarIconImpl::~wxTaskBarIconImpl()
217{
218 delete m_eventWindow;
219}
220
221// ============================================================================
222// wxTaskBarIconDockImpl
223// ============================================================================
224wxTaskBarIconDockImpl *wxTaskBarIconDockImpl::sm_dockIcon = NULL;
225
226wxTaskBarIconDockImpl::wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon)
227: wxTaskBarIconImpl(taskBarIcon)
228{
229 wxASSERT_MSG(!sm_dockIcon, wxT("You should never have more than one dock icon!"));
230 sm_dockIcon = this;
231 m_pMenu = NULL;
232}
233
234wxTaskBarIconDockImpl::~wxTaskBarIconDockImpl()
235{
236 if(sm_dockIcon == this)
237 sm_dockIcon = NULL;
238}
239
240WX_NSMenu wxTaskBarIconDockImpl::OSXGetDockHMenu()
241{
242 if(sm_dockIcon)
243 return sm_dockIcon->OSXDoGetDockHMenu();
244
245 return nil;
246}
247
248WX_NSMenu wxTaskBarIconDockImpl::OSXDoGetDockHMenu()
249{
250 wxMenu *dockMenu = CreatePopupMenu();
251
252 if(!dockMenu)
253 return nil;
254
255 wxDELETE(m_pMenu);
256
257 m_pMenu = dockMenu;
258
259 m_pMenu->SetInvokingWindow(m_eventWindow);
260
261 m_pMenu->UpdateUI();
262
263 return (WX_NSMenu)dockMenu->GetHMenu();
264}
265
266bool wxTaskBarIconDockImpl::SetIcon(const wxIcon& icon, const wxString& WXUNUSED(tooltip))
267{
268 m_icon.CopyFromIcon(icon);
269 [[NSApplication sharedApplication] setApplicationIconImage:m_icon.GetNSImage()];
270 return true;
271}
272
273bool wxTaskBarIconDockImpl::RemoveIcon()
274{
275 wxDELETE(m_pMenu);
276 m_icon = wxBitmap();
277 [[NSApplication sharedApplication] setApplicationIconImage:nil];
278 return true;
279}
280
281bool wxTaskBarIconDockImpl::PopupMenu(wxMenu *WXUNUSED(menu))
282{
283 wxFAIL_MSG(wxT("You cannot force the Dock icon menu to popup"));
284 return false;
285}
286
287@interface wxNSAppController(wxTaskBarIconNSApplicationDelegateCategory)
288- (NSMenu*)applicationDockMenu:(NSApplication *)sender;
289@end
290
291@implementation wxNSAppController(wxTaskBarIconNSApplicationDelegateCategory)
292- (NSMenu*)applicationDockMenu:(NSApplication *)sender
293{
294 wxUnusedVar(sender);
295
296 return wxTaskBarIconDockImpl::OSXGetDockHMenu();
297}
298@end
299
300// ============================================================================
301// wxTaskBarIconCustomStatusItemImpl
302// ============================================================================
303
304@implementation wxOSXStatusItemTarget
305
306- (void) clickedAction: (id) sender
307{
308 wxUnusedVar(sender);
309 wxMenu *menu = impl->CreatePopupMenu();
310 if (menu)
311 {
312 impl->PopupMenu(menu);
313 delete menu;
314 }
315}
316
317- (void)setImplementation: (wxTaskBarIconCustomStatusItemImpl *) theImplementation
318{
319 impl = theImplementation;
320}
321
322- (wxTaskBarIconCustomStatusItemImpl*) implementation
323{
324 return impl;
325}
326
327@end
328
329
330wxTaskBarIconCustomStatusItemImpl::wxTaskBarIconCustomStatusItemImpl(wxTaskBarIcon *taskBarIcon)
331: wxTaskBarIconImpl(taskBarIcon)
332{
333 m_statusItem = nil;
334 m_target = nil;
335}
336
337wxTaskBarIconCustomStatusItemImpl::~wxTaskBarIconCustomStatusItemImpl()
338{
339}
340
341bool wxTaskBarIconCustomStatusItemImpl::SetIcon(const wxIcon& icon, const wxString& WXUNUSED(tooltip))
342{
343 if(!m_statusItem)
344 {
345 m_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
346 [m_statusItem retain];
347
348 m_target = [[wxOSXStatusItemTarget alloc] init];
349 [m_target setImplementation:this];
350 [m_statusItem setTarget:m_target];
351 [m_statusItem setAction:@selector(clickedAction:)];
352 [m_statusItem sendActionOn:NSLeftMouseDownMask];
353 }
354
355 m_icon.CopyFromIcon(icon);
356
357 // status item doesn't scale automatically
358
359 int dimension = m_icon.GetHeight();
360 if ( m_icon.GetWidth() > dimension )
361 dimension = m_icon.GetWidth();
362 if ( dimension > 16 )
363 {
364 wxImage img = m_icon.ConvertToImage();
365 int factor = (dimension+15)/16;
366 m_icon = img.ShrinkBy(factor, factor);
367 }
368
369 [m_statusItem setImage:m_icon.GetNSImage()];
370 return true;
371}
372
373bool wxTaskBarIconCustomStatusItemImpl::RemoveIcon()
374{
375 [m_statusItem release];
376 m_statusItem = nil;
377 [m_target release];
378 m_target = nil;
379
380 m_icon = wxBitmap();
381
382 return true;
383}
384
385bool wxTaskBarIconCustomStatusItemImpl::PopupMenu(wxMenu *menu)
386{
387 wxASSERT(menu);
388
389 menu->SetInvokingWindow(m_eventWindow);
390 menu->UpdateUI();
391
392 [m_statusItem popUpStatusItemMenu:(NSMenu*)menu->GetHMenu()];
393
394 menu->SetInvokingWindow(NULL);
395 return true;
396}
397
398// ============================================================================
399// wxTaskBarIconWindow
400// ============================================================================
401
402BEGIN_EVENT_TABLE(wxTaskBarIconWindow, wxWindow)
403EVT_MENU(-1, wxTaskBarIconWindow::OnMenuEvent)
404EVT_UPDATE_UI(-1, wxTaskBarIconWindow::OnUpdateUIEvent)
405END_EVENT_TABLE()
406
407wxTaskBarIconWindow::wxTaskBarIconWindow(wxTaskBarIconImpl *impl)
408: m_impl(impl)
409{
410}
411
412void wxTaskBarIconWindow::OnMenuEvent(wxCommandEvent& event)
413{
414 m_impl->GetTaskBarIcon()->ProcessEvent(event);
415}
416
417void wxTaskBarIconWindow::OnUpdateUIEvent(wxUpdateUIEvent& event)
418{
419 m_impl->GetTaskBarIcon()->ProcessEvent(event);
420}
421
422#endif //def wxHAS_TASK_BAR_ICON