Tidied view hierarchy management methods

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@3584 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-01-21 18:12:45 +00:00
parent 6639eb3c88
commit d4f4617d09

View file

@ -133,38 +133,61 @@ NSView *current_view = nil;
[super dealloc]; [super dealloc];
} }
- (void)addSubview:(NSView *)aView - (void) addSubview: (NSView*)aView
{ // make sure we are not {
if ([self isDescendantOf:aView]) // making self a subview of if ([self isDescendantOf: aView])
{ // self {
NSLog(@"Operation addSubview: creates a loop in the views tree!\n"); NSLog(@"Operation addSubview: creates a loop in the views tree!\n");
return; return;
} }
[aView viewWillMoveToWindow:window]; [aView retain];
[aView setSuperview:self]; [aView removeFromSuperview];
[aView setNextResponder:self]; [aView viewWillMoveToWindow: window];
[sub_views addObject:(id)aView]; // Add to our subview list [aView viewWillMoveToSuperview: self];
[aView setNextResponder: self];
[sub_views addObject: aView];
[aView resetCursorRects];
[aView setNeedsDisplay: YES];
[aView release];
} }
- (void)addSubview:(NSView *)aView // may not be per OS spec - (void) addSubview: (NSView*)aView
positioned:(NSWindowOrderingMode)place // FIX ME positioned: (NSWindowOrderingMode)place
relativeTo:(NSView *)otherView relativeTo: (NSView*)otherView
{ // make sure we aren't {
// making self a subview of unsigned index;
if ([self isDescendantOf:aView]) // self thereby creating a
{ // loop in the heirarchy
NSLog(@"addSubview:positioned:relativeTo: will create a cycle "
@"in the views tree!\n");
return;
}
[sub_views addObject:(id)aView]; // Add to our subview list if ([self isDescendantOf:aView])
[aView setSuperview:self]; {
// Make ourselves the next NSLog(@"addSubview:positioned:relativeTo: will create a cycle "
[aView setNextResponder:self]; // responder of the view @"in the views tree!\n");
return;
}
[aView viewWillMoveToWindow:window]; // Tell the view what if (aView == otherView)
return;
index = [sub_views indexOfObjectIdenticalTo: otherView];
if (index == NSNotFound)
{
if (place = NSWindowBelow)
index = 0;
else
index = [sub_views length];
}
[aView retain];
[aView removeFromSuperview];
[aView viewWillMoveToWindow: window];
[aView viewWillMoveToSuperview: self];
[aView setNextResponder: self];
if (place == NSWindowBelow)
[sub_views insertObject: aView atIndex: index];
else
[sub_views insertObject: aView atIndex: index+1];
[aView resetCursorRects];
[aView setNeedsDisplay: YES];
[aView release];
} // window it has moved to } // window it has moved to
- (NSView *)ancestorSharedWithView:(NSView *)aView - (NSView *)ancestorSharedWithView:(NSView *)aView
@ -208,47 +231,79 @@ NSView *current_view = nil;
return [super_view opaqueAncestor]; return [super_view opaqueAncestor];
} }
- (void)removeFromSuperview - (void) removeFromSuperviewWithoutNeedinfDisplay
{ {
NSMutableArray *views; NSMutableArray *views;
if (!super_view) // if no superview then if (!super_view) // if no superview then just return
return; // just return return;
if([window firstResponder] == self) if ([window firstResponder] == self)
[window makeFirstResponder:window]; [window makeFirstResponder: window];
views = [super_view subviews];
[self viewWillMoveToWindow:nil]; window = nil;
super_view = nil;
views = [super_view subviews]; [views removeObjectIdenticalTo: self];
[views removeObjectIdenticalTo:self];
super_view = nil;
} }
- (void)replaceSubview:(NSView *)oldView with:(NSView *)newView - (void) removeFromSuperview
{ {
if (!newView) NSMutableArray *views;
return; NSWindow *win;
if (!oldView) if (!super_view) // if no superview then just return
[self addSubview:newView]; return;
else
{
int index = [sub_views indexOfObjectIdenticalTo:oldView];
if (index != NSNotFound) if ([window firstResponder] == self)
{ [window makeFirstResponder: window];
[oldView viewWillMoveToWindow:nil]; views = [super_view subviews];
[oldView setSuperview:nil]; [super_view setNeedsDisplayInRect: frame];
[newView setNextResponder:nil]; win = window;
window = nil;
super_view = nil;
[sub_views replaceObjectAtIndex:index withObject:newView]; [views removeObjectIdenticalTo: self];
}
[newView viewWillMoveToWindow:window]; - (void) replaceSubview: (NSView*)oldView with: (NSView*)newView
[newView setSuperview:self]; {
[newView setNextResponder:self]; if (!newView)
} return;
}
/*
* NB. we implement the replacement in full rather than calling addSubview:
* since classes like NSBox override these methods but expect to be able to
* call [super replaceSubview:with:] safely.
*/
if (!oldView)
{
[newView retain];
[newView removeFromSuperview];
[newView viewWillMoveToWindow: window];
[newView viewWillMoveToSuperview: self];
[newView setNextResponder: self];
[sub_views addObject: newView];
[newView resetCursorRects];
[newView setNeedsDisplay: YES];
[newView release];
}
else if (oldView != newView
&& [sub_views indexOfObjectIdenticalTo: oldView] != NSNotFound)
{
unsigned index;
[newView retain];
[newView removeFromSuperview];
index = [sub_views indexOfObjectIdenticalTo: oldView];
[oldView removeFromSuperview];
[newView viewWillMoveToWindow: window];
[newView viewWillMoveToSuperview: self];
[newView setNextResponder: self];
[sub_views addObject: newView];
[newView resetCursorRects];
[newView setNeedsDisplay: YES];
[newView release];
}
} }
- (void)sortSubviewsUsingFunction:(int (*)(id ,id ,void *))compare - (void)sortSubviewsUsingFunction:(int (*)(id ,id ,void *))compare
@ -257,6 +312,11 @@ NSMutableArray *views;
[sub_views sortUsingFunction:compare context:context]; [sub_views sortUsingFunction:compare context:context];
} }
- (void) viewWillMoveToSuperview: (NSView*)newSuper
{
super_view = newSuper;
}
- (void)viewWillMoveToWindow:(NSWindow *)newWindow - (void)viewWillMoveToWindow:(NSWindow *)newWindow
{ {
int i, count; int i, count;
@ -1350,7 +1410,6 @@ GSTrackingRect *m;
- (unsigned int)autoresizingMask { return autoresizingMask; } - (unsigned int)autoresizingMask { return autoresizingMask; }
- (NSMutableArray *)subviews { return sub_views; } - (NSMutableArray *)subviews { return sub_views; }
- (NSView *)superview { return super_view; } - (NSView *)superview { return super_view; }
- (void)setSuperview:(NSView *)superview { super_view = superview; }
- (BOOL)shouldDrawColor { return YES; } - (BOOL)shouldDrawColor { return YES; }
- (BOOL)isOpaque { return NO; } - (BOOL)isOpaque { return NO; }
- (BOOL)needsDisplay { return needs_display; } - (BOOL)needsDisplay { return needs_display; }