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