]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_image.cpp
1 /////////////////////////////////////////////////////////////////////////////
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"
13 #if wxUSE_HTML && wxUSE_STREAMS
21 #include "wx/scrolwin.h"
23 #include "wx/dcmemory.h"
26 #include "wx/html/forcelnk.h"
27 #include "wx/html/m_templ.h"
28 #include "wx/html/htmlwin.h"
31 #include "wx/gifdecod.h"
32 #include "wx/dynarray.h"
34 #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 DECLARE_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 DECLARE_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(wxWindow
*window
,
290 wxFSFile
*input
, int w
= wxDefaultCoord
, int h
= wxDefaultCoord
,
291 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
292 const wxString
& mapname
= wxEmptyString
);
294 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
295 wxHtmlRenderingInfo
& info
);
296 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
298 void SetImage(const wxImage
& img
);
299 #if wxUSE_GIF && wxUSE_TIMER
300 void AdvanceAnimation(wxTimer
*timer
);
301 virtual void Layout(int w
);
308 wxScrolledWindow
*m_window
;
309 #if wxUSE_GIF && wxUSE_TIMER
310 wxGIFDecoder
*m_gifDecoder
;
312 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(wxWindow
*window
, wxFSFile
*input
,
345 int w
, int h
, double scale
, int align
,
346 const wxString
& mapname
) : wxHtmlCell()
348 m_window
= window
? wxStaticCast(window
, wxScrolledWindow
) : NULL
;
356 SetCanLiveOnPagebreak(false);
357 #if wxUSE_GIF && wxUSE_TIMER
360 m_physX
= m_physY
= wxDefaultCoord
;
363 if ( m_bmpW
&& m_bmpH
)
367 wxInputStream
*s
= input
->GetStream();
371 #if wxUSE_GIF && wxUSE_TIMER
373 if ( (input
->GetLocation().Matches(wxT("*.gif")) ||
374 input
->GetLocation().Matches(wxT("*.GIF"))) && m_window
)
376 m_gifDecoder
= new wxGIFDecoder(s
, true);
377 if ( m_gifDecoder
->ReadGIF() == wxGIF_OK
)
380 if ( m_gifDecoder
->ConvertToImage(&img
) )
385 if ( m_gifDecoder
->IsAnimation() )
387 m_gifTimer
= new wxGIFTimer(this);
388 m_gifTimer
->Start(m_gifDecoder
->GetDelay(), true);
392 wxDELETE(m_gifDecoder
);
397 wxDELETE(m_gifDecoder
);
402 #endif // wxUSE_GIF && wxUSE_TIMER
404 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
410 else // input==NULL, use "broken image" bitmap
412 if ( m_bmpW
== wxDefaultCoord
&& m_bmpH
== wxDefaultCoord
)
420 if ( m_bmpW
== wxDefaultCoord
) m_bmpW
= 31;
421 if ( m_bmpH
== wxDefaultCoord
) m_bmpH
= 33;
424 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
427 //else: ignore the 0-sized images used sometimes on the Web pages
429 m_Width
= (int)(scale
* (double)m_bmpW
);
430 m_Height
= (int)(scale
* (double)m_bmpH
);
434 case wxHTML_ALIGN_TOP
:
435 m_Descent
= m_Height
;
437 case wxHTML_ALIGN_CENTER
:
438 m_Descent
= m_Height
/ 2;
440 case wxHTML_ALIGN_BOTTOM
:
447 void wxHtmlImageCell::SetImage(const wxImage
& img
)
449 #if !defined(__WXMSW__) || wxUSE_WXDIB
456 hh
= img
.GetHeight();
458 if ( m_bmpW
== wxDefaultCoord
)
460 if ( m_bmpH
== wxDefaultCoord
)
463 // Only scale the bitmap at the rendering stage,
464 // so we don't lose quality twice
466 if ((m_bmpW != ww) || (m_bmpH != hh))
468 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
469 m_bitmap = new wxBitmap(img2);
473 m_bitmap
= new wxBitmap(img
);
478 #if wxUSE_GIF && wxUSE_TIMER
479 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
483 m_gifDecoder
->GoNextFrame(true);
485 if ( m_physX
== wxDefaultCoord
)
487 m_physX
= m_physY
= 0;
488 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
490 m_physX
+= cell
->GetPosX();
491 m_physY
+= cell
->GetPosY();
496 m_window
->CalcScrolledPosition(m_physX
, m_physY
, &x
, &y
);
497 wxRect
rect(x
, y
, m_Width
, m_Height
);
499 if ( m_window
->GetClientRect().Intersects(rect
) &&
500 m_gifDecoder
->ConvertToImage(&img
) )
502 #if !defined(__WXMSW__) || wxUSE_WXDIB
503 if ( (int)m_gifDecoder
->GetWidth() != m_Width
||
504 (int)m_gifDecoder
->GetHeight() != m_Height
||
505 m_gifDecoder
->GetLeft() != 0 || m_gifDecoder
->GetTop() != 0 )
509 dc
.SelectObject(*m_bitmap
);
510 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetLeft(), m_gifDecoder
->GetTop(),
511 true /* use mask */);
516 m_window
->Refresh(img
.HasMask(), &rect
);
519 timer
->Start(m_gifDecoder
->GetDelay(), true);
522 void wxHtmlImageCell::Layout(int w
)
524 wxHtmlCell::Layout(w
);
525 m_physX
= m_physY
= wxDefaultCoord
;
530 wxHtmlImageCell::~wxHtmlImageCell()
533 #if wxUSE_GIF && wxUSE_TIMER
540 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
,
541 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
542 wxHtmlRenderingInfo
& WXUNUSED(info
))
546 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
547 dc
.SetPen(*wxBLACK_PEN
);
548 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
553 // We add in the scaling from the desired bitmap width
554 // and height, so we only do the scaling once.
555 double imageScaleX
= 1.0;
556 double imageScaleY
= 1.0;
557 if (m_bmpW
!= m_bitmap
->GetWidth())
558 imageScaleX
= (double) m_bmpW
/ (double) m_bitmap
->GetWidth();
559 if (m_bmpH
!= m_bitmap
->GetHeight())
560 imageScaleY
= (double) m_bmpH
/ (double) m_bitmap
->GetHeight();
563 dc
.GetUserScale(&us_x
, &us_y
);
564 dc
.SetUserScale(us_x
* m_scale
* imageScaleX
, us_y
* m_scale
* imageScaleY
);
566 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / (m_scale
*imageScaleX
)),
567 (int) ((y
+ m_PosY
) / (m_scale
*imageScaleY
)), true);
568 dc
.SetUserScale(us_x
, us_y
);
572 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
574 if (m_mapName
.empty())
575 return wxHtmlCell::GetLink( x
, y
);
578 wxHtmlContainerCell
*p
, *op
;
579 op
= p
= GetParent();
586 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
587 (const void*)(&m_mapName
));
590 ((wxString
&)m_mapName
).Clear();
591 return wxHtmlCell::GetLink( x
, y
);
593 { // dirty hack, ask Joel why he fills m_ImageMap in this place
594 // THE problem is that we're in const method and we can't modify m_ImageMap
595 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
596 *cx
= (wxHtmlImageMapCell
*)cell
;
599 return m_imageMap
->GetLink(x
, y
);
604 //--------------------------------------------------------------------------------
606 //--------------------------------------------------------------------------------
608 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
609 TAG_HANDLER_CONSTR(IMG
) { }
611 TAG_HANDLER_PROC(tag
)
613 if (tag
.GetName() == wxT("IMG"))
615 if (tag
.HasParam(wxT("SRC")))
617 int w
= wxDefaultCoord
, h
= wxDefaultCoord
;
620 wxString tmp
= tag
.GetParam(wxT("SRC"));
621 wxString mn
= wxEmptyString
;
623 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
625 if (tag
.HasParam(wxT("WIDTH")))
626 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
627 if (tag
.HasParam(wxT("HEIGHT")))
628 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
629 al
= wxHTML_ALIGN_BOTTOM
;
630 if (tag
.HasParam(wxT("ALIGN")))
632 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
633 alstr
.MakeUpper(); // for the case alignment was in ".."
634 if (alstr
== wxT("TEXTTOP"))
635 al
= wxHTML_ALIGN_TOP
;
636 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
637 al
= wxHTML_ALIGN_CENTER
;
639 if (tag
.HasParam(wxT("USEMAP")))
641 mn
= tag
.GetParam( wxT("USEMAP") );
642 if (mn
.GetChar(0) == wxT('#'))
647 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
648 m_WParser
->GetWindow(),
650 m_WParser
->GetPixelScale(),
652 m_WParser
->ApplyStateToCell(cel
);
653 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
654 m_WParser
->GetContainer()->InsertCell(cel
);
659 if (tag
.GetName() == wxT("MAP"))
661 m_WParser
->CloseContainer();
662 m_WParser
->OpenContainer();
663 if (tag
.HasParam(wxT("NAME")))
665 wxString tmp
= tag
.GetParam(wxT("NAME"));
666 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
667 m_WParser
->GetContainer()->InsertCell( cel
);
670 m_WParser
->CloseContainer();
671 m_WParser
->OpenContainer();
673 if (tag
.GetName() == wxT("AREA"))
675 if (tag
.HasParam(wxT("SHAPE")))
677 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
678 wxString coords
= wxEmptyString
;
680 wxHtmlImageMapAreaCell
*cel
= NULL
;
681 if (tag
.HasParam(wxT("COORDS")))
683 coords
= tag
.GetParam(wxT("COORDS"));
685 if (tmp
== wxT("POLY"))
687 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
689 else if (tmp
== wxT("CIRCLE"))
691 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
693 else if (tmp
== wxT("RECT"))
695 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
697 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
700 if (tag
.HasParam(wxT("TARGET")))
701 target
= tag
.GetParam(wxT("TARGET"));
702 cel
->SetLink(wxHtmlLinkInfo(tag
.GetParam(wxT("HREF")), target
));
705 m_WParser
->GetContainer()->InsertCell( cel
);
716 TAGS_MODULE_BEGIN(Image
)
720 TAGS_MODULE_END(Image
)