[Gtk-sharp-list] Returning Gdk.Events

Jonathan Pryor jonpryor@vt.edu
11 Mar 2003 06:38:51 -0500

Not sure if it's applicable here, but last year I had a similar problem
where I needed to keep a C# class tree and a C++ class tree "in sync" so
that the usage would be consistent between both languages.

What I settled on doing was using a table (Hashtable, IIRC) mapping of
strings to strings.  The input strings was the result of C++'s
typeid(ClassName).name() function call, while the mapped-to strings were
the corresponding C# class name.

This would allow me to check the dynamic type of the C++ object and
construct a "closest-matching" C# class instance, returning that to the

Advantages: C# dynamic type is "correct", allowing for use of casting,
the "is" operator, sane ToString() output, etc.

Disadvantages: requires building table, which adds to program
initialization overhead.  Also requires being able to get a
string/textual description of the "native" class.  C++ allows this, but
I'm not that familiar with GObject, so this may be possible.  It looks
like g_type_name() would fit the bill, however.

Also, the simplest "closest match" ignores inheritance.  If there is no
explicit mapping, then Gdk.Event is the default wrapper.

This would allow a pseudo-implementation of:

	public class Application {
		static IDictionary mapping;
		static Application () {
			mapping = new Hashtable ();
			mapping["KnownGtkEventType"] = 
				"Corresponding Gtk# Type Name";
			// ...

		public static Gdk.Event CurrrentEvent {
			IntPtr handle = gtk_get_current_event ();
			string gtk_name = g_type_name (handle);
			string cs_name = mapping[gtk_name];
			Gdk.Event ev = null;
			if (cs_name != null)
				ev = Activator.CreateInstance (cs_name, 
					new object[]{handle});
				// no "closest match"
				ev = new Gdk.Event (handle);
			return ev;

I prefer this approach over Mike Kestner's approach, merely because the
dynamic types are kept in sync.  Though the table construction/lookup
overhead may be undesirable, so this might not be the best alternative.

 - Jon

On Mon, 2003-03-10 at 21:17, Miguel de Icaza wrote:
> Hello!
>      I have the following function:
> 	GdkEvent *gtk_get_current_event ()
>      The problem is that the Event can be one of many of the various
> events, how can I construct the right event depending on the type field
> of the event?
> Miguel
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list