1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dialog.mm
3 // Purpose: wxDialog class
4 // Author: David Elliott
8 // Copyright: 2002 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/dialog.h"
17 #include "wx/settings.h"
20 #include "wx/cocoa/autorelease.h"
22 #import <AppKit/NSPanel.h>
23 #import <AppKit/NSApplication.h>
24 #import <AppKit/NSEvent.h>
25 #import <Foundation/NSRunLoop.h>
27 // Lists to keep track of windows, so we can disable/enable them
29 static wxWindowList wxModalDialogs;
31 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
33 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
34 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
35 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
36 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
37 EVT_CLOSE(wxDialog::OnCloseWindow)
40 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
44 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
47 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
48 const wxString& title,
54 wxAutoNSAutoreleasePool pool;
55 wxTopLevelWindows.Append(this);
57 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
61 parent->AddChild(this);
63 NSRect cocoaRect = NSMakeRect(300,300,200,200);
65 unsigned int cocoaStyle = 0;
66 cocoaStyle |= NSTitledWindowMask;
67 cocoaStyle |= NSClosableWindowMask;
68 cocoaStyle |= NSMiniaturizableWindowMask;
69 cocoaStyle |= NSResizableWindowMask;
71 m_cocoaNSWindow = NULL;
72 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
73 // NOTE: SetNSWindow has retained the Cocoa object for this object.
74 // Because we do not release on close, the following release matches the
75 // above alloc and thus the retain count will be 1.
76 [m_cocoaNSWindow release];
77 wxLogDebug("wxDialog m_cocoaNSWindow retainCount=%d",[m_cocoaNSWindow retainCount]);
84 wxLogDebug("Destroying");
85 // setReleasedWhenClosed: NO
86 [m_cocoaNSWindow close];
87 DisassociateNSPanel(GetNSPanel());
90 void wxDialog::Cocoa_close(void)
93 /* Actually, this isn't true anymore */
94 wxLogDebug("Woah: Dialogs are not generally closed");
97 void wxDialog::SetModal(bool flag)
101 wxModelessWindows.DeleteObject(this);
102 m_windowStyle |= wxDIALOG_MODAL ;
106 m_windowStyle &= ~wxDIALOG_MODAL ;
107 wxModelessWindows.Append(this);
111 bool wxDialog::Show(bool show)
113 if(m_isShown == show)
122 wxAutoNSAutoreleasePool pool;
123 wxModalDialogs.Append(this);
124 wxLogDebug("runModal");
125 NSApplication *theNSApp = wxTheApp->GetNSApplication();
126 // If the app hasn't started, flush the event queue
127 // If we don't do this, the Dock doesn't get the message that
128 // the app has started so will refuse to activate it.
129 if(![theNSApp isRunning])
131 while(NSEvent *event = [theNSApp
132 nextEventMatchingMask:NSAnyEventMask
133 untilDate:[NSDate distantPast]
134 inMode:NSDefaultRunLoopMode
137 [theNSApp sendEvent: event];
140 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
141 wxLogDebug("runModal END");
145 wxLogDebug("abortModal");
146 [wxTheApp->GetNSApplication() abortModal];
147 wxModalDialogs.DeleteObject(this);
151 return wxTopLevelWindow::Show(show);
155 // Replacement for Show(TRUE) for modal dialogs - returns return code
156 int wxDialog::ShowModal()
161 return GetReturnCode();
164 // EndModal will work for any dialog
165 void wxDialog::EndModal(int retCode)
167 SetReturnCode(retCode);
171 bool wxDialog::IsModal() const
173 return (GetWindowStyleFlag() & wxDIALOG_MODAL);
176 void wxDialog::OnCloseWindow(wxCloseEvent& event)
178 // We'll send a Cancel message by default,
179 // which may close the dialog.
180 // Check for looping if the Cancel event handler calls Close().
182 // Note that if a cancel button and handler aren't present in the dialog,
183 // nothing will happen when you close the dialog via the window manager, or
185 // We wouldn't want to destroy the dialog by default, since the dialog may have been
186 // created on the stack.
187 // However, this does mean that calling dialog->Close() won't delete the dialog
188 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
189 // sure to destroy the dialog.
190 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
191 // ALWAYS VETO THIS EVENT!!!!
194 static wxList closing;
196 if ( closing.Member(this) )
198 wxLogDebug("WARNING: Attempting to recursively call Close for dialog");
202 closing.Append(this);
204 wxLogDebug("Sending Cancel Event");
205 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
206 cancelEvent.SetEventObject( this );
207 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
209 closing.DeleteObject(this);
213 void wxDialog::OnOK(wxCommandEvent& event)
215 if ( Validate() && TransferDataFromWindow() )
221 void wxDialog::OnApply(wxCommandEvent& event)
224 TransferDataFromWindow();
225 // TODO probably need to disable the Apply button until things change again
228 void wxDialog::OnCancel(wxCommandEvent& event)
230 wxLogDebug("Cancelled!");
231 EndModal(wxID_CANCEL);