]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/gtk/glcanvas.cpp
compilation fix for DLL build
[wxWidgets.git] / utils / glcanvas / gtk / glcanvas.cpp
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 "glcanvas.h"
17
18 #include "wx/frame.h"
19 #include "wx/colour.h"
20 #include "wx/module.h"
21 #include "wx/app.h"
22
23 extern "C" {
24 #include "gtk/gtk.h"
25 #include "gdk/gdk.h"
26 #include "gdk/gdkx.h"
27 }
28
29 #include "wx/gtk/win_gtk.h"
30
31 //---------------------------------------------------------------------------
32 // global data
33 //---------------------------------------------------------------------------
34
35 XVisualInfo *g_vi = (XVisualInfo*) NULL;
36
37 //-----------------------------------------------------------------------------
38 // idle system
39 //-----------------------------------------------------------------------------
40
41 extern void wxapp_install_idle_handler();
42 extern bool g_isIdle;
43
44 //---------------------------------------------------------------------------
45 // wxGLContext
46 //---------------------------------------------------------------------------
47
48 IMPLEMENT_CLASS(wxGLContext,wxObject)
49
50 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
51 {
52 m_window = win;
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" );
59
60 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
61
62 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
63 }
64
65 wxGLContext::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;
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" );
78
79 if( other != 0 )
80 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, other->m_glContext, GL_TRUE );
81 else
82 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
83
84 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
85 }
86
87 wxGLContext::~wxGLContext()
88 {
89 if (!m_glContext) return;
90
91 if (m_glContext == glXGetCurrentContext())
92 {
93 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
94 }
95
96 glXDestroyContext( GDK_DISPLAY(), m_glContext );
97 }
98
99 void wxGLContext::SwapBuffers()
100 {
101 if (m_glContext)
102 {
103 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
104 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
105 }
106 }
107
108 void wxGLContext::SetCurrent()
109 {
110 if (m_glContext)
111 {
112 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
113 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
114 }
115 }
116
117 void wxGLContext::SetColour(const char *colour)
118 {
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 }
130 }
131
132 void wxGLContext::SetupPixelFormat()
133 {
134 }
135
136 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
137 {
138 }
139
140 wxPalette wxGLContext::CreateDefaultPalette()
141 {
142 return wxNullPalette;
143 }
144
145 //-----------------------------------------------------------------------------
146 // "realize" from m_wxwindow
147 //-----------------------------------------------------------------------------
148
149 static gint
150 gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
151 {
152 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, win->m_sharedContext );
153
154 return FALSE;
155 }
156
157 //-----------------------------------------------------------------------------
158 // "map" from m_wxwindow
159 //-----------------------------------------------------------------------------
160
161 static gint
162 gtk_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
177 //-----------------------------------------------------------------------------
178 // "expose_event" of m_wxwindow
179 //-----------------------------------------------------------------------------
180
181 static void
182 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
183 {
184 if (g_isIdle)
185 wxapp_install_idle_handler();
186
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
199 static void
200 gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
201 {
202 if (g_isIdle)
203 wxapp_install_idle_handler();
204
205 win->m_exposed = TRUE;
206
207 win->GetUpdateRegion().Union( rect->x, rect->y,
208 rect->width, rect->height );
209 }
210
211 //-----------------------------------------------------------------------------
212 // "size_allocate" of m_wxwindow
213 //-----------------------------------------------------------------------------
214
215 static void
216 gtk_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
229 //---------------------------------------------------------------------------
230 // wxGlCanvas
231 //---------------------------------------------------------------------------
232
233 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
234
235 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
236 EVT_SIZE(wxGLCanvas::OnSize)
237 END_EVENT_TABLE()
238
239 wxGLCanvas::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 {
245 Create( parent, NULL, id, pos, size, style, name, attribList, palette );
246 }
247
248 wxGLCanvas::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 );
257 }
258
259 bool wxGLCanvas::Create( wxWindow *parent,
260 const wxGLContext *shared,
261 wxWindowID id,
262 const wxPoint& pos, const wxSize& size,
263 long style, const wxString& name,
264 int *attribList,
265 const wxPalette& palette)
266 {
267 m_sharedContext = (wxGLContext*)shared; // const_cast
268 m_glContext = (wxGLContext*) NULL;
269
270 m_exposed = FALSE;
271 m_noExpose = TRUE;
272 m_nativeSizeEvent = TRUE;
273
274 if (!attribList)
275 {
276 int data[] = { GLX_RGBA,
277 GLX_DOUBLEBUFFER,
278 GLX_DEPTH_SIZE, 1, // use largest available depth buffer
279 GLX_RED_SIZE, 1,
280 GLX_GREEN_SIZE, 1,
281 GLX_BLUE_SIZE, 1,
282 GLX_ALPHA_SIZE, 0,
283 None };
284 attribList = (int*) data;
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;
311 }
312
313
314 Display *dpy = GDK_DISPLAY();
315
316 XVisualInfo *vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
317
318 m_vi = vi; // safe for later use
319
320 wxCHECK_MSG( m_vi, FALSE, "required visual couldn't be found" );
321
322 GdkVisual *visual = gdkx_visual_get( vi->visualid );
323 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(vi->visualid), TRUE );
324
325 gtk_widget_push_colormap( colormap );
326 gtk_widget_push_visual( visual );
327
328 wxScrolledWindow::Create( parent, id, pos, size, style, name );
329
330 m_glWidget = m_wxwindow;
331
332 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
333
334 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
335 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
336
337 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map",
338 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this );
339
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
346 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
347 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this );
348
349 gtk_widget_pop_visual();
350 gtk_widget_pop_colormap();
351
352 return TRUE;
353 }
354
355 wxGLCanvas::~wxGLCanvas()
356 {
357 XVisualInfo *vi = (XVisualInfo *) m_vi;
358
359 if (vi)
360 XFree( vi );
361
362 if (m_glContext) delete m_glContext;
363 }
364
365 void wxGLCanvas::SwapBuffers()
366 {
367 if (m_glContext) m_glContext->SwapBuffers();
368 }
369
370 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
371 {
372 int width, height;
373 GetClientSize( &width, &height );
374
375 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
376 {
377 SetCurrent();
378
379 glViewport(0, 0, (GLint)width, (GLint)height );
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 }
385 }
386
387 void wxGLCanvas::SetCurrent()
388 {
389 if (m_glContext) m_glContext->SetCurrent();
390 }
391
392 void wxGLCanvas::SetColour( const char *colour )
393 {
394 if (m_glContext) m_glContext->SetColour( colour );
395 }
396
397 void wxGLCanvas::OnInternalIdle()
398 {
399 if (m_glContext && m_exposed)
400 {
401 wxPaintEvent event( GetId() );
402 event.SetEventObject( this );
403 GetEventHandler()->ProcessEvent( event );
404
405 m_exposed = FALSE;
406 GetUpdateRegion().Clear();
407 }
408
409 wxWindow::OnInternalIdle();
410 }