Skip to content

Commit 10ad6cd

Browse files
author
zhourenjian
committed
Improve Combo widget to support different font-size, support modify event and so on
1 parent afd045e commit 10ad6cd

File tree

1 file changed

+102
-7
lines changed
  • sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets

1 file changed

+102
-7
lines changed

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

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.eclipse.swt.events.SelectionListener;
2121
import org.eclipse.swt.events.VerifyListener;
2222
import org.eclipse.swt.graphics.Color;
23+
import org.eclipse.swt.graphics.Font;
2324
import org.eclipse.swt.graphics.Point;
2425
import org.eclipse.swt.graphics.Rectangle;
2526
import org.eclipse.swt.internal.ResizeSystem;
@@ -86,15 +87,19 @@ public class Combo extends Composite {
8687
private boolean isSimple;
8788
private int itemCount;
8889
private int maxWidth = 0;
89-
90+
private String textValue;
91+
9092
private Object hDropDownClick;
9193
private Object hEditKeyUp;
9294
private Object hEditShow;
9395
private Object hTextChange;
9496
private Object hTextBlur;
9597
private Object hTextMouseUp;
9698
private Object hTextKeyUp;
97-
99+
private Object hModifyFocus;
100+
private Object hModifyBlur;
101+
private Object hModifyKeyUp;
102+
98103
/**
99104
* the operating system limit for the number of characters
100105
* that the text field in an instance of this class can hold
@@ -668,8 +673,71 @@ void hookFocusOut() {
668673
}
669674

670675
void hookModify() {
671-
super.hookModify();
672-
Clazz.addEvent(textInput, "change", hModify);
676+
if (hModifyKeyUp != null) {
677+
return;
678+
}
679+
hModifyKeyUp = new RunnableCompatibility() {
680+
public void run() {
681+
if ((style & SWT.READ_ONLY) != 0
682+
/*
683+
* I have changed !hooks (SWT.Verify) && !filters (SWT.Verify)) to
684+
* hooks (SWT.Verify) && !filters (SWT.Verify))
685+
* I do not know what is the rational behind the first.
686+
*/
687+
|| (hooks (SWT.Verify) && !filters (SWT.Verify))) {
688+
toReturn(true);
689+
return ;
690+
}
691+
String newText = textInput.value;
692+
if (newText != null) {
693+
newText = verifyText (newText, 0, 0, null);
694+
if (newText == null) {
695+
toReturn(true);
696+
return ;
697+
}
698+
if (textValue != textInput.value) {
699+
textValue = textInput.value;
700+
Event e = new Event();
701+
e.type = SWT.Modify;
702+
e.item = Combo.this;
703+
e.widget = Combo.this;
704+
sendEvent(e);
705+
toReturn(e.doit);
706+
}
707+
}
708+
}
709+
};
710+
Clazz.addEvent(textInput, "keyup", hModifyKeyUp);
711+
Clazz.addEvent(textInput, "change", hModifyKeyUp);
712+
713+
hModifyBlur = new RunnableCompatibility() {
714+
public void run() {
715+
OS.removeCSSClass(handle, "text-focus");
716+
Event e = new Event();
717+
e.type = SWT.FocusOut;
718+
e.item = Combo.this;
719+
e.widget = Combo.this;
720+
sendEvent(e);
721+
toReturn(e.doit);
722+
}
723+
};
724+
Clazz.addEvent(textInput, "blur", hModifyBlur);
725+
726+
hModifyFocus = new RunnableCompatibility() {
727+
public void run() {
728+
OS.addCSSClass(handle, "text-focus");
729+
Event e = new Event();
730+
e.type = SWT.FocusIn;
731+
e.item = Combo.this;
732+
e.widget = Combo.this;
733+
sendEvent(e);
734+
toReturn(e.doit);
735+
}
736+
};
737+
Clazz.addEvent(textInput, "focus", hModifyFocus);
738+
739+
// super.hookModify();
740+
// Clazz.addEvent(textInput, "change", hModify);
673741
}
674742

675743
void hookSelection() {
@@ -1766,6 +1834,7 @@ void setBounds (int x, int y, int width, int height, int flags) {
17661834
textInput.style.width = Math.max(0, width - buttonWidth - 3) + "px";
17671835
textInput.style.height = Math.max(0, height - 4) + "px";
17681836
textInput.style.lineHeight = Math.max(0, height - 4) + "px";
1837+
textInput.style.marginTop = "0";
17691838
} else if (OS.isSafari || OS.isChrome) {
17701839
textInput.style.marginTop = "0";
17711840
} else if (OS.isIE) {
@@ -2080,6 +2149,13 @@ public void setSelection (Point selection) {
20802149
Text.setTextSelection(textInput, selection.x + 1, selection.y + 2);
20812150
}
20822151

2152+
public void setFont(Font font) {
2153+
super.setFont(font);
2154+
Element handle = fontHandle();
2155+
selectInput.style.fontFamily = handle.style.fontFamily;
2156+
selectInput.style.fontSize = handle.style.fontSize;
2157+
}
2158+
20832159
/**
20842160
* Sets the contents of the receiver's text field to the
20852161
* given string.
@@ -2122,11 +2198,15 @@ public void setText (String string, boolean modify) {
21222198
/*
21232199
TCHAR buffer = new TCHAR (getCodePage (), string, true);
21242200
if (OS.SetWindowText (handle, buffer)) {
2125-
*/
21262201
if(modify){
21272202
sendEvent (SWT.Modify);
21282203
// widget could be disposed at this point
21292204
}
2205+
*/
2206+
if (textValue != string) {
2207+
textValue = string;
2208+
sendEvent(SWT.Modify);
2209+
}
21302210
}
21312211

21322212
/**
@@ -2759,9 +2839,24 @@ protected void releaseHandle() {
27592839
if (hFocusOut != null) {
27602840
Clazz.removeEvent(textInput, "blur", hFocusOut);
27612841
}
2762-
if (hModify != null) {
2763-
Clazz.removeEvent(textInput, "change", hModify);
2842+
// if (hModify != null) {
2843+
// Clazz.removeEvent(textInput, "change", hModify);
2844+
// }
2845+
2846+
if (hModifyBlur != null) {
2847+
Clazz.removeEvent(textInput, "blur", hModifyBlur);
2848+
hModifyBlur = null;
2849+
}
2850+
if (hModifyFocus != null) {
2851+
Clazz.removeEvent(textInput, "focus", hModifyFocus);
2852+
hModifyFocus = null;
27642853
}
2854+
if (hModifyKeyUp != null) {
2855+
Clazz.removeEvent(textInput, "change", hModifyKeyUp);
2856+
Clazz.removeEvent(textInput, "keyup", hModifyKeyUp);
2857+
hModifyKeyUp = null;
2858+
}
2859+
27652860
Clazz.removeEvent(textInput, "dblclick", hEditShow);
27662861
hEditShow = null;
27672862
Clazz.removeEvent(textInput, "keyup", hEditKeyUp);

0 commit comments

Comments
 (0)