]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/glcanvas.cpp
Added $Id keyword, a bit of cleanup and also some backwards
[wxWidgets.git] / src / gtk / 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 // VZ: apparently in some cases we're called twice -- no idea why,
164 // but a check doesn't hurt
165 if ( !win->m_glContext )
166 {
167 wxGLContext *share = win->m_sharedContext;
168 if ( !share && win->m_sharedContextOf )
169 share = win->m_sharedContextOf->GetContext();
170
171 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
172 }
173
174 return FALSE;
175 }
176
177 //-----------------------------------------------------------------------------
178 // "map" from m_wxwindow
179 //-----------------------------------------------------------------------------
180
181 static gint
182 gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
183 {
184 if (win->m_glContext/* && win->m_exposed*/)
185 {
186 wxPaintEvent event( win->GetId() );
187 event.SetEventObject( win );
188 win->GetEventHandler()->ProcessEvent( event );
189
190 win->m_exposed = FALSE;
191 win->GetUpdateRegion().Clear();
192 }
193
194 return FALSE;
195 }
196
197 //-----------------------------------------------------------------------------
198 // "expose_event" of m_wxwindow
199 //-----------------------------------------------------------------------------
200
201 static void
202 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
203 {
204 if (g_isIdle)
205 wxapp_install_idle_handler();
206
207 win->m_exposed = TRUE;
208
209 win->GetUpdateRegion().Union( gdk_event->area.x,
210 gdk_event->area.y,
211 gdk_event->area.width,
212 gdk_event->area.height );
213 }
214
215 //-----------------------------------------------------------------------------
216 // "draw" of m_wxwindow
217 //-----------------------------------------------------------------------------
218
219 #ifndef __WXGTK20__
220 static void
221 gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
222 {
223 if (g_isIdle)
224 wxapp_install_idle_handler();
225
226 win->m_exposed = TRUE;
227
228 win->GetUpdateRegion().Union( rect->x, rect->y,
229 rect->width, rect->height );
230 }
231 #endif
232
233 //-----------------------------------------------------------------------------
234 // "size_allocate" of m_wxwindow
235 //-----------------------------------------------------------------------------
236
237 static void
238 gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
239 {
240 if (g_isIdle)
241 wxapp_install_idle_handler();
242
243 if (!win->m_hasVMT)
244 return;
245
246 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
247 event.SetEventObject( win );
248 win->GetEventHandler()->ProcessEvent( event );
249 }
250
251 //---------------------------------------------------------------------------
252 // wxGlCanvas
253 //---------------------------------------------------------------------------
254
255 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
256
257 BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
258 EVT_SIZE(wxGLCanvas::OnSize)
259 END_EVENT_TABLE()
260
261 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
262 const wxPoint& pos, const wxSize& size,
263 long style, const wxString& name,
264 int *attribList,
265 const wxPalette& palette )
266 {
267 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
268 }
269
270 wxGLCanvas::wxGLCanvas( wxWindow *parent,
271 const wxGLContext *shared,
272 wxWindowID id,
273 const wxPoint& pos, const wxSize& size,
274 long style, const wxString& name,
275 int *attribList,
276 const wxPalette& palette )
277 {
278 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
279 }
280
281 wxGLCanvas::wxGLCanvas( wxWindow *parent,
282 const wxGLCanvas *shared,
283 wxWindowID id,
284 const wxPoint& pos, const wxSize& size,
285 long style, const wxString& name,
286 int *attribList,
287 const wxPalette& palette )
288 {
289 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
290 }
291
292 bool wxGLCanvas::Create( wxWindow *parent,
293 const wxGLContext *shared,
294 const wxGLCanvas *shared_context_of,
295 wxWindowID id,
296 const wxPoint& pos, const wxSize& size,
297 long style, const wxString& name,
298 int *attribList,
299 const wxPalette& palette)
300 {
301 m_sharedContext = (wxGLContext*)shared; // const_cast
302 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
303 m_glContext = (wxGLContext*) NULL;
304
305 m_exposed = FALSE;
306 m_noExpose = TRUE;
307 m_nativeSizeEvent = TRUE;
308
309 XVisualInfo *vi = NULL;
310 if (wxTheApp->m_glVisualInfo != NULL)
311 {
312 vi = (XVisualInfo *) wxTheApp->m_glVisualInfo;
313 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
314 }
315 else
316 {
317 vi = (XVisualInfo *) ChooseGLVisual(attribList);
318 m_canFreeVi = TRUE;
319 }
320 m_vi = vi; // save for later use
321
322 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
323
324 GdkVisual *visual = gdkx_visual_get( vi->visualid );
325 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(vi->visualid), TRUE );
326
327 gtk_widget_push_colormap( colormap );
328 gtk_widget_push_visual( visual );
329
330 wxWindow::Create( parent, id, pos, size, style, name );
331
332 m_glWidget = m_wxwindow;
333
334 #ifdef __WXGTK20__
335 gtk_widget_set_double_buffered( m_glWidget, FALSE );
336 #endif
337
338 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
339
340 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
341 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
342
343 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
344 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
345
346 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
347 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
348
349 #ifndef __WXGTK20__
350 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
351 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
352 #endif
353
354 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
355 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
356
357 gtk_widget_pop_visual();
358 gtk_widget_pop_colormap();
359
360 if (GTK_WIDGET_REALIZED(m_wxwindow))
361 gtk_glwindow_realized_callback( m_wxwindow, this );
362
363 if (GTK_WIDGET_MAPPED(m_wxwindow))
364 gtk_glwindow_map_callback( m_wxwindow, this );
365
366 return TRUE;
367 }
368
369 wxGLCanvas::~wxGLCanvas()
370 {
371 XVisualInfo *vi = (XVisualInfo *) m_vi;
372
373 if (vi && m_canFreeVi) XFree( vi );
374 delete m_glContext;
375 }
376
377 void* wxGLCanvas::ChooseGLVisual(int *attribList)
378 {
379 int data[512];
380 if (!attribList)
381 {
382 // default settings if attriblist = 0
383 data[0] = GLX_RGBA;
384 data[1] = GLX_DOUBLEBUFFER;
385 data[2] = GLX_DEPTH_SIZE; data[3] = 1;
386 data[4] = GLX_RED_SIZE; data[5] = 1;
387 data[6] = GLX_GREEN_SIZE; data[7] = 1;
388 data[8] = GLX_BLUE_SIZE; data[9] = 1;
389 data[10] = GLX_ALPHA_SIZE; data[11] = 0;
390 data[12] = None;
391
392 attribList = (int*) data;
393 }
394 else
395 {
396 int arg=0, p=0;
397
398 while( (attribList[arg]!=0) && (p<510) )
399 {
400 switch( attribList[arg++] )
401 {
402 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
403 case WX_GL_BUFFER_SIZE:
404 data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
405 case WX_GL_LEVEL:
406 data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break;
407 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
408 case WX_GL_STEREO: data[p++] = GLX_STEREO; break;
409 case WX_GL_AUX_BUFFERS:
410 data[p++]=GLX_AUX_BUFFERS; data[p++]=attribList[arg++]; break;
411 case WX_GL_MIN_RED:
412 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
413 case WX_GL_MIN_GREEN:
414 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
415 case WX_GL_MIN_BLUE:
416 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
417 case WX_GL_MIN_ALPHA:
418 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
419 case WX_GL_DEPTH_SIZE:
420 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
421 case WX_GL_STENCIL_SIZE:
422 data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
423 case WX_GL_MIN_ACCUM_RED:
424 data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
425 case WX_GL_MIN_ACCUM_GREEN:
426 data[p++]=GLX_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break;
427 case WX_GL_MIN_ACCUM_BLUE:
428 data[p++]=GLX_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break;
429 case WX_GL_MIN_ACCUM_ALPHA:
430 data[p++]=GLX_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
431 default:
432 break;
433 }
434 }
435 data[p] = 0;
436
437 attribList = (int*) data;
438 }
439
440
441 Display *dpy = GDK_DISPLAY();
442
443 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
444 }
445
446 void wxGLCanvas::SwapBuffers()
447 {
448 if (m_glContext)
449 m_glContext->SwapBuffers();
450 }
451
452 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
453 {
454 }
455
456 void wxGLCanvas::SetCurrent()
457 {
458 if (m_glContext)
459 m_glContext->SetCurrent();
460 }
461
462 void wxGLCanvas::SetColour( const wxChar *colour )
463 {
464 if (m_glContext)
465 m_glContext->SetColour( colour );
466 }
467
468 void wxGLCanvas::OnInternalIdle()
469 {
470 if (m_glContext && m_exposed)
471 {
472 wxPaintEvent event( GetId() );
473 event.SetEventObject( this );
474 GetEventHandler()->ProcessEvent( event );
475
476 m_exposed = FALSE;
477 GetUpdateRegion().Clear();
478 }
479
480 wxWindow::OnInternalIdle();
481 }
482
483
484
485 //---------------------------------------------------------------------------
486 // wxGLApp
487 //---------------------------------------------------------------------------
488
489 IMPLEMENT_CLASS(wxGLApp, wxApp)
490
491 wxGLApp::~wxGLApp()
492 {
493 if (m_glVisualInfo)
494 XFree(m_glVisualInfo);
495 }
496
497 bool wxGLApp::InitGLVisual(int *attribList)
498 {
499 if (m_glVisualInfo)
500 XFree(m_glVisualInfo);
501
502 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
503
504 return m_glVisualInfo != NULL;
505 }
506
507 #endif
508 // wxUSE_GLCANVAS
509