X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f9f9c8fcf9ab9446bbb14614dab97fe6c2857e63..0c1cc9483ecba053bc9a0983a4a8d48898e334f2:/src/mac/carbon/taskbar.cpp diff --git a/src/mac/carbon/taskbar.cpp b/src/mac/carbon/taskbar.cpp index eb27f186d6..9bb02f5124 100644 --- a/src/mac/carbon/taskbar.cpp +++ b/src/mac/carbon/taskbar.cpp @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: taskbar.cpp -// Purpose: wxTaskBarIcon - OSX implementation +// Name: src/mac/carbon/taskbar.cpp +// Purpose: wxTaskBarIcon // Author: Ryan Norton // Modified by: // Created: 09/25/2004 @@ -9,33 +9,22 @@ // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// -//============================================================================= -// Declarations -//============================================================================= - -//----------------------------------------------------------------------------- -// Includes -//----------------------------------------------------------------------------- - #include "wx/wxprec.h" -#ifdef wxHAS_TASK_BAR_ICON - -#include "wx/mac/private.h" +#if wxUSE_TASKBARICON #include "wx/taskbar.h" -#include "wx/menu.h" -#include "wx/icon.h" -#include "wx/dcmemory.h" -//----------------------------------------------------------------------------- -// -// wxTaskBarIconImpl -// -// Superclass of wxTaskBarIcon implementations -//----------------------------------------------------------------------------- +#ifndef WX_PRECOMP + #include "wx/dcmemory.h" + #include "wx/menu.h" + #include "wx/toplevel.h" + #include "wx/icon.h" +#endif -class wxTaskBarIconImpl +#include "wx/mac/private.h" + +class wxTaskBarIconImpl { public: wxTaskBarIconImpl(wxTaskBarIcon* parent); @@ -46,11 +35,11 @@ public: virtual bool RemoveIcon() = 0; virtual bool PopupMenu(wxMenu *menu) = 0; - wxMenu* CreatePopupMenu() + wxMenu * CreatePopupMenu() { return m_parent->CreatePopupMenu(); } - wxTaskBarIcon* m_parent; - class wxTaskBarIconWindow* m_menuEventWindow; + wxTaskBarIcon *m_parent; + class wxTaskBarIconWindow *m_menuEventWindow; DECLARE_NO_COPY_CLASS(wxTaskBarIconImpl) }; @@ -60,36 +49,30 @@ public: // wxTaskBarIconWindow // // Event handler for menus -// NB: Since wxWindows in mac HAVE to have parents we need this to be +// NB: Since wxWindows in Mac HAVE to have parents we need this to be // a top level window... //----------------------------------------------------------------------------- class wxTaskBarIconWindow : public wxTopLevelWindow { public: - wxTaskBarIconWindow(wxTaskBarIconImpl* impl) - : wxTopLevelWindow(NULL, -1, wxT("")), m_impl(impl) + wxTaskBarIconWindow(wxTaskBarIconImpl *impl) + : wxTopLevelWindow(NULL, wxID_ANY, wxEmptyString), m_impl(impl) { - Connect(-1, wxEVT_COMMAND_MENU_SELECTED, - wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent) - ); + Connect( + -1, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxTaskBarIconWindow::OnMenuEvent) ); } - void OnMenuEvent(wxCommandEvent& event) + void OnMenuEvent(wxCommandEvent& event) { m_impl->m_parent->ProcessEvent(event); } private: - wxTaskBarIconImpl* m_impl; + wxTaskBarIconImpl *m_impl; }; -//----------------------------------------------------------------------------- -// -// wxDockBarIconImpl -// -//----------------------------------------------------------------------------- - class wxDockTaskBarIcon : public wxTaskBarIconImpl { public: @@ -105,23 +88,18 @@ public: EventHandlerRef m_eventHandlerRef; EventHandlerUPP m_eventupp; - wxWindow* m_eventWindow; - wxMenu* m_pMenu; + wxWindow *m_eventWindow; + wxMenu *m_pMenu; MenuRef m_theLastMenu; bool m_iconAdded; }; // Forward declarations for utility functions for dock implementation -pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef, - EventRef inEvent, void* pData); -wxMenu* wxDeepCopyMenu(wxMenu* menu); - +pascal OSStatus wxDockEventHandler( + EventHandlerCallRef inHandlerCallRef, + EventRef inEvent, void* pData ); +wxMenu * wxDeepCopyMenu( wxMenu *menu ); -//============================================================================= -// -// Implementation -// -//============================================================================= //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // @@ -129,21 +107,11 @@ wxMenu* wxDeepCopyMenu(wxMenu* menu); // //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//----------------------------------------------------------------------------- -// wxTaskBarIconImpl Constructor -// -// Initializes members and creates the event window -//----------------------------------------------------------------------------- -wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* parent) +wxTaskBarIconImpl::wxTaskBarIconImpl(wxTaskBarIcon* parent) : m_parent(parent), m_menuEventWindow(new wxTaskBarIconWindow(this)) { } -//----------------------------------------------------------------------------- -// wxTaskBarIconImpl Destructor -// -// Cleans up the event window -//----------------------------------------------------------------------------- wxTaskBarIconImpl::~wxTaskBarIconImpl() { delete m_menuEventWindow; @@ -153,136 +121,115 @@ wxTaskBarIconImpl::~wxTaskBarIconImpl() // // wxDockTaskBarIcon // -// OS X DOCK implementation of wxTaskBarIcon using carbon +// OS X Dock implementation of wxTaskBarIcon using Carbon //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +//----------------------------------------------------------------------------- // wxDockEventHandler // -// This is the global mac/carbon event handler for the dock. +// This is the global Mac/Carbon event handler for the dock. // We need this for two reasons: // 1) To handle wxTaskBarIcon menu events (see below for why) // 2) To handle events from the dock when it requests a menu -//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef, - EventRef inEvent, void* pData) +//----------------------------------------------------------------------------- +pascal OSStatus +wxDockEventHandler(EventHandlerCallRef WXUNUSED(inHandlerCallRef), + EventRef inEvent, + void *pData) { // Get the parameters we want from the event wxDockTaskBarIcon* pTB = (wxDockTaskBarIcon*) pData; const UInt32 eventClass = GetEventClass(inEvent); const UInt32 eventKind = GetEventKind(inEvent); + + OSStatus err = eventNotHandledErr; // Handle wxTaskBar menu events (note that this is a global event handler // so it will actually get called by all commands/menus) - // - if ((eventClass == kEventClassCommand) && (eventKind == kEventCommandProcess)) + if ((eventClass == kEventClassCommand) && (eventKind == kEventCommandProcess || eventKind == kEventCommandUpdateStatus )) { // if we have no taskbar menu quickly pass it back to wxApp - if (pTB->m_pMenu == NULL) - return eventNotHandledErr; - - // - // This is the real reason why we need this. Normally menus - // get handled in wxMacAppEventHandler - // - // pascal OSStatus wxMacAppEventHandler(EventHandlerCallRef handler, - // EventRef event, void *data) - // - // However, in the case of a taskbar menu call - // command.menu.menuRef IS NULL! - // Which causes the wxApp handler just to skip it. - // - MenuRef taskbarMenuRef = MAC_WXHMENU(pTB->m_pMenu->GetHMenu()); - OSStatus result = eventNotHandledErr; - OSErr err; - - // get the HICommand from the event - HICommand command; - err = GetEventParameter(inEvent, kEventParamDirectObject, - typeHICommand, NULL, - sizeof(HICommand), NULL, &command); - if (err == noErr) - { - // Obtain the REAL menuRef and the menuItemIndex in the real menuRef - // - // NOTE: menuRef is generally used here for submenus, as - // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass - // just the top level wxTaskBar menu - // - MenuItemIndex menuItemIndex; - MenuRef menuRef; - - err = GetIndMenuItemWithCommandID(taskbarMenuRef, - command.commandID, - 1, &menuRef, &menuItemIndex); - if (err == noErr) + if (pTB->m_pMenu != NULL) + { + // This is the real reason why we need this. Normally menus + // get handled in wxMacAppEventHandler + // However, in the case of a taskbar menu call + // command.menu.menuRef IS NULL! + // Which causes the wxApp handler just to skip it. + + // get the HICommand from the event + HICommand command; + if (GetEventParameter(inEvent, kEventParamDirectObject, + typeHICommand, NULL,sizeof(HICommand), NULL, &command ) == noErr) { - MenuCommand id = command.commandID; - wxMenuItem* item = NULL; - - if (id != 0) // get the wxMenuItem reference from the MenuRef - GetMenuItemRefCon(menuRef, menuItemIndex, (UInt32*) &item); - - if (item) + // Obtain the REAL menuRef and the menuItemIndex in the real menuRef + // + // NOTE: menuRef is generally used here for submenus, as + // GetMenuItemRefCon could give an incorrect wxMenuItem if we pass + // just the top level wxTaskBar menu + MenuItemIndex menuItemIndex; + MenuRef menuRef; + MenuRef taskbarMenuRef = MAC_WXHMENU(pTB->m_pMenu->GetHMenu()); + + // the next command is only successful if it was a command from the taskbar menu + // otherwise we pass it on + if (GetIndMenuItemWithCommandID(taskbarMenuRef,command.commandID, + 1, &menuRef, &menuItemIndex ) == noErr) { - // Handle items that are checkable - // FIXME: Doesn't work (at least on 10.2)! - if (item->IsCheckable()) - item->Check( !item->IsChecked() ) ; - - // send the wxEvent to the wxMenu - item->GetMenu()->SendEvent(id, - item->IsCheckable() ? - item->IsChecked() : -1 - ); - err = noErr; // successfully handled the event + wxMenu* itemMenu = wxFindMenuFromMacMenu( menuRef ) ; + int id = wxMacCommandToId( command.commandID ) ; + wxMenuItem *item = NULL; + + if (id != 0) // get the wxMenuItem reference from the MenuRef + GetMenuItemRefCon( menuRef, menuItemIndex, (URefCon*) &item ); + + if (item && itemMenu ) + { + if ( eventKind == kEventCommandProcess ) + err = itemMenu->MacHandleCommandProcess( item, id ); + else if ( eventKind == kEventCommandUpdateStatus ) + err = itemMenu->MacHandleCommandUpdateStatus( item, id ); + } } } - } //end if noErr on getting HICommand from event - - // return whether we handled the event or not - return err; + } //end if noErr on getting HICommand from event } - - // We better have a kEventClassApplication/kEventAppGetDockTileMenu combo here, - // otherwise something is truly funky - wxASSERT(eventClass == kEventClassApplication && - eventKind == kEventAppGetDockTileMenu); - - // process the right click events - // NB: This may result in double or even triple-creation of the menus - // We need to do this for 2.4 compat, however - wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN,NULL); - pTB->m_parent->ProcessEvent(downevt); - - wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP,NULL); - pTB->m_parent->ProcessEvent(upevt); - - // create popup menu - wxMenu* menu = pTB->DoCreatePopupMenu(); - - OSStatus err = eventNotHandledErr; - - if (menu != NULL) + else if ((eventClass == kEventClassApplication) && (eventKind == kEventAppGetDockTileMenu )) { - // note to self - a MenuRef *is* a MenuHandle - MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu()); - - // When SetEventParameter is called it will decrement - // the reference count of the menu - we need to make - // sure it stays around in the wxMenu class here - RetainMenu(hMenu); - - // set the actual dock menu - err = SetEventParameter(inEvent, kEventParamMenuRef, - typeMenuRef, sizeof(MenuRef), &hMenu); - wxASSERT(err == noErr); + // process the right click events + // NB: This may result in double or even triple-creation of the menus + // We need to do this for 2.4 compat, however + wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN, NULL); + pTB->m_parent->ProcessEvent(downevt); + + wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP, NULL); + pTB->m_parent->ProcessEvent(upevt); + + // create popup menu + wxMenu* menu = pTB->DoCreatePopupMenu(); + + if (menu != NULL) + { + // note to self - a MenuRef *is* a MenuHandle + MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu()); + + // When SetEventParameter is called it will decrement + // the reference count of the menu - we need to make + // sure it stays around in the wxMenu class here + CFRetain(hMenu); + + // set the actual dock menu + err = SetEventParameter( + inEvent, kEventParamMenuRef, + typeMenuRef, sizeof(MenuRef), &hMenu ); + verify_noerr( err ); + } } return err; } -//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +//----------------------------------------------------------------------------- // wxDeepCopyMenu // // Performs a top-to-bottom copy of the input menu and all of its @@ -290,35 +237,33 @@ pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef, // // This is mostly needed for 2.4 compatability. However wxPython and others // still use this way of setting the taskbarmenu. -//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -wxMenu* wxDeepCopyMenu(wxMenu* menu) +//----------------------------------------------------------------------------- +wxMenu * wxDeepCopyMenu( wxMenu *menu ) { - if (!menu) + if (menu == NULL) return NULL; - // // NB: Here we have to perform a deep copy of the menu, // copying each and every menu item from menu to m_pMenu. - // Other implementations use wxWindow::PopupMenu here, + // Other implementations use wxWindow::PopupMenu here, // which idle execution until the user selects something, - // but since the mac handles this internally, we can't - + // but since the Mac handles this internally, we can't - // and have no way at all to idle it while the dock menu // is being shown before menu goes out of scope (it may // not be on the heap, and may expire right after this function - // is done - we need it to last until the carbon event is triggered - + // is done - we need it to last until the carbon event is triggered - // that's when the user right clicks). // - // Also, since there is no equal (assignment) operator + // Also, since there is no equal (assignment) operator // on either wxMenu or wxMenuItem, we have to do all the // dirty work ourselves. - // // perform a deep copy of the menu wxMenuItemList& theList = menu->GetMenuItems(); wxMenuItemList::compatibility_iterator theNode = theList.GetFirst(); // create the main menu - wxMenu* m_pMenu = new wxMenu(menu->GetTitle()); + wxMenu *m_pMenu = new wxMenu(menu->GetTitle()); while (theNode != NULL) { @@ -327,11 +272,11 @@ wxMenu* wxDeepCopyMenu(wxMenu* menu) new wxMenuItem( m_pMenu, // parent menu theItem->GetId(), // id - theItem->GetText(), // text label + theItem->GetItemLabel(), // text label theItem->GetHelp(), // status bar help string theItem->GetKind(), // menu flags - checkable, separator, etc. - wxDeepCopyMenu(theItem->GetSubMenu()) // submenu - )); + wxDeepCopyMenu(theItem->GetSubMenu()) )); // submenu + theNode = theNode->GetNext(); } @@ -343,31 +288,29 @@ wxMenu* wxDeepCopyMenu(wxMenu* menu) // // Initializes the dock implementation of wxTaskBarIcon. // -// Here we create some mac-specific event handlers and UPPs. +// Here we create some Mac-specific event handlers and UPPs. //----------------------------------------------------------------------------- -wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon* parent) +wxDockTaskBarIcon::wxDockTaskBarIcon(wxTaskBarIcon* parent) : wxTaskBarIconImpl(parent), - m_eventHandlerRef(NULL), m_pMenu(NULL), - m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false) + m_eventHandlerRef(NULL), m_pMenu(NULL), + m_theLastMenu(GetApplicationDockTileMenu()), m_iconAdded(false) { // register the events that will return the dock menu EventTypeSpec tbEventList[] = { { kEventClassCommand, kEventProcessCommand }, + { kEventClassCommand, kEventCommandUpdateStatus }, { kEventClassApplication, kEventAppGetDockTileMenu } }; m_eventupp = NewEventHandlerUPP(wxDockEventHandler); wxASSERT(m_eventupp != NULL); -#ifdef __WXDEBUG__ - OSStatus err = -#endif - InstallApplicationEventHandler( + OSStatus err = InstallApplicationEventHandler( m_eventupp, - GetEventTypeCount(tbEventList), tbEventList, + GetEventTypeCount(tbEventList), tbEventList, this, &m_eventHandlerRef); - wxASSERT( err == noErr ); + verify_noerr( err ); } //----------------------------------------------------------------------------- @@ -382,16 +325,16 @@ wxDockTaskBarIcon::~wxDockTaskBarIcon() DisposeEventHandlerUPP(m_eventupp); // restore old icon and menu to the dock - RemoveIcon(); + RemoveIcon(); } //----------------------------------------------------------------------------- // wxDockTaskBarIcon::DoCreatePopupMenu // -// Helper function that handles a request from the dock event handler +// Helper function that handles a request from the dock event handler // to get the menu for the dock //----------------------------------------------------------------------------- -wxMenu* wxDockTaskBarIcon::DoCreatePopupMenu() +wxMenu * wxDockTaskBarIcon::DoCreatePopupMenu() { // get the menu from the parent wxMenu* theNewMenu = CreatePopupMenu(); @@ -404,7 +347,7 @@ wxMenu* wxDockTaskBarIcon::DoCreatePopupMenu() m_pMenu->SetInvokingWindow(m_menuEventWindow); } - // the return here can be one of three things + // the return here can be one of three things // (in order of priority): // 1) User passed a menu from CreatePopupMenu override // 2) menu sent to and copied from PopupMenu @@ -419,8 +362,8 @@ wxMenu* wxDockTaskBarIcon::DoCreatePopupMenu() // Returns whether or not the dock is not using the default image //----------------------------------------------------------------------------- bool wxDockTaskBarIcon::IsIconInstalled() const -{ - return m_iconAdded; +{ + return m_iconAdded; } //----------------------------------------------------------------------------- @@ -428,21 +371,21 @@ bool wxDockTaskBarIcon::IsIconInstalled() const // // Sets the icon for the dock CGImage functions and SetApplicationDockTileImage //----------------------------------------------------------------------------- -bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) +bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& WXUNUSED(tooltip)) { // convert the wxIcon into a wxBitmap so we can perform some // wxBitmap operations with it - wxBitmap bmp( icon ) ; + wxBitmap bmp( icon ); wxASSERT( bmp.Ok() ); // get the CGImageRef for the wxBitmap: // OSX builds only, but then the dock only exists in OSX - CGImageRef pImage = (CGImageRef) bmp.CGImageCreate(); + CGImageRef pImage = (CGImageRef) bmp.CreateCGImage(); wxASSERT( pImage != NULL ); // actually set the dock image OSStatus err = SetApplicationDockTileImage( pImage ); - wxASSERT( err == noErr ); + verify_noerr( err ); // free the CGImage, now that it's referenced by the dock if (pImage != NULL) @@ -469,10 +412,10 @@ bool wxDockTaskBarIcon::RemoveIcon() // restore old icon to the dock OSStatus err = RestoreApplicationDockTileImage(); - wxASSERT(err == noErr); + verify_noerr( err ); // restore the old menu to the dock - SetApplicationDockTileMenu(m_theLastMenu); + SetApplicationDockTileMenu( m_theLastMenu ); bool success = (err == noErr); m_iconAdded = !success; @@ -485,7 +428,7 @@ bool wxDockTaskBarIcon::RemoveIcon() // // 2.4 and wxPython method that "pops of the menu in the taskbar". // -// In reality because of the way the dock menu works in carbon +// In reality because of the way the dock menu works in carbon // we just save the menu, and if the user didn't override CreatePopupMenu // return the menu passed here, thus sort of getting the same effect. //----------------------------------------------------------------------------- @@ -496,23 +439,23 @@ bool wxDockTaskBarIcon::PopupMenu(wxMenu *menu) if (m_pMenu) delete m_pMenu; - //start copy of menu + // start copy of menu m_pMenu = wxDeepCopyMenu(menu); - //finish up + // finish up m_pMenu->SetInvokingWindow(m_menuEventWindow); return true; } - + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // // wxTaskBarIcon // //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - + IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) - + //----------------------------------------------------------------------------- // wxTaskBarIcon Constructor // @@ -520,14 +463,15 @@ IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) // // Note that we only support DOCK currently as others require cocoa and // also some require hacks and other such things. (MenuExtras are -// actually seperate programs that also require a special undocumented id +// actually seperate programs that also require a special undocumented id // hack and other such fun stuff). //----------------------------------------------------------------------------- wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType nType) { - wxASSERT_MSG(nType == DOCK, - wxT("Only the DOCK implementation of wxTaskBarIcon") - wxT("on mac carbon is currently supported!")); + wxASSERT_MSG( + nType == DOCK, + wxT("Only the DOCK implementation of wxTaskBarIcon on Mac-Carbon is currently supported!") ); + m_impl = new wxDockTaskBarIcon(this); } @@ -550,11 +494,14 @@ wxTaskBarIcon::~wxTaskBarIcon() //----------------------------------------------------------------------------- bool wxTaskBarIcon::IsIconInstalled() const { return m_impl->IsIconInstalled(); } + bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) { return m_impl->SetIcon(icon, tooltip); } + bool wxTaskBarIcon::RemoveIcon() { return m_impl->RemoveIcon(); } + bool wxTaskBarIcon::PopupMenu(wxMenu *menu) { return m_impl->PopupMenu(menu); } -#endif //wxHAS_TASK_BAR_ICON +#endif // wxUSE_TASKBARICON