1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dialog.cpp
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"
22 #include "wx/settings.h"
24 #include "wx/mac/uma.h"
27 // Lists to keep track of windows, so we can disable/enable them
29 wxList wxModalDialogs
;
31 extern wxList wxPendingDelete
;
33 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
35 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
36 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
37 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
38 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
40 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
42 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
44 EVT_CLOSE(wxDialog::OnCloseWindow
)
50 m_isModalStyle
= false;
53 bool wxDialog::Create( wxWindow
*parent
,
55 const wxString
& title
,
59 const wxString
& name
)
61 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
63 // All dialogs should really have this style...
64 style
|= wxTAB_TRAVERSAL
;
66 // ...but not these styles
67 style
&= ~(wxYES
| wxOK
| wxNO
); // | wxCANCEL
69 if ( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
, name
) )
72 #if TARGET_API_MAC_OSX
73 HIViewRef growBoxRef
= 0 ;
74 OSStatus err
= HIViewFindByID( HIViewGetRoot( (WindowRef
)m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
75 if ( err
== noErr
&& growBoxRef
!= 0 )
76 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
82 void wxDialog::SetModal( bool flag
)
86 m_isModalStyle
= true;
88 wxModelessWindows
.DeleteObject( this );
91 SetWindowModality( (WindowRef
)MacGetWindowRef(), kWindowModalityAppModal
, NULL
) ;
96 m_isModalStyle
= false;
98 wxModelessWindows
.Append( this );
102 wxDialog::~wxDialog()
104 m_isBeingDeleted
= true;
108 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
109 void wxDialog::OnCharHook(wxKeyEvent
& event
)
111 if (( event
.m_keyCode
== WXK_ESCAPE
||
112 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
113 && FindWindow(wxID_CANCEL
) )
115 // Behaviour changed in 2.0: we'll send a Cancel message
116 // to the dialog instead of Close.
117 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
118 cancelEvent
.SetEventObject( this );
119 GetEventHandler()->ProcessEvent(cancelEvent
);
124 // We didn't process this event.
128 bool wxDialog::IsModal() const
130 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
131 // return m_isModalStyle;
135 bool wxDialog::IsModalShowing() const
137 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
140 bool wxDialog::Show(bool show
)
142 if ( !wxDialogBase::Show(show
) )
147 // usually will result in TransferDataToWindow() being called
150 if ( m_isModalStyle
)
156 else // end of modal dialog
158 // this will cause IsModalShowing() return false and our local
159 // message loop will terminate
160 wxModalDialogs
.DeleteObject(this);
168 extern bool s_macIsInModalLoop
;
171 void wxDialog::DoShowModal()
173 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
175 wxModalDialogs
.Append(this);
180 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
182 // TODO : test whether parent gets disabled
183 bool formerModal
= s_macIsInModalLoop
;
184 s_macIsInModalLoop
= true ;
187 while ( IsModalShowing() )
189 wxTheApp
->MacDoOneEvent() ;
190 // calls process idle itself
194 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
196 // TODO probably reenable the parent window if any
197 s_macIsInModalLoop
= formerModal
;
202 // Replacement for Show(true) for modal dialogs - returns return code
203 int wxDialog::ShowModal()
205 if ( !m_isModalStyle
)
210 return GetReturnCode();
213 // NB: this function (surprizingly) may be called for both modal and modeless
214 // dialogs and should work for both of them
215 void wxDialog::EndModal(int retCode
)
217 SetReturnCode(retCode
);
223 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
225 if ( Validate() && TransferDataFromWindow() )
229 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
232 TransferDataFromWindow();
234 // TODO probably need to disable the Apply button until things change again
237 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
239 EndModal(wxID_CANCEL
);
242 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
244 // We'll send a Cancel message by default,
245 // which may close the dialog.
246 // Check for looping if the Cancel event handler calls Close().
248 // Note that if a cancel button and handler aren't present in the dialog,
249 // nothing will happen when you close the dialog via the window manager, or
251 // We wouldn't want to destroy the dialog by default, since the dialog may have been
252 // created on the stack.
253 // However, this does mean that calling dialog->Close() won't delete the dialog
254 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
255 // sure to destroy the dialog.
256 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
258 static wxList closing
;
260 if ( closing
.Member(this) )
263 closing
.Append(this);
265 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
266 cancelEvent
.SetEventObject( this );
267 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
269 closing
.DeleteObject(this);
272 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
274 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));