[Gtk-sharp-list] writing custom widgets with Gtk#

Miguel de Icaza miguel@ximian.com
15 Dec 2002 02:08:56 -0500


Hello,

> > My first thoughts were just to override some protected method in the
> > ScrolledWindow class to change the size/layout of the scrollbar buttons.
> > However, after examining the generated Gtk.ScrolledWindow source, it
> > doesn't look like this is/will be possible.
> 
> This would require overriding a GtkWidget class method, and that is not
> possible yet.  I don't think anyone's come up with a good idea of how to
> do it yet, but it's definitely something we want to do

It would be interesting to put the options on the table so we can
discuss the different possibilities that we have at our disposal.

Here is a partial idea that is based on a discussion with Dave Camp from
a few days ago.

* The Foundation

        It would be possible to basically for each GObject Type that we
        want to override to create a child object that actually has the
        overwritten methods pointing to C functions that are basically
        stubs that would call the C# universe code.
        
        This would require that every widget/object actually has a
        derivative class where we can "patch" things up:
        
        struct PatchedGtkButton {
        	GtkButton button;
        }
        
        struct PatchedGtkButtonClass {
        	GtkButtonClass klass
        }
        
        ...
        
        patched_gtk_button_class_init ()
        {
        	widget_class->size_allocate = patched_gtk_widget_size_allocate;
        	...
        	container_class->add = patched_gtk_container_size_allocate;
        	..
        	button_class->button_method = patched_gtk_button_method;
        }
        
        Then every one of those methods basically does:
        
        patched_gtk_widget_size_allocate (...)
        {
        	if (PointerCall (arguments))
        		return;
        	(WIDGET_CLASS (object)->size_allocate)(arguments);
        }

* Problems

        The problem with the above approach is that it would require a
        lot of extra glue code just to be able to override methods.
        
        It is also non-obvious how to handle things in C# like:
        
        public MyWidget : Gtk.Widget  {
        	public override bool SizeAllocate (SizeAllocation s)
        	{
        		base.SizeAllocation (s);
        		s.width += 10;
        		s.height += 20;
        		return true;
        	}
        
        When we call `base.SizeAllocation', how do we make it call the
        right thing?
        
* The optimization.

        One possible optimization that we can do is that most of the
        generated code would stay the same.  What we need to do is call
        a different constructor (possible a generic constructor that
        takes a GType as an argument).
        
        Then we could construct GTypes dynamically with the information
        that would point to a stub routine that would provide patchy
        versions of every one of the virtual methods that we want to
        override.
        
Miguel