2005-03-25 12:23:30 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
|
|
|
//
|
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
#if defined(__GNUG__) && !defined(__APPLE__)
|
|
|
|
#pragma implementation "MacSysMenu.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "stdwx.h"
|
|
|
|
#include "BOINCGUIApp.h"
|
2006-10-21 10:58:22 +00:00
|
|
|
#include "BOINCBaseFrame.h"
|
2005-03-25 12:23:30 +00:00
|
|
|
#include "MacSysMenu.h"
|
|
|
|
#include "DlgAbout.h"
|
|
|
|
#include "Events.h"
|
2005-04-26 15:03:51 +00:00
|
|
|
#include "wx/mac/private.h" // for wxBitmapRefData::GetPictHandle
|
2005-06-23 11:58:09 +00:00
|
|
|
|
2005-03-25 12:23:30 +00:00
|
|
|
pascal OSStatus SysMenuEventHandler( EventHandlerCallRef inHandlerCallRef,
|
|
|
|
EventRef inEvent, void* pData);
|
|
|
|
|
|
|
|
EventTypeSpec myEvents[] = { {kEventClassCommand, kEventCommandProcess},
|
2005-07-06 05:16:53 +00:00
|
|
|
{ kEventClassApplication, kEventAppHidden},
|
|
|
|
{ kEventClassApplication, kEventAppShown},
|
2005-03-25 12:23:30 +00:00
|
|
|
{kEventClassMenu, kEventMenuOpening} };
|
|
|
|
|
2006-07-06 10:52:57 +00:00
|
|
|
CMacSystemMenu::CMacSystemMenu(wxString title, wxIcon* icon, wxIcon* iconDisconnected, wxIcon* iconSnooze)
|
|
|
|
: CTaskBarIcon(title, icon, iconDisconnected, iconSnooze) {
|
2005-03-25 12:23:30 +00:00
|
|
|
CFBundleRef SysMenuBundle = NULL;
|
2005-04-26 15:03:51 +00:00
|
|
|
wxBitmapRefData * theBitsRefData;
|
2005-03-25 12:23:30 +00:00
|
|
|
PicHandle thePICT;
|
2006-01-12 01:42:55 +00:00
|
|
|
wxBitmap theBits;
|
2005-03-25 12:23:30 +00:00
|
|
|
|
2006-01-12 01:42:55 +00:00
|
|
|
theBits.CopyFromIcon(*icon);
|
2005-04-26 15:03:51 +00:00
|
|
|
theBitsRefData = theBits.GetBitmapData();
|
|
|
|
thePICT = theBitsRefData->GetPictHandle();
|
2005-03-25 12:23:30 +00:00
|
|
|
|
2005-07-06 05:16:53 +00:00
|
|
|
m_OpeningAboutDlg = false;
|
|
|
|
|
2005-03-25 12:23:30 +00:00
|
|
|
LoadPrivateFrameworkBundle( CFSTR("SystemMenu.bundle"), &SysMenuBundle );
|
|
|
|
if ( SysMenuBundle != NULL )
|
|
|
|
{
|
|
|
|
SetUpSystemMenu = (SetUpSystemMenuProc)
|
|
|
|
CFBundleGetFunctionPointerForName( SysMenuBundle, CFSTR("SetUpSystemMenu") );
|
|
|
|
SetSystemMenuIcon = (SetSystemMenuIconProc)
|
|
|
|
CFBundleGetFunctionPointerForName( SysMenuBundle, CFSTR("SetSystemMenuIcon") );
|
2005-04-26 15:03:51 +00:00
|
|
|
if ( (SetUpSystemMenu != NULL ) && (thePICT != NULL) )
|
2005-03-25 12:23:30 +00:00
|
|
|
{
|
|
|
|
// Currently, the system menu is the same as the Dock menu with the addition of
|
|
|
|
// the Quit menu item. If in the future you wish to make the system menu different
|
|
|
|
// from the Dock menu, override CTaskBarIcon::BuildContextMenu() and
|
|
|
|
// CTaskBarIcon::AdjustMenuItems().
|
|
|
|
delete m_pMenu;
|
|
|
|
m_pMenu = BuildContextMenu();
|
|
|
|
|
|
|
|
// These should appear in the Mac System Menu but not the Dock
|
|
|
|
m_pMenu->AppendSeparator();
|
|
|
|
m_pMenu->Append( wxID_EXIT, _("E&xit"), wxEmptyString );
|
|
|
|
|
|
|
|
m_pMenu->SetEventHandler(this);
|
|
|
|
SetUpSystemMenu((MenuRef)(m_pMenu->GetHMenu()), thePICT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The base class wxTaskBarIcon will install the wxDockEventHandler for
|
|
|
|
// each instance of the derived classes CTaskBarIcon and CMacSystemMenu.
|
2005-07-21 11:23:06 +00:00
|
|
|
// We remove that handler and substitute our own.
|
|
|
|
RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
|
2005-03-25 12:23:30 +00:00
|
|
|
|
|
|
|
InstallApplicationEventHandler(NewEventHandlerUPP(SysMenuEventHandler),
|
|
|
|
sizeof(myEvents) / sizeof(EventTypeSpec), myEvents,
|
2005-11-04 03:14:33 +00:00
|
|
|
this, (EventHandlerRef*)&m_pEventHandlerRef);
|
2005-03-25 12:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-21 11:23:06 +00:00
|
|
|
CMacSystemMenu::~CMacSystemMenu() {
|
2006-10-30 14:12:22 +00:00
|
|
|
// Remove the System Menu (StatusItem) from menu bar
|
|
|
|
if (SetSystemMenuIcon != NULL )
|
|
|
|
SetSystemMenuIcon(NULL);
|
2005-03-25 12:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the System Menu Icon from XPM data
|
2006-01-12 03:32:15 +00:00
|
|
|
// Warning - this code has not been tested
|
|
|
|
bool CMacSystemMenu::SetIcon(const wxIcon& icon, const wxString&) {
|
2005-04-26 15:03:51 +00:00
|
|
|
wxBitmapRefData * theBitsRefData;
|
2005-03-25 12:23:30 +00:00
|
|
|
PicHandle thePICT;
|
2006-01-12 03:32:15 +00:00
|
|
|
wxBitmap theBits;
|
2006-07-06 11:44:47 +00:00
|
|
|
static const wxIcon* currentIcon = NULL;
|
|
|
|
|
|
|
|
if (&icon == currentIcon)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
currentIcon = &icon;
|
2006-01-12 03:32:15 +00:00
|
|
|
|
|
|
|
theBits.CopyFromIcon(icon);
|
2005-04-26 15:03:51 +00:00
|
|
|
theBitsRefData = theBits.GetBitmapData();
|
|
|
|
thePICT = theBitsRefData->GetPictHandle();
|
2006-01-12 03:32:15 +00:00
|
|
|
if ( (SetSystemMenuIcon != NULL ) && (thePICT != NULL) ) {
|
2005-03-25 12:23:30 +00:00
|
|
|
SetSystemMenuIcon(thePICT);
|
2006-01-12 03:32:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2005-03-25 12:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Utility routine to load a bundle from the application's Frameworks folder.
|
|
|
|
// i.e. : "BOINC.app/Contents/Frameworks/SystemMenu.bundle"
|
2005-07-21 11:23:06 +00:00
|
|
|
void CMacSystemMenu::LoadPrivateFrameworkBundle( CFStringRef framework, CFBundleRef *bundlePtr ) {
|
2005-03-25 12:23:30 +00:00
|
|
|
CFURLRef baseURL = NULL;
|
|
|
|
CFURLRef bundleURL = NULL;
|
|
|
|
CFBundleRef myAppsBundle = NULL;
|
|
|
|
|
|
|
|
if ( bundlePtr == NULL ) goto Bail;
|
|
|
|
*bundlePtr = NULL;
|
|
|
|
|
|
|
|
myAppsBundle = CFBundleGetMainBundle(); // Get our application's main bundle from Core Foundation
|
|
|
|
if ( myAppsBundle == NULL ) goto Bail;
|
|
|
|
|
|
|
|
baseURL = CFBundleCopyPrivateFrameworksURL( myAppsBundle );
|
|
|
|
if ( baseURL == NULL ) goto Bail;
|
|
|
|
|
|
|
|
bundleURL = CFURLCreateCopyAppendingPathComponent( kCFAllocatorSystemDefault, baseURL, framework, false );
|
|
|
|
if ( bundleURL == NULL ) goto Bail;
|
|
|
|
|
|
|
|
*bundlePtr = CFBundleCreate( kCFAllocatorSystemDefault, bundleURL );
|
|
|
|
if ( *bundlePtr == NULL ) goto Bail;
|
|
|
|
|
|
|
|
if ( ! CFBundleLoadExecutable( *bundlePtr ) )
|
|
|
|
{
|
|
|
|
CFRelease( *bundlePtr );
|
|
|
|
*bundlePtr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bail: // Clean up.
|
|
|
|
if ( bundleURL != NULL ) CFRelease( bundleURL );
|
|
|
|
if ( baseURL != NULL ) CFRelease( baseURL );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pascal OSStatus SysMenuEventHandler( EventHandlerCallRef inHandlerCallRef,
|
2005-07-21 11:23:06 +00:00
|
|
|
EventRef inEvent, void* pData) {
|
2005-03-25 12:23:30 +00:00
|
|
|
HICommand command;
|
|
|
|
MenuRef baseMenuRef, sysMenuRef;
|
|
|
|
Str255 theMenuTitle;
|
|
|
|
short i, n, m;
|
|
|
|
CharParameter markChar;
|
|
|
|
const UInt32 eventClass = GetEventClass(inEvent);
|
|
|
|
const UInt32 eventKind = GetEventKind(inEvent);
|
|
|
|
CMacSystemMenu* pMSM;
|
|
|
|
wxMenu* baseMenu;
|
|
|
|
MenuCommand commandID;
|
2005-07-21 11:23:06 +00:00
|
|
|
MenuItemIndex menuItemIndex;
|
|
|
|
wxMenuItem* item = NULL;
|
|
|
|
OSStatus err;
|
2005-03-25 12:23:30 +00:00
|
|
|
|
|
|
|
switch (eventClass) {
|
|
|
|
case kEventClassCommand:
|
|
|
|
if (eventKind != kEventCommandProcess)
|
|
|
|
return eventNotHandledErr;
|
|
|
|
|
|
|
|
GetEventParameter (inEvent, kEventParamDirectObject,
|
|
|
|
typeHICommand, NULL, sizeof(HICommand), NULL, &command);
|
|
|
|
|
2005-07-21 11:23:06 +00:00
|
|
|
commandID = command.commandID;
|
|
|
|
if (commandID == 0)
|
2005-03-25 12:23:30 +00:00
|
|
|
return eventNotHandledErr;
|
2005-07-06 05:16:53 +00:00
|
|
|
|
|
|
|
pMSM = wxGetApp().GetMacSystemMenu();
|
|
|
|
|
2006-05-01 23:35:27 +00:00
|
|
|
// wxMac-2.6.3 "helpfully" converts wxID_ABOUT to kHICommandAbout, wxID_EXIT to kHICommandQuit,
|
|
|
|
// wxID_PREFERENCES to kHICommandPreferences
|
|
|
|
switch (commandID) {
|
|
|
|
case kHICommandAbout:
|
|
|
|
commandID = wxID_ABOUT;
|
|
|
|
break;
|
|
|
|
case kHICommandPreferences:
|
|
|
|
commandID = wxID_PREFERENCES;
|
|
|
|
break;
|
|
|
|
case kHICommandQuit:
|
|
|
|
commandID = wxID_EXIT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-07-21 11:23:06 +00:00
|
|
|
if (commandID == wxID_ABOUT)
|
2005-07-06 05:16:53 +00:00
|
|
|
pMSM->SetOpeningAboutDlg(true);
|
|
|
|
|
2005-03-25 12:23:30 +00:00
|
|
|
// If not our system menu, pass event on to next event handler
|
2005-07-21 11:23:06 +00:00
|
|
|
sysMenuRef = command.menu.menuRef;
|
2005-03-25 12:23:30 +00:00
|
|
|
if (PLstrcmp("\pBOINC!", (GetMenuTitle((sysMenuRef), theMenuTitle) )))
|
|
|
|
return eventNotHandledErr;
|
|
|
|
|
2005-07-21 11:23:06 +00:00
|
|
|
// The following code is adapted from wxTaskBarIcon's wxDockEventHandler().
|
|
|
|
// Work with our base menu instead of the cloned System Menu
|
2005-03-25 12:23:30 +00:00
|
|
|
baseMenu = (pMSM->GetCurrentMenu());
|
|
|
|
baseMenuRef = (MenuRef)(baseMenu->GetHMenu());
|
2005-07-21 11:23:06 +00:00
|
|
|
|
2006-05-01 23:35:27 +00:00
|
|
|
err = GetIndMenuItemWithCommandID(baseMenuRef, command.commandID, 1, NULL, &menuItemIndex);
|
2005-07-21 11:23:06 +00:00
|
|
|
if (err)
|
|
|
|
return eventNotHandledErr; // Command not found in our menu
|
|
|
|
|
|
|
|
GetMenuItemRefCon( baseMenuRef, menuItemIndex, (UInt32*) &item ) ;
|
|
|
|
|
|
|
|
if ( item )
|
|
|
|
{
|
|
|
|
if (item->IsCheckable())
|
|
|
|
item->Check( !item->IsChecked() );
|
|
|
|
|
|
|
|
item->GetMenu()->SendEvent( commandID , item->IsCheckable() ? item->IsChecked() : -1 );
|
|
|
|
return noErr ;
|
|
|
|
}
|
2005-03-25 12:23:30 +00:00
|
|
|
|
|
|
|
return eventNotHandledErr;
|
|
|
|
|
|
|
|
case kEventClassMenu:
|
|
|
|
if (eventKind != kEventMenuOpening)
|
|
|
|
return eventNotHandledErr;
|
|
|
|
|
|
|
|
GetEventParameter (inEvent, kEventParamDirectObject,
|
|
|
|
typeMenuRef, NULL, sizeof(sysMenuRef), NULL, &sysMenuRef);
|
|
|
|
|
|
|
|
// If not our system menu, pass event on to next event handler
|
|
|
|
if (PLstrcmp("\pBOINC!", (GetMenuTitle((sysMenuRef), theMenuTitle) )))
|
|
|
|
return eventNotHandledErr;
|
|
|
|
|
2005-07-06 05:16:53 +00:00
|
|
|
pMSM = wxGetApp().GetMacSystemMenu();
|
2005-03-25 12:23:30 +00:00
|
|
|
baseMenu = (pMSM->GetCurrentMenu());
|
|
|
|
pMSM->AdjustMenuItems(baseMenu);
|
|
|
|
|
|
|
|
// Copy checkmark and enabled status of each item from Dock menu
|
|
|
|
baseMenuRef = (MenuRef)(baseMenu->GetHMenu());
|
|
|
|
n = CountMenuItems(sysMenuRef);
|
|
|
|
m = CountMenuItems(baseMenuRef);
|
|
|
|
if (m < n)
|
|
|
|
n = m;
|
|
|
|
for (i=1; i<=n; i++)
|
|
|
|
{
|
|
|
|
GetMenuItemCommandID(baseMenuRef, i, &commandID);
|
|
|
|
SetMenuItemCommandID(sysMenuRef, i, commandID);
|
|
|
|
GetItemMark(baseMenuRef, i, &markChar);
|
|
|
|
SetItemMark(sysMenuRef, i, markChar);
|
|
|
|
if( IsMenuItemEnabled(baseMenuRef, i) )
|
|
|
|
EnableMenuItem(sysMenuRef, i);
|
|
|
|
else
|
|
|
|
DisableMenuItem(sysMenuRef, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return noErr;
|
2005-07-06 05:16:53 +00:00
|
|
|
|
2005-07-21 11:23:06 +00:00
|
|
|
// Event handling to open or close our main window when applicaiton
|
|
|
|
// is shown or hidden. This probably should go in BOINCGUIApp.cpp
|
|
|
|
// or MainFrame.cpp, but it is more eficient to put it here since
|
|
|
|
// we already have this Mac Event Handler installed.
|
2005-07-06 05:16:53 +00:00
|
|
|
case kEventClassApplication:
|
2006-06-28 02:43:01 +00:00
|
|
|
CBOINCBaseFrame* pFrame = wxGetApp().GetFrame();
|
2005-07-06 05:16:53 +00:00
|
|
|
pMSM = wxGetApp().GetMacSystemMenu();
|
|
|
|
switch (eventKind) {
|
|
|
|
case kEventAppHidden:
|
|
|
|
if (pFrame)
|
|
|
|
pFrame->Hide();
|
|
|
|
pMSM->SetOpeningAboutDlg(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kEventAppShown:
|
|
|
|
// Don't open main window if "About" Dialog from task bar menu.
|
|
|
|
if (pMSM->IsOpeningAboutDlg()) {
|
|
|
|
pMSM->SetOpeningAboutDlg(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ActiveNonFloatingWindow()) // Prevent infinite loop
|
|
|
|
break;
|
|
|
|
if (pFrame) {
|
|
|
|
pFrame->Show();
|
|
|
|
pFrame->SendSizeEvent();
|
|
|
|
}
|
2005-07-06 06:41:27 +00:00
|
|
|
// If a modal dialog was open, make sure it remains in front
|
|
|
|
WindowRef win = GetWindowList();
|
|
|
|
WindowModality modality = kWindowModalityNone;
|
|
|
|
while (win) {
|
|
|
|
GetWindowModality(win, &modality, NULL);
|
|
|
|
if (modality == kWindowModalityAppModal)
|
|
|
|
BringToFront(win);
|
|
|
|
win = GetNextWindow(win);
|
|
|
|
}
|
2005-07-06 05:16:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventNotHandledErr;
|
2005-03-25 12:23:30 +00:00
|
|
|
|
|
|
|
} // End switch (eventClass)
|
|
|
|
|
|
|
|
return eventNotHandledErr;
|
|
|
|
}
|
2005-09-13 09:01:56 +00:00
|
|
|
const char *BOINC_RCSID_533878e385="$Id$";
|