FLTK logo

Re: [fltk.coredev] Proposal: adding FL_WHEN_DELETING and the event FL_DELETING

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.coredev  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Proposal: adding FL_WHEN_DELETING and the event FL_DELETING "'melcher.... Oct 27, 2022  
  Ah, sorry, my message were coincidendtal, but this has nothing to do with Groups. This is purely a callback thing that is meant to give callback users a chance to deallocate user data that they may have allocated for just this widget. The Group thing is quite easy to resolve: we just need to make Fl_Group::insert(), remove(), and clear() virtual.

But now to explain this case. Assuming your app allocates and deletes widgets dynamically, let's say you need more data in a callback than just a single pointer. So you allocate a dataset when allocating the widget, but you can't make sure that the dataset is deleted whenever the widget is deleted by the system.

For example (not compilable code, grossly simplified):

  struct {
    int x;
    int y;
  } CallbackData;


  void button_cb(Fl_Button *w, void *u) {
    CallbackData *d = (CallbackData*)u;
    if (Fl::event()==FL_DELETING)
      delete d;
    else
      printf("I got %d and %d\n", d->x, d->y);
  }

  myButton = new Fl_Button(...)
  myButton->callback(button_cb, new CallbackData(42, 128) );
  myButton->when(FL_WHEN_CHANGED|FL_WHEN_DELETING);

This needs just a few lines of code to be implemented:

in Fl_Widget at the top:
  if (when() & FL_WHEN_DELETING) {
    int old_event = Fl::event();
    Fl::event(FL_DELETING);
    do_callback();
    Fl::event(old_event);
  }

plus the declaration of those constants and some Fluid changes.

There is an alternative: just as in Fl_Widget::label_copy(char*), we could add Fl_Widget::callback_data_copy(void*), set a flag, and call free(_user_data) when deleting, or call delete _user_data, so it would be a set-and-forget with less flexibility.

--
You received this message because you are subscribed to the Google Groups "fltk.coredev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkcoredev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkcoredev/8b686578-dd14-4b42-8927-4fa96d43a6e8n%40googlegroups.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.