Add to git.
[monolith.git] / src / ml_multicol_layout.c
1 /* Monolith multi-column layout class.
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: ml_multicol_layout.c,v 1.3 2002/11/13 20:46:37 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <pthr_iolib.h>
24
25 #include "ml_widget.h"
26 #include "monolith.h"
27 #include "ml_table_layout.h"
28 #include "ml_multicol_layout.h"
29
30 static void repaint (void *, ml_session, const char *, io_handle);
31 static struct ml_widget_property properties[];
32
33 struct ml_widget_operations multicol_layout_ops =
34   {
35     repaint: repaint,
36     properties: properties,
37   };
38
39 struct ml_multicol_layout
40 {
41   struct ml_widget_operations *ops;
42   pool pool;                    /* Pool for allocations. */
43   ml_table_layout tbl;          /* We're really just a table layout. */
44   int cols;                     /* Fixed number of columns. */
45   int rows;                     /* Current number of rows in the table. */
46   int r, c;                     /* Next row, column. */
47   const char *clazz;            /* Stylesheet class. */
48 };
49
50 static void update_table_class (void *vw);
51
52 static struct ml_widget_property properties[] =
53   {
54     { name: "class",
55       offset: ml_offsetof (struct ml_multicol_layout, clazz),
56       type: ML_PROP_STRING,
57       on_set: update_table_class },
58     { 0 }
59   };
60
61 static void make_cell (ml_multicol_layout w);
62
63 ml_multicol_layout
64 new_ml_multicol_layout (pool pool, int nr_cols)
65 {
66   ml_multicol_layout w = pmalloc (pool, sizeof *w);
67
68   w->ops = &multicol_layout_ops;
69   w->pool = pool;
70   w->tbl = new_ml_table_layout (pool, 0, nr_cols);
71   w->cols = nr_cols;
72   w->rows = 0;
73   w->r = w->c = 0;
74   w->clazz = 0;
75
76   return w;
77 }
78
79 static void
80 update_table_class (void *vw)
81 {
82   ml_multicol_layout w = (ml_multicol_layout) vw;
83
84   ml_widget_set_property (w->tbl, "class", w->clazz);
85 }
86
87 void
88 ml_multicol_layout_set_align (ml_multicol_layout w, const char *align)
89 {
90   make_cell (w);
91   ml_table_layout_set_align (w->tbl, w->r, w->c, align);
92 }
93
94 void
95 ml_multicol_layout_set_valign (ml_multicol_layout w, const char *valign)
96 {
97   make_cell (w);
98   ml_table_layout_set_valign (w->tbl, w->r, w->c, valign);
99 }
100
101 void
102 ml_multicol_layout_set_class (ml_multicol_layout w, const char *clazz)
103 {
104   make_cell (w);
105   ml_table_layout_set_class (w->tbl, w->r, w->c, clazz);
106 }
107
108 void
109 ml_multicol_layout_set_header (ml_multicol_layout w, int is_header)
110 {
111   make_cell (w);
112   ml_table_layout_set_header (w->tbl, w->r, w->c, is_header);
113 }
114
115 void
116 ml_multicol_layout_pack (ml_multicol_layout w, ml_widget _w)
117 {
118   make_cell (w);
119
120   /* Insert the widget. */
121   if (_w) ml_table_layout_pack (w->tbl, _w, w->r, w->c);
122
123   /* Move to the next position. */
124   w->c++;
125   if (w->c >= w->cols)
126     {
127       w->c = 0;
128       w->r++;
129     }
130 }
131
132 static void
133 make_cell (ml_multicol_layout w)
134 {
135   /* Create a new row in the underlying table? */
136   if (w->r >= w->rows)
137     {
138       ml_table_layout_add_row (w->tbl);
139       w->rows++;
140     }
141 }
142
143 static void
144 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
145 {
146   ml_multicol_layout w = (ml_multicol_layout) vw;
147
148   ml_widget_repaint (w->tbl, session, windowid, io);
149 }