]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dialog.cpp
updated setup.h for OpenVMS
[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
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_COMMAND_BUTTON_CLICKED, 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 if (IsModal())
186 {
187 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
188 return GetReturnCode();
189 }
190
191 // use the apps top level window as parent if none given unless explicitly
192 // forbidden
193 wxWindow * const parent = GetParentForModalDialog();
194 if ( parent )
195 {
196 m_parent = parent;
197 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
198 }
199
200 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
201
202 Show( true );
203
204 m_modalShowing = true;
205
206 g_openDialogs++;
207
208 gtk_grab_add( m_widget );
209
210 wxEventLoop().Run();
211
212 gtk_grab_remove( m_widget );
213
214 g_openDialogs--;
215
216 return GetReturnCode();
217 }
218
219 void wxDialog::EndModal( int retCode )
220 {
221 SetReturnCode( retCode );
222
223 if (!IsModal())
224 {
225 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
226 return;
227 }
228
229 m_modalShowing = false;
230
231 gtk_main_quit();
232
233 Show( false );
234 }