]> git.saurik.com Git - wxWidgets.git/blob - src/html/mod_image.cpp
call HasMask() before writing PNG files, as was done in WX_2_0_FIXES
[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
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 wxInputStream *s = input -> GetStream();
295
296 img = new wxImage(*s, wxBITMAP_TYPE_ANY);
297
298 m_Image = NULL;
299 if (img && (img -> Ok())) {
300 ww = img -> GetWidth();
301 hh = img -> GetHeight();
302 if (w != -1) m_Width = w; else m_Width = ww;
303 if (h != -1) m_Height = h; else m_Height = hh;
304 if ((m_Width != ww) || (m_Height != hh)) {
305 wxImage img2 = img -> Scale(m_Width, m_Height);
306 m_Image = new wxBitmap(img2.ConvertToBitmap());
307 }
308 else
309 m_Image = new wxBitmap(img -> ConvertToBitmap());
310 delete img;
311 }
312 switch (align) {
313 case HTML_ALIGN_TOP :
314 m_Descent = m_Height; break;
315 case HTML_ALIGN_CENTER :
316 m_Descent = m_Height / 2; break;
317 case HTML_ALIGN_BOTTOM : default :
318 m_Descent = 0; break;
319 }
320
321 m_ImageMap = NULL;
322 m_MapName = mapname;
323 }
324
325
326
327 void wxHtmlImageCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
328 {
329 if (m_Image)
330 dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, TRUE);
331 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
332 }
333
334 wxString wxHtmlImageCell::GetLink( int x, int y ) const
335 {
336 if (m_MapName.IsEmpty())
337 return wxHtmlCell::GetLink( x, y );
338 if (!m_ImageMap)
339 {
340 wxHtmlContainerCell *p, *op;
341 op = p = GetParent();
342 while (p)
343 {
344 op = p;
345 p = p->GetParent();
346 }
347 p = op;
348 wxHtmlCell *cell = (wxHtmlCell*)p->Find( HTML_COND_ISIMAGEMAP, (const void*)(&m_MapName));
349 if (!cell)
350 {
351 ((wxString&)m_MapName).Clear();
352 return wxHtmlCell::GetLink( x, y );
353 }
354 { // dirty hack, ask Joel why he fills m_ImageMap in this place
355 // THE problem is that we're in const method and we can't modify m_ImageMap
356 wxHtmlImageMapCell **cx = (wxHtmlImageMapCell**)(&m_ImageMap);
357 *cx = (wxHtmlImageMapCell*)cell;
358 }
359 }
360 return m_ImageMap->GetLink( x, y );
361 }
362
363
364
365 //--------------------------------------------------------------------------------
366 // tag handler
367 //--------------------------------------------------------------------------------
368
369 TAG_HANDLER_BEGIN(IMG, "IMG, MAP, AREA")
370
371 TAG_HANDLER_PROC(tag)
372 {
373 if (tag.GetName() == "IMG")
374 {
375 if (tag.HasParam("SRC")) {
376 int w = -1, h = -1;
377 int al;
378 wxFSFile *str;
379 wxString tmp = tag.GetParam("SRC");
380 wxString mn = wxEmptyString;
381
382 str = m_WParser -> GetFS() -> OpenFile(tmp);
383 if (tag.HasParam("WIDTH")) tag.ScanParam("WIDTH", "%i", &w);
384 if (tag.HasParam("HEIGHT")) tag.ScanParam("HEIGHT", "%i", &h);
385 al = HTML_ALIGN_BOTTOM;
386 if (tag.HasParam("ALIGN")) {
387 wxString alstr = tag.GetParam("ALIGN");
388 alstr.MakeUpper(); // for the case alignment was in ".."
389 if (alstr == "TEXTTOP") al = HTML_ALIGN_TOP;
390 else if ((alstr == "CENTER") || (alstr == "ABSCENTER")) al = HTML_ALIGN_CENTER;
391 }
392 if (tag.HasParam("USEMAP"))
393 {
394 mn = tag.GetParam( "USEMAP" );
395 if (mn[ 0 ] == '#')
396 {
397 mn = mn.Mid( 1 );
398 }
399 }
400 wxHtmlImageCell *cel = NULL;
401 if (str) {
402 cel = new wxHtmlImageCell(str, w, h, al, mn);
403 cel -> SetLink(m_WParser -> GetLink());
404 m_WParser -> GetContainer() -> InsertCell(cel);
405 delete str;
406 }
407 }
408 }
409 if (tag.GetName() == "MAP")
410 {
411 m_WParser->CloseContainer();
412 m_WParser->OpenContainer();
413 if (tag.HasParam("NAME"))
414 {
415 wxString tmp = tag.GetParam("NAME");
416 wxHtmlImageMapCell *cel = new wxHtmlImageMapCell( tmp );
417 m_WParser->GetContainer()->InsertCell( cel );
418 }
419 ParseInner( tag );
420 m_WParser->CloseContainer();
421 m_WParser->OpenContainer();
422 }
423 if (tag.GetName() == "AREA")
424 {
425 if (tag.HasParam("SHAPE"))
426 {
427 wxString tmp = tag.GetParam("SHAPE");
428 wxString coords;
429 tmp.MakeUpper();
430 wxHtmlImageMapAreaCell *cel = NULL;
431 if (tag.HasParam("COORDS"))
432 {
433 coords = tag.GetParam("COORDS");
434 }
435 if (tmp == "POLY")
436 {
437 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY, coords );
438 }
439 else if (tmp == "CIRCLE")
440 {
441 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE, coords );
442 }
443 else if (tmp == "RECT")
444 {
445 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT, coords );
446 }
447 if (cel && tag.HasParam("HREF"))
448 {
449 wxString tmp = tag.GetParam("HREF");
450 cel->SetLink( tmp );
451 }
452 m_WParser->GetContainer()->InsertCell( cel );
453 }
454 }
455
456 return FALSE;
457 }
458
459 TAG_HANDLER_END(IMAGE)
460
461
462
463 TAGS_MODULE_BEGIN(Image)
464
465 TAGS_MODULE_ADD(IMG)
466
467 TAGS_MODULE_END(Image)
468
469
470 #endif