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