Index: src/display.c
===================================================================
RCS file: /cvs/gnome/metacity/src/display.c,v
retrieving revision 1.225
diff -p -u -r1.225 display.c
--- src/display.c	27 Aug 2004 02:17:49 -0000	1.225
+++ src/display.c	4 Sep 2004 21:06:11 -0000
@@ -329,6 +329,7 @@ meta_display_open (const char *name)
   display->pending_pings = NULL;
   display->autoraise_timeout_id = 0;
   display->focus_window = NULL;
+  display->previously_focused_window = NULL;
   display->expected_focus_window = NULL;
 
 #ifdef HAVE_XSYNC
Index: src/display.h
===================================================================
RCS file: /cvs/gnome/metacity/src/display.h,v
retrieving revision 1.122
diff -p -u -r1.122 display.h
--- src/display.h	27 Aug 2004 02:17:49 -0000	1.122
+++ src/display.h	4 Sep 2004 21:06:11 -0000
@@ -188,6 +188,10 @@ struct _MetaDisplay
    */
   MetaWindow *focus_window;
 
+ /* Window that was the actual window from focus events before focus_window
+  */
+  MetaWindow *previously_focused_window;
+
   /* window we are expecting a FocusIn event for
    */
   MetaWindow *expected_focus_window;
Index: src/window.c
===================================================================
RCS file: /cvs/gnome/metacity/src/window.c,v
retrieving revision 1.343
diff -p -u -r1.343 window.c
--- src/window.c	30 Aug 2004 04:56:07 -0000	1.343
+++ src/window.c	4 Sep 2004 21:06:20 -0000
@@ -4022,7 +4022,29 @@ meta_window_client_message (MetaWindow *
                     event->xclient.data.l[0]);
       if (event->xclient.data.l[0] == IconicState &&
           window->has_minimize_func)
-        meta_window_minimize (window);
+        {
+          meta_window_minimize (window);
+
+          /* If the window being minimized was the one with focus,
+           * then we should focus a new window.  We often receive this
+           * wm_change_state message when a user has clicked on the
+           * tasklist on the panel; in those cases, the current
+           * focus_window is the panel, but the previous focus_window
+           * will have been this one.  This is a special case where we
+           * need to manually handle focusing a new window because
+           * meta_window_minimize will get it wrong.
+           */
+          if (window->display->focus_window &&
+              (window->display->focus_window->type == META_WINDOW_DOCK ||
+               window->display->focus_window->type == META_WINDOW_DESKTOP) &&
+              window->display->previously_focused_window == window)
+            {
+              meta_topic (META_DEBUG_FOCUS,
+                          "Focusing default window because of minimization of former focus window %s, which was due to a wm_change_state client message\n",
+                      window->desc);
+              meta_workspace_focus_default_window (window->screen->active_workspace, window);
+            }
+        }
 
       return TRUE;
     }
@@ -4397,6 +4419,8 @@ meta_window_notify_focus (MetaWindow *wi
           meta_topic (META_DEBUG_FOCUS,
                       "* Focus --> NULL (was %s)\n", window->desc);
           
+          window->display->previously_focused_window =
+            window->display->focus_window;
           window->display->focus_window = NULL;
           window->has_focus = FALSE;
           if (window->frame)
