Search code examples
c++gtksignalsgtkmm

GTK+ / Gtkmm, check if a notebook tab already exists, and create it only if it doesn't exist


I'm learning to use Gtkmm. Now I have got to make a window with a button which, when clicked, opens a "notebook" tab. Now, I want to make it check if the tab already exists, and open the tab only if the tab doesn't exist. Please, someone tell me if there is a way to do it with Gtkmm.
Thanks in advance. My current code is the following:

#include <iostream>
#include <iomanip>
#include <gtkmm-3.0/gtkmm.h>
using namespace std;


class programa : public Gtk::Window
    {
    public:
    //Constructor y destructor
    programa();
    ~programa();

    protected:
    //Signal handlers:
    void en_boton1();
    //Child widgets:
    Gtk::Grid w_grid;
    Gtk::Button boton1;
    Gtk::Notebook hojas;
    Gtk::Label nombre1;
    };


void programa::en_boton1()
    {
    hojas.append_page(nombre1, "Primera");
    show_all_children();
    }


programa::programa() : boton1(), boton2(), nombre1("pestaña 1"), nombre2("pestaña 2")
    {
    set_title("ventana");
    maximize();
    add(w_grid);
    set_border_width(10);

    boton1.add_pixlabel("info.xpm", "botoncito1");
    w_grid.attach(boton1, 1 ,1 ,1 ,1);
    boton1.signal_clicked().connect( sigc::mem_fun(*this,&programa::en_boton1 ) );

    w_grid.attach(hojas, 1,2,4,1);
    hojas.set_border_width(10);

    show_all_children();
    }


programa::~programa() {}


int main(int argc, char *argv[])
    {
    Gtk::Main paq(argc, argv);

    programa principal;

    //Shows the window and returns when it is closed.
    Gtk::Main::run(principal);
    return 0;
    }

Solution

  • Gtk::Notebook::get_n_pages() and get_nth_page() should do what you need: https://developer.gnome.org/gtkmm/stable/classGtk_1_1Notebook.html#aee6987ef10d8e3afcc40824c53f4c1ad

    But you'll need your own logic (probably after a dynamic_cast<>) to decide whether any of notebook pages are the one you are looking for.