X Tutup
Skip to content

Commit 54131f1

Browse files
author
jossonsmith
committed
Improve Combo popup box: Adjust the popup according to the screen constrainted bounds.
1 parent 6b31cfb commit 54131f1

File tree

2 files changed

+105
-3
lines changed
  • sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt

2 files changed

+105
-3
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.swt.internal.browser;
15+
16+
import org.eclipse.swt.graphics.Rectangle;
17+
18+
/**
19+
* @author josson smith
20+
*
21+
* 2006-9-12
22+
*/
23+
public class Popup {
24+
25+
/**
26+
* Popup a given height box near the given rectangle box in the constrainted bounds.
27+
* The popup box is in the same width of give rectangle box.
28+
*
29+
* @param bounds
30+
* @param rect
31+
* @param height
32+
* @return
33+
*/
34+
public static Rectangle popupList(Rectangle bounds, Rectangle rect, int height) {
35+
if (height <= 0) {
36+
return null;
37+
}
38+
int x, y, w, h = height;
39+
if (bounds == null) {
40+
if (rect == null) {
41+
x = y = 0;
42+
w = 100;
43+
} else {
44+
x = rect.x;
45+
y = rect.y + height;
46+
w = rect.width;
47+
}
48+
} else {
49+
if (rect == null) {
50+
x = bounds.x + bounds.width / 4;
51+
y = bounds.y + (bounds.height - height) / 2;
52+
w = bounds.width / 2;
53+
} else {
54+
x = rect.x;
55+
w = rect.width;
56+
if (rect.y + rect.height + height > bounds.y + bounds.height) {
57+
if (rect.y - height >= bounds.y) {
58+
y = rect.y - height;
59+
} else {
60+
if (bounds.height < height) {
61+
y = bounds.y;
62+
h = bounds.height;
63+
} else {
64+
if (Math.abs(bounds.y + bounds.height - height - rect.y) > Math.abs(bounds.y + height - rect.y - rect.height)) {
65+
y = bounds.y;
66+
} else {
67+
y = bounds.y + bounds.height - height;
68+
}
69+
}
70+
}
71+
} else {
72+
y = rect.y + rect.height;
73+
}
74+
}
75+
}
76+
return new Rectangle(x, y, w, h);
77+
}
78+
79+
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Combo.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import org.eclipse.swt.events.SelectionListener;
1919
import org.eclipse.swt.events.VerifyListener;
2020
import org.eclipse.swt.graphics.Point;
21+
import org.eclipse.swt.graphics.Rectangle;
2122
import org.eclipse.swt.internal.RunnableCompatibility;
2223
import org.eclipse.swt.internal.browser.OS;
24+
import org.eclipse.swt.internal.browser.Popup;
2325
import org.eclipse.swt.internal.xhtml.Element;
2426
import org.eclipse.swt.internal.xhtml.HTMLEvent;
2527
import org.eclipse.swt.internal.xhtml.Option;
@@ -671,14 +673,27 @@ void show(){
671673
// }
672674

673675
Point coordinate = OS.calcuateRelativePosition(handle, document.body);
674-
coordinate.y += OS.getContainerHeight(handle);
676+
//coordinate.y += OS.getContainerHeight(handle);
677+
/*
675678
if (OS.isFirefox) {
676679
coordinate.x += 1;
677680
coordinate.y += 1;
678681
} else if (OS.isIE) {
679682
coordinate.x -= 1;
680683
coordinate.y -= 2;
681684
}
685+
*/
686+
int w = OS.getContainerWidth(handle);
687+
int h = OS.getContainerHeight(handle);
688+
if (OS.isFirefox) {
689+
coordinate.x += 1;
690+
//coordinate.y -= 1;
691+
h += 1;
692+
} else if (OS.isIE) {
693+
coordinate.x -= 1;
694+
coordinate.y -= 2;
695+
//h -= 2;
696+
}
682697
this.selectShown = true;
683698
window.currentTopZIndex = "" + (Integer.parseInt(window.currentTopZIndex) + 1);
684699
selectInput.style.zIndex = window.currentTopZIndex;
@@ -688,8 +703,16 @@ void show(){
688703
} catch (Throwable e) {
689704
}
690705
selectInput.className = "combo-select-box-visible" + (isSimple ? "" : " combo-select-box-notsimple");
691-
selectInput.style.top = coordinate.y + "px";
692-
selectInput.style.left = coordinate.x + "px";
706+
int height = OS.getContainerHeight(selectInput);
707+
Rectangle bounds = Popup.popupList(getMonitor().getClientArea(), new Rectangle(coordinate.x, coordinate.y, w, h), height);
708+
selectInput.style.left = bounds.x + "px";
709+
selectInput.style.top = bounds.y + "px";
710+
if (bounds.height != height) {
711+
try {
712+
selectInput.style.overflow = "overflow";
713+
} catch (Throwable e) {} // IE
714+
selectInput.style.height = bounds.height + "px";
715+
}
693716
try {
694717
selectInput.focus();
695718
} catch (Throwable e) {

0 commit comments

Comments
 (0)
X Tutup