From: Mikko Rasa Date: Fri, 6 Mar 2015 00:03:34 +0000 (+0200) Subject: Add a selection_cleared signal to List X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=08b48157dc9ca5a4bd59d8eec9fa4e3bff8f0503;p=libs%2Fgltk.git Add a selection_cleared signal to List --- diff --git a/source/dropdown.cpp b/source/dropdown.cpp index e483033..5b1d19b 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -62,13 +62,6 @@ void Dropdown::autosize_special(const Part &part, Geometry &ageom) const } } -void Dropdown::set_selected_index(int index) -{ - list.set_selected_index(index); - if(index<0) - text.set(string()); -} - void Dropdown::rebuild_special(const Part &part) { if(part.get_name()=="text") diff --git a/source/dropdown.h b/source/dropdown.h index 24c62cf..ad8b461 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -49,7 +49,7 @@ public: template void set_item_type() { list.set_item_type(); } - void set_selected_index(int); + void set_selected_index(int i) { list.set_selected_index(i); } int get_selected_index() const { return list.get_selected_index(); } private: diff --git a/source/list.cpp b/source/list.cpp index f61d7c2..0f887f3 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -147,13 +147,16 @@ void List::set_selected_index(int i) if(i>=static_cast(data->size())) throw out_of_range("List::set_selected_index"); - if(i==sel_index) + if(i==sel_index || (i<0 && sel_index<0)) return; if(sel_index>=0) items[sel_index]->set_active(false); if(i<0) + { sel_index = -1; + signal_selection_cleared.emit(); + } else { sel_index = i; @@ -312,6 +315,7 @@ void List::DataObserver::item_added(unsigned i) void List::DataObserver::item_removed(unsigned i) { + bool had_selection = (list.sel_index>=0); if(list.sel_index>static_cast(i)) --list.sel_index; else if(list.sel_index==static_cast(i)) @@ -320,6 +324,9 @@ void List::DataObserver::item_removed(unsigned i) delete list.items[i]; list.items.erase(list.items.begin()+i); list.items_changed(); + + if(had_selection && list.sel_index<0) + list.signal_selection_cleared.emit(); } void List::DataObserver::cleared() @@ -329,6 +336,8 @@ void List::DataObserver::cleared() delete *i; list.items.clear(); list.items_changed(); + + list.signal_selection_cleared.emit(); } void List::DataObserver::refresh_item(unsigned i) diff --git a/source/list.h b/source/list.h index 8e9a851..246a729 100644 --- a/source/list.h +++ b/source/list.h @@ -130,6 +130,7 @@ private: public: sigc::signal signal_item_selected; + sigc::signal signal_selection_cleared; private: ListData *data;