]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.h
Remove unnecessary destructors from exception classes
[libs/gltk.git] / source / list.h
index caf403a84a0f1b5ddaf39f8f0ce9e70a5ce71401..37d4d7a33201bdf80894508bd8ab8dd58a962fa2 100644 (file)
@@ -1,23 +1,42 @@
 #ifndef MSP_GLTK_LIST_H_
 #define MSP_GLTK_LIST_H_
 
+#include <stdexcept>
+#include <typeinfo>
 #include <sigc++/signal.h>
 #include "container.h"
 #include "label.h"
 #include "listdata.h"
-#include "vslider.h"
+#include "mspgltk_api.h"
+#include "slider.h"
 
 namespace Msp {
 namespace GLtk {
 
+/**
+Thrown if a list's item type is incompatible with its data.
+*/
+class MSPGLTK_API incompatible_data: public std::logic_error
+{
+public:
+       incompatible_data(const std::type_info &);
+};
+
+
 /**
 Shows a list of items, allowing the user to select one.  A slider is included
 to allow scrolling through a long list.
 */
-class List: virtual public Widget, private Container
+class MSPGLTK_API List: virtual public Widget, private Container
 {
 public:
-       class Loader: public Widget::Loader
+       enum ViewMode
+       {
+               LIST,
+               GRID
+       };
+
+       class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
        {
        public:
                Loader(List &);
@@ -38,89 +57,194 @@ private:
                void item_added(unsigned);
                void item_removed(unsigned);
                void cleared();
-               void refresh_strings();
+               void refresh_item(unsigned);
        };
 
-protected:
-       class Item: public Container
+public:
+       class Item: virtual public Widget, protected Container
        {
+       protected:
+               Item();
+
        public:
                virtual const char *get_class() const { return "listitem"; }
 
-               virtual void autosize();
+       protected:
+               virtual void autosize_special(const Part &, Geometry &) const;
+       public:
                void set_active(bool);
 
                virtual void render_special(const Part &, GL::Renderer &) const;
        };
 
-public:
-       class BasicItem: public Item
+       class SimpleItem: public Item
+       {
+       protected:
+               SimpleItem() = default;
+
+               virtual void on_style_change();
+       };
+
+       class MultiColumnItem: public Item
+       {
+       protected:
+               MultiColumnItem() = default;
+
+               virtual void check_widths(std::vector<unsigned> &) const;
+               virtual void set_widths(const std::vector<unsigned> &);
+
+               virtual void on_style_change();
+       };
+
+private:
+       class BasicItem: public SimpleItem
        {
        private:
                Label label;
 
        public:
                BasicItem(const std::string &);
+       };
 
+       class ItemFactory
+       {
+       protected:
+               ItemFactory() = default;
+       public:
+               virtual ~ItemFactory() = default;
+
+               virtual void set_data(const ListData &) = 0;
+               virtual Item *create_item(unsigned) const = 0;
+       };
+
+       template<typename I>
+       class TypedItemFactory: public ItemFactory
+       {
        private:
-               virtual void on_style_change();
+               typedef typename I::ValueType ValueType;
+
+               const ListDataStore<ValueType> *data;
+
+       public:
+               TypedItemFactory(const ListData &d)
+               { set_data(d); }
+
+               virtual void set_data(const ListData &d)
+               {
+                       if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
+                               data = ds;
+                       else
+                               throw incompatible_data(typeid(ValueType));
+               }
+
+               virtual Item *create_item(unsigned i) const
+               {
+                       return new I(data->get(i));
+               }
+       };
+
+       struct Row
+       {
+               unsigned first;
+               unsigned height;
+
+               Row(unsigned f): first(f), height(0) { }
        };
 
 public:
        sigc::signal<void, unsigned> signal_item_selected;
+       sigc::signal<void> signal_selection_cleared;
 
 private:
-       ListData *data;
-       bool own_data;
-       DataObserver *observer;
-       int sel_index;
-       unsigned first;
-       unsigned max_scroll;
+       ListData *data = nullptr;
+       bool own_data = false;
+       DataObserver *observer = nullptr;
+       ItemFactory *item_factory = nullptr;
+       ViewMode view_mode = LIST;
+       int sel_index = -1;
+       int focus_index = -1;
+       unsigned first_row = 0;
+       unsigned max_scroll = 0;
+       unsigned view_rows = 5;
+       unsigned view_columns = 5;
+       const Part *items_part = nullptr;
+       bool ignore_slider_change = false;
+       bool dragging = false;
+       int drag_start_x = 0;
+       int drag_start_y = 0;
 
        VSlider slider;
        std::vector<Item *> items;
+       std::vector<Row> rows;
 
 public:
        List();
        List(ListData &);
-private:
-       void init();
-public:
        virtual ~List();
 
        virtual const char *get_class() const { return "list"; }
 
-       virtual void autosize();
-       void autosize_rows(unsigned);
-       void autosize_all();
+private:
+       virtual void autosize_special(const Part &, Geometry &) const;
 
+public:
        void set_data(ListData &);
        ListData &get_data() { return *data; }
        const ListData &get_data() const { return *data; }
 private:
        void items_changed();
-protected:
-       virtual Item *create_item(unsigned);
+
 public:
+       template<typename I>
+       void set_item_type()
+       {
+               ItemFactory *f = new TypedItemFactory<I>(*data);
+               delete item_factory;
+               item_factory = f;
+       }
+private:
+       Item *create_item(unsigned);
+
+public:
+       void set_view_mode(ViewMode);
+       void set_view_size(unsigned);
+       void set_view_size(unsigned, unsigned);
+       void set_view_all();
 
        void set_selected_index(int);
        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;
 
 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);
 private:
-       virtual void on_geometry_change();
        virtual void on_style_change();
 
-       void reposition_slider();
-       void reposition_items();
+       void move_focus(Navigation, bool);
+       void set_focus_index(int);
+
+       void item_autosize_changed(Item *);
+       void reposition_items(bool);
+       unsigned last_to_first_row(unsigned) const;
+       unsigned item_index_to_row(unsigned) const;
        void check_view_range();
+       void scroll_to_focus();
        void slider_value_changed(double);
+       static void adjust_index(int &, int, int);
 };
 
+MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &);
+
 } // namespace GLtk
 } // namespace Msp