File: test-entry.c

package info (click to toggle)
gspell 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,296 kB
  • sloc: ansic: 8,063; xml: 120; sh: 39; makefile: 12
file content (167 lines) | stat: -rw-r--r-- 3,918 bytes parent folder | download | duplicates (2)
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
/* SPDX-FileCopyrightText: 2016 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <gspell/gspell.h>

#define TEST_TYPE_SPELL (test_spell_get_type ())
G_DECLARE_FINAL_TYPE (TestSpell, test_spell,
		      TEST, SPELL,
		      GtkGrid)

struct _TestSpell
{
	GtkGrid parent;

	GtkEntry *entry;
};

G_DEFINE_TYPE (TestSpell, test_spell, GTK_TYPE_GRID)

static void
test_spell_class_init (TestSpellClass *klass)
{
}

static GtkEntry *
create_entry (void)
{
	GtkEntry *gtk_entry;
	GspellEntry *gspell_entry;

	gtk_entry = GTK_ENTRY (gtk_entry_new ());
	gtk_widget_set_hexpand (GTK_WIDGET (gtk_entry), TRUE);
	gtk_widget_set_valign (GTK_WIDGET (gtk_entry), GTK_ALIGN_START);

	gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
	gspell_entry_basic_setup (gspell_entry);

	return gtk_entry;
}

static void
bold_toggled_cb (GtkToggleButton *button,
		 TestSpell       *spell)
{
	/* Do not care about other users of the GtkEntry:attributes property. An
	 * application or another library might do something similar, but
	 * GspellEntry should still work.
	 */

	if (gtk_toggle_button_get_active (button))
	{
		PangoAttribute *attr_bold;
		PangoAttrList *attr_list;

		attr_bold = pango_attr_weight_new (PANGO_WEIGHT_BOLD);

		attr_list = pango_attr_list_new ();
		pango_attr_list_insert (attr_list, attr_bold);
		gtk_entry_set_attributes (spell->entry, attr_list);
		pango_attr_list_unref (attr_list);
	}
	else
	{
		gtk_entry_set_attributes (spell->entry, NULL);
	}
}

static GtkWidget *
create_sidebar (TestSpell *spell)
{
	GspellEntry *gspell_entry;
	GtkWidget *vgrid;
	GtkWidget *enable_toggle_button;
	GtkWidget *bold_toggle_button;
	GtkWidget *password_toggle_button;

	vgrid = gtk_grid_new ();
	gtk_grid_set_row_spacing (GTK_GRID (vgrid), 6);
	gtk_orientable_set_orientation (GTK_ORIENTABLE (vgrid),
					GTK_ORIENTATION_VERTICAL);

	enable_toggle_button = gtk_toggle_button_new_with_label ("Enable spell-checking");
	gtk_container_add (GTK_CONTAINER (vgrid), enable_toggle_button);

	gspell_entry = gspell_entry_get_from_gtk_entry (spell->entry);
	g_object_bind_property (gspell_entry, "inline-spell-checking",
				enable_toggle_button, "active",
				G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);

	bold_toggle_button = gtk_toggle_button_new_with_label ("Text in bold");
	gtk_container_add (GTK_CONTAINER (vgrid), bold_toggle_button);

	g_signal_connect (bold_toggle_button,
			  "toggled",
			  G_CALLBACK (bold_toggled_cb),
			  spell);

	password_toggle_button = gtk_toggle_button_new_with_label ("Password mode");
	gtk_container_add (GTK_CONTAINER (vgrid), password_toggle_button);

	g_object_bind_property (spell->entry, "visibility",
				password_toggle_button, "active",
				G_BINDING_BIDIRECTIONAL |
				G_BINDING_SYNC_CREATE |
				G_BINDING_INVERT_BOOLEAN);

	gtk_widget_show_all (vgrid);

	return vgrid;
}

static void
test_spell_init (TestSpell *spell)
{
	g_object_set (spell,
		      "margin", 6,
		      NULL);

	gtk_grid_set_column_spacing (GTK_GRID (spell), 6);
	gtk_orientable_set_orientation (GTK_ORIENTABLE (spell),
					GTK_ORIENTATION_HORIZONTAL);

	spell->entry = create_entry ();

	gtk_container_add (GTK_CONTAINER (spell),
			   create_sidebar (spell));

	gtk_container_add (GTK_CONTAINER (spell),
			   GTK_WIDGET (spell->entry));

	gtk_widget_show_all (GTK_WIDGET (spell));
}

static TestSpell *
test_spell_new (void)
{
	return g_object_new (TEST_TYPE_SPELL, NULL);
}

gint
main (gint    argc,
      gchar **argv)
{
	GtkWidget *window;
	TestSpell *spell;

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_window_set_default_size (GTK_WINDOW (window), 400, 200);

	g_signal_connect (window,
			  "destroy",
			  G_CALLBACK (gtk_main_quit),
			  NULL);

	spell = test_spell_new ();
	gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (spell));

	gtk_widget_show (window);

	gtk_main ();

	return 0;
}