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