* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / X11DesktopColors.cs
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 //
25 //
26
27 using System.Drawing;
28 using System.Runtime.InteropServices;
29
30 namespace System.Windows.Forms {
31         internal class X11DesktopColors {
32                 #region Structs & Enums
33                 [StructLayout(LayoutKind.Sequential)]   
34                 internal struct GdkColorStruct {
35                         internal int pixel;
36                         internal short red;
37                         internal short green;
38                         internal short blue;
39                 }
40
41                 [StructLayout(LayoutKind.Sequential)]
42                 internal struct GtkStyleStruct {
43                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12)]
44                         internal byte[] obj; /* GObject is 12 bytes*/
45                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
46                         internal GdkColorStruct[] fg;
47                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
48                         internal GdkColorStruct[] bg;
49                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
50                         internal GdkColorStruct[] light;
51                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
52                         internal GdkColorStruct[] dark;
53                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
54                         internal GdkColorStruct[] mid;
55                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
56                         internal GdkColorStruct[] text;
57                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
58                         internal GdkColorStruct[] baseclr;
59                         [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
60                         internal GdkColorStruct[] text_aa;              /* Halfway between text/base */
61
62                         internal GdkColorStruct black;
63                         internal GdkColorStruct white;
64
65                         /* TODO: There is more stuff that we will add when we need it*/
66                 }
67
68                 private enum Desktop {
69                         Gtk,
70                         KDE,
71                         Unknown
72                 }
73                 #endregion      // Structs & Enums
74
75                 #region Local Variables
76                 static private Desktop          desktop;
77                 #endregion      // Local Variables
78
79                 #region Constructors
80                 static X11DesktopColors() {
81                         // FIXME - figure out what desktop manager we're using; for now we hardcode gtk
82                         desktop = Desktop.Gtk;
83
84                         switch(desktop) {
85                                 case Desktop.Gtk: {
86                                         //IntPtr                dispmgr;
87                                         //IntPtr                gdkdisplay;
88                                         IntPtr          widget;
89                                         IntPtr          style_ptr;
90                                         GtkStyleStruct  style;
91                                         int             argc = 0;
92                                         string          argv = "";
93
94                                         try {
95                                                 gtk_init_check (out argc, argv);
96                                                 //dispmgr =  gdk_display_manager_get ();
97                                                 //gdkdisplay =  gdk_display_manager_get_default_display (dispmgr);
98                                                 gtk_init_check (out argc, argv);
99
100                                                 widget = gtk_invisible_new ();
101                                                 gtk_widget_ensure_style (widget);
102                                                 style_ptr = gtk_widget_get_style (widget);
103
104                                                 style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
105                                                 
106                                                 ThemeEngine.Current.ColorControl = ColorFromGdkColor (style.bg[0]);
107                                                 ThemeEngine.Current.ColorControlText = ColorFromGdkColor (style.fg[0]);
108                                                 ThemeEngine.Current.ColorControlDark = ColorFromGdkColor (style.dark[0]);
109                                                 ThemeEngine.Current.ColorControlLight = ColorFromGdkColor (style.light[0]);
110                                                 ThemeEngine.Current.ColorControlLightLight = ControlPaint.Light(ColorFromGdkColor (style.light[0]));
111                                                 ThemeEngine.Current.ColorControlDarkDark = ControlPaint.Dark(ColorFromGdkColor (style.dark[0]));
112
113                                                 widget = gtk_menu_new ();
114                                                 gtk_widget_ensure_style (widget);
115                                                 style_ptr = gtk_widget_get_style (widget);
116
117                                                 style = (GtkStyleStruct) Marshal.PtrToStructure (style_ptr, typeof (GtkStyleStruct));
118
119                                                 ThemeEngine.Current.ColorMenu = ColorFromGdkColor (style.bg [0]);
120                                                 ThemeEngine.Current.ColorMenuText = ColorFromGdkColor (style.text [0]);
121                                                 Console.WriteLine("Gtk colorscheme read");
122                                         }
123
124                                         catch (DllNotFoundException) {
125                                                 Console.WriteLine("Gtk not found (missing LD_LIBRARY_PATH to libgtk-x11-2.0.so?), using built-in colorscheme");
126                                         }
127
128                                         catch {
129                                                 Console.WriteLine("Gtk colorscheme read failure, using built-in colorscheme");
130                                         }
131                                         break;
132                                 }
133
134                                 default: {
135                                         Console.WriteLine("Unknown destop manager, using default colors");
136                                         break;
137                                 }
138                         }
139                 }
140                 #endregion      // Constructors
141
142                 #region Properties
143                 #endregion      // Properties
144
145                 #region Methods
146                 static internal void Initialize() {
147                         // Do nothing; all is done in our static ctor
148                 }
149
150                 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor) {
151                         return Color.FromArgb (255, 
152                                 (gtkcolor.red >> 8)  & 0xff, 
153                                 (gtkcolor.green  >> 8) & 0xff,
154                                 (gtkcolor.blue >> 8) & 0xff );
155                 }
156                 
157                 #endregion      // Methods
158
159                 #region DllImports
160                 [DllImport("libgtk-x11-2.0.so")]
161                 static extern bool gtk_init_check (out int argc, string argv);
162
163                 [DllImport("libgdk-x11-2.0.so")]
164                 internal static extern IntPtr gdk_display_manager_get ();
165
166                 [DllImport("libgdk-x11-2.0.so")]
167                 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
168
169                 [DllImport("libgtk-x11-2.0.so")]
170                 static extern IntPtr gtk_invisible_new ();
171
172                 [DllImport("libgtk-x11-2.0.so")]
173                 static extern IntPtr gtk_menu_new ();
174
175                 [DllImport("libgtk-x11-2.0.so")]
176                 static extern IntPtr gtk_menu_item_new_with_label (string label);
177
178                 [DllImport("libgtk-x11-2.0.so")]
179                 static extern void gtk_widget_ensure_style (IntPtr raw);
180
181                 [DllImport("libgtk-x11-2.0.so")]
182                 static extern IntPtr gtk_widget_get_style (IntPtr raw);
183                 #endregion      // DllImports
184         }
185 }