]>
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 | 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 | 35 | extern "C" { |
144ae5bb | 36 | static void |
34a34b02 | 37 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) |
8b089c5e | 38 | { |
dc3065a5 | 39 | win->GTKInitImplicitContext(); |
8b089c5e | 40 | } |
865bb325 | 41 | } |
8b089c5e | 42 | |
dc3065a5 VZ |
43 | #endif // WXWIN_COMPATIBILITY_2_8 |
44 | ||
8b089c5e JS |
45 | //----------------------------------------------------------------------------- |
46 | // "map" from m_wxwindow | |
47 | //----------------------------------------------------------------------------- | |
48 | ||
865bb325 | 49 | extern "C" { |
144ae5bb | 50 | static void |
8b089c5e JS |
51 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) |
52 | { | |
dc3065a5 VZ |
53 | wxPaintEvent event( win->GetId() ); |
54 | event.SetEventObject( win ); | |
937013e0 | 55 | win->HandleWindowEvent( event ); |
8b089c5e | 56 | |
dc3065a5 VZ |
57 | win->m_exposed = false; |
58 | win->GetUpdateRegion().Clear(); | |
8b089c5e | 59 | } |
865bb325 | 60 | } |
8b089c5e JS |
61 | |
62 | //----------------------------------------------------------------------------- | |
63 | // "expose_event" of m_wxwindow | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
865bb325 | 66 | extern "C" { |
6d727f6c | 67 | static gboolean |
8b089c5e JS |
68 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) |
69 | { | |
670f9935 | 70 | win->m_exposed = true; |
8b089c5e JS |
71 | |
72 | win->GetUpdateRegion().Union( gdk_event->area.x, | |
73 | gdk_event->area.y, | |
74 | gdk_event->area.width, | |
75 | gdk_event->area.height ); | |
6d727f6c | 76 | return false; |
8b089c5e | 77 | } |
865bb325 | 78 | } |
8b089c5e | 79 | |
8b089c5e JS |
80 | //----------------------------------------------------------------------------- |
81 | // "size_allocate" of m_wxwindow | |
82 | //----------------------------------------------------------------------------- | |
83 | ||
865bb325 | 84 | extern "C" { |
2b5f62a0 | 85 | static void |
e0d1fd7f VZ |
86 | gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget), |
87 | GtkAllocation * WXUNUSED(alloc), | |
88 | wxGLCanvas *win) | |
8b089c5e | 89 | { |
8b089c5e JS |
90 | if (!win->m_hasVMT) |
91 | return; | |
92 | ||
93 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
94 | event.SetEventObject( win ); | |
937013e0 | 95 | win->HandleWindowEvent( event ); |
8b089c5e | 96 | } |
865bb325 | 97 | } |
8b089c5e | 98 | |
3780e252 PC |
99 | //----------------------------------------------------------------------------- |
100 | // emission hook for "parent-set" | |
101 | //----------------------------------------------------------------------------- | |
102 | ||
103 | extern "C" { | |
104 | static gboolean | |
105 | parent_set_hook(GSignalInvocationHint*, guint, const GValue* param_values, void* data) | |
106 | { | |
107 | wxGLCanvas* win = (wxGLCanvas*)data; | |
108 | if (g_value_peek_pointer(¶m_values[0]) == win->m_wxwindow) | |
109 | { | |
110 | const XVisualInfo* xvi = win->GetXVisualInfo(); | |
111 | GdkVisual* visual = gtk_widget_get_visual(win->m_wxwindow); | |
112 | if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid) | |
113 | { | |
114 | GdkScreen* screen = gtk_widget_get_screen(win->m_wxwindow); | |
115 | visual = gdk_x11_screen_lookup_visual(screen, xvi->visualid); | |
116 | GdkColormap* colormap = gdk_colormap_new(visual, false); | |
117 | gtk_widget_set_colormap(win->m_wxwindow, colormap); | |
118 | g_object_unref(colormap); | |
119 | } | |
120 | // remove hook | |
121 | return false; | |
122 | } | |
123 | return true; | |
124 | } | |
125 | } | |
126 | ||
8b089c5e JS |
127 | //--------------------------------------------------------------------------- |
128 | // wxGlCanvas | |
129 | //--------------------------------------------------------------------------- | |
130 | ||
4660d7e5 | 131 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
8b089c5e | 132 | |
dc3065a5 VZ |
133 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
134 | wxWindowID id, | |
135 | const int *attribList, | |
136 | const wxPoint& pos, | |
137 | const wxSize& size, | |
138 | long style, | |
139 | const wxString& name, | |
140 | const wxPalette& palette) | |
141 | #if WXWIN_COMPATIBILITY_2_8 | |
b7ea712c | 142 | : m_createImplicitContext(false) |
dc3065a5 | 143 | #endif |
b7ea712c | 144 | { |
dc3065a5 | 145 | Create(parent, id, pos, size, style, name, attribList, palette); |
b7ea712c RR |
146 | } |
147 | ||
dc3065a5 VZ |
148 | #if WXWIN_COMPATIBILITY_2_8 |
149 | ||
150 | wxGLCanvas::wxGLCanvas(wxWindow *parent, | |
151 | wxWindowID id, | |
152 | const wxPoint& pos, | |
153 | const wxSize& size, | |
154 | long style, | |
155 | const wxString& name, | |
156 | const int *attribList, | |
157 | const wxPalette& palette) | |
b7ea712c | 158 | : m_createImplicitContext(true) |
8b089c5e | 159 | { |
21562ad2 VZ |
160 | m_sharedContext = NULL; |
161 | m_sharedContextOf = NULL; | |
162 | ||
dc3065a5 | 163 | Create(parent, id, pos, size, style, name, attribList, palette); |
8b089c5e JS |
164 | } |
165 | ||
dc3065a5 VZ |
166 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
167 | const wxGLContext *shared, | |
168 | wxWindowID id, | |
169 | const wxPoint& pos, | |
170 | const wxSize& size, | |
171 | long style, | |
172 | const wxString& name, | |
173 | const int *attribList, | |
174 | const wxPalette& palette) | |
b7ea712c | 175 | : m_createImplicitContext(true) |
2b5f62a0 | 176 | { |
5c33522f | 177 | m_sharedContext = const_cast<wxGLContext *>(shared); |
dc3065a5 VZ |
178 | |
179 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
180 | } |
181 | ||
dc3065a5 VZ |
182 | wxGLCanvas::wxGLCanvas(wxWindow *parent, |
183 | const wxGLCanvas *shared, | |
184 | wxWindowID id, | |
185 | const wxPoint& pos, const wxSize& size, | |
186 | long style, const wxString& name, | |
187 | const int *attribList, | |
188 | const wxPalette& palette ) | |
b7ea712c | 189 | : m_createImplicitContext(true) |
2b5f62a0 | 190 | { |
21562ad2 | 191 | m_sharedContext = NULL; |
5c33522f | 192 | m_sharedContextOf = const_cast<wxGLCanvas *>(shared); |
dc3065a5 VZ |
193 | |
194 | Create(parent, id, pos, size, style, name, attribList, palette); | |
8b089c5e JS |
195 | } |
196 | ||
dc3065a5 | 197 | #endif // WXWIN_COMPATIBILITY_2_8 |
2b5f62a0 | 198 | |
dc3065a5 VZ |
199 | bool wxGLCanvas::Create(wxWindow *parent, |
200 | wxWindowID id, | |
201 | const wxPoint& pos, | |
202 | const wxSize& size, | |
203 | long style, | |
204 | const wxString& name, | |
205 | const int *attribList, | |
9ce5cf09 | 206 | const wxPalette& palette) |
dc3065a5 | 207 | { |
9ce5cf09 | 208 | #if wxUSE_PALETTE |
9a83f860 | 209 | wxASSERT_MSG( !palette.IsOk(), wxT("palettes not supported") ); |
9ce5cf09 VZ |
210 | #endif // wxUSE_PALETTE |
211 | wxUnusedVar(palette); // Unused when wxDEBUG_LEVEL==0 | |
e0d1fd7f | 212 | |
670f9935 WS |
213 | m_exposed = false; |
214 | m_noExpose = true; | |
215 | m_nativeSizeEvent = true; | |
34a34b02 | 216 | |
498ace9e VZ |
217 | if ( !InitVisual(attribList) ) |
218 | return false; | |
2b5f62a0 | 219 | |
3780e252 PC |
220 | // watch for the "parent-set" signal on m_wxwindow so we can set colormap |
221 | // before m_wxwindow is realized (which will occur before | |
222 | // wxWindow::Create() returns if parent is already visible) | |
223 | unsigned sig_id = g_signal_lookup("parent-set", GTK_TYPE_WIDGET); | |
224 | g_signal_add_emission_hook(sig_id, 0, parent_set_hook, this, NULL); | |
2b5f62a0 | 225 | |
ff654490 | 226 | wxWindow::Create( parent, id, pos, size, style, name ); |
fee7a683 | 227 | |
144ae5bb | 228 | gtk_widget_set_double_buffered(m_wxwindow, 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 | |
dc3065a5 | 237 | #if WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
238 | // if our parent window is already visible, we had been realized before we |
239 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
240 | // initialized yet and we have to do it now | |
241 | if (GTK_WIDGET_REALIZED(m_wxwindow)) | |
242 | gtk_glwindow_realized_callback( m_wxwindow, this ); | |
dc3065a5 | 243 | #endif // WXWIN_COMPATIBILITY_2_8 |
bc869971 VZ |
244 | |
245 | if (GTK_WIDGET_MAPPED(m_wxwindow)) | |
246 | gtk_glwindow_map_callback( m_wxwindow, this ); | |
247 | ||
670f9935 | 248 | return true; |
a6f5aa49 VZ |
249 | } |
250 | ||
498ace9e | 251 | Window wxGLCanvas::GetXWindow() const |
8b089c5e | 252 | { |
f089940f | 253 | GdkWindow* window = GTKGetDrawingWindow(); |
498ace9e | 254 | return window ? GDK_WINDOW_XWINDOW(window) : 0; |
8b089c5e JS |
255 | } |
256 | ||
8b089c5e JS |
257 | void wxGLCanvas::OnInternalIdle() |
258 | { | |
dc3065a5 | 259 | if (m_exposed) |
8b089c5e JS |
260 | { |
261 | wxPaintEvent event( GetId() ); | |
262 | event.SetEventObject( this ); | |
937013e0 | 263 | HandleWindowEvent( event ); |
8b089c5e | 264 | |
670f9935 | 265 | m_exposed = false; |
8b089c5e JS |
266 | GetUpdateRegion().Clear(); |
267 | } | |
2b5f62a0 | 268 | |
8b089c5e JS |
269 | wxWindow::OnInternalIdle(); |
270 | } | |
271 | ||
dc3065a5 VZ |
272 | #if WXWIN_COMPATIBILITY_2_8 |
273 | ||
274 | void wxGLCanvas::GTKInitImplicitContext() | |
275 | { | |
276 | if ( !m_glContext && m_createImplicitContext ) | |
277 | { | |
278 | wxGLContext *share = m_sharedContext; | |
279 | if ( !share && m_sharedContextOf ) | |
280 | share = m_sharedContextOf->m_glContext; | |
281 | ||
282 | m_glContext = new wxGLContext(this, share); | |
283 | } | |
284 | } | |
a6f5aa49 | 285 | |
dc3065a5 | 286 | #endif // WXWIN_COMPATIBILITY_2_8 |
a6f5aa49 | 287 | |
dc3065a5 | 288 | #endif // wxUSE_GLCANVAS |