]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/glcanvas.cpp
added wxLocale::GetLanguageName
[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
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
2b5b9325 209#ifndef __WXGTK20__
2b5f62a0 210static void
8b089c5e
JS
211gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
212{
2b5f62a0 213 if (g_isIdle)
8b089c5e
JS
214 wxapp_install_idle_handler();
215
216 win->m_exposed = TRUE;
217
218 win->GetUpdateRegion().Union( rect->x, rect->y,
219 rect->width, rect->height );
220}
2b5b9325 221#endif
8b089c5e
JS
222
223//-----------------------------------------------------------------------------
224// "size_allocate" of m_wxwindow
225//-----------------------------------------------------------------------------
226
2b5f62a0 227static void
8b089c5e
JS
228gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
229{
230 if (g_isIdle)
231 wxapp_install_idle_handler();
232
233 if (!win->m_hasVMT)
234 return;
235
236 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
237 event.SetEventObject( win );
238 win->GetEventHandler()->ProcessEvent( event );
239}
240
241//---------------------------------------------------------------------------
242// wxGlCanvas
243//---------------------------------------------------------------------------
244
4660d7e5 245IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
8b089c5e 246
4660d7e5 247BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
8b089c5e
JS
248 EVT_SIZE(wxGLCanvas::OnSize)
249END_EVENT_TABLE()
250
251wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
2b5f62a0
VZ
252 const wxPoint& pos, const wxSize& size,
253 long style, const wxString& name,
254 int *attribList,
255 const wxPalette& palette )
8b089c5e
JS
256{
257 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
258}
259
2b5f62a0 260wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
261 const wxGLContext *shared,
262 wxWindowID id,
2b5f62a0
VZ
263 const wxPoint& pos, const wxSize& size,
264 long style, const wxString& name,
265 int *attribList,
266 const wxPalette& palette )
267{
8b089c5e
JS
268 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
269}
270
2b5f62a0 271wxGLCanvas::wxGLCanvas( wxWindow *parent,
8b089c5e
JS
272 const wxGLCanvas *shared,
273 wxWindowID id,
2b5f62a0
VZ
274 const wxPoint& pos, const wxSize& size,
275 long style, const wxString& name,
276 int *attribList,
277 const wxPalette& palette )
278{
8b089c5e
JS
279 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
280}
281
2b5f62a0 282bool wxGLCanvas::Create( wxWindow *parent,
8b089c5e
JS
283 const wxGLContext *shared,
284 const wxGLCanvas *shared_context_of,
285 wxWindowID id,
2b5f62a0
VZ
286 const wxPoint& pos, const wxSize& size,
287 long style, const wxString& name,
288 int *attribList,
289 const wxPalette& palette)
8b089c5e
JS
290{
291 m_sharedContext = (wxGLContext*)shared; // const_cast
292 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
293 m_glContext = (wxGLContext*) NULL;
2b5f62a0 294
8b089c5e
JS
295 m_exposed = FALSE;
296 m_noExpose = TRUE;
297 m_nativeSizeEvent = TRUE;
2b5f62a0 298
a6f5aa49 299 XVisualInfo *vi = NULL;
2b5f62a0
VZ
300 if (wxTheApp->m_glVisualInfo != NULL)
301 {
302 vi = (XVisualInfo *) wxTheApp->m_glVisualInfo;
a6f5aa49 303 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
2b5f62a0
VZ
304 }
305 else
306 {
a6f5aa49
VZ
307 vi = (XVisualInfo *) ChooseGLVisual(attribList);
308 m_canFreeVi = TRUE;
309 }
310 m_vi = vi; // save for later use
2b5f62a0
VZ
311
312 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
a6f5aa49
VZ
313
314 GdkVisual *visual = gdkx_visual_get( vi->visualid );
315 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(vi->visualid), TRUE );
2b5f62a0 316
a6f5aa49
VZ
317 gtk_widget_push_colormap( colormap );
318 gtk_widget_push_visual( visual );
319
4660d7e5 320 wxWindow::Create( parent, id, pos, size, style, name );
a6f5aa49
VZ
321
322 m_glWidget = m_wxwindow;
2b5f62a0 323
2b5b9325
RR
324#ifdef __WXGTK20__
325 gtk_widget_set_double_buffered( m_glWidget, FALSE );
326#endif
327
a6f5aa49 328 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
2b5f62a0 329
a6f5aa49
VZ
330 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
331 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
332
333 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
334 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
335
336 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
337 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
338
2b5b9325 339#ifndef __WXGTK20__
a6f5aa49
VZ
340 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
341 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
2b5b9325 342#endif
2b5f62a0 343
a6f5aa49
VZ
344 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
345 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
346
347 gtk_widget_pop_visual();
348 gtk_widget_pop_colormap();
2b5f62a0 349
a6f5aa49
VZ
350 if (GTK_WIDGET_REALIZED(m_wxwindow))
351 gtk_glwindow_realized_callback( m_wxwindow, this );
2b5f62a0 352
a6f5aa49
VZ
353 if (GTK_WIDGET_MAPPED(m_wxwindow))
354 gtk_glwindow_map_callback( m_wxwindow, this );
2b5f62a0 355
a6f5aa49
VZ
356 return TRUE;
357}
358
359wxGLCanvas::~wxGLCanvas()
360{
361 XVisualInfo *vi = (XVisualInfo *) m_vi;
2b5f62a0 362
a6f5aa49
VZ
363 if (vi && m_canFreeVi) XFree( vi );
364 if (m_glContext) delete m_glContext;
365}
366
367void* wxGLCanvas::ChooseGLVisual(int *attribList)
368{
369 int data[512];
8b089c5e
JS
370 if (!attribList)
371 {
0f8d11dc
UN
372 // default settings if attriblist = 0
373 data[0] = GLX_RGBA;
374 data[1] = GLX_DOUBLEBUFFER;
e6afccba
UN
375 data[2] = GLX_DEPTH_SIZE; data[3] = 1;
376 data[4] = GLX_RED_SIZE; data[5] = 1;
377 data[6] = GLX_GREEN_SIZE; data[7] = 1;
378 data[8] = GLX_BLUE_SIZE; data[9] = 1;
379 data[10] = GLX_ALPHA_SIZE; data[11] = 0;
aff5d591 380 data[12] = None;
0f8d11dc 381
2b5f62a0 382 attribList = (int*) data;
8b089c5e
JS
383 }
384 else
385 {
83918ccc 386 int arg=0, p=0;
2b5f62a0 387
0f8d11dc 388 while( (attribList[arg]!=0) && (p<510) )
8b089c5e
JS
389 {
390 switch( attribList[arg++] )
391 {
392 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
0f8d11dc
UN
393 case WX_GL_BUFFER_SIZE:
394 data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
395 case WX_GL_LEVEL:
396 data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break;
8b089c5e 397 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
0f8d11dc
UN
398 case WX_GL_STEREO: data[p++] = GLX_STEREO; break;
399 case WX_GL_AUX_BUFFERS:
400 data[p++]=GLX_AUX_BUFFERS; data[p++]=attribList[arg++]; break;
8b089c5e
JS
401 case WX_GL_MIN_RED:
402 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
403 case WX_GL_MIN_GREEN:
404 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
405 case WX_GL_MIN_BLUE:
406 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
0f8d11dc
UN
407 case WX_GL_MIN_ALPHA:
408 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
2b5f62a0 409 case WX_GL_DEPTH_SIZE:
0f8d11dc 410 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
2b5f62a0 411 case WX_GL_STENCIL_SIZE:
0f8d11dc
UN
412 data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
413 case WX_GL_MIN_ACCUM_RED:
414 data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
415 case WX_GL_MIN_ACCUM_GREEN:
416 data[p++]=GLX_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break;
417 case WX_GL_MIN_ACCUM_BLUE:
418 data[p++]=GLX_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break;
419 case WX_GL_MIN_ACCUM_ALPHA:
420 data[p++]=GLX_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
8b089c5e
JS
421 default:
422 break;
423 }
2b5f62a0
VZ
424 }
425 data[p] = 0;
8b089c5e
JS
426
427 attribList = (int*) data;
428 }
2b5f62a0
VZ
429
430
8b089c5e 431 Display *dpy = GDK_DISPLAY();
2b5f62a0 432
a6f5aa49 433 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
8b089c5e
JS
434}
435
436void wxGLCanvas::SwapBuffers()
437{
2b5f62a0
VZ
438 if (m_glContext)
439 m_glContext->SwapBuffers();
8b089c5e
JS
440}
441
442void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
443{
8b089c5e
JS
444}
445
446void wxGLCanvas::SetCurrent()
447{
2b5f62a0
VZ
448 if (m_glContext)
449 m_glContext->SetCurrent();
8b089c5e
JS
450}
451
2b5f62a0 452void wxGLCanvas::SetColour( const wxChar *colour )
8b089c5e 453{
2b5f62a0
VZ
454 if (m_glContext)
455 m_glContext->SetColour( colour );
8b089c5e
JS
456}
457
458void wxGLCanvas::OnInternalIdle()
459{
460 if (m_glContext && m_exposed)
461 {
462 wxPaintEvent event( GetId() );
463 event.SetEventObject( this );
464 GetEventHandler()->ProcessEvent( event );
465
466 m_exposed = FALSE;
467 GetUpdateRegion().Clear();
468 }
2b5f62a0 469
8b089c5e
JS
470 wxWindow::OnInternalIdle();
471}
472
a6f5aa49
VZ
473
474
475//---------------------------------------------------------------------------
476// wxGLApp
477//---------------------------------------------------------------------------
478
479IMPLEMENT_CLASS(wxGLApp, wxApp)
2b5f62a0 480
a6f5aa49
VZ
481wxGLApp::~wxGLApp()
482{
2b5f62a0
VZ
483 if (m_glVisualInfo)
484 XFree(m_glVisualInfo);
a6f5aa49
VZ
485}
486
487bool wxGLApp::InitGLVisual(int *attribList)
488{
2b5f62a0
VZ
489 if (m_glVisualInfo)
490 XFree(m_glVisualInfo);
491
a6f5aa49 492 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
2b5f62a0
VZ
493
494 return m_glVisualInfo != NULL;
a6f5aa49
VZ
495}
496
8b089c5e
JS
497#endif
498 // wxUSE_GLCANVAS
499