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