]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dialog.cpp
mouse handling fixed, skipping if we have no native control handling
[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
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{
facd6764 60 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
eeacbb8c 61
facd6764
SC
62 // All dialogs should really have this style
63 style |= wxTAB_TRAVERSAL;
eeacbb8c 64
e40298d5 65 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
a15eb0a5 66 return FALSE;
eeacbb8c 67
facd6764 68 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e9576ca5 69
e40298d5 70 return TRUE;
e9576ca5
SC
71}
72
73void wxDialog::SetModal(bool flag)
74{
e40298d5 75 if ( flag )
2f1ae414
SC
76 {
77 m_windowStyle |= wxDIALOG_MODAL;
eeacbb8c 78
2f1ae414 79 wxModelessWindows.DeleteObject(this);
643a0828
SC
80#if TARGET_CARBON
81 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
82#endif
2f1ae414
SC
83 }
84 else
85 {
86 m_windowStyle &= ~wxDIALOG_MODAL;
eeacbb8c 87
2f1ae414
SC
88 wxModelessWindows.Append(this);
89 }
e9576ca5
SC
90}
91
92wxDialog::~wxDialog()
93{
4506b24a 94 m_isBeingDeleted = TRUE;
e40298d5 95 Show(FALSE);
e9576ca5
SC
96}
97
a15eb0a5 98// By default, pressing escape cancels the dialog , on mac command-stop does the same thing
e9576ca5
SC
99void wxDialog::OnCharHook(wxKeyEvent& event)
100{
eeacbb8c 101 if (( event.m_keyCode == WXK_ESCAPE ||
e40298d5
JS
102 ( event.m_keyCode == '.' && event.MetaDown() ) )
103 && FindWindow(wxID_CANCEL) )
104 {
105 // Behaviour changed in 2.0: we'll send a Cancel message
106 // to the dialog instead of Close.
107 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
108 cancelEvent.SetEventObject( this );
109 GetEventHandler()->ProcessEvent(cancelEvent);
eeacbb8c 110
e40298d5
JS
111 return;
112 }
113 // We didn't process this event.
114 event.Skip();
e9576ca5
SC
115}
116
2f1ae414 117bool wxDialog::IsModal() const
51abe921 118{
2f1ae414 119 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
51abe921
SC
120}
121
2f1ae414
SC
122
123bool wxDialog::IsModalShowing() const
51abe921 124{
2f1ae414 125 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
51abe921
SC
126}
127
e9576ca5
SC
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
2726cac5
SC
159#if !TARGET_CARBON
160extern bool s_macIsInModalLoop ;
161#endif
162
2f1ae414 163void wxDialog::DoShowModal()
51abe921 164{
2f1ae414 165 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
51abe921 166
2f1ae414 167 wxModalDialogs.Append(this);
51abe921 168
2726cac5 169#if TARGET_CARBON
e40298d5 170 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
2726cac5 171#else
e40298d5
JS
172 // TODO : test whether parent gets disabled
173 bool formerModal = s_macIsInModalLoop ;
174 s_macIsInModalLoop = true ;
2726cac5 175#endif
e40298d5
JS
176 while ( IsModalShowing() )
177 {
178 wxTheApp->MacDoOneEvent() ;
179 // calls process idle itself
180 }
eeacbb8c 181
2726cac5 182#if TARGET_CARBON
e40298d5 183 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
2726cac5
SC
184#else
185 // TODO probably reenable the parent window if any
e40298d5 186 s_macIsInModalLoop = formerModal ;
2726cac5 187#endif
51abe921 188}
519cb848 189
2f1ae414 190
e9576ca5
SC
191// Replacement for Show(TRUE) for modal dialogs - returns return code
192int wxDialog::ShowModal()
193{
e40298d5
JS
194 if ( !IsModal() )
195 {
196 SetModal(TRUE);
197 }
81b41c03 198
e40298d5
JS
199 Show(TRUE);
200 return GetReturnCode();
e9576ca5
SC
201}
202
2f1ae414
SC
203// NB: this function (surprizingly) may be called for both modal and modeless
204// dialogs and should work for both of them
e9576ca5
SC
205void wxDialog::EndModal(int retCode)
206{
51abe921
SC
207 SetReturnCode(retCode);
208 Show(FALSE);
e9576ca5
SC
209}
210
211// Standard buttons
d208e641 212void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
e9576ca5 213{
51abe921
SC
214 if ( Validate() && TransferDataFromWindow() )
215 {
2f1ae414 216 EndModal(wxID_OK);
51abe921 217 }
e9576ca5
SC
218}
219
d208e641 220void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
e9576ca5 221{
51abe921
SC
222 if (Validate())
223 TransferDataFromWindow();
224 // TODO probably need to disable the Apply button until things change again
e9576ca5
SC
225}
226
d208e641 227void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
e9576ca5 228{
d208e641 229 EndModal(wxID_CANCEL);
7c74e7fe
SC
230}
231
d208e641 232void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
e9576ca5 233{
e3065973 234 // We'll send a Cancel message by default,
e9576ca5 235 // which may close the dialog.
e3065973
JS
236 // Check for looping if the Cancel event handler calls Close().
237
238 // Note that if a cancel button and handler aren't present in the dialog,
239 // nothing will happen when you close the dialog via the window manager, or
240 // via Close().
241 // We wouldn't want to destroy the dialog by default, since the dialog may have been
242 // created on the stack.
243 // However, this does mean that calling dialog->Close() won't delete the dialog
244 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
245 // sure to destroy the dialog.
246 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
e9576ca5
SC
247
248 static wxList closing;
51abe921 249
e9576ca5 250 if ( closing.Member(this) )
e3065973 251 return;
51abe921 252
e9576ca5 253 closing.Append(this);
51abe921 254
e3065973
JS
255 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
256 cancelEvent.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
e9576ca5
SC
258
259 closing.DeleteObject(this);
e9576ca5
SC
260}
261
d208e641 262void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
e9576ca5 263{
a756f210 264 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e9576ca5
SC
265 Refresh();
266}
267