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