]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dialog.cpp
added possibility to use several wxFileHistories (patch 685876)
[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 #ifdef __GNUG__
11 #pragma implementation "dialog.h"
12 #endif
13
14 #include "wx/dialog.h"
15 #include "wx/frame.h"
16 #include "wx/app.h"
17 #include "wx/cursor.h"
18
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
22
23 #include "wx/gtk/win_gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31 extern int g_openDialogs;
32
33 //-----------------------------------------------------------------------------
34 // wxDialog
35 //-----------------------------------------------------------------------------
36
37 BEGIN_EVENT_TABLE(wxDialog,wxDialogBase)
38 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
39 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
40 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
41 EVT_CLOSE (wxDialog::OnCloseWindow)
42 END_EVENT_TABLE()
43
44 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow)
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 GetEventHandler()->ProcessEvent(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 bool ret = wxWindow::Show( show );
164
165 if (show) InitDialog();
166
167 return ret;
168 }
169
170 bool wxDialog::IsModal() const
171 {
172 return m_modalShowing;
173 }
174
175 void wxDialog::SetModal( bool WXUNUSED(flag) )
176 {
177 /*
178 if (flag)
179 m_windowStyle |= wxDIALOG_MODAL;
180 else
181 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
182 */
183 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
184 }
185
186 int wxDialog::ShowModal()
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 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
197 {
198 wxWindow *parent = wxTheApp->GetTopWindow();
199 if ( parent &&
200 parent != this &&
201 parent->IsBeingDeleted() &&
202 !(parent->GetExtraStyle() & wxWS_EX_TRANSIENT) )
203 {
204 m_parent = parent;
205 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
206 }
207 }
208
209 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
210
211 Show( TRUE );
212
213 SetFocus();
214
215 m_modalShowing = TRUE;
216
217 g_openDialogs++;
218
219 gtk_grab_add( m_widget );
220 gtk_main();
221 gtk_grab_remove( m_widget );
222
223 g_openDialogs--;
224
225 return GetReturnCode();
226 }
227
228 void wxDialog::EndModal( int retCode )
229 {
230 SetReturnCode( retCode );
231
232 if (!IsModal())
233 {
234 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
235 return;
236 }
237
238 m_modalShowing = FALSE;
239
240 gtk_main_quit();
241
242 Show( FALSE );
243 }