]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/glcanvas.cpp
support full 32bit range in wxGauge
[wxWidgets.git] / src / gtk / glcanvas.cpp
CommitLineData
8b089c5e 1/////////////////////////////////////////////////////////////////////////////
2b5f62a0 2// Name: gtk/glcanvas.cpp
8b089c5e
JS
3// Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4// Author: Robert Roebling
5// Modified by:
6// Created: 17/08/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
2b5f62a0 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
JS
132{
133 float r = 0.0;
134 float g = 0.0;
135 float b = 0.0;
136 wxColour *col = wxTheColourDatabase->FindColour(colour);
137 if (col)
138 {
139 r = (float)(col->Red()/256.0);
140 g = (float)(col->Green()/256.0);
141 b = (float)(col->Blue()/256.0);
142 glColor3f( r, g, b);
143 }
144}
145
146void wxGLContext::SetupPixelFormat()
147{
148}
149
150void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
151{
152}
153
154wxPalette wxGLContext::CreateDefaultPalette()
155{
156 return wxNullPalette;
157}
158
159//-----------------------------------------------------------------------------
160// "realize" from m_wxwindow
161//-----------------------------------------------------------------------------
162
163static gint
164gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
165{
166 wxGLContext *share= win->m_sharedContext;
167 if (share==NULL && win->m_sharedContextOf) share=win->m_sharedContextOf->GetContext();
168
169 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
170
171 return FALSE;
172}
173
174//-----------------------------------------------------------------------------
175// "map" from m_wxwindow
176//-----------------------------------------------------------------------------
177
178static gint
179gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
180{
181 if (win->m_glContext/* && win->m_exposed*/)
182 {
183 wxPaintEvent event( win->GetId() );
184 event.SetEventObject( win );
185 win->GetEventHandler()->ProcessEvent( event );
186
187 win->m_exposed = FALSE;
188 win->GetUpdateRegion().Clear();
189 }
190
191 return FALSE;
192}
193
194//-----------------------------------------------------------------------------
195// "expose_event" of m_wxwindow
196//-----------------------------------------------------------------------------
197
2b5f62a0 198static void
8b089c5e
JS
199gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
200{
2b5f62a0 201 if (g_isIdle)
8b089c5e
JS
202 wxapp_install_idle_handler();
203
204 win->m_exposed = TRUE;
205
206 win->GetUpdateRegion().Union( gdk_event->area.x,
207 gdk_event->area.y,
208 gdk_event->area.width,
209 gdk_event->area.height );
210}
211
212//-----------------------------------------------------------------------------
213// "draw" of m_wxwindow
214//-----------------------------------------------------------------------------
215
2b5b9325 216#ifndef __WXGTK20__
2b5f62a0 217static void
8b089c5e
JS
218gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
219{
2b5f62a0 220 if (g_isIdle)
8b089c5e
JS
221 wxapp_install_idle_handler();
222
223 win->m_exposed = TRUE;
224
225 win->GetUpdateRegion().Union( rect->x, rect->y,
226 rect->width, rect->height );
227}
2b5b9325 228#endif
8b089c5e
JS
229
230//-----------------------------------------------------------------------------
231// "size_allocate" of m_wxwindow
232//-----------------------------------------------------------------------------
233
2b5f62a0 234static void
8b089c5e
JS
235gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
236{
237 if (g_isIdle)
238 wxapp_install_idle_handler();
239
240 if (!win->m_hasVMT)
241 return;
242
243 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
244 event.SetEventObject( win );
245 win->GetEventHandler()->ProcessEvent( event );
246}
247
248//---------------------------------------------------------------------------
249// wxGlCanvas
250//---------------------------------------------------------------------------
251
4660d7e5 252IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
8b089c5e 253
4660d7e5 254BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
8b089c5e
JS
255 EVT_SIZE(wxGLCanvas::OnSize)
256END_EVENT_TABLE()
257
258wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
2b5f62a0
VZ
259 const wxPoint& pos, const wxSize& size,
260 long style, const wxString& name,
261 int *attribList,
262 const wxPalette& palette )
8b089c5e
JS
263{
264 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
265}
266
2b5f62a0 267wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
268 const wxGLContext *shared,
269 wxWindowID id,
2b5f62a0
VZ
270 const wxPoint& pos, const wxSize& size,
271 long style, const wxString& name,
272 int *attribList,
273 const wxPalette& palette )
274{
8b089c5e
JS
275 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
276}
277
2b5f62a0 278wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
279 const wxGLCanvas *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, NULL, shared, id, pos, size, style, name, attribList, palette );
287}
288
2b5f62a0 289bool wxGLCanvas::Create( wxWindow *parent,
8b089c5e
JS
290 const wxGLContext *shared,
291 const wxGLCanvas *shared_context_of,
292 wxWindowID id,
2b5f62a0
VZ
293 const wxPoint& pos, const wxSize& size,
294 long style, const wxString& name,
295 int *attribList,
296 const wxPalette& palette)
8b089c5e
JS
297{
298 m_sharedContext = (wxGLContext*)shared; // const_cast
299 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
300 m_glContext = (wxGLContext*) NULL;
2b5f62a0 301
8b089c5e
JS
302 m_exposed = FALSE;
303 m_noExpose = TRUE;
304 m_nativeSizeEvent = TRUE;
2b5f62a0 305
a6f5aa49 306 XVisualInfo *vi = NULL;
2b5f62a0
VZ
307 if (wxTheApp->m_glVisualInfo != NULL)
308 {
309 vi = (XVisualInfo *) wxTheApp->m_glVisualInfo;
a6f5aa49 310 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
2b5f62a0
VZ
311 }
312 else
313 {
a6f5aa49
VZ
314 vi = (XVisualInfo *) ChooseGLVisual(attribList);
315 m_canFreeVi = TRUE;
316 }
317 m_vi = vi; // save for later use
2b5f62a0
VZ
318
319 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
a6f5aa49
VZ
320
321 GdkVisual *visual = gdkx_visual_get( vi->visualid );
322 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(vi->visualid), TRUE );
2b5f62a0 323
a6f5aa49
VZ
324 gtk_widget_push_colormap( colormap );
325 gtk_widget_push_visual( visual );
326
4660d7e5 327 wxWindow::Create( parent, id, pos, size, style, name );
a6f5aa49
VZ
328
329 m_glWidget = m_wxwindow;
2b5f62a0 330
2b5b9325
RR
331#ifdef __WXGTK20__
332 gtk_widget_set_double_buffered( m_glWidget, FALSE );
333#endif
334
a6f5aa49 335 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
2b5f62a0 336
a6f5aa49
VZ
337 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
338 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
339
340 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
341 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
342
343 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
344 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
345
2b5b9325 346#ifndef __WXGTK20__
a6f5aa49
VZ
347 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
348 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
2b5b9325 349#endif
2b5f62a0 350
a6f5aa49
VZ
351 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
352 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
353
354 gtk_widget_pop_visual();
355 gtk_widget_pop_colormap();
2b5f62a0 356
a6f5aa49
VZ
357 if (GTK_WIDGET_REALIZED(m_wxwindow))
358 gtk_glwindow_realized_callback( m_wxwindow, this );
2b5f62a0 359
a6f5aa49
VZ
360 if (GTK_WIDGET_MAPPED(m_wxwindow))
361 gtk_glwindow_map_callback( m_wxwindow, this );
2b5f62a0 362
a6f5aa49
VZ
363 return TRUE;
364}
365
366wxGLCanvas::~wxGLCanvas()
367{
368 XVisualInfo *vi = (XVisualInfo *) m_vi;
2b5f62a0 369
a6f5aa49
VZ
370 if (vi && m_canFreeVi) XFree( vi );
371 if (m_glContext) delete m_glContext;
372}
373
374void* wxGLCanvas::ChooseGLVisual(int *attribList)
375{
376 int data[512];
8b089c5e
JS
377 if (!attribList)
378 {
0f8d11dc
UN
379 // default settings if attriblist = 0
380 data[0] = GLX_RGBA;
381 data[1] = GLX_DOUBLEBUFFER;
e6afccba
UN
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;
aff5d591 387 data[12] = None;
0f8d11dc 388
2b5f62a0 389 attribList = (int*) data;
8b089c5e
JS
390 }
391 else
392 {
83918ccc 393 int arg=0, p=0;
2b5f62a0 394
0f8d11dc 395 while( (attribList[arg]!=0) && (p<510) )
8b089c5e
JS
396 {
397 switch( attribList[arg++] )
398 {
399 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
0f8d11dc
UN
400 case WX_GL_BUFFER_SIZE:
401 data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
402 case WX_GL_LEVEL:
403 data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break;
8b089c5e 404 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
0f8d11dc
UN
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;
8b089c5e
JS
408 case WX_GL_MIN_RED:
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;
412 case WX_GL_MIN_BLUE:
413 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
0f8d11dc
UN
414 case WX_GL_MIN_ALPHA:
415 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
2b5f62a0 416 case WX_GL_DEPTH_SIZE:
0f8d11dc 417 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
2b5f62a0 418 case WX_GL_STENCIL_SIZE:
0f8d11dc
UN
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;
8b089c5e
JS
428 default:
429 break;
430 }
2b5f62a0
VZ
431 }
432 data[p] = 0;
8b089c5e
JS
433
434 attribList = (int*) data;
435 }
2b5f62a0
VZ
436
437
8b089c5e 438 Display *dpy = GDK_DISPLAY();
2b5f62a0 439
a6f5aa49 440 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
8b089c5e
JS
441}
442
443void wxGLCanvas::SwapBuffers()
444{
2b5f62a0
VZ
445 if (m_glContext)
446 m_glContext->SwapBuffers();
8b089c5e
JS
447}
448
449void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
450{
8b089c5e
JS
451}
452
453void wxGLCanvas::SetCurrent()
454{
2b5f62a0
VZ
455 if (m_glContext)
456 m_glContext->SetCurrent();
8b089c5e
JS
457}
458
2b5f62a0 459void wxGLCanvas::SetColour( const wxChar *colour )
8b089c5e 460{
2b5f62a0
VZ
461 if (m_glContext)
462 m_glContext->SetColour( colour );
8b089c5e
JS
463}
464
465void wxGLCanvas::OnInternalIdle()
466{
467 if (m_glContext && m_exposed)
468 {
469 wxPaintEvent event( GetId() );
470 event.SetEventObject( this );
471 GetEventHandler()->ProcessEvent( event );
472
473 m_exposed = FALSE;
474 GetUpdateRegion().Clear();
475 }
2b5f62a0 476
8b089c5e
JS
477 wxWindow::OnInternalIdle();
478}
479
a6f5aa49
VZ
480
481
482//---------------------------------------------------------------------------
483// wxGLApp
484//---------------------------------------------------------------------------
485
486IMPLEMENT_CLASS(wxGLApp, wxApp)
2b5f62a0 487
a6f5aa49
VZ
488wxGLApp::~wxGLApp()
489{
2b5f62a0
VZ
490 if (m_glVisualInfo)
491 XFree(m_glVisualInfo);
a6f5aa49
VZ
492}
493
494bool wxGLApp::InitGLVisual(int *attribList)
495{
2b5f62a0
VZ
496 if (m_glVisualInfo)
497 XFree(m_glVisualInfo);
498
a6f5aa49 499 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
2b5f62a0
VZ
500
501 return m_glVisualInfo != NULL;
a6f5aa49
VZ
502}
503
8b089c5e
JS
504#endif
505 // wxUSE_GLCANVAS
506