]> git.saurik.com Git - wxWidgets.git/blobdiff - src/cocoa/app.mm
moved compat content to formats, there will be more wx-specific formats
[wxWidgets.git] / src / cocoa / app.mm
index 62574e7a0e29839f032ef800ba251ca3c477d1d6..fe12b0f00e008c52d2b010703fc1c749f8acd61f 100644 (file)
@@ -4,9 +4,9 @@
 // Author:      David Elliott
 // Modified by:
 // Created:     2002/11/27
 // Author:      David Elliott
 // Modified by:
 // Created:     2002/11/27
-// RCS-ID:      $Id:
+// RCS-ID:      $Id$
 // Copyright:   (c) David Elliott
 // Copyright:   (c) David Elliott
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -31,6 +31,7 @@
 #include "wx/module.h"
 
 #include "wx/cocoa/ObjcPose.h"
 #include "wx/module.h"
 
 #include "wx/cocoa/ObjcPose.h"
+#include "wx/cocoa/autorelease.h"
 
 #if wxUSE_WX_RESOURCES
 #  include "wx/resource.h"
 
 #if wxUSE_WX_RESOURCES
 #  include "wx/resource.h"
@@ -39,6 +40,8 @@
 #import <AppKit/NSApplication.h>
 #import <Foundation/NSRunLoop.h>
 #import <Foundation/NSArray.h>
 #import <AppKit/NSApplication.h>
 #import <Foundation/NSRunLoop.h>
 #import <Foundation/NSArray.h>
+#import <Foundation/NSAutoreleasePool.h>
+#import <Foundation/NSThread.h>
 
 // ----------------------------------------------------------------------------
 // globals
 
 // ----------------------------------------------------------------------------
 // globals
@@ -51,7 +54,6 @@ wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
 }
 
 - (void)doIdle: (id)data;
 }
 
 - (void)doIdle: (id)data;
-- (void)finishLaunching;
 - (void)sendEvent: (NSEvent*)anEvent;
 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
 @end // wxPoserNSApplication
 - (void)sendEvent: (NSEvent*)anEvent;
 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
 @end // wxPoserNSApplication
@@ -62,34 +64,33 @@ wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
 {
     wxASSERT(wxTheApp);
     wxLogDebug("doIdle called");
 {
     wxASSERT(wxTheApp);
     wxLogDebug("doIdle called");
-    NSRunLoop *rl = [NSRunLoop currentRunLoop];
-    // runMode: beforeDate returns YES if something was done
-    while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
+#ifdef __WXDEBUG__
+    if(wxTheApp->IsInAssert())
     {
     {
-        wxLogDebug("Looping for idle events");
-        #if 1
-        if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
+        wxLogDebug("Idle events ignored durring assertion dialog");
+    }
+    else
+#endif
+    {
+        NSRunLoop *rl = [NSRunLoop currentRunLoop];
+        // runMode: beforeDate returns YES if something was done
+        while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
         {
         {
-            wxLogDebug("Found actual work to do");
-            break;
+            wxLogDebug("Looping for idle events");
+            #if 1
+            if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
+            {
+                wxLogDebug("Found actual work to do");
+                break;
+            }
+            #endif
         }
         }
-        #endif
     }
     wxLogDebug("Idle processing complete, requesting next idle event");
     // Add ourself back into the run loop (on next event) if necessary
     wxTheApp->CocoaRequestIdle();
 }
 
     }
     wxLogDebug("Idle processing complete, requesting next idle event");
     // Add ourself back into the run loop (on next event) if necessary
     wxTheApp->CocoaRequestIdle();
 }
 
-- (void)finishLaunching
-{
-    wxLogDebug("finishLaunching");
-    bool initsuccess = wxTheApp->OnInit();
-    if(!initsuccess)
-        [super stop: NULL];
-
-    [super finishLaunching];
-}
-
 - (void)sendEvent: (NSEvent*)anEvent
 {
     wxLogDebug("SendEvent");
 - (void)sendEvent: (NSEvent*)anEvent
 {
     wxLogDebug("SendEvent");
@@ -129,7 +130,7 @@ void wxApp::Exit()
 #if !USE_SHARED_LIBRARY
 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
 #if !USE_SHARED_LIBRARY
 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
-    EVT_IDLE(wxApp::OnIdle)
+    EVT_IDLE(wxAppBase::OnIdle)
 //    EVT_END_SESSION(wxApp::OnEndSession)
 //    EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
 END_EVENT_TABLE()
 //    EVT_END_SESSION(wxApp::OnEndSession)
 //    EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
 END_EVENT_TABLE()
@@ -141,6 +142,8 @@ END_EVENT_TABLE()
 
 bool wxApp::Initialize(int& argc, wxChar **argv)
 {
 
 bool wxApp::Initialize(int& argc, wxChar **argv)
 {
+    wxAutoNSAutoreleasePool pool;
+    m_cocoaMainThread = [NSThread currentThread];
     // 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
     // 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
@@ -176,12 +179,15 @@ void wxApp::CleanUp()
 wxApp::wxApp()
 {
     m_topWindow = NULL;
 wxApp::wxApp()
 {
     m_topWindow = NULL;
-    wxTheApp = this;
 
     m_isIdle = true;
 #if WXWIN_COMPATIBILITY_2_2
     m_wantDebugOutput = TRUE;
 #endif
 
     m_isIdle = true;
 #if WXWIN_COMPATIBILITY_2_2
     m_wantDebugOutput = TRUE;
 #endif
+#ifdef __WXDEBUG__
+    m_isInAssert = FALSE;
+#endif // __WXDEBUG__
+
 
     argc = 0;
     argv = NULL;
 
     argc = 0;
     argv = NULL;
@@ -190,6 +196,15 @@ wxApp::wxApp()
 
 void wxApp::CocoaInstallIdleHandler()
 {
 
 void wxApp::CocoaInstallIdleHandler()
 {
+    // If we're not the main thread, don't install the idle handler
+    if(m_cocoaMainThread != [NSThread currentThread])
+    {
+        wxLogDebug("Attempt to install idle handler from secondary thread");
+        return;
+    }
+    // If we're supposed to be stopping, don't add more idle events
+    if(![m_cocoaApp isRunning])
+        return;
     wxLogDebug("wxApp::CocoaInstallIdleHandler");
     m_isIdle = false;
     // Call doIdle for EVERYTHING dammit
     wxLogDebug("wxApp::CocoaInstallIdleHandler");
     m_isIdle = false;
     // Call doIdle for EVERYTHING dammit
@@ -199,6 +214,7 @@ void wxApp::CocoaInstallIdleHandler()
 
 bool wxApp::OnInitGui()
 {
 
 bool wxApp::OnInitGui()
 {
+    wxAutoNSAutoreleasePool pool;
     if(!wxAppBase::OnInitGui())
         return FALSE;
 
     if(!wxAppBase::OnInitGui())
         return FALSE;
 
@@ -214,6 +230,12 @@ bool wxApp::OnInitGui()
     return TRUE;
 }
 
     return TRUE;
 }
 
+bool wxApp::CallOnInit()
+{
+//    wxAutoNSAutoreleasePool pool;
+    return OnInit();
+}
+
 bool wxApp::OnInit()
 {
     if(!wxAppBase::OnInit())
 bool wxApp::OnInit()
 {
     if(!wxAppBase::OnInit())
@@ -236,16 +258,6 @@ int wxApp::MainLoop()
     return 0;
 }
 
     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()
 {
     wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
 void wxApp::ExitMainLoop()
 {
     wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
@@ -258,7 +270,7 @@ void wxApp::ExitMainLoop()
     if(!m_isIdle)
         [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
 #endif
     if(!m_isIdle)
         [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
 #endif
-    [m_cocoaApp terminate: m_cocoaApp];
+    [m_cocoaApp stop: m_cocoaApp];
 }
 
 // Is a message/event pending?
 }
 
 // Is a message/event pending?
@@ -272,75 +284,6 @@ void wxApp::Dispatch()
 {
 }
 
 {
 }
 
-void wxApp::OnIdle(wxIdleEvent& event)
-{
-    wxLogDebug("wxApp::OnIdle");
-   static bool s_inOnIdle = FALSE;
-
-    // Avoid recursion (via ProcessEvent default case)
-    if ( s_inOnIdle )
-        return;
-    s_inOnIdle = TRUE;
-
-
-    DeletePendingObjects();
-
-    // flush the logged messages if any
-    wxLog *pLog = wxLog::GetActiveTarget();
-    if ( pLog != NULL && pLog->HasPendingMessages() )
-        pLog->Flush();
-
-    // Send OnIdle events to all windows
-    bool needMore = SendIdleEvents();
-
-    if (needMore)
-      event.RequestMore(TRUE);
-
-    s_inOnIdle = FALSE;
-}
-
-// Send idle event to all top-level windows
-bool wxApp::SendIdleEvents()
-{
-    bool needMore = FALSE;
-    wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
-    while (node)
-    {
-        wxWindow* win = node->GetData();
-        if (SendIdleEvents(win))
-            needMore = TRUE;
-
-        node = node->GetNext();
-    }
-    return needMore;
-}
-
-// Send idle event to window and all subwindows
-bool wxApp::SendIdleEvents(wxWindow* win)
-{
-//    wxLogDebug("SendIdleEvents win=%p",win);
-    bool needMore = FALSE;
-
-    wxIdleEvent event;
-    event.SetEventObject(win);
-    win->ProcessEvent(event);
-
-    if (event.MoreRequested())
-        needMore = TRUE;
-
-    wxWindowList::Node* node = win->GetChildren().GetFirst();
-    while (node)
-    {
-//        wxLogDebug("child=%p",node->Data());
-        wxWindow* win = node->GetData();
-        if (SendIdleEvents(win))
-            needMore = TRUE;
-
-        node = node->GetNext();
-    }
-    return needMore;
-}
-
 // Yield to other processes
 
 bool wxApp::Yield(bool onlyIfNeeded)
 // Yield to other processes
 
 bool wxApp::Yield(bool onlyIfNeeded)
@@ -379,3 +322,12 @@ bool wxApp::Yield(bool onlyIfNeeded)
     return true;
 }
 
     return true;
 }
 
+#ifdef __WXDEBUG__
+void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
+{
+    m_isInAssert = TRUE;
+    wxAppBase::OnAssert(file, line, cond, msg);
+    m_isInAssert = FALSE;
+}
+#endif // __WXDEBUG__
+