1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Dennis Bartok (pbartok@novell.com)
24 // Alexander Olk (alex.olk@googlemail.com)
29 using System.Runtime.InteropServices;
32 namespace System.Windows.Forms {
33 internal class X11DesktopColors {
34 #region Structs & Enums
35 [StructLayout(LayoutKind.Sequential)]
36 internal struct GdkColorStruct {
43 [StructLayout(LayoutKind.Sequential)]
44 internal struct GObjectStruct {
45 public IntPtr Instance;
46 public IntPtr ref_count;
50 [StructLayout(LayoutKind.Sequential)]
51 internal struct GtkStyleStruct {
52 internal GObjectStruct obj;
53 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
54 internal GdkColorStruct[] fg;
55 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
56 internal GdkColorStruct[] bg;
57 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
58 internal GdkColorStruct[] light;
59 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
60 internal GdkColorStruct[] dark;
61 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
62 internal GdkColorStruct[] mid;
63 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
64 internal GdkColorStruct[] text;
65 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
66 internal GdkColorStruct[] baseclr;
67 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
68 internal GdkColorStruct[] text_aa; /* Halfway between text/base */
70 internal GdkColorStruct black;
71 internal GdkColorStruct white;
73 /* TODO: There is more stuff that we will add when we need it*/
76 private enum Desktop {
81 #endregion // Structs & Enums
83 #region Local Variables
84 static private Desktop desktop;
85 #endregion // Local Variables
88 static X11DesktopColors() {
89 FindDesktopEnvironment();
100 gtk_init_check (IntPtr.Zero, IntPtr.Zero);
101 //dispmgr = gdk_display_manager_get ();
102 //gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
104 widget = gtk_invisible_new ();
105 gtk_widget_ensure_style (widget);
106 style_ptr = gtk_widget_get_style (widget);
108 style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
110 ThemeEngine.Current.ColorControl = ColorFromGdkColor (style.bg[0]);
111 ThemeEngine.Current.ColorControlText = ColorFromGdkColor (style.fg[0]);
112 ThemeEngine.Current.ColorControlDark = ColorFromGdkColor (style.dark[0]);
113 ThemeEngine.Current.ColorControlLight = ColorFromGdkColor (style.light[0]);
114 ThemeEngine.Current.ColorControlLightLight = ControlPaint.Light(ColorFromGdkColor (style.light[0]));
115 ThemeEngine.Current.ColorControlDarkDark = ControlPaint.Dark(ColorFromGdkColor (style.dark[0]));
117 widget = gtk_menu_new ();
118 gtk_widget_ensure_style (widget);
119 style_ptr = gtk_widget_get_style (widget);
121 style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
123 ThemeEngine.Current.ColorMenu = ColorFromGdkColor (style.bg [0]);
124 ThemeEngine.Current.ColorMenuText = ColorFromGdkColor (style.text [0]);
127 catch (DllNotFoundException) {
128 Console.Error.WriteLine("Gtk not found (missing LD_LIBRARY_PATH to libgtk-x11-2.0.so.0?), using built-in colorscheme");
132 Console.Error.WriteLine("Gtk colorscheme read failure, using built-in colorscheme");
138 if (! ReadKDEColorsheme() )
139 Console.Error.WriteLine("KDE colorscheme read failure, using built-in colorscheme");
148 #endregion // Constructors
151 static void FindDesktopEnvironment() {
152 desktop = Desktop.Gtk;
153 string session = Environment.GetEnvironmentVariable("DESKTOP_SESSION");
155 if ( session != null ) {
156 session = session.ToUpper( );
158 if ( session == "DEFAULT" ) {
159 string helper = Environment.GetEnvironmentVariable("KDE_FULL_SESSION");
161 if ( helper != null )
162 desktop = Desktop.KDE;
164 if ( session.StartsWith("KDE") )
165 desktop = Desktop.KDE;
168 #endregion // Properties
171 static internal void Initialize() {
172 // Do nothing; all is done in our static ctor
175 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor) {
176 return Color.FromArgb (255,
177 (gtkcolor.red >> 8) & 0xff,
178 (gtkcolor.green >> 8) & 0xff,
179 (gtkcolor.blue >> 8) & 0xff );
182 private static bool ReadKDEColorsheme() {
183 string full_kdegloabals_filename = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
185 + ".kde/share/config/kdeglobals";
187 if (!File.Exists(full_kdegloabals_filename))
190 StreamReader sr = new StreamReader(full_kdegloabals_filename);
192 string line = sr.ReadLine();
196 while (line != null) {
199 if (line.StartsWith( "background=")) {
200 tmp_color = GetColorFromKDEString(line);
202 if (tmp_color != Color.Empty) {
203 ThemeEngine.Current.ColorControl = tmp_color;
204 ThemeEngine.Current.ColorMenu = tmp_color;
207 if (line.StartsWith( "foreground=")) {
208 tmp_color = GetColorFromKDEString(line);
210 if (tmp_color != Color.Empty) {
211 ThemeEngine.Current.ColorControlText = tmp_color;
212 ThemeEngine.Current.ColorMenuText = tmp_color;
215 if (line.StartsWith("selectBackground")) {
216 tmp_color = GetColorFromKDEString(line);
218 if (tmp_color != Color.Empty) {
219 ThemeEngine.Current.ColorHighlight = tmp_color;
222 if (line.StartsWith("selectForeground")) {
223 tmp_color = GetColorFromKDEString(line);
225 if (tmp_color != Color.Empty) {
226 ThemeEngine.Current.ColorHighlightText = tmp_color;
230 line = sr.ReadLine();
238 private static Color GetColorFromKDEString(string line) {
239 string[] split = line.Split(new char[] {'='});
241 if (split.Length > 0) {
244 split = line.Split(new char[] {','});
246 if (split.Length == 3) {
247 int r = System.Convert.ToInt32(split[0]);
248 int g = System.Convert.ToInt32(split[1]);
249 int b = System.Convert.ToInt32(split[2]);
251 return Color.FromArgb(r, g, b);
257 #endregion // Methods
260 const string libgdk = "libgdk-x11-2.0.so.0";
261 const string libgtk = "libgtk-x11-2.0.so.0";
264 static extern bool gtk_init_check (IntPtr argc, IntPtr argv);
267 internal static extern IntPtr gdk_display_manager_get ();
270 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
273 static extern IntPtr gtk_invisible_new ();
276 static extern IntPtr gtk_menu_new ();
278 //[DllImport(libgtk)]
279 //static extern IntPtr gtk_menu_item_new_with_label (string label);
282 static extern void gtk_widget_ensure_style (IntPtr raw);
285 static extern IntPtr gtk_widget_get_style (IntPtr raw);
286 #endregion // DllImports