]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/gtk/dialog.cpp |
c801d85f KB |
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 | 13 | #include "wx/dialog.h" |
670f9935 WS |
14 | |
15 | #ifndef WX_PRECOMP | |
c8326d64 | 16 | #include "wx/cursor.h" |
670f9935 WS |
17 | #endif // WX_PRECOMP |
18 | ||
924b84ab | 19 | #include "wx/evtloop.h" |
83624f79 | 20 | |
664e1314 | 21 | #include "wx/scopedptr.h" |
643e9cf9 | 22 | #include "wx/testing.h" |
bfafa628 | 23 | |
071a2d78 | 24 | #include <gtk/gtk.h> |
4ea2d0d5 | 25 | #include "wx/gtk/private/gtk2-compat.h" |
5e014a0c | 26 | |
64c11164 VZ |
27 | // this is defined in src/gtk/toplevel.cpp |
28 | extern int wxOpenModalDialogsCount; | |
acfd422a | 29 | |
5c69ef61 | 30 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxGUIEventLoop) |
bfafa628 VZ |
31 | |
32 | ||
c801d85f KB |
33 | //----------------------------------------------------------------------------- |
34 | // wxDialog | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
68995f26 | 37 | void wxDialog::Init() |
c801d85f | 38 | { |
bfafa628 | 39 | m_modalLoop = NULL; |
f03fc89f | 40 | m_returnCode = 0; |
91af0895 WS |
41 | m_modalShowing = false; |
42 | m_themeEnabled = true; | |
c33c4050 | 43 | } |
c801d85f | 44 | |
2b854a32 | 45 | wxDialog::wxDialog( wxWindow *parent, |
fb1585ae | 46 | wxWindowID id, const wxString &title, |
2b854a32 | 47 | const wxPoint &pos, const wxSize &size, |
fb1585ae | 48 | long style, const wxString &name ) |
c801d85f | 49 | { |
68995f26 VZ |
50 | Init(); |
51 | ||
82c9f85c | 52 | (void)Create( parent, id, title, pos, size, style, name ); |
c33c4050 | 53 | } |
c801d85f KB |
54 | |
55 | bool wxDialog::Create( wxWindow *parent, | |
fb1585ae | 56 | wxWindowID id, const wxString &title, |
2b854a32 | 57 | const wxPoint &pos, const wxSize &size, |
fb1585ae | 58 | long style, const wxString &name ) |
c801d85f | 59 | { |
21f4383a | 60 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
2b854a32 | 61 | |
82c9f85c VZ |
62 | // all dialogs should have tab traversal enabled |
63 | style |= wxTAB_TRAVERSAL; | |
64 | ||
7d9f12f3 | 65 | return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name); |
c33c4050 | 66 | } |
c801d85f | 67 | |
debe6624 | 68 | bool wxDialog::Show( bool show ) |
c801d85f | 69 | { |
fb1585ae RR |
70 | if (!show && IsModal()) |
71 | { | |
de8113d9 | 72 | EndModal( wxID_CANCEL ); |
fb1585ae | 73 | } |
c801d85f | 74 | |
3aa8e4ea JS |
75 | if (show && CanDoLayoutAdaptation()) |
76 | DoLayoutAdaptation(); | |
77 | ||
8fb09a08 | 78 | bool ret = wxDialogBase::Show(show); |
e146b8c8 | 79 | |
a9efc294 VS |
80 | if (show) |
81 | InitDialog(); | |
2b854a32 | 82 | |
739730ca | 83 | return ret; |
c33c4050 RR |
84 | } |
85 | ||
a9efc294 VS |
86 | wxDialog::~wxDialog() |
87 | { | |
a9efc294 | 88 | // if the dialog is modal, this will end its event loop |
0af07cc2 VS |
89 | if ( IsModal() ) |
90 | EndModal(wxID_CANCEL); | |
a9efc294 VS |
91 | } |
92 | ||
43a18898 | 93 | bool wxDialog::IsModal() const |
e1e955e1 | 94 | { |
fb1585ae | 95 | return m_modalShowing; |
e1e955e1 RR |
96 | } |
97 | ||
4ea2d0d5 PC |
98 | // Workaround for Ubuntu overlay scrollbar, which adds our GtkWindow to a |
99 | // private window group in a GtkScrollbar realize handler. This breaks the grab | |
100 | // done by gtk_window_set_modal(), and allows menus and toolbars in the parent | |
101 | // frame to remain active. So, we install an emission hook on the "realize" | |
102 | // signal while showing a modal dialog. For any realize on a GtkScrollbar, | |
103 | // we check the top level parent to see if it has an explicitly set window | |
104 | // group that is not the same as its transient parent. If we find this, we | |
105 | // put the top level back in the same window group as its transient parent, and | |
106 | // re-add the grab. | |
107 | // Ubuntu 12.04 and 12.10 are known to have this problem. | |
108 | ||
109 | // need 2.10 for gtk_window_get_group() | |
110 | #if GTK_CHECK_VERSION(2,10,0) | |
111 | extern "C" { | |
112 | static gboolean | |
113 | realize_hook(GSignalInvocationHint*, unsigned, const GValue* param_values, void*) | |
114 | { | |
115 | void* p = g_value_peek_pointer(param_values); | |
116 | if (GTK_IS_SCROLLBAR(p)) | |
117 | { | |
118 | GtkWindow* toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(p))); | |
119 | GtkWindow* transient_parent = gtk_window_get_transient_for(toplevel); | |
120 | if (transient_parent && gtk_window_has_group(toplevel)) | |
121 | { | |
122 | GtkWindowGroup* group = gtk_window_get_group(toplevel); | |
123 | GtkWindowGroup* group_parent = gtk_window_get_group(transient_parent); | |
124 | if (group != group_parent) | |
125 | { | |
126 | gtk_window_group_add_window(group_parent, toplevel); | |
127 | gtk_grab_add(GTK_WIDGET(toplevel)); | |
128 | } | |
129 | } | |
130 | } | |
131 | return true; | |
132 | } | |
133 | } | |
134 | #endif // GTK 2.10 | |
135 | ||
43a18898 | 136 | int wxDialog::ShowModal() |
c801d85f | 137 | { |
643e9cf9 VS |
138 | WX_TESTING_SHOW_MODAL_HOOK(); |
139 | ||
4e2a3778 | 140 | wxASSERT_MSG( !IsModal(), "ShowModal() can't be called twice" ); |
e146b8c8 | 141 | |
7738af59 VZ |
142 | // release the mouse if it's currently captured as the window having it |
143 | // will be disabled when this dialog is shown -- but will still keep the | |
144 | // capture making it impossible to do anything in the modal dialog itself | |
145 | wxWindow * const win = wxWindow::GetCapture(); | |
146 | if ( win ) | |
147 | win->GTKReleaseMouseAndNotify(); | |
148 | ||
cdc48273 | 149 | wxWindow * const parent = GetParentForModalDialog(); |
8bda0ec6 | 150 | if ( parent ) |
f6bcfd97 | 151 | { |
8bda0ec6 VZ |
152 | gtk_window_set_transient_for( GTK_WINDOW(m_widget), |
153 | GTK_WINDOW(parent->m_widget) ); | |
f6bcfd97 BP |
154 | } |
155 | ||
eebe4016 | 156 | wxBusyCursorSuspender cs; // temporarily suppress the busy cursor |
91af0895 | 157 | |
4ea2d0d5 PC |
158 | #if GTK_CHECK_VERSION(2,10,0) |
159 | unsigned sigId = 0; | |
160 | gulong hookId = 0; | |
161 | #ifndef __WXGTK3__ | |
162 | // Ubuntu overlay scrollbar uses at least GTK 2.24 | |
163 | if (gtk_check_version(2,24,0) == NULL) | |
164 | #endif | |
165 | { | |
166 | sigId = g_signal_lookup("realize", GTK_TYPE_WIDGET); | |
167 | hookId = g_signal_add_emission_hook(sigId, 0, realize_hook, NULL, NULL); | |
168 | } | |
169 | #endif | |
170 | ||
91af0895 | 171 | Show( true ); |
2b854a32 | 172 | |
91af0895 | 173 | m_modalShowing = true; |
2b854a32 | 174 | |
64c11164 | 175 | wxOpenModalDialogsCount++; |
304e5625 | 176 | |
f36630af VZ |
177 | // NOTE: gtk_window_set_modal internally calls gtk_grab_add() ! |
178 | gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE); | |
924b84ab | 179 | |
bfafa628 VZ |
180 | // Run modal dialog event loop. |
181 | { | |
182 | wxGUIEventLoopTiedPtr modal(&m_modalLoop, new wxGUIEventLoop()); | |
183 | m_modalLoop->Run(); | |
184 | } | |
924b84ab | 185 | |
4ea2d0d5 PC |
186 | #if GTK_CHECK_VERSION(2,10,0) |
187 | if (sigId) | |
188 | g_signal_remove_emission_hook(sigId, hookId); | |
189 | #endif | |
190 | ||
f36630af | 191 | gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE); |
2b854a32 | 192 | |
64c11164 | 193 | wxOpenModalDialogsCount--; |
304e5625 | 194 | |
fb1585ae | 195 | return GetReturnCode(); |
c33c4050 | 196 | } |
c801d85f KB |
197 | |
198 | void wxDialog::EndModal( int retCode ) | |
199 | { | |
fb1585ae | 200 | SetReturnCode( retCode ); |
2b854a32 | 201 | |
fb1585ae RR |
202 | if (!IsModal()) |
203 | { | |
dc409d37 | 204 | wxFAIL_MSG( "either wxDialog:EndModal called twice or ShowModal wasn't called" ); |
fb1585ae RR |
205 | return; |
206 | } | |
2b854a32 | 207 | |
91af0895 | 208 | m_modalShowing = false; |
2b854a32 | 209 | |
bfafa628 VZ |
210 | // Ensure Exit() is only called once. The dialog's event loop may be terminated |
211 | // externally due to an uncaught exception. | |
212 | if (m_modalLoop && m_modalLoop->IsRunning()) | |
213 | m_modalLoop->Exit(); | |
2b854a32 | 214 | |
91af0895 | 215 | Show( false ); |
c33c4050 | 216 | } |