From 27e08c96c71183edea9be3e189d7152c19b1beac Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 21 Oct 2018 18:03:01 +0200 Subject: Try .ss for stylesheet --- src/narrative_window.c | 8 ++++---- src/presentation.c | 20 +++++++++++++++++++- src/sc_interp.c | 2 ++ src/stylesheet.c | 14 ++++++++++++-- src/stylesheet.h | 4 +++- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/narrative_window.c b/src/narrative_window.c index ead78bd..b287f61 100644 --- a/src/narrative_window.c +++ b/src/narrative_window.c @@ -190,12 +190,12 @@ static gint load_ss_response_sig(GtkWidget *d, gint response, { if ( response == GTK_RESPONSE_ACCEPT ) { - char *filename; + GFile *file; Stylesheet *new_ss; - filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(d)); + file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(d)); - new_ss = stylesheet_load(filename); + new_ss = stylesheet_load(file); if ( new_ss != NULL ) { stylesheet_free(nw->p->stylesheet); @@ -209,7 +209,7 @@ static gint load_ss_response_sig(GtkWidget *d, gint response, fprintf(stderr, _("Failed to load\n")); } - g_free(filename); + g_object_unref(file); } diff --git a/src/presentation.c b/src/presentation.c index 1aa126a..a33a541 100644 --- a/src/presentation.c +++ b/src/presentation.c @@ -245,6 +245,8 @@ int load_presentation(struct presentation *p, GFile *file) { int r = 0; char *everything; + GFile *ssfile; + gchar *ssuri; assert(p->completely_empty); @@ -266,7 +268,23 @@ int load_presentation(struct presentation *p, GFile *file) return r; /* Error */ } - p->stylesheet = stylesheet_load("stylesheet.json"); /* FIXME: ! */ + p->stylesheet = NULL; + ssuri = g_file_get_uri(file); + if ( ssuri != NULL ) { + size_t l = strlen(ssuri); + if ( ssuri[l-3] == '.' && ssuri[l-2] == 's' && ssuri[l-1] =='c' ) { + ssuri[l-1] = 's'; + ssfile = g_file_new_for_uri(ssuri); + p->stylesheet = stylesheet_load(ssfile); + g_object_unref(ssfile); + g_free(ssuri); + } + } + if ( p->stylesheet == NULL ) { + ssfile = g_file_new_for_path("./stylesheet.json"); + p->stylesheet = stylesheet_load(ssfile); + g_object_unref(ssfile); + } set_slide_size_from_stylesheet(p); diff --git a/src/sc_interp.c b/src/sc_interp.c index 780663f..010b100 100644 --- a/src/sc_interp.c +++ b/src/sc_interp.c @@ -945,6 +945,8 @@ static void apply_style(SCInterpreter *scin, Stylesheet *ss, const char *path) return; } + if ( ss == NULL ) return; + /* Font */ strcpy(fullpath, path); strcat(fullpath, ".font"); diff --git a/src/stylesheet.c b/src/stylesheet.c index 228c331..020107e 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -29,8 +29,10 @@ #include #include #include +#include #include "stylesheet.h" +#include "utils.h" struct _stylesheet { @@ -38,19 +40,27 @@ struct _stylesheet { }; -Stylesheet *stylesheet_load(const char *filename) +Stylesheet *stylesheet_load(GFile *file) { JsonParser *parser; gboolean r; GError *err = NULL; Stylesheet *ss; + char *everything; + gsize len; ss = calloc(1, sizeof(Stylesheet)); if ( ss == NULL ) return NULL; parser = json_parser_new(); - r = json_parser_load_from_file(parser, filename, &err); + if ( !g_file_load_contents(file, NULL, &everything, &len, NULL, NULL) ) { + fprintf(stderr, _("Failed to load stylesheet '%s'\n"), + g_file_get_uri(file)); + return NULL; + } + + r = json_parser_load_from_data(parser, everything, len, &err); if ( r == FALSE ) { fprintf(stderr, "Failed to load style sheet: '%s'\n", err->message); return NULL; diff --git a/src/stylesheet.h b/src/stylesheet.h index 59afa7d..aa5867f 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -27,9 +27,11 @@ #include #endif +#include + typedef struct _stylesheet Stylesheet; -extern Stylesheet *stylesheet_load(const char *filename); +extern Stylesheet *stylesheet_load(GFile *file); extern char *stylesheet_lookup(Stylesheet *ss, const char *path); extern void stylesheet_free(Stylesheet *ss); -- cgit v1.2.3