[gtk+/extended-layout-jhs: 37/64] Provide natural size information. Implemented some initial natural size



commit 374c49a9758e8524205cf989745fdee3f509ce0d
Author: Mathias Hasselmann <mathias hasselmann gmx de>
Date:   Mon Jul 23 17:19:46 2007 +0000

    Provide natural size information. Implemented some initial natural size
    
    2007-07-23  Mathias Hasselmann  <mathias hasselmann gmx de>
    
    	* gtk/gtktable.c, gtk/gtktable.h: Provide natural size information.
    	* tests/autotestextendedlayout.c: Implemented some initial natural
    	size tests for GtkTable.
    
    svn path=/branches/extended-layout/; revision=18529

 ChangeLog.gtk-extended-layout  |    6 +++
 gtk/gtktable.c                 |   69 ++++++++++++++++++++++++++++++++++++++--
 gtk/gtktable.h                 |    1 +
 tests/autotestextendedlayout.c |   65 +++++++++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog.gtk-extended-layout b/ChangeLog.gtk-extended-layout
index 07a5669..5444408 100644
--- a/ChangeLog.gtk-extended-layout
+++ b/ChangeLog.gtk-extended-layout
@@ -1,3 +1,9 @@
+2007-07-23  Mathias Hasselmann  <mathias hasselmann gmx de>
+
+	* gtk/gtktable.c, gtk/gtktable.h: Provide natural size information.
+	* tests/autotestextendedlayout.c: Implemented some initial natural 
+	size tests for GtkTable.
+
 2007-07-18  Mathias Hasselmann  <mathias hasselmann gmx de>
 
 	* gtk/gtkextendedlayout.h, gtk/gtkhbox.c, gtk/gtkvbox.c:
diff --git a/gtk/gtktable.c b/gtk/gtktable.c
index 9002ad3..ce766fe 100644
--- a/gtk/gtktable.c
+++ b/gtk/gtktable.c
@@ -26,6 +26,8 @@
 
 #include <config.h>
 #include "gtktable.h"
+#include "gtkextendedlayout.h"
+#include "gtksizegroup.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
 #include "gtkalias.h"
@@ -97,8 +99,11 @@ static void gtk_table_size_allocate_init  (GtkTable *table);
 static void gtk_table_size_allocate_pass1 (GtkTable *table);
 static void gtk_table_size_allocate_pass2 (GtkTable *table);
 
+static void gtk_table_extended_layout_interface_init (GtkExtendedLayoutIface *iface);
 
-G_DEFINE_TYPE (GtkTable, gtk_table, GTK_TYPE_CONTAINER)
+G_DEFINE_TYPE_WITH_CODE (GtkTable, gtk_table, GTK_TYPE_CONTAINER,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
+                                                gtk_table_extended_layout_interface_init))
 
 static void
 gtk_table_class_init (GtkTableClass *class)
@@ -942,11 +947,13 @@ gtk_table_size_request_init (GtkTable *table)
   for (row = 0; row < table->nrows; row++)
     {
       table->rows[row].requisition = 0;
+      table->rows[row].natural_size = 0;
       table->rows[row].expand = FALSE;
     }
   for (col = 0; col < table->ncols; col++)
     {
       table->cols[col].requisition = 0;
+      table->cols[col].natural_size = 0;
       table->cols[col].expand = FALSE;
     }
   
@@ -966,7 +973,7 @@ gtk_table_size_request_init (GtkTable *table)
 	table->rows[child->top_attach].expand = TRUE;
     }
 }
-
+#include "gtklabel.h"
 static void
 gtk_table_size_request_pass1 (GtkTable *table)
 {
@@ -983,15 +990,25 @@ gtk_table_size_request_pass1 (GtkTable *table)
       
       if (GTK_WIDGET_VISIBLE (child->widget))
 	{
-	  GtkRequisition child_requisition;
+	  GtkRequisition child_requisition, child_natural_size;
 	  gtk_widget_get_child_requisition (child->widget, &child_requisition);
 
+          if (GTK_EXTENDED_LAYOUT_HAS_NATURAL_SIZE (child->widget))
+            gtk_extended_layout_get_natural_size (GTK_EXTENDED_LAYOUT (child->widget), 
+                                                  &child_natural_size);
+          else
+            child_natural_size = child_requisition;
+
 	  /* Child spans a single column.
 	   */
 	  if (child->left_attach == (child->right_attach - 1))
 	    {
 	      width = child_requisition.width + child->xpadding * 2;
 	      table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
+
+	      width = child_natural_size.width + child->xpadding * 2;
+if (GTK_IS_LABEL (child->widget))
+              table->cols[child->left_attach].natural_size = MAX (table->cols[child->left_attach].natural_size, width);
 	    }
 	  
 	  /* Child spans a single row.
@@ -1000,6 +1017,9 @@ gtk_table_size_request_pass1 (GtkTable *table)
 	    {
 	      height = child_requisition.height + child->ypadding * 2;
 	      table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
+
+	      height = child_natural_size.height + child->ypadding * 2;
+              table->rows[child->top_attach].natural_size = MAX (table->rows[child->top_attach].natural_size, height);
 	    }
 	}
     }
@@ -1636,5 +1656,48 @@ gtk_table_size_allocate_pass2 (GtkTable *table)
     }
 }
 
+static GtkExtendedLayoutFeatures
+gtk_table_extended_layout_get_features (GtkExtendedLayout *layout)
+{
+  return GTK_EXTENDED_LAYOUT_NATURAL_SIZE;
+}
+
+static void
+gtk_table_extended_layout_get_natural_size (GtkExtendedLayout *layout,
+                                            GtkRequisition    *requisition)
+{
+  int width, height, i;
+  GtkTable *table;
+
+  /* update size requisition when needed */
+  _gtk_size_group_compute_requisition (GTK_WIDGET (layout), NULL);
+
+  table = GTK_TABLE (layout);
+
+  width = 0;
+  height = 0;
+
+  for (i = 0; i < table->ncols; i++)
+    {
+    width += table->cols[i].natural_size;
+    g_print ("%s: cols[%d].natural_size: %d\n", G_STRFUNC, i, table->cols[i].natural_size);
+    }
+  for (i = 0; i < table->nrows; i++)
+    {
+    height += table->rows[i].natural_size;
+    g_print ("%s: rows[%d].natural_size: %d\n", G_STRFUNC, i, table->rows[i].natural_size);
+    }
+
+  requisition->width = width;
+  requisition->height = height;
+}
+
+static void
+gtk_table_extended_layout_interface_init (GtkExtendedLayoutIface *iface)
+{
+  iface->get_features = gtk_table_extended_layout_get_features;
+  iface->get_natural_size = gtk_table_extended_layout_get_natural_size;
+}
+
 #define __GTK_TABLE_C__
 #include "gtkaliasdef.c"
diff --git a/gtk/gtktable.h b/gtk/gtktable.h
index f925d33..fb7cb3c 100644
--- a/gtk/gtktable.h
+++ b/gtk/gtktable.h
@@ -93,6 +93,7 @@ struct _GtkTableRowCol
   guint expand : 1;
   guint shrink : 1;
   guint empty : 1;
+  guint16 natural_size;
 };
 
 
diff --git a/tests/autotestextendedlayout.c b/tests/autotestextendedlayout.c
index bb36c2e..b53acb8 100644
--- a/tests/autotestextendedlayout.c
+++ b/tests/autotestextendedlayout.c
@@ -433,6 +433,70 @@ gtk_bin_test_extended_layout (void)
     }
 }
 
+static void
+gtk_table_test_extended_layout (void)
+{
+  const gchar *numbers[] =
+    {
+      "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
+      "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
+      NULL
+    };
+
+  GtkExtendedLayoutFeatures features;
+  GtkRequisition natural_sizes[4];
+
+  GtkWidget *labels[G_N_ELEMENTS(numbers) - 1];
+  GtkWidget *table;
+  GtkWidget *window;
+
+  int i, row, col;
+
+  table = gtk_table_new (4, 4, FALSE);
+
+  for (i = 0, row; numbers[i]; ++i)
+    {
+      col = i % 4;
+      row = i / 4;
+
+      labels[i] = gtk_label_new (numbers[i]);
+      gtk_table_attach (GTK_TABLE (table), labels[i], col, col + 1, row, row + 1,
+                        (col > 0 ? GTK_FILL : 0) | (2 == col ? GTK_EXPAND : 0),
+                        (row > 0 ? GTK_FILL : 0) | (2 == row ? GTK_EXPAND : 0),
+                        6, 6);
+    }
+
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_container_add (GTK_CONTAINER (window), table);
+  gtk_widget_show_all (window);
+
+  /* feature set */
+
+  features = gtk_extended_layout_get_features (GTK_EXTENDED_LAYOUT (table));
+
+  log_test (0 == (features & GTK_EXTENDED_LAYOUT_HEIGHT_FOR_WIDTH));
+  log_test (0 == (features & GTK_EXTENDED_LAYOUT_WIDTH_FOR_HEIGHT));
+  log_test (0 != (features & GTK_EXTENDED_LAYOUT_NATURAL_SIZE));
+  log_test (0 == (features & GTK_EXTENDED_LAYOUT_BASELINES));
+
+  /* natural size changes are propagated */ 
+
+  gtk_extended_layout_get_natural_size (GTK_EXTENDED_LAYOUT (labels[7]), &natural_sizes[0]);
+  gtk_extended_layout_get_natural_size (GTK_EXTENDED_LAYOUT (table), &natural_sizes[1]);
+
+  gtk_label_set_text (GTK_LABEL (labels[7]), "This text is much longer than the number");
+
+  gtk_extended_layout_get_natural_size (GTK_EXTENDED_LAYOUT (labels[7]), &natural_sizes[2]);
+  gtk_extended_layout_get_natural_size (GTK_EXTENDED_LAYOUT (table), &natural_sizes[3]);
+
+  log_testf (natural_sizes[0].width < natural_sizes[2].width,
+             "Natural size of the label grows: %d < %d",
+             natural_sizes[0].width, natural_sizes[2].width);
+  log_testf (natural_sizes[1].width < natural_sizes[3].width,
+             "Natural size of the table grows: %d < %d",
+	     natural_sizes[1].width, natural_sizes[3].width);
+}
+
 /*****************************************************************************/
 
 int
@@ -444,6 +508,7 @@ main(int argc, char **argv)
 
   gtk_label_test_extended_layout ();
   gtk_bin_test_extended_layout ();
+  gtk_table_test_extended_layout ();
 
   log_testi (0, num_warnings);
   log_testi (0, num_errors);



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