From 2f3895f0882e696598665cefcc0468de4cec0bff Mon Sep 17 00:00:00 2001 From: Adrian Lees Date: Sat, 16 Apr 2005 08:22:57 +0000 Subject: [project @ 2005-04-16 08:22:57 by adrianl] first cut at selecting inter-block spaces svn path=/import/netsurf/; revision=1652 --- desktop/selection.c | 42 ++++++++++++++++++++++++------------------ render/html_redraw.c | 23 +++++++++++++++++++---- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/desktop/selection.c b/desktop/selection.c index 291831723..afec4180a 100644 --- a/desktop/selection.c +++ b/desktop/selection.c @@ -656,26 +656,32 @@ void selection_set_end(struct selection *s, struct box *box, int idx) bool selection_highlighted(struct selection *s, struct box *box, unsigned *start_idx, unsigned *end_idx) { - if (selection_defined(s) && box->length > 0 && - box->byte_offset < s->end_idx && - box->byte_offset + box->length > s->start_idx) { - unsigned offset = 0; - unsigned len; + assert(selection_defined(s)); /* caller should have checked for efficiency */ + assert(s && box); - if (box->byte_offset < s->start_idx) - offset = s->start_idx - box->byte_offset; + if (box->length > 0) { + unsigned box_len = box->length + (box->space ? 1 : 0); - len = box->length - offset; - - if (box->byte_offset + box->length > s->end_idx) - len = s->end_idx - (box->byte_offset + offset); - - assert(offset < box->length); - assert(offset + len <= box->length); - - *start_idx = offset; - *end_idx = offset + len; - return true; + if (box->byte_offset < s->end_idx && + box->byte_offset + box_len > s->start_idx) { + unsigned offset = 0; + unsigned len; + + if (box->byte_offset < s->start_idx) + offset = s->start_idx - box->byte_offset; + + len = box_len - offset; + + if (box->byte_offset + box_len > s->end_idx) + len = s->end_idx - (box->byte_offset + offset); + + assert(offset <= box_len); + assert(offset + len <= box->length + 1); + + *start_idx = offset; + *end_idx = offset + len; + return true; + } } return false; } diff --git a/render/html_redraw.c b/render/html_redraw.c index 491631194..c2eef9d3e 100644 --- a/render/html_redraw.c +++ b/render/html_redraw.c @@ -359,16 +359,31 @@ bool html_redraw_box(struct box *box, selection_highlighted(current_redraw_browser->sel, box, &start_idx, &end_idx)) { + unsigned endtxt_idx = end_idx; int startx, endx; + if (end_idx > box->length) { + /* adjust for trailing space, not present in box->text */ + assert(end_idx == box->length + 1); + endtxt_idx = box->length; + } + if (!nsfont_width(box->style, box->text, start_idx, &startx)) startx = 0; if (!nsfont_width(box->style, box->text + start_idx, - end_idx - start_idx, &endx)) + endtxt_idx - start_idx, &endx)) endx = 0; endx += startx; + /* is there a trailing space that should be highlighted as well? */ + if (end_idx > box->length) { + int spc_width; + /* \todo is there a more elegant/efficient solution? */ + if (nsfont_width(box->style, " ", 1, &spc_width)) + endx += spc_width; + } + if (scale != 1.0) { startx *= scale; endx *= scale; @@ -389,15 +404,15 @@ bool html_redraw_box(struct box *box, return false; if (!plot.text(x + startx, y + (int) (box->height * 0.75 * scale), - box->style, box->text + start_idx, end_idx - start_idx, + box->style, box->text + start_idx, endtxt_idx - start_idx, current_background_color ^ 0xffffff, current_background_color)) return false; - if (end_idx < box->length) { + if (endtxt_idx < box->length) { if (!plot.text(x + endx, y + (int) (box->height * 0.75 * scale), - box->style, box->text + end_idx, box->length - end_idx, + box->style, box->text + endtxt_idx, box->length - endtxt_idx, current_background_color, /*print_text_black ? 0 :*/ box->style->color)) return false; -- cgit v1.2.3