2 * Copyright (C) 2008 Nuanti Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "AXObjectCache.h"
23 #if HAVE(ACCESSIBILITY)
25 #include "AccessibilityObject.h"
26 #include "AccessibilityRenderObject.h"
29 #include "HTMLSelectElement.h"
31 #include "TextIterator.h"
32 #include "WebKitAccessibleWrapperAtk.h"
33 #include <wtf/gobject/GOwnPtr.h>
34 #include <wtf/text/CString.h>
38 void AXObjectCache::detachWrapper(AccessibilityObject* obj)
40 webkitAccessibleDetach(WEBKIT_ACCESSIBLE(obj->wrapper()));
43 void AXObjectCache::attachWrapper(AccessibilityObject* obj)
45 AtkObject* atkObj = ATK_OBJECT(webkitAccessibleNew(obj));
46 obj->setWrapper(atkObj);
47 g_object_unref(atkObj);
50 static AccessibilityObject* getListObject(AccessibilityObject* object)
52 // Only list boxes and menu lists supported so far.
53 if (!object->isListBox() && !object->isMenuList())
56 // For list boxes the list object is just itself.
57 if (object->isListBox())
60 // For menu lists we need to return the first accessible child,
61 // with role MenuListPopupRole, since that's the one holding the list
62 // of items with role MenuListOptionRole.
63 AccessibilityObject::AccessibilityChildrenVector children = object->children();
67 AccessibilityObject* listObject = children.at(0).get();
68 if (!listObject->isMenuListPopup())
74 static void notifyChildrenSelectionChange(AccessibilityObject* object)
76 // This static variables are needed to keep track of the old
77 // focused object and its associated list object, as per previous
78 // calls to this function, in order to properly decide whether to
79 // emit some signals or not.
80 DEFINE_STATIC_LOCAL(RefPtr<AccessibilityObject>, oldListObject, ());
81 DEFINE_STATIC_LOCAL(RefPtr<AccessibilityObject>, oldFocusedObject, ());
83 // Only list boxes and menu lists supported so far.
84 if (!object || !(object->isListBox() || object->isMenuList()))
87 // Only support HTML select elements so far (ARIA selectors not supported).
88 Node* node = object->node();
89 if (!node || !node->hasTagName(HTMLNames::selectTag))
92 // Emit signal from the listbox's point of view first.
93 g_signal_emit_by_name(object->wrapper(), "selection-changed");
95 // Find the item where the selection change was triggered from.
96 HTMLSelectElement* select = toHTMLSelectElement(node);
99 int changedItemIndex = select->activeSelectionStartListIndex();
101 AccessibilityObject* listObject = getListObject(object);
107 AccessibilityObject::AccessibilityChildrenVector items = listObject->children();
108 if (changedItemIndex < 0 || changedItemIndex >= static_cast<int>(items.size()))
110 AccessibilityObject* item = items.at(changedItemIndex).get();
112 // Ensure the current list object is the same than the old one so
113 // further comparisons make sense. Otherwise, just reset
114 // oldFocusedObject so it won't be taken into account.
115 if (oldListObject != listObject)
116 oldFocusedObject = 0;
118 AtkObject* axItem = item ? item->wrapper() : 0;
119 AtkObject* axOldFocusedObject = oldFocusedObject ? oldFocusedObject->wrapper() : 0;
121 // Old focused object just lost focus, so emit the events.
122 if (axOldFocusedObject && axItem != axOldFocusedObject) {
123 g_signal_emit_by_name(axOldFocusedObject, "focus-event", false);
124 g_signal_emit_by_name(axOldFocusedObject, "state-change", "focused", false);
127 // Emit needed events for the currently (un)selected item.
129 bool isSelected = item->isSelected();
130 g_signal_emit_by_name(axItem, "state-change", "selected", isSelected);
131 g_signal_emit_by_name(axItem, "focus-event", isSelected);
132 g_signal_emit_by_name(axItem, "state-change", "focused", isSelected);
135 // Update pointers to the previously involved objects.
136 oldListObject = listObject;
137 oldFocusedObject = item;
140 void AXObjectCache::postPlatformNotification(AccessibilityObject* coreObject, AXNotification notification)
142 AtkObject* axObject = coreObject->wrapper();
146 if (notification == AXCheckedStateChanged) {
147 if (!coreObject->isCheckboxOrRadio())
149 g_signal_emit_by_name(axObject, "state-change", "checked", coreObject->isChecked());
150 } else if (notification == AXSelectedChildrenChanged || notification == AXMenuListValueChanged) {
151 if (notification == AXMenuListValueChanged && coreObject->isMenuList()) {
152 g_signal_emit_by_name(axObject, "focus-event", true);
153 g_signal_emit_by_name(axObject, "state-change", "focused", true);
155 notifyChildrenSelectionChange(coreObject);
156 } else if (notification == AXValueChanged) {
157 if (!ATK_IS_VALUE(axObject))
160 AtkPropertyValues propertyValues;
161 propertyValues.property_name = "accessible-value";
163 memset(&propertyValues.new_value, 0, sizeof(GValue));
164 atk_value_get_current_value(ATK_VALUE(axObject), &propertyValues.new_value);
166 g_signal_emit_by_name(ATK_OBJECT(axObject), "property-change::accessible-value", &propertyValues, NULL);
167 } else if (notification == AXInvalidStatusChanged)
168 g_signal_emit_by_name(axObject, "state-change", "invalid-entry", coreObject->invalidStatus() != "false");
171 void AXObjectCache::nodeTextChangePlatformNotification(AccessibilityObject* object, AXTextChange textChange, unsigned offset, const String& text)
173 if (!object || text.isEmpty())
176 AccessibilityObject* parentObject = object->parentObjectUnignored();
180 AtkObject* wrapper = parentObject->wrapper();
181 if (!wrapper || !ATK_IS_TEXT(wrapper))
184 Node* node = object->node();
188 // Ensure document's layout is up-to-date before using TextIterator.
189 Document& document = node->document();
190 document.updateLayout();
192 // Select the right signal to be emitted
194 switch (textChange) {
195 case AXObjectCache::AXTextInserted:
196 detail = "text-insert";
198 case AXObjectCache::AXTextDeleted:
199 detail = "text-remove";
203 String textToEmit = text;
204 unsigned offsetToEmit = offset;
206 // If the object we're emitting the signal from represents a
207 // password field, we will emit the masked text.
208 if (parentObject->isPasswordField()) {
209 String maskedText = parentObject->passwordFieldValue();
210 textToEmit = maskedText.substring(offset, text.length());
212 // Consider previous text objects that might be present for
213 // the current accessibility object to ensure we emit the
214 // right offset (e.g. multiline text areas).
215 RefPtr<Range> range = Range::create(&document, node->parentNode(), 0, node, 0);
216 offsetToEmit = offset + TextIterator::rangeLength(range.get());
219 g_signal_emit_by_name(wrapper, detail.data(), offsetToEmit, textToEmit.length(), textToEmit.utf8().data());
222 void AXObjectCache::frameLoadingEventPlatformNotification(AccessibilityObject* object, AXLoadingEvent loadingEvent)
227 AtkObject* axObject = object->wrapper();
228 if (!axObject || !ATK_IS_DOCUMENT(axObject))
231 switch (loadingEvent) {
232 case AXObjectCache::AXLoadingStarted:
233 g_signal_emit_by_name(axObject, "state-change", "busy", true);
235 case AXObjectCache::AXLoadingReloaded:
236 g_signal_emit_by_name(axObject, "state-change", "busy", true);
237 g_signal_emit_by_name(axObject, "reload");
239 case AXObjectCache::AXLoadingFailed:
240 g_signal_emit_by_name(axObject, "load-stopped");
241 g_signal_emit_by_name(axObject, "state-change", "busy", false);
243 case AXObjectCache::AXLoadingFinished:
244 g_signal_emit_by_name(axObject, "load-complete");
245 g_signal_emit_by_name(axObject, "state-change", "busy", false);
250 void AXObjectCache::handleFocusedUIElementChanged(Node* oldFocusedNode, Node* newFocusedNode)
252 RefPtr<AccessibilityObject> oldObject = getOrCreate(oldFocusedNode);
254 g_signal_emit_by_name(oldObject->wrapper(), "focus-event", false);
255 g_signal_emit_by_name(oldObject->wrapper(), "state-change", "focused", false);
257 RefPtr<AccessibilityObject> newObject = getOrCreate(newFocusedNode);
259 g_signal_emit_by_name(newObject->wrapper(), "focus-event", true);
260 g_signal_emit_by_name(newObject->wrapper(), "state-change", "focused", true);
264 void AXObjectCache::handleScrolledToAnchor(const Node*)
268 } // namespace WebCore