]>
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"
17 #if wxUSE_HTML && wxUSE_STREAMS
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)
78 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
81 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
84 wxHtmlLinkInfo
*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
)) {
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)
206 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
207 return a
->GetLink( x
, y
);
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 wxHtmlLinkInfo
*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 wxHtmlLinkInfo
*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, double scale
= 1.0, 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 wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
287 //--------------------------------------------------------------------------------
289 //--------------------------------------------------------------------------------
291 wxHtmlImageCell::wxHtmlImageCell(wxFSFile
*input
, int w
, int h
, double scale
, int align
, wxString mapname
) : wxHtmlCell()
295 wxInputStream
*s
= input
->GetStream();
297 img
= new wxImage(*s
, wxBITMAP_TYPE_ANY
);
300 if (img
&& (img
->Ok()))
302 ww
= img
->GetWidth();
303 hh
= img
->GetHeight();
304 if (w
!= -1) m_Width
= w
; else m_Width
= ww
;
305 if (h
!= -1) m_Height
= h
; else m_Height
= hh
;
307 m_Width
= (int)(scale
* (double)m_Width
);
308 m_Height
= (int)(scale
* (double)m_Height
);
310 if ((m_Width
!= ww
) || (m_Height
!= hh
))
312 wxImage img2
= img
->Scale(m_Width
, m_Height
);
313 m_Image
= new wxBitmap(img2
.ConvertToBitmap());
316 m_Image
= new wxBitmap(img
->ConvertToBitmap());
321 case wxHTML_ALIGN_TOP
:
322 m_Descent
= m_Height
;
324 case wxHTML_ALIGN_CENTER
:
325 m_Descent
= m_Height
/ 2;
327 case wxHTML_ALIGN_BOTTOM
:
335 SetCanLiveOnPagebreak(FALSE
);
340 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
343 // dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, (m_Image->GetMask() != (wxMask*) 0));
344 dc
.DrawBitmap(*m_Image
, x
+ m_PosX
, y
+ m_PosY
, TRUE
);
345 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
348 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
350 if (m_MapName
.IsEmpty())
351 return wxHtmlCell::GetLink( x
, y
);
354 wxHtmlContainerCell
*p
, *op
;
355 op
= p
= GetParent();
362 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( wxHTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
365 ((wxString
&)m_MapName
).Clear();
366 return wxHtmlCell::GetLink( x
, y
);
368 { // dirty hack, ask Joel why he fills m_ImageMap in this place
369 // THE problem is that we're in const method and we can't modify m_ImageMap
370 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_ImageMap
);
371 *cx
= (wxHtmlImageMapCell
*)cell
;
374 return m_ImageMap
->GetLink( x
, y
);
379 //--------------------------------------------------------------------------------
381 //--------------------------------------------------------------------------------
383 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
385 TAG_HANDLER_PROC(tag
)
387 if (tag
.GetName() == wxT("IMG"))
389 if (tag
.HasParam(wxT("SRC")))
394 wxString tmp
= tag
.GetParam(wxT("SRC"));
395 wxString mn
= wxEmptyString
;
397 str
= m_WParser
->GetFS()->OpenFile(tmp
);
398 if (tag
.HasParam(wxT("WIDTH"))) tag
.ScanParam(wxT("WIDTH"), wxT("%i"), &w
);
399 if (tag
.HasParam(wxT("HEIGHT"))) tag
.ScanParam(wxT("HEIGHT"), wxT("%i"), &h
);
400 al
= wxHTML_ALIGN_BOTTOM
;
401 if (tag
.HasParam(wxT("ALIGN")))
403 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
404 alstr
.MakeUpper(); // for the case alignment was in ".."
405 if (alstr
== wxT("TEXTTOP")) al
= wxHTML_ALIGN_TOP
;
406 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER"))) al
= wxHTML_ALIGN_CENTER
;
408 if (tag
.HasParam(wxT("USEMAP")))
410 mn
= tag
.GetParam( wxT("USEMAP") );
411 if (mn
[ (unsigned int) 0 ] == wxT('#'))
416 wxHtmlImageCell
*cel
= NULL
;
419 cel
= new wxHtmlImageCell(str
, w
, h
, m_WParser
->GetPixelScale(), al
, mn
);
420 cel
->SetLink(m_WParser
->GetLink());
421 m_WParser
->GetContainer()->InsertCell(cel
);
426 if (tag
.GetName() == wxT("MAP"))
428 m_WParser
->CloseContainer();
429 m_WParser
->OpenContainer();
430 if (tag
.HasParam(wxT("NAME")))
432 wxString tmp
= tag
.GetParam(wxT("NAME"));
433 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
434 m_WParser
->GetContainer()->InsertCell( cel
);
437 m_WParser
->CloseContainer();
438 m_WParser
->OpenContainer();
440 if (tag
.GetName() == wxT("AREA"))
442 if (tag
.HasParam(wxT("SHAPE")))
444 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
445 wxString coords
= wxEmptyString
;
447 wxHtmlImageMapAreaCell
*cel
= NULL
;
448 if (tag
.HasParam(wxT("COORDS")))
450 coords
= tag
.GetParam(wxT("COORDS"));
452 if (tmp
== wxT("POLY"))
454 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
456 else if (tmp
== wxT("CIRCLE"))
458 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
460 else if (tmp
== wxT("RECT"))
462 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
464 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
466 wxString tmp
= tag
.GetParam(wxT("HREF"));
467 wxString target
= wxEmptyString
;
468 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
469 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
471 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
482 TAGS_MODULE_BEGIN(Image
)
486 TAGS_MODULE_END(Image
)