📄 app.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: src/mac/classic/app.cpp
// Purpose: wxApp
// Author: Stefan Csomor
// Modified by:
// Created: 1998-01-01
// RCS-ID: $Id: app.cpp,v 1.32 2006/06/19 20:18:31 ABX Exp $
// Copyright: (c) Stefan Csomor
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "wx/app.h"
#ifndef WX_PRECOMP
#include "wx/intl.h"
#include "wx/log.h"
#include "wx/utils.h"
#include "wx/window.h"
#include "wx/frame.h"
#include "wx/dc.h"
#include "wx/button.h"
#include "wx/menu.h"
#include "wx/pen.h"
#include "wx/brush.h"
#include "wx/palette.h"
#include "wx/icon.h"
#include "wx/cursor.h"
#include "wx/dialog.h"
#include "wx/msgdlg.h"
#include "wx/textctrl.h"
#include "wx/memory.h"
#include "wx/gdicmn.h"
#endif
#include "wx/module.h"
#include "wx/tooltip.h"
#include "wx/docview.h"
#include "wx/filename.h"
#include <string.h>
// mac
#ifndef __DARWIN__
#if __option(profile)
#include <profiler.h>
#endif
#endif
#include "apprsrc.h"
#include "wx/mac/uma.h"
#include "wx/mac/macnotfy.h"
#ifdef __DARWIN__
# include <CoreServices/CoreServices.h>
# if defined(WXMAKINGDLL_CORE)
# include <mach-o/dyld.h>
# endif
#else
# include <Sound.h>
# include <Threads.h>
# include <ToolUtils.h>
# include <DiskInit.h>
# include <Devices.h>
#endif
extern wxList wxPendingDelete;
extern wxList *wxWinMacWindowList;
extern wxList *wxWinMacControlList;
#if wxUSE_THREADS
extern size_t g_numberOfThreads;
#endif // wxUSE_THREADS
// statics for implementation
static bool s_inYield = false;
#if TARGET_CARBON
static bool s_inReceiveEvent = false ;
static EventTime sleepTime = kEventDurationNoWait ;
#else
static long sleepTime = 0 ;
#endif
IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
EVT_IDLE(wxApp::OnIdle)
EVT_END_SESSION(wxApp::OnEndSession)
EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
END_EVENT_TABLE()
const short kMacMinHeap = (29 * 1024) ;
// platform specific static variables
const short kwxMacMenuBarResource = 1 ;
const short kwxMacAppleMenuId = 1 ;
WXHRGN wxApp::s_macCursorRgn = NULL;
wxWindow* wxApp::s_captureWindow = NULL ;
int wxApp::s_lastMouseDown = 0 ;
long wxApp::sm_lastMessageTime = 0;
long wxApp::s_lastModifiers = 0 ;
bool wxApp::s_macSupportPCMenuShortcuts = true ;
long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
long wxApp::s_macExitMenuItemId = wxID_EXIT ;
wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
// Normally we're not a plugin
bool wxApp::sm_isEmbedded = false;
//----------------------------------------------------------------------
// Core Apple Event Support
//----------------------------------------------------------------------
pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
{
return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
}
pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
{
return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
}
pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
{
return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
}
pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
{
return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
}
pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
{
return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
}
// AEODoc Calls MacOpenFile on each of the files passed
short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
{
AEDescList docList;
AEKeyword keywd;
DescType returnedType;
Size actualSize;
long itemsInList;
FSSpec theSpec;
OSErr err;
short i;
err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
if (err != noErr)
return err;
err = AECountItems(&docList, &itemsInList);
if (err != noErr)
return err;
ProcessSerialNumber PSN ;
PSN.highLongOfPSN = 0 ;
PSN.lowLongOfPSN = kCurrentProcess ;
SetFrontProcess( &PSN ) ;
for (i = 1; i <= itemsInList; i++) {
AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
(Ptr) & theSpec, sizeof(theSpec), &actualSize);
wxString fName = wxMacFSSpec2MacFilename(&theSpec);
MacOpenFile(fName);
}
return noErr;
}
// AEPDoc Calls MacPrintFile on each of the files passed
short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
{
AEDescList docList;
AEKeyword keywd;
DescType returnedType;
Size actualSize;
long itemsInList;
FSSpec theSpec;
OSErr err;
short i;
err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
if (err != noErr)
return err;
err = AECountItems(&docList, &itemsInList);
if (err != noErr)
return err;
ProcessSerialNumber PSN ;
PSN.highLongOfPSN = 0 ;
PSN.lowLongOfPSN = kCurrentProcess ;
SetFrontProcess( &PSN ) ;
for (i = 1; i <= itemsInList; i++) {
AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
(Ptr) & theSpec, sizeof(theSpec), &actualSize);
wxString fName = wxMacFSSpec2MacFilename(&theSpec);
MacPrintFile(fName);
}
return noErr;
}
// AEOApp calls MacNewFile
short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
{
MacNewFile() ;
return noErr ;
}
// AEQuit attempts to quit the application
short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
{
wxWindow* win = GetTopWindow() ;
if ( win )
{
wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
if (!win->ProcessEvent(exitEvent))
win->Close(true) ;
}
else
{
ExitMainLoop() ;
}
return noErr ;
}
// AEROApp calls MacReopenApp
short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
{
MacReopenApp() ;
return noErr ;
}
//----------------------------------------------------------------------
// Support Routines linking the Mac...File Calls to the Document Manager
//----------------------------------------------------------------------
void wxApp::MacOpenFile(const wxString & fileName )
{
wxDocManager* dm = wxDocManager::GetDocumentManager() ;
if ( dm )
dm->CreateDocument(fileName , wxDOC_SILENT ) ;
}
void wxApp::MacPrintFile(const wxString & fileName )
{
wxDocManager* dm = wxDocManager::GetDocumentManager() ;
if ( dm )
{
wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
if ( doc )
{
wxView* view = doc->GetFirstView() ;
if( view )
{
wxPrintout *printout = view->OnCreatePrintout();
if (printout)
{
wxPrinter printer;
printer.Print(view->GetFrame(), printout, true);
delete printout;
}
}
if (doc->Close())
{
doc->DeleteAllViews();
dm->RemoveDocument(doc) ;
}
}
}
}
void wxApp::MacNewFile()
{
}
void wxApp::MacReopenApp()
{
// eventually check for open docs, if none, call MacNewFile
}
//----------------------------------------------------------------------
// Carbon Event Handler
//----------------------------------------------------------------------
#if TARGET_CARBON
static const EventTypeSpec eventList[] =
{
{ kEventClassCommand, kEventProcessCommand } ,
{ kEventClassCommand, kEventCommandUpdateStatus } ,
{ kEventClassMenu, kEventMenuOpening },
{ kEventClassMenu, kEventMenuClosed },
{ kEventClassMenu, kEventMenuTargetItem },
{ kEventClassApplication , kEventAppActivated } ,
{ kEventClassApplication , kEventAppDeactivated } ,
// handling the quit event is not recommended by apple
// rather using the quit apple event - which we do
{ kEventClassAppleEvent , kEventAppleEvent } ,
{ kEventClassMouse , kEventMouseDown } ,
{ kEventClassMouse , kEventMouseMoved } ,
{ kEventClassMouse , kEventMouseUp } ,
{ kEventClassMouse , kEventMouseDragged } ,
{ 'WXMC' , 'WXMC' }
} ;
static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
{
wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
if ( mbar )
{
wxFrame* win = mbar->GetFrame();
if ( win )
{
// VZ: we could find the menu from its handle here by examining all
// the menus in the menu bar recursively but knowing that neither
// wxMSW nor wxGTK do it why bother...
#if 0
MenuRef menuRef;
GetEventParameter(event,
kEventParamDirectObject,
typeMenuRef, NULL,
sizeof(menuRef), NULL,
&menuRef);
#endif // 0
wxEventType type=0;
MenuCommand cmd=0;
switch (GetEventKind(event))
{
case kEventMenuOpening:
type = wxEVT_MENU_OPEN;
break;
case kEventMenuClosed:
type = wxEVT_MENU_CLOSE;
break;
case kEventMenuTargetItem:
type = wxEVT_MENU_HIGHLIGHT;
GetEventParameter(event, kEventParamMenuCommand,
typeMenuCommand, NULL,
sizeof(cmd), NULL, &cmd);
if (cmd == 0) return eventNotHandledErr;
break;
default:
wxFAIL_MSG(wxT("Unexpected menu event kind"));
break;
}
wxMenuEvent wxevent(type, cmd);
wxevent.SetEventObject(win);
(void)win->GetEventHandler()->ProcessEvent(wxevent);
}
}
return eventNotHandledErr;
}
// due to the rather low-level event API of wxWidgets, we cannot use RunApplicationEventLoop
// but have to use ReceiveNextEvent dealing with events manually, therefore we also have
// to deal with clicks in the menu bar explicitly
pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
{
OSStatus result = eventNotHandledErr ;
Point point ;
UInt32 modifiers = 0;
EventMouseButton button = 0 ;
UInt32 click = 0 ;
GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
sizeof( Point ), NULL, &point );
GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
sizeof( UInt32 ), NULL, &modifiers );
GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
sizeof( EventMouseButton ), NULL, &button );
GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
sizeof( UInt32 ), NULL, &click );
if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
modifiers += btnState ;
switch( GetEventKind(event) )
{
case kEventMouseDown :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -