Convert an object instance to a subclassing instance


If I have an Object EMenuBar subclassing GtkMenuBar, is there a way to create a new MyImage instance from an existing GtkImage instance ?

Code below does not work, returned object is a GtkMenuBar due to “menu_bar->parent = *widget;”

EMenuBar *
e_menu_bar_new (GtkMenuBar *widget)
	EMenuBar *menu_bar;

	menu_bar = g_object_new (E_TYPE_MENU_BAR, NULL);
	menu_bar->parent = *widget;

	return menu_bar;


I’m sorry, I don’t understand what you’re trying to achieve. What’s EMenuBar? What’s MyImage?

What’s this, and what is it supposed to do? You’re trying to change the parent instance field? Because that can’t ever work in any way, shape or form.

Oh sorry for “MyImage” instance, did not fix my comment after pasting code.

I have a GtkMenuBar object instance and want to extend it. I’ve subclassed GtkMenuBar with EMenuBar.

But for this to be useful, I need to be able to convert GtkMenuBar object instance to EMenuBar instance. But looks like it’s not possible.

I still don’t understand this at all, sorry.

If EMenuBar is a GtkMenuBar—i.e. if you derived the GtkMenuBar type—then you can pass an EMenuBar instance to any API that takes a GtkMenuBar.

Maybe what you need is a cast like E_MENU_BAR (menu_bar), but I’m not sure too.

In such a case, we can forward you to a general tutorial, here is the one I wrote several years ago: The GLib/GTK Development Platform - A Getting Started Guide (still mostly relevant, but the intro/conclusion could be updated).

The GObject library documentation also has an introduction to the concepts, and a tutorial for creating GObject subclasses.

And, if you are using GTK 3 and GtkMenuBar directly, the Amtk library can be of help.

Good luck in any case :wink:

What you are trying to do: is it creating a subclass of GtkMenuBar? In that case, you need to read the docs to know how to create a GObject subclass in general (it needs some boilerplate code because the C language is not object-oriented).

Or do you want to “extend” a GtkMenuBar object (an instance of the GtkMenuBar class)? In that case it depends if you want to create custom properties or signals, or just adding more functions to GtkMenuBar. In the first case, look for example at the implementation of AmtkMenuShell (an extension of GtkMenuShell).

I will try to be a little clearer.

I have an object EMenuBar subclassing a GtkMenuBar.

GtkUIManager gives me a GtkMenuBar but I have no idea how to downcasting it to a EMenuBar. I know it’s possible because Gtk-rs is able to.

You cannot do this with GtkUIManager, it does not support constructing custom types. I think you will have to either port it to GtkBuilder or stick with the current method of storing the GtkMenuBar.

Ok, thanks, that was my guess when I put GtkMenuBar inside EMenuBar.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.