avoiding reentrancy problems and congestion
[wxWidgets.git] / src / osx / dialog_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/dialog_osx.cpp
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dialog.h"
15 #include "wx/evtloop.h"
16 #include "wx/modalhook.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/utils.h"
21 #include "wx/frame.h"
22 #include "wx/settings.h"
23 #endif // WX_PRECOMP
24
25 #include "wx/osx/private.h"
26
27 static int s_openDialogs = 0;
28 bool wxDialog::OSXHasModalDialogsOpen()
29 {
30 return s_openDialogs > 0;
31 }
32
33 void wxDialog::OSXBeginModalDialog()
34 {
35 s_openDialogs++;
36 }
37
38 void wxDialog::OSXEndModalDialog()
39 {
40 wxASSERT_MSG( s_openDialogs > 0, "incorrect internal modal dialog count");
41 s_openDialogs--;
42 }
43
44 void wxDialog::Init()
45 {
46 m_modality = wxDIALOG_MODALITY_NONE;
47 m_eventLoop = NULL;
48 }
49
50 bool wxDialog::Create( wxWindow *parent,
51 wxWindowID id,
52 const wxString& title,
53 const wxPoint& pos,
54 const wxSize& size,
55 long style,
56 const wxString& name )
57 {
58 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
59
60 // All dialogs should really have this style...
61 style |= wxTAB_TRAVERSAL;
62
63 // ...but not these styles
64 style &= ~(wxYES | wxOK | wxNO); // | wxCANCEL
65
66 if ( !wxTopLevelWindow::Create( parent, id, title, pos, size, style, name ) )
67 return false;
68
69 return true;
70 }
71
72 wxDialog::~wxDialog()
73 {
74 SendDestroyEvent();
75
76 // if the dialog is modal, this will end its event loop
77 Show(false);
78 }
79
80 // On mac command-stop does the same thing as Esc, let the base class know
81 // about it
82 bool wxDialog::IsEscapeKey(const wxKeyEvent& event)
83 {
84 if ( event.GetKeyCode() == '.' && event.GetModifiers() == wxMOD_CONTROL )
85 return true;
86
87 return wxDialogBase::IsEscapeKey(event);
88 }
89
90 bool wxDialog::IsModal() const
91 {
92 return m_modality != wxDIALOG_MODALITY_NONE;
93 }
94
95 bool wxDialog::Show(bool show)
96 {
97 if ( m_modality == wxDIALOG_MODALITY_WINDOW_MODAL )
98 {
99 if ( !wxWindow::Show(show) )
100 // nothing to do
101 return false;
102 }
103 else
104 {
105 if ( !wxDialogBase::Show(show) )
106 // nothing to do
107 return false;
108 }
109
110 if (show && CanDoLayoutAdaptation())
111 DoLayoutAdaptation();
112
113 if ( show )
114 // usually will result in TransferDataToWindow() being called
115 InitDialog();
116
117 if ( !show )
118 {
119 const int modalityOrig = m_modality;
120
121 // complete the 'hiding' before we send the event
122 m_modality = wxDIALOG_MODALITY_NONE;
123
124 switch ( modalityOrig )
125 {
126 case wxDIALOG_MODALITY_WINDOW_MODAL:
127 EndWindowModal(); // OS X implementation method for cleanup
128 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
129 break;
130 default:
131 break;
132 }
133 }
134
135 return true;
136 }
137
138 // Replacement for Show(true) for modal dialogs - returns return code
139 int wxDialog::ShowModal()
140 {
141 WX_HOOK_MODAL_DIALOG();
142
143 m_modality = wxDIALOG_MODALITY_APP_MODAL;
144
145 Show();
146
147 wxModalEventLoop modalLoop(this);
148 m_eventLoop = &modalLoop;
149
150 wxDialog::OSXBeginModalDialog();
151 modalLoop.Run();
152 wxDialog::OSXEndModalDialog();
153
154 m_eventLoop = NULL;
155
156 return GetReturnCode();
157 }
158
159 void wxDialog::ShowWindowModal()
160 {
161 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
162
163 Show();
164
165 DoShowWindowModal();
166 }
167
168 wxDialogModality wxDialog::GetModality() const
169 {
170 return m_modality;
171 }
172
173 // NB: this function (surprisingly) may be called for both modal and modeless
174 // dialogs and should work for both of them
175 void wxDialog::EndModal(int retCode)
176 {
177 if ( m_eventLoop )
178 m_eventLoop->Exit(retCode);
179
180 SetReturnCode(retCode);
181 Show(false);
182 }
183