]>
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 | { | |
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 | ||
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 */ |
aae24d21 RR |
220 | None }; |
221 | attribList = (int*) data; | |
089e55d6 RR |
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; | |
aae24d21 RR |
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 | ||
3cdda6cd | 261 | m_glWidget = gtk_myfixed_new(); |
180307da | 262 | |
aae24d21 RR |
263 | gtk_widget_pop_visual(); |
264 | gtk_widget_pop_colormap(); | |
265 | ||
266 | wxScrolledWindow::Create( parent, id, pos, size, style, name ); | |
267 | ||
3cdda6cd RR |
268 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); |
269 | GTK_WIDGET_SET_FLAGS( m_glWidget, GTK_CAN_FOCUS ); | |
270 | ||
08fc1744 | 271 | gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), m_glWidget, 0, 0, m_width, m_height ); |
089e55d6 | 272 | |
20239453 RR |
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 | ||
828f655f RR |
279 | /* connect to key press and mouse handlers etc. */ |
280 | ConnectWidget( m_glWidget ); | |
281 | ||
089e55d6 | 282 | |
3cdda6cd RR |
283 | /* must be realized for OpenGl output */ |
284 | gtk_widget_realize( m_glWidget ); | |
089e55d6 | 285 | |
aae24d21 RR |
286 | gtk_widget_show( m_glWidget ); |
287 | ||
089e55d6 | 288 | m_glContext = new wxGLContext( TRUE, this, palette, shared ); |
aae24d21 RR |
289 | |
290 | XFree( g_vi ); | |
291 | g_vi = (XVisualInfo*) NULL; | |
089e55d6 RR |
292 | |
293 | gdk_window_set_back_pixmap( m_glWidget->window, None, 0 ); | |
294 | ||
aae24d21 | 295 | return TRUE; |
9d3221ab RR |
296 | } |
297 | ||
298 | wxGLCanvas::~wxGLCanvas() | |
299 | { | |
bbe0af5b | 300 | if (m_glContext) delete m_glContext; |
9d3221ab RR |
301 | } |
302 | ||
303 | void wxGLCanvas::SwapBuffers() | |
304 | { | |
bbe0af5b | 305 | if (m_glContext) m_glContext->SwapBuffers(); |
9d3221ab RR |
306 | } |
307 | ||
308 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
309 | { | |
bbe0af5b | 310 | int width, height; |
aae24d21 RR |
311 | GetClientSize( &width, &height ); |
312 | if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) ) | |
bbe0af5b | 313 | { |
aae24d21 | 314 | SetCurrent(); |
089e55d6 | 315 | // gdk_window_set_back_pixmap( gtk_widget_get_parent_window(m_glWidget), None, 0 ); |
aae24d21 RR |
316 | |
317 | glViewport(0, 0, (GLint)width, (GLint)height ); | |
bbe0af5b RR |
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 | } | |
9d3221ab RR |
323 | } |
324 | ||
325 | void wxGLCanvas::SetCurrent() | |
326 | { | |
bbe0af5b | 327 | if (m_glContext) m_glContext->SetCurrent(); |
9d3221ab RR |
328 | } |
329 | ||
330 | void wxGLCanvas::SetColour( const char *colour ) | |
331 | { | |
bbe0af5b | 332 | if (m_glContext) m_glContext->SetColour( colour ); |
9d3221ab RR |
333 | } |
334 | ||
180307da | 335 | void wxGLCanvas::DoSetSize( int x, int y, int width, int height, int sizeFlags ) |
bbe0af5b | 336 | { |
aae24d21 RR |
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 | ||
08fc1744 RR |
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 ); | |
aae24d21 | 396 | } |
bbe0af5b | 397 | |
aae24d21 | 398 | m_sizeSet = TRUE; |
bbe0af5b | 399 | |
aae24d21 RR |
400 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); |
401 | event.SetEventObject( this ); | |
402 | GetEventHandler()->ProcessEvent( event ); | |
bbe0af5b | 403 | |
aae24d21 RR |
404 | m_resizing = FALSE; |
405 | } | |
bbe0af5b | 406 | |
aae24d21 | 407 | GtkWidget *wxGLCanvas::GetConnectWidget() |
bbe0af5b | 408 | { |
aae24d21 | 409 | return m_glWidget; |
bbe0af5b | 410 | } |
9d3221ab | 411 | |
aae24d21 RR |
412 | bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window ) |
413 | { | |
414 | return (window == m_glWidget->window); | |
415 | } |