2021 lines
82 KiB
Ziran
2021 lines
82 KiB
Ziran
// Optional Tk command model, following Inferno libtk and tcl_tk.b. See ../NOTICE.
|
|
#import "std/vec"
|
|
#import "std/number_text"
|
|
using Tcl :: #import "tcl/interpreter"
|
|
using T :: #import "tcl/types"
|
|
using UTF :: #import "tk/text"
|
|
using View :: #import "tk/view"
|
|
using Scale :: #import "tk/scale"
|
|
using Check :: #import "tk/check"
|
|
using Scroll :: #import "tk/scrollbar"
|
|
using Canvas :: #import "tk/canvas"
|
|
using Lists :: #import "tcl/lists"
|
|
#if #defined(TK_LISTBOX) {
|
|
using Listbox :: #import "tk/listbox"
|
|
}
|
|
|
|
#if #defined(TK_MENU) {
|
|
using Menu :: #import "tk/menu"
|
|
}
|
|
|
|
#if #defined(TK_BITMAP) {
|
|
using Bitmap :: #import "tk/bitmap"
|
|
using Images :: #import "tk/image"
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
using Fonts :: #import "tk/font"
|
|
}
|
|
}
|
|
|
|
MAX_WIDGETS :: 32;
|
|
MAX_ENTRY_BYTES :: 16384;
|
|
LABEL :: 0;
|
|
BUTTON :: 1;
|
|
FRAME :: 2;
|
|
ENTRY :: 3;
|
|
TEXT :: 4;
|
|
SCALE :: 5;
|
|
PANEL :: 6;
|
|
CHECKBUTTON :: 7;
|
|
SCROLLBAR :: 8;
|
|
CANVAS :: 9;
|
|
LISTBOX :: 10;
|
|
MENU :: 11;
|
|
MENUBUTTON :: 12;
|
|
RADIOBUTTON :: 13;
|
|
|
|
SelectionButton :: (kind: s64) -> bool {
|
|
return kind == CHECKBUTTON || kind == RADIOBUTTON;
|
|
}
|
|
MAX_PANEL_BYTES :: 16777216;
|
|
CHAR_WIDTH :: Canvas.CELL_WIDTH;
|
|
ENTRY_INSET :: 8;
|
|
ROOT_INSET :: 12;
|
|
DEFAULT_PAD_X :: 8;
|
|
DEFAULT_PAD_Y :: 5;
|
|
Box :: struct { x: s32; y: s32; w: s32; h: s32; }
|
|
Packing :: struct {
|
|
side: s32;
|
|
padx: s32;
|
|
pady: s32;
|
|
fill: s32;
|
|
expand: s32;
|
|
anchor_x: s32;
|
|
anchor_y: s32;
|
|
#if #defined(TK_BITMAP) {
|
|
ipadx: s32;
|
|
ipady: s32;
|
|
master: s64;
|
|
}
|
|
}
|
|
Panel :: struct {
|
|
pixels: Vec(u8);
|
|
width: s32;
|
|
height: s32;
|
|
border: s32;
|
|
}
|
|
Widget :: struct {
|
|
name: Text;
|
|
text: Text;
|
|
command: Text;
|
|
box: Box;
|
|
kind: s64;
|
|
parent: s64;
|
|
cursor: s64;
|
|
view: View.State;
|
|
scale: Scale.State;
|
|
panel: Panel;
|
|
check: Check.State;
|
|
scroll: Scroll.State;
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
canvas: Canvas.State;
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
listbox: Listbox.State;
|
|
entry_width: s32;
|
|
}
|
|
#if #defined(TK_MENU) {
|
|
menu: Menu.State;
|
|
}
|
|
#if #defined(TK_BITMAP) {
|
|
style: Style;
|
|
}
|
|
text_height: s32;
|
|
last_scroll: Scroll.State;
|
|
sent_scroll: bool;
|
|
disabled: bool;
|
|
requested: Box;
|
|
order: s32;
|
|
pack: Packing;
|
|
}
|
|
|
|
// One root per application.
|
|
widgets: Vec(Widget);
|
|
attached: bool;
|
|
closed: bool;
|
|
pressed: s64 = -1;
|
|
pressed_range: s64 = -1;
|
|
focused: s64 = -1;
|
|
sequence: s32;
|
|
drag_start: s32;
|
|
drag_value: s64;
|
|
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
canvas_hover: s64 = -1;
|
|
canvas_buttons: s32;
|
|
canvas_x: s32;
|
|
canvas_y: s32;
|
|
canvas_pointer_known: bool;
|
|
last_canvas_button: s32;
|
|
last_canvas_press: s64 = -1;
|
|
}
|
|
|
|
#if #defined(TK_MENU) {
|
|
popup: s64 = -1;
|
|
popup_owner: s64 = -1;
|
|
popup_x: s32;
|
|
popup_y: s32;
|
|
viewport: Box;
|
|
}
|
|
|
|
#if #defined(TK_BITMAP) {
|
|
Relief :: enum { Flat; Raised; Sunken; Ridge; Groove; }
|
|
RELIEFS :: string.["flat", "raised", "sunken", "ridge", "groove"];
|
|
Style :: struct {
|
|
background: s32;
|
|
foreground: s32;
|
|
border: s32;
|
|
relief: Relief;
|
|
bitmap: s64;
|
|
image: s64;
|
|
}
|
|
image_store: Images.Store;
|
|
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
font_store: Fonts.Store;
|
|
}
|
|
|
|
SetImageLoader :: (loader: Images.Loader) {
|
|
image_store.open = loader;
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
font_store.open = loader
|
|
}
|
|
}
|
|
|
|
ImageCommand :: (_: s64, args: []Text, result: *Result) { Images.Command(*image_store, args, result) }
|
|
|
|
LabelImage :: (widget: *Widget) -> *Images.Entry {
|
|
index := ifx widget.style.image >= 0 then widget.style.image else widget.style.bitmap;
|
|
if index < 0 { return null }
|
|
return *image_store.items[index];
|
|
}
|
|
|
|
ClearImages :: (style: *Style) {
|
|
Images.Release(*image_store, style.bitmap);
|
|
Images.Release(*image_store, style.image);
|
|
style.bitmap = -1;
|
|
style.image = -1;
|
|
}
|
|
|
|
ConfigureAppearance :: (widget: *Widget, args: []Text, result: *Result) -> bool {
|
|
style := widget.style;
|
|
bitmap_source: string;
|
|
bitmap_changed := false;
|
|
text: string;
|
|
text_changed := false;
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
value := TextView(args[2*i+1]);
|
|
if option == "-bg" || option == "-background" || option == "-fg" || option == "-foreground" {
|
|
color, valid := Canvas.ParseColor(value);
|
|
if !valid { SetError(result, "bad value"); return false }
|
|
if option[1] == 98 { style.background = color } else { style.foreground = color }
|
|
} else if option == "-bd" || option == "-borderwidth" {
|
|
border, valid := Distance(value);
|
|
if !valid { SetError(result, "bad value"); return false }
|
|
style.border = border;
|
|
} else if option == "-relief" {
|
|
found := false;
|
|
for RELIEFS {
|
|
if value == it { style.relief = cast(Relief)it_index; found = true; break }
|
|
}
|
|
if !found { SetError(result, "bad value"); return false }
|
|
} else if widget.kind == LABEL && (option == "-text" || option == "-label") {
|
|
text = value;
|
|
text_changed = true;
|
|
} else if widget.kind == LABEL && option == "-bitmap" {
|
|
bitmap_source = value;
|
|
bitmap_changed = true;
|
|
} else if widget.kind == LABEL && option == "-image" {
|
|
style.image = Images.Find(*image_store, value);
|
|
if style.image < 0 && value != "" { SetError(result, "bad value"); return false }
|
|
} else { SetError(result, "unsupported widget option or value"); return false }
|
|
}
|
|
updated: Text;
|
|
if text_changed && !BuilderAppend(updated.bytes, text) {
|
|
SetError(result, "could not allocate label text"); return false;
|
|
}
|
|
if bitmap_changed {
|
|
style.bitmap = -1;
|
|
if bitmap_source != "" && bitmap_source[0] != 45 {
|
|
pixels: Bitmap.Bitmap;
|
|
if !Images.Read(*image_store, bitmap_source, *pixels) {
|
|
SetError(result, "failed to load bitmap"); return false;
|
|
}
|
|
style.bitmap = Images.Allocate(*image_store, *pixels);
|
|
if style.bitmap < 0 { SetError(result, "image limit exceeded"); return false }
|
|
}
|
|
Images.Release(*image_store, widget.style.bitmap);
|
|
}
|
|
if style.image != widget.style.image {
|
|
Images.Retain(*image_store, style.image);
|
|
Images.Release(*image_store, widget.style.image);
|
|
}
|
|
widget.style = style;
|
|
if text_changed { VecSwap(widget.text.bytes, updated.bytes) }
|
|
return true;
|
|
}
|
|
|
|
AppearanceOption :: (widget: *Widget, option: string, result: *Result) -> bool {
|
|
style := widget.style;
|
|
color: s32;
|
|
if option == "-bg" || option == "-background" {
|
|
color = ifx style.background == Canvas.NONE then 0xdddddd else style.background;
|
|
} else if option == "-fg" || option == "-foreground" {
|
|
color = ifx style.foreground == Canvas.NONE then 0 else style.foreground;
|
|
} else {
|
|
if option == "-bd" || option == "-borderwidth" {
|
|
digits: [24]u8;
|
|
unused BuilderAppend(result.value.bytes, FormatInteger(cast(s64)style.border, digits[:]));
|
|
}
|
|
else if option == "-relief" { unused BuilderAppend(result.value.bytes, RELIEFS[cast(s64)style.relief]) }
|
|
else if widget.kind == LABEL && (option == "-text" || option == "-label") {
|
|
unused BuilderAppend(result.value.bytes, TextView(widget.text));
|
|
} else if widget.kind == LABEL && (option == "-bitmap" || option == "-image") {
|
|
index := ifx option == "-bitmap" then style.bitmap else style.image;
|
|
unused BuilderAppend(result.value.bytes, ifx index >= 0 then "1" else "0");
|
|
} else { return false }
|
|
return true;
|
|
}
|
|
hex := "0123456789abcdef";
|
|
unused BuilderAppend(result.value.bytes, "#");
|
|
for shift: 0..5 { unused VecPush(result.value.bytes, hex[(color >> ((5-shift)*4)) & 15]) }
|
|
unused BuilderAppend(result.value.bytes, "ff");
|
|
return true;
|
|
}
|
|
}
|
|
|
|
Attach :: (interpreter: *Tcl.Interpreter) -> bool {
|
|
if attached { return false }
|
|
commands := interpreter.extensions.count;
|
|
prefixes := interpreter.prefixes.count;
|
|
attached = Tcl.RegisterCommand(interpreter, "label", Create, LABEL) &&
|
|
Tcl.RegisterCommand(interpreter, "button", Create, BUTTON) &&
|
|
Tcl.RegisterCommand(interpreter, "frame", Create, FRAME) &&
|
|
Tcl.RegisterCommand(interpreter, "entry", Create, ENTRY) &&
|
|
Tcl.RegisterCommand(interpreter, "text", Create, TEXT) &&
|
|
Tcl.RegisterCommand(interpreter, "scale", Create, SCALE) &&
|
|
Tcl.RegisterCommand(interpreter, "panel", Create, PANEL) &&
|
|
Tcl.RegisterCommand(interpreter, "checkbutton", Create, CHECKBUTTON) &&
|
|
Tcl.RegisterCommand(interpreter, "radiobutton", Create, RADIOBUTTON) &&
|
|
Tcl.RegisterCommand(interpreter, "scrollbar", Create, SCROLLBAR);
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
attached = attached && Tcl.RegisterCommand(interpreter, "canvas", Create, CANVAS);
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
attached = attached && Tcl.RegisterCommand(interpreter, "listbox", Create, LISTBOX);
|
|
}
|
|
#if #defined(TK_MENU) {
|
|
attached = attached && Tcl.RegisterCommand(interpreter, "menu", Create, MENU) &&
|
|
Tcl.RegisterCommand(interpreter, "menubutton", Create, MENUBUTTON);
|
|
}
|
|
#if #defined(TK_BITMAP) {
|
|
attached = attached && Tcl.RegisterCommand(interpreter, "image", ImageCommand);
|
|
}
|
|
attached = attached &&
|
|
Tcl.RegisterCommand(interpreter, "variable", VariableCommand) &&
|
|
Tcl.RegisterPrefix(interpreter, "update", Update) &&
|
|
Tcl.RegisterCommand(interpreter, "pack", Pack) &&
|
|
Tcl.RegisterCommand(interpreter, "focus", Focus) &&
|
|
Tcl.RegisterCommand(interpreter, "bind", Bind) &&
|
|
Tcl.RegisterCommand(interpreter, "destroy", Destroy) &&
|
|
Tcl.RegisterPrefix(interpreter, ".", WidgetCommand);
|
|
if !attached {
|
|
unused VecResize(interpreter.extensions, commands);
|
|
unused VecResize(interpreter.prefixes, prefixes);
|
|
}
|
|
return attached;
|
|
}
|
|
|
|
Find :: (name: string) -> s64 {
|
|
for * widgets { if it.name.bytes.count > 0 && TextView(it.name) == name { return it_index } }
|
|
return -1;
|
|
}
|
|
|
|
ParentEnd :: (path: string) -> s64 {
|
|
if path.count < 2 || path[0] != 46 || path[path.count-1] == 46 { return -1 }
|
|
parent_end: s64;
|
|
for 1..path.count-1 {
|
|
if path[it] == 46 {
|
|
if it == parent_end + 1 { return -1 }
|
|
parent_end = it;
|
|
}
|
|
}
|
|
return parent_end;
|
|
}
|
|
|
|
SetError :: (result: *Result, message: string) {
|
|
result.code = .Error;
|
|
unused BuilderAppend(result.value.bytes, message);
|
|
}
|
|
|
|
Configure :: (widget: *Widget, args: []Text, result: *Result, path: string = "") -> bool {
|
|
if args.count % 2 != 0 { SetError(result, "expected option and value pairs"); return false }
|
|
if widget.kind == SCALE { return ConfigureScale(widget, args, result) }
|
|
if widget.kind == PANEL { return ConfigurePanel(widget, args, result) }
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
if widget.kind == CANVAS { return Canvas.Configure(*widget.canvas, args, result) }
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if widget.kind == LISTBOX { return ConfigureListbox(widget, args, result) }
|
|
}
|
|
#if #defined(TK_MENU) {
|
|
if widget.kind == MENU { return Menu.Configure(*widget.menu, args, result) }
|
|
}
|
|
if widget.kind == TEXT || widget.kind == SCROLLBAR { return ConfigureScroll(widget, args, result) }
|
|
#if #defined(TK_BITMAP) {
|
|
if widget.kind == LABEL || widget.kind == FRAME { return ConfigureAppearance(widget, args, result) }
|
|
}
|
|
button := widget.kind == BUTTON || SelectionButton(widget.kind);
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
if (option == "-text" || option == "-label" && SelectionButton(widget.kind)) &&
|
|
(widget.kind == LABEL || widget.kind == MENUBUTTON || button) { continue }
|
|
if option == "-command" && button || option == "-menu" && widget.kind == MENUBUTTON { continue }
|
|
if option == "-state" && (button || widget.kind == MENUBUTTON) {
|
|
value := TextView(args[2*i+1]);
|
|
if value == "normal" || value == "disabled" { continue }
|
|
}
|
|
if SelectionButton(widget.kind) {
|
|
supported := Check.Option(*widget.check, option);
|
|
if supported && (option != "-indicatoron" || Check.Indicator(TextView(args[2*i+1])) != 0) { continue }
|
|
}
|
|
if option == "-bg" && widget.kind == ENTRY && TextView(args[2*i+1]) == "white" { continue }
|
|
#if #defined(TK_LISTBOX) {
|
|
if option == "-width" && widget.kind == ENTRY {
|
|
_, valid := Distance(TextView(args[2*i+1]));
|
|
if valid { continue }
|
|
}
|
|
}
|
|
SetError(result, "unsupported widget option or value");
|
|
return false;
|
|
}
|
|
if SelectionButton(widget.kind) && !Check.Configure(*widget.check, args, path, false) {
|
|
SetError(result, "could not configure selection button");
|
|
return false;
|
|
}
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
if option == "-state" { widget.disabled = TextView(args[2*i+1]) == "disabled"; continue }
|
|
selection_option := Check.Option(*widget.check, option);
|
|
if option == "-bg" || SelectionButton(widget.kind) && selection_option { continue }
|
|
#if #defined(TK_LISTBOX) {
|
|
if option == "-width" {
|
|
pixels, _ := Distance(TextView(args[2*i+1]));
|
|
widget.entry_width = pixels;
|
|
continue;
|
|
}
|
|
}
|
|
value: Text = TextCopy(TextView(args[2*i+1]));
|
|
if option == "-text" || option == "-label" { VecSwap(widget.text.bytes, value.bytes) }
|
|
else { VecSwap(widget.command.bytes, value.bytes) }
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Create :: (kind: s64, args: []Text, result: *Result) {
|
|
if closed || args.count == 0 { SetError(result, "expected a widget path in an open root"); return }
|
|
path := TextView(args[0]);
|
|
parent_end := ParentEnd(path);
|
|
if parent_end < 0 || Find(path) >= 0 { SetError(result, "invalid or duplicate widget path"); return }
|
|
parent: s64 = -1;
|
|
if parent_end > 0 {
|
|
parent = Find(path[:parent_end]);
|
|
if parent < 0 || (kind != MENU && widgets[parent].kind != FRAME) { SetError(result, "expected a frame parent"); return }
|
|
}
|
|
widget := Widget.{name = TextCopy(path), kind = kind, parent = parent};
|
|
#if #defined(TK_BITMAP) {
|
|
widget.style = .{background = Canvas.NONE, foreground = Canvas.NONE, bitmap = -1, image = -1};
|
|
}
|
|
if kind == SCALE { widget.scale = Scale.Default() }
|
|
#if #defined(TK_MENU) {
|
|
if kind == MENU { Menu.Initialize(*widget.menu) }
|
|
}
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
if kind == CANVAS {
|
|
widget.canvas = Canvas.Default();
|
|
#if #defined(TK_BITMAP) {
|
|
widget.canvas.fonts = *font_store
|
|
}
|
|
}
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if kind == LISTBOX { widget.listbox = Listbox.Default() }
|
|
}
|
|
widget.check.radio = kind == RADIOBUTTON;
|
|
slot := widgets.count;
|
|
for * widgets { if it.name.bytes.count == 0 { slot = it_index; break } }
|
|
if slot == widgets.count {
|
|
if slot == MAX_WIDGETS { SetError(result, "widget limit exceeded"); return }
|
|
if !VecResize(widgets, slot + 1) { SetError(result, "could not allocate widget"); return }
|
|
}
|
|
if !Configure(*widget, args[1:], result, path) { return }
|
|
if kind == SCALE && widget.scale.big_increment == 0 {
|
|
widget.scale.big_increment = ((widget.scale.last - widget.scale.first) / Scale.UNIT / 10) * Scale.UNIT;
|
|
}
|
|
saved := *widgets[slot];
|
|
#if #defined(TK_BITMAP) {
|
|
saved.style = widget.style;
|
|
}
|
|
saved.kind = widget.kind;
|
|
saved.parent = widget.parent;
|
|
saved.cursor = 0;
|
|
saved.disabled = widget.disabled;
|
|
saved.scale = widget.scale;
|
|
saved.scroll = widget.scroll;
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
saved.canvas.viewport = widget.canvas.viewport;
|
|
VecSwap(saved.canvas.yscroll.bytes, widget.canvas.yscroll.bytes);
|
|
saved.canvas.width = widget.canvas.width;
|
|
saved.canvas.height = widget.canvas.height;
|
|
saved.canvas.precise_width = widget.canvas.precise_width;
|
|
saved.canvas.precise_height = widget.canvas.precise_height;
|
|
saved.canvas.background = widget.canvas.background;
|
|
saved.canvas.border = widget.canvas.border;
|
|
saved.canvas.sunken = widget.canvas.sunken;
|
|
#if #defined(TK_BITMAP) {
|
|
saved.canvas.images = *image_store;
|
|
saved.canvas.fonts = widget.canvas.fonts;
|
|
saved.canvas.font = widget.canvas.font;
|
|
}
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
saved.listbox.width = widget.listbox.width;
|
|
saved.listbox.height = widget.listbox.height;
|
|
saved.listbox.visible = widget.listbox.visible;
|
|
saved.listbox.browse = widget.listbox.browse;
|
|
saved.listbox.top = 0;
|
|
saved.listbox.active = -1;
|
|
saved.listbox.anchor = -1;
|
|
saved.entry_width = widget.entry_width;
|
|
}
|
|
#if #defined(TK_MENU) {
|
|
saved.menu.active = widget.menu.active;
|
|
saved.menu.border = widget.menu.border;
|
|
}
|
|
saved.text_height = widget.text_height;
|
|
saved.sent_scroll = false;
|
|
Check.Swap(*saved.check, *widget.check);
|
|
saved.panel.border = widget.panel.border;
|
|
saved.panel.width = 0;
|
|
saved.panel.height = 0;
|
|
View.Reset(*saved.view);
|
|
saved.order = 0;
|
|
saved.box = .{};
|
|
saved.pack = .{padx = DEFAULT_PAD_X, pady = DEFAULT_PAD_Y};
|
|
#if #defined(TK_BITMAP) {
|
|
saved.pack.master = parent
|
|
}
|
|
VecSwap(saved.name.bytes, widget.name.bytes);
|
|
VecSwap(saved.text.bytes, widget.text.bytes);
|
|
VecSwap(saved.command.bytes, widget.command.bytes);
|
|
unused BuilderAppend(result.value.bytes, path);
|
|
}
|
|
|
|
Pack :: (_: s64, args: []Text, result: *Result) {
|
|
if args.count == 0 { SetError(result, "expected pack path ?path ...? ?options?"); return }
|
|
#if #defined(TK_BITMAP) {
|
|
if TextView(args[0]) == "propagate" {
|
|
if args.count != 3 || TextView(args[1]) != "." {
|
|
SetError(result, "bad value"); return;
|
|
}
|
|
value := TextView(args[2]);
|
|
// The host supplies the root's fixed viewport.
|
|
if !T.WordIn("0 1 no yes off on false true", value) { SetError(result, "bad value") }
|
|
return;
|
|
}
|
|
}
|
|
count: s64;
|
|
options := Packing.{side = -1, padx = -1, pady = -1, fill = -1, expand = -1, anchor_x = -2, anchor_y = -2};
|
|
#if #defined(TK_BITMAP) {
|
|
options.ipadx = -1;
|
|
options.ipady = -1;
|
|
options.master = -2;
|
|
}
|
|
while count < args.count && ParentEnd(TextView(args[count])) >= 0 {
|
|
index := Find(TextView(args[count]));
|
|
if index < 0 { SetError(result, "cannot pack unknown widget"); return }
|
|
if widgets[index].kind == MENU { SetError(result, "cannot pack a menu"); return }
|
|
count += 1;
|
|
}
|
|
if count == 0 || (args.count - count) % 2 != 0 { SetError(result, "invalid pack arguments"); return }
|
|
for i: 0..(args.count-count)/2-1 {
|
|
option := TextView(args[count+2*i]);
|
|
value := TextView(args[count+2*i+1]);
|
|
#if #defined(TK_BITMAP) {
|
|
if option == "-in" {
|
|
options.master = Find(value);
|
|
if value != "." && (options.master < 0 || widgets[options.master].kind != FRAME) {
|
|
SetError(result, "bad window path"); return;
|
|
}
|
|
continue;
|
|
}
|
|
if option == "-ipadx" || option == "-ipady" {
|
|
amount, valid := Distance(value);
|
|
if !valid { SetError(result, "invalid pack padding"); return }
|
|
if option == "-ipadx" { options.ipadx = amount } else { options.ipady = amount }
|
|
continue;
|
|
}
|
|
}
|
|
if option == "-side" {
|
|
if value == "top" { options.side = 0 }
|
|
else if value == "left" { options.side = 1 }
|
|
else if value == "bottom" { options.side = 2 }
|
|
else if value == "right" { options.side = 3 }
|
|
else { SetError(result, "unknown pack side"); return }
|
|
} else if option == "-padx" || option == "-pady" {
|
|
amount, ok := ParseInteger(value);
|
|
if !ok || amount < 0 || amount > 512 { SetError(result, "invalid pack padding"); return }
|
|
if option == "-padx" { options.padx = cast(s32)amount } else { options.pady = cast(s32)amount }
|
|
} else if option == "-fill" {
|
|
if value == "none" { options.fill = 0 }
|
|
else if value == "x" { options.fill = 1 }
|
|
else if value == "y" { options.fill = 2 }
|
|
else if value == "both" { options.fill = 3 }
|
|
else { SetError(result, "unknown pack fill"); return }
|
|
} else if option == "-anchor" {
|
|
if value == "center" { options.anchor_x = 0; options.anchor_y = 0 }
|
|
else if value == "w" || value == "e" || value == "n" || value == "s" ||
|
|
value == "nw" || value == "ne" || value == "sw" || value == "se" {
|
|
options.anchor_x = ifx value[value.count-1] == 119 then -1 else ifx value[value.count-1] == 101 then 1 else 0;
|
|
options.anchor_y = ifx value[0] == 110 then -1 else ifx value[0] == 115 then 1 else 0;
|
|
} else { SetError(result, "unknown pack anchor"); return }
|
|
} else if option == "-expand" {
|
|
if value == "1" { options.expand = 1 }
|
|
else if value == "0" { options.expand = 0 }
|
|
else { SetError(result, "expected 0 or 1 for pack -expand"); return }
|
|
} else { SetError(result, "unsupported pack option"); return }
|
|
}
|
|
#if #defined(TK_BITMAP) {
|
|
// Validate the proposed tree before changing any layout links.
|
|
masters: [MAX_WIDGETS]s64;
|
|
for * widgets { masters[it_index] = it.pack.master }
|
|
for 0..count-1 {
|
|
index := Find(TextView(args[it]));
|
|
masters[index] = ifx options.master == -2 then widgets[index].parent else options.master;
|
|
}
|
|
for 0..count-1 {
|
|
ancestor := Find(TextView(args[it]));
|
|
depth := 0;
|
|
while ancestor >= 0 && depth < widgets.count {
|
|
ancestor = masters[ancestor];
|
|
depth += 1;
|
|
}
|
|
if ancestor >= 0 { SetError(result, "cannot pack recursively"); return }
|
|
}
|
|
}
|
|
for 0..count-1 {
|
|
widget := *widgets[Find(TextView(args[it]))];
|
|
#if #defined(TK_BITMAP) {
|
|
widget.pack.master = ifx options.master == -2 then widget.parent else options.master;
|
|
if options.ipadx >= 0 { widget.pack.ipadx = options.ipadx }
|
|
if options.ipady >= 0 { widget.pack.ipady = options.ipady }
|
|
}
|
|
sequence += 1;
|
|
widget.order = sequence;
|
|
if options.side >= 0 { widget.pack.side = options.side }
|
|
if options.padx >= 0 { widget.pack.padx = options.padx }
|
|
if options.pady >= 0 { widget.pack.pady = options.pady }
|
|
if options.fill >= 0 { widget.pack.fill = options.fill }
|
|
if options.expand >= 0 { widget.pack.expand = options.expand }
|
|
if options.anchor_x >= -1 { widget.pack.anchor_x = options.anchor_x; widget.pack.anchor_y = options.anchor_y }
|
|
}
|
|
}
|
|
|
|
Destroy :: (_: s64, args: []Text, result: *Result) {
|
|
if args.count == 0 { SetError(result, "expected destroy path ?path ...?"); return }
|
|
for 0..args.count-1 {
|
|
path := TextView(args[it]);
|
|
if path == "." { closed = true; focused = -1; pressed = -1; return }
|
|
index := Find(path);
|
|
if index < 0 { continue }
|
|
for * widgets {
|
|
if it.name.bytes.count == 0 { continue }
|
|
ancestor := it_index;
|
|
while ancestor >= 0 && ancestor != index { ancestor = widgets[ancestor].parent }
|
|
if ancestor != index { continue }
|
|
#if #defined(TK_BITMAP) {
|
|
ClearImages(*it.style);
|
|
removed := it_index;
|
|
for * child: widgets {
|
|
if child.pack.master == removed {
|
|
child.order = 0;
|
|
child.pack.master = child.parent;
|
|
child.box = .{};
|
|
}
|
|
}
|
|
}
|
|
unused VecResize(it.name.bytes, 0);
|
|
unused VecResize(it.text.bytes, 0);
|
|
unused VecResize(it.command.bytes, 0);
|
|
unused VecResize(it.check.variable.bytes, 0);
|
|
unused VecResize(it.check.on.bytes, 0);
|
|
unused VecResize(it.check.off.bytes, 0);
|
|
unused VecResize(it.view.tags, 0);
|
|
unused VecResize(it.panel.pixels, 0);
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
Canvas.Clear(*it.canvas);
|
|
if canvas_hover == it_index { canvas_hover = -1 }
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
unused VecResize(it.listbox.rows, 0);
|
|
}
|
|
#if #defined(TK_MENU) {
|
|
if popup == it_index || popup_owner == it_index { UnpostMenu() }
|
|
if it.kind == MENU { Menu.Clear(*it.menu) }
|
|
}
|
|
View.Reset(*it.view);
|
|
it.order = 0;
|
|
it.box = .{};
|
|
if pressed == it_index { pressed = -1 }
|
|
if focused == it_index { focused = -1 }
|
|
}
|
|
}
|
|
}
|
|
|
|
WidgetCommand :: (_: s64, name: string, args: []Text, result: *Result, script: *Text) {
|
|
index := Find(name);
|
|
if index < 0 || closed { SetError(result, "unknown widget path"); return }
|
|
if args.count == 0 { SetError(result, "expected widget subcommand"); return }
|
|
widget := *widgets[index];
|
|
#if #defined(TK_MENU) {
|
|
if widget.kind == MENU { MenuCommand(index, args, result, script); return }
|
|
}
|
|
subcommand := TextView(args[0]);
|
|
if subcommand == "configure" && args.count >= 3 {
|
|
if Configure(widget, args[1:], result) {
|
|
if widget.kind == TEXT { TextScroll(widget, script) }
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
if widget.kind == CANVAS && CanvasScroll(*widget.canvas, script) {
|
|
unused BuilderAppend(script.bytes, "list\n");
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
if subcommand == "cget" && args.count == 2 {
|
|
option := TextView(args[1]);
|
|
#if #defined(TK_BITMAP) {
|
|
if (widget.kind == LABEL || widget.kind == FRAME) && AppearanceOption(widget, option, result) { return }
|
|
}
|
|
button := widget.kind == BUTTON || SelectionButton(widget.kind);
|
|
if (option == "-text" || option == "-label" && SelectionButton(widget.kind)) &&
|
|
(widget.kind == LABEL || widget.kind == MENUBUTTON || button) {
|
|
unused BuilderAppend(result.value.bytes, TextView(widget.text)); return;
|
|
}
|
|
if option == "-bg" && widget.kind == ENTRY {
|
|
unused BuilderAppend(result.value.bytes, "white"); return;
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if option == "-width" && widget.kind == ENTRY {
|
|
Listbox.Integer(result, EntryWidth(widget)); return;
|
|
}
|
|
}
|
|
if option == "-command" && button || option == "-menu" && widget.kind == MENUBUTTON {
|
|
unused BuilderAppend(result.value.bytes, TextView(widget.command)); return;
|
|
}
|
|
if option == "-state" && (button || widget.kind == MENUBUTTON) {
|
|
unused BuilderAppend(result.value.bytes, ifx widget.disabled then "disabled" else "normal"); return;
|
|
}
|
|
}
|
|
if subcommand == "invoke" && args.count == 1 && widget.kind == BUTTON {
|
|
if !widget.disabled { unused BuilderAppend(script.bytes, TextView(widget.command)) }
|
|
return;
|
|
}
|
|
if widget.kind == SCALE { ScaleCommand(widget, args, result); return }
|
|
if widget.kind == PANEL { PanelCommand(widget, args, result); return }
|
|
if SelectionButton(widget.kind) { CheckCommand(widget, args, result, script); return }
|
|
if widget.kind == SCROLLBAR { ScrollCommand(widget, args, result); return }
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
if widget.kind == CANVAS {
|
|
Canvas.Command(*widget.canvas, args, result, widget.box.y);
|
|
if subcommand == "yview" && CanvasScroll(*widget.canvas, script) {
|
|
// The Tcl callback replaces the command result, so return the saved value afterward.
|
|
if result.code == .Error {
|
|
unused BuilderAppend(script.bytes, "error");
|
|
Lists.AppendElement(*script.bytes, TextView(result.value));
|
|
result.code = .OK;
|
|
} else {
|
|
unused BuilderAppend(script.bytes, "lindex [list");
|
|
Lists.AppendElement(*script.bytes, TextView(result.value));
|
|
unused BuilderAppend(script.bytes, "] 0");
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if widget.kind == LISTBOX {
|
|
if subcommand == "cget" && args.count == 2 { ListboxOption(widget, TextView(args[1]), result); return }
|
|
Listbox.Command(*widget.listbox, args, result);
|
|
if result.code == .OK && result.value.bytes.count == 0 {
|
|
AppendScroll(widget, script, Listbox.Fractions(*widget.listbox));
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
if widget.kind == ENTRY { EntryCommand(widget, args, result); return }
|
|
if widget.kind == TEXT {
|
|
TextCommand(widget, args, result);
|
|
if result.code == .OK && result.value.bytes.count == 0 { TextScroll(widget, script) }
|
|
return;
|
|
}
|
|
SetError(result, "unsupported widget subcommand or arguments");
|
|
}
|
|
|
|
Focus :: (_: s64, args: []Text, result: *Result) {
|
|
if args.count == 0 {
|
|
if focused >= 0 { unused BuilderAppend(result.value.bytes, TextView(widgets[focused].name)) }
|
|
return;
|
|
}
|
|
if args.count != 1 { SetError(result, "expected focus ?widget?"); return }
|
|
index := Find(TextView(args[0]));
|
|
if closed || index < 0 || (widgets[index].kind != ENTRY && widgets[index].kind != TEXT &&
|
|
widgets[index].kind != SCALE && widgets[index].kind != LISTBOX) {
|
|
SetError(result, "expected a focusable widget path"); return;
|
|
}
|
|
focused = index;
|
|
}
|
|
|
|
Bind :: (_: s64, args: []Text, result: *Result) {
|
|
if args.count != 3 { SetError(result, "expected bind widget event script"); return }
|
|
index := Find(TextView(args[0]));
|
|
event := TextView(args[1]);
|
|
if closed || index < 0 { SetError(result, "bad binding widget"); return }
|
|
kind := widgets[index].kind;
|
|
entry := kind == ENTRY && (event == "<Return>" || event == "<Key-\n>");
|
|
canvas := kind == CANVAS && (event == "<ButtonPress-1>" || event == "<Button-1>");
|
|
if !entry && !canvas {
|
|
SetError(result, "unsupported widget binding"); return;
|
|
}
|
|
command: Text = TextCopy(TextView(args[2]));
|
|
VecSwap(widgets[index].command.bytes, command.bytes);
|
|
}
|
|
|
|
EntryOffset :: (widget: *Widget, index: string) -> (offset: s64, ok: bool) {
|
|
if index == "insert" { return widget.cursor, true }
|
|
if index == "end" { return widget.text.bytes.count, true }
|
|
number, ok := ParseInteger(index);
|
|
if !ok || number < 0 { return 0, false }
|
|
offset, good := UTF.Offset(TextView(widget.text), number);
|
|
return offset, good;
|
|
}
|
|
|
|
Insert :: (widget: *Widget, at: s64, value: string) -> bool {
|
|
if value.count > MAX_ENTRY_BYTES - widget.text.bytes.count || !UTF.SingleLine(value) { return false }
|
|
if value.count == 0 { return true }
|
|
after: Text;
|
|
{
|
|
before := TextView(widget.text);
|
|
if !BuilderAppend(after.bytes, before[:at]) || !BuilderAppend(after.bytes, value) ||
|
|
!BuilderAppend(after.bytes, before[at:]) { return false }
|
|
}
|
|
VecSwap(widget.text.bytes, after.bytes);
|
|
if at <= widget.cursor { widget.cursor += value.count }
|
|
return true;
|
|
}
|
|
|
|
Delete :: (widget: *Widget, first: s64, last: s64) {
|
|
if first >= last { return }
|
|
length := widget.text.bytes.count;
|
|
{
|
|
bytes := VecSlice(widget.text.bytes, 0, length);
|
|
for i: last..length-1 { bytes[first+i-last] = bytes[i] }
|
|
}
|
|
unused VecResize(widget.text.bytes, length - (last - first));
|
|
if last <= widget.cursor { widget.cursor -= last-first }
|
|
else if first < widget.cursor { widget.cursor = first }
|
|
}
|
|
|
|
EntryCommand :: (widget: *Widget, args: []Text, result: *Result) {
|
|
command := TextView(args[0]);
|
|
if command == "get" && args.count == 1 {
|
|
unused BuilderAppend(result.value.bytes, TextView(widget.text)); return;
|
|
}
|
|
valid := ((command == "index" || command == "icursor") && args.count == 2) ||
|
|
(command == "insert" && args.count == 3) || (command == "delete" && (args.count == 2 || args.count == 3));
|
|
if !valid { SetError(result, "unsupported entry subcommand or arguments"); return }
|
|
at, ok := EntryOffset(widget, TextView(args[1]));
|
|
if !ok { SetError(result, "bad entry index"); return }
|
|
if command == "index" {
|
|
value := T.Integer(UTF.Count(TextView(widget.text)[:at]));
|
|
VecSwap(result.value.bytes, value.value.bytes);
|
|
} else if command == "icursor" { widget.cursor = at }
|
|
else if command == "insert" {
|
|
if !Insert(widget, at, TextView(args[2])) { SetError(result, "cannot insert single-line UTF-8 text") }
|
|
} else {
|
|
last := UTF.Next(TextView(widget.text), at);
|
|
if args.count == 3 {
|
|
end, good := EntryOffset(widget, TextView(args[2]));
|
|
if !good { SetError(result, "bad entry index"); return }
|
|
last = end;
|
|
}
|
|
Delete(widget, at, last);
|
|
}
|
|
}
|
|
|
|
TextLayout :: (widget: *Widget) {
|
|
columns := (widget.box.w - 2 * View.INSET) / CHAR_WIDTH;
|
|
height := (widget.box.h - 2 * View.INSET) / View.ROW_HEIGHT;
|
|
if widget.box.w == 0 { columns = 38 }
|
|
if widget.box.h == 0 { height = ifx widget.text_height > 0 then (widget.text_height - 2*View.INSET)/View.ROW_HEIGHT else 14 }
|
|
View.Reflow(*widget.view, TextView(widget.text), Maximum(columns, 1), Maximum(height, 1));
|
|
}
|
|
|
|
TextOffset :: (widget: *Widget, name: string) -> (at: s64, ok: bool) {
|
|
source := TextView(widget.text);
|
|
at: s64;
|
|
if name.count > 0 && name[0] == 64 {
|
|
for i: 1..name.count-1 {
|
|
if name[i] != 44 { continue }
|
|
x, valid_x := ParseInteger(name[1:i]);
|
|
y, valid_y := ParseInteger(name[i+1:]);
|
|
if !valid_x || !valid_y || x < -2147483640 || x > 2147483647 || y < -2147483640 || y > 2147483647 { break }
|
|
TextLayout(widget);
|
|
at, _ = View.Point(*widget.view, source, cast(s32)x - View.INSET, cast(s32)y - View.INSET, CHAR_WIDTH);
|
|
return at, true;
|
|
}
|
|
return 0, false;
|
|
}
|
|
end := name.count;
|
|
for i: 1..name.count-1 {
|
|
if name[i] == 43 || name[i] == 45 { end = i; break }
|
|
}
|
|
base := name[:end];
|
|
if base == "end" { at = source.count }
|
|
else {
|
|
dot: s64;
|
|
while dot < base.count && base[dot] != 46 { dot += 1 }
|
|
if dot >= base.count { return 0, false }
|
|
line, valid_line := ParseInteger(base[:dot]);
|
|
column, valid_column := ParseInteger(base[dot+1:]);
|
|
if !valid_line || !valid_column || line < 1 || column < 0 { return 0, false }
|
|
at = View.Offset(source, line, column);
|
|
}
|
|
if end == name.count { return at, true }
|
|
if name.count-end < 3 || name[name.count-1] != 99 { return 0, false }
|
|
count, valid := ParseInteger(name[end+1:name.count-1]);
|
|
if !valid || count < 0 { return 0, false }
|
|
if name[end] == 43 {
|
|
while count > 0 && at < source.count { at = UTF.Next(source, at); count -= 1 }
|
|
} else {
|
|
while count > 0 && at > 0 { at = UTF.Previous(source, at); count -= 1 }
|
|
}
|
|
return at, true;
|
|
}
|
|
|
|
TextSearch :: (widget: *Widget, args: []Text, result: *Result) {
|
|
arg: s64 = 1;
|
|
backwards: bool;
|
|
while arg < args.count {
|
|
option := TextView(args[arg]);
|
|
if option.count == 0 || option[0] != 45 { break }
|
|
arg += 1;
|
|
if option == "--" { break }
|
|
if option == "-backwards" { backwards = true }
|
|
else if option == "-forwards" { backwards = false }
|
|
else if option != "-exact" { SetError(result, "unsupported text search option"); return }
|
|
}
|
|
if args.count-arg < 2 || args.count-arg > 3 {
|
|
SetError(result, "expected search ?-forwards|-backwards? ?-exact? ?--? pattern index ?stopIndex?"); return;
|
|
}
|
|
source := TextView(widget.text);
|
|
pattern := TextView(args[arg]);
|
|
start, valid := TextOffset(widget, TextView(args[arg+1]));
|
|
if !valid { SetError(result, "bad text index"); return }
|
|
stop := ifx backwards then cast(s64)0 else source.count;
|
|
wrap := args.count-arg == 2;
|
|
if !wrap {
|
|
limit, ok := TextOffset(widget, TextView(args[arg+2]));
|
|
if !ok { SetError(result, "bad text stop index"); return }
|
|
if (backwards && limit >= start) || (!backwards && limit <= start) { return }
|
|
stop = ifx backwards then UTF.Next(source, limit) else limit;
|
|
}
|
|
found := View.Match(source, pattern, start, stop, backwards);
|
|
if found < 0 && wrap {
|
|
if backwards { found = View.Match(source, pattern, source.count, start, true) }
|
|
else { found = View.Match(source, pattern, 0, start, false) }
|
|
}
|
|
if found >= 0 { View.Index(source, found, *result.value.bytes) }
|
|
}
|
|
|
|
TextTag :: (widget: *Widget, args: []Text, result: *Result) {
|
|
if args.count < 3 { SetError(result, "expected text tag operation and name"); return }
|
|
action := TextView(args[1]);
|
|
name := TextView(args[2]);
|
|
view := *widget.view;
|
|
if action == "add" && args.count == 5 {
|
|
first, valid_first := TextOffset(widget, TextView(args[3]));
|
|
last, valid_last := TextOffset(widget, TextView(args[4]));
|
|
if !valid_first || !valid_last { SetError(result, "bad text tag index"); return }
|
|
tag := View.EnsureTag(view, name);
|
|
if tag < 0 || !View.AddRange(view, tag, first, last) { SetError(result, "could not add text tag range") }
|
|
return;
|
|
}
|
|
if action == "remove" && args.count == 5 && TextView(args[3]) == "1.0" && TextView(args[4]) == "end" {
|
|
View.ClearRanges(view, View.FindTag(view, name));
|
|
return;
|
|
}
|
|
underline := action == "configure" && args.count == 5 && TextView(args[3]) == "-underline";
|
|
bind := action == "bind" && args.count == 5 && TextView(args[3]) == "<ButtonRelease-1>";
|
|
if underline || bind {
|
|
value := TextView(args[4]);
|
|
if underline && value != "0" && value != "1" { SetError(result, "expected 0 or 1 for tag -underline"); return }
|
|
tag := View.EnsureTag(view, name);
|
|
if tag < 0 { SetError(result, "could not add text tag"); return }
|
|
if underline { view.tags[tag].underline = value == "1" }
|
|
else {
|
|
script: Text = TextCopy(value);
|
|
VecSwap(view.tags[tag].release.bytes, script.bytes);
|
|
}
|
|
return;
|
|
}
|
|
SetError(result, "unsupported text tag operation or arguments");
|
|
}
|
|
|
|
AppendText :: (widget: *Widget, value: string) -> bool {
|
|
if value.count == 0 { return true }
|
|
if !BuilderAppend(widget.text.bytes, value) { return false }
|
|
widget.view.appended = true;
|
|
return true;
|
|
}
|
|
|
|
TextCommand :: (widget: *Widget, args: []Text, result: *Result) {
|
|
command := TextView(args[0]);
|
|
if command == "cget" && args.count == 2 {
|
|
option := TextView(args[1]);
|
|
if option == "-yscrollcommand" { unused BuilderAppend(result.value.bytes, TextView(widget.command)); return }
|
|
if option == "-height" {
|
|
digits: [24]u8;
|
|
height := ifx widget.text_height > 0 then widget.text_height else 150;
|
|
unused BuilderAppend(result.value.bytes, FormatInteger(height, digits[:]));
|
|
return;
|
|
}
|
|
}
|
|
if command == "search" { TextSearch(widget, args, result); return }
|
|
if command == "tag" { TextTag(widget, args, result); return }
|
|
if (command == "index" || command == "see") && args.count == 2 {
|
|
at, valid := TextOffset(widget, TextView(args[1]));
|
|
if !valid { SetError(result, "bad text index"); return }
|
|
if command == "index" { View.Index(TextView(widget.text), at, *result.value.bytes) }
|
|
else { TextLayout(widget); View.See(*widget.view, at) }
|
|
return;
|
|
}
|
|
if command == "yview" {
|
|
TextLayout(widget);
|
|
if args.count == 1 { View.Position(*widget.view, *result.value.bytes); return }
|
|
if args.count == 3 && TextView(args[1]) == "moveto" && View.Moveto(*widget.view, TextView(args[2])) { return }
|
|
if args.count == 4 && TextView(args[1]) == "scroll" {
|
|
amount, ok := ParseInteger(TextView(args[2]));
|
|
scale := TextView(args[3]);
|
|
if ok && (scale == "units" || scale == "unit" || scale == "pages" || scale == "page") {
|
|
if scale == "page" || scale == "pages" { amount *= View.PageSize(*widget.view) }
|
|
View.Move(*widget.view, amount);
|
|
return;
|
|
}
|
|
}
|
|
SetError(result, "expected yview ?moveto fraction|scroll count units|pages?"); return;
|
|
}
|
|
if command == "get" && (args.count == 2 || args.count == 3) {
|
|
source := TextView(widget.text);
|
|
first, valid_first := TextOffset(widget, TextView(args[1]));
|
|
last := UTF.Next(source, first);
|
|
if !valid_first { SetError(result, "bad text index"); return }
|
|
if args.count == 3 {
|
|
end, valid := TextOffset(widget, TextView(args[2]));
|
|
if !valid { SetError(result, "bad text index"); return }
|
|
last = end;
|
|
}
|
|
if first < last { unused BuilderAppend(result.value.bytes, source[first:last]) }
|
|
return;
|
|
}
|
|
if command == "delete" && args.count == 3 && TextView(args[1]) == "1.0" && TextView(args[2]) == "end" {
|
|
unused VecResize(widget.text.bytes, 0);
|
|
View.Reset(*widget.view);
|
|
return;
|
|
}
|
|
if command == "insert" && args.count == 3 && TextView(args[1]) == "end" {
|
|
if !AppendText(widget, TextView(args[2])) { SetError(result, "could not grow text widget") }
|
|
return;
|
|
}
|
|
SetError(result, "unsupported text subcommand or arguments");
|
|
}
|
|
|
|
EntryKey :: enum s32 { Return; Backspace; Delete; Left; Right; Home; End; Up; Down; PageUp; PageDown; }
|
|
|
|
KeyDown :: (key: EntryKey) -> Text {
|
|
if focused < 0 { return TextCopy("") }
|
|
entry := *widgets[focused];
|
|
if entry.kind == SCALE {
|
|
if key == .Up || key == .Left { return ScaleInput(entry, Scale.Step(*entry.scale, -1)) }
|
|
if key == .Down || key == .Right { return ScaleInput(entry, Scale.Step(*entry.scale, 1)) }
|
|
return .{};
|
|
}
|
|
if entry.kind == TEXT {
|
|
if key == .Up { View.Move(*entry.view, -1) }
|
|
if key == .Down { View.Move(*entry.view, 1) }
|
|
if key == .PageUp { View.Move(*entry.view, -View.PageSize(*entry.view)) }
|
|
if key == .PageDown { View.Move(*entry.view, View.PageSize(*entry.view)) }
|
|
if key == .Home { entry.view.top = 0 }
|
|
if key == .End { View.Move(*entry.view, entry.view.rows.count) }
|
|
return TextCopy("");
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if entry.kind == LISTBOX {
|
|
if key == .Up { Listbox.Step(*entry.listbox, -1) }
|
|
if key == .Down { Listbox.Step(*entry.listbox, 1) }
|
|
if key == .Return { Listbox.Choose(*entry.listbox, entry.listbox.active) }
|
|
return .{};
|
|
}
|
|
}
|
|
if key == .Return { return TextCopy(TextView(entry.command)) }
|
|
if key == .Left { entry.cursor = UTF.Previous(TextView(entry.text), entry.cursor) }
|
|
if key == .Right { entry.cursor = UTF.Next(TextView(entry.text), entry.cursor) }
|
|
if key == .Home { entry.cursor = 0 }
|
|
if key == .End { entry.cursor = entry.text.bytes.count }
|
|
if key == .Backspace {
|
|
at := UTF.Previous(TextView(entry.text), entry.cursor);
|
|
Delete(entry, at, entry.cursor);
|
|
}
|
|
if key == .Delete {
|
|
at := UTF.Next(TextView(entry.text), entry.cursor);
|
|
Delete(entry, entry.cursor, at);
|
|
}
|
|
return TextCopy("");
|
|
}
|
|
|
|
TextInput :: (value: string) {
|
|
if focused >= 0 && widgets[focused].kind == ENTRY {
|
|
unused Insert(*widgets[focused], widgets[focused].cursor, value);
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if focused >= 0 && widgets[focused].kind == LISTBOX && value == " " {
|
|
listbox := *widgets[focused].listbox;
|
|
Listbox.Choose(listbox, listbox.active);
|
|
}
|
|
}
|
|
}
|
|
|
|
Minimum :: (a: s32, b: s32) -> s32 { return ifx a < b then a else b }
|
|
Maximum :: (a: s32, b: s32) -> s32 { return ifx a > b then a else b }
|
|
NonNegative :: (x: s32) -> s32 { return ifx x > 0 then x else 0 }
|
|
|
|
NextChild :: (parent: s64, after: s32) -> s64 {
|
|
index: s64 = -1;
|
|
for * widgets {
|
|
master := it.parent;
|
|
#if #defined(TK_BITMAP) {
|
|
master = it.pack.master
|
|
}
|
|
if master == parent && it.order > after && (index < 0 || it.order < widgets[index].order) { index = it_index }
|
|
}
|
|
return index;
|
|
}
|
|
|
|
Horizontal :: (pack: Packing) -> bool { return pack.side == 1 || pack.side == 3 }
|
|
|
|
EntryWidth :: (widget: *Widget) -> s32 {
|
|
#if #defined(TK_LISTBOX) {
|
|
if widget.entry_width > 0 { return widget.entry_width }
|
|
}
|
|
return 168;
|
|
}
|
|
|
|
Measure :: (parent: s64) -> Box {
|
|
used: Box;
|
|
required: Box;
|
|
index := NextChild(parent, 0);
|
|
while index >= 0 {
|
|
widget := *widgets[index];
|
|
requested: Box;
|
|
if widget.kind == FRAME { requested = Measure(index) }
|
|
else if widget.kind == SCALE {
|
|
length := widget.scale.length + 2 * Scale.INSET;
|
|
minor: s32 = Scale.MINOR;
|
|
if widget.scale.show_value { minor += ifx widget.scale.horizontal then Scale.VALUE_HEIGHT else 120 }
|
|
requested = ifx widget.scale.horizontal then Box.{w = length, h = minor} else Box.{w = minor, h = length};
|
|
}
|
|
else if widget.kind == PANEL { requested = .{w = widget.panel.width + 2*widget.panel.border, h = widget.panel.height + 2*widget.panel.border} }
|
|
else if widget.kind == ENTRY { requested = .{w = EntryWidth(widget), h = 30} }
|
|
else if widget.kind == TEXT { requested = .{w = 280, h = ifx widget.text_height > 0 then widget.text_height else 150} }
|
|
else if widget.kind == SCROLLBAR {
|
|
requested = ifx widget.scroll.horizontal then Box.{w = Scroll.LENGTH, h = Scroll.WIDTH}
|
|
else Box.{w = Scroll.WIDTH, h = Scroll.LENGTH};
|
|
}
|
|
else {
|
|
length := UTF.Count(TextView(widget.text), 512);
|
|
requested = .{w = length * CHAR_WIDTH + 30, h = ifx widget.kind == BUTTON then 36 else 28};
|
|
if SelectionButton(widget.kind) && widget.check.indicator >= 0 { requested.w += 10 }
|
|
if widget.kind == MENUBUTTON { requested.w += 12 }
|
|
}
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
if widget.kind == CANVAS {
|
|
border := 2*widget.canvas.border;
|
|
requested = .{w = widget.canvas.width+border, h = widget.canvas.height+border};
|
|
}
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if widget.kind == LISTBOX { requested = .{w = widget.listbox.width, h = widget.listbox.height} }
|
|
}
|
|
#if #defined(TK_BITMAP) {
|
|
if widget.kind == LABEL {
|
|
image := LabelImage(widget);
|
|
if image != null { requested = .{w = image.pixels.width, h = image.pixels.height} }
|
|
else if widget.style.background != Canvas.NONE || widget.style.foreground != Canvas.NONE {
|
|
requested = .{w = UTF.Count(TextView(widget.text), 512)*CHAR_WIDTH + 4, h = Canvas.TEXT_HEIGHT + 5};
|
|
}
|
|
}
|
|
requested.w += 2*(widget.pack.ipadx + widget.style.border);
|
|
requested.h += 2*(widget.pack.ipady + widget.style.border);
|
|
}
|
|
widget.requested = requested;
|
|
w := requested.w + widget.pack.padx * 2;
|
|
h := requested.h + widget.pack.pady * 2;
|
|
if Horizontal(widget.pack) {
|
|
used.w += w;
|
|
required.h = Maximum(required.h, used.h + h);
|
|
} else {
|
|
used.h += h;
|
|
required.w = Maximum(required.w, used.w + w);
|
|
}
|
|
index = NextChild(parent, widget.order);
|
|
}
|
|
return .{w = Maximum(used.w, required.w), h = Maximum(used.h, required.h)};
|
|
}
|
|
|
|
// Reserve room for later siblings before sharing the cavity among expanders.
|
|
Expansion :: (index: s64, cavity: s32, horizontal: bool) -> s32 {
|
|
available := cavity;
|
|
extra := cavity;
|
|
expanders: s32;
|
|
while index >= 0 {
|
|
widget := *widgets[index];
|
|
needed := ifx horizontal then widget.requested.w + widget.pack.padx * 2 else widget.requested.h + widget.pack.pady * 2;
|
|
if Horizontal(widget.pack) == horizontal {
|
|
available -= needed;
|
|
expanders += widget.pack.expand;
|
|
} else if expanders > 0 { extra = Minimum(extra, (available - needed) / expanders) }
|
|
master := widget.parent;
|
|
#if #defined(TK_BITMAP) {
|
|
master = widget.pack.master
|
|
}
|
|
index = NextChild(master, widget.order);
|
|
}
|
|
if expanders == 0 { return 0 }
|
|
return NonNegative(Minimum(extra, available / expanders));
|
|
}
|
|
|
|
Place :: (parent: s64, cavity: Box) {
|
|
index := NextChild(parent, 0);
|
|
while index >= 0 {
|
|
widget := *widgets[index];
|
|
pack := widget.pack;
|
|
available := cavity;
|
|
if Horizontal(pack) {
|
|
parcel := widget.requested.w + pack.padx * 2;
|
|
if pack.expand > 0 { parcel += Expansion(index, cavity.w, true) }
|
|
available.w = Minimum(cavity.w, parcel);
|
|
if pack.side == 1 { cavity.x += available.w }
|
|
else { available.x += cavity.w - available.w }
|
|
cavity.w -= available.w;
|
|
} else {
|
|
parcel := widget.requested.h + pack.pady * 2;
|
|
if pack.expand > 0 { parcel += Expansion(index, cavity.h, false) }
|
|
available.h = Minimum(cavity.h, parcel);
|
|
if pack.side == 0 { cavity.y += available.h }
|
|
else { available.y += cavity.h - available.h }
|
|
cavity.h -= available.h;
|
|
}
|
|
inner_w := NonNegative(available.w - pack.padx * 2);
|
|
inner_h := NonNegative(available.h - pack.pady * 2);
|
|
w := ifx (pack.fill & 1) != 0 then inner_w else Minimum(widget.requested.w, inner_w);
|
|
h := ifx (pack.fill & 2) != 0 then inner_h else Minimum(widget.requested.h, inner_h);
|
|
x := available.x + pack.padx + (inner_w-w)*(1+pack.anchor_x)/2;
|
|
y := available.y + pack.pady + (inner_h-h)*(1+pack.anchor_y)/2;
|
|
// Keep cramped parcels centered even when padding consumes the cavity.
|
|
if inner_w == 0 { x = available.x + available.w/2 }
|
|
if inner_h == 0 { y = available.y + available.h/2 }
|
|
widget.box = .{x = x, y = y, w = w, h = h};
|
|
if widget.kind == FRAME {
|
|
area := widget.box;
|
|
#if #defined(TK_BITMAP) {
|
|
border := widget.style.border;
|
|
area.x += border;
|
|
area.y += border;
|
|
area.w = NonNegative(area.w-2*border);
|
|
area.h = NonNegative(area.h-2*border);
|
|
}
|
|
Place(index, area);
|
|
}
|
|
else if widget.kind == TEXT { TextLayout(widget) }
|
|
#if #defined(TK_LISTBOX) {
|
|
if widget.kind == LISTBOX { Listbox.Layout(*widget.listbox, h) }
|
|
}
|
|
index = NextChild(parent, widget.order);
|
|
}
|
|
}
|
|
|
|
Layout :: (width: s32, height: s32) {
|
|
#if #defined(TK_MENU) {
|
|
viewport = .{w = width, h = height};
|
|
}
|
|
for * widgets { it.box = .{} }
|
|
unused Measure(-1);
|
|
Place(-1, .{x = ROOT_INSET, y = ROOT_INSET,
|
|
w = NonNegative(width - 2*ROOT_INSET), h = NonNegative(height - 2*ROOT_INSET)});
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
for * widgets {
|
|
if it.name.bytes.count == 0 || it.kind != CANVAS { continue }
|
|
border := 2*it.canvas.border;
|
|
Canvas.Resize(*it.canvas, NonNegative(it.box.w-border), NonNegative(it.box.h-border));
|
|
}
|
|
}
|
|
}
|
|
|
|
EntrySlots :: (box: Box) -> s32 { return NonNegative(box.w - 2 * ENTRY_INSET - 1) / CHAR_WIDTH }
|
|
|
|
EntryStart :: (widget: *Widget) -> s64 {
|
|
text := TextView(widget.text);
|
|
cursor_position := UTF.Count(text[:widget.cursor]);
|
|
first, _ := UTF.Offset(text, NonNegative(cursor_position - EntrySlots(widget.box)));
|
|
return first;
|
|
}
|
|
|
|
Over :: (x: s32, y: s32, kind: s64) -> s64 {
|
|
for * widgets {
|
|
box := it.box;
|
|
if it.kind == kind && !it.disabled && x >= box.x && x < box.x + box.w && y >= box.y && y < box.y + box.h {
|
|
return it_index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
BoundRange :: (widget: *Widget, x: s32, y: s32) -> s64 {
|
|
at, hit := View.Point(*widget.view, TextView(widget.text), x, y, CHAR_WIDTH);
|
|
if hit {
|
|
index := widget.view.ranges.count;
|
|
while index > 0 {
|
|
index -= 1;
|
|
range := *widget.view.ranges[index];
|
|
if at >= range.first && at < range.last && widget.view.tags[range.tag].release.bytes.count > 0 { return index }
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
PointerDown :: (x: s32, y: s32) {
|
|
pressed = Over(x, y, BUTTON);
|
|
pressed_range = -1;
|
|
index := Over(x, y, ENTRY);
|
|
if index < 0 {
|
|
index = Over(x, y, TEXT);
|
|
if index >= 0 {
|
|
focused = index;
|
|
widget := *widgets[index];
|
|
pressed_range = BoundRange(widget, x-widget.box.x-View.INSET, y-widget.box.y-View.INSET);
|
|
if pressed_range >= 0 { pressed = index }
|
|
}
|
|
return;
|
|
}
|
|
focused = index;
|
|
widget := *widgets[index];
|
|
start := EntryStart(widget);
|
|
position := NonNegative(x - widget.box.x - ENTRY_INSET + CHAR_WIDTH/2) / CHAR_WIDTH;
|
|
offset, _ := UTF.Offset(TextView(widget.text)[start:], position);
|
|
widget.cursor = start + offset;
|
|
}
|
|
|
|
PointerUp :: (x: s32, y: s32) -> Text {
|
|
#if #defined(TK_MENU) {
|
|
if popup >= 0 {
|
|
row, inside := MenuHit(x, y);
|
|
script: Text;
|
|
if inside && row >= 0 {
|
|
menu := *widgets[popup].menu;
|
|
Menu.Invoke(menu, row, *script);
|
|
if menu.rows[row].kind == .Command { UnpostMenu() }
|
|
} else {
|
|
UnpostMenu();
|
|
}
|
|
return script;
|
|
}
|
|
}
|
|
index := pressed;
|
|
range := pressed_range;
|
|
pressed = -1;
|
|
pressed_range = -1;
|
|
if index < 0 { return .{} }
|
|
if widgets[index].kind == SCALE {
|
|
widget := *widgets[index];
|
|
return ScaleDrag(widget, x, y);
|
|
}
|
|
if widgets[index].kind == SCROLLBAR { return .{} }
|
|
if range < 0 {
|
|
if Over(x, y, BUTTON) == index { return TextCopy(TextView(widgets[index].command)) }
|
|
return .{};
|
|
}
|
|
if Over(x, y, TEXT) != index { return .{} }
|
|
widget := *widgets[index];
|
|
local_x := x-widget.box.x;
|
|
local_y := y-widget.box.y;
|
|
if BoundRange(widget, local_x-View.INSET, local_y-View.INSET) != range { return .{} }
|
|
tag := widget.view.ranges[range].tag;
|
|
return PointerScript(TextView(widget.view.tags[tag].release), local_x, local_y);
|
|
}
|
|
|
|
PointerScript :: (script: string, local_x: s32, local_y: s32) -> Text {
|
|
output: Text;
|
|
digits: [24]u8;
|
|
i: s64;
|
|
while i < script.count {
|
|
if script[i] == 37 && i+1 < script.count {
|
|
code := script[i+1];
|
|
if code == 120 || code == 121 {
|
|
coordinate := ifx code == 120 then local_x else local_y;
|
|
unused BuilderAppend(output.bytes, FormatInteger(coordinate, digits[:]));
|
|
i += 2;
|
|
continue;
|
|
}
|
|
if code == 37 { i += 1 }
|
|
}
|
|
unused VecPush(output.bytes, script[i]);
|
|
i += 1;
|
|
}
|
|
return output;
|
|
}
|
|
|
|
Closed :: () -> bool { return closed }
|
|
|
|
MenuPosted :: () -> bool {
|
|
#if #defined(TK_MENU) {
|
|
return popup >= 0;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#if #defined(TK_MENU) {
|
|
MenuBounds :: () -> Box {
|
|
menu := *widgets[popup].menu;
|
|
border := menu.border;
|
|
width: s32 = 28;
|
|
for * menu.rows {
|
|
width = Maximum(width, UTF.Count(TextView(it.label), 512)*CHAR_WIDTH+20+Menu.IndicatorWidth(it));
|
|
}
|
|
width = Minimum(viewport.w, width+2*border);
|
|
height := Minimum(viewport.h, Menu.RowTop(menu, menu.rows.count)+2*border);
|
|
y := popup_y;
|
|
if popup_owner >= 0 && y > viewport.h-height && widgets[popup_owner].box.y >= height {
|
|
y = widgets[popup_owner].box.y-height;
|
|
}
|
|
return .{x = Maximum(0, Minimum(popup_x, viewport.w-width)),
|
|
y = Maximum(0, Minimum(y, viewport.h-height)), w = width, h = height};
|
|
}
|
|
|
|
MenuHit :: (x: s32, y: s32) -> (row: s64, inside: bool) {
|
|
box := MenuBounds();
|
|
if x < box.x || x >= box.x+box.w || y < box.y || y >= box.y+box.h { return -1, false }
|
|
border := widgets[popup].menu.border;
|
|
if x < box.x+border || x >= box.x+box.w-border || y >= box.y+box.h-border { return -1, true }
|
|
return Menu.AtY(*widgets[popup].menu, y-box.y-border), true;
|
|
}
|
|
|
|
PostMenu :: (index: s64, owner: s64, x: s32, y: s32) {
|
|
if popup >= 0 && popup != index { UnpostMenu() }
|
|
popup = index;
|
|
popup_owner = owner;
|
|
popup_x = x;
|
|
popup_y = y;
|
|
pressed = -1;
|
|
pressed_range = -1;
|
|
}
|
|
|
|
UnpostMenu :: () {
|
|
widgets[popup].menu.active = -1;
|
|
popup = -1;
|
|
popup_owner = -1;
|
|
}
|
|
|
|
MenuCommand :: (index: s64, args: []Text, result: *Result, script: *Text) {
|
|
op := TextView(args[0]);
|
|
if op == "unpost" && args.count == 1 {
|
|
if popup == index { UnpostMenu() }
|
|
return;
|
|
}
|
|
if op == "post" && args.count == 3 {
|
|
x, x_ok := ParseInteger(TextView(args[1]));
|
|
y, y_ok := ParseInteger(TextView(args[2]));
|
|
if !x_ok || !y_ok || x < -2147483648 || x > 2147483647 || y < -2147483648 || y > 2147483647 {
|
|
SetError(result, "invalid menu coordinates");
|
|
return;
|
|
}
|
|
PostMenu(index, -1, cast(s32)x, cast(s32)y);
|
|
return;
|
|
}
|
|
Menu.Command(*widgets[index].menu, args, result, script);
|
|
}
|
|
}
|
|
|
|
ConfigureScale :: (widget: *Widget, args: []Text, result: *Result) -> bool {
|
|
scale := widget.scale;
|
|
command: string;
|
|
replace_command := false;
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
value := TextView(args[2*i+1]);
|
|
if option == "-command" {
|
|
command = value;
|
|
replace_command = true;
|
|
} else if option == "-orient" && (value == "horizontal" || value == "vertical") {
|
|
scale.horizontal = value == "horizontal";
|
|
} else if option == "-showvalue" && (value == "0" || value == "1") {
|
|
scale.show_value = value == "1";
|
|
} else if option == "-length" {
|
|
length, ok := ParseInteger(value);
|
|
if !ok || length < 1 || length > 4090 { SetError(result, "invalid scale length"); return false }
|
|
scale.length = cast(s32)length;
|
|
} else {
|
|
number, ok := Scale.Parse(value);
|
|
if ok && option == "-from" { scale.first = number }
|
|
else if ok && option == "-to" { scale.last = number }
|
|
else if ok && number >= 0 && option == "-resolution" { scale.resolution = number }
|
|
else if ok && number >= 0 && option == "-bigincrement" { scale.big_increment = number }
|
|
else { SetError(result, "unsupported scale option or value"); return false }
|
|
}
|
|
}
|
|
scale.value = Scale.Bound(*scale, scale.value);
|
|
if replace_command {
|
|
copy: Text = TextCopy(command);
|
|
if copy.bytes.count != command.count { SetError(result, "could not allocate scale command"); return false }
|
|
VecSwap(widget.command.bytes, copy.bytes);
|
|
}
|
|
widget.scale = scale;
|
|
return true;
|
|
}
|
|
|
|
ScaleCommand :: (widget: *Widget, args: []Text, result: *Result) {
|
|
subcommand := TextView(args[0]);
|
|
scale := *widget.scale;
|
|
if subcommand == "get" && args.count == 1 { Scale.Append(*result.value, scale.value); return }
|
|
if subcommand == "set" && args.count == 2 {
|
|
value, ok := Scale.Parse(TextView(args[1]));
|
|
if !ok { SetError(result, "invalid scale value"); return }
|
|
scale.value = Scale.Bound(scale, value);
|
|
return;
|
|
}
|
|
if subcommand == "cget" && args.count == 2 {
|
|
option := TextView(args[1]);
|
|
if option == "-from" { Scale.Append(*result.value, scale.first); return }
|
|
if option == "-to" { Scale.Append(*result.value, scale.last); return }
|
|
if option == "-resolution" { Scale.Append(*result.value, scale.resolution); return }
|
|
if option == "-bigincrement" { Scale.Append(*result.value, scale.big_increment); return }
|
|
if option == "-length" { Scale.Append(*result.value, cast(s64)scale.length * Scale.UNIT); return }
|
|
if option == "-command" { unused BuilderAppend(result.value.bytes, TextView(widget.command)); return }
|
|
if option == "-orient" { unused BuilderAppend(result.value.bytes, ifx scale.horizontal then "horizontal" else "vertical"); return }
|
|
if option == "-showvalue" { unused BuilderAppend(result.value.bytes, ifx scale.show_value then "1" else "0"); return }
|
|
}
|
|
SetError(result, "unsupported scale subcommand or arguments");
|
|
}
|
|
|
|
ScaleLength :: (widget: *Widget) -> s32 { return ifx widget.scale.horizontal then widget.box.w else widget.box.h }
|
|
|
|
ScaleCoordinate :: (widget: *Widget, x: s32, y: s32) -> s32 {
|
|
return ifx widget.scale.horizontal then x else y;
|
|
}
|
|
|
|
ScaleInput :: (widget: *Widget, value: s64) -> Text {
|
|
if widget.scale.value == value { return .{} }
|
|
widget.scale.value = value;
|
|
script: Text = TextCopy(TextView(widget.command));
|
|
if script.bytes.count > 0 {
|
|
unused BuilderAppend(script.bytes, " ");
|
|
Scale.Append(*script, value);
|
|
}
|
|
return script;
|
|
}
|
|
|
|
ScaleTrack :: (widget: *Widget) -> Box {
|
|
box := widget.box;
|
|
if widget.scale.horizontal {
|
|
return .{x = box.x, y = box.y + NonNegative(box.h - Scale.MINOR), w = box.w, h = Minimum(box.h, Scale.MINOR)};
|
|
}
|
|
return .{x = box.x + NonNegative(box.w - Scale.MINOR), y = box.y, w = Minimum(box.w, Scale.MINOR), h = box.h};
|
|
}
|
|
|
|
PointerPress :: (x: s32, y: s32) -> Text {
|
|
#if #defined(TK_MENU) {
|
|
opener := Over(x, y, MENUBUTTON);
|
|
if popup >= 0 {
|
|
row, inside := MenuHit(x, y);
|
|
if inside { Menu.Activate(*widgets[popup].menu, row); return .{} }
|
|
UnpostMenu();
|
|
if opener < 0 { return .{} }
|
|
}
|
|
if opener >= 0 {
|
|
name := TextView(widgets[opener].command);
|
|
index := Find(name);
|
|
if index >= 0 && widgets[index].kind == MENU {
|
|
box := widgets[opener].box;
|
|
PostMenu(index, opener, box.x, box.y+box.h);
|
|
}
|
|
return .{};
|
|
}
|
|
}
|
|
PointerDown(x, y);
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
canvas := Over(x, y, CANVAS);
|
|
if canvas >= 0 {
|
|
widget := *widgets[canvas];
|
|
border := widget.canvas.border;
|
|
return PointerScript(TextView(widget.command), x-widget.box.x-border, y-widget.box.y-border);
|
|
}
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
listbox := Over(x, y, LISTBOX);
|
|
if listbox >= 0 {
|
|
widget := *widgets[listbox];
|
|
focused = listbox;
|
|
pressed = listbox;
|
|
Listbox.Press(*widget.listbox, y-widget.box.y-Listbox.INSET);
|
|
return .{};
|
|
}
|
|
}
|
|
check := Over(x, y, CHECKBUTTON);
|
|
if check < 0 { check = Over(x, y, RADIOBUTTON) }
|
|
if check >= 0 {
|
|
widget := *widgets[check];
|
|
if Check.Invoke(*widget.check) { return TextCopy(TextView(widget.command)) }
|
|
return .{};
|
|
}
|
|
scrollbar := Over(x, y, SCROLLBAR);
|
|
if scrollbar >= 0 { return ScrollPress(scrollbar, x, y) }
|
|
index := Over(x, y, SCALE);
|
|
if index < 0 { return .{} }
|
|
focused = index;
|
|
widget := *widgets[index];
|
|
track := ScaleTrack(widget);
|
|
if x < track.x || y < track.y || x >= track.x + track.w || y >= track.y + track.h { return .{} }
|
|
position := ScaleCoordinate(widget, x-widget.box.x, y-widget.box.y);
|
|
thumb := Scale.Position(*widget.scale, ScaleLength(widget));
|
|
if position < thumb || position >= thumb + Scale.SLIDER {
|
|
return ScaleInput(widget, Scale.Step(*widget.scale, ifx position < thumb then -1 else 1));
|
|
}
|
|
pressed = index;
|
|
drag_start = ScaleCoordinate(widget, x, y);
|
|
drag_value = widget.scale.value;
|
|
return .{};
|
|
}
|
|
|
|
Dragging :: () -> bool {
|
|
if MenuPosted() { return true }
|
|
if pressed < 0 { return false }
|
|
kind := widgets[pressed].kind;
|
|
return kind == SCALE || kind == SCROLLBAR || kind == LISTBOX;
|
|
}
|
|
|
|
PointerMove :: (x: s32, y: s32) -> Text {
|
|
#if #defined(TK_MENU) {
|
|
if popup >= 0 {
|
|
row, _ := MenuHit(x, y);
|
|
Menu.Activate(*widgets[popup].menu, row);
|
|
return .{};
|
|
}
|
|
}
|
|
if !Dragging() { return .{} }
|
|
widget := *widgets[pressed];
|
|
#if #defined(TK_LISTBOX) {
|
|
if widget.kind == LISTBOX {
|
|
Listbox.Drag(*widget.listbox, y-widget.box.y-Listbox.INSET);
|
|
return .{};
|
|
}
|
|
}
|
|
if widget.kind == SCROLLBAR { return ScrollDrag(widget, x, y) }
|
|
return ScaleDrag(widget, x, y);
|
|
}
|
|
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
CanvasPointerKind :: enum { Move; Press; Release; }
|
|
CanvasCallback :: #type (tcl: *Tcl.Interpreter, script: Text) -> bool;
|
|
|
|
CanvasDispatch :: (tcl: *Tcl.Interpreter, index: s64, x: s32, y: s32, mask: s32,
|
|
callback: CanvasCallback) -> bool {
|
|
widget := *widgets[index];
|
|
border := widget.canvas.border;
|
|
event := Canvas.PointerEvent.{mask, x-widget.box.x-border, y-widget.box.y-border, x, y};
|
|
actions: Text = Canvas.PointerActions(*widget.canvas, event, TextView(widget.name));
|
|
if actions.bytes.count == 0 { return false }
|
|
return callback(tcl, actions);
|
|
}
|
|
|
|
CanvasTarget :: (index: s64, x: s32, y: s32) -> s64 {
|
|
if index < 0 { return 0 }
|
|
widget := *widgets[index];
|
|
border := widget.canvas.border;
|
|
local_x := x-widget.box.x-border;
|
|
local_y := y-widget.box.y-border;
|
|
view := *widget.canvas.viewport;
|
|
if local_x < 0 || local_y < 0 || local_x >= view.width || local_y >= view.height { return 0 }
|
|
return Canvas.TopHit(*widget.canvas, local_x+view.x, local_y+view.y);
|
|
}
|
|
|
|
CanvasPointer :: (tcl: *Tcl.Interpreter, x: s32, y: s32, kind: CanvasPointerKind,
|
|
button: s32, time: s64, callback: CanvasCallback) -> bool {
|
|
// The surface queues a position sample before button transitions.
|
|
if kind == .Move && canvas_pointer_known && x == canvas_x && y == canvas_y { return false }
|
|
canvas_x = x;
|
|
canvas_y = y;
|
|
canvas_pointer_known = true;
|
|
bit := Canvas.ButtonBit(button);
|
|
mask: s32;
|
|
if kind == .Move { mask = Canvas.MOTION | canvas_buttons }
|
|
else if kind == .Release { canvas_buttons &= ~bit; mask = bit << 3 }
|
|
else {
|
|
canvas_buttons |= bit;
|
|
mask = bit;
|
|
if bit != 0 {
|
|
if button == last_canvas_button && time >= 0 && last_canvas_press >= 0 &&
|
|
time >= last_canvas_press && time-last_canvas_press < 300 { mask |= Canvas.DOUBLE }
|
|
last_canvas_press = time;
|
|
last_canvas_button = button;
|
|
}
|
|
}
|
|
if mask == 0 { return false }
|
|
|
|
target: s64 = ifx MenuPosted() then -1 else Over(x, y, CANVAS);
|
|
item := CanvasTarget(target, x, y);
|
|
redraw := false;
|
|
if canvas_hover >= 0 && (canvas_hover != target || widgets[canvas_hover].canvas.current != item) {
|
|
previous := canvas_hover;
|
|
redraw = CanvasDispatch(tcl, previous, x, y, Canvas.LEAVE, callback);
|
|
widgets[previous].canvas.current = 0;
|
|
canvas_hover = -1;
|
|
if Closed() { return redraw }
|
|
target = ifx MenuPosted() then -1 else Over(x, y, CANVAS);
|
|
item = CanvasTarget(target, x, y);
|
|
}
|
|
if target < 0 || item == 0 { return redraw }
|
|
if canvas_hover != target {
|
|
canvas_hover = target;
|
|
widgets[target].canvas.current = item;
|
|
if CanvasDispatch(tcl, target, x, y, Canvas.ENTER, callback) { redraw = true }
|
|
}
|
|
if !Closed() && canvas_hover == target && CanvasDispatch(tcl, target, x, y, mask, callback) { redraw = true }
|
|
return redraw;
|
|
}
|
|
}
|
|
|
|
ScaleDrag :: (widget: *Widget, x: s32, y: s32) -> Text {
|
|
return ScaleInput(widget, Scale.Drag(*widget.scale, ScaleLength(widget), ScaleCoordinate(widget, x, y) - drag_start, drag_value));
|
|
}
|
|
|
|
ConfigurePanel :: (widget: *Widget, args: []Text, result: *Result) -> bool {
|
|
border := widget.panel.border;
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
value := TextView(args[2*i+1]);
|
|
if option == "-relief" && value == "flat" { continue }
|
|
if option == "-bd" || option == "-borderwidth" {
|
|
pixels, ok := ParseInteger(value);
|
|
if ok && pixels >= 0 && pixels <= 512 { border = cast(s32)pixels; continue }
|
|
}
|
|
SetError(result, "unsupported panel option or value");
|
|
return false;
|
|
}
|
|
widget.panel.border = border;
|
|
return true;
|
|
}
|
|
|
|
PanelCommand :: (widget: *Widget, args: []Text, result: *Result) {
|
|
subcommand := TextView(args[0]);
|
|
if subcommand == "dirty" && (args.count == 1 || args.count == 5) {
|
|
for i: 1..args.count-1 {
|
|
_, ok := ParseInteger(TextView(args[i]));
|
|
if !ok { SetError(result, "invalid panel dirty rectangle"); return }
|
|
}
|
|
// The surface renderer presents the entire widget tree after callbacks.
|
|
return;
|
|
}
|
|
if subcommand == "cget" && args.count == 2 {
|
|
option := TextView(args[1]);
|
|
if option == "-relief" { unused BuilderAppend(result.value.bytes, "flat"); return }
|
|
if option == "-bd" || option == "-borderwidth" {
|
|
digits: [24]u8;
|
|
unused BuilderAppend(result.value.bytes, FormatInteger(widget.panel.border, digits[:]));
|
|
return;
|
|
}
|
|
}
|
|
SetError(result, "unsupported panel subcommand or arguments");
|
|
}
|
|
|
|
// Allocate a writable RGBA8 image for the panel and return its widget index.
|
|
// Callers take a fresh pixel view while the widget is alive.
|
|
PanelImage :: (name: string, width: s32, height: s32) -> s64 {
|
|
index := Find(name);
|
|
if closed || index < 0 || widgets[index].kind != PANEL || width < 1 || height < 1 || width > 4096 || height > 4096 { return -1 }
|
|
panel := *widgets[index].panel;
|
|
count := cast(s64)width * cast(s64)height * 4;
|
|
if !VecResizeBounded(panel.pixels, count, MAX_PANEL_BYTES) { return -1 }
|
|
panel.width = width;
|
|
panel.height = height;
|
|
return index;
|
|
}
|
|
|
|
VariableCommand :: (_: s64, args: []Text, result: *Result) {
|
|
if args.count < 1 || args.count > 2 { SetError(result, "expected variable name ?value?"); return }
|
|
name := TextView(args[0]);
|
|
if args.count == 2 {
|
|
if !Check.Put(name, TextView(args[1])) { SetError(result, "could not store Tk variable") }
|
|
return;
|
|
}
|
|
Check.Read(name, *result.value);
|
|
}
|
|
|
|
CheckCommand :: (widget: *Widget, args: []Text, result: *Result, script: *Text) {
|
|
operation := TextView(args[0]);
|
|
check := *widget.check;
|
|
if operation == "cget" && args.count == 2 {
|
|
option := TextView(args[1]);
|
|
if Check.Option(check, option, *result.value) { return }
|
|
}
|
|
if args.count == 1 {
|
|
if operation == "select" || operation == "deselect" {
|
|
if !Check.Select(check, operation == "select") { SetError(result, "could not store Tk variable") }
|
|
return;
|
|
}
|
|
if operation == "toggle" && !check.radio || operation == "invoke" {
|
|
if widget.disabled { return }
|
|
if !Check.Invoke(check) { SetError(result, "could not store Tk variable"); return }
|
|
if operation == "invoke" { unused BuilderAppend(script.bytes, TextView(widget.command)) }
|
|
return;
|
|
}
|
|
}
|
|
SetError(result, "unsupported selection button subcommand or arguments");
|
|
}
|
|
|
|
Distance :: (value: string) -> (pixels: s32, ok: bool) {
|
|
if value.count == 0 { return 0, false }
|
|
factor: s64 = 1;
|
|
divisor: s64 = 1;
|
|
length := value.count;
|
|
unit := value[length-1];
|
|
if unit == 99 { factor = 39; length -= 1 }
|
|
else if unit == 109 { factor = 3; length -= 1 }
|
|
else if unit == 105 { factor = 100; length -= 1 }
|
|
else if unit == 112 { factor = 100; divisor = 72; length -= 1 }
|
|
else if unit == 119 { factor = CHAR_WIDTH; length -= 1 }
|
|
else if unit == 104 { factor = View.ROW_HEIGHT; length -= 1 }
|
|
amount, valid := Scale.Parse(value[:length], Scroll.UNIT);
|
|
pixels := cast(s32)((amount*factor/divisor + Scroll.UNIT/2)/Scroll.UNIT);
|
|
return pixels, valid && amount >= 0 && pixels <= 4096;
|
|
}
|
|
|
|
ConfigureScroll :: (widget: *Widget, args: []Text, result: *Result) -> bool {
|
|
bar := widget.kind == SCROLLBAR;
|
|
callback: string;
|
|
replace_callback := false;
|
|
height := widget.text_height;
|
|
horizontal := widget.scroll.horizontal;
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
value := TextView(args[2*i+1]);
|
|
if (bar && option == "-command") || (!bar && option == "-yscrollcommand") {
|
|
callback = value;
|
|
replace_callback = true;
|
|
}
|
|
else if bar && option == "-orient" && (value == "horizontal" || value == "vertical") {
|
|
horizontal = value == "horizontal";
|
|
} else if !bar && option == "-height" {
|
|
pixels, ok := Distance(value);
|
|
if !ok { SetError(result, "invalid text height"); return false }
|
|
height = pixels;
|
|
} else { SetError(result, "unsupported widget option or value"); return false }
|
|
}
|
|
if replace_callback && callback != TextView(widget.command) {
|
|
copy: Text = TextCopy(callback);
|
|
if copy.bytes.count != callback.count { SetError(result, "could not allocate scroll callback"); return false }
|
|
VecSwap(widget.command.bytes, copy.bytes);
|
|
widget.sent_scroll = false;
|
|
}
|
|
widget.text_height = height;
|
|
widget.scroll.horizontal = horizontal;
|
|
return true;
|
|
}
|
|
|
|
#if #defined(TK_LISTBOX) {
|
|
ConfigureListbox :: (widget: *Widget, args: []Text, result: *Result) -> bool {
|
|
width := widget.listbox.width;
|
|
height := widget.listbox.height;
|
|
browse := widget.listbox.browse;
|
|
callback: string;
|
|
replace_callback := false;
|
|
for i: 0..args.count/2-1 {
|
|
option := TextView(args[2*i]);
|
|
value := TextView(args[2*i+1]);
|
|
if option == "-width" || option == "-height" {
|
|
pixels, valid := Distance(value);
|
|
if !valid { SetError(result, "invalid listbox dimension"); return false }
|
|
if option == "-width" { width = pixels }
|
|
else { height = pixels }
|
|
} else if option == "-selectmode" && (value == "single" || value == "browse") {
|
|
browse = value == "browse";
|
|
} else if option == "-yscrollcommand" {
|
|
callback = value;
|
|
replace_callback = true;
|
|
} else {
|
|
SetError(result, "unsupported listbox option or value");
|
|
return false;
|
|
}
|
|
}
|
|
if replace_callback && callback != TextView(widget.command) {
|
|
copy: Text = TextCopy(callback);
|
|
if copy.bytes.count != callback.count { SetError(result, "could not allocate scroll callback"); return false }
|
|
VecSwap(widget.command.bytes, copy.bytes);
|
|
widget.sent_scroll = false;
|
|
}
|
|
widget.listbox.width = width;
|
|
widget.listbox.height = height;
|
|
widget.listbox.browse = browse;
|
|
if widget.order == 0 { Listbox.Layout(*widget.listbox, height) }
|
|
return true;
|
|
}
|
|
|
|
ListboxOption :: (widget: *Widget, option: string, result: *Result) {
|
|
if option == "-width" { Listbox.Integer(result, widget.listbox.width); return }
|
|
if option == "-height" { Listbox.Integer(result, widget.listbox.height); return }
|
|
if option == "-selectmode" { unused BuilderAppend(result.value.bytes, ifx widget.listbox.browse then "browse" else "single"); return }
|
|
if option == "-yscrollcommand" { unused BuilderAppend(result.value.bytes, TextView(widget.command)); return }
|
|
SetError(result, "unsupported listbox option");
|
|
}
|
|
}
|
|
|
|
ScrollCommand :: (widget: *Widget, args: []Text, result: *Result) {
|
|
operation := TextView(args[0]);
|
|
if operation == "set" && args.count == 3 {
|
|
first, first_ok := Scale.Parse(TextView(args[1]), Scroll.UNIT);
|
|
last, last_ok := Scale.Parse(TextView(args[2]), Scroll.UNIT);
|
|
if !first_ok || !last_ok { SetError(result, "invalid scrollbar fraction"); return }
|
|
widget.scroll.first = Scroll.Clamp(first);
|
|
widget.scroll.last = Scroll.Clamp(last);
|
|
return;
|
|
}
|
|
if operation == "get" && args.count == 1 {
|
|
Scale.Append(*result.value, widget.scroll.first, Scroll.UNIT);
|
|
unused BuilderAppend(result.value.bytes, " ");
|
|
Scale.Append(*result.value, widget.scroll.last, Scroll.UNIT);
|
|
return;
|
|
}
|
|
if operation == "cget" && args.count == 2 {
|
|
option := TextView(args[1]);
|
|
if option == "-orient" {
|
|
unused BuilderAppend(result.value.bytes, ifx widget.scroll.horizontal then "horizontal" else "vertical");
|
|
return;
|
|
}
|
|
if option == "-command" { unused BuilderAppend(result.value.bytes, TextView(widget.command)); return }
|
|
if option == "actwidth" || option == "-actwidth" {
|
|
number: [24]u8;
|
|
unused BuilderAppend(result.value.bytes, FormatInteger(widget.box.w, number[:]));
|
|
return;
|
|
}
|
|
}
|
|
if operation == "identify" && args.count == 3 {
|
|
x, x_ok := ParseInteger(TextView(args[1]));
|
|
y, y_ok := ParseInteger(TextView(args[2]));
|
|
if x_ok && y_ok {
|
|
box := widget.box;
|
|
if x >= 0 && y >= 0 && x < box.w && y < box.h {
|
|
length := ScrollLength(widget);
|
|
offset := ifx widget.scroll.horizontal then cast(s32)x else cast(s32)y;
|
|
unused BuilderAppend(result.value.bytes, Scroll.Hit(Scroll.Track(*widget.scroll, length), offset, length));
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
SetError(result, "unsupported scrollbar subcommand or arguments");
|
|
}
|
|
|
|
ScrollLength :: (widget: *Widget) -> s32 {
|
|
return ifx widget.scroll.horizontal then widget.box.w else widget.box.h;
|
|
}
|
|
|
|
ScrollOffset :: (widget: *Widget, x: s32, y: s32) -> s32 {
|
|
return ifx widget.scroll.horizontal then x-widget.box.x else y-widget.box.y;
|
|
}
|
|
|
|
ScrollPress :: (index: s64, x: s32, y: s32) -> Text {
|
|
widget := *widgets[index];
|
|
length := ScrollLength(widget);
|
|
coordinate := ScrollOffset(widget, x, y);
|
|
part := Scroll.Hit(Scroll.Track(*widget.scroll, length), coordinate, length);
|
|
if part == "slider" {
|
|
pressed = index;
|
|
drag_start = coordinate;
|
|
drag_value = widget.scroll.first;
|
|
return .{};
|
|
}
|
|
command := TextView(widget.command);
|
|
if part == "" || command == "" { return .{} }
|
|
script: Text = TextCopy(command);
|
|
action := ifx part == "arrow1" || part == "trough1" then " scroll -1" else " scroll 1";
|
|
unit := ifx part == "arrow1" || part == "arrow2" then " unit" else " page";
|
|
unused BuilderAppend(script.bytes, action);
|
|
unused BuilderAppend(script.bytes, unit);
|
|
return script;
|
|
}
|
|
|
|
ScrollDrag :: (widget: *Widget, x: s32, y: s32) -> Text {
|
|
command := TextView(widget.command);
|
|
track := Scroll.Track(*widget.scroll, ScrollLength(widget));
|
|
if command == "" || track.travel == 0 { return .{} }
|
|
delta := cast(s64)(ScrollOffset(widget, x, y) - drag_start) * Scroll.UNIT / track.travel;
|
|
script: Text = TextCopy(command);
|
|
unused BuilderAppend(script.bytes, " moveto ");
|
|
Scale.Append(*script, Scroll.Clamp(drag_value + delta), Scroll.UNIT);
|
|
return script;
|
|
}
|
|
|
|
TextScroll :: (widget: *Widget, script: *Text) {
|
|
if widget.command.bytes.count == 0 { return }
|
|
TextLayout(widget);
|
|
rows := widget.view.rows.count;
|
|
first: s64;
|
|
last: s64 = Scroll.UNIT;
|
|
if rows > 0 {
|
|
first = cast(s64)widget.view.top * Scroll.UNIT / rows;
|
|
last = Scroll.Clamp(cast(s64)(widget.view.top + widget.view.visible) * Scroll.UNIT / rows);
|
|
}
|
|
AppendScroll(widget, script, .{first = first, last = last});
|
|
}
|
|
|
|
AppendScroll :: (widget: *Widget, script: *Text, fractions: Scroll.State) {
|
|
command := TextView(widget.command);
|
|
if command == "" { return }
|
|
if widget.sent_scroll && widget.last_scroll.first == fractions.first && widget.last_scroll.last == fractions.last { return }
|
|
widget.last_scroll = fractions;
|
|
widget.sent_scroll = true;
|
|
AppendScrollCommand(command, script, fractions);
|
|
unused BuilderAppend(script.bytes, "\nlist\n");
|
|
}
|
|
|
|
AppendScrollCommand :: (command: string, script: *Text, fractions: Scroll.State) {
|
|
unused BuilderAppend(script.bytes, command);
|
|
unused BuilderAppend(script.bytes, " ");
|
|
Scale.Append(script, fractions.first, Scroll.UNIT);
|
|
unused BuilderAppend(script.bytes, " ");
|
|
Scale.Append(script, fractions.last, Scroll.UNIT);
|
|
}
|
|
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
CanvasScroll :: (canvas: *Canvas.State, script: *Text) -> bool {
|
|
command := TextView(canvas.yscroll);
|
|
if command == "" { return false }
|
|
invocation: Text;
|
|
AppendScrollCommand(command, *invocation, Canvas.YFractions(*canvas.viewport, true));
|
|
unused BuilderAppend(script.bytes, "catch");
|
|
Lists.AppendElement(*script.bytes, TextView(invocation));
|
|
unused BuilderAppend(script.bytes, "\n");
|
|
return true;
|
|
}
|
|
}
|
|
|
|
CollectScroll :: (script: *Text) {
|
|
for * widgets {
|
|
if it.name.bytes.count > 0 && it.kind == TEXT { TextScroll(it, script) }
|
|
#if !#defined(TK_NO_CANVAS) {
|
|
if it.name.bytes.count > 0 && it.kind == CANVAS && it.canvas.viewport.resized {
|
|
it.canvas.viewport.resized = false;
|
|
unused CanvasScroll(*it.canvas, script);
|
|
}
|
|
}
|
|
#if #defined(TK_LISTBOX) {
|
|
if it.name.bytes.count > 0 && it.kind == LISTBOX { AppendScroll(it, script, Listbox.Fractions(*it.listbox)) }
|
|
}
|
|
}
|
|
}
|
|
|
|
Update :: (_: s64, name: string, args: []Text, result: *Result, script: *Text) {
|
|
if name != "update" || args.count != 0 { SetError(result, "expected update"); return }
|
|
CollectScroll(script);
|
|
if script.bytes.count > 0 { unused BuilderAppend(script.bytes, "list\n") }
|
|
}
|