1 /******************************************************************************
2 *
3 * Copyright (C) 2006, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: gtitem 18.10.06 0.0.A.
11 *
12 * Author: Alexey Krivonogov ( gentee )
13 *
14 ******************************************************************************/
15
16 method treeitem gtitem.gettreeitem
17 {
18 return ( &this - sizeof( treeitem ))->treeitem
19 }
20
21 method uint gtitem.moveup()
22 {
23 uint prev cur
24
25 cur as this.gettreeitem()
26 prev as cur.getprev()
27
28 if &prev
29 {
30 prev as prev.getprev()
31 return cur.move( prev )
32 }
33 return 0
34 }
35
36 method uint gtitem.movedown()
37 {
38 uint next cur
39
40 cur as this.gettreeitem()
41 next as cur.getnext()
42
43 if &next : return cur.move( next )
44 return 0
45 }
46
47 method gtitem gtitem.parent
48 {
49 uint parent
50
51 parent as this.gettreeitem().parent()
52
53 return ?( parent, parent.data()->gtitem, 0->gtitem )
54 }
55
56 method str gtitem.getfullname( str result )
57 {
58 uint data
59 stack tstack
60 uint ti = &this.gettreeitem()
61
62 do {
63 data = ti->treeitem.data()
64 data as gtitem
65 if *data.name : tstack.push()->uint = &data.name
66 // ti = ti->treeitem.parent
67 } while ti = ti->treeitem.parent
68
69 result.clear()
70
71 do {
72 result += tstack.top()->uint->str
73 result.appendch( $gt_CHDIV )
74 } while tstack.pop()
75
76 // Delete the last $gt_CHDIV
77 result.setlen( *result - 1 )
78 return result
79 }
80
81 method uint gtitem.haschild
82 {
83 return ?( this.gettreeitem().child, 1, 0 )
84 }
85
86 method uint gtitem.isroot
87 {
88 return this.gettreeitem().isroot()
89 }
90
91 method uint gtitem.find( str attr )
92 {
93 uint i
94
95 fornum i = 0, *this.attrib
96 {
97 if this.attrib[ i ].name %== attr : return i + 1
98 }
99 return 0
100 }
101
102 method uint gtitem.delattrib( str attrib )
103 {
104 uint id
105
106 if !*attrib : return 0
107
108 if id = this.find( attrib ) : this.attrib.del( id - 1 )
109 return 1
110 }
111
112 method str gtitem.get( str attrib value )
113 {
114 uint id
115
116 if id = this.find( attrib ) : value = this.attrib[ id - 1 ].value
117 else : value.clear()
118
119 return value
120 }
121
122 method str gtitem.get( str attrib )
123 {
124 uint id
125
126 if id = this.find( attrib ) : return this.attrib[ id - 1 ].value
127 return 0->str
128 }
129
130
131 method uint gtitem.getuint( str attrib )
132 {
133 str stemp
134
135 return uint( this.get( attrib, stemp ))
136 }
137
138 method int gtitem.getint( str attrib )
139 {
140 str stemp
141
142 return int( this.get( attrib, stemp ))
143 }
144
145 operator uint *( gtitem gti )
146 {
147 return *gti.gettreeitem()
148 }
149
150 method gtitem gtitem.lastchild
151 {
152 return this.gettreeitem().lastchild().data()->gtitem
153 }
154
155 method gtitem gtitem.child
156 {
157 return this.gettreeitem().child().data()->gtitem
158 }
159
160 method gtitem gtitem.getnext
161 {
162 return this.gettreeitem().getnext().data()->gtitem
163 }
164
165 method gtitem gtitem.getprev
166 {
167 return this.gettreeitem().getprev().data()->gtitem
168 }
169
170 //-----------------------------------------------------------------
171 // For 'foreach' operator
172 /*
173 type gtitems < index = gtitem >
174 {
175 uint parent
176 uint cur
177 }
178 */
179 //-----------------------------------------------------------------
180 /*
181 method gtitems gtitem.items( gtitems items )
182 {
183 items.parent = &this
184 items.cur = 0
185 return items
186 }
187 */
188 method uint gtitem.eof( fordata tfd )
189 {
190 return !tfd.icur
191 }
192
193 method uint gtitem.next( fordata tfd )
194 {
195 if !tfd.icur : return 0
196
197 tfd.icur = tfd.icur->treeitem.next
198 return tfd.icur->treeitem.data()
199 }
200
201 method uint gtitem.first( fordata tfd )
202 {
203 tfd.icur = this.gettreeitem().child
204 return tfd.icur->treeitem.data()
205 }
206
207 method gtitem.delnames
208 {
209 str name
210
211 foreach child, this : child.delnames()
212 .getfullname( name )
213 .maingt->gt.names.del( name )
214 }
215
216 method gtitem.setnames( str name )
217 {
218 // .getfullname( name )
219 .maingt->gt.names[ name ] = &this.gettreeitem()
220 foreach child, this
221 {
222 str stemp
223 ( stemp = name ).appendch( $gt_CHDIV ) += child.name
224 child.setnames( stemp )
225 }
226 }
227
228 method uint gtitem.move( gtitem target, uint flag )
229 {
230 uint ret
231 str name
232
233 .delnames()
234 ret = this.gettreeitem().move( target.gettreeitem(), flag )
235
236 .setnames( .getfullname( name ) )
237 return ret
238 }
239
240 method uint gtitem.move( gtitem after )
241 {
242 uint flag
243
244 if !&after : flag = $TREE_FIRST
245 elif &after == 1 : flag = $TREE_LAST
246 else : flag = $TREE_AFTER
247 return this.move( after, flag )
248 }
249
250 //-----------------------------------------------------------------
Edit