]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/dialog.cpp
better handling of empty MDI parent frame visibility: show it only when there are...
[wxWidgets.git] / src / mac / classic / 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 #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
22 #include "wx/mac/uma.h"
23
24 // Lists to keep track of windows, so we can disable/enable them
25 // for modal dialogs
26 wxList wxModalDialogs;
27 //wxList wxModelessWindows; // Frames and modeless dialogs
28 extern wxList wxPendingDelete;
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_CHAR_HOOK(wxDialog::OnCharHook)
38
39 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
40
41 EVT_CLOSE(wxDialog::OnCloseWindow)
42 END_EVENT_TABLE()
43
44 void wxDialog::Init()
45 {
46 m_isModalStyle = false;
47 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
48 }
49
50 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
51 const wxString& title,
52 const wxPoint& pos,
53 const wxSize& size,
54 long style,
55 const wxString& name)
56 {
57 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
58
59
60 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
61 return FALSE;
62
63 MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) & ~(wxYES|wxOK|wxNO|wxCANCEL) , name ) ;
64
65 m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ;
66 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macWindowBackgroundTheme , false ) ;
67
68 return TRUE;
69 }
70
71 void wxDialog::SetModal(bool flag)
72 {
73 if ( flag )
74 {
75 m_isModalStyle = true;
76
77 wxModelessWindows.DeleteObject(this);
78 #if TARGET_CARBON
79 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
80 #endif
81 }
82 else
83 {
84 m_isModalStyle = false;
85
86 wxModelessWindows.Append(this);
87 }
88 }
89
90 wxDialog::~wxDialog()
91 {
92 m_isBeingDeleted = TRUE;
93 Show(FALSE);
94 }
95
96 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
97 void wxDialog::OnCharHook(wxKeyEvent& event)
98 {
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();
113 }
114
115 bool wxDialog::IsModal() const
116 {
117 return m_isModalStyle;
118 }
119
120
121 bool wxDialog::IsModalShowing() const
122 {
123 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
124 }
125
126 bool wxDialog::Show(bool show)
127 {
128 if ( !wxDialogBase::Show(show) )
129 {
130 // nothing to do
131 return FALSE;
132 }
133
134 if ( show )
135 {
136 // usually will result in TransferDataToWindow() being called
137 InitDialog();
138 }
139
140 if ( IsModal() )
141 {
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 }
153
154 return TRUE;
155 }
156
157 #if !TARGET_CARBON
158 extern bool s_macIsInModalLoop ;
159 #endif
160
161 void wxDialog::DoShowModal()
162 {
163 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
164
165 wxModalDialogs.Append(this);
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