1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <string_view>
23 
24 #include "atkwrapper.hxx"
25 
26 #include <com/sun/star/accessibility/XAccessibleImage.hpp>
27 
28 using namespace ::com::sun::star;
29 
30 // FIXME
31 static const gchar *
getAsConst(std::u16string_view rString)32 getAsConst( std::u16string_view rString )
33 {
34     static const int nMax = 10;
35     static OString aUgly[nMax];
36     static int nIdx = 0;
37     nIdx = (nIdx + 1) % nMax;
38     aUgly[nIdx] = OUStringToOString( rString, RTL_TEXTENCODING_UTF8 );
39     return aUgly[ nIdx ].getStr();
40 }
41 
42 /// @throws uno::RuntimeException
43 static css::uno::Reference<css::accessibility::XAccessibleImage>
getImage(AtkImage * pImage)44     getImage( AtkImage *pImage )
45 {
46     AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pImage );
47     if( pWrap )
48     {
49         if( !pWrap->mpImage.is() )
50         {
51             pWrap->mpImage.set(pWrap->mpContext, css::uno::UNO_QUERY);
52         }
53 
54         return pWrap->mpImage;
55     }
56 
57     return css::uno::Reference<css::accessibility::XAccessibleImage>();
58 }
59 
60 extern "C" {
61 
62 static const gchar *
image_get_image_description(AtkImage * image)63 image_get_image_description( AtkImage *image )
64 {
65     try {
66         css::uno::Reference<css::accessibility::XAccessibleImage> pImage
67             = getImage( image );
68         if( pImage.is() )
69             return getAsConst( pImage->getAccessibleImageDescription() );
70     }
71     catch(const uno::Exception&) {
72         g_warning( "Exception in getAccessibleImageDescription()" );
73     }
74 
75     return nullptr;
76 }
77 
78 static void
image_get_image_position(AtkImage * image,gint * x,gint * y,AtkCoordType coord_type)79 image_get_image_position( AtkImage     *image,
80                           gint         *x,
81                           gint         *y,
82                           AtkCoordType  coord_type )
83 {
84     *x = *y = -1;
85     if( ATK_IS_COMPONENT( image ) )
86     {
87         SAL_WNODEPRECATED_DECLARATIONS_PUSH
88         atk_component_get_position( ATK_COMPONENT( image ), x, y, coord_type );
89         SAL_WNODEPRECATED_DECLARATIONS_POP
90     }
91     else
92         g_warning( "FIXME: no image position information" );
93 }
94 
95 static void
image_get_image_size(AtkImage * image,gint * width,gint * height)96 image_get_image_size( AtkImage *image,
97                       gint     *width,
98                       gint     *height )
99 {
100     *width = *height = -1;
101     try {
102         css::uno::Reference<css::accessibility::XAccessibleImage> pImage
103             = getImage( image );
104         if( pImage.is() )
105         {
106             *width = pImage->getAccessibleImageWidth();
107             *height = pImage->getAccessibleImageHeight();
108         }
109     }
110     catch(const uno::Exception&) {
111         g_warning( "Exception in getAccessibleImageHeight() or Width" );
112     }
113 }
114 
115 static gboolean
image_set_image_description(AtkImage *,const gchar *)116 image_set_image_description( AtkImage *, const gchar * )
117 {
118     g_warning ("FIXME: no set image description");
119     return FALSE;
120 }
121 
122 } // extern "C"
123 
124 void
imageIfaceInit(AtkImageIface * iface)125 imageIfaceInit (AtkImageIface *iface)
126 {
127   g_return_if_fail (iface != nullptr);
128 
129   iface->set_image_description = image_set_image_description;
130   iface->get_image_description = image_get_image_description;
131   iface->get_image_position = image_get_image_position;
132   iface->get_image_size = image_get_image_size;
133 }
134 
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
136