PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Managed.Windows.Forms/System.Windows.Forms/X11DesktopColors.cs

https://github.com/jeffora/mono
C# | 294 lines | 203 code | 57 blank | 34 comment | 26 complexity | c7aef57992bf843388054075ad568667 MD5 | raw file
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Dennis Bartok (pbartok@novell.com)
  24. // Alexander Olk (alex.olk@googlemail.com)
  25. //
  26. //
  27. using System.Drawing;
  28. using System.Runtime.InteropServices;
  29. using System.IO;
  30. using System;
  31. namespace System.Windows.Forms {
  32. internal class X11DesktopColors {
  33. #region Structs & Enums
  34. [StructLayout(LayoutKind.Sequential)]
  35. internal struct GdkColorStruct {
  36. internal int pixel;
  37. internal short red;
  38. internal short green;
  39. internal short blue;
  40. }
  41. [StructLayout(LayoutKind.Sequential)]
  42. internal struct GObjectStruct {
  43. public IntPtr Instance;
  44. public IntPtr ref_count;
  45. public IntPtr data;
  46. }
  47. [StructLayout(LayoutKind.Sequential)]
  48. internal struct GtkStyleStruct {
  49. internal GObjectStruct obj;
  50. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  51. internal GdkColorStruct[] fg;
  52. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  53. internal GdkColorStruct[] bg;
  54. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  55. internal GdkColorStruct[] light;
  56. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  57. internal GdkColorStruct[] dark;
  58. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  59. internal GdkColorStruct[] mid;
  60. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  61. internal GdkColorStruct[] text;
  62. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  63. internal GdkColorStruct[] baseclr;
  64. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  65. internal GdkColorStruct[] text_aa; /* Halfway between text/base */
  66. internal GdkColorStruct black;
  67. internal GdkColorStruct white;
  68. /* TODO: There is more stuff that we will add when we need it*/
  69. }
  70. private enum Desktop {
  71. Gtk,
  72. KDE,
  73. Unknown
  74. }
  75. #endregion // Structs & Enums
  76. #region Local Variables
  77. static private Desktop desktop;
  78. #endregion // Local Variables
  79. #region Constructors
  80. static X11DesktopColors() {
  81. FindDesktopEnvironment();
  82. switch(desktop) {
  83. case Desktop.Gtk: {
  84. //IntPtr dispmgr;
  85. //IntPtr gdkdisplay;
  86. IntPtr widget;
  87. IntPtr style_ptr;
  88. GtkStyleStruct style;
  89. try {
  90. GtkInit();
  91. //dispmgr = gdk_display_manager_get ();
  92. //gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
  93. widget = gtk_invisible_new ();
  94. gtk_widget_ensure_style (widget);
  95. style_ptr = gtk_widget_get_style (widget);
  96. style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
  97. ThemeEngine.Current.ColorControl = ColorFromGdkColor (style.bg[0]);
  98. ThemeEngine.Current.ColorControlText = ColorFromGdkColor (style.fg[0]);
  99. ThemeEngine.Current.ColorControlDark = ColorFromGdkColor (style.dark[0]);
  100. ThemeEngine.Current.ColorControlLight = ColorFromGdkColor (style.light[0]);
  101. ThemeEngine.Current.ColorControlLightLight = ControlPaint.Light(ColorFromGdkColor (style.light[0]));
  102. ThemeEngine.Current.ColorControlDarkDark = ControlPaint.Dark(ColorFromGdkColor (style.dark[0]));
  103. widget = gtk_menu_new ();
  104. gtk_widget_ensure_style (widget);
  105. style_ptr = gtk_widget_get_style (widget);
  106. style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
  107. ThemeEngine.Current.ColorMenu = ColorFromGdkColor (style.bg [0]);
  108. ThemeEngine.Current.ColorMenuText = ColorFromGdkColor (style.text [0]);
  109. }
  110. catch (DllNotFoundException) {
  111. Console.Error.WriteLine("Gtk not found (missing LD_LIBRARY_PATH to libgtk-x11-2.0.so.0?), using built-in colorscheme");
  112. }
  113. catch {
  114. Console.Error.WriteLine("Gtk colorscheme read failure, using built-in colorscheme");
  115. }
  116. break;
  117. }
  118. case Desktop.KDE: {
  119. if (! ReadKDEColorsheme() )
  120. Console.Error.WriteLine("KDE colorscheme read failure, using built-in colorscheme");
  121. break;
  122. }
  123. default: {
  124. break;
  125. }
  126. }
  127. }
  128. static void GtkInit ()
  129. {
  130. gtk_init_check (IntPtr.Zero, IntPtr.Zero);
  131. }
  132. #endregion // Constructors
  133. #region Properties
  134. static void FindDesktopEnvironment() {
  135. desktop = Desktop.Gtk;
  136. string session = Environment.GetEnvironmentVariable("DESKTOP_SESSION");
  137. if ( session != null ) {
  138. session = session.ToUpper( );
  139. if ( session == "DEFAULT" ) {
  140. string helper = Environment.GetEnvironmentVariable("KDE_FULL_SESSION");
  141. if ( helper != null )
  142. desktop = Desktop.KDE;
  143. } else
  144. if ( session.StartsWith("KDE") )
  145. desktop = Desktop.KDE;
  146. }
  147. }
  148. #endregion // Properties
  149. #region Methods
  150. static internal void Initialize() {
  151. // Do nothing; all is done in our static ctor
  152. }
  153. private static Color ColorFromGdkColor (GdkColorStruct gtkcolor) {
  154. return Color.FromArgb (255,
  155. (gtkcolor.red >> 8) & 0xff,
  156. (gtkcolor.green >> 8) & 0xff,
  157. (gtkcolor.blue >> 8) & 0xff );
  158. }
  159. private static bool ReadKDEColorsheme() {
  160. string full_kdegloabals_filename = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
  161. + "/"
  162. + ".kde/share/config/kdeglobals";
  163. if (!File.Exists(full_kdegloabals_filename))
  164. return false;
  165. StreamReader sr = new StreamReader(full_kdegloabals_filename);
  166. string line = sr.ReadLine();
  167. Color tmp_color;
  168. while (line != null) {
  169. line = line.Trim();
  170. if (line.StartsWith( "background=")) {
  171. tmp_color = GetColorFromKDEString(line);
  172. if (tmp_color != Color.Empty) {
  173. ThemeEngine.Current.ColorControl = tmp_color;
  174. ThemeEngine.Current.ColorMenu = tmp_color;
  175. }
  176. } else
  177. if (line.StartsWith( "foreground=")) {
  178. tmp_color = GetColorFromKDEString(line);
  179. if (tmp_color != Color.Empty) {
  180. ThemeEngine.Current.ColorControlText = tmp_color;
  181. ThemeEngine.Current.ColorMenuText = tmp_color;
  182. }
  183. } else
  184. if (line.StartsWith("selectBackground")) {
  185. tmp_color = GetColorFromKDEString(line);
  186. if (tmp_color != Color.Empty) {
  187. ThemeEngine.Current.ColorHighlight = tmp_color;
  188. }
  189. } else
  190. if (line.StartsWith("selectForeground")) {
  191. tmp_color = GetColorFromKDEString(line);
  192. if (tmp_color != Color.Empty) {
  193. ThemeEngine.Current.ColorHighlightText = tmp_color;
  194. }
  195. }
  196. line = sr.ReadLine();
  197. }
  198. sr.Close();
  199. return true;
  200. }
  201. private static Color GetColorFromKDEString(string line) {
  202. string[] split = line.Split(new char[] {'='});
  203. if (split.Length > 0) {
  204. line = split[1];
  205. split = line.Split(new char[] {','});
  206. if (split.Length == 3) {
  207. int r = System.Convert.ToInt32(split[0]);
  208. int g = System.Convert.ToInt32(split[1]);
  209. int b = System.Convert.ToInt32(split[2]);
  210. return Color.FromArgb(r, g, b);
  211. }
  212. }
  213. return Color.Empty;
  214. }
  215. #endregion // Methods
  216. #region DllImports
  217. const string libgdk = "libgdk-x11-2.0.so.0";
  218. const string libgtk = "libgtk-x11-2.0.so.0";
  219. [DllImport(libgtk)]
  220. static extern bool gtk_init_check (IntPtr argc, IntPtr argv);
  221. [DllImport(libgdk)]
  222. internal static extern IntPtr gdk_display_manager_get ();
  223. [DllImport(libgdk)]
  224. internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
  225. [DllImport(libgtk)]
  226. static extern IntPtr gtk_invisible_new ();
  227. [DllImport(libgtk)]
  228. static extern IntPtr gtk_menu_new ();
  229. //[DllImport(libgtk)]
  230. //static extern IntPtr gtk_menu_item_new_with_label (string label);
  231. [DllImport(libgtk)]
  232. static extern void gtk_widget_ensure_style (IntPtr raw);
  233. [DllImport(libgtk)]
  234. static extern IntPtr gtk_widget_get_style (IntPtr raw);
  235. #endregion // DllImports
  236. }
  237. }