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