From e804300543c1c37393cb6d777e8a2f05686ff2cb Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 26 Dec 2020 17:54:56 -0800 Subject: [PATCH] html: Pop up tooltip with table notes in output. I can't see how to do this for ODP or PDF output. (PDF supports tooltips but Cairo doesn't expose the feature; ODP doesn't seem to support it at all.) --- src/output/ascii.c | 2 +- src/output/html.c | 9 ++++++++- src/output/pivot-output.c | 2 +- src/output/pivot-table.h | 2 +- src/output/table-item.c | 29 ++++++++++++++++++++++++++--- src/output/table-item.h | 14 +++++++++----- src/output/text-item.c | 2 +- tests/output/render-test.c | 2 +- 8 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/output/ascii.c b/src/output/ascii.c index ce3356954b..07b0558016 100644 --- a/src/output/ascii.c +++ b/src/output/ascii.c @@ -485,7 +485,7 @@ static void ascii_output_text (struct ascii_driver *a, const char *text) { ascii_output_table_item_unref ( - a, table_item_create (table_from_string (text), NULL, NULL)); + a, table_item_create (table_from_string (text), NULL, NULL, NULL)); } static void diff --git a/src/output/html.c b/src/output/html.c index b8679b57ec..c1613c4fea 100644 --- a/src/output/html.c +++ b/src/output/html.c @@ -505,7 +505,14 @@ html_output_table (struct html_driver *html, const struct table_item *item) bool tfoot = false; int y; - fputs ("\n", html->file); + fputs ("file); + if (item->notes) + { + fputs (" title=\"", html->file); + escape_string (html->file, item->notes, " ", "\n"); + putc ('"', html->file); + } + fputs (">\n", html->file); const struct table_item_text *caption = table_item_get_caption (item); if (caption) diff --git a/src/output/pivot-output.c b/src/output/pivot-output.c index 31d68c8d37..ea0cdd4a6e 100644 --- a/src/output/pivot-output.c +++ b/src/output/pivot-output.c @@ -451,7 +451,7 @@ pivot_table_submit_layer (const struct pivot_table *pt, free (column_enumeration); free (row_enumeration); - struct table_item *ti = table_item_create (table, NULL, NULL); + struct table_item *ti = table_item_create (table, NULL, NULL, pt->notes); if (pt->title) { diff --git a/src/output/pivot-table.h b/src/output/pivot-table.h index c613e55efc..2770c611d6 100644 --- a/src/output/pivot-table.h +++ b/src/output/pivot-table.h @@ -467,7 +467,7 @@ struct pivot_table struct pivot_value *subtype; /* Same as spv_item's subtype. */ struct pivot_value *corner_text; struct pivot_value *caption; - char *notes; + char *notes; /* Shown as tooltip. */ /* Dimensions. */ struct pivot_dimension **dimensions; diff --git a/src/output/table-item.c b/src/output/table-item.c index 1ead1f92f6..9355a77a11 100644 --- a/src/output/table-item.c +++ b/src/output/table-item.c @@ -120,10 +120,11 @@ table_item_layers_destroy (struct table_item_layers *layers) } /* Initializes ITEM as a table item for rendering TABLE. The new table item - initially has the specified TITLE and CAPTION, which may each be NULL. The - caller retains ownership of TITLE and CAPTION. */ + initially has the specified TITLE, CAPTION, and NOTES, which may each be + NULL. The caller retains ownership of TITLE, CAPTION, and NOTES. */ struct table_item * -table_item_create (struct table *table, const char *title, const char *caption) +table_item_create (struct table *table, const char *title, const char *caption, + const char *notes) { struct table_item *item = xmalloc (sizeof *item); output_item_init (&item->output_item, &table_item_class); @@ -131,6 +132,7 @@ table_item_create (struct table *table, const char *title, const char *caption) item->title = table_item_text_create (title); item->layers = NULL; item->caption = table_item_text_create (caption); + item->notes = notes ? xstrdup (notes) : NULL; item->pt = NULL; return item; } @@ -208,6 +210,26 @@ table_item_set_caption (struct table_item *item, item->caption = table_item_text_clone (caption); } +/* Returns ITEM's notes, which is a null pointer if ITEM has no notes. */ +const char * +table_item_get_notes (const struct table_item *item) +{ + return item->notes; +} + +/* Sets ITEM's notes to NOTES, replacing any previous notes. Specify NULL for + NOTES to clear any notes from ITEM. The caller retains ownership of + NOTES. + + This function may only be used on a table_item that is unshared.*/ +void +table_item_set_notes (struct table_item *item, const char *notes) +{ + assert (!table_item_is_shared (item)); + free (item->notes); + item->notes = notes ? xstrdup (notes) : NULL; +} + /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership to the output subsystem. */ void @@ -223,6 +245,7 @@ table_item_destroy (struct output_item *output_item) table_item_text_destroy (item->title); table_item_text_destroy (item->caption); table_item_layers_destroy (item->layers); + free (item->notes); pivot_table_unref (item->pt); table_unref (item->table); free (item); diff --git a/src/output/table-item.h b/src/output/table-item.h index eb3dcb16ea..e4c40ab228 100644 --- a/src/output/table-item.h +++ b/src/output/table-item.h @@ -70,16 +70,17 @@ void table_item_layers_destroy (struct table_item_layers *); of the accessor functions defined below. */ struct table_item { - struct output_item output_item; /* Superclass. */ - struct table *table; /* The table to be rendered. */ - struct table_item_text *title; /* Null if there is no title. */ - struct table_item_text *caption; /* Null if there is no caption. */ + struct output_item output_item; /* Superclass. */ + struct table *table; /* The table to be rendered. */ + struct table_item_text *title; /* Null if there is no title. */ + struct table_item_text *caption; /* Null if there is no caption. */ struct table_item_layers *layers; /* Null if there is no layer info. */ + char *notes; /* Shown as tooltip. */ struct pivot_table *pt; }; struct table_item *table_item_create (struct table *, const char *title, - const char *caption); + const char *caption, const char *notes); const struct table *table_item_get_table (const struct table_item *); @@ -96,6 +97,9 @@ const struct table_item_text *table_item_get_caption ( const struct table_item *); void table_item_set_caption (struct table_item *, const struct table_item_text *); + +const char *table_item_get_notes (const struct table_item *); +void table_item_set_notes (struct table_item *, const char *notes); /* This boilerplate for table_item, a subclass of output_item, was autogenerated by mk-class-boilerplate. */ diff --git a/src/output/text-item.c b/src/output/text-item.c index 95bb7723c8..8c7f0dc51a 100644 --- a/src/output/text-item.c +++ b/src/output/text-item.c @@ -137,7 +137,7 @@ text_item_to_table_item (struct text_item *text_item) if (text_item->type == TEXT_ITEM_SYNTAX || text_item->type == TEXT_ITEM_LOG) opts |= TAB_FIX; table_text (tab, 0, 0, opts, text_item_get_text (text_item)); - struct table_item *table_item = table_item_create (tab, NULL, NULL); + struct table_item *table_item = table_item_create (tab, NULL, NULL, NULL); text_item_unref (text_item); return table_item; } diff --git a/tests/output/render-test.c b/tests/output/render-test.c index 6b0aa73c4a..0e07f87dc1 100644 --- a/tests/output/render-test.c +++ b/tests/output/render-test.c @@ -114,7 +114,7 @@ main (int argc, char **argv) } table = tables[n_tables - 1]; - table_item_submit (table_item_create (table, NULL, NULL)); + table_item_submit (table_item_create (table, NULL, NULL, NULL)); free (tables); } else -- 2.30.2