]>
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"
29 #include "wx/html/forcelnk.h"
30 #include "wx/html/m_templ.h"
33 #include "wx/gifdecod.h"
34 #include "wx/dynarray.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 imagemap
52 // (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;
72 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
75 wxString x
= incoords
, y
;
78 while ((i
= x
.Find( ',' )) != -1)
80 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
83 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
86 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
98 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
112 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
121 if (coords
.GetCount() >= 6)
126 int totalv
= coords
.GetCount() / 2;
127 int totalc
= totalv
* 2;
128 int xval
= coords
[totalc
- 2];
129 int yval
= coords
[totalc
- 1];
133 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
135 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
137 intersects
+= (xval
>= wherex
) ? 1 : 0;
141 intersects
+= ((xval
- (yval
- wherey
) *
143 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
147 while (pointer
< end
)
149 yval
= coords
[pointer
];
153 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
161 if ((coords
[pointer
- 3] >= wherex
) ==
162 (coords
[pointer
- 1] >= wherex
)) {
163 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
168 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
169 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
170 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
175 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
183 if ((coords
[pointer
- 3] >= wherex
) ==
184 (coords
[pointer
- 1] >= wherex
))
186 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
191 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
192 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
193 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
197 if ((intersects
& 1) != 0)
208 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
209 return a
->GetLink( x
, y
);
221 //--------------------------------------------------------------------------------
222 // wxHtmlImageMapCell
223 // 0-width, 0-height cell that represents map from imagemaps
224 // it is always placed before wxHtmlImageMapAreaCells
225 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
226 //--------------------------------------------------------------------------------
229 class wxHtmlImageMapCell
: public wxHtmlCell
232 wxHtmlImageMapCell( wxString
&name
);
236 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
237 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
241 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
246 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
248 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
250 return a
->GetLink( x
, y
);
251 return wxHtmlCell::GetLink( x
, y
);
254 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
256 if (cond
== wxHTML_COND_ISIMAGEMAP
)
258 if (m_Name
== *((wxString
*)(param
)))
261 return wxHtmlCell::Find(cond
, param
);
268 //--------------------------------------------------------------------------------
271 //--------------------------------------------------------------------------------
273 class wxHtmlImageCell
: public wxHtmlCell
276 wxHtmlImageCell(wxWindow
*window
,
277 wxFSFile
*input
, int w
= -1, int h
= -1,
278 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
279 const wxString
& mapname
= wxEmptyString
);
281 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
282 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
284 void SetImage(const wxImage
& img
);
285 #if wxUSE_GIF && wxUSE_TIMER
286 void AdvanceAnimation(wxTimer
*timer
);
287 virtual void Layout(int w
);
293 wxScrolledWindow
*m_window
;
294 #if wxUSE_GIF && wxUSE_TIMER
295 wxGIFDecoder
*m_gifDecoder
;
297 int m_physX
, m_physY
;
300 wxHtmlImageMapCell
*m_imageMap
;
304 #if wxUSE_GIF && wxUSE_TIMER
305 class wxGIFTimer
: public wxTimer
308 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
309 virtual void Notify()
311 m_cell
->AdvanceAnimation(this);
315 wxHtmlImageCell
*m_cell
;
320 //--------------------------------------------------------------------------------
322 //--------------------------------------------------------------------------------
324 wxHtmlImageCell::wxHtmlImageCell(wxWindow
*window
, wxFSFile
*input
,
325 int w
, int h
, double scale
, int align
,
326 const wxString
& mapname
) : wxHtmlCell()
328 m_window
= wxStaticCast(window
, wxScrolledWindow
);
335 SetCanLiveOnPagebreak(FALSE
);
336 #if wxUSE_GIF && wxUSE_TIMER
339 m_physX
= m_physY
= -1;
342 wxInputStream
*s
= input
->GetStream();
348 #if wxUSE_GIF && wxUSE_TIMER
349 if ( (input
->GetLocation().Matches(wxT("*.gif")) ||
350 input
->GetLocation().Matches(wxT("*.GIF"))) && m_window
)
352 m_gifDecoder
= new wxGIFDecoder(s
, TRUE
);
353 if ( m_gifDecoder
->ReadGIF() == wxGIF_OK
)
356 if ( m_gifDecoder
->ConvertToImage(&img
) )
361 if ( m_gifDecoder
->IsAnimation() )
363 m_gifTimer
= new wxGIFTimer(this);
364 m_gifTimer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
368 wxDELETE(m_gifDecoder
);
373 wxDELETE(m_gifDecoder
);
380 SetImage(wxImage(*s
, wxBITMAP_TYPE_ANY
));
384 m_Width
= (int)(scale
* (double)m_bmpW
);
385 m_Height
= (int)(scale
* (double)m_bmpH
);
389 case wxHTML_ALIGN_TOP
:
390 m_Descent
= m_Height
;
392 case wxHTML_ALIGN_CENTER
:
393 m_Descent
= m_Height
/ 2;
395 case wxHTML_ALIGN_BOTTOM
:
402 void wxHtmlImageCell::SetImage(const wxImage
& img
)
410 hh
= img
.GetHeight();
417 if ((m_bmpW
!= ww
) || (m_bmpH
!= hh
))
419 wxImage img2
= img
.Scale(m_bmpW
, m_bmpH
);
420 m_bitmap
= new wxBitmap(img2
);
423 m_bitmap
= new wxBitmap(img
);
427 #if wxUSE_GIF && wxUSE_TIMER
428 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
432 m_gifDecoder
->GoNextFrame(TRUE
);
436 m_physX
= m_physY
= 0;
437 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
439 m_physX
+= cell
->GetPosX();
440 m_physY
+= cell
->GetPosY();
445 m_window
->CalcScrolledPosition(m_physX
, m_physY
, &x
, &y
);
446 wxRect
rect(x
, y
, m_Width
, m_Height
);
448 if ( m_window
->GetClientRect().Intersects(rect
) &&
449 m_gifDecoder
->ConvertToImage(&img
) )
452 m_window
->Refresh(img
.HasMask(), &rect
);
455 timer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
458 void wxHtmlImageCell::Layout(int w
)
460 wxHtmlCell::Layout(w
);
461 m_physX
= m_physY
= -1;
466 wxHtmlImageCell::~wxHtmlImageCell()
469 #if wxUSE_GIF && wxUSE_TIMER
476 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
481 dc
.GetUserScale(&us_x
, &us_y
);
482 dc
.SetUserScale(us_x
* m_scale
, us_y
* m_scale
);
484 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / m_scale
),
485 (int) ((y
+ m_PosY
) / m_scale
), TRUE
);
486 dc
.SetUserScale(us_x
, us_y
);
490 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
492 if (m_mapName
.IsEmpty())
493 return wxHtmlCell::GetLink( x
, y
);
496 wxHtmlContainerCell
*p
, *op
;
497 op
= p
= GetParent();
504 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
505 (const void*)(&m_mapName
));
508 ((wxString
&)m_mapName
).Clear();
509 return wxHtmlCell::GetLink( x
, y
);
511 { // dirty hack, ask Joel why he fills m_ImageMap in this place
512 // THE problem is that we're in const method and we can't modify m_ImageMap
513 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
514 *cx
= (wxHtmlImageMapCell
*)cell
;
517 return m_imageMap
->GetLink(x
, y
);
522 //--------------------------------------------------------------------------------
524 //--------------------------------------------------------------------------------
526 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
528 TAG_HANDLER_PROC(tag
)
530 if (tag
.GetName() == wxT("IMG"))
532 if (tag
.HasParam(wxT("SRC")))
537 wxString tmp
= tag
.GetParam(wxT("SRC"));
538 wxString mn
= wxEmptyString
;
540 str
= m_WParser
->GetFS()->OpenFile(tmp
);
541 if (tag
.HasParam(wxT("WIDTH")))
542 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
543 if (tag
.HasParam(wxT("HEIGHT")))
544 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
545 al
= wxHTML_ALIGN_BOTTOM
;
546 if (tag
.HasParam(wxT("ALIGN")))
548 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
549 alstr
.MakeUpper(); // for the case alignment was in ".."
550 if (alstr
== wxT("TEXTTOP"))
551 al
= wxHTML_ALIGN_TOP
;
552 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
553 al
= wxHTML_ALIGN_CENTER
;
555 if (tag
.HasParam(wxT("USEMAP")))
557 mn
= tag
.GetParam( wxT("USEMAP") );
558 if (mn
.GetChar(0) == wxT('#'))
563 wxHtmlImageCell
*cel
= NULL
;
566 cel
= new wxHtmlImageCell(m_WParser
->GetWindow(),
568 m_WParser
->GetPixelScale(),
570 cel
->SetLink(m_WParser
->GetLink());
571 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
572 m_WParser
->GetContainer()->InsertCell(cel
);
577 if (tag
.GetName() == wxT("MAP"))
579 m_WParser
->CloseContainer();
580 m_WParser
->OpenContainer();
581 if (tag
.HasParam(wxT("NAME")))
583 wxString tmp
= tag
.GetParam(wxT("NAME"));
584 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
585 m_WParser
->GetContainer()->InsertCell( cel
);
588 m_WParser
->CloseContainer();
589 m_WParser
->OpenContainer();
591 if (tag
.GetName() == wxT("AREA"))
593 if (tag
.HasParam(wxT("SHAPE")))
595 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
596 wxString coords
= wxEmptyString
;
598 wxHtmlImageMapAreaCell
*cel
= NULL
;
599 if (tag
.HasParam(wxT("COORDS")))
601 coords
= tag
.GetParam(wxT("COORDS"));
603 if (tmp
== wxT("POLY"))
605 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
607 else if (tmp
== wxT("CIRCLE"))
609 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
611 else if (tmp
== wxT("RECT"))
613 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
615 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
617 wxString tmp
= tag
.GetParam(wxT("HREF"));
618 wxString target
= wxEmptyString
;
619 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
620 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
622 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
633 TAGS_MODULE_BEGIN(Image
)
637 TAGS_MODULE_END(Image
)