]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dialog.cpp
corrected redraw problems with native controls
[wxWidgets.git] / src / mac / dialog.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
3// Purpose: wxDialog class
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
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
519cb848
SC
22#include <wx/mac/uma.h>
23
e9576ca5
SC
24// Lists to keep track of windows, so we can disable/enable them
25// for modal dialogs
26wxList wxModalDialogs;
27wxList wxModelessWindows; // Frames and modeless dialogs
28extern wxList wxPendingDelete;
29
2f1ae414 30#if !USE_SHARED_LIBRARY
e9576ca5
SC
31IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
32
33BEGIN_EVENT_TABLE(wxDialog, wxPanel)
519cb848 34 EVT_SIZE(wxDialog::OnSize)
e9576ca5
SC
35 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
36 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
37 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
38 EVT_CHAR_HOOK(wxDialog::OnCharHook)
39 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
40 EVT_CLOSE(wxDialog::OnCloseWindow)
41END_EVENT_TABLE()
42
2f1ae414 43#endif
e9576ca5
SC
44
45wxDialog::wxDialog()
46{
2f1ae414 47 m_isShown = FALSE;
e9576ca5
SC
48 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
49}
50
51bool wxDialog::Create(wxWindow *parent, wxWindowID id,
52 const wxString& title,
53 const wxPoint& pos,
54 const wxSize& size,
55 long style,
56 const wxString& name)
57{
e9576ca5
SC
58
59 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
e9576ca5
SC
60
61 if (!parent)
62 wxTopLevelWindows.Append(this);
63
64 if (parent) parent->AddChild(this);
65
66 if ( id == -1 )
67 m_windowId = (int)NewControlId();
68 else
69 m_windowId = id;
70
2f1ae414 71 MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) , name ) ;
e9576ca5 72
519cb848 73 m_macWindowData->m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ;
519cb848 74 return TRUE;
e9576ca5
SC
75}
76
77void wxDialog::SetModal(bool flag)
78{
2f1ae414
SC
79 if ( flag )
80 {
81 m_windowStyle |= wxDIALOG_MODAL;
82
83 wxModelessWindows.DeleteObject(this);
84 }
85 else
86 {
87 m_windowStyle &= ~wxDIALOG_MODAL;
88
89 wxModelessWindows.Append(this);
90 }
e9576ca5
SC
91}
92
93wxDialog::~wxDialog()
94{
e7549107 95 m_isBeingDeleted = TRUE ;
e9576ca5
SC
96 wxTopLevelWindows.DeleteObject(this);
97
2f1ae414 98 Show(FALSE);
e7549107 99
2f1ae414
SC
100 if ( !IsModal() )
101 wxModelessWindows.DeleteObject(this);
e9576ca5
SC
102
103 // If this is the last top-level window, exit.
104 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
105 {
106 wxTheApp->SetTopWindow(NULL);
107
108 if (wxTheApp->GetExitOnFrameDelete())
109 {
519cb848 110 wxTheApp->ExitMainLoop() ;
e9576ca5
SC
111 }
112 }
113}
114
115// By default, pressing escape cancels the dialog
116void wxDialog::OnCharHook(wxKeyEvent& event)
117{
118 if (event.m_keyCode == WXK_ESCAPE)
119 {
120 // Behaviour changed in 2.0: we'll send a Cancel message
121 // to the dialog instead of Close.
122 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
123 cancelEvent.SetEventObject( this );
124 GetEventHandler()->ProcessEvent(cancelEvent);
125
126 return;
127 }
128 // We didn't process this event.
129 event.Skip();
130}
131
132void wxDialog::Iconize(bool WXUNUSED(iconize))
133{
51abe921 134 // mac dialogs cannot be iconized
e9576ca5
SC
135}
136
137bool wxDialog::IsIconized() const
138{
51abe921 139 // mac dialogs cannot be iconized
e9576ca5
SC
140 return FALSE;
141}
142
51abe921
SC
143void wxDialog::DoSetClientSize(int width, int height)
144{
145 wxWindow::DoSetClientSize( width , height ) ;
146}
147
2f1ae414 148void wxDialog::DoGetPosition(int *x, int *y) const
51abe921 149{
2f1ae414 150 wxWindow::DoGetPosition( x , y ) ;
51abe921
SC
151}
152
2f1ae414 153bool wxDialog::IsModal() const
51abe921 154{
2f1ae414 155 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
51abe921
SC
156}
157
2f1ae414
SC
158
159bool wxDialog::IsModalShowing() const
51abe921 160{
2f1ae414 161 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
51abe921
SC
162}
163
164
519cb848 165extern bool s_macIsInModalLoop ;
e9576ca5
SC
166
167bool wxDialog::Show(bool show)
168{
2f1ae414
SC
169 if ( !wxDialogBase::Show(show) )
170 {
171 // nothing to do
172 return FALSE;
173 }
e9576ca5 174
2f1ae414
SC
175 if ( show )
176 {
177 // usually will result in TransferDataToWindow() being called
178 InitDialog();
179 }
e9576ca5 180
2f1ae414 181 if ( IsModal() )
e7549107 182 {
2f1ae414
SC
183 if ( show )
184 {
185 DoShowModal();
186 }
187 else // end of modal dialog
188 {
189 // this will cause IsModalShowing() return FALSE and our local
190 // message loop will terminate
191 wxModalDialogs.DeleteObject(this);
192 }
193 }
e7549107 194
2f1ae414 195 return TRUE;
e9576ca5
SC
196}
197
2f1ae414 198void wxDialog::DoShowModal()
51abe921 199{
2f1ae414 200 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
51abe921 201
2f1ae414 202 wxModalDialogs.Append(this);
51abe921 203
2f1ae414
SC
204 wxWindow *parent = GetParent();
205
206 // remember where the focus was
207 wxWindow *winFocus = FindFocus();
208 if ( !winFocus )
209 {
210 winFocus = parent;
211 }
212 if ( !winFocus )
213 {
214 winFocus = wxTheApp->GetTopWindow();
215 }
216 // TODO : test whether parent gets disabled
217
218 s_macIsInModalLoop = true ;
51abe921 219
2f1ae414
SC
220 while ( IsModalShowing() )
221 {
222 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
223 {
224 }
225 wxTheApp->MacDoOneEvent() ;
226 }
227
228 s_macIsInModalLoop = false ;
51abe921 229
2f1ae414 230 // TODO probably reenable the parent window if any
51abe921 231
2f1ae414
SC
232 // and restore focus
233 if ( winFocus )
234 {
235 winFocus->SetFocus();
236 }
51abe921 237}
519cb848 238
2f1ae414 239
e9576ca5
SC
240// Replacement for Show(TRUE) for modal dialogs - returns return code
241int wxDialog::ShowModal()
242{
519cb848 243 m_windowStyle |= wxDIALOG_MODAL;
51abe921
SC
244 Show(TRUE);
245 return GetReturnCode();
e9576ca5
SC
246}
247
2f1ae414
SC
248// NB: this function (surprizingly) may be called for both modal and modeless
249// dialogs and should work for both of them
e9576ca5
SC
250void wxDialog::EndModal(int retCode)
251{
51abe921
SC
252 SetReturnCode(retCode);
253 Show(FALSE);
e9576ca5
SC
254}
255
256// Standard buttons
257void wxDialog::OnOK(wxCommandEvent& event)
258{
51abe921
SC
259 if ( Validate() && TransferDataFromWindow() )
260 {
2f1ae414 261 EndModal(wxID_OK);
51abe921 262 }
e9576ca5
SC
263}
264
265void wxDialog::OnApply(wxCommandEvent& event)
266{
51abe921
SC
267 if (Validate())
268 TransferDataFromWindow();
269 // TODO probably need to disable the Apply button until things change again
e9576ca5
SC
270}
271
272void wxDialog::OnCancel(wxCommandEvent& event)
273{
e9576ca5 274 EndModal(wxID_CANCEL);
7c74e7fe
SC
275}
276
e3065973 277void wxDialog::OnCloseWindow(wxCloseEvent& event)
e9576ca5 278{
e3065973 279 // We'll send a Cancel message by default,
e9576ca5 280 // which may close the dialog.
e3065973
JS
281 // Check for looping if the Cancel event handler calls Close().
282
283 // Note that if a cancel button and handler aren't present in the dialog,
284 // nothing will happen when you close the dialog via the window manager, or
285 // via Close().
286 // We wouldn't want to destroy the dialog by default, since the dialog may have been
287 // created on the stack.
288 // However, this does mean that calling dialog->Close() won't delete the dialog
289 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
290 // sure to destroy the dialog.
291 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
e9576ca5
SC
292
293 static wxList closing;
51abe921 294
e9576ca5 295 if ( closing.Member(this) )
e3065973 296 return;
51abe921 297
e9576ca5 298 closing.Append(this);
51abe921 299
e3065973
JS
300 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
301 cancelEvent.SetEventObject( this );
302 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
e9576ca5
SC
303
304 closing.DeleteObject(this);
e9576ca5
SC
305}
306
307// Destroy the window (delayed, if a managed window)
308bool wxDialog::Destroy()
309{
2f1ae414
SC
310 wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE,
311 _T("wxDialog destroyed twice") );
312
e9576ca5
SC
313 wxPendingDelete.Append(this);
314 return TRUE;
315}
316
317void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
318{
319 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
320 Refresh();
321}
322