Include wx/frame.h according to precompiled headers of wx/wx.h (with other minor...
[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 #endif // WX_PRECOMP
19
20 #include "wx/cursor.h"
21 #include "wx/evtloop.h"
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "wx/gtk1/win_gtk.h"
28
29 //-----------------------------------------------------------------------------
30 // global data
31 //-----------------------------------------------------------------------------
32
33 extern int g_openDialogs;
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 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
180 }
181
182 int wxDialog::ShowModal()
183 {
184 if (IsModal())
185 {
186 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
187 return GetReturnCode();
188 }
189
190 // use the apps top level window as parent if none given unless explicitly
191 // forbidden
192 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
193 {
194 wxWindow *parent = wxTheApp->GetTopWindow();
195 if ( parent &&
196 parent != this &&
197 parent->IsBeingDeleted() &&
198 !(parent->GetExtraStyle() & wxWS_EX_TRANSIENT) )
199 {
200 m_parent = parent;
201 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
202 }
203 }
204
205 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
206
207 Show( true );
208
209 m_modalShowing = true;
210
211 g_openDialogs++;
212
213 gtk_grab_add( m_widget );
214
215 wxEventLoop().Run();
216
217 gtk_grab_remove( m_widget );
218
219 g_openDialogs--;
220
221 return GetReturnCode();
222 }
223
224 void wxDialog::EndModal( int retCode )
225 {
226 SetReturnCode( retCode );
227
228 if (!IsModal())
229 {
230 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
231 return;
232 }
233
234 m_modalShowing = false;
235
236 gtk_main_quit();
237
238 Show( false );
239 }