]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dialog.cpp
reformat for readability
[wxWidgets.git] / src / mac / carbon / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dialog.h"
15 #include "wx/utils.h"
16 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/settings.h"
19
20 #include "wx/mac/uma.h"
21
22 // Lists to keep track of windows, so we can disable/enable them
23 // for modal dialogs
24 wxList wxModalDialogs;
25 //wxList wxModelessWindows; // Frames and modeless dialogs
26 extern wxList wxPendingDelete;
27
28 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
29
30 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
31 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
32 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
33 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
34
35 EVT_CHAR_HOOK(wxDialog::OnCharHook)
36
37 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
38
39 EVT_CLOSE(wxDialog::OnCloseWindow)
40 END_EVENT_TABLE()
41
42 void wxDialog::Init()
43 {
44 m_isModalStyle = false;
45 }
46
47 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
48 const wxString& title,
49 const wxPoint& pos,
50 const wxSize& size,
51 long style,
52 const wxString& name)
53 {
54 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
55
56 // All dialogs should really have this style
57 style |= wxTAB_TRAVERSAL;
58
59 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style & ~(wxYES|wxOK|wxNO /*|wxCANCEL*/) , name) )
60 return false;
61
62 return true;
63 }
64
65 void wxDialog::SetModal(bool flag)
66 {
67 if ( flag )
68 {
69 m_isModalStyle = true;
70
71 wxModelessWindows.DeleteObject(this);
72 #if TARGET_CARBON
73 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
74 #endif
75 }
76 else
77 {
78 m_isModalStyle = false;
79
80 wxModelessWindows.Append(this);
81 }
82 }
83
84 wxDialog::~wxDialog()
85 {
86 m_isBeingDeleted = true;
87 Show(false);
88 }
89
90 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
91 void wxDialog::OnCharHook(wxKeyEvent& event)
92 {
93 if (( event.m_keyCode == WXK_ESCAPE ||
94 ( event.m_keyCode == '.' && event.MetaDown() ) )
95 && FindWindow(wxID_CANCEL) )
96 {
97 // Behaviour changed in 2.0: we'll send a Cancel message
98 // to the dialog instead of Close.
99 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
100 cancelEvent.SetEventObject( this );
101 GetEventHandler()->ProcessEvent(cancelEvent);
102
103 return;
104 }
105 // We didn't process this event.
106 event.Skip();
107 }
108
109 bool wxDialog::IsModal() const
110 {
111 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
112 // return m_isModalStyle;
113 }
114
115
116 bool wxDialog::IsModalShowing() const
117 {
118 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
119 }
120
121 bool wxDialog::Show(bool show)
122 {
123 if ( !wxDialogBase::Show(show) )
124 {
125 // nothing to do
126 return false;
127 }
128
129 if ( show )
130 {
131 // usually will result in TransferDataToWindow() being called
132 InitDialog();
133 }
134
135 if ( m_isModalStyle )
136 {
137 if ( show )
138 {
139 DoShowModal();
140 }
141 else // end of modal dialog
142 {
143 // this will cause IsModalShowing() return false and our local
144 // message loop will terminate
145 wxModalDialogs.DeleteObject(this);
146 }
147 }
148
149 return true;
150 }
151
152 #if !TARGET_CARBON
153 extern bool s_macIsInModalLoop ;
154 #endif
155
156 void wxDialog::DoShowModal()
157 {
158 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
159
160 wxModalDialogs.Append(this);
161
162 SetFocus() ;
163
164 #if TARGET_CARBON
165 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
166 #else
167 // TODO : test whether parent gets disabled
168 bool formerModal = s_macIsInModalLoop ;
169 s_macIsInModalLoop = true ;
170 #endif
171 while ( IsModalShowing() )
172 {
173 wxTheApp->MacDoOneEvent() ;
174 // calls process idle itself
175 }
176
177 #if TARGET_CARBON
178 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
179 #else
180 // TODO probably reenable the parent window if any
181 s_macIsInModalLoop = formerModal ;
182 #endif
183 }
184
185
186 // Replacement for Show(true) for modal dialogs - returns return code
187 int wxDialog::ShowModal()
188 {
189 if ( !m_isModalStyle )
190 SetModal(true);
191
192 Show(true);
193 return GetReturnCode();
194 }
195
196 // NB: this function (surprizingly) may be called for both modal and modeless
197 // dialogs and should work for both of them
198 void wxDialog::EndModal(int retCode)
199 {
200 SetReturnCode(retCode);
201 Show(false);
202 SetModal(false);
203 }
204
205 // Standard buttons
206 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
207 {
208 if ( Validate() && TransferDataFromWindow() )
209 EndModal(wxID_OK);
210 }
211
212 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
213 {
214 if (Validate())
215 TransferDataFromWindow();
216
217 // TODO probably need to disable the Apply button until things change again
218 }
219
220 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
221 {
222 EndModal(wxID_CANCEL);
223 }
224
225 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
226 {
227 // We'll send a Cancel message by default,
228 // which may close the dialog.
229 // Check for looping if the Cancel event handler calls Close().
230
231 // Note that if a cancel button and handler aren't present in the dialog,
232 // nothing will happen when you close the dialog via the window manager, or
233 // via Close().
234 // We wouldn't want to destroy the dialog by default, since the dialog may have been
235 // created on the stack.
236 // However, this does mean that calling dialog->Close() won't delete the dialog
237 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
238 // sure to destroy the dialog.
239 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
240
241 static wxList closing;
242
243 if ( closing.Member(this) )
244 return;
245
246 closing.Append(this);
247
248 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
249 cancelEvent.SetEventObject( this );
250 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
251
252 closing.DeleteObject(this);
253 }
254
255 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
256 {
257 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
258 Refresh();
259 }
260