]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dialog.cpp
added test for wxScopeGuard
[wxWidgets.git] / src / mac / carbon / dialog.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
3// Purpose: wxDialog class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "dialog.h"
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
e9576ca5
SC
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
d497dca4 24#include "wx/mac/uma.h"
519cb848 25
e9576ca5
SC
26// Lists to keep track of windows, so we can disable/enable them
27// for modal dialogs
28wxList wxModalDialogs;
fe08e597 29//wxList wxModelessWindows; // Frames and modeless dialogs
e9576ca5
SC
30extern wxList wxPendingDelete;
31
a15eb0a5 32IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
e9576ca5 33
d491523b 34BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
e9576ca5
SC
35 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
36 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
37 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
a15eb0a5 38
e9576ca5 39 EVT_CHAR_HOOK(wxDialog::OnCharHook)
a15eb0a5 40
e9576ca5 41 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
a15eb0a5 42
e9576ca5
SC
43 EVT_CLOSE(wxDialog::OnCloseWindow)
44END_EVENT_TABLE()
45
2c326ada 46void wxDialog::Init()
e9576ca5 47{
2c326ada 48 m_isModalStyle = false;
e9576ca5
SC
49}
50
51bool 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{
facd6764 58 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
eeacbb8c 59
facd6764
SC
60 // All dialogs should really have this style
61 style |= wxTAB_TRAVERSAL;
eeacbb8c 62
0f89f2f5 63 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style & ~(wxYES|wxOK|wxNO|wxCANCEL) , name) )
a15eb0a5 64 return FALSE;
eeacbb8c 65
e40298d5 66 return TRUE;
e9576ca5
SC
67}
68
69void wxDialog::SetModal(bool flag)
70{
e40298d5 71 if ( flag )
2f1ae414 72 {
2c326ada 73 m_isModalStyle = true;
eeacbb8c 74
2f1ae414 75 wxModelessWindows.DeleteObject(this);
643a0828
SC
76#if TARGET_CARBON
77 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
78#endif
2f1ae414
SC
79 }
80 else
81 {
2c326ada 82 m_isModalStyle = false;
eeacbb8c 83
2f1ae414
SC
84 wxModelessWindows.Append(this);
85 }
e9576ca5
SC
86}
87
88wxDialog::~wxDialog()
89{
4506b24a 90 m_isBeingDeleted = TRUE;
e40298d5 91 Show(FALSE);
e9576ca5
SC
92}
93
a15eb0a5 94// By default, pressing escape cancels the dialog , on mac command-stop does the same thing
e9576ca5
SC
95void wxDialog::OnCharHook(wxKeyEvent& event)
96{
eeacbb8c 97 if (( event.m_keyCode == WXK_ESCAPE ||
e40298d5
JS
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);
eeacbb8c 106
e40298d5
JS
107 return;
108 }
109 // We didn't process this event.
110 event.Skip();
e9576ca5
SC
111}
112
2f1ae414 113bool wxDialog::IsModal() const
51abe921 114{
2c326ada 115 return m_isModalStyle;
51abe921
SC
116}
117
2f1ae414
SC
118
119bool wxDialog::IsModalShowing() const
51abe921 120{
2f1ae414 121 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
51abe921
SC
122}
123
e9576ca5
SC
124bool wxDialog::Show(bool show)
125{
2f1ae414
SC
126 if ( !wxDialogBase::Show(show) )
127 {
128 // nothing to do
129 return FALSE;
130 }
e9576ca5 131
2f1ae414
SC
132 if ( show )
133 {
134 // usually will result in TransferDataToWindow() being called
135 InitDialog();
136 }
e9576ca5 137
2f1ae414 138 if ( IsModal() )
e7549107 139 {
2f1ae414
SC
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 }
e7549107 151
2f1ae414 152 return TRUE;
e9576ca5
SC
153}
154
2726cac5
SC
155#if !TARGET_CARBON
156extern bool s_macIsInModalLoop ;
157#endif
158
2f1ae414 159void wxDialog::DoShowModal()
51abe921 160{
2f1ae414 161 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
51abe921 162
2f1ae414 163 wxModalDialogs.Append(this);
51abe921 164
90b78a56
SC
165 SetFocus() ;
166
2726cac5 167#if TARGET_CARBON
f697130e 168 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
2726cac5 169#else
e40298d5
JS
170 // TODO : test whether parent gets disabled
171 bool formerModal = s_macIsInModalLoop ;
172 s_macIsInModalLoop = true ;
2726cac5 173#endif
e40298d5
JS
174 while ( IsModalShowing() )
175 {
176 wxTheApp->MacDoOneEvent() ;
177 // calls process idle itself
178 }
eeacbb8c 179
2726cac5 180#if TARGET_CARBON
f697130e 181 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
2726cac5
SC
182#else
183 // TODO probably reenable the parent window if any
e40298d5 184 s_macIsInModalLoop = formerModal ;
2726cac5 185#endif
51abe921 186}
519cb848 187
2f1ae414 188
e9576ca5
SC
189// Replacement for Show(TRUE) for modal dialogs - returns return code
190int wxDialog::ShowModal()
191{
e40298d5
JS
192 if ( !IsModal() )
193 {
194 SetModal(TRUE);
195 }
81b41c03 196
e40298d5
JS
197 Show(TRUE);
198 return GetReturnCode();
e9576ca5
SC
199}
200
2f1ae414
SC
201// NB: this function (surprizingly) may be called for both modal and modeless
202// dialogs and should work for both of them
e9576ca5
SC
203void wxDialog::EndModal(int retCode)
204{
51abe921
SC
205 SetReturnCode(retCode);
206 Show(FALSE);
e9576ca5
SC
207}
208
209// Standard buttons
d208e641 210void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
e9576ca5 211{
51abe921
SC
212 if ( Validate() && TransferDataFromWindow() )
213 {
2f1ae414 214 EndModal(wxID_OK);
51abe921 215 }
e9576ca5
SC
216}
217
d208e641 218void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
e9576ca5 219{
51abe921
SC
220 if (Validate())
221 TransferDataFromWindow();
222 // TODO probably need to disable the Apply button until things change again
e9576ca5
SC
223}
224
d208e641 225void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
e9576ca5 226{
d208e641 227 EndModal(wxID_CANCEL);
7c74e7fe
SC
228}
229
d208e641 230void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
e9576ca5 231{
e3065973 232 // We'll send a Cancel message by default,
e9576ca5 233 // which may close the dialog.
e3065973
JS
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.
e9576ca5
SC
245
246 static wxList closing;
51abe921 247
e9576ca5 248 if ( closing.Member(this) )
e3065973 249 return;
51abe921 250
e9576ca5 251 closing.Append(this);
51abe921 252
e3065973
JS
253 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
254 cancelEvent.SetEventObject( this );
255 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
e9576ca5
SC
256
257 closing.DeleteObject(this);
e9576ca5
SC
258}
259
d208e641 260void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
e9576ca5 261{
a756f210 262 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e9576ca5
SC
263 Refresh();
264}
265