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