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