]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dialog.cpp
fixed memory leak and optimized GetSubBitmap (Kudos to Eric Lavigne)
[wxWidgets.git] / src / mac / carbon / 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
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
a15eb0a5 33BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
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
SC
46
47wxDialog::wxDialog()
48{
2f1ae414 49 m_isShown = FALSE;
e9576ca5
SC
50 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
51}
52
53bool wxDialog::Create(wxWindow *parent, wxWindowID id,
54 const wxString& title,
55 const wxPoint& pos,
56 const wxSize& size,
57 long style,
58 const wxString& name)
59{
e9576ca5
SC
60
61 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
e9576ca5 62
e9576ca5 63
a15eb0a5
SC
64 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
65 return FALSE;
e9576ca5 66
2f1ae414 67 MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) , name ) ;
e9576ca5 68
519cb848 69 m_macWindowData->m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ;
519cb848 70 return TRUE;
e9576ca5
SC
71}
72
73void wxDialog::SetModal(bool flag)
74{
2f1ae414
SC
75 if ( flag )
76 {
77 m_windowStyle |= wxDIALOG_MODAL;
78
79 wxModelessWindows.DeleteObject(this);
80 }
81 else
82 {
83 m_windowStyle &= ~wxDIALOG_MODAL;
84
85 wxModelessWindows.Append(this);
86 }
e9576ca5
SC
87}
88
89wxDialog::~wxDialog()
90{
e7549107 91 m_isBeingDeleted = TRUE ;
a15eb0a5 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{
a15eb0a5
SC
98 if (
99 ( event.m_keyCode == WXK_ESCAPE ||
100 ( event.m_keyCode == '.' && event.MetaDown() ) )
101 && FindWindow(wxID_CANCEL) )
e9576ca5
SC
102 {
103 // Behaviour changed in 2.0: we'll send a Cancel message
104 // to the dialog instead of Close.
105 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
106 cancelEvent.SetEventObject( this );
107 GetEventHandler()->ProcessEvent(cancelEvent);
108
109 return;
110 }
111 // We didn't process this event.
112 event.Skip();
113}
114
2f1ae414 115bool wxDialog::IsModal() const
51abe921 116{
2f1ae414 117 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
51abe921
SC
118}
119
2f1ae414
SC
120
121bool wxDialog::IsModalShowing() const
51abe921 122{
2f1ae414 123 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
51abe921
SC
124}
125
519cb848 126extern bool s_macIsInModalLoop ;
e9576ca5
SC
127
128bool wxDialog::Show(bool show)
129{
2f1ae414
SC
130 if ( !wxDialogBase::Show(show) )
131 {
132 // nothing to do
133 return FALSE;
134 }
e9576ca5 135
2f1ae414
SC
136 if ( show )
137 {
138 // usually will result in TransferDataToWindow() being called
139 InitDialog();
140 }
e9576ca5 141
2f1ae414 142 if ( IsModal() )
e7549107 143 {
2f1ae414
SC
144 if ( show )
145 {
146 DoShowModal();
147 }
148 else // end of modal dialog
149 {
150 // this will cause IsModalShowing() return FALSE and our local
151 // message loop will terminate
152 wxModalDialogs.DeleteObject(this);
153 }
154 }
e7549107 155
2f1ae414 156 return TRUE;
e9576ca5
SC
157}
158
2f1ae414 159void wxDialog::DoShowModal()
51abe921 160{
2f1ae414 161 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
51abe921 162
2f1ae414 163 wxModalDialogs.Append(this);
51abe921 164
2f1ae414
SC
165 wxWindow *parent = GetParent();
166
167 // remember where the focus was
168 wxWindow *winFocus = FindFocus();
169 if ( !winFocus )
170 {
171 winFocus = parent;
172 }
173 if ( !winFocus )
174 {
175 winFocus = wxTheApp->GetTopWindow();
176 }
177 // TODO : test whether parent gets disabled
178
a49afa93 179 bool formerModal = s_macIsInModalLoop ;
2f1ae414 180 s_macIsInModalLoop = true ;
51abe921 181
2f1ae414
SC
182 while ( IsModalShowing() )
183 {
184 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
185 {
186 }
187 wxTheApp->MacDoOneEvent() ;
188 }
189
a49afa93 190 s_macIsInModalLoop = formerModal ;
51abe921 191
2f1ae414 192 // TODO probably reenable the parent window if any
51abe921 193
2f1ae414
SC
194 // and restore focus
195 if ( winFocus )
196 {
197 winFocus->SetFocus();
198 }
51abe921 199}
519cb848 200
2f1ae414 201
e9576ca5
SC
202// Replacement for Show(TRUE) for modal dialogs - returns return code
203int wxDialog::ShowModal()
204{
519cb848 205 m_windowStyle |= wxDIALOG_MODAL;
51abe921
SC
206 Show(TRUE);
207 return GetReturnCode();
e9576ca5
SC
208}
209
2f1ae414
SC
210// NB: this function (surprizingly) may be called for both modal and modeless
211// dialogs and should work for both of them
e9576ca5
SC
212void wxDialog::EndModal(int retCode)
213{
51abe921
SC
214 SetReturnCode(retCode);
215 Show(FALSE);
e9576ca5
SC
216}
217
218// Standard buttons
219void wxDialog::OnOK(wxCommandEvent& event)
220{
51abe921
SC
221 if ( Validate() && TransferDataFromWindow() )
222 {
2f1ae414 223 EndModal(wxID_OK);
51abe921 224 }
e9576ca5
SC
225}
226
227void wxDialog::OnApply(wxCommandEvent& event)
228{
51abe921
SC
229 if (Validate())
230 TransferDataFromWindow();
231 // TODO probably need to disable the Apply button until things change again
e9576ca5
SC
232}
233
234void wxDialog::OnCancel(wxCommandEvent& event)
235{
e9576ca5 236 EndModal(wxID_CANCEL);
7c74e7fe
SC
237}
238
e3065973 239void wxDialog::OnCloseWindow(wxCloseEvent& event)
e9576ca5 240{
e3065973 241 // We'll send a Cancel message by default,
e9576ca5 242 // which may close the dialog.
e3065973
JS
243 // Check for looping if the Cancel event handler calls Close().
244
245 // Note that if a cancel button and handler aren't present in the dialog,
246 // nothing will happen when you close the dialog via the window manager, or
247 // via Close().
248 // We wouldn't want to destroy the dialog by default, since the dialog may have been
249 // created on the stack.
250 // However, this does mean that calling dialog->Close() won't delete the dialog
251 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
252 // sure to destroy the dialog.
253 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
e9576ca5
SC
254
255 static wxList closing;
51abe921 256
e9576ca5 257 if ( closing.Member(this) )
e3065973 258 return;
51abe921 259
e9576ca5 260 closing.Append(this);
51abe921 261
e3065973
JS
262 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
263 cancelEvent.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
e9576ca5
SC
265
266 closing.DeleteObject(this);
e9576ca5
SC
267}
268
e9576ca5
SC
269void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
270{
271 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
272 Refresh();
273}
274