Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
EditPopupMenu.java
Go to the documentation of this file.
1// Copyright (C) 2018 BogDan Vatra <bogdan@kde.org>
2// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5package org.qtproject.qt.android;
6
7import android.app.Activity;
8import android.graphics.Point;
9import android.util.Log;
10import android.view.View;
11import android.view.ViewGroup;
12import android.view.ViewTreeObserver;
13import android.widget.PopupWindow;
14
15// Helper class that manages a cursor or selection handle
16class EditPopupMenu implements ViewTreeObserver.OnPreDrawListener, View.OnLayoutChangeListener,
17 EditContextView.OnClickListener
18{
19 private final EditContextView m_view;
20 private final QtEditText m_editText;
21
22 private PopupWindow m_popup = null;
23 private final Activity m_activity;
24 private int m_posX;
25 private int m_posY;
26 private int m_buttons;
27
28 EditPopupMenu(QtEditText editText)
29 {
30 m_activity = (Activity) editText.getContext();
31 m_view = new EditContextView(m_activity, this);
32 m_view.addOnLayoutChangeListener(this);
33
34 m_editText = editText;
35 m_editText.getViewTreeObserver().addOnPreDrawListener(this);
36 }
37
38 private void initOverlay()
39 {
40 if (m_popup != null)
41 return;
42
43 m_popup = new PopupWindow(m_activity, null, android.R.attr.textSelectHandleWindowStyle);
44 m_popup.setSplitTouchEnabled(true);
45 m_popup.setClippingEnabled(false);
46 m_popup.setContentView(m_view);
47 m_popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
48 m_popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
49 }
50
51 // Show the handle at a given position (or move it if it is already shown)
52 void setPosition(final int x, final int y, final int buttons)
53 {
54 initOverlay();
55
56 m_view.updateButtons(buttons);
57 Point viewSize = m_view.getCalculatedSize();
58
59 final int[] layoutLocation = new int[2];
60
61 // Since QtEditText doesn't match the QtWindow size, we should use its parent for
62 // EditPopupMenu positioning. However, there may be cases where the parent is
63 // not set. In such cases, we need to use QtEditText instead.
64 View positioningView = (View) m_editText.getParent();
65 if (positioningView == null)
66 positioningView = m_editText;
67 positioningView.getLocationOnScreen(layoutLocation);
68
69 // These values are used for handling split screen case
70 final int[] activityLocation = new int[2];
71 final int[] activityLocationInWindow = new int[2];
72 m_activity.getWindow().getDecorView().getLocationOnScreen(activityLocation);
73 m_activity.getWindow().getDecorView().getLocationInWindow(activityLocationInWindow);
74
75 int x2 = x + layoutLocation[0] - activityLocation[0];
76 int y2 = y + layoutLocation[1] + (activityLocationInWindow[1] - activityLocation[1]);
77
78 x2 -= viewSize.x / 2 ;
79
80 y2 -= viewSize.y;
81 if (y2 < 0)
82 y2 = m_editText.getSelectionHandleBottom();
83
84 if (y2 <= 0) {
85 try {
86 QtLayout parentLayout = (QtLayout) m_editText.getParent();
87 parentLayout.requestLayout();
88 } catch (ClassCastException e) {
89 Log.w(QtNative.QtTAG, "QtEditText " + m_editText + " parent is not a QtLayout, " +
90 "requestLayout() skipped");
91 } catch (NullPointerException e) {
92 Log.w(QtNative.QtTAG, "QtEditText " + m_editText + " does not have a parent, " +
93 "requestLayout() skipped");
94 }
95 }
96
97 if (positioningView.getWidth() < x + viewSize.x / 2)
98 x2 = positioningView.getWidth() - viewSize.x;
99
100 if (x2 < 0)
101 x2 = 0;
102
103 if (m_popup.isShowing())
104 m_popup.update(x2, y2, -1, -1);
105 else
106 m_popup.showAtLocation(positioningView, 0, x2, y2);
107
108 m_posX = x;
109 m_posY = y;
110 m_buttons = buttons;
111 }
112
113 void hide() {
114 if (m_popup != null) {
115 m_popup.dismiss();
116 m_popup = null;
117 }
118 }
119
120 @Override
121 public boolean onPreDraw() {
122 // This hook is called when the view location is changed
123 // For example if the keyboard appears.
124 // Adjust the position of the handle accordingly
125 if (m_popup != null && m_popup.isShowing())
126 setPosition(m_posX, m_posY, m_buttons);
127
128 return true;
129 }
130
131 @Override
132 public void onLayoutChange(View v, int left, int top, int right, int bottom,
133 int oldLeft, int oldTop, int oldRight, int oldBottom)
134 {
135 if ((right - left != oldRight - oldLeft || bottom - top != oldBottom - oldTop) &&
136 m_popup != null && m_popup.isShowing())
137 setPosition(m_posX, m_posY, m_buttons);
138 }
139
140 @Override
141 public void contextButtonClicked(int buttonId) {
142 switch (buttonId) {
143 case android.R.string.cut:
144 QtNativeInputConnection.cut();
145 break;
146 case android.R.string.copy:
147 QtNativeInputConnection.copy();
148 break;
149 case android.R.string.paste:
150 QtNativeInputConnection.paste();
151 break;
152 case android.R.string.selectAll:
153 QtNativeInputConnection.selectAll();
154 break;
155 }
156 hide();
157 }
158}
n void setPosition(void) \n\
GLdouble GLdouble right
GLint GLint GLint GLint GLint x
GLint GLint bottom
GLdouble GLdouble GLdouble GLdouble top
GLsizei const GLfloat * v
GLint y
GLint left
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
edit hide()