]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_image.cpp
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(mod_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
);
63 virtual wxString
GetLink( int x
= 0, int y
= 0 ) const;
70 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
)
73 wxString x
= incoords
, y
;
76 while ((i
= x
.Find( ',' )) != -1)
78 coords
.Add( wxAtoi( x
.Left( i
).c_str() ) );
81 coords
.Add( wxAtoi( x
.c_str() ) );
84 wxString
wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
96 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
110 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
119 if (coords
.GetCount() >= 6)
124 int totalv
= coords
.GetCount() / 2;
125 int totalc
= totalv
* 2;
126 int xval
= coords
[totalc
- 2];
127 int yval
= coords
[totalc
- 1];
131 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
133 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
135 intersects
+= (xval
>= wherex
) ? 1 : 0;
139 intersects
+= ((xval
- (yval
- wherey
) *
141 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
145 while (pointer
< end
)
147 yval
= coords
[pointer
];
151 while((pointer
< end
) && (coords
[pointer
] >= wherey
))
159 if ((coords
[pointer
-3] >= wherex
) ==
160 (coords
[pointer
-1] >= wherex
))
162 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;
174 while((pointer
< end
) && (coords
[pointer
] < wherey
))
182 if ((coords
[pointer
-3] >= wherex
) ==
183 (coords
[pointer
-1] >= wherex
))
185 intersects
+= (coords
[pointer
-3] >= wherex
) ? 1:0;
190 ((coords
[pointer
-3] - (coords
[pointer
-2] - wherey
) *
191 (coords
[pointer
-1] - coords
[pointer
-3]) /
192 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1:0;
196 if ((intersects
& 1) != 0)
206 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
207 return a
->GetLink( x
, y
);
209 return wxEmptyString
;
219 //--------------------------------------------------------------------------------
220 // wxHtmlImageMapCell
221 // 0-width, 0-height cell that represents map from imagemaps
222 // it is always placed before wxHtmlImageMapAreaCells
223 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
224 //--------------------------------------------------------------------------------
227 class wxHtmlImageMapCell
: public wxHtmlCell
230 wxHtmlImageMapCell( wxString
&name
);
234 virtual wxString
GetLink( int x
= 0, int y
= 0 ) const;
235 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
239 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
244 wxString
wxHtmlImageMapCell::GetLink( int x
, int y
) const
246 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
248 return a
->GetLink( x
, y
);
249 return wxHtmlCell::GetLink( x
, y
);
252 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
254 if (cond
== wxHTML_COND_ISIMAGEMAP
)
256 if (m_Name
== *((wxString
*)(param
)))
259 return wxHtmlCell::Find(cond
, param
);
266 //--------------------------------------------------------------------------------
269 //--------------------------------------------------------------------------------
271 class wxHtmlImageCell
: public wxHtmlCell
275 wxHtmlImageMapCell
*m_ImageMap
;
278 wxHtmlImageCell(wxFSFile
*input
, int w
= -1, int h
= -1, int align
= wxHTML_ALIGN_BOTTOM
, wxString mapname
= wxEmptyString
);
279 ~wxHtmlImageCell() {if (m_Image
) delete m_Image
;}
280 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
281 virtual wxString
GetLink( int x
= 0, int y
= 0 ) const;
287 //--------------------------------------------------------------------------------
289 //--------------------------------------------------------------------------------
291 wxHtmlImageCell::wxHtmlImageCell(wxFSFile
*input
, int w
, int h
, int align
, wxString mapname
) : wxHtmlCell()
295 wxInputStream
*s
= input
-> GetStream();
297 img
= new wxImage(*s
, wxBITMAP_TYPE_ANY
);
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 wxHTML_ALIGN_TOP
:
315 m_Descent
= m_Height
; break;
316 case wxHTML_ALIGN_CENTER
:
317 m_Descent
= m_Height
/ 2; break;
318 case wxHTML_ALIGN_BOTTOM
: default :
319 m_Descent
= 0; break;
324 SetCanLiveOnPagebreak(FALSE
);
329 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
332 dc
.DrawBitmap(*m_Image
, x
+ m_PosX
, y
+ m_PosY
, TRUE
);
333 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
336 wxString
wxHtmlImageCell::GetLink( int x
, int y
) const
338 if (m_MapName
.IsEmpty())
339 return wxHtmlCell::GetLink( x
, y
);
342 wxHtmlContainerCell
*p
, *op
;
343 op
= p
= GetParent();
350 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( wxHTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
353 ((wxString
&)m_MapName
).Clear();
354 return wxHtmlCell::GetLink( x
, y
);
356 { // dirty hack, ask Joel why he fills m_ImageMap in this place
357 // THE problem is that we're in const method and we can't modify m_ImageMap
358 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_ImageMap
);
359 *cx
= (wxHtmlImageMapCell
*)cell
;
362 return m_ImageMap
->GetLink( x
, y
);
367 //--------------------------------------------------------------------------------
369 //--------------------------------------------------------------------------------
371 TAG_HANDLER_BEGIN(IMG
, "IMG, MAP, AREA")
373 TAG_HANDLER_PROC(tag
)
375 if (tag
.GetName() == "IMG")
377 if (tag
.HasParam("SRC")) {
381 wxString tmp
= tag
.GetParam("SRC");
382 wxString mn
= wxEmptyString
;
384 str
= m_WParser
-> GetFS() -> OpenFile(tmp
);
385 if (tag
.HasParam(wxT("WIDTH"))) tag
.ScanParam(wxT("WIDTH"), wxT("%i"), &w
);
386 if (tag
.HasParam(wxT("HEIGHT"))) tag
.ScanParam(wxT("HEIGHT"), wxT("%i"), &h
);
387 al
= wxHTML_ALIGN_BOTTOM
;
388 if (tag
.HasParam("ALIGN")) {
389 wxString alstr
= tag
.GetParam("ALIGN");
390 alstr
.MakeUpper(); // for the case alignment was in ".."
391 if (alstr
== "TEXTTOP") al
= wxHTML_ALIGN_TOP
;
392 else if ((alstr
== "CENTER") || (alstr
== "ABSCENTER")) al
= wxHTML_ALIGN_CENTER
;
394 if (tag
.HasParam("USEMAP"))
396 mn
= tag
.GetParam( "USEMAP" );
402 wxHtmlImageCell
*cel
= NULL
;
404 cel
= new wxHtmlImageCell(str
, w
, h
, al
, mn
);
405 cel
-> SetLink(m_WParser
-> GetLink());
406 m_WParser
-> GetContainer() -> InsertCell(cel
);
411 if (tag
.GetName() == "MAP")
413 m_WParser
->CloseContainer();
414 m_WParser
->OpenContainer();
415 if (tag
.HasParam("NAME"))
417 wxString tmp
= tag
.GetParam("NAME");
418 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
419 m_WParser
->GetContainer()->InsertCell( cel
);
422 m_WParser
->CloseContainer();
423 m_WParser
->OpenContainer();
425 if (tag
.GetName() == "AREA")
427 if (tag
.HasParam("SHAPE"))
429 wxString tmp
= tag
.GetParam("SHAPE");
432 wxHtmlImageMapAreaCell
*cel
= NULL
;
433 if (tag
.HasParam("COORDS"))
435 coords
= tag
.GetParam("COORDS");
439 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
);
441 else if (tmp
== "CIRCLE")
443 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
);
445 else if (tmp
== "RECT")
447 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
);
449 if (cel
!= NULL
&& tag
.HasParam("HREF"))
451 wxString tmp
= tag
.GetParam("HREF");
454 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
461 TAG_HANDLER_END(IMAGE
)
465 TAGS_MODULE_BEGIN(Image
)
469 TAGS_MODULE_END(Image
)