1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/dialog.h"
20 #include "wx/settings.h"
25 #include "wx/os2/private.h"
27 #include "wx/evtloop.h"
28 #include "wx/ptr_scpd.h"
30 #define wxDIALOG_DEFAULT_X 300
31 #define wxDIALOG_DEFAULT_Y 300
33 #define wxDIALOG_DEFAULT_WIDTH 500
34 #define wxDIALOG_DEFAULT_HEIGHT 500
36 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
38 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
39 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
40 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
41 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
42 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
43 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
44 EVT_CLOSE(wxDialog::OnCloseWindow
)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // this is simply a container for any data we need to implement modality which
52 // allows us to avoid changing wxDialog each time the implementation changes
53 class wxDialogModalData
56 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
69 wxModalEventLoop m_evtLoop
;
72 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
);
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // wxDialog construction
80 // ----------------------------------------------------------------------------
84 m_pOldFocus
= (wxWindow
*)NULL
;
86 m_pWindowDisabler
= (wxWindowDisabler
*)NULL
;
88 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
89 } // end of wxDialog::Init
91 bool wxDialog::Create(
94 , const wxString
& rsTitle
98 , const wxString
& rsName
103 long lWidth
= rSize
.x
;
104 long lHeight
= rSize
.y
;
106 WXDWORD dwExtendedStyle
= 0L;
110 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
113 // Save focus before doing anything which can potentially change it
115 m_pOldFocus
= FindFocus();
118 // All dialogs should really have this style
120 lStyle
|= wxTAB_TRAVERSAL
;
122 if (!wxTopLevelWindow::Create( pParent
131 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
134 // Must defer setting the title until after dialog is created and sized
136 if (!rsTitle
.IsNull())
139 } // end of wxDialog::Create
141 void wxDialog::SetModal(
147 m_windowStyle
|= wxDIALOG_MODAL
;
148 wxModelessWindows
.DeleteObject(this);
152 m_windowStyle
&= ~wxDIALOG_MODAL
;
153 wxModelessWindows
.Append(this);
155 } // end of wxDialog::SetModal
157 wxDialog::~wxDialog()
159 m_isBeingDeleted
= TRUE
;
161 } // end of wxDialog::~wxDialog
164 // By default, pressing escape cancels the dialog
166 void wxDialog::OnCharHook(
172 if (rEvent
.m_keyCode
== WXK_ESCAPE
)
175 // Behaviour changed in 2.0: we'll send a Cancel message
176 // to the dialog instead of Close.
178 wxCommandEvent
vCancelEvent( wxEVT_COMMAND_BUTTON_CLICKED
182 vCancelEvent
.SetEventObject( this );
183 GetEventHandler()->ProcessEvent(vCancelEvent
);
186 // Ensure that there is another message for this window so the
187 // ShowModal loop will exit and won't get stuck in GetMessage().
189 ::WinPostMsg(GetHwnd(), WM_NULL
, 0, 0);
193 // We didn't process this event.
197 // ----------------------------------------------------------------------------
198 // showing the dialogs
199 // ----------------------------------------------------------------------------
201 bool wxDialog::IsModal() const
203 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
204 } // end of wxDialog::IsModal
206 bool wxDialog::IsModalShowing() const
208 return m_modalData
!= NULL
; // const_cast
209 } // end of wxDialog::IsModalShowing
211 void wxDialog::DoShowModal()
213 wxWindow
* pParent
= GetParent();
214 wxWindow
* pOldFocus
= m_pOldFocus
;
215 HWND hWndOldFocus
= 0;
217 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
218 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
221 hWndOldFocus
= (HWND
)pOldFocus
->GetHWND();
224 // Remember where the focus was
230 hWndOldFocus
= GetHwndOf(pParent
);
234 // Disable all other app windows
236 wxASSERT_MSG(!m_pWindowDisabler
, _T("disabling windows twice?"));
239 // Before entering the modal loop, reset the "is in OnIdle()" flag (see
240 // comment in app.cpp)
242 extern bool gbInOnIdle
;
243 bool bWasInOnIdle
= gbInOnIdle
;
247 // enter the modal loop
249 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
250 new wxDialogModalData(this));
251 modalData
->RunLoop();
253 gbInOnIdle
= bWasInOnIdle
;
257 // Note that this code MUST NOT access the dialog object's data
258 // in case the object has been deleted (which will be the case
259 // for a modal dialog that has been destroyed before calling EndModal).
261 if (pOldFocus
&& (pOldFocus
!= this) && ::WinIsWindow(vHabmain
, hWndOldFocus
))
264 // This is likely to prove that the object still exists
266 if (wxFindWinFromHandle((WXHWND
) hWndOldFocus
) == pOldFocus
)
267 pOldFocus
->SetFocus();
269 } // end of wxDialog::DoShowModal
278 // If we had disabled other app windows, reenable them back now because
279 // if they stay disabled Windows will activate another window (one
280 // which is enabled, anyhow) and we will lose activation. We really don't
281 // do this in OS/2 since PM does this for us.
283 if (m_pWindowDisabler
)
285 delete m_pWindowDisabler
;
286 m_pWindowDisabler
= NULL
;
289 m_modalData
->ExitLoop();
293 // ShowModal() may be called for already shown dialog
295 if (!wxDialogBase::Show(bShow
) && !(bShow
&& IsModal()))
305 // dialogs don't get WM_SIZE message after creation unlike most (all?)
306 // other windows and so could start their life non laid out correctly
307 // if we didn't call Layout() from here
309 // NB: normally we should call it just the first time but doing it
310 // every time is simpler than keeping a flag
314 // Usually will result in TransferDataToWindow() being called
319 if (GetTitle().c_str())
320 ::WinSetWindowText((HWND
)GetHwnd(), GetTitle().c_str());
326 // Modal dialog needs a parent window, so try to find one
330 wxWindow
* pParent
= wxTheApp
->GetTopWindow();
332 if ( pParent
&& pParent
!= this && pParent
->IsShown() )
345 } // end of wxDialog::Show
348 // Replacement for Show(TRUE) for modal dialogs - returns return code
350 int wxDialog::ShowModal()
357 return GetReturnCode();
358 } // end of wxDialog::ShowModal
360 void wxDialog::EndModal(
364 SetReturnCode(nRetCode
);
366 } // end of wxDialog::EndModal
368 // ----------------------------------------------------------------------------
369 // wxWin event handlers
370 // ----------------------------------------------------------------------------
372 void wxDialog::OnApply(
373 wxCommandEvent
& rEvent
377 TransferDataFromWindow();
378 } // end of wxDialog::OnApply
382 wxCommandEvent
& rEvent
385 if ( Validate() && TransferDataFromWindow() )
389 } // end of wxDialog::OnOK
391 void wxDialog::OnCancel(
392 wxCommandEvent
& rEvent
395 EndModal(wxID_CANCEL
);
396 } // end of wxDialog::OnCancel
398 void wxDialog::OnCloseWindow(
403 // We'll send a Cancel message by default,
404 // 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
410 // We wouldn't want to destroy the dialog by default, since the dialog may have been
411 // created on the stack.
412 // However, this does mean that calling dialog->Close() won't delete the dialog
413 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
414 // sure to destroy the dialog.
415 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
419 // Ugh??? This is not good but until I figure out a global list it'll have to do
421 static wxList closing
;
423 if ( closing
.Member(this) )
426 closing
.Append(this);
428 wxCommandEvent
vCancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
430 vCancelEvent
.SetEventObject( this );
431 GetEventHandler()->ProcessEvent(vCancelEvent
); // This may close the dialog
433 closing
.DeleteObject(this);
434 } // end of wxDialog::OnCloseWindow
436 void wxDialog::OnSysColourChanged(
437 wxSysColourChangedEvent
& rEvent
440 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
442 } // end of wxDialog::OnSysColourChanged
444 MRESULT
wxDialog::OS2WindowProc(
451 bool bProcessed
= FALSE
;
457 // If we can't close, tell the system that we processed the
458 // message - otherwise it would close us
460 bProcessed
= !Close();
465 rc
= wxWindow::OS2WindowProc( uMessage
470 } // end of wxDialog::OS2WindowProc