~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/MonoDevelop.MacDev.PlistEditor/StringListWidget.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// StringListWidget.cs
3
 
//  
4
 
// Author: Jeffrey Stedfast <jeff@xamarin.com>
5
 
// 
6
 
// Copyright (c) 2012 Xamarin Inc.
7
 
// 
8
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
 
// of this software and associated documentation files (the "Software"), to deal
10
 
// in the Software without restriction, including without limitation the rights
11
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 
// copies of the Software, and to permit persons to whom the Software is
13
 
// furnished to do so, subject to the following conditions:
14
 
// 
15
 
// The above copyright notice and this permission notice shall be included in
16
 
// all copies or substantial portions of the Software.
17
 
// 
18
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 
// THE SOFTWARE.
25
 
 
26
 
using System;
27
 
using System.Linq;
28
 
using System.Collections.Generic;
29
 
 
30
 
using Gtk;
31
 
using MonoMac.Foundation;
32
 
 
33
 
using Mono.TextEditor;
34
 
using MonoDevelop.Ide;
35
 
using MonoDevelop.Core;
36
 
using MonoDevelop.Components.Commands;
37
 
 
38
 
namespace MonoDevelop.MacDev.PlistEditor
39
 
{
40
 
        [System.ComponentModel.ToolboxItem(true)]
41
 
        public class StringListWidget : VBox, IRawPListDisplayWidget
42
 
        {
43
 
                const string AddNewEntry = "Add new entry";
44
 
                
45
 
                enum ListColumn {
46
 
                        DisplayValue,
47
 
                        Object,
48
 
                }
49
 
                
50
 
                TreeStore treeStore = new TreeStore (typeof (string), typeof (PString));
51
 
                bool editing = false;
52
 
                TreeView treeview;
53
 
                PArray array;
54
 
                
55
 
                class CellRendererAddRemove : CellRenderer
56
 
                {
57
 
                        static Gdk.Pixbuf RemoveIcon = ImageService.GetPixbuf ("gtk-remove", IconSize.Menu);
58
 
                        static Gdk.Pixbuf AddIcon = ImageService.GetPixbuf ("gtk-add", IconSize.Menu);
59
 
                        public static int MinWidth = Math.Max (AddIcon.Width, RemoveIcon.Width);
60
 
                        
61
 
                        public enum AddRemoveAction {
62
 
                                Add,
63
 
                                Remove,
64
 
                        }
65
 
                        
66
 
                        public AddRemoveAction Action {
67
 
                                get; set;
68
 
                        }
69
 
                        
70
 
                        public CellRendererAddRemove ()
71
 
                        {
72
 
                                Mode = CellRendererMode.Editable;
73
 
                        }
74
 
                        
75
 
                        protected virtual void OnClicked (EventArgs e)
76
 
                        {
77
 
                                EventHandler handler = Action == AddRemoveAction.Add ? AddClicked : RemoveClicked;
78
 
                                
79
 
                                if (handler != null)
80
 
                                        handler (this, e);
81
 
                        }
82
 
                        
83
 
                        public event EventHandler RemoveClicked;
84
 
                        public event EventHandler AddClicked;
85
 
                        
86
 
                        public override void GetSize (Widget widget, ref Gdk.Rectangle cell_area, out int x_offset, out int y_offset, out int width, out int height)
87
 
                        {
88
 
                                var pixbuf = Action == AddRemoveAction.Add ? AddIcon : RemoveIcon;
89
 
                                
90
 
                                x_offset = 0;
91
 
                                y_offset = 0;
92
 
                                
93
 
                                width = pixbuf.Width;
94
 
                                height = pixbuf.Width;
95
 
                        }
96
 
                        
97
 
                        public override CellEditable StartEditing (Gdk.Event evnt, Widget widget, string path, Gdk.Rectangle background_area, Gdk.Rectangle cell_area, CellRendererState flags)
98
 
                        {
99
 
                                if (evnt.Type == Gdk.EventType.ButtonPress)
100
 
                                        OnClicked (EventArgs.Empty);
101
 
                                
102
 
                                return base.StartEditing (evnt, widget, path, background_area, cell_area, flags);
103
 
                        }
104
 
                        
105
 
                        protected override void Render (Gdk.Drawable window, Widget widget, Gdk.Rectangle background_area, Gdk.Rectangle cell_area, Gdk.Rectangle expose_area, CellRendererState flags)
106
 
                        {
107
 
                                if (!Visible)
108
 
                                        return;
109
 
                                
110
 
                                var pixbuf = Action == AddRemoveAction.Add ? AddIcon : RemoveIcon;
111
 
                                int x = cell_area.X;
112
 
                                int y = cell_area.Y;
113
 
                                
114
 
                                window.DrawPixbuf (widget.Style.BaseGC (StateType.Normal), pixbuf, 0, 0, x, y, pixbuf.Width, pixbuf.Height, Gdk.RgbDither.None, 0, 0);
115
 
                        }
116
 
                }
117
 
                
118
 
                public StringListWidget ()
119
 
                {
120
 
                        treeview = new Gtk.TreeView ();
121
 
                        treeview.HeadersVisible = false;
122
 
                        PackStart (treeview, true, true, 0);
123
 
                        ShowAll ();
124
 
                        
125
 
                        var col = new TreeViewColumn { MinWidth = CellRendererAddRemove.MinWidth, Resizable = true, Sizing = Gtk.TreeViewColumnSizing.Autosize };
126
 
                        var addRemoveRenderer = new CellRendererAddRemove ();
127
 
                        col.PackStart (addRemoveRenderer, false);
128
 
                        col.SetCellDataFunc (addRemoveRenderer, delegate (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
129
 
                                if (treeStore.GetValue (iter, (int) ListColumn.Object) != null)
130
 
                                        addRemoveRenderer.Action = CellRendererAddRemove.AddRemoveAction.Remove;
131
 
                                else
132
 
                                        addRemoveRenderer.Action = CellRendererAddRemove.AddRemoveAction.Add;
133
 
                        });
134
 
                        addRemoveRenderer.RemoveClicked += delegate {
135
 
                                TreeIter iter;
136
 
                                bool hasSelection = treeview.Selection.GetSelected (out iter);
137
 
                                PObject obj = null;
138
 
                                if (hasSelection) {
139
 
                                        obj = (PObject) treeStore.GetValue (iter, (int) ListColumn.Object);
140
 
                                        treeStore.Remove (ref iter);
141
 
 
142
 
                                        editing = true;
143
 
                                        obj.Remove ();
144
 
                                        editing = false;
145
 
                                }
146
 
                        };
147
 
                        addRemoveRenderer.AddClicked += delegate {
148
 
                                PObject obj = new PString (string.Empty);
149
 
                                TreeIter iter;
150
 
 
151
 
                                bool hasSelection = treeview.Selection.GetSelected (out iter);
152
 
                                if (hasSelection) {
153
 
                                        treeStore.SetValues (iter, string.Empty, obj);
154
 
                                        AppendCreateNewEntry ();
155
 
 
156
 
                                        editing = true;
157
 
                                        array.Add (obj);
158
 
                                        editing = false;
159
 
                                }
160
 
                        };
161
 
                        treeview.AppendColumn (col);
162
 
                        
163
 
                        var valueRenderer = new CellRendererCombo ();
164
 
                        valueRenderer.Mode = CellRendererMode.Editable;
165
 
                        valueRenderer.TextColumn = (int) ListColumn.DisplayValue;
166
 
                        
167
 
                        valueRenderer.Edited += delegate (object o, EditedArgs args) {
168
 
                                TreeIter iter;
169
 
                                
170
 
                                if (!treeStore.GetIterFromString (out iter, args.Path))
171
 
                                        return;
172
 
                                
173
 
                                var pObject = (PObject) treeStore.GetValue (iter, (int) ListColumn.Object);
174
 
                                if (pObject == null)
175
 
                                        return;
176
 
                                
177
 
                                string newValue = !string.IsNullOrEmpty (ValuePrefix) ? ValuePrefix + args.NewText : args.NewText;
178
 
                                
179
 
                                pObject.SetValue (newValue);
180
 
                        };
181
 
                        
182
 
                        treeview.AppendColumn (GettextCatalog.GetString ("Value"), valueRenderer, delegate (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) {
183
 
                                var text     = (string) tree_model.GetValue (iter, (int) ListColumn.DisplayValue);
184
 
                                var obj      = (PObject) tree_model.GetValue (iter, (int) ListColumn.Object);
185
 
                                var renderer = (CellRendererCombo) cell;
186
 
                                
187
 
                                renderer.Sensitive = obj != null;
188
 
                                renderer.Editable = obj != null;
189
 
                                renderer.Text = text;
190
 
                        });
191
 
                        
192
 
                        treeview.EnableGridLines = TreeViewGridLines.Horizontal;
193
 
                        treeview.Model = treeStore;
194
 
                }
195
 
                
196
 
                public string ValuePrefix {
197
 
                        get; set;
198
 
                }
199
 
                
200
 
                public bool ShowDescriptions {
201
 
                        get { return false; }
202
 
                }
203
 
                
204
 
                public PListScheme Scheme {
205
 
                        get { return null; }
206
 
                }
207
 
                
208
 
                public void SetPListContainer (PObjectContainer container)
209
 
                {
210
 
                        if (!(container is PArray))
211
 
                                throw new ArgumentException ("The PList container must be a PArray.", "container");
212
 
                        
213
 
                        array = (PArray) container;
214
 
                        array.Changed += OnArrayChanged;
215
 
                        RefreshList ();
216
 
                }
217
 
 
218
 
                void OnArrayChanged (object sender, EventArgs e)
219
 
                {
220
 
                        if (editing)
221
 
                                return;
222
 
 
223
 
                        RefreshList ();
224
 
                        QueueDraw ();
225
 
                }
226
 
 
227
 
                void AppendCreateNewEntry ()
228
 
                {
229
 
                        treeStore.AppendValues (GettextCatalog.GetString (AddNewEntry), null);
230
 
                }
231
 
 
232
 
                void Rebuild (object sender, EventArgs e)
233
 
                {
234
 
                        RefreshList ();
235
 
                }
236
 
                
237
 
                void RefreshList ()
238
 
                {
239
 
                        treeStore.Clear ();
240
 
                        
241
 
                        for (int i = 0; i < array.Count; i++) {
242
 
                                var item = array[i];
243
 
                                var str = item as PString;
244
 
                                
245
 
                                if (str == null)
246
 
                                        continue;
247
 
                                
248
 
                                string value = str.Value;
249
 
                                if (!string.IsNullOrEmpty (ValuePrefix) && value.StartsWith (ValuePrefix))
250
 
                                        value = value.Substring (ValuePrefix.Length);
251
 
                                
252
 
                                treeStore.AppendValues (value, item);
253
 
                        }
254
 
                        
255
 
                        AppendCreateNewEntry ();
256
 
                }
257
 
        }
258
 
}