1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dialog.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dialog.h"
36 #include "wx/settings.h"
41 #include "wx/msw/private.h"
44 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // default dialog pos and size
54 #define wxDIALOG_DEFAULT_X 300
55 #define wxDIALOG_DEFAULT_Y 300
57 #define wxDIALOG_DEFAULT_WIDTH 500
58 #define wxDIALOG_DEFAULT_HEIGHT 500
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // all modal dialogs currently shown
65 static wxWindowList wxModalDialogs
;
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
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
)
78 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
80 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
82 EVT_CLOSE(wxDialog::OnCloseWindow
)
85 // ============================================================================
87 // ============================================================================
89 // ----------------------------------------------------------------------------
90 // wxDialog construction
91 // ----------------------------------------------------------------------------
95 m_oldFocus
= (wxWindow
*)NULL
;
99 m_windowDisabler
= (wxWindowDisabler
*)NULL
;
101 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
104 bool wxDialog::Create(wxWindow
*parent
,
106 const wxString
& title
,
110 const wxString
& name
)
114 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
116 // save focus before doing anything which can potentially change it
117 m_oldFocus
= FindFocus();
119 // All dialogs should really have this style
120 style
|= wxTAB_TRAVERSAL
;
122 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
125 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
130 void wxDialog::SetModal(bool flag
)
134 m_windowStyle
|= wxDIALOG_MODAL
;
136 wxModelessWindows
.DeleteObject(this);
140 m_windowStyle
&= ~wxDIALOG_MODAL
;
142 wxModelessWindows
.Append(this);
146 wxDialog::~wxDialog()
148 m_isBeingDeleted
= TRUE
;
150 // this will also reenable all the other windows for a modal dialog
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 // By default, pressing escape cancels the dialog
159 void wxDialog::OnCharHook(wxKeyEvent
& event
)
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
) )
167 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
168 cancelEvent
.SetEventObject( this );
169 GetEventHandler()->ProcessEvent(cancelEvent
);
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);
179 // We didn't process this event.
183 // ----------------------------------------------------------------------------
184 // showing the dialogs
185 // ----------------------------------------------------------------------------
187 bool wxDialog::IsModal() const
189 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
192 bool wxDialog::IsModalShowing() const
194 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
197 wxWindow
*wxDialog::FindSuitableParent() const
199 // first try to use the currently active window
200 HWND hwndFg
= ::GetForegroundWindow();
201 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
205 // next try the main app window
206 parent
= wxTheApp
->GetTopWindow();
209 // finally, check if the parent we found is really suitable
210 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
212 // don't use this one
219 void wxDialog::DoShowModal()
221 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
222 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
224 wxModalDialogs
.Append(this);
226 wxWindow
*parent
= GetParent();
228 wxWindow
* oldFocus
= m_oldFocus
;
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;
235 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
237 // remember where the focus was
242 hwndOldFocus
= GetHwndOf(parent
);
245 // disable all other app windows
246 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
248 m_windowDisabler
= new wxWindowDisabler(this);
250 // enter the modal loop
251 while ( IsModalShowing() )
254 wxMutexGuiLeaveOrEnter();
255 #endif // wxUSE_THREADS
257 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
260 // a message came or no more idle processing to do
261 wxTheApp
->DoMessage();
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
))
270 // This is likely to prove that the object still exists
271 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
272 oldFocus
->SetFocus();
276 bool wxDialog::Show(bool show
)
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
)
285 delete m_windowDisabler
;
286 m_windowDisabler
= NULL
;
290 // ShowModal() may be called for already shown dialog
291 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
299 // usually will result in TransferDataToWindow() being called
307 // modal dialog needs a parent window, so try to find one
310 m_parent
= FindSuitableParent();
315 else // end of modal dialog
317 // this will cause IsModalShowing() return FALSE and our local
318 // message loop will terminate
319 wxModalDialogs
.DeleteObject(this);
326 // a special version for Show(TRUE) for modal dialogs which returns return code
327 int wxDialog::ShowModal()
336 return GetReturnCode();
339 // NB: this function (surprizingly) may be called for both modal and modeless
340 // dialogs and should work for both of them
341 void wxDialog::EndModal(int retCode
)
343 SetReturnCode(retCode
);
348 // ----------------------------------------------------------------------------
349 // wxWin event handlers
350 // ----------------------------------------------------------------------------
353 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
355 if ( Validate() && TransferDataFromWindow() )
361 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
364 TransferDataFromWindow();
366 // TODO probably need to disable the Apply button until things change again
369 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
371 EndModal(wxID_CANCEL
);
374 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
376 // We'll send a Cancel message by default, which may close the dialog.
377 // Check for looping if the Cancel event handler calls Close().
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
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.
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!
390 static wxList closing
;
392 if ( closing
.Member(this) )
395 closing
.Append(this);
397 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
398 cancelEvent
.SetEventObject( this );
399 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
401 closing
.DeleteObject(this);
404 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
409 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
414 // ---------------------------------------------------------------------------
415 // dialog window proc
416 // ---------------------------------------------------------------------------
418 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
421 bool processed
= FALSE
;
426 // if we can't close, tell the system that we processed the
427 // message - otherwise it would close us
428 processed
= !Close();
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
);
440 if ( !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
442 ::InvalidateRect(GetHwnd(), NULL
, FALSE
/* erase bg */);
446 #ifndef __WXMICROWIN__
448 // we want to override the busy cursor for modal dialogs:
449 // typically, wxBeginBusyCursor() is called and then a modal dialog
450 // is shown, but the modal dialog shouldn't have hourglass cursor
451 if ( IsModalShowing() && wxIsBusy() )
453 // set our cursor for all windows (but see below)
454 wxCursor cursor
= m_cursor
;
456 cursor
= wxCURSOR_ARROW
;
458 ::SetCursor(GetHcursorOf(cursor
));
460 // in any case, stop here and don't let wxWindow process this
461 // message (it would set the busy cursor)
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()
472 #endif // __WXMICROWIN__
476 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
483 // Define for each class of dialog and control
484 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
485 WXHWND
WXUNUSED(pWnd
),
486 WXUINT
WXUNUSED(nCtlColor
),
491 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
494 #endif // wxUSE_CTL3D