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/dialog.h"
14 #include "wx/settings.h"
17 #import <AppKit/NSPanel.h>
18 #import <AppKit/NSApplication.h>
20 // Lists to keep track of windows, so we can disable/enable them
22 static wxWindowList wxModalDialogs;
24 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
26 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
27 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
28 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
29 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
30 EVT_CLOSE(wxDialog::OnCloseWindow)
33 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
37 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
40 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
41 const wxString& title,
47 wxTopLevelWindows.Append(this);
49 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
53 parent->AddChild(this);
55 NSRect cocoaRect = NSMakeRect(300,300,200,200);
57 unsigned int cocoaStyle = 0;
58 cocoaStyle |= NSTitledWindowMask;
59 cocoaStyle |= NSClosableWindowMask;
60 cocoaStyle |= NSMiniaturizableWindowMask;
61 cocoaStyle |= NSResizableWindowMask;
63 m_cocoaNSWindow = NULL;
64 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
65 // NOTE: SetNSWindow has retained the Cocoa object for this object.
66 // Because we do not release on close, the following release matches the
67 // above alloc and thus the retain count will be 1.
68 [m_cocoaNSWindow release];
69 wxLogDebug("wxDialog m_cocoaNSWindow retainCount=%d",[m_cocoaNSWindow retainCount]);
76 wxLogDebug("Destroying");
77 // setReleasedWhenClosed: NO
78 [m_cocoaNSWindow close];
82 void wxDialog::Cocoa_close(void)
85 /* Actually, this isn't true anymore */
86 wxLogDebug("Woah: Dialogs are not generally closed");
89 void wxDialog::SetModal(bool flag)
93 wxModelessWindows.DeleteObject(this);
94 m_windowStyle |= wxDIALOG_MODAL ;
98 m_windowStyle &= ~wxDIALOG_MODAL ;
99 wxModelessWindows.Append(this);
103 bool wxDialog::Show(bool show)
111 wxModalDialogs.Append(this);
112 wxLogDebug("runModal");
113 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
114 wxLogDebug("runModal END");
118 wxLogDebug("abortModal");
119 [wxTheApp->GetNSApplication() abortModal];
120 wxModalDialogs.DeleteObject(this);
126 // Replacement for Show(TRUE) for modal dialogs - returns return code
127 int wxDialog::ShowModal()
132 return GetReturnCode();
135 // EndModal will work for any dialog
136 void wxDialog::EndModal(int retCode)
138 SetReturnCode(retCode);
142 bool wxDialog::IsModal() const
144 return (GetWindowStyleFlag() & wxDIALOG_MODAL);
147 void wxDialog::OnCloseWindow(wxCloseEvent& event)
149 // We'll send a Cancel message by default,
150 // which may close the dialog.
151 // Check for looping if the Cancel event handler calls Close().
153 // Note that if a cancel button and handler aren't present in the dialog,
154 // nothing will happen when you close the dialog via the window manager, or
156 // We wouldn't want to destroy the dialog by default, since the dialog may have been
157 // created on the stack.
158 // However, this does mean that calling dialog->Close() won't delete the dialog
159 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
160 // sure to destroy the dialog.
161 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
162 // ALWAYS VETO THIS EVENT!!!!
165 static wxList closing;
167 if ( closing.Member(this) )
169 wxLogDebug("WARNING: Attempting to recursively call Close for dialog");
173 closing.Append(this);
175 wxLogDebug("Sending Cancel Event");
176 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
177 cancelEvent.SetEventObject( this );
178 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
180 closing.DeleteObject(this);
184 void wxDialog::OnOK(wxCommandEvent& event)
186 if ( Validate() && TransferDataFromWindow() )
192 void wxDialog::OnApply(wxCommandEvent& event)
195 TransferDataFromWindow();
196 // TODO probably need to disable the Apply button until things change again
199 void wxDialog::OnCancel(wxCommandEvent& event)
201 wxLogDebug("Cancelled!");
202 EndModal(wxID_CANCEL);