summaryrefslogtreecommitdiff
path: root/src/tests/clutter/accessibility/cally-atktext-example.c
blob: 64a920da57601db586fe575a5bd7aeb6e5f60ff4 (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
/* CALLY - The Clutter Accessibility Implementation Library
 *
 * Copyright (C) 2009 Igalia, S.L.
 *
 * Author: Alejandro PiƱeiro Iglesias <apinheiro@igalia.com>
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <atk/atk.h>
#include <clutter/clutter.h>

#include "cally-examples-util.h"

#define WIDTH 800
#define HEIGHT 600

static ClutterActor *text_actor          = NULL;
static ClutterActor *text_editable_actor = NULL;

/*
 * Test AtkText interface
 */
static void
test_atk_text (ClutterActor *actor)
{
  gchar           *text       = NULL;
  AtkObject       *object     = NULL;
  AtkText         *cally_text = NULL;
  gboolean         boolean    = FALSE;
  gunichar         unichar;
  gint             count      = -1;
  gint             start      = -1;
  gint             end        = -1;
  gint             pos        = -1;
  AtkAttributeSet *at_set     = NULL;
  GSList          *attrs;
  gchar           *buf;
  gint             x, y, width, height;

  object = atk_gobject_accessible_for_object (G_OBJECT (actor));
  cally_text = ATK_TEXT (object);

  if (!cally_text)
      return;

  text = atk_text_get_text (cally_text, 0, -1);
  g_print ("atk_text_get_text output: %s\n", text);

  unichar = atk_text_get_character_at_offset (cally_text, 5);
  buf = g_ucs4_to_utf8 (&unichar, 1, NULL, NULL, NULL);
  g_print ("atk_text_get_character_at_offset(5): '%s' vs '%c'\n", buf, text[5]);
  g_free (text); text = NULL;
  g_free (buf); buf = NULL;

  text = atk_text_get_string_at_offset (cally_text,
                                        5,
                                        ATK_TEXT_GRANULARITY_WORD,
                                        &start, &end);
  g_print ("atk_text_get_string_at_offset: %s, %i, %i\n",
           text, start, end);
  g_free (text); text = NULL;

  pos = atk_text_get_caret_offset (cally_text);
  g_print ("atk_text_get_caret_offset: %i\n", pos);

  atk_text_set_caret_offset (cally_text, 5);

  count = atk_text_get_character_count (cally_text);
  g_print ("atk_text_get_character_count: %i\n", count);

  count = atk_text_get_n_selections (cally_text);
  g_print ("atk_text_get_n_selections: %i\n", count);

  text = atk_text_get_selection (cally_text, 0, &start, &end);
  g_print ("atk_text_get_selection: %s, %i, %i\n", text, start, end);
  g_free(text); text = NULL;

  boolean = atk_text_remove_selection (cally_text, 0);
  g_print ("atk_text_remove_selection (0): %i\n", boolean);

  boolean = atk_text_remove_selection (cally_text, 1);
  g_print ("atk_text_remove_selection (1): %i\n", boolean);

  boolean = atk_text_add_selection (cally_text, 5, 10);
  g_print ("atk_text_add_selection: %i\n", boolean);

  boolean = atk_text_set_selection (cally_text, 0, 6, 10);
  g_print ("atk_text_set_selection: %i\n", boolean);

  at_set = atk_text_get_run_attributes (cally_text, 0,
                                        &start, &end);
  g_print ("atk_text_get_run_attributes: %i, %i\n", start, end);
  attrs = (GSList*) at_set;
  while (attrs)
    {
        AtkAttribute *at = (AtkAttribute *) attrs->data;
        g_print ("text run %s = %s\n", at->name, at->value);
        attrs = g_slist_next (attrs);
    }

  atk_text_get_character_extents (cally_text, 0, &x, &y, &width, &height,
                                  ATK_XY_WINDOW);
  g_print ("atk_text_get_character_extents (0, window): x=%i y=%i width=%i height=%i\n",
           x, y, width, height);

  atk_text_get_character_extents (cally_text, 0, &x, &y, &width, &height,
                                  ATK_XY_SCREEN);
  g_print ("atk_text_get_character_extents (0, screen): x=%i y=%i width=%i height=%i\n",
           x, y, width, height);

  pos = atk_text_get_offset_at_point (cally_text, 200, 10, ATK_XY_WINDOW);
  g_print ("atk_text_get_offset_at_point (200, 10, window): %i\n", pos);

  pos = atk_text_get_offset_at_point (cally_text, 200, 100, ATK_XY_SCREEN);
  g_print ("atk_text_get_offset_at_point (200, 100, screen): %i\n", pos);

}

static void
dump_actor_default_atk_attributes (ClutterActor *actor)
{
  AtkObject       *object     = NULL;
  AtkText         *cally_text = NULL;
  AtkAttributeSet *at_set     = NULL;
  GSList          *attrs;
  const gchar     *text_value = NULL;

  object = atk_gobject_accessible_for_object (G_OBJECT (actor));
  cally_text = ATK_TEXT (object);

  if (!cally_text)
      return;

  text_value = clutter_text_get_text (CLUTTER_TEXT (actor));
  g_print ("text value = %s\n", text_value);

  at_set = atk_text_get_default_attributes (cally_text);
  attrs = (GSList*) at_set;
  while (attrs) {
      AtkAttribute *at = (AtkAttribute *) attrs->data;
      g_print ("text default %s = %s\n", at->name, at->value);
      attrs = g_slist_next (attrs);
  }
}

static gboolean
button_press_cb (ClutterActor *actor,
                 ClutterButtonEvent *event,
                 gpointer data)
{
  test_atk_text (text_actor);
  test_atk_text (text_editable_actor);

  return TRUE;
}

static void
make_ui (ClutterActor *stage)
{
  ClutterColor  color_stage = { 0x00, 0x00, 0x00, 0xff };
  ClutterColor  color_text  = { 0xff, 0x00, 0x00, 0xff };
  ClutterColor  color_sel   = { 0x00, 0xff, 0x00, 0x55 };
  ClutterColor  color_rect  = { 0x00, 0xff, 0xff, 0xff };
  ClutterColor  color_label = { 0x00, 0x00, 0x00, 0xff };
  ClutterActor *button      = NULL;
  ClutterActor *rectangle   = NULL;
  ClutterActor *label       = NULL;

  clutter_actor_set_background_color (CLUTTER_ACTOR (stage), &color_stage);
  clutter_actor_set_size (stage, WIDTH, HEIGHT);

  /* text */
  text_actor = clutter_text_new_full ("Sans Bold 32px",
                                      "",
                                      &color_text);
  clutter_text_set_markup (CLUTTER_TEXT(text_actor),
                           "<span fgcolor=\"#FFFF00\" bgcolor=\"#00FF00\"><s>Lorem ipsum dolor sit amet</s></span>");
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), text_actor);
  dump_actor_default_atk_attributes (text_actor);

  /* text_editable */
  text_editable_actor = clutter_text_new_full ("Sans Bold 32px",
                                               "consectetur adipisicing elit",
                                               &color_text);
  clutter_actor_set_position (text_editable_actor, 20, 100);
  clutter_text_set_editable (CLUTTER_TEXT (text_editable_actor), TRUE);
  clutter_text_set_selectable (CLUTTER_TEXT (text_editable_actor), TRUE);
  clutter_text_set_selection_color (CLUTTER_TEXT (text_editable_actor),
                                    &color_sel);
  clutter_text_set_line_wrap (CLUTTER_TEXT (text_editable_actor), TRUE);
  clutter_actor_grab_key_focus (text_editable_actor);
  clutter_actor_set_reactive (text_editable_actor, TRUE);
  dump_actor_default_atk_attributes (text_editable_actor);

  clutter_container_add_actor (CLUTTER_CONTAINER (stage), text_editable_actor);

  /* test button */
  button = clutter_actor_new ();
  rectangle = clutter_actor_new ();
  clutter_actor_set_background_color (rectangle, &color_rect);
  clutter_actor_set_size (rectangle, 75, 35);

  label = clutter_text_new_full ("Sans Bold 32px",
                                 "Test", &color_label);
  clutter_actor_set_position (button, 100, 200);
  clutter_container_add_actor (CLUTTER_CONTAINER (button), rectangle);
  clutter_container_add_actor (CLUTTER_CONTAINER (button), label);
  clutter_actor_set_reactive (button, TRUE);

  g_signal_connect_after (button, "button-press-event",
                          G_CALLBACK (button_press_cb), NULL);

  clutter_container_add_actor (CLUTTER_CONTAINER (stage), button);
}

int
main (int argc, char *argv[])
{
  ClutterActor *stage;

  g_set_application_name ("AtkText");

  cally_util_a11y_init (&argc, &argv);

  stage = clutter_test_get_stage ();
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Cally - AtkText Test");
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_test_quit), NULL);

  make_ui (stage);

  clutter_actor_show (stage);

  test_atk_text (text_actor);
  test_atk_text (text_editable_actor);

  clutter_test_main ();

  return 0;
}