diff options
author | Matthias Clasen <mclasen@redhat.com> | 2015-12-02 18:46:54 -0500 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2015-12-02 18:49:07 -0500 |
commit | b5bcc299adc1f32e6d1d4f332204399de89823f7 (patch) | |
tree | 370335a7638c80bcb70ec5baef923f5cdff96137 /tests | |
parent | 7fa37e4bf886745e8e7b07b6653513ac4c5172d3 (diff) | |
download | gtk+-b5bcc299adc1f32e6d1d4f332204399de89823f7.tar.gz |
Add an example of foreign drawing
This uses the gtk_render apis with a style context constructed
from a style path.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 4 | ||||
-rw-r--r-- | tests/foreigndrawing.c | 121 |
2 files changed, 125 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 50f2986c5f..ac8181fe23 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 0000000000..5d274eab04 --- /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; +} |