]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
b6c588e1 | 2 | // Name: src/msw/dialog.cpp |
2bda0e17 KB |
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 and Markus Holzem | |
dc1c4b62 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
b6c588e1 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
b6c588e1 | 21 | #pragma implementation "dialog.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
b6c588e1 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
b6c588e1 VZ |
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" | |
2bda0e17 KB |
39 | #endif |
40 | ||
41 | #include "wx/msw/private.h" | |
dbda9e86 | 42 | #include "wx/log.h" |
2bda0e17 | 43 | |
c67d6888 | 44 | #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__) |
b6c588e1 | 45 | #include <commdlg.h> |
2bda0e17 KB |
46 | #endif |
47 | ||
b6c588e1 VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // constants | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | // default dialog pos and size | |
53 | ||
2bda0e17 KB |
54 | #define wxDIALOG_DEFAULT_X 300 |
55 | #define wxDIALOG_DEFAULT_Y 300 | |
56 | ||
b6c588e1 VZ |
57 | #define wxDIALOG_DEFAULT_WIDTH 500 |
58 | #define wxDIALOG_DEFAULT_HEIGHT 500 | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // globals | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
b6c588e1 VZ |
64 | // all modal dialogs currently shown |
65 | static wxWindowList wxModalDialogs; | |
66 | ||
67 | // ---------------------------------------------------------------------------- | |
68 | // wxWin macros | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
82c9f85c | 71 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) |
1b6452df | 72 | |
138e1502 | 73 | BEGIN_EVENT_TABLE(wxDialog, wxDialogBase) |
1b6452df VZ |
74 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) |
75 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) | |
76 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) | |
b6c588e1 | 77 | |
1b6452df | 78 | EVT_CHAR_HOOK(wxDialog::OnCharHook) |
b6c588e1 | 79 | |
1b6452df | 80 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) |
b6c588e1 | 81 | |
1b6452df VZ |
82 | EVT_CLOSE(wxDialog::OnCloseWindow) |
83 | END_EVENT_TABLE() | |
2bda0e17 | 84 | |
b6c588e1 VZ |
85 | // ============================================================================ |
86 | // implementation | |
87 | // ============================================================================ | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // wxDialog construction | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
b0a6bb75 | 93 | void wxDialog::Init() |
2bda0e17 | 94 | { |
52a07708 | 95 | m_oldFocus = (wxWindow *)NULL; |
b0a6bb75 | 96 | |
b6c588e1 | 97 | m_isShown = FALSE; |
2bda0e17 | 98 | |
b0a6bb75 VZ |
99 | m_windowDisabler = (wxWindowDisabler *)NULL; |
100 | ||
a756f210 | 101 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
2bda0e17 KB |
102 | } |
103 | ||
b3daa5a3 VZ |
104 | bool wxDialog::Create(wxWindow *parent, |
105 | wxWindowID id, | |
462e2437 VZ |
106 | const wxString& title, |
107 | const wxPoint& pos, | |
108 | const wxSize& size, | |
109 | long style, | |
110 | const wxString& name) | |
2bda0e17 | 111 | { |
b0a6bb75 VZ |
112 | Init(); |
113 | ||
82c9f85c | 114 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
bd9d76cb | 115 | |
b225f659 | 116 | // save focus before doing anything which can potentially change it |
82c9f85c | 117 | m_oldFocus = FindFocus(); |
462e2437 | 118 | |
706bb5f9 | 119 | // All dialogs should really have this style |
b225f659 | 120 | style |= wxTAB_TRAVERSAL; |
2bda0e17 | 121 | |
b225f659 | 122 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) |
462e2437 | 123 | return FALSE; |
2bda0e17 | 124 | |
a756f210 | 125 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
e1bdd507 | 126 | |
462e2437 | 127 | return TRUE; |
2bda0e17 KB |
128 | } |
129 | ||
debe6624 | 130 | void wxDialog::SetModal(bool flag) |
2bda0e17 | 131 | { |
b6c588e1 VZ |
132 | if ( flag ) |
133 | { | |
134 | m_windowStyle |= wxDIALOG_MODAL; | |
135 | ||
136 | wxModelessWindows.DeleteObject(this); | |
137 | } | |
138 | else | |
139 | { | |
140 | m_windowStyle &= ~wxDIALOG_MODAL; | |
141 | ||
142 | wxModelessWindows.Append(this); | |
143 | } | |
2bda0e17 KB |
144 | } |
145 | ||
146 | wxDialog::~wxDialog() | |
147 | { | |
b6c588e1 | 148 | m_isBeingDeleted = TRUE; |
2bda0e17 | 149 | |
b0a6bb75 | 150 | // this will also reenable all the other windows for a modal dialog |
b6c588e1 | 151 | Show(FALSE); |
2bda0e17 KB |
152 | } |
153 | ||
b6c588e1 VZ |
154 | // ---------------------------------------------------------------------------- |
155 | // kbd handling | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
2bda0e17 KB |
158 | // By default, pressing escape cancels the dialog |
159 | void wxDialog::OnCharHook(wxKeyEvent& event) | |
160 | { | |
cbc66a27 | 161 | if (GetHWND()) |
2bda0e17 | 162 | { |
cbc66a27 VZ |
163 | // "Esc" works as an accelerator for the "Cancel" button, but it |
164 | // shouldn't close the dialog which doesn't have any cancel button | |
165 | if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) ) | |
166 | { | |
167 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
168 | cancelEvent.SetEventObject( this ); | |
169 | GetEventHandler()->ProcessEvent(cancelEvent); | |
170 | ||
171 | // ensure that there is another message for this window so the | |
172 | // ShowModal loop will exit and won't get stuck in GetMessage(). | |
173 | ::PostMessage(GetHwnd(), WM_NULL, 0, 0); | |
174 | ||
175 | return; | |
176 | } | |
2bda0e17 | 177 | } |
cbc66a27 VZ |
178 | |
179 | // We didn't process this event. | |
180 | event.Skip(); | |
2bda0e17 KB |
181 | } |
182 | ||
b6c588e1 VZ |
183 | // ---------------------------------------------------------------------------- |
184 | // showing the dialogs | |
185 | // ---------------------------------------------------------------------------- | |
22cf5fec VZ |
186 | |
187 | bool wxDialog::IsModal() const | |
188 | { | |
b6c588e1 | 189 | return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0; |
2bda0e17 KB |
190 | } |
191 | ||
b6c588e1 | 192 | bool wxDialog::IsModalShowing() const |
2bda0e17 | 193 | { |
b6c588e1 | 194 | return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast |
2bda0e17 KB |
195 | } |
196 | ||
a543e3ce VZ |
197 | wxWindow *wxDialog::FindSuitableParent() const |
198 | { | |
199 | // first try to use the currently active window | |
200 | HWND hwndFg = ::GetForegroundWindow(); | |
201 | wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg) | |
202 | : NULL; | |
203 | if ( !parent ) | |
204 | { | |
205 | // next try the main app window | |
206 | parent = wxTheApp->GetTopWindow(); | |
207 | } | |
208 | ||
209 | // finally, check if the parent we found is really suitable | |
210 | if ( !parent || parent == (wxWindow *)this || !parent->IsShown() ) | |
211 | { | |
212 | // don't use this one | |
213 | parent = NULL; | |
214 | } | |
215 | ||
216 | return parent; | |
217 | } | |
218 | ||
b6c588e1 | 219 | void wxDialog::DoShowModal() |
2bda0e17 | 220 | { |
b6c588e1 | 221 | wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") ); |
f6bcfd97 | 222 | wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") ); |
b6c588e1 VZ |
223 | |
224 | wxModalDialogs.Append(this); | |
225 | ||
226 | wxWindow *parent = GetParent(); | |
227 | ||
f6bcfd97 | 228 | wxWindow* oldFocus = m_oldFocus; |
b6c588e1 | 229 | |
f6bcfd97 BP |
230 | // We have to remember the HWND because we need to check |
231 | // the HWND still exists (oldFocus can be garbage when the dialog | |
232 | // exits, if it has been destroyed) | |
233 | HWND hwndOldFocus = 0; | |
234 | if (oldFocus) | |
235 | hwndOldFocus = (HWND) oldFocus->GetHWND(); | |
b6c588e1 | 236 | |
b0a6bb75 VZ |
237 | // remember where the focus was |
238 | if ( !oldFocus ) | |
b6c588e1 | 239 | { |
b0a6bb75 VZ |
240 | oldFocus = parent; |
241 | if ( parent ) | |
242 | hwndOldFocus = GetHwndOf(parent); | |
243 | } | |
f6bcfd97 | 244 | |
b0a6bb75 VZ |
245 | // disable all other app windows |
246 | wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") ); | |
f6bcfd97 | 247 | |
b0a6bb75 VZ |
248 | m_windowDisabler = new wxWindowDisabler(this); |
249 | ||
250 | // enter the modal loop | |
251 | while ( IsModalShowing() ) | |
252 | { | |
b6c588e1 | 253 | #if wxUSE_THREADS |
b0a6bb75 | 254 | wxMutexGuiLeaveOrEnter(); |
b6c588e1 VZ |
255 | #endif // wxUSE_THREADS |
256 | ||
b0a6bb75 VZ |
257 | while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() ) |
258 | ; | |
b6c588e1 | 259 | |
b0a6bb75 VZ |
260 | // a message came or no more idle processing to do |
261 | wxTheApp->DoMessage(); | |
b6c588e1 VZ |
262 | } |
263 | ||
b6c588e1 | 264 | // and restore focus |
f6bcfd97 BP |
265 | // Note that this code MUST NOT access the dialog object's data |
266 | // in case the object has been deleted (which will be the case | |
267 | // for a modal dialog that has been destroyed before calling EndModal). | |
268 | if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus)) | |
b6c588e1 | 269 | { |
f6bcfd97 BP |
270 | // This is likely to prove that the object still exists |
271 | if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus) | |
272 | oldFocus->SetFocus(); | |
b6c588e1 | 273 | } |
2bda0e17 KB |
274 | } |
275 | ||
b6c588e1 | 276 | bool wxDialog::Show(bool show) |
2bda0e17 | 277 | { |
abceee76 | 278 | if ( !show ) |
86ad564e | 279 | { |
b0a6bb75 VZ |
280 | // if we had disabled other app windows, reenable them back now because |
281 | // if they stay disabled Windows will activate another window (one | |
282 | // which is enabled, anyhow) and we will lose activation | |
283 | if ( m_windowDisabler ) | |
86ad564e | 284 | { |
b0a6bb75 VZ |
285 | delete m_windowDisabler; |
286 | m_windowDisabler = NULL; | |
86ad564e JS |
287 | } |
288 | } | |
289 | ||
abceee76 VZ |
290 | // ShowModal() may be called for already shown dialog |
291 | if ( !wxDialogBase::Show(show) && !(show && IsModal()) ) | |
b6c588e1 VZ |
292 | { |
293 | // nothing to do | |
294 | return FALSE; | |
295 | } | |
2bda0e17 | 296 | |
b6c588e1 VZ |
297 | if ( show ) |
298 | { | |
299 | // usually will result in TransferDataToWindow() being called | |
300 | InitDialog(); | |
301 | } | |
2bda0e17 | 302 | |
b6c588e1 VZ |
303 | if ( IsModal() ) |
304 | { | |
305 | if ( show ) | |
306 | { | |
f6bcfd97 BP |
307 | // modal dialog needs a parent window, so try to find one |
308 | if ( !GetParent() ) | |
309 | { | |
a543e3ce | 310 | m_parent = FindSuitableParent(); |
f6bcfd97 BP |
311 | } |
312 | ||
b6c588e1 VZ |
313 | DoShowModal(); |
314 | } | |
315 | else // end of modal dialog | |
316 | { | |
317 | // this will cause IsModalShowing() return FALSE and our local | |
318 | // message loop will terminate | |
319 | wxModalDialogs.DeleteObject(this); | |
320 | } | |
321 | } | |
2bda0e17 | 322 | |
b6c588e1 | 323 | return TRUE; |
2bda0e17 KB |
324 | } |
325 | ||
f6bcfd97 | 326 | // a special version for Show(TRUE) for modal dialogs which returns return code |
a23fd0e1 | 327 | int wxDialog::ShowModal() |
2bda0e17 | 328 | { |
f6bcfd97 | 329 | if ( !IsModal() ) |
5e1febfa | 330 | { |
f6bcfd97 | 331 | SetModal(TRUE); |
5e1febfa VZ |
332 | } |
333 | ||
b6c588e1 | 334 | Show(TRUE); |
5e1febfa | 335 | |
b6c588e1 | 336 | return GetReturnCode(); |
2bda0e17 KB |
337 | } |
338 | ||
b6c588e1 VZ |
339 | // NB: this function (surprizingly) may be called for both modal and modeless |
340 | // dialogs and should work for both of them | |
2bda0e17 KB |
341 | void wxDialog::EndModal(int retCode) |
342 | { | |
b6c588e1 | 343 | SetReturnCode(retCode); |
6a088435 | 344 | |
b6c588e1 | 345 | Show(FALSE); |
2bda0e17 KB |
346 | } |
347 | ||
b6c588e1 VZ |
348 | // ---------------------------------------------------------------------------- |
349 | // wxWin event handlers | |
350 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
351 | |
352 | // Standard buttons | |
33ac7e6f | 353 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
2bda0e17 | 354 | { |
dc1c4b62 VZ |
355 | if ( Validate() && TransferDataFromWindow() ) |
356 | { | |
b6c588e1 | 357 | EndModal(wxID_OK); |
dc1c4b62 | 358 | } |
2bda0e17 KB |
359 | } |
360 | ||
33ac7e6f | 361 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) |
2bda0e17 | 362 | { |
b6c588e1 VZ |
363 | if ( Validate() ) |
364 | TransferDataFromWindow(); | |
365 | ||
366 | // TODO probably need to disable the Apply button until things change again | |
2bda0e17 KB |
367 | } |
368 | ||
33ac7e6f | 369 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
2bda0e17 | 370 | { |
b6c588e1 | 371 | EndModal(wxID_CANCEL); |
2bda0e17 KB |
372 | } |
373 | ||
33ac7e6f | 374 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
2bda0e17 | 375 | { |
b6c588e1 | 376 | // We'll send a Cancel message by default, which may close the dialog. |
e3065973 JS |
377 | // Check for looping if the Cancel event handler calls Close(). |
378 | ||
379 | // Note that if a cancel button and handler aren't present in the dialog, | |
380 | // nothing will happen when you close the dialog via the window manager, or | |
b6c588e1 VZ |
381 | // via Close(). We wouldn't want to destroy the dialog by default, since |
382 | // the dialog may have been created on the stack. However, this does mean | |
383 | // that calling dialog->Close() won't delete the dialog unless the handler | |
384 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to | |
385 | // destroy the dialog. The default OnCancel (above) simply ends a modal | |
386 | // dialog, and hides a modeless dialog. | |
387 | ||
388 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global | |
389 | // lists here? don't dare to change it now, but should be done later! | |
2bda0e17 | 390 | static wxList closing; |
bd9d76cb | 391 | |
2bda0e17 | 392 | if ( closing.Member(this) ) |
e3065973 | 393 | return; |
bd9d76cb | 394 | |
2bda0e17 | 395 | closing.Append(this); |
bd9d76cb | 396 | |
387a3b02 JS |
397 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
398 | cancelEvent.SetEventObject( this ); | |
e3065973 | 399 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog |
2bda0e17 KB |
400 | |
401 | closing.DeleteObject(this); | |
e3065973 | 402 | } |
2bda0e17 | 403 | |
33ac7e6f | 404 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
2bda0e17 | 405 | { |
1f112209 | 406 | #if wxUSE_CTL3D |
b6c588e1 | 407 | Ctl3dColorChange(); |
2bda0e17 | 408 | #else |
a756f210 | 409 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
b6c588e1 | 410 | Refresh(); |
2bda0e17 | 411 | #endif |
68ad65f8 | 412 | } |
42e69d6b VZ |
413 | |
414 | // --------------------------------------------------------------------------- | |
415 | // dialog window proc | |
416 | // --------------------------------------------------------------------------- | |
417 | ||
418 | long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
419 | { | |
420 | long rc = 0; | |
421 | bool processed = FALSE; | |
422 | ||
423 | switch ( message ) | |
424 | { | |
425 | case WM_CLOSE: | |
426 | // if we can't close, tell the system that we processed the | |
427 | // message - otherwise it would close us | |
428 | processed = !Close(); | |
429 | break; | |
abceee76 | 430 | |
f73f67be VZ |
431 | case WM_SIZE: |
432 | // the Windows dialogs unfortunately are not meant to be resizeable | |
433 | // at all and their standard class doesn't include CS_[VH]REDRAW | |
434 | // styles which means that the window is not refreshed properly | |
435 | // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can | |
436 | // help with it - so we have to refresh it manually which certainly | |
437 | // creates flicker but at least doesn't show garbage on the screen | |
438 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
439 | processed = TRUE; | |
440 | if ( !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) ) | |
441 | { | |
6b6ea919 | 442 | ::InvalidateRect(GetHwnd(), NULL, FALSE /* erase bg */); |
f73f67be VZ |
443 | } |
444 | break; | |
445 | ||
04ef50df | 446 | #ifndef __WXMICROWIN__ |
abceee76 VZ |
447 | case WM_SETCURSOR: |
448 | // we want to override the busy cursor for modal dialogs: | |
449 | // typically, wxBeginBusyCursor() is called and then a modal dialog | |
bfbd6dc1 | 450 | // is shown, but the modal dialog shouldn't have hourglass cursor |
d1477745 | 451 | if ( IsModalShowing() && wxIsBusy() ) |
abceee76 | 452 | { |
bfbd6dc1 VZ |
453 | // set our cursor for all windows (but see below) |
454 | wxCursor cursor = m_cursor; | |
455 | if ( !cursor.Ok() ) | |
456 | cursor = wxCURSOR_ARROW; | |
abceee76 | 457 | |
bfbd6dc1 VZ |
458 | ::SetCursor(GetHcursorOf(cursor)); |
459 | ||
460 | // in any case, stop here and don't let wxWindow process this | |
461 | // message (it would set the busy cursor) | |
abceee76 | 462 | processed = TRUE; |
bfbd6dc1 VZ |
463 | |
464 | // but return FALSE to tell the child window (if the event | |
465 | // comes from one of them and not from ourselves) that it can | |
466 | // set its own cursor if it has one: thus, standard controls | |
467 | // (e.g. text ctrl) still have correct cursors in a dialog | |
468 | // invoked while wxIsBusy() | |
469 | rc = FALSE; | |
abceee76 | 470 | } |
bfbd6dc1 | 471 | break; |
82c9f85c | 472 | #endif // __WXMICROWIN__ |
42e69d6b VZ |
473 | } |
474 | ||
475 | if ( !processed ) | |
476 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); | |
477 | ||
478 | return rc; | |
479 | } | |
b6c588e1 VZ |
480 | |
481 | #if wxUSE_CTL3D | |
482 | ||
483 | // Define for each class of dialog and control | |
484 | WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC), | |
485 | WXHWND WXUNUSED(pWnd), | |
486 | WXUINT WXUNUSED(nCtlColor), | |
487 | WXUINT message, | |
488 | WXWPARAM wParam, | |
489 | WXLPARAM lParam) | |
490 | { | |
491 | return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam); | |
492 | } | |
493 | ||
494 | #endif // wxUSE_CTL3D | |
495 |