Squeak SmalltalkJoker Squeak Smalltalk : Morphic : prevnext Morphic Layout

Chris,

This is exactly the right place to ask those questions. Here are two
examples:

Version 1: Using a ProportionalLayout

| rectangle string frame |
"set up the string"
string := StringMorph new contents: 'This is a string'.

"this is the trick - set up a layout frame so that
it will align the string at the top-center of the owner
but offset left by half of the string's width"
frame := LayoutFrame new.
frame leftFraction: 0.5 offset: string width // -2.
frame topFraction: 0 offset: 0.

"install it"
string layoutFrame: frame.

"the container"
rectangle := RectangleMorph new.
rectangle layoutPolicy: ProportionalLayout new.
rectangle addMorph: string.

"clip parent so string doesn't show outside"
rectangle clipSubmorphs: true.
rectangle openInHand.

Version 2: Using a TableLayout

| rectangle string |
"the container"
rectangle := RectangleMorph new.

"this is the trick - set up a layout
so that it is oriented left to right and
then center the entire list"
rectangle layoutPolicy: TableLayout new.
rectangle listDirection: #leftToRight.
rectangle listCentering: #center.

"Note: the same effect could be achieved using:
rectangle listDirection: #topToBottom.
rectangle wrapCentering: #center.
This may work better if the entire contents
of the rectangle should be a vertical list"

"set up the string"
string := StringMorph new contents: 'This is a string'.
rectangle addMorph: string.

"clip parent so string doesn't show outside"
rectangle clipSubmorphs: true.
rectangle openInHand.

Which one to use?! It depends. Version 1 has the advantage that you can
add other morphs to the container which may (or may not) be affected by
the layout. It is not as good if your string varies in size since the
offset needs to be recomputed each time. Version 2 has the advantage
that it'll work even if the string is dynamically changed. It has the
disadvantage that adding anything to the rectangle will be included in
the layout (that is unless you set the #disableTableLayout property) and
that it's left-aligned if the rectangle is smaller than the string
(which may or may not be what you need...).

Cheers,
  - Andreas

> -----Original Message-----
> From: squeak-dev-admin_at_lists.squeakfoundation.org 
> [mailto:squeak-dev-admin_at_lists.squeakfoundation.org] On 
> Behalf Of Chris Burkert
> Sent: Monday, December 02, 2002 10:37 PM
> To: squeak-dev_at_lists.squeakfoundation.org
> Subject: [Q] Morphic Layout
> 
> 
> Hi All
> 
> At first ... if this is the wrong place for asking those questions 
> please tell me.
> 
> I have a RectangleMorph and a StringMorph. The String is 
> Submorph of the 
> Rectangle. I want that the String is in the top center of the 
> Rectangle 
> even when I resize the Rectangle. I played a little bit with 
> TableLayout 
> and ProportionalLayout, but didn't find what I need. Can you help me ?
> 
> minimum width: |theString|
> and bigger:    |                  theString                  |
> 
> Regards
>             Chris Burkert
> -- 
> --------------------------------------------------------------
> ----------
> Student of applied Computer Science at Chemnitz University of 
> Technology
>       http://www.chrisburkert.de/            chbu_at_hrz.tu-chemnitz.de
> --------------------------------------------------------------
> ----------
> "I invented the term Object-Oriented, and I can tell you I 
> did not have
>   C++ in mind." - Alan Kay --> http://www.squeak.org/
> 
>