]>
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
27 #include "wx/html/forcelnk.h"
28 #include "wx/html/m_templ.h"
32 #include "wx/dynarray.h"
37 FORCE_LINK_ME(m_image
)
42 WX_DECLARE_OBJARRAY(int, CoordArray
);
43 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
44 WX_DEFINE_OBJARRAY(CoordArray
);
47 //--------------------------------------------------------------------------------
48 // wxHtmlImageMapAreaCell
49 // 0-width, 0-height cell that represents single area in imagemap
50 // (it's GetLink is called from wxHtmlImageCell's)
51 //--------------------------------------------------------------------------------
53 class wxHtmlImageMapAreaCell
: public wxHtmlCell
56 enum celltype
{ CIRCLE
, RECT
, POLY
};
62 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
63 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
70 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
73 wxString x
= incoords
, y
;
76 while ((i
= x
.Find( ',' )) != -1)
78 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
81 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
84 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
96 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
110 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
119 if (coords
.GetCount() >= 6)
124 int totalv
= coords
.GetCount() / 2;
125 int totalc
= totalv
* 2;
126 int xval
= coords
[totalc
- 2];
127 int yval
= coords
[totalc
- 1];
131 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
133 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
135 intersects
+= (xval
>= wherex
) ? 1 : 0;
139 intersects
+= ((xval
- (yval
- wherey
) *
141 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
145 while (pointer
< end
)
147 yval
= coords
[pointer
];
151 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
159 if ((coords
[pointer
- 3] >= wherex
) ==
160 (coords
[pointer
- 1] >= wherex
)) {
161 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
166 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
167 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
168 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
173 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
181 if ((coords
[pointer
- 3] >= wherex
) ==
182 (coords
[pointer
- 1] >= wherex
))
184 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
189 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
190 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
191 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
195 if ((intersects
& 1) != 0)
206 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
207 return a
->GetLink( x
, y
);
219 //--------------------------------------------------------------------------------
220 // wxHtmlImageMapCell
221 // 0-width, 0-height cell that represents map from imagemaps
222 // it is always placed before wxHtmlImageMapAreaCells
223 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
224 //--------------------------------------------------------------------------------
227 class wxHtmlImageMapCell
: public wxHtmlCell
230 wxHtmlImageMapCell( wxString
&name
);
234 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
235 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
239 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
244 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
246 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
248 return a
->GetLink( x
, y
);
249 return wxHtmlCell::GetLink( x
, y
);
252 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
254 if (cond
== wxHTML_COND_ISIMAGEMAP
)
256 if (m_Name
== *((wxString
*)(param
)))
259 return wxHtmlCell::Find(cond
, param
);
266 //--------------------------------------------------------------------------------
269 //--------------------------------------------------------------------------------
271 class wxHtmlImageCell
: public wxHtmlCell
276 wxHtmlImageMapCell
*m_ImageMap
;
279 wxHtmlImageCell(wxFSFile
*input
, int w
= -1, int h
= -1, double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
, wxString mapname
= wxEmptyString
);
280 ~wxHtmlImageCell() {if (m_Image
) delete m_Image
; }
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;
288 //--------------------------------------------------------------------------------
290 //--------------------------------------------------------------------------------
292 wxHtmlImageCell::wxHtmlImageCell(wxFSFile
*input
, int w
, int h
, double scale
, int align
, wxString mapname
) : wxHtmlCell()
296 wxInputStream
*s
= input
->GetStream();
299 img
= new wxImage(*s
, wxBITMAP_TYPE_ANY
);
301 if (img
&& (img
->Ok()))
303 ww
= img
->GetWidth();
304 hh
= img
->GetHeight();
305 if (w
!= -1) bw
= w
; else bw
= ww
;
306 if (h
!= -1) bh
= h
; else bh
= hh
;
308 m_Width
= (int)(scale
* (double)bw
);
309 m_Height
= (int)(scale
* (double)bh
);
311 if ((bw
!= ww
) || (bh
!= hh
))
313 wxImage img2
= img
->Scale(bw
, bh
);
314 m_Image
= new wxBitmap(img2
.ConvertToBitmap());
317 m_Image
= new wxBitmap(img
->ConvertToBitmap());
322 case wxHTML_ALIGN_TOP
:
323 m_Descent
= m_Height
;
325 case wxHTML_ALIGN_CENTER
:
326 m_Descent
= m_Height
/ 2;
328 case wxHTML_ALIGN_BOTTOM
:
336 SetCanLiveOnPagebreak(FALSE
);
341 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
346 dc
.GetUserScale(&us_x
, &us_y
);
347 dc
.SetUserScale(us_x
* m_Scale
, us_y
* m_Scale
);
349 // dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, (m_Image->GetMask() != (wxMask*) 0));
350 dc
.DrawBitmap(*m_Image
, (int) ((x
+ m_PosX
) / m_Scale
),
351 (int) ((y
+ m_PosY
) / m_Scale
), TRUE
);
352 dc
.SetUserScale(us_x
, us_y
);
354 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
357 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
359 if (m_MapName
.IsEmpty())
360 return wxHtmlCell::GetLink( x
, y
);
363 wxHtmlContainerCell
*p
, *op
;
364 op
= p
= GetParent();
371 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( wxHTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
374 ((wxString
&)m_MapName
).Clear();
375 return wxHtmlCell::GetLink( x
, y
);
377 { // dirty hack, ask Joel why he fills m_ImageMap in this place
378 // THE problem is that we're in const method and we can't modify m_ImageMap
379 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_ImageMap
);
380 *cx
= (wxHtmlImageMapCell
*)cell
;
383 return m_ImageMap
->GetLink( x
, y
);
388 //--------------------------------------------------------------------------------
390 //--------------------------------------------------------------------------------
392 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
394 TAG_HANDLER_PROC(tag
)
396 if (tag
.GetName() == wxT("IMG"))
398 if (tag
.HasParam(wxT("SRC")))
403 wxString tmp
= tag
.GetParam(wxT("SRC"));
404 wxString mn
= wxEmptyString
;
406 str
= m_WParser
->GetFS()->OpenFile(tmp
);
407 if (tag
.HasParam(wxT("WIDTH"))) tag
.ScanParam(wxT("WIDTH"), wxT("%i"), &w
);
408 if (tag
.HasParam(wxT("HEIGHT"))) tag
.ScanParam(wxT("HEIGHT"), wxT("%i"), &h
);
409 al
= wxHTML_ALIGN_BOTTOM
;
410 if (tag
.HasParam(wxT("ALIGN")))
412 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
413 alstr
.MakeUpper(); // for the case alignment was in ".."
414 if (alstr
== wxT("TEXTTOP")) al
= wxHTML_ALIGN_TOP
;
415 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER"))) al
= wxHTML_ALIGN_CENTER
;
417 if (tag
.HasParam(wxT("USEMAP")))
419 mn
= tag
.GetParam( wxT("USEMAP") );
420 if (mn
[ (unsigned int) 0 ] == wxT('#'))
425 wxHtmlImageCell
*cel
= NULL
;
428 cel
= new wxHtmlImageCell(str
, w
, h
, m_WParser
->GetPixelScale(), al
, mn
);
429 cel
->SetLink(m_WParser
->GetLink());
430 m_WParser
->GetContainer()->InsertCell(cel
);
435 if (tag
.GetName() == wxT("MAP"))
437 m_WParser
->CloseContainer();
438 m_WParser
->OpenContainer();
439 if (tag
.HasParam(wxT("NAME")))
441 wxString tmp
= tag
.GetParam(wxT("NAME"));
442 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
443 m_WParser
->GetContainer()->InsertCell( cel
);
446 m_WParser
->CloseContainer();
447 m_WParser
->OpenContainer();
449 if (tag
.GetName() == wxT("AREA"))
451 if (tag
.HasParam(wxT("SHAPE")))
453 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
454 wxString coords
= wxEmptyString
;
456 wxHtmlImageMapAreaCell
*cel
= NULL
;
457 if (tag
.HasParam(wxT("COORDS")))
459 coords
= tag
.GetParam(wxT("COORDS"));
461 if (tmp
== wxT("POLY"))
463 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
465 else if (tmp
== wxT("CIRCLE"))
467 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
469 else if (tmp
== wxT("RECT"))
471 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
473 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
475 wxString tmp
= tag
.GetParam(wxT("HREF"));
476 wxString target
= wxEmptyString
;
477 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
478 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
480 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
491 TAGS_MODULE_BEGIN(Image
)
495 TAGS_MODULE_END(Image
)