From 62068535c57c64732d49c7d8cf770c05903402c0 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 2 Apr 2010 14:52:08 +0000 Subject: [PATCH] simplifying modal event loop handling git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63828 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/dialog.h | 13 ++++---- src/osx/carbon/dialog.cpp | 36 --------------------- src/osx/cocoa/dialog.mm | 68 --------------------------------------- src/osx/dialog_osx.cpp | 15 +++++++-- 4 files changed, 19 insertions(+), 113 deletions(-) diff --git a/include/wx/osx/dialog.h b/include/wx/osx/dialog.h index 77427be8b4..83dd07b816 100644 --- a/include/wx/osx/dialog.h +++ b/include/wx/osx/dialog.h @@ -17,6 +17,7 @@ WXDLLIMPEXP_DATA_CORE(extern const char) wxDialogNameStr[]; class WXDLLIMPEXP_FWD_CORE wxMacToolTip ; +class WXDLLIMPEXP_FWD_CORE wxModalEventLoop ; // Dialog boxes class WXDLLIMPEXP_CORE wxDialog : public wxDialogBase @@ -71,19 +72,19 @@ public: #endif protected: - // show modal dialog and enter modal loop - void DoShowModal(); - - // show modal dialog and enter modal loop + // show window modal dialog void DoShowWindowModal(); + // end window modal dialog. + void EndWindowModal(); + // mac also takes command-period as cancel virtual bool IsEscapeKey(const wxKeyEvent& event); - // needed for cleanup on the Cocoa side. - void EndWindowModal(); wxDialogModality m_modality; + + wxModalEventLoop* m_eventLoop; private: void Init(); diff --git a/src/osx/carbon/dialog.cpp b/src/osx/carbon/dialog.cpp index 8b198abb76..20742be5e4 100644 --- a/src/osx/carbon/dialog.cpp +++ b/src/osx/carbon/dialog.cpp @@ -39,39 +39,3 @@ void wxDialog::DoShowWindowModal() ShowModal(); SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); } - -void wxDialog::DoShowModal() -{ - - SetFocus() ; - - WindowRef windowRef = (WindowRef) GetWXWindow(); - WindowGroupRef windowGroup = NULL; - WindowGroupRef formerParentGroup = NULL; - bool resetGroupParent = false; - - if ( GetParent() == NULL ) - { - windowGroup = GetWindowGroup(windowRef) ; - if ( windowGroup != GetWindowGroupOfClass( kMovableModalWindowClass ) ) - { - formerParentGroup = GetWindowGroupParent( windowGroup ); - SetWindowGroupParent( windowGroup, GetWindowGroupOfClass( kMovableModalWindowClass ) ); - resetGroupParent = true; - } - } - BeginAppModalStateForWindow(windowRef) ; - -#if wxUSE_CONSOLE_EVENTLOOP - wxEventLoopGuarantor ensureHasLoop; -#endif - wxEventLoopBase * const loop = wxEventLoop::GetActive(); - while ( IsModal() ) - loop->Dispatch(); - - EndAppModalStateForWindow(windowRef) ; - if ( resetGroupParent ) - { - SetWindowGroupParent( windowGroup , formerParentGroup ); - } -} diff --git a/src/osx/cocoa/dialog.mm b/src/osx/cocoa/dialog.mm index d55449d1b5..51a4d7ea40 100644 --- a/src/osx/cocoa/dialog.mm +++ b/src/osx/cocoa/dialog.mm @@ -45,71 +45,3 @@ void wxDialog::EndWindowModal() [NSApp endSheet: GetWXWindow()]; [GetWXWindow() orderOut:GetWXWindow()]; } - -void wxDialog::DoShowModal() -{ - // If the app hasn't started, flush the event queue - // If we don't do this, the Dock doesn't get the message that - // the app has started so will refuse to activate it. - NSApplication *theNSApp = [NSApplication sharedApplication]; - if (![theNSApp isRunning]) - { - wxMacAutoreleasePool pool; - while(NSEvent *event = [theNSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES]) - { - [theNSApp sendEvent:event]; - } - } - - SetFocus() ; -/* - WindowGroupRef windowGroup; - WindowGroupRef formerParentGroup; - bool resetGroupParent = false; - - if ( GetParent() == NULL ) - { - windowGroup = GetWindowGroup(windowRef) ; - formerParentGroup = GetWindowGroupParent( windowGroup ); - SetWindowGroupParent( windowGroup, GetWindowGroupOfClass( kMovableModalWindowClass ) ); - resetGroupParent = true; - } -*/ - NSWindow* theWindow = GetWXWindow(); - - NSModalSession session = [NSApp beginModalSessionForWindow:theWindow]; - while (IsModal()) - { - wxMacAutoreleasePool autoreleasepool; - // we cannot break based on the return value, because nested - // alerts might set this to stopped as well, so it would be - // unsafe - [NSApp runModalSession:session]; - - // break if ended, perform no further idle processing - if (!IsModal()) - break; - - // do some idle processing - bool needMore = false; - if (wxTheApp) - { - wxTheApp->ProcessPendingEvents(); - needMore = wxTheApp->ProcessIdle(); - } - - if (!needMore) - { - // no more idle processing wanted - block until the next event - [theNSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:NO]; - } - } - [NSApp endModalSession:session]; - -/* - if ( resetGroupParent ) - { - SetWindowGroupParent( windowGroup , formerParentGroup ); - } -*/ -} diff --git a/src/osx/dialog_osx.cpp b/src/osx/dialog_osx.cpp index 1408ea0fb3..e605e95f3f 100644 --- a/src/osx/dialog_osx.cpp +++ b/src/osx/dialog_osx.cpp @@ -12,6 +12,7 @@ #include "wx/wxprec.h" #include "wx/dialog.h" +#include "wx/evtloop.h" #ifndef WX_PRECOMP #include "wx/app.h" @@ -22,7 +23,6 @@ #include "wx/osx/private.h" - // Lists to keep track of windows, so we can disable/enable them // for modal dialogs @@ -33,6 +33,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) void wxDialog::Init() { m_modality = wxDIALOG_MODALITY_NONE; + m_eventLoop = NULL; } bool wxDialog::Create( wxWindow *parent, @@ -126,8 +127,13 @@ int wxDialog::ShowModal() Show(); - DoShowModal(); - + wxModalEventLoop modalLoop(this); + m_eventLoop = &modalLoop; + + modalLoop.Run(); + + m_eventLoop = NULL; + return GetReturnCode(); } @@ -149,6 +155,9 @@ wxDialogModality wxDialog::GetModality() const // dialogs and should work for both of them void wxDialog::EndModal(int retCode) { + if ( m_eventLoop ) + m_eventLoop->Exit(retCode); + SetReturnCode(retCode); Show(false); } -- 2.47.2