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

   1: /* GtkTextAreaPeer.java -- Implements TextAreaPeer 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.Dimension;
  42: import java.awt.Font;
  43: import java.awt.FontMetrics;
  44: import java.awt.Rectangle;
  45: import java.awt.TextArea;
  46: import java.awt.im.InputMethodRequests;
  47: import java.awt.peer.TextAreaPeer;
  48: import java.awt.peer.TextComponentPeer;
  49: 
  50: public class GtkTextAreaPeer extends GtkComponentPeer
  51:   implements TextComponentPeer, TextAreaPeer
  52: {
  53:   private static transient int DEFAULT_ROWS = 10;
  54:   private static transient int DEFAULT_COLS = 80;
  55: 
  56:   native void create (int width, int height, int scrollbarVisibility);
  57: 
  58:   native void gtkWidgetModifyFont (String name, int style, int size);
  59:   native void gtkWidgetRequestFocus ();
  60: 
  61:   public native void connectSignals ();
  62: 
  63:   public native int getCaretPosition ();
  64:   public native void setCaretPosition (int pos);
  65:   public native int getSelectionStart ();
  66:   public native int getSelectionEnd ();
  67:   public native String getText ();
  68:   public native void select (int start, int end);
  69:   public native void setEditable (boolean state);
  70:   public native void setText (String text);
  71: 
  72:   public int getIndexAtPoint(int x, int y)
  73:   {
  74:     // FIXME
  75:     return 0;
  76:   }
  77: 
  78:   public Rectangle getCharacterBounds (int pos)
  79:   {
  80:     // FIXME
  81:     return null;
  82:   }
  83: 
  84:   public long filterEvents (long filter)
  85:   {
  86:     // FIXME
  87:     return filter;
  88:   }
  89: 
  90:   void create ()
  91:   {
  92:     Font f = awtComponent.getFont ();
  93: 
  94:     // By default, Sun sets a TextArea's font when its peer is
  95:     // created.  If f != null then the peer's font is set by
  96:     // GtkComponent.create.
  97:     if (f == null)
  98:       {
  99:     f = new Font ("Dialog", Font.PLAIN, 12);
 100:     awtComponent.setFont (f);
 101:       }
 102: 
 103:     FontMetrics fm = getFontMetrics (f);
 104: 
 105:     TextArea ta = ((TextArea) awtComponent);
 106:     int sizeRows = ta.getRows ();
 107:     int sizeCols = ta.getColumns ();
 108: 
 109:     sizeRows = sizeRows == 0 ? DEFAULT_ROWS : sizeRows;
 110:     sizeCols = sizeCols == 0 ? DEFAULT_COLS : sizeCols;
 111: 
 112:     int width = sizeCols * fm.getMaxAdvance ();
 113:     int height = sizeRows * (fm.getMaxDescent () + fm.getMaxAscent ());
 114: 
 115:     create (width, height, ta.getScrollbarVisibility ());
 116:     setEditable (ta.isEditable ());
 117:   }
 118: 
 119:   public GtkTextAreaPeer (TextArea ta)
 120:   {
 121:     super (ta);
 122: 
 123:     setText (ta.getText ());
 124:     setCaretPosition (0);
 125:   }
 126: 
 127:   public native void insert (String str, int pos);
 128:   public native void replaceRange (String str, int start, int end);
 129: 
 130:   public Dimension getMinimumSize (int rows, int cols)
 131:   {
 132:     return minimumSize (rows == 0 ? DEFAULT_ROWS : rows,
 133:                         cols == 0 ? DEFAULT_COLS : cols);
 134:   }
 135: 
 136:   public Dimension getPreferredSize (int rows, int cols)
 137:   {
 138:     return preferredSize (rows == 0 ? DEFAULT_ROWS : rows,
 139:                           cols == 0 ? DEFAULT_COLS : cols);
 140:   }
 141: 
 142:   native int getHScrollbarHeight ();
 143:   native int getVScrollbarWidth ();
 144: 
 145:   // Deprecated
 146:   public Dimension minimumSize (int rows, int cols)
 147:   {
 148:     TextArea ta = ((TextArea) awtComponent);
 149:     int height = 0;
 150:     int width = 0;
 151: 
 152:     if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH
 153:     || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_HORIZONTAL_ONLY)
 154:       height = getHScrollbarHeight ();
 155: 
 156:     if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH
 157:     || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_VERTICAL_ONLY)
 158:       width = getVScrollbarWidth ();
 159: 
 160:     Font f = awtComponent.getFont ();
 161:     if (f == null)
 162:       return new Dimension (width, height);
 163: 
 164:     FontMetrics fm = getFontMetrics (f);
 165: 
 166:     int sizeRows = rows == 0 ? DEFAULT_ROWS : rows;
 167:     int sizeCols = cols == 0 ? DEFAULT_COLS : cols;
 168: 
 169:     width += sizeCols * fm.getMaxAdvance ();
 170:     height += sizeRows * (fm.getMaxDescent () + fm.getMaxAscent ());
 171: 
 172:     return new Dimension (width, height);
 173:   }
 174: 
 175:   public Dimension preferredSize (int rows, int cols)
 176:   {
 177:     TextArea ta = ((TextArea) awtComponent);
 178:     int height = 0;
 179:     int width = 0;
 180: 
 181:     if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH
 182:     || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_HORIZONTAL_ONLY)
 183:       height = getHScrollbarHeight ();
 184: 
 185:     if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH
 186:     || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_VERTICAL_ONLY)
 187:       width = getVScrollbarWidth ();
 188: 
 189:     Font f = awtComponent.getFont ();
 190:     if (f == null)
 191:       return new Dimension (width, height);
 192: 
 193:     FontMetrics fm = getFontMetrics (f);
 194: 
 195:     int sizeRows = rows == 0 ? DEFAULT_ROWS : rows;
 196:     int sizeCols = cols == 0 ? DEFAULT_COLS : cols;
 197: 
 198:     width += sizeCols * fm.getMaxAdvance ();
 199:     height += sizeRows * (fm.getMaxDescent () + fm.getMaxAscent ());
 200: 
 201:     return new Dimension (width, height);
 202:   }
 203: 
 204:   public void replaceText (String str, int start, int end)
 205:   {
 206:     replaceRange (str, start, end);
 207:   }
 208: 
 209:   public void insertText (String str, int pos)
 210:   {
 211:     insert (str, pos);
 212:   }
 213: 
 214:   public InputMethodRequests getInputMethodRequests()
 215:   {
 216:       // FIXME: implement
 217:     return null;
 218:   }
 219: }