-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConnect.cs
304 lines (261 loc) · 19.3 KB
/
Connect.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
using WordLight.Settings;
namespace WordLight
{
public class Connect : IDTExtensibility2, IDTCommandTarget
{
private const string SettingCommandName = "Settings";
private const string Freeze1CommandName = "Freeze1";
private const string Freeze2CommandName = "Freeze2";
private const string Freeze3CommandName = "Freeze3";
private const string EnableLogCommandName = "EnableLog";
private DTE2 _application;
private AddIn _addInInstance;
private WindowWatcher _watcher;
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_application = (DTE2)application;
_addInInstance = (AddIn)addInInst;
Log.Initialize(_application, "WordLight");
Log.Debug("Initializing the add-in...");
try
{
AddinSettings.Instance.Load(
new RegistrySettingRepository(_application.RegistryRoot + @"\WordLight")
//new VsSettingRepository(_application.Globals, "WordLight")
);
if (connectMode == ext_ConnectMode.ext_cm_AfterStartup)
{
RegisterCommands();
}
if (_watcher == null)
{
_watcher = new WindowWatcher(_application);
}
Log.Debug("Initialized.");
}
catch (Exception ex)
{
Log.Error("Unhandled exception during initializing", ex);
}
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
_watcher.Dispose();
if (disconnectMode == ext_DisconnectMode.ext_dm_HostShutdown || disconnectMode == ext_DisconnectMode.ext_dm_UserClosed)
{
RemoveCommands();
}
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
try
{
RegisterCommands();
}
catch (Exception ex)
{
Log.Error("Unhandled exception in OnStartupComplete", ex);
}
}
public void OnBeginShutdown(ref Array custom)
{
}
private void RegisterCommands()
{
Commands2 commands = (Commands2)_application.Commands;
CommandBars commandBars = (CommandBars)_application.CommandBars;
CommandBar menuBar = commandBars["MenuBar"];
//Find the Tools command bar on the MenuBar command bar:
string toolsMenuName = GetLocalizedMenuName("Tools");
CommandBarControl toolsControl = menuBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
object[] contextGUIDS = new object[] { };
//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
// just make sure you also update the QueryStatus/Exec method to include the new command names.
try
{
//Add a command to the Commands collection:
Command settingCommand = commands.AddNamedCommand2(_addInInstance, SettingCommandName, "WordLight settings...", string.Empty, true, 102, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
//Add a control for the command to the tools menu:
if ((settingCommand != null) && (toolsPopup != null))
{
settingCommand.AddControl(toolsPopup.CommandBar, 1);
}
RegisterSimpleCommand(commands, Freeze1CommandName, "Freeze search 1", AddinSettings.Instance.FreezeMark1Hotkey);
RegisterSimpleCommand(commands, Freeze2CommandName, "Freeze search 2", AddinSettings.Instance.FreezeMark2Hotkey);
RegisterSimpleCommand(commands, Freeze3CommandName, "Freeze search 3", AddinSettings.Instance.FreezeMark3Hotkey);
RegisterSimpleCommand(commands, EnableLogCommandName, "Enable log", null);
}
catch (System.ArgumentException)
{
//If we are here, then the exception is probably because a command with that name
// already exists. If so there is no need to recreate the command and we can
// safely ignore the exception.
}
}
private void RegisterSimpleCommand(Commands2 commands, string name, string buttonText, string bindings)
{
object[] contextGUIDS = new object[] { };
Command cmd = commands.AddNamedCommand2(_addInInstance, name, buttonText, string.Empty, true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
if (bindings != null)
cmd.Bindings = bindings;
}
private void RemoveCommands()
{
List<Command> toDelete = new List<Command>();
foreach (Command cmd in _application.Commands)
{
if (!string.IsNullOrEmpty(cmd.Name) && cmd.Name.StartsWith(_addInInstance.ProgID + "."))
toDelete.Add(cmd);
}
foreach (Command cmd in toDelete)
{
cmd.Delete();
}
}
private string GetLocalizedMenuName(string menuName)
{
string localizedName;
try
{
ResourceManager resourceManager =
new ResourceManager("WordLight.CommandBar", Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(_application.LocaleID);
string resourceName;
if (cultureInfo.TwoLetterISOLanguageName == "zh")
{
System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, menuName);
}
else
{
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, menuName);
}
localizedName = resourceManager.GetString(resourceName);
}
catch
{
localizedName = menuName;
}
return localizedName;
}
#region Implements IDTCommandTarget
/// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated</summary>
/// <param term='commandName'>The name of the command to determine state for.</param>
/// <param term='neededText'>Text that is needed for the command.</param>
/// <param term='status'>The state of the command in the user interface.</param>
/// <param term='commandText'>Text requested by the neededText parameter.</param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
try
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
string commandPrefix = _addInInstance.ProgID + ".";
if (commandName == commandPrefix + SettingCommandName ||
commandName == commandPrefix + Freeze1CommandName ||
commandName == commandPrefix + Freeze2CommandName ||
commandName == commandPrefix + Freeze3CommandName ||
commandName == commandPrefix + EnableLogCommandName)
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}
catch (Exception ex)
{
Log.Error("Unhandled exception in IDTCommandTarget.QueryStatus", ex);
}
}
/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
/// <param term='commandName'>The name of the command to execute.</param>
/// <param term='executeOption'>Describes how the command should be run.</param>
/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
/// <param term='handled'>Informs the caller if the command was handled or not.</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
try
{
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
string commandPrefix = _addInInstance.ProgID + ".";
if (commandName == commandPrefix + SettingCommandName)
{
EditSettings();
handled = true;
}
else if (commandName == commandPrefix + EnableLogCommandName)
{
Log.Enabled = true;
handled = true;
}
else if (commandName == commandPrefix + Freeze1CommandName)
{
FreezeSearchGroup(1);
handled = true;
}
else if (commandName == commandPrefix + Freeze2CommandName)
{
FreezeSearchGroup(2);
handled = true;
}
else if (commandName == commandPrefix + Freeze3CommandName)
{
FreezeSearchGroup(3);
handled = true;
}
}
}
catch (Exception ex)
{
Log.Error("Unhandled exception in IDTCommandTarget.Exec", ex);
}
}
#endregion
private void EditSettings()
{
using (Form dialog = new SettingsForm())
{
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
AddinSettings.Instance.Save();
else
AddinSettings.Instance.Reload();
}
}
private void FreezeSearchGroup(int groupIndex)
{
TextView activeView = _watcher.GetActiveTextView();
if (activeView != null)
{
activeView.FreezeSearch(groupIndex);
Log.Debug("Freezed search group: {0}", groupIndex);
}
else
{
Log.Debug("No active view to freeze group: {0}", groupIndex);
}
}
}
}