2
* Toolbox.cs - A toolbox widget
5
* Michael Hutchinson <m.j.hutchinson@gmail.com>
7
* Copyright (C) 2005 Michael Hutchinson
9
* This sourcecode is licenced under The MIT License:
11
* Permission is hereby granted, free of charge, to any person obtaining
12
* a copy of this software and associated documentation files (the
13
* "Software"), to deal in the Software without restriction, including
14
* without limitation the rights to use, copy, modify, merge, publish,
15
* distribute, sublicense, and/or sell copies of the Software, and to permit
16
* persons to whom the Software is furnished to do so, subject to the
17
* following conditions:
19
* The above copyright notice and this permission notice shall be included in
20
* all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
25
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
28
* USE OR OTHER DEALINGS IN THE SOFTWARE.
2
* Toolbox.cs - A toolbox widget
5
* Michael Hutchinson <m.j.hutchinson@gmail.com>
7
* Copyright (C) 2005 Michael Hutchinson
9
* This sourcecode is licenced under The MIT License:
11
* Permission is hereby granted, free of charge, to any person obtaining
12
* a copy of this software and associated documentation files (the
13
* "Software"), to deal in the Software without restriction, including
14
* without limitation the rights to use, copy, modify, merge, publish,
15
* distribute, sublicense, and/or sell copies of the Software, and to permit
16
* persons to whom the Software is furnished to do so, subject to the
17
* following conditions:
19
* The above copyright notice and this permission notice shall be included in
20
* all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
25
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
28
* USE OR OTHER DEALINGS IN THE SOFTWARE.
33
using System.Collections.Generic;
33
using System.Collections.Generic;
35
using System.Drawing.Design;
35
using System.Drawing.Design;
36
36
using MonoDevelop.Core;
37
37
using MonoDevelop.Ide.Gui;
38
using MonoDevelop.Components.Commands;
39
using MonoDevelop.Components.Docking;
40
using MonoDevelop.Ide;
41
using Mono.TextEditor;
43
namespace MonoDevelop.DesignerSupport.Toolbox
38
using MonoDevelop.Components.Commands;
39
using MonoDevelop.Components.Docking;
40
using MonoDevelop.Ide;
41
using Mono.TextEditor;
42
using MonoDevelop.Components;
44
namespace MonoDevelop.DesignerSupport.Toolbox
45
46
public class Toolbox : VBox, IPropertyPadProvider, IToolboxConfiguration
52
53
ToolboxWidget toolboxWidget;
53
54
ScrolledWindow scrolledWindow;
55
ToggleButton filterToggleButton;
56
56
ToggleButton catToggleButton;
57
57
ToggleButton compactModeToggleButton;
60
MonoDevelop.Ide.Gui.PadFontChanger fontChanger;
62
Dictionary<string,int> categoryPriorities = new Dictionary<string, int> ();
63
Button toolboxAddButton;
58
SearchEntry filterEntry;
59
MonoDevelop.Ide.Gui.PadFontChanger fontChanger;
61
Dictionary<string,int> categoryPriorities = new Dictionary<string, int> ();
62
Button toolboxAddButton;
65
public Toolbox (ToolboxService toolboxService, IPadWindow container)
67
this.toolboxService = toolboxService;
64
public Toolbox (ToolboxService toolboxService, IPadWindow container)
66
this.toolboxService = toolboxService;
68
67
this.container = container;
71
70
DockItemToolbar toolbar = container.GetToolbar (PositionType.Top);
73
filterToggleButton = new ToggleButton ();
74
filterToggleButton.Image = new Image (Gtk.Stock.Find, IconSize.Menu);
75
filterToggleButton.Toggled += new EventHandler (toggleFiltering);
76
filterToggleButton.TooltipText = GettextCatalog.GetString ("Show search box");
77
toolbar.Add (filterToggleButton);
72
filterEntry = new SearchEntry();
73
filterEntry.Ready = true;
74
filterEntry.HasFrame = true;
75
filterEntry.WidthRequest = 150;
76
filterEntry.Changed += new EventHandler (filterTextChanged);
79
toolbar.Add (filterEntry, true);
79
81
catToggleButton = new ToggleButton ();
80
catToggleButton.Image = new Image (ImageService.GetPixbuf ("md-design-categorise", IconSize.Menu));
81
catToggleButton.Toggled += new EventHandler (toggleCategorisation);
82
catToggleButton.TooltipText = GettextCatalog.GetString ("Show categories");
82
catToggleButton.Image = new Image (ImageService.GetPixbuf ("md-design-categorise", IconSize.Menu));
83
catToggleButton.Toggled += new EventHandler (toggleCategorisation);
84
catToggleButton.TooltipText = GettextCatalog.GetString ("Show categories");
83
85
toolbar.Add (catToggleButton);
85
87
compactModeToggleButton = new ToggleButton ();
86
compactModeToggleButton.Image = new Image (ImageService.GetPixbuf ("md-design-listboxtoggle", IconSize.Menu));
87
compactModeToggleButton.Toggled += new EventHandler (ToggleCompactMode);
88
compactModeToggleButton.Image = new Image (ImageService.GetPixbuf ("md-design-listboxtoggle", IconSize.Menu));
89
compactModeToggleButton.Toggled += new EventHandler (ToggleCompactMode);
88
90
compactModeToggleButton.TooltipText = GettextCatalog.GetString ("Use compact display");
89
91
toolbar.Add (compactModeToggleButton);
91
VSeparator sep = new VSeparator ();
94
toolboxAddButton = new Button (new Gtk.Image (Gtk.Stock.Add, IconSize.Menu));
95
toolbar.Add (toolboxAddButton);
96
toolboxAddButton.TooltipText = GettextCatalog.GetString ("Add toolbox items");
97
toolboxAddButton.Clicked += new EventHandler (toolboxAddButton_Clicked);
93
toolboxAddButton = new Button (new Gtk.Image (Gtk.Stock.Add, IconSize.Menu));
94
toolbar.Add (toolboxAddButton);
95
toolboxAddButton.TooltipText = GettextCatalog.GetString ("Add toolbox items");
96
toolboxAddButton.Clicked += new EventHandler (toolboxAddButton_Clicked);
98
97
toolbar.ShowAll ();
100
filterEntry = new Entry();
101
filterEntry.WidthRequest = 150;
102
filterEntry.Changed += new EventHandler (filterTextChanged);
106
101
toolboxWidget = new ToolboxWidget ();
107
102
toolboxWidget.SelectedItemChanged += delegate {
108
selectedNode = this.toolboxWidget.SelectedItem != null ? this.toolboxWidget.SelectedItem.Tag as ItemToolboxNode : null;
103
selectedNode = this.toolboxWidget.SelectedItem != null ? this.toolboxWidget.SelectedItem.Tag as ItemToolboxNode : null;
109
104
toolboxService.SelectItem (selectedNode);
111
this.toolboxWidget.DragBegin += delegate(object sender, Gtk.DragBeginArgs e) {
113
if (this.toolboxWidget.SelectedItem != null) {
114
this.toolboxWidget.HideTooltipWindow ();
115
toolboxService.DragSelectedItem (this.toolboxWidget, e.Context);
118
this.toolboxWidget.ActivateSelectedItem += delegate {
119
toolboxService.UseSelectedItem ();
122
fontChanger = new MonoDevelop.Ide.Gui.PadFontChanger (toolboxWidget, toolboxWidget.SetCustomFont, toolboxWidget.QueueResize);
106
this.toolboxWidget.DragBegin += delegate(object sender, Gtk.DragBeginArgs e) {
108
if (this.toolboxWidget.SelectedItem != null) {
109
this.toolboxWidget.HideTooltipWindow ();
110
toolboxService.DragSelectedItem (this.toolboxWidget, e.Context);
113
this.toolboxWidget.ActivateSelectedItem += delegate {
114
toolboxService.UseSelectedItem ();
117
fontChanger = new MonoDevelop.Ide.Gui.PadFontChanger (toolboxWidget, toolboxWidget.SetCustomFont, toolboxWidget.QueueResize);
124
119
this.toolboxWidget.DoPopupMenu = ShowPopup;
126
121
scrolledWindow = new MonoDevelop.Components.CompactScrolledWindow ();
127
122
base.PackEnd (scrolledWindow, true, true, 0);
128
base.FocusChain = new Gtk.Widget [] { scrolledWindow };
123
base.FocusChain = new Gtk.Widget [] { scrolledWindow };
130
125
//Initialise self
131
scrolledWindow.ShadowType = ShadowType.None;
132
scrolledWindow.VscrollbarPolicy = PolicyType.Automatic;
126
scrolledWindow.ShadowType = ShadowType.None;
127
scrolledWindow.VscrollbarPolicy = PolicyType.Automatic;
133
128
scrolledWindow.HscrollbarPolicy = PolicyType.Never;
134
129
scrolledWindow.WidthRequest = 150;
135
130
scrolledWindow.Add (this.toolboxWidget);
142
137
//set initial state
143
filterToggleButton.Active = false;
144
138
this.toolboxWidget.ShowCategories = catToggleButton.Active = true;
145
compactModeToggleButton.Active = MonoDevelop.Core.PropertyService.Get ("ToolboxIsInCompactMode", false);
139
compactModeToggleButton.Active = MonoDevelop.Core.PropertyService.Get ("ToolboxIsInCompactMode", false);
146
140
this.toolboxWidget.IsListMode = !compactModeToggleButton.Active;
150
144
#region Toolbar event handlers
152
146
void ToggleCompactMode (object sender, EventArgs e)
154
this.toolboxWidget.IsListMode = !compactModeToggleButton.Active;
148
this.toolboxWidget.IsListMode = !compactModeToggleButton.Active;
155
149
MonoDevelop.Core.PropertyService.Set ("ToolboxIsInCompactMode", compactModeToggleButton.Active);
158
void toggleFiltering (object sender, EventArgs e)
161
filterEntry.Text = "";
162
base.Remove (filterEntry);
164
base.PackStart (filterEntry, false, false, 4);
166
filterEntry.GrabFocus ();
168
filterVisible = !filterVisible;
171
152
void toggleCategorisation (object sender, EventArgs e)
176
157
void filterTextChanged (object sender, EventArgs e)
178
foreach (Category cat in toolboxWidget.Categories) {
179
bool hasVisibleChild = false;
180
foreach (Item child in cat.Items) {
181
child.IsVisible = ((ItemToolboxNode) child.Tag).Filter (filterEntry.Text);
182
hasVisibleChild |= child.IsVisible;
184
cat.IsVisible = hasVisibleChild;
186
toolboxWidget.QueueDraw ();
159
foreach (Category cat in toolboxWidget.Categories) {
160
bool hasVisibleChild = false;
161
foreach (Item child in cat.Items) {
162
child.IsVisible = ((ItemToolboxNode) child.Tag).Filter (filterEntry.Entry.Text);
163
hasVisibleChild |= child.IsVisible;
165
cat.IsVisible = hasVisibleChild;
167
toolboxWidget.QueueDraw ();
168
toolboxWidget.QueueResize ();
189
171
void toolboxAddButton_Clicked (object sender, EventArgs e)
202
184
[CommandHandler (MonoDevelop.Ide.Commands.EditCommands.Delete)]
203
185
internal void OnDeleteItem ()
205
if (MessageService.Confirm (GettextCatalog.GetString ("Are you sure you want to remove the selected Item?"), AlertButton.Delete))
187
if (MessageService.Confirm (GettextCatalog.GetString ("Are you sure you want to remove the selected Item?"), AlertButton.Delete))
206
188
toolboxService.RemoveUserItem (selectedNode);
209
191
[CommandUpdateHandler (MonoDevelop.Ide.Commands.EditCommands.Delete)]
210
192
internal void OnUpdateDeleteItem (CommandInfo info)
212
// Hack manually filter out gtk# widgets & container since they cannot be re added
213
// because they're missing the toolbox attributes.
214
info.Enabled = selectedNode != null
215
&& (selectedNode.ItemDomain != GtkWidgetDomain
216
|| (selectedNode.Category != "Widgets" && selectedNode.Category != "Container"));
194
// Hack manually filter out gtk# widgets & container since they cannot be re added
195
// because they're missing the toolbox attributes.
196
info.Enabled = selectedNode != null
197
&& (selectedNode.ItemDomain != GtkWidgetDomain
198
|| (selectedNode.Category != "Widgets" && selectedNode.Category != "Container"));
219
201
static readonly string GtkWidgetDomain = GettextCatalog.GetString ("GTK# Widgets");
223
#region GUI population
225
Dictionary<string, Category> categories = new Dictionary<string, Category> ();
226
void AddItems (IEnumerable<ItemToolboxNode> nodes)
205
#region GUI population
207
Dictionary<string, Category> categories = new Dictionary<string, Category> ();
208
void AddItems (IEnumerable<ItemToolboxNode> nodes)
228
210
foreach (ItemToolboxNode itbn in nodes) {
229
Item newItem = new Item (itbn.Icon, itbn.Name, String.IsNullOrEmpty (itbn.Description) ? itbn.Name : itbn.Description, itbn);
230
if (!categories.ContainsKey (itbn.Category)) {
231
var cat = new Category (itbn.Category);
233
if (!categoryPriorities.TryGetValue (itbn.Category, out prio))
236
categories[itbn.Category] = cat;
238
if (newItem.Text != null)
211
Item newItem = new Item (itbn);
212
if (!categories.ContainsKey (itbn.Category)) {
213
var cat = new Category (itbn.Category);
215
if (!categoryPriorities.TryGetValue (itbn.Category, out prio))
218
categories[itbn.Category] = cat;
220
if (newItem.Text != null)
239
221
categories[itbn.Category].Add (newItem);
243
public void Refresh ()
245
// GUI assert here is to catch Bug 434065 - Exception while going to the editor
246
DispatchService.AssertGuiThread ();
248
if (toolboxService.Initializing) {
249
toolboxWidget.CustomMessage = GettextCatalog.GetString ("Initializing...");
255
toolboxWidget.CustomMessage = null;
258
AddItems (toolboxService.GetCurrentToolboxItems ());
260
Drag.SourceUnset (toolboxWidget);
261
toolboxWidget.ClearCategories ();
263
var cats = categories.Values.ToList ();
264
cats.Sort ((a,b) => a.Priority != b.Priority ? a.Priority.CompareTo (b.Priority) : a.Text.CompareTo (b.Text));
266
foreach (Category category in cats) {
267
category.IsExpanded = true;
225
public void Refresh ()
227
// GUI assert here is to catch Bug 434065 - Exception while going to the editor
228
DispatchService.AssertGuiThread ();
230
if (toolboxService.Initializing) {
231
toolboxWidget.CustomMessage = GettextCatalog.GetString ("Initializing...");
237
toolboxWidget.CustomMessage = null;
240
AddItems (toolboxService.GetCurrentToolboxItems ());
242
Drag.SourceUnset (toolboxWidget);
243
toolboxWidget.ClearCategories ();
245
var cats = categories.Values.ToList ();
246
cats.Sort ((a,b) => a.Priority != b.Priority ? a.Priority.CompareTo (b.Priority) : a.Text.CompareTo (b.Text));
248
foreach (Category category in cats) {
249
category.IsExpanded = true;
268
250
toolboxWidget.AddCategory (category);
270
toolboxWidget.QueueResize ();
252
toolboxWidget.QueueResize ();
271
253
Gtk.TargetEntry[] targetTable = toolboxService.GetCurrentDragTargetTable ();
272
if (targetTable != null)
273
Drag.SourceSet (toolboxWidget, Gdk.ModifierType.Button1Mask, targetTable, Gdk.DragAction.Copy | Gdk.DragAction.Move);
274
compactModeToggleButton.Visible = toolboxWidget.CanIconizeToolboxCategories;
277
void ConfigureToolbar ()
279
// Default configuration
280
categoryPriorities.Clear ();
281
toolboxAddButton.Visible = true;
283
toolboxService.Customize (container, this);
286
protected override void OnDestroyed ()
288
if (fontChanger != null) {
289
fontChanger.Dispose ();
297
#region IPropertyPadProvider
254
if (targetTable != null)
255
Drag.SourceSet (toolboxWidget, Gdk.ModifierType.Button1Mask, targetTable, Gdk.DragAction.Copy | Gdk.DragAction.Move);
256
compactModeToggleButton.Visible = toolboxWidget.CanIconizeToolboxCategories;
259
void ConfigureToolbar ()
261
// Default configuration
262
categoryPriorities.Clear ();
263
toolboxAddButton.Visible = true;
265
toolboxService.Customize (container, this);
268
protected override void OnDestroyed ()
270
if (fontChanger != null) {
271
fontChanger.Dispose ();
279
#region IPropertyPadProvider
299
281
object IPropertyPadProvider.GetActiveComponent ()
301
283
return selectedNode;
304
286
object IPropertyPadProvider.GetProvider ()
306
288
return selectedNode;