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