]>
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 view_y1
, int 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
);
353 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
356 wxHtmlLinkInfo
*wxHtmlImageCell::GetLink( int x
, int y
) const
358 if (m_MapName
.IsEmpty())
359 return wxHtmlCell::GetLink( x
, y
);
362 wxHtmlContainerCell
*p
, *op
;
363 op
= p
= GetParent();
370 wxHtmlCell
*cell
= (wxHtmlCell
*)p
->Find( wxHTML_COND_ISIMAGEMAP
, (const void*)(&m_MapName
));
373 ((wxString
&)m_MapName
).Clear();
374 return wxHtmlCell::GetLink( x
, y
);
376 { // dirty hack, ask Joel why he fills m_ImageMap in this place
377 // THE problem is that we're in const method and we can't modify m_ImageMap
378 wxHtmlImageMapCell
**cx
= (wxHtmlImageMapCell
**)(&m_ImageMap
);
379 *cx
= (wxHtmlImageMapCell
*)cell
;
382 return m_ImageMap
->GetLink( x
, y
);
387 //--------------------------------------------------------------------------------
389 //--------------------------------------------------------------------------------
391 TAG_HANDLER_BEGIN(IMG
, "IMG,MAP,AREA")
393 TAG_HANDLER_PROC(tag
)
395 if (tag
.GetName() == wxT("IMG"))
397 if (tag
.HasParam(wxT("SRC")))
402 wxString tmp
= tag
.GetParam(wxT("SRC"));
403 wxString mn
= wxEmptyString
;
405 str
= m_WParser
->GetFS()->OpenFile(tmp
);
406 if (tag
.HasParam(wxT("WIDTH")))
407 tag
.GetParamAsInt(wxT("WIDTH"), &w
);
408 if (tag
.HasParam(wxT("HEIGHT")))
409 tag
.GetParamAsInt(wxT("HEIGHT"), &h
);
410 al
= wxHTML_ALIGN_BOTTOM
;
411 if (tag
.HasParam(wxT("ALIGN")))
413 wxString alstr
= tag
.GetParam(wxT("ALIGN"));
414 alstr
.MakeUpper(); // for the case alignment was in ".."
415 if (alstr
== wxT("TEXTTOP"))
416 al
= wxHTML_ALIGN_TOP
;
417 else if ((alstr
== wxT("CENTER")) || (alstr
== wxT("ABSCENTER")))
418 al
= wxHTML_ALIGN_CENTER
;
420 if (tag
.HasParam(wxT("USEMAP")))
422 mn
= tag
.GetParam( wxT("USEMAP") );
423 if (mn
.GetChar(0) == wxT('#'))
428 wxHtmlImageCell
*cel
= NULL
;
431 cel
= new wxHtmlImageCell(str
, w
, h
, m_WParser
->GetPixelScale(), al
, mn
);
432 cel
->SetLink(m_WParser
->GetLink());
433 m_WParser
->GetContainer()->InsertCell(cel
);
438 if (tag
.GetName() == wxT("MAP"))
440 m_WParser
->CloseContainer();
441 m_WParser
->OpenContainer();
442 if (tag
.HasParam(wxT("NAME")))
444 wxString tmp
= tag
.GetParam(wxT("NAME"));
445 wxHtmlImageMapCell
*cel
= new wxHtmlImageMapCell( tmp
);
446 m_WParser
->GetContainer()->InsertCell( cel
);
449 m_WParser
->CloseContainer();
450 m_WParser
->OpenContainer();
452 if (tag
.GetName() == wxT("AREA"))
454 if (tag
.HasParam(wxT("SHAPE")))
456 wxString tmp
= tag
.GetParam(wxT("SHAPE"));
457 wxString coords
= wxEmptyString
;
459 wxHtmlImageMapAreaCell
*cel
= NULL
;
460 if (tag
.HasParam(wxT("COORDS")))
462 coords
= tag
.GetParam(wxT("COORDS"));
464 if (tmp
== wxT("POLY"))
466 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY
, coords
, m_WParser
->GetPixelScale() );
468 else if (tmp
== wxT("CIRCLE"))
470 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE
, coords
, m_WParser
->GetPixelScale() );
472 else if (tmp
== wxT("RECT"))
474 cel
= new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT
, coords
, m_WParser
->GetPixelScale() );
476 if (cel
!= NULL
&& tag
.HasParam(wxT("HREF")))
478 wxString tmp
= tag
.GetParam(wxT("HREF"));
479 wxString target
= wxEmptyString
;
480 if (tag
.HasParam(wxT("TARGET"))) target
= tag
.GetParam(wxT("TARGET"));
481 cel
->SetLink( wxHtmlLinkInfo(tmp
, target
));
483 if (cel
!= NULL
) m_WParser
->GetContainer()->InsertCell( cel
);
494 TAGS_MODULE_BEGIN(Image
)
498 TAGS_MODULE_END(Image
)