]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/gtk/glcanvas.cpp
1. wxPostEvent added and documented
[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 = ((wxGLCanvas*)win)->m_glWidget;
54
55 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
56
57 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
58
59 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
60 }
61
62 wxGLContext::wxGLContext(
63 bool WXUNUSED(isRGB), wxWindow *win,
64 const wxPalette& WXUNUSED(palette),
65 const wxGLContext *other /* for sharing display lists */
66 )
67 {
68 m_window = win;
69 m_widget = ((wxGLCanvas*)win)->m_glWidget;
70
71 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
72
73 if( other != 0 )
74 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, other->m_glContext,
75 GL_TRUE );
76 else
77 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
78
79 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
80 }
81
82 wxGLContext::~wxGLContext()
83 {
84 if (!m_glContext) return;
85
86 if (m_glContext == glXGetCurrentContext())
87 {
88 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
89 }
90
91 glXDestroyContext( GDK_DISPLAY(), m_glContext );
92 }
93
94 void wxGLContext::SwapBuffers()
95 {
96 if (m_glContext)
97 {
98 GdkWindow *window = GTK_MYFIXED(m_widget)->bin_window;
99 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
100 }
101 }
102
103 void wxGLContext::SetCurrent()
104 {
105 if (m_glContext)
106 {
107 GdkWindow *window = GTK_MYFIXED(m_widget)->bin_window;
108 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
109 }
110 }
111
112 void wxGLContext::SetColour(const char *colour)
113 {
114 float r = 0.0;
115 float g = 0.0;
116 float b = 0.0;
117 wxColour *col = wxTheColourDatabase->FindColour(colour);
118 if (col)
119 {
120 r = (float)(col->Red()/256.0);
121 g = (float)(col->Green()/256.0);
122 b = (float)(col->Blue()/256.0);
123 glColor3f( r, g, b);
124 }
125 }
126
127 void wxGLContext::SetupPixelFormat()
128 {
129 }
130
131 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
132 {
133 }
134
135 wxPalette wxGLContext::CreateDefaultPalette()
136 {
137 return wxNullPalette;
138 }
139
140 //-----------------------------------------------------------------------------
141 // "realize" from m_wxwindow
142 //-----------------------------------------------------------------------------
143
144 static gint
145 gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
146 {
147 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, win->m_sharedContext );
148
149 XFree( g_vi );
150 g_vi = (XVisualInfo*) NULL;
151
152 return FALSE;
153 }
154
155 //-----------------------------------------------------------------------------
156 // "expose_event" of m_wxwindow
157 //-----------------------------------------------------------------------------
158
159 static void
160 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
161 {
162 if (g_isIdle)
163 wxapp_install_idle_handler();
164
165 win->m_exposed = TRUE;
166
167 win->GetUpdateRegion().Union( gdk_event->area.x,
168 gdk_event->area.y,
169 gdk_event->area.width,
170 gdk_event->area.height );
171 }
172
173 //-----------------------------------------------------------------------------
174 // "draw" of m_wxwindow
175 //-----------------------------------------------------------------------------
176
177 static void
178 gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win )
179 {
180 if (g_isIdle)
181 wxapp_install_idle_handler();
182
183 win->m_exposed = TRUE;
184
185 win->GetUpdateRegion().Union( rect->x, rect->y,
186 rect->width, rect->height );
187 }
188
189 //---------------------------------------------------------------------------
190 // wxGlCanvas
191 //---------------------------------------------------------------------------
192
193 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
194
195 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
196 EVT_SIZE(wxGLCanvas::OnSize)
197 END_EVENT_TABLE()
198
199 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
200 const wxPoint& pos, const wxSize& size,
201 long style, const wxString& name,
202 int *attribList,
203 const wxPalette& palette )
204 {
205 Create( parent, NULL, id, pos, size, style, name, attribList, palette );
206 }
207
208 wxGLCanvas::wxGLCanvas( wxWindow *parent,
209 const wxGLContext *shared,
210 wxWindowID id,
211 const wxPoint& pos, const wxSize& size,
212 long style, const wxString& name,
213 int *attribList,
214 const wxPalette& palette )
215 {
216 Create( parent, shared, id, pos, size, style, name, attribList, palette );
217 }
218
219 bool wxGLCanvas::Create( wxWindow *parent,
220 const wxGLContext *shared,
221 wxWindowID id,
222 const wxPoint& pos, const wxSize& size,
223 long style, const wxString& name,
224 int *attribList,
225 const wxPalette& palette)
226 {
227 m_sharedContext = (wxGLContext*)shared; // const_cast
228
229 m_exposed = FALSE;
230 m_noExpose = TRUE;
231
232 if (!attribList)
233 {
234 int data[] = { GLX_RGBA,
235 GLX_DOUBLEBUFFER,
236 GLX_DEPTH_SIZE, 1, // use largest available depth buffer
237 GLX_RED_SIZE, 1,
238 GLX_GREEN_SIZE, 1,
239 GLX_BLUE_SIZE, 1,
240 GLX_ALPHA_SIZE, 0,
241 None };
242 attribList = (int*) data;
243 }
244 else
245 {
246 int data[512], arg=0, p=0;
247
248 while( (attribList[arg]!=0) && (p<512) )
249 {
250 switch( attribList[arg++] )
251 {
252 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
253 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
254 case WX_GL_DEPTH_SIZE:
255 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
256 case WX_GL_MIN_RED:
257 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
258 case WX_GL_MIN_GREEN:
259 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
260 case WX_GL_MIN_BLUE:
261 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
262 default:
263 break;
264 }
265 }
266 data[p] = 0;
267
268 attribList = (int*) data;
269 }
270
271
272 Display *dpy = GDK_DISPLAY();
273
274 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
275
276 wxCHECK_MSG( g_vi, FALSE, "required visual couldn't be found" );
277
278 GdkVisual *visual = gdkx_visual_get( g_vi->visualid );
279 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE );
280
281 gtk_widget_push_colormap( colormap );
282 gtk_widget_push_visual( visual );
283
284 wxScrolledWindow::Create( parent, id, pos, size, style, name );
285
286 m_glWidget = m_wxwindow;
287
288 gtk_my_fixed_set_clear( GTK_MYFIXED(m_wxwindow), FALSE );
289
290 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
291 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
292
293 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
294 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this );
295
296 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
297 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this );
298
299 gtk_widget_pop_visual();
300 gtk_widget_pop_colormap();
301
302 return TRUE;
303 }
304
305 wxGLCanvas::~wxGLCanvas()
306 {
307 if (m_glContext) delete m_glContext;
308 }
309
310 void wxGLCanvas::SwapBuffers()
311 {
312 if (m_glContext) m_glContext->SwapBuffers();
313 }
314
315 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
316 {
317 int width, height;
318 GetClientSize( &width, &height );
319
320 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
321 {
322 SetCurrent();
323
324 glViewport(0, 0, (GLint)width, (GLint)height );
325 glMatrixMode(GL_PROJECTION);
326 glLoadIdentity();
327 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
328 glMatrixMode(GL_MODELVIEW);
329 }
330 }
331
332 void wxGLCanvas::SetCurrent()
333 {
334 if (m_glContext) m_glContext->SetCurrent();
335 }
336
337 void wxGLCanvas::SetColour( const char *colour )
338 {
339 if (m_glContext) m_glContext->SetColour( colour );
340 }
341
342 void wxGLCanvas::OnInternalIdle()
343 {
344 if (m_glContext && m_exposed)
345 {
346 wxPaintEvent event( GetId() );
347 event.SetEventObject( this );
348 GetEventHandler()->ProcessEvent( event );
349
350 m_exposed = FALSE;
351 GetUpdateRegion().Clear();
352 }
353
354 wxWindow::OnInternalIdle();
355 }