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