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 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
51 #include "wx/msw/wince/resources.h"
52 #endif // __SMARTPHONE__ && __WXWINCE__
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 #if wxUSE_EXTENDED_RTTI
59 WX_DEFINE_FLAGS( wxDialogStyle
)
61 wxBEGIN_FLAGS( wxDialogStyle
)
62 // new style border flags, we put them first to
63 // use them for streaming out
64 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
65 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
66 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
67 wxFLAGS_MEMBER(wxBORDER_RAISED
)
68 wxFLAGS_MEMBER(wxBORDER_STATIC
)
69 wxFLAGS_MEMBER(wxBORDER_NONE
)
71 // old style border flags
72 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
73 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
74 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
75 wxFLAGS_MEMBER(wxRAISED_BORDER
)
76 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
77 wxFLAGS_MEMBER(wxNO_BORDER
)
79 // standard window styles
80 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
81 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
84 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
85 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
86 wxFLAGS_MEMBER(wxCAPTION
)
87 wxFLAGS_MEMBER(wxTHICK_FRAME
)
88 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
89 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
90 wxFLAGS_MEMBER(wxRESIZE_BOX
)
91 wxFLAGS_MEMBER(wxCLOSE_BOX
)
92 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
93 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
94 wxEND_FLAGS( wxDialogStyle
)
96 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
98 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
99 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
101 wxEND_PROPERTIES_TABLE()
103 wxBEGIN_HANDLERS_TABLE(wxDialog
)
104 wxEND_HANDLERS_TABLE()
106 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
109 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
112 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
113 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
114 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
115 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
117 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
119 EVT_CLOSE(wxDialog::OnCloseWindow
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // this is simply a container for any data we need to implement modality which
127 // allows us to avoid changing wxDialog each time the implementation changes
128 class wxDialogModalData
131 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
144 wxModalEventLoop m_evtLoop
;
147 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
);
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
154 // wxDialog construction
155 // ----------------------------------------------------------------------------
157 void wxDialog::Init()
159 m_oldFocus
= (wxWindow
*)NULL
;
162 m_endModalCalled
= false;
165 bool wxDialog::Create(wxWindow
*parent
,
167 const wxString
& title
,
171 const wxString
& name
)
173 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
175 // save focus before doing anything which can potentially change it
176 m_oldFocus
= FindFocus();
178 // All dialogs should really have this style
179 style
|= wxTAB_TRAVERSAL
;
181 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
185 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
187 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
189 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
190 SetLeftMenu(wxID_OK
, _("OK"));
197 wxDialog::wxDialog(wxWindow
*parent
,
198 const wxString
& title
,
199 bool WXUNUSED(modal
),
205 const wxString
& name
)
209 Create(parent
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
), style
, name
);
212 void wxDialog::SetModal(bool WXUNUSED(flag
))
214 // nothing to do, obsolete method
217 wxDialog::~wxDialog()
219 m_isBeingDeleted
= true;
221 // this will also reenable all the other windows for a modal dialog
225 // ----------------------------------------------------------------------------
226 // showing the dialogs
227 // ----------------------------------------------------------------------------
229 bool wxDialog::IsModalShowing() const
234 wxWindow
*wxDialog::FindSuitableParent() const
236 // first try to use the currently active window
237 HWND hwndFg
= ::GetForegroundWindow();
238 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
242 // next try the main app window
243 parent
= wxTheApp
->GetTopWindow();
246 // finally, check if the parent we found is really suitable
247 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
249 // don't use this one
256 bool wxDialog::Show(bool show
)
258 if ( show
== IsShown() )
261 if ( !show
&& m_modalData
)
263 // we need to do this before calling wxDialogBase version because if we
264 // had disabled other app windows, they must be reenabled right now as
265 // if they stay disabled Windows will activate another window (one
266 // which is enabled, anyhow) when we're hidden in the base class Show()
267 // and we will lose activation
268 m_modalData
->ExitLoop();
273 // this usually will result in TransferDataToWindow() being called
274 // which will change the controls values so do it before showing as
275 // otherwise we could have some flicker
279 wxDialogBase::Show(show
);
283 // dialogs don't get WM_SIZE message after creation unlike most (all?)
284 // other windows and so could start their life non laid out correctly
285 // if we didn't call Layout() from here
287 // NB: normally we should call it just the first time but doing it
288 // every time is simpler than keeping a flag
295 void wxDialog::Raise()
297 ::SetForegroundWindow(GetHwnd());
300 // show dialog modally
301 int wxDialog::ShowModal()
303 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
305 m_endModalCalled
= false;
309 // EndModal may have been called from InitDialog handler (called from
310 // inside Show()), which would cause an infinite loop if we didn't take it
312 if ( !m_endModalCalled
)
314 // modal dialog needs a parent window, so try to find one
315 wxWindow
*parent
= GetParent();
318 parent
= FindSuitableParent();
321 // remember where the focus was
322 wxWindow
*oldFocus
= m_oldFocus
;
325 // VZ: do we really want to do this?
329 // We have to remember the HWND because we need to check
330 // the HWND still exists (oldFocus can be garbage when the dialog
331 // exits, if it has been destroyed)
332 HWND hwndOldFocus
= oldFocus
? GetHwndOf(oldFocus
) : NULL
;
335 // enter and run the modal loop
337 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
338 new wxDialogModalData(this));
339 modalData
->RunLoop();
344 // Note that this code MUST NOT access the dialog object's data
345 // in case the object has been deleted (which will be the case
346 // for a modal dialog that has been destroyed before calling EndModal).
347 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
349 // This is likely to prove that the object still exists
350 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
351 oldFocus
->SetFocus();
355 return GetReturnCode();
358 void wxDialog::EndModal(int retCode
)
360 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
362 m_endModalCalled
= true;
363 SetReturnCode(retCode
);
368 void wxDialog::EndDialog(int rc
)
376 // ----------------------------------------------------------------------------
377 // wxWin event handlers
378 // ----------------------------------------------------------------------------
381 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
383 if ( Validate() && TransferDataFromWindow() )
389 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
392 TransferDataFromWindow();
394 // TODO probably need to disable the Apply button until things change again
397 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
399 EndDialog(wxID_CANCEL
);
402 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
404 // We'll send a Cancel message by default, which may close the dialog.
405 // Check for looping if the Cancel event handler calls Close().
407 // Note that if a cancel button and handler aren't present in the dialog,
408 // nothing will happen when you close the dialog via the window manager, or
409 // via Close(). We wouldn't want to destroy the dialog by default, since
410 // the dialog may have been created on the stack. However, this does mean
411 // that calling dialog->Close() won't delete the dialog unless the handler
412 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
413 // destroy the dialog. The default OnCancel (above) simply ends a modal
414 // dialog, and hides a modeless dialog.
416 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
417 // lists here? don't dare to change it now, but should be done later!
418 static wxList closing
;
420 if ( closing
.Member(this) )
423 closing
.Append(this);
425 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
426 cancelEvent
.SetEventObject( this );
427 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
429 closing
.DeleteObject(this);
432 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
437 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
442 // ---------------------------------------------------------------------------
443 // dialog window proc
444 // ---------------------------------------------------------------------------
446 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
449 bool processed
= false;
454 // react to pressing the OK button in the title
457 switch ( LOWORD(wParam
) )
459 #ifndef __SMARTPHONE__
461 wxButton
*btn
= wxDynamicCast(FindWindow(wxID_CANCEL
), wxButton
);
462 if ( btn
&& btn
->IsEnabled() )
464 // if we do have a cancel button, do press it
465 btn
->MSWCommand(BN_CLICKED
, 0 /* unused */);
469 #else // ifdef __SMARTPHONE__
472 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
474 #endif // __SMARTPHONE__
480 // if we can't close, tell the system that we processed the
481 // message - otherwise it would close us
482 processed
= !Close();
486 // the Windows dialogs unfortunately are not meant to be resizeable
487 // at all and their standard class doesn't include CS_[VH]REDRAW
488 // styles which means that the window is not refreshed properly
489 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
490 // help with it - so we have to refresh it manually which certainly
491 // creates flicker but at least doesn't show garbage on the screen
492 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
494 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
496 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
500 #ifndef __WXMICROWIN__
502 // we want to override the busy cursor for modal dialogs:
503 // typically, wxBeginBusyCursor() is called and then a modal dialog
504 // is shown, but the modal dialog shouldn't have hourglass cursor
505 if ( IsModal() && wxIsBusy() )
507 // set our cursor for all windows (but see below)
508 wxCursor cursor
= m_cursor
;
510 cursor
= wxCURSOR_ARROW
;
512 ::SetCursor(GetHcursorOf(cursor
));
514 // in any case, stop here and don't let wxWindow process this
515 // message (it would set the busy cursor)
518 // but return false to tell the child window (if the event
519 // comes from one of them and not from ourselves) that it can
520 // set its own cursor if it has one: thus, standard controls
521 // (e.g. text ctrl) still have correct cursors in a dialog
522 // invoked while wxIsBusy()
526 #endif // __WXMICROWIN__
530 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
537 // Define for each class of dialog and control
538 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
539 WXHWND
WXUNUSED(pWnd
),
540 WXUINT
WXUNUSED(nCtlColor
),
545 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
548 #endif // wxUSE_CTL3D