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.
25 #include "../cspi/spi-private.h"
30 * Screen Review Algorithm Demonstration, Benchmark, and Test-bed.
34 * * there are bugs in the compositing code.
36 * * brute-force algorithm uses no client-side cache; performance mediocre.
38 * * we don't have good ordering information for the toplevel windows,
39 * and our current heuristic is not guaranteed if
40 * the active window is not always on top (i.e. autoraise is
43 * * can't know about "inaccessible" objects that may be obscuring the
44 * accessible windows (inherent to API-based approach).
46 * * text column alignment is crude since it relies on a hard-coded
47 * (and arbitrary) ratio of horizontal pixels to character columns.
48 * For small proportional fonts, overruns are likely to result in
49 * occasional review lines that exceed the standard length.
54 #undef CHUNK_LIST_DEBUG /* define to get list of text chunks before clip */
56 #define BOUNDS_CONTAIN_X_BOUNDS(b, p) ( ((p).x>=(b).x) &&\
57 (((p).x + (p).width) <= \
58 ((b).x + (b).width)) && \
59 ((b).width > 0) && ((b).height > 0))
61 #define BOUNDS_CONTAIN_Y(b, p) (((p)>=(b)->y) && ((p)<=((b)->y + (b)->height))\
62 && ((b)->width > 0) && ((b)->height > 0))
64 #define CHUNK_BOUNDS_BEFORE_START(c, i) ((i) && ((c)->clip_bounds.x < \
67 #define CHUNK_BOUNDS_END_BEFORE_START(c, i) ((i) && \
68 (((c)->clip_bounds.x + \
69 (c)->clip_bounds.width) < \
72 #define CHUNK_BOUNDS_AFTER_END(c, i) ((i) && ((c)->clip_bounds.x >= \
73 ((i)->clip_bounds.x + \
74 (i)->clip_bounds.width)))
76 #define CHUNK_BOUNDS_SPANS_END(c, i) ((i) && (((c)->clip_bounds.x + \
77 (c)->clip_bounds.width) >= \
78 ((i)->clip_bounds.x + \
79 (i)->clip_bounds.width)))
81 * #define CHUNK_BOUNDS_WITHIN(c, i) ((i) && ((c)->clip_bounds.x >= \
82 * (i)->text_bounds.x) && \
83 * (((c)->clip_bounds.x + (c)->clip_bounds.width) \
84 * <= ((i)->text_bounds.x + (i)->text_bounds.width)))
87 #define IS_CLIPPING_CONTAINER(a) ((a) != SPI_ROLE_PAGE_TAB)
89 static void report_screen_review_line (const AccessibleEvent *event, void *user_data);
91 static gint n_elements_traversed = 0;
92 static AccessibleEventListener *mouseclick_listener;
94 typedef struct _BoundaryRect {
99 AccessibleRole role; /* role of last clipping element */
104 typedef struct _TextChunk {
109 BoundaryRect clip_bounds;
110 BoundaryRect text_bounds;
111 BoundaryRect start_char_bounds;
112 BoundaryRect end_char_bounds;
115 typedef struct _ScreenReviewBuffer {
117 } ScreenReviewBuffer;
119 static gboolean isJava = FALSE;
122 main (int argc, char **argv)
129 gdouble elapsed_time;
131 Accessible *application;
136 mouseclick_listener = SPI_createAccessibleEventListener (
137 report_screen_review_line, NULL);
139 SPI_registerGlobalEventListener (mouseclick_listener,
140 "Gtk:GtkWidget:button-press-event");
141 #undef JAVA_TEST_HACK
142 #ifdef JAVA_TEST_HACK /* Only use this to test Java apps */
143 SPI_registerGlobalEventListener (mouseclick_listener,
144 "object:text-caret-moved");
149 putenv ("AT_BRIDGE_SHUTDOWN=1");
152 * TODO: Add a key event listener that calls test_exit, to
153 * deregister and cleanup appropriately.
159 static inline gboolean
160 chunk_bounds_within (TextChunk *chunk, TextChunk *test_chunk)
162 int x1, x2, tx1, tx2;
165 x1 = chunk->clip_bounds.x;
166 x2 = x1 + chunk->clip_bounds.width;
167 tx1 = test_chunk->clip_bounds.x;
168 tx2 = tx1 + test_chunk->clip_bounds.width;
169 gtx1 = (chunk->clip_bounds.x >= test_chunk->clip_bounds.x);
170 ltx2 = (chunk->clip_bounds.x + chunk->clip_bounds.width
171 <= test_chunk->clip_bounds.x + test_chunk->clip_bounds.width);
175 #define CHUNK_BOUNDS_WITHIN(a, b) chunk_bounds_within(a, b)
177 static BoundaryRect **
178 clip_bounds_clone (BoundaryRect *bounds[])
181 BoundaryRect **bounds_clone;
182 bounds_clone = (BoundaryRect **)
183 g_new0 (gpointer, SPI_LAYER_LAST_DEFINED);
184 for (i = 1; i < SPI_LAYER_LAST_DEFINED; ++i) {
185 bounds_clone[i] = g_new0 (BoundaryRect, 1);
186 *bounds_clone[i] = *bounds[i];
192 boundary_clip (BoundaryRect *bounds, BoundaryRect *clipBounds)
194 int x2 = bounds->x + bounds->width;
195 int y2 = bounds->y + bounds->height;
197 fprintf (stderr, "bclip %d-%d, %d-%d; ",
199 clipBounds->x, clipBounds->x+clipBounds->width);
201 bounds->x = MAX (bounds->x, clipBounds->x);
202 bounds->y = MAX (bounds->y, clipBounds->y);
203 x2 = MIN (x2, clipBounds->x + clipBounds->width);
204 y2 = MIN (y2, clipBounds->y + clipBounds->height);
205 bounds->width = MAX (x2 - bounds->x, 0);
206 bounds->height = MAX (y2 - bounds->y, 0);
207 if (!bounds->width || !bounds->height)
208 bounds->isEmpty = TRUE;
209 if (IS_CLIPPING_CONTAINER (bounds->role)) {
210 *clipBounds = *bounds;
213 fprintf (stderr, "%d-%d\n",
214 bounds->x, bounds->x+bounds->width);
219 boundary_xclip_head (BoundaryRect *bounds, BoundaryRect *clipBounds)
222 int cx2 = clipBounds->x + clipBounds->width;
223 if (cx2 < bounds->x) return;
224 x2 = bounds->x + bounds->width;
225 if (cx2 <= x2) bounds->x = cx2;
226 bounds->width = MAX (0, x2 - cx2);
230 boundary_xclip_tail (BoundaryRect *bounds, BoundaryRect *clipBounds)
232 int x2 = bounds->x + bounds->width;
233 if (clipBounds->x > x2) return;
234 bounds->width = MAX (0, clipBounds->x - bounds->x);
238 text_chunk_copy (TextChunk *chunk)
240 TextChunk *copy = g_new0 (TextChunk, 1);
242 if (chunk->string) copy->string = g_strdup (chunk->string);
243 if (copy->source) Accessible_ref (copy->source);
248 text_chunk_tail_clip (TextChunk *bottom, TextChunk *top)
251 fprintf (stderr, "bottom %d-%d, top %d-%d;",
252 bottom->clip_bounds.x,
253 bottom->clip_bounds.x + bottom->clip_bounds.width,
255 top->clip_bounds.x + top->clip_bounds.width);
257 boundary_xclip_tail (&bottom->text_bounds, &top->clip_bounds);
258 boundary_xclip_tail (&bottom->clip_bounds, &top->clip_bounds);
259 bottom->text_bounds.isClipped = TRUE;
260 bottom->clip_bounds.isClipped = TRUE;
262 fprintf (stderr, "result %d-%d\n",
263 bottom->clip_bounds.x,
264 bottom->clip_bounds.x + bottom->clip_bounds.width);
269 text_chunk_head_clip (TextChunk *bottom, TextChunk *top)
272 fprintf (stderr, "bottom %d-%d, top %d-%d;",
273 bottom->clip_bounds.x,
274 bottom->clip_bounds.x + bottom->clip_bounds.width,
276 top->clip_bounds.x + top->clip_bounds.width);
278 boundary_xclip_head (&bottom->text_bounds, &top->clip_bounds);
279 boundary_xclip_head (&bottom->clip_bounds, &top->clip_bounds);
280 bottom->text_bounds.isClipped = TRUE;
281 bottom->clip_bounds.isClipped = TRUE;
283 fprintf (stderr, "result %d-%d\n",
284 bottom->clip_bounds.x,
285 bottom->clip_bounds.x + bottom->clip_bounds.width);
290 text_chunk_split_insert (GList *chunk_list, GList *iter, TextChunk *chunk)
292 TextChunk *iter_chunk = iter->data;
293 TextChunk *iter_copy = text_chunk_copy (iter_chunk);
294 /* TODO: FIXME something is wrong here */
296 fprintf (stderr, "***clip insert of %s into %s\n",
297 chunk->string ? chunk->string : "<null>",
298 iter_chunk->string ? iter_chunk->string : "<null>");
300 chunk_list = g_list_insert_before (chunk_list, iter, iter_copy);
301 text_chunk_tail_clip (iter_copy, chunk);
302 chunk_list = g_list_insert_before (chunk_list, iter, chunk);
303 text_chunk_head_clip (iter_chunk, chunk);
307 /* #define PRINT_CHUNK_DEBUG(a, b, c, d) print_chunk_debug(a, b, c, d) */
309 #define PRINT_CHUNK_DEBUG(a, b, c, d)
311 #ifdef PRINT_CHUNK_DEBUG
313 print_chunk_debug (TextChunk *chunk, char *opname, GList *prev, GList *next)
315 fprintf (stderr, "%sing chunk %s between %s and %s; %d-%d\n",
318 (prev ? ((TextChunk *) prev->data)->string : "<null>"),
319 (next ? ((TextChunk *) next->data)->string : "<null>"),
320 chunk->clip_bounds.x,
321 chunk->text_bounds.x + chunk->text_bounds.width);
326 text_chunk_list_head_clip (GList *text_chunk_list,
330 GList *target, *iter = next, *prev;
332 // if (chunk->string && strlen (chunk->string)) {
334 g_list_insert_before (text_chunk_list, next, chunk);
336 while (iter && CHUNK_BOUNDS_SPANS_END (chunk, (TextChunk *)iter->data)) {
338 fprintf (stderr, "deleting %s\n",
339 ((TextChunk *)iter->data)->string);
344 g_list_delete_link (text_chunk_list, target);
346 if (iter && !CHUNK_BOUNDS_END_BEFORE_START (chunk,
347 (TextChunk *)iter->data)) {
348 text_chunk_head_clip ((TextChunk *)iter->data,
352 !CHUNK_BOUNDS_AFTER_END (
354 (TextChunk *)prev->data)) {
355 text_chunk_tail_clip (
356 (TextChunk *)prev->data,
360 return text_chunk_list;
364 text_chunk_list_clip_and_insert (GList *text_chunk_list,
370 /* if (chunk->string) */
371 fprintf (stderr, "clip-and-insert for %s, between %s (%d) and %s (%d)\n",
373 (prev && ((TextChunk *)prev->data)->string ? ((TextChunk *)prev->data)->string : "<null>"),
374 (prev ? ((TextChunk *)prev->data)->text_bounds.x : -1),
375 (next && ((TextChunk *)next->data)->string ? ((TextChunk *)next->data)->string : "<null>"),
376 (next ? ((TextChunk *)next->data)->text_bounds.x : -1));
379 if (!prev && !next) { /* first element in, no clip needed */
381 g_list_append (text_chunk_list, chunk);
382 PRINT_CHUNK_DEBUG (chunk, "append",
385 } else { /* check for clip with prev */
386 /* if we split the prev */
388 CHUNK_BOUNDS_WITHIN (chunk, (TextChunk *) prev->data)) {
390 text_chunk_split_insert (
394 /* we split the 'next' element */
395 if (CHUNK_BOUNDS_WITHIN (chunk, (TextChunk *)next->data)) {
397 text_chunk_split_insert (text_chunk_list,
400 /* do an insert + head clip */
402 text_chunk_list_head_clip (
408 if (!CHUNK_BOUNDS_AFTER_END (chunk,
409 (TextChunk *) prev->data)) {
410 text_chunk_tail_clip (prev->data, chunk);
413 g_list_append (text_chunk_list, chunk);
416 return text_chunk_list;
420 text_chunk_list_insert_chunk (GList *text_chunk_list, TextChunk *chunk)
422 GList *iter = g_list_first (text_chunk_list);
423 TextChunk *iter_chunk = NULL;
425 if (iter) iter_chunk = (TextChunk *) iter->data;
426 /* if we're ahead of the current element */
429 text_chunk_list_clip_and_insert (text_chunk_list,
434 } else if (CHUNK_BOUNDS_BEFORE_START (chunk, iter_chunk)) {
436 text_chunk_list_clip_and_insert (text_chunk_list,
441 } else if (!iter->next ) {
443 text_chunk_list_clip_and_insert (text_chunk_list,
449 if (iter) iter = iter->next;
451 return text_chunk_list;
455 review_buffer_get_text_chunk (ScreenReviewBuffer *reviewBuffer,
456 Accessible *accessible, BoundaryRect *bounds,
457 int screen_x, int screen_y)
459 AccessibleText *text = NULL;
461 TextChunk *text_chunk;
462 BoundaryRect start_char_bounds, end_char_bounds;
468 role = Accessible_getRole (accessible);
469 text_chunk = g_new0 (TextChunk, 1);
470 text_chunk->clip_bounds = *bounds;
471 text_chunk->source = accessible;
472 Accessible_ref (accessible);
473 if (Accessible_isText (accessible)) {
474 text = Accessible_getText (accessible);
475 offset = AccessibleText_getOffsetAtPoint (text,
478 SPI_COORD_TYPE_SCREEN);
479 s = AccessibleText_getTextAtOffset (text, offset,
480 SPI_TEXT_BOUNDARY_LINE_START,
483 AccessibleText_getCharacterExtents (
485 &text_chunk->start_char_bounds.x,
486 &text_chunk->start_char_bounds.y,
487 &text_chunk->start_char_bounds.width,
488 &text_chunk->start_char_bounds.height,
489 SPI_COORD_TYPE_SCREEN);
491 fprintf (stderr, "%s: start char (%d) x, width %d %d; ",
494 text_chunk->start_char_bounds.x,
495 text_chunk->start_char_bounds.width);
497 if (s && strlen (s) && s[strlen (s) - 1] == '\n')
499 AccessibleText_getCharacterExtents (
501 &text_chunk->end_char_bounds.x,
502 &text_chunk->end_char_bounds.y,
503 &text_chunk->end_char_bounds.width,
504 &text_chunk->end_char_bounds.height,
505 SPI_COORD_TYPE_SCREEN);
507 fprintf (stderr, "end char (%d) x, width %d %d\n",
509 text_chunk->end_char_bounds.x,
510 text_chunk->end_char_bounds.width);
513 text_chunk->text_bounds.x = MIN (text_chunk->start_char_bounds.x,
514 text_chunk->end_char_bounds.x);
515 text_chunk->text_bounds.y = MIN (text_chunk->start_char_bounds.y,
516 text_chunk->end_char_bounds.y);
517 x2 = MAX (text_chunk->start_char_bounds.x +
518 text_chunk->start_char_bounds.width,
519 text_chunk->end_char_bounds.x +
520 text_chunk->end_char_bounds.width);
521 text_chunk->text_bounds.width = x2 - text_chunk->text_bounds.x;
522 y2 = MAX (text_chunk->start_char_bounds.y +
523 text_chunk->start_char_bounds.height,
524 text_chunk->end_char_bounds.y +
525 text_chunk->end_char_bounds.height);
526 text_chunk->text_bounds.height = y2 - text_chunk->text_bounds.y;
527 text_chunk->start_offset = start;
528 text_chunk->end_offset = end;
529 AccessibleText_unref (text);
531 if (role == SPI_ROLE_PUSH_BUTTON ||
532 role == SPI_ROLE_CHECK_BOX ||
533 role == SPI_ROLE_LABEL ||
534 role == SPI_ROLE_MENU ||
535 role == SPI_ROLE_MENU_ITEM) { /* don't like this
536 special casing :-( */
537 s = Accessible_getName (accessible);
538 /* use name instead */
539 text_chunk->text_bounds = text_chunk->clip_bounds;
540 text_chunk->start_offset = 0;
541 text_chunk->end_offset = strlen (s);
544 if (text_chunk->text_bounds.x < text_chunk->clip_bounds.x) {
545 text_chunk->text_bounds.x = text_chunk->clip_bounds.x;
546 text_chunk->text_bounds.isClipped = TRUE;
548 if ((text_chunk->text_bounds.x +
549 text_chunk->text_bounds.width)
550 > (text_chunk->clip_bounds.x +
551 text_chunk->clip_bounds.width)) {
552 text_chunk->text_bounds.width =
553 MAX (0, (text_chunk->clip_bounds.x +
554 text_chunk->clip_bounds.width) -
555 text_chunk->text_bounds.x);
556 text_chunk->text_bounds.isClipped = TRUE;
558 if (!BOUNDS_CONTAIN_Y (&text_chunk->text_bounds,
561 fprintf (stderr, "%s out of bounds (%d-%d)\n", s,
562 text_chunk->text_bounds.y,
563 text_chunk->text_bounds.y +
564 text_chunk->text_bounds.height);
567 text_chunk->start_offset = offset;
568 text_chunk->end_offset = offset;
570 if (s && strlen (s)) {
571 if (s[strlen(s)-1] == '\n') s[strlen(s)-1] = ' ';
572 /* XXX: if last char is newline, aren't its bounds wrong now? */
573 text_chunk->string = s;
575 fprintf (stderr, "%s, bounds %d-%d; clip %d-%d\n",
577 text_chunk->text_bounds.x,
578 text_chunk->text_bounds.x+text_chunk->text_bounds.width,
579 text_chunk->clip_bounds.x,
580 text_chunk->clip_bounds.x+text_chunk->clip_bounds.width);
583 text_chunk->string = NULL;
589 debug_chunk_list (GList *iter)
593 chunk = (TextChunk *)iter->data;
594 fprintf (stderr, "Chunk %s, clip %d-%d, text %d-%d\n",
596 chunk->clip_bounds.x,
597 chunk->clip_bounds.x + chunk->clip_bounds.width,
598 chunk->text_bounds.x,
599 chunk->text_bounds.x + chunk->text_bounds.width);
605 clip_into_buffers (Accessible *accessible, BoundaryRect* parentClipBounds[],
606 ScreenReviewBuffer *reviewBuffers[],
607 int screen_x, int screen_y)
609 int n_children, child_n;
612 BoundaryRect** clip_bounds;
613 TextChunk *text_chunk;
614 AccessibleComponent *component;
618 clip_bounds = clip_bounds_clone (parentClipBounds);
619 if (Accessible_isComponent (accessible)) {
620 role = Accessible_getRole (accessible);
621 component = Accessible_getComponent (accessible);
622 layer = AccessibleComponent_getLayer (component);
623 bounds = *clip_bounds[layer];
624 if (!bounds.isEmpty) {
625 AccessibleComponent_getExtents (component,
630 SPI_COORD_TYPE_SCREEN);
632 if (clip_bounds[layer])
633 boundary_clip (&bounds, clip_bounds[layer]);
634 if (BOUNDS_CONTAIN_Y (&bounds, screen_y)) {
635 text_chunk = review_buffer_get_text_chunk (
636 reviewBuffers[layer], accessible, &bounds,
638 reviewBuffers[layer]->text_chunks =
639 text_chunk_list_insert_chunk (
640 reviewBuffers[layer]->text_chunks,
644 IS_CLIPPING_CONTAINER (bounds.role);
647 Accessible_unref (component);
650 * we always descend into children in case they are in a higher layer
651 * this can of course be optimized for the topmost layer...
652 * but nobody uses that one! (SPI_LAYER_OVERLAY)
654 n_children = Accessible_getChildCount (accessible);
655 for (child_n = 0; child_n < n_children; ++child_n) {
656 child = Accessible_getChildAtIndex (accessible, child_n);
657 clip_into_buffers (child, clip_bounds, reviewBuffers, screen_x, screen_y);
658 Accessible_unref (child);
660 /* TODO: free the parent clip bounds */
663 #undef CHARACTER_CLIP_DEBUG
666 text_chunk_get_clipped_substring_by_char (TextChunk *chunk, int start, int end)
668 BoundaryRect char_bounds;
671 GString *string = g_string_new ("");
673 AccessibleText *text = Accessible_getText (chunk->source);
674 for (i = start; i < end; ++i) {
675 AccessibleText_getCharacterExtents (text,
681 SPI_COORD_TYPE_SCREEN);
682 #ifdef CHARACTER_CLIP_DEBUG
683 fprintf (stderr, "testing %d-%d against %d-%d\n",
684 char_bounds.x, char_bounds.x+char_bounds.width,
685 chunk->text_bounds.x,
686 chunk->text_bounds.x + chunk->text_bounds.width);
688 if (BOUNDS_CONTAIN_X_BOUNDS (chunk->text_bounds,
690 c = AccessibleText_getCharacterAtOffset (
693 fprintf (stderr, "[%c]", c);
695 g_string_append_unichar (string, c);
698 AccessibleText_unref (text);
700 g_string_free (string, FALSE);
706 * Note: this routine shouldn't have to do as much as it currently does,
707 * but at the moment it works around another bug (probably one in this
711 string_strip_newlines (char *s, long offset, long *start_offset, long *end_offset)
714 char *word_start = s;
715 /* FIXME: potential memory leak here */
716 for (i=0; s && s[i]; ++i)
718 if (s [i] == '\n' && i > (offset - *start_offset) ) {
720 *end_offset = *start_offset + i;
722 } else if (s [i] == '\n') {
723 word_start = &s[i + 1];
730 string_guess_clip (TextChunk *chunk)
733 char *s = NULL, *sp = chunk->string, *ep;
734 long start_offset, end_offset, len;
736 AccessibleComponent *component =
737 Accessible_getComponent (chunk->source);
738 ep = sp + (strlen (sp));
739 len = g_utf8_strlen (chunk->string, -1);
741 AccessibleComponent_getExtents (component,
744 SPI_COORD_TYPE_SCREEN);
745 start_offset = len * (chunk->text_bounds.x - b.x) / b.width;
746 end_offset = len * (chunk->text_bounds.x +
747 chunk->text_bounds.width - b.x) / b.width;
748 fprintf (stderr, "String len %d, clipped to %d-%d\n",
749 len, start_offset, end_offset);
750 len = end_offset - start_offset;
751 sp = g_utf8_offset_to_pointer (chunk->string, start_offset);
752 ep = g_utf8_offset_to_pointer (chunk->string, end_offset);
754 s = g_new0 (char, ep - sp + 1);
755 s = g_utf8_strncpy (s, sp, len);
757 g_assert (g_utf8_validate (s, -1, NULL));
763 text_chunk_get_clipped_string (TextChunk *chunk)
765 char *s, *string = "";
767 long start = chunk->start_offset, end = chunk->end_offset;
768 long word_start, word_end, range_end;
769 BoundaryRect start_bounds, end_bounds;
770 gboolean start_inside, end_inside;
771 if (!chunk->text_bounds.isClipped || !chunk->string)
772 string = chunk->string;
773 else if (chunk->source && Accessible_isText (chunk->source)) {
774 /* while words at offset lie within the bounds, add them */
775 AccessibleText *text = Accessible_getText (chunk->source);
777 fprintf (stderr, "clipping %s\n", chunk->string);
780 s = AccessibleText_getTextAtOffset (text,
782 SPI_TEXT_BOUNDARY_WORD_END,
785 range_end = word_end;
786 s = string_strip_newlines (s, start, &word_start, &word_end);
787 AccessibleText_getCharacterExtents (text,
792 &start_bounds.height,
793 SPI_COORD_TYPE_SCREEN);
794 AccessibleText_getCharacterExtents (text,
800 SPI_COORD_TYPE_SCREEN);
801 start_inside = BOUNDS_CONTAIN_X_BOUNDS (chunk->text_bounds,
803 end_inside = BOUNDS_CONTAIN_X_BOUNDS (chunk->text_bounds,
805 if (start_inside && end_inside) {
806 /* word is contained in bounds */
807 string = g_strconcat (string, s, NULL);
808 } else if (start_inside || end_inside) {
809 /* one end of word is in */
810 if (word_end > end) word_end = end;
811 s = text_chunk_get_clipped_substring_by_char (
813 MAX (word_start, chunk->start_offset),
814 MIN (word_end, chunk->end_offset));
815 string = g_strconcat (string, s, NULL);
819 } while (start < chunk->end_offset);
820 } else { /* we're clipped, but don't implement AccessibleText :-( */
821 /* guess for now, maybe we can do better someday */
822 string = string_guess_clip (chunk);
829 text_chunk_pad_string (TextChunk *chunk, char *string, glong offset, const char *pad_chars)
833 char startbuf[6], padbuf[6], endbuf[6];
834 int pixels_per_column = 6;
835 /* this is an arbitrary pixel-to-textcolumn mapping at present */
838 howmany = g_unichar_to_utf8 (g_utf8_get_char (pad_chars), startbuf);
839 startbuf[howmany] = '\0';
840 g_assert (howmany < 7 && howmany > 0);
841 cp = g_utf8_find_next_char (pad_chars, NULL);
842 howmany = g_unichar_to_utf8 (g_utf8_get_char (cp), padbuf);
843 padbuf[howmany] = '\0';
844 g_assert (howmany < 7 && howmany > 0);
845 cp = g_utf8_find_next_char (cp, NULL);
846 howmany = g_unichar_to_utf8 (g_utf8_get_char (cp), endbuf);
847 endbuf[howmany] = '\0';
848 g_assert (howmany < 7 && howmany > 0);
849 end_padding = chunk->clip_bounds.x / pixels_per_column;
850 while (offset < end_padding - 1) {
851 s = g_strconcat (s, padbuf, NULL); /* could be more efficient */
854 s = g_strconcat (s, startbuf, string, NULL);
855 offset += g_utf8_strlen (string, -1) + 1;
856 end_padding = chunk->text_bounds.x / pixels_per_column;
857 while (offset < end_padding) {
858 s = g_strconcat (s, padbuf, NULL); /* could be more efficient */
861 end_padding = (chunk->clip_bounds.x + chunk->clip_bounds.width) /
863 while (offset < end_padding - 1) {
864 s = g_strconcat (s, padbuf, NULL); /* could be more efficient */
867 s = g_strconcat (s, endbuf, NULL);
872 text_chunk_to_string (TextChunk *chunk, glong offset)
876 s = text_chunk_get_clipped_string (chunk);
877 if (chunk->clip_bounds.role == SPI_ROLE_PUSH_BUTTON) {
878 s = text_chunk_pad_string (chunk, s, offset, "[ ]");
879 } else if (chunk->clip_bounds.role == SPI_ROLE_FRAME) {
880 s = text_chunk_pad_string (chunk, s, offset, "| |");
881 } else if (chunk->clip_bounds.role == SPI_ROLE_TEXT) {
882 s = text_chunk_pad_string (chunk, s, offset, "\" \"");
884 s = text_chunk_pad_string (chunk, s, offset, " ");
891 text_chunk_list_to_string (GList *iter)
895 TextChunk *chunk = NULL;
898 chunk = (TextChunk *)iter->data;
900 string = text_chunk_to_string (chunk, g_utf8_strlen (s, -1));
902 s = g_strconcat (s, string, NULL);
906 s = g_strconcat (s, "|", NULL);
910 #define COMPOSITE_DEBUG
913 toplevel_composite (ScreenReviewBuffer *buffers[])
916 GList *chunk_list, *iter;
919 chunk_list = buffers[SPI_LAYER_CANVAS]->text_chunks;
920 for (i = SPI_LAYER_MDI; i < SPI_LAYER_OVERLAY; ++i) {
921 iter = buffers[i]->text_chunks;
922 #ifdef COMPOSITE_DEBUG
923 fprintf (stderr, "layer %d has %d chunks\n",
924 i, g_list_length (iter));
927 chunk = (TextChunk *) iter->data;
929 #ifdef COMPOSITE_DEBUG
930 fprintf (stderr, "inserting chunk <%s>\n",
931 chunk->string ? chunk->string : "<null>");
934 text_chunk_list_insert_chunk (chunk_list,
943 review_buffer_composite (ScreenReviewBuffer *buffers[])
945 /* TODO: FIXME: something is wrong here, compositing fails */
947 GList *chunk_list, *iter;
949 chunk_list = buffers[SPI_LAYER_BACKGROUND]->text_chunks;
950 for (i = 2; i < SPI_LAYER_LAST_DEFINED; ++i) {
951 if (i == SPI_LAYER_WIDGET) i = SPI_LAYER_OVERLAY;
953 * Q: why skip these layers ?
954 * A: since layers WIDGET, MDI, and POPUP have already been
955 * composited into layer CANVAS for each toplevel before this
958 iter = buffers[i]->text_chunks;
960 fprintf (stderr, "layer %d has %d chunks\n",
961 i, g_list_length (iter));
964 chunk = (TextChunk *) iter->data;
967 fprintf (stderr, "inserting chunk <%s>\n",
968 chunk->string ? chunk->string : "<null>");
971 text_chunk_list_insert_chunk (chunk_list,
978 chunk_list = buffers[SPI_LAYER_WIDGET]->text_chunks;
979 return text_chunk_list_to_string (chunk_list);
983 get_screen_review_line_at (int x, int y)
986 Accessible *desktop, *app, *toplevel, *child;
987 AccessibleComponent *component;
988 AccessibleStateSet *states;
989 GList *toplevels = NULL, *actives = NULL, *iter;
990 ScreenReviewBuffer* reviewBuffers[SPI_LAYER_LAST_DEFINED];
991 BoundaryRect* clip_bounds[SPI_LAYER_LAST_DEFINED];
992 BoundaryRect toplevel_bounds;
993 int n_apps, n_toplevels, n_children, app_n, toplevel_n, child_n;
994 GTimer *timer = g_timer_new ();
997 for (i = 1; i < SPI_LAYER_LAST_DEFINED; ++i) {
998 reviewBuffers[i] = g_new0 (ScreenReviewBuffer, 1);
999 clip_bounds[i] = g_new0 (BoundaryRect, 1);
1000 clip_bounds[i]->isClipped = FALSE;
1001 clip_bounds[i]->isEmpty = FALSE;
1004 /* how do we decide which desktop ? */
1005 desktop = SPI_getDesktop (0);
1008 n_apps = Accessible_getChildCount (desktop);
1009 for (app_n = 0; app_n < n_apps; ++app_n) {
1010 /* for each toplevel in app */
1011 app = Accessible_getChildAtIndex (desktop, app_n);
1012 n_toplevels = Accessible_getChildCount (app);
1013 for (toplevel_n = 0; toplevel_n < n_toplevels; ++toplevel_n) {
1014 Accessible *toplevel = Accessible_getChildAtIndex (app, toplevel_n);
1015 if (Accessible_isComponent (toplevel))
1016 toplevels = g_list_prepend (toplevels, toplevel);
1018 Accessible_unref (toplevel);
1019 fprintf (stderr, "warning, app toplevel not a component.\n");
1024 /* sort: at the moment we don't have a good way to sort except to put actives on top */
1025 for (iter = g_list_first (toplevels); iter; iter = iter->next) {
1026 Accessible *toplevel =
1027 (Accessible *) iter->data;
1028 if (AccessibleStateSet_contains (Accessible_getStateSet (toplevel),
1029 SPI_STATE_ACTIVE)) {
1030 actives = g_list_prepend (actives, toplevel);
1034 for (iter = g_list_first (actives); iter; iter = actives->next) {
1035 toplevels = g_list_remove (toplevels, iter->data); /* place at end */
1036 toplevels = g_list_append (toplevels, iter->data);
1038 g_list_free (actives);
1040 /* for each toplevel, ending with the active one(s),
1041 * clip against children, putting results into appropriate charBuffer.
1043 for (iter = g_list_first (toplevels); iter; iter = iter->next) {
1044 toplevel = (Accessible *) iter->data;
1045 if (Accessible_isComponent (toplevel)) {
1046 /* make sure toplevel is visible and not iconified or shaded */
1047 states = Accessible_getStateSet (toplevel);
1048 if (AccessibleStateSet_contains (states, SPI_STATE_VISIBLE)
1049 && !AccessibleStateSet_contains (states, SPI_STATE_ICONIFIED)
1050 || isJava) { /* isJava hack! */
1051 component = Accessible_getComponent (toplevel);
1052 AccessibleComponent_getExtents (component,
1055 &toplevel_bounds.width,
1056 &toplevel_bounds.height,
1057 SPI_COORD_TYPE_SCREEN);
1058 toplevel_bounds.isEmpty = FALSE;
1059 for (i = 1; i < SPI_LAYER_LAST_DEFINED; ++i) {
1060 *clip_bounds[i] = toplevel_bounds;
1062 clip_into_buffers (toplevel, clip_bounds,
1063 reviewBuffers, x, y);
1065 toplevel_composite (reviewBuffers);
1066 #ifdef CHUNK_LIST_DEBUG
1067 fprintf (stderr, "toplevel clip done\n");
1068 debug_chunk_list (reviewBuffers[SPI_LAYER_WIDGET]->text_chunks);
1072 Accessible_unref (toplevel);
1075 string = review_buffer_composite (reviewBuffers);
1077 /* SIMPLE SINGLE-PASS ALGORITHM:*/
1078 /* traverse the tree:
1079 * keep a pointer to outermost instance of each layer
1080 * clip against outermost in same layer
1081 * when this clip occurs, store outermost clipped string in 2d string buffer.
1082 * string buffer may have attributes to mark component bounds, line art,
1083 * or attributes of text being reviewed.
1084 * composite the layers, ignoring NULL chars in the string buffers.
1087 * sibling clip not correct, text may overwrite if siblings intersect onscreen
1088 * length of resulting text buffer may vary!
1091 * no API for ordering toplevels yet, other than knowing which is ACTIVE.
1092 * not much implementation for the LAYER API yet, other than menus.
1094 g_timer_stop (timer);
1095 fprintf (stderr, "elapsed time = %f s\n", g_timer_elapsed (timer, NULL));
1101 report_screen_review_line (const AccessibleEvent *event, void *user_data)
1103 static Display *display = NULL;
1104 int x, y, win_x, win_y;
1105 Window root_return, child_return;
1106 unsigned int mask_return;
1108 if (!display) display = XOpenDisplay (getenv ("DISPLAY"));
1110 * we would prefer to get the x,y info in the above event.
1111 * At the moment we don't get detail params for "toolkit" events,
1112 * so for testing purposes we use XQueryPointer. Actual apps
1113 * probably shouldn't do this.
1115 XQueryPointer (display,
1116 DefaultRootWindow (display),
1117 &root_return, &child_return,
1122 fprintf (stderr, "screen review event %s at %d, %d\n", event->type,
1124 fprintf (stderr, "[%s]\n",
1125 get_screen_review_line_at (x, y));
1131 SPI_deregisterGlobalEventListenerAll (mouseclick_listener);
1132 AccessibleEventListener_unref (mouseclick_listener);