]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/os2/dialog.cpp | |
3 | // Purpose: wxDialog class | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/14/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include "wx/dialog.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/utils.h" | |
19 | #include "wx/frame.h" | |
20 | #include "wx/app.h" | |
21 | #include "wx/settings.h" | |
22 | #include "wx/intl.h" | |
23 | #include "wx/log.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/os2/private.h" | |
27 | #include "wx/evtloop.h" | |
28 | #include "wx/scopedptr.h" | |
29 | #include "wx/testing.h" | |
30 | ||
31 | #define wxDIALOG_DEFAULT_X 300 | |
32 | #define wxDIALOG_DEFAULT_Y 300 | |
33 | ||
34 | #define wxDIALOG_DEFAULT_WIDTH 500 | |
35 | #define wxDIALOG_DEFAULT_HEIGHT 500 | |
36 | ||
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // wxDialogModalData | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // this is simply a container for any data we need to implement modality which | |
43 | // allows us to avoid changing wxDialog each time the implementation changes | |
44 | class wxDialogModalData | |
45 | { | |
46 | public: | |
47 | wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { } | |
48 | ||
49 | void RunLoop() | |
50 | { | |
51 | m_evtLoop.Run(); | |
52 | } | |
53 | ||
54 | void ExitLoop() | |
55 | { | |
56 | m_evtLoop.Exit(); | |
57 | } | |
58 | ||
59 | private: | |
60 | wxModalEventLoop m_evtLoop; | |
61 | }; | |
62 | ||
63 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData); | |
64 | ||
65 | // ============================================================================ | |
66 | // implementation | |
67 | // ============================================================================ | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // wxDialog construction | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | void wxDialog::Init() | |
74 | { | |
75 | m_pOldFocus = NULL; | |
76 | m_isShown = false; | |
77 | m_pWindowDisabler = NULL; | |
78 | m_modalData = NULL; | |
79 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
80 | } // end of wxDialog::Init | |
81 | ||
82 | bool wxDialog::Create( wxWindow* pParent, | |
83 | wxWindowID vId, | |
84 | const wxString& rsTitle, | |
85 | const wxPoint& rPos, | |
86 | const wxSize& rSize, | |
87 | long lStyle, | |
88 | const wxString& rsName ) | |
89 | { | |
90 | Init(); | |
91 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); | |
92 | ||
93 | // | |
94 | // Save focus before doing anything which can potentially change it | |
95 | // | |
96 | m_pOldFocus = FindFocus(); | |
97 | ||
98 | // | |
99 | // All dialogs should really have this style | |
100 | // | |
101 | lStyle |= wxTAB_TRAVERSAL; | |
102 | ||
103 | if (!wxTopLevelWindow::Create( pParent | |
104 | ,vId | |
105 | ,rsTitle | |
106 | ,rPos | |
107 | ,rSize | |
108 | ,lStyle | |
109 | ,rsName | |
110 | )) | |
111 | return false; | |
112 | ||
113 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
114 | ||
115 | // | |
116 | // Must defer setting the title until after dialog is created and sized | |
117 | // | |
118 | if ( !rsTitle.empty() ) | |
119 | SetTitle(rsTitle); | |
120 | return true; | |
121 | } // end of wxDialog::Create | |
122 | ||
123 | #if WXWIN_COMPATIBILITY_2_6 | |
124 | ||
125 | // deprecated ctor | |
126 | wxDialog::wxDialog(wxWindow *parent, | |
127 | const wxString& title, | |
128 | bool WXUNUSED(modal), | |
129 | int x, | |
130 | int y, | |
131 | int w, | |
132 | int h, | |
133 | long style, | |
134 | const wxString& name) | |
135 | { | |
136 | Init(); | |
137 | ||
138 | Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name); | |
139 | } | |
140 | ||
141 | void wxDialog::SetModal(bool WXUNUSED(bFlag)) | |
142 | { | |
143 | // nothing to do, obsolete method | |
144 | } // end of wxDialog::SetModal | |
145 | ||
146 | #endif // WXWIN_COMPATIBILITY_2_6 | |
147 | ||
148 | wxDialog::~wxDialog() | |
149 | { | |
150 | SendDestroyEvent(); | |
151 | ||
152 | // this will also reenable all the other windows for a modal dialog | |
153 | Show(false); | |
154 | } // end of wxDialog::~wxDialog | |
155 | ||
156 | // ---------------------------------------------------------------------------- | |
157 | // showing the dialogs | |
158 | // ---------------------------------------------------------------------------- | |
159 | ||
160 | #if WXWIN_COMPATIBILITY_2_6 | |
161 | ||
162 | bool wxDialog::IsModalShowing() const | |
163 | { | |
164 | return IsModal(); | |
165 | } // end of wxDialog::IsModalShowing | |
166 | ||
167 | #endif // WXWIN_COMPATIBILITY_2_6 | |
168 | ||
169 | bool wxDialog::Show( bool bShow ) | |
170 | { | |
171 | if ( bShow == IsShown() ) | |
172 | return false; | |
173 | ||
174 | if (!bShow && m_modalData ) | |
175 | { | |
176 | // we need to do this before calling wxDialogBase version because if we | |
177 | // had disabled other app windows, they must be reenabled right now as | |
178 | // if they stay disabled Windows will activate another window (one | |
179 | // which is enabled, anyhow) when we're hidden in the base class Show() | |
180 | // and we will lose activation | |
181 | m_modalData->ExitLoop(); | |
182 | #if 0 | |
183 | wxDELETE(m_pWindowDisabler); | |
184 | #endif | |
185 | } | |
186 | ||
187 | if (bShow) | |
188 | { | |
189 | if (CanDoLayoutAdaptation()) | |
190 | DoLayoutAdaptation(); | |
191 | ||
192 | // this usually will result in TransferDataToWindow() being called | |
193 | // which will change the controls values so do it before showing as | |
194 | // otherwise we could have some flicker | |
195 | InitDialog(); | |
196 | } | |
197 | ||
198 | wxDialogBase::Show(bShow); | |
199 | ||
200 | wxString title = GetTitle(); | |
201 | if (!title.empty()) | |
202 | ::WinSetWindowText((HWND)GetHwnd(), title.c_str()); | |
203 | ||
204 | if ( bShow ) | |
205 | { | |
206 | // dialogs don't get WM_SIZE message after creation unlike most (all?) | |
207 | // other windows and so could start their life not laid out correctly | |
208 | // if we didn't call Layout() from here | |
209 | // | |
210 | // NB: normally we should call it just the first time but doing it | |
211 | // every time is simpler than keeping a flag | |
212 | Layout(); | |
213 | } | |
214 | ||
215 | return true; | |
216 | } // end of wxDialog::Show | |
217 | ||
218 | // | |
219 | // Replacement for Show(true) for modal dialogs - returns return code | |
220 | // | |
221 | int wxDialog::ShowModal() | |
222 | { | |
223 | WX_TESTING_SHOW_MODAL_HOOK(); | |
224 | ||
225 | wxASSERT_MSG( !IsModal(), wxT("wxDialog::ShowModal() reentered?") ); | |
226 | ||
227 | m_endModalCalled = false; | |
228 | ||
229 | Show(); | |
230 | ||
231 | // EndModal may have been called from InitDialog handler (called from | |
232 | // inside Show()), which would cause an infinite loop if we didn't take it | |
233 | // into account | |
234 | if ( !m_endModalCalled ) | |
235 | { | |
236 | // modal dialog needs a parent window, so try to find one | |
237 | wxWindow * const parent = GetParentForModalDialog(); | |
238 | ||
239 | // remember where the focus was | |
240 | wxWindow *oldFocus = m_pOldFocus; | |
241 | if ( !oldFocus ) | |
242 | { | |
243 | // VZ: do we really want to do this? | |
244 | oldFocus = parent; | |
245 | } | |
246 | ||
247 | // We have to remember the HWND because we need to check | |
248 | // the HWND still exists (oldFocus can be garbage when the dialog | |
249 | // exits, if it has been destroyed) | |
250 | HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL; | |
251 | ||
252 | ||
253 | // | |
254 | // Before entering the modal loop, reset the "is in OnIdle()" flag (see | |
255 | // comment in app.cpp) | |
256 | // | |
257 | extern bool gbInOnIdle; | |
258 | bool bWasInOnIdle = gbInOnIdle; | |
259 | ||
260 | gbInOnIdle = false; | |
261 | ||
262 | // enter and run the modal loop | |
263 | { | |
264 | wxDialogModalDataTiedPtr modalData(&m_modalData, | |
265 | new wxDialogModalData(this)); | |
266 | modalData->RunLoop(); | |
267 | } | |
268 | gbInOnIdle = bWasInOnIdle; | |
269 | ||
270 | // and restore focus | |
271 | // Note that this code MUST NOT access the dialog object's data | |
272 | // in case the object has been deleted (which will be the case | |
273 | // for a modal dialog that has been destroyed before calling EndModal). | |
274 | if ( oldFocus && (oldFocus != this) && ::WinIsWindow(vHabmain, hwndOldFocus)) | |
275 | { | |
276 | // This is likely to prove that the object still exists | |
277 | if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus) | |
278 | oldFocus->SetFocus(); | |
279 | } | |
280 | } | |
281 | ||
282 | return GetReturnCode(); | |
283 | } // end of wxDialog::ShowModal | |
284 | ||
285 | void wxDialog::EndModal( | |
286 | int nRetCode | |
287 | ) | |
288 | { | |
289 | wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") ); | |
290 | ||
291 | m_endModalCalled = true; | |
292 | SetReturnCode(nRetCode); | |
293 | ||
294 | Hide(); | |
295 | } // end of wxDialog::EndModal | |
296 | ||
297 | MRESULT wxDialog::OS2WindowProc( WXUINT uMessage, WXWPARAM wParam, WXLPARAM lParam ) | |
298 | { | |
299 | MRESULT rc = 0; | |
300 | bool bProcessed = false; | |
301 | ||
302 | switch (uMessage) | |
303 | { | |
304 | case WM_CLOSE: | |
305 | // | |
306 | // If we can't close, tell the system that we processed the | |
307 | // message - otherwise it would close us | |
308 | // | |
309 | bProcessed = !Close(); | |
310 | break; | |
311 | } | |
312 | ||
313 | if (!bProcessed) | |
314 | rc = wxWindow::OS2WindowProc( uMessage | |
315 | ,wParam | |
316 | ,lParam | |
317 | ); | |
318 | return rc; | |
319 | } // end of wxDialog::OS2WindowProc |