]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/glcanvas.cpp
Fix my last fix. The method is pure virtual in the base class so it
[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 extern "C"
27 {
28 #include "gtk/gtk.h"
29 #include "gdk/gdk.h"
30 #include "gdk/gdkx.h"
31 }
32
33 #include "wx/gtk/win_gtk.h"
34 #include "wx/gtk/private.h"
35
36 // DLL options compatibility check:
37 #include "wx/build.h"
38 WX_CHECK_BUILD_OPTIONS("wxGL")
39
40
41 //---------------------------------------------------------------------------
42 // static variables
43 //---------------------------------------------------------------------------
44 int wxGLCanvas::m_glxVersion = 0;
45
46 //---------------------------------------------------------------------------
47 // global data
48 //---------------------------------------------------------------------------
49
50 XVisualInfo *g_vi = (XVisualInfo*) NULL;
51
52 //---------------------------------------------------------------------------
53 // wxGLContext
54 //---------------------------------------------------------------------------
55
56 IMPLEMENT_CLASS(wxGLContext,wxObject)
57
58 wxGLContext::wxGLContext(wxWindow* win, const wxGLContext* other)
59 {
60 wxGLCanvas *gc = (wxGLCanvas*) win;
61
62 if (wxGLCanvas::GetGLXVersion() >= 13)
63 {
64 // GLX >= 1.3
65 GLXFBConfig *fbc = gc->m_fbc;
66 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") );
67 m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE,
68 other ? other->m_glContext : None,
69 GL_TRUE );
70 }
71 else
72 {
73 // GLX <= 1.2
74 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
75 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
76 m_glContext = glXCreateContext( GDK_DISPLAY(), vi,
77 other ? other->m_glContext : None,
78 GL_TRUE );
79 }
80
81 if ( !m_glContext )
82 {
83 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
84 }
85 }
86
87 wxGLContext::~wxGLContext()
88 {
89 if (!m_glContext) return;
90
91 if (m_glContext == glXGetCurrentContext())
92 {
93 if (wxGLCanvas::GetGLXVersion() >= 13)
94 // GLX >= 1.3
95 glXMakeContextCurrent( GDK_DISPLAY(), None, None, NULL);
96 else
97 // GLX <= 1.2
98 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
99 }
100
101 glXDestroyContext( GDK_DISPLAY(), m_glContext );
102 }
103
104 void wxGLContext::SetCurrent(const wxGLCanvas& win) const
105 {
106 if (m_glContext)
107 {
108 GdkWindow *window = GTK_PIZZA(win.m_wxwindow)->bin_window;
109
110 if (wxGLCanvas::GetGLXVersion() >= 13)
111 // GLX >= 1.3
112 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), GDK_WINDOW_XWINDOW(window), m_glContext );
113 else
114 // GLX <= 1.2
115 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
116 }
117 }
118
119
120 //-----------------------------------------------------------------------------
121 // "realize" from m_wxwindow
122 //-----------------------------------------------------------------------------
123
124 extern "C" {
125 static gint
126 gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
127 {
128 if (!win->m_glContext && win->m_createImplicitContext)
129 {
130 wxGLContext *share = win->m_sharedContext;
131 if ( !share && win->m_sharedContextOf )
132 share = win->m_sharedContextOf->GetContext();
133
134 win->m_glContext = new wxGLContext(win, share);
135 }
136
137 return FALSE;
138 }
139 }
140
141 //-----------------------------------------------------------------------------
142 // "map" from m_wxwindow
143 //-----------------------------------------------------------------------------
144
145 extern "C" {
146 static gint
147 gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
148 {
149 // CF: Can the "if" line be removed, and the code unconditionally (always) be run?
150 if (win->m_glContext || !win->m_createImplicitContext)
151 {
152 wxPaintEvent event( win->GetId() );
153 event.SetEventObject( win );
154 win->GetEventHandler()->ProcessEvent( event );
155
156 win->m_exposed = false;
157 win->GetUpdateRegion().Clear();
158 }
159
160 return FALSE;
161 }
162 }
163
164 //-----------------------------------------------------------------------------
165 // "expose_event" of m_wxwindow
166 //-----------------------------------------------------------------------------
167
168 extern "C" {
169 static void
170 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
171 {
172 // don't need to install idle handler, its done from "event" signal
173
174 win->m_exposed = true;
175
176 win->GetUpdateRegion().Union( gdk_event->area.x,
177 gdk_event->area.y,
178 gdk_event->area.width,
179 gdk_event->area.height );
180 }
181 }
182
183 //-----------------------------------------------------------------------------
184 // "size_allocate" of m_wxwindow
185 //-----------------------------------------------------------------------------
186
187 extern "C" {
188 static void
189 gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
190 {
191 if (g_isIdle)
192 wxapp_install_idle_handler();
193
194 if (!win->m_hasVMT)
195 return;
196
197 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
198 event.SetEventObject( win );
199 win->GetEventHandler()->ProcessEvent( event );
200 }
201 }
202
203 //---------------------------------------------------------------------------
204 // wxGlCanvas
205 //---------------------------------------------------------------------------
206
207 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
208
209 BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
210 EVT_SIZE(wxGLCanvas::OnSize)
211 END_EVENT_TABLE()
212
213 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
214 int *attribList,
215 const wxPoint& pos, const wxSize& size,
216 long style, const wxString& name,
217 const wxPalette& palette )
218 : m_createImplicitContext(false)
219 {
220 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
221 }
222
223 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
224 const wxPoint& pos, const wxSize& size,
225 long style, const wxString& name,
226 int *attribList,
227 const wxPalette& palette )
228 : m_createImplicitContext(true)
229 {
230 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
231 }
232
233 wxGLCanvas::wxGLCanvas( wxWindow *parent,
234 const wxGLContext *shared,
235 wxWindowID id,
236 const wxPoint& pos, const wxSize& size,
237 long style, const wxString& name,
238 int *attribList,
239 const wxPalette& palette )
240 : m_createImplicitContext(true)
241 {
242 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
243 }
244
245 wxGLCanvas::wxGLCanvas( wxWindow *parent,
246 const wxGLCanvas *shared,
247 wxWindowID id,
248 const wxPoint& pos, const wxSize& size,
249 long style, const wxString& name,
250 int *attribList,
251 const wxPalette& palette )
252 : m_createImplicitContext(true)
253 {
254 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
255 }
256
257 bool wxGLCanvas::Create( wxWindow *parent,
258 const wxGLContext *shared,
259 const wxGLCanvas *shared_context_of,
260 wxWindowID id,
261 const wxPoint& pos, const wxSize& size,
262 long style, const wxString& name,
263 int *attribList,
264 const wxPalette& palette)
265 {
266 m_sharedContext = (wxGLContext*)shared; // const_cast
267 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
268 m_glContext = (wxGLContext*) NULL;
269
270 m_exposed = false;
271 m_noExpose = true;
272 m_nativeSizeEvent = true;
273 m_fbc = NULL;
274 m_vi = NULL;
275
276 // to be sure the glx version is known
277 wxGLCanvas::QueryGLXVersion();
278
279 if (wxGLCanvas::GetGLXVersion() >= 13)
280 {
281 // GLX >= 1.3 uses a GLXFBConfig
282 GLXFBConfig * fbc = NULL;
283 if (wxTheApp->m_glFBCInfo != NULL)
284 {
285 fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo;
286 m_canFreeFBC = false; // owned by wxTheApp - don't free upon destruction
287 }
288 else
289 {
290 fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList);
291 m_canFreeFBC = true;
292 }
293 m_fbc = fbc; // save for later use
294 wxCHECK_MSG( m_fbc, false, _T("required FBConfig couldn't be found") );
295 }
296
297 XVisualInfo *vi = NULL;
298 if (wxTheApp->m_glVisualInfo != NULL)
299 {
300 vi = (XVisualInfo *)wxTheApp->m_glVisualInfo;
301 m_canFreeVi = false; // owned by wxTheApp - don't free upon destruction
302 }
303 else
304 {
305 if (wxGLCanvas::GetGLXVersion() >= 13)
306 // GLX >= 1.3
307 vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]);
308 else
309 // GLX <= 1.2
310 vi = (XVisualInfo *) ChooseGLVisual(attribList);
311
312 m_canFreeVi = true;
313 }
314
315 m_vi = vi; // save for later use
316
317 wxCHECK_MSG( m_vi, false, _T("required visual couldn't be found") );
318 GdkVisual *visual;
319 GdkColormap *colormap;
320
321 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
322 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
323 if (!gtk_check_version(2,2,0))
324 {
325 wxWindow::Create( parent, id, pos, size, style, name );
326
327 m_glWidget = m_wxwindow;
328
329 GdkScreen *screen = gtk_widget_get_screen( m_glWidget );
330 colormap = gdk_screen_get_default_colormap(screen);
331 visual = gdk_colormap_get_visual(colormap);
332
333 if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid)
334 {
335 visual = gdk_x11_screen_lookup_visual( screen, vi->visualid );
336 colormap = gdk_colormap_new(visual, FALSE);
337 }
338
339 gtk_widget_set_colormap( m_glWidget, colormap );
340 }
341 else
342 #endif
343 {
344 visual = gdkx_visual_get( vi->visualid );
345 colormap = gdk_colormap_new( visual, TRUE );
346
347 gtk_widget_push_colormap( colormap );
348
349 wxWindow::Create( parent, id, pos, size, style, name );
350 m_glWidget = m_wxwindow;
351 }
352
353 gtk_widget_set_double_buffered( m_glWidget, FALSE );
354
355 g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this);
356 g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this);
357 g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this);
358 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this);
359
360 if (gtk_check_version(2,2,0) != NULL)
361 {
362 gtk_widget_pop_colormap();
363 }
364
365 // if our parent window is already visible, we had been realized before we
366 // connected to the "realize" signal and hence our m_glContext hasn't been
367 // initialized yet and we have to do it now
368 if (GTK_WIDGET_REALIZED(m_wxwindow))
369 gtk_glwindow_realized_callback( m_wxwindow, this );
370
371 if (GTK_WIDGET_MAPPED(m_wxwindow))
372 gtk_glwindow_map_callback( m_wxwindow, this );
373
374 return true;
375 }
376
377 wxGLCanvas::~wxGLCanvas()
378 {
379 GLXFBConfig * fbc = (GLXFBConfig *) m_fbc;
380 if (fbc && m_canFreeFBC)
381 XFree( fbc );
382
383 XVisualInfo *vi = (XVisualInfo *) m_vi;
384 if (vi && m_canFreeVi)
385 XFree( vi );
386
387 delete m_glContext;
388 }
389
390 void* wxGLCanvas::ChooseGLVisual(int *attribList)
391 {
392 int data[512];
393 GetGLAttribListFromWX( attribList, data );
394 attribList = (int*) data;
395
396 Display *dpy = GDK_DISPLAY();
397
398 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
399 }
400
401 void* wxGLCanvas::ChooseGLFBC(int *attribList)
402 {
403 int data[512];
404 GetGLAttribListFromWX( attribList, data );
405 attribList = (int*) data;
406
407 int returned;
408 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
409 attribList, &returned );
410 }
411
412
413 void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList )
414 {
415 if (!wx_attribList)
416 {
417 if (wxGLCanvas::GetGLXVersion() >= 13)
418 // leave GLX >= 1.3 choose the default attributes
419 gl_attribList[0] = 0;
420 else
421 {
422 int i = 0;
423 // default settings if attriblist = 0
424 gl_attribList[i++] = GLX_RGBA;
425 gl_attribList[i++] = GLX_DOUBLEBUFFER;
426 gl_attribList[i++] = GLX_DEPTH_SIZE; gl_attribList[i++] = 1;
427 gl_attribList[i++] = GLX_RED_SIZE; gl_attribList[i++] = 1;
428 gl_attribList[i++] = GLX_GREEN_SIZE; gl_attribList[i++] = 1;
429 gl_attribList[i++] = GLX_BLUE_SIZE; gl_attribList[i++] = 1;
430 gl_attribList[i++] = GLX_ALPHA_SIZE; gl_attribList[i++] = 0;
431 gl_attribList[i++] = None;
432 }
433 }
434 else
435 {
436 int arg=0, p=0;
437 while( (wx_attribList[arg]!=0) && (p<510) )
438 {
439 switch( wx_attribList[arg++] )
440 {
441 case WX_GL_RGBA:
442 if (wxGLCanvas::GetGLXVersion() <= 12)
443 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
444 gl_attribList[p++] = GLX_RGBA;
445 break;
446 case WX_GL_BUFFER_SIZE:
447 gl_attribList[p++] = GLX_BUFFER_SIZE;
448 gl_attribList[p++] = wx_attribList[arg++];
449 break;
450 case WX_GL_LEVEL:
451 gl_attribList[p++] = GLX_LEVEL;
452 gl_attribList[p++] = wx_attribList[arg++];
453 break;
454 case WX_GL_DOUBLEBUFFER:
455 if (wxGLCanvas::GetGLXVersion() <= 12)
456 gl_attribList[p++] = GLX_DOUBLEBUFFER;
457 else
458 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
459 // it seems this flag is useless for some hardware opengl implementation.
460 // but for Mesa 6.2.1, this flag is used so don't ignore it.
461 gl_attribList[p++] = GLX_DOUBLEBUFFER;
462 gl_attribList[p++] = 1;
463 break;
464 case WX_GL_STEREO:
465 gl_attribList[p++] = GLX_STEREO;
466 break;
467 case WX_GL_AUX_BUFFERS:
468 gl_attribList[p++] = GLX_AUX_BUFFERS;
469 gl_attribList[p++] = wx_attribList[arg++];
470 break;
471 case WX_GL_MIN_RED:
472 gl_attribList[p++] = GLX_RED_SIZE;
473 gl_attribList[p++] = wx_attribList[arg++];
474 break;
475 case WX_GL_MIN_GREEN:
476 gl_attribList[p++] = GLX_GREEN_SIZE;
477 gl_attribList[p++] = wx_attribList[arg++];
478 break;
479 case WX_GL_MIN_BLUE:
480 gl_attribList[p++] = GLX_BLUE_SIZE;
481 gl_attribList[p++] = wx_attribList[arg++];
482 break;
483 case WX_GL_MIN_ALPHA:
484 gl_attribList[p++] = GLX_ALPHA_SIZE;
485 gl_attribList[p++] = wx_attribList[arg++];
486 break;
487 case WX_GL_DEPTH_SIZE:
488 gl_attribList[p++] = GLX_DEPTH_SIZE;
489 gl_attribList[p++] = wx_attribList[arg++];
490 break;
491 case WX_GL_STENCIL_SIZE:
492 gl_attribList[p++] = GLX_STENCIL_SIZE;
493 gl_attribList[p++] = wx_attribList[arg++];
494 break;
495 case WX_GL_MIN_ACCUM_RED:
496 gl_attribList[p++] = GLX_ACCUM_RED_SIZE;
497 gl_attribList[p++] = wx_attribList[arg++];
498 break;
499 case WX_GL_MIN_ACCUM_GREEN:
500 gl_attribList[p++] = GLX_ACCUM_GREEN_SIZE;
501 gl_attribList[p++] = wx_attribList[arg++];
502 break;
503 case WX_GL_MIN_ACCUM_BLUE:
504 gl_attribList[p++] = GLX_ACCUM_BLUE_SIZE;
505 gl_attribList[p++] = wx_attribList[arg++];
506 break;
507 case WX_GL_MIN_ACCUM_ALPHA:
508 gl_attribList[p++] = GLX_ACCUM_ALPHA_SIZE;
509 gl_attribList[p++] = wx_attribList[arg++];
510 break;
511 default:
512 break;
513 }
514 }
515
516 gl_attribList[p] = 0;
517 }
518 }
519
520 void wxGLCanvas::QueryGLXVersion()
521 {
522 if (m_glxVersion == 0)
523 {
524 // check the GLX version
525 int glxMajorVer, glxMinorVer;
526 bool ok = glXQueryVersion(GDK_DISPLAY(), &glxMajorVer, &glxMinorVer);
527 wxASSERT_MSG( ok, _T("GLX version not found") );
528 if (!ok)
529 m_glxVersion = 10; // 1.0 by default
530 else
531 m_glxVersion = glxMajorVer*10 + glxMinorVer;
532 }
533 }
534
535 int wxGLCanvas::GetGLXVersion()
536 {
537 wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
538 return m_glxVersion;
539 }
540
541
542 void wxGLCanvas::SwapBuffers()
543 {
544 GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window;
545 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
546 }
547
548 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
549 {
550 }
551
552 void wxGLCanvas::SetCurrent(const wxGLContext& RC) const
553 {
554 RC.SetCurrent(*this);
555 }
556
557 void wxGLCanvas::SetCurrent()
558 {
559 if (m_glContext)
560 m_glContext->SetCurrent(*this);
561 }
562
563 void wxGLCanvas::SetColour( const wxChar *colour )
564 {
565 wxColour col = wxTheColourDatabase->Find(colour);
566 if (col.Ok())
567 {
568 float r = (float)(col.Red()/256.0);
569 float g = (float)(col.Green()/256.0);
570 float b = (float)(col.Blue()/256.0);
571 glColor3f( r, g, b);
572 }
573 }
574
575 void wxGLCanvas::OnInternalIdle()
576 {
577 if (/*m_glContext &&*/ m_exposed)
578 {
579 wxPaintEvent event( GetId() );
580 event.SetEventObject( this );
581 GetEventHandler()->ProcessEvent( event );
582
583 m_exposed = false;
584 GetUpdateRegion().Clear();
585 }
586
587 wxWindow::OnInternalIdle();
588 }
589
590
591
592 //---------------------------------------------------------------------------
593 // wxGLApp
594 //---------------------------------------------------------------------------
595
596 IMPLEMENT_CLASS(wxGLApp, wxApp)
597
598 wxGLApp::~wxGLApp()
599 {
600 if (m_glFBCInfo)
601 XFree(m_glFBCInfo);
602 if (m_glVisualInfo)
603 XFree(m_glVisualInfo);
604 }
605
606 bool wxGLApp::InitGLVisual(int *attribList)
607 {
608 wxGLCanvas::QueryGLXVersion();
609
610 if (wxGLCanvas::GetGLXVersion() >= 13)
611 {
612 // GLX >= 1.3
613 if (m_glFBCInfo)
614 XFree(m_glFBCInfo);
615 m_glFBCInfo = wxGLCanvas::ChooseGLFBC(attribList);
616
617 if (m_glFBCInfo)
618 {
619 if (m_glVisualInfo)
620 XFree(m_glVisualInfo);
621 m_glVisualInfo = glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig *)m_glFBCInfo)[0]);
622 }
623 return (m_glFBCInfo != NULL) && (m_glVisualInfo != NULL);
624 }
625 else
626 {
627 // GLX <= 1.2
628 if (m_glVisualInfo)
629 XFree(m_glVisualInfo);
630 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
631 return m_glVisualInfo != NULL;
632 }
633 }
634
635 #endif
636 // wxUSE_GLCANVAS