]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dialog.cpp
f75fdfbd7a62605eee792d34249909d1228a27df
[wxWidgets.git] / src / mac / carbon / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/frame.h"
20 #include "wx/settings.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/mac/uma.h"
24
25
26 // Lists to keep track of windows, so we can disable/enable them
27 // for modal dialogs
28 wxList wxModalDialogs;
29
30 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
31
32 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
33 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
34 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
35 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
36
37 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
38
39 EVT_CLOSE(wxDialog::OnCloseWindow)
40 END_EVENT_TABLE()
41
42
43 void wxDialog::Init()
44 {
45 m_isModalStyle = false;
46 }
47
48 bool wxDialog::Create( wxWindow *parent,
49 wxWindowID id,
50 const wxString& title,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name )
55 {
56 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
57
58 // All dialogs should really have this style...
59 style |= wxTAB_TRAVERSAL;
60
61 // ...but not these styles
62 style &= ~(wxYES | wxOK | wxNO); // | wxCANCEL
63
64 if ( !wxTopLevelWindow::Create( parent, id, title, pos, size, style, name ) )
65 return false;
66
67 #if TARGET_API_MAC_OSX
68 HIViewRef growBoxRef = 0 ;
69 OSStatus err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
70 if ( err == noErr && growBoxRef != 0 )
71 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
72 #endif
73
74 return true;
75 }
76
77 void wxDialog::SetModal( bool flag )
78 {
79 if ( flag )
80 {
81 m_isModalStyle = true;
82
83 wxModelessWindows.DeleteObject( this );
84
85 #if TARGET_CARBON
86 SetWindowModality( (WindowRef)MacGetWindowRef(), kWindowModalityAppModal, NULL ) ;
87 #endif
88 }
89 else
90 {
91 m_isModalStyle = false;
92
93 wxModelessWindows.Append( this );
94 }
95 }
96
97 wxDialog::~wxDialog()
98 {
99 m_isBeingDeleted = true;
100 Show(false);
101 }
102
103 // On mac command-stop does the same thing as Esc, let the base class know
104 // about it
105 bool wxDialog::IsEscapeKey(const wxKeyEvent& event)
106 {
107 if ( event.GetKeyCode() == '.' && event.GetModifiers() == wxMOD_CMD )
108 return true;
109
110 return wxDialogBase::IsEscapeKey(event);
111 }
112
113 bool wxDialog::IsModal() const
114 {
115 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
116 // return m_isModalStyle;
117 }
118
119
120 bool wxDialog::IsModalShowing() const
121 {
122 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
123 }
124
125 bool wxDialog::Show(bool show)
126 {
127 if ( !wxDialogBase::Show(show) )
128 // nothing to do
129 return false;
130
131 if ( show )
132 // usually will result in TransferDataToWindow() being called
133 InitDialog();
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(), wxT("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
172 while ( IsModalShowing() )
173 {
174 wxTheApp->MacDoOneEvent() ;
175 // calls process idle itself
176 }
177
178 #if TARGET_CARBON
179 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
180 #else
181 // TODO probably reenable the parent window if any
182 s_macIsInModalLoop = formerModal ;
183 #endif
184 }
185
186
187 // Replacement for Show(true) for modal dialogs - returns return code
188 int wxDialog::ShowModal()
189 {
190 if ( !m_isModalStyle )
191 SetModal(true);
192
193 Show(true);
194
195 return GetReturnCode();
196 }
197
198 // NB: this function (surprizingly) may be called for both modal and modeless
199 // dialogs and should work for both of them
200 void wxDialog::EndModal(int retCode)
201 {
202 SetReturnCode(retCode);
203 Show(false);
204 SetModal(false);
205 }
206
207 // Standard buttons
208 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
209 {
210 if ( Validate() && TransferDataFromWindow() )
211 EndModal(wxID_OK);
212 }
213
214 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
215 {
216 if (Validate())
217 TransferDataFromWindow();
218
219 // TODO probably need to disable the Apply button until things change again
220 }
221
222 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
223 {
224 EndModal(wxID_CANCEL);
225 }
226
227 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
228 {
229 // We'll send a Cancel message by default,
230 // which may close the dialog.
231 // Check for looping if the Cancel event handler calls Close().
232
233 // Note that if a cancel button and handler aren't present in the dialog,
234 // nothing will happen when you close the dialog via the window manager, or
235 // via Close().
236 // We wouldn't want to destroy the dialog by default, since the dialog may have been
237 // created on the stack.
238 // However, this does mean that calling dialog->Close() won't delete the dialog
239 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
240 // sure to destroy the dialog.
241 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
242
243 static wxList closing;
244
245 if ( closing.Member(this) )
246 return;
247
248 closing.Append(this);
249
250 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
251 cancelEvent.SetEventObject( this );
252 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
253
254 closing.DeleteObject(this);
255 }
256
257 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
258 {
259 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
260 Refresh();
261 }