summaryrefslogtreecommitdiffstats
path: root/awt/org/apache/harmony/awt/wtk/NativeEvent.java
blob: 1471c1a72aaaebbe0140b7a50dad7d93412c8b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Mikhail Danilov
 * @version $Revision$
 */
package org.apache.harmony.awt.wtk;

import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.event.KeyEvent;

import org.apache.harmony.awt.gl.MultiRectArea;


/**
 * The interface describing cross-platform translation of system
 * messages.
 *
 * <p/>Some messages can appear only on specific platform,
 * but they still can have cross-platform interpretation if the
 * application should be aware of them and can react using
 * cross-platform API.
 *
 */
public abstract class NativeEvent {

    /**
     * Message has no common cross-platform
     * interpretation and should be skipped.
     */
    public static final int ID_PLATFORM = 0;

    /**
     * Window bounds have changed.
     */
    public static final int ID_BOUNDS_CHANGED = -1;

    /**
     * Window decoration size has changed.
     */
    public static final int ID_INSETS_CHANGED = -2;

    /**
     * Window was just created (WM_CREATE on Windows)
     */
    public static final int ID_CREATED = -3;

    /**
     * Mouse grab was canceled by the native system
     */
    public static final int ID_MOUSE_GRAB_CANCELED = -4;

    /**
     * System color scheme or visual theme was changed
     */
    public static final int ID_THEME_CHANGED = -5;

    protected long windowId;
    protected int eventId;
    protected long otherWindowId;

    protected Point screenPos;
    protected Point localPos;
    protected Rectangle windowRect;

    protected int modifiers;
    protected int mouseButton;
    protected int wheelRotation;

    protected KeyInfo keyInfo = new KeyInfo();

    protected int windowState = -1;
    protected long time;

    /**
     * Returns the system window id of the event recipient.
     * @return HWND on Windows, xwindnow on X
     */
    public long getWindowId() {
        return windowId;
    }

    /**
     * Returns cross-platform event id
     * should be one of ID_* constants or
     * id constants from java.awt.AWTEvent subclasess
     * @return cross-platform event id
     */
    public int getEventId() {
        return eventId;
    }

    /**
     * Returns the position of cursor when event occured relative to
     * top-left corner of recipient window
     * @return position of cursor in local coordinates
     */
    public Point getLocalPos() {
        return localPos;
    }

    /**
     * Returns the position of cursor when event occured
     * in screen coordinates.
     * @return position of cursor in screen coordinates
     */
    public Point getScreenPos() {
        return screenPos;
    }

    /**
     * The recipient window bounds when the event occured
     * @return window bounds
     */
    public Rectangle getWindowRect() {
        return windowRect;
    }

    /**
     * Returns the state of keyboard and mouse buttons when the event
     * occured if event from mouse or keyboard, for other events can
     * return junk values. The value is bitwise OR of
     * java.awt.event.InputEvent *_DOWN constants.
     *
     * Method is aware of system mouse button swap for left-hand
     * mouse and return swapped values.
     * @return bitwise OR of java.awt.event.InputEvent *_DOWN constants
     */
    public int getInputModifiers() {
        return modifiers;
    }

    /**
     * Returns the iconified/maximized state of recipient window if
     * event is state related, for other events can junk values.
     * The value has the same meaning as Frame.getExtendedState
     * It's bitwise OR of ICONIFIED, MAXIMIZED_HORIZ, MAXIMIZED_VERT
     * @return bitwise OR of ICONIFIED, MAXIMIZED_HORIZ, MAXIMIZED_VERT
     */
    public int getWindowState() {
        return windowState;
    }

    /**
     * The same meaning as java.awt.event.getKeyCode
     * @return java.awt.event VK_* constant
     */
    public int getVKey() {
        return (keyInfo != null) ? keyInfo.vKey : KeyInfo.DEFAULT_VKEY;
    }

    /**
     * The same meaning as java.awt.event.getKeyLocation
     * @return java.awt.event KEY_LOCATION_* constant
     */
    public int getKeyLocation() {
        return (keyInfo != null) ? keyInfo.keyLocation : KeyInfo.DEFAULT_LOCATION;
    }

    /**
     * Return the string of characters associated with the event
     * Has meaning only for KEY_PRESSED as should be translated to
     * serie of KEY_TYPED events. For dead keys and input methods
     * one key press can generate multiple key chars.
     * @return string of characters
     */
    public StringBuffer getKeyChars() {
        if (keyInfo == null) {
            return null;
        }
        if (keyInfo.vKey == KeyEvent.VK_ENTER) {
            keyInfo.keyChars.setLength(0);
            keyInfo.setKeyChars('\n');
        }
        return keyInfo.keyChars;
    }

    public char getLastChar() {
        if (keyInfo == null || keyInfo.keyChars.length() == 0) {
            return KeyEvent.CHAR_UNDEFINED;
        }
        return keyInfo.keyChars.charAt(keyInfo.keyChars.length()-1);
    }

    /**
     * Returns the number of mouse button which changed it's state,
     * otherwise 0.
     * Left button is 1, middle button is 2, right button is 3.
     *
     * Method is aware of system mouse button swap for left-hand
     * mouse and return swapped values.
     * @return mouse button number
     */
    public int getMouseButton() {
        return mouseButton;
    }

    /**
     * Returns time when the message was received
     * @return time in milliseconds
     */
    public long getTime() {
        return time;
    }

    /**
     * For the focus event contains the oposite window.
     * This means it lost focus if recipient gains it,
     * or will gain focus if recipient looses it.
     * @return HWND on Windows, xwindnow on X
     */
    public long getOtherWindowId() {
        return otherWindowId;
    }

    /**
     * Returns the "dirty" area of the window as set of non-intersecting
     * rectangles. This area is to be painted.
     * @return non-empty array of null if empty
     */
    public abstract MultiRectArea getClipRects();

    /**
     * Returns the "dirty" area of the window as one rectangle.
     * This area is to be painted.
     * @return non-null Rectangle
     */
    public abstract Rectangle getClipBounds();

    /**
     * Returns the window insets. Insets is area which belongs to
     * window somehow but is outside of it's client area,
     * it usually contains system provided border and titlebar.
     * @return non-null java.awt.Insets
     */
    public abstract Insets getInsets();

    /**
     * Returns true if event is popup menu trigger.
     * @return boolean flag
     */
    public abstract boolean getTrigger();

    /**
     * Returns the number of "clicks" the mouse wheel was rotated.
     * @return negative values if the mouse wheel was rotated up/away from the user,
     * and positive values if the mouse wheel was rotated down/ towards the user
     */
    public int getWheelRotation() {
        return wheelRotation;
    }
}