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