Skip to content

Commit d4c145b

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 d4c145b

1 file changed

Lines changed: 45 additions & 21 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: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8290,8 +8290,10 @@ public void setBackground(Color color) {
82908290
customBackground = color != null && !this.insideSetEnableCall && !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,41 @@ 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+
* @since 3.134
8785+
*
8786+
*/
8787+
protected void updateColors(boolean enabled, boolean editable) {
8788+
this.insideSetEnableCall = true;
8789+
Display display = getDisplay();
8790+
try {
8791+
if (enabled && editable) {
8792+
if (!customBackground)
8793+
setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
8794+
if (!customForeground)
8795+
setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
8796+
} else if (!enabled) {
8797+
if (!customBackground)
8798+
setBackground(display.getSystemColor(SWT.COLOR_TEXT_DISABLED_BACKGROUND));
8799+
if (!customForeground)
8800+
setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DISABLED_FOREGROUND));
8801+
} else if (!editable) {
8802+
if (!customBackground)
8803+
setBackground(display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND));
8804+
if (!customForeground)
8805+
setForeground(display.getSystemColor(SWT.COLOR_GRAY));
8806+
}
8807+
} finally {
8808+
this.insideSetEnableCall = false;
8809+
}
8810+
}
8811+
87758812
/**
87768813
* Sets whether the widget content can be edited.
87778814
*
@@ -8785,28 +8822,13 @@ public void setDragDetect (boolean dragDetect) {
87858822
public void setEditable(boolean editable) {
87868823
checkWidget();
87878824
this.editable = editable;
8825+
updateColors(this.enabled, this.editable);
87888826
}
87898827
@Override
87908828
public void setEnabled(boolean enabled) {
87918829
super.setEnabled(enabled);
8792-
Display display = getDisplay();
87938830
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-
}
8831+
updateColors(this.enabled, this.editable);
88108832
}
88118833

88128834
@Override
@@ -8885,8 +8907,10 @@ public void setForeground(Color color) {
88858907
customForeground = color != null && !this.insideSetEnableCall && !foregroundDisabled;
88868908
foreground = color;
88878909
super.setForeground(color);
8888-
resetCache(0, content.getLineCount());
8889-
setCaretLocations();
8910+
if (content != null) {
8911+
resetCache(0, content.getLineCount());
8912+
setCaretLocations();
8913+
}
88908914
super.redraw();
88918915
}
88928916
/**

0 commit comments

Comments
 (0)