Bug 534007: [GTK3] Support for detecting themes like in 4.7.3 (but
without the memory leaks)

Check for the presence of the GTK_THEME environment variable. When
loading system colors from the GTK CSS theme, we can make use of this
information to load the proper theme information and dark variants, if
necessary.

Tested with the Adwaita and Arc themes (and their dark variants) on
GTK3.22, X11 and Wayland. No AllNonBrowser JUnit tests fail.

Change-Id: If3697495a941ac4e26fb2d714d4b221da65325a1
Signed-off-by: Eric Williams <ericwill@redhat.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java
index 7b7685e..b83491e 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java
@@ -691,6 +691,28 @@
 	 */
 	public static final boolean SWT_MENU_LOCATION_DEBUGGING;
 
+	/*
+	 * Check for the GTK_THEME environment variable. If set, parse
+	 * it to get the theme name and check if a dark variant is specified.
+	 * We can make use of this information when loading SWT system colors.
+	 * See bug 534007.
+	 */
+	/**
+	 * True if the GTK_THEME environment variable is specified
+	 * and is non-empty.
+	 */
+	public static final boolean GTK_THEME_SET;
+	/**
+	 * A string containing the theme name supplied via the GTK_THEME
+	 * environment variable. Otherwise this will contain an empty string.
+	 */
+	public static final String GTK_THEME_NAME;
+	/**
+	 * True if GTK_THEME_SET is true, and if the dark variant was
+	 * specified via the GTK_THEME environment variable.
+	 */
+	public static final boolean GTK_THEME_DARK;
+
 	/* Feature in Gtk: with the switch to GtkMenuItems from GtkImageMenuItems
 	* in Gtk3 came a small Gtk shortfall: a small amount of padding on the left hand
 	* side of MenuItems was added. This padding is not accessible to the developer,
@@ -720,6 +742,21 @@
 		}
 		SWT_MENU_LOCATION_DEBUGGING = menuLocationDebuggingEnabled;
 
+		String gtkThemeProperty = "GTK_THEME";
+		String gtkThemeCheck = getEnvironmentalVariable(gtkThemeProperty);
+		boolean gtkThemeSet = false;
+		String gtkThemeName = "";
+		boolean gtkThemeDark = false;
+		if (gtkThemeCheck != null && !gtkThemeCheck.isEmpty()) {
+			gtkThemeSet = true;
+			gtkThemeDark = gtkThemeCheck.contains(":dark") ? true : false;
+			String [] themeNameSplit = gtkThemeCheck.split(":");
+			gtkThemeName = themeNameSplit[0];
+		}
+		GTK_THEME_SET = gtkThemeSet;
+		GTK_THEME_NAME = gtkThemeName;
+		GTK_THEME_DARK = gtkThemeDark;
+
 		System.setProperty("org.eclipse.swt.internal.gtk.version",
 				(GTK.GTK_VERSION >>> 16) + "." + (GTK.GTK_VERSION >>> 8 & 0xFF) + "." + (GTK.GTK_VERSION & 0xFF));
 		// set GDK backend if we are on X11
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java
index a5e08f6..425c55f 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java
@@ -1925,16 +1925,22 @@
  * directly
  */
 String gtk_css_default_theme_values (int swt) {
-	// Find CSS theme name
+	/*
+	 * Find current GTK theme: either use the system theme,
+	 * or one provided using the GTK_THEME environment variable.
+	 * See bug 534007.
+	 */
 	int length;
 	long /*int*/ str;
-	byte [] buffer = OS.getThemeNameBytes();
+	byte [] buffer = OS.GTK_THEME_SET ? Converter.wcsToMbcs (OS.GTK_THEME_NAME, true) : OS.getThemeNameBytes();
+	// Load the dark variant if specified
+	byte [] darkBuffer = OS.GTK_THEME_DARK ? darkBuffer = Converter.wcsToMbcs ("dark", true) : null;
 	if (buffer == null || buffer.length == 0) {
 		return "";
 	}
 
 	// Fetch the actual theme in char/string format
-	long /*int*/ themeProvider = GTK.gtk_css_provider_get_named(buffer, null);
+	long /*int*/ themeProvider = GTK.gtk_css_provider_get_named(buffer, darkBuffer);
 	str = GTK.gtk_css_provider_to_string (themeProvider);
 	length = C.strlen (str);
 	if (length == 0) {