168 lines
7.6 KiB
Ziran
168 lines
7.6 KiB
Ziran
#import "std/vec"
|
|
using Tcl :: #import "tcl/interpreter"
|
|
using Tk :: #import "tk/widgets"
|
|
using Paint :: #import "tk/paint"
|
|
using Input :: #import "widget_fixtures"
|
|
|
|
main :: () -> s32 {
|
|
interpreter: Tcl.Interpreter;
|
|
if !Tk.Attach(*interpreter) { return 1 }
|
|
reference := Tcl.Eval(*interpreter, Input.Script);
|
|
if reference.code != .OK || TextView(reference.value) != ".f.input.e" { return 2 }
|
|
print("%\n", TextView(reference.value));
|
|
entry := Tcl.Eval(*interpreter, ".f.input.e get");
|
|
if entry.code != .OK || TextView(entry.value) != "aßéz" { return 9 }
|
|
print("%\n", TextView(entry.value));
|
|
index := Tcl.Eval(*interpreter, ".f.input.e index insert");
|
|
if index.code != .OK || TextView(index.value) != "2" { return 10 }
|
|
end := Tcl.Eval(*interpreter, ".f.input.e index end");
|
|
if end.code != .OK || TextView(end.value) != "4" { return 11 }
|
|
forward := Tcl.Eval(*interpreter, ".sample search -forwards -- café 1.1");
|
|
if forward.code != .OK || TextView(forward.value) != "1.11" { return 16 }
|
|
backward := Tcl.Eval(*interpreter, ".sample search -backwards -- café 1.11");
|
|
if backward.code != .OK || TextView(backward.value) != "1.0" { return 17 }
|
|
utf := Tcl.Eval(*interpreter, ".sample get 1.0 1.0+4c");
|
|
if utf.code != .OK || TextView(utf.value) != "café" { return 18 }
|
|
wrap := Tcl.Eval(*interpreter, ".sample search café end");
|
|
if wrap.code != .OK || TextView(wrap.value) != "1.0" { return 19 }
|
|
bounded := Tcl.Eval(*interpreter, ".sample search café 1.1 1.11");
|
|
if bounded.code != .OK || bounded.value.bytes.count != 0 { return 20 }
|
|
point := Tcl.Eval(*interpreter, ".sample index @31,6");
|
|
if point.code != .OK || TextView(point.value) != "1.2" { return 21 }
|
|
|
|
bad_config := Tcl.Eval(*interpreter, ".count configure -text Lost -unsupported 1");
|
|
if bad_config.code != .Error { return 3 }
|
|
after := Tcl.Eval(*interpreter, ".count cget -text");
|
|
if after.code != .OK || TextView(after.value) != "9 balls" { return 4 }
|
|
changed := Tcl.Eval(*interpreter,
|
|
"button .local -command {incr n}; proc p {} {set n 8; .local invoke}; p");
|
|
if changed.code != .OK || TextView(changed.value) != "9" { return 5 }
|
|
|
|
Tk.Layout(300, 208);
|
|
entry_id := Tk.Find(".f.input.e");
|
|
layout := Tk.widgets[entry_id].box;
|
|
if layout.w <= 168 || layout.y < 100 || layout.y + layout.h >= 208 { return 12 }
|
|
Tcl.Eval(*interpreter, "frame .f.hidden; entry .f.hidden.e; pack .f.hidden.e");
|
|
Tk.Layout(300, 208);
|
|
if Tk.widgets[Tk.Find(".f.hidden.e")].box.w != 0 { return 13 }
|
|
removed := Tcl.Eval(*interpreter, "destroy .f; focus");
|
|
if removed.code != .OK || removed.value.bytes.count != 0 || Tk.Find(".f.input.e") >= 0 ||
|
|
Tk.Find(".f.hidden.e") >= 0 { return 14 }
|
|
reused := Tcl.Eval(*interpreter, "frame .f; entry .f.input; .f.input get");
|
|
if reused.code != .OK || reused.value.bytes.count != 0 { return 15 }
|
|
Tk.PointerDown(150, 70);
|
|
Tcl.Eval(*interpreter, "destroy .more");
|
|
cancelled: Text = Tk.PointerUp(150, 70);
|
|
if cancelled.bytes.count != 0 { return 6 }
|
|
destroyed := Tcl.Eval(*interpreter, ".more cget -text");
|
|
if destroyed.code != .Error { return 7 }
|
|
if !ScaleCommands(*interpreter) { return 22 }
|
|
if !Rendering(*interpreter) { return 23 }
|
|
root := Tcl.Eval(*interpreter, "destroy .");
|
|
if root.code != .OK || !Tk.Closed() { return 8 }
|
|
return 0;
|
|
}
|
|
|
|
ScaleCommands :: (interpreter: *Tcl.Interpreter) -> bool {
|
|
input := Input.ScaleCommands;
|
|
count: s64;
|
|
first: s64;
|
|
for i: 0..input.count-1 {
|
|
if input[i] != 10 { continue }
|
|
result := Tcl.Eval(interpreter, input[first:i]);
|
|
if result.code != .OK { print("%\n", TextView(result.value)); return false }
|
|
if result.value.bytes.count > 0 {
|
|
value := TextView(result.value);
|
|
if count >= Input.ScaleResults.count || value != Input.ScaleResults[count] {
|
|
print("unexpected scale script result: %\n", value);
|
|
return false;
|
|
}
|
|
print("%\n", value);
|
|
count += 1;
|
|
}
|
|
first = i + 1;
|
|
}
|
|
if count != Input.ScaleResults.count { return false }
|
|
Tk.Layout(448, 380);
|
|
index := Tk.Find(".range");
|
|
widget := *Tk.widgets[index];
|
|
track := Tk.ScaleTrack(widget);
|
|
if track.x < 12 || widget.scale.value != 5000 { return false }
|
|
y := track.y + track.h/2;
|
|
x := track.x + 7;
|
|
unused Tk.PointerPress(x, y);
|
|
result: Text = Tk.PointerMove(x+30, y);
|
|
if TextView(result) == "" || widget.scale.value >= 5000 { return false }
|
|
unused Tk.PointerUp(x+30, y);
|
|
invalid := Tcl.Eval(interpreter, ".range configure -from 99 -orient unknown");
|
|
if invalid.code != .Error || widget.scale.first != 5000 { return false }
|
|
return true;
|
|
}
|
|
|
|
MemoryBitmap :: (name: string, bytes: *Text) -> bool {
|
|
if name != "dot" { return false }
|
|
return BuilderAppend(bytes.bytes, " k1 0 0 8 1 ") &&
|
|
VecPush(bytes.bytes, cast(u8)128);
|
|
}
|
|
|
|
WIDTH :: 80;
|
|
HEIGHT :: 64;
|
|
FIRST_ROW :: 16;
|
|
|
|
Rendering :: (interpreter: *Tcl.Interpreter) -> bool {
|
|
Tk.SetImageLoader(MemoryBitmap);
|
|
if !Evaluate(interpreter, "destroy .range .image .count .f .sample", "") ||
|
|
!Evaluate(interpreter, "menu .options; .options add command -label Red; .options entrycget 0 -label", "Red") ||
|
|
!Evaluate(interpreter, "listbox .rows; .rows insert end café naïve; .rows get 1", "naïve") ||
|
|
!Evaluate(interpreter, "canvas .art -width 40 -height 25 -bd 0 -bg white", ".art") ||
|
|
!Evaluate(interpreter, ".art create rectangle 1 1 20 20 -fill red -outline {} -tags backing", "1") ||
|
|
!Evaluate(interpreter, ".art create rectangle 4 4 16 16 -fill blue -outline {} -tags front", "2") ||
|
|
!Evaluate(interpreter, "image create bitmap dot -maskfile @dot", "dot") ||
|
|
!Evaluate(interpreter, ".art create image 25 10 -image dot -anchor nw", "3") ||
|
|
!Evaluate(interpreter, "image delete dot; pack .art -anchor nw", "") { return false }
|
|
|
|
pixels: [WIDTH*HEIGHT*4]u8;
|
|
Tk.Layout(WIDTH, HEIGHT);
|
|
box := Tk.widgets[Tk.Find(".art")].box;
|
|
if box.w != 40 || box.h != 25 {
|
|
print("unexpected Canvas geometry %x%\n", box.w, box.h);
|
|
return false;
|
|
}
|
|
Paint.Render(pixels[:], WIDTH, HEIGHT);
|
|
paint_at := ((box.y+7)*WIDTH+box.x+7)*4;
|
|
mask_at := ((box.y+10)*WIDTH+box.x+25)*4;
|
|
if !Pixel(pixels[paint_at:paint_at+4], 0, 0, 255) ||
|
|
!Pixel(pixels[mask_at:mask_at+4], 0, 0, 0) ||
|
|
!Pixel(pixels[mask_at+4:mask_at+8], 255, 255, 255) { return false }
|
|
if !Evaluate(interpreter, ".art delete front; .art find withtag backing", "1") { return false }
|
|
Paint.Render(pixels[:], WIDTH, HEIGHT);
|
|
if !Pixel(pixels[paint_at:paint_at+4], 255, 0, 0) { return false }
|
|
|
|
rows: [WIDTH*(HEIGHT-FIRST_ROW)*4]u8;
|
|
Paint.Render(rows[:], WIDTH, HEIGHT, FIRST_ROW);
|
|
checksum: s64;
|
|
for rows {
|
|
if it != pixels[FIRST_ROW*WIDTH*4+it_index] {
|
|
print("partial paint differs at byte %\n", it_index);
|
|
return false;
|
|
}
|
|
checksum = (checksum*257+cast(s64)it) % 2147483647;
|
|
}
|
|
print("RGBA %\n", checksum);
|
|
return true;
|
|
}
|
|
|
|
Evaluate :: (interpreter: *Tcl.Interpreter, script: string, expected: string) -> bool {
|
|
result := Tcl.Eval(interpreter, script);
|
|
got := TextView(result.value);
|
|
if result.code == .OK && got == expected { return true }
|
|
print("%: expected %, got %\n", script, expected, got);
|
|
return false;
|
|
}
|
|
|
|
Pixel :: (pixel: []u8, r: u8, g: u8, b: u8) -> bool {
|
|
if pixel[0] == r && pixel[1] == g && pixel[2] == b && pixel[3] == 255 { return true }
|
|
print("expected RGBA %,%,%,255, got %,%,%,%\n", r, g, b, pixel[0], pixel[1], pixel[2], pixel[3]);
|
|
return false;
|
|
}
|