Skip to content

Commit 52b0611

Browse files
committed
Compute fg and bg colors for setEditable for StyledText
This commit adds the foreground and background color computation for setEditable. Earlier this was handled only for setEnabled. This commit also adds a different foreground and background color for StyledText when background is not editable. Fixes :#3132
1 parent 8e9c223 commit 52b0611

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2021 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -149,7 +149,7 @@ public class StyledText extends Canvas {
149149
/** False iff the widget is disabled */
150150
boolean enabled = true;
151151
/** True iff the widget is in the midst of being enabled or disabled */
152-
boolean insideSetEnableCall;
152+
boolean insideUpdateColorsCall;
153153
Clipboard clipboard;
154154
int clickCount;
155155
int autoScrollDirection = SWT.NULL; // the direction of autoscrolling (up, down, right, left)
@@ -8278,7 +8278,7 @@ public void setBackground(Color color) {
82788278
boolean backgroundDisabled = false;
82798279
if (!this.enabled && color == null) {
82808280
if (background != null) {
8281-
Color disabledBg = getDisplay().getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND);
8281+
Color disabledBg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
82828282
if (background.equals(disabledBg)) {
82838283
return;
82848284
} else {
@@ -8287,11 +8287,13 @@ public void setBackground(Color color) {
82878287
}
82888288
}
82898289
}
8290-
customBackground = color != null && !this.insideSetEnableCall && !backgroundDisabled;
8290+
customBackground = color != null && !this.insideUpdateColorsCall && !backgroundDisabled;
82918291
background = color;
82928292
super.setBackground(color);
8293-
resetCache(0, content.getLineCount());
8294-
setCaretLocations();
8293+
if (content != null) {
8294+
resetCache(0, content.getLineCount());
8295+
setCaretLocations();
8296+
}
82958297
super.redraw();
82968298
}
82978299
/**
@@ -8772,6 +8774,35 @@ public void setDragDetect (boolean dragDetect) {
87728774
checkWidget ();
87738775
this.dragDetect = dragDetect;
87748776
}
8777+
8778+
/**
8779+
* Applies foreground and background colors based on the control's state.
8780+
* Custom foreground and background settings are preserved and not overridden.
8781+
*
8782+
* @param enabled {@code true} if the control is enabled; {@code false} otherwise
8783+
* @param editable {@code true} if the control is editable; {@code false} otherwise
8784+
*
8785+
*/
8786+
private void updateColors(boolean enabled, boolean editable) {
8787+
this.insideUpdateColorsCall = true;
8788+
Display display = getDisplay();
8789+
try {
8790+
if (enabled && editable) {
8791+
if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
8792+
if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
8793+
} else if(!enabled) {
8794+
if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND));
8795+
if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
8796+
} else if(!editable) {
8797+
if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND));
8798+
if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
8799+
}
8800+
}
8801+
finally {
8802+
this.insideUpdateColorsCall = false;
8803+
}
8804+
}
8805+
87758806
/**
87768807
* Sets whether the widget content can be edited.
87778808
*
@@ -8785,28 +8816,13 @@ public void setDragDetect (boolean dragDetect) {
87858816
public void setEditable(boolean editable) {
87868817
checkWidget();
87878818
this.editable = editable;
8819+
updateColors(this.enabled, this.editable);
87888820
}
87898821
@Override
87908822
public void setEnabled(boolean enabled) {
87918823
super.setEnabled(enabled);
8792-
Display display = getDisplay();
87938824
this.enabled = enabled;
8794-
this.insideSetEnableCall = true;
8795-
try {
8796-
if (enabled && editable) {
8797-
if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
8798-
if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
8799-
} else if(!enabled) {
8800-
if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND));
8801-
if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND));
8802-
} else if(!editable) {
8803-
if (!customBackground) setBackground(display.getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND));
8804-
if (!customForeground) setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
8805-
}
8806-
}
8807-
finally {
8808-
this.insideSetEnableCall = false;
8809-
}
8825+
updateColors(this.enabled, this.editable);
88108826
}
88118827

88128828
@Override
@@ -8873,7 +8889,7 @@ public void setForeground(Color color) {
88738889
boolean foregroundDisabled = false;
88748890
if (!this.enabled && color == null) {
88758891
if (foreground != null) {
8876-
Color disabledFg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
8892+
Color disabledFg = getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
88778893
if (foreground.equals(disabledFg)) {
88788894
return;
88798895
} else {
@@ -8882,11 +8898,13 @@ public void setForeground(Color color) {
88828898
}
88838899
}
88848900
}
8885-
customForeground = color != null && !this.insideSetEnableCall && !foregroundDisabled;
8901+
customForeground = color != null && !this.insideUpdateColorsCall && !foregroundDisabled;
88868902
foreground = color;
88878903
super.setForeground(color);
8888-
resetCache(0, content.getLineCount());
8889-
setCaretLocations();
8904+
if (content != null) {
8905+
resetCache(0, content.getLineCount());
8906+
setCaretLocations();
8907+
}
88908908
super.redraw();
88918909
}
88928910
/**

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2025 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -3245,8 +3245,8 @@ public void test_setDoubleClickEnabledZ(){
32453245
@Test
32463246
public void test_setEnabled(){
32473247
// Get colors
3248-
Color disabledBg = text.getDisplay().getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND);
3249-
Color disabledFg = text.getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
3248+
Color disabledBg = text.getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
3249+
Color disabledFg = text.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
32503250
Color enabledBg = text.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
32513251
Color enabledFg = text.getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND);
32523252

0 commit comments

Comments
 (0)