1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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
)
76 style (wxDEFAULT_DIALOG_STYLE)
77 centered (bool, false )
80 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
81 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
82 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
83 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
85 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
87 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
89 EVT_CLOSE(wxDialog::OnCloseWindow
)
92 // ============================================================================
94 // ============================================================================
96 // ----------------------------------------------------------------------------
97 // wxDialog construction
98 // ----------------------------------------------------------------------------
100 void wxDialog::Init()
102 m_oldFocus
= (wxWindow
*)NULL
;
106 m_windowDisabler
= (wxWindowDisabler
*)NULL
;
108 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
111 bool wxDialog::Create(wxWindow
*parent
,
113 const wxString
& title
,
117 const wxString
& name
)
121 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
123 // save focus before doing anything which can potentially change it
124 m_oldFocus
= FindFocus();
126 // All dialogs should really have this style
127 style
|= wxTAB_TRAVERSAL
;
129 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
132 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
137 void wxDialog::SetModal(bool flag
)
141 m_windowStyle
|= wxDIALOG_MODAL
;
143 wxModelessWindows
.DeleteObject(this);
147 m_windowStyle
&= ~wxDIALOG_MODAL
;
149 wxModelessWindows
.Append(this);
153 wxDialog::~wxDialog()
155 m_isBeingDeleted
= TRUE
;
157 // this will also reenable all the other windows for a modal dialog
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 // By default, pressing escape cancels the dialog
166 void wxDialog::OnCharHook(wxKeyEvent
& event
)
170 // "Esc" works as an accelerator for the "Cancel" button, but it
171 // shouldn't close the dialog which doesn't have any cancel button
172 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
174 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
175 cancelEvent
.SetEventObject( this );
176 GetEventHandler()->ProcessEvent(cancelEvent
);
178 // ensure that there is another message for this window so the
179 // ShowModal loop will exit and won't get stuck in GetMessage().
180 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
186 // We didn't process this event.
190 // ----------------------------------------------------------------------------
191 // showing the dialogs
192 // ----------------------------------------------------------------------------
194 bool wxDialog::IsModal() const
196 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
199 bool wxDialog::IsModalShowing() const
201 return wxModalDialogs
.Find(wxConstCast(this, wxDialog
)) != NULL
;
204 wxWindow
*wxDialog::FindSuitableParent() const
206 // first try to use the currently active window
207 HWND hwndFg
= ::GetForegroundWindow();
208 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
212 // next try the main app window
213 parent
= wxTheApp
->GetTopWindow();
216 // finally, check if the parent we found is really suitable
217 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
219 // don't use this one
226 void wxDialog::DoShowModal()
228 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
229 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
231 wxModalDialogs
.Append(this);
233 wxWindow
*parent
= GetParent();
235 wxWindow
* oldFocus
= m_oldFocus
;
237 // We have to remember the HWND because we need to check
238 // the HWND still exists (oldFocus can be garbage when the dialog
239 // exits, if it has been destroyed)
240 HWND hwndOldFocus
= 0;
242 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
244 // remember where the focus was
249 hwndOldFocus
= GetHwndOf(parent
);
252 // disable all other app windows
253 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
255 m_windowDisabler
= new wxWindowDisabler(this);
257 // before entering the modal loop, reset the "is in OnIdle()" flag (see
258 // comment in app.cpp)
259 extern bool wxIsInOnIdleFlag
;
260 bool wasInOnIdle
= wxIsInOnIdleFlag
;
261 wxIsInOnIdleFlag
= FALSE
;
263 // enter the modal loop
264 while ( IsModalShowing() )
267 wxMutexGuiLeaveOrEnter();
268 #endif // wxUSE_THREADS
270 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
273 // a message came or no more idle processing to do
274 wxTheApp
->DoMessage();
277 wxIsInOnIdleFlag
= wasInOnIdle
;
280 // Note that this code MUST NOT access the dialog object's data
281 // in case the object has been deleted (which will be the case
282 // for a modal dialog that has been destroyed before calling EndModal).
283 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
285 // This is likely to prove that the object still exists
286 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
287 oldFocus
->SetFocus();
291 bool wxDialog::Show(bool show
)
295 // if we had disabled other app windows, reenable them back now because
296 // if they stay disabled Windows will activate another window (one
297 // which is enabled, anyhow) and we will lose activation
298 if ( m_windowDisabler
)
300 delete m_windowDisabler
;
301 m_windowDisabler
= NULL
;
305 // ShowModal() may be called for already shown dialog
306 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
314 // dialogs don't get WM_SIZE message after creation unlike most (all?)
315 // other windows and so could start their life non laid out correctly
316 // if we didn't call Layout() from here
318 // NB: normally we should call it just the first time but doing it
319 // every time is simpler than keeping a flag
322 // usually will result in TransferDataToWindow() being called
330 // modal dialog needs a parent window, so try to find one
333 m_parent
= FindSuitableParent();
338 else // end of modal dialog
340 // this will cause IsModalShowing() return FALSE and our local
341 // message loop will terminate
342 wxModalDialogs
.DeleteObject(this);
344 // ensure that there is another message for this window so the
345 // ShowModal loop will exit and won't get stuck in GetMessage().
346 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
353 void wxDialog::Raise()
355 ::SetForegroundWindow(GetHwnd());
358 // a special version for Show(TRUE) for modal dialogs which returns return code
359 int wxDialog::ShowModal()
368 return GetReturnCode();
371 // NB: this function (surprizingly) may be called for both modal and modeless
372 // dialogs and should work for both of them
373 void wxDialog::EndModal(int retCode
)
375 SetReturnCode(retCode
);
380 // ----------------------------------------------------------------------------
381 // wxWin event handlers
382 // ----------------------------------------------------------------------------
385 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
387 if ( Validate() && TransferDataFromWindow() )
393 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
396 TransferDataFromWindow();
398 // TODO probably need to disable the Apply button until things change again
401 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
403 EndModal(wxID_CANCEL
);
406 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
408 // We'll send a Cancel message by default, which may close the dialog.
409 // Check for looping if the Cancel event handler calls Close().
411 // Note that if a cancel button and handler aren't present in the dialog,
412 // nothing will happen when you close the dialog via the window manager, or
413 // via Close(). We wouldn't want to destroy the dialog by default, since
414 // the dialog may have been created on the stack. However, this does mean
415 // that calling dialog->Close() won't delete the dialog unless the handler
416 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
417 // destroy the dialog. The default OnCancel (above) simply ends a modal
418 // dialog, and hides a modeless dialog.
420 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
421 // lists here? don't dare to change it now, but should be done later!
422 static wxList closing
;
424 if ( closing
.Member(this) )
427 closing
.Append(this);
429 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
430 cancelEvent
.SetEventObject( this );
431 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
433 closing
.DeleteObject(this);
436 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
441 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
446 // ---------------------------------------------------------------------------
447 // dialog window proc
448 // ---------------------------------------------------------------------------
450 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
453 bool processed
= FALSE
;
458 // if we can't close, tell the system that we processed the
459 // message - otherwise it would close us
460 processed
= !Close();
464 // the Windows dialogs unfortunately are not meant to be resizeable
465 // at all and their standard class doesn't include CS_[VH]REDRAW
466 // styles which means that the window is not refreshed properly
467 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
468 // help with it - so we have to refresh it manually which certainly
469 // creates flicker but at least doesn't show garbage on the screen
470 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
472 if ( !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
474 ::InvalidateRect(GetHwnd(), NULL
, FALSE
/* erase bg */);
478 #ifndef __WXMICROWIN__
480 // we want to override the busy cursor for modal dialogs:
481 // typically, wxBeginBusyCursor() is called and then a modal dialog
482 // is shown, but the modal dialog shouldn't have hourglass cursor
483 if ( IsModalShowing() && wxIsBusy() )
485 // set our cursor for all windows (but see below)
486 wxCursor cursor
= m_cursor
;
488 cursor
= wxCURSOR_ARROW
;
490 ::SetCursor(GetHcursorOf(cursor
));
492 // in any case, stop here and don't let wxWindow process this
493 // message (it would set the busy cursor)
496 // but return FALSE to tell the child window (if the event
497 // comes from one of them and not from ourselves) that it can
498 // set its own cursor if it has one: thus, standard controls
499 // (e.g. text ctrl) still have correct cursors in a dialog
500 // invoked while wxIsBusy()
504 #endif // __WXMICROWIN__
508 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
515 // Define for each class of dialog and control
516 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
517 WXHWND
WXUNUSED(pWnd
),
518 WXUINT
WXUNUSED(nCtlColor
),
523 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
526 #endif // wxUSE_CTL3D