summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/SVGList.h
blob: fdf13bd9705d0bc5231328b541ac15e8ed5c1b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
    Copyright (C) 2001-2003 KSVG Team
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef SVGList_H
#define SVGList_H

#include "ksvg_bridge.h"
#include "ksvg_lookup.h"
#include "ksvg_scriptinterpreter.h"
#include <tqptrlist.h>
#include <dom/dom_misc.h>

namespace KSVG
{

class SVGListDefs
{
public:
	enum
	{
		// Properties
		NumberOfItems,
		// Functions
		GetItem, RemoveItem, AppendItem, Initialize,
		InsertItemBefore, ReplaceItem, Clear
	};
};
	
template<class T>
class SVGList : public DOM::DomShared
{
public:
	SVGList() { m_impl.setAutoDelete(false); }
	SVGList(const SVGList &other) { *this = other; }
	~SVGList() { clear(); }

	SVGList<T> &operator=(const SVGList<T> &other)
	{
		// Clear own list
		clear();

		// Clone other's elements and append them
		SVGList<T> &get = const_cast<SVGList<T> &>(other);
		for(unsigned int i = 0; i < other.numberOfItems(); i++)
		{
			T *obj = new T(*get.getItem(i));
			obj->ref();

			appendItem(obj);
		}

		return *this;
	}

	unsigned int numberOfItems() const { return m_impl.count(); }

	void clear()
	{
		for(unsigned int i = 0; i < numberOfItems(); i++)
			getItem(i)->deref();

		m_impl.clear();
	}

	T *initialize(T *newItem)
	{
		clear();
		return appendItem(newItem);
	}

	T *getFirst() const { return m_impl.getFirst(); }

	T *getLast() const { return m_impl.getLast(); }

	T *getItem(unsigned int index) { return m_impl.at(index); }
	const T *getItem(unsigned int index) const { return const_cast<SVGList<T> *>(this)->m_impl.at(index); }

	T *insertItemBefore(T *newItem, unsigned int index)
	{
		m_impl.insert(index, newItem);
		return newItem;
	}

	T *replaceItem(T *newItem, unsigned int index)
	{
		m_impl.take(index);
		m_impl.insert(index, newItem);
		return newItem;
	}

	T *removeItem(unsigned int index)
	{
		return m_impl.take(index);
	}

	void removeItem(const T *item)
	{
		m_impl.remove(item);
	}

	T *appendItem(T *newItem)
	{
		m_impl.append(newItem);
		return newItem;
	}

	// Ecma stuff
	KJS::Value getValueProperty(KJS::ExecState *, int token) const
	{
		switch(token)
		{
			case SVGListDefs::NumberOfItems:
				return KJS::Number(numberOfItems());
			default:
				kdWarning() << "Unhandled token in " << k_funcinfo << " : " << token << endl;
				return KJS::Undefined();
		}
	}

	KJS::Value call(KJS::ExecState *exec, SVGList<T> *obj, const KJS::List &args, int id)
	{
		switch(id)
		{
			case SVGListDefs::GetItem:
				return obj->getItem(static_cast<unsigned int>(args[0].toNumber(exec)))->cache(exec);
			case SVGListDefs::RemoveItem:
				return obj->removeItem(static_cast<unsigned int>(args[0].toNumber(exec)))->cache(exec);
			case SVGListDefs::AppendItem:
				return obj->appendItem(static_cast<KSVGBridge<T> *>(args[0].imp())->impl())->cache(exec);
			case SVGListDefs::InsertItemBefore:
				return obj->insertItemBefore(static_cast<KSVGBridge<T> *>(args[0].imp())->impl(), static_cast<unsigned int>(args[1].toNumber(exec)))->cache(exec);
			case SVGListDefs::Initialize:
				return obj->initialize(static_cast<KSVGBridge<T> *>(args[0].imp())->impl())->cache(exec);
			case SVGListDefs::Clear:
			{
				obj->clear();
				return KJS::Undefined();
			}
			default:
				kdWarning() << "Unhandled function id in " << k_funcinfo << " : " << id << endl;
				break;
		}

		return KJS::Undefined();
	}	

private:
	TQPtrList<T> m_impl;
};

}

#endif