📄 gtkcomponentpeer.java
字号:
/* GtkComponentPeer.java -- Implements ComponentPeer with GTK Copyright (C) 1998, 1999, 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.java.awt.peer.gtk;import java.awt.AWTEvent;import java.awt.AWTException;import java.awt.BufferCapabilities;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.Image;import java.awt.Insets;import java.awt.ItemSelectable;import java.awt.Point;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.Window;import java.awt.event.FocusEvent;import java.awt.event.ItemEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.PaintEvent;import java.awt.event.TextEvent;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.ImageObserver;import java.awt.image.ImageProducer;import java.awt.image.VolatileImage;import java.awt.peer.ComponentPeer;import java.awt.peer.ContainerPeer;import java.awt.peer.WindowPeer;import java.util.Timer;import java.util.TimerTask;public class GtkComponentPeer extends GtkGenericPeer implements ComponentPeer{ VolatileImage backBuffer; BufferCapabilities caps; Component awtComponent; Insets insets; boolean isInRepaint; static final Timer repaintTimer = new Timer (true); /* this isEnabled differs from Component.isEnabled, in that it knows if a parent is disabled. In that case Component.isEnabled may return true, but our isEnabled will always return false */ native boolean isEnabled (); static native boolean modalHasGrab(); native int[] gtkWidgetGetForeground (); native int[] gtkWidgetGetBackground (); native void gtkWidgetGetDimensions (int[] dim); native void gtkWidgetGetPreferredDimensions (int[] dim); native void gtkWindowGetLocationOnScreen (int[] point); native void gtkWidgetGetLocationOnScreen (int[] point); native void gtkWidgetSetCursor (int type); native void gtkWidgetSetCursorUnlocked (int type); native void gtkWidgetSetBackground (int red, int green, int blue); native void gtkWidgetSetForeground (int red, int green, int blue); native void gtkWidgetSetSensitive (boolean sensitive); native void gtkWidgetSetParent (ComponentPeer parent); native void gtkWidgetRequestFocus (); native void gtkWidgetDispatchKeyEvent (int id, long when, int mods, int keyCode, int keyLocation); native boolean isRealized (); void realize () { // Default implementation does nothing } native void setNativeEventMask (); void create () { throw new RuntimeException (); } native void connectSignals (); protected GtkComponentPeer (Component awtComponent) { super (awtComponent); this.awtComponent = awtComponent; insets = new Insets (0, 0, 0, 0); create (); connectSignals (); if (awtComponent.getForeground () != null) setForeground (awtComponent.getForeground ()); if (awtComponent.getBackground () != null) setBackground (awtComponent.getBackground ()); if (awtComponent.getFont() != null) setFont(awtComponent.getFont()); Component parent = awtComponent.getParent (); // Only set our parent on the GTK side if our parent on the AWT // side is not showing. Otherwise the gtk peer will be shown // before we've had a chance to position and size it properly. if (awtComponent instanceof Window || (parent != null && ! parent.isShowing ())) setParentAndBounds (); setNativeEventMask (); realize (); } void setParentAndBounds () { setParent (); setComponentBounds (); setVisibleAndEnabled (); } void setParent () { ComponentPeer p; Component component = awtComponent; do { component = component.getParent (); p = component.getPeer (); } while (p instanceof java.awt.peer.LightweightPeer); if (p != null) gtkWidgetSetParent (p); } void beginNativeRepaint () { isInRepaint = true; } void endNativeRepaint () { isInRepaint = false; } /* * Set the bounds of this peer's AWT Component based on dimensions * returned by the native windowing system. Most Components impose * their dimensions on the peers which is what the default * implementation does. However some peers, like GtkFileDialogPeer, * need to pass their size back to the AWT Component. */ void setComponentBounds () { Rectangle bounds = awtComponent.getBounds (); if (bounds.x == 0 && bounds.y == 0 && bounds.width == 0 && bounds.height == 0) return; setBounds (bounds.x, bounds.y, bounds.width, bounds.height); } void setVisibleAndEnabled () { setVisible (awtComponent.isVisible ()); setEnabled (awtComponent.isEnabled ()); } public int checkImage (Image image, int width, int height, ImageObserver observer) { return getToolkit().checkImage(image, width, height, observer); } public Image createImage (ImageProducer producer) { return new GtkImage (producer); } public Image createImage (int width, int height) { Image image; if (GtkToolkit.useGraphics2D ()) image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); else image = new GtkImage (width, height); Graphics g = image.getGraphics(); g.setColor(getBackground()); g.fillRect(0, 0, width, height); return image; } public void disable () { setEnabled (false); } public void enable () { setEnabled (true); } public ColorModel getColorModel () { return ColorModel.getRGBdefault (); } public FontMetrics getFontMetrics (Font font) { return getToolkit().getFontMetrics(font); } public Graphics getGraphics () { if (GtkToolkit.useGraphics2D ()) return new GdkGraphics2D (this); else return new GdkGraphics (this); } public Point getLocationOnScreen () { int point[] = new int[2]; if( this instanceof WindowPeer ) gtkWindowGetLocationOnScreen (point); else gtkWidgetGetLocationOnScreen (point); return new Point (point[0], point[1]); } public Dimension getMinimumSize () { return minimumSize (); } public Dimension getPreferredSize () { return preferredSize (); } public Toolkit getToolkit () { return Toolkit.getDefaultToolkit(); } public void handleEvent (AWTEvent event) { int id = event.getID(); KeyEvent ke = null; switch (id) { case PaintEvent.PAINT: case PaintEvent.UPDATE: { try { Graphics g = getGraphics (); // Some peers like GtkFileDialogPeer are repainted by Gtk itself if (g == null) break; g.setClip (((PaintEvent) event).getUpdateRect()); if (id == PaintEvent.PAINT) awtComponent.paint (g); else awtComponent.update (g); g.dispose (); } catch (InternalError e) { System.err.println (e); } } break; case KeyEvent.KEY_PRESSED: ke = (KeyEvent) event; gtkWidgetDispatchKeyEvent (ke.getID (), ke.getWhen (), ke.getModifiersEx (), ke.getKeyCode (), ke.getKeyLocation ()); break; case KeyEvent.KEY_RELEASED: ke = (KeyEvent) event; gtkWidgetDispatchKeyEvent (ke.getID (), ke.getWhen (), ke.getModifiersEx (), ke.getKeyCode (), ke.getKeyLocation ()); break; } } public boolean isFocusTraversable () { return true; } public Dimension minimumSize () { int dim[] = new int[2]; gtkWidgetGetPreferredDimensions (dim); return new Dimension (dim[0], dim[1]); } public void paint (Graphics g) { } public Dimension preferredSize () { int dim[] = new int[2]; gtkWidgetGetPreferredDimensions (dim); return new Dimension (dim[0], dim[1]); } public boolean prepareImage (Image image, int width, int height, ImageObserver observer) { return getToolkit().prepareImage(image, width, height, observer);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -