pivot-table: New functions for setting captions, etc.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 2 Jan 2022 03:27:31 +0000 (19:27 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 13 Mar 2022 23:56:02 +0000 (16:56 -0700)
src/language/dictionary/sys-file-info.c
src/language/stats/factor.c
src/output/pivot-table.c
src/output/pivot-table.h

index 3ab4c040991d74c126d74501ac799ae6853ef829..7f5216134c8e1d869700ce3437db2ea7e7209845 100644 (file)
@@ -1002,11 +1002,12 @@ report_encodings (const struct file_handle *h, struct pool *pool,
   struct pivot_table *table = pivot_table_create__ (
     pivot_value_new_text_format (N_("Usable encodings for %s."),
                                  fh_get_name (h)), "Usable Encodings");
-  table->caption = pivot_value_new_text_format (
-    N_("Encodings that can successfully read %s (by specifying the encoding "
-       "name on the GET command's ENCODING subcommand).  Encodings that "
-       "yield identical text are listed together."),
-    fh_get_name (h));
+  pivot_table_set_caption (
+    table, pivot_value_new_text_format (
+      N_("Encodings that can successfully read %s (by specifying the encoding "
+         "name on the GET command's ENCODING subcommand).  Encodings that "
+         "yield identical text are listed together."),
+      fh_get_name (h)));
 
   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Encodings"),
                           N_("Encodings"));
@@ -1041,9 +1042,10 @@ report_encodings (const struct file_handle *h, struct pool *pool,
     pivot_value_new_text_format (N_("%s Encoded Text Strings"),
                                  fh_get_name (h)),
     "Alternate Encoded Text Strings");
-  table->caption = pivot_value_new_text (
-    N_("Text strings in the file dictionary that the previously listed "
-       "encodings interpret differently, along with the interpretations."));
+  pivot_table_set_caption (
+    table, pivot_value_new_text (
+      N_("Text strings in the file dictionary that the previously listed "
+         "encodings interpret differently, along with the interpretations.")));
 
   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Text"), N_("Text"));
 
index 8a5efac14b020d3c38645047224e82086664ea99..3c1ce6094e5a2854be019066efdffae0e6afa618 100644 (file)
@@ -1945,8 +1945,11 @@ show_correlation_matrix (const struct cmd_factor *factor, const struct idata *id
     }
 
   if (factor->print & PRINT_DETERMINANT)
-    table->caption = pivot_value_new_user_text_nocopy (
-      xasprintf ("%s: %.2f", _("Determinant"), idata->detR));
+    {
+      struct pivot_value *caption = pivot_value_new_user_text_nocopy (
+        xasprintf ("%s: %.2f", _("Determinant"), idata->detR));
+      pivot_table_set_caption (table, caption);
+    }
 
   pivot_table_submit (table);
 }
index 6ba4c16f2e0811545b4b5673678900f5b37a1cf3..a3325a1469324332ff947444559ad6da32bbafbf 100644 (file)
@@ -865,20 +865,20 @@ pivot_table_create (const char *title)
 struct pivot_table *
 pivot_table_create__ (struct pivot_value *title, const char *subtype)
 {
-  struct pivot_table *table = XZALLOC (struct pivot_table);
-  table->ref_cnt = 1;
-  table->show_title = true;
-  table->show_caption = true;
-  table->weight_format = (struct fmt_spec) { .type = FMT_F, .w = 40 };
-  table->title = title;
-  table->subtype = subtype ? pivot_value_new_text (subtype) : NULL;
-  table->command_c = xstrdup_if_nonempty (output_get_command_name ());
-  table->look = pivot_table_look_ref (pivot_table_look_get_default ());
-  table->settings = fmt_settings_copy (settings_get_fmt_settings ());
-  table->small = settings_get_small ();
-
-  hmap_init (&table->cells);
-
+  struct pivot_table *table = xmalloc (sizeof *table);
+  *table = (struct pivot_table) {
+    .ref_cnt = 1,
+    .show_title = true,
+    .show_caption = true,
+    .weight_format = (struct fmt_spec) { .type = FMT_F, .w = 40 },
+    .title = title,
+    .subtype = subtype ? pivot_value_new_text (subtype) : NULL,
+    .command_c = xstrdup_if_nonempty (output_get_command_name ()),
+    .look = pivot_table_look_ref (pivot_table_look_get_default ()),
+    .settings = fmt_settings_copy (settings_get_fmt_settings ()),
+    .small = settings_get_small (),
+    .cells = HMAP_INITIALIZER (table->cells),
+  };
   return table;
 }
 
@@ -1191,6 +1191,43 @@ pivot_table_is_shared (const struct pivot_table *table)
   return table->ref_cnt > 1;
 }
 
+static void
+pivot_table_set_value__ (struct pivot_value **dstp, struct pivot_value *src)
+{
+  pivot_value_destroy (*dstp);
+  *dstp = src;
+}
+
+/* Changes the title of TABLE to TITLE.  Takes ownership of TITLE. */
+void
+pivot_table_set_title (struct pivot_table *table, struct pivot_value *title)
+{
+  pivot_table_set_value__ (&table->title, title);
+}
+
+/* Changes the subtype of TABLE to SUBTYPE.  Takes ownership of SUBTYPE. */
+void
+pivot_table_set_subtype (struct pivot_table *table, struct pivot_value *subtype)
+{
+  pivot_table_set_value__ (&table->subtype, subtype);
+}
+
+/* Changes the corner text of TABLE to CORNER_TEXT.  Takes ownership of
+   CORNER_TEXT. */
+void
+pivot_table_set_corner_text (struct pivot_table *table,
+                             struct pivot_value *corner_text)
+{
+  pivot_table_set_value__ (&table->corner_text, corner_text);
+}
+
+/* Changes the caption of TABLE to CAPTION.  Takes ownership of CAPTION. */
+void
+pivot_table_set_caption (struct pivot_table *table, struct pivot_value *caption)
+{
+  pivot_table_set_value__ (&table->caption, caption);
+}
+
 /* Swaps axes A and B in TABLE. */
 void
 pivot_table_swap_axes (struct pivot_table *table,
@@ -2693,9 +2730,9 @@ pivot_value_new_user_text_nocopy (char *text)
    that pivot_value_new_variable() should be used for variable names).  For
    strings that are part of the PSPP user interface, such as names of
    procedures, statistics, annotations, error messages, etc., use
-   pivot_value_new_text().j
+   pivot_value_new_text().
 
-   The caller retains ownership of TEXT.*/
+   The caller retains ownership of TEXT. */
 struct pivot_value *
 pivot_value_new_user_text (const char *text, size_t length)
 {
index e7e5ad403b1b38b0cb769e404b8f1c002b131212..88edb62031c5227b304bb62a761704d25a03b47c 100644 (file)
@@ -489,6 +489,12 @@ struct pivot_table *pivot_table_unshare (struct pivot_table *);
 void pivot_table_unref (struct pivot_table *);
 bool pivot_table_is_shared (const struct pivot_table *);
 
+/* Titles. */
+void pivot_table_set_title (struct pivot_table *, struct pivot_value *);
+void pivot_table_set_subtype (struct pivot_table *, struct pivot_value *);
+void pivot_table_set_corner_text (struct pivot_table *, struct pivot_value *);
+void pivot_table_set_caption (struct pivot_table *, struct pivot_value *);
+
 /* Axes. */
 void pivot_table_swap_axes (struct pivot_table *,
                             enum pivot_axis_type, enum pivot_axis_type);