Skip to content

Commit

Permalink
checkbuttons
Browse files Browse the repository at this point in the history
  • Loading branch information
roseman committed Dec 2, 2011
1 parent 3aa3451 commit 0e0d68f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 14 deletions.
17 changes: 16 additions & 1 deletion wtk-base.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ namespace eval ::wtk {
set options($opt) $var
$self _setuptextvar
}

# variable handling; only relevant if -variable option is delegated to us

}

proc getwidget {id} {return $wtk::wobj($id)}
Expand All @@ -91,4 +94,16 @@ namespace eval ::wtk {
proc focus {w} {$w _focus; return ""}
proc bind {args} {; # placeholder}

}
# Macro that can be used to simplify the definition of any widget
snit::macro _stdwidget {} {
component W; delegate method * to W
constructor {args} {install W using Widget %AUTO% $self; $self configurelist $args}
}

# Macro that can be used to simplify the creation of widgets using -text and -textvariable
snit::macro _textvarwidget {} {
component W; delegate method * to W; delegate option -textvariable to W; delegate option -text to W
constructor {args} {install W using Widget %AUTO% $self; $self configurelist $args; $W _setuptextvar}
}

}
53 changes: 41 additions & 12 deletions wtk-widgets.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ namespace eval ::wtk {
# Widgets that receive events from the Javascript side are expected to implement the "_event"
# method, which is passed the widget-specific type of event and any parameters.

# Macro that can be used to simplify the definition of any widget
snit::macro _stdwidget {} {
component W; delegate method * to W
constructor {args} {install W using Widget %AUTO% $self; $self configurelist $args}
}

# Macro that can be used to simplify the creation of widgets using -text and -textvariable
snit::macro _textvarwidget {} {
component W; delegate method * to W; delegate option -textvariable to W; delegate option -text to W
constructor {args} {install W using Widget %AUTO% $self; $self configurelist $args; $W _setuptextvar}
}


# Button widgets
snit::type button {
Expand All @@ -43,6 +31,46 @@ namespace eval ::wtk {
method _textchangejs {txt} {return "[$self jqobj].html('$txt');"}
}

# Checkbutton
snit::type checkbutton {
_textvarwidget
variable currentvalue 0
option -command
option -onvalue -default 1 -configuremethod _onoffchanged
option -offvalue -default 0 -configuremethod _onoffchanged
option -variable -configuremethod _varnameset

# TODO : move -variable handling into generic widget base
method _createjs {} {set r "wtk.createCheckButton('[$self id]','[$self cget -text]');"; if {$currentvalue==$options(-onvalue)} {append r "[$self jsobj].childNodes\[0\].checked=true;"}; return $r}
method _textchangejs {txt} {return "[$self jqobj].children(':last').html('$txt');"}
method _event {which} {
if {$which in "checked unchecked"} {
if {$which=="checked"} {set val $options(-onvalue)} else {set val $options(-offvalue)}
$self _changevalue $val 1; uplevel #0 $options(-command)
}
}
method _varnameset {opt var} {set options($opt) $var;
if {$var!=""} {
if {![uplevel #0 info exists $var]} {uplevel #0 set $var $currentvalue} else {set currentvalue [uplevel #0 set $var]}
uplevel #0 trace add variable $var write [list [list $self _varchanged]]
}
}
method _onoffchanged {opt val} {if {$currentvalue==$options($opt)} {set options($opt) $val; $self _changevalue $val} else {set options($opt) $val}}
method _varchanged {args} {if {$currentvalue ne [uplevel #0 set $options(-variable)]} {$self _changevalue [uplevel #0 set $options(-variable)]}}; # trace callback
method _changevalue {newval {fromwidget 0}} {
if {[$self _created?] && !$fromwidget} {
if {$newval eq $options(-onvalue) && $options(-onvalue) ne $currentvalue} {
wtk::toclient "[$self jsobj].childNodes\[0\].checked=true;"
} elseif {$newval ne $options(-onvalue) && $options(-onvalue) eq $currentvalue} {
wtk::toclient "[$self jsobj].childNodes\[0\].checked=false;"
}
}
set currentvalue $newval
if {$options(-variable) ne ""} {uplevel #0 set $options(-variable) [list $newval]}
}

}

# Entry widgets
snit::type entry {
_textvarwidget
Expand All @@ -52,6 +80,7 @@ namespace eval ::wtk {
method _event {which args} {if {$which eq "value"} {$self _textchanged -text $args 1}}
method _widthchanged {opt val} {set options($opt) $val; if {[$self _created?]} {wtk::toclient "[$self jsobj].size=$val;"}}
}


# Frame
snit::type frame {
Expand Down
13 changes: 12 additions & 1 deletion wtk.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var wtk = {
},

poller : function() {$.ajax({type:'GET', url:'wtkpoll.html?sessionid='+wtk.sessionid, dataType:'script',
success: function() {setTimeout(wtk.poller,100);}});},
complete: function() {setTimeout(wtk.poller,100);},
error: function(jqXHR, textStatus, errorThrown) {console.log('ajax error '+textStatus+' '+errorThrown);}});},
sendto : function(msg) { $.get('wtkcb.html?sessionid='+wtk.sessionid, {cmd : msg});},

/*
Expand Down Expand Up @@ -43,6 +44,16 @@ var wtk = {

createFrame : function(id) { wtk.CreateWidget(id, 'div', '', '');},

createCheckButton : function(id,txt) {
var w = wtk.CreateWidget(id,'span', '', '');
var c = w.appendChild(document.createElement('input'));
var l = w.appendChild(document.createElement('span'));
c.type = 'checkbox';
c.onclick = function() {wtk.checkButtonClicked(id);};
l.innerHTML = txt;
},
checkButtonClicked : function(id) { var ev; if (wtk.widgets[id].childNodes[0].checked==true) {ev='checked';} else {ev='unchecked';}; wtk.sendto('EVENT ' + id + ' ' + ev);},

/*
* Grid .
*/
Expand Down
32 changes: 32 additions & 0 deletions wtk.test
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ test wtk-1.6 {focus} {
} {}


test wtk-1.7 {checkbutton, especially -variable handling} {
# variable doesn't exist
assert {![info exists ::v1]}
assert_equal "" [wtk::grid [wtk::checkbutton .cb -text Checker -variable v1 -onvalue 9 -offvalue 3]]
assert_equal "wtk.createCheckButton('obj4','Checker')" [lindex [split [jsmsgs] ";"] 0]
assert_equal 3 $::v1
wtk::fromclient "EVENT obj4 checked"
assert_equal 9 $::v1
wtk::fromclient "EVENT obj4 unchecked"
assert_equal 3 $::v1
set ::v1 5
assert_equal "" [jsmsgs]
set ::v1 9
assert_equal {wtk.widgets['obj4'].childNodes[0].checked=true;} [jsmsgs]
set ::v1 3
assert_equal {wtk.widgets['obj4'].childNodes[0].checked=false;} [jsmsgs]

# variable exists, is on value
set ::v2 5
wtk::grid [wtk::checkbutton .cb2 -variable v2 -onvalue 5]
lassign [lrange [split [jsmsgs] ";"] 0 1] create set
assert_equal "wtk.createCheckButton('obj5','')" $create
assert_equal {wtk.widgets['obj5'].childNodes[0].checked=true} $set

# variable exists, is not on value
set ::v3 25
wtk::grid [wtk::checkbutton .cb3 -variable v3 -onvalue 5]
lassign [lrange [split [jsmsgs] ";"] 0 1] create set
assert_equal "wtk.createCheckButton('obj6','')" $create
assert_no_match "checked" $set
} {}

wtk::_reset

test wtk-2.1 {grid debugging, start state} {
Expand Down

0 comments on commit 0e0d68f

Please sign in to comment.