Fl_WidgetAssociation.H 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef fltk_WidgetAssociation_h
  2. #define fltk_WidgetAssociation_h
  3. #include <FL/Fl_Export.H>
  4. class Fl_Widget;
  5. /** Base class for the association type.
  6. *
  7. * FLTK allows you to attach any kind of user data to a widget. This data is automatically freed when the
  8. * widget to which it is attached is destroyed. To identify tha attached data an instance of a subclass
  9. * of this class is used. Each instance of attached data needs its own instance of a subclass.
  10. * It is not possibel to attach multiple instances of the same type.
  11. *
  12. * The subclass is also used to free the data, once the widget that the data is attached to is deleted.
  13. * When that happens all attached data is destroyed using the destroy function within this class. As
  14. * each attached data package has its own pointer to an association type it can also have its own
  15. * function to free the data.
  16. *
  17. * possible uses:
  18. * - assign key shortcuts to certain widgets
  19. * - assign a tooltip to some widgets
  20. * - assign a help-index to widgets
  21. * - assign a unique identifier to widgets to remote controlling
  22. * - assign additional layouting data for new container widgets
  23. * - assign data needed by typesafe callback mechanisms
  24. * - assign all kind of data not always required within a widget / each widget
  25. */
  26. class Fl_Association_Type {
  27. public:
  28. /** This function is called when associated data is freed.
  29. *
  30. * This function must be proveded when creating a data specific subclass. The function
  31. * must do whatever is necessary to free associated data. Most of the time it will be a cast
  32. * to the right datatype and a delete
  33. */
  34. virtual void destroy(void* data) const = 0;
  35. };
  36. /** Fl_AssociationFunctor Class used by the foreach() functions.
  37. *
  38. * This is the abstract base class for the association functor that is used in foreach().
  39. * If you want to supply your specific actions to do with the associated data found by the
  40. * foreach() functions you need to derive from this class and provide a new handle function.
  41. */
  42. class Fl_Association_Functor {
  43. public:
  44. /** For each found association this function is called.
  45. * If the function returns true the
  46. * loop is aborted and the data pointer for the current association is returned.
  47. * If you want to iterator over all associations simply don't return true, but
  48. * only do whatever you need within this function.
  49. */
  50. virtual bool handle(const Fl_Association_Type&, const Fl_Widget*, void* data) = 0;
  51. };
  52. /** \relates Fl_AssociationType
  53. * This function allows traversing all associations of a certain association type, a certain widget,
  54. * both, or none of the constraints.
  55. *
  56. * For each found widget the handle function in the associaionFunctor class is called. If that
  57. * function returns true the scan is aborted and the data for the current widget is returned
  58. * A NULL pointer for the Fl_AssociationType or the Fl_Widget pointer means to call the functor for all
  59. * AssociationTypes and/or all Widgets.
  60. * Important: don't modify any associations while this function is running!
  61. *
  62. * The function either returns the first associated data for which the functor returns true, or NULL.
  63. * \param[in] at the type of associations you want to iterate over, if NULL all associations will be taken
  64. * \param[in] w the widget you want to iterate over, if NULL all widgets will be taken
  65. * \param[in] ftk the functor object that contains the handle function to be called for all associations found
  66. */
  67. void* AssocForeach(const Fl_Association_Type* at, const Fl_Widget* w, Fl_Association_Functor& fkt);
  68. #endif