Mixing C and C++ APIs
You can use C APIs which do not yet have convenient C++ interfaces. It is generally not a problem to use C APIs from C++, and gtkmm helps by providing access to the underlying C object, and providing an easy way to create a C++ wrapper object from a C object, provided that the C API is also based on the GObject system.
To use a gtkmm instance with a C function that requires a C GObject instance, use the C++ instance’s gobj() function to obtain a pointer to the underlying C instance. For example:
Gtk::Button button("example"); gtk_button_do_something_that_gtkmm_cannot(button.gobj());
To obtain a gtkmm instance from a C GObject instance, use one of the many overloaded Glib::wrap() functions. The C instance’s reference count is not incremented, unless you set the optional take_copy argument to true. For example:
GtkButton* cbutton = get_a_button(); Gtk::Button* button = Glib::wrap(cbutton); button->set_label("Now I speak C++ too!");
- it's a widget or other class that inherits from Gtk::Object, and
- the C instance has a floating reference when the wrapper is created, and
- Gtk::manage() has not been called on it (which includes if it was created with Gtk::make_managed()), or
- Gtk::manage() was called on it, but it was never added to, or was later removed from, its parent.
In all other cases the C++ instance is automatically deleted when the last reference to the C instance is dropped. This includes all Glib::wrap() overloads that return a Glib::RefPtr.