Add support for GTK 3
authorAndy Spencer <andy753421@gmail.com>
Mon, 3 Dec 2012 00:12:41 +0000 (00:12 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 21 Jan 2013 04:51:50 +0000 (04:51 +0000)
First, most of the code has already been converted to work the same on
the latest copy of GTK 2 and GTK 3. These changes include things like
using GDK_KEY_* and removing all the gdk threading code.

Second, a compatibility file was added to mimic GTK 3 features that will
not be added to GTK 2. This includes things like gtk_box_new.

Third, any other differences are resolved using GTK_CHECK_VERSION.
This was done as a last resort where there are actual differences
between GTK 2 and GTK 3, such as the "expose-event" to "draw" change.

configure.ac
examples/gl/gl.c
examples/gl/mkfile
src/compat.h [new file with mode: 0644]
src/grits-demo.c
src/grits-opengl.c
src/grits-test.c
src/grits.pc.in
src/gtkgl.c
src/gtkgl.h
src/tile-test.c

index e3e9dc4..33758b7 100644 (file)
@@ -26,11 +26,21 @@ PKG_PROG_PKG_CONFIG
 GTK_DOC_CHECK(1.9)
 
 # Check for required packages
-PKG_CHECK_MODULES(GLIB,  glib-2.0 >= 2.14 gobject-2.0 gthread-2.0 gmodule-2.0)
+PKG_CHECK_MODULES(GLIB,  glib-2.0 >= 2.32 gobject-2.0 gthread-2.0 gmodule-2.0)
 PKG_CHECK_MODULES(CAIRO, cairo)
-PKG_CHECK_MODULES(GTK,   gtk+-2.0 >= 2.16)
 PKG_CHECK_MODULES(SOUP,  libsoup-2.4 >= 2.26)
 
+# Check for gtk 3
+AC_ARG_ENABLE([gtk3],
+       AS_HELP_STRING([--enable-gtk3], [Build with gtk3 support]))
+AS_IF([test "$enable_gtk3" = "yes"],
+       [PKG_CHECK_MODULES(GTK, gtk+-3.0 >= 3.0)],
+       [PKG_CHECK_MODULES(GTK, gtk+-2.0 >= 2.18)])
+AS_IF([test "$enable_gtk3" = "yes"],
+       [GTK_VERSION=gtk+-3.0],
+       [GTK_VERSION=gtk+-2.0])
+AC_SUBST(GTK_VERSION)
+
 # Check for glut for example plugin
 AC_CHECK_LIB(glut, glutSolidTeapot, GLUT_LIBS="-lglut")
 AM_CONDITIONAL(HAVE_GLUT, test "$GLUT_LIBS" != "")
index 45075f6..1ece4c2 100644 (file)
@@ -100,10 +100,15 @@ gpointer setup(GtkWidget *widget)
        XVisualInfo *xvinfo  = glXChooseVisual(xdisplay, nscreen, attribs);
        GLXContext   context = glXCreateContext(xdisplay, xvinfo, 0, True);
 
-       /* Fix up colormap */
+       /* Fix up visual/colormap */
+#if GTK_CHECK_VERSION(3,0,0)
+       GdkVisual *visual = gdk_x11_screen_lookup_visual(screen, xvinfo->visualid);
+       gtk_widget_set_visual(widget, visual);
+#else
        GdkVisual   *visual  = gdk_x11_screen_lookup_visual(screen, xvinfo->visualid);
        GdkColormap *cmap    = gdk_colormap_new(visual, FALSE);
        gtk_widget_set_colormap(widget, cmap);
+#endif
 
        /* Disable GTK double buffering */
        gtk_widget_set_double_buffered(widget, FALSE);
@@ -326,7 +331,7 @@ int main(int argc, char **argv)
 {
        gtk_init_check(&argc, &argv);
        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       GtkWidget *box    = gtk_vbox_new(FALSE, 5);
+       GtkWidget *box    = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
        GtkWidget *draw   = gtk_drawing_area_new();
        GtkWidget *label  = gtk_label_new("Hello, World");
        GtkWidget *button = gtk_button_new_with_label("Hello, World");
@@ -334,7 +339,11 @@ int main(int argc, char **argv)
        g_signal_connect(window, "destroy",         G_CALLBACK(gtk_main_quit), NULL);
        g_signal_connect(window, "key-press-event", G_CALLBACK(key_press),     NULL);
        //g_signal_connect(draw,   "configure-event", G_CALLBACK(configure),     data);
+#if GTK_CHECK_VERSION(3,0,0)
+       g_signal_connect(draw,   "draw",            G_CALLBACK(expose),        data);
+#else
        g_signal_connect(draw,   "expose-event",    G_CALLBACK(expose),        data);
+#endif
        gtk_widget_set_size_request(draw,   300, 300);
        gtk_widget_set_size_request(button, -1,  50);
        gtk_box_pack_start(GTK_BOX(box), label,  FALSE, TRUE, 0);
index ecb6803..1205899 100644 (file)
@@ -1,4 +1,4 @@
-PKGS=gtk+-2.0
+PKGS=gtk+-3.0
 PROGS=gl
 
 # Cairo
diff --git a/src/compat.h b/src/compat.h
new file mode 100644 (file)
index 0000000..2e8b4e6
--- /dev/null
@@ -0,0 +1,10 @@
+#include <gtk/gtk.h>
+
+#if ! GTK_CHECK_VERSION(3,0,0)
+static inline GtkWidget *gtk_box_new(GtkOrientation orientation, gint spacing)
+{
+       return orientation == GTK_ORIENTATION_HORIZONTAL
+               ? gtk_hbox_new(0, spacing)
+               : gtk_vbox_new(0, spacing);
+}
+#endif
index 05803be..c0d7267 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "grits.h"
 
+#include "compat.h"
 
 static GritsPrefs   *prefs;
 static GritsPlugins *plugins;
@@ -127,7 +128,7 @@ static GtkWidget *setup_window(GtkUIManager *manager, GtkWidget **_notebook)
        GtkWidget *window   = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        GtkWidget *menu     = gtk_ui_manager_get_widget(manager, "/Menu");
        GtkWidget *notebook = gtk_notebook_new();
-       GtkWidget *vbox     = gtk_vbox_new(FALSE, 0);
+       GtkWidget *vbox     = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add(GTK_CONTAINER(window), vbox);
        gtk_box_pack_start(GTK_BOX(vbox), menu,               FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(viewer), TRUE,  TRUE, 0);
index c4c7dac..2bd0082 100644 (file)
@@ -447,7 +447,11 @@ static void on_realize(GritsOpenGL *opengl, gpointer _)
        /* Connect signals and idle functions now that opengl is fully initialized */
        gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
        g_signal_connect(opengl, "configure-event",  G_CALLBACK(_set_projection), NULL);
+#if GTK_CHECK_VERSION(3,0,0)
+       g_signal_connect(opengl, "draw",             G_CALLBACK(on_expose),       NULL);
+#else
        g_signal_connect(opengl, "expose-event",     G_CALLBACK(on_expose),       NULL);
+#endif
 
        g_signal_connect(opengl, "key-press-event",  G_CALLBACK(on_key_press),    NULL);
 
index 6c393e0..cde36cd 100644 (file)
@@ -21,6 +21,8 @@
 
 #include "grits.h"
 
+#include "compat.h"
+
 GritsPrefs   *prefs   = NULL;
 GritsPlugins *plugins = NULL;
 GritsViewer  *viewer  = NULL;
@@ -72,7 +74,7 @@ int main(int argc, char **argv)
        viewer  = grits_opengl_new(plugins, prefs);
 
        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       GtkWidget *vbox   = gtk_vbox_new(FALSE, 0);
+       GtkWidget *vbox   = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        GtkWidget *config = gtk_notebook_new();
        g_signal_connect(window, "delete-event",    G_CALLBACK(on_delete),    NULL);
        g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), NULL);
@@ -97,7 +99,7 @@ int main(int argc, char **argv)
        gtk_widget_show_all(config);
        gtk_main();
 
-       gdk_display_close(gdk_display_get_default());
+       //gdk_display_close(gdk_display_get_default());
 
        prefs   = NULL;
        plugins = NULL;
index da0ea6a..81a41cc 100644 (file)
@@ -6,6 +6,6 @@ includedir=@includedir@
 Name: Grits
 Description: Grits is a Virtual Globe library
 Version: @VERSION@
-Requires: gmodule-2.0 gtk+-2.0 libsoup-2.4
+Requires: gmodule-2.0 libsoup-2.4 @GTK_VERSION@
 Libs: -L${libdir} -lgrits @GL_LIBS@
 Cflags: -I${includedir}/grits @GL_CFLAGS@
index 6c4913b..19f3219 100644 (file)
@@ -108,11 +108,16 @@ void gtk_gl_enable(GtkWidget *widget)
 
        g_object_set_data(G_OBJECT(widget), "glcontext", context);
 
-       /* Fix up colormap */
+       /* Fix up visual/colormap */
+#if GTK_CHECK_VERSION(3,0,0)
+       GdkVisual *visual = gdk_x11_screen_lookup_visual(screen, xvinfo->visualid);
+       gtk_widget_set_visual(widget, visual);
+#else
        GdkVisual   *visual = gdk_x11_screen_lookup_visual(screen, xvinfo->visualid);
        GdkColormap *cmap   = gdk_colormap_new(visual, FALSE);
        gtk_widget_set_colormap(widget, cmap);
        g_object_unref(cmap);
+#endif
        XFree(xvinfo);
 
        /* Disable GTK double buffering */
index fac5086..0f9f2a0 100644 (file)
 /* Call before widget is realized */
 void gtk_gl_enable(GtkWidget *widget);
 
-/* Call at the start of "expose-event" */
+/* Call at the start of expose */
 void gtk_gl_begin(GtkWidget *widget);
 
-/* Call at the end of "expose-event" */
+/* Call at the end of expose */
 void gtk_gl_end(GtkWidget *widget);
 
 /* Call when done to cleanup data */
index 0951541..e4b2379 100644 (file)
@@ -23,6 +23,8 @@
 #include "data/grits-tms.h"
 #include "objects/grits-tile.h"
 
+#include "compat.h"
+
 struct CacheState {
        GtkWidget *image;
        GtkWidget *status;
@@ -151,8 +153,8 @@ int main(int argc, char **argv)
        gtk_init(&argc, &argv);
 
        GtkWidget *win        = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       GtkWidget *vbox1      = gtk_vbox_new(FALSE, 0);
-       GtkWidget *vbox2      = gtk_vbox_new(FALSE, 0);
+       GtkWidget *vbox1      = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+       GtkWidget *vbox2      = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        GtkWidget *status     = gtk_statusbar_new();
        GtkWidget *scroll     = gtk_scrolled_window_new(NULL, NULL);
        GtkWidget *bmng_image = gtk_image_new();