]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmltag.cpp
Minor beautification of wxToolBar under Mac.
[wxWidgets.git] / src / html / htmltag.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmltag.cpp
3// Purpose: wxHtmlTag class (represents single tag)
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation
13#endif
14
3096bd2f 15#include "wx/wxprec.h"
5526e819
VS
16
17#include "wx/defs.h"
18#if wxUSE_HTML
19
20#ifdef __BORDLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WXPRECOMP
5526e819
VS
25#endif
26
69941f05 27#include "wx/html/htmltag.h"
daa616fc 28#include "wx/html/htmlpars.h"
7e1e0960 29#include <stdio.h> // for vsscanf
5526e819
VS
30#include <stdarg.h>
31
32
5526e819
VS
33//-----------------------------------------------------------------------------
34// wxHtmlTagsCache
35//-----------------------------------------------------------------------------
36
5e8e25e7
VS
37struct wxHtmlCacheItem
38{
39 // this is "pos" value passed to wxHtmlTag's constructor.
40 // it is position of '<' character of the tag
41 int Key;
42
43 // end positions for the tag:
44 // end1 is '<' of ending tag,
45 // end2 is '>' or both are
46 // -1 if there is no ending tag for this one...
47 // or -2 if this is ending tag </...>
48 int End1, End2;
49
50 // name of this tag
51 wxChar *Name;
52};
53
54
5526e819
VS
55IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject)
56
57#define CACHE_INCREMENT 64
58
59wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source)
60{
66a77a74 61 const wxChar *src = source.c_str();
5526e819
VS
62 int i, tg, pos, stpos;
63 int lng = source.Length();
66a77a74 64 wxChar dummy[256];
5526e819
VS
65
66 m_Cache = NULL;
67 m_CacheSize = 0;
68 m_CachePos = 0;
69
70 pos = 0;
4f9297b0
VS
71 while (pos < lng)
72 {
73 if (src[pos] == wxT('<')) // tag found:
a914db0f 74 {
5526e819 75 if (m_CacheSize % CACHE_INCREMENT == 0)
5e8e25e7 76 m_Cache = (wxHtmlCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(wxHtmlCacheItem));
5526e819
VS
77 tg = m_CacheSize++;
78 m_Cache[tg].Key = stpos = pos++;
79 dummy[0] = 0; i = 0;
15db3cf5
VS
80 while (pos < lng &&
81 src[pos] != wxT('>') &&
f6bcfd97 82 src[pos] != wxT(' ') && src[pos] != wxT('\r') &&
4f9297b0 83 src[pos] != wxT('\n') && src[pos] != wxT('\t'))
a914db0f 84 {
5526e819 85 dummy[i] = src[pos++];
66a77a74 86 if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A'));
5526e819
VS
87 i++;
88 }
89 dummy[i] = 0;
66a77a74
OK
90 m_Cache[tg].Name = new wxChar[i+1];
91 memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar));
5526e819 92
15db3cf5 93 while (pos < lng && src[pos] != wxT('>')) pos++;
5526e819 94
4f9297b0 95 if (src[stpos+1] == wxT('/')) // ending tag:
a914db0f 96 {
5526e819
VS
97 m_Cache[tg].End1 = m_Cache[tg].End2 = -2;
98 // find matching begin tag:
99 for (i = tg; i >= 0; i--)
4f9297b0 100 if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0))
a914db0f 101 {
5526e819
VS
102 m_Cache[i].End1 = stpos;
103 m_Cache[i].End2 = pos + 1;
104 break;
105 }
106 }
4f9297b0 107 else
a914db0f 108 {
5526e819
VS
109 m_Cache[tg].End1 = m_Cache[tg].End2 = -1;
110 }
111 }
112
113 pos++;
114 }
115
116 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
4f9297b0
VS
117 for (i = 0; i < m_CacheSize; i++)
118 {
2776d7c3 119 delete[] m_Cache[i].Name;
5526e819
VS
120 m_Cache[i].Name = NULL;
121 }
122}
123
5526e819
VS
124void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
125{
126 if (m_Cache == NULL) return;
4f9297b0
VS
127 if (m_Cache[m_CachePos].Key != at)
128 {
5526e819 129 int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1;
daa616fc
VS
130 do
131 {
132 m_CachePos += delta;
133 }
134 while (m_Cache[m_CachePos].Key != at);
5526e819
VS
135 }
136 *end1 = m_Cache[m_CachePos].End1;
137 *end2 = m_Cache[m_CachePos].End2;
138}
139
140
141
142
143//-----------------------------------------------------------------------------
144// wxHtmlTag
145//-----------------------------------------------------------------------------
146
147IMPLEMENT_CLASS(wxHtmlTag,wxObject)
148
daa616fc
VS
149wxHtmlTag::wxHtmlTag(const wxString& source, int pos, int end_pos,
150 wxHtmlTagsCache *cache,
151 wxHtmlEntitiesParser *entParser) : wxObject()
5526e819
VS
152{
153 int i;
daa616fc 154 wxChar c;
5526e819
VS
155
156 // fill-in name, params and begin pos:
5526e819 157 i = pos+1;
daa616fc
VS
158 if (source[i] == wxT('/'))
159 { m_Ending = TRUE; i++; }
160 else
161 m_Ending = FALSE;
5526e819 162
b076dc01 163 // find tag's name and convert it to uppercase:
f6bcfd97 164 while ((i < end_pos) &&
daa616fc
VS
165 ((c = source[i++]) != wxT(' ') && c != wxT('\r') &&
166 c != wxT('\n') && c != wxT('\t') &&
167 c != wxT('>')))
a914db0f 168 {
daa616fc
VS
169 if ((c >= wxT('a')) && (c <= wxT('z')))
170 c -= (wxT('a') - wxT('A'));
171 m_Name << c;
5526e819
VS
172 }
173
b076dc01
VS
174 // if the tag has parameters, read them and "normalize" them,
175 // i.e. convert to uppercase, replace whitespaces by spaces and
176 // remove whitespaces around '=':
c9893146 177 if (source[i-1] != wxT('>'))
daa616fc
VS
178 {
179 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
180 c == wxT('\n') || c == wxT('\t'))
181 wxString pname, pvalue;
182 wxChar quote;
183 enum
a914db0f 184 {
daa616fc
VS
185 ST_BEFORE_NAME = 1,
186 ST_NAME,
187 ST_BEFORE_EQ,
188 ST_BEFORE_VALUE,
189 ST_VALUE
190 } state;
191
192 quote = 0;
193 state = ST_BEFORE_NAME;
194 while (i < end_pos)
195 {
196 c = source[i++];
197
198 if (c == wxT('>') && !(state == ST_VALUE && quote != 0))
a914db0f 199 {
daa616fc 200 if (state == ST_BEFORE_EQ || state == ST_NAME)
b076dc01 201 {
daa616fc
VS
202 m_ParamNames.Add(pname);
203 m_ParamValues.Add(wxEmptyString);
b076dc01 204 }
daa616fc
VS
205 else if (state == ST_VALUE && quote == 0)
206 {
207 m_ParamNames.Add(pname);
367c84b9
VS
208 if (entParser)
209 m_ParamValues.Add(entParser->Parse(pvalue));
210 else
211 m_ParamValues.Add(pvalue);
daa616fc
VS
212 }
213 break;
5526e819 214 }
daa616fc 215 switch (state)
a914db0f 216 {
daa616fc
VS
217 case ST_BEFORE_NAME:
218 if (!IS_WHITE(c))
219 {
220 pname = c;
221 state = ST_NAME;
222 }
223 break;
224 case ST_NAME:
225 if (IS_WHITE(c))
226 state = ST_BEFORE_EQ;
227 else if (c == wxT('='))
228 state = ST_BEFORE_VALUE;
229 else
230 pname << c;
231 break;
232 case ST_BEFORE_EQ:
233 if (c == wxT('='))
234 state = ST_BEFORE_VALUE;
235 else if (!IS_WHITE(c))
236 {
237 m_ParamNames.Add(pname);
238 m_ParamValues.Add(wxEmptyString);
239 pname = c;
240 state = ST_NAME;
241 }
242 break;
243 case ST_BEFORE_VALUE:
244 if (!IS_WHITE(c))
245 {
246 if (c == wxT('"') || c == wxT('\''))
247 quote = c, pvalue = wxEmptyString;
248 else
249 quote = 0, pvalue = c;
250 state = ST_VALUE;
251 }
252 break;
253 case ST_VALUE:
254 if ((quote != 0 && c == quote) ||
255 (quote == 0 && IS_WHITE(c)))
256 {
257 m_ParamNames.Add(pname);
258 if (quote == 0)
259 {
260 // VS: backward compatibility, no real reason,
261 // but wxHTML code relies on this... :(
262 pvalue.MakeUpper();
263 }
367c84b9
VS
264 if (entParser)
265 m_ParamValues.Add(entParser->Parse(pvalue));
266 else
267 m_ParamValues.Add(pvalue);
daa616fc
VS
268 state = ST_BEFORE_NAME;
269 }
270 else
271 pvalue << c;
272 break;
72aa4a98 273 }
5526e819 274 }
daa616fc
VS
275
276 #undef IS_WHITE
277 }
5526e819
VS
278 m_Begin = i;
279
4f9297b0 280 cache->QueryTag(pos, &m_End1, &m_End2);
5526e819
VS
281 if (m_End1 > end_pos) m_End1 = end_pos;
282 if (m_End2 > end_pos) m_End2 = end_pos;
283}
284
5526e819
VS
285bool wxHtmlTag::HasParam(const wxString& par) const
286{
daa616fc 287 return (m_ParamNames.Index(par, FALSE) != wxNOT_FOUND);
5526e819
VS
288}
289
5526e819
VS
290wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const
291{
daa616fc
VS
292 int index = m_ParamNames.Index(par, FALSE);
293 if (index == wxNOT_FOUND)
294 return wxEmptyString;
295 if (with_commas)
4f9297b0 296 {
daa616fc
VS
297 // VS: backward compatibility, seems to be never used by wxHTML...
298 wxString s;
299 s << wxT('"') << m_ParamValues[index] << wxT('"');
300 return s;
5526e819 301 }
daa616fc
VS
302 else
303 return m_ParamValues[index];
5526e819
VS
304}
305
66a77a74 306int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const
5526e819 307{
5526e819 308 wxString parval = GetParam(par);
161f4f73 309 return wxSscanf(parval, format, param);
5526e819
VS
310}
311
8bd72d90
VS
312bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
313{
314 wxString str = GetParam(par);
315
316 if (str.IsEmpty()) return FALSE;
317 if (str.GetChar(0) == wxT('#'))
318 {
319 unsigned long tmp;
320 if (ScanParam(par, wxT("#%lX"), &tmp) != 1)
321 return FALSE;
322 *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16),
323 (unsigned char)((tmp & 0x00FF00) >> 8),
324 (unsigned char)(tmp & 0x0000FF));
325 return TRUE;
326 }
327 else
328 {
329 // Handle colours defined in HTML 4.0:
330 #define HTML_COLOUR(name,r,g,b) \
331 if (str.IsSameAs(wxT(name), FALSE)) \
332 { *clr = wxColour(r,g,b); return TRUE; }
333 HTML_COLOUR("black", 0x00,0x00,0x00)
334 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
335 HTML_COLOUR("gray", 0x80,0x80,0x80)
336 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
337 HTML_COLOUR("maroon", 0x80,0x00,0x00)
338 HTML_COLOUR("red", 0xFF,0x00,0x00)
339 HTML_COLOUR("purple", 0x80,0x00,0x80)
340 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
341 HTML_COLOUR("green", 0x00,0x80,0x00)
342 HTML_COLOUR("lime", 0x00,0xFF,0x00)
343 HTML_COLOUR("olive", 0x80,0x80,0x00)
344 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
345 HTML_COLOUR("navy", 0x00,0x00,0x80)
346 HTML_COLOUR("blue", 0x00,0x00,0xFF)
347 HTML_COLOUR("teal", 0x00,0x80,0x80)
348 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
349 #undef HTML_COLOUR
350 return FALSE;
351 }
352}
353
354bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
355{
356 if (!HasParam(par)) return FALSE;
357 long i;
358 bool succ = GetParam(par).ToLong(&i);
359 *clr = (int)i;
360 return succ;
361}
362
daa616fc
VS
363wxString wxHtmlTag::GetAllParams() const
364{
365 // VS: this function is for backward compatiblity only,
366 // never used by wxHTML
367 wxString s;
368 size_t cnt = m_ParamNames.GetCount();
369 for (size_t i = 0; i < cnt; i++)
370 {
371 s << m_ParamNames[i];
372 s << wxT('=');
373 if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND)
374 s << wxT('\'') << m_ParamValues[i] << wxT('\'');
375 else
376 s << wxT('"') << m_ParamValues[i] << wxT('"');
377 }
378 return s;
379}
380
4d223b67 381#endif