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