Bug 482308 - [win32] Main Menu not refreshed after deactivating Word
editor

Change-Id: Ifd1e947beece18e1a36aa4031e3c42c71508ddd9
Signed-off-by: Szymon Ptaszkiewicz <szymon.ptaszkiewicz@pl.ibm.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleClientSite.java b/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleClientSite.java
index 3ddae69..46bb952 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleClientSite.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleClientSite.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -122,13 +122,26 @@
 	isStatic = false;
 
 	listener = new Listener() {
+		private int nestedFocusEvents = 0;
 		public void handleEvent(Event e) {
 			switch (e.type) {
 			case SWT.Resize :
 			case SWT.Move :    onResize(e); break;
 			case SWT.Dispose : onDispose(e); break;
-			case SWT.FocusIn:  onFocusIn(e); break;
-			case SWT.FocusOut:  onFocusOut(e); break;
+			case SWT.FocusIn:
+				nestedFocusEvents++;
+				onFocusIn(e);
+				nestedFocusEvents--;
+				if (nestedFocusEvents == 0)
+					frame.onFocusIn(e);
+				break;
+			case SWT.FocusOut:
+				nestedFocusEvents++;
+				onFocusOut(e);
+				nestedFocusEvents--;
+				if (nestedFocusEvents == 0)
+					frame.onFocusOut(e);
+				break;
 			case SWT.Paint:    onPaint(e); break;
 			case SWT.Traverse: onTraverse(e); break;
 			case SWT.KeyDown: /* required for traversal */ break;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleFrame.java b/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleFrame.java
index 1f0fb62..c7bf8f4 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleFrame.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleFrame.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -57,6 +57,10 @@
 
 	private Listener listener;
 	
+	private long /*int*/ shellHandle;
+	private long /*int*/ oldMenuHandle;
+	private long /*int*/ newMenuHandle;
+
 	private static String CHECK_FOCUS = "OLE_CHECK_FOCUS"; //$NON-NLS-1$
 	private static String HHOOK = "OLE_HHOOK"; //$NON-NLS-1$
 	private static String HHOOKMSG = "OLE_HHOOK_MSG"; //$NON-NLS-1$
@@ -573,6 +577,15 @@
 	removeListener(SWT.Resize, listener);
 	removeListener(SWT.Move, listener);
 }
+void onFocusIn(Event e) {
+	if (OS.GetMenu(shellHandle) != newMenuHandle)
+		OS.SetMenu(shellHandle, newMenuHandle);
+}
+void onFocusOut(Event e) {
+	Control control = getDisplay().getFocusControl();
+	if (OS.GetMenu(shellHandle) != oldMenuHandle && control != null && control.handle != shellHandle)
+		OS.SetMenu(shellHandle, oldMenuHandle);
+}
 private void onResize(Event e) {
 	if (objIOleInPlaceActiveObject != null) {
 		RECT lpRect = new RECT();
@@ -765,8 +778,9 @@
 	}
 	if (hmenuShared == 0) return COM.E_FAIL;
 	
-	OS.SetMenu(handle, hmenuShared);
-	OS.DrawMenuBar(handle);
+	shellHandle = handle;
+	oldMenuHandle = menubar.handle;
+	newMenuHandle = hmenuShared;
 	
 	return COM.OleSetMenuDescriptor(holemenu, handle, hwndActiveObject, iOleInPlaceFrame.getAddress(), inPlaceActiveObject);
 }