Can anyone help me with this Objective-C?

Archives Forums/MacOS X Discussion/Can anyone help me with this Objective-C?

SebHoll(Posted 2007) [#1]
Hi,

I've been trying to make an equivalent Mac OS X function as alternatives to the ScreenToClient() and ClientToScreen() Windows API functions and am finding it extremely hard picking up the language. Basically what I would like to do is to get the absolute screen co-ordinates of a point when I pass a position within a panel, and vice-verse (i.e. get the relative position of a absolute screen point from a specific panel).

I've currently got the following...

Working Code At Bottom of Thread

However as soon as it calls the external function, the program crashes... The code is taken from some sample code around the web where it apparently works.

Many thanks for all your help,


Seb


skidracer(Posted 2007) [#2]
You can't return structs to BitzMax as your declaration is expecting a pointer to a blitzmax type not an actual instance of a raw NSPoint.

You can't return a pointer to the NSPoint because it has been allocated on the stack.

I would pass an int pointer to the function and write the result there.


SebHoll(Posted 2007) [#3]
Thanks for replying Skid...

I would pass an int pointer to the function and write the result there.


I've had a go, but I can't work out how to return the value of the int pointer in the function properly.

Working Code At Bottom of Thread

...gives me a compiler error message of "Incompatible type for argument 1 of NSMakePoint". Any ideas?


Cheers


Seb


Craig Watson(Posted 2007) [#4]
You need to dereference the pointers (ie. *x,*y) because NSMakePoint expects plain ints (technically floats.)

Since you want to return pointer ints you will need to assign them the address of returnPoint.x,y items.

You should remove :Point from your functions because they don't return a Point.

You should be able to achieve the same stuff through Carbon/C++ if you'd prefer.


SebHoll(Posted 2007) [#5]
Cheers Craig...

You need to dereference the pointers (ie. *x,*y) because NSMakePoint expects plain ints (technically floats.)


I found a tutorial on pointers in Objective-C here. So I updated the code with the tweaks you mentioned, and it's compiling.

However it's giving me really strange negative values where 0 is reported when the mouse is moved near the top left corner.

Working Code At Bottom of Thread

Any more ideas? Thanks


Seb


Craig Watson(Posted 2007) [#6]
Instead of using those temporary ints, just dereference x and y directly in NSMakePoint, so NSMakePoint(*x,*y) should work fine.

I guess I'm not really sure what you're trying to achieve. The Cocoa coordinate system starts from the bottom left, so the numbers may not be what you expect.

From what I can tell from your code, the relativeTo function will return the x and y position relative to the whole screen. The absoluteFrom will return a value telling you how the x and y screen coordinates you give are relative to the view. The mouse event values you provide therefore are going to look odd, number one because they start in the top-left, and number two because they are window relative.

Instead of getting mousex and y, try setting a specific screen coordinate (ie. 10,10=bottom left corner) and then using your function, move the window around relative to that and you'll get how it works.


SebHoll(Posted 2007) [#7]
Thanks Craig...

I think we've cracked it - I managed to get the absoluteFrom function to work correctly (i.e. returning absolute screen co-ordinates from panel coords). In order to do so, I had to convert the bottom-left coordinate so it's relative to top-left.

From there, relativeTo function was easy by finding the absolute distance between point 0,0 on the panel and the passed location gives me the relative positions.

In case anybody is interested. Here's the working program:





I've removed the previous versions to avoid confusion for any people wanting to use this code.

Thanks Craig & Skid for all your help,


Seb