Include wx/module.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / gtk / glcanvas.cpp
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 #include "wx/module.h"
24 #endif // WX_PRECOMP
25
26 extern "C"
27 {
28 #include "gtk/gtk.h"
29 #include "gdk/gdk.h"
30 #include "gdk/gdkx.h"
31 }
32
33 #include "wx/gtk/win_gtk.h"
34 #include "wx/gtk/private.h"
35
36 // DLL options compatibility check:
37 #include "wx/build.h"
38 WX_CHECK_BUILD_OPTIONS("wxGL")
39
40
41 //---------------------------------------------------------------------------
42 // static variables
43 //---------------------------------------------------------------------------
44 int wxGLCanvas::m_glxVersion = 0;
45
46 //---------------------------------------------------------------------------
47 // global data
48 //---------------------------------------------------------------------------
49
50 XVisualInfo *g_vi = (XVisualInfo*) NULL;
51
52 //---------------------------------------------------------------------------
53 // wxGLContext
54 //---------------------------------------------------------------------------
55
56 IMPLEMENT_CLASS(wxGLContext,wxObject)
57
58 wxGLContext::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;
64
65 if (wxGLCanvas::GetGLXVersion() >= 13)
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 }
72 else
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 }
79
80 wxCHECK_RET( m_glContext, _T("Couldn't create OpenGl context") );
81 }
82
83 wxGLContext::wxGLContext(
84 bool WXUNUSED(isRGB), wxWindow *win,
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;
93
94 if (wxGLCanvas::GetGLXVersion() >= 13)
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 }
103 else
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 }
112
113 if ( !m_glContext )
114 {
115 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
116 }
117 }
118
119 wxGLContext::~wxGLContext()
120 {
121 if (!m_glContext) return;
122
123 if (m_glContext == glXGetCurrentContext())
124 {
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);
131 }
132
133 glXDestroyContext( GDK_DISPLAY(), m_glContext );
134 }
135
136 void 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
145 void wxGLContext::SetCurrent()
146 {
147 if (m_glContext)
148 {
149 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
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 );
158 }
159 }
160
161 void wxGLContext::SetColour(const wxChar *colour)
162 {
163 wxColour col = wxTheColourDatabase->Find(colour);
164 if (col.Ok())
165 {
166 float r = (float)(col.Red()/256.0);
167 float g = (float)(col.Green()/256.0);
168 float b = (float)(col.Blue()/256.0);
169 glColor3f( r, g, b);
170 }
171 }
172
173 void wxGLContext::SetupPixelFormat()
174 {
175 }
176
177 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
178 {
179 }
180
181 wxPalette wxGLContext::CreateDefaultPalette()
182 {
183 return wxNullPalette;
184 }
185
186 //-----------------------------------------------------------------------------
187 // "realize" from m_wxwindow
188 //-----------------------------------------------------------------------------
189
190 extern "C" {
191 static gint
192 gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
193 {
194 if ( !win->m_glContext )
195 {
196 wxGLContext *share = win->m_sharedContext;
197 if ( !share && win->m_sharedContextOf )
198 share = win->m_sharedContextOf->GetContext();
199
200 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
201 }
202
203 return FALSE;
204 }
205 }
206
207 //-----------------------------------------------------------------------------
208 // "map" from m_wxwindow
209 //-----------------------------------------------------------------------------
210
211 extern "C" {
212 static gint
213 gtk_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
221 win->m_exposed = false;
222 win->GetUpdateRegion().Clear();
223 }
224
225 return FALSE;
226 }
227 }
228
229 //-----------------------------------------------------------------------------
230 // "expose_event" of m_wxwindow
231 //-----------------------------------------------------------------------------
232
233 extern "C" {
234 static void
235 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
236 {
237 // don't need to install idle handler, its done from "event" signal
238
239 win->m_exposed = true;
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 }
246 }
247
248 //-----------------------------------------------------------------------------
249 // "size_allocate" of m_wxwindow
250 //-----------------------------------------------------------------------------
251
252 extern "C" {
253 static void
254 gtk_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 }
266 }
267
268 //---------------------------------------------------------------------------
269 // wxGlCanvas
270 //---------------------------------------------------------------------------
271
272 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
273
274 BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
275 EVT_SIZE(wxGLCanvas::OnSize)
276 END_EVENT_TABLE()
277
278 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
279 const wxPoint& pos, const wxSize& size,
280 long style, const wxString& name,
281 int *attribList,
282 const wxPalette& palette )
283 {
284 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
285 }
286
287 wxGLCanvas::wxGLCanvas( wxWindow *parent,
288 const wxGLContext *shared,
289 wxWindowID id,
290 const wxPoint& pos, const wxSize& size,
291 long style, const wxString& name,
292 int *attribList,
293 const wxPalette& palette )
294 {
295 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
296 }
297
298 wxGLCanvas::wxGLCanvas( wxWindow *parent,
299 const wxGLCanvas *shared,
300 wxWindowID id,
301 const wxPoint& pos, const wxSize& size,
302 long style, const wxString& name,
303 int *attribList,
304 const wxPalette& palette )
305 {
306 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
307 }
308
309 bool wxGLCanvas::Create( wxWindow *parent,
310 const wxGLContext *shared,
311 const wxGLCanvas *shared_context_of,
312 wxWindowID id,
313 const wxPoint& pos, const wxSize& size,
314 long style, const wxString& name,
315 int *attribList,
316 const wxPalette& palette)
317 {
318 m_sharedContext = (wxGLContext*)shared; // const_cast
319 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
320 m_glContext = (wxGLContext*) NULL;
321
322 m_exposed = false;
323 m_noExpose = true;
324 m_nativeSizeEvent = true;
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)
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;
338 m_canFreeFBC = false; // owned by wxTheApp - don't free upon destruction
339 }
340 else
341 {
342 fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList);
343 m_canFreeFBC = true;
344 }
345 m_fbc = fbc; // save for later use
346 wxCHECK_MSG( m_fbc, false, _T("required FBConfig couldn't be found") );
347 }
348
349 XVisualInfo *vi = NULL;
350 if (wxTheApp->m_glVisualInfo != NULL)
351 {
352 vi = (XVisualInfo *)wxTheApp->m_glVisualInfo;
353 m_canFreeVi = false; // owned by wxTheApp - don't free upon destruction
354 }
355 else
356 {
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
364 m_canFreeVi = true;
365 }
366
367 m_vi = vi; // save for later use
368
369 wxCHECK_MSG( m_vi, false, _T("required visual couldn't be found") );
370 GdkVisual *visual;
371 GdkColormap *colormap;
372
373 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
374 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
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 );
398
399 gtk_widget_push_colormap( colormap );
400
401 wxWindow::Create( parent, id, pos, size, style, name );
402 m_glWidget = m_wxwindow;
403 }
404
405 gtk_widget_set_double_buffered( m_glWidget, FALSE );
406
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);
419
420 if (gtk_check_version(2,2,0) != NULL)
421 {
422 gtk_widget_pop_colormap();
423 }
424
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
434 return true;
435 }
436
437 wxGLCanvas::~wxGLCanvas()
438 {
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 );
446
447 delete m_glContext;
448 }
449
450 void* wxGLCanvas::ChooseGLVisual(int *attribList)
451 {
452 int data[512];
453 GetGLAttribListFromWX( attribList, data );
454 attribList = (int*) data;
455
456 Display *dpy = GDK_DISPLAY();
457
458 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
459 }
460
461 void* wxGLCanvas::ChooseGLFBC(int *attribList)
462 {
463 int data[512];
464 GetGLAttribListFromWX( attribList, data );
465 attribList = (int*) data;
466
467 int returned;
468 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
469 attribList, &returned );
470 }
471
472
473 void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList )
474 {
475 if (!wx_attribList)
476 {
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 }
493 }
494 else
495 {
496 int arg=0, p=0;
497 while( (wx_attribList[arg]!=0) && (p<510) )
498 {
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 }
574 }
575
576 gl_attribList[p] = 0;
577 }
578 }
579
580 void wxGLCanvas::QueryGLXVersion()
581 {
582 if (m_glxVersion == 0)
583 {
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;
592 }
593 }
594
595 int wxGLCanvas::GetGLXVersion()
596 {
597 wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
598 return m_glxVersion;
599 }
600
601
602 void wxGLCanvas::SwapBuffers()
603 {
604 if (m_glContext)
605 m_glContext->SwapBuffers();
606 }
607
608 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
609 {
610 }
611
612 void wxGLCanvas::SetCurrent()
613 {
614 if (m_glContext)
615 m_glContext->SetCurrent();
616 }
617
618 void wxGLCanvas::SetColour( const wxChar *colour )
619 {
620 if (m_glContext)
621 m_glContext->SetColour( colour );
622 }
623
624 void wxGLCanvas::OnInternalIdle()
625 {
626 if (m_glContext && m_exposed)
627 {
628 wxPaintEvent event( GetId() );
629 event.SetEventObject( this );
630 GetEventHandler()->ProcessEvent( event );
631
632 m_exposed = false;
633 GetUpdateRegion().Clear();
634 }
635
636 wxWindow::OnInternalIdle();
637 }
638
639
640
641 //---------------------------------------------------------------------------
642 // wxGLApp
643 //---------------------------------------------------------------------------
644
645 IMPLEMENT_CLASS(wxGLApp, wxApp)
646
647 wxGLApp::~wxGLApp()
648 {
649 if (m_glFBCInfo)
650 XFree(m_glFBCInfo);
651 if (m_glVisualInfo)
652 XFree(m_glVisualInfo);
653 }
654
655 bool wxGLApp::InitGLVisual(int *attribList)
656 {
657 wxGLCanvas::QueryGLXVersion();
658
659 if (wxGLCanvas::GetGLXVersion() >= 13)
660 {
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);
673 }
674 else
675 {
676 // GLX <= 1.2
677 if (m_glVisualInfo)
678 XFree(m_glVisualInfo);
679 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
680 return m_glVisualInfo != NULL;
681 }
682 }
683
684 #endif
685 // wxUSE_GLCANVAS