]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/glcanvas.cpp | |
3 | // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 17/08/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_GLCANVAS | |
16 | ||
17 | #include "wx/glcanvas.h" | |
18 | ||
19 | #include <gtk/gtk.h> | |
20 | #include <gdk/gdkx.h> | |
21 | #include "wx/gtk/private/gtk2-compat.h" | |
22 | ||
23 | #if WXWIN_COMPATIBILITY_2_8 | |
24 | ||
25 | //----------------------------------------------------------------------------- | |
26 | // "realize" from m_wxwindow: used to create m_glContext implicitly | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | extern "C" { | |
30 | static void | |
31 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) | |
32 | { | |
33 | win->GTKInitImplicitContext(); | |
34 | } | |
35 | } | |
36 | ||
37 | #endif // WXWIN_COMPATIBILITY_2_8 | |
38 | ||
39 | //----------------------------------------------------------------------------- | |
40 | // "map" from m_wxwindow | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | extern "C" { | |
44 | static void | |
45 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
46 | { | |
47 | wxPaintEvent event( win->GetId() ); | |
48 | event.SetEventObject( win ); | |
49 | win->HandleWindowEvent( event ); | |
50 | ||
51 | win->m_exposed = false; | |
52 | win->GetUpdateRegion().Clear(); | |
53 | } | |
54 | } | |
55 | ||
56 | //----------------------------------------------------------------------------- | |
57 | // "expose_event" of m_wxwindow | |
58 | //----------------------------------------------------------------------------- | |
59 | ||
60 | extern "C" { | |
61 | static gboolean | |
62 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) | |
63 | { | |
64 | win->m_exposed = true; | |
65 | ||
66 | win->GetUpdateRegion().Union( gdk_event->area.x, | |
67 | gdk_event->area.y, | |
68 | gdk_event->area.width, | |
69 | gdk_event->area.height ); | |
70 | return false; | |
71 | } | |
72 | } | |
73 | ||
74 | //----------------------------------------------------------------------------- | |
75 | // "size_allocate" of m_wxwindow | |
76 | //----------------------------------------------------------------------------- | |
77 | ||
78 | extern "C" { | |
79 | static void | |
80 | gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget), | |
81 | GtkAllocation * WXUNUSED(alloc), | |
82 | wxGLCanvas *win) | |
83 | { | |
84 | if (!win->m_hasVMT) | |
85 | return; | |
86 | ||
87 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
88 | event.SetEventObject( win ); | |
89 | win->HandleWindowEvent( event ); | |
90 | } | |
91 | } | |
92 | ||
93 | //----------------------------------------------------------------------------- | |
94 | // emission hook for "parent-set" | |
95 | //----------------------------------------------------------------------------- | |
96 | ||
97 | extern "C" { | |
98 | static gboolean | |
99 | parent_set_hook(GSignalInvocationHint*, guint, const GValue* param_values, void* data) | |
100 | { | |
101 | wxGLCanvas* win = (wxGLCanvas*)data; | |
102 | if (g_value_peek_pointer(¶m_values[0]) == win->m_wxwindow) | |
103 | { | |
104 | const XVisualInfo* xvi = win->GetXVisualInfo(); | |
105 | GdkVisual* visual = gtk_widget_get_visual(win->m_wxwindow); | |
106 | if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid) | |
107 | { | |
108 | GdkScreen* screen = gtk_widget_get_screen(win->m_wxwindow); | |
109 | visual = gdk_x11_screen_lookup_visual(screen, xvi->visualid); | |
110 | GdkColormap* colormap = gdk_colormap_new(visual, false); | |
111 | gtk_widget_set_colormap(win->m_wxwindow, colormap); | |
112 | g_object_unref(colormap); | |
113 | } | |
114 | // remove hook | |
115 | return false; | |
116 | } | |
117 | return true; | |
118 | } | |
119 | } | |
120 | ||
121 | //--------------------------------------------------------------------------- | |
122 | // wxGlCanvas | |
123 | //--------------------------------------------------------------------------- | |
124 | ||
125 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) | |
126 | ||
127 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
128 | wxWindowID id, | |
129 | const int *attribList, | |
130 | const wxPoint& pos, | |
131 | const wxSize& size, | |
132 | long style, | |
133 | const wxString& name, | |
134 | const wxPalette& palette) | |
135 | #if WXWIN_COMPATIBILITY_2_8 | |
136 | : m_createImplicitContext(false) | |
137 | #endif | |
138 | { | |
139 | Create(parent, id, pos, size, style, name, attribList, palette); | |
140 | } | |
141 | ||
142 | #if WXWIN_COMPATIBILITY_2_8 | |
143 | ||
144 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
145 | wxWindowID id, | |
146 | const wxPoint& pos, | |
147 | const wxSize& size, | |
148 | long style, | |
149 | const wxString& name, | |
150 | const int *attribList, | |
151 | const wxPalette& palette) | |
152 | : m_createImplicitContext(true) | |
153 | { | |
154 | m_sharedContext = NULL; | |
155 | m_sharedContextOf = NULL; | |
156 | ||
157 | Create(parent, id, pos, size, style, name, attribList, palette); | |
158 | } | |
159 | ||
160 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
161 | const wxGLContext *shared, | |
162 | wxWindowID id, | |
163 | const wxPoint& pos, | |
164 | const wxSize& size, | |
165 | long style, | |
166 | const wxString& name, | |
167 | const int *attribList, | |
168 | const wxPalette& palette) | |
169 | : m_createImplicitContext(true) | |
170 | { | |
171 | m_sharedContext = const_cast<wxGLContext *>(shared); | |
172 | ||
173 | Create(parent, id, pos, size, style, name, attribList, palette); | |
174 | } | |
175 | ||
176 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
177 | const wxGLCanvas *shared, | |
178 | wxWindowID id, | |
179 | const wxPoint& pos, const wxSize& size, | |
180 | long style, const wxString& name, | |
181 | const int *attribList, | |
182 | const wxPalette& palette ) | |
183 | : m_createImplicitContext(true) | |
184 | { | |
185 | m_sharedContext = NULL; | |
186 | m_sharedContextOf = const_cast<wxGLCanvas *>(shared); | |
187 | ||
188 | Create(parent, id, pos, size, style, name, attribList, palette); | |
189 | } | |
190 | ||
191 | #endif // WXWIN_COMPATIBILITY_2_8 | |
192 | ||
193 | bool wxGLCanvas::Create(wxWindow *parent, | |
194 | wxWindowID id, | |
195 | const wxPoint& pos, | |
196 | const wxSize& size, | |
197 | long style, | |
198 | const wxString& name, | |
199 | const int *attribList, | |
200 | const wxPalette& palette) | |
201 | { | |
202 | #if wxUSE_PALETTE | |
203 | wxASSERT_MSG( !palette.IsOk(), wxT("palettes not supported") ); | |
204 | #endif // wxUSE_PALETTE | |
205 | wxUnusedVar(palette); // Unused when wxDEBUG_LEVEL==0 | |
206 | ||
207 | m_exposed = false; | |
208 | m_noExpose = true; | |
209 | m_nativeSizeEvent = true; | |
210 | ||
211 | if ( !InitVisual(attribList) ) | |
212 | return false; | |
213 | ||
214 | // watch for the "parent-set" signal on m_wxwindow so we can set colormap | |
215 | // before m_wxwindow is realized (which will occur before | |
216 | // wxWindow::Create() returns if parent is already visible) | |
217 | unsigned sig_id = g_signal_lookup("parent-set", GTK_TYPE_WIDGET); | |
218 | g_signal_add_emission_hook(sig_id, 0, parent_set_hook, this, NULL); | |
219 | ||
220 | wxWindow::Create( parent, id, pos, size, style, name ); | |
221 | ||
222 | gtk_widget_set_double_buffered(m_wxwindow, false); | |
223 | ||
224 | #if WXWIN_COMPATIBILITY_2_8 | |
225 | g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this); | |
226 | #endif // WXWIN_COMPATIBILITY_2_8 | |
227 | g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this); | |
228 | g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this); | |
229 | g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this); | |
230 | ||
231 | #if WXWIN_COMPATIBILITY_2_8 | |
232 | // if our parent window is already visible, we had been realized before we | |
233 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
234 | // initialized yet and we have to do it now | |
235 | if (gtk_widget_get_realized(m_wxwindow)) | |
236 | gtk_glwindow_realized_callback( m_wxwindow, this ); | |
237 | #endif // WXWIN_COMPATIBILITY_2_8 | |
238 | ||
239 | if (gtk_widget_get_mapped(m_wxwindow)) | |
240 | gtk_glwindow_map_callback( m_wxwindow, this ); | |
241 | ||
242 | return true; | |
243 | } | |
244 | ||
245 | Window wxGLCanvas::GetXWindow() const | |
246 | { | |
247 | GdkWindow* window = GTKGetDrawingWindow(); | |
248 | return window ? GDK_WINDOW_XWINDOW(window) : 0; | |
249 | } | |
250 | ||
251 | void wxGLCanvas::OnInternalIdle() | |
252 | { | |
253 | if (m_exposed) | |
254 | { | |
255 | wxPaintEvent event( GetId() ); | |
256 | event.SetEventObject( this ); | |
257 | HandleWindowEvent( event ); | |
258 | ||
259 | m_exposed = false; | |
260 | GetUpdateRegion().Clear(); | |
261 | } | |
262 | ||
263 | wxWindow::OnInternalIdle(); | |
264 | } | |
265 | ||
266 | #if WXWIN_COMPATIBILITY_2_8 | |
267 | ||
268 | void wxGLCanvas::GTKInitImplicitContext() | |
269 | { | |
270 | if ( !m_glContext && m_createImplicitContext ) | |
271 | { | |
272 | wxGLContext *share = m_sharedContext; | |
273 | if ( !share && m_sharedContextOf ) | |
274 | share = m_sharedContextOf->m_glContext; | |
275 | ||
276 | m_glContext = new wxGLContext(this, share); | |
277 | } | |
278 | } | |
279 | ||
280 | #endif // WXWIN_COMPATIBILITY_2_8 | |
281 | ||
282 | #endif // wxUSE_GLCANVAS |