]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/glcanvas.cpp
More comments about non-native GTH widgets.
[wxWidgets.git] / src / gtk / glcanvas.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/glcanvas.cpp
3// Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK
4// Author: Robert Roebling
5// Modified by:
6// Created: 17/08/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if wxUSE_GLCANVAS
16
17#include "wx/glcanvas.h"
18
19#ifndef WX_PRECOMP
20 #include "wx/app.h"
21 #include "wx/frame.h"
22 #include "wx/colour.h"
23#endif // WX_PRECOMP
24
25#include "wx/module.h"
26
27extern "C"
28{
29#include "gtk/gtk.h"
30#include "gdk/gdk.h"
31#include "gdk/gdkx.h"
32}
33
34#include "wx/gtk/win_gtk.h"
35#include "wx/gtk/private.h"
36
37// DLL options compatibility check:
38#include "wx/build.h"
39WX_CHECK_BUILD_OPTIONS("wxGL")
40
41
42//---------------------------------------------------------------------------
43// static variables
44//---------------------------------------------------------------------------
45int wxGLCanvas::m_glxVersion = 0;
46
47//---------------------------------------------------------------------------
48// global data
49//---------------------------------------------------------------------------
50
51XVisualInfo *g_vi = (XVisualInfo*) NULL;
52
53//---------------------------------------------------------------------------
54// wxGLContext
55//---------------------------------------------------------------------------
56
57IMPLEMENT_CLASS(wxGLContext,wxObject)
58
59wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
60{
61 m_window = win;
62 m_widget = win->m_wxwindow;
63
64 wxGLCanvas *gc = (wxGLCanvas*) win;
65
66 if (wxGLCanvas::GetGLXVersion() >= 13)
67 {
68 // GLX >= 1.3
69 GLXFBConfig *fbc = gc->m_fbc;
70 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") );
71 m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE, None, GL_TRUE );
72 }
73 else
74 {
75 // GLX <= 1.2
76 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
77 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
78 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
79 }
80
81 wxCHECK_RET( m_glContext, _T("Couldn't create OpenGl context") );
82}
83
84wxGLContext::wxGLContext(
85 bool WXUNUSED(isRGB), wxWindow *win,
86 const wxPalette& WXUNUSED(palette),
87 const wxGLContext *other /* for sharing display lists */
88)
89{
90 m_window = win;
91 m_widget = win->m_wxwindow;
92
93 wxGLCanvas *gc = (wxGLCanvas*) win;
94
95 if (wxGLCanvas::GetGLXVersion() >= 13)
96 {
97 // GLX >= 1.3
98 GLXFBConfig *fbc = gc->m_fbc;
99 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") );
100 m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE,
101 other ? other->m_glContext : None,
102 GL_TRUE );
103 }
104 else
105 {
106 // GLX <= 1.2
107 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
108 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
109 m_glContext = glXCreateContext( GDK_DISPLAY(), vi,
110 other ? other->m_glContext : None,
111 GL_TRUE );
112 }
113
114 if ( !m_glContext )
115 {
116 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
117 }
118}
119
120wxGLContext::~wxGLContext()
121{
122 if (!m_glContext) return;
123
124 if (m_glContext == glXGetCurrentContext())
125 {
126 if (wxGLCanvas::GetGLXVersion() >= 13)
127 // GLX >= 1.3
128 glXMakeContextCurrent( GDK_DISPLAY(), None, None, NULL);
129 else
130 // GLX <= 1.2
131 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
132 }
133
134 glXDestroyContext( GDK_DISPLAY(), m_glContext );
135}
136
137void wxGLContext::SwapBuffers()
138{
139 if (m_glContext)
140 {
141 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
142 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
143 }
144}
145
146void wxGLContext::SetCurrent()
147{
148 if (m_glContext)
149 {
150 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
151
152 if (wxGLCanvas::GetGLXVersion() >= 13)
153 // GLX >= 1.3
154 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window),
155 GDK_WINDOW_XWINDOW(window), m_glContext );
156 else
157 // GLX <= 1.2
158 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
159 }
160}
161
162void wxGLContext::SetColour(const wxChar *colour)
163{
164 wxColour col = wxTheColourDatabase->Find(colour);
165 if (col.Ok())
166 {
167 float r = (float)(col.Red()/256.0);
168 float g = (float)(col.Green()/256.0);
169 float b = (float)(col.Blue()/256.0);
170 glColor3f( r, g, b);
171 }
172}
173
174void wxGLContext::SetupPixelFormat()
175{
176}
177
178void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
179{
180}
181
182wxPalette wxGLContext::CreateDefaultPalette()
183{
184 return wxNullPalette;
185}
186
187//-----------------------------------------------------------------------------
188// "realize" from m_wxwindow
189//-----------------------------------------------------------------------------
190
191extern "C" {
192static gint
193gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
194{
195 if ( !win->m_glContext )
196 {
197 wxGLContext *share = win->m_sharedContext;
198 if ( !share && win->m_sharedContextOf )
199 share = win->m_sharedContextOf->GetContext();
200
201 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
202 }
203
204 return FALSE;
205}
206}
207
208//-----------------------------------------------------------------------------
209// "map" from m_wxwindow
210//-----------------------------------------------------------------------------
211
212extern "C" {
213static gint
214gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
215{
216 if (win->m_glContext/* && win->m_exposed*/)
217 {
218 wxPaintEvent event( win->GetId() );
219 event.SetEventObject( win );
220 win->GetEventHandler()->ProcessEvent( event );
221
222 win->m_exposed = false;
223 win->GetUpdateRegion().Clear();
224 }
225
226 return FALSE;
227}
228}
229
230//-----------------------------------------------------------------------------
231// "expose_event" of m_wxwindow
232//-----------------------------------------------------------------------------
233
234extern "C" {
235static void
236gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
237{
238 // don't need to install idle handler, its done from "event" signal
239
240 win->m_exposed = true;
241
242 win->GetUpdateRegion().Union( gdk_event->area.x,
243 gdk_event->area.y,
244 gdk_event->area.width,
245 gdk_event->area.height );
246}
247}
248
249//-----------------------------------------------------------------------------
250// "size_allocate" of m_wxwindow
251//-----------------------------------------------------------------------------
252
253extern "C" {
254static void
255gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
256{
257 if (g_isIdle)
258 wxapp_install_idle_handler();
259
260 if (!win->m_hasVMT)
261 return;
262
263 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
264 event.SetEventObject( win );
265 win->GetEventHandler()->ProcessEvent( event );
266}
267}
268
269//---------------------------------------------------------------------------
270// wxGlCanvas
271//---------------------------------------------------------------------------
272
273IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
274
275BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
276 EVT_SIZE(wxGLCanvas::OnSize)
277END_EVENT_TABLE()
278
279wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
280 const wxPoint& pos, const wxSize& size,
281 long style, const wxString& name,
282 int *attribList,
283 const wxPalette& palette )
284{
285 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
286}
287
288wxGLCanvas::wxGLCanvas( wxWindow *parent,
289 const wxGLContext *shared,
290 wxWindowID id,
291 const wxPoint& pos, const wxSize& size,
292 long style, const wxString& name,
293 int *attribList,
294 const wxPalette& palette )
295{
296 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
297}
298
299wxGLCanvas::wxGLCanvas( wxWindow *parent,
300 const wxGLCanvas *shared,
301 wxWindowID id,
302 const wxPoint& pos, const wxSize& size,
303 long style, const wxString& name,
304 int *attribList,
305 const wxPalette& palette )
306{
307 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
308}
309
310bool wxGLCanvas::Create( wxWindow *parent,
311 const wxGLContext *shared,
312 const wxGLCanvas *shared_context_of,
313 wxWindowID id,
314 const wxPoint& pos, const wxSize& size,
315 long style, const wxString& name,
316 int *attribList,
317 const wxPalette& palette)
318{
319 m_sharedContext = (wxGLContext*)shared; // const_cast
320 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
321 m_glContext = (wxGLContext*) NULL;
322
323 m_exposed = false;
324 m_noExpose = true;
325 m_nativeSizeEvent = true;
326 m_fbc = NULL;
327 m_vi = NULL;
328
329 // to be sure the glx version is known
330 wxGLCanvas::QueryGLXVersion();
331
332 if (wxGLCanvas::GetGLXVersion() >= 13)
333 {
334 // GLX >= 1.3 uses a GLXFBConfig
335 GLXFBConfig * fbc = NULL;
336 if (wxTheApp->m_glFBCInfo != NULL)
337 {
338 fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo;
339 m_canFreeFBC = false; // owned by wxTheApp - don't free upon destruction
340 }
341 else
342 {
343 fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList);
344 m_canFreeFBC = true;
345 }
346 m_fbc = fbc; // save for later use
347 wxCHECK_MSG( m_fbc, false, _T("required FBConfig couldn't be found") );
348 }
349
350 XVisualInfo *vi = NULL;
351 if (wxTheApp->m_glVisualInfo != NULL)
352 {
353 vi = (XVisualInfo *)wxTheApp->m_glVisualInfo;
354 m_canFreeVi = false; // owned by wxTheApp - don't free upon destruction
355 }
356 else
357 {
358 if (wxGLCanvas::GetGLXVersion() >= 13)
359 // GLX >= 1.3
360 vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]);
361 else
362 // GLX <= 1.2
363 vi = (XVisualInfo *) ChooseGLVisual(attribList);
364
365 m_canFreeVi = true;
366 }
367
368 m_vi = vi; // save for later use
369
370 wxCHECK_MSG( m_vi, false, _T("required visual couldn't be found") );
371 GdkVisual *visual;
372 GdkColormap *colormap;
373
374 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
375#if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
376 if (!gtk_check_version(2,2,0))
377 {
378 wxWindow::Create( parent, id, pos, size, style, name );
379
380 m_glWidget = m_wxwindow;
381
382 GdkScreen *screen = gtk_widget_get_screen( m_glWidget );
383 colormap = gdk_screen_get_default_colormap(screen);
384 visual = gdk_colormap_get_visual(colormap);
385
386 if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid)
387 {
388 visual = gdk_x11_screen_lookup_visual( screen, vi->visualid );
389 colormap = gdk_colormap_new(visual, FALSE);
390 }
391
392 gtk_widget_set_colormap( m_glWidget, colormap );
393 }
394 else
395#endif
396 {
397 visual = gdkx_visual_get( vi->visualid );
398 colormap = gdk_colormap_new( visual, TRUE );
399
400 gtk_widget_push_colormap( colormap );
401
402 wxWindow::Create( parent, id, pos, size, style, name );
403 m_glWidget = m_wxwindow;
404 }
405
406 gtk_widget_set_double_buffered( m_glWidget, FALSE );
407
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);
420
421 if (gtk_check_version(2,2,0) != NULL)
422 {
423 gtk_widget_pop_colormap();
424 }
425
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
435 return true;
436}
437
438wxGLCanvas::~wxGLCanvas()
439{
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 );
447
448 delete m_glContext;
449}
450
451void* wxGLCanvas::ChooseGLVisual(int *attribList)
452{
453 int data[512];
454 GetGLAttribListFromWX( attribList, data );
455 attribList = (int*) data;
456
457 Display *dpy = GDK_DISPLAY();
458
459 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
460}
461
462void* wxGLCanvas::ChooseGLFBC(int *attribList)
463{
464 int data[512];
465 GetGLAttribListFromWX( attribList, data );
466 attribList = (int*) data;
467
468 int returned;
469 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
470 attribList, &returned );
471}
472
473
474void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList )
475{
476 if (!wx_attribList)
477 {
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 }
494 }
495 else
496 {
497 int arg=0, p=0;
498 while( (wx_attribList[arg]!=0) && (p<510) )
499 {
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 }
575 }
576
577 gl_attribList[p] = 0;
578 }
579}
580
581void wxGLCanvas::QueryGLXVersion()
582{
583 if (m_glxVersion == 0)
584 {
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;
593 }
594}
595
596int wxGLCanvas::GetGLXVersion()
597{
598 wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
599 return m_glxVersion;
600}
601
602
603void wxGLCanvas::SwapBuffers()
604{
605 if (m_glContext)
606 m_glContext->SwapBuffers();
607}
608
609void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
610{
611}
612
613void wxGLCanvas::SetCurrent()
614{
615 if (m_glContext)
616 m_glContext->SetCurrent();
617}
618
619void wxGLCanvas::SetColour( const wxChar *colour )
620{
621 if (m_glContext)
622 m_glContext->SetColour( colour );
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 }
636
637 wxWindow::OnInternalIdle();
638}
639
640
641
642//---------------------------------------------------------------------------
643// wxGLApp
644//---------------------------------------------------------------------------
645
646IMPLEMENT_CLASS(wxGLApp, wxApp)
647
648wxGLApp::~wxGLApp()
649{
650 if (m_glFBCInfo)
651 XFree(m_glFBCInfo);
652 if (m_glVisualInfo)
653 XFree(m_glVisualInfo);
654}
655
656bool wxGLApp::InitGLVisual(int *attribList)
657{
658 wxGLCanvas::QueryGLXVersion();
659
660 if (wxGLCanvas::GetGLXVersion() >= 13)
661 {
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);
674 }
675 else
676 {
677 // GLX <= 1.2
678 if (m_glVisualInfo)
679 XFree(m_glVisualInfo);
680 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
681 return m_glVisualInfo != NULL;
682 }
683}
684
685#endif
686 // wxUSE_GLCANVAS