[gtk+/wip/css: 10/16] gtk-demo: Add a simple example for theming
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/css: 10/16] gtk-demo: Add a simple example for theming
- Date: Wed, 23 May 2012 16:20:41 +0000 (UTC)
commit 2d8f492aa67e611b19c62eaacaa2ff898e0c6676
Author: Benjamin Otte <otte redhat com>
Date: Sun May 13 19:10:55 2012 +0200
gtk-demo: Add a simple example for theming
demos/gtk-demo/Makefile.am | 1 +
demos/gtk-demo/css_basics.c | 133 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 134 insertions(+), 0 deletions(-)
---
diff --git a/demos/gtk-demo/Makefile.am b/demos/gtk-demo/Makefile.am
index 0c4da0f..f261a10 100644
--- a/demos/gtk-demo/Makefile.am
+++ b/demos/gtk-demo/Makefile.am
@@ -15,6 +15,7 @@ demos = \
clipboard.c \
colorsel.c \
combobox.c \
+ css_basics.c \
dialog.c \
drawingarea.c \
editable_cells.c \
diff --git a/demos/gtk-demo/css_basics.c b/demos/gtk-demo/css_basics.c
new file mode 100644
index 0000000..0cbabbe
--- /dev/null
+++ b/demos/gtk-demo/css_basics.c
@@ -0,0 +1,133 @@
+/* CSS Theming/CSS Basics
+ *
+ * Gtk themes are written using CSS. Every widget is build of multiple items
+ * that you can style very similarly to a regular website.
+ *
+ */
+
+#include <gtk/gtk.h>
+
+static GtkWidget *window = NULL;
+
+#define DEFAULT_CSS \
+ "/* You can edit the CSS to change the appearance of this Window */\n" \
+ "\n" \
+ "GtkWindow {\n" \
+ " engine: none;\n" \
+ " background-image: none;\n" \
+ " background-color: brown;\n" \
+ "}\n" \
+ "\n" \
+ "GtkTextView {\n" \
+ " color: green;\n" \
+ "}\n" \
+ "\n"
+
+static void
+show_parsing_error (GtkCssProvider *provider,
+ GtkCssSection *section,
+ const GError *error,
+ GtkTextBuffer *buffer)
+{
+ GtkTextIter start, end;
+ const char *tag_name;
+
+ gtk_text_buffer_get_iter_at_line_index (buffer,
+ &start,
+ gtk_css_section_get_start_line (section),
+ gtk_css_section_get_start_position (section));
+ gtk_text_buffer_get_iter_at_line_index (buffer,
+ &end,
+ gtk_css_section_get_end_line (section),
+ gtk_css_section_get_end_position (section));
+
+ if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
+ tag_name = "warning";
+ else
+ tag_name = "error";
+
+ gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
+}
+
+static void
+css_text_changed (GtkTextBuffer *buffer,
+ GtkCssProvider *provider)
+{
+ GtkTextIter start, end;
+ char *text;
+
+ gtk_text_buffer_get_start_iter (buffer, &start);
+ gtk_text_buffer_get_end_iter (buffer, &end);
+ gtk_text_buffer_remove_all_tags (buffer, &start, &end);
+
+ text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+ gtk_css_provider_load_from_data (provider, text, -1, NULL);
+ g_free (text);
+
+ gtk_style_context_reset_widgets (gdk_screen_get_default ());
+}
+
+static void
+apply_css (GtkWidget *widget, GtkStyleProvider *provider)
+{
+ gtk_style_context_add_provider (gtk_widget_get_style_context (widget), provider, G_MAXUINT);
+ if (GTK_IS_CONTAINER (widget))
+ gtk_container_forall (GTK_CONTAINER (widget), (GtkCallback) apply_css, provider);
+}
+
+GtkWidget *
+do_css_basics (GtkWidget *do_widget)
+{
+ if (!window)
+ {
+ GtkWidget *container, *child;
+ GtkStyleProvider *provider;
+ GtkTextBuffer *text;
+
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (do_widget));
+ gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
+ g_signal_connect (window, "destroy",
+ G_CALLBACK (gtk_widget_destroyed), &window);
+
+ text = gtk_text_buffer_new (NULL);
+ gtk_text_buffer_create_tag (text,
+ "warning",
+ "underline", PANGO_UNDERLINE_SINGLE,
+ NULL);
+ gtk_text_buffer_create_tag (text,
+ "error",
+ "underline", PANGO_UNDERLINE_ERROR,
+ NULL);
+
+ provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
+
+ container = gtk_scrolled_window_new (NULL, NULL);
+ gtk_container_add (GTK_CONTAINER (window), container);
+ child = gtk_text_view_new_with_buffer (text);
+ gtk_container_add (GTK_CONTAINER (container), child);
+ g_signal_connect (text,
+ "changed",
+ G_CALLBACK (css_text_changed),
+ provider);
+ gtk_text_buffer_set_text (text,
+ DEFAULT_CSS,
+ -1);
+ g_signal_connect (provider,
+ "parsing-error",
+ G_CALLBACK (show_parsing_error),
+ gtk_text_view_get_buffer (GTK_TEXT_VIEW (child)));
+
+ apply_css (window, provider);
+ }
+
+ if (!gtk_widget_get_visible (window))
+ gtk_widget_show_all (window);
+ else
+ {
+ gtk_widget_destroy (window);
+ window = NULL;
+ }
+
+ return window;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]