[dconf] editor: Support enumerations, fix bug where editing one value modifies another



commit ccfc1f19cfb56e66791c244f6acb6816a2820d77
Author: Robert Ancell <robert ancell gmail com>
Date:   Wed Jul 7 15:35:42 2010 +1000

    editor: Support enumerations, fix bug where editing one value modifies another

 editor/dconf-editor.vala |   30 ++---
 editor/dconf-model.vala  |  167 ++++++++++++++++++++++++++---
 editor/dconf-schema.vala |  107 ++++++++++++++++--
 editor/dconf-view.vala   |  268 +++++++++++++++++++++++++++-------------------
 4 files changed, 417 insertions(+), 155 deletions(-)
---
diff --git a/editor/dconf-editor.vala b/editor/dconf-editor.vala
index e52057a..0f91d14 100644
--- a/editor/dconf-editor.vala
+++ b/editor/dconf-editor.vala
@@ -1,7 +1,6 @@
 public class EditorWindow : Gtk.Window
 {
     public SettingsModel model;
-    public SchemaList schemas;
 
     private Gtk.TreeView dir_tree_view;
     private Gtk.TreeView key_tree_view;
@@ -22,13 +21,6 @@ public class EditorWindow : Gtk.Window
         add(hbox);
 
         model = new SettingsModel();
-        schemas = new SchemaList();
-        try
-        {
-            schemas.load_directory("/usr/share/glib-2.0/schemas");
-        } catch (Error e) {
-            warning("Failed to parse schemas: %s", e.message);
-        }
 
         Gtk.ScrolledWindow scroll = new Gtk.ScrolledWindow(null, null);
         scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
@@ -83,6 +75,7 @@ public class EditorWindow : Gtk.Window
 
         value_label = new Gtk.Label("");
         value_label.set_alignment(0.0f, 0.5f);
+        value_label.wrap = true;
         table.attach_defaults(value_label, 1, 2, row, row+1);
         
         name_label.show();
@@ -90,7 +83,7 @@ public class EditorWindow : Gtk.Window
 
         return value_label;
     }
-    
+
     private void dir_selected_cb()
     {
         KeyModel? key_model = null;
@@ -129,6 +122,8 @@ public class EditorWindow : Gtk.Window
            return "Boolean";
         case "s":
            return "String";
+        case "enum":
+           return "Enumeration";
         default:
            return type;
         }
@@ -141,16 +136,15 @@ public class EditorWindow : Gtk.Window
 
         if (key != null)
         {
-            SchemaKey? schema = schemas.keys.get(key.full_name);
-            if (schema != null)
+            if (key.schema != null)
             {
-                schema_name = schema.schema.id;
-                if (schema.summary != null)
-                    summary = schema.summary;
-                if (schema.description != null)
-                    description = schema.description;
-                type = type_to_description(schema.type);
-                default_value = schema.default_value;
+                schema_name = key.schema.schema.id;
+                if (key.schema.summary != null)
+                    summary = key.schema.summary;
+                if (key.schema.description != null)
+                    description = key.schema.description;
+                type = type_to_description(key.schema.type);
+                default_value = key.schema.default_value;
             }
             else
             {
diff --git a/editor/dconf-model.vala b/editor/dconf-model.vala
index f17daff..53024a4 100644
--- a/editor/dconf-model.vala
+++ b/editor/dconf-model.vala
@@ -1,13 +1,15 @@
 public class Key : GLib.Object
 {
-    private DConf.Client client;
+    private SettingsModel model;
 
     public string name;
     public string full_name;
 
     public Key? next;
-    
-    private Variant _value;
+
+    public SchemaKey? schema;
+
+    private Variant? _value;
     public Variant value
     {
         get { update_value(); return _value; }
@@ -15,7 +17,7 @@ public class Key : GLib.Object
             _value = value;
             try
             {
-                client.write(full_name, value, 0, null);
+                model.client.write(full_name, value, 0, null);
             }
             catch (GLib.Error e)
             {
@@ -23,22 +25,23 @@ public class Key : GLib.Object
         }
     }
 
-    public Key(DConf.Client client, string name, string full_name)
+    public Key(SettingsModel model, string name, string full_name)
     {
-        this.client = client;
+        this.model = model;
         this.name = name;
         this.full_name = full_name;
+        this.schema = model.schemas.keys.get(full_name);
     }
-    
+
     private void update_value()
     {
-        _value = client.read(full_name);
+        _value = model.client.read(full_name);
     }
 }
 
 public class Directory : GLib.Object
 {
-    private DConf.Client client;
+    private SettingsModel model;
 
     public string name;
     public string full_name;
@@ -81,9 +84,9 @@ public class Directory : GLib.Object
 
     private bool have_children;
     
-    public Directory(DConf.Client client, string name, string full_name, Directory? parent = null)
+    public Directory(SettingsModel model, string name, string full_name, Directory? parent = null)
     {
-        this.client = client;
+        this.model = model;
         this.parent = parent;
         this.name = name;
         this.full_name = full_name;
@@ -97,7 +100,7 @@ public class Directory : GLib.Object
 
         Directory? last_directory = null;
         Key? last_key = null;
-        string[] items = client.list(full_name);
+        string[] items = model.client.list(full_name);
         _n_children = 0;
         _n_keys = 0;
         for (int i = 0; i < items.length; i++)
@@ -108,7 +111,7 @@ public class Directory : GLib.Object
             {
                 string dir_name = items[i][0:-1];
 
-                Directory directory = new Directory(client, dir_name, item_name, this);
+                Directory directory = new Directory(model, dir_name, item_name, this);
                 if (last_directory == null)
                    child = directory;
                 else
@@ -118,7 +121,7 @@ public class Directory : GLib.Object
             }
             else
             {
-                Key key = new Key(client, items[i], item_name);
+                Key key = new Key(model, items[i], item_name);
                 if (last_key == null)
                     keys = key;
                 else
@@ -269,9 +272,133 @@ public class KeyModel: GLib.Object, Gtk.TreeModel/*, Gtk.TreeSortable*/
     }
 }
 
+public class EnumModel: GLib.Object, Gtk.TreeModel
+{
+    private SchemaEnum schema_enum;
+
+    construct {}
+
+    public EnumModel(SchemaEnum schema_enum)
+    {
+        this.schema_enum = schema_enum;
+    }
+
+    public Gtk.TreeModelFlags get_flags()
+    {
+        return Gtk.TreeModelFlags.LIST_ONLY;
+    }
+
+    public int get_n_columns()
+    {
+        return 2;
+    }
+
+    public Type get_column_type(int index)
+    {
+        if (index == 0)
+            return typeof(string);
+        else
+            return typeof(int);
+    }
+    
+    private void set_iter(out Gtk.TreeIter iter, SchemaEnumValue value)
+    {
+        iter.stamp = 0;
+        iter.user_data = value;
+        iter.user_data2 = value;
+        iter.user_data3 = value;
+    }
+
+    public SchemaEnumValue get_enum_value(Gtk.TreeIter iter)
+    {
+        return (SchemaEnumValue)iter.user_data;
+    }
+
+    public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path)
+    {
+        if (path.get_depth() != 1)
+            return false;
+
+        return iter_nth_child(out iter, null, path.get_indices()[0]);
+    }
+
+    public Gtk.TreePath get_path(Gtk.TreeIter iter)
+    {
+        Gtk.TreePath path = new Gtk.TreePath();
+        path.append_index(get_enum_value(iter).index);
+        return path;
+    }
+
+    public void get_value(Gtk.TreeIter iter, int column, out Value value)
+    {
+        if (column == 0)
+            value = get_enum_value(iter).nick;
+        else if (column == 1)
+            value = get_enum_value(iter).value;
+    }
+
+    public bool iter_next(ref Gtk.TreeIter iter)
+    {
+        int index = get_enum_value(iter).index;
+        if (index >= schema_enum.values.size - 1)
+            return false;
+        set_iter(out iter, schema_enum.values[index + 1]);
+        return true;
+    }
+
+    public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent)
+    {
+        if (parent != null || schema_enum.values.size == 0)
+            return false;
+        set_iter(out iter, schema_enum.values[0]);
+        return true;
+    }
+
+    public bool iter_has_child(Gtk.TreeIter iter)
+    {
+        return false;
+    }
+
+    public int iter_n_children(Gtk.TreeIter? iter)
+    {
+        if (iter == null)
+            return schema_enum.values.size;
+        else
+            return 0;
+    }
+
+    public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n)
+    {
+        if (parent != null)
+            return false;
+
+        if (n >= schema_enum.values.size)
+            return false;
+        set_iter(out iter, schema_enum.values[n]);
+        return true;
+    }
+
+    public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child)
+    {
+        return false;
+    }
+
+    public void ref_node(Gtk.TreeIter iter)
+    {
+        get_enum_value(iter).ref();
+    }
+
+    public void unref_node(Gtk.TreeIter iter)
+    {
+        get_enum_value(iter).unref();
+    }
+}
+
 public class SettingsModel: GLib.Object, Gtk.TreeModel
 {
-    private DConf.Client client;
+    public SchemaList schemas;
+
+    public DConf.Client client;
     private Directory root;
 
     construct {}
@@ -279,7 +406,15 @@ public class SettingsModel: GLib.Object, Gtk.TreeModel
     public SettingsModel()
     {
         client = new DConf.Client("", true, null, null);
-        root = new Directory(client, "/", "/");
+        root = new Directory(this, "/", "/");
+
+        schemas = new SchemaList();
+        try
+        {
+            schemas.load_directory("/usr/share/glib-2.0/schemas");
+        } catch (Error e) {
+            warning("Failed to parse schemas: %s", e.message);
+        }
     }
 
     public Gtk.TreeModelFlags get_flags()
diff --git a/editor/dconf-schema.vala b/editor/dconf-schema.vala
index ee17e02..e94b9f9 100644
--- a/editor/dconf-schema.vala
+++ b/editor/dconf-schema.vala
@@ -6,6 +6,7 @@ public class SchemaKey
     public string name;
     public string type;
     public string default_value;
+    public string? enum_name;
     public string? summary;
     public string? description;
     public string? gettext_domain;
@@ -21,6 +22,11 @@ public class SchemaKey
                 name = prop->children->content;
             else if (prop->name == "type")
                 type = prop->children->content;
+            else if (prop->name == "enum")
+            {
+                type = "enum";
+                enum_name = prop->children->content;
+            }
             //else
             //    ?
         }
@@ -45,6 +51,75 @@ public class SchemaKey
     }
 }
 
+public class SchemaEnumValue : GLib.Object
+{
+    public SchemaEnum schema_enum;
+    public int index;
+    public string nick;
+    public int value;
+
+    public SchemaEnumValue(SchemaEnum schema_enum, int index, string nick, int value)
+    {
+        this.schema_enum = schema_enum;
+        this.index = index;
+        this.nick = nick;
+        this.value = value;
+    }
+}
+
+public class SchemaEnum
+{
+    public SchemaList list;
+    public string id;
+    public ArrayList<SchemaEnumValue> values = new ArrayList<SchemaEnumValue>();
+
+    public SchemaEnum(SchemaList list, Xml.Node* node)
+    {
+        this.list = list;
+
+        for (Xml.Attr* prop = node->properties; prop != null; prop = prop->next)
+        {
+            if (prop->name == "id")
+                id = prop->children->content;
+            //else
+            //    ?
+        }
+
+        //if (id = null)
+        //    ?
+
+        for (Xml.Node* child = node->children; child != null; child = child->next)
+        {
+            if (child->name == "value")
+            {
+                string? nick = null;
+                int value = -1;
+
+                for (Xml.Attr* prop = child->properties; prop != null; prop = prop->next)
+                {
+                    if (prop->name == "value")
+                        value = prop->children->content.to_int();
+                    else if (prop->name == "nick")
+                        nick = prop->children->content;
+                    //else
+                    //    ?
+                }
+
+                //if (value < 0 || nick == null)
+                //    ?
+
+                SchemaEnumValue schema_value = new SchemaEnumValue(this, values.size, nick, value);
+                values.add(schema_value);
+            }
+            //else
+            //   ?
+        }
+        
+        //if (default_value == null)
+        //    ?
+    }
+}
+
 public class Schema
 {
     public SchemaList list;
@@ -85,6 +160,7 @@ public class SchemaList
 {
     public ArrayList<Schema> schemas = new ArrayList<Schema>();
     public HashMap<string, SchemaKey> keys = new HashMap<string, SchemaKey>();
+    public HashMap<string, SchemaEnum> enums = new HashMap<string, SchemaEnum>();
 
     public void parse_file(string path)
     {
@@ -107,22 +183,29 @@ public class SchemaList
 
         for (Xml.Node* node = root->children; node != null; node = node->next)
         {
-            if (node->name != "schema")
-               continue;
-
-            Schema schema = new Schema(this, node, gettext_domain);
-            schemas.add(schema);
-            if (schema.path == null)
+            if (node->name == "schema")
             {
-                // FIXME: What to do here?
-                continue;
+                Schema schema = new Schema(this, node, gettext_domain);
+                schemas.add(schema);
+                if (schema.path == null)
+                {
+                    // FIXME: What to do here?
+                    continue;
+                }
+
+                foreach (var key in schema.keys.values)
+                {
+                    string full_name = schema.path + key.name;
+                    keys.set(full_name, key);
+                }
             }
-            
-            foreach (var key in schema.keys.values)
+            else if (node->name == "enum")
             {
-                string full_name = schema.path + key.name;
-                keys.set(full_name, key);
+                SchemaEnum enum = new SchemaEnum(this, node);
+                enums.set(enum.id, enum);
             }
+            //else
+            //    ?
         }
 
         delete doc;
diff --git a/editor/dconf-view.vala b/editor/dconf-view.vala
index 8ec36e8..4174b92 100644
--- a/editor/dconf-view.vala
+++ b/editor/dconf-view.vala
@@ -1,8 +1,10 @@
 private class KeyValueRenderer: Gtk.CellRenderer
 {
+    private DConfKeyView view;
     private Gtk.CellRendererText text_renderer;
+    private Gtk.CellRendererSpin spin_renderer;
     private Gtk.CellRendererToggle toggle_renderer;
-    private Gtk.CellEditable cell_editor;
+    private Gtk.CellRendererCombo combo_renderer;
 
     private Key _key;
     public Key key
@@ -11,41 +13,135 @@ private class KeyValueRenderer: Gtk.CellRenderer
         set
         {
             _key = value;
-            if (_key.value.is_of_type(VariantType.BOOLEAN))
+            if (key.schema != null && key.value.is_of_type(VariantType.STRING) && key.schema.type == "enum")
+            {
+                combo_renderer.text = key.value.get_string();
+                combo_renderer.model = new EnumModel(key.schema.schema.list.enums[key.schema.enum_name]);
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.BOOLEAN))
+            {
+                toggle_renderer.active = key.value.get_boolean();
                 mode = Gtk.CellRendererMode.ACTIVATABLE;
-            else if (_key.value.is_of_type(VariantType.STRING) ||
-                     _key.value.is_of_type(VariantType.BYTE) ||
-                     _key.value.is_of_type(VariantType.INT16) ||
-                     _key.value.is_of_type(VariantType.UINT16) ||
-                     _key.value.is_of_type(VariantType.INT32) ||
-                     _key.value.is_of_type(VariantType.UINT32) ||
-                     _key.value.is_of_type(VariantType.INT64) ||
-                     _key.value.is_of_type(VariantType.UINT64) ||
-                     _key.value.is_of_type(VariantType.DOUBLE))
+            }
+            else if (key.value.is_of_type(VariantType.STRING))
+            {
+                text_renderer.text = key.value.get_string();
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.BYTE))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_byte(), 0, 255, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.INT16))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_int16(), int16.MIN, int16.MAX, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.UINT16))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_uint16(), uint16.MIN, uint16.MAX, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.INT32))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_int32(), int32.MIN, int32.MAX, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.UINT32))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_uint32(), int32.MIN, uint32.MAX, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.INT64))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_int64(), int64.MIN, int64.MAX, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.UINT64))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_uint64(), uint64.MIN, uint64.MAX, 1, 0, 0);
+                spin_renderer.digits = 0;
+                mode = Gtk.CellRendererMode.EDITABLE;
+            }
+            else if (key.value.is_of_type(VariantType.DOUBLE))
+            {
+                spin_renderer.text = key.value.print(false);
+                spin_renderer.adjustment = new Gtk.Adjustment(key.value.get_double(), double.MIN, double.MAX, 1, 0, 0);
+                spin_renderer.digits = 6;
                 mode = Gtk.CellRendererMode.EDITABLE;
+            }
             else
+            {
+                text_renderer.text = key.value.print(false);            
                 mode = Gtk.CellRendererMode.INERT;
+            }
         }
     }
-    
+
     construct
     {
         text_renderer = new Gtk.CellRendererText();
+        text_renderer.editable = true;
+        text_renderer.edited.connect(text_edited_cb);
+
+        spin_renderer = new Gtk.CellRendererSpin();
+        spin_renderer.editable = true;
+        spin_renderer.edited.connect(spin_edited_cb);
+
         toggle_renderer = new Gtk.CellRendererToggle();
         toggle_renderer.xalign = 0f;
+        toggle_renderer.activatable = true;
+        toggle_renderer.toggled.connect(toggle_cb);
+
+        combo_renderer = new Gtk.CellRendererCombo();
+        combo_renderer.has_entry = false;
+        combo_renderer.text_column = 0;
+        combo_renderer.editable = true;
+        combo_renderer.edited.connect(text_edited_cb);
     }
 
-    private Gtk.CellRenderer get_renderer()
+    public KeyValueRenderer(DConfKeyView view)
     {
-        if (key.value.is_of_type(VariantType.BOOLEAN))
-        {
-            toggle_renderer.active = key.value.get_boolean();
-            return toggle_renderer;
-        }
-        else
+        this.view = view;
+    }
+
+    private Gtk.CellRenderer renderer
+    {
+        set {}
+        get
         {
-            text_renderer.text = key.value.print(false);
-            return text_renderer;
+            if (key.schema != null && key.schema.type == "enum")
+                return combo_renderer;
+            else if (key.value.is_of_type(VariantType.BOOLEAN))
+                return toggle_renderer;
+            else if (key.value.is_of_type(VariantType.STRING))
+                return text_renderer;
+            else if (key.value.is_of_type(VariantType.BYTE) ||
+                     key.value.is_of_type(VariantType.INT16) ||
+                     key.value.is_of_type(VariantType.UINT16) ||
+                     key.value.is_of_type(VariantType.INT32) ||
+                     key.value.is_of_type(VariantType.UINT32) ||
+                     key.value.is_of_type(VariantType.INT64) ||
+                     key.value.is_of_type(VariantType.UINT64) ||
+                     key.value.is_of_type(VariantType.DOUBLE))
+                return spin_renderer;
+            else
+                return text_renderer;            
         }
     }
 
@@ -56,7 +152,7 @@ private class KeyValueRenderer: Gtk.CellRenderer
                                   out int        width,
                                   out int        height)
     {
-        get_renderer().get_size(widget, cell_area, out x_offset, out y_offset, out width, out height);
+        renderer.get_size(widget, cell_area, out x_offset, out y_offset, out width, out height);
     }
 
     public override void render(Gdk.Window    window,
@@ -66,7 +162,7 @@ private class KeyValueRenderer: Gtk.CellRenderer
                                 Gdk.Rectangle expose_area,
                                 Gtk.CellRendererState flags)
     {
-        get_renderer().render(window, widget, background_area, cell_area, expose_area, flags);
+        renderer.render(window, widget, background_area, cell_area, expose_area, flags);
     }
 
     public override bool activate(Gdk.Event event,
@@ -76,41 +172,7 @@ private class KeyValueRenderer: Gtk.CellRenderer
                                   Gdk.Rectangle cell_area,
                                   Gtk.CellRendererState flags)
     {
-        key.value = new Variant.boolean(!key.value.get_boolean());
-        return true;
-    }
-    
-    private void editing_done_cb(Gtk.CellEditable cell_editable)
-    {
-        cell_editor = null;
-        // FIXME: Appears to be broken
-        /*if (cell_editable.editing_canceled)
-            return;*/
-
-        if (key.value.is_of_type(VariantType.STRING))
-        {
-            var entry = (Gtk.Entry)cell_editable;
-            key.value = new Variant.string(entry.get_text());
-            return;
-        }
-
-        var spin = (Gtk.SpinButton)cell_editable;
-        if (key.value.is_of_type(VariantType.BYTE))
-            key.value = new Variant.byte((uchar)spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.INT16))
-            key.value = new Variant.int16((int16)spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.UINT16))
-            key.value = new Variant.uint16((uint16)spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.INT32))
-            key.value = new Variant.int32(spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.UINT32))
-            key.value = new Variant.uint32(spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.INT64))
-            key.value = new Variant.int64(spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.UINT64))
-            key.value = new Variant.uint64(spin.get_value_as_int());
-        else if (key.value.is_of_type(VariantType.DOUBLE))
-            key.value = new Variant.double(spin.get_value());
+        return renderer.activate(event, widget, path, background_area, cell_area, flags);
     }
 
     public override unowned Gtk.CellEditable start_editing(Gdk.Event event,
@@ -120,63 +182,51 @@ private class KeyValueRenderer: Gtk.CellRenderer
                                                            Gdk.Rectangle cell_area,
                                                            Gtk.CellRendererState flags)
     {
-        if (key.value.is_of_type(VariantType.STRING))
-        {
-            var entry = new Gtk.Entry();
-            entry.set_text(_key.value.get_string());
-            cell_editor = entry;
-        }
-        else if (key.value.is_of_type(VariantType.BYTE))
-        {
-            var spin = new Gtk.SpinButton.with_range(0, 255, 1);
-            spin.set_value(key.value.get_byte());
-            cell_editor = spin;
-        }
+        return renderer.start_editing(event, widget, path, background_area, cell_area, flags);
+    }
+    
+    private Key get_key_from_path(string path)
+    {
+        Gtk.TreeIter iter;
+        view.model.get_iter_from_string(out iter, path);
+
+        Key key;
+        view.model.get(iter, 0, out key, -1);
+        
+        return key;
+    }
+
+    private void toggle_cb(Gtk.CellRendererToggle renderer, string path)
+    {
+        Key key = get_key_from_path(path);
+        key.value = new Variant.boolean(!key.value.get_boolean());
+    }
+
+    private void text_edited_cb(Gtk.CellRendererText renderer, string path, string text)
+    {
+        Key key = get_key_from_path(path);
+        key.value = new Variant.string(text);
+    }
+
+    private void spin_edited_cb(Gtk.CellRendererText renderer, string path, string text)
+    {
+        Key key = get_key_from_path(path);
+        if (key.value.is_of_type(VariantType.BYTE))
+            key.value = new Variant.byte((uchar)text.to_int());
         else if (key.value.is_of_type(VariantType.INT16))
-        {
-            var spin = new Gtk.SpinButton.with_range(int16.MIN, int16.MAX, 1);
-            spin.set_value(key.value.get_int16());
-            cell_editor = spin;
-        }
+            key.value = new Variant.int16((int16)text.to_int());
         else if (key.value.is_of_type(VariantType.UINT16))
-        {
-            var spin = new Gtk.SpinButton.with_range(0, uint16.MAX, 1);
-            spin.set_value(key.value.get_uint16());
-            cell_editor = spin;
-        }
+            key.value = new Variant.uint16((uint16)text.to_int());
         else if (key.value.is_of_type(VariantType.INT32))
-        {
-            var spin = new Gtk.SpinButton.with_range(int32.MIN, int32.MAX, 1);
-            spin.set_value(key.value.get_int32());
-            cell_editor = spin;
-        }
+            key.value = new Variant.int32(text.to_int());
         else if (key.value.is_of_type(VariantType.UINT32))
-        {
-            var spin = new Gtk.SpinButton.with_range(0, uint32.MAX, 1);
-            spin.set_value(key.value.get_uint32());
-            cell_editor = spin;
-        }
+            key.value = new Variant.uint32(text.to_int());
         else if (key.value.is_of_type(VariantType.INT64))
-        {
-            var spin = new Gtk.SpinButton.with_range(int64.MIN, int64.MAX, 1);
-            spin.set_value(key.value.get_int64());
-            cell_editor = spin;
-        }
+            key.value = new Variant.int64(text.to_int());
         else if (key.value.is_of_type(VariantType.UINT64))
-        {
-            var spin = new Gtk.SpinButton.with_range(0, uint64.MAX, 1);
-            spin.set_value(key.value.get_uint64());
-            cell_editor = spin;
-        }
+            key.value = new Variant.uint64(text.to_int());
         else if (key.value.is_of_type(VariantType.DOUBLE))
-        {
-            var spin = new Gtk.SpinButton.with_range(double.MIN, double.MAX, 1);
-            spin.set_value(key.value.get_uint64());
-            cell_editor = spin;
-        }
-        cell_editor.editing_done.connect(editing_done_cb);
-        cell_editor.show();
-        return cell_editor;
+            key.value = new Variant.double(text.to_double());
     }
 }
 
@@ -196,6 +246,6 @@ public class DConfKeyView : Gtk.TreeView
         var column = new Gtk.TreeViewColumn.with_attributes("Name", new Gtk.CellRendererText(), "text", 1, null);
         /*column.set_sort_column_id(1);*/
         append_column(column);
-        insert_column_with_attributes(-1, "Value", new KeyValueRenderer(), "key", 0, null);
+        insert_column_with_attributes(-1, "Value", new KeyValueRenderer(this), "key", 0, null);
     }
 }



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