]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/glcanvas.cpp
disable eVC makefiles for sockets (no CLI targets possible on this platform)
[wxWidgets.git] / src / gtk / glcanvas.cpp
CommitLineData
8b089c5e 1/////////////////////////////////////////////////////////////////////////////
9b5f1895 2// Name: src/gtk/glcanvas.cpp
77ffb593 3// Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK
8b089c5e
JS
4// Author: Robert Roebling
5// Modified by:
6// Created: 17/08/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
65571936 9// Licence: wxWindows licence
8b089c5e
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
8b089c5e
JS
15#if wxUSE_GLCANVAS
16
17#include "wx/glcanvas.h"
18
670f9935
WS
19#ifndef WX_PRECOMP
20 #include "wx/app.h"
76b49cf4 21 #include "wx/frame.h"
7cf41a5d 22 #include "wx/colour.h"
02761f6c 23 #include "wx/module.h"
670f9935
WS
24#endif // WX_PRECOMP
25
a1abca32
PC
26#include <gtk/gtk.h>
27#include <gdk/gdkx.h>
8b089c5e 28
dc3065a5
VZ
29#if WXWIN_COMPATIBILITY_2_8
30
8b089c5e 31//-----------------------------------------------------------------------------
dc3065a5 32// "realize" from m_wxwindow: used to create m_glContext implicitly
8b089c5e
JS
33//-----------------------------------------------------------------------------
34
865bb325 35extern "C" {
8b089c5e 36static gint
34a34b02 37gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
8b089c5e 38{
dc3065a5 39 win->GTKInitImplicitContext();
8b089c5e
JS
40
41 return FALSE;
42}
865bb325 43}
8b089c5e 44
dc3065a5
VZ
45#endif // WXWIN_COMPATIBILITY_2_8
46
8b089c5e
JS
47//-----------------------------------------------------------------------------
48// "map" from m_wxwindow
49//-----------------------------------------------------------------------------
50
865bb325 51extern "C" {
8b089c5e
JS
52static gint
53gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
54{
dc3065a5
VZ
55 wxPaintEvent event( win->GetId() );
56 event.SetEventObject( win );
937013e0 57 win->HandleWindowEvent( event );
8b089c5e 58
dc3065a5
VZ
59 win->m_exposed = false;
60 win->GetUpdateRegion().Clear();
8b089c5e
JS
61
62 return FALSE;
63}
865bb325 64}
8b089c5e
JS
65
66//-----------------------------------------------------------------------------
67// "expose_event" of m_wxwindow
68//-----------------------------------------------------------------------------
69
865bb325 70extern "C" {
6d727f6c 71static gboolean
8b089c5e
JS
72gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
73{
670f9935 74 win->m_exposed = true;
8b089c5e
JS
75
76 win->GetUpdateRegion().Union( gdk_event->area.x,
77 gdk_event->area.y,
78 gdk_event->area.width,
79 gdk_event->area.height );
6d727f6c 80 return false;
8b089c5e 81}
865bb325 82}
8b089c5e 83
8b089c5e
JS
84//-----------------------------------------------------------------------------
85// "size_allocate" of m_wxwindow
86//-----------------------------------------------------------------------------
87
865bb325 88extern "C" {
2b5f62a0 89static void
e0d1fd7f
VZ
90gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget),
91 GtkAllocation * WXUNUSED(alloc),
92 wxGLCanvas *win)
8b089c5e 93{
8b089c5e
JS
94 if (!win->m_hasVMT)
95 return;
96
97 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
98 event.SetEventObject( win );
937013e0 99 win->HandleWindowEvent( event );
8b089c5e 100}
865bb325 101}
8b089c5e
JS
102
103//---------------------------------------------------------------------------
104// wxGlCanvas
105//---------------------------------------------------------------------------
106
4660d7e5 107IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
8b089c5e 108
dc3065a5
VZ
109wxGLCanvas::wxGLCanvas(wxWindow *parent,
110 wxWindowID id,
111 const int *attribList,
112 const wxPoint& pos,
113 const wxSize& size,
114 long style,
115 const wxString& name,
116 const wxPalette& palette)
117#if WXWIN_COMPATIBILITY_2_8
b7ea712c 118 : m_createImplicitContext(false)
dc3065a5 119#endif
b7ea712c 120{
dc3065a5 121 Create(parent, id, pos, size, style, name, attribList, palette);
b7ea712c
RR
122}
123
dc3065a5
VZ
124#if WXWIN_COMPATIBILITY_2_8
125
126wxGLCanvas::wxGLCanvas(wxWindow *parent,
127 wxWindowID id,
128 const wxPoint& pos,
129 const wxSize& size,
130 long style,
131 const wxString& name,
132 const int *attribList,
133 const wxPalette& palette)
b7ea712c 134 : m_createImplicitContext(true)
8b089c5e 135{
dc3065a5 136 Create(parent, id, pos, size, style, name, attribList, palette);
8b089c5e
JS
137}
138
dc3065a5
VZ
139wxGLCanvas::wxGLCanvas(wxWindow *parent,
140 const wxGLContext *shared,
141 wxWindowID id,
142 const wxPoint& pos,
143 const wxSize& size,
144 long style,
145 const wxString& name,
146 const int *attribList,
147 const wxPalette& palette)
b7ea712c 148 : m_createImplicitContext(true)
2b5f62a0 149{
dc3065a5
VZ
150 m_sharedContext = wx_const_cast(wxGLContext *, shared);
151
152 Create(parent, id, pos, size, style, name, attribList, palette);
8b089c5e
JS
153}
154
dc3065a5
VZ
155wxGLCanvas::wxGLCanvas(wxWindow *parent,
156 const wxGLCanvas *shared,
157 wxWindowID id,
158 const wxPoint& pos, const wxSize& size,
159 long style, const wxString& name,
160 const int *attribList,
161 const wxPalette& palette )
b7ea712c 162 : m_createImplicitContext(true)
2b5f62a0 163{
dc3065a5
VZ
164 m_sharedContextOf = wx_const_cast(wxGLCanvas *, shared);
165
166 Create(parent, id, pos, size, style, name, attribList, palette);
8b089c5e
JS
167}
168
dc3065a5 169#endif // WXWIN_COMPATIBILITY_2_8
2b5f62a0 170
dc3065a5
VZ
171bool wxGLCanvas::Create(wxWindow *parent,
172 wxWindowID id,
173 const wxPoint& pos,
174 const wxSize& size,
175 long style,
176 const wxString& name,
177 const int *attribList,
e0d1fd7f 178 const wxPalette& WXUNUSED_UNLESS_DEBUG(palette))
dc3065a5 179{
e0d1fd7f
VZ
180 wxASSERT_MSG( !palette.IsOk(), _T("palettes not supported") );
181
670f9935
WS
182 m_exposed = false;
183 m_noExpose = true;
184 m_nativeSizeEvent = true;
34a34b02 185
498ace9e
VZ
186 if ( !InitVisual(attribList) )
187 return false;
2b5f62a0 188
498ace9e 189 XVisualInfo * const xvi = GetXVisualInfo();
b4d06fb7 190
fee7a683
MR
191 GdkVisual *visual;
192 GdkColormap *colormap;
2b5f62a0 193
ff654490 194 wxWindow::Create( parent, id, pos, size, style, name );
fee7a683 195
ff654490 196 m_glWidget = m_wxwindow;
fee7a683 197
ff654490
VZ
198 GdkScreen *screen = gtk_widget_get_screen( m_glWidget );
199 colormap = gdk_screen_get_default_colormap(screen);
200 visual = gdk_colormap_get_visual(colormap);
fee7a683 201
ff654490 202 if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid)
fee7a683 203 {
ff654490
VZ
204 visual = gdk_x11_screen_lookup_visual( screen, xvi->visualid );
205 colormap = gdk_colormap_new(visual, FALSE);
fee7a683 206 }
2b5f62a0 207
ff654490
VZ
208 gtk_widget_set_colormap( m_glWidget, colormap );
209
2b5b9325 210 gtk_widget_set_double_buffered( m_glWidget, FALSE );
2b5b9325 211
dc3065a5 212#if WXWIN_COMPATIBILITY_2_8
b7ea712c 213 g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this);
dc3065a5 214#endif // WXWIN_COMPATIBILITY_2_8
b7ea712c
RR
215 g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this);
216 g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this);
217 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this);
a6f5aa49 218
dc3065a5 219#if WXWIN_COMPATIBILITY_2_8
bc869971
VZ
220 // if our parent window is already visible, we had been realized before we
221 // connected to the "realize" signal and hence our m_glContext hasn't been
222 // initialized yet and we have to do it now
223 if (GTK_WIDGET_REALIZED(m_wxwindow))
224 gtk_glwindow_realized_callback( m_wxwindow, this );
dc3065a5 225#endif // WXWIN_COMPATIBILITY_2_8
bc869971
VZ
226
227 if (GTK_WIDGET_MAPPED(m_wxwindow))
228 gtk_glwindow_map_callback( m_wxwindow, this );
229
670f9935 230 return true;
a6f5aa49
VZ
231}
232
498ace9e 233Window wxGLCanvas::GetXWindow() const
8b089c5e 234{
08f53168 235 GdkWindow *window = m_wxwindow->window;
498ace9e 236 return window ? GDK_WINDOW_XWINDOW(window) : 0;
8b089c5e
JS
237}
238
8b089c5e
JS
239void wxGLCanvas::OnInternalIdle()
240{
dc3065a5 241 if (m_exposed)
8b089c5e
JS
242 {
243 wxPaintEvent event( GetId() );
244 event.SetEventObject( this );
937013e0 245 HandleWindowEvent( event );
8b089c5e 246
670f9935 247 m_exposed = false;
8b089c5e
JS
248 GetUpdateRegion().Clear();
249 }
2b5f62a0 250
8b089c5e
JS
251 wxWindow::OnInternalIdle();
252}
253
dc3065a5
VZ
254#if WXWIN_COMPATIBILITY_2_8
255
256void wxGLCanvas::GTKInitImplicitContext()
257{
258 if ( !m_glContext && m_createImplicitContext )
259 {
260 wxGLContext *share = m_sharedContext;
261 if ( !share && m_sharedContextOf )
262 share = m_sharedContextOf->m_glContext;
263
264 m_glContext = new wxGLContext(this, share);
265 }
266}
a6f5aa49 267
dc3065a5 268#endif // WXWIN_COMPATIBILITY_2_8
a6f5aa49 269
dc3065a5 270#endif // wxUSE_GLCANVAS