gwenhywfar  4.3.1
o_box.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : Sat Feb 20 2010
00003  copyright   : (C) 2010 by Martin Preuss
00004  email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *          Please see toplevel file COPYING for license details           *
00008  ***************************************************************************/
00009 
00010 #ifdef HAVE_CONFIG_H
00011 # include <config.h>
00012 #endif
00013 
00014 #define DISABLE_DEBUGLOG
00015 
00016 
00017 #include "o_box_l.h"
00018 #include "htmlctx_l.h"
00019 
00020 #include <gwenhywfar/debug.h>
00021 
00022 
00023 
00024 #define LINE_EXTRA_OFFSET_DIV 20
00025 
00026 
00027 
00028 static int HtmlObject_Box_Layout(HTML_OBJECT *o) {
00029   HTML_OBJECT *c;
00030   HTML_OBJECT *cFirstInLine;
00031   GWEN_XML_CONTEXT *ctx;
00032   int w;
00033   int h;
00034   int x=0;
00035   int y=0;
00036   int maxX=0;
00037   int lineHeight=0;
00038   int rv;
00039   int resX;
00040   int resY;
00041 
00042   w=HtmlObject_GetWidth(o);
00043   h=HtmlObject_GetHeight(o);
00044 
00045   ctx=HtmlObject_GetXmlCtx(o);
00046   resX=HtmlCtx_GetResolutionX(ctx);
00047   resY=HtmlCtx_GetResolutionY(ctx);
00048 
00049   c=HtmlObject_Tree_GetFirstChild(o);
00050   cFirstInLine=c;
00051   while(c) {
00052     int th;
00053 
00054     if ((HtmlObject_GetFlags(c) & HTML_OBJECT_FLAGS_START_ON_NEWLINE) &&
00055         x>0) {
00056       /* next line */
00057       if (x>maxX)
00058         maxX=x;
00059 
00060       /* possibly justify */
00061       if (w!=-1 && x<w) {
00062         int diff=0;
00063 
00064         if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
00065           diff=w-x;
00066         else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
00067           diff=(w-x)>>1;
00068         }
00069         if (diff) {
00070           HTML_OBJECT *ct;
00071 
00072           ct=cFirstInLine;
00073           while(ct) {
00074             HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
00075             if (ct==c)
00076               break;
00077             ct=HtmlObject_Tree_GetNext(ct);
00078           }
00079         }
00080       }
00081 
00082       x=0;
00083       y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
00084       lineHeight=0;
00085       cFirstInLine=HtmlObject_Tree_GetNext(c);
00086     }
00087 
00088     HtmlObject_SetHeight(c, -1);
00089     if (w==-1) {
00090       HtmlObject_SetWidth(c, -1);
00091       rv=HtmlObject_Layout(c);
00092       if (rv<0) {
00093         DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00094         return rv;
00095       }
00096     }
00097     else {
00098       int tw;
00099 
00100       tw=w-x;
00101       HtmlObject_SetWidth(c, tw);
00102       rv=HtmlObject_Layout(c);
00103       if (rv<0) {
00104         DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00105         return rv;
00106       }
00107 
00108       if (HtmlObject_GetWidth(c)>tw && x>0) {
00109         /* next line */
00110         if (x>maxX)
00111           maxX=x;
00112 
00113         /* possibly justify */
00114         if (x<w) {
00115           int diff=0;
00116   
00117           if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
00118             diff=w-x;
00119           else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
00120             diff=(w-x)>>1;
00121           }
00122           if (diff) {
00123             HTML_OBJECT *ct;
00124 
00125             ct=cFirstInLine;
00126             while(ct) {
00127               HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
00128               if (ct==c)
00129                 break;
00130               ct=HtmlObject_Tree_GetNext(ct);
00131             }
00132           }
00133         }
00134 
00135         x=0;
00136         y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
00137         lineHeight=0;
00138         cFirstInLine=HtmlObject_Tree_GetNext(c);
00139       }
00140     }
00141 
00142     HtmlObject_SetX(c, x);
00143     HtmlObject_SetY(c, y);
00144 
00145     th=HtmlObject_GetHeight(c);
00146     if (th>lineHeight)
00147       lineHeight=th;
00148 
00149     x+=HtmlObject_GetWidth(c);
00150 
00151     if ((HtmlObject_GetFlags(c) & HTML_OBJECT_FLAGS_END_WITH_NEWLINE)) {
00152       if (x>0) {
00153         /* next line */
00154         if (x>maxX)
00155           maxX=x;
00156 
00157         /* possibly justify */
00158         if (x<w) {
00159           int diff=0;
00160 
00161           if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
00162             diff=w-x;
00163           else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
00164             diff=(w-x)>>1;
00165           }
00166           if (diff) {
00167             HTML_OBJECT *ct;
00168 
00169             ct=cFirstInLine;
00170             while(ct) {
00171               HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
00172               if (ct==c)
00173                 break;
00174               ct=HtmlObject_Tree_GetNext(ct);
00175             }
00176           }
00177         }
00178       }
00179 
00180       x=0;
00181       if (lineHeight==0) {
00182         HTML_PROPS *pr;
00183         HTML_FONT *fnt;
00184 
00185         pr=HtmlObject_GetProperties(o);
00186         assert(pr);
00187         fnt=HtmlProps_GetFont(pr);
00188         lineHeight=HtmlCtx_GetTextHeight(HtmlObject_GetXmlCtx(o), fnt, "ABCD");
00189       }
00190       y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
00191       lineHeight=0;
00192       cFirstInLine=HtmlObject_Tree_GetNext(c);
00193     }
00194 
00195     c=HtmlObject_Tree_GetNext(c);
00196   }
00197 
00198   if (x>0) {
00199     /* next line */
00200     if (x>maxX)
00201       maxX=x;
00202 
00203     /* possibly justify */
00204     if (x<w) {
00205       int diff=0;
00206 
00207       if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_RIGHT)
00208         diff=w-x;
00209       else if (HtmlObject_GetFlags(o) & HTML_OBJECT_FLAGS_JUSTIFY_HCENTER) {
00210         diff=(w-x)>>1;
00211       }
00212       if (diff) {
00213         HTML_OBJECT *ct;
00214 
00215         ct=cFirstInLine;
00216         while(ct) {
00217           HtmlObject_SetX(ct, HtmlObject_GetX(ct)+diff);
00218           ct=HtmlObject_Tree_GetNext(ct);
00219         }
00220       }
00221     }
00222 
00223     x=0;
00224     y+=lineHeight+(resY/LINE_EXTRA_OFFSET_DIV);
00225     lineHeight=0;
00226   }
00227 
00228   /* finish layout */
00229   HtmlObject_SetWidth(o, maxX+1);
00230   HtmlObject_SetHeight(o, y+1);
00231   return 0;
00232 }
00233 
00234 
00235 
00236 HTML_OBJECT *HtmlObject_Box_new(GWEN_XML_CONTEXT *ctx) {
00237   HTML_OBJECT *o;
00238 
00239   o=HtmlObject_new(ctx, HtmlObjectType_Box);
00240   HtmlObject_SetLayoutFn(o, HtmlObject_Box_Layout);
00241 
00242   return o;
00243 }
00244 
00245 
00246 
00247 
00248