]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/glcanvas.cpp
guard for 10.2
[wxWidgets.git] / src / gtk1 / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "glcanvas.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/setup.h"
20
21 #if wxUSE_GLCANVAS
22
23 #include "wx/glcanvas.h"
24
25 #include "wx/frame.h"
26 #include "wx/colour.h"
27 #include "wx/module.h"
28 #include "wx/app.h"
29
30 extern "C"
31 {
32 #include "gtk/gtk.h"
33 #include "gdk/gdk.h"
34 #include "gdk/gdkx.h"
35 }
36
37 #include "wx/gtk/win_gtk.h"
38
39 // DLL options compatibility check:
40 #include "wx/build.h"
41 WX_CHECK_BUILD_OPTIONS("wxGL")
42
43 //---------------------------------------------------------------------------
44 // global data
45 //---------------------------------------------------------------------------
46
47 XVisualInfo *g_vi = (XVisualInfo*) NULL;
48
49 //-----------------------------------------------------------------------------
50 // idle system
51 //-----------------------------------------------------------------------------
52
53 extern void wxapp_install_idle_handler();
54 extern bool g_isIdle;
55
56 //---------------------------------------------------------------------------
57 // wxGLContext
58 //---------------------------------------------------------------------------
59
60 IMPLEMENT_CLASS(wxGLContext,wxObject)
61
62 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
63 {
64 m_window = win;
65 m_widget = win->m_wxwindow;
66
67 wxGLCanvas *gc = (wxGLCanvas*) win;
68 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
69
70 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
71
72 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
73
74 wxCHECK_RET( m_glContext, _T("Couldn't create OpenGl context") );
75 }
76
77 wxGLContext::wxGLContext(
78 bool WXUNUSED(isRGB), wxWindow *win,
79 const wxPalette& WXUNUSED(palette),
80 const wxGLContext *other /* for sharing display lists */
81 )
82 {
83 m_window = win;
84 m_widget = win->m_wxwindow;
85
86 wxGLCanvas *gc = (wxGLCanvas*) win;
87 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
88
89 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
90
91 m_glContext = glXCreateContext( GDK_DISPLAY(), vi,
92 other ? other->m_glContext : None,
93 GL_TRUE );
94
95 if ( !m_glContext )
96 {
97 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
98 }
99 }
100
101 wxGLContext::~wxGLContext()
102 {
103 if (!m_glContext) return;
104
105 if (m_glContext == glXGetCurrentContext())
106 {
107 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
108 }
109
110 glXDestroyContext( GDK_DISPLAY(), m_glContext );
111 }
112
113 void wxGLContext::SwapBuffers()
114 {
115 if (m_glContext)
116 {
117 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
118 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
119 }
120 }
121
122 void wxGLContext::SetCurrent()
123 {
124 if (m_glContext)
125 {
126 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
127 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
128 }
129 }
130
131 void wxGLContext::SetColour(const wxChar *colour)
132 {
133 wxColour col = wxTheColourDatabase->Find(colour);
134 if (col.Ok())
135 {
136 float r = (float)(col.Red()/256.0);
137 float g = (float)(col.Green()/256.0);
138 float b = (float)(col.Blue()/256.0);
139 glColor3f( r, g, b);
140 }
141 }
142
143 void wxGLContext::SetupPixelFormat()
144 {
145 }
146
147 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
148 {
149 }
150
151 wxPalette wxGLContext::CreateDefaultPalette()
152 {
153 return wxNullPalette;
154 }
155
156 //-----------------------------------------------------------------------------
157 // "realize" from m_wxwindow
158 //-----------------------------------------------------------------------------
159
160 static gint
161 gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
162 {
163 if ( !win->m_glContext )
164 {
165 wxGLContext *share = win->m_sharedContext;
166 if ( !share && win->m_sharedContextOf )
167 share = win->m_sharedContextOf->GetContext();
168
169 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
170 }
171
172 return FALSE;
173 }
174
175 //-----------------------------------------------------------------------------
176 // "map" from m_wxwindow
177 //-----------------------------------------------------------------------------
178
179 static gint
180 gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
181 {
182 if (win->m_glContext/* && win->m_exposed*/)
183 {
184 wxPaintEvent event( win->GetId() );
185 event.SetEventObject( win );
186 win->GetEventHandler()->ProcessEvent( event );
187
188 win->m_exposed = FALSE;
189 win->GetUpdateRegion().Clear();
190 }
191
192 return FALSE;
193 }
194
195 //-----------------------------------------------------------------------------
196 // "expose_event" of m_wxwindow
197 //-----------------------------------------------------------------------------
198
199 static void
200 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
201 {
202 if (g_isIdle)
203 wxapp_install_idle_handler();
204
205 win->m_exposed = TRUE;
206
207 win->GetUpdateRegion().Union( gdk_event->area.x,
208 gdk_event->area.y,
209 gdk_event->area.width,
210 gdk_event->area.height );
211 }
212
213 //-----------------------------------------------------------------------------
214 // "draw" of m_wxwindow
215 //-----------------------------------------------------------------------------
216
217 #ifndef __WXGTK20__
218 static void
219 gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
220 {
221 if (g_isIdle)
222 wxapp_install_idle_handler();
223
224 win->m_exposed = TRUE;
225
226 win->GetUpdateRegion().Union( rect->x, rect->y,
227 rect->width, rect->height );
228 }
229 #endif
230
231 //-----------------------------------------------------------------------------
232 // "size_allocate" of m_wxwindow
233 //-----------------------------------------------------------------------------
234
235 static void
236 gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
237 {
238 if (g_isIdle)
239 wxapp_install_idle_handler();
240
241 if (!win->m_hasVMT)
242 return;
243
244 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
245 event.SetEventObject( win );
246 win->GetEventHandler()->ProcessEvent( event );
247 }
248
249 //---------------------------------------------------------------------------
250 // wxGlCanvas
251 //---------------------------------------------------------------------------
252
253 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
254
255 BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
256 EVT_SIZE(wxGLCanvas::OnSize)
257 END_EVENT_TABLE()
258
259 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
260 const wxPoint& pos, const wxSize& size,
261 long style, const wxString& name,
262 int *attribList,
263 const wxPalette& palette )
264 {
265 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
266 }
267
268 wxGLCanvas::wxGLCanvas( wxWindow *parent,
269 const wxGLContext *shared,
270 wxWindowID id,
271 const wxPoint& pos, const wxSize& size,
272 long style, const wxString& name,
273 int *attribList,
274 const wxPalette& palette )
275 {
276 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
277 }
278
279 wxGLCanvas::wxGLCanvas( wxWindow *parent,
280 const wxGLCanvas *shared,
281 wxWindowID id,
282 const wxPoint& pos, const wxSize& size,
283 long style, const wxString& name,
284 int *attribList,
285 const wxPalette& palette )
286 {
287 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
288 }
289
290 bool wxGLCanvas::Create( wxWindow *parent,
291 const wxGLContext *shared,
292 const wxGLCanvas *shared_context_of,
293 wxWindowID id,
294 const wxPoint& pos, const wxSize& size,
295 long style, const wxString& name,
296 int *attribList,
297 const wxPalette& palette)
298 {
299 m_sharedContext = (wxGLContext*)shared; // const_cast
300 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
301 m_glContext = (wxGLContext*) NULL;
302
303 m_exposed = FALSE;
304 m_noExpose = TRUE;
305 m_nativeSizeEvent = TRUE;
306
307 XVisualInfo *vi = NULL;
308 if (wxTheApp->m_glVisualInfo != NULL)
309 {
310 vi = (XVisualInfo *) wxTheApp->m_glVisualInfo;
311 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
312 }
313 else
314 {
315 vi = (XVisualInfo *) ChooseGLVisual(attribList);
316 m_canFreeVi = TRUE;
317 }
318 m_vi = vi; // save for later use
319
320 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
321
322 GdkVisual *visual = gdkx_visual_get( vi->visualid );
323 GdkColormap *colormap = gdk_colormap_new( visual, TRUE );
324
325 gtk_widget_push_colormap( colormap );
326 gtk_widget_push_visual( visual );
327
328 wxWindow::Create( parent, id, pos, size, style, name );
329
330 m_glWidget = m_wxwindow;
331
332 #ifdef __WXGTK20__
333 gtk_widget_set_double_buffered( m_glWidget, FALSE );
334 #endif
335
336 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
337
338 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
339 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
340
341 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
342 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
343
344 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
345 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
346
347 #ifndef __WXGTK20__
348 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
349 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
350 #endif
351
352 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
353 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
354
355 gtk_widget_pop_visual();
356 gtk_widget_pop_colormap();
357
358 return TRUE;
359 }
360
361 wxGLCanvas::~wxGLCanvas()
362 {
363 XVisualInfo *vi = (XVisualInfo *) m_vi;
364
365 if (vi && m_canFreeVi) XFree( vi );
366 delete m_glContext;
367 }
368
369 void* wxGLCanvas::ChooseGLVisual(int *attribList)
370 {
371 int data[512];
372 if (!attribList)
373 {
374 // default settings if attriblist = 0
375 data[0] = GLX_RGBA;
376 data[1] = GLX_DOUBLEBUFFER;
377 data[2] = GLX_DEPTH_SIZE; data[3] = 1;
378 data[4] = GLX_RED_SIZE; data[5] = 1;
379 data[6] = GLX_GREEN_SIZE; data[7] = 1;
380 data[8] = GLX_BLUE_SIZE; data[9] = 1;
381 data[10] = GLX_ALPHA_SIZE; data[11] = 0;
382 data[12] = None;
383
384 attribList = (int*) data;
385 }
386 else
387 {
388 int arg=0, p=0;
389
390 while( (attribList[arg]!=0) && (p<510) )
391 {
392 switch( attribList[arg++] )
393 {
394 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
395 case WX_GL_BUFFER_SIZE:
396 data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
397 case WX_GL_LEVEL:
398 data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break;
399 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
400 case WX_GL_STEREO: data[p++] = GLX_STEREO; break;
401 case WX_GL_AUX_BUFFERS:
402 data[p++]=GLX_AUX_BUFFERS; data[p++]=attribList[arg++]; break;
403 case WX_GL_MIN_RED:
404 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
405 case WX_GL_MIN_GREEN:
406 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
407 case WX_GL_MIN_BLUE:
408 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
409 case WX_GL_MIN_ALPHA:
410 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
411 case WX_GL_DEPTH_SIZE:
412 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
413 case WX_GL_STENCIL_SIZE:
414 data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
415 case WX_GL_MIN_ACCUM_RED:
416 data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
417 case WX_GL_MIN_ACCUM_GREEN:
418 data[p++]=GLX_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break;
419 case WX_GL_MIN_ACCUM_BLUE:
420 data[p++]=GLX_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break;
421 case WX_GL_MIN_ACCUM_ALPHA:
422 data[p++]=GLX_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
423 default:
424 break;
425 }
426 }
427 data[p] = 0;
428
429 attribList = (int*) data;
430 }
431
432
433 Display *dpy = GDK_DISPLAY();
434
435 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
436 }
437
438 void wxGLCanvas::SwapBuffers()
439 {
440 if (m_glContext)
441 m_glContext->SwapBuffers();
442 }
443
444 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
445 {
446 }
447
448 void wxGLCanvas::SetCurrent()
449 {
450 if (m_glContext)
451 m_glContext->SetCurrent();
452 }
453
454 void wxGLCanvas::SetColour( const wxChar *colour )
455 {
456 if (m_glContext)
457 m_glContext->SetColour( colour );
458 }
459
460 void wxGLCanvas::OnInternalIdle()
461 {
462 if (m_glContext && m_exposed)
463 {
464 wxPaintEvent event( GetId() );
465 event.SetEventObject( this );
466 GetEventHandler()->ProcessEvent( event );
467
468 m_exposed = FALSE;
469 GetUpdateRegion().Clear();
470 }
471
472 wxWindow::OnInternalIdle();
473 }
474
475
476
477 //---------------------------------------------------------------------------
478 // wxGLApp
479 //---------------------------------------------------------------------------
480
481 IMPLEMENT_CLASS(wxGLApp, wxApp)
482
483 wxGLApp::~wxGLApp()
484 {
485 if (m_glVisualInfo)
486 XFree(m_glVisualInfo);
487 }
488
489 bool wxGLApp::InitGLVisual(int *attribList)
490 {
491 if (m_glVisualInfo)
492 XFree(m_glVisualInfo);
493
494 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
495
496 return m_glVisualInfo != NULL;
497 }
498
499 #endif
500 // wxUSE_GLCANVAS
501