fltk::StringHierarchy Class Reference

Inherits fltk::List.

Inherited by fltk::StringList.

List of all members.

Public Member Functions

virtual Widgetchild (const Menu *, const int *indexes, int level)
virtual int children (const Menu *, const int *indexes, int level)=0
Widgetgenerated_item ()

Detailed Description

This subclass of List allows a Menu or Browser to display strings that are generated at run time. This is the most efficient way to make hundreds of thousands of items.

Typically you will create a subclass with the children() and label() functions overridden, and make a single static instance of this class. It can be used by several menus, but the Menu* argument to children() and label() is used to differentiate them.

Member Function Documentation

Widget * StringHierarchy::child ( const Menu menu,
const int *  indexes,
int  level 
) [virtual]

Return a given child as a widget. draw() and measure() will be called on this widget to figure out where to place it and to draw it. Typical implementations create a reusable fltk::Item and fill it in with the correct data. This should return NULL if there is anything illegal about the indexes.

Here is a sample implementation, where Node is a data type that you have defined. This demonstrates how to create the dummy widget:

fltk::Widget* My_List::child(const fltk::Menu*, const int* indexes, int level) {
  Node* node = root;
  for (int l = 0; l <= level; l++) {
    if (!node->is_parent()) return 0;
    if (indexes[l] >= node->children_count()) return 0;
    node = node->child(indexes[l]);
  static fltk::Widget* widget;
  if (!widget) {
    widget = new fltk::Item();
  widget->w(0); // cause measure() to be called
  if (node->selected) widget->set_flag(fltk::SELECTED);
  else widget->clear_flag(fltk::SELECTED);
  if (node->is_parent() && node->open) widget->set_flag(fltk::STATE);
  else widget->clear_flag(fltk::STATE);
  return widget;

Reimplemented from fltk::List.

virtual int fltk::StringHierarchy::children ( const Menu menu,
const int *  indexes,
int  level 
) [pure virtual]

Return how many children are under a given item. If level is zero, this should return how many items are at the top level. Otherwise indexes is an array of level numbers indicating the index of an item at the top level, the index of the an item that is the child of that, and so on.

This should return -1 if the item is not a "parent" item or the index array is illegal. It is not necessary to return the correct value until the parent is "open", which means the fltk::STATE flag was set in it, so if it is expensive to calculate the number you can return 1 for any closed parent.

Here is a sample implementation, where Node is a data type that you have defined:

int My_List::children(const fltk::Menu*, const int* indexes, int level) {
  Node* node = root;
  for (int l = 0; l < level; l++) {
    if (indexes[l] >= node->children_count()) return -1;
    node = node->child(indexes[l]);
    if (!node->is_parent()) return -1;
  return node->children_count();

Reimplemented from fltk::List.

Implemented in fltk::StringList.

Widget * StringHierarchy::generated_item ( ) [inline]

Inside label() this points at the widget that will have the label set. This allows you to mess with the flags, font, user_data, etc of the widget. This widget is reused every time a new label is needed, so be sure to set everything on each call and do not assumme they have been put back to the default values.

If your items vary in vertical size, you must do generated_item()->h(0) in label(). This is unfortunate but it speeds up the common case where they all have the same height.

Convesely, setting w() will avoid the need to call measure() if the width is needed and you happen to know it.

The documentation for this class was generated from the following files: