]> git.saurik.com Git - wxWidgets.git/blame - src/motif/glcanvas.cpp
Implement GetValue() and SetValue() and send an event when clicked
[wxWidgets.git] / src / motif / glcanvas.cpp
CommitLineData
8b089c5e
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: glcanvas.cpp
3// Purpose: wxGLCanvas, for using OpenGL with wxWindows 2.0 for Motif.
4// Uses the GLX extension.
5// Author: Julian Smart and Wolfram Gloger
6// Modified by:
7// Created: 1995, 1999
8// RCS-ID: $Id$
9// Copyright: (c) Julian Smart, Wolfram Gloger
f48d169c 10// Licence: wxWindows licence
8b089c5e
JS
11/////////////////////////////////////////////////////////////////////////////
12
14f355c2 13#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
8b089c5e
JS
14#pragma implementation "glcanvas.h"
15#endif
16
17#include "wx/setup.h"
18
19#if wxUSE_GLCANVAS
20
21#include "wx/glcanvas.h"
22#include "wx/utils.h"
23#include "wx/app.h"
67a26a1a 24#include "wx/log.h"
8b089c5e 25
dae752d4
JJ
26#ifdef __VMS
27# pragma message disable nosimpint
28#endif
8b089c5e 29#include <Xm/Xm.h>
dae752d4
JJ
30#ifdef __VMS
31# pragma message enable nosimpint
32#endif
8b089c5e
JS
33#include "wx/motif/private.h"
34
34fdf762
VS
35// DLL options compatibility check:
36#include "wx/build.h"
37WX_CHECK_BUILD_OPTIONS("wxGL")
38
8b089c5e
JS
39#ifdef OLD_MESA
40// workaround for bug in Mesa's glx.c
41static int bitcount( unsigned long n )
42{
43 int bits;
44 for (bits=0; n>0;) {
f48d169c
UN
45 if(n & 1) bits++;
46 n = n >> 1;
8b089c5e
JS
47 }
48 return bits;
49}
50#endif
51
2695b174
UN
52/*
53 * GLContext implementation
54 */
55
56IMPLEMENT_CLASS(wxGLContext,wxObject)
57
58wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win,
59 const wxPalette& WXUNUSED(palette) )
60{
61 m_window = win;
62 // m_widget = win->m_wxwindow;
63
64 wxGLCanvas *gc = (wxGLCanvas*) win;
65 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
66
67 wxCHECK_RET( vi, "invalid visual for OpenGl" );
68
69 m_glContext = glXCreateContext( (Display *)m_window->GetXDisplay(), vi,
70 None, GL_TRUE);
71
72 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
73}
74
75wxGLContext::wxGLContext(
76 bool WXUNUSED(isRGB), wxWindow *win,
77 const wxPalette& WXUNUSED(palette),
78 const wxGLContext *other /* for sharing display lists */
79)
80{
81 m_window = win;
82 // m_widget = win->m_wxwindow;
83
84 wxGLCanvas *gc = (wxGLCanvas*) win;
85 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
86
87 wxCHECK_RET( vi, "invalid visual for OpenGl" );
88
89 if( other != 0 )
90 m_glContext = glXCreateContext( (Display *)m_window->GetXDisplay(), vi,
91 other->m_glContext, GL_TRUE );
92 else
93 m_glContext = glXCreateContext( (Display *)m_window->GetXDisplay(), vi,
94 None, GL_TRUE );
95
96 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
97}
98
99wxGLContext::~wxGLContext()
100{
101 if (!m_glContext) return;
102
103 if (m_glContext == glXGetCurrentContext())
104 {
105 glXMakeCurrent( (Display*) m_window->GetXDisplay(), None, NULL);
106 }
107
108 glXDestroyContext( (Display*) m_window->GetXDisplay(), m_glContext );
109}
110
111void wxGLContext::SwapBuffers()
112{
113 if (m_glContext)
114 {
115 Display* display = (Display*) m_window->GetXDisplay();
116 glXSwapBuffers(display, (Window) m_window->GetXWindow());
117 }
118}
119
120void wxGLContext::SetCurrent()
121{
122 if (m_glContext)
123 {
124 Display* display = (Display*) m_window->GetXDisplay();
125 glXMakeCurrent(display, (Window) m_window->GetXWindow(),
126 m_glContext );;
127 }
128}
129
130void wxGLContext::SetColour(const char *colour)
131{
132 wxColour *the_colour = wxTheColourDatabase->FindColour(colour);
133 if(the_colour) {
134 GLboolean b;
135 glGetBooleanv(GL_RGBA_MODE, &b);
136 if(b) {
137 glColor3ub(the_colour->Red(),
138 the_colour->Green(),
139 the_colour->Blue());
140 } else {
141 GLint pix = (GLint)the_colour->m_pixel;
142 if(pix == -1) {
143 XColor exact_def;
144 exact_def.red = (unsigned short)the_colour->Red() << 8;
145 exact_def.green = (unsigned short)the_colour->Green() << 8;
146 exact_def.blue = (unsigned short)the_colour->Blue() << 8;
147 exact_def.flags = DoRed | DoGreen | DoBlue;
148 if(!XAllocColor((Display*) m_window->GetXDisplay(),
149 (Colormap) wxTheApp->GetMainColormap(m_window->GetXDisplay()),
150 &exact_def)) {
b64c9793 151 wxLogError("wxGLCanvas: cannot allocate color\n");
2695b174
UN
152 return;
153 }
154 pix = the_colour->m_pixel = exact_def.pixel;
155 }
156 glIndexi(pix);
157 }
158 }
159}
160
161void wxGLContext::SetupPixelFormat()
162{
163}
164
165void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
166{
167}
168
169wxPalette wxGLContext::CreateDefaultPalette()
170{
171 return wxNullPalette;
172}
173
174
175
176
8b089c5e
JS
177/*
178 * GLCanvas implementation
179 */
180
181IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
182
2695b174
UN
183BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
184// EVT_SIZE(wxGLCanvas::OnSize)
185END_EVENT_TABLE()
186
187
188wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
189 const wxPoint& pos, const wxSize& size,
190 long style, const wxString& name,
191 int *attribList,
192 const wxPalette& palette )
193: wxScrolledWindow(parent, id, pos, size, style, name)
194{
195 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
196}
197
198wxGLCanvas::wxGLCanvas( wxWindow *parent,
199 const wxGLContext *shared,
200 wxWindowID id,
201 const wxPoint& pos, const wxSize& size,
202 long style, const wxString& name,
203 int *attribList,
204 const wxPalette& palette )
205: wxScrolledWindow(parent, id, pos, size, style, name)
206{
207 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
208}
209
210wxGLCanvas::wxGLCanvas( wxWindow *parent,
211 const wxGLCanvas *shared,
212 wxWindowID id,
213 const wxPoint& pos, const wxSize& size,
214 long style, const wxString& name,
215 int *attribList,
216 const wxPalette& palette )
217: wxScrolledWindow(parent, id, pos, size, style, name)
218{
219 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
220}
221
222
223/*
224bool wxGLCanvas::Create(wxWindow *parent,
225 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
226 wxWindowID id = -1, const wxPoint& pos,
f48d169c
UN
227 const wxSize& size, long style,
228 const wxString& name, int *attribList, const wxPalette& palette):
229 wxScrolledWindow(parent, id, pos, size, style, name)
2695b174
UN
230*/
231
232bool wxGLCanvas::Create( wxWindow *parent,
233 const wxGLContext *shared,
234 const wxGLCanvas *shared_context_of,
235 wxWindowID id,
236 const wxPoint& pos, const wxSize& size,
237 long style, const wxString& name,
238 int *attribList,
239 const wxPalette& palette)
8b089c5e
JS
240{
241 XVisualInfo *vi, vi_templ;
242 XWindowAttributes xwa;
243 int val, n;
244
2695b174
UN
245 m_sharedContext = (wxGLContext*)shared; // const_cast
246 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
247 m_glContext = (wxGLContext*) NULL;
248
8b089c5e
JS
249 Display* display = (Display*) GetXDisplay();
250
8b089c5e
JS
251 // Check for the presence of the GLX extension
252 if(!glXQueryExtension(display, NULL, NULL)) {
b64c9793 253 wxLogError("wxGLCanvas: GLX extension is missing\n");
2695b174 254 return false;
8b089c5e
JS
255 }
256
f48d169c
UN
257 if(attribList) {
258 int data[512], arg=0, p=0;
259
260 while( (attribList[arg]!=0) && (p<512) )
261 {
262 switch( attribList[arg++] )
263 {
264 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
265 case WX_GL_BUFFER_SIZE:
266 data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
267 case WX_GL_LEVEL:
268 data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break;
269 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
270 case WX_GL_STEREO: data[p++] = GLX_STEREO; break;
271 case WX_GL_AUX_BUFFERS:
272 data[p++]=GLX_AUX_BUFFERS; data[p++]=attribList[arg++]; break;
273 case WX_GL_MIN_RED:
274 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
275 case WX_GL_MIN_GREEN:
276 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
277 case WX_GL_MIN_BLUE:
278 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
279 case WX_GL_MIN_ALPHA:
280 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
281 case WX_GL_DEPTH_SIZE:
282 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
283 case WX_GL_STENCIL_SIZE:
284 data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
285 case WX_GL_MIN_ACCUM_RED:
286 data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
287 case WX_GL_MIN_ACCUM_GREEN:
288 data[p++]=GLX_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break;
289 case WX_GL_MIN_ACCUM_BLUE:
290 data[p++]=GLX_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break;
291 case WX_GL_MIN_ACCUM_ALPHA:
292 data[p++]=GLX_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
293 default:
294 break;
295 }
296 }
297 data[p] = 0;
298
299 attribList = (int*) data;
300 // Get an appropriate visual
301 vi = glXChooseVisual(display, DefaultScreen(display), attribList);
2695b174 302 if(!vi) return false;
f48d169c
UN
303
304 // Here we should make sure that vi is the same visual as the
305 // one used by the xwindow drawable in wxCanvas. However,
306 // there is currently no mechanism for this in wx_canvs.cc.
8b089c5e 307 } else {
f48d169c
UN
308 // By default, we use the visual of xwindow
309 // NI: is this really senseful ? opengl in e.g. color index mode ?
310 XGetWindowAttributes(display, (Window) GetXWindow(), &xwa);
311 vi_templ.visualid = XVisualIDFromVisual(xwa.visual);
312 vi = XGetVisualInfo(display, VisualIDMask, &vi_templ, &n);
2695b174 313 if(!vi) return false;
f48d169c 314 glXGetConfig(display, vi, GLX_USE_GL, &val);
2695b174 315 if(!val) return false;
f48d169c
UN
316 // Basically, this is it. It should be possible to use vi
317 // in glXCreateContext() below. But this fails with Mesa.
318 // I notified the Mesa author about it; there may be a fix.
8b089c5e 319#ifdef OLD_MESA
f48d169c
UN
320 // Construct an attribute list matching the visual
321 int a_list[32];
322 n = 0;
323 if(vi->c_class==TrueColor || vi->c_class==DirectColor) { // RGBA visual
324 a_list[n++] = GLX_RGBA;
325 a_list[n++] = GLX_RED_SIZE;
326 a_list[n++] = bitcount(vi->red_mask);
327 a_list[n++] = GLX_GREEN_SIZE;
328 a_list[n++] = bitcount(vi->green_mask);
329 a_list[n++] = GLX_BLUE_SIZE;
330 a_list[n++] = bitcount(vi->blue_mask);
331 glXGetConfig(display, vi, GLX_ALPHA_SIZE, &val);
332 a_list[n++] = GLX_ALPHA_SIZE;
333 a_list[n++] = val;
334 } else { // Color index visual
335 glXGetConfig(display, vi, GLX_BUFFER_SIZE, &val);
336 a_list[n++] = GLX_BUFFER_SIZE;
337 a_list[n++] = val;
338 }
339 a_list[n] = None;
2695b174 340 // XFree(vi);
f48d169c 341 vi = glXChooseVisual(display, DefaultScreen(display), a_list);
2695b174 342 if(!vi) return false;
8b089c5e
JS
343#endif /* OLD_MESA */
344 }
345
2695b174
UN
346 m_vi = vi; // safe for later use
347
348 wxCHECK_MSG( m_vi, FALSE, "required visual couldn't be found" );
349
8b089c5e 350 // Create the GLX context and make it current
2695b174
UN
351
352 wxGLContext *share= m_sharedContext;
353 if (share==NULL && m_sharedContextOf)
354 share = m_sharedContextOf->GetContext();
355
356 m_glContext = new wxGLContext( TRUE, this, wxNullPalette, share );
357
8b089c5e 358#ifndef OLD_MESA
2695b174 359 // XFree(vi);
8b089c5e
JS
360#endif
361 SetCurrent();
2695b174
UN
362
363 return true;
8b089c5e
JS
364}
365
366wxGLCanvas::~wxGLCanvas(void)
367{
2695b174
UN
368 XVisualInfo *vi = (XVisualInfo *) m_vi;
369
370 if (vi) XFree( vi );
371 if (m_glContext) delete m_glContext;
372
373 // Display* display = (Display*) GetXDisplay();
374 // if(glx_cx) glXDestroyContext(display, glx_cx);
8b089c5e
JS
375}
376
377void wxGLCanvas::SwapBuffers()
378{
2695b174
UN
379 if( m_glContext ) m_glContext->SwapBuffers();
380
381 // Display* display = (Display*) GetXDisplay();
382 // if(glx_cx) glXSwapBuffers(display, (Window) GetXWindow());
8b089c5e
JS
383}
384
385void wxGLCanvas::SetCurrent()
386{
2695b174
UN
387 if( m_glContext ) m_glContext->SetCurrent();
388
389 // Display* display = (Display*) GetXDisplay();
390 // if(glx_cx) glXMakeCurrent(display, (Window) GetXWindow(), glx_cx);
8b089c5e
JS
391}
392
393void wxGLCanvas::SetColour(const char *col)
394{
2695b174 395 if( m_glContext ) m_glContext->SetColour(col);
8b089c5e
JS
396}
397
398#endif
399 // wxUSE_GLCANVAS
400