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>
25 // Lists to keep track of windows, so we can disable/enable them
27 static wxWindowList wxModalDialogs;
29 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
31 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
32 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
33 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
34 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
35 EVT_CLOSE(wxDialog::OnCloseWindow)
38 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
42 SetBackgroundColour(wxSystemSettings::GetSystemColour(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 NSRect cocoaRect = NSMakeRect(300,300,200,200);
63 unsigned int cocoaStyle = 0;
64 cocoaStyle |= NSTitledWindowMask;
65 cocoaStyle |= NSClosableWindowMask;
66 cocoaStyle |= NSMiniaturizableWindowMask;
67 cocoaStyle |= NSResizableWindowMask;
69 m_cocoaNSWindow = NULL;
70 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
71 // NOTE: SetNSWindow has retained the Cocoa object for this object.
72 // Because we do not release on close, the following release matches the
73 // above alloc and thus the retain count will be 1.
74 [m_cocoaNSWindow release];
75 wxLogDebug("wxDialog m_cocoaNSWindow retainCount=%d",[m_cocoaNSWindow retainCount]);
82 wxLogDebug("Destroying");
83 // setReleasedWhenClosed: NO
84 [m_cocoaNSWindow close];
85 DisassociateNSPanel(m_cocoaNSWindow);
88 void wxDialog::Cocoa_close(void)
91 /* Actually, this isn't true anymore */
92 wxLogDebug("Woah: Dialogs are not generally closed");
95 void wxDialog::SetModal(bool flag)
99 wxModelessWindows.DeleteObject(this);
100 m_windowStyle |= wxDIALOG_MODAL ;
104 m_windowStyle &= ~wxDIALOG_MODAL ;
105 wxModelessWindows.Append(this);
109 bool wxDialog::Show(bool show)
111 if(m_isShown == show)
120 wxAutoNSAutoreleasePool pool;
121 wxModalDialogs.Append(this);
122 wxLogDebug("runModal");
123 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
124 wxLogDebug("runModal END");
128 wxLogDebug("abortModal");
129 [wxTheApp->GetNSApplication() abortModal];
130 wxModalDialogs.DeleteObject(this);
134 return wxTopLevelWindow::Show(show);
138 // Replacement for Show(TRUE) for modal dialogs - returns return code
139 int wxDialog::ShowModal()
144 return GetReturnCode();
147 // EndModal will work for any dialog
148 void wxDialog::EndModal(int retCode)
150 SetReturnCode(retCode);
154 bool wxDialog::IsModal() const
156 return (GetWindowStyleFlag() & wxDIALOG_MODAL);
159 void wxDialog::OnCloseWindow(wxCloseEvent& event)
161 // We'll send a Cancel message by default,
162 // which may close the dialog.
163 // Check for looping if the Cancel event handler calls Close().
165 // Note that if a cancel button and handler aren't present in the dialog,
166 // nothing will happen when you close the dialog via the window manager, or
168 // We wouldn't want to destroy the dialog by default, since the dialog may have been
169 // created on the stack.
170 // However, this does mean that calling dialog->Close() won't delete the dialog
171 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
172 // sure to destroy the dialog.
173 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
174 // ALWAYS VETO THIS EVENT!!!!
177 static wxList closing;
179 if ( closing.Member(this) )
181 wxLogDebug("WARNING: Attempting to recursively call Close for dialog");
185 closing.Append(this);
187 wxLogDebug("Sending Cancel Event");
188 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
189 cancelEvent.SetEventObject( this );
190 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
192 closing.DeleteObject(this);
196 void wxDialog::OnOK(wxCommandEvent& event)
198 if ( Validate() && TransferDataFromWindow() )
204 void wxDialog::OnApply(wxCommandEvent& event)
207 TransferDataFromWindow();
208 // TODO probably need to disable the Apply button until things change again
211 void wxDialog::OnCancel(wxCommandEvent& event)
213 wxLogDebug("Cancelled!");
214 EndModal(wxID_CANCEL);