WX_PRECOMP, not WXPRECOMP
[wxWidgets.git] / src / html / m_image.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/m_image.cpp
3 // Purpose: wxHtml module for displaying images
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik, Joel Lucsy
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_HTML && wxUSE_STREAMS
17
18 #ifndef WX_PRECOMP
19 #include "wx/dynarray.h"
20 #include "wx/dc.h"
21 #include "wx/scrolwin.h"
22 #include "wx/timer.h"
23 #include "wx/dcmemory.h"
24 #include "wx/log.h"
25 #include "wx/math.h"
26 #include "wx/image.h"
27 #endif
28
29 #include "wx/html/forcelnk.h"
30 #include "wx/html/m_templ.h"
31 #include "wx/html/htmlwin.h"
32
33 #include "wx/gifdecod.h"
34 #include "wx/artprov.h"
35
36 #include <float.h>
37
38 FORCE_LINK_ME(m_image)
39
40
41
42
43 WX_DECLARE_OBJARRAY(int, CoordArray);
44 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
45 WX_DEFINE_OBJARRAY(CoordArray)
46
47
48 // ---------------------------------------------------------------------------
49 // wxHtmlImageMapAreaCell
50 // 0-width, 0-height cell that represents single area in
51 // imagemap (it's GetLink is called from wxHtmlImageCell's)
52 // ---------------------------------------------------------------------------
53
54 class wxHtmlImageMapAreaCell : public wxHtmlCell
55 {
56 public:
57 enum celltype { CIRCLE, RECT, POLY };
58 protected:
59 CoordArray coords;
60 celltype type;
61 int radius;
62 public:
63 wxHtmlImageMapAreaCell( celltype t, wxString &coords, double pixel_scale = 1.0);
64 virtual wxHtmlLinkInfo *GetLink( int x = 0, int y = 0 ) const;
65 void Draw(wxDC& WXUNUSED(dc),
66 int WXUNUSED(x), int WXUNUSED(y),
67 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
68 wxHtmlRenderingInfo& WXUNUSED(info)) {}
69
70
71 DECLARE_NO_COPY_CLASS(wxHtmlImageMapAreaCell)
72 };
73
74
75
76
77
78 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t, wxString &incoords, double pixel_scale )
79 {
80 int i;
81 wxString x = incoords, y;
82
83 type = t;
84 while ((i = x.Find( ',' )) != wxNOT_FOUND)
85 {
86 coords.Add( (int)(pixel_scale * (double)wxAtoi( x.Left( i ).c_str())) );
87 x = x.Mid( i + 1 );
88 }
89 coords.Add( (int)(pixel_scale * (double)wxAtoi( x.c_str())) );
90 }
91
92 wxHtmlLinkInfo *wxHtmlImageMapAreaCell::GetLink( int x, int y ) const
93 {
94 switch (type)
95 {
96 case RECT:
97 {
98 int l, t, r, b;
99
100 l = coords[ 0 ];
101 t = coords[ 1 ];
102 r = coords[ 2 ];
103 b = coords[ 3 ];
104 if (x >= l && x <= r && y >= t && y <= b)
105 {
106 return m_Link;
107 }
108 break;
109 }
110 case CIRCLE:
111 {
112 int l, t, r;
113 double d;
114
115 l = coords[ 0 ];
116 t = coords[ 1 ];
117 r = coords[ 2 ];
118 d = sqrt( (double) (((x - l) * (x - l)) + ((y - t) * (y - t))) );
119 if (d < (double)r)
120 {
121 return m_Link;
122 }
123 }
124 break;
125 case POLY:
126 {
127 if (coords.GetCount() >= 6)
128 {
129 int intersects = 0;
130 int wherex = x;
131 int wherey = y;
132 int totalv = coords.GetCount() / 2;
133 int totalc = totalv * 2;
134 int xval = coords[totalc - 2];
135 int yval = coords[totalc - 1];
136 int end = totalc;
137 int pointer = 1;
138
139 if ((yval >= wherey) != (coords[pointer] >= wherey))
140 {
141 if ((xval >= wherex) == (coords[0] >= wherex))
142 {
143 intersects += (xval >= wherex) ? 1 : 0;
144 }
145 else
146 {
147 intersects += ((xval - (yval - wherey) *
148 (coords[0] - xval) /
149 (coords[pointer] - yval)) >= wherex) ? 1 : 0;
150 }
151 }
152
153 while (pointer < end)
154 {
155 yval = coords[pointer];
156 pointer += 2;
157 if (yval >= wherey)
158 {
159 while ((pointer < end) && (coords[pointer] >= wherey))
160 {
161 pointer += 2;
162 }
163 if (pointer >= end)
164 {
165 break;
166 }
167 if ((coords[pointer - 3] >= wherex) ==
168 (coords[pointer - 1] >= wherex)) {
169 intersects += (coords[pointer - 3] >= wherex) ? 1 : 0;
170 }
171 else
172 {
173 intersects +=
174 ((coords[pointer - 3] - (coords[pointer - 2] - wherey) *
175 (coords[pointer - 1] - coords[pointer - 3]) /
176 (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1 : 0;
177 }
178 }
179 else
180 {
181 while ((pointer < end) && (coords[pointer] < wherey))
182 {
183 pointer += 2;
184 }
185 if (pointer >= end)
186 {
187 break;
188 }
189 if ((coords[pointer - 3] >= wherex) ==
190 (coords[pointer - 1] >= wherex))
191 {
192 intersects += (coords[pointer - 3] >= wherex) ? 1 : 0;
193 }
194 else
195 {
196 intersects +=
197 ((coords[pointer - 3] - (coords[pointer - 2] - wherey) *
198 (coords[pointer - 1] - coords[pointer - 3]) /
199 (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1 : 0;
200 }
201 }
202 }
203 if ((intersects & 1) != 0)
204 {
205 return m_Link;
206 }
207 }
208 }
209 break;
210 }
211
212 if (m_Next)
213 {
214 wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next;
215 return a->GetLink( x, y );
216 }
217 return NULL;
218 }
219
220
221
222
223
224
225
226
227 //--------------------------------------------------------------------------------
228 // wxHtmlImageMapCell
229 // 0-width, 0-height cell that represents map from imagemaps
230 // it is always placed before wxHtmlImageMapAreaCells
231 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
232 //--------------------------------------------------------------------------------
233
234
235 class wxHtmlImageMapCell : public wxHtmlCell
236 {
237 public:
238 wxHtmlImageMapCell( wxString &name );
239 protected:
240 wxString m_Name;
241 public:
242 virtual wxHtmlLinkInfo *GetLink( int x = 0, int y = 0 ) const;
243 virtual const wxHtmlCell *Find( int cond, const void *param ) const;
244 void Draw(wxDC& WXUNUSED(dc),
245 int WXUNUSED(x), int WXUNUSED(y),
246 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
247 wxHtmlRenderingInfo& WXUNUSED(info)) {}
248
249 DECLARE_NO_COPY_CLASS(wxHtmlImageMapCell)
250 };
251
252
253 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString &name )
254 {
255 m_Name = name ;
256 }
257
258 wxHtmlLinkInfo *wxHtmlImageMapCell::GetLink( int x, int y ) const
259 {
260 wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next;
261 if (a)
262 return a->GetLink( x, y );
263 return wxHtmlCell::GetLink( x, y );
264 }
265
266 const wxHtmlCell *wxHtmlImageMapCell::Find( int cond, const void *param ) const
267 {
268 if (cond == wxHTML_COND_ISIMAGEMAP)
269 {
270 if (m_Name == *((wxString*)(param)))
271 return this;
272 }
273 return wxHtmlCell::Find(cond, param);
274 }
275
276
277
278
279
280 //--------------------------------------------------------------------------------
281 // wxHtmlImageCell
282 // Image/bitmap
283 //--------------------------------------------------------------------------------
284
285 class wxHtmlImageCell : public wxHtmlCell
286 {
287 public:
288 wxHtmlImageCell(wxHtmlWindowInterface *windowIface,
289 wxFSFile *input, int w = wxDefaultCoord, int h = wxDefaultCoord,
290 double scale = 1.0, int align = wxHTML_ALIGN_BOTTOM,
291 const wxString& mapname = wxEmptyString);
292 virtual ~wxHtmlImageCell();
293 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
294 wxHtmlRenderingInfo& info);
295 virtual wxHtmlLinkInfo *GetLink(int x = 0, int y = 0) const;
296
297 void SetImage(const wxImage& img);
298 #if wxUSE_GIF && wxUSE_TIMER
299 void AdvanceAnimation(wxTimer *timer);
300 virtual void Layout(int w);
301 #endif
302
303 private:
304 wxBitmap *m_bitmap;
305 int m_bmpW, m_bmpH;
306 bool m_showFrame:1;
307 wxHtmlWindowInterface *m_windowIface;
308 #if wxUSE_GIF && wxUSE_TIMER
309 wxGIFDecoder *m_gifDecoder;
310 wxTimer *m_gifTimer;
311 int m_physX, m_physY;
312 size_t m_nCurrFrame;
313 #endif
314 double m_scale;
315 wxHtmlImageMapCell *m_imageMap;
316 wxString m_mapName;
317
318 DECLARE_NO_COPY_CLASS(wxHtmlImageCell)
319 };
320
321 #if wxUSE_GIF && wxUSE_TIMER
322 class wxGIFTimer : public wxTimer
323 {
324 public:
325 wxGIFTimer(wxHtmlImageCell *cell) : m_cell(cell) {}
326 virtual void Notify()
327 {
328 m_cell->AdvanceAnimation(this);
329 }
330
331 private:
332 wxHtmlImageCell *m_cell;
333
334 DECLARE_NO_COPY_CLASS(wxGIFTimer)
335 };
336 #endif
337
338
339 //----------------------------------------------------------------------------
340 // wxHtmlImageCell
341 //----------------------------------------------------------------------------
342
343
344 wxHtmlImageCell::wxHtmlImageCell(wxHtmlWindowInterface *windowIface,
345 wxFSFile *input,
346 int w, int h, double scale, int align,
347 const wxString& mapname) : wxHtmlCell()
348 {
349 m_windowIface = windowIface;
350 m_scale = scale;
351 m_showFrame = false;
352 m_bitmap = NULL;
353 m_bmpW = w;
354 m_bmpH = h;
355 m_imageMap = NULL;
356 m_mapName = mapname;
357 SetCanLiveOnPagebreak(false);
358 #if wxUSE_GIF && wxUSE_TIMER
359 m_gifDecoder = NULL;
360 m_gifTimer = NULL;
361 m_physX = m_physY = wxDefaultCoord;
362 m_nCurrFrame = 0;
363 #endif
364
365 if ( m_bmpW && m_bmpH )
366 {
367 if ( input )
368 {
369 wxInputStream *s = input->GetStream();
370
371 if ( s )
372 {
373 #if wxUSE_GIF && wxUSE_TIMER
374 bool readImg = true;
375 if ( m_windowIface &&
376 (input->GetLocation().Matches(wxT("*.gif")) ||
377 input->GetLocation().Matches(wxT("*.GIF"))) )
378 {
379 m_gifDecoder = new wxGIFDecoder();
380 if ( m_gifDecoder->LoadGIF(*s) == wxGIF_OK )
381 {
382 wxImage img;
383 if ( m_gifDecoder->ConvertToImage(0, &img) )
384 SetImage(img);
385
386 readImg = false;
387
388 if ( m_gifDecoder->IsAnimation() )
389 {
390 m_gifTimer = new wxGIFTimer(this);
391 m_gifTimer->Start(m_gifDecoder->GetDelay(0), true);
392 }
393 else
394 {
395 wxDELETE(m_gifDecoder);
396 }
397 }
398 else
399 {
400 wxDELETE(m_gifDecoder);
401 }
402 }
403
404 if ( readImg )
405 #endif // wxUSE_GIF && wxUSE_TIMER
406 {
407 wxImage image(*s, wxBITMAP_TYPE_ANY);
408 if ( image.Ok() )
409 SetImage(image);
410 }
411 }
412 }
413 else // input==NULL, use "broken image" bitmap
414 {
415 if ( m_bmpW == wxDefaultCoord && m_bmpH == wxDefaultCoord )
416 {
417 m_bmpW = 29;
418 m_bmpH = 31;
419 }
420 else
421 {
422 m_showFrame = true;
423 if ( m_bmpW == wxDefaultCoord ) m_bmpW = 31;
424 if ( m_bmpH == wxDefaultCoord ) m_bmpH = 33;
425 }
426 m_bitmap =
427 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
428 }
429 }
430 //else: ignore the 0-sized images used sometimes on the Web pages
431
432 m_Width = (int)(scale * (double)m_bmpW);
433 m_Height = (int)(scale * (double)m_bmpH);
434
435 switch (align)
436 {
437 case wxHTML_ALIGN_TOP :
438 m_Descent = m_Height;
439 break;
440 case wxHTML_ALIGN_CENTER :
441 m_Descent = m_Height / 2;
442 break;
443 case wxHTML_ALIGN_BOTTOM :
444 default :
445 m_Descent = 0;
446 break;
447 }
448 }
449
450 void wxHtmlImageCell::SetImage(const wxImage& img)
451 {
452 #if !defined(__WXMSW__) || wxUSE_WXDIB
453 if ( img.Ok() )
454 {
455 delete m_bitmap;
456
457 int ww, hh;
458 ww = img.GetWidth();
459 hh = img.GetHeight();
460
461 if ( m_bmpW == wxDefaultCoord )
462 m_bmpW = ww;
463 if ( m_bmpH == wxDefaultCoord )
464 m_bmpH = hh;
465
466 // Only scale the bitmap at the rendering stage,
467 // so we don't lose quality twice
468 /*
469 if ((m_bmpW != ww) || (m_bmpH != hh))
470 {
471 wxImage img2 = img.Scale(m_bmpW, m_bmpH);
472 m_bitmap = new wxBitmap(img2);
473 }
474 else
475 */
476 m_bitmap = new wxBitmap(img);
477 }
478 #endif
479 }
480
481 #if wxUSE_GIF && wxUSE_TIMER
482 void wxHtmlImageCell::AdvanceAnimation(wxTimer *timer)
483 {
484 wxImage img;
485
486 // advance current frame
487 m_nCurrFrame++;
488 if (m_nCurrFrame == m_gifDecoder->GetFrameCount())
489 m_nCurrFrame = 0;
490
491 if ( m_physX == wxDefaultCoord )
492 {
493 m_physX = m_physY = 0;
494 for (wxHtmlCell *cell = this; cell; cell = cell->GetParent())
495 {
496 m_physX += cell->GetPosX();
497 m_physY += cell->GetPosY();
498 }
499 }
500
501 wxWindow *win = m_windowIface->GetHTMLWindow();
502 wxPoint pos =
503 m_windowIface->HTMLCoordsToWindow(this, wxPoint(m_physX, m_physY));
504 wxRect rect(pos, wxSize(m_Width, m_Height));
505
506 if ( win->GetClientRect().Intersects(rect) &&
507 m_gifDecoder->ConvertToImage(m_nCurrFrame, &img) )
508 {
509 #if !defined(__WXMSW__) || wxUSE_WXDIB
510 if ( m_gifDecoder->GetFrameSize(m_nCurrFrame) != wxSize(m_Width, m_Height) ||
511 m_gifDecoder->GetFramePosition(m_nCurrFrame) != wxPoint(0, 0) )
512 {
513 wxBitmap bmp(img);
514 wxMemoryDC dc;
515 dc.SelectObject(*m_bitmap);
516 dc.DrawBitmap(bmp, m_gifDecoder->GetFramePosition(m_nCurrFrame),
517 true /* use mask */);
518 }
519 else
520 #endif
521 SetImage(img);
522 win->Refresh(img.HasMask(), &rect);
523 }
524
525 timer->Start(m_gifDecoder->GetDelay(m_nCurrFrame), true);
526 }
527
528 void wxHtmlImageCell::Layout(int w)
529 {
530 wxHtmlCell::Layout(w);
531 m_physX = m_physY = wxDefaultCoord;
532 }
533
534 #endif
535
536 wxHtmlImageCell::~wxHtmlImageCell()
537 {
538 delete m_bitmap;
539 #if wxUSE_GIF && wxUSE_TIMER
540 delete m_gifTimer;
541 delete m_gifDecoder;
542 #endif
543 }
544
545
546 void wxHtmlImageCell::Draw(wxDC& dc, int x, int y,
547 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
548 wxHtmlRenderingInfo& WXUNUSED(info))
549 {
550 if ( m_showFrame )
551 {
552 dc.SetBrush(*wxTRANSPARENT_BRUSH);
553 dc.SetPen(*wxBLACK_PEN);
554 dc.DrawRectangle(x + m_PosX, y + m_PosY, m_Width, m_Height);
555 x++, y++;
556 }
557 if ( m_bitmap )
558 {
559 // We add in the scaling from the desired bitmap width
560 // and height, so we only do the scaling once.
561 double imageScaleX = 1.0;
562 double imageScaleY = 1.0;
563 if (m_bmpW != m_bitmap->GetWidth())
564 imageScaleX = (double) m_bmpW / (double) m_bitmap->GetWidth();
565 if (m_bmpH != m_bitmap->GetHeight())
566 imageScaleY = (double) m_bmpH / (double) m_bitmap->GetHeight();
567
568 double us_x, us_y;
569 dc.GetUserScale(&us_x, &us_y);
570 dc.SetUserScale(us_x * m_scale * imageScaleX, us_y * m_scale * imageScaleY);
571
572 dc.DrawBitmap(*m_bitmap, (int) ((x + m_PosX) / (m_scale*imageScaleX)),
573 (int) ((y + m_PosY) / (m_scale*imageScaleY)), true);
574 dc.SetUserScale(us_x, us_y);
575 }
576 }
577
578 wxHtmlLinkInfo *wxHtmlImageCell::GetLink( int x, int y ) const
579 {
580 if (m_mapName.empty())
581 return wxHtmlCell::GetLink( x, y );
582 if (!m_imageMap)
583 {
584 wxHtmlContainerCell *p, *op;
585 op = p = GetParent();
586 while (p)
587 {
588 op = p;
589 p = p->GetParent();
590 }
591 p = op;
592 wxHtmlCell *cell = (wxHtmlCell*)p->Find(wxHTML_COND_ISIMAGEMAP,
593 (const void*)(&m_mapName));
594 if (!cell)
595 {
596 ((wxString&)m_mapName).Clear();
597 return wxHtmlCell::GetLink( x, y );
598 }
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;
603 }
604 }
605 return m_imageMap->GetLink(x, y);
606 }
607
608
609
610 //--------------------------------------------------------------------------------
611 // tag handler
612 //--------------------------------------------------------------------------------
613
614 TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA")
615 TAG_HANDLER_CONSTR(IMG) { }
616
617 TAG_HANDLER_PROC(tag)
618 {
619 if (tag.GetName() == wxT("IMG"))
620 {
621 if (tag.HasParam(wxT("SRC")))
622 {
623 int w = wxDefaultCoord, h = wxDefaultCoord;
624 int al;
625 wxFSFile *str;
626 wxString tmp = tag.GetParam(wxT("SRC"));
627 wxString mn = wxEmptyString;
628
629 str = m_WParser->OpenURL(wxHTML_URL_IMAGE, tmp);
630
631 if (tag.HasParam(wxT("WIDTH")))
632 tag.GetParamAsInt(wxT("WIDTH"), &w);
633 if (tag.HasParam(wxT("HEIGHT")))
634 tag.GetParamAsInt(wxT("HEIGHT"), &h);
635 al = wxHTML_ALIGN_BOTTOM;
636 if (tag.HasParam(wxT("ALIGN")))
637 {
638 wxString alstr = tag.GetParam(wxT("ALIGN"));
639 alstr.MakeUpper(); // for the case alignment was in ".."
640 if (alstr == wxT("TEXTTOP"))
641 al = wxHTML_ALIGN_TOP;
642 else if ((alstr == wxT("CENTER")) || (alstr == wxT("ABSCENTER")))
643 al = wxHTML_ALIGN_CENTER;
644 }
645 if (tag.HasParam(wxT("USEMAP")))
646 {
647 mn = tag.GetParam( wxT("USEMAP") );
648 if (mn.GetChar(0) == wxT('#'))
649 {
650 mn = mn.Mid( 1 );
651 }
652 }
653 wxHtmlImageCell *cel = new wxHtmlImageCell(
654 m_WParser->GetWindowInterface(),
655 str, w, h,
656 m_WParser->GetPixelScale(),
657 al, mn);
658 m_WParser->ApplyStateToCell(cel);
659 cel->SetId(tag.GetParam(wxT("id"))); // may be empty
660 m_WParser->GetContainer()->InsertCell(cel);
661 if (str)
662 delete str;
663 }
664 }
665 if (tag.GetName() == wxT("MAP"))
666 {
667 m_WParser->CloseContainer();
668 m_WParser->OpenContainer();
669 if (tag.HasParam(wxT("NAME")))
670 {
671 wxString tmp = tag.GetParam(wxT("NAME"));
672 wxHtmlImageMapCell *cel = new wxHtmlImageMapCell( tmp );
673 m_WParser->GetContainer()->InsertCell( cel );
674 }
675 ParseInner( tag );
676 m_WParser->CloseContainer();
677 m_WParser->OpenContainer();
678 }
679 if (tag.GetName() == wxT("AREA"))
680 {
681 if (tag.HasParam(wxT("SHAPE")))
682 {
683 wxString tmp = tag.GetParam(wxT("SHAPE"));
684 wxString coords = wxEmptyString;
685 tmp.MakeUpper();
686 wxHtmlImageMapAreaCell *cel = NULL;
687 if (tag.HasParam(wxT("COORDS")))
688 {
689 coords = tag.GetParam(wxT("COORDS"));
690 }
691 if (tmp == wxT("POLY"))
692 {
693 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY, coords, m_WParser->GetPixelScale() );
694 }
695 else if (tmp == wxT("CIRCLE"))
696 {
697 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE, coords, m_WParser->GetPixelScale() );
698 }
699 else if (tmp == wxT("RECT"))
700 {
701 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT, coords, m_WParser->GetPixelScale() );
702 }
703 if (cel != NULL && tag.HasParam(wxT("HREF")))
704 {
705 wxString target;
706 if (tag.HasParam(wxT("TARGET")))
707 target = tag.GetParam(wxT("TARGET"));
708 cel->SetLink(wxHtmlLinkInfo(tag.GetParam(wxT("HREF")), target));
709 }
710 if (cel != NULL)
711 m_WParser->GetContainer()->InsertCell( cel );
712 }
713 }
714
715 return false;
716 }
717
718 TAG_HANDLER_END(IMG)
719
720
721
722 TAGS_MODULE_BEGIN(Image)
723
724 TAGS_MODULE_ADD(IMG)
725
726 TAGS_MODULE_END(Image)
727
728
729 #endif