[hitori] Use CSS provider to control colors used in UI



commit 9effdc2a21eadd9da61a0b6a31df5e9a5b2ce01d
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sat Jul 5 13:18:07 2014 -0500

    Use CSS provider to control colors used in UI
    
    This style looks the same as Hitori using Adwaita prior to GTK+ 3.13.3
    
    https://bugzilla.gnome.org/show_bug.cgi?id=732300

 Makefile.am     |    6 +++++
 data/hitori.css |   24 +++++++++++++++++++
 src/interface.c |   68 +++++++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 86 insertions(+), 12 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index f9d8436..37fd25a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -69,6 +69,11 @@ EXTRA_DIST = \
        $(icon48_DATA) \
        $(NULL)
 
+# CSS
+
+cssdir = $(datadir)/hitori
+css_DATA = data/hitori.css
+
 # UI file
 uidir = $(datadir)/hitori
 ui_DATA = data/hitori.ui
@@ -83,6 +88,7 @@ desktopdir = $(datadir)/applications
 desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
 
 EXTRA_DIST += \
+       $(css_DATA) \
        $(ui_DATA) \
        $(desktop_in_files) \
        $(NULL)
diff --git a/data/hitori.css b/data/hitori.css
new file mode 100644
index 0000000..c89221b
--- /dev/null
+++ b/data/hitori.css
@@ -0,0 +1,24 @@
+/* Cell background color */
+
+ define-color unpainted-bg-color #ededed;
+ define-color painted-bg-color #f4f4f4;        /* Adwaita's insensitive-bg-color */
+
+/* Cell border color */
+
+ define-color unpainted-border-color #2e3436; /* Nearly black */
+ define-color painted-border-color #babcb8;
+ define-color hint-border-color red;
+
+/* Colors of numbers within cells */
+
+ define-color unpainted-number-color @unpainted-border-color;
+ define-color painted-number-color #a7aba7; /* Dark gray */
+ define-color mistaken-number-color #ef2929; /* Tango's lightest "scarlet red" */
+
+/* Shift-click a cell for tag1, ctrl-click for tag2 */
+
+ define-color tag1-unpainted-color #729fcf; /* Tango's lightest "sky blue" */
+ define-color tag1-painted-color alpha(@tag1-unpainted-color, 0.7);
+
+ define-color tag2-unpainted-color #8ae234; /* Tango's lightest "chameleon" */
+ define-color tag2-painted-color alpha(@tag2-unpainted-color, 0.7);
diff --git a/src/interface.c b/src/interface.c
index 3e5d6f1..fe7d7cc 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -29,8 +29,6 @@
 
 #define NORMAL_FONT_SCALE 0.9
 #define PAINTED_FONT_SCALE 0.6
-#define PAINTED_ALPHA 0.7
-#define NORMAL_ALPHA 1.0
 #define TAG_OFFSET 0.75
 #define TAG_RADIUS 0.25
 #define HINT_FLASHES 6
@@ -74,6 +72,7 @@ hitori_create_interface (Hitori *hitori)
        GError *error = NULL;
        GtkBuilder *builder;
        GtkStyleContext *style_context;
+       GtkCssProvider *css_provider;
        const PangoFontDescription *font;
        GMenuModel *app_menu;  /* owned */
 
@@ -127,6 +126,18 @@ hitori_create_interface (Hitori *hitori)
        hitori->normal_font_desc = pango_font_description_copy (font);
        hitori->painted_font_desc = pango_font_description_copy (font);
 
+       /* Load CSS for the drawing area */
+       css_provider = gtk_css_provider_new ();
+       gtk_css_provider_load_from_path (css_provider, PACKAGE_DATA_DIR"/hitori/hitori.css", &error);
+       if (error) {
+               g_warning ("Unable to load CSS: %s", error->message);
+               g_error_free (error);
+       } else {
+               gtk_style_context_add_provider (style_context, GTK_STYLE_PROVIDER (css_provider),
+                                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+       }
+       g_object_unref (css_provider);
+
        /* Reset the timer */
        hitori_reset_timer (hitori);
 
@@ -143,6 +154,14 @@ hitori_create_interface (Hitori *hitori)
 #define BORDER_LEFT 2.0
 
 static void
+lookup_color (GtkStyleContext *style_context, const gchar *name, GdkRGBA *result)
+{
+       if (!gtk_style_context_lookup_color (style_context, name, result)) {
+               g_warning ("Failed to look up color %s", name);
+       }
+}
+
+static void
 draw_cell (Hitori *hitori, GtkStyleContext *style_context, cairo_t *cr, gfloat cell_size, gdouble x_pos, 
gdouble y_pos,
            HitoriVector iter)
 {
@@ -152,8 +171,8 @@ draw_cell (Hitori *hitori, GtkStyleContext *style_context, cairo_t *cr, gfloat c
        GtkStateFlags state = 0;
        gboolean painted = FALSE;
        PangoFontDescription *font_desc;
-       GdkRGBA colour;
        GtkBorder border;
+       GdkRGBA colour = {0.0, 0.0, 0.0, 0.0};
 
        if (hitori->board[iter.x][iter.y].status & CELL_PAINTED) {
                painted = TRUE;
@@ -169,15 +188,24 @@ draw_cell (Hitori *hitori, GtkStyleContext *style_context, cairo_t *cr, gfloat c
                g_debug ("State: %u", state);
        }
 
-       gtk_style_context_get_background_color (style_context, state, &colour);
+       if (painted) {
+               lookup_color (style_context, "painted-bg-color", &colour);
+       } else {
+               lookup_color (style_context, "unpainted-bg-color", &colour);
+       }
+
        gdk_cairo_set_source_rgba (cr, &colour);
        cairo_rectangle (cr, x_pos, y_pos, cell_size, cell_size);
        cairo_fill (cr);
 
        /* If the cell is tagged, draw the tag dots */
        if (hitori->board[iter.x][iter.y].status & CELL_TAG1) {
-               /* Tango's lightest "sky blue" */
-               cairo_set_source_rgba (cr, 0.447058824, 0.623529412, 0.811764706, (painted == TRUE) ? 
PAINTED_ALPHA : NORMAL_ALPHA);
+               if (painted) {
+                       lookup_color (style_context, "tag1-painted-color", &colour);
+               } else {
+                       lookup_color (style_context, "tag1-unpainted-color", &colour);
+               }
+               gdk_cairo_set_source_rgba (cr, &colour);
 
                cairo_move_to (cr, x_pos, y_pos + TAG_OFFSET);
                cairo_line_to (cr, x_pos, y_pos);
@@ -187,8 +215,12 @@ draw_cell (Hitori *hitori, GtkStyleContext *style_context, cairo_t *cr, gfloat c
        }
 
        if (hitori->board[iter.x][iter.y].status & CELL_TAG2) {
-               /* Tango's lightest "chameleon" */
-               cairo_set_source_rgba (cr, 0.541176471, 0.88627451, 0.203921569, (painted == TRUE) ? 
PAINTED_ALPHA : NORMAL_ALPHA);
+               if (painted) {
+                       lookup_color (style_context, "tag2-painted-color", &colour);
+               } else {
+                       lookup_color (style_context, "tag2-unpainted-color", &colour);
+               }
+               gdk_cairo_set_source_rgba (cr, &colour);
 
                cairo_move_to (cr, x_pos + cell_size - TAG_OFFSET, y_pos);
                cairo_line_to (cr, x_pos + cell_size, y_pos);
@@ -198,7 +230,11 @@ draw_cell (Hitori *hitori, GtkStyleContext *style_context, cairo_t *cr, gfloat c
        }
 
        /* Draw the border */
-       gtk_style_context_get_border_color (style_context, state, &colour);
+       if (painted) {
+               lookup_color (style_context, "painted-border-color", &colour);
+       } else {
+               lookup_color (style_context, "unpainted-border-color", &colour);
+       }
        gdk_cairo_set_source_rgba (cr, &colour);
        cairo_set_line_width (cr, border.left);
        cairo_rectangle (cr, x_pos, y_pos, cell_size, cell_size);
@@ -213,10 +249,16 @@ draw_cell (Hitori *hitori, GtkStyleContext *style_context, cairo_t *cr, gfloat c
        font_desc = (painted == TRUE) ? hitori->painted_font_desc : hitori->normal_font_desc;
 
        if (hitori->board[iter.x][iter.y].status & CELL_ERROR) {
-               cairo_set_source_rgb (cr, 0.937254902, 0.160784314, 0.160784314); /* Tango's lightest 
"scarlet red" */
+               lookup_color (style_context, "mistaken-number-color", &colour);
+               gdk_cairo_set_source_rgba (cr, &colour);
                pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
+       } else if (painted) {
+               lookup_color (style_context, "painted-number-color", &colour);
+               gdk_cairo_set_source_rgba (cr, &colour);
+               pango_font_description_set_weight (font_desc, PANGO_WEIGHT_NORMAL);
        } else {
-               gtk_style_context_get_color (style_context, state, &colour);
+               g_assert (!painted);
+               lookup_color (style_context, "unpainted-number-color", &colour);
                gdk_cairo_set_source_rgba (cr, &colour);
                pango_font_description_set_weight (font_desc, PANGO_WEIGHT_NORMAL);
        }
@@ -288,7 +330,9 @@ hitori_draw_cb (GtkWidget *drawing_area, cairo_t *cr, Hitori *hitori)
        /* Draw a hint if applicable */
        if (hitori->hint_status % 2 == 1) {
                gfloat line_width = BORDER_LEFT * 2.5;
-               cairo_set_source_rgb (cr, 1, 0, 0); /* red */
+               GdkRGBA colour;
+               lookup_color (style_context, "hint-border-color", &colour);
+               gdk_cairo_set_source_rgba (cr, &colour);
                cairo_set_line_width (cr, line_width);
                cairo_rectangle (cr,
                                 hitori->hint_position.x * cell_size + line_width / 2,


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