Skip to content

Commit 032bd55

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 b829b31 commit 032bd55

2 files changed

Lines changed: 69 additions & 28 deletions

File tree

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

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -186,6 +186,8 @@ public class StyledText extends Canvas {
186186
AccessibleAdapter accAdapter;
187187
MouseNavigator mouseNavigator;
188188
boolean middleClickPressed;
189+
private static final RGB COLOR_DISABLED_STATE = new RGB(54, 54, 54);
190+
private static final RGB COLOR_NON_EDITABLE_STATE = new RGB(48, 48,48);
189191

190192
//block selection
191193
boolean blockSelection;
@@ -8278,7 +8280,7 @@ public void setBackground(Color color) {
82788280
boolean backgroundDisabled = false;
82798281
if (!this.enabled && color == null) {
82808282
if (background != null) {
8281-
Color disabledBg = getDisplay().getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND);
8283+
Color disabledBg = new Color(COLOR_DISABLED_STATE);
82828284
if (background.equals(disabledBg)) {
82838285
return;
82848286
} else {
@@ -8287,11 +8289,13 @@ public void setBackground(Color color) {
82878289
}
82888290
}
82898291
}
8290-
customBackground = color != null && !this.insideSetEnableCall && !backgroundDisabled;
8292+
customBackground = color != null && !this.insideUpdateColorsCall && !backgroundDisabled;
82918293
background = color;
82928294
super.setBackground(color);
8293-
resetCache(0, content.getLineCount());
8294-
setCaretLocations();
8295+
if (content != null) {
8296+
resetCache(0, content.getLineCount());
8297+
setCaretLocations();
8298+
}
82958299
super.redraw();
82968300
}
82978301
/**
@@ -8772,6 +8776,54 @@ public void setDragDetect (boolean dragDetect) {
87728776
checkWidget ();
87738777
this.dragDetect = dragDetect;
87748778
}
8779+
8780+
/**
8781+
* Applies foreground and background colors based on the control's state.
8782+
* Custom foreground and background settings are preserved and not overridden.
8783+
*
8784+
* @param enabled {@code true} if the control is enabled; {@code false} otherwise
8785+
* @param editable {@code true} if the control is editable; {@code false} otherwise
8786+
* @since 3.134
8787+
*
8788+
*/
8789+
protected void updateColors(boolean enabled, boolean editable) {
8790+
this.insideUpdateColorsCall = true;
8791+
Display display = getDisplay();
8792+
try {
8793+
if (enabled && editable) {
8794+
if (!customBackground) {
8795+
Color bg = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
8796+
setBackground(bg);
8797+
}
8798+
if (!customForeground) {
8799+
Color fg = display.getSystemColor(SWT.COLOR_LIST_FOREGROUND);
8800+
setForeground(fg);
8801+
}
8802+
} else if (!enabled) {
8803+
if (!customBackground) {
8804+
Color bg = new Color(display, COLOR_DISABLED_STATE);
8805+
setBackground(bg);
8806+
}
8807+
if (!customForeground) {
8808+
Color fg = display.getSystemColor(SWT.COLOR_DARK_GRAY);
8809+
setForeground(fg);
8810+
}
8811+
} else if (!editable) {
8812+
8813+
if (!customBackground) {
8814+
Color bg = new Color(display, COLOR_NON_EDITABLE_STATE);
8815+
setBackground(bg);
8816+
}
8817+
if (!customForeground) {
8818+
Color fg = display.getSystemColor(SWT.COLOR_GRAY);
8819+
setForeground(fg);
8820+
}
8821+
}
8822+
} finally {
8823+
this.insideUpdateColorsCall = false;
8824+
}
8825+
}
8826+
87758827
/**
87768828
* Sets whether the widget content can be edited.
87778829
*
@@ -8785,28 +8837,13 @@ public void setDragDetect (boolean dragDetect) {
87858837
public void setEditable(boolean editable) {
87868838
checkWidget();
87878839
this.editable = editable;
8840+
updateColors(this.enabled, this.editable);
87888841
}
87898842
@Override
87908843
public void setEnabled(boolean enabled) {
87918844
super.setEnabled(enabled);
8792-
Display display = getDisplay();
87938845
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-
}
8846+
updateColors(this.enabled, this.editable);
88108847
}
88118848

88128849
@Override
@@ -8873,7 +8910,7 @@ public void setForeground(Color color) {
88738910
boolean foregroundDisabled = false;
88748911
if (!this.enabled && color == null) {
88758912
if (foreground != null) {
8876-
Color disabledFg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
8913+
Color disabledFg = getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
88778914
if (foreground.equals(disabledFg)) {
88788915
return;
88798916
} else {
@@ -8882,11 +8919,13 @@ public void setForeground(Color color) {
88828919
}
88838920
}
88848921
}
8885-
customForeground = color != null && !this.insideSetEnableCall && !foregroundDisabled;
8922+
customForeground = color != null && !this.insideUpdateColorsCall && !foregroundDisabled;
88868923
foreground = color;
88878924
super.setForeground(color);
8888-
resetCache(0, content.getLineCount());
8889-
setCaretLocations();
8925+
if (content != null) {
8926+
resetCache(0, content.getLineCount());
8927+
setCaretLocations();
8928+
}
88908929
super.redraw();
88918930
}
88928931
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.eclipse.swt.graphics.GC;
6767
import org.eclipse.swt.graphics.GlyphMetrics;
6868
import org.eclipse.swt.graphics.Point;
69+
import org.eclipse.swt.graphics.RGB;
6970
import org.eclipse.swt.graphics.Rectangle;
7071
import org.eclipse.swt.internal.BidiUtil;
7172
import org.eclipse.swt.layout.FillLayout;
@@ -101,6 +102,7 @@ public class Test_org_eclipse_swt_custom_StyledText extends Test_org_eclipse_swt
101102
final static String PLATFORM_LINE_DELIMITER = System.lineSeparator();
102103
private boolean listenerCalled;
103104
private boolean listener2Called;
105+
private static final RGB COLOR_DISABLED_STATE = new RGB(54, 54, 54);
104106

105107
@Override
106108
@BeforeEach
@@ -3245,8 +3247,8 @@ public void test_setDoubleClickEnabledZ(){
32453247
@Test
32463248
public void test_setEnabled(){
32473249
// Get colors
3248-
Color disabledBg = text.getDisplay().getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND);
3249-
Color disabledFg = text.getDisplay().getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND);
3250+
Color disabledBg = new Color(text.getDisplay(), COLOR_DISABLED_STATE);
3251+
Color disabledFg = text.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
32503252
Color enabledBg = text.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
32513253
Color enabledFg = text.getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND);
32523254

0 commit comments

Comments
 (0)