Re: Changing order of views dynamically
Re: Changing order of views dynamically
- Subject: Re: Changing order of views dynamically
- From: Rainer Brockerhoff <email@hidden>
- Date: Tue, 20 Mar 2007 08:32:52 -0300
At 04:10 -0700 20/03/2007, email@hidden wrote:
>From: Sergey Shapovalov <email@hidden>
>References: <email@hidden>
> <email@hidden>
>In-Reply-To: <email@hidden>
>Date: Tue, 20 Mar 2007 11:40:24 +0300
>Message-ID: <email@hidden>
>
>....
>Now: how it works. Every NSView has an array of subviews available through [NSView subviews]. When I add a subview via [NSView addSubview:], it goes to the end of the array. When I add a subview via [NSView addSubview:positioned:relativeTo:], it is inserted before or after the relative subview into this array.
>
>When the parent view draws itself, it also re-draws its subviews in a loop. It calls drawRect for the subviews in the same order as the subviews go in the array. That's why if the subviews (partially) overlap, the ones with smaller indices in the array appear below the ones with bigger indices.
>...
>So, now the solution is evident. To provide proper positioning of some subviews over others, I need proper sorting of the subviews array. And yes - there is a function to do this:
>[NSView sortSubviewsUsingFunction:context:].
There are other uses for the subviews array in a certain order, not just for redrawing; mouse hit testing also iterates over the array. Or, for instance, when I wrote RBSplitView I use that to keep subviews in left-to-right (or top-to-bottom) order, and I suppose NSSplitView also does that.
Your reasoning might work (coincidentally) for redrawing if ALL your subviews, and the parent view(s), are opaque. If that's not true you'll probably run into drawing glitches right away. View redrawing is a complex processes and overlapping views would make it much slower - the framework would have to check for overlaps at every redraw.
The late PowerPlant allowed overlapping subviews because all subviews were opaque AND the redrawing order was officially defined (which it isn't in Cocoa)... still, there's this method in NSWindow:
>- (void)useOptimizedDrawing:(BOOL)flag
>Informs the receiver whether to optimize focusing and drawing when displaying its views.
>The optimizations may prevent sibling subviews from being displayed in the correct order-which matters only if the subviews overlap. You should always set flag to YES if there are no overlapping subviews within the receiver. The default is NO.
...which is sort of weird. If, as the docs say elsewhere, overlapping subviews are usually disallowed, the default should be YES; I suppose calling this doesn't do anything useful nowadays.
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
Weblog: http://www.brockerhoff.net/bb/viewtopic.php
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden