Index: Source/NSApplication.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSApplication.m,v
retrieving revision 1.253
diff -u -r1.253 NSApplication.m
--- Source/NSApplication.m	28 Oct 2003 11:33:04 -0000	1.253
+++ Source/NSApplication.m	30 Oct 2003 04:49:13 -0000
@@ -931,7 +931,7 @@
 	  [[_inactive objectAtIndex: i] orderFrontRegardless];
 	}
       [_inactive removeAllObjects];
-      if (_hidden_key != nil
+      if ([self keyWindow] == nil && _hidden_key != nil
 	&& [[self windows] indexOfObjectIdenticalTo: _hidden_key] != NSNotFound)
 	{
 	  [_hidden_key makeKeyWindow];
@@ -949,8 +949,17 @@
 	}
       else if ([self mainWindow] != nil)
 	{
-	  [[self mainWindow] orderFront: self];
+	  [[self mainWindow] makeKeyAndOrderFront: self];
 	}
+      else
+        {
+	  /* We need give input focus to some window otherwise we'll never get
+	     keyboard events. FIXME: doesn't work. */
+	  NSWindow *menu_window= [[self mainMenu] window];
+	  NSDebugLLog(@"Focus", @"No key on activation - make menu key");
+	  [GSServerForWindow(menu_window) setinputfocus: 
+			      [menu_window windowNumber]];
+        }
 
       [nc postNotificationName: NSApplicationDidBecomeActiveNotification
 			object: self];
@@ -975,6 +984,10 @@
 	  _hidden_key = [self keyWindow];
 	  [_hidden_key resignKeyWindow];
 	}
+      // FIXME: main window is not saved for when the app is activated again.
+      // This is not a problem if it is also key, and I'm not sure if it
+      // is a problem at all. May be annoying in the case of workspace switch.
+      [[self mainWindow] resignMainWindow];
       for (i = 0; i < count; i++)
 	{
 	  NSModalSession theSession;
Index: Source/NSWindow.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSWindow.m,v
retrieving revision 1.294
diff -u -r1.294 NSWindow.m
--- Source/NSWindow.m	28 Oct 2003 21:33:52 -0000	1.294
+++ Source/NSWindow.m	30 Oct 2003 04:49:19 -0000
@@ -3074,7 +3079,6 @@
 		{
 		  /* Window Manager just deminiaturized us */
 		  [self deminiaturize: self];
-		  //[self _didDeminiaturize: self];
 		}
 	      if ([NSApp isHidden])
 		{
@@ -3085,6 +3089,24 @@
 		  NSDebugLLog(@"Focus", @"WM take focus while hiding");
 		  break;
 		}
+	      if (self == [[NSApp mainMenu] window] && [NSApp keyWindow])
+		{
+		  /* This might occur when the window manager just wants
+		     someone to become key, so it tells the main menu
+		     (typically the first menu in the list), but since we
+		     already have a window that was key before, use that
+		     instead */
+		  NSDebugLLog(@"Focus", @"Key window is already %d",
+			      [[NSApp keyWindow] windowNumber]);
+		  [[NSApp keyWindow] sendEvent: theEvent];
+		  break;
+		}
+	      if (self == [NSApp keyWindow])
+		  {
+		    NSDebugLLog(@"Focus", @"I'm (%d) already key window",
+				_windowNum);
+		    [GSServerForWindow(self) setinputfocus: _windowNum];
+		  }
 	      if ([self canBecomeKeyWindow] == YES)
 		{
 		  [self makeKeyWindow];
