| 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 "atkwrapper.hxx" |
| 21 | |
| 22 | #include <com/sun/star/accessibility/XAccessibleValue.hpp> |
| 23 | |
| 24 | #include <cmath> |
| 25 | #include <string.h> |
| 26 | |
| 27 | using namespace ::com::sun::star; |
| 28 | |
| 29 | /// @throws uno::RuntimeException |
| 30 | static css::uno::Reference<css::accessibility::XAccessibleValue> |
| 31 | getValue( AtkValue *pValue ) |
| 32 | { |
| 33 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pValue ); |
| 34 | if( pWrap ) |
| 35 | { |
| 36 | if( !pWrap->mpValue.is() ) |
| 37 | { |
| 38 | pWrap->mpValue.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 39 | } |
| 40 | |
| 41 | return pWrap->mpValue; |
| 42 | } |
| 43 | |
| 44 | return css::uno::Reference<css::accessibility::XAccessibleValue>(); |
| 45 | } |
| 46 | |
| 47 | static void anyToGValue( const uno::Any& aAny, GValue *pValue ) |
| 48 | { |
| 49 | // FIXME: expand to lots of types etc. |
| 50 | double aDouble=0; |
| 51 | aAny >>= aDouble; |
| 52 | |
| 53 | memset( pValue, 0, sizeof( GValue ) ); |
| 54 | g_value_init( pValue, G_TYPE_DOUBLE ); |
| 55 | g_value_set_double( pValue, aDouble ); |
| 56 | } |
| 57 | |
| 58 | extern "C" { |
| 59 | |
| 60 | static void |
| 61 | value_wrapper_get_current_value( AtkValue *value, |
| 62 | GValue *gval ) |
| 63 | { |
| 64 | try { |
| 65 | css::uno::Reference<css::accessibility::XAccessibleValue> pValue |
| 66 | = getValue( value ); |
| 67 | if( pValue.is() ) |
| 68 | anyToGValue( pValue->getCurrentValue(), gval ); |
| 69 | } |
| 70 | catch(const uno::Exception&) { |
| 71 | g_warning( "Exception in getCurrentValue()" ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | value_wrapper_get_maximum_value( AtkValue *value, |
| 77 | GValue *gval ) |
| 78 | { |
| 79 | try { |
| 80 | css::uno::Reference<css::accessibility::XAccessibleValue> pValue |
| 81 | = getValue( value ); |
| 82 | if( pValue.is() ) |
| 83 | anyToGValue( pValue->getMaximumValue(), gval ); |
| 84 | } |
| 85 | catch(const uno::Exception&) { |
| 86 | g_warning( "Exception in getCurrentValue()" ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | value_wrapper_get_minimum_value( AtkValue *value, |
| 92 | GValue *gval ) |
| 93 | { |
| 94 | try { |
| 95 | css::uno::Reference<css::accessibility::XAccessibleValue> pValue |
| 96 | = getValue( value ); |
| 97 | if( pValue.is() ) |
| 98 | anyToGValue( pValue->getMinimumValue(), gval ); |
| 99 | } |
| 100 | catch(const uno::Exception&) { |
| 101 | g_warning( "Exception in getCurrentValue()" ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static gboolean |
| 106 | value_wrapper_set_current_value( AtkValue *value, |
| 107 | const GValue *gval ) |
| 108 | { |
| 109 | try { |
| 110 | css::uno::Reference<css::accessibility::XAccessibleValue> pValue |
| 111 | = getValue( value ); |
| 112 | if( pValue.is() ) |
| 113 | { |
| 114 | double aDouble = g_value_get_double( gval ); |
| 115 | |
| 116 | // Different types of numerical values for XAccessibleValue are possible. |
| 117 | // If current value has an integer type, also use that for the new value, to make |
| 118 | // sure underlying implementations expecting that can handle the value properly. |
| 119 | const css::uno::Any aCurrentValue = pValue->getCurrentValue(); |
| 120 | if (aCurrentValue.getValueTypeClass() == css::uno::TypeClass::TypeClass_LONG) |
| 121 | { |
| 122 | const sal_Int32 nValue = std::round<sal_Int32>(aDouble); |
| 123 | return pValue->setCurrentValue(css::uno::Any(nValue)); |
| 124 | } |
| 125 | else if (aCurrentValue.getValueTypeClass() == css::uno::TypeClass::TypeClass_HYPER) |
| 126 | { |
| 127 | const sal_Int64 nValue = std::round<sal_Int64>(aDouble); |
| 128 | return pValue->setCurrentValue(css::uno::Any(nValue)); |
| 129 | } |
| 130 | |
| 131 | return pValue->setCurrentValue( uno::Any(aDouble) ); |
| 132 | } |
| 133 | } |
| 134 | catch(const uno::Exception&) { |
| 135 | g_warning( "Exception in getCurrentValue()" ); |
| 136 | } |
| 137 | |
| 138 | return FALSE; |
| 139 | } |
| 140 | |
| 141 | } // extern "C" |
| 142 | |
| 143 | void |
| 144 | valueIfaceInit (gpointer iface_, gpointer) |
| 145 | { |
| 146 | auto const iface = static_cast<AtkValueIface *>(iface_); |
| 147 | g_return_if_fail (iface != nullptr); |
| 148 | |
| 149 | iface->get_current_value = value_wrapper_get_current_value; |
| 150 | iface->get_maximum_value = value_wrapper_get_maximum_value; |
| 151 | iface->get_minimum_value = value_wrapper_get_minimum_value; |
| 152 | iface->set_current_value = value_wrapper_set_current_value; |
| 153 | } |
| 154 | |
| 155 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |