]>
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
);
301 #if wxUSE_GIF && wxUSE_TIMER
302 void AdvanceAnimation(wxTimer
*timer
);
303 virtual void Layout(int w
);
310 bool m_bmpWpercent
:1;
311 bool m_bmpHpresent
:1;
313 wxHtmlWindowInterface
*m_windowIface
;
314 #if wxUSE_GIF && wxUSE_TIMER
315 wxGIFDecoder
*m_gifDecoder
;
317 int m_physX
, m_physY
;
321 wxHtmlImageMapCell
*m_imageMap
;
324 wxDECLARE_NO_COPY_CLASS(wxHtmlImageCell
);
327 #if wxUSE_GIF && wxUSE_TIMER
328 class wxGIFTimer
: public wxTimer
331 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
332 virtual void Notify()
334 m_cell
->AdvanceAnimation(this);
338 wxHtmlImageCell
*m_cell
;
340 wxDECLARE_NO_COPY_CLASS(wxGIFTimer
);
345 //----------------------------------------------------------------------------
347 //----------------------------------------------------------------------------
350 wxHtmlImageCell::wxHtmlImageCell(wxHtmlWindowInterface
*windowIface
,
352 int w
, bool wpercent
, int h
, bool hpresent
, double scale
, int align
,
353 const wxString
& mapname
) : wxHtmlCell()
355 m_windowIface
= windowIface
;
362 m_bmpWpercent
= wpercent
;
363 m_bmpHpresent
= hpresent
;
366 SetCanLiveOnPagebreak(false);
367 #if wxUSE_GIF && wxUSE_TIMER
370 m_physX
= m_physY
= wxDefaultCoord
;
374 if ( m_bmpW
&& m_bmpH
)
378 wxInputStream
*s
= input
->GetStream();
382 #if wxUSE_GIF && wxUSE_TIMER
384 if ( m_windowIface
&&
385 (input
->GetLocation().Matches(wxT("*.gif")) ||
386 input
->GetLocation().Matches(wxT("*.GIF"))) )
388 m_gifDecoder
= new wxGIFDecoder();
389 if ( m_gifDecoder
->LoadGIF(*s
) == wxGIF_OK
)
392 if ( m_gifDecoder
->ConvertToImage(0, &img
) )
397 if ( m_gifDecoder
->IsAnimation() )
399 m_gifTimer
= new wxGIFTimer(this);
400 long delay
= m_gifDecoder
->GetDelay(0);
403 m_gifTimer
->Start(delay
, true);
407 wxDELETE(m_gifDecoder
);
412 wxDELETE(m_gifDecoder
);
417 #endif // wxUSE_GIF && wxUSE_TIMER
419 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
425 else // input==NULL, use "broken image" bitmap
427 if ( m_bmpW
== wxDefaultCoord
&& m_bmpH
== wxDefaultCoord
)
435 if ( m_bmpW
== wxDefaultCoord
) m_bmpW
= 31;
436 if ( m_bmpH
== wxDefaultCoord
) m_bmpH
= 33;
439 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
442 //else: ignore the 0-sized images used sometimes on the Web pages
446 void wxHtmlImageCell::SetImage(const wxImage
& img
)
448 #if !defined(__WXMSW__) || wxUSE_WXDIB
455 hh
= img
.GetHeight();
457 if ( m_bmpW
== wxDefaultCoord
)
459 if ( m_bmpH
== wxDefaultCoord
)
462 // Only scale the bitmap at the rendering stage,
463 // so we don't lose quality twice
465 if ((m_bmpW != ww) || (m_bmpH != hh))
467 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
468 m_bitmap = new wxBitmap(img2);
472 m_bitmap
= new wxBitmap(img
);
477 #if wxUSE_GIF && wxUSE_TIMER
478 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
482 // advance current frame
484 if (m_nCurrFrame
== m_gifDecoder
->GetFrameCount())
487 if ( m_physX
== wxDefaultCoord
)
489 m_physX
= m_physY
= 0;
490 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
492 m_physX
+= cell
->GetPosX();
493 m_physY
+= cell
->GetPosY();
497 wxWindow
*win
= m_windowIface
->GetHTMLWindow();
499 m_windowIface
->HTMLCoordsToWindow(this, wxPoint(m_physX
, m_physY
));
500 wxRect
rect(pos
, wxSize(m_Width
, m_Height
));
502 if ( win
->GetClientRect().Intersects(rect
) &&
503 m_gifDecoder
->ConvertToImage(m_nCurrFrame
, &img
) )
505 #if !defined(__WXMSW__) || wxUSE_WXDIB
506 if ( m_gifDecoder
->GetFrameSize(m_nCurrFrame
) != wxSize(m_Width
, m_Height
) ||
507 m_gifDecoder
->GetFramePosition(m_nCurrFrame
) != wxPoint(0, 0) )
511 dc
.SelectObject(*m_bitmap
);
512 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetFramePosition(m_nCurrFrame
),
513 true /* use mask */);
518 win
->Refresh(img
.HasMask(), &rect
);
521 long delay
= m_gifDecoder
->GetDelay(m_nCurrFrame
);
524 timer
->Start(delay
, true);
527 void wxHtmlImageCell::Layout(int w
)
532 m_Width
= w
*m_bmpW
/100;
534 if (!m_bmpHpresent
&& m_bitmap
!= NULL
)
535 m_Height
= m_bitmap
->GetHeight()*m_Width
/m_bitmap
->GetWidth();
537 m_Height
= static_cast<int>(m_scale
*m_bmpH
);
540 m_Width
= static_cast<int>(m_scale
*m_bmpW
);
541 m_Height
= static_cast<int>(m_scale
*m_bmpH
);
546 case wxHTML_ALIGN_TOP
:
547 m_Descent
= m_Height
;
549 case wxHTML_ALIGN_CENTER
:
550 m_Descent
= m_Height
/ 2;
552 case wxHTML_ALIGN_BOTTOM
:
558 wxHtmlCell::Layout(w
);
559 m_physX
= m_physY
= wxDefaultCoord
;
564 wxHtmlImageCell::~wxHtmlImageCell()
567 #if wxUSE_GIF && wxUSE_TIMER
574 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
,
575 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
576 wxHtmlRenderingInfo
& WXUNUSED(info
))
580 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
581 dc
.SetPen(*wxBLACK_PEN
);
582 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
587 // We add in the scaling from the desired bitmap width
588 // and height, so we only do the scaling once.
589 double imageScaleX
= 1.0;
590 double imageScaleY
= 1.0;
591 if (m_Width
!= m_bitmap
->GetWidth())
592 imageScaleX
= (double) m_Width
/ (double) m_bitmap
->GetWidth();
593 if (m_Height
!= m_bitmap
->GetHeight())
594 imageScaleY
= (double) m_Height
/ (double) m_bitmap
->GetHeight();
597 dc
.GetUserScale(&us_x
, &us_y
);
598 dc
.SetUserScale(us_x
* imageScaleX
, us_y
* imageScaleY
);
600 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / (imageScaleX
)),
601 (int) ((y
+ m_PosY
) / (imageScaleY
)), true);
602 dc
.SetUserScale(us_x
, us_y
);
606 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
608 if (m_mapName
.empty())
609 return wxHtmlCell::GetLink( x
, y
);
612 wxHtmlContainerCell
*p
, *op
;
613 op
= p
= GetParent();
620 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
621 (const void*)(&m_mapName
));
624 ((wxString
&)m_mapName
).Clear();
625 return wxHtmlCell::GetLink( x
, y
);
627 { // dirty hack, ask Joel why he fills m_ImageMap in this place
628 // THE problem is that we're in const method and we can't modify m_ImageMap
629 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
630 *cx
= (wxHtmlImageMapCell
*)cell
;
633 return m_imageMap
->GetLink(x
, y
);
638 //--------------------------------------------------------------------------------
640 //--------------------------------------------------------------------------------
642 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
643 TAG_HANDLER_CONSTR(IMG
) { }
645 TAG_HANDLER_PROC(tag
)
647 if (tag
.GetName() == wxT("IMG"))
649 if (tag
.HasParam(wxT("SRC")))
651 int w
= wxDefaultCoord
, h
= wxDefaultCoord
;
652 bool wpercent
= false;
653 bool hpresent
= false;
656 wxString tmp
= tag
.GetParam(wxT("SRC"));
657 wxString mn
= wxEmptyString
;
659 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
661 if (tag
.HasParam(wxT("WIDTH")))
663 wxString param
= tag
.GetParam(wxT("WIDTH"));
664 wxSscanf(param
.c_str(), wxT("%i"), &w
);
665 if (param
.EndsWith(wxT("%"))) {
675 if (tag
.HasParam(wxT("HEIGHT")))
677 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
681 al
= wxHTML_ALIGN_BOTTOM
;
682 if (tag
.HasParam(wxT("ALIGN")))
684 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
685 alstr
.MakeUpper(); // for the case alignment was in ".."
686 if (alstr
== wxT("TEXTTOP"))
687 al
= wxHTML_ALIGN_TOP
;
688 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
689 al
= wxHTML_ALIGN_CENTER
;
691 if (tag
.HasParam(wxT("USEMAP")))
693 mn
= tag
.GetParam( wxT("USEMAP") );
694 if ( !mn
.empty() && *mn
.begin() == '#' )
699 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
700 m_WParser
->GetWindowInterface(),
701 str
, w
, wpercent
, h
, hpresent
,
702 m_WParser
->GetPixelScale(),
704 m_WParser
->ApplyStateToCell(cel
);
705 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
706 m_WParser
->GetContainer()->InsertCell(cel
);
711 if (tag
.GetName() == wxT("MAP"))
713 m_WParser
->CloseContainer();
714 m_WParser
->OpenContainer();
715 if (tag
.HasParam(wxT("NAME")))
717 wxString tmp
= tag
.GetParam(wxT("NAME"));
718 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
719 m_WParser
->GetContainer()->InsertCell( cel
);
722 m_WParser
->CloseContainer();
723 m_WParser
->OpenContainer();
725 if (tag
.GetName() == wxT("AREA"))
727 if (tag
.HasParam(wxT("SHAPE")))
729 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
730 wxString coords
= wxEmptyString
;
732 wxHtmlImageMapAreaCell
*cel
= NULL
;
733 if (tag
.HasParam(wxT("COORDS")))
735 coords
= tag
.GetParam(wxT("COORDS"));
737 if (tmp
== wxT("POLY"))
739 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
741 else if (tmp
== wxT("CIRCLE"))
743 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
745 else if (tmp
== wxT("RECT"))
747 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
749 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
752 if (tag
.HasParam(wxT("TARGET")))
753 target
= tag
.GetParam(wxT("TARGET"));
754 cel
->SetLink(wxHtmlLinkInfo(tag
.GetParam(wxT("HREF")), target
));
757 m_WParser
->GetContainer()->InsertCell( cel
);
768 TAGS_MODULE_BEGIN(Image
)
772 TAGS_MODULE_END(Image
)