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