]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dialog.cpp
preserve checkbox status in SetString()
[wxWidgets.git] / src / gtk / dialog.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/gtk/dialog.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/dialog.h"
670f9935
WS
14
15#ifndef WX_PRECOMP
c8326d64 16 #include "wx/cursor.h"
670f9935
WS
17#endif // WX_PRECOMP
18
924b84ab 19#include "wx/evtloop.h"
83624f79 20
664e1314 21#include "wx/scopedptr.h"
691745ab 22#include "wx/modalhook.h"
bfafa628 23
071a2d78 24#include <gtk/gtk.h>
4ea2d0d5 25#include "wx/gtk/private/gtk2-compat.h"
d3bd8b1a 26#include "wx/gtk/private/dialogcount.h"
acfd422a 27
5c69ef61 28wxDEFINE_TIED_SCOPED_PTR_TYPE(wxGUIEventLoop)
bfafa628
VZ
29
30
c801d85f
KB
31//-----------------------------------------------------------------------------
32// wxDialog
33//-----------------------------------------------------------------------------
34
68995f26 35void wxDialog::Init()
c801d85f 36{
bfafa628 37 m_modalLoop = NULL;
f03fc89f 38 m_returnCode = 0;
91af0895
WS
39 m_modalShowing = false;
40 m_themeEnabled = true;
c33c4050 41}
c801d85f 42
2b854a32 43wxDialog::wxDialog( wxWindow *parent,
fb1585ae 44 wxWindowID id, const wxString &title,
2b854a32 45 const wxPoint &pos, const wxSize &size,
fb1585ae 46 long style, const wxString &name )
c801d85f 47{
68995f26
VZ
48 Init();
49
82c9f85c 50 (void)Create( parent, id, title, pos, size, style, name );
c33c4050 51}
c801d85f
KB
52
53bool wxDialog::Create( wxWindow *parent,
fb1585ae 54 wxWindowID id, const wxString &title,
2b854a32 55 const wxPoint &pos, const wxSize &size,
fb1585ae 56 long style, const wxString &name )
c801d85f 57{
21f4383a 58 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
2b854a32 59
82c9f85c
VZ
60 // all dialogs should have tab traversal enabled
61 style |= wxTAB_TRAVERSAL;
62
7d9f12f3 63 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
c33c4050 64}
c801d85f 65
debe6624 66bool wxDialog::Show( bool show )
c801d85f 67{
fb1585ae
RR
68 if (!show && IsModal())
69 {
de8113d9 70 EndModal( wxID_CANCEL );
fb1585ae 71 }
c801d85f 72
3aa8e4ea
JS
73 if (show && CanDoLayoutAdaptation())
74 DoLayoutAdaptation();
75
8fb09a08 76 bool ret = wxDialogBase::Show(show);
e146b8c8 77
a9efc294
VS
78 if (show)
79 InitDialog();
2b854a32 80
739730ca 81 return ret;
c33c4050
RR
82}
83
a9efc294
VS
84wxDialog::~wxDialog()
85{
a9efc294 86 // if the dialog is modal, this will end its event loop
0af07cc2
VS
87 if ( IsModal() )
88 EndModal(wxID_CANCEL);
a9efc294
VS
89}
90
43a18898 91bool wxDialog::IsModal() const
e1e955e1 92{
fb1585ae 93 return m_modalShowing;
e1e955e1
RR
94}
95
4ea2d0d5
PC
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)
109extern "C" {
110static gboolean
111realize_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
43a18898 134int wxDialog::ShowModal()
c801d85f 135{
691745ab 136 WX_HOOK_MODAL_DIALOG();
643e9cf9 137
4e2a3778 138 wxASSERT_MSG( !IsModal(), "ShowModal() can't be called twice" );
e146b8c8 139
7738af59
VZ
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
cdc48273 147 wxWindow * const parent = GetParentForModalDialog();
8bda0ec6 148 if ( parent )
f6bcfd97 149 {
8bda0ec6
VZ
150 gtk_window_set_transient_for( GTK_WINDOW(m_widget),
151 GTK_WINDOW(parent->m_widget) );
f6bcfd97
BP
152 }
153
eebe4016 154 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
91af0895 155
4ea2d0d5
PC
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
91af0895 169 Show( true );
2b854a32 170
91af0895 171 m_modalShowing = true;
2b854a32 172
d3bd8b1a 173 wxOpenModalDialogLocker modalLock;
304e5625 174
f36630af
VZ
175 // NOTE: gtk_window_set_modal internally calls gtk_grab_add() !
176 gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE);
924b84ab 177
bfafa628
VZ
178 // Run modal dialog event loop.
179 {
180 wxGUIEventLoopTiedPtr modal(&m_modalLoop, new wxGUIEventLoop());
181 m_modalLoop->Run();
182 }
924b84ab 183
4ea2d0d5
PC
184#if GTK_CHECK_VERSION(2,10,0)
185 if (sigId)
186 g_signal_remove_emission_hook(sigId, hookId);
187#endif
188
f36630af 189 gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE);
2b854a32 190
fb1585ae 191 return GetReturnCode();
c33c4050 192}
c801d85f
KB
193
194void wxDialog::EndModal( int retCode )
195{
fb1585ae 196 SetReturnCode( retCode );
2b854a32 197
fb1585ae
RR
198 if (!IsModal())
199 {
dc409d37 200 wxFAIL_MSG( "either wxDialog:EndModal called twice or ShowModal wasn't called" );
fb1585ae
RR
201 return;
202 }
2b854a32 203
91af0895 204 m_modalShowing = false;
2b854a32 205
bfafa628
VZ
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();
2b854a32 210
91af0895 211 Show( false );
c33c4050 212}