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