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