1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "glcanvas.h"
19 #include "wx/colour.h"
20 #include "wx/module.h"
29 #include "wx/gtk/win_gtk.h"
31 //---------------------------------------------------------------------------
33 //---------------------------------------------------------------------------
35 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
37 //---------------------------------------------------------------------------
39 //---------------------------------------------------------------------------
41 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
43 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
46 m_widget
= ((wxGLCanvas
*)win
)->m_glWidget
;
48 wxCHECK_RET( g_vi
, "invalid visual for OpenGl" );
50 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, None
, GL_TRUE
);
52 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
55 wxGLContext::~wxGLContext()
57 if (!m_glContext
) return;
59 if (m_glContext
== glXGetCurrentContext())
61 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
64 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
67 void wxGLContext::SwapBuffers()
71 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget
->window
) );
75 void wxGLContext::SetCurrent()
79 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget
->window
), m_glContext
);
83 void wxGLContext::SetColour(const char *colour
)
88 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
91 r
= (float)(col
->Red()/256.0);
92 g
= (float)(col
->Green()/256.0);
93 b
= (float)(col
->Blue()/256.0);
98 void wxGLContext::SetupPixelFormat()
102 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
106 wxPalette
wxGLContext::CreateDefaultPalette()
108 return wxNullPalette
;
111 //-----------------------------------------------------------------------------
112 // "expose_event" of m_glWidget
113 //-----------------------------------------------------------------------------
115 static void gtk_window_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxWindow
*win
)
117 if (!win
->HasVMT()) return;
119 win
->m_updateRegion
.Union( gdk_event
->area
.x
,
121 gdk_event
->area
.width
,
122 gdk_event
->area
.height
);
124 if (gdk_event
->count
> 0) return;
127 printf( "OnExpose from " );
128 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
129 printf( win->GetClassInfo()->GetClassName() );
133 wxPaintEvent
event( win
->GetId() );
134 event
.SetEventObject( win
);
135 win
->GetEventHandler()->ProcessEvent( event
);
137 win
->m_updateRegion
.Clear();
140 //-----------------------------------------------------------------------------
141 // "draw" of m_glWidget
142 //-----------------------------------------------------------------------------
144 static void gtk_window_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxWindow
*win
)
146 if (!win
->HasVMT()) return;
148 win
->m_updateRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
150 wxPaintEvent
event( win
->GetId() );
151 event
.SetEventObject( win
);
152 win
->GetEventHandler()->ProcessEvent( event
);
154 win
->m_updateRegion
.Clear();
157 //---------------------------------------------------------------------------
159 //---------------------------------------------------------------------------
161 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
163 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
164 EVT_SIZE(wxGLCanvas::OnSize
)
167 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
168 const wxPoint
& pos
, const wxSize
& size
,
169 long style
, const wxString
& name
,
171 const wxPalette
& palette
)
173 Create( parent
, id
, pos
, size
, style
, name
, attribList
, palette
);
176 bool wxGLCanvas::Create( wxWindow
*parent
, wxWindowID id
,
177 const wxPoint
& pos
, const wxSize
& size
,
178 long style
, const wxString
& name
,
180 const wxPalette
& palette
)
184 int data
[] = { GLX_RGBA
,
188 attribList
= (int*) data
;
191 Display
*dpy
= GDK_DISPLAY();
193 g_vi
= glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
195 GdkVisual
*visual
= gdkx_visual_get( g_vi
->visualid
);
196 GdkColormap
*colormap
= gdk_colormap_new( gdkx_visual_get(g_vi
->visualid
), TRUE
);
198 gtk_widget_push_colormap( colormap
);
199 gtk_widget_push_visual( visual
);
201 m_glWidget
= gtk_drawing_area_new();
202 gtk_widget_set_events( m_glWidget
,
204 GDK_POINTER_MOTION_HINT_MASK
|
205 GDK_POINTER_MOTION_MASK
|
206 GDK_BUTTON_MOTION_MASK
|
207 GDK_BUTTON1_MOTION_MASK
|
208 GDK_BUTTON2_MOTION_MASK
|
209 GDK_BUTTON3_MOTION_MASK
|
210 GDK_BUTTON_PRESS_MASK
|
211 GDK_BUTTON_RELEASE_MASK
|
213 GDK_KEY_RELEASE_MASK
|
214 GDK_ENTER_NOTIFY_MASK
|
215 GDK_LEAVE_NOTIFY_MASK
);
217 gtk_widget_pop_visual();
218 gtk_widget_pop_colormap();
220 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
222 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow
), m_glWidget
, 0, 0 );
224 gtk_signal_connect( GTK_OBJECT(m_glWidget
), "expose_event",
225 GTK_SIGNAL_FUNC(gtk_window_expose_callback
), (gpointer
)this );
227 gtk_signal_connect( GTK_OBJECT(m_glWidget
), "draw",
228 GTK_SIGNAL_FUNC(gtk_window_draw_callback
), (gpointer
)this );
230 /* connect to key press and mouse handlers etc. */
231 ConnectWidget( m_glWidget
);
233 gtk_widget_show( m_glWidget
);
235 m_glContext
= new wxGLContext( TRUE
, this, palette
);
238 g_vi
= (XVisualInfo
*) NULL
;
243 wxGLCanvas::~wxGLCanvas()
245 if (m_glContext
) delete m_glContext
;
248 void wxGLCanvas::SwapBuffers()
250 if (m_glContext
) m_glContext
->SwapBuffers();
253 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
256 GetClientSize( &width
, &height
);
257 if (m_glContext
&& GTK_WIDGET_REALIZED(m_glWidget
) )
261 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
262 glMatrixMode(GL_PROJECTION
);
264 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
265 glMatrixMode(GL_MODELVIEW
);
269 void wxGLCanvas::SetCurrent()
271 if (m_glContext
) m_glContext
->SetCurrent();
274 void wxGLCanvas::SetColour( const char *colour
)
276 if (m_glContext
) m_glContext
->SetColour( colour
);
279 void wxGLCanvas::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
281 if (m_resizing
) return; // I don't like recursions
284 if (m_parent
->m_wxwindow
== NULL
) // i.e. wxNotebook
286 // don't set the size for children of wxNotebook, just take the values.
294 int old_width
= m_width
;
295 int old_height
= m_height
;
297 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
299 if (x
!= -1) m_x
= x
;
300 if (y
!= -1) m_y
= y
;
301 if (width
!= -1) m_width
= width
;
302 if (height
!= -1) m_height
= height
;
312 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
314 if (width
== -1) m_width
= 80;
317 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
319 if (height
== -1) m_height
= 26;
322 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
323 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
324 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
325 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
327 wxPoint
pt( m_parent
->GetClientAreaOrigin() );
328 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), m_widget
, m_x
+pt
.x
, m_y
+pt
.y
);
330 if ((old_width
!= m_width
) || (old_height
!= m_height
))
332 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
334 gtk_drawing_area_size( GTK_DRAWING_AREA(m_glWidget
), m_width
, m_height
);
339 allo
.width
= m_width
;
340 allo
.height
= m_height
;
341 gtk_widget_size_allocate( m_glWidget
, &allo
);
347 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
348 event
.SetEventObject( this );
349 GetEventHandler()->ProcessEvent( event
);
354 void wxGLCanvas::SetSize( int width
, int height
)
356 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
359 GtkWidget
*wxGLCanvas::GetConnectWidget()
364 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow
*window
)
366 return (window
== m_glWidget
->window
);