xref: /trunk/main/vcl/unx/gtk/a11y/atkeditabletext.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include "atkwrapper.hxx"
32 #include "atktextattributes.hxx"
33 
34 #include <com/sun/star/accessibility/XAccessibleEditableText.hpp>
35 #include <com/sun/star/accessibility/TextSegment.hpp>
36 
37 // #include <functional>
38 // #include <hash_map>
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 using namespace ::com::sun::star;
44 
45 static accessibility::XAccessibleEditableText*
46     getEditableText( AtkEditableText *pEditableText ) throw (uno::RuntimeException)
47 {
48     AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pEditableText );
49     if( pWrap )
50     {
51         if( !pWrap->mpEditableText && pWrap->mpContext )
52         {
53             uno::Any any = pWrap->mpContext->queryInterface( accessibility::XAccessibleEditableText::static_type(NULL) );
54             pWrap->mpEditableText = reinterpret_cast< accessibility::XAccessibleEditableText * > (any.pReserved);
55             pWrap->mpEditableText->acquire();
56         }
57 
58         return pWrap->mpEditableText;
59     }
60 
61     return NULL;
62 }
63 
64 
65 /*****************************************************************************/
66 
67 extern "C" {
68 
69 static gboolean
70 editable_text_wrapper_set_run_attributes( AtkEditableText  *text,
71                                           AtkAttributeSet  *attribute_set,
72                                           gint              nStartOffset,
73                                           gint              nEndOffset)
74 {
75     try {
76         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
77         if( pEditableText )
78         {
79             uno::Sequence< beans::PropertyValue > aAttributeList;
80 
81             if( attribute_set_map_to_property_values( attribute_set, aAttributeList ) )
82                 return pEditableText->setAttributes(nStartOffset, nEndOffset, aAttributeList);
83         }
84     }
85     catch(const uno::Exception& e) {
86         g_warning( "Exception in setAttributes()" );
87     }
88 
89     return FALSE;
90 }
91 
92 static void
93 editable_text_wrapper_set_text_contents( AtkEditableText  *text,
94                                          const gchar      *string )
95 {
96     try {
97         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
98         if( pEditableText )
99         {
100             rtl::OUString aString ( string, strlen(string), RTL_TEXTENCODING_UTF8 );
101             pEditableText->setText( aString );
102         }
103     }
104     catch(const uno::Exception& e) {
105         g_warning( "Exception in setText()" );
106     }
107 }
108 
109 static void
110 editable_text_wrapper_insert_text( AtkEditableText  *text,
111                                    const gchar      *string,
112                                    gint             length,
113                                    gint             *pos )
114 {
115     try {
116         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
117         if( pEditableText )
118         {
119             rtl::OUString aString ( string, length, RTL_TEXTENCODING_UTF8 );
120             if( pEditableText->insertText( aString, *pos ) )
121                 *pos += length;
122         }
123     }
124     catch(const uno::Exception& e) {
125         g_warning( "Exception in insertText()" );
126     }
127 }
128 
129 static void
130 editable_text_wrapper_cut_text( AtkEditableText  *text,
131                                 gint             start,
132                                 gint             end )
133 {
134     try {
135         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
136         if( pEditableText )
137             pEditableText->cutText( start, end );
138     }
139     catch(const uno::Exception& e) {
140         g_warning( "Exception in cutText()" );
141     }
142 }
143 
144 static void
145 editable_text_wrapper_delete_text( AtkEditableText  *text,
146                                    gint             start,
147                                    gint             end )
148 {
149     try {
150         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
151         if( pEditableText )
152             pEditableText->deleteText( start, end );
153     }
154     catch(const uno::Exception& e) {
155         g_warning( "Exception in deleteText()" );
156     }
157 }
158 
159 static void
160 editable_text_wrapper_paste_text( AtkEditableText  *text,
161                                   gint             pos )
162 {
163     try {
164         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
165         if( pEditableText )
166             pEditableText->pasteText( pos );
167     }
168     catch(const uno::Exception& e) {
169         g_warning( "Exception in pasteText()" );
170     }
171 }
172 
173 static void
174 editable_text_wrapper_copy_text( AtkEditableText  *text,
175                                  gint             start,
176                                  gint             end )
177 {
178     try {
179         accessibility::XAccessibleEditableText* pEditableText = getEditableText( text );
180         if( pEditableText )
181             pEditableText->copyText( start, end );
182     }
183     catch(const uno::Exception& e) {
184         g_warning( "Exception in copyText()" );
185     }
186 }
187 
188 } // extern "C"
189 
190 void
191 editableTextIfaceInit (AtkEditableTextIface *iface)
192 {
193   g_return_if_fail (iface != NULL);
194 
195   iface->set_text_contents = editable_text_wrapper_set_text_contents;
196   iface->insert_text = editable_text_wrapper_insert_text;
197   iface->copy_text = editable_text_wrapper_copy_text;
198   iface->cut_text = editable_text_wrapper_cut_text;
199   iface->delete_text = editable_text_wrapper_delete_text;
200   iface->paste_text = editable_text_wrapper_paste_text;
201   iface->set_run_attributes = editable_text_wrapper_set_run_attributes;
202 }
203