app: Generalize gimp_dock_add/remove_book()

Move the GtkPaned management code into gimp_widgets_add_paned_widget()
and gimp_widgets_remove_paned_widget() in gimpwidgets-utils.[ch] so we
can share this code for GimpDockColumns later.
This commit is contained in:
Martin Nordholts
2009-10-11 16:58:59 +02:00
parent 423c9d8212
commit 21c037fdd6
3 changed files with 259 additions and 187 deletions

View File

@ -54,6 +54,7 @@
#include "widgets-types.h"
#include "gimpdockseparator.h"
#include "gimperrordialog.h"
#include "gimpwidgets-utils.h"
@ -1159,3 +1160,174 @@ gimp_pango_layout_set_weight (PangoLayout *layout,
pango_layout_set_attributes (layout, attrs);
pango_attr_list_unref (attrs);
}
/**
* gimp_widgets_add_paned_widget:
* @box: A #GtkBox
* @box_widget_list: List of current widgets in the @box
* @box_widget_keep_last: The widget to keep last in the @box
* @widget: The #GtkWidget to add
* @index: Where to add the @widget
*
* Add a #GtkWidget to a #GtkBox in a hierarchy of #GtkPaned:s so the
* space can be manually distributed between the widgets.
**/
void
gimp_widgets_add_paned_widget (GtkBox *box,
GList **box_widget_list,
GtkWidget *box_widget_keep_last,
GtkWidget *widget,
gint index)
{
gint old_length = 0;
g_return_if_fail (GTK_IS_BOX (box));
g_return_if_fail (box_widget_list != NULL);
g_return_if_fail (GTK_IS_WIDGET (box_widget_keep_last) ||
box_widget_keep_last == NULL);
g_return_if_fail (GTK_IS_WIDGET (widget));
/* Calculate length */
old_length = g_list_length (*box_widget_list);
/* If index is invalid append at the end */
if (index >= old_length || index < 0)
{
index = old_length;
}
/* Insert into the list */
*box_widget_list = g_list_insert (*box_widget_list, widget, index);
/* Insert into the GtkPaned hierarchy */
if (old_length == 0)
{
gtk_box_pack_start (box, widget, TRUE, TRUE, 0);
/* Keep the desired widget at the end */
if (box_widget_keep_last)
gtk_box_reorder_child (box, box_widget_keep_last, -1);
}
else
{
GtkWidget *old_widget;
GtkWidget *parent;
GtkWidget *paned;
/* Figure out what widget to detach */
if (index == 0)
{
old_widget = g_list_nth_data (*box_widget_list, index + 1);
}
else
{
old_widget = g_list_nth_data (*box_widget_list, index - 1);
}
parent = gtk_widget_get_parent (old_widget);
if (old_length > 1 && index > 0)
{
GtkWidget *grandparent = gtk_widget_get_parent (parent);
old_widget = parent;
parent = grandparent;
}
/* Deatch the widget and bulid up a new hierarchy */
g_object_ref (old_widget);
gtk_container_remove (GTK_CONTAINER (parent), old_widget);
paned = gtk_vpaned_new ();
if (GTK_IS_VPANED (parent))
{
gtk_paned_pack1 (GTK_PANED (parent), paned, TRUE, FALSE);
}
else
{
gtk_box_pack_start (GTK_BOX (parent), paned, TRUE, TRUE, 0);
}
gtk_widget_show (paned);
if (index == 0)
{
gtk_paned_pack1 (GTK_PANED (paned), widget,
TRUE, FALSE);
gtk_paned_pack2 (GTK_PANED (paned), old_widget,
TRUE, FALSE);
}
else
{
gtk_paned_pack1 (GTK_PANED (paned), old_widget,
TRUE, FALSE);
gtk_paned_pack2 (GTK_PANED (paned), widget,
TRUE, FALSE);
}
g_object_unref (old_widget);
}
}
/**
* gimp_widgets_remove_paned_widget:
* @box: A #GtkBox
* @box_widget_list: Pointer to list of current widgets in the @box
* @widget: The #GtkWidget to remove
*
* Remove a #GtkWidget from a #GtkBox added with
* gimp_widgets_add_paned_widget().
**/
void
gimp_widgets_remove_paned_widget (GtkBox *box,
GList **box_widget_list,
GtkWidget *widget)
{
gint old_length = 0;
gint index = 0;
GtkWidget *other_widget = NULL;
GtkWidget *parent = NULL;
GtkWidget *grandparent = NULL;
g_return_if_fail (GTK_IS_BOX (box));
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (box_widget_list);
/* Calculate length and index */
old_length = g_list_length (*box_widget_list);
index = g_list_index (*box_widget_list, widget);
/* Remove from list */
*box_widget_list = g_list_remove (*box_widget_list, widget);
/* Remove from widget hierarchy */
if (old_length == 1)
{
gtk_container_remove (GTK_CONTAINER (box), widget);
}
else
{
g_object_ref (widget);
parent = gtk_widget_get_parent (GTK_WIDGET (widget));
grandparent = gtk_widget_get_parent (parent);
if (index == 0)
other_widget = gtk_paned_get_child2 (GTK_PANED (parent));
else
other_widget = gtk_paned_get_child1 (GTK_PANED (parent));
g_object_ref (other_widget);
gtk_container_remove (GTK_CONTAINER (parent), other_widget);
gtk_container_remove (GTK_CONTAINER (parent), GTK_WIDGET (widget));
gtk_container_remove (GTK_CONTAINER (grandparent), parent);
if (GTK_IS_VPANED (grandparent))
gtk_paned_pack1 (GTK_PANED (grandparent), other_widget, TRUE, FALSE);
else
gtk_box_pack_start (box, other_widget, TRUE, TRUE, 0);
g_object_unref (other_widget);
}
}