]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/glcanvas.cpp
support retina display
[wxWidgets.git] / src / gtk / glcanvas.cpp
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 #ifndef __WXGTK3__
44 extern "C" {
45 static void
46 gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
47 {
48 wxPaintEvent event( win->GetId() );
49 event.SetEventObject( win );
50 win->HandleWindowEvent( event );
51
52 win->m_exposed = false;
53 win->GetUpdateRegion().Clear();
54 }
55 }
56 #endif
57
58 //-----------------------------------------------------------------------------
59 // "expose_event" of m_wxwindow
60 //-----------------------------------------------------------------------------
61
62 extern "C" {
63 #ifdef __WXGTK3__
64 static gboolean draw(GtkWidget*, cairo_t* cr, wxGLCanvas* win)
65 {
66 win->m_exposed = true;
67 if (win->m_cairoPaintContext == NULL)
68 {
69 win->m_cairoPaintContext = cr;
70 cairo_reference(cr);
71 }
72 double x1, y1, x2, y2;
73 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
74 win->GetUpdateRegion().Union(int(x1), int(y1), int(x2 - x1), int(y2 - y1));
75 return false;
76 }
77 #else
78 static gboolean
79 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
80 {
81 win->m_exposed = true;
82
83 win->GetUpdateRegion().Union( gdk_event->area.x,
84 gdk_event->area.y,
85 gdk_event->area.width,
86 gdk_event->area.height );
87 return false;
88 }
89 #endif
90 }
91
92 //-----------------------------------------------------------------------------
93 // "size_allocate" of m_wxwindow
94 //-----------------------------------------------------------------------------
95
96 extern "C" {
97 static void
98 gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget),
99 GtkAllocation * WXUNUSED(alloc),
100 wxGLCanvas *win)
101 {
102 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
103 event.SetEventObject( win );
104 win->HandleWindowEvent( event );
105 }
106 }
107
108 //-----------------------------------------------------------------------------
109 // emission hook for "parent-set"
110 //-----------------------------------------------------------------------------
111
112 extern "C" {
113 static gboolean
114 parent_set_hook(GSignalInvocationHint*, guint, const GValue* param_values, void* data)
115 {
116 wxGLCanvas* win = (wxGLCanvas*)data;
117 if (g_value_peek_pointer(&param_values[0]) == win->m_wxwindow)
118 {
119 const XVisualInfo* xvi = win->GetXVisualInfo();
120 GdkVisual* visual = gtk_widget_get_visual(win->m_wxwindow);
121 if (GDK_VISUAL_XVISUAL(visual)->visualid != xvi->visualid)
122 {
123 GdkScreen* screen = gtk_widget_get_screen(win->m_wxwindow);
124 visual = gdk_x11_screen_lookup_visual(screen, xvi->visualid);
125 #ifdef __WXGTK3__
126 gtk_widget_set_visual(win->m_wxwindow, visual);
127 #else
128 GdkColormap* colormap = gdk_colormap_new(visual, false);
129 gtk_widget_set_colormap(win->m_wxwindow, colormap);
130 g_object_unref(colormap);
131 #endif
132 }
133 // remove hook
134 return false;
135 }
136 return true;
137 }
138 }
139
140 //---------------------------------------------------------------------------
141 // wxGlCanvas
142 //---------------------------------------------------------------------------
143
144 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
145
146 wxGLCanvas::wxGLCanvas(wxWindow *parent,
147 wxWindowID id,
148 const int *attribList,
149 const wxPoint& pos,
150 const wxSize& size,
151 long style,
152 const wxString& name,
153 const wxPalette& palette)
154 #if WXWIN_COMPATIBILITY_2_8
155 : m_createImplicitContext(false)
156 #endif
157 {
158 Create(parent, id, pos, size, style, name, attribList, palette);
159 }
160
161 #if WXWIN_COMPATIBILITY_2_8
162
163 wxGLCanvas::wxGLCanvas(wxWindow *parent,
164 wxWindowID id,
165 const wxPoint& pos,
166 const wxSize& size,
167 long style,
168 const wxString& name,
169 const int *attribList,
170 const wxPalette& palette)
171 : m_createImplicitContext(true)
172 {
173 m_sharedContext = NULL;
174 m_sharedContextOf = NULL;
175
176 Create(parent, id, pos, size, style, name, attribList, palette);
177 }
178
179 wxGLCanvas::wxGLCanvas(wxWindow *parent,
180 const wxGLContext *shared,
181 wxWindowID id,
182 const wxPoint& pos,
183 const wxSize& size,
184 long style,
185 const wxString& name,
186 const int *attribList,
187 const wxPalette& palette)
188 : m_createImplicitContext(true)
189 {
190 m_sharedContext = const_cast<wxGLContext *>(shared);
191
192 Create(parent, id, pos, size, style, name, attribList, palette);
193 }
194
195 wxGLCanvas::wxGLCanvas(wxWindow *parent,
196 const wxGLCanvas *shared,
197 wxWindowID id,
198 const wxPoint& pos, const wxSize& size,
199 long style, const wxString& name,
200 const int *attribList,
201 const wxPalette& palette )
202 : m_createImplicitContext(true)
203 {
204 m_sharedContext = NULL;
205 m_sharedContextOf = const_cast<wxGLCanvas *>(shared);
206
207 Create(parent, id, pos, size, style, name, attribList, palette);
208 }
209
210 #endif // WXWIN_COMPATIBILITY_2_8
211
212 bool wxGLCanvas::Create(wxWindow *parent,
213 wxWindowID id,
214 const wxPoint& pos,
215 const wxSize& size,
216 long style,
217 const wxString& name,
218 const int *attribList,
219 const wxPalette& palette)
220 {
221 #if wxUSE_PALETTE
222 wxASSERT_MSG( !palette.IsOk(), wxT("palettes not supported") );
223 #endif // wxUSE_PALETTE
224 wxUnusedVar(palette); // Unused when wxDEBUG_LEVEL==0
225
226 m_exposed = false;
227 m_noExpose = true;
228 m_nativeSizeEvent = true;
229 #ifdef __WXGTK3__
230 m_cairoPaintContext = NULL;
231 m_backgroundStyle = wxBG_STYLE_PAINT;
232 #endif
233
234 if ( !InitVisual(attribList) )
235 return false;
236
237 // watch for the "parent-set" signal on m_wxwindow so we can set colormap
238 // before m_wxwindow is realized (which will occur before
239 // wxWindow::Create() returns if parent is already visible)
240 unsigned sig_id = g_signal_lookup("parent-set", GTK_TYPE_WIDGET);
241 g_signal_add_emission_hook(sig_id, 0, parent_set_hook, this, NULL);
242
243 wxWindow::Create( parent, id, pos, size, style, name );
244
245 gtk_widget_set_double_buffered(m_wxwindow, false);
246
247 #if WXWIN_COMPATIBILITY_2_8
248 g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this);
249 #endif // WXWIN_COMPATIBILITY_2_8
250 #ifdef __WXGTK3__
251 g_signal_connect(m_wxwindow, "draw", G_CALLBACK(draw), this);
252 #else
253 g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this);
254 g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this);
255 #endif
256 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this);
257
258 #if WXWIN_COMPATIBILITY_2_8
259 // if our parent window is already visible, we had been realized before we
260 // connected to the "realize" signal and hence our m_glContext hasn't been
261 // initialized yet and we have to do it now
262 if (gtk_widget_get_realized(m_wxwindow))
263 gtk_glwindow_realized_callback( m_wxwindow, this );
264 #endif // WXWIN_COMPATIBILITY_2_8
265
266 #ifndef __WXGTK3__
267 if (gtk_widget_get_mapped(m_wxwindow))
268 gtk_glwindow_map_callback( m_wxwindow, this );
269 #endif
270
271 return true;
272 }
273
274 bool wxGLCanvas::SetBackgroundStyle(wxBackgroundStyle /* style */)
275 {
276 return false;
277 }
278
279 Window wxGLCanvas::GetXWindow() const
280 {
281 GdkWindow* window = GTKGetDrawingWindow();
282 return window ? GDK_WINDOW_XID(window) : 0;
283 }
284
285 void wxGLCanvas::OnInternalIdle()
286 {
287 if (m_exposed)
288 {
289 #ifdef __WXGTK3__
290 GTKSendPaintEvents(m_cairoPaintContext);
291 cairo_destroy(m_cairoPaintContext);
292 m_cairoPaintContext = NULL;
293 #else
294 wxPaintEvent event( GetId() );
295 event.SetEventObject( this );
296 HandleWindowEvent( event );
297 #endif
298
299 m_exposed = false;
300 GetUpdateRegion().Clear();
301 }
302
303 wxWindow::OnInternalIdle();
304 }
305
306 #if WXWIN_COMPATIBILITY_2_8
307
308 void wxGLCanvas::GTKInitImplicitContext()
309 {
310 if ( !m_glContext && m_createImplicitContext )
311 {
312 wxGLContext *share = m_sharedContext;
313 if ( !share && m_sharedContextOf )
314 share = m_sharedContextOf->m_glContext;
315
316 m_glContext = new wxGLContext(this, share);
317 }
318 }
319
320 #endif // WXWIN_COMPATIBILITY_2_8
321
322 #endif // wxUSE_GLCANVAS