From e5c7702870a1bddac6b46ae82c90864f94172ca9 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 25 Jan 2020 19:45:04 +0100 Subject: Implement loading stylesheets --- libstorycode/stylesheet.c | 17 ++++++++++ libstorycode/stylesheet.h | 3 ++ src/narrative_window.c | 79 +++++++++++++++++++++-------------------------- 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/libstorycode/stylesheet.c b/libstorycode/stylesheet.c index bc22852..16cf218 100644 --- a/libstorycode/stylesheet.c +++ b/libstorycode/stylesheet.c @@ -593,6 +593,23 @@ int stylesheet_set_from_storycode(Stylesheet *ss, const char *sc) } +int stylesheet_set_from_file(Stylesheet *ss, GFile *file) +{ + GBytes *bytes; + const char *text; + size_t len; + int r = 0; + + bytes = g_file_load_bytes(file, NULL, NULL, NULL); + if ( bytes == NULL ) return 0; + + text = g_bytes_get_data(bytes, &len); + r = stylesheet_set_from_storycode(ss, text); + g_bytes_unref(bytes); + return r; +} + + double lcalc(struct length l, double pd) { if ( l.unit == LENGTH_UNIT ) { diff --git a/libstorycode/stylesheet.h b/libstorycode/stylesheet.h index 82f90e8..4a8e58c 100644 --- a/libstorycode/stylesheet.h +++ b/libstorycode/stylesheet.h @@ -27,6 +27,8 @@ #include #endif +#include + typedef struct _stylesheet Stylesheet; enum alignment @@ -80,6 +82,7 @@ enum gradient extern Stylesheet *stylesheet_new(void); extern void stylesheet_free(Stylesheet *s); extern int stylesheet_set_from_storycode(Stylesheet *ss, const char *sc); +extern int stylesheet_set_from_file(Stylesheet *ss, GFile *file); extern int stylesheet_get_slide_default_size(Stylesheet *s, double *w, double *h); diff --git a/src/narrative_window.c b/src/narrative_window.c index 9ccdafa..073a05e 100644 --- a/src/narrative_window.c +++ b/src/narrative_window.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #define _(x) gettext(x) @@ -237,32 +238,26 @@ static void save_sig(GSimpleAction *action, GVariant *parameter, gpointer vp) static gint load_ss_response_sig(GtkWidget *d, gint response, NarrativeWindow *nw) { -// if ( response == GTK_RESPONSE_ACCEPT ) { -// -// GFile *file; -// Stylesheet *new_ss; -// -// file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(d)); -// -// new_ss = stylesheet_load(file); -// if ( new_ss != NULL ) { -// -// stylesheet_free(nw->p->stylesheet); -// nw->n->stylesheet = new_ss; -// sc_editor_set_stylesheet(nw->nv, new_ss); -// -// /* Full rerender */ -// sc_editor_set_scblock(nw->nv, nw->dummy_top); -// -// } else { -// fprintf(stderr, _("Failed to load stylesheet\n")); -// } -// -// g_object_unref(file); -// -// } -// -// gtk_widget_destroy(d); + if ( response == GTK_RESPONSE_ACCEPT ) { + + GFile *file; + + file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(d)); + + if ( stylesheet_set_from_file(narrative_get_stylesheet(nw->n), file) ) { + + fprintf(stderr, _("Failed to load stylesheet\n")); + + } else { + + gtk_narrative_view_redraw(GTK_NARRATIVE_VIEW(nw->nv)); + } + + g_object_unref(file); + + } + + gtk_widget_destroy(d); return 0; } @@ -298,22 +293,20 @@ static void edit_ss_sig(GSimpleAction *action, GVariant *parameter, static void load_ss_sig(GSimpleAction *action, GVariant *parameter, gpointer vp) { -// //SCBlock *nsblock; -// //SCBlock *templ; -// NarrativeWindow *nw = vp; -// GtkWidget *d; -// -// d = gtk_file_chooser_dialog_new(_("Load stylesheet"), -// GTK_WINDOW(nw->window), -// GTK_FILE_CHOOSER_ACTION_OPEN, -// _("_Cancel"), GTK_RESPONSE_CANCEL, -// _("_Open"), GTK_RESPONSE_ACCEPT, -// NULL); -// -// g_signal_connect(G_OBJECT(d), "response", -// G_CALLBACK(load_ss_response_sig), nw); -// -// gtk_widget_show_all(d); + NarrativeWindow *nw = vp; + GtkWidget *d; + + d = gtk_file_chooser_dialog_new(_("Load stylesheet"), + GTK_WINDOW(nw->window), + GTK_FILE_CHOOSER_ACTION_OPEN, + _("_Cancel"), GTK_RESPONSE_CANCEL, + _("_Open"), GTK_RESPONSE_ACCEPT, + NULL); + + g_signal_connect(G_OBJECT(d), "response", + G_CALLBACK(load_ss_response_sig), nw); + + gtk_widget_show_all(d); } @@ -724,7 +717,7 @@ GActionEntry nw_entries[] = { { "prestitle", add_prestitle_sig, NULL, NULL, NULL }, { "segstart", add_segstart_sig, NULL, NULL, NULL }, { "segend", add_segend_sig, NULL, NULL, NULL }, - { "loadstylesheet", load_ss_sig, NULL, NULL, NULL }, + { "loadstyle", load_ss_sig, NULL, NULL, NULL }, { "stylesheet", edit_ss_sig, NULL, NULL, NULL }, { "startslideshow", start_slideshow_sig, NULL, NULL, NULL }, { "startslideshowhere", start_slideshow_here_sig, NULL, NULL, NULL }, -- cgit v1.2.3