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