2006-01-18 Alexander Olk <alex.olk@googlemail.com>
[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-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
24 //      Alexander Olk, alex.olk@googlemail.com
25 //
26 //      This is an experimental GTK theme. 
27 //
28 //      Comments:
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.
40
41
42 // NOT COMPLETE
43
44 using System;
45 using System.Drawing;
46 using System.Drawing.Drawing2D;
47 using System.Drawing.Imaging;
48 using System.Reflection;
49 using System.Runtime.InteropServices;
50
51 namespace System.Windows.Forms
52 {
53         internal class ThemeGtk : ThemeWin32Classic
54         {               
55                 /* GTK enums */
56                 internal enum StateType 
57                 {
58                         Normal,
59                         Active,
60                         Prelight,
61                         Selected,
62                         Insensitive,
63                 }       
64
65                 internal enum ShadowType 
66                 {
67                         None,
68                         In,
69                         Out,
70                         EtchedIn,
71                         EtchedOut,
72                 }       
73
74                 internal enum ArrowType 
75                 {
76                         Up,
77                         Down,
78                         Left,
79                         Right,
80                 }
81
82                 /* Structs */
83                 [StructLayout(LayoutKind.Sequential)]   
84                 internal struct GdkColorStruct
85                 {
86                         internal int pixel;
87                         internal short red;
88                         internal short green;
89                         internal short blue;
90                 }
91
92                 [StructLayout(LayoutKind.Sequential)]   
93                 internal struct GtkStyleStruct
94                 {
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 */
113   
114                         internal GdkColorStruct black;
115                         internal GdkColorStruct white;
116
117                         /* TODO: There is more stuff that we will add when we need it*/
118                 }
119                 
120                 /* GDK imports */
121                 [DllImport("libgdk-x11-2.0.so")]
122                 internal static extern IntPtr gdk_display_manager_get ();
123
124                 [DllImport("libgdk-x11-2.0.so")]
125                 internal static extern IntPtr gdk_display_manager_get_default_display (IntPtr display_manager);
126
127                 [DllImport("libgdk-x11-2.0.so")]
128                 internal static extern void gdk_display_manager_set_default_display (IntPtr display_manager, IntPtr display);
129
130                 [DllImport("libgdk-x11-2.0.so")]
131                 internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
132
133                 [DllImport("libgdk-x11-2.0.so")]
134                 static extern IntPtr gdk_window_foreign_new_for_display (IntPtr display, uint anid);
135
136                 [DllImport("libgdk-x11-2.0.so")]
137                 static extern bool gdk_init_check(out int argc, string argv);   
138
139                 /* GTK imports */               
140                 [DllImport("libgtk-x11-2.0.so")]
141                 static extern bool gtk_init_check (out int argc, string argv);
142
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);
145
146                 [DllImport("libgtk-x11-2.0.so")]
147                 static extern IntPtr gtk_rc_get_style (IntPtr widget);
148
149                 [DllImport("libgtk-x11-2.0.so")]
150                 static extern IntPtr gtk_vscrollbar_new(IntPtr adjustment);
151                 
152                 [DllImport("libgtk-x11-2.0.so")]
153                 static extern IntPtr gtk_hscrollbar_new(IntPtr adjustment);
154
155                 [DllImport("libgtk-x11-2.0.so")]
156                 static extern IntPtr gtk_style_attach (IntPtr raw, IntPtr window);
157
158                 [DllImport("libgtk-x11-2.0.so")]
159                 static extern IntPtr gtk_rc_style_new ();
160
161                 [DllImport("libgtk-x11-2.0.so")]
162                 static extern IntPtr gtk_invisible_new ();
163
164                 [DllImport("libgtk-x11-2.0.so")]
165                 static extern void gtk_widget_ensure_style (IntPtr raw);
166
167                 [DllImport("libgtk-x11-2.0.so")]
168                 static extern IntPtr gtk_widget_get_style (IntPtr raw);
169
170                 [DllImport("libgtk-x11-2.0.so")]
171                 static extern void gtk_style_detach (IntPtr raw);
172                 
173                 [DllImport("libgtk-x11-2.0.so")]
174                 static extern IntPtr  gtk_button_new ();
175
176                 /* GTK Drawing */
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);
179
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);
183
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);
187
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);
191                 
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);
195                 
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);
198                 
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);
201                 
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);
204                 
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);
207                 
208                 [DllImport("libgtk-x11-2.0.so")]
209                 static extern void gtk_widget_size_allocate (IntPtr widget, ref Rectangle allocation);
210                 
211                 [DllImport("libgtk-x11-2.0.so")]
212                 static extern void gtk_widget_set_size_request (IntPtr widget, int width, int height);
213
214                 /* Data */
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;
221                 
222                 IntPtr global_gtk_button = gtk_button_new();
223                 IntPtr global_gtk_vscrollbar = IntPtr.Zero;
224                 IntPtr global_gtk_hscrollbar = IntPtr.Zero;
225                 
226                 IntPtr current_gdk_window = IntPtr.Zero;
227                 IntPtr current_style = IntPtr.Zero;
228
229                 public static void InitGtk ()
230                 {       
231                         Console.WriteLine ("ThemeGtk Init");            
232                         int argc = 0;
233                         string argv = "";
234                         
235                         gdk_init_check (out argc, argv);        
236
237                         dispmgr =  gdk_display_manager_get ();
238                         gdkdisplay =  gdk_display_manager_get_default_display (dispmgr);
239                         gtk_init_check (out argc, argv);
240
241                         widget = gtk_invisible_new ();
242                         gtk_widget_ensure_style (widget);
243                         style = gtk_widget_get_style (widget);                  
244
245                         XplatUIX11.GetInstance().SetDisplay (gdk_x11_display_get_xdisplay (gdkdisplay));
246                 }
247
248                 public void LoadSysDefaultColors ()
249                 {
250                         GtkStyleStruct style_struct;                    
251                         
252                         style_struct = (GtkStyleStruct) Marshal.PtrToStructure (style, typeof (GtkStyleStruct));                        
253                         defaultWindowBackColor = ColorFromGdkColor (style_struct.bg[0]);
254                         defaultWindowForeColor = ColorFromGdkColor (style_struct.fg[0]);
255                 }
256
257                 public ThemeGtk () : base ()
258                 {
259                         Console.WriteLine ("ThemeGtk constructor");
260                         InitGtk ();
261                         default_font =  new Font (FontFamily.GenericSansSerif, 8.25f);
262                         
263                         LoadSysDefaultColors ();        
264
265                         br_buttonface = new SolidBrush (defaultWindowBackColor);                
266                         br_buttontext = new SolidBrush (defaultWindowForeColor);                        
267                         always_draw_hotkeys = true;
268                         
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);
272                 }       
273
274                 public override bool DoubleBufferingSupported {
275                         get {return false; }
276                 }
277                 
278 //              public override Color ColorControl {
279 //                      get { return Color.Blue;}
280 //              }
281                 
282                 protected override void ButtonBase_DrawButton(ButtonBase button, Graphics dc)
283                 {
284                         Rectangle buttonRectangle = button.ClientRectangle;
285                         
286                         IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) button.Handle);
287                         
288                         IntPtr style;
289                         
290                         style = gtk_rc_get_style (global_gtk_button);           
291                         style = gtk_style_attach (style, gdkwindow);  // need it
292                         
293                         StateType state_type = StateType.Normal;
294                         ShadowType shadow_type = button.flat_style == FlatStyle.Flat ? ShadowType.In : ShadowType.Out;
295                         string detail = "buttondefault";
296                         
297                         if (!button.is_enabled) {
298                                 state_type = StateType.Insensitive;
299                         } else
300                         if (button.is_pressed) {
301                                 state_type = StateType.Active;
302                                 shadow_type = ShadowType.In;
303                                 detail = "button";
304                         } else
305                         if (button.is_entered) {
306                                 state_type = StateType.Prelight;
307                         }
308                         
309                         if (button.flat_style == FlatStyle.Flat)
310                                 gtk_paint_flat_box (style, gdkwindow,
311                                                     (int) state_type,
312                                                     (int) shadow_type,
313                                                     IntPtr.Zero,
314                                                     IntPtr.Zero,
315                                                     detail,
316                                                     buttonRectangle.X, buttonRectangle.Y,
317                                                     buttonRectangle.Width, buttonRectangle.Height);
318                         else
319                         if (button.flat_style != FlatStyle.Popup || (button.flat_style == FlatStyle.Popup && button.is_entered))
320                                 gtk_paint_box (style, gdkwindow,
321                                                (int) state_type,
322                                                (int) shadow_type,
323                                                IntPtr.Zero,
324                                                IntPtr.Zero,
325                                                detail,
326                                                buttonRectangle.X, buttonRectangle.Y,
327                                                buttonRectangle.Width, buttonRectangle.Height);
328                 }
329                 
330                 protected override void ButtonBase_DrawFocus( ButtonBase button, Graphics dc ) {
331                         
332                         if ( !button.is_enabled)
333                                 return;
334                         
335                         Rectangle focus_rect = new Rectangle( button.ClientRectangle.X + 4, button.ClientRectangle.Y + 4, button.ClientRectangle.Width - 9, button.ClientRectangle.Height - 9 );
336                         
337                         IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) button.Handle);
338                         
339                         IntPtr style;
340                         
341                         style = gtk_rc_get_style (global_gtk_button);           
342                         style = gtk_style_attach (style, gdkwindow);  // need it
343                         
344                         gtk_paint_focus (style,
345                                          gdkwindow,
346                                          (int) StateType.Active,
347                                          IntPtr.Zero,
348                                          IntPtr.Zero,
349                                          "button",
350                                          focus_rect.X,
351                                          focus_rect.Y,
352                                          focus_rect.Width,
353                                          focus_rect.Height);
354                 }
355                 
356                 #region ScrollBar
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;                      
362                         Rectangle       thumb_pos;
363                         
364                         IntPtr gdkwindow = gdk_window_foreign_new_for_display (gdkdisplay, (uint) bar.Handle);
365                         
366                         IntPtr style;
367                         
368                         Rectangle allocation = new Rectangle (bar.ClientRectangle.X, bar.ClientRectangle.Y, bar.ClientRectangle.Width, bar.ClientRectangle.Height);
369                         
370                         // fix for artefacts
371                         Color fix_color = bar.Parent != null ? bar.Parent.BackColor : ColorControl;
372                         
373                         if (bar.vert) {
374                                 style = gtk_rc_get_style (global_gtk_vscrollbar);
375                                 
376                                 gtk_widget_size_allocate (global_gtk_vscrollbar, ref allocation);
377                                 
378                                 // fix for artefacts
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);
383                         } else {
384                                 style = gtk_rc_get_style (global_gtk_hscrollbar);
385                                 
386                                 gtk_widget_size_allocate (global_gtk_hscrollbar, ref allocation);
387                                 
388                                 // fix for artefacts
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);
393                         }
394                         
395                         style = gtk_style_attach (style, gdkwindow);  // need it
396                         
397                         current_gdk_window = gdkwindow;
398                         current_style = style;
399                         
400                         thumb_pos = bar.ThumbPos;
401                         
402                         if ( bar.vert ) {
403                                 first_arrow_area = new Rectangle( 0, 0, bar.Width, scrollbutton_height + 1 );
404                                 bar.FirstArrowArea = first_arrow_area;
405                                 
406                                 second_arrow_area = new Rectangle( 0, bar.ClientRectangle.Height - scrollbutton_height - 1, bar.Width, scrollbutton_height + 1 );
407                                 bar.SecondArrowArea = second_arrow_area;
408                                 
409                                 thumb_pos.Width = bar.Width;
410                                 bar.ThumbPos = thumb_pos;
411                                 
412                                 ScrollBar_Vertical_Draw_ThumbMoving_None (scrollbutton_height, bar, clip, dc);
413                                 
414                                 /* Buttons */
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 );
419                         } else {
420                                 first_arrow_area = new Rectangle( 0, 0, scrollbutton_width + 1, bar.Height );
421                                 bar.FirstArrowArea = first_arrow_area;
422                                 
423                                 second_arrow_area = new Rectangle( bar.ClientRectangle.Width - scrollbutton_width - 1, 0, scrollbutton_width + 1, bar.Height );
424                                 bar.SecondArrowArea = second_arrow_area;
425                                 
426                                 thumb_pos.Height = bar.Height;
427                                 bar.ThumbPos = thumb_pos;
428                                 
429                                 /* Background */                                        
430                                 ScrollBar_Horizontal_Draw_ThumbMoving_None (scrollbutton_width, bar, clip, dc);
431                                 
432                                 /* Buttons */
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 );
437                         }
438                         
439                         /* Thumb */
440                         ScrollBar_DrawThumb( bar, thumb_pos, clip, dc );                                
441                 }
442                 
443                 protected override void ScrollBar_DrawThumb( ScrollBar bar, Rectangle thumb_pos, Rectangle clip, Graphics dc ) {
444                         if ( bar.Enabled)
445                                 DrawScrollBarThumb( dc, thumb_pos, bar );
446                 }
447                 
448                 protected override void ScrollBar_Vertical_Draw_ThumbMoving_None (int scrollbutton_height, ScrollBar bar, Rectangle clip, Graphics dc)
449                 {
450                         Rectangle r = new Rectangle (0,
451                                                      scrollbutton_height, bar.ClientRectangle.Width, bar.ClientRectangle.Height - (scrollbutton_height * 2));
452                         gtk_paint_box (style, 
453                                        current_gdk_window, 
454                                        (int) StateType.Active,
455                                        (int) ShadowType.In,
456                                        IntPtr.Zero,
457                                        global_gtk_vscrollbar,
458                                        "vscrollbar",
459                                        r.X, r.Y,
460                                        r.Width, r.Height);
461                 }
462                 
463                 protected override void ScrollBar_Horizontal_Draw_ThumbMoving_None (int scrollbutton_width, ScrollBar bar, Rectangle clip, Graphics dc)
464                 {
465                         Rectangle r = new Rectangle (scrollbutton_width,
466                                                      0, bar.ClientRectangle.Width - (scrollbutton_width * 2), bar.ClientRectangle.Height);
467                         
468                         gtk_paint_box (style, 
469                                        current_gdk_window, 
470                                        (int) StateType.Active,
471                                        (int) ShadowType.In,
472                                        IntPtr.Zero,
473                                        global_gtk_hscrollbar,
474                                        "hscrollbar",
475                                        r.X, r.Y,
476                                        r.Width, r.Height);
477                 }
478                 
479                 private void DrawScrollBarThumb( Graphics dc, Rectangle area, ScrollBar bar ) {
480                         IntPtr gtk_scrollbar = bar.vert ? global_gtk_vscrollbar : global_gtk_hscrollbar;
481                         
482                         gtk_paint_box (style, 
483                                        current_gdk_window, 
484                                        (int) StateType.Active,
485                                        (int) ShadowType.Out,
486                                        IntPtr.Zero,
487                                        gtk_scrollbar,
488                                        "slider",
489                                        area.X, area.Y,
490                                        area.Width, area.Height);
491                         
492 //                      if ( bar.vert ) {
493 //                              if ( area.Height > 12 ) {
494 //                                      int mid_y = area.Y + ( area.Height / 2 );
495 //                                      int mid_x = area.X + ( area.Width / 2 );
496 //                                      
497 //                                      // TODO: add 2 lines
498 //                                      gtk_paint_hline(current_style,
499 //                                                      current_gdk_window,
500 //                                                      (int) StateType.Normal,
501 //                                                      IntPtr.Zero,
502 //                                                      IntPtr.Zero,
503 //                                                      "through",
504 //                                                      mid_x - 4,
505 //                                                      mid_x + 4,
506 //                                                      mid_y);
507 //                              }
508 //                      } else {
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 );
513 //                                      
514 //                                      // TODO: add 2 lines
515 //                                      gtk_paint_vline(current_style,
516 //                                                      current_gdk_window,
517 //                                                      (int) StateType.Normal,
518 //                                                      IntPtr.Zero,
519 //                                                      IntPtr.Zero,
520 //                                                      "through",
521 //                                                      mid_y - 4,
522 //                                                      mid_y + 4,
523 //                                                      mid_x);
524 //                              }
525 //                      }
526                 }
527                 #endregion      // ScrollBar
528                 
529                 public override void CPDrawButton (Graphics graphics, Rectangle rectangle, ButtonState state)
530                 {
531                         bool is_pushed = false;
532 //                      bool is_checked = false;
533 //                      bool is_flat = false;
534                         bool is_inactive = false;
535                         
536                         if ((state & ButtonState.Pushed) != 0) {
537                                 is_pushed = true;
538                         }
539                         
540 //                      if ((state & ButtonState.Checked) != 0) {
541 //                              is_checked = true;
542 //                      }
543 //                      
544 //                      if ((state & ButtonState.Flat) != 0) {
545 //                              is_flat = true;
546 //                      }
547                         
548                         if ((state & ButtonState.Inactive) != 0) {
549                                 is_inactive = true;
550                         }
551                         
552                         IntPtr style;
553                         
554                         style = gtk_rc_get_style (global_gtk_button);           
555                         style = gtk_style_attach (style, current_gdk_window);  // need it
556                         
557                         StateType state_type = StateType.Normal;
558                         ShadowType shadow_type = ShadowType.Out;
559                         string detail = "buttondefault";
560                         
561                         if (is_inactive) {
562                                 state_type = StateType.Insensitive;
563                         } else
564                         if (is_pushed) {
565                                 state_type = StateType.Active;
566                                 shadow_type = ShadowType.In;
567                                 detail = "button";
568                         }
569                         
570                         gtk_paint_box (style, current_gdk_window,
571                                        (int) state_type,
572                                        (int) shadow_type,
573                                        IntPtr.Zero,
574                                        IntPtr.Zero,
575                                        detail,
576                                        rectangle.X, rectangle.Y,
577                                        rectangle.Width, rectangle.Height);
578                 }
579                 
580                 /* Scroll button: regular button + direction arrow */
581                 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton scroll_button_type, ButtonState state)
582                 {
583                         bool enabled = (state == ButtonState.Inactive) ? false: true;
584                         
585                         StateType state_type = enabled ? StateType.Normal : StateType.Insensitive;
586                         
587                         DrawScrollButtonPrimitive (dc, area, state, scroll_button_type);
588                         
589                         if (area.Width < 12 || area.Height < 12) /* Cannot see a thing at smaller sizes */
590                                 return;
591                         
592                         ArrowType arrow_type = 0;
593                         
594                         switch (scroll_button_type) {
595                         case ScrollButton.Up:
596                                 arrow_type = ArrowType.Up;
597                                 break;
598                         case ScrollButton.Down:
599                                 arrow_type = ArrowType.Down;
600                                 break;
601                         case ScrollButton.Right:
602                                 arrow_type = ArrowType.Right;
603                                 break;
604                         case ScrollButton.Left:
605                                 arrow_type = ArrowType.Left;
606                                 break;
607                         default:
608                                 break;
609                         }
610                         
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;
614                         
615                         switch (scroll_button_type) {
616                         case ScrollButton.Down:
617                         case ScrollButton.Up:
618                                 arrow_x = centerX - 4;
619                                 arrow_y = centerY - 2;
620                                 arrow_width = 8;
621                                 arrow_height = 4;
622                                 break;
623                         case ScrollButton.Left:
624                         case ScrollButton.Right:
625                                 arrow_x = centerX - 2;
626                                 arrow_y = centerY - 4;
627                                 arrow_width = 4;
628                                 arrow_height = 8;
629                                 break;
630                         default:
631                                 break;
632                         }
633                         
634                         gtk_paint_arrow (style, 
635                                          current_gdk_window, 
636                                          (int) state_type,
637                                          (int) ShadowType.Out,
638                                          IntPtr.Zero,
639                                          IntPtr.Zero,
640                                          "",            
641                                          (int) arrow_type, true, 
642                                          arrow_x, 
643                                          arrow_y,
644                                          arrow_width, arrow_height);
645                 }
646                 
647                 public void DrawScrollButtonPrimitive (Graphics dc, Rectangle area, ButtonState state, ScrollButton scroll_button_type)
648                 {
649                         StateType state_type = StateType.Normal;
650                         ShadowType shadow_type = ShadowType.Out;
651                         
652                         if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
653                                 state_type = StateType.Active;
654                                 shadow_type = ShadowType.In;
655                         }
656                         
657                         switch (scroll_button_type) {
658                         case ScrollButton.Left:
659                         case ScrollButton.Right:
660                                 gtk_paint_box (style, 
661                                                current_gdk_window, 
662                                                (int) state_type,
663                                                (int) shadow_type,
664                                                IntPtr.Zero,
665                                                global_gtk_hscrollbar,
666                                                "stepper",
667                                                area.X, area.Y,
668                                                area.Width, area.Height);
669                                 break;
670                         case ScrollButton.Up:
671                         case ScrollButton.Down:
672                                 gtk_paint_box (style, 
673                                                current_gdk_window, 
674                                                (int) state_type,
675                                                (int) shadow_type,
676                                                IntPtr.Zero,
677                                                global_gtk_vscrollbar,
678                                                "stepper",
679                                                area.X, area.Y,
680                                                area.Width, area.Height);
681                                 break;
682                         }
683                 }
684                 
685                 private static Color ColorFromGdkColor (GdkColorStruct gtkcolor)
686                 {
687                         return Color.FromArgb (255, 
688                                 (gtkcolor.red >> 8)  & 0xff, 
689                                 (gtkcolor.green  >> 8) & 0xff,
690                                 (gtkcolor.blue >> 8) & 0xff );
691                 }
692
693         } //class
694 }