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