forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeManager.cs
More file actions
176 lines (167 loc) · 5.62 KB
/
Copy pathThemeManager.cs
File metadata and controls
176 lines (167 loc) · 5.62 KB
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
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Collections.Generic;
using PluginCore.Localization;
using PluginCore.Managers;
using System.Collections;
using PluginCore.Helpers;
namespace FlashDevelop.Managers
{
class ThemeManager
{
/// <summary>
/// Dictionary containing the loaded theme values
/// </summary>
private static Dictionary<String, String> valueMap = new Dictionary<String, String>();
/// <summary>
/// Gets a value entry from the config.
/// </summary>
public static String GetThemeValue(String id)
{
String result;
if (valueMap.TryGetValue(id, out result)) return result;
else return null;
}
/// <summary>
/// Gets a color entry from the config.
/// </summary>
public static Color GetThemeColor(String id)
{
try { return ColorTranslator.FromHtml(GetThemeValue(id)); }
catch { return Color.Empty; }
}
/// <summary>
/// Loads and applies the theme to MainForm.
/// </summary>
public static void LoadTheme(String file)
{
try
{
if (File.Exists(file))
{
valueMap.Clear();
String[] lines = File.ReadAllLines(file);
foreach (String rawLine in lines)
{
String line = rawLine.Trim();
if (line.Length < 2 || line.StartsWith("#")) continue;
String[] entry = line.Split(new Char[] { '=' }, 2);
if (entry.Length < 2) continue;
valueMap[entry[0]] = entry[1];
}
String currentFile = Path.Combine(PathHelper.ThemesDir, "CURRENT");
if (file != currentFile) File.Copy(file, currentFile, true);
WalkControls(Globals.MainForm);
}
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
/// <summary>
/// Walks the control tree down and themes all controls.
/// </summary>
public static void WalkControls(Object obj)
{
try
{
if (obj is ListView)
{
ListView parent = obj as ListView;
foreach (ListViewItem item in parent.Items)
{
WalkControls(item);
}
}
else if (obj is TreeView)
{
TreeView parent = obj as TreeView;
foreach (TreeNode item in parent.Nodes)
{
WalkControls(item);
}
}
else if (obj is MenuStrip)
{
MenuStrip parent = obj as MenuStrip;
foreach (ToolStripItem item in parent.Items)
{
WalkControls(item);
}
}
else if (obj is ToolStripMenuItem)
{
ToolStripMenuItem parent = obj as ToolStripMenuItem;
foreach (ToolStripItem item in parent.DropDownItems)
{
WalkControls(item);
}
}
else if (obj is Control)
{
Control parent = obj as Control;
foreach (Control control in parent.Controls)
{
WalkControls(control);
}
}
ThemeControl(obj);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
/// <summary>
/// Applies the theme colors to the control.
/// </summary>
public static void ThemeControl(Object obj)
{
try
{
Type type = obj.GetType();
String full = type.Name;
String name = full.EndsWith("Ex") ? full.Remove(full.Length - 2) : full;
PropertyInfo ground = type.GetProperty("BackgroundColor");
PropertyInfo back = type.GetProperty("BackColor");
PropertyInfo fore = type.GetProperty("ForeColor");
if (back != null)
{
String key = name + ".BackColor";
Color color = GetThemeColor(key);
if (color != Color.Empty)
{
back.SetValue(obj, color, null);
}
}
if (fore != null)
{
String key = name + ".ForeColor";
Color color = GetThemeColor(key);
if (color != Color.Empty)
{
fore.SetValue(obj, color, null);
}
}
if (ground != null)
{
String key = name + ".BackgroundColor";
Color color = GetThemeColor(key);
if (color != Color.Empty)
{
ground.SetValue(obj, color, null);
}
}
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
}
}