+/* Pivot table looks. */
+
+static const struct pivot_table_look *
+default_look (const struct pivot_table_look *new)
+{
+ static struct pivot_table_look *look;
+ if (new)
+ {
+ pivot_table_look_unref (look);
+ look = pivot_table_look_ref (new);
+ }
+ else if (!look)
+ {
+ char *error = pivot_table_look_read ("default.stt", &look);
+ if (error)
+ look = pivot_table_look_ref (pivot_table_look_builtin_default ());
+ }
+ return look;
+}
+
+const struct pivot_table_look *
+pivot_table_look_get_default (void)
+{
+ return default_look (NULL);
+}
+
+void
+pivot_table_look_set_default (const struct pivot_table_look *look)
+{
+ default_look (look);
+}
+
+char * WARN_UNUSED_RESULT
+pivot_table_look_read (const char *name, struct pivot_table_look **lookp)
+{
+ *lookp = NULL;
+
+ /* Construct search path. */
+ const char *path[4];
+ size_t n = 0;
+ path[n++] = ".";
+ const char *home = getenv ("HOME");
+ if (home != NULL)
+ path[n++] = xasprintf ("%s/.pspp/looks", home);
+ char *allocated;
+ path[n++] = relocate2 (PKGDATADIR "/looks", &allocated);
+ path[n++] = NULL;
+
+ /* Search path. */
+ char *file = fn_search_path (name, (char **) path);
+ if (!file)
+ {
+ char *name2 = xasprintf ("%s.stt", name);
+ file = fn_search_path (name2, (char **) path);
+ free (name2);
+ }
+ free (allocated);
+ if (!file)
+ return xasprintf ("%s: not found", name);
+
+ /* Read file. */
+ char *error = spv_table_look_read (file, lookp);
+ free (file);
+ return error;
+}
+
+const struct pivot_table_look *
+pivot_table_look_builtin_default (void)
+{
+ static struct pivot_table_look look = {
+ .ref_cnt = 1,
+
+ .omit_empty = true,
+ .row_labels_in_corner = true,
+ .width_ranges = {
+ [TABLE_HORZ] = { 36, 72 },
+ [TABLE_VERT] = { 36, 120 },
+ },
+
+ .areas = {
+#define AREA(BOLD, H, V, L, R, T, B) { \
+ .cell_style = { \
+ .halign = TABLE_HALIGN_##H, \
+ .valign = TABLE_VALIGN_##V, \
+ .margin = { [TABLE_HORZ][0] = L, [TABLE_HORZ][1] = R, \
+ [TABLE_VERT][0] = T, [TABLE_VERT][1] = B }, \
+ }, \
+ .font_style = { \
+ .bold = BOLD, \
+ .fg = { [0] = CELL_COLOR_BLACK, [1] = CELL_COLOR_BLACK}, \
+ .bg = { [0] = CELL_COLOR_WHITE, [1] = CELL_COLOR_WHITE}, \
+ .size = 9, \
+ .typeface = (char *) "Sans Serif", \
+ }, \
+ }
+ [PIVOT_AREA_TITLE] = AREA(true, CENTER, CENTER, 8,11,1,8),
+ [PIVOT_AREA_CAPTION] = AREA(false, LEFT, TOP, 8,11,1,1),
+ [PIVOT_AREA_FOOTER] = AREA(false, LEFT, TOP, 11, 8,2,3),
+ [PIVOT_AREA_CORNER] = AREA(false, LEFT, BOTTOM, 8,11,1,1),
+ [PIVOT_AREA_COLUMN_LABELS] = AREA(false, CENTER, BOTTOM, 8,11,1,3),
+ [PIVOT_AREA_ROW_LABELS] = AREA(false, LEFT, TOP, 8,11,1,3),
+ [PIVOT_AREA_DATA] = AREA(false, MIXED, TOP, 8,11,1,1),
+ [PIVOT_AREA_LAYERS] = AREA(false, LEFT, BOTTOM, 8,11,1,3),
+#undef AREA
+ },
+
+ .borders = {
+#define BORDER(STROKE) { .stroke = STROKE, .color = CELL_COLOR_BLACK }
+ [PIVOT_BORDER_TITLE] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_OUTER_LEFT] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_OUTER_TOP] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_OUTER_RIGHT] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_OUTER_BOTTOM] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_INNER_LEFT] = BORDER(TABLE_STROKE_THICK),
+ [PIVOT_BORDER_INNER_TOP] = BORDER(TABLE_STROKE_THICK),
+ [PIVOT_BORDER_INNER_RIGHT] = BORDER(TABLE_STROKE_THICK),
+ [PIVOT_BORDER_INNER_BOTTOM] = BORDER(TABLE_STROKE_THICK),
+ [PIVOT_BORDER_DATA_LEFT] = BORDER(TABLE_STROKE_THICK),
+ [PIVOT_BORDER_DATA_TOP] = BORDER(TABLE_STROKE_THICK),
+ [PIVOT_BORDER_DIM_ROW_HORZ] = BORDER(TABLE_STROKE_SOLID),
+ [PIVOT_BORDER_DIM_ROW_VERT] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_DIM_COL_HORZ] = BORDER(TABLE_STROKE_SOLID),
+ [PIVOT_BORDER_DIM_COL_VERT] = BORDER(TABLE_STROKE_SOLID),
+ [PIVOT_BORDER_CAT_ROW_HORZ] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_CAT_ROW_VERT] = BORDER(TABLE_STROKE_NONE),
+ [PIVOT_BORDER_CAT_COL_HORZ] = BORDER(TABLE_STROKE_SOLID),
+ [PIVOT_BORDER_CAT_COL_VERT] = BORDER(TABLE_STROKE_SOLID),
+ },
+ };
+
+ return &look;
+}
+
+struct pivot_table_look *
+pivot_table_look_new_builtin_default (void)
+{
+ return pivot_table_look_unshare (
+ pivot_table_look_ref (pivot_table_look_builtin_default ()));
+}
+
+struct pivot_table_look *
+pivot_table_look_ref (const struct pivot_table_look *look_)
+{
+ assert (look_->ref_cnt > 0);
+
+ struct pivot_table_look *look = CONST_CAST (struct pivot_table_look *, look_);
+ look->ref_cnt++;
+ return look;
+}
+
+static char *
+xstrdup_if_nonempty (const char *s)
+{
+ return s && s[0] ? xstrdup (s) : NULL;
+}
+
+struct pivot_table_look *
+pivot_table_look_unshare (struct pivot_table_look *old)
+{
+ assert (old->ref_cnt > 0);
+ if (old->ref_cnt == 1)
+ return old;
+
+ pivot_table_look_unref (old);
+
+ struct pivot_table_look *new = xmemdup (old, sizeof *old);
+ new->ref_cnt = 1;
+ new->name = xstrdup_if_nonempty (old->name);
+ for (size_t i = 0; i < PIVOT_N_AREAS; i++)
+ table_area_style_copy (NULL, &new->areas[i], &old->areas[i]);
+ new->continuation = xstrdup_if_nonempty (old->continuation);
+
+ return new;
+}
+
+void
+pivot_table_look_unref (struct pivot_table_look *look)
+{
+ if (look)
+ {
+ assert (look->ref_cnt > 0);
+ if (!--look->ref_cnt)
+ {
+ free (look->name);
+ for (size_t i = 0; i < PIVOT_N_AREAS; i++)
+ table_area_style_uninit (&look->areas[i]);
+ free (look->continuation);
+ free (look);
+ }
+ }
+}
+\f