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-2006 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Alexander Olk, alex.olk@googlemail.com
26 // This is an experimental GTK theme.
29 // - For now we would keep all the themes in the same assembly to have
30 // handy the internals methods.
31 // - We are using Pinovoke for now to access GTK/GDK to avoid adding
32 // gtk-sharp as a SWF dependency
33 // - The ThemeGtk comes from ThemeWin32Classic, we use it as the default
34 // implementation for the methods that we are not taking care of.
35 // - When GDK is initialised it opens its own display. There is not way of changing it,
36 // then we use that display as SWF display
37 // - You can activate this Theme in Linux doing export MONO_THEME=gtk
38 // - GTK paints controls into a window no a device context. We should inverstigate if we
39 // 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*/
121 [DllImport("libgdk-x11-2.0.so")]
122 internal static extern IntPtr gdk_display_manager_get ();
124 [DllImport("libgdk-x11-2.0.so")]
125 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);
130 [DllImport("libgdk-x11-2.0.so")]
131 internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
133 [DllImport("libgdk-x11-2.0.so")]
134 static extern IntPtr gdk_window_foreign_new_for_display (IntPtr display, uint anid);
136 [DllImport("libgdk-x11-2.0.so")]
137 static extern bool gdk_init_check(out int argc, string argv);
140 [DllImport("libgtk-x11-2.0.so")]
141 static extern bool gtk_init_check (out int argc, string argv);
143 [DllImport("libgtk-x11-2.0.so")]
144 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);
149 [DllImport("libgtk-x11-2.0.so")]
150 static extern IntPtr gtk_vscrollbar_new(IntPtr adjustment);
152 [DllImport("libgtk-x11-2.0.so")]
153 static extern IntPtr gtk_hscrollbar_new(IntPtr adjustment);
155 [DllImport("libgtk-x11-2.0.so")]
156 static extern IntPtr gtk_style_attach (IntPtr raw, IntPtr window);
158 [DllImport("libgtk-x11-2.0.so")]
159 static extern IntPtr gtk_rc_style_new ();
161 [DllImport("libgtk-x11-2.0.so")]
162 static extern IntPtr gtk_invisible_new ();
164 [DllImport("libgtk-x11-2.0.so")]
165 static extern void gtk_widget_ensure_style (IntPtr raw);
167 [DllImport("libgtk-x11-2.0.so")]
168 static extern IntPtr gtk_widget_get_style (IntPtr raw);
170 [DllImport("libgtk-x11-2.0.so")]
171 static extern void gtk_style_detach (IntPtr raw);
173 [DllImport("libgtk-x11-2.0.so")]
174 static extern IntPtr gtk_button_new ();
177 [DllImport("libgtk-x11-2.0.so")]
178 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);
180 [DllImport("libgtk-x11-2.0.so")]
181 static extern void gtk_paint_arrow (IntPtr style, IntPtr window, int state_type, int shadow_type,
182 IntPtr area, IntPtr widget, string detail, int arrow_type, bool fill, int x, int y, int width, int height);
184 [DllImport("libgtk-x11-2.0.so")]
185 static extern void gtk_paint_slider (IntPtr style, IntPtr window, int state_type, int shadow_type,
186 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height, int orientation);
188 [DllImport("libgtk-x11-2.0.so")]
189 static extern void gtk_paint_box (IntPtr style, IntPtr window, int state_type, int shadow_type,
190 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
192 [DllImport("libgtk-x11-2.0.so")]
193 static extern void gtk_paint_flat_box (IntPtr style, IntPtr window, int state_type, int shadow_type,
194 IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
196 [DllImport("libgtk-x11-2.0.so")]
197 static extern void gtk_paint_hline(IntPtr style, IntPtr window, int state_type, IntPtr area, IntPtr widget, string detail, int x1, int x2, int y);
199 [DllImport("libgtk-x11-2.0.so")]
200 static extern void gtk_paint_vline(IntPtr style, IntPtr window, int state_type, IntPtr area, IntPtr widget, string detail, int y1, int y2, int x);
202 [DllImport("libgtk-x11-2.0.so")]
203 static extern void gtk_paint_check(IntPtr style, IntPtr window, int state_type, int shadow_type, IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
205 [DllImport("libgtk-x11-2.0.so")]
206 static extern void gtk_paint_focus(IntPtr style, IntPtr window, int state_type, IntPtr area, IntPtr widget, string detail, int x, int y, int width, int height);
208 [DllImport("libgtk-x11-2.0.so")]
209 static extern void gtk_widget_size_allocate (IntPtr widget, ref Rectangle allocation);
211 [DllImport("libgtk-x11-2.0.so")]
212 static extern void gtk_widget_set_size_request (IntPtr widget, int width, int height);
215 static protected IntPtr dispmgr;
216 static protected IntPtr gdkdisplay;
217 static protected IntPtr widget;
218 static protected IntPtr style;
219 static protected SolidBrush br_buttonface;
220 static protected SolidBrush br_buttontext;
222 IntPtr global_gtk_button = gtk_button_new();
223 IntPtr global_gtk_vscrollbar = IntPtr.Zero;
224 IntPtr global_gtk_hscrollbar = IntPtr.Zero;
226 IntPtr current_gdk_window = IntPtr.Zero;
227 IntPtr current_style = IntPtr.Zero;
229 public static void InitGtk ()
231 Console.WriteLine ("ThemeGtk Init");
235 gdk_init_check (out argc, argv);
237 dispmgr = gdk_display_manager_get ();
238 gdkdisplay = gdk_display_manager_get_default_display (dispmgr);
239 gtk_init_check (out argc, argv);
241 widget = gtk_invisible_new ();
242 gtk_widget_ensure_style (widget);
243 style = gtk_widget_get_style (widget);
245 XplatUIX11.GetInstance().SetDisplay (gdk_x11_display_get_xdisplay (gdkdisplay));
248 public void LoadSysDefaultColors ()
250 GtkStyleStruct style_struct;
252 style_struct = (GtkStyleStruct) Marshal.PtrToStructure (style, typeof (GtkStyleStruct));
253 defaultWindowBackColor = ColorFromGdkColor (style_struct.bg[0]);
254 defaultWindowForeColor = ColorFromGdkColor (style_struct.fg[0]);
257 public ThemeGtk () : base ()
259 Console.WriteLine ("ThemeGtk constructor");
261 default_font = new Font (FontFamily.GenericSansSerif, 8.25f);
263 LoadSysDefaultColors ();
265 br_buttonface = new SolidBrush (defaultWindowBackColor);
266 br_buttontext = new SolidBrush (defaultWindowForeColor);
267 always_draw_hotkeys = true;
269 IntPtr adj = gtk_adjustment_new (0, 0, 0, 0, 0, 0);
270 global_gtk_vscrollbar = gtk_vscrollbar_new (adj);
271 global_gtk_hscrollbar = gtk_hscrollbar_new (adj);
274 public override bool DoubleBufferingSupported {
278 // public override Color ColorControl {
279 // get { return Color.Blue;}
282 protected override void ButtonBase_DrawButton(ButtonBase button, Graphics dc)
284 Rectangle buttonRectangle = button.ClientRectangle;
286 IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) button.Handle);
290 style = gtk_rc_get_style (global_gtk_button);
291 style = gtk_style_attach (style, gdkwindow); // need it
293 StateType state_type = StateType.Normal;
294 ShadowType shadow_type = button.flat_style == FlatStyle.Flat ? ShadowType.In : ShadowType.Out;
295 string detail = "buttondefault";
297 if (!button.is_enabled) {
298 state_type = StateType.Insensitive;
300 if (button.is_pressed) {
301 state_type = StateType.Active;
302 shadow_type = ShadowType.In;
305 if (button.is_entered) {
306 state_type = StateType.Prelight;
309 if (button.flat_style == FlatStyle.Flat)
310 gtk_paint_flat_box (style, gdkwindow,
316 buttonRectangle.X, buttonRectangle.Y,
317 buttonRectangle.Width, buttonRectangle.Height);
319 if (button.flat_style != FlatStyle.Popup || (button.flat_style == FlatStyle.Popup && button.is_entered))
320 gtk_paint_box (style, gdkwindow,
326 buttonRectangle.X, buttonRectangle.Y,
327 buttonRectangle.Width, buttonRectangle.Height);
330 protected override void ButtonBase_DrawFocus( ButtonBase button, Graphics dc ) {
332 if ( !button.is_enabled)
335 Rectangle focus_rect = new Rectangle( button.ClientRectangle.X + 4, button.ClientRectangle.Y + 4, button.ClientRectangle.Width - 9, button.ClientRectangle.Height - 9 );
337 IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) button.Handle);
341 style = gtk_rc_get_style (global_gtk_button);
342 style = gtk_style_attach (style, gdkwindow); // need it
344 gtk_paint_focus (style,
346 (int) StateType.Active,
357 public override void DrawScrollBar( Graphics dc, Rectangle clip, ScrollBar bar ) {
358 int scrollbutton_width = bar.scrollbutton_width;
359 int scrollbutton_height = bar.scrollbutton_height;
360 Rectangle first_arrow_area;
361 Rectangle second_arrow_area;
364 IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) bar.Handle);
368 Rectangle allocation = new Rectangle (bar.ClientRectangle.X, bar.ClientRectangle.Y, bar.ClientRectangle.Width, bar.ClientRectangle.Height);
371 Color fix_color = bar.Parent != null ? bar.Parent.BackColor : ColorControl;
374 style = gtk_rc_get_style (global_gtk_vscrollbar);
376 gtk_widget_size_allocate (global_gtk_vscrollbar, ref allocation);
379 dc.FillRectangle (ResPool.GetSolidBrush (fix_color),
380 bar.ClientRectangle.X, bar.ClientRectangle.Y, bar.ClientRectangle.Width, 3);
381 dc.FillRectangle (ResPool.GetSolidBrush (fix_color),
382 bar.ClientRectangle.X, bar.ClientRectangle.Bottom - 4, bar.ClientRectangle.Width, 3);
384 style = gtk_rc_get_style (global_gtk_hscrollbar);
386 gtk_widget_size_allocate (global_gtk_hscrollbar, ref allocation);
389 dc.FillRectangle (ResPool.GetSolidBrush (fix_color),
390 bar.ClientRectangle.X, bar.ClientRectangle.Y, 3, bar.ClientRectangle.Height);
391 dc.FillRectangle (ResPool.GetSolidBrush (fix_color),
392 bar.ClientRectangle.Right - 4, bar.ClientRectangle.Y, 3, bar.ClientRectangle.Height);
395 style = gtk_style_attach (style, gdkwindow); // need it
397 current_gdk_window = gdkwindow;
398 current_style = style;
400 thumb_pos = bar.ThumbPos;
403 first_arrow_area = new Rectangle( 0, 0, bar.Width, scrollbutton_height + 1 );
404 bar.FirstArrowArea = first_arrow_area;
406 second_arrow_area = new Rectangle( 0, bar.ClientRectangle.Height - scrollbutton_height - 1, bar.Width, scrollbutton_height + 1 );
407 bar.SecondArrowArea = second_arrow_area;
409 thumb_pos.Width = bar.Width;
410 bar.ThumbPos = thumb_pos;
412 ScrollBar_Vertical_Draw_ThumbMoving_None (scrollbutton_height, bar, clip, dc);
415 if ( clip.IntersectsWith( first_arrow_area ) )
416 CPDrawScrollButton( dc, first_arrow_area, ScrollButton.Up, bar.firstbutton_state );
417 if ( clip.IntersectsWith( second_arrow_area ) )
418 CPDrawScrollButton( dc, second_arrow_area, ScrollButton.Down, bar.secondbutton_state );
420 first_arrow_area = new Rectangle( 0, 0, scrollbutton_width + 1, bar.Height );
421 bar.FirstArrowArea = first_arrow_area;
423 second_arrow_area = new Rectangle( bar.ClientRectangle.Width - scrollbutton_width - 1, 0, scrollbutton_width + 1, bar.Height );
424 bar.SecondArrowArea = second_arrow_area;
426 thumb_pos.Height = bar.Height;
427 bar.ThumbPos = thumb_pos;
430 ScrollBar_Horizontal_Draw_ThumbMoving_None (scrollbutton_width, bar, clip, dc);
433 if ( clip.IntersectsWith( first_arrow_area ) )
434 CPDrawScrollButton( dc, first_arrow_area, ScrollButton.Left, bar.firstbutton_state );
435 if ( clip.IntersectsWith( second_arrow_area ) )
436 CPDrawScrollButton( dc, second_arrow_area, ScrollButton.Right, bar.secondbutton_state );
440 ScrollBar_DrawThumb( bar, thumb_pos, clip, dc );
443 protected override void ScrollBar_DrawThumb( ScrollBar bar, Rectangle thumb_pos, Rectangle clip, Graphics dc ) {
445 DrawScrollBarThumb( dc, thumb_pos, bar );
448 protected override void ScrollBar_Vertical_Draw_ThumbMoving_None (int scrollbutton_height, ScrollBar bar, Rectangle clip, Graphics dc)
450 Rectangle r = new Rectangle (0,
451 scrollbutton_height, bar.ClientRectangle.Width, bar.ClientRectangle.Height - (scrollbutton_height * 2));
452 gtk_paint_box (style,
454 (int) StateType.Active,
457 global_gtk_vscrollbar,
463 protected override void ScrollBar_Horizontal_Draw_ThumbMoving_None (int scrollbutton_width, ScrollBar bar, Rectangle clip, Graphics dc)
465 Rectangle r = new Rectangle (scrollbutton_width,
466 0, bar.ClientRectangle.Width - (scrollbutton_width * 2), bar.ClientRectangle.Height);
468 gtk_paint_box (style,
470 (int) StateType.Active,
473 global_gtk_hscrollbar,
479 private void DrawScrollBarThumb( Graphics dc, Rectangle area, ScrollBar bar ) {
480 IntPtr gtk_scrollbar = bar.vert ? global_gtk_vscrollbar : global_gtk_hscrollbar;
482 gtk_paint_box (style,
484 (int) StateType.Active,
485 (int) ShadowType.Out,
490 area.Width, area.Height);
493 // if ( area.Height > 12 ) {
494 // int mid_y = area.Y + ( area.Height / 2 );
495 // int mid_x = area.X + ( area.Width / 2 );
497 // // TODO: add 2 lines
498 // gtk_paint_hline(current_style,
499 // current_gdk_window,
500 // (int) StateType.Normal,
509 // // draw grip lines only if there is enough space
510 // if ( area.Width > 12 ) {
511 // int mid_x = area.X + ( area.Width / 2 );
512 // int mid_y = area.Y + ( area.Height / 2 );
514 // // TODO: add 2 lines
515 // gtk_paint_vline(current_style,
516 // current_gdk_window,
517 // (int) StateType.Normal,
527 #endregion // ScrollBar
529 public override void CPDrawButton (Graphics graphics, Rectangle rectangle, ButtonState state)
531 bool is_pushed = false;
532 // bool is_checked = false;
533 // bool is_flat = false;
534 bool is_inactive = false;
536 if ((state & ButtonState.Pushed) != 0) {
540 // if ((state & ButtonState.Checked) != 0) {
541 // is_checked = true;
544 // if ((state & ButtonState.Flat) != 0) {
548 if ((state & ButtonState.Inactive) != 0) {
554 style = gtk_rc_get_style (global_gtk_button);
555 style = gtk_style_attach (style, current_gdk_window); // need it
557 StateType state_type = StateType.Normal;
558 ShadowType shadow_type = ShadowType.Out;
559 string detail = "buttondefault";
562 state_type = StateType.Insensitive;
565 state_type = StateType.Active;
566 shadow_type = ShadowType.In;
570 gtk_paint_box (style, current_gdk_window,
576 rectangle.X, rectangle.Y,
577 rectangle.Width, rectangle.Height);
580 /* Scroll button: regular button + direction arrow */
581 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton scroll_button_type, ButtonState state)
583 bool enabled = (state == ButtonState.Inactive) ? false: true;
585 StateType state_type = enabled ? StateType.Normal : StateType.Insensitive;
587 DrawScrollButtonPrimitive (dc, area, state, scroll_button_type);
589 if (area.Width < 12 || area.Height < 12) /* Cannot see a thing at smaller sizes */
592 ArrowType arrow_type = 0;
594 switch (scroll_button_type) {
595 case ScrollButton.Up:
596 arrow_type = ArrowType.Up;
598 case ScrollButton.Down:
599 arrow_type = ArrowType.Down;
601 case ScrollButton.Right:
602 arrow_type = ArrowType.Right;
604 case ScrollButton.Left:
605 arrow_type = ArrowType.Left;
611 int centerX = area.Left + area.Width / 2;
612 int centerY = area.Top + area.Height / 2;
613 int arrow_x = 0, arrow_y = 0, arrow_height = 0, arrow_width = 0;
615 switch (scroll_button_type) {
616 case ScrollButton.Down:
617 case ScrollButton.Up:
618 arrow_x = centerX - 4;
619 arrow_y = centerY - 2;
623 case ScrollButton.Left:
624 case ScrollButton.Right:
625 arrow_x = centerX - 2;
626 arrow_y = centerY - 4;
634 gtk_paint_arrow (style,
637 (int) ShadowType.Out,
641 (int) arrow_type, true,
644 arrow_width, arrow_height);
647 public void DrawScrollButtonPrimitive (Graphics dc, Rectangle area, ButtonState state, ScrollButton scroll_button_type)
649 StateType state_type = StateType.Normal;
650 ShadowType shadow_type = ShadowType.Out;
652 if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
653 state_type = StateType.Active;
654 shadow_type = ShadowType.In;
657 switch (scroll_button_type) {
658 case ScrollButton.Left:
659 case ScrollButton.Right:
660 gtk_paint_box (style,
665 global_gtk_hscrollbar,
668 area.Width, area.Height);
670 case ScrollButton.Up:
671 case ScrollButton.Down:
672 gtk_paint_box (style,
677 global_gtk_vscrollbar,
680 area.Width, area.Height);
685 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor)
687 return Color.FromArgb (255,
688 (gtkcolor.red >> 8) & 0xff,
689 (gtkcolor.green >> 8) & 0xff,
690 (gtkcolor.blue >> 8) & 0xff );