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