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