| 1 |
#include <stdlib.h> |
| 2 |
#include <gmodule.h> |
| 3 |
#include <clutter/clutter.h> |
| 4 |
|
| 5 |
static void |
| 6 |
on_entry_activate (ClutterText *text, |
| 7 |
gpointer data) |
| 8 |
{ |
| 9 |
g_print ("Text activated: %s (cursor: %d, selection at: %d)\n", |
| 10 |
clutter_text_get_text (text), |
| 11 |
clutter_text_get_cursor_position (text), |
| 12 |
clutter_text_get_selection_bound (text)); |
| 13 |
} |
| 14 |
|
| 15 |
#define is_hex_digit(c) (((c) >= '0' && (c) <= '9') || \ |
| 16 |
((c) >= 'a' && (c) <= 'f') || \ |
| 17 |
((c) >= 'A' && (c) <= 'F')) |
| 18 |
#define to_hex_digit(c) (((c) <= '9') ? (c) - '0' : ((c) & 7) + 9) |
| 19 |
|
| 20 |
static gboolean |
| 21 |
on_captured_event (ClutterText *text, |
| 22 |
ClutterEvent *event, |
| 23 |
gpointer dummy G_GNUC_UNUSED) |
| 24 |
{ |
| 25 |
gboolean is_unicode_mode = FALSE; |
| 26 |
gunichar c; |
| 27 |
guint keyval; |
| 28 |
|
| 29 |
if (event->type != CLUTTER_KEY_PRESS) |
| 30 |
return FALSE; |
| 31 |
|
| 32 |
is_unicode_mode = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (text), |
| 33 |
"unicode-mode")); |
| 34 |
|
| 35 |
c = clutter_event_get_key_unicode (event); |
| 36 |
keyval = clutter_event_get_key_symbol (event); |
| 37 |
if (keyval == CLUTTER_KEY_U) |
| 38 |
{ |
| 39 |
if (is_unicode_mode) |
| 40 |
{ |
| 41 |
GString *str = g_object_get_data (G_OBJECT (text), "unicode-str"); |
| 42 |
|
| 43 |
clutter_text_set_preedit_string (text, NULL, NULL, 0); |
| 44 |
|
| 45 |
g_object_set_data (G_OBJECT (text), "unicode-mode", |
| 46 |
GINT_TO_POINTER (FALSE)); |
| 47 |
g_object_set_data (G_OBJECT (text), "unicode-str", |
| 48 |
NULL); |
| 49 |
|
| 50 |
g_string_free (str, TRUE); |
| 51 |
|
| 52 |
return FALSE; |
| 53 |
} |
| 54 |
|
| 55 |
if (clutter_event_has_control_modifier (event)) |
| 56 |
{ |
| 57 |
PangoAttrList *attrs; |
| 58 |
PangoAttribute *a; |
| 59 |
GString *str = g_string_sized_new (5); |
| 60 |
|
| 61 |
g_string_append (str, "u"); |
| 62 |
|
| 63 |
g_object_set_data (G_OBJECT (text), |
| 64 |
"unicode-mode", |
| 65 |
GINT_TO_POINTER (TRUE)); |
| 66 |
g_object_set_data (G_OBJECT (text), |
| 67 |
"unicode-str", |
| 68 |
str); |
| 69 |
|
| 70 |
attrs = pango_attr_list_new (); |
| 71 |
|
| 72 |
a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE); |
| 73 |
a->start_index = 0; |
| 74 |
a->end_index = str->len; |
| 75 |
pango_attr_list_insert (attrs, a); |
| 76 |
|
| 77 |
clutter_text_set_preedit_string (text, str->str, attrs, str->len); |
| 78 |
|
| 79 |
pango_attr_list_unref (attrs); |
| 80 |
|
| 81 |
return TRUE; |
| 82 |
} |
| 83 |
|
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
else if (is_unicode_mode && is_hex_digit (c)) |
| 87 |
{ |
| 88 |
GString *str = g_object_get_data (G_OBJECT (text), "unicode-str"); |
| 89 |
PangoAttrList *attrs; |
| 90 |
PangoAttribute *a; |
| 91 |
gchar buf[8]; |
| 92 |
gsize len; |
| 93 |
|
| 94 |
len = g_unichar_to_utf8 (c, buf); |
| 95 |
buf[len] = '\0'; |
| 96 |
|
| 97 |
g_string_append (str, buf); |
| 98 |
|
| 99 |
g_print ("added '%s' to '%s' (len:%d)\n", |
| 100 |
buf, |
| 101 |
str->str, |
| 102 |
(int) str->len); |
| 103 |
|
| 104 |
attrs = pango_attr_list_new (); |
| 105 |
|
| 106 |
a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE); |
| 107 |
a->start_index = 0; |
| 108 |
a->end_index = str->len; |
| 109 |
pango_attr_list_insert (attrs, a); |
| 110 |
|
| 111 |
clutter_text_set_preedit_string (text, str->str, attrs, str->len); |
| 112 |
|
| 113 |
pango_attr_list_unref (attrs); |
| 114 |
|
| 115 |
return TRUE; |
| 116 |
} |
| 117 |
else if (is_unicode_mode && (keyval == CLUTTER_KEY_BackSpace)) |
| 118 |
{ |
| 119 |
GString *str = g_object_get_data (G_OBJECT (text), "unicode-str"); |
| 120 |
PangoAttrList *attrs; |
| 121 |
PangoAttribute *a; |
| 122 |
|
| 123 |
g_string_truncate (str, str->len - 1); |
| 124 |
|
| 125 |
attrs = pango_attr_list_new (); |
| 126 |
|
| 127 |
a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE); |
| 128 |
a->start_index = 0; |
| 129 |
a->end_index = str->len; |
| 130 |
pango_attr_list_insert (attrs, a); |
| 131 |
|
| 132 |
clutter_text_set_preedit_string (text, str->str, attrs, str->len); |
| 133 |
|
| 134 |
pango_attr_list_unref (attrs); |
| 135 |
|
| 136 |
return TRUE; |
| 137 |
} |
| 138 |
else if (is_unicode_mode && |
| 139 |
(keyval == CLUTTER_KEY_Return || |
| 140 |
keyval == CLUTTER_KEY_KP_Enter || |
| 141 |
keyval == CLUTTER_KEY_ISO_Enter || |
| 142 |
keyval == CLUTTER_KEY_KP_Space)) |
| 143 |
{ |
| 144 |
GString *str = g_object_get_data (G_OBJECT (text), "unicode-str"); |
| 145 |
const gchar *contents = clutter_text_get_text (text); |
| 146 |
gunichar uchar = 0; |
| 147 |
gchar ch; |
| 148 |
gint i; |
| 149 |
|
| 150 |
clutter_text_set_preedit_string (text, NULL, NULL, 0); |
| 151 |
|
| 152 |
g_object_set_data (G_OBJECT (text), "unicode-mode", |
| 153 |
GINT_TO_POINTER (FALSE)); |
| 154 |
g_object_set_data (G_OBJECT (text), "unicode-str", |
| 155 |
NULL); |
| 156 |
|
| 157 |
for (i = 0; i < str->len; i++) |
| 158 |
{ |
| 159 |
ch = str->str[i]; |
| 160 |
|
| 161 |
if (is_hex_digit (ch)) |
| 162 |
uchar += ((gunichar) to_hex_digit (ch) << ((4 - i) * 4)); |
| 163 |
} |
| 164 |
|
| 165 |
g_assert (g_unichar_validate (uchar)); |
| 166 |
|
| 167 |
g_string_overwrite (str, 0, contents); |
| 168 |
g_string_insert_unichar (str, |
| 169 |
clutter_text_get_cursor_position (text), |
| 170 |
uchar); |
| 171 |
|
| 172 |
i = clutter_text_get_cursor_position (text); |
| 173 |
clutter_text_set_text (text, str->str); |
| 174 |
|
| 175 |
if (i >= 0) |
| 176 |
i += 1; |
| 177 |
else |
| 178 |
i = -1; |
| 179 |
|
| 180 |
clutter_text_set_cursor_position (text, i); |
| 181 |
clutter_text_set_selection_bound (text, i); |
| 182 |
|
| 183 |
g_string_free (str, TRUE); |
| 184 |
|
| 185 |
return TRUE; |
| 186 |
} |
| 187 |
else |
| 188 |
return FALSE; |
| 189 |
} |
| 190 |
|
| 191 |
static ClutterActor * |
| 192 |
create_label (const ClutterColor *color, |
| 193 |
const gchar *text) |
| 194 |
{ |
| 195 |
ClutterActor *retval = clutter_text_new (); |
| 196 |
|
| 197 |
clutter_text_set_color (CLUTTER_TEXT (retval), color); |
| 198 |
clutter_text_set_markup (CLUTTER_TEXT (retval), text); |
| 199 |
clutter_text_set_editable (CLUTTER_TEXT (retval), FALSE); |
| 200 |
clutter_text_set_selectable (CLUTTER_TEXT (retval), FALSE); |
| 201 |
clutter_text_set_single_line_mode (CLUTTER_TEXT (retval), TRUE); |
| 202 |
clutter_text_set_ellipsize (CLUTTER_TEXT (retval), PANGO_ELLIPSIZE_END); |
| 203 |
|
| 204 |
return retval; |
| 205 |
} |
| 206 |
|
| 207 |
static ClutterActor * |
| 208 |
create_entry (const ClutterColor *color, |
| 209 |
const gchar *text, |
| 210 |
PangoAttrList *attrs, |
| 211 |
gunichar password_char, |
| 212 |
gint max_length) |
| 213 |
{ |
| 214 |
ClutterActor *retval = clutter_text_new_full (NULL, text, color); |
| 215 |
ClutterColor selection = { 0, }; |
| 216 |
ClutterColor selected_text = { 0x00, 0x00, 0xff, 0xff }; |
| 217 |
|
| 218 |
clutter_actor_set_reactive (retval, TRUE); |
| 219 |
|
| 220 |
clutter_color_darken (color, &selection); |
| 221 |
|
| 222 |
clutter_text_set_editable (CLUTTER_TEXT (retval), TRUE); |
| 223 |
clutter_text_set_selectable (CLUTTER_TEXT (retval), TRUE); |
| 224 |
clutter_text_set_activatable (CLUTTER_TEXT (retval), TRUE); |
| 225 |
clutter_text_set_single_line_mode (CLUTTER_TEXT (retval), TRUE); |
| 226 |
clutter_text_set_password_char (CLUTTER_TEXT (retval), password_char); |
| 227 |
clutter_text_set_cursor_color (CLUTTER_TEXT (retval), &selection); |
| 228 |
clutter_text_set_max_length (CLUTTER_TEXT (retval), max_length); |
| 229 |
clutter_text_set_selected_text_color (CLUTTER_TEXT (retval), &selected_text); |
| 230 |
clutter_actor_set_background_color (retval, CLUTTER_COLOR_LightGray); |
| 231 |
if (attrs) |
| 232 |
clutter_text_set_attributes (CLUTTER_TEXT (retval), attrs); |
| 233 |
|
| 234 |
g_signal_connect (retval, "activate", |
| 235 |
G_CALLBACK (on_entry_activate), |
| 236 |
NULL); |
| 237 |
g_signal_connect (retval, "captured-event", |
| 238 |
G_CALLBACK (on_captured_event), |
| 239 |
NULL); |
| 240 |
|
| 241 |
return retval; |
| 242 |
} |
| 243 |
|
| 244 |
G_MODULE_EXPORT gint |
| 245 |
test_text_field_main (gint argc, |
| 246 |
gchar **argv) |
| 247 |
{ |
| 248 |
ClutterActor *stage; |
| 249 |
ClutterActor *box, *label, *entry; |
| 250 |
ClutterLayoutManager *table; |
| 251 |
PangoAttrList *entry_attrs; |
| 252 |
|
| 253 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 254 |
return EXIT_FAILURE; |
| 255 |
|
| 256 |
stage = clutter_stage_new (); |
| 257 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Text Fields"); |
| 258 |
clutter_actor_set_background_color (stage, CLUTTER_COLOR_Black); |
| 259 |
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); |
| 260 |
|
| 261 |
table = clutter_table_layout_new (); |
| 262 |
clutter_table_layout_set_column_spacing (CLUTTER_TABLE_LAYOUT (table), 6); |
| 263 |
clutter_table_layout_set_row_spacing (CLUTTER_TABLE_LAYOUT (table), 6); |
| 264 |
|
| 265 |
box = clutter_actor_new (); |
| 266 |
clutter_actor_set_layout_manager (box, table); |
| 267 |
clutter_actor_add_constraint (box, clutter_bind_constraint_new (stage, CLUTTER_BIND_WIDTH, -24.0)); |
| 268 |
clutter_actor_add_constraint (box, clutter_bind_constraint_new (stage, CLUTTER_BIND_HEIGHT, -24.0)); |
| 269 |
clutter_actor_set_position (box, 12, 12); |
| 270 |
clutter_actor_add_child (stage, box); |
| 271 |
|
| 272 |
label = create_label (CLUTTER_COLOR_White, "<b>Input field:</b>"); |
| 273 |
g_object_set (label, "min-width", 150.0, NULL); |
| 274 |
clutter_actor_add_child (box, label); |
| 275 |
clutter_layout_manager_child_set (table, CLUTTER_CONTAINER (box), label, |
| 276 |
"row", 0, |
| 277 |
"column", 0, |
| 278 |
"x-expand", FALSE, |
| 279 |
"y-expand", FALSE, |
| 280 |
NULL); |
| 281 |
|
| 282 |
entry_attrs = pango_attr_list_new (); |
| 283 |
pango_attr_list_insert (entry_attrs, pango_attr_underline_new (PANGO_UNDERLINE_ERROR)); |
| 284 |
pango_attr_list_insert (entry_attrs, pango_attr_underline_color_new (65535, 0, 0)); |
| 285 |
entry = create_entry (CLUTTER_COLOR_Black, "somme misspeeled textt", entry_attrs, 0, 0); |
| 286 |
clutter_actor_add_child (box, entry); |
| 287 |
clutter_layout_manager_child_set (table, CLUTTER_CONTAINER (box), entry, |
| 288 |
"row", 0, |
| 289 |
"column", 1, |
| 290 |
"x-expand", TRUE, |
| 291 |
"x-fill", TRUE, |
| 292 |
"y-expand", FALSE, |
| 293 |
NULL); |
| 294 |
clutter_actor_grab_key_focus (entry); |
| 295 |
|
| 296 |
label = create_label (CLUTTER_COLOR_White, "<b>A very long password field:</b>"); |
| 297 |
clutter_actor_add_child (box, label); |
| 298 |
clutter_layout_manager_child_set (table, CLUTTER_CONTAINER (box), label, |
| 299 |
"row", 1, |
| 300 |
"column", 0, |
| 301 |
"x-expand", FALSE, |
| 302 |
"y-expand", FALSE, |
| 303 |
NULL); |
| 304 |
|
| 305 |
entry = create_entry (CLUTTER_COLOR_Black, "password", NULL, '*', 8); |
| 306 |
clutter_actor_add_child (box, entry); |
| 307 |
clutter_layout_manager_child_set (table, CLUTTER_CONTAINER (box), entry, |
| 308 |
"row", 1, |
| 309 |
"column", 1, |
| 310 |
"x-expand", TRUE, |
| 311 |
"x-fill", TRUE, |
| 312 |
"y-expand", FALSE, |
| 313 |
NULL); |
| 314 |
|
| 315 |
clutter_actor_show (stage); |
| 316 |
|
| 317 |
clutter_main (); |
| 318 |
|
| 319 |
return EXIT_SUCCESS; |
| 320 |
} |
| 321 |
|
| 322 |
G_MODULE_EXPORT const char * |
| 323 |
test_text_field_describe (void) |
| 324 |
{ |
| 325 |
return |
| 326 |
"Text actor single-line and password mode support\n" |
| 327 |
"\n" |
| 328 |
"This test checks the :single-line-mode and :password-char properties of\n" |
| 329 |
"the ClutterText actor, plus the password hint feature and the :max-length\n" |
| 330 |
"property."; |
| 331 |
} |