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