c++windowdesktop-applicationfltk

Create a closable tab in FLTK


So I'm new to using FLTK in C++ and I'm learning the widgets. There is a class called Fl_Tabs that creates a new tab, using the label of the widget inserted into it.

However this tab label is not able to be interacted with.

I want the user to be able to click a button on the tab to close it, and i want them to be able to interact with the menubar to add new tabs...

here's my current code:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Enumerations.H>
 
int main(int argc, char **argv) {
  Fl_Window window(Fl::w()/2,Fl::h()/2, "test");

  Fl_Box windowBox(0,32,window.w(),window.h()-32);
  window.resizable(&windowBox);
  Fl_Tabs mainTabs(0,32,window.w(),window.h()-32);
  Fl_Group tab1(0,64,window.w(),window.h()-32, "Tab 1");
  tab1.end();
  Fl_Group tab2(0,64,window.w(),window.h()-32, "Tab 1");
  tab2.end();
  Fl_Group tab3(0,64,window.w(),window.h()-32, "Tab 1");
  tab3.end();
  mainTabs.end();

  window.end();
  window.show(argc, argv);
  return Fl::run();
}

How do i add a close button to the tab label?


Solution

  • As Matthias Melcher mentioned, this was recently added to FLTK (by Matthias Melcher :) ). Modifying your example:

    #include <FL/Fl.H>
    #include <FL/Enumerations.H>
    #include <FL/Fl_Box.H>
    #include <FL/Fl_Tabs.H>
    #include <FL/Fl_Group.H>
    #include <FL/Fl_Window.H>
    
    void tab_closed_cb(Fl_Widget *w, void *data) {
        auto parent = w->parent();
        parent->remove(w);
    }
    
    int main(int argc, char **argv) {
        Fl_Window window(Fl::w()/2,Fl::h()/2, "test");
        Fl_Box windowBox(0,32,window.w(),window.h()-32);
        window.resizable(&windowBox);
        Fl_Tabs mainTabs(0,32,window.w(),window.h()-32);
        // first tab
        Fl_Group tab1(0,64,window.w(),window.h()-32, "Tab 1");
        tab1.when(FL_WHEN_CLOSED);
        tab1.callback(tab_closed_cb);
        tab1.end();
    
        // second tab
        Fl_Group tab2(0,64,window.w(),window.h()-32, "Tab 2");
        tab2.when(FL_WHEN_CLOSED);
        tab2.callback(tab_closed_cb);
        tab2.end();
    
        // third tab
        Fl_Group tab3(0,64,window.w(),window.h()-32, "Tab 3");
        tab3.when(FL_WHEN_CLOSED);
        tab3.callback(tab_closed_cb);
        tab3.end();
    
        mainTabs.end();
        window.end();
        window.show(argc, argv);
        return Fl::run();
    }