]>
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"
34 #include "wx/gifdecod.h"
35 #include "wx/dynarray.h"
40 FORCE_LINK_ME(m_image
)
45 WX_DECLARE_OBJARRAY(int, CoordArray
);
46 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
47 WX_DEFINE_OBJARRAY(CoordArray
);
50 //--------------------------------------------------------------------------------
51 // wxHtmlImageMapAreaCell
52 // 0-width, 0-height cell that represents single area in imagemap
53 // (it's GetLink is called from wxHtmlImageCell's)
54 //--------------------------------------------------------------------------------
56 class wxHtmlImageMapAreaCell
: public wxHtmlCell
59 enum celltype
{ CIRCLE
, RECT
, POLY
};
65 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
66 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
73 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
76 wxString x
= incoords
, y
;
79 while ((i
= x
.Find( ',' )) != -1)
81 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
84 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
87 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
99 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
113 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
122 if (coords
.GetCount() >= 6)
127 int totalv
= coords
.GetCount() / 2;
128 int totalc
= totalv
* 2;
129 int xval
= coords
[totalc
- 2];
130 int yval
= coords
[totalc
- 1];
134 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
136 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
138 intersects
+= (xval
>= wherex
) ? 1 : 0;
142 intersects
+= ((xval
- (yval
- wherey
) *
144 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
148 while (pointer
< end
)
150 yval
= coords
[pointer
];
154 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
162 if ((coords
[pointer
- 3] >= wherex
) ==
163 (coords
[pointer
- 1] >= wherex
)) {
164 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
169 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
170 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
171 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
176 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
184 if ((coords
[pointer
- 3] >= wherex
) ==
185 (coords
[pointer
- 1] >= wherex
))
187 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
192 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
193 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
194 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
198 if ((intersects
& 1) != 0)
209 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
210 return a
->GetLink( x
, y
);
222 //--------------------------------------------------------------------------------
223 // wxHtmlImageMapCell
224 // 0-width, 0-height cell that represents map from imagemaps
225 // it is always placed before wxHtmlImageMapAreaCells
226 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
227 //--------------------------------------------------------------------------------
230 class wxHtmlImageMapCell
: public wxHtmlCell
233 wxHtmlImageMapCell( wxString
&name
);
237 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
238 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
242 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
247 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
249 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
251 return a
->GetLink( x
, y
);
252 return wxHtmlCell::GetLink( x
, y
);
255 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
257 if (cond
== wxHTML_COND_ISIMAGEMAP
)
259 if (m_Name
== *((wxString
*)(param
)))
262 return wxHtmlCell::Find(cond
, param
);
269 //--------------------------------------------------------------------------------
272 //--------------------------------------------------------------------------------
274 class wxHtmlImageCell
: public wxHtmlCell
277 wxHtmlImageCell(wxWindow
*window
,
278 wxFSFile
*input
, int w
= -1, int h
= -1,
279 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
280 const wxString
& mapname
= wxEmptyString
);
282 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
283 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
285 void SetImage(const wxImage
& img
);
286 #if wxUSE_GIF && wxUSE_TIMER
287 void AdvanceAnimation(wxTimer
*timer
);
288 virtual void Layout(int w
);
294 wxScrolledWindow
*m_window
;
295 #if wxUSE_GIF && wxUSE_TIMER
296 wxGIFDecoder
*m_gifDecoder
;
298 int m_physX
, m_physY
;
301 wxHtmlImageMapCell
*m_imageMap
;
305 #if wxUSE_GIF && wxUSE_TIMER
306 class wxGIFTimer
: public wxTimer
309 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
310 virtual void Notify()
312 m_cell
->AdvanceAnimation(this);
316 wxHtmlImageCell
*m_cell
;
321 //--------------------------------------------------------------------------------
323 //--------------------------------------------------------------------------------
325 wxHtmlImageCell::wxHtmlImageCell(wxWindow
*window
, wxFSFile
*input
,
326 int w
, int h
, double scale
, int align
,
327 const wxString
& mapname
) : wxHtmlCell()
329 m_window
= wxStaticCast(window
, wxScrolledWindow
);
336 SetCanLiveOnPagebreak(FALSE
);
337 #if wxUSE_GIF && wxUSE_TIMER
340 m_physX
= m_physY
= -1;
343 wxInputStream
*s
= input
->GetStream();
349 #if wxUSE_GIF && wxUSE_TIMER
350 if ( (input
->GetLocation().Matches(wxT("*.gif")) ||
351 input
->GetLocation().Matches(wxT("*.GIF"))) && m_window
)
353 m_gifDecoder
= new wxGIFDecoder(s
, TRUE
);
354 if ( m_gifDecoder
->ReadGIF() == wxGIF_OK
)
357 if ( m_gifDecoder
->ConvertToImage(&img
) )
362 if ( m_gifDecoder
->IsAnimation() )
364 m_gifTimer
= new wxGIFTimer(this);
365 m_gifTimer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
369 wxDELETE(m_gifDecoder
);
374 wxDELETE(m_gifDecoder
);
381 SetImage(wxImage(*s
, wxBITMAP_TYPE_ANY
));
385 m_Width
= (int)(scale
* (double)m_bmpW
);
386 m_Height
= (int)(scale
* (double)m_bmpH
);
390 case wxHTML_ALIGN_TOP
:
391 m_Descent
= m_Height
;
393 case wxHTML_ALIGN_CENTER
:
394 m_Descent
= m_Height
/ 2;
396 case wxHTML_ALIGN_BOTTOM
:
403 void wxHtmlImageCell::SetImage(const wxImage
& img
)
411 hh
= img
.GetHeight();
418 if ((m_bmpW
!= ww
) || (m_bmpH
!= hh
))
420 wxImage img2
= img
.Scale(m_bmpW
, m_bmpH
);
421 m_bitmap
= new wxBitmap(img2
);
424 m_bitmap
= new wxBitmap(img
);
428 #if wxUSE_GIF && wxUSE_TIMER
429 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
433 m_gifDecoder
->GoNextFrame(TRUE
);
437 m_physX
= m_physY
= 0;
438 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
440 m_physX
+= cell
->GetPosX();
441 m_physY
+= cell
->GetPosY();
446 m_window
->CalcScrolledPosition(m_physX
, m_physY
, &x
, &y
);
447 wxRect
rect(x
, y
, m_Width
, m_Height
);
449 if ( m_window
->GetClientRect().Intersects(rect
) &&
450 m_gifDecoder
->ConvertToImage(&img
) )
452 if ( (int)m_gifDecoder
->GetWidth() != m_Width
||
453 (int)m_gifDecoder
->GetHeight() != m_Height
||
454 m_gifDecoder
->GetLeft() != 0 || m_gifDecoder
->GetTop() != 0 )
458 dc
.SelectObject(*m_bitmap
);
459 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetLeft(), m_gifDecoder
->GetTop());
463 m_window
->Refresh(img
.HasMask(), &rect
);
466 timer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
469 void wxHtmlImageCell::Layout(int w
)
471 wxHtmlCell::Layout(w
);
472 m_physX
= m_physY
= -1;
477 wxHtmlImageCell::~wxHtmlImageCell()
480 #if wxUSE_GIF && wxUSE_TIMER
487 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
492 dc
.GetUserScale(&us_x
, &us_y
);
493 dc
.SetUserScale(us_x
* m_scale
, us_y
* m_scale
);
495 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / m_scale
),
496 (int) ((y
+ m_PosY
) / m_scale
), TRUE
);
497 dc
.SetUserScale(us_x
, us_y
);
501 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
503 if (m_mapName
.IsEmpty())
504 return wxHtmlCell::GetLink( x
, y
);
507 wxHtmlContainerCell
*p
, *op
;
508 op
= p
= GetParent();
515 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
516 (const void*)(&m_mapName
));
519 ((wxString
&)m_mapName
).Clear();
520 return wxHtmlCell::GetLink( x
, y
);
522 { // dirty hack, ask Joel why he fills m_ImageMap in this place
523 // THE problem is that we're in const method and we can't modify m_ImageMap
524 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
525 *cx
= (wxHtmlImageMapCell
*)cell
;
528 return m_imageMap
->GetLink(x
, y
);
533 //--------------------------------------------------------------------------------
535 //--------------------------------------------------------------------------------
537 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
539 TAG_HANDLER_PROC(tag
)
541 if (tag
.GetName() == wxT("IMG"))
543 if (tag
.HasParam(wxT("SRC")))
548 wxString tmp
= tag
.GetParam(wxT("SRC"));
549 wxString mn
= wxEmptyString
;
551 str
= m_WParser
->GetFS()->OpenFile(tmp
);
552 if (tag
.HasParam(wxT("WIDTH")))
553 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
554 if (tag
.HasParam(wxT("HEIGHT")))
555 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
556 al
= wxHTML_ALIGN_BOTTOM
;
557 if (tag
.HasParam(wxT("ALIGN")))
559 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
560 alstr
.MakeUpper(); // for the case alignment was in ".."
561 if (alstr
== wxT("TEXTTOP"))
562 al
= wxHTML_ALIGN_TOP
;
563 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
564 al
= wxHTML_ALIGN_CENTER
;
566 if (tag
.HasParam(wxT("USEMAP")))
568 mn
= tag
.GetParam( wxT("USEMAP") );
569 if (mn
.GetChar(0) == wxT('#'))
574 wxHtmlImageCell
*cel
= NULL
;
577 cel
= new wxHtmlImageCell(m_WParser
->GetWindow(),
579 m_WParser
->GetPixelScale(),
581 cel
->SetLink(m_WParser
->GetLink());
582 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
583 m_WParser
->GetContainer()->InsertCell(cel
);
588 if (tag
.GetName() == wxT("MAP"))
590 m_WParser
->CloseContainer();
591 m_WParser
->OpenContainer();
592 if (tag
.HasParam(wxT("NAME")))
594 wxString tmp
= tag
.GetParam(wxT("NAME"));
595 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
596 m_WParser
->GetContainer()->InsertCell( cel
);
599 m_WParser
->CloseContainer();
600 m_WParser
->OpenContainer();
602 if (tag
.GetName() == wxT("AREA"))
604 if (tag
.HasParam(wxT("SHAPE")))
606 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
607 wxString coords
= wxEmptyString
;
609 wxHtmlImageMapAreaCell
*cel
= NULL
;
610 if (tag
.HasParam(wxT("COORDS")))
612 coords
= tag
.GetParam(wxT("COORDS"));
614 if (tmp
== wxT("POLY"))
616 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
618 else if (tmp
== wxT("CIRCLE"))
620 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
622 else if (tmp
== wxT("RECT"))
624 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
626 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
628 wxString tmp
= tag
.GetParam(wxT("HREF"));
629 wxString target
= wxEmptyString
;
630 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
631 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
633 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
644 TAGS_MODULE_BEGIN(Image
)
648 TAGS_MODULE_END(Image
)