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