use style_attach
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ThemeGtk.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) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
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
55 using System;
56 using System.Drawing;
57 using System.Drawing.Drawing2D;
58 using System.Drawing.Imaging;
59 using System.Reflection;
60 using System.Runtime.InteropServices;
61
62 namespace System.Windows.Forms
63 {
64         internal class ThemeGtk : ThemeWin32Classic
65         {               
66                 /* GTK enums */
67                 internal enum StateType 
68                 {
69                         Normal,
70                         Active,
71                         Prelight,
72                         Selected,
73                         Insensitive,
74                 }       
75
76                 internal enum ShadowType 
77                 {
78                         None,
79                         In,
80                         Out,
81                         EtchedIn,
82                         EtchedOut,
83                 }       
84
85                 internal enum ArrowType 
86                 {
87                         Up,
88                         Down,
89                         Left,
90                         Right,
91                 }
92
93                 /* Structs */
94                 [StructLayout(LayoutKind.Sequential)]   
95                 internal struct GdkColorStruct
96                 {
97                         internal int pixel;
98                         internal short red;
99                         internal short green;
100                         internal short blue;
101                 }
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   
125                         internal GdkColorStruct black;
126                         internal GdkColorStruct white;
127
128                         /* TODO: There is more stuff that we will add when we need it*/
129                 }
130
131                         
132                 /* GDK imports */
133                 [DllImport("libgdk-x11-2.0.so")]
134                 internal static extern IntPtr gdk_display_manager_get ();
135
136                 [DllImport("libgdk-x11-2.0.so")]
137                 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
138
139                 [DllImport("libgdk-x11-2.0.so")]
140                 internal static extern void gdk_display_manager_set_default_display (IntPtr display_manager, IntPtr display);
141
142                 [DllImport("libgdk-x11-2.0.so")]
143                 internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
144
145                 [DllImport("libgdk-x11-2.0.so")]
146                 static extern IntPtr gdk_window_foreign_new_for_display (IntPtr display, uint anid);
147
148                 [DllImport("libgdk-x11-2.0.so")]
149                 static extern bool gdk_init_check(out int argc, string argv);   
150
151                 /* GTK imports */               
152                 [DllImport("libgtk-x11-2.0.so")]
153                 static extern bool gtk_init_check (out int argc, string argv);
154
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);
157
158                 [DllImport("libgtk-x11-2.0.so")]
159                 static extern IntPtr gtk_rc_get_style (IntPtr widget);
160
161                 [DllImport("libgtk-x11-2.0.so")]
162                 static extern IntPtr gtk_vscrollbar_new(IntPtr adjustment);
163
164                 [DllImport("libgtk-x11-2.0.so")]
165                 static extern IntPtr gtk_style_attach (IntPtr raw, IntPtr window);
166
167                 [DllImport("libgtk-x11-2.0.so")]
168                 static extern IntPtr gtk_rc_style_new ();
169
170                 [DllImport("libgtk-x11-2.0.so")]
171                 static extern IntPtr gtk_invisible_new ();
172
173                 [DllImport("libgtk-x11-2.0.so")]
174                 static extern void gtk_widget_ensure_style (IntPtr raw);
175
176                 [DllImport("libgtk-x11-2.0.so")]
177                 static extern IntPtr gtk_widget_get_style (IntPtr raw);
178
179                 [DllImport("libgtk-x11-2.0.so")]
180                 static extern void gtk_style_detach (IntPtr raw);
181
182                 /* GTK Drawing */
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);
185
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);
189
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);
193
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);
197
198                 /* Data */
199                 static protected IntPtr dispmgr;
200                 static protected IntPtr gdkdisplay;
201                 static protected IntPtr widget;
202                 static protected IntPtr style;
203
204                 public static void InitGtk ()
205                 {       
206                         Console.WriteLine ("ThemeGtk Init");            
207                         int argc = 0;
208                         string argv = "";
209                         
210                         gdk_init_check (out argc, argv);        
211
212                         dispmgr =  gdk_display_manager_get ();
213                         gdkdisplay =  gdk_display_manager_get_default_display (dispmgr);
214                         gtk_init_check (out argc, argv);
215
216                         widget = gtk_invisible_new ();
217                         gtk_widget_ensure_style (widget);
218                         style = gtk_widget_get_style (widget);                  
219
220                         XplatUIX11.SetDisplay (gdk_x11_display_get_xdisplay (gdkdisplay));
221                 }
222
223                 public void LoadSysDefaultColors ()
224                 {
225                         GtkStyleStruct style_struct;                    
226                         
227                         style_struct = (GtkStyleStruct) Marshal.PtrToStructure (style, typeof (GtkStyleStruct));                        
228                         defaultWindowBackColor = ColorFromGdkColor (style_struct.bg[0]);
229                         defaultWindowForeColor = ColorFromGdkColor (style_struct.fg[0]);
230                 }
231
232                 public ThemeGtk () : base ()
233                 {
234                         Console.WriteLine ("ThemeGtk constructor");
235                         InitGtk ();
236                         default_font =  new Font (FontFamily.GenericSansSerif, 8.25f);
237                         
238                         LoadSysDefaultColors ();        
239
240                         br_buttonface = new SolidBrush (defaultWindowBackColor);                
241                         br_buttontext = new SolidBrush (defaultWindowForeColor);                        
242                 }       
243
244                 public override bool WriteToWindow {
245                         get {return true; }
246                 }       
247
248
249                 public void DrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state,
250                         IntPtr gdkwindow, IntPtr style)
251                 {
252                         ArrowType arrow_type = 0;
253
254                         gtk_paint_box (style, 
255                                         gdkwindow, 
256                                         (int) StateType.Normal,
257                                         (int) ShadowType.Out,
258                                         IntPtr.Zero,
259                                         IntPtr.Zero,
260                                         "trough",                                       
261                                         area.X, area.Y,
262                                         area.Width, area.Height);
263                         
264                         /* Calc arrows coordinates */
265                         switch (type) {
266                         case ScrollButton.Up:
267                                 arrow_type = ArrowType.Up;
268                                 break;
269                         case ScrollButton.Down:
270                                 arrow_type = ArrowType.Down;
271                                 break;
272                         case ScrollButton.Right:
273                                 arrow_type = ArrowType.Right;
274                                 break;
275                         case ScrollButton.Left:
276                                 arrow_type = ArrowType.Left;
277                                 break;
278                         default:
279                                 break;
280                         }
281                                 
282
283                         gtk_paint_arrow (style, 
284                                         gdkwindow, 
285                                         (int) StateType.Normal,
286                                         (int) ShadowType.In,
287                                         IntPtr.Zero,
288                                         IntPtr.Zero,
289                                         "",             
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);                       
294                 
295                 }
296                 
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)
301                 {
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);
305                         IntPtr style;
306                                        
307                         style = gtk_rc_get_style (scrollbar);           
308                         style = gtk_style_attach (style, gdkwindow);  // need it                        
309                         
310                         /* Background */
311                         gtk_paint_box (style, 
312                                 gdkwindow, 
313                                 (int) StateType.Active,
314                                 (int) ShadowType.In,
315                                 IntPtr.Zero,
316                                 IntPtr.Zero,
317                                 "trough",
318                                 area.X, area.Y,
319                                 area.Width, area.Height);
320
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;
325
326                         if (vert) {             
327
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;
332
333                                 /* First button*/
334                                 DrawScrollButton (dc, first_arrow_area, ScrollButton.Up, first_arrow,
335                                         gdkwindow, style);
336
337                                 /* Second button*/
338                                 DrawScrollButton (dc, second_arrow_area, ScrollButton.Down, second_arrow,
339                                         gdkwindow, style);
340
341                         } else {
342
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;
347
348                                 /* First button*/
349                                 DrawScrollButton (dc, first_arrow_area, ScrollButton.Left, first_arrow,
350                                         gdkwindow, style);
351
352                                 /* Second button*/
353                                 DrawScrollButton (dc, second_arrow_area, ScrollButton.Right, second_arrow,
354                                         gdkwindow, style);
355
356                         }
357
358                         /* Slider */
359                         gtk_paint_slider (style, 
360                                 gdkwindow, 
361                                 (int) StateType.Normal,
362                                 (int) ShadowType.Out,
363                                 IntPtr.Zero,
364                                 IntPtr.Zero,
365                                 "",             
366                                 thumb_pos.X, thumb_pos.Y,
367                                 thumb_pos.Width, thumb_pos.Height,
368                                 (int) Orientation.Vertical);
369
370                         //gtk_style_detach (style);
371                 }
372
373                 
374                 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor)
375                 {
376                         return Color.FromArgb (255, 
377                                 (gtkcolor.red >> 8)  & 0xff, 
378                                 (gtkcolor.green  >> 8) & 0xff,
379                                 (gtkcolor.blue >> 8) & 0xff );
380                 }
381
382         } //class
383 }