-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPluginMain.cs
391 lines (333 loc) · 13.5 KB
/
PluginMain.cs
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using NavigationBar.Helpers;
using NavigationBar.Managers;
using PluginCore;
using PluginCore.Helpers;
using PluginCore.Managers;
using PluginCore.Utilities;
using ProjectManager;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace NavigationBar
{
public class PluginMain : IPlugin
{
private const int API = 1;
private const string NAME = "NavigationBar";
private const string GUID = "F313AE66-0C5F-4388-B281-9E9AFAD7B8F9";
private const string HELP = "www.flashdevelop.org/community/viewtopic.php?f=4&t=9376";
private const string DESCRIPTION = "Adds a navigation bar for quickly moving through your source.";
private const string AUTHOR = "Joey Robichaud";
private string _settingFilename = "";
private Settings _settings;
private ToolStripSplitButton _navigateBackwardButton = null;
private ToolStripSplitButton _navigateForwardButton = null;
#region Required Properties
/// <summary>
/// Api level of the plugin
/// </summary>
public int Api
{
get { return API; }
}
/// <summary>
/// Name of the plugin
/// </summary>
public string Name
{
get { return NAME; }
}
/// <summary>
/// GUID of the plugin
/// </summary>
public string Guid
{
get { return GUID; }
}
/// <summary>
/// Author of the plugin
/// </summary>
public string Author
{
get { return AUTHOR; }
}
/// <summary>
/// Description of the plugin
/// </summary>
public string Description
{
get { return DESCRIPTION; }
}
/// <summary>
/// Web address for help
/// </summary>
public string Help
{
get { return HELP; }
}
/// <summary>
/// Object that contains the settings
/// </summary>
[Browsable(false)]
public object Settings
{
get { return _settings; }
}
#endregion
#region Required Methods
/// <summary>
/// Initializes the plugin
/// </summary>
public void Initialize()
{
InitBasics();
LoadSettings();
AddEventHandlers();
CreateMenuItems();
CreateToolbarItems();
}
/// <summary>
/// Disposes the plugin
/// </summary>
public void Dispose()
{
SaveSettings();
}
/// <summary>
/// Handles the incoming events
/// </summary>
public void HandleEvent(object sender, NotifyEvent e, HandlingPriority prority)
{
if (e.Type == EventType.FileOpen || e.Type == EventType.FileNew)
{
ITabbedDocument document = PluginBase.MainForm.CurrentDocument;
if (document != null)
{
// Check to see if we've already added an NavigationBar to the
// current document.
if (document.SciControl == null)
return;
Controls.NavigationBar bar = GetNavigationBar(document);
if (bar != null)
return;
// Dock a new navigation bar to the top of the current document
bar = new Controls.NavigationBar(document, _settings);
document.Controls.Add(bar);
}
}
else if (e.Type == EventType.FileSwitch)
{
ITabbedDocument document = PluginBase.MainForm.CurrentDocument;
if (document != null)
{
// Check to see if this document contains a text editor.
if (document.SciControl == null)
return;
// Refresh the navigation bar
Controls.NavigationBar bar = GetNavigationBar(document);
if (bar != null)
bar.RefreshSettings();
}
}
else if (e.Type == EventType.Command)
{
DataEvent de = e as DataEvent;
if (de.Action.StartsWith("ProjectManager."))
{
if (de.Action == ProjectManagerCommands.NewProject)
NavigationManager.Instance.Clear();
else if (de.Action == ProjectManagerCommands.OpenProject)
NavigationManager.Instance.Clear();
}
}
else if (e.Type == EventType.ApplyTheme)
{
foreach (var document in PluginBase.MainForm.Documents)
{
if (document.SciControl == null)
continue;
var bar = GetNavigationBar(document);
if (bar != null)
bar.ApplyTheme();
}
}
}
void _settings_OnSettingsChanged()
{
UpdateNavigationButtons();
}
private void NavigationManager_LocationChanged(object sender, EventArgs e)
{
UpdateNavigationButtons();
}
private void UpdateNavigationButtons()
{
_navigateBackwardButton.Enabled = NavigationManager.Instance.CanNavigateBackward;
_navigateBackwardButton.Visible = _settings.ShowNavigationToolbar;
_navigateForwardButton.Enabled = NavigationManager.Instance.CanNavigateForward;
_navigateForwardButton.Visible = _settings.ShowNavigationToolbar;
}
void NavigateForward(object sender, EventArgs e)
{
NavigationManager.Instance.NavigateForward();
}
void NavigateBackward(object sender, EventArgs e)
{
NavigationManager.Instance.NavigateBackward();
}
#endregion
#region Plugin Methods
private void OpenImports(object sender, EventArgs e)
{
Controls.NavigationBar navBar = GetNavigationBar();
if (navBar == null || !navBar.Visible)
return;
navBar.OpenImports();
}
private void OpenClasses(object sender, EventArgs e)
{
Controls.NavigationBar navBar = GetNavigationBar();
if (navBar == null || !navBar.Visible)
return;
navBar.OpenClasses();
}
private void OpenMembers(object sender, EventArgs e)
{
Controls.NavigationBar navBar = GetNavigationBar();
if (navBar == null || !navBar.Visible)
return;
navBar.OpenMembers();
}
private Controls.NavigationBar GetNavigationBar()
{
ITabbedDocument document = PluginBase.MainForm.CurrentDocument;
if (document == null)
return null;
return GetNavigationBar(document);
}
private Controls.NavigationBar GetNavigationBar(ITabbedDocument document)
{
return document.Controls
.OfType<Controls.NavigationBar>()
.FirstOrDefault();
}
#endregion
#region Custom Methods
/// <summary>
/// Initializes important variables
/// </summary>
public void InitBasics()
{
string dataPath = Path.Combine(PathHelper.DataDir, NAME);
if (!Directory.Exists(dataPath)) Directory.CreateDirectory(dataPath);
_settingFilename = Path.Combine(dataPath, "Settings.fdb");
}
/// <summary>
/// Adds the required event handlers
/// </summary>
public void AddEventHandlers()
{
// Set events you want to listen (combine as flags)
EventManager.AddEventHandler(this, EventType.FileNew | EventType.FileOpen | EventType.FileSwitch | EventType.Command | EventType.ApplyTheme);
NavigationManager.Instance.LocationChanged += new EventHandler(NavigationManager_LocationChanged);
_settings.OnSettingsChanged += new SettingsChangesEvent(_settings_OnSettingsChanged);
}
/// <summary>
/// Adds shortcuts for manipulating the navigation bar
/// </summary>
public void CreateMenuItems()
{
ToolStripMenuItem menu = (ToolStripMenuItem)PluginBase.MainForm.FindMenuItem("SearchMenu");
ToolStripMenuItem menuItem;
menuItem = new ToolStripMenuItem(ResourceHelper.GetString("NavigationBar.Label.OpenImports"), null, new EventHandler(OpenImports));
menuItem.Visible = false;
PluginBase.MainForm.RegisterShortcutItem("NavigationBar.OpenImports", menuItem);
menu.DropDownItems.Add(menuItem);
menuItem = new ToolStripMenuItem(ResourceHelper.GetString("NavigationBar.Label.OpenClasses"), null, new EventHandler(OpenClasses));
menuItem.Visible = false;
PluginBase.MainForm.RegisterShortcutItem("NavigationBar.OpenClasses", menuItem);
menu.DropDownItems.Add(menuItem);
menuItem = new ToolStripMenuItem(ResourceHelper.GetString("NavigationBar.Label.OpenMembers"), null, new EventHandler(OpenMembers));
menuItem.Visible = false;
PluginBase.MainForm.RegisterShortcutItem("NavigationBar.OpenMembers", menuItem);
menu.DropDownItems.Add(menuItem);
menuItem = new ToolStripMenuItem(ResourceHelper.GetString("NavigationBar.Label.NavigateForward"), null, new EventHandler(NavigateForward));
menuItem.Visible = false;
PluginBase.MainForm.RegisterShortcutItem("NavigationBar.NavigateForward", menuItem);
menu.DropDownItems.Add(menuItem);
menuItem = new ToolStripMenuItem(ResourceHelper.GetString("NavigationBar.Label.NavigateBackward"), null, new EventHandler(NavigateBackward));
menuItem.Visible = false;
PluginBase.MainForm.RegisterShortcutItem("NavigationBar.NavigateBackward", menuItem);
menu.DropDownItems.Add(menuItem);
}
public void CreateToolbarItems()
{
_navigateBackwardButton = new ToolStripSplitButton(ResourceHelper.GetString("NavigationBar.Label.NavigateBackward"), PluginBase.MainForm.FindImage("315|1|-3|3"));
_navigateBackwardButton.Name = "NavigateBackward";
_navigateBackwardButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
_navigateBackwardButton.ButtonClick += new EventHandler(NavigateBackward);
_navigateBackwardButton.DropDownOpening += NavigateBackwardDropDownOpening;
_navigateBackwardButton.DropDown.Renderer = new DockPanelStripRenderer();
PluginBase.MainForm.ToolStrip.Items.Add(_navigateBackwardButton);
_navigateForwardButton = new ToolStripSplitButton(ResourceHelper.GetString("NavigationBar.Label.NavigateForward"), PluginBase.MainForm.FindImage("315|9|3|3"));
_navigateForwardButton.Name = "NavigateForward";
_navigateForwardButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
_navigateForwardButton.ButtonClick += new EventHandler(NavigateForward);
_navigateForwardButton.DropDownOpening += NavigateForwardDropDownOpening;
_navigateForwardButton.DropDown.Renderer = new DockPanelStripRenderer();
PluginBase.MainForm.ToolStrip.Items.Add(_navigateForwardButton);
UpdateNavigationButtons();
}
private void NavigateBackwardDropDownOpening(object sender, EventArgs e)
{
_navigateBackwardButton.DropDownItems.Clear();
var historyItems = NavigationManager.Instance.BackwardHistory.Select(nl =>
new ToolStripMenuItem(nl.ToString(), null, NavigateBackwardDropDownItemClick) { Tag = nl }
);
_navigateBackwardButton.DropDownItems.AddRange(historyItems.ToArray());
}
private void NavigateBackwardDropDownItemClick(object sender, EventArgs e)
{
var item = (ToolStripMenuItem)sender;
var navigationLocation = (NavigationLocation)item.Tag;
NavigationManager.Instance.NavigateBackwardTo(navigationLocation);
}
private void NavigateForwardDropDownOpening(object sender, EventArgs e)
{
_navigateForwardButton.DropDownItems.Clear();
var historyItems = NavigationManager.Instance.ForwardHistory.Select(nl =>
new ToolStripMenuItem(nl.ToString(), null, NavigateForwardDropDownItemClick) { Tag = nl }
);
_navigateForwardButton.DropDownItems.AddRange(historyItems.ToArray());
}
private void NavigateForwardDropDownItemClick(object sender, EventArgs e)
{
var item = (ToolStripMenuItem)sender;
var navigationLocation = (NavigationLocation)item.Tag;
NavigationManager.Instance.NavigateForwardTo(navigationLocation);
}
/// <summary>
/// Loads the plugin settings
/// </summary>
public void LoadSettings()
{
_settings = new Settings();
if (!File.Exists(_settingFilename)) SaveSettings();
else
{
object obj = ObjectSerializer.Deserialize(_settingFilename, _settings);
_settings = (Settings)obj;
}
}
/// <summary>
/// Saves the plugin settings
/// </summary>
public void SaveSettings()
{
ObjectSerializer.Serialize(_settingFilename, _settings);
}
#endregion
}
}