| 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 | #ifndef WX_PRECOMP |
| 20 | #include "wx/app.h" |
| 21 | #include "wx/frame.h" |
| 22 | #include "wx/colour.h" |
| 23 | #include "wx/module.h" |
| 24 | #endif // WX_PRECOMP |
| 25 | |
| 26 | #include <gtk/gtk.h> |
| 27 | #include <gdk/gdkx.h> |
| 28 | |
| 29 | #if WXWIN_COMPATIBILITY_2_8 |
| 30 | |
| 31 | //----------------------------------------------------------------------------- |
| 32 | // "realize" from m_wxwindow: used to create m_glContext implicitly |
| 33 | //----------------------------------------------------------------------------- |
| 34 | |
| 35 | extern "C" { |
| 36 | static void |
| 37 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) |
| 38 | { |
| 39 | win->GTKInitImplicitContext(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #endif // WXWIN_COMPATIBILITY_2_8 |
| 44 | |
| 45 | //----------------------------------------------------------------------------- |
| 46 | // "map" from m_wxwindow |
| 47 | //----------------------------------------------------------------------------- |
| 48 | |
| 49 | extern "C" { |
| 50 | static void |
| 51 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) |
| 52 | { |
| 53 | wxPaintEvent event( win->GetId() ); |
| 54 | event.SetEventObject( win ); |
| 55 | win->HandleWindowEvent( event ); |
| 56 | |
| 57 | win->m_exposed = false; |
| 58 | win->GetUpdateRegion().Clear(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //----------------------------------------------------------------------------- |
| 63 | // "expose_event" of m_wxwindow |
| 64 | //----------------------------------------------------------------------------- |
| 65 | |
| 66 | extern "C" { |
| 67 | static gboolean |
| 68 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) |
| 69 | { |
| 70 | win->m_exposed = true; |
| 71 | |
| 72 | win->GetUpdateRegion().Union( gdk_event->area.x, |
| 73 | gdk_event->area.y, |
| 74 | gdk_event->area.width, |
| 75 | gdk_event->area.height ); |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | //----------------------------------------------------------------------------- |
| 81 | // "size_allocate" of m_wxwindow |
| 82 | //----------------------------------------------------------------------------- |
| 83 | |
| 84 | extern "C" { |
| 85 | static void |
| 86 | gtk_glcanvas_size_callback(GtkWidget *WXUNUSED(widget), |
| 87 | GtkAllocation * WXUNUSED(alloc), |
| 88 | wxGLCanvas *win) |
| 89 | { |
| 90 | if (!win->m_hasVMT) |
| 91 | return; |
| 92 | |
| 93 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); |
| 94 | event.SetEventObject( win ); |
| 95 | win->HandleWindowEvent( event ); |
| 96 | } |
| 97 | } |
| 98 | |
| 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 | |
| 127 | //--------------------------------------------------------------------------- |
| 128 | // wxGlCanvas |
| 129 | //--------------------------------------------------------------------------- |
| 130 | |
| 131 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
| 132 | |
| 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 |
| 142 | : m_createImplicitContext(false) |
| 143 | #endif |
| 144 | { |
| 145 | Create(parent, id, pos, size, style, name, attribList, palette); |
| 146 | } |
| 147 | |
| 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) |
| 158 | : m_createImplicitContext(true) |
| 159 | { |
| 160 | m_sharedContext = NULL; |
| 161 | m_sharedContextOf = NULL; |
| 162 | |
| 163 | Create(parent, id, pos, size, style, name, attribList, palette); |
| 164 | } |
| 165 | |
| 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) |
| 175 | : m_createImplicitContext(true) |
| 176 | { |
| 177 | m_sharedContext = wx_const_cast(wxGLContext *, shared); |
| 178 | |
| 179 | Create(parent, id, pos, size, style, name, attribList, palette); |
| 180 | } |
| 181 | |
| 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 ) |
| 189 | : m_createImplicitContext(true) |
| 190 | { |
| 191 | m_sharedContext = NULL; |
| 192 | m_sharedContextOf = wx_const_cast(wxGLCanvas *, shared); |
| 193 | |
| 194 | Create(parent, id, pos, size, style, name, attribList, palette); |
| 195 | } |
| 196 | |
| 197 | #endif // WXWIN_COMPATIBILITY_2_8 |
| 198 | |
| 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, |
| 206 | const wxPalette& WXUNUSED_UNLESS_DEBUG(palette)) |
| 207 | { |
| 208 | wxASSERT_MSG( !palette.IsOk(), _T("palettes not supported") ); |
| 209 | |
| 210 | m_exposed = false; |
| 211 | m_noExpose = true; |
| 212 | m_nativeSizeEvent = true; |
| 213 | |
| 214 | if ( !InitVisual(attribList) ) |
| 215 | return false; |
| 216 | |
| 217 | // watch for the "parent-set" signal on m_wxwindow so we can set colormap |
| 218 | // before m_wxwindow is realized (which will occur before |
| 219 | // wxWindow::Create() returns if parent is already visible) |
| 220 | unsigned sig_id = g_signal_lookup("parent-set", GTK_TYPE_WIDGET); |
| 221 | g_signal_add_emission_hook(sig_id, 0, parent_set_hook, this, NULL); |
| 222 | |
| 223 | wxWindow::Create( parent, id, pos, size, style, name ); |
| 224 | |
| 225 | gtk_widget_set_double_buffered(m_wxwindow, false); |
| 226 | |
| 227 | #if WXWIN_COMPATIBILITY_2_8 |
| 228 | g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this); |
| 229 | #endif // WXWIN_COMPATIBILITY_2_8 |
| 230 | g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this); |
| 231 | g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this); |
| 232 | g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this); |
| 233 | |
| 234 | #if WXWIN_COMPATIBILITY_2_8 |
| 235 | // if our parent window is already visible, we had been realized before we |
| 236 | // connected to the "realize" signal and hence our m_glContext hasn't been |
| 237 | // initialized yet and we have to do it now |
| 238 | if (GTK_WIDGET_REALIZED(m_wxwindow)) |
| 239 | gtk_glwindow_realized_callback( m_wxwindow, this ); |
| 240 | #endif // WXWIN_COMPATIBILITY_2_8 |
| 241 | |
| 242 | if (GTK_WIDGET_MAPPED(m_wxwindow)) |
| 243 | gtk_glwindow_map_callback( m_wxwindow, this ); |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | Window wxGLCanvas::GetXWindow() const |
| 249 | { |
| 250 | GdkWindow *window = m_wxwindow->window; |
| 251 | return window ? GDK_WINDOW_XWINDOW(window) : 0; |
| 252 | } |
| 253 | |
| 254 | void wxGLCanvas::OnInternalIdle() |
| 255 | { |
| 256 | if (m_exposed) |
| 257 | { |
| 258 | wxPaintEvent event( GetId() ); |
| 259 | event.SetEventObject( this ); |
| 260 | HandleWindowEvent( event ); |
| 261 | |
| 262 | m_exposed = false; |
| 263 | GetUpdateRegion().Clear(); |
| 264 | } |
| 265 | |
| 266 | wxWindow::OnInternalIdle(); |
| 267 | } |
| 268 | |
| 269 | #if WXWIN_COMPATIBILITY_2_8 |
| 270 | |
| 271 | void wxGLCanvas::GTKInitImplicitContext() |
| 272 | { |
| 273 | if ( !m_glContext && m_createImplicitContext ) |
| 274 | { |
| 275 | wxGLContext *share = m_sharedContext; |
| 276 | if ( !share && m_sharedContextOf ) |
| 277 | share = m_sharedContextOf->m_glContext; |
| 278 | |
| 279 | m_glContext = new wxGLContext(this, share); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | #endif // WXWIN_COMPATIBILITY_2_8 |
| 284 | |
| 285 | #endif // wxUSE_GLCANVAS |