1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/glcanvas.h"
21 #include "wx/gtk/private/gtk2-compat.h"
23 #if WXWIN_COMPATIBILITY_2_8
25 //-----------------------------------------------------------------------------
26 // "realize" from m_wxwindow: used to create m_glContext implicitly
27 //-----------------------------------------------------------------------------
31 gtk_glwindow_realized_callback( GtkWidget
*WXUNUSED(widget
), wxGLCanvas
*win
)
33 win
->GTKInitImplicitContext();
37 #endif // WXWIN_COMPATIBILITY_2_8
39 //-----------------------------------------------------------------------------
40 // "map" from m_wxwindow
41 //-----------------------------------------------------------------------------
45 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
47 wxPaintEvent
event( win
->GetId() );
48 event
.SetEventObject( win
);
49 win
->HandleWindowEvent( event
);
51 win
->m_exposed
= false;
52 win
->GetUpdateRegion().Clear();
56 //-----------------------------------------------------------------------------
57 // "expose_event" of m_wxwindow
58 //-----------------------------------------------------------------------------
62 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
64 win
->m_exposed
= true;
66 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
68 gdk_event
->area
.width
,
69 gdk_event
->area
.height
);
74 //-----------------------------------------------------------------------------
75 // "size_allocate" of m_wxwindow
76 //-----------------------------------------------------------------------------
80 gtk_glcanvas_size_callback(GtkWidget
*WXUNUSED(widget
),
81 GtkAllocation
* WXUNUSED(alloc
),
87 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
88 event
.SetEventObject( win
);
89 win
->HandleWindowEvent( event
);
93 //-----------------------------------------------------------------------------
94 // emission hook for "parent-set"
95 //-----------------------------------------------------------------------------
99 parent_set_hook(GSignalInvocationHint
*, guint
, const GValue
* param_values
, void* data
)
101 wxGLCanvas
* win
= (wxGLCanvas
*)data
;
102 if (g_value_peek_pointer(¶m_values
[0]) == win
->m_wxwindow
)
104 const XVisualInfo
* xvi
= win
->GetXVisualInfo();
105 GdkVisual
* visual
= gtk_widget_get_visual(win
->m_wxwindow
);
106 if (GDK_VISUAL_XVISUAL(visual
)->visualid
!= xvi
->visualid
)
108 GdkScreen
* screen
= gtk_widget_get_screen(win
->m_wxwindow
);
109 visual
= gdk_x11_screen_lookup_visual(screen
, xvi
->visualid
);
110 GdkColormap
* colormap
= gdk_colormap_new(visual
, false);
111 gtk_widget_set_colormap(win
->m_wxwindow
, colormap
);
112 g_object_unref(colormap
);
121 //---------------------------------------------------------------------------
123 //---------------------------------------------------------------------------
125 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
127 wxGLCanvas::wxGLCanvas(wxWindow
*parent
,
129 const int *attribList
,
133 const wxString
& name
,
134 const wxPalette
& palette
)
135 #if WXWIN_COMPATIBILITY_2_8
136 : m_createImplicitContext(false)
139 Create(parent
, id
, pos
, size
, style
, name
, attribList
, palette
);
142 #if WXWIN_COMPATIBILITY_2_8
144 wxGLCanvas::wxGLCanvas(wxWindow
*parent
,
149 const wxString
& name
,
150 const int *attribList
,
151 const wxPalette
& palette
)
152 : m_createImplicitContext(true)
154 m_sharedContext
= NULL
;
155 m_sharedContextOf
= NULL
;
157 Create(parent
, id
, pos
, size
, style
, name
, attribList
, palette
);
160 wxGLCanvas::wxGLCanvas(wxWindow
*parent
,
161 const wxGLContext
*shared
,
166 const wxString
& name
,
167 const int *attribList
,
168 const wxPalette
& palette
)
169 : m_createImplicitContext(true)
171 m_sharedContext
= const_cast<wxGLContext
*>(shared
);
173 Create(parent
, id
, pos
, size
, style
, name
, attribList
, palette
);
176 wxGLCanvas::wxGLCanvas(wxWindow
*parent
,
177 const wxGLCanvas
*shared
,
179 const wxPoint
& pos
, const wxSize
& size
,
180 long style
, const wxString
& name
,
181 const int *attribList
,
182 const wxPalette
& palette
)
183 : m_createImplicitContext(true)
185 m_sharedContext
= NULL
;
186 m_sharedContextOf
= const_cast<wxGLCanvas
*>(shared
);
188 Create(parent
, id
, pos
, size
, style
, name
, attribList
, palette
);
191 #endif // WXWIN_COMPATIBILITY_2_8
193 bool wxGLCanvas::Create(wxWindow
*parent
,
198 const wxString
& name
,
199 const int *attribList
,
200 const wxPalette
& palette
)
203 wxASSERT_MSG( !palette
.IsOk(), wxT("palettes not supported") );
204 #endif // wxUSE_PALETTE
205 wxUnusedVar(palette
); // Unused when wxDEBUG_LEVEL==0
209 m_nativeSizeEvent
= true;
211 if ( !InitVisual(attribList
) )
214 // watch for the "parent-set" signal on m_wxwindow so we can set colormap
215 // before m_wxwindow is realized (which will occur before
216 // wxWindow::Create() returns if parent is already visible)
217 unsigned sig_id
= g_signal_lookup("parent-set", GTK_TYPE_WIDGET
);
218 g_signal_add_emission_hook(sig_id
, 0, parent_set_hook
, this, NULL
);
220 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
222 gtk_widget_set_double_buffered(m_wxwindow
, false);
224 #if WXWIN_COMPATIBILITY_2_8
225 g_signal_connect(m_wxwindow
, "realize", G_CALLBACK(gtk_glwindow_realized_callback
), this);
226 #endif // WXWIN_COMPATIBILITY_2_8
227 g_signal_connect(m_wxwindow
, "map", G_CALLBACK(gtk_glwindow_map_callback
), this);
228 g_signal_connect(m_wxwindow
, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback
), this);
229 g_signal_connect(m_widget
, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback
), this);
231 #if WXWIN_COMPATIBILITY_2_8
232 // if our parent window is already visible, we had been realized before we
233 // connected to the "realize" signal and hence our m_glContext hasn't been
234 // initialized yet and we have to do it now
235 if (gtk_widget_get_realized(m_wxwindow
))
236 gtk_glwindow_realized_callback( m_wxwindow
, this );
237 #endif // WXWIN_COMPATIBILITY_2_8
239 if (gtk_widget_get_mapped(m_wxwindow
))
240 gtk_glwindow_map_callback( m_wxwindow
, this );
245 Window
wxGLCanvas::GetXWindow() const
247 GdkWindow
* window
= GTKGetDrawingWindow();
248 return window
? GDK_WINDOW_XWINDOW(window
) : 0;
251 void wxGLCanvas::OnInternalIdle()
255 wxPaintEvent
event( GetId() );
256 event
.SetEventObject( this );
257 HandleWindowEvent( event
);
260 GetUpdateRegion().Clear();
263 wxWindow::OnInternalIdle();
266 #if WXWIN_COMPATIBILITY_2_8
268 void wxGLCanvas::GTKInitImplicitContext()
270 if ( !m_glContext
&& m_createImplicitContext
)
272 wxGLContext
*share
= m_sharedContext
;
273 if ( !share
&& m_sharedContextOf
)
274 share
= m_sharedContextOf
->m_glContext
;
276 m_glContext
= new wxGLContext(this, share
);
280 #endif // WXWIN_COMPATIBILITY_2_8
282 #endif // wxUSE_GLCANVAS