]> git.saurik.com Git - wxWidgets.git/blame - utils/glcanvas/gtk/glcanvas.cpp
Small Tex2RTF fixes; wxMotif compile fixes (motif.inc, wxCheckListBox);
[wxWidgets.git] / utils / glcanvas / gtk / glcanvas.cpp
CommitLineData
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 23extern "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 35XVisualInfo *g_vi = (XVisualInfo*) NULL;
9d3221ab
RR
36
37//---------------------------------------------------------------------------
38// wxGLContext
39//---------------------------------------------------------------------------
40
41IMPLEMENT_CLASS(wxGLContext,wxObject)
42
43wxGLContext::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
55wxGLContext::~wxGLContext()
56{
aae24d21 57 if (!m_glContext) return;
9d3221ab 58
aae24d21
RR
59 if (m_glContext == glXGetCurrentContext())
60 {
61 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
bbe0af5b 62 }
aae24d21
RR
63
64 glXDestroyContext( GDK_DISPLAY(), m_glContext );
9d3221ab
RR
65}
66
67void wxGLContext::SwapBuffers()
68{
bbe0af5b
RR
69 if (m_glContext)
70 {
71 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) );
72 }
9d3221ab
RR
73}
74
75void wxGLContext::SetCurrent()
76{
bbe0af5b
RR
77 if (m_glContext)
78 {
79 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
80 }
9d3221ab
RR
81}
82
83void wxGLContext::SetColour(const char *colour)
84{
bbe0af5b
RR
85 float r = 0.0;
86 float g = 0.0;
87 float b = 0.0;
88 wxColour *col = wxTheColourDatabase->FindColour(colour);
89 if (col)
90 {
91 r = (float)(col->Red()/256.0);
92 g = (float)(col->Green()/256.0);
93 b = (float)(col->Blue()/256.0);
94 glColor3f( r, g, b);
95 }
9d3221ab
RR
96}
97
98void wxGLContext::SetupPixelFormat()
99{
100}
101
102void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
103{
104}
105
106wxPalette wxGLContext::CreateDefaultPalette()
107{
bbe0af5b 108 return wxNullPalette;
9d3221ab
RR
109}
110
111//---------------------------------------------------------------------------
112// wxGlCanvas
113//---------------------------------------------------------------------------
114
115IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
116
117BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
118 EVT_SIZE(wxGLCanvas::OnSize)
119END_EVENT_TABLE()
120
aae24d21
RR
121wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
122 const wxPoint& pos, const wxSize& size,
123 long style, const wxString& name,
124 int *attribList,
125 const wxPalette& palette )
126{
127 Create( parent, id, pos, size, style, name, attribList, palette );
128}
129
130bool wxGLCanvas::Create( wxWindow *parent, wxWindowID id,
131 const wxPoint& pos, const wxSize& size,
132 long style, const wxString& name,
133 int *attribList,
134 const wxPalette& palette )
9d3221ab 135{
aae24d21
RR
136 if (!attribList)
137 {
138 int data[] = { GLX_RGBA,
139 GLX_DOUBLEBUFFER,
140 GLX_DEPTH_SIZE, 1,
141 None };
142 attribList = (int*) data;
143 }
144
145 Display *dpy = GDK_DISPLAY();
146
147 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
148
149 GdkVisual *visual = gdkx_visual_get( g_vi->visualid );
150 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE );
151
152 gtk_widget_push_colormap( colormap );
153 gtk_widget_push_visual( visual );
154
155 m_glWidget = gtk_drawing_area_new();
156 gtk_widget_set_events( m_glWidget,
157 GDK_EXPOSURE_MASK |
158 GDK_POINTER_MOTION_HINT_MASK |
159 GDK_POINTER_MOTION_MASK |
160 GDK_BUTTON_MOTION_MASK |
161 GDK_BUTTON1_MOTION_MASK |
162 GDK_BUTTON2_MOTION_MASK |
163 GDK_BUTTON3_MOTION_MASK |
164 GDK_BUTTON_PRESS_MASK |
165 GDK_BUTTON_RELEASE_MASK |
166 GDK_KEY_PRESS_MASK |
167 GDK_KEY_RELEASE_MASK |
168 GDK_ENTER_NOTIFY_MASK |
169 GDK_LEAVE_NOTIFY_MASK );
170
171 gtk_widget_pop_visual();
172 gtk_widget_pop_colormap();
173
174 wxScrolledWindow::Create( parent, id, pos, size, style, name );
175
176 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), m_glWidget, 0, 0 );
177
178 gtk_widget_show( m_glWidget );
179
bbe0af5b 180 m_glContext = new wxGLContext( TRUE, this, palette );
aae24d21
RR
181
182 XFree( g_vi );
183 g_vi = (XVisualInfo*) NULL;
184
185 return TRUE;
9d3221ab
RR
186}
187
188wxGLCanvas::~wxGLCanvas()
189{
bbe0af5b 190 if (m_glContext) delete m_glContext;
9d3221ab
RR
191}
192
193void wxGLCanvas::SwapBuffers()
194{
bbe0af5b 195 if (m_glContext) m_glContext->SwapBuffers();
9d3221ab
RR
196}
197
198void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
199{
bbe0af5b 200 int width, height;
aae24d21
RR
201 GetClientSize( &width, &height );
202 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
bbe0af5b 203 {
aae24d21
RR
204 SetCurrent();
205
206 glViewport(0, 0, (GLint)width, (GLint)height );
bbe0af5b
RR
207 glMatrixMode(GL_PROJECTION);
208 glLoadIdentity();
209 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
210 glMatrixMode(GL_MODELVIEW);
211 }
9d3221ab
RR
212}
213
214void wxGLCanvas::SetCurrent()
215{
bbe0af5b 216 if (m_glContext) m_glContext->SetCurrent();
9d3221ab
RR
217}
218
219void wxGLCanvas::SetColour( const char *colour )
220{
bbe0af5b 221 if (m_glContext) m_glContext->SetColour( colour );
9d3221ab
RR
222}
223
aae24d21 224void wxGLCanvas::SetSize( int x, int y, int width, int height, int sizeFlags )
bbe0af5b 225{
aae24d21
RR
226 if (m_resizing) return; // I don't like recursions
227 m_resizing = TRUE;
228
229 if (m_parent->m_wxwindow == NULL) // i.e. wxNotebook
230 {
231 // don't set the size for children of wxNotebook, just take the values.
232 m_x = x;
233 m_y = y;
234 m_width = width;
235 m_height = height;
236 }
237 else
238 {
239 int old_width = m_width;
240 int old_height = m_height;
241
242 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
243 {
244 if (x != -1) m_x = x;
245 if (y != -1) m_y = y;
246 if (width != -1) m_width = width;
247 if (height != -1) m_height = height;
248 }
249 else
250 {
251 m_x = x;
252 m_y = y;
253 m_width = width;
254 m_height = height;
255 }
256
257 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
258 {
259 if (width == -1) m_width = 80;
260 }
261
262 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
263 {
264 if (height == -1) m_height = 26;
265 }
266
267 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
268 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
269 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
270 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
271
272 wxPoint pt( m_parent->GetClientAreaOrigin() );
273 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), m_widget, m_x+pt.x, m_y+pt.y );
274
275 if ((old_width != m_width) || (old_height != m_height))
276 {
277 gtk_widget_set_usize( m_widget, m_width, m_height );
278
279 gtk_drawing_area_size( GTK_DRAWING_AREA(m_glWidget), m_width, m_height );
280
281 GtkAllocation allo;
282 allo.x = 0;
283 allo.y = 0;
284 allo.width = m_width;
285 allo.height = m_height;
286 gtk_widget_size_allocate( m_glWidget, &allo );
287 }
288 }
bbe0af5b 289
aae24d21 290 m_sizeSet = TRUE;
bbe0af5b 291
aae24d21
RR
292 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
293 event.SetEventObject( this );
294 GetEventHandler()->ProcessEvent( event );
bbe0af5b 295
aae24d21
RR
296 m_resizing = FALSE;
297}
bbe0af5b 298
aae24d21
RR
299void wxGLCanvas::SetSize( int width, int height )
300{
301 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
bbe0af5b
RR
302}
303
aae24d21 304GtkWidget *wxGLCanvas::GetConnectWidget()
bbe0af5b 305{
aae24d21 306 return m_glWidget;
bbe0af5b 307}
9d3221ab 308
aae24d21
RR
309bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window )
310{
311 return (window == m_glWidget->window);
312}