[gnome-clocks/wip/geoclue] Mark up automatic location



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

    Mark up automatic location

 src/alarm.vala   |    1 +
 src/widgets.vala |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 src/world.vala   |    8 ++++-
 3 files changed, 78 insertions(+), 5 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;
         });
     }
 
diff --git a/src/world.vala b/src/world.vala
index 5ea52a9..0057967 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -24,6 +24,7 @@ private class Item : Object, ContentItem {
     private static Gdk.Pixbuf? night_pixbuf = Utils.load_image ("night.png");
 
     public GWeather.Location location { get; set; }
+    public bool automatic { get; set; default = false; }
     public string name {
         get {
             // We store it in a _name member even if we overwrite it every time
@@ -41,6 +42,7 @@ private class Item : Object, ContentItem {
         }
     }
 
+
     public string city_name {
         owned get {
             return location.get_city_name ();
@@ -343,7 +345,9 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
     private void save () {
         var builder = new GLib.VariantBuilder (new VariantType ("aa{sv}"));
         foreach (Item i in locations) {
-            i.serialize (builder);
+            if (!i.automatic) {
+                i.serialize (builder);
+            }
         }
         settings.set_value ("world-clocks", builder.end ());
     }
@@ -368,10 +372,10 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
             
                 if (not_included) {
                     var item = new Item (found_location);
+                    item.automatic = true;
 
                     locations.append (item);
                     content_view.add_item (item);
-                    //save ();
                 }
             }
         });


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