]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/gtk/glcanvas.cpp
wxGTK now works a little again.
[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 // wxGLContext
39 //---------------------------------------------------------------------------
40
41 IMPLEMENT_CLASS(wxGLContext,wxObject)
42
43 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
44 {
45 m_window = win;
46 m_widget = ((wxGLCanvas*)win)->m_glWidget;
47
48 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
49
50 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
51
52 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
53 }
54
55 wxGLContext::wxGLContext(
56 bool WXUNUSED(isRGB), wxWindow *win,
57 const wxPalette& WXUNUSED(palette),
58 const wxGLContext *other /* for sharing display lists */
59 )
60 {
61 m_window = win;
62 m_widget = ((wxGLCanvas*)win)->m_glWidget;
63
64 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
65
66 if( other != 0 )
67 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, other->m_glContext,
68 GL_TRUE );
69 else
70 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
71
72 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
73 }
74
75 wxGLContext::~wxGLContext()
76 {
77 if (!m_glContext) return;
78
79 if (m_glContext == glXGetCurrentContext())
80 {
81 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
82 }
83
84 glXDestroyContext( GDK_DISPLAY(), m_glContext );
85 }
86
87 void wxGLContext::SwapBuffers()
88 {
89 if (m_glContext)
90 {
91 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) );
92 }
93 }
94
95 void wxGLContext::SetCurrent()
96 {
97 if (m_glContext)
98 {
99 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
100 }
101 }
102
103 void wxGLContext::SetColour(const char *colour)
104 {
105 float r = 0.0;
106 float g = 0.0;
107 float b = 0.0;
108 wxColour *col = wxTheColourDatabase->FindColour(colour);
109 if (col)
110 {
111 r = (float)(col->Red()/256.0);
112 g = (float)(col->Green()/256.0);
113 b = (float)(col->Blue()/256.0);
114 glColor3f( r, g, b);
115 }
116 }
117
118 void wxGLContext::SetupPixelFormat()
119 {
120 }
121
122 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
123 {
124 }
125
126 wxPalette wxGLContext::CreateDefaultPalette()
127 {
128 return wxNullPalette;
129 }
130
131 //-----------------------------------------------------------------------------
132 // "expose_event" of m_glWidget
133 //-----------------------------------------------------------------------------
134
135 static void gtk_window_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxWindow *win )
136 {
137 if (!win->HasVMT()) return;
138
139 win->m_updateRegion.Union( gdk_event->area.x,
140 gdk_event->area.y,
141 gdk_event->area.width,
142 gdk_event->area.height );
143
144 if (gdk_event->count > 0) return;
145
146 /*
147 printf( "OnExpose from " );
148 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
149 printf( win->GetClassInfo()->GetClassName() );
150 printf( ".\n" );
151 */
152
153 wxPaintEvent event( win->GetId() );
154 event.SetEventObject( win );
155 win->GetEventHandler()->ProcessEvent( event );
156
157 win->m_updateRegion.Clear();
158 }
159
160 //-----------------------------------------------------------------------------
161 // "draw" of m_glWidget
162 //-----------------------------------------------------------------------------
163
164 static void gtk_window_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxWindow *win )
165 {
166 if (!win->HasVMT()) return;
167
168 win->m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
169
170 wxPaintEvent event( win->GetId() );
171 event.SetEventObject( win );
172 win->GetEventHandler()->ProcessEvent( event );
173
174 win->m_updateRegion.Clear();
175 }
176
177 //---------------------------------------------------------------------------
178 // wxGlCanvas
179 //---------------------------------------------------------------------------
180
181 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
182
183 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
184 EVT_SIZE(wxGLCanvas::OnSize)
185 END_EVENT_TABLE()
186
187 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
188 const wxPoint& pos, const wxSize& size,
189 long style, const wxString& name,
190 int *attribList,
191 const wxPalette& palette )
192 {
193 Create( parent, NULL, id, pos, size, style, name, attribList, palette );
194 }
195
196 wxGLCanvas::wxGLCanvas( wxWindow *parent,
197 const wxGLContext *shared,
198 wxWindowID id,
199 const wxPoint& pos, const wxSize& size,
200 long style, const wxString& name,
201 int *attribList,
202 const wxPalette& palette )
203 {
204 Create( parent, shared, id, pos, size, style, name, attribList, palette );
205 }
206
207 bool wxGLCanvas::Create( wxWindow *parent,
208 const wxGLContext *shared,
209 wxWindowID id,
210 const wxPoint& pos, const wxSize& size,
211 long style, const wxString& name,
212 int *attribList,
213 const wxPalette& palette)
214 {
215 if (!attribList)
216 {
217 int data[] = { GLX_RGBA,
218 GLX_DOUBLEBUFFER,
219 GLX_DEPTH_SIZE, 1, /* use largest available depth buffer */
220 None };
221 attribList = (int*) data;
222 printf( "using default values\n" );
223 }
224 else
225 {
226 int data[512], arg=0, p=0;
227
228 while( (attribList[arg]!=0) && (p<512) )
229 {
230 switch( attribList[arg++] )
231 {
232 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
233 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
234 case WX_GL_DEPTH_SIZE:
235 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
236 case WX_GL_MIN_RED:
237 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
238 case WX_GL_MIN_GREEN:
239 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
240 case WX_GL_MIN_BLUE:
241 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
242 default:
243 break;
244 }
245 }
246 data[p] = 0;
247
248 attribList = (int*) data;
249 }
250
251 Display *dpy = GDK_DISPLAY();
252
253 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
254
255 GdkVisual *visual = gdkx_visual_get( g_vi->visualid );
256 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE );
257
258 gtk_widget_push_colormap( colormap );
259 gtk_widget_push_visual( visual );
260
261 m_glWidget = gtk_myfixed_new();
262
263 gtk_widget_pop_visual();
264 gtk_widget_pop_colormap();
265
266 wxScrolledWindow::Create( parent, id, pos, size, style, name );
267
268 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
269 GTK_WIDGET_SET_FLAGS( m_glWidget, GTK_CAN_FOCUS );
270
271 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), m_glWidget, 0, 0, m_width, m_height );
272
273 gtk_signal_connect( GTK_OBJECT(m_glWidget), "expose_event",
274 GTK_SIGNAL_FUNC(gtk_window_expose_callback), (gpointer)this );
275
276 gtk_signal_connect( GTK_OBJECT(m_glWidget), "draw",
277 GTK_SIGNAL_FUNC(gtk_window_draw_callback), (gpointer)this );
278
279 /* connect to key press and mouse handlers etc. */
280 ConnectWidget( m_glWidget );
281
282
283 /* must be realized for OpenGl output */
284 gtk_widget_realize( m_glWidget );
285
286 gtk_widget_show( m_glWidget );
287
288 m_glContext = new wxGLContext( TRUE, this, palette, shared );
289
290 XFree( g_vi );
291 g_vi = (XVisualInfo*) NULL;
292
293 gdk_window_set_back_pixmap( m_glWidget->window, None, 0 );
294
295 return TRUE;
296 }
297
298 wxGLCanvas::~wxGLCanvas()
299 {
300 if (m_glContext) delete m_glContext;
301 }
302
303 void wxGLCanvas::SwapBuffers()
304 {
305 if (m_glContext) m_glContext->SwapBuffers();
306 }
307
308 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
309 {
310 int width, height;
311 GetClientSize( &width, &height );
312 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
313 {
314 SetCurrent();
315 // gdk_window_set_back_pixmap( gtk_widget_get_parent_window(m_glWidget), None, 0 );
316
317 glViewport(0, 0, (GLint)width, (GLint)height );
318 glMatrixMode(GL_PROJECTION);
319 glLoadIdentity();
320 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
321 glMatrixMode(GL_MODELVIEW);
322 }
323 }
324
325 void wxGLCanvas::SetCurrent()
326 {
327 if (m_glContext) m_glContext->SetCurrent();
328 }
329
330 void wxGLCanvas::SetColour( const char *colour )
331 {
332 if (m_glContext) m_glContext->SetColour( colour );
333 }
334
335 void wxGLCanvas::DoSetSize( int x, int y, int width, int height, int sizeFlags )
336 {
337 if (m_resizing) return; // I don't like recursions
338 m_resizing = TRUE;
339
340 if (m_parent->m_wxwindow == NULL) // i.e. wxNotebook
341 {
342 // don't set the size for children of wxNotebook, just take the values.
343 m_x = x;
344 m_y = y;
345 m_width = width;
346 m_height = height;
347 }
348 else
349 {
350 int old_width = m_width;
351 int old_height = m_height;
352
353 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
354 {
355 if (x != -1) m_x = x;
356 if (y != -1) m_y = y;
357 if (width != -1) m_width = width;
358 if (height != -1) m_height = height;
359 }
360 else
361 {
362 m_x = x;
363 m_y = y;
364 m_width = width;
365 m_height = height;
366 }
367
368 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
369 {
370 if (width == -1) m_width = 80;
371 }
372
373 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
374 {
375 if (height == -1) m_height = 26;
376 }
377
378 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
379 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
380 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
381 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
382
383 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow),
384 m_widget,
385 m_x,
386 m_y,
387 m_width,
388 m_height );
389
390 gtk_myfixed_set_size( GTK_MYFIXED(m_wxwindow),
391 m_glWidget,
392 m_x,
393 m_y,
394 m_width,
395 m_height );
396 }
397
398 m_sizeSet = TRUE;
399
400 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
401 event.SetEventObject( this );
402 GetEventHandler()->ProcessEvent( event );
403
404 m_resizing = FALSE;
405 }
406
407 GtkWidget *wxGLCanvas::GetConnectWidget()
408 {
409 return m_glWidget;
410 }
411
412 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window )
413 {
414 return (window == m_glWidget->window);
415 }