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) 2004 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
25 // This is an experimental GTK theme.
28 // - For now we would keep all the themes in the same assembly to have
29 // handy the internals methods.
30 // - We are using Pinovoke for now to access GTK/GDK to avoid adding
31 // gtk-sharp as a SWF dependency
32 // - The ThemeGtk comes from ThemeWin32Classic, we use it as the default
33 // implementation for the methods that we are not taking care of.
34 // - When GDK is initialised it opens its own display. There is not way of changing it,
35 // then we use that display as SWF display
36 // - You can activate this Theme in Linux doing export MONO_THEME=gtk
37 // - GTK paints controls into a window no a device context. We should inverstigate if we
38 // we can encapsulate a dc in a gtkwindow.
46 using System.Drawing.Drawing2D;
47 using System.Drawing.Imaging;
48 using System.Reflection;
49 using System.Runtime.InteropServices;
51 namespace System.Windows.Forms
53 internal class ThemeGtk : ThemeWin32Classic
56 internal enum StateType
65 internal enum ShadowType
74 internal enum ArrowType
83 [StructLayout(LayoutKind.Sequential)]
84 internal struct GdkColorStruct
92 [StructLayout(LayoutKind.Sequential)]
93 internal struct GtkStyleStruct
95 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12)]
96 internal byte[] obj; /* GObject is 12 bytes*/
97 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
98 internal GdkColorStruct[] fg;
99 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
100 internal GdkColorStruct[] bg;
101 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
102 internal GdkColorStruct[] light;
103 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
104 internal GdkColorStruct[] dark;
105 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
106 internal GdkColorStruct[] mid;
107 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
108 internal GdkColorStruct[] text;
109 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
110 internal GdkColorStruct[] baseclr;
111 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
112 internal GdkColorStruct[] text_aa; /* Halfway between text/base */
114 internal GdkColorStruct black;
115 internal GdkColorStruct white;
117 /* TODO: There is more stuff that we will add when we need it*/
122 [DllImport("libgdk-x11-2.0.so")]
123 internal static extern IntPtr gdk_display_manager_get ();
125 [DllImport("libgdk-x11-2.0.so")]
126 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
128 [DllImport("libgdk-x11-2.0.so")]
129 internal static extern void gdk_display_manager_set_default_display (IntPtr display_manager, IntPtr display);
131 [DllImport("libgdk-x11-2.0.so")]
132 internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
134 [DllImport("libgdk-x11-2.0.so")]
135 static extern IntPtr gdk_window_foreign_new_for_display (IntPtr display, uint anid);
137 [DllImport("libgdk-x11-2.0.so")]
138 static extern bool gdk_init_check(out int argc, string argv);
141 [DllImport("libgtk-x11-2.0.so")]
142 static extern bool gtk_init_check (out int argc, string argv);
144 [DllImport("libgtk-x11-2.0.so")]
145 static extern IntPtr gtk_adjustment_new (double value, double lower, double upper, double step_increment, double page_increment, double page_size);
147 [DllImport("libgtk-x11-2.0.so")]
148 static extern IntPtr gtk_rc_get_style (IntPtr widget);
150 [DllImport("libgtk-x11-2.0.so")]
151 static extern IntPtr gtk_vscrollbar_new(IntPtr adjustment);
153 [DllImport("libgtk-x11-2.0.so")]
154 static extern IntPtr gtk_style_attach (IntPtr raw, IntPtr window);
156 [DllImport("libgtk-x11-2.0.so")]
157 static extern IntPtr gtk_rc_style_new ();
159 [DllImport("libgtk-x11-2.0.so")]
160 static extern IntPtr gtk_invisible_new ();
162 [DllImport("libgtk-x11-2.0.so")]
163 static extern void gtk_widget_ensure_style (IntPtr raw);
165 [DllImport("libgtk-x11-2.0.so")]
166 static extern IntPtr gtk_widget_get_style (IntPtr raw);
168 [DllImport("libgtk-x11-2.0.so")]
169 static extern void gtk_style_detach (IntPtr raw);
172 [DllImport("libgtk-x11-2.0.so")]
173 static extern void gtk_paint_handle (IntPtr style, IntPtr window, int state_type, int shadow_type, IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height, int orientation);
175 [DllImport("libgtk-x11-2.0.so")]
176 static extern void gtk_paint_arrow (IntPtr style, IntPtr window, int state_type, int shadow_type,
177 IntPtr area, IntPtr widget, string detail, int arrow_type, bool fill, int x, int y, int width, int height);
179 [DllImport("libgtk-x11-2.0.so")]
180 static extern void gtk_paint_slider(IntPtr style, IntPtr window, int state_type, int shadow_type,
181 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height, int orientation);
183 [DllImport("libgtk-x11-2.0.so")]
184 static extern void gtk_paint_box(IntPtr style, IntPtr window, int state_type, int shadow_type,
185 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
188 static protected IntPtr dispmgr;
189 static protected IntPtr gdkdisplay;
190 static protected IntPtr widget;
191 static protected IntPtr style;
192 static protected SolidBrush br_buttonface;
193 static protected SolidBrush br_buttontext;
195 public static void InitGtk ()
197 Console.WriteLine ("ThemeGtk Init");
201 gdk_init_check (out argc, argv);
203 dispmgr = gdk_display_manager_get ();
204 gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
205 gtk_init_check (out argc, argv);
207 widget = gtk_invisible_new ();
208 gtk_widget_ensure_style (widget);
209 style = gtk_widget_get_style (widget);
211 XplatUIX11.GetInstance().SetDisplay (gdk_x11_display_get_xdisplay (gdkdisplay));
214 public void LoadSysDefaultColors ()
216 GtkStyleStruct style_struct;
218 style_struct = (GtkStyleStruct) Marshal.PtrToStructure (style, typeof (GtkStyleStruct));
219 defaultWindowBackColor = ColorFromGdkColor (style_struct.bg[0]);
220 defaultWindowForeColor = ColorFromGdkColor (style_struct.fg[0]);
223 public ThemeGtk () : base ()
225 Console.WriteLine ("ThemeGtk constructor");
227 default_font = new Font (FontFamily.GenericSansSerif, 8.25f);
229 LoadSysDefaultColors ();
231 br_buttonface = new SolidBrush (defaultWindowBackColor);
232 br_buttontext = new SolidBrush (defaultWindowForeColor);
235 public override bool DoubleBufferingSupported {
240 public void DrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state,
241 IntPtr gdkwindow, IntPtr style)
243 ArrowType arrow_type = 0;
245 gtk_paint_box (style,
247 (int) StateType.Normal,
248 (int) ShadowType.Out,
253 area.Width, area.Height);
255 /* Calc arrows coordinates */
257 case ScrollButton.Up:
258 arrow_type = ArrowType.Up;
260 case ScrollButton.Down:
261 arrow_type = ArrowType.Down;
263 case ScrollButton.Right:
264 arrow_type = ArrowType.Right;
266 case ScrollButton.Left:
267 arrow_type = ArrowType.Left;
274 gtk_paint_arrow (style,
276 (int) StateType.Normal,
281 (int) arrow_type, true,
282 area.X + ((area.Width - (area.Width/2) ) / 2),
283 area.Y + ((area.Height - (area.Height/2) ) / 2),
284 area.Width / 2, area.Height / 2);
288 public override void DrawScrollBar (Graphics dc, Rectangle area, ScrollBar bar,
289 ref Rectangle thumb_pos, ref Rectangle first_arrow_area, ref Rectangle second_arrow_area,
290 ButtonState first_arrow, ButtonState second_arrow, ref int scrollbutton_width,
291 ref int scrollbutton_height, bool vert)
293 IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) bar.Handle);
294 IntPtr adj = gtk_adjustment_new (0, 0, 0, 0, 0, 0);
295 IntPtr scrollbar = gtk_vscrollbar_new (adj);
298 style = gtk_rc_get_style (scrollbar);
299 style = gtk_style_attach (style, gdkwindow); // need it
302 gtk_paint_box (style,
304 (int) StateType.Active,
310 area.Width, area.Height);
312 /* See gtk_range_expose */
313 first_arrow_area.X = first_arrow_area. Y = 0;
314 first_arrow_area.Width = scrollbutton_width;
315 first_arrow_area.Height = scrollbutton_height;
319 second_arrow_area.X = 0;
320 second_arrow_area.Y = area.Height - scrollbutton_height;
321 second_arrow_area.Width = scrollbutton_width;
322 second_arrow_area.Height = scrollbutton_height;
325 DrawScrollButton (dc, first_arrow_area, ScrollButton.Up, first_arrow,
329 DrawScrollButton (dc, second_arrow_area, ScrollButton.Down, second_arrow,
334 second_arrow_area.Y = 0;
335 second_arrow_area.X = area.Width - scrollbutton_width;
336 second_arrow_area.Width = scrollbutton_width;
337 second_arrow_area.Height = scrollbutton_height;
340 DrawScrollButton (dc, first_arrow_area, ScrollButton.Left, first_arrow,
344 DrawScrollButton (dc, second_arrow_area, ScrollButton.Right, second_arrow,
350 gtk_paint_slider (style,
352 (int) StateType.Normal,
353 (int) ShadowType.Out,
357 thumb_pos.X, thumb_pos.Y,
358 thumb_pos.Width, thumb_pos.Height,
359 (int) Orientation.Vertical);
361 //gtk_style_detach (style);
365 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor)
367 return Color.FromArgb (255,
368 (gtkcolor.red >> 8) & 0xff,
369 (gtkcolor.green >> 8) & 0xff,
370 (gtkcolor.blue >> 8) & 0xff );