OSDN Git Service

libjava/
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / awt / peer / gtk / GdkScreenGraphicsDevice.java
1 /* GdkScreenGraphicsDevice.java -- information about a screen device
2    Copyright (C) 2004, 2005, 2006  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.java.awt.peer.gtk;
40
41 import java.awt.DisplayMode;
42 import java.awt.Frame;
43 import java.awt.GraphicsConfiguration;
44 import java.awt.GraphicsDevice;
45 import java.awt.Rectangle;
46 import java.awt.Window;
47 import java.util.ArrayList;
48
49 import gnu.classpath.Configuration;
50 import gnu.classpath.Pointer;
51
52 class GdkScreenGraphicsDevice extends GraphicsDevice
53 {
54   private final int native_state = GtkGenericPeer.getUniqueInteger ();
55   
56   private Window fullscreenWindow;
57   
58   private boolean oldWindowDecorationState;
59   
60   private Rectangle oldWindowBounds;
61   
62   private Rectangle bounds;
63   
64   private GdkGraphicsConfiguration[] configurations;
65   
66   /** The <code>GdkGraphicsEnvironment</code> instance that created this
67    * <code>GdkScreenGraphicsDevice</code>. This is only needed for native
68    * methods which need to access the 'native_state' field storing a pointer
69    * to a GdkDisplay object.
70    */ 
71   GdkGraphicsEnvironment env;
72   
73   /** An identifier that is created by Gdk
74    */
75   String idString;
76   
77   /** The display modes supported by this <code>GdkScreenGraphicsDevice</code>.
78    * If the array is <code>null</code> <code>nativeGetDisplayModes</code> has
79    * to be called.
80    */
81   X11DisplayMode[] displayModes;
82
83   /** The non-changeable display mode of this <code>GdkScreenGraphicsDevice
84    * </code>. This field gets initialized by the {@link #init()} method. If it
85    * is still <code>null</code> afterwards, the XRandR extension is available
86    * and display mode changes are possible. If it is non-null XRandR is not
87    * available, no display mode changes are possible and no other native
88    * method must be called. 
89    */
90   DisplayMode fixedDisplayMode;
91
92   /**
93    * The pointer to the native screen resource.
94    *
95    * This field is manipulated by native code. Don't change or remove
96    * without adjusting the native code.
97    */
98   private Pointer screen;
99
100   static
101   {
102     if (Configuration.INIT_LOAD_LIBRARY)
103       {
104         System.loadLibrary("gtkpeer");
105       }
106
107     GtkToolkit.initializeGlobalIDs();
108     initIDs();
109   }
110   
111   static native void initIDs();
112   
113   GdkScreenGraphicsDevice (GdkGraphicsEnvironment e)
114   {
115     super();
116     env = e;
117     
118     configurations = new GdkGraphicsConfiguration[1];
119     configurations[0] = new GdkGraphicsConfiguration(this);
120   }
121
122   /** This method is called from the native side immediately after
123    * the constructor is run.
124    */
125   void init()
126   {
127     fixedDisplayMode = nativeGetFixedDisplayMode(env);
128   }
129   
130   /** Depending on the availability of the XRandR extension the method returns
131    * the screens' non-changeable display mode or null, meaning that XRandR can
132    * handle display mode changes.
133    */
134   native DisplayMode nativeGetFixedDisplayMode(GdkGraphicsEnvironment env);
135   
136   public int getType ()
137   {
138     // Gdk manages only raster screens.
139     return GraphicsDevice.TYPE_RASTER_SCREEN;
140   }
141
142   public String getIDstring ()
143   {
144     if (idString == null)
145       idString = nativeGetIDString();
146     
147     return idString;
148   }
149   
150   private native String nativeGetIDString(); 
151
152   public GraphicsConfiguration[] getConfigurations ()
153   {
154     return (GraphicsConfiguration[]) configurations.clone();
155   }
156   
157   public GraphicsConfiguration getDefaultConfiguration ()
158   {
159     return configurations[0];
160   }
161
162
163   /**
164    * Returns the current display mode of this device, or null if unknown.
165    *
166    * @return the current display mode
167    * @see #setDisplayMode(DisplayMode)
168    * @see #getDisplayModes()
169    * @since 1.4
170    */
171   public DisplayMode getDisplayMode()
172   {
173     if (fixedDisplayMode != null)
174       return fixedDisplayMode;
175     
176     synchronized (this)
177       {
178         if (displayModes == null)
179           displayModes = nativeGetDisplayModes(env);
180       }
181
182     int index = nativeGetDisplayModeIndex(env);
183     int rate = nativeGetDisplayModeRate(env);
184     
185     return new DisplayMode(displayModes[index].width,
186                            displayModes[index].height,
187                            DisplayMode.BIT_DEPTH_MULTI,
188                            rate);
189   }
190   
191   native int nativeGetDisplayModeIndex(GdkGraphicsEnvironment env);
192   
193   native int nativeGetDisplayModeRate(GdkGraphicsEnvironment env);
194   
195   public DisplayMode[] getDisplayModes()
196   {
197     if (fixedDisplayMode != null)
198       return new DisplayMode[] { fixedDisplayMode };
199     
200     synchronized (this)
201       {
202         if (displayModes == null)
203           displayModes = nativeGetDisplayModes(env);
204       }
205     
206     ArrayList<DisplayMode> list = new ArrayList<DisplayMode>();
207     for(int i=0;i<displayModes.length;i++)
208       for(int j=0;j<displayModes[i].rates.length;j++)
209         list.add(new DisplayMode(displayModes[i].width,
210                                  displayModes[i].height,
211                                  DisplayMode.BIT_DEPTH_MULTI,
212                                  displayModes[i].rates[j]));
213     
214     return list.toArray(new DisplayMode[list.size()]);
215   }
216   
217   native X11DisplayMode[] nativeGetDisplayModes(GdkGraphicsEnvironment env);
218
219   /**
220    * Real fullscreen exclusive mode is not supported.
221    *
222    * @return <code>false</code>
223    * @since 1.4
224    */
225   public boolean isFullScreenSupported()
226   {
227     return true;
228   }
229   
230   public boolean isDisplayChangeSupported()
231   {
232     return fixedDisplayMode == null;
233   }
234
235   public void setDisplayMode(DisplayMode dm)
236   {
237     if (fixedDisplayMode != null)
238       throw new UnsupportedOperationException("Cannnot change display mode.");
239     
240     if (dm == null)
241       throw new IllegalArgumentException("DisplayMode must not be null.");
242     
243     synchronized (this)
244       {
245         if (displayModes == null)
246           displayModes = nativeGetDisplayModes(env);
247       }
248     
249     for (int i=0; i<displayModes.length; i++)
250       if (displayModes[i].width == dm.getWidth()
251           && displayModes[i].height == dm.getHeight())
252         {
253           synchronized (this)
254           {
255             nativeSetDisplayMode(env,
256                                  i,
257                                  (short) dm.getRefreshRate());
258           
259             bounds = null;
260           }
261           
262           return;
263         }
264     
265     throw new IllegalArgumentException("Mode not supported by this device.");
266   }
267   
268   native void nativeSetDisplayMode(GdkGraphicsEnvironment env,
269                                 int index, short rate);
270   
271   /** A class that simply encapsulates the X11 display mode data.
272    */
273   static class X11DisplayMode
274   {
275     short[] rates;
276     int width;
277     int height;
278     
279     X11DisplayMode(int width, int height, short[] rates)
280     {
281       this.width = width;
282       this.height = height;
283       this.rates = rates;
284     }
285     
286   }
287   
288   public void setFullScreenWindow(Window w)
289   {
290     // Bring old fullscreen window back into its original state.
291     if (fullscreenWindow != null && w != fullscreenWindow)
292       {
293         if (fullscreenWindow instanceof Frame)
294           {
295             // Decoration state can only be switched when the peer is
296             // non-existent. That means we have to dispose the 
297             // Frame.
298             Frame f = (Frame) fullscreenWindow;
299             if (oldWindowDecorationState != f.isUndecorated())
300               {
301                 f.dispose();
302                 f.setUndecorated(oldWindowDecorationState);
303               }
304           }
305         
306         fullscreenWindow.setBounds(oldWindowBounds);
307
308         if (!fullscreenWindow.isVisible())
309           fullscreenWindow.setVisible(true);
310       }
311     
312     // If applicable remove decoration, then maximize the window and
313     // bring it to the foreground.
314     if (w != null)
315       {
316         if (w instanceof Frame)
317           {
318             Frame f = (Frame) w;
319             oldWindowDecorationState = f.isUndecorated();
320             if (!oldWindowDecorationState)
321               {
322                 f.dispose();
323                 f.setUndecorated(true);
324               }
325           }
326         
327         oldWindowBounds = w.getBounds();
328     
329         DisplayMode dm = getDisplayMode();
330     
331         w.setBounds(0, 0, dm.getWidth(), dm.getHeight());
332         
333         if (!w.isVisible())
334           w.setVisible(true);
335         
336         w.requestFocus();
337         w.toFront();
338         
339       }
340     
341     fullscreenWindow = w;
342   }
343   
344   public Window getFullScreenWindow()
345   {
346    return fullscreenWindow; 
347   }
348
349   Rectangle getBounds()
350   {
351    synchronized(this)
352      {
353        if (bounds == null)
354          bounds = nativeGetBounds();
355      }
356    
357    return bounds;
358   }
359   
360   native Rectangle nativeGetBounds();
361
362 }