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