/////////////////////////////////////////////////////////////////////////////
-// Name: cocoa/app.mm
+// Name: src/cocoa/app.mm
// Purpose: wxApp
// Author: David Elliott
// Modified by:
// Created: 2002/11/27
-// RCS-ID: $Id:
+// RCS-ID: $Id$
// Copyright: (c) David Elliott
-// Licence: wxWindows license
+// Software 2000 Ltd.
+// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-// ============================================================================
-// declarations
-// ============================================================================
+#include "wx/wxprec.h"
-// ----------------------------------------------------------------------------
-// headers
-// ----------------------------------------------------------------------------
+#include "wx/app.h"
-#include "wx/wxprec.h"
#ifndef WX_PRECOMP
- #include "wx/defs.h"
- #include "wx/app.h"
- #include "wx/frame.h"
- #include "wx/dialog.h"
- #include "wx/dc.h"
#include "wx/intl.h"
#include "wx/log.h"
+ #include "wx/module.h"
#endif
-#include "wx/module.h"
-
-#include "wx/cocoa/ObjcPose.h"
+#include "wx/cocoa/ObjcRef.h"
+#include "wx/cocoa/autorelease.h"
+#include "wx/cocoa/mbarman.h"
+#include "wx/cocoa/NSApplication.h"
-#if wxUSE_WX_RESOURCES
-# include "wx/resource.h"
-#endif
+#include "wx/cocoa/dc.h"
#import <AppKit/NSApplication.h>
#import <Foundation/NSRunLoop.h>
-#import <Foundation/NSArray.h>
+#import <Foundation/NSThread.h>
+#import <AppKit/NSEvent.h>
+#import <Foundation/NSString.h>
+#import <Foundation/NSNotification.h>
+#import <AppKit/NSCell.h>
-// ----------------------------------------------------------------------------
-// globals
-// ----------------------------------------------------------------------------
+bool wxApp::sm_isEmbedded = false; // Normally we're not a plugin
+
+// wxNSApplicationObserver singleton.
+static wxObjcAutoRefFromAlloc<wxNSApplicationObserver*> sg_cocoaAppObserver = [[WX_GET_OBJC_CLASS(wxNSApplicationObserver) alloc] init];
-wxApp *wxTheApp = NULL;
-wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
+// ========================================================================
+// wxNSApplicationDelegate
+// ========================================================================
+@implementation wxNSApplicationDelegate : NSObject
-@interface wxPoserNSApplication : NSApplication
+// NOTE: Terminate means that the event loop does NOT return and thus
+// cleanup code doesn't properly execute. Furthermore, wxWidgets has its
+// own exit on frame delete mechanism.
+- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
+ return NO;
}
-- (void)doIdle: (id)data;
-- (void)finishLaunching;
-- (void)sendEvent: (NSEvent*)anEvent;
-- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
-@end // wxPoserNSApplication
+@end // implementation wxNSApplicationDelegate : NSObject
+WX_IMPLEMENT_GET_OBJC_CLASS(wxNSApplicationDelegate,NSObject)
-@implementation wxPoserNSApplication : NSApplication
+// ========================================================================
+// wxNSApplicationObserver
+// ========================================================================
+@implementation wxNSApplicationObserver : NSObject
-- (void)doIdle: (id)data
+- (void)applicationWillBecomeActive:(NSNotification *)notification
{
- 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
- {
- wxLogDebug("Looping for idle events");
- #if 1
- if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
- {
- wxLogDebug("Found actual work to do");
- break;
- }
- #endif
- }
- wxLogDebug("Idle processing complete, requesting next idle event");
- // Add ourself back into the run loop (on next event) if necessary
- wxTheApp->CocoaRequestIdle();
+ wxTheApp->CocoaDelegate_applicationWillBecomeActive();
}
-- (void)finishLaunching
+- (void)applicationDidBecomeActive:(NSNotification *)notification
{
- wxLogDebug("finishLaunching");
- bool initsuccess = wxTheApp->OnInit();
- if(!initsuccess)
- [super stop: NULL];
-
- [super finishLaunching];
+ wxTheApp->CocoaDelegate_applicationDidBecomeActive();
}
-- (void)sendEvent: (NSEvent*)anEvent
+- (void)applicationWillResignActive:(NSNotification *)notification
{
- wxLogDebug("SendEvent");
- wxTheApp->CocoaInstallRequestedIdleHandler();
- [super sendEvent: anEvent];
+ wxTheApp->CocoaDelegate_applicationWillResignActive();
}
-- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
+- (void)applicationDidResignActive:(NSNotification *)notification
{
- BOOL ret = wxTheApp->GetExitOnFrameDelete();
- wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
- return ret;
+ wxTheApp->CocoaDelegate_applicationDidResignActive();
}
-@end // wxPoserNSApplication
-WX_IMPLEMENT_POSER(wxPoserNSApplication);
-
-// ============================================================================
-// functions
-// ============================================================================
-
-//----------------------------------------------------------------------
-// wxEntry
-//----------------------------------------------------------------------
-
-int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
+- (void)applicationWillUpdate:(NSNotification *)notification;
{
- return wxApp::Initialize();
+ wxTheApp->CocoaDelegate_applicationWillUpdate();
}
-int WXDLLEXPORT wxEntryInitGui()
+- (void)controlTintChanged:(NSNotification *)notification
{
- return wxTheApp->OnInitGui();
+ wxLogDebug(wxT("TODO: send EVT_SYS_COLOUR_CHANGED as appropriate"));
}
-void WXDLLEXPORT wxEntryCleanup()
-{
- wxApp::CleanUp();
-}
+@end // implementation wxNSApplicationObserver : NSObject
+WX_IMPLEMENT_GET_OBJC_CLASS(wxNSApplicationObserver,NSObject)
-int wxEntry( int argc, char *argv[])
-{
- if (!wxEntryStart(argc, argv)) {
- return 0;
- }
- wxLogDebug("Creating application");
- // 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.") );
+// ========================================================================
+// wxApp
+// ========================================================================
- wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
- }
-
- wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
+// ----------------------------------------------------------------------------
+// wxApp Static member initialization
+// ----------------------------------------------------------------------------
+IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
+// ----------------------------------------------------------------------------
+// wxApp initialization/cleanup
+// ----------------------------------------------------------------------------
+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
// application (otherwise applications would need to handle it)
-
- if (argc > 1) {
- char theArg[6] = "";
- strncpy(theArg, argv[1], 5);
-
- if (strcmp(theArg, "-psn_") == 0) {
- // assume the argument is always the only one and remove it
+ if ( argc > 1 )
+ {
+ static const wxChar *ARG_PSN = wxT("-psn_");
+ if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
+ {
+ // remove this argument
--argc;
+ memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
}
}
- wxTheApp->argc = argc;
- wxTheApp->argv = argv;
-
- wxLogDebug("initializing gui");
- // 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;
-
- wxLogDebug("Time to run");
- retValue = wxTheApp->OnRun();
-
- wxWindow *topWindow = wxTheApp->GetTopWindow();
- if ( topWindow )
+ /*
+ Cocoa supports -Key value options which set the user defaults key "Key"
+ to the value "value" Some of them are very handy for debugging like
+ -NSShowAllViews YES. Cocoa picks these up from the real argv so
+ our removal of them from the wx copy of it does not affect Cocoa's
+ ability to see them.
+
+ We basically just assume that any "-NS" option and its following
+ argument needs to be removed from argv. We hope that user code does
+ not expect to see -NS options and indeed it's probably a safe bet
+ since most user code accepting options is probably using the
+ double-dash GNU-style syntax.
+ */
+ for(int i=1; i < argc; ++i)
{
- // Forcibly delete the window.
- if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
- topWindow->IsKindOf(CLASSINFO(wxDialog)) )
+ static const wxChar *ARG_NS = wxT("-NS");
+ static const int ARG_NS_LEN = wxStrlen(ARG_NS);
+ if( wxStrncmp(argv[i], ARG_NS, ARG_NS_LEN) == 0 )
{
- topWindow->Close(TRUE);
- }
- else
- {
- delete topWindow;
- wxTheApp->SetTopWindow(NULL);
+ // Only eat this option if it has an argument
+ if( (i + 1) < argc )
+ {
+ argc -= 2;
+ memmove(argv + i, argv + i + 2, argc * sizeof(wxChar*));
+ // drop back one position so the next run through the loop
+ // reprocesses the argument at our current index.
+ --i;
+ }
}
}
- wxTheApp->OnExit();
-
- wxEntryCleanup();
-
- return retValue;
-}
-
-// ----------------------------------------------------------------------------
-// other functions
-// ----------------------------------------------------------------------------
-void wxWakeUpIdle()
-{
- wxTheApp->CocoaRequestIdle();
-}
-
-void wxExit()
-{
- wxLogError(_("Fatal error: exiting"));
-
- wxApp::CleanUp();
- exit(1);
-}
-
-// ============================================================================
-// wxApp implementation
-// ============================================================================
-
-// ----------------------------------------------------------------------------
-// wxApp Static member initialization
-// ----------------------------------------------------------------------------
-wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
-
-#if !USE_SHARED_LIBRARY
-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()
-#endif
-
-// ----------------------------------------------------------------------------
-// wxApp static functions
-// ----------------------------------------------------------------------------
-/*static*/ bool wxApp::Initialize()
-{
- wxPoseAsInitializer::InitializePosers();
- wxClassInfo::InitializeClasses();
-
-#if wxUSE_THREADS
- wxPendingEventsLocker = new wxCriticalSection;
-#endif
-
- wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
- wxTheColourDatabase->Initialize();
-
- wxInitializeStockLists();
- wxInitializeStockObjects();
-
-#if wxUSE_WX_RESOURCES
- wxInitializeResourceSystem();
-#endif
-
- wxBitmap::InitStandardHandlers();
-
- wxModule::RegisterModules();
- if (!wxModule::InitializeModules()) {
- return FALSE;
- }
- return TRUE;
+ return wxAppBase::Initialize(argc, argv);
}
-/*static*/ void wxApp::CleanUp()
+void wxApp::CleanUp()
{
- wxModule::CleanUpModules();
-
-#if wxUSE_WX_RESOURCES
- wxCleanUpResourceSystem();
-#endif
-
- wxDeleteStockObjects() ;
-
- // Destroy all GDI lists, etc.
- wxDeleteStockLists();
-
- delete wxTheColourDatabase;
- wxTheColourDatabase = NULL;
-
- wxBitmap::CleanUpHandlers();
+ wxAutoNSAutoreleasePool pool;
- delete wxPendingEvents;
+ wxCocoaDCImpl::CocoaShutdownTextSystem();
+ wxMenuBarManager::DestroyInstance();
-#if wxUSE_THREADS
- delete wxPendingEventsLocker;
- // If we don't do the following, we get an apparent memory leak.
- ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
-#endif
-
- wxClassInfo::CleanUpClasses();
-
- delete wxTheApp;
- wxTheApp = NULL;
-
-#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
- // At this point we want to check if there are any memory
- // blocks that aren't part of the wxDebugContext itself,
- // as a special case. Then when dumping we need to ignore
- // wxDebugContext, too.
- if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
+ [[NSNotificationCenter defaultCenter] removeObserver:sg_cocoaAppObserver];
+ if(!sm_isEmbedded)
{
- wxLogDebug(wxT("There were memory leaks."));
- wxDebugContext::Dump();
- wxDebugContext::PrintStatistics();
+ [m_cocoaApp setDelegate:nil];
+ [m_cocoaAppDelegate release];
+ m_cocoaAppDelegate = NULL;
}
- // wxDebugContext::SetStream(NULL, NULL);
-#endif
- wxDC::CocoaShutdownTextSystem();
-#if wxUSE_LOG
- // do it as the very last thing because everything else can log messages
- delete wxLog::SetActiveTarget(NULL);
-#endif // wxUSE_LOG
+ wxAppBase::CleanUp();
}
// ----------------------------------------------------------------------------
// wxApp creation
// ----------------------------------------------------------------------------
-
wxApp::wxApp()
{
m_topWindow = NULL;
- wxTheApp = this;
-
- m_isIdle = true;
-#if WXWIN_COMPATIBILITY_2_2
- m_wantDebugOutput = TRUE;
-#endif
argc = 0;
+#if !wxUSE_UNICODE
argv = NULL;
+#endif
m_cocoaApp = NULL;
+ m_cocoaAppDelegate = NULL;
}
-void wxApp::CocoaInstallIdleHandler()
-{
- wxLogDebug("wxApp::CocoaInstallIdleHandler");
- m_isIdle = false;
- // Call doIdle for EVERYTHING dammit
-// We'd need Foundation/NSConnection.h for this next constant, do we need it?
- [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
-}
-
-bool wxApp::OnInitGui()
-{
- if(!wxAppBase::OnInitGui())
- return FALSE;
-
- // Create the app using the sharedApplication method
- m_cocoaApp = [NSApplication sharedApplication];
- wxDC::CocoaInitializeTextSystem();
-// [ m_cocoaApp setDelegate:m_cocoaApp ];
- #if 0
- wxLogDebug("Just for kicks");
- [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
- wxLogDebug("okay.. done now");
- #endif
- return TRUE;
-}
-
-bool wxApp::OnInit()
+void wxApp::CocoaDelegate_applicationWillBecomeActive()
{
- if(!wxAppBase::OnInit())
- return FALSE;
-
- return TRUE;
}
-bool wxApp::Initialized()
+void wxApp::CocoaDelegate_applicationDidBecomeActive()
{
- if (GetTopWindow())
- return TRUE;
- else
- return FALSE;
}
-int wxApp::MainLoop()
+void wxApp::CocoaDelegate_applicationWillResignActive()
{
- [m_cocoaApp run];
- return 0;
+ wxTopLevelWindowCocoa::DeactivatePendingWindow();
}
-// Returns TRUE if more time is needed.
-bool wxApp::ProcessIdle()
+void wxApp::CocoaDelegate_applicationDidResignActive()
{
- wxIdleEvent event;
- event.SetEventObject(this);
- ProcessEvent(event);
-
- return event.MoreRequested();
}
-void wxApp::ExitMainLoop()
+bool wxApp::OnInitGui()
{
- wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
-// CocoaInstallRequestedIdleHandler();
-// if(m_isIdle)
-// [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
-// actually.. we WANT the idle event
-// or not
-#if 0
- if(!m_isIdle)
- [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
-#endif
- [m_cocoaApp terminate: m_cocoaApp];
-}
+ wxAutoNSAutoreleasePool pool;
+ if(!wxAppBase::OnInitGui())
+ return false;
-// Is a message/event pending?
-bool wxApp::Pending()
-{
- return 0;
-}
+ // Create the app using the sharedApplication method
+ m_cocoaApp = [NSApplication sharedApplication];
-// Dispatch a message.
-void wxApp::Dispatch()
-{
-}
+ if(!sm_isEmbedded)
+ {
+ // Enable response to application delegate messages
+ m_cocoaAppDelegate = [[WX_GET_OBJC_CLASS(wxNSApplicationDelegate) alloc] init];
+ [m_cocoaApp setDelegate:m_cocoaAppDelegate];
+ }
-void wxApp::OnIdle(wxIdleEvent& event)
-{
- wxLogDebug("wxApp::OnIdle");
- static bool s_inOnIdle = FALSE;
+ // Enable response to "delegate" messages on the notification observer
+ [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
+ selector:@selector(applicationWillBecomeActive:)
+ name:NSApplicationWillBecomeActiveNotification object:nil];
- // Avoid recursion (via ProcessEvent default case)
- if ( s_inOnIdle )
- return;
- s_inOnIdle = TRUE;
+ [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
+ selector:@selector(applicationDidBecomeActive:)
+ name:NSApplicationDidBecomeActiveNotification object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
+ selector:@selector(applicationWillResignActive:)
+ name:NSApplicationWillResignActiveNotification object:nil];
- DeletePendingObjects();
+ [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
+ selector:@selector(applicationDidResignActive:)
+ name:NSApplicationDidResignActiveNotification object:nil];
- // flush the logged messages if any
- wxLog *pLog = wxLog::GetActiveTarget();
- if ( pLog != NULL && pLog->HasPendingMessages() )
- pLog->Flush();
+ [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
+ selector:@selector(applicationWillUpdate:)
+ name:NSApplicationWillUpdateNotification object:nil];
- // Send OnIdle events to all windows
- bool needMore = SendIdleEvents();
+ // Enable response to system notifications
+ [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
+ selector:@selector(controlTintChanged:)
+ name:NSControlTintDidChangeNotification object:nil];
- if (needMore)
- event.RequestMore(TRUE);
+ if(!sm_isEmbedded)
+ wxMenuBarManager::CreateInstance();
- s_inOnIdle = FALSE;
+ wxCocoaDCImpl::CocoaInitializeTextSystem();
+ return true;
}
-// Send idle event to all top-level windows
-bool wxApp::SendIdleEvents()
+wxApp::~wxApp()
{
- bool needMore = FALSE;
- wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
- while (node)
+ if(m_cfRunLoopIdleObserver != NULL)
{
- wxWindow* win = node->GetData();
- if (SendIdleEvents(win))
- needMore = TRUE;
-
- node = node->GetNext();
+ // Invalidate the observer which also removes it from the run loop.
+ CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
+ // Release the ref as we don't need it anymore.
+ m_cfRunLoopIdleObserver.reset();
}
- return needMore;
}
-// Send idle event to window and all subwindows
-bool wxApp::SendIdleEvents(wxWindow* win)
+bool wxApp::CallOnInit()
{
-// wxLogDebug("SendIdleEvents win=%p",win);
- bool needMore = FALSE;
-
- wxIdleEvent event;
- event.SetEventObject(win);
- win->ProcessEvent(event);
-
- if (event.MoreRequested())
- needMore = TRUE;
+// wxAutoNSAutoreleasePool pool;
+ return OnInit();
+}
- wxWindowList::Node* node = win->GetChildren().GetFirst();
- while (node)
- {
-// wxLogDebug("child=%p",node->Data());
- wxWindow* win = node->GetData();
- if (SendIdleEvents(win))
- needMore = TRUE;
+bool wxApp::OnInit()
+{
+ if(!wxAppBase::OnInit())
+ return false;
- node = node->GetNext();
- }
- return needMore;
+ return true;
}
-// Yield to other processes
-
-bool wxApp::Yield(bool onlyIfNeeded)
+void wxApp::Exit()
{
- // MT-FIXME
- static bool s_inYield = false;
+ wxApp::CleanUp();
-#if wxUSE_LOG
- // disable log flushing from here because a call to wxYield() shouldn't
- // normally result in message boxes popping up &c
- wxLog::Suspend();
-#endif // wxUSE_LOG
+ wxAppConsole::Exit();
+}
- if (s_inYield)
- {
- if ( !onlyIfNeeded )
- {
- wxFAIL_MSG( wxT("wxYield called recursively" ) );
- }
+void wxApp::WakeUpIdle()
+{
+ /* When called from the main thread the NSAutoreleasePool managed by
+ the [NSApplication run] method would ordinarily be in place and so
+ one would think a pool here would be unnecessary.
+
+ However, when called from a different thread there is usually no
+ NSAutoreleasePool in place because wxThread has no knowledge of
+ wxCocoa. The pool here is generally only ever going to contain
+ the NSEvent we create with the factory method. As soon as we add
+ it to the main event queue with postEvent:atStart: it is retained
+ and so safe for our pool to release.
+ */
+ wxAutoNSAutoreleasePool pool;
+ /* NOTE: This is a little heavy handed. What this does is cause an
+ AppKit NSEvent to be added to NSApplication's queue (which is always
+ on the main thread). This will cause the main thread runloop to
+ exit which returns control to nextEventMatchingMask which returns
+ the event which is then sent with sendEvent: and essentially dropped
+ since it's not for a window (windowNumber 0) and NSApplication
+ certainly doesn't understand it.
+
+ With the exception of wxEventLoop::Exit which uses us to cause the
+ runloop to exit and return to the NSApplication event loop, most
+ callers only need wx idle to happen, or more specifically only really
+ need to ensure that ProcessPendingEvents is called which is currently
+ done without exiting the runloop.
+
+ Be careful if you decide to change the implementation of this method
+ as wxEventLoop::Exit depends on the current behaviour.
+ */
+ [m_cocoaApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
+ location:NSZeroPoint modifierFlags:NSAnyEventMask
+ timestamp:0 windowNumber:0 context:nil
+ subtype:0 data1:0 data2:0] atStart:NO];
+}
- return false;
- }
+extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info);
+extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
+{
+ static_cast<wxApp*>(info)->CF_ObserveMainRunLoopBeforeWaiting(observer, activity);
+}
- s_inYield = true;
+#if 0
+static int sg_cApplicationWillUpdate = 0;
+#endif
- wxLogDebug("WARNING: SUPPOSED to have yielded!");
- // FIXME: Do something!
+/*!
+ Invoked from the applicationWillUpdate notification observer. See the
+ NSApplication documentation for the official statement on when this
+ will be called. Since it can be hard to understand for a Cocoa newbie
+ I'll try to explain it here as it relates to wxCocoa.
+
+ Basically, we get called from within nextEventMatchingMask if and only
+ if any user code told the application to send the update notification
+ (sort of like a request for idle events). However, unlike wx idle events,
+ this notification is sent quite often, nearly every time through the loop
+ because nearly every control tells the application to send it.
+
+ Because wx idle events are only supposed to be sent when the event loop
+ is about to block we instead schedule a function to be called just
+ before the run loop waits and send the idle events from there.
+
+ It also has the desirable effect of only sending the wx idle events when
+ the event loop is actually going to block. If the event loop is being
+ pumped manualy (e.g. like a PeekMessage) then the kCFRunLoopBeforeWaiting
+ observer never fires. Our Yield() method depends on this because sending
+ idle events from within Yield would be bad.
+
+ Normally you might think that we could just set the observer up once and
+ leave it attached. However, this is problematic because our run loop
+ observer calls user code (the idle handlers) which can actually display
+ modal dialogs. Displaying a modal dialog causes reentry of the event
+ loop, usually in a different run loop mode than the main loop (e.g. in
+ modal-dialog mode instead of default mode). Because we only register the
+ observer with the run loop mode at the time of this call, it won't be
+ called from a modal loop.
+
+ We want it to be called and thus we need a new observer.
+ */
+void wxApp::CocoaDelegate_applicationWillUpdate()
+{
+ wxLogTrace(wxTRACE_COCOA,wxT("applicationWillUpdate"));
-#if wxUSE_LOG
- // let the logs be flashed again
- wxLog::Resume();
-#endif // wxUSE_LOG
+// CFRunLoopRef cfRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
+ CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
+ wxCFRef<CFStringRef> cfRunLoopMode(CFRunLoopCopyCurrentMode(cfRunLoop));
- s_inYield = false;
+ /* If we have an observer and that observer is for the wrong run loop
+ mode then invalidate it and release it.
+ */
+ if(m_cfRunLoopIdleObserver != NULL && m_cfObservedRunLoopMode != cfRunLoopMode)
+ {
+ CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
+ m_cfRunLoopIdleObserver.reset();
+ }
+#if 0
+ ++sg_cApplicationWillUpdate;
+#endif
+ /* This will be true either on the first call or when the above code has
+ invalidated and released the exisiting observer.
+ */
+ if(m_cfRunLoopIdleObserver == NULL)
+ {
+ // Enable idle event handling
+ CFRunLoopObserverContext observerContext =
+ { 0
+ , this
+ , NULL
+ , NULL
+ , NULL
+ };
+ /* NOTE: I can't recall why we don't just let the observer repeat
+ instead of invalidating itself each time it fires thus requiring
+ it to be recreated for each shot but there was if I remember
+ some good (but very obscure) reason for it.
+
+ On the other hand, I could be wrong so don't take that as gospel.
+ */
+ m_cfRunLoopIdleObserver.reset(CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, /*repeats*/FALSE, /*priority*/0, ObserveMainRunLoopBeforeWaiting, &observerContext));
+ m_cfObservedRunLoopMode = cfRunLoopMode;
+ CFRunLoopAddObserver(cfRunLoop, m_cfRunLoopIdleObserver, m_cfObservedRunLoopMode);
+ }
+}
- return true;
+static inline bool FakeNeedMoreIdle()
+{
+#if 0
+// Return true on every 10th call.
+ static int idleCount = 0;
+ return ++idleCount % 10;
+#else
+ return false;
+#endif
}
-void wxApp::DeletePendingObjects()
+/*!
+ Called by CFRunLoop just before waiting. This is the appropriate time to
+ send idle events. Unlike other ports, we don't peek the queue for events
+ and stop idling if there is one. Instead, if the user requests more idle
+ events we tell Cocoa to send us an applicationWillUpdate notification
+ which will cause our observer of that notification to tell CFRunLoop to
+ call us before waiting which will cause us to be fired again but only
+ after exhausting the event queue.
+
+ The reason we do it this way is that peeking for an event causes CFRunLoop
+ to reenter and fire off its timers, observers, and sources which we're
+ better off avoiding. Doing it this way, we basically let CFRunLoop do the
+ work of peeking for the next event which is much nicer.
+ */
+void wxApp::CF_ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, int activity)
{
- wxNode *node = wxPendingDelete.GetFirst();
- while (node)
+ // Ensure that CocoaDelegate_applicationWillUpdate will recreate us.
+ // We've already been invalidated by CFRunLoop because we are one-shot.
+ m_cfRunLoopIdleObserver.reset();
+#if 0
+ wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN (%d)"), sg_cApplicationWillUpdate);
+ sg_cApplicationWillUpdate = 0;
+#else
+ wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN"));
+#endif
+ if( ProcessIdle() || FakeNeedMoreIdle() )
{
- wxObject *obj = (wxObject *)node->GetData();
-
- delete obj;
-
- if (wxPendingDelete.Find(obj))
- delete node;
-
- node = wxPendingDelete.GetFirst();
+ wxLogTrace(wxTRACE_COCOA, wxT("Idle REQUEST MORE"));
+ [NSApp setWindowsNeedUpdate:YES];
+ }
+ else
+ {
+ wxLogTrace(wxTRACE_COCOA, wxT("Idle END"));
}
}
-// platform specifics
+/* A note about Cocoa's event loops vs. run loops:
+
+ It's important to understand that Cocoa has a two-level event loop. The
+ outer level is run by NSApplication and can only ever happen on the main
+ thread. The nextEventMatchingMask:untilDate:inMode:dequeue: method returns
+ the next event which is then given to sendEvent: to send it. These
+ methods are defined in NSApplication and are thus part of AppKit.
+
+ Events (NSEvent) are only sent due to actual user actions like clicking
+ the mouse or moving the mouse or pressing a key and so on. There are no
+ paint events; there are no timer events; there are no socket events; there
+ are no idle events.
+
+ All of those types of "events" have nothing to do with the GUI at all.
+ That is why Cocoa's AppKit doesn't implement them. Instead, they are
+ implemented in Foundation's NSRunLoop which on OS X uses CFRunLoop
+ to do the actual work.
+
+ How NSApplication uses NSRunLoop is rather interesting. Basically, it
+ interacts with NSRunLoop only from within the nextEventMatchingMask
+ method. It passes its inMode: argument almost directly to NSRunLoop
+ and thus CFRunLoop. The run loop then runs (e.g. loops) until it
+ is told to exit. The run loop calls the callout functions directly.
+ From within those callout functions the run loop is considered to
+ be running. Presumably, the AppKit installs a run loop source to
+ receive messages from the window server over the mach port (like a
+ socket). For some messages (e.g. need to paint) the AppKit will
+ call application code like drawRect: without exiting the run loop.
+ For other messages (ones that can be encapsulated in an NSEvent)
+ the AppKit tells the run loop to exit which returns control to
+ the nextEventMatchingMask method which then returns the NSEvent
+ object. It's important to note that once the runloop has exited
+ it is no longer considered running and thus if you ask it which
+ mode it is running in it will return nil.
+
+ When manually pumping the event loop care should be taken to
+ tell it to run in the correct mode. For instance, if you are
+ using it to run a modal dialog then you want to run it in
+ the modal panel run loop mode. AppKit presumably has sources
+ or timers or observers that specifically don't listen on this
+ mode. Another interesting mode is the connection reply mode.
+ This allows Cocoa to wait for a response from a distributed
+ objects message without firing off user code that may result
+ in a DO call being made thus recursing. So basically, the
+ mode is a way for Cocoa to attempt to avoid run loop recursion
+ but to allow it under certain circumstances.
+ */