]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/gtk/glcanvas.cpp
fix for menu separator
[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 {
57 if (!m_glContext) return;
58
59 if (m_glContext == glXGetCurrentContext())
60 {
61 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
62 }
63
64 glXDestroyContext( GDK_DISPLAY(), m_glContext );
65 }
66
67 void wxGLContext::SwapBuffers()
68 {
69 if (m_glContext)
70 {
71 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) );
72 }
73 }
74
75 void wxGLContext::SetCurrent()
76 {
77 if (m_glContext)
78 {
79 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
80 }
81 }
82
83 void wxGLContext::SetColour(const char *colour)
84 {
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 }
96 }
97
98 void wxGLContext::SetupPixelFormat()
99 {
100 }
101
102 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
103 {
104 }
105
106 wxPalette wxGLContext::CreateDefaultPalette()
107 {
108 return wxNullPalette;
109 }
110
111 //-----------------------------------------------------------------------------
112 // "expose_event" of m_glWidget
113 //-----------------------------------------------------------------------------
114
115 static void gtk_window_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxWindow *win )
116 {
117 if (!win->HasVMT()) return;
118
119 win->m_updateRegion.Union( gdk_event->area.x,
120 gdk_event->area.y,
121 gdk_event->area.width,
122 gdk_event->area.height );
123
124 if (gdk_event->count > 0) return;
125
126 /*
127 printf( "OnExpose from " );
128 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
129 printf( win->GetClassInfo()->GetClassName() );
130 printf( ".\n" );
131 */
132
133 wxPaintEvent event( win->GetId() );
134 event.SetEventObject( win );
135 win->GetEventHandler()->ProcessEvent( event );
136
137 win->m_updateRegion.Clear();
138 }
139
140 //-----------------------------------------------------------------------------
141 // "draw" of m_glWidget
142 //-----------------------------------------------------------------------------
143
144 static void gtk_window_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxWindow *win )
145 {
146 if (!win->HasVMT()) return;
147
148 win->m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
149
150 wxPaintEvent event( win->GetId() );
151 event.SetEventObject( win );
152 win->GetEventHandler()->ProcessEvent( event );
153
154 win->m_updateRegion.Clear();
155 }
156
157 //---------------------------------------------------------------------------
158 // wxGlCanvas
159 //---------------------------------------------------------------------------
160
161 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
162
163 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
164 EVT_SIZE(wxGLCanvas::OnSize)
165 END_EVENT_TABLE()
166
167 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
168 const wxPoint& pos, const wxSize& size,
169 long style, const wxString& name,
170 int *attribList,
171 const wxPalette& palette )
172 {
173 Create( parent, id, pos, size, style, name, attribList, palette );
174 }
175
176 bool wxGLCanvas::Create( wxWindow *parent, wxWindowID id,
177 const wxPoint& pos, const wxSize& size,
178 long style, const wxString& name,
179 int *attribList,
180 const wxPalette& palette )
181 {
182 if (!attribList)
183 {
184 int data[] = { GLX_RGBA,
185 GLX_DOUBLEBUFFER,
186 GLX_DEPTH_SIZE, 1,
187 None };
188 attribList = (int*) data;
189 }
190
191 Display *dpy = GDK_DISPLAY();
192
193 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
194
195 GdkVisual *visual = gdkx_visual_get( g_vi->visualid );
196 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE );
197
198 gtk_widget_push_colormap( colormap );
199 gtk_widget_push_visual( visual );
200
201 m_glWidget = gtk_myfixed_new();
202
203 gtk_widget_pop_visual();
204 gtk_widget_pop_colormap();
205
206 wxScrolledWindow::Create( parent, id, pos, size, style, name );
207
208 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
209 GTK_WIDGET_SET_FLAGS( m_glWidget, GTK_CAN_FOCUS );
210
211 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), m_glWidget, 0, 0, m_width, m_height );
212
213 gtk_signal_connect( GTK_OBJECT(m_glWidget), "expose_event",
214 GTK_SIGNAL_FUNC(gtk_window_expose_callback), (gpointer)this );
215
216 gtk_signal_connect( GTK_OBJECT(m_glWidget), "draw",
217 GTK_SIGNAL_FUNC(gtk_window_draw_callback), (gpointer)this );
218
219 /* connect to key press and mouse handlers etc. */
220 ConnectWidget( m_glWidget );
221
222 /* must be realized for OpenGl output */
223 gtk_widget_realize( m_glWidget );
224
225 gtk_widget_show( m_glWidget );
226
227 m_glContext = new wxGLContext( TRUE, this, palette );
228
229 XFree( g_vi );
230 g_vi = (XVisualInfo*) NULL;
231
232 return TRUE;
233 }
234
235 wxGLCanvas::~wxGLCanvas()
236 {
237 if (m_glContext) delete m_glContext;
238 }
239
240 void wxGLCanvas::SwapBuffers()
241 {
242 if (m_glContext) m_glContext->SwapBuffers();
243 }
244
245 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
246 {
247 int width, height;
248 GetClientSize( &width, &height );
249 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
250 {
251 SetCurrent();
252
253 glViewport(0, 0, (GLint)width, (GLint)height );
254 glMatrixMode(GL_PROJECTION);
255 glLoadIdentity();
256 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
257 glMatrixMode(GL_MODELVIEW);
258 }
259 }
260
261 void wxGLCanvas::SetCurrent()
262 {
263 if (m_glContext) m_glContext->SetCurrent();
264 }
265
266 void wxGLCanvas::SetColour( const char *colour )
267 {
268 if (m_glContext) m_glContext->SetColour( colour );
269 }
270
271 void wxGLCanvas::DoSetSize( int x, int y, int width, int height, int sizeFlags )
272 {
273 if (m_resizing) return; // I don't like recursions
274 m_resizing = TRUE;
275
276 if (m_parent->m_wxwindow == NULL) // i.e. wxNotebook
277 {
278 // don't set the size for children of wxNotebook, just take the values.
279 m_x = x;
280 m_y = y;
281 m_width = width;
282 m_height = height;
283 }
284 else
285 {
286 int old_width = m_width;
287 int old_height = m_height;
288
289 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
290 {
291 if (x != -1) m_x = x;
292 if (y != -1) m_y = y;
293 if (width != -1) m_width = width;
294 if (height != -1) m_height = height;
295 }
296 else
297 {
298 m_x = x;
299 m_y = y;
300 m_width = width;
301 m_height = height;
302 }
303
304 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
305 {
306 if (width == -1) m_width = 80;
307 }
308
309 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
310 {
311 if (height == -1) m_height = 26;
312 }
313
314 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
315 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
316 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
317 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
318
319 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow),
320 m_widget,
321 m_x,
322 m_y,
323 m_width,
324 m_height );
325
326 gtk_myfixed_set_size( GTK_MYFIXED(m_wxwindow),
327 m_glWidget,
328 m_x,
329 m_y,
330 m_width,
331 m_height );
332 }
333
334 m_sizeSet = TRUE;
335
336 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
337 event.SetEventObject( this );
338 GetEventHandler()->ProcessEvent( event );
339
340 m_resizing = FALSE;
341 }
342
343 GtkWidget *wxGLCanvas::GetConnectWidget()
344 {
345 return m_glWidget;
346 }
347
348 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window )
349 {
350 return (window == m_glWidget->window);
351 }