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