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