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
[aView setSuperview:self];
// Make ourselves the next
[aView setNextResponder:self]; // responder of the view
[aView viewWillMoveToWindow:window]; // Tell the view what if ([self isDescendantOf:aView])
{
NSLog(@"addSubview:positioned:relativeTo: will create a cycle "
@"in the views tree!\n");
return;
}
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
return; // just return
if([window firstResponder] == self) if (!super_view) // if no superview then just return
[window makeFirstResponder:window]; return;
[self viewWillMoveToWindow:nil];
views = [super_view subviews]; if ([window firstResponder] == self)
[views removeObjectIdenticalTo:self]; [window makeFirstResponder: window];
super_view = nil; views = [super_view subviews];
window = nil;
super_view = nil;
[views removeObjectIdenticalTo: self];
} }
- (void)replaceSubview:(NSView *)oldView with:(NSView *)newView - (void) removeFromSuperview
{ {
if (!newView) NSMutableArray *views;
return; NSWindow *win;
if (!super_view) // if no superview then just return
return;
if (!oldView) if ([window firstResponder] == self)
[self addSubview:newView]; [window makeFirstResponder: window];
else views = [super_view subviews];
{ [super_view setNeedsDisplayInRect: frame];
int index = [sub_views indexOfObjectIdenticalTo:oldView]; win = window;
window = nil;
super_view = nil;
if (index != NSNotFound) [views removeObjectIdenticalTo: self];
{ }
[oldView viewWillMoveToWindow:nil];
[oldView setSuperview:nil];
[newView setNextResponder:nil];
[sub_views replaceObjectAtIndex:index withObject:newView]; - (void) replaceSubview: (NSView*)oldView with: (NSView*)newView
{
[newView viewWillMoveToWindow:window]; if (!newView)
[newView setSuperview:self]; return;
[newView setNextResponder:self];
} /*
} * 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; }