]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dialog.cpp
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / gtk / dialog.cpp
CommitLineData
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 27wxDEFINE_TIED_SCOPED_PTR_TYPE(wxGUIEventLoop)
bfafa628
VZ
28
29
c801d85f
KB
30//-----------------------------------------------------------------------------
31// wxDialog
32//-----------------------------------------------------------------------------
33
68995f26 34void wxDialog::Init()
c801d85f 35{
bfafa628 36 m_modalLoop = NULL;
91af0895 37 m_modalShowing = false;
c33c4050 38}
c801d85f 39
2b854a32 40wxDialog::wxDialog( wxWindow *parent,
fb1585ae 41 wxWindowID id, const wxString &title,
2b854a32 42 const wxPoint &pos, const wxSize &size,
fb1585ae 43 long style, const wxString &name )
c801d85f 44{
68995f26
VZ
45 Init();
46
82c9f85c 47 (void)Create( parent, id, title, pos, size, style, name );
c33c4050 48}
c801d85f
KB
49
50bool wxDialog::Create( wxWindow *parent,
fb1585ae 51 wxWindowID id, const wxString &title,
2b854a32 52 const wxPoint &pos, const wxSize &size,
fb1585ae 53 long style, const wxString &name )
c801d85f 54{
21f4383a 55 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
2b854a32 56
82c9f85c
VZ
57 // all dialogs should have tab traversal enabled
58 style |= wxTAB_TRAVERSAL;
59
7d9f12f3 60 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
c33c4050 61}
c801d85f 62
debe6624 63bool wxDialog::Show( bool show )
c801d85f 64{
fb1585ae
RR
65 if (!show && IsModal())
66 {
de8113d9 67 EndModal( wxID_CANCEL );
fb1585ae 68 }
c801d85f 69
3aa8e4ea
JS
70 if (show && CanDoLayoutAdaptation())
71 DoLayoutAdaptation();
72
8fb09a08 73 bool ret = wxDialogBase::Show(show);
e146b8c8 74
a9efc294
VS
75 if (show)
76 InitDialog();
2b854a32 77
739730ca 78 return ret;
c33c4050
RR
79}
80
a9efc294
VS
81wxDialog::~wxDialog()
82{
a9efc294 83 // if the dialog is modal, this will end its event loop
0af07cc2
VS
84 if ( IsModal() )
85 EndModal(wxID_CANCEL);
a9efc294
VS
86}
87
43a18898 88bool wxDialog::IsModal() const
e1e955e1 89{
fb1585ae 90 return m_modalShowing;
e1e955e1
RR
91}
92
4ea2d0d5
PC
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)
106extern "C" {
107static gboolean
108realize_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
43a18898 131int wxDialog::ShowModal()
c801d85f 132{
691745ab 133 WX_HOOK_MODAL_DIALOG();
643e9cf9 134
4e2a3778 135 wxASSERT_MSG( !IsModal(), "ShowModal() can't be called twice" );
e146b8c8 136
7738af59
VZ
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
cdc48273 144 wxWindow * const parent = GetParentForModalDialog();
8bda0ec6 145 if ( parent )
f6bcfd97 146 {
8bda0ec6
VZ
147 gtk_window_set_transient_for( GTK_WINDOW(m_widget),
148 GTK_WINDOW(parent->m_widget) );
f6bcfd97
BP
149 }
150
eebe4016 151 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
91af0895 152
4ea2d0d5
PC
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
91af0895 166 Show( true );
2b854a32 167
91af0895 168 m_modalShowing = true;
2b854a32 169
d3bd8b1a 170 wxOpenModalDialogLocker modalLock;
304e5625 171
f36630af
VZ
172 // NOTE: gtk_window_set_modal internally calls gtk_grab_add() !
173 gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE);
924b84ab 174
bfafa628
VZ
175 // Run modal dialog event loop.
176 {
177 wxGUIEventLoopTiedPtr modal(&m_modalLoop, new wxGUIEventLoop());
178 m_modalLoop->Run();
179 }
924b84ab 180
4ea2d0d5
PC
181#if GTK_CHECK_VERSION(2,10,0)
182 if (sigId)
183 g_signal_remove_emission_hook(sigId, hookId);
184#endif
185
f36630af 186 gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE);
2b854a32 187
fb1585ae 188 return GetReturnCode();
c33c4050 189}
c801d85f
KB
190
191void wxDialog::EndModal( int retCode )
192{
fb1585ae 193 SetReturnCode( retCode );
2b854a32 194
fb1585ae
RR
195 if (!IsModal())
196 {
dc409d37 197 wxFAIL_MSG( "either wxDialog:EndModal called twice or ShowModal wasn't called" );
fb1585ae
RR
198 return;
199 }
2b854a32 200
91af0895 201 m_modalShowing = false;
2b854a32 202
bfafa628
VZ
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();
2b854a32 207
91af0895 208 Show( false );
c33c4050 209}