|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Patrick Ziegler and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Patrick Ziegler - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.wb.internal.os.linux; |
| 14 | + |
| 15 | +import java.lang.foreign.Arena; |
| 16 | +import java.lang.foreign.FunctionDescriptor; |
| 17 | +import java.lang.foreign.SymbolLookup; |
| 18 | +import java.lang.foreign.ValueLayout; |
| 19 | +import java.lang.invoke.MethodHandle; |
| 20 | + |
| 21 | +/** |
| 22 | + * The GTK toolkit |
| 23 | + */ |
| 24 | +public abstract class GTK extends Native { |
| 25 | + protected static final SymbolLookup GTK; |
| 26 | + |
| 27 | + static { |
| 28 | + if (isGtk4()) { |
| 29 | + GTK = SymbolLookup.libraryLookup("libgtk-4.so.0", Arena.ofAuto()); |
| 30 | + } else { |
| 31 | + GTK = SymbolLookup.libraryLookup("libgtk-3.so.0", Arena.ofAuto()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private static class InstanceHolder { |
| 36 | + private static final MethodHandle gtk_widget_get_allocated_baseline = createHandle(GTK, "gtk_widget_get_allocated_baseline", |
| 37 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS)); |
| 38 | + |
| 39 | + private static final MethodHandle gtk_get_major_version = createHandle(GTK, "gtk_get_major_version", |
| 40 | + FunctionDescriptor.of(ValueLayout.JAVA_INT)); |
| 41 | + |
| 42 | + private static final MethodHandle gtk_get_minor_version = createHandle(GTK, "gtk_get_minor_version", |
| 43 | + FunctionDescriptor.of(ValueLayout.JAVA_INT)); |
| 44 | + |
| 45 | + private static final MethodHandle gtk_get_micro_version = createHandle(GTK, "gtk_get_micro_version", |
| 46 | + FunctionDescriptor.of(ValueLayout.JAVA_INT)); |
| 47 | + |
| 48 | + private static final MethodHandle gtk_widget_get_allocation = createHandle(GTK, "gtk_widget_get_allocation", |
| 49 | + FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS)); |
| 50 | + |
| 51 | + private static final MethodHandle gtk_widget_hide = createHandle(GTK, "gtk_widget_hide", |
| 52 | + FunctionDescriptor.ofVoid(ValueLayout.ADDRESS)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return The major version number of the GTK+ library. |
| 57 | + */ |
| 58 | + public static int gtk_get_major_version() { |
| 59 | + return (int) callSafe(() -> InstanceHolder.gtk_get_major_version.invoke()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return The minor version number of the GTK+ library. |
| 64 | + */ |
| 65 | + public static int gtk_get_minor_version() { |
| 66 | + return (int) callSafe(() -> InstanceHolder.gtk_get_minor_version.invoke()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return The micro version number of the GTK+ library. |
| 71 | + */ |
| 72 | + public static int gtk_get_micro_version() { |
| 73 | + return (int) callSafe(() -> InstanceHolder.gtk_get_micro_version.invoke()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Retrieves the widget’s allocation. |
| 78 | + * |
| 79 | + * Note, when implementing a {@code GtkContainer}: a widget’s allocation will be |
| 80 | + * its “adjusted” allocation, that is, the widget’s parent container typically |
| 81 | + * calls {@code gtk_widget_size_allocate()} with an allocation, and that |
| 82 | + * allocation is then adjusted (to handle margin and alignment for example) |
| 83 | + * before assignment to the widget. |
| 84 | + * |
| 85 | + * {@code gtk_widget_get_allocation()} returns the adjusted allocation that was |
| 86 | + * actually assigned to the widget. The adjusted allocation is guaranteed to be |
| 87 | + * completely contained within the {@code gtk_widget_size_allocate()} |
| 88 | + * allocation, however. |
| 89 | + * |
| 90 | + * So a {@code GtkContainer} is guaranteed that its children stay inside the |
| 91 | + * assigned bounds, but not that they have exactly the bounds the container |
| 92 | + * assigned. There is no way to get the original allocation assigned by |
| 93 | + * {@code gtk_widget_size_allocate()}, since it isn’t stored; if a container |
| 94 | + * implementation needs that information it will have to track it itself. |
| 95 | + */ |
| 96 | + public static void gtk_widget_get_allocation(GtkWidget widget, GtkAllocation allocation) { |
| 97 | + runSafe(() -> InstanceHolder.gtk_widget_get_allocation.invoke(widget.segment(), allocation.segment())); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Reverses the effects of {@code gtk_widget_show()}, causing the {@code widget} |
| 102 | + * to be hidden (invisible to the user). |
| 103 | + */ |
| 104 | + public static void gtk_widget_hide(GtkWidget widget) { |
| 105 | + runSafe(() -> InstanceHolder.gtk_widget_hide.invoke(widget.segment())); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the baseline that has currently been allocated to {@code widget} or |
| 110 | + * -1, if none. |
| 111 | + */ |
| 112 | + public static int gtk_widget_get_allocated_baseline(GtkWidget widget) { |
| 113 | + return (int) callSafe(() -> InstanceHolder.gtk_widget_get_allocated_baseline.invoke(widget.segment())); |
| 114 | + } |
| 115 | +} |
0 commit comments