I have a piece of code I've been playing with that needs to dynamically tear 
down and build the contents of a group.

The thing is, when I actually ask it to clear() out the group, it seems like 
the old stuff is still somehow drawn despite the group() reporting no children 
(the drawn items are not interact-able, they seem to be cached drawing material 
left there for some reason).

In my experiments, only resizing the window seems to get the contents to clear 
out.

Here's an example of the behavior; in the following code, select '2-item3' or 
'3-item4' from the Choice box. Some editable parameter fields will show up. Now 
select '0-item1' or '1-item2'. The old value_inputs still show up, but clearly 
aren't *really* there. If you resize the window, they disappear.

I assume that this is me not using the library correctly, but I'd sure like to 
know what the right way to do this is. Sorry for the long-ish 'minimal working 
example':

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Value_Input.H>
#include <FL/Fl_Choice.H>

struct dispitem
{
    const char  *name;
    int          entries_n;
    const char **entries;
};

static const char *item3[]  = {"i3p0", "i3p1"};
static const char *item4[]  = {"i4p0", "i4p1", "i4p2"};
static dispitem the_items[] =
    {
        {
            "item 1",
            0, 0,
        },
        {
            "item 2",
            0, 0,
        },
        {
            "item 3",
            2,
            item3,
        },
        {
            "item 4",
            3,
            item4,
        },
    };

static const int  the_items_n         = sizeof(the_items)/sizeof(the_items[0]);
static dispitem  *current_item        = the_items + 0;
static Fl_Group  *var_group           = (Fl_Group *)0;
static Fl_Choice *item_picker         = (Fl_Choice *)0;
static Fl_Group  *param_control_group = (Fl_Group *)0;

static void cb_param_value_input(Fl_Value_Input* o, void *d)
{
    int idx = (int)o->user_data();
    printf("input for param %d is %f\n", idx, o->value());
}

static void add_param_group(Fl_Group *parent, const char *name, int idx)
{
    Fl_Value_Input *param_value = new Fl_Value_Input(parent->x()+parent->w()/3, 
parent->y() + 60*idx, parent->w()/3, 23, name);
    param_value->value(0);
    param_value->range(-10000, 10000);
    param_value->step(0.1);
    param_value->callback((Fl_Callback*)cb_param_value_input);
    param_value->user_data((void*)idx);
    param_value->align(Fl_Align(FL_ALIGN_LEFT));
}

static void setup_params(const dispitem *ve)
{
    printf("nch0: %d\n", param_control_group->children());
    param_control_group->clear();
    printf("nch1: %d\n", param_control_group->children());
    param_control_group->begin();
    for(int i = 0; i < ve->entries_n; ++i)
        add_param_group(param_control_group, ve->entries[i], i);
    param_control_group->end();
    printf("nch2: %d\n", param_control_group->children());
    param_control_group->redraw();
}

static void set_item(int i) {
    current_item = the_items + i;
    setup_params(current_item);
}

static void cb_item_picker(Fl_Choice* o, void*) {
  set_item(o->value());
}

static void populate_item_picker() {
  Fl_Menu_Item *items = (Fl_Menu_Item *) malloc(sizeof(Fl_Menu_Item) * 
(the_items_n + 1));
  for(int i = 0; i < the_items_n; ++i)
  {
          memset(items + i, 0, sizeof(Fl_Menu_Item));
          char buff[1024];
          snprintf(buff, 1023, "%d-%s", i, the_items[i].name);
          items[i].text = strdup(buff);
          items[i].flags = 0;
  }
  memset(items + the_items_n, 0, sizeof(Fl_Menu_Item));
  item_picker->menu(items);
}

Fl_Double_Window* make_window() {
  Fl_Double_Window* w;
  { Fl_Double_Window* o = new Fl_Double_Window(200, 300);
    w = o;
    { var_group = new Fl_Group(0, 0, 200, 300);
      var_group->box(FL_BORDER_FRAME);
      var_group->color(FL_FOREGROUND_COLOR);
      { item_picker = new Fl_Choice(0, 0, 200, 23, "item");
        item_picker->down_box(FL_BORDER_BOX);
        item_picker->callback((Fl_Callback*)cb_item_picker);
        item_picker->align(Fl_Align(FL_ALIGN_TOP));
        populate_item_picker();
      } // Fl_Choice* item_picker
      { param_control_group = new Fl_Group(0, 57, 200, 241);
        param_control_group->color(FL_BACKGROUND2_COLOR);
        param_control_group->end();
      } // Fl_Group* param_control_group
      var_group->end();
    } // Fl_Group* var_group
    o->end();
    o->resizable(o);
  } // Fl_Double_Window* o
  return w;
}

int main(int argc, char *argv[])
{
    Fl_Double_Window *helper = make_window();
    helper->show(1, argv);

    return Fl::run();
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to