]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dialog.cpp
applying OSAF patch for "toolbar selection not correct on Mac after calling Realize...
[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
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "dialog.h"
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
e9576ca5
SC
18#include "wx/dialog.h"
19#include "wx/utils.h"
20#include "wx/frame.h"
21#include "wx/app.h"
22#include "wx/settings.h"
23
d497dca4 24#include "wx/mac/uma.h"
519cb848 25
e9576ca5
SC
26// Lists to keep track of windows, so we can disable/enable them
27// for modal dialogs
28wxList wxModalDialogs;
fe08e597 29//wxList wxModelessWindows; // Frames and modeless dialogs
e9576ca5
SC
30extern wxList wxPendingDelete;
31
2f1ae414 32#if !USE_SHARED_LIBRARY
a15eb0a5 33IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
e9576ca5 34
d491523b 35BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
e9576ca5
SC
36 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
37 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
38 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
a15eb0a5 39
e9576ca5 40 EVT_CHAR_HOOK(wxDialog::OnCharHook)
a15eb0a5 41
e9576ca5 42 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
a15eb0a5 43
e9576ca5
SC
44 EVT_CLOSE(wxDialog::OnCloseWindow)
45END_EVENT_TABLE()
46
2f1ae414 47#endif
e9576ca5 48
2c326ada 49void wxDialog::Init()
e9576ca5 50{
2c326ada 51 m_isModalStyle = false;
e9576ca5
SC
52}
53
54bool wxDialog::Create(wxWindow *parent, wxWindowID id,
55 const wxString& title,
56 const wxPoint& pos,
57 const wxSize& size,
58 long style,
59 const wxString& name)
60{
facd6764 61 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
eeacbb8c 62
facd6764
SC
63 // All dialogs should really have this style
64 style |= wxTAB_TRAVERSAL;
eeacbb8c 65
0f89f2f5 66 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style & ~(wxYES|wxOK|wxNO|wxCANCEL) , name) )
a15eb0a5 67 return FALSE;
eeacbb8c 68
e40298d5 69 return TRUE;
e9576ca5
SC
70}
71
72void wxDialog::SetModal(bool flag)
73{
e40298d5 74 if ( flag )
2f1ae414 75 {
2c326ada 76 m_isModalStyle = true;
eeacbb8c 77
2f1ae414 78 wxModelessWindows.DeleteObject(this);
643a0828
SC
79#if TARGET_CARBON
80 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
81#endif
2f1ae414
SC
82 }
83 else
84 {
2c326ada 85 m_isModalStyle = false;
eeacbb8c 86
2f1ae414
SC
87 wxModelessWindows.Append(this);
88 }
e9576ca5
SC
89}
90
91wxDialog::~wxDialog()
92{
4506b24a 93 m_isBeingDeleted = TRUE;
e40298d5 94 Show(FALSE);
e9576ca5
SC
95}
96
a15eb0a5 97// By default, pressing escape cancels the dialog , on mac command-stop does the same thing
e9576ca5
SC
98void wxDialog::OnCharHook(wxKeyEvent& event)
99{
eeacbb8c 100 if (( event.m_keyCode == WXK_ESCAPE ||
e40298d5
JS
101 ( event.m_keyCode == '.' && event.MetaDown() ) )
102 && FindWindow(wxID_CANCEL) )
103 {
104 // Behaviour changed in 2.0: we'll send a Cancel message
105 // to the dialog instead of Close.
106 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
107 cancelEvent.SetEventObject( this );
108 GetEventHandler()->ProcessEvent(cancelEvent);
eeacbb8c 109
e40298d5
JS
110 return;
111 }
112 // We didn't process this event.
113 event.Skip();
e9576ca5
SC
114}
115
2f1ae414 116bool wxDialog::IsModal() const
51abe921 117{
2c326ada 118 return m_isModalStyle;
51abe921
SC
119}
120
2f1ae414
SC
121
122bool wxDialog::IsModalShowing() const
51abe921 123{
2f1ae414 124 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
51abe921
SC
125}
126
e9576ca5
SC
127bool wxDialog::Show(bool show)
128{
2f1ae414
SC
129 if ( !wxDialogBase::Show(show) )
130 {
131 // nothing to do
132 return FALSE;
133 }
e9576ca5 134
2f1ae414
SC
135 if ( show )
136 {
137 // usually will result in TransferDataToWindow() being called
138 InitDialog();
139 }
e9576ca5 140
2f1ae414 141 if ( IsModal() )
e7549107 142 {
2f1ae414
SC
143 if ( show )
144 {
145 DoShowModal();
146 }
147 else // end of modal dialog
148 {
149 // this will cause IsModalShowing() return FALSE and our local
150 // message loop will terminate
151 wxModalDialogs.DeleteObject(this);
152 }
153 }
e7549107 154
2f1ae414 155 return TRUE;
e9576ca5
SC
156}
157
2726cac5
SC
158#if !TARGET_CARBON
159extern bool s_macIsInModalLoop ;
160#endif
161
2f1ae414 162void wxDialog::DoShowModal()
51abe921 163{
2f1ae414 164 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
51abe921 165
2f1ae414 166 wxModalDialogs.Append(this);
51abe921 167
90b78a56
SC
168 SetFocus() ;
169
2726cac5 170#if TARGET_CARBON
f697130e 171 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
2726cac5 172#else
e40298d5
JS
173 // TODO : test whether parent gets disabled
174 bool formerModal = s_macIsInModalLoop ;
175 s_macIsInModalLoop = true ;
2726cac5 176#endif
e40298d5
JS
177 while ( IsModalShowing() )
178 {
179 wxTheApp->MacDoOneEvent() ;
180 // calls process idle itself
181 }
eeacbb8c 182
2726cac5 183#if TARGET_CARBON
f697130e 184 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
2726cac5
SC
185#else
186 // TODO probably reenable the parent window if any
e40298d5 187 s_macIsInModalLoop = formerModal ;
2726cac5 188#endif
51abe921 189}
519cb848 190
2f1ae414 191
e9576ca5
SC
192// Replacement for Show(TRUE) for modal dialogs - returns return code
193int wxDialog::ShowModal()
194{
e40298d5
JS
195 if ( !IsModal() )
196 {
197 SetModal(TRUE);
198 }
81b41c03 199
e40298d5
JS
200 Show(TRUE);
201 return GetReturnCode();
e9576ca5
SC
202}
203
2f1ae414
SC
204// NB: this function (surprizingly) may be called for both modal and modeless
205// dialogs and should work for both of them
e9576ca5
SC
206void wxDialog::EndModal(int retCode)
207{
51abe921
SC
208 SetReturnCode(retCode);
209 Show(FALSE);
e9576ca5
SC
210}
211
212// Standard buttons
d208e641 213void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
e9576ca5 214{
51abe921
SC
215 if ( Validate() && TransferDataFromWindow() )
216 {
2f1ae414 217 EndModal(wxID_OK);
51abe921 218 }
e9576ca5
SC
219}
220
d208e641 221void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
e9576ca5 222{
51abe921
SC
223 if (Validate())
224 TransferDataFromWindow();
225 // TODO probably need to disable the Apply button until things change again
e9576ca5
SC
226}
227
d208e641 228void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
e9576ca5 229{
d208e641 230 EndModal(wxID_CANCEL);
7c74e7fe
SC
231}
232
d208e641 233void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
e9576ca5 234{
e3065973 235 // We'll send a Cancel message by default,
e9576ca5 236 // which may close the dialog.
e3065973
JS
237 // Check for looping if the Cancel event handler calls Close().
238
239 // Note that if a cancel button and handler aren't present in the dialog,
240 // nothing will happen when you close the dialog via the window manager, or
241 // via Close().
242 // We wouldn't want to destroy the dialog by default, since the dialog may have been
243 // created on the stack.
244 // However, this does mean that calling dialog->Close() won't delete the dialog
245 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
246 // sure to destroy the dialog.
247 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
e9576ca5
SC
248
249 static wxList closing;
51abe921 250
e9576ca5 251 if ( closing.Member(this) )
e3065973 252 return;
51abe921 253
e9576ca5 254 closing.Append(this);
51abe921 255
e3065973
JS
256 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
257 cancelEvent.SetEventObject( this );
258 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
e9576ca5
SC
259
260 closing.DeleteObject(this);
e9576ca5
SC
261}
262
d208e641 263void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
e9576ca5 264{
a756f210 265 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e9576ca5
SC
266 Refresh();
267}
268