Navigation

[en] - Tutorials
SUnit
Events
Morphic Button
Exceptions


Login
Username:
Password:


Danke

Danke Chris


Du bist hier   >   home   >   projects   >   Squeak   >   [en] - Tutorials   >   Morphic Button

Morphic Button

Introduction

In order to work with Morphs, the graphic elements in Squeak, you should know the basic mechanisms. Some of them can be found in this Tutorial. However there are already sufficient Buttons in Squeak. Simply search for classes, which have 'Button' in their name.

Attention

This Tutorial works in Squeak 3.4. Earlier versions have different interfaces in the Events. The idea is however the same.

Important Messages

  • Object
    • when:send:to:
    • triggerEvent:
  • Morph
    • on:send:to:
    • lock
    • unlock
    • openInHand

Example

In order to create a Button, we need a class, which defines its interfaces. Thus we open a Browser in Squeak and accept the following:
RectangleMorph subclass: #MyButtonMorph
 	instanceVariableNames: ''
 	classVariableNames: ''
 	poolDictionaries: ''
 	category: 'Test-Button'
After that we make sure that the Button is correctly initialized. The trick thereby is following: If one sends the Message #new to a Morph, then a new instance is produced and this the Message #initialize is sent.
In addition we would like that the Button does somewhat if we 'touch' it with the mouse. For this we use the method #on:send:to:. This is not a method, which belongs to the Events in Squeak. It is implemented in Morph themselves.
Naturally we should give it a few beautiful colors. Since the Button cannot do anything at all, we deactivate him with #lock.
MyButtonMorph>>initialize
     super initialize.
     self
          on: #mouseEnter  send: #aMouseEnter: to: self;
          on: #mouseLeave  send: #aMouseLeave: to: self;
          on: #mouseDown   send: #aMouseDown:  to: self;
          on: #mouseUp     send: #aMouseUp:    to: self;
          color:       (Color r: 0.55 g: 0.59 b: 0.72);
          borderWidth: 2;
          borderColor: Color transparent;
          lock.
Now we must define the 4 methods, so that we don't get a run time error later :-) To realize everything it would be enough to implement the click with the mouse, but let us animate the Button beautifully. A so-called (Morphic) Event is evt. Try to send it the message #inspect to find out more about it. We will perform the action when the Mausbutton gets released.
MyButtonMorph>>aMouseDown: evt
  self borderColor: Color red.

MyButtonMorph>>aMouseUp: evt
  self
    borderColor: Color transparent;
    triggerEvent: #act.

MyButtonMorph>>aMouseEnter: evt
  self borderColor: Color black.

MyButtonMorph>>aMouseLeave: evt
  self borderColor: Color transparent.
So that the Button can finally do somewhat, we must provide a Object that should perform a selector. Let's say we want to open a new Transcript. Since the Button can do something now, we can release it with #unlock.
MyButtonMorph>>send: aSelector to: aTarget
  super when: #act send: aSelector to: aTarget.
  self unlock.
Let's try it out.
MyButtonMorph new
  send: #open to: Transcript;
  openInHand.

Disclaimer

Sitemap

hosted by: Beine Computer - www.beine-computer.de - gerrit@beine-computer.de
Webmaster: Chris Burkert - www.chrisburkert.de - chris@chrisburkert.de

Auf alle Inhalte und Gestaltungen gelinkter Seiten, die nicht im Geltungsbereich dieser Website liegen und die verlinkten Seiten selbst, habe ich keinerlei Einfluss und distanziere mich hiermit ausdrücklich.

© Copyright by Chris Burkert

www.whysmalltalk.com   ..::..     ..::..   www.squeak.org