Skip to content

Commit d440697

Browse files
committed
Do not set IME font if the font is disposed
Prior to PR eclipse-platform#2062, the hFont variable in Caret::setIMEFont was retrieved directly from the font object (using font.handle). Since eclipse-platform#2602, hFont is fetched via SWTFontProvider, which throws an exception if the font is disposed. Previously, when the font was disposed, hFont (font.handle) would be zero, and the method would fall back to using defaultFont for setting the IME font. After eclipse-platform#2602, this fallback no longer works because the exception is thrown before the fallback can occur. This commit restores the intended behavior by setting hfont as zero if the font is disposed, preventing the exception. Fixes eclipse-platform#2323
1 parent 13934ee commit d440697

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,10 @@ public void setImage (Image image) {
509509
void setIMEFont () {
510510
if (!OS.IsDBLocale) return;
511511
long hFont = 0;
512-
if (font != null) hFont = SWTFontProvider.getFontHandle(font, getNativeZoom());
512+
if (font != null)
513+
{
514+
hFont = font.isDisposed() ? 0 : SWTFontProvider.getFontHandle(font, getNativeZoom());
515+
}
513516
if (hFont == 0) hFont = defaultFont ();
514517
long hwnd = parent.handle;
515518
long hIMC = OS.ImmGetContext (hwnd);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ public void test_setFontLorg_eclipse_swt_graphics_Font() {
108108
font.dispose();
109109
}
110110

111+
@Test
112+
public void test_CaretWithDisposedFontDoesNotThrow_issue2323() {
113+
try {
114+
Caret caret = new Caret(canvas, SWT.NONE);
115+
Font font = new Font(canvas.getDisplay(), "Default", 10, SWT.BOLD);
116+
shell.open();
117+
caret.setFont(font);
118+
font.dispose();
119+
canvas.setFocus();
120+
canvas.setCaret(caret);
121+
} catch (Exception e) {
122+
fail("Exception thrown when a canvase sets a caret with a disposed font: " + e.getMessage());
123+
}
124+
}
125+
111126
/* custom*/
112127
@Test
113128
public void test_consistency_MenuDetect() {

0 commit comments

Comments
 (0)