]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: mod_image.cpp | |
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 VS |
16 | #include "wx/defs.h" |
17 | #if wxUSE_HTML | |
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 VS |
36 | |
37 | FORCE_LINK_ME(mod_image) | |
38 | ||
39 | ||
25082126 VS |
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 ); | |
63 | virtual wxString GetLink( int x = 0, int y = 0 ) const; | |
64 | }; | |
65 | ||
66 | ||
67 | ||
68 | ||
69 | ||
70 | wxHtmlImageMapAreaCell::wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::celltype t, wxString &incoords ) | |
71 | { | |
72 | int i; | |
73 | wxString x = incoords, y; | |
74 | ||
75 | type = t; | |
76 | while ((i = x.Find( ',' )) != -1) | |
77 | { | |
78 | coords.Add( atoi( x.Left( i ).c_str() ) ); | |
79 | x = x.Mid( i + 1 ); | |
80 | } | |
81 | coords.Add( atoi( x.c_str() ) ); | |
82 | } | |
83 | ||
84 | wxString 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( ((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 | } | |
f42b1601 RD |
144 | |
145 | while (pointer < end) | |
25082126 VS |
146 | { |
147 | yval = coords[pointer]; | |
148 | pointer += 2; | |
f42b1601 | 149 | if (yval >= wherey) |
25082126 VS |
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 | { | |
162 | intersects += (coords[pointer-3] >= wherex) ? 1 : 0; | |
163 | } | |
164 | else | |
165 | { | |
166 | intersects += | |
167 | ((coords[pointer-3] - (coords[pointer-2] - wherey) * | |
168 | (coords[pointer-1] - coords[pointer-3]) / | |
169 | (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1:0; | |
170 | } | |
171 | } | |
172 | else | |
173 | { | |
174 | while((pointer < end) && (coords[pointer] < wherey)) | |
175 | { | |
176 | pointer+=2; | |
177 | } | |
178 | if (pointer >= end) | |
179 | { | |
180 | break; | |
181 | } | |
182 | if ((coords[pointer-3] >= wherex) == | |
183 | (coords[pointer-1] >= wherex)) | |
184 | { | |
185 | intersects += (coords[pointer-3] >= wherex) ? 1:0; | |
186 | } | |
187 | else | |
188 | { | |
189 | intersects += | |
190 | ((coords[pointer-3] - (coords[pointer-2] - wherey) * | |
191 | (coords[pointer-1] - coords[pointer-3]) / | |
192 | (coords[pointer] - coords[pointer - 2])) >= wherex) ? 1:0; | |
193 | } | |
194 | } | |
195 | } | |
196 | if ((intersects & 1) != 0) | |
197 | { | |
198 | return m_Link; | |
199 | } | |
200 | } | |
201 | } | |
202 | break; | |
203 | } | |
204 | if (m_Next) | |
205 | { | |
206 | wxHtmlImageMapAreaCell *a = (wxHtmlImageMapAreaCell*)m_Next; | |
207 | return a->GetLink( x, y ); | |
208 | } | |
209 | return wxEmptyString; | |
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(HTML_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 wxString 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 | wxString 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 == HTML_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 | ||
5526e819 VS |
266 | //-------------------------------------------------------------------------------- |
267 | // wxHtmlImageCell | |
268 | // Image/bitmap | |
269 | //-------------------------------------------------------------------------------- | |
270 | ||
271 | class wxHtmlImageCell : public wxHtmlCell | |
272 | { | |
273 | public: | |
274 | wxBitmap *m_Image; | |
25082126 VS |
275 | wxHtmlImageMapCell *m_ImageMap; |
276 | wxString m_MapName; | |
5526e819 | 277 | |
25082126 | 278 | wxHtmlImageCell(wxFSFile *input, int w = -1, int h = -1, int align = HTML_ALIGN_BOTTOM, wxString mapname = wxEmptyString); |
5526e819 VS |
279 | ~wxHtmlImageCell() {if (m_Image) delete m_Image;} |
280 | void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2); | |
25082126 | 281 | virtual wxString GetLink( int x = 0, int y = 0 ) const; |
5526e819 VS |
282 | }; |
283 | ||
284 | ||
285 | ||
25082126 | 286 | |
5526e819 VS |
287 | //-------------------------------------------------------------------------------- |
288 | // wxHtmlImageCell | |
289 | //-------------------------------------------------------------------------------- | |
290 | ||
25082126 | 291 | wxHtmlImageCell::wxHtmlImageCell(wxFSFile *input, int w, int h, int align, wxString mapname) : wxHtmlCell() |
5526e819 VS |
292 | { |
293 | wxImage *img; | |
294 | int ww, hh; | |
5526e819 VS |
295 | wxInputStream *s = input -> GetStream(); |
296 | ||
72aa4a98 | 297 | img = new wxImage(*s, wxBITMAP_TYPE_ANY); |
5526e819 VS |
298 | |
299 | m_Image = NULL; | |
300 | if (img && (img -> Ok())) { | |
301 | ww = img -> GetWidth(); | |
302 | hh = img -> GetHeight(); | |
303 | if (w != -1) m_Width = w; else m_Width = ww; | |
304 | if (h != -1) m_Height = h; else m_Height = hh; | |
305 | if ((m_Width != ww) || (m_Height != hh)) { | |
306 | wxImage img2 = img -> Scale(m_Width, m_Height); | |
307 | m_Image = new wxBitmap(img2.ConvertToBitmap()); | |
308 | } | |
309 | else | |
310 | m_Image = new wxBitmap(img -> ConvertToBitmap()); | |
311 | delete img; | |
312 | } | |
313 | switch (align) { | |
314 | case HTML_ALIGN_TOP : | |
315 | m_Descent = m_Height; break; | |
316 | case HTML_ALIGN_CENTER : | |
317 | m_Descent = m_Height / 2; break; | |
318 | case HTML_ALIGN_BOTTOM : default : | |
319 | m_Descent = 0; break; | |
320 | } | |
25082126 VS |
321 | |
322 | m_ImageMap = NULL; | |
323 | m_MapName = mapname; | |
5526e819 VS |
324 | } |
325 | ||
326 | ||
327 | ||
328 | void wxHtmlImageCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2) | |
329 | { | |
330 | if (m_Image) | |
331 | dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, TRUE); | |
332 | wxHtmlCell::Draw(dc, x, y, view_y1, view_y2); | |
333 | } | |
334 | ||
25082126 VS |
335 | wxString wxHtmlImageCell::GetLink( int x, int y ) const |
336 | { | |
337 | if (m_MapName.IsEmpty()) | |
338 | return wxHtmlCell::GetLink( x, y ); | |
339 | if (!m_ImageMap) | |
340 | { | |
341 | wxHtmlContainerCell *p, *op; | |
342 | op = p = GetParent(); | |
343 | while (p) | |
344 | { | |
345 | op = p; | |
346 | p = p->GetParent(); | |
347 | } | |
348 | p = op; | |
349 | wxHtmlCell *cell = (wxHtmlCell*)p->Find( HTML_COND_ISIMAGEMAP, (const void*)(&m_MapName)); | |
350 | if (!cell) | |
351 | { | |
352 | ((wxString&)m_MapName).Clear(); | |
353 | return wxHtmlCell::GetLink( x, y ); | |
354 | } | |
355 | { // dirty hack, ask Joel why he fills m_ImageMap in this place | |
356 | // THE problem is that we're in const method and we can't modify m_ImageMap | |
357 | wxHtmlImageMapCell **cx = (wxHtmlImageMapCell**)(&m_ImageMap); | |
358 | *cx = (wxHtmlImageMapCell*)cell; | |
359 | } | |
360 | } | |
361 | return m_ImageMap->GetLink( x, y ); | |
362 | } | |
5526e819 VS |
363 | |
364 | ||
365 | ||
366 | //-------------------------------------------------------------------------------- | |
367 | // tag handler | |
368 | //-------------------------------------------------------------------------------- | |
369 | ||
25082126 | 370 | TAG_HANDLER_BEGIN(IMG, "IMG, MAP, AREA") |
5526e819 VS |
371 | |
372 | TAG_HANDLER_PROC(tag) | |
373 | { | |
25082126 VS |
374 | if (tag.GetName() == "IMG") |
375 | { | |
376 | if (tag.HasParam("SRC")) { | |
377 | int w = -1, h = -1; | |
378 | int al; | |
379 | wxFSFile *str; | |
380 | wxString tmp = tag.GetParam("SRC"); | |
381 | wxString mn = wxEmptyString; | |
382 | ||
383 | str = m_WParser -> GetFS() -> OpenFile(tmp); | |
384 | if (tag.HasParam("WIDTH")) tag.ScanParam("WIDTH", "%i", &w); | |
e90c1d2a | 385 | if (tag.HasParam("HEIGHT(")) tag.ScanParam("HEIGHT(", "%i", &h); |
25082126 VS |
386 | al = HTML_ALIGN_BOTTOM; |
387 | if (tag.HasParam("ALIGN")) { | |
388 | wxString alstr = tag.GetParam("ALIGN"); | |
389 | alstr.MakeUpper(); // for the case alignment was in ".." | |
390 | if (alstr == "TEXTTOP") al = HTML_ALIGN_TOP; | |
391 | else if ((alstr == "CENTER") || (alstr == "ABSCENTER")) al = HTML_ALIGN_CENTER; | |
392 | } | |
393 | if (tag.HasParam("USEMAP")) | |
394 | { | |
395 | mn = tag.GetParam( "USEMAP" ); | |
396 | if (mn[ 0 ] == '#') | |
397 | { | |
398 | mn = mn.Mid( 1 ); | |
399 | } | |
400 | } | |
401 | wxHtmlImageCell *cel = NULL; | |
402 | if (str) { | |
403 | cel = new wxHtmlImageCell(str, w, h, al, mn); | |
404 | cel -> SetLink(m_WParser -> GetLink()); | |
405 | m_WParser -> GetContainer() -> InsertCell(cel); | |
406 | delete str; | |
407 | } | |
408 | } | |
409 | } | |
410 | if (tag.GetName() == "MAP") | |
411 | { | |
412 | m_WParser->CloseContainer(); | |
413 | m_WParser->OpenContainer(); | |
414 | if (tag.HasParam("NAME")) | |
415 | { | |
416 | wxString tmp = tag.GetParam("NAME"); | |
417 | wxHtmlImageMapCell *cel = new wxHtmlImageMapCell( tmp ); | |
418 | m_WParser->GetContainer()->InsertCell( cel ); | |
419 | } | |
420 | ParseInner( tag ); | |
421 | m_WParser->CloseContainer(); | |
422 | m_WParser->OpenContainer(); | |
423 | } | |
424 | if (tag.GetName() == "AREA") | |
425 | { | |
426 | if (tag.HasParam("SHAPE")) | |
427 | { | |
428 | wxString tmp = tag.GetParam("SHAPE"); | |
429 | wxString coords; | |
430 | tmp.MakeUpper(); | |
431 | wxHtmlImageMapAreaCell *cel = NULL; | |
432 | if (tag.HasParam("COORDS")) | |
433 | { | |
434 | coords = tag.GetParam("COORDS"); | |
435 | } | |
436 | if (tmp == "POLY") | |
437 | { | |
438 | cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::POLY, coords ); | |
439 | } | |
440 | else if (tmp == "CIRCLE") | |
441 | { | |
442 | cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::CIRCLE, coords ); | |
443 | } | |
e90c1d2a | 444 | else if (tmp == "RECT(") |
25082126 VS |
445 | { |
446 | cel = new wxHtmlImageMapAreaCell( wxHtmlImageMapAreaCell::RECT, coords ); | |
447 | } | |
448 | if (cel && tag.HasParam("HREF")) | |
449 | { | |
450 | wxString tmp = tag.GetParam("HREF"); | |
451 | cel->SetLink( tmp ); | |
452 | } | |
453 | m_WParser->GetContainer()->InsertCell( cel ); | |
454 | } | |
455 | } | |
5526e819 VS |
456 | |
457 | return FALSE; | |
458 | } | |
459 | ||
460 | TAG_HANDLER_END(IMAGE) | |
461 | ||
462 | ||
463 | ||
464 | TAGS_MODULE_BEGIN(Image) | |
465 | ||
466 | TAGS_MODULE_ADD(IMG) | |
467 | ||
468 | TAGS_MODULE_END(Image) | |
469 | ||
470 | ||
3364ab79 | 471 | #endif |