forked from pasztorpisti/vs-window-title-changer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackageGlobals.cs
716 lines (609 loc) · 19.7 KB
/
PackageGlobals.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using VSWindowTitleChanger.ExpressionEvaluator;
using Microsoft.VisualStudio.Shell;
namespace VSWindowTitleChanger
{
class PackageGlobals
{
// Replace every backslash chars to slashes in the solution pathname before regex matching.
// This makes it easier to write regex patterns that often overuse backslash chars.
public const bool SlashPathSeparator = true;
static PackageGlobals m_Globals;
public static void InitInstance(VSWindowTitleChangerPackage package)
{
Debug.Assert(m_Globals == null);
if (m_Globals == null)
m_Globals = new PackageGlobals(package);
}
public static void DeinitInstance()
{
Debug.Assert(m_Globals != null);
if (m_Globals != null)
{
m_Globals.Cleanup();
m_Globals = null;
}
}
public static PackageGlobals Instance()
{
Debug.Assert(m_Globals != null);
return m_Globals;
}
public class DebugOutputInterface
{
Package m_Package;
public void SetPackage(Package package)
{
m_Package = package;
}
public void Write(string msg)
{
Debug.Write(msg);
if (m_Package == null)
return;
PackageGlobals.InvokeOnUIThread(delegate()
{
IVsOutputWindowPane output_pane = m_Package.GetOutputPane(GuidList.guidVSWindowTitleChangerCmdSet, "VSWindowTitleChanger");
if (output_pane != null)
output_pane.OutputString(msg);
});
}
public void Write(string msg, params object[] args)
{
Write(string.Format(msg, args));
}
public void WriteLine(string msg)
{
Debug.WriteLine(msg);
if (m_Package == null)
return;
PackageGlobals.InvokeOnUIThread(delegate()
{
IVsOutputWindowPane output_pane = m_Package.GetOutputPane(GuidList.guidVSWindowTitleChangerCmdSet, "VSWindowTitleChanger");
if (output_pane != null)
{
msg += "\n";
output_pane.OutputString(msg);
}
});
}
public void WriteLine(string msg, params object[] args)
{
WriteLine(string.Format(msg, args));
}
}
public static DebugOutputInterface DebugOutput = new DebugOutputInterface();
public delegate void Job();
class UIThreadDispatcher : UserControl
{
public UIThreadDispatcher()
{
CreateHandle();
}
}
static Control m_UIThreadDispatcher = new UIThreadDispatcher();
// Poor man's UI Thread Dispatcher for framework v2
public static void BeginInvokeOnUIThread(Job action)
{
m_UIThreadDispatcher.BeginInvoke(action);
}
public static void InvokeOnUIThread(Job action)
{
if (m_UIThreadDispatcher.InvokeRequired)
m_UIThreadDispatcher.Invoke(action);
else
action();
}
public TitleSetup TitleSetup
{
get
{
return m_TitleSetup;
}
}
public void ShowTitleExpressionEditor()
{
if (m_TitleSetupEditor.Visible)
{
m_TitleSetupEditor.BringToFront();
return;
}
m_TitleSetupEditor.TitleSetup = m_TitleSetup;
m_TitleSetupEditor.Show();
}
internal VariableValueResolver CompileTimeConstants
{
get
{
return m_CompileTimeConstants;
}
}
public TitleSetupEditor TitleSetupEditor
{
get
{
return m_TitleSetupEditor;
}
}
public ExecFuncEvaluatorThread ExecFuncEvaluator
{
get
{
return m_ExecFuncEvaluatorThread;
}
}
public int DTEMajorVersion
{
get
{
Debug.Assert(m_DTEMajorVersion >= 0);
return m_DTEMajorVersion;
}
}
public int DTEMinorVersion
{
get
{
Debug.Assert(m_DTEMinorVersion >= 0);
return m_DTEMinorVersion;
}
}
int m_DTEMajorVersion = -1;
int m_DTEMinorVersion = -1;
VSWindowTitleChangerPackage m_Package;
TitleSetup m_TitleSetup;
TitleSetupEditor m_TitleSetupEditor;
VariableValueResolver m_CompileTimeConstants;
ExecFuncEvaluatorThread m_ExecFuncEvaluatorThread;
PackageGlobals(VSWindowTitleChangerPackage package)
{
m_Package = package;
CreateCompileTimeConstants();
ParseDTEVersion();
m_ExecFuncEvaluatorThread = new ExecFuncEvaluatorThread();
m_TitleSetup = package.GetTitleSetupFromOptions();
m_TitleSetupEditor = new TitleSetupEditor();
m_TitleSetupEditor.SaveEditedSetup += SaveEditedSetup;
m_TitleSetupEditor.RevertToOriginalSetup += RevertToOriginalSetup;
m_TitleSetupEditor.CustomTabbingEnabled = true;
}
void SaveEditedSetup(TitleSetup title_setup)
{
m_TitleSetup = title_setup;
m_Package.SaveTitleSetupToOptions(m_TitleSetup);
}
void RevertToOriginalSetup(TitleSetup title_setup)
{
m_TitleSetup = title_setup;
}
void Cleanup()
{
m_TitleSetupEditor.Dispose();
m_TitleSetupEditor = null;
m_ExecFuncEvaluatorThread.Dispose();
}
void CreateCompileTimeConstants()
{
DTE2 dte = (DTE2)m_Package.GetInterface(typeof(DTE));
m_CompileTimeConstants = new VariableValueResolver();
m_CompileTimeConstants.SetVariable("true", true);
m_CompileTimeConstants.SetVariable("false", false);
m_CompileTimeConstants.SetVariable("dte_version", dte.Version);
m_CompileTimeConstants.SetVariable("vs_version", DTEVersionToVSYear(dte.Version));
m_CompileTimeConstants.SetVariable("vs_edition", dte.Edition);
}
void ParseDTEVersion()
{
try
{
DTE2 dte = (DTE2)m_Package.GetInterface(typeof(DTE));
string[] version_numbers = dte.Version.Split(new char[] { '.' });
Debug.Assert(version_numbers.Length == 2);
if (version_numbers.Length != 2)
return;
string dte_version = dte.Version;
m_DTEMajorVersion = Convert.ToInt32(version_numbers[0]);
m_DTEMinorVersion = Convert.ToInt32(version_numbers[1]);
}
catch (System.Exception)
{
}
}
static string DTEVersionToVSYear(string dte_version)
{
switch (dte_version)
{
case "8.0": return "2005";
case "9.0": return "2008";
case "10.0": return "2010";
case "11.0": return "2012";
case "12.0": return "2013";
default: return dte_version;
}
}
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
static string GetWindowClassName(IntPtr hWnd)
{
// Pre-allocate 256 characters, since this is the maximum class name length.
StringBuilder class_name = new StringBuilder(256);
//Get the window class name
int ret = GetClassName(hWnd, class_name, class_name.Capacity);
if (ret <= 0)
return "";
return class_name.ToString();
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private static string GetWindowText(IntPtr hWnd)
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
public static Project GetStartupProject(Solution solution)
{
Project ret = null;
if (solution != null &&
solution.SolutionBuild != null &&
solution.SolutionBuild.StartupProjects != null)
{
string uniqueName = (string)((object[])solution.SolutionBuild.StartupProjects)[0];
// Can't use the solution.Item(uniqueName) here since that doesn't work
// for projects under solution folders.
ret = GetProject(solution, uniqueName);
}
return ret;
}
public static Project GetProject(Solution solution, string uniqueName)
{
Project ret = null;
if (solution != null && uniqueName != null)
{
foreach (Project p in solution.Projects)
{
ret = GetSubProject(p, uniqueName);
if (ret != null)
break;
}
}
return ret;
}
public static Project GetSubProject(Project project, string uniqueName)
{
Project ret = null;
if (project != null)
{
if (project.UniqueName == uniqueName)
{
ret = project;
}
else if (project.Kind == EnvDTE.Constants.vsProjectKindSolutionItems)
{
// Solution folder
foreach (ProjectItem projectItem in project.ProjectItems)
{
ret = GetSubProject(projectItem.SubProject, uniqueName);
if (ret != null)
break;
}
}
}
return ret;
}
class VarValues
{
public string sln_path = "";
public DBGMODE dbgmode = DBGMODE.DBGMODE_Design;
public bool sln_dirty = false;
public string configuration = "";
public string platform = "";
public string startup_proj = "";
public string startup_proj_path = "";
public bool startup_proj_dirty = false;
public string doc_path = "";
public bool doc_dirty = false;
public bool any_doc_dirty = false;
public bool any_proj_dirty = false;
public bool wnd_minimized = false;
public bool wnd_foreground = false;
public bool app_active = false;
public string active_wnd_title = "";
public string active_wnd_class = "";
public string orig_title = "";
public string cmdline = "";
public List<Exception> exceptions = new List<Exception>();
}
Project FindProjectObjectInSolutionFolder(Project solution_folder, object proj_id)
{
Project proj;
try
{
proj = solution_folder.ProjectItems.Item(proj_id).SubProject;
}
catch
{
proj = null;
}
if (proj != null)
return proj;
for (int i=1,e=solution_folder.ProjectItems.Count; i<=e; ++i)
{
Project sub_proj = solution_folder.ProjectItems.Item(i).SubProject;
if (sub_proj == null || sub_proj.Kind != ProjectKinds.vsProjectKindSolutionFolder)
continue;
proj = FindProjectObjectInSolutionFolder(sub_proj, proj_id);
if (proj != null)
return proj;
}
return null;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetCommandLine();
void GetVariableValues(VarValues var_values)
{
DTE2 dte = (DTE2)m_Package.GetInterface(typeof(DTE));
IVsSolution vs_solution = (IVsSolution)m_Package.GetInterface(typeof(IVsSolution));
string temp_solution_dir, temp_solution_options;
if (VSConstants.S_OK != vs_solution.GetSolutionInfo(out temp_solution_dir, out var_values.sln_path, out temp_solution_options) || var_values.sln_path == null)
var_values.sln_path = "";
IVsDebugger debugger = (IVsDebugger)m_Package.GetInterface(typeof(IVsDebugger));
DBGMODE[] adbgmode = new DBGMODE[] { DBGMODE.DBGMODE_Design };
if (VSConstants.S_OK != debugger.GetMode(adbgmode))
adbgmode[0] = DBGMODE.DBGMODE_Design;
var_values.dbgmode = adbgmode[0] & ~DBGMODE.DBGMODE_EncMask;
var_values.sln_dirty = !dte.Solution.Saved;
try
{
SolutionConfiguration2 active_cfg = (SolutionConfiguration2)dte.Solution.SolutionBuild.ActiveConfiguration;
if (active_cfg != null)
{
var_values.configuration = active_cfg.Name == null ? "" : active_cfg.Name; ;
var_values.platform = active_cfg.PlatformName == null ? "" : active_cfg.PlatformName;
}
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
try
{
Project startup_project = GetStartupProject(dte.Solution);
if (startup_project != null)
{
var_values.startup_proj = startup_project.Name;
var_values.startup_proj_path = startup_project.FullName;
var_values.startup_proj_dirty = !startup_project.Saved;
}
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
try
{
Document active_document = dte.ActiveDocument;
if (active_document != null)
{
var_values.doc_path = active_document.FullName;
var_values.doc_dirty = !active_document.Saved;
}
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
try
{
foreach (Document doc in dte.Documents)
{
if (!doc.Saved)
{
var_values.any_doc_dirty = true;
break;
}
}
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
try
{
foreach (Project proj in dte.Solution.Projects)
{
if (!proj.Saved)
{
var_values.any_proj_dirty = true;
break;
}
}
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
try
{
var_values.wnd_minimized = m_Package.VSMainWindow.Minimized;
var_values.wnd_foreground = m_Package.VSMainWindow.IsForegroundWindow();
var_values.app_active = m_Package.VSMainWindow.IsAppActive;
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
IntPtr active_wnd = GetActiveWindow();
if (active_wnd != IntPtr.Zero)
{
var_values.active_wnd_title = GetWindowText(active_wnd);
var_values.active_wnd_class = GetWindowClassName(active_wnd);
}
var_values.orig_title = m_Package.VSMainWindow.OriginalTitle;
try
{
var_values.cmdline = Marshal.PtrToStringAuto(GetCommandLine());
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
}
void SetVariableValuesFromIDEState(IVariableValueSetter var_value_setter, VSMultiInstanceInfo multi_instance_info)
{
VarValues var_values = new VarValues();
try
{
GetVariableValues(var_values);
}
catch (System.Exception ex)
{
var_values.exceptions.Add(ex);
}
if (var_values.exceptions.Count > 0 && m_Package.IsDebugEnabled())
{
DebugOutput.WriteLine("----- Exceptions: GetVariableValues:");
foreach (Exception ex in var_values.exceptions)
DebugOutput.WriteLine("EX: " + ex.ToString());
}
AddFilePathVars(var_value_setter, ref var_values.sln_path, SlashPathSeparator, "sln_");
var_value_setter.SetVariable("sln_open", var_values.sln_path.Length > 0);
var_value_setter.SetVariable("sln_dirty", var_values.sln_dirty ? "*" : "");
AddFilePathVars(var_value_setter, ref var_values.doc_path, SlashPathSeparator, "doc_");
var_value_setter.SetVariable("doc_open", var_values.doc_path.Length > 0);
var_value_setter.SetVariable("doc_dirty", var_values.doc_dirty ? "*" : "");
var_value_setter.SetVariable("any_doc_dirty", var_values.any_doc_dirty ? "*" : "");
AddFilePathVars(var_value_setter, ref var_values.startup_proj_path, SlashPathSeparator, "startup_proj_");
var_value_setter.SetVariable("startup_proj", var_values.startup_proj);
var_value_setter.SetVariable("startup_proj_dirty", var_values.startup_proj_dirty ? "*" : "");
var_value_setter.SetVariable("any_proj_dirty", var_values.any_proj_dirty ? "*" : "");
var_value_setter.SetVariable("anything_dirty", (var_values.sln_dirty || var_values.any_proj_dirty || var_values.any_doc_dirty) ? "*" : "");
var_value_setter.SetVariable("wnd_minimized", var_values.wnd_minimized);
var_value_setter.SetVariable("wnd_foreground", var_values.wnd_foreground);
var_value_setter.SetVariable("app_active", var_values.app_active);
bool debugging = false;
string debug_mode = "";
switch (var_values.dbgmode)
{
case DBGMODE.DBGMODE_Run:
debugging = true;
debug_mode = "running";
break;
case DBGMODE.DBGMODE_Break:
debugging = true;
debug_mode = "debugging";
break;
}
var_value_setter.SetVariable("debugging", debugging);
var_value_setter.SetVariable("debug_mode", debug_mode);
var_value_setter.SetVariable("configuration", var_values.configuration);
var_value_setter.SetVariable("platform", var_values.platform);
var_value_setter.SetVariable("orig_title", var_values.orig_title);
var_value_setter.SetVariable("multi_instances", multi_instance_info.multiple_instances);
var_value_setter.SetVariable("multi_instances_same_ver", multi_instance_info.multiple_instances_same_version);
var_value_setter.SetVariable("active_wnd_title", var_values.active_wnd_title);
var_value_setter.SetVariable("active_wnd_class", var_values.active_wnd_class);
var_value_setter.SetVariable("cmdline", var_values.cmdline);
}
[DllImport("ole32.dll")]
static extern int GetRunningObjectTable(uint reserved, out IRunningObjectTable pprot);
[DllImport("ole32.dll")]
static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);
static Regex m_DTEComObjectNameRegex = new Regex(@"^!VisualStudio\.DTE\.(?<dte_version>\d+\.\d+).*$");
public struct VSMultiInstanceInfo
{
public bool multiple_instances;
public bool multiple_instances_same_version;
}
public void GetVSMultiInstanceInfo(out VSMultiInstanceInfo vs_instance_info)
{
DTE2 dte = (DTE2)m_Package.GetInterface(typeof(DTE));
GetVSMultiInstanceInfo(out vs_instance_info, dte.Version);
}
public static void GetVSMultiInstanceInfo(out VSMultiInstanceInfo vs_instance_info, string our_dte_version)
{
vs_instance_info.multiple_instances = false;
vs_instance_info.multiple_instances_same_version = false;
try
{
IRunningObjectTable running_object_table;
if (VSConstants.S_OK != GetRunningObjectTable(0, out running_object_table))
return;
IEnumMoniker moniker_enumerator;
running_object_table.EnumRunning(out moniker_enumerator);
moniker_enumerator.Reset();
IMoniker[] monikers = new IMoniker[1];
IntPtr num_fetched = IntPtr.Zero;
int dte_count = 0;
int dte_count_our_version = 0;
while (VSConstants.S_OK == moniker_enumerator.Next(1, monikers, num_fetched))
{
IBindCtx ctx;
if (VSConstants.S_OK != CreateBindCtx(0, out ctx))
continue;
string name;
monikers[0].GetDisplayName(ctx, null, out name);
if (!name.StartsWith("!VisualStudio.DTE."))
continue;
object com_object;
if (VSConstants.S_OK != running_object_table.GetObject(monikers[0], out com_object))
continue;
DTE2 dte = com_object as DTE2;
if (dte != null)
{
++dte_count;
Match m = m_DTEComObjectNameRegex.Match(name);
if (m.Success)
{
Group g = m.Groups["dte_version"];
if (g.Success && g.Value == our_dte_version)
++dte_count_our_version;
}
}
}
vs_instance_info.multiple_instances = dte_count > 1;
vs_instance_info.multiple_instances_same_version = dte_count_our_version > 1;
}
catch
{
vs_instance_info.multiple_instances = false;
vs_instance_info.multiple_instances_same_version = false;
}
}
public EvalContext CreateFreshEvalContext()
{
PackageGlobals.VSMultiInstanceInfo multi_instance_info;
GetVSMultiInstanceInfo(out multi_instance_info);
EvalContext eval_ctx = new EvalContext();
SetVariableValuesFromIDEState(eval_ctx, multi_instance_info);
return eval_ctx;
}
void AddFilePathVars(IVariableValueSetter var_value_setter, ref string path, bool user_forward_slashes, string var_name_prefix)
{
Util.FilenameParts parts = new Util.FilenameParts();
Util.ProcessFilePath(path, user_forward_slashes, ref parts);
var_value_setter.SetVariable(var_name_prefix + "path", parts.path);
var_value_setter.SetVariable(var_name_prefix + "dir", parts.dir);
var_value_setter.SetVariable(var_name_prefix + "file", parts.file);
var_value_setter.SetVariable(var_name_prefix + "filename", parts.filename);
var_value_setter.SetVariable(var_name_prefix + "ext", parts.ext);
path = parts.path;
}
}
}