[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: remove -removeSubview:
From: |
Banlu Kemiyatorn |
Subject: |
Re: remove -removeSubview: |
Date: |
Wed, 27 Apr 2011 18:23:34 +0700 |
On Wed, Apr 27, 2011 at 5:52 PM, Quentin Mathé <address@hidden> wrote:
> 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.
Then a subclass must not override -removeSubview: without [self
removeSubview:aView]; I raised this coz I saw some code that subclass
removeSubview: w/o calling super's.
http://developer.apple.com/library/mac/#samplecode/SourceView/Listings/MyWindowController_m.html
> 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).
Actually in my second mail I suggested to dereference the subview
within -willRemoveSubview: hence, setSubviews should be somewhat
higher level.
Not directly a patch, but probably something like.
- (void) removeFromSuperview
{
if (_superview == nil) return;
if ([ [_superview subviews] containsObject:self] == NO) return;
/* may be deallocated */
AANode *oldParent = _superview;
_superview = nil;
[oldParent willRemoveSubspace:self];
}
- (void) willRemoveSubview: (id)subview
{
[_subviews removeObject:subview];
}
- (void) replaceSubview: (id)oldSpace
with: (id)newSpace
{
if ([_subviews containsObject:oldSpace])
{
[oldSpace removeFromSuperview];
[self addSubview:newSpace];
}
}
I didn't test this.. should work...
>
> 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
>
>
--
.----. Banlu Kemiyatorn
/.../\...\ 漫画家
|.../ \...| http://qstx.blogspot.com (Free Software Advocacy & Development)
|../ \..| http://feedbat.blogspot.com (Studio Work For Hire)
\/ \/ http://groundzerostudiocomplex.blogspot.com (Studio Research)
- remove -removeSubview:, Banlu Kemiyatorn, 2011/04/19
- Re: remove -removeSubview:, Banlu Kemiyatorn, 2011/04/20
- Re: remove -removeSubview:, Quentin Mathé, 2011/04/27
- Re: remove -removeSubview:, Wolfgang Lux, 2011/04/27
- Re: remove -removeSubview:, Banlu Kemiyatorn, 2011/04/27
- Re: remove -removeSubview:, Wolfgang Lux, 2011/04/27
- Re: remove -removeSubview:, Banlu Kemiyatorn, 2011/04/27
- Re: remove -removeSubview:, Wolfgang Lux, 2011/04/27
- Re: remove -removeSubview:, Banlu Kemiyatorn, 2011/04/27
- Re: remove -removeSubview:, Quentin Mathé, 2011/04/27
- Re: remove -removeSubview:, Banlu Kemiyatorn, 2011/04/27
- Re: remove -removeSubview:, Quentin Mathé, 2011/04/28