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