diff --git a/ChangeLog b/ChangeLog index a344132a0..67cfafb03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2011-05-19 Fred Kiefer + + * Headers/AppKit/NSView.h, + * Source/NSView.m (-setSubviews:): New method. + Code by Banlu Kemiyatorn . + 2011-05-18 Fred Kiefer * Source/NSBitmapImageRep+ICNS.h diff --git a/Headers/AppKit/NSView.h b/Headers/AppKit/NSView.h index 4b309cd8a..4019773e4 100644 --- a/Headers/AppKit/NSView.h +++ b/Headers/AppKit/NSView.h @@ -178,6 +178,9 @@ PACKAGE_SCOPE - (void) sortSubviewsUsingFunction: (int (*)(id ,id ,void*))compare context: (void*)context; - (NSArray*) subviews; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) +- (void) setSubviews: (NSArray *)newSubviews; +#endif - (NSView*) superview; - (NSWindow*) window; - (void) viewWillMoveToSuperview: (NSView*)newSuper; diff --git a/Source/NSView.m b/Source/NSView.m index 7608e6188..d876830eb 100644 --- a/Source/NSView.m +++ b/Source/NSView.m @@ -1056,6 +1056,59 @@ GSSetDragTypes(NSView* obj, NSArray *types) } } +- (void) setSubviews: (NSArray *)newSubviews +{ + NSEnumerator *en; + NSView *aView; + NSMutableArray *uniqNew = [NSMutableArray array]; + + if (nil == newSubviews) + { + [NSException raise: NSInvalidArgumentException + format: @"Setting nil as new subviews."]; + } + + // Use a copy as we remove from the subviews array + en = [[NSArray arrayWithArray: _sub_views] objectEnumerator]; + while ((aView = [en nextObject])) + { + if (NO == [newSubviews containsObject: aView]) + { + [aView removeFromSuperview]; + } + } + + en = [newSubviews objectEnumerator]; + while ((aView = [en nextObject])) + { + id supersub = [aView superview]; + + if (supersub != nil && supersub != self) + { + [NSException raise: NSInvalidArgumentException + format: @"Superviews of new subviews must be either nil or receiver."]; + } + + if ([uniqNew containsObject: aView]) + { + [NSException raise: NSInvalidArgumentException + format: @"Duplicated new subviews."]; + } + + if (NO == [_sub_views containsObject: aView]) + { + [self addSubview: aView]; + } + + [uniqNew addObject: aView]; + } + + ASSIGN(_sub_views, uniqNew); + + // The order of the subviews may have changed + [self setNeedsDisplay: YES]; +} + - (void) sortSubviewsUsingFunction: (int (*)(id ,id ,void*))compare context: (void*)context {