]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_image.cpp
added virtual listctrl support
[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 __BORDLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/html/forcelnk.h"
28 #include "wx/html/m_templ.h"
29
30
31 #include "wx/image.h"
32 #include "wx/dynarray.h"
33
34 #include <math.h>
35 #include <float.h>
36
37 FORCE_LINK_ME(m_image)
38
39
40
41
42 WX_DECLARE_OBJARRAY(int, CoordArray);
43 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
44 WX_DEFINE_OBJARRAY(CoordArray);
45
46
47 //--------------------------------------------------------------------------------
48 // wxHtmlImageMapAreaCell
49 // 0-width, 0-height cell that represents single area in imagemap
50 // (it's GetLink is called from wxHtmlImageCell's)
51 //--------------------------------------------------------------------------------
52
53 class wxHtmlImageMapAreaCell : public wxHtmlCell
54 {
55 public:
56 enum celltype { CIRCLE, RECT, POLY };
57 protected:
58 CoordArray coords;
59 celltype type;
60 int radius;
61 public:
62 wxHtmlImageMapAreaCell( celltype t, wxString &coords, double pixel_scale = 1.0);
63 virtual wxHtmlLinkInfo *GetLink( int x = 0, int y = 0 ) const;
64 };
65
66
67
68
69
70 wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t, wxString &incoords, double pixel_scale )
71 {
72 int i;
73 wxString x = incoords, y;
74
75 type = t;
76 while ((i = x.Find( ',' )) != -1)
77 {
78 coords.Add( (int)(pixel_scale * (double)wxAtoi( x.Left( i ).c_str())) );
79 x = x.Mid( i + 1 );
80 }
81 coords.Add( (int)(pixel_scale * (double)wxAtoi( x.c_str())) );
82 }
83
84 wxHtmlLinkInfo *wxHtmlImageMapAreaCell::GetLink( int x, int y ) const
85 {
86 switch (type)
87 {
88 case RECT:
89 {
90 int l, t, r, b;
91
92 l = coords[ 0 ];
93 t = coords[ 1 ];
94 r = coords[ 2 ];
95 b = coords[ 3 ];
96 if (x >= l && x <= r && y >= t && y <= b)
97 {
98 return m_Link;
99 }
100 break;
101 }
102 case CIRCLE:
103 {
104 int l, t, r;
105 double d;
106
107 l = coords[ 0 ];
108 t = coords[ 1 ];
109 r = coords[ 2 ];
110 d = sqrt( (double) (((x - l) * (x - l)) + ((y - t) * (y - t))) );
111 if (d < (double)r)
112 {
113 return m_Link;
114 }
115 }
116 break;
117 case POLY:
118 {
119 if (coords.GetCount() >= 6)
120 {
121 int intersects = 0;
122 int wherex = x;
123 int wherey = y;
124 int totalv = coords.GetCount() / 2;
125 int totalc = totalv * 2;
126 int xval = coords[totalc - 2];
127 int yval = coords[totalc - 1];
128 int end = totalc;
129 int pointer = 1;
130
131 if ((yval >= wherey) != (coords[pointer] >= wherey))
132 {
133 if ((xval >= wherex) == (coords[0] >= wherex))
134 {
135 intersects += (xval >= wherex) ? 1 : 0;
136 }
137 else
138 {
139 intersects += ((xval - (yval - wherey) *
140 (coords[0] - xval) /
141 (coords[pointer] - yval)) >= wherex) ? 1 : 0;
142 }
143 }
144
145 while (pointer < end)
146 {
147 yval = coords[pointer];
148 pointer += 2;
149 if (yval >= wherey)
150 {
151 while ((pointer < end) && (coords[pointer] >= wherey))
152 {
153 pointer += 2;
154 }
155 if (pointer >= end)
156 {
157 break;
158 }
159 if ((coords[pointer - 3] >= wherex) ==
160 (coords[pointer - 1] >= wherex)) {
161 intersects += (coords[pointer - 3] >= wherex) ? 1 : 0;
162 }
163 else
164 {
165 intersects +=
166 ((coords[pointer - 3] - (coords[pointer - 2] - wherey) *
167 (coords[pointer - 1] - coords[pointer - 3]) /
168 (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1 : 0;
169 }
170 }
171 else
172 {
173 while ((pointer < end) && (coords[pointer] < wherey))
174 {
175 pointer += 2;
176 }
177 if (pointer >= end)
178 {
179 break;
180 }
181 if ((coords[pointer - 3] >= wherex) ==
182 (coords[pointer - 1] >= wherex))
183 {
184 intersects += (coords[pointer - 3] >= wherex) ? 1 : 0;
185 }
186 else
187 {
188 intersects +=
189 ((coords[pointer - 3] - (coords[pointer - 2] - wherey) *
190 (coords[pointer - 1] - coords[pointer - 3]) /
191 (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1 : 0;
192 }
193 }
194 }
195 if ((intersects & 1) != 0)
196 {
197 return m_Link;
198 }
199 }
200 }
201 break;
202 }
203
204 if (m_Next)
205 {
206 wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next;
207 return a->GetLink( x, y );
208 }
209 return NULL;
210 }
211
212
213
214
215
216
217
218
219 //--------------------------------------------------------------------------------
220 // wxHtmlImageMapCell
221 // 0-width, 0-height cell that represents map from imagemaps
222 // it is always placed before wxHtmlImageMapAreaCells
223 // It responds to Find(wxHTML_COND_ISIMAGEMAP)
224 //--------------------------------------------------------------------------------
225
226
227 class wxHtmlImageMapCell : public wxHtmlCell
228 {
229 public:
230 wxHtmlImageMapCell( wxString &name );
231 protected:
232 wxString m_Name;
233 public:
234 virtual wxHtmlLinkInfo *GetLink( int x = 0, int y = 0 ) const;
235 virtual const wxHtmlCell *Find( int cond, const void *param ) const;
236 };
237
238
239 wxHtmlImageMapCell::wxHtmlImageMapCell( wxString &name )
240 {
241 m_Name = name ;
242 }
243
244 wxHtmlLinkInfo *wxHtmlImageMapCell::GetLink( int x, int y ) const
245 {
246 wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next;
247 if (a)
248 return a->GetLink( x, y );
249 return wxHtmlCell::GetLink( x, y );
250 }
251
252 const wxHtmlCell *wxHtmlImageMapCell::Find( int cond, const void *param ) const
253 {
254 if (cond == wxHTML_COND_ISIMAGEMAP)
255 {
256 if (m_Name == *((wxString*)(param)))
257 return this;
258 }
259 return wxHtmlCell::Find(cond, param);
260 }
261
262
263
264
265
266 //--------------------------------------------------------------------------------
267 // wxHtmlImageCell
268 // Image/bitmap
269 //--------------------------------------------------------------------------------
270
271 class wxHtmlImageCell : public wxHtmlCell
272 {
273 public:
274 wxBitmap *m_Image;
275 double m_Scale;
276 wxHtmlImageMapCell *m_ImageMap;
277 wxString m_MapName;
278
279 wxHtmlImageCell(wxFSFile *input, int w = -1, int h = -1, double scale = 1.0, int align = wxHTML_ALIGN_BOTTOM, wxString mapname = wxEmptyString);
280 ~wxHtmlImageCell() {if (m_Image) delete m_Image; }
281 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2);
282 virtual wxHtmlLinkInfo *GetLink( int x = 0, int y = 0 ) const;
283 };
284
285
286
287
288 //--------------------------------------------------------------------------------
289 // wxHtmlImageCell
290 //--------------------------------------------------------------------------------
291
292 wxHtmlImageCell::wxHtmlImageCell(wxFSFile *input, int w, int h, double scale, int align, wxString mapname) : wxHtmlCell()
293 {
294 wxImage *img;
295 int ww, hh, bw, bh;
296 wxInputStream *s = input->GetStream();
297
298 m_Scale = scale;
299 img = new wxImage(*s, wxBITMAP_TYPE_ANY);
300 m_Image = NULL;
301 if (img && (img->Ok()))
302 {
303 ww = img->GetWidth();
304 hh = img->GetHeight();
305 if (w != -1) bw = w; else bw = ww;
306 if (h != -1) bh = h; else bh = hh;
307
308 m_Width = (int)(scale * (double)bw);
309 m_Height = (int)(scale * (double)bh);
310
311 if ((bw != ww) || (bh != hh))
312 {
313 wxImage img2 = img->Scale(bw, bh);
314 m_Image = new wxBitmap(img2.ConvertToBitmap());
315 }
316 else
317 m_Image = new wxBitmap(img->ConvertToBitmap());
318 delete img;
319 }
320 switch (align)
321 {
322 case wxHTML_ALIGN_TOP :
323 m_Descent = m_Height;
324 break;
325 case wxHTML_ALIGN_CENTER :
326 m_Descent = m_Height / 2;
327 break;
328 case wxHTML_ALIGN_BOTTOM :
329 default :
330 m_Descent = 0;
331 break;
332 }
333
334 m_ImageMap = NULL;
335 m_MapName = mapname;
336 SetCanLiveOnPagebreak(FALSE);
337 }
338
339
340
341 void wxHtmlImageCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
342 {
343 if (m_Image)
344 {
345 double us_x, us_y;
346 dc.GetUserScale(&us_x, &us_y);
347 dc.SetUserScale(us_x * m_Scale, us_y * m_Scale);
348
349 // dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, (m_Image->GetMask() != (wxMask*) 0));
350 dc.DrawBitmap(*m_Image, (int) ((x + m_PosX) / m_Scale),
351 (int) ((y + m_PosY) / m_Scale), TRUE);
352 dc.SetUserScale(us_x, us_y);
353 }
354 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
355 }
356
357 wxHtmlLinkInfo *wxHtmlImageCell::GetLink( int x, int y ) const
358 {
359 if (m_MapName.IsEmpty())
360 return wxHtmlCell::GetLink( x, y );
361 if (!m_ImageMap)
362 {
363 wxHtmlContainerCell *p, *op;
364 op = p = GetParent();
365 while (p)
366 {
367 op = p;
368 p = p->GetParent();
369 }
370 p = op;
371 wxHtmlCell *cell = (wxHtmlCell*)p->Find( wxHTML_COND_ISIMAGEMAP, (const void*)(&m_MapName));
372 if (!cell)
373 {
374 ((wxString&)m_MapName).Clear();
375 return wxHtmlCell::GetLink( x, y );
376 }
377 { // dirty hack, ask Joel why he fills m_ImageMap in this place
378 // THE problem is that we're in const method and we can't modify m_ImageMap
379 wxHtmlImageMapCell **cx = (wxHtmlImageMapCell**)(&m_ImageMap);
380 *cx = (wxHtmlImageMapCell*)cell;
381 }
382 }
383 return m_ImageMap->GetLink( x, y );
384 }
385
386
387
388 //--------------------------------------------------------------------------------
389 // tag handler
390 //--------------------------------------------------------------------------------
391
392 TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA")
393
394 TAG_HANDLER_PROC(tag)
395 {
396 if (tag.GetName() == wxT("IMG"))
397 {
398 if (tag.HasParam(wxT("SRC")))
399 {
400 int w = -1, h = -1;
401 int al;
402 wxFSFile *str;
403 wxString tmp = tag.GetParam(wxT("SRC"));
404 wxString mn = wxEmptyString;
405
406 str = m_WParser->GetFS()->OpenFile(tmp);
407 if (tag.HasParam(wxT("WIDTH")))
408 tag.GetParamAsInt(wxT("WIDTH"), &w);
409 if (tag.HasParam(wxT("HEIGHT")))
410 tag.GetParamAsInt(wxT("HEIGHT"), &h);
411 al = wxHTML_ALIGN_BOTTOM;
412 if (tag.HasParam(wxT("ALIGN")))
413 {
414 wxString alstr = tag.GetParam(wxT("ALIGN"));
415 alstr.MakeUpper(); // for the case alignment was in ".."
416 if (alstr == wxT("TEXTTOP"))
417 al = wxHTML_ALIGN_TOP;
418 else if ((alstr == wxT("CENTER")) || (alstr == wxT("ABSCENTER")))
419 al = wxHTML_ALIGN_CENTER;
420 }
421 if (tag.HasParam(wxT("USEMAP")))
422 {
423 mn = tag.GetParam( wxT("USEMAP") );
424 if (mn.GetChar(0) == wxT('#'))
425 {
426 mn = mn.Mid( 1 );
427 }
428 }
429 wxHtmlImageCell *cel = NULL;
430 if (str)
431 {
432 cel = new wxHtmlImageCell(str, w, h, m_WParser->GetPixelScale(), al, mn);
433 cel->SetLink(m_WParser->GetLink());
434 m_WParser->GetContainer()->InsertCell(cel);
435 delete str;
436 }
437 }
438 }
439 if (tag.GetName() == wxT("MAP"))
440 {
441 m_WParser->CloseContainer();
442 m_WParser->OpenContainer();
443 if (tag.HasParam(wxT("NAME")))
444 {
445 wxString tmp = tag.GetParam(wxT("NAME"));
446 wxHtmlImageMapCell *cel = new wxHtmlImageMapCell( tmp );
447 m_WParser->GetContainer()->InsertCell( cel );
448 }
449 ParseInner( tag );
450 m_WParser->CloseContainer();
451 m_WParser->OpenContainer();
452 }
453 if (tag.GetName() == wxT("AREA"))
454 {
455 if (tag.HasParam(wxT("SHAPE")))
456 {
457 wxString tmp = tag.GetParam(wxT("SHAPE"));
458 wxString coords = wxEmptyString;
459 tmp.MakeUpper();
460 wxHtmlImageMapAreaCell *cel = NULL;
461 if (tag.HasParam(wxT("COORDS")))
462 {
463 coords = tag.GetParam(wxT("COORDS"));
464 }
465 if (tmp == wxT("POLY"))
466 {
467 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY, coords, m_WParser->GetPixelScale() );
468 }
469 else if (tmp == wxT("CIRCLE"))
470 {
471 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE, coords, m_WParser->GetPixelScale() );
472 }
473 else if (tmp == wxT("RECT"))
474 {
475 cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT, coords, m_WParser->GetPixelScale() );
476 }
477 if (cel != NULL && tag.HasParam(wxT("HREF")))
478 {
479 wxString tmp = tag.GetParam(wxT("HREF"));
480 wxString target = wxEmptyString;
481 if (tag.HasParam(wxT("TARGET"))) target = tag.GetParam(wxT("TARGET"));
482 cel->SetLink( wxHtmlLinkInfo(tmp, target));
483 }
484 if (cel != NULL) m_WParser->GetContainer()->InsertCell( cel );
485 }
486 }
487
488 return FALSE;
489 }
490
491 TAG_HANDLER_END(IMG)
492
493
494
495 TAGS_MODULE_BEGIN(Image)
496
497 TAGS_MODULE_ADD(IMG)
498
499 TAGS_MODULE_END(Image)
500
501
502 #endif