gwenhywfar
4.3.1
|
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_htmllabel.hpp" 00015 #include "fox16_htmlctx.hpp" 00016 00017 #include <gwenhywfar/debug.h> 00018 #include <gwenhywfar/i18n.h> 00019 #include <gwenhywfar/stringlist.h> 00020 00021 #include <assert.h> 00022 00023 00024 #define MAX_DEFAULT_WIDTH 400 00025 #define ICON_SPACE 4 00026 00027 00028 00029 FXDEFMAP(FOX16_HtmlLabel) FOX16_HtmlLabelMap[]={ 00030 FXMAPFUNC(SEL_PAINT,0,FOX16_HtmlLabel::onPaint), 00031 }; 00032 00033 00034 00035 // Object implementation 00036 FXIMPLEMENT(FOX16_HtmlLabel, FXFrame, FOX16_HtmlLabelMap, ARRAYNUMBER(FOX16_HtmlLabelMap)) 00037 00038 00039 00040 00041 FOX16_HtmlLabel::FOX16_HtmlLabel(FXComposite* p, const FXString& text, 00042 FXuint opts, 00043 FXint x, FXint y, FXint w, FXint h, 00044 FXint pl, FXint pr, 00045 FXint pt, FXint pb) 00046 :FXFrame(p, opts, x, y, w, h, pl, pr, pt, pb) 00047 ,m_htmlCtx(NULL) 00048 ,m_minWidth(0) 00049 ,m_maxDefaultWidth(MAX_DEFAULT_WIDTH) 00050 ,m_haveDefaultDims(false) 00051 ,m_mediaPaths(NULL) 00052 ,m_icon(NULL) 00053 { 00054 m_mediaPaths=GWEN_StringList_new(); 00055 setText(text); 00056 flags|=FLAG_ENABLED|FLAG_DIRTY|FLAG_RECALC; 00057 00058 } 00059 00060 00061 00062 FOX16_HtmlLabel::FOX16_HtmlLabel() 00063 :FXFrame() 00064 ,m_htmlCtx(NULL) 00065 ,m_minWidth(0) 00066 ,m_mediaPaths(NULL) 00067 ,m_icon(NULL) 00068 { 00069 flags|=FLAG_ENABLED; 00070 } 00071 00072 00073 00074 FOX16_HtmlLabel::~FOX16_HtmlLabel() { 00075 if (m_htmlCtx) 00076 delete m_htmlCtx; 00077 GWEN_StringList_free(m_mediaPaths); 00078 } 00079 00080 00081 00082 void FOX16_HtmlLabel::setText(const FXString& text) { 00083 m_haveDefaultDims=false; 00084 m_text=text; 00085 updateHtml(); 00086 flags|=FLAG_DIRTY; 00087 layout(); 00088 recalc(); 00089 update(); 00090 } 00091 00092 00093 00094 void FOX16_HtmlLabel::addMediaPath(const char *s) { 00095 assert(s); 00096 GWEN_StringList_AppendString(m_mediaPaths, s, 0, 1); 00097 } 00098 00099 00100 00101 void FOX16_HtmlLabel::setIcon(FXIcon *ic) { 00102 m_icon=ic; 00103 flags|=FLAG_DIRTY; 00104 layout(); 00105 recalc(); 00106 update(); 00107 } 00108 00109 00110 00111 void FOX16_HtmlLabel::calcDefaultDims() { 00112 #if 0 00113 int w; 00114 int wNeeded; 00115 00116 m_htmlCtx->layout(-1, -1); 00117 wNeeded=m_htmlCtx->getWidth(); 00118 w=wNeeded; 00119 if (w>m_maxDefaultWidth) 00120 w=m_maxDefaultWidth; 00121 if (w<width) 00122 w=width; 00123 if (w<wNeeded) { 00124 m_htmlCtx->layout(w-border*2, -1); 00125 } 00126 m_defaultWidth=m_htmlCtx->getWidth(); 00127 m_defaultHeight=m_htmlCtx->getHeight(); 00128 m_haveDefaultDims=true; 00129 #else 00130 int w; 00131 00132 if (options & FLAGS_NO_WORDWRAP) 00133 w=-1; 00134 else if (options & FLAGS_USE_FULL_WIDTH) 00135 w=width; 00136 else 00137 w=m_maxDefaultWidth; 00138 m_htmlCtx->layout(w-border*2, -1); 00139 m_defaultWidth=m_htmlCtx->getWidth(); 00140 m_defaultHeight=m_htmlCtx->getHeight(); 00141 m_haveDefaultDims=true; 00142 #endif 00143 } 00144 00145 00146 00147 FXint FOX16_HtmlLabel::getDefaultWidth() { 00148 int w; 00149 00150 if (m_htmlCtx==NULL) 00151 updateHtml(); 00152 if (!m_haveDefaultDims) 00153 calcDefaultDims(); 00154 00155 w=m_defaultWidth; 00156 if (m_icon) 00157 w+=m_icon->getWidth()+ICON_SPACE; 00158 return w; 00159 } 00160 00161 00162 00163 FXint FOX16_HtmlLabel::getDefaultHeight() { 00164 int h; 00165 00166 if (m_htmlCtx==NULL) 00167 updateHtml(); 00168 if (!m_haveDefaultDims) 00169 calcDefaultDims(); 00170 h=m_defaultHeight; 00171 if (m_icon) { 00172 int ih; 00173 00174 ih=m_icon->getHeight(); 00175 if (ih>h) 00176 h=ih; 00177 } 00178 00179 return h; 00180 } 00181 00182 00183 00184 long FOX16_HtmlLabel::onPaint(FXObject*, FXSelector, void *ptr) { 00185 FXEvent *ev=(FXEvent*)ptr; 00186 FXDCWindow dc(this, ev); 00187 00188 dc.setForeground(backColor); 00189 dc.fillRectangle(border, border, width-(border*2), height-(border*2)); 00190 00191 if (m_htmlCtx) { 00192 if (m_icon) { 00193 int th; 00194 int ih; 00195 int ty=border; 00196 00197 if(isEnabled()) 00198 dc.drawIcon(m_icon, border, border); 00199 else 00200 dc.drawIconSunken(m_icon, border, border); 00201 00202 ih=m_icon->getHeight(); 00203 th=m_htmlCtx->getHeight(); 00204 if (ih>th) 00205 ty+=(ih-th)/2; 00206 m_htmlCtx->paint(&dc, border+ICON_SPACE+m_icon->getWidth(), ty); 00207 } 00208 else { 00209 m_htmlCtx->paint(&dc, border, border); 00210 } 00211 } 00212 else { 00213 DBG_ERROR(GWEN_LOGDOMAIN, "No HtmlContext"); 00214 } 00215 00216 drawFrame(dc, 0, 0, width, height); 00217 return 1; 00218 } 00219 00220 00221 00222 void FOX16_HtmlLabel::create() { 00223 FXFrame::create(); 00224 if (m_icon) 00225 m_icon->create(); 00226 updateHtml(); 00227 recalc(); 00228 } 00229 00230 00231 00232 void FOX16_HtmlLabel::layout() { 00233 #if 0 00234 int w; 00235 00236 m_haveDefaultDims=false; 00237 if (options & FLAGS_NO_WORDWRAP) 00238 w=-1; 00239 else 00240 w=width; 00241 00242 if (m_htmlCtx==NULL) 00243 updateHtml(); 00244 m_htmlCtx->layout(w-border*2, height-border*2); 00245 update(); 00246 flags&=~FLAG_DIRTY; 00247 #else 00248 int w; 00249 00250 m_haveDefaultDims=false; 00251 if (options & FLAGS_NO_WORDWRAP) 00252 w=-1; 00253 else if (options & FLAGS_USE_FULL_WIDTH) { 00254 w=width; 00255 if (m_icon) 00256 w-=(m_icon->getWidth()+ICON_SPACE); 00257 } 00258 else 00259 w=m_maxDefaultWidth; 00260 00261 if (m_htmlCtx==NULL) 00262 updateHtml(); 00263 m_htmlCtx->layout(w-border*2, height-border*2); 00264 update(); 00265 flags&=~FLAG_DIRTY; 00266 #endif 00267 } 00268 00269 00270 00271 void FOX16_HtmlLabel::updateHtml() { 00272 GWEN_STRINGLISTENTRY *se; 00273 00274 if (m_htmlCtx) 00275 delete m_htmlCtx; 00276 m_haveDefaultDims=false; 00277 m_htmlCtx=new FOX16_HtmlCtx(0); 00278 /* copy media paths to context */ 00279 se=GWEN_StringList_FirstEntry(m_mediaPaths); 00280 while(se) { 00281 const char *s; 00282 00283 s=GWEN_StringListEntry_Data(se); 00284 assert(s); 00285 m_htmlCtx->addMediaPath(s); 00286 se=GWEN_StringListEntry_Next(se); 00287 } 00288 00289 m_htmlCtx->setBackgroundColor(backColor); 00290 m_htmlCtx->setForegroundColor(fxcolorfromname("black")); 00291 m_htmlCtx->setText(m_text.text()); 00292 flags|=FLAG_DIRTY; 00293 } 00294 00295 00296 00297 00298 00299 00300