]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.h
Rework how widget ownership works in Container
[libs/gltk.git] / source / list.h
index d6cb2b554d859dc04d32307c4bdd9ec6d90dcfdb..5fa687ddbc8ec0ff9712b4fa48baa465373fc897 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_LIST_H_
 #define MSP_GLTK_LIST_H_
 
+#include <memory>
 #include <stdexcept>
 #include <typeinfo>
 #include <sigc++/signal.h>
@@ -20,7 +21,6 @@ class MSPGLTK_API incompatible_data: public std::logic_error
 {
 public:
        incompatible_data(const std::type_info &);
-       virtual ~incompatible_data() throw() { }
 };
 
 
@@ -37,10 +37,13 @@ public:
                GRID
        };
 
+       static constexpr size_t INVALID_INDEX = std::numeric_limits<size_t>::max();
+
        class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
        {
        public:
                Loader(List &);
+
        private:
                void item(const std::string &);
        };
@@ -55,10 +58,10 @@ private:
        public:
                DataObserver(List &);
 
-               void item_added(unsigned);
-               void item_removed(unsigned);
+               void item_added(std::size_t);
+               void item_removed(std::size_t);
                void cleared();
-               void refresh_item(unsigned);
+               void refresh_item(std::size_t);
        };
 
 public:
@@ -68,14 +71,14 @@ public:
                Item();
 
        public:
-               virtual const char *get_class() const { return "listitem"; }
+               const char *get_class() const override { return "listitem"; }
 
        protected:
-               virtual void autosize_special(const Part &, Geometry &) const;
+               void autosize_special(const Part &, Geometry &) const override;
        public:
                void set_active(bool);
 
-               virtual void render_special(const Part &, GL::Renderer &) const;
+               void render_special(const Part &, GL::Renderer &) const override;
        };
 
        class SimpleItem: public Item
@@ -83,7 +86,7 @@ public:
        protected:
                SimpleItem() = default;
 
-               virtual void on_style_change();
+               void on_style_change() override;
        };
 
        class MultiColumnItem: public Item
@@ -94,7 +97,7 @@ public:
                virtual void check_widths(std::vector<unsigned> &) const;
                virtual void set_widths(const std::vector<unsigned> &);
 
-               virtual void on_style_change();
+               void on_style_change() override;
        };
 
 private:
@@ -115,7 +118,7 @@ private:
                virtual ~ItemFactory() = default;
 
                virtual void set_data(const ListData &) = 0;
-               virtual Item *create_item(unsigned) const = 0;
+               virtual std::unique_ptr<Item> create_item(std::size_t) const = 0;
        };
 
        template<typename I>
@@ -127,10 +130,9 @@ private:
                const ListDataStore<ValueType> *data;
 
        public:
-               TypedItemFactory(const ListData &d)
-               { set_data(d); }
+               TypedItemFactory(const ListData &d) { set_data(d); }
 
-               virtual void set_data(const ListData &d)
+               void set_data(const ListData &d) override
                {
                        if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
                                data = ds;
@@ -138,34 +140,34 @@ private:
                                throw incompatible_data(typeid(ValueType));
                }
 
-               virtual Item *create_item(unsigned i) const
+               std::unique_ptr<Item> create_item(std::size_t i) const override
                {
-                       return new I(data->get(i));
+                       return std::make_unique<I>(data->get(i));
                }
        };
 
        struct Row
        {
-               unsigned first;
+               std::size_t first;
                unsigned height;
 
-               Row(unsigned f): first(f), height(0) { }
+               Row(std::size_t f): first(f), height(0) { }
        };
 
 public:
-       sigc::signal<void, unsigned> signal_item_selected;
+       sigc::signal<void, std::size_t> signal_item_selected;
        sigc::signal<void> signal_selection_cleared;
 
 private:
+       std::unique_ptr<ListData> own_data;
        ListData *data = nullptr;
-       bool own_data = false;
-       DataObserver *observer = nullptr;
-       ItemFactory *item_factory = nullptr;
+       std::unique_ptr<DataObserver> observer = nullptr;
+       std::unique_ptr<ItemFactory> item_factory = nullptr;
        ViewMode view_mode = LIST;
-       int sel_index = -1;
-       int focus_index = -1;
-       unsigned first_row = 0;
-       unsigned max_scroll = 0;
+       std::size_t sel_index = INVALID_INDEX;
+       std::size_t focus_index = INVALID_INDEX;
+       std::size_t first_row = 0;
+       std::size_t max_scroll = 0;
        unsigned view_rows = 5;
        unsigned view_columns = 5;
        const Part *items_part = nullptr;
@@ -175,18 +177,18 @@ private:
        int drag_start_y = 0;
 
        VSlider slider;
-       std::vector<Item *> items;
+       std::vector<std::unique_ptr<Item>> items;
        std::vector<Row> rows;
 
+       List(std::unique_ptr<ListData>);
 public:
        List();
        List(ListData &);
-       virtual ~List();
 
-       virtual const char *get_class() const { return "list"; }
+       const char *get_class() const override { return "list"; }
 
 private:
-       virtual void autosize_special(const Part &, Geometry &) const;
+       void autosize_special(const Part &, Geometry &) const override;
 
 public:
        void set_data(ListData &);
@@ -197,14 +199,9 @@ private:
 
 public:
        template<typename I>
-       void set_item_type()
-       {
-               ItemFactory *f = new TypedItemFactory<I>(*data);
-               delete item_factory;
-               item_factory = f;
-       }
+       void set_item_type() { item_factory = std::make_unique<TypedItemFactory<I>>(*data); }
 private:
-       Item *create_item(unsigned);
+       std::unique_ptr<Item> create_item(std::size_t);
 
 public:
        void set_view_mode(ViewMode);
@@ -212,36 +209,36 @@ public:
        void set_view_size(unsigned, unsigned);
        void set_view_all();
 
-       void set_selected_index(int);
+       void set_selected_index(std::size_t);
        int get_selected_index() const { return sel_index; }
 private:
        void set_selected_item(Widget *);
 
-       virtual void rebuild_special(const Part &);
-       virtual void render_special(const Part &, GL::Renderer &) const;
+       void rebuild_special(const Part &) override;
+       void render_special(const Part &, GL::Renderer &) const override;
 
 public:
-       virtual bool key_press(unsigned, unsigned);
-       virtual void button_press(int, int, unsigned);
-       virtual void touch_press(int, int, unsigned);
-       virtual void touch_release(int, int, unsigned);
-       virtual void touch_motion(int, int, unsigned);
-       virtual void focus_in();
-       virtual bool navigate(Navigation);
+       bool key_press(unsigned, unsigned) override;
+       void button_press(int, int, unsigned) override;
+       void touch_press(int, int, unsigned) override;
+       void touch_release(int, int, unsigned) override;
+       void touch_motion(int, int, unsigned) override;
+       void focus_in() override;
+       bool navigate(Navigation) override;
 private:
-       virtual void on_style_change();
+       void on_style_change() override;
 
        void move_focus(Navigation, bool);
-       void set_focus_index(int);
+       void set_focus_index(std::size_t);
 
        void item_autosize_changed(Item *);
        void reposition_items(bool);
-       unsigned last_to_first_row(unsigned) const;
-       unsigned item_index_to_row(unsigned) const;
+       std::size_t last_to_first_row(std::size_t) const;
+       std::size_t item_index_to_row(std::size_t) const;
        void check_view_range();
        void scroll_to_focus();
        void slider_value_changed(double);
-       static void adjust_index(int &, int, int);
+       static void adjust_index(std::size_t &, std::size_t, std::ptrdiff_t);
 };
 
 MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &);