internal SelectionData() { ThreadHelper.ThrowIfNotOnUIThread(); selectionMonitor = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection; InitializeSelectionItems(); InitializeContextDictionary(); IVsSettingsManager userSettings = Package.GetGlobalService(typeof(SVsSettingsManager)) as IVsSettingsManager; userSettings.GetReadOnlySettingsStore((uint)__VsSettingsScope.SettingsScope_UserSettings, out var settingsStore); LoadContextIDList(settingsStore, FavoriteContexts, "Favorites"); // at the point where we are created, we don't know what contexts are live so we need to look // for all of them that we know about. foreach (uint contextID in contextIDNames.Keys) { if (ErrorHandler.Succeeded(selectionMonitor.IsCmdUIContextActive(contextID, out var active)) && active != 0) { // Add the item to the live contexts LiveContexts.Add(contextIDNames[contextID]); contextIDNames[contextID].Enabled = true; } } selectionMonitor.AdviseSelectionEvents(this, out selectionEventsCookie); }
/// <summary> /// When the CmdUIContext changes, find the real name of the context. If it looks like a package resource, look up the real name. /// Mark our internal data to be active or inactive and add this event to the log. /// </summary> /// <param name="dwCmdUICookie"></param> /// <param name="fActive"></param> /// <returns></returns> public int OnCmdUIContextChanged(uint dwCmdUICookie, int fActive) { ThreadHelper.ThrowIfNotOnUIThread(); if (contextIDNames.ContainsKey(dwCmdUICookie)) { UIContextInformation ci = contextIDNames[dwCmdUICookie]; if (fActive != 0) { // Before we add this guy, check to see if its name starts with a '#' // Then look it up in its package now since it will be loaded if (!string.IsNullOrEmpty(ci.Package)) { if (ci.Name.StartsWith("#")) { Guid packageGuid = new Guid(ci.Package); IVsResourceManager resources = Package.GetGlobalService(typeof(SVsResourceManager)) as IVsResourceManager; if (ErrorHandler.Succeeded(resources.LoadResourceString(ref packageGuid, 0, ci.Name, out var newName))) { ci.Name = newName; } // Also try without the "#" in the name else if (ErrorHandler.Succeeded(resources.LoadResourceString(ref packageGuid, 0, ci.Name.Substring(1), out newName))) { ci.Name = newName; } } } else if (ci.Name.StartsWith("resource=")) { // Ad7 engines StringBuilder newName = new StringBuilder(255); int resourceIDPosition = ci.Name.IndexOf('#'); string resourceIDString = ci.Name.Substring(resourceIDPosition); string resourceDLL = ci.Name.Substring("resource=".Length, ci.Name.Length - resourceIDPosition); if (uint.TryParse(resourceIDString.Substring(1), out var resourceID)) { if (!string.IsNullOrEmpty(resourceDLL)) { IntPtr hModule = IntPtr.Zero; try { hModule = NativeMethods.LoadLibrary(resourceDLL); if (hModule != IntPtr.Zero) { NativeMethods.LoadString(hModule, resourceID, newName, newName.Capacity + 1); ci.Name = newName.ToString(); } } finally { if (hModule != IntPtr.Zero) { NativeMethods.FreeLibrary(hModule); } } } } } LiveContexts.Add(ci); ci.Enabled = true; } else { LiveContexts.Remove(ci); ci.Enabled = false; } UIContextLog.Insert(0, new UIContextLogItem(fActive == 1, ci)); }