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

   1: /* GtkTextFieldPeer.java -- Implements TextFieldPeer with GTK
   2:    Copyright (C) 1998, 1999, 2002 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.AWTEvent;
  42: import java.awt.Dimension;
  43: import java.awt.Font;
  44: import java.awt.FontMetrics;
  45: import java.awt.Rectangle;
  46: import java.awt.TextField;
  47: import java.awt.event.KeyEvent;
  48: import java.awt.im.InputMethodRequests;
  49: import java.awt.peer.TextFieldPeer;
  50: import java.awt.peer.TextComponentPeer;
  51: 
  52: public class GtkTextFieldPeer extends GtkComponentPeer
  53:   implements TextComponentPeer, TextFieldPeer
  54: {
  55:   native void create (int width);
  56:   native void gtkWidgetSetBackground (int red, int green, int blue);
  57:   native void gtkWidgetSetForeground (int red, int green, int blue);
  58: 
  59:   public native void connectSignals ();
  60: 
  61:   public native int getCaretPosition ();
  62:   public native void setCaretPosition (int pos);
  63:   public native int getSelectionStart ();
  64:   public native int getSelectionEnd ();
  65:   public native String getText ();
  66:   public native void select (int start, int end);
  67:   public native void setEditable (boolean state);
  68:   public native void setText (String text);
  69: 
  70:   public int getIndexAtPoint(int x, int y)
  71:   {
  72:     // FIXME
  73:     return 0;
  74:   }
  75: 
  76:   public Rectangle getCharacterBounds (int pos)
  77:   {
  78:     // FIXME
  79:     return null;
  80:   }
  81: 
  82:   public long filterEvents (long filter)
  83:   {
  84:     // FIXME
  85:     return filter;
  86:   }
  87: 
  88:   void create ()
  89:   {
  90:     Font f = awtComponent.getFont ();
  91: 
  92:     // By default, Sun sets a TextField's font when its peer is
  93:     // created.  If f != null then the peer's font is set by
  94:     // GtkComponent.create.
  95:     if (f == null)
  96:       {
  97:         f = new Font ("Dialog", Font.PLAIN, 12);
  98:         awtComponent.setFont (f);
  99:       }
 100: 
 101:     FontMetrics fm = getFontMetrics (f);
 102: 
 103:     TextField tf = ((TextField) awtComponent);
 104:     int cols = tf.getColumns ();
 105: 
 106:     int text_width = cols * fm.getMaxAdvance ();
 107: 
 108:     create (text_width);
 109: 
 110:     setEditable (tf.isEditable ());
 111:   }
 112: 
 113:   native int gtkEntryGetBorderWidth ();
 114: 
 115:   public GtkTextFieldPeer (TextField tf)
 116:   {
 117:     super (tf);
 118: 
 119:     setText (tf.getText ());
 120:     setCaretPosition (0);
 121: 
 122:     if (tf.echoCharIsSet ())
 123:       setEchoChar (tf.getEchoChar ());
 124:   }
 125: 
 126:   public Dimension getMinimumSize (int cols)
 127:   {
 128:     return minimumSize (cols);
 129:   }
 130: 
 131:   public Dimension getPreferredSize (int cols)
 132:   {
 133:     return preferredSize (cols);
 134:   }
 135: 
 136:   public native void setEchoChar (char c);
 137: 
 138:   // Deprecated
 139:   public Dimension minimumSize (int cols)
 140:   {
 141:     int dim[] = new int[2];
 142: 
 143:     gtkWidgetGetPreferredDimensions (dim);
 144: 
 145:     Font f = awtComponent.getFont ();
 146:     if (f == null)
 147:       return new Dimension (2 * gtkEntryGetBorderWidth (), dim[1]);
 148: 
 149:     FontMetrics fm = getFontMetrics (f);
 150: 
 151:     int text_width = cols * fm.getMaxAdvance ();
 152: 
 153:     int width = text_width + 2 * gtkEntryGetBorderWidth ();
 154: 
 155:     return new Dimension (width, dim[1]);
 156:   }
 157: 
 158:   public Dimension preferredSize (int cols)
 159:   {
 160:     int dim[] = new int[2];
 161: 
 162:     gtkWidgetGetPreferredDimensions (dim);
 163: 
 164:     Font f = awtComponent.getFont ();
 165:     if (f == null)
 166:       return new Dimension (2 * gtkEntryGetBorderWidth (), dim[1]);
 167: 
 168:     FontMetrics fm = getFontMetrics (f);
 169: 
 170:     int text_width = cols * fm.getMaxAdvance ();
 171: 
 172:     int width = text_width + 2 * gtkEntryGetBorderWidth ();
 173: 
 174:     return new Dimension (width, dim[1]);
 175:   }
 176: 
 177:   public void setEchoCharacter (char c)
 178:   {
 179:     setEchoChar (c);
 180:   }
 181: 
 182:   public void handleEvent (AWTEvent e)
 183:   {
 184:     if (e.getID () == KeyEvent.KEY_PRESSED)
 185:       {
 186:         KeyEvent ke = (KeyEvent) e;
 187: 
 188:         if (!ke.isConsumed ()
 189:             && ke.getKeyCode () == KeyEvent.VK_ENTER)
 190:           postActionEvent (getText (), ke.getModifiersEx ());
 191:       }
 192: 
 193:     super.handleEvent (e);
 194:   }
 195:   public InputMethodRequests getInputMethodRequests()
 196:   {
 197:       // FIXME: implement
 198:     return null;
 199:   }
 200: }