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
) )
128 bool wxDialog::EnableCloseButton(bool enable
)
130 #ifndef __WXMICROWIN__
131 // get system (a.k.a. window) menu
132 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
135 wxLogLastError(_T("GetSystemMenu"));
140 // enabling/disabling the close item from it also automatically
141 // disables/enabling the close title bar button
142 if ( !::EnableMenuItem(hmenu
, SC_CLOSE
,
143 MF_BYCOMMAND
| (enable
? MF_ENABLED
: MF_GRAYED
)) )
145 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
150 // update appearance immediately
151 if ( !::DrawMenuBar(GetHwnd()) )
153 wxLogLastError(_T("DrawMenuBar"));
160 void wxDialog::SetModal(bool flag
)
164 m_windowStyle
|= wxDIALOG_MODAL
;
166 wxModelessWindows
.DeleteObject(this);
170 m_windowStyle
&= ~wxDIALOG_MODAL
;
172 wxModelessWindows
.Append(this);
176 wxDialog::~wxDialog()
178 m_isBeingDeleted
= TRUE
;
180 // this will also reenable all the other windows for a modal dialog
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 // By default, pressing escape cancels the dialog
189 void wxDialog::OnCharHook(wxKeyEvent
& event
)
193 // "Esc" works as an accelerator for the "Cancel" button, but it
194 // shouldn't close the dialog which doesn't have any cancel button
195 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
197 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
198 cancelEvent
.SetEventObject( this );
199 GetEventHandler()->ProcessEvent(cancelEvent
);
201 // ensure that there is another message for this window so the
202 // ShowModal loop will exit and won't get stuck in GetMessage().
203 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
209 // We didn't process this event.
213 // ----------------------------------------------------------------------------
214 // showing the dialogs
215 // ----------------------------------------------------------------------------
217 bool wxDialog::IsModal() const
219 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
222 bool wxDialog::IsModalShowing() const
224 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
227 void wxDialog::DoShowModal()
229 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
230 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
232 wxModalDialogs
.Append(this);
234 wxWindow
*parent
= GetParent();
236 wxWindow
* oldFocus
= m_oldFocus
;
238 // We have to remember the HWND because we need to check
239 // the HWND still exists (oldFocus can be garbage when the dialog
240 // exits, if it has been destroyed)
241 HWND hwndOldFocus
= 0;
243 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
245 // remember where the focus was
250 hwndOldFocus
= GetHwndOf(parent
);
253 // disable all other app windows
254 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
256 m_windowDisabler
= new wxWindowDisabler(this);
258 // enter the modal loop
259 while ( IsModalShowing() )
262 wxMutexGuiLeaveOrEnter();
263 #endif // wxUSE_THREADS
265 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
268 // a message came or no more idle processing to do
269 wxTheApp
->DoMessage();
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
))
278 // This is likely to prove that the object still exists
279 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
280 oldFocus
->SetFocus();
284 bool wxDialog::Show(bool show
)
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
)
293 delete m_windowDisabler
;
294 m_windowDisabler
= NULL
;
298 // ShowModal() may be called for already shown dialog
299 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
307 // usually will result in TransferDataToWindow() being called
315 // modal dialog needs a parent window, so try to find one
318 wxWindow
*parent
= wxTheApp
->GetTopWindow();
319 if ( parent
&& parent
!= this && parent
->IsShown() )
324 // VZ: to make dialog behave properly we should reparent
325 // the dialog for Windows as well - unfortunately,
326 // following the docs for SetParent() results in this
327 // code which plainly doesn't work
329 long dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
330 dwStyle
&= ~WS_POPUP
;
332 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyle
);
333 ::SetParent(GetHwnd(), GetHwndOf(parent
));
340 else // end of modal dialog
342 // this will cause IsModalShowing() return FALSE and our local
343 // message loop will terminate
344 wxModalDialogs
.DeleteObject(this);
351 // a special version for Show(TRUE) for modal dialogs which returns return code
352 int wxDialog::ShowModal()
361 return GetReturnCode();
364 // NB: this function (surprizingly) may be called for both modal and modeless
365 // dialogs and should work for both of them
366 void wxDialog::EndModal(int retCode
)
368 SetReturnCode(retCode
);
373 // ----------------------------------------------------------------------------
374 // wxWin event handlers
375 // ----------------------------------------------------------------------------
378 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
380 if ( Validate() && TransferDataFromWindow() )
386 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
389 TransferDataFromWindow();
391 // TODO probably need to disable the Apply button until things change again
394 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
396 EndModal(wxID_CANCEL
);
399 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
401 // We'll send a Cancel message by default, which may close the dialog.
402 // Check for looping if the Cancel event handler calls Close().
404 // Note that if a cancel button and handler aren't present in the dialog,
405 // nothing will happen when you close the dialog via the window manager, or
406 // via Close(). We wouldn't want to destroy the dialog by default, since
407 // the dialog may have been created on the stack. However, this does mean
408 // that calling dialog->Close() won't delete the dialog unless the handler
409 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
410 // destroy the dialog. The default OnCancel (above) simply ends a modal
411 // dialog, and hides a modeless dialog.
413 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
414 // lists here? don't dare to change it now, but should be done later!
415 static wxList closing
;
417 if ( closing
.Member(this) )
420 closing
.Append(this);
422 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
423 cancelEvent
.SetEventObject( this );
424 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
426 closing
.DeleteObject(this);
429 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
434 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
439 // ---------------------------------------------------------------------------
440 // dialog window proc
441 // ---------------------------------------------------------------------------
443 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
446 bool processed
= FALSE
;
450 #if 0 // now that we got owner window right it doesn't seem to be needed
452 switch ( LOWORD(wParam
) )
456 if ( IsModalShowing() && GetParent() )
458 // bring the owner window to top as the standard dialog
462 GetHwndOf(GetParent()),
471 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
474 // fall through to process it normally as well
480 // if we can't close, tell the system that we processed the
481 // message - otherwise it would close us
482 processed
= !Close();
485 #ifndef __WXMICROWIN__
487 // we want to override the busy cursor for modal dialogs:
488 // typically, wxBeginBusyCursor() is called and then a modal dialog
489 // is shown, but the modal dialog shouldn't have hourglass cursor
490 if ( IsModalShowing() && wxIsBusy() )
492 // set our cursor for all windows (but see below)
493 wxCursor cursor
= m_cursor
;
495 cursor
= wxCURSOR_ARROW
;
497 ::SetCursor(GetHcursorOf(cursor
));
499 // in any case, stop here and don't let wxWindow process this
500 // message (it would set the busy cursor)
503 // but return FALSE to tell the child window (if the event
504 // comes from one of them and not from ourselves) that it can
505 // set its own cursor if it has one: thus, standard controls
506 // (e.g. text ctrl) still have correct cursors in a dialog
507 // invoked while wxIsBusy()
511 #endif // __WXMICROWIN__
515 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
522 // Define for each class of dialog and control
523 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
524 WXHWND
WXUNUSED(pWnd
),
525 WXUINT
WXUNUSED(nCtlColor
),
530 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
533 #endif // wxUSE_CTL3D