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(
56 bool WXUNUSED(isRGB
), wxWindow
*win
,
57 const wxPalette
& WXUNUSED(palette
),
58 const wxGLContext
*other
/* for sharing display lists */
62 m_widget
= ((wxGLCanvas
*)win
)->m_glWidget
;
64 wxCHECK_RET( g_vi
, "invalid visual for OpenGl" );
67 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, other
->m_glContext
,
70 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, None
, GL_TRUE
);
72 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
75 wxGLContext::~wxGLContext()
77 if (!m_glContext
) return;
79 if (m_glContext
== glXGetCurrentContext())
81 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
84 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
87 void wxGLContext::SwapBuffers()
91 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget
->window
) );
95 void wxGLContext::SetCurrent()
99 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget
->window
), m_glContext
);
103 void wxGLContext::SetColour(const char *colour
)
108 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
111 r
= (float)(col
->Red()/256.0);
112 g
= (float)(col
->Green()/256.0);
113 b
= (float)(col
->Blue()/256.0);
118 void wxGLContext::SetupPixelFormat()
122 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
126 wxPalette
wxGLContext::CreateDefaultPalette()
128 return wxNullPalette
;
131 //-----------------------------------------------------------------------------
132 // "expose_event" of m_glWidget
133 //-----------------------------------------------------------------------------
135 static void gtk_window_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxWindow
*win
)
137 if (!win
->m_hasVMT
) return;
139 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
141 gdk_event
->area
.width
,
142 gdk_event
->area
.height
);
144 if (gdk_event
->count
> 0) return;
147 printf( "OnExpose from " );
148 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
149 printf( win->GetClassInfo()->GetClassName() );
153 wxPaintEvent
event( win
->GetId() );
154 event
.SetEventObject( win
);
155 win
->GetEventHandler()->ProcessEvent( event
);
157 win
->GetUpdateRegion().Clear();
160 //-----------------------------------------------------------------------------
161 // "draw" of m_glWidget
162 //-----------------------------------------------------------------------------
164 static void gtk_window_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxWindow
*win
)
166 if (!win
->m_hasVMT
) return;
168 win
->GetUpdateRegion().Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
170 wxPaintEvent
event( win
->GetId() );
171 event
.SetEventObject( win
);
172 win
->GetEventHandler()->ProcessEvent( event
);
174 win
->GetUpdateRegion().Clear();
177 //---------------------------------------------------------------------------
179 //---------------------------------------------------------------------------
181 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
183 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
184 EVT_SIZE(wxGLCanvas::OnSize
)
187 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
188 const wxPoint
& pos
, const wxSize
& size
,
189 long style
, const wxString
& name
,
191 const wxPalette
& palette
)
193 Create( parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
196 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
197 const wxGLContext
*shared
,
199 const wxPoint
& pos
, const wxSize
& size
,
200 long style
, const wxString
& name
,
202 const wxPalette
& palette
)
204 Create( parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
207 bool wxGLCanvas::Create( wxWindow
*parent
,
208 const wxGLContext
*shared
,
210 const wxPoint
& pos
, const wxSize
& size
,
211 long style
, const wxString
& name
,
213 const wxPalette
& palette
)
217 int data
[] = { GLX_RGBA
,
219 GLX_DEPTH_SIZE
, 1, /* use largest available depth buffer */
225 attribList
= (int*) data
;
229 int data
[512], arg
=0, p
=0;
231 while( (attribList
[arg
]!=0) && (p
<512) )
233 switch( attribList
[arg
++] )
235 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
236 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
237 case WX_GL_DEPTH_SIZE
:
238 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
240 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
241 case WX_GL_MIN_GREEN
:
242 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
244 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
251 attribList
= (int*) data
;
254 Display
*dpy
= GDK_DISPLAY();
256 g_vi
= glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
258 GdkVisual
*visual
= gdkx_visual_get( g_vi
->visualid
);
259 GdkColormap
*colormap
= gdk_colormap_new( gdkx_visual_get(g_vi
->visualid
), TRUE
);
261 gtk_widget_push_colormap( colormap
);
262 gtk_widget_push_visual( visual
);
264 m_glWidget
= gtk_myfixed_new();
266 gtk_widget_pop_visual();
267 gtk_widget_pop_colormap();
269 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
271 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
272 GTK_WIDGET_SET_FLAGS( m_glWidget
, GTK_CAN_FOCUS
);
274 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow
), m_glWidget
, 0, 0, m_width
, m_height
);
276 gtk_signal_connect( GTK_OBJECT(m_glWidget
), "expose_event",
277 GTK_SIGNAL_FUNC(gtk_window_expose_callback
), (gpointer
)this );
279 gtk_signal_connect( GTK_OBJECT(m_glWidget
), "draw",
280 GTK_SIGNAL_FUNC(gtk_window_draw_callback
), (gpointer
)this );
282 /* connect to key press and mouse handlers etc. */
283 ConnectWidget( m_glWidget
);
286 /* must be realized for OpenGl output */
287 gtk_widget_realize( m_glWidget
);
289 gtk_widget_show( m_glWidget
);
291 m_glContext
= new wxGLContext( TRUE
, this, palette
, shared
);
294 g_vi
= (XVisualInfo
*) NULL
;
296 gdk_window_set_back_pixmap( m_glWidget
->window
, None
, 0 );
301 wxGLCanvas::~wxGLCanvas()
303 if (m_glContext
) delete m_glContext
;
306 void wxGLCanvas::SwapBuffers()
308 if (m_glContext
) m_glContext
->SwapBuffers();
311 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
314 GetClientSize( &width
, &height
);
315 if (m_glContext
&& GTK_WIDGET_REALIZED(m_glWidget
) )
318 // gdk_window_set_back_pixmap( gtk_widget_get_parent_window(m_glWidget), None, 0 );
320 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
321 glMatrixMode(GL_PROJECTION
);
323 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
324 glMatrixMode(GL_MODELVIEW
);
328 void wxGLCanvas::SetCurrent()
330 if (m_glContext
) m_glContext
->SetCurrent();
333 void wxGLCanvas::SetColour( const char *colour
)
335 if (m_glContext
) m_glContext
->SetColour( colour
);
338 void wxGLCanvas::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
340 if (m_resizing
) return; // I don't like recursions
343 if (m_parent
->m_wxwindow
== NULL
) // i.e. wxNotebook
345 // don't set the size for children of wxNotebook, just take the values.
353 int old_width
= m_width
;
354 int old_height
= m_height
;
356 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
358 if (x
!= -1) m_x
= x
;
359 if (y
!= -1) m_y
= y
;
360 if (width
!= -1) m_width
= width
;
361 if (height
!= -1) m_height
= height
;
371 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
373 if (width
== -1) m_width
= 80;
376 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
378 if (height
== -1) m_height
= 26;
381 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
382 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
383 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
384 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
386 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
),
393 gtk_myfixed_set_size( GTK_MYFIXED(m_wxwindow
),
403 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
404 event
.SetEventObject( this );
405 GetEventHandler()->ProcessEvent( event
);
410 GtkWidget
*wxGLCanvas::GetConnectWidget()
415 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow
*window
)
417 return (window
== m_glWidget
->window
);