aboutsummaryrefslogtreecommitdiff
path: root/src/tool_text.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2011-10-04 09:51:47 +0200
committerThomas White <taw@bitwiz.org.uk>2011-10-04 09:51:47 +0200
commitc30470fbd44b78b17740969055a932abc7402c92 (patch)
treef1d1518676ea69417a278b2a7fcdd2a35a1323a1 /src/tool_text.c
parent02295c300cac996e29bd9193223d7da3e8c67dd3 (diff)
Cursor stuff (broken)
Diffstat (limited to 'src/tool_text.c')
-rw-r--r--src/tool_text.c84
1 files changed, 30 insertions, 54 deletions
diff --git a/src/tool_text.c b/src/tool_text.c
index 5d5f754..cb689db 100644
--- a/src/tool_text.c
+++ b/src/tool_text.c
@@ -245,61 +245,49 @@ void insert_text(struct object *op, char *t)
}
-static int find_prev_index(const char *t, int p)
+void move_cursor_left(struct object *op)
{
- int i, nback;
+ struct text_object *o = (struct text_object *)op;
+ int new_idx, new_trail;
- if ( p == 0 ) return 0;
+ pango_layout_move_cursor_visually(o->layout, TRUE, o->insertion_point,
+ 0, +1, &new_idx, &new_trail);
- if ( !(t[p-1] & 0x80) ) {
- nback = 1;
- } else {
- nback = 0;
- for ( i=1; i<=6; i++ ) {
- if ( p-i == 0 ) return 0;
- if ( !(t[p-i] & 0xC0) ) nback++;
- }
+ if ( (new_idx > 0) && (new_idx < G_MAXINT) ) {
+ o->insertion_point = new_idx;
}
-
- return p - nback;
}
-static int find_next_index(const char *t, int p)
+void move_cursor_right(struct object *op)
{
- int i, nfor;
+ struct text_object *o = (struct text_object *)op;
+ int new_idx, new_trail;
- if ( t[p] == '\0' ) return p;
+ pango_layout_move_cursor_visually(o->layout, TRUE, o->insertion_point,
+ 0, -1, &new_idx, &new_trail);
- if ( !(t[p+1] & 0x80) ) {
- nfor = 1;
- } else {
- nfor = 0;
- for ( i=1; i<=6; i++ ) {
- if ( t[p+i] == '\0' ) return p+i;
- if ( !(t[p+i] & 0xC0) ) nfor++;
- }
+ if ( (new_idx > 0) && (new_idx < G_MAXINT) ) {
+ o->insertion_point = new_idx;
}
-
- return p + nfor;
}
void handle_text_backspace(struct object *op)
{
- int prev_index;
+ int old_idx, new_idx;
struct text_object *o = (struct text_object *)op;
assert(o->base.type == TEXT);
if ( o->insertion_point == 0 ) return; /* Nothing to delete */
- prev_index = find_prev_index(o->text, o->insertion_point);
-
- memmove(o->text+prev_index, o->text+o->insertion_point,
- o->text_len-o->insertion_point);
+ old_idx = o->insertion_point;
+ move_cursor_left(op);
+ new_idx = o->insertion_point;
- o->insertion_point = prev_index;
+ memmove(o->text+new_idx, o->text+old_idx,
+ o->text_len-new_idx);
if ( strlen(o->text) == 0 ) o->base.empty = 1;
@@ -308,20 +296,6 @@ void handle_text_backspace(struct object *op)
}
-void move_cursor_left(struct object *op)
-{
- struct text_object *o = (struct text_object *)op;
- o->insertion_point = find_prev_index(o->text, o->insertion_point);
-}
-
-
-void move_cursor_right(struct object *op)
-{
- struct text_object *o = (struct text_object *)op;
- o->insertion_point = find_next_index(o->text, o->insertion_point);
-}
-
-
static void render_text_object(cairo_t *cr, struct object *op)
{
struct text_object *o = (struct text_object *)op;
@@ -337,21 +311,23 @@ static void render_text_object(cairo_t *cr, struct object *op)
static void draw_caret(cairo_t *cr, struct object *op)
{
- int line, xpos;
- double xposd, cx;
+ double xposd, yposd, cx;
double clow, chigh;
+ PangoRectangle pos;
const double t = 1.8;
struct text_object *o = (struct text_object *)op;
assert(o->base.type == TEXT);
- pango_layout_index_to_line_x(o->layout, o->insertion_point,
- 0, &line, &xpos);
+ pango_layout_get_cursor_pos(o->layout, o->insertion_point, &pos, NULL);
+
+ xposd = pos.x/PANGO_SCALE;
+ cx = o->base.x + xposd;
+ yposd = pos.y/PANGO_SCALE;
+ clow = o->base.y + yposd;
+ chigh = clow + (pos.height/PANGO_SCALE);
- xposd = xpos/PANGO_SCALE;
- cx = o->base.x+xposd;
- clow = o->base.y;
- chigh = o->base.y+o->base.bb_height;
+ printf("Cursor %f, %f-%f\n", cx, clow, chigh);
cairo_move_to(cr, cx, clow);
cairo_line_to(cr, cx, chigh);