Exemplo n.º 1
0
        public PString GetString(string key)
        {
            var result = Get <PString> (key);

            if (result == null)
            {
                this[key] = result = new PString("");
            }
            return(result);
        }
Exemplo n.º 2
0
        public void SetString(string key, string value)
        {
            var result = Get <PString> (key);

            if (result == null)
            {
                this[key] = result = new PString(value);
                return;
            }
            result.Value = value;
            OnChanged(EventArgs.Empty);
        }
Exemplo n.º 3
0
        public CustomPropertiesWidget(PListScheme scheme)
        {
            this.scheme = scheme = scheme ?? PListScheme.Empty;
            treeview    = new PopupTreeView(this);
            treeview.HeadersClickable = true;
            this.PackStart(treeview, true, true, 0);
            ShowAll();

            var keyRenderer = new CellRendererCombo();

            keyRenderer.Editable   = true;
            keyRenderer.Model      = keyStore;
            keyRenderer.Mode       = CellRendererMode.Editable;
            keyRenderer.TextColumn = 0;
            keyRenderer.Edited    += delegate(object o, EditedArgs args) {
                TreeIter selIter;
                if (!treeStore.GetIterFromString(out selIter, args.Path))
                {
                    return;
                }
                if (args.NewText == (string)treeStore.GetValue(selIter, 0))
                {
                    return;
                }

                var obj = (PObject)treeStore.GetValue(selIter, 1);

                var dict = obj.Parent as PDictionary;
                if (dict == null)
                {
                    return;
                }

                var key    = scheme.Keys.FirstOrDefault(k => k.Identifier == args.NewText || k.Description == args.NewText);
                var newKey = key != null ? key.Identifier : args.NewText;

                dict.ChangeKey(obj, newKey, key == null || obj.TypeString == key.Type ? null : CreateNewObject(key.Type));
            };
            var col = new TreeViewColumn();

            col.Resizable = true;
            col.Title     = GettextCatalog.GetString("Property");
            col.PackStart(keyRenderer, true);
            col.SetCellDataFunc(keyRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                var renderer = (CellRendererCombo)cell;
                string id    = (string)tree_model.GetValue(iter, 0) ?? "";
                var obj      = (PObject)tree_model.GetValue(iter, 1);
                if (obj == null)
                {
                    renderer.Text      = id;
                    renderer.Editable  = false;
                    renderer.Sensitive = false;
                    return;
                }

                var key            = scheme.GetKey(id);
                renderer.Editable  = !(obj.Parent is PArray);
                renderer.Sensitive = true;
                renderer.Text      = key != null && ShowDescriptions ? GettextCatalog.GetString(key.Description) : id;
            });
            treeview.AppendColumn(col);

            var iconSize = IconSize.Menu;

            col = new TreeViewColumn {
                MinWidth = 25, Resizable = true, Sizing = Gtk.TreeViewColumnSizing.Autosize
            };

            var removeRenderer = new CellRendererButton(ImageService.GetPixbuf("gtk-remove", IconSize.Menu));

            removeRenderer.Clicked += delegate {
                TreeIter iter;
                bool     hasSelection = treeview.Selection.GetSelected(out iter);
                PObject  obj          = null;
                if (hasSelection)
                {
                    obj = (PObject)treeStore.GetValue(iter, 1);
                    obj.Remove();
                }
            };
            col.PackEnd(removeRenderer, false);
            col.SetCellDataFunc(removeRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                removeRenderer.Visible = treeview.Selection.IterIsSelected(iter) && !AddKeyNode.Equals(treeStore.GetValue(iter, 0));
            });

            var addRenderer = new CellRendererButton(ImageService.GetPixbuf("gtk-add", IconSize.Menu));

            addRenderer.Clicked += delegate {
                Gtk.TreeIter iter = Gtk.TreeIter.Zero;
                if (!treeview.Selection.GetSelected(out iter))
                {
                    return;
                }

                PObject obj = null;
                if (treeStore.IterParent(out iter, iter))
                {
                    obj = (PObject)treeStore.GetValue(iter, 1);
                }
                obj = obj ?? nsDictionary;

                var newObj = new PString("");
                if (obj is PArray)
                {
                    var arr = (PArray)obj;
                    arr.Add(newObj);
                }
                else if (obj is PDictionary)
                {
                    string name = "newNode";
                    var    dict = (PDictionary)obj;
                    while (dict.ContainsKey(name))
                    {
                        name += "_";
                    }
                    dict.Add(name, newObj);
                }
                else
                {
                    return;
                }
            };

            col.PackEnd(addRenderer, false);
            col.SetCellDataFunc(addRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                addRenderer.Visible = treeview.Selection.IterIsSelected(iter) && AddKeyNode.Equals(treeStore.GetValue(iter, 0));
            });
            treeview.AppendColumn(col);

            treeview.RowExpanded += delegate(object o, RowExpandedArgs args) {
                var obj = (PObject)treeStore.GetValue(args.Iter, 1);
                expandedObjects.Add(obj);
            };
            treeview.RowCollapsed += delegate(object o, RowCollapsedArgs args) {
                var obj = (PObject)treeStore.GetValue(args.Iter, 1);
                expandedObjects.Remove(obj);
            };
            var comboRenderer = new CellRendererCombo();

            var typeModel = new ListStore(typeof(string));

            typeModel.AppendValues("Array");
            typeModel.AppendValues("Dictionary");
            typeModel.AppendValues("Boolean");
            typeModel.AppendValues("Data");
            typeModel.AppendValues("Date");
            typeModel.AppendValues("Number");
            typeModel.AppendValues("String");
            comboRenderer.Model      = typeModel;
            comboRenderer.Mode       = CellRendererMode.Editable;
            comboRenderer.HasEntry   = false;
            comboRenderer.TextColumn = 0;
            comboRenderer.Edited    += delegate(object o, EditedArgs args) {
                TreeIter selIter;
                if (!treeStore.GetIterFromString(out selIter, args.Path))
                {
                    return;
                }

                PObject oldObj = (PObject)treeStore.GetValue(selIter, 1);
                if (oldObj != null && oldObj.TypeString != args.NewText)
                {
                    oldObj.Replace(CreateNewObject(args.NewText));
                }
            };

            treeview.AppendColumn(GettextCatalog.GetString("Type"), comboRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                var renderer = (CellRendererCombo)cell;
                string id    = (string)tree_model.GetValue(iter, 0) ?? "";
                var key      = scheme.GetKey(id);
                var obj      = (PObject)tree_model.GetValue(iter, 1);
                if (obj == null)
                {
                    renderer.Editable = false;
                    renderer.Text     = "";
                    return;
                }
                renderer.Editable      = key == null;
                renderer.ForegroundGdk = Style.Text(renderer.Editable ? StateType.Normal : StateType.Insensitive);
                renderer.Text          = obj.TypeString;
            });

            var propRenderer = new CellRendererCombo();

            propRenderer.Model           = valueStore;
            propRenderer.Mode            = CellRendererMode.Editable;
            propRenderer.TextColumn      = 0;
            propRenderer.EditingStarted += delegate(object o, EditingStartedArgs args) {
                valueStore.Clear();
                if (Scheme == null)
                {
                    return;
                }
                TreeIter iter;
                if (!treeStore.GetIterFromString(out iter, args.Path))
                {
                    return;
                }
                var pObject = (PObject)treeStore.GetValue(iter, 1);
                if (pObject == null)
                {
                    return;
                }
                var key = Parent != null?Scheme.GetKey(pObject.Parent.Key) : null;

                if (key != null)
                {
                    var descr = new List <string> (key.Values.Select(v => v.Description));
                    descr.Sort();
                    foreach (var val in descr)
                    {
                        valueStore.AppendValues(val);
                    }
                }
            };

            propRenderer.Edited += delegate(object o, EditedArgs args) {
                TreeIter iter;
                if (!treeStore.GetIterFromString(out iter, args.Path))
                {
                    return;
                }
                var pObject = (PObject)treeStore.GetValue(iter, 1);
                if (pObject == null)
                {
                    return;
                }
                string newText = args.NewText;
                var    key     = Parent != null?Scheme.GetKey(pObject.Parent.Key) : null;

                if (key != null)
                {
                    foreach (var val in key.Values)
                    {
                        if (newText == val.Description)
                        {
                            newText = val.Identifier;
                            break;
                        }
                    }
                }
                pObject.SetValue(newText);
            };

            /*		propRenderer.EditingStarted += delegate(object o, EditingStartedArgs args) {
             *                      TreeIter iter;
             *                      if (!treeStore.GetIterFromString (out iter, args.Path))
             *                              return;
             *                      PObject obj = (PObject)treeStore.GetValue (iter, 1);
             *                      if (obj is PBoolean) {
             *                              ((PBoolean)obj).Value = !((PBoolean)obj).Value;
             *                              propRenderer.StopEditing (false);
             *                      }
             *              };*/


            treeview.AppendColumn(GettextCatalog.GetString("Value"), propRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                var renderer = (CellRendererCombo)cell;
                var obj      = (PObject)tree_model.GetValue(iter, 1);
                if (obj == null)
                {
                    renderer.Editable = false;
                    renderer.Text     = "";
                    return;
                }
                renderer.Editable = !(obj is PArray || obj is PDictionary || obj is PData);
                obj.RenderValue(this, renderer);
            });
            treeview.EnableGridLines = TreeViewGridLines.Horizontal;
            treeview.Model           = treeStore;
        }
Exemplo n.º 4
0
        public StringListWidget()
        {
            treeview = new Gtk.TreeView();
            treeview.HeadersVisible = false;
            PackStart(treeview, true, true, 0);
            ShowAll();

            var col = new TreeViewColumn {
                MinWidth = CellRendererAddRemove.MinWidth, Resizable = true, Sizing = Gtk.TreeViewColumnSizing.Autosize
            };
            var addRemoveRenderer = new CellRendererAddRemove();

            col.PackStart(addRemoveRenderer, false);
            col.SetCellDataFunc(addRemoveRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                if (treeStore.GetValue(iter, (int)ListColumn.Object) != null)
                {
                    addRemoveRenderer.Action = CellRendererAddRemove.AddRemoveAction.Remove;
                }
                else
                {
                    addRemoveRenderer.Action = CellRendererAddRemove.AddRemoveAction.Add;
                }
            });
            addRemoveRenderer.RemoveClicked += delegate {
                TreeIter iter;
                bool     hasSelection = treeview.Selection.GetSelected(out iter);
                PObject  obj          = null;
                if (hasSelection)
                {
                    obj = (PObject)treeStore.GetValue(iter, (int)ListColumn.Object);
                    treeStore.Remove(ref iter);

                    editing = true;
                    obj.Remove();
                    editing = false;
                }
            };
            addRemoveRenderer.AddClicked += delegate {
                PObject  obj = new PString(string.Empty);
                TreeIter iter;

                bool hasSelection = treeview.Selection.GetSelected(out iter);
                if (hasSelection)
                {
                    treeStore.SetValues(iter, string.Empty, obj);
                    AppendCreateNewEntry();

                    editing = true;
                    array.Add(obj);
                    editing = false;
                }
            };
            treeview.AppendColumn(col);

            var valueRenderer = new CellRendererCombo();

            valueRenderer.Mode       = CellRendererMode.Editable;
            valueRenderer.TextColumn = (int)ListColumn.DisplayValue;

            valueRenderer.Edited += delegate(object o, EditedArgs args) {
                TreeIter iter;

                if (!treeStore.GetIterFromString(out iter, args.Path))
                {
                    return;
                }

                var pObject = (PObject)treeStore.GetValue(iter, (int)ListColumn.Object);
                if (pObject == null)
                {
                    return;
                }

                string newValue = !string.IsNullOrEmpty(ValuePrefix) ? ValuePrefix + args.NewText : args.NewText;

                pObject.SetValue(newValue);
            };

            treeview.AppendColumn(GettextCatalog.GetString("Value"), valueRenderer, delegate(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
                var text     = (string)tree_model.GetValue(iter, (int)ListColumn.DisplayValue);
                var obj      = (PObject)tree_model.GetValue(iter, (int)ListColumn.Object);
                var renderer = (CellRendererCombo)cell;

                renderer.Sensitive = obj != null;
                renderer.Editable  = obj != null;
                renderer.Text      = text;
            });

            treeview.EnableGridLines = TreeViewGridLines.Horizontal;
            treeview.Model           = treeStore;
        }
Exemplo n.º 5
0
            void ShowPopup(Gdk.EventButton evnt)
            {
                Gtk.TreeIter iter;
                bool         hasSelection = Selection.GetSelected(out iter);
                PObject      obj          = null;

                if (hasSelection)
                {
                    obj = (PObject)widget.treeStore.GetValue(iter, 1);
                }
                else
                {
                    return;
                }

                var menu   = new Gtk.Menu();
                var newKey = new Gtk.MenuItem(GettextCatalog.GetString("New key"));

                menu.Append(newKey);

                newKey.Activated += delegate(object sender, EventArgs e) {
                    var newObj = new PString("");

                    PObject parent;
                    if (obj != null)
                    {
                        parent = obj.Parent;
                    }
                    else
                    {
                        Gtk.TreeIter parentIter;
                        if (widget.treeStore.IterParent(out parentIter, iter))
                        {
                            parent = (PObject)widget.treeStore.GetValue(parentIter, 1);
                        }
                        else
                        {
                            parent = widget.nsDictionary;
                        }
                    }

                    if (parent is PArray)
                    {
                        var arr = (PArray)parent;
                        arr.Add(newObj);
                        return;
                    }

                    var dict = parent as PDictionary;
                    if (dict == null)
                    {
                        return;
                    }

                    string name = "newNode";
                    while (dict.ContainsKey(name))
                    {
                        name += "_";
                    }
                    dict[name] = newObj;
                };

                if (hasSelection && obj != null)
                {
                    var removeKey = new Gtk.MenuItem(GettextCatalog.GetString("Remove key"));
                    menu.Append(removeKey);
                    removeKey.Activated += delegate(object sender, EventArgs e) {
                        //the change event handler removes it from the store
                        obj.Remove();
                    };
                }

                if (widget.scheme != null)
                {
                    menu.Append(new Gtk.SeparatorMenuItem());
                    var showDescItem = new Gtk.CheckMenuItem(GettextCatalog.GetString("Show descriptions"));
                    showDescItem.Active     = widget.ShowDescriptions;
                    showDescItem.Activated += delegate {
                        widget.ShowDescriptions = !widget.ShowDescriptions;
                    };
                    menu.Append(showDescItem);
                }
                menu.ShowAll();
                IdeApp.CommandService.ShowContextMenu(this, evnt, menu, this);
            }