]> git.tdb.fi Git - libs/gltk.git/blob - source/layout.h
Add a Layout class to automatically position widgets within a Panel
[libs/gltk.git] / source / layout.h
1 #ifndef MSP_GLTK_LAYOUT_H_
2 #define MSP_GLTK_LAYOUT_H_
3
4 #include <list>
5 #include <set>
6 #include <sigc++/trackable.h>
7 #include "geometry.h"
8
9 namespace Msp {
10 namespace GLtk {
11
12 class Container;
13 class Widget;
14
15 class Layout
16 {
17 private:
18         enum
19         {
20                 HORIZONTAL = 0,
21                 VERTICAL = 1,
22                 SELF_POS = 2,
23                 SELF_DIM = 4,
24                 TARGET_POS = 8,
25                 TARGET_DIM = 16,
26                 SPACING = 32
27         };
28
29 public:
30         enum ConstraintType
31         {
32                 ABOVE = VERTICAL|SELF_POS|TARGET_POS|TARGET_DIM|SPACING,
33                 BELOW = VERTICAL|SELF_POS|SELF_DIM|TARGET_POS|SPACING,
34                 RIGHT_OF = HORIZONTAL|SELF_POS|TARGET_POS|TARGET_DIM|SPACING,
35                 LEFT_OF = HORIZONTAL|SELF_POS|SELF_DIM|TARGET_POS|SPACING,
36                 ALIGN_TOP = VERTICAL|SELF_POS|SELF_DIM|TARGET_POS|TARGET_DIM,
37                 ALIGN_BOTTOM = VERTICAL|SELF_POS|TARGET_POS,
38                 ALIGN_RIGHT = HORIZONTAL|SELF_POS|SELF_DIM|TARGET_POS|TARGET_DIM,
39                 ALIGN_LEFT = HORIZONTAL|SELF_POS|TARGET_POS,
40                 COPY_WIDTH = HORIZONTAL|SELF_DIM|TARGET_DIM,
41                 COPY_HEIGHT = VERTICAL|SELF_DIM|TARGET_DIM
42         };
43
44 protected:
45         struct Slot;
46
47         struct Constraint
48         {
49                 ConstraintType type;
50                 Slot &target;
51
52                 Constraint(ConstraintType, Slot &);
53         };
54
55         struct Packing
56         {
57                 int gravity;
58                 bool expand;
59
60                 Packing();
61         };
62
63         struct Slot: public sigc::trackable
64         {
65                 Layout &layout;
66                 unsigned index;
67                 Widget &widget;
68                 Geometry geom;
69                 std::list<Constraint> constraints;
70                 Packing horiz_pack;
71                 Packing vert_pack;
72
73                 Slot(Layout &, Widget &);
74                 virtual ~Slot() { }
75
76                 void autosize_changed();
77         };
78
79         class LinearProgram;
80         struct Pointers;
81
82         Container *container;
83         std::list<Slot *> slots;
84         Sides margin;
85         unsigned row_spacing;
86         unsigned col_spacing;
87
88         static Pointers pointers[2];
89
90 public:
91         Layout();
92         virtual ~Layout();
93
94         void set_container(Container &);
95         void set_margin(const Sides &);
96         void set_spacing(unsigned);
97         void set_row_spacing(unsigned);
98         void set_column_spacing(unsigned);
99
100         void add_widget(Widget &);
101         void remove_widget(Widget &);
102 protected:
103         virtual Slot *create_slot(Widget &);
104         Slot &get_slot_for_widget(Widget &);
105         static ConstraintType complement(ConstraintType);
106 public:
107         void add_constraint(Widget &, ConstraintType, Widget &);
108         void set_gravity(Widget &, int, int);
109         void set_expand(Widget &, bool, bool);
110
111         void update();
112
113 protected:
114         void find_constraint_group(Slot &, ConstraintType, std::set<Slot *> &);
115         void sort_slots(std::list<Slot *> &, ConstraintType);
116         void solve_constraints(int);
117 };
118
119 } // namespace GLtk
120 } // namespace Msp
121
122 #endif