]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/dialog.cpp | |
3 | // Purpose: wxDialog class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "dialog.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/dialog.h" | |
33 | #include "wx/utils.h" | |
34 | #include "wx/frame.h" | |
35 | #include "wx/app.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/intl.h" | |
38 | #include "wx/log.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/msw/private.h" | |
42 | #include "wx/log.h" | |
43 | #include "wx/evtloop.h" | |
44 | #include "wx/ptr_scpd.h" | |
45 | ||
46 | #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__) | |
47 | #include <commdlg.h> | |
48 | #endif | |
49 | ||
50 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) | |
51 | #include "wx/msw/wince/resources.h" | |
52 | #endif // __SMARTPHONE__ && __WXWINCE__ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxWin macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | #if wxUSE_EXTENDED_RTTI | |
59 | WX_DEFINE_FLAGS( wxDialogStyle ) | |
60 | ||
61 | wxBEGIN_FLAGS( wxDialogStyle ) | |
62 | // new style border flags, we put them first to | |
63 | // use them for streaming out | |
64 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
65 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
66 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
67 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
68 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
69 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
70 | ||
71 | // old style border flags | |
72 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
73 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
74 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
75 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
76 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
77 | wxFLAGS_MEMBER(wxNO_BORDER) | |
78 | ||
79 | // standard window styles | |
80 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
81 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
82 | ||
83 | // dialog styles | |
84 | wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY) | |
85 | wxFLAGS_MEMBER(wxSTAY_ON_TOP) | |
86 | wxFLAGS_MEMBER(wxCAPTION) | |
87 | wxFLAGS_MEMBER(wxTHICK_FRAME) | |
88 | wxFLAGS_MEMBER(wxSYSTEM_MENU) | |
89 | wxFLAGS_MEMBER(wxRESIZE_BORDER) | |
90 | wxFLAGS_MEMBER(wxRESIZE_BOX) | |
91 | wxFLAGS_MEMBER(wxCLOSE_BOX) | |
92 | wxFLAGS_MEMBER(wxMAXIMIZE_BOX) | |
93 | wxFLAGS_MEMBER(wxMINIMIZE_BOX) | |
94 | wxEND_FLAGS( wxDialogStyle ) | |
95 | ||
96 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h") | |
97 | ||
98 | wxBEGIN_PROPERTIES_TABLE(wxDialog) | |
99 | wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
100 | wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
101 | wxEND_PROPERTIES_TABLE() | |
102 | ||
103 | wxBEGIN_HANDLERS_TABLE(wxDialog) | |
104 | wxEND_HANDLERS_TABLE() | |
105 | ||
106 | wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle) | |
107 | ||
108 | #else | |
109 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) | |
110 | #endif | |
111 | ||
112 | BEGIN_EVENT_TABLE(wxDialog, wxDialogBase) | |
113 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) | |
114 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) | |
115 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) | |
116 | ||
117 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) | |
118 | ||
119 | EVT_CLOSE(wxDialog::OnCloseWindow) | |
120 | END_EVENT_TABLE() | |
121 | ||
122 | // ---------------------------------------------------------------------------- | |
123 | // wxDialogModalData | |
124 | // ---------------------------------------------------------------------------- | |
125 | ||
126 | // this is simply a container for any data we need to implement modality which | |
127 | // allows us to avoid changing wxDialog each time the implementation changes | |
128 | class wxDialogModalData | |
129 | { | |
130 | public: | |
131 | wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { } | |
132 | ||
133 | void RunLoop() | |
134 | { | |
135 | m_evtLoop.Run(); | |
136 | } | |
137 | ||
138 | void ExitLoop() | |
139 | { | |
140 | m_evtLoop.Exit(); | |
141 | } | |
142 | ||
143 | private: | |
144 | wxModalEventLoop m_evtLoop; | |
145 | }; | |
146 | ||
147 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData); | |
148 | ||
149 | // ============================================================================ | |
150 | // implementation | |
151 | // ============================================================================ | |
152 | ||
153 | // ---------------------------------------------------------------------------- | |
154 | // wxDialog construction | |
155 | // ---------------------------------------------------------------------------- | |
156 | ||
157 | void wxDialog::Init() | |
158 | { | |
159 | m_oldFocus = (wxWindow *)NULL; | |
160 | m_isShown = false; | |
161 | m_modalData = NULL; | |
162 | m_endModalCalled = false; | |
163 | } | |
164 | ||
165 | bool wxDialog::Create(wxWindow *parent, | |
166 | wxWindowID id, | |
167 | const wxString& title, | |
168 | const wxPoint& pos, | |
169 | const wxSize& size, | |
170 | long style, | |
171 | const wxString& name) | |
172 | { | |
173 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); | |
174 | ||
175 | // save focus before doing anything which can potentially change it | |
176 | m_oldFocus = FindFocus(); | |
177 | ||
178 | // All dialogs should really have this style | |
179 | style |= wxTAB_TRAVERSAL; | |
180 | ||
181 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) | |
182 | return false; | |
183 | ||
184 | if ( !m_hasFont ) | |
185 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
186 | ||
187 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) | |
188 | SetLeftMenu(wxID_OK, _("OK")); | |
189 | #endif | |
190 | ||
191 | return true; | |
192 | } | |
193 | ||
194 | // deprecated ctor | |
195 | wxDialog::wxDialog(wxWindow *parent, | |
196 | const wxString& title, | |
197 | bool WXUNUSED(modal), | |
198 | int x, | |
199 | int y, | |
200 | int w, | |
201 | int h, | |
202 | long style, | |
203 | const wxString& name) | |
204 | { | |
205 | Init(); | |
206 | ||
207 | Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name); | |
208 | } | |
209 | ||
210 | void wxDialog::SetModal(bool WXUNUSED(flag)) | |
211 | { | |
212 | // nothing to do, obsolete method | |
213 | } | |
214 | ||
215 | wxDialog::~wxDialog() | |
216 | { | |
217 | m_isBeingDeleted = true; | |
218 | ||
219 | // this will also reenable all the other windows for a modal dialog | |
220 | Show(false); | |
221 | } | |
222 | ||
223 | // ---------------------------------------------------------------------------- | |
224 | // showing the dialogs | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | bool wxDialog::IsModalShowing() const | |
228 | { | |
229 | return IsModal(); | |
230 | } | |
231 | ||
232 | wxWindow *wxDialog::FindSuitableParent() const | |
233 | { | |
234 | // first try to use the currently active window | |
235 | HWND hwndFg = ::GetForegroundWindow(); | |
236 | wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg) | |
237 | : NULL; | |
238 | if ( !parent ) | |
239 | { | |
240 | // next try the main app window | |
241 | parent = wxTheApp->GetTopWindow(); | |
242 | } | |
243 | ||
244 | // finally, check if the parent we found is really suitable | |
245 | if ( !parent || parent == (wxWindow *)this || !parent->IsShown() ) | |
246 | { | |
247 | // don't use this one | |
248 | parent = NULL; | |
249 | } | |
250 | ||
251 | return parent; | |
252 | } | |
253 | ||
254 | bool wxDialog::Show(bool show) | |
255 | { | |
256 | if ( show == IsShown() ) | |
257 | return false; | |
258 | ||
259 | if ( !show && m_modalData ) | |
260 | { | |
261 | // we need to do this before calling wxDialogBase version because if we | |
262 | // had disabled other app windows, they must be reenabled right now as | |
263 | // if they stay disabled Windows will activate another window (one | |
264 | // which is enabled, anyhow) when we're hidden in the base class Show() | |
265 | // and we will lose activation | |
266 | m_modalData->ExitLoop(); | |
267 | } | |
268 | ||
269 | if ( show ) | |
270 | { | |
271 | // this usually will result in TransferDataToWindow() being called | |
272 | // which will change the controls values so do it before showing as | |
273 | // otherwise we could have some flicker | |
274 | InitDialog(); | |
275 | } | |
276 | ||
277 | wxDialogBase::Show(show); | |
278 | ||
279 | if ( show ) | |
280 | { | |
281 | // dialogs don't get WM_SIZE message after creation unlike most (all?) | |
282 | // other windows and so could start their life non laid out correctly | |
283 | // if we didn't call Layout() from here | |
284 | // | |
285 | // NB: normally we should call it just the first time but doing it | |
286 | // every time is simpler than keeping a flag | |
287 | Layout(); | |
288 | } | |
289 | ||
290 | return true; | |
291 | } | |
292 | ||
293 | void wxDialog::Raise() | |
294 | { | |
295 | ::SetForegroundWindow(GetHwnd()); | |
296 | } | |
297 | ||
298 | // show dialog modally | |
299 | int wxDialog::ShowModal() | |
300 | { | |
301 | wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") ); | |
302 | ||
303 | m_endModalCalled = false; | |
304 | ||
305 | Show(); | |
306 | ||
307 | // EndModal may have been called from InitDialog handler (called from | |
308 | // inside Show()), which would cause an infinite loop if we didn't take it | |
309 | // into account | |
310 | if ( !m_endModalCalled ) | |
311 | { | |
312 | // modal dialog needs a parent window, so try to find one | |
313 | wxWindow *parent = GetParent(); | |
314 | if ( !parent ) | |
315 | { | |
316 | parent = FindSuitableParent(); | |
317 | } | |
318 | ||
319 | // remember where the focus was | |
320 | wxWindow *oldFocus = m_oldFocus; | |
321 | if ( !oldFocus ) | |
322 | { | |
323 | // VZ: do we really want to do this? | |
324 | oldFocus = parent; | |
325 | } | |
326 | ||
327 | // We have to remember the HWND because we need to check | |
328 | // the HWND still exists (oldFocus can be garbage when the dialog | |
329 | // exits, if it has been destroyed) | |
330 | HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL; | |
331 | ||
332 | ||
333 | // enter and run the modal loop | |
334 | { | |
335 | wxDialogModalDataTiedPtr modalData(&m_modalData, | |
336 | new wxDialogModalData(this)); | |
337 | modalData->RunLoop(); | |
338 | } | |
339 | ||
340 | ||
341 | // and restore focus | |
342 | // Note that this code MUST NOT access the dialog object's data | |
343 | // in case the object has been deleted (which will be the case | |
344 | // for a modal dialog that has been destroyed before calling EndModal). | |
345 | if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus)) | |
346 | { | |
347 | // This is likely to prove that the object still exists | |
348 | if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus) | |
349 | oldFocus->SetFocus(); | |
350 | } | |
351 | } | |
352 | ||
353 | return GetReturnCode(); | |
354 | } | |
355 | ||
356 | void wxDialog::EndModal(int retCode) | |
357 | { | |
358 | wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") ); | |
359 | ||
360 | m_endModalCalled = true; | |
361 | SetReturnCode(retCode); | |
362 | ||
363 | Hide(); | |
364 | } | |
365 | ||
366 | void wxDialog::EndDialog(int rc) | |
367 | { | |
368 | if ( IsModal() ) | |
369 | EndModal(rc); | |
370 | else | |
371 | Hide(); | |
372 | } | |
373 | ||
374 | // ---------------------------------------------------------------------------- | |
375 | // wxWin event handlers | |
376 | // ---------------------------------------------------------------------------- | |
377 | ||
378 | // Standard buttons | |
379 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) | |
380 | { | |
381 | if ( Validate() && TransferDataFromWindow() ) | |
382 | { | |
383 | EndDialog(wxID_OK); | |
384 | } | |
385 | } | |
386 | ||
387 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) | |
388 | { | |
389 | if ( Validate() ) | |
390 | TransferDataFromWindow(); | |
391 | ||
392 | // TODO probably need to disable the Apply button until things change again | |
393 | } | |
394 | ||
395 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
396 | { | |
397 | EndDialog(wxID_CANCEL); | |
398 | } | |
399 | ||
400 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
401 | { | |
402 | // We'll send a Cancel message by default, which may close the dialog. | |
403 | // Check for looping if the Cancel event handler calls Close(). | |
404 | ||
405 | // Note that if a cancel button and handler aren't present in the dialog, | |
406 | // nothing will happen when you close the dialog via the window manager, or | |
407 | // via Close(). We wouldn't want to destroy the dialog by default, since | |
408 | // the dialog may have been created on the stack. However, this does mean | |
409 | // that calling dialog->Close() won't delete the dialog unless the handler | |
410 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to | |
411 | // destroy the dialog. The default OnCancel (above) simply ends a modal | |
412 | // dialog, and hides a modeless dialog. | |
413 | ||
414 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global | |
415 | // lists here? don't dare to change it now, but should be done later! | |
416 | static wxList closing; | |
417 | ||
418 | if ( closing.Member(this) ) | |
419 | return; | |
420 | ||
421 | closing.Append(this); | |
422 | ||
423 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
424 | cancelEvent.SetEventObject( this ); | |
425 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
426 | ||
427 | closing.DeleteObject(this); | |
428 | } | |
429 | ||
430 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) | |
431 | { | |
432 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
433 | Refresh(); | |
434 | } | |
435 | ||
436 | // --------------------------------------------------------------------------- | |
437 | // dialog window proc | |
438 | // --------------------------------------------------------------------------- | |
439 | ||
440 | WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
441 | { | |
442 | WXLRESULT rc = 0; | |
443 | bool processed = false; | |
444 | ||
445 | switch ( message ) | |
446 | { | |
447 | #ifdef __WXWINCE__ | |
448 | // react to pressing the OK button in the title | |
449 | case WM_COMMAND: | |
450 | { | |
451 | switch ( LOWORD(wParam) ) | |
452 | { | |
453 | #ifndef __SMARTPHONE__ | |
454 | case IDOK: | |
455 | wxButton *btn = wxDynamicCast(FindWindow(wxID_CANCEL), wxButton); | |
456 | if ( btn && btn->IsEnabled() ) | |
457 | { | |
458 | // if we do have a cancel button, do press it | |
459 | btn->MSWCommand(BN_CLICKED, 0 /* unused */); | |
460 | processed = true; | |
461 | break; | |
462 | } | |
463 | #else // ifdef __SMARTPHONE__ | |
464 | case IDM_LEFT: | |
465 | case IDM_RIGHT: | |
466 | processed = HandleCommand( LOWORD(wParam) , 0 , NULL ); | |
467 | break; | |
468 | #endif // __SMARTPHONE__ | |
469 | } | |
470 | break; | |
471 | } | |
472 | #endif | |
473 | case WM_CLOSE: | |
474 | // if we can't close, tell the system that we processed the | |
475 | // message - otherwise it would close us | |
476 | processed = !Close(); | |
477 | break; | |
478 | ||
479 | case WM_SIZE: | |
480 | // the Windows dialogs unfortunately are not meant to be resizeable | |
481 | // at all and their standard class doesn't include CS_[VH]REDRAW | |
482 | // styles which means that the window is not refreshed properly | |
483 | // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can | |
484 | // help with it - so we have to refresh it manually which certainly | |
485 | // creates flicker but at least doesn't show garbage on the screen | |
486 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
487 | processed = true; | |
488 | if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) ) | |
489 | { | |
490 | ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */); | |
491 | } | |
492 | break; | |
493 | ||
494 | #ifndef __WXMICROWIN__ | |
495 | case WM_SETCURSOR: | |
496 | // we want to override the busy cursor for modal dialogs: | |
497 | // typically, wxBeginBusyCursor() is called and then a modal dialog | |
498 | // is shown, but the modal dialog shouldn't have hourglass cursor | |
499 | if ( IsModal() && wxIsBusy() ) | |
500 | { | |
501 | // set our cursor for all windows (but see below) | |
502 | wxCursor cursor = m_cursor; | |
503 | if ( !cursor.Ok() ) | |
504 | cursor = wxCURSOR_ARROW; | |
505 | ||
506 | ::SetCursor(GetHcursorOf(cursor)); | |
507 | ||
508 | // in any case, stop here and don't let wxWindow process this | |
509 | // message (it would set the busy cursor) | |
510 | processed = true; | |
511 | ||
512 | // but return false to tell the child window (if the event | |
513 | // comes from one of them and not from ourselves) that it can | |
514 | // set its own cursor if it has one: thus, standard controls | |
515 | // (e.g. text ctrl) still have correct cursors in a dialog | |
516 | // invoked while wxIsBusy() | |
517 | rc = false; | |
518 | } | |
519 | break; | |
520 | #endif // __WXMICROWIN__ | |
521 | } | |
522 | ||
523 | if ( !processed ) | |
524 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
525 | ||
526 | return rc; | |
527 | } | |
528 |