]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkwindow.c
Fix to properly take the default window into account by setting a flag and
[~andy/gtk] / gtk / gtkwindow.c
index 597ca48f4d9d82cce0c416053a6571d3c5eb828c..c1e006de622113845b342c1c1bf1fb7af60295fe 100644 (file)
@@ -35,6 +35,7 @@
 #include "gtkwindow.h"
 #include "gtkwindow-decorate.h"
 #include "gtkbindings.h"
+#include "gtkkeyhash.h"
 #include "gtkmain.h"
 #include "gtkiconfactory.h"
 #include "gtkintl.h"
@@ -117,6 +118,12 @@ struct _GtkWindowGeometryInfo
    * we sent the last configure request.
    */
   guint          position_constraints_changed : 1;
+
+  /* if true, default_width, height come from gtk_window_parse_geometry,
+   * and thus should be multiplied by the increments and affect the
+   * geometry widget only
+   */
+  guint          default_is_geometry : 1;
   
   GtkWindowLastGeometryInfo last;
 };
@@ -174,6 +181,7 @@ static void gtk_window_real_activate_default (GtkWindow         *window);
 static void gtk_window_real_activate_focus   (GtkWindow         *window);
 static void gtk_window_move_focus            (GtkWindow         *window,
                                               GtkDirectionType   dir);
+static void gtk_window_keys_changed          (GtkWindow         *window);
 static void gtk_window_read_rcfiles       (GtkWidget         *widget,
                                           GdkEventClient    *event);
 static void gtk_window_paint              (GtkWidget         *widget,
@@ -218,11 +226,15 @@ static void     gtk_window_set_default_size_internal (GtkWindow    *window,
                                                       gboolean      change_width,
                                                       gint          width,
                                                       gboolean      change_height,
-                                                      gint          height);
+                                                      gint          height,
+                                                     gboolean      is_geometry);
 
 static void     gtk_window_realize_icon               (GtkWindow    *window);
 static void     gtk_window_unrealize_icon             (GtkWindow    *window);
-static void    gtk_window_notify_keys_changed      (GtkWindow    *window);
+
+static void        gtk_window_notify_keys_changed (GtkWindow   *window);
+static GtkKeyHash *gtk_window_get_key_hash        (GtkWindow   *window);
+static void        gtk_window_free_key_hash       (GtkWindow   *window);
 
 static GSList      *toplevel_list = NULL;
 static GHashTable  *mnemonic_hash_table = NULL;
@@ -308,9 +320,6 @@ add_tab_bindings (GtkBindingSet    *binding_set,
   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
                                 "move_focus", 1,
                                 GTK_TYPE_DIRECTION_TYPE, direction);
-  gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, modifiers,
-                                "move_focus", 1,
-                                GTK_TYPE_DIRECTION_TYPE, direction);
 }
 
 static void
@@ -388,7 +397,7 @@ gtk_window_class_init (GtkWindowClass *klass)
   klass->activate_default = gtk_window_real_activate_default;
   klass->activate_focus = gtk_window_real_activate_focus;
   klass->move_focus = gtk_window_move_focus;
-  klass->keys_changed = NULL;
+  klass->keys_changed = gtk_window_keys_changed;
   
   /* Construct */
   g_object_class_install_property (gobject_class,
@@ -673,12 +682,12 @@ gtk_window_set_property (GObject      *object,
     case PROP_DEFAULT_WIDTH:
       gtk_window_set_default_size_internal (window,
                                             TRUE, g_value_get_int (value),
-                                            FALSE, -1);
+                                            FALSE, -1, FALSE);
       break;
     case PROP_DEFAULT_HEIGHT:
       gtk_window_set_default_size_internal (window,
                                             FALSE, -1,
-                                            TRUE, g_value_get_int (value));
+                                            TRUE, g_value_get_int (value), FALSE);
       break;
     case PROP_DESTROY_WITH_PARENT:
       gtk_window_set_destroy_with_parent (window, g_value_get_boolean (value));
@@ -1134,7 +1143,7 @@ gtk_window_add_mnemonic (GtkWindow *window,
   if (mnemonic)
     {
       g_return_if_fail (g_slist_find (mnemonic->targets, target) == NULL);
-      mnemonic->targets = g_slist_prepend (mnemonic->targets, target);
+      mnemonic->targets = g_slist_append (mnemonic->targets, target);
     }
   else
     {
@@ -1753,6 +1762,7 @@ gtk_window_get_geometry_info (GtkWindow *window,
       info->initial_x = 0;
       info->initial_y = 0;
       info->initial_pos_set = FALSE;
+      info->default_is_geometry = FALSE;
       info->position_constraints_changed = FALSE;
       info->last.configure_request.x = 0;
       info->last.configure_request.y = 0;
@@ -2338,7 +2348,8 @@ gtk_window_set_default_size_internal (GtkWindow    *window,
                                       gboolean      change_width,
                                       gint          width,
                                       gboolean      change_height,
-                                      gint          height)
+                                      gint          height,
+                                     gboolean      is_geometry)
 {
   GtkWindowGeometryInfo *info;
 
@@ -2349,6 +2360,8 @@ gtk_window_set_default_size_internal (GtkWindow    *window,
 
   g_object_freeze_notify (G_OBJECT (window));
 
+  info->default_is_geometry = is_geometry != FALSE;
+
   if (change_width)
     {
       if (width == 0)
@@ -2403,11 +2416,6 @@ gtk_window_set_default_size_internal (GtkWindow    *window,
  * For more control over a window's initial size and how resizing works,
  * investigate gtk_window_set_geometry_hints().
  *
- * A useful feature: if you set the "geometry widget" via
- * gtk_window_set_geometry_hints(), the default size specified by
- * gtk_window_set_default_size() will be the default size of that
- * widget, not of the entire window.
- *
  * For some uses, gtk_window_resize() is a more appropriate function.
  * gtk_window_resize() changes the current size of the window, rather
  * than the size to be used on initial display. gtk_window_resize() always
@@ -2429,7 +2437,7 @@ gtk_window_set_default_size (GtkWindow   *window,
   g_return_if_fail (width >= -1);
   g_return_if_fail (height >= -1);
 
-  gtk_window_set_default_size_internal (window, TRUE, width, TRUE, height);
+  gtk_window_set_default_size_internal (window, TRUE, width, TRUE, height, FALSE);
 }
 
 /**
@@ -2465,8 +2473,8 @@ gtk_window_get_default_size (GtkWindow *window,
 /**
  * gtk_window_resize:
  * @window: a #GtkWindow
- * @width: width to resize the window to
- * @height: height to resize the window to
+ * @width: width in pixels to resize the window to
+ * @height: height in pixels to resize the window to
  *
  * Resizes the window as if the user had done so, obeying geometry
  * constraints. The default geometry constraint is that windows may
@@ -2901,7 +2909,9 @@ gtk_window_destroy (GtkObject *object)
   if (window->group)
     gtk_window_group_remove_window (window->group, window);
 
-  GTK_OBJECT_CLASS (parent_class)->destroy (object);
+   gtk_window_free_key_hash (window);
+
+   GTK_OBJECT_CLASS (parent_class)->destroy (object);
 }
 
 static gboolean
@@ -3525,13 +3535,10 @@ gtk_window_key_press_event (GtkWidget   *widget,
 
   handled = FALSE;
 
+  /* Check for mnemonics and accelerators
+   */
   if (!handled)
-    handled = gtk_window_mnemonic_activate (window,
-                                           event->keyval,
-                                           event->state);
-
-  if (!handled)
-    handled = gtk_accel_groups_activate (G_OBJECT (window), event->keyval, event->state);
+    handled = _gtk_window_activate_key (window, event);
 
   if (!handled)
     {
@@ -3626,12 +3633,35 @@ gtk_window_leave_notify_event (GtkWidget        *widget,
   return FALSE;
 }
 
+static void
+do_focus_change (GtkWidget *widget,
+                gboolean   in)
+{
+  GdkEventFocus fevent;
+
+  g_object_ref (widget);
+   
+ if (in)
+    GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
+  else
+    GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
+
+  fevent.type = GDK_FOCUS_CHANGE;
+  fevent.window = widget->window;
+  fevent.in = in;
+  
+  gtk_widget_event (widget, (GdkEvent*) &fevent);
+  
+  g_object_notify (G_OBJECT (widget), "has_focus");
+
+  g_object_unref (widget);
+}
+
 static gint
 gtk_window_focus_in_event (GtkWidget     *widget,
                           GdkEventFocus *event)
 {
   GtkWindow *window = GTK_WINDOW (widget);
-  GdkEventFocus fevent;
 
   /* It appears spurious focus in events can occur when
    *  the window is hidden. So we'll just check to see if
@@ -3645,13 +3675,7 @@ gtk_window_focus_in_event (GtkWidget     *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;
-         fevent.in = TRUE;
-
-         gtk_widget_event (window->focus_widget, (GdkEvent*) &fevent);
-       }
+       do_focus_change (window->focus_widget, TRUE);   
     }
 
   return FALSE;
@@ -3662,20 +3686,13 @@ gtk_window_focus_out_event (GtkWidget     *widget,
                            GdkEventFocus *event)
 {
   GtkWindow *window = GTK_WINDOW (widget);
-  GdkEventFocus fevent;
 
   window->has_focus = FALSE;
   
   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;
-      fevent.in = FALSE;
-
-      gtk_widget_event (window->focus_widget, (GdkEvent*) &fevent);
-    }
+    do_focus_change (window->focus_widget, FALSE);
 
   return FALSE;
 }
@@ -3785,7 +3802,6 @@ static void
 gtk_window_real_set_focus (GtkWindow *window,
                           GtkWidget *focus)
 {
-  GdkEventFocus event;
   gboolean def_flags = 0;
 
   if (window->default_widget)
@@ -3803,13 +3819,7 @@ gtk_window_real_set_focus (GtkWindow *window,
         }
 
       if (window->has_focus)
-       {
-         event.type = GDK_FOCUS_CHANGE;
-         event.window = window->focus_widget->window;
-         event.in = FALSE;
-         
-         gtk_widget_event (window->focus_widget, (GdkEvent*) &event);
-       }
+       do_focus_change (window->focus_widget, FALSE);
     }
   
   window->focus_widget = focus;
@@ -3827,13 +3837,7 @@ gtk_window_real_set_focus (GtkWindow *window,
        }
 
       if (window->has_focus)
-       {
-         event.type = GDK_FOCUS_CHANGE;
-         event.window = window->focus_widget->window;
-         event.in = TRUE;
-         
-         gtk_widget_event (window->focus_widget, (GdkEvent*) &event);
-       }
+       do_focus_change (window->focus_widget, TRUE);
     }
   
   if (window->default_widget &&
@@ -3882,11 +3886,41 @@ gtk_window_compute_configure_request_size (GtkWindow *window,
 
        if (info)
          {
-           if (info->default_width > 0)
-             *width = info->default_width;
-           
-           if (info->default_height > 0)
-             *height = info->default_height;
+          gint base_width = 0;
+          gint base_height = 0;
+          gint width_inc = 1;
+          gint height_inc = 1;
+          
+          if (info->default_is_geometry &&
+              (info->default_width > 0 || info->default_height > 0))
+            {
+              GdkGeometry geometry;
+              guint flags;
+              
+              gtk_window_compute_hints (window, &geometry, &flags);
+
+              if (flags & GDK_HINT_BASE_SIZE)
+                {
+                  base_width = geometry.base_width;
+                  base_height = geometry.base_height;
+                }
+              else if (flags & GDK_HINT_MIN_SIZE)
+                {
+                  base_width = geometry.min_width;
+                  base_height = geometry.min_height;
+                }
+              if (flags & GDK_HINT_RESIZE_INC)
+                {
+                  width_inc = geometry.width_inc;
+                  height_inc = geometry.height_inc;
+                }
+            }
+            
+          if (info->default_width > 0)
+            *width = info->default_width * width_inc + base_width;
+          
+          if (info->default_height > 0)
+            *height = info->default_height * height_inc + base_height;
          }
     }
   else
@@ -4386,8 +4420,9 @@ gtk_window_move_resize (GtkWindow *window)
        * GTK_RESIZE_IMMEDIATE containers)
        */
       info->position_constraints_changed = FALSE;
-      window->need_default_position = FALSE;
       info->initial_pos_set = FALSE;
+      info->resize_width = -1;
+      info->resize_height = -1;
 
       /* for GTK_RESIZE_QUEUE toplevels, we are now awaiting a new
        * configure event in response to our resizing request.
@@ -5608,14 +5643,7 @@ gtk_window_parse_geometry (GtkWindow   *window,
   size_set = FALSE;
   if ((result & WidthValue) || (result & HeightValue))
     {
-      GtkWindowGeometryInfo *info;
-      info = gtk_window_get_geometry_info (window, FALSE);
-      if (info && info->mask & GDK_HINT_RESIZE_INC)
-        {
-          w *= info->geometry.width_inc;
-          h *= info->geometry.height_inc;
-        }
-      gtk_window_set_default_size (window, w, h);
+      gtk_window_set_default_size_internal (window, TRUE, w, TRUE, h, TRUE);
       size_set = TRUE;
     }
 
@@ -5676,3 +5704,186 @@ gtk_window_parse_geometry (GtkWindow   *window,
   
   return result != 0;
 }
+
+static void
+gtk_window_mnemonic_hash_foreach (gpointer key,
+                                 gpointer value,
+                                 gpointer data)
+{
+  struct {
+    GtkWindow *window;
+    GtkWindowKeysForeachFunc func;
+    gpointer func_data;
+  } *info = data;
+
+  GtkWindowMnemonic *mnemonic = value;
+
+  if (mnemonic->window == info->window)
+    (*info->func) (info->window, mnemonic->keyval, info->window->mnemonic_modifier, TRUE, info->func_data);
+}
+
+void
+_gtk_window_keys_foreach (GtkWindow                *window,
+                         GtkWindowKeysForeachFunc func,
+                         gpointer                 func_data)
+{
+  GSList *groups;
+
+  struct {
+    GtkWindow *window;
+    GtkWindowKeysForeachFunc func;
+    gpointer func_data;
+  } info;
+
+  info.window = window;
+  info.func = func;
+  info.func_data = func_data;
+
+  g_hash_table_foreach (mnemonic_hash_table,
+                       gtk_window_mnemonic_hash_foreach,
+                       &info);
+
+  groups = gtk_accel_groups_from_object (G_OBJECT (window));
+  while (groups)
+    {
+      GtkAccelGroup *group = groups->data;
+      gint i;
+
+      for (i = 0; i < group->n_accels; i++)
+       {
+         GtkAccelKey *key = &group->priv_accels[i].key;
+         
+         if (key->accel_key)
+           (*func) (window, key->accel_key, key->accel_mods, FALSE, func_data);
+       }
+      
+      groups = groups->next;
+    }
+}
+
+static void
+gtk_window_keys_changed (GtkWindow *window)
+{
+  gtk_window_free_key_hash (window);
+  gtk_window_get_key_hash (window);
+}
+
+typedef struct _GtkWindowKeyEntry GtkWindowKeyEntry;
+
+struct _GtkWindowKeyEntry
+{
+  guint keyval;
+  guint modifiers;
+  gboolean is_mnemonic;
+};
+
+static void
+add_to_key_hash (GtkWindow      *window,
+                guint           keyval,
+                GdkModifierType modifiers,
+                gboolean        is_mnemonic,
+                gpointer        data)
+{
+  GtkKeyHash *key_hash = data;
+
+  GtkWindowKeyEntry *entry = g_new (GtkWindowKeyEntry, 1);
+
+  entry->keyval = keyval;
+  entry->modifiers = modifiers;
+  entry->is_mnemonic = is_mnemonic;
+
+  /* GtkAccelGroup stores lowercased accelerators. To deal
+   * with this, if <Shift> was specified, uppercase.
+   */
+  if (modifiers & GDK_SHIFT_MASK)
+    {
+      if (keyval == GDK_Tab)
+       keyval = GDK_ISO_Left_Tab;
+      else
+       keyval = gdk_keyval_to_upper (keyval);
+    }
+  
+  _gtk_key_hash_add_entry (key_hash, keyval, entry->modifiers, entry);
+}
+
+static GtkKeyHash *
+gtk_window_get_key_hash (GtkWindow *window)
+{
+  GtkKeyHash *key_hash = g_object_get_data (G_OBJECT (window), "gtk-window-key-hash");
+  if (key_hash)
+    return key_hash;
+  
+  key_hash = _gtk_key_hash_new (gdk_keymap_get_default(), (GDestroyNotify)g_free);
+  _gtk_window_keys_foreach (window, add_to_key_hash, key_hash);
+  g_object_set_data (G_OBJECT (window), "gtk-window-key-hash", key_hash);
+
+  return key_hash;
+}
+
+static void
+gtk_window_free_key_hash (GtkWindow *window)
+{
+  GtkKeyHash *key_hash = g_object_get_data (G_OBJECT (window), "gtk-window-key-hash");
+  if (key_hash)
+    {
+      _gtk_key_hash_free (key_hash);
+      g_object_set_data (G_OBJECT (window), "gtk-window-key-hash", NULL);
+    }
+}
+
+/**
+ * _gtk_window_activate_key:
+ * @window: a #GtkWindow
+ * @event: a #GdkEventKey
+ * 
+ * Activates mnemonics and accelerators for this #GtKWindow
+ * 
+ * Return value: %TRUE if a mnemonic or accelerator was found and activated.
+ **/
+gboolean
+_gtk_window_activate_key (GtkWindow   *window,
+                         GdkEventKey *event)
+{
+  GtkKeyHash *key_hash = g_object_get_data (G_OBJECT (window), "gtk-window-key-hash");
+  GtkWindowKeyEntry *found_entry = NULL;
+
+  if (!key_hash)
+    {
+      gtk_window_keys_changed (window);
+      key_hash = g_object_get_data (G_OBJECT (window), "gtk-window-key-hash");
+    }
+  
+  if (key_hash)
+    {
+      GSList *entries = _gtk_key_hash_lookup (key_hash,
+                                             event->hardware_keycode,
+                                             event->state & gtk_accelerator_get_default_mod_mask (),
+                                             event->group);
+      GSList *tmp_list;
+
+      for (tmp_list = entries; tmp_list; tmp_list = tmp_list->next)
+       {
+         GtkWindowKeyEntry *entry = tmp_list->data;
+         if (entry->is_mnemonic)
+           {
+             found_entry = entry;
+             break;
+           }
+       }
+      
+      if (!found_entry && entries)
+       found_entry = entries->data;
+
+      g_slist_free (entries);
+    }
+
+  if (found_entry)
+    {
+      if (found_entry->is_mnemonic)
+       return gtk_window_mnemonic_activate (window, found_entry->keyval, found_entry->modifiers);
+      else
+       return gtk_accel_groups_activate (G_OBJECT (window), found_entry->keyval, found_entry->modifiers);
+    }
+  else
+    return FALSE;
+}