简体   繁体   中英

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?

Fl_Tabs does not offer that feature. You can work around it by adding a close button in the group itself. Adding and removing individual tabs to Fl_Tabs works with ‚add' and ‚remove', just like any other Fl_Group.

This feature is about to be added though some time in Jan 23, so if you watch the master on GitHub, you should be able to use this feature soon.

The height for tab1, 2, and 3 should probably be window.h()-64, not 32.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM