[gtk+] Add an example of foreign drawing



commit b5bcc299adc1f32e6d1d4f332204399de89823f7
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Dec 2 18:46:54 2015 -0500

    Add an example of foreign drawing
    
    This uses the gtk_render apis with a style context constructed
    from a style path.

 tests/Makefile.am      |    4 ++
 tests/foreigndrawing.c |  121 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 50f2986..ac8181f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -168,6 +168,7 @@ noinst_PROGRAMS =  $(TEST_PROGS)    \
        testpopover                     \
        gdkgears                        \
        listmodel                       \
+       foreigndrawing                  \
        $(NULL)
 
 if USE_X11
@@ -308,6 +309,7 @@ testrevealer_DEPENDENCIES = $(TEST_DEPS)
 testtitlebar_DEPENDENCIES = $(TEST_DEPS)
 testwindowsize_DEPENDENCIES = $(TEST_DEPS)
 listmodel_DEPENDENCIES = $(TEST_DEPS)
+foreigndrawing_DEPENDENCIES = $(TEST_DEPS)
 
 animated_resizing_SOURCES =    \
        animated-resizing.c     \
@@ -542,6 +544,8 @@ testglblending_SOURCES =    \
 
 listmodel_SOURCES = listmodel.c
 
+foreigndrawing_SOURCES = foreigndrawing.c
+
 EXTRA_DIST +=                  \
        gradient1.png           \
        testgtk.1               \
diff --git a/tests/foreigndrawing.c b/tests/foreigndrawing.c
new file mode 100644
index 0000000..5d274ea
--- /dev/null
+++ b/tests/foreigndrawing.c
@@ -0,0 +1,121 @@
+/* foreign-drawing.c
+ * Copyright (C) 2015 Red Hat, Inc
+ * Author: Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtk/gtk.h>
+
+typedef struct {
+  GType type;
+  const gchar *name;
+  const gchar *class;
+  GtkStateFlags state;
+} PathElt;
+
+static GtkStyleContext *
+get_style (PathElt pelt[], gint n_elts)
+{
+  GtkWidgetPath *path;
+  gint i;
+  GtkStyleContext *context;
+
+  path = gtk_widget_path_new ();
+
+  for (i = 0; i < n_elts; i++)
+    {
+      gtk_widget_path_append_type (path, pelt[i].type);
+      if (pelt[i].name)
+        gtk_widget_path_iter_set_object_name (path, i, pelt[i].name);
+      if (pelt[i].class)
+        gtk_widget_path_iter_add_class (path, i, pelt[i].class);
+      gtk_widget_path_iter_set_state (path, i, pelt[i].state);
+    }
+
+  context = gtk_style_context_new ();
+  gtk_style_context_set_path (context, path);
+  gtk_widget_path_unref (path);
+
+  return context;
+}
+
+static void
+draw_horizontal_scrollbar (GtkWidget *widget,
+                           cairo_t   *cr,
+                           gint       x,
+                           gint       y,
+                           gint       width,
+                           gint       height,
+                           gint       position)
+{
+  GtkStyleContext *context;
+
+  PathElt trough[2] = {
+    { GTK_TYPE_SCROLLBAR, "scrollbar", "horizontal", GTK_STATE_FLAG_NORMAL },
+    { G_TYPE_NONE, "trough", NULL, GTK_STATE_FLAG_NORMAL }
+  };
+
+  PathElt slider[3] = {
+    { GTK_TYPE_SCROLLBAR, "scrollbar", "horizontal", GTK_STATE_FLAG_NORMAL },
+    { G_TYPE_NONE, "trough", NULL, GTK_STATE_FLAG_NORMAL },
+    { G_TYPE_NONE, "slider", NULL, GTK_STATE_FLAG_NORMAL }
+  };
+
+  context = get_style (trough, G_N_ELEMENTS (trough));
+
+  gtk_render_background (context, cr, x, y, width, height);
+  gtk_render_frame (context, cr, x, y, width, height);
+
+  g_object_unref (context);
+
+  context = get_style (slider, G_N_ELEMENTS (slider));
+
+  gtk_render_slider (context, cr, x + position, y + 1, position + 30, height - 2, 
GTK_ORIENTATION_HORIZONTAL);
+
+  g_object_unref (context);
+}
+
+static gboolean
+draw_cb (GtkWidget *widget,
+         cairo_t   *cr)
+{
+  gint width;
+
+  width = gtk_widget_get_allocated_width (widget);
+
+  draw_horizontal_scrollbar (widget, cr, 10, 10, width - 20, 10, 30);
+
+  return FALSE;
+}
+
+int
+main (int argc, char *argv[])
+{
+  GtkWidget *window;
+
+  gtk_init (NULL, NULL);
+
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_widget_set_size_request (window, 200, 200);
+  gtk_widget_set_app_paintable (window, TRUE);
+
+  g_signal_connect (window, "draw", G_CALLBACK (draw_cb), NULL);
+
+  gtk_widget_show (window);
+
+  gtk_main ();
+
+  return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]