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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
43 #include "wx/evtloop.h"
45 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // default dialog pos and size
55 #define wxDIALOG_DEFAULT_X 300
56 #define wxDIALOG_DEFAULT_Y 300
58 #define wxDIALOG_DEFAULT_WIDTH 500
59 #define wxDIALOG_DEFAULT_HEIGHT 500
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #if wxUSE_EXTENDED_RTTI
66 WX_DEFINE_FLAGS( wxDialogStyle
)
68 wxBEGIN_FLAGS( wxDialogStyle
)
69 // new style border flags, we put them first to
70 // use them for streaming out
71 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
72 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
73 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
74 wxFLAGS_MEMBER(wxBORDER_RAISED
)
75 wxFLAGS_MEMBER(wxBORDER_STATIC
)
76 wxFLAGS_MEMBER(wxBORDER_NONE
)
78 // old style border flags
79 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
80 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
81 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
82 wxFLAGS_MEMBER(wxRAISED_BORDER
)
83 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
84 wxFLAGS_MEMBER(wxNO_BORDER
)
86 // standard window styles
87 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
88 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
91 wxFLAGS_MEMBER(wxDIALOG_MODAL
)
92 wxFLAGS_MEMBER(wxDIALOG_MODELESS
)
93 wxFLAGS_MEMBER(wxNO_3D
)
94 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
95 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
96 wxFLAGS_MEMBER(wxCAPTION
)
97 wxFLAGS_MEMBER(wxTHICK_FRAME
)
98 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
99 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
100 wxFLAGS_MEMBER(wxRESIZE_BOX
)
101 wxFLAGS_MEMBER(wxCLOSE_BOX
)
102 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
103 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
104 wxEND_FLAGS( wxDialogStyle
)
106 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
108 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
109 wxPROPERTY( Title
,wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
110 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
111 wxEND_PROPERTIES_TABLE()
113 wxBEGIN_HANDLERS_TABLE(wxDialog
)
114 wxEND_HANDLERS_TABLE()
116 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
119 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
122 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
123 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
124 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
125 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
127 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
129 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
131 EVT_CLOSE(wxDialog::OnCloseWindow
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 // this is simply a container for wxEventLoop and wxWindowDisabler which allows
139 // to have a single opaque pointer in wxDialog itself
140 class wxDialogModalData
143 wxDialogModalData() { m_windowDisabler
= NULL
; }
145 void RunLoop(wxDialog
*dialog
)
147 m_windowDisabler
= new wxWindowDisabler(dialog
);
154 delete m_windowDisabler
;
155 m_windowDisabler
= NULL
;
162 wxASSERT_MSG( !m_windowDisabler
, _T("forgot to call ExitLoop?") );
166 wxEventLoop m_evtLoop
;
167 wxWindowDisabler
*m_windowDisabler
;
170 // ============================================================================
172 // ============================================================================
174 // ----------------------------------------------------------------------------
175 // wxDialog construction
176 // ----------------------------------------------------------------------------
178 void wxDialog::Init()
180 m_oldFocus
= (wxWindow
*)NULL
;
186 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
189 bool wxDialog::Create(wxWindow
*parent
,
191 const wxString
& title
,
195 const wxString
& name
)
199 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
201 // save focus before doing anything which can potentially change it
202 m_oldFocus
= FindFocus();
204 // All dialogs should really have this style
205 style
|= wxTAB_TRAVERSAL
;
207 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
210 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
215 void wxDialog::SetModal(bool flag
)
219 m_windowStyle
|= wxDIALOG_MODAL
;
221 wxModelessWindows
.DeleteObject(this);
225 m_windowStyle
&= ~wxDIALOG_MODAL
;
227 wxModelessWindows
.Append(this);
231 wxDialog::~wxDialog()
233 m_isBeingDeleted
= TRUE
;
235 // this will also reenable all the other windows for a modal dialog
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 // By default, pressing escape cancels the dialog
244 void wxDialog::OnCharHook(wxKeyEvent
& event
)
248 // "Esc" works as an accelerator for the "Cancel" button, but it
249 // shouldn't close the dialog which doesn't have any cancel button
250 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
252 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
253 cancelEvent
.SetEventObject( this );
254 GetEventHandler()->ProcessEvent(cancelEvent
);
256 // ensure that there is another message for this window so the
257 // ShowModal loop will exit and won't get stuck in GetMessage().
258 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
264 // We didn't process this event.
268 // ----------------------------------------------------------------------------
269 // showing the dialogs
270 // ----------------------------------------------------------------------------
272 bool wxDialog::IsModal() const
274 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
277 bool wxDialog::IsModalShowing() const
279 return m_modalData
!= NULL
;
282 wxWindow
*wxDialog::FindSuitableParent() const
284 // first try to use the currently active window
285 HWND hwndFg
= ::GetForegroundWindow();
286 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
290 // next try the main app window
291 parent
= wxTheApp
->GetTopWindow();
294 // finally, check if the parent we found is really suitable
295 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
297 // don't use this one
304 void wxDialog::DoShowModal()
306 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
307 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
309 wxWindow
*parent
= GetParent();
311 wxWindow
* oldFocus
= m_oldFocus
;
313 // We have to remember the HWND because we need to check
314 // the HWND still exists (oldFocus can be garbage when the dialog
315 // exits, if it has been destroyed)
316 HWND hwndOldFocus
= 0;
318 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
320 // remember where the focus was
325 hwndOldFocus
= GetHwndOf(parent
);
328 // before entering the modal loop, reset the "is in OnIdle()" flag (see
329 // comment in app.cpp)
330 extern bool wxIsInOnIdleFlag
;
331 bool wasInOnIdle
= wxIsInOnIdleFlag
;
332 wxIsInOnIdleFlag
= FALSE
;
334 // enter the modal loop
335 m_modalData
= new wxDialogModalData
;
336 m_modalData
->RunLoop(this);
340 wxIsInOnIdleFlag
= wasInOnIdle
;
343 // Note that this code MUST NOT access the dialog object's data
344 // in case the object has been deleted (which will be the case
345 // for a modal dialog that has been destroyed before calling EndModal).
346 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
348 // This is likely to prove that the object still exists
349 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
350 oldFocus
->SetFocus();
354 bool wxDialog::Show(bool show
)
356 if ( !show
&& m_modalData
)
358 // we need to do this before calling wxDialogBase version because if we
359 // had disabled other app windows, they must be reenabled right now as
360 // if they stay disabled Windows will activate another window (one
361 // which is enabled, anyhow) when we're hidden in the base class Show()
362 // and we will lose activation
363 m_modalData
->ExitLoop();
366 // ShowModal() may be called for already shown dialog
367 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
375 // dialogs don't get WM_SIZE message after creation unlike most (all?)
376 // other windows and so could start their life non laid out correctly
377 // if we didn't call Layout() from here
379 // NB: normally we should call it just the first time but doing it
380 // every time is simpler than keeping a flag
383 // this usually will result in TransferDataToWindow() being called
387 if ( show
&& IsModal() )
389 // modal dialog needs a parent window, so try to find one
392 m_parent
= FindSuitableParent();
401 void wxDialog::Raise()
403 ::SetForegroundWindow(GetHwnd());
406 // a special version for Show(TRUE) for modal dialogs which returns return code
407 int wxDialog::ShowModal()
416 return GetReturnCode();
419 // NB: this function (surprizingly) may be called for both modal and modeless
420 // dialogs and should work for both of them
421 void wxDialog::EndModal(int retCode
)
423 SetReturnCode(retCode
);
428 // ----------------------------------------------------------------------------
429 // wxWin event handlers
430 // ----------------------------------------------------------------------------
433 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
435 if ( Validate() && TransferDataFromWindow() )
441 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
444 TransferDataFromWindow();
446 // TODO probably need to disable the Apply button until things change again
449 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
451 EndModal(wxID_CANCEL
);
454 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
456 // We'll send a Cancel message by default, which may close the dialog.
457 // Check for looping if the Cancel event handler calls Close().
459 // Note that if a cancel button and handler aren't present in the dialog,
460 // nothing will happen when you close the dialog via the window manager, or
461 // via Close(). We wouldn't want to destroy the dialog by default, since
462 // the dialog may have been created on the stack. However, this does mean
463 // that calling dialog->Close() won't delete the dialog unless the handler
464 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
465 // destroy the dialog. The default OnCancel (above) simply ends a modal
466 // dialog, and hides a modeless dialog.
468 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
469 // lists here? don't dare to change it now, but should be done later!
470 static wxList closing
;
472 if ( closing
.Member(this) )
475 closing
.Append(this);
477 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
478 cancelEvent
.SetEventObject( this );
479 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
481 closing
.DeleteObject(this);
484 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
489 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
494 // ---------------------------------------------------------------------------
495 // dialog window proc
496 // ---------------------------------------------------------------------------
498 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
501 bool processed
= FALSE
;
506 // if we can't close, tell the system that we processed the
507 // message - otherwise it would close us
508 processed
= !Close();
512 // the Windows dialogs unfortunately are not meant to be resizeable
513 // at all and their standard class doesn't include CS_[VH]REDRAW
514 // styles which means that the window is not refreshed properly
515 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
516 // help with it - so we have to refresh it manually which certainly
517 // creates flicker but at least doesn't show garbage on the screen
518 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
520 if ( !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE
) )
522 ::InvalidateRect(GetHwnd(), NULL
, FALSE
/* erase bg */);
526 #ifndef __WXMICROWIN__
528 // we want to override the busy cursor for modal dialogs:
529 // typically, wxBeginBusyCursor() is called and then a modal dialog
530 // is shown, but the modal dialog shouldn't have hourglass cursor
531 if ( IsModalShowing() && wxIsBusy() )
533 // set our cursor for all windows (but see below)
534 wxCursor cursor
= m_cursor
;
536 cursor
= wxCURSOR_ARROW
;
538 ::SetCursor(GetHcursorOf(cursor
));
540 // in any case, stop here and don't let wxWindow process this
541 // message (it would set the busy cursor)
544 // but return FALSE to tell the child window (if the event
545 // comes from one of them and not from ourselves) that it can
546 // set its own cursor if it has one: thus, standard controls
547 // (e.g. text ctrl) still have correct cursors in a dialog
548 // invoked while wxIsBusy()
552 #endif // __WXMICROWIN__
556 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
563 // Define for each class of dialog and control
564 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
565 WXHWND
WXUNUSED(pWnd
),
566 WXUINT
WXUNUSED(nCtlColor
),
571 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
574 #endif // wxUSE_CTL3D