]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_image.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/m_image.cpp
3 // Purpose: wxHtml module for displaying images
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik, Joel Lucsy
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
15 #if wxUSE_HTML && wxUSE_STREAMS
18 #include "wx/dynarray.h"
20 #include "wx/scrolwin.h"
22 #include "wx/dcmemory.h"
26 #include "wx/wxcrtvararg.h"
29 #include "wx/html/forcelnk.h"
30 #include "wx/html/m_templ.h"
31 #include "wx/html/htmlwin.h"
33 #include "wx/gifdecod.h"
34 #include "wx/artprov.h"
38 FORCE_LINK_ME(m_image
)
43 WX_DECLARE_OBJARRAY(int, CoordArray
);
44 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
45 WX_DEFINE_OBJARRAY(CoordArray
)
48 // ---------------------------------------------------------------------------
49 // wxHtmlImageMapAreaCell
50 // 0-width, 0-height cell that represents single area in
51 // imagemap (it's GetLink is called from wxHtmlImageCell's)
52 // ---------------------------------------------------------------------------
54 class wxHtmlImageMapAreaCell
: public wxHtmlCell
57 enum celltype
{ CIRCLE
, RECT
, POLY
};
63 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
64 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
65 void Draw(wxDC
& WXUNUSED(dc
),
66 int WXUNUSED(x
), int WXUNUSED(y
),
67 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
68 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
71 wxDECLARE_NO_COPY_CLASS(wxHtmlImageMapAreaCell
);
78 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
81 wxString x
= incoords
, y
;
84 while ((i
= x
.Find( ',' )) != wxNOT_FOUND
)
86 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
89 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
92 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
97 if ( coords
.GetCount() == 4 )
105 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
112 if ( coords
.GetCount() == 3 )
120 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
128 if (coords
.GetCount() >= 6)
133 int totalv
= coords
.GetCount() / 2;
134 int totalc
= totalv
* 2;
135 int xval
= coords
[totalc
- 2];
136 int yval
= coords
[totalc
- 1];
140 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
142 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
144 intersects
+= (xval
>= wherex
) ? 1 : 0;
148 intersects
+= ((xval
- (yval
- wherey
) *
150 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
154 while (pointer
< end
)
156 yval
= coords
[pointer
];
160 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
168 if ((coords
[pointer
- 3] >= wherex
) ==
169 (coords
[pointer
- 1] >= wherex
)) {
170 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
175 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
176 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
177 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
182 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
190 if ((coords
[pointer
- 3] >= wherex
) ==
191 (coords
[pointer
- 1] >= wherex
))
193 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
198 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
199 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
200 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
204 if ((intersects
& 1) != 0)
214 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
215 return a
->GetLink( x
, y
);
227 //--------------------------------------------------------------------------------
228 // wxHtmlImageMapCell
229 // 0-width, 0-height cell that represents map from imagemaps
230 // it is always placed before wxHtmlImageMapAreaCells
231 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
232 //--------------------------------------------------------------------------------
235 class wxHtmlImageMapCell
: public wxHtmlCell
238 wxHtmlImageMapCell( wxString
&name
);
242 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
243 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
244 void Draw(wxDC
& WXUNUSED(dc
),
245 int WXUNUSED(x
), int WXUNUSED(y
),
246 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
247 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
249 wxDECLARE_NO_COPY_CLASS(wxHtmlImageMapCell
);
253 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
258 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
260 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
262 return a
->GetLink( x
, y
);
263 return wxHtmlCell::GetLink( x
, y
);
266 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
268 if (cond
== wxHTML_COND_ISIMAGEMAP
)
270 if (m_Name
== *((wxString
*)(param
)))
273 return wxHtmlCell::Find(cond
, param
);
280 //--------------------------------------------------------------------------------
283 //--------------------------------------------------------------------------------
285 class wxHtmlImageCell
: public wxHtmlCell
288 wxHtmlImageCell(wxHtmlWindowInterface
*windowIface
,
290 int w
= wxDefaultCoord
, bool wpercent
= false,
291 int h
= wxDefaultCoord
, bool hpresent
= false,
292 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
293 const wxString
& mapname
= wxEmptyString
);
294 virtual ~wxHtmlImageCell();
295 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
296 wxHtmlRenderingInfo
& info
);
297 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
299 void SetImage(const wxImage
& img
);
301 // If "alt" text is set, it will be used when converting this cell to text.
302 void SetAlt(const wxString
& alt
);
303 virtual wxString
ConvertToText(wxHtmlSelection
*sel
) const;
305 #if wxUSE_GIF && wxUSE_TIMER
306 void AdvanceAnimation(wxTimer
*timer
);
307 virtual void Layout(int w
);
314 bool m_bmpWpercent
:1;
315 bool m_bmpHpresent
:1;
317 wxHtmlWindowInterface
*m_windowIface
;
318 #if wxUSE_GIF && wxUSE_TIMER
319 wxGIFDecoder
*m_gifDecoder
;
321 int m_physX
, m_physY
;
325 wxHtmlImageMapCell
*m_imageMap
;
329 wxDECLARE_NO_COPY_CLASS(wxHtmlImageCell
);
332 #if wxUSE_GIF && wxUSE_TIMER
333 class wxGIFTimer
: public wxTimer
336 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
337 virtual void Notify()
339 m_cell
->AdvanceAnimation(this);
343 wxHtmlImageCell
*m_cell
;
345 wxDECLARE_NO_COPY_CLASS(wxGIFTimer
);
350 //----------------------------------------------------------------------------
352 //----------------------------------------------------------------------------
355 wxHtmlImageCell::wxHtmlImageCell(wxHtmlWindowInterface
*windowIface
,
357 int w
, bool wpercent
, int h
, bool hpresent
, double scale
, int align
,
358 const wxString
& mapname
) : wxHtmlCell()
360 m_windowIface
= windowIface
;
367 m_bmpWpercent
= wpercent
;
368 m_bmpHpresent
= hpresent
;
371 SetCanLiveOnPagebreak(false);
372 #if wxUSE_GIF && wxUSE_TIMER
375 m_physX
= m_physY
= wxDefaultCoord
;
379 if ( m_bmpW
&& m_bmpH
)
383 wxInputStream
*s
= input
->GetStream();
387 #if wxUSE_GIF && wxUSE_TIMER
389 if ( m_windowIface
&&
390 (input
->GetLocation().Matches(wxT("*.gif")) ||
391 input
->GetLocation().Matches(wxT("*.GIF"))) )
393 m_gifDecoder
= new wxGIFDecoder();
394 if ( m_gifDecoder
->LoadGIF(*s
) == wxGIF_OK
)
397 if ( m_gifDecoder
->ConvertToImage(0, &img
) )
402 if ( m_gifDecoder
->IsAnimation() )
404 m_gifTimer
= new wxGIFTimer(this);
405 long delay
= m_gifDecoder
->GetDelay(0);
408 m_gifTimer
->Start(delay
, true);
412 wxDELETE(m_gifDecoder
);
417 wxDELETE(m_gifDecoder
);
422 #endif // wxUSE_GIF && wxUSE_TIMER
424 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
430 else // input==NULL, use "broken image" bitmap
432 if ( m_bmpW
== wxDefaultCoord
&& m_bmpH
== wxDefaultCoord
)
440 if ( m_bmpW
== wxDefaultCoord
) m_bmpW
= 31;
441 if ( m_bmpH
== wxDefaultCoord
) m_bmpH
= 33;
444 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
447 //else: ignore the 0-sized images used sometimes on the Web pages
451 void wxHtmlImageCell::SetImage(const wxImage
& img
)
453 #if !defined(__WXMSW__) || wxUSE_WXDIB
460 hh
= img
.GetHeight();
462 if ( m_bmpW
== wxDefaultCoord
)
464 if ( m_bmpH
== wxDefaultCoord
)
467 // Only scale the bitmap at the rendering stage,
468 // so we don't lose quality twice
470 if ((m_bmpW != ww) || (m_bmpH != hh))
472 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
473 m_bitmap = new wxBitmap(img2);
477 m_bitmap
= new wxBitmap(img
);
482 void wxHtmlImageCell::SetAlt(const wxString
& alt
)
487 wxString
wxHtmlImageCell::ConvertToText(wxHtmlSelection
* WXUNUSED(sel
)) const
492 #if wxUSE_GIF && wxUSE_TIMER
493 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
497 // advance current frame
499 if (m_nCurrFrame
== m_gifDecoder
->GetFrameCount())
502 if ( m_physX
== wxDefaultCoord
)
504 m_physX
= m_physY
= 0;
505 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
507 m_physX
+= cell
->GetPosX();
508 m_physY
+= cell
->GetPosY();
512 wxWindow
*win
= m_windowIface
->GetHTMLWindow();
514 m_windowIface
->HTMLCoordsToWindow(this, wxPoint(m_physX
, m_physY
));
515 wxRect
rect(pos
, wxSize(m_Width
, m_Height
));
517 if ( win
->GetClientRect().Intersects(rect
) &&
518 m_gifDecoder
->ConvertToImage(m_nCurrFrame
, &img
) )
520 #if !defined(__WXMSW__) || wxUSE_WXDIB
521 if ( m_gifDecoder
->GetFrameSize(m_nCurrFrame
) != wxSize(m_Width
, m_Height
) ||
522 m_gifDecoder
->GetFramePosition(m_nCurrFrame
) != wxPoint(0, 0) )
526 dc
.SelectObject(*m_bitmap
);
527 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetFramePosition(m_nCurrFrame
),
528 true /* use mask */);
533 win
->Refresh(img
.HasMask(), &rect
);
536 long delay
= m_gifDecoder
->GetDelay(m_nCurrFrame
);
539 timer
->Start(delay
, true);
542 void wxHtmlImageCell::Layout(int w
)
547 m_Width
= w
*m_bmpW
/100;
549 if (!m_bmpHpresent
&& m_bitmap
!= NULL
)
550 m_Height
= m_bitmap
->GetHeight()*m_Width
/m_bitmap
->GetWidth();
552 m_Height
= static_cast<int>(m_scale
*m_bmpH
);
555 m_Width
= static_cast<int>(m_scale
*m_bmpW
);
556 m_Height
= static_cast<int>(m_scale
*m_bmpH
);
561 case wxHTML_ALIGN_TOP
:
562 m_Descent
= m_Height
;
564 case wxHTML_ALIGN_CENTER
:
565 m_Descent
= m_Height
/ 2;
567 case wxHTML_ALIGN_BOTTOM
:
573 wxHtmlCell::Layout(w
);
574 m_physX
= m_physY
= wxDefaultCoord
;
579 wxHtmlImageCell::~wxHtmlImageCell()
582 #if wxUSE_GIF && wxUSE_TIMER
589 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
,
590 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
591 wxHtmlRenderingInfo
& WXUNUSED(info
))
595 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
596 dc
.SetPen(*wxBLACK_PEN
);
597 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
602 // We add in the scaling from the desired bitmap width
603 // and height, so we only do the scaling once.
604 double imageScaleX
= 1.0;
605 double imageScaleY
= 1.0;
606 if (m_Width
!= m_bitmap
->GetWidth())
607 imageScaleX
= (double) m_Width
/ (double) m_bitmap
->GetWidth();
608 if (m_Height
!= m_bitmap
->GetHeight())
609 imageScaleY
= (double) m_Height
/ (double) m_bitmap
->GetHeight();
612 dc
.GetUserScale(&us_x
, &us_y
);
613 dc
.SetUserScale(us_x
* imageScaleX
, us_y
* imageScaleY
);
615 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / (imageScaleX
)),
616 (int) ((y
+ m_PosY
) / (imageScaleY
)), true);
617 dc
.SetUserScale(us_x
, us_y
);
621 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
623 if (m_mapName
.empty())
624 return wxHtmlCell::GetLink( x
, y
);
627 wxHtmlContainerCell
*p
, *op
;
628 op
= p
= GetParent();
635 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
636 (const void*)(&m_mapName
));
639 ((wxString
&)m_mapName
).Clear();
640 return wxHtmlCell::GetLink( x
, y
);
642 { // dirty hack, ask Joel why he fills m_ImageMap in this place
643 // THE problem is that we're in const method and we can't modify m_ImageMap
644 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
645 *cx
= (wxHtmlImageMapCell
*)cell
;
648 return m_imageMap
->GetLink(x
, y
);
653 //--------------------------------------------------------------------------------
655 //--------------------------------------------------------------------------------
657 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
658 TAG_HANDLER_CONSTR(IMG
) { }
660 TAG_HANDLER_PROC(tag
)
662 if (tag
.GetName() == wxT("IMG"))
664 if (tag
.HasParam(wxT("SRC")))
666 int w
= wxDefaultCoord
, h
= wxDefaultCoord
;
667 bool wpercent
= false;
668 bool hpresent
= false;
671 wxString tmp
= tag
.GetParam(wxT("SRC"));
672 wxString mn
= wxEmptyString
;
674 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
676 if (tag
.HasParam(wxT("WIDTH")))
678 if (tag
.GetParamAsIntOrPercent(wxT("WIDTH"), &w
, wpercent
))
690 if (tag
.HasParam(wxT("HEIGHT")))
692 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
696 al
= wxHTML_ALIGN_BOTTOM
;
697 if (tag
.HasParam(wxT("ALIGN")))
699 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
700 alstr
.MakeUpper(); // for the case alignment was in ".."
701 if (alstr
== wxT("TEXTTOP"))
702 al
= wxHTML_ALIGN_TOP
;
703 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
704 al
= wxHTML_ALIGN_CENTER
;
706 if (tag
.HasParam(wxT("USEMAP")))
708 mn
= tag
.GetParam( wxT("USEMAP") );
709 if ( !mn
.empty() && *mn
.begin() == '#' )
714 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
715 m_WParser
->GetWindowInterface(),
716 str
, w
, wpercent
, h
, hpresent
,
717 m_WParser
->GetPixelScale(),
719 m_WParser
->ApplyStateToCell(cel
);
720 m_WParser
->StopCollapsingSpaces();
721 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
722 cel
->SetAlt(tag
.GetParam(wxT("alt")));
723 m_WParser
->GetContainer()->InsertCell(cel
);
728 if (tag
.GetName() == wxT("MAP"))
730 m_WParser
->CloseContainer();
731 m_WParser
->OpenContainer();
732 if (tag
.HasParam(wxT("NAME")))
734 wxString tmp
= tag
.GetParam(wxT("NAME"));
735 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
736 m_WParser
->GetContainer()->InsertCell( cel
);
739 m_WParser
->CloseContainer();
740 m_WParser
->OpenContainer();
742 if (tag
.GetName() == wxT("AREA"))
744 if (tag
.HasParam(wxT("SHAPE")))
746 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
747 wxString coords
= wxEmptyString
;
749 wxHtmlImageMapAreaCell
*cel
= NULL
;
750 if (tag
.HasParam(wxT("COORDS")))
752 coords
= tag
.GetParam(wxT("COORDS"));
754 if (tmp
== wxT("POLY"))
756 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
758 else if (tmp
== wxT("CIRCLE"))
760 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
762 else if (tmp
== wxT("RECT"))
764 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
766 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
769 if (tag
.HasParam(wxT("TARGET")))
770 target
= tag
.GetParam(wxT("TARGET"));
771 cel
->SetLink(wxHtmlLinkInfo(tag
.GetParam(wxT("HREF")), target
));
774 m_WParser
->GetContainer()->InsertCell( cel
);
785 TAGS_MODULE_BEGIN(Image
)
789 TAGS_MODULE_END(Image
)