wxChoiceDialog fix.
[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->m_hasVMT) return;
138
139 win->GetUpdateRegion().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->GetUpdateRegion().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->m_hasVMT) return;
167
168 win->GetUpdateRegion().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->GetUpdateRegion().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 GLX_RED_SIZE, 1,
221 GLX_GREEN_SIZE, 1,
222 GLX_BLUE_SIZE, 1,
223 GLX_ALPHA_SIZE, 1,
224 None };
225 attribList = (int*) data;
226 }
227 else
228 {
229 int data[512], arg=0, p=0;
230
231 while( (attribList[arg]!=0) && (p<512) )
232 {
233 switch( attribList[arg++] )
234 {
235 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
236 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
237 case WX_GL_DEPTH_SIZE:
238 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
239 case WX_GL_MIN_RED:
240 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
241 case WX_GL_MIN_GREEN:
242 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
243 case WX_GL_MIN_BLUE:
244 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
245 default:
246 break;
247 }
248 }
249 data[p] = 0;
250
251 attribList = (int*) data;
252 }
253
254 Display *dpy = GDK_DISPLAY();
255
256 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
257
258 GdkVisual *visual = gdkx_visual_get( g_vi->visualid );
259 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE );
260
261 gtk_widget_push_colormap( colormap );
262 gtk_widget_push_visual( visual );
263
264 m_glWidget = gtk_myfixed_new();
265
266 gtk_widget_pop_visual();
267 gtk_widget_pop_colormap();
268
269 wxScrolledWindow::Create( parent, id, pos, size, style, name );
270
271 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
272 GTK_WIDGET_SET_FLAGS( m_glWidget, GTK_CAN_FOCUS );
273
274 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), m_glWidget, 0, 0, m_width, m_height );
275
276 gtk_signal_connect( GTK_OBJECT(m_glWidget), "expose_event",
277 GTK_SIGNAL_FUNC(gtk_window_expose_callback), (gpointer)this );
278
279 gtk_signal_connect( GTK_OBJECT(m_glWidget), "draw",
280 GTK_SIGNAL_FUNC(gtk_window_draw_callback), (gpointer)this );
281
282 /* connect to key press and mouse handlers etc. */
283 ConnectWidget( m_glWidget );
284
285
286 /* must be realized for OpenGl output */
287 gtk_widget_realize( m_glWidget );
288
289 gtk_widget_show( m_glWidget );
290
291 m_glContext = new wxGLContext( TRUE, this, palette, shared );
292
293 XFree( g_vi );
294 g_vi = (XVisualInfo*) NULL;
295
296 gdk_window_set_back_pixmap( m_glWidget->window, None, 0 );
297
298 return TRUE;
299 }
300
301 wxGLCanvas::~wxGLCanvas()
302 {
303 if (m_glContext) delete m_glContext;
304 }
305
306 void wxGLCanvas::SwapBuffers()
307 {
308 if (m_glContext) m_glContext->SwapBuffers();
309 }
310
311 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
312 {
313 int width, height;
314 GetClientSize( &width, &height );
315 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
316 {
317 SetCurrent();
318 // gdk_window_set_back_pixmap( gtk_widget_get_parent_window(m_glWidget), None, 0 );
319
320 glViewport(0, 0, (GLint)width, (GLint)height );
321 glMatrixMode(GL_PROJECTION);
322 glLoadIdentity();
323 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
324 glMatrixMode(GL_MODELVIEW);
325 }
326 }
327
328 void wxGLCanvas::SetCurrent()
329 {
330 if (m_glContext) m_glContext->SetCurrent();
331 }
332
333 void wxGLCanvas::SetColour( const char *colour )
334 {
335 if (m_glContext) m_glContext->SetColour( colour );
336 }
337
338 void wxGLCanvas::DoSetSize( int x, int y, int width, int height, int sizeFlags )
339 {
340 if (m_resizing) return; // I don't like recursions
341 m_resizing = TRUE;
342
343 if (m_parent->m_wxwindow == NULL) // i.e. wxNotebook
344 {
345 // don't set the size for children of wxNotebook, just take the values.
346 m_x = x;
347 m_y = y;
348 m_width = width;
349 m_height = height;
350 }
351 else
352 {
353 int old_width = m_width;
354 int old_height = m_height;
355
356 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
357 {
358 if (x != -1) m_x = x;
359 if (y != -1) m_y = y;
360 if (width != -1) m_width = width;
361 if (height != -1) m_height = height;
362 }
363 else
364 {
365 m_x = x;
366 m_y = y;
367 m_width = width;
368 m_height = height;
369 }
370
371 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
372 {
373 if (width == -1) m_width = 80;
374 }
375
376 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
377 {
378 if (height == -1) m_height = 26;
379 }
380
381 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
382 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
383 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
384 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
385
386 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow),
387 m_widget,
388 m_x,
389 m_y,
390 m_width,
391 m_height );
392
393 gtk_myfixed_set_size( GTK_MYFIXED(m_wxwindow),
394 m_glWidget,
395 m_x,
396 m_y,
397 m_width,
398 m_height );
399 }
400
401 m_sizeSet = TRUE;
402
403 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
404 event.SetEventObject( this );
405 GetEventHandler()->ProcessEvent( event );
406
407 m_resizing = FALSE;
408 }
409
410 GtkWidget *wxGLCanvas::GetConnectWidget()
411 {
412 return m_glWidget;
413 }
414
415 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window )
416 {
417 return (window == m_glWidget->window);
418 }