001: /*
002: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt;
026:
027: import java.awt.RenderingHints;
028: import static java.awt.RenderingHints.*;
029: import java.awt.color.ColorSpace;
030: import java.awt.image.*;
031: import java.security.AccessController;
032: import java.security.PrivilegedAction;
033: import sun.security.action.GetIntegerAction;
034: import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection;
035: import sun.java2d.opengl.OGLRenderQueue;
036: import java.lang.reflect.InvocationTargetException;
037:
038: public abstract class UNIXToolkit extends SunToolkit {
039: /** All calls into GTK should be synchronized on this lock */
040: public static final Object GTK_LOCK = new Object();
041:
042: private static final int[] BAND_OFFSETS = { 0, 1, 2 };
043: private static final int[] BAND_OFFSETS_ALPHA = { 0, 1, 2, 3 };
044: private static final int DEFAULT_DATATRANSFER_TIMEOUT = 10000;
045:
046: private Boolean nativeGTKAvailable;
047: private Boolean nativeGTKLoaded;
048: private BufferedImage tmpImage = null;
049:
050: public static int getDatatransferTimeout() {
051: Integer dt = (Integer) AccessController
052: .doPrivileged(new GetIntegerAction(
053: "sun.awt.datatransfer.timeout"));
054: if (dt == null || dt <= 0) {
055: return DEFAULT_DATATRANSFER_TIMEOUT;
056: } else {
057: return dt;
058: }
059: }
060:
061: /**
062: * Returns true if the native GTK libraries are capable of being
063: * loaded and are expected to work properly, false otherwise. Note
064: * that this method will not leave the native GTK libraries loaded if
065: * they haven't already been loaded. This allows, for example, Swing's
066: * GTK L&F to test for the presence of native GTK support without
067: * leaving the native libraries loaded. To attempt long-term loading
068: * of the native GTK libraries, use the loadGTK() method instead.
069: */
070: @Override
071: public boolean isNativeGTKAvailable() {
072: synchronized (GTK_LOCK) {
073: if (nativeGTKLoaded != null) {
074: // We've already attempted to load GTK, so just return the
075: // status of that attempt.
076: return nativeGTKLoaded.booleanValue();
077:
078: } else if (nativeGTKAvailable != null) {
079: // We've already checked the availability of the native GTK
080: // libraries, so just return the status of that attempt.
081: return nativeGTKAvailable.booleanValue();
082:
083: } else {
084: boolean success = check_gtk();
085: nativeGTKAvailable = Boolean.valueOf(success);
086: return success;
087: }
088: }
089: }
090:
091: /**
092: * Loads the GTK libraries, if necessary. The first time this method
093: * is called, it will attempt to load the native GTK library. If
094: * successful, it leaves the library open and returns true; otherwise,
095: * the library is left closed and returns false. On future calls to
096: * this method, the status of the first attempt is returned (a simple
097: * lightweight boolean check, no native calls required).
098: */
099: public boolean loadGTK() {
100: synchronized (GTK_LOCK) {
101: if (nativeGTKLoaded == null) {
102: boolean success = load_gtk();
103: nativeGTKLoaded = Boolean.valueOf(success);
104: }
105: }
106: return nativeGTKLoaded.booleanValue();
107: }
108:
109: /**
110: * Overridden to handle GTK icon loading
111: */
112: protected Object lazilyLoadDesktopProperty(String name) {
113: if (name.startsWith("gtk.icon.")) {
114: return lazilyLoadGTKIcon(name);
115: }
116: return super .lazilyLoadDesktopProperty(name);
117: }
118:
119: /**
120: * Load a native Gtk stock icon.
121: *
122: * @param longname a desktop property name. This contains icon name, size
123: * and orientation, e.g. <code>"gtk.icon.gtk-add.4.rtl"</code>
124: * @return an <code>Image</code> for the icon, or <code>null</code> if the
125: * icon could not be loaded
126: */
127: protected Object lazilyLoadGTKIcon(String longname) {
128: // Check if we have already loaded it.
129: Object result = desktopProperties.get(longname);
130: if (result != null) {
131: return result;
132: }
133:
134: // We need to have at least gtk.icon.<stock_id>.<size>.<orientation>
135: String str[] = longname.split("\\.");
136: if (str.length != 5) {
137: return null;
138: }
139:
140: // Parse out the stock icon size we are looking for.
141: int size = 0;
142: try {
143: size = Integer.parseInt(str[3]);
144: } catch (NumberFormatException nfe) {
145: return null;
146: }
147:
148: // Direction.
149: TextDirection dir = ("ltr".equals(str[4]) ? TextDirection.LTR
150: : TextDirection.RTL);
151:
152: // Load the stock icon.
153: BufferedImage img = getStockIcon(-1, str[2], size, dir
154: .ordinal(), null);
155: if (img != null) {
156: // Create the desktop property for the icon.
157: setDesktopProperty(longname, img);
158: }
159: return img;
160: }
161:
162: /**
163: * Returns a BufferedImage which contains the Gtk icon requested. If no
164: * such icon exists or an error occurs loading the icon the result will
165: * be null.
166: *
167: * @param filename
168: * @return The icon or null if it was not found or loaded.
169: */
170: public BufferedImage getGTKIcon(final String filename) {
171: if (!loadGTK()) {
172: return null;
173:
174: } else {
175: // Call the native method to load the icon.
176: synchronized (GTK_LOCK) {
177: if (!load_gtk_icon(filename)) {
178: tmpImage = null;
179: }
180: }
181: }
182: // Return local image the callback loaded the icon into.
183: return tmpImage;
184: }
185:
186: /**
187: * Returns a BufferedImage which contains the Gtk stock icon requested.
188: * If no such stock icon exists the result will be null.
189: *
190: * @param widgetType one of WidgetType values defined in GTKNativeEngine or
191: * -1 for system default stock icon.
192: * @param stockId String which defines the stock id of the gtk item.
193: * For a complete list reference the API at www.gtk.org for StockItems.
194: * @param iconSize One of the GtkIconSize values defined in GTKConstants
195: * @param textDirection One of the TextDirection values defined in
196: * GTKConstants
197: * @param detail Render detail that is passed to the native engine (feel
198: * free to pass null)
199: * @return The stock icon or null if it was not found or loaded.
200: */
201: public BufferedImage getStockIcon(final int widgetType,
202: final String stockId, final int iconSize,
203: final int direction, final String detail) {
204: if (!loadGTK()) {
205: return null;
206:
207: } else {
208: // Call the native method to load the icon.
209: synchronized (GTK_LOCK) {
210: if (!load_stock_icon(widgetType, stockId, iconSize,
211: direction, detail)) {
212: tmpImage = null;
213: }
214: }
215: }
216: // Return local image the callback loaded the icon into.
217: return tmpImage; // set by loadIconCallback
218: }
219:
220: /**
221: * This method is used by JNI as a callback from load_stock_icon.
222: * Image data is passed back to us via this method and loaded into the
223: * local BufferedImage and then returned via getStockIcon.
224: *
225: * Do NOT call this method directly.
226: */
227: public void loadIconCallback(byte[] data, int width, int height,
228: int rowStride, int bps, int channels, boolean alpha) {
229: // Reset the stock image to null.
230: tmpImage = null;
231:
232: // Create a new BufferedImage based on the data returned from the
233: // JNI call.
234: DataBuffer dataBuf = new DataBufferByte(data,
235: (rowStride * height));
236: // Maybe test # channels to determine band offsets?
237: WritableRaster raster = Raster.createInterleavedRaster(dataBuf,
238: width, height, rowStride, channels,
239: (alpha ? BAND_OFFSETS_ALPHA : BAND_OFFSETS), null);
240: ColorModel colorModel = new ComponentColorModel(ColorSpace
241: .getInstance(ColorSpace.CS_sRGB), alpha, false,
242: ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
243:
244: // Set the local image so we can return it later from
245: // getStockIcon().
246: tmpImage = new BufferedImage(colorModel, raster, false, null);
247: }
248:
249: private static native boolean check_gtk();
250:
251: private static native boolean load_gtk();
252:
253: private static native boolean unload_gtk();
254:
255: private native boolean load_gtk_icon(String filename);
256:
257: private native boolean load_stock_icon(int widget_type,
258: String stock_id, int iconSize, int textDirection,
259: String detail);
260:
261: private native void nativeSync();
262:
263: public void sync() {
264: // flush the X11 buffer
265: nativeSync();
266: // now flush the OGL pipeline (this is a no-op if OGL is not enabled)
267: OGLRenderQueue.sync();
268: }
269:
270: /*
271: * This returns the value for the desktop property "awt.font.desktophints"
272: * It builds this by querying the Gnome desktop properties to return
273: * them as platform independent hints.
274: * This requires that the Gnome properties have already been gathered.
275: */
276: public static final String FONTCONFIGAAHINT = "fontconfig/Antialias";
277:
278: protected RenderingHints getDesktopAAHints() {
279:
280: Object aaValue = getDesktopProperty("gnome.Xft/Antialias");
281:
282: if (aaValue == null) {
283: /* On a KDE desktop running KWin the rendering hint will
284: * have been set as property "fontconfig/Antialias".
285: * No need to parse further in this case.
286: */
287: aaValue = getDesktopProperty(FONTCONFIGAAHINT);
288: if (aaValue != null) {
289: return new RenderingHints(KEY_TEXT_ANTIALIASING,
290: aaValue);
291: } else {
292: return null; // no Gnome or KDE Desktop properties available.
293: }
294: }
295:
296: /* 0 means off, 1 means some ON. What would any other value mean?
297: * If we require "1" to enable AA then some new value would cause
298: * us to default to "OFF". I don't think that's the best guess.
299: * So if its !=0 then lets assume AA.
300: */
301: boolean aa = Boolean
302: .valueOf(((aaValue instanceof Number) && ((Number) aaValue)
303: .intValue() != 0));
304: Object aaHint;
305: if (aa) {
306: String subpixOrder = (String) getDesktopProperty("gnome.Xft/RGBA");
307:
308: if (subpixOrder == null || subpixOrder.equals("none")) {
309: aaHint = VALUE_TEXT_ANTIALIAS_ON;
310: } else if (subpixOrder.equals("rgb")) {
311: aaHint = VALUE_TEXT_ANTIALIAS_LCD_HRGB;
312: } else if (subpixOrder.equals("bgr")) {
313: aaHint = VALUE_TEXT_ANTIALIAS_LCD_HBGR;
314: } else if (subpixOrder.equals("vrgb")) {
315: aaHint = VALUE_TEXT_ANTIALIAS_LCD_VRGB;
316: } else if (subpixOrder.equals("vbgr")) {
317: aaHint = VALUE_TEXT_ANTIALIAS_LCD_VBGR;
318: } else {
319: /* didn't recognise the string, but AA is requested */
320: aaHint = VALUE_TEXT_ANTIALIAS_ON;
321: }
322: } else {
323: aaHint = VALUE_TEXT_ANTIALIAS_DEFAULT;
324: }
325: return new RenderingHints(KEY_TEXT_ANTIALIASING, aaHint);
326: }
327: }
|