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;
33 namespace System.Windows.Forms {
34 internal class X11DesktopColors {
35 #region Structs & Enums
36 [StructLayout(LayoutKind.Sequential)]
37 internal struct GdkColorStruct {
44 [StructLayout(LayoutKind.Sequential)]
45 internal struct GObjectStruct {
46 public IntPtr Instance;
47 public IntPtr ref_count;
51 [StructLayout(LayoutKind.Sequential)]
52 internal struct GtkStyleStruct {
53 internal GObjectStruct obj;
54 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
55 internal GdkColorStruct[] fg;
56 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
57 internal GdkColorStruct[] bg;
58 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
59 internal GdkColorStruct[] light;
60 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
61 internal GdkColorStruct[] dark;
62 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
63 internal GdkColorStruct[] mid;
64 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
65 internal GdkColorStruct[] text;
66 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
67 internal GdkColorStruct[] baseclr;
68 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
69 internal GdkColorStruct[] text_aa; /* Halfway between text/base */
71 internal GdkColorStruct black;
72 internal GdkColorStruct white;
74 /* TODO: There is more stuff that we will add when we need it*/
77 private enum Desktop {
82 #endregion // Structs & Enums
84 #region Local Variables
85 static private Desktop desktop;
86 #endregion // Local Variables
89 static X11DesktopColors() {
90 FindDesktopEnvironment();
102 //dispmgr = gdk_display_manager_get ();
103 //gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
105 widget = gtk_invisible_new ();
106 gtk_widget_ensure_style (widget);
107 style_ptr = gtk_widget_get_style (widget);
109 style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
111 ThemeEngine.Current.ColorControl = ColorFromGdkColor (style.bg[0]);
112 ThemeEngine.Current.ColorControlText = ColorFromGdkColor (style.fg[0]);
113 ThemeEngine.Current.ColorControlDark = ColorFromGdkColor (style.dark[0]);
114 ThemeEngine.Current.ColorControlLight = ColorFromGdkColor (style.light[0]);
115 ThemeEngine.Current.ColorControlLightLight = ControlPaint.Light (ThemeEngine.Current.ColorControlLight);
116 ThemeEngine.Current.ColorControlDarkDark = ControlPaint.Dark (ThemeEngine.Current.ColorControlDark);
118 // We don't want ControlLight to disappear on a white background!
119 if (ThemeEngine.Current.ColorControlLight.ToArgb () == Color.White.ToArgb ()) {
120 ThemeEngine.Current.ColorControlLight = Color.FromArgb (255, 227, 227, 227);
122 widget = gtk_menu_new ();
123 gtk_widget_ensure_style (widget);
124 style_ptr = gtk_widget_get_style (widget);
126 style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
128 ThemeEngine.Current.ColorMenu = ColorFromGdkColor (style.bg [0]);
129 ThemeEngine.Current.ColorMenuText = ColorFromGdkColor (style.text [0]);
132 catch (DllNotFoundException) {
133 Console.Error.WriteLine("Gtk not found (missing LD_LIBRARY_PATH to libgtk-x11-2.0.so.0?), using built-in colorscheme");
137 Console.Error.WriteLine("Gtk colorscheme read failure, using built-in colorscheme");
143 if (! ReadKDEColorsheme() )
144 Console.Error.WriteLine("KDE colorscheme read failure, using built-in colorscheme");
154 static void GtkInit ()
156 gtk_init_check (IntPtr.Zero, IntPtr.Zero);
158 #endregion // Constructors
161 static void FindDesktopEnvironment() {
162 desktop = Desktop.Gtk;
163 string session = Environment.GetEnvironmentVariable("DESKTOP_SESSION");
165 if ( session != null ) {
166 session = session.ToUpper( );
168 if ( session == "DEFAULT" ) {
169 string helper = Environment.GetEnvironmentVariable("KDE_FULL_SESSION");
171 if ( helper != null )
172 desktop = Desktop.KDE;
174 if ( session.StartsWith("KDE") )
175 desktop = Desktop.KDE;
178 #endregion // Properties
181 static internal void Initialize() {
182 // Do nothing; all is done in our static ctor
185 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor) {
186 return Color.FromArgb (255,
187 (gtkcolor.red >> 8) & 0xff,
188 (gtkcolor.green >> 8) & 0xff,
189 (gtkcolor.blue >> 8) & 0xff );
192 private static bool ReadKDEColorsheme() {
193 string full_kdegloabals_filename = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
195 + ".kde/share/config/kdeglobals";
197 if (!File.Exists(full_kdegloabals_filename))
200 StreamReader sr = new StreamReader(full_kdegloabals_filename);
202 string line = sr.ReadLine();
206 while (line != null) {
209 if (line.StartsWith( "background=")) {
210 tmp_color = GetColorFromKDEString(line);
212 if (tmp_color != Color.Empty) {
213 ThemeEngine.Current.ColorControl = tmp_color;
214 ThemeEngine.Current.ColorMenu = tmp_color;
217 if (line.StartsWith( "foreground=")) {
218 tmp_color = GetColorFromKDEString(line);
220 if (tmp_color != Color.Empty) {
221 ThemeEngine.Current.ColorControlText = tmp_color;
222 ThemeEngine.Current.ColorMenuText = tmp_color;
225 if (line.StartsWith("selectBackground")) {
226 tmp_color = GetColorFromKDEString(line);
228 if (tmp_color != Color.Empty) {
229 ThemeEngine.Current.ColorHighlight = tmp_color;
232 if (line.StartsWith("selectForeground")) {
233 tmp_color = GetColorFromKDEString(line);
235 if (tmp_color != Color.Empty) {
236 ThemeEngine.Current.ColorHighlightText = tmp_color;
240 line = sr.ReadLine();
248 private static Color GetColorFromKDEString(string line) {
249 string[] split = line.Split(new char[] {'='});
251 if (split.Length > 0) {
254 split = line.Split(new char[] {','});
256 if (split.Length == 3) {
257 int r = System.Convert.ToInt32(split[0]);
258 int g = System.Convert.ToInt32(split[1]);
259 int b = System.Convert.ToInt32(split[2]);
261 return Color.FromArgb(r, g, b);
267 #endregion // Methods
270 const string libgdk = "libgdk-x11-2.0.so.0";
271 const string libgtk = "libgtk-x11-2.0.so.0";
274 static extern bool gtk_init_check (IntPtr argc, IntPtr argv);
277 internal static extern IntPtr gdk_display_manager_get ();
280 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
283 static extern IntPtr gtk_invisible_new ();
286 static extern IntPtr gtk_menu_new ();
288 //[DllImport(libgtk)]
289 //static extern IntPtr gtk_menu_item_new_with_label (string label);
292 static extern void gtk_widget_ensure_style (IntPtr raw);
295 static extern IntPtr gtk_widget_get_style (IntPtr raw);
296 #endregion // DllImports