ThemeGtk.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. // This is an experimental GTK theme.
  26. //
  27. // Comments:
  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.
  39. //
  40. // $Revision: 1.3 $
  41. // $Modtime: $
  42. // $Log: ThemeGtk.cs,v $
  43. // Revision 1.3 2004/08/20 20:39:51 jordi
  44. // use style_attach
  45. //
  46. // Revision 1.2 2004/08/20 00:55:28 jordi
  47. // fixes button order
  48. //
  49. // Revision 1.1 2004/08/19 22:27:40 jordi
  50. // experimental GTK theme support
  51. //
  52. //
  53. // NOT COMPLETE
  54. using System;
  55. using System.Drawing;
  56. using System.Drawing.Drawing2D;
  57. using System.Drawing.Imaging;
  58. using System.Reflection;
  59. using System.Runtime.InteropServices;
  60. namespace System.Windows.Forms
  61. {
  62. internal class ThemeGtk : ThemeWin32Classic
  63. {
  64. /* GTK enums */
  65. internal enum StateType
  66. {
  67. Normal,
  68. Active,
  69. Prelight,
  70. Selected,
  71. Insensitive,
  72. }
  73. internal enum ShadowType
  74. {
  75. None,
  76. In,
  77. Out,
  78. EtchedIn,
  79. EtchedOut,
  80. }
  81. internal enum ArrowType
  82. {
  83. Up,
  84. Down,
  85. Left,
  86. Right,
  87. }
  88. /* Structs */
  89. [StructLayout(LayoutKind.Sequential)]
  90. internal struct GdkColorStruct
  91. {
  92. internal int pixel;
  93. internal short red;
  94. internal short green;
  95. internal short blue;
  96. }
  97. [StructLayout(LayoutKind.Sequential)]
  98. internal struct GtkStyleStruct
  99. {
  100. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12)]
  101. internal byte[] obj; /* GObject is 12 bytes*/
  102. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  103. internal GdkColorStruct[] fg;
  104. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  105. internal GdkColorStruct[] bg;
  106. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  107. internal GdkColorStruct[] light;
  108. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  109. internal GdkColorStruct[] dark;
  110. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  111. internal GdkColorStruct[] mid;
  112. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  113. internal GdkColorStruct[] text;
  114. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  115. internal GdkColorStruct[] baseclr;
  116. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=5)]
  117. internal GdkColorStruct[] text_aa; /* Halfway between text/base */
  118. internal GdkColorStruct black;
  119. internal GdkColorStruct white;
  120. /* TODO: There is more stuff that we will add when we need it*/
  121. }
  122. /* GDK imports */
  123. [DllImport("libgdk-x11-2.0.so")]
  124. 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);
  127. [DllImport("libgdk-x11-2.0.so")]
  128. internal static extern void gdk_display_manager_set_default_display (IntPtr display_manager, IntPtr display);
  129. [DllImport("libgdk-x11-2.0.so")]
  130. internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
  131. [DllImport("libgdk-x11-2.0.so")]
  132. static extern IntPtr gdk_window_foreign_new_for_display (IntPtr display, uint anid);
  133. [DllImport("libgdk-x11-2.0.so")]
  134. static extern bool gdk_init_check(out int argc, string argv);
  135. /* GTK imports */
  136. [DllImport("libgtk-x11-2.0.so")]
  137. static extern bool gtk_init_check (out int argc, string argv);
  138. [DllImport("libgtk-x11-2.0.so")]
  139. static extern IntPtr gtk_adjustment_new (double value, double lower, double upper, double step_increment, double page_increment, double page_size);
  140. [DllImport("libgtk-x11-2.0.so")]
  141. static extern IntPtr gtk_rc_get_style (IntPtr widget);
  142. [DllImport("libgtk-x11-2.0.so")]
  143. static extern IntPtr gtk_vscrollbar_new(IntPtr adjustment);
  144. [DllImport("libgtk-x11-2.0.so")]
  145. static extern IntPtr gtk_style_attach (IntPtr raw, IntPtr window);
  146. [DllImport("libgtk-x11-2.0.so")]
  147. static extern IntPtr gtk_rc_style_new ();
  148. [DllImport("libgtk-x11-2.0.so")]
  149. static extern IntPtr gtk_invisible_new ();
  150. [DllImport("libgtk-x11-2.0.so")]
  151. static extern void gtk_widget_ensure_style (IntPtr raw);
  152. [DllImport("libgtk-x11-2.0.so")]
  153. static extern IntPtr gtk_widget_get_style (IntPtr raw);
  154. [DllImport("libgtk-x11-2.0.so")]
  155. static extern void gtk_style_detach (IntPtr raw);
  156. /* GTK Drawing */
  157. [DllImport("libgtk-x11-2.0.so")]
  158. 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);
  159. [DllImport("libgtk-x11-2.0.so")]
  160. static extern void gtk_paint_arrow (IntPtr style, IntPtr window, int state_type, int shadow_type,
  161. IntPtr area, IntPtr widget, string detail, int arrow_type, bool fill, int x, int y, int width, int height);
  162. [DllImport("libgtk-x11-2.0.so")]
  163. static extern void gtk_paint_slider(IntPtr style, IntPtr window, int state_type, int shadow_type,
  164. IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height, int orientation);
  165. [DllImport("libgtk-x11-2.0.so")]
  166. static extern void gtk_paint_box(IntPtr style, IntPtr window, int state_type, int shadow_type,
  167. IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
  168. /* Data */
  169. static protected IntPtr dispmgr;
  170. static protected IntPtr gdkdisplay;
  171. static protected IntPtr widget;
  172. static protected IntPtr style;
  173. public static void InitGtk ()
  174. {
  175. Console.WriteLine ("ThemeGtk Init");
  176. int argc = 0;
  177. string argv = "";
  178. gdk_init_check (out argc, argv);
  179. dispmgr = gdk_display_manager_get ();
  180. gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
  181. gtk_init_check (out argc, argv);
  182. widget = gtk_invisible_new ();
  183. gtk_widget_ensure_style (widget);
  184. style = gtk_widget_get_style (widget);
  185. XplatUIX11.SetDisplay (gdk_x11_display_get_xdisplay (gdkdisplay));
  186. }
  187. public void LoadSysDefaultColors ()
  188. {
  189. GtkStyleStruct style_struct;
  190. style_struct = (GtkStyleStruct) Marshal.PtrToStructure (style, typeof (GtkStyleStruct));
  191. defaultWindowBackColor = ColorFromGdkColor (style_struct.bg[0]);
  192. defaultWindowForeColor = ColorFromGdkColor (style_struct.fg[0]);
  193. }
  194. public ThemeGtk () : base ()
  195. {
  196. Console.WriteLine ("ThemeGtk constructor");
  197. InitGtk ();
  198. default_font = new Font (FontFamily.GenericSansSerif, 8.25f);
  199. LoadSysDefaultColors ();
  200. br_buttonface = new SolidBrush (defaultWindowBackColor);
  201. br_buttontext = new SolidBrush (defaultWindowForeColor);
  202. }
  203. public override bool WriteToWindow {
  204. get {return true; }
  205. }
  206. public void DrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state,
  207. IntPtr gdkwindow, IntPtr style)
  208. {
  209. ArrowType arrow_type = 0;
  210. gtk_paint_box (style,
  211. gdkwindow,
  212. (int) StateType.Normal,
  213. (int) ShadowType.Out,
  214. IntPtr.Zero,
  215. IntPtr.Zero,
  216. "trough",
  217. area.X, area.Y,
  218. area.Width, area.Height);
  219. /* Calc arrows coordinates */
  220. switch (type) {
  221. case ScrollButton.Up:
  222. arrow_type = ArrowType.Up;
  223. break;
  224. case ScrollButton.Down:
  225. arrow_type = ArrowType.Down;
  226. break;
  227. case ScrollButton.Right:
  228. arrow_type = ArrowType.Right;
  229. break;
  230. case ScrollButton.Left:
  231. arrow_type = ArrowType.Left;
  232. break;
  233. default:
  234. break;
  235. }
  236. gtk_paint_arrow (style,
  237. gdkwindow,
  238. (int) StateType.Normal,
  239. (int) ShadowType.In,
  240. IntPtr.Zero,
  241. IntPtr.Zero,
  242. "",
  243. (int) arrow_type, true,
  244. area.X + ((area.Width - (area.Width/2) ) / 2),
  245. area.Y + ((area.Height - (area.Height/2) ) / 2),
  246. area.Width / 2, area.Height / 2);
  247. }
  248. public override void DrawScrollBar (Graphics dc, Rectangle area, ScrollBar bar,
  249. Rectangle thumb_pos, ref Rectangle first_arrow_area, ref Rectangle second_arrow_area,
  250. ButtonState first_arrow, ButtonState second_arrow, ref int scrollbutton_width,
  251. ref int scrollbutton_height, bool vert)
  252. {
  253. IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) bar.Handle);
  254. IntPtr adj = gtk_adjustment_new (0, 0, 0, 0, 0, 0);
  255. IntPtr scrollbar = gtk_vscrollbar_new (adj);
  256. IntPtr style;
  257. style = gtk_rc_get_style (scrollbar);
  258. style = gtk_style_attach (style, gdkwindow); // need it
  259. /* Background */
  260. gtk_paint_box (style,
  261. gdkwindow,
  262. (int) StateType.Active,
  263. (int) ShadowType.In,
  264. IntPtr.Zero,
  265. IntPtr.Zero,
  266. "trough",
  267. area.X, area.Y,
  268. area.Width, area.Height);
  269. /* See gtk_range_expose */
  270. first_arrow_area.X = first_arrow_area. Y = 0;
  271. first_arrow_area.Width = scrollbutton_width;
  272. first_arrow_area.Height = scrollbutton_height;
  273. if (vert) {
  274. second_arrow_area.X = 0;
  275. second_arrow_area.Y = area.Height - scrollbutton_height;
  276. second_arrow_area.Width = scrollbutton_width;
  277. second_arrow_area.Height = scrollbutton_height;
  278. /* First button*/
  279. DrawScrollButton (dc, first_arrow_area, ScrollButton.Up, first_arrow,
  280. gdkwindow, style);
  281. /* Second button*/
  282. DrawScrollButton (dc, second_arrow_area, ScrollButton.Down, second_arrow,
  283. gdkwindow, style);
  284. } else {
  285. second_arrow_area.Y = 0;
  286. second_arrow_area.X = area.Width - scrollbutton_width;
  287. second_arrow_area.Width = scrollbutton_width;
  288. second_arrow_area.Height = scrollbutton_height;
  289. /* First button*/
  290. DrawScrollButton (dc, first_arrow_area, ScrollButton.Left, first_arrow,
  291. gdkwindow, style);
  292. /* Second button*/
  293. DrawScrollButton (dc, second_arrow_area, ScrollButton.Right, second_arrow,
  294. gdkwindow, style);
  295. }
  296. /* Slider */
  297. gtk_paint_slider (style,
  298. gdkwindow,
  299. (int) StateType.Normal,
  300. (int) ShadowType.Out,
  301. IntPtr.Zero,
  302. IntPtr.Zero,
  303. "",
  304. thumb_pos.X, thumb_pos.Y,
  305. thumb_pos.Width, thumb_pos.Height,
  306. (int) Orientation.Vertical);
  307. //gtk_style_detach (style);
  308. }
  309. private static Color ColorFromGdkColor (GdkColorStruct gtkcolor)
  310. {
  311. return Color.FromArgb (255,
  312. (gtkcolor.red >> 8) & 0xff,
  313. (gtkcolor.green >> 8) & 0xff,
  314. (gtkcolor.blue >> 8) & 0xff );
  315. }
  316. } //class
  317. }