PDA

View Full Version : Placing Views inside Classes


Anne
01-11-2003, 11:35 AM
Hey All,

By now you might have seen or used a custom class. It's all very well being able to build and reuse a class, but how do you put items into it? There might be a class with many subviews, and one particular view is to be the designated "content" view, or you may want to put something in a particular view of an instance:

For the first case (the general "content" view), you need the defaultplacement attribute. This goes in the class definition, and names the view that content will be placed in:

<canvas>
<!-- The class DEFINITION -->
<class name="myClass" bgcolor="blue">
<attribute name="defaultplacement" value="'interiorMargins'" />
<view name="interiorMargins"
x="10" y="10"
width="parent.width - 20"
height="parent.height - 20">
<!-- This is where the content will be placed -->
</view>
</class>

<!-- An INSTANCE of that class -->
<myClass x="20" y="20" width="175" height="90">
<!-- A view that has been PLACED into the instance -->
<view name="theContent" bgcolor="green"
width="immediateparent.width"
height="immediateparent.height" />
</myClass>
</canvas>

Note that there is no addressing; the destination view is just named. In this case, the green view gets inserted based on the defaultplacement <attribute> tag. Also notice at the use of the "immediateparent" reference in the dimensions of the placed view - I'll talk about that later.

Now suppose that this was a far more complex class, and you wanted to control where each subview of its instance was placed. The above example could be rewritten as follows:

<canvas>
<!-- The class DEFINITION -->
<class name="myClass" bgcolor="blue">
<attribute name="defaultplacement" />
<view name="interiorMargins"
x="10" y="10"
width="parent.width - 20"
height="parent.height - 20">
<!-- This is where the content will be placed -->
</view>
</class>

<!-- An INSTANCE of that class -->
<myClass x="20" y="20" width="175" height="90">
<!-- A view that has been PLACED into the instance -->
<view name="theContent" bgcolor="green"
width="immediateparent.width"
height="immediateparent.height"
placement="interiorMargins" />
</myClass>
</canvas>

Here, the view is placed based on the its own placement attribute.


// parent and immediateparent
//
You ONLY have to worry about the difference between the two when working with views placed in classes. This applies to non user-defined classes as well, such as the <window> tag (that's effectively a class that we made for you).

Consider the example above by looking at the tags for now. The view named "theContent" has a parent: the tag <myClass>. So if you set theContent's width to parent.width, it would be 175px. But you went to all the trouble of creating a class that creates margins, so you want to be able to set it to
the "parent view as defined by the class". That's the immediateparent, (in this case the "interiorMargins" view).

In general you can always use immediateparent, even if you're not placing stuff, which is handy if you're not sure if something is a class or not.

That's all, enjoy!

Antun Karlovac