]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/taskbar.cpp | |
3 | // Purpose: wxTaskBarIcon | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 09/25/2004 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2004 Ryan Norton | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef wxHAS_TASK_BAR_ICON | |
15 | ||
16 | #include "wx/mac/private.h" | |
17 | ||
18 | #include "wx/taskbar.h" | |
19 | #include "wx/menu.h" | |
20 | #include "wx/icon.h" | |
21 | #include "wx/dcmemory.h" | |
22 | ||
23 | ||
24 | class wxTaskBarIconImpl | |
25 | { | |
26 | public: | |
27 | wxTaskBarIconImpl(wxTaskBarIcon* parent); | |
28 | virtual ~wxTaskBarIconImpl(); | |
29 | ||
30 | virtual bool IsIconInstalled() const = 0; | |
31 | virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip) = 0; | |
32 | virtual bool RemoveIcon() = 0; | |
33 | virtual bool PopupMenu(wxMenu *menu) = 0; | |
34 | ||
35 | wxMenu * CreatePopupMenu() | |
36 | { return m_parent->CreatePopupMenu(); } | |
37 | ||
38 | wxTaskBarIcon *m_parent; | |
39 | class wxTaskBarIconWindow *m_menuEventWindow; | |
40 | ||
41 | DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl) | |
42 | }; | |
43 | ||
44 | //----------------------------------------------------------------------------- | |
45 | // | |
46 | // wxTaskBarIconWindow | |
47 | // | |
48 | // Event handler for menus | |
49 | // NB: Since wxWindows in Mac HAVE to have parents we need this to be | |
50 | // a top level window... | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | class wxTaskBarIconWindow : public wxTopLevelWindow | |
54 | { | |
55 | public: | |
56 | wxTaskBarIconWindow(wxTaskBarIconImpl *impl) | |
57 | : wxTopLevelWindow(NULL, -1, wxT("")), m_impl(impl) | |
58 | { | |
59 | Connect( | |
60 | -1, wxEVT_COMMAND_MENU_SELECTED, | |
61 | wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent) ); | |
62 | } | |
63 | ||
64 | void OnMenuEvent(wxCommandEvent& event) | |
65 | { | |
66 | m_impl->m_parent->ProcessEvent(event); | |
67 | } | |
68 | ||
69 | private: | |
70 | wxTaskBarIconImpl *m_impl; | |
71 | }; | |
72 | ||
73 | class wxDockTaskBarIcon : public wxTaskBarIconImpl | |
74 | { | |
75 | public: | |
76 | wxDockTaskBarIcon(wxTaskBarIcon* parent); | |
77 | virtual ~wxDockTaskBarIcon(); | |
78 | ||
79 | virtual bool IsIconInstalled() const; | |
80 | virtual bool SetIcon(const wxIcon& icon, const wxString& tooltip); | |
81 | virtual bool RemoveIcon(); | |
82 | virtual bool PopupMenu(wxMenu *menu); | |
83 | ||
84 | wxMenu* DoCreatePopupMenu(); | |
85 | ||
86 | EventHandlerRef m_eventHandlerRef; | |
87 | EventHandlerUPP m_eventupp; | |
88 | wxWindow *m_eventWindow; | |
89 | wxMenu *m_pMenu; | |
90 | MenuRef m_theLastMenu; | |
91 | bool m_iconAdded; | |
92 | }; | |
93 | ||
94 | // Forward declarations for utility functions for dock implementation | |
95 | pascal OSStatus wxDockEventHandler( | |
96 | EventHandlerCallRef inHandlerCallRef, | |
97 | EventRef inEvent, void* pData ); | |
98 | wxMenu * wxDeepCopyMenu( wxMenu *menu ); | |
99 | ||
100 | ||
101 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
102 | // | |
103 | // wxTaskBarIconImpl | |
104 | // | |
105 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
106 | ||
107 | wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* parent) | |
108 | : m_parent(parent), m_menuEventWindow(new wxTaskBarIconWindow(this)) | |
109 | { | |
110 | } | |
111 | ||
112 | wxTaskBarIconImpl::~wxTaskBarIconImpl() | |
113 | { | |
114 | delete m_menuEventWindow; | |
115 | } | |
116 | ||
117 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
118 | // | |
119 | // wxDockTaskBarIcon | |
120 | // | |
121 | // OS X Dock implementation of wxTaskBarIcon using Carbon | |
122 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
123 | ||
124 | //----------------------------------------------------------------------------- | |
125 | // wxDockEventHandler | |
126 | // | |
127 | // This is the global Mac/Carbon event handler for the dock. | |
128 | // We need this for two reasons: | |
129 | // 1) To handle wxTaskBarIcon menu events (see below for why) | |
130 | // 2) To handle events from the dock when it requests a menu | |
131 | //----------------------------------------------------------------------------- | |
132 | pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef, | |
133 | EventRef inEvent, void *pData ) | |
134 | { | |
135 | // Get the parameters we want from the event | |
136 | wxDockTaskBarIcon* pTB = (wxDockTaskBarIcon*) pData; | |
137 | const UInt32 eventClass = GetEventClass(inEvent); | |
138 | const UInt32 eventKind = GetEventKind(inEvent); | |
139 | ||
140 | // Handle wxTaskBar menu events (note that this is a global event handler | |
141 | // so it will actually get called by all commands/menus) | |
142 | if ((eventClass == kEventClassCommand) && (eventKind == kEventCommandProcess)) | |
143 | { | |
144 | // if we have no taskbar menu quickly pass it back to wxApp | |
145 | if (pTB->m_pMenu == NULL) | |
146 | return eventNotHandledErr; | |
147 | ||
148 | // This is the real reason why we need this. Normally menus | |
149 | // get handled in wxMacAppEventHandler | |
150 | // | |
151 | // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler, | |
152 | // EventRef event, void *data) | |
153 | // | |
154 | // However, in the case of a taskbar menu call | |
155 | // command.menu.menuRef IS NULL! | |
156 | // Which causes the wxApp handler just to skip it. | |
157 | MenuRef taskbarMenuRef = MAC_WXHMENU(pTB->m_pMenu->GetHMenu()); | |
158 | OSStatus err; | |
159 | ||
160 | // get the HICommand from the event | |
161 | HICommand command; | |
162 | err = GetEventParameter( | |
163 | inEvent, kEventParamDirectObject, | |
164 | typeHICommand, NULL, | |
165 | sizeof(HICommand), NULL, &command ); | |
166 | if (err == noErr) | |
167 | { | |
168 | // Obtain the REAL menuRef and the menuItemIndex in the real menuRef | |
169 | // | |
170 | // NOTE: menuRef is generally used here for submenus, as | |
171 | // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass | |
172 | // just the top level wxTaskBar menu | |
173 | MenuItemIndex menuItemIndex; | |
174 | MenuRef menuRef; | |
175 | ||
176 | err = GetIndMenuItemWithCommandID( | |
177 | taskbarMenuRef, | |
178 | command.commandID, | |
179 | 1, &menuRef, &menuItemIndex ); | |
180 | if (err == noErr) | |
181 | { | |
182 | MenuCommand id = command.commandID; | |
183 | wxMenuItem *item = NULL; | |
184 | ||
185 | if (id != 0) // get the wxMenuItem reference from the MenuRef | |
186 | GetMenuItemRefCon( menuRef, menuItemIndex, (UInt32*) &item ); | |
187 | ||
188 | if (item) | |
189 | { | |
190 | // Handle items that are checkable | |
191 | // FIXME: Doesn't work (at least on 10.2)! | |
192 | if (item->IsCheckable()) | |
193 | item->Check( !item->IsChecked() ); | |
194 | ||
195 | // send the wxEvent to the wxMenu | |
196 | item->GetMenu()->SendEvent( id, item->IsCheckable() ? item->IsChecked() : -1 ); | |
197 | ||
198 | // successfully handled the event | |
199 | err = noErr; | |
200 | } | |
201 | } | |
202 | } //end if noErr on getting HICommand from event | |
203 | ||
204 | // return whether we handled the event or not | |
205 | return err; | |
206 | } | |
207 | ||
208 | // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here, | |
209 | // otherwise something is truly funky | |
210 | wxASSERT(eventClass == kEventClassApplication && | |
211 | eventKind == kEventAppGetDockTileMenu); | |
212 | ||
213 | // process the right click events | |
214 | // NB: This may result in double or even triple-creation of the menus | |
215 | // We need to do this for 2.4 compat, however | |
216 | wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN, NULL); | |
217 | pTB->m_parent->ProcessEvent(downevt); | |
218 | ||
219 | wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP, NULL); | |
220 | pTB->m_parent->ProcessEvent(upevt); | |
221 | ||
222 | // create popup menu | |
223 | wxMenu* menu = pTB->DoCreatePopupMenu(); | |
224 | ||
225 | OSStatus err = eventNotHandledErr; | |
226 | ||
227 | if (menu != NULL) | |
228 | { | |
229 | // note to self - a MenuRef *is* a MenuHandle | |
230 | MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu()); | |
231 | ||
232 | // When SetEventParameter is called it will decrement | |
233 | // the reference count of the menu - we need to make | |
234 | // sure it stays around in the wxMenu class here | |
235 | RetainMenu(hMenu); | |
236 | ||
237 | // set the actual dock menu | |
238 | err = SetEventParameter( | |
239 | inEvent, kEventParamMenuRef, | |
240 | typeMenuRef, sizeof(MenuRef), &hMenu ); | |
241 | verify_noerr( err ); | |
242 | } | |
243 | ||
244 | return err; | |
245 | } | |
246 | ||
247 | //----------------------------------------------------------------------------- | |
248 | // wxDeepCopyMenu | |
249 | // | |
250 | // Performs a top-to-bottom copy of the input menu and all of its | |
251 | // submenus. | |
252 | // | |
253 | // This is mostly needed for 2.4 compatability. However wxPython and others | |
254 | // still use this way of setting the taskbarmenu. | |
255 | //----------------------------------------------------------------------------- | |
256 | wxMenu * wxDeepCopyMenu( wxMenu *menu ) | |
257 | { | |
258 | if (menu == NULL) | |
259 | return NULL; | |
260 | ||
261 | // NB: Here we have to perform a deep copy of the menu, | |
262 | // copying each and every menu item from menu to m_pMenu. | |
263 | // Other implementations use wxWindow::PopupMenu here, | |
264 | // which idle execution until the user selects something, | |
265 | // but since the Mac handles this internally, we can't - | |
266 | // and have no way at all to idle it while the dock menu | |
267 | // is being shown before menu goes out of scope (it may | |
268 | // not be on the heap, and may expire right after this function | |
269 | // is done - we need it to last until the carbon event is triggered - | |
270 | // that's when the user right clicks). | |
271 | // | |
272 | // Also, since there is no equal (assignment) operator | |
273 | // on either wxMenu or wxMenuItem, we have to do all the | |
274 | // dirty work ourselves. | |
275 | ||
276 | // perform a deep copy of the menu | |
277 | wxMenuItemList& theList = menu->GetMenuItems(); | |
278 | wxMenuItemList::compatibility_iterator theNode = theList.GetFirst(); | |
279 | ||
280 | // create the main menu | |
281 | wxMenu *m_pMenu = new wxMenu(menu->GetTitle()); | |
282 | ||
283 | while (theNode != NULL) | |
284 | { | |
285 | wxMenuItem* theItem = theNode->GetData(); | |
286 | m_pMenu->Append( | |
287 | new wxMenuItem( | |
288 | m_pMenu, // parent menu | |
289 | theItem->GetId(), // id | |
290 | theItem->GetText(), // text label | |
291 | theItem->GetHelp(), // status bar help string | |
292 | theItem->GetKind(), // menu flags - checkable, separator, etc. | |
293 | wxDeepCopyMenu(theItem->GetSubMenu()) )); // submenu | |
294 | ||
295 | theNode = theNode->GetNext(); | |
296 | } | |
297 | ||
298 | return m_pMenu; | |
299 | } | |
300 | ||
301 | //----------------------------------------------------------------------------- | |
302 | // wxDockTaskBarIcon ctor | |
303 | // | |
304 | // Initializes the dock implementation of wxTaskBarIcon. | |
305 | // | |
306 | // Here we create some Mac-specific event handlers and UPPs. | |
307 | //----------------------------------------------------------------------------- | |
308 | wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon* parent) | |
309 | : wxTaskBarIconImpl(parent), | |
310 | m_eventHandlerRef(NULL), m_pMenu(NULL), | |
311 | m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false) | |
312 | { | |
313 | // register the events that will return the dock menu | |
314 | EventTypeSpec tbEventList[] = | |
315 | { | |
316 | { kEventClassCommand, kEventProcessCommand }, | |
317 | { kEventClassApplication, kEventAppGetDockTileMenu } | |
318 | }; | |
319 | ||
320 | m_eventupp = NewEventHandlerUPP(wxDockEventHandler); | |
321 | wxASSERT(m_eventupp != NULL); | |
322 | ||
323 | OSStatus err = InstallApplicationEventHandler( | |
324 | m_eventupp, | |
325 | GetEventTypeCount(tbEventList), tbEventList, | |
326 | this, &m_eventHandlerRef); | |
327 | verify_noerr( err ); | |
328 | } | |
329 | ||
330 | //----------------------------------------------------------------------------- | |
331 | // wxDockTaskBarIcon Destructor | |
332 | // | |
333 | // Cleans up mac events and restores the old icon to the dock | |
334 | //----------------------------------------------------------------------------- | |
335 | wxDockTaskBarIcon::~wxDockTaskBarIcon() | |
336 | { | |
337 | // clean up event handler and event UPP | |
338 | RemoveEventHandler(m_eventHandlerRef); | |
339 | DisposeEventHandlerUPP(m_eventupp); | |
340 | ||
341 | // restore old icon and menu to the dock | |
342 | RemoveIcon(); | |
343 | } | |
344 | ||
345 | //----------------------------------------------------------------------------- | |
346 | // wxDockTaskBarIcon::DoCreatePopupMenu | |
347 | // | |
348 | // Helper function that handles a request from the dock event handler | |
349 | // to get the menu for the dock | |
350 | //----------------------------------------------------------------------------- | |
351 | wxMenu * wxDockTaskBarIcon::DoCreatePopupMenu() | |
352 | { | |
353 | // get the menu from the parent | |
354 | wxMenu* theNewMenu = CreatePopupMenu(); | |
355 | ||
356 | if (theNewMenu) | |
357 | { | |
358 | if (m_pMenu) | |
359 | delete m_pMenu; | |
360 | m_pMenu = theNewMenu; | |
361 | m_pMenu->SetInvokingWindow(m_menuEventWindow); | |
362 | } | |
363 | ||
364 | // the return here can be one of three things | |
365 | // (in order of priority): | |
366 | // 1) User passed a menu from CreatePopupMenu override | |
367 | // 2) menu sent to and copied from PopupMenu | |
368 | // 3) If neither (1) or (2), then NULL | |
369 | // | |
370 | return m_pMenu; | |
371 | } | |
372 | ||
373 | //----------------------------------------------------------------------------- | |
374 | // wxDockTaskBarIcon::IsIconInstalled | |
375 | // | |
376 | // Returns whether or not the dock is not using the default image | |
377 | //----------------------------------------------------------------------------- | |
378 | bool wxDockTaskBarIcon::IsIconInstalled() const | |
379 | { | |
380 | return m_iconAdded; | |
381 | } | |
382 | ||
383 | //----------------------------------------------------------------------------- | |
384 | // wxDockTaskBarIcon::SetIcon | |
385 | // | |
386 | // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage | |
387 | //----------------------------------------------------------------------------- | |
388 | bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) | |
389 | { | |
390 | // convert the wxIcon into a wxBitmap so we can perform some | |
391 | // wxBitmap operations with it | |
392 | wxBitmap bmp( icon ); | |
393 | wxASSERT( bmp.Ok() ); | |
394 | ||
395 | // get the CGImageRef for the wxBitmap: | |
396 | // OSX builds only, but then the dock only exists in OSX | |
397 | CGImageRef pImage = (CGImageRef) bmp.CGImageCreate(); | |
398 | wxASSERT( pImage != NULL ); | |
399 | ||
400 | // actually set the dock image | |
401 | OSStatus err = SetApplicationDockTileImage( pImage ); | |
402 | verify_noerr( err ); | |
403 | ||
404 | // free the CGImage, now that it's referenced by the dock | |
405 | if (pImage != NULL) | |
406 | CGImageRelease( pImage ); | |
407 | ||
408 | bool success = (err == noErr); | |
409 | m_iconAdded = success; | |
410 | ||
411 | return success; | |
412 | } | |
413 | ||
414 | //----------------------------------------------------------------------------- | |
415 | // wxDockTaskBarIcon::RemoveIcon | |
416 | // | |
417 | // Restores the old image for the dock via RestoreApplicationDockTileImage | |
418 | //----------------------------------------------------------------------------- | |
419 | bool wxDockTaskBarIcon::RemoveIcon() | |
420 | { | |
421 | if (m_pMenu) | |
422 | { | |
423 | delete m_pMenu; | |
424 | m_pMenu = NULL; | |
425 | } | |
426 | ||
427 | // restore old icon to the dock | |
428 | OSStatus err = RestoreApplicationDockTileImage(); | |
429 | verify_noerr( err ); | |
430 | ||
431 | // restore the old menu to the dock | |
432 | SetApplicationDockTileMenu( m_theLastMenu ); | |
433 | ||
434 | bool success = (err == noErr); | |
435 | m_iconAdded = !success; | |
436 | ||
437 | return success; | |
438 | } | |
439 | ||
440 | //----------------------------------------------------------------------------- | |
441 | // wxDockTaskBarIcon::PopupMenu | |
442 | // | |
443 | // 2.4 and wxPython method that "pops of the menu in the taskbar". | |
444 | // | |
445 | // In reality because of the way the dock menu works in carbon | |
446 | // we just save the menu, and if the user didn't override CreatePopupMenu | |
447 | // return the menu passed here, thus sort of getting the same effect. | |
448 | //----------------------------------------------------------------------------- | |
449 | bool wxDockTaskBarIcon::PopupMenu(wxMenu *menu) | |
450 | { | |
451 | wxASSERT(menu != NULL); | |
452 | ||
453 | if (m_pMenu) | |
454 | delete m_pMenu; | |
455 | ||
456 | // start copy of menu | |
457 | m_pMenu = wxDeepCopyMenu(menu); | |
458 | ||
459 | // finish up | |
460 | m_pMenu->SetInvokingWindow(m_menuEventWindow); | |
461 | ||
462 | return true; | |
463 | } | |
464 | ||
465 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
466 | // | |
467 | // wxTaskBarIcon | |
468 | // | |
469 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
470 | ||
471 | IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) | |
472 | ||
473 | //----------------------------------------------------------------------------- | |
474 | // wxTaskBarIcon Constructor | |
475 | // | |
476 | // Creates the backend | |
477 | // | |
478 | // Note that we only support DOCK currently as others require cocoa and | |
479 | // also some require hacks and other such things. (MenuExtras are | |
480 | // actually seperate programs that also require a special undocumented id | |
481 | // hack and other such fun stuff). | |
482 | //----------------------------------------------------------------------------- | |
483 | wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType) | |
484 | { | |
485 | wxASSERT_MSG( | |
486 | nType == DOCK, | |
487 | wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") ); | |
488 | ||
489 | m_impl = new wxDockTaskBarIcon(this); | |
490 | } | |
491 | ||
492 | //----------------------------------------------------------------------------- | |
493 | // wxTaskBarIcon Destructor | |
494 | // | |
495 | // Destroys the backend | |
496 | //----------------------------------------------------------------------------- | |
497 | wxTaskBarIcon::~wxTaskBarIcon() | |
498 | { | |
499 | delete m_impl; | |
500 | } | |
501 | ||
502 | //----------------------------------------------------------------------------- | |
503 | // wxTaskBarIcon::SetIcon | |
504 | // wxTaskBarIcon::RemoveIcon | |
505 | // wxTaskBarIcon::PopupMenu | |
506 | // | |
507 | // Just calls the backend version of the said function. | |
508 | //----------------------------------------------------------------------------- | |
509 | bool wxTaskBarIcon::IsIconInstalled() const | |
510 | { return m_impl->IsIconInstalled(); } | |
511 | ||
512 | bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) | |
513 | { return m_impl->SetIcon(icon, tooltip); } | |
514 | ||
515 | bool wxTaskBarIcon::RemoveIcon() | |
516 | { return m_impl->RemoveIcon(); } | |
517 | ||
518 | bool wxTaskBarIcon::PopupMenu(wxMenu *menu) | |
519 | { return m_impl->PopupMenu(menu); } | |
520 | ||
521 | #endif // wxHAS_TASK_BAR_ICON |