1/*
2 * Copyright (C) 2009 Igalia S.L.
3 *
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.
8 *
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.
13 *
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.
18 */
19
20#include "config.h"
21#include "SelectionController.h"
22
23#include "AccessibilityObjectWrapperAtk.h"
24#include "AXObjectCache.h"
25#include "Frame.h"
26#include "RefPtr.h"
27
28#include <gtk/gtk.h>
29
30namespace WebCore {
31
32static void emitTextSelectionChange(AccessibilityObject* object, VisibleSelection selection, int offset)
33{
34    AtkObject* axObject = object->wrapper();
35    if (!axObject || !ATK_IS_TEXT(axObject))
36        return;
37
38    g_signal_emit_by_name(axObject, "text-caret-moved", offset);
39    if (selection.isRange())
40        g_signal_emit_by_name(axObject, "text-selection-changed");
41}
42
43static void maybeEmitTextFocusChange(PassRefPtr<AccessibilityObject> prpObject)
44{
45    // This static variable is needed to keep track of the old object
46    // as per previous calls to this function, in order to properly
47    // decide whether to emit some signals or not.
48    DEFINE_STATIC_LOCAL(RefPtr<AccessibilityObject>, oldObject, ());
49
50    RefPtr<AccessibilityObject> object = prpObject;
51
52    // Ensure the oldObject belongs to the same document that the
53    // current object so further comparisons make sense. Otherwise,
54    // just reset oldObject to 0 so it won't be taken into account in
55    // the immediately following call to this function.
56    if (object && oldObject && oldObject->document() != object->document())
57        oldObject = 0;
58
59    AtkObject* axObject = object ? object->wrapper() : 0;
60    AtkObject* oldAxObject = oldObject ? oldObject->wrapper() : 0;
61
62    if (axObject != oldAxObject) {
63        if (oldAxObject && ATK_IS_TEXT(oldAxObject)) {
64            g_signal_emit_by_name(oldAxObject, "focus-event", false);
65            g_signal_emit_by_name(oldAxObject, "state-change", "focused", false);
66        }
67        if (axObject && ATK_IS_TEXT(axObject)) {
68            g_signal_emit_by_name(axObject, "focus-event", true);
69            g_signal_emit_by_name(axObject, "state-change", "focused", true);
70        }
71    }
72
73    // Update pointer to last focused object.
74    oldObject = object;
75}
76
77
78void SelectionController::notifyAccessibilityForSelectionChange()
79{
80    if (!AXObjectCache::accessibilityEnabled())
81        return;
82
83    // Reset lastFocuseNode and return for no valid selections.
84    if (!m_selection.start().isNotNull() || !m_selection.end().isNotNull())
85        return;
86
87    RenderObject* focusedNode = m_selection.end().deprecatedNode()->renderer();
88    AccessibilityObject* accessibilityObject = m_frame->document()->axObjectCache()->getOrCreate(focusedNode);
89
90    // Need to check this as getOrCreate could return 0,
91    if (!accessibilityObject)
92        return;
93
94    int offset;
95    // Always report the events w.r.t. the non-linked unignored parent. (i.e. ignoreLinks == true).
96    RefPtr<AccessibilityObject> object = objectAndOffsetUnignored(accessibilityObject, offset, true);
97    if (!object)
98        return;
99
100    // Emit relatedsignals.
101    emitTextSelectionChange(object.get(), m_selection, offset);
102    maybeEmitTextFocusChange(object.release());
103}
104
105} // namespace WebCore
106