2.4 Nest of layouts

To wrap up our discussion on using the LayoutMorph class, let’s look at an example that nests layouts

column1 := LayoutMorph newColumn
    addMorph: (LabelMorph contents: 'Apple');
    addMorph: (LabelMorph contents: 'Banana');
    addMorph: (LabelMorph contents: 'Cherry').
column1 layoutSpec proportionalHeight: 0. "defaults to 1"

column2 := LayoutMorph newColumn
    addMorph: (LabelMorph contents: 'Spring');
    addMorph: (LabelMorph contents: 'Winter');
    addMorph: (LabelMorph contents: 'Summer');
    addMorph: (LabelMorph contents: 'Fall').
column2 layoutSpec proportionalHeight: 0. "defaults to 1"
        
row := LayoutMorph newRow
    separation: 20;
    addMorph: column1;
    addMorph: (LabelMorph contents: 'What are your favorites?');
    addMorph: column2.
                
row openInWorld.

Example 2.3: Nesting layouts

ch03-layoutmorph-example9

Figure 2.10: Nested LayoutMorphs

We can add a colored border to any morph that is an instance of a subclass of BorderedBoxMorph. This is useful for debugging.

Let’s add borders to some of the morphs in the previous example Example 2.3

column1 := LayoutMorph newColumn
    addMorph: (LabelMorph contents: 'Apple');
    addMorph: (LabelMorph contents: 'Banana');
    addMorph: (LabelMorph contents: 'Cherry');
    borderColor: Color red; borderWidth: 2.
column1 layoutSpec proportionalHeight: 0.

column2 := LayoutMorph newColumn
    addMorph: (LabelMorph contents: 'Spring');
    addMorph: (LabelMorph contents: 'Winter');
    addMorph: (LabelMorph contents: 'Summer');
    addMorph: (LabelMorph contents: 'Fall');
    borderColor: Color blue; borderWidth: 2.
column2 layoutSpec proportionalHeight: 0.
    
center := LabelMorph contents: 'What are your favorites?' ::
    borderColor: Color green; borderWidth: 2.

row := LayoutMorph newRow
    separation: 20;
    addMorph: column1;
    addMorph: center;
    addMorph: column2.
                
row openInWorld.

Example 2.4: Revealed nested layouts

ch03-layoutmorph-example10

Figure 2.11: Morph borders