]>
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"
42 FORCE_LINK_ME(m_image
)
47 WX_DECLARE_OBJARRAY(int, CoordArray
);
48 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
49 WX_DEFINE_OBJARRAY(CoordArray
);
52 //--------------------------------------------------------------------------------
53 // wxHtmlImageMapAreaCell
54 // 0-width, 0-height cell that represents single area in imagemap
55 // (it's GetLink is called from wxHtmlImageCell's)
56 //--------------------------------------------------------------------------------
58 class wxHtmlImageMapAreaCell
: public wxHtmlCell
61 enum celltype
{ CIRCLE
, RECT
, POLY
};
67 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
68 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
75 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
78 wxString x
= incoords
, y
;
81 while ((i
= x
.Find( ',' )) != -1)
83 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
86 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
89 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
101 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
115 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
124 if (coords
.GetCount() >= 6)
129 int totalv
= coords
.GetCount() / 2;
130 int totalc
= totalv
* 2;
131 int xval
= coords
[totalc
- 2];
132 int yval
= coords
[totalc
- 1];
136 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
138 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
140 intersects
+= (xval
>= wherex
) ? 1 : 0;
144 intersects
+= ((xval
- (yval
- wherey
) *
146 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
150 while (pointer
< end
)
152 yval
= coords
[pointer
];
156 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
164 if ((coords
[pointer
- 3] >= wherex
) ==
165 (coords
[pointer
- 1] >= wherex
)) {
166 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
171 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
172 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
173 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
178 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
186 if ((coords
[pointer
- 3] >= wherex
) ==
187 (coords
[pointer
- 1] >= wherex
))
189 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
194 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
195 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
196 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
200 if ((intersects
& 1) != 0)
211 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
212 return a
->GetLink( x
, y
);
224 //--------------------------------------------------------------------------------
225 // wxHtmlImageMapCell
226 // 0-width, 0-height cell that represents map from imagemaps
227 // it is always placed before wxHtmlImageMapAreaCells
228 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
229 //--------------------------------------------------------------------------------
232 class wxHtmlImageMapCell
: public wxHtmlCell
235 wxHtmlImageMapCell( wxString
&name
);
239 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
240 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
244 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
249 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
251 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
253 return a
->GetLink( x
, y
);
254 return wxHtmlCell::GetLink( x
, y
);
257 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
259 if (cond
== wxHTML_COND_ISIMAGEMAP
)
261 if (m_Name
== *((wxString
*)(param
)))
264 return wxHtmlCell::Find(cond
, param
);
271 //--------------------------------------------------------------------------------
274 //--------------------------------------------------------------------------------
276 class wxHtmlImageCell
: public wxHtmlCell
279 wxHtmlImageCell(wxWindow
*window
,
280 wxFSFile
*input
, int w
= -1, int h
= -1,
281 double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
,
282 const wxString
& mapname
= wxEmptyString
);
284 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
285 virtual wxHtmlLinkInfo
*GetLink(int x
= 0, int y
= 0) const;
287 void SetImage(const wxImage
& img
);
288 #if wxUSE_GIF && wxUSE_TIMER
289 void AdvanceAnimation(wxTimer
*timer
);
290 virtual void Layout(int w
);
297 wxScrolledWindow
*m_window
;
298 #if wxUSE_GIF && wxUSE_TIMER
299 wxGIFDecoder
*m_gifDecoder
;
301 int m_physX
, m_physY
;
304 wxHtmlImageMapCell
*m_imageMap
;
308 #if wxUSE_GIF && wxUSE_TIMER
309 class wxGIFTimer
: public wxTimer
312 wxGIFTimer(wxHtmlImageCell
*cell
) : m_cell(cell
) {}
313 virtual void Notify()
315 m_cell
->AdvanceAnimation(this);
319 wxHtmlImageCell
*m_cell
;
324 //--------------------------------------------------------------------------------
326 //--------------------------------------------------------------------------------
329 static const char * broken_image_xpm
[] = {
338 "..................... ",
339 ".+++++++++++++++++++.. ",
340 ".+++++++++++++++++++.@. ",
341 ".++@@@@@@@@@@@@@@@@@.+@. ",
342 ".++@@@@@@@@@@@@@@@@@.++@. ",
343 ".++@@@@@.@@@@.@@@@@@.+++@. ",
344 ".++@@@@@@@@@@@@@@@@@.++++@. ",
345 ".++@@@@@@@@@@@@@@@@@.+++++@. ",
346 ".++@@.@@@@@@@@@@.@@@######## ",
347 ".++@@@@@@@@@@@@@@@@@@$$$$$$#.",
348 ".######@@@@@@@@@@@@@@@.....#.",
349 " ###@@@@@@@@@@@@@@@++#.",
350 " #####@@@@@@@@@@++#.",
353 ".+.... #@@@@@@++#.",
354 ".++@@@... ####@@++#.",
355 ".++@@@@@@.. #####.",
358 ".++@@@@@@%%%%@@.... ",
359 ".++@@@@@@%%%%@@@@@@.... ",
360 ".++@@@@@@%%%%@@@@@@@@@@.... ",
361 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
362 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
363 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
364 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
365 ".++@@@@@@@@@@@@@@@@@@@@@@++#.",
366 ".++++++++++++++++++++++++++#.",
367 ".++++++++++++++++++++++++++#.",
368 "############################."};
370 wxHtmlImageCell::wxHtmlImageCell(wxWindow
*window
, wxFSFile
*input
,
371 int w
, int h
, double scale
, int align
,
372 const wxString
& mapname
) : wxHtmlCell()
374 m_window
= window
? wxStaticCast(window
, wxScrolledWindow
) : NULL
;
382 SetCanLiveOnPagebreak(FALSE
);
383 #if wxUSE_GIF && wxUSE_TIMER
386 m_physX
= m_physY
= -1;
389 if ( m_bmpW
&& m_bmpH
)
393 wxInputStream
*s
= input
->GetStream();
399 #if wxUSE_GIF && wxUSE_TIMER
400 if ( (input
->GetLocation().Matches(wxT("*.gif")) ||
401 input
->GetLocation().Matches(wxT("*.GIF"))) && m_window
)
403 m_gifDecoder
= new wxGIFDecoder(s
, TRUE
);
404 if ( m_gifDecoder
->ReadGIF() == wxGIF_OK
)
407 if ( m_gifDecoder
->ConvertToImage(&img
) )
412 if ( m_gifDecoder
->IsAnimation() )
414 m_gifTimer
= new wxGIFTimer(this);
415 m_gifTimer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
419 wxDELETE(m_gifDecoder
);
424 wxDELETE(m_gifDecoder
);
429 #endif // wxUSE_GIF && wxUSE_TIMER
431 wxImage
image(*s
, wxBITMAP_TYPE_ANY
);
437 else // input==NULL, use "broken image" bitmap
439 if ( m_bmpW
== -1 && m_bmpH
== -1 )
447 if ( m_bmpW
== -1 ) m_bmpW
= 31;
448 if ( m_bmpH
== -1 ) m_bmpH
= 33;
450 m_bitmap
= new wxBitmap(broken_image_xpm
);
453 //else: ignore the 0-sized images used sometimes on the Web pages
455 m_Width
= (int)(scale
* (double)m_bmpW
);
456 m_Height
= (int)(scale
* (double)m_bmpH
);
460 case wxHTML_ALIGN_TOP
:
461 m_Descent
= m_Height
;
463 case wxHTML_ALIGN_CENTER
:
464 m_Descent
= m_Height
/ 2;
466 case wxHTML_ALIGN_BOTTOM
:
473 void wxHtmlImageCell::SetImage(const wxImage
& img
)
481 hh
= img
.GetHeight();
488 if ((m_bmpW
!= ww
) || (m_bmpH
!= hh
))
490 wxImage img2
= img
.Scale(m_bmpW
, m_bmpH
);
491 m_bitmap
= new wxBitmap(img2
);
494 m_bitmap
= new wxBitmap(img
);
498 #if wxUSE_GIF && wxUSE_TIMER
499 void wxHtmlImageCell::AdvanceAnimation(wxTimer
*timer
)
503 m_gifDecoder
->GoNextFrame(TRUE
);
507 m_physX
= m_physY
= 0;
508 for (wxHtmlCell
*cell
= this; cell
; cell
= cell
->GetParent())
510 m_physX
+= cell
->GetPosX();
511 m_physY
+= cell
->GetPosY();
516 m_window
->CalcScrolledPosition(m_physX
, m_physY
, &x
, &y
);
517 wxRect
rect(x
, y
, m_Width
, m_Height
);
519 if ( m_window
->GetClientRect().Intersects(rect
) &&
520 m_gifDecoder
->ConvertToImage(&img
) )
522 if ( (int)m_gifDecoder
->GetWidth() != m_Width
||
523 (int)m_gifDecoder
->GetHeight() != m_Height
||
524 m_gifDecoder
->GetLeft() != 0 || m_gifDecoder
->GetTop() != 0 )
528 dc
.SelectObject(*m_bitmap
);
529 dc
.DrawBitmap(bmp
, m_gifDecoder
->GetLeft(), m_gifDecoder
->GetTop());
533 m_window
->Refresh(img
.HasMask(), &rect
);
536 timer
->Start(m_gifDecoder
->GetDelay(), TRUE
);
539 void wxHtmlImageCell::Layout(int w
)
541 wxHtmlCell::Layout(w
);
542 m_physX
= m_physY
= -1;
547 wxHtmlImageCell::~wxHtmlImageCell()
550 #if wxUSE_GIF && wxUSE_TIMER
557 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
561 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
562 dc
.SetPen(*wxBLACK_PEN
);
563 dc
.DrawRectangle(x
+ m_PosX
, y
+ m_PosY
, m_Width
, m_Height
);
569 dc
.GetUserScale(&us_x
, &us_y
);
570 dc
.SetUserScale(us_x
* m_scale
, us_y
* m_scale
);
572 dc
.DrawBitmap(*m_bitmap
, (int) ((x
+ m_PosX
) / m_scale
),
573 (int) ((y
+ m_PosY
) / m_scale
), TRUE
);
574 dc
.SetUserScale(us_x
, us_y
);
578 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
580 if (m_mapName
.IsEmpty())
581 return wxHtmlCell::GetLink( x
, y
);
584 wxHtmlContainerCell
*p
, *op
;
585 op
= p
= GetParent();
592 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find(wxHTML_COND_ISIMAGEMAP
,
593 (const void*)(&m_mapName
));
596 ((wxString
&)m_mapName
).Clear();
597 return wxHtmlCell::GetLink( x
, y
);
599 { // dirty hack, ask Joel why he fills m_ImageMap in this place
600 // THE problem is that we're in const method and we can't modify m_ImageMap
601 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_imageMap
);
602 *cx
= (wxHtmlImageMapCell
*)cell
;
605 return m_imageMap
->GetLink(x
, y
);
610 //--------------------------------------------------------------------------------
612 //--------------------------------------------------------------------------------
614 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
616 TAG_HANDLER_PROC(tag
)
618 if (tag
.GetName() == wxT("IMG"))
620 if (tag
.HasParam(wxT("SRC")))
625 wxString tmp
= tag
.GetParam(wxT("SRC"));
626 wxString mn
= wxEmptyString
;
628 str
= m_WParser
->OpenURL(wxHTML_URL_IMAGE
, tmp
);
630 if (tag
.HasParam(wxT("WIDTH")))
631 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
632 if (tag
.HasParam(wxT("HEIGHT")))
633 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
634 al
= wxHTML_ALIGN_BOTTOM
;
635 if (tag
.HasParam(wxT("ALIGN")))
637 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
638 alstr
.MakeUpper(); // for the case alignment was in ".."
639 if (alstr
== wxT("TEXTTOP"))
640 al
= wxHTML_ALIGN_TOP
;
641 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
642 al
= wxHTML_ALIGN_CENTER
;
644 if (tag
.HasParam(wxT("USEMAP")))
646 mn
= tag
.GetParam( wxT("USEMAP") );
647 if (mn
.GetChar(0) == wxT('#'))
652 wxHtmlImageCell
*cel
= new wxHtmlImageCell(
653 m_WParser
->GetWindow(),
655 m_WParser
->GetPixelScale(),
657 cel
->SetLink(m_WParser
->GetLink());
658 cel
->SetId(tag
.GetParam(wxT("id"))); // may be empty
659 m_WParser
->GetContainer()->InsertCell(cel
);
664 if (tag
.GetName() == wxT("MAP"))
666 m_WParser
->CloseContainer();
667 m_WParser
->OpenContainer();
668 if (tag
.HasParam(wxT("NAME")))
670 wxString tmp
= tag
.GetParam(wxT("NAME"));
671 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
672 m_WParser
->GetContainer()->InsertCell( cel
);
675 m_WParser
->CloseContainer();
676 m_WParser
->OpenContainer();
678 if (tag
.GetName() == wxT("AREA"))
680 if (tag
.HasParam(wxT("SHAPE")))
682 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
683 wxString coords
= wxEmptyString
;
685 wxHtmlImageMapAreaCell
*cel
= NULL
;
686 if (tag
.HasParam(wxT("COORDS")))
688 coords
= tag
.GetParam(wxT("COORDS"));
690 if (tmp
== wxT("POLY"))
692 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
694 else if (tmp
== wxT("CIRCLE"))
696 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
698 else if (tmp
== wxT("RECT"))
700 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
702 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
704 wxString tmp
= tag
.GetParam(wxT("HREF"));
705 wxString target
= wxEmptyString
;
706 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
707 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
709 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
720 TAGS_MODULE_BEGIN(Image
)
724 TAGS_MODULE_END(Image
)