-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#ifndef MSP_GLTK_LIST_H_
#define MSP_GLTK_LIST_H_
+#include <stdexcept>
+#include <typeinfo>
#include <sigc++/signal.h>
-#include "widget.h"
+#include "container.h"
+#include "label.h"
+#include "listdata.h"
+#include "vslider.h"
namespace Msp {
namespace GLtk {
-class VSlider;
+/**
+Thrown if a list's item type is incompatible with its data.
+*/
+class incompatible_data: public std::logic_error
+{
+public:
+ incompatible_data(const std::type_info &);
+ virtual ~incompatible_data() throw() { }
+};
+
/**
Shows a list of items, allowing the user to select one. A slider is included
to allow scrolling through a long list.
*/
-class List: public Widget
+class List: virtual public Widget, private Container
{
public:
- class Loader: public Widget::Loader
+ class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
{
public:
Loader(List &);
};
private:
- std::vector<std::string> items;
+ /// This exists to make disconnecting signals easier
+ class DataObserver: public sigc::trackable
+ {
+ private:
+ List &list;
+
+ public:
+ DataObserver(List &);
+
+ void item_added(unsigned);
+ void item_removed(unsigned);
+ void cleared();
+ void refresh_item(unsigned);
+ };
+
+public:
+ class Item: public Container
+ {
+ public:
+ virtual const char *get_class() const { return "listitem"; }
+
+ protected:
+ virtual void autosize_special(const Part &, Geometry &) const;
+ public:
+ void set_active(bool);
+
+ virtual void render_special(const Part &, GL::Renderer &) const;
+ };
+
+ class MultiColumnItem: public Item
+ {
+ protected:
+ virtual void check_widths(std::vector<unsigned> &) const;
+ virtual void set_widths(const std::vector<unsigned> &);
+
+ private:
+ virtual void on_style_change();
+ };
+
+private:
+ class BasicItem: public Item
+ {
+ private:
+ Label label;
+
+ public:
+ BasicItem(const std::string &);
+
+ private:
+ virtual void on_style_change();
+ };
+
+ class ItemFactory
+ {
+ protected:
+ ItemFactory() { }
+ public:
+ virtual ~ItemFactory() { }
+
+ virtual void set_data(const ListData &) = 0;
+ virtual Item *create_item(unsigned) const = 0;
+ };
+
+ template<typename I>
+ class TypedItemFactory: public ItemFactory
+ {
+ private:
+ 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));
+ }
+ };
+
+public:
+ sigc::signal<void, unsigned> signal_item_selected;
+ sigc::signal<void> signal_selection_cleared;
+
+private:
+ ListData *data;
+ bool own_data;
+ DataObserver *observer;
+ ItemFactory *item_factory;
int sel_index;
+ int focus_index;
unsigned first;
- unsigned n_visible;
- unsigned row_height;
+ unsigned max_scroll;
+ unsigned view_size;
+ bool ignore_slider_change;
+ bool dragging;
+ int drag_start_x;
+ int drag_start_y;
- const Part *items_part;
+ VSlider slider;
+ std::vector<Item *> items;
- VSlider *slider;
- bool slider_active;
+public:
+ List();
+ List(ListData &);
+private:
+ void init();
+public:
+ virtual ~List();
+
+ virtual const char *get_class() const { return "list"; }
+
+private:
+ virtual void autosize_special(const Part &, Geometry &) const;
public:
- sigc::signal<void, unsigned, const std::string &> signal_item_selected;
+ void set_data(ListData &);
+ ListData &get_data() { return *data; }
+ const ListData &get_data() const { return *data; }
+private:
+ void items_changed();
- List(const Resources &);
- ~List();
+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);
- void append(const std::string &);
- void insert(unsigned, const std::string &);
- void remove(unsigned);
- void clear();
- unsigned get_n_items() const { return items.size(); }
+public:
+ void set_view_size(unsigned);
+ void set_view_all();
void set_selected_index(int);
- const std::string &get_selected() const;
int get_selected_index() const { return sel_index; }
+private:
+ void set_selected_item(Widget *);
- virtual void button_press(int, int, unsigned);
- virtual void button_release(int, int, unsigned);
- virtual void pointer_motion(int, int);
+ virtual void rebuild_special(const Part &);
+ virtual void render_special(const Part &, GL::Renderer &) const;
+public:
+ 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 const char *get_class() const { return "list"; }
- virtual void render_special(const Part &) const;
+ void set_focus_index(int);
- virtual void on_geometry_change();
- virtual void on_style_change();
- void reposition_slider();
- void recalculate_parameters();
+ void item_autosize_changed(Item *);
+ unsigned last_to_first(unsigned) const;
+ void check_view_range();
+ void scroll_to_focus();
void slider_value_changed(double);
+ static void adjust_index(int &, int, int);
};
} // namespace GLtk