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