]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dialog.cpp
corrected GetPosition and extended erasing to include the entire update rgn passed
[wxWidgets.git] / src / mac / carbon / dialog.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
3// Purpose: wxDialog class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
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
d497dca4 22#include "wx/mac/uma.h"
519cb848 23
e9576ca5
SC
24// Lists to keep track of windows, so we can disable/enable them
25// for modal dialogs
26wxList wxModalDialogs;
fe08e597 27//wxList wxModelessWindows; // Frames and modeless dialogs
e9576ca5
SC
28extern wxList wxPendingDelete;
29
2f1ae414 30#if !USE_SHARED_LIBRARY
a15eb0a5 31IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
e9576ca5 32
d491523b 33BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
e9576ca5
SC
34 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
35 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
36 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
a15eb0a5 37
e9576ca5 38 EVT_CHAR_HOOK(wxDialog::OnCharHook)
a15eb0a5 39
e9576ca5 40 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
a15eb0a5 41
e9576ca5
SC
42 EVT_CLOSE(wxDialog::OnCloseWindow)
43END_EVENT_TABLE()
44
2f1ae414 45#endif
e9576ca5 46
2c326ada 47void wxDialog::Init()
e9576ca5 48{
2c326ada 49 m_isModalStyle = false;
e9576ca5
SC
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{
facd6764 59 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
eeacbb8c 60
facd6764
SC
61 // All dialogs should really have this style
62 style |= wxTAB_TRAVERSAL;
eeacbb8c 63
0f89f2f5 64 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style & ~(wxYES|wxOK|wxNO|wxCANCEL) , name) )
a15eb0a5 65 return FALSE;
eeacbb8c 66
e40298d5 67 return TRUE;
e9576ca5
SC
68}
69
70void wxDialog::SetModal(bool flag)
71{
e40298d5 72 if ( flag )
2f1ae414 73 {
2c326ada 74 m_isModalStyle = true;
eeacbb8c 75
2f1ae414 76 wxModelessWindows.DeleteObject(this);
643a0828
SC
77#if TARGET_CARBON
78 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
79#endif
2f1ae414
SC
80 }
81 else
82 {
2c326ada 83 m_isModalStyle = false;
eeacbb8c 84
2f1ae414
SC
85 wxModelessWindows.Append(this);
86 }
e9576ca5
SC
87}
88
89wxDialog::~wxDialog()
90{
4506b24a 91 m_isBeingDeleted = TRUE;
e40298d5 92 Show(FALSE);
e9576ca5
SC
93}
94
a15eb0a5 95// By default, pressing escape cancels the dialog , on mac command-stop does the same thing
e9576ca5
SC
96void wxDialog::OnCharHook(wxKeyEvent& event)
97{
eeacbb8c 98 if (( event.m_keyCode == WXK_ESCAPE ||
e40298d5
JS
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);
eeacbb8c 107
e40298d5
JS
108 return;
109 }
110 // We didn't process this event.
111 event.Skip();
e9576ca5
SC
112}
113
2f1ae414 114bool wxDialog::IsModal() const
51abe921 115{
2c326ada 116 return m_isModalStyle;
51abe921
SC
117}
118
2f1ae414
SC
119
120bool wxDialog::IsModalShowing() const
51abe921 121{
2f1ae414 122 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
51abe921
SC
123}
124
e9576ca5
SC
125bool wxDialog::Show(bool show)
126{
2f1ae414
SC
127 if ( !wxDialogBase::Show(show) )
128 {
129 // nothing to do
130 return FALSE;
131 }
e9576ca5 132
2f1ae414
SC
133 if ( show )
134 {
135 // usually will result in TransferDataToWindow() being called
136 InitDialog();
137 }
e9576ca5 138
2f1ae414 139 if ( IsModal() )
e7549107 140 {
2f1ae414
SC
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 }
e7549107 152
2f1ae414 153 return TRUE;
e9576ca5
SC
154}
155
2726cac5
SC
156#if !TARGET_CARBON
157extern bool s_macIsInModalLoop ;
158#endif
159
2f1ae414 160void wxDialog::DoShowModal()
51abe921 161{
2f1ae414 162 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
51abe921 163
2f1ae414 164 wxModalDialogs.Append(this);
51abe921 165
2726cac5 166#if TARGET_CARBON
f697130e 167 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
2726cac5 168#else
e40298d5
JS
169 // TODO : test whether parent gets disabled
170 bool formerModal = s_macIsInModalLoop ;
171 s_macIsInModalLoop = true ;
2726cac5 172#endif
e40298d5
JS
173 while ( IsModalShowing() )
174 {
175 wxTheApp->MacDoOneEvent() ;
176 // calls process idle itself
177 }
eeacbb8c 178
2726cac5 179#if TARGET_CARBON
f697130e 180 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
2726cac5
SC
181#else
182 // TODO probably reenable the parent window if any
e40298d5 183 s_macIsInModalLoop = formerModal ;
2726cac5 184#endif
51abe921 185}
519cb848 186
2f1ae414 187
e9576ca5
SC
188// Replacement for Show(TRUE) for modal dialogs - returns return code
189int wxDialog::ShowModal()
190{
e40298d5
JS
191 if ( !IsModal() )
192 {
193 SetModal(TRUE);
194 }
81b41c03 195
e40298d5
JS
196 Show(TRUE);
197 return GetReturnCode();
e9576ca5
SC
198}
199
2f1ae414
SC
200// NB: this function (surprizingly) may be called for both modal and modeless
201// dialogs and should work for both of them
e9576ca5
SC
202void wxDialog::EndModal(int retCode)
203{
51abe921
SC
204 SetReturnCode(retCode);
205 Show(FALSE);
e9576ca5
SC
206}
207
208// Standard buttons
d208e641 209void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
e9576ca5 210{
51abe921
SC
211 if ( Validate() && TransferDataFromWindow() )
212 {
2f1ae414 213 EndModal(wxID_OK);
51abe921 214 }
e9576ca5
SC
215}
216
d208e641 217void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
e9576ca5 218{
51abe921
SC
219 if (Validate())
220 TransferDataFromWindow();
221 // TODO probably need to disable the Apply button until things change again
e9576ca5
SC
222}
223
d208e641 224void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
e9576ca5 225{
d208e641 226 EndModal(wxID_CANCEL);
7c74e7fe
SC
227}
228
d208e641 229void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
e9576ca5 230{
e3065973 231 // We'll send a Cancel message by default,
e9576ca5 232 // which may close the dialog.
e3065973
JS
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.
e9576ca5
SC
244
245 static wxList closing;
51abe921 246
e9576ca5 247 if ( closing.Member(this) )
e3065973 248 return;
51abe921 249
e9576ca5 250 closing.Append(this);
51abe921 251
e3065973
JS
252 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
253 cancelEvent.SetEventObject( this );
254 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
e9576ca5
SC
255
256 closing.DeleteObject(this);
e9576ca5
SC
257}
258
d208e641 259void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
e9576ca5 260{
a756f210 261 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e9576ca5
SC
262 Refresh();
263}
264