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