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
This commit is contained in:
Michael Drake 2011-07-26 13:53:42 +00:00
parent d97e99b02b
commit 16b92d1613
7 changed files with 167 additions and 151 deletions

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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)

View File

@ -258,7 +258,7 @@ nserror html_create_html_data(html_content *c, const http_parameter *params)
c->font_func = &nsfont;
c->scrollbar = NULL;
selection_prepare(&c->sel);
selection_prepare(&c->sel, (struct content *)c, true);
nerror = http_parameter_list_find_item(params, html_charset, &charset);
if (nerror == NSERROR_OK) {
@ -2040,7 +2040,6 @@ void html_open(struct content *c, struct browser_window *bw,
/* text selection */
selection_init(&html->sel, html->layout);
selection_set_browser_window(&html->sel, bw);
for (object = html->object_list; object != NULL; object = next) {
next = object->next;
@ -2068,8 +2067,6 @@ void html_close(struct content *c)
html_content *html = (html_content *) c;
struct content_html_object *object, *next;
selection_set_browser_window(&html->sel, NULL);
html->bw = NULL;
for (object = html->object_list; object != NULL; object = next) {
@ -2367,3 +2364,19 @@ content_type html_content_type(lwc_string *mime_type)
return CONTENT_HTML;
}
/**
* Get the browser window containing an HTML content
*
* \param c HTML content
* \return the browser window
*/
struct browser_window *html_get_browser_window(struct content *c)
{
html_content *html = (html_content *) c;
assert(c != NULL);
assert(c->handler == &html_content_handler);
return html->bw;
}

View File

@ -111,6 +111,8 @@ void html_set_status(html_content *c, const char *extra);
void html__redraw_a_box(struct content *c, struct box *box);
struct browser_window *html_get_browser_window(struct content *c);
/* in render/html_redraw.c */
bool html_redraw(struct content *c, struct content_redraw_data *data,
const struct rect *clip, const struct redraw_context *ctx);

View File

@ -292,7 +292,7 @@ nserror textplain_create_internal(textplain_content *c, lwc_string *encoding)
c->formatted_width = 0;
c->bw = NULL;
selection_prepare(&c->sel);
selection_prepare(&c->sel, (struct content *)c, false);
return NSERROR_OK;
@ -623,7 +623,6 @@ void textplain_mouse_track(struct content *c, struct browser_window *bw,
browser_mouse_state mouse, int x, int y)
{
textplain_content *text = (textplain_content *) c;
hlcache_handle *h = bw->current_content;
if (bw->drag_type == DRAGGING_SELECTION && !mouse) {
int dir = -1;
@ -632,7 +631,7 @@ void textplain_mouse_track(struct content *c, struct browser_window *bw,
if (selection_dragging_start(&text->sel))
dir = 1;
idx = textplain_offset_from_coords(h, x, y, dir);
idx = textplain_offset_from_coords(c, x, y, dir);
selection_track(&text->sel, mouse, idx);
browser_window_set_drag_type(bw, DRAGGING_NONE);
@ -641,13 +640,12 @@ void textplain_mouse_track(struct content *c, struct browser_window *bw,
switch (bw->drag_type) {
case DRAGGING_SELECTION: {
hlcache_handle *h = bw->current_content;
int dir = -1;
size_t idx;
if (selection_dragging_start(&text->sel)) dir = 1;
idx = textplain_offset_from_coords(h, x, y, dir);
idx = textplain_offset_from_coords(c, x, y, dir);
selection_track(&text->sel, mouse, idx);
}
break;
@ -673,7 +671,6 @@ void textplain_mouse_action(struct content *c, struct browser_window *bw,
browser_mouse_state mouse, int x, int y)
{
textplain_content *text = (textplain_content *) c;
hlcache_handle *h = bw->current_content;
gui_pointer_shape pointer = GUI_POINTER_DEFAULT;
const char *status = 0;
size_t idx;
@ -681,7 +678,7 @@ void textplain_mouse_action(struct content *c, struct browser_window *bw,
browser_window_set_drag_type(bw, DRAGGING_NONE);
idx = textplain_offset_from_coords(h, x, y, dir);
idx = textplain_offset_from_coords(c, x, y, dir);
if (selection_click(&text->sel, mouse, idx)) {
if (selection_dragging(&text->sel)) {
@ -689,14 +686,14 @@ void textplain_mouse_action(struct content *c, struct browser_window *bw,
status = messages_get("Selecting");
}
else
status = content_get_status_message(h);
status = content__get_status_message(c);
}
else {
if (bw->loading_content)
status = content_get_status_message(
bw->loading_content);
else
status = content_get_status_message(h);
status = content__get_status_message(c);
if (mouse & (BROWSER_MOUSE_DRAG_1 | BROWSER_MOUSE_DRAG_2)) {
browser_window_page_drag_start(bw, x, y);
@ -867,7 +864,6 @@ void textplain_open(struct content *c, struct browser_window *bw,
text->bw = bw;
/* text selection */
selection_set_browser_window(&text->sel, bw);
selection_init(&text->sel, NULL);
}
@ -880,8 +876,6 @@ void textplain_close(struct content *c)
{
textplain_content *text = (textplain_content *) c;
selection_set_browser_window(&text->sel, NULL);
text->bw = NULL;
}
@ -903,14 +897,13 @@ struct selection *textplain_get_selection(struct content *c)
* \param h Content to retrieve line count from
* \return Number of lines
*/
unsigned long textplain_line_count(hlcache_handle *h)
unsigned long textplain_line_count(struct content *c)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *text = (textplain_content *) c;
assert(c != NULL);
return c->physical_line_count;
return text->physical_line_count;
}
/**
@ -919,14 +912,13 @@ unsigned long textplain_line_count(hlcache_handle *h)
* \param h Content to retrieve size of
* \return Size, in bytes, of data
*/
size_t textplain_size(hlcache_handle *h)
size_t textplain_size(struct content *c)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *text = (textplain_content *) c;
assert(c != NULL);
return c->utf8_data_size;
return text->utf8_data_size;
}
/**
@ -942,10 +934,9 @@ size_t textplain_size(hlcache_handle *h)
* \return byte offset of character containing (or nearest to) point
*/
size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir)
size_t textplain_offset_from_coords(struct content *c, int x, int y, int dir)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *textc = (textplain_content *) c;
float line_height = textplain_line_height();
struct textplain_line *line;
const char *text;
@ -958,7 +949,7 @@ size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir)
y = (int)((float)(y - MARGIN) / line_height);
x -= MARGIN;
nlines = c->physical_line_count;
nlines = textc->physical_line_count;
if (!nlines)
return 0;
@ -966,8 +957,8 @@ size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir)
else if ((unsigned)y >= nlines)
y = nlines - 1;
line = &c->physical_line[y];
text = c->utf8_data + line->start;
line = &textc->physical_line[y];
text = textc->utf8_data + line->start;
length = line->length;
idx = 0;
@ -1016,25 +1007,24 @@ size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir)
* Given a byte offset within the text, return the line number
* of the line containing that offset (or -1 if offset invalid)
*
* \param h content of type CONTENT_TEXTPLAIN
* \param c content of type CONTENT_TEXTPLAIN
* \param offset byte offset within textual representation
* \return line number, or -1 if offset invalid (larger than size)
*/
int textplain_find_line(hlcache_handle *h, unsigned offset)
int textplain_find_line(struct content *c, unsigned offset)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *text = (textplain_content *) c;
struct textplain_line *line;
int nlines;
int lineno = 0;
assert(c != NULL);
line = c->physical_line;
nlines = c->physical_line_count;
line = text->physical_line;
nlines = text->physical_line_count;
if (offset > c->utf8_data_size)
if (offset > text->utf8_data_size)
return -1;
/* \todo - implement binary search here */
@ -1097,11 +1087,10 @@ int textplain_coord_from_offset(const char *text, size_t offset, size_t length)
* \param r rectangle to be completed
*/
void textplain_coords_from_range(hlcache_handle *h, unsigned start,
void textplain_coords_from_range(struct content *c, unsigned start,
unsigned end, struct rect *r)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *text = (textplain_content *) c;
float line_height = textplain_line_height();
char *utf8_data;
struct textplain_line *line;
@ -1110,14 +1099,14 @@ void textplain_coords_from_range(hlcache_handle *h, unsigned start,
assert(c != NULL);
assert(start <= end);
assert(end <= c->utf8_data_size);
assert(end <= text->utf8_data_size);
utf8_data = c->utf8_data;
nlines = c->physical_line_count;
line = c->physical_line;
utf8_data = text->utf8_data;
nlines = text->physical_line_count;
line = text->physical_line;
/* find start */
lineno = textplain_find_line(h, start);
lineno = textplain_find_line(c, start);
r->y0 = (int)(MARGIN + lineno * line_height);
@ -1126,10 +1115,10 @@ void textplain_coords_from_range(hlcache_handle *h, unsigned start,
forwards most of the time */
/* find end */
lineno = textplain_find_line(h, end);
lineno = textplain_find_line(c, end);
r->x0 = 0;
r->x1 = c->formatted_width;
r->x1 = text->formatted_width;
}
else {
/* single line */
@ -1156,22 +1145,21 @@ void textplain_coords_from_range(hlcache_handle *h, unsigned start,
* \return pointer to text, or NULL if invalid line number
*/
char *textplain_get_line(hlcache_handle *h, unsigned lineno,
char *textplain_get_line(struct content *c, unsigned lineno,
size_t *poffset, size_t *plen)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *text = (textplain_content *) c;
struct textplain_line *line;
assert(c != NULL);
if (lineno >= c->physical_line_count)
if (lineno >= text->physical_line_count)
return NULL;
line = &c->physical_line[lineno];
line = &text->physical_line[lineno];
*poffset = line->start;
*plen = line->length;
return c->utf8_data + line->start;
return text->utf8_data + line->start;
}
@ -1187,16 +1175,15 @@ char *textplain_get_line(hlcache_handle *h, unsigned lineno,
* \return pointer to text, or NULL if no text
*/
char *textplain_get_raw_data(hlcache_handle *h, unsigned start, unsigned end,
char *textplain_get_raw_data(struct content *c, unsigned start, unsigned end,
size_t *plen)
{
textplain_content *c =
(textplain_content *) hlcache_handle_get_content(h);
textplain_content *text = (textplain_content *) c;
size_t utf8_size;
assert(c != NULL);
utf8_size = c->utf8_data_size;
utf8_size = text->utf8_data_size;
/* any text at all? */
if (!utf8_size) return NULL;
@ -1207,7 +1194,7 @@ char *textplain_get_raw_data(hlcache_handle *h, unsigned start, unsigned end,
*plen = end - start;
return c->utf8_data + start;
return text->utf8_data + start;
}
/**
@ -1224,3 +1211,19 @@ float textplain_line_height(void)
INTTOFIX((textplain_style.size / FONT_SIZE_SCALE))))), F_72));
}
/**
* Get the browser window containing a textplain content
*
* \param c text/plain content
* \return the browser window
*/
struct browser_window *textplain_get_browser_window(struct content *c)
{
textplain_content *text = (textplain_content *) c;
assert(c != NULL);
assert(c->handler == &textplain_content_handler);
return text->bw;
}

View File

@ -36,17 +36,17 @@ nserror textplain_init(void);
void textplain_fini(void);
/* access to lines for text selection and searching */
unsigned long textplain_line_count(struct hlcache_handle *h);
size_t textplain_size(struct hlcache_handle *h);
unsigned long textplain_line_count(struct content *c);
size_t textplain_size(struct content *c);
size_t textplain_offset_from_coords(struct hlcache_handle *h, int x, int y,
int dir);
void textplain_coords_from_range(struct hlcache_handle *h,
size_t textplain_offset_from_coords(struct content *c, int x, int y, int dir);
void textplain_coords_from_range(struct content *c,
unsigned start, unsigned end, struct rect *r);
char *textplain_get_line(struct hlcache_handle *h, unsigned lineno,
char *textplain_get_line(struct content *c, unsigned lineno,
size_t *poffset, size_t *plen);
int textplain_find_line(struct hlcache_handle *h, unsigned offset);
char *textplain_get_raw_data(struct hlcache_handle *h,
int textplain_find_line(struct content *c, unsigned offset);
char *textplain_get_raw_data(struct content *c,
unsigned start, unsigned end, size_t *plen);
struct browser_window *textplain_get_browser_window(struct content *c);
#endif