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