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
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
2008-04-13 22:21:22 +04:00
|
|
|
* Text selection within browser windows (implementation).
|
2005-04-15 09:51:32 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "desktop/gui.h"
|
|
|
|
#include "desktop/plotters.h"
|
2008-04-13 22:21:22 +04:00
|
|
|
#include "desktop/save_text.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "desktop/selection.h"
|
|
|
|
#include "render/box.h"
|
2008-04-07 13:00:21 +04:00
|
|
|
#include "render/font.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "render/form.h"
|
|
|
|
#include "render/textplain.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utf8.h"
|
|
|
|
#include "utils/utils.h"
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
|
2006-02-11 21:33:05 +03:00
|
|
|
/**
|
|
|
|
* Text selection works by labelling each node in the box tree with its
|
|
|
|
* start index in the textual representation of the tree's content.
|
|
|
|
*
|
|
|
|
* Text input fields and text areas have their own number spaces so that
|
|
|
|
* they can be relabelled more efficiently when editing (rather than relabel
|
|
|
|
* the entire box tree) and so that selections are either wholly within
|
|
|
|
* or wholly without the textarea/input box.
|
|
|
|
*/
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
#define IS_INPUT(box) ((box) && (box)->gadget && \
|
2006-02-11 21:33:05 +03:00
|
|
|
((box)->gadget->type == GADGET_TEXTAREA || (box)->gadget->type == GADGET_TEXTBOX))
|
|
|
|
|
|
|
|
/** check whether the given text box is in the same number space as the
|
|
|
|
current selection; number spaces are identified by their uppermost nybble */
|
|
|
|
|
2006-09-10 19:08:30 +04:00
|
|
|
#define NUMBER_SPACE(x) ((x) & 0xF0000000U)
|
|
|
|
#define SAME_SPACE(s, offset) (NUMBER_SPACE((s)->max_idx) == NUMBER_SPACE(offset))
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
struct rdw_info {
|
|
|
|
bool inited;
|
2006-02-16 02:09:55 +03:00
|
|
|
struct rect r;
|
2005-04-15 09:51:32 +04:00
|
|
|
};
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
static bool redraw_handler(const char *text, size_t length, struct box *box,
|
2008-03-31 22:59:23 +04:00
|
|
|
void *handle, const char *whitespace_text,
|
|
|
|
size_t whitespace_length);
|
2008-03-31 22:04:36 +04:00
|
|
|
static void selection_redraw(struct selection *s, unsigned start_idx,
|
|
|
|
unsigned end_idx);
|
2008-04-07 13:00:21 +04:00
|
|
|
static unsigned selection_label_subtree(struct box *box, unsigned idx);
|
2008-03-31 22:04:36 +04:00
|
|
|
static bool save_handler(const char *text, size_t length, struct box *box,
|
2008-03-31 22:59:23 +04:00
|
|
|
void *handle, const char *whitespace_text,
|
|
|
|
size_t whitespace_length);
|
2005-07-24 10:13:25 +04:00
|
|
|
static bool selected_part(struct box *box, unsigned start_idx, unsigned end_idx,
|
|
|
|
unsigned *start_offset, unsigned *end_offset);
|
2005-04-15 09:51:32 +04:00
|
|
|
static bool traverse_tree(struct box *box, unsigned start_idx, unsigned end_idx,
|
2006-09-10 19:08:30 +04:00
|
|
|
unsigned int num_space, seln_traverse_handler handler,
|
2008-04-13 22:21:22 +04:00
|
|
|
void *handle, save_text_whitespace *before, bool *first,
|
2008-04-07 13:00:21 +04:00
|
|
|
bool do_marker);
|
2005-07-21 03:27:28 +04:00
|
|
|
static struct box *get_box(struct box *b, unsigned offset, int *pidx);
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new selection object associated with a browser window.
|
|
|
|
*
|
|
|
|
* \param bw browser window
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct selection *selection_create(struct browser_window *bw)
|
|
|
|
{
|
|
|
|
struct selection *s = malloc(sizeof(struct selection));
|
|
|
|
if (s) {
|
|
|
|
s->bw = bw;
|
|
|
|
s->root = NULL;
|
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
selection_clear(s, false);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-02-13 00:23:22 +03:00
|
|
|
* Destroys a selection object, without updating the
|
|
|
|
* owning window (caller should call selection_clear()
|
|
|
|
* first if update is desired)
|
2005-04-15 09:51:32 +04:00
|
|
|
*
|
2006-02-13 00:23:22 +03:00
|
|
|
* \param s selection object
|
2005-04-15 09:51:32 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_destroy(struct selection *s)
|
|
|
|
{
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the selection object to use the given box subtree as its root,
|
|
|
|
* ie. selections are confined to that subtree, whilst maintaining the current
|
|
|
|
* selection whenever possible because, for example, it's just the page being
|
|
|
|
* resized causing the layout to change.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param root the box (page/textarea) to be used as the root node for this selection
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_reinit(struct selection *s, struct box *root)
|
|
|
|
{
|
2006-02-11 21:33:05 +03:00
|
|
|
unsigned root_idx;
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
assert(s);
|
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
if (IS_INPUT(root)) {
|
2006-02-11 21:33:05 +03:00
|
|
|
static int next_idx = 0;
|
|
|
|
root_idx = (next_idx++) << 28;
|
|
|
|
}
|
2006-02-13 00:23:22 +03:00
|
|
|
else
|
|
|
|
root_idx = 0;
|
|
|
|
|
|
|
|
// if (s->root == root) {
|
|
|
|
// /* keep the same number space as before, because we want
|
|
|
|
// to keep the selection too */
|
|
|
|
// root_idx = (s->max_idx & 0xF0000000U);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// static int next_idx = 0;
|
|
|
|
// root_idx = (next_idx++) << 28;
|
|
|
|
// }
|
2006-02-11 21:33:05 +03:00
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
s->root = root;
|
2005-07-21 03:27:28 +04:00
|
|
|
if (root) {
|
2008-04-07 13:00:21 +04:00
|
|
|
s->max_idx = selection_label_subtree(root, root_idx);
|
2005-07-21 03:27:28 +04:00
|
|
|
}
|
2006-02-16 02:09:55 +03:00
|
|
|
else {
|
|
|
|
struct content *c = s->bw->current_content;
|
|
|
|
if (c && c->type == CONTENT_TEXTPLAIN)
|
|
|
|
s->max_idx = textplain_size(c);
|
|
|
|
else
|
|
|
|
s->max_idx = 0;
|
|
|
|
}
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
if (s->defined) {
|
|
|
|
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;
|
|
|
|
s->defined = (s->end_idx > s->start_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the selection object to use the given box subtree as its root,
|
|
|
|
* ie. selections are confined to that subtree.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param root the box (page/textarea) to be used as the root node for this selection
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_init(struct selection *s, struct box *root)
|
|
|
|
{
|
2005-07-21 03:27:28 +04:00
|
|
|
if (s->defined)
|
|
|
|
selection_clear(s, true);
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
s->defined = false;
|
|
|
|
s->start_idx = 0;
|
|
|
|
s->end_idx = 0;
|
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
|
|
|
|
selection_reinit(s, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Label each text box in the given box subtree with its position
|
|
|
|
* in a textual representation of the content.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param node box at root of subtree
|
|
|
|
* \param idx current position within textual representation
|
|
|
|
* \return updated position
|
|
|
|
*/
|
|
|
|
|
2008-04-07 13:00:21 +04:00
|
|
|
unsigned selection_label_subtree(struct box *box, unsigned idx)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2008-04-07 13:00:21 +04:00
|
|
|
struct box *child = box->children;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2008-04-07 13:00:21 +04:00
|
|
|
box->byte_offset = idx;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2008-04-08 16:40:05 +04:00
|
|
|
if (box->text)
|
2008-04-07 13:00:21 +04:00
|
|
|
idx += box->length + box->space;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
while (child) {
|
2008-04-07 13:00:21 +04:00
|
|
|
if (!IS_INPUT(child)) {
|
|
|
|
if (child->list_marker)
|
|
|
|
idx = selection_label_subtree(
|
|
|
|
child->list_marker, idx);
|
|
|
|
|
|
|
|
idx = selection_label_subtree(child, idx);
|
|
|
|
}
|
2005-04-15 09:51:32 +04:00
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles mouse clicks (including drag starts) in or near a selection
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param mouse state of mouse buttons and modifier keys
|
2006-02-16 02:09:55 +03:00
|
|
|
* \param idx byte offset within textual representation
|
2005-04-15 09:51:32 +04:00
|
|
|
*
|
|
|
|
* \return true iff the click has been handled by the selection code
|
|
|
|
*/
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
bool selection_click(struct selection *s, browser_mouse_state mouse,
|
2008-03-31 22:59:23 +04:00
|
|
|
unsigned idx)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2008-03-31 22:04:36 +04:00
|
|
|
browser_mouse_state modkeys =
|
|
|
|
(mouse & (BROWSER_MOUSE_MOD_1 | BROWSER_MOUSE_MOD_2));
|
2005-04-15 09:51:32 +04:00
|
|
|
int pos = -1; /* 0 = inside selection, 1 = after it */
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
if (!SAME_SPACE(s, idx))
|
2005-04-15 09:51:32 +04:00
|
|
|
return false; /* not our problem */
|
|
|
|
|
|
|
|
if (selection_defined(s)) {
|
2008-03-30 04:06:16 +04:00
|
|
|
if (idx > s->start_idx) {
|
2008-04-07 13:00:21 +04:00
|
|
|
if (idx <= s->end_idx)
|
2005-04-15 09:51:32 +04:00
|
|
|
pos = 0;
|
|
|
|
else
|
|
|
|
pos = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 03:07:18 +03:00
|
|
|
if (!pos &&
|
2006-02-16 02:09:55 +03:00
|
|
|
((mouse & BROWSER_MOUSE_DRAG_1) ||
|
|
|
|
(modkeys && (mouse & BROWSER_MOUSE_DRAG_2)))) {
|
2005-04-15 09:51:32 +04:00
|
|
|
/* drag-saving selection */
|
2005-04-18 15:52:26 +04:00
|
|
|
assert(s->bw);
|
|
|
|
gui_drag_save_selection(s, s->bw->window);
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
else if (!modkeys) {
|
|
|
|
if (mouse & BROWSER_MOUSE_DRAG_1) {
|
Merged revisions 4114-4265,4267-4272,4275-4285,4287-4325 via svnmerge from
svn://source.netsurf-browser.org/branches/mikeL/netsurf
........
r4116 | mikeL | 2008-05-01 22:15:12 +0100 (Thu, 01 May 2008) | 2 lines
Redesigned right-click menu: added back, forward, and reload, previously default menu items are now hidden unless view->toolbars->menu bar is disabled
........
r4117 | mikeL | 2008-05-02 03:54:10 +0100 (Fri, 02 May 2008) | 2 lines
Added a Current Page button to Preferences next to the url entry for setting the home page
........
r4118 | mikeL | 2008-05-02 04:27:37 +0100 (Fri, 02 May 2008) | 1 line
Cleanup
........
r4119 | mikeL | 2008-05-02 21:09:44 +0100 (Fri, 02 May 2008) | 1 line
Preferences window is now initialized the first time edit->preferences is clicked instead of during nsgtk initialization. Expedites start-up time because preferences is a non-essential dialog.
........
r4123 | mikeL | 2008-05-04 15:43:20 +0100 (Sun, 04 May 2008) | 1 line
Converted Preferences window to a dialog. Fixed spacing and naming in Preferences dialog. Split Preferences section of the Glade file into its own gtk_options.glade file. Moved all Preferences related files to gtk/dialogs.
........
r4127 | mikeL | 2008-05-05 20:45:44 +0100 (Mon, 05 May 2008) | 1 line
Fixed a glaring error in directory structure. All glade files are now stored in the res directory.
........
r4128 | mikeL | 2008-05-05 20:52:08 +0100 (Mon, 05 May 2008) | 1 line
Added directory 'dialogs' to revision control
........
r4129 | mikeL | 2008-05-05 20:58:53 +0100 (Mon, 05 May 2008) | 1 line
Cleaned up gtk folder by removing gtk_options.c and gtk_options.h (Moved to dialogs folder)
........
r4130 | mikeL | 2008-05-05 23:42:15 +0100 (Mon, 05 May 2008) | 1 line
Huzzah\! Preferences dialog is now instant apply\!
........
r4131 | mikeL | 2008-05-06 20:43:26 +0100 (Tue, 06 May 2008) | 1 line
nsgtk_reflow_all_windows() is now called whenever an option that requires redrawing of the current page is changed. Preferences dialog is now *completely* instant apply
........
r4132 | mikeL | 2008-05-06 20:55:05 +0100 (Tue, 06 May 2008) | 1 line
Renamed macros to better reflect their purpose
........
r4133 | mikeL | 2008-05-06 21:30:44 +0100 (Tue, 06 May 2008) | 1 line
Added gtk/res/options.glade to svn control
........
r4134 | mikeL | 2008-05-06 21:39:12 +0100 (Tue, 06 May 2008) | 1 line
Preferences window is now resizable
........
r4135 | mikeL | 2008-05-06 21:42:00 +0100 (Tue, 06 May 2008) | 1 line
Fixed reload menu item in popup menu
........
r4136 | mikeL | 2008-05-07 00:24:35 +0100 (Wed, 07 May 2008) | 1 line
Proxy configurations are now insensitive if proxy type is set to no proxy. Changing the home page with the Current Page button is now saved. Moved Current Page button in Preferences window and added Default Page.
........
r4137 | mikeL | 2008-05-07 00:41:29 +0100 (Wed, 07 May 2008) | 1 line
Solved a problem with all options being saved prematurely on initialization
........
r4145 | mikeL | 2008-05-11 18:07:06 +0100 (Sun, 11 May 2008) | 1 line
Optimized prefences window signal handling, entry signals are now caught on focus-out-event and checked for changes
........
r4146 | mikeL | 2008-05-11 18:16:04 +0100 (Sun, 11 May 2008) | 1 line
Fixed regression where 'Set Current Page' button stopped working
........
r4147 | mikeL | 2008-05-11 18:32:10 +0100 (Sun, 11 May 2008) | 1 line
Added 'Default Page' button functionality
........
r4148 | mikeL | 2008-05-11 18:33:03 +0100 (Sun, 11 May 2008) | 1 line
Updated TODO list
........
r4149 | mikeL | 2008-05-12 01:04:00 +0100 (Mon, 12 May 2008) | 1 line
Added 'Hide Advertisement' functionality
........
r4150 | mikeL | 2008-05-12 01:30:40 +0100 (Mon, 12 May 2008) | 1 line
Re-activated the option 'Disable Animation' and fixed bug where it would actually enable animation
........
r4151 | mikeL | 2008-05-12 01:42:43 +0100 (Mon, 12 May 2008) | 1 line
Added 'Send site referral information' functionality
........
r4152 | mikeL | 2008-05-12 02:01:00 +0100 (Mon, 12 May 2008) | 1 line
Added 'Disc cache age' functionality
........
r4154 | mikeL | 2008-05-13 20:35:29 +0100 (Tue, 13 May 2008) | 1 line
Fixed regression where preferences were not written to file in some cases and preferences dialog could not be reopened.
........
r4158 | mikeL | 2008-05-14 21:57:50 +0100 (Wed, 14 May 2008) | 1 line
Added 'preview' button to fonts tab of preferences dialog. nsgtk_reflow_all_windows is now called only when that button is clicked.
........
r4162 | mikeL | 2008-05-15 20:09:30 +0100 (Thu, 15 May 2008) | 1 line
Possible fix to segfault due to invalid test
........
r4163 | mikeL | 2008-05-15 20:25:28 +0100 (Thu, 15 May 2008) | 1 line
Fixed typo that caused preferences dialog to be unopenable
........
r4164 | mikeL | 2008-05-15 20:57:49 +0100 (Thu, 15 May 2008) | 1 line
Added initialization statement for preferences_dialog to make sure that
........
r4169 | mikeL | 2008-05-17 00:30:54 +0100 (Sat, 17 May 2008) | 1 line
Added a 'parent_window' initialization parameter to the preferences dialog so that it may center itself on parent. This also optimizes the closing code as destroy is now called on the dialog when the main window is closed
........
r4174 | mikeL | 2008-05-18 15:46:43 +0100 (Sun, 18 May 2008) | 1 line
Animation speed option is now insensitive when 'Disable animations' is true
........
r4175 | mikeL | 2008-05-18 15:50:06 +0100 (Sun, 18 May 2008) | 1 line
Added tooltip to preview button
........
r4176 | mikeL | 2008-05-18 16:04:05 +0100 (Sun, 18 May 2008) | 1 line
Animation speed is now always sensitive
........
r4177 | mikeL | 2008-05-19 02:32:21 +0100 (Mon, 19 May 2008) | 1 line
Redesigned about dialog as a GtkAboutDialog and removed the respective section from netsurf.glade (May need string revision)
........
r4178 | mikeL | 2008-05-19 02:36:15 +0100 (Mon, 19 May 2008) | 1 line
Fixed compile warning relating to improper cast of netsurf_version
........
r4180 | mikeL | 2008-05-19 21:42:04 +0100 (Mon, 19 May 2008) | 1 line
Updated credits
........
r4181 | mikeL | 2008-05-19 21:43:16 +0100 (Mon, 19 May 2008) | 1 line
launch_url is no longer static
........
r4182 | mikeL | 2008-05-19 21:50:03 +0100 (Mon, 19 May 2008) | 1 line
netsurf_version is now const in function which addresses jmb's concern
........
r4183 | mikeL | 2008-05-19 21:59:55 +0100 (Mon, 19 May 2008) | 1 line
Updated header
........
r4184 | mikeL | 2008-05-19 22:03:28 +0100 (Mon, 19 May 2008) | 1 line
local variables are now static
........
r4186 | mikeL | 2008-05-21 22:03:43 +0100 (Wed, 21 May 2008) | 1 line
Removed 'Main Development Team' from the beginning of all credits
........
r4219 | mikeL | 2008-05-28 18:17:12 +0100 (Wed, 28 May 2008) | 1 line
Removed wndOpenFile from glade file.
........
r4236 | mikeL | 2008-05-31 23:25:32 +0100 (Sat, 31 May 2008) | 1 line
Fixed proxy options for both riscos and gtk versions thanks to a tip from Leon Stringer
........
r4275 | mikeL | 2008-06-06 17:16:29 +0100 (Fri, 06 Jun 2008) | 1 line
Added support for dragging, clicks are now emited on button release
........
r4276 | mikeL | 2008-06-06 17:18:18 +0100 (Fri, 06 Jun 2008) | 1 line
gtk_window.c now sets the current_redraw_browser. Text selection now highlights properly
........
r4277 | mikeL | 2008-06-06 17:47:35 +0100 (Fri, 06 Jun 2008) | 1 line
Cleaned up the button detection code
........
r4278 | mikeL | 2008-06-06 17:58:51 +0100 (Fri, 06 Jun 2008) | 1 line
Removed leftover variables
........
r4279 | mikeL | 2008-06-06 18:13:58 +0100 (Fri, 06 Jun 2008) | 1 line
Enabled 'Select All'
........
r4280 | mikeL | 2008-06-06 18:31:56 +0100 (Fri, 06 Jun 2008) | 1 line
Fixed bug where mouse state would always remain as PRESS even if dragging
........
r4281 | mikeL | 2008-06-06 18:45:16 +0100 (Fri, 06 Jun 2008) | 1 line
Fixed regression where mouse state was cleared improperly
........
r4287 | mikeL | 2008-06-07 00:21:32 +0100 (Sat, 07 Jun 2008) | 1 line
Mouse code can now handle modifiers. Fixed bug where end of drag was not detected until mouse moved after being released. Improved mouse handling.
........
r4294 | mikeL | 2008-06-07 03:21:03 +0100 (Sat, 07 Jun 2008) | 1 line
Added ability to handle modifiers pressed during a drag event. Mouse movement handling now uses switch statements (Thanks tlsa)
........
r4295 | mikeL | 2008-06-07 03:40:11 +0100 (Sat, 07 Jun 2008) | 1 line
Fixed bug where modifier keys were being detected incorrectly
........
r4296 | mikeL | 2008-06-07 03:42:31 +0100 (Sat, 07 Jun 2008) | 1 line
Reversed accidental file modification
........
r4299 | mikeL | 2008-06-07 21:32:15 +0100 (Sat, 07 Jun 2008) | 1 line
Fixed inclusion of gtk_about source file instead of header
........
r4300 | mikeL | 2008-06-07 22:27:39 +0100 (Sat, 07 Jun 2008) | 1 line
Fixed problem with about.h not linking properly.
........
r4301 | mikeL | 2008-06-07 22:37:28 +0100 (Sat, 07 Jun 2008) | 1 line
Moved definitions of gui_window and browser_mouse to gtk_window.h. Moved all selection related functions to gtk_selection.c. Implemented copy functionality.
........
r4302 | mikeL | 2008-06-07 22:48:18 +0100 (Sat, 07 Jun 2008) | 1 line
Implemented paste functionality
........
r4303 | mikeL | 2008-06-07 22:48:40 +0100 (Sat, 07 Jun 2008) | 1 line
Removed old test case
........
r4304 | mikeL | 2008-06-07 22:53:56 +0100 (Sat, 07 Jun 2008) | 1 line
Fixed bug where click would be sent at the end of a button 2 drag (Thanks tlsa)
........
r4305 | mikeL | 2008-06-07 23:48:01 +0100 (Sat, 07 Jun 2008) | 1 line
Prevented gui_copy_to_clipboard from overwriting clipboard with a NULL string.
........
r4306 | mikeL | 2008-06-08 00:00:55 +0100 (Sun, 08 Jun 2008) | 1 line
Fixed regression where the state of the modifier keys would alternate as the mouse moved. Fixed bug that prevented drags from being registered if a modifier key was pressed
........
r4308 | mikeL | 2008-06-08 00:53:26 +0100 (Sun, 08 Jun 2008) | 1 line
Changed 'Select All' menu item to stock
........
r4314 | mikeL | 2008-06-09 19:09:23 +0100 (Mon, 09 Jun 2008) | 1 line
Added function nsgtk_scaffolding_set_sensitive
........
r4315 | mikeL | 2008-06-09 19:14:14 +0100 (Mon, 09 Jun 2008) | 1 line
Added function nsgtk_scaffolding_set_sensitive
........
r4316 | mikeL | 2008-06-09 19:20:16 +0100 (Mon, 09 Jun 2008) | 1 line
Removed option 'Use Cairo for anti-aliased rendering'
........
r4317 | mikeL | 2008-06-09 20:10:55 +0100 (Mon, 09 Jun 2008) | 1 line
Fixed bug where text would be selected in all windows instead of only the active one
........
r4318 | mikeL | 2008-06-09 20:29:42 +0100 (Mon, 09 Jun 2008) | 1 line
(Drastically) Improved redraw handling. (Thanks jmb)
........
r4319 | mikeL | 2008-06-09 21:10:17 +0100 (Mon, 09 Jun 2008) | 1 line
Removed old test case
........
r4320 | mikeL | 2008-06-10 07:27:32 +0100 (Tue, 10 Jun 2008) | 1 line
Implemented (almost) the rest of the clipboard functionality. Clipboard functions now handle the url bar correctly. Clipboard menu items are now appropriately sensitive.
........
r4321 | mikeL | 2008-06-10 19:08:05 +0100 (Tue, 10 Jun 2008) | 1 line
Added clipboard functions to contextual right click menu
........
r4323 | mikeL | 2008-06-10 23:57:43 +0100 (Tue, 10 Jun 2008) | 1 line
Fixed regression where modifiers key states would be cleared on button release. Modifier keys are now set only on button press and can only be removed during motion or upon release. Fixed spacing (thanks tlsa)
........
svn path=/trunk/netsurf/; revision=4326
2008-06-11 14:57:44 +04:00
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
/* start new selection drag */
|
|
|
|
selection_clear(s, true);
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_start(s, idx);
|
|
|
|
selection_set_end(s, idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
s->drag_state = DRAG_END;
|
|
|
|
|
|
|
|
gui_start_selection(s->bw->window);
|
|
|
|
}
|
|
|
|
else if (mouse & BROWSER_MOUSE_DRAG_2) {
|
|
|
|
|
|
|
|
/* adjust selection, but only if there is one */
|
|
|
|
if (!selection_defined(s))
|
|
|
|
return false; /* ignore Adjust drags */
|
|
|
|
|
2008-03-30 01:26:04 +03:00
|
|
|
if (pos >= 0) {
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_end(s, idx);
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
s->drag_state = DRAG_END;
|
|
|
|
}
|
|
|
|
else {
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_start(s, idx);
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
s->drag_state = DRAG_START;
|
|
|
|
}
|
|
|
|
gui_start_selection(s->bw->window);
|
|
|
|
}
|
2006-02-06 03:07:18 +03:00
|
|
|
else if (pos && (mouse & BROWSER_MOUSE_CLICK_1)) {
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
/* clear selection */
|
|
|
|
selection_clear(s, true);
|
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
}
|
|
|
|
else if (mouse & BROWSER_MOUSE_CLICK_2) {
|
|
|
|
|
|
|
|
/* ignore Adjust clicks when there's no selection */
|
|
|
|
if (!selection_defined(s))
|
|
|
|
return false;
|
|
|
|
|
2008-03-30 01:26:04 +03:00
|
|
|
if (pos >= 0)
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_end(s, idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
else
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_start(s, idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
s->drag_state = DRAG_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* not our problem */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this mouse click is selection-related */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles movements related to the selection, eg. dragging of start and
|
|
|
|
* end points.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param mouse state of mouse buttons and modifier keys
|
2006-02-16 02:09:55 +03:00
|
|
|
* \param idx byte offset within text representation
|
2005-04-15 09:51:32 +04:00
|
|
|
*/
|
|
|
|
|
2008-03-31 22:59:23 +04:00
|
|
|
void selection_track(struct selection *s, browser_mouse_state mouse,
|
|
|
|
unsigned idx)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2006-02-16 02:09:55 +03:00
|
|
|
if (!SAME_SPACE(s, idx))
|
2006-02-11 21:33:05 +03:00
|
|
|
return;
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
switch (s->drag_state) {
|
|
|
|
|
|
|
|
case DRAG_START:
|
2006-02-16 02:09:55 +03:00
|
|
|
if (idx > s->end_idx) {
|
2005-07-21 03:27:28 +04:00
|
|
|
unsigned old_end = s->end_idx;
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_end(s, idx);
|
|
|
|
selection_set_start(s, old_end);
|
2005-04-15 09:51:32 +04:00
|
|
|
s->drag_state = DRAG_END;
|
|
|
|
}
|
|
|
|
else
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_start(s, idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DRAG_END:
|
2006-02-16 02:09:55 +03:00
|
|
|
if (idx < s->start_idx) {
|
2005-07-21 03:27:28 +04:00
|
|
|
unsigned old_start = s->start_idx;
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_start(s, idx);
|
|
|
|
selection_set_end(s, old_start);
|
2005-04-15 09:51:32 +04:00
|
|
|
s->drag_state = DRAG_START;
|
|
|
|
}
|
|
|
|
else
|
2006-02-16 02:09:55 +03:00
|
|
|
selection_set_end(s, idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-24 10:13:25 +04:00
|
|
|
/**
|
|
|
|
* Tests whether a text box lies partially within the given range of
|
|
|
|
* byte offsets, returning the start and end indexes of the bytes
|
|
|
|
* that are enclosed.
|
|
|
|
*
|
|
|
|
* \param box box to be tested
|
|
|
|
* \param start_idx byte offset of start of range
|
|
|
|
* \param end_idx byte offset of end of range
|
|
|
|
* \param start_offset receives the start offset of the selected part
|
|
|
|
* \param end_offset receives the end offset of the selected part
|
|
|
|
* \return true iff the range encloses at least part of the box
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool selected_part(struct box *box, unsigned start_idx, unsigned end_idx,
|
|
|
|
unsigned *start_offset, unsigned *end_offset)
|
|
|
|
{
|
|
|
|
size_t box_length = box->length + box->space;
|
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
if (box_length > 0) {
|
|
|
|
if (box->byte_offset >= start_idx &&
|
|
|
|
box->byte_offset + box_length <= end_idx) {
|
2006-04-26 21:26:03 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
/* fully enclosed */
|
|
|
|
*start_offset = 0;
|
|
|
|
*end_offset = box_length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (box->byte_offset + box_length > start_idx &&
|
|
|
|
box->byte_offset < end_idx) {
|
|
|
|
/* partly enclosed */
|
|
|
|
int offset = 0;
|
|
|
|
int len;
|
2006-04-26 21:26:03 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
if (box->byte_offset < start_idx)
|
|
|
|
offset = start_idx - box->byte_offset;
|
2006-04-26 21:26:03 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
len = box_length - offset;
|
2006-04-26 21:26:03 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
if (box->byte_offset + box_length > end_idx)
|
|
|
|
len = end_idx - (box->byte_offset + offset);
|
2006-04-26 21:26:03 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
*start_offset = offset;
|
|
|
|
*end_offset = offset + len;
|
2006-04-26 21:26:03 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
return true;
|
|
|
|
}
|
2005-07-24 10:13:25 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
/**
|
|
|
|
* Traverse the given box subtree, calling the handler function (with its handle)
|
|
|
|
* for all boxes that lie (partially) within the given range
|
|
|
|
*
|
|
|
|
* \param box box subtree
|
|
|
|
* \param start_idx start of range within textual representation (bytes)
|
|
|
|
* \param end_idx end of range
|
2006-09-10 19:08:30 +04:00
|
|
|
* \param num_space number space of the selection
|
2005-04-15 09:51:32 +04:00
|
|
|
* \param handler handler function to call
|
|
|
|
* \param handle handle to pass
|
2008-03-31 22:04:36 +04:00
|
|
|
* \param before type of whitespace to place before next encountered text
|
|
|
|
* \param first whether this is the first box with text
|
2008-04-07 13:00:21 +04:00
|
|
|
* \param do_marker whether deal enter any marker box
|
2005-04-15 09:51:32 +04:00
|
|
|
* \return false iff traversal abandoned part-way through
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool traverse_tree(struct box *box, unsigned start_idx, unsigned end_idx,
|
2006-09-10 19:08:30 +04:00
|
|
|
unsigned int num_space, seln_traverse_handler handler,
|
2008-04-13 22:21:22 +04:00
|
|
|
void *handle, save_text_whitespace *before, bool *first,
|
2008-04-07 13:00:21 +04:00
|
|
|
bool do_marker)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
|
|
|
struct box *child;
|
2008-03-31 22:04:36 +04:00
|
|
|
const char *whitespace_text = "";
|
2008-03-31 22:59:23 +04:00
|
|
|
size_t whitespace_length = 0;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
assert(box);
|
2008-04-07 13:00:21 +04:00
|
|
|
|
|
|
|
/* If selection starts inside marker */
|
2008-06-04 02:18:15 +04:00
|
|
|
if (box->parent && box->parent->list_marker == box && !do_marker) {
|
2008-04-07 13:00:21 +04:00
|
|
|
/* set box to main list element */
|
|
|
|
box = box->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If box has a list marker */
|
|
|
|
if (box->list_marker) {
|
|
|
|
/* do the marker box before continuing with the rest of the
|
|
|
|
* list element */
|
|
|
|
if (!traverse_tree(box->list_marker, start_idx, end_idx,
|
|
|
|
num_space, handler, handle, before, first,
|
|
|
|
true))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we can prune this subtree, it's after the selection */
|
2005-04-15 09:51:32 +04:00
|
|
|
if (box->byte_offset >= end_idx)
|
|
|
|
return true;
|
|
|
|
|
2005-07-21 03:27:28 +04:00
|
|
|
/* read before calling the handler in case it modifies the tree */
|
|
|
|
child = box->children;
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
/* If nicely formatted output of the selected text is required, work
|
|
|
|
* out what whitespace should be placed before the next bit of text */
|
|
|
|
if (before) {
|
2008-04-13 22:21:22 +04:00
|
|
|
save_text_solve_whitespace(box, first, before, &whitespace_text,
|
|
|
|
&whitespace_length);
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-03-31 22:04:36 +04:00
|
|
|
whitespace_text = NULL;
|
|
|
|
}
|
2008-04-01 01:52:12 +04:00
|
|
|
if (num_space == NUMBER_SPACE(box->byte_offset) &&
|
2008-04-11 19:52:56 +04:00
|
|
|
box->type != BOX_BR &&
|
|
|
|
!((box->type == BOX_FLOAT_LEFT ||
|
|
|
|
box->type == BOX_FLOAT_RIGHT) &&
|
|
|
|
!box->text)) {
|
2008-03-31 22:04:36 +04:00
|
|
|
unsigned start_offset;
|
|
|
|
unsigned end_offset;
|
|
|
|
|
|
|
|
if (selected_part(box, start_idx, end_idx, &start_offset,
|
|
|
|
&end_offset)) {
|
|
|
|
if (!handler(box->text + start_offset, min(box->length,
|
|
|
|
end_offset) - start_offset,
|
2008-03-31 22:59:23 +04:00
|
|
|
box, handle, whitespace_text,
|
|
|
|
whitespace_length))
|
2005-04-15 09:51:32 +04:00
|
|
|
return false;
|
2008-03-31 22:04:36 +04:00
|
|
|
if (before) {
|
|
|
|
*first = false;
|
|
|
|
*before = WHITESPACE_NONE;
|
|
|
|
}
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the first child that could lie partially within the selection;
|
2008-03-31 22:04:36 +04:00
|
|
|
* this is important at the top-levels of the tree for pruning subtrees
|
|
|
|
* that lie entirely before the selection */
|
2005-04-15 09:51:32 +04:00
|
|
|
|
|
|
|
if (child) {
|
|
|
|
struct box *next = child->next;
|
|
|
|
|
|
|
|
while (next && next->byte_offset < start_idx) {
|
|
|
|
child = next;
|
|
|
|
next = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (child) {
|
2008-03-31 22:04:36 +04:00
|
|
|
/* read before calling the handler in case it modifies
|
|
|
|
* the tree */
|
2005-07-21 03:27:28 +04:00
|
|
|
struct box *next = child->next;
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
if (!traverse_tree(child, start_idx, end_idx, num_space,
|
2008-04-07 13:00:21 +04:00
|
|
|
handler, handle, before, first, false))
|
2005-04-15 09:51:32 +04:00
|
|
|
return false;
|
2005-07-21 03:27:28 +04:00
|
|
|
|
|
|
|
child = next;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-03-31 22:04:36 +04:00
|
|
|
* Traverse the current selection, calling the handler function (with its
|
|
|
|
* handle) for all boxes that lie (partially) within the given range
|
2005-04-15 09:51:32 +04:00
|
|
|
*
|
|
|
|
* \param handler handler function to call
|
|
|
|
* \param handle handle to pass
|
|
|
|
* \return false iff traversal abandoned part-way through
|
|
|
|
*/
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
bool selection_traverse(struct selection *s, seln_traverse_handler handler,
|
2008-03-31 22:59:23 +04:00
|
|
|
void *handle)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2006-02-16 02:09:55 +03:00
|
|
|
struct content *c;
|
2008-04-13 22:21:22 +04:00
|
|
|
save_text_whitespace before = WHITESPACE_NONE;
|
2008-03-31 22:04:36 +04:00
|
|
|
bool first = true;
|
2006-02-16 02:09:55 +03:00
|
|
|
|
|
|
|
if (!selection_defined(s))
|
|
|
|
return true; /* easy case, nothing to do */
|
|
|
|
|
|
|
|
if (s->root)
|
2008-03-31 22:04:36 +04:00
|
|
|
return traverse_tree(s->root, s->start_idx, s->end_idx,
|
|
|
|
NUMBER_SPACE(s->max_idx), handler, handle,
|
2008-04-07 13:00:21 +04:00
|
|
|
&before, &first, false);
|
2006-02-16 02:09:55 +03:00
|
|
|
|
|
|
|
c = s->bw->current_content;
|
|
|
|
if (!c) return true;
|
|
|
|
|
|
|
|
size_t length;
|
2008-03-31 22:04:36 +04:00
|
|
|
const char *text = textplain_get_raw_data(c, s->start_idx,
|
|
|
|
s->end_idx, &length);
|
2006-02-16 02:09:55 +03:00
|
|
|
|
2008-03-31 22:59:23 +04:00
|
|
|
if (text && !handler(text, length, NULL, handle, NULL, 0))
|
2006-02-16 02:09:55 +03:00
|
|
|
return false;
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selection traversal handler for redrawing the screen when the selection
|
|
|
|
* has been altered.
|
|
|
|
*
|
2008-03-31 22:59:23 +04:00
|
|
|
* \param text pointer to text string
|
|
|
|
* \param length length of text to be appended (bytes)
|
|
|
|
* \param box pointer to text box being (partially) added
|
|
|
|
* \param handle unused handle, we don't need one
|
|
|
|
* \param whitespace_text whitespace to place before text for formatting
|
|
|
|
* may be NULL
|
|
|
|
* \param whitespace_length length of whitespace_text
|
2005-04-15 09:51:32 +04:00
|
|
|
* \return true iff successful and traversal should continue
|
|
|
|
*/
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
bool redraw_handler(const char *text, size_t length, struct box *box,
|
2008-03-31 22:59:23 +04:00
|
|
|
void *handle, const char *whitespace_text,
|
|
|
|
size_t whitespace_length)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
|
|
|
if (box) {
|
|
|
|
struct rdw_info *r = (struct rdw_info*)handle;
|
2008-04-07 13:00:21 +04:00
|
|
|
int width, height, space_width;
|
2005-04-15 09:51:32 +04:00
|
|
|
int x, y;
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
/* \todo - it should be possible to reduce the redrawn area by
|
2008-03-31 22:04:36 +04:00
|
|
|
* considering the 'text', 'length' and 'space' parameters */
|
2005-04-15 09:51:32 +04:00
|
|
|
box_coords(box, &x, &y);
|
|
|
|
|
|
|
|
width = box->padding[LEFT] + box->width + box->padding[RIGHT];
|
|
|
|
height = box->padding[TOP] + box->height + box->padding[BOTTOM];
|
|
|
|
|
2008-04-07 13:00:21 +04:00
|
|
|
if (box->type == BOX_TEXT && box->space &&
|
|
|
|
nsfont_width(box->style, " ", 1, &space_width))
|
|
|
|
width += space_width;
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
if (r->inited) {
|
2006-02-16 02:09:55 +03:00
|
|
|
if (x < r->r.x0) r->r.x0 = x;
|
|
|
|
if (y < r->r.y0) r->r.y0 = y;
|
|
|
|
if (x + width > r->r.x1) r->r.x1 = x + width;
|
|
|
|
if (y + height > r->r.y1) r->r.y1 = y + height;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
r->inited = true;
|
2006-02-16 02:09:55 +03:00
|
|
|
r->r.x0 = x;
|
|
|
|
r->r.y0 = y;
|
|
|
|
r->r.x1 = x + width;
|
|
|
|
r->r.y1 = y + height;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redraws the given range of text.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param start_idx start offset (bytes) within the textual representation
|
|
|
|
* \param end_idx end offset (bytes) within the textual representation
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_redraw(struct selection *s, unsigned start_idx, unsigned end_idx)
|
|
|
|
{
|
|
|
|
struct rdw_info rdw;
|
|
|
|
|
|
|
|
assert(end_idx >= start_idx);
|
|
|
|
rdw.inited = false;
|
2006-02-16 02:09:55 +03:00
|
|
|
|
|
|
|
if (s->root) {
|
2008-03-31 22:04:36 +04:00
|
|
|
if (!traverse_tree(s->root, start_idx, end_idx,
|
|
|
|
NUMBER_SPACE(s->max_idx), redraw_handler, &rdw,
|
2008-04-07 13:00:21 +04:00
|
|
|
NULL, NULL, false))
|
2006-02-16 02:09:55 +03:00
|
|
|
return;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
2006-02-16 02:09:55 +03:00
|
|
|
else {
|
|
|
|
struct content *c = s->bw->current_content;
|
|
|
|
if (c && c->type == CONTENT_TEXTPLAIN && end_idx > start_idx) {
|
2008-03-31 22:04:36 +04:00
|
|
|
textplain_coords_from_range(c, start_idx,
|
|
|
|
end_idx, &rdw.r);
|
2006-02-16 02:09:55 +03:00
|
|
|
rdw.inited = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rdw.inited)
|
|
|
|
browser_window_redraw_rect(s->bw, rdw.r.x0, rdw.r.y0,
|
|
|
|
rdw.r.x1 - rdw.r.x0, rdw.r.y1 - rdw.r.y0);
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the current selection, optionally causing the screen to be updated.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param redraw true iff the previously selected region of the browser
|
|
|
|
* window should be redrawn
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_clear(struct selection *s, bool redraw)
|
|
|
|
{
|
|
|
|
int old_start, old_end;
|
|
|
|
bool was_defined;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
was_defined = selection_defined(s);
|
|
|
|
old_start = s->start_idx;
|
|
|
|
old_end = s->end_idx;
|
|
|
|
|
|
|
|
s->defined = false;
|
|
|
|
s->start_idx = 0;
|
|
|
|
s->end_idx = 0;
|
|
|
|
|
|
|
|
if (redraw && was_defined)
|
|
|
|
selection_redraw(s, old_start, old_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects all the text within the box subtree controlled by
|
|
|
|
* this selection object, updating the screen accordingly.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_select_all(struct selection *s)
|
|
|
|
{
|
2005-07-21 03:27:28 +04:00
|
|
|
unsigned old_start, old_end;
|
2005-04-15 09:51:32 +04:00
|
|
|
bool was_defined;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
was_defined = selection_defined(s);
|
|
|
|
old_start = s->start_idx;
|
|
|
|
old_end = s->end_idx;
|
|
|
|
|
|
|
|
s->defined = true;
|
|
|
|
s->start_idx = 0;
|
|
|
|
s->end_idx = s->max_idx;
|
|
|
|
|
|
|
|
if (was_defined) {
|
|
|
|
selection_redraw(s, 0, old_start);
|
|
|
|
selection_redraw(s, old_end, s->end_idx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
selection_redraw(s, 0, s->max_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
/**
|
|
|
|
* Set the start position of the current selection, updating the screen.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param offset byte offset within textual representation
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_set_start(struct selection *s, unsigned offset)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
|
|
|
bool was_defined = selection_defined(s);
|
2005-07-21 03:27:28 +04:00
|
|
|
unsigned old_start = s->start_idx;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2005-07-21 03:27:28 +04:00
|
|
|
s->start_idx = offset;
|
2005-04-15 09:51:32 +04:00
|
|
|
s->defined = (s->start_idx < s->end_idx);
|
|
|
|
|
|
|
|
if (was_defined) {
|
2005-07-21 03:27:28 +04:00
|
|
|
if (offset < old_start)
|
2005-04-15 09:51:32 +04:00
|
|
|
selection_redraw(s, s->start_idx, old_start);
|
|
|
|
else
|
|
|
|
selection_redraw(s, old_start, s->start_idx);
|
|
|
|
}
|
|
|
|
else if (selection_defined(s))
|
|
|
|
selection_redraw(s, s->start_idx, s->end_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
/**
|
|
|
|
* Set the end position of the current selection, updating the screen.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param offset byte offset within textual representation
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_set_end(struct selection *s, unsigned offset)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
|
|
|
bool was_defined = selection_defined(s);
|
2005-07-21 03:27:28 +04:00
|
|
|
unsigned old_end = s->end_idx;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2005-07-21 03:27:28 +04:00
|
|
|
s->end_idx = offset;
|
2005-04-15 09:51:32 +04:00
|
|
|
s->defined = (s->start_idx < s->end_idx);
|
|
|
|
|
|
|
|
if (was_defined) {
|
2005-07-21 03:27:28 +04:00
|
|
|
if (offset < old_end)
|
2005-04-15 09:51:32 +04:00
|
|
|
selection_redraw(s, s->end_idx, old_end);
|
|
|
|
else
|
|
|
|
selection_redraw(s, old_end, s->end_idx);
|
|
|
|
}
|
|
|
|
else if (selection_defined(s))
|
|
|
|
selection_redraw(s, s->start_idx, s->end_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-21 03:27:28 +04:00
|
|
|
/**
|
|
|
|
* Get the box and index of the specified byte offset within the
|
|
|
|
* textual representation.
|
|
|
|
*
|
|
|
|
* \param b root node of search
|
|
|
|
* \param offset byte offset within textual representation
|
|
|
|
* \param pidx receives byte index of selection start point within box
|
|
|
|
* \return ptr to box, or NULL if no selection defined
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct box *get_box(struct box *b, unsigned offset, int *pidx)
|
|
|
|
{
|
|
|
|
struct box *child = b->children;
|
|
|
|
|
2008-04-08 16:40:05 +04:00
|
|
|
if (b->text) {
|
2005-07-21 03:27:28 +04:00
|
|
|
|
|
|
|
if (offset >= b->byte_offset &&
|
|
|
|
offset < b->byte_offset + b->length + b->space) {
|
|
|
|
|
|
|
|
/* it's in this box */
|
|
|
|
*pidx = offset - b->byte_offset;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the first child that could contain this offset */
|
|
|
|
if (child) {
|
|
|
|
struct box *next = child->next;
|
|
|
|
while (next && next->byte_offset < offset) {
|
|
|
|
child = next;
|
|
|
|
next = child->next;
|
|
|
|
}
|
|
|
|
return get_box(child, offset, pidx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the box and index of the selection start, if defined.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param pidx receives byte index of selection start point within box
|
|
|
|
* \return ptr to box, or NULL if no selection defined
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct box *selection_get_start(struct selection *s, int *pidx)
|
|
|
|
{
|
|
|
|
return (s->defined ? get_box(s->root, s->start_idx, pidx) : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the box and index of the selection end, if defined.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param pidx receives byte index of selection end point within box
|
|
|
|
* \return ptr to box, or NULL if no selection defined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct box *selection_get_end(struct selection *s, int *pidx)
|
|
|
|
{
|
|
|
|
return (s->defined ? get_box(s->root, s->end_idx, pidx) : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-15 09:51:32 +04:00
|
|
|
/**
|
2006-02-16 02:09:55 +03:00
|
|
|
* Tests whether a text range lies partially within the selection, if there is
|
2005-04-15 09:51:32 +04:00
|
|
|
* a selection defined, returning the start and end indexes of the bytes
|
|
|
|
* that should be selected.
|
|
|
|
*
|
|
|
|
* \param s the selection object
|
2006-02-16 02:09:55 +03:00
|
|
|
* \param start byte offset of start of text
|
2005-04-15 09:51:32 +04:00
|
|
|
* \param start_idx receives the start index (in bytes) of the highlighted portion
|
|
|
|
* \param end_idx receives the end index (in bytes)
|
|
|
|
* \return true iff part of the given box lies within the selection
|
|
|
|
*/
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
bool selection_highlighted(struct selection *s, unsigned start, unsigned end,
|
2005-04-15 09:51:32 +04:00
|
|
|
unsigned *start_idx, unsigned *end_idx)
|
|
|
|
{
|
2005-04-17 07:30:35 +04:00
|
|
|
/* caller should have checked first for efficiency */
|
2005-07-24 10:13:25 +04:00
|
|
|
assert(s);
|
2005-04-17 07:30:35 +04:00
|
|
|
assert(selection_defined(s));
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
if (end <= s->start_idx || start >= s->end_idx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*start_idx = (s->start_idx >= start) ? (s->start_idx - start) : 0;
|
|
|
|
*end_idx = min(end, s->end_idx) - start;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// assert(box);
|
|
|
|
// assert(IS_TEXT(box));
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
// return selected_part(box, s->start_idx, s->end_idx, start_idx, end_idx);
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selection traversal handler for saving the text to a file.
|
|
|
|
*
|
2008-03-31 22:59:23 +04:00
|
|
|
* \param text pointer to text being added, or NULL for newline
|
|
|
|
* \param length length of text to be appended (bytes)
|
|
|
|
* \param box pointer to text box (or NULL for textplain content)
|
|
|
|
* \param handle our save_state workspace pointer
|
|
|
|
* \param whitespace_text whitespace to place before text for formatting
|
|
|
|
* may be NULL
|
|
|
|
* \param whitespace_length length of whitespace_text
|
2005-04-15 09:51:32 +04:00
|
|
|
* \return true iff the file writing succeeded and traversal should continue.
|
|
|
|
*/
|
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
bool save_handler(const char *text, size_t length, struct box *box,
|
2008-03-31 22:59:23 +04:00
|
|
|
void *handle, const char *whitespace_text,
|
|
|
|
size_t whitespace_length)
|
2005-04-15 09:51:32 +04:00
|
|
|
{
|
2008-04-13 22:21:22 +04:00
|
|
|
struct save_text_state *sv = handle;
|
2005-09-01 07:20:21 +04:00
|
|
|
size_t new_length;
|
2008-03-31 22:04:36 +04:00
|
|
|
int space = 0;
|
2005-09-01 07:20:21 +04:00
|
|
|
|
|
|
|
assert(sv);
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
if (box->space > 0)
|
|
|
|
space = 1;
|
|
|
|
|
2008-03-31 22:59:23 +04:00
|
|
|
if (whitespace_text)
|
2008-03-31 22:04:36 +04:00
|
|
|
length += whitespace_length;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
new_length = sv->length + whitespace_length + length + space;
|
2005-09-01 07:20:21 +04:00
|
|
|
if (new_length >= sv->alloc) {
|
|
|
|
size_t new_alloc = sv->alloc + (sv->alloc / 4);
|
|
|
|
char *new_block;
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
if (new_alloc < new_length) new_alloc = new_length;
|
|
|
|
|
|
|
|
new_block = realloc(sv->block, new_alloc);
|
|
|
|
if (!new_block) return false;
|
|
|
|
|
|
|
|
sv->block = new_block;
|
|
|
|
sv->alloc = new_alloc;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
2008-03-31 22:04:36 +04:00
|
|
|
if (whitespace_text) {
|
|
|
|
memcpy(sv->block + sv->length, whitespace_text,
|
|
|
|
whitespace_length);
|
|
|
|
}
|
|
|
|
memcpy(sv->block + sv->length + whitespace_length, text, length);
|
2006-02-16 02:09:55 +03:00
|
|
|
sv->length += length;
|
2005-09-01 07:20:21 +04:00
|
|
|
|
2008-03-31 22:04:36 +04:00
|
|
|
if (space == 1)
|
2005-09-01 07:20:21 +04:00
|
|
|
sv->block[sv->length++] = ' ';
|
|
|
|
|
|
|
|
return true;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the given selection to a file.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param path pathname to be used
|
|
|
|
* \return true iff the save succeeded
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool selection_save_text(struct selection *s, const char *path)
|
|
|
|
{
|
2006-02-16 02:09:55 +03:00
|
|
|
struct content *c = s->bw->current_content;
|
2008-04-13 22:21:22 +04:00
|
|
|
struct save_text_state sv = { NULL, 0, 0 };
|
2005-09-01 07:20:21 +04:00
|
|
|
utf8_convert_ret ret;
|
|
|
|
char *result;
|
|
|
|
FILE *out;
|
|
|
|
|
2006-02-16 02:09:55 +03:00
|
|
|
assert(c);
|
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
if (!selection_traverse(s, save_handler, &sv)) {
|
|
|
|
free(sv.block);
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2006-04-26 21:26:03 +04:00
|
|
|
if (!sv.block)
|
|
|
|
return false;
|
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
ret = utf8_to_local_encoding(sv.block, sv.length, &result);
|
|
|
|
free(sv.block);
|
2005-04-15 09:51:32 +04:00
|
|
|
|
2005-09-01 07:20:21 +04:00
|
|
|
if (ret != UTF8_CONVERT_OK) {
|
|
|
|
LOG(("failed to convert to local encoding, return %d", ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = fopen(path, "w");
|
|
|
|
if (out) {
|
|
|
|
int res = fputs(result, out);
|
2008-04-13 22:21:22 +04:00
|
|
|
res = fputs("\n", out);
|
2005-09-01 07:20:21 +04:00
|
|
|
fclose(out);
|
2008-04-13 22:21:22 +04:00
|
|
|
free(result);
|
2005-09-01 07:20:21 +04:00
|
|
|
return (res != EOF);
|
|
|
|
}
|
2008-04-13 22:21:22 +04:00
|
|
|
free(result);
|
2005-09-01 07:20:21 +04:00
|
|
|
|
|
|
|
return false;
|
2005-04-15 09:51:32 +04:00
|
|
|
}
|
2006-02-06 03:07:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the selection to reflect a change in the selected text,
|
|
|
|
* eg. editing in a text area/input field.
|
|
|
|
*
|
|
|
|
* \param s selection object
|
|
|
|
* \param byte_offset byte offset of insertion/removal point
|
|
|
|
* \param change byte size of change, +ve = insertion, -ve = removal
|
|
|
|
* \param redraw true iff the screen should be updated
|
|
|
|
*/
|
|
|
|
|
|
|
|
void selection_update(struct selection *s, size_t byte_offset,
|
|
|
|
int change, bool redraw)
|
|
|
|
{
|
|
|
|
if (selection_defined(s) &&
|
|
|
|
byte_offset >= s->start_idx &&
|
|
|
|
byte_offset < s->end_idx)
|
|
|
|
{
|
|
|
|
if (change > 0)
|
|
|
|
s->end_idx += change;
|
|
|
|
else
|
2006-09-11 04:47:46 +04:00
|
|
|
s->end_idx +=
|
|
|
|
max(change, (int)(byte_offset - s->end_idx));
|
2006-02-06 03:07:18 +03:00
|
|
|
}
|
|
|
|
}
|