void onelabGroup::_addMenu(const std::string &path, Fl_Callback *callback, void *data) { Fl_Tree_Item *n = _tree->add(path.c_str()); _tree->begin(); int ww = _baseWidth - (n->depth() + 1) * _indent; int hh = n->labelsize() + 4; Fl_Group *grp = new Fl_Group(1, 1, ww, hh); Fl_Button *but = new Fl_Button(1, 1, ww, hh); but->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE | FL_ALIGN_CLIP); but->callback(callback, data); but->box(FL_FLAT_BOX); but->color(_tree->color()); but->selection_color(_tree->color()); grp->end(); if(!_enableTreeWidgetResize) grp->resizable(0); _treeWidgets.push_back(grp); std::string label = path; std::string::size_type last = path.find_last_of('/'); if(last != std::string::npos) label = path.substr(last + 1); but->copy_label(label.c_str()); n->widget(grp); _tree->end(); }
void ModelerUserInterface::pickGroupProperty(GroupProperty* group) { // Remove the event listeners for old controls // TODO: we really need to have a PropertyEditor class that handles this // automatically... if (currentGroup) { PropertyList* props = currentGroup->getProperties(); for (PropertyList::iterator iter = props->begin(); iter != props->end(); iter++) { if (RangeProperty* prop = dynamic_cast<RangeProperty*>(*iter)) { prop->unlisten((SignalListener)updateRangeSlider); } else if (RGBProperty* prop = dynamic_cast<RGBProperty*>(*iter)) { prop->unlisten((SignalListener)updateColorChooser); } else if (BooleanProperty* prop = dynamic_cast<BooleanProperty*>(*iter)) { prop->unlisten((SignalListener)updateCheckbox); } else if (ChoiceProperty* prop = dynamic_cast<ChoiceProperty*>(*iter)) { prop->unlisten((SignalListener)updateChoice); } } // Clear out the old controls m_controlsPack->clear(); currentGroup = NULL; } // Reset the scrollbar m_controlsScroll->position(0, 0); // If there's no group, exit if (!group) { m_controlsScroll->redraw(); return; } // Constants for slider dimensions const int packWidth = m_controlsPack->w(); const int textHeight = 20; const int sliderHeight = 20; const int chooserHeight = 100; const int buttonHeight = 20; // Show them // For each control, add appropriate objects to the user interface currentGroup = group; PropertyList* props = group->getProperties(); for (PropertyList::iterator iter = props->begin(); iter != props->end(); iter++) { // Ignore it if it's a group property (those belong in the tree). if (dynamic_cast<GroupProperty*>(*iter)) continue; // And now we'll create a UI element for the property. // The big if-statement below uses dynamic_cast<PropertyType*>(ptr), // to see if a property has a given type. dynamic_cast will // return 0 if ptr is not of type PropertyType. // Add a slider if the property is a RangeProperty if (RangeProperty* prop = dynamic_cast<RangeProperty*>(*iter)) { // Add the label Fl_Box *box = new Fl_Box(0, 0, packWidth, textHeight, (*iter)->getName()); box->labelsize(14); box->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); box->box(FL_FLAT_BOX); // otherwise, Fl_Scroll messes up (ehsu) m_controlsPack->add(box); // Add the slider Fl_Value_Slider *slider = new Fl_Value_Slider(0, 0, packWidth, sliderHeight); slider->type(1); slider->range(prop->getMin(), prop->getMax()); slider->step(prop->getStep()); slider->value(prop->getValue()); m_controlsPack->add(slider); // Use the step size to determine the number of decimal places // shown in the slider's label. if (prop->getStep() > 0) { slider->precision((int)-log(prop->getStep())); } // Have the slider notify the program when it changes slider->callback((Fl_Callback*)SliderCallback, (void*) prop); // Have the property notify the slider when it changes prop->listen((SignalListener)updateRangeSlider, (void*) slider); // Add a color picker if the property is an RGB property } else if (RGBProperty* prop = dynamic_cast<RGBProperty*>(*iter)) { // Add the label Fl_Box *box = new Fl_Box(0, 0, packWidth, textHeight, (*iter)->getName()); box->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); box->labelsize(14); box->box(FL_FLAT_BOX); // otherwise, Fl_Scroll messes up (ehsu) m_controlsPack->add(box); // Add a color chooser Fl_Color_Chooser* chooser = new Fl_Color_Chooser(0, 0, packWidth, chooserHeight); chooser->rgb(prop->getRed(), prop->getGreen(), prop->getBlue()); m_controlsPack->add(chooser); // Have the chooser notify the program when it changes chooser->callback((Fl_Callback*)ColorPickerCallback, (void*) prop); // Remove any existing color chooser listeners on the property prop->unlisten((SignalListener)updateColorChooser); // Have the property notify the chooser when it changes prop->listen((SignalListener)updateColorChooser, (void*) chooser); // Add a checkbox if the property is a boolean property } else if (BooleanProperty* prop = dynamic_cast<BooleanProperty*>(*iter)) { // Add the checkbox -- no label needed! Fl_Check_Button* btn = new Fl_Check_Button(0, 0, packWidth, buttonHeight, prop->getName()); btn->labelsize(14); btn->type(FL_TOGGLE_BUTTON); btn->value(prop->getValue()); m_controlsPack->add(btn); // Have the button notify the program when it changes btn->callback((Fl_Callback*)ButtonCallback, (void*) prop); // Remove any existing color chooser listeners on the property prop->unlisten((SignalListener)updateCheckbox); // Have the property notify the chooser when it changes prop->listen((SignalListener)updateCheckbox, (void*) btn); // Add radio buttons if the property is a choice property } else if (ChoiceProperty* prop = dynamic_cast<ChoiceProperty*>(*iter)) { // Add the label Fl_Box *box = new Fl_Box(0, 0, packWidth, textHeight, (*iter)->getName()); box->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); box->labelsize(14); box->box(FL_FLAT_BOX); // otherwise, Fl_Scroll messes up (ehsu) m_controlsPack->add(box); // Add a group Fl_Pack* pack = new Fl_Pack(0, 0, packWidth, buttonHeight); pack->type(Fl_Pack::VERTICAL); pack->box(FL_THIN_DOWN_FRAME); pack->user_data((void*) prop); // Add the radio buttons const char* choices = prop->getLabels(); int start = 0, end = -1, index = 0; do { end++; if (choices[end] == 0 || choices[end] == '|') { string str(choices, start, end - start); Fl_Button* btn = new Fl_Round_Button(0, 0, packWidth, buttonHeight, prop->getName()); btn->type(FL_RADIO_BUTTON); btn->copy_label(str.c_str()); btn->value(prop->getValue() == index); // Have the button notify the program when it changes btn->callback((Fl_Callback*)ChoiceCallback, (void*)index); index++; start = end + 1; } } while (choices[end] != 0); pack->end(); m_controlsPack->add(pack); // Remove any existing choce listeners on the property prop->unlisten((SignalListener)updateChoice); // Have the property update the choices when it changes prop->listen((SignalListener)updateChoice, (void*) pack); } } m_controlsScroll->redraw(); }