]>
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"
41 FORCE_LINK_ME(m_image
)
46 WX_DECLARE_OBJARRAY(int, CoordArray
);
47 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
48 WX_DEFINE_OBJARRAY(CoordArray
);
51 //--------------------------------------------------------------------------------
52 // wxHtmlImageMapAreaCell
53 // 0-width, 0-height cell that represents single area in imagemap
54 // (it's GetLink is called from wxHtmlImageCell's)
55 //--------------------------------------------------------------------------------
57 class wxHtmlImageMapAreaCell
: public wxHtmlCell
60 enum celltype
{ CIRCLE
, RECT
, POLY
};
66 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
67 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
74 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
77 wxString x
= incoords
, y
;
80 while ((i
= x
.Find( ',' )) != -1)
82 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
85 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
88 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
100 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
114 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
123 if (coords
.GetCount() >= 6)
128 int totalv
= coords
.GetCount() / 2;
129 int totalc
= totalv
* 2;
130 int xval
= coords
[totalc
- 2];
131 int yval
= coords
[totalc
- 1];
135 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
137 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
139 intersects
+= (xval
>= wherex
) ? 1 : 0;
143 intersects
+= ((xval
- (yval
- wherey
) *
145 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
149 while (pointer
< end
)
151 yval
= coords
[pointer
];
155 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
163 if ((coords
[pointer
- 3] >= wherex
) ==
164 (coords
[pointer
- 1] >= wherex
)) {
165 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
170 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
171 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
172 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
177 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
185 if ((coords
[pointer
- 3] >= wherex
) ==
186 (coords
[pointer
- 1] >= wherex
))
188 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
193 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
194 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
195 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
199 if ((intersects
& 1) != 0)
210 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
211 return a
->GetLink( x
, y
);
223 //--------------------------------------------------------------------------------
224 // wxHtmlImageMapCell
225 // 0-width, 0-height cell that represents map from imagemaps
226 // it is always placed before wxHtmlImageMapAreaCells
227 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
228 //--------------------------------------------------------------------------------
231 class wxHtmlImageMapCell
: public wxHtmlCell
234 wxHtmlImageMapCell( wxString
&name
);
238 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
239 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
243 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
248 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
250 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
252 return a
->GetLink( x
, y
);
253 return wxHtmlCell::GetLink( x
, y
);
256 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
258 if (cond
== wxHTML_COND_ISIMAGEMAP
)
260 if (m_Name
== *((wxString
*)(param
)))
263 return wxHtmlCell::Find(cond
, param
);
270 //--------------------------------------------------------------------------------
273 //--------------------------------------------------------------------------------
275 class wxHtmlImageCell
: public wxHtmlCell
278 wxHtmlImageCell(wxWindow
*window
,
279 wxFSFile
*input
, int w
= -1, int h
= -1,
280 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
281 const wxString
& mapname
= wxEmptyString
);
283 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
284 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
286 void SetImage(const wxImage
& img
);
287 #if wxUSE_GIF && wxUSE_TIMER
288 void AdvanceAnimation(wxTimer
*timer
);
289 virtual void Layout(int w
);
296 wxScrolledWindow
*m_window
;
297 #if wxUSE_GIF && wxUSE_TIMER
298 wxGIFDecoder
*m_gifDecoder
;
300 int m_physX
, m_physY
;
303 wxHtmlImageMapCell
*m_imageMap
;
307 #if wxUSE_GIF && wxUSE_TIMER
308 class wxGIFTimer
: public wxTimer
311 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
312 virtual void Notify()
314 m_cell
->AdvanceAnimation(this);
318 wxHtmlImageCell
*m_cell
;
323 //--------------------------------------------------------------------------------
325 //--------------------------------------------------------------------------------
328 static const char * broken_image_xpm
[] = {
337 "..................... ",
338 ".+++++++++++++++++++.. ",
339 ".+++++++++++++++++++.@. ",
340 ".++@@@@@@@@@@@@@@@@@.+@. ",
341 ".++@@@@@@@@@@@@@@@@@.++@. ",
342 ".++@@@@@.@@@@.@@@@@@.+++@. ",
343 ".++@@@@@@@@@@@@@@@@@.++++@. ",
344 ".++@@@@@@@@@@@@@@@@@.+++++@. ",
345 ".++@@.@@@@@@@@@@.@@@######## ",
346 ".++@@@@@@@@@@@@@@@@@@$$$$$$#.",
347 ".######@@@@@@@@@@@@@@@.....#.",
348 " ###@@@@@@@@@@@@@@@++#.",
349 " #####@@@@@@@@@@++#.",
352 ".+.... #@@@@@@++#.",
353 ".++@@@... ####@@++#.",
354 ".++@@@@@@.. #####.",
357 ".++@@@@@@%%%%@@.... ",
358 ".++@@@@@@%%%%@@@@@@.... ",
359 ".++@@@@@@%%%%@@@@@@@@@@.... ",
360 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
361 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
362 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
363 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
364 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
365 ".++++++++++++++++++++++++++#.",
366 ".++++++++++++++++++++++++++#.",
367 "############################."};
369 wxHtmlImageCell::wxHtmlImageCell(wxWindow
*window
, wxFSFile
*input
,
370 int w
, int h
, double scale
, int align
,
371 const wxString
& mapname
) : wxHtmlCell()
373 m_window
= window
? wxStaticCast(window
, wxScrolledWindow
) : NULL
;
381 SetCanLiveOnPagebreak(FALSE
);
382 #if wxUSE_GIF && wxUSE_TIMER
385 m_physX
= m_physY
= -1;
388 if ( m_bmpW
&& m_bmpH
)
392 wxInputStream
*s
= input
->GetStream();
398 #if wxUSE_GIF && wxUSE_TIMER
399 if ( (input
->GetLocation().Matches(wxT("*.gif")) ||
400 input
->GetLocation().Matches(wxT("*.GIF"))) && m_window
)
402 m_gifDecoder
= new wxGIFDecoder(s
, TRUE
);
403 if ( m_gifDecoder
->ReadGIF() == wxGIF_OK
)
406 if ( m_gifDecoder
->ConvertToImage(&img
) )
411 if ( m_gifDecoder
->IsAnimation() )
413 m_gifTimer
= new wxGIFTimer(this);
414 m_gifTimer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
418 wxDELETE(m_gifDecoder
);
423 wxDELETE(m_gifDecoder
);
428 #endif // wxUSE_GIF && wxUSE_TIMER
430 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
436 else // input==NULL, use "broken image" bitmap
438 if ( m_bmpW
== -1 && m_bmpH
== -1 )
446 if ( m_bmpW
== -1 ) m_bmpW
= 31;
447 if ( m_bmpH
== -1 ) m_bmpH
= 33;
449 m_bitmap
= new wxBitmap(broken_image_xpm
);
452 //else: ignore the 0-sized images used sometimes on the Web pages
454 m_Width
= (int)(scale
* (double)m_bmpW
);
455 m_Height
= (int)(scale
* (double)m_bmpH
);
459 case wxHTML_ALIGN_TOP
:
460 m_Descent
= m_Height
;
462 case wxHTML_ALIGN_CENTER
:
463 m_Descent
= m_Height
/ 2;
465 case wxHTML_ALIGN_BOTTOM
:
472 void wxHtmlImageCell::SetImage(const wxImage
& img
)
480 hh
= img
.GetHeight();
487 if ((m_bmpW
!= ww
) || (m_bmpH
!= hh
))
489 wxImage img2
= img
.Scale(m_bmpW
, m_bmpH
);
490 m_bitmap
= new wxBitmap(img2
);
493 m_bitmap
= new wxBitmap(img
);
497 #if wxUSE_GIF && wxUSE_TIMER
498 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
502 m_gifDecoder
->GoNextFrame(TRUE
);
506 m_physX
= m_physY
= 0;
507 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
509 m_physX
+= cell
->GetPosX();
510 m_physY
+= cell
->GetPosY();
515 m_window
->CalcScrolledPosition(m_physX
, m_physY
, &x
, &y
);
516 wxRect
rect(x
, y
, m_Width
, m_Height
);
518 if ( m_window
->GetClientRect().Intersects(rect
) &&
519 m_gifDecoder
->ConvertToImage(&img
) )
521 if ( (int)m_gifDecoder
->GetWidth() != m_Width
||
522 (int)m_gifDecoder
->GetHeight() != m_Height
||
523 m_gifDecoder
->GetLeft() != 0 || m_gifDecoder
->GetTop() != 0 )
527 dc
.SelectObject(*m_bitmap
);
528 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetLeft(), m_gifDecoder
->GetTop());
532 m_window
->Refresh(img
.HasMask(), &rect
);
535 timer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
538 void wxHtmlImageCell::Layout(int w
)
540 wxHtmlCell::Layout(w
);
541 m_physX
= m_physY
= -1;
546 wxHtmlImageCell::~wxHtmlImageCell()
549 #if wxUSE_GIF && wxUSE_TIMER
556 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
560 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
561 dc
.SetPen(*wxBLACK_PEN
);
562 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
568 dc
.GetUserScale(&us_x
, &us_y
);
569 dc
.SetUserScale(us_x
* m_scale
, us_y
* m_scale
);
571 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / m_scale
),
572 (int) ((y
+ m_PosY
) / m_scale
), TRUE
);
573 dc
.SetUserScale(us_x
, us_y
);
577 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
579 if (m_mapName
.IsEmpty())
580 return wxHtmlCell::GetLink( x
, y
);
583 wxHtmlContainerCell
*p
, *op
;
584 op
= p
= GetParent();
591 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
592 (const void*)(&m_mapName
));
595 ((wxString
&)m_mapName
).Clear();
596 return wxHtmlCell::GetLink( x
, y
);
598 { // dirty hack, ask Joel why he fills m_ImageMap in this place
599 // THE problem is that we're in const method and we can't modify m_ImageMap
600 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
601 *cx
= (wxHtmlImageMapCell
*)cell
;
604 return m_imageMap
->GetLink(x
, y
);
609 //--------------------------------------------------------------------------------
611 //--------------------------------------------------------------------------------
613 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
615 TAG_HANDLER_PROC(tag
)
617 if (tag
.GetName() == wxT("IMG"))
619 if (tag
.HasParam(wxT("SRC")))
624 wxString tmp
= tag
.GetParam(wxT("SRC"));
625 wxString mn
= wxEmptyString
;
627 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
629 if (tag
.HasParam(wxT("WIDTH")))
630 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
631 if (tag
.HasParam(wxT("HEIGHT")))
632 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
633 al
= wxHTML_ALIGN_BOTTOM
;
634 if (tag
.HasParam(wxT("ALIGN")))
636 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
637 alstr
.MakeUpper(); // for the case alignment was in ".."
638 if (alstr
== wxT("TEXTTOP"))
639 al
= wxHTML_ALIGN_TOP
;
640 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
641 al
= wxHTML_ALIGN_CENTER
;
643 if (tag
.HasParam(wxT("USEMAP")))
645 mn
= tag
.GetParam( wxT("USEMAP") );
646 if (mn
.GetChar(0) == wxT('#'))
651 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
652 m_WParser
->GetWindow(),
654 m_WParser
->GetPixelScale(),
656 cel
->SetLink(m_WParser
->GetLink());
657 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
658 m_WParser
->GetContainer()->InsertCell(cel
);
663 if (tag
.GetName() == wxT("MAP"))
665 m_WParser
->CloseContainer();
666 m_WParser
->OpenContainer();
667 if (tag
.HasParam(wxT("NAME")))
669 wxString tmp
= tag
.GetParam(wxT("NAME"));
670 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
671 m_WParser
->GetContainer()->InsertCell( cel
);
674 m_WParser
->CloseContainer();
675 m_WParser
->OpenContainer();
677 if (tag
.GetName() == wxT("AREA"))
679 if (tag
.HasParam(wxT("SHAPE")))
681 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
682 wxString coords
= wxEmptyString
;
684 wxHtmlImageMapAreaCell
*cel
= NULL
;
685 if (tag
.HasParam(wxT("COORDS")))
687 coords
= tag
.GetParam(wxT("COORDS"));
689 if (tmp
== wxT("POLY"))
691 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
693 else if (tmp
== wxT("CIRCLE"))
695 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
697 else if (tmp
== wxT("RECT"))
699 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
701 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
703 wxString tmp
= tag
.GetParam(wxT("HREF"));
704 wxString target
= wxEmptyString
;
705 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
706 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
708 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
719 TAGS_MODULE_BEGIN(Image
)
723 TAGS_MODULE_END(Image
)