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