Fix for bug [ 744199 ] wxBringWindowToTop, child window z-order
[wxWidgets.git] / src / msw / dialog.cpp
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 #ifdef __GNUG__
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
44 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
45 #include <commdlg.h>
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // default dialog pos and size
53
54 #define wxDIALOG_DEFAULT_X 300
55 #define wxDIALOG_DEFAULT_Y 300
56
57 #define wxDIALOG_DEFAULT_WIDTH 500
58 #define wxDIALOG_DEFAULT_HEIGHT 500
59
60 // ----------------------------------------------------------------------------
61 // globals
62 // ----------------------------------------------------------------------------
63
64 // all modal dialogs currently shown
65 static wxWindowList wxModalDialogs;
66
67 // ----------------------------------------------------------------------------
68 // wxWin macros
69 // ----------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
72
73 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
74 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
75 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
76 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
77
78 EVT_CHAR_HOOK(wxDialog::OnCharHook)
79
80 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
81
82 EVT_CLOSE(wxDialog::OnCloseWindow)
83 END_EVENT_TABLE()
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // wxDialog construction
91 // ----------------------------------------------------------------------------
92
93 void wxDialog::Init()
94 {
95 m_oldFocus = (wxWindow *)NULL;
96
97 m_isShown = FALSE;
98
99 m_windowDisabler = (wxWindowDisabler *)NULL;
100
101 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
102 }
103
104 bool wxDialog::Create(wxWindow *parent,
105 wxWindowID id,
106 const wxString& title,
107 const wxPoint& pos,
108 const wxSize& size,
109 long style,
110 const wxString& name)
111 {
112 Init();
113
114 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
115
116 // save focus before doing anything which can potentially change it
117 m_oldFocus = FindFocus();
118
119 // All dialogs should really have this style
120 style |= wxTAB_TRAVERSAL;
121
122 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
123 return FALSE;
124
125 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
126
127 return TRUE;
128 }
129
130 void wxDialog::SetModal(bool flag)
131 {
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 }
144 }
145
146 wxDialog::~wxDialog()
147 {
148 m_isBeingDeleted = TRUE;
149
150 // this will also reenable all the other windows for a modal dialog
151 Show(FALSE);
152 }
153
154 // ----------------------------------------------------------------------------
155 // kbd handling
156 // ----------------------------------------------------------------------------
157
158 // By default, pressing escape cancels the dialog
159 void wxDialog::OnCharHook(wxKeyEvent& event)
160 {
161 if (GetHWND())
162 {
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 }
177 }
178
179 // We didn't process this event.
180 event.Skip();
181 }
182
183 // ----------------------------------------------------------------------------
184 // showing the dialogs
185 // ----------------------------------------------------------------------------
186
187 bool wxDialog::IsModal() const
188 {
189 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
190 }
191
192 bool wxDialog::IsModalShowing() const
193 {
194 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
195 }
196
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
219 void wxDialog::DoShowModal()
220 {
221 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
222 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
223
224 wxModalDialogs.Append(this);
225
226 wxWindow *parent = GetParent();
227
228 wxWindow* oldFocus = m_oldFocus;
229
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();
236
237 // remember where the focus was
238 if ( !oldFocus )
239 {
240 oldFocus = parent;
241 if ( parent )
242 hwndOldFocus = GetHwndOf(parent);
243 }
244
245 // disable all other app windows
246 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
247
248 m_windowDisabler = new wxWindowDisabler(this);
249
250 // before entering the modal loop, reset the "is in OnIdle()" flag (see
251 // comment in app.cpp)
252 extern bool wxIsInOnIdleFlag;
253 bool wasInOnIdle = wxIsInOnIdleFlag;
254 wxIsInOnIdleFlag = FALSE;
255
256 // enter the modal loop
257 while ( IsModalShowing() )
258 {
259 #if wxUSE_THREADS
260 wxMutexGuiLeaveOrEnter();
261 #endif // wxUSE_THREADS
262
263 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
264 ;
265
266 // a message came or no more idle processing to do
267 wxTheApp->DoMessage();
268 }
269
270 wxIsInOnIdleFlag = wasInOnIdle;
271
272 // and restore focus
273 // Note that this code MUST NOT access the dialog object's data
274 // in case the object has been deleted (which will be the case
275 // for a modal dialog that has been destroyed before calling EndModal).
276 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
277 {
278 // This is likely to prove that the object still exists
279 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
280 oldFocus->SetFocus();
281 }
282 }
283
284 bool wxDialog::Show(bool show)
285 {
286 if ( !show )
287 {
288 // if we had disabled other app windows, reenable them back now because
289 // if they stay disabled Windows will activate another window (one
290 // which is enabled, anyhow) and we will lose activation
291 if ( m_windowDisabler )
292 {
293 delete m_windowDisabler;
294 m_windowDisabler = NULL;
295 }
296 }
297
298 // ShowModal() may be called for already shown dialog
299 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
300 {
301 // nothing to do
302 return FALSE;
303 }
304
305 if ( show )
306 {
307 // dialogs don't get WM_SIZE message after creation unlike most (all?)
308 // other windows and so could start their life non laid out correctly
309 // if we didn't call Layout() from here
310 //
311 // NB: normally we should call it just the first time but doing it
312 // every time is simpler than keeping a flag
313 Layout();
314
315 // usually will result in TransferDataToWindow() being called
316 InitDialog();
317 }
318
319 if ( IsModal() )
320 {
321 if ( show )
322 {
323 // modal dialog needs a parent window, so try to find one
324 if ( !GetParent() )
325 {
326 m_parent = FindSuitableParent();
327 }
328
329 DoShowModal();
330 }
331 else // end of modal dialog
332 {
333 // this will cause IsModalShowing() return FALSE and our local
334 // message loop will terminate
335 wxModalDialogs.DeleteObject(this);
336 }
337 }
338
339 return TRUE;
340 }
341
342 void wxDialog::Raise()
343 {
344 ::SetForegroundWindow(GetHwnd());
345 }
346
347 // a special version for Show(TRUE) for modal dialogs which returns return code
348 int wxDialog::ShowModal()
349 {
350 if ( !IsModal() )
351 {
352 SetModal(TRUE);
353 }
354
355 Show(TRUE);
356
357 return GetReturnCode();
358 }
359
360 // NB: this function (surprizingly) may be called for both modal and modeless
361 // dialogs and should work for both of them
362 void wxDialog::EndModal(int retCode)
363 {
364 SetReturnCode(retCode);
365
366 Show(FALSE);
367 }
368
369 // ----------------------------------------------------------------------------
370 // wxWin event handlers
371 // ----------------------------------------------------------------------------
372
373 // Standard buttons
374 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
375 {
376 if ( Validate() && TransferDataFromWindow() )
377 {
378 EndModal(wxID_OK);
379 }
380 }
381
382 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
383 {
384 if ( Validate() )
385 TransferDataFromWindow();
386
387 // TODO probably need to disable the Apply button until things change again
388 }
389
390 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
391 {
392 EndModal(wxID_CANCEL);
393 }
394
395 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
396 {
397 // We'll send a Cancel message by default, which may close the dialog.
398 // Check for looping if the Cancel event handler calls Close().
399
400 // Note that if a cancel button and handler aren't present in the dialog,
401 // nothing will happen when you close the dialog via the window manager, or
402 // via Close(). We wouldn't want to destroy the dialog by default, since
403 // the dialog may have been created on the stack. However, this does mean
404 // that calling dialog->Close() won't delete the dialog unless the handler
405 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
406 // destroy the dialog. The default OnCancel (above) simply ends a modal
407 // dialog, and hides a modeless dialog.
408
409 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
410 // lists here? don't dare to change it now, but should be done later!
411 static wxList closing;
412
413 if ( closing.Member(this) )
414 return;
415
416 closing.Append(this);
417
418 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
419 cancelEvent.SetEventObject( this );
420 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
421
422 closing.DeleteObject(this);
423 }
424
425 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
426 {
427 #if wxUSE_CTL3D
428 Ctl3dColorChange();
429 #else
430 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
431 Refresh();
432 #endif
433 }
434
435 // ---------------------------------------------------------------------------
436 // dialog window proc
437 // ---------------------------------------------------------------------------
438
439 long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
440 {
441 long rc = 0;
442 bool processed = FALSE;
443
444 switch ( message )
445 {
446 case WM_CLOSE:
447 // if we can't close, tell the system that we processed the
448 // message - otherwise it would close us
449 processed = !Close();
450 break;
451
452 case WM_SIZE:
453 // the Windows dialogs unfortunately are not meant to be resizeable
454 // at all and their standard class doesn't include CS_[VH]REDRAW
455 // styles which means that the window is not refreshed properly
456 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
457 // help with it - so we have to refresh it manually which certainly
458 // creates flicker but at least doesn't show garbage on the screen
459 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
460 processed = TRUE;
461 if ( !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) )
462 {
463 ::InvalidateRect(GetHwnd(), NULL, FALSE /* erase bg */);
464 }
465 break;
466
467 #ifndef __WXMICROWIN__
468 case WM_SETCURSOR:
469 // we want to override the busy cursor for modal dialogs:
470 // typically, wxBeginBusyCursor() is called and then a modal dialog
471 // is shown, but the modal dialog shouldn't have hourglass cursor
472 if ( IsModalShowing() && wxIsBusy() )
473 {
474 // set our cursor for all windows (but see below)
475 wxCursor cursor = m_cursor;
476 if ( !cursor.Ok() )
477 cursor = wxCURSOR_ARROW;
478
479 ::SetCursor(GetHcursorOf(cursor));
480
481 // in any case, stop here and don't let wxWindow process this
482 // message (it would set the busy cursor)
483 processed = TRUE;
484
485 // but return FALSE to tell the child window (if the event
486 // comes from one of them and not from ourselves) that it can
487 // set its own cursor if it has one: thus, standard controls
488 // (e.g. text ctrl) still have correct cursors in a dialog
489 // invoked while wxIsBusy()
490 rc = FALSE;
491 }
492 break;
493 #endif // __WXMICROWIN__
494 }
495
496 if ( !processed )
497 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
498
499 return rc;
500 }
501
502 #if wxUSE_CTL3D
503
504 // Define for each class of dialog and control
505 WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
506 WXHWND WXUNUSED(pWnd),
507 WXUINT WXUNUSED(nCtlColor),
508 WXUINT message,
509 WXWPARAM wParam,
510 WXLPARAM lParam)
511 {
512 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
513 }
514
515 #endif // wxUSE_CTL3D
516