]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/dialog.cpp
Rich text lib separation.
[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"
9eddec69 20 #include "wx/settings.h"
670f9935
WS
21#endif // WX_PRECOMP
22
2646f485
SC
23#include "wx/mac/uma.h"
24
25// Lists to keep track of windows, so we can disable/enable them
26// for modal dialogs
27wxList wxModalDialogs;
2646f485 28
2646f485
SC
29IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
30
31BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
32 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
33 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
34 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
35
36 EVT_CHAR_HOOK(wxDialog::OnCharHook)
37
38 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
39
40 EVT_CLOSE(wxDialog::OnCloseWindow)
41END_EVENT_TABLE()
42
6bc3b8e9 43void wxDialog::Init()
2646f485 44{
70cbdcca 45 m_isModalStyle = false;
2646f485
SC
46 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
47}
48
49bool wxDialog::Create(wxWindow *parent, wxWindowID id,
50 const wxString& title,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name)
55{
56 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
57
58
59 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
670f9935 60 return false;
2646f485
SC
61
62 MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) & ~(wxYES|wxOK|wxNO|wxCANCEL) , name ) ;
63
64 m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ;
65 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macWindowBackgroundTheme , false ) ;
66
670f9935 67 return true;
2646f485
SC
68}
69
70void wxDialog::SetModal(bool flag)
71{
72 if ( flag )
73 {
70cbdcca 74 m_isModalStyle = true;
2646f485
SC
75
76 wxModelessWindows.DeleteObject(this);
77#if TARGET_CARBON
78 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
79#endif
80 }
81 else
82 {
70cbdcca 83 m_isModalStyle = false;
2646f485
SC
84
85 wxModelessWindows.Append(this);
86 }
87}
88
89wxDialog::~wxDialog()
90{
670f9935
WS
91 m_isBeingDeleted = true;
92 Show(false);
2646f485
SC
93}
94
95// By default, pressing escape cancels the dialog , on mac command-stop does the same thing
96void wxDialog::OnCharHook(wxKeyEvent& event)
97{
98 if (( event.m_keyCode == WXK_ESCAPE ||
99 ( event.m_keyCode == '.' && event.MetaDown() ) )
100 && FindWindow(wxID_CANCEL) )
101 {
102 // Behaviour changed in 2.0: we'll send a Cancel message
103 // to the dialog instead of Close.
104 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
105 cancelEvent.SetEventObject( this );
106 GetEventHandler()->ProcessEvent(cancelEvent);
107
108 return;
109 }
110 // We didn't process this event.
111 event.Skip();
112}
113
114bool wxDialog::IsModal() const
115{
70cbdcca 116 return m_isModalStyle;
2646f485
SC
117}
118
119
120bool wxDialog::IsModalShowing() const
121{
122 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
123}
124
125bool wxDialog::Show(bool show)
126{
127 if ( !wxDialogBase::Show(show) )
128 {
129 // nothing to do
670f9935 130 return false;
2646f485
SC
131 }
132
133 if ( show )
134 {
135 // usually will result in TransferDataToWindow() being called
136 InitDialog();
137 }
138
139 if ( IsModal() )
140 {
141 if ( show )
142 {
143 DoShowModal();
144 }
145 else // end of modal dialog
146 {
670f9935 147 // this will cause IsModalShowing() return false and our local
2646f485
SC
148 // message loop will terminate
149 wxModalDialogs.DeleteObject(this);
150 }
151 }
152
670f9935 153 return true;
2646f485
SC
154}
155
156#if !TARGET_CARBON
157extern bool s_macIsInModalLoop ;
158#endif
159
160void wxDialog::DoShowModal()
161{
162 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
163
164 wxModalDialogs.Append(this);
165
166#if TARGET_CARBON
167 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
168#else
169 // TODO : test whether parent gets disabled
170 bool formerModal = s_macIsInModalLoop ;
171 s_macIsInModalLoop = true ;
172#endif
173 while ( IsModalShowing() )
174 {
175 wxTheApp->MacDoOneEvent() ;
176 // calls process idle itself
177 }
178
179#if TARGET_CARBON
180 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
181#else
182 // TODO probably reenable the parent window if any
183 s_macIsInModalLoop = formerModal ;
184#endif
185}
186
187
670f9935 188// Replacement for Show(true) for modal dialogs - returns return code
2646f485
SC
189int wxDialog::ShowModal()
190{
191 if ( !IsModal() )
192 {
670f9935 193 SetModal(true);
2646f485
SC
194 }
195
670f9935 196 Show(true);
2646f485
SC
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
202void wxDialog::EndModal(int retCode)
203{
670f9935
WS
204 SetReturnCode(retCode);
205 Show(false);
2646f485
SC
206}
207
208// Standard buttons
209void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
210{
211 if ( Validate() && TransferDataFromWindow() )
212 {
213 EndModal(wxID_OK);
214 }
215}
216
217void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
218{
219 if (Validate())
220 TransferDataFromWindow();
221 // TODO probably need to disable the Apply button until things change again
222}
223
224void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
225{
226 EndModal(wxID_CANCEL);
227}
228
229void 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
259void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
260{
261 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
262 Refresh();
263}