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
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Peter Dennis Bartok (pbartok@novell.com)
// Alexander Olk (alex.olk@googlemail.com)
//
//
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
namespace System.Windows.Forms {
internal class X11DesktopColors {
#region Structs & Enums
[StructLayout(LayoutKind.Sequential)]
internal struct GdkColorStruct {
internal int pixel;
internal short red;
internal short green;
internal short blue;
}
[StructLayout(LayoutKind.Sequential)]
internal struct GtkStyleStruct {
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12)]
internal byte[] obj; /* GObject is 12 bytes*/
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] fg;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] bg;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] light;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] dark;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] mid;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] text;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] baseclr;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
internal GdkColorStruct[] text_aa; /* Halfway between text/base */
internal GdkColorStruct black;
internal GdkColorStruct white;
/* TODO: There is more stuff that we will add when we need it*/
}
private enum Desktop {
Gtk,
KDE,
Unknown
}
#endregion // Structs & Enums
#region Local Variables
static private Desktop desktop;
#endregion // Local Variables
#region Constructors
static X11DesktopColors() {
FindDesktopEnvironment();
switch(desktop) {
case Desktop.Gtk: {
//IntPtr dispmgr;
//IntPtr gdkdisplay;
IntPtr widget;
IntPtr style_ptr;
GtkStyleStruct style;
int argc = 0;
string argv = "";
try {
gtk_init_check (out argc, argv);
//dispmgr = gdk_display_manager_get ();
//gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
gtk_init_check (out argc, argv);
widget = gtk_invisible_new ();
gtk_widget_ensure_style (widget);
style_ptr = gtk_widget_get_style (widget);
style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
ThemeEngine.Current.ColorControl = ColorFromGdkColor (style.bg[0]);
ThemeEngine.Current.ColorControlText = ColorFromGdkColor (style.fg[0]);
ThemeEngine.Current.ColorControlDark = ColorFromGdkColor (style.dark[0]);
ThemeEngine.Current.ColorControlLight = ColorFromGdkColor (style.light[0]);
ThemeEngine.Current.ColorControlLightLight = ControlPaint.Light(ColorFromGdkColor (style.light[0]));
ThemeEngine.Current.ColorControlDarkDark = ControlPaint.Dark(ColorFromGdkColor (style.dark[0]));
widget = gtk_menu_new ();
gtk_widget_ensure_style (widget);
style_ptr = gtk_widget_get_style (widget);
style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
ThemeEngine.Current.ColorMenu = ColorFromGdkColor (style.bg [0]);
ThemeEngine.Current.ColorMenuText = ColorFromGdkColor (style.text [0]);
Console.WriteLine("Gtk colorscheme read");
}
catch (DllNotFoundException) {
Console.WriteLine("Gtk not found (missing LD_LIBRARY_PATH to libgtk-x11-2.0.so?), using built-in colorscheme");
}
catch {
Console.WriteLine("Gtk colorscheme read failure, using built-in colorscheme");
}
break;
}
case Desktop.KDE: {
if ( ReadKDEColorsheme() )
Console.WriteLine("KDE colorscheme read");
else
Console.WriteLine("KDE colorscheme read failure, using built-in colorscheme");
break;
}
default: {
Console.WriteLine("Unknown desktop manager, using default colors");
break;
}
}
}
#endregion // Constructors
#region Properties
static void FindDesktopEnvironment() {
desktop = Desktop.Gtk;
string session = Environment.GetEnvironmentVariable("DESKTOP_SESSION");
if ( session != null ) {
session = session.ToUpper( );
if ( session == "DEFAULT" ) {
string helper = Environment.GetEnvironmentVariable("KDE_FULL_SESSION");
if ( helper != null )
desktop = Desktop.KDE;
} else
if ( session == "KDE" )
desktop = Desktop.KDE;
}
}
#endregion // Properties
#region Methods
static internal void Initialize() {
// Do nothing; all is done in our static ctor
}
private static Color ColorFromGdkColor (GdkColorStruct gtkcolor) {
return Color.FromArgb (255,
(gtkcolor.red >> 8) & 0xff,
(gtkcolor.green >> 8) & 0xff,
(gtkcolor.blue >> 8) & 0xff );
}
private static bool ReadKDEColorsheme() {
string full_kdegloabals_filename = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
+ "/"
+ ".kde/share/config/kdeglobals";
if (!File.Exists(full_kdegloabals_filename))
return false;
StreamReader sr = new StreamReader(full_kdegloabals_filename);
string line = sr.ReadLine();
Color tmp_color;
while (line != null) {
line = line.Trim();
if (line.StartsWith( "background=")) {
tmp_color = GetColorFromKDEString(line);
if (tmp_color != Color.Empty) {
ThemeEngine.Current.ColorControl = tmp_color;
ThemeEngine.Current.ColorMenu = tmp_color;
}
} else
if (line.StartsWith( "foreground=")) {
tmp_color = GetColorFromKDEString(line);
if (tmp_color != Color.Empty) {
ThemeEngine.Current.ColorControlText = tmp_color;
ThemeEngine.Current.ColorMenuText = tmp_color;
}
} else
if (line.StartsWith("selectBackground")) {
tmp_color = GetColorFromKDEString(line);
if (tmp_color != Color.Empty) {
ThemeEngine.Current.ColorHighlight = tmp_color;
}
} else
if (line.StartsWith("selectForeground")) {
tmp_color = GetColorFromKDEString(line);
if (tmp_color != Color.Empty) {
ThemeEngine.Current.ColorHighlightText = tmp_color;
}
}
line = sr.ReadLine();
}
sr.Close();
return true;
}
private static Color GetColorFromKDEString(string line) {
string[] split = line.Split(new char[] {'='});
if (split.Length > 0) {
line = split[1];
split = line.Split(new char[] {','});
if (split.Length == 3) {
int r = System.Convert.ToInt32(split[0]);
int g = System.Convert.ToInt32(split[1]);
int b = System.Convert.ToInt32(split[2]);
return Color.FromArgb(r, g, b);
}
}
return Color.Empty;
}
#endregion // Methods
#region DllImports
[DllImport("libgtk-x11-2.0.so")]
static extern bool gtk_init_check (out int argc, string argv);
[DllImport("libgdk-x11-2.0.so")]
internal static extern IntPtr gdk_display_manager_get ();
[DllImport("libgdk-x11-2.0.so")]
internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
[DllImport("libgtk-x11-2.0.so")]
static extern IntPtr gtk_invisible_new ();
[DllImport("libgtk-x11-2.0.so")]
static extern IntPtr gtk_menu_new ();
[DllImport("libgtk-x11-2.0.so")]
static extern IntPtr gtk_menu_item_new_with_label (string label);
[DllImport("libgtk-x11-2.0.so")]
static extern void gtk_widget_ensure_style (IntPtr raw);
[DllImport("libgtk-x11-2.0.so")]
static extern IntPtr gtk_widget_get_style (IntPtr raw);
#endregion // DllImports
}
}
|