From d42c3fce150f7285e9aaec6990b03ed8858a223b Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Fri, 25 Jul 2008 19:10:49 +0000 Subject: [PATCH] Add a callback when the selection in a listbox changes. --- src/gui/widgets/listbox.cpp | 7 ++++++- src/gui/widgets/listbox.hpp | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/gui/widgets/listbox.cpp b/src/gui/widgets/listbox.cpp index 9b6790cc2c5..22ebdc4db41 100644 --- a/src/gui/widgets/listbox.cpp +++ b/src/gui/widgets/listbox.cpp @@ -87,6 +87,7 @@ tlistbox::tlistbox() : list_rect_(), list_background_(), best_spacer_size_(0, 0), + callback_value_change_(NULL), rows_() { } @@ -173,7 +174,11 @@ bool tlistbox::list_row_selected(const size_t row, twidget* caller) assert(rows_[row].grid()); if(rows_[row].grid()->has_widget(caller)) { - if(!select_row(row, !rows_[row].get_selected())) { + if(select_row(row, !rows_[row].get_selected())) { + if(callback_value_change_) { + callback_value_change_(this); + } + } else { // if not allowed to deselect reselect. tselectable_* selectable = dynamic_cast(caller); assert(selectable); diff --git a/src/gui/widgets/listbox.hpp b/src/gui/widgets/listbox.hpp index a8c52f93fa0..9c85f49e6fc 100644 --- a/src/gui/widgets/listbox.hpp +++ b/src/gui/widgets/listbox.hpp @@ -57,13 +57,19 @@ public: */ void list_item_selected(twidget* caller); + /***** ***** ***** setters / getters for members ***** ****** *****/ + + void set_callback_value_change(void (*callback) (twidget* caller)) + { callback_value_change_ = callback; } + private: /** * Helper for list_item_selected(). * * Tries to sets the selected state for the row, but only if it contains the - * wanted widget. + * wanted widget. NOTE this function assumes the event was triggered by the + * user and calls the callback handler. * * @param row The row to test. * @param caller The widget to look for. @@ -289,6 +295,13 @@ private: /** The best size for the spacer, if not set it's calculated. */ tpoint best_spacer_size_; + /** + * This callback is used when the selection is chnaged due to a user event. + * The name is not fully appropriate for the event but it's choosen to be + * generic. + */ + void (*callback_value_change_) (twidget* caller); + class trow { public: