Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
VisualStudioThemeEngine.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Theme Coloring Source: https://github.com/fsprojects/VisualFSharpPowerTools
5 //
6 // Copyright 2014 F# Software Foundation
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 using System;
21 using System.Collections.Generic;
22 using EnvDTE80;
23 using Microsoft.VisualStudio;
24 using Microsoft.VisualStudio.Shell.Interop;
25 using Microsoft.Win32;
26 
27 namespace SiliconStudio.Paradox.VisualStudio.Classifiers
28 {
29  internal class VisualStudioThemeEngine : IVsBroadcastMessageEvents, IDisposable
30  {
31  private const uint WM_SYSCOLORCHANGE = 0x0015;
32 
33  private readonly DTE2 dte;
34  private readonly IVsShell shellService;
35  private uint broadcastCookie;
36 
37  private readonly Dictionary<Guid, VisualStudioTheme> availableThemes = new Dictionary<Guid, VisualStudioTheme>
38  {
39  { new Guid("DE3DBBCD-F642-433C-8353-8F1DF4370ABA"), VisualStudioTheme.Light },
40  { new Guid("A4D6A176-B948-4B29-8C66-53C97A1ED7D0"), VisualStudioTheme.Blue },
41  { new Guid("1DED0138-47CE-435E-84EF-9EC1F439B749"), VisualStudioTheme.Dark }
42  };
43 
44  public event EventHandler OnThemeChanged;
45 
46  public VisualStudioThemeEngine(IServiceProvider serviceProvider)
47  {
48  dte = (DTE2)serviceProvider.GetService(typeof(SDTE));
49 
50  // Register to Visual Studio theme change
51  shellService = serviceProvider.GetService(typeof(SVsShell)) as IVsShell;
52  if (shellService != null)
53  ErrorHandler.ThrowOnFailure(shellService.AdviseBroadcastMessages(this, out broadcastCookie));
54  }
55 
56  public void Dispose()
57  {
58  if (shellService != null && broadcastCookie != 0)
59  {
60  shellService.UnadviseBroadcastMessages(broadcastCookie);
61  broadcastCookie = 0;
62  }
63  }
64 
65  public VisualStudioTheme GetCurrentTheme()
66  {
67  var currentUser32 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
68  using (var subkey = currentUser32.OpenSubKey(string.Format(@"{0}\General", dte.RegistryRoot)))
69  {
70  if (subkey == null)
71  return VisualStudioTheme.Unknown;
72 
73  var themeValue = (string)subkey.GetValue("CurrentTheme");
74  if (themeValue == null)
75  return VisualStudioTheme.Unknown;
76 
77  Guid themeGuid;
78  if (!Guid.TryParse(themeValue, out themeGuid))
79  return VisualStudioTheme.Unknown;
80 
81  VisualStudioTheme theme;
82  availableThemes.TryGetValue(themeGuid, out theme);
83 
84  return theme;
85  }
86  }
87 
88  public int OnBroadcastMessage(uint msg, IntPtr wParam, IntPtr lParam)
89  {
90  if (msg == WM_SYSCOLORCHANGE)
91  {
92  if (OnThemeChanged != null)
93  {
94  OnThemeChanged(this, EventArgs.Empty);
95  }
96  }
97  return VSConstants.S_OK;
98  }
99  }
100 }