]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dialog.mm
delete the old log target if one had been created somehow even before DoCommonPreInit...
[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: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/dialog.h"
17 #include "wx/settings.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
22
23 #import <AppKit/NSPanel.h>
24 #import <AppKit/NSApplication.h>
25 #import <AppKit/NSEvent.h>
26 #import <Foundation/NSRunLoop.h>
27
28 // Lists to keep track of windows, so we can disable/enable them
29 // for modal dialogs
30 static wxWindowList wxModalDialogs;
31
32 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
33
34 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
35 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
36 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
37 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
38 EVT_CLOSE(wxDialog::OnCloseWindow)
39 END_EVENT_TABLE()
40
41 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
42
43 void wxDialog::Init()
44 {
45 m_isModal = false;
46 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
47 }
48
49 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
50 const wxString& title,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name)
55 {
56 wxAutoNSAutoreleasePool pool;
57 wxTopLevelWindows.Append(this);
58
59 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
60 return false;
61
62 if (parent)
63 parent->AddChild(this);
64
65 NSRect cocoaRect = NSMakeRect(300,300,200,200);
66
67 unsigned int cocoaStyle = 0;
68 cocoaStyle |= NSTitledWindowMask;
69 cocoaStyle |= NSClosableWindowMask;
70 cocoaStyle |= NSMiniaturizableWindowMask;
71 cocoaStyle |= NSResizableWindowMask;
72
73 m_cocoaNSWindow = NULL;
74 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
75 // NOTE: SetNSWindow has retained the Cocoa object for this object.
76 // Because we do not release on close, the following release matches the
77 // above alloc and thus the retain count will be 1.
78 [m_cocoaNSWindow release];
79 wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
80 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
81 [m_cocoaNSWindow setHidesOnDeactivate:NO];
82
83 return true;
84 }
85
86 wxDialog::~wxDialog()
87 {
88 DisassociateNSPanel(GetNSPanel());
89 }
90
91 void wxDialog::CocoaDelegate_windowWillClose(void)
92 {
93 m_closed = true;
94 /* Actually, this isn't true anymore */
95 wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
96 }
97
98 void wxDialog::SetModal(bool flag)
99 {
100 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
101 }
102
103 bool wxDialog::Show(bool show)
104 {
105 if(m_isShown == show)
106 return false;
107
108 if(show)
109 {
110 wxAutoNSAutoreleasePool pool;
111 InitDialog();
112 if(IsModal())
113 { // ShowModal() will show the dialog
114 m_isShown = true;
115 return true;
116 }
117 }
118 else
119 {
120 if(IsModal())
121 { // this doesn't hide the dialog, base class Show(false) does.
122 wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
123 [wxTheApp->GetNSApplication() abortModal];
124 wxModalDialogs.DeleteObject(this);
125 m_isModal = false;
126 }
127 }
128 return wxTopLevelWindow::Show(show);
129 }
130
131 // Shows the dialog and begins a modal event loop. When the event loop
132 // is stopped (via EndModal()) it returns the exit code.
133 int wxDialog::ShowModal()
134 {
135 wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
136
137 // Show(true) will set m_isShown = true
138 m_isShown = false;
139 m_isModal = true;
140 wxModalDialogs.Append(this);
141
142 wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
143 NSApplication *theNSApp = wxTheApp->GetNSApplication();
144 // If the app hasn't started, flush the event queue
145 // If we don't do this, the Dock doesn't get the message that
146 // the app has started so will refuse to activate it.
147 if(![theNSApp isRunning])
148 {
149 // We should only do a few iterations so one pool should be okay
150 wxAutoNSAutoreleasePool pool;
151 while(NSEvent *event = [theNSApp
152 nextEventMatchingMask:NSAnyEventMask
153 untilDate:[NSDate distantPast]
154 inMode:NSDefaultRunLoopMode
155 dequeue: YES])
156 {
157 [theNSApp sendEvent: event];
158 }
159 }
160
161 Show(true);
162 do {
163 wxAutoNSAutoreleasePool pool;
164 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
165 } while(0);
166 wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
167
168 return GetReturnCode();
169 }
170
171 void wxDialog::EndModal(int retCode)
172 {
173 wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
174 SetReturnCode(retCode);
175 Show(false);
176 }
177
178 void wxDialog::EndDialog(int retCode)
179 {
180 if(IsModal())
181 EndModal(retCode);
182 else
183 Show(false);
184 }
185
186 void wxDialog::OnCloseWindow(wxCloseEvent& event)
187 {
188 // We'll send a Cancel message by default,
189 // which may close the dialog.
190 // Check for looping if the Cancel event handler calls Close().
191
192 // Note that if a cancel button and handler aren't present in the dialog,
193 // nothing will happen when you close the dialog via the window manager, or
194 // via Close().
195 // We wouldn't want to destroy the dialog by default, since the dialog may have been
196 // created on the stack.
197 // However, this does mean that calling dialog->Close() won't delete the dialog
198 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
199 // sure to destroy the dialog.
200 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
201 // ALWAYS VETO THIS EVENT!!!!
202 event.Veto();
203
204 static wxList closing;
205
206 if ( closing.Member(this) )
207 {
208 wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
209 return;
210 }
211
212 closing.Append(this);
213
214 wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event"));
215 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
216 cancelEvent.SetEventObject( this );
217 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
218
219 closing.DeleteObject(this);
220 }
221
222 // Standard buttons
223 void wxDialog::OnOK(wxCommandEvent& event)
224 {
225 if ( Validate() && TransferDataFromWindow() )
226 {
227 EndDialog(wxID_OK);
228 }
229 }
230
231 void wxDialog::OnApply(wxCommandEvent& event)
232 {
233 if (Validate())
234 TransferDataFromWindow();
235 // TODO probably need to disable the Apply button until things change again
236 }
237
238 void wxDialog::OnCancel(wxCommandEvent& event)
239 {
240 wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!"));
241 EndDialog(wxID_CANCEL);
242 }
243