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