53832
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Canvas.java b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Canvas.java
index 866cc88..01268f5 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Canvas.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Canvas.java
@@ -93,12 +93,8 @@
 	checkWidget();
 	return caret;
 }
-
-short [] getIMCaretPos () {
-	if (caret == null) return super.getIMCaretPos ();
-	int x = caret.x + (caret.width <= 0 ? 2 : caret.width);
-	int y = caret.y + getFontAscent();
-	return new short[]{(short) x, (short) y};
+Caret getIMCaret () {
+	return caret;
 }
 void redrawWidget (int x, int y, int width, int height, boolean all) {
 	boolean isFocus = caret != null && caret.isFocusCaret ();
@@ -222,28 +218,8 @@
 }
 public void setFont (Font font) {
 	checkWidget();
-	super.setFont (font);
 	if (caret != null) caret.setFont (font);
-}
-void updateCaret () {
-	if (caret == null) return;
-	if (!OS.IsDBLocale) return;
-	short [] point = getIMCaretPos ();
-	int ptr1 = OS.XtMalloc (4);
-	OS.memmove (ptr1, point, 4);
-	int [] argList1 = {OS.XmNwidth, 0, OS.XmNheight, 0};
-	OS.XtGetValues (handle, argList1, argList1.length / 2);
-	short [] rect = new short[]{0, 0, (short) argList1 [1], (short) argList1 [3]};
-	int ptr2 = OS.XtMalloc (8);
-	OS.memmove (ptr2, rect, 8);
-	int [] argList2 = {OS.XmNspotLocation, ptr1, OS.XmNarea, ptr2};
-	OS.XmImSetValues (handle, argList2, argList2.length / 2);
-	int focusHandle = focusHandle ();
-	if (handle != focusHandle) {
-		OS.XmImSetValues (focusHandle, argList2, argList2.length / 2);
-	}
-	if (ptr1 != 0) OS.XtFree (ptr1);
-	if (ptr2 != 0) OS.XtFree (ptr2);
+	super.setFont (font);
 }
 int XExposure (int w, int client_data, int call_data, int continue_to_dispatch) {
 	boolean isFocus = caret != null && caret.isFocusCaret ();
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Caret.java b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Caret.java
index b12ad3d..ea234eb 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Caret.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Caret.java
@@ -305,7 +305,7 @@
 	if (isFocus) hideCaret ();
 	this.x = x; this.y = y;
 	this.width = width; this.height = height;
-	parent.updateCaret ();
+	if (isVisible) parent.updateIM ();
 	if (isFocus) showCaret ();
 }
 /**
@@ -353,14 +353,7 @@
 		error (SWT.ERROR_INVALID_ARGUMENT);
 	}
 	this.font = font;
-	if (isVisible && parent.hasFocus()) {
-		int [] argList = {OS.XmNfontList, font.handle};
-		OS.XmImSetValues (handle, argList, argList.length / 2);
-		int focusHandle = parent.focusHandle ();
-		if (handle != focusHandle) {
-			OS.XmImSetValues (focusHandle, argList, argList.length / 2);
-		}
-	}
+	if (isVisible) parent.updateIM ();
 }
 /**
  * Sets the image that the receiver will use to paint the caret
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Control.java
index d6d2bb9..6f1a074 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Control.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Control.java
@@ -697,11 +697,11 @@
 	return font;
 }
 
-int getFontAscent () {
+int getFontAscent (int font) {
 	
 	/* Create a font context to iterate over each element in the font list */
 	int [] buffer = new int [1];
-	if (!OS.XmFontListInitFontContext (buffer, font.handle)) {
+	if (!OS.XmFontListInitFontContext (buffer, font)) {
 		error (SWT.ERROR_NO_HANDLES);
 	}
 	int context = buffer [0];
@@ -816,6 +816,9 @@
 short [] getIMCaretPos () {
 	return new short[]{0, 0};
 }
+Caret getIMCaret () {
+	return null;
+}
 /**
  * Returns layout data which is associated with the receiver.
  *
@@ -1766,20 +1769,7 @@
 		boolean sameExtent = (width == argList [5]) && (height == argList [7]);
 		if (sameOrigin && sameExtent) return false;
 		OS.XtConfigureWidget (topHandle, x, y, width, height, argList [9]);
-		if (OS.IsDBLocale && !hasIMSupport ()) {
-			int [] argList1 = {OS.XmNwidth, 0, OS.XmNheight, 0};
-			OS.XtGetValues (handle, argList1, argList1.length / 2);
-			short [] rect = new short[]{0, 0, (short) argList1 [1], (short) argList1 [3]};
-			int ptr = OS.XtMalloc (8);
-			OS.memmove (ptr, rect, 8);
-			int [] argList2 = {OS.XmNarea, ptr};
-			OS.XmImSetValues (handle, argList2, argList2.length / 2);
-			int focusHandle = focusHandle ();
-			if (handle != focusHandle) {
-				OS.XmImSetValues (focusHandle, argList2, argList2.length / 2);
-			}
-			if (ptr != 0) OS.XtFree (ptr);
-		}
+		updateIM ();
 		if (!sameOrigin) sendEvent (SWT.Move);
 		if (!sameExtent) sendEvent (SWT.Resize);
 		return true;
@@ -1804,6 +1794,7 @@
 		height = Math.max (height - (argList [5] * 2), 1);
 		if (width == argList [1] && height == argList [3]) return false;
 		OS.XtResizeWidget (topHandle, width, height, argList [5]);
+		updateIM ();
 		sendEvent (SWT.Resize);
 		return true;
 	}
@@ -2003,13 +1994,7 @@
 	int fontHandle = fontHandle ();
 	int [] argList2 = {OS.XmNfontList, font.handle};
 	OS.XtSetValues (fontHandle, argList2, argList2.length / 2);
-	if (!hasIMSupport()) {
-		OS.XmImSetValues (handle, argList2, argList2.length / 2);
-		int focusHandle = focusHandle ();
-		if (handle != focusHandle) {
-			OS.XmImSetValues (focusHandle, argList2, argList2.length / 2);
-		}
-	}
+	updateIM ();
 
 	/* Restore the widget size */
 	OS.XtSetValues (handle, argList1, argList1.length / 2);
@@ -2691,6 +2676,55 @@
 		}
 	}
 }
+void updateIM () {
+	if (!OS.IsDBLocale) return;
+	if (!hasFocus ()) return;
+	int[] argList2;
+	int ptr1 = 0, ptr2 = 0;
+	if (hasIMSupport ()) {
+		argList2 = new int[]{
+			OS.XmNfontList, font.handle,
+//			OS.XmNforeground, getForegroundPixel (),
+//			OS.XmNbackground, getBackgroundPixel (),
+		};
+	} else {
+		int x = 0, y = 0;
+		Font font = this.font;
+		Caret caret = getIMCaret ();
+		if (caret != null) {
+			x += caret.x + (caret.width <= 0 ? 2 : caret.width);
+			y += caret.y;
+			if (caret.font != null) font = caret.font;
+		}
+		y += getFontAscent (font.handle);
+		short [] point = new short[]{(short) x, (short) y};
+		ptr1 = OS.XtMalloc (4);
+		OS.memmove (ptr1, point, 4);
+		int [] argList1 = {OS.XmNwidth, 0, OS.XmNheight, 0};
+		OS.XtGetValues (handle, argList1, argList1.length / 2);
+		short [] rect = new short[]{0, 0, (short) argList1 [1], (short) argList1 [3]};
+		ptr2 = OS.XtMalloc (8);
+		OS.memmove (ptr2, rect, 8);
+		/*
+		* Feature in Motif.  The XmNarea resource has to be set after
+		* the XmNspotLocation.
+		*/
+		argList2 = new int[]{
+			OS.XmNfontList, font.handle,
+//			OS.XmNforeground, getForegroundPixel (),
+//			OS.XmNbackground, getBackgroundPixel (),
+			OS.XmNspotLocation, ptr1,
+			OS.XmNarea, ptr2,
+		};
+	}
+	OS.XmImSetValues (handle, argList2, argList2.length / 2);
+	int focusHandle = focusHandle ();
+	if (handle != focusHandle) {
+		OS.XmImSetValues (focusHandle, argList2, argList2.length / 2);
+	}
+	if (ptr1 != 0) OS.XtFree (ptr1);
+	if (ptr2 != 0) OS.XtFree (ptr2);
+}
 int XButtonPress (int w, int client_data, int call_data, int continue_to_dispatch) {
 	Display display = getDisplay ();
 	Shell shell = getShell ();
@@ -2832,52 +2866,54 @@
 	return 0;
 }
 int xFocusIn (XFocusChangeEvent xEvent) {
-	if (!hasIMSupport()) {
+	/*
+	* Bug in Motif.  For some reason, when the widget font is
+	* not the default font and the widget loses focus, the
+	* X input method segment faults.  A BadFont (invalid font
+	* parameter) error is printed.  This problem also happens
+	* to XmText and XmTextField.  The fix is to change the
+	* X input method font back to the default font when the
+	* widget loses focus and restore it when the widget gets
+	* focus.
+	*/
+	updateIM ();
+	if (!hasIMSupport ()) {
 		int focusHandle = OS.XtWindowToWidget (xEvent.display, xEvent.window);
-		if (OS.IsDBLocale) {
-			short [] point = getIMCaretPos ();
-			int ptr1 = OS.XtMalloc (4);
-			OS.memmove (ptr1, point, 4);
-			int [] argList1 = {OS.XmNwidth, 0, OS.XmNheight, 0};
-			OS.XtGetValues (handle, argList1, argList1.length / 2);
-			short [] rect = new short[]{0, 0, (short) argList1 [1], (short) argList1 [3]};
-			int ptr2 = OS.XtMalloc (8);
-			OS.memmove (ptr2, rect, 8);
-			/*
-			* Bug in Motif. On Linux Japanese only, XmImSetFocusValues() causes
-			* a GP when the XmNfontList resources does not containt a FontSet.
-			* The fix is to call XmImSetValues() to set the values and then call
-			* XmImSetFocusValues() with no parameters to set the IME focus.
-			*/
-			int[] argList = {
-//				OS.XmNforeground, getForegroundPixel(),
-//				OS.XmNbackground, getBackgroundPixel(),
-				OS.XmNspotLocation, ptr1,
-				OS.XmNarea, ptr2,
-				OS.XmNfontList, font.handle,
-			};
-			OS.XmImSetValues (focusHandle, argList, argList.length / 2);
-			if (ptr1 != 0) OS.XtFree (ptr1);
-			if (ptr2 != 0) OS.XtFree (ptr2);
-		}
 		OS.XmImSetFocusValues (focusHandle, null, 0);
-	}
+	} 
 	sendEvent (SWT.FocusIn);
 	// widget could be disposed at this point
 	return 0;
 }
 int xFocusOut (XFocusChangeEvent xEvent) {
-	if (!hasIMSupport()) {
-		int focusHandle = OS.XtWindowToWidget (xEvent.display, xEvent.window);
-		OS.XmImUnsetFocus (focusHandle);
+	int focusHandle = OS.XtWindowToWidget (xEvent.display, xEvent.window);	
+	if (!hasIMSupport ()) OS.XmImUnsetFocus (focusHandle);
+
+	/*
+	* Bug in Motif.  For some reason, when the widget font is
+	* not the default font and the widget loses focus, the
+	* X input method segment faults.  A BadFont (invalid font
+	* parameter) error is printed.  This problem also happens
+	* to XmText and XmTextField.  The fix is to change the
+	* X input method font back to the default font when the
+	* widget loses focus and restore it when the widget gets
+	* focus.
+	*/
+	int fontList = defaultFont ().handle;
+	if (font.handle != fontList) {
+		int [] argList2 = {OS.XmNfontList, fontList};
+		OS.XmImSetValues (focusHandle, argList2, argList2.length / 2);
 	}
-	Display display = getDisplay();
+	
+	/* Set the focus out event */
+	Display display = getDisplay ();
 	if (display.postFocusOut) {
 		postEvent (SWT.FocusOut);
 	} else {
 		sendEvent (SWT.FocusOut);
 		// widget could be disposed at this point
 	}
+
 	return 0;
 }
 int XKeyPress (int w, int client_data, int call_data, int continue_to_dispatch) {
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java
index 0e5f070..e70d846 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java
@@ -439,7 +439,7 @@
 	}
 	short [] x = new short [1], y = new short [1];
 	OS.XmTextPosToXY (handle, position, x, y);
-	return new Point (x [0], y [0] - getFontAscent ());
+	return new Point (x [0], y [0] - getFontAscent (font.handle));
 }
 /**
  * Gets the position of the caret.