Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Gtkmm: Setting current tab label?

  1. #11
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    838
    Distro
    Ubuntu 17.10 Artful Aardvark

    Re: Gtkmm: Setting current tab label?

    I came back to this today, I know im pretty close now. Just stuck on this final piece of the puzzle:

    ‘class Gtk::Widget’ has no member named ‘set_text’
    Of course this is due to trying set_text a Label method on a widget. Can a widget be converted to GTK:Label so I can use set_text?

    Code:
    label.set_text(Glib::ustring("TEST"));
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  2. #12
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Gtkmm: Setting current tab label?

    A label is a widget (subclass of) so you can cast it as long as it really is a label, ie it was created as a new label but declared as a widget variable. This is very common in C programming with Gtk but I don't know specifically how to do it in C++. I suspect it's just a simple cast.

  3. #13
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    838
    Distro
    Ubuntu 17.10 Artful Aardvark

    Re: Gtkmm: Setting current tab label?

    When you say simple cast do you mean a static cast?
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  4. #14
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Gtkmm: Setting current tab label?

    I probably meant a static cast but I've had a play around with it and I don't think you need one:

    This is really crude, but I think it's the basic idea, i.e. create a widget (in this case a Hbox subclass) that combines the label and close button., then add that as the tab label.

    notebook.cc
    Code:
    #include <gtkmm.h>
    #include "closelabel.h"
    
    int main(int argc, char *argv[])
    {
        Gtk::Main kit(argc, argv);
        Gtk::Window window;
        window.set_title("Notebook");
        window.set_default_size(600, 400);
    
        Gtk::VBox vbox;
        window.add(vbox);
    
        Gtk::Notebook notebook;
    
        Gtk::Label label0("Content Alpha");
        CloseLabel tabLabel0("Alpha");
        notebook.append_page(label0, tabLabel0);
    
        Gtk::Label label1("Content Beta");
        CloseLabel tabLabel1("Beta");
        notebook.append_page(label1, tabLabel1);
    
        vbox.pack_start(notebook);
        window.show_all();
        Gtk::Main::run(window);
    }
    closelabel.h
    Code:
    #ifndef CLOSELABEL_H
    #define CLOSELABEL_H
    
    #include <gtkmm.h>
    
    class CloseLabel : public Gtk::HBox
    {
    public:
        CloseLabel(const char *text);
    
    protected:
        void on_close_button_clicked();
        Gtk::Label tab_label;
        Gtk::Button close_button;
    };
    
    #endif
    closelabel.cc
    Code:
    #include "closelabel.h"
    #include <iostream>
    
    CloseLabel::CloseLabel(const char *text)
        : tab_label(text), close_button("close")
    {
        close_button.signal_clicked().connect(
            sigc::mem_fun(*this, &CloseLabel::on_close_button_clicked)
        );
        pack_start(tab_label);
        pack_start(close_button);
        show_all();
    }
    
    void CloseLabel::on_close_button_clicked()
    {
        std::cout << "on_close_button_clicked" << std::endl;
    }
    It needs some work. You'd need to keep track of the notebook page in the CloseLabel class so that you know which one to close in the handler. And, obviously, you'd use a nice little little "X" icon as an image rather than the ugly close button.

    As you know, my C++ is not brilliant, but I think this is the way forward with these closeable tabs in GTK3.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •