summaryrefslogtreecommitdiff
path: root/src/gui/widgets/layout.h
blob: 8fd7a4054b7df68948794739878b8020fcf10900 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 *  The Mana Client
 *  Copyright (C) 2007-2009  The Mana World Development Team
 *  Copyright (C) 2009-2012  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef WIDGET_LAYOUT_H
#define WIDGET_LAYOUT_H

#include <guichan/widgets/container.hpp>

#include <vector>

class LayoutCell;

/**
 * This class is a helper for adding widgets to nested tables in a window.
 */
class ContainerPlacer
{
    public:
        ContainerPlacer(gcn::Container *c = nullptr, LayoutCell *l = nullptr):
            mContainer(c), mCell(l)
        {}

        /**
         * Gets the pointed cell.
         */
        LayoutCell &getCell()
        { return *mCell; }

        /**
         * Returns a placer for the same container but to an inner cell.
         */
        ContainerPlacer at(int x, int y);

        /**
         * Adds the given widget to the container and places it in the layout.
         * @see LayoutArray::place
         */
        LayoutCell &operator()
            (int x, int y, gcn::Widget *, int w = 1, int h = 1);

    private:
        gcn::Container *mContainer;
        LayoutCell *mCell;
};

/**
 * This class contains a rectangular array of cells.
 */
class LayoutArray
{
    friend class LayoutCell;

    public:

        LayoutArray();

        ~LayoutArray();

        /**
         * Returns a reference on the cell at given position.
         */
        LayoutCell &at(int x, int y, int w = 1, int h = 1);

        /**
         * Places a widget in a given cell.
         * @param w number of columns the widget spawns.
         * @param h number of rows the widget spawns.
         * @note When @a w is 1, the width of column @a x is reset to zero if
         *       it was AUTO_DEF. Similarly for @a h.
         */
        LayoutCell &place(gcn::Widget *, int x, int y, int w = 1, int h = 1);

        /**
         * Sets the minimum width of a column.
         */
        void setColWidth(int n, int w);

        /**
         * Sets the minimum height of a row.
         */
        void setRowHeight(int n, int h);

        /**
         * Sets the widths of two columns to the maximum of their widths.
         */
        void matchColWidth(int n1, int n2);

        /**
         * Spawns a cell over several columns/rows.
         */
        void extend(int x, int y, int w, int h);

        /**
         * Computes and sets the positions of all the widgets.
         * @param nW width of the array, used to resize the AUTO_ columns.
         * @param nH height of the array, used to resize the AUTO_ rows.
         */
        void reflow(int nX, int nY, int nW, int nH);

    private:

        // Copy not allowed, as the array owns all its cells.
        LayoutArray(LayoutArray const &);
        LayoutArray &operator=(LayoutArray const &);

        /**
         * Gets the position and size of a widget along a given axis
         */
        void align(int &pos, int &size, int dim,
                   LayoutCell const &cell, short *sizes) const;

        /**
         * Ensures the private vectors are large enough.
         */
        void resizeGrid(int w, int h);

        /**
         * Gets the column/row sizes along a given axis.
         * @param upp target size for the array. Ignored if AUTO_DEF.
         */
        std::vector< short > getSizes(int dim, int upp) const;

        /**
         * Gets the total size along a given axis.
         */
        int getSize(int dim) const;

        std::vector< short > mSizes[2];
        std::vector< std::vector < LayoutCell * > > mCells;

        char mSpacing;
};

/**
 * This class describes the formatting of a widget in the cell of a layout
 * table. Horizontally, a widget can either fill the width of the cell (minus
 * the cell padding), or it can retain its size and be flushed left, or flush
 * right, or centered in the cell. The process is similar for the vertical
 * alignment, except that top is represented by LEFT and bottom by RIGHT.
 */
class LayoutCell
{
    friend class Layout;
    friend class LayoutArray;

    public:
        enum Alignment
        {
            LEFT, RIGHT, CENTER, FILL
        };

        LayoutCell(): mType(NONE) {}

        ~LayoutCell();

        /**
         * Sets the padding around the cell content.
         */
        LayoutCell &setPadding(int p)
        { mPadding = p; return *this; }

        /**
         * Sets the horizontal alignment of the cell content.
         */
        LayoutCell &setHAlign(Alignment a)
        { mAlign[0] = a; return *this; }

        /**
         * Sets the vertical alignment of the cell content.
         */
        LayoutCell &setVAlign(Alignment a)
        { mAlign[1] = a; return *this; }

        /**
         * @see LayoutArray::at
         */
        LayoutCell &at(int x, int y)
        { return getArray().at(x, y); }

        /**
         * @see LayoutArray::place
         */
        LayoutCell &place(gcn::Widget *wg, int x, int y, int w = 1, int h = 1)
        { return getArray().place(wg, x, y, w, h); }

        /**
         * @see LayoutArray::matchColWidth
         */
        void matchColWidth(int n1, int n2)
        { getArray().matchColWidth(n1, n2); }

        /**
         * @see LayoutArray::setColWidth
         */
        void setColWidth(int n, int w)
        { getArray().setColWidth(n, w); }

        /**
         * @see LayoutArray::setRowHeight
         */
        void setRowHeight(int n, int h)
        { getArray().setRowHeight(n, h); }

        /**
         * @see LayoutArray::extend.
         */
        void extend(int x, int y, int w, int h)
        { getArray().extend(x, y, w, h); }

        /**
         * Sets the minimum widths and heights of this cell and of all the
         * inner cells.
         */
        void computeSizes();

    private:
        // Copy not allowed, as the cell may own an array.
        LayoutCell(LayoutCell const &);
        LayoutCell &operator=(LayoutCell const &);

        union
        {
            gcn::Widget *mWidget;
            LayoutArray *mArray;
        };

        enum
        {
            NONE, WIDGET, ARRAY
        };

        /**
         * Returns the embedded array. Creates it if the cell does not contain
         * anything yet. Aborts if it contains a widget.
         */
        LayoutArray &getArray();

        /**
         * @see LayoutArray::reflow
         */
        void reflow(int nx, int ny, int nw, int nh);

        short mSize[2];
        char mPadding;
        char mExtent[2];
        char mAlign[2];
        char mNbFill[2];
        char mType;
};

/**
 * This class is an helper for setting the position of widgets. They are
 * positioned along the cells of some rectangular tables. The layout may either
 * be a single table or a tree of nested tables.
 *
 * The size of a given table column can either be set manually or be chosen
 * from the widest widget of the column. An empty column has a AUTO_DEF width,
 * which means it will be extended so that the layout fits its minimum width.
 *
 * The process is similar for table rows. By default, there is a spacing of 4
 * pixels between rows and between columns, and a margin of 6 pixels around the
 * whole layout.
 */
class Layout : public LayoutCell
{
    public:
        Layout();

        /**
         * Sets the margin around the layout.
         */
        void setMargin(int m)
        { setPadding(m); }

        /**
         * Sets the positions of all the widgets.
         * @see LayoutArray::reflow
         */
        void reflow(int &nW, int &nH);

        /**
         * When the minimum size of the layout is less than the available size,
         * the remaining pixels are equally split amongst the FILL items.
         */
        enum
        {
            AUTO_DEF = -42, /**< Default value, behaves like AUTO_ADD. */
            AUTO_SET = -43, /**< Uses the share as the new size. */
            AUTO_ADD = -44  /**< Adds the share to the current size. */
        };

    private:
        bool mComputed;
};

#endif // WIDGET_LAYOUT_H