gwenhywfar  4.8.0beta
fox16_htmllabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Tue Feb 23 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_htmllabel.hpp"
15 #include "fox16_htmlctx.hpp"
16 
17 #include <gwenhywfar/debug.h>
18 #include <gwenhywfar/i18n.h>
19 #include <gwenhywfar/stringlist.h>
20 
21 #include <assert.h>
22 
23 
24 #define MAX_DEFAULT_WIDTH 400
25 #define ICON_SPACE 4
26 
27 
28 
29 FXDEFMAP(FOX16_HtmlLabel) FOX16_HtmlLabelMap[]={
30  FXMAPFUNC(SEL_PAINT,0,FOX16_HtmlLabel::onPaint),
31 };
32 
33 
34 
35 // Object implementation
36 FXIMPLEMENT(FOX16_HtmlLabel, FXFrame, FOX16_HtmlLabelMap, ARRAYNUMBER(FOX16_HtmlLabelMap))
37 
38 
39 
40 
41 FOX16_HtmlLabel::FOX16_HtmlLabel(FXComposite* p, const FXString& text,
42  FXuint opts,
43  FXint x, FXint y, FXint w, FXint h,
44  FXint pl, FXint pr,
45  FXint pt, FXint pb)
46 :FXFrame(p, opts, x, y, w, h, pl, pr, pt, pb)
47 ,m_htmlCtx(NULL)
48 ,m_minWidth(0)
49 ,m_maxDefaultWidth(-1)
50 ,m_haveDefaultDims(false)
51 ,m_mediaPaths(NULL)
52 ,m_icon(NULL)
53 {
54  m_mediaPaths=GWEN_StringList_new();
55  setText(text);
56  flags|=FLAG_ENABLED|FLAG_DIRTY|FLAG_RECALC;
57 
58 }
59 
60 
61 
63 :FXFrame()
64 ,m_htmlCtx(NULL)
65 ,m_minWidth(0)
66 ,m_mediaPaths(NULL)
67 ,m_icon(NULL)
68 {
69  flags|=FLAG_ENABLED;
70 }
71 
72 
73 
75  if (m_htmlCtx)
76  delete m_htmlCtx;
78 }
79 
80 
81 
82 void FOX16_HtmlLabel::setText(const FXString& text) {
83  m_haveDefaultDims=false;
84  m_text=text;
85  updateHtml();
86  flags|=FLAG_DIRTY;
87  layout();
88  recalc();
89  update();
90 }
91 
92 
93 
94 void FOX16_HtmlLabel::addMediaPath(const char *s) {
95  assert(s);
97 }
98 
99 
100 
101 void FOX16_HtmlLabel::setIcon(FXIcon *ic) {
102  m_icon=ic;
103  flags|=FLAG_DIRTY;
104  layout();
105  recalc();
106  update();
107 }
108 
109 
110 
112 #if 0
113  int w;
114 
115  if (options & FLAGS_NO_WORDWRAP)
116  w=-1;
117  else if (options & FLAGS_USE_FULL_WIDTH)
118  w=width;
119  else
121  m_htmlCtx->layout(w-border*2, -1);
124  m_haveDefaultDims=true;
125 #else
126  int w;
127 
128  if (options & FLAGS_NO_WORDWRAP)
129  w=-1;
130  else if (options & FLAGS_USE_FULL_WIDTH)
131  w=width;
132  else if (m_maxDefaultWidth!=-1)
134  else {
135  int wTmp=1024;
136  int mask=1024;
137  int i;
138 
139  for (i=0; i<10; i++) {
140  double ar, nw, nh;
141 
142  m_htmlCtx->layout(wTmp-border*2, -1);
143  nw=(double) (m_htmlCtx->getWidth());
144  nh=(double)(m_htmlCtx->getHeight());
145  ar=nw/nh;
146 
147  if (ar>=3.5 && ar<4.0)
148  break;
149 
150  if (ar>4.0)
151  /* w/h too high, so we need to reduce the width */
152  wTmp&=~mask;
153 
154  mask>>=1;
155  wTmp|=mask;
156  }
157  w=wTmp;
158  }
159 
160  /* TODO: handle icon size correctly */
161 
162  m_htmlCtx->layout(w-border*2, -1);
165  m_haveDefaultDims=true;
166  //DBG_ERROR(0, "Labelsize: %d / %d", m_defaultWidth, m_defaultHeight);
167 #endif
168 }
169 
170 
171 
173  int w;
174 
175  if (m_htmlCtx==NULL)
176  updateHtml();
177  if (!m_haveDefaultDims)
178  calcDefaultDims();
179 
180  w=m_defaultWidth;
181  if (m_icon)
182  w+=m_icon->getWidth()+ICON_SPACE;
183 
184  //DBG_ERROR(0, "GetDefaultWidth[%s]: Width= %d", m_text.text(), w);
185 
186  return w;
187 }
188 
189 
190 
192  int h;
193 
194  if (m_htmlCtx==NULL)
195  updateHtml();
196  if (!m_haveDefaultDims)
197  calcDefaultDims();
198  h=m_defaultHeight;
199  if (m_icon) {
200  int ih;
201 
202  ih=m_icon->getHeight();
203  if (ih>h)
204  h=ih;
205  }
206 
207  //DBG_ERROR(0, "GetDefaultHeight[%s]: Height= %d", m_text.text(), h);
208 
209  return h;
210 }
211 
212 
213 
214 long FOX16_HtmlLabel::onPaint(FXObject*, FXSelector, void *ptr) {
215  FXEvent *ev=(FXEvent*)ptr;
216  FXDCWindow dc(this, ev);
217 
218  dc.setForeground(backColor);
219  dc.fillRectangle(border, border, width-(border*2), height-(border*2));
220 
221  if (m_htmlCtx) {
222  //DBG_ERROR(0, "Paint: Labelsize= %d / %d", m_htmlCtx->getWidth(), m_htmlCtx->getHeight());
223  if (m_icon) {
224  int th;
225  int ih;
226  int ty=border;
227 
228  if(isEnabled())
229  dc.drawIcon(m_icon, border, border);
230  else
231  dc.drawIconSunken(m_icon, border, border);
232 
233  ih=m_icon->getHeight();
234  th=m_htmlCtx->getHeight();
235  if (ih>th)
236  ty+=(ih-th)/2;
237  m_htmlCtx->paint(&dc, border+ICON_SPACE+m_icon->getWidth(), ty);
238  }
239  else {
240  m_htmlCtx->paint(&dc, border, border);
241  }
242  }
243  else {
244  DBG_ERROR(GWEN_LOGDOMAIN, "No HtmlContext");
245  }
246 
247  drawFrame(dc, 0, 0, width, height);
248  return 1;
249 }
250 
251 
252 
254  FXFrame::create();
255  if (m_icon)
256  m_icon->create();
257  updateHtml();
258  recalc();
259 }
260 
261 
262 
264 #if 0
265  int w;
266 
267  m_haveDefaultDims=false;
268  if (options & FLAGS_NO_WORDWRAP)
269  w=-1;
270  else if (options & FLAGS_USE_FULL_WIDTH) {
271  w=width;
272  if (m_icon)
273  w-=(m_icon->getWidth()+ICON_SPACE);
274  }
275  else
277 
278  if (m_htmlCtx==NULL)
279  updateHtml();
280  m_htmlCtx->layout(w-border*2, height-border*2);
281  update();
282  flags&=~FLAG_DIRTY;
283 #else
284  int w;
285 
286  //DBG_ERROR(0, "Layout[%s]: Width=%d, height=%d", m_text.text(), width, height);
287 
288  if (m_htmlCtx==NULL)
289  updateHtml();
290 
291  if (!m_haveDefaultDims)
292  calcDefaultDims();
293 
294  if (options & FLAGS_NO_WORDWRAP)
295  w=-1;
296  else if (options & FLAGS_USE_FULL_WIDTH) {
297  w=width;
298  if (m_icon)
299  w-=(m_icon->getWidth()+ICON_SPACE);
300  }
301  else
302  w=m_defaultWidth;
303 
304  m_htmlCtx->layout(w-border*2, height-border*2);
305  update();
306  flags&=~FLAG_DIRTY;
307 
308 #endif
309 }
310 
311 
312 
315 
316  if (m_htmlCtx)
317  delete m_htmlCtx;
318  m_haveDefaultDims=false;
319  m_htmlCtx=new FOX16_HtmlCtx(0);
320  /* copy media paths to context */
322  while(se) {
323  const char *s;
324 
326  assert(s);
329  }
330 
331  m_htmlCtx->setBackgroundColor(backColor);
332  m_htmlCtx->setForegroundColor(fxcolorfromname("black"));
333  m_htmlCtx->setText(m_text.text());
334  flags|=FLAG_DIRTY;
335 }
336 
337 
338 
339 
340 
341 
342