public UiString string(String name) {
return new UiString(this, name);
}
+
+ public void attach(Document doc) {
+ ToolkitFuncs ui = ToolkitFuncs.getInstance();
+ try {
+ ui.attach_document.invoke(ptr, doc.getPtr());
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ }
}
package de.unixwork.ui;
-public class Document {
+import java.lang.foreign.MemorySegment;
+
+public class Document extends Context {
+ protected MemorySegment ptr;
+
+ public Document() {
+ ToolkitFuncs ui = ToolkitFuncs.getInstance();
+ // create a toolkit document object and get the UiContext ptr
+ try {
+ // the document_new size parameter is not really relevant here
+ ptr = (MemorySegment) ui.document_new.invoke(16);
+ setCtx((MemorySegment) ui.document_context.invoke(ptr));
+
+ Toolkit toolkit = Toolkit.getInstance();
+ toolkit.registerDocument(this);
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public MemorySegment getPtr() {
+ return ptr;
+ }
+
+ public void close() {
+ ToolkitFuncs ui = ToolkitFuncs.getInstance();
+ try {
+ ui.document_destroy.invoke(ptr);
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ }
}
public class Event {
private UiObject object;
- private Object documemt;
+ private Document document;
private Object windowData;
private Object eventdata;
private int intval;
windowPtr = (MemorySegment) ui.event_get_windowdata.invoke(eventPtr);
documentPtr = (MemorySegment) ui.event_get_document.invoke(eventPtr);
+ document = toolkit.getDocument(documentPtr.address());
+
intval = (int)ui.event_get_int.invoke(eventPtr);
set = (int)ui.event_get_set.invoke(eventPtr);
} catch (Throwable e) {
return object;
}
- public Object getDocumemt() {
- return documemt;
+ public Document getDocumemt() {
+ return document;
}
public Object getWindowData() {
private Application app;
private HashMap<Long, UiObject> toplevelObjects = new HashMap<>();
+ private HashMap<Long, Document> documents = new HashMap<>();
// used for all strings and other memory, that is expected to be const
// and the UI toolkit does not create copies
public UiObject getToplevelObject(long address) {
return toplevelObjects.get(address);
}
+
+ public void registerDocument(Document doc) {
+ documents.put(doc.ptr.address(), doc);
+ }
+
+ public void removeDocument(Document doc) {
+ documents.remove(doc.ptr.address());
+ }
+
+ public Document getDocument(long address) {
+ return documents.get(address);
+ }
}
public MethodHandle object_get_context;
+ public MethodHandle document_new;
+ public MethodHandle document_destroy;
+ public MethodHandle document_context;
+ public MethodHandle attach_document;
+ public MethodHandle detach_document;
+ public MethodHandle context_parent;
+
public MethodHandle event_get_obj;
public MethodHandle event_get_document;
public MethodHandle event_get_windowdata;
FunctionDescriptor sigi_m = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS);
FunctionDescriptor sigm_mm = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
FunctionDescriptor sigv_mm = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS);
+ FunctionDescriptor sigv_m = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
MemorySegment object_get_context_addr = lib.find("ui_object_get_context").orElseThrow();
+ MemorySegment document_new_addr = lib.find("ui_document_new").orElseThrow();
+ MemorySegment document_destroy_addr = lib.find("ui_document_destroy").orElseThrow();
+ MemorySegment document_context_addr = lib.find("ui_document_context").orElseThrow();
+ MemorySegment attach_document_addr = lib.find("ui_attach_document").orElseThrow();
+ MemorySegment detach_document_addr = lib.find("ui_detach_document").orElseThrow();
+ MemorySegment context_parent_addr = lib.find("ui_context_parent").orElseThrow();
+
MemorySegment event_get_obj_addr = lib.find("ui_event_get_obj").orElseThrow();
MemorySegment event_get_document_addr = lib.find("ui_event_get_document").orElseThrow();
MemorySegment event_get_windowdata_addr = lib.find("ui_event_get_windowdata").orElseThrow();
object_get_context = linker.downcallHandle(object_get_context_addr, sigm_m);
+ // use JAVA_LONG for size_t, because we don't support 32bit platforms
+ document_new = linker.downcallHandle(document_new_addr, FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG));
+ document_destroy = linker.downcallHandle(document_destroy_addr, sigv_m);
+ document_context = linker.downcallHandle(document_context_addr, sigm_m);
+ attach_document = linker.downcallHandle(attach_document_addr, sigv_mm);
+ detach_document = linker.downcallHandle(detach_document_addr, sigv_mm);
+ context_parent = linker.downcallHandle(context_parent_addr, sigm_m);
+
event_get_obj = linker.downcallHandle(event_get_obj_addr, sigm_m);
event_get_document = linker.downcallHandle(event_get_document_addr, sigm_m);
event_get_windowdata = linker.downcallHandle(event_get_windowdata_addr, sigm_m);