Mercurial > projects > dynamin
comparison dynamin/gui/container.d @ 57:1db4cb3abbb0
Add Container.descendants
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 08 Aug 2009 17:15:31 -0500 |
parents | c2566ab82535 |
children | 712ea1ea845a |
comparison
equal
deleted
inserted
replaced
56:c2566ab82535 | 57:1db4cb3abbb0 |
---|---|
352 if(int result = dg(i, tmp)) | 352 if(int result = dg(i, tmp)) |
353 return result; | 353 return result; |
354 } | 354 } |
355 return 0; | 355 return 0; |
356 } | 356 } |
357 /** | |
358 * Calls the specified delegate with each descendant of this container, for | |
359 * use with foreach. | |
360 * | |
361 * Example: | |
362 * ----- | |
363 * foreach(c; &container.descendants) { | |
364 * // do something with c | |
365 * } | |
366 * ----- | |
367 */ | |
368 int descendants(int delegate(ref Control item) dg) { | |
369 for(int i = 0; i < _children.count; ++i) { | |
370 auto tmp = _children[i]; | |
371 if(int result = dg(tmp)) | |
372 return result; | |
373 auto c = cast(Container)_children[i]; | |
374 if(c) | |
375 if(int result = c.descendants(dg)) | |
376 return result; | |
377 } | |
378 return 0; | |
379 } | |
380 /** | |
381 * Same as descendants(), but includes this container in addition to | |
382 * descendants. | |
383 */ | |
384 int descendantsPlus(int delegate(ref Control item) dg) { | |
385 Control tmp = this; | |
386 if(int result = dg(tmp)) | |
387 return result; | |
388 if(int result = descendants(dg)) | |
389 return result; | |
390 return 0; | |
391 } | |
357 } | 392 } |
358 unittest { | 393 unittest { |
359 auto i = 0; | 394 int a = 0, r = 0; |
360 auto container = new Panel; | 395 auto container1 = new Panel; |
361 container.descendantAdded += (HierarchyEventArgs e) { i++; }; | 396 auto container2 = new Panel; |
362 auto sub = new Panel; | 397 auto control1 = new Control; |
363 sub.add(new Control); | 398 auto control2 = new Control; |
364 container.add(sub); | 399 auto control3 = new Control; |
365 assert(i == 2); | 400 container1.descendantAdded += (HierarchyEventArgs e) { a++; }; |
401 container1.descendantRemoved += (HierarchyEventArgs e) { r++; }; | |
402 | |
403 container2.add(control2); | |
404 container2.children.add(control3); | |
405 assert(control3.parent == container2); | |
406 container1.add(control1); | |
407 assert(a == 1); // test descendantAdded | |
408 container1.add(container2); | |
409 assert(a == 4); // test descendantAdded | |
410 | |
411 // test Container.descendants | |
412 auto list = new List!(Control); | |
413 foreach(c; &container1.descendants) | |
414 list.add(c); | |
415 assert(list.data == [control1, container2, control2, control3]); | |
416 | |
417 list.clear(); | |
418 foreach(c; &container1.descendantsPlus) | |
419 list.add(c); | |
420 assert(list.data == [cast(Control)container1, control1, container2, | |
421 control2, control3]); | |
366 } | 422 } |
367 | 423 |
368 class Panel : Container { | 424 class Panel : Container { |
369 ControlList children() { return _children; } | 425 ControlList children() { return _children; } |
370 void add(Control child) { super.add(child); }; | 426 void add(Control child) { super.add(child); }; |