[Cuis] How to center a submorph in its owner morph?

H. Hirzel hannes.hirzel at gmail.com
Wed Jan 1 09:54:09 CST 2014


The solution:

LayoutMorph>>addMorph:

attaches as default LayoutSpec a

   LayoutSpec new

which uses all the available space.

LayoutSpec>>initialize

initialize
	"Just some reasonable defaults, use all available space"
	minorDirectionPadding _ 0.5.
	fixedWidth _ 0.
	fixedHeight _ 0.
	proportionalWidth _ 1.0.
	proportionalHeight _ 1.0



So the fix is to go for

#addMorph:layoutSpec:

with a layoutSpec of

   LayoutSpec keepMorphExtent.


The whole script

| row |
row := LayoutMorph newRow.
row
    color: Color green;
    addMorph: (BorderedRectMorph new color: (Color red); morphExtent: 150@ 150)
    layoutSpec: LayoutSpec keepMorphExtent.
row padding: #center.
row morphExtent:  300 @ 300.
row openInWorld.
row morphPosition: 150 at 130



On 1/1/14, H. Hirzel <hannes.hirzel at gmail.com> wrote:
> Hello
>
> I currently encounter some problems with Morph layout and I would like
> to ask for help.
>
> The code [1]  works fine in Cuis 4.1.
>
> A slightly adapted version (just below) in Cuis 4.1 gives the morph as
> shown in file
> Cuis4.1-green-LayoutMorph-with-red-BorderedMorph
>
> | row b |
> row := LayoutMorph newRow.
> b := BorderedRectMorph new color: Color red.
> b morphExtent: 120 at 120.
> row
>     color: Color green;
>     addMorph: b.
> row padding: #center.
> row morphExtent:  200 @ 200.
> row morphPosition: 150 at 130.
> row openInWorld.
> b inspect
>
> However the extent of morph b is wrong. After setting it manually in
> the inspector it looks as shown in picture 'Cuis4.1-green-red120.png'.
> This means setting the extent works here.
>
> In Cuis 4.2 the result of the same code looks as shown in the picture
> 'Cuis4.2-initial.jpeg'.
>
> And it refuses to resize the morph b even if done manually.
>
> Any suggestions to help to fix these bugs are welcome.
>
> Regards
> Hannes
>
>
>
> [1]
> On 2/8/13, Juan Vuletich <juan at jvuletich.org> wrote:
>> Hi Hannes,
>>
>> Something like:
>>
>> | row |
>> row := LayoutMorph newRow.
>> row
>>      color: Color green;
>>      addMorph: (BorderedRectMorph new color: (Color h: 60 s: 0.6 v:
>> 0.6)).
>> row padding: #center.
>> row openInWorld.
>> row morphPosition: 150 at 130
>>
>> If you use a Column and not a Row, you don't even need to specify the
>> #padding:, as it defaults to #center.
>>
>> Cheers,
>> Juan Vuletich
>>
>> On 2/7/2013 12:32 PM, H. Hirzel wrote:
>>> Hello
>>>
>>> I want to add an instance of ImageMorph to an instance of  Morph and
>>> center it there?
>>>
>>> How do I do that?
>>>
>>> I have done experiments with newRow and newColumn layout, see
>>>    https://github.com/hhzl/Cuis-Add-Ons
>>>
>>> But I do not have examples yet of centering.
>>>
>>> Thank you for the answer in advance.
>>>
>>> Hannes
>




More information about the Cuis mailing list