how does Glib::wrap work?

Hello.
I’m migrating a plugin from an IDE made in gtk to gtkmm.
And I’m having trouble understanding Glib::wrap.
Below is what I want to do.

#include<gtkmm.h>
#include<iostream>
#include<gtk/gtk.h>

class example : public Gtk::Window
{
public:

example();
~example();

GtkWidget *cbutton;
};


example::example()
{
set_default_size(200,200);
cbutton = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(cbutton), "hello world");

Gtk::Button *m_button = Glib::wrap(cbutton);
add(*m_button);
std::cout << m_button->get_label() << '\n';
}

example::~example()
{
}


int main(int argc, char* argv[])
{
auto app = Gtk::Application::create(argc, argv, "exempple.org");
example ex;
return app->run(ex);
}

the compiler displays the following error:

error: invalid conversion from ‘Gtk::Widget*’ to ‘Gtk::Button*’ [-fpermissive]
      |         Gtk::Button *m_button = Glib::wrap(cbutton);
      |                                 ~~~~~~~~~~^~~~~~~~~
      |                                           |
      |                                           Gtk::Widget*

cbutton is of type ‘GtkWidget*’, so Glib::wrap() is returning a Gtk::Widget*. If you give Glib::wrap() a GtkButton* you should get a Gtk::Button* out. So you can:

Gtk::Button *m_button = Glib::wrap(GTK_BUTTON(cbutton));
// or
Gtk::Widget *m_button = Glib::wrap(cbutton);
// or
Gtk::Button *m_button = static_cast<Gtk::Button*>(Glib::wrap(cbutton));

I got using the following codes:

Gtkbutton *gbutton = GTK_BUTTON (cbutton);

GTK :: Button *m_button = Glib :: wrap (gbutton);

But I will use what I told me:

GTK :: Button *m_button = Glib :: wrap (GTK_BUTTON(cbutton));

Thank you Andrew :slight_smile:

There are many overloaded Glib::wrap() functions, such as

Gtk::Button* wrap(GtkButton* object, bool take_copy = false);
Gtk::Widget* wrap(GtkWidget* object, bool take_copy = false);
Glib::RefPtr<Gtk::BinLayout> wrap(GtkBinLayout* object, bool take_copy = false);

If you are migrating to gtkmm, why do you call gtk functions which have
gtkmm equivalents? What’s wrong with this?

#include<gtkmm.h>
#include<iostream>

class example : public Gtk::Window
{
public:

example();
~example();

Gtk::Button m_button;
};

example::example()
{
set_default_size(200,200);
m_button.set_label("hello world");
add(*m_button);
std::cout << m_button->get_label() << '\n';
}

It looks like you’re using gtkmm3. Gtkmm4 is recommended in new code, unless you
have a good reason to use gtkmm3. If you use gtkmm4, change your main() function.

int main(int argc, char* argv[])
{
auto app = Gtk::Application::create("exemple.org");
return app->make_window_and_run<example>(argc, argv);

Hello Kjell Ahlstedt

This was just an example.
The program I intend to port is a Geany IDE plugin.
And to access certain parts of the IDE’s main window, I need to get the widgets that are in it and were created in Gtk+3.0

Example:

Gtk::Notebook *notebook = Glib::wrap(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook));

References:
https://www.geany.org/manual/reference/howto.html
https://www.geany.org/manual/reference/structGeanyMainWidgets.html

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