[gnumeric] GUI: Dead kittens



commit 26b089d6af19b34f34f4e5d88da23866385b0345
Author: Morten Welinder <terra gnome org>
Date:   Tue May 3 21:07:46 2016 -0400

    GUI: Dead kittens

 src/gnumeric.css |   25 +++++++++
 src/gui-util.c   |  144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gui-util.h   |    3 +
 src/item-bar.c   |   55 ++++++++++++---------
 4 files changed, 204 insertions(+), 23 deletions(-)
---
diff --git a/src/gnumeric.css b/src/gnumeric.css
index 0779d10..29117fe 100644
--- a/src/gnumeric.css
+++ b/src/gnumeric.css
@@ -234,3 +234,28 @@ GtkBox.toolbarzone GtkToolbar * {
 }
 
 /* ------------------------------------------------------------------------- */
+/* Styles for Gtk+ 3.20 and later */
+/* ------------------------------------------------------------------------- */
+/* See bug 765921 as to why we duplicate the selectors.  */
+
+button.itembar, button.itembar * {
+  padding-left: 5px;
+  padding-right: 5px;
+  padding-top: 2px;
+  padding-bottom: 2px;
+}
+
+/* The whole column/row selected. */
+button:active.itembar, button:active.itembar * {
+  color: red;
+  font-weight: bold;
+}
+
+/* Some, but not all, of a column/row selected. */
+button:hover.itembar, button:hover.itembar * {
+  color: red;
+  font-weight: bold;
+}
+
+
+/* ------------------------------------------------------------------------- */
diff --git a/src/gui-util.c b/src/gui-util.c
index 3dd0010..d3fffbc 100644
--- a/src/gui-util.c
+++ b/src/gui-util.c
@@ -1358,3 +1358,147 @@ gnm_style_context_get_color (GtkStyleContext *context,
                                     color);
        gtk_style_context_restore (context);
 }
+
+// ---------------------------------------------------------------------------
+// Foreign drawing style code copied from foreigndrawing.c
+
+#if GTK_CHECK_VERSION(3,20,0)
+static void
+append_element (GtkWidgetPath *path,
+                const char    *selector)
+{
+  static const struct {
+    const char    *name;
+    GtkStateFlags  state_flag;
+  } pseudo_classes[] = {
+    { "active",        GTK_STATE_FLAG_ACTIVE },
+    { "hover",         GTK_STATE_FLAG_PRELIGHT },
+    { "selected",      GTK_STATE_FLAG_SELECTED },
+    { "disabled",      GTK_STATE_FLAG_INSENSITIVE },
+    { "indeterminate", GTK_STATE_FLAG_INCONSISTENT },
+    { "focus",         GTK_STATE_FLAG_FOCUSED },
+    { "backdrop",      GTK_STATE_FLAG_BACKDROP },
+    { "dir(ltr)",      GTK_STATE_FLAG_DIR_LTR },
+    { "dir(rtl)",      GTK_STATE_FLAG_DIR_RTL },
+    { "link",          GTK_STATE_FLAG_LINK },
+    { "visited",       GTK_STATE_FLAG_VISITED },
+    { "checked",       GTK_STATE_FLAG_CHECKED },
+    { "drop(active)",  GTK_STATE_FLAG_DROP_ACTIVE }
+  };
+  const char *next;
+  char *name;
+  char type;
+  guint i;
+
+  next = strpbrk (selector, "#.:");
+  if (next == NULL)
+    next = selector + strlen (selector);
+
+  name = g_strndup (selector, next - selector);
+  if (g_ascii_isupper (selector[0]))
+    {
+      GType gtype;
+      gtype = g_type_from_name (name);
+      if (gtype == G_TYPE_INVALID)
+        {
+          g_critical ("Unknown type name `%s'", name);
+          g_free (name);
+          return;
+        }
+      gtk_widget_path_append_type (path, gtype);
+    }
+  else
+    {
+      /* Omit type, we're using name */
+      gtk_widget_path_append_type (path, G_TYPE_NONE);
+      gtk_widget_path_iter_set_object_name (path, -1, name);
+    }
+  g_free (name);
+
+  while (*next != '\0')
+    {
+      type = *next;
+      selector = next + 1;
+      next = strpbrk (selector, "#.:");
+      if (next == NULL)
+        next = selector + strlen (selector);
+      name = g_strndup (selector, next - selector);
+
+      switch (type)
+        {
+        case '#':
+          gtk_widget_path_iter_set_name (path, -1, name);
+          break;
+
+        case '.':
+          gtk_widget_path_iter_add_class (path, -1, name);
+          break;
+
+        case ':':
+          for (i = 0; i < G_N_ELEMENTS (pseudo_classes); i++)
+            {
+              if (g_str_equal (pseudo_classes[i].name, name))
+                {
+                  gtk_widget_path_iter_set_state (path,
+                                                  -1,
+                                                  gtk_widget_path_iter_get_state (path, -1)
+                                                  | pseudo_classes[i].state_flag);
+                  break;
+                }
+            }
+          if (i == G_N_ELEMENTS (pseudo_classes))
+            g_critical ("Unknown pseudo-class :%s", name);
+          break;
+
+        default:
+          g_assert_not_reached ();
+          break;
+        }
+
+      g_free (name);
+    }
+}
+
+static GtkStyleContext *
+create_context_for_path (GtkWidgetPath   *path,
+                         GtkStyleContext *parent)
+{
+  GtkStyleContext *context;
+
+  context = gtk_style_context_new ();
+  gtk_style_context_set_path (context, path);
+  gtk_style_context_set_parent (context, parent);
+  /* Unfortunately, we have to explicitly set the state again here
+   * for it to take effect
+   */
+  gtk_style_context_set_state (context, gtk_widget_path_iter_get_state (path, -1));
+  gtk_widget_path_unref (path);
+
+  return context;
+}
+#endif
+
+GtkStyleContext *
+gnm_style_context_from_selector (GtkStyleContext *parent,
+                                const char      *selector)
+{
+#if GTK_CHECK_VERSION(3,20,0)
+  GtkWidgetPath *path;
+
+  if (parent)
+    path = gtk_widget_path_copy (gtk_style_context_get_path (parent));
+  else
+    path = gtk_widget_path_new ();
+
+  append_element (path, selector);
+
+  return create_context_for_path (path, parent);
+#else
+  (void)parent;
+  (void)selector;
+  g_assert_not_reached ();
+  return NULL;
+#endif
+}
+
+// ----------------------------------------------------------------------------
diff --git a/src/gui-util.h b/src/gui-util.h
index 905d59e..9d333e4 100644
--- a/src/gui-util.h
+++ b/src/gui-util.h
@@ -187,6 +187,9 @@ void gnm_style_context_get_color (GtkStyleContext *context,
                                  GtkStateFlags state,
                                  GdkRGBA *color);
 
+GtkStyleContext * gnm_style_context_from_selector (GtkStyleContext *parent,
+                                                  const char      *selector);
+
 G_END_DECLS
 
 #endif /* _GNM_GUI_UTIL_H_ */
diff --git a/src/item-bar.c b/src/item-bar.c
index df581e6..758ffa9 100644
--- a/src/item-bar.c
+++ b/src/item-bar.c
@@ -60,6 +60,7 @@ struct _GnmItemBar {
        PangoFont *selection_fonts[3];
        int selection_font_ascents[3];
        PangoRectangle selection_logical_sizes[3];
+       GtkStyleContext *styles[3];
 
        GdkRGBA grouping_color;
 
@@ -105,6 +106,11 @@ static const GtkStateFlags selection_type_flags[3] = {
        GTK_STATE_FLAG_ACTIVE
 };
 
+static const char * const selection_styles[3] = {
+       "button.itembar",
+       "button.itembar:hover",
+       "button.itembar:active"
+};
 
 static void
 ib_reload_color_style (GnmItemBar *ib)
@@ -133,7 +139,6 @@ ib_reload_sizing_style (GnmItemBar *ib)
        double const zoom_factor = sheet->last_zoom_factor_used;
        gboolean const char_label =
                ib->is_col_header && !sheet->convs->r1c1_addresses;
-       GtkStyleContext *context = goc_item_get_style_context (item);
        unsigned ui;
        PangoContext *pcontext =
                gtk_widget_get_pango_context (GTK_WIDGET (item->canvas));
@@ -141,15 +146,25 @@ ib_reload_sizing_style (GnmItemBar *ib)
        PangoAttrList *attr_list;
        GList *item_list;
 
-       gtk_style_context_save (context);
        for (ui = 0; ui < G_N_ELEMENTS (selection_type_flags); ui++) {
                GtkStateFlags state = selection_type_flags[ui];
                PangoFontDescription *desc;
                PangoRectangle ink_rect;
                const char *long_name;
-
+               GtkStyleContext *context;
+
+               g_clear_object (&ib->styles[ui]);
+#if GTK_CHECK_VERSION(3,20,0)
+               context = gnm_style_context_from_selector (NULL, selection_styles[ui]);
+#else
+               context = g_object_ref (goc_item_get_style_context (item));
+#endif
+
+               ib->styles[ui] = context;
+               gtk_style_context_save (context);
+#if !GTK_CHECK_VERSION(3,20,0)
                gtk_style_context_set_state (context, state);
-
+#endif
                gtk_style_context_get (context, state, "font", &desc, NULL);
                pango_font_description_set_size (desc,
                                                 zoom_factor * pango_font_description_get_size (desc));
@@ -187,12 +202,13 @@ ib_reload_sizing_style (GnmItemBar *ib)
                         strlen (long_name));
                pango_layout_get_extents (layout, NULL,
                                          &ib->selection_logical_sizes[ui]);
-       }
 
-       gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL,
-                                      &ib->padding);
+               if (state == GTK_STATE_FLAG_NORMAL)
+                       gtk_style_context_get_padding (context, state,
+                                                      &ib->padding);
 
-       gtk_style_context_restore (context);
+               gtk_style_context_restore (context);
+       }
 
        attr_list = pango_attr_list_new ();
        item_list = pango_itemize (pcontext, "A", 0, 1, attr_list, NULL);
@@ -317,14 +333,14 @@ ib_draw_cell (GnmItemBar const * const ib, cairo_t *cr,
              ColRowSelectionType const type,
              char const * const str, GocRect *rect)
 {
-       GtkStyleContext *ctxt = goc_item_get_style_context (GOC_ITEM (ib));
+       GtkStyleContext *ctxt = ib->styles[type];
 
        g_return_if_fail ((size_t)type < G_N_ELEMENTS (selection_type_flags));
 
        cairo_save (cr);
 
        gtk_style_context_save (ctxt);
-       gtk_style_context_set_state (ctxt, selection_type_flags[type]);
+       //gtk_style_context_set_state (ctxt, selection_type_flags[type]);
        gtk_render_background (ctxt, cr, rect->x, rect->y,
                               rect->width + 1, rect->height + 1);
 
@@ -334,6 +350,7 @@ ib_draw_cell (GnmItemBar const * const ib, cairo_t *cr,
                PangoFont *font = ib->selection_fonts[type];
                int ascent = ib->selection_font_ascents[type];
                int w, h;
+               GdkRGBA c;
 
                g_return_if_fail (font != NULL);
                g_object_unref (ib->pango.item->analysis.font);
@@ -351,14 +368,8 @@ ib_draw_cell (GnmItemBar const * const ib, cairo_t *cr,
                                 rect->width - 2, rect->height - 2);
                cairo_clip (cr);
 
-               if (1) {
-                       GdkRGBA c;
-
-                       gnm_style_context_get_color (ctxt, selection_type_flags[type], &c);
-                       gdk_cairo_set_source_rgba (cr, &c);
-               } else {
-                       gdk_cairo_set_source_rgba (cr, &ib->selection_colors[type]);
-               }
+               gtk_style_context_get_color (ctxt, selection_type_flags[type], &c);
+               gdk_cairo_set_source_rgba (cr, &c);
 
                cairo_translate (cr,
                                 rect->x + ib->padding.left +
@@ -1150,6 +1161,7 @@ static void
 item_bar_dispose (GObject *obj)
 {
        GnmItemBar *ib = GNM_ITEM_BAR (obj);
+       unsigned ui;
 
        ib_dispose_fonts (ib);
 
@@ -1166,6 +1178,8 @@ item_bar_dispose (GObject *obj)
                pango_item_free (ib->pango.item);
                ib->pango.item = NULL;
        }
+       for (ui = 0; ui < G_N_ELEMENTS(ib->styles); ui++)
+               g_clear_object (&ib->styles[ui]);
 
        G_OBJECT_CLASS (parent_class)->dispose (obj);
 }
@@ -1190,11 +1204,6 @@ gnm_item_bar_init (GnmItemBar *ib)
        ib->has_resize_guides = FALSE;
        ib->pango.item = NULL;
        ib->pango.glyphs = pango_glyph_string_new ();
-
-       /* Style-wise we are a button.  */
-       gtk_style_context_add_class
-               (goc_item_get_style_context (GOC_ITEM (ib)),
-                GTK_STYLE_CLASS_BUTTON);
 }
 
 static void


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