+int wxEntry( int argc, char *argv[] , bool enterLoop )
+{
+#ifdef __MWERKS__
+#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
+ // This seems to be necessary since there are 'rogue'
+ // objects present at this point (perhaps global objects?)
+ // Setting a checkpoint will ignore them as far as the
+ // memory checking facility is concerned.
+ // Of course you may argue that memory allocated in globals should be
+ // checked, but this is a reasonable compromise.
+ wxDebugContext::SetCheckpoint();
+#endif
+#endif
+ if (!wxEntryStart(argc, argv)) {
+ return 0;
+ }
+ // create the application object or ensure that one already exists
+ if (!wxTheApp)
+ {
+ // The app may have declared a global application object, but we recommend
+ // the IMPLEMENT_APP macro is used instead, which sets an initializer
+ // function for delayed, dynamic app object construction.
+ wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
+ wxT("No initializer - use IMPLEMENT_APP macro.") );
+
+ wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
+ }
+
+ wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
+
+#ifdef __DARWIN__
+ // Mac OS X passes a process serial number command line argument when
+ // the application is launched from the Finder. This argument must be
+ // removed from the command line arguments before being handled by the
+ // application (otherwise applications would need to handle it)
+
+ if (argc > 1) {
+ if (strncmp(argv[1], "-psn_", 5) == 0) {
+ // assume the argument is always the only one and remove it
+ --argc;
+ }
+ }
+#else
+ argc = 0 ; // currently we don't support files as parameters
+#endif
+ // we could try to get the open apple events here to adjust argc and argv better
+
+ wxTheApp->argc = argc;
+ wxTheApp->argv = argv;
+
+ // GUI-specific initialization, such as creating an app context.
+ wxEntryInitGui();
+
+ // Here frames insert themselves automatically
+ // into wxTopLevelWindows by getting created
+ // in OnInit().
+
+ int retValue = 0;
+
+ if ( wxTheApp->OnInit() )
+ {
+ if ( enterLoop )
+ {
+ retValue = wxTheApp->OnRun();
+ }
+ else
+ // We want to initialize, but not run or exit immediately.
+ return 1;
+ }
+ //else: app initialization failed, so we skipped OnRun()
+
+ wxWindow *topWindow = wxTheApp->GetTopWindow();
+ if ( topWindow )
+ {
+ // Forcibly delete the window.
+ if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
+ topWindow->IsKindOf(CLASSINFO(wxDialog)) )
+ {
+ topWindow->Close(TRUE);
+ wxTheApp->DeletePendingObjects();
+ }
+ else
+ {
+ delete topWindow;
+ wxTheApp->SetTopWindow(NULL);
+ }
+ }
+
+ wxTheApp->OnExit();
+
+ wxEntryCleanup();
+
+ return retValue;
+}
+
+#if TARGET_CARBON
+
+bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
+{
+ bool converted = ConvertEventRefToEventRecord( event,rec) ;
+ OSStatus err = noErr ;
+ if ( !converted )
+ {
+ switch( GetEventClass( event ) )
+ {
+ case kEventClassKeyboard :
+ {
+ converted = true ;
+ switch( GetEventKind(event) )
+ {
+ case kEventRawKeyDown :
+ rec->what = keyDown ;
+ break ;
+ case kEventRawKeyRepeat :
+ rec->what = autoKey ;
+ break ;
+ case kEventRawKeyUp :
+ rec->what = keyUp ;
+ break ;
+ case kEventRawKeyModifiersChanged :
+ rec->what = nullEvent ;
+ break ;
+ default :
+ converted = false ;
+ break ;
+ }
+ if ( converted )
+ {
+ UInt32 keyCode ;
+ unsigned char charCode ;
+ UInt32 modifiers ;
+ GetMouse( &rec->where) ;
+
+ err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
+ err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
+ err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
+ rec->modifiers = modifiers ;
+ rec->message = (keyCode << 8 ) + charCode ;
+ }
+ }
+ break ;
+ case kEventClassTextInput :
+ {
+ switch( GetEventKind( event ) )
+ {
+ case kEventTextInputUnicodeForKeyEvent :
+ {
+ EventRef rawEvent ;
+ err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
+ converted = true ;
+ {
+ UInt32 keyCode ;
+ unsigned char charCode ;
+ UInt32 modifiers ;
+ GetMouse( &rec->where) ;
+ rec->what = keyDown ;
+ err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
+ err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
+ err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
+ rec->modifiers = modifiers ;
+ rec->message = (keyCode << 8 ) + charCode ;
+ }
+ }
+ break ;
+ default :
+ break ;
+ }
+ }
+ break ;
+ }
+ }
+
+ return converted ;
+}
+
+/*
+pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ EventRecord rec ;
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassKeyboard :
+ if ( wxMacConvertEventToRecord( event , &rec ) )
+ {
+ wxTheApp->MacHandleModifierEvents( &rec ) ;
+ wxTheApp->MacHandleOneEvent( &rec ) ;
+ result = noErr ;
+ }
+ break ;
+ case kEventClassTextInput :
+ if ( wxMacConvertEventToRecord( event , &rec ) )
+ {
+ wxTheApp->MacHandleModifierEvents( &rec ) ;
+ wxTheApp->MacHandleOneEvent( &rec ) ;
+ result = noErr ;
+ }
+ break ;
+ default :
+ break ;
+ }
+ return result ;
+}
+*/
+#endif
+
+// Static member initialization
+wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
+
+wxApp::wxApp()
+{
+ m_topWindow = NULL;
+ wxTheApp = this;
+
+#if WXWIN_COMPATIBILITY_2_2
+ m_wantDebugOutput = TRUE;
+#endif
+
+ argc = 0;
+ argv = NULL;
+
+ m_printMode = wxPRINT_WINDOWS;
+ m_auto3D = TRUE;
+}
+
+bool wxApp::Initialized()
+{
+ if (GetTopWindow())
+ return TRUE;
+ else
+ return FALSE;
+}
+
+int wxApp::MainLoop()
+{
+ m_keepGoing = TRUE;
+
+ while (m_keepGoing)
+ {
+ MacDoOneEvent() ;
+ }
+
+ return 0;
+}
+
+// Returns TRUE if more time is needed.
+bool wxApp::ProcessIdle()
+{
+ wxIdleEvent event;
+ event.SetEventObject(this);
+ ProcessEvent(event);
+
+ return event.MoreRequested();
+}
+
+void wxApp::ExitMainLoop()
+{
+#if TARGET_CARBON
+ if (s_inReceiveEvent)
+ QuitApplicationEventLoop() ;
+ else
+ m_keepGoing = FALSE;
+#else
+ m_keepGoing = FALSE;
+#endif
+}
+
+// Is a message/event pending?
+bool wxApp::Pending()
+{
+#if TARGET_CARBON
+ return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
+#else
+ EventRecord event ;
+
+ return EventAvail( everyEvent , &event ) ;
+#endif
+}
+
+// Dispatch a message.
+void wxApp::Dispatch()
+{
+ MacDoOneEvent() ;
+}
+
+void wxApp::OnIdle(wxIdleEvent& event)
+{
+ // Avoid recursion (via ProcessEvent default case)
+ if ( s_inOnIdle )
+ return;
+
+
+ s_inOnIdle = TRUE;
+
+ // 'Garbage' collection of windows deleted with Close().
+ DeletePendingObjects();