aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2016-04-19 22:25:05 +0200
committerThomas White <taw@bitwiz.org.uk>2016-04-19 22:53:06 +0200
commit890e5ac7b89f5cccf120d966dc488f9ddd516a3d (patch)
tree4e5ab43c4fee1cb6f41cb842402d99da469dd52f /src
parent43cefb51f137481e873e3401bbad88b140c4b521 (diff)
Clarify that cursor_pos is BYTE offset (not character)
Diffstat (limited to 'src')
-rw-r--r--src/frame.c91
-rw-r--r--src/frame.h6
-rw-r--r--src/sc_editor.c4
-rw-r--r--src/sc_editor.h2
4 files changed, 81 insertions, 22 deletions
diff --git a/src/frame.c b/src/frame.c
index a0124a7..3699b74 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -542,7 +542,7 @@ size_t end_offset_of_para(struct frame *fr, int pn)
/* Local x,y in paragraph -> text offset */
-static int text_para_pos(Paragraph *para, double x, double y, int *ptrail)
+static size_t text_para_pos(Paragraph *para, double x, double y, int *ptrail)
{
int idx;
pango_layout_xy_to_index(para->layout, pango_units_from_double(x),
@@ -552,7 +552,7 @@ static int text_para_pos(Paragraph *para, double x, double y, int *ptrail)
int find_cursor(struct frame *fr, double x, double y,
- int *ppara, int *ppos, int *ptrail)
+ int *ppara, size_t *ppos, int *ptrail)
{
double pos = fr->pad_t;
int i;
@@ -657,16 +657,19 @@ int get_cursor_pos(struct frame *fr, int cursor_para, int cursor_pos,
}
-void cursor_moveh(struct frame *fr, int *cpara, int *cpos, int *ctrail,
+void cursor_moveh(struct frame *fr, int *cpara, size_t *cpos, int *ctrail,
signed int dir)
{
Paragraph *para = fr->paras[*cpara];
+ int np = *cpos;
- if ( (*cpos+*ctrail == end_offset_of_para(fr, *cpara)) && (dir > 0) ) {
- if ( *cpara < fr->n_paras-1 ) {
- (*cpara)++;
- *cpos = 0;
- *ctrail = 0;
+ pango_layout_move_cursor_visually(para->layout, 1, *cpos, *ctrail,
+ dir, &np, ctrail);
+ if ( np == -1 ) {
+ if ( *cpara > 0 ) {
+ (*cpara)--;
+ *cpos = end_offset_of_para(fr, *cpara) - 1;
+ *ctrail = 1;
return;
} else {
/* Can't move any further */
@@ -674,23 +677,23 @@ void cursor_moveh(struct frame *fr, int *cpara, int *cpos, int *ctrail,
}
}
- if ( (*cpos+*ctrail == 0) && (dir < 0) ) {
- if ( *cpara > 0 ) {
- (*cpara)--;
- *cpos = end_offset_of_para(fr, *cpara) - 1;
- *ctrail = 1;
+ if ( np == G_MAXINT ) {
+ if ( *cpara < fr->n_paras-1 ) {
+ (*cpara)++;
+ *cpos = 0;
+ *ctrail = 0;
return;
} else {
/* Can't move any further */
return;
}
}
- pango_layout_move_cursor_visually(para->layout, 1, *cpos, *ctrail,
- dir, cpos, ctrail);
+
+ *cpos = np;
}
-void cursor_movev(struct frame *fr, int *cpara, int *cpos, int *ctrail,
+void cursor_movev(struct frame *fr, int *cpara, size_t *cpos, int *ctrail,
signed int dir)
{
}
@@ -793,3 +796,59 @@ void delete_text_in_paragraph(Paragraph *para, size_t offs1, size_t offs2)
printf("Multi-run delete!\n");
}
}
+
+
+static void split_text_paragraph(struct frame *fr, int pn, int pos,
+ PangoContext *pc)
+{
+ Paragraph *pnew;
+ int i, j;
+ int run;
+ Paragraph *para = fr->paras[pn];
+
+ pnew = insert_paragraph(fr, pn);
+ if ( pnew == NULL ) {
+ fprintf(stderr, "Failed to insert paragraph\n");
+ return;
+ }
+
+ /* Determine which run the cursor is in */
+ run = which_run(para, pos);
+
+ pnew->type = PARA_TYPE_TEXT;
+ pnew->open = para->open;
+ pnew->n_runs = para->n_runs - run;
+ pnew->runs = malloc(pnew->n_runs * sizeof(struct text_run));
+ if ( pnew->runs == NULL ) {
+ fprintf(stderr, "Failed to allocate runs.\n");
+ return; /* Badness is coming */
+ }
+
+ /* If the position is right at the start of a run, the whole run
+ * gets moved to the next paragraph */
+ if ( para->runs[run].para_offs_bytes ) {
+ }
+
+ j = 0;
+ for ( i=run; i<para->n_runs; i++ ) {
+ pnew->runs[j++] = para->runs[i];
+ }
+
+ para->open = 0;
+ para->n_runs = run+1;
+
+ wrap_paragraph(para, pc, fr->w);
+ wrap_paragraph(pnew, pc, fr->w);
+}
+
+
+void split_paragraph(struct frame *fr, int pn, int pos, PangoContext *pc)
+{
+ Paragraph *para = fr->paras[pn];
+
+ if ( para->type == PARA_TYPE_TEXT ) {
+ split_text_paragraph(fr, pn, pos, pc);
+ } else {
+ /* Other types can't be split */
+ }
+}
diff --git a/src/frame.h b/src/frame.h
index a6b316f..d6d171c 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -128,7 +128,7 @@ extern void wrap_paragraph(Paragraph *para, PangoContext *pc, double w);
extern size_t end_offset_of_para(struct frame *fr, int pn);
extern int find_cursor(struct frame *fr, double x, double y,
- int *ppara, int *ppos, int *ptrail);
+ int *ppara, size_t *ppos, int *ptrail);
extern int get_para_highlight(struct frame *fr, int cursor_para,
double *cx, double *cy, double *cw, double *ch);
@@ -136,10 +136,10 @@ extern int get_para_highlight(struct frame *fr, int cursor_para,
extern int get_cursor_pos(struct frame *fr, int cursor_para, int cursor_pos,
double *cx, double *cy, double *ch);
-extern void cursor_moveh(struct frame *fr, int *cpara, int *cpos, int *ctrail,
+extern void cursor_moveh(struct frame *fr, int *cpara, size_t *cpos, int *ctrail,
signed int dir);
-extern void cursor_movev(struct frame *fr, int *cpara, int *cpos, int *ctrail,
+extern void cursor_movev(struct frame *fr, int *cpara, size_t *cpos, int *ctrail,
signed int dir);
extern void check_callback_click(struct frame *fr, int para);
diff --git a/src/sc_editor.c b/src/sc_editor.c
index 0ad5e37..5549c54 100644
--- a/src/sc_editor.c
+++ b/src/sc_editor.c
@@ -613,12 +613,12 @@ static void insert_text(char *t, SCEditor *e)
static void do_backspace(struct frame *fr, SCEditor *e)
{
- int old_pos = e->cursor_pos;
+ size_t old_pos = e->cursor_pos;
int old_para = e->cursor_para;
int old_trail = e->cursor_trail;
int new_para = old_para;
- int new_pos = old_pos;
+ size_t new_pos = old_pos;
int new_trail = old_trail;
Paragraph *para = e->cursor_frame->paras[old_para];
diff --git a/src/sc_editor.h b/src/sc_editor.h
index eaa7f41..594f344 100644
--- a/src/sc_editor.h
+++ b/src/sc_editor.h
@@ -118,7 +118,7 @@ struct _sceditor
/* Location of the cursor */
struct frame *cursor_frame;
int cursor_para; /* paragraph index */
- int cursor_pos; /* character offset into paragraph */
+ size_t cursor_pos; /* byte offset into paragraph */
int cursor_trail;
/* Border surrounding actual slide within drawingarea */