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_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
39 EVT_CLOSE(wxDialog::OnCloseWindow
)
45 m_isModalStyle
= false;
48 bool wxDialog::Create( wxWindow
*parent
,
50 const wxString
& title
,
54 const wxString
& name
)
56 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
58 // All dialogs should really have this style...
59 style
|= wxTAB_TRAVERSAL
;
61 // ...but not these styles
62 style
&= ~(wxYES
| wxOK
| wxNO
); // | wxCANCEL
64 if ( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
, name
) )
67 #if TARGET_API_MAC_OSX
68 HIViewRef growBoxRef
= 0 ;
69 OSStatus err
= HIViewFindByID( HIViewGetRoot( (WindowRef
)m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
70 if ( err
== noErr
&& growBoxRef
!= 0 )
71 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
77 void wxDialog::SetModal( bool flag
)
81 m_isModalStyle
= true;
83 wxModelessWindows
.DeleteObject( this );
86 SetWindowModality( (WindowRef
)MacGetWindowRef(), kWindowModalityAppModal
, NULL
) ;
91 m_isModalStyle
= false;
93 wxModelessWindows
.Append( this );
99 m_isBeingDeleted
= true;
103 // On mac command-stop does the same thing as Esc, let the base class know
105 bool wxDialog::IsEscapeKey(const wxKeyEvent
& event
)
107 if ( event
.GetKeyCode() == '.' && event
.GetModifiers() == wxMOD_CMD
)
110 return wxDialogBase::IsEscapeKey(event
);
113 bool wxDialog::IsModal() const
115 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
116 // return m_isModalStyle;
120 bool wxDialog::IsModalShowing() const
122 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
125 bool wxDialog::Show(bool show
)
127 if ( !wxDialogBase::Show(show
) )
132 // usually will result in TransferDataToWindow() being called
135 if ( m_isModalStyle
)
141 else // end of modal dialog
143 // this will cause IsModalShowing() return false and our local
144 // message loop will terminate
145 wxModalDialogs
.DeleteObject(this);
153 extern bool s_macIsInModalLoop
;
156 void wxDialog::DoShowModal()
158 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
160 wxModalDialogs
.Append(this);
165 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
167 // TODO : test whether parent gets disabled
168 bool formerModal
= s_macIsInModalLoop
;
169 s_macIsInModalLoop
= true ;
172 while ( IsModalShowing() )
174 wxTheApp
->MacDoOneEvent() ;
175 // calls process idle itself
179 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
181 // TODO probably reenable the parent window if any
182 s_macIsInModalLoop
= formerModal
;
187 // Replacement for Show(true) for modal dialogs - returns return code
188 int wxDialog::ShowModal()
190 if ( !m_isModalStyle
)
195 return GetReturnCode();
198 // NB: this function (surprizingly) may be called for both modal and modeless
199 // dialogs and should work for both of them
200 void wxDialog::EndModal(int retCode
)
202 SetReturnCode(retCode
);
208 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
210 if ( Validate() && TransferDataFromWindow() )
214 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
217 TransferDataFromWindow();
219 // TODO probably need to disable the Apply button until things change again
222 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
224 EndModal(wxID_CANCEL
);
227 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
229 // We'll send a Cancel message by default,
230 // which may close the dialog.
231 // Check for looping if the Cancel event handler calls Close().
233 // Note that if a cancel button and handler aren't present in the dialog,
234 // nothing will happen when you close the dialog via the window manager, or
236 // We wouldn't want to destroy the dialog by default, since the dialog may have been
237 // created on the stack.
238 // However, this does mean that calling dialog->Close() won't delete the dialog
239 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
240 // sure to destroy the dialog.
241 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
243 static wxList closing
;
245 if ( closing
.Member(this) )
248 closing
.Append(this);
250 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
251 cancelEvent
.SetEventObject( this );
252 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
254 closing
.DeleteObject(this);
257 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
259 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));