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 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
55 #include "wx/toolbar.h"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 #if wxUSE_EXTENDED_RTTI
63 WX_DEFINE_FLAGS( wxDialogStyle
)
65 wxBEGIN_FLAGS( wxDialogStyle
)
66 // new style border flags, we put them first to
67 // use them for streaming out
68 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
69 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
70 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
71 wxFLAGS_MEMBER(wxBORDER_RAISED
)
72 wxFLAGS_MEMBER(wxBORDER_STATIC
)
73 wxFLAGS_MEMBER(wxBORDER_NONE
)
75 // old style border flags
76 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
77 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
78 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
79 wxFLAGS_MEMBER(wxRAISED_BORDER
)
80 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
81 wxFLAGS_MEMBER(wxNO_BORDER
)
83 // standard window styles
84 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
85 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
88 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
89 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
90 wxFLAGS_MEMBER(wxCAPTION
)
91 wxFLAGS_MEMBER(wxTHICK_FRAME
)
92 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
93 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
94 wxFLAGS_MEMBER(wxRESIZE_BOX
)
95 wxFLAGS_MEMBER(wxCLOSE_BOX
)
96 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
97 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
98 wxEND_FLAGS( wxDialogStyle
)
100 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
102 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
103 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
104 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
105 wxEND_PROPERTIES_TABLE()
107 wxBEGIN_HANDLERS_TABLE(wxDialog
)
108 wxEND_HANDLERS_TABLE()
110 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
113 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
116 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
117 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
118 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
119 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
121 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
123 EVT_CLOSE(wxDialog::OnCloseWindow
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 // this is simply a container for any data we need to implement modality which
131 // allows us to avoid changing wxDialog each time the implementation changes
132 class wxDialogModalData
135 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
148 wxModalEventLoop m_evtLoop
;
151 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
);
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
158 // wxDialog construction
159 // ----------------------------------------------------------------------------
161 void wxDialog::Init()
163 m_oldFocus
= (wxWindow
*)NULL
;
166 m_endModalCalled
= false;
167 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
168 m_dialogToolBar
= NULL
;
172 bool wxDialog::Create(wxWindow
*parent
,
174 const wxString
& title
,
178 const wxString
& name
)
180 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
182 // save focus before doing anything which can potentially change it
183 m_oldFocus
= FindFocus();
185 // All dialogs should really have this style
186 style
|= wxTAB_TRAVERSAL
;
188 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
192 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
194 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
195 SetLeftMenu(wxID_OK
, _("OK"));
197 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
205 wxDialog::wxDialog(wxWindow
*parent
,
206 const wxString
& title
,
207 bool WXUNUSED(modal
),
213 const wxString
& name
)
217 Create(parent
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
), style
, name
);
220 void wxDialog::SetModal(bool WXUNUSED(flag
))
222 // nothing to do, obsolete method
225 wxDialog::~wxDialog()
227 m_isBeingDeleted
= true;
229 // this will also reenable all the other windows for a modal dialog
233 // ----------------------------------------------------------------------------
234 // showing the dialogs
235 // ----------------------------------------------------------------------------
237 bool wxDialog::IsModalShowing() const
242 wxWindow
*wxDialog::FindSuitableParent() const
244 // first try to use the currently active window
245 HWND hwndFg
= ::GetForegroundWindow();
246 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
250 // next try the main app window
251 parent
= wxTheApp
->GetTopWindow();
254 // finally, check if the parent we found is really suitable
255 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
257 // don't use this one
264 bool wxDialog::Show(bool show
)
266 if ( show
== IsShown() )
269 if ( !show
&& m_modalData
)
271 // we need to do this before calling wxDialogBase version because if we
272 // had disabled other app windows, they must be reenabled right now as
273 // if they stay disabled Windows will activate another window (one
274 // which is enabled, anyhow) when we're hidden in the base class Show()
275 // and we will lose activation
276 m_modalData
->ExitLoop();
281 // this usually will result in TransferDataToWindow() being called
282 // which will change the controls values so do it before showing as
283 // otherwise we could have some flicker
287 wxDialogBase::Show(show
);
291 // dialogs don't get WM_SIZE message after creation unlike most (all?)
292 // other windows and so could start their life non laid out correctly
293 // if we didn't call Layout() from here
295 // NB: normally we should call it just the first time but doing it
296 // every time is simpler than keeping a flag
303 void wxDialog::Raise()
305 ::SetForegroundWindow(GetHwnd());
308 // show dialog modally
309 int wxDialog::ShowModal()
311 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
313 m_endModalCalled
= false;
317 // EndModal may have been called from InitDialog handler (called from
318 // inside Show()), which would cause an infinite loop if we didn't take it
320 if ( !m_endModalCalled
)
322 // modal dialog needs a parent window, so try to find one
323 wxWindow
*parent
= GetParent();
326 parent
= FindSuitableParent();
329 // remember where the focus was
330 wxWindow
*oldFocus
= m_oldFocus
;
333 // VZ: do we really want to do this?
337 // We have to remember the HWND because we need to check
338 // the HWND still exists (oldFocus can be garbage when the dialog
339 // exits, if it has been destroyed)
340 HWND hwndOldFocus
= oldFocus
? GetHwndOf(oldFocus
) : NULL
;
343 // enter and run the modal loop
345 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
346 new wxDialogModalData(this));
347 modalData
->RunLoop();
352 // Note that this code MUST NOT access the dialog object's data
353 // in case the object has been deleted (which will be the case
354 // for a modal dialog that has been destroyed before calling EndModal).
355 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
357 // This is likely to prove that the object still exists
358 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
359 oldFocus
->SetFocus();
363 return GetReturnCode();
366 void wxDialog::EndModal(int retCode
)
368 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
370 m_endModalCalled
= true;
371 SetReturnCode(retCode
);
376 void wxDialog::EndDialog(int rc
)
384 // ----------------------------------------------------------------------------
385 // wxWin event handlers
386 // ----------------------------------------------------------------------------
389 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
391 if ( Validate() && TransferDataFromWindow() )
397 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
400 TransferDataFromWindow();
402 // TODO probably need to disable the Apply button until things change again
405 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
407 EndDialog(wxID_CANCEL
);
410 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
412 // We'll send a Cancel message by default, which may close the dialog.
413 // Check for looping if the Cancel event handler calls Close().
415 // Note that if a cancel button and handler aren't present in the dialog,
416 // nothing will happen when you close the dialog via the window manager, or
417 // via Close(). We wouldn't want to destroy the dialog by default, since
418 // the dialog may have been created on the stack. However, this does mean
419 // that calling dialog->Close() won't delete the dialog unless the handler
420 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
421 // destroy the dialog. The default OnCancel (above) simply ends a modal
422 // dialog, and hides a modeless dialog.
424 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
425 // lists here? don't dare to change it now, but should be done later!
426 static wxList closing
;
428 if ( closing
.Member(this) )
431 closing
.Append(this);
433 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
434 cancelEvent
.SetEventObject( this );
435 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
437 closing
.DeleteObject(this);
440 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
442 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
447 // Responds to the OK button in a PocketPC titlebar. This
448 // can be overridden, or you can change the id used for
449 // sending the event, by calling SetAffirmativeId.
450 bool wxDialog::DoOK()
452 wxButton
*btn
= wxDynamicCast(FindWindow(GetAffirmativeId()), wxButton
);
454 if ( btn
&& btn
->IsEnabled() )
456 // If we have this button, press it
457 btn
->MSWCommand(BN_CLICKED
, 0 /* unused */);
462 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
463 event
.SetEventObject(this);
465 return GetEventHandler()->ProcessEvent(event
);
470 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
471 // create main toolbar by calling OnCreateToolBar()
472 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
474 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
476 return m_dialogToolBar
;
479 // return a new toolbar
480 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
482 const wxString
& name
)
484 return new wxToolMenuBar(this, winid
,
485 wxDefaultPosition
, wxDefaultSize
,
490 // ---------------------------------------------------------------------------
491 // dialog window proc
492 // ---------------------------------------------------------------------------
494 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
497 bool processed
= false;
502 // react to pressing the OK button in the title
505 switch ( LOWORD(wParam
) )
511 processed
= !Close();
513 #ifdef __SMARTPHONE__
516 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
518 #endif // __SMARTPHONE__
524 // if we can't close, tell the system that we processed the
525 // message - otherwise it would close us
526 processed
= !Close();
530 // the Windows dialogs unfortunately are not meant to be resizeable
531 // at all and their standard class doesn't include CS_[VH]REDRAW
532 // styles which means that the window is not refreshed properly
533 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
534 // help with it - so we have to refresh it manually which certainly
535 // creates flicker but at least doesn't show garbage on the screen
536 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
538 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
540 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
544 #ifndef __WXMICROWIN__
546 // we want to override the busy cursor for modal dialogs:
547 // typically, wxBeginBusyCursor() is called and then a modal dialog
548 // is shown, but the modal dialog shouldn't have hourglass cursor
549 if ( IsModal() && wxIsBusy() )
551 // set our cursor for all windows (but see below)
552 wxCursor cursor
= m_cursor
;
554 cursor
= wxCURSOR_ARROW
;
556 ::SetCursor(GetHcursorOf(cursor
));
558 // in any case, stop here and don't let wxWindow process this
559 // message (it would set the busy cursor)
562 // but return false to tell the child window (if the event
563 // comes from one of them and not from ourselves) that it can
564 // set its own cursor if it has one: thus, standard controls
565 // (e.g. text ctrl) still have correct cursors in a dialog
566 // invoked while wxIsBusy()
570 #endif // __WXMICROWIN__
574 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);