forked from Bryan2333/JBR-IME-HiDPI-Fixed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idea.patch
429 lines (407 loc) · 17.6 KB
/
idea.patch
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
diff --git a/src/java.desktop/share/classes/java/awt/Container.java b/src/java.desktop/share/classes/java/awt/Container.java
index 1327b2fded9..c2ebc03fcc1 100644
--- a/src/java.desktop/share/classes/java/awt/Container.java
+++ b/src/java.desktop/share/classes/java/awt/Container.java
@@ -25,6 +25,7 @@
package java.awt;
+import java.awt.Point;
import java.awt.dnd.DropTarget;
import java.awt.event.AWTEventListener;
import java.awt.event.ComponentEvent;
@@ -682,6 +683,14 @@ public class Container extends Component {
}
}
+ /**
+ * fix fcitx position
+ * @return Point
+ */
+ public Point getPeerLocationOnScreen(){
+ return peer.getLocationOnScreen();
+ }
+
/**
* Detects whether or not remove from current parent and adding to new parent requires call of
* removeNotify on the component. Since removeNotify destroys native window this might (not)
diff --git a/src/java.desktop/share/classes/javax/swing/JTextArea.java b/src/java.desktop/share/classes/javax/swing/JTextArea.java
index ecc9342f05a..eea39c9e03a 100644
--- a/src/java.desktop/share/classes/javax/swing/JTextArea.java
+++ b/src/java.desktop/share/classes/javax/swing/JTextArea.java
@@ -562,6 +562,14 @@ public class JTextArea extends JTextComponent {
return rowHeight;
}
+ /**
+ * fix fcitx position
+ * @return FontMetrics
+ */
+ public FontMetrics getFontMetrics() {
+ return getFontMetrics(getFont());
+ }
+
/**
* Returns the number of columns in the TextArea.
*
diff --git a/src/java.desktop/share/classes/javax/swing/JTextField.java b/src/java.desktop/share/classes/javax/swing/JTextField.java
index 3abe09d0565..50dc549bdba 100644
--- a/src/java.desktop/share/classes/javax/swing/JTextField.java
+++ b/src/java.desktop/share/classes/javax/swing/JTextField.java
@@ -427,6 +427,14 @@ public class JTextField extends JTextComponent implements SwingConstants {
return columnWidth;
}
+ /**
+ * fix fcitx position
+ * @return FontMetrics
+ */
+ public FontMetrics getFontMetrics() {
+ return getFontMetrics(getFont());
+ }
+
/**
* Returns the preferred size <code>Dimensions</code> needed for this
* <code>TextField</code>. If a non-zero number of columns has been
diff --git a/src/java.desktop/share/classes/sun/awt/im/InputContext.java b/src/java.desktop/share/classes/sun/awt/im/InputContext.java
index bb955dc5089..7c42e2aa685 100644
--- a/src/java.desktop/share/classes/sun/awt/im/InputContext.java
+++ b/src/java.desktop/share/classes/sun/awt/im/InputContext.java
@@ -28,6 +28,7 @@ package sun.awt.im;
import java.awt.AWTEvent;
import java.awt.AWTKeyStroke;
import java.awt.Component;
+import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Rectangle;
@@ -39,6 +40,7 @@ import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.InputMethodEvent;
import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.im.InputMethodRequests;
@@ -54,6 +56,7 @@ import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import sun.util.logging.PlatformLogger;
import sun.awt.SunToolkit;
+import sun.awt.X11InputMethod;
/**
* This InputContext class contains parts of the implementation of
@@ -249,13 +252,23 @@ public class InputContext extends java.awt.im.InputContext
focusLost((Component) event.getSource(), ((FocusEvent) event).isTemporary());
break;
+ case MouseEvent.MOUSE_RELEASED:
+ if (((Component) event.getSource()).getCursor().getType() == Cursor.TEXT_CURSOR) {
+ transCaretPositionToXIM((Component) event.getSource());
+ break;
+ }
+
case KeyEvent.KEY_PRESSED:
- if (checkInputMethodSelectionKey((KeyEvent)event)) {
+ if (event instanceof KeyEvent && checkInputMethodSelectionKey((KeyEvent)event)) {
// pop up the input method selection menu
InputMethodManager.getInstance().notifyChangeRequestByHotKey((Component)event.getSource());
break;
}
+ case KeyEvent.KEY_RELEASED:
+ transCaretPositionToXIM((Component) event.getSource());
+ break;
+
// fall through
default:
@@ -360,6 +373,46 @@ public class InputContext extends java.awt.im.InputContext
}
}
+ /**
+ * fix fcitx position
+ */
+ private void transCaretPositionToXIM(Component source) {
+ synchronized (source.getTreeLock()) {
+ synchronized (this) {
+ if ("sun.awt.im.CompositionArea".equals(source.getClass().getName())) {
+ // no special handling for this one
+ } else if (getComponentWindow(source) instanceof InputMethodWindow) {
+ // no special handling for this one either
+ } else {
+ if (!source.isDisplayable()) {
+ // Component is being disposed
+ return;
+ }
+ currentClientComponent = source;
+ }
+ awtFocussedComponent = source;
+ if (inputMethod != null && inputMethod instanceof X11InputMethod) {
+ ((X11InputMethod)inputMethod).setXICTextCursorPosition(source);
+ }
+ InputMethodContext inputContext = ((InputMethodContext)this);
+ if (!inputContext.isCompositionAreaVisible()) {
+ InputMethodRequests req = source.getInputMethodRequests();
+ if (req != null && inputContext.useBelowTheSpotInput()) {
+ inputContext.setCompositionAreaUndecorated(true);
+ } else {
+ inputContext.setCompositionAreaUndecorated(false);
+ }
+ }
+ // restores the composition area if it was set to invisible
+ // when focus got lost
+ if (compositionAreaHidden == true) {
+ ((InputMethodContext)this).setCompositionAreaVisible(true);
+ compositionAreaHidden = false;
+ }
+ }
+ }
+ }
+
/**
* Activates the current input method of this input context, and grabs
* the composition area for use by this input context.
diff --git a/src/java.desktop/unix/classes/sun/awt/X11/XInputMethod.java b/src/java.desktop/unix/classes/sun/awt/X11/XInputMethod.java
index 4244812ceb0..e1e8eff44d3 100644
--- a/src/java.desktop/unix/classes/sun/awt/X11/XInputMethod.java
+++ b/src/java.desktop/unix/classes/sun/awt/X11/XInputMethod.java
@@ -28,12 +28,26 @@ package sun.awt.X11;
import java.awt.AWTException;
import java.awt.Component;
import java.awt.Container;
+import java.awt.geom.Point2D;
+import java.awt.FontMetrics;
+import java.awt.Point;
import java.awt.Rectangle;
import java.awt.im.spi.InputMethodContext;
import java.awt.peer.ComponentPeer;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+import javax.swing.text.JTextComponent;
import sun.awt.AWTAccessor;
import sun.awt.X11InputMethod;
+import sun.awt.X11GraphicsDevice;
import sun.util.logging.PlatformLogger;
@@ -45,8 +59,13 @@ import sun.util.logging.PlatformLogger;
public class XInputMethod extends X11InputMethod {
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XInputMethod");
+ private int scale = -1;
+
public XInputMethod() throws AWTException {
super();
+ GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
+ if (device instanceof X11GraphicsDevice)
+ scale = ((X11GraphicsDevice) device).getScaleFactor();
}
public void setInputMethodContext(InputMethodContext context) {
@@ -86,15 +105,29 @@ public class XInputMethod extends X11InputMethod {
private static volatile long xicFocus = 0;
+ /**
+ * fix fcitx position
+ */
+ public void setXICTextCursorOffXY(ComponentPeer peer) {
+ if (peer == null) {
+ return;
+ }
+ xicFocus = ((XComponentPeer)peer).getContentWindow();
+ int[] result = getOffXYRelateToFrame(peer ,true);
+ setXICFocusNative(((XComponentPeer)peer).getContentWindow(),true,true,result);
+ }
+
protected void setXICFocus(ComponentPeer peer,
boolean value, boolean active) {
if (peer == null) {
return;
}
xicFocus = ((XComponentPeer)peer).getContentWindow();
+ int[] result = getOffXYRelateToFrame(peer ,value);
setXICFocusNative(((XComponentPeer)peer).getContentWindow(),
value,
- active);
+ active,
+ result);
}
public static long getXICFocus() {
@@ -133,6 +166,74 @@ public class XInputMethod extends X11InputMethod {
return null;
}
+ protected int[] getOffXYRelateToFrame(ComponentPeer peer, boolean value) {
+ int[] result = null;
+ if(value && this.awtFocussedComponent !=null && this.awtFocussedComponent instanceof JTextComponent){
+ try {
+ Method method_getFontMetrics = null;
+ Method method_getEditor = null;
+ FontMetrics font_metrics = null;
+ Object editor = null;
+ int font_height = 0;
+ int caret_x = 0;
+ int caret_y = 0;
+ if(this.awtFocussedComponent instanceof JTextArea || this.awtFocussedComponent instanceof JTextField){
+ method_getFontMetrics = this.awtFocussedComponent.getClass().getMethod("getFontMetrics");
+ font_metrics = (FontMetrics)method_getFontMetrics.invoke(this.awtFocussedComponent);
+ font_height = font_metrics.getHeight();
+ JTextComponent jc = (JTextComponent)this.awtFocussedComponent;
+ if(jc.getCaret().getMagicCaretPosition() != null) {
+ caret_x = jc.getCaret().getMagicCaretPosition().x;
+ caret_y = jc.getCaret().getMagicCaretPosition().y;
+ }
+ } else {
+ method_getEditor = this.awtFocussedComponent.getClass().getMethod("getEditor");
+
+ editor = method_getEditor.invoke(this.awtFocussedComponent);
+
+ method_getFontMetrics = editor.getClass().getMethod("getFontMetrics",int.class);
+ font_metrics = (FontMetrics)method_getFontMetrics.invoke(editor, 0);
+ font_height = font_metrics.getHeight();
+ Method getCaretLocations = editor.getClass().getMethod("getCaretLocations", boolean.class);
+ Object[] locations = (Object[])getCaretLocations.invoke(editor, false);
+ Field point = locations[0].getClass().getField("myPoint");
+ Object myPoint = point.get(locations[0]);
+ if (myPoint instanceof Point) {
+ // convert to Point
+ Point p = (Point) myPoint;
+ caret_x = p.x;
+ caret_y = p.y;
+ } else if (myPoint instanceof Point2D) {
+ // convert to Point2D.Double
+ Point2D.Double pd = (Point2D.Double) myPoint;
+ caret_x = (int) pd.x;
+ caret_y = (int) pd.y;
+ }
+ }
+
+ Method method_getLocationOnScreen = this.awtFocussedComponent.getClass().getMethod("getLocationOnScreen");
+
+ Point point = (Point)method_getLocationOnScreen.invoke(this.awtFocussedComponent);
+
+ Method method_getNativeContainer = Component.class.getDeclaredMethod("getNativeContainer");
+ method_getNativeContainer.setAccessible(true);
+ Container c = (Container)method_getNativeContainer.invoke(awtFocussedComponent);
+ if(c != null) {
+ result = new int[]{point.x - c.getPeerLocationOnScreen().x + caret_x, point.y - c.getPeerLocationOnScreen().y + font_height + caret_y};
+ if (scale > 1) {
+ result[0] *= scale;
+ result[1] *= scale;
+ }
+ }
+
+ return result;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ return result;
+ }
+
/*
* Subclasses should override disposeImpl() instead of dispose(). Client
* code should always invoke dispose(), never disposeImpl().
@@ -253,6 +354,6 @@ public class XInputMethod extends X11InputMethod {
private native boolean recreateXICNative(long window, long px11data, int ctxid);
private native int releaseXICNative(long px11data);
private native void setXICFocusNative(long window,
- boolean value, boolean active);
+ boolean value, boolean active, int[] offxy);
private native void adjustStatusWindow(long window);
}
diff --git a/src/java.desktop/unix/classes/sun/awt/X11InputMethod.java b/src/java.desktop/unix/classes/sun/awt/X11InputMethod.java
index d9bed439688..623af69478e 100644
--- a/src/java.desktop/unix/classes/sun/awt/X11InputMethod.java
+++ b/src/java.desktop/unix/classes/sun/awt/X11InputMethod.java
@@ -33,6 +33,7 @@ import java.awt.AWTException;
import java.awt.event.InputMethodEvent;
import java.awt.font.TextAttribute;
import java.awt.font.TextHitInfo;
+import java.awt.Component;
import java.awt.peer.ComponentPeer;
import java.text.AttributedString;
import java.util.Map;
@@ -57,6 +58,11 @@ public abstract class X11InputMethod extends X11InputMethodBase {
super();
}
+ /**
+ * fix fcitx position
+ */
+ public abstract void setXICTextCursorOffXY(ComponentPeer peer);
+
/**
* Reset the composition state to the current composition state.
*/
@@ -380,6 +386,27 @@ public abstract class X11InputMethod extends X11InputMethodBase {
}
}
+ /**
+ * fix fcitx position
+ */
+ public synchronized void setXICTextCursorPosition(Component component) {
+ if (component == null) {
+ return;
+ }
+ if (isActive) {
+ // deactivate/activate are being suppressed during a focus change -
+ // this may happen when an input method window is made visible
+ // boolean ac = haveActiveClient(); already set true in awt_InputMethod.c
+ setXICTextCursorOffXY(getPeer(awtFocussedComponent));
+
+ }
+ awtFocussedComponent = component;
+ ComponentPeer awtFocussedComponentPeer = getPeer(awtFocussedComponent);
+ if(awtFocussedComponent != null) {
+ setXICTextCursorOffXY(awtFocussedComponentPeer);
+ }
+ }
+
protected abstract boolean recreateXIC(int ctxid);
protected abstract int releaseXIC();
private static native boolean recreateX11InputMethod();
diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c b/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c
index e63741f583e..55d847b98ac 100644
--- a/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c
+++ b/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c
@@ -434,13 +434,17 @@ setXICFocus(XIC ic, unsigned short req)
* Sets the focus window to the given XIC.
*/
static void
-setXICWindowFocus(XIC ic, Window w)
+setXICWindowFocus(XIC ic, Window w, int arr[2])
{
if (ic == NULL) {
(void)fprintf(stderr, "Couldn't find X Input Context\n");
return;
}
- (void) XSetICValues(ic, XNFocusWindow, w, NULL);
+ XPoint spot;
+ spot.x = arr[0];
+ spot.y = arr[1];
+ XVaNestedList xy = (XVaNestedList)XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
+ (void) XSetICValues(ic, XNFocusWindow, w, XNPreeditAttributes, xy, NULL);
}
/*
@@ -1519,7 +1523,8 @@ Java_sun_awt_X11_XInputMethod_setXICFocusNative(JNIEnv *env,
jobject this,
jlong w,
jboolean req,
- jboolean active)
+ jboolean active,
+ jintArray arr)
{
X11InputMethodData *pX11IMData;
AWT_LOCK();
@@ -1540,7 +1545,17 @@ Java_sun_awt_X11_XInputMethod_setXICFocusNative(JNIEnv *env,
* On Solaris2.6, setXICWindowFocus() has to be invoked
* before setting focus.
*/
- setXICWindowFocus(pX11IMData->current_ic, w);
+ int positions[2] = {100,50};
+ if (arr)
+ {
+ int length = (*env)->GetArrayLength(env,arr);
+ int *addArr = (*env)->GetIntArrayElements(env, arr, NULL);
+ for (int i= 0; i < length; ++i) {
+ positions[i] = *(addArr + i);
+ }
+ (*env)->ReleaseIntArrayElements(env, arr, addArr, 0);
+ }
+ setXICWindowFocus(pX11IMData->current_ic, w, positions);
setXICFocus(pX11IMData->current_ic, req);
currentX11InputMethodInstance = pX11IMData->x11inputmethod;
currentFocusWindow = w;