## numeric_chooser.pkg
## (C) 1999, Bremen Institute for Safe Systems, Universitaet Bremen
## Author: ludi
# Compiled by:
#
src/lib/tk/src/toolkit/sources.sublib# ***************************************************************************
# Numeric Choosers
# **************************************************************************
### "There is a computer disease that anybody
### who works with computers knows about.
###
### "It's a very serious disease and it
### interferes completely with the work.
###
### "The trouble with computers is that
### you play with them!"
###
### -- Richard P. Feynman
package numeric_chooser: (weak) Numeric_Chooser { # Numeric_Chooser is from
src/lib/tk/src/toolkit/numeric_chooser.api fun numeric_chooser {
initial_value,
min,
max,
increment,
width,
orientation,
selection_notifier
}
=
{ include package tk;
label_id = make_widget_id ();
state = REF initial_value;
fun to_string i
=
if (i >= 0) int::to_string i;
else ("-" + int::to_string (abs i));
fi;
fun change b _
=
if ( (b and not_null max and *state < the max)
or (b and not (not_null max))
or (not b and not_null min and *state > the min)
or (not b and not (not_null min))
)
state := b ?? *state + increment
:: *state - increment;
add_trait label_id [TEXT (to_string *state)];
selection_notifier *state;
fi;
path = winix__premicrothread::path::cat (get_lib_path(),
"icons/numeric_chooser");
left =
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "left.gif"},
make_image_id());
left_highlighted
=
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "left_highlighted.gif"},
make_image_id());
right
=
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "right.gif"},
make_image_id());
right_highlighted
=
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "right_highlighted.gif"},
make_image_id());
up =
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "up.gif"},
make_image_id());
up_highlighted
=
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "up_highlighted.gif"},
make_image_id());
down =
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "down.gif"},
make_image_id());
down_highlighted
=
FILE_IMAGE
(winix__premicrothread::path::make_path_from_dir_and_file { dir => path,
file => "down_highlighted.gif"},
make_image_id());
fun ch_icon id icon _
=
add_trait id [ICON icon];
arrow1
=
{ id = make_widget_id();
ic = if (orientation == HORIZONTAL ) left; else up;fi;
ic_highlighted =
if (orientation == HORIZONTAL ) left_highlighted;
else up_highlighted;fi;
inc = not (orientation == HORIZONTAL);
BUTTON {
widget_id => id,
packing_hints =>
if (orientation == HORIZONTAL ) [PACK_AT LEFT];
else [];fi,
traits => [ICON ic],
event_callbacks
=>
[ EVENT_CALLBACK (ENTER, ch_icon id ic_highlighted),
EVENT_CALLBACK (LEAVE, ch_icon id ic),
EVENT_CALLBACK (BUTTON_PRESS (THE 1), change inc)
]
};
};
lab
=
LABEL {
widget_id => label_id,
event_callbacks => [],
packing_hints => if (orientation == HORIZONTAL ) [PACK_AT LEFT];
else [];fi,
traits => [ BACKGROUND WHITE,
WIDTH width,
TEXT (int::to_string initial_value)
]
};
arrow2
=
{
id = make_widget_id ();
ic = orientation == HORIZONTAL ?? right :: down;
ic_highlighted
=
if (orientation == HORIZONTAL ) right_highlighted;
else down_highlighted;fi;
inc = orientation == HORIZONTAL;
BUTTON
{ widget_id => id,
packing_hints => orientation == HORIZONTAL ?? [PACK_AT LEFT] :: [],
traits => [ICON ic],
event_callbacks
=>
[ EVENT_CALLBACK (ENTER, ch_icon id ic_highlighted),
EVENT_CALLBACK (LEAVE, ch_icon id ic),
EVENT_CALLBACK (BUTTON_PRESS (THE 1), change inc)
]
}
;};
wids = [arrow1, lab, arrow2];
fun read_value ()
=
*state;
fun set_value i
=
if ( (not_null min and i < the min)
or (not_null max and i > the max)
)
print "NumericChooser: set_value with value out of range, ignoring...";
else
state := i;
add_trait label_id [TEXT (int::to_string i)];
fi;
{ chooser => FRAME { widget_id => make_widget_id(),
subwidgets => PACKED wids,
packing_hints => [],
traits => [],
event_callbacks => []
},
set_value,
read_value
};
};
};