Home | History | Annotate | Download | only in wtk
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 /**
     18  * @author Mikhail Danilov
     19  * @version $Revision$
     20  */
     21 package org.apache.harmony.awt.wtk;
     22 
     23 import java.awt.Insets;
     24 import java.awt.Rectangle;
     25 import java.awt.Point;
     26 import java.awt.event.KeyEvent;
     27 
     28 import org.apache.harmony.awt.gl.MultiRectArea;
     29 
     30 
     31 /**
     32  * The interface describing cross-platform translation of system
     33  * messages.
     34  *
     35  * <p/>Some messages can appear only on specific platform,
     36  * but they still can have cross-platform interpretation if the
     37  * application should be aware of them and can react using
     38  * cross-platform API.
     39  *
     40  */
     41 public abstract class NativeEvent {
     42 
     43     /**
     44      * Message has no common cross-platform
     45      * interpretation and should be skipped.
     46      */
     47     public static final int ID_PLATFORM = 0;
     48 
     49     /**
     50      * Window bounds have changed.
     51      */
     52     public static final int ID_BOUNDS_CHANGED = -1;
     53 
     54     /**
     55      * Window decoration size has changed.
     56      */
     57     public static final int ID_INSETS_CHANGED = -2;
     58 
     59     /**
     60      * Window was just created (WM_CREATE on Windows)
     61      */
     62     public static final int ID_CREATED = -3;
     63 
     64     /**
     65      * Mouse grab was canceled by the native system
     66      */
     67     public static final int ID_MOUSE_GRAB_CANCELED = -4;
     68 
     69     /**
     70      * System color scheme or visual theme was changed
     71      */
     72     public static final int ID_THEME_CHANGED = -5;
     73 
     74     protected long windowId;
     75     protected int eventId;
     76     protected long otherWindowId;
     77 
     78     protected Point screenPos;
     79     protected Point localPos;
     80     protected Rectangle windowRect;
     81 
     82     protected int modifiers;
     83     protected int mouseButton;
     84     protected int wheelRotation;
     85 
     86     protected KeyInfo keyInfo = new KeyInfo();
     87 
     88     protected int windowState = -1;
     89     protected long time;
     90 
     91     /**
     92      * Returns the system window id of the event recipient.
     93      * @return HWND on Windows, xwindnow on X
     94      */
     95     public long getWindowId() {
     96         return windowId;
     97     }
     98 
     99     /**
    100      * Returns cross-platform event id
    101      * should be one of ID_* constants or
    102      * id constants from java.awt.AWTEvent subclasess
    103      * @return cross-platform event id
    104      */
    105     public int getEventId() {
    106         return eventId;
    107     }
    108 
    109     /**
    110      * Returns the position of cursor when event occured relative to
    111      * top-left corner of recipient window
    112      * @return position of cursor in local coordinates
    113      */
    114     public Point getLocalPos() {
    115         return localPos;
    116     }
    117 
    118     /**
    119      * Returns the position of cursor when event occured
    120      * in screen coordinates.
    121      * @return position of cursor in screen coordinates
    122      */
    123     public Point getScreenPos() {
    124         return screenPos;
    125     }
    126 
    127     /**
    128      * The recipient window bounds when the event occured
    129      * @return window bounds
    130      */
    131     public Rectangle getWindowRect() {
    132         return windowRect;
    133     }
    134 
    135     /**
    136      * Returns the state of keyboard and mouse buttons when the event
    137      * occured if event from mouse or keyboard, for other events can
    138      * return junk values. The value is bitwise OR of
    139      * java.awt.event.InputEvent *_DOWN constants.
    140      *
    141      * Method is aware of system mouse button swap for left-hand
    142      * mouse and return swapped values.
    143      * @return bitwise OR of java.awt.event.InputEvent *_DOWN constants
    144      */
    145     public int getInputModifiers() {
    146         return modifiers;
    147     }
    148 
    149     /**
    150      * Returns the iconified/maximized state of recipient window if
    151      * event is state related, for other events can junk values.
    152      * The value has the same meaning as Frame.getExtendedState
    153      * It's bitwise OR of ICONIFIED, MAXIMIZED_HORIZ, MAXIMIZED_VERT
    154      * @return bitwise OR of ICONIFIED, MAXIMIZED_HORIZ, MAXIMIZED_VERT
    155      */
    156     public int getWindowState() {
    157         return windowState;
    158     }
    159 
    160     /**
    161      * The same meaning as java.awt.event.getKeyCode
    162      * @return java.awt.event VK_* constant
    163      */
    164     public int getVKey() {
    165         return (keyInfo != null) ? keyInfo.vKey : KeyInfo.DEFAULT_VKEY;
    166     }
    167 
    168     /**
    169      * The same meaning as java.awt.event.getKeyLocation
    170      * @return java.awt.event KEY_LOCATION_* constant
    171      */
    172     public int getKeyLocation() {
    173         return (keyInfo != null) ? keyInfo.keyLocation : KeyInfo.DEFAULT_LOCATION;
    174     }
    175 
    176     /**
    177      * Return the string of characters associated with the event
    178      * Has meaning only for KEY_PRESSED as should be translated to
    179      * serie of KEY_TYPED events. For dead keys and input methods
    180      * one key press can generate multiple key chars.
    181      * @return string of characters
    182      */
    183     public StringBuffer getKeyChars() {
    184         if (keyInfo == null) {
    185             return null;
    186         }
    187         if (keyInfo.vKey == KeyEvent.VK_ENTER) {
    188             keyInfo.keyChars.setLength(0);
    189             keyInfo.setKeyChars('\n');
    190         }
    191         return keyInfo.keyChars;
    192     }
    193 
    194     public char getLastChar() {
    195         if (keyInfo == null || keyInfo.keyChars.length() == 0) {
    196             return KeyEvent.CHAR_UNDEFINED;
    197         }
    198         return keyInfo.keyChars.charAt(keyInfo.keyChars.length()-1);
    199     }
    200 
    201     /**
    202      * Returns the number of mouse button which changed it's state,
    203      * otherwise 0.
    204      * Left button is 1, middle button is 2, right button is 3.
    205      *
    206      * Method is aware of system mouse button swap for left-hand
    207      * mouse and return swapped values.
    208      * @return mouse button number
    209      */
    210     public int getMouseButton() {
    211         return mouseButton;
    212     }
    213 
    214     /**
    215      * Returns time when the message was received
    216      * @return time in milliseconds
    217      */
    218     public long getTime() {
    219         return time;
    220     }
    221 
    222     /**
    223      * For the focus event contains the oposite window.
    224      * This means it lost focus if recipient gains it,
    225      * or will gain focus if recipient looses it.
    226      * @return HWND on Windows, xwindnow on X
    227      */
    228     public long getOtherWindowId() {
    229         return otherWindowId;
    230     }
    231 
    232     /**
    233      * Returns the "dirty" area of the window as set of non-intersecting
    234      * rectangles. This area is to be painted.
    235      * @return non-empty array of null if empty
    236      */
    237     public abstract MultiRectArea getClipRects();
    238 
    239     /**
    240      * Returns the "dirty" area of the window as one rectangle.
    241      * This area is to be painted.
    242      * @return non-null Rectangle
    243      */
    244     public abstract Rectangle getClipBounds();
    245 
    246     /**
    247      * Returns the window insets. Insets is area which belongs to
    248      * window somehow but is outside of it's client area,
    249      * it usually contains system provided border and titlebar.
    250      * @return non-null java.awt.Insets
    251      */
    252     public abstract Insets getInsets();
    253 
    254     /**
    255      * Returns true if event is popup menu trigger.
    256      * @return boolean flag
    257      */
    258     public abstract boolean getTrigger();
    259 
    260     /**
    261      * Returns the number of "clicks" the mouse wheel was rotated.
    262      * @return negative values if the mouse wheel was rotated up/away from the user,
    263      * and positive values if the mouse wheel was rotated down/ towards the user
    264      */
    265     public int getWheelRotation() {
    266         return wheelRotation;
    267     }
    268 }
    269