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