]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/glcanvas.cpp
Added assert (WinCE doesn't report checked status correctly)
[wxWidgets.git] / src / gtk / 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
160static gint
161gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
162{
4230303c
VZ
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();
8b089c5e 170
4230303c
VZ
171 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
172 }
8b089c5e
JS
173
174 return FALSE;
175}
176
177//-----------------------------------------------------------------------------
178// "map" from m_wxwindow
179//-----------------------------------------------------------------------------
180
181static gint
182gtk_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
2b5f62a0 201static void
8b089c5e
JS
202gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
203{
2b5f62a0 204 if (g_isIdle)
8b089c5e
JS
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
2b5b9325 219#ifndef __WXGTK20__
2b5f62a0 220static void
8b089c5e
JS
221gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
222{
2b5f62a0 223 if (g_isIdle)
8b089c5e
JS
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}
2b5b9325 231#endif
8b089c5e
JS
232
233//-----------------------------------------------------------------------------
234// "size_allocate" of m_wxwindow
235//-----------------------------------------------------------------------------
236
2b5f62a0 237static void
8b089c5e
JS
238gtk_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
4660d7e5 255IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
8b089c5e 256
4660d7e5 257BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
8b089c5e
JS
258 EVT_SIZE(wxGLCanvas::OnSize)
259END_EVENT_TABLE()
260
261wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
2b5f62a0
VZ
262 const wxPoint& pos, const wxSize& size,
263 long style, const wxString& name,
264 int *attribList,
265 const wxPalette& palette )
8b089c5e
JS
266{
267 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
268}
269
2b5f62a0 270wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
271 const wxGLContext *shared,
272 wxWindowID id,
2b5f62a0
VZ
273 const wxPoint& pos, const wxSize& size,
274 long style, const wxString& name,
275 int *attribList,
276 const wxPalette& palette )
277{
8b089c5e
JS
278 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
279}
280
2b5f62a0 281wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
282 const wxGLCanvas *shared,
283 wxWindowID id,
2b5f62a0
VZ
284 const wxPoint& pos, const wxSize& size,
285 long style, const wxString& name,
286 int *attribList,
287 const wxPalette& palette )
288{
8b089c5e
JS
289 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
290}
291
2b5f62a0 292bool wxGLCanvas::Create( wxWindow *parent,
8b089c5e
JS
293 const wxGLContext *shared,
294 const wxGLCanvas *shared_context_of,
295 wxWindowID id,
2b5f62a0
VZ
296 const wxPoint& pos, const wxSize& size,
297 long style, const wxString& name,
298 int *attribList,
299 const wxPalette& palette)
8b089c5e
JS
300{
301 m_sharedContext = (wxGLContext*)shared; // const_cast
302 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
303 m_glContext = (wxGLContext*) NULL;
2b5f62a0 304
8b089c5e
JS
305 m_exposed = FALSE;
306 m_noExpose = TRUE;
307 m_nativeSizeEvent = TRUE;
2b5f62a0 308
a6f5aa49 309 XVisualInfo *vi = NULL;
2b5f62a0
VZ
310 if (wxTheApp->m_glVisualInfo != NULL)
311 {
312 vi = (XVisualInfo *) wxTheApp->m_glVisualInfo;
a6f5aa49 313 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
2b5f62a0
VZ
314 }
315 else
316 {
a6f5aa49
VZ
317 vi = (XVisualInfo *) ChooseGLVisual(attribList);
318 m_canFreeVi = TRUE;
319 }
320 m_vi = vi; // save for later use
2b5f62a0
VZ
321
322 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
a6f5aa49
VZ
323
324 GdkVisual *visual = gdkx_visual_get( vi->visualid );
645a8fab 325 GdkColormap *colormap = gdk_colormap_new( visual, TRUE );
2b5f62a0 326
a6f5aa49
VZ
327 gtk_widget_push_colormap( colormap );
328 gtk_widget_push_visual( visual );
329
4660d7e5 330 wxWindow::Create( parent, id, pos, size, style, name );
a6f5aa49
VZ
331
332 m_glWidget = m_wxwindow;
2b5f62a0 333
2b5b9325
RR
334#ifdef __WXGTK20__
335 gtk_widget_set_double_buffered( m_glWidget, FALSE );
336#endif
337
a6f5aa49 338 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
2b5f62a0 339
a6f5aa49
VZ
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
2b5b9325 349#ifndef __WXGTK20__
a6f5aa49
VZ
350 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
351 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
2b5b9325 352#endif
2b5f62a0 353
a6f5aa49
VZ
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();
2b5f62a0 359
a6f5aa49
VZ
360 if (GTK_WIDGET_REALIZED(m_wxwindow))
361 gtk_glwindow_realized_callback( m_wxwindow, this );
2b5f62a0 362
a6f5aa49
VZ
363 if (GTK_WIDGET_MAPPED(m_wxwindow))
364 gtk_glwindow_map_callback( m_wxwindow, this );
2b5f62a0 365
a6f5aa49
VZ
366 return TRUE;
367}
368
369wxGLCanvas::~wxGLCanvas()
370{
371 XVisualInfo *vi = (XVisualInfo *) m_vi;
2b5f62a0 372
a6f5aa49 373 if (vi && m_canFreeVi) XFree( vi );
4230303c 374 delete m_glContext;
a6f5aa49
VZ
375}
376
377void* wxGLCanvas::ChooseGLVisual(int *attribList)
378{
379 int data[512];
8b089c5e
JS
380 if (!attribList)
381 {
0f8d11dc
UN
382 // default settings if attriblist = 0
383 data[0] = GLX_RGBA;
384 data[1] = GLX_DOUBLEBUFFER;
e6afccba
UN
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;
aff5d591 390 data[12] = None;
0f8d11dc 391
2b5f62a0 392 attribList = (int*) data;
8b089c5e
JS
393 }
394 else
395 {
83918ccc 396 int arg=0, p=0;
2b5f62a0 397
0f8d11dc 398 while( (attribList[arg]!=0) && (p<510) )
8b089c5e
JS
399 {
400 switch( attribList[arg++] )
401 {
402 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
0f8d11dc
UN
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;
8b089c5e 407 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
0f8d11dc
UN
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;
8b089c5e
JS
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;
0f8d11dc
UN
417 case WX_GL_MIN_ALPHA:
418 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
2b5f62a0 419 case WX_GL_DEPTH_SIZE:
0f8d11dc 420 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
2b5f62a0 421 case WX_GL_STENCIL_SIZE:
0f8d11dc
UN
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;
8b089c5e
JS
431 default:
432 break;
433 }
2b5f62a0
VZ
434 }
435 data[p] = 0;
8b089c5e
JS
436
437 attribList = (int*) data;
438 }
2b5f62a0
VZ
439
440
8b089c5e 441 Display *dpy = GDK_DISPLAY();
2b5f62a0 442
a6f5aa49 443 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
8b089c5e
JS
444}
445
446void wxGLCanvas::SwapBuffers()
447{
2b5f62a0
VZ
448 if (m_glContext)
449 m_glContext->SwapBuffers();
8b089c5e
JS
450}
451
452void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
453{
8b089c5e
JS
454}
455
456void wxGLCanvas::SetCurrent()
457{
2b5f62a0
VZ
458 if (m_glContext)
459 m_glContext->SetCurrent();
8b089c5e
JS
460}
461
2b5f62a0 462void wxGLCanvas::SetColour( const wxChar *colour )
8b089c5e 463{
2b5f62a0
VZ
464 if (m_glContext)
465 m_glContext->SetColour( colour );
8b089c5e
JS
466}
467
468void 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 }
2b5f62a0 479
8b089c5e
JS
480 wxWindow::OnInternalIdle();
481}
482
a6f5aa49
VZ
483
484
485//---------------------------------------------------------------------------
486// wxGLApp
487//---------------------------------------------------------------------------
488
489IMPLEMENT_CLASS(wxGLApp, wxApp)
2b5f62a0 490
a6f5aa49
VZ
491wxGLApp::~wxGLApp()
492{
2b5f62a0
VZ
493 if (m_glVisualInfo)
494 XFree(m_glVisualInfo);
a6f5aa49
VZ
495}
496
497bool wxGLApp::InitGLVisual(int *attribList)
498{
2b5f62a0
VZ
499 if (m_glVisualInfo)
500 XFree(m_glVisualInfo);
501
a6f5aa49 502 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
2b5f62a0
VZ
503
504 return m_glVisualInfo != NULL;
a6f5aa49
VZ
505}
506
8b089c5e
JS
507#endif
508 // wxUSE_GLCANVAS
509