Qt 6.x
The Qt SDK
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.content.Context;
8import android.os.Bundle;
9import android.util.Log;
10import android.view.LayoutInflater;
11import android.view.View;
12import android.widget.LinearLayout;
13import android.widget.ImageView;
14import android.content.res.TypedArray;
15import android.graphics.drawable.Drawable;
16import android.view.MotionEvent;
17import android.widget.PopupWindow;
18import android.app.Activity;
19import android.view.ViewTreeObserver;
20import android.view.View.OnClickListener;
21import android.view.ViewGroup.LayoutParams;
22import android.view.ViewGroup;
23import android.R;
24
25// Helper class that manages a cursor or selection handle
26public class EditPopupMenu implements ViewTreeObserver.OnPreDrawListener, View.OnLayoutChangeListener,
27 EditContextView.OnClickListener
28{
29 private View m_layout = null;
30 private EditContextView m_view = null;
31 private PopupWindow m_popup = null;
32 private Activity m_activity;
33 private int m_posX;
34 private int m_posY;
35 private int m_buttons;
36 private CursorHandle m_cursorHandle;
37 private CursorHandle m_leftSelectionHandle;
38 private CursorHandle m_rightSelectionHandle;
39
40 public EditPopupMenu(Activity activity, View layout)
41 {
42 m_activity = activity;
43 m_view = new EditContextView(activity, this);
44 m_view.addOnLayoutChangeListener(this);
45
46 m_layout = layout;
47 }
48
49 private void initOverlay()
50 {
51 if (m_popup != null)
52 return;
53
54 Context context = m_layout.getContext();
55 m_popup = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle);
56 m_popup.setSplitTouchEnabled(true);
57 m_popup.setClippingEnabled(false);
58 m_popup.setContentView(m_view);
59 m_popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
60 m_popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
61
62 m_layout.getViewTreeObserver().addOnPreDrawListener(this);
63 }
64
65 // Show the handle at a given position (or move it if it is already shown)
66 public void setPosition(final int x, final int y, final int buttons,
67 CursorHandle cursorHandle, CursorHandle leftSelectionHandle, CursorHandle rightSelectionHandle)
68 {
69 initOverlay();
70
71 m_view.updateButtons(buttons);
72 final int[] layoutLocation = new int[2];
73 m_layout.getLocationOnScreen(layoutLocation);
74
75 // These values are used for handling split screen case
76 final int[] activityLocation = new int[2];
77 final int[] activityLocationInWindow = new int[2];
78 m_activity.getWindow().getDecorView().getLocationOnScreen(activityLocation);
79 m_activity.getWindow().getDecorView().getLocationInWindow(activityLocationInWindow);
80
81 int x2 = x + layoutLocation[0] - activityLocation[0];
82 int y2 = y + layoutLocation[1] + (activityLocationInWindow[1] - activityLocation[1]);
83
84 x2 -= m_view.getWidth() / 2 ;
85
86 y2 -= m_view.getHeight();
87 if (y2 < 0) {
88 if (cursorHandle != null) {
89 y2 = cursorHandle.bottom();
90 } else if (leftSelectionHandle != null && rightSelectionHandle != null) {
91 y2 = Math.max(leftSelectionHandle.bottom(), rightSelectionHandle.bottom());
92 if (y2 <= 0)
93 m_layout.requestLayout();
94 }
95 }
96
97 if (m_layout.getWidth() < x + m_view.getWidth() / 2)
98 x2 = m_layout.getWidth() - m_view.getWidth();
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(m_layout, 0, x2, y2);
107
108 m_posX = x;
109 m_posY = y;
110 m_buttons = buttons;
111 m_cursorHandle = cursorHandle;
112 m_leftSelectionHandle = leftSelectionHandle;
113 m_rightSelectionHandle = rightSelectionHandle;
114 }
115
116 public void hide() {
117 if (m_popup != null) {
118 m_popup.dismiss();
119 m_popup = null;
120 }
121 }
122
123 @Override
124 public boolean onPreDraw() {
125 // This hook is called when the view location is changed
126 // For example if the keyboard appears.
127 // Adjust the position of the handle accordingly
128 if (m_popup != null && m_popup.isShowing())
129 setPosition(m_posX, m_posY, m_buttons, m_cursorHandle, m_leftSelectionHandle, m_rightSelectionHandle);
130
131 return true;
132 }
133
134 @Override
135 public void onLayoutChange(View v, int left, int top, int right, int bottom,
136 int oldLeft, int oldTop, int oldRight, int oldBottom)
137 {
138 if ((right - left != oldRight - oldLeft || bottom - top != oldBottom - oldTop) &&
139 m_popup != null && m_popup.isShowing())
140 setPosition(m_posX, m_posY, m_buttons, m_cursorHandle, m_leftSelectionHandle, m_rightSelectionHandle);
141 }
142
143 @Override
144 public void contextButtonClicked(int buttonId) {
145 switch (buttonId) {
146 case R.string.cut:
147 QtNativeInputConnection.cut();
148 break;
149 case R.string.copy:
150 QtNativeInputConnection.copy();
151 break;
152 case R.string.paste:
153 QtNativeInputConnection.paste();
154 break;
155 case R.string.selectAll:
156 QtNativeInputConnection.selectAll();
157 break;
158 }
159 hide();
160 }
161}
EditPopupMenu(Activity activity, View layout)
void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
void setPosition(final int x, final int y, final int buttons, CursorHandle cursorHandle, CursorHandle leftSelectionHandle, CursorHandle rightSelectionHandle)
static void * context
Qt::MouseButtons m_buttons
Definition qnsview.mm:100
n void setPosition(void) \n\
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint left
GLint GLint bottom
GLint y
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
QVBoxLayout * layout