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