gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: remove -removeSubview:


From: Quentin Mathé
Subject: Re: remove -removeSubview:
Date: Wed, 27 Apr 2011 12:52:30 +0200

Hi Banlu,

For view removal, -removeFromSuperviewWithoutNeedingDisplay should be the 
primitive method at first sight. However given that the _sub_views needs to be 
mutated, it does make sense to make -removeSubview: the primitive method as we 
do. 
If we support -setSubviews: and we try to leverage it as you suggest… According 
to Cocoa documentation -subviews doesn't return a defensive copy, hence in 
-removeFromSuperviewXXX we could retrieve the subviews with -subviews, remove 
the right subview from the returned array and then set its again with 
-setSubviews: without overhead (no array allocation or deallocation). Not sure 
the change is really worth the investment. It also contradicts the Cocoa 
documentation that states that -setSubviews: will call -addSubview: or 
-removeFromSuperview (and not the reverse).

This puts aside, in our currrent implementation, I think -removeSubview: code 
could just call -replaceSubview:with: (the core logic in both methods seems to 
be the same).

Here is also quick implementation of -setSubviews: I wrote a while ago. I never 
committed it to GNUstep Gui, because it doesn't exactly match the behavior 
described in Cocoa documentation, although it's pretty close. iirc implementing 
the exact behavior was requiring some more substantial changes that were not 
worth the time spent on it for my needs.

If someone wants to improve/commit this code to GNUstep Gui, feel free to do so.

- (void) setSubviews: (NSArray *)newSubviews
{
        NSArray *oldSubviews = [NSArray arrayWithArray: _sub_views];
        NSUInteger oldCount = [oldSubviews count];
        NSUInteger newCount = [newSubviews count];
        NSUInteger maxCount = MAX(oldCount, newCount);

        for (int i = 0; i < maxCount; i++)
        {
                if (i < oldCount && i < newCount)
                {
                        NSView *oldSubview = [oldSubviews objectAtIndex: i];
                        NSView *newSubview = [newSubviews objectAtIndex: i];

                        if (oldSubview != newSubview)
                        {
                                [self replaceSubview: oldSubview with: 
newSubview]; 
                        }
                }
                else if (i < oldCount) /* i >= newCount */
                {
                        [self removeSubview: [oldSubviews objectAtIndex: i]]; 
                }
                else if (i < newCount) /* i >= oldCount */
                {
                        [self addSubview: [newSubviews objectAtIndex: i]];
                }
        }
}

Cheers,
Quentin.

Le 21 avr. 2011 à 02:51, Banlu Kemiyatorn a écrit :

> Hmm this shouldnt make sense, may be dereference from -willRemoveSubview:? 
> -- 
> Sent from my GNU/Linux N900 
> 
> ----- Original message ----- 
> > Hi, 
> > Should we remove -removeSubview: and -removeFromSuperview should use 
> > superview's -setSubviews: ? 
> > 
> > lol who wrote this 
> > http://osdir.com/ml/lib.gnustep.devel/2002-03/msg00019.html 
> > 
> > -- 
> >     .----.     Banlu Kemiyatorn 
> >   /.../\...\   漫画家 
> >  |.../  \...|  http://qstx.blogspot.com (Free Software Advocacy & 
> > Development) |../    \..|  http://feedbat.blogspot.com (Studio Work For 
> > Hire)  \/      \/   http://groundzerostudiocomplex.blogspot.com (Studio 
> > Research) 
> 
> 
> _______________________________________________
> Gnustep-dev mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/gnustep-dev




reply via email to

[Prev in Thread] Current Thread [Next in Thread]