Skip to content

Commit dc8f0f4

Browse files
author
jossonsmith
committed
Improve popup of Combo: The SELECT is displayed in document.body rather in Shell's
handle element. Fix layout bug for Combo in IE.
1 parent 22becc5 commit dc8f0f4

File tree

3 files changed

+94
-52
lines changed

3 files changed

+94
-52
lines changed

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/browser/OS.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,43 @@ public static Point getStringStyledSize(String str, String className, String css
427427
}
428428

429429
public static Point calcuateRelativePosition(Element el, Element relativeEl){
430-
Element currentEl = el;
430+
Element srcEl = el;
431431
int left = 0;
432432
int top = 0;
433-
while (currentEl != null && currentEl != relativeEl) {
434-
left += currentEl.offsetLeft;
435-
top += currentEl.offsetTop;
436-
currentEl = currentEl.offsetParent;
437-
}
438-
if(isFirefox){
439-
// why?
440-
left += 6;
441-
top += 2;
433+
while (el != null && el != relativeEl) {
434+
left += el.offsetLeft;
435+
top += el.offsetTop;
436+
if (el != srcEl)
437+
/**
438+
* @j2sNative
439+
var style = null;
440+
if (document.defaultView != null) { // OS.isMozilla || OS.isOpera
441+
style = document.defaultView.getComputedStyle (el, null);
442+
} else if (el.currentStyle != null) { // OS.isIE
443+
style = el.currentStyle;
444+
}
445+
if (!O$.isOpera && style != null) {
446+
var w = 0;
447+
var bw = style.borderLeftWidth;
448+
if (bw.length != 0) {
449+
w = parseInt (bw); // unit is considered as "px"
450+
if (!isNaN(w)) {
451+
left += w;
452+
}
453+
}
454+
bw = style.borderTopWidth;
455+
if (bw.length != 0) {
456+
w = parseInt (bw); // unit is considered as "px"
457+
if (!isNaN(w)) {
458+
top += w;
459+
}
460+
}
461+
}
462+
*/ {}
463+
464+
el = el.offsetParent;
442465
}
443-
return new Point(left, top + OS.getContainerHeight(el));
466+
return new Point(left, top);
444467
}
445468

446469
public static void updateArrowSize(Object el, int style, int cx, int cy) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.combo-default {
22
padding: 0;
33
border-style:none;
4-
overflow:hidden;
4+
/*overflow:hidden;*/
55
position:absolute;
66
border-width:2px;
77
border-style:inset;
@@ -16,7 +16,8 @@
1616
}
1717

1818
.combo-default button {
19-
float:right;
19+
/*float:right;*/
20+
position:absolute;
2021
right:0;
2122
top:0;
2223
height:100%;
@@ -26,6 +27,9 @@
2627
font-size:0;
2728
margin:0;
2829
}
30+
* html .combo-default button {
31+
right:-2px;
32+
}
2933
.combo-button-arrow-down {
3034
margin:auto;
3135
height:0;

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

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.swt.internal.xhtml.HTMLEvent;
2525
import org.eclipse.swt.internal.xhtml.Option;
2626
import org.eclipse.swt.internal.xhtml.document;
27+
import org.eclipse.swt.internal.xhtml.window;
2728

2829
/**
2930
* Instances of this class are controls that allow the user
@@ -551,33 +552,10 @@ protected void createHandle () {
551552
textInput.type = "text";
552553
textInput.className = "combo-input-box";
553554
textInput.readOnly = (style & SWT.READ_ONLY)!=0;
554-
textInput.size = Combo.LIMIT;
555+
//textInput.size = Combo.LIMIT;
555556
handle.appendChild(textInput);
556557

557-
int height = OS.getContainerHeight(dropDownButton);
558-
559-
selectInput = document.createElement("SELECT");
560-
if(isSimple){
561-
selectInput.style.top = height + "px";
562-
selectInput.style.left = textInput.style.left;
563-
// System.out.println("is Simple " + isSimple);
564-
selectInput.className = "combo-select-box-visible";
565-
selectInput.size = visibleCount;
566-
handle.appendChild(selectInput);
567-
}else{
568-
selectInput.style.top = height + "px" ;
569-
selectInput.style.left = textInput.style.left;
570-
// System.out.println("is Simple " + isSimple);
571-
selectInput.className = "combo-select-box-invisible combo-select-box-notsimple";
572-
selectInput.size = visibleCount;
573-
// System.out.println("ho combo1 " + textInput.scrollHeight);
574-
// System.out.println("ho combo2 " + textInput.offsetHeight);
575-
// System.out.println("ho combo3 " + textInput.clientHeight);
576-
//TODO: add the select to shell to be shown every where
577-
578-
getShell().handle.appendChild(selectInput);
579-
// handle.appendChild(selectInput);
580-
}
558+
//int height = OS.getContainerHeight(dropDownButton);
581559

582560
textInput.ondblclick = new RunnableCompatibility() {
583561
public void run() {
@@ -624,6 +602,28 @@ public void run() {
624602
}
625603
};
626604

605+
createSelect();
606+
configureSelect();
607+
}
608+
609+
void createSelect() {
610+
selectInput = document.createElement("SELECT");
611+
if(isSimple){
612+
selectInput.style.top = height + "px";
613+
selectInput.style.left = textInput.style.left;
614+
selectInput.className = "combo-select-box-visible";
615+
selectInput.size = visibleCount;
616+
handle.appendChild(selectInput);
617+
}else{
618+
selectInput.style.position = "absolute";
619+
selectInput.style.top = height + "px" ;
620+
selectInput.style.left = textInput.style.left;
621+
selectInput.className = "combo-select-box-invisible combo-select-box-notsimple";
622+
selectInput.size = visibleCount;
623+
handle.appendChild(selectInput);
624+
}
625+
}
626+
void configureSelect() {
627627
selectInput.onchange = new RunnableCompatibility() {
628628
public void run() {
629629
// System.out.println("select changed!" + selectInput.selectedIndex);
@@ -634,7 +634,7 @@ public void run() {
634634
}
635635
};
636636

637-
selectInput.onselectchange = selectInput.onblur = new RunnableCompatibility() {
637+
selectInput.onblur = new RunnableCompatibility() {
638638
public void run() {
639639
// System.out.println("handle blurred!");
640640
// updateSelection();
@@ -649,16 +649,18 @@ public void run() {
649649
// hide();
650650
// }
651651
// };
652-
653-
654652
}
655-
656653
void hide(){
657-
// if(!this.selectShown){
658-
// return;
659-
// }
660-
654+
if(!this.selectShown){
655+
return;
656+
}
661657
this.selectShown = false;
658+
try {
659+
document.body.removeChild(selectInput);
660+
handle.appendChild(selectInput);
661+
} catch (Throwable e) {
662+
663+
}
662664
selectInput.className = "combo-select-box-invisible" + (isSimple ? "" : " combo-select-box-notsimple");
663665
}
664666

@@ -668,13 +670,26 @@ void show(){
668670
// return;
669671
// }
670672

671-
Point coordinate = OS.calcuateRelativePosition(textInput, getShell().handle);
673+
Point coordinate = OS.calcuateRelativePosition(handle, document.body);
674+
coordinate.y += OS.getContainerHeight(handle);
675+
if (OS.isFirefox) {
676+
coordinate.x += 1;
677+
coordinate.y += 1;
678+
} else if (OS.isIE) {
679+
coordinate.x -= 1;
680+
coordinate.y -= 2;
681+
}
672682
this.selectShown = true;
673-
selectInput.style.zIndex = "120";
683+
window.currentTopZIndex = "" + (Integer.parseInt(window.currentTopZIndex) + 1);
684+
selectInput.style.zIndex = window.currentTopZIndex;
685+
try {
686+
handle.removeChild(selectInput);
687+
document.body.appendChild(selectInput);
688+
} catch (Throwable e) {
689+
}
674690
selectInput.className = "combo-select-box-visible" + (isSimple ? "" : " combo-select-box-notsimple");
675-
selectInput.style.top = (coordinate.y + 2) + "px";
676-
selectInput.style.left = (coordinate.x - 4) + "px";
677-
// System.out.println("Z " + selectInput.style.zIndex);
691+
selectInput.style.top = coordinate.y + "px";
692+
selectInput.style.left = coordinate.x + "px";
678693
try {
679694
selectInput.focus();
680695
} catch (Throwable e) {
@@ -2501,7 +2516,7 @@ void enableWidget(boolean enabled) {
25012516
}
25022517
protected boolean SetWindowPos(Object hWnd, Object hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags) {
25032518
if (OS.isIE) {
2504-
dropDownButton.style.height = (cy - 2) + "px";
2519+
dropDownButton.style.height = (cy - 4) + "px";
25052520
}
25062521
OS.updateArrowSize(dropDownButton.childNodes[0], SWT.DOWN, 16, cy);
25072522

0 commit comments

Comments
 (0)