]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_image.cpp
wxAUI: Made fixed size floating panes work
[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 WXPRECOMP
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 ~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 #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(wxHtmlWindowInterface *windowIface,
344 wxFSFile *input,
345 int w, int h, double scale, int align,
346 const wxString& mapname) : wxHtmlCell()
347 {
348 m_windowIface = windowIface;
349 m_scale = scale;
350 m_showFrame = false;
351 m_bitmap = NULL;
352 m_bmpW = w;
353 m_bmpH = h;
354 m_imageMap = NULL;
355 m_mapName = mapname;
356 SetCanLiveOnPagebreak(false);
357 #if wxUSE_GIF && wxUSE_TIMER
358 m_gifDecoder = NULL;
359 m_gifTimer = NULL;
360 m_physX = m_physY = wxDefaultCoord;
361 #endif
362
363 if ( m_bmpW && m_bmpH )
364 {
365 if ( input )
366 {
367 wxInputStream *s = input->GetStream();
368
369 if ( s )
370 {
371 #if wxUSE_GIF && wxUSE_TIMER
372 bool readImg = true;
373 if ( m_windowIface &&
374 (input->GetLocation().Matches(wxT("*.gif")) ||
375 input->GetLocation().Matches(wxT("*.GIF"))) )
376 {
377 m_gifDecoder = new wxGIFDecoder(s, true);
378 if ( m_gifDecoder->ReadGIF() == wxGIF_OK )
379 {
380 wxImage img;
381 if ( m_gifDecoder->ConvertToImage(&img) )
382 SetImage(img);
383
384 readImg = false;
385
386 if ( m_gifDecoder->IsAnimation() )
387 {
388 m_gifTimer = new wxGIFTimer(this);
389 m_gifTimer->Start(m_gifDecoder->GetDelay(), true);
390 }
391 else
392 {
393 wxDELETE(m_gifDecoder);
394 }
395 }
396 else
397 {
398 wxDELETE(m_gifDecoder);
399 }
400 }
401
402 if ( readImg )
403 #endif // wxUSE_GIF && wxUSE_TIMER
404 {
405 wxImage image(*s, wxBITMAP_TYPE_ANY);
406 if ( image.Ok() )
407 SetImage(image);
408 }
409 }
410 }
411 else // input==NULL, use "broken image" bitmap
412 {
413 if ( m_bmpW == wxDefaultCoord && m_bmpH == wxDefaultCoord )
414 {
415 m_bmpW = 29;
416 m_bmpH = 31;
417 }
418 else
419 {
420 m_showFrame = true;
421 if ( m_bmpW == wxDefaultCoord ) m_bmpW = 31;
422 if ( m_bmpH == wxDefaultCoord ) m_bmpH = 33;
423 }
424 m_bitmap =
425 new wxBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
426 }
427 }
428 //else: ignore the 0-sized images used sometimes on the Web pages
429
430 m_Width = (int)(scale * (double)m_bmpW);
431 m_Height = (int)(scale * (double)m_bmpH);
432
433 switch (align)
434 {
435 case wxHTML_ALIGN_TOP :
436 m_Descent = m_Height;
437 break;
438 case wxHTML_ALIGN_CENTER :
439 m_Descent = m_Height / 2;
440 break;
441 case wxHTML_ALIGN_BOTTOM :
442 default :
443 m_Descent = 0;
444 break;
445 }
446 }
447
448 void wxHtmlImageCell::SetImage(const wxImage& img)
449 {
450 #if !defined(__WXMSW__) || wxUSE_WXDIB
451 if ( img.Ok() )
452 {
453 delete m_bitmap;
454
455 int ww, hh;
456 ww = img.GetWidth();
457 hh = img.GetHeight();
458
459 if ( m_bmpW == wxDefaultCoord )
460 m_bmpW = ww;
461 if ( m_bmpH == wxDefaultCoord )
462 m_bmpH = hh;
463
464 // Only scale the bitmap at the rendering stage,
465 // so we don't lose quality twice
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 */
474 m_bitmap = new wxBitmap(img);
475 }
476 #endif
477 }
478
479 #if wxUSE_GIF && wxUSE_TIMER
480 void wxHtmlImageCell::AdvanceAnimation(wxTimer *timer)
481 {
482 wxImage img;
483
484 m_gifDecoder->GoNextFrame(true);
485
486 if ( m_physX == wxDefaultCoord )
487 {
488 m_physX = m_physY = 0;
489 for (wxHtmlCell *cell = this; cell; cell = cell->GetParent())
490 {
491 m_physX += cell->GetPosX();
492 m_physY += cell->GetPosY();
493 }
494 }
495
496 wxWindow *win = m_windowIface->GetHTMLWindow();
497 wxPoint pos =
498 m_windowIface->HTMLCoordsToWindow(this, wxPoint(m_physX, m_physY));
499 wxRect rect(pos, wxSize(m_Width, m_Height));
500
501 if ( win->GetClientRect().Intersects(rect) &&
502 m_gifDecoder->ConvertToImage(&img) )
503 {
504 #if !defined(__WXMSW__) || wxUSE_WXDIB
505 if ( (int)m_gifDecoder->GetWidth() != m_Width ||
506 (int)m_gifDecoder->GetHeight() != m_Height ||
507 m_gifDecoder->GetLeft() != 0 || m_gifDecoder->GetTop() != 0 )
508 {
509 wxBitmap bmp(img);
510 wxMemoryDC dc;
511 dc.SelectObject(*m_bitmap);
512 dc.DrawBitmap(bmp, m_gifDecoder->GetLeft(), m_gifDecoder->GetTop(),
513 true /* use mask */);
514 }
515 else
516 #endif
517 SetImage(img);
518 win->Refresh(img.HasMask(), &rect);
519 }
520
521 timer->Start(m_gifDecoder->GetDelay(), true);
522 }
523
524 void wxHtmlImageCell::Layout(int w)
525 {
526 wxHtmlCell::Layout(w);
527 m_physX = m_physY = wxDefaultCoord;
528 }
529
530 #endif
531
532 wxHtmlImageCell::~wxHtmlImageCell()
533 {
534 delete m_bitmap;
535 #if wxUSE_GIF && wxUSE_TIMER
536 delete m_gifTimer;
537 delete m_gifDecoder;
538 #endif
539 }
540
541
542 void wxHtmlImageCell::Draw(wxDC& dc, int x, int y,
543 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
544 wxHtmlRenderingInfo& WXUNUSED(info))
545 {
546 if ( m_showFrame )
547 {
548 dc.SetBrush(*wxTRANSPARENT_BRUSH);
549 dc.SetPen(*wxBLACK_PEN);
550 dc.DrawRectangle(x + m_PosX, y + m_PosY, m_Width, m_Height);
551 x++, y++;
552 }
553 if ( m_bitmap )
554 {
555 // We add in the scaling from the desired bitmap width
556 // and height, so we only do the scaling once.
557 double imageScaleX = 1.0;
558 double imageScaleY = 1.0;
559 if (m_bmpW != m_bitmap->GetWidth())
560 imageScaleX = (double) m_bmpW / (double) m_bitmap->GetWidth();
561 if (m_bmpH != m_bitmap->GetHeight())
562 imageScaleY = (double) m_bmpH / (double) m_bitmap->GetHeight();
563
564 double us_x, us_y;
565 dc.GetUserScale(&us_x, &us_y);
566 dc.SetUserScale(us_x * m_scale * imageScaleX, us_y * m_scale * imageScaleY);
567
568 dc.DrawBitmap(*m_bitmap, (int) ((x + m_PosX) / (m_scale*imageScaleX)),
569 (int) ((y + m_PosY) / (m_scale*imageScaleY)), true);
570 dc.SetUserScale(us_x, us_y);
571 }
572 }
573
574 wxHtmlLinkInfo *wxHtmlImageCell::GetLink( int x, int y ) const
575 {
576 if (m_mapName.empty())
577 return wxHtmlCell::GetLink( x, y );
578 if (!m_imageMap)
579 {
580 wxHtmlContainerCell *p, *op;
581 op = p = GetParent();
582 while (p)
583 {
584 op = p;
585 p = p->GetParent();
586 }
587 p = op;
588 wxHtmlCell *cell = (wxHtmlCell*)p->Find(wxHTML_COND_ISIMAGEMAP,
589 (const void*)(&m_mapName));
590 if (!cell)
591 {
592 ((wxString&)m_mapName).Clear();
593 return wxHtmlCell::GetLink( x, y );
594 }
595 { // dirty hack, ask Joel why he fills m_ImageMap in this place
596 // THE problem is that we're in const method and we can't modify m_ImageMap
597 wxHtmlImageMapCell **cx = (wxHtmlImageMapCell**)(&m_imageMap);
598 *cx = (wxHtmlImageMapCell*)cell;
599 }
600 }
601 return m_imageMap->GetLink(x, y);
602 }
603
604
605
606 //--------------------------------------------------------------------------------
607 // tag handler
608 //--------------------------------------------------------------------------------
609
610 TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA")
611 TAG_HANDLER_CONSTR(IMG) { }
612
613 TAG_HANDLER_PROC(tag)
614 {
615 if (tag.GetName() == wxT("IMG"))
616 {
617 if (tag.HasParam(wxT("SRC")))
618 {
619 int w = wxDefaultCoord, h = wxDefaultCoord;
620 int al;
621 wxFSFile *str;
622 wxString tmp = tag.GetParam(wxT("SRC"));
623 wxString mn = wxEmptyString;
624
625 str = m_WParser->OpenURL(wxHTML_URL_IMAGE, tmp);
626
627 if (tag.HasParam(wxT("WIDTH")))
628 tag.GetParamAsInt(wxT("WIDTH"), &w);
629 if (tag.HasParam(wxT("HEIGHT")))
630 tag.GetParamAsInt(wxT("HEIGHT"), &h);
631 al = wxHTML_ALIGN_BOTTOM;
632 if (tag.HasParam(wxT("ALIGN")))
633 {
634 wxString alstr = tag.GetParam(wxT("ALIGN"));
635 alstr.MakeUpper(); // for the case alignment was in ".."
636 if (alstr == wxT("TEXTTOP"))
637 al = wxHTML_ALIGN_TOP;
638 else if ((alstr == wxT("CENTER")) || (alstr == wxT("ABSCENTER")))
639 al = wxHTML_ALIGN_CENTER;
640 }
641 if (tag.HasParam(wxT("USEMAP")))
642 {
643 mn = tag.GetParam( wxT("USEMAP") );
644 if (mn.GetChar(0) == wxT('#'))
645 {
646 mn = mn.Mid( 1 );
647 }
648 }
649 wxHtmlImageCell *cel = new wxHtmlImageCell(
650 m_WParser->GetWindowInterface(),
651 str, w, h,
652 m_WParser->GetPixelScale(),
653 al, mn);
654 m_WParser->ApplyStateToCell(cel);
655 cel->SetId(tag.GetParam(wxT("id"))); // may be empty
656 m_WParser->GetContainer()->InsertCell(cel);
657 if (str)
658 delete str;
659 }
660 }
661 if (tag.GetName() == wxT("MAP"))
662 {
663 m_WParser->CloseContainer();
664 m_WParser->OpenContainer();
665 if (tag.HasParam(wxT("NAME")))
666 {
667 wxString tmp = tag.GetParam(wxT("NAME"));
668 wxHtmlImageMapCell *cel = new wxHtmlImageMapCell( tmp );
669 m_WParser->GetContainer()->InsertCell( cel );
670 }
671 ParseInner( tag );
672 m_WParser->CloseContainer();
673 m_WParser->OpenContainer();
674 }
675 if (tag.GetName() == wxT("AREA"))
676 {
677 if (tag.HasParam(wxT("SHAPE")))
678 {
679 wxString tmp = tag.GetParam(wxT("SHAPE"));
680 wxString coords = wxEmptyString;
681 tmp.MakeUpper();
682 wxHtmlImageMapAreaCell *cel = NULL;
683 if (tag.HasParam(wxT("COORDS")))
684 {
685 coords = tag.GetParam(wxT("COORDS"));
686 }
687 if (tmp == wxT("POLY"))
688 {
689 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY, coords, m_WParser->GetPixelScale() );
690 }
691 else if (tmp == wxT("CIRCLE"))
692 {
693 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE, coords, m_WParser->GetPixelScale() );
694 }
695 else if (tmp == wxT("RECT"))
696 {
697 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT, coords, m_WParser->GetPixelScale() );
698 }
699 if (cel != NULL && tag.HasParam(wxT("HREF")))
700 {
701 wxString target;
702 if (tag.HasParam(wxT("TARGET")))
703 target = tag.GetParam(wxT("TARGET"));
704 cel->SetLink(wxHtmlLinkInfo(tag.GetParam(wxT("HREF")), target));
705 }
706 if (cel != NULL)
707 m_WParser->GetContainer()->InsertCell( cel );
708 }
709 }
710
711 return false;
712 }
713
714 TAG_HANDLER_END(IMG)
715
716
717
718 TAGS_MODULE_BEGIN(Image)
719
720 TAGS_MODULE_ADD(IMG)
721
722 TAGS_MODULE_END(Image)
723
724
725 #endif