2
// ProjectSelectorWidget.cs
5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using MonoDevelop.Projects;
30
using System.Collections.Generic;
32
namespace MonoDevelop.Ide.Gui.Components
34
[System.ComponentModel.ToolboxItem(true)]
35
public partial class ProjectSelectorWidget : Gtk.Bin
39
IBuildTarget rootItem;
40
IBuildTarget currentSelection;
41
HashSet<IBuildTarget> activeItems = new HashSet<IBuildTarget> ();
42
HashSet<Type> selectableTypes = new HashSet<Type> ();
44
public event EventHandler SelectionChanged;
45
public event EventHandler ActiveChanged;
47
public ProjectSelectorWidget ()
51
store = new TreeStore (typeof(string), typeof(string), typeof(object), typeof(bool), typeof(bool));
54
tree.HeadersVisible = false;
55
TreeViewColumn col = new TreeViewColumn ();
56
Gtk.CellRendererToggle ctog = new CellRendererToggle ();
57
ctog.Toggled += OnToggled;
58
col.PackStart (ctog, false);
59
Gtk.CellRendererPixbuf cr = new Gtk.CellRendererPixbuf();
60
col.PackStart (cr, false);
61
Gtk.CellRendererText crt = new Gtk.CellRendererText();
62
crt.Mode &= ~CellRendererMode.Activatable;
63
col.PackStart (crt, true);
64
col.AddAttribute (cr, "stock-id", 0);
65
col.AddAttribute (crt, "markup", 1);
66
col.AddAttribute (ctog, "active", 3);
67
col.AddAttribute (ctog, "visible", 4);
68
tree.AppendColumn (col);
70
tree.Selection.Changed += HandleTreeSelectionChanged;
73
void HandleTreeSelectionChanged (object sender, EventArgs e)
76
if (tree.Selection.GetSelected (out it))
77
currentSelection = (IBuildTarget) store.GetValue (it, 2);
79
currentSelection = null;
81
if (SelectionChanged != null)
82
SelectionChanged (this, EventArgs.Empty);
85
public IBuildTarget SelectedItem {
87
if (currentSelection != null && selectableTypes.Count > 0 && !selectableTypes.Any (t => t.IsAssignableFrom (currentSelection.GetType ())))
90
return currentSelection;
93
currentSelection = value;
94
SetSelection (currentSelection, null);
98
public IEnumerable<IBuildTarget> ActiveItems {
103
activeItems = new HashSet<IBuildTarget> ();
104
activeItems.UnionWith (value);
105
SetSelection (currentSelection, activeItems);
109
public IEnumerable<Type> SelectableItemTypes {
111
return selectableTypes;
114
selectableTypes = new HashSet<Type> ();
115
selectableTypes.UnionWith (value);
120
public bool ShowCheckboxes {
121
get { return showCheckboxes; }
122
set { showCheckboxes = value; Fill (); }
125
public bool CascadeCheckboxSelection { get; set; }
127
public IBuildTarget RootItem {
129
return this.rootItem;
139
IBuildTarget sel = SelectedItem;
141
if (rootItem is RootWorkspace) {
142
foreach (var item in ((RootWorkspace)rootItem).Items)
143
AddEntry (TreeIter.Zero, item);
146
else if (rootItem != null) {
147
AddEntry (TreeIter.Zero, rootItem);
152
void AddEntry (TreeIter iter, IBuildTarget item)
154
if (!IsVisible (item))
158
if (item is Solution)
159
icon = MonoDevelop.Ide.Gui.Stock.Solution;
160
else if (item is SolutionFolder)
161
icon = MonoDevelop.Ide.Gui.Stock.SolutionFolderClosed;
162
else if (item is WorkspaceItem)
163
icon = MonoDevelop.Ide.Gui.Stock.Workspace;
164
else if (item is Project)
165
icon = ((Project)item).StockIcon;
167
icon = MonoDevelop.Ide.Gui.Stock.Project;
169
bool checkVisible = IsCheckboxVisible (item);
170
bool selected = activeItems.Contains (item);
172
if (!iter.Equals (TreeIter.Zero))
173
iter = store.AppendValues (iter, icon, item.Name, item, selected && checkVisible, checkVisible);
175
iter = store.AppendValues (icon, item.Name, item, selected && checkVisible, checkVisible);
178
tree.ExpandToPath (store.GetPath (iter));
180
foreach (IBuildTarget ce in GetChildren (item))
184
void SetSelection (IBuildTarget selected, HashSet<IBuildTarget> active)
187
if (store.GetIterFirst (out it))
188
SetSelection (it, selected, active);
191
bool SetSelection (TreeIter it, IBuildTarget selected, HashSet<IBuildTarget> active)
194
IBuildTarget item = (IBuildTarget) store.GetValue (it, 2);
195
if (selected != null && item == selected) {
196
tree.Selection.SelectIter (it);
197
tree.ExpandToPath (store.GetPath (it));
198
tree.ScrollToCell (store.GetPath (it), tree.Columns[0], false, 0, 0);
202
bool val = (bool) store.GetValue (it, 3);
203
bool newVal = active != null ? active.Contains (item) : val;
205
store.SetValue (it, 3, newVal);
208
if (store.IterChildren (out ci, it)) {
209
if (SetSelection (ci, selected, active))
213
} while (store.IterNext (ref it));
218
void OnToggled (object sender, Gtk.ToggledArgs args)
221
store.GetIterFromString (out iter, args.Path);
222
IBuildTarget ob = (IBuildTarget) store.GetValue (iter, 2);
223
if (activeItems.Contains (ob)) {
224
activeItems.Remove (ob);
225
if (CascadeCheckboxSelection) {
226
foreach (var i in GetAllChildren (ob))
227
activeItems.Remove (i);
228
SetSelection (iter, null, new HashSet<IBuildTarget> ());
230
store.SetValue (iter, 3, false);
233
activeItems.Add (ob);
234
if (CascadeCheckboxSelection) {
235
foreach (var i in GetAllChildren (ob))
237
SetSelection (iter, null, activeItems);
240
store.SetValue (iter, 3, true);
243
if (ActiveChanged != null)
244
ActiveChanged (this, EventArgs.Empty);
247
IEnumerable<IBuildTarget> GetAllChildren (IBuildTarget item)
249
IEnumerable<IBuildTarget> res = GetChildren (item);
250
return res.Concat (res.SelectMany (i => GetAllChildren (i)));
253
IEnumerable<IBuildTarget> GetChildren (IBuildTarget item)
255
if (item is SolutionFolder) {
256
return ((SolutionFolder)item).Items;
257
} else if (item is Solution) {
258
return ((Solution)item).RootFolder.Items;
259
} else if (item is Workspace) {
260
return ((Workspace)item).Items;
262
return new IBuildTarget [0];
265
protected bool IsVisible (IBuildTarget item)
270
protected bool IsCheckboxVisible (IBuildTarget item)
274
if (selectableTypes.Count > 0)
275
return selectableTypes.Any (t => t.IsAssignableFrom (item.GetType ()));