gwenhywfar  4.8.0beta
o_grid.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Feb 22 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #define DISABLE_DEBUGLOG
15 
16 
17 #include "o_grid_p.h"
18 #include "o_gridentry_l.h"
19 
20 #include <gwenhywfar/debug.h>
21 
22 
23 
24 GWEN_INHERIT(HTML_OBJECT, OBJECT_GRID);
25 
26 
27 #define MAX_COLUMN 32
28 #define COLUMN_SPACING 4
29 #define ROW_SPACING 4
30 
31 
32 
34  OBJECT_GRID *xo;
35  HTML_OBJECT *c;
36  int w;
37  int h;
38  int x;
39  int y;
40  int rv;
41  int i;
42  int j;
43  int cw[MAX_COLUMN];
44  int maxLineHeight;
45  int maxLineWidth;
46  int currentRow;
47 
48  assert(o);
49  xo=GWEN_INHERIT_GETDATA(HTML_OBJECT, OBJECT_GRID, o);
50  assert(xo);
51 
54 
55  /* subtract spacing from available width */
56  if (w!=-1)
57  w-=(xo->columns+1)*COLUMN_SPACING;
58 
59  /* determine the maximum width of each column */
60  for (i=0; i<xo->columns; i++)
61  cw[i]=0;
62  c=HtmlObject_Tree_GetFirstChild(o);
63  while(c) {
64  int k;
65 
67  HtmlObject_SetHeight(c, -1);
68  HtmlObject_SetWidth(c, -1);
69  rv=HtmlObject_Layout(c);
70  if (rv<0) {
71  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
72  return rv;
73  }
75  if (k>cw[i])
76  cw[i]=k;
77  c=HtmlObject_Tree_GetNext(c);
78  }
79 
80  if (w!=-1) {
81  /* do the columns all fit into the width? */
82  x=0;
83  for (i=0; i<xo->columns; i++)
84  x+=cw[i];
85 
86  if (x>w) {
87  int fullw[MAX_COLUMN];
88  int meanColumnWidth;
89  int k;
90 
91  /* doesn't fit, so we need to adjust the columns */
92  meanColumnWidth=w/xo->columns;
93 
94  /* reset full width of every column */
95  for (i=0; i<xo->columns; i++)
96  fullw[i]=0;
97  /* calculate full width of every column */
98  c=HtmlObject_Tree_GetFirstChild(o);
99  while(c) {
101  k=HtmlObject_GetWidth(c);
102  if (k>fullw[i])
103  fullw[i]=k;
104  c=HtmlObject_Tree_GetNext(c);
105  }
106 
107  for (i=0; i<xo->columns; i++)
108  cw[i]=0;
109 
110  /* set fixed widths to those columns which are smaller than fullWidth/columns */
111  k=0;
112  for (i=0; i<xo->columns; i++) {
113  int p;
114 
115  p=fullw[i];
116  if (p<=meanColumnWidth) {
117  k+=p;
118  cw[i]=p;
119  }
120  }
121  /* now get the remaining width */
122  j=0;
123  k=w-k;
124  for (i=0; i<xo->columns; i++) {
125  if (cw[i]==0)
126  j+=fullw[i];
127  }
128 
129  if (j>0) {
130  /* calculate percentual width of each remaining column */
131  for (i=0; i<xo->columns; i++) {
132  if (cw[i]==0) {
133  int p;
134 
135  p=fullw[i]*100/j;
136  cw[i]=p*k/100;
137  }
138  }
139  }
140 
141  /* re-layout columns */
142  c=HtmlObject_Tree_GetFirstChild(o);
143  while(c) {
145  HtmlObject_SetHeight(c, -1);
146  HtmlObject_SetWidth(c, cw[i]);
147  rv=HtmlObject_Layout(c);
148  if (rv<0) {
149  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
150  return rv;
151  }
152  c=HtmlObject_Tree_GetNext(c);
153  }
154  }
155  }
156 
157  /* now layout elements according to column sizes */
158  x=COLUMN_SPACING/2;
159  y=ROW_SPACING/2;
160  maxLineHeight=0;
161  maxLineWidth=0;
162  currentRow=0;
163  c=HtmlObject_Tree_GetFirstChild(o);
164  while(c) {
165  int r;
166  int ch;
167 
170  if (r!=currentRow) {
171  /* next row */
172  y+=maxLineHeight+ROW_SPACING;
173  x=COLUMN_SPACING/2;
174  currentRow=r;
175  maxLineHeight=0;
176  }
177 
178  HtmlObject_SetWidth(c, cw[i]);
180 
181  /* place object */
182  HtmlObject_SetX(c, x);
183  HtmlObject_SetY(c, y);
184 
185  /* calculate maximum height */
186  ch=HtmlObject_GetHeight(c);
187  if (ch>maxLineHeight)
188  maxLineHeight=ch;
189 
190  /* advance */
191  x+=cw[i]+COLUMN_SPACING;
192  if (x>maxLineWidth)
193  maxLineWidth=x;
194  c=HtmlObject_Tree_GetNext(c);
195  }
196  y+=maxLineHeight+(ROW_SPACING/2);
197 
198  HtmlObject_SetWidth(o, maxLineWidth);
199  HtmlObject_SetHeight(o, y);
200 
201  return 0;
202 }
203 
204 
205 
207  HTML_OBJECT *o;
208  OBJECT_GRID *xo;
209 
211  GWEN_NEW_OBJECT(OBJECT_GRID, xo);
213 
218 
219  return o;
220 }
221 
222 
223 
224 void GWENHYWFAR_CB HtmlObject_Grid_FreeData(void *bp, void *p) {
225  OBJECT_GRID *xo;
226 
227  xo=(OBJECT_GRID*) p;
228 
229  GWEN_FREE_OBJECT(xo);
230 }
231 
232 
233 
235  OBJECT_GRID *xo;
236 
237  assert(o);
238  xo=GWEN_INHERIT_GETDATA(HTML_OBJECT, OBJECT_GRID, o);
239  assert(xo);
240 
241  return xo->rows;
242 }
243 
244 
245 
247  OBJECT_GRID *xo;
248 
249  assert(o);
250  xo=GWEN_INHERIT_GETDATA(HTML_OBJECT, OBJECT_GRID, o);
251  assert(xo);
252 
253  xo->rows=i;
254 }
255 
256 
257 
259  OBJECT_GRID *xo;
260 
261  assert(o);
262  xo=GWEN_INHERIT_GETDATA(HTML_OBJECT, OBJECT_GRID, o);
263  assert(xo);
264 
265  return xo->columns;
266 }
267 
268 
269 
271  OBJECT_GRID *xo;
272 
273  assert(o);
274  xo=GWEN_INHERIT_GETDATA(HTML_OBJECT, OBJECT_GRID, o);
275  assert(xo);
276 
277  xo->columns=i;
278 }
279 
280 
281 
282 
283 
284