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"
44 #include "wx/ptr_scpd.h"
46 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #if wxUSE_EXTENDED_RTTI
55 WX_DEFINE_FLAGS( wxDialogStyle
)
57 wxBEGIN_FLAGS( wxDialogStyle
)
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
63 wxFLAGS_MEMBER(wxBORDER_RAISED
)
64 wxFLAGS_MEMBER(wxBORDER_STATIC
)
65 wxFLAGS_MEMBER(wxBORDER_NONE
)
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
71 wxFLAGS_MEMBER(wxRAISED_BORDER
)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
73 wxFLAGS_MEMBER(wxNO_BORDER
)
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
80 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
81 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
82 wxFLAGS_MEMBER(wxCAPTION
)
83 wxFLAGS_MEMBER(wxTHICK_FRAME
)
84 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
85 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
86 wxFLAGS_MEMBER(wxRESIZE_BOX
)
87 wxFLAGS_MEMBER(wxCLOSE_BOX
)
88 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
89 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
90 wxEND_FLAGS( wxDialogStyle
)
92 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
94 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
95 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
96 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
97 wxEND_PROPERTIES_TABLE()
99 wxBEGIN_HANDLERS_TABLE(wxDialog
)
100 wxEND_HANDLERS_TABLE()
102 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
105 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
108 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
109 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
110 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
111 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
113 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
115 EVT_CLOSE(wxDialog::OnCloseWindow
)
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // this is simply a container for any data we need to implement modality which
123 // allows us to avoid changing wxDialog each time the implementation changes
124 class wxDialogModalData
127 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
140 wxModalEventLoop m_evtLoop
;
143 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
);
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
150 // wxDialog construction
151 // ----------------------------------------------------------------------------
153 void wxDialog::Init()
155 m_oldFocus
= (wxWindow
*)NULL
;
158 m_endModalCalled
= FALSE
;
161 bool wxDialog::Create(wxWindow
*parent
,
163 const wxString
& title
,
167 const wxString
& name
)
169 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
171 // save focus before doing anything which can potentially change it
172 m_oldFocus
= FindFocus();
174 // All dialogs should really have this style
175 style
|= wxTAB_TRAVERSAL
;
177 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
181 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
183 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
189 wxDialog::wxDialog(wxWindow
*parent
,
190 const wxString
& title
,
191 bool WXUNUSED(modal
),
197 const wxString
& name
)
201 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
), style
, name
);
204 void wxDialog::SetModal(bool WXUNUSED(flag
))
206 // nothing to do, obsolete method
209 wxDialog::~wxDialog()
211 m_isBeingDeleted
= TRUE
;
213 // this will also reenable all the other windows for a modal dialog
217 // ----------------------------------------------------------------------------
218 // showing the dialogs
219 // ----------------------------------------------------------------------------
221 bool wxDialog::IsModalShowing() const
226 wxWindow
*wxDialog::FindSuitableParent() const
228 // first try to use the currently active window
229 HWND hwndFg
= ::GetForegroundWindow();
230 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
234 // next try the main app window
235 parent
= wxTheApp
->GetTopWindow();
238 // finally, check if the parent we found is really suitable
239 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
241 // don't use this one
248 bool wxDialog::Show(bool show
)
250 if ( !show
&& m_modalData
)
252 // we need to do this before calling wxDialogBase version because if we
253 // had disabled other app windows, they must be reenabled right now as
254 // if they stay disabled Windows will activate another window (one
255 // which is enabled, anyhow) when we're hidden in the base class Show()
256 // and we will lose activation
257 m_modalData
->ExitLoop();
260 // ShowModal() may be called for already shown dialog
261 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
269 // dialogs don't get WM_SIZE message after creation unlike most (all?)
270 // other windows and so could start their life non laid out correctly
271 // if we didn't call Layout() from here
273 // NB: normally we should call it just the first time but doing it
274 // every time is simpler than keeping a flag
277 // this usually will result in TransferDataToWindow() being called
284 void wxDialog::Raise()
286 ::SetForegroundWindow(GetHwnd());
289 // show dialog modally
290 int wxDialog::ShowModal()
292 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
294 m_endModalCalled
= FALSE
;
298 // EndModal may have been called from InitDialog handler (called from
299 // inside Show()), which would cause an infinite loop if we didn't take it
301 if ( !m_endModalCalled
)
303 // modal dialog needs a parent window, so try to find one
304 wxWindow
*parent
= GetParent();
307 parent
= FindSuitableParent();
310 // remember where the focus was
311 wxWindow
*oldFocus
= m_oldFocus
;
314 // VZ: do we really want to do this?
318 // We have to remember the HWND because we need to check
319 // the HWND still exists (oldFocus can be garbage when the dialog
320 // exits, if it has been destroyed)
321 HWND hwndOldFocus
= oldFocus
? GetHwndOf(oldFocus
) : NULL
;
324 // enter and run the modal loop
326 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
327 new wxDialogModalData(this));
328 modalData
->RunLoop();
333 // Note that this code MUST NOT access the dialog object's data
334 // in case the object has been deleted (which will be the case
335 // for a modal dialog that has been destroyed before calling EndModal).
336 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
338 // This is likely to prove that the object still exists
339 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
340 oldFocus
->SetFocus();
344 return GetReturnCode();
347 void wxDialog::EndModal(int retCode
)
349 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
351 m_endModalCalled
= TRUE
;
352 SetReturnCode(retCode
);
357 // ----------------------------------------------------------------------------
358 // wxWin event handlers
359 // ----------------------------------------------------------------------------
362 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
364 if ( Validate() && TransferDataFromWindow() )
370 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
373 TransferDataFromWindow();
375 // TODO probably need to disable the Apply button until things change again
378 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
380 EndModal(wxID_CANCEL
);
383 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
385 // We'll send a Cancel message by default, which may close the dialog.
386 // Check for looping if the Cancel event handler calls Close().
388 // Note that if a cancel button and handler aren't present in the dialog,
389 // nothing will happen when you close the dialog via the window manager, or
390 // via Close(). We wouldn't want to destroy the dialog by default, since
391 // the dialog may have been created on the stack. However, this does mean
392 // that calling dialog->Close() won't delete the dialog unless the handler
393 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
394 // destroy the dialog. The default OnCancel (above) simply ends a modal
395 // dialog, and hides a modeless dialog.
397 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
398 // lists here? don't dare to change it now, but should be done later!
399 static wxList closing
;
401 if ( closing
.Member(this) )
404 closing
.Append(this);
406 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
407 cancelEvent
.SetEventObject( this );
408 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
410 closing
.DeleteObject(this);
413 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
418 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
423 // ---------------------------------------------------------------------------
424 // dialog window proc
425 // ---------------------------------------------------------------------------
427 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
430 bool processed
= FALSE
;
435 // react to pressing the OK button in the title
437 if (LOWORD(wParam
) == IDOK
)
439 wxButton
*btn
= wxDynamicCast(FindWindow(wxID_CANCEL
), wxButton
);
440 if ( btn
&& btn
->IsEnabled() )
442 // if we do have a cancel button, do press it
443 btn
->MSWCommand(BN_CLICKED
, 0 /* unused */);
451 // if we can't close, tell the system that we processed the
452 // message - otherwise it would close us
453 processed
= !Close();
457 // the Windows dialogs unfortunately are not meant to be resizeable
458 // at all and their standard class doesn't include CS_[VH]REDRAW
459 // styles which means that the window is not refreshed properly
460 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
461 // help with it - so we have to refresh it manually which certainly
462 // creates flicker but at least doesn't show garbage on the screen
463 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
465 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
467 ::InvalidateRect(GetHwnd(), NULL
, FALSE
/* erase bg */);
471 #ifndef __WXMICROWIN__
473 // we want to override the busy cursor for modal dialogs:
474 // typically, wxBeginBusyCursor() is called and then a modal dialog
475 // is shown, but the modal dialog shouldn't have hourglass cursor
476 if ( IsModal() && wxIsBusy() )
478 // set our cursor for all windows (but see below)
479 wxCursor cursor
= m_cursor
;
481 cursor
= wxCURSOR_ARROW
;
483 ::SetCursor(GetHcursorOf(cursor
));
485 // in any case, stop here and don't let wxWindow process this
486 // message (it would set the busy cursor)
489 // but return FALSE to tell the child window (if the event
490 // comes from one of them and not from ourselves) that it can
491 // set its own cursor if it has one: thus, standard controls
492 // (e.g. text ctrl) still have correct cursors in a dialog
493 // invoked while wxIsBusy()
497 #endif // __WXMICROWIN__
501 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
508 // Define for each class of dialog and control
509 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
510 WXHWND
WXUNUSED(pWnd
),
511 WXUINT
WXUNUSED(nCtlColor
),
516 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
519 #endif // wxUSE_CTL3D