1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/dialog.h"
18 #include "wx/settings.h"
20 #include "wx/mac/uma.h"
23 // Lists to keep track of windows, so we can disable/enable them
25 wxList wxModalDialogs
;
27 extern wxList wxPendingDelete
;
29 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
31 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
32 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
33 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
34 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
36 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
38 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
40 EVT_CLOSE(wxDialog::OnCloseWindow
)
46 m_isModalStyle
= false;
49 bool wxDialog::Create( wxWindow
*parent
,
51 const wxString
& title
,
55 const wxString
& name
)
57 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
59 // All dialogs should really have this style...
60 style
|= wxTAB_TRAVERSAL
;
62 // ...but not these styles
63 style
&= ~(wxYES
| wxOK
| wxNO
); // | wxCANCEL
65 if ( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
, name
) )
68 HIViewRef growBoxRef
= 0 ;
69 OSStatus err
= HIViewFindByID( HIViewGetRoot( (WindowRef
)m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
70 if ( err
== noErr
&& growBoxRef
!= 0 )
71 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
76 void wxDialog::SetModal( bool flag
)
80 m_isModalStyle
= true;
82 wxModelessWindows
.DeleteObject( this );
85 SetWindowModality( (WindowRef
)MacGetWindowRef(), kWindowModalityAppModal
, NULL
) ;
90 m_isModalStyle
= false;
92 wxModelessWindows
.Append( this );
98 m_isBeingDeleted
= true;
102 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
103 void wxDialog::OnCharHook(wxKeyEvent
& event
)
105 if (( event
.m_keyCode
== WXK_ESCAPE
||
106 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
107 && FindWindow(wxID_CANCEL
) )
109 // Behaviour changed in 2.0: we'll send a Cancel message
110 // to the dialog instead of Close.
111 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
112 cancelEvent
.SetEventObject( this );
113 GetEventHandler()->ProcessEvent(cancelEvent
);
118 // We didn't process this event.
122 bool wxDialog::IsModal() const
124 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
125 // return m_isModalStyle;
129 bool wxDialog::IsModalShowing() const
131 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
134 bool wxDialog::Show(bool show
)
136 if ( !wxDialogBase::Show(show
) )
141 // usually will result in TransferDataToWindow() being called
144 if ( m_isModalStyle
)
150 else // end of modal dialog
152 // this will cause IsModalShowing() return false and our local
153 // message loop will terminate
154 wxModalDialogs
.DeleteObject(this);
162 extern bool s_macIsInModalLoop
;
165 void wxDialog::DoShowModal()
167 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
169 wxModalDialogs
.Append(this);
174 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
176 // TODO : test whether parent gets disabled
177 bool formerModal
= s_macIsInModalLoop
;
178 s_macIsInModalLoop
= true ;
181 while ( IsModalShowing() )
183 wxTheApp
->MacDoOneEvent() ;
184 // calls process idle itself
188 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
190 // TODO probably reenable the parent window if any
191 s_macIsInModalLoop
= formerModal
;
196 // Replacement for Show(true) for modal dialogs - returns return code
197 int wxDialog::ShowModal()
199 if ( !m_isModalStyle
)
204 return GetReturnCode();
207 // NB: this function (surprizingly) may be called for both modal and modeless
208 // dialogs and should work for both of them
209 void wxDialog::EndModal(int retCode
)
211 SetReturnCode(retCode
);
217 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
219 if ( Validate() && TransferDataFromWindow() )
223 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
226 TransferDataFromWindow();
228 // TODO probably need to disable the Apply button until things change again
231 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
233 EndModal(wxID_CANCEL
);
236 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
238 // We'll send a Cancel message by default,
239 // which may close the dialog.
240 // Check for looping if the Cancel event handler calls Close().
242 // Note that if a cancel button and handler aren't present in the dialog,
243 // nothing will happen when you close the dialog via the window manager, or
245 // We wouldn't want to destroy the dialog by default, since the dialog may have been
246 // created on the stack.
247 // However, this does mean that calling dialog->Close() won't delete the dialog
248 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
249 // sure to destroy the dialog.
250 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
252 static wxList closing
;
254 if ( closing
.Member(this) )
257 closing
.Append(this);
259 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
260 cancelEvent
.SetEventObject( this );
261 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
263 closing
.DeleteObject(this);
266 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
268 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));