Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Williams2019-05-03 13:51:43 +0000
committerEric Williams2019-08-06 13:43:09 +0000
commit6f7a89a9aab424b92bc846d66939ab93a4ea38d7 (patch)
tree857a42c64a33e993b0470c301f8638bb49221091
parentf3664c83ee537adc3df5ea4f3f151cbdf0f47281 (diff)
downloadeclipse.platform.swt-6f7a89a9aab424b92bc846d66939ab93a4ea38d7.tar.gz
eclipse.platform.swt-6f7a89a9aab424b92bc846d66939ab93a4ea38d7.tar.xz
eclipse.platform.swt-6f7a89a9aab424b92bc846d66939ab93a4ea38d7.zip
Bug 4745: [StyledText] StyledText does not provide a disabled look
(1GG00JX) Set background/foreground colors to COLOR_TEXT_DISABLED_BACKGROUND and COLOR_WIDGET_DISABLED_FOREGROUND respectively, to provide a disabled look when StyledText.setEnabled(false) is called. The behaves the same way as the Text widget on GTK does. Change-Id: Id0325993c6206cc660568bff9d094df514194e88 Signed-off-by: Eric Williams <ericwill@redhat.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java52
-rw-r--r--tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java58
2 files changed, 109 insertions, 1 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
index 676bc7ff1d..0762e3f3ae 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
@@ -137,6 +137,14 @@ public class StyledText extends Canvas {
Map<Integer, Integer> keyActionMap = new HashMap<>();
Color background = null; // workaround for bug 4791
Color foreground = null; //
+ /** True if a non-default background color is set */
+ boolean customBackground;
+ /** True if a non-default foreground color is set */
+ boolean customForeground;
+ /** False iff the widget is disabled */
+ boolean enabled = true;
+ /** True iff the widget is in the midst of being enabled or disabled */
+ boolean insideSetEnableCall;
Clipboard clipboard;
int clickCount;
int autoScrollDirection = SWT.NULL; // the direction of autoscrolling (up, down, right, left)
@@ -8490,6 +8498,19 @@ public void setAlwaysShowScrollBars(boolean show) {
@Override
public void setBackground(Color color) {
checkWidget();
+ boolean backgroundDisabled = false;
+ if (!this.enabled && color == null) {
+ if (background != null) {
+ Color disabledBg = getDisplay().getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND);
+ if (background.equals(disabledBg)) {
+ return;
+ } else {
+ color = new Color (getDisplay(), disabledBg.getRGBA());
+ backgroundDisabled = true;
+ }
+ }
+ }
+ customBackground = color != null && !this.insideSetEnableCall && !backgroundDisabled;
background = color;
super.setBackground(color);
resetCache(0, content.getLineCount());
@@ -8934,6 +8955,22 @@ public void setEditable(boolean editable) {
checkWidget();
this.editable = editable;
}
+@Override
+public void setEnabled (boolean enabled) {
+ super.setEnabled(enabled);
+ Display display = getDisplay();
+ this.enabled = enabled;
+ this.insideSetEnableCall = true;
+ if (enabled) {
+ if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
+ if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
+ } else {
+ setSelectionRange(0,0);
+ if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND));
+ if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND));
+ }
+ this.insideSetEnableCall = false;
+}
/**
* Sets a new font to render text with.
* <p>
@@ -8972,8 +9009,21 @@ public void setFont(Font font) {
@Override
public void setForeground(Color color) {
checkWidget();
+ boolean foregroundDisabled = false;
+ if (!this.enabled && color == null) {
+ if (foreground != null) {
+ Color disabledFg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
+ if (foreground.equals(disabledFg)) {
+ return;
+ } else {
+ color = new Color (getDisplay(), disabledFg.getRGBA());
+ foregroundDisabled = true;
+ }
+ }
+ }
+ customForeground = color != null && !this.insideSetEnableCall && !foregroundDisabled;
foreground = color;
- super.setForeground(getForeground());
+ super.setForeground(color);
resetCache(0, content.getLineCount());
setCaretLocation();
super.redraw();
diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java
index d67658e18b..c11d4b4ff0 100644
--- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java
+++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java
@@ -3423,6 +3423,64 @@ public void test_setDoubleClickEnabledZ(){
}
@Test
+public void test_setEnabled(){
+ // Get colors
+ Color disabledBg = text.getDisplay().getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND);
+ Color disabledFg = text.getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
+ Color enabledBg = text.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
+ Color enabledFg = text.getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND);
+
+ // Test basic enabled/disabled functionality twice
+ text.setEnabled(false);
+ assertEquals(disabledBg, text.getBackground());
+ assertEquals(disabledFg, text.getForeground());
+ text.setEnabled(true);
+ assertEquals(enabledBg, text.getBackground());
+ assertEquals(enabledFg, text.getForeground());
+ text.setEnabled(false);
+ assertEquals(disabledBg, text.getBackground());
+ assertEquals(disabledFg, text.getForeground());
+ text.setEnabled(true);
+ assertEquals(enabledBg, text.getBackground());
+ assertEquals(enabledFg, text.getForeground());
+
+ // Test color preservation
+ text.setBackground(getColor(BLUE));
+ text.setForeground(getColor(RED));
+ text.setEnabled(false);
+ assertEquals(getColor(BLUE), text.getBackground());
+ assertEquals(getColor(RED), text.getForeground());
+ text.setEnabled(true);
+ assertEquals(getColor(BLUE), text.getBackground());
+ assertEquals(getColor(RED), text.getForeground());
+
+ // Test color reset
+ text.setBackground(null);
+ text.setForeground(null);
+ assertEquals(enabledBg, text.getBackground());
+ assertEquals(enabledFg, text.getForeground());
+ text.setEnabled(false);
+ text.setBackground(null);
+ text.setForeground(null);
+ assertEquals(disabledBg, text.getBackground());
+ assertEquals(disabledFg, text.getForeground());
+ text.setBackground(getColor(GREEN));
+ text.setForeground(getColor(CYAN));
+ assertEquals(getColor(GREEN), text.getBackground());
+ assertEquals(getColor(CYAN), text.getForeground());
+ text.setBackground(null);
+ text.setForeground(null);
+ assertEquals(disabledBg, text.getBackground());
+ assertEquals(disabledFg, text.getForeground());
+
+ // Dispose colors
+ disabledBg.dispose();
+ disabledFg.dispose();
+ enabledBg.dispose();
+ enabledFg.dispose();
+}
+
+@Test
public void test_setEditableZ(){
text.setEditable(true);
assertTrue(":a:", text.getEditable());

Back to the top