1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dialog.mm
3 // Purpose: wxDialog class
4 // Author: David Elliott
8 // Copyright: 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/dialog.h"
19 #include "wx/settings.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/string.h"
25 #import <AppKit/NSPanel.h>
26 #import <AppKit/NSApplication.h>
27 #import <AppKit/NSEvent.h>
28 #import <Foundation/NSRunLoop.h>
30 // Lists to keep track of windows, so we can disable/enable them
32 static wxWindowList wxModalDialogs;
34 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
36 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
41 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
44 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
45 const wxString& title,
51 wxAutoNSAutoreleasePool pool;
52 wxTopLevelWindows.Append(this);
54 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
58 parent->AddChild(this);
60 NSRect cocoaRect = NSMakeRect(300,300,200,200);
62 unsigned int cocoaStyle = 0;
63 cocoaStyle |= NSTitledWindowMask;
64 cocoaStyle |= NSClosableWindowMask;
65 cocoaStyle |= NSMiniaturizableWindowMask;
66 cocoaStyle |= NSResizableWindowMask;
68 m_cocoaNSWindow = NULL;
69 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
70 // NOTE: SetNSWindow has retained the Cocoa object for this object.
71 // Because we do not release on close, the following release matches the
72 // above alloc and thus the retain count will be 1.
73 [m_cocoaNSWindow release];
74 wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
75 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
76 [m_cocoaNSWindow setHidesOnDeactivate:NO];
83 DisassociateNSPanel(GetNSPanel());
86 void wxDialog::CocoaDelegate_windowWillClose(void)
89 /* Actually, this isn't true anymore */
90 wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
93 void wxDialog::SetModal(bool flag)
95 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
98 bool wxDialog::Show(bool show)
100 if(m_isShown == show)
105 wxAutoNSAutoreleasePool pool;
108 { // ShowModal() will show the dialog
116 { // this doesn't hide the dialog, base class Show(false) does.
117 wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
118 [wxTheApp->GetNSApplication() abortModal];
119 wxModalDialogs.DeleteObject(this);
123 return wxTopLevelWindow::Show(show);
126 // Shows the dialog and begins a modal event loop. When the event loop
127 // is stopped (via EndModal()) it returns the exit code.
128 int wxDialog::ShowModal()
130 wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
132 // Show(true) will set m_isShown = true
135 wxModalDialogs.Append(this);
137 wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
138 NSApplication *theNSApp = wxTheApp->GetNSApplication();
139 // If the app hasn't started, flush the event queue
140 // If we don't do this, the Dock doesn't get the message that
141 // the app has started so will refuse to activate it.
142 if(![theNSApp isRunning])
144 // We should only do a few iterations so one pool should be okay
145 wxAutoNSAutoreleasePool pool;
146 while(NSEvent *event = [theNSApp
147 nextEventMatchingMask:NSAnyEventMask
148 untilDate:[NSDate distantPast]
149 inMode:NSDefaultRunLoopMode
152 [theNSApp sendEvent: event];
158 wxAutoNSAutoreleasePool pool;
159 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
161 wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
163 return GetReturnCode();
166 void wxDialog::EndModal(int retCode)
168 wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
169 SetReturnCode(retCode);