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