ThemeGtk.cs 12 KB

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