]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dialog.mm
removed zlib tests
[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/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::GetSystemColour(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::OnCloseWindow(wxCloseEvent& event)
179 {
180 // We'll send a Cancel message by default,
181 // which may close the dialog.
182 // Check for looping if the Cancel event handler calls Close().
183
184 // Note that if a cancel button and handler aren't present in the dialog,
185 // nothing will happen when you close the dialog via the window manager, or
186 // via Close().
187 // We wouldn't want to destroy the dialog by default, since the dialog may have been
188 // created on the stack.
189 // However, this does mean that calling dialog->Close() won't delete the dialog
190 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
191 // sure to destroy the dialog.
192 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
193 // ALWAYS VETO THIS EVENT!!!!
194 event.Veto();
195
196 static wxList closing;
197
198 if ( closing.Member(this) )
199 {
200 wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
201 return;
202 }
203
204 closing.Append(this);
205
206 wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event"));
207 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
208 cancelEvent.SetEventObject( this );
209 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
210
211 closing.DeleteObject(this);
212 }
213
214 // Standard buttons
215 void wxDialog::OnOK(wxCommandEvent& event)
216 {
217 if ( Validate() && TransferDataFromWindow() )
218 {
219 EndModal(wxID_OK);
220 }
221 }
222
223 void wxDialog::OnApply(wxCommandEvent& event)
224 {
225 if (Validate())
226 TransferDataFromWindow();
227 // TODO probably need to disable the Apply button until things change again
228 }
229
230 void wxDialog::OnCancel(wxCommandEvent& event)
231 {
232 wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!"));
233 EndModal(wxID_CANCEL);
234 }
235