Reply to this topic  Start new topic
> Simple Checkbox
CSM
post Mar 12 2009, 09:52 AM
Post #1






Posts: 2,386
Joined: 1-September 06
From: ̶O̶h̶i̶o̶ Washington
Member No.: 16,587



A very simple checkbox:
CODE
function Checkbox(_frame, _text, _checked) {
var $outer = this;

// Status of checkbox
var checked = (_checked !== undefined)? _checked : false;

if(!(_frame instanceof Window || _frame instanceof Frame)) {
throw "Checkbox parent element must be a Window or Frame";
}

// Frame containing all checkbox-related components
var frame = new Frame();
_frame.appendChild(frame);

// Used to draw the visual checked "state" of the checkbox
var canvas = new Canvas();
canvas.width = 15;
canvas.height = 15;
frame.appendChild(canvas);

// When the checkbox is clicked, change the status and redraw it
canvas.onClick = function() {
$outer.checked = !checked; // Set checked via the public setter
draw();
};

// Text object to display to the left of the checkable area.
var text = new Text();
text.data = _text;
text.anchorStyle = "topLeft";
text.hOffset = 20;
frame.appendChild(text);

// Ensure that clicking the text associated with a checkbox changes the state too
text.onClick = function() {
canvas.onClick();
};

// Context for the canvas
var ctx = canvas.getContext("2d");

// This is called each time you need to update the checkbox's state
function draw() {
/* We don't need to clear the context each time because the
* checkbox doesn't draw any transparent parts. If there
* were, we'd have to clear the context each time otherwise
* the transparent parts would start to become more opaque
* with each redraw.
*/

var width = canvas.width;
var height = canvas.height;

// Draw border
ctx.fillStyle = "#000000"; // border color
ctx.fillRect(0,0,width,height);

// Paint the background
ctx.fillStyle = "#f0f0f0"; // background color
ctx.fillRect(1,1,width-2,height-2);

// Draw "check" inside of box, if we're supposed to
if(checked) {
ctx.beginPath();
ctx.moveTo(width/4,height/2);
ctx.lineTo(2*width/5,height-height/4);
ctx.lineTo(width-width/4,height/4);
ctx.stroke();
ctx.closePath();
}
}
draw();

// Defines a "getter" which is called each time a user tries to get the value of the public property "hOffset"
this.__defineGetter__("hOffset", function() {
return frame.hOffset;
});

// Defines a "setter" which is called each time a user tries to set the value of the public property "hOffset"
this.__defineSetter__("hOffset", function(value) {
frame.hOffset = value;
});

this.__defineGetter__("vOffset", function() {
return frame.vOffset;
});
this.__defineSetter__("vOffset", function(value) {
frame.vOffset = value;
});

this.__defineGetter__("checked", function() {
return checked;
});
this.__defineSetter__("checked", function(value) {
// Convert inputs to booleans, null/undefined -> false, etc.
checked = Boolean(value);
draw();
});

// Make the text property public, so we can use it to identify this checkbox later.
this.__defineGetter__("label", function() {
return text.data;
});
this.__defineGetter__("label", function(value) {
text.data = value;
});
}


It is over commented to demonstrate how it all works, not because I like doing so. Accessible from the outside is "checked" (state), hOffset and vOffset (position), and "label" (title/text-label).
Constructor takes the frame to which it will belong, it's text label, and initial state.

Height of the checkbox is nominally 15px.




A result of starting my server over, links from my posts may not work (especially those in the "temp" subdomain). If there is a link to something of which anyone would like to have a copy, personal message me with what you're looking for along with a way to provide this to you, and I'll see if I can find a copy. Thanks for your patience and understanding.


IPB Image - "Not just another open source project. Lend your talent and make a difference!" (Dead)

IPB Image - "The future is now." (No longer community site) (Domain has lapsed)

Published: AtomicComicBlast, Barra de Lenguas, ComicWizard-4.0, MicroColors, PassGen, ScrabbleChecker, SoundBank, Uni, VisualWidget, WarpedReality
Unavailable: Paradigm [clock], Puzzled, SecurityLogger, Wayback Widget
Ready to be published: Cαlcυlατοr, CursorTails, Blackout, Block Puzzler, BombSquad, Palette, SnipIt
ActiveDev:
InactiveDev/Dead: BeatMod, Bubble Pop, Canvas Clock, Canvas Gauges, Canvas Pro, Clipboard, Crayon, Hermes, InTune, Konverter, Magic Deck, OverRuled, Outside, Slither, SystemBeat, Tetresque, Tetrad, Widget, WinSysRemote
Dropped: BlankScreen, Document "Fixer", Intuitive [ -> Blackout], Motion Widget: HHGTTG
CoDevelopment: Atmosphere, Block Puzzler,
BombSquad
Miniature Scripts: BinarySearchTree, Calendar, Canvas Gears, Checkbox, File-Browser, LinkedList/Stack, MDI Setup, MiniMax AI, PieGraph, ProgressBar, Slider widget, TabbedPane, Table, Tokenizer, TreeMenu
Java+: Java Music Daemon, ScreenCapture JAR, Widget-Java/Server Bridge Example
"Published" Texts: DynamicWidgetGuide
Konfabulator Libraries: Color-space Library, Javable Widget Project
Widget Tutorials: "Spawning" Widgets, JavaScript Classes
Contests: Widget 4k - "Expanded" [not happening; canceled]
Non-Widget Work: Hazlenut, Konspirators Online, PHP BB-Code Parser, ShortClient, Zap
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
  Reply to this topic    Start new topic  
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: