]>
Commit | Line | Data |
---|---|---|
fb896a32 DE |
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 | ||
449c5673 DE |
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 | |
fb896a32 | 19 | |
7fc77f30 | 20 | #include "wx/cocoa/autorelease.h" |
35812955 | 21 | #include "wx/cocoa/string.h" |
7fc77f30 | 22 | |
fb896a32 DE |
23 | #import <AppKit/NSPanel.h> |
24 | #import <AppKit/NSApplication.h> | |
47885968 DE |
25 | #import <AppKit/NSEvent.h> |
26 | #import <Foundation/NSRunLoop.h> | |
fb896a32 DE |
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 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); | |
46 | } | |
47 | ||
48 | bool wxDialog::Create(wxWindow *parent, wxWindowID winid, | |
49 | const wxString& title, | |
50 | const wxPoint& pos, | |
51 | const wxSize& size, | |
52 | long style, | |
53 | const wxString& name) | |
54 | { | |
7fc77f30 | 55 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
56 | wxTopLevelWindows.Append(this); |
57 | ||
58 | if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name)) | |
59 | return false; | |
60 | ||
61 | if (parent) | |
62 | parent->AddChild(this); | |
63 | ||
64 | NSRect cocoaRect = NSMakeRect(300,300,200,200); | |
65 | ||
66 | unsigned int cocoaStyle = 0; | |
67 | cocoaStyle |= NSTitledWindowMask; | |
68 | cocoaStyle |= NSClosableWindowMask; | |
69 | cocoaStyle |= NSMiniaturizableWindowMask; | |
70 | cocoaStyle |= NSResizableWindowMask; | |
71 | ||
72 | m_cocoaNSWindow = NULL; | |
73 | SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]); | |
74 | // NOTE: SetNSWindow has retained the Cocoa object for this object. | |
75 | // Because we do not release on close, the following release matches the | |
76 | // above alloc and thus the retain count will be 1. | |
77 | [m_cocoaNSWindow release]; | |
48580976 | 78 | wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]); |
35812955 DE |
79 | [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)]; |
80 | [m_cocoaNSWindow setHidesOnDeactivate:NO]; | |
fb896a32 DE |
81 | |
82 | return true; | |
83 | } | |
84 | ||
85 | wxDialog::~wxDialog() | |
86 | { | |
162c4aad | 87 | DisassociateNSPanel(GetNSPanel()); |
fb896a32 DE |
88 | } |
89 | ||
9692f42b | 90 | void wxDialog::CocoaDelegate_windowWillClose(void) |
fb896a32 DE |
91 | { |
92 | m_closed = true; | |
93 | /* Actually, this isn't true anymore */ | |
48580976 | 94 | wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed")); |
fb896a32 DE |
95 | } |
96 | ||
97 | void wxDialog::SetModal(bool flag) | |
98 | { | |
99 | if ( flag ) | |
100 | { | |
101 | wxModelessWindows.DeleteObject(this); | |
102 | m_windowStyle |= wxDIALOG_MODAL ; | |
103 | } | |
104 | else | |
105 | { | |
106 | m_windowStyle &= ~wxDIALOG_MODAL ; | |
107 | wxModelessWindows.Append(this); | |
108 | } | |
109 | } | |
110 | ||
111 | bool wxDialog::Show(bool show) | |
112 | { | |
0411b864 DE |
113 | if(m_isShown == show) |
114 | return false; | |
fb896a32 DE |
115 | if(show) |
116 | InitDialog(); | |
117 | if(IsModal()) | |
118 | { | |
0411b864 | 119 | m_isShown = show; |
fb896a32 DE |
120 | if(show) |
121 | { | |
7fc77f30 | 122 | wxAutoNSAutoreleasePool pool; |
fb896a32 | 123 | wxModalDialogs.Append(this); |
48580976 | 124 | wxLogTrace(wxTRACE_COCOA,wxT("runModal")); |
47885968 DE |
125 | NSApplication *theNSApp = wxTheApp->GetNSApplication(); |
126 | // If the app hasn't started, flush the event queue | |
127 | // If we don't do this, the Dock doesn't get the message that | |
128 | // the app has started so will refuse to activate it. | |
129 | if(![theNSApp isRunning]) | |
130 | { | |
131 | while(NSEvent *event = [theNSApp | |
132 | nextEventMatchingMask:NSAnyEventMask | |
133 | untilDate:[NSDate distantPast] | |
134 | inMode:NSDefaultRunLoopMode | |
135 | dequeue: YES]) | |
136 | { | |
137 | [theNSApp sendEvent: event]; | |
138 | } | |
139 | } | |
fb896a32 | 140 | [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow]; |
48580976 | 141 | wxLogTrace(wxTRACE_COCOA,wxT("runModal END")); |
fb896a32 DE |
142 | } |
143 | else | |
144 | { | |
48580976 | 145 | wxLogTrace(wxTRACE_COCOA,wxT("abortModal")); |
fb896a32 | 146 | [wxTheApp->GetNSApplication() abortModal]; |
35e144c2 | 147 | [m_cocoaNSWindow orderOut:m_cocoaNSWindow]; |
fb896a32 DE |
148 | wxModalDialogs.DeleteObject(this); |
149 | } | |
150 | } | |
0411b864 DE |
151 | else |
152 | return wxTopLevelWindow::Show(show); | |
fb896a32 DE |
153 | return true; |
154 | } | |
155 | ||
156 | // Replacement for Show(TRUE) for modal dialogs - returns return code | |
157 | int wxDialog::ShowModal() | |
158 | { | |
159 | if(!IsModal()) | |
160 | SetModal(true); | |
161 | Show(true); | |
162 | return GetReturnCode(); | |
163 | } | |
164 | ||
165 | // EndModal will work for any dialog | |
166 | void wxDialog::EndModal(int retCode) | |
167 | { | |
168 | SetReturnCode(retCode); | |
169 | Show(false); | |
170 | } | |
171 | ||
172 | bool wxDialog::IsModal() const | |
173 | { | |
174 | return (GetWindowStyleFlag() & wxDIALOG_MODAL); | |
175 | } | |
176 | ||
177 | void wxDialog::OnCloseWindow(wxCloseEvent& event) | |
178 | { | |
179 | // We'll send a Cancel message by default, | |
180 | // which may close the dialog. | |
181 | // Check for looping if the Cancel event handler calls Close(). | |
182 | ||
183 | // Note that if a cancel button and handler aren't present in the dialog, | |
184 | // nothing will happen when you close the dialog via the window manager, or | |
185 | // via Close(). | |
186 | // We wouldn't want to destroy the dialog by default, since the dialog may have been | |
187 | // created on the stack. | |
188 | // However, this does mean that calling dialog->Close() won't delete the dialog | |
189 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be | |
190 | // sure to destroy the dialog. | |
191 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. | |
192 | // ALWAYS VETO THIS EVENT!!!! | |
193 | event.Veto(); | |
194 | ||
195 | static wxList closing; | |
196 | ||
197 | if ( closing.Member(this) ) | |
198 | { | |
2b030203 | 199 | wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog")); |
fb896a32 DE |
200 | return; |
201 | } | |
202 | ||
203 | closing.Append(this); | |
204 | ||
48580976 | 205 | wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event")); |
fb896a32 DE |
206 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
207 | cancelEvent.SetEventObject( this ); | |
208 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
209 | ||
210 | closing.DeleteObject(this); | |
211 | } | |
212 | ||
213 | // Standard buttons | |
214 | void wxDialog::OnOK(wxCommandEvent& event) | |
215 | { | |
216 | if ( Validate() && TransferDataFromWindow() ) | |
217 | { | |
218 | EndModal(wxID_OK); | |
219 | } | |
220 | } | |
221 | ||
222 | void wxDialog::OnApply(wxCommandEvent& event) | |
223 | { | |
224 | if (Validate()) | |
225 | TransferDataFromWindow(); | |
226 | // TODO probably need to disable the Apply button until things change again | |
227 | } | |
228 | ||
229 | void wxDialog::OnCancel(wxCommandEvent& event) | |
230 | { | |
48580976 | 231 | wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!")); |
fb896a32 DE |
232 | EndModal(wxID_CANCEL); |
233 | } | |
234 |