aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2019-10-02 18:41:13 +0200
committerThomas White <taw@physics.org>2019-10-05 17:03:43 +0200
commit8e7ee1c1c1e4aedd0b2720ce9bb1cc199bcd8678 (patch)
tree1175488ffae15320028ec9a1d030509470c66412
parent0667ba5a597f5b94c68f414909453648eab2835c (diff)
Get slide out of parser context
-rw-r--r--libstorycode/gtk/gtkslideview.c10
-rw-r--r--libstorycode/scparse_priv.h1
-rw-r--r--libstorycode/slide.c58
-rw-r--r--libstorycode/slide.h18
-rw-r--r--libstorycode/storycode.y30
-rw-r--r--src/slide_window.c8
6 files changed, 69 insertions, 56 deletions
diff --git a/libstorycode/gtk/gtkslideview.c b/libstorycode/gtk/gtkslideview.c
index 339723f..6cf0ec5 100644
--- a/libstorycode/gtk/gtkslideview.c
+++ b/libstorycode/gtk/gtkslideview.c
@@ -827,19 +827,23 @@ static SlideItem *create_image(GtkSlideView *e, const char *filename,
double cx, double cy, double w, double h)
{
struct frame_geom geom;
+ SlideItem *item;
char *fn = strdup(filename);
if ( fn == NULL ) return NULL;
geom.x.len = cx; geom.x.unit = LENGTH_UNIT;
geom.y.len = cy; geom.y.unit = LENGTH_UNIT;
geom.w.len = w; geom.w.unit = LENGTH_UNIT;
geom.h.len = h; geom.h.unit = LENGTH_UNIT;
- return slide_add_image(e->slide, fn, geom);
+ item = slide_item_image(fn, geom);
+ slide_add_item(e->slide, item);
+ return item;
}
static SlideItem *create_frame(GtkSlideView *e, double cx, double cy,
double w, double h)
{
+ SlideItem *item;
struct frame_geom geom;
struct text_run *runs;
int nruns = 1;
@@ -865,7 +869,9 @@ static SlideItem *create_frame(GtkSlideView *e, double cx, double cy,
geom.y.len = cy; geom.y.unit = LENGTH_UNIT;
geom.w.len = w; geom.w.unit = LENGTH_UNIT;
geom.h.len = h; geom.h.unit = LENGTH_UNIT;
- return slide_add_text(e->slide, &runs, &nruns, 1, geom, ALIGN_INHERIT);
+ item = slide_item_text(&runs, &nruns, 1, geom, ALIGN_INHERIT);
+ slide_add_item(e->slide, item);
+ return item;
}
diff --git a/libstorycode/scparse_priv.h b/libstorycode/scparse_priv.h
index 2b1dd16..7e1e6db 100644
--- a/libstorycode/scparse_priv.h
+++ b/libstorycode/scparse_priv.h
@@ -40,7 +40,6 @@ enum style_mask
struct scpctx
{
Narrative *n;
- Slide *s;
/* Current style or frame options.
* These will be copied to a stylesheet entry or frame when the
diff --git a/libstorycode/slide.c b/libstorycode/slide.c
index 1bcec6e..09c91ba 100644
--- a/libstorycode/slide.c
+++ b/libstorycode/slide.c
@@ -72,27 +72,35 @@ void slide_delete_item(Slide *s, SlideItem *item)
}
-static SlideItem *add_item(Slide *s)
+static SlideItem *slide_item_new()
{
- SlideItem *new_items;
SlideItem *item;
+ item = malloc(sizeof(struct _slideitem));
+ if ( item == NULL ) return NULL;
+ item->paras = NULL;
+ return item;
+}
+
+
+void slide_add_item(Slide *s, SlideItem *item)
+{
+ SlideItem *new_items;
new_items = realloc(s->items, (s->n_items+1)*sizeof(SlideItem));
- if ( new_items == NULL ) return NULL;
+ if ( new_items == NULL ) return;
s->items = new_items;
- item = &s->items[s->n_items++];
- item->paras = NULL;
-
- return item;
+ /* Copy contents and free top-level */
+ s->items[s->n_items++] = *item;
+ free(item);
}
-SlideItem *slide_add_image(Slide *s, char *filename, struct frame_geom geom)
+SlideItem *slide_item_image(char *filename, struct frame_geom geom)
{
SlideItem *item;
- item = add_item(s);
+ item = slide_item_new();
if ( item == NULL ) return NULL;
item->type = SLIDE_ITEM_IMAGE;
@@ -110,20 +118,20 @@ SlideItem *slide_add_image(Slide *s, char *filename, struct frame_geom geom)
* Will take ownership of the arrays of text runs, but not the array of arrays
* Will NOT take ownership of the array of numbers of runs
*/
-static SlideItem *add_text_item(Slide *s, struct text_run **paras, int *n_runs, int n_paras,
+static SlideItem *text_item_new(struct text_run **paras, int *n_runs, int n_paras,
struct frame_geom geom, enum alignment alignment,
enum slide_item_type slide_item)
{
int i;
SlideItem *item;
- item = add_item(s);
+ item = slide_item_new();
if ( item == NULL ) return NULL;
item->type = slide_item;
item->paras = malloc(n_paras*sizeof(struct slide_text_paragraph));
if ( item->paras == NULL ) {
- s->n_items--;
+ free(item);
return NULL;
}
item->n_paras = n_paras;
@@ -141,28 +149,24 @@ static SlideItem *add_text_item(Slide *s, struct text_run **paras, int *n_runs,
}
-int slide_add_footer(Slide *s)
+SlideItem *slide_item_footer()
{
- SlideItem *item;
-
- item = add_item(s);
- if ( item == NULL ) return 1;
-
+ SlideItem *item = slide_item_new();
+ if ( item == NULL ) return NULL;
item->type = SLIDE_ITEM_FOOTER;
-
- return 0;
+ return item;
}
-SlideItem *slide_add_text(Slide *s, struct text_run **paras, int *n_runs, int n_paras,
- struct frame_geom geom, enum alignment alignment)
+SlideItem *slide_item_text(struct text_run **paras, int *n_runs, int n_paras,
+ struct frame_geom geom, enum alignment alignment)
{
- return add_text_item(s, paras, n_runs, n_paras, geom, alignment,
+ return text_item_new(paras, n_runs, n_paras, geom, alignment,
SLIDE_ITEM_TEXT);
}
-SlideItem *slide_add_slidetitle(Slide *s, struct text_run **paras, int *n_runs, int n_paras)
+SlideItem *slide_item_slidetitle(struct text_run **paras, int *n_runs, int n_paras)
{
struct frame_geom geom;
@@ -176,12 +180,12 @@ SlideItem *slide_add_slidetitle(Slide *s, struct text_run **paras, int *n_runs,
geom.h.len = 1.0;
geom.h.unit = LENGTH_FRAC;
- return add_text_item(s, paras, n_runs, n_paras, geom, ALIGN_INHERIT,
+ return text_item_new(paras, n_runs, n_paras, geom, ALIGN_INHERIT,
SLIDE_ITEM_SLIDETITLE);
}
-SlideItem *slide_add_prestitle(Slide *s, struct text_run **paras, int *n_runs, int n_paras)
+SlideItem *slide_item_prestitle(struct text_run **paras, int *n_runs, int n_paras)
{
struct frame_geom geom;
@@ -195,7 +199,7 @@ SlideItem *slide_add_prestitle(Slide *s, struct text_run **paras, int *n_runs, i
geom.h.len = 1.0;
geom.h.unit = LENGTH_FRAC;
- return add_text_item(s, paras, n_runs, n_paras, geom, ALIGN_INHERIT,
+ return text_item_new(paras, n_runs, n_paras, geom, ALIGN_INHERIT,
SLIDE_ITEM_PRESTITLE);
}
diff --git a/libstorycode/slide.h b/libstorycode/slide.h
index 87635b8..f880c2e 100644
--- a/libstorycode/slide.h
+++ b/libstorycode/slide.h
@@ -38,18 +38,22 @@ typedef struct _slideitem SlideItem;
extern Slide *slide_new(void);
extern void slide_free(Slide *s);
+extern void slide_add_item(Slide *s, SlideItem *item);
extern void slide_delete_item(Slide *s, SlideItem *item);
-extern SlideItem *slide_add_image(Slide *s, char *filename, struct frame_geom geom);
-extern SlideItem *slide_add_text(Slide *s, struct text_run **paras, int *n_runs, int n_paras,
- struct frame_geom geom, enum alignment alignment);
-extern int slide_add_footer(Slide *s);
-extern SlideItem *slide_add_slidetitle(Slide *s, struct text_run **paras, int *n_runs, int n_paras);
-extern SlideItem *slide_add_prestitle(Slide *s, struct text_run **paras, int *n_runs, int n_paras);
extern int slide_set_logical_size(Slide *s, double w, double h);
-
extern int slide_get_logical_size(Slide *s, Stylesheet *ss, double *w, double *h);
+
+/* Constructors for slide items */
+extern SlideItem *slide_item_image(char *filename, struct frame_geom geom);
+extern SlideItem *slide_item_text(struct text_run **paras, int *n_runs, int n_paras,
+ struct frame_geom geom, enum alignment alignment);
+extern SlideItem *slide_item_footer(void);
+extern SlideItem *slide_item_slidetitle(struct text_run **paras, int *n_runs, int n_paras);
+extern SlideItem *slide_item_prestitle(struct text_run **paras, int *n_runs, int n_paras);
+
+
/* Slide items */
extern void slide_item_get_geom(SlideItem *item, Stylesheet *ss,
double *x, double *y, double *w, double *h,
diff --git a/libstorycode/storycode.y b/libstorycode/storycode.y
index b6ee9e5..f53ee87 100644
--- a/libstorycode/storycode.y
+++ b/libstorycode/storycode.y
@@ -51,7 +51,8 @@
Stylesheet *ss;
Narrative *n;
- Slide *s;
+ Slide *slide;
+ SlideItem *slide_item;
char *str;
struct text_run run;
@@ -102,8 +103,9 @@
%token TEXT_START
%type <n> narrative
-%type <s> slide
+%type <slide> slide_parts
%type <ss> stylesheet
+%type <slide_item> slide_part
%type <many_paragraphs> textframe
%type <many_paragraphs> slide_prestitle
%type <many_paragraphs> slidetitle
@@ -134,10 +136,6 @@
%initial-action
{
ctx->n = narrative_new();
-
- /* The slide currently being created.
- * Will be added to the narrative when complete */
- ctx->s = slide_new();
ctx->mask = 0;
}
@@ -310,32 +308,30 @@ one_or_more_runs:
/* -------- Slide -------- */
slide:
- SLIDE '{' slide_parts '}' { narrative_add_slide(ctx->n, ctx->s);
- /* New work in progress object */
- ctx->s = slide_new(); }
+ SLIDE '{' slide_parts '}' { narrative_add_slide(ctx->n, $3); }
;
-slide_parts:
+slide_parts: { $<slide>$ = slide_new(); }
%empty
-| slide_parts slide_part
+| slide_parts slide_part { slide_add_item($<slide>$, $2); }
;
slide_part:
slide_prestitle { struct text_run **cp;
int *n_runs;
cp = combine_paras($1, &n_runs);
- slide_add_prestitle(ctx->s, cp, n_runs, $1.n_paras); }
+ $$ = slide_item_prestitle(cp, n_runs, $1.n_paras); }
| textframe { struct text_run **cp;
int *n_runs;
cp = combine_paras($1, &n_runs);
- slide_add_text(ctx->s, cp, n_runs, $1.n_paras,
- ctx->geom, ctx->alignment); }
+ $$ = slide_item_text(cp, n_runs, $1.n_paras,
+ ctx->geom, ctx->alignment); }
| slidetitle { struct text_run **cp;
int *n_runs;
cp = combine_paras($1, &n_runs);
- slide_add_slidetitle(ctx->s, cp, n_runs, $1.n_paras); }
-| imageframe { slide_add_image(ctx->s, $1, ctx->geom); }
-| FOOTER { slide_add_footer(ctx->s); }
+ $$ = slide_item_slidetitle(cp, n_runs, $1.n_paras); }
+| imageframe { $$ = slide_item_image($1, ctx->geom); }
+| FOOTER { $$ = slide_item_footer(); }
;
slide_prestitle:
diff --git a/src/slide_window.c b/src/slide_window.c
index fa172ea..8a0db2c 100644
--- a/src/slide_window.c
+++ b/src/slide_window.c
@@ -58,6 +58,7 @@ struct _slidewindow
static void insert_slidetitle_sig(GSimpleAction *action, GVariant *parameter,
gpointer vp)
{
+ SlideItem *item;
SlideWindow *sw = vp;
struct text_run *runs;
int nruns = 1;
@@ -67,7 +68,8 @@ static void insert_slidetitle_sig(GSimpleAction *action, GVariant *parameter,
runs[0].type = TEXT_RUN_NORMAL;
runs[0].text = strdup("Slide title");
- slide_add_slidetitle(sw->slide, &runs, &nruns, 1);
+ item = slide_item_slidetitle(&runs, &nruns, 1);
+ slide_add_item(sw->slide, item);
gtk_slide_view_set_slide(sw->sv, sw->slide);
}
@@ -83,6 +85,7 @@ static gint insert_image_response_sig(GtkWidget *d, gint response, SlideWindow *
if ( response == GTK_RESPONSE_ACCEPT ) {
char *filename;
+ SlideItem *item;
struct frame_geom geom;
char *fn;
double slide_w, slide_h;
@@ -116,7 +119,8 @@ static gint insert_image_response_sig(GtkWidget *d, gint response, SlideWindow *
geom.h.len = 1.0; geom.h.unit = LENGTH_FRAC;
}
- slide_add_image(sw->slide, fn, geom);
+ item = slide_item_image(fn, geom);
+ slide_add_item(sw->slide, item);
}
gtk_widget_destroy(d);