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