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