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
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
, wxTopLevelWindow
)
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::GetSystemColour(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::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
130 bool wxDialog::EnableCloseButton(bool enable
)
132 #ifndef __WXMICROWIN__
133 // get system (a.k.a. window) menu
134 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
137 wxLogLastError(_T("GetSystemMenu"));
142 // enabling/disabling the close item from it also automatically
143 // disables/enabling the close title bar button
144 if ( !::EnableMenuItem(hmenu
, SC_CLOSE
,
145 MF_BYCOMMAND
| (enable
? MF_ENABLED
: MF_GRAYED
)) )
147 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
152 // update appearance immediately
153 if ( !::DrawMenuBar(GetHwnd()) )
155 wxLogLastError(_T("DrawMenuBar"));
162 void wxDialog::SetModal(bool flag
)
166 m_windowStyle
|= wxDIALOG_MODAL
;
168 wxModelessWindows
.DeleteObject(this);
172 m_windowStyle
&= ~wxDIALOG_MODAL
;
174 wxModelessWindows
.Append(this);
178 wxDialog::~wxDialog()
180 m_isBeingDeleted
= TRUE
;
182 // this will also reenable all the other windows for a modal dialog
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 // By default, pressing escape cancels the dialog
191 void wxDialog::OnCharHook(wxKeyEvent
& event
)
195 // "Esc" works as an accelerator for the "Cancel" button, but it
196 // shouldn't close the dialog which doesn't have any cancel button
197 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
199 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
200 cancelEvent
.SetEventObject( this );
201 GetEventHandler()->ProcessEvent(cancelEvent
);
203 // ensure that there is another message for this window so the
204 // ShowModal loop will exit and won't get stuck in GetMessage().
205 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
211 // We didn't process this event.
215 // ----------------------------------------------------------------------------
216 // showing the dialogs
217 // ----------------------------------------------------------------------------
219 bool wxDialog::IsModal() const
221 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
224 bool wxDialog::IsModalShowing() const
226 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
229 void wxDialog::DoShowModal()
231 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
232 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
234 wxModalDialogs
.Append(this);
236 wxWindow
*parent
= GetParent();
238 wxWindow
* oldFocus
= m_oldFocus
;
240 // We have to remember the HWND because we need to check
241 // the HWND still exists (oldFocus can be garbage when the dialog
242 // exits, if it has been destroyed)
243 HWND hwndOldFocus
= 0;
245 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
247 // remember where the focus was
252 hwndOldFocus
= GetHwndOf(parent
);
255 // disable all other app windows
256 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
258 m_windowDisabler
= new wxWindowDisabler(this);
260 // enter the modal loop
261 while ( IsModalShowing() )
264 wxMutexGuiLeaveOrEnter();
265 #endif // wxUSE_THREADS
267 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
270 // a message came or no more idle processing to do
271 wxTheApp
->DoMessage();
275 // Note that this code MUST NOT access the dialog object's data
276 // in case the object has been deleted (which will be the case
277 // for a modal dialog that has been destroyed before calling EndModal).
278 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
280 // This is likely to prove that the object still exists
281 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
282 oldFocus
->SetFocus();
286 bool wxDialog::Show(bool show
)
290 // if we had disabled other app windows, reenable them back now because
291 // if they stay disabled Windows will activate another window (one
292 // which is enabled, anyhow) and we will lose activation
293 if ( m_windowDisabler
)
295 delete m_windowDisabler
;
296 m_windowDisabler
= NULL
;
300 // ShowModal() may be called for already shown dialog
301 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
309 // usually will result in TransferDataToWindow() being called
317 // modal dialog needs a parent window, so try to find one
320 wxWindow
*parent
= wxTheApp
->GetTopWindow();
321 if ( parent
&& parent
!= this && parent
->IsShown() )
326 // VZ: to make dialog behave properly we should reparent
327 // the dialog for Windows as well - unfortunately,
328 // following the docs for SetParent() results in this
329 // code which plainly doesn't work
331 long dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
332 dwStyle
&= ~WS_POPUP
;
334 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyle
);
335 ::SetParent(GetHwnd(), GetHwndOf(parent
));
342 else // end of modal dialog
344 // this will cause IsModalShowing() return FALSE and our local
345 // message loop will terminate
346 wxModalDialogs
.DeleteObject(this);
353 // a special version for Show(TRUE) for modal dialogs which returns return code
354 int wxDialog::ShowModal()
363 return GetReturnCode();
366 // NB: this function (surprizingly) may be called for both modal and modeless
367 // dialogs and should work for both of them
368 void wxDialog::EndModal(int retCode
)
370 SetReturnCode(retCode
);
375 // ----------------------------------------------------------------------------
376 // wxWin event handlers
377 // ----------------------------------------------------------------------------
380 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
382 if ( Validate() && TransferDataFromWindow() )
388 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
391 TransferDataFromWindow();
393 // TODO probably need to disable the Apply button until things change again
396 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
398 EndModal(wxID_CANCEL
);
401 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
403 // We'll send a Cancel message by default, which may close the dialog.
404 // Check for looping if the Cancel event handler calls Close().
406 // Note that if a cancel button and handler aren't present in the dialog,
407 // nothing will happen when you close the dialog via the window manager, or
408 // via Close(). We wouldn't want to destroy the dialog by default, since
409 // the dialog may have been created on the stack. However, this does mean
410 // that calling dialog->Close() won't delete the dialog unless the handler
411 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
412 // destroy the dialog. The default OnCancel (above) simply ends a modal
413 // dialog, and hides a modeless dialog.
415 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
416 // lists here? don't dare to change it now, but should be done later!
417 static wxList closing
;
419 if ( closing
.Member(this) )
422 closing
.Append(this);
424 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
425 cancelEvent
.SetEventObject( this );
426 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
428 closing
.DeleteObject(this);
431 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
436 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
441 // ---------------------------------------------------------------------------
442 // dialog window proc
443 // ---------------------------------------------------------------------------
445 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
448 bool processed
= FALSE
;
452 #if 0 // now that we got owner window right it doesn't seem to be needed
454 switch ( LOWORD(wParam
) )
458 if ( IsModalShowing() && GetParent() )
460 // bring the owner window to top as the standard dialog
464 GetHwndOf(GetParent()),
473 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
476 // fall through to process it normally as well
482 // if we can't close, tell the system that we processed the
483 // message - otherwise it would close us
484 processed
= !Close();
487 #ifndef __WXMICROWIN__
489 // we want to override the busy cursor for modal dialogs:
490 // typically, wxBeginBusyCursor() is called and then a modal dialog
491 // is shown, but the modal dialog shouldn't have hourglass cursor
492 if ( IsModalShowing() && wxIsBusy() )
494 // set our cursor for all windows (but see below)
495 wxCursor cursor
= m_cursor
;
497 cursor
= wxCURSOR_ARROW
;
499 ::SetCursor(GetHcursorOf(cursor
));
501 // in any case, stop here and don't let wxWindow process this
502 // message (it would set the busy cursor)
505 // but return FALSE to tell the child window (if the event
506 // comes from one of them and not from ourselves) that it can
507 // set its own cursor if it has one: thus, standard controls
508 // (e.g. text ctrl) still have correct cursors in a dialog
509 // invoked while wxIsBusy()
513 #endif // __WXMICROWIN__
517 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
524 // Define for each class of dialog and control
525 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
526 WXHWND
WXUNUSED(pWnd
),
527 WXUINT
WXUNUSED(nCtlColor
),
532 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
535 #endif // wxUSE_CTL3D