From e9c36334f53bb6aea931d0ec00e8967ef822c92c Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 7 May 2019 18:23:59 +0200 Subject: Beautify cursor and selection highlight for slides --- libstorycode/gtk/gtknarrativeview.c | 74 +++++++++++++++++++++-------------- libstorycode/narrative.c | 7 ++++ libstorycode/narrative.h | 2 + libstorycode/narrative_priv.h | 1 + libstorycode/narrative_render_cairo.c | 14 ++++++- 5 files changed, 67 insertions(+), 31 deletions(-) diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c index 600e01f..a174c9d 100644 --- a/libstorycode/gtk/gtknarrativeview.c +++ b/libstorycode/gtk/gtknarrativeview.c @@ -554,50 +554,66 @@ static void get_cursor_pos(Narrative *n, struct edit_pos cpos, } -static void draw_caret(cairo_t *cr, Narrative *n, struct edit_pos cpos, - int hgh, double ww) +static void draw_caret(cairo_t *cr, Narrative *n, struct edit_pos cpos, double ww) { - double cx, clow, chigh, h; - const double t = 1.8; + assert(n != NULL); - if ( hgh ) { - draw_para_highlight(cr, n, cpos.para, ww); - return; - } + if ( narrative_item_is_text(n, cpos.para) ) { - assert(n != NULL); + /* Normal text-style cursor */ - if ( n->items[cpos.para].type == NARRATIVE_ITEM_SLIDE ) { - draw_para_highlight(cr, n, cpos.para, ww); - return; - } + double cx, clow, chigh, h; + const double t = 1.8; - get_cursor_pos(n, cpos, &cx, &clow, &h); + get_cursor_pos(n, cpos, &cx, &clow, &h); - chigh = clow+h; + chigh = clow+h; - cairo_move_to(cr, cx, clow); - cairo_line_to(cr, cx, chigh); + cairo_move_to(cr, cx, clow); + cairo_line_to(cr, cx, chigh); - cairo_move_to(cr, cx-t, clow-t); - cairo_line_to(cr, cx, clow); - cairo_move_to(cr, cx+t, clow-t); - cairo_line_to(cr, cx, clow); + cairo_move_to(cr, cx-t, clow-t); + cairo_line_to(cr, cx, clow); + cairo_move_to(cr, cx+t, clow-t); + cairo_line_to(cr, cx, clow); - cairo_move_to(cr, cx-t, chigh+t); - cairo_line_to(cr, cx, chigh); - cairo_move_to(cr, cx+t, chigh+t); - cairo_line_to(cr, cx, chigh); + cairo_move_to(cr, cx-t, chigh+t); + cairo_line_to(cr, cx, chigh); + cairo_move_to(cr, cx+t, chigh+t); + cairo_line_to(cr, cx, chigh); - cairo_set_source_rgb(cr, 0.86, 0.0, 0.0); - cairo_set_line_width(cr, 1.0); - cairo_stroke(cr); + cairo_set_source_rgb(cr, 0.86, 0.0, 0.0); + cairo_set_line_width(cr, 1.0); + cairo_stroke(cr); + + } else { + + /* Block highlight cursor */ + + double cx, cy, cw, ch; + + cx = n->space_l - 5.5; + cy = n->space_t + para_top(n, cpos.para) - 5.5; + cw = n->items[cpos.para].slide_w + 11.0; + ch = n->items[cpos.para].slide_h + 11.0; + + cairo_new_path(cr); + cairo_rectangle(cr, cx, cy, cw, ch); + cairo_set_source_rgb(cr, 0.86, 0.0, 0.0); + cairo_set_line_width(cr, 1.0); + cairo_stroke(cr); + + } } static void draw_overlay(cairo_t *cr, GtkNarrativeView *e) { - draw_caret(cr, e->n, e->cpos, e->para_highlight, e->w); + if ( e->para_highlight ) { + draw_para_highlight(cr, e->n, e->cpos.para, e->w); + } else { + draw_caret(cr, e->n, e->cpos, e->w); + } } diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 1e7753d..cefbd0d 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -162,6 +162,13 @@ int narrative_get_unsaved(Narrative *n) } +int narrative_item_is_text(Narrative *n, int item) +{ + if ( n->items[item].type == NARRATIVE_ITEM_SLIDE ) return 0; + return 1; +} + + void narrative_add_stylesheet(Narrative *n, Stylesheet *ss) { assert(n->stylesheet == NULL); diff --git a/libstorycode/narrative.h b/libstorycode/narrative.h index 22918e1..7f9b491 100644 --- a/libstorycode/narrative.h +++ b/libstorycode/narrative.h @@ -49,6 +49,8 @@ extern int narrative_save(Narrative *n, GFile *file); extern void narrative_set_unsaved(Narrative *n); extern int narrative_get_unsaved(Narrative *n); +extern int narrative_item_is_text(Narrative *n, int item); + extern void narrative_add_prestitle(Narrative *n, char *text); extern void narrative_add_bp(Narrative *n, char *text); extern void narrative_add_text(Narrative *n, char *text); diff --git a/libstorycode/narrative_priv.h b/libstorycode/narrative_priv.h index 065b416..7ba7028 100644 --- a/libstorycode/narrative_priv.h +++ b/libstorycode/narrative_priv.h @@ -68,6 +68,7 @@ struct narrative_item #else void *slide_thumbnail; #endif + int selected; /* Whether or not this item should be given a "selected" highlight */ }; diff --git a/libstorycode/narrative_render_cairo.c b/libstorycode/narrative_render_cairo.c index 6556c15..db1590f 100644 --- a/libstorycode/narrative_render_cairo.c +++ b/libstorycode/narrative_render_cairo.c @@ -181,7 +181,7 @@ static cairo_surface_t *render_thumbnail(Slide *s, Stylesheet *ss, ImageStore *i } -static void wrap_slide(struct narrative_item *item, Stylesheet *ss, ImageStore *is) +static void wrap_slide(struct narrative_item *item, Stylesheet *ss, ImageStore *is, int sel_block) { double w, h; @@ -201,6 +201,7 @@ static void wrap_slide(struct narrative_item *item, Stylesheet *ss, ImageStore * } item->slide_thumbnail = render_thumbnail(item->slide, ss, is, item->slide_w, item->slide_h); + item->selected = sel_block; } @@ -285,6 +286,8 @@ int narrative_wrap_range(Narrative *n, Stylesheet *stylesheet, PangoLanguage *la for ( i=min; i<=max; i++ ) { size_t srt, end; + int sel_block = 0; + if ( i >= sel_start.para && i <= sel_end.para ) { if ( i == sel_start.para ) { srt = sel_s; @@ -299,6 +302,7 @@ int narrative_wrap_range(Narrative *n, Stylesheet *stylesheet, PangoLanguage *la if ( i > sel_start.para && i < sel_end.para ) { end = G_MAXUINT; } + sel_block = 1; } else { srt = 0; end = 0; @@ -322,7 +326,7 @@ int narrative_wrap_range(Narrative *n, Stylesheet *stylesheet, PangoLanguage *la break; case NARRATIVE_ITEM_SLIDE : - wrap_slide(&n->items[i], stylesheet, is); + wrap_slide(&n->items[i], stylesheet, is, sel_block); break; default : @@ -364,6 +368,12 @@ static void draw_slide(struct narrative_item *item, cairo_t *cr) x = rint(x); y = rint(y); cairo_device_to_user(cr, &x, &y); + if ( item->selected ) { + cairo_rectangle(cr, x-5.0, y-5.0, item->slide_w+10.0, item->slide_h+10.0); + cairo_set_source_rgb(cr, 0.655, 0.899, 1.0); + cairo_fill(cr); + } + cairo_rectangle(cr, x, y, item->slide_w, item->slide_h); cairo_set_source_surface(cr, item->slide_thumbnail, 0.0, 0.0); cairo_fill(cr); -- cgit v1.2.3