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 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
32 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
33 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
34 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
35 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
37 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
39 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
41 EVT_CLOSE(wxDialog::OnCloseWindow
)
47 m_isModalStyle
= false;
50 bool wxDialog::Create( wxWindow
*parent
,
52 const wxString
& title
,
56 const wxString
& name
)
58 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
60 // All dialogs should really have this style...
61 style
|= wxTAB_TRAVERSAL
;
63 // ...but not these styles
64 style
&= ~(wxYES
| wxOK
| wxNO
); // | wxCANCEL
66 if ( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
, name
) )
69 #if TARGET_API_MAC_OSX
70 HIViewRef growBoxRef
= 0 ;
71 OSStatus err
= HIViewFindByID( HIViewGetRoot( (WindowRef
)m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
72 if ( err
== noErr
&& growBoxRef
!= 0 )
73 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
79 void wxDialog::SetModal( bool flag
)
83 m_isModalStyle
= true;
85 wxModelessWindows
.DeleteObject( this );
88 SetWindowModality( (WindowRef
)MacGetWindowRef(), kWindowModalityAppModal
, NULL
) ;
93 m_isModalStyle
= false;
95 wxModelessWindows
.Append( this );
101 m_isBeingDeleted
= true;
105 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
106 void wxDialog::OnCharHook(wxKeyEvent
& event
)
108 if (( event
.m_keyCode
== WXK_ESCAPE
||
109 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
110 && FindWindow(wxID_CANCEL
) )
112 // Behaviour changed in 2.0: we'll send a Cancel message
113 // to the dialog instead of Close.
114 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
115 cancelEvent
.SetEventObject( this );
116 GetEventHandler()->ProcessEvent(cancelEvent
);
121 // We didn't process this event.
125 bool wxDialog::IsModal() const
127 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
128 // return m_isModalStyle;
132 bool wxDialog::IsModalShowing() const
134 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
137 bool wxDialog::Show(bool show
)
139 if ( !wxDialogBase::Show(show
) )
144 // usually will result in TransferDataToWindow() being called
147 if ( m_isModalStyle
)
153 else // end of modal dialog
155 // this will cause IsModalShowing() return false and our local
156 // message loop will terminate
157 wxModalDialogs
.DeleteObject(this);
165 extern bool s_macIsInModalLoop
;
168 void wxDialog::DoShowModal()
170 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
172 wxModalDialogs
.Append(this);
177 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
179 // TODO : test whether parent gets disabled
180 bool formerModal
= s_macIsInModalLoop
;
181 s_macIsInModalLoop
= true ;
184 while ( IsModalShowing() )
186 wxTheApp
->MacDoOneEvent() ;
187 // calls process idle itself
191 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
193 // TODO probably reenable the parent window if any
194 s_macIsInModalLoop
= formerModal
;
199 // Replacement for Show(true) for modal dialogs - returns return code
200 int wxDialog::ShowModal()
202 if ( !m_isModalStyle
)
207 return GetReturnCode();
210 // NB: this function (surprizingly) may be called for both modal and modeless
211 // dialogs and should work for both of them
212 void wxDialog::EndModal(int retCode
)
214 SetReturnCode(retCode
);
220 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
222 if ( Validate() && TransferDataFromWindow() )
226 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
229 TransferDataFromWindow();
231 // TODO probably need to disable the Apply button until things change again
234 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
236 EndModal(wxID_CANCEL
);
239 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
241 // We'll send a Cancel message by default,
242 // which may close the dialog.
243 // Check for looping if the Cancel event handler calls Close().
245 // Note that if a cancel button and handler aren't present in the dialog,
246 // nothing will happen when you close the dialog via the window manager, or
248 // We wouldn't want to destroy the dialog by default, since the dialog may have been
249 // created on the stack.
250 // However, this does mean that calling dialog->Close() won't delete the dialog
251 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
252 // sure to destroy the dialog.
253 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
255 static wxList closing
;
257 if ( closing
.Member(this) )
260 closing
.Append(this);
262 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
263 cancelEvent
.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
266 closing
.DeleteObject(this);
269 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
271 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));