]> git.saurik.com Git - wxWidgets.git/blob - src/html/mod_image.cpp
imagemap support (html)
[wxWidgets.git] / src / html / mod_image.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mod_image.cpp
3 // Purpose: wxHtml module for displaying images
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik, Joel Lucsy
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifdef __GNUG__
10 #pragma implementation
11 #endif
12
13 #include "wx/wxprec.h"
14
15 #include "wx/defs.h"
16 #if wxUSE_HTML
17
18 #ifdef __BORDLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #include "wx/html/forcelink.h"
27 #include "wx/html/mod_templ.h"
28
29 #include "wx/wxhtml.h"
30 #include "wx/image.h"
31 #include "wx/dynarray.h"
32
33 #include <math.h>
34 #include <float.h>
35
36 FORCE_LINK_ME(mod_image)
37
38
39
40
41 WX_DECLARE_OBJARRAY(int, CoordArray);
42 #include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
43 WX_DEFINE_OBJARRAY(CoordArray);
44
45
46 //--------------------------------------------------------------------------------
47 // wxHtmlImageMapAreaCell
48 // 0-width, 0-height cell that represents single area in imagemap
49 // (it's GetLink is called from wxHtmlImageCell's)
50 //--------------------------------------------------------------------------------
51
52 class wxHtmlImageMapAreaCell : public wxHtmlCell
53 {
54 public:
55 enum celltype{ CIRCLE, RECT, POLY };
56 protected:
57 CoordArray coords;
58 celltype type;
59 int radius;
60 public:
61 wxHtmlImageMapAreaCell( celltype t, wxString &coords );
62 virtual wxString GetLink( int x = 0, int y = 0 ) const;
63 };
64
65
66
67
68
69 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t, wxString &incoords )
70 {
71 int i;
72 wxString x = incoords, y;
73
74 type = t;
75 while ((i = x.Find( ',' )) != -1)
76 {
77 coords.Add( atoi( x.Left( i ).c_str() ) );
78 x = x.Mid( i + 1 );
79 }
80 coords.Add( atoi( x.c_str() ) );
81 }
82
83 wxString wxHtmlImageMapAreaCell::GetLink( int x, int y ) const
84 {
85 switch (type)
86 {
87 case RECT:
88 {
89 int l, t, r, b;
90
91 l = coords[ 0 ];
92 t = coords[ 1 ];
93 r = coords[ 2 ];
94 b = coords[ 3 ];
95 if (x >= l && x <= r && y >= t && y <= b)
96 {
97 return m_Link;
98 }
99 break;
100 }
101 case CIRCLE:
102 {
103 int l, t, r;
104 double d;
105
106 l = coords[ 0 ];
107 t = coords[ 1 ];
108 r = coords[ 2 ];
109 d = sqrt( ((x - l) * (x - l)) + ((y - t) * (y - t)) );
110 if (d < (double)r)
111 {
112 return m_Link;
113 }
114 }
115 break;
116 case POLY:
117 {
118 if (coords.GetCount() >= 6)
119 {
120 int intersects = 0;
121 int wherex = x;
122 int wherey = y;
123 int totalv = coords.GetCount() / 2;
124 int totalc = totalv * 2;
125 int xval = coords[totalc - 2];
126 int yval = coords[totalc - 1];
127 int end = totalc;
128 int pointer = 1;
129
130 if ((yval >= wherey) != (coords[pointer] >= wherey))
131 {
132 if ((xval >= wherex) == (coords[0] >= wherex))
133 {
134 intersects += (xval >= wherex) ? 1 : 0;
135 }
136 else
137 {
138 intersects += ((xval - (yval - wherey) *
139 (coords[0] - xval) /
140 (coords[pointer] - yval)) >= wherex) ? 1 : 0;
141 }
142 }
143
144 while (pointer < end)
145 {
146 yval = coords[pointer];
147 pointer += 2;
148 if (yval >= wherey)
149 {
150 while((pointer < end) && (coords[pointer] >= wherey))
151 {
152 pointer+=2;
153 }
154 if (pointer >= end)
155 {
156 break;
157 }
158 if ((coords[pointer-3] >= wherex) ==
159 (coords[pointer-1] >= wherex))
160 {
161 intersects += (coords[pointer-3] >= wherex) ? 1 : 0;
162 }
163 else
164 {
165 intersects +=
166 ((coords[pointer-3] - (coords[pointer-2] - wherey) *
167 (coords[pointer-1] - coords[pointer-3]) /
168 (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1:0;
169 }
170 }
171 else
172 {
173 while((pointer < end) && (coords[pointer] < wherey))
174 {
175 pointer+=2;
176 }
177 if (pointer >= end)
178 {
179 break;
180 }
181 if ((coords[pointer-3] >= wherex) ==
182 (coords[pointer-1] >= wherex))
183 {
184 intersects += (coords[pointer-3] >= wherex) ? 1:0;
185 }
186 else
187 {
188 intersects +=
189 ((coords[pointer-3] - (coords[pointer-2] - wherey) *
190 (coords[pointer-1] - coords[pointer-3]) /
191 (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1:0;
192 }
193 }
194 }
195 if ((intersects & 1) != 0)
196 {
197 return m_Link;
198 }
199 }
200 }
201 break;
202 }
203 if (m_Next)
204 {
205 wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next;
206 return a->GetLink( x, y );
207 }
208 return wxEmptyString;
209 }
210
211
212
213
214
215
216
217
218 //--------------------------------------------------------------------------------
219 // wxHtmlImageMapCell
220 // 0-width, 0-height cell that represents map from imagemaps
221 // it is always placed before wxHtmlImageMapAreaCells
222 // It responds to Find(HTML_COND_ISIMAGEMAP)
223 //--------------------------------------------------------------------------------
224
225
226 class wxHtmlImageMapCell : public wxHtmlCell
227 {
228 public:
229 wxHtmlImageMapCell( wxString &name );
230 protected:
231 wxString m_Name;
232 public:
233 virtual wxString GetLink( int x = 0, int y = 0 ) const;
234 virtual const wxHtmlCell *Find( int cond, const void *param ) const;
235 };
236
237
238 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString &name )
239 {
240 m_Name = name ;
241 }
242
243 wxString wxHtmlImageMapCell::GetLink( int x, int y ) const
244 {
245 wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next;
246 if (a)
247 return a->GetLink( x, y );
248 return wxHtmlCell::GetLink( x, y );
249 }
250
251 const wxHtmlCell *wxHtmlImageMapCell::Find( int cond, const void *param ) const
252 {
253 if (cond == HTML_COND_ISIMAGEMAP)
254 {
255 if (m_Name == *((wxString*)(param)))
256 return this;
257 }
258 return wxHtmlCell::Find(cond, param);
259 }
260
261
262
263
264
265 //--------------------------------------------------------------------------------
266 // wxHtmlImageCell
267 // Image/bitmap
268 //--------------------------------------------------------------------------------
269
270 class wxHtmlImageCell : public wxHtmlCell
271 {
272 public:
273 wxBitmap *m_Image;
274 wxHtmlImageMapCell *m_ImageMap;
275 wxString m_MapName;
276
277 wxHtmlImageCell(wxFSFile *input, int w = -1, int h = -1, int align = HTML_ALIGN_BOTTOM, wxString mapname = wxEmptyString);
278 ~wxHtmlImageCell() {if (m_Image) delete m_Image;}
279 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2);
280 virtual wxString GetLink( int x = 0, int y = 0 ) const;
281 };
282
283
284
285
286 //--------------------------------------------------------------------------------
287 // wxHtmlImageCell
288 //--------------------------------------------------------------------------------
289
290 wxHtmlImageCell::wxHtmlImageCell(wxFSFile *input, int w, int h, int align, wxString mapname) : wxHtmlCell()
291 {
292 wxImage *img;
293 int ww, hh;
294 wxString m = input -> GetMimeType();
295 wxInputStream *s = input -> GetStream();
296
297 img = new wxImage(*s, m);
298
299 m_Image = NULL;
300 if (img && (img -> Ok())) {
301 ww = img -> GetWidth();
302 hh = img -> GetHeight();
303 if (w != -1) m_Width = w; else m_Width = ww;
304 if (h != -1) m_Height = h; else m_Height = hh;
305 if ((m_Width != ww) || (m_Height != hh)) {
306 wxImage img2 = img -> Scale(m_Width, m_Height);
307 m_Image = new wxBitmap(img2.ConvertToBitmap());
308 }
309 else
310 m_Image = new wxBitmap(img -> ConvertToBitmap());
311 delete img;
312 }
313 switch (align) {
314 case HTML_ALIGN_TOP :
315 m_Descent = m_Height; break;
316 case HTML_ALIGN_CENTER :
317 m_Descent = m_Height / 2; break;
318 case HTML_ALIGN_BOTTOM : default :
319 m_Descent = 0; break;
320 }
321
322 m_ImageMap = NULL;
323 m_MapName = mapname;
324 }
325
326
327
328 void wxHtmlImageCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
329 {
330 if (m_Image)
331 dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, TRUE);
332 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
333 }
334
335 wxString wxHtmlImageCell::GetLink( int x, int y ) const
336 {
337 if (m_MapName.IsEmpty())
338 return wxHtmlCell::GetLink( x, y );
339 if (!m_ImageMap)
340 {
341 wxHtmlContainerCell *p, *op;
342 op = p = GetParent();
343 while (p)
344 {
345 op = p;
346 p = p->GetParent();
347 }
348 p = op;
349 wxHtmlCell *cell = (wxHtmlCell*)p->Find( HTML_COND_ISIMAGEMAP, (const void*)(&m_MapName));
350 if (!cell)
351 {
352 ((wxString&)m_MapName).Clear();
353 return wxHtmlCell::GetLink( x, y );
354 }
355 { // dirty hack, ask Joel why he fills m_ImageMap in this place
356 // THE problem is that we're in const method and we can't modify m_ImageMap
357 wxHtmlImageMapCell **cx = (wxHtmlImageMapCell**)(&m_ImageMap);
358 *cx = (wxHtmlImageMapCell*)cell;
359 }
360 }
361 return m_ImageMap->GetLink( x, y );
362 }
363
364
365
366 //--------------------------------------------------------------------------------
367 // tag handler
368 //--------------------------------------------------------------------------------
369
370 TAG_HANDLER_BEGIN(IMG, "IMG, MAP, AREA")
371
372 TAG_HANDLER_PROC(tag)
373 {
374 if (tag.GetName() == "IMG")
375 {
376 if (tag.HasParam("SRC")) {
377 int w = -1, h = -1;
378 int al;
379 wxFSFile *str;
380 wxString tmp = tag.GetParam("SRC");
381 wxString mn = wxEmptyString;
382
383 str = m_WParser -> GetFS() -> OpenFile(tmp);
384 if (tag.HasParam("WIDTH")) tag.ScanParam("WIDTH", "%i", &w);
385 if (tag.HasParam("HEIGHT")) tag.ScanParam("HEIGHT", "%i", &h);
386 al = HTML_ALIGN_BOTTOM;
387 if (tag.HasParam("ALIGN")) {
388 wxString alstr = tag.GetParam("ALIGN");
389 alstr.MakeUpper(); // for the case alignment was in ".."
390 if (alstr == "TEXTTOP") al = HTML_ALIGN_TOP;
391 else if ((alstr == "CENTER") || (alstr == "ABSCENTER")) al = HTML_ALIGN_CENTER;
392 }
393 if (tag.HasParam("USEMAP"))
394 {
395 mn = tag.GetParam( "USEMAP" );
396 if (mn[ 0 ] == '#')
397 {
398 mn = mn.Mid( 1 );
399 }
400 }
401 wxHtmlImageCell *cel = NULL;
402 if (str) {
403 cel = new wxHtmlImageCell(str, w, h, al, mn);
404 cel -> SetLink(m_WParser -> GetLink());
405 m_WParser -> GetContainer() -> InsertCell(cel);
406 delete str;
407 }
408 }
409 }
410 if (tag.GetName() == "MAP")
411 {
412 m_WParser->CloseContainer();
413 m_WParser->OpenContainer();
414 if (tag.HasParam("NAME"))
415 {
416 wxString tmp = tag.GetParam("NAME");
417 wxHtmlImageMapCell *cel = new wxHtmlImageMapCell( tmp );
418 m_WParser->GetContainer()->InsertCell( cel );
419 }
420 ParseInner( tag );
421 m_WParser->CloseContainer();
422 m_WParser->OpenContainer();
423 }
424 if (tag.GetName() == "AREA")
425 {
426 if (tag.HasParam("SHAPE"))
427 {
428 wxString tmp = tag.GetParam("SHAPE");
429 wxString coords;
430 tmp.MakeUpper();
431 wxHtmlImageMapAreaCell *cel = NULL;
432 if (tag.HasParam("COORDS"))
433 {
434 coords = tag.GetParam("COORDS");
435 }
436 if (tmp == "POLY")
437 {
438 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY, coords );
439 }
440 else if (tmp == "CIRCLE")
441 {
442 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE, coords );
443 }
444 else if (tmp == "RECT")
445 {
446 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT, coords );
447 }
448 if (cel && tag.HasParam("HREF"))
449 {
450 wxString tmp = tag.GetParam("HREF");
451 cel->SetLink( tmp );
452 }
453 m_WParser->GetContainer()->InsertCell( cel );
454 }
455 }
456
457 return FALSE;
458 }
459
460 TAG_HANDLER_END(IMAGE)
461
462
463
464 TAGS_MODULE_BEGIN(Image)
465
466 TAGS_MODULE_ADD(IMG)
467
468 TAGS_MODULE_END(Image)
469
470
471 #endif