]>
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"
31 #include "wx/dynarray.h"
36 FORCE_LINK_ME(m_image
)
41 WX_DECLARE_OBJARRAY(int, CoordArray
);
42 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
43 WX_DEFINE_OBJARRAY(CoordArray
);
46 //--------------------------------------------------------------------------------
47 // wxHtmlImageMapAreaCell
48 // 0-width, 0-height cell that represents single area in imagemap
49 // (it's GetLink is called from wxHtmlImageCell's)
50 //--------------------------------------------------------------------------------
52 class wxHtmlImageMapAreaCell
: public wxHtmlCell
55 enum celltype
{ CIRCLE
, RECT
, POLY
};
61 wxHtmlImageMapAreaCell( celltype t
, wxString
&coords
, double pixel_scale
= 1.0);
62 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
69 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t
, wxString
&incoords
, double pixel_scale
)
72 wxString x
= incoords
, y
;
75 while ((i
= x
.Find( ',' )) != -1)
77 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.Left( i
).c_str())) );
80 coords
.Add( (int)(pixel_scale
* (double)wxAtoi( x
.c_str())) );
83 wxHtmlLinkInfo
*wxHtmlImageMapAreaCell::GetLink( int x
, int y
) const
95 if (x
>= l
&& x
<= r
&& y
>= t
&& y
<= b
)
109 d
= sqrt( (double) (((x
- l
) * (x
- l
)) + ((y
- t
) * (y
- t
))) );
118 if (coords
.GetCount() >= 6)
123 int totalv
= coords
.GetCount() / 2;
124 int totalc
= totalv
* 2;
125 int xval
= coords
[totalc
- 2];
126 int yval
= coords
[totalc
- 1];
130 if ((yval
>= wherey
) != (coords
[pointer
] >= wherey
))
132 if ((xval
>= wherex
) == (coords
[0] >= wherex
))
134 intersects
+= (xval
>= wherex
) ? 1 : 0;
138 intersects
+= ((xval
- (yval
- wherey
) *
140 (coords
[pointer
] - yval
)) >= wherex
) ? 1 : 0;
144 while (pointer
< end
)
146 yval
= coords
[pointer
];
150 while ((pointer
< end
) && (coords
[pointer
] >= wherey
))
158 if ((coords
[pointer
- 3] >= wherex
) ==
159 (coords
[pointer
- 1] >= wherex
)) {
160 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
165 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
166 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
167 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
172 while ((pointer
< end
) && (coords
[pointer
] < wherey
))
180 if ((coords
[pointer
- 3] >= wherex
) ==
181 (coords
[pointer
- 1] >= wherex
))
183 intersects
+= (coords
[pointer
- 3] >= wherex
) ? 1 : 0;
188 ((coords
[pointer
- 3] - (coords
[pointer
- 2] - wherey
) *
189 (coords
[pointer
- 1] - coords
[pointer
- 3]) /
190 (coords
[pointer
] - coords
[pointer
- 2])) >= wherex
) ? 1 : 0;
194 if ((intersects
& 1) != 0)
205 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
206 return a
->GetLink( x
, y
);
218 //--------------------------------------------------------------------------------
219 // wxHtmlImageMapCell
220 // 0-width, 0-height cell that represents map from imagemaps
221 // it is always placed before wxHtmlImageMapAreaCells
222 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
223 //--------------------------------------------------------------------------------
226 class wxHtmlImageMapCell
: public wxHtmlCell
229 wxHtmlImageMapCell( wxString
&name
);
233 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
234 virtual const wxHtmlCell
*Find( int cond
, const void *param
) const;
238 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString
&name
)
243 wxHtmlLinkInfo
*wxHtmlImageMapCell::GetLink( int x
, int y
) const
245 wxHtmlImageMapAreaCell
*a
= (wxHtmlImageMapAreaCell
*)m_Next
;
247 return a
->GetLink( x
, y
);
248 return wxHtmlCell::GetLink( x
, y
);
251 const wxHtmlCell
*wxHtmlImageMapCell::Find( int cond
, const void *param
) const
253 if (cond
== wxHTML_COND_ISIMAGEMAP
)
255 if (m_Name
== *((wxString
*)(param
)))
258 return wxHtmlCell::Find(cond
, param
);
265 //--------------------------------------------------------------------------------
268 //--------------------------------------------------------------------------------
270 class wxHtmlImageCell
: public wxHtmlCell
275 wxHtmlImageMapCell
*m_ImageMap
;
278 wxHtmlImageCell(wxFSFile
*input
, int w
= -1, int h
= -1, double scale
= 1.0, int align
= wxHTML_ALIGN_BOTTOM
, wxString mapname
= wxEmptyString
);
279 ~wxHtmlImageCell() {if (m_Image
) delete m_Image
; }
280 void Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
);
281 virtual wxHtmlLinkInfo
*GetLink( int x
= 0, int y
= 0 ) const;
287 //--------------------------------------------------------------------------------
289 //--------------------------------------------------------------------------------
291 wxHtmlImageCell::wxHtmlImageCell(wxFSFile
*input
, int w
, int h
, double scale
, int align
, wxString mapname
) : wxHtmlCell()
295 wxInputStream
*s
= input
->GetStream();
298 img
= new wxImage(*s
, wxBITMAP_TYPE_ANY
);
300 if (img
&& (img
->Ok()))
302 ww
= img
->GetWidth();
303 hh
= img
->GetHeight();
304 if (w
!= -1) bw
= w
; else bw
= ww
;
305 if (h
!= -1) bh
= h
; else bh
= hh
;
307 m_Width
= (int)(scale
* (double)bw
);
308 m_Height
= (int)(scale
* (double)bh
);
310 if ((bw
!= ww
) || (bh
!= hh
))
312 wxImage img2
= img
->Scale(bw
, bh
);
313 m_Image
= new wxBitmap(img2
.ConvertToBitmap());
316 m_Image
= new wxBitmap(img
->ConvertToBitmap());
321 case wxHTML_ALIGN_TOP
:
322 m_Descent
= m_Height
;
324 case wxHTML_ALIGN_CENTER
:
325 m_Descent
= m_Height
/ 2;
327 case wxHTML_ALIGN_BOTTOM
:
335 SetCanLiveOnPagebreak(FALSE
);
340 void wxHtmlImageCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
345 dc
.GetUserScale(&us_x
, &us_y
);
346 dc
.SetUserScale(us_x
* m_Scale
, us_y
* m_Scale
);
348 // dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, (m_Image->GetMask() != (wxMask*) 0));
349 dc
.DrawBitmap(*m_Image
, (int) ((x
+ m_PosX
) / m_Scale
),
350 (int) ((y
+ m_PosY
) / m_Scale
), TRUE
);
351 dc
.SetUserScale(us_x
, us_y
);
355 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
357 if (m_MapName
.IsEmpty())
358 return wxHtmlCell::GetLink( x
, y
);
361 wxHtmlContainerCell
*p
, *op
;
362 op
= p
= GetParent();
369 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( wxHTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
372 ((wxString
&)m_MapName
).Clear();
373 return wxHtmlCell::GetLink( x
, y
);
375 { // dirty hack, ask Joel why he fills m_ImageMap in this place
376 // THE problem is that we're in const method and we can't modify m_ImageMap
377 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_ImageMap
);
378 *cx
= (wxHtmlImageMapCell
*)cell
;
381 return m_ImageMap
->GetLink( x
, y
);
386 //--------------------------------------------------------------------------------
388 //--------------------------------------------------------------------------------
390 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
392 TAG_HANDLER_PROC(tag
)
394 if (tag
.GetName() == wxT("IMG"))
396 if (tag
.HasParam(wxT("SRC")))
401 wxString tmp
= tag
.GetParam(wxT("SRC"));
402 wxString mn
= wxEmptyString
;
404 str
= m_WParser
->GetFS()->OpenFile(tmp
);
405 if (tag
.HasParam(wxT("WIDTH")))
406 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
407 if (tag
.HasParam(wxT("HEIGHT")))
408 tag
.GetParamAsInt(wxT("HEIGHT"), &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"))
415 al
= wxHTML_ALIGN_TOP
;
416 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
417 al
= wxHTML_ALIGN_CENTER
;
419 if (tag
.HasParam(wxT("USEMAP")))
421 mn
= tag
.GetParam( wxT("USEMAP") );
422 if (mn
.GetChar(0) == wxT('#'))
427 wxHtmlImageCell
*cel
= NULL
;
430 cel
= new wxHtmlImageCell(str
, w
, h
, m_WParser
->GetPixelScale(), al
, mn
);
431 cel
->SetLink(m_WParser
->GetLink());
432 m_WParser
->GetContainer()->InsertCell(cel
);
437 if (tag
.GetName() == wxT("MAP"))
439 m_WParser
->CloseContainer();
440 m_WParser
->OpenContainer();
441 if (tag
.HasParam(wxT("NAME")))
443 wxString tmp
= tag
.GetParam(wxT("NAME"));
444 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
445 m_WParser
->GetContainer()->InsertCell( cel
);
448 m_WParser
->CloseContainer();
449 m_WParser
->OpenContainer();
451 if (tag
.GetName() == wxT("AREA"))
453 if (tag
.HasParam(wxT("SHAPE")))
455 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
456 wxString coords
= wxEmptyString
;
458 wxHtmlImageMapAreaCell
*cel
= NULL
;
459 if (tag
.HasParam(wxT("COORDS")))
461 coords
= tag
.GetParam(wxT("COORDS"));
463 if (tmp
== wxT("POLY"))
465 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
467 else if (tmp
== wxT("CIRCLE"))
469 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
471 else if (tmp
== wxT("RECT"))
473 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
475 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
477 wxString tmp
= tag
.GetParam(wxT("HREF"));
478 wxString target
= wxEmptyString
;
479 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
480 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
482 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
493 TAGS_MODULE_BEGIN(Image
)
497 TAGS_MODULE_END(Image
)