[gnome-clocks/wip/geolocation] Mark up automatic location 2



commit 9c31e28ac420e3822ca6e72f66c894486f89cf2d
Author: Evgeny Bobkin <evgen ibqn gmail com>
Date:   Mon Aug 26 22:10:29 2013 +0200

    Mark up automatic location 2

 src/alarm.vala   |    1 +
 src/widgets.vala |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 72 insertions(+), 3 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index 0e24233..037957f 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -31,6 +31,7 @@ private class Item : Object, ContentItem {
         SNOOZING
     }
 
+    public bool automatic { get; set; default = false; }
     public string name {
         get {
             return _name;
diff --git a/src/widgets.vala b/src/widgets.vala
index 663fe9f..26350da 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -56,6 +56,72 @@ public class HeaderBar : Gtk.HeaderBar {
     }
 }
 
+private class CustomTitleRenderer : Gtk.CellRendererText {
+    private int ICON_XOFF;
+    private int ICON_YOFF;
+    private int ICON_SIZE;
+
+    public string title {
+        get {
+            return _title;
+        }
+        set {
+            markup = _title = value;
+        }
+    }
+
+    public bool automatic { get; set; default = false; }
+    private string _title;
+
+    public CustomTitleRenderer () {
+        ICON_YOFF = 5;
+        ICON_XOFF = 25;
+        ICON_SIZE = 18;
+    }
+
+    public override void render (Cairo.Context cr, Gtk.Widget widget, Gdk.Rectangle background_area, 
Gdk.Rectangle cell_area, Gtk.CellRendererState flags) {
+        base.render (cr, widget, cell_area, cell_area, flags);
+
+        if (automatic) {
+            var context = widget.get_style_context ();
+            context.save ();
+
+            cr.save ();
+            Gdk.cairo_rectangle (cr, cell_area);
+            cr.clip ();
+
+            cr.translate (cell_area.x, cell_area.y);
+
+            // create the layouts so that we can measure them
+            var layout = widget.create_pango_layout ("");
+            layout.set_markup (title, -1);
+            layout.set_alignment (Pango.Alignment.CENTER);
+            int text_w, text_h;
+            layout.get_pixel_size (out text_w, out text_h);
+
+            int x = (cell_area.width - text_w) / 2 - ICON_XOFF, y = ICON_YOFF;
+
+            if (widget.get_direction () == Gtk.TextDirection.RTL) {
+                x = (cell_area.width + text_w) / 2 + ICON_XOFF - ICON_SIZE;
+            }
+
+            Gtk.IconTheme icon_theme = Gtk.IconTheme.get_for_screen (Gdk.Screen.get_default ());
+                   try {
+                           Gtk.IconInfo? icon_info = icon_theme.lookup_icon 
("find-location-symbolic",ICON_SIZE, 0);
+                           assert (icon_info != null);
+
+                           Gdk.Pixbuf pixbuf = icon_info.load_icon ();
+                           context.render_icon (cr, pixbuf, x, y);
+                   } catch (Error e) {
+                           warning (e.message);
+                   }
+
+            context.restore ();
+            cr.restore ();
+        }
+    }
+}
+
 private class DigitalClockRenderer : Gtk.CellRendererPixbuf {
     public const int TILE_SIZE = 256;
     public const int CHECK_ICON_SIZE = 40;
@@ -175,6 +241,7 @@ private class DigitalClockRenderer : Gtk.CellRendererPixbuf {
 
 public interface ContentItem : GLib.Object {
     public abstract string name { get; set; }
+    public abstract bool automatic { get; set; default = false; }
     public abstract void get_thumb_properties (out string text, out string subtext, out Gdk.Pixbuf? pixbuf, 
out string css_class);
 }
 
@@ -244,7 +311,7 @@ private class IconView : Gtk.IconView {
             renderer.css_class = css_class;
         });
 
-        var text_renderer = new Gtk.CellRendererText ();
+        var text_renderer = new CustomTitleRenderer ();
         text_renderer.set_alignment (0.5f, 0.5f);
         text_renderer.set_fixed_size (tile_width, -1);
         text_renderer.alignment = Pango.Alignment.CENTER;
@@ -254,8 +321,9 @@ private class IconView : Gtk.IconView {
         set_cell_data_func (text_renderer, (column, cell, model, iter) => {
             ContentItem item;
             model.get (iter, IconView.Column.ITEM, out item);
-            var renderer = (Gtk.CellRendererText) cell;
-            renderer.markup = GLib.Markup.escape_text (item.name);
+            var renderer = (CustomTitleRenderer) cell;
+            renderer.title = GLib.Markup.escape_text (item.name);
+            renderer.automatic = item.automatic;
         });
     }
 


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