| 1 |
/* SPDX-FileCopyrightText: 2016 - Sébastien Wilmet <[email protected]> |
| 2 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <gspell/gspell.h> |
| 6 |
|
| 7 |
#define TEST_TYPE_SPELL (test_spell_get_type ()) |
| 8 |
G_DECLARE_FINAL_TYPE (TestSpell, test_spell, |
| 9 |
TEST, SPELL, |
| 10 |
GtkGrid) |
| 11 |
|
| 12 |
struct _TestSpell |
| 13 |
{ |
| 14 |
GtkGrid parent; |
| 15 |
|
| 16 |
GtkEntry *entry; |
| 17 |
}; |
| 18 |
|
| 19 |
G_DEFINE_TYPE (TestSpell, test_spell, GTK_TYPE_GRID) |
| 20 |
|
| 21 |
static void |
| 22 |
test_spell_class_init (TestSpellClass *klass) |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
static GtkEntry * |
| 27 |
create_entry (void) |
| 28 |
{ |
| 29 |
GtkEntry *gtk_entry; |
| 30 |
GspellEntry *gspell_entry; |
| 31 |
|
| 32 |
gtk_entry = GTK_ENTRY (gtk_entry_new ()); |
| 33 |
gtk_widget_set_hexpand (GTK_WIDGET (gtk_entry), TRUE); |
| 34 |
gtk_widget_set_valign (GTK_WIDGET (gtk_entry), GTK_ALIGN_START); |
| 35 |
|
| 36 |
gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry); |
| 37 |
gspell_entry_basic_setup (gspell_entry); |
| 38 |
|
| 39 |
return gtk_entry; |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
bold_toggled_cb (GtkToggleButton *button, |
| 44 |
TestSpell *spell) |
| 45 |
{ |
| 46 |
/* Do not care about other users of the GtkEntry:attributes property. An |
| 47 |
* application or another library might do something similar, but |
| 48 |
* GspellEntry should still work. |
| 49 |
*/ |
| 50 |
|
| 51 |
if (gtk_toggle_button_get_active (button)) |
| 52 |
{ |
| 53 |
PangoAttribute *attr_bold; |
| 54 |
PangoAttrList *attr_list; |
| 55 |
|
| 56 |
attr_bold = pango_attr_weight_new (PANGO_WEIGHT_BOLD); |
| 57 |
|
| 58 |
attr_list = pango_attr_list_new (); |
| 59 |
pango_attr_list_insert (attr_list, attr_bold); |
| 60 |
gtk_entry_set_attributes (spell->entry, attr_list); |
| 61 |
pango_attr_list_unref (attr_list); |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
gtk_entry_set_attributes (spell->entry, NULL); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static GtkWidget * |
| 70 |
create_sidebar (TestSpell *spell) |
| 71 |
{ |
| 72 |
GspellEntry *gspell_entry; |
| 73 |
GtkWidget *vgrid; |
| 74 |
GtkWidget *enable_toggle_button; |
| 75 |
GtkWidget *bold_toggle_button; |
| 76 |
GtkWidget *password_toggle_button; |
| 77 |
|
| 78 |
vgrid = gtk_grid_new (); |
| 79 |
gtk_grid_set_row_spacing (GTK_GRID (vgrid), 6); |
| 80 |
gtk_orientable_set_orientation (GTK_ORIENTABLE (vgrid), |
| 81 |
GTK_ORIENTATION_VERTICAL); |
| 82 |
|
| 83 |
enable_toggle_button = gtk_toggle_button_new_with_label ("Enable spell-checking"); |
| 84 |
gtk_container_add (GTK_CONTAINER (vgrid), enable_toggle_button); |
| 85 |
|
| 86 |
gspell_entry = gspell_entry_get_from_gtk_entry (spell->entry); |
| 87 |
g_object_bind_property (gspell_entry, "inline-spell-checking", |
| 88 |
enable_toggle_button, "active", |
| 89 |
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); |
| 90 |
|
| 91 |
bold_toggle_button = gtk_toggle_button_new_with_label ("Text in bold"); |
| 92 |
gtk_container_add (GTK_CONTAINER (vgrid), bold_toggle_button); |
| 93 |
|
| 94 |
g_signal_connect (bold_toggle_button, |
| 95 |
"toggled", |
| 96 |
G_CALLBACK (bold_toggled_cb), |
| 97 |
spell); |
| 98 |
|
| 99 |
password_toggle_button = gtk_toggle_button_new_with_label ("Password mode"); |
| 100 |
gtk_container_add (GTK_CONTAINER (vgrid), password_toggle_button); |
| 101 |
|
| 102 |
g_object_bind_property (spell->entry, "visibility", |
| 103 |
password_toggle_button, "active", |
| 104 |
G_BINDING_BIDIRECTIONAL | |
| 105 |
G_BINDING_SYNC_CREATE | |
| 106 |
G_BINDING_INVERT_BOOLEAN); |
| 107 |
|
| 108 |
gtk_widget_show_all (vgrid); |
| 109 |
|
| 110 |
return vgrid; |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
test_spell_init (TestSpell *spell) |
| 115 |
{ |
| 116 |
g_object_set (spell, |
| 117 |
"margin", 6, |
| 118 |
NULL); |
| 119 |
|
| 120 |
gtk_grid_set_column_spacing (GTK_GRID (spell), 6); |
| 121 |
gtk_orientable_set_orientation (GTK_ORIENTABLE (spell), |
| 122 |
GTK_ORIENTATION_HORIZONTAL); |
| 123 |
|
| 124 |
spell->entry = create_entry (); |
| 125 |
|
| 126 |
gtk_container_add (GTK_CONTAINER (spell), |
| 127 |
create_sidebar (spell)); |
| 128 |
|
| 129 |
gtk_container_add (GTK_CONTAINER (spell), |
| 130 |
GTK_WIDGET (spell->entry)); |
| 131 |
|
| 132 |
gtk_widget_show_all (GTK_WIDGET (spell)); |
| 133 |
} |
| 134 |
|
| 135 |
static TestSpell * |
| 136 |
test_spell_new (void) |
| 137 |
{ |
| 138 |
return g_object_new (TEST_TYPE_SPELL, NULL); |
| 139 |
} |
| 140 |
|
| 141 |
gint |
| 142 |
main (gint argc, |
| 143 |
gchar **argv) |
| 144 |
{ |
| 145 |
GtkWidget *window; |
| 146 |
TestSpell *spell; |
| 147 |
|
| 148 |
gtk_init (&argc, &argv); |
| 149 |
|
| 150 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 151 |
|
| 152 |
gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); |
| 153 |
|
| 154 |
g_signal_connect (window, |
| 155 |
"destroy", |
| 156 |
G_CALLBACK (gtk_main_quit), |
| 157 |
NULL); |
| 158 |
|
| 159 |
spell = test_spell_new (); |
| 160 |
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (spell)); |
| 161 |
|
| 162 |
gtk_widget_show (window); |
| 163 |
|
| 164 |
gtk_main (); |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |