1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "glcanvas.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/glcanvas.h"
26 #include "wx/colour.h"
27 #include "wx/module.h"
37 #include "wx/gtk/win_gtk.h"
39 // DLL options compatibility check:
41 WX_CHECK_BUILD_OPTIONS("wxGL")
43 //---------------------------------------------------------------------------
45 //---------------------------------------------------------------------------
47 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 extern void wxapp_install_idle_handler();
56 //---------------------------------------------------------------------------
58 //---------------------------------------------------------------------------
60 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
62 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
65 m_widget
= win
->m_wxwindow
;
67 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
68 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
70 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
72 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
, None
, GL_TRUE
);
74 wxCHECK_RET( m_glContext
, _T("Couldn't create OpenGl context") );
77 wxGLContext::wxGLContext(
78 bool WXUNUSED(isRGB
), wxWindow
*win
,
79 const wxPalette
& WXUNUSED(palette
),
80 const wxGLContext
*other
/* for sharing display lists */
84 m_widget
= win
->m_wxwindow
;
86 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
87 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
89 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
91 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
,
92 other
? other
->m_glContext
: None
,
97 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
101 wxGLContext::~wxGLContext()
103 if (!m_glContext
) return;
105 if (m_glContext
== glXGetCurrentContext())
107 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
110 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
113 void wxGLContext::SwapBuffers()
117 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
118 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
122 void wxGLContext::SetCurrent()
126 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
127 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
131 void wxGLContext::SetColour(const wxChar
*colour
)
136 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
139 r
= (float)(col
->Red()/256.0);
140 g
= (float)(col
->Green()/256.0);
141 b
= (float)(col
->Blue()/256.0);
146 void wxGLContext::SetupPixelFormat()
150 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
154 wxPalette
wxGLContext::CreateDefaultPalette()
156 return wxNullPalette
;
159 //-----------------------------------------------------------------------------
160 // "realize" from m_wxwindow
161 //-----------------------------------------------------------------------------
164 gtk_glwindow_realized_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
166 wxGLContext
*share
= win
->m_sharedContext
;
167 if (share
==NULL
&& win
->m_sharedContextOf
) share
=win
->m_sharedContextOf
->GetContext();
169 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, share
);
174 //-----------------------------------------------------------------------------
175 // "map" from m_wxwindow
176 //-----------------------------------------------------------------------------
179 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
181 if (win
->m_glContext
/* && win->m_exposed*/)
183 wxPaintEvent
event( win
->GetId() );
184 event
.SetEventObject( win
);
185 win
->GetEventHandler()->ProcessEvent( event
);
187 win
->m_exposed
= FALSE
;
188 win
->GetUpdateRegion().Clear();
194 //-----------------------------------------------------------------------------
195 // "expose_event" of m_wxwindow
196 //-----------------------------------------------------------------------------
199 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
202 wxapp_install_idle_handler();
204 win
->m_exposed
= TRUE
;
206 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
208 gdk_event
->area
.width
,
209 gdk_event
->area
.height
);
212 //-----------------------------------------------------------------------------
213 // "draw" of m_wxwindow
214 //-----------------------------------------------------------------------------
218 gtk_glwindow_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxGLCanvas
*win
)
221 wxapp_install_idle_handler();
223 win
->m_exposed
= TRUE
;
225 win
->GetUpdateRegion().Union( rect
->x
, rect
->y
,
226 rect
->width
, rect
->height
);
230 //-----------------------------------------------------------------------------
231 // "size_allocate" of m_wxwindow
232 //-----------------------------------------------------------------------------
235 gtk_glcanvas_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxGLCanvas
*win
)
238 wxapp_install_idle_handler();
243 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
244 event
.SetEventObject( win
);
245 win
->GetEventHandler()->ProcessEvent( event
);
248 //---------------------------------------------------------------------------
250 //---------------------------------------------------------------------------
252 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
254 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
255 EVT_SIZE(wxGLCanvas::OnSize
)
258 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
259 const wxPoint
& pos
, const wxSize
& size
,
260 long style
, const wxString
& name
,
262 const wxPalette
& palette
)
264 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
267 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
268 const wxGLContext
*shared
,
270 const wxPoint
& pos
, const wxSize
& size
,
271 long style
, const wxString
& name
,
273 const wxPalette
& palette
)
275 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
278 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
279 const wxGLCanvas
*shared
,
281 const wxPoint
& pos
, const wxSize
& size
,
282 long style
, const wxString
& name
,
284 const wxPalette
& palette
)
286 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
289 bool wxGLCanvas::Create( wxWindow
*parent
,
290 const wxGLContext
*shared
,
291 const wxGLCanvas
*shared_context_of
,
293 const wxPoint
& pos
, const wxSize
& size
,
294 long style
, const wxString
& name
,
296 const wxPalette
& palette
)
298 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
299 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
300 m_glContext
= (wxGLContext
*) NULL
;
304 m_nativeSizeEvent
= TRUE
;
306 XVisualInfo
*vi
= NULL
;
307 if (wxTheApp
->m_glVisualInfo
!= NULL
)
309 vi
= (XVisualInfo
*) wxTheApp
->m_glVisualInfo
;
310 m_canFreeVi
= FALSE
; // owned by wxTheApp - don't free upon destruction
314 vi
= (XVisualInfo
*) ChooseGLVisual(attribList
);
317 m_vi
= vi
; // save for later use
319 wxCHECK_MSG( m_vi
, FALSE
, _T("required visual couldn't be found") );
321 GdkVisual
*visual
= gdkx_visual_get( vi
->visualid
);
322 GdkColormap
*colormap
= gdk_colormap_new( gdkx_visual_get(vi
->visualid
), TRUE
);
324 gtk_widget_push_colormap( colormap
);
325 gtk_widget_push_visual( visual
);
327 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
329 m_glWidget
= m_wxwindow
;
332 gtk_widget_set_double_buffered( m_glWidget
, FALSE
);
335 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow
), FALSE
);
337 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "realize",
338 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback
), (gpointer
) this );
340 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "map",
341 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback
), (gpointer
) this );
343 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "expose_event",
344 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback
), (gpointer
)this );
347 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "draw",
348 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback
), (gpointer
)this );
351 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
352 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback
), (gpointer
)this );
354 gtk_widget_pop_visual();
355 gtk_widget_pop_colormap();
357 if (GTK_WIDGET_REALIZED(m_wxwindow
))
358 gtk_glwindow_realized_callback( m_wxwindow
, this );
360 if (GTK_WIDGET_MAPPED(m_wxwindow
))
361 gtk_glwindow_map_callback( m_wxwindow
, this );
366 wxGLCanvas::~wxGLCanvas()
368 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
370 if (vi
&& m_canFreeVi
) XFree( vi
);
371 if (m_glContext
) delete m_glContext
;
374 void* wxGLCanvas::ChooseGLVisual(int *attribList
)
379 // default settings if attriblist = 0
381 data
[1] = GLX_DOUBLEBUFFER
;
382 data
[2] = GLX_DEPTH_SIZE
; data
[3] = 1;
383 data
[4] = GLX_RED_SIZE
; data
[5] = 1;
384 data
[6] = GLX_GREEN_SIZE
; data
[7] = 1;
385 data
[8] = GLX_BLUE_SIZE
; data
[9] = 1;
386 data
[10] = GLX_ALPHA_SIZE
; data
[11] = 0;
389 attribList
= (int*) data
;
395 while( (attribList
[arg
]!=0) && (p
<510) )
397 switch( attribList
[arg
++] )
399 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
400 case WX_GL_BUFFER_SIZE
:
401 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
403 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
404 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
405 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
406 case WX_GL_AUX_BUFFERS
:
407 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
409 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
410 case WX_GL_MIN_GREEN
:
411 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
413 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
414 case WX_GL_MIN_ALPHA
:
415 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
416 case WX_GL_DEPTH_SIZE
:
417 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
418 case WX_GL_STENCIL_SIZE
:
419 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
420 case WX_GL_MIN_ACCUM_RED
:
421 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
422 case WX_GL_MIN_ACCUM_GREEN
:
423 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
424 case WX_GL_MIN_ACCUM_BLUE
:
425 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
426 case WX_GL_MIN_ACCUM_ALPHA
:
427 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
434 attribList
= (int*) data
;
438 Display
*dpy
= GDK_DISPLAY();
440 return glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
443 void wxGLCanvas::SwapBuffers()
446 m_glContext
->SwapBuffers();
449 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
453 void wxGLCanvas::SetCurrent()
456 m_glContext
->SetCurrent();
459 void wxGLCanvas::SetColour( const wxChar
*colour
)
462 m_glContext
->SetColour( colour
);
465 void wxGLCanvas::OnInternalIdle()
467 if (m_glContext
&& m_exposed
)
469 wxPaintEvent
event( GetId() );
470 event
.SetEventObject( this );
471 GetEventHandler()->ProcessEvent( event
);
474 GetUpdateRegion().Clear();
477 wxWindow::OnInternalIdle();
482 //---------------------------------------------------------------------------
484 //---------------------------------------------------------------------------
486 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
491 XFree(m_glVisualInfo
);
494 bool wxGLApp::InitGLVisual(int *attribList
)
497 XFree(m_glVisualInfo
);
499 m_glVisualInfo
= wxGLCanvas::ChooseGLVisual(attribList
);
501 return m_glVisualInfo
!= NULL
;