summaryrefslogtreecommitdiff
path: root/gtksourceview/gtksourcehoverdisplay.c
blob: 310de86b5a34ca4abde5b02e4ff906ffafd3e9f9 (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
/*
 * This file is part of GtkSourceView
 *
 * Copyright 2021 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/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "gtksourcehoverdisplay-private.h"

/**
 * GtkSourceHoverDisplay:
 *
 * Display for interactive tooltips.
 *
 * `GtkSourceHoverDisplay` is a [class@Gtk.Widget] that may be populated with widgets
 * to be displayed to the user in interactive tooltips. The children widgets
 * are packed vertically using a [class@Gtk.Box].
 *
 * Implement the [iface@HoverProvider] interface to be notified of when
 * to populate a `GtkSourceHoverDisplay` on behalf of the user.
 */

struct _GtkSourceHoverDisplay
{
	GtkWidget parent_instance;

	GtkBox *vbox;
};

G_DEFINE_TYPE (GtkSourceHoverDisplay, gtk_source_hover_display, GTK_TYPE_WIDGET)

static void
gtk_source_hover_display_dispose (GObject *object)
{
	GtkSourceHoverDisplay *self = (GtkSourceHoverDisplay *)object;

	g_clear_pointer ((GtkWidget **)&self->vbox, gtk_widget_unparent);

	G_OBJECT_CLASS (gtk_source_hover_display_parent_class)->dispose (object);
}

static void
gtk_source_hover_display_class_init (GtkSourceHoverDisplayClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	object_class->dispose = gtk_source_hover_display_dispose;

	gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
	gtk_widget_class_set_css_name (widget_class, "GtkSourceHoverDisplay");
}

static void
gtk_source_hover_display_init (GtkSourceHoverDisplay *self)
{
	self->vbox = g_object_new (GTK_TYPE_BOX,
	                           "orientation", GTK_ORIENTATION_VERTICAL,
	                           NULL);
	gtk_widget_set_parent (GTK_WIDGET (self->vbox), GTK_WIDGET (self));
}

void
gtk_source_hover_display_append (GtkSourceHoverDisplay *self,
                                 GtkWidget             *child)
{
	g_return_if_fail (GTK_SOURCE_IS_HOVER_DISPLAY (self));
	g_return_if_fail (GTK_IS_WIDGET (child));

	gtk_box_append (self->vbox, child);
}

void
gtk_source_hover_display_prepend (GtkSourceHoverDisplay *self,
                                  GtkWidget             *child)
{
	g_return_if_fail (GTK_SOURCE_IS_HOVER_DISPLAY (self));
	g_return_if_fail (GTK_IS_WIDGET (child));

	gtk_box_prepend (self->vbox, child);
}

void
gtk_source_hover_display_insert_after (GtkSourceHoverDisplay *self,
                                       GtkWidget             *child,
                                       GtkWidget             *sibling)
{
	g_return_if_fail (GTK_SOURCE_IS_HOVER_DISPLAY (self));
	g_return_if_fail (GTK_IS_WIDGET (child));
	g_return_if_fail (!sibling || GTK_IS_WIDGET (sibling));

	if (sibling == NULL)
	{
		gtk_source_hover_display_append (self, child);
	}
	else
	{
		gtk_box_insert_child_after (self->vbox, child, sibling);
	}
}

void
gtk_source_hover_display_remove (GtkSourceHoverDisplay *self,
                                 GtkWidget             *child)
{
	g_return_if_fail (GTK_SOURCE_IS_HOVER_DISPLAY (self));
	g_return_if_fail (GTK_IS_WIDGET (child));
	g_return_if_fail (gtk_widget_get_parent (child) == (GtkWidget *)self->vbox);

	gtk_box_remove (self->vbox, child);
}

void
_gtk_source_hover_display_clear (GtkSourceHoverDisplay *self)
{
	GtkWidget *child;

	g_return_if_fail (GTK_SOURCE_IS_HOVER_DISPLAY (self));

	while ((child = gtk_widget_get_first_child (GTK_WIDGET (self->vbox))))
	{
		gtk_box_remove (self->vbox, child);
	}
}

gboolean
_gtk_source_hover_display_is_empty (GtkSourceHoverDisplay *self)
{
	g_return_val_if_fail (GTK_SOURCE_IS_HOVER_DISPLAY (self), FALSE);

	return gtk_widget_get_first_child (GTK_WIDGET (self->vbox)) == NULL;
}