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