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