fix for GTK assertion from gtk_widget_set_colormap, bug 1882040
[wxWidgets.git] / src / gtk / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 17/08/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_GLCANVAS
16
17 #include "wx/glcanvas.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/app.h"
21 #include "wx/frame.h"
22 #include "wx/colour.h"
23 #include "wx/module.h"
24 #endif // WX_PRECOMP
25
26 #include <gtk/gtk.h>
27 #include <gdk/gdkx.h>
28
29 #if WXWIN_COMPATIBILITY_2_8
30
31 //-----------------------------------------------------------------------------
32 // "realize" from m_wxwindow: used to create m_glContext implicitly
33 //-----------------------------------------------------------------------------
34
35 extern "C" {
36 static gint
37 gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
38 {
39 win->GTKInitImplicitContext();
40
41 return FALSE;
42 }
43 }
44
45 #endif // WXWIN_COMPATIBILITY_2_8
46
47 //-----------------------------------------------------------------------------
48 // "map" from m_wxwindow
49 //-----------------------------------------------------------------------------
50
51 extern "C" {
52 static gint
53 gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
54 {
55 wxPaintEvent event( win->GetId() );
56 event.SetEventObject( win );
57 win->HandleWindowEvent( event );
58
59 win->m_exposed = false;
60 win->GetUpdateRegion().Clear();
61
62 return FALSE;
63 }
64 }
65
66 //-----------------------------------------------------------------------------
67 // "expose_event" of m_wxwindow
68 //-----------------------------------------------------------------------------
69
70 extern "C" {
71 static gboolean
72 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
73 {
74 win->m_exposed = true;
75
76 win->GetUpdateRegion().Union( gdk_event->area.x,
77 gdk_event->area.y,
78 gdk_event->area.width,
79 gdk_event->area.height );
80 return false;
81 }
82 }
83
84 //-----------------------------------------------------------------------------
85 // "size_allocate" of m_wxwindow
86 //-----------------------------------------------------------------------------
87
88 extern "C" {
89 static void
90 gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget),
91 GtkAllocation * WXUNUSED(alloc),
92 wxGLCanvas *win)
93 {
94 if (!win->m_hasVMT)
95 return;
96
97 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
98 event.SetEventObject( win );
99 win->HandleWindowEvent( event );
100 }
101 }
102
103 //-----------------------------------------------------------------------------
104 // emission hook for "parent-set"
105 //-----------------------------------------------------------------------------
106
107 extern "C" {
108 static gboolean
109 parent_set_hook(GSignalInvocationHint*, guint, const GValue* param_values, void* data)
110 {
111 wxGLCanvas* win = (wxGLCanvas*)data;
112 if (g_value_peek_pointer(&param_values[0]) == win->m_wxwindow)
113 {
114 const XVisualInfo* xvi = win->GetXVisualInfo();
115 GdkVisual* visual = gtk_widget_get_visual(win->m_wxwindow);
116 if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid)
117 {
118 GdkScreen* screen = gtk_widget_get_screen(win->m_wxwindow);
119 visual = gdk_x11_screen_lookup_visual(screen, xvi->visualid);
120 GdkColormap* colormap = gdk_colormap_new(visual, false);
121 gtk_widget_set_colormap(win->m_wxwindow, colormap);
122 g_object_unref(colormap);
123 }
124 // remove hook
125 return false;
126 }
127 return true;
128 }
129 }
130
131 //---------------------------------------------------------------------------
132 // wxGlCanvas
133 //---------------------------------------------------------------------------
134
135 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
136
137 wxGLCanvas::wxGLCanvas(wxWindow *parent,
138 wxWindowID id,
139 const int *attribList,
140 const wxPoint& pos,
141 const wxSize& size,
142 long style,
143 const wxString& name,
144 const wxPalette& palette)
145 #if WXWIN_COMPATIBILITY_2_8
146 : m_createImplicitContext(false)
147 #endif
148 {
149 Create(parent, id, pos, size, style, name, attribList, palette);
150 }
151
152 #if WXWIN_COMPATIBILITY_2_8
153
154 wxGLCanvas::wxGLCanvas(wxWindow *parent,
155 wxWindowID id,
156 const wxPoint& pos,
157 const wxSize& size,
158 long style,
159 const wxString& name,
160 const int *attribList,
161 const wxPalette& palette)
162 : m_createImplicitContext(true)
163 {
164 Create(parent, id, pos, size, style, name, attribList, palette);
165 }
166
167 wxGLCanvas::wxGLCanvas(wxWindow *parent,
168 const wxGLContext *shared,
169 wxWindowID id,
170 const wxPoint& pos,
171 const wxSize& size,
172 long style,
173 const wxString& name,
174 const int *attribList,
175 const wxPalette& palette)
176 : m_createImplicitContext(true)
177 {
178 m_sharedContext = wx_const_cast(wxGLContext *, shared);
179
180 Create(parent, id, pos, size, style, name, attribList, palette);
181 }
182
183 wxGLCanvas::wxGLCanvas(wxWindow *parent,
184 const wxGLCanvas *shared,
185 wxWindowID id,
186 const wxPoint& pos, const wxSize& size,
187 long style, const wxString& name,
188 const int *attribList,
189 const wxPalette& palette )
190 : m_createImplicitContext(true)
191 {
192 m_sharedContextOf = wx_const_cast(wxGLCanvas *, shared);
193
194 Create(parent, id, pos, size, style, name, attribList, palette);
195 }
196
197 #endif // WXWIN_COMPATIBILITY_2_8
198
199 bool wxGLCanvas::Create(wxWindow *parent,
200 wxWindowID id,
201 const wxPoint& pos,
202 const wxSize& size,
203 long style,
204 const wxString& name,
205 const int *attribList,
206 const wxPalette& WXUNUSED_UNLESS_DEBUG(palette))
207 {
208 wxASSERT_MSG( !palette.IsOk(), _T("palettes not supported") );
209
210 m_exposed = false;
211 m_noExpose = true;
212 m_nativeSizeEvent = true;
213
214 if ( !InitVisual(attribList) )
215 return false;
216
217 // watch for the "parent-set" signal on m_wxwindow so we can set colormap
218 // before m_wxwindow is realized (which will occur before
219 // wxWindow::Create() returns if parent is already visible)
220 unsigned sig_id = g_signal_lookup("parent-set", GTK_TYPE_WIDGET);
221 g_signal_add_emission_hook(sig_id, 0, parent_set_hook, this, NULL);
222
223 wxWindow::Create( parent, id, pos, size, style, name );
224
225 m_glWidget = m_wxwindow;
226
227 gtk_widget_set_double_buffered( m_glWidget, FALSE );
228
229 #if WXWIN_COMPATIBILITY_2_8
230 g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this);
231 #endif // WXWIN_COMPATIBILITY_2_8
232 g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this);
233 g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this);
234 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this);
235
236 #if WXWIN_COMPATIBILITY_2_8
237 // if our parent window is already visible, we had been realized before we
238 // connected to the "realize" signal and hence our m_glContext hasn't been
239 // initialized yet and we have to do it now
240 if (GTK_WIDGET_REALIZED(m_wxwindow))
241 gtk_glwindow_realized_callback( m_wxwindow, this );
242 #endif // WXWIN_COMPATIBILITY_2_8
243
244 if (GTK_WIDGET_MAPPED(m_wxwindow))
245 gtk_glwindow_map_callback( m_wxwindow, this );
246
247 return true;
248 }
249
250 Window wxGLCanvas::GetXWindow() const
251 {
252 GdkWindow *window = m_wxwindow->window;
253 return window ? GDK_WINDOW_XWINDOW(window) : 0;
254 }
255
256 void wxGLCanvas::OnInternalIdle()
257 {
258 if (m_exposed)
259 {
260 wxPaintEvent event( GetId() );
261 event.SetEventObject( this );
262 HandleWindowEvent( event );
263
264 m_exposed = false;
265 GetUpdateRegion().Clear();
266 }
267
268 wxWindow::OnInternalIdle();
269 }
270
271 #if WXWIN_COMPATIBILITY_2_8
272
273 void wxGLCanvas::GTKInitImplicitContext()
274 {
275 if ( !m_glContext && m_createImplicitContext )
276 {
277 wxGLContext *share = m_sharedContext;
278 if ( !share && m_sharedContextOf )
279 share = m_sharedContextOf->m_glContext;
280
281 m_glContext = new wxGLContext(this, share);
282 }
283 }
284
285 #endif // WXWIN_COMPATIBILITY_2_8
286
287 #endif // wxUSE_GLCANVAS