Source for gnu.java.awt.peer.x.XWindowPeer

   1: /* XWindowPeer.java -- Window peer for X
   2:    Copyright (C) 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.x;
  40: 
  41: import java.awt.Component;
  42: import java.awt.EventQueue;
  43: import java.awt.Font;
  44: import java.awt.FontMetrics;
  45: import java.awt.Graphics;
  46: import java.awt.Image;
  47: import java.awt.Insets;
  48: import java.awt.Point;
  49: import java.awt.Rectangle;
  50: import java.awt.event.PaintEvent;
  51: import java.awt.event.WindowEvent;
  52: 
  53: import gnu.x11.Window;
  54: import gnu.x11.event.Event;
  55: 
  56: import gnu.java.awt.peer.swing.SwingWindowPeer;
  57: 
  58: public class XWindowPeer
  59:     extends SwingWindowPeer
  60: {
  61: 
  62:   private static int standardSelect = Event.BUTTON_PRESS_MASK
  63:                                       | Event.BUTTON_RELEASE_MASK
  64:                                       | Event.POINTER_MOTION_MASK
  65:                                       //| Event.RESIZE_REDIRECT_MASK
  66:                                       | Event.EXPOSURE_MASK
  67:                                       //| Event.PROPERTY_CHANGE_MASK
  68:                                       | Event.STRUCTURE_NOTIFY_MASK
  69:                                       | Event.KEY_PRESS_MASK
  70:                                       | Event.KEY_RELEASE_MASK
  71:                                       ;
  72: 
  73:   /**
  74:    * Indicates if we are in callback mode, that is when a property (like size)
  75:    * is changed in reponse to a request from the X server and doesn't need
  76:    * to be propagated back to the X server.
  77:    */
  78:   boolean callback = false;
  79: 
  80:   /**
  81:    * The X window.
  82:    */
  83:   private Window xwindow;
  84: 
  85:   XWindowPeer(java.awt.Window window)
  86:   {
  87:     super(window);
  88:     XGraphicsDevice dev = XToolkit.getDefaultDevice();
  89: 
  90:     // TODO: Maybe initialize lazily in show().
  91:     // FIXME: Howto generate a Window without decorations?
  92:     int x = Math.max(window.getX(), 0);
  93:     int y = Math.max(window.getY(), 0);
  94:     int w = Math.max(window.getWidth(), 1);
  95:     int h = Math.max(window.getHeight(), 1);
  96:     xwindow = new Window(dev.getDisplay().default_root, x, y, w, h);
  97:     xwindow.create();
  98:     xwindow.select_input(standardSelect);
  99:     dev.getEventPump().registerWindow(xwindow, window);
 100:   }
 101: 
 102:   public void toBack()
 103:   {
 104:     // TODO Auto-generated method stub
 105: 
 106:   }
 107: 
 108:   public void toFront()
 109:   {
 110:     // TODO Auto-generated method stub
 111: 
 112:   }
 113: 
 114:   public void updateAlwaysOnTop()
 115:   {
 116:     // TODO Auto-generated method stub
 117: 
 118:   }
 119: 
 120:   public boolean requestWindowFocus()
 121:   {
 122:     // TODO Auto-generated method stub
 123:     return false;
 124:   }
 125: 
 126:   public Point getLocationOnScreen()
 127:   {
 128:     return new Point(xwindow.x, xwindow.y);
 129:   }
 130: 
 131:   /**
 132:    * Returns a XGraphics suitable for drawing on this frame.
 133:    *
 134:    * @return a XGraphics suitable for drawing on this frame
 135:    */
 136:   public Graphics getGraphics()
 137:   {
 138:     return new XGraphics(xwindow);
 139:   }
 140: 
 141:   public Image createImage(int w, int h)
 142:   {
 143:     return new XImage(w, h);
 144:   }
 145: 
 146:   /**
 147:    * Makes the component visible. This is called by {@link Component#show()}.
 148:    *
 149:    * This is implemented to call setVisible(true) on the Swing component.
 150:    */
 151:   public void show()
 152:   {
 153: //    // Prevent ResizeRedirect events.
 154: //    //xwindow.select_input(noResizeRedirectSelect);
 155: //    Window.Attributes atts = new Window.Attributes();
 156: //    atts.set_override_redirect(true);
 157: //    xwindow.change_attributes(atts);
 158: 
 159:     // Prevent ResizeRedirect events.
 160:     //xwindow.select_input(Event.NO_EVENT_MASK);
 161:     //xwindow.select_input(noResizeRedirectSelect);
 162: 
 163:     xwindow.map();
 164:     EventQueue eq = XToolkit.getDefaultToolkit().getSystemEventQueue();
 165:     java.awt.Window w = (java.awt.Window) super.awtComponent;
 166:     eq.postEvent(new WindowEvent(w, WindowEvent.WINDOW_OPENED));
 167:     eq.postEvent(new PaintEvent(w, PaintEvent.PAINT,
 168:                                 new Rectangle(0, 0, w.getWidth(),
 169:                                               w.getHeight())));
 170: 
 171: //    // Reset input selection.
 172: //    atts.set_override_redirect(false);
 173: //    xwindow.change_attributes(atts);
 174:   }
 175: 
 176:   /**
 177:    * Makes the component invisible. This is called from
 178:    * {@link Component#hide()}.
 179:    *
 180:    * This is implemented to call setVisible(false) on the Swing component.
 181:    */
 182:   public void hide()
 183:   {
 184:     xwindow.unmap();
 185:   }
 186: 
 187:   /**
 188:    * Notifies the peer that the bounds of this component have changed. This
 189:    * is called by {@link Component#reshape(int, int, int, int)}.
 190:    *
 191:    * This is implemented to call setBounds() on the Swing component.
 192:    *
 193:    * @param x the X coordinate of the upper left corner of the component
 194:    * @param y the Y coordinate of the upper left corner of the component
 195:    * @param width the width of the component
 196:    * @param height the height of the component
 197:    */
 198:   public void reshape(int x, int y, int width, int height)
 199:   {
 200:     // Prevent ResizeRedirect events.
 201: //    //xwindow.select_input(noResizeRedirectSelect);
 202: //    Window.Attributes atts = new Window.Attributes();
 203: //    atts.set_override_redirect(true);
 204: //    xwindow.change_attributes(atts);
 205: 
 206:     // Need to substract insets because AWT size is including insets,
 207:     // and X size is excuding insets.
 208:     Insets i = insets();
 209:     xwindow.move_resize(x - i.left, y - i.right, width - i.left - i.right,
 210:                         height - i.top - i.bottom);
 211: 
 212:     // Reset input selection.
 213: //    atts = new Window.Attributes();
 214: //    atts.set_override_redirect(false);
 215: //    xwindow.change_attributes(atts);
 216:   }
 217: 
 218:   public Insets insets()
 219:   {
 220:     Insets i = new Insets(0, 0, 0, 0);
 221: //    Window.GeometryReply g = xwindow.geometry();
 222: //    int b = g.border_width();
 223: //    Insets i = new Insets(b, b, b, b);
 224: //    Window.WMSizeHints wmSize = xwindow.wm_normal_hints();
 225: //    if (wmSize != null)
 226: //      {
 227: //        i.left = wmSize.x() - g.x();
 228: //        i.right = wmSize.width() - g.width() - i.left ;
 229: //        i.top = wmSize.y() - g.y();
 230: //        i.bottom = wmSize.height() - g.height() - i.top;
 231: //      }
 232: //    System.err.println("insets: " + i);
 233:     return i;
 234:   }
 235: 
 236:   /**
 237:    * Returns the font metrics for the specified font.
 238:    *
 239:    * @return the font metrics for the specified font
 240:    */
 241:   public FontMetrics getFontMetrics(Font font)
 242:   {
 243:     XFontPeer fontPeer = (XFontPeer) font.getPeer();
 244:     return fontPeer.getFontMetrics(font);
 245:   }
 246: 
 247:   /**
 248:    * Unregisters the window in the event pump when it is closed.
 249:    */
 250:   protected void finalize()
 251:   {
 252:     XGraphicsDevice dev = XToolkit.getDefaultDevice();
 253:     dev.getEventPump().unregisterWindow(xwindow);
 254:   }
 255: }