]>
Commit | Line | Data |
---|---|---|
8b089c5e | 1 | ///////////////////////////////////////////////////////////////////////////// |
9b5f1895 | 2 | // Name: src/gtk1/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 | |
8b089c5e | 7 | // Copyright: (c) Robert Roebling |
65571936 | 8 | // Licence: wxWindows licence |
8b089c5e JS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
14f355c2 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
8b089c5e JS |
14 | #if wxUSE_GLCANVAS |
15 | ||
16 | #include "wx/glcanvas.h" | |
17 | ||
670f9935 WS |
18 | #ifndef WX_PRECOMP |
19 | #include "wx/app.h" | |
76b49cf4 | 20 | #include "wx/frame.h" |
7cf41a5d | 21 | #include "wx/colour.h" |
02761f6c | 22 | #include "wx/module.h" |
670f9935 WS |
23 | #endif // WX_PRECOMP |
24 | ||
2b5f62a0 VZ |
25 | extern "C" |
26 | { | |
8b089c5e JS |
27 | #include "gtk/gtk.h" |
28 | #include "gdk/gdk.h" | |
29 | #include "gdk/gdkx.h" | |
30 | } | |
31 | ||
3cbab641 | 32 | #include "wx/gtk1/win_gtk.h" |
dc3065a5 | 33 | #include "wx/gtk1/private.h" |
34a34b02 | 34 | |
8b089c5e JS |
35 | //----------------------------------------------------------------------------- |
36 | // idle system | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | extern void wxapp_install_idle_handler(); | |
40 | extern bool g_isIdle; | |
41 | ||
dc3065a5 | 42 | #if WXWIN_COMPATIBILITY_2_8 |
8b089c5e JS |
43 | |
44 | //----------------------------------------------------------------------------- | |
dc3065a5 | 45 | // "realize" from m_wxwindow: used to create m_glContext implicitly |
8b089c5e JS |
46 | //----------------------------------------------------------------------------- |
47 | ||
865bb325 | 48 | extern "C" { |
8b089c5e | 49 | static gint |
34a34b02 | 50 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) |
8b089c5e | 51 | { |
dc3065a5 | 52 | win->GTKInitImplicitContext(); |
8b089c5e JS |
53 | |
54 | return FALSE; | |
55 | } | |
865bb325 | 56 | } |
8b089c5e | 57 | |
dc3065a5 VZ |
58 | #endif // WXWIN_COMPATIBILITY_2_8 |
59 | ||
8b089c5e JS |
60 | //----------------------------------------------------------------------------- |
61 | // "map" from m_wxwindow | |
62 | //----------------------------------------------------------------------------- | |
63 | ||
865bb325 | 64 | extern "C" { |
8b089c5e JS |
65 | static gint |
66 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
67 | { | |
dc3065a5 VZ |
68 | wxPaintEvent event( win->GetId() ); |
69 | event.SetEventObject( win ); | |
937013e0 | 70 | win->HandleWindowEvent( event ); |
8b089c5e | 71 | |
dc3065a5 | 72 | win->GetUpdateRegion().Clear(); |
8b089c5e JS |
73 | |
74 | return FALSE; | |
75 | } | |
865bb325 | 76 | } |
8b089c5e JS |
77 | |
78 | //----------------------------------------------------------------------------- | |
79 | // "expose_event" of m_wxwindow | |
80 | //----------------------------------------------------------------------------- | |
81 | ||
865bb325 | 82 | extern "C" { |
2b5f62a0 | 83 | static void |
8b089c5e JS |
84 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) |
85 | { | |
2b5f62a0 | 86 | if (g_isIdle) |
8b089c5e JS |
87 | wxapp_install_idle_handler(); |
88 | ||
8b089c5e JS |
89 | win->GetUpdateRegion().Union( gdk_event->area.x, |
90 | gdk_event->area.y, | |
91 | gdk_event->area.width, | |
92 | gdk_event->area.height ); | |
93 | } | |
865bb325 | 94 | } |
8b089c5e JS |
95 | |
96 | //----------------------------------------------------------------------------- | |
97 | // "draw" of m_wxwindow | |
98 | //----------------------------------------------------------------------------- | |
99 | ||
865bb325 | 100 | extern "C" { |
2b5f62a0 | 101 | static void |
8b089c5e JS |
102 | gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win ) |
103 | { | |
2b5f62a0 | 104 | if (g_isIdle) |
8b089c5e JS |
105 | wxapp_install_idle_handler(); |
106 | ||
8b089c5e JS |
107 | win->GetUpdateRegion().Union( rect->x, rect->y, |
108 | rect->width, rect->height ); | |
109 | } | |
865bb325 | 110 | } |
8b089c5e JS |
111 | |
112 | //----------------------------------------------------------------------------- | |
113 | // "size_allocate" of m_wxwindow | |
114 | //----------------------------------------------------------------------------- | |
115 | ||
865bb325 | 116 | extern "C" { |
2b5f62a0 | 117 | static void |
8b089c5e JS |
118 | gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win ) |
119 | { | |
120 | if (g_isIdle) | |
121 | wxapp_install_idle_handler(); | |
122 | ||
123 | if (!win->m_hasVMT) | |
124 | return; | |
125 | ||
126 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
127 | event.SetEventObject( win ); | |
937013e0 | 128 | win->HandleWindowEvent( event ); |
8b089c5e | 129 | } |
865bb325 | 130 | } |
8b089c5e JS |
131 | |
132 | //--------------------------------------------------------------------------- | |
133 | // wxGlCanvas | |
134 | //--------------------------------------------------------------------------- | |
135 | ||
4660d7e5 | 136 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
8b089c5e | 137 | |
dc3065a5 VZ |
138 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
139 | wxWindowID id, | |
140 | const int *attribList, | |
141 | const wxPoint& pos, | |
142 | const wxSize& size, | |
143 | long style, | |
144 | const wxString& name, | |
145 | const wxPalette& palette) | |
146 | #if WXWIN_COMPATIBILITY_2_8 | |
147 | : m_createImplicitContext(false) | |
148 | #endif | |
8b089c5e | 149 | { |
dc3065a5 | 150 | Create(parent, id, pos, size, style, name, attribList, palette); |
8b089c5e JS |
151 | } |
152 | ||
dc3065a5 VZ |
153 | #if WXWIN_COMPATIBILITY_2_8 |
154 | ||
155 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
156 | wxWindowID id, | |
157 | const wxPoint& pos, | |
158 | const wxSize& size, | |
159 | long style, | |
160 | const wxString& name, | |
161 | const int *attribList, | |
162 | const wxPalette& palette) | |
163 | : m_createImplicitContext(true) | |
2b5f62a0 | 164 | { |
dc3065a5 | 165 | Create(parent, id, pos, size, style, name, attribList, palette); |
8b089c5e JS |
166 | } |
167 | ||
dc3065a5 VZ |
168 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
169 | const wxGLContext *shared, | |
170 | wxWindowID id, | |
171 | const wxPoint& pos, | |
172 | const wxSize& size, | |
173 | long style, | |
174 | const wxString& name, | |
175 | const int *attribList, | |
176 | const wxPalette& palette) | |
177 | : m_createImplicitContext(true) | |
2b5f62a0 | 178 | { |
5c33522f | 179 | m_sharedContext = const_cast<wxGLContext *>(shared); |
dc3065a5 VZ |
180 | |
181 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
182 | } |
183 | ||
dc3065a5 VZ |
184 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
185 | const wxGLCanvas *shared, | |
186 | wxWindowID id, | |
187 | const wxPoint& pos, const wxSize& size, | |
188 | long style, const wxString& name, | |
189 | const int *attribList, | |
190 | const wxPalette& palette ) | |
191 | : m_createImplicitContext(true) | |
8b089c5e | 192 | { |
5c33522f | 193 | m_sharedContextOf = const_cast<wxGLCanvas *>(shared); |
dc3065a5 VZ |
194 | |
195 | Create(parent, id, pos, size, style, name, attribList, palette); | |
196 | } | |
2b5f62a0 | 197 | |
dc3065a5 VZ |
198 | #endif // WXWIN_COMPATIBILITY_2_8 |
199 | ||
200 | bool wxGLCanvas::Create(wxWindow *parent, | |
201 | wxWindowID id, | |
202 | const wxPoint& pos, | |
203 | const wxSize& size, | |
204 | long style, | |
205 | const wxString& name, | |
206 | const int *attribList, | |
207 | const wxPalette& palette) | |
208 | { | |
670f9935 WS |
209 | m_noExpose = true; |
210 | m_nativeSizeEvent = true; | |
b4d06fb7 | 211 | |
498ace9e VZ |
212 | if ( !InitVisual(attribList) ) |
213 | return false; | |
2b5f62a0 | 214 | |
498ace9e VZ |
215 | GdkVisual *visual = gdkx_visual_get( GetXVisualInfo()->visualid ); |
216 | GdkColormap *colormap = gdk_colormap_new( visual, TRUE ); | |
fee7a683 | 217 | |
dc3065a5 VZ |
218 | gtk_widget_push_colormap( colormap ); |
219 | gtk_widget_push_visual( visual ); | |
fee7a683 | 220 | |
dc3065a5 VZ |
221 | wxWindow::Create( parent, id, pos, size, style, name ); |
222 | m_glWidget = m_wxwindow; | |
2b5f62a0 | 223 | |
a6f5aa49 | 224 | gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE ); |
2b5f62a0 | 225 | |
dc3065a5 | 226 | #if WXWIN_COMPATIBILITY_2_8 |
a6f5aa49 | 227 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize", |
dc3065a5 VZ |
228 | GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this); |
229 | #endif // WXWIN_COMPATIBILITY_2_8 | |
a6f5aa49 VZ |
230 | |
231 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map", | |
dc3065a5 | 232 | GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this); |
a6f5aa49 VZ |
233 | |
234 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event", | |
dc3065a5 | 235 | GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer) this); |
a6f5aa49 VZ |
236 | |
237 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw", | |
dc3065a5 | 238 | GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer) this); |
2b5f62a0 | 239 | |
a6f5aa49 | 240 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
dc3065a5 | 241 | GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer) this); |
a6f5aa49 | 242 | |
3cbab641 | 243 | gtk_widget_pop_visual(); |
dc3065a5 | 244 | |
3cbab641 | 245 | gtk_widget_pop_colormap(); |
2b5f62a0 | 246 | |
dc3065a5 | 247 | #if WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
248 | // if our parent window is already visible, we had been realized before we |
249 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
250 | // initialized yet and we have to do it now | |
251 | if (GTK_WIDGET_REALIZED(m_wxwindow)) | |
252 | gtk_glwindow_realized_callback( m_wxwindow, this ); | |
dc3065a5 | 253 | #endif // WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
254 | |
255 | if (GTK_WIDGET_MAPPED(m_wxwindow)) | |
256 | gtk_glwindow_map_callback( m_wxwindow, this ); | |
257 | ||
670f9935 | 258 | return true; |
a6f5aa49 VZ |
259 | } |
260 | ||
498ace9e | 261 | Window wxGLCanvas::GetXWindow() const |
8b089c5e | 262 | { |
dc3065a5 | 263 | GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window; |
498ace9e | 264 | return window ? GDK_WINDOW_XWINDOW(window) : 0; |
8b089c5e JS |
265 | } |
266 | ||
267 | void wxGLCanvas::OnInternalIdle() | |
268 | { | |
dc3065a5 | 269 | if (!m_updateRegion.IsEmpty()) |
8b089c5e JS |
270 | { |
271 | wxPaintEvent event( GetId() ); | |
272 | event.SetEventObject( this ); | |
937013e0 | 273 | HandleWindowEvent( event ); |
8b089c5e | 274 | |
8b089c5e JS |
275 | GetUpdateRegion().Clear(); |
276 | } | |
2b5f62a0 | 277 | |
8b089c5e JS |
278 | wxWindow::OnInternalIdle(); |
279 | } | |
280 | ||
dc3065a5 | 281 | #if WXWIN_COMPATIBILITY_2_8 |
a6f5aa49 | 282 | |
dc3065a5 VZ |
283 | void wxGLCanvas::GTKInitImplicitContext() |
284 | { | |
285 | if ( !m_glContext && m_createImplicitContext ) | |
286 | { | |
287 | wxGLContext *share = m_sharedContext; | |
288 | if ( !share && m_sharedContextOf ) | |
289 | share = m_sharedContextOf->m_glContext; | |
290 | ||
291 | m_glContext = new wxGLContext(this, share); | |
292 | } | |
293 | } | |
294 | ||
295 | #endif // WXWIN_COMPATIBILITY_2_8 | |
a6f5aa49 | 296 | |
dc3065a5 | 297 | #endif // wxUSE_GLCANVAS |