[Newbies] submorphs (was: Beginners Digest, Vol 123, Issue 4)

Jecel Assumpcao Jr. jecel at merlintec.com
Wed Feb 22 23:45:20 UTC 2017


Tim,

> Thank you, Jecel, for trying to help me solve my problem. However, my plan is
> to have multiple sibling morphs for both the buttons and the texts.

Code like this can get you a list of all buttons:

| buttons |
buttons := submorphs select: [ :m | m name includesSubString: 'Button')
].

You can do the same thing to get a list of text fields, but the order of
texts and buttons might not match. The order depends on when submorphs
are added and removed and not on they physical position.

> So, I am diving in to trying to use addMorph: programatically to add submorphs
> to my display. This allows me to hold a reference to each submorph in named
> variables, through which I should then be able to specify state changes and act
> on events to and from my submorphs.

Nearly all code you will find in Squeak uses this style. And note that
if your main morph isn't temporary, you can still manually reorganize
the submorphs even if they were created programatically. It might be
tricky to have them stay the way you want as the main morph gets
changed.

> Maybe my mindset is just too old fashioned. Back in the 90's, I programmed MVC
> applications in VisualWorks. I thought Morphic was supposed to be much easier
> to use than MVC, but so far, I have not been able to figure out how to control my
> objects in Morphic.

When Morphic was introduced in Squeak, we already had a bunch of MVC
applications that we wanted to keep using without having to
significantly rewrite them.  So the reasonably simple Morphic from Self
became a much more complicated framework.

The layout concept in the original Morphic was borrowed from TeX. The
idea is that by having nested boxes with three simple alternatives
(fixed, shrink-wrap and stretch) in each dimension (horizontal and
vertical) you could use direct manipulation to build most interesting
GUI elements. The most significant exception is tables since you can
only align on rows or on columns but not on both with nested boxes.

MVC, on the other hand, used pieces of code in a simple constraint
system to define layouts. So Squeak Morphic ended up with a combination
of both. And with a fix to the table problem. The unfortunate result is
that it isn't always easy to figure out why the submorphs are positioned
as they are.

Back to your problem: suppose you want a bunch of pairs of buttons and
text fields, one under the other. You could do something like:

| col row button text fields |
col := AlignmentMorph newColumn.
fields Dictionary new.
1 to: 7 do: [ :i |
	row := AlignmentMorph newRow.
	col addMorph: row.
	button := PluggableButtonMorph new.
	text := TextFieldMorph new.
	row addMorph: button.
	row addMorph: text.
	fields at: button put: text.
	].

There is probably a "self changed" missing somewhere in there. And it
might be a good idea to put a stretching morph between the button and
the text if you want the align the first to the left and the text to the
right. With the table layout the 7 row morphs shown here are probably
not needed. But I hope this gives you the basic idea.

-- Jecel


More information about the Beginners mailing list