Source for gnu.java.awt.peer.gtk.GtkWindowPeer

   1: /* GtkWindowPeer.java -- Implements WindowPeer with GTK
   2:    Copyright (C) 1998, 1999, 2002, 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.Component;
  42: import java.awt.Frame;
  43: import java.awt.Graphics;
  44: import java.awt.KeyboardFocusManager;
  45: import java.awt.Rectangle;
  46: import java.awt.Window;
  47: import java.awt.event.ComponentEvent;
  48: import java.awt.event.FocusEvent;
  49: import java.awt.event.PaintEvent;
  50: import java.awt.event.WindowEvent;
  51: import java.awt.peer.WindowPeer;
  52: 
  53: public class GtkWindowPeer extends GtkContainerPeer
  54:   implements WindowPeer
  55: {
  56:   protected static final int GDK_WINDOW_TYPE_HINT_NORMAL = 0;
  57:   protected static final int GDK_WINDOW_TYPE_HINT_DIALOG = 1;
  58:   protected static final int GDK_WINDOW_TYPE_HINT_MENU = 2;
  59:   protected static final int GDK_WINDOW_TYPE_HINT_TOOLBAR = 3;
  60:   protected static final int GDK_WINDOW_TYPE_HINT_SPLASHSCREEN = 4;
  61:   protected static final int GDK_WINDOW_TYPE_HINT_UTILITY = 5;
  62:   protected static final int GDK_WINDOW_TYPE_HINT_DOCK = 6;
  63:   protected static final int GDK_WINDOW_TYPE_HINT_DESKTOP = 7;
  64: 
  65:   private boolean hasBeenShown = false;
  66:   private int oldState = Frame.NORMAL;
  67: 
  68:   // Cached awt window component location, width and height.
  69:   private int x, y, width, height;
  70: 
  71:   native void gtkWindowSetTitle (String title);
  72:   native void gtkWindowSetResizable (boolean resizable);
  73:   native void gtkWindowSetModal (boolean modal);
  74:   native void gtkWindowSetAlwaysOnTop ( boolean alwaysOnTop );
  75:   native boolean gtkWindowHasFocus();
  76:   native void realize ();
  77: 
  78:   /** Returns the cached width of the AWT window component. */
  79:   int getX ()
  80:   {
  81:     return x;
  82:   }
  83: 
  84:   /** Returns the cached width of the AWT window component. */
  85:   int getY ()
  86:   {
  87:     return y;
  88:   }
  89: 
  90:   /** Returns the cached width of the AWT window component. */
  91:   int getWidth ()
  92:   {
  93:     return width;
  94:   }
  95: 
  96:   /** Returns the cached height of the AWT window component. */
  97:   int getHeight ()
  98:   {
  99:     return height;
 100:   }
 101: 
 102:   native void create (int type, boolean decorated, GtkWindowPeer parent);
 103: 
 104:   void create (int type, boolean decorated)
 105:   {
 106:     Window window = (Window) awtComponent;
 107:     GtkWindowPeer parent_peer = null;
 108:     Component parent = awtComponent.getParent();
 109:     x = awtComponent.getX();
 110:     y = awtComponent.getY();
 111:     height = awtComponent.getHeight();
 112:     width = awtComponent.getWidth();
 113:     
 114:     if (!window.isFocusableWindow())
 115:       type = GDK_WINDOW_TYPE_HINT_MENU;
 116:     
 117:     if (parent != null)
 118:       parent_peer = (GtkWindowPeer) awtComponent.getParent().getPeer();
 119:     
 120:     create (type, decorated, parent_peer);
 121:   }
 122: 
 123:   void create ()
 124:   {
 125:     // Create a normal undecorated window.
 126:     create (GDK_WINDOW_TYPE_HINT_NORMAL, false);
 127:   }
 128: 
 129:   void setParent ()
 130:   {
 131:     setVisible (awtComponent.isVisible ());
 132:     setEnabled (awtComponent.isEnabled ());
 133:   }
 134: 
 135:   void setVisibleAndEnabled ()
 136:   {
 137:   }
 138: 
 139:   public native void setVisibleNative (boolean b);
 140:   public native void setVisibleNativeUnlocked (boolean b);
 141: 
 142:   native void connectSignals ();
 143: 
 144:   public GtkWindowPeer (Window window)
 145:   {
 146:     super (window);
 147:   }
 148: 
 149:   public native void toBack();
 150:   public native void toFront();
 151: 
 152:   native void nativeSetBounds (int x, int y, int width, int height);
 153:   native void nativeSetBoundsUnlocked (int x, int y, int width, int height);
 154:   native void nativeSetLocation (int x, int y);
 155:   native void nativeSetLocationUnlocked (int x, int y);
 156: 
 157:   // Called from show.
 158:   protected void setLocation (int x, int y)
 159:   {
 160:     nativeSetLocation (x, y);
 161:   }
 162: 
 163:   public void setBounds (int x, int y, int width, int height)
 164:   {
 165:     if (x != getX()
 166:     || y != getY()
 167:     || width != getWidth()
 168:     || height != getHeight())
 169:       {
 170:     this.x = x;
 171:     this.y = y;
 172:     this.width = width;
 173:     this.height = height;
 174:     
 175:     nativeSetBounds (x, y,
 176:              width - insets.left - insets.right,
 177:              height - insets.top - insets.bottom);
 178:       }
 179:   }
 180: 
 181:   public void setTitle (String title)
 182:   {
 183:     gtkWindowSetTitle (title);
 184:   }
 185: 
 186:   // Called from setResizable
 187:   protected native void setSize (int width, int height);
 188:   
 189:   /**
 190:    * Needed by both GtkFramePeer and GtkDialogPeer subclasses, so
 191:    * implemented here. But never actually called on a GtkWindowPeer
 192:    * itself.
 193:    */
 194:   public void setResizable (boolean resizable)
 195:   {
 196:     // Call setSize; otherwise when resizable is changed from true to
 197:     // false the window will shrink to the dimensions it had before it
 198:     // was resizable.
 199:     x = awtComponent.getX();
 200:     y = awtComponent.getY();
 201:     width = awtComponent.getWidth();
 202:     height = awtComponent.getHeight();
 203:     setSize (width - insets.left - insets.right,
 204:          height - insets.top - insets.bottom);
 205:     gtkWindowSetResizable (resizable);
 206:   }
 207: 
 208:   protected void postInsetsChangedEvent (int top, int left,
 209:                      int bottom, int right)
 210:   {
 211:     insets.top = top;
 212:     insets.left = left;
 213:     insets.bottom = bottom;
 214:     insets.right = right;
 215:   }
 216: 
 217:   // called back by native side: window_configure_cb
 218:   // only called from GTK thread
 219:   protected void postConfigureEvent (int x, int y, int width, int height)
 220:   {
 221:     int frame_width = width + insets.left + insets.right;
 222:     int frame_height = height + insets.top + insets.bottom;
 223: 
 224:     if (frame_width != getWidth()
 225:     || frame_height != getHeight())
 226:       {
 227:     this.width = frame_width;
 228:     this.height = frame_height;
 229:     q().postEvent(new ComponentEvent(awtComponent,
 230:                      ComponentEvent.COMPONENT_RESIZED));
 231:       }
 232: 
 233:     int frame_x = x - insets.left;
 234:     int frame_y = y - insets.top;
 235: 
 236:     if (frame_x != getX()
 237:     || frame_y != getY())
 238:       {
 239:     this.x = frame_x;
 240:     this.y = frame_y;
 241:     q().postEvent(new ComponentEvent(awtComponent,
 242:                      ComponentEvent.COMPONENT_MOVED));
 243:       }
 244:   }
 245: 
 246:   public void show ()
 247:   {
 248:     x = awtComponent.getX();
 249:     y = awtComponent.getY();
 250:     width = awtComponent.getWidth();
 251:     height = awtComponent.getHeight();
 252:     setLocation(x, y);
 253:     setVisible (true);
 254:   }
 255: 
 256:   void postWindowEvent (int id, Window opposite, int newState)
 257:   {
 258:     if (id == WindowEvent.WINDOW_OPENED)
 259:       {
 260:     // Post a WINDOW_OPENED event the first time this window is shown.
 261:     if (!hasBeenShown)
 262:       {
 263:         q().postEvent (new WindowEvent ((Window) awtComponent, id,
 264:                       opposite));
 265:         hasBeenShown = true;
 266:       }
 267:       }
 268:     else if (id == WindowEvent.WINDOW_STATE_CHANGED)
 269:       {
 270:     if (oldState != newState)
 271:       {
 272:         q().postEvent (new WindowEvent ((Window) awtComponent, id, opposite,
 273:                       oldState, newState));
 274:         oldState = newState;
 275:       }
 276:       }
 277:     else
 278:       q().postEvent (new WindowEvent ((Window) awtComponent, id, opposite));
 279:   }
 280: 
 281:   /**
 282:    * Update the always-on-top status of the native window.
 283:    */
 284:   public void updateAlwaysOnTop()
 285:   {
 286:     gtkWindowSetAlwaysOnTop( ((Window)awtComponent).isAlwaysOnTop() );
 287:   }
 288: 
 289:   protected void postExposeEvent (int x, int y, int width, int height)
 290:   {
 291:     // Translate GTK co-ordinates, which do not include a window
 292:     // frame's insets, to AWT co-ordinates, which do include a window
 293:     // frame's insets.  GtkWindowPeer should always have all-zero
 294:     // insets but GtkFramePeer and GtkDialogPeer insets will be
 295:     // non-zero.
 296:     q().postEvent (new PaintEvent (awtComponent, PaintEvent.PAINT,
 297:                                    new Rectangle (x + insets.left, 
 298:                                                   y + insets.top, 
 299:                                                   width, height)));
 300:   }
 301: 
 302:   public boolean requestWindowFocus()
 303:   {
 304:     // TODO Auto-generated method stub
 305:     return false;
 306:   }
 307: 
 308:   public boolean requestFocus (Component request, boolean temporary, 
 309:                                boolean allowWindowFocus, long time)
 310:   {
 311:     assert request == awtComponent || isLightweightDescendant(request);
 312:     boolean retval = false;
 313:     if (gtkWindowHasFocus())
 314:       {
 315:         KeyboardFocusManager kfm =
 316:           KeyboardFocusManager.getCurrentKeyboardFocusManager();
 317:         Component currentFocus = kfm.getFocusOwner();
 318:         if (currentFocus == request)
 319:           // Nothing to do in this trivial case.
 320:           retval = true;
 321:         else
 322:           {
 323:             // Requested component is a lightweight descendant of this one
 324:             // or the actual heavyweight.
 325:             // Since this (native) component is already focused, we simply
 326:             // change the actual focus and be done.
 327:             postFocusEvent(FocusEvent.FOCUS_GAINED, temporary);
 328:             retval = true;
 329:           }
 330:       }
 331:     else
 332:       {
 333:         if (allowWindowFocus)
 334:           {
 335:             retval = requestWindowFocus();
 336:           }
 337:       }
 338:     return retval;
 339:   }
 340: 
 341:   public Graphics getGraphics ()
 342:   {
 343:     Graphics g = super.getGraphics ();
 344:     // Translate AWT co-ordinates, which include a window frame's
 345:     // insets, to GTK co-ordinates, which do not include a window
 346:     // frame's insets.  GtkWindowPeer should always have all-zero
 347:     // insets but GtkFramePeer and GtkDialogPeer insets will be
 348:     // non-zero.
 349:     g.translate (-insets.left, -insets.top);
 350:     return g;
 351:   }
 352: 
 353:   protected void updateComponent (PaintEvent event)
 354:   {
 355:     // Do not clear anything before painting.  Sun never calls
 356:     // Window.update, only Window.paint.
 357:     paintComponent(event);
 358:   }
 359: 
 360:   protected void postMouseEvent(int id, long when, int mods, int x, int y, 
 361:                 int clickCount, boolean popupTrigger)
 362:   {
 363:     // Translate AWT co-ordinates, which include a window frame's
 364:     // insets, to GTK co-ordinates, which do not include a window
 365:     // frame's insets.  GtkWindowPeer should always have all-zero
 366:     // insets but GtkFramePeer and GtkDialogPeer insets will be
 367:     // non-zero.
 368:     super.postMouseEvent (id, when, mods, 
 369:               x + insets.left, y + insets.top, 
 370:               clickCount, popupTrigger);
 371:   }
 372: 
 373:   // We override this to keep it in sync with our internal
 374:   // representation.
 375:   public Rectangle getBounds()
 376:   {
 377:     return new Rectangle(x, y, width, height);
 378:   }
 379: }