]>
Commit | Line | Data |
---|---|---|
8b089c5e | 1 | ///////////////////////////////////////////////////////////////////////////// |
9b5f1895 | 2 | // Name: src/gtk/glcanvas.cpp |
77ffb593 | 3 | // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK |
8b089c5e JS |
4 | // Author: Robert Roebling |
5 | // Modified by: | |
6 | // Created: 17/08/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
65571936 | 9 | // Licence: wxWindows licence |
8b089c5e JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 VS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
8b089c5e JS |
15 | #if wxUSE_GLCANVAS |
16 | ||
17 | #include "wx/glcanvas.h" | |
18 | ||
a1abca32 PC |
19 | #include <gtk/gtk.h> |
20 | #include <gdk/gdkx.h> | |
86b8b3ac | 21 | #include "wx/gtk/private/gtk2-compat.h" |
8b089c5e | 22 | |
dc3065a5 VZ |
23 | #if WXWIN_COMPATIBILITY_2_8 |
24 | ||
8b089c5e | 25 | //----------------------------------------------------------------------------- |
dc3065a5 | 26 | // "realize" from m_wxwindow: used to create m_glContext implicitly |
8b089c5e JS |
27 | //----------------------------------------------------------------------------- |
28 | ||
865bb325 | 29 | extern "C" { |
144ae5bb | 30 | static void |
34a34b02 | 31 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) |
8b089c5e | 32 | { |
dc3065a5 | 33 | win->GTKInitImplicitContext(); |
8b089c5e | 34 | } |
865bb325 | 35 | } |
8b089c5e | 36 | |
dc3065a5 VZ |
37 | #endif // WXWIN_COMPATIBILITY_2_8 |
38 | ||
8b089c5e JS |
39 | //----------------------------------------------------------------------------- |
40 | // "map" from m_wxwindow | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
9dc44eff | 43 | #ifndef __WXGTK3__ |
865bb325 | 44 | extern "C" { |
144ae5bb | 45 | static void |
8b089c5e JS |
46 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) |
47 | { | |
dc3065a5 VZ |
48 | wxPaintEvent event( win->GetId() ); |
49 | event.SetEventObject( win ); | |
937013e0 | 50 | win->HandleWindowEvent( event ); |
8b089c5e | 51 | |
dc3065a5 VZ |
52 | win->m_exposed = false; |
53 | win->GetUpdateRegion().Clear(); | |
8b089c5e | 54 | } |
865bb325 | 55 | } |
9dc44eff | 56 | #endif |
8b089c5e JS |
57 | |
58 | //----------------------------------------------------------------------------- | |
59 | // "expose_event" of m_wxwindow | |
60 | //----------------------------------------------------------------------------- | |
61 | ||
865bb325 | 62 | extern "C" { |
9dc44eff PC |
63 | #ifdef __WXGTK3__ |
64 | static gboolean draw(GtkWidget*, cairo_t* cr, wxGLCanvas* win) | |
65 | { | |
66 | win->m_exposed = true; | |
67 | if (win->m_cairoPaintContext == NULL) | |
68 | { | |
69 | win->m_cairoPaintContext = cr; | |
70 | cairo_reference(cr); | |
71 | } | |
72 | double x1, y1, x2, y2; | |
73 | cairo_clip_extents(cr, &x1, &y1, &x2, &y2); | |
74 | win->GetUpdateRegion().Union(int(x1), int(y1), int(x2 - x1), int(y2 - y1)); | |
75 | return false; | |
76 | } | |
77 | #else | |
6d727f6c | 78 | static gboolean |
8b089c5e JS |
79 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) |
80 | { | |
670f9935 | 81 | win->m_exposed = true; |
8b089c5e JS |
82 | |
83 | win->GetUpdateRegion().Union( gdk_event->area.x, | |
84 | gdk_event->area.y, | |
85 | gdk_event->area.width, | |
86 | gdk_event->area.height ); | |
6d727f6c | 87 | return false; |
8b089c5e | 88 | } |
9dc44eff | 89 | #endif |
865bb325 | 90 | } |
8b089c5e | 91 | |
8b089c5e JS |
92 | //----------------------------------------------------------------------------- |
93 | // "size_allocate" of m_wxwindow | |
94 | //----------------------------------------------------------------------------- | |
95 | ||
865bb325 | 96 | extern "C" { |
2b5f62a0 | 97 | static void |
e0d1fd7f VZ |
98 | gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget), |
99 | GtkAllocation * WXUNUSED(alloc), | |
100 | wxGLCanvas *win) | |
8b089c5e | 101 | { |
8b089c5e JS |
102 | if (!win->m_hasVMT) |
103 | return; | |
104 | ||
105 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
106 | event.SetEventObject( win ); | |
937013e0 | 107 | win->HandleWindowEvent( event ); |
8b089c5e | 108 | } |
865bb325 | 109 | } |
8b089c5e | 110 | |
3780e252 PC |
111 | //----------------------------------------------------------------------------- |
112 | // emission hook for "parent-set" | |
113 | //----------------------------------------------------------------------------- | |
114 | ||
115 | extern "C" { | |
116 | static gboolean | |
117 | parent_set_hook(GSignalInvocationHint*, guint, const GValue* param_values, void* data) | |
118 | { | |
119 | wxGLCanvas* win = (wxGLCanvas*)data; | |
120 | if (g_value_peek_pointer(¶m_values[0]) == win->m_wxwindow) | |
121 | { | |
122 | const XVisualInfo* xvi = win->GetXVisualInfo(); | |
123 | GdkVisual* visual = gtk_widget_get_visual(win->m_wxwindow); | |
124 | if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid) | |
125 | { | |
126 | GdkScreen* screen = gtk_widget_get_screen(win->m_wxwindow); | |
127 | visual = gdk_x11_screen_lookup_visual(screen, xvi->visualid); | |
9dc44eff PC |
128 | #ifdef __WXGTK3__ |
129 | gtk_widget_set_visual(win->m_wxwindow, visual); | |
130 | #else | |
3780e252 PC |
131 | GdkColormap* colormap = gdk_colormap_new(visual, false); |
132 | gtk_widget_set_colormap(win->m_wxwindow, colormap); | |
133 | g_object_unref(colormap); | |
9dc44eff | 134 | #endif |
3780e252 PC |
135 | } |
136 | // remove hook | |
137 | return false; | |
138 | } | |
139 | return true; | |
140 | } | |
141 | } | |
142 | ||
8b089c5e JS |
143 | //--------------------------------------------------------------------------- |
144 | // wxGlCanvas | |
145 | //--------------------------------------------------------------------------- | |
146 | ||
4660d7e5 | 147 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
8b089c5e | 148 | |
dc3065a5 VZ |
149 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
150 | wxWindowID id, | |
151 | const int *attribList, | |
152 | const wxPoint& pos, | |
153 | const wxSize& size, | |
154 | long style, | |
155 | const wxString& name, | |
156 | const wxPalette& palette) | |
157 | #if WXWIN_COMPATIBILITY_2_8 | |
b7ea712c | 158 | : m_createImplicitContext(false) |
dc3065a5 | 159 | #endif |
b7ea712c | 160 | { |
dc3065a5 | 161 | Create(parent, id, pos, size, style, name, attribList, palette); |
b7ea712c RR |
162 | } |
163 | ||
dc3065a5 VZ |
164 | #if WXWIN_COMPATIBILITY_2_8 |
165 | ||
166 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
167 | wxWindowID id, | |
168 | const wxPoint& pos, | |
169 | const wxSize& size, | |
170 | long style, | |
171 | const wxString& name, | |
172 | const int *attribList, | |
173 | const wxPalette& palette) | |
b7ea712c | 174 | : m_createImplicitContext(true) |
8b089c5e | 175 | { |
21562ad2 VZ |
176 | m_sharedContext = NULL; |
177 | m_sharedContextOf = NULL; | |
178 | ||
dc3065a5 | 179 | Create(parent, id, pos, size, style, name, attribList, palette); |
8b089c5e JS |
180 | } |
181 | ||
dc3065a5 VZ |
182 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
183 | const wxGLContext *shared, | |
184 | wxWindowID id, | |
185 | const wxPoint& pos, | |
186 | const wxSize& size, | |
187 | long style, | |
188 | const wxString& name, | |
189 | const int *attribList, | |
190 | const wxPalette& palette) | |
b7ea712c | 191 | : m_createImplicitContext(true) |
2b5f62a0 | 192 | { |
5c33522f | 193 | m_sharedContext = const_cast<wxGLContext *>(shared); |
dc3065a5 VZ |
194 | |
195 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
196 | } |
197 | ||
dc3065a5 VZ |
198 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
199 | const wxGLCanvas *shared, | |
200 | wxWindowID id, | |
201 | const wxPoint& pos, const wxSize& size, | |
202 | long style, const wxString& name, | |
203 | const int *attribList, | |
204 | const wxPalette& palette ) | |
b7ea712c | 205 | : m_createImplicitContext(true) |
2b5f62a0 | 206 | { |
21562ad2 | 207 | m_sharedContext = NULL; |
5c33522f | 208 | m_sharedContextOf = const_cast<wxGLCanvas *>(shared); |
dc3065a5 VZ |
209 | |
210 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
211 | } |
212 | ||
dc3065a5 | 213 | #endif // WXWIN_COMPATIBILITY_2_8 |
2b5f62a0 | 214 | |
dc3065a5 VZ |
215 | bool wxGLCanvas::Create(wxWindow *parent, |
216 | wxWindowID id, | |
217 | const wxPoint& pos, | |
218 | const wxSize& size, | |
219 | long style, | |
220 | const wxString& name, | |
221 | const int *attribList, | |
9ce5cf09 | 222 | const wxPalette& palette) |
dc3065a5 | 223 | { |
9ce5cf09 | 224 | #if wxUSE_PALETTE |
9a83f860 | 225 | wxASSERT_MSG( !palette.IsOk(), wxT("palettes not supported") ); |
9ce5cf09 VZ |
226 | #endif // wxUSE_PALETTE |
227 | wxUnusedVar(palette); // Unused when wxDEBUG_LEVEL==0 | |
e0d1fd7f | 228 | |
670f9935 WS |
229 | m_exposed = false; |
230 | m_noExpose = true; | |
231 | m_nativeSizeEvent = true; | |
9dc44eff PC |
232 | #ifdef __WXGTK3__ |
233 | m_cairoPaintContext = NULL; | |
234 | m_backgroundStyle = wxBG_STYLE_PAINT; | |
235 | #endif | |
34a34b02 | 236 | |
498ace9e VZ |
237 | if ( !InitVisual(attribList) ) |
238 | return false; | |
2b5f62a0 | 239 | |
3780e252 PC |
240 | // watch for the "parent-set" signal on m_wxwindow so we can set colormap |
241 | // before m_wxwindow is realized (which will occur before | |
242 | // wxWindow::Create() returns if parent is already visible) | |
243 | unsigned sig_id = g_signal_lookup("parent-set", GTK_TYPE_WIDGET); | |
244 | g_signal_add_emission_hook(sig_id, 0, parent_set_hook, this, NULL); | |
2b5f62a0 | 245 | |
ff654490 | 246 | wxWindow::Create( parent, id, pos, size, style, name ); |
fee7a683 | 247 | |
144ae5bb | 248 | gtk_widget_set_double_buffered(m_wxwindow, false); |
2b5b9325 | 249 | |
dc3065a5 | 250 | #if WXWIN_COMPATIBILITY_2_8 |
b7ea712c | 251 | g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this); |
dc3065a5 | 252 | #endif // WXWIN_COMPATIBILITY_2_8 |
9dc44eff PC |
253 | #ifdef __WXGTK3__ |
254 | g_signal_connect(m_wxwindow, "draw", G_CALLBACK(draw), this); | |
255 | #else | |
b7ea712c RR |
256 | g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this); |
257 | g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this); | |
9dc44eff | 258 | #endif |
b7ea712c | 259 | g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this); |
a6f5aa49 | 260 | |
dc3065a5 | 261 | #if WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
262 | // if our parent window is already visible, we had been realized before we |
263 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
264 | // initialized yet and we have to do it now | |
fc9ab22a | 265 | if (gtk_widget_get_realized(m_wxwindow)) |
bc869971 | 266 | gtk_glwindow_realized_callback( m_wxwindow, this ); |
dc3065a5 | 267 | #endif // WXWIN_COMPATIBILITY_2_8 |
bc869971 | 268 | |
9dc44eff | 269 | #ifndef __WXGTK3__ |
fc9ab22a | 270 | if (gtk_widget_get_mapped(m_wxwindow)) |
bc869971 | 271 | gtk_glwindow_map_callback( m_wxwindow, this ); |
9dc44eff | 272 | #endif |
bc869971 | 273 | |
670f9935 | 274 | return true; |
a6f5aa49 VZ |
275 | } |
276 | ||
9dc44eff PC |
277 | bool wxGLCanvas::SetBackgroundStyle(wxBackgroundStyle /* style */) |
278 | { | |
279 | return false; | |
280 | } | |
281 | ||
498ace9e | 282 | Window wxGLCanvas::GetXWindow() const |
8b089c5e | 283 | { |
f089940f | 284 | GdkWindow* window = GTKGetDrawingWindow(); |
9dc44eff | 285 | return window ? GDK_WINDOW_XID(window) : 0; |
8b089c5e JS |
286 | } |
287 | ||
8b089c5e JS |
288 | void wxGLCanvas::OnInternalIdle() |
289 | { | |
dc3065a5 | 290 | if (m_exposed) |
8b089c5e | 291 | { |
9dc44eff PC |
292 | #ifdef __WXGTK3__ |
293 | GTKSendPaintEvents(m_cairoPaintContext); | |
294 | cairo_destroy(m_cairoPaintContext); | |
295 | m_cairoPaintContext = NULL; | |
296 | #else | |
8b089c5e JS |
297 | wxPaintEvent event( GetId() ); |
298 | event.SetEventObject( this ); | |
937013e0 | 299 | HandleWindowEvent( event ); |
9dc44eff | 300 | #endif |
8b089c5e | 301 | |
670f9935 | 302 | m_exposed = false; |
8b089c5e JS |
303 | GetUpdateRegion().Clear(); |
304 | } | |
2b5f62a0 | 305 | |
8b089c5e JS |
306 | wxWindow::OnInternalIdle(); |
307 | } | |
308 | ||
dc3065a5 VZ |
309 | #if WXWIN_COMPATIBILITY_2_8 |
310 | ||
311 | void wxGLCanvas::GTKInitImplicitContext() | |
312 | { | |
313 | if ( !m_glContext && m_createImplicitContext ) | |
314 | { | |
315 | wxGLContext *share = m_sharedContext; | |
316 | if ( !share && m_sharedContextOf ) | |
317 | share = m_sharedContextOf->m_glContext; | |
318 | ||
319 | m_glContext = new wxGLContext(this, share); | |
320 | } | |
321 | } | |
a6f5aa49 | 322 | |
dc3065a5 | 323 | #endif // WXWIN_COMPATIBILITY_2_8 |
a6f5aa49 | 324 | |
dc3065a5 | 325 | #endif // wxUSE_GLCANVAS |