]>
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"
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 DECLARE_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
104 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
118 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
127 if (coords
.GetCount() >= 6)
132 int totalv
= coords
.GetCount() / 2;
133 int totalc
= totalv
* 2;
134 int xval
= coords
[totalc
- 2];
135 int yval
= coords
[totalc
- 1];
139 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
141 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
143 intersects
+= (xval
>= wherex
) ? 1 : 0;
147 intersects
+= ((xval
- (yval
- wherey
) *
149 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
153 while (pointer
< end
)
155 yval
= coords
[pointer
];
159 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
167 if ((coords
[pointer
- 3] >= wherex
) ==
168 (coords
[pointer
- 1] >= wherex
)) {
169 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
174 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
175 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
176 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
181 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
189 if ((coords
[pointer
- 3] >= wherex
) ==
190 (coords
[pointer
- 1] >= wherex
))
192 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
197 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
198 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
199 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
203 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 DECLARE_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
,
289 wxFSFile
*input
, int w
= wxDefaultCoord
, int h
= wxDefaultCoord
,
290 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
291 const wxString
& mapname
= wxEmptyString
);
292 virtual ~wxHtmlImageCell();
293 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
294 wxHtmlRenderingInfo
& info
);
295 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
297 void SetImage(const wxImage
& img
);
298 #if wxUSE_GIF && wxUSE_TIMER
299 void AdvanceAnimation(wxTimer
*timer
);
300 virtual void Layout(int w
);
307 wxHtmlWindowInterface
*m_windowIface
;
308 #if wxUSE_GIF && wxUSE_TIMER
309 wxGIFDecoder
*m_gifDecoder
;
311 int m_physX
, m_physY
;
315 wxHtmlImageMapCell
*m_imageMap
;
318 DECLARE_NO_COPY_CLASS(wxHtmlImageCell
)
321 #if wxUSE_GIF && wxUSE_TIMER
322 class wxGIFTimer
: public wxTimer
325 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
326 virtual void Notify()
328 m_cell
->AdvanceAnimation(this);
332 wxHtmlImageCell
*m_cell
;
334 DECLARE_NO_COPY_CLASS(wxGIFTimer
)
339 //----------------------------------------------------------------------------
341 //----------------------------------------------------------------------------
344 wxHtmlImageCell::wxHtmlImageCell(wxHtmlWindowInterface
*windowIface
,
346 int w
, int h
, double scale
, int align
,
347 const wxString
& mapname
) : wxHtmlCell()
349 m_windowIface
= windowIface
;
357 SetCanLiveOnPagebreak(false);
358 #if wxUSE_GIF && wxUSE_TIMER
361 m_physX
= m_physY
= wxDefaultCoord
;
365 if ( m_bmpW
&& m_bmpH
)
369 wxInputStream
*s
= input
->GetStream();
373 #if wxUSE_GIF && wxUSE_TIMER
375 if ( m_windowIface
&&
376 (input
->GetLocation().Matches(wxT("*.gif")) ||
377 input
->GetLocation().Matches(wxT("*.GIF"))) )
379 m_gifDecoder
= new wxGIFDecoder();
380 if ( m_gifDecoder
->LoadGIF(*s
) == wxGIF_OK
)
383 if ( m_gifDecoder
->ConvertToImage(0, &img
) )
388 if ( m_gifDecoder
->IsAnimation() )
390 m_gifTimer
= new wxGIFTimer(this);
391 m_gifTimer
->Start(m_gifDecoder
->GetDelay(0), true);
395 wxDELETE(m_gifDecoder
);
400 wxDELETE(m_gifDecoder
);
405 #endif // wxUSE_GIF && wxUSE_TIMER
407 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
413 else // input==NULL, use "broken image" bitmap
415 if ( m_bmpW
== wxDefaultCoord
&& m_bmpH
== wxDefaultCoord
)
423 if ( m_bmpW
== wxDefaultCoord
) m_bmpW
= 31;
424 if ( m_bmpH
== wxDefaultCoord
) m_bmpH
= 33;
427 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
430 //else: ignore the 0-sized images used sometimes on the Web pages
432 m_Width
= (int)(scale
* (double)m_bmpW
);
433 m_Height
= (int)(scale
* (double)m_bmpH
);
437 case wxHTML_ALIGN_TOP
:
438 m_Descent
= m_Height
;
440 case wxHTML_ALIGN_CENTER
:
441 m_Descent
= m_Height
/ 2;
443 case wxHTML_ALIGN_BOTTOM
:
450 void wxHtmlImageCell::SetImage(const wxImage
& img
)
452 #if !defined(__WXMSW__) || wxUSE_WXDIB
459 hh
= img
.GetHeight();
461 if ( m_bmpW
== wxDefaultCoord
)
463 if ( m_bmpH
== wxDefaultCoord
)
466 // Only scale the bitmap at the rendering stage,
467 // so we don't lose quality twice
469 if ((m_bmpW != ww) || (m_bmpH != hh))
471 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
472 m_bitmap = new wxBitmap(img2);
476 m_bitmap
= new wxBitmap(img
);
481 #if wxUSE_GIF && wxUSE_TIMER
482 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
486 // advance current frame
488 if (m_nCurrFrame
== m_gifDecoder
->GetFrameCount())
491 if ( m_physX
== wxDefaultCoord
)
493 m_physX
= m_physY
= 0;
494 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
496 m_physX
+= cell
->GetPosX();
497 m_physY
+= cell
->GetPosY();
501 wxWindow
*win
= m_windowIface
->GetHTMLWindow();
503 m_windowIface
->HTMLCoordsToWindow(this, wxPoint(m_physX
, m_physY
));
504 wxRect
rect(pos
, wxSize(m_Width
, m_Height
));
506 if ( win
->GetClientRect().Intersects(rect
) &&
507 m_gifDecoder
->ConvertToImage(m_nCurrFrame
, &img
) )
509 #if !defined(__WXMSW__) || wxUSE_WXDIB
510 if ( m_gifDecoder
->GetFrameSize(m_nCurrFrame
) != wxSize(m_Width
, m_Height
) ||
511 m_gifDecoder
->GetFramePosition(m_nCurrFrame
) != wxPoint(0, 0) )
515 dc
.SelectObject(*m_bitmap
);
516 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetFramePosition(m_nCurrFrame
),
517 true /* use mask */);
522 win
->Refresh(img
.HasMask(), &rect
);
525 timer
->Start(m_gifDecoder
->GetDelay(m_nCurrFrame
), true);
528 void wxHtmlImageCell::Layout(int w
)
530 wxHtmlCell::Layout(w
);
531 m_physX
= m_physY
= wxDefaultCoord
;
536 wxHtmlImageCell::~wxHtmlImageCell()
539 #if wxUSE_GIF && wxUSE_TIMER
546 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
,
547 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
548 wxHtmlRenderingInfo
& WXUNUSED(info
))
552 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
553 dc
.SetPen(*wxBLACK_PEN
);
554 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
559 // We add in the scaling from the desired bitmap width
560 // and height, so we only do the scaling once.
561 double imageScaleX
= 1.0;
562 double imageScaleY
= 1.0;
563 if (m_bmpW
!= m_bitmap
->GetWidth())
564 imageScaleX
= (double) m_bmpW
/ (double) m_bitmap
->GetWidth();
565 if (m_bmpH
!= m_bitmap
->GetHeight())
566 imageScaleY
= (double) m_bmpH
/ (double) m_bitmap
->GetHeight();
569 dc
.GetUserScale(&us_x
, &us_y
);
570 dc
.SetUserScale(us_x
* m_scale
* imageScaleX
, us_y
* m_scale
* imageScaleY
);
572 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / (m_scale
*imageScaleX
)),
573 (int) ((y
+ m_PosY
) / (m_scale
*imageScaleY
)), true);
574 dc
.SetUserScale(us_x
, us_y
);
578 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
580 if (m_mapName
.empty())
581 return wxHtmlCell::GetLink( x
, y
);
584 wxHtmlContainerCell
*p
, *op
;
585 op
= p
= GetParent();
592 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
593 (const void*)(&m_mapName
));
596 ((wxString
&)m_mapName
).Clear();
597 return wxHtmlCell::GetLink( x
, y
);
599 { // dirty hack, ask Joel why he fills m_ImageMap in this place
600 // THE problem is that we're in const method and we can't modify m_ImageMap
601 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
602 *cx
= (wxHtmlImageMapCell
*)cell
;
605 return m_imageMap
->GetLink(x
, y
);
610 //--------------------------------------------------------------------------------
612 //--------------------------------------------------------------------------------
614 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
615 TAG_HANDLER_CONSTR(IMG
) { }
617 TAG_HANDLER_PROC(tag
)
619 if (tag
.GetName() == wxT("IMG"))
621 if (tag
.HasParam(wxT("SRC")))
623 int w
= wxDefaultCoord
, h
= wxDefaultCoord
;
626 wxString tmp
= tag
.GetParam(wxT("SRC"));
627 wxString mn
= wxEmptyString
;
629 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
631 if (tag
.HasParam(wxT("WIDTH")))
632 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
633 if (tag
.HasParam(wxT("HEIGHT")))
634 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
635 al
= wxHTML_ALIGN_BOTTOM
;
636 if (tag
.HasParam(wxT("ALIGN")))
638 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
639 alstr
.MakeUpper(); // for the case alignment was in ".."
640 if (alstr
== wxT("TEXTTOP"))
641 al
= wxHTML_ALIGN_TOP
;
642 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
643 al
= wxHTML_ALIGN_CENTER
;
645 if (tag
.HasParam(wxT("USEMAP")))
647 mn
= tag
.GetParam( wxT("USEMAP") );
648 if (mn
.GetChar(0) == wxT('#'))
653 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
654 m_WParser
->GetWindowInterface(),
656 m_WParser
->GetPixelScale(),
658 m_WParser
->ApplyStateToCell(cel
);
659 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
660 m_WParser
->GetContainer()->InsertCell(cel
);
665 if (tag
.GetName() == wxT("MAP"))
667 m_WParser
->CloseContainer();
668 m_WParser
->OpenContainer();
669 if (tag
.HasParam(wxT("NAME")))
671 wxString tmp
= tag
.GetParam(wxT("NAME"));
672 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
673 m_WParser
->GetContainer()->InsertCell( cel
);
676 m_WParser
->CloseContainer();
677 m_WParser
->OpenContainer();
679 if (tag
.GetName() == wxT("AREA"))
681 if (tag
.HasParam(wxT("SHAPE")))
683 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
684 wxString coords
= wxEmptyString
;
686 wxHtmlImageMapAreaCell
*cel
= NULL
;
687 if (tag
.HasParam(wxT("COORDS")))
689 coords
= tag
.GetParam(wxT("COORDS"));
691 if (tmp
== wxT("POLY"))
693 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
695 else if (tmp
== wxT("CIRCLE"))
697 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
699 else if (tmp
== wxT("RECT"))
701 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
703 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
706 if (tag
.HasParam(wxT("TARGET")))
707 target
= tag
.GetParam(wxT("TARGET"));
708 cel
->SetLink(wxHtmlLinkInfo(tag
.GetParam(wxT("HREF")), target
));
711 m_WParser
->GetContainer()->InsertCell( cel
);
722 TAGS_MODULE_BEGIN(Image
)
726 TAGS_MODULE_END(Image
)