gwenhywfar  4.3.1
fox16_htmltext.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     begin       : Tue Feb 23 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 #include "fox16_htmltext.hpp"
00015 #include "fox16_htmlctx.hpp"
00016 
00017 #include <gwenhywfar/debug.h>
00018 #include <gwenhywfar/i18n.h>
00019 
00020 #include <assert.h>
00021 
00022 
00023 #define MAX_DEFAULT_WIDTH 400
00024 #define BORDER 2
00025 
00026 
00027 
00028 FXDEFMAP(FOX16_HtmlText) FOX16_HtmlTextMap[]={
00029   FXMAPFUNC(SEL_PAINT,0,FOX16_HtmlText::onPaint),
00030 };
00031 
00032 
00033 
00034 // Object implementation
00035 FXIMPLEMENT(FOX16_HtmlText, FXScrollArea, FOX16_HtmlTextMap, ARRAYNUMBER(FOX16_HtmlTextMap))
00036 
00037 
00038 
00039 
00040 FOX16_HtmlText::FOX16_HtmlText(FXComposite* p, const FXString& text,
00041                                FXuint opts,
00042                                FXint x, FXint y, FXint w, FXint h)
00043 :FXScrollArea(p, opts, x, y, w, h)
00044 ,m_htmlCtx(NULL)
00045 ,m_minWidth(0)
00046 ,m_maxDefaultWidth(MAX_DEFAULT_WIDTH)
00047 ,m_haveDefaultDims(false)
00048 ,margintop(BORDER)
00049 ,marginbottom(BORDER)
00050 ,marginleft(BORDER)
00051 ,marginright(BORDER)
00052 ,barwidth(8)
00053 {
00054   setText(text);
00055   flags|=FLAG_ENABLED;
00056 }
00057 
00058 
00059 
00060 FOX16_HtmlText::FOX16_HtmlText()
00061 :FXScrollArea()
00062 ,m_htmlCtx(NULL)
00063 ,m_minWidth(0)
00064 {
00065   flags|=FLAG_ENABLED;
00066 }
00067 
00068 
00069 
00070 FOX16_HtmlText::~FOX16_HtmlText() {
00071   if (m_htmlCtx)
00072     delete m_htmlCtx;
00073 }
00074 
00075 
00076 
00077 void FOX16_HtmlText::setText(const FXString& text) {
00078   m_haveDefaultDims=false;
00079   m_text=text;
00080   updateHtml();
00081   flags|=FLAG_DIRTY;
00082   layout();
00083   recalc();
00084   update();
00085 }
00086 
00087 
00088 
00089 void FOX16_HtmlText::calcDefaultDims() {
00090   int w;
00091   int wNeeded;
00092 
00093   m_htmlCtx->layout(-1, -1);
00094   wNeeded=m_htmlCtx->getWidth();
00095   w=wNeeded;
00096   if (w>m_maxDefaultWidth)
00097     w=m_maxDefaultWidth;
00098   if (w<viewport_w)
00099     w=viewport_w;
00100   //if (w<wNeeded) {
00101     m_htmlCtx->layout(w-BORDER*2, -1);
00102   //}
00103   m_defaultWidth=m_htmlCtx->getWidth();
00104   m_defaultHeight=m_htmlCtx->getHeight();
00105   m_haveDefaultDims=true;
00106 }
00107 
00108 
00109 
00110 FXint FOX16_HtmlText::getContentWidth() {
00111   if (m_htmlCtx==NULL)
00112     updateHtml();
00113 
00114   if (!m_haveDefaultDims)
00115     calcDefaultDims();
00116 
00117   m_htmlCtx->layout(viewport_w-(marginleft+marginright+barwidth), -1);
00118   return m_htmlCtx->getWidth();
00119 }
00120 
00121 
00122 
00123 FXint FOX16_HtmlText::getContentHeight() {
00124   if (m_htmlCtx==NULL)
00125     updateHtml();
00126 
00127   if (!m_haveDefaultDims)
00128     calcDefaultDims();
00129 
00130   return m_htmlCtx->getHeight();
00131 }
00132 
00133 
00134 
00135 long FOX16_HtmlText::onPaint(FXObject*, FXSelector, void *ptr) {
00136   FXEvent* event=(FXEvent*)ptr;
00137   FXDCWindow dc(this,event);
00138   //dc.setFont(font);
00139   //dc.setForeground(FXRGB(255,0,0));
00140   //dc.fillRectangle(event->rect.x,event->rect.y,event->rect.w,event->rect.h);
00141 
00142   dc.setForeground(backColor);
00143   dc.fillRectangle(event->rect.x, event->rect.y, event->rect.w, event->rect.h);
00144 
00145   // Paint top margin
00146   if (event->rect.y<=margintop){
00147     dc.setForeground(backColor);
00148     dc.fillRectangle(0, 0, viewport_w, margintop);
00149   }
00150 
00151   // Paint bottom margin
00152   if (event->rect.y+event->rect.h>=viewport_h-marginbottom){
00153     dc.setForeground(backColor);
00154     dc.fillRectangle(0, viewport_h-marginbottom, viewport_w, marginbottom);
00155   }
00156 
00157   // Paint left margin
00158   if(event->rect.x<marginleft){
00159     dc.setForeground(backColor);
00160     dc.fillRectangle(0, margintop, marginleft, viewport_h-margintop-marginbottom);
00161   }
00162 
00163   // Paint right margin
00164   if(event->rect.x+event->rect.w>=viewport_w-marginright){
00165     dc.setForeground(backColor);
00166     dc.fillRectangle(viewport_w-marginright, margintop, marginright, viewport_h-margintop-marginbottom);
00167   }
00168 
00169   // Paint text
00170   //dc.setClipRectangle(marginleft, margintop, viewport_w-marginright-marginleft, viewport_h-margintop-marginbottom);
00171 
00172   //drawContents(dc, event->rect.x, event->rect.y, event->rect.w, event->rect.h);
00173 
00174   if (m_htmlCtx) {
00175 #if 0 /* this doesn work */
00176     m_htmlCtx->paintAt(&dc, -marginleft-pos_x, -margintop-pos_y,
00177                        event->rect.x,
00178                        event->rect.y,
00179                        event->rect.w,
00180                        event->rect.h);
00181 #else
00182     m_htmlCtx->paintAt(&dc, -marginleft-pos_x, -margintop-pos_y,
00183                        0, 0,
00184                        viewport_w-(marginleft+marginright+barwidth),
00185                        viewport_h-(margintop+marginbottom));
00186 #endif
00187   }
00188   else {
00189     DBG_ERROR(GWEN_LOGDOMAIN, "No HtmlContext");
00190   }
00191 
00192   return 1;
00193 }
00194 
00195 
00196 
00197 
00198 void FOX16_HtmlText::layout() {
00199   int w;
00200 
00201   m_haveDefaultDims=false;
00202   if (options & FLAGS_NO_WORDWRAP)
00203     w=-1;
00204   else
00205     w=viewport_w;
00206 
00207   if (m_htmlCtx==NULL)
00208     updateHtml();
00209   m_htmlCtx->layout(w-(marginleft+marginright+barwidth), height-(margintop+marginbottom));
00210 
00211   // Scrollbars adjusted
00212   FXScrollArea::layout();
00213 
00214   update();
00215   flags&=~FLAG_DIRTY;
00216 }
00217 
00218 
00219 
00220 void FOX16_HtmlText::updateHtml() {
00221   if (m_htmlCtx)
00222     delete m_htmlCtx;
00223   m_htmlCtx=new FOX16_HtmlCtx(0);
00224   m_htmlCtx->setBackgroundColor(backColor);
00225   m_htmlCtx->setForegroundColor(fxcolorfromname("black"));
00226   m_htmlCtx->setText(m_text.text());
00227   flags|=FLAG_DIRTY;
00228 }
00229 
00230 
00231 void FOX16_HtmlText::makePositionVisible(FXint pos) {
00232   FXint xPos, yPos;
00233 
00234   xPos=pos_x;
00235   yPos=getContentHeight()-viewport_h;
00236   if (xPos>=0 && yPos>=0)
00237     setPosition(-xPos, -yPos);
00238 }
00239 
00240 
00241 
00242 
00243