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 #include "wx/cocoa/autorelease.h"
19 #import <AppKit/NSPanel.h>
20 #import <AppKit/NSApplication.h>
22 // Lists to keep track of windows, so we can disable/enable them
24 static wxWindowList wxModalDialogs;
26 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
28 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
29 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
30 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
31 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
32 EVT_CLOSE(wxDialog::OnCloseWindow)
35 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
39 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
42 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
43 const wxString& title,
49 wxAutoNSAutoreleasePool pool;
50 wxTopLevelWindows.Append(this);
52 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
56 parent->AddChild(this);
58 NSRect cocoaRect = NSMakeRect(300,300,200,200);
60 unsigned int cocoaStyle = 0;
61 cocoaStyle |= NSTitledWindowMask;
62 cocoaStyle |= NSClosableWindowMask;
63 cocoaStyle |= NSMiniaturizableWindowMask;
64 cocoaStyle |= NSResizableWindowMask;
66 m_cocoaNSWindow = NULL;
67 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
68 // NOTE: SetNSWindow has retained the Cocoa object for this object.
69 // Because we do not release on close, the following release matches the
70 // above alloc and thus the retain count will be 1.
71 [m_cocoaNSWindow release];
72 wxLogDebug("wxDialog m_cocoaNSWindow retainCount=%d",[m_cocoaNSWindow retainCount]);
79 wxLogDebug("Destroying");
80 // setReleasedWhenClosed: NO
81 [m_cocoaNSWindow close];
82 DisassociateNSPanel(m_cocoaNSWindow);
85 void wxDialog::Cocoa_close(void)
88 /* Actually, this isn't true anymore */
89 wxLogDebug("Woah: Dialogs are not generally closed");
92 void wxDialog::SetModal(bool flag)
96 wxModelessWindows.DeleteObject(this);
97 m_windowStyle |= wxDIALOG_MODAL ;
101 m_windowStyle &= ~wxDIALOG_MODAL ;
102 wxModelessWindows.Append(this);
106 bool wxDialog::Show(bool show)
114 wxAutoNSAutoreleasePool pool;
115 wxModalDialogs.Append(this);
116 wxLogDebug("runModal");
117 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
118 wxLogDebug("runModal END");
122 wxLogDebug("abortModal");
123 [wxTheApp->GetNSApplication() abortModal];
124 wxModalDialogs.DeleteObject(this);
130 // Replacement for Show(TRUE) for modal dialogs - returns return code
131 int wxDialog::ShowModal()
136 return GetReturnCode();
139 // EndModal will work for any dialog
140 void wxDialog::EndModal(int retCode)
142 SetReturnCode(retCode);
146 bool wxDialog::IsModal() const
148 return (GetWindowStyleFlag() & wxDIALOG_MODAL);
151 void wxDialog::OnCloseWindow(wxCloseEvent& event)
153 // We'll send a Cancel message by default,
154 // which may close the dialog.
155 // Check for looping if the Cancel event handler calls Close().
157 // Note that if a cancel button and handler aren't present in the dialog,
158 // nothing will happen when you close the dialog via the window manager, or
160 // We wouldn't want to destroy the dialog by default, since the dialog may have been
161 // created on the stack.
162 // However, this does mean that calling dialog->Close() won't delete the dialog
163 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
164 // sure to destroy the dialog.
165 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
166 // ALWAYS VETO THIS EVENT!!!!
169 static wxList closing;
171 if ( closing.Member(this) )
173 wxLogDebug("WARNING: Attempting to recursively call Close for dialog");
177 closing.Append(this);
179 wxLogDebug("Sending Cancel Event");
180 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
181 cancelEvent.SetEventObject( this );
182 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
184 closing.DeleteObject(this);
188 void wxDialog::OnOK(wxCommandEvent& event)
190 if ( Validate() && TransferDataFromWindow() )
196 void wxDialog::OnApply(wxCommandEvent& event)
199 TransferDataFromWindow();
200 // TODO probably need to disable the Apply button until things change again
203 void wxDialog::OnCancel(wxCommandEvent& event)
205 wxLogDebug("Cancelled!");
206 EndModal(wxID_CANCEL);