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 #if TARGET_API_MAC_OSX
69 HIViewRef growBoxRef
= 0 ;
70 OSStatus err
= HIViewFindByID( HIViewGetRoot( (WindowRef
)m_macWindow
), kHIViewWindowGrowBoxID
, &growBoxRef
);
71 if ( err
== noErr
&& growBoxRef
!= 0 )
72 HIGrowBoxViewSetTransparent( growBoxRef
, true ) ;
78 void wxDialog::SetModal( bool flag
)
82 m_isModalStyle
= true;
84 wxModelessWindows
.DeleteObject( this );
87 SetWindowModality( (WindowRef
)MacGetWindowRef(), kWindowModalityAppModal
, NULL
) ;
92 m_isModalStyle
= false;
94 wxModelessWindows
.Append( this );
100 m_isBeingDeleted
= true;
104 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
105 void wxDialog::OnCharHook(wxKeyEvent
& event
)
107 if (( event
.m_keyCode
== WXK_ESCAPE
||
108 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
109 && FindWindow(wxID_CANCEL
) )
111 // Behaviour changed in 2.0: we'll send a Cancel message
112 // to the dialog instead of Close.
113 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
114 cancelEvent
.SetEventObject( this );
115 GetEventHandler()->ProcessEvent(cancelEvent
);
120 // We didn't process this event.
124 bool wxDialog::IsModal() const
126 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
127 // return m_isModalStyle;
131 bool wxDialog::IsModalShowing() const
133 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
136 bool wxDialog::Show(bool show
)
138 if ( !wxDialogBase::Show(show
) )
143 // usually will result in TransferDataToWindow() being called
146 if ( m_isModalStyle
)
152 else // end of modal dialog
154 // this will cause IsModalShowing() return false and our local
155 // message loop will terminate
156 wxModalDialogs
.DeleteObject(this);
164 extern bool s_macIsInModalLoop
;
167 void wxDialog::DoShowModal()
169 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
171 wxModalDialogs
.Append(this);
176 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
178 // TODO : test whether parent gets disabled
179 bool formerModal
= s_macIsInModalLoop
;
180 s_macIsInModalLoop
= true ;
183 while ( IsModalShowing() )
185 wxTheApp
->MacDoOneEvent() ;
186 // calls process idle itself
190 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
192 // TODO probably reenable the parent window if any
193 s_macIsInModalLoop
= formerModal
;
198 // Replacement for Show(true) for modal dialogs - returns return code
199 int wxDialog::ShowModal()
201 if ( !m_isModalStyle
)
206 return GetReturnCode();
209 // NB: this function (surprizingly) may be called for both modal and modeless
210 // dialogs and should work for both of them
211 void wxDialog::EndModal(int retCode
)
213 SetReturnCode(retCode
);
219 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
221 if ( Validate() && TransferDataFromWindow() )
225 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
228 TransferDataFromWindow();
230 // TODO probably need to disable the Apply button until things change again
233 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
235 EndModal(wxID_CANCEL
);
238 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
240 // We'll send a Cancel message by default,
241 // which may close the dialog.
242 // Check for looping if the Cancel event handler calls Close().
244 // Note that if a cancel button and handler aren't present in the dialog,
245 // nothing will happen when you close the dialog via the window manager, or
247 // We wouldn't want to destroy the dialog by default, since the dialog may have been
248 // created on the stack.
249 // However, this does mean that calling dialog->Close() won't delete the dialog
250 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
251 // sure to destroy the dialog.
252 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
254 static wxList closing
;
256 if ( closing
.Member(this) )
259 closing
.Append(this);
261 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
262 cancelEvent
.SetEventObject( this );
263 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
265 closing
.DeleteObject(this);
268 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
270 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));