Fl_Scroll broken?

9 views
Skip to first unread message

supsm17

unread,
Jun 20, 2021, 2:13:47 AM6/20/21
to fltk.general
I made an application which adds check buttons for each entry found in a directory (this part isn't super important). I've noticed that the text on the check buttons don't match what I get when I cout the string. 
temp.png
This is what it is supposed to say v
temp.png

Also, I've noticed that when I get the utf8 string (.u8string()) of a filesystem path, then scroll away and scroll back, the text changes to garbage. The garbage changes every time I restart my program. 
temp.png
This does not happen when I get a regular string (.string())
The scroll bar also does not show up until I start scrolling (as seen in the first screenshot)


Code:

for (auto& entry : std::filesystem::directory_iterator(std::filesystem::u8path(path)))
{
addentry(entry.path().filename().u8string());
std::cout << entry.path().filename().u8string() << std::endl;
Fl::wait();
}

void addentry(std::string name)
{
ui->scroll->begin();
Fl_Check_Button* o = new Fl_Check_Button(470, 60 + 15 * numbuttons, 1000, 15, name.c_str());
o->down_box(FL_DOWN_BOX);
o->value(1);
o->user_data((void*)(numbuttons));
o->callback((Fl_Callback*)(callback));
o->when(FL_WHEN_CHANGED);
ui->scroll->end();
numbuttons++;
}

UI is defined as
extern UI* ui = new UI()

UI class:
h file: 
// generated by Fast Light User Interface Designer (fluid) version 1.0306

#ifndef fltktest_h
#define fltktest_h
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
extern void run(Fl_Button*, void*);
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Check_Button.H>
extern void conversion(Fl_Check_Button*, void*);
#include <FL/Fl_Scroll.H>

class UI {
public:
  UI();
  Fl_Window *window;
  Fl_Text_Display *text_display;
  Fl_Scroll *scroll;
  void show();
};
#endif
cxx file:
// generated by Fast Light User Interface Designer (fluid) version 1.0306

#include "fltktest.h"

UI::UI() {
  { window = new Fl_Window(659, 349, "FLTK test");
    window->user_data((void*)(this));
    // removed irrelevant stuff
    { scroll = new Fl_Scroll(445, 40, 190, 260, "Title");
      { // padding
        Fl_Check_Button* o = new Fl_Check_Button(470, 45, 150, 15);
        o->down_box(FL_DOWN_BOX);
        o->labeltype(FL_NO_LABEL);
        o->hide();
        o->deactivate();
      } // Fl_Check_Button* o
      scroll->end();
    } // Fl_Scroll* scroll
    window->end();
  } // Fl_Window* window
}

void UI::show() {
  window->show();
}

Any help is appreciated :)

Greg Ercolano

unread,
Jun 20, 2021, 2:18:58 AM6/20/21
to fltkg...@googlegroups.com

Fl_Check_Button* o = new Fl_Check_Button(470, 60 + 15 * numbuttons, 1000, 15, name.c_str());


    Here Fl_Check_Button's constructor is saving the pointer to name.c_str()
    and is not making a copy of the string. So the string has gone out of scope by the
    time the strings are being drawn.

    Instead use:

Fl_Check_Button* o = new Fl_Check_Button(470, 60 + 15 * numbuttons, 1000, 15);
        o->copy_label(
name.c_str());

    ..so that the widget internally makes a copy of the string.

supsm17

unread,
Jun 20, 2021, 1:27:39 PM6/20/21
to fltk.general
That solved the issue, thank you so much!
Reply all
Reply to author
Forward
0 new messages