]>
git.saurik.com Git - wxWidgets.git/blob - src/html/mod_image.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml module for displaying images
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik, Joel Lucsy
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation
13 #include "wx/wxprec.h"
26 #include "wx/html/forcelink.h"
27 #include "wx/html/mod_templ.h"
29 #include "wx/wxhtml.h"
31 #include "wx/dynarray.h"
36 FORCE_LINK_ME(mod_image
)
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
);
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 //--------------------------------------------------------------------------------
52 class wxHtmlImageMapAreaCell
: public wxHtmlCell
55 enum celltype
{ CIRCLE
, RECT
, POLY
};
61 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
);
62 virtual wxString
GetLink( int x
= 0, int y
= 0 ) const;
69 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
)
72 wxString x
= incoords
, y
;
75 while ((i
= x
.Find( ',' )) != -1)
77 coords
.Add( atoi( x
.Left( i
).c_str() ) );
80 coords
.Add( atoi( x
.c_str() ) );
83 wxString
wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
95 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
109 d
= sqrt( ((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
)) );
118 if (coords
.GetCount() >= 6)
123 int totalv
= coords
.GetCount() / 2;
124 int totalc
= totalv
* 2;
125 int xval
= coords
[totalc
- 2];
126 int yval
= coords
[totalc
- 1];
130 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
132 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
134 intersects
+= (xval
>= wherex
) ? 1 : 0;
138 intersects
+= ((xval
- (yval
- wherey
) *
140 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
144 while (pointer
< end
)
146 yval
= coords
[pointer
];
150 while((pointer
< end
) && (coords
[pointer
] >= wherey
))
158 if ((coords
[pointer
-3] >= wherex
) ==
159 (coords
[pointer
-1] >= wherex
))
161 intersects
+= (coords
[pointer
-3] >= wherex
) ? 1 : 0;
166 ((coords
[pointer
-3] - (coords
[pointer
-2] - wherey
) *
167 (coords
[pointer
-1] - coords
[pointer
-3]) /
168 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1:0;
173 while((pointer
< end
) && (coords
[pointer
] < wherey
))
181 if ((coords
[pointer
-3] >= wherex
) ==
182 (coords
[pointer
-1] >= wherex
))
184 intersects
+= (coords
[pointer
-3] >= wherex
) ? 1:0;
189 ((coords
[pointer
-3] - (coords
[pointer
-2] - wherey
) *
190 (coords
[pointer
-1] - coords
[pointer
-3]) /
191 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1:0;
195 if ((intersects
& 1) != 0)
205 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
206 return a
->GetLink( x
, y
);
208 return wxEmptyString
;
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 //--------------------------------------------------------------------------------
226 class wxHtmlImageMapCell
: public wxHtmlCell
229 wxHtmlImageMapCell( wxString
&name
);
233 virtual wxString
GetLink( int x
= 0, int y
= 0 ) const;
234 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
238 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
243 wxString
wxHtmlImageMapCell::GetLink( int x
, int y
) const
245 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
247 return a
->GetLink( x
, y
);
248 return wxHtmlCell::GetLink( x
, y
);
251 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
253 if (cond
== HTML_COND_ISIMAGEMAP
)
255 if (m_Name
== *((wxString
*)(param
)))
258 return wxHtmlCell::Find(cond
, param
);
265 //--------------------------------------------------------------------------------
268 //--------------------------------------------------------------------------------
270 class wxHtmlImageCell
: public wxHtmlCell
274 wxHtmlImageMapCell
*m_ImageMap
;
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;
286 //--------------------------------------------------------------------------------
288 //--------------------------------------------------------------------------------
290 wxHtmlImageCell::wxHtmlImageCell(wxFSFile
*input
, int w
, int h
, int align
, wxString mapname
) : wxHtmlCell()
294 wxString m
= input
-> GetMimeType();
295 wxInputStream
*s
= input
-> GetStream();
297 img
= new wxImage(*s
, m
);
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());
310 m_Image
= new wxBitmap(img
-> ConvertToBitmap());
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;
328 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
331 dc
.DrawBitmap(*m_Image
, x
+ m_PosX
, y
+ m_PosY
, TRUE
);
332 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
335 wxString
wxHtmlImageCell::GetLink( int x
, int y
) const
337 if (m_MapName
.IsEmpty())
338 return wxHtmlCell::GetLink( x
, y
);
341 wxHtmlContainerCell
*p
, *op
;
342 op
= p
= GetParent();
349 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( HTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
352 ((wxString
&)m_MapName
).Clear();
353 return wxHtmlCell::GetLink( x
, y
);
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
;
361 return m_ImageMap
->GetLink( x
, y
);
366 //--------------------------------------------------------------------------------
368 //--------------------------------------------------------------------------------
370 TAG_HANDLER_BEGIN(IMG
, "IMG, MAP, AREA")
372 TAG_HANDLER_PROC(tag
)
374 if (tag
.GetName() == "IMG")
376 if (tag
.HasParam("SRC")) {
380 wxString tmp
= tag
.GetParam("SRC");
381 wxString mn
= wxEmptyString
;
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
;
393 if (tag
.HasParam("USEMAP"))
395 mn
= tag
.GetParam( "USEMAP" );
401 wxHtmlImageCell
*cel
= NULL
;
403 cel
= new wxHtmlImageCell(str
, w
, h
, al
, mn
);
404 cel
-> SetLink(m_WParser
-> GetLink());
405 m_WParser
-> GetContainer() -> InsertCell(cel
);
410 if (tag
.GetName() == "MAP")
412 m_WParser
->CloseContainer();
413 m_WParser
->OpenContainer();
414 if (tag
.HasParam("NAME"))
416 wxString tmp
= tag
.GetParam("NAME");
417 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
418 m_WParser
->GetContainer()->InsertCell( cel
);
421 m_WParser
->CloseContainer();
422 m_WParser
->OpenContainer();
424 if (tag
.GetName() == "AREA")
426 if (tag
.HasParam("SHAPE"))
428 wxString tmp
= tag
.GetParam("SHAPE");
431 wxHtmlImageMapAreaCell
*cel
= NULL
;
432 if (tag
.HasParam("COORDS"))
434 coords
= tag
.GetParam("COORDS");
438 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
);
440 else if (tmp
== "CIRCLE")
442 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
);
444 else if (tmp
== "RECT")
446 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
);
448 if (cel
&& tag
.HasParam("HREF"))
450 wxString tmp
= tag
.GetParam("HREF");
453 m_WParser
->GetContainer()->InsertCell( cel
);
460 TAG_HANDLER_END(IMAGE
)
464 TAGS_MODULE_BEGIN(Image
)
468 TAGS_MODULE_END(Image
)