1 #ifndef MSP_GLTK_LAYOUT_H_
2 #define MSP_GLTK_LAYOUT_H_
6 #include <sigc++/trackable.h>
7 #include <msp/strings/lexicalcast.h>
18 Positions Widgets inside a Container.
20 A layout operates on constraints, which are used to form a linear program that
21 is then solved to obtain positions and dimensions that fulfill the constraints.
22 There are three kinds of constraints available: ordering, alignment and
25 Ordering constraints specify that the widgets should be placed next to each
26 other along X or Y axis. These operate on one axis at a time, so a widget
27 could be "right of" another even if they are separated by hundreds of pixels
28 vertically. The widgets will be separated by a spacing value, which is
29 settable on a per-layout basis.
31 Alignment constraints make the corresponding edges of two widgets be on the
32 same line. These are incompatible with ordering constraints, so only one or
33 the other should be used between any pair of widgets for the same axis.
35 Dimension matching constraints force the two widgets to have the same dimension
36 along the relevant axis.
38 In addition to constraints, there are some other properties that can be set on
39 widgets to determine how they are laid out. Gravity affects which edge of the
40 container the widget should be placed at. This is a relatively weak hint and
41 will be overridden by many other things. Margins can also be specified to
42 prevent widgets from getting too close to the container's edges.
44 Usually widgets are made as small as their content allows. Setting the expand
45 flag for a widget causes it to use as much space as possible. If multiple co-
46 dependent widgets have the expand flag set, the results are currently
49 Since specifiyng constraints manually can be quite tedious, an Arrangement
50 interface is provided to automatically arrange widgets. See classes Row,
51 Column and Grid for some commonly used arrangements.
73 ABOVE = VERTICAL|SELF_POS|TARGET_POS|TARGET_DIM|SPACING,
74 BELOW = VERTICAL|SELF_POS|SELF_DIM|TARGET_POS|SPACING,
75 RIGHT_OF = HORIZONTAL|SELF_POS|TARGET_POS|TARGET_DIM|SPACING,
76 LEFT_OF = HORIZONTAL|SELF_POS|SELF_DIM|TARGET_POS|SPACING,
77 FAR_ABOVE = VERTICAL|SELF_POS|TARGET_POS|TARGET_DIM|SPACING|SLACK,
78 FAR_BELOW = VERTICAL|SELF_POS|SELF_DIM|TARGET_POS|SPACING|SLACK,
79 FAR_RIGHT_OF = HORIZONTAL|SELF_POS|TARGET_POS|TARGET_DIM|SPACING|SLACK,
80 FAR_LEFT_OF = HORIZONTAL|SELF_POS|SELF_DIM|TARGET_POS|SPACING|SLACK,
81 ALIGN_TOP = VERTICAL|SELF_POS|SELF_DIM|TARGET_POS|TARGET_DIM,
82 ALIGN_BOTTOM = VERTICAL|SELF_POS|TARGET_POS,
83 ALIGN_RIGHT = HORIZONTAL|SELF_POS|SELF_DIM|TARGET_POS|TARGET_DIM,
84 ALIGN_LEFT = HORIZONTAL|SELF_POS|TARGET_POS,
85 COPY_WIDTH = HORIZONTAL|SELF_DIM|TARGET_DIM,
86 COPY_HEIGHT = VERTICAL|SELF_DIM|TARGET_DIM
89 class Loader: public DataFile::ObjectLoader<Layout>
92 typedef std::map<std::string, Widget *> WidgetMap;
95 const WidgetMap &wdg_map;
98 Loader(Layout &, const WidgetMap &);
101 void column_spacing(unsigned);
103 void row_spacing(unsigned);
104 void spacing(unsigned);
105 void widget(const std::string &);
109 class WidgetLoader: public DataFile::Loader
114 const Layout::Loader::WidgetMap &wdg_map;
117 WidgetLoader(Layout &, Widget &, const Layout::Loader::WidgetMap &);
120 void constraint(ConstraintType, const std::string &);
121 void expand(bool, bool);
123 void gravity(int, int);
134 Constraint(ConstraintType, Slot &);
145 struct Slot: public sigc::trackable
150 Geometry autosize_geom;
152 std::list<Constraint> constraints;
157 Slot(Layout &, Widget &);
159 void autosize_changed();
160 void visibility_changed(bool);
172 Container *container;
173 std::list<Slot *> slots;
174 unsigned n_active_slots;
175 unsigned n_slack_constraints[2];
177 unsigned row_spacing;
178 unsigned col_spacing;
179 Geometry autosize_geom;
180 std::list<Arrangement *> arrangement_stack;
182 static Pointers pointers[2];
188 void set_container(Container &);
189 void set_margin(const Sides &);
191 /** Sets the default spacing between widgets in bothg directions. */
192 void set_spacing(unsigned);
194 /** Sets the default vertical spacing between widgets. Affects the ABOVE
195 and BELOW constraints. */
196 void set_row_spacing(unsigned);
198 /** Sets the default horizontal spacing between widgets. Affects the
199 LEFT_OF and RIGHT_OF constraints. */
200 void set_column_spacing(unsigned);
202 void push_arrangement(Arrangement &);
203 Arrangement *get_arrangement() const;
204 void pop_arrangement(Arrangement &);
206 void add_widget(Widget &);
207 void remove_widget(Widget &);
209 void update_slot_indices();
210 Slot &get_slot_for_widget(Widget &);
211 static ConstraintType complement(ConstraintType);
212 void create_constraint(Widget &, ConstraintType, Widget &, int);
215 /** Adds a constraint between two widgets. */
216 void add_constraint(Widget &src, ConstraintType type, Widget &tgt);
218 /** Adds a constraint between two widgets, overriding the default spacing.
219 Not all constraint types use a spacing. */
220 void add_constraint(Widget &src, ConstraintType type, Widget &tgt, unsigned);
222 void set_gravity(Widget &, int, int);
223 void set_expand(Widget &, bool, bool);
225 /// Sets a widget as a ghost, taking up space even if it is hidden.
226 void set_ghost(Widget &, bool);
229 void autosize(Geometry &);
232 void solve_constraints(int, SolveMode);
235 void operator>>(const LexicalConverter &, Layout::ConstraintType &);