discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Geometry problem


From: Jeff Teunissen
Subject: Re: Geometry problem
Date: Tue, 24 Jun 2003 10:13:06 -0400

Kazunobu Kuriyama wrote:

> I have a question about the GNUstep's geometry.
> The question is as follows:
> 
>         X
>     |-------|    Object A
>  -- +---------------------------------------------------
>   | |(0,0)
> Y | |
>   | |            Object B
>  -- |       +----------------------------------------
>     |       |(X,Y)
>     |       |
>     |       |
>     |       |
> 
> Here, you have two GUI objects Object A and Object B.  Object A
> is the main window of an GNUstep application, and Object B is an
> instance of either the NSView class or its subclass.  The coordinates
> system is taken as shown above.  Ignore the border width for now
> because it's a matter of arithmetic.  Then, you need to know the
> coordinates values (X, Y) to implement a method declared in the
> interface of Object B.  Write a piece of code to calculate X and Y.
> The code should be independent of the underlying window system.
> 
> If Object A is a parent or ancestor of Object B (in a sense of
> window hierarchy), the answer may be easy.  I have no idea,
> however, if Object A is an instance of NSApplication.

It's not, it's probably an instance of NSView (or one of its subclasses).

> Could you help me so that I won't devise GSTranslateCoordinates,
> a ridiculous wrapper of XTranslateCoordinates() that ruins
> the GNUstep's OO structure?

GNUstep's geometry system is incredibly flexible, but pretty simple if you
don't do anything weird. :)

Every view has its own coordinate system. Every window has a "content
view", which takes up its entire displayable area (the window's border,
etc. are owned by the window system/window manager and are, of course, not
part of this displayable area).

X origin is always at the left, unless the view's origin is repositioned
(more on this later).

Y origin is, by default, at the bottom and increases upwards. This can be
changed by making the view "flipped", using [view setFlipped: YES].
Flipped views have their Y origin and Y coordinates switched, so that 0.0
is at the top and coordinates increase downwards.

Note: coordinates are floating-point, and a coordinate system may be
modified, scaled, rotated, repositioned, etc. arbitrarily.

You could create an unflipped view such that the origin is at the top
right (by translating the origin and rotating the coordinate system 180
degrees, which will cause the contents to be upside-down and backwards; or
just by setting the bounds origin and using negative coordinates), or even
at the center of the view (by setting the bounds origin). In this case,
negative coordinates are valid (they're already valid anyway, just clipped
to the view's visible bounds). This can make things interesting, as you
can probably imagine. :)

When positioning a view inside a superview (the window's content view, in
this case), you need to know that your coordinate system (by default, as
described above) is positioned inside the superview's coordinate system.
So to place a view I points down and J points to the right of the
enclosing view (which is a little odd in the GNUstep system, because it's
normally like a graph with the origin at the bottom left), you might do
something like:

--->8---cut-here---8<---

float i = 50; // down
float j = 50; // right
NSRect superFrame = [view frame]; // get the view's frame
NSSize mySize = NSMakeSize(100, 100); // size of my new view
NSRect myFrame;

// make a rectangle describing where you want the view to be
// note: this is wrong if "view" is flipped
myFrame = NSMakeRect(superFrame.origin.x + j,
                    (superFrame.origin.y + superFrame.size.height)
                     - (i + mySize.height),
                     mySize.width,
                     mySize.height);

myView = [[NSView alloc] initWithFrame: myFrame];
[view addSubview: myView];

--->8---cut-here---8<---

[snip]

I hope I've gone some way in helping you to understand what you want. :)

-- 
| Jeff Teunissen  -=-  Pres., Dusk To Dawn Computing  -=-  deek @ d2dc.net
| GPG: 1024D/9840105A   7102 808A 7733 C2F3 097B  161B 9222 DAB8 9840 105A
| Core developer, The QuakeForge Project        http://www.quakeforge.net/
| Specializing in Debian GNU/Linux              http://www.d2dc.net/~deek/




reply via email to

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