summaryrefslogtreecommitdiff
path: root/desktop/selection.c
blob: 332e9dafa1a176fbf955e1cb0a6b91e7aa381ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
 * Copyright 2008 Michael Drake <tlsa@netsurf-browser.org>
 *
 * 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/>.
 */

/**
 * \file
 * implementation of text selection within browser windows.
 */

#include <stdlib.h>
#include <string.h>

#include "netsurf/clipboard.h"
#include "netsurf/browser_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/selection.h"


struct selection_string {
	char *buffer;
	size_t buffer_len;
	size_t length;

	int n_styles;
	nsclipboard_styles *styles;
};


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

/**
 * 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
 */
static nserror
selection_redraw(struct selection *s, unsigned start_idx, unsigned end_idx)
{
	nserror res;

	if (s->c->handler->textselection_redraw != NULL) {
		res = s->c->handler->textselection_redraw(s->c,
							  start_idx,
							  end_idx);
	} else {
		res = NSERROR_NOT_IMPLEMENTED;
	}

	return res;
}


/**
 * 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)
{
	bool was_defined;
	unsigned old_end;

	old_end = s->end_idx;
	s->end_idx = offset;

	was_defined = s->defined;
	s->defined = (s->start_idx < s->end_idx);

	if (was_defined) {
		if (offset < old_end) {
			selection_redraw(s, s->end_idx, old_end);
		} else {
			selection_redraw(s, old_end, s->end_idx);
		}
	} else if (s->defined) {
		selection_redraw(s, s->start_idx, s->end_idx);
	}
}


/**
 * Traverse the current selection, calling the handler function (with its
 * handle) for all boxes that lie (partially) within the given range
 *
 * \param s The selection context.
 * \param handler handler function to call
 * \param handle  handle to pass
 * \return false iff traversal abandoned part-way through
 */
static bool
selection_copy(struct selection *s, struct selection_string *selstr)
{
	nserror res;

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

	if (res != NSERROR_OK) {
		return false;
	}
	return true;
}


/**
 * Append text to selection string.
 *
 * \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
 * \return true iff successful
 */
bool
selection_string_append(const char *text,
			size_t length,
			bool space,
			plot_font_style_t *style,
			struct selection_string *sel_string)
{
	size_t new_length = sel_string->length + length + (space ? 1 : 0) + 1;

	if (style != NULL) {
		/* Add text run style */
		nsclipboard_styles *new_styles;

		if (sel_string->n_styles == 0) {
			assert(sel_string->length == 0);
		}

		new_styles = realloc(sel_string->styles,
				     (sel_string->n_styles + 1) *
				     sizeof(nsclipboard_styles));
		if (new_styles == NULL) {
			return false;
		}

		sel_string->styles = new_styles;

		sel_string->styles[sel_string->n_styles].style = *style;
		sel_string->styles[sel_string->n_styles].start =
			sel_string->length;

		sel_string->n_styles++;
	}

	if (new_length > sel_string->buffer_len) {
		/* Need to extend buffer */
		size_t new_alloc = new_length + (new_length / 4);
		char *new_buff;

		new_buff = realloc(sel_string->buffer, new_alloc);
		if (new_buff == NULL) {
			return false;
		}

		sel_string->buffer = new_buff;
		sel_string->buffer_len = new_alloc;
	}

	/* Copy text onto end of existing text in buffer */
	memcpy(sel_string->buffer + sel_string->length, text, length);
	sel_string->length += length;

	if (space) {
		sel_string->buffer[sel_string->length++] = ' ';
	}

	/* Ensure NULL termination */
	sel_string->buffer[sel_string->length] = '\0';

	return true;
}


/* exported interface documented in desktop/selection.h */
struct selection *selection_create(struct content *c)
{
	struct selection *sel;
	sel = calloc(1, sizeof(struct selection));
	if (sel) {
		sel->c = c;
		sel->root = NULL;
		sel->drag_state = DRAG_NONE;
		sel->max_idx = 0;
		selection_clear(sel, false);
	}

	return sel;
}


/* 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 */
void selection_reinit(struct selection *s)
{
	s->max_idx = 0;

	if (s->c->handler->textselection_get_end != NULL) {
		s->c->handler->textselection_get_end(s->c, &s->max_idx);
	}

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


/* exported interface documented in desktop/selection.h */
void selection_init(struct selection *s)
{
	if (s->defined) {
		selection_clear(s, true);
	}

	s->defined = false;
	s->start_idx = 0;
	s->end_idx = 0;
	s->drag_state = DRAG_NONE;

	selection_reinit(s);
}


/* exported interface documented in desktop/selection.h */
bool
selection_click(struct selection *s,
		struct browser_window *top,
		browser_mouse_state mouse,
		unsigned idx)
{
	browser_mouse_state modkeys;
	int pos = -1;  /* 0 = inside selection, 1 = after it */

	modkeys = (mouse & (BROWSER_MOUSE_MOD_1 | BROWSER_MOUSE_MOD_2));

	top = browser_window_get_root(top);

	if (s->defined) {
		if (idx > s->start_idx) {
			if (idx <= s->end_idx) {
				pos = 0;
			} else {
				pos = 1;
			}
		}
	}

	if (!pos &&
	    ((mouse & BROWSER_MOUSE_DRAG_1) ||
	     (modkeys && (mouse & BROWSER_MOUSE_DRAG_2)))) {
		/* drag-saving selection */
		char *sel = selection_get_copy(s);
		guit->window->drag_save_selection(top->window, sel);
		free(sel);
	} else if (!modkeys) {
		if (pos && (mouse & BROWSER_MOUSE_PRESS_1)) {
			/* Clear the selection if mouse is pressed
			 * outside the selection, Otherwise clear on
			 * release (to allow for drags)
			 */

			selection_clear(s, true);

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

			guit->window->event(top->window,
					    GW_EVENT_START_SELECTION);

		} else if (mouse & BROWSER_MOUSE_DRAG_2) {

			/* adjust selection, but only if there is one */
			if (!s->defined) {
				return false;	/* ignore Adjust drags */
			}

			if (pos >= 0) {
				selection_set_end(s, idx);

				s->drag_state = DRAG_END;
			} else {
				selection_set_start(s, idx);

				s->drag_state = DRAG_START;
			}

			guit->window->event(top->window,
					    GW_EVENT_START_SELECTION);

		} else if (mouse & BROWSER_MOUSE_CLICK_2) {

			/* ignore Adjust clicks when there's no selection */
			if (!s->defined) {
				return false;
			}

			if (pos >= 0) {
				selection_set_end(s, idx);
			} else {
				selection_set_start(s, idx);
			}
			s->drag_state = DRAG_NONE;

		} else {
			return false;
		}

	} else {
		/* 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) {

	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;

	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;

	default:
		break;
	}
}


/* exported interface documented in desktop/selection.h */
char *selection_get_copy(struct selection *s)
{
	struct selection_string sel_string = {
		.buffer = NULL,
		.buffer_len = 0,
		.length = 0,

		.n_styles = 0,
		.styles = NULL
	};

	if (s == NULL || !s->defined)
		return NULL;

	if (!selection_copy(s, &sel_string)) {
		free(sel_string.buffer);
		free(sel_string.styles);
		return NULL;
	}

	free(sel_string.styles);

	return sel_string.buffer;
}


/* exported interface documented in desktop/selection.h */
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
	};

	if (s == NULL || !s->defined) {
		return false;
	}

	if (!selection_copy(s, &sel_string)) {
		free(sel_string.buffer);
		free(sel_string.styles);
		return false;
	}

	guit->clipboard->set(sel_string.buffer,
			     sel_string.length,
			     sel_string.styles,
			     sel_string.n_styles);

	free(sel_string.buffer);
	free(sel_string.styles);

	return true;
}


/* exported interface documented in desktop/selection.h */
bool selection_clear(struct selection *s, bool redraw)
{
	int old_start, old_end;
	bool was_defined;

	assert(s);

	was_defined = s->defined;
	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);
	}

	return was_defined;
}


/* exported interface documented in desktop/selection.h */
void selection_select_all(struct selection *s)
{
	assert(s);
	s->defined = true;

	selection_set_start(s, 0);
	selection_set_end(s, s->max_idx);
}


/* exported interface documented in desktop/selection.h */
void selection_set_position(struct selection *s, unsigned start, unsigned end)
{
	selection_set_start(s, start);
	selection_set_end(s, end);
}


/* exported interface documented in desktop/selection.h */
bool
selection_highlighted(const struct selection *s,
		      unsigned start,
		      unsigned end,
		      unsigned *start_idx,
		      unsigned *end_idx)
{
	assert(s);

	if (!s->defined) {
		return false;
	}

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

/* 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;
}