]>
Commit | Line | Data |
---|---|---|
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 JS |
28 | |
29 | #include "wx/gtk/win_gtk.h" | |
30 | ||
dc3065a5 VZ |
31 | #if WXWIN_COMPATIBILITY_2_8 |
32 | ||
8b089c5e | 33 | //----------------------------------------------------------------------------- |
dc3065a5 | 34 | // "realize" from m_wxwindow: used to create m_glContext implicitly |
8b089c5e JS |
35 | //----------------------------------------------------------------------------- |
36 | ||
865bb325 | 37 | extern "C" { |
8b089c5e | 38 | static gint |
34a34b02 | 39 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) |
8b089c5e | 40 | { |
dc3065a5 | 41 | win->GTKInitImplicitContext(); |
8b089c5e JS |
42 | |
43 | return FALSE; | |
44 | } | |
865bb325 | 45 | } |
8b089c5e | 46 | |
dc3065a5 VZ |
47 | #endif // WXWIN_COMPATIBILITY_2_8 |
48 | ||
8b089c5e JS |
49 | //----------------------------------------------------------------------------- |
50 | // "map" from m_wxwindow | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
865bb325 | 53 | extern "C" { |
8b089c5e JS |
54 | static gint |
55 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
56 | { | |
dc3065a5 VZ |
57 | wxPaintEvent event( win->GetId() ); |
58 | event.SetEventObject( win ); | |
59 | win->GetEventHandler()->ProcessEvent( event ); | |
8b089c5e | 60 | |
dc3065a5 VZ |
61 | win->m_exposed = false; |
62 | win->GetUpdateRegion().Clear(); | |
8b089c5e JS |
63 | |
64 | return FALSE; | |
65 | } | |
865bb325 | 66 | } |
8b089c5e JS |
67 | |
68 | //----------------------------------------------------------------------------- | |
69 | // "expose_event" of m_wxwindow | |
70 | //----------------------------------------------------------------------------- | |
71 | ||
865bb325 | 72 | extern "C" { |
6d727f6c | 73 | static gboolean |
8b089c5e JS |
74 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) |
75 | { | |
670f9935 | 76 | win->m_exposed = true; |
8b089c5e JS |
77 | |
78 | win->GetUpdateRegion().Union( gdk_event->area.x, | |
79 | gdk_event->area.y, | |
80 | gdk_event->area.width, | |
81 | gdk_event->area.height ); | |
6d727f6c | 82 | return false; |
8b089c5e | 83 | } |
865bb325 | 84 | } |
8b089c5e | 85 | |
8b089c5e JS |
86 | //----------------------------------------------------------------------------- |
87 | // "size_allocate" of m_wxwindow | |
88 | //----------------------------------------------------------------------------- | |
89 | ||
865bb325 | 90 | extern "C" { |
2b5f62a0 | 91 | static void |
e0d1fd7f VZ |
92 | gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget), |
93 | GtkAllocation * WXUNUSED(alloc), | |
94 | wxGLCanvas *win) | |
8b089c5e | 95 | { |
8b089c5e JS |
96 | if (!win->m_hasVMT) |
97 | return; | |
98 | ||
99 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
100 | event.SetEventObject( win ); | |
101 | win->GetEventHandler()->ProcessEvent( event ); | |
102 | } | |
865bb325 | 103 | } |
8b089c5e JS |
104 | |
105 | //--------------------------------------------------------------------------- | |
106 | // wxGlCanvas | |
107 | //--------------------------------------------------------------------------- | |
108 | ||
4660d7e5 | 109 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
8b089c5e | 110 | |
dc3065a5 VZ |
111 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
112 | wxWindowID id, | |
113 | const int *attribList, | |
114 | const wxPoint& pos, | |
115 | const wxSize& size, | |
116 | long style, | |
117 | const wxString& name, | |
118 | const wxPalette& palette) | |
119 | #if WXWIN_COMPATIBILITY_2_8 | |
b7ea712c | 120 | : m_createImplicitContext(false) |
dc3065a5 | 121 | #endif |
b7ea712c | 122 | { |
dc3065a5 | 123 | Create(parent, id, pos, size, style, name, attribList, palette); |
b7ea712c RR |
124 | } |
125 | ||
dc3065a5 VZ |
126 | #if WXWIN_COMPATIBILITY_2_8 |
127 | ||
128 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
129 | wxWindowID id, | |
130 | const wxPoint& pos, | |
131 | const wxSize& size, | |
132 | long style, | |
133 | const wxString& name, | |
134 | const int *attribList, | |
135 | const wxPalette& palette) | |
b7ea712c | 136 | : m_createImplicitContext(true) |
8b089c5e | 137 | { |
dc3065a5 | 138 | Create(parent, id, pos, size, style, name, attribList, palette); |
8b089c5e JS |
139 | } |
140 | ||
dc3065a5 VZ |
141 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
142 | const wxGLContext *shared, | |
143 | wxWindowID id, | |
144 | const wxPoint& pos, | |
145 | const wxSize& size, | |
146 | long style, | |
147 | const wxString& name, | |
148 | const int *attribList, | |
149 | const wxPalette& palette) | |
b7ea712c | 150 | : m_createImplicitContext(true) |
2b5f62a0 | 151 | { |
dc3065a5 VZ |
152 | m_sharedContext = wx_const_cast(wxGLContext *, shared); |
153 | ||
154 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
155 | } |
156 | ||
dc3065a5 VZ |
157 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
158 | const wxGLCanvas *shared, | |
159 | wxWindowID id, | |
160 | const wxPoint& pos, const wxSize& size, | |
161 | long style, const wxString& name, | |
162 | const int *attribList, | |
163 | const wxPalette& palette ) | |
b7ea712c | 164 | : m_createImplicitContext(true) |
2b5f62a0 | 165 | { |
dc3065a5 VZ |
166 | m_sharedContextOf = wx_const_cast(wxGLCanvas *, shared); |
167 | ||
168 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
169 | } |
170 | ||
dc3065a5 | 171 | #endif // WXWIN_COMPATIBILITY_2_8 |
2b5f62a0 | 172 | |
dc3065a5 VZ |
173 | bool wxGLCanvas::Create(wxWindow *parent, |
174 | wxWindowID id, | |
175 | const wxPoint& pos, | |
176 | const wxSize& size, | |
177 | long style, | |
178 | const wxString& name, | |
179 | const int *attribList, | |
e0d1fd7f | 180 | const wxPalette& WXUNUSED_UNLESS_DEBUG(palette)) |
dc3065a5 | 181 | { |
e0d1fd7f VZ |
182 | wxASSERT_MSG( !palette.IsOk(), _T("palettes not supported") ); |
183 | ||
670f9935 WS |
184 | m_exposed = false; |
185 | m_noExpose = true; | |
186 | m_nativeSizeEvent = true; | |
34a34b02 | 187 | |
498ace9e VZ |
188 | if ( !InitVisual(attribList) ) |
189 | return false; | |
2b5f62a0 | 190 | |
498ace9e | 191 | XVisualInfo * const xvi = GetXVisualInfo(); |
b4d06fb7 | 192 | |
fee7a683 MR |
193 | GdkVisual *visual; |
194 | GdkColormap *colormap; | |
2b5f62a0 | 195 | |
fee7a683 | 196 | // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME) |
b3f4c570 | 197 | #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0) |
fee7a683 MR |
198 | if (!gtk_check_version(2,2,0)) |
199 | { | |
200 | wxWindow::Create( parent, id, pos, size, style, name ); | |
201 | ||
202 | m_glWidget = m_wxwindow; | |
203 | ||
204 | GdkScreen *screen = gtk_widget_get_screen( m_glWidget ); | |
205 | colormap = gdk_screen_get_default_colormap(screen); | |
206 | visual = gdk_colormap_get_visual(colormap); | |
207 | ||
498ace9e | 208 | if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid) |
fee7a683 | 209 | { |
498ace9e | 210 | visual = gdk_x11_screen_lookup_visual( screen, xvi->visualid ); |
fee7a683 MR |
211 | colormap = gdk_colormap_new(visual, FALSE); |
212 | } | |
213 | ||
214 | gtk_widget_set_colormap( m_glWidget, colormap ); | |
215 | } | |
216 | else | |
dc3065a5 | 217 | #endif // GTK+ >= 2.2 |
fee7a683 | 218 | { |
498ace9e | 219 | visual = gdkx_visual_get( xvi->visualid ); |
fee7a683 | 220 | colormap = gdk_colormap_new( visual, TRUE ); |
a6f5aa49 | 221 | |
fee7a683 | 222 | gtk_widget_push_colormap( colormap ); |
a6f5aa49 | 223 | |
fee7a683 MR |
224 | wxWindow::Create( parent, id, pos, size, style, name ); |
225 | m_glWidget = m_wxwindow; | |
226 | } | |
2b5f62a0 | 227 | |
2b5b9325 | 228 | gtk_widget_set_double_buffered( m_glWidget, FALSE ); |
2b5b9325 | 229 | |
dc3065a5 | 230 | #if WXWIN_COMPATIBILITY_2_8 |
b7ea712c | 231 | g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this); |
dc3065a5 | 232 | #endif // WXWIN_COMPATIBILITY_2_8 |
b7ea712c RR |
233 | g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this); |
234 | g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this); | |
235 | g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this); | |
a6f5aa49 | 236 | |
fee7a683 MR |
237 | if (gtk_check_version(2,2,0) != NULL) |
238 | { | |
fee7a683 MR |
239 | gtk_widget_pop_colormap(); |
240 | } | |
2b5f62a0 | 241 | |
dc3065a5 | 242 | #if WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
243 | // if our parent window is already visible, we had been realized before we |
244 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
245 | // initialized yet and we have to do it now | |
246 | if (GTK_WIDGET_REALIZED(m_wxwindow)) | |
247 | gtk_glwindow_realized_callback( m_wxwindow, this ); | |
dc3065a5 | 248 | #endif // WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
249 | |
250 | if (GTK_WIDGET_MAPPED(m_wxwindow)) | |
251 | gtk_glwindow_map_callback( m_wxwindow, this ); | |
252 | ||
670f9935 | 253 | return true; |
a6f5aa49 VZ |
254 | } |
255 | ||
498ace9e | 256 | Window wxGLCanvas::GetXWindow() const |
8b089c5e | 257 | { |
b7ea712c | 258 | GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window; |
498ace9e | 259 | return window ? GDK_WINDOW_XWINDOW(window) : 0; |
8b089c5e JS |
260 | } |
261 | ||
8b089c5e JS |
262 | void wxGLCanvas::OnInternalIdle() |
263 | { | |
dc3065a5 | 264 | if (m_exposed) |
8b089c5e JS |
265 | { |
266 | wxPaintEvent event( GetId() ); | |
267 | event.SetEventObject( this ); | |
268 | GetEventHandler()->ProcessEvent( event ); | |
269 | ||
670f9935 | 270 | m_exposed = false; |
8b089c5e JS |
271 | GetUpdateRegion().Clear(); |
272 | } | |
2b5f62a0 | 273 | |
8b089c5e JS |
274 | wxWindow::OnInternalIdle(); |
275 | } | |
276 | ||
dc3065a5 VZ |
277 | #if WXWIN_COMPATIBILITY_2_8 |
278 | ||
279 | void wxGLCanvas::GTKInitImplicitContext() | |
280 | { | |
281 | if ( !m_glContext && m_createImplicitContext ) | |
282 | { | |
283 | wxGLContext *share = m_sharedContext; | |
284 | if ( !share && m_sharedContextOf ) | |
285 | share = m_sharedContextOf->m_glContext; | |
286 | ||
287 | m_glContext = new wxGLContext(this, share); | |
288 | } | |
289 | } | |
a6f5aa49 | 290 | |
dc3065a5 | 291 | #endif // WXWIN_COMPATIBILITY_2_8 |
a6f5aa49 | 292 | |
dc3065a5 | 293 | #endif // wxUSE_GLCANVAS |