210 lines
11 KiB
Ziran
210 lines
11 KiB
Ziran
using Scroll :: #import "gtk_scroll_linux";
|
|
using Gtk :: #import "gtk_style_linux";
|
|
using Format :: #import "std/format";
|
|
using Vectors :: #import "std/vec";
|
|
Bytes :: #import "std/byte_text_linux";
|
|
CString :: #import "std/c_string";
|
|
Number :: #import "std/number_text";
|
|
libc :: #system_library "c";
|
|
Env :: (name: *u8) -> *u8 #foreign libc "getenv";
|
|
Sleep :: (microseconds: u32) -> s32 #foreign libc "usleep";
|
|
Open :: (path: *u8, mode: *u8) -> *void #foreign libc "fopen";
|
|
Write :: (data: *u8, size: usize, count: usize, file: *void) -> usize #foreign libc "fwrite";
|
|
Close :: (file: *void) -> s32 #foreign libc "fclose";
|
|
gdk :: #system_library "gdk-3";
|
|
gtk :: #system_library "gtk-3";
|
|
gobject :: #system_library "gobject-2.0";
|
|
Settings :: () -> *void #foreign gtk "gtk_settings_get_default";
|
|
SetBoolean :: (object: *void, name: *u8, value: s32, end: *u8) #foreign gobject "g_object_set";
|
|
Display :: () -> *void #foreign gdk "gdk_display_get_default";
|
|
Seat :: (display: *void) -> *void #foreign gdk "gdk_display_get_default_seat";
|
|
Device :: (seat: *void) -> *void #foreign gdk "gdk_seat_get_pointer";
|
|
DevicePosition :: (device: *void, screen: *void, x: *s32, y: *s32) #foreign gdk "gdk_device_get_position";
|
|
DeviceGrabbed :: (display: *void, device: *void) -> s32 #foreign gdk "gdk_display_device_is_grabbed";
|
|
PointerState :: () -> GtkScrollRectangle {
|
|
result: GtkScrollRectangle; display:=Display(); device:=Device(Seat(display))
|
|
DevicePosition(device,null,*result.x,*result.y);result.width=DeviceGrabbed(display,device)
|
|
return result
|
|
}
|
|
PointerSame :: (before: GtkScrollRectangle) -> bool {
|
|
after:=PointerState();return before.x==after.x && before.y==after.y && before.width==after.width
|
|
}
|
|
Read :: (name: string) -> string {
|
|
buffer: [64]u8
|
|
if !CString.CopyCString(name,buffer[:]) { return "" }
|
|
return Bytes.TextFromCString(Env(*buffer[0]))
|
|
}
|
|
Integer :: (name: string, fallback: s32) -> s32 {
|
|
value,count:=Number.IntegerPrefix(Read(name)); return ifx count>0 then cast(s32)value else fallback
|
|
}
|
|
Bar :: (name: string, value: GtkScrollBarMetrics) {
|
|
print("% % % % % % % % % % % % % % % %\n",name,cast(s32)value.visible,cast(s32)value.overlay,value.opacity,
|
|
value.bounds.x,value.bounds.y,value.bounds.width,value.bounds.height,value.slider_start,value.slider_end,
|
|
value.minimum_width,value.minimum_height,value.value,value.lower,value.upper,value.page_size)
|
|
}
|
|
Pulse :: (handle: u64, props: GtkScrollProps) -> GtkScrollMetrics {
|
|
result:=GtkScrollUpdate(handle,props)
|
|
for i: 0..5 { unused Sleep(20000); result=GtkScrollUpdate(handle,props) }
|
|
return result
|
|
}
|
|
Snapshot :: (name: string, m: GtkScrollMetrics) {
|
|
print("% % % % % % % % % % % %\n",name,cast(s32)m.dragging,m.horizontal.opacity,m.vertical.opacity,
|
|
m.horizontal.value,m.vertical.value,m.horizontal.bounds.width,m.horizontal.bounds.height,
|
|
m.vertical.bounds.width,m.vertical.bounds.height,m.horizontal.slider_start,m.vertical.slider_start)
|
|
}
|
|
ReleaseMany :: (handles: []u64) {
|
|
at: s64
|
|
while at<handles.count { if handles[at]!=0 { unused GtkScrollRelease(handles[at]) }; at+=1 }
|
|
}
|
|
Bounds :: (nodes: []GtkStyleNode, handle: u64, props: GtkScrollProps) -> s32 {
|
|
if GtkScrollRelease(0) || GtkScrollCancel(0) || GtkScrollUpdate(0,props).available { return 20 }
|
|
empty:=GtkScrollRaster(0); if empty.rgba.count!=0 { return 21 }
|
|
if GtkScrollCreate(nodes[:1])!=0 { return 22 }
|
|
for choice: 0..5 {
|
|
bad:=props
|
|
if choice==0 { bad.width=0 } else if choice==1 { bad.width=4097 }
|
|
else if choice==2 { bad.height=-1 } else if choice==3 { bad.content_width=-1 }
|
|
else if choice==4 { bad.pointer_x=100001.0 } else { bad.pointer_y=-100001.0 }
|
|
if GtkScrollUpdate(handle,bad).available { return 23 }
|
|
}
|
|
for scale: cast(s32)0..5 {
|
|
if scale!=0 && scale!=5 { continue }
|
|
image:=GtkScrollRaster(handle,false,scale); if image.rgba.count!=0 { GtkStyleBitmapRelease(*image); return 24 }
|
|
}
|
|
handles: [31]u64
|
|
defer ReleaseMany(handles[:])
|
|
at: s32=0
|
|
while at<31 { handles[at]=GtkScrollCreate(nodes); if handles[at]==0 { return 25 }; at+=1 }
|
|
if GtkScrollCreate(nodes)!=0 { return 26 }
|
|
at=0
|
|
while at<31 {
|
|
id:=handles[at]; if !GtkScrollRelease(id) || GtkScrollRelease(id) || GtkScrollUpdate(id,props).available { return 27 }
|
|
handles[at]=0; at+=1
|
|
}
|
|
at=0
|
|
while at<8 {
|
|
id:=GtkScrollCreate(nodes); if id==0 || id==handle { return 28 }
|
|
if !GtkScrollCancel(id) || !GtkScrollRelease(id) { return 29 }; at+=1
|
|
}
|
|
print("BOUNDS 1\n")
|
|
return 0
|
|
}
|
|
main :: () -> s32 {
|
|
root_classes: [1]string=.["background"]; grid_classes: [1]string=.["horizontal"]
|
|
classes: [1]string=.["frame"]
|
|
nodes: [3]GtkStyleNode=.[.{.widget_type="GtkWindow",.element="window",.classes=root_classes[:]},
|
|
.{.widget_type="GtkGrid",.element="grid",.classes=grid_classes[:]},
|
|
.{.widget_type="GtkScrolledWindow",.element="scrolledwindow",.classes=classes[:]}]
|
|
css:=Read("SCROLL_CSS"); provider: u64
|
|
if css!="" {
|
|
provider=GtkStyleProviderCreate(css)
|
|
if provider==0 || !GtkStyleProviderInstall(provider,800) { return 41 }
|
|
}
|
|
defer { if provider!=0 { unused GtkStyleProviderRelease(provider) } }
|
|
if Integer("SCROLL_ANIMATIONS",0)!=0 {
|
|
if !GtkNativeReady() { return 45 }
|
|
key: [32]u8; unused CString.CopyCString("gtk-enable-animations",key[:])
|
|
SetBoolean(Settings(),*key[0],1,null)
|
|
}
|
|
handle:=GtkScrollCreate(nodes[:]); if handle==0 { print("CREATE FAILED\n"); return 1 }
|
|
defer { unused GtkScrollRelease(handle) }
|
|
pointer_before:=PointerState()
|
|
props:=GtkScrollProps.{.width=Integer("SCROLL_WIDTH",300),.height=Integer("SCROLL_HEIGHT",200),
|
|
.content_width=Integer("SCROLL_CONTENT_WIDTH",100),.content_height=Integer("SCROLL_CONTENT_HEIGHT",1000),
|
|
.offset_x=Integer("SCROLL_X",0),.offset_y=Integer("SCROLL_Y",0),.horizontal=true,.vertical=true,
|
|
.overlay=Integer("SCROLL_OVERLAY",1)!=0}
|
|
metrics:=GtkScrollUpdate(handle,props)
|
|
if Integer("SCROLL_WHEEL",0)!=0 {
|
|
for horizontal: cast(s32)0..1 {
|
|
for step: cast(s32)0..7 {
|
|
delta: float64=ifx step<4 then -1.0 else 1.0
|
|
metrics=GtkScrollWheel(handle,delta,horizontal==1)
|
|
print("WHEEL % % % %\n",horizontal,step,metrics.horizontal.value,metrics.vertical.value)
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
for i: 0..5 { unused Sleep(20000); metrics=GtkScrollUpdate(handle,props) }
|
|
idle:=Integer("SCROLL_IDLE_MS",0)
|
|
if idle>0 {
|
|
if props.offset_y>0 && metrics.vertical.opacity<=0.0 { return 46 }
|
|
props.pointer_present=true; props.pointer_x=40.0; props.pointer_y=40.0
|
|
for i: 0..idle/20 {
|
|
metrics=GtkScrollUpdate(handle,props)
|
|
image:=GtkScrollRaster(handle); GtkStyleBitmapRelease(*image)
|
|
unused Sleep(20000)
|
|
}
|
|
metrics=GtkScrollUpdate(handle,props)
|
|
if metrics.vertical.opacity!=0.0 { return 47 }
|
|
}
|
|
if !metrics.available { print("UPDATE FAILED\n"); return 2 }
|
|
replacement_css:=Read("SCROLL_CSS_CHANGE")
|
|
if replacement_css!="" {
|
|
generation:=metrics.generation
|
|
if !GtkStyleProviderReplace(provider,replacement_css) { return 42 }
|
|
metrics=Pulse(handle,props)
|
|
if metrics.generation<=generation { return 43 }
|
|
}
|
|
action:=Read("SCROLL_ACTION")
|
|
if action!="" {
|
|
horizontal:=action=="h"
|
|
bar:=ifx horizontal then metrics.horizontal else metrics.vertical
|
|
props.pointer_present=true
|
|
props.pointer_x=cast(float64)bar.bounds.x+cast(float64)bar.bounds.width*0.5
|
|
props.pointer_y=cast(float64)bar.bounds.y+cast(float64)bar.bounds.height*0.5
|
|
metrics=Pulse(handle,props); Snapshot("HOVER",metrics)
|
|
bar=ifx horizontal then metrics.horizontal else metrics.vertical
|
|
if horizontal { props.pointer_x=cast(float64)bar.bounds.x+cast(float64)(bar.slider_start+bar.slider_end)*0.5 }
|
|
else { props.pointer_y=cast(float64)bar.bounds.y+cast(float64)(bar.slider_start+bar.slider_end)*0.5 }
|
|
metrics=Pulse(handle,props)
|
|
props.pressed=true; props.down=true
|
|
metrics=GtkScrollUpdate(handle,props); Snapshot("PRESS",metrics);props.pressed=false
|
|
if horizontal { props.pointer_x+=30.0 } else { props.pointer_y+=30.0 }
|
|
metrics=GtkScrollUpdate(handle,props); Snapshot("DRAG",metrics)
|
|
if horizontal { props.pointer_x=cast(float64)props.width+100.0 } else { props.pointer_y=cast(float64)props.height+100.0 }
|
|
metrics=GtkScrollUpdate(handle,props); Snapshot("OUTSIDE",metrics)
|
|
if Integer("SCROLL_DESTROY",0)!=0 {
|
|
if !metrics.dragging || !GtkScrollRelease(handle) || GtkScrollUpdate(handle,props).available || GtkScrollCancel(handle) { return 30 }
|
|
replacement:=GtkScrollCreate(nodes[:]); if replacement==0 || replacement==handle { return 31 }
|
|
props.down=false;props.pressed=false;props.released=false;props.pointer_present=false
|
|
metrics=Pulse(replacement,props)
|
|
if metrics.dragging || !GtkScrollRelease(replacement) { return 32 }
|
|
if !PointerSame(pointer_before) { return 40 }
|
|
print("DESTROY 1\n"); return 0
|
|
}
|
|
props.down=false;props.released=Integer("SCROLL_CANCEL",0)==0
|
|
if Integer("SCROLL_CANCEL",0)==2 { if !GtkScrollCancel(handle) || !GtkScrollCancel(handle) { return 44 } }
|
|
metrics=GtkScrollUpdate(handle,props); Snapshot("RELEASE",metrics)
|
|
}
|
|
if Integer("SCROLL_LIFECYCLE",0)!=0 { result:=Bounds(nodes[:],handle,props); if result!=0 { return result } }
|
|
print("VIEW % % % %\n",metrics.viewport.x,metrics.viewport.y,metrics.viewport.width,metrics.viewport.height)
|
|
Bar("H",metrics.horizontal); Bar("V",metrics.vertical)
|
|
horizontal:=Integer("SCROLL_RASTER_H",0)!=0
|
|
scale:=Integer("SCROLL_SCALE",1)
|
|
backdrop: Vec(u8)
|
|
defer VecFree(backdrop)
|
|
alpha:=Integer("SCROLL_BACKDROP_ALPHA",-1)
|
|
if alpha>=0 {
|
|
bar:=ifx horizontal then metrics.horizontal else metrics.vertical
|
|
if !VecResize(backdrop,cast(s64)bar.bounds.width*cast(s64)bar.bounds.height*scale*scale*4) { return 48 }
|
|
for i: cast(s64)0..backdrop.count/4-1 {
|
|
backdrop[i*4]=32; backdrop[i*4+1]=48; backdrop[i*4+2]=80; backdrop[i*4+3]=cast(u8)alpha
|
|
}
|
|
invalid:=GtkScrollRasterOver(handle,backdrop[:backdrop.count-1],horizontal,scale)
|
|
if invalid.rgba.count!=0 { GtkStyleBitmapRelease(*invalid); return 49 }
|
|
}
|
|
image:=GtkScrollRasterOver(handle,backdrop[:],horizontal,scale)
|
|
defer GtkStyleBitmapRelease(*image)
|
|
print("IMAGE % % %\n",image.width,image.height,image.rgba.count)
|
|
output:=Read("SCROLL_OUTPUT")
|
|
if output!="" && image.rgba.count>0 {
|
|
path: [1024]u8; mode: [3]u8
|
|
if !CString.CopyCString(output,path[:]) || !CString.CopyCString("wb",mode[:]) { return 3 }
|
|
file:=Open(*path[0],*mode[0]); if file==null { return 4 }
|
|
count:=Write(*image.rgba[0],1,cast(usize)image.rgba.count,file); unused Close(file)
|
|
if count!=cast(usize)image.rgba.count { return 5 }
|
|
}
|
|
if !PointerSame(pointer_before) { return 40 }
|
|
return 0
|
|
}
|