=== Top of the Swiki === Attachments ===

FormsViewsAndWindows

From the Squeak mailing list:

What is the difference between views, viewports, forms, canvases, and windows?

Would you use a view, a viewport, a form, a canvas, or a window, if you simply wanted to draw a shape in a window (separate from the Workspace) using a Pen?



Definition Summary:

MVC Architecture:

 Form:     Basic display medium.
View: Structured picture that can contain other views;
part of "Model, View, Control" (MVC) architecture.

 viewport: Not a basic object; it is the rectangle of a subview
in the superview's coordinate system.
window: Not a basic object; it is the rectangle of a
view in its coordinate system.

Morphic Architecture, MorphicInterface
 Canvas:   Part of the new Morphic display architecture; FormCanvas is
used as the background on which to draw Morphs. Morphic is a
newer display architecture which will slowly replace the older
MVC architecture in Squeak.

In the MVC display architecture, the basic display medium is a Form. This can be used as a source or destination for displaying on other Forms using a BitBlt object. The DisplayScreen "Display" is a kind of form, so you can draw directly on it like you can any other form:

 | pen |
pen := Pen newOnForm: Display.
pen defaultNib: 4.
1 to: 50 do: [:i | pen go: i*4. pen turn: 89].

will draw a black spiral in the center of the Display. Note that since you are drawing directly to the display, it draws over any existing views ("windows"), and will be erased as views are moved over it.

You can draw the spiral on another form to "retain it", then draw that form on the Display:

 | form pen |
form := Form extent: 300@300 depth: Display depth.
form fillColor: Color green.
pen := Pen newOnForm: form.
pen defaultNib: 4.
1 to: 50 do: [:i | pen go: i*4. pen turn: 89].
form display.
form displayAt: Display center - form center.

Here the pen draws on the form, which in turn is then drawn on the Display at two locations. However, this is still drawing directly to the display, so it again draws over existing views and will be erased as views are moved over it.

To put the form in its own "window" (view) that interacts correctly with the other system views, you must use the View classes. A View is a structured picture which can contain subviews, and is part of the "Model, View, Control" (MVC) architecture. Here we will put our form in a FormView (which knows how to display forms, and can edit them too), and add the FormView as a subview of a ColorSystemView (which adds the standard title bar, close and grow boxes, and other standard window controls):

 | form pen view topView |
form := Form extent: 300@300 depth: Display depth.
form fillColor: Color green.
pen := Pen newOnForm: form.
pen defaultNib: 4.
1 to: 50 do: [:i | pen go: i*4. pen turn: 89].
view := FormView new.
view model: form.
view borderWidth: 2.
topView := ColorSystemView new.
topView model: form. "to set the correct window size"
topView addSubView: view.
topView controller open.

Now the picture can be moved, resized, edited, etc.