Creating Custom Controls
From Pointui
In addition to extending existing controls, it’s possible to create custom controls that combine multiple other controls. This is really handy for re-usability because it’s possible to package up functionality for both individual controls and combination of controls. The following example is taken from the Tasks applet – each row displayed is one of the following controls – the base functionality coming from a Button, but with a Label and optional Image control displayed as well. Notice how even the event handlers can be configured within the TaskRow class. It’s important to note that child controls (such as lblSubject in the following example) must only ever be added once as a child control using Controls.Add().
class TaskRow : Button { Label lblSubject; int x, y, w, h; String s; int ObjectID; bool isComplete; void Load() { OnClick = OnClick_Handler; Image.LoadFromFile("Row.Background.jif"); ImageSelected.LoadFromFile("Row.Background.Selected.jif"); lblSubject.SetAlign("Left", "Center"); lblSubject.SetFont("Font.Large"); Controls.Add(lblSubject); } void Set(DataTable tbl, int y, int delay) { w = Image.GetWidth(); h = Image.GetHeight(); //position correctly SetBounds(0, y); //get data from the table tbl.GetValue("Subject", s); lblSubject.SetText(s); tbl.GetValue("ObjectID", ObjectID); tbl.GetValue("Complete", isComplete); w = GetWidth(); x = w / 24; Image img; img.Surface.LoadFromFile("Icon.Ring.Tick.jif"); img.SetBounds(x, y); img.AnimateClear(); if (isComplete) { img.AnimateFade(0, 100, 5, delay); } else { img.AnimateFade(0, 30, 5, delay); } Controls.Add(img); int padding, left; padding = 20; left = img.GetWidth() + padding; lblSubject.SetBounds(0, 0, GetWidth(), GetHeight()); lblSubject.SetPadding(0, padding, 0, left); lblSubject.AnimateClear(); lblSubject.AnimateFade(0, 100, 5, delay); //show SetVisible(true); } void OnClick_Handler(Control sender, int x, int y) { //show task Tasks.Display(ObjectID); } }
