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