forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStripBarManager.cs
More file actions
343 lines (322 loc) · 13.2 KB
/
Copy pathStripBarManager.cs
File metadata and controls
343 lines (322 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using PluginCore;
using PluginCore.Controls;
using PluginCore.Helpers;
using PluginCore.Localization;
using PluginCore.Managers;
namespace FlashDevelop.Managers
{
static class StripBarManager
{
public static List<ToolStripItem> Items = new List<ToolStripItem>();
/// <summary>
/// Finds the tool or menu strip item by name or text
/// </summary>
public static ToolStripItem FindMenuItem(String name)
{
for (Int32 i = 0; i < Items.Count; i++)
{
ToolStripItem item = Items[i];
if (item.Name == name) return item;
}
ShortcutItem item2 = ShortcutManager.GetRegisteredItem(name);
if (item2 != null) return item2.Item;
ToolStripItem item3 = ShortcutManager.GetSecondaryItem(name);
return item3;
}
/// <summary>
/// Finds the tool or menu strip items by name or text
/// </summary>
public static List<ToolStripItem> FindMenuItems(String name)
{
List<ToolStripItem> found = new List<ToolStripItem>();
for (Int32 i = 0; i < Items.Count; i++)
{
ToolStripItem item = Items[i];
if (item.Name == name) found.Add(item);
}
ShortcutItem item2 = ShortcutManager.GetRegisteredItem(name);
if (item2 != null) found.Add(item2.Item);
ToolStripItem item3 = ShortcutManager.GetSecondaryItem(name);
if (item3 != null) found.Add(item3);
return found;
}
/// <summary>
/// Gets a tool strip from the specified xml file
/// </summary>
public static ToolStrip GetToolStrip(String file)
{
ToolStripEx toolStrip = new ToolStripEx();
toolStrip.ImageScalingSize = ScaleHelper.Scale(new Size(16, 16));
XmlNode rootNode = XmlHelper.LoadXmlDocument(file);
foreach (XmlNode subNode in rootNode.ChildNodes)
{
FillToolItems(toolStrip.Items, subNode);
}
return toolStrip;
}
/// <summary>
/// Gets a context menu strip from the specified xml file
/// </summary>
public static ContextMenuStrip GetContextMenu(String file)
{
ContextMenuStrip contextMenu = new ContextMenuStrip();
contextMenu.ImageScalingSize = ScaleHelper.Scale(new Size(16, 16));
XmlNode rootNode = XmlHelper.LoadXmlDocument(file);
foreach (XmlNode subNode in rootNode.ChildNodes)
{
FillMenuItems(contextMenu.Items, subNode);
}
return contextMenu;
}
/// <summary>
/// Gets a menu strip from the specified xml file
/// </summary>
public static MenuStrip GetMenuStrip(String file)
{
MenuStrip menuStrip = new MenuStrip();
menuStrip.ImageScalingSize = ScaleHelper.Scale(new Size(16, 16));
XmlNode rootNode = XmlHelper.LoadXmlDocument(file);
foreach (XmlNode subNode in rootNode.ChildNodes)
{
FillMenuItems(menuStrip.Items, subNode);
}
return menuStrip;
}
/// <summary>
/// Fills items to the specified tool strip item collection
/// </summary>
public static void FillMenuItems(ToolStripItemCollection items, XmlNode node)
{
switch (node.Name)
{
case "menu" :
String name = XmlHelper.GetAttribute(node, "name");
if (name == "SyntaxMenu") node.InnerXml = GetSyntaxMenuXml();
items.Add(GetMenu(node));
break;
case "separator" :
items.Add(GetSeparator(node));
break;
case "button" :
ToolStripMenuItem menu = GetMenuItem(node);
items.Add(menu); // Add menu first to get the id correct
String id = GetMenuItemId(menu);
if (id.IndexOf('.') > -1 && ShortcutManager.GetRegisteredItem(id) == null)
{
ShortcutManager.RegisterItem(id, menu);
}
else ShortcutManager.RegisterSecondaryItem(menu);
break;
}
}
/// <summary>
/// Fills items to the specified tool strip item collection
/// </summary>
public static void FillToolItems(ToolStripItemCollection items, XmlNode node)
{
switch (node.Name)
{
case "separator":
items.Add(GetSeparator(node));
break;
case "button":
ToolStripItem button = GetButtonItem(node);
items.Add(button); // Add button first to get the id correct
ShortcutManager.RegisterSecondaryItem(button);
break;
}
}
/// <summary>
/// Gets a menu from the specified xml node
/// </summary>
public static ToolStripMenuItem GetMenu(XmlNode node)
{
ToolStripMenuItem menu = new ToolStripMenuItem();
String name = XmlHelper.GetAttribute(node, "name");
String image = XmlHelper.GetAttribute(node, "image");
String label = XmlHelper.GetAttribute(node, "label");
String click = XmlHelper.GetAttribute(node, "click");
String flags = XmlHelper.GetAttribute(node, "flags");
String enabled = XmlHelper.GetAttribute(node, "enabled");
String tag = XmlHelper.GetAttribute(node, "tag");
menu.Tag = new ItemData(label, tag, flags);
menu.Text = GetLocalizedString(label);
if (name != null) menu.Name = name; // Use the given name
else menu.Name = label.Replace("Label.", ""); // Assign from id
if (enabled != null) menu.Enabled = Convert.ToBoolean(enabled);
if (image != null) menu.Image = Globals.MainForm.FindImage(image);
if (click != null) menu.Click += GetEventHandler(click);
foreach (XmlNode subNode in node.ChildNodes)
{
FillMenuItems(menu.DropDownItems, subNode);
}
Items.Add(menu);
return menu;
}
/// <summary>
/// Gets a button item from the specified xml node
/// </summary>
public static ToolStripItem GetButtonItem(XmlNode node)
{
ToolStripButton button = new ToolStripButton();
String name = XmlHelper.GetAttribute(node, "name");
String image = XmlHelper.GetAttribute(node, "image");
String label = XmlHelper.GetAttribute(node, "label");
String click = XmlHelper.GetAttribute(node, "click");
String flags = XmlHelper.GetAttribute(node, "flags");
String enabled = XmlHelper.GetAttribute(node, "enabled");
String keyId = XmlHelper.GetAttribute(node, "keyid");
String tag = XmlHelper.GetAttribute(node, "tag");
button.Tag = new ItemData(label + ";" + keyId, tag, flags);
if (name != null) button.Name = name; // Use the given name
else button.Name = label.Replace("Label.", ""); // Assign from id
String stripped = GetStrippedString(GetLocalizedString(label), false);
if (image != null) button.ToolTipText = stripped;
else button.Text = stripped; // Use text instead...
if (enabled != null) button.Enabled = Convert.ToBoolean(enabled);
if (image != null) button.Image = Globals.MainForm.FindImage(image);
if (click != null) button.Click += GetEventHandler(click);
Items.Add(button);
return button;
}
/// <summary>
/// Get a menu item from the specified xml node
/// </summary>
public static ToolStripMenuItem GetMenuItem(XmlNode node)
{
ToolStripMenuItem menu = new ToolStripMenuItem();
String name = XmlHelper.GetAttribute(node, "name");
String image = XmlHelper.GetAttribute(node, "image");
String label = XmlHelper.GetAttribute(node, "label");
String click = XmlHelper.GetAttribute(node, "click");
String enabled = XmlHelper.GetAttribute(node, "enabled");
String shortcut = XmlHelper.GetAttribute(node, "shortcut");
String keytext = XmlHelper.GetAttribute(node, "keytext");
String keyId = XmlHelper.GetAttribute(node, "keyid");
String flags = XmlHelper.GetAttribute(node, "flags");
String tag = XmlHelper.GetAttribute(node, "tag");
menu.Tag = new ItemData(label + ";" + keyId, tag, flags);
menu.Text = GetLocalizedString(label);
if (name != null) menu.Name = name; // Use the given name
else menu.Name = label.Replace("Label.", ""); // Assign from id
if (image != null) menu.Image = Globals.MainForm.FindImage(image);
if (enabled != null) menu.Enabled = Convert.ToBoolean(enabled);
if (keytext != null) menu.ShortcutKeyDisplayString = GetKeyText(keytext);
if (click != null) menu.Click += GetEventHandler(click);
if (shortcut != null) menu.ShortcutKeys = GetKeys(shortcut);
Items.Add(menu);
return menu;
}
/// <summary>
/// Gets a new tool strip separetor item
/// </summary>
public static ToolStripSeparator GetSeparator(XmlNode node)
{
return new ToolStripSeparator();
}
/// <summary>
/// Gets the dynamic syntax menu xml (easy integration :)
/// </summary>
private static String GetSyntaxMenuXml()
{
String syntaxXml = "";
String[] syntaxFiles = Directory.GetFiles(Path.Combine(PathHelper.SettingDir, "Languages"), "*.xml");
String xmlTmpl = "<button label=\"{0}\" click=\"ChangeSyntax\" tag=\"{1}\" image=\"559\" flags=\"Enable:IsEditable+Check:IsEditable|IsActiveSyntax\" />";
for (Int32 i = 0; i < syntaxFiles.Length; i++)
{
String fileName = Path.GetFileNameWithoutExtension(syntaxFiles[i]);
syntaxXml += String.Format(xmlTmpl, fileName, fileName.ToLower());
}
return syntaxXml;
}
/// <summary>
/// Strips normal label characters from the string
/// </summary>
private static String GetStrippedString(String text, Boolean removeWhite)
{
text = TextHelper.RemoveMnemonicsAndEllipsis(text);
if (removeWhite)
{
text = text.Replace(" ", "");
text = text.Replace("\t", "");
}
return text;
}
/// <summary>
/// Gets a localized string if available
/// </summary>
private static String GetLocalizedString(String key)
{
try
{
if (!key.StartsWithOrdinal("Label.")) return key;
else return TextHelper.GetString(key);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
return String.Empty;
}
}
/// <summary>
/// Gets the id of the menu item from owner tree
/// </summary>
public static String GetMenuItemId(ToolStripItem menu)
{
if (menu.OwnerItem != null)
{
ToolStripItem parent = menu.OwnerItem;
return GetMenuItemId(parent) + "." + GetStrippedString(menu.Name, true);
}
else return GetStrippedString(menu.Name, true);
}
/// <summary>
/// Gets a shortcut key string from a string
/// </summary>
private static String GetKeyText(String data)
{
data = data.Replace("|", "+");
data = data.Replace("Control", "Ctrl");
return data;
}
/// <summary>
/// Gets a shortcut keys from a string
/// </summary>
private static Keys GetKeys(String data)
{
try
{
Keys shortcut = Keys.None;
String[] keys = data.Split('|');
for (Int32 i = 0; i < keys.Length; i++) shortcut = shortcut | (Keys)Enum.Parse(typeof(Keys), keys[i]);
return shortcut;
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
return Keys.None;
}
}
/// <summary>
/// Gets a click handler from a string
/// </summary>
private static EventHandler GetEventHandler(String method)
{
try
{
return (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), Globals.MainForm, method);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
return null;
}
}
}
}