summaryrefslogtreecommitdiff
path: root/gtksourceview/gtksourcegutterlines.c
blob: 0194fc4985b1a4ac1352a952c18f49c93f36e5ed (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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
 * This file is part of GtkSourceView
 *
 * Copyright 2019 - Christian Hergert <chergert@redhat.com>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtksourcegutterlines.h"
#include "gtksourcegutterlines-private.h"

#include "quarkset-inline.h"

/**
 * GtkSourceGutterLines:
 *
 * Collected information about visible lines.
 *
 * The `GtkSourceGutterLines` object is used to collect information about
 * visible lines.
 *
 * Use this from your [signal@GutterRenderer::query-data] to collect the
 * necessary information on visible lines. Doing so reduces the number of
 * passes through the text btree allowing GtkSourceView to reach more
 * frames-per-second while performing kinetic scrolling.
 */

struct _GtkSourceGutterLines
{
	GObject       parent_instance;
	GtkTextView  *view;
	GArray       *lines;
	GdkRectangle  visible_rect;
	guint         first;
	guint         last;
	guint         cursor_line;
};

typedef struct
{
	QuarkSet classes;
	gint     y;
	gint     height;
	gint     first_height;
	gint     last_height;
} LineInfo;

G_DEFINE_TYPE (GtkSourceGutterLines, gtk_source_gutter_lines, G_TYPE_OBJECT)

static GQuark q_cursor_line;
static GQuark q_prelit;
static GQuark q_selected;

static void
gtk_source_gutter_lines_finalize (GObject *object)
{
	GtkSourceGutterLines *lines = (GtkSourceGutterLines *)object;

	g_clear_pointer (&lines->lines, g_array_unref);
	g_clear_object (&lines->view);

	G_OBJECT_CLASS (gtk_source_gutter_lines_parent_class)->finalize (object);
}

static void
gtk_source_gutter_lines_class_init (GtkSourceGutterLinesClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = gtk_source_gutter_lines_finalize;

	q_cursor_line = g_quark_from_static_string ("cursor-line");
	q_prelit = g_quark_from_static_string ("prelit");
	q_selected = g_quark_from_static_string ("selected");
}

static void
gtk_source_gutter_lines_init (GtkSourceGutterLines *self)
{
	self->cursor_line = -1;
}

static void
clear_line_info (gpointer data)
{
	LineInfo *info = data;

	info->y = 0;
	info->height = 0;
	quark_set_clear (&info->classes);
}

GtkSourceGutterLines *
_gtk_source_gutter_lines_new (GtkTextView       *text_view,
                              const GtkTextIter *begin,
                              const GtkTextIter *end,
                              gboolean           needs_wrap_first,
                              gboolean           needs_wrap_last)
{
	GtkSourceGutterLines *lines;
	GtkTextBuffer *buffer;
	GtkTextMark *mark;
	GtkTextIter iter;
	GtkTextIter sel_begin, sel_end;
	gboolean single_line;
	guint cursor_line;
	guint i;
	int first_selected = -1;
	int last_selected = -1;

	g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
	g_return_val_if_fail (begin != NULL, NULL);
	g_return_val_if_fail (end != NULL, NULL);
	g_return_val_if_fail (begin != end, NULL);

	buffer = gtk_text_view_get_buffer (text_view);

	g_return_val_if_fail (gtk_text_iter_get_buffer (begin) == buffer, NULL);
	g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);

	if (gtk_text_buffer_get_selection_bounds (buffer, &sel_begin, &sel_end))
	{
		gtk_text_iter_order (&sel_begin, &sel_end);
		first_selected = gtk_text_iter_get_line (&sel_begin);
		last_selected = gtk_text_iter_get_line (&sel_end);
	}

	if (gtk_text_iter_compare (begin, end) > 0)
	{
		const GtkTextIter *tmp = begin;
		begin = end;
		end = tmp;
	}

	g_return_val_if_fail (begin != end, NULL);

	lines = g_object_new (GTK_SOURCE_TYPE_GUTTER_LINES, NULL);
	lines->view = g_object_ref (text_view);
	lines->first = gtk_text_iter_get_line (begin);
	lines->last = gtk_text_iter_get_line (end);
	lines->lines = g_array_sized_new (FALSE,
	                                  FALSE,
	                                  sizeof (LineInfo),
	                                  lines->last - lines->first + 1);
	g_array_set_clear_func (lines->lines, clear_line_info);
	gtk_text_view_get_visible_rect (text_view, &lines->visible_rect);

	/* No need to calculate special wrapping if wrap mode is none */
	if (gtk_text_view_get_wrap_mode (text_view) == GTK_WRAP_NONE)
	{
		needs_wrap_first = FALSE;
		needs_wrap_last = FALSE;
	}

	single_line = !needs_wrap_first && !needs_wrap_last;

	/* Get the line number containing the cursor to compare while
	 * building the lines to add the "cursor-line" quark.
	 */
	mark = gtk_text_buffer_get_insert (buffer);
	gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
	cursor_line = gtk_text_iter_get_line (&iter);

	lines->cursor_line = cursor_line;

	iter = *begin;

	if (!gtk_text_iter_starts_line (&iter))
	{
		gtk_text_iter_set_line_offset (&iter, 0);
	}

	for (i = lines->first; i <= lines->last; i++)
	{
		LineInfo info = {0};

		if G_LIKELY (single_line)
		{
			/* Need to use yrange so that line-height can be taken
			 * into account.
			 */
			gtk_text_view_get_line_yrange (text_view, &iter, &info.y, &info.height);

			info.first_height = info.height;
			info.last_height = info.height;
		}
		else
		{
			gtk_text_view_get_line_yrange (text_view,
			                               &iter,
			                               &info.y,
			                               &info.height);

			if (gtk_text_iter_starts_line (&iter) &&
			    gtk_text_iter_ends_line (&iter))
			{
				info.first_height = info.height;
				info.last_height = info.height;
			}
			else
			{
				GdkRectangle rect;

				if (needs_wrap_first)
				{
					gtk_text_view_get_iter_location (text_view, &iter, &rect);

					/* Try to somewhat handle line-height correctly */
					info.first_height = ((rect.y - info.y) * 2) + rect.height;
				}
				else
				{
					info.first_height = info.height;
				}

				if (needs_wrap_last)
				{
					gtk_text_iter_forward_to_line_end (&iter);

					/* Prefer the character right before \n to get
					 * more accurate rectangle sizing.
					 */
					if (!gtk_text_iter_starts_line (&iter))
					{
						gtk_text_iter_backward_char (&iter);
						gtk_text_view_get_iter_location (text_view, &iter, &rect);
						gtk_text_iter_forward_char (&iter);
					}
					else
					{
						gtk_text_view_get_iter_location (text_view, &iter, &rect);
					}

					/* Try to somewhat handle line-height correctly */
					info.last_height = ((info.y + info.height) - (rect.y + rect.height)) * 2 + rect.height;
				}
				else
				{
					info.last_height = info.first_height;
				}
			}
		}

		if G_UNLIKELY (i == cursor_line)
		{
			quark_set_add (&info.classes, q_cursor_line);
		}

		if G_UNLIKELY (i >= first_selected && i <= last_selected)
		{
			quark_set_add (&info.classes, q_selected);
		}

		g_array_append_val (lines->lines, info);

		if G_UNLIKELY (!gtk_text_iter_forward_line (&iter) &&
			       !gtk_text_iter_is_end (&iter))
		{
			break;
		}
	}

	g_return_val_if_fail (lines->lines->len > 0, NULL);
	g_return_val_if_fail ((lines->last - lines->first) >= (lines->lines->len - 1), NULL);

	return g_steal_pointer (&lines);
}

/**
 * gtk_source_gutter_lines_add_qclass:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @qname: a class name as a #GQuark
 *
 * Adds the class denoted by @qname to @line.
 *
 * You may check if a line has @qname by calling
 * [method@GutterLines.has_qclass].
 *
 * You can remove @qname by calling
 * [method@GutterLines.remove_qclass].
 */
void
gtk_source_gutter_lines_add_qclass (GtkSourceGutterLines *lines,
                                    guint                 line,
                                    GQuark                qname)
{
	LineInfo *info;

	g_return_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines));
	g_return_if_fail (qname != 0);
	g_return_if_fail (line >= lines->first);
	g_return_if_fail (line <= lines->last);
	g_return_if_fail (line - lines->first < lines->lines->len);

	info = &g_array_index (lines->lines, LineInfo, line - lines->first);
	quark_set_add (&info->classes, qname);
}

/**
 * gtk_source_gutter_lines_add_class:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @name: a class name
 *
 * Adds the class @name to @line.
 *
 * @name will be converted to a [alias@GLib.Quark] as part of this process. A
 * faster version of this function is available via
 * [method@GutterLines.add_qclass] for situations where the [alias@GLib.Quark] is
 * known ahead of time.
 */
void
gtk_source_gutter_lines_add_class (GtkSourceGutterLines *lines,
                                   guint                 line,
                                   const gchar          *name)
{
	g_return_if_fail (name != NULL);

	gtk_source_gutter_lines_add_qclass (lines,
	                             line,
	                             g_quark_from_string (name));
}

/**
 * gtk_source_gutter_lines_remove_class:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @name: a class name
 *
 * Removes the class matching @name from @line.
 *
 * A faster version of this function is available via
 * [method@GutterLines.remove_qclass] for situations where the
 * #GQuark is known ahead of time.
 */
void
gtk_source_gutter_lines_remove_class (GtkSourceGutterLines *lines,
                                      guint                 line,
                                      const gchar          *name)
{
	GQuark qname;

	g_return_if_fail (name != NULL);

	qname = g_quark_try_string (name);

	if (qname != 0)
	{
		gtk_source_gutter_lines_remove_qclass (lines, line, qname);
	}
}

/**
 * gtk_source_gutter_lines_remove_qclass:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @qname: a #GQuark to remove from @line
 *
 * Reverses a call to [method@GutterLines.add_qclass] by removing
 * the [alias@GLib.Quark] matching @qname.
 */
void
gtk_source_gutter_lines_remove_qclass (GtkSourceGutterLines *lines,
                                       guint                 line,
                                       GQuark                qname)
{
	LineInfo *info;

	g_return_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines));
	g_return_if_fail (qname != 0);
	g_return_if_fail (line >= lines->first);
	g_return_if_fail (line <= lines->last);
	g_return_if_fail (line - lines->first < lines->lines->len);

	info = &g_array_index (lines->lines, LineInfo, line - lines->first);
	quark_set_remove (&info->classes, qname);
}

/**
 * gtk_source_gutter_lines_has_class:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @name: a class name that may be converted, to a #GQuark
 *
 * Checks to see if [method@GutterLines.add_class] was called with
 * the @name for @line.
 *
 * A faster version of this function is provided via
 * [method@GutterLines.has_qclass] for situations where the quark
 * is known ahead of time.
 *
 * Returns: %TRUE if @line contains @name
 */
gboolean
gtk_source_gutter_lines_has_class (GtkSourceGutterLines *lines,
                                   guint                 line,
                                   const gchar          *name)
{
	GQuark qname;

	g_return_val_if_fail (name != NULL, FALSE);

	qname = g_quark_try_string (name);

	if (qname == 0)
	{
		return FALSE;
	}

	return gtk_source_gutter_lines_has_qclass (lines, line, qname);
}

/**
 * gtk_source_gutter_lines_has_qclass:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @qname: a #GQuark containing the class name
 *
 * Checks to see if [method@GutterLines.add_qclass] was called with
 * the quark denoted by @qname for @line.
 *
 * Returns: %TRUE if @line contains @qname
 */
gboolean
gtk_source_gutter_lines_has_qclass (GtkSourceGutterLines *lines,
                                    guint                 line,
                                    GQuark                qname)
{
	LineInfo *info;

	g_return_val_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines), FALSE);
	g_return_val_if_fail (qname != 0, FALSE);
	g_return_val_if_fail (line >= lines->first, FALSE);
	g_return_val_if_fail (line <= lines->last, FALSE);
	g_return_val_if_fail (line - lines->first < lines->lines->len, FALSE);

	info = &g_array_index (lines->lines, LineInfo, line - lines->first);

	return quark_set_contains (&info->classes, qname);
}

/**
 * gtk_source_gutter_lines_is_cursor:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 *
 * Checks to see if @line contains the insertion cursor.
 *
 * Returns: %TRUE if the insertion cursor is on @line
 */
gboolean
gtk_source_gutter_lines_is_cursor (GtkSourceGutterLines *lines,
                                   guint                 line)
{
	return line == lines->cursor_line ||
	       gtk_source_gutter_lines_has_qclass (lines, line, q_cursor_line);
}

/**
 * gtk_source_gutter_lines_is_prelit:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 *
 * Checks to see if @line is marked as prelit. Generally, this means
 * the mouse pointer is over the line within the gutter.
 *
 * Returns: %TRUE if the line is prelit
 */
gboolean
gtk_source_gutter_lines_is_prelit (GtkSourceGutterLines *lines,
                                   guint                 line)
{
	return gtk_source_gutter_lines_has_qclass (lines, line, q_prelit);
}

/**
 * gtk_source_gutter_lines_is_selected:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 *
 * Checks to see if the view had a selection and if that selection overlaps
 * @line in some way.
 *
 * Returns: %TRUE if the line contains a selection
 */
gboolean
gtk_source_gutter_lines_is_selected (GtkSourceGutterLines *lines,
                                     guint                 line)
{
	return gtk_source_gutter_lines_has_qclass (lines, line, q_selected);
}

/**
 * gtk_source_gutter_lines_get_first:
 * @lines: a #GtkSourceGutterLines
 *
 * Gets the line number (starting from 0) for the first line that is
 * user visible.
 *
 * Returns: a line number starting from 0
 */
guint
gtk_source_gutter_lines_get_first (GtkSourceGutterLines *lines)
{
	g_return_val_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines), 0);

	return lines->first;
}

/**
 * gtk_source_gutter_lines_get_last:
 * @lines: a #GtkSourceGutterLines
 *
 * Gets the line number (starting from 0) for the last line that is
 * user visible.
 *
 * Returns: a line number starting from 0
 */
guint
gtk_source_gutter_lines_get_last (GtkSourceGutterLines *lines)
{
	g_return_val_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines), 0);

	return lines->last;
}

/**
 * gtk_source_gutter_lines_get_iter_at_line:
 * @lines: a #GtkSourceGutterLines
 * @iter: (out): a location for a #GtkTextIter
 * @line: the line number
 *
 * Gets a #GtkTextIter for the current buffer at @line
 */
void
gtk_source_gutter_lines_get_iter_at_line (GtkSourceGutterLines *lines,
                                          GtkTextIter          *iter,
                                          guint                 line)
{
	GtkTextBuffer *buffer;

	g_return_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines));
	g_return_if_fail (iter != NULL);

	buffer = gtk_text_view_get_buffer (lines->view);
	gtk_text_buffer_get_iter_at_line (buffer, iter, line);
}

/**
 * gtk_source_gutter_lines_get_view:
 * @lines: a #GtkSourceGutterLines
 *
 * Gets the [class@Gtk.TextView] that the `GtkSourceGutterLines` represents.
 *
 * Returns: (transfer none) (not nullable): a #GtkTextView
 */
GtkTextView *
gtk_source_gutter_lines_get_view (GtkSourceGutterLines *lines)
{
	g_return_val_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines), NULL);

	return lines->view;
}

/**
 * gtk_source_gutter_lines_get_buffer:
 * @lines: a #GtkSourceGutterLines
 *
 * Gets the [class@Gtk.TextBuffer] that the `GtkSourceGutterLines` represents.
 *
 * Returns: (transfer none) (not nullable): a #GtkTextBuffer
 */
GtkTextBuffer *
gtk_source_gutter_lines_get_buffer (GtkSourceGutterLines *lines)
{
	g_return_val_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines), NULL);

	return gtk_text_view_get_buffer (lines->view);
}

/**
 * gtk_source_gutter_lines_get_line_yrange:
 * @lines: a #GtkSourceGutterLines
 * @line: a line number starting from zero
 * @mode: a #GtkSourceGutterRendererAlignmentMode
 * @y: (out): a location for the Y position in widget coordinates
 * @height: (out): the line height based on @mode
 *
 * Gets the Y range for a line based on @mode.
 *
 * The value for @y is relative to the renderers widget coordinates.
 */
void
gtk_source_gutter_lines_get_line_yrange (GtkSourceGutterLines                 *lines,
                                         guint                                 line,
                                         GtkSourceGutterRendererAlignmentMode  mode,
                                         gint                                 *y,
                                         gint                                 *height)
{
	LineInfo *info;

	g_return_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines));
	g_return_if_fail (line >= lines->first);
	g_return_if_fail (line <= lines->last);

	info = &g_array_index (lines->lines, LineInfo, line - lines->first);

	if (mode == GTK_SOURCE_GUTTER_RENDERER_ALIGNMENT_MODE_CELL)
	{
		*y = info->y;
		*height = info->height;
	}
	else if (mode == GTK_SOURCE_GUTTER_RENDERER_ALIGNMENT_MODE_FIRST)
	{
		*y = info->y;
		*height = info->first_height;
	}
	else if (mode == GTK_SOURCE_GUTTER_RENDERER_ALIGNMENT_MODE_LAST)
	{
		*y = info->y + info->height - info->last_height;
		*height = info->last_height;
	}
	else
	{
		g_return_if_reached ();
	}

	*y -= lines->visible_rect.y;
}

guint
_gtk_source_gutter_lines_get_cursor_line (GtkSourceGutterLines *lines)
{
	g_return_val_if_fail (GTK_SOURCE_IS_GUTTER_LINES (lines), 0);

	return lines->cursor_line;
}

/**
 * gtk_source_gutter_lines_has_any_class:
 * @lines: a #GtkSourceGutterLines
 * @line: a line contained within @lines
 *
 * Checks to see if the line has any GQuark classes set. This can be
 * used to help renderer implementations avoid work if nothing has
 * been set on the class.
 *
 * Returns: %TRUE if any quark was set for the line
 *
 * Since: 5.6
 */
gboolean
gtk_source_gutter_lines_has_any_class (GtkSourceGutterLines *lines,
                                       guint                 line)
{
	if (lines == NULL || line < lines->first || line > lines->last || line - lines->first >= lines->lines->len)
    return FALSE;

  return g_array_index (lines->lines, LineInfo, line - lines->first).classes.len > 0;
}