Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGHtmlImage.cxx
Go to the documentation of this file.
1// $Id: TGHtmlImage.cxx,v 1.2 2007/05/07 15:28:48 brun Exp $
2// Author: Valeriy Onuchin 03/05/2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2001, Rene Brun, Fons Rademakers and Reiner Rohlfs *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**************************************************************************
13
14 HTML widget for xclass. Based on tkhtml 1.28
15 Copyright (C) 1997-2000 D. Richard Hipp <drh@acm.org>
16 Copyright (C) 2002-2003 Hector Peraza.
17
18 This library is free software; you can redistribute it and/or
19 modify it under the terms of the GNU Library General Public
20 License as published by the Free Software Foundation; either
21 version 2 of the License, or (at your option) any later version.
22
23 This library is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Library General Public License for more details.
27
28 You should have received a copy of the GNU Library General Public
29 License along with this library; if not, write to the Free
30 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
32**************************************************************************/
33
34// Routines used for processing <IMG> markup
35
36#include <cstring>
37#include <cstdlib>
38
39#include "TGHtml.h"
40#include "snprintf.h"
41#include "TImage.h"
42#include "TUrl.h"
43#include "TSocket.h"
44#include "TSystem.h"
45#include "TError.h"
46
47////////////////////////////////////////////////////////////////////////////////
48/// ctor.
49
50TGHtmlImage::TGHtmlImage(TGHtml *htm, const char *url, const char *width,
51 const char *height)
52{
53 fHtml = htm;
54 fZUrl = StrDup(url);
57 fImage = NULL;
58 fPNext = NULL;
59 fPList = NULL;
60 fW = 0;
61 fH = 0;
62 fTimer = NULL;
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// dtor.
67
69{
70 delete [] fZUrl;
71 delete [] fZWidth;
72 delete [] fZHeight;
73
74 if (fImage) delete fImage;
75 if (fTimer) delete fTimer;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Find the alignment for an image
80
82{
83 const char *z;
84 int i;
85 int result;
86
87 static struct {
88 const char *zName;
89 int iValue;
90 } aligns[] = {
91 { "bottom", IMAGE_ALIGN_Bottom },
92 { "baseline", IMAGE_ALIGN_Bottom },
93 { "middle", IMAGE_ALIGN_Middle },
94 { "top", IMAGE_ALIGN_Top },
95 { "absbottom", IMAGE_ALIGN_AbsBottom },
96 { "absmiddle", IMAGE_ALIGN_AbsMiddle },
97 { "texttop", IMAGE_ALIGN_TextTop },
98 { "left", IMAGE_ALIGN_Left },
99 { "right", IMAGE_ALIGN_Right },
100 };
101
102 z = p->MarkupArg("align", 0);
104 if (z) {
105 for (i = 0; i < int(sizeof(aligns) / sizeof(aligns[0])); i++) {
106 if (strcasecmp(aligns[i].zName, z) == 0) {
107 result = aligns[i].iValue;
108 break;
109 }
110 }
111 }
112 return result;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// This routine is called when an image changes. If the size of the
117/// images changes, then we need to completely redo the layout. If
118/// only the appearance changes, then this works like an expose event.
119///
120/// pImage - Pointer to an TGHtmlImage object
121/// newWidth - New width of the image
122/// newHeight - New height of the image
123
125{
127
128 if (pImage->fW != newWidth || pImage->fH != newHeight) {
129 // We have to completely redo the layout after adjusting the size
130 // of the images
131 for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
132 pElem->fW = newWidth;
133 pElem->fH = newHeight;
134 }
135 fFlags |= RELAYOUT;
136 pImage->fW = newWidth;
137 pImage->fH = newHeight;
139 } else {
140#if 0
141 for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
142 pElem->fRedrawNeeded = 1;
143 }
146#else
147 for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
148 pElem->fRedrawNeeded = 1;
149 DrawRegion(pElem->fX, pElem->fY - pElem->fAscent, pElem->fW, pElem->fH);
150 }
151#endif
152 }
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// Given an `<IMG>` markup, find or create an appropriate TGHtmlImage
157/// object and return a pointer to that object. `NULL` might be returned.
158
160{
161 const char *zWidth;
162 const char *zHeight;
163 const char *zSrc;
165
166 if (p->fType != Html_IMG) { CANT_HAPPEN; return 0; }
167
168 zSrc = p->MarkupArg("src", 0);
169 if (zSrc == 0) return 0;
170
172 if (zSrc == 0) return 0;
173
174 zWidth = p->MarkupArg("width", "");
175 zHeight = p->MarkupArg("height", "");
176
177 //p->w = atoi(fZWidth);
178 //p->h = atoi(zHeight);
179
180 for (pImage = fImageList; pImage; pImage = pImage->fPNext) {
181 if (strcmp(pImage->fZUrl, zSrc) == 0
182 && strcmp(pImage->fZWidth, zWidth) == 0
183 && strcmp(pImage->fZHeight, zHeight) == 0) {
184 delete [] zSrc;
185 return pImage;
186 }
187 }
188
189 TImage *img = LoadImage(zSrc, atoi(zWidth), atoi(zHeight));
190
191 if (img) {
192 pImage = new TGHtmlImage(this, zSrc, zWidth, zHeight);
193 pImage->fImage = img;
194 //if (img->IsAnimated()) {
195 // pImage->timer = new TTimer(this, img->GetAnimDelay());
196 //}
197 ImageChanged(pImage, img->GetWidth(), img->GetHeight());
198 pImage->fPNext = fImageList;
200 } else {
201 pImage = 0;
202 }
203
204 delete [] zSrc;
205
206 return pImage;
207}
208
209////////////////////////////////////////////////////////////////////////////////
210/// This is the default LoadImage() procedure. It just tries to load the
211/// image from a file in the local filesystem.
212
213TImage *TGHtml::LoadImage(const char *url, int w, int h)
214{
215 TImage *image = 0;
216
218 if (image) {
219 if (!image->IsValid()) {
220 delete image;
221 image = 0;
222 return 0;
223 }
224 if ((w > 0 && h > 0) && ((w != (int)image->GetWidth()) ||
225 (h != (int)image->GetHeight()))) {
226 image->Scale(w, h);
227 }
228 }
229 return image;
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// Return the height and width, converting to percent if required
234/// ret must be at least 16 characters long
235
236const char *TGHtml::GetPctWidth(TGHtmlElement *p, char *opt, char *ret)
237{
238 int n, m, val;
239 const char *tz, *z;
241
242 z = pElem->MarkupArg(opt, "");
243 if (!z) return z;
244 if (!strchr(z, '%')) return z;
245 // coverity[secure_coding]
246 if (!sscanf(z, "%d", &n)) return z;
247 if (n <= 0 || n > 100) return z;
248 if (opt[0] == 'h') {
249 val = fCanvas->GetHeight() * 100;
250 } else {
251 val = fCanvas->GetWidth() * 100;
252 }
253 if (!fInTd) {
254 snprintf(ret, 15, "%d", val / n);
255 } else {
256 while (pElem && pElem->fType != Html_TD) pElem = pElem->fPPrev;
257 if (!pElem) return z;
258 tz = pElem->MarkupArg(opt, 0);
259 // coverity[secure_coding]
260 if (tz && !strchr(tz, '%') && sscanf(tz, "%d", &m)) {
261 snprintf(ret, 15, "%d", m * 100 / n);
262 return ret;
263 }
264 pElem = ((TGHtmlCell *)pElem)->fPTable;
265 if (!pElem) return z;
266 tz = pElem->MarkupArg(opt, 0);
267 // coverity[secure_coding]
268 if (tz && !strchr(tz, '%') && sscanf(tz, "%d", &m)) {
269 snprintf(ret, 15, "%d", m * 100 / n);
270 return ret;
271 }
272 return z;
273 }
274 return ret;
275}
276
277////////////////////////////////////////////////////////////////////////////////
278/// This routine searchs for an image beneath the coordinates x,y
279/// and returns the token number of the image, or -1 if no
280/// image found.
281
282int TGHtml::GetImageAt(int x, int y)
283{
286 //int n;
287
288 for (pBlock = fFirstBlock; pBlock; pBlock = pBlock->fBNext) {
289 if (pBlock->fTop > y || pBlock->fBottom < y ||
290 pBlock->fLeft > x || pBlock->fRight < x) {
291 continue;
292 }
293 for (pElem = pBlock->fPNext; pElem; pElem = pElem->fPNext) {
294 if (pBlock->fBNext && pElem == pBlock->fBNext->fPNext) break;
295 if (pElem->fType == Html_IMG) {
296 return TokenNumber(pElem);
297 }
298 }
299 }
300
301 return -1;
302}
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ Html_TD
@ Html_IMG
#define IMAGE_ALIGN_Top
Definition TGHtml.h:563
#define CANT_HAPPEN
Definition TGHtml.h:60
#define RELAYOUT
Definition TGHtml.h:1333
#define IMAGE_ALIGN_Bottom
Definition TGHtml.h:561
#define IMAGE_ALIGN_AbsMiddle
Definition TGHtml.h:565
#define IMAGE_ALIGN_Right
Definition TGHtml.h:568
#define REDRAW_IMAGES
Definition TGHtml.h:1340
#define IMAGE_ALIGN_Left
Definition TGHtml.h:567
#define IMAGE_ALIGN_TextTop
Definition TGHtml.h:564
#define IMAGE_ALIGN_AbsBottom
Definition TGHtml.h:566
#define IMAGE_ALIGN_Middle
Definition TGHtml.h:562
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
char * StrDup(const char *str)
Duplicate the string str.
Definition TString.cxx:2564
#define snprintf
Definition civetweb.c:1579
UInt_t GetHeight() const
Definition TGFrame.h:227
UInt_t GetWidth() const
Definition TGFrame.h:226
char * fZUrl
Definition TGHtml.h:522
Html_32_t fW
Definition TGHtml.h:520
TImage * fImage
Definition TGHtml.h:519
char * fZHeight
Definition TGHtml.h:523
char * fZWidth
Definition TGHtml.h:523
TTimer * fTimer
Definition TGHtml.h:527
TGHtmlImageMarkup * fPList
Definition TGHtml.h:525
TGHtmlImage * fPNext
Definition TGHtml.h:524
TGHtmlImage(const TGHtmlImage &)=delete
TGHtml * fHtml
Definition TGHtml.h:518
Html_32_t fH
Definition TGHtml.h:521
~TGHtmlImage() override
dtor.
The ROOT HTML widget.
Definition TGHtml.h:873
const char * GetPctWidth(TGHtmlElement *p, char *opt, char *ret)
Return the height and width, converting to percent if required ret must be at least 16 characters lon...
TGHtmlImage * fImageList
Definition TGHtml.h:1245
void ScheduleRedraw()
Make sure that a call to the Redraw() routine has been queued.
Definition TGHtml.cxx:761
int fFlags
Definition TGHtml.h:1278
void ImageChanged(TGHtmlImage *image, int newWidth, int newHeight)
This routine is called when an image changes.
virtual TImage * LoadImage(const char *uri, int w=0, int h=0)
This is the default LoadImage() procedure.
int GetImageAt(int x, int y)
This routine searchs for an image beneath the coordinates x,y and returns the token number of the ima...
int GetImageAlignment(TGHtmlElement *p)
Find the alignment for an image.
TGHtmlBlock * fFirstBlock
Definition TGHtml.h:1143
int TokenNumber(TGHtmlElement *p)
Return the token number for the given TGHtmlElement.
int fInTd
Definition TGHtml.h:1203
virtual char * ResolveUri(const char *uri)
This function resolves the specified URI and returns the result in a newly allocated string.
void RedrawEverything()
Call this routine to force the entire widget to be redrawn.
Definition TGHtml.cxx:876
void DrawRegion(Int_t x, Int_t y, UInt_t w, UInt_t h) override
Draw region defined by [x,y] [w,h].
Definition TGHtml.cxx:804
TGHtmlImage * GetImage(TGHtmlImageMarkup *p)
Given an <IMG> markup, find or create an appropriate TGHtmlImage object and return a pointer to that ...
TGViewFrame * fCanvas
frame containing the text
Definition TGView.h:42
An abstract interface to image processing library.
Definition TImage.h:29
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition TImage.cxx:117
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TMarker m
Definition textangle.C:8