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