* Use wxTopLevelWindow::Show() for modeless dialogs
[wxWidgets.git] / src / cocoa / dialog.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/dialog.mm
3 // Purpose:     wxDialog class
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2002/12/15
7 // RCS-ID:      $Id: 
8 // Copyright:   2002 David Elliott
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/dialog.h"
13 #include "wx/app.h"
14 #include "wx/settings.h"
15 #include "wx/log.h"
16
17 #include "wx/cocoa/autorelease.h"
18
19 #import <AppKit/NSPanel.h>
20 #import <AppKit/NSApplication.h>
21
22 // Lists to keep track of windows, so we can disable/enable them
23 // for modal dialogs
24 static wxWindowList wxModalDialogs;
25
26 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
27
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)
33 END_EVENT_TABLE()
34
35 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
36
37 void wxDialog::Init()
38 {
39     SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
40 }
41
42 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
43            const wxString& title,
44            const wxPoint& pos,
45            const wxSize& size,
46            long style,
47            const wxString& name)
48 {
49     wxAutoNSAutoreleasePool pool;
50     wxTopLevelWindows.Append(this);
51
52     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
53         return false;
54
55     if (parent)
56         parent->AddChild(this);
57
58     NSRect cocoaRect = NSMakeRect(300,300,200,200);
59
60     unsigned int cocoaStyle = 0;
61     cocoaStyle |= NSTitledWindowMask;
62     cocoaStyle |= NSClosableWindowMask;
63     cocoaStyle |= NSMiniaturizableWindowMask;
64     cocoaStyle |= NSResizableWindowMask;
65
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]);
73
74     return true;
75 }
76
77 wxDialog::~wxDialog()
78 {
79     wxLogDebug("Destroying");
80     // setReleasedWhenClosed: NO
81     [m_cocoaNSWindow close];
82     DisassociateNSPanel(m_cocoaNSWindow);
83 }
84
85 void wxDialog::Cocoa_close(void)
86 {
87     m_closed = true;
88     /* Actually, this isn't true anymore */
89     wxLogDebug("Woah: Dialogs are not generally closed");
90 }
91
92 void wxDialog::SetModal(bool flag)
93 {
94     if ( flag )
95     {
96         wxModelessWindows.DeleteObject(this);
97         m_windowStyle |= wxDIALOG_MODAL ;
98     }
99     else
100     {
101         m_windowStyle &= ~wxDIALOG_MODAL ;
102         wxModelessWindows.Append(this);
103     }
104 }
105
106 bool wxDialog::Show(bool show)
107 {
108     if(m_isShown == show)
109         return false;
110     if(show)
111         InitDialog();
112     if(IsModal())
113     {
114         m_isShown = show;
115         if(show)
116         {
117             wxAutoNSAutoreleasePool pool;
118             wxModalDialogs.Append(this);
119             wxLogDebug("runModal");
120             [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
121             wxLogDebug("runModal END");
122         }
123         else
124         {
125             wxLogDebug("abortModal");
126             [wxTheApp->GetNSApplication() abortModal];
127             wxModalDialogs.DeleteObject(this);
128         }
129     }
130     else
131         return wxTopLevelWindow::Show(show);
132     return true;
133 }
134
135 // Replacement for Show(TRUE) for modal dialogs - returns return code
136 int wxDialog::ShowModal()
137 {
138     if(!IsModal())
139         SetModal(true);
140     Show(true);
141     return GetReturnCode();
142 }
143
144 // EndModal will work for any dialog
145 void wxDialog::EndModal(int retCode)
146 {
147     SetReturnCode(retCode);
148     Show(false);
149 }
150
151 bool wxDialog::IsModal() const
152 {
153     return (GetWindowStyleFlag() & wxDIALOG_MODAL);
154 }
155
156 void wxDialog::OnCloseWindow(wxCloseEvent& event)
157 {
158     // We'll send a Cancel message by default,
159     // which may close the dialog.
160     // Check for looping if the Cancel event handler calls Close().
161
162     // Note that if a cancel button and handler aren't present in the dialog,
163     // nothing will happen when you close the dialog via the window manager, or
164     // via Close().
165     // We wouldn't want to destroy the dialog by default, since the dialog may have been
166     // created on the stack.
167     // However, this does mean that calling dialog->Close() won't delete the dialog
168     // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
169     // sure to destroy the dialog.
170     // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
171     // ALWAYS VETO THIS EVENT!!!!
172     event.Veto();
173
174     static wxList closing;
175     
176     if ( closing.Member(this) )
177     {
178         wxLogDebug("WARNING: Attempting to recursively call Close for dialog");
179         return;
180     }
181     
182     closing.Append(this);
183     
184     wxLogDebug("Sending Cancel Event");
185     wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
186     cancelEvent.SetEventObject( this );
187     GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
188
189     closing.DeleteObject(this);
190 }
191
192 // Standard buttons
193 void wxDialog::OnOK(wxCommandEvent& event)
194 {
195     if ( Validate() && TransferDataFromWindow() )
196     {
197         EndModal(wxID_OK);
198     }
199 }
200
201 void wxDialog::OnApply(wxCommandEvent& event)
202 {
203         if (Validate())
204                 TransferDataFromWindow();
205         // TODO probably need to disable the Apply button until things change again
206 }
207
208 void wxDialog::OnCancel(wxCommandEvent& event)
209 {
210     wxLogDebug("Cancelled!");
211     EndModal(wxID_CANCEL);
212 }
213