[gimp] app: Generalize gimp_dock_add/remove_book()
- From: Martin Nordholts <martinn src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gimp] app: Generalize gimp_dock_add/remove_book()
- Date: Sun, 11 Oct 2009 14:55:21 +0000 (UTC)
commit 21c037fdd66b5faf699caf1b7f0b30a3ae08ca26
Author: Martin Nordholts <martinn src gnome org>
Date: Sun Oct 11 16:58:59 2009 +0200
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.
app/widgets/gimpdock.c | 125 +++-------------------------
app/widgets/gimpwidgets-utils.c | 172 +++++++++++++++++++++++++++++++++++++++
app/widgets/gimpwidgets-utils.h | 149 +++++++++++++++++-----------------
3 files changed, 259 insertions(+), 187 deletions(-)
---
diff --git a/app/widgets/gimpdock.c b/app/widgets/gimpdock.c
index d67683e..17eed25 100644
--- a/app/widgets/gimpdock.c
+++ b/app/widgets/gimpdock.c
@@ -36,6 +36,7 @@
#include "gimpdockbook.h"
#include "gimpdockseparator.h"
#include "gimpuimanager.h"
+#include "gimpwidgets-utils.h"
#include "gimp-intl.h"
@@ -582,83 +583,18 @@ gimp_dock_add_book (GimpDock *dock,
GimpDockbook *dockbook,
gint index)
{
- gint old_length;
-
g_return_if_fail (GIMP_IS_DOCK (dock));
g_return_if_fail (GIMP_IS_DOCKBOOK (dockbook));
g_return_if_fail (gimp_dockbook_get_dock (dockbook) == NULL);
- old_length = g_list_length (dock->p->dockbooks);
-
- if (index >= old_length || index < 0)
- index = old_length;
-
gimp_dockbook_set_dock (dockbook, dock);
- dock->p->dockbooks = g_list_insert (dock->p->dockbooks, dockbook, index);
-
- if (old_length == 0)
- {
- gtk_box_pack_start (GTK_BOX (dock->p->vbox), GTK_WIDGET (dockbook),
- TRUE, TRUE, 0);
-
- /* Keep the south separator at the end */
- gtk_box_reorder_child (GTK_BOX (dock->p->vbox),
- dock->p->south_separator,
- -1);
- }
- else
- {
- GtkWidget *old_book;
- GtkWidget *parent;
- GtkWidget *paned;
-
- if (index == 0)
- old_book = g_list_nth_data (dock->p->dockbooks, index + 1);
- else
- old_book = g_list_nth_data (dock->p->dockbooks, index - 1);
-
- parent = gtk_widget_get_parent (old_book);
-
- if ((old_length > 1) && (index > 0))
- {
- GtkWidget *grandparent;
-
- grandparent = gtk_widget_get_parent (parent);
-
- old_book = parent;
- parent = grandparent;
- }
-
- g_object_ref (old_book);
-
- gtk_container_remove (GTK_CONTAINER (parent), old_book);
-
- 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), GTK_WIDGET (dockbook),
- TRUE, FALSE);
- gtk_paned_pack2 (GTK_PANED (paned), old_book,
- TRUE, FALSE);
- }
- else
- {
- gtk_paned_pack1 (GTK_PANED (paned), old_book,
- TRUE, FALSE);
- gtk_paned_pack2 (GTK_PANED (paned), GTK_WIDGET (dockbook),
- TRUE, FALSE);
- }
-
- g_object_unref (old_book);
- }
+ /* Add the dockbook to the hierarchy of GtkPaned:s */
+ gimp_widgets_add_paned_widget (GTK_BOX (dock->p->vbox),
+ &dock->p->dockbooks,
+ dock->p->south_separator,
+ GTK_WIDGET (dockbook),
+ index);
gtk_widget_show (GTK_WIDGET (dockbook));
@@ -669,54 +605,21 @@ void
gimp_dock_remove_book (GimpDock *dock,
GimpDockbook *dockbook)
{
- gint old_length;
- gint index;
-
g_return_if_fail (GIMP_IS_DOCK (dock));
g_return_if_fail (GIMP_IS_DOCKBOOK (dockbook));
-
g_return_if_fail (gimp_dockbook_get_dock (dockbook) == dock);
- old_length = g_list_length (dock->p->dockbooks);
- index = g_list_index (dock->p->dockbooks, dockbook);
-
gimp_dockbook_set_dock (dockbook, NULL);
- dock->p->dockbooks = g_list_remove (dock->p->dockbooks, dockbook);
+ /* Ref the dockbook so we can emit the "book-removed" signal and
+ * pass it as a parameter before it's destroyed
+ */
g_object_ref (dockbook);
- if (old_length == 1)
- {
- gtk_container_remove (GTK_CONTAINER (dock->p->vbox), GTK_WIDGET (dockbook));
- }
- else
- {
- GtkWidget *other_book;
- GtkWidget *parent;
- GtkWidget *grandparent;
-
- parent = gtk_widget_get_parent (GTK_WIDGET (dockbook));
- grandparent = gtk_widget_get_parent (parent);
-
- if (index == 0)
- other_book = gtk_paned_get_child2 (GTK_PANED (parent));
- else
- other_book = gtk_paned_get_child1 (GTK_PANED (parent));
-
- g_object_ref (other_book);
-
- gtk_container_remove (GTK_CONTAINER (parent), other_book);
- gtk_container_remove (GTK_CONTAINER (parent), GTK_WIDGET (dockbook));
-
- gtk_container_remove (GTK_CONTAINER (grandparent), parent);
-
- if (GTK_IS_VPANED (grandparent))
- gtk_paned_pack1 (GTK_PANED (grandparent), other_book, TRUE, FALSE);
- else
- gtk_box_pack_start (GTK_BOX (dock->p->vbox), other_book, TRUE, TRUE, 0);
-
- g_object_unref (other_book);
- }
+ /* Now remove the dockbook from the hierarchy of GtkPaned:s */
+ gimp_widgets_remove_paned_widget (GTK_BOX (dock->p->vbox),
+ &dock->p->dockbooks,
+ GTK_WIDGET (dockbook));
g_signal_emit (dock, dock_signals[BOOK_REMOVED], 0, dockbook);
diff --git a/app/widgets/gimpwidgets-utils.c b/app/widgets/gimpwidgets-utils.c
index b17fd07..3d152c1 100644
--- a/app/widgets/gimpwidgets-utils.c
+++ b/app/widgets/gimpwidgets-utils.c
@@ -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);
+ }
+}
diff --git a/app/widgets/gimpwidgets-utils.h b/app/widgets/gimpwidgets-utils.h
index 097d034..51b6f8c 100644
--- a/app/widgets/gimpwidgets-utils.h
+++ b/app/widgets/gimpwidgets-utils.h
@@ -22,82 +22,79 @@
#define __GIMP_WIDGETS_UTILS_H__
-void gimp_menu_position (GtkMenu *menu,
- gint *x,
- gint *y);
-void gimp_button_menu_position (GtkWidget *button,
- GtkMenu *menu,
- GtkPositionType position,
- gint *x,
- gint *y);
-
-void gimp_table_attach_stock (GtkTable *table,
- gint row,
- const gchar *stock_id,
- GtkWidget *widget,
- gint colspan,
- gboolean left_align);
-void gimp_enum_radio_box_add (GtkBox *box,
- GtkWidget *widget,
- gint enum_value,
- gboolean below);
-void gimp_enum_radio_frame_add (GtkFrame *frame,
- GtkWidget *widget,
- gint enum_value,
- gboolean below);
-
-GtkIconSize gimp_get_icon_size (GtkWidget *widget,
- const gchar *stock_id,
- GtkIconSize max_size,
- gint width,
- gint height);
-
-const gchar * gimp_get_mod_name_shift (void);
-const gchar * gimp_get_mod_name_control (void);
-const gchar * gimp_get_mod_name_alt (void);
-const gchar * gimp_get_mod_separator (void);
-const gchar * gimp_get_mod_string (GdkModifierType modifiers);
-gchar * gimp_suggest_modifiers (const gchar *message,
- GdkModifierType modifiers,
- const gchar *shift_format,
- const gchar *control_format,
- const gchar *alt_format);
-
-void gimp_get_screen_resolution (GdkScreen *screen,
- gdouble *xres,
- gdouble *yres);
-
-void gimp_rgb_get_gdk_color (const GimpRGB *rgb,
- GdkColor *gdk_color);
-void gimp_rgb_set_gdk_color (GimpRGB *rgb,
- const GdkColor *gdk_color);
-
-void gimp_window_set_hint (GtkWindow *window,
- GimpWindowHint hint);
-GdkNativeWindow gimp_window_get_native (GtkWindow *window);
-void gimp_window_set_transient_for (GtkWindow *window,
- guint32 parent_ID);
-
-gboolean gimp_text_buffer_load (GtkTextBuffer *buffer,
- const gchar *filename,
- GError **error);
-gboolean gimp_text_buffer_save (GtkTextBuffer *buffer,
- const gchar *filename,
- gboolean selection_only,
- GError **error);
-
-void gimp_toggle_button_set_visible (GtkToggleButton *toggle,
- GtkWidget *widget);
-
-void gimp_widget_set_accel_help (GtkWidget *widget,
- GtkAction *action);
-
-const gchar * gimp_get_message_stock_id (GimpMessageSeverity severity);
-
-void gimp_pango_layout_set_scale (PangoLayout *layout,
- double scale);
-void gimp_pango_layout_set_weight (PangoLayout *layout,
- PangoWeight weight);
+void gimp_menu_position (GtkMenu *menu,
+ gint *x,
+ gint *y);
+void gimp_button_menu_position (GtkWidget *button,
+ GtkMenu *menu,
+ GtkPositionType position,
+ gint *x,
+ gint *y);
+void gimp_table_attach_stock (GtkTable *table,
+ gint row,
+ const gchar *stock_id,
+ GtkWidget *widget,
+ gint colspan,
+ gboolean left_align);
+void gimp_enum_radio_box_add (GtkBox *box,
+ GtkWidget *widget,
+ gint enum_value,
+ gboolean below);
+void gimp_enum_radio_frame_add (GtkFrame *frame,
+ GtkWidget *widget,
+ gint enum_value,
+ gboolean below);
+GtkIconSize gimp_get_icon_size (GtkWidget *widget,
+ const gchar *stock_id,
+ GtkIconSize max_size,
+ gint width,
+ gint height);
+const gchar * gimp_get_mod_name_shift (void);
+const gchar * gimp_get_mod_name_control (void);
+const gchar * gimp_get_mod_name_alt (void);
+const gchar * gimp_get_mod_separator (void);
+const gchar * gimp_get_mod_string (GdkModifierType modifiers);
+gchar * gimp_suggest_modifiers (const gchar *message,
+ GdkModifierType modifiers,
+ const gchar *shift_format,
+ const gchar *control_format,
+ const gchar *alt_format);
+void gimp_get_screen_resolution (GdkScreen *screen,
+ gdouble *xres,
+ gdouble *yres);
+void gimp_rgb_get_gdk_color (const GimpRGB *rgb,
+ GdkColor *gdk_color);
+void gimp_rgb_set_gdk_color (GimpRGB *rgb,
+ const GdkColor *gdk_color);
+void gimp_window_set_hint (GtkWindow *window,
+ GimpWindowHint hint);
+GdkNativeWindow gimp_window_get_native (GtkWindow *window);
+void gimp_window_set_transient_for (GtkWindow *window,
+ guint32 parent_ID);
+gboolean gimp_text_buffer_load (GtkTextBuffer *buffer,
+ const gchar *filename,
+ GError **error);
+gboolean gimp_text_buffer_save (GtkTextBuffer *buffer,
+ const gchar *filename,
+ gboolean selection_only,
+ GError **error);
+void gimp_toggle_button_set_visible (GtkToggleButton *toggle,
+ GtkWidget *widget);
+void gimp_widget_set_accel_help (GtkWidget *widget,
+ GtkAction *action);
+const gchar * gimp_get_message_stock_id (GimpMessageSeverity severity);
+void gimp_pango_layout_set_scale (PangoLayout *layout,
+ double scale);
+void gimp_pango_layout_set_weight (PangoLayout *layout,
+ PangoWeight weight);
+void gimp_widgets_add_paned_widget (GtkBox *box,
+ GList **box_widget_list,
+ GtkWidget *box_widget_keep_last,
+ GtkWidget *widget,
+ gint index);
+void gimp_widgets_remove_paned_widget (GtkBox *box,
+ GList **box_widget_list,
+ GtkWidget *widget);
#endif /* __GIMP_WIDGETS_UTILS_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]