]>
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 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
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 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
,
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 wxDECLARE_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 wxDECLARE_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 long delay
= m_gifDecoder
->GetDelay(0);
394 m_gifTimer
->Start(delay
, true);
398 wxDELETE(m_gifDecoder
);
403 wxDELETE(m_gifDecoder
);
408 #endif // wxUSE_GIF && wxUSE_TIMER
410 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
416 else // input==NULL, use "broken image" bitmap
418 if ( m_bmpW
== wxDefaultCoord
&& m_bmpH
== wxDefaultCoord
)
426 if ( m_bmpW
== wxDefaultCoord
) m_bmpW
= 31;
427 if ( m_bmpH
== wxDefaultCoord
) m_bmpH
= 33;
430 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
433 //else: ignore the 0-sized images used sometimes on the Web pages
435 m_Width
= (int)(scale
* (double)m_bmpW
);
436 m_Height
= (int)(scale
* (double)m_bmpH
);
440 case wxHTML_ALIGN_TOP
:
441 m_Descent
= m_Height
;
443 case wxHTML_ALIGN_CENTER
:
444 m_Descent
= m_Height
/ 2;
446 case wxHTML_ALIGN_BOTTOM
:
453 void wxHtmlImageCell::SetImage(const wxImage
& img
)
455 #if !defined(__WXMSW__) || wxUSE_WXDIB
462 hh
= img
.GetHeight();
464 if ( m_bmpW
== wxDefaultCoord
)
466 if ( m_bmpH
== wxDefaultCoord
)
469 // Only scale the bitmap at the rendering stage,
470 // so we don't lose quality twice
472 if ((m_bmpW != ww) || (m_bmpH != hh))
474 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
475 m_bitmap = new wxBitmap(img2);
479 m_bitmap
= new wxBitmap(img
);
484 #if wxUSE_GIF && wxUSE_TIMER
485 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
489 // advance current frame
491 if (m_nCurrFrame
== m_gifDecoder
->GetFrameCount())
494 if ( m_physX
== wxDefaultCoord
)
496 m_physX
= m_physY
= 0;
497 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
499 m_physX
+= cell
->GetPosX();
500 m_physY
+= cell
->GetPosY();
504 wxWindow
*win
= m_windowIface
->GetHTMLWindow();
506 m_windowIface
->HTMLCoordsToWindow(this, wxPoint(m_physX
, m_physY
));
507 wxRect
rect(pos
, wxSize(m_Width
, m_Height
));
509 if ( win
->GetClientRect().Intersects(rect
) &&
510 m_gifDecoder
->ConvertToImage(m_nCurrFrame
, &img
) )
512 #if !defined(__WXMSW__) || wxUSE_WXDIB
513 if ( m_gifDecoder
->GetFrameSize(m_nCurrFrame
) != wxSize(m_Width
, m_Height
) ||
514 m_gifDecoder
->GetFramePosition(m_nCurrFrame
) != wxPoint(0, 0) )
518 dc
.SelectObject(*m_bitmap
);
519 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetFramePosition(m_nCurrFrame
),
520 true /* use mask */);
525 win
->Refresh(img
.HasMask(), &rect
);
528 long delay
= m_gifDecoder
->GetDelay(m_nCurrFrame
);
531 timer
->Start(delay
, true);
534 void wxHtmlImageCell::Layout(int w
)
536 wxHtmlCell::Layout(w
);
537 m_physX
= m_physY
= wxDefaultCoord
;
542 wxHtmlImageCell::~wxHtmlImageCell()
545 #if wxUSE_GIF && wxUSE_TIMER
552 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
,
553 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
554 wxHtmlRenderingInfo
& WXUNUSED(info
))
558 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
559 dc
.SetPen(*wxBLACK_PEN
);
560 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
565 // We add in the scaling from the desired bitmap width
566 // and height, so we only do the scaling once.
567 double imageScaleX
= 1.0;
568 double imageScaleY
= 1.0;
569 if (m_bmpW
!= m_bitmap
->GetWidth())
570 imageScaleX
= (double) m_bmpW
/ (double) m_bitmap
->GetWidth();
571 if (m_bmpH
!= m_bitmap
->GetHeight())
572 imageScaleY
= (double) m_bmpH
/ (double) m_bitmap
->GetHeight();
575 dc
.GetUserScale(&us_x
, &us_y
);
576 dc
.SetUserScale(us_x
* m_scale
* imageScaleX
, us_y
* m_scale
* imageScaleY
);
578 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / (m_scale
*imageScaleX
)),
579 (int) ((y
+ m_PosY
) / (m_scale
*imageScaleY
)), true);
580 dc
.SetUserScale(us_x
, us_y
);
584 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
586 if (m_mapName
.empty())
587 return wxHtmlCell::GetLink( x
, y
);
590 wxHtmlContainerCell
*p
, *op
;
591 op
= p
= GetParent();
598 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
599 (const void*)(&m_mapName
));
602 ((wxString
&)m_mapName
).Clear();
603 return wxHtmlCell::GetLink( x
, y
);
605 { // dirty hack, ask Joel why he fills m_ImageMap in this place
606 // THE problem is that we're in const method and we can't modify m_ImageMap
607 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
608 *cx
= (wxHtmlImageMapCell
*)cell
;
611 return m_imageMap
->GetLink(x
, y
);
616 //--------------------------------------------------------------------------------
618 //--------------------------------------------------------------------------------
620 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
621 TAG_HANDLER_CONSTR(IMG
) { }
623 TAG_HANDLER_PROC(tag
)
625 if (tag
.GetName() == wxT("IMG"))
627 if (tag
.HasParam(wxT("SRC")))
629 int w
= wxDefaultCoord
, h
= wxDefaultCoord
;
632 wxString tmp
= tag
.GetParam(wxT("SRC"));
633 wxString mn
= wxEmptyString
;
635 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
637 if (tag
.HasParam(wxT("WIDTH")))
638 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
639 if (tag
.HasParam(wxT("HEIGHT")))
640 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
641 al
= wxHTML_ALIGN_BOTTOM
;
642 if (tag
.HasParam(wxT("ALIGN")))
644 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
645 alstr
.MakeUpper(); // for the case alignment was in ".."
646 if (alstr
== wxT("TEXTTOP"))
647 al
= wxHTML_ALIGN_TOP
;
648 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
649 al
= wxHTML_ALIGN_CENTER
;
651 if (tag
.HasParam(wxT("USEMAP")))
653 mn
= tag
.GetParam( wxT("USEMAP") );
654 if ( !mn
.empty() && *mn
.begin() == '#' )
659 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
660 m_WParser
->GetWindowInterface(),
662 m_WParser
->GetPixelScale(),
664 m_WParser
->ApplyStateToCell(cel
);
665 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
666 m_WParser
->GetContainer()->InsertCell(cel
);
671 if (tag
.GetName() == wxT("MAP"))
673 m_WParser
->CloseContainer();
674 m_WParser
->OpenContainer();
675 if (tag
.HasParam(wxT("NAME")))
677 wxString tmp
= tag
.GetParam(wxT("NAME"));
678 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
679 m_WParser
->GetContainer()->InsertCell( cel
);
682 m_WParser
->CloseContainer();
683 m_WParser
->OpenContainer();
685 if (tag
.GetName() == wxT("AREA"))
687 if (tag
.HasParam(wxT("SHAPE")))
689 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
690 wxString coords
= wxEmptyString
;
692 wxHtmlImageMapAreaCell
*cel
= NULL
;
693 if (tag
.HasParam(wxT("COORDS")))
695 coords
= tag
.GetParam(wxT("COORDS"));
697 if (tmp
== wxT("POLY"))
699 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
701 else if (tmp
== wxT("CIRCLE"))
703 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
705 else if (tmp
== wxT("RECT"))
707 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
709 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
712 if (tag
.HasParam(wxT("TARGET")))
713 target
= tag
.GetParam(wxT("TARGET"));
714 cel
->SetLink(wxHtmlLinkInfo(tag
.GetParam(wxT("HREF")), target
));
717 m_WParser
->GetContainer()->InsertCell( cel
);
728 TAGS_MODULE_BEGIN(Image
)
732 TAGS_MODULE_END(Image
)