]>
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 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include "wx/wxprec.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
25 #include "wx/scrolwin.h"
27 #include "wx/dcmemory.h"
30 #include "wx/html/forcelnk.h"
31 #include "wx/html/m_templ.h"
32 #include "wx/html/htmlwin.h"
35 #include "wx/gifdecod.h"
36 #include "wx/dynarray.h"
38 #include "wx/artprov.h"
43 FORCE_LINK_ME(m_image
)
48 WX_DECLARE_OBJARRAY(int, CoordArray
);
49 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
50 WX_DEFINE_OBJARRAY(CoordArray
);
53 //--------------------------------------------------------------------------------
54 // wxHtmlImageMapAreaCell
55 // 0-width, 0-height cell that represents single area in imagemap
56 // (it's GetLink is called from wxHtmlImageCell's)
57 //--------------------------------------------------------------------------------
59 class wxHtmlImageMapAreaCell
: public wxHtmlCell
62 enum celltype
{ CIRCLE
, RECT
, POLY
};
68 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
69 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
76 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
79 wxString x
= incoords
, y
;
82 while ((i
= x
.Find( ',' )) != -1)
84 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
87 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
90 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
102 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
116 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
125 if (coords
.GetCount() >= 6)
130 int totalv
= coords
.GetCount() / 2;
131 int totalc
= totalv
* 2;
132 int xval
= coords
[totalc
- 2];
133 int yval
= coords
[totalc
- 1];
137 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
139 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
141 intersects
+= (xval
>= wherex
) ? 1 : 0;
145 intersects
+= ((xval
- (yval
- wherey
) *
147 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
151 while (pointer
< end
)
153 yval
= coords
[pointer
];
157 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
165 if ((coords
[pointer
- 3] >= wherex
) ==
166 (coords
[pointer
- 1] >= wherex
)) {
167 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
172 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
173 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
174 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
179 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
187 if ((coords
[pointer
- 3] >= wherex
) ==
188 (coords
[pointer
- 1] >= wherex
))
190 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
195 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
196 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
197 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
201 if ((intersects
& 1) != 0)
212 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
213 return a
->GetLink( x
, y
);
225 //--------------------------------------------------------------------------------
226 // wxHtmlImageMapCell
227 // 0-width, 0-height cell that represents map from imagemaps
228 // it is always placed before wxHtmlImageMapAreaCells
229 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
230 //--------------------------------------------------------------------------------
233 class wxHtmlImageMapCell
: public wxHtmlCell
236 wxHtmlImageMapCell( wxString
&name
);
240 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
241 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
245 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
250 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
252 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
254 return a
->GetLink( x
, y
);
255 return wxHtmlCell::GetLink( x
, y
);
258 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
260 if (cond
== wxHTML_COND_ISIMAGEMAP
)
262 if (m_Name
== *((wxString
*)(param
)))
265 return wxHtmlCell::Find(cond
, param
);
272 //--------------------------------------------------------------------------------
275 //--------------------------------------------------------------------------------
277 class wxHtmlImageCell
: public wxHtmlCell
280 wxHtmlImageCell(wxWindow
*window
,
281 wxFSFile
*input
, int w
= -1, int h
= -1,
282 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
283 const wxString
& mapname
= wxEmptyString
);
285 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
286 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
288 void SetImage(const wxImage
& img
);
289 #if wxUSE_GIF && wxUSE_TIMER
290 void AdvanceAnimation(wxTimer
*timer
);
291 virtual void Layout(int w
);
298 wxScrolledWindow
*m_window
;
299 #if wxUSE_GIF && wxUSE_TIMER
300 wxGIFDecoder
*m_gifDecoder
;
302 int m_physX
, m_physY
;
305 wxHtmlImageMapCell
*m_imageMap
;
308 DECLARE_NO_COPY_CLASS(wxHtmlImageCell
)
311 #if wxUSE_GIF && wxUSE_TIMER
312 class wxGIFTimer
: public wxTimer
315 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
316 virtual void Notify()
318 m_cell
->AdvanceAnimation(this);
322 wxHtmlImageCell
*m_cell
;
324 DECLARE_NO_COPY_CLASS(wxGIFTimer
)
329 //----------------------------------------------------------------------------
331 //----------------------------------------------------------------------------
334 wxHtmlImageCell::wxHtmlImageCell(wxWindow
*window
, wxFSFile
*input
,
335 int w
, int h
, double scale
, int align
,
336 const wxString
& mapname
) : wxHtmlCell()
338 m_window
= window
? wxStaticCast(window
, wxScrolledWindow
) : NULL
;
346 SetCanLiveOnPagebreak(FALSE
);
347 #if wxUSE_GIF && wxUSE_TIMER
350 m_physX
= m_physY
= -1;
353 if ( m_bmpW
&& m_bmpH
)
357 wxInputStream
*s
= input
->GetStream();
363 #if wxUSE_GIF && wxUSE_TIMER
364 if ( (input
->GetLocation().Matches(wxT("*.gif")) ||
365 input
->GetLocation().Matches(wxT("*.GIF"))) && m_window
)
367 m_gifDecoder
= new wxGIFDecoder(s
, TRUE
);
368 if ( m_gifDecoder
->ReadGIF() == wxGIF_OK
)
371 if ( m_gifDecoder
->ConvertToImage(&img
) )
376 if ( m_gifDecoder
->IsAnimation() )
378 m_gifTimer
= new wxGIFTimer(this);
379 m_gifTimer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
383 wxDELETE(m_gifDecoder
);
388 wxDELETE(m_gifDecoder
);
393 #endif // wxUSE_GIF && wxUSE_TIMER
395 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
401 else // input==NULL, use "broken image" bitmap
403 if ( m_bmpW
== -1 && m_bmpH
== -1 )
411 if ( m_bmpW
== -1 ) m_bmpW
= 31;
412 if ( m_bmpH
== -1 ) m_bmpH
= 33;
415 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
));
418 //else: ignore the 0-sized images used sometimes on the Web pages
420 m_Width
= (int)(scale
* (double)m_bmpW
);
421 m_Height
= (int)(scale
* (double)m_bmpH
);
425 case wxHTML_ALIGN_TOP
:
426 m_Descent
= m_Height
;
428 case wxHTML_ALIGN_CENTER
:
429 m_Descent
= m_Height
/ 2;
431 case wxHTML_ALIGN_BOTTOM
:
438 void wxHtmlImageCell::SetImage(const wxImage
& img
)
446 hh
= img
.GetHeight();
453 if ((m_bmpW
!= ww
) || (m_bmpH
!= hh
))
455 wxImage img2
= img
.Scale(m_bmpW
, m_bmpH
);
456 m_bitmap
= new wxBitmap(img2
);
459 m_bitmap
= new wxBitmap(img
);
463 #if wxUSE_GIF && wxUSE_TIMER
464 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
468 m_gifDecoder
->GoNextFrame(TRUE
);
472 m_physX
= m_physY
= 0;
473 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
475 m_physX
+= cell
->GetPosX();
476 m_physY
+= cell
->GetPosY();
481 m_window
->CalcScrolledPosition(m_physX
, m_physY
, &x
, &y
);
482 wxRect
rect(x
, y
, m_Width
, m_Height
);
484 if ( m_window
->GetClientRect().Intersects(rect
) &&
485 m_gifDecoder
->ConvertToImage(&img
) )
487 if ( (int)m_gifDecoder
->GetWidth() != m_Width
||
488 (int)m_gifDecoder
->GetHeight() != m_Height
||
489 m_gifDecoder
->GetLeft() != 0 || m_gifDecoder
->GetTop() != 0 )
493 dc
.SelectObject(*m_bitmap
);
494 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetLeft(), m_gifDecoder
->GetTop());
498 m_window
->Refresh(img
.HasMask(), &rect
);
501 timer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
504 void wxHtmlImageCell::Layout(int w
)
506 wxHtmlCell::Layout(w
);
507 m_physX
= m_physY
= -1;
512 wxHtmlImageCell::~wxHtmlImageCell()
515 #if wxUSE_GIF && wxUSE_TIMER
522 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
526 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
527 dc
.SetPen(*wxBLACK_PEN
);
528 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
534 dc
.GetUserScale(&us_x
, &us_y
);
535 dc
.SetUserScale(us_x
* m_scale
, us_y
* m_scale
);
537 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / m_scale
),
538 (int) ((y
+ m_PosY
) / m_scale
), TRUE
);
539 dc
.SetUserScale(us_x
, us_y
);
543 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
545 if (m_mapName
.IsEmpty())
546 return wxHtmlCell::GetLink( x
, y
);
549 wxHtmlContainerCell
*p
, *op
;
550 op
= p
= GetParent();
557 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
558 (const void*)(&m_mapName
));
561 ((wxString
&)m_mapName
).Clear();
562 return wxHtmlCell::GetLink( x
, y
);
564 { // dirty hack, ask Joel why he fills m_ImageMap in this place
565 // THE problem is that we're in const method and we can't modify m_ImageMap
566 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
567 *cx
= (wxHtmlImageMapCell
*)cell
;
570 return m_imageMap
->GetLink(x
, y
);
575 //--------------------------------------------------------------------------------
577 //--------------------------------------------------------------------------------
579 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
581 TAG_HANDLER_PROC(tag
)
583 if (tag
.GetName() == wxT("IMG"))
585 if (tag
.HasParam(wxT("SRC")))
590 wxString tmp
= tag
.GetParam(wxT("SRC"));
591 wxString mn
= wxEmptyString
;
593 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
595 if (tag
.HasParam(wxT("WIDTH")))
596 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
597 if (tag
.HasParam(wxT("HEIGHT")))
598 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
599 al
= wxHTML_ALIGN_BOTTOM
;
600 if (tag
.HasParam(wxT("ALIGN")))
602 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
603 alstr
.MakeUpper(); // for the case alignment was in ".."
604 if (alstr
== wxT("TEXTTOP"))
605 al
= wxHTML_ALIGN_TOP
;
606 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
607 al
= wxHTML_ALIGN_CENTER
;
609 if (tag
.HasParam(wxT("USEMAP")))
611 mn
= tag
.GetParam( wxT("USEMAP") );
612 if (mn
.GetChar(0) == wxT('#'))
617 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
618 m_WParser
->GetWindow(),
620 m_WParser
->GetPixelScale(),
622 cel
->SetLink(m_WParser
->GetLink());
623 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
624 m_WParser
->GetContainer()->InsertCell(cel
);
629 if (tag
.GetName() == wxT("MAP"))
631 m_WParser
->CloseContainer();
632 m_WParser
->OpenContainer();
633 if (tag
.HasParam(wxT("NAME")))
635 wxString tmp
= tag
.GetParam(wxT("NAME"));
636 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
637 m_WParser
->GetContainer()->InsertCell( cel
);
640 m_WParser
->CloseContainer();
641 m_WParser
->OpenContainer();
643 if (tag
.GetName() == wxT("AREA"))
645 if (tag
.HasParam(wxT("SHAPE")))
647 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
648 wxString coords
= wxEmptyString
;
650 wxHtmlImageMapAreaCell
*cel
= NULL
;
651 if (tag
.HasParam(wxT("COORDS")))
653 coords
= tag
.GetParam(wxT("COORDS"));
655 if (tmp
== wxT("POLY"))
657 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
659 else if (tmp
== wxT("CIRCLE"))
661 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
663 else if (tmp
== wxT("RECT"))
665 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
667 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
669 wxString tmp
= tag
.GetParam(wxT("HREF"));
670 wxString target
= wxEmptyString
;
671 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
672 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
674 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
685 TAGS_MODULE_BEGIN(Image
)
689 TAGS_MODULE_END(Image
)