libegg r839 - in trunk: . libegg/toolpalette



Author: hasselmm
Date: Fri Jan 11 20:38:35 2008
New Revision: 839
URL: http://svn.gnome.org/viewvc/libegg?rev=839&view=rev

Log:
* libegg/toolpalette/eggtoolitemgroup.h:
Change signature of egg_tool_item_group_get_nth_item.
* libegg/toolpalette/eggtoolitemgroup.c:
Implement egg_tool_item_group_remove.
* libegg/toolpalette/eggtoolpalette.c:
Implement egg_tool_palette_remove.


Modified:
   trunk/ChangeLog
   trunk/libegg/toolpalette/eggtoolitemgroup.c
   trunk/libegg/toolpalette/eggtoolitemgroup.h
   trunk/libegg/toolpalette/eggtoolpalette.c

Modified: trunk/libegg/toolpalette/eggtoolitemgroup.c
==============================================================================
--- trunk/libegg/toolpalette/eggtoolitemgroup.c	(original)
+++ trunk/libegg/toolpalette/eggtoolitemgroup.c	Fri Jan 11 20:38:35 2008
@@ -47,7 +47,10 @@
 struct _EggToolItemGroupPrivate
 {
   GtkWidget        *header;
-  GArray           *items;
+
+  GtkToolItem     **items;
+  gsize             items_size;
+  gsize             items_length;
 
   gint64            animation_start;
   GSource          *animation_timeout;
@@ -55,6 +58,7 @@
   gint              expander_size;
   gint              header_spacing;
 
+  guint             sparse_items : 1;
   guint             expanded : 1;
 };
 
@@ -62,6 +66,26 @@
                egg_tool_item_group,
                GTK_TYPE_CONTAINER);
 
+static void
+egg_tool_item_group_repack (EggToolItemGroup *group)
+{
+  guint si, di;
+
+  if (group->priv->sparse_items)
+    for (si = di = 0; di < group->priv->items_length; ++si)
+      {
+        if (group->priv->items[si])
+          {
+            group->priv->items[di] = group->priv->items[si];
+            ++di;
+          }
+        else
+          --group->priv->items_length;
+      }
+
+  group->priv->sparse_items = FALSE;
+}
+
 static gboolean
 egg_tool_item_group_header_expose_event_cb (GtkWidget      *widget,
                                             GdkEventExpose *event,
@@ -110,7 +134,9 @@
                                             EGG_TYPE_TOOL_ITEM_GROUP,
                                             EggToolItemGroupPrivate);
 
-  group->priv->items = g_array_new (TRUE, TRUE, sizeof (GtkToolItem*));
+  group->priv->items_length = 0;
+  group->priv->items_size = 4;
+  group->priv->items = g_new (GtkToolItem*, group->priv->items_size);
   group->priv->header_spacing = DEFAULT_HEADER_SPACING;
   group->priv->expander_size = DEFAULT_EXPANDER_SIZE;
   group->priv->expander_style = GTK_EXPANDER_EXPANDED;
@@ -203,7 +229,7 @@
 
   if (group->priv->items)
     {
-      g_array_free (group->priv->items, TRUE);
+      g_free (group->priv->items);
       group->priv->items = NULL;
     }
 
@@ -217,7 +243,9 @@
   const gint border_width = GTK_CONTAINER (widget)->border_width;
   EggToolItemGroup *group = EGG_TOOL_ITEM_GROUP (widget);
 
-  if (group->priv->items->len && egg_tool_item_group_get_name (group))
+  egg_tool_item_group_repack (group);
+
+  if (group->priv->items_length && egg_tool_item_group_get_name (group))
     {
       gtk_widget_size_request (group->priv->header, requisition);
       gtk_widget_show (group->priv->header);
@@ -275,9 +303,12 @@
 
     if (group->priv->expanded || group->priv->animation_timeout)
       {
-        for (i = 0; i < group->priv->items->len; ++i)
+        for (i = 0; i < group->priv->items_length; ++i)
           {
-            GtkToolItem *item = egg_tool_item_group_get_nth_item (group, i);
+            GtkToolItem *item = group->priv->items[i];
+
+            if (!item)
+              continue;
 
             child_allocation.width = item_size.width;
             child_allocation.height = item_size.height;
@@ -299,10 +330,12 @@
       }
     else
       {
-        for (i = 0; i < group->priv->items->len; ++i)
+        for (i = 0; i < group->priv->items_length; ++i)
           {
-            GtkToolItem *item = egg_tool_item_group_get_nth_item (group, i);
-            gtk_widget_hide (GTK_WIDGET (item));
+            GtkToolItem *item = group->priv->items[i];
+
+            if (item)
+              gtk_widget_hide (GTK_WIDGET (item));
           }
       }
 
@@ -398,10 +431,22 @@
 
 static void
 egg_tool_item_group_remove (GtkContainer *container,
-                            GtkWidget    *widget G_GNUC_UNUSED)
+                            GtkWidget    *child)
 {
+  EggToolItemGroup *group;
+  guint i;
+
   g_return_if_fail (EGG_IS_TOOL_ITEM_GROUP (container));
-  g_return_if_reached ();
+  group = EGG_TOOL_ITEM_GROUP (container);
+
+  for (i = 0; i < group->priv->items_length; ++i)
+    if ((GtkWidget*) group->priv->items[i] == child)
+      {
+        g_object_unref (child);
+        gtk_widget_unparent (child);
+        group->priv->items[i] = NULL;
+        group->priv->sparse_items = TRUE;
+      }
 }
 
 static void
@@ -417,10 +462,12 @@
     callback (group->priv->header, callback_data);
 
   if (NULL != group->priv->items)
-    for (i = 0; i < group->priv->items->len; ++i)
+    for (i = 0; i < group->priv->items_length; ++i)
       {
-        GtkToolItem *item = egg_tool_item_group_get_nth_item (group, i);
-        callback (GTK_WIDGET (item), callback_data);
+        GtkToolItem *item = group->priv->items[i];
+
+        if (item)
+          callback (GTK_WIDGET (item), callback_data);
       }
 }
 
@@ -510,8 +557,9 @@
     {
       label = egg_tool_item_group_get_label (group);
       gtk_label_set_text (GTK_LABEL (label), name);
+      egg_tool_item_group_repack (group);
 
-      if (name && group->priv->items->len)
+      if (name && group->priv->items_length)
         gtk_widget_show (group->priv->header);
       else
         gtk_widget_hide (group->priv->header);
@@ -652,9 +700,21 @@
   if (-1 == position)
     position = egg_tool_item_group_get_n_items (group);
 
-  g_return_if_fail (position <= egg_tool_item_group_get_n_items (group));
-  g_array_insert_val (group->priv->items, position, item);
-  g_object_ref_sink (item);
+  g_return_if_fail ((guint) position <= egg_tool_item_group_get_n_items (group));
+
+  if (group->priv->items_length == group->priv->items_size)
+    {
+      group->priv->items_size *= 2;
+      group->priv->items = g_renew (GtkToolItem*,
+                                    group->priv->items,
+                                    group->priv->items_size);
+    }
+
+  memmove (group->priv->items + position + 1, group->priv->items + position,
+           sizeof (GtkToolItem*) * (group->priv->items_length - position));
+
+  group->priv->items[position] = g_object_ref_sink (item);
+  group->priv->items_length += 1;
 
   if (EGG_IS_TOOL_PALETTE (parent))
     _egg_tool_palette_item_set_drag_source (GTK_WIDGET (item), parent);
@@ -670,7 +730,7 @@
   g_return_if_fail (EGG_IS_TOOL_ITEM_GROUP (group));
   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
 
-  g_return_if_fail (position < (guint) egg_tool_item_group_get_n_items (group));
+  g_return_if_fail (position < egg_tool_item_group_get_n_items (group));
   g_return_if_reached ();
 }
 
@@ -683,29 +743,34 @@
   g_return_val_if_fail (EGG_IS_TOOL_ITEM_GROUP (group), -1);
   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
 
-  for (i = 0; i < group->priv->items->len; ++i)
-    if (item == egg_tool_item_group_get_nth_item (group, i))
+  egg_tool_item_group_repack (group);
+
+  for (i = 0; i < group->priv->items_length; ++i)
+    if (item == group->priv->items[i])
       return i;
 
   return -1;
 }
 
-gint
+guint
 egg_tool_item_group_get_n_items (EggToolItemGroup *group)
 {
   g_return_val_if_fail (EGG_IS_TOOL_ITEM_GROUP (group), 0);
-  return group->priv->items->len;
+  egg_tool_item_group_repack (group);
+  return group->priv->items_length;
 }
 
 GtkToolItem*
 egg_tool_item_group_get_nth_item (EggToolItemGroup *group,
-                                  gint              index)
+                                  guint             index)
 {
   g_return_val_if_fail (EGG_IS_TOOL_ITEM_GROUP (group), NULL);
-  g_return_val_if_fail (index < egg_tool_item_group_get_n_items (group), NULL);
-  g_return_val_if_fail (index >= 0, NULL);
 
-  return g_array_index (group->priv->items, GtkToolItem*, index);
+  egg_tool_item_group_repack (group);
+
+  g_return_val_if_fail (index < group->priv->items_length, NULL);
+
+  return group->priv->items[index];
 }
 
 GtkToolItem*
@@ -737,9 +802,12 @@
 
   item_size->width = item_size->height = 0;
 
-  for (i = 0; i < group->priv->items->len; ++i)
+  for (i = 0; i < group->priv->items_length; ++i)
     {
-      GtkToolItem *item = egg_tool_item_group_get_nth_item (group, i);
+      GtkToolItem *item = group->priv->items[i];
+
+      if (!item)
+        continue;
 
       gtk_widget_size_request (GTK_WIDGET (item), &child_requisition);
 
@@ -795,9 +863,10 @@
 _egg_tool_item_group_get_height_for_width (EggToolItemGroup *group,
                                            gint              width)
 {
-  guint n_items = group->priv->items->len;
+  guint n_items = group->priv->items_length;
   GtkRequisition child_requisition;
 
+  egg_tool_item_group_repack (group);
   gtk_widget_size_request (GTK_WIDGET (group), &child_requisition);
 
   if (n_items && (group->priv->expanded || group->priv->animation_timeout))

Modified: trunk/libegg/toolpalette/eggtoolitemgroup.h
==============================================================================
--- trunk/libegg/toolpalette/eggtoolitemgroup.h	(original)
+++ trunk/libegg/toolpalette/eggtoolitemgroup.h	Fri Jan 11 20:38:35 2008
@@ -69,9 +69,9 @@
 gint                  egg_tool_item_group_get_item_position (EggToolItemGroup *group,
                                                              GtkToolItem      *item);
 
-gint                  egg_tool_item_group_get_n_items       (EggToolItemGroup *group);
+guint                 egg_tool_item_group_get_n_items       (EggToolItemGroup *group);
 GtkToolItem*          egg_tool_item_group_get_nth_item      (EggToolItemGroup *group,
-                                                             gint              index);
+                                                             guint             index);
 GtkToolItem*          egg_tool_item_group_get_drop_item     (EggToolItemGroup *group,
                                                              gint              x,
                                                              gint              y);

Modified: trunk/libegg/toolpalette/eggtoolpalette.c
==============================================================================
--- trunk/libegg/toolpalette/eggtoolpalette.c	(original)
+++ trunk/libegg/toolpalette/eggtoolpalette.c	Fri Jan 11 20:38:35 2008
@@ -43,17 +43,20 @@
 
 struct _EggToolPalettePrivate
 {
-  GArray          *groups;
+  EggToolItemGroup **groups;
+  gsize              groups_size;
+  gsize              groups_length;
+
+  GtkAdjustment     *hadjustment;
+  GtkAdjustment     *vadjustment;
+
+  GtkRequisition     item_size;
+  GtkIconSize        icon_size;
+  GtkOrientation     orientation;
+  GtkToolbarStyle    style;
 
-  GtkAdjustment   *hadjustment;
-  GtkAdjustment   *vadjustment;
-
-  GtkRequisition   item_size;
-  GtkIconSize      icon_size;
-  GtkOrientation   orientation;
-  GtkToolbarStyle  style;
-
-  guint            is_drag_source : 1;
+  guint              sparse_groups : 1;
+  guint              drag_source : 1;
 };
 
 struct _EggToolPaletteDragData
@@ -72,14 +75,6 @@
                egg_tool_palette,
                GTK_TYPE_CONTAINER);
 
-static EggToolItemGroup*
-egg_tool_palette_get_group (EggToolPalette *palette,
-                            guint           index)
-{
-  g_return_val_if_fail (index < palette->priv->groups->len, NULL);
-  return g_array_index (palette->priv->groups, EggToolItemGroup*, index);
-}
-
 static void
 egg_tool_palette_init (EggToolPalette *palette)
 {
@@ -87,7 +82,10 @@
                                             EGG_TYPE_TOOL_PALETTE,
                                             EggToolPalettePrivate);
 
-  palette->priv->groups = g_array_new (TRUE, TRUE, sizeof (EggToolItemGroup*));
+  palette->priv->groups_size = 4;
+  palette->priv->groups_length = 0;
+  palette->priv->groups = g_new (EggToolItemGroup*, palette->priv->groups_size);
+
   palette->priv->icon_size = DEFAULT_ICON_SIZE;
   palette->priv->orientation = DEFAULT_ORIENTATION;
   palette->priv->style = DEFAULT_STYLE;
@@ -188,7 +186,7 @@
 
   if (palette->priv->groups)
     {
-      g_array_free (palette->priv->groups, TRUE);
+      g_free (palette->priv->groups);
       palette->priv->groups = NULL;
     }
 
@@ -210,9 +208,12 @@
   palette->priv->item_size.width = 0;
   palette->priv->item_size.height = 0;
 
-  for (i = 0; i < palette->priv->groups->len; ++i)
+  for (i = 0; i < palette->priv->groups_length; ++i)
     {
-      EggToolItemGroup *group = egg_tool_palette_get_group (palette, i);
+      EggToolItemGroup *group = palette->priv->groups[i];
+
+      if (!group)
+        continue;
 
       gtk_widget_size_request (GTK_WIDGET (group), &child_requisition);
 
@@ -258,9 +259,9 @@
   child_allocation.y = border_width + offset;
   child_allocation.width = allocation->width - border_width * 2;
 
-  for (i = 0; i < palette->priv->groups->len; ++i)
+  for (i = 0; i < palette->priv->groups_length; ++i)
     {
-      EggToolItemGroup *group = egg_tool_palette_get_group (palette, i);
+      EggToolItemGroup *group = palette->priv->groups[i];
 
       if (egg_tool_item_group_get_n_items (group))
         {
@@ -308,10 +309,12 @@
   cr = gdk_cairo_create (widget->window);
   cairo_push_group (cr);
 
-  for (i = 0; i < palette->priv->groups->len; ++i)
+  for (i = 0; i < palette->priv->groups_length; ++i)
     {
-      EggToolItemGroup *group = egg_tool_palette_get_group (palette, i);
-      _egg_tool_item_group_paint (group, cr);
+      EggToolItemGroup *group = palette->priv->groups[i];
+
+      if (group)
+        _egg_tool_item_group_paint (group, cr);
     }
 
   cairo_pop_group_to_source (cr);
@@ -394,6 +397,26 @@
 }
 
 static void
+egg_tool_palette_repack (EggToolPalette *palette)
+{
+  guint si, di;
+
+  if (palette->priv->sparse_groups)
+    for (si = di = 0; di < palette->priv->groups_length; ++si)
+      {
+        if (palette->priv->groups[si])
+          {
+            palette->priv->groups[di] = palette->priv->groups[si];
+            ++di;
+          }
+        else
+          --palette->priv->groups_length;
+      }
+
+  palette->priv->sparse_groups = FALSE;
+}
+
+static void
 egg_tool_palette_add (GtkContainer *container,
                       GtkWidget    *child)
 {
@@ -404,17 +427,41 @@
 
   palette = EGG_TOOL_PALETTE (container);
 
-  g_object_ref_sink (child);
-  g_array_append_val (palette->priv->groups, child);
+  if (palette->priv->groups_length == palette->priv->groups_size)
+    egg_tool_palette_repack (palette);
+
+  if (palette->priv->groups_length == palette->priv->groups_size)
+    {
+      palette->priv->groups_size *= 2;
+      palette->priv->groups = g_renew (EggToolItemGroup*,
+                                       palette->priv->groups,
+                                       palette->priv->groups_size);
+    }
+
+  palette->priv->groups[palette->priv->groups_length] = g_object_ref_sink (child);
+  palette->priv->groups_length += 1;
+
   gtk_widget_set_parent (child, GTK_WIDGET (palette));
 }
 
 static void
 egg_tool_palette_remove (GtkContainer *container,
-                         GtkWidget    *child G_GNUC_UNUSED)
+                         GtkWidget    *child)
 {
+  EggToolPalette *palette;
+  guint i;
+
   g_return_if_fail (EGG_IS_TOOL_PALETTE (container));
-  g_return_if_reached ();
+  palette = EGG_TOOL_PALETTE (container);
+
+  for (i = 0; i < palette->priv->groups_length; ++i)
+    if ((GtkWidget*) palette->priv->groups[i] == child)
+      {
+        g_object_unref (child);
+        gtk_widget_unparent (child);
+        palette->priv->groups[i] = NULL;
+        palette->priv->sparse_groups = TRUE;
+      }
 }
 
 static void
@@ -427,10 +474,12 @@
   guint i;
 
   if (palette->priv->groups)
-    for (i = 0; i < palette->priv->groups->len; ++i)
+    for (i = 0; i < palette->priv->groups_length; ++i)
       {
-        EggToolItemGroup *group = egg_tool_palette_get_group (palette, i);
-        callback (GTK_WIDGET (group), callback_data);
+        EggToolItemGroup *group = palette->priv->groups[i];
+
+        if (group)
+          callback (GTK_WIDGET (group), callback_data);
       }
 }
 
@@ -544,7 +593,7 @@
 {
   g_return_if_fail (EGG_IS_TOOL_PALETTE (palette));
   g_return_if_fail (EGG_IS_TOOL_ITEM_GROUP (group));
-  g_return_if_fail (position < palette->priv->groups->len);
+  g_return_if_fail (position < palette->priv->groups_length);
 
   g_return_if_reached ();
 }
@@ -593,13 +642,13 @@
 
   g_return_if_fail (EGG_IS_TOOL_PALETTE (palette));
 
-  if (palette->priv->is_drag_source)
+  if (palette->priv->drag_source)
     return;
 
-  palette->priv->is_drag_source = TRUE;
+  palette->priv->drag_source = TRUE;
 
-  for (i = 0; i < palette->priv->groups->len; ++i)
-    gtk_container_foreach (GTK_CONTAINER (egg_tool_palette_get_group (palette, i)),
+  for (i = 0; i < palette->priv->groups_length; ++i)
+    gtk_container_foreach (GTK_CONTAINER (palette->priv->groups[i]),
                            _egg_tool_palette_item_set_drag_source,
                            palette);
 }
@@ -658,7 +707,7 @@
 
   g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
 
-  if (palette->priv->is_drag_source)
+  if (palette->priv->drag_source)
     {
       gtk_tool_item_set_use_drag_window (GTK_TOOL_ITEM (widget), TRUE);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]