]>
Commit | Line | Data |
---|---|---|
9d3221ab RR |
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 | ||
bbe0af5b | 16 | #include "glcanvas.h" |
9d3221ab RR |
17 | |
18 | #include "wx/frame.h" | |
19 | #include "wx/colour.h" | |
bbe0af5b RR |
20 | #include "wx/module.h" |
21 | #include "wx/app.h" | |
22 | ||
aae24d21 | 23 | extern "C" { |
bbe0af5b RR |
24 | #include "gtk/gtk.h" |
25 | #include "gdk/gdk.h" | |
bbe0af5b RR |
26 | #include "gdk/gdkx.h" |
27 | } | |
28 | ||
aae24d21 RR |
29 | #include "wx/gtk/win_gtk.h" |
30 | ||
bbe0af5b | 31 | //--------------------------------------------------------------------------- |
aae24d21 | 32 | // global data |
bbe0af5b RR |
33 | //--------------------------------------------------------------------------- |
34 | ||
aae24d21 | 35 | XVisualInfo *g_vi = (XVisualInfo*) NULL; |
9d3221ab RR |
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 | { | |
bbe0af5b | 45 | m_window = win; |
aae24d21 | 46 | m_widget = ((wxGLCanvas*)win)->m_glWidget; |
9d3221ab | 47 | |
aae24d21 | 48 | wxCHECK_RET( g_vi, "invalid visual for OpenGl" ); |
bbe0af5b | 49 | |
aae24d21 | 50 | m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE ); |
9d3221ab | 51 | |
aae24d21 | 52 | wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" ); |
9d3221ab RR |
53 | } |
54 | ||
089e55d6 RR |
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 | ||
9d3221ab RR |
75 | wxGLContext::~wxGLContext() |
76 | { | |
aae24d21 | 77 | if (!m_glContext) return; |
9d3221ab | 78 | |
aae24d21 RR |
79 | if (m_glContext == glXGetCurrentContext()) |
80 | { | |
81 | glXMakeCurrent( GDK_DISPLAY(), None, NULL); | |
bbe0af5b | 82 | } |
aae24d21 RR |
83 | |
84 | glXDestroyContext( GDK_DISPLAY(), m_glContext ); | |
9d3221ab RR |
85 | } |
86 | ||
87 | void wxGLContext::SwapBuffers() | |
88 | { | |
bbe0af5b RR |
89 | if (m_glContext) |
90 | { | |
91 | glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) ); | |
92 | } | |
9d3221ab RR |
93 | } |
94 | ||
95 | void wxGLContext::SetCurrent() | |
96 | { | |
bbe0af5b RR |
97 | if (m_glContext) |
98 | { | |
99 | glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext ); | |
100 | } | |
9d3221ab RR |
101 | } |
102 | ||
103 | void wxGLContext::SetColour(const char *colour) | |
104 | { | |
bbe0af5b RR |
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 | } | |
9d3221ab RR |
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 | { | |
bbe0af5b | 128 | return wxNullPalette; |
9d3221ab RR |
129 | } |
130 | ||
20239453 RR |
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 | { | |
92abe7f6 | 137 | if (!win->m_hasVMT) return; |
20239453 | 138 | |
92abe7f6 | 139 | win->GetUpdateRegion().Union( gdk_event->area.x, |
20239453 RR |
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 | ||
92abe7f6 | 157 | win->GetUpdateRegion().Clear(); |
20239453 RR |
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 | { | |
92abe7f6 | 166 | if (!win->m_hasVMT) return; |
20239453 | 167 | |
92abe7f6 | 168 | win->GetUpdateRegion().Union( rect->x, rect->y, rect->width, rect->height ); |
20239453 RR |
169 | |
170 | wxPaintEvent event( win->GetId() ); | |
171 | event.SetEventObject( win ); | |
172 | win->GetEventHandler()->ProcessEvent( event ); | |
173 | ||
92abe7f6 | 174 | win->GetUpdateRegion().Clear(); |
20239453 RR |
175 | } |
176 | ||
9d3221ab RR |
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 | ||
aae24d21 RR |
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 | { | |
089e55d6 RR |
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 ); | |
aae24d21 RR |
205 | } |
206 | ||
089e55d6 RR |
207 | bool wxGLCanvas::Create( wxWindow *parent, |
208 | const wxGLContext *shared, | |
209 | wxWindowID id, | |
aae24d21 RR |
210 | const wxPoint& pos, const wxSize& size, |
211 | long style, const wxString& name, | |
212 | int *attribList, | |
089e55d6 | 213 | const wxPalette& palette) |
9d3221ab | 214 | { |
aae24d21 RR |
215 | if (!attribList) |
216 | { | |
217 | int data[] = { GLX_RGBA, | |
218 | GLX_DOUBLEBUFFER, | |
089e55d6 | 219 | GLX_DEPTH_SIZE, 1, /* use largest available depth buffer */ |
a533f5c1 RR |
220 | GLX_RED_SIZE, 1, |
221 | GLX_GREEN_SIZE, 1, | |
222 | GLX_BLUE_SIZE, 1, | |
265b4577 | 223 | GLX_ALPHA_SIZE, 0, |
aae24d21 RR |
224 | None }; |
225 | attribList = (int*) data; | |
089e55d6 RR |
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; | |
aae24d21 RR |
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 | ||
3cdda6cd | 264 | m_glWidget = gtk_myfixed_new(); |
180307da | 265 | |
aae24d21 RR |
266 | gtk_widget_pop_visual(); |
267 | gtk_widget_pop_colormap(); | |
268 | ||
269 | wxScrolledWindow::Create( parent, id, pos, size, style, name ); | |
270 | ||
3cdda6cd RR |
271 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); |
272 | GTK_WIDGET_SET_FLAGS( m_glWidget, GTK_CAN_FOCUS ); | |
273 | ||
08fc1744 | 274 | gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), m_glWidget, 0, 0, m_width, m_height ); |
089e55d6 | 275 | |
20239453 RR |
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 | ||
828f655f RR |
282 | /* connect to key press and mouse handlers etc. */ |
283 | ConnectWidget( m_glWidget ); | |
284 | ||
089e55d6 | 285 | |
3cdda6cd RR |
286 | /* must be realized for OpenGl output */ |
287 | gtk_widget_realize( m_glWidget ); | |
089e55d6 | 288 | |
aae24d21 RR |
289 | gtk_widget_show( m_glWidget ); |
290 | ||
089e55d6 | 291 | m_glContext = new wxGLContext( TRUE, this, palette, shared ); |
aae24d21 RR |
292 | |
293 | XFree( g_vi ); | |
294 | g_vi = (XVisualInfo*) NULL; | |
089e55d6 RR |
295 | |
296 | gdk_window_set_back_pixmap( m_glWidget->window, None, 0 ); | |
297 | ||
aae24d21 | 298 | return TRUE; |
9d3221ab RR |
299 | } |
300 | ||
301 | wxGLCanvas::~wxGLCanvas() | |
302 | { | |
bbe0af5b | 303 | if (m_glContext) delete m_glContext; |
9d3221ab RR |
304 | } |
305 | ||
306 | void wxGLCanvas::SwapBuffers() | |
307 | { | |
bbe0af5b | 308 | if (m_glContext) m_glContext->SwapBuffers(); |
9d3221ab RR |
309 | } |
310 | ||
311 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
312 | { | |
bbe0af5b | 313 | int width, height; |
aae24d21 RR |
314 | GetClientSize( &width, &height ); |
315 | if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) ) | |
bbe0af5b | 316 | { |
aae24d21 | 317 | SetCurrent(); |
089e55d6 | 318 | // gdk_window_set_back_pixmap( gtk_widget_get_parent_window(m_glWidget), None, 0 ); |
aae24d21 RR |
319 | |
320 | glViewport(0, 0, (GLint)width, (GLint)height ); | |
bbe0af5b RR |
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 | } | |
9d3221ab RR |
326 | } |
327 | ||
328 | void wxGLCanvas::SetCurrent() | |
329 | { | |
bbe0af5b | 330 | if (m_glContext) m_glContext->SetCurrent(); |
9d3221ab RR |
331 | } |
332 | ||
333 | void wxGLCanvas::SetColour( const char *colour ) | |
334 | { | |
bbe0af5b | 335 | if (m_glContext) m_glContext->SetColour( colour ); |
9d3221ab RR |
336 | } |
337 | ||
180307da | 338 | void wxGLCanvas::DoSetSize( int x, int y, int width, int height, int sizeFlags ) |
bbe0af5b | 339 | { |
aae24d21 RR |
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 | ||
85ad5eb5 | 356 | if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0) |
aae24d21 RR |
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 | ||
08fc1744 RR |
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 ); | |
aae24d21 | 399 | } |
bbe0af5b | 400 | |
aae24d21 | 401 | m_sizeSet = TRUE; |
bbe0af5b | 402 | |
aae24d21 RR |
403 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); |
404 | event.SetEventObject( this ); | |
405 | GetEventHandler()->ProcessEvent( event ); | |
bbe0af5b | 406 | |
aae24d21 RR |
407 | m_resizing = FALSE; |
408 | } | |
bbe0af5b | 409 | |
aae24d21 | 410 | GtkWidget *wxGLCanvas::GetConnectWidget() |
bbe0af5b | 411 | { |
aae24d21 | 412 | return m_glWidget; |
bbe0af5b | 413 | } |
9d3221ab | 414 | |
aae24d21 RR |
415 | bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window ) |
416 | { | |
417 | return (window == m_glWidget->window); | |
418 | } |