diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2011-07-26 13:53:42 +0000 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2011-07-26 13:53:42 +0000 |
commit | 16b92d1613e245919743be5f168abe77712ec84b (patch) | |
tree | 5f2516ecf29989bd4fffeb56ef6b4a02410ec8c1 /desktop | |
parent | d97e99b02b40114c584661541805704006ffefee (diff) | |
download | netsurf-16b92d1613e245919743be5f168abe77712ec84b.tar.gz netsurf-16b92d1613e245919743be5f168abe77712ec84b.tar.bz2 |
Selection is now subordinate to html and text content types, and disassociated from browser windows. Note: search currently uses hlcache_handle_get_content() to go from bw to h to get at c for search highlighting via selection.
svn path=/trunk/netsurf/; revision=12626
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/search.c | 71 | ||||
-rw-r--r-- | desktop/selection.c | 87 | ||||
-rw-r--r-- | desktop/selection.h | 10 |
3 files changed, 83 insertions, 85 deletions
diff --git a/desktop/search.c b/desktop/search.c index 29455d8d5..014014e71 100644 --- a/desktop/search.c +++ b/desktop/search.c @@ -81,7 +81,7 @@ static bool find_occurrences_html(const char *pattern, int p_len, struct box *cur, bool case_sens, struct search_context *context); static bool find_occurrences_text(const char *pattern, int p_len, - hlcache_handle *c, bool case_sens, + struct content *c, bool case_sens, struct search_context *context); static struct list_entry *add_entry(unsigned start_idx, unsigned end_idx, struct search_context *context); @@ -236,7 +236,7 @@ void search_text(const char *string, int string_len, struct search_context *context, search_flags_t flags) { struct rect bounds; - hlcache_handle *c; + hlcache_handle *h; struct box *box = NULL; bool case_sensitive, forwards, showall; @@ -247,15 +247,15 @@ void search_text(const char *string, int string_len, if (context->bw == NULL) return; - c = context->bw->current_content; + h = context->bw->current_content; /* only handle html contents */ - if ((!c) || (content_get_type(c) != CONTENT_HTML && - content_get_type(c) != CONTENT_TEXTPLAIN)) + if ((!h) || (content_get_type(h) != CONTENT_HTML && + content_get_type(h) != CONTENT_TEXTPLAIN)) return; - if (content_get_type(c) == CONTENT_HTML) { - box = html_get_box_tree(c); + if (content_get_type(h) == CONTENT_HTML) { + box = html_get_box_tree(h); if (!box) return; @@ -284,13 +284,14 @@ void search_text(const char *string, int string_len, (context->callbacks->hourglass != NULL)) context->callbacks->hourglass(true, context->p); - if (content_get_type(c) == CONTENT_HTML) + if (content_get_type(h) == CONTENT_HTML) res = find_occurrences_html(string, string_len, box, case_sensitive, context); else { - assert(content_get_type(c) == CONTENT_TEXTPLAIN); + assert(content_get_type(h) == CONTENT_TEXTPLAIN); res = find_occurrences_text(string, string_len, - c, case_sensitive, context); + hlcache_handle_get_content(h), + case_sensitive, context); } if (!res) { @@ -343,7 +344,7 @@ void search_text(const char *string, int string_len, if (context->current == NULL) return; - switch (content_get_type(c)) { + switch (content_get_type(h)) { case CONTENT_HTML: /* get box position and jump to it */ box_coords(context->current->start_box, @@ -357,8 +358,9 @@ void search_text(const char *string, int string_len, break; default: - assert(content_get_type(c) == CONTENT_TEXTPLAIN); - textplain_coords_from_range(c, + assert(content_get_type(h) == CONTENT_TEXTPLAIN); + textplain_coords_from_range( + hlcache_handle_get_content(h), context->current->start_idx, context->current->end_idx, &bounds); break; @@ -551,7 +553,7 @@ bool find_occurrences_html(const char *pattern, int p_len, struct box *cur, */ bool find_occurrences_text(const char *pattern, int p_len, - hlcache_handle *c, bool case_sens, + struct content *c, bool case_sens, struct search_context *context) { int nlines = textplain_line_count(c); @@ -640,25 +642,30 @@ void search_show_all(bool all, struct search_context *context) } } if (add && !a->sel) { - a->sel = selection_create(); - selection_set_browser_window(a->sel, context->bw); - if (a->sel) { - hlcache_handle *c = context->bw-> - current_content; - switch (content_get_type(c)) { - case CONTENT_HTML: - selection_init(a->sel, - html_get_box_tree(c)); - break; - default: - assert(content_get_type(c) == - CONTENT_TEXTPLAIN); - selection_init(a->sel, NULL); - break; - } - selection_set_start(a->sel, a->start_idx); - selection_set_end(a->sel, a->end_idx); + hlcache_handle *h = context->bw->current_content; + struct content *c = hlcache_handle_get_content(h); + + switch (content_get_type(h)) { + case CONTENT_HTML: + a->sel = selection_create(c, true); + if (!a->sel) + continue; + + selection_init(a->sel, html_get_box_tree(h)); + break; + default: + assert(content_get_type(h) == + CONTENT_TEXTPLAIN); + a->sel = selection_create(c, false); + if (!a->sel) + continue; + + selection_init(a->sel, NULL); + break; } + + selection_set_start(a->sel, a->start_idx); + selection_set_end(a->sel, a->end_idx); } } } diff --git a/desktop/selection.c b/desktop/selection.c index 7c775f4c2..2c61c511e 100644 --- a/desktop/selection.c +++ b/desktop/selection.c @@ -27,7 +27,6 @@ #include <stdbool.h> #include <string.h> -#include "content/hlcache.h" #include "desktop/gui.h" #include "desktop/mouse.h" #include "desktop/plotters.h" @@ -36,6 +35,7 @@ #include "render/box.h" #include "render/font.h" #include "render/form.h" +#include "render/html_internal.h" #include "render/textplain.h" #include "utils/log.h" #include "utils/utf8.h" @@ -87,32 +87,51 @@ static bool traverse_tree(struct box *box, unsigned start_idx, unsigned end_idx, bool do_marker); static struct box *get_box(struct box *b, unsigned offset, size_t *pidx); + +/** + * Get the browser window containing the content a selection object belongs to. + * + * \param s selection object + * \return the browser window + */ +static struct browser_window * selection_get_browser_window(struct selection *s) +{ + if (s->is_html) + return html_get_browser_window(s->c); + else + return textplain_get_browser_window(s->c); +} + + /** * Creates a new selection object associated with a browser window. * * \return new selection context */ -struct selection *selection_create(void) +struct selection *selection_create(struct content *c, bool is_html) { struct selection *s = calloc(1, sizeof(struct selection)); if (s) { - selection_prepare(s); + selection_prepare(s, c, is_html); } + return s; } /** * Prepare a newly created selection object for use. * - * \param s selection object - * \param bw browser window + * \param s selection object + * \param c content + * \param is_html true if content is html false if content is textplain */ -void selection_prepare(struct selection *s) +void selection_prepare(struct selection *s, struct content *c, bool is_html) { if (s) { - s->bw = NULL; + s->c = c; + s->is_html = is_html; s->root = NULL; s->drag_state = DRAG_NONE; s->max_idx = 0; @@ -120,21 +139,6 @@ void selection_prepare(struct selection *s) } } -/** - * Set the browser window that contains the selection within a selection - * object. - * - * \param bw browser window - */ - -void selection_set_browser_window(struct selection *s, - struct browser_window *bw) -{ - assert(s); - - s->bw = bw; -} - /** * Destroys a selection object, without updating the @@ -188,11 +192,9 @@ void selection_reinit(struct selection *s, struct box *root) s->root = root; if (root) { s->max_idx = selection_label_subtree(root, root_idx); - } - else { - hlcache_handle *c = s->bw->current_content; - if (c && content_get_type(c) == CONTENT_TEXTPLAIN) - s->max_idx = textplain_size(c); + } else { + if (s->is_html == false) + s->max_idx = textplain_size(s->c); else s->max_idx = 0; } @@ -291,11 +293,13 @@ bool selection_click(struct selection *s, browser_mouse_state mouse, browser_mouse_state modkeys = (mouse & (BROWSER_MOUSE_MOD_1 | BROWSER_MOUSE_MOD_2)); int pos = -1; /* 0 = inside selection, 1 = after it */ - struct browser_window *top; + struct browser_window *top = selection_get_browser_window(s); - if (s->bw == NULL) + if (top == NULL) return false; /* not our problem */ + top = browser_window_get_root(top); + if (!SAME_SPACE(s, idx)) return false; /* not our problem */ @@ -308,8 +312,6 @@ bool selection_click(struct selection *s, browser_mouse_state mouse, } } - top = browser_window_get_root(s->bw); - if (!pos && ((mouse & BROWSER_MOUSE_DRAG_1) || (modkeys && (mouse & BROWSER_MOUSE_DRAG_2)))) { @@ -612,7 +614,6 @@ bool traverse_tree(struct box *box, unsigned start_idx, unsigned end_idx, bool selection_traverse(struct selection *s, seln_traverse_handler handler, void *handle) { - hlcache_handle *c; save_text_whitespace before = WHITESPACE_NONE; bool first = true; const char *text; @@ -629,10 +630,7 @@ bool selection_traverse(struct selection *s, seln_traverse_handler handler, } /* Text */ - c = s->bw->current_content; - if (!c) return true; - - text = textplain_get_raw_data(c, s->start_idx, s->end_idx, &length); + text = textplain_get_raw_data(s->c, s->start_idx, s->end_idx, &length); if (text && !handler(text, length, NULL, handle, NULL, 0)) return false; @@ -717,17 +715,15 @@ void selection_redraw(struct selection *s, unsigned start_idx, unsigned end_idx) return; } else { - hlcache_handle *c = s->bw->current_content; - if (c && content_get_type(c) == CONTENT_TEXTPLAIN && - end_idx > start_idx) { - textplain_coords_from_range(c, start_idx, + if (s->is_html == false && end_idx > start_idx) { + textplain_coords_from_range(s->c, start_idx, end_idx, &rdw.r); rdw.inited = true; } } if (rdw.inited) - browser_window_redraw_rect(s->bw, rdw.r.x0, rdw.r.y0, + content__request_redraw(s->c, rdw.r.x0, rdw.r.y0, rdw.r.x1 - rdw.r.x0, rdw.r.y1 - rdw.r.y0); } @@ -744,7 +740,7 @@ void selection_clear(struct selection *s, bool redraw) { int old_start, old_end; bool was_defined; - struct browser_window *top; + struct browser_window *top = selection_get_browser_window(s); assert(s); was_defined = selection_defined(s); @@ -755,10 +751,10 @@ void selection_clear(struct selection *s, bool redraw) s->start_idx = 0; s->end_idx = 0; - if (!s->bw) + if (!top) return; - top = browser_window_get_root(s->bw); + top = browser_window_get_root(top); gui_clear_selection(top->window); @@ -1015,14 +1011,11 @@ bool save_handler(const char *text, size_t length, struct box *box, bool selection_save_text(struct selection *s, const char *path) { - hlcache_handle *c = s->bw->current_content; struct save_text_state sv = { NULL, 0, 0 }; utf8_convert_ret ret; char *result; FILE *out; - assert(c); - if (!selection_traverse(s, save_handler, &sv)) { free(sv.block); return false; diff --git a/desktop/selection.h b/desktop/selection.h index 4b8bd8ec8..b4e4b31e9 100644 --- a/desktop/selection.h +++ b/desktop/selection.h @@ -40,7 +40,7 @@ typedef enum { struct selection { - struct browser_window *bw; + struct content *c; struct box *root; unsigned max_idx; /* total bytes in text representation */ @@ -49,6 +49,7 @@ struct selection unsigned end_idx; bool defined; + bool is_html; seln_drag_state drag_state; }; @@ -59,16 +60,13 @@ typedef bool (*seln_traverse_handler)(const char *text, size_t length, size_t whitespace_length); -struct selection *selection_create(void); -void selection_prepare(struct selection *s); +struct selection *selection_create(struct content *c, bool is_html); +void selection_prepare(struct selection *s, struct content *c, bool is_html); void selection_destroy(struct selection *s); void selection_init(struct selection *s, struct box *root); void selection_reinit(struct selection *s, struct box *root); -void selection_set_browser_window(struct selection *s, - struct browser_window *bw); - /* struct box *selection_root(struct selection *s); */ #define selection_root(s) ((s)->root) |