aboutsummaryrefslogtreecommitdiff
path: root/src/stardict-view.c
blob: 32d725992d9377ed5854a4b18d2eeff28606806c (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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*
 * StarDict GTK+ UI - dictionary view component
 *
 * Copyright (c) 2021 - 2022, Přemysl Eric Janouch <p@janouch.name>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "stardict-view.h"
#include "utils.h"


typedef struct view_entry ViewEntry;

struct view_entry
{
	gchar *word;                        ///< The word
	gsize word_matched;                 ///< Initial matching bytes of the word
	gchar *definition;                  ///< Definition lines, in Pango markup

	PangoLayout *word_layout;           ///< Ellipsized one-line layout or NULL
	PangoLayout *definition_layout;     ///< Multiline layout or NULL
};

static void
view_entry_destroy (ViewEntry *self)
{
	g_free (self->word);
	g_free (self->definition);
	g_clear_object (&self->word_layout);
	g_clear_object (&self->definition_layout);
	g_slice_free1 (sizeof *self, self);
}

static ViewEntry *
view_entry_new (StardictIterator *iterator, const gchar *matched)
{
	g_return_val_if_fail (stardict_iterator_is_valid (iterator), NULL);

	StardictEntry *entry = stardict_iterator_get_entry (iterator);
	g_return_val_if_fail (entry != NULL, NULL);

	// Highlighting may change the rendition, so far it's easiest to recompute
	// it on each search field change by rebuilding the list of view entries.
	// The phonetics suffix would need to be stored separately.
	const gchar *word = stardict_iterator_get_word (iterator);

	GString *adjusted_word = g_string_new (word);
	GPtrArray *definitions = g_ptr_array_new_full (2, g_free);
	for (const GList *fields = stardict_entry_get_fields (entry); fields; )
	{
		const StardictEntryField *field = fields->data;
		switch (field->type)
		{
		case STARDICT_FIELD_MEANING:
			g_ptr_array_add (definitions,
				g_markup_escape_text (field->data, -1));
			break;
		case STARDICT_FIELD_PANGO:
			g_ptr_array_add (definitions, g_strdup (field->data));
			break;
		case STARDICT_FIELD_XDXF:
			g_ptr_array_add (definitions,
				xdxf_to_pango_markup_with_reduced_effort (field->data));
			break;
		case STARDICT_FIELD_PHONETIC:
			g_string_append_printf (adjusted_word,
				" /%s/", (const char *) field->data);
		default:
			// TODO: support more of them
			break;
		}
		fields = fields->next;
	}
	g_object_unref (entry);

	ViewEntry *ve = g_slice_alloc0 (sizeof *ve);
	ve->word = g_string_free (adjusted_word, FALSE);
	ve->word_matched = stardict_longest_common_collation_prefix
		(iterator->owner, word, matched);
	if (!definitions->len)
	{
		gchar *message = g_markup_escape_text (_("no usable field found"), -1);
		g_ptr_array_add (definitions, g_strdup_printf ("&lt;%s&gt;", message));
		g_free (message);
	}

	g_ptr_array_add (definitions, NULL);
	ve->definition = g_strjoinv ("\n", (gchar **) definitions->pdata);
	g_ptr_array_free (definitions, TRUE);
	return ve;
}

static gint
view_entry_height (ViewEntry *ve, gint *word_offset, gint *defn_offset)
{
	gint word_w = 0, word_h = 0;
	gint defn_w = 0, defn_h = 0;
	pango_layout_get_pixel_size (ve->word_layout,       &word_w, &word_h);
	pango_layout_get_pixel_size (ve->definition_layout, &defn_w, &defn_h);

	// Align baselines, without further considerations
	gint wb = pango_layout_get_baseline (ve->word_layout);
	gint db = pango_layout_get_baseline (ve->definition_layout);
	gint word_y = MAX (0, PANGO_PIXELS (db - wb));
	gint defn_y = MAX (0, PANGO_PIXELS (wb - db));

	if (word_offset)
		*word_offset = word_y;
	if (defn_offset)
		*defn_offset = defn_y;

	return MAX (word_y + word_h, defn_y + defn_h);
}

static GtkBorder
view_entry_get_padding (GtkStyleContext *style)
{
	GtkBorder padding = {};
	GtkStateFlags state = gtk_style_context_get_state (style);
	gtk_style_context_get_padding (style, state, &padding);
	return padding;
}

typedef struct view_entry_render_ctx ViewEntryRenderCtx;

// TODO: see if we can't think of a cleaner way of doing this
struct view_entry_render_ctx
{
	GtkStyleContext *style;
	cairo_t *cr;
	int width;
	int height;

	// Forwarded from StardictView
	PangoLayout *selection_layout;
	int selection_begin;
	int selection_end;
};

static void
view_entry_render (ViewEntryRenderCtx *ctx, gdouble x, gdouble y,
	PangoLayout *layout)
{
	gtk_render_layout (ctx->style, ctx->cr, x, y, layout);
	if (layout != ctx->selection_layout)
		return;

	gtk_style_context_save (ctx->style);
	gtk_style_context_set_state (ctx->style, GTK_STATE_FLAG_SELECTED);
	cairo_save (ctx->cr);

	int ranges[2] = { MIN (ctx->selection_begin, ctx->selection_end),
		MAX (ctx->selection_begin, ctx->selection_end) };
	cairo_region_t *region
		= gdk_pango_layout_get_clip_region (layout, x, y, ranges, 1);
	gdk_cairo_region (ctx->cr, region);
	cairo_clip (ctx->cr);
	cairo_region_destroy (region);

	gtk_render_background (ctx->style, ctx->cr, 0, 0, ctx->width, ctx->height);
	gtk_render_layout (ctx->style, ctx->cr, x, y, layout);

	cairo_restore (ctx->cr);
	gtk_style_context_restore (ctx->style);
}

static gint
view_entry_draw (ViewEntry *ve, ViewEntryRenderCtx *ctx)
{
	gint word_y = 0, defn_y = 0;
	ctx->height = view_entry_height (ve, &word_y, &defn_y);

	gtk_render_background (ctx->style, ctx->cr, 0, 0, ctx->width, ctx->height);
	gtk_render_frame (ctx->style, ctx->cr, 0, 0, ctx->width, ctx->height);

	// Top/bottom and left/right-dependent padding will not work, too much code
	GtkBorder padding = view_entry_get_padding (ctx->style);

	gtk_style_context_save (ctx->style);
	gtk_style_context_add_class (ctx->style, GTK_STYLE_CLASS_RIGHT);
	view_entry_render (ctx, ctx->width / 2 + padding.left, defn_y,
		ve->definition_layout);
	gtk_style_context_restore (ctx->style);

	gtk_style_context_save (ctx->style);
	gtk_style_context_add_class (ctx->style, GTK_STYLE_CLASS_LEFT);
	PangoLayoutIter *iter = pango_layout_get_iter (ve->definition_layout);
	do
	{
		if (!pango_layout_iter_get_line_readonly (iter)->is_paragraph_start)
			continue;

		PangoRectangle logical = {};
		pango_layout_iter_get_line_extents (iter, NULL, &logical);
		view_entry_render (ctx, padding.left, word_y + PANGO_PIXELS (logical.y),
			ve->word_layout);
	}
	while (pango_layout_iter_next_line (iter));
	pango_layout_iter_free (iter);
	gtk_style_context_restore (ctx->style);
	return ctx->height;
}

static void
view_entry_rebuild_layouts (ViewEntry *ve, GtkWidget *widget)
{
	PangoContext *pc = gtk_widget_get_pango_context (widget);
	GtkStyleContext *style = gtk_widget_get_style_context (widget);
	gint full_width = gtk_widget_get_allocated_width (widget);

	g_clear_object (&ve->word_layout);
	g_clear_object (&ve->definition_layout);

	GtkBorder padding = view_entry_get_padding (style);
	gint part_width = full_width / 2 - padding.left - padding.right;
	if (part_width < 1)
		return;

	// Left/right-dependent fonts aren't supported
	ve->word_layout = pango_layout_new (pc);
	pango_layout_set_text (ve->word_layout, ve->word, -1);
	pango_layout_set_ellipsize (ve->word_layout, PANGO_ELLIPSIZE_END);
	pango_layout_set_single_paragraph_mode (ve->word_layout, TRUE);
	pango_layout_set_width (ve->word_layout, PANGO_SCALE * part_width);

	// gtk_css_style_get_pango_attributes() is completely inaccessible,
	// so the underline cannot be styled (GTK_STYLE_PROPERTY_FONT isn't enough)
	PangoAttrList *attrs = pango_attr_list_new ();
	PangoAttribute *u = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
	u->end_index = ve->word_matched;
	pango_attr_list_insert (attrs, u);
	pango_layout_set_attributes (ve->word_layout, attrs);
	pango_attr_list_unref (attrs);

	// TODO: preferably pre-validate the layout with pango_parse_markup(),
	//   so that it doesn't warn without indication on the frontend
	ve->definition_layout = pango_layout_new (pc);
	pango_layout_set_markup (ve->definition_layout, ve->definition, -1);
	pango_layout_set_width (ve->definition_layout, PANGO_SCALE * part_width);
	pango_layout_set_wrap (ve->definition_layout, PANGO_WRAP_WORD_CHAR);
}

// --- Widget ------------------------------------------------------------------

struct _StardictView
{
	GtkWidget parent_instance;

	StardictDict *dict;                 ///< The displayed dictionary
	guint top_position;                 ///< Index of the topmost dict. entry
	gchar *matched;                     ///< Highlight common word part of this

	gint top_offset;                    ///< Pixel offset into the entry
	gdouble drag_last_offset;           ///< Last offset when dragging
	GList *entries;                     ///< ViewEntry-s within the view

	GtkGesture *selection_gesture;      ///< Selection gesture
	GWeakRef selection;                 ///< Selected PangoLayout, if any
	int selection_begin;                ///< Start index within `selection`
	int selection_end;                  ///< End index within `selection`
};

static ViewEntry *
make_entry (StardictView *self, StardictIterator *iterator)
{
	const gchar *matched = self->matched ? self->matched : "";
	ViewEntry *ve = view_entry_new (iterator, matched);
	view_entry_rebuild_layouts (ve, GTK_WIDGET (self));
	return ve;
}

static void
adjust_for_height (StardictView *self)
{
	GtkWidget *widget = GTK_WIDGET (self);
	StardictIterator *iterator =
		stardict_iterator_new (self->dict, self->top_position);

	gint missing = gtk_widget_get_allocated_height (widget) + self->top_offset;
	for (GList *iter = self->entries, *next;
		 next = g_list_next (iter), iter; iter = next)
	{
		if (missing > 0)
			missing -= view_entry_height (iter->data, NULL, NULL);
		else
		{
			view_entry_destroy (iter->data);
			self->entries = g_list_delete_link (self->entries, iter);
		}
		stardict_iterator_next (iterator);
	}

	GList *append = NULL;
	while (missing > 0 && stardict_iterator_is_valid (iterator))
	{
		ViewEntry *ve = make_entry (self, iterator);
		missing -= view_entry_height (ve, NULL, NULL);
		append = g_list_prepend (append, ve);
		stardict_iterator_next (iterator);
	}
	g_object_unref (iterator);

	self->entries = g_list_concat (self->entries, g_list_reverse (append));
	gtk_widget_queue_draw (widget);
}

static void
adjust_for_offset (StardictView *self)
{
	// If scrolled way up, prepend entries so long as it's possible
	StardictIterator *iterator =
		stardict_iterator_new (self->dict, self->top_position);
	while (self->top_offset < 0)
	{
		stardict_iterator_prev (iterator);
		if (!stardict_iterator_is_valid (iterator))
		{
			self->top_offset = 0;
			break;
		}

		self->top_position = stardict_iterator_get_offset (iterator);
		ViewEntry *ve = make_entry (self, iterator);
		self->top_offset += view_entry_height (ve, NULL, NULL);
		self->entries = g_list_prepend (self->entries, ve);
	}
	g_object_unref (iterator);

	// If scrolled way down, drop leading entries so long as it's possible
	while (self->entries)
	{
		gint height = view_entry_height (self->entries->data, NULL, NULL);
		if (self->top_offset < height)
			break;

		self->top_offset -= height;
		view_entry_destroy (self->entries->data);
		self->entries = g_list_delete_link (self->entries, self->entries);
		self->top_position++;

	}
	if (self->top_offset && !self->entries)
		self->top_offset = 0;

	// Load replacement trailing entries, or drop those no longer visible
	adjust_for_height (self);
}

static void
reload (StardictView *self)
{
	GtkWidget *widget = GTK_WIDGET (self);

	// FIXME: this invalidates the selection, we'd need better identification
	g_list_free_full (self->entries, (GDestroyNotify) view_entry_destroy);
	self->entries = NULL;
	gtk_widget_queue_draw (widget);

	// For consistency, and the check in make_context_menu()
	self->selection_begin = self->selection_end = -1;

	if (gtk_widget_get_realized (widget) && self->dict)
		adjust_for_height (self);
}

static gint
natural_row_size (GtkWidget *widget)
{
	PangoLayout *layout = gtk_widget_create_pango_layout (widget, "X");
	gint width = 0, height = 0;
	pango_layout_get_pixel_size (layout, &width, &height);
	g_object_unref (layout);
	return height;
}

// --- Boilerplate -------------------------------------------------------------

G_DEFINE_TYPE (StardictView, stardict_view, GTK_TYPE_WIDGET)

static void
stardict_view_finalize (GObject *gobject)
{
	StardictView *self = STARDICT_VIEW (gobject);
	g_clear_object (&self->dict);

	g_list_free_full (self->entries, (GDestroyNotify) view_entry_destroy);
	self->entries = NULL;

	g_object_unref (self->selection_gesture);
	g_weak_ref_clear (&self->selection);

	g_free (self->matched);
	self->matched = NULL;

	G_OBJECT_CLASS (stardict_view_parent_class)->finalize (gobject);
}

static void
stardict_view_get_preferred_height (GtkWidget *widget,
	gint *minimum, gint *natural)
{
	// There isn't any value that would make any real sense
	if (!STARDICT_VIEW (widget)->dict)
		*natural = *minimum = 0;
	else
		*natural = *minimum = natural_row_size (widget);
}

static void
stardict_view_get_preferred_width (GtkWidget *widget,
	gint *minimum, gint *natural)
{
	GtkStyleContext *style = gtk_widget_get_style_context (widget);
	GtkBorder padding = view_entry_get_padding (style);
	*natural = *minimum = 2 * (padding.left + 1 * padding.right);
}

static void
stardict_view_realize (GtkWidget *widget)
{
	GtkAllocation allocation;
	gtk_widget_get_allocation (widget, &allocation);

	GdkWindowAttr attributes =
	{
		.window_type = GDK_WINDOW_CHILD,
		.x           = allocation.x,
		.y           = allocation.y,
		.width       = allocation.width,
		.height      = allocation.height,

		// Input-only would presumably also work (as in GtkPathBar, e.g.),
		// but it merely seems to involve more work.
		.wclass      = GDK_INPUT_OUTPUT,
		.visual      = gtk_widget_get_visual (widget),
		.event_mask  = gtk_widget_get_events (widget) | GDK_SCROLL_MASK
			| GDK_SMOOTH_SCROLL_MASK | GDK_BUTTON_PRESS_MASK,
	};

	// We need this window to receive input events at all.
	// TODO: see if we don't want GDK_WA_CURSOR for setting a text cursor
	GdkWindow *window = gdk_window_new (gtk_widget_get_parent_window (widget),
		&attributes, GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL);

	// The default background colour of the GDK window is transparent
	gtk_widget_register_window (widget, window);
	gtk_widget_set_window (widget, window);
	gtk_widget_set_realized (widget, TRUE);
}

static gboolean
stardict_view_draw (GtkWidget *widget, cairo_t *cr)
{
	StardictView *self = STARDICT_VIEW (widget);

	GtkAllocation allocation;
	gtk_widget_get_allocation (widget, &allocation);

	GtkStyleContext *style = gtk_widget_get_style_context (widget);
	gtk_render_background (style, cr,
		0, 0, allocation.width, allocation.height);
	gtk_render_frame (style, cr,
		0, 0, allocation.width, allocation.height);

	ViewEntryRenderCtx ctx =
	{
		.style = style,
		.cr = cr,
		.width = allocation.width,
		.height = 0,
		.selection_layout = g_weak_ref_get (&self->selection),
		.selection_begin = self->selection_begin,
		.selection_end = self->selection_end,
	};

	gint offset = -self->top_offset;
	gint i = self->top_position;
	for (GList *iter = self->entries; iter; iter = iter->next)
	{
		// Style regions would be appropriate, if they weren't deprecated.
		// GTK+ CSS gadgets/nodes are an internal API.  We don't want to turn
		// this widget into a container, to avoid needless complexity.
		//
		// gtk_style_context_{get,set}_path() may be misused by adding the same
		// GType with gtk_widget_path_append_type() and changing its name
		// using gtk_widget_path_iter_set_name()... but that is ugly.
		gtk_style_context_save (style);
		gtk_style_context_add_class (style, (i++ & 1) ? "even" : "odd");

		cairo_save (cr);
		cairo_translate (cr, 0, offset);
		// TODO: later exclude clipped entries, but it's not that important
		offset += view_entry_draw (iter->data, &ctx);
		cairo_restore (cr);

		gtk_style_context_restore (style);
	}
	g_clear_object (&ctx.selection_layout);
	return TRUE;
}

/// Figure out which layout is at given widget coordinates, and translate them.
static PangoLayout *
layout_at (StardictView *self, int *x, int *y)
{
	GtkWidget *widget = GTK_WIDGET (self);
	int width = gtk_widget_get_allocated_width (widget);

	// The algorithm here is a simplification of stardict_view_draw().
	GtkStyleContext *style = gtk_widget_get_style_context (widget);
	GtkBorder padding = view_entry_get_padding (style);
	gint offset = -self->top_offset;
	for (GList *iter = self->entries; iter; iter = iter->next)
	{
		ViewEntry *ve = iter->data;
		if (G_UNLIKELY (*y < offset))
			break;

		gint top_y = offset, word_y = 0, defn_y = 0;
		offset += view_entry_height (ve, &word_y, &defn_y);
		if (*y >= offset)
			continue;

		if (*x >= width / 2)
		{
			*x -= width / 2 + padding.left;
			*y -= top_y + defn_y;
			return ve->definition_layout;
		}
		else
		{
			*x -= padding.left;
			*y -= top_y + word_y;
			return ve->word_layout;
		}
	}
	return NULL;
}

/// Figure out a layout's coordinates.
static gboolean
layout_coords (StardictView *self, PangoLayout *layout, int *x, int *y)
{
	GtkWidget *widget = GTK_WIDGET (self);
	int width = gtk_widget_get_allocated_width (widget);

	// The algorithm here is a simplification of stardict_view_draw().
	GtkStyleContext *style = gtk_widget_get_style_context (widget);
	GtkBorder padding = view_entry_get_padding (style);
	gint offset = -self->top_offset;
	for (GList *iter = self->entries; iter; iter = iter->next)
	{
		ViewEntry *ve = iter->data;
		gint top_y = offset, word_y = 0, defn_y = 0;
		offset += view_entry_height (ve, &word_y, &defn_y);

		if (layout == ve->definition_layout)
		{
			*x = width / 2 + padding.left;
			*y = top_y + defn_y;
			return TRUE;
		}
		if (layout == ve->word_layout)
		{
			*x = padding.left;
			*y = top_y + word_y;
			return TRUE;
		}
	}
	return FALSE;
}

static void
stardict_view_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
	GTK_WIDGET_CLASS (stardict_view_parent_class)
		->size_allocate (widget, allocation);

	reload (STARDICT_VIEW (widget));
}

static void
stardict_view_screen_changed (GtkWidget *widget, G_GNUC_UNUSED GdkScreen *prev)
{
	// Update the minimum size
	gtk_widget_queue_resize (widget);

	// Recreate Pango layouts
	reload (STARDICT_VIEW (widget));
}

static gboolean
stardict_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
{
	// TODO: rethink the notes here to rather iterate over /definition lines/
	//  - iterate over selected lines, maybe one, maybe three
	StardictView *self = STARDICT_VIEW (widget);
	if (!self->dict)
		return FALSE;

	switch (event->direction)
	{
	case GDK_SCROLL_UP:
		stardict_view_scroll (self, GTK_SCROLL_STEPS, -3);
		return TRUE;
	case GDK_SCROLL_DOWN:
		stardict_view_scroll (self, GTK_SCROLL_STEPS, +3);
		return TRUE;
	case GDK_SCROLL_SMOOTH:
	{
		// On GDK/Wayland, the mouse wheel will typically create 1.5 deltas,
		// after dividing a 15 degree click angle from libinput by 10.
		// (Noticed on Arch + Sway, cannot reproduce on Ubuntu 22.04.)
		// On X11, as libinput(4) indicates, the delta will always be 1.0.
		double delta = CLAMP (event->delta_y, -1, +1);
		stardict_view_scroll (self, GTK_SCROLL_STEPS, 3 * delta);
		return TRUE;
	}
	default:
		return FALSE;
	}
}

static int
layout_index_at (PangoLayout *layout, int x, int y)
{
	int index = 0, trailing = 0;
	(void) pango_layout_xy_to_index (layout,
		x * PANGO_SCALE,
		y * PANGO_SCALE,
		&index,
		&trailing);

	const char *text = pango_layout_get_text (layout) + index;
	while (trailing--)
	{
		int len = g_utf8_next_char(text) - text;
		text += len;
		index += len;
	}
	return index;
}

static void
publish_selection (StardictView *self, GdkAtom target)
{
	PangoLayout *layout = g_weak_ref_get (&self->selection);
	if (!layout || self->selection_begin == self->selection_end)
		return;

	// Unlike GtkLabel, we don't place the selection in PRIMARY immediately.
	const char *text = pango_layout_get_text (layout);
	int len = strlen (text),
		s1 = MIN (self->selection_begin, self->selection_end),
		s2 = MAX (self->selection_begin, self->selection_end);
	if (s1 >= 0 && s1 <= len && s2 >= 0 && s2 <= len)
		gtk_clipboard_set_text (gtk_clipboard_get (target), text + s1, s2 - s1);
	g_object_unref (layout);
}

static void
select_word_at (StardictView *self, int x, int y)
{
	PangoLayout *layout = layout_at (self, &x, &y);
	if (!layout)
		return;

	const char *text = pango_layout_get_text (layout), *p = NULL;
	const char *begin = text + layout_index_at (layout, x, y), *end = begin;
	while ((p = g_utf8_find_prev_char (text, begin))
		&& !g_unichar_isspace (g_utf8_get_char (p)))
		begin = p;
	gunichar c;
	while ((c = g_utf8_get_char (end)) && !g_unichar_isspace (c))
		end = g_utf8_next_char (end);

	g_weak_ref_set (&self->selection, layout);
	self->selection_begin = begin - text;
	self->selection_end = end - text;
	gtk_widget_queue_draw (GTK_WIDGET (self));
	publish_selection (self, GDK_SELECTION_PRIMARY);
}

static void
select_all_at (StardictView *self, int x, int y)
{
	PangoLayout *layout = layout_at (self, &x, &y);
	if (!layout)
		return;

	g_weak_ref_set (&self->selection, layout);
	self->selection_begin = 0;
	self->selection_end = strlen (pango_layout_get_text (layout));
	gtk_widget_queue_draw (GTK_WIDGET (self));
	publish_selection (self, GDK_SELECTION_PRIMARY);
}

static void
on_copy_activate (G_GNUC_UNUSED GtkMenuItem *item, gpointer user_data)
{
	publish_selection (STARDICT_VIEW (user_data), GDK_SELECTION_CLIPBOARD);
}

static gboolean
destroy_widget_idle_source_func (GtkWidget *widget)
{
	// The whole menu is deactivated /before/ any item is activated,
	// and a destroyed child item will not activate.
	gtk_widget_destroy (widget);
	return FALSE;
}

static GtkMenu *
make_context_menu (StardictView *self)
{
	GtkWidget *copy = gtk_menu_item_new_with_mnemonic ("_Copy");
	gtk_widget_set_sensitive (copy,
		self->selection_begin < self->selection_end);
	g_signal_connect_data (copy, "activate",
		G_CALLBACK (on_copy_activate), g_object_ref (self),
		(GClosureNotify) g_object_unref, 0);

	GtkWidget *menu = gtk_menu_new ();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), copy);

	// As per GTK+ 3 Common Questions, 1.5.
	g_object_ref_sink (menu);
	g_signal_connect_swapped (menu, "deactivate",
		G_CALLBACK (g_idle_add), destroy_widget_idle_source_func);
	g_signal_connect (menu, "destroy",
		G_CALLBACK (g_object_unref), NULL);

	gtk_widget_show_all (menu);
	return GTK_MENU (menu);
}

static gboolean
stardict_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
{
	StardictView *self = STARDICT_VIEW (widget);
	if (gdk_event_triggers_context_menu((const GdkEvent *) event))
	{
		gtk_menu_popup_at_pointer (make_context_menu (self),
			(const GdkEvent *) event);
		return GDK_EVENT_STOP;
	}

	if (event->type == GDK_2BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
	{
		gtk_event_controller_reset (
			GTK_EVENT_CONTROLLER (self->selection_gesture));
		select_word_at (self, event->x, event->y);
		return GDK_EVENT_STOP;
	}

	if (event->type == GDK_3BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
	{
		gtk_event_controller_reset (
			GTK_EVENT_CONTROLLER (self->selection_gesture));
		select_all_at (self, event->x, event->y);
		return GDK_EVENT_STOP;
	}

	return GTK_WIDGET_CLASS (stardict_view_parent_class)
		->button_press_event (widget, event);
}

static void
on_drag_begin (GtkGestureDrag *drag, G_GNUC_UNUSED gdouble start_x,
	G_GNUC_UNUSED gdouble start_y, gpointer user_data)
{
	GtkGesture *gesture = GTK_GESTURE (drag);
	GdkEventSequence *sequence
		= gtk_gesture_get_last_updated_sequence (gesture);

	GdkModifierType state = 0;
	const GdkEvent *last_event = gtk_gesture_get_last_event (gesture, sequence);
	(void) gdk_event_get_state (last_event, &state);
	if (state & gtk_accelerator_get_default_mod_mask ())
		gtk_gesture_set_sequence_state (gesture, sequence,
			GTK_EVENT_SEQUENCE_DENIED);
	else
	{
		gtk_gesture_set_sequence_state (gesture, sequence,
			GTK_EVENT_SEQUENCE_CLAIMED);
		STARDICT_VIEW (user_data)->drag_last_offset = 0;
	}
}

static void
on_drag_update (G_GNUC_UNUSED GtkGestureDrag *drag,
	G_GNUC_UNUSED gdouble offset_x, gdouble offset_y, gpointer user_data)
{
	StardictView *self = STARDICT_VIEW (user_data);
	self->top_offset += self->drag_last_offset - offset_y;
	adjust_for_offset (self);
	self->drag_last_offset = offset_y;
}

static void
on_select_begin (GtkGestureDrag *drag, gdouble start_x, gdouble start_y,
	gpointer user_data)
{
	// Despite our two gestures not being grouped up, claiming one doesn't
	// deny the other, and :exclusive isn't the opposite of :touch-only.
	// A non-NULL sequence indicates a touch event.
	GtkGesture *gesture = GTK_GESTURE (drag);
	if (gtk_gesture_get_last_updated_sequence (gesture))
	{
		gtk_gesture_set_state(gesture, GTK_EVENT_SEQUENCE_DENIED);
		return;
	}

	StardictView *self = STARDICT_VIEW (user_data);
	g_weak_ref_set (&self->selection, NULL);
	self->selection_begin = -1;
	self->selection_end = -1;
	gtk_widget_queue_draw (GTK_WIDGET (self));

	int layout_x = start_x, layout_y = start_y;
	PangoLayout *layout = layout_at (self, &layout_x, &layout_y);
	if (!layout)
	{
		gtk_gesture_set_state(gesture, GTK_EVENT_SEQUENCE_DENIED);
		return;
	}

	g_weak_ref_set (&self->selection, layout);
	self->selection_end = self->selection_begin
		= layout_index_at (layout, layout_x, layout_y);
	gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_CLAIMED);
}

static void
on_select_update (GtkGestureDrag *drag, gdouble offset_x, gdouble offset_y,
	gpointer user_data)
{
	GtkGesture *gesture = GTK_GESTURE (drag);
	StardictView *self = STARDICT_VIEW (user_data);
	PangoLayout *layout = g_weak_ref_get (&self->selection);
	if (!layout)
	{
		gtk_gesture_set_state(gesture, GTK_EVENT_SEQUENCE_DENIED);
		return;
	}

	double start_x = 0, start_y = 0;
	(void) gtk_gesture_drag_get_start_point (drag, &start_x, &start_y);

	int x = 0, y = 0;
	if (!layout_coords (self, layout, &x, &y))
	{
		g_warning ("internal error: weakly referenced layout not found");
		gtk_gesture_set_state(gesture, GTK_EVENT_SEQUENCE_DENIED);
		goto out;
	}

	self->selection_end = layout_index_at (layout,
		start_x + offset_x - x,
		start_y + offset_y - y);
	gtk_widget_queue_draw (GTK_WIDGET (self));

out:
	g_object_unref (layout);
}

static void
on_select_end (G_GNUC_UNUSED GtkGestureDrag *drag,
	G_GNUC_UNUSED gdouble offset_x, G_GNUC_UNUSED gdouble offset_y,
	gpointer user_data)
{
	publish_selection (STARDICT_VIEW (user_data), GDK_SELECTION_PRIMARY);
}

static void
stardict_view_class_init (StardictViewClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = stardict_view_finalize;

	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
	widget_class->get_preferred_height = stardict_view_get_preferred_height;
	widget_class->get_preferred_width = stardict_view_get_preferred_width;
	widget_class->realize = stardict_view_realize;
	widget_class->draw = stardict_view_draw;
	widget_class->size_allocate = stardict_view_size_allocate;
	widget_class->screen_changed = stardict_view_screen_changed;
	widget_class->scroll_event = stardict_view_scroll_event;
	widget_class->button_press_event = stardict_view_button_press_event;

	gtk_widget_class_set_css_name (widget_class, "stardict-view");
}

static void
stardict_view_init (StardictView *self)
{
	g_weak_ref_init (&self->selection, NULL);
	self->selection_begin = -1;
	self->selection_end = -1;

	GtkGesture *drag = gtk_gesture_drag_new (GTK_WIDGET (self));
	gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (drag), TRUE);
	gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (drag),
		GTK_PHASE_TARGET);
	g_object_set_data_full (G_OBJECT (self), "stardict-view-drag-gesture",
		drag, g_object_unref);
	g_signal_connect (drag, "drag-begin",
		G_CALLBACK (on_drag_begin), self);
	g_signal_connect (drag, "drag-update",
		G_CALLBACK (on_drag_update), self);

	self->selection_gesture = gtk_gesture_drag_new (GTK_WIDGET (self));
	gtk_gesture_single_set_exclusive (
		GTK_GESTURE_SINGLE (self->selection_gesture), TRUE);
	gtk_event_controller_set_propagation_phase (
		GTK_EVENT_CONTROLLER (self->selection_gesture), GTK_PHASE_TARGET);
	g_signal_connect (self->selection_gesture, "drag-begin",
		G_CALLBACK (on_select_begin), self);
	g_signal_connect (self->selection_gesture, "drag-update",
		G_CALLBACK (on_select_update), self);
	g_signal_connect (self->selection_gesture, "drag-end",
		G_CALLBACK (on_select_end), self);
}

// --- Public ------------------------------------------------------------------

GtkWidget *
stardict_view_new (void)
{
	return GTK_WIDGET (g_object_new (STARDICT_TYPE_VIEW, NULL));
}

void
stardict_view_set_position (StardictView *self,
	StardictDict *dict, guint position)
{
	g_return_if_fail (STARDICT_IS_VIEW (self));
	g_return_if_fail (dict == NULL || STARDICT_IS_DICT (dict));

	// Update the minimum size, if appropriate (almost never)
	if (!self->dict != !dict)
		gtk_widget_queue_resize (GTK_WIDGET (self));

	g_clear_object (&self->dict);
	self->dict = dict ? g_object_ref (dict) : NULL;
	self->top_position = position;
	self->top_offset = 0;

	reload (self);
}

void
stardict_view_set_matched (StardictView *self, const gchar *matched)
{
	g_return_if_fail (STARDICT_IS_VIEW (self));

	g_free (self->matched);
	self->matched = g_strdup (matched);
	reload (self);
}

void
stardict_view_scroll (StardictView *self, GtkScrollStep step, gdouble amount)
{
	g_return_if_fail (STARDICT_IS_VIEW (self));

	GtkWidget *widget = GTK_WIDGET (self);
	switch (step)
	{
	case GTK_SCROLL_STEPS:
		self->top_offset += amount * natural_row_size (widget);
		break;
	case GTK_SCROLL_PAGES:
		self->top_offset += amount * gtk_widget_get_allocated_height (widget);
		break;
	default:
		break;
	}

	adjust_for_offset (self);
}