2005-04-15 09:51:32 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
|
2008-04-01 02:22:14 +04:00
|
|
|
* Copyright 2008 Michael Drake <tlsa@netsurf-browser.org>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-04-15 09:51:32 +04:00
|
|
|
*/
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/**
|
|
|
|
* \file
|
2020-05-22 01:23:52 +03:00
|
|
|
* implementation of text selection within browser windows.
|
|
|
|
*/
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-05-24 00:59:40 +03:00
|
|
|
#include "netsurf/clipboard.h"
|
2019-08-01 19:22:17 +03:00
|
|
|
#include "netsurf/browser_window.h"
|
2020-05-24 00:59:40 +03:00
|
|
|
#include "netsurf/window.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "content/content_protected.h"
|
2014-10-13 14:56:31 +04:00
|
|
|
|
2014-10-17 15:21:06 +04:00
|
|
|
#include "desktop/browser_private.h"
|
2014-10-16 12:48:09 +04:00
|
|
|
#include "desktop/gui_internal.h"
|
2020-05-24 00:59:40 +03:00
|
|
|
#include "desktop/selection.h"
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
|
2012-08-13 20:09:42 +04:00
|
|
|
struct selection_string {
|
|
|
|
char *buffer;
|
|
|
|
size_t buffer_len;
|
|
|
|
size_t length;
|
2013-01-08 20:54:46 +04:00
|
|
|
|
|
|
|
int n_styles;
|
|
|
|
nsclipboard_styles *styles;
|
2012-08-13 20:09:42 +04:00
|
|
|
};
|
|
|
|
|
2012-08-13 20:48:03 +04:00
|
|
|
|
2020-05-24 01:33:52 +03:00
|
|
|
typedef enum {
|
|
|
|
DRAG_NONE,
|
|
|
|
DRAG_START,
|
|
|
|
DRAG_END
|
|
|
|
} seln_drag_state;
|
|
|
|
|
|
|
|
struct selection {
|
|
|
|
struct content *c;
|
|
|
|
struct box *root;
|
|
|
|
|
|
|
|
unsigned max_idx; /* total bytes in text representation */
|
|
|
|
|
|
|
|
unsigned start_idx; /* offset in bytes within text representation */
|
|
|
|
unsigned end_idx;
|
|
|
|
|
|
|
|
bool defined;
|
|
|
|
|
|
|
|
seln_drag_state drag_state;
|
|
|
|
};
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
/**
|
|
|
|
* Redraws the given range of text.
|
|
|
|
*
|
2020-05-22 01:23:52 +03:00
|
|
|
* \param s selection object
|
|
|
|
* \param start_idx start offset (bytes) within the textual representation
|
|
|
|
* \param end_idx end offset (bytes) within the textual representation
|
2005-04-15 09:51:32 +04:00
|
|
|
*/
|
2020-05-23 20:39:25 +03:00
|
|
|
static nserror
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_redraw(struct selection *s, unsigned start_idx, unsigned end_idx)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2020-05-23 20:39:25 +03:00
|
|
|
nserror res;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2020-05-23 20:39:25 +03:00
|
|
|
if (s->c->handler->textselection_redraw != NULL) {
|
|
|
|
res = s->c->handler->textselection_redraw(s->c,
|
|
|
|
start_idx,
|
|
|
|
end_idx);
|
2020-05-22 01:23:52 +03:00
|
|
|
} else {
|
2020-05-23 20:39:25 +03:00
|
|
|
res = NSERROR_NOT_IMPLEMENTED;
|
2006-02-16 02:09:55 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:39:25 +03:00
|
|
|
return res;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/**
|
|
|
|
* Set the start position of the current selection, updating the screen.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param offset byte offset within textual representation
|
|
|
|
*/
|
|
|
|
static void selection_set_start(struct selection *s, unsigned offset)
|
|
|
|
{
|
|
|
|
bool was_defined;
|
|
|
|
unsigned old_start;
|
|
|
|
|
|
|
|
old_start = s->start_idx;
|
|
|
|
s->start_idx = offset;
|
|
|
|
|
|
|
|
was_defined = s->defined;
|
|
|
|
s->defined = (s->start_idx < s->end_idx);
|
|
|
|
|
|
|
|
if (was_defined) {
|
|
|
|
if (offset < old_start) {
|
|
|
|
selection_redraw(s, s->start_idx, old_start);
|
|
|
|
} else {
|
|
|
|
selection_redraw(s, old_start, s->start_idx);
|
|
|
|
}
|
|
|
|
} else if (s->defined) {
|
|
|
|
selection_redraw(s, s->start_idx, s->end_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the end position of the current selection, updating the screen.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param offset byte offset within textual representation
|
|
|
|
*/
|
|
|
|
static void selection_set_end(struct selection *s, unsigned offset)
|
|
|
|
{
|
2020-05-22 01:23:52 +03:00
|
|
|
bool was_defined;
|
|
|
|
unsigned old_end;
|
2020-05-21 00:15:33 +03:00
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
old_end = s->end_idx;
|
2020-05-21 00:15:33 +03:00
|
|
|
s->end_idx = offset;
|
2020-05-22 01:23:52 +03:00
|
|
|
|
|
|
|
was_defined = s->defined;
|
2020-05-21 00:15:33 +03:00
|
|
|
s->defined = (s->start_idx < s->end_idx);
|
|
|
|
|
|
|
|
if (was_defined) {
|
2020-05-22 01:23:52 +03:00
|
|
|
if (offset < old_end) {
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_redraw(s, s->end_idx, old_end);
|
2020-05-22 01:23:52 +03:00
|
|
|
} else {
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_redraw(s, old_end, s->end_idx);
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
|
|
|
} else if (s->defined) {
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_redraw(s, s->start_idx, s->end_idx);
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse the current selection, calling the handler function (with its
|
|
|
|
* handle) for all boxes that lie (partially) within the given range
|
|
|
|
*
|
2020-05-22 01:23:52 +03:00
|
|
|
* \param s The selection context.
|
2020-05-21 00:15:33 +03:00
|
|
|
* \param handler handler function to call
|
|
|
|
* \param handle handle to pass
|
|
|
|
* \return false iff traversal abandoned part-way through
|
|
|
|
*/
|
|
|
|
static bool
|
2020-05-23 22:38:41 +03:00
|
|
|
selection_copy(struct selection *s, struct selection_string *selstr)
|
2020-05-21 00:15:33 +03:00
|
|
|
{
|
2020-05-23 22:38:41 +03:00
|
|
|
nserror res;
|
2020-05-21 00:15:33 +03:00
|
|
|
|
2020-05-23 22:38:41 +03:00
|
|
|
if (s->c->handler->textselection_copy != NULL) {
|
|
|
|
res = s->c->handler->textselection_copy(s->c,
|
|
|
|
s->start_idx,
|
|
|
|
s->end_idx,
|
|
|
|
selstr);
|
|
|
|
} else {
|
|
|
|
res = NSERROR_NOT_IMPLEMENTED;
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 22:38:41 +03:00
|
|
|
if (res != NSERROR_OK) {
|
2020-05-21 00:15:33 +03:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-13 20:09:42 +04:00
|
|
|
/**
|
|
|
|
* Append text to selection string.
|
|
|
|
*
|
2014-11-10 20:00:15 +03:00
|
|
|
* \param text text to be added
|
|
|
|
* \param length length of text in bytes
|
|
|
|
* \param space indicates whether a trailing space should be appended
|
|
|
|
* \param style The font style to use.
|
|
|
|
* \param sel_string string to append to, may be resized
|
2012-08-13 20:09:42 +04:00
|
|
|
* \return true iff successful
|
|
|
|
*/
|
2020-05-23 22:38:41 +03:00
|
|
|
bool
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_string_append(const char *text,
|
|
|
|
size_t length,
|
|
|
|
bool space,
|
|
|
|
plot_font_style_t *style,
|
|
|
|
struct selection_string *sel_string)
|
2012-08-13 20:09:42 +04:00
|
|
|
{
|
|
|
|
size_t new_length = sel_string->length + length + (space ? 1 : 0) + 1;
|
|
|
|
|
2013-01-08 20:54:46 +04:00
|
|
|
if (style != NULL) {
|
|
|
|
/* Add text run style */
|
|
|
|
nsclipboard_styles *new_styles;
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
if (sel_string->n_styles == 0) {
|
2013-01-08 20:54:46 +04:00
|
|
|
assert(sel_string->length == 0);
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2013-01-08 20:54:46 +04:00
|
|
|
|
|
|
|
new_styles = realloc(sel_string->styles,
|
2020-05-22 01:23:52 +03:00
|
|
|
(sel_string->n_styles + 1) *
|
|
|
|
sizeof(nsclipboard_styles));
|
|
|
|
if (new_styles == NULL) {
|
2013-01-08 20:54:46 +04:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2013-01-08 20:54:46 +04:00
|
|
|
|
|
|
|
sel_string->styles = new_styles;
|
|
|
|
|
|
|
|
sel_string->styles[sel_string->n_styles].style = *style;
|
|
|
|
sel_string->styles[sel_string->n_styles].start =
|
2020-05-22 01:23:52 +03:00
|
|
|
sel_string->length;
|
2013-01-08 20:54:46 +04:00
|
|
|
|
|
|
|
sel_string->n_styles++;
|
|
|
|
}
|
|
|
|
|
2012-08-13 20:09:42 +04:00
|
|
|
if (new_length > sel_string->buffer_len) {
|
2013-01-08 20:54:46 +04:00
|
|
|
/* Need to extend buffer */
|
2012-08-13 20:09:42 +04:00
|
|
|
size_t new_alloc = new_length + (new_length / 4);
|
|
|
|
char *new_buff;
|
|
|
|
|
|
|
|
new_buff = realloc(sel_string->buffer, new_alloc);
|
2020-05-22 01:23:52 +03:00
|
|
|
if (new_buff == NULL) {
|
2012-08-13 20:09:42 +04:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2012-08-13 20:09:42 +04:00
|
|
|
|
|
|
|
sel_string->buffer = new_buff;
|
|
|
|
sel_string->buffer_len = new_alloc;
|
|
|
|
}
|
|
|
|
|
2013-01-08 20:54:46 +04:00
|
|
|
/* Copy text onto end of existing text in buffer */
|
2012-08-13 20:09:42 +04:00
|
|
|
memcpy(sel_string->buffer + sel_string->length, text, length);
|
|
|
|
sel_string->length += length;
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
if (space) {
|
2012-08-13 20:09:42 +04:00
|
|
|
sel_string->buffer[sel_string->length++] = ' ';
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2012-08-13 20:09:42 +04:00
|
|
|
|
2013-01-08 20:54:46 +04:00
|
|
|
/* Ensure NULL termination */
|
2012-08-13 20:09:42 +04:00
|
|
|
sel_string->buffer[sel_string->length] = '\0';
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/* exported interface documented in desktop/selection.h */
|
2020-05-24 00:09:40 +03:00
|
|
|
struct selection *selection_create(struct content *c)
|
2020-05-21 00:15:33 +03:00
|
|
|
{
|
2020-05-24 00:09:40 +03:00
|
|
|
struct selection *sel;
|
|
|
|
sel = calloc(1, sizeof(struct selection));
|
|
|
|
if (sel) {
|
|
|
|
selection_prepare(sel, c);
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 00:09:40 +03:00
|
|
|
return sel;
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
2020-05-24 00:09:40 +03:00
|
|
|
void selection_prepare(struct selection *s, struct content *c)
|
2020-05-21 00:15:33 +03:00
|
|
|
{
|
|
|
|
if (s) {
|
|
|
|
s->c = c;
|
|
|
|
s->root = NULL;
|
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
s->max_idx = 0;
|
|
|
|
selection_clear(s, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
|
|
|
void selection_destroy(struct selection *s)
|
|
|
|
{
|
|
|
|
if (s == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
selection_clear(s, true);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
2020-05-24 00:00:00 +03:00
|
|
|
void selection_reinit(struct selection *s)
|
2020-05-21 00:15:33 +03:00
|
|
|
{
|
2020-05-24 00:00:00 +03:00
|
|
|
s->max_idx = 0;
|
2012-08-13 20:09:42 +04:00
|
|
|
|
2020-05-24 00:00:00 +03:00
|
|
|
if (s->c->handler->textselection_get_end != NULL) {
|
|
|
|
s->c->handler->textselection_get_end(s->c, &s->max_idx);
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->defined) {
|
2020-05-22 01:23:52 +03:00
|
|
|
if (s->end_idx > s->max_idx) {
|
|
|
|
s->end_idx = s->max_idx;
|
|
|
|
}
|
|
|
|
if (s->start_idx > s->max_idx) {
|
|
|
|
s->start_idx = s->max_idx;
|
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
s->defined = (s->end_idx > s->start_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
2020-05-24 00:00:00 +03:00
|
|
|
void selection_init(struct selection *s)
|
2020-05-21 00:15:33 +03:00
|
|
|
{
|
|
|
|
if (s->defined) {
|
|
|
|
selection_clear(s, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->defined = false;
|
|
|
|
s->start_idx = 0;
|
|
|
|
s->end_idx = 0;
|
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
|
2020-05-24 00:00:00 +03:00
|
|
|
selection_reinit(s);
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
|
|
|
bool
|
|
|
|
selection_click(struct selection *s,
|
2020-05-21 01:17:48 +03:00
|
|
|
struct browser_window *top,
|
2020-05-21 00:15:33 +03:00
|
|
|
browser_mouse_state mouse,
|
|
|
|
unsigned idx)
|
|
|
|
{
|
2020-05-22 01:23:52 +03:00
|
|
|
browser_mouse_state modkeys;
|
2020-05-21 00:15:33 +03:00
|
|
|
int pos = -1; /* 0 = inside selection, 1 = after it */
|
2020-05-21 01:17:48 +03:00
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
modkeys = (mouse & (BROWSER_MOUSE_MOD_1 | BROWSER_MOUSE_MOD_2));
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
top = browser_window_get_root(top);
|
|
|
|
|
2020-05-24 00:59:40 +03:00
|
|
|
if (s->defined) {
|
2020-05-21 00:15:33 +03:00
|
|
|
if (idx > s->start_idx) {
|
2020-05-22 01:23:52 +03:00
|
|
|
if (idx <= s->end_idx) {
|
2020-05-21 00:15:33 +03:00
|
|
|
pos = 0;
|
2020-05-22 01:23:52 +03:00
|
|
|
} else {
|
2020-05-21 00:15:33 +03:00
|
|
|
pos = 1;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pos &&
|
2020-05-22 01:23:52 +03:00
|
|
|
((mouse & BROWSER_MOUSE_DRAG_1) ||
|
|
|
|
(modkeys && (mouse & BROWSER_MOUSE_DRAG_2)))) {
|
2020-05-21 00:15:33 +03:00
|
|
|
/* drag-saving selection */
|
|
|
|
char *sel = selection_get_copy(s);
|
|
|
|
guit->window->drag_save_selection(top->window, sel);
|
|
|
|
free(sel);
|
2020-05-22 01:23:52 +03:00
|
|
|
} else if (!modkeys) {
|
2020-05-21 00:15:33 +03:00
|
|
|
if (pos && (mouse & BROWSER_MOUSE_PRESS_1)) {
|
2020-05-22 01:23:52 +03:00
|
|
|
/* Clear the selection if mouse is pressed
|
|
|
|
* outside the selection, Otherwise clear on
|
|
|
|
* release (to allow for drags)
|
|
|
|
*/
|
2020-05-21 00:15:33 +03:00
|
|
|
|
|
|
|
selection_clear(s, true);
|
2020-05-22 01:23:52 +03:00
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
} else if (mouse & BROWSER_MOUSE_DRAG_1) {
|
|
|
|
/* start new selection drag */
|
|
|
|
|
|
|
|
selection_clear(s, true);
|
|
|
|
|
|
|
|
selection_set_start(s, idx);
|
|
|
|
selection_set_end(s, idx);
|
|
|
|
|
|
|
|
s->drag_state = DRAG_END;
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
guit->window->event(top->window,
|
|
|
|
GW_EVENT_START_SELECTION);
|
|
|
|
|
|
|
|
} else if (mouse & BROWSER_MOUSE_DRAG_2) {
|
2020-05-21 00:15:33 +03:00
|
|
|
|
|
|
|
/* adjust selection, but only if there is one */
|
2020-05-24 00:59:40 +03:00
|
|
|
if (!s->defined) {
|
2020-05-21 00:15:33 +03:00
|
|
|
return false; /* ignore Adjust drags */
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
|
|
|
|
if (pos >= 0) {
|
|
|
|
selection_set_end(s, idx);
|
|
|
|
|
|
|
|
s->drag_state = DRAG_END;
|
2020-05-22 01:23:52 +03:00
|
|
|
} else {
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_set_start(s, idx);
|
|
|
|
|
|
|
|
s->drag_state = DRAG_START;
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
guit->window->event(top->window,
|
|
|
|
GW_EVENT_START_SELECTION);
|
|
|
|
|
|
|
|
} else if (mouse & BROWSER_MOUSE_CLICK_2) {
|
2020-05-21 00:15:33 +03:00
|
|
|
|
|
|
|
/* ignore Adjust clicks when there's no selection */
|
2020-05-24 00:59:40 +03:00
|
|
|
if (!s->defined) {
|
2020-05-21 00:15:33 +03:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
if (pos >= 0) {
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_set_end(s, idx);
|
2020-05-22 01:23:52 +03:00
|
|
|
} else {
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_set_start(s, idx);
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-21 00:15:33 +03:00
|
|
|
s->drag_state = DRAG_NONE;
|
2020-05-22 01:23:52 +03:00
|
|
|
|
|
|
|
} else {
|
2020-05-21 00:15:33 +03:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2020-05-21 00:15:33 +03:00
|
|
|
/* not our problem */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this mouse click is selection-related */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
|
|
|
void
|
|
|
|
selection_track(struct selection *s, browser_mouse_state mouse, unsigned idx)
|
|
|
|
{
|
|
|
|
if (!mouse) {
|
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->drag_state) {
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
case DRAG_START:
|
|
|
|
if (idx > s->end_idx) {
|
|
|
|
unsigned old_end = s->end_idx;
|
|
|
|
selection_set_end(s, idx);
|
|
|
|
selection_set_start(s, old_end);
|
|
|
|
s->drag_state = DRAG_END;
|
|
|
|
} else {
|
|
|
|
selection_set_start(s, idx);
|
|
|
|
}
|
|
|
|
break;
|
2020-05-21 00:15:33 +03:00
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
case DRAG_END:
|
|
|
|
if (idx < s->start_idx) {
|
|
|
|
unsigned old_start = s->start_idx;
|
|
|
|
selection_set_start(s, idx);
|
|
|
|
selection_set_end(s, old_start);
|
|
|
|
s->drag_state = DRAG_START;
|
|
|
|
} else {
|
|
|
|
selection_set_end(s, idx);
|
|
|
|
}
|
|
|
|
break;
|
2020-05-21 00:15:33 +03:00
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
default:
|
|
|
|
break;
|
2020-05-21 00:15:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/selection.h */
|
2013-01-08 20:54:46 +04:00
|
|
|
char *selection_get_copy(struct selection *s)
|
2012-08-13 20:09:42 +04:00
|
|
|
{
|
2012-08-13 20:21:04 +04:00
|
|
|
struct selection_string sel_string = {
|
|
|
|
.buffer = NULL,
|
|
|
|
.buffer_len = 0,
|
2013-01-08 20:54:46 +04:00
|
|
|
.length = 0,
|
|
|
|
|
|
|
|
.n_styles = 0,
|
|
|
|
.styles = NULL
|
2012-08-13 20:21:04 +04:00
|
|
|
};
|
2012-08-13 20:09:42 +04:00
|
|
|
|
2012-08-13 20:38:09 +04:00
|
|
|
if (s == NULL || !s->defined)
|
2012-08-13 20:09:42 +04:00
|
|
|
return NULL;
|
|
|
|
|
2020-05-23 22:38:41 +03:00
|
|
|
if (!selection_copy(s, &sel_string)) {
|
2013-01-08 20:54:46 +04:00
|
|
|
free(sel_string.buffer);
|
|
|
|
free(sel_string.styles);
|
2012-08-13 20:09:42 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-08 20:54:46 +04:00
|
|
|
free(sel_string.styles);
|
|
|
|
|
2012-08-13 20:09:42 +04:00
|
|
|
return sel_string.buffer;
|
2012-08-02 17:23:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/* exported interface documented in desktop/selection.h */
|
2013-01-08 20:54:46 +04:00
|
|
|
bool selection_copy_to_clipboard(struct selection *s)
|
|
|
|
{
|
|
|
|
struct selection_string sel_string = {
|
|
|
|
.buffer = NULL,
|
|
|
|
.buffer_len = 0,
|
|
|
|
.length = 0,
|
|
|
|
|
|
|
|
.n_styles = 0,
|
|
|
|
.styles = NULL
|
|
|
|
};
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
if (s == NULL || !s->defined) {
|
2013-01-08 20:54:46 +04:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2013-01-08 20:54:46 +04:00
|
|
|
|
2020-05-23 22:38:41 +03:00
|
|
|
if (!selection_copy(s, &sel_string)) {
|
2013-01-08 20:54:46 +04:00
|
|
|
free(sel_string.buffer);
|
|
|
|
free(sel_string.styles);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
guit->clipboard->set(sel_string.buffer,
|
|
|
|
sel_string.length,
|
|
|
|
sel_string.styles,
|
|
|
|
sel_string.n_styles);
|
2013-01-08 20:54:46 +04:00
|
|
|
|
|
|
|
free(sel_string.buffer);
|
|
|
|
free(sel_string.styles);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/* exported interface documented in desktop/selection.h */
|
2020-05-24 00:59:40 +03:00
|
|
|
bool selection_clear(struct selection *s, bool redraw)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
|
|
|
int old_start, old_end;
|
|
|
|
bool was_defined;
|
|
|
|
|
|
|
|
assert(s);
|
2020-05-22 01:23:52 +03:00
|
|
|
|
|
|
|
was_defined = s->defined;
|
2005-04-15 09:51:32 +04:00
|
|
|
old_start = s->start_idx;
|
|
|
|
old_end = s->end_idx;
|
|
|
|
|
|
|
|
s->defined = false;
|
|
|
|
s->start_idx = 0;
|
|
|
|
s->end_idx = 0;
|
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
if (redraw && was_defined) {
|
2005-04-15 09:51:32 +04:00
|
|
|
selection_redraw(s, old_start, old_end);
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2020-05-24 00:59:40 +03:00
|
|
|
|
|
|
|
return was_defined;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/* exported interface documented in desktop/selection.h */
|
2005-04-15 09:51:32 +04:00
|
|
|
void selection_select_all(struct selection *s)
|
|
|
|
{
|
|
|
|
assert(s);
|
|
|
|
s->defined = true;
|
2020-05-21 00:15:33 +03:00
|
|
|
|
2013-02-07 18:21:48 +04:00
|
|
|
selection_set_start(s, 0);
|
Merged revisions 4282-4285,4288-4293,4297-4298,4307,4309-4313,4322,4324-4680 via svnmerge from
svn://svn.netsurf-browser.org/branches/mikeL/netsurf
........
r4432 | mikeL | 2008-06-24 04:00:36 +0100 (Tue, 24 Jun 2008) | 1 line
Drag events are now emited from where the press originated, instead of from where they became a drag
........
r4433 | mikeL | 2008-06-24 04:25:33 +0100 (Tue, 24 Jun 2008) | 1 line
Added accelerator to 'Select All'
........
r4495 | mikeL | 2008-07-02 21:36:32 +0100 (Wed, 02 Jul 2008) | 1 line
Selections are now deleted and replaced when a key is typed in a text area or text box. All input box behavior while a selection is active is now implemented (ex: pressing the right arrow key moves the caret to the end of the selection). Cut now works properly in both versions. Fixed discrepancy between where the caret was placed and selection began when starting a drag-select. Fixed bug with calculation of a selections end box.
........
r4496 | mikeL | 2008-07-02 22:11:24 +0100 (Wed, 02 Jul 2008) | 1 line
Added support for cut in input boxes
........
r4497 | mikeL | 2008-07-02 22:21:35 +0100 (Wed, 02 Jul 2008) | 1 line
Removed unused variables (Thanks tlsa)
........
r4498 | mikeL | 2008-07-02 23:30:30 +0100 (Wed, 02 Jul 2008) | 1 line
Modified selection clearing behavior to allow for drag-saves
........
r4499 | mikeL | 2008-07-03 00:51:50 +0100 (Thu, 03 Jul 2008) | 1 line
Fixed regression where it would take two clicks to place caret in an input (Thanks tlsa)
........
r4509 | mikeL | 2008-07-06 07:55:09 +0100 (Sun, 06 Jul 2008) | 1 line
Basic download support implemented. Only downloading of text files works
........
r4510 | mikeL | 2008-07-06 18:55:31 +0100 (Sun, 06 Jul 2008) | 1 line
Downloading of non-text files is now possible. Progress bar and size downloaded are now updated
........
r4511 | mikeL | 2008-07-06 20:46:00 +0100 (Sun, 06 Jul 2008) | 1 line
Added downloads glade file
........
r4512 | mikeL | 2008-07-06 20:47:39 +0100 (Sun, 06 Jul 2008) | 1 line
Downloads window now spawns in the center of the parent browser window
........
r4513 | mikeL | 2008-07-06 20:56:12 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed bug where backspace would be ignored if the selection began at the beginning on an input
........
r4514 | mikeL | 2008-07-06 21:26:45 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed compiler warnings by adding casts
........
r4516 | mikeL | 2008-07-06 21:32:41 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed initialization of size string, added initialization of progress
........
r4518 | mikeL | 2008-07-06 21:51:08 +0100 (Sun, 06 Jul 2008) | 1 line
Added an option for short units (with space) to human_friendly_bytesize
........
r4519 | mikeL | 2008-07-06 21:52:05 +0100 (Sun, 06 Jul 2008) | 1 line
Removed function size_to_string
........
r4520 | mikeL | 2008-07-06 22:03:11 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed pedantic error (kB instead of KB). Added missing necessary parameters to human_friendly_bytesize. Fixed incorrect bool types
........
r4521 | mikeL | 2008-07-06 22:08:15 +0100 (Sun, 06 Jul 2008) | 1 line
Removed unnecessary parameter and units list from human_friendly_bytesize
........
r4522 | mikeL | 2008-07-06 22:57:03 +0100 (Sun, 06 Jul 2008) | 1 line
Removed unnused variable
........
r4523 | mikeL | 2008-07-06 23:03:46 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed url parsing by replacing url_parse_filename with url_nice. Total size and size downloaded are now in human readable form. Speed is now calculated (roughly)
........
r4524 | mikeL | 2008-07-07 01:19:01 +0100 (Mon, 07 Jul 2008) | 1 line
Added file overwrite confirmation. Changed speed to a double
........
r4546 | mikeL | 2008-07-09 17:21:43 +0100 (Wed, 09 Jul 2008) | 1 line
Changed parameter of selection_get_end/start to a size_t instead of int and changed all offset variables to size_t as well
........
r4547 | mikeL | 2008-07-09 17:30:47 +0100 (Wed, 09 Jul 2008) | 1 line
Added action buttons to the bottom toolbar. Added ability to clear selected (and completed) downloads with a framework for other actions.
........
r4556 | jmb | 2008-07-10 00:17:24 +0100 (Thu, 10 Jul 2008) | 3 lines
A large bunch of tidying and general fixes to text input handling.
Make selection code treat password fields as inputs, too.
........
r4557 | mikeL | 2008-07-10 00:24:46 +0100 (Thu, 10 Jul 2008) | 1 line
Added functionality to gui_empty_clipboard and gui_start_selection (Thanks jmb)
........
r4573 | mikeL | 2008-07-10 16:33:27 +0100 (Thu, 10 Jul 2008) | 1 line
Removed example download. Made the list store row aware of its download and vise versa. Added new way of handling actions from the dialog (e.g. buttons) which handles all rows in the selection. Added a few memory management function calls to clean up better
........
r4577 | mikeL | 2008-07-10 21:11:51 +0100 (Thu, 10 Jul 2008) | 1 line
Download write channels now close properly. Added status column to the tree store which will change the progress bar text if a download is canceled or completed. Implemented cancel button functionality.
........
r4578 | mikeL | 2008-07-10 21:17:51 +0100 (Thu, 10 Jul 2008) | 1 line
Speed is now displayed as '-' when 0 or download has stopped
........
r4580 | mikeL | 2008-07-11 02:10:57 +0100 (Fri, 11 Jul 2008) | 1 line
Added two download related options (Download directory & Clear completed downloads) and a Downloads tab to the preferences dialog. Also moved the option to ask when overwriting files to Downloads tab. Added another option to the pre-download dialog, Save, which downloads the file immediately to your 'Download directory'
........
r4581 | mikeL | 2008-07-11 02:26:00 +0100 (Fri, 11 Jul 2008) | 1 line
Rearranged pre-download dialog buttons to conform to the HIG
........
r4616 | mikeL | 2008-07-11 19:54:12 +0100 (Fri, 11 Jul 2008) | 1 line
Limited download window updates to a user-defined variable that can be set in options (default is .5). Updates are now only done if the download window is visible. This greatly reduces the cpu usage.
........
r4617 | mikeL | 2008-07-11 20:07:48 +0100 (Fri, 11 Jul 2008) | 1 line
Removed unnecessary update limit option (it is now fixed at .5)
........
r4629 | mikeL | 2008-07-13 04:21:07 +0100 (Sun, 13 Jul 2008) | 1 line
Reorganized button sensitivity functions. Sensitivities are now updated when the selection changes as well as when a selected download's state changes.
........
r4635 | mikeL | 2008-07-13 17:00:05 +0100 (Sun, 13 Jul 2008) | 1 line
Added error handling. Added word-wrap to the "info" cell renderer so that to keep everything under control. Fixed bug where downloads would always go to you default folder (missing breaks).
........
r4642 | mikeL | 2008-07-13 21:46:09 +0100 (Sun, 13 Jul 2008) | 1 line
Added time remaining column. Fixed regression where the download info would be erased upon completion/cancelation.
........
r4655 | mikeL | 2008-07-14 23:20:33 +0100 (Mon, 14 Jul 2008) | 1 line
Downloads dialog is now initialized in gtk_gui.c with no parent window. The parent windows are now set when a download is created (through an extra parameter in gui_download_window_create) and when nsgtk_download_show is called. When it is closed (when NS shuts down) all incomplete downloads are canceled (and the files deleted). Added a warning dialog when netsurf tries to close with incomplete downloads. Fixed bug where default save directory would initialize to NULL.
........
r4676 | mikeL | 2008-07-15 21:01:17 +0100 (Tue, 15 Jul 2008) | 1 line
Downloads dialog is now initialized in gtk_gui.c with no parent window. The parent windows are now set when a download is created (through an extra parameter in gui_download_window_create) and when nsgtk_download_show is called. (This is the second half of the patch, last commit was only partial for some reason)
........
r4678 | mikeL | 2008-07-16 01:18:52 +0100 (Wed, 16 Jul 2008) | 1 line
Addresses almost all of rjek and jmb's concerns, fixes most of the sloppiness that was present earlier. Downloads without a total_size are now handled correctly (thanks tlsa). Changes to the default download directly are now saved correctly. Billions of other small bugs fixed
........
svn path=/trunk/netsurf/; revision=4681
2008-07-16 14:19:30 +04:00
|
|
|
selection_set_end(s, s->max_idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/* exported interface documented in desktop/selection.h */
|
|
|
|
void selection_set_position(struct selection *s, unsigned start, unsigned end)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2020-05-21 00:15:33 +03:00
|
|
|
selection_set_start(s, start);
|
|
|
|
selection_set_end(s, end);
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 00:15:33 +03:00
|
|
|
/* exported interface documented in desktop/selection.h */
|
|
|
|
bool
|
|
|
|
selection_highlighted(const struct selection *s,
|
|
|
|
unsigned start,
|
|
|
|
unsigned end,
|
|
|
|
unsigned *start_idx,
|
|
|
|
unsigned *end_idx)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2005-07-24 10:13:25 +04:00
|
|
|
assert(s);
|
2020-05-24 00:59:40 +03:00
|
|
|
|
|
|
|
if (!s->defined) {
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2020-05-22 01:23:52 +03:00
|
|
|
if ((end <= s->start_idx) ||
|
|
|
|
(start >= s->end_idx)) {
|
2006-02-16 02:09:55 +03:00
|
|
|
return false;
|
2020-05-22 01:23:52 +03:00
|
|
|
}
|
2006-02-16 02:09:55 +03:00
|
|
|
|
|
|
|
*start_idx = (s->start_idx >= start) ? (s->start_idx - start) : 0;
|
|
|
|
*end_idx = min(end, s->end_idx) - start;
|
|
|
|
|
|
|
|
return true;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
2020-05-24 00:59:40 +03:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|