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