2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "../cspi/spi-private.h"
31 * Screen Review Algorithm Demonstration, Benchmark, and Test-bed.
35 * * there are bugs in the compositing code.
37 * * brute-force algorithm uses no client-side cache; performance mediocre.
39 * * we don't have good ordering information for the toplevel windows,
40 * and our current heuristic is not guaranteed if
41 * the active window is not always on top (i.e. autoraise is
44 * * can't know about "inaccessible" objects that may be obscuring the
45 * accessible windows (inherent to API-based approach).
47 * * text column alignment is crude since it relies on a hard-coded
48 * (and arbitrary) ratio of horizontal pixels to character columns.
49 * For small proportional fonts, overruns are likely to result in
50 * occasional review lines that exceed the standard length.
55 #undef CHUNK_LIST_DEBUG /* define to get list of text chunks before clip */
57 #define BOUNDS_CONTAIN_X_BOUNDS(b, p) ( ((p).x>=(b).x) &&\
58 (((p).x + (p).width) <= \
59 ((b).x + (b).width)) && \
60 ((b).width > 0) && ((b).height > 0))
62 #define BOUNDS_CONTAIN_Y(b, p) (((p)>=(b)->y) && ((p)<=((b)->y + (b)->height))\
63 && ((b)->width > 0) && ((b)->height > 0))
65 #define CHUNK_BOUNDS_BEFORE_START(c, i) ((i) && ((c)->clip_bounds.x < \
68 #define CHUNK_BOUNDS_END_BEFORE_START(c, i) ((i) && \
69 (((c)->clip_bounds.x + \
70 (c)->clip_bounds.width) < \
73 #define CHUNK_BOUNDS_AFTER_END(c, i) ((i) && ((c)->clip_bounds.x >= \
74 ((i)->clip_bounds.x + \
75 (i)->clip_bounds.width)))
77 #define CHUNK_BOUNDS_SPANS_END(c, i) ((i) && (((c)->clip_bounds.x + \
78 (c)->clip_bounds.width) >= \
79 ((i)->clip_bounds.x + \
80 (i)->clip_bounds.width)))
82 * #define CHUNK_BOUNDS_WITHIN(c, i) ((i) && ((c)->clip_bounds.x >= \
83 * (i)->text_bounds.x) && \
84 * (((c)->clip_bounds.x + (c)->clip_bounds.width) \
85 * <= ((i)->text_bounds.x + (i)->text_bounds.width)))
88 #define IS_CLIPPING_CONTAINER(a) ((a) != SPI_ROLE_PAGE_TAB)
90 static void report_screen_review_line (const AccessibleEvent *event, void *user_data);
92 static gint n_elements_traversed = 0;
93 static AccessibleEventListener *mouseclick_listener;
95 typedef struct _BoundaryRect {
100 AccessibleRole role; /* role of last clipping element */
105 typedef struct _TextChunk {
110 BoundaryRect clip_bounds;
111 BoundaryRect text_bounds;
112 BoundaryRect start_char_bounds;
113 BoundaryRect end_char_bounds;
116 typedef struct _ScreenReviewBuffer {
118 } ScreenReviewBuffer;
120 static gboolean isJava = FALSE;
123 main (int argc, char **argv)
130 gdouble elapsed_time;
132 Accessible *application;
137 mouseclick_listener = SPI_createAccessibleEventListener (
138 report_screen_review_line, NULL);
140 SPI_registerGlobalEventListener (mouseclick_listener,
141 "Gtk:GtkWidget:button-press-event");
142 #undef JAVA_TEST_HACK
143 #ifdef JAVA_TEST_HACK /* Only use this to test Java apps */
144 SPI_registerGlobalEventListener (mouseclick_listener,
145 "object:text-caret-moved");
150 putenv ("AT_BRIDGE_SHUTDOWN=1");
153 * TODO: Add a key event listener that calls test_exit, to
154 * deregister and cleanup appropriately.
160 static inline gboolean
161 chunk_bounds_within (TextChunk *chunk, TextChunk *test_chunk)
163 int x1, x2, tx1, tx2;
166 x1 = chunk->clip_bounds.x;
167 x2 = x1 + chunk->clip_bounds.width;
168 tx1 = test_chunk->clip_bounds.x;
169 tx2 = tx1 + test_chunk->clip_bounds.width;
170 gtx1 = (chunk->clip_bounds.x >= test_chunk->clip_bounds.x);
171 ltx2 = (chunk->clip_bounds.x + chunk->clip_bounds.width
172 <= test_chunk->clip_bounds.x + test_chunk->clip_bounds.width);
176 #define CHUNK_BOUNDS_WITHIN(a, b) chunk_bounds_within(a, b)
178 static BoundaryRect **
179 clip_bounds_clone (BoundaryRect *bounds[])
182 BoundaryRect **bounds_clone;
183 bounds_clone = (BoundaryRect **)
184 g_new0 (gpointer, SPI_LAYER_LAST_DEFINED);
185 for (i = 1; i < SPI_LAYER_LAST_DEFINED; ++i) {
186 bounds_clone[i] = g_new0 (BoundaryRect, 1);
187 *bounds_clone[i] = *bounds[i];
193 boundary_clip (BoundaryRect *bounds, BoundaryRect *clipBounds)
195 int x2 = bounds->x + bounds->width;
196 int y2 = bounds->y + bounds->height;
198 fprintf (stderr, "bclip %d-%d, %d-%d; ",
200 clipBounds->x, clipBounds->x+clipBounds->width);
202 bounds->x = MAX (bounds->x, clipBounds->x);
203 bounds->y = MAX (bounds->y, clipBounds->y);
204 x2 = MIN (x2, clipBounds->x + clipBounds->width);
205 y2 = MIN (y2, clipBounds->y + clipBounds->height);
206 bounds->width = MAX (x2 - bounds->x, 0);
207 bounds->height = MAX (y2 - bounds->y, 0);
208 if (!bounds->width || !bounds->height)
209 bounds->isEmpty = TRUE;
210 if (IS_CLIPPING_CONTAINER (bounds->role)) {
211 *clipBounds = *bounds;
214 fprintf (stderr, "%d-%d\n",
215 bounds->x, bounds->x+bounds->width);
220 boundary_xclip_head (BoundaryRect *bounds, BoundaryRect *clipBounds)
223 int cx2 = clipBounds->x + clipBounds->width;
224 if (cx2 < bounds->x) return;
225 x2 = bounds->x + bounds->width;
226 if (cx2 <= x2) bounds->x = cx2;
227 bounds->width = MAX (0, x2 - cx2);
231 boundary_xclip_tail (BoundaryRect *bounds, BoundaryRect *clipBounds)
233 int x2 = bounds->x + bounds->width;
234 if (clipBounds->x > x2) return;
235 bounds->width = MAX (0, clipBounds->x - bounds->x);
239 text_chunk_copy (TextChunk *chunk)
241 TextChunk *copy = g_new0 (TextChunk, 1);
243 if (chunk->string) copy->string = g_strdup (chunk->string);
244 if (copy->source) Accessible_ref (copy->source);
249 text_chunk_tail_clip (TextChunk *bottom, TextChunk *top)
252 fprintf (stderr, "bottom %d-%d, top %d-%d;",
253 bottom->clip_bounds.x,
254 bottom->clip_bounds.x + bottom->clip_bounds.width,
256 top->clip_bounds.x + top->clip_bounds.width);
258 boundary_xclip_tail (&bottom->text_bounds, &top->clip_bounds);
259 boundary_xclip_tail (&bottom->clip_bounds, &top->clip_bounds);
260 bottom->text_bounds.isClipped = TRUE;
261 bottom->clip_bounds.isClipped = TRUE;
263 fprintf (stderr, "result %d-%d\n",
264 bottom->clip_bounds.x,
265 bottom->clip_bounds.x + bottom->clip_bounds.width);
270 text_chunk_head_clip (TextChunk *bottom, TextChunk *top)
273 fprintf (stderr, "bottom %d-%d, top %d-%d;",
274 bottom->clip_bounds.x,
275 bottom->clip_bounds.x + bottom->clip_bounds.width,
277 top->clip_bounds.x + top->clip_bounds.width);
279 boundary_xclip_head (&bottom->text_bounds, &top->clip_bounds);
280 boundary_xclip_head (&bottom->clip_bounds, &top->clip_bounds);
281 bottom->text_bounds.isClipped = TRUE;
282 bottom->clip_bounds.isClipped = TRUE;
284 fprintf (stderr, "result %d-%d\n",
285 bottom->clip_bounds.x,
286 bottom->clip_bounds.x + bottom->clip_bounds.width);
291 text_chunk_split_insert (GList *chunk_list, GList *iter, TextChunk *chunk)
293 TextChunk *iter_chunk = iter->data;
294 TextChunk *iter_copy = text_chunk_copy (iter_chunk);
295 /* TODO: FIXME something is wrong here */
297 fprintf (stderr, "***clip insert of %s into %s\n",
298 chunk->string ? chunk->string : "<null>",
299 iter_chunk->string ? iter_chunk->string : "<null>");
301 chunk_list = g_list_insert_before (chunk_list, iter, iter_copy);
302 text_chunk_tail_clip (iter_copy, chunk);
303 chunk_list = g_list_insert_before (chunk_list, iter, chunk);
304 text_chunk_head_clip (iter_chunk, chunk);
308 /* #define PRINT_CHUNK_DEBUG(a, b, c, d) print_chunk_debug(a, b, c, d) */
310 #define PRINT_CHUNK_DEBUG(a, b, c, d)
312 #ifdef PRINT_CHUNK_DEBUG
314 print_chunk_debug (TextChunk *chunk, char *opname, GList *prev, GList *next)
316 fprintf (stderr, "%sing chunk %s between %s and %s; %d-%d\n",
319 (prev ? ((TextChunk *) prev->data)->string : "<null>"),
320 (next ? ((TextChunk *) next->data)->string : "<null>"),
321 chunk->clip_bounds.x,
322 chunk->text_bounds.x + chunk->text_bounds.width);
327 text_chunk_list_head_clip (GList *text_chunk_list,
331 GList *target, *iter = next, *prev;
333 // if (chunk->string && strlen (chunk->string)) {
335 g_list_insert_before (text_chunk_list, next, chunk);
337 while (iter && CHUNK_BOUNDS_SPANS_END (chunk, (TextChunk *)iter->data)) {
339 fprintf (stderr, "deleting %s\n",
340 ((TextChunk *)iter->data)->string);
345 g_list_delete_link (text_chunk_list, target);
347 if (iter && !CHUNK_BOUNDS_END_BEFORE_START (chunk,
348 (TextChunk *)iter->data)) {
349 text_chunk_head_clip ((TextChunk *)iter->data,
353 !CHUNK_BOUNDS_AFTER_END (
355 (TextChunk *)prev->data)) {
356 text_chunk_tail_clip (
357 (TextChunk *)prev->data,
361 return text_chunk_list;
365 text_chunk_list_clip_and_insert (GList *text_chunk_list,
371 /* if (chunk->string) */
372 fprintf (stderr, "clip-and-insert for %s, between %s (%d) and %s (%d)\n",
374 (prev && ((TextChunk *)prev->data)->string ? ((TextChunk *)prev->data)->string : "<null>"),
375 (prev ? ((TextChunk *)prev->data)->text_bounds.x : -1),
376 (next && ((TextChunk *)next->data)->string ? ((TextChunk *)next->data)->string : "<null>"),
377 (next ? ((TextChunk *)next->data)->text_bounds.x : -1));
380 if (!prev && !next) { /* first element in, no clip needed */
382 g_list_append (text_chunk_list, chunk);
383 PRINT_CHUNK_DEBUG (chunk, "append",
386 } else { /* check for clip with prev */
387 /* if we split the prev */
389 CHUNK_BOUNDS_WITHIN (chunk, (TextChunk *) prev->data)) {
391 text_chunk_split_insert (
395 /* we split the 'next' element */
396 if (CHUNK_BOUNDS_WITHIN (chunk, (TextChunk *)next->data)) {
398 text_chunk_split_insert (text_chunk_list,
401 /* do an insert + head clip */
403 text_chunk_list_head_clip (
409 if (!CHUNK_BOUNDS_AFTER_END (chunk,
410 (TextChunk *) prev->data)) {
411 text_chunk_tail_clip (prev->data, chunk);
414 g_list_append (text_chunk_list, chunk);
417 return text_chunk_list;
421 text_chunk_list_insert_chunk (GList *text_chunk_list, TextChunk *chunk)
423 GList *iter = g_list_first (text_chunk_list);
424 TextChunk *iter_chunk = NULL;
426 if (iter) iter_chunk = (TextChunk *) iter->data;
427 /* if we're ahead of the current element */
430 text_chunk_list_clip_and_insert (text_chunk_list,
435 } else if (CHUNK_BOUNDS_BEFORE_START (chunk, iter_chunk)) {
437 text_chunk_list_clip_and_insert (text_chunk_list,
442 } else if (!iter->next ) {
444 text_chunk_list_clip_and_insert (text_chunk_list,
450 if (iter) iter = iter->next;
452 return text_chunk_list;
456 review_buffer_get_text_chunk (ScreenReviewBuffer *reviewBuffer,
457 Accessible *accessible, BoundaryRect *bounds,
458 int screen_x, int screen_y)
460 AccessibleText *text = NULL;
462 TextChunk *text_chunk;
463 BoundaryRect start_char_bounds, end_char_bounds;
469 role = Accessible_getRole (accessible);
470 text_chunk = g_new0 (TextChunk, 1);
471 text_chunk->clip_bounds = *bounds;
472 text_chunk->source = accessible;
473 Accessible_ref (accessible);
474 if (Accessible_isText (accessible)) {
475 text = Accessible_getText (accessible);
476 offset = AccessibleText_getOffsetAtPoint (text,
479 SPI_COORD_TYPE_SCREEN);
480 s = AccessibleText_getTextAtOffset (text, offset,
481 SPI_TEXT_BOUNDARY_LINE_START,
484 AccessibleText_getCharacterExtents (
486 &text_chunk->start_char_bounds.x,
487 &text_chunk->start_char_bounds.y,
488 &text_chunk->start_char_bounds.width,
489 &text_chunk->start_char_bounds.height,
490 SPI_COORD_TYPE_SCREEN);
492 fprintf (stderr, "%s: start char (%d) x, width %d %d; ",
495 text_chunk->start_char_bounds.x,
496 text_chunk->start_char_bounds.width);
498 if (s && strlen (s) && s[strlen (s) - 1] == '\n')
500 AccessibleText_getCharacterExtents (
502 &text_chunk->end_char_bounds.x,
503 &text_chunk->end_char_bounds.y,
504 &text_chunk->end_char_bounds.width,
505 &text_chunk->end_char_bounds.height,
506 SPI_COORD_TYPE_SCREEN);
508 fprintf (stderr, "end char (%d) x, width %d %d\n",
510 text_chunk->end_char_bounds.x,
511 text_chunk->end_char_bounds.width);
514 text_chunk->text_bounds.x = MIN (text_chunk->start_char_bounds.x,
515 text_chunk->end_char_bounds.x);
516 text_chunk->text_bounds.y = MIN (text_chunk->start_char_bounds.y,
517 text_chunk->end_char_bounds.y);
518 x2 = MAX (text_chunk->start_char_bounds.x +
519 text_chunk->start_char_bounds.width,
520 text_chunk->end_char_bounds.x +
521 text_chunk->end_char_bounds.width);
522 text_chunk->text_bounds.width = x2 - text_chunk->text_bounds.x;
523 y2 = MAX (text_chunk->start_char_bounds.y +
524 text_chunk->start_char_bounds.height,
525 text_chunk->end_char_bounds.y +
526 text_chunk->end_char_bounds.height);
527 text_chunk->text_bounds.height = y2 - text_chunk->text_bounds.y;
528 text_chunk->start_offset = start;
529 text_chunk->end_offset = end;
530 AccessibleText_unref (text);
532 if (role == SPI_ROLE_PUSH_BUTTON ||
533 role == SPI_ROLE_CHECK_BOX ||
534 role == SPI_ROLE_LABEL ||
535 role == SPI_ROLE_MENU ||
536 role == SPI_ROLE_MENU_ITEM) { /* don't like this
537 special casing :-( */
538 s = Accessible_getName (accessible);
539 /* use name instead */
540 text_chunk->text_bounds = text_chunk->clip_bounds;
541 text_chunk->start_offset = 0;
542 text_chunk->end_offset = strlen (s);
545 if (text_chunk->text_bounds.x < text_chunk->clip_bounds.x) {
546 text_chunk->text_bounds.x = text_chunk->clip_bounds.x;
547 text_chunk->text_bounds.isClipped = TRUE;
549 if ((text_chunk->text_bounds.x +
550 text_chunk->text_bounds.width)
551 > (text_chunk->clip_bounds.x +
552 text_chunk->clip_bounds.width)) {
553 text_chunk->text_bounds.width =
554 MAX (0, (text_chunk->clip_bounds.x +
555 text_chunk->clip_bounds.width) -
556 text_chunk->text_bounds.x);
557 text_chunk->text_bounds.isClipped = TRUE;
559 if (!BOUNDS_CONTAIN_Y (&text_chunk->text_bounds,
562 fprintf (stderr, "%s out of bounds (%d-%d)\n", s,
563 text_chunk->text_bounds.y,
564 text_chunk->text_bounds.y +
565 text_chunk->text_bounds.height);
568 text_chunk->start_offset = offset;
569 text_chunk->end_offset = offset;
571 if (s && strlen (s)) {
572 if (s[strlen(s)-1] == '\n') s[strlen(s)-1] = ' ';
573 /* XXX: if last char is newline, aren't its bounds wrong now? */
574 text_chunk->string = s;
576 fprintf (stderr, "%s, bounds %d-%d; clip %d-%d\n",
578 text_chunk->text_bounds.x,
579 text_chunk->text_bounds.x+text_chunk->text_bounds.width,
580 text_chunk->clip_bounds.x,
581 text_chunk->clip_bounds.x+text_chunk->clip_bounds.width);
584 text_chunk->string = NULL;
590 debug_chunk_list (GList *iter)
594 chunk = (TextChunk *)iter->data;
595 fprintf (stderr, "Chunk %s, clip %d-%d, text %d-%d\n",
597 chunk->clip_bounds.x,
598 chunk->clip_bounds.x + chunk->clip_bounds.width,
599 chunk->text_bounds.x,
600 chunk->text_bounds.x + chunk->text_bounds.width);
606 clip_into_buffers (Accessible *accessible, BoundaryRect* parentClipBounds[],
607 ScreenReviewBuffer *reviewBuffers[],
608 int screen_x, int screen_y)
610 int n_children, child_n;
613 BoundaryRect** clip_bounds;
614 TextChunk *text_chunk;
615 AccessibleComponent *component;
619 clip_bounds = clip_bounds_clone (parentClipBounds);
620 if (Accessible_isComponent (accessible)) {
621 role = Accessible_getRole (accessible);
622 component = Accessible_getComponent (accessible);
623 layer = AccessibleComponent_getLayer (component);
624 bounds = *clip_bounds[layer];
625 if (!bounds.isEmpty) {
626 AccessibleComponent_getExtents (component,
631 SPI_COORD_TYPE_SCREEN);
633 if (clip_bounds[layer])
634 boundary_clip (&bounds, clip_bounds[layer]);
635 if (BOUNDS_CONTAIN_Y (&bounds, screen_y)) {
636 text_chunk = review_buffer_get_text_chunk (
637 reviewBuffers[layer], accessible, &bounds,
639 reviewBuffers[layer]->text_chunks =
640 text_chunk_list_insert_chunk (
641 reviewBuffers[layer]->text_chunks,
645 IS_CLIPPING_CONTAINER (bounds.role);
648 Accessible_unref (component);
651 * we always descend into children in case they are in a higher layer
652 * this can of course be optimized for the topmost layer...
653 * but nobody uses that one! (SPI_LAYER_OVERLAY)
655 n_children = Accessible_getChildCount (accessible);
656 for (child_n = 0; child_n < n_children; ++child_n) {
657 child = Accessible_getChildAtIndex (accessible, child_n);
658 clip_into_buffers (child, clip_bounds, reviewBuffers, screen_x, screen_y);
659 Accessible_unref (child);
661 /* TODO: free the parent clip bounds */
664 #undef CHARACTER_CLIP_DEBUG
667 text_chunk_get_clipped_substring_by_char (TextChunk *chunk, int start, int end)
669 BoundaryRect char_bounds;
672 GString *string = g_string_new ("");
674 AccessibleText *text = Accessible_getText (chunk->source);
675 for (i = start; i < end; ++i) {
676 AccessibleText_getCharacterExtents (text,
682 SPI_COORD_TYPE_SCREEN);
683 #ifdef CHARACTER_CLIP_DEBUG
684 fprintf (stderr, "testing %d-%d against %d-%d\n",
685 char_bounds.x, char_bounds.x+char_bounds.width,
686 chunk->text_bounds.x,
687 chunk->text_bounds.x + chunk->text_bounds.width);
689 if (BOUNDS_CONTAIN_X_BOUNDS (chunk->text_bounds,
691 c = AccessibleText_getCharacterAtOffset (
694 fprintf (stderr, "[%c]", c);
696 g_string_append_unichar (string, c);
699 AccessibleText_unref (text);
701 g_string_free (string, FALSE);
707 * Note: this routine shouldn't have to do as much as it currently does,
708 * but at the moment it works around another bug (probably one in this
712 string_strip_newlines (char *s, long offset, long *start_offset, long *end_offset)
715 char *word_start = s;
716 /* FIXME: potential memory leak here */
717 for (i=0; s && s[i]; ++i)
719 if (s [i] == '\n' && i > (offset - *start_offset) ) {
721 *end_offset = *start_offset + i;
723 } else if (s [i] == '\n') {
724 word_start = &s[i + 1];
731 string_guess_clip (TextChunk *chunk)
734 char *s = NULL, *sp = chunk->string, *ep;
735 long start_offset, end_offset, len;
737 AccessibleComponent *component =
738 Accessible_getComponent (chunk->source);
739 ep = sp + (strlen (sp));
740 len = g_utf8_strlen (chunk->string, -1);
742 AccessibleComponent_getExtents (component,
745 SPI_COORD_TYPE_SCREEN);
746 start_offset = len * (chunk->text_bounds.x - b.x) / b.width;
747 end_offset = len * (chunk->text_bounds.x +
748 chunk->text_bounds.width - b.x) / b.width;
749 fprintf (stderr, "String len %d, clipped to %d-%d\n",
750 len, start_offset, end_offset);
751 len = end_offset - start_offset;
752 sp = g_utf8_offset_to_pointer (chunk->string, start_offset);
753 ep = g_utf8_offset_to_pointer (chunk->string, end_offset);
755 s = g_new0 (char, ep - sp + 1);
756 s = g_utf8_strncpy (s, sp, len);
758 g_assert (g_utf8_validate (s, -1, NULL));
764 text_chunk_get_clipped_string (TextChunk *chunk)
766 char *s, *string = "";
768 long start = chunk->start_offset, end = chunk->end_offset;
769 long word_start, word_end, range_end;
770 BoundaryRect start_bounds, end_bounds;
771 gboolean start_inside, end_inside;
772 if (!chunk->text_bounds.isClipped || !chunk->string)
773 string = chunk->string;
774 else if (chunk->source && Accessible_isText (chunk->source)) {
775 /* while words at offset lie within the bounds, add them */
776 AccessibleText *text = Accessible_getText (chunk->source);
778 fprintf (stderr, "clipping %s\n", chunk->string);
781 s = AccessibleText_getTextAtOffset (text,
783 SPI_TEXT_BOUNDARY_WORD_END,
786 range_end = word_end;
787 s = string_strip_newlines (s, start, &word_start, &word_end);
788 AccessibleText_getCharacterExtents (text,
793 &start_bounds.height,
794 SPI_COORD_TYPE_SCREEN);
795 AccessibleText_getCharacterExtents (text,
801 SPI_COORD_TYPE_SCREEN);
802 start_inside = BOUNDS_CONTAIN_X_BOUNDS (chunk->text_bounds,
804 end_inside = BOUNDS_CONTAIN_X_BOUNDS (chunk->text_bounds,
806 if (start_inside && end_inside) {
807 /* word is contained in bounds */
808 string = g_strconcat (string, s, NULL);
809 } else if (start_inside || end_inside) {
810 /* one end of word is in */
811 if (word_end > end) word_end = end;
812 s = text_chunk_get_clipped_substring_by_char (
814 MAX (word_start, chunk->start_offset),
815 MIN (word_end, chunk->end_offset));
816 string = g_strconcat (string, s, NULL);
820 } while (start < chunk->end_offset);
821 } else { /* we're clipped, but don't implement AccessibleText :-( */
822 /* guess for now, maybe we can do better someday */
823 string = string_guess_clip (chunk);
830 text_chunk_pad_string (TextChunk *chunk, char *string, glong offset, const char *pad_chars)
834 char startbuf[6], padbuf[6], endbuf[6];
835 int pixels_per_column = 6;
836 /* this is an arbitrary pixel-to-textcolumn mapping at present */
839 howmany = g_unichar_to_utf8 (g_utf8_get_char (pad_chars), startbuf);
840 startbuf[howmany] = '\0';
841 g_assert (howmany < 7 && howmany > 0);
842 cp = g_utf8_find_next_char (pad_chars, NULL);
843 howmany = g_unichar_to_utf8 (g_utf8_get_char (cp), padbuf);
844 padbuf[howmany] = '\0';
845 g_assert (howmany < 7 && howmany > 0);
846 cp = g_utf8_find_next_char (cp, NULL);
847 howmany = g_unichar_to_utf8 (g_utf8_get_char (cp), endbuf);
848 endbuf[howmany] = '\0';
849 g_assert (howmany < 7 && howmany > 0);
850 end_padding = chunk->clip_bounds.x / pixels_per_column;
851 while (offset < end_padding - 1) {
852 s = g_strconcat (s, padbuf, NULL); /* could be more efficient */
855 s = g_strconcat (s, startbuf, string, NULL);
856 offset += g_utf8_strlen (string, -1) + 1;
857 end_padding = chunk->text_bounds.x / pixels_per_column;
858 while (offset < end_padding) {
859 s = g_strconcat (s, padbuf, NULL); /* could be more efficient */
862 end_padding = (chunk->clip_bounds.x + chunk->clip_bounds.width) /
864 while (offset < end_padding - 1) {
865 s = g_strconcat (s, padbuf, NULL); /* could be more efficient */
868 s = g_strconcat (s, endbuf, NULL);
873 text_chunk_to_string (TextChunk *chunk, glong offset)
877 s = text_chunk_get_clipped_string (chunk);
878 if (chunk->clip_bounds.role == SPI_ROLE_PUSH_BUTTON) {
879 s = text_chunk_pad_string (chunk, s, offset, "[ ]");
880 } else if (chunk->clip_bounds.role == SPI_ROLE_FRAME) {
881 s = text_chunk_pad_string (chunk, s, offset, "| |");
882 } else if (chunk->clip_bounds.role == SPI_ROLE_TEXT) {
883 s = text_chunk_pad_string (chunk, s, offset, "\" \"");
885 s = text_chunk_pad_string (chunk, s, offset, " ");
892 text_chunk_list_to_string (GList *iter)
896 TextChunk *chunk = NULL;
899 chunk = (TextChunk *)iter->data;
901 string = text_chunk_to_string (chunk, g_utf8_strlen (s, -1));
903 s = g_strconcat (s, string, NULL);
907 s = g_strconcat (s, "|", NULL);
911 #define COMPOSITE_DEBUG
914 toplevel_composite (ScreenReviewBuffer *buffers[])
917 GList *chunk_list, *iter;
920 chunk_list = buffers[SPI_LAYER_CANVAS]->text_chunks;
921 for (i = SPI_LAYER_MDI; i < SPI_LAYER_OVERLAY; ++i) {
922 iter = buffers[i]->text_chunks;
923 #ifdef COMPOSITE_DEBUG
924 fprintf (stderr, "layer %d has %d chunks\n",
925 i, g_list_length (iter));
928 chunk = (TextChunk *) iter->data;
930 #ifdef COMPOSITE_DEBUG
931 fprintf (stderr, "inserting chunk <%s>\n",
932 chunk->string ? chunk->string : "<null>");
935 text_chunk_list_insert_chunk (chunk_list,
944 review_buffer_composite (ScreenReviewBuffer *buffers[])
946 /* TODO: FIXME: something is wrong here, compositing fails */
948 GList *chunk_list, *iter;
950 chunk_list = buffers[SPI_LAYER_BACKGROUND]->text_chunks;
951 for (i = 2; i < SPI_LAYER_LAST_DEFINED; ++i) {
952 if (i == SPI_LAYER_WIDGET) i = SPI_LAYER_OVERLAY;
954 * Q: why skip these layers ?
955 * A: since layers WIDGET, MDI, and POPUP have already been
956 * composited into layer CANVAS for each toplevel before this
959 iter = buffers[i]->text_chunks;
961 fprintf (stderr, "layer %d has %d chunks\n",
962 i, g_list_length (iter));
965 chunk = (TextChunk *) iter->data;
968 fprintf (stderr, "inserting chunk <%s>\n",
969 chunk->string ? chunk->string : "<null>");
972 text_chunk_list_insert_chunk (chunk_list,
979 chunk_list = buffers[SPI_LAYER_WIDGET]->text_chunks;
980 return text_chunk_list_to_string (chunk_list);
984 get_screen_review_line_at (int x, int y)
987 Accessible *desktop, *app, *toplevel, *child;
988 AccessibleComponent *component;
989 AccessibleStateSet *states;
990 GList *toplevels = NULL, *actives = NULL, *iter;
991 ScreenReviewBuffer* reviewBuffers[SPI_LAYER_LAST_DEFINED];
992 BoundaryRect* clip_bounds[SPI_LAYER_LAST_DEFINED];
993 BoundaryRect toplevel_bounds;
994 int n_apps, n_toplevels, n_children, app_n, toplevel_n, child_n;
995 GTimer *timer = g_timer_new ();
998 for (i = 1; i < SPI_LAYER_LAST_DEFINED; ++i) {
999 reviewBuffers[i] = g_new0 (ScreenReviewBuffer, 1);
1000 clip_bounds[i] = g_new0 (BoundaryRect, 1);
1001 clip_bounds[i]->isClipped = FALSE;
1002 clip_bounds[i]->isEmpty = FALSE;
1005 /* how do we decide which desktop ? */
1006 desktop = SPI_getDesktop (0);
1009 n_apps = Accessible_getChildCount (desktop);
1010 for (app_n = 0; app_n < n_apps; ++app_n) {
1011 /* for each toplevel in app */
1012 app = Accessible_getChildAtIndex (desktop, app_n);
1013 n_toplevels = Accessible_getChildCount (app);
1014 for (toplevel_n = 0; toplevel_n < n_toplevels; ++toplevel_n) {
1015 Accessible *toplevel = Accessible_getChildAtIndex (app, toplevel_n);
1016 if (Accessible_isComponent (toplevel))
1017 toplevels = g_list_prepend (toplevels, toplevel);
1019 Accessible_unref (toplevel);
1020 fprintf (stderr, "warning, app toplevel not a component.\n");
1025 /* sort: at the moment we don't have a good way to sort except to put actives on top */
1026 for (iter = g_list_first (toplevels); iter; iter = iter->next) {
1027 Accessible *toplevel =
1028 (Accessible *) iter->data;
1029 if (AccessibleStateSet_contains (Accessible_getStateSet (toplevel),
1030 SPI_STATE_ACTIVE)) {
1031 actives = g_list_prepend (actives, toplevel);
1035 for (iter = g_list_first (actives); iter; iter = actives->next) {
1036 toplevels = g_list_remove (toplevels, iter->data); /* place at end */
1037 toplevels = g_list_append (toplevels, iter->data);
1039 g_list_free (actives);
1041 /* for each toplevel, ending with the active one(s),
1042 * clip against children, putting results into appropriate charBuffer.
1044 for (iter = g_list_first (toplevels); iter; iter = iter->next) {
1045 toplevel = (Accessible *) iter->data;
1046 if (Accessible_isComponent (toplevel)) {
1047 /* make sure toplevel is visible and not iconified or shaded */
1048 states = Accessible_getStateSet (toplevel);
1049 if (AccessibleStateSet_contains (states, SPI_STATE_VISIBLE)
1050 && !AccessibleStateSet_contains (states, SPI_STATE_ICONIFIED)
1051 || isJava) { /* isJava hack! */
1052 component = Accessible_getComponent (toplevel);
1053 AccessibleComponent_getExtents (component,
1056 &toplevel_bounds.width,
1057 &toplevel_bounds.height,
1058 SPI_COORD_TYPE_SCREEN);
1059 toplevel_bounds.isEmpty = FALSE;
1060 for (i = 1; i < SPI_LAYER_LAST_DEFINED; ++i) {
1061 *clip_bounds[i] = toplevel_bounds;
1063 clip_into_buffers (toplevel, clip_bounds,
1064 reviewBuffers, x, y);
1066 toplevel_composite (reviewBuffers);
1067 #ifdef CHUNK_LIST_DEBUG
1068 fprintf (stderr, "toplevel clip done\n");
1069 debug_chunk_list (reviewBuffers[SPI_LAYER_WIDGET]->text_chunks);
1073 Accessible_unref (toplevel);
1076 string = review_buffer_composite (reviewBuffers);
1078 /* SIMPLE SINGLE-PASS ALGORITHM:*/
1079 /* traverse the tree:
1080 * keep a pointer to outermost instance of each layer
1081 * clip against outermost in same layer
1082 * when this clip occurs, store outermost clipped string in 2d string buffer.
1083 * string buffer may have attributes to mark component bounds, line art,
1084 * or attributes of text being reviewed.
1085 * composite the layers, ignoring NULL chars in the string buffers.
1088 * sibling clip not correct, text may overwrite if siblings intersect onscreen
1089 * length of resulting text buffer may vary!
1092 * no API for ordering toplevels yet, other than knowing which is ACTIVE.
1093 * not much implementation for the LAYER API yet, other than menus.
1095 g_timer_stop (timer);
1096 fprintf (stderr, "elapsed time = %f s\n", g_timer_elapsed (timer, NULL));
1102 report_screen_review_line (const AccessibleEvent *event, void *user_data)
1104 static Display *display = NULL;
1105 int x, y, win_x, win_y;
1106 Window root_return, child_return;
1107 unsigned int mask_return;
1109 if (!display) display = XOpenDisplay (getenv ("DISPLAY"));
1111 * we would prefer to get the x,y info in the above event.
1112 * At the moment we don't get detail params for "toolkit" events,
1113 * so for testing purposes we use XQueryPointer. Actual apps
1114 * probably shouldn't do this.
1116 XQueryPointer (display,
1117 DefaultRootWindow (display),
1118 &root_return, &child_return,
1123 fprintf (stderr, "screen review event %s at %d, %d\n", event->type,
1125 fprintf (stderr, "[%s]\n",
1126 get_screen_review_line_at (x, y));
1132 SPI_deregisterGlobalEventListenerAll (mouseclick_listener);
1133 AccessibleEventListener_unref (mouseclick_listener);