Re: View system usage with lower left origin
Re: View system usage with lower left origin
- Subject: Re: View system usage with lower left origin
- From: Fritz Anderson <email@hidden>
- Date: Fri, 13 Jun 2003 15:08:20 -0500
On Thursday, June 12, 2003, at 06:35 PM, Ron Wagner wrote:
>
I am new to Cocoa and have a basic question regarding usage of the
>
view system. I have a window that will contain several similar views
>
which I want to add disclosure triangle controls to, similar to the
>
Apple System Profiler application. Since the origin is at the lower
>
left corner, seems like I can't just set the size of the containing
>
view to just expose the triangle control and the title. The whole
>
origin at the bottom left corner instead of the top left corner seems
>
weird to me since layouts tend to naturally originate from the top and
>
flow downwards.
Here's how you'd do it without flipping the coordinate space, which is
the case I have the most experience with. If your application is
text-intensive, that's an argument in favor of flipping, but I find it
easier to remember to subtract as you go down than I do to keep track
of which views go in opposite directions.
Usual disclaimers about typed-in-Mail code. Variables beginning in m
are members, s statics elsewhere initialized.
@implementation GrowingView
#define LINE_HEIGHT 16.0
#define BOX_HEIGHT 48.0
- (void) setExpanded: (BOOL) newState
{
if (newState != mExpanded) {
[self expandViewRect: newState? (BOX_HEIGHT-LINE_HEIGHT)
: (LINE_HEIGHT-BOX_HEIGHT)];
mExpanded = newState;
}
}
- (void) expandViewRect: (float) howMuch
{
NSRect oldFrame = [self frame];
NSRect newFrame = oldFrame;
newFrame.origin.y -= howMuch;
newFrame.size.height += howMuch;
[self setFrame: newFrame];
[self setNeedsDisplay: YES];
/* If this were a cell and not a view, use
setNeedsDisplayInRect: on the union of
oldFrame and newFrame.
*/
}
- (void) drawRect: (NSRect) destRect
{
NSRect bounds = [self bounds];
MyDrawTriangle(mExpanded, bounds.origin.x,
NSMaxY(bounds)-LINE_HEIGHT/2.0);
[mTitle drawAtPoint: NSMakePoint(bounds.origin.x + 20.0,
NSMaxY(bounds)-LINE_HEIGHT)
withAttributes: sSavedAttrs];
if (mExpanded) {
int i;
for (i = 0; i < mLines; i++) {
float y = NSMaxY(bounds) - LINE_HEIGHT * (1 + i);
NSString * currLine = [mLines objectAtIndex: i];
[currLine drawAtPoint: NSMakePoint(bounds.origin.x + 4, y)
withAttributes: sSavedAttrs];
}
}
NSFrameRect(bounds);
}
@end
Actually, I kind of wonder if you really need the full weight of an
NSView for what you're doing. If you aren't likely to need
event-dispatching or view-embedding in the future, you'll notice that
the above code could be part of a subclass of NSObject that just knows
how to draw these little expandable cells in your larger view. It might
be faster, especially if you're creating and destroying them
arbitrarily. Something to consider.
>
So how do seasoned cocoa developers handle things like this? Would you
>
set the height of the containing view to a size just exposing the
>
triangle control with title and apply a transformation matrix to the
>
view to move it down into position? I've seen some example
>
applications that have their content stick to the lower left corner
>
when the window is enlarged. How do you get stuff to stick to the top
>
of the window?
You've read-up on the options in the size pane in Interface Builder?
It's trivial to have an NSView keep its position relative to an edge of
the window, regardless of how the window resizes.
>
Also, is there anything equivalent to the MacApp adorners in Cocoa?
That would be a special class of delegates that would be called
immediately before or after -[NSView drawRect:], so you could do things
like activation rings or highlight backgrounds? There's no hook like
that; you have to override drawRect:.
-- F
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.