[patch]: take 2: FtlkListResource
Since Johannes' criticisms were (IIRC) mostly about my patch's complexity I hope he will look at a new patch that sticks to my original approach but is a lot simpler. I realised that if the user_data() of the Menu Widget items stored the index of the Item instead of the Item itself then FltkListResource could store the selections by index in a misc::SimpleVector<bool>. This means there is no need to wrap bools in object::Object pointers and no need to add instance methods to Items to make them Hashable. Most of the scaffolding of the previous patch just disappears (the line count decreases by a third). I don't want to dismiss Johannes' alternative suggestion out of hand, but I still think my approach has a lot going for it and I'd like people to have a chance to criticise my best shot at it. Comments are welcome! Jeremy Henty
On Thu, May 01, 2008 at 03:09:41PM +0100, Jeremy Henty wrote:
Since Johannes' criticisms were (IIRC) mostly about my patch's complexity I hope he will look at a new patch that sticks to my original approach but is a lot simpler.
I realised that if the user_data() of the Menu Widget items stored the index of the Item instead of the Item itself then FltkListResource could store the selections by index in a misc::SimpleVector<bool>. This means there is no need to wrap bools in object::Object pointers and no need to add instance methods to Items to make them Hashable. Most of the scaffolding of the previous patch just disappears (the line count decreases by a third).
I don't want to dismiss Johannes' alternative suggestion out of hand, but I still think my approach has a lot going for it and I'd like people to have a chance to criticise my best shot at it.
Very nice. This is really much simpler. I also understand, your point about not to extend Item with members specific for this usecase. Cheers, Johannes
Comments are welcome!
Jeremy Henty
diff -pru -- 00_pre/dw/fltkui.cc 01_post/dw/fltkui.cc --- 00_pre/dw/fltkui.cc 2008-04-26 21:42:58.000000000 +0100 +++ 01_post/dw/fltkui.cc 2008-05-01 14:00:11.000000000 +0100 @@ -858,10 +858,11 @@ template <class I> FltkSelectionResource }
template <class I> -::fltk::Item *FltkSelectionResource<I>::Item::createNewWidget () +::fltk::Item *FltkSelectionResource<I>::Item::createNewWidget (int index) { - ::fltk::Item *item = new ::fltk::Item (name); - item->user_data (this); + ::fltk::Item *item = + new ::fltk::Item (name); + item->user_data ((void *) index); return item; }
@@ -869,7 +870,10 @@ template <class I> ::fltk::ItemGroup * FltkSelectionResource<I>::Item::createNewGroupWidget () { - return new ::fltk::ItemGroup (name); + ::fltk::ItemGroup *itemGroup = + new ::fltk::ItemGroup (name); + itemGroup->user_data ((void *) -1); + return itemGroup; }
@@ -922,12 +926,14 @@ FltkSelectionResource<I>::createNewWidge
::fltk::Group *currGroup = widgetStack->stack->getTop()->getTypedValue();
+ int index = 0; for (Iterator <Item> it = allItems->iterator (); it.hasNext (); ) { Item *item = it.getNext (); switch (item->type) { case Item::ITEM: - itemWidget = item->createNewWidget (); + itemWidget = item->createNewWidget (index); currGroup->add (itemWidget); + index++; break;
case Item::START: @@ -960,6 +966,7 @@ template <class I> void FltkSelectionRes bool enabled, bool selected) { + int index = items->size (); Item *item = createNewItem (Item::ITEM, str, enabled, selected); items->put (item); allItems->append (item); @@ -967,7 +974,7 @@ template <class I> void FltkSelectionRes for (Iterator <WidgetStack> it = widgetStacks->iterator (); it.hasNext(); ) { WidgetStack *widgetStack = it.getNext (); - ::fltk::Item *itemWidget = item->createNewWidget (); + ::fltk::Item *itemWidget = item->createNewWidget (index); widgetStack->stack->getTop()->getTypedValue()->add (itemWidget);
if (!enabled) @@ -1031,10 +1038,10 @@ template <class I> const char *FltkSelec // ----------------------------------------------------------------------
FltkOptionMenuResource::FltkOptionMenuResource (FltkPlatform *platform): - FltkSelectionResource <dw::core::ui::OptionMenuResource> (platform) + FltkSelectionResource <dw::core::ui::OptionMenuResource> (platform), + selection(-1) { init (platform); - selection = NULL; }
FltkOptionMenuResource::~FltkOptionMenuResource () @@ -1057,7 +1064,7 @@ void FltkOptionMenuResource::widgetCallb void *data) { ((FltkOptionMenuResource *) data)->selection = - ((::fltk::Menu *) widget)->item()->user_data(); + (int) (((::fltk::Menu *) widget)->item()->user_data()); }
void FltkOptionMenuResource::sizeRequest (core::Requisition *requisition) @@ -1092,14 +1099,14 @@ void FltkOptionMenuResource::addItem (co FltkSelectionResource<dw::core::ui::OptionMenuResource>::addItem (str,enabled,selected); if (selected) - selection = items->get (items->size()-1); + selection = (items->size ()) - 1;
queueResize (true); }
bool FltkOptionMenuResource::isSelected (int index) { - return items->get(index) == selection; + return index == selection; }
// ---------------------------------------------------------------------- @@ -1107,7 +1114,8 @@ bool FltkOptionMenuResource::isSelected FltkListResource::FltkListResource (FltkPlatform *platform, core::ui::ListResource::SelectionMode selectionMode): - FltkSelectionResource <dw::core::ui::ListResource> (platform) + FltkSelectionResource <dw::core::ui::ListResource> (platform), + itemsSelected(8) { init (platform); } @@ -1119,10 +1127,34 @@ FltkListResource::~FltkListResource ()
::fltk::Menu *FltkListResource::createNewMenu (core::Allocation *allocation) { - return + ::fltk::Menu *menu = new ::fltk::MultiBrowser (allocation->x, allocation->y, allocation->width, allocation->ascent + allocation->descent); + menu->callback(widgetCallback,this); + menu->when(::fltk::WHEN_CHANGED); + return menu; +} + +void FltkListResource::widgetCallback (::fltk::Widget *widget, void *data) +{ + ::fltk::Widget *fltkItem = ((::fltk::Menu *) widget)->item (); + int index = (int) (fltkItem->user_data ()); + if (index == -1) return; + bool selected = fltkItem->selected (); + ((FltkListResource *) data)->itemsSelected.set (index, selected); +} + +void FltkListResource::addItem (const char *str, bool enabled, bool selected) +{ + FltkSelectionResource<dw::core::ui::ListResource>::addItem + (str,enabled,selected); + int index = itemsSelected.size (); + itemsSelected.increase (); + itemsSelected.set (index,selected); + + // blindly copied from FltkOptionMenuResource, is it necessary? + queueResize (true); }
void FltkListResource::sizeRequest (core::Requisition *requisition) @@ -1135,8 +1167,7 @@ void FltkListResource::sizeRequest (core
bool FltkListResource::isSelected (int index) { - /** todo Not implemented. */ - return true; + return itemsSelected.get (index); }
} // namespace ui diff -pru -- 00_pre/dw/fltkui.hh 01_post/dw/fltkui.hh --- 00_pre/dw/fltkui.hh 2008-04-26 21:42:58.000000000 +0100 +++ 01_post/dw/fltkui.hh 2008-05-01 14:00:11.000000000 +0100 @@ -458,8 +458,8 @@ protected: bool selected = false); ~Item ();
- ::fltk::Item *createNewWidget (); - ::fltk::ItemGroup *createNewGroupWidget (); + ::fltk::Item *createNewWidget (int index); + ::fltk::ItemGroup *createNewGroupWidget (); };
class WidgetStack: public object::Object @@ -508,7 +508,7 @@ protected:
private: static void widgetCallback (::fltk::Widget *widget, void *data); - void *selection; + int selection;
public: FltkOptionMenuResource (FltkPlatform *platform); @@ -526,11 +526,17 @@ class FltkListResource: protected: ::fltk::Menu *createNewMenu (core::Allocation *allocation);
+private: + static void widgetCallback (::fltk::Widget *widget, void *data); + misc::SimpleVector <bool> itemsSelected; + public: FltkListResource (FltkPlatform *platform, core::ui::ListResource::SelectionMode selectionMode); ~FltkListResource ();
+ void addItem (const char *str, bool enabled, bool selected); + void sizeRequest (core::Requisition *requisition); bool isSelected (int index); }; diff -pru -- 00_pre/test/dw_ui_test.cc 01_post/test/dw_ui_test.cc --- 00_pre/test/dw_ui_test.cc 2008-04-26 21:42:59.000000000 +0100 +++ 01_post/test/dw_ui_test.cc 2008-05-01 14:00:11.000000000 +0100 @@ -192,7 +192,7 @@ int main(int argc, char **argv) checklabel->addText (strdup (" check"), cellStyle); checklabel->flush ();
- for(int i = 0; i < 1; i++) { + for(int i = 0; i < 2; i++) { table->addRow (cellStyle);
Embed *sel = new Embed (selres[i]);
_______________________________________________ Dillo-dev mailing list Dillo-dev@dillo.org http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/dillo-dev
participants (2)
-
Johannes.Hofmann@gmx.de
-
onepoint@starurchin.org