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.
42 // $Log: ThemeGtk.cs,v $
43 // Revision 1.3 2004/08/20 20:39:51 jordi
46 // Revision 1.2 2004/08/20 00:55:28 jordi
49 // Revision 1.1 2004/08/19 22:27:40 jordi
50 // experimental GTK theme support
57 using System.Drawing.Drawing2D;
58 using System.Drawing.Imaging;
59 using System.Reflection;
60 using System.Runtime.InteropServices;
62 namespace System.Windows.Forms
64 internal class ThemeGtk : ThemeWin32Classic
67 internal enum StateType
76 internal enum ShadowType
85 internal enum ArrowType
94 [StructLayout(LayoutKind.Sequential)]
95 internal struct GdkColorStruct
103 [StructLayout(LayoutKind.Sequential)]
104 internal struct GtkStyleStruct
106 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12)]
107 internal byte[] obj; /* GObject is 12 bytes*/
108 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
109 internal GdkColorStruct[] fg;
110 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
111 internal GdkColorStruct[] bg;
112 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
113 internal GdkColorStruct[] light;
114 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
115 internal GdkColorStruct[] dark;
116 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
117 internal GdkColorStruct[] mid;
118 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
119 internal GdkColorStruct[] text;
120 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
121 internal GdkColorStruct[] baseclr;
122 [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
123 internal GdkColorStruct[] text_aa; /* Halfway between text/base */
125 internal GdkColorStruct black;
126 internal GdkColorStruct white;
128 /* TODO: There is more stuff that we will add when we need it*/
133 [DllImport("libgdk-x11-2.0.so")]
134 internal static extern IntPtr gdk_display_manager_get ();
136 [DllImport("libgdk-x11-2.0.so")]
137 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
139 [DllImport("libgdk-x11-2.0.so")]
140 internal static extern void gdk_display_manager_set_default_display (IntPtr display_manager, IntPtr display);
142 [DllImport("libgdk-x11-2.0.so")]
143 internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
145 [DllImport("libgdk-x11-2.0.so")]
146 static extern IntPtr gdk_window_foreign_new_for_display (IntPtr display, uint anid);
148 [DllImport("libgdk-x11-2.0.so")]
149 static extern bool gdk_init_check(out int argc, string argv);
152 [DllImport("libgtk-x11-2.0.so")]
153 static extern bool gtk_init_check (out int argc, string argv);
155 [DllImport("libgtk-x11-2.0.so")]
156 static extern IntPtr gtk_adjustment_new (double value, double lower, double upper, double step_increment, double page_increment, double page_size);
158 [DllImport("libgtk-x11-2.0.so")]
159 static extern IntPtr gtk_rc_get_style (IntPtr widget);
161 [DllImport("libgtk-x11-2.0.so")]
162 static extern IntPtr gtk_vscrollbar_new(IntPtr adjustment);
164 [DllImport("libgtk-x11-2.0.so")]
165 static extern IntPtr gtk_style_attach (IntPtr raw, IntPtr window);
167 [DllImport("libgtk-x11-2.0.so")]
168 static extern IntPtr gtk_rc_style_new ();
170 [DllImport("libgtk-x11-2.0.so")]
171 static extern IntPtr gtk_invisible_new ();
173 [DllImport("libgtk-x11-2.0.so")]
174 static extern void gtk_widget_ensure_style (IntPtr raw);
176 [DllImport("libgtk-x11-2.0.so")]
177 static extern IntPtr gtk_widget_get_style (IntPtr raw);
179 [DllImport("libgtk-x11-2.0.so")]
180 static extern void gtk_style_detach (IntPtr raw);
183 [DllImport("libgtk-x11-2.0.so")]
184 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);
186 [DllImport("libgtk-x11-2.0.so")]
187 static extern void gtk_paint_arrow (IntPtr style, IntPtr window, int state_type, int shadow_type,
188 IntPtr area, IntPtr widget, string detail, int arrow_type, bool fill, int x, int y, int width, int height);
190 [DllImport("libgtk-x11-2.0.so")]
191 static extern void gtk_paint_slider(IntPtr style, IntPtr window, int state_type, int shadow_type,
192 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height, int orientation);
194 [DllImport("libgtk-x11-2.0.so")]
195 static extern void gtk_paint_box(IntPtr style, IntPtr window, int state_type, int shadow_type,
196 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
199 static protected IntPtr dispmgr;
200 static protected IntPtr gdkdisplay;
201 static protected IntPtr widget;
202 static protected IntPtr style;
204 public static void InitGtk ()
206 Console.WriteLine ("ThemeGtk Init");
210 gdk_init_check (out argc, argv);
212 dispmgr = gdk_display_manager_get ();
213 gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
214 gtk_init_check (out argc, argv);
216 widget = gtk_invisible_new ();
217 gtk_widget_ensure_style (widget);
218 style = gtk_widget_get_style (widget);
220 XplatUIX11.SetDisplay (gdk_x11_display_get_xdisplay (gdkdisplay));
223 public void LoadSysDefaultColors ()
225 GtkStyleStruct style_struct;
227 style_struct = (GtkStyleStruct) Marshal.PtrToStructure (style, typeof (GtkStyleStruct));
228 defaultWindowBackColor = ColorFromGdkColor (style_struct.bg[0]);
229 defaultWindowForeColor = ColorFromGdkColor (style_struct.fg[0]);
232 public ThemeGtk () : base ()
234 Console.WriteLine ("ThemeGtk constructor");
236 default_font = new Font (FontFamily.GenericSansSerif, 8.25f);
238 LoadSysDefaultColors ();
240 br_buttonface = new SolidBrush (defaultWindowBackColor);
241 br_buttontext = new SolidBrush (defaultWindowForeColor);
244 public override bool WriteToWindow {
249 public void DrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state,
250 IntPtr gdkwindow, IntPtr style)
252 ArrowType arrow_type = 0;
254 gtk_paint_box (style,
256 (int) StateType.Normal,
257 (int) ShadowType.Out,
262 area.Width, area.Height);
264 /* Calc arrows coordinates */
266 case ScrollButton.Up:
267 arrow_type = ArrowType.Up;
269 case ScrollButton.Down:
270 arrow_type = ArrowType.Down;
272 case ScrollButton.Right:
273 arrow_type = ArrowType.Right;
275 case ScrollButton.Left:
276 arrow_type = ArrowType.Left;
283 gtk_paint_arrow (style,
285 (int) StateType.Normal,
290 (int) arrow_type, true,
291 area.X + ((area.Width - (area.Width/2) ) / 2),
292 area.Y + ((area.Height - (area.Height/2) ) / 2),
293 area.Width / 2, area.Height / 2);
297 public override void DrawScrollBar (Graphics dc, Rectangle area, ScrollBar bar,
298 Rectangle thumb_pos, ref Rectangle first_arrow_area, ref Rectangle second_arrow_area,
299 ButtonState first_arrow, ButtonState second_arrow, ref int scrollbutton_width,
300 ref int scrollbutton_height, bool vert)
302 IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) bar.Handle);
303 IntPtr adj = gtk_adjustment_new (0, 0, 0, 0, 0, 0);
304 IntPtr scrollbar = gtk_vscrollbar_new (adj);
307 style = gtk_rc_get_style (scrollbar);
308 style = gtk_style_attach (style, gdkwindow); // need it
311 gtk_paint_box (style,
313 (int) StateType.Active,
319 area.Width, area.Height);
321 /* See gtk_range_expose */
322 first_arrow_area.X = first_arrow_area. Y = 0;
323 first_arrow_area.Width = scrollbutton_width;
324 first_arrow_area.Height = scrollbutton_height;
328 second_arrow_area.X = 0;
329 second_arrow_area.Y = area.Height - scrollbutton_height;
330 second_arrow_area.Width = scrollbutton_width;
331 second_arrow_area.Height = scrollbutton_height;
334 DrawScrollButton (dc, first_arrow_area, ScrollButton.Up, first_arrow,
338 DrawScrollButton (dc, second_arrow_area, ScrollButton.Down, second_arrow,
343 second_arrow_area.Y = 0;
344 second_arrow_area.X = area.Width - scrollbutton_width;
345 second_arrow_area.Width = scrollbutton_width;
346 second_arrow_area.Height = scrollbutton_height;
349 DrawScrollButton (dc, first_arrow_area, ScrollButton.Left, first_arrow,
353 DrawScrollButton (dc, second_arrow_area, ScrollButton.Right, second_arrow,
359 gtk_paint_slider (style,
361 (int) StateType.Normal,
362 (int) ShadowType.Out,
366 thumb_pos.X, thumb_pos.Y,
367 thumb_pos.Width, thumb_pos.Height,
368 (int) Orientation.Vertical);
370 //gtk_style_detach (style);
374 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor)
376 return Color.FromArgb (255,
377 (gtkcolor.red >> 8) & 0xff,
378 (gtkcolor.green >> 8) & 0xff,
379 (gtkcolor.blue >> 8) & 0xff );