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"
20 #include "wx/settings.h"
23 #include "wx/mac/uma.h"
26 // Lists to keep track of windows, so we can disable/enable them
28 wxList wxModalDialogs
;
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
)
49 m_isModalStyle
= false;
52 bool wxDialog::Create( wxWindow
*parent
,
54 const wxString
& title
,
58 const wxString
& name
)
60 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
62 // All dialogs should really have this style...
63 style
|= wxTAB_TRAVERSAL
;
65 // ...but not these styles
66 style
&= ~(wxYES
| wxOK
| wxNO
); // | wxCANCEL
68 if ( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
, name
) )
71 #if TARGET_API_MAC_OSX
72 HIViewRef growBoxRef
= 0 ;
73 OSStatus err
= HIViewFindByID( HIViewGetRoot( (WindowRef
)m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
74 if ( err
== noErr
&& growBoxRef
!= 0 )
75 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
81 void wxDialog::SetModal( bool flag
)
85 m_isModalStyle
= true;
87 wxModelessWindows
.DeleteObject( this );
90 SetWindowModality( (WindowRef
)MacGetWindowRef(), kWindowModalityAppModal
, NULL
) ;
95 m_isModalStyle
= false;
97 wxModelessWindows
.Append( this );
101 wxDialog::~wxDialog()
103 m_isBeingDeleted
= true;
107 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
108 void wxDialog::OnCharHook(wxKeyEvent
& event
)
110 if (( event
.m_keyCode
== WXK_ESCAPE
||
111 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
112 && FindWindow(wxID_CANCEL
) )
114 // Behaviour changed in 2.0: we'll send a Cancel message
115 // to the dialog instead of Close.
116 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
117 cancelEvent
.SetEventObject( this );
118 GetEventHandler()->ProcessEvent(cancelEvent
);
123 // We didn't process this event.
127 bool wxDialog::IsModal() const
129 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
130 // return m_isModalStyle;
134 bool wxDialog::IsModalShowing() const
136 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
139 bool wxDialog::Show(bool show
)
141 if ( !wxDialogBase::Show(show
) )
146 // usually will result in TransferDataToWindow() being called
149 if ( m_isModalStyle
)
155 else // end of modal dialog
157 // this will cause IsModalShowing() return false and our local
158 // message loop will terminate
159 wxModalDialogs
.DeleteObject(this);
167 extern bool s_macIsInModalLoop
;
170 void wxDialog::DoShowModal()
172 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
174 wxModalDialogs
.Append(this);
179 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
181 // TODO : test whether parent gets disabled
182 bool formerModal
= s_macIsInModalLoop
;
183 s_macIsInModalLoop
= true ;
186 while ( IsModalShowing() )
188 wxTheApp
->MacDoOneEvent() ;
189 // calls process idle itself
193 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
195 // TODO probably reenable the parent window if any
196 s_macIsInModalLoop
= formerModal
;
201 // Replacement for Show(true) for modal dialogs - returns return code
202 int wxDialog::ShowModal()
204 if ( !m_isModalStyle
)
209 return GetReturnCode();
212 // NB: this function (surprizingly) may be called for both modal and modeless
213 // dialogs and should work for both of them
214 void wxDialog::EndModal(int retCode
)
216 SetReturnCode(retCode
);
222 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
224 if ( Validate() && TransferDataFromWindow() )
228 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
231 TransferDataFromWindow();
233 // TODO probably need to disable the Apply button until things change again
236 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
238 EndModal(wxID_CANCEL
);
241 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
243 // We'll send a Cancel message by default,
244 // which may close the dialog.
245 // Check for looping if the Cancel event handler calls Close().
247 // Note that if a cancel button and handler aren't present in the dialog,
248 // nothing will happen when you close the dialog via the window manager, or
250 // We wouldn't want to destroy the dialog by default, since the dialog may have been
251 // created on the stack.
252 // However, this does mean that calling dialog->Close() won't delete the dialog
253 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
254 // sure to destroy the dialog.
255 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
257 static wxList closing
;
259 if ( closing
.Member(this) )
262 closing
.Append(this);
264 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
265 cancelEvent
.SetEventObject( this );
266 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
268 closing
.DeleteObject(this);
271 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
273 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));