KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > GTKNativeStyle


1 /*
2  * GTKNativeStyle.java
3  *
4  * @(#)GTKNativeStyle.java 1.7 07/03/15
5  *
6  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */

9
10 package com.sun.java.swing.plaf.gtk;
11
12 import java.awt.*;
13 import java.awt.image.*;
14 import javax.swing.*;
15 import javax.swing.plaf.*;
16 import javax.swing.plaf.synth.*;
17
18 import com.sun.java.swing.plaf.gtk.GTKConstants.WidgetType;
19 import com.sun.java.swing.plaf.gtk.GTKConstants.StateType;
20 import sun.awt.image.CachingSurfaceManager;
21 import sun.awt.image.SurfaceManager;
22 import sun.awt.UNIXToolkit;
23
24 /**
25  *
26  * @version 1.7, 03/15/07
27  * @author Kirill Kirichenko
28  */

29 class GTKNativeStyle extends GTKStyle {
30     
31     private native int native_get_xthickness(int widgetType);
32     private native int native_get_ythickness(int widgetType);
33     private native int native_get_color_for_state(int widgetType,
34             int state, int typeID);
35     private native Object JavaDoc native_get_class_value(int widgetType, String JavaDoc key);
36     private native String JavaDoc native_get_pango_font_name(int widgetType);
37     private native Dimension native_get_image_dimension(int widgetType, int state);
38     private native void native_get_image(int[] buffer, int width, int height,
39                                          int widgetType, int state);
40     
41     /**
42      * Type of the GTK widget that has this kind of style.
43      * I.e. it is responsible for the style.
44      */

45     private final int widgetType;
46
47     private final int xThickness, yThickness;
48     private final Font pangoFont;
49     
50     /**
51      * Background images for this style.
52      * They are uploaded by demand.
53      */

54     private Object JavaDoc[] images = new Object JavaDoc[5];
55     
56     private static final Object JavaDoc EMPTY_IMAGE_TAG = "<none>";
57     
58     GTKNativeStyle(Font font, WidgetType widgetType) {
59         super(font);
60         this.widgetType = widgetType.ordinal();
61
62         String JavaDoc pangoFontName = null;
63         synchronized(sun.awt.UNIXToolkit.GTK_LOCK) {
64             xThickness = native_get_xthickness(this.widgetType);
65             yThickness = native_get_ythickness(this.widgetType);
66             pangoFontName = native_get_pango_font_name(this.widgetType);
67         }
68         pangoFont = (pangoFontName != null) ?
69             PangoFonts.lookupFont(pangoFontName) : null;
70     }
71
72     Color getStyleSpecificColor(SynthContext context, int state, ColorType type) {
73         state = GTKLookAndFeel.synthStateToGTKStateType(state).ordinal();
74         synchronized(sun.awt.UNIXToolkit.GTK_LOCK) {
75             int rgb = native_get_color_for_state(widgetType, state, type.getID());
76             return new ColorUIResource(rgb);
77         }
78     }
79
80     /**
81      * Returns the font for the specified state.
82      *
83      * @param c JComponent the style is associated with
84      * @param id Region identifier
85      * @param state State of the region.
86      * @return Font to render with
87      */

88     protected Font getFontForState(JComponent c, Region id, int state) {
89         if (pangoFont != null) {
90             return pangoFont;
91         } else {
92             return super.getFontForState(c, id, state);
93         }
94     }
95     
96     /**
97      * Returns the X thickness to use for this GTKStyle.
98      *
99      * @return x thickness.
100      */

101     int getXThickness() {
102         return xThickness;
103     }
104
105     /**
106      * Returns the Y thickness to use for this GTKStyle.
107      *
108      * @return x thickness.
109      */

110     int getYThickness() {
111         return yThickness;
112     }
113     
114     /**
115      * Returns the value for a class specific property. A class specific value
116      * is a value that will be picked up based on class hierarchy.
117      *
118      * @param context SynthContext indentifying requestor
119      * @param key Key identifying class specific value
120      * @return Value, or null if one has not been defined.
121      */

122     Object JavaDoc getClassSpecificValue(Region region, String JavaDoc key) {
123         synchronized(sun.awt.UNIXToolkit.GTK_LOCK) {
124             return native_get_class_value(widgetType, key);
125         }
126     }
127
128     /**
129      * Returns the value for a class specific property for a particular
130      * WidgetType. This method is useful in those cases where we need to
131      * fetch a value for a Region that is not associated with the component
132      * currently in use (e.g. we need to figure out the insets for a
133      * SCROLL_BAR, but certain values can only be extracted from a
134      * SCROLL_PANE region).
135      *
136      * @param wt WidgetType for which to fetch the value
137      * @param key Key identifying class specific value
138      * @return Value, or null if one has not been defined
139      */

140     Object JavaDoc getClassSpecificValue(WidgetType wt, String JavaDoc key) {
141         synchronized (UNIXToolkit.GTK_LOCK) {
142             return native_get_class_value(wt.ordinal(), key);
143         }
144     }
145
146     /**
147      * Returns true if the style should fill in the background of the
148      * specified context for the specified state.
149      */

150     boolean fillBackground(SynthContext context, int state) {
151         Object JavaDoc image = getBackgroundImage(state);
152         return image == EMPTY_IMAGE_TAG;
153     }
154
155     /**
156      * Returns fontname specific for the given WidgetType
157      *
158      * @param wt WidgetType to return fontname for
159      * @return fontname
160      */

161     String JavaDoc getFontNameForWidgetType(WidgetType wt) {
162         synchronized (sun.awt.UNIXToolkit.GTK_LOCK) {
163             return native_get_pango_font_name(wt.ordinal());
164         }
165     }
166
167     /**
168      * Returns the Icon to fill the background in with for the specified
169      * context and state.
170      */

171     Image getBackgroundImage(SynthContext context, int state) {
172         Object JavaDoc image = getBackgroundImage(state);
173         return (image == EMPTY_IMAGE_TAG) ? null : (Image)image;
174     }
175
176     private Object JavaDoc getBackgroundImage(int state) {
177         state = GTKLookAndFeel.synthStateToGTKStateType(state).ordinal();
178         
179         if (images[state] != null) {
180             return images[state];
181         }
182         
183         Dimension size = null;
184         
185         synchronized(sun.awt.UNIXToolkit.GTK_LOCK) {
186             size = native_get_image_dimension(widgetType, state);
187         }
188         
189         if (size != null) {
190             BufferedImage image = new BufferedImage(size.width, size.height,
191                                                     BufferedImage.TYPE_INT_RGB);
192
193             // Getting the DataBuffer for an image (as it's done below) defeats
194
// possible future acceleration.
195
// Calling setLocalAccelerationEnabled on that image's surface
196
// manager re-enables it.
197
boolean accelerated = false;
198             CachingSurfaceManager csm = null;
199             SurfaceManager sm = SurfaceManager.getManager(image);
200             if (sm instanceof CachingSurfaceManager) {
201                 csm = (CachingSurfaceManager)sm;
202                 accelerated = csm.isLocalAccelerationEnabled();
203             }
204
205             DataBufferInt data = (DataBufferInt)image.getRaster().getDataBuffer();
206             synchronized(sun.awt.UNIXToolkit.GTK_LOCK) {
207                 native_get_image(data.getData(), size.width, size.height,
208                                  widgetType, state);
209             }
210             
211             if (csm != null && accelerated != csm.isLocalAccelerationEnabled()) {
212                 csm.setLocalAccelerationEnabled(accelerated);
213                 csm.rasterChanged();
214             }
215             
216             return images[state] = image;
217         }
218         
219         return images[state] = EMPTY_IMAGE_TAG;
220     }
221
222     Icon getStyleSpecificIcon(String JavaDoc key, TextDirection direction, int type) {
223         Image img = ((UNIXToolkit)Toolkit.getDefaultToolkit()).
224                 getStockIcon(widgetType, key, type, direction.ordinal(), null);
225         
226          return (img != null) ? new ImageIcon(img) : null;
227     }
228 }
229
Popular Tags