[Xfce4-commits] <postler:master> Implement toggle renderer with prelight for 'starring'

Christian Dywan noreply at xfce.org
Sat Nov 6 20:12:02 CET 2010


Updating branch refs/heads/master
         to 5b684840187cc322bfb2b48671f66370dd109fc5 (commit)
       from 2ce52fc0b55423d2f9e6e4392be4fdc6a52097d9 (commit)

commit 5b684840187cc322bfb2b48671f66370dd109fc5
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sat Nov 6 20:08:05 2010 +0100

    Implement toggle renderer with prelight for 'starring'
    
    The cell renderer has a boolean state and can display a
    different image on hover.
    If 'starred' and 'not-starred' icons are available, the
    'not-starred' icon appear on hover of a message and 'starred'
    if the message has the important flag.

 postler/postler-app.vala                |    4 ++
 postler/postler-cellrenderertoggle.vala |   54 +++++++++++++++++++++++++++++++
 postler/postler-messages.vala           |   49 ++++++++++++++++++++++++---
 3 files changed, 101 insertions(+), 6 deletions(-)

diff --git a/postler/postler-app.vala b/postler/postler-app.vala
index 91726aa..219d69c 100644
--- a/postler/postler-app.vala
+++ b/postler/postler-app.vala
@@ -15,6 +15,8 @@ namespace Postler {
     const string STOCK_ARCHIVE = "gnome-mime-application-x-archive";
     const string STOCK_EMBLEM_DRAFT = "emblem-draft";
     const string STOCK_EMBLEM_IMPORTANT = "emblem-important";
+    const string STOCK_STARRED = "starred";
+    const string STOCK_NOT_STARRED = "not-starred";
     const string STOCK_FACE_SMILE_BIG = "face-smile-big";
     const string STOCK_FACE_SAD = "face-sad";
     const string STOCK_FACE_WINK = "face-wink";
@@ -57,6 +59,8 @@ public class Postler.App : Unique.App {
         { STOCK_ARCHIVE, N_("Archi_ve") },
         { STOCK_EMBLEM_DRAFT },
         { STOCK_EMBLEM_IMPORTANT },
+        { STOCK_STARRED },
+        { STOCK_NOT_STARRED },
         { STOCK_FACE_SMILE_BIG },
         { STOCK_FACE_SAD },
         { STOCK_FACE_WINK },
diff --git a/postler/postler-cellrenderertoggle.vala b/postler/postler-cellrenderertoggle.vala
new file mode 100644
index 0000000..597b3d5
--- /dev/null
+++ b/postler/postler-cellrenderertoggle.vala
@@ -0,0 +1,54 @@
+/*
+ Copyright (C) 2010 Christian Dywan <christian at twotoasts.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+public class Postler.CellRendererToggle : Gtk.CellRendererToggle {
+    public string? stock_id { get; set; }
+    public string? prelight_stock_id { get; set; }
+
+    public CellRendererToggle () {
+    }
+    public override void get_size (Gtk.Widget widget, Gdk.Rectangle? cell_area,
+        out int x_offset, out int y_offset, out int width, out int height) {
+        if (&x_offset != null) x_offset = 0;
+        if (&y_offset != null) y_offset = cell_area.height / 3;
+        if (&width != null && &height != null)
+            Gtk.icon_size_lookup_for_settings (widget.get_settings (),
+                Gtk.IconSize.MENU, out width, out height);
+    }
+    public override void render (Gdk.Window window, Gtk.Widget widget,
+        Gdk.Rectangle background_area, Gdk.Rectangle cell_area,
+        Gdk.Rectangle expose_area, Gtk.CellRendererState flags) {
+
+        var context = Gdk.cairo_create (window);
+        Gdk.cairo_rectangle (context, expose_area);
+        Gdk.cairo_rectangle (context, background_area);
+
+        int x, y;
+        Gdk.Rectangle draw_rect = Gdk.Rectangle ();
+        Gdk.Pixbuf? icon = null;
+
+        window.get_pointer (out x, out y, null);
+        if (expose_area.intersect (cell_area, draw_rect)
+         && (flags & Gtk.CellRendererState.PRELIT) != 0
+         && prelight_stock_id != null) {
+            icon = widget.render_icon (prelight_stock_id, Gtk.IconSize.MENU, null);
+        }
+        else if (stock_id != null) {
+            icon = widget.render_icon (stock_id, Gtk.IconSize.MENU, null);
+        }
+        if (icon != null) {
+            Gdk.cairo_set_source_pixbuf (context, icon,
+                cell_area.x, cell_area.y + cell_area.height / 3);
+            context.fill ();
+        }
+    }
+}
+
diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index e43a601..76d5919 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -88,6 +88,26 @@ public class Postler.Messages : Gtk.TreeView {
         return escaped.str;
     }
 
+    void render_flag (Gtk.CellLayout layout, Gtk.CellRenderer cell,
+        Gtk.TreeModel model, Gtk.TreeIter iter) {
+        bool flagged;
+        var screen = get_screen ();
+        unowned string stock_id = null;
+        unowned string prelight_stock_id = null;
+
+        model.get (iter, Columns.FLAGGED, out flagged);
+        if (flagged) {
+            if (Gtk.IconTheme.get_for_screen (screen).has_icon (STOCK_STARRED))
+                stock_id = STOCK_STARRED;
+            else
+                stock_id = STOCK_EMBLEM_IMPORTANT;
+        } else {
+            if (Gtk.IconTheme.get_for_screen (screen).has_icon (STOCK_NOT_STARRED))
+                prelight_stock_id = STOCK_NOT_STARRED;
+        }
+        cell.set ("stock-id", stock_id, "prelight-stock-id", prelight_stock_id);
+    }
+
     void render_subject (Gtk.CellLayout layout, Gtk.CellRenderer cell,
         Gtk.TreeModel model, Gtk.TreeIter iter) {
         string charset, subject;
@@ -159,9 +179,20 @@ public class Postler.Messages : Gtk.TreeView {
         }
     }
 
+    void renderer_flag_toggled (Gtk.CellRendererToggle renderer,
+                                string                 path) {
+        Gtk.TreeIter sort_iter = Gtk.TreeIter ();
+        if (!sort.get_iter_from_string (out sort_iter, path))
+            return;
+
+        string location;
+        sort.get (sort_iter, Columns.LOCATION, out location);
+        toggle_message_flag (sort_iter, ref location, 'F');
+    }
+
     public Messages (Accounts accounts) {
         this.accounts = accounts;
-        store = new Gtk.TreeStore (9, typeof (string), typeof (string),
+        store = new Gtk.TreeStore (9, typeof (bool), typeof (string),
             typeof (string), typeof (string), typeof (int), typeof (string),
             typeof (int64), typeof (string), typeof (ulong));
         sort = new Gtk.TreeModelSort.with_model (store);
@@ -169,11 +200,17 @@ public class Postler.Messages : Gtk.TreeView {
         set_search_equal_func (search_inline);
         get_selection ().set_mode (Gtk.SelectionMode.MULTIPLE);
         get_selection ().changed.connect (selection_changed);
-        insert_column_with_attributes (-1, _("Flagged"),
-            new Gtk.CellRendererPixbuf (), "stock-id", Columns.FLAGGED);
+        var column = new Gtk.TreeViewColumn ();
+        column.set_title (_("Flagged"));
+        var renderer_flag = new Postler.CellRendererToggle ();
+        column.pack_start (renderer_flag, true);
+        column.set_cell_data_func (renderer_flag, render_flag);
+        column.add_attribute (renderer_flag, "active", Columns.FLAGGED);
+        renderer_flag.toggled.connect (renderer_flag_toggled);
+        insert_column (column, -1);
         insert_column_with_attributes (-1, _("Status"),
             new Gtk.CellRendererPixbuf (), "stock-id", Columns.STATUS);
-        var column = new Gtk.TreeViewColumn ();
+        column = new Gtk.TreeViewColumn ();
         column.set_title (_("Subject"));
         var renderer_text = new Gtk.CellRendererText ();
         column.pack_start (renderer_text, true);
@@ -564,7 +601,7 @@ public class Postler.Messages : Gtk.TreeView {
                         attachment = STOCK_MAIL_ATTACHMENT;
 
                     store.insert_with_values (out account_iter, null, -1,
-                        Columns.FLAGGED, flagged,
+                        Columns.FLAGGED, flagged != null ? true : false,
                         Columns.STATUS, status,
                         Columns.ATTACHMENT, attachment,
                         Columns.SUBJECT, subject != null ? subject : _("No subject"),
@@ -634,7 +671,7 @@ public class Postler.Messages : Gtk.TreeView {
             Gtk.TreeIter child_iter;
             sort.convert_iter_to_child_iter (out child_iter, sort_iter);
             store.set (child_iter,
-                       Columns.FLAGGED, flagged,
+                       Columns.FLAGGED, flagged != null ? true : false,
                        Columns.STATUS, status,
                        Columns.LOCATION, location,
                        Columns.WEIGHT, font_weight);



More information about the Xfce4-commits mailing list