aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2019-12-26 20:31:35 +0100
committerThomas White <taw@physics.org>2019-12-26 20:31:35 +0100
commitaf1fccb1e2bd7355f4e00d014ccad232240aced7 (patch)
tree5321132b46da68c733d8f0c6949f53ac3a2c7d9f
parent718314e8b9301f826f35a05fcd9567572bd62b54 (diff)
WIP on copy and pastewip2
-rw-r--r--libstorycode/gtk/gtknarrativeview.c61
-rw-r--r--libstorycode/narrative.c40
-rw-r--r--libstorycode/narrative.h2
3 files changed, 77 insertions, 26 deletions
diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c
index b26c86b..df71f1e 100644
--- a/libstorycode/gtk/gtknarrativeview.c
+++ b/libstorycode/gtk/gtknarrativeview.c
@@ -409,15 +409,33 @@ void sc_editor_paste(GtkNarrativeView *e)
static void clipboard_get(GtkClipboard *cb, GtkSelectionData *seldata,
guint info, gpointer data)
{
- char *t = data;
+ GtkNarrativeView *e = data;
printf("clipboard get\n");
if ( info == 0 ) {
- printf("sending SC frame\n");
+ char *t;
+ size_t start_offs, end_offs;
+ start_offs = narrative_pos_trail_to_offset(e->n, e->sel_start.para,
+ e->sel_start.pos, e->sel_start.trail);
+ end_offs = narrative_pos_trail_to_offset(e->n, e->sel_end.para,
+ e->sel_end.pos, e->sel_end.trail);
+ t = narrative_range_as_text(e->n, e->sel_start.para, start_offs,
+ e->sel_end.para, end_offs);
+ if ( t != NULL ) {
+ printf("got text '%s'\n", t);
+ gtk_selection_data_set_text(seldata, t, -1);
+ } else {
+ fprintf(stderr, "Couldn't format Storycode as text\n");
+ }
+
+ } else if ( info == 1 ) {
+ char *t = strdup(": Text");
+ printf("sending SC\n");
gtk_selection_data_set(seldata,
gtk_selection_data_get_target(seldata),
8, (const guchar *)t, strlen(t)+1);
+
} else {
GdkAtom target;
gchar *name;
@@ -431,35 +449,26 @@ static void clipboard_get(GtkClipboard *cb, GtkSelectionData *seldata,
static void clipboard_clear(GtkClipboard *cb, gpointer data)
{
- free(data);
+ /* Do absolutely nothing */
}
static void copy_selection(GtkNarrativeView *e)
{
-// char *t;
-// GtkClipboard *cb;
-// GdkAtom atom;
-// GtkTargetEntry targets[1];
-//
-// atom = gdk_atom_intern("CLIPBOARD", FALSE);
-// if ( atom == GDK_NONE ) return;
-//
-// cb = gtk_clipboard_get(atom);
-//
-// targets[0].target = "text/x-storycode";
-// targets[0].flags = 0;
-// targets[0].info = 0;
-//
-// printf("copying selection\n");
-//
-// bl = block_at_cursor(e->cursor_frame, e->cpos.para, 0);
-// if ( bl == NULL ) return;
-//
-// t = serialise_sc_block(bl);
-//
-// gtk_clipboard_set_with_data(cb, targets, 1,
-// clipboard_get, clipboard_clear, t);
+ GtkClipboard *cb;
+ GtkTargetEntry targets[2];
+
+ cb = gtk_clipboard_get_default(gtk_widget_get_display(GTK_WIDGET(e)));
+
+ targets[0].target = "text/plain";
+ targets[0].flags = 0;
+ targets[0].info = 0;
+
+ targets[1].target = "text/x-storycode";
+ targets[1].flags = 0;
+ targets[1].info = 1;
+
+ gtk_clipboard_set_with_owner(cb, targets, 2, clipboard_get, clipboard_clear, G_OBJECT(e));
}
diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c
index 67fe9e9..cd4b10e 100644
--- a/libstorycode/narrative.c
+++ b/libstorycode/narrative.c
@@ -589,6 +589,45 @@ Slide *narrative_get_slide_by_number(Narrative *n, int pos)
}
+/* Return the text between item p1/offset o1 and p2/o2 */
+char *narrative_range_as_text(Narrative *n, int p1, size_t o1, int p2, size_t o2)
+{
+ int i;
+ char *t;
+ size_t len = 0;
+ size_t size = 256;
+
+ t = malloc(size);
+ if ( t == NULL ) return NULL;
+
+ t[0] = '\0';
+
+ for ( i=p1; i<=p2; i++ ) {
+ int r;
+ if ( !narrative_item_is_text(n, i) ) continue;
+ for ( r=0; r<n->items[i].n_runs; r++ ) {
+ size_t this_len = strlen(n->items[i].runs[r].text);
+ if ( this_len + len + 2 > size ) {
+ char *nt;
+ size += 256 + this_len;
+ nt = realloc(t, size);
+ if ( nt == NULL ) {
+ fprintf(stderr, "Failed to allocate\n");
+ return NULL;
+ }
+ t = nt;
+ }
+ memcpy(&t[len], n->items[i].runs[r].text, this_len+1);
+ len += this_len;
+ }
+ t[len++] = '\n';
+ t[len] = '\0';
+ }
+
+ return t;
+}
+
+
static void debug_runs(struct narrative_item *item)
{
int j;
@@ -597,6 +636,7 @@ static void debug_runs(struct narrative_item *item)
}
}
+
void narrative_debug(Narrative *n)
{
int i;
diff --git a/libstorycode/narrative.h b/libstorycode/narrative.h
index c5cb6f3..701d409 100644
--- a/libstorycode/narrative.h
+++ b/libstorycode/narrative.h
@@ -73,6 +73,8 @@ extern Slide *narrative_get_slide_by_number(Narrative *n, int pos);
extern int narrative_get_slide_number_for_para(Narrative *n, int para);
extern int narrative_get_slide_number_for_slide(Narrative *n, Slide *s);
+extern char *narrative_range_as_text(Narrative *n, int p1, size_t o1, int p2, size_t o2);
+
extern void narrative_debug(Narrative *n);
#endif /* NARRATIVE_H */