Getting it on with Flex 2 "content" coordinates

In Flex 2, UIComponent class provides several useful functions to convert Flash’s global and local x and y coordinates to “content” coordinates. If your component has a 10-pixel border, and its subcomponents always appear inside that border, you might want to establish a content coordinate system that makes a subcomponent at (0,0) appear at (10,10) instead. This is a great addition that I noticed in either beta 3 or the final release of Flex 2. I’ve been making great use of functions like contentToLocal, contentToGlobal, localToContent, and globalToContent. You’ll also find the contentMouseX and contentMouseY properties which automatically convert mouse coordinates.

There’s one missing bit of functionality that I can’t find in the documentation. I’d like properties for the content area width and height. Maybe a rectangle representing the bounds of the content area. Windows Forms in the .NET Framework have a property called ClientRectangle which has this functionality. As a poor hack, I might assume that the distance to content (0,0) should be the same on the opposite corner. That won’t work on the Panel component, though. The title bar of the Panel is bigger than the bottom border. So how do I figure out where to find the bottom-right point of an arbitrary component’s content area?

As a worst case scenario, I could put a temporary, invisible component in there and use constraint-based layout to align the component at the bottom-right corner, but I’d rather just do a little math to calculate this value.

Update (7.17.2006): After some searching, I discovered the viewMetrics property in the Container class. It returns an EdgeMetrics object that holds the sizes of each of the borders. You might also find the viewMetricsAndPadding property useful as well.

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. Lisa Margret

    Hi Josh Tynjala,
    Thats a nice article about the coordinates in flex
    actually i have been trying to find the boundaries of a cell in advance data grid
    i m puzzled a bit how to use these co ordinates to accomplish it
    i m a flex newbie though i would really appreciate any help on this
    Thanks & Regards,
    Lisa