]>
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 wxInputStream
*s
= input
-> GetStream();
296 img
= new wxImage(*s
, wxBITMAP_TYPE_ANY
);
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());
309 m_Image
= new wxBitmap(img
-> ConvertToBitmap());
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;
327 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
330 dc
.DrawBitmap(*m_Image
, x
+ m_PosX
, y
+ m_PosY
, TRUE
);
331 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
334 wxString
wxHtmlImageCell::GetLink( int x
, int y
) const
336 if (m_MapName
.IsEmpty())
337 return wxHtmlCell::GetLink( x
, y
);
340 wxHtmlContainerCell
*p
, *op
;
341 op
= p
= GetParent();
348 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( HTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
351 ((wxString
&)m_MapName
).Clear();
352 return wxHtmlCell::GetLink( x
, y
);
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
;
360 return m_ImageMap
->GetLink( x
, y
);
365 //--------------------------------------------------------------------------------
367 //--------------------------------------------------------------------------------
369 TAG_HANDLER_BEGIN(IMG
, "IMG, MAP, AREA")
371 TAG_HANDLER_PROC(tag
)
373 if (tag
.GetName() == "IMG")
375 if (tag
.HasParam("SRC")) {
379 wxString tmp
= tag
.GetParam("SRC");
380 wxString mn
= wxEmptyString
;
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
;
392 if (tag
.HasParam("USEMAP"))
394 mn
= tag
.GetParam( "USEMAP" );
400 wxHtmlImageCell
*cel
= NULL
;
402 cel
= new wxHtmlImageCell(str
, w
, h
, al
, mn
);
403 cel
-> SetLink(m_WParser
-> GetLink());
404 m_WParser
-> GetContainer() -> InsertCell(cel
);
409 if (tag
.GetName() == "MAP")
411 m_WParser
->CloseContainer();
412 m_WParser
->OpenContainer();
413 if (tag
.HasParam("NAME"))
415 wxString tmp
= tag
.GetParam("NAME");
416 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
417 m_WParser
->GetContainer()->InsertCell( cel
);
420 m_WParser
->CloseContainer();
421 m_WParser
->OpenContainer();
423 if (tag
.GetName() == "AREA")
425 if (tag
.HasParam("SHAPE"))
427 wxString tmp
= tag
.GetParam("SHAPE");
430 wxHtmlImageMapAreaCell
*cel
= NULL
;
431 if (tag
.HasParam("COORDS"))
433 coords
= tag
.GetParam("COORDS");
437 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
);
439 else if (tmp
== "CIRCLE")
441 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
);
443 else if (tmp
== "RECT")
445 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
);
447 if (cel
&& tag
.HasParam("HREF"))
449 wxString tmp
= tag
.GetParam("HREF");
452 m_WParser
->GetContainer()->InsertCell( cel
);
459 TAG_HANDLER_END(IMAGE
)
463 TAGS_MODULE_BEGIN(Image
)
467 TAGS_MODULE_END(Image
)