1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dialog.h"
16 #include "wx/wxprec.h"
18 #include "wx/dialog.h"
22 #include "wx/settings.h"
24 #include "wx/mac/uma.h"
26 // Lists to keep track of windows, so we can disable/enable them
28 wxList wxModalDialogs
;
29 //wxList wxModelessWindows; // Frames and modeless dialogs
30 extern wxList wxPendingDelete
;
32 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
34 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
35 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
36 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
37 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
39 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
41 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
43 EVT_CLOSE(wxDialog::OnCloseWindow
)
48 m_isModalStyle
= false;
51 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
52 const wxString
& title
,
58 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
60 // All dialogs should really have this style
61 style
|= wxTAB_TRAVERSAL
;
63 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
& ~(wxYES
|wxOK
|wxNO
|wxCANCEL
) , name
) )
69 void wxDialog::SetModal(bool flag
)
73 m_isModalStyle
= true;
75 wxModelessWindows
.DeleteObject(this);
77 SetWindowModality( (WindowRef
) MacGetWindowRef() , kWindowModalityAppModal
, NULL
) ;
82 m_isModalStyle
= false;
84 wxModelessWindows
.Append(this);
90 m_isBeingDeleted
= TRUE
;
94 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
95 void wxDialog::OnCharHook(wxKeyEvent
& event
)
97 if (( event
.m_keyCode
== WXK_ESCAPE
||
98 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
99 && FindWindow(wxID_CANCEL
) )
101 // Behaviour changed in 2.0: we'll send a Cancel message
102 // to the dialog instead of Close.
103 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
104 cancelEvent
.SetEventObject( this );
105 GetEventHandler()->ProcessEvent(cancelEvent
);
109 // We didn't process this event.
113 bool wxDialog::IsModal() const
115 return m_isModalStyle
;
119 bool wxDialog::IsModalShowing() const
121 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
124 bool wxDialog::Show(bool show
)
126 if ( !wxDialogBase::Show(show
) )
134 // usually will result in TransferDataToWindow() being called
144 else // end of modal dialog
146 // this will cause IsModalShowing() return FALSE and our local
147 // message loop will terminate
148 wxModalDialogs
.DeleteObject(this);
156 extern bool s_macIsInModalLoop
;
159 void wxDialog::DoShowModal()
161 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
163 wxModalDialogs
.Append(this);
168 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
170 // TODO : test whether parent gets disabled
171 bool formerModal
= s_macIsInModalLoop
;
172 s_macIsInModalLoop
= true ;
174 while ( IsModalShowing() )
176 wxTheApp
->MacDoOneEvent() ;
177 // calls process idle itself
181 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
183 // TODO probably reenable the parent window if any
184 s_macIsInModalLoop
= formerModal
;
189 // Replacement for Show(TRUE) for modal dialogs - returns return code
190 int wxDialog::ShowModal()
198 return GetReturnCode();
201 // NB: this function (surprizingly) may be called for both modal and modeless
202 // dialogs and should work for both of them
203 void wxDialog::EndModal(int retCode
)
205 SetReturnCode(retCode
);
210 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
212 if ( Validate() && TransferDataFromWindow() )
218 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
221 TransferDataFromWindow();
222 // TODO probably need to disable the Apply button until things change again
225 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
227 EndModal(wxID_CANCEL
);
230 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
232 // We'll send a Cancel message by default,
233 // which may close the dialog.
234 // Check for looping if the Cancel event handler calls Close().
236 // Note that if a cancel button and handler aren't present in the dialog,
237 // nothing will happen when you close the dialog via the window manager, or
239 // We wouldn't want to destroy the dialog by default, since the dialog may have been
240 // created on the stack.
241 // However, this does mean that calling dialog->Close() won't delete the dialog
242 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
243 // sure to destroy the dialog.
244 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
246 static wxList closing
;
248 if ( closing
.Member(this) )
251 closing
.Append(this);
253 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
254 cancelEvent
.SetEventObject( this );
255 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
257 closing
.DeleteObject(this);
260 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
262 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));