[gimp] app: factor out duplicated code to gimp_tool_palette_get_button_size()



commit e4a96c738eb46e33ed150f40aff441b1ad5fabe4
Author: Michael Natterer <mitch gimp org>
Date:   Sat Nov 20 10:20:52 2010 +0100

    app: factor out duplicated code to gimp_tool_palette_get_button_size()
    
    and use that function in GimpToolPalette and GimpToolbox.

 app/widgets/gimptoolbox.c     |   37 +++++++----------------
 app/widgets/gimptoolpalette.c |   65 +++++++++++++++++++++++++++++------------
 app/widgets/gimptoolpalette.h |   13 +++++---
 3 files changed, 66 insertions(+), 49 deletions(-)
---
diff --git a/app/widgets/gimptoolbox.c b/app/widgets/gimptoolbox.c
index 2ae1e6e..9d7abba 100644
--- a/app/widgets/gimptoolbox.c
+++ b/app/widgets/gimptoolbox.c
@@ -57,8 +57,6 @@
 #include "gimp-intl.h"
 
 
-#define TOOL_BUTTON_DATA_KEY "gimp-tool-palette-item"
-
 enum
 {
   PROP_0,
@@ -650,38 +648,27 @@ static void
 gimp_toolbox_set_host_geometry_hints (GimpDock  *dock,
                                       GtkWindow *window)
 {
-  GimpToolbox  *toolbox = GIMP_TOOLBOX (dock);
-  Gimp         *gimp;
-  GimpToolInfo *tool_info;
-  GtkWidget    *tool_button;
-
-  gimp = gimp_toolbox_get_context (toolbox)->gimp;
-
-  tool_info   = gimp_get_tool_info (gimp, "gimp-rect-select-tool");
-  tool_button = g_object_get_data (G_OBJECT (tool_info), TOOL_BUTTON_DATA_KEY);
+  GimpToolbox *toolbox = GIMP_TOOLBOX (dock);
+  gint         button_width;
+  gint         button_height;
 
-  if (tool_button)
+  if (gimp_tool_palette_get_button_size (GIMP_TOOL_PALETTE (toolbox->p->tool_palette),
+                                         &button_width, &button_height))
     {
-      GtkWidget      *main_vbox = gimp_dock_get_main_vbox (GIMP_DOCK (toolbox));
-      GtkRequisition  button_requisition;
-      gint            border_width;
-      GdkGeometry     geometry;
-
-      gtk_widget_size_request (tool_button, &button_requisition);
+      GtkWidget   *main_vbox = gimp_dock_get_main_vbox (GIMP_DOCK (toolbox));
+      gint         border_width;
+      GdkGeometry  geometry;
 
       gtk_widget_set_size_request (toolbox->p->header,
-                                   -1,
-                                   button_requisition.height *
-                                   PANGO_SCALE_SMALL);
+                                   -1, button_height * PANGO_SCALE_SMALL);
 
       border_width = gtk_container_get_border_width (GTK_CONTAINER (main_vbox));
 
-      geometry.min_width  = (2 * border_width +
-                             2 * button_requisition.width);
+      geometry.min_width  = (2 * border_width + 2 * button_width);
       geometry.min_height = -1;
-      geometry.width_inc  = button_requisition.width;
+      geometry.width_inc  = button_width;
       geometry.height_inc = (gimp_dock_get_dockbooks (GIMP_DOCK (toolbox)) ?
-                             1 : button_requisition.height);
+                             1 : button_height);
 
       gtk_window_set_geometry_hints (window,
                                      NULL,
diff --git a/app/widgets/gimptoolpalette.c b/app/widgets/gimptoolpalette.c
index 18d784d..636d059 100644
--- a/app/widgets/gimptoolpalette.c
+++ b/app/widgets/gimptoolpalette.c
@@ -344,38 +344,31 @@ gimp_tool_palette_size_allocate (GtkWidget     *widget,
                                  GtkAllocation *allocation)
 {
   GimpToolPalettePrivate *private = GET_PRIVATE (widget);
-  Gimp                   *gimp;
-  GimpToolInfo           *tool_info;
-  GtkWidget              *tool_button;
+  gint                    button_width;
+  gint                    button_height;
 
   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
 
-  gimp = private->context->gimp;
-
-  tool_info   = gimp_get_tool_info (gimp, "gimp-rect-select-tool");
-  tool_button = g_object_get_data (G_OBJECT (tool_info), TOOL_BUTTON_DATA_KEY);
-
-  if (tool_button)
+  if (gimp_tool_palette_get_button_size (GIMP_TOOL_PALETTE (widget),
+                                         &button_width, &button_height))
     {
-      GtkRequisition  button_requisition;
-      GList          *list;
-      gint            n_tools;
-      gint            tool_rows;
-      gint            tool_columns;
-
-      gtk_widget_size_request (tool_button, &button_requisition);
+      Gimp  *gimp = private->context->gimp;
+      GList *list;
+      gint   n_tools;
+      gint   tool_rows;
+      gint   tool_columns;
 
       for (list = gimp_get_tool_info_iter (gimp), n_tools = 0;
            list;
            list = list->next)
         {
-          tool_info = list->data;
+          GimpToolInfo *tool_info = list->data;
 
           if (tool_info->visible)
             n_tools++;
         }
 
-      tool_columns = MAX (1, (allocation->width / button_requisition.width));
+      tool_columns = MAX (1, (allocation->width / button_width));
       tool_rows    = n_tools / tool_columns;
 
       if (n_tools % tool_columns)
@@ -388,7 +381,7 @@ gimp_tool_palette_size_allocate (GtkWidget     *widget,
           private->tool_columns = tool_columns;
 
           gtk_widget_set_size_request (widget, -1,
-                                       tool_rows * button_requisition.height);
+                                       tool_rows * button_height);
         }
     }
 }
@@ -448,6 +441,40 @@ gimp_tool_palette_new (GimpContext       *context,
                        NULL);
 }
 
+gboolean
+gimp_tool_palette_get_button_size (GimpToolPalette *palette,
+                                   gint            *width,
+                                   gint            *height)
+{
+  GimpToolPalettePrivate *private;
+  GimpToolInfo           *tool_info;
+  GtkWidget              *tool_button;
+
+  g_return_val_if_fail (GIMP_IS_TOOL_PALETTE (palette), FALSE);
+  g_return_val_if_fail (width != NULL, FALSE);
+  g_return_val_if_fail (height != NULL, FALSE);
+
+  private = GET_PRIVATE (palette);
+
+  tool_info   = gimp_get_tool_info (private->context->gimp,
+                                    "gimp-rect-select-tool");
+  tool_button = g_object_get_data (G_OBJECT (tool_info), TOOL_BUTTON_DATA_KEY);
+
+  if (tool_button)
+    {
+      GtkRequisition button_requisition;
+
+      gtk_widget_size_request (tool_button, &button_requisition);
+
+      *width  = button_requisition.width;
+      *height = button_requisition.height;
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
 
 /*  private functions  */
 
diff --git a/app/widgets/gimptoolpalette.h b/app/widgets/gimptoolpalette.h
index 4453b1a..8251f24 100644
--- a/app/widgets/gimptoolpalette.h
+++ b/app/widgets/gimptoolpalette.h
@@ -43,11 +43,14 @@ struct _GimpToolPaletteClass
 };
 
 
-GType       gimp_tool_palette_get_type (void) G_GNUC_CONST;
-
-GtkWidget * gimp_tool_palette_new      (GimpContext       *context,
-                                        GimpUIManager     *ui_manager,
-                                        GimpDialogFactory *dialog_factory);
+GType       gimp_tool_palette_get_type        (void) G_GNUC_CONST;
+
+GtkWidget * gimp_tool_palette_new             (GimpContext       *context,
+                                               GimpUIManager     *ui_manager,
+                                               GimpDialogFactory *dialog_factory);
+gboolean    gimp_tool_palette_get_button_size (GimpToolPalette   *palette,
+                                               gint              *width,
+                                               gint              *height);
 
 
 #endif /* __GIMP_TOOL_PALETTE_H__ */



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