]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dialog.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
c801d85f KB |
13 | #include "wx/dialog.h" |
14 | #include "wx/frame.h" | |
15 | #include "wx/app.h" | |
69ffe1d2 | 16 | #include "wx/cursor.h" |
924b84ab | 17 | #include "wx/evtloop.h" |
83624f79 | 18 | |
071a2d78 RR |
19 | #include <gdk/gdk.h> |
20 | #include <gtk/gtk.h> | |
21 | #include <gdk/gdkkeysyms.h> | |
22 | ||
3cbab641 | 23 | #include "wx/gtk1/win_gtk.h" |
5e014a0c | 24 | |
acfd422a | 25 | //----------------------------------------------------------------------------- |
91af0895 | 26 | // global data |
acfd422a RR |
27 | //----------------------------------------------------------------------------- |
28 | ||
2d68e1b4 | 29 | extern int g_openDialogs; |
acfd422a | 30 | |
c801d85f KB |
31 | //----------------------------------------------------------------------------- |
32 | // wxDialog | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
7d9f12f3 | 35 | BEGIN_EVENT_TABLE(wxDialog,wxDialogBase) |
fb1585ae RR |
36 | EVT_BUTTON (wxID_OK, wxDialog::OnOK) |
37 | EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel) | |
38 | EVT_BUTTON (wxID_APPLY, wxDialog::OnApply) | |
39 | EVT_CLOSE (wxDialog::OnCloseWindow) | |
c801d85f KB |
40 | END_EVENT_TABLE() |
41 | ||
7d9f12f3 | 42 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow) |
c801d85f | 43 | |
68995f26 | 44 | void wxDialog::Init() |
c801d85f | 45 | { |
f03fc89f | 46 | m_returnCode = 0; |
91af0895 WS |
47 | m_sizeSet = false; |
48 | m_modalShowing = false; | |
49 | m_themeEnabled = true; | |
c33c4050 | 50 | } |
c801d85f | 51 | |
2b854a32 | 52 | wxDialog::wxDialog( wxWindow *parent, |
fb1585ae | 53 | wxWindowID id, const wxString &title, |
2b854a32 | 54 | const wxPoint &pos, const wxSize &size, |
fb1585ae | 55 | long style, const wxString &name ) |
c801d85f | 56 | { |
68995f26 VZ |
57 | Init(); |
58 | ||
82c9f85c | 59 | (void)Create( parent, id, title, pos, size, style, name ); |
c33c4050 | 60 | } |
c801d85f KB |
61 | |
62 | bool wxDialog::Create( wxWindow *parent, | |
fb1585ae | 63 | wxWindowID id, const wxString &title, |
2b854a32 | 64 | const wxPoint &pos, const wxSize &size, |
fb1585ae | 65 | long style, const wxString &name ) |
c801d85f | 66 | { |
21f4383a | 67 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
2b854a32 | 68 | |
82c9f85c VZ |
69 | // all dialogs should have tab traversal enabled |
70 | style |= wxTAB_TRAVERSAL; | |
71 | ||
7d9f12f3 | 72 | return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name); |
c33c4050 | 73 | } |
c801d85f KB |
74 | |
75 | void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) ) | |
76 | { | |
82c9f85c VZ |
77 | if (Validate()) |
78 | TransferDataFromWindow(); | |
c33c4050 | 79 | } |
c801d85f KB |
80 | |
81 | void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) ) | |
82 | { | |
fb1585ae RR |
83 | if (IsModal()) |
84 | { | |
85 | EndModal(wxID_CANCEL); | |
86 | } | |
87 | else | |
88 | { | |
89 | SetReturnCode(wxID_CANCEL); | |
91af0895 | 90 | Show(false); |
fb1585ae | 91 | } |
c33c4050 | 92 | } |
c801d85f | 93 | |
903f689b | 94 | void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) ) |
c801d85f | 95 | { |
32ac755d | 96 | if (Validate() && TransferDataFromWindow()) |
1a6944fd | 97 | { |
2b854a32 | 98 | if (IsModal()) |
fb1585ae RR |
99 | { |
100 | EndModal(wxID_OK); | |
101 | } | |
102 | else | |
103 | { | |
104 | SetReturnCode(wxID_OK); | |
91af0895 | 105 | Show(false); |
fb1585ae | 106 | } |
1a6944fd | 107 | } |
c33c4050 | 108 | } |
c801d85f KB |
109 | |
110 | void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
111 | { | |
2b854a32 | 112 | // yes |
c33c4050 | 113 | } |
c801d85f | 114 | |
a492cb0f | 115 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 116 | { |
e3065973 JS |
117 | // We'll send a Cancel message by default, |
118 | // which may close the dialog. | |
119 | // Check for looping if the Cancel event handler calls Close(). | |
120 | ||
121 | // Note that if a cancel button and handler aren't present in the dialog, | |
122 | // nothing will happen when you close the dialog via the window manager, or | |
123 | // via Close(). | |
124 | // We wouldn't want to destroy the dialog by default, since the dialog may have been | |
125 | // created on the stack. | |
126 | // However, this does mean that calling dialog->Close() won't delete the dialog | |
127 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be | |
128 | // sure to destroy the dialog. | |
129 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. | |
130 | ||
ab2b3dd4 | 131 | static wxList s_closing; |
c801d85f | 132 | |
ab2b3dd4 | 133 | if (s_closing.Member(this)) |
2b854a32 VZ |
134 | return; // no loops |
135 | ||
ab2b3dd4 | 136 | s_closing.Append(this); |
c801d85f | 137 | |
fb1585ae RR |
138 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
139 | cancelEvent.SetEventObject( this ); | |
140 | GetEventHandler()->ProcessEvent(cancelEvent); | |
ab2b3dd4 | 141 | s_closing.DeleteObject(this); |
c801d85f KB |
142 | } |
143 | ||
debe6624 | 144 | bool wxDialog::Show( bool show ) |
c801d85f | 145 | { |
fb1585ae RR |
146 | if (!show && IsModal()) |
147 | { | |
de8113d9 | 148 | EndModal( wxID_CANCEL ); |
fb1585ae | 149 | } |
c801d85f | 150 | |
de8113d9 RR |
151 | if (show && !m_sizeSet) |
152 | { | |
153 | /* by calling GtkOnSize here, we don't have to call | |
154 | either after showing the frame, which would entail | |
155 | much ugly flicker nor from within the size_allocate | |
156 | handler, because GTK 1.1.X forbids that. */ | |
157 | ||
158 | GtkOnSize( m_x, m_y, m_width, m_height ); | |
159 | } | |
2b854a32 | 160 | |
739730ca | 161 | bool ret = wxWindow::Show( show ); |
e146b8c8 | 162 | |
fb1585ae | 163 | if (show) InitDialog(); |
2b854a32 | 164 | |
739730ca | 165 | return ret; |
c33c4050 RR |
166 | } |
167 | ||
43a18898 | 168 | bool wxDialog::IsModal() const |
e1e955e1 | 169 | { |
fb1585ae | 170 | return m_modalShowing; |
e1e955e1 RR |
171 | } |
172 | ||
173 | void wxDialog::SetModal( bool WXUNUSED(flag) ) | |
c33c4050 | 174 | { |
223d09f6 | 175 | wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") ); |
c33c4050 | 176 | } |
c801d85f | 177 | |
43a18898 | 178 | int wxDialog::ShowModal() |
c801d85f | 179 | { |
fb1585ae RR |
180 | if (IsModal()) |
181 | { | |
223d09f6 | 182 | wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") ); |
fb1585ae RR |
183 | return GetReturnCode(); |
184 | } | |
e146b8c8 | 185 | |
b3daa5a3 VZ |
186 | // use the apps top level window as parent if none given unless explicitly |
187 | // forbidden | |
188 | if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) ) | |
f6bcfd97 BP |
189 | { |
190 | wxWindow *parent = wxTheApp->GetTopWindow(); | |
39cc7a0b VZ |
191 | if ( parent && |
192 | parent != this && | |
193 | parent->IsBeingDeleted() && | |
194 | !(parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ) | |
f6bcfd97 BP |
195 | { |
196 | m_parent = parent; | |
197 | gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) ); | |
198 | } | |
199 | } | |
200 | ||
eebe4016 | 201 | wxBusyCursorSuspender cs; // temporarily suppress the busy cursor |
91af0895 WS |
202 | |
203 | Show( true ); | |
2b854a32 | 204 | |
91af0895 | 205 | m_modalShowing = true; |
2b854a32 | 206 | |
304e5625 RR |
207 | g_openDialogs++; |
208 | ||
fb1585ae | 209 | gtk_grab_add( m_widget ); |
924b84ab VS |
210 | |
211 | wxEventLoop().Run(); | |
212 | ||
fb1585ae | 213 | gtk_grab_remove( m_widget ); |
2b854a32 | 214 | |
304e5625 RR |
215 | g_openDialogs--; |
216 | ||
fb1585ae | 217 | return GetReturnCode(); |
c33c4050 | 218 | } |
c801d85f KB |
219 | |
220 | void wxDialog::EndModal( int retCode ) | |
221 | { | |
fb1585ae | 222 | SetReturnCode( retCode ); |
2b854a32 | 223 | |
fb1585ae RR |
224 | if (!IsModal()) |
225 | { | |
223d09f6 | 226 | wxFAIL_MSG( wxT("wxDialog:EndModal called twice") ); |
fb1585ae RR |
227 | return; |
228 | } | |
2b854a32 | 229 | |
91af0895 | 230 | m_modalShowing = false; |
2b854a32 | 231 | |
fb1585ae | 232 | gtk_main_quit(); |
2b854a32 | 233 | |
91af0895 | 234 | Show( false ); |
c33c4050 | 235 | } |