1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dialog.mm
3 // Purpose: wxDialog class
4 // Author: David Elliott
8 // Copyright: 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/dialog.h"
19 #include "wx/settings.h"
22 #include "wx/testing.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
26 #import <AppKit/NSPanel.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSEvent.h>
29 #import <Foundation/NSRunLoop.h>
31 // Lists to keep track of windows, so we can disable/enable them
33 static wxWindowList wxModalDialogs;
35 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
37 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
42 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
45 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
46 const wxString& title,
52 wxAutoNSAutoreleasePool pool;
53 wxTopLevelWindows.Append(this);
55 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
59 parent->AddChild(this);
61 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
63 NSRect cocoaRect = MakeInitialNSWindowContentRect(pos,size,cocoaStyle);
65 m_cocoaNSWindow = NULL;
66 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
67 // NOTE: SetNSWindow has retained the Cocoa object for this object.
68 // Because we do not release on close, the following release matches the
69 // above alloc and thus the retain count will be 1.
70 [m_cocoaNSWindow release];
71 wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
72 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
73 [m_cocoaNSWindow setHidesOnDeactivate:NO];
80 DisassociateNSPanel(GetNSPanel());
83 void wxDialog::CocoaDelegate_windowWillClose(void)
86 /* Actually, this isn't true anymore */
87 wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
90 void wxDialog::SetModal(bool flag)
92 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
95 bool wxDialog::Show(bool show)
102 wxAutoNSAutoreleasePool pool;
104 if (CanDoLayoutAdaptation())
105 DoLayoutAdaptation();
109 { // ShowModal() will show the dialog
117 { // this doesn't hide the dialog, base class Show(false) does.
118 wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
119 [wxTheApp->GetNSApplication() abortModal];
120 wxModalDialogs.DeleteObject(this);
124 return wxTopLevelWindow::Show(show);
127 // Shows the dialog and begins a modal event loop. When the event loop
128 // is stopped (via EndModal()) it returns the exit code.
129 int wxDialog::ShowModal()
131 WX_TESTING_SHOW_MODAL_HOOK();
133 wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
135 // Show(true) will set m_isShown = true
138 wxModalDialogs.Append(this);
140 wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
141 NSApplication *theNSApp = wxTheApp->GetNSApplication();
142 // If the app hasn't started, flush the event queue
143 // If we don't do this, the Dock doesn't get the message that
144 // the app has started so will refuse to activate it.
145 if(![theNSApp isRunning])
147 // We should only do a few iterations so one pool should be okay
148 wxAutoNSAutoreleasePool pool;
149 while(NSEvent *event = [theNSApp
150 nextEventMatchingMask:NSAnyEventMask
151 untilDate:[NSDate distantPast]
152 inMode:NSDefaultRunLoopMode
155 [theNSApp sendEvent: event];
161 wxAutoNSAutoreleasePool pool;
162 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
164 wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
166 return GetReturnCode();
169 void wxDialog::EndModal(int retCode)
171 wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
172 SetReturnCode(retCode);