]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/glcanvas.cpp
Applied patch [ 1435340 ] Add support of wxToggleButton to wxGenericValidator
[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
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
3cbab641 31#include "wx/gtk1/win_gtk.h"
8b089c5e 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
JS
251
252//-----------------------------------------------------------------------------
253// "draw" of m_wxwindow
254//-----------------------------------------------------------------------------
255
865bb325 256extern "C" {
2b5f62a0 257static void
8b089c5e
JS
258gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
259{
2b5f62a0 260 if (g_isIdle)
8b089c5e
JS
261 wxapp_install_idle_handler();
262
263 win->m_exposed = TRUE;
264
265 win->GetUpdateRegion().Union( rect->x, rect->y,
266 rect->width, rect->height );
267}
865bb325 268}
8b089c5e
JS
269
270//-----------------------------------------------------------------------------
271// "size_allocate" of m_wxwindow
272//-----------------------------------------------------------------------------
273
865bb325 274extern "C" {
2b5f62a0 275static void
8b089c5e
JS
276gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
277{
278 if (g_isIdle)
279 wxapp_install_idle_handler();
280
281 if (!win->m_hasVMT)
282 return;
283
284 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
285 event.SetEventObject( win );
286 win->GetEventHandler()->ProcessEvent( event );
287}
865bb325 288}
8b089c5e
JS
289
290//---------------------------------------------------------------------------
291// wxGlCanvas
292//---------------------------------------------------------------------------
293
4660d7e5 294IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
8b089c5e 295
4660d7e5 296BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
8b089c5e
JS
297 EVT_SIZE(wxGLCanvas::OnSize)
298END_EVENT_TABLE()
299
300wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
2b5f62a0
VZ
301 const wxPoint& pos, const wxSize& size,
302 long style, const wxString& name,
303 int *attribList,
304 const wxPalette& palette )
8b089c5e
JS
305{
306 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
307}
308
2b5f62a0 309wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
310 const wxGLContext *shared,
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 )
316{
8b089c5e
JS
317 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
318}
319
2b5f62a0 320wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
321 const wxGLCanvas *shared,
322 wxWindowID id,
2b5f62a0
VZ
323 const wxPoint& pos, const wxSize& size,
324 long style, const wxString& name,
325 int *attribList,
326 const wxPalette& palette )
327{
8b089c5e
JS
328 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
329}
330
2b5f62a0 331bool wxGLCanvas::Create( wxWindow *parent,
8b089c5e
JS
332 const wxGLContext *shared,
333 const wxGLCanvas *shared_context_of,
334 wxWindowID id,
2b5f62a0
VZ
335 const wxPoint& pos, const wxSize& size,
336 long style, const wxString& name,
337 int *attribList,
338 const wxPalette& palette)
8b089c5e
JS
339{
340 m_sharedContext = (wxGLContext*)shared; // const_cast
341 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
342 m_glContext = (wxGLContext*) NULL;
2b5f62a0 343
8b089c5e
JS
344 m_exposed = FALSE;
345 m_noExpose = TRUE;
346 m_nativeSizeEvent = TRUE;
34a34b02
VZ
347 m_fbc = NULL;
348 m_vi = NULL;
349
350 // to be sure the glx version is known
351 wxGLCanvas::QueryGLXVersion();
352
353 if (wxGLCanvas::GetGLXVersion() >= 13)
b4d06fb7
MR
354 {
355 // GLX >= 1.3 uses a GLXFBConfig
356 GLXFBConfig * fbc = NULL;
357 if (wxTheApp->m_glFBCInfo != NULL)
358 {
359 fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo;
360 m_canFreeFBC = FALSE; // owned by wxTheApp - don't free upon destruction
361 }
362 else
363 {
364 fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList);
365 m_canFreeFBC = TRUE;
366 }
367 m_fbc = fbc; // save for later use
368 wxCHECK_MSG( m_fbc, FALSE, _T("required FBConfig couldn't be found") );
369 }
2b5f62a0 370
a6f5aa49 371 XVisualInfo *vi = NULL;
2b5f62a0
VZ
372 if (wxTheApp->m_glVisualInfo != NULL)
373 {
b4d06fb7 374 vi = (XVisualInfo *)wxTheApp->m_glVisualInfo;
a6f5aa49 375 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
2b5f62a0
VZ
376 }
377 else
378 {
b4d06fb7
MR
379 if (wxGLCanvas::GetGLXVersion() >= 13)
380 // GLX >= 1.3
381 vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]);
382 else
383 // GLX <= 1.2
384 vi = (XVisualInfo *) ChooseGLVisual(attribList);
385
a6f5aa49
VZ
386 m_canFreeVi = TRUE;
387 }
b4d06fb7 388
a6f5aa49 389 m_vi = vi; // save for later use
2b5f62a0
VZ
390
391 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
fee7a683
MR
392 GdkVisual *visual;
393 GdkColormap *colormap;
2b5f62a0 394
fee7a683 395 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
3cbab641 396#if 0
fee7a683
MR
397 if (!gtk_check_version(2,2,0))
398 {
399 wxWindow::Create( parent, id, pos, size, style, name );
400
401 m_glWidget = m_wxwindow;
402
403 GdkScreen *screen = gtk_widget_get_screen( m_glWidget );
404 colormap = gdk_screen_get_default_colormap(screen);
405 visual = gdk_colormap_get_visual(colormap);
406
407 if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid)
408 {
409 visual = gdk_x11_screen_lookup_visual( screen, vi->visualid );
410 colormap = gdk_colormap_new(visual, FALSE);
411 }
412
413 gtk_widget_set_colormap( m_glWidget, colormap );
414 }
415 else
416#endif
417 {
418 visual = gdkx_visual_get( vi->visualid );
419 colormap = gdk_colormap_new( visual, TRUE );
a6f5aa49 420
fee7a683
MR
421 gtk_widget_push_colormap( colormap );
422 gtk_widget_push_visual( visual );
a6f5aa49 423
fee7a683
MR
424 wxWindow::Create( parent, id, pos, size, style, name );
425 m_glWidget = m_wxwindow;
426 }
2b5f62a0 427
a6f5aa49 428 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
2b5f62a0 429
a6f5aa49
VZ
430 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
431 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
432
433 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
434 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
435
436 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
437 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
438
439 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
440 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
2b5f62a0 441
a6f5aa49
VZ
442 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
443 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
444
3cbab641
MR
445 gtk_widget_pop_visual();
446 gtk_widget_pop_colormap();
2b5f62a0 447
bc869971
VZ
448 // if our parent window is already visible, we had been realized before we
449 // connected to the "realize" signal and hence our m_glContext hasn't been
450 // initialized yet and we have to do it now
451 if (GTK_WIDGET_REALIZED(m_wxwindow))
452 gtk_glwindow_realized_callback( m_wxwindow, this );
453
454 if (GTK_WIDGET_MAPPED(m_wxwindow))
455 gtk_glwindow_map_callback( m_wxwindow, this );
456
a6f5aa49
VZ
457 return TRUE;
458}
459
460wxGLCanvas::~wxGLCanvas()
461{
b4d06fb7
MR
462 GLXFBConfig * fbc = (GLXFBConfig *) m_fbc;
463 if (fbc && m_canFreeFBC)
464 XFree( fbc );
465
466 XVisualInfo *vi = (XVisualInfo *) m_vi;
467 if (vi && m_canFreeVi)
468 XFree( vi );
2b5f62a0 469
4230303c 470 delete m_glContext;
a6f5aa49
VZ
471}
472
473void* wxGLCanvas::ChooseGLVisual(int *attribList)
474{
475 int data[512];
b4d06fb7
MR
476 GetGLAttribListFromWX( attribList, data );
477 attribList = (int*) data;
34a34b02 478
b4d06fb7
MR
479 Display *dpy = GDK_DISPLAY();
480
481 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
34a34b02 482}
0f8d11dc 483
34a34b02
VZ
484void* wxGLCanvas::ChooseGLFBC(int *attribList)
485{
b4d06fb7
MR
486 int data[512];
487 GetGLAttribListFromWX( attribList, data );
488 attribList = (int*) data;
34a34b02 489
b4d06fb7
MR
490 int returned;
491 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
492 attribList, &returned );
34a34b02
VZ
493}
494
495
496void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList )
497{
b4d06fb7 498 if (!wx_attribList)
34a34b02 499 {
b4d06fb7
MR
500 if (wxGLCanvas::GetGLXVersion() >= 13)
501 // leave GLX >= 1.3 choose the default attributes
502 gl_attribList[0] = 0;
503 else
504 {
505 int i = 0;
506 // default settings if attriblist = 0
507 gl_attribList[i++] = GLX_RGBA;
508 gl_attribList[i++] = GLX_DOUBLEBUFFER;
509 gl_attribList[i++] = GLX_DEPTH_SIZE; gl_attribList[i++] = 1;
510 gl_attribList[i++] = GLX_RED_SIZE; gl_attribList[i++] = 1;
511 gl_attribList[i++] = GLX_GREEN_SIZE; gl_attribList[i++] = 1;
512 gl_attribList[i++] = GLX_BLUE_SIZE; gl_attribList[i++] = 1;
513 gl_attribList[i++] = GLX_ALPHA_SIZE; gl_attribList[i++] = 0;
514 gl_attribList[i++] = None;
515 }
8b089c5e
JS
516 }
517 else
518 {
b4d06fb7
MR
519 int arg=0, p=0;
520 while( (wx_attribList[arg]!=0) && (p<510) )
8b089c5e 521 {
b4d06fb7
MR
522 switch( wx_attribList[arg++] )
523 {
524 case WX_GL_RGBA:
525 if (wxGLCanvas::GetGLXVersion() <= 12)
526 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
527 gl_attribList[p++] = GLX_RGBA;
528 break;
529 case WX_GL_BUFFER_SIZE:
530 gl_attribList[p++] = GLX_BUFFER_SIZE;
531 gl_attribList[p++] = wx_attribList[arg++];
532 break;
533 case WX_GL_LEVEL:
534 gl_attribList[p++] = GLX_LEVEL;
535 gl_attribList[p++] = wx_attribList[arg++];
536 break;
537 case WX_GL_DOUBLEBUFFER:
538 if (wxGLCanvas::GetGLXVersion() <= 12)
539 gl_attribList[p++] = GLX_DOUBLEBUFFER;
540 else
541 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
542 // it seems this flag is useless for some hardware opengl implementation.
543 // but for Mesa 6.2.1, this flag is used so don't ignore it.
544 gl_attribList[p++] = GLX_DOUBLEBUFFER;
545 gl_attribList[p++] = 1;
546 break;
547 case WX_GL_STEREO:
548 gl_attribList[p++] = GLX_STEREO;
549 break;
550 case WX_GL_AUX_BUFFERS:
551 gl_attribList[p++] = GLX_AUX_BUFFERS;
552 gl_attribList[p++] = wx_attribList[arg++];
553 break;
554 case WX_GL_MIN_RED:
555 gl_attribList[p++] = GLX_RED_SIZE;
556 gl_attribList[p++] = wx_attribList[arg++];
557 break;
558 case WX_GL_MIN_GREEN:
559 gl_attribList[p++] = GLX_GREEN_SIZE;
560 gl_attribList[p++] = wx_attribList[arg++];
561 break;
562 case WX_GL_MIN_BLUE:
563 gl_attribList[p++] = GLX_BLUE_SIZE;
564 gl_attribList[p++] = wx_attribList[arg++];
565 break;
566 case WX_GL_MIN_ALPHA:
567 gl_attribList[p++] = GLX_ALPHA_SIZE;
568 gl_attribList[p++] = wx_attribList[arg++];
569 break;
570 case WX_GL_DEPTH_SIZE:
571 gl_attribList[p++] = GLX_DEPTH_SIZE;
572 gl_attribList[p++] = wx_attribList[arg++];
573 break;
574 case WX_GL_STENCIL_SIZE:
575 gl_attribList[p++] = GLX_STENCIL_SIZE;
576 gl_attribList[p++] = wx_attribList[arg++];
577 break;
578 case WX_GL_MIN_ACCUM_RED:
579 gl_attribList[p++] = GLX_ACCUM_RED_SIZE;
580 gl_attribList[p++] = wx_attribList[arg++];
581 break;
582 case WX_GL_MIN_ACCUM_GREEN:
583 gl_attribList[p++] = GLX_ACCUM_GREEN_SIZE;
584 gl_attribList[p++] = wx_attribList[arg++];
585 break;
586 case WX_GL_MIN_ACCUM_BLUE:
587 gl_attribList[p++] = GLX_ACCUM_BLUE_SIZE;
588 gl_attribList[p++] = wx_attribList[arg++];
589 break;
590 case WX_GL_MIN_ACCUM_ALPHA:
591 gl_attribList[p++] = GLX_ACCUM_ALPHA_SIZE;
592 gl_attribList[p++] = wx_attribList[arg++];
593 break;
594 default:
595 break;
596 }
8b089c5e 597 }
b4d06fb7
MR
598
599 gl_attribList[p] = 0;
8b089c5e 600 }
34a34b02 601}
2b5f62a0 602
34a34b02
VZ
603void wxGLCanvas::QueryGLXVersion()
604{
b4d06fb7 605 if (m_glxVersion == 0)
34a34b02 606 {
b4d06fb7
MR
607 // check the GLX version
608 int glxMajorVer, glxMinorVer;
609 bool ok = glXQueryVersion(GDK_DISPLAY(), &glxMajorVer, &glxMinorVer);
610 wxASSERT_MSG( ok, _T("GLX version not found") );
611 if (!ok)
612 m_glxVersion = 10; // 1.0 by default
613 else
614 m_glxVersion = glxMajorVer*10 + glxMinorVer;
34a34b02
VZ
615 }
616}
2b5f62a0 617
34a34b02
VZ
618int wxGLCanvas::GetGLXVersion()
619{
b4d06fb7
MR
620 wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
621 return m_glxVersion;
8b089c5e
JS
622}
623
34a34b02 624
8b089c5e
JS
625void wxGLCanvas::SwapBuffers()
626{
2b5f62a0
VZ
627 if (m_glContext)
628 m_glContext->SwapBuffers();
8b089c5e
JS
629}
630
631void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
632{
8b089c5e
JS
633}
634
635void wxGLCanvas::SetCurrent()
636{
2b5f62a0
VZ
637 if (m_glContext)
638 m_glContext->SetCurrent();
8b089c5e
JS
639}
640
2b5f62a0 641void wxGLCanvas::SetColour( const wxChar *colour )
8b089c5e 642{
2b5f62a0
VZ
643 if (m_glContext)
644 m_glContext->SetColour( colour );
8b089c5e
JS
645}
646
647void wxGLCanvas::OnInternalIdle()
648{
649 if (m_glContext && m_exposed)
650 {
651 wxPaintEvent event( GetId() );
652 event.SetEventObject( this );
653 GetEventHandler()->ProcessEvent( event );
654
655 m_exposed = FALSE;
656 GetUpdateRegion().Clear();
657 }
2b5f62a0 658
8b089c5e
JS
659 wxWindow::OnInternalIdle();
660}
661
a6f5aa49
VZ
662
663
664//---------------------------------------------------------------------------
665// wxGLApp
666//---------------------------------------------------------------------------
667
668IMPLEMENT_CLASS(wxGLApp, wxApp)
2b5f62a0 669
a6f5aa49
VZ
670wxGLApp::~wxGLApp()
671{
b4d06fb7
MR
672 if (m_glFBCInfo)
673 XFree(m_glFBCInfo);
2b5f62a0
VZ
674 if (m_glVisualInfo)
675 XFree(m_glVisualInfo);
a6f5aa49
VZ
676}
677
678bool wxGLApp::InitGLVisual(int *attribList)
679{
b4d06fb7 680 wxGLCanvas::QueryGLXVersion();
34a34b02 681
b4d06fb7 682 if (wxGLCanvas::GetGLXVersion() >= 13)
34a34b02 683 {
b4d06fb7
MR
684 // GLX >= 1.3
685 if (m_glFBCInfo)
686 XFree(m_glFBCInfo);
687 m_glFBCInfo = wxGLCanvas::ChooseGLFBC(attribList);
688
689 if (m_glFBCInfo)
690 {
691 if (m_glVisualInfo)
692 XFree(m_glVisualInfo);
693 m_glVisualInfo = glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig *)m_glFBCInfo)[0]);
694 }
695 return (m_glFBCInfo != NULL) && (m_glVisualInfo != NULL);
34a34b02 696 }
b4d06fb7 697 else
34a34b02 698 {
b4d06fb7
MR
699 // GLX <= 1.2
700 if (m_glVisualInfo)
701 XFree(m_glVisualInfo);
702 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
703 return m_glVisualInfo != NULL;
34a34b02 704 }
a6f5aa49
VZ
705}
706
8b089c5e
JS
707#endif
708 // wxUSE_GLCANVAS