| typedef |
|
grammar |
| |
By using
typedef,
one can add to the Yoix
typedict.
The parameter list provided in the
typedef
statement give the names of parameters that can be used with the
type definition and are populated with the values provided in square brackets
when a new object of this type is declared.
typedef
should probably be avoided by novice users.
Its usage description can be summarized as follows:
Statement:
typedef name ( ParameterList ) Compound
| |
| Example: |
This extended example defines a new type called a
ScrollCanvas
that is, essentially, a
TextCanvas
with a vertical
Scrollbar.
At the bottom of the example, the
ScrollCanvas
is exercised by putting it in a
Frame
and setting its text to the source HTML from the AT&T home page.
import yoix.awt.*;
import yoix.awt.KeyCode.VK_DOWN;
import yoix.awt.KeyCode.VK_UP;
import yoix.awt.KeyCode.VK_PAGE_DOWN;
import yoix.awt.KeyCode.VK_PAGE_UP;
import yoix.io.*;
import yoix.string.*;
typedef ScrollCanvas(lines) {
Panel p = {
Color background = Color.white;
Color foreground = Color.black;
TextCanvas canvas = {
String font = "Helvetica-plain-14";
int textmode = LINEMODE;
int textwrap = TRUE;
Object panel;
Insets insets = {
int top = 8;
int left = 8;
int bottom = 8;
int right = 8;
};
mousePressed(e) {
requestfocus = TRUE;
}
keyPressed(e) {
panel.scrollBy(e.keycode);
}
};
Scrollbar vbar = {
Color background = Color.lightGray;
int orientation = VERTICAL;
int unitincrement =
yoix.math.iceil(canvas.font.height);
int blockincrement = 22 * unitincrement;
int maximum = (lines + 22) * unitincrement;
Object canvas;
adjustmentValueChanged(e) {
canvas.origin = new Point {int y = value;};
}
};
BorderLayout layoutmanager;
Array layout = {
canvas, CENTER,
vbar, EAST,
};
//
// Support functions
//
setText(text) {
canvas.text = text;
}
setVbar(lines) {
}
scrollBy(keycode) {
int dy = 0;
switch (keycode) {
case VK_DOWN:
dy = vbar.unitincrement;
break;
case VK_UP:
dy = -vbar.unitincrement;
break;
case VK_PAGE_DOWN:
dy = vbar.blockincrement;
break;
case VK_PAGE_UP:
dy = -vbar.blockincrement;
break;
}
if (dy != 0) {
vbar.value += dy;
postEvent(new AdjustmentEvent {
String id = "adjustmentValueChanged";
double value = vbar.value;
}, vbar);
}
}
};
p.canvas.panel = p;
p.vbar.canvas = p.canvas;
return(p);
}
URL att = open("http://www.att.com", "r", URL);
String line;
String buffer[4096,...];
int pos = 0;
int lines = 0;
while (line = att.nextline) {
strcpy(buffer+pos, line);
pos += line@length;
strcpy(buffer+pos, "\n");
pos++;
lines++;
}
Frame f = {
ScrollCanvas canvas[lines];
canvas.setText(buffer);
Array layout = {
canvas
};
};
f.visible = TRUE;
| | |
| See Also: |
reference
|
|
Yoix is a registered trademark of AT&T Inc.
|