]>
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
6 // Copyright: (c) 1999 Vaclav Slavik, Joel Lucsy
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_HTML && wxUSE_STREAMS
19 #include "wx/dynarray.h"
21 #include "wx/scrolwin.h"
23 #include "wx/dcmemory.h"
27 #include "wx/wxcrtvararg.h"
30 #include "wx/html/forcelnk.h"
31 #include "wx/html/m_templ.h"
32 #include "wx/html/htmlwin.h"
34 #include "wx/gifdecod.h"
35 #include "wx/artprov.h"
39 FORCE_LINK_ME(m_image
)
44 WX_DECLARE_OBJARRAY(int, CoordArray
);
45 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
46 WX_DEFINE_OBJARRAY(CoordArray
)
49 // ---------------------------------------------------------------------------
50 // wxHtmlImageMapAreaCell
51 // 0-width, 0-height cell that represents single area in
52 // imagemap (it's GetLink is called from wxHtmlImageCell's)
53 // ---------------------------------------------------------------------------
55 class wxHtmlImageMapAreaCell
: public wxHtmlCell
58 enum celltype
{ CIRCLE
, RECT
, POLY
};
64 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
65 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
66 void Draw(wxDC
& WXUNUSED(dc
),
67 int WXUNUSED(x
), int WXUNUSED(y
),
68 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
69 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
72 wxDECLARE_NO_COPY_CLASS(wxHtmlImageMapAreaCell
);
79 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
82 wxString x
= incoords
, y
;
85 while ((i
= x
.Find( ',' )) != wxNOT_FOUND
)
87 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
90 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
93 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
105 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
119 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)
215 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
216 return a
->GetLink( x
, y
);
228 //--------------------------------------------------------------------------------
229 // wxHtmlImageMapCell
230 // 0-width, 0-height cell that represents map from imagemaps
231 // it is always placed before wxHtmlImageMapAreaCells
232 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
233 //--------------------------------------------------------------------------------
236 class wxHtmlImageMapCell
: public wxHtmlCell
239 wxHtmlImageMapCell( wxString
&name
);
243 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
244 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
245 void Draw(wxDC
& WXUNUSED(dc
),
246 int WXUNUSED(x
), int WXUNUSED(y
),
247 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
248 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
250 wxDECLARE_NO_COPY_CLASS(wxHtmlImageMapCell
);
254 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
259 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
261 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
263 return a
->GetLink( x
, y
);
264 return wxHtmlCell::GetLink( x
, y
);
267 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
269 if (cond
== wxHTML_COND_ISIMAGEMAP
)
271 if (m_Name
== *((wxString
*)(param
)))
274 return wxHtmlCell::Find(cond
, param
);
281 //--------------------------------------------------------------------------------
284 //--------------------------------------------------------------------------------
286 class wxHtmlImageCell
: public wxHtmlCell
289 wxHtmlImageCell(wxHtmlWindowInterface
*windowIface
,
291 int w
= wxDefaultCoord
, bool wpercent
= false,
292 int h
= wxDefaultCoord
, bool hpresent
= false,
293 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
294 const wxString
& mapname
= wxEmptyString
);
295 virtual ~wxHtmlImageCell();
296 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
297 wxHtmlRenderingInfo
& info
);
298 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
300 void SetImage(const wxImage
& img
);
302 // If "alt" text is set, it will be used when converting this cell to text.
303 void SetAlt(const wxString
& alt
);
304 virtual wxString
ConvertToText(wxHtmlSelection
*sel
) const;
306 #if wxUSE_GIF && wxUSE_TIMER
307 void AdvanceAnimation(wxTimer
*timer
);
308 virtual void Layout(int w
);
315 bool m_bmpWpercent
:1;
316 bool m_bmpHpresent
:1;
318 wxHtmlWindowInterface
*m_windowIface
;
319 #if wxUSE_GIF && wxUSE_TIMER
320 wxGIFDecoder
*m_gifDecoder
;
322 int m_physX
, m_physY
;
326 wxHtmlImageMapCell
*m_imageMap
;
330 wxDECLARE_NO_COPY_CLASS(wxHtmlImageCell
);
333 #if wxUSE_GIF && wxUSE_TIMER
334 class wxGIFTimer
: public wxTimer
337 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
338 virtual void Notify()
340 m_cell
->AdvanceAnimation(this);
344 wxHtmlImageCell
*m_cell
;
346 wxDECLARE_NO_COPY_CLASS(wxGIFTimer
);
351 //----------------------------------------------------------------------------
353 //----------------------------------------------------------------------------
356 wxHtmlImageCell::wxHtmlImageCell(wxHtmlWindowInterface
*windowIface
,
358 int w
, bool wpercent
, int h
, bool hpresent
, double scale
, int align
,
359 const wxString
& mapname
) : wxHtmlCell()
361 m_windowIface
= windowIface
;
368 m_bmpWpercent
= wpercent
;
369 m_bmpHpresent
= hpresent
;
372 SetCanLiveOnPagebreak(false);
373 #if wxUSE_GIF && wxUSE_TIMER
376 m_physX
= m_physY
= wxDefaultCoord
;
380 if ( m_bmpW
&& m_bmpH
)
384 wxInputStream
*s
= input
->GetStream();
388 #if wxUSE_GIF && wxUSE_TIMER
390 if ( m_windowIface
&&
391 (input
->GetLocation().Matches(wxT("*.gif")) ||
392 input
->GetLocation().Matches(wxT("*.GIF"))) )
394 m_gifDecoder
= new wxGIFDecoder();
395 if ( m_gifDecoder
->LoadGIF(*s
) == wxGIF_OK
)
398 if ( m_gifDecoder
->ConvertToImage(0, &img
) )
403 if ( m_gifDecoder
->IsAnimation() )
405 m_gifTimer
= new wxGIFTimer(this);
406 long delay
= m_gifDecoder
->GetDelay(0);
409 m_gifTimer
->Start(delay
, true);
413 wxDELETE(m_gifDecoder
);
418 wxDELETE(m_gifDecoder
);
423 #endif // wxUSE_GIF && wxUSE_TIMER
425 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
431 else // input==NULL, use "broken image" bitmap
433 if ( m_bmpW
== wxDefaultCoord
&& m_bmpH
== wxDefaultCoord
)
441 if ( m_bmpW
== wxDefaultCoord
) m_bmpW
= 31;
442 if ( m_bmpH
== wxDefaultCoord
) m_bmpH
= 33;
445 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
448 //else: ignore the 0-sized images used sometimes on the Web pages
452 void wxHtmlImageCell::SetImage(const wxImage
& img
)
454 #if !defined(__WXMSW__) || wxUSE_WXDIB
461 hh
= img
.GetHeight();
463 if ( m_bmpW
== wxDefaultCoord
)
465 if ( m_bmpH
== wxDefaultCoord
)
468 // Only scale the bitmap at the rendering stage,
469 // so we don't lose quality twice
471 if ((m_bmpW != ww) || (m_bmpH != hh))
473 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
474 m_bitmap = new wxBitmap(img2);
478 m_bitmap
= new wxBitmap(img
);
483 void wxHtmlImageCell::SetAlt(const wxString
& alt
)
488 wxString
wxHtmlImageCell::ConvertToText(wxHtmlSelection
* WXUNUSED(sel
)) const
493 #if wxUSE_GIF && wxUSE_TIMER
494 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
498 // advance current frame
500 if (m_nCurrFrame
== m_gifDecoder
->GetFrameCount())
503 if ( m_physX
== wxDefaultCoord
)
505 m_physX
= m_physY
= 0;
506 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
508 m_physX
+= cell
->GetPosX();
509 m_physY
+= cell
->GetPosY();
513 wxWindow
*win
= m_windowIface
->GetHTMLWindow();
515 m_windowIface
->HTMLCoordsToWindow(this, wxPoint(m_physX
, m_physY
));
516 wxRect
rect(pos
, wxSize(m_Width
, m_Height
));
518 if ( win
->GetClientRect().Intersects(rect
) &&
519 m_gifDecoder
->ConvertToImage(m_nCurrFrame
, &img
) )
521 #if !defined(__WXMSW__) || wxUSE_WXDIB
522 if ( m_gifDecoder
->GetFrameSize(m_nCurrFrame
) != wxSize(m_Width
, m_Height
) ||
523 m_gifDecoder
->GetFramePosition(m_nCurrFrame
) != wxPoint(0, 0) )
527 dc
.SelectObject(*m_bitmap
);
528 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetFramePosition(m_nCurrFrame
),
529 true /* use mask */);
534 win
->Refresh(img
.HasMask(), &rect
);
537 long delay
= m_gifDecoder
->GetDelay(m_nCurrFrame
);
540 timer
->Start(delay
, true);
543 void wxHtmlImageCell::Layout(int w
)
548 m_Width
= w
*m_bmpW
/100;
550 if (!m_bmpHpresent
&& m_bitmap
!= NULL
)
551 m_Height
= m_bitmap
->GetHeight()*m_Width
/m_bitmap
->GetWidth();
553 m_Height
= static_cast<int>(m_scale
*m_bmpH
);
556 m_Width
= static_cast<int>(m_scale
*m_bmpW
);
557 m_Height
= static_cast<int>(m_scale
*m_bmpH
);
562 case wxHTML_ALIGN_TOP
:
563 m_Descent
= m_Height
;
565 case wxHTML_ALIGN_CENTER
:
566 m_Descent
= m_Height
/ 2;
568 case wxHTML_ALIGN_BOTTOM
:
574 wxHtmlCell::Layout(w
);
575 m_physX
= m_physY
= wxDefaultCoord
;
580 wxHtmlImageCell::~wxHtmlImageCell()
583 #if wxUSE_GIF && wxUSE_TIMER
590 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
,
591 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
592 wxHtmlRenderingInfo
& WXUNUSED(info
))
596 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
597 dc
.SetPen(*wxBLACK_PEN
);
598 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
603 // We add in the scaling from the desired bitmap width
604 // and height, so we only do the scaling once.
605 double imageScaleX
= 1.0;
606 double imageScaleY
= 1.0;
607 if (m_Width
!= m_bitmap
->GetWidth())
608 imageScaleX
= (double) m_Width
/ (double) m_bitmap
->GetWidth();
609 if (m_Height
!= m_bitmap
->GetHeight())
610 imageScaleY
= (double) m_Height
/ (double) m_bitmap
->GetHeight();
613 dc
.GetUserScale(&us_x
, &us_y
);
614 dc
.SetUserScale(us_x
* imageScaleX
, us_y
* imageScaleY
);
616 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / (imageScaleX
)),
617 (int) ((y
+ m_PosY
) / (imageScaleY
)), true);
618 dc
.SetUserScale(us_x
, us_y
);
622 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
624 if (m_mapName
.empty())
625 return wxHtmlCell::GetLink( x
, y
);
628 wxHtmlContainerCell
*p
, *op
;
629 op
= p
= GetParent();
636 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
637 (const void*)(&m_mapName
));
640 ((wxString
&)m_mapName
).Clear();
641 return wxHtmlCell::GetLink( x
, y
);
643 { // dirty hack, ask Joel why he fills m_ImageMap in this place
644 // THE problem is that we're in const method and we can't modify m_ImageMap
645 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
646 *cx
= (wxHtmlImageMapCell
*)cell
;
649 return m_imageMap
->GetLink(x
, y
);
654 //--------------------------------------------------------------------------------
656 //--------------------------------------------------------------------------------
658 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
659 TAG_HANDLER_CONSTR(IMG
) { }
661 TAG_HANDLER_PROC(tag
)
663 if (tag
.GetName() == wxT("IMG"))
665 if (tag
.HasParam(wxT("SRC")))
667 int w
= wxDefaultCoord
, h
= wxDefaultCoord
;
668 bool wpercent
= false;
669 bool hpresent
= false;
672 wxString tmp
= tag
.GetParam(wxT("SRC"));
673 wxString mn
= wxEmptyString
;
675 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
677 if (tag
.HasParam(wxT("WIDTH")))
679 if (tag
.GetParamAsIntOrPercent(wxT("WIDTH"), &w
, wpercent
))
691 if (tag
.HasParam(wxT("HEIGHT")))
693 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
697 al
= wxHTML_ALIGN_BOTTOM
;
698 if (tag
.HasParam(wxT("ALIGN")))
700 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
701 alstr
.MakeUpper(); // for the case alignment was in ".."
702 if (alstr
== wxT("TEXTTOP"))
703 al
= wxHTML_ALIGN_TOP
;
704 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
705 al
= wxHTML_ALIGN_CENTER
;
707 if (tag
.HasParam(wxT("USEMAP")))
709 mn
= tag
.GetParam( wxT("USEMAP") );
710 if ( !mn
.empty() && *mn
.begin() == '#' )
715 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
716 m_WParser
->GetWindowInterface(),
717 str
, w
, wpercent
, h
, hpresent
,
718 m_WParser
->GetPixelScale(),
720 m_WParser
->ApplyStateToCell(cel
);
721 m_WParser
->StopCollapsingSpaces();
722 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
723 cel
->SetAlt(tag
.GetParam(wxT("alt")));
724 m_WParser
->GetContainer()->InsertCell(cel
);
729 if (tag
.GetName() == wxT("MAP"))
731 m_WParser
->CloseContainer();
732 m_WParser
->OpenContainer();
733 if (tag
.HasParam(wxT("NAME")))
735 wxString tmp
= tag
.GetParam(wxT("NAME"));
736 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
737 m_WParser
->GetContainer()->InsertCell( cel
);
740 m_WParser
->CloseContainer();
741 m_WParser
->OpenContainer();
743 if (tag
.GetName() == wxT("AREA"))
745 if (tag
.HasParam(wxT("SHAPE")))
747 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
748 wxString coords
= wxEmptyString
;
750 wxHtmlImageMapAreaCell
*cel
= NULL
;
751 if (tag
.HasParam(wxT("COORDS")))
753 coords
= tag
.GetParam(wxT("COORDS"));
755 if (tmp
== wxT("POLY"))
757 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
759 else if (tmp
== wxT("CIRCLE"))
761 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
763 else if (tmp
== wxT("RECT"))
765 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
767 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
770 if (tag
.HasParam(wxT("TARGET")))
771 target
= tag
.GetParam(wxT("TARGET"));
772 cel
->SetLink(wxHtmlLinkInfo(tag
.GetParam(wxT("HREF")), target
));
775 m_WParser
->GetContainer()->InsertCell( cel
);
786 TAGS_MODULE_BEGIN(Image
)
790 TAGS_MODULE_END(Image
)