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