remove all direct access to selection structure

This commit is contained in:
Vincent Sanders 2020-05-23 22:59:40 +01:00
parent 1161029d29
commit 6108d210d5
6 changed files with 65 additions and 82 deletions

View File

@ -1247,7 +1247,7 @@ default_mouse_action(html_content *html,
selection_clear(&html->sel, true); selection_clear(&html->sel, true);
} }
if (selection_defined(&html->sel)) { if (selection_active(&html->sel)) {
sel_owner.none = false; sel_owner.none = false;
html_set_selection(html, html_set_selection(html,
HTML_SELECTION_SELF, HTML_SELECTION_SELF,
@ -1586,13 +1586,8 @@ bool html_keypress(struct content *c, uint32_t key)
return true; return true;
case NS_KEY_ESCAPE: case NS_KEY_ESCAPE:
if (selection_defined(sel)) {
selection_clear(sel, true);
return true;
}
/* if there's no selection, leave Escape for the caller */ /* if there's no selection, leave Escape for the caller */
return false; return selection_clear(sel, true);
} }
return false; return false;

View File

@ -184,8 +184,7 @@ text_redraw(const char *utf8_text,
unsigned end_idx; unsigned end_idx;
/* first try the browser window's current selection */ /* first try the browser window's current selection */
if (selection_defined(sel) && if (selection_highlighted(sel,
selection_highlighted(sel,
offset, offset,
offset + len, offset + len,
&start_idx, &start_idx,

View File

@ -810,21 +810,14 @@ static bool textplain_keypress(struct content *c, uint32_t key)
return true; return true;
case NS_KEY_ESCAPE: case NS_KEY_ESCAPE:
if (selection_defined(sel)) {
selection_clear(sel, true);
return true;
}
/* if there's no selection, leave Escape for the caller */ /* if there's no selection, leave Escape for the caller */
return false; return selection_clear(sel, true);
} }
return false; return false;
} }
/** /**
* Redraw a text string with highlighting * Redraw a text string with highlighting
* (for selection/search) * (for selection/search)
@ -871,8 +864,7 @@ text_draw(const char *utf8_text,
unsigned end_idx; unsigned end_idx;
/* first try the browser window's current selection */ /* first try the browser window's current selection */
if (selection_defined(sel) && if (selection_highlighted(sel,
selection_highlighted(sel,
offset, offset,
offset + len, offset + len,
&start_idx, &start_idx,
@ -1167,16 +1159,16 @@ textplain_redraw(struct content *c,
struct selection *sel = &text->sel; struct selection *sel = &text->sel;
bool highlighted = false; bool highlighted = false;
if (selection_defined(sel)) { unsigned start_idx, end_idx;
unsigned start_idx, end_idx; if (selection_highlighted(sel,
if (selection_highlighted(sel, tab_ofst,
tab_ofst, tab_ofst + 1,
tab_ofst + 1, &start_idx,
&start_idx, &end_idx)) {
&end_idx)) highlighted = true;
highlighted = true;
} }
if (!highlighted && if (!highlighted &&
(c->textsearch.context != NULL)) { (c->textsearch.context != NULL)) {
unsigned start_idx, end_idx; unsigned start_idx, end_idx;
@ -1682,9 +1674,3 @@ size_t textplain_size(struct content *c)
return text->utf8_data_size; return text->utf8_data_size;
} }

View File

@ -165,7 +165,6 @@ static void free_matches(struct textsearch_context *textsearch)
static void search_show_all(bool all, struct textsearch_context *context) static void search_show_all(bool all, struct textsearch_context *context)
{ {
struct list_entry *a; struct list_entry *a;
nserror res;
for (a = context->found->next; a; a = a->next) { for (a = context->found->next; a; a = a->next) {
bool add = true; bool add = true;
@ -628,7 +627,6 @@ content_textsearch_ishighlighted(struct textsearch_context *textsearch,
for (cur = textsearch->found->next; cur != NULL; cur = cur->next) { for (cur = textsearch->found->next; cur != NULL; cur = cur->next) {
if (cur->sel && if (cur->sel &&
selection_defined(cur->sel) &&
selection_highlighted(cur->sel, selection_highlighted(cur->sel,
start_offset, start_offset,
end_offset, end_offset,

View File

@ -22,32 +22,17 @@
* implementation of text selection within browser windows. * implementation of text selection within browser windows.
*/ */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <dom/dom.h>
#include "utils/log.h"
#include "utils/utf8.h"
#include "utils/utils.h"
#include "netsurf/form.h"
#include "html/box.h"
#include "html/box_inspect.h"
#include "html/private.h"
#include "html/font.h"
#include "text/textplain.h"
#include "netsurf/browser_window.h"
#include "netsurf/mouse.h"
#include "desktop/browser_private.h"
#include "netsurf/plotters.h"
#include "desktop/save_text.h"
#include "desktop/selection.h"
#include "netsurf/clipboard.h" #include "netsurf/clipboard.h"
#include "netsurf/browser_window.h"
#include "netsurf/window.h" #include "netsurf/window.h"
#include "utils/utils.h"
#include "content/content_protected.h"
#include "desktop/browser_private.h"
#include "desktop/gui_internal.h" #include "desktop/gui_internal.h"
#include "desktop/selection.h"
struct selection_string { struct selection_string {
@ -334,7 +319,7 @@ selection_click(struct selection *s,
top = browser_window_get_root(top); top = browser_window_get_root(top);
if (selection_defined(s)) { if (s->defined) {
if (idx > s->start_idx) { if (idx > s->start_idx) {
if (idx <= s->end_idx) { if (idx <= s->end_idx) {
pos = 0; pos = 0;
@ -376,7 +361,7 @@ selection_click(struct selection *s,
} else if (mouse & BROWSER_MOUSE_DRAG_2) { } else if (mouse & BROWSER_MOUSE_DRAG_2) {
/* adjust selection, but only if there is one */ /* adjust selection, but only if there is one */
if (!selection_defined(s)) { if (!s->defined) {
return false; /* ignore Adjust drags */ return false; /* ignore Adjust drags */
} }
@ -396,7 +381,7 @@ selection_click(struct selection *s,
} else if (mouse & BROWSER_MOUSE_CLICK_2) { } else if (mouse & BROWSER_MOUSE_CLICK_2) {
/* ignore Adjust clicks when there's no selection */ /* ignore Adjust clicks when there's no selection */
if (!selection_defined(s)) { if (!s->defined) {
return false; return false;
} }
@ -521,7 +506,7 @@ bool selection_copy_to_clipboard(struct selection *s)
/* exported interface documented in desktop/selection.h */ /* exported interface documented in desktop/selection.h */
void selection_clear(struct selection *s, bool redraw) bool selection_clear(struct selection *s, bool redraw)
{ {
int old_start, old_end; int old_start, old_end;
bool was_defined; bool was_defined;
@ -539,6 +524,8 @@ void selection_clear(struct selection *s, bool redraw)
if (redraw && was_defined) { if (redraw && was_defined) {
selection_redraw(s, old_start, old_end); selection_redraw(s, old_start, old_end);
} }
return was_defined;
} }
@ -569,9 +556,11 @@ selection_highlighted(const struct selection *s,
unsigned *start_idx, unsigned *start_idx,
unsigned *end_idx) unsigned *end_idx)
{ {
/* caller should have checked first for efficiency */
assert(s); assert(s);
assert(s->defined);
if (!s->defined) {
return false;
}
if ((end <= s->start_idx) || if ((end <= s->start_idx) ||
(start >= s->end_idx)) { (start >= s->end_idx)) {
@ -583,3 +572,24 @@ selection_highlighted(const struct selection *s,
return true; return true;
} }
/* exported interface documented in desktop/selection.h */
bool selection_active(struct selection *s)
{
return s->defined;
}
bool selection_dragging(struct selection *s)
{
return s->drag_state != DRAG_NONE;
}
bool selection_dragging_start(struct selection *s)
{
return s->drag_state == DRAG_START;
}
void selection_drag_end(struct selection *s)
{
s->drag_state = DRAG_NONE;
}

View File

@ -20,8 +20,8 @@
* Text selection within browser windows (interface). * Text selection within browser windows (interface).
*/ */
#ifndef _NETSURF_DESKTOP_SELECTION_H_ #ifndef NETSURF_DESKTOP_SELECTION_H_
#define _NETSURF_DESKTOP_SELECTION_H_ #define NETSURF_DESKTOP_SELECTION_H_
#include <stdbool.h> #include <stdbool.h>
#include "netsurf/mouse.h" #include "netsurf/mouse.h"
@ -37,13 +37,7 @@ typedef enum {
DRAG_END DRAG_END
} seln_drag_state; } seln_drag_state;
struct selection {
/* this structure should be treated as opaque outside selection.c
(it's defined here to accelerate selection_defined(s) for reduced
impact on redraw code) */
struct selection
{
struct content *c; struct content *c;
struct box *root; struct box *root;
@ -57,19 +51,19 @@ struct selection
seln_drag_state drag_state; seln_drag_state drag_state;
}; };
/**
* determine if a selecion is active
*/
bool selection_active(struct selection *s);
/* bool selection_defined(struct selection *s); */ bool selection_dragging(struct selection *s);
#define selection_defined(s) ((s)->defined)
/* bool selection_dragging(struct selection *s); */ bool selection_dragging_start(struct selection *s);
#define selection_dragging(s) ((s)->drag_state != DRAG_NONE)
/* bool selection_dragging_start(struct selection *s); */ /**
#define selection_dragging_start(s) ((s)->drag_state == DRAG_START) * Handles completion of a drag operation
*/
/** Handles completion of a drag operation */ void selection_drag_end(struct selection *s);
/* void selection_drag_end(struct selection *s); */
#define selection_drag_end(s) ((s)->drag_state = DRAG_NONE)
/** /**
* Creates a new selection object associated with a browser window. * Creates a new selection object associated with a browser window.
@ -129,8 +123,9 @@ void selection_reinit(struct selection *s);
* \param s selection object * \param s selection object
* \param redraw true iff the previously selected region of the browser * \param redraw true iff the previously selected region of the browser
* window should be redrawn * window should be redrawn
* \return true if selection was cleared false if not
*/ */
void selection_clear(struct selection *s, bool redraw); bool selection_clear(struct selection *s, bool redraw);
/** /**
* Selects all the text within the box subtree controlled by * Selects all the text within the box subtree controlled by