don't use windows about to be destroyed as implicit dialog parent
[wxWidgets.git] / src / gtk1 / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "dialog.h"
12 #endif
13
14 #include "wx/dialog.h"
15 #include "wx/frame.h"
16 #include "wx/app.h"
17 #include "wx/cursor.h"
18
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
22
23 #include "wx/gtk/win_gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31 extern int g_openDialogs;
32
33
34
35 //-----------------------------------------------------------------------------
36 // wxDialog
37 //-----------------------------------------------------------------------------
38
39 BEGIN_EVENT_TABLE(wxDialog,wxDialogBase)
40 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
41 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
42 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
43 EVT_CLOSE (wxDialog::OnCloseWindow)
44 END_EVENT_TABLE()
45
46 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow)
47
48 void wxDialog::Init()
49 {
50 m_returnCode = 0;
51 m_sizeSet = FALSE;
52 m_modalShowing = FALSE;
53 m_themeEnabled = TRUE;
54 }
55
56 wxDialog::wxDialog( wxWindow *parent,
57 wxWindowID id, const wxString &title,
58 const wxPoint &pos, const wxSize &size,
59 long style, const wxString &name )
60 {
61 Init();
62
63 (void)Create( parent, id, title, pos, size, style, name );
64 }
65
66 bool wxDialog::Create( wxWindow *parent,
67 wxWindowID id, const wxString &title,
68 const wxPoint &pos, const wxSize &size,
69 long style, const wxString &name )
70 {
71 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
72
73 // all dialogs should have tab traversal enabled
74 style |= wxTAB_TRAVERSAL;
75
76 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
77 }
78
79 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
80 {
81 if (Validate())
82 TransferDataFromWindow();
83 }
84
85 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
86 {
87 if (IsModal())
88 {
89 EndModal(wxID_CANCEL);
90 }
91 else
92 {
93 SetReturnCode(wxID_CANCEL);
94 Show(FALSE);
95 }
96 }
97
98 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
99 {
100 if (Validate() && TransferDataFromWindow())
101 {
102 if (IsModal())
103 {
104 EndModal(wxID_OK);
105 }
106 else
107 {
108 SetReturnCode(wxID_OK);
109 Show(FALSE);
110 }
111 }
112 }
113
114 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
115 {
116 // yes
117 }
118
119 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
120 {
121 // We'll send a Cancel message by default,
122 // which may close the dialog.
123 // Check for looping if the Cancel event handler calls Close().
124
125 // Note that if a cancel button and handler aren't present in the dialog,
126 // nothing will happen when you close the dialog via the window manager, or
127 // via Close().
128 // We wouldn't want to destroy the dialog by default, since the dialog may have been
129 // created on the stack.
130 // However, this does mean that calling dialog->Close() won't delete the dialog
131 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
132 // sure to destroy the dialog.
133 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
134
135 static wxList s_closing;
136
137 if (s_closing.Member(this))
138 return; // no loops
139
140 s_closing.Append(this);
141
142 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
143 cancelEvent.SetEventObject( this );
144 GetEventHandler()->ProcessEvent(cancelEvent);
145 s_closing.DeleteObject(this);
146 }
147
148 bool wxDialog::Show( bool show )
149 {
150 if (!show && IsModal())
151 {
152 EndModal( wxID_CANCEL );
153 }
154
155 if (show && !m_sizeSet)
156 {
157 /* by calling GtkOnSize here, we don't have to call
158 either after showing the frame, which would entail
159 much ugly flicker nor from within the size_allocate
160 handler, because GTK 1.1.X forbids that. */
161
162 GtkOnSize( m_x, m_y, m_width, m_height );
163 }
164
165 bool ret = wxWindow::Show( show );
166
167 if (show) InitDialog();
168
169 return ret;
170 }
171
172 bool wxDialog::IsModal() const
173 {
174 return m_modalShowing;
175 }
176
177 void wxDialog::SetModal( bool WXUNUSED(flag) )
178 {
179 /*
180 if (flag)
181 m_windowStyle |= wxDIALOG_MODAL;
182 else
183 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
184 */
185 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
186 }
187
188 int wxDialog::ShowModal()
189 {
190 if (IsModal())
191 {
192 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
193 return GetReturnCode();
194 }
195
196 // use the apps top level window as parent if none given unless explicitly
197 // forbidden
198 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
199 {
200 wxWindow *parent = wxTheApp->GetTopWindow();
201 if ( parent && parent != this && parent->IsBeingDeleted() )
202 {
203 m_parent = parent;
204 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
205 }
206 }
207
208 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
209
210 Show( TRUE );
211
212 m_modalShowing = TRUE;
213
214 g_openDialogs++;
215
216 gtk_grab_add( m_widget );
217 gtk_main();
218 gtk_grab_remove( m_widget );
219
220 g_openDialogs--;
221
222 return GetReturnCode();
223 }
224
225 void wxDialog::EndModal( int retCode )
226 {
227 SetReturnCode( retCode );
228
229 if (!IsModal())
230 {
231 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
232 return;
233 }
234
235 m_modalShowing = FALSE;
236
237 gtk_main_quit();
238
239 Show( FALSE );
240 }