Next: Shape Up: The core graphics framework Previous: Complex item classes

## Constructing hierarchy

Any subclass of ComplexItem can add items to itself, thus creating a recursive hierarchy of items. For example, a Frame item consists of four simple items: a rectangle for the central surface, two polygons for the ``lit'' and ``shaded'' borders, and a transparent rectangle that is used as a place-holder for the coordinates of the whole Frame.

In addition to creating hierarchy by creating new item types, an instance of the ComplexItem class can have arbitrary sub-items added to it. To illustrate, we can create a blank complex item:

      set citem [$slate create ComplexItem 50 50 100 100]

The coordinates give the region to be occupied by the item so that methods such as coords will operate correctly. Now we can add items to it. For the sake of example, let's add a pair of lines and an oval to it:

      $slate createchild $citem line 50 50 100 100
      $slate createchild $citem line 50 100 100 50
      $slate createchild $citem oval 60 60 90 90 -fill green

The item that results is shown at the left of figure 4. Like any slate item, this item responds to methods that move and scale it. For example,

      $slate coords $citem

will return `{50 50 100 100}`. The code

      $slate scale $citem 50 50 0.5 1.5

will scale the item, producing the item at the center of figure 4.

Figure 4: A dynamically-constructed complex item

In general, arbitrarily complex items can be built up this way. Figure 5 shows one such hierarchy, similar to those we use in one of our graphical editors. As a general rule, a programmer should define a new subclass of ComplexItem when a particular graphical representation is used again and again, and use dynamic composition of items, such as just given, when items are combined as part of the editing operations in a graphical editor.

Figure 5: A sample visual hierarchy

Next: Shape Up: The core graphics framework Previous: Complex item classes