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"
28 #define wxDIALOG_DEFAULT_X 300
29 #define wxDIALOG_DEFAULT_Y 300
31 #define wxDIALOG_DEFAULT_WIDTH 500
32 #define wxDIALOG_DEFAULT_HEIGHT 500
34 wxWindowList wxModalDialogs
;
36 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
38 BEGIN_EVENT_TABLE(wxDialog
, wxTopLevelWindow
)
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
)
49 m_pOldFocus
= (wxWindow
*)NULL
;
51 m_pWindowDisabler
= (wxWindowDisabler
*)NULL
;
52 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
53 } // end of wxDialog::Init
55 bool wxDialog::Create(
58 , const wxString
& rsTitle
62 , const wxString
& rsName
67 long lWidth
= rSize
.x
;
68 long lHeight
= rSize
.y
;
70 WXDWORD dwExtendedStyle
= 0L;
74 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
77 // Save focus before doing anything which can potentially change it
79 m_pOldFocus
= FindFocus();
82 // All dialogs should really have this style
84 lStyle
|= wxTAB_TRAVERSAL
;
86 if (!wxTopLevelWindow::Create( pParent
95 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
97 } // end of wxDialog::Create
99 void wxDialog::SetModal(
105 m_windowStyle
|= wxDIALOG_MODAL
;
106 wxModelessWindows
.DeleteObject(this);
110 m_windowStyle
&= ~wxDIALOG_MODAL
;
111 wxModelessWindows
.Append(this);
113 } // end of wxDialog::SetModal
115 wxDialog::~wxDialog()
117 m_isBeingDeleted
= TRUE
;
119 } // end of wxDialog::~wxDialog
122 // By default, pressing escape cancels the dialog
124 void wxDialog::OnCharHook(
130 if (rEvent
.m_keyCode
== WXK_ESCAPE
)
133 // Behaviour changed in 2.0: we'll send a Cancel message
134 // to the dialog instead of Close.
136 wxCommandEvent
vCancelEvent( wxEVT_COMMAND_BUTTON_CLICKED
140 vCancelEvent
.SetEventObject( this );
141 GetEventHandler()->ProcessEvent(vCancelEvent
);
144 // Ensure that there is another message for this window so the
145 // ShowModal loop will exit and won't get stuck in GetMessage().
147 ::WinPostMsg(GetHwnd(), WM_NULL
, 0, 0);
151 // We didn't process this event.
155 // ----------------------------------------------------------------------------
156 // showing the dialogs
157 // ----------------------------------------------------------------------------
159 bool wxDialog::IsModal() const
161 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
162 } // end of wxDialog::IsModal
164 bool wxDialog::IsModalShowing() const
166 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
167 } // end of wxDialog::IsModalShowing
169 void wxDialog::DoShowModal()
171 wxWindow
* pParent
= GetParent();
172 wxWindow
* pOldFocus
= m_pOldFocus
;
173 HWND hWndOldFocus
= 0;
175 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
176 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
178 wxModalDialogs
.Append(this);
180 hWndOldFocus
= (HWND
)pOldFocus
->GetHWND();
183 // Remember where the focus was
189 hWndOldFocus
= GetHwndOf(pParent
);
193 // Disable all other app windows
195 wxASSERT_MSG(!m_pWindowDisabler
, _T("disabling windows twice?"));
197 m_pWindowDisabler
= new wxWindowDisabler(this);
200 // Enter the modal loop
202 while ( IsModalShowing() )
205 wxMutexGuiLeaveOrEnter();
206 #endif // wxUSE_THREADS
208 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
211 // a message came or no more idle processing to do
212 wxTheApp
->DoMessage();
217 // Note that this code MUST NOT access the dialog object's data
218 // in case the object has been deleted (which will be the case
219 // for a modal dialog that has been destroyed before calling EndModal).
221 if (pOldFocus
&& (pOldFocus
!= this) && ::WinIsWindow(vHabmain
, hWndOldFocus
))
224 // This is likely to prove that the object still exists
226 if (wxFindWinFromHandle((WXHWND
) hWndOldFocus
) == pOldFocus
)
227 pOldFocus
->SetFocus();
229 } // end of wxDialog::DoShowModal
238 // If we had disabled other app windows, reenable them back now because
239 // if they stay disabled Windows will activate another window (one
240 // which is enabled, anyhow) and we will lose activation
242 if (m_pWindowDisabler
)
244 delete m_pWindowDisabler
;
245 m_pWindowDisabler
= NULL
;
250 // ShowModal() may be called for already shown dialog
252 if (!wxDialogBase::Show(bShow
) && !(bShow
&& IsModal()))
263 // Usually will result in TransferDataToWindow() being called
273 // Modal dialog needs a parent window, so try to find one
277 wxWindow
* pParent
= wxTheApp
->GetTopWindow();
279 if ( pParent
&& pParent
!= this && pParent
->IsShown() )
290 else // end of modal dialog
293 // This will cause IsModalShowing() return FALSE and our local
294 // message loop will terminate
296 wxModalDialogs
.DeleteObject(this);
300 } // end of wxDialog::Show
303 // Replacement for Show(TRUE) for modal dialogs - returns return code
305 int wxDialog::ShowModal()
312 return GetReturnCode();
313 } // end of wxDialog::ShowModal
315 void wxDialog::EndModal(
319 SetReturnCode(nRetCode
);
321 } // end of wxDialog::EndModal
323 // ----------------------------------------------------------------------------
324 // wxWin event handlers
325 // ----------------------------------------------------------------------------
327 void wxDialog::OnApply(
328 wxCommandEvent
& rEvent
332 TransferDataFromWindow();
333 } // end of wxDialog::OnApply
337 wxCommandEvent
& rEvent
340 if ( Validate() && TransferDataFromWindow() )
344 } // end of wxDialog::OnOK
346 void wxDialog::OnCancel(
347 wxCommandEvent
& rEvent
350 EndModal(wxID_CANCEL
);
351 } // end of wxDialog::OnCancel
353 void wxDialog::OnCloseWindow(
358 // We'll send a Cancel message by default,
359 // which may close the dialog.
360 // Check for looping if the Cancel event handler calls Close().
362 // Note that if a cancel button and handler aren't present in the dialog,
363 // nothing will happen when you close the dialog via the window manager, or
365 // We wouldn't want to destroy the dialog by default, since the dialog may have been
366 // created on the stack.
367 // However, this does mean that calling dialog->Close() won't delete the dialog
368 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
369 // sure to destroy the dialog.
370 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
374 // Ugh??? This is not good but until I figure out a global list it'll have to do
376 static wxList closing
;
378 if ( closing
.Member(this) )
381 closing
.Append(this);
383 wxCommandEvent
vCancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
385 vCancelEvent
.SetEventObject( this );
386 GetEventHandler()->ProcessEvent(vCancelEvent
); // This may close the dialog
388 closing
.DeleteObject(this);
389 } // end of wxDialog::OnCloseWindow
391 void wxDialog::OnSysColourChanged(
392 wxSysColourChangedEvent
& rEvent
395 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
397 } // end of wxDialog::OnSysColourChanged
399 MRESULT
wxDialog::OS2WindowProc(
406 bool bProcessed
= FALSE
;
412 // If we can't close, tell the system that we processed the
413 // message - otherwise it would close us
415 bProcessed
= !Close();
420 rc
= wxWindow::OS2WindowProc( uMessage
425 } // end of wxDialog::OS2WindowProc