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"
16 #include "wx/dialog.h"
17 #include "wx/settings.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
23 #import <AppKit/NSPanel.h>
24 #import <AppKit/NSApplication.h>
25 #import <AppKit/NSEvent.h>
26 #import <Foundation/NSRunLoop.h>
28 // Lists to keep track of windows, so we can disable/enable them
30 static wxWindowList wxModalDialogs;
32 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
34 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
35 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
36 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
37 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
38 EVT_CLOSE(wxDialog::OnCloseWindow)
41 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
46 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
49 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
50 const wxString& title,
56 wxAutoNSAutoreleasePool pool;
57 wxTopLevelWindows.Append(this);
59 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
63 parent->AddChild(this);
65 NSRect cocoaRect = NSMakeRect(300,300,200,200);
67 unsigned int cocoaStyle = 0;
68 cocoaStyle |= NSTitledWindowMask;
69 cocoaStyle |= NSClosableWindowMask;
70 cocoaStyle |= NSMiniaturizableWindowMask;
71 cocoaStyle |= NSResizableWindowMask;
73 m_cocoaNSWindow = NULL;
74 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
75 // NOTE: SetNSWindow has retained the Cocoa object for this object.
76 // Because we do not release on close, the following release matches the
77 // above alloc and thus the retain count will be 1.
78 [m_cocoaNSWindow release];
79 wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
80 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
81 [m_cocoaNSWindow setHidesOnDeactivate:NO];
88 DisassociateNSPanel(GetNSPanel());
91 void wxDialog::CocoaDelegate_windowWillClose(void)
94 /* Actually, this isn't true anymore */
95 wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
98 void wxDialog::SetModal(bool flag)
100 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
103 bool wxDialog::Show(bool show)
105 if(m_isShown == show)
110 wxAutoNSAutoreleasePool pool;
113 { // ShowModal() will show the dialog
121 { // this doesn't hide the dialog, base class Show(false) does.
122 wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
123 [wxTheApp->GetNSApplication() abortModal];
124 wxModalDialogs.DeleteObject(this);
128 return wxTopLevelWindow::Show(show);
131 // Shows the dialog and begins a modal event loop. When the event loop
132 // is stopped (via EndModal()) it returns the exit code.
133 int wxDialog::ShowModal()
135 wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
137 // Show(true) will set m_isShown = true
140 wxModalDialogs.Append(this);
142 wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
143 NSApplication *theNSApp = wxTheApp->GetNSApplication();
144 // If the app hasn't started, flush the event queue
145 // If we don't do this, the Dock doesn't get the message that
146 // the app has started so will refuse to activate it.
147 if(![theNSApp isRunning])
149 // We should only do a few iterations so one pool should be okay
150 wxAutoNSAutoreleasePool pool;
151 while(NSEvent *event = [theNSApp
152 nextEventMatchingMask:NSAnyEventMask
153 untilDate:[NSDate distantPast]
154 inMode:NSDefaultRunLoopMode
157 [theNSApp sendEvent: event];
163 wxAutoNSAutoreleasePool pool;
164 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
166 wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
168 return GetReturnCode();
171 void wxDialog::EndModal(int retCode)
173 wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
174 SetReturnCode(retCode);
178 void wxDialog::OnCloseWindow(wxCloseEvent& event)
180 // We'll send a Cancel message by default,
181 // which may close the dialog.
182 // Check for looping if the Cancel event handler calls Close().
184 // Note that if a cancel button and handler aren't present in the dialog,
185 // nothing will happen when you close the dialog via the window manager, or
187 // We wouldn't want to destroy the dialog by default, since the dialog may have been
188 // created on the stack.
189 // However, this does mean that calling dialog->Close() won't delete the dialog
190 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
191 // sure to destroy the dialog.
192 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
193 // ALWAYS VETO THIS EVENT!!!!
196 static wxList closing;
198 if ( closing.Member(this) )
200 wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
204 closing.Append(this);
206 wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event"));
207 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
208 cancelEvent.SetEventObject( this );
209 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
211 closing.DeleteObject(this);
215 void wxDialog::OnOK(wxCommandEvent& event)
217 if ( Validate() && TransferDataFromWindow() )
223 void wxDialog::OnApply(wxCommandEvent& event)
226 TransferDataFromWindow();
227 // TODO probably need to disable the Apply button until things change again
230 void wxDialog::OnCancel(wxCommandEvent& event)
232 wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!"));
233 EndModal(wxID_CANCEL);