Skip to content

Commit e2635ea

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 cc104be commit e2635ea

1 file changed

Lines changed: 63 additions & 24 deletions

File tree

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

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

Lines changed: 63 additions & 24 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;
@@ -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
@@ -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
/**

0 commit comments

Comments
 (0)