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