]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_image.cpp
35165bb4f44406f564b4fd74cf4885080371f520
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml module for displaying images
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik, Joel Lucsy
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include "wx/wxprec.h"
27 #include "wx/html/forcelnk.h"
28 #include "wx/html/m_templ.h"
32 #include "wx/dynarray.h"
37 FORCE_LINK_ME(m_image
)
42 WX_DECLARE_OBJARRAY(int, CoordArray
);
43 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
44 WX_DEFINE_OBJARRAY(CoordArray
);
47 //--------------------------------------------------------------------------------
48 // wxHtmlImageMapAreaCell
49 // 0-width, 0-height cell that represents single area in imagemap
50 // (it's GetLink is called from wxHtmlImageCell's)
51 //--------------------------------------------------------------------------------
53 class wxHtmlImageMapAreaCell
: public wxHtmlCell
56 enum celltype
{ CIRCLE
, RECT
, POLY
};
62 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
63 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
70 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
73 wxString x
= incoords
, y
;
76 while ((i
= x
.Find( ',' )) != -1) {
77 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
80 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
83 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
94 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
) {
107 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
115 if (coords
.GetCount() >= 6) {
119 int totalv
= coords
.GetCount() / 2;
120 int totalc
= totalv
* 2;
121 int xval
= coords
[totalc
- 2];
122 int yval
= coords
[totalc
- 1];
126 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
)) {
127 if ((xval
>= wherex
) == (coords
[0] >= wherex
)) {
128 intersects
+= (xval
>= wherex
) ? 1 : 0;
130 intersects
+= ((xval
- (yval
- wherey
) *
132 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
136 while (pointer
< end
) {
137 yval
= coords
[pointer
];
139 if (yval
>= wherey
) {
140 while ((pointer
< end
) && (coords
[pointer
] >= wherey
)) {
143 if (pointer
>= end
) {
146 if ((coords
[pointer
- 3] >= wherex
) ==
147 (coords
[pointer
- 1] >= wherex
)) {
148 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
151 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
152 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
153 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
156 while ((pointer
< end
) && (coords
[pointer
] < wherey
)) {
159 if (pointer
>= end
) {
162 if ((coords
[pointer
- 3] >= wherex
) ==
163 (coords
[pointer
- 1] >= wherex
)) {
164 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
167 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
168 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
169 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
173 if ((intersects
& 1) != 0) {
181 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
182 return a
->GetLink( x
, y
);
194 //--------------------------------------------------------------------------------
195 // wxHtmlImageMapCell
196 // 0-width, 0-height cell that represents map from imagemaps
197 // it is always placed before wxHtmlImageMapAreaCells
198 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
199 //--------------------------------------------------------------------------------
202 class wxHtmlImageMapCell
: public wxHtmlCell
205 wxHtmlImageMapCell( wxString
&name
);
209 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
210 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
214 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
219 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
221 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
223 return a
->GetLink( x
, y
);
224 return wxHtmlCell::GetLink( x
, y
);
227 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
229 if (cond
== wxHTML_COND_ISIMAGEMAP
) {
230 if (m_Name
== *((wxString
*)(param
)))
233 return wxHtmlCell::Find(cond
, param
);
240 //--------------------------------------------------------------------------------
243 //--------------------------------------------------------------------------------
245 class wxHtmlImageCell
: public wxHtmlCell
249 wxHtmlImageMapCell
*m_ImageMap
;
252 wxHtmlImageCell(wxFSFile
*input
, int w
= -1, int h
= -1, int align
= wxHTML_ALIGN_BOTTOM
, wxString mapname
= wxEmptyString
);
253 ~wxHtmlImageCell() {if (m_Image
) delete m_Image
; }
254 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
255 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
261 //--------------------------------------------------------------------------------
263 //--------------------------------------------------------------------------------
265 wxHtmlImageCell::wxHtmlImageCell(wxFSFile
*input
, int w
, int h
, int align
, wxString mapname
) : wxHtmlCell()
269 wxInputStream
*s
= input
-> GetStream();
271 img
= new wxImage(*s
, wxBITMAP_TYPE_ANY
);
274 if (img
&& (img
-> Ok())) {
275 ww
= img
-> GetWidth();
276 hh
= img
-> GetHeight();
277 if (w
!= -1) m_Width
= w
; else m_Width
= ww
;
278 if (h
!= -1) m_Height
= h
; else m_Height
= hh
;
279 if ((m_Width
!= ww
) || (m_Height
!= hh
)) {
280 wxImage img2
= img
-> Scale(m_Width
, m_Height
);
281 m_Image
= new wxBitmap(img2
.ConvertToBitmap());
283 m_Image
= new wxBitmap(img
-> ConvertToBitmap());
287 case wxHTML_ALIGN_TOP
:
288 m_Descent
= m_Height
;
290 case wxHTML_ALIGN_CENTER
:
291 m_Descent
= m_Height
/ 2;
293 case wxHTML_ALIGN_BOTTOM
:
301 SetCanLiveOnPagebreak(FALSE
);
306 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
309 // dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, (m_Image->GetMask() != (wxMask*) 0));
310 dc
.DrawBitmap(*m_Image
, x
+ m_PosX
, y
+ m_PosY
, TRUE
);
311 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
314 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
316 if (m_MapName
.IsEmpty())
317 return wxHtmlCell::GetLink( x
, y
);
319 wxHtmlContainerCell
*p
, *op
;
320 op
= p
= GetParent();
326 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( wxHTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
328 ((wxString
&)m_MapName
).Clear();
329 return wxHtmlCell::GetLink( x
, y
);
331 { // dirty hack, ask Joel why he fills m_ImageMap in this place
332 // THE problem is that we're in const method and we can't modify m_ImageMap
333 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_ImageMap
);
334 *cx
= (wxHtmlImageMapCell
*)cell
;
337 return m_ImageMap
->GetLink( x
, y
);
342 //--------------------------------------------------------------------------------
344 //--------------------------------------------------------------------------------
346 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
348 TAG_HANDLER_PROC(tag
)
350 if (tag
.GetName() == wxT("IMG")) {
351 if (tag
.HasParam(wxT("SRC"))) {
355 wxString tmp
= tag
.GetParam(wxT("SRC"));
356 wxString mn
= wxEmptyString
;
358 str
= m_WParser
-> GetFS() -> OpenFile(tmp
);
359 if (tag
.HasParam(wxT("WIDTH"))) tag
.ScanParam(wxT("WIDTH"), wxT("%i"), &w
);
360 if (tag
.HasParam(wxT("HEIGHT"))) tag
.ScanParam(wxT("HEIGHT"), wxT("%i"), &h
);
361 al
= wxHTML_ALIGN_BOTTOM
;
362 if (tag
.HasParam(wxT("ALIGN"))) {
363 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
364 alstr
.MakeUpper(); // for the case alignment was in ".."
365 if (alstr
== wxT("TEXTTOP")) al
= wxHTML_ALIGN_TOP
;
366 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER"))) al
= wxHTML_ALIGN_CENTER
;
368 if (tag
.HasParam(wxT("USEMAP"))) {
369 mn
= tag
.GetParam( wxT("USEMAP") );
370 if (mn
[ (unsigned int) 0 ] == wxT('#')) {
374 wxHtmlImageCell
*cel
= NULL
;
376 int neww
= (w
== -1) ? -1 : (int)(m_WParser
-> GetPixelScale() * (double)w
),
377 newh
= (h
== -1) ? -1 : (int)(m_WParser
-> GetPixelScale() * (double)h
);
379 cel
= new wxHtmlImageCell(str
, neww
, newh
, al
, mn
);
380 cel
-> SetLink(m_WParser
-> GetLink());
381 m_WParser
-> GetContainer() -> InsertCell(cel
);
386 if (tag
.GetName() == wxT("MAP")) {
387 m_WParser
->CloseContainer();
388 m_WParser
->OpenContainer();
389 if (tag
.HasParam(wxT("NAME"))) {
390 wxString tmp
= tag
.GetParam(wxT("NAME"));
391 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
392 m_WParser
->GetContainer()->InsertCell( cel
);
395 m_WParser
->CloseContainer();
396 m_WParser
->OpenContainer();
398 if (tag
.GetName() == wxT("AREA")) {
399 if (tag
.HasParam(wxT("SHAPE"))) {
400 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
401 wxString coords
= wxEmptyString
;
403 wxHtmlImageMapAreaCell
*cel
= NULL
;
404 if (tag
.HasParam(wxT("COORDS"))) {
405 coords
= tag
.GetParam(wxT("COORDS"));
407 if (tmp
== wxT("POLY")) {
408 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
-> GetPixelScale() );
409 } else if (tmp
== wxT("CIRCLE")) {
410 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
-> GetPixelScale() );
411 } else if (tmp
== wxT("RECT")) {
412 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
-> GetPixelScale() );
414 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF"))) {
415 wxString tmp
= tag
.GetParam(wxT("HREF"));
416 wxString target
= wxEmptyString
;
417 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
418 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
420 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
431 TAGS_MODULE_BEGIN(Image
)
435 TAGS_MODULE_END(Image
)