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

   1: /* GtkFramePeer.java -- Implements FramePeer with GTK
   2:    Copyright (C) 1999, 2002, 2004, 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.Frame;
  42: import java.awt.Image;
  43: import java.awt.MenuBar;
  44: import java.awt.Rectangle;
  45: import java.awt.peer.FramePeer;
  46: import java.awt.peer.MenuBarPeer;
  47: 
  48: public class GtkFramePeer extends GtkWindowPeer
  49:     implements FramePeer
  50: {
  51:   private int menuBarHeight;
  52:   private MenuBarPeer menuBar;
  53:   native int getMenuBarHeight (MenuBarPeer bar);
  54:   native void setMenuBarWidthUnlocked (MenuBarPeer bar, int width);
  55:   native void setMenuBarWidth (MenuBarPeer bar, int width);
  56:   native void setMenuBarPeer (MenuBarPeer bar);
  57:   native void removeMenuBarPeer ();
  58:   native void gtkFixedSetVisible (boolean visible);
  59: 
  60:   int getMenuBarHeight ()
  61:   {
  62:     return menuBar == null ? 0 : getMenuBarHeight (menuBar);
  63:   }
  64: 
  65:   public void setMenuBar (MenuBar bar)
  66:   {
  67:     if (bar == null && menuBar != null)
  68:       {
  69:         // We're removing the menubar.
  70:         gtkFixedSetVisible (false);
  71:         menuBar = null;
  72:         removeMenuBarPeer ();
  73:         insets.top -= menuBarHeight;
  74:         menuBarHeight = 0;
  75:         // if component has already been validated, we need to revalidate.
  76:         // otherwise, it will be validated when it is shown.
  77:         if (awtComponent.isValid())
  78:           awtComponent.validate ();
  79:         gtkFixedSetVisible (true);
  80:       }
  81:     else if (bar != null && menuBar == null)
  82:       {
  83:         // We're adding a menubar where there was no menubar before.
  84:         gtkFixedSetVisible (false);
  85:         menuBar = (MenuBarPeer) ((MenuBar) bar).getPeer();
  86:         setMenuBarPeer (menuBar);
  87:         int menuBarWidth =
  88:           awtComponent.getWidth () - insets.left - insets.right;
  89:         if (menuBarWidth > 0)
  90:           setMenuBarWidth (menuBar, menuBarWidth);
  91:         menuBarHeight = getMenuBarHeight ();
  92:         insets.top += menuBarHeight;
  93:         // if component has already been validated, we need to revalidate.
  94:         // otherwise, it will be validated when it is shown.
  95:         if (awtComponent.isValid())
  96:           awtComponent.validate ();
  97:         gtkFixedSetVisible (true);
  98:       }
  99:     else if (bar != null && menuBar != null)
 100:       {
 101:         // We're swapping the menubar.
 102:         gtkFixedSetVisible (false);
 103:         removeMenuBarPeer();
 104:         int oldHeight = menuBarHeight;
 105:         int menuBarWidth =
 106:           awtComponent.getWidth () - insets.left - insets.right;
 107:         menuBar = (MenuBarPeer) ((MenuBar) bar).getPeer ();
 108:         setMenuBarPeer (menuBar);
 109:         if (menuBarWidth > 0)
 110:           setMenuBarWidth (menuBar, menuBarWidth);
 111:         menuBarHeight = getMenuBarHeight ();
 112:         if (oldHeight != menuBarHeight)
 113:           {
 114:             insets.top += (menuBarHeight - oldHeight);
 115:             awtComponent.validate ();
 116:           }
 117:         gtkFixedSetVisible (true);
 118:       }
 119:   }
 120: 
 121:   public void setBounds (int x, int y, int width, int height)
 122:   {
 123:     int menuBarWidth = width - insets.left - insets.right;
 124:     if (menuBar != null && menuBarWidth > 0)
 125:       setMenuBarWidth (menuBar, menuBarWidth);
 126: 
 127:     super.setBounds(x, y, width, height + menuBarHeight);
 128:   }  
 129:   
 130:   public void setResizable (boolean resizable)
 131:   {
 132:     // Call setSize; otherwise when resizable is changed from true to
 133:     // false the frame will shrink to the dimensions it had before it
 134:     // was resizable.
 135:     setSize (awtComponent.getWidth() - insets.left - insets.right,
 136:              awtComponent.getHeight() - insets.top - insets.bottom
 137:              + menuBarHeight);
 138:     gtkWindowSetResizable (resizable);
 139:   }
 140: 
 141:   protected void postInsetsChangedEvent (int top, int left,
 142:                      int bottom, int right)
 143:   {
 144:     insets.top = top + menuBarHeight;
 145:     insets.left = left;
 146:     insets.bottom = bottom;
 147:     insets.right = right;
 148:   }
 149: 
 150:   public GtkFramePeer (Frame frame)
 151:   {
 152:     super (frame);
 153:   }
 154: 
 155:   void create ()
 156:   {
 157:     // Create a normal decorated window.
 158:     create (GDK_WINDOW_TYPE_HINT_NORMAL,
 159:             !((Frame) awtComponent).isUndecorated ());
 160: 
 161:     Frame frame = (Frame) awtComponent;
 162: 
 163:     setMenuBar (frame.getMenuBar ());
 164: 
 165:     setTitle (frame.getTitle ());
 166:     gtkWindowSetResizable (frame.isResizable ());
 167:     setIconImage(frame.getIconImage());
 168:   }
 169: 
 170:   native void nativeSetIconImage (GtkImage image);
 171: 
 172:   public void setIconImage (Image image) 
 173:   {
 174:       if (image != null)
 175:     {
 176:       if (image instanceof GtkImage)
 177:         nativeSetIconImage((GtkImage) image);
 178:       else
 179:         nativeSetIconImage(new GtkImage(image.getSource()));
 180:     }
 181:   }
 182: 
 183:   protected void postConfigureEvent (int x, int y, int width, int height)
 184:   {
 185:     if (menuBar != null && width > 0)
 186:       setMenuBarWidthUnlocked (menuBar, width);
 187: 
 188:     // Since insets.top already includes the MenuBar's height, we need
 189:     // to subtract the MenuBar's height from the top inset.
 190:     int frame_height = height - menuBarHeight;
 191: 
 192:     // Likewise, since insets.top includes the MenuBar height, we need
 193:     // to add back the MenuBar height to the frame's y position.  If
 194:     // no MenuBar exists in this frame, the MenuBar height will be 0.
 195:     int frame_y = y + menuBarHeight;
 196: 
 197:     super.postConfigureEvent(x, frame_y, width, frame_height);
 198:   }
 199: 
 200:   public int getState ()
 201:   {
 202:     return 0;
 203:   }
 204: 
 205:   public void setState (int state)
 206:   {
 207: 
 208:   }
 209: 
 210:   public void setMaximizedBounds (Rectangle r)
 211:   {
 212: 
 213:   }
 214:   public void setBoundsPrivate(int x, int y, int width, int height)
 215:   {
 216:     // TODO Auto-generated method stub
 217:     
 218:   }
 219: 
 220:   public boolean requestWindowFocus()
 221:   {
 222:     // TODO Auto-generated method stub
 223:     return false;
 224:   }
 225: }
 226: