]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkwindow.c
Adapt cast macros to standard.
[~andy/gtk] / gtk / gtkwindow.c
index 29ce4afc992efed980bec48f397c43b5280ddc27..ea08918635bf0a7904aa632545cf7d2695d66378 100644 (file)
 #include "gtkbindings.h"
 #include "gtkmain.h"
 
+/* TODO: remove this define and assorted code in 1.3 and fix up the
+ * real culprits.
+ */
+#define FIXME_ZVT_ME_HARDER
+
 enum {
   SET_FOCUS,
   LAST_SIGNAL
@@ -48,17 +53,28 @@ enum {
   ARG_ALLOW_SHRINK,
   ARG_ALLOW_GROW,
   ARG_MODAL,
-  ARG_WIN_POS
+  ARG_WIN_POS,
+  ARG_DEFAULT_WIDTH,
+  ARG_DEFAULT_HEIGHT
 };
 
 typedef struct {
-  GdkGeometry    geometry;
-  GdkWindowHints mask;
-  GtkWidget     *widget;
+  GdkGeometry    geometry; /* Last set of geometry hints we set */
+  GdkWindowHints flags;
   gint           width;
   gint           height;
-  gint           last_width;
-  gint           last_height;
+} GtkWindowLastGeometryInfo;
+
+typedef struct {
+  /* Properties that the app has set on the window
+   */
+  GdkGeometry    geometry;     /* Geometry hints */
+  GdkWindowHints mask;
+  GtkWidget     *widget;       /* subwidget to which hints apply */
+  gint           width;                /* Default size */
+  gint           height;
+
+  GtkWindowLastGeometryInfo last;
 } GtkWindowGeometryInfo;
 
 static void gtk_window_class_init         (GtkWindowClass    *klass);
@@ -100,9 +116,30 @@ static gint gtk_window_client_event          (GtkWidget         *widget,
 static void gtk_window_check_resize       (GtkContainer      *container);
 static void gtk_window_real_set_focus     (GtkWindow         *window,
                                           GtkWidget         *focus);
+
 static void gtk_window_move_resize        (GtkWindow         *window);
-static void gtk_window_set_hints          (GtkWidget         *widget,
-                                          GtkRequisition    *requisition);
+static gboolean gtk_window_compare_hints  (GdkGeometry       *geometry_a,
+                                          guint              flags_a,
+                                          GdkGeometry       *geometry_b,
+                                          guint              flags_b);
+static void gtk_window_compute_default_size (GtkWindow       *window,
+                                            guint           *width,
+                                            guint           *height);
+static void  gtk_window_constrain_size      (GtkWindow       *window,
+                                            GdkGeometry     *geometry,
+                                            guint            flags,
+                                            gint             width,
+                                            gint             height,
+                                            gint            *new_width,
+                                            gint            *new_height);
+static void gtk_window_compute_hints      (GtkWindow         *window, 
+                                          GdkGeometry       *new_geometry,
+                                          guint             *new_flags);
+static void gtk_window_compute_reposition (GtkWindow         *window,
+                                          gint               new_width,
+                                          gint               new_height,
+                                          gint              *x,
+                                          gint              *y);
 
 static void gtk_window_read_rcfiles       (GtkWidget         *widget,
                                           GdkEventClient    *event);
@@ -171,7 +208,9 @@ gtk_window_class_init (GtkWindowClass *klass)
   gtk_object_add_arg_type ("GtkWindow::allow_grow", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_ALLOW_GROW);
   gtk_object_add_arg_type ("GtkWindow::modal", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_MODAL);
   gtk_object_add_arg_type ("GtkWindow::window_position", GTK_TYPE_WINDOW_POSITION, GTK_ARG_READWRITE, ARG_WIN_POS);
-
+  gtk_object_add_arg_type ("GtkWindow::default_width", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_DEFAULT_WIDTH);
+  gtk_object_add_arg_type ("GtkWindow::default_height", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_DEFAULT_HEIGHT);
+  
   window_signals[SET_FOCUS] =
     gtk_signal_new ("set_focus",
                     GTK_RUN_LAST,
@@ -258,15 +297,15 @@ gtk_window_set_arg (GtkObject  *object,
       break;
     case ARG_AUTO_SHRINK:
       window->auto_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
-      gtk_window_set_hints (GTK_WIDGET (window), &GTK_WIDGET (window)->requisition);
+      gtk_widget_queue_resize (GTK_WIDGET (window));
       break;
     case ARG_ALLOW_SHRINK:
       window->allow_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
-      gtk_window_set_hints (GTK_WIDGET (window), &GTK_WIDGET (window)->requisition);
+      gtk_widget_queue_resize (GTK_WIDGET (window));
       break;
     case ARG_ALLOW_GROW:
       window->allow_grow = (GTK_VALUE_BOOL (*arg) != FALSE);
-      gtk_window_set_hints (GTK_WIDGET (window), &GTK_WIDGET (window)->requisition);
+      gtk_widget_queue_resize (GTK_WIDGET (window));
       break;
     case ARG_MODAL:
       gtk_window_set_modal (window, GTK_VALUE_BOOL (*arg));
@@ -274,6 +313,12 @@ gtk_window_set_arg (GtkObject  *object,
     case ARG_WIN_POS:
       gtk_window_set_position (window, GTK_VALUE_ENUM (*arg));
       break;
+    case ARG_DEFAULT_WIDTH:
+      gtk_window_set_default_size (window, GTK_VALUE_INT (*arg), -2);
+      break;
+    case ARG_DEFAULT_HEIGHT:
+      gtk_window_set_default_size (window, -2, GTK_VALUE_INT (*arg));
+      break;
     default:
       break;
     }
@@ -290,6 +335,7 @@ gtk_window_get_arg (GtkObject  *object,
 
   switch (arg_id)
     {
+      GtkWindowGeometryInfo *info;
     case ARG_TYPE:
       GTK_VALUE_ENUM (*arg) = window->type;
       break;
@@ -311,6 +357,20 @@ gtk_window_get_arg (GtkObject  *object,
     case ARG_WIN_POS:
       GTK_VALUE_ENUM (*arg) = window->position;
       break;
+    case ARG_DEFAULT_WIDTH:
+      info = gtk_window_get_geometry_info (window, FALSE);
+      if (!info)
+       GTK_VALUE_INT (*arg) = -1;
+      else
+       GTK_VALUE_INT (*arg) = info->width;
+      break;
+    case ARG_DEFAULT_HEIGHT:
+      info = gtk_window_get_geometry_info (window, FALSE);
+      if (!info)
+       GTK_VALUE_INT (*arg) = -1;
+      else
+       GTK_VALUE_INT (*arg) = info->height;
+      break;
     default:
       arg->type = GTK_TYPE_INVALID;
       break;
@@ -322,7 +382,9 @@ gtk_window_new (GtkWindowType type)
 {
   GtkWindow *window;
 
-  window = gtk_type_new (gtk_window_get_type ());
+  g_return_val_if_fail (type >= GTK_WINDOW_TOPLEVEL && type <= GTK_WINDOW_POPUP, NULL);
+
+  window = gtk_type_new (GTK_TYPE_WINDOW);
 
   window->type = type;
 
@@ -420,7 +482,7 @@ gtk_window_set_policy (GtkWindow *window,
   window->allow_grow = (allow_grow != FALSE);
   window->auto_shrink = (auto_shrink != FALSE);
 
-  gtk_window_set_hints (GTK_WIDGET (window), &GTK_WIDGET (window)->requisition);
+  gtk_widget_queue_resize (GTK_WIDGET (window));
 }
 
 void
@@ -463,7 +525,8 @@ gtk_window_activate_focus (GtkWindow      *window)
 
   if (window->focus_widget)
     {
-      gtk_widget_activate (window->focus_widget);
+      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
+       gtk_widget_activate (window->focus_widget);
       return TRUE;
     }
 
@@ -476,7 +539,7 @@ gtk_window_activate_default (GtkWindow      *window)
   g_return_val_if_fail (window != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
 
-  if (window->default_widget)
+  if (window->default_widget && GTK_WIDGET_IS_SENSITIVE (window->default_widget))
     {
       gtk_widget_activate (window->default_widget);
       return TRUE;
@@ -549,6 +612,34 @@ gtk_window_remove_embedded_xid (GtkWindow *window, guint xid)
                              (GtkDestroyNotify) g_list_free : NULL);
 }
 
+void       
+gtk_window_reposition (GtkWindow *window,
+                      gint       x,
+                      gint       y)
+{
+  GtkWindowGeometryInfo *info;
+  
+  g_return_if_fail (window != NULL);
+  g_return_if_fail (GTK_IS_WINDOW (window));
+
+  /* keep this in sync with gtk_window_compute_reposition()
+   */
+  if (GTK_WIDGET_REALIZED (window))
+    {
+      info = gtk_window_get_geometry_info (window, TRUE);
+
+      if (!(info->last.flags & GDK_HINT_POS))
+       {
+         info->last.flags |= GDK_HINT_POS;
+         gdk_window_set_geometry_hints (GTK_WIDGET (window)->window,
+                                        &info->last.geometry,
+                                        info->last.flags);
+       }
+  
+      gdk_window_move (GTK_WIDGET (window)->window, x, y);
+    }
+}
+
 static void
 gtk_window_shutdown (GtkObject *object)
 {
@@ -609,13 +700,13 @@ gtk_window_set_transient_for  (GtkWindow *window,
 
   if (window->transient_parent)
     {
-      gtk_window_unset_transient_for (window);
-      
       if (GTK_WIDGET_REALIZED (window) && 
          GTK_WIDGET_REALIZED (window->transient_parent) && 
          (!parent || !GTK_WIDGET_REALIZED (parent)))
        gtk_window_transient_parent_unrealized (GTK_WIDGET (window->transient_parent),
                                                GTK_WIDGET (window));
+
+      gtk_window_unset_transient_for (window);
     }
 
   window->transient_parent = parent;
@@ -649,8 +740,9 @@ gtk_window_geometry_destroy (GtkWindowGeometryInfo *info)
   g_free (info);
 }
 
-static GtkWindowGeometryInfo *
-gtk_window_get_geometry_info (GtkWindow *window, gboolean create)
+static GtkWindowGeometryInfo*
+gtk_window_get_geometry_info (GtkWindow *window,
+                             gboolean   create)
 {
   GtkWindowGeometryInfo *info;
 
@@ -658,17 +750,16 @@ gtk_window_get_geometry_info (GtkWindow *window, gboolean create)
 
   if (!info && create)
     {
-      info = g_new (GtkWindowGeometryInfo, 1);
+      info = g_new0 (GtkWindowGeometryInfo, 1);
 
-      info->width = - 1;
-      info->height = -1;
-      info->last_width = -1;
-      info->last_height = -1;
+      info->width = 0;
+      info->height = 0;
+      info->last.width = -1;
+      info->last.height = -1;
       info->widget = NULL;
       info->mask = 0;
 
       gtk_object_set_data_full (GTK_OBJECT (window), 
-                               
                                "gtk-window-geometry",
                                info, 
                                (GtkDestroyNotify) gtk_window_geometry_destroy);
@@ -704,6 +795,8 @@ gtk_window_set_geometry_hints (GtkWindow       *window,
     info->geometry = *geometry;
 
   info->mask = geom_mask;
+
+  gtk_widget_queue_resize (GTK_WIDGET (window));
 }
 
 void       
@@ -713,12 +806,16 @@ gtk_window_set_default_size (GtkWindow   *window,
 {
   GtkWindowGeometryInfo *info;
 
-  g_return_if_fail (window != NULL);
+  g_return_if_fail (GTK_IS_WINDOW (window));
 
   info = gtk_window_get_geometry_info (window, TRUE);
 
-  info->width = width;
-  info->height = height;
+  if (width >= 0)
+    info->width = width;
+  if (height >= 0)
+    info->height = height;
+
+  gtk_widget_queue_resize (GTK_WIDGET (window));
 }
   
 static void
@@ -758,14 +855,53 @@ gtk_window_finalize (GtkObject *object)
 static void
 gtk_window_show (GtkWidget *widget)
 {
-  g_return_if_fail (widget != NULL);
-  g_return_if_fail (GTK_IS_WINDOW (widget));
+  GtkWindow *window = GTK_WINDOW (widget);
+  GtkContainer *container = GTK_CONTAINER (window);
+  gboolean need_resize;
 
   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
-  gtk_container_check_resize (GTK_CONTAINER (widget));
+
+  need_resize = container->need_resize || !GTK_WIDGET_REALIZED (widget);
+  container->need_resize = FALSE;
+
+  if (need_resize)
+    {
+      GtkWindowGeometryInfo *info = gtk_window_get_geometry_info (window, TRUE);
+      GtkAllocation allocation = { 0, 0 };
+      GdkGeometry new_geometry;
+      guint width, height, new_flags;
+
+      /* determine default size to initially show the window with */
+      gtk_widget_size_request (widget, NULL);
+      gtk_window_compute_default_size (window, &width, &height);
+
+      /* save away the last default size for later comparisions */
+      info->last.width = width;
+      info->last.height = height;
+
+      /* constrain size to geometry */
+      gtk_window_compute_hints (window, &new_geometry, &new_flags);
+      gtk_window_constrain_size (window,
+                                &new_geometry, new_flags,
+                                width, height,
+                                &width, &height);
+
+      /* and allocate the window */
+      allocation.width  = width;
+      allocation.height = height;
+      gtk_widget_size_allocate (widget, &allocation);
+      
+      if (GTK_WIDGET_REALIZED (widget))
+       gdk_window_resize (widget->window, width, height);
+      else
+       gtk_widget_realize (widget);
+    }
+  
+  gtk_container_check_resize (container);
+
   gtk_widget_map (widget);
 
-  if (GTK_WINDOW (widget)->modal)
+  if (window->modal)
     gtk_grab_add (widget);
 }
 
@@ -803,7 +939,6 @@ gtk_window_map (GtkWidget *widget)
       !GTK_WIDGET_MAPPED (window->bin.child))
     gtk_widget_map (window->bin.child);
 
-  gtk_window_set_hints (widget, &widget->requisition);
   gdk_window_show (widget->window);
 }
 
@@ -820,6 +955,8 @@ gtk_window_unmap (GtkWidget *widget)
 
   window = GTK_WINDOW (widget);
   window->use_uposition = TRUE;
+  window->resize_count = 0;
+  window->handling_resize = FALSE;
 }
 
 static void
@@ -829,11 +966,34 @@ gtk_window_realize (GtkWidget *widget)
   GdkWindowAttr attributes;
   gint attributes_mask;
   
-  g_return_if_fail (widget != NULL);
   g_return_if_fail (GTK_IS_WINDOW (widget));
+
+  window = GTK_WINDOW (widget);
+
+  /* ensure widget tree is properly size allocated */
+  if (widget->allocation.x == -1 &&
+      widget->allocation.y == -1 &&
+      widget->allocation.width == 1 &&
+      widget->allocation.height == 1)
+    {
+      GtkRequisition requisition;
+      GtkAllocation allocation = { 0, 0, 200, 200 };
+
+      gtk_widget_size_request (widget, &requisition);
+      if (requisition.width || requisition.height)
+       {
+         /* non-empty window */
+         allocation.width = requisition.width;
+         allocation.height = requisition.height;
+       }
+      gtk_widget_size_allocate (widget, &allocation);
+      
+      gtk_container_queue_resize (GTK_CONTAINER (widget));
+
+      g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
+    }
   
   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
-  window = GTK_WINDOW (widget);
   
   switch (window->type)
     {
@@ -907,11 +1067,6 @@ gtk_window_size_request (GtkWidget      *widget,
       requisition->width += child_requisition.width;
       requisition->height += child_requisition.height;
     }
-  else
-    {
-      if (!GTK_WIDGET_VISIBLE (window))
-       GTK_CONTAINER (window)->need_resize = TRUE;
-    }
 }
 
 static void
@@ -932,8 +1087,10 @@ gtk_window_size_allocate (GtkWidget     *widget,
     {
       child_allocation.x = GTK_CONTAINER (window)->border_width;
       child_allocation.y = GTK_CONTAINER (window)->border_width;
-      child_allocation.width = allocation->width - child_allocation.x * 2;
-      child_allocation.height = allocation->height - child_allocation.y * 2;
+      child_allocation.width =
+       MAX (1, (gint)allocation->width - child_allocation.x * 2);
+      child_allocation.height =
+       MAX (1, (gint)allocation->height - child_allocation.y * 2);
 
       gtk_widget_size_allocate (window->bin.child, &child_allocation);
     }
@@ -944,8 +1101,6 @@ gtk_window_configure_event (GtkWidget         *widget,
                            GdkEventConfigure *event)
 {
   GtkWindow *window;
-  GtkAllocation allocation;
-  gboolean need_expose = FALSE;
   
   g_return_val_if_fail (widget != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
@@ -953,59 +1108,43 @@ gtk_window_configure_event (GtkWidget         *widget,
   
   window = GTK_WINDOW (widget);
 
-  /* If the window was merely moved, do nothing */
-  if ((widget->allocation.width == event->width) &&
-      (widget->allocation.height == event->height))
+  /* we got a configure event specifying the new window size and position,
+   * in principle we have to distinguish 4 cases here:
+   * 1) the size didn't change and resize_count == 0
+   *    -> the window was merely moved (sometimes not even that)
+   * 2) the size didn't change and resize_count > 0
+   *    -> we requested a new size, but didn't get it
+   * 3) the size changed and resize_count > 0
+   *    -> we asked for a new size and we got one
+   * 4) the size changed and resize_count == 0
+   *    -> we got resized from outside the toolkit, and have to
+   *    accept that size since we don't want to fight neither the
+   *    window manager nor the user
+   * in the three latter cases we have to reallocate the widget tree,
+   * which happens in gtk_window_move_resize(), so we set a flag for
+   * that function and assign the new size. if resize_count > 1,
+   * we simply do nothing and wait for more configure events.
+   */
+
+  if (window->resize_count > 0 ||
+      widget->allocation.width != event->width ||
+      widget->allocation.height != event->height)
     {
-      if (window->resize_count == 0)      /* The window was merely moved */
-       return FALSE;
-      else
+      if (window->resize_count > 0)
+       window->resize_count -= 1;
+
+      if (window->resize_count == 0)
        {
-         /* We asked for a new size, which was rejected, so the
-          * WM sent us a synthetic configure event. We won't
-          * get the expose event we would normally get (since
-          * we have ForgetGravity), so we need to fake it.
-          */
-         need_expose = TRUE;
+         window->handling_resize = TRUE;
+         
+         widget->allocation.width = event->width;
+         widget->allocation.height = event->height;
+         
+         gtk_widget_queue_resize (widget);
        }
     }
-       
-  
-  window->handling_resize = TRUE;
-  
-  allocation.x = 0;
-  allocation.y = 0;
-  allocation.width = event->width;
-  allocation.height = event->height;
-  
-  gtk_widget_size_allocate (widget, &allocation);
-  
-  if (window->bin.child &&
-      GTK_WIDGET_VISIBLE (window->bin.child) &&
-      !GTK_WIDGET_MAPPED (window->bin.child))
-    gtk_widget_map (window->bin.child);
-  
-  if (window->resize_count > 0)
-      window->resize_count -= 1;
-  
-  if (need_expose)
-    {
-      GdkEvent temp_event;
-      temp_event.type = GDK_EXPOSE;
-      temp_event.expose.window = widget->window;
-      temp_event.expose.send_event = TRUE;
-      temp_event.expose.area.x = 0;
-      temp_event.expose.area.y = 0;
-      temp_event.expose.area.width = event->width;
-      temp_event.expose.area.height = event->height;
-      temp_event.expose.count = 0;
-      
-      gtk_widget_event (widget, &temp_event);
-    }
 
-  window->handling_resize = FALSE;
-  
-  return FALSE;
+  return TRUE;
 }
 
 static gint
@@ -1024,7 +1163,9 @@ gtk_window_key_press_event (GtkWidget   *widget,
 
   handled = FALSE;
   
-  if (window->focus_widget && GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
+  if (window->focus_widget &&
+      window->focus_widget != widget &&
+      GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
     {
       handled = gtk_widget_event (window->focus_widget, (GdkEvent*) event);
     }
@@ -1039,22 +1180,23 @@ gtk_window_key_press_event (GtkWidget   *widget,
        case GDK_space:
          if (window->focus_widget)
            {
-             gtk_widget_activate (window->focus_widget);
+             if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
+               gtk_widget_activate (window->focus_widget);
              handled = TRUE;
            }
          break;
        case GDK_Return:
        case GDK_KP_Enter:
-         if (window->default_widget &&
-             (!window->focus_widget || 
-              !GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget)))
+         if (window->default_widget && GTK_WIDGET_IS_SENSITIVE (window->default_widget) &&
+             (!window->focus_widget || !GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget)))
            {
              gtk_widget_activate (window->default_widget);
              handled = TRUE;
            }
           else if (window->focus_widget)
            {
-             gtk_widget_activate (window->focus_widget);
+             if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
+               gtk_widget_activate (window->focus_widget);
              handled = TRUE;
            }
          break;
@@ -1062,20 +1204,28 @@ gtk_window_key_press_event (GtkWidget   *widget,
        case GDK_Down:
        case GDK_Left:
        case GDK_Right:
+       case GDK_KP_Up:
+       case GDK_KP_Down:
+       case GDK_KP_Left:
+       case GDK_KP_Right:
        case GDK_Tab:
        case GDK_ISO_Left_Tab:
          switch (event->keyval)
            {
            case GDK_Up:
+           case GDK_KP_Up:
              direction = GTK_DIR_UP;
              break;
            case GDK_Down:
+           case GDK_KP_Down:
              direction = GTK_DIR_DOWN;
              break;
            case GDK_Left:
+           case GDK_KP_Left:
              direction = GTK_DIR_LEFT;
              break;
            case GDK_Right:
+           case GDK_KP_Right:
              direction = GTK_DIR_RIGHT;
              break;
            case GDK_Tab:
@@ -1118,7 +1268,9 @@ gtk_window_key_release_event (GtkWidget   *widget,
   
   window = GTK_WINDOW (widget);
   handled = FALSE;
-  if (window->focus_widget && GTK_WIDGET_SENSITIVE (window->focus_widget))
+  if (window->focus_widget &&
+      window->focus_widget != widget &&
+      GTK_WIDGET_SENSITIVE (window->focus_widget))
     {
       handled = gtk_widget_event (window->focus_widget, (GdkEvent*) event);
     }
@@ -1170,7 +1322,9 @@ gtk_window_focus_in_event (GtkWidget     *widget,
   if (GTK_WIDGET_VISIBLE (widget))
     {
       window = GTK_WINDOW (widget);
-      if (window->focus_widget && !GTK_WIDGET_HAS_FOCUS (window->focus_widget))
+      if (window->focus_widget &&
+         window->focus_widget != widget &&
+         !GTK_WIDGET_HAS_FOCUS (window->focus_widget))
        {
          fevent.type = GDK_FOCUS_CHANGE;
          fevent.window = window->focus_widget->window;
@@ -1195,7 +1349,9 @@ gtk_window_focus_out_event (GtkWidget     *widget,
   g_return_val_if_fail (event != NULL, FALSE);
 
   window = GTK_WINDOW (widget);
-  if (window->focus_widget && GTK_WIDGET_HAS_FOCUS (window->focus_widget))
+  if (window->focus_widget &&
+      window->focus_widget != widget &&
+      GTK_WIDGET_HAS_FOCUS (window->focus_widget))
     {
       fevent.type = GDK_FOCUS_CHANGE;
       fevent.window = window->focus_widget->window;
@@ -1279,236 +1435,9 @@ gtk_window_check_resize (GtkContainer *container)
   g_return_if_fail (GTK_IS_WINDOW (container));
 
   window = GTK_WINDOW (container);
-  if (!window->handling_resize)
-    {
-      if (GTK_WIDGET_VISIBLE (container))
-       gtk_window_move_resize (window);
-      else
-       GTK_CONTAINER (window)->need_resize = TRUE;
-    }
-}
-
-/* FIXME: we leave container->resize_widgets set under some
-   circumstances ? */
-static void
-gtk_window_move_resize (GtkWindow *window)
-{
-  GtkWidget    *widget;
-  GtkWindowGeometryInfo *info;
-  GtkRequisition requisition;
-  GtkContainer *container;
-  gint x, y;
-  gint width, height;
-  gint new_width, new_height;
-  gint min_width, min_height;
-  gint screen_width;
-  gint screen_height;
-  gboolean needed_resize;
-  gboolean size_changed;
-
-  g_return_if_fail (window != NULL);
-  g_return_if_fail (GTK_IS_WINDOW (window));
-
-  widget = GTK_WIDGET (window);
-  container = GTK_CONTAINER (widget);
-
-  info = gtk_window_get_geometry_info (window, FALSE);
-  
-  /* Remember old size, to know if we have to reset hints */
-  if (info && (info->last_width > 0))
-    width = info->last_width;
-  else
-    width = widget->requisition.width;
-
-  if (info && (info->last_height > 0))
-    height = info->last_height;
-  else
-    height = widget->requisition.height;
-
-  size_changed = FALSE;
-
-  gtk_widget_size_request (widget, &requisition);
 
-  size_changed |= requisition.width != widget->requisition.width;
-  size_changed |= requisition.height != widget->requisition.height;
-  widget->requisition = requisition;
-
-  /* Figure out the new desired size */
-
-  if (info && info->width > 0)
-    {
-      size_changed |= width != info->last_width;
-      info->last_width = width;
-      new_width = info->width;
-    }
-  else
-    {
-      size_changed |= width != widget->requisition.width;
-      new_width = widget->requisition.width;
-    }
-
-  if (info && info->height > 0)
-    {
-      size_changed |= height != info->last_height;
-      info->last_height = height;
-      new_height = info->height;
-    }
-  else
-    {
-      size_changed |= height != widget->requisition.height;
-      new_height = widget->requisition.height;
-    }
-
-  /* Figure out the new minimum size */
-
-  if (info && (info->mask & (GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE)))
-    {
-      if (info->mask && GDK_HINT_MIN_SIZE)
-       {
-         min_width = info->geometry.min_width;
-         min_height = info->geometry.min_height;
-       }
-      else
-       {
-         min_width = info->geometry.base_width;
-         min_height = info->geometry.base_height;
-       }
-
-      if (info->widget)
-       {
-         min_width += widget->requisition.width - info->widget->requisition.width;
-         min_height += widget->requisition.height - info->widget->requisition.height;
-       }
-    }
-  else
-    {
-      min_width = widget->requisition.width;
-      min_height = widget->requisition.height;
-    }
-
-  if (size_changed)
-    {
-      gboolean saved_use_upos;
-
-      saved_use_upos = window->use_uposition;
-      gtk_window_set_hints (widget, &widget->requisition);
-      window->use_uposition = saved_use_upos;
-    }
-  
-  x = -1;
-  y = -1;
-  
-  if (window->use_uposition)
-    switch (window->position)
-      {
-      case GTK_WIN_POS_CENTER:
-       x = (gdk_screen_width () - new_width) / 2;
-       y = (gdk_screen_height () - new_height) / 2;
-       gtk_widget_set_uposition (widget, x, y);
-       break;
-      case GTK_WIN_POS_MOUSE:
-       gdk_window_get_pointer (NULL, &x, &y, NULL);
-       
-       x -= new_width / 2;
-       y -= new_height / 2;
-       
-       screen_width = gdk_screen_width ();
-       screen_height = gdk_screen_height ();
-       
-       if (x < 0)
-         x = 0;
-       else if (x > (screen_width - new_width))
-         x = screen_width - new_width;
-       
-       if (y < 0)
-         y = 0;
-       else if (y > (screen_height - new_height))
-         y = screen_height - new_height;
-       
-       gtk_widget_set_uposition (widget, x, y);
-       break;
-      }
-
-  /* Now, do the resizing */
-
-  needed_resize = container->need_resize;
-  container->need_resize = FALSE;
-
-  if ((new_width == 0) || (new_height == 0))
-    {
-      new_width = 200;
-      new_height = 200;
-    }
-  
-  if (!GTK_WIDGET_REALIZED (window))
-    {
-      GtkAllocation allocation;
-
-      allocation.x = 0;
-      allocation.y = 0;
-      allocation.width = new_width;
-      allocation.height = new_height;
-      
-      gtk_widget_size_allocate (widget, &allocation);
-
-      return;
-    }
-  
-  gdk_window_get_geometry (widget->window, NULL, NULL, &width, &height, NULL);
-  
-  /* As an optimization, we don't try to get a new size from the
-   * window manager if we asked for the same size last time and
-   * didn't get it */
-
-  if (size_changed && 
-      (((window->auto_shrink &&
-       ((width != new_width) ||
-        (height != new_height)))) ||
-       ((width < min_width) ||
-       (height < min_height))))
-    {
-      window->resize_count += 1;
-
-      if (!window->auto_shrink)
-       {
-         new_width = MAX(width, min_width);
-         new_height = MAX(height, min_height);
-       }
-      
-      if ((x != -1) && (y != -1))
-       gdk_window_move_resize (widget->window, x, y,
-                               new_width,
-                               new_height);
-      else
-        gdk_window_resize (widget->window,
-                          new_width,
-                          new_height);
-    }
-  else if (needed_resize)
-    {
-      /* The windows contents changed size while it was not
-       * visible, so reallocate everything, since we didn't
-       * keep track of what changed
-       */
-      GtkAllocation allocation;
-      
-      allocation.x = 0;
-      allocation.y = 0;
-      allocation.width = new_width;
-      allocation.height = new_height;
-      
-      gtk_widget_size_allocate (widget, &allocation);
-      gdk_window_resize (widget->window,
-                        new_width,
-                        new_height);
-    }
-  else
-    {
-      if ((x != -1) && (y != -1))
-       gdk_window_move (widget->window, x, y);
-      
-      gtk_container_resize_children (GTK_CONTAINER (window));
-    }
+  if (GTK_WIDGET_VISIBLE (container))
+    gtk_window_move_resize (window);
 }
 
 static void
@@ -1579,105 +1508,591 @@ gtk_window_real_set_focus (GtkWindow *window,
     gtk_widget_queue_draw (window->default_widget);
 }
 
+/*********************************
+ * Functions related to resizing *
+ *********************************/
+
 static void
-gtk_window_set_hints (GtkWidget      *widget,
-                     GtkRequisition *requisition)
+gtk_window_move_resize (GtkWindow *window)
 {
-  GtkWindow *window;
-  GtkWidgetAuxInfo *aux_info;
-  GtkWindowGeometryInfo *geometry_info;
+  GtkWidget *widget;
+  GtkContainer *container;
+  GtkWindowGeometryInfo *info;
+  GtkWindowLastGeometryInfo saved_last_info;
   GdkGeometry new_geometry;
-  gint flags;
-  gint ux, uy;
-  gint extra_width = 0;
-  gint extra_height = 0;
+  guint new_flags;
+  gint x, y;
+  gint width, height;
+  gint new_width, new_height;
+  gboolean default_size_changed = FALSE;
+  gboolean hints_changed = FALSE;
 
-  g_return_if_fail (widget != NULL);
-  g_return_if_fail (GTK_IS_WINDOW (widget));
-  g_return_if_fail (requisition != NULL);
+  g_return_if_fail (GTK_IS_WINDOW (window));
+  g_return_if_fail (GTK_WIDGET_REALIZED (window));
 
-  if (GTK_WIDGET_REALIZED (widget))
+  widget = GTK_WIDGET (window);
+  container = GTK_CONTAINER (widget);
+  info = gtk_window_get_geometry_info (window, TRUE);
+  saved_last_info = info->last;
+
+  gtk_widget_size_request (widget, NULL);
+  gtk_window_compute_default_size (window, &new_width, &new_height);
+  
+  if (info->last.width < 0 ||
+      info->last.width != new_width ||
+      info->last.height != new_height)
     {
-      window = GTK_WINDOW (widget);
+      default_size_changed = TRUE;
+      info->last.width = new_width;
+      info->last.height = new_height;
 
-      geometry_info = gtk_window_get_geometry_info (GTK_WINDOW (widget), FALSE);
+      /* We need to force a reposition in this case
+       */
+      if (window->position == GTK_WIN_POS_CENTER_ALWAYS)
+       window->use_uposition = TRUE;
+    }
+  
+  /* Compute new set of hints for the window
+   */
+  gtk_window_compute_hints (window, &new_geometry, &new_flags);
+  if (!gtk_window_compare_hints (&info->last.geometry, info->last.flags,
+                                &new_geometry, new_flags))
+    {
+      hints_changed = TRUE;
+      info->last.geometry = new_geometry;
+      info->last.flags = new_flags;
+    }
+
+  /* From the default size and the allocation, figure out the size
+   * the window should be.
+   */
+  if (!default_size_changed ||
+      (!window->auto_shrink &&
+       new_width <= widget->allocation.width &&
+       new_height <= widget->allocation.height))
+    {
+      new_width = widget->allocation.width;
+      new_height = widget->allocation.height;
+    }
+
+  /* constrain the window size to the specified geometry */
+  gtk_window_constrain_size (window,
+                            &new_geometry, new_flags,
+                            new_width, new_height,
+                            &new_width, &new_height);
+
+  /* compute new window position if a move is required
+   */
+  gtk_window_compute_reposition (window, new_width, new_height, &x, &y);
+  if (x != 1 && y != -1 && !(new_flags & GDK_HINT_POS))
+    {
+      new_flags |= GDK_HINT_POS;
+      hints_changed = TRUE;
+    }
 
-      if (geometry_info)
+
+  /* handle actual resizing:
+   * - handle reallocations due to configure events
+   * - figure whether we need to request a new window size
+   * - handle simple resizes within our widget tree
+   * - reposition window if neccessary
+   */
+  width = widget->allocation.width;
+  height = widget->allocation.height;
+
+  if (window->handling_resize)
+    { 
+      GtkAllocation allocation;
+      
+      /* if we are just responding to a configure event, which
+       * might be due to a resize by the window manager, the
+       * user, or a response to a resizing request we made
+       * earlier, we go ahead, allocate the new size and we're done
+       * (see gtk_window_configure_event() for more details).
+       */
+      
+      window->handling_resize = FALSE;
+      
+      allocation = widget->allocation;
+      
+      gtk_widget_size_allocate (widget, &allocation);
+      gtk_widget_queue_draw (widget);
+
+#ifdef FIXME_ZVT_ME_HARDER
+      if ((default_size_changed || hints_changed) && (width != new_width || height != new_height))
        {
-         flags = geometry_info->mask;
-         new_geometry = geometry_info->geometry;
+         /* We could be here for two reasons
+          *  1) We coincidentally got a resize while handling
+          *     another resize.
+          *  2) Our computation of default_size_changed was completely
+          *     screwed up, probably because one of our children
+          *     is broken (i.e. changes requisition during
+          *     size allocation). It's probably a zvt widget.
+          *
+          * For 1), we could just go ahead and ask for the
+          * new size right now, but doing that for 2)
+          * might well be fighting the user (and can even
+          * trigger a loop). Since we really don't want to
+          * do that, we requeue a resize in hopes that
+          * by the time it gets handled, the child has seen
+          * the light and is willing to go along with the
+          * new size. (this happens for the zvt widget, since
+          * the size_allocate() above will have stored the
+          * requisition corresponding to the new size in the
+          * zvt widget)
+          *
+          * This doesn't buy us anything for 1), but it shouldn't
+          * hurt us too badly, since it is what would have
+          * happened if we had gotten the configure event before
+          * the new size had been set.
+          */
+         
+         if (x != -1 && y != -1)
+           gdk_window_move (widget->window, x, y);
 
-         if (geometry_info->widget)
-           {
-             extra_width = requisition->width - geometry_info->widget->requisition.width;
-             extra_height = requisition->height - geometry_info->widget->requisition.height;
-           }
+         /* we have to preserve the values and flags that are used
+          * for computation of default_size_changed and hints_changed
+          */
+
+         info->last = saved_last_info;
+         
+         gtk_widget_queue_resize (widget);
+
+         return;
        }
+#endif /* FIXME_ZVT_ME_HARDER */
+    }
+
+  /* Now set hints if necessary
+   */
+  if (hints_changed)
+    gdk_window_set_geometry_hints (widget->window,
+                                  &new_geometry,
+                                  new_flags);
+
+  if ((default_size_changed || hints_changed) &&
+      (width != new_width || height != new_height))
+    {
+      /* given that (width != new_width || height != new_height), we are in one
+       * of the following situations:
+       * 
+       * default_size_changed
+       *   our requisition has changed and we need a different window size,
+       *   so we request it from the window manager.
+       *
+       * !default_size_changed
+       *   the window manager wouldn't assign us the size we requested, in this
+       *   case we don't try to request a new size with every resize.
+       *
+       * !default_size_changed && hints_changed
+       *   the window manager rejects our size, but we have just changed the
+       *   window manager hints, so there's a certain chance our request will
+       *   be honoured this time, so we try again.
+       */
+      
+      /* request a new window size */
+      if (x != -1 && y != -1)
+       gdk_window_move_resize (GTK_WIDGET (window)->window, x, y, new_width, new_height);
       else
-       flags = 0;
+       gdk_window_resize (GTK_WIDGET (window)->window, new_width, new_height);
+      window->resize_count += 1;
       
-      ux = 0;
-      uy = 0;
+      /* we are now awaiting the new configure event in response to our
+       * resizing request. the configure event will cause a new resize
+       * with ->handling_resize=TRUE.
+       * until then, we want to
+       * - discard expose events
+       * - coalesce resizes for our children
+       * - defer any window resizes until the configure event arrived
+       * to achive this, we queue a resize for the window, but remove its
+       * resizing handler, so resizing will not be handled from the next
+       * idle handler but when the configure event arrives.
+       *
+       * FIXME: we should also dequeue the pending redraws here, since
+       * we handle those ourselves in ->handling_resize==TRUE.
+       */
+      gtk_widget_queue_resize (GTK_WIDGET (container));
+      if (container->resize_mode == GTK_RESIZE_QUEUE)
+       gtk_container_dequeue_resize_handler (container);
+    }
+  else
+    {
+      if (x != -1 && y != -1)
+       gdk_window_move (widget->window, x, y);
 
-      aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
-      if (aux_info && (aux_info->x != -1) && (aux_info->y != -1))
+      if (container->resize_widgets)
+       gtk_container_resize_children (GTK_CONTAINER (window));
+    }
+}
+
+/* Compare two sets of Geometry hints for equality.
+ */
+static gboolean
+gtk_window_compare_hints (GdkGeometry *geometry_a,
+                         guint        flags_a,
+                         GdkGeometry *geometry_b,
+                         guint        flags_b)
+{
+  if (flags_a != flags_b)
+    return FALSE;
+  
+  if ((flags_a & GDK_HINT_MIN_SIZE) &&
+      (geometry_a->min_width != geometry_b->min_width ||
+       geometry_a->min_height != geometry_b->min_height))
+    return FALSE;
+
+  if ((flags_a & GDK_HINT_MAX_SIZE) &&
+      (geometry_a->max_width != geometry_b->max_width ||
+       geometry_a->max_height != geometry_b->max_height))
+    return FALSE;
+
+  if ((flags_a & GDK_HINT_BASE_SIZE) &&
+      (geometry_a->base_width != geometry_b->base_width ||
+       geometry_a->base_height != geometry_b->base_height))
+    return FALSE;
+
+  if ((flags_a & GDK_HINT_ASPECT) &&
+      (geometry_a->min_aspect != geometry_b->min_aspect ||
+       geometry_a->max_aspect != geometry_b->max_aspect))
+    return FALSE;
+
+  if ((flags_a & GDK_HINT_RESIZE_INC) &&
+      (geometry_a->width_inc != geometry_b->width_inc ||
+       geometry_a->height_inc != geometry_b->height_inc))
+    return FALSE;
+
+  return TRUE;
+}
+
+/* Compute the default_size for a window. The result will
+ * be stored in *width and *height. The default size is
+ * the size the window should have when initially mapped.
+ * This routine does not attempt to constrain the size
+ * to obey the geometry hints - that must be done elsewhere.
+ */
+static void 
+gtk_window_compute_default_size (GtkWindow       *window,
+                                guint           *width,
+                                guint           *height)
+{
+  GtkRequisition requisition;
+  GtkWindowGeometryInfo *info;
+  
+  gtk_widget_get_child_requisition (GTK_WIDGET (window), &requisition);
+  *width = requisition.width;
+  *height = requisition.height;
+
+  info = gtk_window_get_geometry_info (window, FALSE);
+  
+  if (*width == 0 && *height == 0)
+    {
+      /* empty window */
+      *width = 200;
+      *height = 200;
+    }
+  
+  if (info)
+    {
+      *width = info->width > 0 ? info->width : *width;
+      *height = info->height > 0 ? info->height : *height;
+    }
+}
+
+/* Constrain a window size to obey the hints passed in geometry
+ * and flags. The result will be stored in *new_width and *new_height
+ *
+ * This routine is partially borrowed from fvwm.
+ *
+ * Copyright 1993, Robert Nation
+ *     You may use this code for any purpose, as long as the original
+ *     copyright remains in the source code and all documentation
+ *
+ * which in turn borrows parts of the algorithm from uwm
+ */
+static void 
+gtk_window_constrain_size (GtkWindow   *window,
+                          GdkGeometry *geometry,
+                          guint        flags,
+                          gint         width,
+                          gint         height,
+                          gint        *new_width,
+                          gint        *new_height)
+{
+  gint min_width = 0;
+  gint min_height = 0;
+  gint base_width = 0;
+  gint base_height = 0;
+  gint xinc = 1;
+  gint yinc = 1;
+  gint max_width = G_MAXINT;
+  gint max_height = G_MAXINT;
+  
+#define FLOOR(value, base)     ( ((gint) ((value) / (base))) * (base) )
+
+  if ((flags & GDK_HINT_BASE_SIZE) && (flags & GDK_HINT_MIN_SIZE))
+    {
+      base_width = geometry->base_width;
+      base_height = geometry->base_height;
+      min_width = geometry->min_width;
+      min_height = geometry->min_height;
+    }
+  else if (flags & GDK_HINT_BASE_SIZE)
+    {
+      base_width = geometry->base_width;
+      base_height = geometry->base_height;
+      min_width = geometry->base_width;
+      min_height = geometry->base_height;
+    }
+  else if (flags & GDK_HINT_MIN_SIZE)
+    {
+      base_width = geometry->min_width;
+      base_height = geometry->min_height;
+      min_width = geometry->min_width;
+      min_height = geometry->min_height;
+    }
+
+  if (flags & GDK_HINT_MAX_SIZE)
+    {
+      max_width = geometry->max_width ;
+      max_height = geometry->max_height;
+    }
+
+  if (flags & GDK_HINT_RESIZE_INC)
+    {
+      xinc = MAX (xinc, geometry->width_inc);
+      yinc = MAX (yinc, geometry->height_inc);
+    }
+  
+  /* clamp width and height to min and max values
+   */
+  width = CLAMP (width, min_width, max_width);
+  height = CLAMP (height, min_height, max_height);
+  
+  /* shrink to base + N * inc
+   */
+  width = base_width + FLOOR (width - base_width, xinc);
+  height = base_height + FLOOR (height - base_height, yinc);
+
+  /* constrain aspect ratio, according to:
+   *
+   *                width     
+   * min_aspect <= -------- <= max_aspect
+   *                height    
+   */
+  
+  if (flags & GDK_HINT_ASPECT &&
+      geometry->min_aspect > 0 &&
+      geometry->max_aspect > 0)
+    {
+      gint delta;
+
+      if (geometry->min_aspect * height > width)
        {
-         ux = aux_info->x;
-         uy = aux_info->y;
-         flags |= GDK_HINT_POS;
+         delta = FLOOR (height - width * geometry->min_aspect, yinc);
+         if (height - delta >= min_height)
+           height -= delta;
+         else
+           { 
+             delta = FLOOR (height * geometry->min_aspect - width, xinc);
+             if (width + delta <= max_width) 
+               width += delta;
+           }
        }
       
-      if (flags & GDK_HINT_BASE_SIZE)
+      if (geometry->max_aspect * height < width)
        {
-         new_geometry.base_width += extra_width;
-         new_geometry.base_height += extra_height;
+         delta = FLOOR (width - height * geometry->max_aspect, xinc);
+         if (width - delta >= min_width) 
+           width -= delta;
+         else
+           {
+             delta = FLOOR (width / geometry->max_aspect - height, yinc);
+             if (height + delta <= max_height)
+               height += delta;
+           }
        }
-      else if (!(flags & GDK_HINT_MIN_SIZE) &&
-              (flags & GDK_HINT_RESIZE_INC) &&
-              ((extra_width != 0) || (extra_height != 0)))
-       {
-         flags |= GDK_HINT_BASE_SIZE;
+    }
 
-         new_geometry.base_width = extra_width;
-         new_geometry.base_height = extra_height;
-       }
+#undef FLOOR
+  
+  *new_width = width;
+  *new_height = height;
+}
 
-      if (flags & GDK_HINT_MIN_SIZE)
-       {
-         new_geometry.min_width += extra_width;
-         new_geometry.min_height += extra_height;
-       }
-      else if (!window->allow_shrink)
-       {
-         flags |= GDK_HINT_MIN_SIZE;
+/* Compute the set of geometry hints and flags for a window
+ * based on the application set geometry, and requisiition
+ * of the window. gtk_widget_size_request() must have been
+ * called first.
+ */
+static void
+gtk_window_compute_hints (GtkWindow   *window,
+                         GdkGeometry *new_geometry,
+                         guint       *new_flags)
+{
+  GtkWidget *widget;
+  GtkWidgetAuxInfo *aux_info;
+  gint ux, uy;
+  gint extra_width = 0;
+  gint extra_height = 0;
+  GtkWindowGeometryInfo *geometry_info;
+  GtkRequisition requisition;
 
-         new_geometry.min_width = requisition->width;
-         new_geometry.min_height = requisition->height;
-       }
+  g_return_if_fail (GTK_IS_WINDOW (window));
+
+  widget = GTK_WIDGET (window);
+  
+  gtk_widget_get_child_requisition (widget, &requisition);
+  geometry_info = gtk_window_get_geometry_info (GTK_WINDOW (widget), FALSE);
+
+  g_return_if_fail (geometry_info != NULL);
+  
+  *new_flags = geometry_info->mask;
+  *new_geometry = geometry_info->geometry;
+  
+  if (geometry_info->widget)
+    {
+      extra_width = widget->requisition.width - geometry_info->widget->requisition.width;
+      extra_height = widget->requisition.height - geometry_info->widget->requisition.height;
+    }
+  
+  ux = 0;
+  uy = 0;
+  
+  aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
+  if (aux_info && (aux_info->x != -1) && (aux_info->y != -1))
+    {
+      ux = aux_info->x;
+      uy = aux_info->y;
+      *new_flags |= GDK_HINT_POS;
+    }
+  
+  if (*new_flags & GDK_HINT_BASE_SIZE)
+    {
+      new_geometry->base_width += extra_width;
+      new_geometry->base_height += extra_height;
+    }
+  else if (!(*new_flags & GDK_HINT_MIN_SIZE) &&
+          (*new_flags & GDK_HINT_RESIZE_INC) &&
+          ((extra_width != 0) || (extra_height != 0)))
+    {
+      *new_flags |= GDK_HINT_BASE_SIZE;
+      
+      new_geometry->base_width = extra_width;
+      new_geometry->base_height = extra_height;
+    }
+  
+  if (*new_flags & GDK_HINT_MIN_SIZE)
+    {
+      new_geometry->min_width += extra_width;
+      new_geometry->min_height += extra_height;
+    }
+  else if (!window->allow_shrink)
+    {
+      *new_flags |= GDK_HINT_MIN_SIZE;
+      
+      new_geometry->min_width = requisition.width;
+      new_geometry->min_height = requisition.height;
+    }
+  
+  if (*new_flags & GDK_HINT_MAX_SIZE)
+    {
+      new_geometry->max_width += extra_width;
+      new_geometry->max_height += extra_height;
+    }
+  else if (!window->allow_grow)
+    {
+      *new_flags |= GDK_HINT_MAX_SIZE;
+      
+      new_geometry->max_width = requisition.width;
+      new_geometry->max_height = requisition.height;
+    }
+}
+
+/* Compute a new position for the window based on a new
+ * size. *x and *y will be set to the new coordinates, or to -1 if the
+ * window does not need to be moved
+ */
+static void 
+gtk_window_compute_reposition (GtkWindow  *window,
+                              gint        new_width,
+                              gint        new_height,
+                              gint       *x,
+                              gint       *y)
+{
+  GtkWidget *widget;
+
+  widget = GTK_WIDGET (window);
 
-      if (flags & GDK_HINT_MAX_SIZE)
+  *x = -1;
+  *y = -1;
+  
+  switch (window->position)
+    {
+    case GTK_WIN_POS_CENTER:
+    case GTK_WIN_POS_CENTER_ALWAYS:
+      if (window->use_uposition)
        {
-         new_geometry.max_width += extra_width;
-         new_geometry.max_height += extra_height;
+         gint screen_width = gdk_screen_width ();
+         gint screen_height = gdk_screen_height ();
+         
+         *x = (screen_width - new_width) / 2;
+         *y = (screen_height - new_height) / 2;
        }
-      else if (!window->allow_grow)
+      break;
+    case GTK_WIN_POS_MOUSE:
+      if (window->use_uposition)
        {
-         flags |= GDK_HINT_MAX_SIZE;
-
-         new_geometry.max_width = requisition->width;
-         new_geometry.max_height = requisition->height;
+         gint screen_width = gdk_screen_width ();
+         gint screen_height = gdk_screen_height ();
+         
+         gdk_window_get_pointer (NULL, x, y, NULL);
+         *x -= new_width / 2;
+         *y -= new_height / 2;
+         *x = CLAMP (*x, 0, screen_width - new_width);
+         *y = CLAMP (*y, 0, screen_height - new_height);
        }
-
-      gdk_window_set_geometry_hints (widget->window, &new_geometry, flags);
-
-      if (window->use_uposition && (flags & GDK_HINT_POS))
+      break;
+    default:
+      if (window->use_uposition)
        {
-         window->use_uposition = FALSE;
-         gdk_window_move (widget->window, ux, uy);
+         GtkWidgetAuxInfo *aux_info;
+         
+         aux_info = gtk_object_get_data (GTK_OBJECT (window), "gtk-aux-info");
+         if (aux_info &&
+             aux_info->x != -1 && aux_info->y != -1 &&
+             aux_info->x != -2 && aux_info->y != -2)
+           {
+             *x = aux_info->x;
+             *y = aux_info->y;
+           }
        }
+      break;
+    }
+
+  if (*x != -1 && *y != -1)
+    {
+      GtkWidgetAuxInfo *aux_info;
+      
+      /* we handle necessary window positioning by hand here,
+       * so we can coalesce the window movement with possible
+       * resizes to get only one configure event.
+       * keep this in sync with gtk_widget_set_uposition()
+       * and gtk_window_reposition().
+       */
+      gtk_widget_set_uposition (widget, -1, -1); /* ensure we have aux_info */
+      
+      aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
+      aux_info->x = *x;
+      aux_info->y = *y;
+
+      window->use_uposition = FALSE;
     }
 }
 
+/***********************
+ * Redrawing functions *
+ ***********************/
+
 static void
 gtk_window_paint (GtkWidget     *widget,
                  GdkRectangle *area)
@@ -1698,9 +2113,9 @@ gtk_window_expose (GtkWidget      *widget,
     gtk_window_paint (widget, &event->area);
   
   if (GTK_WIDGET_CLASS (parent_class)->expose_event)
-    return (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
+    return GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
 
-  return FALSE;
+  return TRUE;
 }
 
 static void