From 6be7445a7702b880096759705bcbf850e1e910b0 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 3 Jul 2017 20:59:09 +0200 Subject: [PATCH] Added methods to get widget sizes. Code cleanup. --- src/dorkbox/systemTray/jna/linux/Gtk.java | 43 +++++++++++++++++-- src/dorkbox/systemTray/jna/linux/Gtk2.java | 24 ++++++++++- src/dorkbox/systemTray/jna/linux/Gtk3.java | 20 ++++++++- .../systemTray/jna/linux/GtkBorder.java | 20 --------- 4 files changed, 80 insertions(+), 27 deletions(-) delete mode 100644 src/dorkbox/systemTray/jna/linux/GtkBorder.java diff --git a/src/dorkbox/systemTray/jna/linux/Gtk.java b/src/dorkbox/systemTray/jna/linux/Gtk.java index 745d725..8615afe 100644 --- a/src/dorkbox/systemTray/jna/linux/Gtk.java +++ b/src/dorkbox/systemTray/jna/linux/Gtk.java @@ -39,7 +39,7 @@ import dorkbox.util.jna.JnaHelper; *

* Direct-mapping, See: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md */ -@SuppressWarnings({"Duplicates", "SameParameterValue", "DeprecatedIsStillUsed", "WeakerAccess", "deprecation"}) +@SuppressWarnings({"Duplicates", "SameParameterValue", "DeprecatedIsStillUsed", "WeakerAccess"}) public class Gtk { // objdump -T /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0 | grep gtk @@ -67,6 +67,15 @@ class Gtk { public static final int FLAG_BACKDROP = 1 << 6; } + public static class IconSize { + public static final int INVALID = 0; // Invalid size. + public static final int MENU = 1; // Size appropriate for menus (16px). + public static final int SMALL_TOOLBAR = 2; // Size appropriate for small toolbars (16px). + public static final int LARGE_TOOLBAR = 3; // Size appropriate for large toolbars (24px) + public static final int BUTTON = 4; // Size appropriate for buttons (16px) + public static final int DND = 5; // Size appropriate for drag and drop (32px) + public static final int DIALOG = 6; // Size appropriate for dialogs (48px) + } // NOTE: AppIndicator uses this info to figure out WHAT VERSION OF appindicator to use: GTK2 -> appindicator1, GTK3 -> appindicator3 @@ -925,11 +934,37 @@ class Gtk { } /** - * Retrieves the border width of the container. See gtk_container_set_border_width(). + * This function is typically used when implementing a GtkContainer subclass. Obtains the preferred size of a widget. The + * container uses this information to arrange its child widgets and decide what size allocations to give them with + * gtk_widget_size_allocate(). + * + * You can also call this function from an application, with some caveats. Most notably, getting a size request requires the + * widget to be associated with a screen, because font information may be needed. Multihead-aware applications should keep this in mind. + * + * Also remember that the size request is not necessarily the size a widget will actually be allocated. */ public static - int gtk_container_get_border_width(Pointer container) { - return Gtk2.gtk_container_get_border_width(container); + void gtk_widget_size_request(final Pointer widget, final Pointer requisition) { + if (isGtk2) { + Gtk2.gtk_widget_size_request(widget, requisition); + } + else { + Gtk3.gtk_widget_get_preferred_size(widget, requisition, null); + } + } + + /** + * Creates a new GtkImageMenuItem containing the image and text from a stock item. Some stock ids have preprocessor macros + * like GTK_STOCK_OK and GTK_STOCK_APPLY. + * + * @param stock_id the name of the stock item. + * @param accel_group the GtkAccelGroup to add the menu items accelerator to, or NULL. + * + * @return a new GtkImageMenuItem. + */ + public static + Pointer gtk_image_menu_item_new_from_stock(String stock_id, Pointer accel_group) { + return Gtk2.gtk_image_menu_item_new_from_stock(stock_id, accel_group); } } diff --git a/src/dorkbox/systemTray/jna/linux/Gtk2.java b/src/dorkbox/systemTray/jna/linux/Gtk2.java index 6607fad..ffe5e2d 100644 --- a/src/dorkbox/systemTray/jna/linux/Gtk2.java +++ b/src/dorkbox/systemTray/jna/linux/Gtk2.java @@ -381,8 +381,28 @@ class Gtk2 { Pointer gtk_offscreen_window_new (); /** - * Retrieves the border width of the container. See gtk_container_set_border_width(). + * This function is typically used when implementing a GtkContainer subclass. Obtains the preferred size of a widget. The + * container uses this information to arrange its child widgets and decide what size allocations to give them with + * gtk_widget_size_allocate(). + * + * You can also call this function from an application, with some caveats. Most notably, getting a size request requires the + * widget to be associated with a screen, because font information may be needed. Multihead-aware applications should keep this in mind. + * + * Also remember that the size request is not necessarily the size a widget will actually be allocated. + */ + @Deprecated + public static native + void gtk_widget_size_request(final Pointer widget, final Pointer requisition); + + /** + * Creates a new GtkImageMenuItem containing the image and text from a stock item. Some stock ids have preprocessor macros + * like GTK_STOCK_OK and GTK_STOCK_APPLY. + * + * @param stock_id the name of the stock item. + * @param accel_group the GtkAccelGroup to add the menu items accelerator to, or NULL. + * + * @return a new GtkImageMenuItem. */ public static native - int gtk_container_get_border_width(Pointer container); + Pointer gtk_image_menu_item_new_from_stock(String stock_id, Pointer accel_group); } diff --git a/src/dorkbox/systemTray/jna/linux/Gtk3.java b/src/dorkbox/systemTray/jna/linux/Gtk3.java index 48f8256..d8ae0fa 100644 --- a/src/dorkbox/systemTray/jna/linux/Gtk3.java +++ b/src/dorkbox/systemTray/jna/linux/Gtk3.java @@ -89,7 +89,6 @@ class Gtk3 { */ public static native Pointer gtk_widget_get_style_context(Pointer widget); - /** * Saves the context state, so temporary modifications done through gtk_style_context_add_class(), gtk_style_context_remove_class(), * gtk_style_context_set_state(), etc. can quickly be reverted in one go through gtk_style_context_restore(). @@ -148,4 +147,23 @@ class Gtk3 { */ public static native int gdk_window_get_scale_factor (Pointer window); + + /** + * Retrieves the minimum and natural size of a widget, taking into account the widget’s preference for height-for-width management. + * + * This is used to retrieve a suitable size by container widgets which do not impose any restrictions on the child placement. + * It can be used to deduce toplevel window and menu sizes as well as child widgets in free-form containers such as GtkLayout. + * + * Handle with care. Note that the natural height of a height-for-width widget will generally be a smaller size than the minimum + * height, since the required height for the natural width is generally smaller than the required height for the minimum width. + * + * Use gtk_widget_get_preferred_height_and_baseline_for_width() if you want to support baseline alignment. + * + * + * @param widget a GtkWidget instance + * @param minimum_size location for storing the minimum size, or NULL. + * @param natural_size location for storing the natural size, or NULL. + */ + public static native + void gtk_widget_get_preferred_size(final Pointer widget, final Pointer minimum_size, final Pointer natural_size); } diff --git a/src/dorkbox/systemTray/jna/linux/GtkBorder.java b/src/dorkbox/systemTray/jna/linux/GtkBorder.java deleted file mode 100644 index d454729..0000000 --- a/src/dorkbox/systemTray/jna/linux/GtkBorder.java +++ /dev/null @@ -1,20 +0,0 @@ -package dorkbox.systemTray.jna.linux; - -import java.util.Arrays; -import java.util.List; - -import com.sun.jna.Structure; - -public -class GtkBorder extends Structure { - public short left; - public short right; - public short top; - public short bottom; - - @Override - protected - List getFieldOrder() { - return Arrays.asList("left", "right", "top", "bottom"); - } -}