gwenhywfar  4.8.0beta
fox16_htmlctx.cpp
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 #include "fox16_htmlctx_p.hpp"
15 #include "fox16_gui.hpp"
16 
17 #include <assert.h>
18 
19 #include <gwenhywfar/inherit.h>
20 #include <gwenhywfar/debug.h>
21 #include <gwenhywfar/gui_be.h>
22 #include <gwenhywfar/i18n.h>
23 #include <gwenhywfar/htmlctx_be.h>
24 #include <gwenhywfar/o_image_be.h>
25 #include <gwenhywfar/text.h>
26 #include <gwenhywfar/debug.h>
27 #include <gwenhywfar/syncio_memory.h>
28 #include <gwenhywfar/directory.h>
29 
30 
31 
33 GWEN_INHERIT(HTML_FONT, FXFont)
34 GWEN_INHERIT(HTML_IMAGE, FXImage)
35 
36 
37 
38 int FOX16_HtmlCtxLinker::GetTextWidth(GWEN_XML_CONTEXT *ctx,
39  HTML_FONT *fnt,
40  const char *s) {
41  FOX16_HtmlCtx *xctx;
42 
43  assert(ctx);
45  assert(xctx);
46 
47  return xctx->getTextWidth(fnt, s);
48 }
49 
50 
51 
52 int FOX16_HtmlCtxLinker::GetTextHeight(GWEN_XML_CONTEXT *ctx,
53  HTML_FONT *fnt,
54  const char *s) {
55  FOX16_HtmlCtx *xctx;
56 
57  assert(ctx);
59  assert(xctx);
60 
61  return xctx->getTextHeight(fnt, s);
62 }
63 
64 
65 
66 uint32_t FOX16_HtmlCtxLinker::GetColorFromName(const GWEN_XML_CONTEXT *ctx,
67  const char *name) {
68  FOX16_HtmlCtx *xctx;
69 
70  assert(ctx);
72  assert(xctx);
73 
74  return xctx->getColorFromName(name);
75 }
76 
77 
78 
79 HTML_FONT *FOX16_HtmlCtxLinker::GetFont(GWEN_XML_CONTEXT *ctx,
80  const char *fontName,
81  int fontSize,
82  uint32_t fontFlags) {
83  FOX16_HtmlCtx *xctx;
84 
85  assert(ctx);
87  assert(xctx);
88 
89  return xctx->getFont(fontName, fontSize, fontFlags);
90 }
91 
92 
93 
94 HTML_IMAGE *FOX16_HtmlCtxLinker::GetImage(GWEN_XML_CONTEXT *ctx,
95  const char *imageName) {
96  FOX16_HtmlCtx *xctx;
97 
98  assert(ctx);
100  assert(xctx);
101 
102  return xctx->getImage(imageName);
103 
104 }
105 
106 
107 
108 void FOX16_HtmlCtxLinker::freeData(void *bp, void *p) {
109  FOX16_HtmlCtx *xctx;
110 
111  xctx=(FOX16_HtmlCtx*) p;
112  if (xctx->_context)
113  xctx->_context=NULL;
114  delete xctx;
115 }
116 
117 
118 
119 void FOX16_HtmlCtxLinker::freeFontData(void *bp, void *p) {
120  FXFont *xfnt;
121 
122  xfnt=(FXFont*) p;
123  delete xfnt;
124 }
125 
126 
127 
128 
129 void FOX16_HtmlCtxLinker::freeImageData(void *bp, void *p) {
130  FXImage *ximg;
131 
132  ximg=(FXImage*) p;
133  delete ximg;
134 }
135 
136 
137 
138 
139 
141 :_context(NULL)
142 ,_font(NULL)
143 ,_fgColor(0)
144 ,_bgColor(0)
145 ,m_iconSource(NULL)
146 {
147  HTML_PROPS *pr;
148  HTML_FONT *fnt;
149 
150  _context=HtmlCtx_new(flags);
152  FOX16_HtmlCtxLinker::freeData);
153  _font=FXApp::instance()->getNormalFont();
154  HtmlCtx_SetGetTextWidthFn(_context, FOX16_HtmlCtxLinker::GetTextWidth);
155  HtmlCtx_SetGetTextHeightFn(_context, FOX16_HtmlCtxLinker::GetTextHeight);
156  HtmlCtx_SetGetColorFromNameFn(_context, FOX16_HtmlCtxLinker::GetColorFromName);
157  HtmlCtx_SetGetFontFn(_context, FOX16_HtmlCtxLinker::GetFont);
158  HtmlCtx_SetGetImageFn(_context, FOX16_HtmlCtxLinker::GetImage);
159 
160  pr=HtmlProps_new();
161  fnt=HtmlCtx_GetFont(_context, _font->getName().text(), _font->getSize()/10, 0);
162  HtmlProps_SetFont(pr, fnt);
164  HtmlProps_free(pr);
165 
166 }
167 
168 
169 
171  if (_context) {
174  }
175 }
176 
177 
178 
179 FXFont *FOX16_HtmlCtx::_getFoxFont(HTML_FONT *fnt) {
180  FXFont *xfnt;
181 
182  if (GWEN_INHERIT_ISOFTYPE(HTML_FONT, FXFont, fnt)) {
183  xfnt=GWEN_INHERIT_GETDATA(HTML_FONT, FXFont, fnt);
184  return xfnt;
185  }
186  else {
187  FXuint size;
188  FXuint weight;
189  FXuint slant;
190  FXuint encoding;
191  FXString face;
192  uint32_t flags;
193 
194  if (HtmlFont_GetFontName(fnt))
195  face=HtmlFont_GetFontName(fnt);
196  else
197  face=_font->getName();
198  size=HtmlFont_GetFontSize(fnt);
199  weight=FXFont::Normal;
200  slant=_font->getSlant();
201  encoding=_font->getEncoding();
202 
203  flags=HtmlFont_GetFontFlags(fnt);
204  if (flags & HTML_FONT_FLAGS_STRONG)
205  weight=FXFont::Bold;
206  if (flags & HTML_FONT_FLAGS_ITALIC)
207  slant=FXFont::Italic;
208 
210  "Creating font [%s], size=%d, weight=%d, slant=%d, encoding=%d",
211  face.text(), size, weight, slant, encoding);
212 
213  xfnt=new FXFont(FXApp::instance(), face, size, weight, slant, encoding);
214  if (xfnt==NULL) {
216  "Could not create font [%s], size=%d, weight=%d, slant=%d, encoding=%d",
217  face.text(), size, weight, slant, encoding);
218  return NULL;
219  }
220  xfnt->create();
221  GWEN_INHERIT_SETDATA(HTML_FONT, FXFont, fnt, xfnt,
222  FOX16_HtmlCtxLinker::freeFontData);
223  return xfnt;
224  }
225 }
226 
227 
228 
229 int FOX16_HtmlCtx::getTextWidth(HTML_FONT *fnt, const char *s) {
230  if (s==NULL) {
231  DBG_ERROR(GWEN_LOGDOMAIN, "NULLPOINTER, returning size 0");
232  return 0;
233  }
234  else {
235  FXFont *xfnt;
236  FXString str;
237 
238  str=FXString(s);
239  xfnt=_getFoxFont(fnt);
240  if (xfnt==NULL)
241  return _font->getTextWidth(str);
242  else
243  return xfnt->getTextWidth(str);
244  }
245 }
246 
247 
248 
249 int FOX16_HtmlCtx::getTextHeight(HTML_FONT *fnt, const char *s) {
250  if (s==NULL) {
251  DBG_ERROR(GWEN_LOGDOMAIN, "NULLPOINTER, returning size 0");
252  return 0;
253  }
254  else {
255  FXFont *xfnt;
256  FXString str;
257 
258  str=FXString(s);
259  xfnt=_getFoxFont(fnt);
260  if (xfnt==NULL)
261  return _font->getTextHeight(str);
262  else
263  return xfnt->getTextHeight(str);
264  }
265 }
266 
267 
268 
269 uint32_t FOX16_HtmlCtx::getColorFromName(const char *name) {
270  return fxcolorfromname(name);
271 }
272 
273 
274 
275 int FOX16_HtmlCtx::layout(int width, int height) {
276  return HtmlCtx_Layout(_context, width, height);
277 }
278 
279 
280 
281 void FOX16_HtmlCtx::setText(const char *s) {
282  int rv;
283 
285  if (rv<0) {
286  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
287  }
288 }
289 
290 
291 
292 void FOX16_HtmlCtx::addMediaPath(const char *s) {
294 }
295 
296 
297 
298 static void dumpObject(HTML_OBJECT *o, FILE *f, int indent) {
299  HTML_OBJECT *c;
300  int i;
301  const char *s;
302  HTML_PROPS *pr;
303  HTML_FONT *fnt;
304 
305  s=HtmlObject_GetText(o);
306  for (i=0; i<indent; i++) fprintf(f, " ");
307  fprintf(stderr, "Object type: %d [%s] flags: %08x, x=%d, y=%d, w=%d, h=%d\n",
309  s?s:"(empty)",
311  HtmlObject_GetX(o),
312  HtmlObject_GetY(o),
315 
317  fnt=HtmlProps_GetFont(pr);
318 
319  for (i=0; i<indent+2; i++) fprintf(f, " ");
320  fprintf(stderr, "fgcol=%06x, bgcol=%06x, fontsize=%d, fontflags=%08x, fontname=[%s]\n",
325  HtmlFont_GetFontName(fnt));
326 
327  c=HtmlObject_Tree_GetFirstChild(o);
328  while(c) {
329  dumpObject(c, f, indent+2);
330  c=HtmlObject_Tree_GetNext(c);
331  }
332 }
333 
334 
335 
337  HTML_OBJECT *o;
338 
340  if (o)
341  dumpObject(o, stderr, 2);
342 }
343 
344 
345 
346 void FOX16_HtmlCtx::_paint(FXDC *dc, HTML_OBJECT *o, int xOffset, int yOffset) {
347  HTML_OBJECT *c;
348 
349  xOffset+=HtmlObject_GetX(o);
350  yOffset+=HtmlObject_GetY(o);
351 
352  switch(HtmlObject_GetObjectType(o)) {
353  case HtmlObjectType_Word: {
354  HTML_PROPS *pr;
355  HTML_FONT *fnt;
356  FXFont *xfnt;
357  int ascent=0;
358  uint32_t col;
359 
361 
362  /* select font */
363  fnt=HtmlProps_GetFont(pr);
364  xfnt=_getFoxFont(fnt);
365  if (xfnt) {
366  dc->setFont(xfnt);
367  ascent=xfnt->getFontAscent();
368  }
369 
370  /* select foreground color */
372  if (col==HTML_PROPS_NOCOLOR)
373  dc->setForeground(_fgColor);
374  else
375  dc->setForeground(col);
376 
377  /* select background color */
379  if (col==HTML_PROPS_NOCOLOR)
380  dc->setBackground(_bgColor);
381  else
382  dc->setBackground(col);
383 
384  dc->drawText(xOffset, yOffset+ascent, HtmlObject_GetText(o));
385  break;
386  }
387 
388  case HtmlObjectType_Image: {
389  HTML_IMAGE *img;
390 
392  if (img) {
393  FXImage *ximg;
394 
395  ximg=GWEN_INHERIT_GETDATA(HTML_IMAGE, FXImage, img);
396  if (ximg) {
397  HTML_PROPS *pr;
398  uint32_t col;
399 
401 
402  /* select background color */
404  if (col==HTML_PROPS_NOCOLOR) {
405  dc->setBackground(_bgColor);
406  dc->setForeground(_bgColor);
407  }
408  else {
409  dc->setBackground(col);
410  dc->setForeground(col);
411  }
412 
413  dc->fillRectangle(xOffset, yOffset, ximg->getWidth(), ximg->getHeight());
414 
415  dc->drawImage(ximg, xOffset, yOffset);
416  }
417  }
418  break;
419  }
420  default:
421  break;
422  }
423 
424  c=HtmlObject_Tree_GetFirstChild(o);
425  while(c) {
426  _paint(dc, c, xOffset, yOffset);
427  c=HtmlObject_Tree_GetNext(c);
428  }
429 }
430 
431 
432 
434  int xOffset, int yOffset,
435  int xText, int yText,
436  int w, int h) {
437  HTML_OBJECT *c;
438  int x;
439  int y;
440  int printX;
441  int printY;
442  int objectW;
443  int objectH;
444 
445  x=xText+HtmlObject_GetX(o);
446  y=yText+HtmlObject_GetY(o);
447  objectW=HtmlObject_GetWidth(o);
448  objectH=HtmlObject_GetHeight(o);
449 
450  printX=x-xOffset;
451  printY=y-yOffset;
452 
453  if (printX<w && printX+objectW>=0 &&
454  printY<h && printY+objectH>=0) {
455  switch(HtmlObject_GetObjectType(o)) {
456 #if 0
457  case HtmlObjectType_Grid:
458  dc->setForeground(FXRGB(255,0,0));
459  dc->fillRectangle(printX, printY,
462 #endif
463  case HtmlObjectType_Word: {
464  HTML_PROPS *pr;
465  HTML_FONT *fnt;
466  FXFont *xfnt;
467  int ascent=0;
468  uint32_t col;
469 
471 
472  /* select font */
473  fnt=HtmlProps_GetFont(pr);
474  xfnt=_getFoxFont(fnt);
475  if (xfnt) {
476  dc->setFont(xfnt);
477  ascent=xfnt->getFontAscent();
478  }
479 
480  /* select foreground color */
482  if (col==HTML_PROPS_NOCOLOR)
483  dc->setForeground(_fgColor);
484  else
485  dc->setForeground(col);
486 
487  /* select background color */
489  if (col==HTML_PROPS_NOCOLOR)
490  dc->setBackground(_bgColor);
491  else
492  dc->setBackground(col);
493 
494  dc->drawText(printX, printY+ascent, HtmlObject_GetText(o));
495  break;
496  }
497 
498  case HtmlObjectType_Image: {
499  HTML_IMAGE *img;
500 
502  if (img) {
503  FXImage *ximg;
504 
505  ximg=GWEN_INHERIT_GETDATA(HTML_IMAGE, FXImage, img);
506  if (ximg) {
507  HTML_PROPS *pr;
508  uint32_t col;
509 
511 
512  /* select background color */
514  if (col==HTML_PROPS_NOCOLOR) {
515  dc->setBackground(_bgColor);
516  dc->setForeground(_bgColor);
517  }
518  else {
519  dc->setBackground(col);
520  dc->setForeground(col);
521  }
522  dc->fillRectangle(printX, printY, ximg->getWidth(), ximg->getHeight());
523 
524  dc->drawImage(ximg, printX, printY);
525  }
526  }
527  break;
528  }
529  default:
530  break;
531  }
532 
533 
534  c=HtmlObject_Tree_GetFirstChild(o);
535  while(c) {
536  _paintAt(dc, c, xOffset, yOffset, x, y, w, h);
537  c=HtmlObject_Tree_GetNext(c);
538  }
539  }
540 }
541 
542 
543 
544 void FOX16_HtmlCtx::paint(FXDC *dc, int xOffset, int yOffset) {
545  HTML_OBJECT *o;
546 
548  if (o)
549  _paint(dc, o, xOffset, yOffset);
550 }
551 
552 
553 
555  int xOffset, int yOffset,
556  int xText, int yText,
557  int w, int h) {
558  HTML_OBJECT *o;
559 
561  if (o)
562  _paintAt(dc, o, xOffset, yOffset, xText, yText, w, h);
563 }
564 
565 
566 
568  HTML_OBJECT *o;
569 
571  if (o)
572  return HtmlObject_GetWidth(o);
573  else
574  return -1;
575 }
576 
577 
578 
580  HTML_OBJECT *o;
581 
583  if (o)
584  return HtmlObject_GetHeight(o);
585  else
586  return -1;
587 }
588 
589 
590 
591 HTML_FONT *FOX16_HtmlCtx::getFont(const char *fontName,
592  int fontSize,
593  uint32_t fontFlags) {
594  FOX16_Gui *gui;
595 
596  gui=FOX16_Gui::getFgGui();
597  assert(gui);
598 
599  return gui->getFont(fontName, fontSize, fontFlags);
600 }
601 
602 
603 
604 HTML_IMAGE *FOX16_HtmlCtx::getImage(const char *fileName) {
605  GWEN_STRINGLIST *sl;
606 
608  if (sl) {
609  GWEN_BUFFER *tbuf;
610  int rv;
611  FXImage *ximg;
612  HTML_IMAGE *img;
613 
614  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
615  rv=GWEN_Directory_FindFileInPaths(sl, fileName, tbuf);
616  if (rv<0) {
617  DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", rv);
618  GWEN_Buffer_free(tbuf);
619  return NULL;
620  }
621 
622  if (m_iconSource==NULL)
623  m_iconSource=new FXIconSource(FXApp::instance());
624 
625  ximg=m_iconSource->loadIconFile(GWEN_Buffer_GetStart(tbuf));
626  if (ximg==NULL) {
627  DBG_ERROR(GWEN_LOGDOMAIN, "Could not load icon [%s]", GWEN_Buffer_GetStart(tbuf));
628  GWEN_Buffer_free(tbuf);
629  return NULL;
630  }
631 
632  ximg->create();
633  img=HtmlImage_new();
635  HtmlImage_SetWidth(img, ximg->getWidth());
636  HtmlImage_SetHeight(img, ximg->getHeight());
637 
638  GWEN_INHERIT_SETDATA(HTML_IMAGE, FXImage, img, ximg,
639  FOX16_HtmlCtxLinker::freeImageData);
640  GWEN_Buffer_free(tbuf);
641  return img;
642  }
643  else {
644  DBG_ERROR(GWEN_LOGDOMAIN, "No media paths in dialog");
645  return NULL;
646  }
647 }
648 
649 
650 
651 
653  _bgColor=c;
654 }
655 
656 
657 
659  _fgColor=c;
660 }
661 
662 
663 
664 
665 
666 
667 
668