]> git.saurik.com Git - wxWidgets.git/blame - utils/glcanvas/gtk/glcanvas.cpp
last line missing
[wxWidgets.git] / utils / glcanvas / gtk / glcanvas.cpp
CommitLineData
9d3221ab
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: glcanvas.cpp
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
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "glcanvas.h"
14#endif
15
bbe0af5b 16#include "glcanvas.h"
9d3221ab
RR
17
18#include "wx/frame.h"
19#include "wx/colour.h"
bbe0af5b
RR
20#include "wx/module.h"
21#include "wx/app.h"
22
aae24d21 23extern "C" {
bbe0af5b
RR
24#include "gtk/gtk.h"
25#include "gdk/gdk.h"
bbe0af5b
RR
26#include "gdk/gdkx.h"
27}
28
aae24d21
RR
29#include "wx/gtk/win_gtk.h"
30
bbe0af5b 31//---------------------------------------------------------------------------
aae24d21 32// global data
bbe0af5b
RR
33//---------------------------------------------------------------------------
34
aae24d21 35XVisualInfo *g_vi = (XVisualInfo*) NULL;
9d3221ab 36
54517652
RR
37//-----------------------------------------------------------------------------
38// idle system
39//-----------------------------------------------------------------------------
40
41extern void wxapp_install_idle_handler();
42extern bool g_isIdle;
43
9d3221ab
RR
44//---------------------------------------------------------------------------
45// wxGLContext
46//---------------------------------------------------------------------------
47
48IMPLEMENT_CLASS(wxGLContext,wxObject)
49
50wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
51{
bbe0af5b 52 m_window = win;
4b59bea3
RR
53 m_widget = win->m_wxwindow;
54
55 wxGLCanvas *gc = (wxGLCanvas*) win;
56 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
57
58 wxCHECK_RET( vi, "invalid visual for OpenGl" );
bbe0af5b 59
4b59bea3 60 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
9d3221ab 61
aae24d21 62 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
9d3221ab
RR
63}
64
089e55d6
RR
65wxGLContext::wxGLContext(
66 bool WXUNUSED(isRGB), wxWindow *win,
67 const wxPalette& WXUNUSED(palette),
68 const wxGLContext *other /* for sharing display lists */
69)
70{
71 m_window = win;
4b59bea3
RR
72 m_widget = win->m_wxwindow;
73
74 wxGLCanvas *gc = (wxGLCanvas*) win;
75 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
76
77 wxCHECK_RET( vi, "invalid visual for OpenGl" );
089e55d6
RR
78
79 if( other != 0 )
4b59bea3 80 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, other->m_glContext, GL_TRUE );
089e55d6 81 else
4b59bea3 82 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
089e55d6
RR
83
84 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
85}
86
9d3221ab
RR
87wxGLContext::~wxGLContext()
88{
aae24d21 89 if (!m_glContext) return;
9d3221ab 90
aae24d21
RR
91 if (m_glContext == glXGetCurrentContext())
92 {
93 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
bbe0af5b 94 }
aae24d21
RR
95
96 glXDestroyContext( GDK_DISPLAY(), m_glContext );
9d3221ab
RR
97}
98
99void wxGLContext::SwapBuffers()
100{
bbe0af5b
RR
101 if (m_glContext)
102 {
da048e3d 103 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
147bc491 104 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
bbe0af5b 105 }
9d3221ab
RR
106}
107
108void wxGLContext::SetCurrent()
109{
bbe0af5b
RR
110 if (m_glContext)
111 {
da048e3d 112 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
147bc491 113 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
bbe0af5b 114 }
9d3221ab
RR
115}
116
117void wxGLContext::SetColour(const char *colour)
118{
bbe0af5b
RR
119 float r = 0.0;
120 float g = 0.0;
121 float b = 0.0;
122 wxColour *col = wxTheColourDatabase->FindColour(colour);
123 if (col)
124 {
125 r = (float)(col->Red()/256.0);
126 g = (float)(col->Green()/256.0);
127 b = (float)(col->Blue()/256.0);
128 glColor3f( r, g, b);
129 }
9d3221ab
RR
130}
131
132void wxGLContext::SetupPixelFormat()
133{
134}
135
136void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
137{
138}
139
140wxPalette wxGLContext::CreateDefaultPalette()
141{
bbe0af5b 142 return wxNullPalette;
9d3221ab
RR
143}
144
1d61ebac
RR
145//-----------------------------------------------------------------------------
146// "realize" from m_wxwindow
147//-----------------------------------------------------------------------------
148
149static gint
150gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
151{
147bc491 152 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, win->m_sharedContext );
1d61ebac
RR
153
154 return FALSE;
155}
156
7941ba11
RR
157//-----------------------------------------------------------------------------
158// "map" from m_wxwindow
159//-----------------------------------------------------------------------------
160
161static gint
162gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
163{
164 if (win->m_glContext/* && win->m_exposed*/)
165 {
166 wxPaintEvent event( win->GetId() );
167 event.SetEventObject( win );
168 win->GetEventHandler()->ProcessEvent( event );
169
170 win->m_exposed = FALSE;
171 win->GetUpdateRegion().Clear();
172 }
173
174 return FALSE;
175}
176
147bc491
RR
177//-----------------------------------------------------------------------------
178// "expose_event" of m_wxwindow
179//-----------------------------------------------------------------------------
180
181static void
182gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
183{
54517652
RR
184 if (g_isIdle)
185 wxapp_install_idle_handler();
186
147bc491
RR
187 win->m_exposed = TRUE;
188
189 win->GetUpdateRegion().Union( gdk_event->area.x,
190 gdk_event->area.y,
191 gdk_event->area.width,
192 gdk_event->area.height );
193}
194
195//-----------------------------------------------------------------------------
196// "draw" of m_wxwindow
197//-----------------------------------------------------------------------------
198
199static void
200gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
201{
54517652
RR
202 if (g_isIdle)
203 wxapp_install_idle_handler();
204
147bc491
RR
205 win->m_exposed = TRUE;
206
207 win->GetUpdateRegion().Union( rect->x, rect->y,
208 rect->width, rect->height );
209}
210
30760ce7
RR
211//-----------------------------------------------------------------------------
212// "size_allocate" of m_wxwindow
213//-----------------------------------------------------------------------------
214
215static void
216gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
217{
218 if (g_isIdle)
219 wxapp_install_idle_handler();
220
221 if (!win->m_hasVMT)
222 return;
223
224 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
225 event.SetEventObject( win );
226 win->GetEventHandler()->ProcessEvent( event );
227}
228
9d3221ab
RR
229//---------------------------------------------------------------------------
230// wxGlCanvas
231//---------------------------------------------------------------------------
232
233IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
234
235BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
236 EVT_SIZE(wxGLCanvas::OnSize)
237END_EVENT_TABLE()
238
aae24d21
RR
239wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
240 const wxPoint& pos, const wxSize& size,
241 long style, const wxString& name,
242 int *attribList,
243 const wxPalette& palette )
244{
089e55d6
RR
245 Create( parent, NULL, id, pos, size, style, name, attribList, palette );
246}
247
248wxGLCanvas::wxGLCanvas( wxWindow *parent,
249 const wxGLContext *shared,
250 wxWindowID id,
251 const wxPoint& pos, const wxSize& size,
252 long style, const wxString& name,
253 int *attribList,
254 const wxPalette& palette )
255{
256 Create( parent, shared, id, pos, size, style, name, attribList, palette );
aae24d21
RR
257}
258
089e55d6
RR
259bool wxGLCanvas::Create( wxWindow *parent,
260 const wxGLContext *shared,
261 wxWindowID id,
aae24d21
RR
262 const wxPoint& pos, const wxSize& size,
263 long style, const wxString& name,
264 int *attribList,
089e55d6 265 const wxPalette& palette)
9d3221ab 266{
147bc491 267 m_sharedContext = (wxGLContext*)shared; // const_cast
4b59bea3 268 m_glContext = (wxGLContext*) NULL;
147bc491
RR
269
270 m_exposed = FALSE;
271 m_noExpose = TRUE;
30760ce7 272 m_nativeSizeEvent = TRUE;
1d61ebac 273
aae24d21
RR
274 if (!attribList)
275 {
276 int data[] = { GLX_RGBA,
277 GLX_DOUBLEBUFFER,
1d61ebac 278 GLX_DEPTH_SIZE, 1, // use largest available depth buffer
a533f5c1
RR
279 GLX_RED_SIZE, 1,
280 GLX_GREEN_SIZE, 1,
281 GLX_BLUE_SIZE, 1,
265b4577 282 GLX_ALPHA_SIZE, 0,
aae24d21
RR
283 None };
284 attribList = (int*) data;
089e55d6
RR
285 }
286 else
287 {
288 int data[512], arg=0, p=0;
289
290 while( (attribList[arg]!=0) && (p<512) )
291 {
292 switch( attribList[arg++] )
293 {
294 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
295 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
296 case WX_GL_DEPTH_SIZE:
297 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
298 case WX_GL_MIN_RED:
299 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
300 case WX_GL_MIN_GREEN:
301 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
302 case WX_GL_MIN_BLUE:
303 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
304 default:
305 break;
306 }
307 }
308 data[p] = 0;
309
310 attribList = (int*) data;
aae24d21
RR
311 }
312
e8b04eb3 313
aae24d21
RR
314 Display *dpy = GDK_DISPLAY();
315
4b59bea3
RR
316 XVisualInfo *vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
317
318 m_vi = vi; // safe for later use
aae24d21 319
4b59bea3 320 wxCHECK_MSG( m_vi, FALSE, "required visual couldn't be found" );
a20cd16c 321
4b59bea3
RR
322 GdkVisual *visual = gdkx_visual_get( vi->visualid );
323 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(vi->visualid), TRUE );
aae24d21
RR
324
325 gtk_widget_push_colormap( colormap );
326 gtk_widget_push_visual( visual );
1d61ebac
RR
327
328 wxScrolledWindow::Create( parent, id, pos, size, style, name );
329
330 m_glWidget = m_wxwindow;
aae24d21 331
da048e3d 332 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
147bc491
RR
333
334 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
1d61ebac 335 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
147bc491 336
7941ba11
RR
337 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
338 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
339
147bc491
RR
340 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
341 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
342
343 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
344 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
345
30760ce7
RR
346 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
347 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
348
aae24d21
RR
349 gtk_widget_pop_visual();
350 gtk_widget_pop_colormap();
351
aae24d21 352 return TRUE;
9d3221ab
RR
353}
354
355wxGLCanvas::~wxGLCanvas()
356{
4b59bea3
RR
357 XVisualInfo *vi = (XVisualInfo *) m_vi;
358
359 if (vi)
360 XFree( vi );
361
bbe0af5b 362 if (m_glContext) delete m_glContext;
9d3221ab
RR
363}
364
365void wxGLCanvas::SwapBuffers()
366{
bbe0af5b 367 if (m_glContext) m_glContext->SwapBuffers();
9d3221ab
RR
368}
369
370void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
371{
bbe0af5b 372 int width, height;
aae24d21 373 GetClientSize( &width, &height );
5fd11f09 374
aae24d21 375 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
bbe0af5b 376 {
aae24d21
RR
377 SetCurrent();
378
379 glViewport(0, 0, (GLint)width, (GLint)height );
bbe0af5b
RR
380 glMatrixMode(GL_PROJECTION);
381 glLoadIdentity();
382 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
383 glMatrixMode(GL_MODELVIEW);
384 }
9d3221ab
RR
385}
386
387void wxGLCanvas::SetCurrent()
388{
bbe0af5b 389 if (m_glContext) m_glContext->SetCurrent();
9d3221ab
RR
390}
391
392void wxGLCanvas::SetColour( const char *colour )
393{
bbe0af5b 394 if (m_glContext) m_glContext->SetColour( colour );
9d3221ab
RR
395}
396
147bc491 397void wxGLCanvas::OnInternalIdle()
bbe0af5b 398{
147bc491
RR
399 if (m_glContext && m_exposed)
400 {
401 wxPaintEvent event( GetId() );
402 event.SetEventObject( this );
403 GetEventHandler()->ProcessEvent( event );
9d3221ab 404
147bc491
RR
405 m_exposed = FALSE;
406 GetUpdateRegion().Clear();
407 }
54517652
RR
408
409 wxWindow::OnInternalIdle();
410}