| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.1.1.1 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoChildList, SoHiddenChildList |
| 47 |
| |
| 48 |
| Author(s) : Paul S. Strauss |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include <Inventor/misc/SoChildList.h> |
| 55 |
#include <Inventor/actions/SoAction.h> |
| 56 |
|
| 57 |
////////////////////////////////////////////////////////////////////////////// |
| 58 |
// |
| 59 |
// SoChildList class. |
| 60 |
// |
| 61 |
////////////////////////////////////////////////////////////////////////////// |
| 62 |
|
| 63 |
//////////////////////////////////////////////////////////////////////// |
| 64 |
// |
| 65 |
// Description: |
| 66 |
// Constructor. |
| 67 |
// |
| 68 |
// Use: public |
| 69 |
|
| 70 |
SoChildList::SoChildList(SoNode *parentNode) : SoNodeList() |
| 71 |
// |
| 72 |
//////////////////////////////////////////////////////////////////////// |
| 73 |
{ |
| 74 |
parent = parentNode; |
| 75 |
} |
| 76 |
|
| 77 |
//////////////////////////////////////////////////////////////////////// |
| 78 |
// |
| 79 |
// Description: |
| 80 |
// Constructor. |
| 81 |
// |
| 82 |
// Use: public |
| 83 |
|
| 84 |
SoChildList::SoChildList(SoNode *parentNode, int size) : SoNodeList(size) |
| 85 |
// |
| 86 |
//////////////////////////////////////////////////////////////////////// |
| 87 |
{ |
| 88 |
parent = parentNode; |
| 89 |
} |
| 90 |
|
| 91 |
//////////////////////////////////////////////////////////////////////// |
| 92 |
// |
| 93 |
// Description: |
| 94 |
// Constructor. |
| 95 |
// |
| 96 |
// Use: public |
| 97 |
|
| 98 |
SoChildList::SoChildList(SoNode *parentNode, const SoChildList &l) : |
| 99 |
SoNodeList(l) |
| 100 |
// |
| 101 |
//////////////////////////////////////////////////////////////////////// |
| 102 |
{ |
| 103 |
parent = parentNode; |
| 104 |
} |
| 105 |
|
| 106 |
//////////////////////////////////////////////////////////////////////// |
| 107 |
// |
| 108 |
// Description: |
| 109 |
// Destructor. |
| 110 |
// |
| 111 |
// Use: public |
| 112 |
|
| 113 |
SoChildList::~SoChildList() |
| 114 |
// |
| 115 |
//////////////////////////////////////////////////////////////////////// |
| 116 |
{ |
| 117 |
truncate(0); |
| 118 |
} |
| 119 |
|
| 120 |
//////////////////////////////////////////////////////////////////////// |
| 121 |
// |
| 122 |
// Description: |
| 123 |
// Adds a child to the end of the list. |
| 124 |
// |
| 125 |
// Use: public |
| 126 |
|
| 127 |
void |
| 128 |
SoChildList::append(SoNode *child) |
| 129 |
// |
| 130 |
//////////////////////////////////////////////////////////////////////// |
| 131 |
{ |
| 132 |
SoNodeList::append(child); |
| 133 |
|
| 134 |
// Express interest by parent in child's modification |
| 135 |
child->addAuditor(parent, SoNotRec::PARENT); |
| 136 |
|
| 137 |
// the parent has changed; notify its auditors |
| 138 |
parent->startNotify(); |
| 139 |
} |
| 140 |
|
| 141 |
//////////////////////////////////////////////////////////////////////// |
| 142 |
// |
| 143 |
// Description: |
| 144 |
// Insert given child into list before child with given index |
| 145 |
// |
| 146 |
// Use: public |
| 147 |
|
| 148 |
void |
| 149 |
SoChildList::insert(SoNode *child, int addBefore) |
| 150 |
// |
| 151 |
//////////////////////////////////////////////////////////////////////// |
| 152 |
{ |
| 153 |
int i; |
| 154 |
|
| 155 |
SoNodeList::insert(child, addBefore); |
| 156 |
|
| 157 |
// If any paths go through the parent, make sure they get updated |
| 158 |
for (i = 0; i < auditors.getLength(); i++) |
| 159 |
((SoPath *)auditors[i])->insertIndex(parent, addBefore); |
| 160 |
|
| 161 |
// Express interest by parent in child's modification |
| 162 |
child->addAuditor(parent, SoNotRec::PARENT); |
| 163 |
|
| 164 |
// the parent has changed; notify its auditors |
| 165 |
parent->startNotify(); |
| 166 |
} |
| 167 |
|
| 168 |
//////////////////////////////////////////////////////////////////////// |
| 169 |
// |
| 170 |
// Description: |
| 171 |
// Removes child with given index. |
| 172 |
// |
| 173 |
// Use: public |
| 174 |
|
| 175 |
void |
| 176 |
SoChildList::remove(int which) |
| 177 |
// |
| 178 |
//////////////////////////////////////////////////////////////////////// |
| 179 |
{ |
| 180 |
int i; |
| 181 |
|
| 182 |
// Remove interest of parent in child |
| 183 |
(*this)[which]->removeAuditor(parent, SoNotRec::PARENT); |
| 184 |
|
| 185 |
// If any paths go through the parent, make sure they get updated |
| 186 |
for (i = 0; i < auditors.getLength(); i++) |
| 187 |
((SoPath *)auditors[i])->removeIndex(parent, which); |
| 188 |
|
| 189 |
SoNodeList::remove(which); |
| 190 |
|
| 191 |
// the parent has changed; notify its auditors |
| 192 |
parent->startNotify(); |
| 193 |
} |
| 194 |
|
| 195 |
//////////////////////////////////////////////////////////////////////// |
| 196 |
// |
| 197 |
// Description: |
| 198 |
// Removes all children from one with given index on, inclusive. |
| 199 |
// |
| 200 |
// Use: public |
| 201 |
|
| 202 |
void |
| 203 |
SoChildList::truncate(int start) |
| 204 |
// |
| 205 |
//////////////////////////////////////////////////////////////////////// |
| 206 |
{ |
| 207 |
int which; |
| 208 |
|
| 209 |
// Remove children from right to left. This allows the indices to |
| 210 |
// remain correct during the removal and also keeps the paths that |
| 211 |
// audit the group from doing too much work. (A path has to adjust |
| 212 |
// indices if we remove a child before the one in the path.) It |
| 213 |
// also minimizes shuffling of entries in the children array. |
| 214 |
|
| 215 |
for (which = getLength() - 1; which >= start; --which) { |
| 216 |
int i; |
| 217 |
|
| 218 |
// Remove interest of parent in child |
| 219 |
(*this)[which]->removeAuditor(parent, SoNotRec::PARENT); |
| 220 |
|
| 221 |
// If any paths go through the parent, make sure they get updated |
| 222 |
for (i = 0; i < auditors.getLength(); i++) |
| 223 |
((SoPath *)auditors[i])->removeIndex(parent, which); |
| 224 |
|
| 225 |
SoNodeList::remove(which); |
| 226 |
} |
| 227 |
|
| 228 |
// the parent has changed; notify its auditors |
| 229 |
parent->startNotify(); |
| 230 |
} |
| 231 |
|
| 232 |
//////////////////////////////////////////////////////////////////////// |
| 233 |
// |
| 234 |
// Description: |
| 235 |
// Copies a list of children. |
| 236 |
// |
| 237 |
// Use: public |
| 238 |
|
| 239 |
void |
| 240 |
SoChildList::copy(const SoChildList &cList) |
| 241 |
// |
| 242 |
//////////////////////////////////////////////////////////////////////// |
| 243 |
{ |
| 244 |
int i; |
| 245 |
|
| 246 |
// Truncate to get rid of old children |
| 247 |
truncate(0); |
| 248 |
|
| 249 |
// Copy children |
| 250 |
SoNodeList::copy(cList); |
| 251 |
|
| 252 |
// Express parent's interest in all children |
| 253 |
for (i = 0; i < getLength(); i++) |
| 254 |
(*this)[i]->addAuditor(parent, SoNotRec::PARENT); |
| 255 |
|
| 256 |
// the parent has changed; notify its auditors |
| 257 |
parent->startNotify(); |
| 258 |
} |
| 259 |
|
| 260 |
//////////////////////////////////////////////////////////////////////// |
| 261 |
// |
| 262 |
// Description: |
| 263 |
// Replaces child with given index with new child. |
| 264 |
// |
| 265 |
// Use: public |
| 266 |
|
| 267 |
void |
| 268 |
SoChildList::set(int which, SoNode *child) |
| 269 |
// |
| 270 |
//////////////////////////////////////////////////////////////////////// |
| 271 |
{ |
| 272 |
int i; |
| 273 |
|
| 274 |
// Remove interest of parent in old child |
| 275 |
(*this)[which]->removeAuditor(parent, SoNotRec::PARENT); |
| 276 |
|
| 277 |
// If any paths go through the parent, make sure they get updated |
| 278 |
for (i = 0; i < auditors.getLength(); i++) |
| 279 |
((SoPath *)auditors[i])->replaceIndex(parent, which, child); |
| 280 |
|
| 281 |
// Replace child |
| 282 |
SoNodeList::set(which, child); |
| 283 |
|
| 284 |
// Express parent's interest in new child |
| 285 |
child->addAuditor(parent, SoNotRec::PARENT); |
| 286 |
|
| 287 |
// the parent has changed; notify its auditors |
| 288 |
parent->startNotify(); |
| 289 |
} |
| 290 |
|
| 291 |
//////////////////////////////////////////////////////////////////////// |
| 292 |
// |
| 293 |
// Description: |
| 294 |
// Traverses all children between the two given indices, inclusive, |
| 295 |
// to apply the given action to them. Stops if the termination |
| 296 |
// condition for the action has been reached. |
| 297 |
// |
| 298 |
// Use: public |
| 299 |
|
| 300 |
void |
| 301 |
SoChildList::traverse(SoAction *action, int firstChild, int lastChild) |
| 302 |
// |
| 303 |
//////////////////////////////////////////////////////////////////////// |
| 304 |
{ |
| 305 |
int i; |
| 306 |
|
| 307 |
// Optimization: If the node this childList is part of is not in |
| 308 |
// the path, then the path code can't change as we traverse its |
| 309 |
// children, and we can use a more efficient traversal: |
| 310 |
SoAction::PathCode pc = action->getCurPathCode(); |
| 311 |
|
| 312 |
if (pc == SoAction::NO_PATH || pc == SoAction::BELOW_PATH) { |
| 313 |
//begin by pushing nonexistent child: |
| 314 |
action->pushCurPath(); |
| 315 |
for (i = firstChild; i <= lastChild; i++) { |
| 316 |
SoNode *child = (*this)[i]; |
| 317 |
// Now pop and push in one move: |
| 318 |
action->popPushCurPath(i); |
| 319 |
action->traverse(child); |
| 320 |
|
| 321 |
// Stop if action has reached termination condition. (For |
| 322 |
// example, search has found what it was looking for, or event |
| 323 |
// was handled.) |
| 324 |
if (action->hasTerminated()) |
| 325 |
break; |
| 326 |
} |
| 327 |
action->popCurPath(); |
| 328 |
} |
| 329 |
else { |
| 330 |
for (i = firstChild; i <= lastChild; i++) { |
| 331 |
SoNode *child = (*this)[i]; |
| 332 |
|
| 333 |
// Never traverse a child that does not affect the state |
| 334 |
// if we are off the path |
| 335 |
if (pc == SoAction::OFF_PATH && ! child->affectsState()) |
| 336 |
continue; |
| 337 |
|
| 338 |
// If we are in the path, don't traverse a child that does |
| 339 |
// not affect the state if it is not on a path |
| 340 |
action->pushCurPath(i); |
| 341 |
|
| 342 |
// If action code is now OFF_PATH, we know that the child |
| 343 |
// was not on a path. Don't traverse it if it does not |
| 344 |
// affect the state |
| 345 |
if (action->getCurPathCode() != SoAction::OFF_PATH || |
| 346 |
child->affectsState()) |
| 347 |
action->traverse(child); |
| 348 |
|
| 349 |
action->popCurPath(pc); |
| 350 |
|
| 351 |
// Stop if action has reached termination condition. (For |
| 352 |
// example, search has found what it was looking for, or event |
| 353 |
// was handled.) |
| 354 |
if (action->hasTerminated()) |
| 355 |
break; |
| 356 |
} |
| 357 |
} |
| 358 |
} |