Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-form-field-accessible.c
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2014 Igalia
5  * Author: Joanmarie Diggs <jdiggs@igalia.com>
6  *
7  * Evince is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Evince is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include <config.h>
23 
25 #include "ev-view-private.h"
26 
31 
32  gchar *name;
34  gint end_index;
35 
36  AtkStateSet *saved_states;
37 };
38 
39 static void ev_form_field_accessible_component_iface_init (AtkComponentIface *iface);
40 
41 G_DEFINE_TYPE_WITH_CODE (EvFormFieldAccessible, ev_form_field_accessible, ATK_TYPE_OBJECT,
42  G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, ev_form_field_accessible_component_iface_init))
43 
44 static void
45 ev_form_field_accessible_get_extents (AtkComponent *atk_component,
46  gint *x,
47  gint *y,
48  gint *width,
49  gint *height,
50  AtkCoordType coord_type)
51 {
53  EvViewAccessible *view_accessible;
54  gint page;
55  EvRectangle atk_rect;
56 
57  self = EV_FORM_FIELD_ACCESSIBLE (atk_component);
58  view_accessible = ev_page_accessible_get_view_accessible (self->priv->page);
59  page = ev_page_accessible_get_page (self->priv->page);
60  _transform_doc_rect_to_atk_rect (view_accessible, page, &self->priv->area, &atk_rect, coord_type);
61  *x = atk_rect.x1;
62  *y = atk_rect.y1;
63  *width = atk_rect.x2 - atk_rect.x1;
64  *height = atk_rect.y2 - atk_rect.y1;
65 }
66 
67 static gboolean
68 ev_form_field_accessible_grab_focus (AtkComponent *atk_component)
69 {
71  EvView *view;
72 
73  self = EV_FORM_FIELD_ACCESSIBLE (atk_component);
74  view = ev_page_accessible_get_view (self->priv->page);
75  _ev_view_focus_form_field (view, self->priv->form_field);
76 
77  return TRUE;
78 }
79 
80 static void
82 {
83  iface->get_extents = ev_form_field_accessible_get_extents;
84  iface->grab_focus = ev_form_field_accessible_grab_focus;
85 }
86 
87 static gboolean
88 get_indices_in_parent (AtkObject *atk_object,
89  gint *start,
90  gint *end)
91 {
93  EvView *view;
94  EvRectangle *areas = NULL;
95  guint n_areas = 0;
96  gint last_zero_sized_index = -1;
97  gint i;
98 
99  priv = EV_FORM_FIELD_ACCESSIBLE (atk_object)->priv;
100  if (priv->start_index != -1 && priv->end_index != -1) {
101  *start = priv->start_index;
102  *end = priv->end_index;
103  return TRUE;
104  }
105 
106  view = ev_page_accessible_get_view (priv->page);
107  if (!view->page_cache)
108  return FALSE;
109 
112  &areas, &n_areas);
113  if (!areas)
114  return FALSE;
115 
116  for (i = 0; i < n_areas; i++) {
117  EvRectangle *rect = areas + i;
118  gdouble c_x, c_y;
119 
120  c_x = rect->x1 + (rect->x2 - rect->x1) / 2.;
121  c_y = rect->y1 + (rect->y2 - rect->y1) / 2.;
122 
123  if (c_x >= priv->area.x1 && c_x <= priv->area.x2 &&
124  c_y >= priv->area.y1 && c_y <= priv->area.y2) {
125  priv->start_index = i;
126  break;
127  }
128  }
129 
130  if (priv->start_index == -1)
131  return FALSE;
132 
133  for (i = priv->start_index + 1; i < n_areas; i++) {
134  EvRectangle *rect = areas + i;
135  gdouble c_x, c_y;
136 
137  /* A zero-sized text rect suggests a line break. If it is within the text of the
138  * field, we want to preserve it; if it is the character immediately after, we
139  * do not. We won't know which it is until we find the first text rect that is
140  * outside of the area occupied by the field.
141  */
142  if (rect->y1 == rect->y2) {
143  last_zero_sized_index = i;
144  continue;
145  }
146 
147  c_x = rect->x1 + (rect->x2 - rect->x1) / 2.;
148  c_y = rect->y1 + (rect->y2 - rect->y1) / 2.;
149 
150  if (c_x < priv->area.x1 || c_x > priv->area.x2 ||
151  c_y < priv->area.y1 || c_y > priv->area.y2) {
152  priv->end_index = last_zero_sized_index + 1 == i ? i - 1 : i;
153  break;
154  }
155  }
156 
157  if (priv->end_index == -1)
158  return FALSE;
159 
160  *start = priv->start_index;
161  *end = priv->end_index;
162  return TRUE;
163 }
164 
165 static gchar *
166 get_text_under_element (AtkObject *atk_object)
167 {
168  gint start = -1;
169  gint end = -1;
170 
171  if (get_indices_in_parent (atk_object, &start, &end) && start != end)
172  return atk_text_get_text (ATK_TEXT (atk_object_get_parent (atk_object)), start, end);
173 
174  return NULL;
175 }
176 
177 static const gchar *
178 ev_form_field_accessible_get_name (AtkObject *atk_object)
179 {
181 
182  priv = EV_FORM_FIELD_ACCESSIBLE (atk_object)->priv;
183  if (priv->name)
184  return priv->name;
185 
186  if (EV_IS_FORM_FIELD_BUTTON (priv->form_field)) {
188 
189  if (button->type == EV_FORM_FIELD_BUTTON_PUSH)
190  priv->name = get_text_under_element (atk_object);
191  }
192 
193  return priv->name;
194 }
195 
196 static AtkObject *
197 ev_form_field_accessible_get_parent (AtkObject *atk_object)
198 {
199  EvFormFieldAccessiblePrivate *priv = EV_FORM_FIELD_ACCESSIBLE (atk_object)->priv;
200 
201  return ATK_OBJECT (priv->page);
202 }
203 
204 static AtkRole
205 ev_form_field_accessible_get_role (AtkObject *atk_object)
206 {
207  EvFormField *ev_form_field;
208 
209  ev_form_field = EV_FORM_FIELD_ACCESSIBLE (atk_object)->priv->form_field;
210  if (EV_IS_FORM_FIELD_BUTTON (ev_form_field)) {
211  EvFormFieldButton *field_button = EV_FORM_FIELD_BUTTON (ev_form_field);
212 
213  switch (field_button->type) {
215  return ATK_ROLE_CHECK_BOX;
217  return ATK_ROLE_RADIO_BUTTON;
219  return ATK_ROLE_PUSH_BUTTON;
220  default:
221  return ATK_ROLE_UNKNOWN;
222  }
223  }
224 
225  if (EV_IS_FORM_FIELD_CHOICE (ev_form_field)) {
226  EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (ev_form_field);
227 
228  switch (field_choice->type) {
230  return ATK_ROLE_COMBO_BOX;
232  return ATK_ROLE_LIST_BOX;
233  default:
234  return ATK_ROLE_UNKNOWN;
235  }
236  }
237 
238  if (EV_IS_FORM_FIELD_TEXT (ev_form_field)) {
239  EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (ev_form_field);
240 
241  if (field_text->is_password)
242  return ATK_ROLE_PASSWORD_TEXT;
243  else if (field_text->type == EV_FORM_FIELD_TEXT_MULTILINE)
244  return ATK_ROLE_TEXT;
245  else
246  return ATK_ROLE_ENTRY;
247  }
248 
249  return ATK_ROLE_UNKNOWN;
250 }
251 
252 static AtkStateSet *
254 {
255  AtkStateSet *state_set;
256  AtkStateSet *copy_set;
257  AtkStateSet *page_accessible_state_set;
258  EvFormFieldAccessible *self;
259  EvFormField *ev_form_field;
260  EvViewAccessible *view_accessible;
261  gint page;
262 
263  self = EV_FORM_FIELD_ACCESSIBLE (atk_object);
264  state_set = ATK_OBJECT_CLASS (ev_form_field_accessible_parent_class)->ref_state_set (atk_object);
265  atk_state_set_clear_states (state_set);
266 
267  page_accessible_state_set = atk_object_ref_state_set (ATK_OBJECT (self->priv->page));
268  copy_set = atk_state_set_or_sets (state_set, page_accessible_state_set);
269 
270  view_accessible = ev_page_accessible_get_view_accessible (self->priv->page);
271  page = ev_page_accessible_get_page (self->priv->page);
272  if (!ev_view_accessible_is_doc_rect_showing (view_accessible, page, &self->priv->area))
273  atk_state_set_remove_state (copy_set, ATK_STATE_SHOWING);
274 
275  ev_form_field = EV_FORM_FIELD_ACCESSIBLE (atk_object)->priv->form_field;
276  if (EV_IS_FORM_FIELD_BUTTON (ev_form_field)) {
277  EvFormFieldButton *field_button = EV_FORM_FIELD_BUTTON (ev_form_field);
278 
279  if (field_button->state) {
280  if (field_button->type == EV_FORM_FIELD_BUTTON_PUSH)
281  atk_state_set_add_state (copy_set, ATK_STATE_PRESSED);
282  else
283  atk_state_set_add_state (copy_set, ATK_STATE_CHECKED);
284  }
285  }
286 
287  else if (EV_IS_FORM_FIELD_CHOICE (ev_form_field)) {
288  EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (ev_form_field);
289 
290  if (field_choice->is_editable && !ev_form_field->is_read_only)
291  atk_state_set_add_state (copy_set, ATK_STATE_EDITABLE);
292  if (field_choice->multi_select)
293  atk_state_set_add_state (copy_set, ATK_STATE_MULTISELECTABLE);
294  }
295 
296  else if (EV_IS_FORM_FIELD_TEXT (ev_form_field)) {
297  EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (ev_form_field);
298 
299  if (!ev_form_field->is_read_only)
300  atk_state_set_add_state (copy_set, ATK_STATE_EDITABLE);
301  if (field_text->type == EV_FORM_FIELD_TEXT_NORMAL)
302  atk_state_set_add_state (copy_set, ATK_STATE_SINGLE_LINE);
303  else if (field_text->type == EV_FORM_FIELD_TEXT_MULTILINE)
304  atk_state_set_add_state (copy_set, ATK_STATE_MULTI_LINE);
305  }
306 
307  g_object_unref (state_set);
308  g_object_unref (page_accessible_state_set);
309 
310  return copy_set;
311 }
312 
313 void
315 {
316  AtkObject *atk_object;
317  AtkStateSet *states;
318  AtkStateSet *changed_states;
319  gint i;
320 
321  atk_object = ATK_OBJECT (accessible);
322  states = ev_form_field_accessible_ref_state_set (atk_object);
323  changed_states = atk_state_set_xor_sets (accessible->priv->saved_states, states);
324  if (changed_states && !atk_state_set_is_empty (accessible->priv->saved_states)) {
325  for (i = 0; i < ATK_STATE_LAST_DEFINED; i++) {
326  if (atk_state_set_contains_state (changed_states, i))
327  atk_object_notify_state_change (atk_object, i, atk_state_set_contains_state (states, i));
328  }
329  }
330 
331  g_object_unref (accessible->priv->saved_states);
332 
333  atk_state_set_clear_states (changed_states);
334  accessible->priv->saved_states = atk_state_set_or_sets (changed_states, states);
335 
336  g_object_unref (changed_states);
337  g_object_unref (states);
338 }
339 
340 static void
342 {
344 
345  g_object_unref (priv->form_field);
346  g_free (priv->name);
347  g_object_unref (priv->saved_states);
348 
349  G_OBJECT_CLASS (ev_form_field_accessible_parent_class)->finalize (object);
350 }
351 
352 static void
354 {
355  GObjectClass *object_class = G_OBJECT_CLASS (klass);
356  AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
357 
358  object_class->finalize = ev_form_field_accessible_finalize;
359  atk_class->get_name = ev_form_field_accessible_get_name;
360  atk_class->get_parent = ev_form_field_accessible_get_parent;
361  atk_class->get_role = ev_form_field_accessible_get_role;
362  atk_class->ref_state_set = ev_form_field_accessible_ref_state_set;
363 
364  g_type_class_add_private (klass, sizeof (EvFormFieldAccessiblePrivate));
365 }
366 
367 static void
369 {
370  accessible->priv = G_TYPE_INSTANCE_GET_PRIVATE (accessible, EV_TYPE_FORM_FIELD_ACCESSIBLE, EvFormFieldAccessiblePrivate);
371  accessible->priv->start_index = -1;
372  accessible->priv->end_index = -1;
373 }
374 
377  EvFormField *form_field,
378  EvRectangle *area)
379 {
380  EvFormFieldAccessible *atk_form_field;
381 
382  atk_form_field = g_object_new (EV_TYPE_FORM_FIELD_ACCESSIBLE, NULL);
383  atk_form_field->priv->page = page;
384  atk_form_field->priv->form_field = g_object_ref (form_field);
385  atk_form_field->priv->area = *area;
386  atk_form_field->priv->saved_states = atk_state_set_new ();
387  ev_form_field_accessible_update_state (atk_form_field);
388 
389  return EV_FORM_FIELD_ACCESSIBLE (atk_form_field);
390 }
391 
392 EvFormField *
394 {
395  return accessible->priv->form_field;
396 }