1 /***************************************************************************
2  begin       : Sat Feb 20 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_box_l.h"
18 #include "htmlctx_l.h"
19 
20 #include <gwenhywfar/debug.h>
21 
22 
23 
24 #define LINE_EXTRA_OFFSET_DIV 20
25 
26 
27 
HtmlObject_Box_Layout(HTML_OBJECT * o)28 static int HtmlObject_Box_Layout(HTML_OBJECT *o)
29 {
30   HTML_OBJECT *c;
31   HTML_OBJECT *cFirstInLine;
32   GWEN_XML_CONTEXT *ctx;
33   int w;
34   //int h;
35   int x=0;
36   int y=0;
37   int maxX=0;
38   int lineHeight=0;
39   int rv;
40   //int resX;
41   int resY;
42 
43   w=HtmlObject_GetWidth(o);
44   //h=HtmlObject_GetHeight(o);
45 
46   ctx=HtmlObject_GetXmlCtx(o);
47   //resX=HtmlCtx_GetResolutionX(ctx);
48   resY=HtmlCtx_GetResolutionY(ctx);
49 
50   c=HtmlObject_Tree_GetFirstChild(o);
51   cFirstInLine=c;
52   while (c) {
53     int th;
54 
55     if ((HtmlObject_GetFlags(c) & HTML_OBJECT_FLAGS_START_ON_NEWLINE) &&
56         x>0) {
57       /* next line */
58       if (x>maxX)
59         maxX=x;
60 
61       /* possibly justify */
62       if (w!=-1 && x<w) {
63         int diff=0;
64 
65         if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
66           diff=w-x;
67         else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
68           diff=(w-x)>>1;
69         }
70         if (diff) {
71           HTML_OBJECT *ct;
72 
73           ct=cFirstInLine;
74           while (ct) {
75             HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
76             if (ct==c)
77               break;
78             ct=HtmlObject_Tree_GetNext(ct);
79           }
80         }
81       }
82 
83       x=0;
84       y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
85       lineHeight=0;
86       cFirstInLine=HtmlObject_Tree_GetNext(c);
87     }
88 
89     HtmlObject_SetHeight(c, -1);
90     if (w==-1) {
91       HtmlObject_SetWidth(c, -1);
92       rv=HtmlObject_Layout(c);
93       if (rv<0) {
94         DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
95         return rv;
96       }
97     }
98     else {
99       int tw;
100 
101       tw=w-x;
102       HtmlObject_SetWidth(c, tw);
103       rv=HtmlObject_Layout(c);
104       if (rv<0) {
105         DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
106         return rv;
107       }
108 
109       if (HtmlObject_GetWidth(c)>tw && x>0) {
110         /* next line */
111         if (x>maxX)
112           maxX=x;
113 
114         /* possibly justify */
115         if (x<w) {
116           int diff=0;
117 
118           if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
119             diff=w-x;
120           else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
121             diff=(w-x)>>1;
122           }
123           if (diff) {
124             HTML_OBJECT *ct;
125 
126             ct=cFirstInLine;
127             while (ct) {
128               HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
129               if (ct==c)
130                 break;
131               ct=HtmlObject_Tree_GetNext(ct);
132             }
133           }
134         }
135 
136         x=0;
137         y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
138         lineHeight=0;
139         cFirstInLine=HtmlObject_Tree_GetNext(c);
140       }
141     }
142 
143     HtmlObject_SetX(c, x);
144     HtmlObject_SetY(c, y);
145 
146     th=HtmlObject_GetHeight(c);
147     if (th>lineHeight)
148       lineHeight=th;
149 
150     x+=HtmlObject_GetWidth(c);
151 
152     if ((HtmlObject_GetFlags(c) & HTML_OBJECT_FLAGS_END_WITH_NEWLINE)) {
153       if (x>0) {
154         /* next line */
155         if (x>maxX)
156           maxX=x;
157 
158         /* possibly justify */
159         if (x<w) {
160           int diff=0;
161 
162           if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
163             diff=w-x;
164           else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
165             diff=(w-x)>>1;
166           }
167           if (diff) {
168             HTML_OBJECT *ct;
169 
170             ct=cFirstInLine;
171             while (ct) {
172               HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
173               if (ct==c)
174                 break;
175               ct=HtmlObject_Tree_GetNext(ct);
176             }
177           }
178         }
179       }
180 
181       x=0;
182       if (lineHeight==0) {
183         HTML_PROPS *pr;
184         HTML_FONT *fnt;
185 
186         pr=HtmlObject_GetProperties(o);
187         assert(pr);
188         fnt=HtmlProps_GetFont(pr);
189         lineHeight=HtmlCtx_GetTextHeight(HtmlObject_GetXmlCtx(o), fnt, "ABCD");
190       }
191       y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
192       lineHeight=0;
193       cFirstInLine=HtmlObject_Tree_GetNext(c);
194     }
195 
196     c=HtmlObject_Tree_GetNext(c);
197   }
198 
199   if (x>0) {
200     /* next line */
201     if (x>maxX)
202       maxX=x;
203 
204     /* possibly justify */
205     if (x<w) {
206       int diff=0;
207 
208       if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
209         diff=w-x;
210       else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
211         diff=(w-x)>>1;
212       }
213       if (diff) {
214         HTML_OBJECT *ct;
215 
216         ct=cFirstInLine;
217         while (ct) {
218           HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
219           ct=HtmlObject_Tree_GetNext(ct);
220         }
221       }
222     }
223 
224     x=0;
225     y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
226     lineHeight=0;
227   }
228 
229   /* finish layout */
230   HtmlObject_SetWidth(o, maxX+1);
231   HtmlObject_SetHeight(o, y+1);
232   return 0;
233 }
234 
235 
236 
HtmlObject_Box_new(GWEN_XML_CONTEXT * ctx)237 HTML_OBJECT *HtmlObject_Box_new(GWEN_XML_CONTEXT *ctx)
238 {
239   HTML_OBJECT *o;
240 
241   o=HtmlObject_new(ctx, HtmlObjectType_Box);
242   HtmlObject_SetLayoutFn(o, HtmlObject_Box_Layout);
243 
244   return o;
245 }
246 
247 
248 
249 
250 
251