diff options
Diffstat (limited to 'vcl/unx/gtk3')
42 files changed, 15069 insertions, 41 deletions
diff --git a/vcl/unx/gtk3/a11y/TODO b/vcl/unx/gtk3/a11y/TODO new file mode 100644 index 000000000000..1048bd96ef75 --- /dev/null +++ b/vcl/unx/gtk3/a11y/TODO @@ -0,0 +1,49 @@ +cws 'atkbridge' +#Issue number: i#47890# +Submitted by: mmeeks + +Hacked up prototype of atk bridge + + +Serious problems + + Threading/locking: + + incoming CORBA calls & the GDK lock + + how are these being processed & on what thread ? + + are we holding the GDK_THREADS lock ? + + can we even do that ? + + is it really necessary to be thread safe ? + + how does this work in combination with the (unsafe) GAIL code ? + + what should incoming CORBA calls be doing ? + + esp. since we can't tell if they're coming from + in-proc or not either [ though this is unlikely ] + + +Test: + + in-line text editing, does the TEXT_CHANGED signal get it right, + + why not copy/paste/delete etc. ? + + check vs. writer & other bits ... + + AtkSelection + + AtkHyper* + +* At-poke + + implement non-gui mode - for to-console event logging + + logging + + more detail from remaining events + + add a Tree navigation thing instead (?) + + poke a sub-child (?) + + embed a tree widget inside the tree view ? + + AtkHyperText testing (?) + + +Known bugs: + + AtkText + + selection interface - multiple selections ? + + word boundary issues + + copy AccessibleTextImpl.java's getAfterIndex eg. + + the 'getFoo' methods need to use UNO_QUERY_THROW & + throw an exception to avoid null pointer dereferences. + + AtkAttributeSet (etc.) + + AtkEditableText + + finish/test AtkTable + + HyperLink 'link_activated', HyperText 'link_selected' (?) + + tooltips create new toplevels with broken roles. diff --git a/vcl/unx/gtk3/a11y/atkfactory.hxx b/vcl/unx/gtk3/a11y/atkfactory.hxx new file mode 100644 index 000000000000..ac72b5897813 --- /dev/null +++ b/vcl/unx/gtk3/a11y/atkfactory.hxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKFACTORY_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKFACTORY_HXX + +#include <atk/atk.h> + +extern "C" { + +GType wrapper_factory_get_type(); + +} // extern "C" + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/atklistener.hxx b/vcl/unx/gtk3/a11y/atklistener.hxx new file mode 100644 index 000000000000..58798d4439fb --- /dev/null +++ b/vcl/unx/gtk3/a11y/atklistener.hxx @@ -0,0 +1,71 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKLISTENER_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKLISTENER_HXX + +#include <com/sun/star/accessibility/XAccessibleEventListener.hpp> +#include <cppuhelper/implbase.hxx> + +#include <vector> + +#include "atkwrapper.hxx" + +class AtkListener : public ::cppu::WeakImplHelper< css::accessibility::XAccessibleEventListener > +{ +public: + explicit AtkListener(AtkObjectWrapper * pWrapper); + + // XEventListener + virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override; + + // XAccessibleEventListener + virtual void SAL_CALL notifyEvent( const css::accessibility::AccessibleEventObject& aEvent ) override; + +private: + + AtkObjectWrapper *mpWrapper; + std::vector< css::uno::Reference< css::accessibility::XAccessible > > + m_aChildList; + + virtual ~AtkListener() override; + + // Updates the child list held to provide the old IndexInParent on children_changed::remove + void updateChildList( + css::uno::Reference<css::accessibility::XAccessibleContext> const & + pContext); + + // Process CHILD_EVENT notifications with a new child added + void handleChildAdded( + const css::uno::Reference< css::accessibility::XAccessibleContext >& rxParent, + const css::uno::Reference< css::accessibility::XAccessible>& rxChild); + + // Process CHILD_EVENT notifications with a child removed + void handleChildRemoved( + const css::uno::Reference< css::accessibility::XAccessibleContext >& rxParent, + const css::uno::Reference< css::accessibility::XAccessible>& rxChild); + + // Process INVALIDATE_ALL_CHILDREN notification + void handleInvalidateChildren( + const css::uno::Reference< css::accessibility::XAccessibleContext >& rxParent); +}; + +#endif // INCLUDED_VCL_UNX_GTK_A11Y_ATKLISTENER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/atkregistry.hxx b/vcl/unx/gtk3/a11y/atkregistry.hxx new file mode 100644 index 000000000000..b692290847d8 --- /dev/null +++ b/vcl/unx/gtk3/a11y/atkregistry.hxx @@ -0,0 +1,35 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKREGISTRY_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKREGISTRY_HXX + +#include <com/sun/star/accessibility/XAccessible.hpp> +#include <atk/atk.h> + +AtkObject * ooo_wrapper_registry_get(const css::uno::Reference< css::accessibility::XAccessible >& rxAccessible); + +void ooo_wrapper_registry_add(const css::uno::Reference< css::accessibility::XAccessible >& rxAccessible, AtkObject *obj); + +void ooo_wrapper_registry_remove( + css::uno::Reference<css::accessibility::XAccessible> const & pAccessible); + +#endif // __ATK_REGISTRY_HXX_ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/atktextattributes.hxx b/vcl/unx/gtk3/a11y/atktextattributes.hxx new file mode 100644 index 000000000000..f86c82f4fe09 --- /dev/null +++ b/vcl/unx/gtk3/a11y/atktextattributes.hxx @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKTEXTATTRIBUTES_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKTEXTATTRIBUTES_HXX + +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/accessibility/XAccessibleExtendedAttributes.hpp> + +#include <atk/atk.h> + +AtkAttributeSet* +attribute_set_new_from_property_values( + const css::uno::Sequence< css::beans::PropertyValue >& rAttributeList, + bool run_attributes_only, + AtkText *text); + +AtkAttributeSet* +attribute_set_new_from_extended_attributes( + const css::uno::Reference< css::accessibility::XAccessibleExtendedAttributes >& rExtendedAttributes ); + +bool +attribute_set_map_to_property_values( + AtkAttributeSet* attribute_set, + css::uno::Sequence< css::beans::PropertyValue >& rValueList ); + +AtkAttributeSet* attribute_set_prepend_misspelled( AtkAttributeSet* attribute_set ); +// #i92232# +AtkAttributeSet* attribute_set_prepend_tracked_change_insertion( AtkAttributeSet* attribute_set ); +AtkAttributeSet* attribute_set_prepend_tracked_change_deletion( AtkAttributeSet* attribute_set ); +AtkAttributeSet* attribute_set_prepend_tracked_change_formatchange( AtkAttributeSet* attribute_set ); + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/atkutil.hxx b/vcl/unx/gtk3/a11y/atkutil.hxx new file mode 100644 index 000000000000..cb4a005157a0 --- /dev/null +++ b/vcl/unx/gtk3/a11y/atkutil.hxx @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKUTIL_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKUTIL_HXX + +#include <atk/atk.h> + +GType ooo_atk_util_get_type(); +void ooo_atk_util_ensure_event_listener(); + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/atkwindow.hxx b/vcl/unx/gtk3/a11y/atkwindow.hxx new file mode 100644 index 000000000000..35a4bcedf486 --- /dev/null +++ b/vcl/unx/gtk3/a11y/atkwindow.hxx @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKWINDOW_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKWINDOW_HXX + +#include <atk/atk.h> + +GType ooo_window_wrapper_get_type(); +void restore_gail_window_vtable(); + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/atkwrapper.hxx b/vcl/unx/gtk3/a11y/atkwrapper.hxx new file mode 100644 index 000000000000..7ae0379de9c3 --- /dev/null +++ b/vcl/unx/gtk3/a11y/atkwrapper.hxx @@ -0,0 +1,125 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_A11Y_ATKWRAPPER_HXX +#define INCLUDED_VCL_UNX_GTK_A11Y_ATKWRAPPER_HXX + +#include <atk/atk.h> +#include <gtk/gtk.h> +#include <gtk/gtk-a11y.h> +#include <com/sun/star/accessibility/XAccessible.hpp> + +extern "C" { + +namespace com { namespace sun { namespace star { namespace accessibility { + class XAccessibleAction; + class XAccessibleComponent; + class XAccessibleEditableText; + class XAccessibleHypertext; + class XAccessibleImage; + class XAccessibleMultiLineText; + class XAccessibleSelection; + class XAccessibleTable; + class XAccessibleText; + class XAccessibleTextMarkup; + class XAccessibleTextAttributes; + class XAccessibleValue; +} } } } + +struct AtkObjectWrapper +{ + AtkObject const aParent; + AtkObject* mpOrig; //if we're a GtkDrawingArea acting as a custom LibreOffice widget, this is the toolkit default impl + + css::uno::Reference<css::accessibility::XAccessible> mpAccessible; + css::uno::Reference<css::accessibility::XAccessibleContext> mpContext; + css::uno::Reference<css::accessibility::XAccessibleAction> mpAction; + css::uno::Reference<css::accessibility::XAccessibleComponent> mpComponent; + css::uno::Reference<css::accessibility::XAccessibleEditableText> + mpEditableText; + css::uno::Reference<css::accessibility::XAccessibleHypertext> mpHypertext; + css::uno::Reference<css::accessibility::XAccessibleImage> mpImage; + css::uno::Reference<css::accessibility::XAccessibleMultiLineText> + mpMultiLineText; + css::uno::Reference<css::accessibility::XAccessibleSelection> mpSelection; + css::uno::Reference<css::accessibility::XAccessibleTable> mpTable; + css::uno::Reference<css::accessibility::XAccessibleText> mpText; + css::uno::Reference<css::accessibility::XAccessibleTextMarkup> mpTextMarkup; + css::uno::Reference<css::accessibility::XAccessibleTextAttributes> + mpTextAttributes; + css::uno::Reference<css::accessibility::XAccessibleValue> mpValue; + + AtkObject *child_about_to_be_removed; + gint index_of_child_about_to_be_removed; +// OString * m_pKeyBindings +}; + +struct AtkObjectWrapperClass +{ + GtkWidgetAccessibleClass aParentClass; +}; + +GType atk_object_wrapper_get_type() G_GNUC_CONST; +AtkObject * atk_object_wrapper_ref( + const css::uno::Reference< css::accessibility::XAccessible >& rxAccessible, + bool create = true ); + +AtkObject * atk_object_wrapper_new( + const css::uno::Reference< css::accessibility::XAccessible >& rxAccessible, + AtkObject* parent = nullptr, AtkObject* orig = nullptr ); + +void atk_object_wrapper_add_child(AtkObjectWrapper* wrapper, AtkObject *child, gint index); +void atk_object_wrapper_remove_child(AtkObjectWrapper* wrapper, AtkObject *child, gint index); +void atk_object_wrapper_set_role(AtkObjectWrapper* wrapper, sal_Int16 role); + +void atk_object_wrapper_dispose(AtkObjectWrapper* wrapper); + +AtkStateType mapAtkState( sal_Int16 nState ); + +AtkRelation* atk_object_wrapper_relation_new(const css::accessibility::AccessibleRelation& rRelation); + +void actionIfaceInit(AtkActionIface *iface); +void componentIfaceInit(AtkComponentIface *iface); +void editableTextIfaceInit(AtkEditableTextIface *iface); +void hypertextIfaceInit(AtkHypertextIface *iface); +void imageIfaceInit(AtkImageIface *iface); +void selectionIfaceInit(AtkSelectionIface *iface); +void tableIfaceInit(AtkTableIface *iface); +void textIfaceInit(AtkTextIface *iface); +void valueIfaceInit(AtkValueIface *iface); + +} // extern "C" + +#define ATK_TYPE_OBJECT_WRAPPER atk_object_wrapper_get_type() +#define ATK_OBJECT_WRAPPER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), ATK_TYPE_OBJECT_WRAPPER, AtkObjectWrapper)) +#define ATK_IS_OBJECT_WRAPPER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ATK_TYPE_OBJECT_WRAPPER)) + +static inline gchar * +OUStringToGChar(const OUString& rString ) +{ + OString aUtf8 = OUStringToOString( rString, RTL_TEXTENCODING_UTF8 ); + return g_strdup( aUtf8.getStr() ); +} + +#define OUStringToConstGChar( string ) OUStringToOString( string, RTL_TEXTENCODING_UTF8 ).getStr() + +#endif // INCLUDED_VCL_UNX_GTK_A11Y_ATKWRAPPER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkaction.cxx b/vcl/unx/gtk3/a11y/gtk3atkaction.cxx index a3fa632d08a1..81e743238d84 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkaction.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkaction.cxx @@ -5,8 +5,271 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkaction.cxx" +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleAction.hpp> +#include <com/sun/star/accessibility/XAccessibleKeyBinding.hpp> + +#include <com/sun/star/awt/Key.hpp> +#include <com/sun/star/awt/KeyModifier.hpp> + +#include <rtl/strbuf.hxx> +#include <algorithm> +#include <map> + +using namespace ::com::sun::star; + +// FIXME +static G_CONST_RETURN gchar * +getAsConst( const OString& rString ) +{ + static const int nMax = 10; + static OString aUgly[nMax]; + static int nIdx = 0; + nIdx = (nIdx + 1) % nMax; + aUgly[nIdx] = rString; + return aUgly[ nIdx ].getStr(); +} + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleAction> + getAction( AtkAction *action ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( action ); + + if( pWrap ) + { + if( !pWrap->mpAction.is() ) + { + pWrap->mpAction.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpAction; + } + + return css::uno::Reference<css::accessibility::XAccessibleAction>(); +} + +extern "C" { + +static gboolean +action_wrapper_do_action (AtkAction *action, + gint i) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleAction> pAction + = getAction( action ); + if( pAction.is() ) + return pAction->doAccessibleAction( i ); + } + catch(const uno::Exception&) { + g_warning( "Exception in doAccessibleAction()" ); + } + + return FALSE; +} + +static gint +action_wrapper_get_n_actions (AtkAction *action) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleAction> pAction + = getAction( action ); + if( pAction.is() ) + return pAction->getAccessibleActionCount(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleActionCount()" ); + } + + return 0; +} + +static G_CONST_RETURN gchar * +action_wrapper_get_description (AtkAction *, gint) +{ + // GAIL implement this only for cells + g_warning( "Not implemented: get_description()" ); + return ""; +} + +static G_CONST_RETURN gchar * +action_wrapper_get_localized_name (AtkAction *, gint) +{ + // GAIL doesn't implement this as well + g_warning( "Not implemented: get_localized_name()" ); + return ""; +} + +#define ACTION_NAME_PAIR( OOoName, AtkName ) \ + std::pair< const OUString, const gchar * > ( OUString( OOoName ), AtkName ) + +static G_CONST_RETURN gchar * +action_wrapper_get_name (AtkAction *action, + gint i) +{ + static std::map< OUString, const gchar * > aNameMap; + + if( aNameMap.empty() ) + { + aNameMap.insert( ACTION_NAME_PAIR( "click", "click" ) ); + aNameMap.insert( ACTION_NAME_PAIR( "select", "click" ) ); + aNameMap.insert( ACTION_NAME_PAIR( "togglePopup", "push" ) ); + } + + try { + css::uno::Reference<css::accessibility::XAccessibleAction> pAction + = getAction( action ); + if( pAction.is() ) + { + std::map< OUString, const gchar * >::iterator iter; + + OUString aDesc( pAction->getAccessibleActionDescription( i ) ); + + iter = aNameMap.find( aDesc ); + if( iter != aNameMap.end() ) + return iter->second; + + std::pair< const OUString, const gchar * > aNewVal( aDesc, + g_strdup( OUStringToConstGChar(aDesc) ) ); + + if( aNameMap.insert( aNewVal ).second ) + return aNewVal.second; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleActionDescription()" ); + } + + return ""; +} + +/* +* GNOME Expects a string in the format: +* +* <mnemonic>;<full-path>;<accelerator> +* +* The keybindings in <full-path> should be separated by ":" +*/ + +static void +appendKeyStrokes(OStringBuffer& rBuffer, const uno::Sequence< awt::KeyStroke >& rKeyStrokes) +{ + for( const auto& rKeyStroke : rKeyStrokes ) + { + if( rKeyStroke.Modifiers & awt::KeyModifier::SHIFT ) + rBuffer.append("<Shift>"); + if( rKeyStroke.Modifiers & awt::KeyModifier::MOD1 ) + rBuffer.append("<Control>"); + if( rKeyStroke.Modifiers & awt::KeyModifier::MOD2 ) + rBuffer.append("<Alt>"); + + if( ( rKeyStroke.KeyCode >= awt::Key::A ) && ( rKeyStroke.KeyCode <= awt::Key::Z ) ) + rBuffer.append( static_cast<sal_Char>( 'a' + ( rKeyStroke.KeyCode - awt::Key::A ) ) ); + else + { + sal_Char c = '\0'; + + switch( rKeyStroke.KeyCode ) + { + case awt::Key::TAB: c = '\t'; break; + case awt::Key::SPACE: c = ' '; break; + case awt::Key::ADD: c = '+'; break; + case awt::Key::SUBTRACT: c = '-'; break; + case awt::Key::MULTIPLY: c = '*'; break; + case awt::Key::DIVIDE: c = '/'; break; + case awt::Key::POINT: c = '.'; break; + case awt::Key::COMMA: c = ','; break; + case awt::Key::LESS: c = '<'; break; + case awt::Key::GREATER: c = '>'; break; + case awt::Key::EQUAL: c = '='; break; + case 0: + break; + default: + g_warning( "Unmapped KeyCode: %d", rKeyStroke.KeyCode ); + break; + } + + if( c != '\0' ) + rBuffer.append( c ); + else + { + // The KeyCode approach did not work, probably a non ascii character + // let's hope that there is a character given in KeyChar. + rBuffer.append( OUStringToGChar( OUString( rKeyStroke.KeyChar ) ) ); + } + } + } +} + +static G_CONST_RETURN gchar * +action_wrapper_get_keybinding (AtkAction *action, + gint i) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleAction> pAction + = getAction( action ); + if( pAction.is() ) + { + uno::Reference< accessibility::XAccessibleKeyBinding > xBinding( pAction->getAccessibleActionKeyBinding( i )); + + if( xBinding.is() ) + { + OStringBuffer aRet; + + sal_Int32 nmax = std::min( xBinding->getAccessibleKeyBindingCount(), sal_Int32(3) ); + for( sal_Int32 n = 0; n < nmax; n++ ) + { + appendKeyStrokes( aRet, xBinding->getAccessibleKeyBinding( n ) ); + + if( n < 2 ) + aRet.append( ';' ); + } + + // !! FIXME !! remember keystroke in wrapper object ? + return getAsConst( aRet.makeStringAndClear() ); + } + } + } + catch(const uno::Exception&) { + g_warning( "Exception in get_keybinding()" ); + } + + return ""; +} + +static gboolean +action_wrapper_set_description (AtkAction *, gint, const gchar *) +{ + return FALSE; +} + +} // extern "C" + +void +actionIfaceInit (AtkActionIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->do_action = action_wrapper_do_action; + iface->get_n_actions = action_wrapper_get_n_actions; + iface->get_description = action_wrapper_get_description; + iface->get_keybinding = action_wrapper_get_keybinding; + iface->get_name = action_wrapper_get_name; + iface->get_localized_name = action_wrapper_get_localized_name; + iface->set_description = action_wrapper_set_description; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkbridge.cxx b/vcl/unx/gtk3/a11y/gtk3atkbridge.cxx index d8e087956067..0e2781cf8fd5 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkbridge.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkbridge.cxx @@ -5,8 +5,33 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkbridge.cxx" +#include <unx/gtk/atkbridge.hxx> +#include <unx/gtk/gtkframe.hxx> + +#include "atkfactory.hxx" +#include "atkutil.hxx" +#include "atkwindow.hxx" + +bool InitAtkBridge() +{ + ooo_atk_util_ensure_event_listener(); + return true; +} + +void DeInitAtkBridge() +{ +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkcomponent.cxx b/vcl/unx/gtk3/a11y/gtk3atkcomponent.cxx index e4eabec20fb8..e904b12ac610 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkcomponent.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkcomponent.cxx @@ -5,8 +5,379 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkcomponent.cxx" +#include "atkwrapper.hxx" +#include <com/sun/star/accessibility/XAccessibleComponent.hpp> +#include <gtk/gtk.h> + +using namespace ::com::sun::star; + +static AtkObjectWrapper* getObjectWrapper(AtkComponent *pComponent) +{ + AtkObjectWrapper *pWrap = nullptr; + if (ATK_IS_OBJECT_WRAPPER(pComponent)) + pWrap = ATK_OBJECT_WRAPPER(pComponent); + else if (GTK_IS_DRAWING_AREA(pComponent)) //when using a GtkDrawingArea as a custom widget in welded gtk3 + { + GtkWidget* pDrawingArea = GTK_WIDGET(pComponent); + AtkObject* pAtkObject = gtk_widget_get_accessible(pDrawingArea); + pWrap = ATK_IS_OBJECT_WRAPPER(pAtkObject) ? ATK_OBJECT_WRAPPER(pAtkObject) : nullptr; + } + return pWrap; +} + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleComponent> + getComponent(AtkObjectWrapper *pWrap) +{ + if (pWrap) + { + if (!pWrap->mpComponent.is()) + pWrap->mpComponent.set(pWrap->mpContext, css::uno::UNO_QUERY); + return pWrap->mpComponent; + } + + return css::uno::Reference<css::accessibility::XAccessibleComponent>(); +} + +/*****************************************************************************/ + +static awt::Point +translatePoint( css::uno::Reference<accessibility::XAccessibleComponent> const & pComponent, + gint x, gint y, AtkCoordType t) +{ + awt::Point aOrigin( 0, 0 ); + if( t == ATK_XY_SCREEN ) + aOrigin = pComponent->getLocationOnScreen(); + return awt::Point( x - aOrigin.X, y - aOrigin.Y ); +} + +/*****************************************************************************/ + +extern "C" { + +static gboolean +component_wrapper_grab_focus (AtkComponent *component) +{ + AtkObjectWrapper* obj = getObjectWrapper(component); + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y + if (obj && obj->mpOrig) + return atk_component_grab_focus(ATK_COMPONENT(obj->mpOrig)); + + try + { + css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent + = getComponent(obj); + if( pComponent.is() ) + { + pComponent->grabFocus(); + return TRUE; + } + } + catch( const uno::Exception & ) + { + g_warning( "Exception in grabFocus()" ); + } + + return FALSE; +} + +/*****************************************************************************/ + +static gboolean +component_wrapper_contains (AtkComponent *component, + gint x, + gint y, + AtkCoordType coord_type) +{ + AtkObjectWrapper* obj = getObjectWrapper(component); + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y + if (obj && obj->mpOrig) + return atk_component_contains(ATK_COMPONENT(obj->mpOrig), x, y, coord_type); + + try + { + css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent + = getComponent(obj); + if( pComponent.is() ) + return pComponent->containsPoint( translatePoint( pComponent, x, y, coord_type ) ); + } + catch( const uno::Exception & ) + { + g_warning( "Exception in containsPoint()" ); + } + + return FALSE; +} + +/*****************************************************************************/ + +static AtkObject * +component_wrapper_ref_accessible_at_point (AtkComponent *component, + gint x, + gint y, + AtkCoordType coord_type) +{ + AtkObjectWrapper* obj = getObjectWrapper(component); + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y + if (obj && obj->mpOrig) + return atk_component_ref_accessible_at_point(ATK_COMPONENT(obj->mpOrig), x, y, coord_type); + + try + { + css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent + = getComponent(obj); + + if( pComponent.is() ) + { + uno::Reference< accessibility::XAccessible > xAccessible = pComponent->getAccessibleAtPoint( + translatePoint( pComponent, x, y, coord_type ) ); + return atk_object_wrapper_ref( xAccessible ); + } + } + catch( const uno::Exception & ) + { + g_warning( "Exception in getAccessibleAtPoint()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static void +component_wrapper_get_position (AtkComponent *component, + gint *x, + gint *y, + AtkCoordType coord_type) +{ + AtkObjectWrapper* obj = getObjectWrapper(component); + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y + if (obj && obj->mpOrig) + { + atk_component_get_extents(ATK_COMPONENT(obj->mpOrig), x, y, nullptr, nullptr, coord_type); + return; + } + + try + { + css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent + = getComponent(obj); + if( pComponent.is() ) + { + awt::Point aPos; + + if( coord_type == ATK_XY_SCREEN ) + aPos = pComponent->getLocationOnScreen(); + else + aPos = pComponent->getLocation(); + + *x = aPos.X; + *y = aPos.Y; + } + } + catch( const uno::Exception & ) + { + g_warning( "Exception in getLocation[OnScreen]()" ); + } +} + +/*****************************************************************************/ + +static void +component_wrapper_get_size (AtkComponent *component, + gint *width, + gint *height) +{ + AtkObjectWrapper* obj = getObjectWrapper(component); + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y + if (obj && obj->mpOrig) + { + atk_component_get_extents(ATK_COMPONENT(obj->mpOrig), nullptr, nullptr, width, height, ATK_XY_WINDOW); + return; + } + + try + { + css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent + = getComponent(obj); + if( pComponent.is() ) + { + awt::Size aSize = pComponent->getSize(); + *width = aSize.Width; + *height = aSize.Height; + } + } + catch( const uno::Exception & ) + { + g_warning( "Exception in getSize()" ); + } +} + +/*****************************************************************************/ + +static void +component_wrapper_get_extents (AtkComponent *component, + gint *x, + gint *y, + gint *width, + gint *height, + AtkCoordType coord_type) +{ + component_wrapper_get_position( component, x, y, coord_type ); + component_wrapper_get_size( component, width, height ); +} + +/*****************************************************************************/ + +static gboolean +component_wrapper_set_extents (AtkComponent *, gint, gint, gint, gint, AtkCoordType) +{ + g_warning( "AtkComponent::set_extents unimplementable" ); + return FALSE; +} + +/*****************************************************************************/ + +static gboolean +component_wrapper_set_position (AtkComponent *, gint, gint, AtkCoordType) +{ + g_warning( "AtkComponent::set_position unimplementable" ); + return FALSE; +} + +/*****************************************************************************/ + +static gboolean +component_wrapper_set_size (AtkComponent *, gint, gint) +{ + g_warning( "AtkComponent::set_size unimplementable" ); + return FALSE; +} + +/*****************************************************************************/ + +static AtkLayer +component_wrapper_get_layer (AtkComponent *component) +{ + AtkRole role = atk_object_get_role( ATK_OBJECT( component ) ); + AtkLayer layer = ATK_LAYER_WIDGET; + + switch (role) + { + case ATK_ROLE_POPUP_MENU: + case ATK_ROLE_MENU_ITEM: + case ATK_ROLE_CHECK_MENU_ITEM: + case ATK_ROLE_SEPARATOR: + case ATK_ROLE_LIST_ITEM: + layer = ATK_LAYER_POPUP; + break; + case ATK_ROLE_MENU: + { + AtkObject * parent = atk_object_get_parent( ATK_OBJECT( component ) ); + if( atk_object_get_role( parent ) != ATK_ROLE_MENU_BAR ) + layer = ATK_LAYER_POPUP; + } + break; + + case ATK_ROLE_LIST: + { + AtkObject * parent = atk_object_get_parent( ATK_OBJECT( component ) ); + if( atk_object_get_role( parent ) == ATK_ROLE_COMBO_BOX ) + layer = ATK_LAYER_POPUP; + } + break; + + default: + ; + } + + return layer; +} + +/*****************************************************************************/ + +static gint +component_wrapper_get_mdi_zorder (AtkComponent *) +{ + // only needed for ATK_LAYER_MDI (not used) or ATK_LAYER_WINDOW (inherited from GAIL) + return G_MININT; +} + +/*****************************************************************************/ + +// This code is mostly stolen from libgail .. + +static guint +component_wrapper_add_focus_handler (AtkComponent *component, + AtkFocusHandler handler) +{ + GSignalMatchType match_type; + gulong ret; + guint signal_id; + + match_type = GSignalMatchType(G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC); + signal_id = g_signal_lookup( "focus-event", ATK_TYPE_OBJECT ); + + ret = g_signal_handler_find( component, match_type, signal_id, 0, nullptr, + static_cast<gpointer>(&handler), nullptr); + if (!ret) + { + return g_signal_connect_closure_by_id (component, + signal_id, 0, + g_cclosure_new ( + G_CALLBACK (handler), nullptr, + nullptr), + FALSE); + } + else + { + return 0; + } +} + +/*****************************************************************************/ + +static void +component_wrapper_remove_focus_handler (AtkComponent *component, + guint handler_id) +{ + g_signal_handler_disconnect (component, handler_id); +} + +/*****************************************************************************/ + +} // extern "C" + +void +componentIfaceInit (AtkComponentIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->add_focus_handler = component_wrapper_add_focus_handler; + iface->contains = component_wrapper_contains; + iface->get_extents = component_wrapper_get_extents; + iface->get_layer = component_wrapper_get_layer; + iface->get_mdi_zorder = component_wrapper_get_mdi_zorder; + iface->get_position = component_wrapper_get_position; + iface->get_size = component_wrapper_get_size; + iface->grab_focus = component_wrapper_grab_focus; + iface->ref_accessible_at_point = component_wrapper_ref_accessible_at_point; + iface->remove_focus_handler = component_wrapper_remove_focus_handler; + iface->set_extents = component_wrapper_set_extents; + iface->set_position = component_wrapper_set_position; + iface->set_size = component_wrapper_set_size; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkeditabletext.cxx b/vcl/unx/gtk3/a11y/gtk3atkeditabletext.cxx index ea3f0895fb8e..49d3eb9ccfc9 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkeditabletext.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkeditabletext.cxx @@ -5,8 +5,190 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkeditabletext.cxx" +#include "atkwrapper.hxx" +#include "atktextattributes.hxx" + +#include <com/sun/star/accessibility/XAccessibleEditableText.hpp> +#include <com/sun/star/accessibility/TextSegment.hpp> + +#include <string.h> + +using namespace ::com::sun::star; + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleEditableText> + getEditableText( AtkEditableText *pEditableText ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pEditableText ); + if( pWrap ) + { + if( !pWrap->mpEditableText.is() ) + { + pWrap->mpEditableText.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpEditableText; + } + + return css::uno::Reference<css::accessibility::XAccessibleEditableText>(); +} + +/*****************************************************************************/ + +extern "C" { + +static gboolean +editable_text_wrapper_set_run_attributes( AtkEditableText *text, + AtkAttributeSet *attribute_set, + gint nStartOffset, + gint nEndOffset) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + { + uno::Sequence< beans::PropertyValue > aAttributeList; + + if( attribute_set_map_to_property_values( attribute_set, aAttributeList ) ) + return pEditableText->setAttributes(nStartOffset, nEndOffset, aAttributeList); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in setAttributes()" ); + } + + return FALSE; +} + +static void +editable_text_wrapper_set_text_contents( AtkEditableText *text, + const gchar *string ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + { + OUString aString ( string, strlen(string), RTL_TEXTENCODING_UTF8 ); + pEditableText->setText( aString ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in setText()" ); + } +} + +static void +editable_text_wrapper_insert_text( AtkEditableText *text, + const gchar *string, + gint length, + gint *pos ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + { + OUString aString ( string, length, RTL_TEXTENCODING_UTF8 ); + if( pEditableText->insertText( aString, *pos ) ) + *pos += length; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in insertText()" ); + } +} + +static void +editable_text_wrapper_cut_text( AtkEditableText *text, + gint start, + gint end ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + pEditableText->cutText( start, end ); + } + catch(const uno::Exception&) { + g_warning( "Exception in cutText()" ); + } +} + +static void +editable_text_wrapper_delete_text( AtkEditableText *text, + gint start, + gint end ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + pEditableText->deleteText( start, end ); + } + catch(const uno::Exception&) { + g_warning( "Exception in deleteText()" ); + } +} + +static void +editable_text_wrapper_paste_text( AtkEditableText *text, + gint pos ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + pEditableText->pasteText( pos ); + } + catch(const uno::Exception&) { + g_warning( "Exception in pasteText()" ); + } +} + +static void +editable_text_wrapper_copy_text( AtkEditableText *text, + gint start, + gint end ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleEditableText> + pEditableText = getEditableText( text ); + if( pEditableText.is() ) + pEditableText->copyText( start, end ); + } + catch(const uno::Exception&) { + g_warning( "Exception in copyText()" ); + } +} + +} // extern "C" + +void +editableTextIfaceInit (AtkEditableTextIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->set_text_contents = editable_text_wrapper_set_text_contents; + iface->insert_text = editable_text_wrapper_insert_text; + iface->copy_text = editable_text_wrapper_copy_text; + iface->cut_text = editable_text_wrapper_cut_text; + iface->delete_text = editable_text_wrapper_delete_text; + iface->paste_text = editable_text_wrapper_paste_text; + iface->set_run_attributes = editable_text_wrapper_set_run_attributes; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkfactory.cxx b/vcl/unx/gtk3/a11y/gtk3atkfactory.cxx index c60db2fcc074..d7c8bf9f6289 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkfactory.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkfactory.cxx @@ -5,8 +5,182 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unx/gtk/gtkframe.hxx> +#include <vcl/window.hxx> +#include "atkwrapper.hxx" +#include "atkfactory.hxx" +#include "atkregistry.hxx" + +using namespace ::com::sun::star; + +extern "C" { + +/* + * Instances of this dummy object class are returned whenever we have to + * create an AtkObject, but can't touch the OOo object anymore since it + * is already disposed. + */ + +static AtkStateSet * +noop_wrapper_ref_state_set( AtkObject * ) +{ + AtkStateSet *state_set = atk_state_set_new(); + atk_state_set_add_state( state_set, ATK_STATE_DEFUNCT ); + return state_set; +} + +static void +atk_noop_object_wrapper_class_init(AtkNoOpObjectClass *klass) +{ + AtkObjectClass *atk_class = ATK_OBJECT_CLASS( klass ); + atk_class->ref_state_set = noop_wrapper_ref_state_set; +} + +static GType +atk_noop_object_wrapper_get_type() +{ + static GType type = 0; + + if (!type) + { + static const GTypeInfo typeInfo = + { + sizeof (AtkNoOpObjectClass), + nullptr, + nullptr, + reinterpret_cast<GClassInitFunc>(atk_noop_object_wrapper_class_init), + nullptr, + nullptr, + sizeof (AtkObjectWrapper), + 0, + nullptr, + nullptr + } ; + + type = g_type_register_static (ATK_TYPE_OBJECT, "OOoAtkNoOpObj", &typeInfo, GTypeFlags(0)) ; + } + return type; +} + +static AtkObject* +atk_noop_object_wrapper_new() +{ + AtkObject *accessible; + + accessible = static_cast<AtkObject *>(g_object_new (atk_noop_object_wrapper_get_type(), nullptr)); + g_return_val_if_fail (accessible != nullptr, nullptr); + + accessible->role = ATK_ROLE_INVALID; + accessible->layer = ATK_LAYER_INVALID; + + return accessible; +} + +/* + * The wrapper factory */ -#include "../../gtk/a11y/atkfactory.cxx" +static GType +wrapper_factory_get_accessible_type() +{ + return atk_object_wrapper_get_type(); +} + +static AtkObject* +wrapper_factory_create_accessible( GObject *obj ) +{ + GtkWidget* pEventBox = gtk_widget_get_parent(GTK_WIDGET(obj)); + + // gail_container_real_remove_gtk tries to re-instantiate an accessible + // for a widget that is about to vanish .. + if (!pEventBox) + return atk_noop_object_wrapper_new(); + + GtkWidget* pTopLevelGrid = gtk_widget_get_parent(pEventBox); + if (!pTopLevelGrid) + return atk_noop_object_wrapper_new(); + + GtkWidget* pTopLevel = gtk_widget_get_parent(pTopLevelGrid); + if (!pTopLevel) + return atk_noop_object_wrapper_new(); + + GtkSalFrame* pFrame = GtkSalFrame::getFromWindow(GTK_WINDOW(pTopLevel)); + g_return_val_if_fail( pFrame != nullptr, nullptr ); + + vcl::Window* pFrameWindow = pFrame->GetWindow(); + if( pFrameWindow ) + { + vcl::Window* pWindow = pFrameWindow; + + // skip accessible objects already exposed by the frame objects + if( WindowType::BORDERWINDOW == pWindow->GetType() ) + pWindow = pFrameWindow->GetAccessibleChildWindow(0); + + if( pWindow ) + { + uno::Reference< accessibility::XAccessible > xAccessible = pWindow->GetAccessible(); + if( xAccessible.is() ) + { + AtkObject *accessible = ooo_wrapper_registry_get( xAccessible ); + + if( accessible ) + g_object_ref( G_OBJECT(accessible) ); + else + accessible = atk_object_wrapper_new( xAccessible, gtk_widget_get_accessible(pTopLevel) ); + + return accessible; + } + } + } + + return nullptr; +} + +AtkObject* ooo_fixed_get_accessible(GtkWidget *obj) +{ + return wrapper_factory_create_accessible(G_OBJECT(obj)); +} + +static void +wrapper_factory_class_init( AtkObjectFactoryClass *klass ) +{ + klass->create_accessible = wrapper_factory_create_accessible; + klass->get_accessible_type = wrapper_factory_get_accessible_type; +} + +GType +wrapper_factory_get_type() +{ + static GType t = 0; + + if (!t) { + static const GTypeInfo tinfo = + { + sizeof (AtkObjectFactoryClass), + nullptr, nullptr, reinterpret_cast<GClassInitFunc>(wrapper_factory_class_init), + nullptr, nullptr, sizeof (AtkObjectFactory), 0, nullptr, nullptr + }; + + t = g_type_register_static ( + ATK_TYPE_OBJECT_FACTORY, "OOoAtkObjectWrapperFactory", + &tinfo, GTypeFlags(0)); + } + + return t; +} + +} // extern C /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkhypertext.cxx b/vcl/unx/gtk3/a11y/gtk3atkhypertext.cxx index bb9749c36af3..ab94126dfc51 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkhypertext.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkhypertext.cxx @@ -5,8 +5,269 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkhypertext.cxx" +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleHypertext.hpp> + +using namespace ::com::sun::star; + +// ---------------------- AtkHyperlink ---------------------- + +struct HyperLink { + AtkHyperlink const atk_hyper_link; + + uno::Reference< accessibility::XAccessibleHyperlink > xLink; +}; + +static uno::Reference< accessibility::XAccessibleHyperlink > const & + getHyperlink( AtkHyperlink *pHyperlink ) +{ + HyperLink *pLink = reinterpret_cast<HyperLink *>(pHyperlink); + return pLink->xLink; +} + +static GObjectClass *hyper_parent_class = nullptr; + +extern "C" { + +static void +hyper_link_finalize (GObject *obj) +{ + HyperLink *hl = reinterpret_cast<HyperLink *>(obj); + hl->xLink.clear(); + hyper_parent_class->finalize (obj); +} + +static gchar * +hyper_link_get_uri( AtkHyperlink *pLink, + gint i ) +{ + try { + uno::Any aAny = getHyperlink( pLink )->getAccessibleActionObject( i ); + OUString aUri = aAny.get< OUString > (); + return OUStringToGChar(aUri); + } + catch(const uno::Exception&) { + g_warning( "Exception in hyper_link_get_uri" ); + } + return nullptr; +} + +static AtkObject * +hyper_link_get_object( AtkHyperlink *, + gint ) +{ + g_warning( "FIXME: hyper_link_get_object unimplemented" ); + return nullptr; +} +static gint +hyper_link_get_end_index( AtkHyperlink *pLink ) +{ + try { + return getHyperlink( pLink )->getEndIndex(); + } + catch(const uno::Exception&) { + } + return -1; +} +static gint +hyper_link_get_start_index( AtkHyperlink *pLink ) +{ + try { + return getHyperlink( pLink )->getStartIndex(); + } + catch(const uno::Exception&) { + } + return -1; +} +static gboolean +hyper_link_is_valid( AtkHyperlink *pLink ) +{ + try { + return getHyperlink( pLink )->isValid(); + } + catch(const uno::Exception&) { + } + return FALSE; +} +static gint +hyper_link_get_n_anchors( AtkHyperlink *pLink ) +{ + try { + return getHyperlink( pLink )->getAccessibleActionCount(); + } + catch(const uno::Exception&) { + } + return 0; +} + +static guint +hyper_link_link_state( AtkHyperlink * ) +{ + g_warning( "FIXME: hyper_link_link_state unimplemented" ); + return 0; +} +static gboolean +hyper_link_is_selected_link( AtkHyperlink * ) +{ + g_warning( "FIXME: hyper_link_is_selected_link unimplemented" ); + return FALSE; +} + +static void +hyper_link_class_init (AtkHyperlinkClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = hyper_link_finalize; + + hyper_parent_class = static_cast<GObjectClass *>(g_type_class_peek_parent (klass)); + + klass->get_uri = hyper_link_get_uri; + klass->get_object = hyper_link_get_object; + klass->get_end_index = hyper_link_get_end_index; + klass->get_start_index = hyper_link_get_start_index; + klass->is_valid = hyper_link_is_valid; + klass->get_n_anchors = hyper_link_get_n_anchors; + klass->link_state = hyper_link_link_state; + klass->is_selected_link = hyper_link_is_selected_link; +} + +static GType +hyper_link_get_type() +{ + static GType type = 0; + + if (!type) { + static const GTypeInfo tinfo = { + sizeof (AtkHyperlinkClass), + nullptr, /* base init */ + nullptr, /* base finalize */ + reinterpret_cast<GClassInitFunc>(hyper_link_class_init), + nullptr, /* class finalize */ + nullptr, /* class data */ + sizeof (HyperLink), /* instance size */ + 0, /* nb preallocs */ + nullptr, /* instance init */ + nullptr /* value table */ + }; + + static const GInterfaceInfo atk_action_info = { + reinterpret_cast<GInterfaceInitFunc>(actionIfaceInit), + nullptr, + nullptr + }; + + type = g_type_register_static (ATK_TYPE_HYPERLINK, + "OOoAtkObjHyperLink", &tinfo, + GTypeFlags(0)); + g_type_add_interface_static (type, ATK_TYPE_ACTION, + &atk_action_info); + } + + return type; +} + +// ---------------------- AtkHyperText ---------------------- + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleHypertext> + getHypertext( AtkHypertext *pHypertext ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pHypertext ); + if( pWrap ) + { + if( !pWrap->mpHypertext.is() ) + { + pWrap->mpHypertext.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpHypertext; + } + + return css::uno::Reference<css::accessibility::XAccessibleHypertext>(); +} + +static AtkHyperlink * +hypertext_get_link( AtkHypertext *hypertext, + gint link_index) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleHypertext> pHypertext + = getHypertext( hypertext ); + if( pHypertext.is() ) + { + HyperLink *pLink = static_cast<HyperLink *>(g_object_new( hyper_link_get_type(), nullptr )); + pLink->xLink = pHypertext->getHyperLink( link_index ); + if( !pLink->xLink.is() ) { + g_object_unref( G_OBJECT( pLink ) ); + pLink = nullptr; + } + return ATK_HYPERLINK( pLink ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getHyperLink()" ); + } + + return nullptr; +} + +static gint +hypertext_get_n_links( AtkHypertext *hypertext ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleHypertext> pHypertext + = getHypertext( hypertext ); + if( pHypertext.is() ) + return pHypertext->getHyperLinkCount(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getHyperLinkCount()" ); + } + + return 0; +} + +static gint +hypertext_get_link_index( AtkHypertext *hypertext, + gint index) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleHypertext> pHypertext + = getHypertext( hypertext ); + if( pHypertext.is() ) + return pHypertext->getHyperLinkIndex( index ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getHyperLinkIndex()" ); + } + + return 0; +} + +} // extern "C" + +void +hypertextIfaceInit (AtkHypertextIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->get_link = hypertext_get_link; + iface->get_n_links = hypertext_get_n_links; + iface->get_link_index = hypertext_get_link_index; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkimage.cxx b/vcl/unx/gtk3/a11y/gtk3atkimage.cxx index 4e2c77e9f2b1..1c9bc2c640f9 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkimage.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkimage.cxx @@ -5,8 +5,128 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkimage.cxx" +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleImage.hpp> + +using namespace ::com::sun::star; + +// FIXME +static G_CONST_RETURN gchar * +getAsConst( const OUString& rString ) +{ + static const int nMax = 10; + static OString aUgly[nMax]; + static int nIdx = 0; + nIdx = (nIdx + 1) % nMax; + aUgly[nIdx] = OUStringToOString( rString, RTL_TEXTENCODING_UTF8 ); + return aUgly[ nIdx ].getStr(); +} + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleImage> + getImage( AtkImage *pImage ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pImage ); + if( pWrap ) + { + if( !pWrap->mpImage.is() ) + { + pWrap->mpImage.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpImage; + } + + return css::uno::Reference<css::accessibility::XAccessibleImage>(); +} + +extern "C" { + +static G_CONST_RETURN gchar * +image_get_image_description( AtkImage *image ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleImage> pImage + = getImage( image ); + if( pImage.is() ) + return getAsConst( pImage->getAccessibleImageDescription() ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleImageDescription()" ); + } + + return nullptr; +} + +static void +image_get_image_position( AtkImage *image, + gint *x, + gint *y, + AtkCoordType coord_type ) +{ + *x = *y = 0; + if( ATK_IS_COMPONENT( image ) ) + { + SAL_WNODEPRECATED_DECLARATIONS_PUSH + atk_component_get_position( ATK_COMPONENT( image ), x, y, coord_type ); + SAL_WNODEPRECATED_DECLARATIONS_POP + } + else + g_warning( "FIXME: no image position information" ); +} + +static void +image_get_image_size( AtkImage *image, + gint *width, + gint *height ) +{ + *width = 0; + *height = 0; + try { + css::uno::Reference<css::accessibility::XAccessibleImage> pImage + = getImage( image ); + if( pImage.is() ) + { + *width = pImage->getAccessibleImageWidth(); + *height = pImage->getAccessibleImageHeight(); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleImageHeight() or Width" ); + } +} + +static gboolean +image_set_image_description( AtkImage *, const gchar * ) +{ + g_warning ("FIXME: no set image description"); + return FALSE; +} + +} // extern "C" + +void +imageIfaceInit (AtkImageIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->set_image_description = image_set_image_description; + iface->get_image_description = image_get_image_description; + iface->get_image_position = image_get_image_position; + iface->get_image_size = image_get_image_size; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atklistener.cxx b/vcl/unx/gtk3/a11y/gtk3atklistener.cxx index eca1cd7ec1d0..c30bc638bced 100644 --- a/vcl/unx/gtk3/a11y/gtk3atklistener.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atklistener.cxx @@ -5,8 +5,735 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atklistener.cxx" +#ifdef AIX +#define _LINUX_SOURCE_COMPAT +#include <sys/timer.h> +#undef _LINUX_SOURCE_COMPAT +#endif + +#include <com/sun/star/accessibility/TextSegment.hpp> +#include <com/sun/star/accessibility/AccessibleEventId.hpp> +#include <com/sun/star/accessibility/AccessibleStateType.hpp> +#include <com/sun/star/accessibility/AccessibleTableModelChange.hpp> +#include <com/sun/star/accessibility/AccessibleTableModelChangeType.hpp> +#include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp> +#include <com/sun/star/lang/IndexOutOfBoundsException.hpp> + +#include "atklistener.hxx" +#include "atkwrapper.hxx" +#include <vcl/svapp.hxx> + +#include <rtl/ref.hxx> +#include <sal/log.hxx> + +#define DEBUG_ATK_LISTENER 0 + +#if DEBUG_ATK_LISTENER +#include <iostream> +#include <sstream> +#endif + +using namespace com::sun::star; + +AtkListener::AtkListener( AtkObjectWrapper* pWrapper ) : mpWrapper( pWrapper ) +{ + if( mpWrapper ) + { + g_object_ref( mpWrapper ); + updateChildList( mpWrapper->mpContext ); + } +} + +AtkListener::~AtkListener() +{ + if( mpWrapper ) + g_object_unref( mpWrapper ); +} + +/*****************************************************************************/ + +static AtkStateType mapState( const uno::Any &rAny ) +{ + sal_Int16 nState = accessibility::AccessibleStateType::INVALID; + rAny >>= nState; + return mapAtkState( nState ); +} + +/*****************************************************************************/ + +extern "C" { + // rhbz#1001768 - down to horrific problems releasing the solar mutex + // while destroying a Window - which occurs inside these notifications. + static gboolean + idle_defunc_state_change( AtkObject *atk_obj ) + { + SolarMutexGuard aGuard; + + // This is an equivalent to a state change to DEFUNC(T). + atk_object_notify_state_change( atk_obj, ATK_STATE_DEFUNCT, TRUE ); + if( atk_get_focus_object() == atk_obj ) + { + SAL_WNODEPRECATED_DECLARATIONS_PUSH + atk_focus_tracker_notify( nullptr ); + SAL_WNODEPRECATED_DECLARATIONS_POP + } + g_object_unref( G_OBJECT( atk_obj ) ); + return false; + } +} + +// XEventListener implementation +void AtkListener::disposing( const lang::EventObject& ) +{ + if( mpWrapper ) + { + AtkObject *atk_obj = ATK_OBJECT( mpWrapper ); + + // Release all interface references to avoid shutdown problems with + // global mutex + atk_object_wrapper_dispose( mpWrapper ); + + g_idle_add( reinterpret_cast<GSourceFunc>(idle_defunc_state_change), + g_object_ref( G_OBJECT( atk_obj ) ) ); + + // Release the wrapper object so that it can vanish .. + g_object_unref( mpWrapper ); + mpWrapper = nullptr; + } +} + +/*****************************************************************************/ + +static AtkObject *getObjFromAny( const uno::Any &rAny ) +{ + uno::Reference< accessibility::XAccessible > xAccessible; + rAny >>= xAccessible; + return xAccessible.is() ? atk_object_wrapper_ref( xAccessible ) : nullptr; +} + +/*****************************************************************************/ + +// Updates the child list held to provide the old IndexInParent on children_changed::remove +void AtkListener::updateChildList( + css::uno::Reference<css::accessibility::XAccessibleContext> const & + pContext) +{ + m_aChildList.clear(); + + uno::Reference< accessibility::XAccessibleStateSet > xStateSet = pContext->getAccessibleStateSet(); + if( xStateSet.is() + && !xStateSet->contains(accessibility::AccessibleStateType::DEFUNC) + && !xStateSet->contains(accessibility::AccessibleStateType::MANAGES_DESCENDANTS) ) + { + sal_Int32 nChildren = pContext->getAccessibleChildCount(); + m_aChildList.resize(nChildren); + for(sal_Int32 n = 0; n < nChildren; n++) + { + try + { + m_aChildList[n] = pContext->getAccessibleChild(n); + } + catch (lang::IndexOutOfBoundsException const&) + { + sal_Int32 nChildren2 = pContext->getAccessibleChildCount(); + assert(nChildren2 <= n && "consistency?"); + m_aChildList.resize(std::min(nChildren2, n)); + break; + } + } + } +} + +/*****************************************************************************/ + +void AtkListener::handleChildAdded( + const uno::Reference< accessibility::XAccessibleContext >& rxParent, + const uno::Reference< accessibility::XAccessible>& rxAccessible) +{ + AtkObject * pChild = rxAccessible.is() ? atk_object_wrapper_ref( rxAccessible ) : nullptr; + + if( pChild ) + { + updateChildList(rxParent); + + atk_object_wrapper_add_child( mpWrapper, pChild, + atk_object_get_index_in_parent( pChild )); + + g_object_unref( pChild ); + } +} + +/*****************************************************************************/ + +void AtkListener::handleChildRemoved( + const uno::Reference< accessibility::XAccessibleContext >& rxParent, + const uno::Reference< accessibility::XAccessible>& rxChild) +{ + sal_Int32 nIndex = -1; + + // Locate the child in the children list + size_t n, nmax = m_aChildList.size(); + for( n = 0; n < nmax; ++n ) + { + if( rxChild == m_aChildList[n] ) + { + nIndex = n; + break; + } + } + + // FIXME: two problems here: + // a) we get child-removed events for objects that are no real children + // in the accessibility hierarchy or have been removed before due to + // some child removing batch. + // b) spi_atk_bridge_signal_listener ignores the given parameters + // for children_changed events and always asks the parent for the + // 0. child, which breaks somehow on vanishing list boxes. + // Ignoring "remove" events for objects not in the m_aChildList + // for now. + if( nIndex >= 0 ) + { + uno::Reference<accessibility::XAccessibleEventBroadcaster> xBroadcaster( + rxChild->getAccessibleContext(), uno::UNO_QUERY); + + if (xBroadcaster.is()) + { + uno::Reference<accessibility::XAccessibleEventListener> xListener(this); + xBroadcaster->removeAccessibleEventListener(xListener); + } + + updateChildList(rxParent); + + AtkObject * pChild = atk_object_wrapper_ref( rxChild, false ); + if( pChild ) + { + atk_object_wrapper_remove_child( mpWrapper, pChild, nIndex ); + g_object_unref( pChild ); + } + } +} + +/*****************************************************************************/ + +void AtkListener::handleInvalidateChildren( + const uno::Reference< accessibility::XAccessibleContext >& rxParent) +{ + // Send notifications for all previous children + size_t n = m_aChildList.size(); + while( n-- > 0 ) + { + if( m_aChildList[n].is() ) + { + AtkObject * pChild = atk_object_wrapper_ref( m_aChildList[n], false ); + if( pChild ) + { + atk_object_wrapper_remove_child( mpWrapper, pChild, n ); + g_object_unref( pChild ); + } + } + } + + updateChildList(rxParent); + + // Send notifications for all new children + size_t nmax = m_aChildList.size(); + for( n = 0; n < nmax; ++n ) + { + if( m_aChildList[n].is() ) + { + AtkObject * pChild = atk_object_wrapper_ref( m_aChildList[n] ); + + if( pChild ) + { + atk_object_wrapper_add_child( mpWrapper, pChild, n ); + g_object_unref( pChild ); + } + } + } +} + +/*****************************************************************************/ + +static uno::Reference< accessibility::XAccessibleContext > +getAccessibleContextFromSource( const uno::Reference< uno::XInterface >& rxSource ) +{ + uno::Reference< accessibility::XAccessibleContext > xContext(rxSource, uno::UNO_QUERY); + if( ! xContext.is() ) + { + g_warning( "ERROR: Event source does not implement XAccessibleContext" ); + + // Second try - query for XAccessible, which should give us access to + // XAccessibleContext. + uno::Reference< accessibility::XAccessible > xAccessible(rxSource, uno::UNO_QUERY); + if( xAccessible.is() ) + xContext = xAccessible->getAccessibleContext(); + } + + return xContext; +} + +#if DEBUG_ATK_LISTENER + +namespace { + +void printNotifyEvent( const accessibility::AccessibleEventObject& rEvent ) +{ + static std::vector<const char*> aLabels = { + 0, + "NAME_CHANGED", // 01 + "DESCRIPTION_CHANGED", // 02 + "ACTION_CHANGED", // 03 + "STATE_CHANGED", // 04 + "ACTIVE_DESCENDANT_CHANGED", // 05 + "BOUNDRECT_CHANGED", // 06 + "CHILD", // 07 + "INVALIDATE_ALL_CHILDREN", // 08 + "SELECTION_CHANGED", // 09 + "VISIBLE_DATA_CHANGED", // 10 + "VALUE_CHANGED", // 11 + "CONTENT_FLOWS_FROM_RELATION_CHANGED", // 12 + "CONTENT_FLOWS_TO_RELATION_CHANGED", // 13 + "CONTROLLED_BY_RELATION_CHANGED", // 14 + "CONTROLLER_FOR_RELATION_CHANGED", // 15 + "LABEL_FOR_RELATION_CHANGED", // 16 + "LABELED_BY_RELATION_CHANGED", // 17 + "MEMBER_OF_RELATION_CHANGED", // 18 + "SUB_WINDOW_OF_RELATION_CHANGED", // 19 + "CARET_CHANGED", // 20 + "TEXT_SELECTION_CHANGED", // 21 + "TEXT_CHANGED", // 22 + "TEXT_ATTRIBUTE_CHANGED", // 23 + "HYPERTEXT_CHANGED", // 24 + "TABLE_CAPTION_CHANGED", // 25 + "TABLE_COLUMN_DESCRIPTION_CHANGED", // 26 + "TABLE_COLUMN_HEADER_CHANGED", // 27 + "TABLE_MODEL_CHANGED", // 28 + "TABLE_ROW_DESCRIPTION_CHANGED", // 29 + "TABLE_ROW_HEADER_CHANGED", // 30 + "TABLE_SUMMARY_CHANGED", // 31 + "LISTBOX_ENTRY_EXPANDED", // 32 + "LISTBOX_ENTRY_COLLAPSED", // 33 + "ACTIVE_DESCENDANT_CHANGED_NOFOCUS", // 34 + "SELECTION_CHANGED_ADD", // 35 + "SELECTION_CHANGED_REMOVE", // 36 + "SELECTION_CHANGED_WITHIN", // 37 + "PAGE_CHANGED", // 38 + "SECTION_CHANGED", // 39 + "COLUMN_CHANGED", // 40 + "ROLE_CHANGED", // 41 + }; + + static std::vector<const char*> aStates = { + "INVALID", // 00 + "ACTIVE", // 01 + "ARMED", // 02 + "BUSY", // 03 + "CHECKED", // 04 + "DEFUNC", // 05 + "EDITABLE", // 06 + "ENABLED", // 07 + "EXPANDABLE", // 08 + "EXPANDED", // 09 + "FOCUSABLE", // 10 + "FOCUSED", // 11 + "HORIZONTAL", // 12 + "ICONIFIED", // 13 + "INDETERMINATE", // 14 + "MANAGES_DESCENDANTS", // 15 + "MODAL", // 16 + "MULTI_LINE", // 17 + "MULTI_SELECTABLE", // 18 + "OPAQUE", // 19 + "PRESSED", // 20 + "RESIZABLE", // 21 + "SELECTABLE", // 22 + "SELECTED", // 23 + "SENSITIVE", // 24 + "SHOWING", // 25 + "SINGLE_LINE", // 26 + "STALE", // 27 + "TRANSIENT", // 28 + "VERTICAL", // 29 + "VISIBLE", // 30 + "MOVEABLE", // 31 + "DEFAULT", // 32 + "OFFSCREEN", // 33 + "COLLAPSE", // 34 + }; + + auto getOrUnknown = [](const std::vector<const char*>& rCont, size_t nIndex) -> std::string + { + return (nIndex < rCont.size()) ? rCont[nIndex] : "<unknown>"; + }; + + std::ostringstream os; + os << "--" << std::endl; + os << "* event = " << getOrUnknown(aLabels, rEvent.EventId) << std::endl; + + switch (rEvent.EventId) + { + case accessibility::AccessibleEventId::STATE_CHANGED: + { + sal_Int16 nState; + if (rEvent.OldValue >>= nState) + os << " * old state = " << getOrUnknown(aStates, nState); + if (rEvent.NewValue >>= nState) + os << " * new state = " << getOrUnknown(aStates, nState); + + os << std::endl; + break; + } + default: + ; + } + + std::cout << os.str(); +} + +} + +#endif + +void AtkListener::notifyEvent( const accessibility::AccessibleEventObject& aEvent ) +{ + if( !mpWrapper ) + return; + + AtkObject *atk_obj = ATK_OBJECT( mpWrapper ); + + switch( aEvent.EventId ) + { + // AtkObject signals: + // Hierarchy signals + case accessibility::AccessibleEventId::CHILD: + { + uno::Reference< accessibility::XAccessibleContext > xParent; + uno::Reference< accessibility::XAccessible > xChild; + + xParent = getAccessibleContextFromSource(aEvent.Source); + g_return_if_fail( xParent.is() ); + + if( aEvent.OldValue >>= xChild ) + handleChildRemoved(xParent, xChild); + + if( aEvent.NewValue >>= xChild ) + handleChildAdded(xParent, xChild); + break; + } + + case accessibility::AccessibleEventId::INVALIDATE_ALL_CHILDREN: + { + uno::Reference< accessibility::XAccessibleContext > xParent = getAccessibleContextFromSource(aEvent.Source); + g_return_if_fail( xParent.is() ); + + handleInvalidateChildren(xParent); + break; + } + + case accessibility::AccessibleEventId::NAME_CHANGED: + { + OUString aName; + if( aEvent.NewValue >>= aName ) + { + atk_object_set_name(atk_obj, + OUStringToOString(aName, RTL_TEXTENCODING_UTF8).getStr()); + } + break; + } + + case accessibility::AccessibleEventId::DESCRIPTION_CHANGED: + { + OUString aDescription; + if( aEvent.NewValue >>= aDescription ) + { + atk_object_set_description(atk_obj, + OUStringToOString(aDescription, RTL_TEXTENCODING_UTF8).getStr()); + } + break; + } + + case accessibility::AccessibleEventId::STATE_CHANGED: + { + AtkStateType eOldState = mapState( aEvent.OldValue ); + AtkStateType eNewState = mapState( aEvent.NewValue ); + + gboolean bState = eNewState != ATK_STATE_INVALID; + AtkStateType eRealState = bState ? eNewState : eOldState; + + atk_object_notify_state_change( atk_obj, eRealState, bState ); + break; + } + + case accessibility::AccessibleEventId::BOUNDRECT_CHANGED: + +#ifdef HAS_ATKRECTANGLE + if( ATK_IS_COMPONENT( atk_obj ) ) + { + AtkRectangle rect; + + atk_component_get_extents( ATK_COMPONENT( atk_obj ), + &rect.x, + &rect.y, + &rect.width, + &rect.height, + ATK_XY_SCREEN ); + + g_signal_emit_by_name( atk_obj, "bounds_changed", &rect ); + } + else + g_warning( "bounds_changed event for object not implementing AtkComponent\n"); +#endif + + break; + + case accessibility::AccessibleEventId::VISIBLE_DATA_CHANGED: + g_signal_emit_by_name( atk_obj, "visible-data-changed" ); + break; + + case accessibility::AccessibleEventId::ACTIVE_DESCENDANT_CHANGED: + { + AtkObject *pChild = getObjFromAny( aEvent.NewValue ); + if( pChild ) + { + g_signal_emit_by_name( atk_obj, "active-descendant-changed", pChild ); + g_object_unref( pChild ); + } + break; + } + + //ACTIVE_DESCENDANT_CHANGED_NOFOCUS (sic) appears to have been added + //as a workaround or an aid for the ia2 winaccessibility implementation + //so ignore it silently without warning here + case accessibility::AccessibleEventId::ACTIVE_DESCENDANT_CHANGED_NOFOCUS: + break; + + // #i92103# + case accessibility::AccessibleEventId::LISTBOX_ENTRY_EXPANDED: + { + AtkObject *pChild = getObjFromAny( aEvent.NewValue ); + if( pChild ) + { + atk_object_notify_state_change( pChild, ATK_STATE_EXPANDED, true ); + g_object_unref( pChild ); + } + break; + } + + case accessibility::AccessibleEventId::LISTBOX_ENTRY_COLLAPSED: + { + AtkObject *pChild = getObjFromAny( aEvent.NewValue ); + if( pChild ) + { + atk_object_notify_state_change( pChild, ATK_STATE_EXPANDED, false ); + g_object_unref( pChild ); + } + break; + } + + // AtkAction signals ... + case accessibility::AccessibleEventId::ACTION_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-actions"); + break; + + // AtkText + case accessibility::AccessibleEventId::CARET_CHANGED: + { + sal_Int32 nPos=0; + aEvent.NewValue >>= nPos; + g_signal_emit_by_name( atk_obj, "text_caret_moved", nPos ); + break; + } + case accessibility::AccessibleEventId::TEXT_CHANGED: + { + // TESTME: and remove this comment: + // cf. comphelper/source/misc/accessibletexthelper.cxx (implInitTextChangedEvent) + accessibility::TextSegment aDeletedText; + accessibility::TextSegment aInsertedText; + + // TODO: when GNOME starts to send "update" kind of events, change + // we need to re-think this implementation as well + if( aEvent.OldValue >>= aDeletedText ) + { + /* Remember the text segment here to be able to return removed text in get_text(). + * This is clearly a hack to be used until appropriate API exists in atk to pass + * the string value directly or we find a compelling reason to start caching the + * UTF-8 converted strings in the atk wrapper object. + */ + + g_object_set_data( G_OBJECT(atk_obj), "ooo::text_changed::delete", &aDeletedText); + + g_signal_emit_by_name( atk_obj, "text_changed::delete", + static_cast<gint>(aDeletedText.SegmentStart), + static_cast<gint>( aDeletedText.SegmentEnd - aDeletedText.SegmentStart ) ); + + g_object_steal_data( G_OBJECT(atk_obj), "ooo::text_changed::delete" ); + } + + if( aEvent.NewValue >>= aInsertedText ) + g_signal_emit_by_name( atk_obj, "text_changed::insert", + static_cast<gint>(aInsertedText.SegmentStart), + static_cast<gint>( aInsertedText.SegmentEnd - aInsertedText.SegmentStart ) ); + break; + } + + case accessibility::AccessibleEventId::TEXT_SELECTION_CHANGED: + { + g_signal_emit_by_name( atk_obj, "text-selection-changed" ); + break; + } + + case accessibility::AccessibleEventId::TEXT_ATTRIBUTE_CHANGED: + g_signal_emit_by_name( atk_obj, "text-attributes-changed" ); + break; + + // AtkValue + case accessibility::AccessibleEventId::VALUE_CHANGED: + g_object_notify( G_OBJECT( atk_obj ), "accessible-value" ); + break; + + case accessibility::AccessibleEventId::CONTENT_FLOWS_FROM_RELATION_CHANGED: + case accessibility::AccessibleEventId::CONTENT_FLOWS_TO_RELATION_CHANGED: + case accessibility::AccessibleEventId::CONTROLLED_BY_RELATION_CHANGED: + case accessibility::AccessibleEventId::CONTROLLER_FOR_RELATION_CHANGED: + case accessibility::AccessibleEventId::LABEL_FOR_RELATION_CHANGED: + case accessibility::AccessibleEventId::LABELED_BY_RELATION_CHANGED: + case accessibility::AccessibleEventId::MEMBER_OF_RELATION_CHANGED: + case accessibility::AccessibleEventId::SUB_WINDOW_OF_RELATION_CHANGED: + // FIXME: ask Bill how Atk copes with this little lot ... + break; + + // AtkTable + case accessibility::AccessibleEventId::TABLE_MODEL_CHANGED: + { + accessibility::AccessibleTableModelChange aChange; + aEvent.NewValue >>= aChange; + + sal_Int32 nRowsChanged = aChange.LastRow - aChange.FirstRow + 1; + sal_Int32 nColumnsChanged = aChange.LastColumn - aChange.FirstColumn + 1; + + static const struct { + const char *row; + const char *col; + } aSignalNames[] = + { + { nullptr, nullptr }, // dummy + { "row_inserted", "column_inserted" }, // INSERT = 1 + { "row_deleted", "column_deleted" } // DELETE = 2 + }; + switch( aChange.Type ) + { + case accessibility::AccessibleTableModelChangeType::INSERT: + case accessibility::AccessibleTableModelChangeType::DELETE: + if( nRowsChanged > 0 ) + g_signal_emit_by_name( G_OBJECT( atk_obj ), + aSignalNames[aChange.Type].row, + aChange.FirstRow, nRowsChanged ); + if( nColumnsChanged > 0 ) + g_signal_emit_by_name( G_OBJECT( atk_obj ), + aSignalNames[aChange.Type].col, + aChange.FirstColumn, nColumnsChanged ); + break; + + case accessibility::AccessibleTableModelChangeType::UPDATE: + // This is not really a model change, is it ? + break; + default: + g_warning( "TESTME: unusual table model change %d\n", aChange.Type ); + break; + } + g_signal_emit_by_name( G_OBJECT( atk_obj ), "model-changed" ); + break; + } + + case accessibility::AccessibleEventId::TABLE_COLUMN_HEADER_CHANGED: + { + accessibility::AccessibleTableModelChange aChange; + aEvent.NewValue >>= aChange; + + AtkPropertyValues values; + memset(&values, 0, sizeof(AtkPropertyValues)); + g_value_init (&values.new_value, G_TYPE_INT); + values.property_name = "accessible-table-column-header"; + + for (sal_Int32 nChangedColumn = aChange.FirstColumn; nChangedColumn <= aChange.LastColumn; ++nChangedColumn) + { + g_value_set_int (&values.new_value, nChangedColumn); + g_signal_emit_by_name(G_OBJECT(atk_obj), "property_change::accessible-table-column-header", &values, nullptr); + } + break; + } + + case accessibility::AccessibleEventId::TABLE_CAPTION_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-caption"); + break; + + case accessibility::AccessibleEventId::TABLE_COLUMN_DESCRIPTION_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-column-description"); + break; + + case accessibility::AccessibleEventId::TABLE_ROW_DESCRIPTION_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-row-description"); + break; + + case accessibility::AccessibleEventId::TABLE_ROW_HEADER_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-row-header"); + break; + + case accessibility::AccessibleEventId::TABLE_SUMMARY_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-summary"); + break; + + case accessibility::AccessibleEventId::SELECTION_CHANGED: + case accessibility::AccessibleEventId::SELECTION_CHANGED_ADD: + case accessibility::AccessibleEventId::SELECTION_CHANGED_REMOVE: + case accessibility::AccessibleEventId::SELECTION_CHANGED_WITHIN: + if (ATK_IS_SELECTION(atk_obj)) + g_signal_emit_by_name(G_OBJECT(atk_obj), "selection_changed"); + else + { + // e.g. tdf#122353, when such dialogs become native the problem will go away anyway + SAL_INFO("vcl.gtk", "selection change from obj which doesn't support XAccessibleSelection"); + } + break; + + case accessibility::AccessibleEventId::HYPERTEXT_CHANGED: + g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-hypertext-offset"); + break; + + case accessibility::AccessibleEventId::ROLE_CHANGED: + { + uno::Reference< accessibility::XAccessibleContext > xContext = getAccessibleContextFromSource( aEvent.Source ); + atk_object_wrapper_set_role( mpWrapper, xContext->getAccessibleRole() ); + break; + } + + case accessibility::AccessibleEventId::PAGE_CHANGED: + { + /* // If we implemented AtkDocument then I imagine this is what this + // handler should look like + sal_Int32 nPos=0; + aEvent.NewValue >>= nPos; + g_signal_emit_by_name( G_OBJECT( atk_obj ), "page_changed", nPos ); + */ + break; + } + + default: + SAL_WARN("vcl.gtk", "Unknown event notification: " << aEvent.EventId); + break; + } +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkregistry.cxx b/vcl/unx/gtk3/a11y/gtk3atkregistry.cxx index 126e97a808ae..ff96378c41dc 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkregistry.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkregistry.cxx @@ -5,8 +5,62 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkregistry.cxx" +#include "atkregistry.hxx" + +using namespace ::com::sun::star::accessibility; +using namespace ::com::sun::star::uno; + +static GHashTable *uno_to_gobject = nullptr; + +/*****************************************************************************/ + +AtkObject * +ooo_wrapper_registry_get(const Reference< XAccessible >& rxAccessible) +{ + if( uno_to_gobject ) + { + gpointer cached = + g_hash_table_lookup(uno_to_gobject, static_cast<gpointer>(rxAccessible.get())); + + if( cached ) + return ATK_OBJECT( cached ); + } + + return nullptr; +} + +/*****************************************************************************/ + +void +ooo_wrapper_registry_add(const Reference< XAccessible >& rxAccessible, AtkObject *obj) +{ + if( !uno_to_gobject ) + uno_to_gobject = g_hash_table_new (nullptr, nullptr); + + g_hash_table_insert( uno_to_gobject, static_cast<gpointer>(rxAccessible.get()), obj ); +} + +/*****************************************************************************/ + +void +ooo_wrapper_registry_remove( + css::uno::Reference<css::accessibility::XAccessible> const & pAccessible) +{ + if( uno_to_gobject ) + g_hash_table_remove( + uno_to_gobject, static_cast<gpointer>(pAccessible.get()) ); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkselection.cxx b/vcl/unx/gtk3/a11y/gtk3atkselection.cxx index f67b665304d7..1d9772bc1587 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkselection.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkselection.cxx @@ -5,8 +5,186 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkselection.cxx" +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleSelection.hpp> + +using namespace ::com::sun::star; + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleSelection> + getSelection( AtkSelection *pSelection ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pSelection ); + if( pWrap ) + { + if( !pWrap->mpSelection.is() ) + { + pWrap->mpSelection.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpSelection; + } + + return css::uno::Reference<css::accessibility::XAccessibleSelection>(); +} + +extern "C" { + +static gboolean +selection_add_selection( AtkSelection *selection, + gint i ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + { + pSelection->selectAccessibleChild( i ); + return TRUE; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in selectAccessibleChild()" ); + } + + return FALSE; +} + +static gboolean +selection_clear_selection( AtkSelection *selection ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + { + pSelection->clearAccessibleSelection(); + return TRUE; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in selectAccessibleChild()" ); + } + + return FALSE; +} + +static AtkObject* +selection_ref_selection( AtkSelection *selection, + gint i ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + return atk_object_wrapper_ref( pSelection->getSelectedAccessibleChild( i ) ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleChild()" ); + } + + return nullptr; +} + +static gint +selection_get_selection_count( AtkSelection *selection) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + return pSelection->getSelectedAccessibleChildCount(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleChildCount()" ); + } + + return -1; +} + +static gboolean +selection_is_child_selected( AtkSelection *selection, + gint i) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + return pSelection->isAccessibleChildSelected( i ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleChildCount()" ); + } + + return FALSE; +} + +static gboolean +selection_remove_selection( AtkSelection *selection, + gint i ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + { + pSelection->deselectAccessibleChild( i ); + return TRUE; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleChildCount()" ); + } + + return FALSE; +} + +static gboolean +selection_select_all_selection( AtkSelection *selection) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection + = getSelection( selection ); + if( pSelection.is() ) + { + pSelection->selectAllAccessibleChildren(); + return TRUE; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleChildCount()" ); + } + + return FALSE; +} + +} // extern "C" + +void +selectionIfaceInit( AtkSelectionIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->add_selection = selection_add_selection; + iface->clear_selection = selection_clear_selection; + iface->ref_selection = selection_ref_selection; + iface->get_selection_count = selection_get_selection_count; + iface->is_child_selected = selection_is_child_selected; + iface->remove_selection = selection_remove_selection; + iface->select_all_selection = selection_select_all_selection; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atktable.cxx b/vcl/unx/gtk3/a11y/gtk3atktable.cxx index d886ac07296a..29ffa48d5b9d 100644 --- a/vcl/unx/gtk3/a11y/gtk3atktable.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atktable.cxx @@ -5,8 +5,576 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atktable.cxx" +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleTable.hpp> +#include <comphelper/sequence.hxx> + +using namespace ::com::sun::star; + +static AtkObject * +atk_object_wrapper_conditional_ref( const uno::Reference< accessibility::XAccessible >& rxAccessible ) +{ + if( rxAccessible.is() ) + return atk_object_wrapper_ref( rxAccessible ); + + return nullptr; +} + +/*****************************************************************************/ + +// FIXME +static G_CONST_RETURN gchar * +getAsConst( const OUString& rString ) +{ + static const int nMax = 10; + static OString aUgly[nMax]; + static int nIdx = 0; + nIdx = (nIdx + 1) % nMax; + aUgly[nIdx] = OUStringToOString( rString, RTL_TEXTENCODING_UTF8 ); + return aUgly[ nIdx ].getStr(); +} + +/*****************************************************************************/ + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleTable> + getTable( AtkTable *pTable ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pTable ); + if( pWrap ) + { + if( !pWrap->mpTable.is() ) + { + pWrap->mpTable.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpTable; + } + + return css::uno::Reference<css::accessibility::XAccessibleTable>(); +} + +/*****************************************************************************/ + +extern "C" { + +static AtkObject* +table_wrapper_ref_at (AtkTable *table, + gint row, + gint column) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable = getTable( table ); + if( pTable.is() ) + return atk_object_wrapper_conditional_ref( pTable->getAccessibleCellAt( row, column ) ); + } + + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleCellAt()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_index_at (AtkTable *table, + gint row, + gint column) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleIndex( row, column ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleIndex()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_column_at_index (AtkTable *table, + gint nIndex) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleColumn( nIndex ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleColumn()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_row_at_index( AtkTable *table, + gint nIndex ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleRow( nIndex ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleRow()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_n_columns( AtkTable *table ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleColumnCount(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleColumnCount()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_n_rows( AtkTable *table ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleRowCount(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleRowCount()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_column_extent_at( AtkTable *table, + gint row, + gint column ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleColumnExtentAt( row, column ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleColumnExtentAt()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_row_extent_at( AtkTable *table, + gint row, + gint column ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->getAccessibleRowExtentAt( row, column ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleRowExtentAt()" ); + } + + return -1; +} + +/*****************************************************************************/ + +static AtkObject * +table_wrapper_get_caption( AtkTable *table ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return atk_object_wrapper_conditional_ref( pTable->getAccessibleCaption() ); + } + + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleCaption()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static G_CONST_RETURN gchar * +table_wrapper_get_row_description( AtkTable *table, + gint row ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return getAsConst( pTable->getAccessibleRowDescription( row ) ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleRowDescription()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static G_CONST_RETURN gchar * +table_wrapper_get_column_description( AtkTable *table, + gint column ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return getAsConst( pTable->getAccessibleColumnDescription( column ) ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleColumnDescription()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static AtkObject * +table_wrapper_get_row_header( AtkTable *table, + gint row ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + { + uno::Reference< accessibility::XAccessibleTable > xRowHeaders( pTable->getAccessibleRowHeaders() ); + if( xRowHeaders.is() ) + return atk_object_wrapper_conditional_ref( xRowHeaders->getAccessibleCellAt( row, 0 ) ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleRowHeaders()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static AtkObject * +table_wrapper_get_column_header( AtkTable *table, + gint column ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + { + uno::Reference< accessibility::XAccessibleTable > xColumnHeaders( pTable->getAccessibleColumnHeaders() ); + if( xColumnHeaders.is() ) + return atk_object_wrapper_conditional_ref( xColumnHeaders->getAccessibleCellAt( 0, column ) ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleColumnHeaders()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static AtkObject * +table_wrapper_get_summary( AtkTable *table ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + { + return atk_object_wrapper_conditional_ref( pTable->getAccessibleSummary() ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleSummary()" ); + } + + return nullptr; +} + +/*****************************************************************************/ + +static gint +convertToGIntArray( const uno::Sequence< ::sal_Int32 >& aSequence, gint **pSelected ) +{ + if( aSequence.hasElements() ) + { + *pSelected = g_new( gint, aSequence.getLength() ); + + *pSelected = comphelper::sequenceToArray(*pSelected, aSequence); + } + + return aSequence.getLength(); +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_selected_columns( AtkTable *table, + gint **pSelected ) +{ + *pSelected = nullptr; + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return convertToGIntArray( pTable->getSelectedAccessibleColumns(), pSelected ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleColumns()" ); + } + + return 0; +} + +/*****************************************************************************/ + +static gint +table_wrapper_get_selected_rows( AtkTable *table, + gint **pSelected ) +{ + *pSelected = nullptr; + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return convertToGIntArray( pTable->getSelectedAccessibleRows(), pSelected ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectedAccessibleRows()" ); + } + + return 0; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_is_column_selected( AtkTable *table, + gint column ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->isAccessibleColumnSelected( column ); + } + catch(const uno::Exception&) { + g_warning( "Exception in isAccessibleColumnSelected()" ); + } + + return 0; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_is_row_selected( AtkTable *table, + gint row ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->isAccessibleRowSelected( row ); + } + catch(const uno::Exception&) { + g_warning( "Exception in isAccessibleRowSelected()" ); + } + + return FALSE; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_is_selected( AtkTable *table, + gint row, + gint column ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleTable> pTable + = getTable( table ); + if( pTable.is() ) + return pTable->isAccessibleSelected( row, column ); + } + catch(const uno::Exception&) { + g_warning( "Exception in isAccessibleSelected()" ); + } + + return FALSE; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_add_row_selection( AtkTable *, gint ) +{ + g_warning( "FIXME: no simple analogue for add_row_selection" ); + return 0; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_remove_row_selection( AtkTable *, gint ) +{ + g_warning( "FIXME: no simple analogue for remove_row_selection" ); + return 0; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_add_column_selection( AtkTable *, gint ) +{ + g_warning( "FIXME: no simple analogue for add_column_selection" ); + return 0; +} + +/*****************************************************************************/ + +static gboolean +table_wrapper_remove_column_selection( AtkTable *, gint ) +{ + g_warning( "FIXME: no simple analogue for remove_column_selection" ); + return 0; +} + +/*****************************************************************************/ + +static void +table_wrapper_set_caption( AtkTable *, AtkObject * ) +{ // meaningless helper +} + +/*****************************************************************************/ + +static void +table_wrapper_set_column_description( AtkTable *, gint, const gchar * ) +{ // meaningless helper +} + +/*****************************************************************************/ + +static void +table_wrapper_set_column_header( AtkTable *, gint, AtkObject * ) +{ // meaningless helper +} + +/*****************************************************************************/ + +static void +table_wrapper_set_row_description( AtkTable *, gint, const gchar * ) +{ // meaningless helper +} + +/*****************************************************************************/ + +static void +table_wrapper_set_row_header( AtkTable *, gint, AtkObject * ) +{ // meaningless helper +} + +/*****************************************************************************/ + +static void +table_wrapper_set_summary( AtkTable *, AtkObject * ) +{ // meaningless helper +} + +/*****************************************************************************/ + +} // extern "C" + +void +tableIfaceInit (AtkTableIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->ref_at = table_wrapper_ref_at; + iface->get_n_rows = table_wrapper_get_n_rows; + iface->get_n_columns = table_wrapper_get_n_columns; + iface->get_index_at = table_wrapper_get_index_at; + iface->get_column_at_index = table_wrapper_get_column_at_index; + iface->get_row_at_index = table_wrapper_get_row_at_index; + iface->is_row_selected = table_wrapper_is_row_selected; + iface->is_selected = table_wrapper_is_selected; + iface->get_selected_rows = table_wrapper_get_selected_rows; + iface->add_row_selection = table_wrapper_add_row_selection; + iface->remove_row_selection = table_wrapper_remove_row_selection; + iface->add_column_selection = table_wrapper_add_column_selection; + iface->remove_column_selection = table_wrapper_remove_column_selection; + iface->get_selected_columns = table_wrapper_get_selected_columns; + iface->is_column_selected = table_wrapper_is_column_selected; + iface->get_column_extent_at = table_wrapper_get_column_extent_at; + iface->get_row_extent_at = table_wrapper_get_row_extent_at; + iface->get_row_header = table_wrapper_get_row_header; + iface->set_row_header = table_wrapper_set_row_header; + iface->get_column_header = table_wrapper_get_column_header; + iface->set_column_header = table_wrapper_set_column_header; + iface->get_caption = table_wrapper_get_caption; + iface->set_caption = table_wrapper_set_caption; + iface->get_summary = table_wrapper_get_summary; + iface->set_summary = table_wrapper_set_summary; + iface->get_row_description = table_wrapper_get_row_description; + iface->set_row_description = table_wrapper_set_row_description; + iface->get_column_description = table_wrapper_get_column_description; + iface->set_column_description = table_wrapper_set_column_description; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atktext.cxx b/vcl/unx/gtk3/a11y/gtk3atktext.cxx index e4bbd5a38980..532b55013ddd 100644 --- a/vcl/unx/gtk3/a11y/gtk3atktext.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atktext.cxx @@ -5,8 +5,833 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atktext.cxx" +#include "atkwrapper.hxx" +#include "atktextattributes.hxx" +#include <algorithm> + +#include <osl/diagnose.h> + +#include <com/sun/star/accessibility/AccessibleTextType.hpp> +#include <com/sun/star/accessibility/TextSegment.hpp> +#include <com/sun/star/accessibility/XAccessibleMultiLineText.hpp> +#include <com/sun/star/accessibility/XAccessibleText.hpp> +#include <com/sun/star/accessibility/XAccessibleTextAttributes.hpp> +#include <com/sun/star/accessibility/XAccessibleTextMarkup.hpp> +#include <com/sun/star/text/TextMarkupType.hpp> + +using namespace ::com::sun::star; + +static sal_Int16 +text_type_from_boundary(AtkTextBoundary boundary_type) +{ + switch(boundary_type) + { + case ATK_TEXT_BOUNDARY_CHAR: + return accessibility::AccessibleTextType::CHARACTER; + case ATK_TEXT_BOUNDARY_WORD_START: + case ATK_TEXT_BOUNDARY_WORD_END: + return accessibility::AccessibleTextType::WORD; + case ATK_TEXT_BOUNDARY_SENTENCE_START: + case ATK_TEXT_BOUNDARY_SENTENCE_END: + return accessibility::AccessibleTextType::SENTENCE; + case ATK_TEXT_BOUNDARY_LINE_START: + case ATK_TEXT_BOUNDARY_LINE_END: + return accessibility::AccessibleTextType::LINE; + default: + return -1; + } +} + +/*****************************************************************************/ + +static gchar * +adjust_boundaries( css::uno::Reference<css::accessibility::XAccessibleText> const & pText, + accessibility::TextSegment const & rTextSegment, + AtkTextBoundary boundary_type, + gint * start_offset, gint * end_offset ) +{ + accessibility::TextSegment aTextSegment; + OUString aString; + gint start = 0, end = 0; + + if( !rTextSegment.SegmentText.isEmpty() ) + { + switch(boundary_type) + { + case ATK_TEXT_BOUNDARY_CHAR: + case ATK_TEXT_BOUNDARY_LINE_START: + case ATK_TEXT_BOUNDARY_LINE_END: + case ATK_TEXT_BOUNDARY_SENTENCE_START: + start = rTextSegment.SegmentStart; + end = rTextSegment.SegmentEnd; + aString = rTextSegment.SegmentText; + break; + + // the OOo break iterator behaves as SENTENCE_START + case ATK_TEXT_BOUNDARY_SENTENCE_END: + start = rTextSegment.SegmentStart; + end = rTextSegment.SegmentEnd; + + if( start > 0 ) + --start; + if( end > 0 && end < pText->getCharacterCount() - 1 ) + --end; + + aString = pText->getTextRange(start, end); + break; + + case ATK_TEXT_BOUNDARY_WORD_START: + start = rTextSegment.SegmentStart; + + // Determine the start index of the next segment + aTextSegment = pText->getTextBehindIndex(rTextSegment.SegmentEnd, + text_type_from_boundary(boundary_type)); + if( !aTextSegment.SegmentText.isEmpty() ) + end = aTextSegment.SegmentStart; + else + end = pText->getCharacterCount(); + + aString = pText->getTextRange(start, end); + break; + + case ATK_TEXT_BOUNDARY_WORD_END: + end = rTextSegment.SegmentEnd; + + // Determine the end index of the previous segment + aTextSegment = pText->getTextBeforeIndex(rTextSegment.SegmentStart, + text_type_from_boundary(boundary_type)); + if( !aTextSegment.SegmentText.isEmpty() ) + start = aTextSegment.SegmentEnd; + else + start = 0; + + aString = pText->getTextRange(start, end); + break; + + default: + return nullptr; + } + } + + *start_offset = start; + *end_offset = end; + + return OUStringToGChar(aString); +} + +/*****************************************************************************/ + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleText> + getText( AtkText *pText ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); + if( pWrap ) + { + if( !pWrap->mpText.is() ) + { + pWrap->mpText.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpText; + } + + return css::uno::Reference<css::accessibility::XAccessibleText>(); +} + +/*****************************************************************************/ + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleTextMarkup> + getTextMarkup( AtkText *pText ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); + if( pWrap ) + { + if( !pWrap->mpTextMarkup.is() ) + { + pWrap->mpTextMarkup.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpTextMarkup; + } + + return css::uno::Reference<css::accessibility::XAccessibleTextMarkup>(); +} + +/*****************************************************************************/ + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleTextAttributes> + getTextAttributes( AtkText *pText ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); + if( pWrap ) + { + if( !pWrap->mpTextAttributes.is() ) + { + pWrap->mpTextAttributes.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpTextAttributes; + } + + return css::uno::Reference<css::accessibility::XAccessibleTextAttributes>(); +} + +/*****************************************************************************/ + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleMultiLineText> + getMultiLineText( AtkText *pText ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); + if( pWrap ) + { + if( !pWrap->mpMultiLineText.is() ) + { + pWrap->mpMultiLineText.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpMultiLineText; + } + + return css::uno::Reference<css::accessibility::XAccessibleMultiLineText>(); +} + +/*****************************************************************************/ + +extern "C" { + +static gchar * +text_wrapper_get_text (AtkText *text, + gint start_offset, + gint end_offset) +{ + gchar * ret = nullptr; + + g_return_val_if_fail( (end_offset == -1) || (end_offset >= start_offset), nullptr ); + + /* at-spi expects the delete event to be send before the deletion happened + * so we save the deleted string object in the UNO event notification and + * fool libatk-bridge.so here .. + */ + void * pData = g_object_get_data( G_OBJECT(text), "ooo::text_changed::delete" ); + if( pData != nullptr ) + { + accessibility::TextSegment * pTextSegment = + static_cast <accessibility::TextSegment *> (pData); + + if( pTextSegment->SegmentStart == start_offset && + pTextSegment->SegmentEnd == end_offset ) + { + OString aUtf8 = OUStringToOString( pTextSegment->SegmentText, RTL_TEXTENCODING_UTF8 ); + return g_strdup( aUtf8.getStr() ); + } + } + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + OUString aText; + sal_Int32 n = pText->getCharacterCount(); + + if( -1 == end_offset ) + aText = pText->getText(); + else if( start_offset < n ) + aText = pText->getTextRange(start_offset, end_offset); + + ret = g_strdup( OUStringToOString(aText, RTL_TEXTENCODING_UTF8 ).getStr() ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getText()" ); + } + + return ret; +} + +static gchar * +text_wrapper_get_text_after_offset (AtkText *text, + gint offset, + AtkTextBoundary boundary_type, + gint *start_offset, + gint *end_offset) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + accessibility::TextSegment aTextSegment = pText->getTextBehindIndex(offset, text_type_from_boundary(boundary_type)); + return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in get_text_after_offset()" ); + } + + return nullptr; +} + +static gchar * +text_wrapper_get_text_at_offset (AtkText *text, + gint offset, + AtkTextBoundary boundary_type, + gint *start_offset, + gint *end_offset) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + /* If the user presses the 'End' key, the caret will be placed behind the last character, + * which is the same index as the first character of the next line. In atk the magic offset + * '-2' is used to cover this special case. + */ + if ( + -2 == offset && + (ATK_TEXT_BOUNDARY_LINE_START == boundary_type || + ATK_TEXT_BOUNDARY_LINE_END == boundary_type) + ) + { + css::uno::Reference< + css::accessibility::XAccessibleMultiLineText> pMultiLineText + = getMultiLineText( text ); + if( pMultiLineText.is() ) + { + accessibility::TextSegment aTextSegment = pMultiLineText->getTextAtLineWithCaret(); + return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); + } + } + + accessibility::TextSegment aTextSegment = pText->getTextAtIndex(offset, text_type_from_boundary(boundary_type)); + return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in get_text_at_offset()" ); + } + + return nullptr; +} + +static gunichar +text_wrapper_get_character_at_offset (AtkText *text, + gint offset) +{ + gint start, end; + gunichar uc = 0; + + gchar * char_as_string = + text_wrapper_get_text_at_offset(text, offset, ATK_TEXT_BOUNDARY_CHAR, + &start, &end); + if( char_as_string ) + { + uc = g_utf8_get_char( char_as_string ); + g_free( char_as_string ); + } + + return uc; +} + +static gchar * +text_wrapper_get_text_before_offset (AtkText *text, + gint offset, + AtkTextBoundary boundary_type, + gint *start_offset, + gint *end_offset) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + accessibility::TextSegment aTextSegment = pText->getTextBeforeIndex(offset, text_type_from_boundary(boundary_type)); + return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in text_before_offset()" ); + } + + return nullptr; +} + +static gint +text_wrapper_get_caret_offset (AtkText *text) +{ + gint offset = -1; + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + offset = pText->getCaretPosition(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getCaretPosition()" ); + } + + return offset; +} + +static gboolean +text_wrapper_set_caret_offset (AtkText *text, + gint offset) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + return pText->setCaretPosition( offset ); + } + catch(const uno::Exception&) { + g_warning( "Exception in setCaretPosition()" ); + } + + return FALSE; +} + +// #i92232# +static AtkAttributeSet* +handle_text_markup_as_run_attribute( css::uno::Reference<css::accessibility::XAccessibleTextMarkup> const & pTextMarkup, + const gint nTextMarkupType, + const gint offset, + AtkAttributeSet* pSet, + gint *start_offset, + gint *end_offset ) +{ + const gint nTextMarkupCount( pTextMarkup->getTextMarkupCount( nTextMarkupType ) ); + if ( nTextMarkupCount > 0 ) + { + for ( gint nTextMarkupIndex = 0; + nTextMarkupIndex < nTextMarkupCount; + ++nTextMarkupIndex ) + { + accessibility::TextSegment aTextSegment = + pTextMarkup->getTextMarkup( nTextMarkupIndex, nTextMarkupType ); + const gint nStartOffsetTextMarkup = aTextSegment.SegmentStart; + const gint nEndOffsetTextMarkup = aTextSegment.SegmentEnd; + if ( nStartOffsetTextMarkup <= offset ) + { + if ( offset < nEndOffsetTextMarkup ) + { + // text markup at <offset> + *start_offset = ::std::max( *start_offset, + nStartOffsetTextMarkup ); + *end_offset = ::std::min( *end_offset, + nEndOffsetTextMarkup ); + switch ( nTextMarkupType ) + { + case css::text::TextMarkupType::SPELLCHECK: + { + pSet = attribute_set_prepend_misspelled( pSet ); + } + break; + case css::text::TextMarkupType::TRACK_CHANGE_INSERTION: + { + pSet = attribute_set_prepend_tracked_change_insertion( pSet ); + } + break; + case css::text::TextMarkupType::TRACK_CHANGE_DELETION: + { + pSet = attribute_set_prepend_tracked_change_deletion( pSet ); + } + break; + case css::text::TextMarkupType::TRACK_CHANGE_FORMATCHANGE: + { + pSet = attribute_set_prepend_tracked_change_formatchange( pSet ); + } + break; + default: + { + OSL_ASSERT( false ); + } + } + break; // no further iteration needed. + } + else + { + *start_offset = ::std::max( *start_offset, + nEndOffsetTextMarkup ); + // continue iteration. + } + } + else + { + *end_offset = ::std::min( *end_offset, + nStartOffsetTextMarkup ); + break; // no further iteration. + } + } // eof iteration over text markups + } + + return pSet; +} + +static AtkAttributeSet * +text_wrapper_get_run_attributes( AtkText *text, + gint offset, + gint *start_offset, + gint *end_offset) +{ + AtkAttributeSet *pSet = nullptr; + + try { + bool bOffsetsAreValid = false; + + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is()) + { + uno::Sequence< beans::PropertyValue > aAttributeList; + + css::uno::Reference<css::accessibility::XAccessibleTextAttributes> + pTextAttributes = getTextAttributes( text ); + if(pTextAttributes.is()) // Text attributes are available for paragraphs only + { + aAttributeList = pTextAttributes->getRunAttributes( offset, uno::Sequence< OUString > () ); + } + else // For other text objects use character attributes + { + aAttributeList = pText->getCharacterAttributes( offset, uno::Sequence< OUString > () ); + } + + pSet = attribute_set_new_from_property_values( aAttributeList, true, text ); + // #i100938# + // - always provide start_offset and end_offset + { + accessibility::TextSegment aTextSegment = + pText->getTextAtIndex(offset, accessibility::AccessibleTextType::ATTRIBUTE_RUN); + + *start_offset = aTextSegment.SegmentStart; + // #i100938# + // Do _not_ increment the end_offset provide by <accessibility::TextSegment> instance + *end_offset = aTextSegment.SegmentEnd; + bOffsetsAreValid = true; + } + } + + // Special handling for misspelled text + // #i92232# + // - add special handling for tracked changes and refactor the + // corresponding code for handling misspelled text. + css::uno::Reference<css::accessibility::XAccessibleTextMarkup> + pTextMarkup = getTextMarkup( text ); + if( pTextMarkup.is() ) + { + // Get attribute run here if it hasn't been done before + if (!bOffsetsAreValid && pText.is()) + { + accessibility::TextSegment aAttributeTextSegment = + pText->getTextAtIndex(offset, accessibility::AccessibleTextType::ATTRIBUTE_RUN); + *start_offset = aAttributeTextSegment.SegmentStart; + *end_offset = aAttributeTextSegment.SegmentEnd; + } + // handle misspelled text + pSet = handle_text_markup_as_run_attribute( + pTextMarkup, + css::text::TextMarkupType::SPELLCHECK, + offset, pSet, start_offset, end_offset ); + // handle tracked changes + pSet = handle_text_markup_as_run_attribute( + pTextMarkup, + css::text::TextMarkupType::TRACK_CHANGE_INSERTION, + offset, pSet, start_offset, end_offset ); + pSet = handle_text_markup_as_run_attribute( + pTextMarkup, + css::text::TextMarkupType::TRACK_CHANGE_DELETION, + offset, pSet, start_offset, end_offset ); + pSet = handle_text_markup_as_run_attribute( + pTextMarkup, + css::text::TextMarkupType::TRACK_CHANGE_FORMATCHANGE, + offset, pSet, start_offset, end_offset ); + } + } + catch(const uno::Exception&){ + + g_warning( "Exception in get_run_attributes()" ); + + if( pSet ) + { + atk_attribute_set_free( pSet ); + pSet = nullptr; + } + } + + return pSet; +} + +/*****************************************************************************/ + +static AtkAttributeSet * +text_wrapper_get_default_attributes( AtkText *text ) +{ + AtkAttributeSet *pSet = nullptr; + + try { + css::uno::Reference<css::accessibility::XAccessibleTextAttributes> + pTextAttributes = getTextAttributes( text ); + if( pTextAttributes.is() ) + { + uno::Sequence< beans::PropertyValue > aAttributeList = + pTextAttributes->getDefaultAttributes( uno::Sequence< OUString > () ); + + pSet = attribute_set_new_from_property_values( aAttributeList, false, text ); + } + } + catch(const uno::Exception&) { + + g_warning( "Exception in get_default_attributes()" ); + + if( pSet ) + { + atk_attribute_set_free( pSet ); + pSet = nullptr; + } + } + + return pSet; +} + +/*****************************************************************************/ + +static void +text_wrapper_get_character_extents( AtkText *text, + gint offset, + gint *x, + gint *y, + gint *width, + gint *height, + AtkCoordType coords ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + *x = *y = *width = *height = 0; + awt::Rectangle aRect = pText->getCharacterBounds( offset ); + + gint origin_x = 0; + gint origin_y = 0; + + if( coords == ATK_XY_SCREEN ) + { + g_return_if_fail( ATK_IS_COMPONENT( text ) ); + SAL_WNODEPRECATED_DECLARATIONS_PUSH + atk_component_get_position( ATK_COMPONENT( text ), &origin_x, &origin_y, coords); + SAL_WNODEPRECATED_DECLARATIONS_POP + } + + *x = aRect.X + origin_x; + *y = aRect.Y + origin_y; + *width = aRect.Width; + *height = aRect.Height; + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getCharacterBounds" ); + } +} + +static gint +text_wrapper_get_character_count (AtkText *text) +{ + gint rv = 0; + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + rv = pText->getCharacterCount(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getCharacterCount" ); + } + + return rv; +} + +static gint +text_wrapper_get_offset_at_point (AtkText *text, + gint x, + gint y, + AtkCoordType coords) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + gint origin_x = 0; + gint origin_y = 0; + + if( coords == ATK_XY_SCREEN ) + { + g_return_val_if_fail( ATK_IS_COMPONENT( text ), -1 ); + SAL_WNODEPRECATED_DECLARATIONS_PUSH + atk_component_get_position( ATK_COMPONENT( text ), &origin_x, &origin_y, coords); + SAL_WNODEPRECATED_DECLARATIONS_POP + } + + return pText->getIndexAtPoint( awt::Point(x - origin_x, y - origin_y) ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getIndexAtPoint" ); + } + + return -1; +} + +// FIXME: the whole series of selections API is problematic ... + +static gint +text_wrapper_get_n_selections (AtkText *text) +{ + gint rv = 0; + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + rv = ( pText->getSelectionEnd() > pText->getSelectionStart() ) ? 1 : 0; + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectionEnd() or getSelectionStart()" ); + } + + return rv; +} + +static gchar * +text_wrapper_get_selection (AtkText *text, + gint selection_num, + gint *start_offset, + gint *end_offset) +{ + g_return_val_if_fail( selection_num == 0, FALSE ); + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + { + *start_offset = pText->getSelectionStart(); + *end_offset = pText->getSelectionEnd(); + + return OUStringToGChar( pText->getSelectedText() ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getSelectionEnd(), getSelectionStart() or getSelectedText()" ); + } + + return nullptr; +} + +static gboolean +text_wrapper_add_selection (AtkText *text, + gint start_offset, + gint end_offset) +{ + // FIXME: can we try to be more compatible by expanding an + // existing adjacent selection ? + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + return pText->setSelection( start_offset, end_offset ); // ? + } + catch(const uno::Exception&) { + g_warning( "Exception in setSelection()" ); + } + + return FALSE; +} + +static gboolean +text_wrapper_remove_selection (AtkText *text, + gint selection_num) +{ + g_return_val_if_fail( selection_num == 0, FALSE ); + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + return pText->setSelection( 0, 0 ); // ? + } + catch(const uno::Exception&) { + g_warning( "Exception in setSelection()" ); + } + + return FALSE; +} + +static gboolean +text_wrapper_set_selection (AtkText *text, + gint selection_num, + gint start_offset, + gint end_offset) +{ + g_return_val_if_fail( selection_num == 0, FALSE ); + + try { + css::uno::Reference<css::accessibility::XAccessibleText> pText + = getText( text ); + if( pText.is() ) + return pText->setSelection( start_offset, end_offset ); + } + catch(const uno::Exception&) { + g_warning( "Exception in setSelection()" ); + } + + return FALSE; +} + +} // extern "C" + +void +textIfaceInit (AtkTextIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->get_text = text_wrapper_get_text; + iface->get_character_at_offset = text_wrapper_get_character_at_offset; + iface->get_text_before_offset = text_wrapper_get_text_before_offset; + iface->get_text_at_offset = text_wrapper_get_text_at_offset; + iface->get_text_after_offset = text_wrapper_get_text_after_offset; + iface->get_caret_offset = text_wrapper_get_caret_offset; + iface->set_caret_offset = text_wrapper_set_caret_offset; + iface->get_character_count = text_wrapper_get_character_count; + iface->get_n_selections = text_wrapper_get_n_selections; + iface->get_selection = text_wrapper_get_selection; + iface->add_selection = text_wrapper_add_selection; + iface->remove_selection = text_wrapper_remove_selection; + iface->set_selection = text_wrapper_set_selection; + iface->get_run_attributes = text_wrapper_get_run_attributes; + iface->get_default_attributes = text_wrapper_get_default_attributes; + iface->get_character_extents = text_wrapper_get_character_extents; + iface->get_offset_at_point = text_wrapper_get_offset_at_point; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atktextattributes.cxx b/vcl/unx/gtk3/a11y/gtk3atktextattributes.cxx index b0edad06a65c..0ba7fd561862 100644 --- a/vcl/unx/gtk3/a11y/gtk3atktextattributes.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atktextattributes.cxx @@ -5,8 +5,1379 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atktextattributes.cxx" +#include "atktextattributes.hxx" + +#include <com/sun/star/awt/FontSlant.hpp> +#include <com/sun/star/awt/FontStrikeout.hpp> +#include <com/sun/star/awt/FontUnderline.hpp> + +#include <com/sun/star/style/CaseMap.hpp> +#include <com/sun/star/style/LineSpacing.hpp> +#include <com/sun/star/style/LineSpacingMode.hpp> +#include <com/sun/star/style/ParagraphAdjust.hpp> +#include <com/sun/star/style/TabAlign.hpp> +#include <com/sun/star/style/TabStop.hpp> + +#include <com/sun/star/text/WritingMode2.hpp> + +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleComponent.hpp> + +#include <i18nlangtag/languagetag.hxx> +#include <vcl/svapp.hxx> +#include <vcl/outdev.hxx> + +#include <stdio.h> +#include <string.h> + +using namespace ::com::sun::star; + +typedef gchar* (* AtkTextAttrFunc) ( const uno::Any& rAny ); +typedef bool (* TextPropertyValueFunc) ( uno::Any& rAny, const gchar * value ); + +#define STRNCMP_PARAM( s ) s,sizeof( s )-1 + +/*****************************************************************************/ + +static AtkTextAttribute atk_text_attribute_paragraph_style = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_font_effect = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_decoration = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_line_height = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_rotation = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_shadow = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_tab_interval = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_tab_stops = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_writing_mode = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_vertical_align = ATK_TEXT_ATTR_INVALID; +static AtkTextAttribute atk_text_attribute_misspelled = ATK_TEXT_ATTR_INVALID; +// #i92232# +static AtkTextAttribute atk_text_attribute_tracked_change = ATK_TEXT_ATTR_INVALID; +// #i92233# +static AtkTextAttribute atk_text_attribute_mm_to_pixel_ratio = ATK_TEXT_ATTR_INVALID; + +/*****************************************************************************/ + +/** + * !! IMPORTANT NOTE !! : when adding items to this list, KEEP THE LIST SORTED + * and re-arrange the enum values accordingly. + */ + +enum ExportedAttribute +{ + TEXT_ATTRIBUTE_BACKGROUND_COLOR = 0, + TEXT_ATTRIBUTE_CASEMAP, + TEXT_ATTRIBUTE_FOREGROUND_COLOR, + TEXT_ATTRIBUTE_CONTOURED, + TEXT_ATTRIBUTE_CHAR_ESCAPEMENT, + TEXT_ATTRIBUTE_BLINKING, + TEXT_ATTRIBUTE_FONT_NAME, + TEXT_ATTRIBUTE_HEIGHT, + TEXT_ATTRIBUTE_HIDDEN, + TEXT_ATTRIBUTE_KERNING, + TEXT_ATTRIBUTE_LOCALE, + TEXT_ATTRIBUTE_POSTURE, + TEXT_ATTRIBUTE_RELIEF, + TEXT_ATTRIBUTE_ROTATION, + TEXT_ATTRIBUTE_SCALE, + TEXT_ATTRIBUTE_SHADOWED, + TEXT_ATTRIBUTE_STRIKETHROUGH, + TEXT_ATTRIBUTE_UNDERLINE, + TEXT_ATTRIBUTE_WEIGHT, + // #i92233# + TEXT_ATTRIBUTE_MM_TO_PIXEL_RATIO, + TEXT_ATTRIBUTE_JUSTIFICATION, + TEXT_ATTRIBUTE_BOTTOM_MARGIN, + TEXT_ATTRIBUTE_FIRST_LINE_INDENT, + TEXT_ATTRIBUTE_LEFT_MARGIN, + TEXT_ATTRIBUTE_LINE_SPACING, + TEXT_ATTRIBUTE_RIGHT_MARGIN, + TEXT_ATTRIBUTE_STYLE_NAME, + TEXT_ATTRIBUTE_TAB_STOPS, + TEXT_ATTRIBUTE_TOP_MARGIN, + TEXT_ATTRIBUTE_WRITING_MODE, + TEXT_ATTRIBUTE_LAST +}; + +static const char * ExportedTextAttributes[TEXT_ATTRIBUTE_LAST] = +{ + "CharBackColor", // TEXT_ATTRIBUTE_BACKGROUND_COLOR + "CharCaseMap", // TEXT_ATTRIBUTE_CASEMAP + "CharColor", // TEXT_ATTRIBUTE_FOREGROUND_COLOR + "CharContoured", // TEXT_ATTRIBUTE_CONTOURED + "CharEscapement", // TEXT_ATTRIBUTE_CHAR_ESCAPEMENT + "CharFlash", // TEXT_ATTRIBUTE_BLINKING + "CharFontName", // TEXT_ATTRIBUTE_FONT_NAME + "CharHeight", // TEXT_ATTRIBUTE_HEIGHT + "CharHidden", // TEXT_ATTRIBUTE_HIDDEN + "CharKerning", // TEXT_ATTRIBUTE_KERNING + "CharLocale", // TEXT_ATTRIBUTE_LOCALE + "CharPosture", // TEXT_ATTRIBUTE_POSTURE + "CharRelief", // TEXT_ATTRIBUTE_RELIEF + "CharRotation", // TEXT_ATTRIBUTE_ROTATION + "CharScaleWidth", // TEXT_ATTRIBUTE_SCALE + "CharShadowed", // TEXT_ATTRIBUTE_SHADOWED + "CharStrikeout", // TEXT_ATTRIBUTE_STRIKETHROUGH + "CharUnderline", // TEXT_ATTRIBUTE_UNDERLINE + "CharWeight", // TEXT_ATTRIBUTE_WEIGHT + // #i92233# + "MMToPixelRatio", // TEXT_ATTRIBUTE_MM_TO_PIXEL_RATIO + "ParaAdjust", // TEXT_ATTRIBUTE_JUSTIFICATION + "ParaBottomMargin", // TEXT_ATTRIBUTE_BOTTOM_MARGIN + "ParaFirstLineIndent", // TEXT_ATTRIBUTE_FIRST_LINE_INDENT + "ParaLeftMargin", // TEXT_ATTRIBUTE_LEFT_MARGIN + "ParaLineSpacing", // TEXT_ATTRIBUTE_LINE_SPACING + "ParaRightMargin", // TEXT_ATTRIBUTE_RIGHT_MARGIN + "ParaStyleName", // TEXT_ATTRIBUTE_STYLE_NAME + "ParaTabStops", // TEXT_ATTRIBUTE_TAB_STOPS + "ParaTopMargin", // TEXT_ATTRIBUTE_TOP_MARGIN + "WritingMode" // TEXT_ATTRIBUTE_WRITING_MODE +}; + +/*****************************************************************************/ + +static gchar* +get_value( const uno::Sequence< beans::PropertyValue >& rAttributeList, + sal_Int32 nIndex, AtkTextAttrFunc func ) +{ + if( nIndex != -1 ) + return func(rAttributeList[nIndex].Value); + + return nullptr; +} + +#define get_bool_value( list, index ) get_value( list, index, Bool2String ) +#define get_height_value( list, index ) get_value( list, index, Float2String ) +#define get_justification_value( list, index ) get_value( list, index, Adjust2Justification ) +#define get_cmm_value( list, index ) get_value( list, index, CMM2UnitString ) +#define get_scale_width( list, index ) get_value( list, index, Scale2String ) +#define get_strikethrough_value( list, index ) get_value( list, index, Strikeout2String ) +#define get_string_value( list, index ) get_value( list, index, GetString ) +#define get_style_value( list, index ) get_value( list, index, FontSlant2Style ) +#define get_underline_value( list, index ) get_value( list, index, Underline2String ) +#define get_variant_value( list, index ) get_value( list, index, CaseMap2String ) +#define get_weight_value( list, index ) get_value( list, index, Weight2String ) +#define get_language_string( list, index ) get_value( list, index, Locale2String ) + +static double toPoint(sal_Int16 n) +{ + // 100th mm -> pt + return static_cast<double>(n * 72) / 2540; +} + +/*****************************************************************************/ + +static bool +InvalidValue( uno::Any&, const gchar * ) +{ + return false; +} + +/*****************************************************************************/ + +static gchar* +Float2String(const uno::Any& rAny) +{ + return g_strdup_printf( "%g", rAny.get<float>() ); +} + +static bool +String2Float( uno::Any& rAny, const gchar * value ) +{ + float fval; + + if( 1 != sscanf( value, "%g", &fval ) ) + return false; + + rAny <<= fval; + return true; +} + +/*****************************************************************************/ + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleComponent> + getComponent( AtkText *pText ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); + if( pWrap ) + { + if( !pWrap->mpComponent.is() ) + { + pWrap->mpComponent.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpComponent; + } + + return css::uno::Reference<css::accessibility::XAccessibleComponent>(); +} + +static gchar* +get_color_value(const uno::Sequence< beans::PropertyValue >& rAttributeList, + const sal_Int32 * pIndexArray, + ExportedAttribute attr, + AtkText * text) +{ + sal_Int32 nColor = -1; // AUTOMATIC + sal_Int32 nIndex = pIndexArray[attr]; + + if( nIndex != -1 ) + nColor = rAttributeList[nIndex].Value.get<sal_Int32>(); + + /* + * Check for color value for 100% alpha white, which means + * "automatic". Grab the RGB value from XAccessibleComponent + * in this case. + */ + + if( (nColor == -1) && text ) + { + try + { + css::uno::Reference<css::accessibility::XAccessibleComponent> + pComponent = getComponent( text ); + if( pComponent.is() ) + { + switch( attr ) + { + case TEXT_ATTRIBUTE_BACKGROUND_COLOR: + nColor = pComponent->getBackground(); + break; + case TEXT_ATTRIBUTE_FOREGROUND_COLOR: + nColor = pComponent->getForeground(); + break; + default: + break; + } + } + } + + catch(const uno::Exception&) { + g_warning( "Exception in get[Fore|Back]groundColor()" ); + } + } + + if( nColor != -1 ) + { + sal_uInt8 blue = nColor & 0xFF; + sal_uInt8 green = (nColor >> 8) & 0xFF; + sal_uInt8 red = (nColor >> 16) & 0xFF; + + return g_strdup_printf( "%u,%u,%u", red, green, blue ); + } + + return nullptr; +} + +static bool +String2Color( uno::Any& rAny, const gchar * value ) +{ + int red, green, blue; + + if( 3 != sscanf( value, "%d,%d,%d", &red, &green, &blue ) ) + return false; + + sal_Int32 nColor = static_cast<sal_Int32>(blue) | ( static_cast<sal_Int32>(green) << 8 ) | ( static_cast<sal_Int32>(red) << 16 ); + rAny <<= nColor; + return true; +} + +/*****************************************************************************/ + +static gchar* +FontSlant2Style(const uno::Any& rAny) +{ + const gchar * value = nullptr; + + awt::FontSlant aFontSlant; + if(!(rAny >>= aFontSlant)) + return nullptr; + + switch( aFontSlant ) + { + case awt::FontSlant_NONE: + value = "normal"; + break; + + case awt::FontSlant_OBLIQUE: + value = "oblique"; + break; + + case awt::FontSlant_ITALIC: + value = "italic"; + break; + + case awt::FontSlant_REVERSE_OBLIQUE: + value = "reverse oblique"; + break; + + case awt::FontSlant_REVERSE_ITALIC: + value = "reverse italic"; + break; + + default: + break; + } + + if( value ) + return g_strdup( value ); + + return nullptr; +} + +static bool +Style2FontSlant( uno::Any& rAny, const gchar * value ) +{ + awt::FontSlant aFontSlant; + + if( strncmp( value, STRNCMP_PARAM( "normal" ) ) == 0 ) + aFontSlant = awt::FontSlant_NONE; + else if( strncmp( value, STRNCMP_PARAM( "oblique" ) ) == 0 ) + aFontSlant = awt::FontSlant_OBLIQUE; + else if( strncmp( value, STRNCMP_PARAM( "italic" ) ) == 0 ) + aFontSlant = awt::FontSlant_ITALIC; + else if( strncmp( value, STRNCMP_PARAM( "reverse oblique" ) ) == 0 ) + aFontSlant = awt::FontSlant_REVERSE_OBLIQUE; + else if( strncmp( value, STRNCMP_PARAM( "reverse italic" ) ) == 0 ) + aFontSlant = awt::FontSlant_REVERSE_ITALIC; + else + return false; + + rAny <<= aFontSlant; + return true; +} + +/*****************************************************************************/ + +static gchar* +Weight2String(const uno::Any& rAny) +{ + return g_strdup_printf( "%g", rAny.get<float>() * 4 ); +} + +static bool +String2Weight( uno::Any& rAny, const gchar * value ) +{ + float weight; + + if( 1 != sscanf( value, "%g", &weight ) ) + return false; + + rAny <<= weight / 4; + return true; +} + +/*****************************************************************************/ + +static gchar* +Adjust2Justification(const uno::Any& rAny) +{ + const gchar * value = nullptr; + + switch( static_cast<style::ParagraphAdjust>(rAny.get<short>()) ) + { + case style::ParagraphAdjust_LEFT: + value = "left"; + break; + + case style::ParagraphAdjust_RIGHT: + value = "right"; + break; + + case style::ParagraphAdjust_BLOCK: + case style::ParagraphAdjust_STRETCH: + value = "fill"; + break; + + case style::ParagraphAdjust_CENTER: + value = "center"; + break; + + default: + break; + } + + if( value ) + return g_strdup( value ); + + return nullptr; +} + +static bool +Justification2Adjust( uno::Any& rAny, const gchar * value ) +{ + style::ParagraphAdjust nParagraphAdjust; + + if( strncmp( value, STRNCMP_PARAM( "left" ) ) == 0 ) + nParagraphAdjust = style::ParagraphAdjust_LEFT; + else if( strncmp( value, STRNCMP_PARAM( "right" ) ) == 0 ) + nParagraphAdjust = style::ParagraphAdjust_RIGHT; + else if( strncmp( value, STRNCMP_PARAM( "fill" ) ) == 0 ) + nParagraphAdjust = style::ParagraphAdjust_BLOCK; + else if( strncmp( value, STRNCMP_PARAM( "center" ) ) == 0 ) + nParagraphAdjust = style::ParagraphAdjust_CENTER; + else + return false; + + rAny <<= static_cast<short>(nParagraphAdjust); + return true; +} + +/*****************************************************************************/ + +const gchar * const font_strikethrough[] = { + "none", // FontStrikeout::NONE + "single", // FontStrikeout::SINGLE + "double", // FontStrikeout::DOUBLE + nullptr, // FontStrikeout::DONTKNOW + "bold", // FontStrikeout::BOLD + "with /", // FontStrikeout::SLASH + "with X" // FontStrikeout::X +}; + +static gchar* +Strikeout2String(const uno::Any& rAny) +{ + sal_Int16 n = rAny.get<sal_Int16>(); + + if( n >= 0 && n < sal_Int16(SAL_N_ELEMENTS(font_strikethrough)) ) + return g_strdup( font_strikethrough[n] ); + + return nullptr; +} + +static bool +String2Strikeout( uno::Any& rAny, const gchar * value ) +{ + for( sal_Int16 n=0; n < sal_Int16(SAL_N_ELEMENTS(font_strikethrough)); ++n ) + { + if( ( nullptr != font_strikethrough[n] ) && + 0 == strncmp( value, font_strikethrough[n], strlen( font_strikethrough[n] ) ) ) + { + rAny <<= n; + return true; + } + } + + return false; +} + +/*****************************************************************************/ + +static gchar* +Underline2String(const uno::Any& rAny) +{ + const gchar * value = nullptr; + + switch( rAny.get<sal_Int16>() ) + { + case awt::FontUnderline::NONE: + value = "none"; + break; + + case awt::FontUnderline::SINGLE: + value = "single"; + break; + + case awt::FontUnderline::DOUBLE: + value = "double"; + break; + + default: + break; + } + + if( value ) + return g_strdup( value ); + + return nullptr; +} + +static bool +String2Underline( uno::Any& rAny, const gchar * value ) +{ + short nUnderline; + + if( strncmp( value, STRNCMP_PARAM( "none" ) ) == 0 ) + nUnderline = awt::FontUnderline::NONE; + else if( strncmp( value, STRNCMP_PARAM( "single" ) ) == 0 ) + nUnderline = awt::FontUnderline::SINGLE; + else if( strncmp( value, STRNCMP_PARAM( "double" ) ) == 0 ) + nUnderline = awt::FontUnderline::DOUBLE; + else + return false; + + rAny <<= nUnderline; + return true; +} + +/*****************************************************************************/ + +static gchar* +GetString(const uno::Any& rAny) +{ + OString aFontName = OUStringToOString( rAny.get< OUString > (), RTL_TEXTENCODING_UTF8 ); + + if( !aFontName.isEmpty() ) + return g_strdup( aFontName.getStr() ); + + return nullptr; +} + +static bool +SetString( uno::Any& rAny, const gchar * value ) +{ + OString aFontName( value ); + + if( !aFontName.isEmpty() ) + { + rAny <<= OStringToOUString( aFontName, RTL_TEXTENCODING_UTF8 ); + return true; + } + + return false; +} + +/*****************************************************************************/ + +// @see http://developer.gnome.org/doc/API/2.0/atk/AtkText.html#AtkTextAttribute + +// CMM = 100th of mm +static gchar* +CMM2UnitString(const uno::Any& rAny) +{ + double fValue = rAny.get<sal_Int32>(); + fValue = fValue * 0.01; + + return g_strdup_printf( "%gmm", fValue ); +} + +static bool +UnitString2CMM( uno::Any& rAny, const gchar * value ) +{ + float fValue = 0.0; // pb: don't use double here because of warning on linux + + if( 1 != sscanf( value, "%gmm", &fValue ) ) + return false; + + fValue = fValue * 100; + + rAny <<= static_cast<sal_Int32>(fValue); + return true; +} + +/*****************************************************************************/ + +static const gchar * bool_values[] = { "true", "false" }; + +static gchar * +Bool2String( const uno::Any& rAny ) +{ + int n = 1; + + if( rAny.get<bool>() ) + n = 0; + + return g_strdup( bool_values[n] ); +} + +static bool +String2Bool( uno::Any& rAny, const gchar * value ) +{ + bool bValue; + + if( strncmp( value, STRNCMP_PARAM( "true" ) ) == 0 ) + bValue = true; + else if( strncmp( value, STRNCMP_PARAM( "false" ) ) == 0 ) + bValue = false; + else + return false; + + rAny <<= bValue; + return true; +} + +/*****************************************************************************/ + +static gchar* +Scale2String( const uno::Any& rAny ) +{ + return g_strdup_printf( "%g", static_cast<double>(rAny.get< sal_Int16 > ()) / 100 ); +} + +static bool +String2Scale( uno::Any& rAny, const gchar * value ) +{ + double dval; + + if( 1 != sscanf( value, "%lg", &dval ) ) + return false; + + rAny <<= static_cast<sal_Int16>(dval * 100); + return true; +} + +/*****************************************************************************/ + +static gchar * +CaseMap2String( const uno::Any& rAny ) +{ + const gchar * value; + + switch( rAny.get<short>() ) + { + case style::CaseMap::SMALLCAPS: + value = "small_caps"; + break; + + default: + value = "normal"; + break; + } + + return g_strdup(value); +} + +static bool +String2CaseMap( uno::Any& rAny, const gchar * value ) +{ + short nCaseMap; + + if( strncmp( value, STRNCMP_PARAM( "normal" ) ) == 0 ) + nCaseMap = style::CaseMap::NONE; + else if( strncmp( value, STRNCMP_PARAM( "small_caps" ) ) == 0 ) + nCaseMap = style::CaseMap::SMALLCAPS; + else + return false; + + rAny <<= nCaseMap; + return true; +} + +/*****************************************************************************/ + +const gchar * const font_stretch[] = { + "ultra_condensed", + "extra_condensed", + "condensed", + "semi_condensed", + "normal", + "semi_expanded", + "expanded", + "extra_expanded", + "ultra_expanded" +}; + +static gchar* +Kerning2Stretch(const uno::Any& rAny) +{ + sal_Int16 n = rAny.get<sal_Int16>(); + int i = 4; + + // No good idea for a mapping - just return the basic info + if( n < 0 ) + i=2; + else if( n > 0 ) + i=6; + + return g_strdup(font_stretch[i]); +} + +/*****************************************************************************/ + +static gchar* +Locale2String(const uno::Any& rAny) +{ + /* FIXME-BCP47: support language tags? And why is country lowercase? */ + lang::Locale aLocale = rAny.get<lang::Locale> (); + LanguageTag aLanguageTag( aLocale); + return g_strdup_printf( "%s-%s", + OUStringToOString( aLanguageTag.getLanguage(), RTL_TEXTENCODING_ASCII_US).getStr(), + OUStringToOString( aLanguageTag.getCountry(), RTL_TEXTENCODING_ASCII_US).toAsciiLowerCase().getStr() ); +} + +static bool +String2Locale( uno::Any& rAny, const gchar * value ) +{ + /* FIXME-BCP47: support language tags? */ + bool ret = false; + + gchar ** str_array = g_strsplit_set( value, "-.@", -1 ); + if( str_array[0] != nullptr ) + { + ret = true; + + lang::Locale aLocale; + + aLocale.Language = OUString::createFromAscii(str_array[0]); + if( str_array[1] != nullptr ) + { + gchar * country = g_ascii_strup(str_array[1], -1); + aLocale.Country = OUString::createFromAscii(country); + g_free(country); + } + + rAny <<= aLocale; + } + + g_strfreev(str_array); + return ret; +} + +/*****************************************************************************/ + +// @see http://www.w3.org/TR/2002/WD-css3-fonts-20020802/#font-effect-prop +static const gchar * relief[] = { "none", "emboss", "engrave" }; +static const gchar * const outline = "outline"; + +static gchar * +get_font_effect(const uno::Sequence< beans::PropertyValue >& rAttributeList, + sal_Int32 nContourIndex, sal_Int32 nReliefIndex) +{ + if( nContourIndex != -1 ) + { + if( rAttributeList[nContourIndex].Value.get<bool>() ) + return g_strdup(outline); + } + + if( nReliefIndex != -1 ) + { + sal_Int16 n = rAttributeList[nReliefIndex].Value.get<sal_Int16>(); + if( n < 3) + return g_strdup(relief[n]); + } + + return nullptr; +} + +/*****************************************************************************/ + +// @see http://www.w3.org/TR/REC-CSS2/text.html#lining-striking-props + +enum +{ + DECORATION_NONE = 0, + DECORATION_BLINK, + DECORATION_UNDERLINE, + DECORATION_LINE_THROUGH +}; + +static const gchar * decorations[] = { "none", "blink", "underline", "line-through" }; + +static gchar * +get_text_decoration(const uno::Sequence< beans::PropertyValue >& rAttributeList, + sal_Int32 nBlinkIndex, sal_Int32 nUnderlineIndex, + sal_Int16 nStrikeoutIndex) +{ + gchar * value_list[4] = { nullptr, nullptr, nullptr, nullptr }; + gint count = 0; + + // no property value found + if( ( nBlinkIndex == -1 ) && (nUnderlineIndex == -1 ) && (nStrikeoutIndex == -1)) + return nullptr; + + if( nBlinkIndex != -1 ) + { + if( rAttributeList[nBlinkIndex].Value.get<bool>() ) + value_list[count++] = const_cast <gchar *> (decorations[DECORATION_BLINK]); + } + if( nUnderlineIndex != -1 ) + { + sal_Int16 n = rAttributeList[nUnderlineIndex].Value.get<sal_Int16> (); + if( n != awt::FontUnderline::NONE ) + value_list[count++] = const_cast <gchar *> (decorations[DECORATION_UNDERLINE]); + } + if( nStrikeoutIndex != -1 ) + { + sal_Int16 n = rAttributeList[nStrikeoutIndex].Value.get<sal_Int16> (); + if( n != awt::FontStrikeout::NONE && n != awt::FontStrikeout::DONTKNOW ) + value_list[count++] = const_cast <gchar *> (decorations[DECORATION_LINE_THROUGH]); + } + + if( count == 0 ) + value_list[count++] = const_cast <gchar *> (decorations[DECORATION_NONE]); + + return g_strjoinv(" ", value_list); +} + +/*****************************************************************************/ + +// @see http://www.w3.org/TR/REC-CSS2/text.html#propdef-text-shadow + +static const gchar * shadow_values[] = { "none", "black" }; + +static gchar * +Bool2Shadow( const uno::Any& rAny ) +{ + int n = 0; + + if( rAny.get<bool>() ) + n = 1; + + return g_strdup( shadow_values[n] ); +} + +/*****************************************************************************/ + +static gchar * +Short2Degree( const uno::Any& rAny ) +{ + float f = rAny.get<sal_Int16>() / 10.0; + return g_strdup_printf( "%g", f ); +} + +/*****************************************************************************/ + +const gchar * const directions[] = { "ltr", "rtl", "rtl", "ltr", "none" }; + +static gchar * +WritingMode2Direction( const uno::Any& rAny ) +{ + sal_Int16 n = rAny.get<sal_Int16>(); + + if( 0 <= n && n <= text::WritingMode2::PAGE ) + return g_strdup(directions[n]); + + return nullptr; +} + +// @see http://www.w3.org/TR/2001/WD-css3-text-20010517/#PrimaryTextAdvanceDirection + +const gchar * const writing_modes[] = { "lr-tb", "rl-tb", "tb-rl", "tb-lr", "none" }; +static gchar * +WritingMode2String( const uno::Any& rAny ) +{ + sal_Int16 n = rAny.get<sal_Int16>(); + + if( 0 <= n && n <= text::WritingMode2::PAGE ) + return g_strdup(writing_modes[n]); + + return nullptr; +} + +/*****************************************************************************/ + +const char * const baseline_values[] = { "baseline", "sub", "super" }; + +// @see http://www.w3.org/TR/REC-CSS2/visudet.html#propdef-vertical-align +static gchar * +Escapement2VerticalAlign( const uno::Any& rAny ) +{ + sal_Int16 n = rAny.get<sal_Int16>(); + gchar * ret = nullptr; + + // Values are in %, 101% means "automatic" + if( n == 0 ) + ret = g_strdup(baseline_values[0]); + else if( n == 101 ) + ret = g_strdup(baseline_values[2]); + else if( n == -101 ) + ret = g_strdup(baseline_values[1]); + else + ret = g_strdup_printf( "%d%%", n ); + + return ret; +} + +/*****************************************************************************/ + +// @see http://www.w3.org/TR/REC-CSS2/visudet.html#propdef-line-height +static gchar * +LineSpacing2LineHeight( const uno::Any& rAny ) +{ + style::LineSpacing ls; + gchar * ret = nullptr; + + if( rAny >>= ls ) + { + if( ls.Mode == style::LineSpacingMode::PROP ) + ret = g_strdup_printf( "%d%%", ls.Height ); + else if( ls.Mode == style::LineSpacingMode::FIX ) + ret = g_strdup_printf( "%.3gpt", toPoint(ls.Height) ); + } + + return ret; +} + +/*****************************************************************************/ + +// @see http://www.w3.org/People/howcome/t/970224HTMLERB-CSS/WD-tabs-970117.html +static gchar * +TabStopList2String( const uno::Any& rAny, bool default_tabs ) +{ + uno::Sequence< style::TabStop > theTabStops; + gchar * ret = nullptr; + + if( rAny >>= theTabStops) + { + sal_Unicode lastFillChar = ' '; + + for( const auto& rTabStop : std::as_const(theTabStops) ) + { + bool is_default_tab = (style::TabAlign_DEFAULT == rTabStop.Alignment); + + if( is_default_tab != default_tabs ) + continue; + + double fValue = rTabStop.Position; + fValue = fValue * 0.01; + + const gchar * tab_align = ""; + switch( rTabStop.Alignment ) + { + case style::TabAlign_LEFT : + tab_align = "left "; + break; + case style::TabAlign_CENTER : + tab_align = "center "; + break; + case style::TabAlign_RIGHT : + tab_align = "right "; + break; + case style::TabAlign_DECIMAL : + tab_align = "decimal "; + break; + default: + break; + } + + const gchar * lead_char = ""; + + if( rTabStop.FillChar != lastFillChar ) + { + lastFillChar = rTabStop.FillChar; + switch (lastFillChar) + { + case ' ': + lead_char = "blank "; + break; + + case '.': + lead_char = "dotted "; + break; + + case '-': + lead_char = "dashed "; + break; + + case '_': + lead_char = "lined "; + break; + + default: + lead_char = "custom "; + break; + } + } + + gchar * tab_str = g_strdup_printf( "%s%s%gmm", lead_char, tab_align, fValue ); + + if( ret ) + { + gchar * old_tab_str = ret; + ret = g_strconcat(old_tab_str, " ", tab_str, nullptr); + g_free( old_tab_str ); + } + else + ret = tab_str; + } + } + + return ret; +} + +static gchar * +TabStops2String( const uno::Any& rAny ) +{ + return TabStopList2String(rAny, false); +} + +static gchar * +DefaultTabStops2String( const uno::Any& rAny ) +{ + return TabStopList2String(rAny, true); +} + +/*****************************************************************************/ + +extern "C" { + +static int +attr_compare(const void *p1,const void *p2) +{ + const rtl_uString * pustr = static_cast<const rtl_uString *>(p1); + const char * pc = *static_cast<const char * const *>(p2); + + return rtl_ustr_ascii_compare_WithLength(pustr->buffer, pustr->length, pc); +} + +} + +static void +find_exported_attributes( sal_Int32 *pArray, + const css::uno::Sequence< css::beans::PropertyValue >& rAttributeList ) +{ + for( sal_Int32 i = 0; i < rAttributeList.getLength(); i++ ) + { + const char ** pAttr = static_cast<const char **>(bsearch(rAttributeList[i].Name.pData, + ExportedTextAttributes, TEXT_ATTRIBUTE_LAST, sizeof(const char *), + attr_compare)); + + if( pAttr ) + { + sal_Int32 nIndex = pAttr - ExportedTextAttributes; + pArray[nIndex] = i; + } + } +} + +/*****************************************************************************/ + +static AtkAttributeSet* +attribute_set_prepend( AtkAttributeSet* attribute_set, + AtkTextAttribute attribute, + gchar * value ) +{ + if( value ) + { + AtkAttribute *at = static_cast<AtkAttribute *>(g_malloc( sizeof (AtkAttribute) )); + at->name = g_strdup( atk_text_attribute_get_name( attribute ) ); + at->value = value; + + return g_slist_prepend(attribute_set, at); + } + + return attribute_set; +} + +/*****************************************************************************/ + +AtkAttributeSet* +attribute_set_new_from_property_values( + const uno::Sequence< beans::PropertyValue >& rAttributeList, + bool run_attributes_only, + AtkText *text) +{ + AtkAttributeSet* attribute_set = nullptr; + + sal_Int32 aIndexList[TEXT_ATTRIBUTE_LAST] = { -1 }; + + // Initialize index array with -1 + for(sal_Int32 & rn : aIndexList) + rn = -1; + + find_exported_attributes(aIndexList, rAttributeList); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_BG_COLOR, + get_color_value(rAttributeList, aIndexList, TEXT_ATTRIBUTE_BACKGROUND_COLOR, run_attributes_only ? nullptr : text ) ); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_FG_COLOR, + get_color_value(rAttributeList, aIndexList, TEXT_ATTRIBUTE_FOREGROUND_COLOR, run_attributes_only ? nullptr : text) ); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_INVISIBLE, + get_bool_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_HIDDEN])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_UNDERLINE, + get_underline_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_UNDERLINE])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_STRIKETHROUGH, + get_strikethrough_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_STRIKETHROUGH])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_SIZE, + get_height_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_HEIGHT])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_WEIGHT, + get_weight_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_WEIGHT])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_FAMILY_NAME, + get_string_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_FONT_NAME])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_VARIANT, + get_variant_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_CASEMAP])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_STYLE, + get_style_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_POSTURE])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_SCALE, + get_scale_width(rAttributeList, aIndexList[TEXT_ATTRIBUTE_SCALE])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_LANGUAGE, + get_language_string(rAttributeList, aIndexList[TEXT_ATTRIBUTE_LOCALE])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_DIRECTION, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_WRITING_MODE], WritingMode2Direction)); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_STRETCH, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_KERNING], Kerning2Stretch)); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_font_effect ) + atk_text_attribute_font_effect = atk_text_attribute_register("font-effect"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_font_effect, + get_font_effect(rAttributeList, aIndexList[TEXT_ATTRIBUTE_CONTOURED], aIndexList[TEXT_ATTRIBUTE_RELIEF])); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_decoration ) + atk_text_attribute_decoration = atk_text_attribute_register("text-decoration"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_decoration, + get_text_decoration(rAttributeList, aIndexList[TEXT_ATTRIBUTE_BLINKING], + aIndexList[TEXT_ATTRIBUTE_UNDERLINE], aIndexList[TEXT_ATTRIBUTE_STRIKETHROUGH])); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_rotation ) + atk_text_attribute_rotation = atk_text_attribute_register("text-rotation"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_rotation, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_ROTATION], Short2Degree)); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_shadow ) + atk_text_attribute_shadow = atk_text_attribute_register("text-shadow"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_shadow, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_SHADOWED], Bool2Shadow)); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_writing_mode ) + atk_text_attribute_writing_mode = atk_text_attribute_register("writing-mode"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_writing_mode, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_WRITING_MODE], WritingMode2String)); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_vertical_align ) + atk_text_attribute_vertical_align = atk_text_attribute_register("vertical-align"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_vertical_align, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_CHAR_ESCAPEMENT], Escapement2VerticalAlign)); + + if( run_attributes_only ) + return attribute_set; + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_LEFT_MARGIN, + get_cmm_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_LEFT_MARGIN])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_RIGHT_MARGIN, + get_cmm_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_RIGHT_MARGIN])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_INDENT, + get_cmm_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_FIRST_LINE_INDENT])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_PIXELS_ABOVE_LINES, + get_cmm_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_TOP_MARGIN])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_PIXELS_BELOW_LINES, + get_cmm_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_BOTTOM_MARGIN])); + + attribute_set = attribute_set_prepend(attribute_set, ATK_TEXT_ATTR_JUSTIFICATION, + get_justification_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_JUSTIFICATION])); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_paragraph_style ) + atk_text_attribute_paragraph_style = atk_text_attribute_register("paragraph-style"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_paragraph_style, + get_string_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_STYLE_NAME])); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_line_height ) + atk_text_attribute_line_height = atk_text_attribute_register("line-height"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_line_height, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_LINE_SPACING], LineSpacing2LineHeight)); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_tab_interval ) + atk_text_attribute_tab_interval = atk_text_attribute_register("tab-interval"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_tab_interval, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_TAB_STOPS], DefaultTabStops2String)); + + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_tab_stops ) + atk_text_attribute_tab_stops = atk_text_attribute_register("tab-stops"); + + attribute_set = attribute_set_prepend(attribute_set, atk_text_attribute_tab_stops, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_TAB_STOPS], TabStops2String)); + + // #i92233# + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_mm_to_pixel_ratio ) + atk_text_attribute_mm_to_pixel_ratio = atk_text_attribute_register("mm-to-pixel-ratio"); + + attribute_set = attribute_set_prepend( attribute_set, atk_text_attribute_mm_to_pixel_ratio, + get_value(rAttributeList, aIndexList[TEXT_ATTRIBUTE_MM_TO_PIXEL_RATIO], Float2String)); + + return attribute_set; +} + +AtkAttributeSet* +attribute_set_new_from_extended_attributes( + const css::uno::Reference< css::accessibility::XAccessibleExtendedAttributes >& rExtendedAttributes ) +{ + AtkAttributeSet *pSet = nullptr; + + // extended attributes is a string of colon-separated pairs of property and value, + // with pairs separated by semicolons. Example: "heading-level:2;weight:bold;" + uno::Any anyVal = rExtendedAttributes->getExtendedAttributes(); + OUString sExtendedAttrs; + anyVal >>= sExtendedAttrs; + sal_Int32 nIndex = 0; + do + { + OUString sProperty = sExtendedAttrs.getToken( 0, ';', nIndex ); + + sal_Int32 nColonPos = 0; + OString sPropertyName = OUStringToOString( sProperty.getToken( 0, ':', nColonPos ), + RTL_TEXTENCODING_UTF8 ); + OString sPropertyValue = OUStringToOString( sProperty.getToken( 0, ':', nColonPos ), + RTL_TEXTENCODING_UTF8 ); + + pSet = attribute_set_prepend( pSet, + atk_text_attribute_register( sPropertyName.getStr() ), + g_strdup_printf( "%s", sPropertyValue.getStr() ) ); + } + while ( nIndex >= 0 && nIndex < sExtendedAttrs.getLength() ); + + return pSet; +} + +AtkAttributeSet* attribute_set_prepend_misspelled( AtkAttributeSet* attribute_set ) +{ + if( ATK_TEXT_ATTR_INVALID == atk_text_attribute_misspelled ) + atk_text_attribute_misspelled = atk_text_attribute_register( "text-spelling" ); + + attribute_set = attribute_set_prepend( attribute_set, atk_text_attribute_misspelled, + g_strdup_printf( "misspelled" ) ); + + return attribute_set; +} + +// #i92232# +AtkAttributeSet* attribute_set_prepend_tracked_change_insertion( AtkAttributeSet* attribute_set ) +{ + if ( ATK_TEXT_ATTR_INVALID == atk_text_attribute_tracked_change ) + { + atk_text_attribute_tracked_change = atk_text_attribute_register( "text-tracked-change" ); + } + + attribute_set = attribute_set_prepend( attribute_set, + atk_text_attribute_tracked_change, + g_strdup_printf( "insertion" ) ); + + return attribute_set; +} + +AtkAttributeSet* attribute_set_prepend_tracked_change_deletion( AtkAttributeSet* attribute_set ) +{ + if ( ATK_TEXT_ATTR_INVALID == atk_text_attribute_tracked_change ) + { + atk_text_attribute_tracked_change = atk_text_attribute_register( "text-tracked-change" ); + } + + attribute_set = attribute_set_prepend( attribute_set, + atk_text_attribute_tracked_change, + g_strdup_printf( "deletion" ) ); + + return attribute_set; +} + +AtkAttributeSet* attribute_set_prepend_tracked_change_formatchange( AtkAttributeSet* attribute_set ) +{ + if ( ATK_TEXT_ATTR_INVALID == atk_text_attribute_tracked_change ) + { + atk_text_attribute_tracked_change = atk_text_attribute_register( "text-tracked-change" ); + } + + attribute_set = attribute_set_prepend( attribute_set, + atk_text_attribute_tracked_change, + g_strdup_printf( "attribute-change" ) ); + + return attribute_set; +} + +/*****************************************************************************/ + +struct AtkTextAttrMapping +{ + const char * name; + TextPropertyValueFunc const toPropertyValue; +}; + +const AtkTextAttrMapping g_TextAttrMap[] = +{ + { "", InvalidValue }, // ATK_TEXT_ATTR_INVALID = 0 + { "ParaLeftMargin", UnitString2CMM }, // ATK_TEXT_ATTR_LEFT_MARGIN + { "ParaRightMargin", UnitString2CMM }, // ATK_TEXT_ATTR_RIGHT_MARGIN + { "ParaFirstLineIndent", UnitString2CMM }, // ATK_TEXT_ATTR_INDENT + { "CharHidden", String2Bool }, // ATK_TEXT_ATTR_INVISIBLE + { "", InvalidValue }, // ATK_TEXT_ATTR_EDITABLE + { "ParaTopMargin", UnitString2CMM }, // ATK_TEXT_ATTR_PIXELS_ABOVE_LINES + { "ParaBottomMargin", UnitString2CMM }, // ATK_TEXT_ATTR_PIXELS_BELOW_LINES + { "", InvalidValue }, // ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP + { "", InvalidValue }, // ATK_TEXT_ATTR_BG_FULL_HEIGHT + { "", InvalidValue }, // ATK_TEXT_ATTR_RISE + { "CharUnderline", String2Underline }, // ATK_TEXT_ATTR_UNDERLINE + { "CharStrikeout", String2Strikeout }, // ATK_TEXT_ATTR_STRIKETHROUGH + { "CharHeight", String2Float }, // ATK_TEXT_ATTR_SIZE + { "CharScaleWidth", String2Scale }, // ATK_TEXT_ATTR_SCALE + { "CharWeight", String2Weight }, // ATK_TEXT_ATTR_WEIGHT + { "CharLocale", String2Locale }, // ATK_TEXT_ATTR_LANGUAGE + { "CharFontName", SetString }, // ATK_TEXT_ATTR_FAMILY_NAME + { "CharBackColor", String2Color }, // ATK_TEXT_ATTR_BG_COLOR + { "CharColor", String2Color }, // ATK_TEXT_ATTR_FG_COLOR + { "", InvalidValue }, // ATK_TEXT_ATTR_BG_STIPPLE + { "", InvalidValue }, // ATK_TEXT_ATTR_FG_STIPPLE + { "", InvalidValue }, // ATK_TEXT_ATTR_WRAP_MODE + { "", InvalidValue }, // ATK_TEXT_ATTR_DIRECTION + { "ParaAdjust", Justification2Adjust }, // ATK_TEXT_ATTR_JUSTIFICATION + { "", InvalidValue }, // ATK_TEXT_ATTR_STRETCH + { "CharCaseMap", String2CaseMap }, // ATK_TEXT_ATTR_VARIANT + { "CharPosture", Style2FontSlant } // ATK_TEXT_ATTR_STYLE +}; + +/*****************************************************************************/ + +bool +attribute_set_map_to_property_values( + AtkAttributeSet* attribute_set, + uno::Sequence< beans::PropertyValue >& rValueList ) +{ + // Ensure enough space .. + uno::Sequence< beans::PropertyValue > aAttributeList (SAL_N_ELEMENTS(g_TextAttrMap)); + + sal_Int32 nIndex = 0; + for( GSList * item = attribute_set; item != nullptr; item = g_slist_next( item ) ) + { + AtkAttribute* attribute = reinterpret_cast<AtkAttribute *>(item); + + AtkTextAttribute text_attr = atk_text_attribute_for_name( attribute->name ); + if( text_attr < SAL_N_ELEMENTS(g_TextAttrMap) ) + { + if( g_TextAttrMap[text_attr].name[0] != '\0' ) + { + if( ! g_TextAttrMap[text_attr].toPropertyValue( aAttributeList[nIndex].Value, attribute->value) ) + return false; + + aAttributeList[nIndex].Name = OUString::createFromAscii( g_TextAttrMap[text_attr].name ); + aAttributeList[nIndex].State = beans::PropertyState_DIRECT_VALUE; + ++nIndex; + } + } + else + { + // Unsupported text attribute + return false; + } + } + + aAttributeList.realloc( nIndex ); + rValueList = aAttributeList; + return true; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkutil.cxx b/vcl/unx/gtk3/a11y/gtk3atkutil.cxx index 8c1eeaf9882b..cac3ac6e4805 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkutil.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkutil.cxx @@ -5,8 +5,785 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkutil.cxx" +#ifdef AIX +#define _LINUX_SOURCE_COMPAT +#include <sys/timer.h> +#undef _LINUX_SOURCE_COMPAT +#endif + +#include <com/sun/star/accessibility/XAccessibleContext.hpp> +#include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp> +#include <com/sun/star/accessibility/XAccessibleSelection.hpp> +#include <com/sun/star/accessibility/AccessibleEventId.hpp> +#include <com/sun/star/accessibility/AccessibleStateType.hpp> +#include <com/sun/star/accessibility/XAccessibleText.hpp> +#include <com/sun/star/lang/IndexOutOfBoundsException.hpp> +#include <cppuhelper/implbase.hxx> +#include <cppuhelper/weakref.hxx> +#include <rtl/ref.hxx> +#include <sal/log.hxx> + +#include <vcl/svapp.hxx> +#include <vcl/window.hxx> +#include <vcl/menu.hxx> +#include <vcl/toolbox.hxx> + +#include <unx/gtk/gtkdata.hxx> +#include "atkwrapper.hxx" +#include "atkutil.hxx" + +#include <gtk/gtk.h> +#include <config_version.h> + +#include <cassert> +#include <set> + +using namespace ::com::sun::star; + +namespace +{ + struct theNextFocusObject : + public rtl::Static< uno::WeakReference< accessibility::XAccessible >, theNextFocusObject> + { + }; +} + +static guint focus_notify_handler = 0; + +/*****************************************************************************/ + +extern "C" { + +static gboolean +atk_wrapper_focus_idle_handler (gpointer data) +{ + SolarMutexGuard aGuard; + + focus_notify_handler = 0; + + uno::Reference< accessibility::XAccessible > xAccessible = theNextFocusObject::get(); + if( xAccessible.get() == static_cast < accessibility::XAccessible * > (data) ) + { + AtkObject *atk_obj = xAccessible.is() ? atk_object_wrapper_ref( xAccessible ) : nullptr; + // Gail does not notify focus changes to NULL, so do we .. + if( atk_obj ) + { + SAL_WNODEPRECATED_DECLARATIONS_PUSH + atk_focus_tracker_notify(atk_obj); + SAL_WNODEPRECATED_DECLARATIONS_POP + // #i93269# + // emit text_caret_moved event for <XAccessibleText> object, + // if cursor is inside the <XAccessibleText> object. + // also emit state-changed:focused event under the same condition. + { + AtkObjectWrapper* wrapper_obj = ATK_OBJECT_WRAPPER (atk_obj); + if( wrapper_obj && !wrapper_obj->mpText.is() ) + { + wrapper_obj->mpText.set(wrapper_obj->mpContext, css::uno::UNO_QUERY); + if ( wrapper_obj->mpText.is() ) + { + gint caretPos = -1; + + try { + caretPos = wrapper_obj->mpText->getCaretPosition(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getCaretPosition()" ); + } + + if ( caretPos != -1 ) + { + atk_object_notify_state_change( atk_obj, ATK_STATE_FOCUSED, TRUE ); + g_signal_emit_by_name( atk_obj, "text_caret_moved", caretPos ); + } + } + } + } + g_object_unref(atk_obj); + } + } + + return false; +} + +} // extern "C" + +/*****************************************************************************/ + +static void +atk_wrapper_focus_tracker_notify_when_idle( const uno::Reference< accessibility::XAccessible > &xAccessible ) +{ + if( focus_notify_handler ) + g_source_remove(focus_notify_handler); + + theNextFocusObject::get() = xAccessible; + + focus_notify_handler = g_idle_add (atk_wrapper_focus_idle_handler, xAccessible.get()); +} + +/*****************************************************************************/ + +class DocumentFocusListener : + public ::cppu::WeakImplHelper< accessibility::XAccessibleEventListener > +{ + + std::set< uno::Reference< uno::XInterface > > m_aRefList; + +public: + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + void attachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible + ); + + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + void attachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible, + const uno::Reference< accessibility::XAccessibleContext >& xContext + ); + + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + void attachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible, + const uno::Reference< accessibility::XAccessibleContext >& xContext, + const uno::Reference< accessibility::XAccessibleStateSet >& xStateSet + ); + + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + void detachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible + ); + + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + void detachRecursive( + const uno::Reference< accessibility::XAccessibleContext >& xContext + ); + + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + void detachRecursive( + const uno::Reference< accessibility::XAccessibleContext >& xContext, + const uno::Reference< accessibility::XAccessibleStateSet >& xStateSet + ); + + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + static uno::Reference< accessibility::XAccessible > getAccessible(const lang::EventObject& aEvent ); + + // XEventListener + virtual void SAL_CALL disposing( const lang::EventObject& Source ) override; + + // XAccessibleEventListener + virtual void SAL_CALL notifyEvent( const accessibility::AccessibleEventObject& aEvent ) override; +}; + +/*****************************************************************************/ + +void DocumentFocusListener::disposing( const lang::EventObject& aEvent ) +{ + + // Unref the object here, but do not remove as listener since the object + // might no longer be in a state that safely allows this. + if( aEvent.Source.is() ) + m_aRefList.erase(aEvent.Source); + +} + +/*****************************************************************************/ + +void DocumentFocusListener::notifyEvent( const accessibility::AccessibleEventObject& aEvent ) +{ + try { + switch( aEvent.EventId ) + { + case accessibility::AccessibleEventId::STATE_CHANGED: + { + sal_Int16 nState = accessibility::AccessibleStateType::INVALID; + aEvent.NewValue >>= nState; + + if( accessibility::AccessibleStateType::FOCUSED == nState ) + atk_wrapper_focus_tracker_notify_when_idle( getAccessible(aEvent) ); + + break; + } + + case accessibility::AccessibleEventId::CHILD: + { + uno::Reference< accessibility::XAccessible > xChild; + if( (aEvent.OldValue >>= xChild) && xChild.is() ) + detachRecursive(xChild); + + if( (aEvent.NewValue >>= xChild) && xChild.is() ) + attachRecursive(xChild); + + break; + } + + case accessibility::AccessibleEventId::INVALIDATE_ALL_CHILDREN: + SAL_INFO("vcl.a11y", "Invalidate all children called"); + break; + + default: + break; + } + } + catch( const lang::IndexOutOfBoundsException& e ) + { + g_warning("Focused object has invalid index in parent"); + } +} + +/*****************************************************************************/ + +uno::Reference< accessibility::XAccessible > DocumentFocusListener::getAccessible(const lang::EventObject& aEvent ) +{ + uno::Reference< accessibility::XAccessible > xAccessible(aEvent.Source, uno::UNO_QUERY); + + if( xAccessible.is() ) + return xAccessible; + + uno::Reference< accessibility::XAccessibleContext > xContext(aEvent.Source, uno::UNO_QUERY); + + if( xContext.is() ) + { + uno::Reference< accessibility::XAccessible > xParent( xContext->getAccessibleParent() ); + if( xParent.is() ) + { + uno::Reference< accessibility::XAccessibleContext > xParentContext( xParent->getAccessibleContext() ); + if( xParentContext.is() ) + { + return xParentContext->getAccessibleChild( xContext->getAccessibleIndexInParent() ); + } + } + } + + return uno::Reference< accessibility::XAccessible >(); +} + +/*****************************************************************************/ + +void DocumentFocusListener::attachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible +) +{ + uno::Reference< accessibility::XAccessibleContext > xContext = + xAccessible->getAccessibleContext(); + + if( xContext.is() ) + attachRecursive(xAccessible, xContext); +} + +/*****************************************************************************/ + +void DocumentFocusListener::attachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible, + const uno::Reference< accessibility::XAccessibleContext >& xContext +) +{ + uno::Reference< accessibility::XAccessibleStateSet > xStateSet = + xContext->getAccessibleStateSet(); + + if( xStateSet.is() ) + attachRecursive(xAccessible, xContext, xStateSet); +} + +/*****************************************************************************/ + +void DocumentFocusListener::attachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible, + const uno::Reference< accessibility::XAccessibleContext >& xContext, + const uno::Reference< accessibility::XAccessibleStateSet >& xStateSet +) +{ + if( xStateSet->contains(accessibility::AccessibleStateType::FOCUSED ) ) + atk_wrapper_focus_tracker_notify_when_idle( xAccessible ); + + uno::Reference< accessibility::XAccessibleEventBroadcaster > xBroadcaster(xContext, uno::UNO_QUERY); + + if (!xBroadcaster.is()) + return; + + // If not already done, add the broadcaster to the list and attach as listener. + const uno::Reference< uno::XInterface >& xInterface = xBroadcaster; + if( m_aRefList.insert(xInterface).second ) + { + xBroadcaster->addAccessibleEventListener(static_cast< accessibility::XAccessibleEventListener *>(this)); + + if( ! xStateSet->contains(accessibility::AccessibleStateType::MANAGES_DESCENDANTS ) ) + { + sal_Int32 n, nmax = xContext->getAccessibleChildCount(); + for( n = 0; n < nmax; n++ ) + { + uno::Reference< accessibility::XAccessible > xChild( xContext->getAccessibleChild( n ) ); + + if( xChild.is() ) + attachRecursive(xChild); + } + } + } +} + +/*****************************************************************************/ + +void DocumentFocusListener::detachRecursive( + const uno::Reference< accessibility::XAccessible >& xAccessible +) +{ + uno::Reference< accessibility::XAccessibleContext > xContext = + xAccessible->getAccessibleContext(); + + if( xContext.is() ) + detachRecursive(xContext); +} + +/*****************************************************************************/ + +void DocumentFocusListener::detachRecursive( + const uno::Reference< accessibility::XAccessibleContext >& xContext +) +{ + uno::Reference< accessibility::XAccessibleStateSet > xStateSet = + xContext->getAccessibleStateSet(); + + if( xStateSet.is() ) + detachRecursive(xContext, xStateSet); +} + +/*****************************************************************************/ + +void DocumentFocusListener::detachRecursive( + const uno::Reference< accessibility::XAccessibleContext >& xContext, + const uno::Reference< accessibility::XAccessibleStateSet >& xStateSet +) +{ + uno::Reference< accessibility::XAccessibleEventBroadcaster > xBroadcaster(xContext, uno::UNO_QUERY); + + if( xBroadcaster.is() && 0 < m_aRefList.erase(xBroadcaster) ) + { + xBroadcaster->removeAccessibleEventListener(static_cast< accessibility::XAccessibleEventListener *>(this)); + + if( ! xStateSet->contains(accessibility::AccessibleStateType::MANAGES_DESCENDANTS ) ) + { + sal_Int32 n, nmax = xContext->getAccessibleChildCount(); + for( n = 0; n < nmax; n++ ) + { + uno::Reference< accessibility::XAccessible > xChild( xContext->getAccessibleChild( n ) ); + + if( xChild.is() ) + detachRecursive(xChild); + } + } + } +} + +/*****************************************************************************/ + +/* + * page tabs in gtk are widgets, so we need to simulate focus events for those + */ + +static void handle_tabpage_activated(vcl::Window *pWindow) +{ + uno::Reference< accessibility::XAccessible > xAccessible = + pWindow->GetAccessible(); + + if( ! xAccessible.is() ) + return; + + uno::Reference< accessibility::XAccessibleSelection > xSelection( + xAccessible->getAccessibleContext(), uno::UNO_QUERY); + + if( xSelection.is() ) + atk_wrapper_focus_tracker_notify_when_idle( xSelection->getSelectedAccessibleChild(0) ); +} + +/*****************************************************************************/ + +/* + * toolbar items in gtk are widgets, so we need to simulate focus events for those + */ + +static void notify_toolbox_item_focus(ToolBox *pToolBox) +{ + uno::Reference< accessibility::XAccessible > xAccessible = + pToolBox->GetAccessible(); + + if( ! xAccessible.is() ) + return; + + uno::Reference< accessibility::XAccessibleContext > xContext = + xAccessible->getAccessibleContext(); + + if( ! xContext.is() ) + return; + + ToolBox::ImplToolItems::size_type nPos = pToolBox->GetItemPos( pToolBox->GetHighlightItemId() ); + if( nPos != ToolBox::ITEM_NOTFOUND ) + atk_wrapper_focus_tracker_notify_when_idle( xContext->getAccessibleChild( nPos ) ); + //TODO: ToolBox::ImplToolItems::size_type -> sal_Int32 +} + +static void handle_toolbox_highlight(vcl::Window *pWindow) +{ + ToolBox *pToolBox = static_cast <ToolBox *> (pWindow); + + // Make sure either the toolbox or its parent toolbox has the focus + if ( ! pToolBox->HasFocus() ) + { + ToolBox* pToolBoxParent = dynamic_cast< ToolBox* >( pToolBox->GetParent() ); + if ( ! pToolBoxParent || ! pToolBoxParent->HasFocus() ) + return; + } + + notify_toolbox_item_focus(pToolBox); +} + +static void handle_toolbox_highlightoff(vcl::Window const *pWindow) +{ + ToolBox* pToolBoxParent = dynamic_cast< ToolBox* >( pWindow->GetParent() ); + + // Notify when leaving sub toolboxes + if( pToolBoxParent && pToolBoxParent->HasFocus() ) + notify_toolbox_item_focus( pToolBoxParent ); +} + +/*****************************************************************************/ + +static void create_wrapper_for_child( + const uno::Reference< accessibility::XAccessibleContext >& xContext, + sal_Int32 index) +{ + if( xContext.is() ) + { + uno::Reference< accessibility::XAccessible > xChild(xContext->getAccessibleChild(index)); + if( xChild.is() ) + { + // create the wrapper object - it will survive the unref unless it is a transient object + g_object_unref( atk_object_wrapper_ref( xChild ) ); + } + } +} + +/*****************************************************************************/ + +static void handle_toolbox_buttonchange(VclWindowEvent const *pEvent) +{ + vcl::Window* pWindow = pEvent->GetWindow(); + sal_Int32 index = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(pEvent->GetData())); + + if( pWindow && pWindow->IsReallyVisible() ) + { + uno::Reference< accessibility::XAccessible > xAccessible(pWindow->GetAccessible()); + if( xAccessible.is() ) + { + create_wrapper_for_child(xAccessible->getAccessibleContext(), index); + } + } +} + +/*****************************************************************************/ + +namespace { + +struct WindowList { + ~WindowList() { assert(list.empty()); }; + // needs to be empty already on DeInitVCL, but at least check it's empty + // on exit + + std::set< VclPtr<vcl::Window> > list; +}; + +WindowList g_aWindowList; + +} + +DocumentFocusListener & GtkSalData::GetDocumentFocusListener() +{ + if (!m_pDocumentFocusListener) + { + m_pDocumentFocusListener = new DocumentFocusListener; + m_xDocumentFocusListener.set(m_pDocumentFocusListener); + } + return *m_pDocumentFocusListener; +} + +static void handle_get_focus(::VclWindowEvent const * pEvent) +{ + GtkSalData *const pSalData(GetGtkSalData()); + assert(pSalData); + + DocumentFocusListener & rDocumentFocusListener(pSalData->GetDocumentFocusListener()); + + vcl::Window *pWindow = pEvent->GetWindow(); + + // The menu bar is handled through VclEventId::MenuHighlightED + if( ! pWindow || !pWindow->IsReallyVisible() || pWindow->GetType() == WindowType::MENUBARWINDOW ) + return; + + // ToolBoxes are handled through VclEventId::ToolboxHighlight + if( pWindow->GetType() == WindowType::TOOLBOX ) + return; + + if( pWindow->GetType() == WindowType::TABCONTROL ) + { + handle_tabpage_activated( pWindow ); + return; + } + + uno::Reference< accessibility::XAccessible > xAccessible = + pWindow->GetAccessible(); + + if( ! xAccessible.is() ) + return; + + uno::Reference< accessibility::XAccessibleContext > xContext = + xAccessible->getAccessibleContext(); + + if( ! xContext.is() ) + return; + + uno::Reference< accessibility::XAccessibleStateSet > xStateSet = + xContext->getAccessibleStateSet(); + + if( ! xStateSet.is() ) + return; + +/* the UNO ToolBox wrapper does not (yet?) support XAccessibleSelection, so we + * need to add listeners to the children instead of re-using the tabpage stuff + */ + if( xStateSet->contains(accessibility::AccessibleStateType::FOCUSED) && + ( pWindow->GetType() != WindowType::TREELISTBOX ) ) + { + atk_wrapper_focus_tracker_notify_when_idle( xAccessible ); + } + else + { + if( g_aWindowList.list.insert(pWindow).second ) + { + try + { + rDocumentFocusListener.attachRecursive(xAccessible, xContext, xStateSet); + } + catch (const uno::Exception&) + { + g_warning( "Exception caught processing focus events" ); + } + } + } +} + +/*****************************************************************************/ + +static void handle_menu_highlighted(::VclMenuEvent const * pEvent) +{ + try + { + Menu* pMenu = pEvent->GetMenu(); + sal_uInt16 nPos = pEvent->GetItemPos(); + + if( pMenu && nPos != 0xFFFF) + { + uno::Reference< accessibility::XAccessible > xAccessible ( pMenu->GetAccessible() ); + + if( xAccessible.is() ) + { + uno::Reference< accessibility::XAccessibleContext > xContext ( xAccessible->getAccessibleContext() ); + + if( xContext.is() ) + atk_wrapper_focus_tracker_notify_when_idle( xContext->getAccessibleChild( nPos ) ); + } + } + } + catch (const uno::Exception&) + { + g_warning( "Exception caught processing menu highlight events" ); + } +} + +/*****************************************************************************/ + +static void WindowEventHandler(void *, VclSimpleEvent& rEvent) +{ + try + { + switch (rEvent.GetId()) + { + case VclEventId::WindowShow: + break; + case VclEventId::WindowHide: + break; + case VclEventId::WindowClose: + break; + case VclEventId::WindowGetFocus: + handle_get_focus(static_cast< ::VclWindowEvent const * >(&rEvent)); + break; + case VclEventId::WindowLoseFocus: + break; + case VclEventId::WindowMinimize: + break; + case VclEventId::WindowNormalize: + break; + case VclEventId::WindowKeyInput: + case VclEventId::WindowKeyUp: + case VclEventId::WindowCommand: + case VclEventId::WindowMouseMove: + break; + + case VclEventId::MenuHighlight: + if (const VclMenuEvent* pMenuEvent = dynamic_cast<const VclMenuEvent*>(&rEvent)) + { + handle_menu_highlighted(pMenuEvent); + } + else if (const VclAccessibleEvent* pAccEvent = dynamic_cast<const VclAccessibleEvent*>(&rEvent)) + { + const uno::Reference< accessibility::XAccessible >& xAccessible = pAccEvent->GetAccessible(); + if (xAccessible.is()) + atk_wrapper_focus_tracker_notify_when_idle(xAccessible); + } + break; + + case VclEventId::ToolboxHighlight: + handle_toolbox_highlight(static_cast< ::VclWindowEvent const * >(&rEvent)->GetWindow()); + break; + + case VclEventId::ToolboxButtonStateChanged: + handle_toolbox_buttonchange(static_cast< ::VclWindowEvent const * >(&rEvent)); + break; + + case VclEventId::ObjectDying: + g_aWindowList.list.erase( static_cast< ::VclWindowEvent const * >(&rEvent)->GetWindow() ); + [[fallthrough]]; + case VclEventId::ToolboxHighlightOff: + handle_toolbox_highlightoff(static_cast< ::VclWindowEvent const * >(&rEvent)->GetWindow()); + break; + + case VclEventId::TabpageActivate: + handle_tabpage_activated(static_cast< ::VclWindowEvent const * >(&rEvent)->GetWindow()); + break; + + case VclEventId::ComboboxSetText: + // This looks quite strange to me. Stumbled over this when fixing #i104290#. + // This kicked in when leaving the combobox in the toolbar, after that the events worked. + // I guess this was a try to work around missing combobox events, which didn't do the full job, and shouldn't be necessary anymore. + // Fix for #i104290# was done in toolkit/source/awt/vclxaccessiblecomponent, FOCUSED state for compound controls in general. + // create_wrapper_for_children(static_cast< ::VclWindowEvent const * >(pEvent)->GetWindow()); + break; + + default: + break; + } + } + catch (const lang::IndexOutOfBoundsException&) + { + g_warning("Focused object has invalid index in parent"); + } +} + +static Link<VclSimpleEvent&,void> g_aEventListenerLink( nullptr, WindowEventHandler ); + +/*****************************************************************************/ + +extern "C" { + +static G_CONST_RETURN gchar * +ooo_atk_util_get_toolkit_name() +{ + return "VCL"; +} + +/*****************************************************************************/ + +static G_CONST_RETURN gchar * +ooo_atk_util_get_toolkit_version() +{ + return LIBO_VERSION_DOTTED; +} + +/*****************************************************************************/ + +/* + * GObject inheritance + */ + +static void +ooo_atk_util_class_init (AtkUtilClass *) +{ + AtkUtilClass *atk_class; + gpointer data; + + data = g_type_class_peek (ATK_TYPE_UTIL); + atk_class = ATK_UTIL_CLASS (data); + + atk_class->get_toolkit_name = ooo_atk_util_get_toolkit_name; + atk_class->get_toolkit_version = ooo_atk_util_get_toolkit_version; + + ooo_atk_util_ensure_event_listener(); +} + +} // extern "C" + +void ooo_atk_util_ensure_event_listener() +{ + static bool bInited; + if (!bInited) + { + Application::AddEventListener( g_aEventListenerLink ); + bInited = true; + } +} + +GType +ooo_atk_util_get_type() +{ + static GType type = 0; + + if (!type) + { + GType parent_type = g_type_from_name( "GailUtil" ); + + if( ! parent_type ) + { + g_warning( "Unknown type: GailUtil" ); + parent_type = ATK_TYPE_UTIL; + } + + GTypeQuery type_query; + g_type_query( parent_type, &type_query ); + + static const GTypeInfo typeInfo = + { + static_cast<guint16>(type_query.class_size), + nullptr, + nullptr, + reinterpret_cast<GClassInitFunc>(ooo_atk_util_class_init), + nullptr, + nullptr, + static_cast<guint16>(type_query.instance_size), + 0, + nullptr, + nullptr + } ; + + type = g_type_register_static (parent_type, "OOoUtil", &typeInfo, GTypeFlags(0)) ; + } + + return type; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkvalue.cxx b/vcl/unx/gtk3/a11y/gtk3atkvalue.cxx index 30057943bc44..f5e45d3b2556 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkvalue.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkvalue.cxx @@ -5,8 +5,134 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkvalue.cxx" +#include "atkwrapper.hxx" + +#include <com/sun/star/accessibility/XAccessibleValue.hpp> + +#include <string.h> + +using namespace ::com::sun::star; + +/// @throws uno::RuntimeException +static css::uno::Reference<css::accessibility::XAccessibleValue> + getValue( AtkValue *pValue ) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pValue ); + if( pWrap ) + { + if( !pWrap->mpValue.is() ) + { + pWrap->mpValue.set(pWrap->mpContext, css::uno::UNO_QUERY); + } + + return pWrap->mpValue; + } + + return css::uno::Reference<css::accessibility::XAccessibleValue>(); +} + +static void anyToGValue( const uno::Any& aAny, GValue *pValue ) +{ + // FIXME: expand to lots of types etc. + double aDouble=0; + aAny >>= aDouble; + + memset( pValue, 0, sizeof( GValue ) ); + g_value_init( pValue, G_TYPE_DOUBLE ); + g_value_set_double( pValue, aDouble ); +} + +extern "C" { + +static void +value_wrapper_get_current_value( AtkValue *value, + GValue *gval ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleValue> pValue + = getValue( value ); + if( pValue.is() ) + anyToGValue( pValue->getCurrentValue(), gval ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getCurrentValue()" ); + } +} + +static void +value_wrapper_get_maximum_value( AtkValue *value, + GValue *gval ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleValue> pValue + = getValue( value ); + if( pValue.is() ) + anyToGValue( pValue->getMaximumValue(), gval ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getCurrentValue()" ); + } +} + +static void +value_wrapper_get_minimum_value( AtkValue *value, + GValue *gval ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleValue> pValue + = getValue( value ); + if( pValue.is() ) + anyToGValue( pValue->getMinimumValue(), gval ); + } + catch(const uno::Exception&) { + g_warning( "Exception in getCurrentValue()" ); + } +} + +static gboolean +value_wrapper_set_current_value( AtkValue *value, + const GValue *gval ) +{ + try { + css::uno::Reference<css::accessibility::XAccessibleValue> pValue + = getValue( value ); + if( pValue.is() ) + { + // FIXME - this needs expanding + double aDouble = g_value_get_double( gval ); + return pValue->setCurrentValue( uno::Any(aDouble) ); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getCurrentValue()" ); + } + + return FALSE; +} + +} // extern "C" + +void +valueIfaceInit (AtkValueIface *iface) +{ + g_return_if_fail (iface != nullptr); + + iface->get_current_value = value_wrapper_get_current_value; + iface->get_maximum_value = value_wrapper_get_maximum_value; + iface->get_minimum_value = value_wrapper_get_minimum_value; + iface->set_current_value = value_wrapper_set_current_value; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkwindow.cxx b/vcl/unx/gtk3/a11y/gtk3atkwindow.cxx index cd8479cd4df7..eb72edf4908c 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkwindow.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkwindow.cxx @@ -5,8 +5,326 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkwindow.cxx" +#include <unx/gtk/gtkframe.hxx> +#include <vcl/svapp.hxx> +#include <vcl/window.hxx> +#include <vcl/popupmenuwindow.hxx> +#include <sal/log.hxx> + +#include "atkwindow.hxx" +#include "atkwrapper.hxx" +#include "atkregistry.hxx" + +#include <com/sun/star/accessibility/AccessibleRole.hpp> + +using namespace ::com::sun::star::accessibility; +using namespace ::com::sun::star::uno; + +extern "C" { + +static void (* window_real_initialize) (AtkObject *obj, gpointer data) = nullptr; +static void (* window_real_finalize) (GObject *obj) = nullptr; + +static void +init_from_window( AtkObject *accessible, vcl::Window const *pWindow ) +{ + static AtkRole aDefaultRole = ATK_ROLE_INVALID; + + // Special role for sub-menu and combo-box popups that are exposed directly + // by their parents already. + if( aDefaultRole == ATK_ROLE_INVALID ) + { + SAL_WNODEPRECATED_DECLARATIONS_PUSH + aDefaultRole = atk_role_register( "redundant object" ); + SAL_WNODEPRECATED_DECLARATIONS_POP + } + + AtkRole role = aDefaultRole; + + // Determine the appropriate role for the GtkWindow + switch( pWindow->GetAccessibleRole() ) + { + case AccessibleRole::ALERT: + role = ATK_ROLE_ALERT; + break; + + case AccessibleRole::DIALOG: + role = ATK_ROLE_DIALOG; + break; + + case AccessibleRole::FRAME: + role = ATK_ROLE_FRAME; + break; + + /* Ignore window objects for sub-menus, combo- and list boxes, + * which are exposed as children of their parents. + */ + case AccessibleRole::WINDOW: + { + WindowType type = WindowType::WINDOW; + bool parentIsMenuFloatingWindow = false; + + vcl::Window *pParent = pWindow->GetParent(); + if( pParent ) { + type = pParent->GetType(); + parentIsMenuFloatingWindow = pParent->IsMenuFloatingWindow(); + } + + if( (WindowType::LISTBOX != type) && (WindowType::COMBOBOX != type) && + (WindowType::MENUBARWINDOW != type) && ! parentIsMenuFloatingWindow ) + { + role = ATK_ROLE_WINDOW; + } + } + break; + + default: + { + vcl::Window *pChild = pWindow->GetWindow(GetWindowType::FirstChild); + if( pChild ) + { + if( WindowType::HELPTEXTWINDOW == pChild->GetType() ) + { + role = ATK_ROLE_TOOL_TIP; + pChild->SetAccessibleRole( AccessibleRole::LABEL ); + accessible->name = g_strdup( OUStringToOString( pChild->GetText(), RTL_TEXTENCODING_UTF8 ).getStr() ); + } + else if ( pWindow->GetType() == WindowType::BORDERWINDOW && pChild->GetType() == WindowType::FLOATINGWINDOW ) + { + PopupMenuFloatingWindow* p = dynamic_cast<PopupMenuFloatingWindow*>(pChild); + if (p && p->IsPopupMenu() && p->GetMenuStackLevel() == 0) + { + // This is a top-level menu popup. Register it. + role = ATK_ROLE_POPUP_MENU; + pChild->SetAccessibleRole( AccessibleRole::POPUP_MENU ); + accessible->name = g_strdup( OUStringToOString( pChild->GetText(), RTL_TEXTENCODING_UTF8 ).getStr() ); + } + } + } + break; + } + } + + accessible->role = role; +} + +/*****************************************************************************/ + +static gboolean +ooo_window_wrapper_clear_focus(gpointer) +{ + SolarMutexGuard aGuard; + SAL_WNODEPRECATED_DECLARATIONS_PUSH + atk_focus_tracker_notify( nullptr ); + SAL_WNODEPRECATED_DECLARATIONS_POP + return false; +} + +/*****************************************************************************/ + +static gboolean +ooo_window_wrapper_real_focus_gtk (GtkWidget *, GdkEventFocus *) +{ + g_idle_add( ooo_window_wrapper_clear_focus, nullptr ); + return false; +} + +static gboolean ooo_tooltip_map( GtkWidget* pToolTip, gpointer ) +{ + AtkObject* pAccessible = gtk_widget_get_accessible( pToolTip ); + if( pAccessible ) + atk_object_notify_state_change( pAccessible, ATK_STATE_SHOWING, TRUE ); + return FALSE; +} + +static gboolean ooo_tooltip_unmap( GtkWidget* pToolTip, gpointer ) +{ + AtkObject* pAccessible = gtk_widget_get_accessible( pToolTip ); + if( pAccessible ) + atk_object_notify_state_change( pAccessible, ATK_STATE_SHOWING, FALSE ); + return FALSE; +} + +/*****************************************************************************/ + +static bool +isChildPopupMenu(vcl::Window* pWindow) +{ + vcl::Window* pChild = pWindow->GetAccessibleChildWindow(0); + if (!pChild) + return false; + + if (WindowType::FLOATINGWINDOW != pChild->GetType()) + return false; + + PopupMenuFloatingWindow* p = dynamic_cast<PopupMenuFloatingWindow*>(pChild); + if (!p) + return false; + + return p->IsPopupMenu(); +} + +static void +ooo_window_wrapper_real_initialize(AtkObject *obj, gpointer data) +{ + window_real_initialize(obj, data); + + GtkSalFrame *pFrame = GtkSalFrame::getFromWindow( GTK_WINDOW( data ) ); + if( pFrame ) + { + vcl::Window *pWindow = pFrame->GetWindow(); + if( pWindow ) + { + init_from_window( obj, pWindow ); + + Reference< XAccessible > xAccessible( pWindow->GetAccessible() ); + + /* We need the wrapper object for the top-level XAccessible to be + * in the wrapper registry when atk traverses the hierarchy up on + * focus events + */ + if( WindowType::BORDERWINDOW == pWindow->GetType() ) + { + if ( isChildPopupMenu(pWindow) ) + { + AtkObject *child = atk_object_wrapper_new( xAccessible, obj ); + ooo_wrapper_registry_add( xAccessible, child ); + } + else + { + ooo_wrapper_registry_add( xAccessible, obj ); + g_object_set_data( G_OBJECT(obj), "ooo:atk-wrapper-key", xAccessible.get() ); + } + } + else + { + AtkObject *child = atk_object_wrapper_new( xAccessible, obj ); + child->role = ATK_ROLE_FILLER; + if( (ATK_ROLE_DIALOG == obj->role) || (ATK_ROLE_ALERT == obj->role) ) + child->role = ATK_ROLE_OPTION_PANE; + ooo_wrapper_registry_add( xAccessible, child ); + } + } + } + + g_signal_connect_after( GTK_WIDGET( data ), "focus-out-event", + G_CALLBACK (ooo_window_wrapper_real_focus_gtk), + nullptr); + + if( obj->role == ATK_ROLE_TOOL_TIP ) + { + g_signal_connect_after( GTK_WIDGET( data ), "map-event", + G_CALLBACK (ooo_tooltip_map), + nullptr); + g_signal_connect_after( GTK_WIDGET( data ), "unmap-event", + G_CALLBACK (ooo_tooltip_unmap), + nullptr); + } +} + +/*****************************************************************************/ + +static void +ooo_window_wrapper_real_finalize (GObject *obj) +{ + ooo_wrapper_registry_remove( static_cast<XAccessible *>(g_object_get_data( obj, "ooo:atk-wrapper-key" ))); + window_real_finalize( obj ); +} + +/*****************************************************************************/ + +static void +ooo_window_wrapper_class_init (AtkObjectClass *klass, gpointer) +{ + AtkObjectClass *atk_class; + GObjectClass *gobject_class; + gpointer data; + + /* + * Patch the gobject vtable of GailWindow to refer to our instance of + * "initialize". + */ + + data = g_type_class_peek_parent( klass ); + atk_class = ATK_OBJECT_CLASS (data); + + window_real_initialize = atk_class->initialize; + atk_class->initialize = ooo_window_wrapper_real_initialize; + + gobject_class = G_OBJECT_CLASS (data); + + window_real_finalize = gobject_class->finalize; + gobject_class->finalize = ooo_window_wrapper_real_finalize; +} + +} // extern "C" + +/*****************************************************************************/ + +GType +ooo_window_wrapper_get_type() +{ + static GType type = 0; + + if (!type) + { + GType parent_type = g_type_from_name( "GailWindow" ); + + if( ! parent_type ) + { + SAL_INFO("vcl.a11y", "Unknown type: GailWindow"); + parent_type = ATK_TYPE_OBJECT; + } + + GTypeQuery type_query; + g_type_query( parent_type, &type_query ); + + static const GTypeInfo typeInfo = + { + static_cast<guint16>(type_query.class_size), + nullptr, + nullptr, + reinterpret_cast<GClassInitFunc>(ooo_window_wrapper_class_init), + nullptr, + nullptr, + static_cast<guint16>(type_query.instance_size), + 0, + nullptr, + nullptr + } ; + + type = g_type_register_static (parent_type, "OOoWindowAtkObject", &typeInfo, GTypeFlags(0)) ; + } + + return type; +} + +void restore_gail_window_vtable() +{ + AtkObjectClass *atk_class; + gpointer data; + + GType type = g_type_from_name( "GailWindow" ); + + if( type == G_TYPE_INVALID ) + return; + + data = g_type_class_peek( type ); + atk_class = ATK_OBJECT_CLASS (data); + + atk_class->initialize = window_real_initialize; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/a11y/gtk3atkwrapper.cxx b/vcl/unx/gtk3/a11y/gtk3atkwrapper.cxx index 3b07e9536d77..0998553bd927 100644 --- a/vcl/unx/gtk3/a11y/gtk3atkwrapper.cxx +++ b/vcl/unx/gtk3/a11y/gtk3atkwrapper.cxx @@ -5,8 +5,955 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#include "../../gtk/a11y/atkwrapper.cxx" +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Type.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/accessibility/AccessibleRole.hpp> +#include <com/sun/star/accessibility/AccessibleRelation.hpp> +#include <com/sun/star/accessibility/AccessibleRelationType.hpp> +#include <com/sun/star/accessibility/AccessibleStateType.hpp> +#include <com/sun/star/accessibility/XAccessible.hpp> +#include <com/sun/star/accessibility/XAccessibleText.hpp> +#include <com/sun/star/accessibility/XAccessibleTextMarkup.hpp> +#include <com/sun/star/accessibility/XAccessibleTextAttributes.hpp> +#include <com/sun/star/accessibility/XAccessibleValue.hpp> +#include <com/sun/star/accessibility/XAccessibleAction.hpp> +#include <com/sun/star/accessibility/XAccessibleContext.hpp> +#include <com/sun/star/accessibility/XAccessibleContext2.hpp> +#include <com/sun/star/accessibility/XAccessibleComponent.hpp> +#include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp> +#include <com/sun/star/accessibility/XAccessibleMultiLineText.hpp> +#include <com/sun/star/accessibility/XAccessibleStateSet.hpp> +#include <com/sun/star/accessibility/XAccessibleRelationSet.hpp> +#include <com/sun/star/accessibility/XAccessibleTable.hpp> +#include <com/sun/star/accessibility/XAccessibleEditableText.hpp> +#include <com/sun/star/accessibility/XAccessibleExtendedAttributes.hpp> +#include <com/sun/star/accessibility/XAccessibleImage.hpp> +#include <com/sun/star/accessibility/XAccessibleHyperlink.hpp> +#include <com/sun/star/accessibility/XAccessibleHypertext.hpp> +#include <com/sun/star/accessibility/XAccessibleSelection.hpp> +#include <com/sun/star/awt/XExtendedToolkit.hpp> +#include <com/sun/star/awt/XTopWindow.hpp> +#include <com/sun/star/awt/XTopWindowListener.hpp> +#include <com/sun/star/awt/XWindow.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/beans/Property.hpp> + +#include <rtl/ref.hxx> +#include <rtl/strbuf.hxx> +#include <osl/diagnose.h> +#include <cppuhelper/factory.hxx> +#include <cppuhelper/queryinterface.hxx> + +#include "atkwrapper.hxx" +#include "atkregistry.hxx" +#include "atklistener.hxx" +#include "atktextattributes.hxx" + +#include <string.h> +#include <vector> + +using namespace ::com::sun::star; + +static GObjectClass *parent_class = nullptr; + +static AtkRelationType mapRelationType( sal_Int16 nRelation ) +{ + AtkRelationType type = ATK_RELATION_NULL; + + switch( nRelation ) + { + case accessibility::AccessibleRelationType::CONTENT_FLOWS_FROM: + type = ATK_RELATION_FLOWS_FROM; + break; + + case accessibility::AccessibleRelationType::CONTENT_FLOWS_TO: + type = ATK_RELATION_FLOWS_TO; + break; + + case accessibility::AccessibleRelationType::CONTROLLED_BY: + type = ATK_RELATION_CONTROLLED_BY; + break; + + case accessibility::AccessibleRelationType::CONTROLLER_FOR: + type = ATK_RELATION_CONTROLLER_FOR; + break; + + case accessibility::AccessibleRelationType::LABEL_FOR: + type = ATK_RELATION_LABEL_FOR; + break; + + case accessibility::AccessibleRelationType::LABELED_BY: + type = ATK_RELATION_LABELLED_BY; + break; + + case accessibility::AccessibleRelationType::MEMBER_OF: + type = ATK_RELATION_MEMBER_OF; + break; + + case accessibility::AccessibleRelationType::SUB_WINDOW_OF: + type = ATK_RELATION_SUBWINDOW_OF; + break; + + case accessibility::AccessibleRelationType::NODE_CHILD_OF: + type = ATK_RELATION_NODE_CHILD_OF; + break; + + default: + break; + } + + return type; +} + +AtkStateType mapAtkState( sal_Int16 nState ) +{ + AtkStateType type = ATK_STATE_INVALID; + + // A perfect / complete mapping ... + switch( nState ) + { +#define MAP_DIRECT( a ) \ + case accessibility::AccessibleStateType::a: \ + type = ATK_STATE_##a; break + + MAP_DIRECT( INVALID ); + MAP_DIRECT( ACTIVE ); + MAP_DIRECT( ARMED ); + MAP_DIRECT( BUSY ); + MAP_DIRECT( CHECKED ); + MAP_DIRECT( EDITABLE ); + MAP_DIRECT( ENABLED ); + MAP_DIRECT( EXPANDABLE ); + MAP_DIRECT( EXPANDED ); + MAP_DIRECT( FOCUSABLE ); + MAP_DIRECT( FOCUSED ); + MAP_DIRECT( HORIZONTAL ); + MAP_DIRECT( ICONIFIED ); + MAP_DIRECT( INDETERMINATE ); + MAP_DIRECT( MANAGES_DESCENDANTS ); + MAP_DIRECT( MODAL ); + MAP_DIRECT( MULTI_LINE ); + MAP_DIRECT( OPAQUE ); + MAP_DIRECT( PRESSED ); + MAP_DIRECT( RESIZABLE ); + MAP_DIRECT( SELECTABLE ); + MAP_DIRECT( SELECTED ); + MAP_DIRECT( SENSITIVE ); + MAP_DIRECT( SHOWING ); + MAP_DIRECT( SINGLE_LINE ); + MAP_DIRECT( STALE ); + MAP_DIRECT( TRANSIENT ); + MAP_DIRECT( VERTICAL ); + MAP_DIRECT( VISIBLE ); + MAP_DIRECT( DEFAULT ); + // a spelling error ... + case accessibility::AccessibleStateType::DEFUNC: + type = ATK_STATE_DEFUNCT; break; + case accessibility::AccessibleStateType::MULTI_SELECTABLE: + type = ATK_STATE_MULTISELECTABLE; break; + default: + //Mis-use ATK_STATE_LAST_DEFINED to check if a state is unmapped + //NOTE! Do not report it + type = ATK_STATE_LAST_DEFINED; + break; + } + + return type; +} + +static AtkRole getRoleForName( const gchar * name ) +{ + AtkRole ret = atk_role_for_name( name ); + if( ATK_ROLE_INVALID == ret ) + { + // this should only happen in old ATK versions + SAL_WNODEPRECATED_DECLARATIONS_PUSH + ret = atk_role_register( name ); + SAL_WNODEPRECATED_DECLARATIONS_POP + } + + return ret; +} + +static AtkRole mapToAtkRole( sal_Int16 nRole ) +{ + AtkRole role = ATK_ROLE_UNKNOWN; + + static AtkRole roleMap[] = { + ATK_ROLE_UNKNOWN, + ATK_ROLE_ALERT, + ATK_ROLE_COLUMN_HEADER, + ATK_ROLE_CANVAS, + ATK_ROLE_CHECK_BOX, + ATK_ROLE_CHECK_MENU_ITEM, + ATK_ROLE_COLOR_CHOOSER, + ATK_ROLE_COMBO_BOX, + ATK_ROLE_DATE_EDITOR, + ATK_ROLE_DESKTOP_ICON, + ATK_ROLE_DESKTOP_FRAME, // ? pane + ATK_ROLE_DIRECTORY_PANE, + ATK_ROLE_DIALOG, + ATK_ROLE_UNKNOWN, // DOCUMENT - registered below + ATK_ROLE_UNKNOWN, // EMBEDDED_OBJECT - registered below + ATK_ROLE_UNKNOWN, // END_NOTE - registered below + ATK_ROLE_FILE_CHOOSER, + ATK_ROLE_FILLER, + ATK_ROLE_FONT_CHOOSER, + ATK_ROLE_FOOTER, + ATK_ROLE_UNKNOWN, // FOOTNOTE - registered below + ATK_ROLE_FRAME, + ATK_ROLE_GLASS_PANE, + ATK_ROLE_IMAGE, // GRAPHIC + ATK_ROLE_UNKNOWN, // GROUP_BOX - registered below + ATK_ROLE_HEADER, + ATK_ROLE_HEADING, + ATK_ROLE_TEXT, // HYPER_LINK - registered below + ATK_ROLE_ICON, + ATK_ROLE_INTERNAL_FRAME, + ATK_ROLE_LABEL, + ATK_ROLE_LAYERED_PANE, + ATK_ROLE_LIST, + ATK_ROLE_LIST_ITEM, + ATK_ROLE_MENU, + ATK_ROLE_MENU_BAR, + ATK_ROLE_MENU_ITEM, + ATK_ROLE_OPTION_PANE, + ATK_ROLE_PAGE_TAB, + ATK_ROLE_PAGE_TAB_LIST, + ATK_ROLE_PANEL, + ATK_ROLE_PARAGRAPH, + ATK_ROLE_PASSWORD_TEXT, + ATK_ROLE_POPUP_MENU, + ATK_ROLE_PUSH_BUTTON, + ATK_ROLE_PROGRESS_BAR, + ATK_ROLE_RADIO_BUTTON, + ATK_ROLE_RADIO_MENU_ITEM, + ATK_ROLE_ROW_HEADER, + ATK_ROLE_ROOT_PANE, + ATK_ROLE_SCROLL_BAR, + ATK_ROLE_SCROLL_PANE, + ATK_ROLE_PANEL, // SHAPE + ATK_ROLE_SEPARATOR, + ATK_ROLE_SLIDER, + ATK_ROLE_SPIN_BUTTON, // SPIN_BOX ? + ATK_ROLE_SPLIT_PANE, + ATK_ROLE_STATUSBAR, + ATK_ROLE_TABLE, + ATK_ROLE_TABLE_CELL, + ATK_ROLE_TEXT, + ATK_ROLE_PANEL, // TEXT_FRAME + ATK_ROLE_TOGGLE_BUTTON, + ATK_ROLE_TOOL_BAR, + ATK_ROLE_TOOL_TIP, + ATK_ROLE_TREE, + ATK_ROLE_VIEWPORT, + ATK_ROLE_WINDOW, + ATK_ROLE_PUSH_BUTTON, // BUTTON_DROPDOWN + ATK_ROLE_PUSH_BUTTON, // BUTTON_MENU + ATK_ROLE_UNKNOWN, // CAPTION - registered below + ATK_ROLE_UNKNOWN, // CHART - registered below + ATK_ROLE_UNKNOWN, // EDIT_BAR - registered below + ATK_ROLE_UNKNOWN, // FORM - registered below + ATK_ROLE_UNKNOWN, // IMAGE_MAP - registered below + ATK_ROLE_UNKNOWN, // NOTE - registered below + ATK_ROLE_UNKNOWN, // PAGE - registered below + ATK_ROLE_RULER, + ATK_ROLE_UNKNOWN, // SECTION - registered below + ATK_ROLE_UNKNOWN, // TREE_ITEM - registered below + ATK_ROLE_TREE_TABLE, + ATK_ROLE_SCROLL_PANE, // COMMENT - mapped to atk_role_scroll_pane + ATK_ROLE_UNKNOWN // COMMENT_END - mapped to atk_role_unknown +#if defined(ATK_CHECK_VERSION) + //older ver that doesn't define ATK_CHECK_VERSION doesn't have the following + , ATK_ROLE_DOCUMENT_PRESENTATION + , ATK_ROLE_DOCUMENT_SPREADSHEET + , ATK_ROLE_DOCUMENT_TEXT +#if ATK_CHECK_VERSION(2,15,2) + , ATK_ROLE_STATIC +#else + , ATK_ROLE_LABEL +#endif +#else + //older version should fallback to DOCUMENT_FRAME role + , ATK_ROLE_DOCUMENT_FRAME + , ATK_ROLE_DOCUMENT_FRAME + , ATK_ROLE_DOCUMENT_FRAME + , ATK_ROLE_LABEL +#endif + }; + + static bool initialized = false; + + if( ! initialized ) + { + // the accessible roles below were added to ATK in later versions, + // with role_for_name we will know if they exist in runtime. + roleMap[accessibility::AccessibleRole::EDIT_BAR] = getRoleForName("edit bar"); + roleMap[accessibility::AccessibleRole::EMBEDDED_OBJECT] = getRoleForName("embedded"); + roleMap[accessibility::AccessibleRole::CHART] = getRoleForName("chart"); + roleMap[accessibility::AccessibleRole::CAPTION] = getRoleForName("caption"); + roleMap[accessibility::AccessibleRole::DOCUMENT] = getRoleForName("document frame"); + roleMap[accessibility::AccessibleRole::PAGE] = getRoleForName("page"); + roleMap[accessibility::AccessibleRole::SECTION] = getRoleForName("section"); + roleMap[accessibility::AccessibleRole::FORM] = getRoleForName("form"); + roleMap[accessibility::AccessibleRole::GROUP_BOX] = getRoleForName("grouping"); + roleMap[accessibility::AccessibleRole::COMMENT] = getRoleForName("comment"); + roleMap[accessibility::AccessibleRole::IMAGE_MAP] = getRoleForName("image map"); + roleMap[accessibility::AccessibleRole::TREE_ITEM] = getRoleForName("tree item"); + roleMap[accessibility::AccessibleRole::HYPER_LINK] = getRoleForName("link"); + roleMap[accessibility::AccessibleRole::END_NOTE] = getRoleForName("footnote"); + roleMap[accessibility::AccessibleRole::FOOTNOTE] = getRoleForName("footnote"); + roleMap[accessibility::AccessibleRole::NOTE] = getRoleForName("comment"); + + initialized = true; + } + + static const sal_Int32 nMapSize = SAL_N_ELEMENTS(roleMap); + if( 0 <= nRole && nMapSize > nRole ) + role = roleMap[nRole]; + + return role; +} + +/*****************************************************************************/ + +extern "C" { + +/*****************************************************************************/ + +static G_CONST_RETURN gchar* +wrapper_get_name( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + + if( obj->mpContext.is() ) + { + try { + OString aName = + OUStringToOString( + obj->mpContext->getAccessibleName(), + RTL_TEXTENCODING_UTF8); + + int nCmp = atk_obj->name ? rtl_str_compare( atk_obj->name, aName.getStr() ) : -1; + if( nCmp != 0 ) + { + if( atk_obj->name ) + g_free(atk_obj->name); + atk_obj->name = g_strdup(aName.getStr()); + } + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleName()" ); + } + } + + return ATK_OBJECT_CLASS (parent_class)->get_name(atk_obj); +} + +/*****************************************************************************/ + +static G_CONST_RETURN gchar* +wrapper_get_description( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + + if( obj->mpContext.is() ) + { + try { + OString aDescription = + OUStringToOString( + obj->mpContext->getAccessibleDescription(), + RTL_TEXTENCODING_UTF8); + + g_free(atk_obj->description); + atk_obj->description = g_strdup(aDescription.getStr()); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleDescription()" ); + } + } + + return ATK_OBJECT_CLASS (parent_class)->get_description(atk_obj); + +} + +/*****************************************************************************/ + +static AtkAttributeSet * +wrapper_get_attributes( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER( atk_obj ); + AtkAttributeSet *pSet = nullptr; + + try + { + uno::Reference< accessibility::XAccessibleExtendedAttributes > + xExtendedAttrs( obj->mpContext, uno::UNO_QUERY ); + if( xExtendedAttrs.is() ) + pSet = attribute_set_new_from_extended_attributes( xExtendedAttrs ); + } + catch(const uno::Exception&) + { + g_warning( "Exception in getAccessibleAttributes()" ); + } + + return pSet; +} + +/*****************************************************************************/ + +static gint +wrapper_get_n_children( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + gint n = 0; + + if( obj->mpContext.is() ) + { + try { + n = obj->mpContext->getAccessibleChildCount(); + } + catch(const uno::Exception&) { + OSL_FAIL("Exception in getAccessibleChildCount()" ); + } + } + + return n; +} + +/*****************************************************************************/ + +static AtkObject * +wrapper_ref_child( AtkObject *atk_obj, + gint i ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + AtkObject* child = nullptr; + + // see comments above atk_object_wrapper_remove_child + if( -1 < i && obj->index_of_child_about_to_be_removed == i ) + { + g_object_ref( obj->child_about_to_be_removed ); + return obj->child_about_to_be_removed; + } + + if( obj->mpContext.is() ) + { + try { + uno::Reference< accessibility::XAccessible > xAccessible = + obj->mpContext->getAccessibleChild( i ); + + child = atk_object_wrapper_ref( xAccessible ); + } + catch(const uno::Exception&) { + OSL_FAIL("Exception in getAccessibleChild"); + } + } + + return child; +} + +/*****************************************************************************/ + +static gint +wrapper_get_index_in_parent( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y + if (obj->mpOrig) + return atk_object_get_index_in_parent(obj->mpOrig); + + gint i = -1; + + if( obj->mpContext.is() ) + { + try { + i = obj->mpContext->getAccessibleIndexInParent(); + } + catch(const uno::Exception&) { + g_warning( "Exception in getAccessibleIndexInParent()" ); + } + } + return i; +} + +/*****************************************************************************/ + +AtkRelation* +atk_object_wrapper_relation_new(const accessibility::AccessibleRelation& rRelation) +{ + sal_uInt32 nTargetCount = rRelation.TargetSet.getLength(); + + std::vector<AtkObject*> aTargets; + + for (const auto& rTarget : rRelation.TargetSet) + { + uno::Reference< accessibility::XAccessible > xAccessible( rTarget, uno::UNO_QUERY ); + aTargets.push_back(atk_object_wrapper_ref(xAccessible)); + } + + AtkRelation *pRel = + atk_relation_new( + aTargets.data(), nTargetCount, + mapRelationType( rRelation.RelationType ) + ); + + return pRel; +} + +static AtkRelationSet * +wrapper_ref_relation_set( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + + //if we're a native GtkDrawingArea with custom a11y, use the default toolkit relation set impl + if (obj->mpOrig) + return atk_object_ref_relation_set(obj->mpOrig); + + AtkRelationSet *pSet = atk_relation_set_new(); + + if( obj->mpContext.is() ) + { + try { + uno::Reference< accessibility::XAccessibleRelationSet > xRelationSet( + obj->mpContext->getAccessibleRelationSet() + ); + + sal_Int32 nRelations = xRelationSet.is() ? xRelationSet->getRelationCount() : 0; + for( sal_Int32 n = 0; n < nRelations; n++ ) + { + AtkRelation *pRel = atk_object_wrapper_relation_new(xRelationSet->getRelation(n)); + atk_relation_set_add(pSet, pRel); + g_object_unref(pRel); + } + } + catch(const uno::Exception &) { + g_object_unref( G_OBJECT( pSet ) ); + pSet = nullptr; + } + } + + return pSet; +} + +static AtkStateSet * +wrapper_ref_state_set( AtkObject *atk_obj ) +{ + AtkObjectWrapper *obj = ATK_OBJECT_WRAPPER (atk_obj); + AtkStateSet *pSet = atk_state_set_new(); + + if( obj->mpContext.is() ) + { + try { + uno::Reference< accessibility::XAccessibleStateSet > xStateSet( + obj->mpContext->getAccessibleStateSet()); + + if( xStateSet.is() ) + { + uno::Sequence< sal_Int16 > aStates = xStateSet->getStates(); + + for( const auto nState : aStates ) + { + // ATK_STATE_LAST_DEFINED is used to check if the state + // is unmapped, do not report it to Atk + if ( mapAtkState( nState ) != ATK_STATE_LAST_DEFINED ) + atk_state_set_add_state( pSet, mapAtkState( nState ) ); + } + + // We need to emulate FOCUS state for menus, menu-items etc. + if( atk_obj == atk_get_focus_object() ) + atk_state_set_add_state( pSet, ATK_STATE_FOCUSED ); +/* FIXME - should we do this ? + else + atk_state_set_remove_state( pSet, ATK_STATE_FOCUSED ); +*/ + } + } + + catch(const uno::Exception &) { + g_warning( "Exception in wrapper_ref_state_set" ); + atk_state_set_add_state( pSet, ATK_STATE_DEFUNCT ); + } + } + else + atk_state_set_add_state( pSet, ATK_STATE_DEFUNCT ); + + return pSet; +} + +/*****************************************************************************/ + +static void +atk_object_wrapper_finalize (GObject *obj) +{ + AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER (obj); + + if( pWrap->mpAccessible.is() ) + { + ooo_wrapper_registry_remove( pWrap->mpAccessible ); + pWrap->mpAccessible.clear(); + } + + atk_object_wrapper_dispose( pWrap ); + + parent_class->finalize( obj ); +} + +static void +atk_object_wrapper_class_init (AtkObjectWrapperClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( klass ); + AtkObjectClass *atk_class = ATK_OBJECT_CLASS( klass ); + + parent_class = static_cast<GObjectClass *>(g_type_class_peek_parent (klass)); + + // GObject methods + gobject_class->finalize = atk_object_wrapper_finalize; + + // AtkObject methods + atk_class->get_name = wrapper_get_name; + atk_class->get_description = wrapper_get_description; + atk_class->get_attributes = wrapper_get_attributes; + atk_class->get_n_children = wrapper_get_n_children; + atk_class->ref_child = wrapper_ref_child; + atk_class->get_index_in_parent = wrapper_get_index_in_parent; + atk_class->ref_relation_set = wrapper_ref_relation_set; + atk_class->ref_state_set = wrapper_ref_state_set; +} + +static void +atk_object_wrapper_init (AtkObjectWrapper *wrapper, + AtkObjectWrapperClass*) +{ + wrapper->mpAction = nullptr; + wrapper->mpComponent = nullptr; + wrapper->mpEditableText = nullptr; + wrapper->mpHypertext = nullptr; + wrapper->mpImage = nullptr; + wrapper->mpSelection = nullptr; + wrapper->mpTable = nullptr; + wrapper->mpText = nullptr; + wrapper->mpValue = nullptr; +} + +} // extern "C" + +GType +atk_object_wrapper_get_type() +{ + static GType type = 0; + + if (!type) + { + static const GTypeInfo typeInfo = + { + sizeof (AtkObjectWrapperClass), + nullptr, + nullptr, + reinterpret_cast<GClassInitFunc>(atk_object_wrapper_class_init), + nullptr, + nullptr, + sizeof (AtkObjectWrapper), + 0, + reinterpret_cast<GInstanceInitFunc>(atk_object_wrapper_init), + nullptr + } ; + type = g_type_register_static (ATK_TYPE_OBJECT, + "OOoAtkObj", + &typeInfo, GTypeFlags(0)) ; + } + return type; +} + +static bool +isOfType( uno::XInterface *pInterface, const uno::Type & rType ) +{ + g_return_val_if_fail( pInterface != nullptr, false ); + + bool bIs = false; + try { + uno::Any aRet = pInterface->queryInterface( rType ); + + bIs = ( ( typelib_TypeClass_INTERFACE == aRet.pType->eTypeClass ) && + ( aRet.pReserved != nullptr ) ); + } catch( const uno::Exception &) { } + + return bIs; +} + +extern "C" { +typedef GType (* GetGIfaceType ) (); +} +const struct { + const char *name; + GInterfaceInitFunc const aInit; + GetGIfaceType const aGetGIfaceType; + const uno::Type & (*aGetUnoType) (); +} aTypeTable[] = { +// re-location heaven: + { + "Comp", reinterpret_cast<GInterfaceInitFunc>(componentIfaceInit), + atk_component_get_type, + cppu::UnoType<accessibility::XAccessibleComponent>::get + }, + { + "Act", reinterpret_cast<GInterfaceInitFunc>(actionIfaceInit), + atk_action_get_type, + cppu::UnoType<accessibility::XAccessibleAction>::get + }, + { + "Txt", reinterpret_cast<GInterfaceInitFunc>(textIfaceInit), + atk_text_get_type, + cppu::UnoType<accessibility::XAccessibleText>::get + }, + { + "Val", reinterpret_cast<GInterfaceInitFunc>(valueIfaceInit), + atk_value_get_type, + cppu::UnoType<accessibility::XAccessibleValue>::get + }, + { + "Tab", reinterpret_cast<GInterfaceInitFunc>(tableIfaceInit), + atk_table_get_type, + cppu::UnoType<accessibility::XAccessibleTable>::get + }, + { + "Edt", reinterpret_cast<GInterfaceInitFunc>(editableTextIfaceInit), + atk_editable_text_get_type, + cppu::UnoType<accessibility::XAccessibleEditableText>::get + }, + { + "Img", reinterpret_cast<GInterfaceInitFunc>(imageIfaceInit), + atk_image_get_type, + cppu::UnoType<accessibility::XAccessibleImage>::get + }, + { + "Hyp", reinterpret_cast<GInterfaceInitFunc>(hypertextIfaceInit), + atk_hypertext_get_type, + cppu::UnoType<accessibility::XAccessibleHypertext>::get + }, + { + "Sel", reinterpret_cast<GInterfaceInitFunc>(selectionIfaceInit), + atk_selection_get_type, + cppu::UnoType<accessibility::XAccessibleSelection>::get + } + // AtkDocument is a nastily broken interface (so far) + // we could impl. get_document_type perhaps though. +}; + +const int aTypeTableSize = G_N_ELEMENTS( aTypeTable ); + +static GType +ensureTypeFor( uno::XInterface *pAccessible ) +{ + int i; + bool bTypes[ aTypeTableSize ] = { false, }; + OStringBuffer aTypeNameBuf( "OOoAtkObj" ); + + for( i = 0; i < aTypeTableSize; i++ ) + { + if( isOfType( pAccessible, aTypeTable[i].aGetUnoType() ) ) + { + aTypeNameBuf.append(aTypeTable[i].name); + bTypes[i] = true; + } + } + + OString aTypeName = aTypeNameBuf.makeStringAndClear(); + GType nType = g_type_from_name( aTypeName.getStr() ); + if( nType == G_TYPE_INVALID ) + { + GTypeInfo aTypeInfo = { + sizeof( AtkObjectWrapperClass ), + nullptr, nullptr, nullptr, nullptr, nullptr, + sizeof( AtkObjectWrapper ), + 0, nullptr, nullptr + } ; + nType = g_type_register_static( ATK_TYPE_OBJECT_WRAPPER, + aTypeName.getStr(), &aTypeInfo, + GTypeFlags(0) ) ; + + for( int j = 0; j < aTypeTableSize; j++ ) + if( bTypes[j] ) + { + GInterfaceInfo aIfaceInfo = { nullptr, nullptr, nullptr }; + aIfaceInfo.interface_init = aTypeTable[j].aInit; + g_type_add_interface_static (nType, aTypeTable[j].aGetGIfaceType(), + &aIfaceInfo); + } + } + return nType; +} + +AtkObject * +atk_object_wrapper_ref( const uno::Reference< accessibility::XAccessible > &rxAccessible, bool create ) +{ + g_return_val_if_fail( rxAccessible.get() != nullptr, nullptr ); + + AtkObject *obj = ooo_wrapper_registry_get(rxAccessible); + if( obj ) + { + g_object_ref( obj ); + return obj; + } + + if( create ) + return atk_object_wrapper_new( rxAccessible ); + + return nullptr; +} + +AtkObject * +atk_object_wrapper_new( const css::uno::Reference< css::accessibility::XAccessible >& rxAccessible, + AtkObject* parent, AtkObject* orig ) +{ + g_return_val_if_fail( rxAccessible.get() != nullptr, nullptr ); + + AtkObjectWrapper *pWrap = nullptr; + + try { + uno::Reference< accessibility::XAccessibleContext > xContext(rxAccessible->getAccessibleContext()); + + g_return_val_if_fail( xContext.get() != nullptr, nullptr ); + + GType nType = ensureTypeFor( xContext.get() ); + gpointer obj = g_object_new( nType, nullptr); + + pWrap = ATK_OBJECT_WRAPPER( obj ); + pWrap->mpAccessible = rxAccessible; + + pWrap->index_of_child_about_to_be_removed = -1; + pWrap->child_about_to_be_removed = nullptr; + + pWrap->mpContext = xContext; + pWrap->mpOrig = orig; + + AtkObject* atk_obj = ATK_OBJECT(pWrap); + atk_obj->role = mapToAtkRole( xContext->getAccessibleRole() ); + atk_obj->accessible_parent = parent; + + ooo_wrapper_registry_add( rxAccessible, atk_obj ); + + if( parent ) + g_object_ref( atk_obj->accessible_parent ); + else + { + /* gail_focus_tracker remembers the focused object at the first + * parent in the hierarchy that is a Gtk+ widget, but at the time the + * event gets processed (at idle), it may be too late to create the + * hierarchy, so doing it now .. + */ + uno::Reference< accessibility::XAccessible > xParent( xContext->getAccessibleParent() ); + + if( xParent.is() ) + atk_obj->accessible_parent = atk_object_wrapper_ref( xParent ); + } + + // Attach a listener to the UNO object if it's not TRANSIENT + uno::Reference< accessibility::XAccessibleStateSet > xStateSet( xContext->getAccessibleStateSet() ); + if( xStateSet.is() && ! xStateSet->contains( accessibility::AccessibleStateType::TRANSIENT ) ) + { + uno::Reference< accessibility::XAccessibleEventBroadcaster > xBroadcaster(xContext, uno::UNO_QUERY); + if( xBroadcaster.is() ) + { + uno::Reference<accessibility::XAccessibleEventListener> xListener(new AtkListener(pWrap)); + xBroadcaster->addAccessibleEventListener(xListener); + } + else + OSL_ASSERT( false ); + } + +#if ATK_CHECK_VERSION(2,33,1) + { + css::uno::Reference<css::accessibility::XAccessibleContext2> xContext2(xContext, css::uno::UNO_QUERY); + if( xContext2.is() ) + { + OString aId = OUStringToOString( xContext2->getAccessibleId(), RTL_TEXTENCODING_UTF8); + atk_object_set_accessible_id(atk_obj, aId.getStr()); + } + } +#endif + + return ATK_OBJECT( pWrap ); + } + catch (const uno::Exception &) + { + if( pWrap ) + g_object_unref( pWrap ); + + return nullptr; + } +} + +/*****************************************************************************/ + +void atk_object_wrapper_add_child(AtkObjectWrapper* wrapper, AtkObject *child, gint index) +{ + AtkObject *atk_obj = ATK_OBJECT( wrapper ); + + atk_object_set_parent( child, atk_obj ); + g_signal_emit_by_name( atk_obj, "children_changed::add", index, child, nullptr ); +} + +/*****************************************************************************/ + +void atk_object_wrapper_remove_child(AtkObjectWrapper* wrapper, AtkObject *child, gint index) +{ + /* + * the atk-bridge GTK+ module gets back to the event source to ref the child just + * vanishing, so we keep this reference because the semantic on OOo side is different. + */ + wrapper->child_about_to_be_removed = child; + wrapper->index_of_child_about_to_be_removed = index; + + g_signal_emit_by_name( ATK_OBJECT( wrapper ), "children_changed::remove", index, child, nullptr ); + + wrapper->index_of_child_about_to_be_removed = -1; + wrapper->child_about_to_be_removed = nullptr; +} + +/*****************************************************************************/ + +void atk_object_wrapper_set_role(AtkObjectWrapper* wrapper, sal_Int16 role) +{ + AtkObject *atk_obj = ATK_OBJECT( wrapper ); + atk_object_set_role( atk_obj, mapToAtkRole( role ) ); +} + +/*****************************************************************************/ + +void atk_object_wrapper_dispose(AtkObjectWrapper* wrapper) +{ + wrapper->mpContext.clear(); + wrapper->mpAction.clear(); + wrapper->mpComponent.clear(); + wrapper->mpEditableText.clear(); + wrapper->mpHypertext.clear(); + wrapper->mpImage.clear(); + wrapper->mpSelection.clear(); + wrapper->mpMultiLineText.clear(); + wrapper->mpTable.clear(); + wrapper->mpText.clear(); + wrapper->mpTextMarkup.clear(); + wrapper->mpTextAttributes.clear(); + wrapper->mpValue.clear(); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/SalGtkFilePicker.cxx b/vcl/unx/gtk3/fpicker/SalGtkFilePicker.cxx new file mode 100644 index 000000000000..2552bb16b253 --- /dev/null +++ b/vcl/unx/gtk3/fpicker/SalGtkFilePicker.cxx @@ -0,0 +1,1993 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifdef AIX +#define _LINUX_SOURCE_COMPAT +#include <sys/timer.h> +#undef _LINUX_SOURCE_COMPAT +#endif + +#include <config_gio.h> + +#include <com/sun/star/awt/SystemDependentXWindow.hpp> +#include <com/sun/star/awt/Toolkit.hpp> +#include <com/sun/star/awt/XSystemDependentWindowPeer.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/lang/IllegalArgumentException.hpp> +#include <com/sun/star/lang/SystemDependent.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> +#include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp> +#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp> +#include <osl/diagnose.h> +#include <osl/process.h> +#include <rtl/process.h> +#include <sal/log.hxx> +#include <com/sun/star/ui/dialogs/TemplateDescription.hpp> +#include <com/sun/star/ui/dialogs/ControlActions.hpp> +#include <com/sun/star/uno/Any.hxx> +#include <unx/gtk/gtkdata.hxx> +#include <unx/gtk/gtkinst.hxx> + +#include <vcl/svapp.hxx> + +#include <tools/urlobj.hxx> + +#include <algorithm> +#include <set> +#include <string.h> + +#include "SalGtkFilePicker.hxx" + +using namespace ::com::sun::star; +using namespace ::com::sun::star::ui::dialogs; +using namespace ::com::sun::star::ui::dialogs::TemplateDescription; +using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds; +using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::uno; + +void SalGtkFilePicker::dialog_mapped_cb(GtkWidget *, SalGtkFilePicker *pobjFP) +{ + pobjFP->InitialMapping(); +} + +void SalGtkFilePicker::InitialMapping() +{ + if (!mbPreviewState ) + { + gtk_widget_hide( m_pPreview ); + gtk_file_chooser_set_preview_widget_active( GTK_FILE_CHOOSER( m_pDialog ), false); + } + gtk_widget_set_size_request (m_pPreview, -1, -1); +} + +SalGtkFilePicker::SalGtkFilePicker( const uno::Reference< uno::XComponentContext >& xContext ) : + SalGtkPicker( xContext ), + SalGtkFilePicker_Base( m_rbHelperMtx ), + m_pParentWidget ( nullptr ), + m_pVBox ( nullptr ), + mnHID_FolderChange( 0 ), + mnHID_SelectionChange( 0 ), + bVersionWidthUnset( false ), + mbPreviewState( false ), + mHID_Preview( 0 ), + m_pPreview( nullptr ), + m_pPseudoFilter( nullptr ) +{ + int i; + + for( i = 0; i < TOGGLE_LAST; i++ ) + { + m_pToggles[i] = nullptr; + mbToggleVisibility[i] = false; + } + + for( i = 0; i < BUTTON_LAST; i++ ) + { + m_pButtons[i] = nullptr; + mbButtonVisibility[i] = false; + } + + for( i = 0; i < LIST_LAST; i++ ) + { + m_pHBoxs[i] = nullptr; + m_pAligns[i] = nullptr; + m_pLists[i] = nullptr; + m_pListLabels[i] = nullptr; + mbListVisibility[i] = false; + } + + OUString aFilePickerTitle = getResString( FILE_PICKER_TITLE_OPEN ); + + m_pDialog = gtk_file_chooser_dialog_new( + OUStringToOString( aFilePickerTitle, RTL_TEXTENCODING_UTF8 ).getStr(), + nullptr, + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + nullptr ); + + gtk_dialog_set_default_response( GTK_DIALOG (m_pDialog), GTK_RESPONSE_ACCEPT ); + +#if ENABLE_GIO + gtk_file_chooser_set_local_only( GTK_FILE_CHOOSER( m_pDialog ), false ); +#endif + gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( m_pDialog ), false ); + + m_pVBox = gtk_vbox_new( false, 0 ); + + // We don't want clickable items to have a huge hit-area + GtkWidget *pHBox = gtk_hbox_new( false, 0 ); + GtkWidget *pThinVBox = gtk_vbox_new( false, 0 ); + + gtk_box_pack_end (GTK_BOX( m_pVBox ), pHBox, false, false, 0); + gtk_box_pack_start (GTK_BOX( pHBox ), pThinVBox, false, false, 0); + gtk_widget_show( pHBox ); + gtk_widget_show( pThinVBox ); + + OUString aLabel; + + for( i = 0; i < TOGGLE_LAST; i++ ) + { + m_pToggles[i] = gtk_check_button_new(); + +#define LABEL_TOGGLE( elem ) \ + case elem : \ + aLabel = getResString( CHECKBOX_##elem ); \ + setLabel( CHECKBOX_##elem, aLabel ); \ + break + + switch( i ) { + LABEL_TOGGLE( AUTOEXTENSION ); + LABEL_TOGGLE( PASSWORD ); + LABEL_TOGGLE( GPGENCRYPTION ); + LABEL_TOGGLE( FILTEROPTIONS ); + LABEL_TOGGLE( READONLY ); + LABEL_TOGGLE( LINK ); + LABEL_TOGGLE( PREVIEW ); + LABEL_TOGGLE( SELECTION ); + default: + SAL_WARN( "vcl.gtk", "Handle unknown control " << i); + break; + } + + gtk_box_pack_end( GTK_BOX( pThinVBox ), m_pToggles[i], false, false, 0 ); + } + + for( i = 0; i < LIST_LAST; i++ ) + { + m_pHBoxs[i] = gtk_hbox_new( false, 0 ); + + m_pAligns[i] = gtk_alignment_new(0, 0, 0, 1); + + GtkListStore *pListStores[ LIST_LAST ]; + pListStores[i] = gtk_list_store_new (1, G_TYPE_STRING); + m_pLists[i] = gtk_combo_box_new_with_model(GTK_TREE_MODEL(pListStores[i])); + g_object_unref (pListStores[i]); // owned by the widget. + GtkCellRenderer *pCell = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start( + GTK_CELL_LAYOUT(m_pLists[i]), pCell, TRUE); + gtk_cell_layout_set_attributes( + GTK_CELL_LAYOUT (m_pLists[i]), pCell, "text", 0, nullptr); + + m_pListLabels[i] = gtk_label_new( "" ); + +#define LABEL_LIST( elem ) \ + case elem : \ + aLabel = getResString( LISTBOX_##elem##_LABEL ); \ + setLabel( LISTBOX_##elem##_LABEL, aLabel ); \ + break + + switch( i ) + { + LABEL_LIST( VERSION ); + LABEL_LIST( TEMPLATE ); + LABEL_LIST( IMAGE_TEMPLATE ); + LABEL_LIST( IMAGE_ANCHOR ); + default: + SAL_WARN( "vcl.gtk", "Handle unknown control " << i); + break; + } + + gtk_container_add( GTK_CONTAINER( m_pAligns[i]), m_pLists[i] ); + gtk_box_pack_end( GTK_BOX( m_pHBoxs[i] ), m_pAligns[i], false, false, 0 ); + gtk_box_pack_end( GTK_BOX( m_pHBoxs[i] ), m_pListLabels[i], false, false, 0 ); + gtk_label_set_mnemonic_widget( GTK_LABEL(m_pListLabels[i]), m_pLists[i] ); + gtk_box_set_spacing( GTK_BOX( m_pHBoxs[i] ), 12 ); + + gtk_box_pack_end( GTK_BOX( m_pVBox ), m_pHBoxs[i], false, false, 0 ); + } + + aLabel = getResString( FILE_PICKER_FILE_TYPE ); + m_pFilterExpander = gtk_expander_new_with_mnemonic( + OUStringToOString( aLabel, RTL_TEXTENCODING_UTF8 ).getStr()); + + gtk_box_pack_end( GTK_BOX( m_pVBox ), m_pFilterExpander, false, true, 0 ); + + GtkWidget *scrolled_window = gtk_scrolled_window_new (nullptr, nullptr); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), + GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), + GTK_SHADOW_IN); + gtk_container_add (GTK_CONTAINER (m_pFilterExpander), scrolled_window); + gtk_widget_show (scrolled_window); + + m_pFilterStore = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, + G_TYPE_STRING, G_TYPE_STRING); + m_pFilterView = gtk_tree_view_new_with_model (GTK_TREE_MODEL(m_pFilterStore)); + gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(m_pFilterView), false); + gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(m_pFilterView), true); + + GtkCellRenderer *cell = nullptr; + + for (i = 0; i < 2; ++i) + { + GtkTreeViewColumn *column = gtk_tree_view_column_new (); + cell = gtk_cell_renderer_text_new (); + gtk_tree_view_column_set_expand (column, true); + gtk_tree_view_column_pack_start (column, cell, false); + gtk_tree_view_column_set_attributes (column, cell, "text", i, nullptr); + gtk_tree_view_append_column (GTK_TREE_VIEW(m_pFilterView), column); + } + + gtk_container_add (GTK_CONTAINER (scrolled_window), m_pFilterView); + gtk_widget_show (m_pFilterView); + + gtk_file_chooser_set_extra_widget( GTK_FILE_CHOOSER( m_pDialog ), m_pVBox ); + + m_pPreview = gtk_image_new(); + gtk_file_chooser_set_preview_widget( GTK_FILE_CHOOSER( m_pDialog ), m_pPreview ); + + g_signal_connect( G_OBJECT( m_pToggles[PREVIEW] ), "toggled", + G_CALLBACK( preview_toggled_cb ), this ); + g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW(m_pFilterView)), "changed", + G_CALLBACK ( type_changed_cb ), this); + g_signal_connect( G_OBJECT( m_pDialog ), "notify::filter", + G_CALLBACK( filter_changed_cb ), this); + g_signal_connect( G_OBJECT( m_pFilterExpander ), "activate", + G_CALLBACK( expander_changed_cb ), this); + g_signal_connect (G_OBJECT( m_pDialog ), "map", + G_CALLBACK (dialog_mapped_cb), this); + + gtk_widget_show( m_pVBox ); + + PangoLayout *layout = gtk_widget_create_pango_layout (m_pFilterView, nullptr); + guint ypad; + PangoRectangle row_height; + pango_layout_set_markup (layout, "All Files", -1); + pango_layout_get_pixel_extents (layout, nullptr, &row_height); + g_object_unref (layout); + + g_object_get (cell, "ypad", &ypad, nullptr); + guint height = (row_height.height + 2*ypad) * 5; + gtk_widget_set_size_request (m_pFilterView, -1, height); + gtk_widget_set_size_request (m_pPreview, 1, height); + + gtk_file_chooser_set_preview_widget_active( GTK_FILE_CHOOSER( m_pDialog ), true); +} + +// XFilePickerNotifier + +void SAL_CALL SalGtkFilePicker::addFilePickerListener( const uno::Reference<XFilePickerListener>& xListener ) +{ + SolarMutexGuard g; + + OSL_ENSURE(!m_xListener.is(), + "SalGtkFilePicker only talks with one listener at a time..."); + m_xListener = xListener; +} + +void SAL_CALL SalGtkFilePicker::removeFilePickerListener( const uno::Reference<XFilePickerListener>& ) +{ + SolarMutexGuard g; + + m_xListener.clear(); +} + +// FilePicker Event functions + +void SalGtkFilePicker::impl_fileSelectionChanged( const FilePickerEvent& aEvent ) +{ + if (m_xListener.is()) m_xListener->fileSelectionChanged( aEvent ); +} + +void SalGtkFilePicker::impl_directoryChanged( const FilePickerEvent& aEvent ) +{ + if (m_xListener.is()) m_xListener->directoryChanged( aEvent ); +} + +void SalGtkFilePicker::impl_controlStateChanged( const FilePickerEvent& aEvent ) +{ + if (m_xListener.is()) m_xListener->controlStateChanged( aEvent ); +} + +struct FilterEntry +{ +protected: + OUString m_sTitle; + OUString m_sFilter; + + css::uno::Sequence< css::beans::StringPair > m_aSubFilters; + +public: + FilterEntry( const OUString& _rTitle, const OUString& _rFilter ) + :m_sTitle( _rTitle ) + ,m_sFilter( _rFilter ) + { + } + + const OUString& getTitle() const { return m_sTitle; } + const OUString& getFilter() const { return m_sFilter; } + + /// determines if the filter has sub filter (i.e., the filter is a filter group in real) + bool hasSubFilters( ) const; + + /** retrieves the filters belonging to the entry + */ + void getSubFilters( css::uno::Sequence< css::beans::StringPair >& _rSubFilterList ); + + // helpers for iterating the sub filters + const css::beans::StringPair* beginSubFilters() const { return m_aSubFilters.begin(); } + const css::beans::StringPair* endSubFilters() const { return m_aSubFilters.end(); } +}; + +bool FilterEntry::hasSubFilters() const +{ + return m_aSubFilters.hasElements(); +} + +void FilterEntry::getSubFilters( css::uno::Sequence< css::beans::StringPair >& _rSubFilterList ) +{ + _rSubFilterList = m_aSubFilters; +} + +static bool +isFilterString( const OUString &rFilterString, const char *pMatch ) +{ + sal_Int32 nIndex = 0; + OUString aToken; + bool bIsFilter = true; + + OUString aMatch(OUString::createFromAscii(pMatch)); + + do + { + aToken = rFilterString.getToken( 0, ';', nIndex ); + if( !aToken.match( aMatch ) ) + { + bIsFilter = false; + break; + } + } + while( nIndex >= 0 ); + + return bIsFilter; +} + +static OUString +shrinkFilterName( const OUString &rFilterName, bool bAllowNoStar = false ) +{ + int i; + int nBracketLen = -1; + int nBracketEnd = -1; + const sal_Unicode *pStr = rFilterName.getStr(); + OUString aRealName = rFilterName; + + for( i = aRealName.getLength() - 1; i > 0; i-- ) + { + if( pStr[i] == ')' ) + nBracketEnd = i; + else if( pStr[i] == '(' ) + { + nBracketLen = nBracketEnd - i; + if( nBracketEnd <= 0 ) + continue; + if( isFilterString( rFilterName.copy( i + 1, nBracketLen - 1 ), "*." ) ) + aRealName = aRealName.replaceAt( i, nBracketLen + 1, OUString() ); + else if (bAllowNoStar) + { + if( isFilterString( rFilterName.copy( i + 1, nBracketLen - 1 ), ".") ) + aRealName = aRealName.replaceAt( i, nBracketLen + 1, OUString() ); + } + } + } + + return aRealName; +} + +static void +dialog_remove_buttons(GtkWidget *pActionArea) +{ + GtkContainer *pContainer = GTK_CONTAINER( pActionArea ); + + g_return_if_fail( pContainer != nullptr ); + + GList *pChildren = gtk_container_get_children( pContainer ); + + for( GList *p = pChildren; p; p = p->next ) + { + GtkWidget *pWidget = GTK_WIDGET( p->data ); + if ( GTK_IS_BUTTON( pWidget ) ) + gtk_widget_destroy( pWidget ); + } + + g_list_free( pChildren ); +} + +static void +dialog_remove_buttons( GtkDialog *pDialog ) +{ + g_return_if_fail( GTK_IS_DIALOG( pDialog ) ); + + GtkWidget *pHeaderBar = gtk_dialog_get_header_bar(pDialog); + if( pHeaderBar != nullptr ) + dialog_remove_buttons( pHeaderBar ); + else + dialog_remove_buttons(gtk_dialog_get_action_area(pDialog)); +} + +namespace { + + struct FilterTitleMatch + { + protected: + const OUString& rTitle; + + public: + explicit FilterTitleMatch( const OUString& _rTitle ) : rTitle( _rTitle ) { } + + bool operator () ( const FilterEntry& _rEntry ) + { + bool bMatch; + if( !_rEntry.hasSubFilters() ) + // a real filter + bMatch = (_rEntry.getTitle() == rTitle) + || (shrinkFilterName(_rEntry.getTitle()) == rTitle); + else + // a filter group -> search the sub filters + bMatch = + ::std::any_of( + _rEntry.beginSubFilters(), + _rEntry.endSubFilters(), + *this + ); + + return bMatch; + } + bool operator () ( const css::beans::StringPair& _rEntry ) + { + OUString aShrunkName = shrinkFilterName( _rEntry.First ); + return aShrunkName == rTitle; + } + }; +} + +bool SalGtkFilePicker::FilterNameExists( const OUString& rTitle ) +{ + bool bRet = false; + + if( m_pFilterVector ) + bRet = + ::std::any_of( + m_pFilterVector->begin(), + m_pFilterVector->end(), + FilterTitleMatch( rTitle ) + ); + + return bRet; +} + +bool SalGtkFilePicker::FilterNameExists( const css::uno::Sequence< css::beans::StringPair >& _rGroupedFilters ) +{ + bool bRet = false; + + if( m_pFilterVector ) + { + bRet = std::any_of(_rGroupedFilters.begin(), _rGroupedFilters.end(), + [&](const css::beans::StringPair& rFilter) { + return ::std::any_of( m_pFilterVector->begin(), m_pFilterVector->end(), FilterTitleMatch( rFilter.First ) ); }); + } + + return bRet; +} + +void SalGtkFilePicker::ensureFilterVector( const OUString& _rInitialCurrentFilter ) +{ + if( !m_pFilterVector ) + { + m_pFilterVector.reset( new std::vector<FilterEntry> ); + + // set the first filter to the current filter + if ( m_aCurrentFilter.isEmpty() ) + m_aCurrentFilter = _rInitialCurrentFilter; + } +} + +void SAL_CALL SalGtkFilePicker::appendFilter( const OUString& aTitle, const OUString& aFilter ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + if( FilterNameExists( aTitle ) ) + throw IllegalArgumentException(); + + // ensure that we have a filter list + ensureFilterVector( aTitle ); + + // append the filter + m_pFilterVector->insert( m_pFilterVector->end(), FilterEntry( aTitle, aFilter ) ); +} + +void SAL_CALL SalGtkFilePicker::setCurrentFilter( const OUString& aTitle ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + if( aTitle != m_aCurrentFilter ) + { + m_aCurrentFilter = aTitle; + SetCurFilter( m_aCurrentFilter ); + } + + // TODO m_pImpl->setCurrentFilter( aTitle ); +} + +void SalGtkFilePicker::updateCurrentFilterFromName(const gchar* filtername) +{ + OUString aFilterName(filtername, strlen(filtername), RTL_TEXTENCODING_UTF8); + if (m_pFilterVector) + { + for (auto const& filter : *m_pFilterVector) + { + if (aFilterName == shrinkFilterName(filter.getTitle())) + { + m_aCurrentFilter = filter.getTitle(); + break; + } + } + } +} + +void SalGtkFilePicker::UpdateFilterfromUI() +{ + // Update the filtername from the users selection if they have had a chance to do so. + // If the user explicitly sets a type then use that, if not then take the implicit type + // from the filter of the files glob on which he is currently searching + if (!mnHID_FolderChange || !mnHID_SelectionChange) + return; + + GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(m_pFilterView)); + GtkTreeIter iter; + GtkTreeModel *model; + if (gtk_tree_selection_get_selected (selection, &model, &iter)) + { + gchar *title; + gtk_tree_model_get (model, &iter, 2, &title, -1); + updateCurrentFilterFromName(title); + g_free (title); + } + else if( GtkFileFilter *filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_pDialog))) + { + if (m_pPseudoFilter != filter) + updateCurrentFilterFromName(gtk_file_filter_get_name( filter )); + else + updateCurrentFilterFromName(OUStringToOString( m_aInitialFilter, RTL_TEXTENCODING_UTF8 ).getStr()); + } +} + +OUString SAL_CALL SalGtkFilePicker::getCurrentFilter() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + UpdateFilterfromUI(); + + return m_aCurrentFilter; +} + +// XFilterGroupManager functions + +void SAL_CALL SalGtkFilePicker::appendFilterGroup( const OUString& /*sGroupTitle*/, const uno::Sequence<beans::StringPair>& aFilters ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + // TODO m_pImpl->appendFilterGroup( sGroupTitle, aFilters ); + // check the names + if( FilterNameExists( aFilters ) ) + // TODO: a more precise exception message + throw IllegalArgumentException(); + + // ensure that we have a filter list + OUString sInitialCurrentFilter; + if( aFilters.hasElements() ) + sInitialCurrentFilter = aFilters[0].First; + + ensureFilterVector( sInitialCurrentFilter ); + + // append the filter + for( const auto& rSubFilter : aFilters ) + m_pFilterVector->insert( m_pFilterVector->end(), FilterEntry( rSubFilter.First, rSubFilter.Second ) ); + +} + +// XFilePicker functions + +void SAL_CALL SalGtkFilePicker::setMultiSelectionMode( sal_Bool bMode ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER(m_pDialog), bMode ); +} + +void SAL_CALL SalGtkFilePicker::setDefaultName( const OUString& aName ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + OString aStr = OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ); + GtkFileChooserAction eAction = gtk_file_chooser_get_action( GTK_FILE_CHOOSER( m_pDialog ) ); + + // set_current_name launches a Gtk critical error if called for other than save + if( GTK_FILE_CHOOSER_ACTION_SAVE == eAction ) + gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER( m_pDialog ), aStr.getStr() ); +} + +void SAL_CALL SalGtkFilePicker::setDisplayDirectory( const OUString& rDirectory ) +{ + SolarMutexGuard g; + + implsetDisplayDirectory(rDirectory); +} + +OUString SAL_CALL SalGtkFilePicker::getDisplayDirectory() +{ + SolarMutexGuard g; + + return implgetDisplayDirectory(); +} + +uno::Sequence<OUString> SAL_CALL SalGtkFilePicker::getFiles() +{ + // no member access => no mutex needed + + uno::Sequence< OUString > aFiles = getSelectedFiles(); + /* + The previous multiselection API design was completely broken + and unimplementable for some heterogeneous pseudo-URIs eg. search: + Thus crop unconditionally to a single selection. + */ + aFiles.realloc (1); + return aFiles; +} + +namespace +{ + +bool lcl_matchFilter( const OUString& rFilter, const OUString& rExt ) +{ + const sal_Unicode cSep {';'}; + sal_Int32 nIdx {0}; + + for (;;) + { + const sal_Int32 nBegin = rFilter.indexOf(rExt, nIdx); + + if (nBegin<0) // not found + break; + + // Let nIdx point to end of matched string, useful in order to + // check string boundaries and also for a possible next iteration + nIdx = nBegin + rExt.getLength(); + + // Check if the found occurrence is an exact match: right side + if (nIdx<rFilter.getLength() && rFilter[nIdx]!=cSep) + continue; + + // Check if the found occurrence is an exact match: left side + if (nBegin>0 && rFilter[nBegin-1]!=cSep) + continue; + + return true; + } + + return false; +} + +} + +uno::Sequence<OUString> SAL_CALL SalGtkFilePicker::getSelectedFiles() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + GSList* pPathList = gtk_file_chooser_get_uris( GTK_FILE_CHOOSER(m_pDialog) ); + + int nCount = g_slist_length( pPathList ); + int nIndex = 0; + + // get the current action setting + GtkFileChooserAction eAction = gtk_file_chooser_get_action( + GTK_FILE_CHOOSER( m_pDialog )); + + uno::Sequence< OUString > aSelectedFiles(nCount); + + // Convert to OOo + for( GSList *pElem = pPathList; pElem; pElem = pElem->next) + { + gchar *pURI = static_cast<gchar*>(pElem->data); + aSelectedFiles[ nIndex ] = uritounicode(pURI); + + if( GTK_FILE_CHOOSER_ACTION_SAVE == eAction ) + { + OUString sFilterName; + sal_Int32 nTokenIndex = 0; + bool bExtensionTypedIn = false; + + GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(m_pFilterView)); + GtkTreeIter iter; + GtkTreeModel *model; + if (gtk_tree_selection_get_selected (selection, &model, &iter)) + { + gchar *title = nullptr; + gtk_tree_model_get (model, &iter, 2, &title, -1); + if (title) + sFilterName = OUString( title, strlen( title ), RTL_TEXTENCODING_UTF8 ); + else + sFilterName = OUString(); + g_free (title); + } + else + { + if( aSelectedFiles[nIndex].indexOf('.') > 0 ) + { + OUString sExtension; + nTokenIndex = 0; + do + sExtension = aSelectedFiles[nIndex].getToken( 0, '.', nTokenIndex ); + while( nTokenIndex >= 0 ); + + if( sExtension.getLength() >= 3 ) // 3 = typical/minimum extension length + { + OUString aNewFilter; + OUString aOldFilter = getCurrentFilter(); + bool bChangeFilter = true; + if ( m_pFilterVector) + for (auto const& filter : *m_pFilterVector) + { + if( lcl_matchFilter( filter.getFilter(), "*." + sExtension ) ) + { + if( aNewFilter.isEmpty() ) + aNewFilter = filter.getTitle(); + + if( aOldFilter == filter.getTitle() ) + bChangeFilter = false; + + bExtensionTypedIn = true; + } + } + if( bChangeFilter && bExtensionTypedIn ) + { + gchar* pCurrentName = gtk_file_chooser_get_current_name(GTK_FILE_CHOOSER(m_pDialog)); + setCurrentFilter( aNewFilter ); + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_pDialog), pCurrentName); + g_free(pCurrentName); + } + } + } + + GtkFileFilter *filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_pDialog)); + if (m_pPseudoFilter != filter) + { + const gchar* filtername = filter ? gtk_file_filter_get_name(filter) : nullptr; + if (filtername) + sFilterName = OUString(filtername, strlen( filtername ), RTL_TEXTENCODING_UTF8); + else + sFilterName.clear(); + } + else + sFilterName = m_aInitialFilter; + } + + if (m_pFilterVector) + { + auto aVectorIter = ::std::find_if( + m_pFilterVector->begin(), m_pFilterVector->end(), FilterTitleMatch(sFilterName) ); + + OUString aFilter; + if (aVectorIter != m_pFilterVector->end()) + aFilter = aVectorIter->getFilter(); + + nTokenIndex = 0; + OUString sToken; + do + { + sToken = aFilter.getToken( 0, '.', nTokenIndex ); + + if ( sToken.lastIndexOf( ';' ) != -1 ) + { + sToken = sToken.getToken(0, ';'); + break; + } + } + while( nTokenIndex >= 0 ); + + if( !bExtensionTypedIn && ( sToken != "*" ) ) + { + //if the filename does not already have the auto extension, stick it on + OUString sExtension = "." + sToken; + OUString &rBase = aSelectedFiles[nIndex]; + sal_Int32 nExtensionIdx = rBase.getLength() - sExtension.getLength(); + SAL_INFO( + "vcl.gtk", + "idx are " << rBase.lastIndexOf(sExtension) << " " + << nExtensionIdx); + + if( rBase.lastIndexOf( sExtension ) != nExtensionIdx ) + rBase += sExtension; + } + } + + } + + nIndex++; + g_free( pURI ); + } + + g_slist_free( pPathList ); + + return aSelectedFiles; +} + +// XExecutableDialog functions + +void SAL_CALL SalGtkFilePicker::setTitle( const OUString& rTitle ) +{ + SolarMutexGuard g; + + implsetTitle(rTitle); +} + +sal_Int16 SAL_CALL SalGtkFilePicker::execute() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + sal_Int16 retVal = 0; + + SetFilters(); + + // tdf#84431 - set the filter after the corresponding widget is created + if ( !m_aCurrentFilter.isEmpty() ) + SetCurFilter(m_aCurrentFilter); + + mnHID_FolderChange = + g_signal_connect( GTK_FILE_CHOOSER( m_pDialog ), "current-folder-changed", + G_CALLBACK( folder_changed_cb ), static_cast<gpointer>(this) ); + + mnHID_SelectionChange = + g_signal_connect( GTK_FILE_CHOOSER( m_pDialog ), "selection-changed", + G_CALLBACK( selection_changed_cb ), static_cast<gpointer>(this) ); + + int btn = GTK_RESPONSE_NO; + + uno::Reference< awt::XExtendedToolkit > xToolkit( + awt::Toolkit::create(m_xContext), + UNO_QUERY_THROW ); + + uno::Reference< frame::XDesktop > xDesktop( + frame::Desktop::create(m_xContext), + UNO_QUERY_THROW ); + + GtkWindow *pParent = GTK_WINDOW(m_pParentWidget); + if (!pParent) + { + SAL_WARN( "vcl.gtk", "no parent widget set"); + pParent = RunDialog::GetTransientFor(); + } + if (pParent) + gtk_window_set_transient_for(GTK_WINDOW(m_pDialog), pParent); + RunDialog* pRunDialog = new RunDialog(m_pDialog, xToolkit, xDesktop); + uno::Reference < awt::XTopWindowListener > xLifeCycle(pRunDialog); + while( GTK_RESPONSE_NO == btn ) + { + btn = GTK_RESPONSE_YES; // we don't want to repeat unless user clicks NO for file save. + + gint nStatus = pRunDialog->run(); + switch( nStatus ) + { + case GTK_RESPONSE_ACCEPT: + if( GTK_FILE_CHOOSER_ACTION_SAVE == gtk_file_chooser_get_action( GTK_FILE_CHOOSER( m_pDialog ) ) ) + { + Sequence < OUString > aPathSeq = getFiles(); + if( aPathSeq.getLength() == 1 ) + { + OString sFileName = unicodetouri( aPathSeq[0] ); + gchar *gFileName = g_filename_from_uri ( sFileName.getStr(), nullptr, nullptr ); + if( g_file_test( gFileName, G_FILE_TEST_IS_REGULAR ) ) + { + INetURLObject aFileObj( OStringToOUString(sFileName, RTL_TEXTENCODING_UTF8) ); + + OString baseName( + OUStringToOString( + aFileObj.getName( + INetURLObject::LAST_SEGMENT, + true, + INetURLObject::DecodeMechanism::WithCharset + ), + RTL_TEXTENCODING_UTF8 + ) + ); + OString aMsg( + OUStringToOString( + getResString( FILE_PICKER_OVERWRITE_PRIMARY ), + RTL_TEXTENCODING_UTF8 + ) + ); + OString toReplace("$filename$"); + + aMsg = aMsg.replaceAt( + aMsg.indexOf( toReplace ), + toReplace.getLength(), + baseName + ); + + GtkWidget *dlg = gtk_message_dialog_new( nullptr, + GTK_DIALOG_MODAL, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "%s", + aMsg.getStr() + ); + + sal_Int32 nSegmentCount = aFileObj.getSegmentCount(); + if (nSegmentCount >= 2) + { + OString dirName( + OUStringToOString( + aFileObj.getName( + nSegmentCount-2, + true, + INetURLObject::DecodeMechanism::WithCharset + ), + RTL_TEXTENCODING_UTF8 + ) + ); + + aMsg = + OUStringToOString( + getResString( FILE_PICKER_OVERWRITE_SECONDARY ), + RTL_TEXTENCODING_UTF8 + ); + + toReplace = "$dirname$"; + + aMsg = aMsg.replaceAt( + aMsg.indexOf( toReplace ), + toReplace.getLength(), + dirName + ); + + gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( dlg ), "%s", aMsg.getStr() ); + } + + gtk_window_set_title( GTK_WINDOW( dlg ), + OUStringToOString(getResString(FILE_PICKER_TITLE_SAVE ), + RTL_TEXTENCODING_UTF8 ).getStr() ); + gtk_window_set_transient_for(GTK_WINDOW(dlg), GTK_WINDOW(m_pDialog)); + RunDialog* pAnotherDialog = new RunDialog(dlg, xToolkit, xDesktop); + uno::Reference < awt::XTopWindowListener > xAnotherLifeCycle(pAnotherDialog); + btn = pAnotherDialog->run(); + + gtk_widget_destroy( dlg ); + } + g_free (gFileName); + + if( btn == GTK_RESPONSE_YES ) + retVal = ExecutableDialogResults::OK; + } + } + else + retVal = ExecutableDialogResults::OK; + break; + + case GTK_RESPONSE_CANCEL: + retVal = ExecutableDialogResults::CANCEL; + break; + + case 1: //PLAY + { + FilePickerEvent evt; + evt.ElementId = PUSHBUTTON_PLAY; + impl_controlStateChanged( evt ); + btn = GTK_RESPONSE_NO; + } + break; + + default: + retVal = 0; + break; + } + } + gtk_widget_hide(m_pDialog); + + if (mnHID_FolderChange) + g_signal_handler_disconnect(GTK_FILE_CHOOSER( m_pDialog ), mnHID_FolderChange); + if (mnHID_SelectionChange) + g_signal_handler_disconnect(GTK_FILE_CHOOSER( m_pDialog ), mnHID_SelectionChange); + + return retVal; +} + +// cf. offapi/com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.idl +GtkWidget *SalGtkFilePicker::getWidget( sal_Int16 nControlId, GType *pType ) +{ + GType tType = GTK_TYPE_TOGGLE_BUTTON; //prevent warning by initializing + GtkWidget *pWidget = nullptr; + +#define MAP_TOGGLE( elem ) \ + case ExtendedFilePickerElementIds::CHECKBOX_##elem: \ + pWidget = m_pToggles[elem]; tType = GTK_TYPE_TOGGLE_BUTTON; \ + break +#define MAP_BUTTON( elem ) \ + case ExtendedFilePickerElementIds::PUSHBUTTON_##elem: \ + pWidget = m_pButtons[elem]; tType = GTK_TYPE_BUTTON; \ + break +#define MAP_LIST( elem ) \ + case ExtendedFilePickerElementIds::LISTBOX_##elem: \ + pWidget = m_pLists[elem]; tType = GTK_TYPE_COMBO_BOX; \ + break +#define MAP_LIST_LABEL( elem ) \ + case ExtendedFilePickerElementIds::LISTBOX_##elem##_LABEL: \ + pWidget = m_pListLabels[elem]; tType = GTK_TYPE_LABEL; \ + break + + switch( nControlId ) + { + MAP_TOGGLE( AUTOEXTENSION ); + MAP_TOGGLE( PASSWORD ); + MAP_TOGGLE( GPGENCRYPTION ); + MAP_TOGGLE( FILTEROPTIONS ); + MAP_TOGGLE( READONLY ); + MAP_TOGGLE( LINK ); + MAP_TOGGLE( PREVIEW ); + MAP_TOGGLE( SELECTION ); + MAP_BUTTON( PLAY ); + MAP_LIST( VERSION ); + MAP_LIST( TEMPLATE ); + MAP_LIST( IMAGE_TEMPLATE ); + MAP_LIST( IMAGE_ANCHOR ); + MAP_LIST_LABEL( VERSION ); + MAP_LIST_LABEL( TEMPLATE ); + MAP_LIST_LABEL( IMAGE_TEMPLATE ); + MAP_LIST_LABEL( IMAGE_ANCHOR ); + default: + SAL_WARN( "vcl.gtk", "Handle unknown control " << nControlId); + break; + } +#undef MAP + + if( pType ) + *pType = tType; + return pWidget; +} + +// XFilePickerControlAccess functions + +static void HackWidthToFirst(GtkComboBox *pWidget) +{ + GtkRequisition requisition; + gtk_widget_size_request(GTK_WIDGET(pWidget), &requisition); + gtk_widget_set_size_request(GTK_WIDGET(pWidget), requisition.width, -1); +} + +static void ComboBoxAppendText(GtkComboBox *pCombo, const OUString &rStr) +{ + GtkTreeIter aIter; + GtkListStore *pStore = GTK_LIST_STORE(gtk_combo_box_get_model(pCombo)); + OString aStr = OUStringToOString(rStr, RTL_TEXTENCODING_UTF8); + gtk_list_store_append(pStore, &aIter); + gtk_list_store_set(pStore, &aIter, 0, aStr.getStr(), -1); +} + +void SalGtkFilePicker::HandleSetListValue(GtkComboBox *pWidget, sal_Int16 nControlAction, const uno::Any& rValue) +{ + switch (nControlAction) + { + case ControlActions::ADD_ITEM: + { + OUString sItem; + rValue >>= sItem; + ComboBoxAppendText(pWidget, sItem); + if (!bVersionWidthUnset) + { + HackWidthToFirst(pWidget); + bVersionWidthUnset = true; + } + } + break; + case ControlActions::ADD_ITEMS: + { + Sequence< OUString > aStringList; + rValue >>= aStringList; + for (const auto& rString : std::as_const(aStringList)) + { + ComboBoxAppendText(pWidget, rString); + if (!bVersionWidthUnset) + { + HackWidthToFirst(pWidget); + bVersionWidthUnset = true; + } + } + } + break; + case ControlActions::DELETE_ITEM: + { + sal_Int32 nPos=0; + rValue >>= nPos; + + GtkTreeIter aIter; + GtkListStore *pStore = GTK_LIST_STORE( + gtk_combo_box_get_model(GTK_COMBO_BOX(pWidget))); + if(gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(pStore), &aIter, nullptr, nPos)) + gtk_list_store_remove(pStore, &aIter); + } + break; + case ControlActions::DELETE_ITEMS: + { + gtk_combo_box_set_active(pWidget, -1); + GtkListStore *pStore = GTK_LIST_STORE( + gtk_combo_box_get_model(GTK_COMBO_BOX(pWidget))); + gtk_list_store_clear(pStore); + } + break; + case ControlActions::SET_SELECT_ITEM: + { + sal_Int32 nPos=0; + rValue >>= nPos; + gtk_combo_box_set_active(pWidget, nPos); + } + break; + default: + SAL_WARN( "vcl.gtk", "undocumented/unimplemented ControlAction for a list " << nControlAction); + break; + } + + //I think its best to make it insensitive unless there is the chance to + //actually select something from the list. + gint nItems = gtk_tree_model_iter_n_children( + gtk_combo_box_get_model(pWidget), nullptr); + gtk_widget_set_sensitive(GTK_WIDGET(pWidget), nItems > 1); +} + +uno::Any SalGtkFilePicker::HandleGetListValue(GtkComboBox *pWidget, sal_Int16 nControlAction) +{ + uno::Any aAny; + switch (nControlAction) + { + case ControlActions::GET_ITEMS: + { + Sequence< OUString > aItemList; + + GtkTreeModel *pTree = gtk_combo_box_get_model(pWidget); + GtkTreeIter iter; + if (gtk_tree_model_get_iter_first(pTree, &iter)) + { + sal_Int32 nSize = gtk_tree_model_iter_n_children( + pTree, nullptr); + + aItemList.realloc(nSize); + for (sal_Int32 i=0; i < nSize; ++i) + { + gchar *item; + gtk_tree_model_get(gtk_combo_box_get_model(pWidget), + &iter, 0, &item, -1); + aItemList[i] = OUString(item, strlen(item), RTL_TEXTENCODING_UTF8); + g_free(item); + (void)gtk_tree_model_iter_next(pTree, &iter); + } + } + aAny <<= aItemList; + } + break; + case ControlActions::GET_SELECTED_ITEM: + { + GtkTreeIter iter; + if (gtk_combo_box_get_active_iter(pWidget, &iter)) + { + gchar *item; + gtk_tree_model_get(gtk_combo_box_get_model(pWidget), + &iter, 0, &item, -1); + OUString sItem(item, strlen(item), RTL_TEXTENCODING_UTF8); + aAny <<= sItem; + g_free(item); + } + } + break; + case ControlActions::GET_SELECTED_ITEM_INDEX: + { + gint nActive = gtk_combo_box_get_active(pWidget); + aAny <<= static_cast< sal_Int32 >(nActive); + } + break; + default: + SAL_WARN( "vcl.gtk", "undocumented/unimplemented ControlAction for a list " << nControlAction); + break; + } + return aAny; +} + +void SAL_CALL SalGtkFilePicker::setValue( sal_Int16 nControlId, sal_Int16 nControlAction, const uno::Any& rValue ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + GType tType; + GtkWidget *pWidget; + + if( !( pWidget = getWidget( nControlId, &tType ) ) ) + SAL_WARN( "vcl.gtk", "enable unknown control " << nControlId); + else if( tType == GTK_TYPE_TOGGLE_BUTTON ) + { + bool bChecked = false; + rValue >>= bChecked; + gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( pWidget ), bChecked ); + } + else if( tType == GTK_TYPE_COMBO_BOX ) + HandleSetListValue(GTK_COMBO_BOX(pWidget), nControlAction, rValue); + else + { + SAL_WARN( "vcl.gtk", "Can't set value on button / list " << nControlId << " " << nControlAction ); + } +} + +uno::Any SAL_CALL SalGtkFilePicker::getValue( sal_Int16 nControlId, sal_Int16 nControlAction ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + uno::Any aRetval; + + GType tType; + GtkWidget *pWidget; + + if( !( pWidget = getWidget( nControlId, &tType ) ) ) + SAL_WARN( "vcl.gtk", "enable unknown control " << nControlId); + else if( tType == GTK_TYPE_TOGGLE_BUTTON ) + aRetval <<= bool( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( pWidget ) ) ); + else if( tType == GTK_TYPE_COMBO_BOX ) + aRetval = HandleGetListValue(GTK_COMBO_BOX(pWidget), nControlAction); + else + SAL_WARN( "vcl.gtk", "Can't get value on button / list " << nControlId << " " << nControlAction ); + + return aRetval; +} + +void SAL_CALL SalGtkFilePicker::enableControl( sal_Int16 nControlId, sal_Bool bEnable ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + GtkWidget *pWidget; + + if ( ( pWidget = getWidget( nControlId ) ) ) + { + if( bEnable ) + { + gtk_widget_set_sensitive( pWidget, true ); + } + else + { + gtk_widget_set_sensitive( pWidget, false ); + } + } + else + SAL_WARN( "vcl.gtk", "enable unknown control " << nControlId ); +} + +void SAL_CALL SalGtkFilePicker::setLabel( sal_Int16 nControlId, const OUString& rLabel ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + GType tType; + GtkWidget *pWidget; + + if( !( pWidget = getWidget( nControlId, &tType ) ) ) + { + SAL_WARN( "vcl.gtk", "Set label on unknown control " << nControlId); + return; + } + + OString aTxt = OUStringToOString( rLabel.replace('~', '_'), RTL_TEXTENCODING_UTF8 ); + if (nControlId == ExtendedFilePickerElementIds::PUSHBUTTON_PLAY) + { +#ifdef GTK_STOCK_MEDIA_PLAY + if (msPlayLabel.isEmpty()) + msPlayLabel = rLabel; + if (msPlayLabel == rLabel) + gtk_button_set_label(GTK_BUTTON(pWidget), GTK_STOCK_MEDIA_PLAY); + else + gtk_button_set_label(GTK_BUTTON(pWidget), GTK_STOCK_MEDIA_STOP); +#else + gtk_button_set_label(GTK_BUTTON(pWidget), aTxt.getStr()); +#endif + } + else if( tType == GTK_TYPE_TOGGLE_BUTTON || tType == GTK_TYPE_BUTTON || tType == GTK_TYPE_LABEL ) + g_object_set( pWidget, "label", aTxt.getStr(), + "use_underline", true, nullptr ); + else + SAL_WARN( "vcl.gtk", "Can't set label on list"); +} + +OUString SAL_CALL SalGtkFilePicker::getLabel( sal_Int16 nControlId ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + GType tType; + OString aTxt; + GtkWidget *pWidget; + + if( !( pWidget = getWidget( nControlId, &tType ) ) ) + SAL_WARN( "vcl.gtk", "Get label on unknown control " << nControlId); + else if( tType == GTK_TYPE_TOGGLE_BUTTON || tType == GTK_TYPE_BUTTON || tType == GTK_TYPE_LABEL ) + aTxt = gtk_button_get_label( GTK_BUTTON( pWidget ) ); + else + SAL_WARN( "vcl.gtk", "Can't get label on list"); + + return OStringToOUString( aTxt, RTL_TEXTENCODING_UTF8 ); +} + +// XFilePreview functions + +uno::Sequence<sal_Int16> SAL_CALL SalGtkFilePicker::getSupportedImageFormats() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + // TODO return m_pImpl->getSupportedImageFormats(); + return uno::Sequence<sal_Int16>(); +} + +sal_Int32 SAL_CALL SalGtkFilePicker::getTargetColorDepth() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + // TODO return m_pImpl->getTargetColorDepth(); + return 0; +} + +sal_Int32 SAL_CALL SalGtkFilePicker::getAvailableWidth() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + return g_PreviewImageWidth; +} + +sal_Int32 SAL_CALL SalGtkFilePicker::getAvailableHeight() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + return g_PreviewImageHeight; +} + +void SAL_CALL SalGtkFilePicker::setImage( sal_Int16 /*aImageFormat*/, const uno::Any& /*aImage*/ ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + // TODO m_pImpl->setImage( aImageFormat, aImage ); +} + +void SalGtkFilePicker::implChangeType( GtkTreeSelection *selection ) +{ + OUString aLabel = getResString( FILE_PICKER_FILE_TYPE ); + + GtkTreeIter iter; + GtkTreeModel *model; + if (gtk_tree_selection_get_selected (selection, &model, &iter)) + { + gchar *title; + gtk_tree_model_get (model, &iter, 2, &title, -1); + aLabel += ": "; + aLabel += OUString( title, strlen(title), RTL_TEXTENCODING_UTF8 ); + g_free (title); + } + gtk_expander_set_label (GTK_EXPANDER (m_pFilterExpander), + OUStringToOString( aLabel, RTL_TEXTENCODING_UTF8 ).getStr()); + FilePickerEvent evt; + evt.ElementId = LISTBOX_FILTER; + impl_controlStateChanged( evt ); +} + +void SalGtkFilePicker::type_changed_cb( GtkTreeSelection *selection, SalGtkFilePicker *pobjFP ) +{ + pobjFP->implChangeType(selection); +} + +void SalGtkFilePicker::unselect_type() +{ + gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_pFilterView))); +} + +void SalGtkFilePicker::expander_changed_cb( GtkExpander *expander, SalGtkFilePicker *pobjFP ) +{ + if (gtk_expander_get_expanded(expander)) + pobjFP->unselect_type(); +} + +void SalGtkFilePicker::filter_changed_cb( GtkFileChooser *, GParamSpec *, + SalGtkFilePicker *pobjFP ) +{ + FilePickerEvent evt; + evt.ElementId = LISTBOX_FILTER; + SAL_INFO( "vcl.gtk", "filter_changed, isn't it great " << pobjFP ); + pobjFP->impl_controlStateChanged( evt ); +} + +void SalGtkFilePicker::folder_changed_cb( GtkFileChooser *, SalGtkFilePicker *pobjFP ) +{ + FilePickerEvent evt; + SAL_INFO( "vcl.gtk", "folder_changed, isn't it great " << pobjFP ); + pobjFP->impl_directoryChanged( evt ); +} + +void SalGtkFilePicker::selection_changed_cb( GtkFileChooser *, SalGtkFilePicker *pobjFP ) +{ + FilePickerEvent evt; + SAL_INFO( "vcl.gtk", "selection_changed, isn't it great " << pobjFP ); + pobjFP->impl_fileSelectionChanged( evt ); +} + +void SalGtkFilePicker::update_preview_cb( GtkFileChooser *file_chooser, SalGtkFilePicker* pobjFP ) +{ + gboolean have_preview = false; + + GtkWidget* preview = pobjFP->m_pPreview; + char* filename = gtk_file_chooser_get_preview_filename( file_chooser ); + + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pobjFP->m_pToggles[PREVIEW])) && filename && g_file_test(filename, G_FILE_TEST_IS_REGULAR)) + { + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size( + filename, + g_PreviewImageWidth, + g_PreviewImageHeight, nullptr ); + + have_preview = ( pixbuf != nullptr ); + + gtk_image_set_from_pixbuf( GTK_IMAGE( preview ), pixbuf ); + if( pixbuf ) + g_object_unref( G_OBJECT( pixbuf ) ); + + } + + gtk_file_chooser_set_preview_widget_active( file_chooser, have_preview ); + + if( filename ) + g_free( filename ); +} + +sal_Bool SAL_CALL SalGtkFilePicker::setShowState( sal_Bool bShowState ) +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + // TODO return m_pImpl->setShowState( bShowState ); + if( bool(bShowState) != mbPreviewState ) + { + if( bShowState ) + { + // Show + if( !mHID_Preview ) + { + mHID_Preview = g_signal_connect( + GTK_FILE_CHOOSER( m_pDialog ), "update-preview", + G_CALLBACK( update_preview_cb ), static_cast<gpointer>(this) ); + } + gtk_widget_show( m_pPreview ); + } + else + { + // Hide + gtk_widget_hide( m_pPreview ); + } + + // also emit the signal + g_signal_emit_by_name( G_OBJECT( m_pDialog ), "update-preview" ); + + mbPreviewState = bShowState; + } + return true; +} + +sal_Bool SAL_CALL SalGtkFilePicker::getShowState() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + return mbPreviewState; +} + +// XInitialization + +void SAL_CALL SalGtkFilePicker::initialize( const uno::Sequence<uno::Any>& aArguments ) +{ + // parameter checking + uno::Any aAny; + if( !aArguments.hasElements() ) + throw lang::IllegalArgumentException( + "no arguments", + static_cast<XFilePicker2*>( this ), 1 ); + + aAny = aArguments[0]; + + if( ( aAny.getValueType() != cppu::UnoType<sal_Int16>::get()) && + (aAny.getValueType() != cppu::UnoType<sal_Int8>::get()) ) + throw lang::IllegalArgumentException( + "invalid argument type", + static_cast<XFilePicker2*>( this ), 1 ); + + sal_Int16 templateId = -1; + aAny >>= templateId; + + css::uno::Reference<css::awt::XWindow> xParentWindow; + if (aArguments.getLength() > 1) + { + aArguments[1] >>= xParentWindow; + } + + if (xParentWindow.is()) + { + if (SalGtkXWindow* pGtkXWindow = dynamic_cast<SalGtkXWindow*>(xParentWindow.get())) + m_pParentWidget = pGtkXWindow->getWidget(); + else + { + css::uno::Reference<css::awt::XSystemDependentWindowPeer> xSysDepWin(xParentWindow, css::uno::UNO_QUERY); + if (xSysDepWin.is()) + { + css::uno::Sequence<sal_Int8> aProcessIdent(16); + rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8*>(aProcessIdent.getArray())); + aAny = xSysDepWin->getWindowHandle(aProcessIdent, css::lang::SystemDependent::SYSTEM_XWINDOW); + css::awt::SystemDependentXWindow tmp; + aAny >>= tmp; + m_pParentWidget = GetGtkSalData()->GetGtkDisplay()->findGtkWidgetForNativeHandle(tmp.WindowHandle); + } + } + } + + GtkFileChooserAction eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + const gchar *first_button_text = GTK_STOCK_OPEN; + + SolarMutexGuard g; + + // TODO: extract full semantic from + // svtools/source/filepicker/filepicker.cxx (getWinBits) + switch( templateId ) + { + case FILEOPEN_SIMPLE: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + break; + case FILESAVE_SIMPLE: + eAction = GTK_FILE_CHOOSER_ACTION_SAVE; + first_button_text = GTK_STOCK_SAVE; + break; + case FILESAVE_AUTOEXTENSION_PASSWORD: + eAction = GTK_FILE_CHOOSER_ACTION_SAVE; + first_button_text = GTK_STOCK_SAVE; + mbToggleVisibility[PASSWORD] = true; + mbToggleVisibility[GPGENCRYPTION] = true; + // TODO + break; + case FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS: + eAction = GTK_FILE_CHOOSER_ACTION_SAVE; + first_button_text = GTK_STOCK_SAVE; + mbToggleVisibility[PASSWORD] = true; + mbToggleVisibility[GPGENCRYPTION] = true; + mbToggleVisibility[FILTEROPTIONS] = true; + // TODO + break; + case FILESAVE_AUTOEXTENSION_SELECTION: + eAction = GTK_FILE_CHOOSER_ACTION_SAVE; // SELECT_FOLDER ? + first_button_text = GTK_STOCK_SAVE; + mbToggleVisibility[SELECTION] = true; + // TODO + break; + case FILESAVE_AUTOEXTENSION_TEMPLATE: + eAction = GTK_FILE_CHOOSER_ACTION_SAVE; + first_button_text = GTK_STOCK_SAVE; + mbListVisibility[TEMPLATE] = true; + // TODO + break; + case FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbToggleVisibility[LINK] = true; + mbToggleVisibility[PREVIEW] = true; + mbListVisibility[IMAGE_TEMPLATE] = true; + // TODO + break; + case FILEOPEN_LINK_PREVIEW_IMAGE_ANCHOR: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbToggleVisibility[LINK] = true; + mbToggleVisibility[PREVIEW] = true; + mbListVisibility[IMAGE_ANCHOR] = true; + // TODO + break; + case FILEOPEN_PLAY: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbButtonVisibility[PLAY] = true; + // TODO + break; + case FILEOPEN_LINK_PLAY: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbToggleVisibility[LINK] = true; + mbButtonVisibility[PLAY] = true; + // TODO + break; + case FILEOPEN_READONLY_VERSION: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbToggleVisibility[READONLY] = true; + mbListVisibility[VERSION] = true; + break; + case FILEOPEN_LINK_PREVIEW: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbToggleVisibility[LINK] = true; + mbToggleVisibility[PREVIEW] = true; + // TODO + break; + case FILESAVE_AUTOEXTENSION: + eAction = GTK_FILE_CHOOSER_ACTION_SAVE; + first_button_text = GTK_STOCK_SAVE; + // TODO + break; + case FILEOPEN_PREVIEW: + eAction = GTK_FILE_CHOOSER_ACTION_OPEN; + first_button_text = GTK_STOCK_OPEN; + mbToggleVisibility[PREVIEW] = true; + // TODO + break; + default: + throw lang::IllegalArgumentException( + "Unknown template", + static_cast< XFilePicker2* >( this ), + 1 ); + } + + if( GTK_FILE_CHOOSER_ACTION_SAVE == eAction ) + { + OUString aFilePickerTitle(getResString( FILE_PICKER_TITLE_SAVE )); + gtk_window_set_title ( GTK_WINDOW( m_pDialog ), + OUStringToOString( aFilePickerTitle, RTL_TEXTENCODING_UTF8 ).getStr() ); + } + + gtk_file_chooser_set_action( GTK_FILE_CHOOSER( m_pDialog ), eAction); + dialog_remove_buttons( GTK_DIALOG( m_pDialog ) ); + gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL ); + for( int nTVIndex = 0; nTVIndex < BUTTON_LAST; nTVIndex++ ) + { + if( mbButtonVisibility[nTVIndex] ) + { +#ifdef GTK_STOCK_MEDIA_PLAY + m_pButtons[ nTVIndex ] = gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), GTK_STOCK_MEDIA_PLAY, 1 ); +#else + OString aPlay = OUStringToOString( getResString( PUSHBUTTON_PLAY ), RTL_TEXTENCODING_UTF8 ); + m_pButtons[ nTVIndex ] = gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), aPlay.getStr(), 1 ); +#endif + } + } + gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), first_button_text, GTK_RESPONSE_ACCEPT ); + + gtk_dialog_set_default_response( GTK_DIALOG (m_pDialog), GTK_RESPONSE_ACCEPT ); + + // Setup special flags + for( int nTVIndex = 0; nTVIndex < TOGGLE_LAST; nTVIndex++ ) + { + if( mbToggleVisibility[nTVIndex] ) + gtk_widget_show( m_pToggles[ nTVIndex ] ); + } + + for( int nTVIndex = 0; nTVIndex < LIST_LAST; nTVIndex++ ) + { + if( mbListVisibility[nTVIndex] ) + { + gtk_widget_set_sensitive( m_pLists[ nTVIndex ], false ); + gtk_widget_show( m_pLists[ nTVIndex ] ); + gtk_widget_show( m_pListLabels[ nTVIndex ] ); + gtk_widget_show( m_pAligns[ nTVIndex ] ); + gtk_widget_show( m_pHBoxs[ nTVIndex ] ); + } + } +} + +void SalGtkFilePicker::preview_toggled_cb( GObject *cb, SalGtkFilePicker* pobjFP ) +{ + if( pobjFP->mbToggleVisibility[PREVIEW] ) + pobjFP->setShowState( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cb ) ) ); +} + +// XCancellable + +void SAL_CALL SalGtkFilePicker::cancel() +{ + SolarMutexGuard g; + + OSL_ASSERT( m_pDialog != nullptr ); + + // TODO m_pImpl->cancel(); +} + +// Misc + +void SalGtkFilePicker::SetCurFilter( const OUString& rFilter ) +{ + // Get all the filters already added + GSList *filters = gtk_file_chooser_list_filters ( GTK_FILE_CHOOSER( m_pDialog ) ); + bool bFound = false; + + for( GSList *iter = filters; !bFound && iter; iter = iter->next ) + { + GtkFileFilter* pFilter = static_cast<GtkFileFilter *>( iter->data ); + const gchar * filtername = gtk_file_filter_get_name( pFilter ); + OUString sFilterName( filtername, strlen( filtername ), RTL_TEXTENCODING_UTF8 ); + + OUString aShrunkName = shrinkFilterName( rFilter ); + if( aShrunkName == sFilterName ) + { + SAL_INFO( "vcl.gtk", "actually setting " << filtername ); + gtk_file_chooser_set_filter( GTK_FILE_CHOOSER( m_pDialog ), pFilter ); + bFound = true; + } + } + + g_slist_free( filters ); +} + +extern "C" +{ +static gboolean +case_insensitive_filter (const GtkFileFilterInfo *filter_info, gpointer data) +{ + gboolean bRetval = false; + const char *pFilter = static_cast<const char *>(data); + + g_return_val_if_fail( data != nullptr, false ); + g_return_val_if_fail( filter_info != nullptr, false ); + + if( !filter_info->uri ) + return false; + + const char *pExtn = strrchr( filter_info->uri, '.' ); + if( !pExtn ) + return false; + pExtn++; + + if( !g_ascii_strcasecmp( pFilter, pExtn ) ) + bRetval = true; + + SAL_INFO( "vcl.gtk", "'" << filter_info->uri << "' match extn '" << pExtn << "' vs '" << pFilter << "' yields " << bRetval ); + + return bRetval; +} +} + +GtkFileFilter* SalGtkFilePicker::implAddFilter( const OUString& rFilter, const OUString& rType ) +{ + GtkFileFilter *filter = gtk_file_filter_new(); + + OUString aShrunkName = shrinkFilterName( rFilter ); + OString aFilterName = OUStringToOString( aShrunkName, RTL_TEXTENCODING_UTF8 ); + gtk_file_filter_set_name( filter, aFilterName.getStr() ); + + OUStringBuffer aTokens; + + bool bAllGlob = rType == "*.*" || rType == "*"; + if (bAllGlob) + gtk_file_filter_add_pattern( filter, "*" ); + else + { + sal_Int32 nIndex = 0; + do + { + OUString aToken = rType.getToken( 0, ';', nIndex ); + // Assume all have the "*.<extn>" syntax + sal_Int32 nStarDot = aToken.lastIndexOf( "*." ); + if (nStarDot >= 0) + aToken = aToken.copy( nStarDot + 2 ); + if (!aToken.isEmpty()) + { + if (!aTokens.isEmpty()) + aTokens.append(","); + aTokens.append(aToken); + gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_URI, + case_insensitive_filter, + g_strdup( OUStringToOString(aToken, RTL_TEXTENCODING_UTF8).getStr() ), + g_free ); + + SAL_INFO( "vcl.gtk", "fustering with " << aToken ); + } +#if OSL_DEBUG_LEVEL > 0 + else + { + g_warning( "Duff filter token '%s'\n", + OUStringToOString( + rType.getToken( 0, ';', nIndex ), RTL_TEXTENCODING_UTF8 ).getStr() ); + } +#endif + } + while( nIndex >= 0 ); + } + + gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( m_pDialog ), filter ); + + if (!bAllGlob) + { + GtkTreeIter iter; + gtk_list_store_append (m_pFilterStore, &iter); + gtk_list_store_set (m_pFilterStore, &iter, + 0, OUStringToOString(shrinkFilterName( rFilter, true ), RTL_TEXTENCODING_UTF8).getStr(), + 1, OUStringToOString(aTokens.makeStringAndClear(), RTL_TEXTENCODING_UTF8).getStr(), + 2, aFilterName.getStr(), + 3, OUStringToOString(rType, RTL_TEXTENCODING_UTF8).getStr(), + -1); + } + return filter; +} + +void SalGtkFilePicker::implAddFilterGroup( const OUString& /*_rFilter*/, const Sequence< StringPair >& _rFilters ) +{ + // Gtk+ has no filter group concept I think so ... + // implAddFilter( _rFilter, String() ); + for( const auto& rSubFilter : _rFilters ) + implAddFilter( rSubFilter.First, rSubFilter.Second ); +} + +void SalGtkFilePicker::SetFilters() +{ + if (m_aInitialFilter.isEmpty()) + m_aInitialFilter = m_aCurrentFilter; + + OUString sPseudoFilter; + if( GTK_FILE_CHOOSER_ACTION_SAVE == gtk_file_chooser_get_action( GTK_FILE_CHOOSER( m_pDialog ) ) ) + { + std::set<OUString> aAllFormats; + if( m_pFilterVector ) + { + for (auto & filter : *m_pFilterVector) + { + if( filter.hasSubFilters() ) + { // it's a filter group + css::uno::Sequence< css::beans::StringPair > aSubFilters; + filter.getSubFilters( aSubFilters ); + for( const auto& rSubFilter : std::as_const(aSubFilters) ) + aAllFormats.insert(rSubFilter.Second); + } + else + aAllFormats.insert(filter.getFilter()); + } + } + if (aAllFormats.size() > 1) + { + OUStringBuffer sAllFilter; + for (auto const& format : aAllFormats) + { + if (!sAllFilter.isEmpty()) + sAllFilter.append(";"); + sAllFilter.append(format); + } + sPseudoFilter = getResString(FILE_PICKER_ALLFORMATS); + m_pPseudoFilter = implAddFilter( sPseudoFilter, sAllFilter.makeStringAndClear() ); + } + } + + if( m_pFilterVector ) + { + for (auto & filter : *m_pFilterVector) + { + if( filter.hasSubFilters() ) + { // it's a filter group + + css::uno::Sequence< css::beans::StringPair > aSubFilters; + filter.getSubFilters( aSubFilters ); + + implAddFilterGroup( filter.getTitle(), aSubFilters ); + } + else + { + // it's a single filter + + implAddFilter( filter.getTitle(), filter.getFilter() ); + } + } + } + + // We always hide the expander now and depend on the user using the glob + // list, or type a filename suffix, to select a filter by inference. + gtk_widget_hide(m_pFilterExpander); + + // set the default filter + if (!sPseudoFilter.isEmpty()) + SetCurFilter( sPseudoFilter ); + else if(!m_aCurrentFilter.isEmpty()) + SetCurFilter( m_aCurrentFilter ); + + SAL_INFO( "vcl.gtk", "end setting filters"); +} + +SalGtkFilePicker::~SalGtkFilePicker() +{ + SolarMutexGuard g; + + int i; + + for( i = 0; i < TOGGLE_LAST; i++ ) + gtk_widget_destroy( m_pToggles[i] ); + + for( i = 0; i < LIST_LAST; i++ ) + { + gtk_widget_destroy( m_pListLabels[i] ); + gtk_widget_destroy( m_pAligns[i] ); //m_pAligns[i] owns m_pLists[i] + gtk_widget_destroy( m_pHBoxs[i] ); + } + + m_pFilterVector.reset(); + + gtk_widget_destroy( m_pVBox ); +} + +uno::Reference< ui::dialogs::XFilePicker2 > +GtkInstance::createFilePicker( const css::uno::Reference< css::uno::XComponentContext > &xMSF ) +{ + return uno::Reference< ui::dialogs::XFilePicker2 >( + new SalGtkFilePicker( xMSF ) ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/SalGtkFilePicker.hxx b/vcl/unx/gtk3/fpicker/SalGtkFilePicker.hxx new file mode 100644 index 000000000000..db471d5e5c6c --- /dev/null +++ b/vcl/unx/gtk3/fpicker/SalGtkFilePicker.hxx @@ -0,0 +1,241 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKFILEPICKER_HXX +#define INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKFILEPICKER_HXX + +#include <cppuhelper/compbase.hxx> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp> +#include <com/sun/star/ui/dialogs/XFilePreview.hpp> +#include <com/sun/star/ui/dialogs/XFilePicker3.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/beans/StringPair.hpp> + +#include <vector> +#include <memory> +#include <rtl/ustring.hxx> + +#include "SalGtkPicker.hxx" + +// Implementation class for the XFilePicker Interface + +struct FilterEntry; +struct ElementEntry_Impl; + +// class declaration + +typedef cppu::WeakComponentImplHelper< + css::ui::dialogs::XFilePickerControlAccess, + css::ui::dialogs::XFilePreview, + css::ui::dialogs::XFilePicker3, + css::lang::XInitialization + > SalGtkFilePicker_Base; + +class SalGtkFilePicker : public SalGtkPicker, public SalGtkFilePicker_Base +{ + public: + + // constructor + SalGtkFilePicker( const css::uno::Reference< css::uno::XComponentContext >& xServiceMgr ); + + // XFilePickerNotifier + + virtual void SAL_CALL addFilePickerListener( const css::uno::Reference< css::ui::dialogs::XFilePickerListener >& xListener ) override; + virtual void SAL_CALL removeFilePickerListener( const css::uno::Reference< css::ui::dialogs::XFilePickerListener >& xListener ) override; + + // XExecutableDialog functions + + virtual void SAL_CALL setTitle( const OUString& aTitle ) override; + + virtual sal_Int16 SAL_CALL execute() override; + + // XFilePicker functions + + virtual void SAL_CALL setMultiSelectionMode( sal_Bool bMode ) override; + + virtual void SAL_CALL setDefaultName( const OUString& aName ) override; + + virtual void SAL_CALL setDisplayDirectory( const OUString& aDirectory ) override; + + virtual OUString SAL_CALL getDisplayDirectory( ) override; + + virtual css::uno::Sequence< OUString > SAL_CALL getFiles( ) override; + + // XFilePicker2 functions + + virtual css::uno::Sequence< OUString > SAL_CALL getSelectedFiles() override; + + // XFilterManager functions + + virtual void SAL_CALL appendFilter( const OUString& aTitle, const OUString& aFilter ) override; + + virtual void SAL_CALL setCurrentFilter( const OUString& aTitle ) override; + + virtual OUString SAL_CALL getCurrentFilter( ) override; + + // XFilterGroupManager functions + + virtual void SAL_CALL appendFilterGroup( const OUString& sGroupTitle, const css::uno::Sequence< css::beans::StringPair >& aFilters ) override; + + // XFilePickerControlAccess functions + + virtual void SAL_CALL setValue( sal_Int16 nControlId, sal_Int16 nControlAction, const css::uno::Any& aValue ) override; + + virtual css::uno::Any SAL_CALL getValue( sal_Int16 aControlId, sal_Int16 aControlAction ) override; + + virtual void SAL_CALL enableControl( sal_Int16 nControlId, sal_Bool bEnable ) override; + + virtual void SAL_CALL setLabel( sal_Int16 nControlId, const OUString& aLabel ) override; + + virtual OUString SAL_CALL getLabel( sal_Int16 nControlId ) override; + + // XFilePreview + + virtual css::uno::Sequence< sal_Int16 > SAL_CALL getSupportedImageFormats( ) override; + + virtual sal_Int32 SAL_CALL getTargetColorDepth( ) override; + + virtual sal_Int32 SAL_CALL getAvailableWidth( ) override; + + virtual sal_Int32 SAL_CALL getAvailableHeight( ) override; + + virtual void SAL_CALL setImage( sal_Int16 aImageFormat, const css::uno::Any& aImage ) override; + + virtual sal_Bool SAL_CALL setShowState( sal_Bool bShowState ) override; + + virtual sal_Bool SAL_CALL getShowState( ) override; + + // XInitialization + + virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override; + + // XCancellable + + virtual void SAL_CALL cancel( ) override; + + // FilePicker Event functions + + private: + SalGtkFilePicker( const SalGtkFilePicker& ) = delete; + SalGtkFilePicker& operator=( const SalGtkFilePicker& ) = delete; + + bool FilterNameExists( const OUString& rTitle ); + bool FilterNameExists( const css::uno::Sequence< css::beans::StringPair >& _rGroupedFilters ); + + void ensureFilterVector( const OUString& _rInitialCurrentFilter ); + + void impl_fileSelectionChanged( const css::ui::dialogs::FilePickerEvent& aEvent ); + void impl_directoryChanged( const css::ui::dialogs::FilePickerEvent& aEvent ); + void impl_controlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent ); + + private: + css::uno::Reference< css::ui::dialogs::XFilePickerListener > + m_xListener; + OUString msPlayLabel; + std::unique_ptr<std::vector<FilterEntry>> m_pFilterVector; + GtkWidget *m_pParentWidget; + GtkWidget *m_pVBox; + GtkWidget *m_pFilterExpander; + GtkWidget *m_pFilterView; + GtkListStore *m_pFilterStore; + + enum { + AUTOEXTENSION, + PASSWORD, + FILTEROPTIONS, + READONLY, + LINK, + PREVIEW, + SELECTION, + GPGENCRYPTION, + TOGGLE_LAST + }; + + GtkWidget *m_pToggles[ TOGGLE_LAST ]; + + bool mbToggleVisibility[TOGGLE_LAST]; + + enum { + PLAY, + BUTTON_LAST }; + + GtkWidget *m_pButtons[ BUTTON_LAST ]; + + enum { + VERSION, + TEMPLATE, + IMAGE_TEMPLATE, + IMAGE_ANCHOR, + LIST_LAST + }; + + GtkWidget *m_pHBoxs[ LIST_LAST ]; + GtkWidget *m_pAligns[ LIST_LAST ]; + GtkWidget *m_pLists[ LIST_LAST ]; + GtkWidget *m_pListLabels[ LIST_LAST ]; + bool mbListVisibility[ LIST_LAST ]; + bool mbButtonVisibility[ BUTTON_LAST ]; + gulong mnHID_FolderChange; + gulong mnHID_SelectionChange; + + OUString m_aCurrentFilter; + OUString m_aInitialFilter; + + bool bVersionWidthUnset; + bool mbPreviewState; + gulong mHID_Preview; + GtkWidget* m_pPreview; + GtkFileFilter* m_pPseudoFilter; + static constexpr sal_Int32 g_PreviewImageWidth = 256; + static constexpr sal_Int32 g_PreviewImageHeight = 256; + + GtkWidget *getWidget( sal_Int16 nControlId, GType *pType = nullptr); + + void SetCurFilter( const OUString& rFilter ); + void SetFilters(); + void UpdateFilterfromUI(); + + void implChangeType( GtkTreeSelection *selection ); + GtkFileFilter * implAddFilter( const OUString& rFilter, const OUString& rType ); + void implAddFilterGroup( const OUString& rFilter, + const css::uno::Sequence< css::beans::StringPair>& _rFilters ); + void updateCurrentFilterFromName(const gchar* filtername); + void unselect_type(); + void InitialMapping(); + + void HandleSetListValue(GtkComboBox *pWidget, sal_Int16 nControlAction, + const css::uno::Any& rValue); + static css::uno::Any HandleGetListValue(GtkComboBox *pWidget, sal_Int16 nControlAction); + + static void expander_changed_cb( GtkExpander *expander, SalGtkFilePicker *pobjFP ); + static void preview_toggled_cb( GObject *cb, SalGtkFilePicker *pobjFP ); + static void filter_changed_cb( GtkFileChooser *file_chooser, GParamSpec *pspec, SalGtkFilePicker *pobjFP ); + static void type_changed_cb( GtkTreeSelection *selection, SalGtkFilePicker *pobjFP ); + static void folder_changed_cb (GtkFileChooser *file_chooser, SalGtkFilePicker *pobjFP); + static void selection_changed_cb (GtkFileChooser *file_chooser, SalGtkFilePicker *pobjFP); + static void update_preview_cb (GtkFileChooser *file_chooser, SalGtkFilePicker *pobjFP); + static void dialog_mapped_cb(GtkWidget *widget, SalGtkFilePicker *pobjFP); + public: + virtual ~SalGtkFilePicker() override; + +}; +#endif // INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKFILEPICKER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/SalGtkFolderPicker.cxx b/vcl/unx/gtk3/fpicker/SalGtkFolderPicker.cxx new file mode 100644 index 000000000000..13df53994dee --- /dev/null +++ b/vcl/unx/gtk3/fpicker/SalGtkFolderPicker.cxx @@ -0,0 +1,183 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifdef AIX +#define _LINUX_SOURCE_COMPAT +#include <sys/timer.h> +#undef _LINUX_SOURCE_COMPAT +#endif + +#include <config_gio.h> + +#include <com/sun/star/awt/Toolkit.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/lang/XMultiComponentFactory.hpp> +#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> +#include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp> +#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp> +#include <com/sun/star/ui/dialogs/TemplateDescription.hpp> +#include <vcl/svapp.hxx> +#include <unx/gtk/gtkinst.hxx> +#include "SalGtkFolderPicker.hxx" +#include <sal/log.hxx> + +#include <string.h> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::ui::dialogs; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::uno; + +// constructor + +SalGtkFolderPicker::SalGtkFolderPicker( const uno::Reference< uno::XComponentContext >& xContext ) : + SalGtkPicker( xContext ) +{ + m_pDialog = gtk_file_chooser_dialog_new( + OUStringToOString( getResString( FOLDERPICKER_TITLE ), RTL_TEXTENCODING_UTF8 ).getStr(), + nullptr, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, nullptr ); + + gtk_dialog_set_default_response( GTK_DIALOG (m_pDialog), GTK_RESPONSE_ACCEPT ); +#if ENABLE_GIO + gtk_file_chooser_set_local_only( GTK_FILE_CHOOSER( m_pDialog ), false ); +#endif + gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( m_pDialog ), false ); +} + +void SAL_CALL SalGtkFolderPicker::setDisplayDirectory( const OUString& aDirectory ) +{ + SolarMutexGuard g; + + assert( m_pDialog != nullptr ); + + OString aTxt = unicodetouri( aDirectory ); + if( aTxt.isEmpty() ){ + aTxt = unicodetouri("file:///."); + } + + if( aTxt.endsWith("/") ) + aTxt = aTxt.copy( 0, aTxt.getLength() - 1 ); + + SAL_INFO( "vcl", "setting path to " << aTxt ); + + gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ), + aTxt.getStr() ); +} + +OUString SAL_CALL SalGtkFolderPicker::getDisplayDirectory() +{ + SolarMutexGuard g; + + assert( m_pDialog != nullptr ); + + gchar* pCurrentFolder = + gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ) ); + OUString aCurrentFolderName = uritounicode(pCurrentFolder); + g_free( pCurrentFolder ); + + return aCurrentFolderName; +} + +OUString SAL_CALL SalGtkFolderPicker::getDirectory() +{ + SolarMutexGuard g; + + assert( m_pDialog != nullptr ); + + gchar* pSelectedFolder = + gtk_file_chooser_get_uri( GTK_FILE_CHOOSER( m_pDialog ) ); + OUString aSelectedFolderName = uritounicode(pSelectedFolder); + g_free( pSelectedFolder ); + + return aSelectedFolderName; +} + +void SAL_CALL SalGtkFolderPicker::setDescription( const OUString& /*rDescription*/ ) +{ +} + +// XExecutableDialog functions + +void SAL_CALL SalGtkFolderPicker::setTitle( const OUString& aTitle ) +{ + SolarMutexGuard g; + + assert( m_pDialog != nullptr ); + + OString aWindowTitle = OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 ); + + gtk_window_set_title( GTK_WINDOW( m_pDialog ), aWindowTitle.getStr() ); +} + +sal_Int16 SAL_CALL SalGtkFolderPicker::execute() +{ + SolarMutexGuard g; + + assert( m_pDialog != nullptr ); + + sal_Int16 retVal = 0; + + uno::Reference< awt::XExtendedToolkit > xToolkit = + awt::Toolkit::create(m_xContext); + + uno::Reference<frame::XDesktop> xDesktop = frame::Desktop::create(m_xContext); + + GtkWindow *pParent = RunDialog::GetTransientFor(); + if (pParent) + gtk_window_set_transient_for(GTK_WINDOW(m_pDialog), pParent); + RunDialog* pRunDialog = new RunDialog(m_pDialog, xToolkit, xDesktop); + uno::Reference < awt::XTopWindowListener > xLifeCycle(pRunDialog); + gint nStatus = pRunDialog->run(); + switch( nStatus ) + { + case GTK_RESPONSE_ACCEPT: + retVal = ExecutableDialogResults::OK; + break; + case GTK_RESPONSE_CANCEL: + retVal = ExecutableDialogResults::CANCEL; + break; + default: + retVal = 0; + break; + } + gtk_widget_hide(m_pDialog); + + return retVal; +} + +// XCancellable + +void SAL_CALL SalGtkFolderPicker::cancel() +{ + SolarMutexGuard g; + + assert( m_pDialog != nullptr ); + + // TODO m_pImpl->cancel(); +} + +uno::Reference< ui::dialogs::XFolderPicker2 > +GtkInstance::createFolderPicker( const uno::Reference< uno::XComponentContext > &xMSF ) +{ + return uno::Reference< ui::dialogs::XFolderPicker2 >( + new SalGtkFolderPicker( xMSF ) ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/SalGtkFolderPicker.hxx b/vcl/unx/gtk3/fpicker/SalGtkFolderPicker.hxx new file mode 100644 index 000000000000..229bbe8b8749 --- /dev/null +++ b/vcl/unx/gtk3/fpicker/SalGtkFolderPicker.hxx @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKFOLDERPICKER_HXX +#define INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKFOLDERPICKER_HXX + +#include <list> +#include <memory> +#include <rtl/ustring.hxx> +#include <cppuhelper/implbase.hxx> + +#include "SalGtkPicker.hxx" + +class SalGtkFolderPicker : + public SalGtkPicker, + public cppu::WeakImplHelper< css::ui::dialogs::XFolderPicker2 > +{ + public: + + // constructor + SalGtkFolderPicker( const css::uno::Reference< css::uno::XComponentContext >& xServiceMgr ); + + // XExecutableDialog functions + + virtual void SAL_CALL setTitle( const OUString& aTitle ) override; + + virtual sal_Int16 SAL_CALL execute( ) override; + + // XFolderPicker functions + + virtual void SAL_CALL setDisplayDirectory( const OUString& rDirectory ) override; + + virtual OUString SAL_CALL getDisplayDirectory( ) override; + + virtual OUString SAL_CALL getDirectory( ) override; + + virtual void SAL_CALL setDescription( const OUString& rDescription ) override; + + // XCancellable + + virtual void SAL_CALL cancel( ) override; + + private: + SalGtkFolderPicker( const SalGtkFolderPicker& ) = delete; + SalGtkFolderPicker& operator=( const SalGtkFolderPicker& ) = delete; +}; + +#endif // INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKFOLDERPICKER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/SalGtkPicker.cxx b/vcl/unx/gtk3/fpicker/SalGtkPicker.cxx new file mode 100644 index 000000000000..7db114069651 --- /dev/null +++ b/vcl/unx/gtk3/fpicker/SalGtkPicker.cxx @@ -0,0 +1,267 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifdef AIX +#define _LINUX_SOURCE_COMPAT +#include <sys/timer.h> +#undef _LINUX_SOURCE_COMPAT +#endif + +#include <com/sun/star/frame/TerminationVetoException.hpp> +#include <com/sun/star/lang/XMultiComponentFactory.hpp> +#include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp> +#include <com/sun/star/accessibility/XAccessibleContext.hpp> +#include <com/sun/star/accessibility/AccessibleRole.hpp> +#include <rtl/process.h> +#include <osl/diagnose.h> +#include <sal/log.hxx> +#include <vcl/svapp.hxx> +#include <tools/urlobj.hxx> + +#include <vcl/window.hxx> +#include <unx/gtk/gtkframe.hxx> +#include "SalGtkPicker.hxx" + +using namespace ::rtl; +using namespace ::com::sun::star; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::uno; + +OUString SalGtkPicker::uritounicode(const gchar* pIn) +{ + if (!pIn) + return OUString(); + + OUString sURL( pIn, strlen(pIn), + RTL_TEXTENCODING_UTF8 ); + + INetURLObject aURL(sURL); + if (INetProtocol::File == aURL.GetProtocol()) + { + // all the URLs are handled by office in UTF-8 + // so the Gnome FP related URLs should be converted accordingly + OUString aNewURL = uri::ExternalUriReferenceTranslator::create( m_xContext )->translateToInternal(sURL); + if( !aNewURL.isEmpty() ) + sURL = aNewURL; + } + return sURL; +} + +OString SalGtkPicker::unicodetouri(const OUString &rURL) +{ + // all the URLs are handled by office in UTF-8 ( and encoded with "%xx" codes based on UTF-8 ) + // so the Gnome FP related URLs should be converted accordingly + OString sURL = OUStringToOString(rURL, RTL_TEXTENCODING_UTF8); + INetURLObject aURL(rURL); + if (INetProtocol::File == aURL.GetProtocol()) + { + OUString aNewURL = uri::ExternalUriReferenceTranslator::create( m_xContext )->translateToExternal(rURL); + + if( !aNewURL.isEmpty() ) + { + // At this point the URL should contain ascii characters only actually + sURL = OUStringToOString( aNewURL, osl_getThreadTextEncoding() ); + } + } + return sURL; +} + +extern "C" +{ + static gboolean canceldialog(RunDialog *pDialog) + { + SolarMutexGuard g; + pDialog->cancel(); + return false; + } +} + +GtkWindow* RunDialog::GetTransientFor() +{ + GtkWindow *pParent = nullptr; + + vcl::Window * pWindow = ::Application::GetActiveTopWindow(); + if( pWindow ) + { + GtkSalFrame *pFrame = dynamic_cast<GtkSalFrame *>( pWindow->ImplGetFrame() ); + if( pFrame ) + pParent = GTK_WINDOW( pFrame->getWindow() ); + } + + return pParent; +} + +RunDialog::RunDialog(GtkWidget *pDialog, const uno::Reference<awt::XExtendedToolkit>& rToolkit, + const uno::Reference<frame::XDesktop>& rDesktop) + : cppu::WeakComponentImplHelper<awt::XTopWindowListener, frame::XTerminateListener>(maLock) + , mpDialog(pDialog) + , mbTerminateDesktop(false) + , mxToolkit(rToolkit) + , mxDesktop(rDesktop) +{ +} + +RunDialog::~RunDialog() +{ + SolarMutexGuard g; + + g_source_remove_by_user_data (this); +} + +void SAL_CALL RunDialog::windowOpened(const css::lang::EventObject& e) +{ + SolarMutexGuard g; + + //Don't popdown dialogs if a tooltip appears elsewhere, that's ok, but do pop down + //if another dialog/frame is launched. + css::uno::Reference<css::accessibility::XAccessible> xAccessible(e.Source, css::uno::UNO_QUERY); + if (xAccessible.is()) + { + css::uno::Reference<css::accessibility::XAccessibleContext> xContext(xAccessible->getAccessibleContext()); + if (xContext.is() && xContext->getAccessibleRole() == css::accessibility::AccessibleRole::TOOL_TIP) + { + return; + } + } + + g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, reinterpret_cast<GSourceFunc>(canceldialog), this, nullptr); +} + +void SAL_CALL RunDialog::queryTermination( const css::lang::EventObject& ) +{ + SolarMutexGuard g; + + g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, reinterpret_cast<GSourceFunc>(canceldialog), this, nullptr); + + mbTerminateDesktop = true; + + throw css::frame::TerminationVetoException(); +} + +void SAL_CALL RunDialog::notifyTermination( const css::lang::EventObject& ) +{ +} + +void RunDialog::cancel() +{ + gtk_dialog_response( GTK_DIALOG( mpDialog ), GTK_RESPONSE_CANCEL ); + gtk_widget_hide( mpDialog ); +} + +namespace +{ + class ExecuteInfo + { + private: + css::uno::Reference<css::frame::XDesktop> mxDesktop; + public: + ExecuteInfo(const css::uno::Reference<css::frame::XDesktop>& rDesktop) + : mxDesktop(rDesktop) + { + } + void terminate() + { + mxDesktop->terminate(); + } + }; +} + +gint RunDialog::run() +{ + if (mxToolkit.is()) + mxToolkit->addTopWindowListener(this); + + mxDesktop->addTerminateListener(this); + gint nStatus = gtk_dialog_run(GTK_DIALOG(mpDialog)); + mxDesktop->removeTerminateListener(this); + + if (mxToolkit.is()) + mxToolkit->removeTopWindowListener(this); + + if (mbTerminateDesktop) + { + ExecuteInfo* pExecuteInfo = new ExecuteInfo(mxDesktop); + Application::PostUserEvent(LINK(nullptr, RunDialog, TerminateDesktop), pExecuteInfo); + } + + return nStatus; +} + +IMPL_STATIC_LINK(RunDialog, TerminateDesktop, void*, p, void) +{ + ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p); + pExecuteInfo->terminate(); + delete pExecuteInfo; +} + +SalGtkPicker::SalGtkPicker( const uno::Reference<uno::XComponentContext>& xContext ) + : m_pDialog( nullptr ), m_xContext( xContext ) +{ +} + +SalGtkPicker::~SalGtkPicker() +{ + SolarMutexGuard g; + + if (m_pDialog) + { + gtk_widget_destroy(m_pDialog); + } +} + +void SalGtkPicker::implsetDisplayDirectory( const OUString& aDirectory ) +{ + OSL_ASSERT( m_pDialog != nullptr ); + + OString aTxt = unicodetouri(aDirectory); + if( aTxt.isEmpty() ){ + aTxt = unicodetouri("file:///."); + } + + if( aTxt.endsWith("/") ) + aTxt = aTxt.copy( 0, aTxt.getLength() - 1 ); + + SAL_INFO( "vcl", "setting path to " << aTxt ); + + gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ), + aTxt.getStr() ); +} + +OUString SalGtkPicker::implgetDisplayDirectory() +{ + OSL_ASSERT( m_pDialog != nullptr ); + + gchar* pCurrentFolder = + gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ) ); + OUString aCurrentFolderName = uritounicode(pCurrentFolder); + g_free( pCurrentFolder ); + + return aCurrentFolderName; +} + +void SalGtkPicker::implsetTitle( const OUString& aTitle ) +{ + OSL_ASSERT( m_pDialog != nullptr ); + + OString aWindowTitle = OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 ); + + gtk_window_set_title( GTK_WINDOW( m_pDialog ), aWindowTitle.getStr() ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/SalGtkPicker.hxx b/vcl/unx/gtk3/fpicker/SalGtkPicker.hxx new file mode 100644 index 000000000000..0eb8720f771a --- /dev/null +++ b/vcl/unx/gtk3/fpicker/SalGtkPicker.hxx @@ -0,0 +1,117 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKPICKER_HXX +#define INCLUDED_VCL_UNX_GTK_FPICKER_SALGTKPICKER_HXX + +#include <osl/mutex.hxx> +#include <tools/link.hxx> +#include <cppuhelper/compbase.hxx> + +#include <com/sun/star/awt/XTopWindowListener.hpp> +#include <com/sun/star/awt/XExtendedToolkit.hpp> +#include <com/sun/star/frame/XDesktop.hpp> +#include <com/sun/star/frame/XTerminateListener.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> + +#include <gtk/gtk.h> +#include <gdk/gdkkeysyms.h> + +#define FOLDERPICKER_TITLE 500 +#define FOLDER_PICKER_DEF_DESCRIPTION 501 +#define FILE_PICKER_TITLE_OPEN 502 +#define FILE_PICKER_TITLE_SAVE 503 +#define FILE_PICKER_FILE_TYPE 504 +#define FILE_PICKER_OVERWRITE_PRIMARY 505 +#define FILE_PICKER_OVERWRITE_SECONDARY 506 +#define FILE_PICKER_ALLFORMATS 507 + +class SalGtkPicker +{ + public: + SalGtkPicker( const css::uno::Reference<css::uno::XComponentContext>& xContext ); + virtual ~SalGtkPicker(); + protected: + osl::Mutex m_rbHelperMtx; + GtkWidget *m_pDialog; + protected: + /// @throws css::uno::RuntimeException + void implsetTitle( const OUString& aTitle ); + + /// @throws css::lang::IllegalArgumentException + /// @throws css::uno::RuntimeException + void implsetDisplayDirectory( const OUString& rDirectory ); + + /// @throws css::uno::RuntimeException + OUString implgetDisplayDirectory( ); + OUString uritounicode(const gchar *pIn); + OString unicodetouri(const OUString &rURL); + + // to instantiate own services + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + static OUString getResString( sal_Int32 aId ); +}; + +//Run the Gtk Dialog. Watch for any "new windows" created while we're +//executing and consider that a CANCEL event to avoid e.g. "file cannot be opened" +//modal dialogs and this one getting locked if some other API call causes this +//to happen while we're opened waiting for user input, e.g. +//https://bugzilla.redhat.com/show_bug.cgi?id=441108 +class RunDialog : + public cppu::WeakComponentImplHelper< + css::awt::XTopWindowListener, + css::frame::XTerminateListener > +{ +private: + osl::Mutex maLock; + GtkWidget * const mpDialog; + bool mbTerminateDesktop; + css::uno::Reference<css::awt::XExtendedToolkit> mxToolkit; + css::uno::Reference<css::frame::XDesktop> mxDesktop; + DECL_STATIC_LINK(RunDialog, TerminateDesktop, void*, void); +public: + + // XTopWindowListener + using cppu::WeakComponentImplHelperBase::disposing; + virtual void SAL_CALL disposing( const css::lang::EventObject& ) override {} + virtual void SAL_CALL windowOpened( const css::lang::EventObject& e ) override; + virtual void SAL_CALL windowClosing( const css::lang::EventObject& ) override {} + virtual void SAL_CALL windowClosed( const css::lang::EventObject& ) override {} + virtual void SAL_CALL windowMinimized( const css::lang::EventObject& ) override {} + virtual void SAL_CALL windowNormalized( const css::lang::EventObject& ) override {} + virtual void SAL_CALL windowActivated( const css::lang::EventObject& ) override {} + virtual void SAL_CALL windowDeactivated( const css::lang::EventObject& ) override {} + + // XTerminateListener + virtual void SAL_CALL queryTermination( const css::lang::EventObject& aEvent ) override; + virtual void SAL_CALL notifyTermination( const css::lang::EventObject& aEvent ) override; +public: + RunDialog(GtkWidget *pDialog, + const css::uno::Reference<css::awt::XExtendedToolkit>& rToolkit, + const css::uno::Reference<css::frame::XDesktop>& rDesktop); + virtual ~RunDialog() override; + gint run(); + void cancel(); + static GtkWindow* GetTransientFor(); +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/eventnotification.hxx b/vcl/unx/gtk3/fpicker/eventnotification.hxx new file mode 100644 index 000000000000..88a472e0e8ce --- /dev/null +++ b/vcl/unx/gtk3/fpicker/eventnotification.hxx @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_VCL_UNX_GTK_FPICKER_EVENTNOTIFICATION_HXX +#define INCLUDED_VCL_UNX_GTK_FPICKER_EVENTNOTIFICATION_HXX + +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/uno/Reference.hxx> + +// encapsulate a filepicker event +// notification, because there are +// two types of filepicker notifications +// with and without parameter +// this is an application of the +// "command" pattern see GoF + +class CEventNotification +{ +public: + virtual ~CEventNotification() { }; + + virtual void SAL_CALL notifyEventListener( css::uno::Reference< css::uno::XInterface > xListener ) = 0; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/fpicker/resourceprovider.cxx b/vcl/unx/gtk3/fpicker/resourceprovider.cxx new file mode 100644 index 000000000000..818018b4a7e5 --- /dev/null +++ b/vcl/unx/gtk3/fpicker/resourceprovider.cxx @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <vcl/svapp.hxx> +#include <unotools/resmgr.hxx> +#include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp> +#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp> + +#include <strings.hrc> +#include <svdata.hxx> +#include "SalGtkPicker.hxx" + +using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds; +using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds; + +// translate control ids to resource ids + +static const struct +{ + sal_Int32 const ctrlId; + const char *resId; +} CtrlIdToResIdTable[] = { + { CHECKBOX_AUTOEXTENSION, STR_FPICKER_AUTO_EXTENSION }, + { CHECKBOX_PASSWORD, STR_FPICKER_PASSWORD }, + { CHECKBOX_GPGENCRYPTION, STR_FPICKER_GPGENCRYPT }, + { CHECKBOX_FILTEROPTIONS, STR_FPICKER_FILTER_OPTIONS }, + { CHECKBOX_READONLY, STR_FPICKER_READONLY }, + { CHECKBOX_LINK, STR_FPICKER_INSERT_AS_LINK }, + { CHECKBOX_PREVIEW, STR_FPICKER_SHOW_PREVIEW }, + { PUSHBUTTON_PLAY, STR_FPICKER_PLAY }, + { LISTBOX_VERSION_LABEL, STR_FPICKER_VERSION }, + { LISTBOX_TEMPLATE_LABEL, STR_FPICKER_TEMPLATES }, + { LISTBOX_IMAGE_TEMPLATE_LABEL, STR_FPICKER_IMAGE_TEMPLATE }, + { LISTBOX_IMAGE_ANCHOR_LABEL, STR_FPICKER_IMAGE_ANCHOR }, + { CHECKBOX_SELECTION, STR_FPICKER_SELECTION }, + { FOLDERPICKER_TITLE, STR_FPICKER_FOLDER_DEFAULT_TITLE }, + { FOLDER_PICKER_DEF_DESCRIPTION, STR_FPICKER_FOLDER_DEFAULT_DESCRIPTION }, + { FILE_PICKER_OVERWRITE_PRIMARY, STR_FPICKER_ALREADYEXISTOVERWRITE_PRIMARY }, + { FILE_PICKER_OVERWRITE_SECONDARY, STR_FPICKER_ALREADYEXISTOVERWRITE_SECONDARY }, + { FILE_PICKER_ALLFORMATS, STR_FPICKER_ALLFORMATS }, + { FILE_PICKER_TITLE_OPEN, STR_FPICKER_OPEN }, + { FILE_PICKER_TITLE_SAVE, STR_FPICKER_SAVE }, + { FILE_PICKER_FILE_TYPE, STR_FPICKER_TYPE } +}; + +static const char* CtrlIdToResId( sal_Int32 aControlId ) +{ + for (auto & i : CtrlIdToResIdTable) + { + if ( i.ctrlId == aControlId ) + return i.resId; + } + return nullptr; +} + +OUString SalGtkPicker::getResString( sal_Int32 aId ) +{ + OUString aResString; + // translate the control id to a resource id + const char *pResId = CtrlIdToResId( aId ); + if (pResId) + aResString = VclResId(pResId); + return aResString.replace('~', '_'); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3fpicker.cxx b/vcl/unx/gtk3/gtk3fpicker.cxx deleted file mode 100644 index 9b83c57daed8..000000000000 --- a/vcl/unx/gtk3/gtk3fpicker.cxx +++ /dev/null @@ -1,15 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#include "../gtk/fpicker/resourceprovider.cxx" -#include "../gtk/fpicker/SalGtkPicker.cxx" -#include "../gtk/fpicker/SalGtkFilePicker.cxx" -#include "../gtk/fpicker/SalGtkFolderPicker.cxx" - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3gloactiongroup.cxx b/vcl/unx/gtk3/gtk3gloactiongroup.cxx index 749f5434a456..19b412faec38 100644 --- a/vcl/unx/gtk3/gtk3gloactiongroup.cxx +++ b/vcl/unx/gtk3/gtk3gloactiongroup.cxx @@ -1,5 +1,403 @@ /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ -#include "../gtk/gloactiongroup.cxx" +#include <unx/gtk/gtksalmenu.hxx> + +#include <unx/gtk/gloactiongroup.h> +#include <unx/gtk/gtkinst.hxx> +#include <unx/gtk/gtkframe.hxx> + +#include <sal/log.hxx> + +/* + * GLOAction + */ + +#define G_TYPE_LO_ACTION (g_lo_action_get_type ()) +#define G_LO_ACTION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \ + G_TYPE_LO_ACTION, GLOAction)) + +struct GLOAction +{ + GObject parent_instance; + + gint item_id; // Menu item ID. + gboolean submenu; // TRUE if action is a submenu action. + gboolean enabled; // TRUE if action is enabled. + GVariantType* parameter_type; // A GVariantType with the action parameter type. + GVariantType* state_type; // A GVariantType with item state type + GVariant* state_hint; // A GVariant with state hints. + GVariant* state; // A GVariant with current item state +}; + +typedef GObjectClass GLOActionClass; + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif +G_DEFINE_TYPE (GLOAction, g_lo_action, G_TYPE_OBJECT); +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + +static GLOAction* +g_lo_action_new() +{ + return G_LO_ACTION (g_object_new (G_TYPE_LO_ACTION, nullptr)); +} + +static void +g_lo_action_init (GLOAction *action) +{ + action->item_id = -1; + action->submenu = FALSE; + action->enabled = TRUE; + action->parameter_type = nullptr; + action->state_type = nullptr; + action->state_hint = nullptr; + action->state = nullptr; +} + +static void +g_lo_action_finalize (GObject *object) +{ + GLOAction* action = G_LO_ACTION(object); + + if (action->parameter_type) + g_variant_type_free (action->parameter_type); + + if (action->state_type) + g_variant_type_free (action->state_type); + + if (action->state_hint) + g_variant_unref (action->state_hint); + + if (action->state) + g_variant_unref (action->state); + + G_OBJECT_CLASS (g_lo_action_parent_class)->finalize (object); +} + +static void +g_lo_action_class_init (GLOActionClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS(klass); + + object_class->finalize = g_lo_action_finalize; +} + +/* + * GLOActionGroup + */ + +struct GLOActionGroupPrivate +{ + GHashTable *table; /* string -> GLOAction */ +}; + +static void g_lo_action_group_iface_init (GActionGroupInterface *); + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif +G_DEFINE_TYPE_WITH_CODE (GLOActionGroup, + g_lo_action_group, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, + g_lo_action_group_iface_init)); +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + +static gchar ** +g_lo_action_group_list_actions (GActionGroup *group) +{ + GLOActionGroup *loGroup = G_LO_ACTION_GROUP (group); + GHashTableIter iter; + gint n, i = 0; + gchar **keys; + gpointer key; + + n = g_hash_table_size (loGroup->priv->table); + keys = g_new (gchar *, n + 1); + + g_hash_table_iter_init (&iter, loGroup->priv->table); + while (g_hash_table_iter_next (&iter, &key, nullptr)) + keys[i++] = g_strdup (static_cast<gchar*>(key)); + g_assert_cmpint (i, ==, n); + keys[n] = nullptr; + + return keys; +} + +static gboolean +g_lo_action_group_query_action (GActionGroup *group, + const gchar *action_name, + gboolean *enabled, + const GVariantType **parameter_type, + const GVariantType **state_type, + GVariant **state_hint, + GVariant **state) +{ + //SAL_INFO("vcl.unity", "g_lo_action_group_query_action on " << group); + GLOActionGroup *lo_group = G_LO_ACTION_GROUP (group); + GLOAction* action = G_LO_ACTION (g_hash_table_lookup (lo_group->priv->table, action_name)); + + if (action == nullptr) + return FALSE; + + if (enabled) + { + *enabled = action->enabled; + } + + if (parameter_type) + *parameter_type = action->parameter_type; + + if (state_type) + *state_type = action->state_type; + + if (state_hint) + *state_hint = (action->state_hint) ? g_variant_ref (action->state_hint) : nullptr; + + if (state) + *state = (action->state) ? g_variant_ref (action->state) : nullptr; + + return TRUE; +} + +static void +g_lo_action_group_perform_submenu_action (GLOActionGroup *group, + const gchar *action_name, + GVariant *state) +{ + gboolean bState = g_variant_get_boolean (state); + SAL_INFO("vcl.unity", "g_lo_action_group_perform_submenu_action on " << group << " to " << bState); + + if (bState) + GtkSalMenu::Activate(action_name); + else + GtkSalMenu::Deactivate(action_name); +} + +static void +g_lo_action_group_change_state (GActionGroup *group, + const gchar *action_name, + GVariant *value) +{ + SAL_INFO("vcl.unity", "g_lo_action_group_change_state on " << group ); + g_return_if_fail (value != nullptr); + + g_variant_ref_sink (value); + + if (action_name != nullptr) + { + GLOActionGroup* lo_group = G_LO_ACTION_GROUP (group); + GLOAction* action = G_LO_ACTION (g_hash_table_lookup (lo_group->priv->table, action_name)); + + if (action != nullptr) + { + if (action->submenu) + g_lo_action_group_perform_submenu_action (lo_group, action_name, value); + else + { + gboolean is_new = FALSE; + + /* If action already exists but has no state, it should be removed and added again. */ + if (action->state_type == nullptr) + { + g_action_group_action_removed (G_ACTION_GROUP (group), action_name); + action->state_type = g_variant_type_copy (g_variant_get_type(value)); + is_new = TRUE; + } + + if (g_variant_is_of_type (value, action->state_type)) + { + if (action->state) + g_variant_unref(action->state); + + action->state = g_variant_ref (value); + + if (is_new) + g_action_group_action_added (G_ACTION_GROUP (group), action_name); + else + g_action_group_action_state_changed (group, action_name, value); + } + } + } + } + + g_variant_unref (value); +} + +static void +g_lo_action_group_activate (GActionGroup *group, + const gchar *action_name, + GVariant *parameter) +{ + if (parameter != nullptr) + g_action_group_change_action_state(group, action_name, parameter); + GtkSalMenu::DispatchCommand(action_name); +} + +void +g_lo_action_group_insert (GLOActionGroup *group, + const gchar *action_name, + gint item_id, + gboolean submenu) +{ + g_lo_action_group_insert_stateful (group, action_name, item_id, submenu, nullptr, nullptr, nullptr, nullptr); +} + +void +g_lo_action_group_insert_stateful (GLOActionGroup *group, + const gchar *action_name, + gint item_id, + gboolean submenu, + const GVariantType *parameter_type, + const GVariantType *state_type, + GVariant *state_hint, + GVariant *state) +{ + g_return_if_fail (G_IS_LO_ACTION_GROUP (group)); + + GLOAction* old_action = G_LO_ACTION (g_hash_table_lookup (group->priv->table, action_name)); + + if (old_action == nullptr || old_action->item_id != item_id) + { + if (old_action != nullptr) + g_lo_action_group_remove (group, action_name); + + GLOAction* action = g_lo_action_new(); + + g_hash_table_insert (group->priv->table, g_strdup (action_name), action); + + action->item_id = item_id; + action->submenu = submenu; + + if (parameter_type) + action->parameter_type = const_cast<GVariantType*>(parameter_type); + + if (state_type) + action->state_type = const_cast<GVariantType*>(state_type); + + if (state_hint) + action->state_hint = g_variant_ref_sink (state_hint); + + if (state) + action->state = g_variant_ref_sink (state); + + g_action_group_action_added (G_ACTION_GROUP (group), action_name); + } +} + +static void +g_lo_action_group_finalize (GObject *object) +{ + GLOActionGroup *lo_group = G_LO_ACTION_GROUP (object); + + g_hash_table_unref (lo_group->priv->table); + + G_OBJECT_CLASS (g_lo_action_group_parent_class)->finalize (object); +} + +static void +g_lo_action_group_init (GLOActionGroup *group) +{ + SAL_INFO("vcl.unity", "g_lo_action_group_init on " << group); + group->priv = G_TYPE_INSTANCE_GET_PRIVATE (group, + G_TYPE_LO_ACTION_GROUP, + GLOActionGroupPrivate); + group->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_object_unref); +} + +static void +g_lo_action_group_class_init (GLOActionGroupClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = g_lo_action_group_finalize; +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + g_type_class_add_private (klass, sizeof (GLOActionGroupPrivate)); +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif +} + +static void +g_lo_action_group_iface_init (GActionGroupInterface *iface) +{ + iface->list_actions = g_lo_action_group_list_actions; + iface->query_action = g_lo_action_group_query_action; + iface->change_action_state = g_lo_action_group_change_state; + iface->activate_action = g_lo_action_group_activate; +} + +GLOActionGroup * +g_lo_action_group_new() +{ + GLOActionGroup* group = G_LO_ACTION_GROUP (g_object_new (G_TYPE_LO_ACTION_GROUP, nullptr)); + return group; +} + +void +g_lo_action_group_set_action_enabled (GLOActionGroup *group, + const gchar *action_name, + gboolean enabled) +{ + SAL_INFO("vcl.unity", "g_lo_action_group_set_action_enabled on " << group); + g_return_if_fail (G_IS_LO_ACTION_GROUP (group)); + g_return_if_fail (action_name != nullptr); + + GLOAction* action = G_LO_ACTION (g_hash_table_lookup (group->priv->table, action_name)); + + if (action == nullptr) + return; + + action->enabled = enabled; + + g_action_group_action_enabled_changed (G_ACTION_GROUP (group), action_name, enabled); +} + +void +g_lo_action_group_remove (GLOActionGroup *group, + const gchar *action_name) +{ + SAL_INFO("vcl.unity", "g_lo_action_group_remove on " << group); + g_return_if_fail (G_IS_LO_ACTION_GROUP (group)); + + if (action_name != nullptr) + { + g_action_group_action_removed (G_ACTION_GROUP (group), action_name); + g_hash_table_remove (group->priv->table, action_name); + } +} + +void +g_lo_action_group_clear (GLOActionGroup *group) +{ + SAL_INFO("vcl.unity", "g_lo_action_group_clear on " << group); + g_return_if_fail (G_IS_LO_ACTION_GROUP (group)); + + GList* keys = g_hash_table_get_keys (group->priv->table); + + for (GList* element = g_list_first (keys); element != nullptr; element = g_list_next (element)) + { + g_lo_action_group_remove (group, static_cast<gchar*>(element->data)); + } + + g_list_free (keys); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3glomenu.cxx b/vcl/unx/gtk3/gtk3glomenu.cxx index e894b09320fd..f20903d0bd5a 100644 --- a/vcl/unx/gtk3/gtk3glomenu.cxx +++ b/vcl/unx/gtk3/gtk3glomenu.cxx @@ -1,5 +1,686 @@ /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ -#include "../gtk/glomenu.cxx" +#include <string.h> + +#include <unx/gtk/gtksalmenu.hxx> +#include <unx/gtk/glomenu.h> + +struct GLOMenu +{ + GMenuModel const parent_instance; + + GArray *items; +}; + +typedef GMenuModelClass GLOMenuClass; + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif +G_DEFINE_TYPE (GLOMenu, g_lo_menu, G_TYPE_MENU_MODEL); +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + +struct item +{ + GHashTable* attributes; // Item attributes. + GHashTable* links; // Item links. +}; + +static void +g_lo_menu_struct_item_init (struct item *menu_item) +{ + menu_item->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, reinterpret_cast<GDestroyNotify>(g_variant_unref)); + menu_item->links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); +} + +/* We treat attribute names the same as GSettings keys: + * - only lowercase ascii, digits and '-' + * - must start with lowercase + * - must not end with '-' + * - no consecutive '-' + * - not longer than 1024 chars + */ +static gboolean +valid_attribute_name (const gchar *name) +{ + gint i; + + if (!g_ascii_islower (name[0])) + return FALSE; + + for (i = 1; name[i]; i++) + { + if (name[i] != '-' && + !g_ascii_islower (name[i]) && + !g_ascii_isdigit (name[i])) + return FALSE; + + if (name[i] == '-' && name[i + 1] == '-') + return FALSE; + } + + if (name[i - 1] == '-') + return FALSE; + + if (i > 1024) + return FALSE; + + return TRUE; +} + +/* + * GLOMenu + */ + +static gboolean +g_lo_menu_is_mutable (GMenuModel*) +{ + // Menu is always mutable. + return TRUE; +} + +static gint +g_lo_menu_get_n_items (GMenuModel *model) +{ + g_return_val_if_fail (model != nullptr, 0); + GLOMenu *menu = G_LO_MENU (model); + g_return_val_if_fail (menu->items != nullptr, 0); + + return menu->items->len; +} + +gint +g_lo_menu_get_n_items_from_section (GLOMenu *menu, + gint section) +{ + g_return_val_if_fail (0 <= section && section < static_cast<gint>(menu->items->len), 0); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_val_if_fail (model != nullptr, 0); + + gint length = model->items->len; + + g_object_unref (model); + + return length; +} + +static void +g_lo_menu_get_item_attributes (GMenuModel *model, + gint position, + GHashTable **table) +{ + GLOMenu *menu = G_LO_MENU (model); + *table = g_hash_table_ref (g_array_index (menu->items, struct item, position).attributes); +} + +static void +g_lo_menu_get_item_links (GMenuModel *model, + gint position, + GHashTable **table) +{ + GLOMenu *menu = G_LO_MENU (model); + *table = g_hash_table_ref (g_array_index (menu->items, struct item, position).links); +} + +void +g_lo_menu_insert (GLOMenu *menu, + gint position, + const gchar *label) +{ + g_lo_menu_insert_section (menu, position, label, nullptr); +} + +void +g_lo_menu_insert_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *label) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + g_return_if_fail (0 <= section && section < static_cast<gint>(menu->items->len)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + g_lo_menu_insert (model, position, label); + + g_object_unref (model); +} + +GLOMenu * +g_lo_menu_new() +{ + return G_LO_MENU( g_object_new (G_TYPE_LO_MENU, nullptr) ); +} + +static void +g_lo_menu_set_attribute_value (GLOMenu *menu, + gint position, + const gchar *attribute, + GVariant *value) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + g_return_if_fail (attribute != nullptr); + g_return_if_fail (valid_attribute_name (attribute)); + + if (position >= static_cast<gint>(menu->items->len)) + return; + + struct item menu_item = g_array_index (menu->items, struct item, position); + + if (value != nullptr) + g_hash_table_insert (menu_item.attributes, g_strdup (attribute), g_variant_ref_sink (value)); + else + g_hash_table_remove (menu_item.attributes, attribute); +} + +static GVariant* +g_lo_menu_get_attribute_value_from_item_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *attribute, + const GVariantType *type) +{ + GMenuModel *model = G_MENU_MODEL (g_lo_menu_get_section (menu, section)); + + g_return_val_if_fail (model != nullptr, nullptr); + + GVariant *value = g_menu_model_get_item_attribute_value (model, + position, + attribute, + type); + + g_object_unref (model); + + return value; +} + +void +g_lo_menu_set_label (GLOMenu *menu, + gint position, + const gchar *label) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GVariant *value; + + if (label != nullptr) + value = g_variant_new_string (label); + else + value = nullptr; + + g_lo_menu_set_attribute_value (menu, position, G_MENU_ATTRIBUTE_LABEL, value); +} + +void +g_lo_menu_set_icon (GLOMenu *menu, + gint position, + const GIcon *icon) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GVariant *value; + + if (icon != nullptr) + { +#if GLIB_CHECK_VERSION(2,38,0) + value = g_icon_serialize (const_cast<GIcon*>(icon)); +#else + value = nullptr; +#endif + } + else + value = nullptr; + +#ifndef G_MENU_ATTRIBUTE_ICON +# define G_MENU_ATTRIBUTE_ICON "icon" +#endif + + g_lo_menu_set_attribute_value (menu, position, G_MENU_ATTRIBUTE_ICON, value); + if (value) + g_variant_unref (value); +} + +void +g_lo_menu_set_label_to_item_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *label) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + g_lo_menu_set_label (model, position, label); + + // Notify the update. + g_menu_model_items_changed (G_MENU_MODEL (model), position, 1, 1); + + g_object_unref (model); +} + +void +g_lo_menu_set_icon_to_item_in_section (GLOMenu *menu, + gint section, + gint position, + const GIcon *icon) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + g_lo_menu_set_icon (model, position, icon); + + // Notify the update. + g_menu_model_items_changed (G_MENU_MODEL (model), position, 1, 1); + + g_object_unref (model); +} + +gchar * +g_lo_menu_get_label_from_item_in_section (GLOMenu *menu, + gint section, + gint position) +{ + g_return_val_if_fail (G_IS_LO_MENU (menu), nullptr); + + GVariant *label_value = g_lo_menu_get_attribute_value_from_item_in_section (menu, + section, + position, + G_MENU_ATTRIBUTE_LABEL, + G_VARIANT_TYPE_STRING); + + gchar *label = nullptr; + + if (label_value) + { + label = g_variant_dup_string (label_value, nullptr); + g_variant_unref (label_value); + } + + return label; +} + +void +g_lo_menu_set_action_and_target_value (GLOMenu *menu, + gint position, + const gchar *action, + GVariant *target_value) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GVariant *action_value; + + if (action != nullptr) + { + action_value = g_variant_new_string (action); + } + else + { + action_value = nullptr; + target_value = nullptr; + } + + g_lo_menu_set_attribute_value (menu, position, G_MENU_ATTRIBUTE_ACTION, action_value); + g_lo_menu_set_attribute_value (menu, position, G_MENU_ATTRIBUTE_TARGET, target_value); + g_lo_menu_set_attribute_value (menu, position, G_LO_MENU_ATTRIBUTE_SUBMENU_ACTION, nullptr); + + g_menu_model_items_changed (G_MENU_MODEL (menu), position, 1, 1); +} + +void +g_lo_menu_set_action_and_target_value_to_item_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *command, + GVariant *target_value) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + g_lo_menu_set_action_and_target_value (model, position, command, target_value); + + g_object_unref (model); +} + +void +g_lo_menu_set_accelerator_to_item_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *accelerator) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + GVariant *value; + + if (accelerator != nullptr) + value = g_variant_new_string (accelerator); + else + value = nullptr; + + g_lo_menu_set_attribute_value (model, position, G_LO_MENU_ATTRIBUTE_ACCELERATOR, value); + + // Notify the update. + g_menu_model_items_changed (G_MENU_MODEL (model), position, 1, 1); + + g_object_unref (model); +} + +gchar * +g_lo_menu_get_accelerator_from_item_in_section (GLOMenu *menu, + gint section, + gint position) +{ + g_return_val_if_fail (G_IS_LO_MENU (menu), nullptr); + + GVariant *accel_value = g_lo_menu_get_attribute_value_from_item_in_section (menu, + section, + position, + G_LO_MENU_ATTRIBUTE_ACCELERATOR, + G_VARIANT_TYPE_STRING); + + gchar *accel = nullptr; + + if (accel_value != nullptr) + { + accel = g_variant_dup_string (accel_value, nullptr); + g_variant_unref (accel_value); + } + + return accel; +} + +void +g_lo_menu_set_command_to_item_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *command) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + GVariant *value; + + if (command != nullptr) + value = g_variant_new_string (command); + else + value = nullptr; + + g_lo_menu_set_attribute_value (model, position, G_LO_MENU_ATTRIBUTE_COMMAND, value); + + // Notify the update. + g_menu_model_items_changed (G_MENU_MODEL (model), position, 1, 1); + + g_object_unref (model); +} + +gchar * +g_lo_menu_get_command_from_item_in_section (GLOMenu *menu, + gint section, + gint position) +{ + g_return_val_if_fail (G_IS_LO_MENU (menu), nullptr); + + GVariant *command_value = g_lo_menu_get_attribute_value_from_item_in_section (menu, + section, + position, + G_LO_MENU_ATTRIBUTE_COMMAND, + G_VARIANT_TYPE_STRING); + + gchar *command = nullptr; + + if (command_value != nullptr) + { + command = g_variant_dup_string (command_value, nullptr); + g_variant_unref (command_value); + } + + return command; +} + +static void +g_lo_menu_set_link (GLOMenu *menu, + gint position, + const gchar *link, + GMenuModel *model) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + g_return_if_fail (link != nullptr); + g_return_if_fail (valid_attribute_name (link)); + + if (position < 0 || position >= static_cast<gint>(menu->items->len)) + position = menu->items->len - 1; + + struct item menu_item = g_array_index (menu->items, struct item, position); + + if (model != nullptr) + g_hash_table_insert (menu_item.links, g_strdup (link), g_object_ref (model)); + else + g_hash_table_remove (menu_item.links, link); +} + +void +g_lo_menu_insert_section (GLOMenu *menu, + gint position, + const gchar *label, + GMenuModel *section) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + if (position < 0 || position > static_cast<gint>(menu->items->len)) + position = menu->items->len; + + struct item menu_item; + + g_lo_menu_struct_item_init(&menu_item); + + g_array_insert_val (menu->items, position, menu_item); + + g_lo_menu_set_label (menu, position, label); + g_lo_menu_set_link (menu, position, G_MENU_LINK_SECTION, section); + + g_menu_model_items_changed (G_MENU_MODEL (menu), position, 0, 1); +} + +void +g_lo_menu_new_section (GLOMenu *menu, + gint position, + const gchar *label) +{ + GMenuModel *section = G_MENU_MODEL (g_lo_menu_new()); + + g_lo_menu_insert_section (menu, position, label, section); + + g_object_unref (section); +} + +GLOMenu * +g_lo_menu_get_section (GLOMenu *menu, + gint section) +{ + g_return_val_if_fail (G_IS_LO_MENU (menu), nullptr); + + return G_LO_MENU (G_MENU_MODEL_CLASS (g_lo_menu_parent_class) + ->get_item_link (G_MENU_MODEL (menu), section, G_MENU_LINK_SECTION)); +} + +void +g_lo_menu_new_submenu_in_item_in_section (GLOMenu *menu, + gint section, + gint position) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + g_return_if_fail (0 <= section && section < static_cast<gint>(menu->items->len)); + + GLOMenu* model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + if (0 <= position && position < static_cast<gint>(model->items->len)) { + GMenuModel* submenu = G_MENU_MODEL (g_lo_menu_new()); + + g_lo_menu_set_link (model, position, G_MENU_LINK_SUBMENU, submenu); + + g_object_unref (submenu); + + g_menu_model_items_changed (G_MENU_MODEL (model), position, 1, 1); + + g_object_unref (model); + } +} + +GLOMenu * +g_lo_menu_get_submenu_from_item_in_section (GLOMenu *menu, + gint section, + gint position) +{ + g_return_val_if_fail (G_IS_LO_MENU (menu), nullptr); + g_return_val_if_fail (0 <= section && section < static_cast<gint>(menu->items->len), nullptr); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_val_if_fail (model != nullptr, nullptr); + + GLOMenu *submenu = nullptr; + + if (0 <= position && position < static_cast<gint>(model->items->len)) + submenu = G_LO_MENU (G_MENU_MODEL_CLASS (g_lo_menu_parent_class) + ->get_item_link (G_MENU_MODEL (model), position, G_MENU_LINK_SUBMENU)); + //submenu = g_menu_model_get_item_link (G_MENU_MODEL (model), position, G_MENU_LINK_SUBMENU); + + g_object_unref (model); + + return submenu; +} + +void +g_lo_menu_set_submenu_action_to_item_in_section (GLOMenu *menu, + gint section, + gint position, + const gchar *action) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + + GMenuModel *model = G_MENU_MODEL (g_lo_menu_get_section (menu, section)); + + g_return_if_fail (model != nullptr); + + GVariant *value; + + if (action != nullptr) + value = g_variant_new_string (action); + else + value = nullptr; + + g_lo_menu_set_attribute_value (G_LO_MENU (model), position, G_LO_MENU_ATTRIBUTE_SUBMENU_ACTION, value); + + // Notify the update. + g_menu_model_items_changed (model, position, 1, 1); + + g_object_unref (model); +} + +static void +g_lo_menu_clear_item (struct item *menu_item) +{ + if (menu_item->attributes != nullptr) + g_hash_table_unref (menu_item->attributes); + if (menu_item->links != nullptr) + g_hash_table_unref (menu_item->links); +} + +void +g_lo_menu_remove (GLOMenu *menu, + gint position) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + g_return_if_fail (0 <= position && position < static_cast<gint>(menu->items->len)); + + g_lo_menu_clear_item (&g_array_index (menu->items, struct item, position)); + g_array_remove_index (menu->items, position); + g_menu_model_items_changed (G_MENU_MODEL (menu), position, 1, 0); +} + +void +g_lo_menu_remove_from_section (GLOMenu *menu, + gint section, + gint position) +{ + g_return_if_fail (G_IS_LO_MENU (menu)); + g_return_if_fail (0 <= section && section < static_cast<gint>(menu->items->len)); + + GLOMenu *model = g_lo_menu_get_section (menu, section); + + g_return_if_fail (model != nullptr); + + g_lo_menu_remove (model, position); + + g_object_unref (model); +} + +static void +g_lo_menu_finalize (GObject *object) +{ + GLOMenu *menu = G_LO_MENU (object); + struct item *items; + gint n_items; + gint i; + + n_items = menu->items->len; + items = reinterpret_cast<struct item *>(g_array_free (menu->items, FALSE)); + for (i = 0; i < n_items; i++) + g_lo_menu_clear_item (&items[i]); + g_free (items); + + G_OBJECT_CLASS (g_lo_menu_parent_class) + ->finalize (object); +} + +static void +g_lo_menu_init (GLOMenu *menu) +{ + menu->items = g_array_new (FALSE, FALSE, sizeof (struct item)); +} + +static void +g_lo_menu_class_init (GLOMenuClass *klass) +{ + GMenuModelClass *model_class = G_MENU_MODEL_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = g_lo_menu_finalize; + + model_class->is_mutable = g_lo_menu_is_mutable; + model_class->get_n_items = g_lo_menu_get_n_items; + model_class->get_item_attributes = g_lo_menu_get_item_attributes; + model_class->get_item_links = g_lo_menu_get_item_links; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3gtkinst.cxx b/vcl/unx/gtk3/gtk3gtkinst.cxx index 333db6693d13..f5e26be83b59 100644 --- a/vcl/unx/gtk3/gtk3gtkinst.cxx +++ b/vcl/unx/gtk3/gtk3gtkinst.cxx @@ -7,8 +7,35 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "../gtk/gtkinst.cxx" -#include "../gtk/a11y/atkwrapper.hxx" +#include <stack> +#include <string.h> +#include <osl/process.h> +#include <unx/gtk/gtkdata.hxx> +#include <unx/gtk/gtkinst.hxx> +#include <unx/salobj.h> +#include <unx/gtk/gtkgdi.hxx> +#include <unx/gtk/gtkframe.hxx> +#include <unx/gtk/gtkobject.hxx> +#include <unx/gtk/atkbridge.hxx> +#include <unx/gtk/gtkprn.hxx> +#include <unx/gtk/gtksalmenu.hxx> +#include <headless/svpvd.hxx> +#include <headless/svpbmp.hxx> +#include <vcl/inputtypes.hxx> +#include <unx/genpspgraphics.h> +#include <rtl/strbuf.hxx> +#include <sal/log.hxx> +#include <rtl/uri.hxx> + +#include <vcl/settings.hxx> + +#include <dlfcn.h> +#include <fcntl.h> +#include <unistd.h> + +#include <unx/gtk/gtkprintwrapper.hxx> + +#include "a11y/atkwrapper.hxx" #include <com/sun/star/lang/IllegalArgumentException.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/lang/XServiceInfo.hpp> @@ -56,6 +83,376 @@ using namespace com::sun::star; using namespace com::sun::star::uno; using namespace com::sun::star::lang; +extern "C" +{ + #define GET_YIELD_MUTEX() static_cast<GtkYieldMutex*>(GetSalData()->m_pInstance->GetYieldMutex()) + static void GdkThreadsEnter() + { + GtkYieldMutex *pYieldMutex = GET_YIELD_MUTEX(); + pYieldMutex->ThreadsEnter(); + } + static void GdkThreadsLeave() + { + GtkYieldMutex *pYieldMutex = GET_YIELD_MUTEX(); + pYieldMutex->ThreadsLeave(); + } + + VCLPLUG_GTK_PUBLIC SalInstance* create_SalInstance() + { + SAL_INFO( + "vcl.gtk", + "create vcl plugin instance with gtk version " << gtk_major_version + << " " << gtk_minor_version << " " << gtk_micro_version); + + if (gtk_major_version == 3 && gtk_minor_version < 18) + { + g_warning("require gtk >= 3.18 for theme expectations"); + return nullptr; + } + + // for gtk2 it is always built with X support, so this is always called + // for gtk3 it is normally built with X and Wayland support, if + // X is supported GDK_WINDOWING_X11 is defined and this is always + // called, regardless of if we're running under X or Wayland. + // We can't use (DLSYM_GDK_IS_X11_DISPLAY(pDisplay)) to only do it under + // X, because we need to do it earlier than we have a display +#if defined(GDK_WINDOWING_X11) + /* #i92121# workaround deadlocks in the X11 implementation + */ + static const char* pNoXInitThreads = getenv( "SAL_NO_XINITTHREADS" ); + /* #i90094# + from now on we know that an X connection will be + established, so protect X against itself + */ + if( ! ( pNoXInitThreads && *pNoXInitThreads ) ) + XInitThreads(); +#endif + + // init gdk thread protection + bool const sup = g_thread_supported(); + // extracted from the 'if' to avoid Clang -Wunreachable-code + if ( !sup ) + g_thread_init( nullptr ); + + gdk_threads_set_lock_functions (GdkThreadsEnter, GdkThreadsLeave); + SAL_INFO("vcl.gtk", "Hooked gdk threads locks"); + + auto pYieldMutex = std::make_unique<GtkYieldMutex>(); + + gdk_threads_init(); + + GtkInstance* pInstance = new GtkInstance( std::move(pYieldMutex) ); + SAL_INFO("vcl.gtk", "creating GtkInstance " << pInstance); + + // Create SalData, this does not leak + new GtkSalData( pInstance ); + + return pInstance; + } +} + +static VclInputFlags categorizeEvent(const GdkEvent *pEvent) +{ + VclInputFlags nType = VclInputFlags::NONE; + switch( pEvent->type ) + { + case GDK_MOTION_NOTIFY: + case GDK_BUTTON_PRESS: + case GDK_2BUTTON_PRESS: + case GDK_3BUTTON_PRESS: + case GDK_BUTTON_RELEASE: + case GDK_ENTER_NOTIFY: + case GDK_LEAVE_NOTIFY: + case GDK_SCROLL: + nType = VclInputFlags::MOUSE; + break; + case GDK_KEY_PRESS: + // case GDK_KEY_RELEASE: //similar to the X11SalInstance one + nType = VclInputFlags::KEYBOARD; + break; + case GDK_EXPOSE: + nType = VclInputFlags::PAINT; + break; + default: + nType = VclInputFlags::OTHER; + break; + } + return nType; +} + +GtkInstance::GtkInstance( std::unique_ptr<SalYieldMutex> pMutex ) + : SvpSalInstance( std::move(pMutex) ) + , m_pTimer(nullptr) + , bNeedsInit(true) + , m_pLastCairoFontOptions(nullptr) +{ +} + +//We want to defer initializing gtk until we are after uno has been +//bootstrapped so we can ask the config what the UI language is so that we can +//force that in as $LANGUAGE to get gtk to render widgets RTL if we have a RTL +//UI in a LTR locale +void GtkInstance::AfterAppInit() +{ + EnsureInit(); +} + +void GtkInstance::EnsureInit() +{ + if (!bNeedsInit) + return; + // initialize SalData + GtkSalData *pSalData = GetGtkSalData(); + pSalData->Init(); + GtkSalData::initNWF(); + + InitAtkBridge(); + + ImplSVData* pSVData = ImplGetSVData(); +#ifdef GTK_TOOLKIT_NAME + pSVData->maAppData.mxToolkitName = OUString(GTK_TOOLKIT_NAME); +#else + pSVData->maAppData.mxToolkitName = OUString("gtk3"); +#endif + + bNeedsInit = false; +} + +GtkInstance::~GtkInstance() +{ + assert( nullptr == m_pTimer ); + DeInitAtkBridge(); + ResetLastSeenCairoFontOptions(nullptr); +} + +SalFrame* GtkInstance::CreateFrame( SalFrame* pParent, SalFrameStyleFlags nStyle ) +{ + EnsureInit(); + return new GtkSalFrame( pParent, nStyle ); +} + +SalFrame* GtkInstance::CreateChildFrame( SystemParentData* pParentData, SalFrameStyleFlags ) +{ + EnsureInit(); + return new GtkSalFrame( pParentData ); +} + +SalObject* GtkInstance::CreateObject( SalFrame* pParent, SystemWindowData* /*pWindowData*/, bool bShow ) +{ + EnsureInit(); + //FIXME: Missing CreateObject functionality ... + return new GtkSalObject( static_cast<GtkSalFrame*>(pParent), bShow ); +} + +extern "C" +{ + typedef void*(* getDefaultFnc)(); + typedef void(* addItemFnc)(void *, const char *); +} + +void GtkInstance::AddToRecentDocumentList(const OUString& rFileUrl, const OUString&, const OUString&) +{ + EnsureInit(); + OString sGtkURL; + rtl_TextEncoding aSystemEnc = osl_getThreadTextEncoding(); + if ((aSystemEnc == RTL_TEXTENCODING_UTF8) || !rFileUrl.startsWith( "file://" )) + sGtkURL = OUStringToOString(rFileUrl, RTL_TEXTENCODING_UTF8); + else + { + //Non-utf8 locales are a bad idea if trying to work with non-ascii filenames + //Decode %XX components + OUString sDecodedUri = rtl::Uri::decode(rFileUrl.copy(7), rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8); + //Convert back to system locale encoding + OString sSystemUrl = OUStringToOString(sDecodedUri, aSystemEnc); + //Encode to an escaped ASCII-encoded URI + gchar *g_uri = g_filename_to_uri(sSystemUrl.getStr(), nullptr, nullptr); + sGtkURL = OString(g_uri); + g_free(g_uri); + } + GtkRecentManager *manager = gtk_recent_manager_get_default (); + gtk_recent_manager_add_item (manager, sGtkURL.getStr()); +} + +SalInfoPrinter* GtkInstance::CreateInfoPrinter( SalPrinterQueueInfo* pQueueInfo, + ImplJobSetup* pSetupData ) +{ + EnsureInit(); + mbPrinterInit = true; + // create and initialize SalInfoPrinter + PspSalInfoPrinter* pPrinter = new GtkSalInfoPrinter; + configurePspInfoPrinter(pPrinter, pQueueInfo, pSetupData); + return pPrinter; +} + +std::unique_ptr<SalPrinter> GtkInstance::CreatePrinter( SalInfoPrinter* pInfoPrinter ) +{ + EnsureInit(); + mbPrinterInit = true; + return std::unique_ptr<SalPrinter>(new GtkSalPrinter( pInfoPrinter )); +} + +/* + * These methods always occur in pairs + * A ThreadsEnter is followed by a ThreadsLeave + * We need to queue up the recursive lock count + * for each pair, so we can accurately restore + * it later. + */ +thread_local std::stack<sal_uInt32> GtkYieldMutex::yieldCounts; + +void GtkYieldMutex::ThreadsEnter() +{ + acquire(); + if (!yieldCounts.empty()) { + auto n = yieldCounts.top(); + yieldCounts.pop(); + assert(n > 0); + n--; + if (n > 0) + acquire(n); + } +} + +void GtkYieldMutex::ThreadsLeave() +{ + assert(m_nCount != 0); + yieldCounts.push(m_nCount); + release(true); +} + +std::unique_ptr<SalVirtualDevice> GtkInstance::CreateVirtualDevice( SalGraphics *pG, + long &nDX, long &nDY, + DeviceFormat eFormat, + const SystemGraphicsData* /*pGd*/ ) +{ + EnsureInit(); + SvpSalGraphics *pSvpSalGraphics = dynamic_cast<SvpSalGraphics*>(pG); + assert(pSvpSalGraphics); + std::unique_ptr<SalVirtualDevice> pNew(new SvpSalVirtualDevice(eFormat, pSvpSalGraphics->getSurface())); + pNew->SetSize( nDX, nDY ); + return pNew; +} + +std::shared_ptr<SalBitmap> GtkInstance::CreateSalBitmap() +{ + EnsureInit(); + return SvpSalInstance::CreateSalBitmap(); +} + +std::unique_ptr<SalMenu> GtkInstance::CreateMenu( bool bMenuBar, Menu* pVCLMenu ) +{ + EnsureInit(); + GtkSalMenu* pSalMenu = new GtkSalMenu( bMenuBar ); + pSalMenu->SetMenu( pVCLMenu ); + return std::unique_ptr<SalMenu>(pSalMenu); +} + +std::unique_ptr<SalMenuItem> GtkInstance::CreateMenuItem( const SalItemParams & rItemData ) +{ + EnsureInit(); + return std::unique_ptr<SalMenuItem>(new GtkSalMenuItem( &rItemData )); +} + +SalTimer* GtkInstance::CreateSalTimer() +{ + EnsureInit(); + assert( nullptr == m_pTimer ); + if ( nullptr == m_pTimer ) + m_pTimer = new GtkSalTimer(); + return m_pTimer; +} + +void GtkInstance::RemoveTimer () +{ + EnsureInit(); + m_pTimer = nullptr; +} + +bool GtkInstance::DoYield(bool bWait, bool bHandleAllCurrentEvents) +{ + EnsureInit(); + return GetGtkSalData()->Yield( bWait, bHandleAllCurrentEvents ); +} + +bool GtkInstance::IsTimerExpired() +{ + EnsureInit(); + return (m_pTimer && m_pTimer->Expired()); +} + +bool GtkInstance::AnyInput( VclInputFlags nType ) +{ + EnsureInit(); + if( (nType & VclInputFlags::TIMER) && IsTimerExpired() ) + return true; + if (!gdk_events_pending()) + return false; + + if (nType == VCL_INPUT_ANY) + return true; + + bool bRet = false; + std::stack<GdkEvent*> aEvents; + GdkEvent *pEvent = nullptr; + while ((pEvent = gdk_event_get())) + { + aEvents.push(pEvent); + VclInputFlags nEventType = categorizeEvent(pEvent); + if ( (nEventType & nType) || ( nEventType == VclInputFlags::NONE && (nType & VclInputFlags::OTHER) ) ) + { + bRet = true; + break; + } + } + + while (!aEvents.empty()) + { + pEvent = aEvents.top(); + gdk_event_put(pEvent); + gdk_event_free(pEvent); + aEvents.pop(); + } + return bRet; +} + +std::unique_ptr<GenPspGraphics> GtkInstance::CreatePrintGraphics() +{ + EnsureInit(); + return std::make_unique<GenPspGraphics>(); +} + +std::shared_ptr<vcl::unx::GtkPrintWrapper> const & +GtkInstance::getPrintWrapper() const +{ + if (!m_xPrintWrapper) + m_xPrintWrapper.reset(new vcl::unx::GtkPrintWrapper); + return m_xPrintWrapper; +} + +const cairo_font_options_t* GtkInstance::GetCairoFontOptions() +{ + const cairo_font_options_t* pCairoFontOptions = gdk_screen_get_font_options(gdk_screen_get_default()); + if (!m_pLastCairoFontOptions && pCairoFontOptions) + m_pLastCairoFontOptions = cairo_font_options_copy(pCairoFontOptions); + return pCairoFontOptions; +} + +const cairo_font_options_t* GtkInstance::GetLastSeenCairoFontOptions() const +{ + return m_pLastCairoFontOptions; +} + +void GtkInstance::ResetLastSeenCairoFontOptions(const cairo_font_options_t* pCairoFontOptions) +{ + if (m_pLastCairoFontOptions) + cairo_font_options_destroy(m_pLastCairoFontOptions); + if (pCairoFontOptions) + m_pLastCairoFontOptions = cairo_font_options_copy(pCairoFontOptions); + else + m_pLastCairoFontOptions = nullptr; +} + + namespace { struct TypeEntry diff --git a/vcl/unx/gtk3/gtk3gtkprintwrapper.cxx b/vcl/unx/gtk3/gtk3gtkprintwrapper.cxx index edefca9626ad..b1a8b9c66d13 100644 --- a/vcl/unx/gtk3/gtk3gtkprintwrapper.cxx +++ b/vcl/unx/gtk3/gtk3gtkprintwrapper.cxx @@ -7,6 +7,154 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "../gtk/gtkprintwrapper.cxx" +#include <cassert> + +#include <rtl/ustring.hxx> + +#include <unx/gtk/gtkprintwrapper.hxx> + +namespace vcl +{ +namespace unx +{ + +GtkPrintWrapper::GtkPrintWrapper() +{ +} + +GtkPrintWrapper::~GtkPrintWrapper() +{ +} + +bool GtkPrintWrapper::supportsPrinting() const +{ + (void) this; // loplugin:staticmethods + return true; +} + +bool GtkPrintWrapper::supportsPrintSelection() const +{ + (void) this; // loplugin:staticmethods + return true; +} + +GtkPageSetup* GtkPrintWrapper::page_setup_new() const +{ + (void) this; // loplugin:staticmethods + return gtk_page_setup_new(); +} + +GtkPrintJob* GtkPrintWrapper::print_job_new(const gchar* title, GtkPrinter* printer, GtkPrintSettings* settings, GtkPageSetup* page_setup) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_job_new(title, printer, settings, page_setup); +} + +void GtkPrintWrapper::print_job_send(GtkPrintJob* job, GtkPrintJobCompleteFunc callback, gpointer user_data, GDestroyNotify dnotify) const +{ + (void) this; // loplugin:staticmethods + gtk_print_job_send(job, callback, user_data, dnotify); +} + +gboolean GtkPrintWrapper::print_job_set_source_file(GtkPrintJob* job, const gchar* filename, GError** error) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_job_set_source_file(job, filename, error); +} + +const gchar* GtkPrintWrapper::print_settings_get(GtkPrintSettings* settings, const gchar* key) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_settings_get(settings, key); +} + +gboolean GtkPrintWrapper::print_settings_get_collate(GtkPrintSettings* settings) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_settings_get_collate(settings); +} + +void GtkPrintWrapper::print_settings_set_collate(GtkPrintSettings* settings, gboolean collate) const +{ + (void) this; // loplugin:staticmethods + gtk_print_settings_set_collate(settings, collate); +} + +gint GtkPrintWrapper::print_settings_get_n_copies(GtkPrintSettings* settings) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_settings_get_n_copies(settings); +} + +void GtkPrintWrapper::print_settings_set_n_copies(GtkPrintSettings* settings, gint num_copies) const +{ + (void) this; // loplugin:staticmethods + gtk_print_settings_set_n_copies(settings, num_copies); +} + +GtkPageRange* GtkPrintWrapper::print_settings_get_page_ranges(GtkPrintSettings* settings, gint* num_ranges) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_settings_get_page_ranges(settings, num_ranges); +} + +void GtkPrintWrapper::print_settings_set_print_pages(GtkPrintSettings* settings, GtkPrintPages pages) const +{ + (void) this; // loplugin:staticmethods + gtk_print_settings_set_print_pages(settings, pages); +} + +GtkWidget* GtkPrintWrapper::print_unix_dialog_new() const +{ + (void) this; // loplugin:staticmethods + return gtk_print_unix_dialog_new(nullptr, nullptr); +} + +void GtkPrintWrapper::print_unix_dialog_add_custom_tab(GtkPrintUnixDialog* dialog, GtkWidget* child, GtkWidget* tab_label) const +{ + (void) this; // loplugin:staticmethods + gtk_print_unix_dialog_add_custom_tab(dialog, child, tab_label); +} + +GtkPrinter* GtkPrintWrapper::print_unix_dialog_get_selected_printer(GtkPrintUnixDialog* dialog) const +{ + (void) this; // loplugin:staticmethods + GtkPrinter* pRet = gtk_print_unix_dialog_get_selected_printer(dialog); + g_object_ref(G_OBJECT(pRet)); + return pRet; +} + +void GtkPrintWrapper::print_unix_dialog_set_manual_capabilities(GtkPrintUnixDialog* dialog, GtkPrintCapabilities capabilities) const +{ + (void) this; // loplugin:staticmethods + gtk_print_unix_dialog_set_manual_capabilities(dialog, capabilities); +} + +GtkPrintSettings* GtkPrintWrapper::print_unix_dialog_get_settings(GtkPrintUnixDialog* dialog) const +{ + (void) this; // loplugin:staticmethods + return gtk_print_unix_dialog_get_settings(dialog); +} + +void GtkPrintWrapper::print_unix_dialog_set_settings(GtkPrintUnixDialog* dialog, GtkPrintSettings* settings) const +{ + (void) this; // loplugin:staticmethods + gtk_print_unix_dialog_set_settings(dialog, settings); +} + +void GtkPrintWrapper::print_unix_dialog_set_support_selection(GtkPrintUnixDialog* dialog, gboolean support_selection) const +{ + (void) this; // loplugin:staticmethods + gtk_print_unix_dialog_set_support_selection(dialog, support_selection); +} + +void GtkPrintWrapper::print_unix_dialog_set_has_selection(GtkPrintUnixDialog* dialog, gboolean has_selection) const +{ + (void) this; // loplugin:staticmethods + gtk_print_unix_dialog_set_has_selection(dialog, has_selection); +} + +} +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3gtksalmenu.cxx b/vcl/unx/gtk3/gtk3gtksalmenu.cxx index d465594fc9bc..81d5d650dd26 100644 --- a/vcl/unx/gtk3/gtk3gtksalmenu.cxx +++ b/vcl/unx/gtk3/gtk3gtksalmenu.cxx @@ -1,5 +1,1396 @@ /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ -#include "../gtk/gtksalmenu.cxx" +#include <unx/gtk/gtksalmenu.hxx> + +#include <unx/gendata.hxx> +#include <unx/saldisp.hxx> +#include <unx/gtk/gtkdata.hxx> +#include <unx/gtk/glomenu.h> +#include <unx/gtk/gloactiongroup.h> +#include <vcl/floatwin.hxx> +#include <vcl/menu.hxx> +#include <vcl/pngwrite.hxx> +#include <unx/gtk/gtkinst.hxx> + +#include <sal/log.hxx> +#include <tools/stream.hxx> +#include <window.h> +#include <strings.hrc> + +static bool bUnityMode = false; + +/* + * This function generates a unique command name for each menu item + */ +static gchar* GetCommandForItem(GtkSalMenu* pParentMenu, sal_uInt16 nItemId) +{ + OString aCommand("window-"); + aCommand = aCommand + OString::number(reinterpret_cast<unsigned long>(pParentMenu)); + aCommand = aCommand + "-" + OString::number(nItemId); + return g_strdup(aCommand.getStr()); +} + +static gchar* GetCommandForItem(GtkSalMenuItem* pSalMenuItem) +{ + return GetCommandForItem(pSalMenuItem->mpParentMenu, + pSalMenuItem->mnId); +} + +bool GtkSalMenu::PrepUpdate() +{ + return mpMenuModel && mpActionGroup; +} + +/* + * Menu updating methods + */ + +static void RemoveSpareItemsFromNativeMenu( GLOMenu* pMenu, GList** pOldCommandList, unsigned nSection, unsigned nValidItems ) +{ + sal_Int32 nSectionItems = g_lo_menu_get_n_items_from_section( pMenu, nSection ); + + while ( nSectionItems > static_cast<sal_Int32>(nValidItems) ) + { + gchar* aCommand = g_lo_menu_get_command_from_item_in_section( pMenu, nSection, --nSectionItems ); + + if ( aCommand != nullptr && pOldCommandList != nullptr ) + *pOldCommandList = g_list_append( *pOldCommandList, g_strdup( aCommand ) ); + + g_free( aCommand ); + + g_lo_menu_remove_from_section( pMenu, nSection, nSectionItems ); + } +} + +typedef std::pair<GtkSalMenu*, sal_uInt16> MenuAndId; + +namespace +{ + MenuAndId decode_command(const gchar *action_name) + { + OString sCommand(action_name); + + sal_Int32 nIndex = 0; + OString sWindow = sCommand.getToken(0, '-', nIndex); + OString sGtkSalMenu = sCommand.getToken(0, '-', nIndex); + OString sItemId = sCommand.getToken(0, '-', nIndex); + + GtkSalMenu* pSalSubMenu = reinterpret_cast<GtkSalMenu*>(sGtkSalMenu.toInt64()); + + assert(sWindow == "window" && pSalSubMenu); + (void) sWindow; + + return MenuAndId(pSalSubMenu, sItemId.toInt32()); + } +} + +static void RemoveDisabledItemsFromNativeMenu(GLOMenu* pMenu, GList** pOldCommandList, + sal_Int32 nSection, GActionGroup* pActionGroup) +{ + while (nSection >= 0) + { + sal_Int32 nSectionItems = g_lo_menu_get_n_items_from_section( pMenu, nSection ); + while (nSectionItems--) + { + gchar* pCommand = g_lo_menu_get_command_from_item_in_section(pMenu, nSection, nSectionItems); + // remove disabled entries + bool bRemove = !g_action_group_get_action_enabled(pActionGroup, pCommand); + if (!bRemove) + { + //also remove any empty submenus + GLOMenu* pSubMenuModel = g_lo_menu_get_submenu_from_item_in_section(pMenu, nSection, nSectionItems); + if (pSubMenuModel) + { + gint nSubMenuSections = g_menu_model_get_n_items(G_MENU_MODEL(pSubMenuModel)); + if (nSubMenuSections == 0) + bRemove = true; + else if (nSubMenuSections == 1) + { + gint nItems = g_lo_menu_get_n_items_from_section(pSubMenuModel, 0); + if (nItems == 0) + bRemove = true; + else if (nItems == 1) + { + //If the only entry is the "No Selection Possible" entry, then we are allowed + //to removed it + gchar* pSubCommand = g_lo_menu_get_command_from_item_in_section(pSubMenuModel, 0, 0); + MenuAndId aMenuAndId(decode_command(pSubCommand)); + bRemove = aMenuAndId.second == 0xFFFF; + g_free(pSubCommand); + } + } + } + } + + if (bRemove) + { + //but tdf#86850 Always display clipboard functions + bRemove = g_strcmp0(pCommand, ".uno:Cut") && + g_strcmp0(pCommand, ".uno:Copy") && + g_strcmp0(pCommand, ".uno:Paste"); + } + + if (bRemove) + { + if (pCommand != nullptr && pOldCommandList != nullptr) + *pOldCommandList = g_list_append(*pOldCommandList, g_strdup(pCommand)); + g_lo_menu_remove_from_section(pMenu, nSection, nSectionItems); + } + + g_free(pCommand); + } + --nSection; + } +} + +static void RemoveSpareSectionsFromNativeMenu( GLOMenu* pMenu, GList** pOldCommandList, sal_Int32 nLastSection ) +{ + if ( pMenu == nullptr || pOldCommandList == nullptr ) + return; + + sal_Int32 n = g_menu_model_get_n_items( G_MENU_MODEL( pMenu ) ) - 1; + + for ( ; n > nLastSection; n--) + { + RemoveSpareItemsFromNativeMenu( pMenu, pOldCommandList, n, 0 ); + g_lo_menu_remove( pMenu, n ); + } +} + +static gint CompareStr( gpointer str1, gpointer str2 ) +{ + return g_strcmp0( static_cast<const gchar*>(str1), static_cast<const gchar*>(str2) ); +} + +static void RemoveUnusedCommands( GLOActionGroup* pActionGroup, GList* pOldCommandList, GList* pNewCommandList ) +{ + if ( pActionGroup == nullptr || pOldCommandList == nullptr ) + { + g_list_free_full( pOldCommandList, g_free ); + g_list_free_full( pNewCommandList, g_free ); + return; + } + + while ( pNewCommandList != nullptr ) + { + GList* pNewCommand = g_list_first( pNewCommandList ); + pNewCommandList = g_list_remove_link( pNewCommandList, pNewCommand ); + + gpointer aCommand = g_list_nth_data( pNewCommand, 0 ); + + GList* pOldCommand = g_list_find_custom( pOldCommandList, aCommand, reinterpret_cast<GCompareFunc>(CompareStr) ); + + if ( pOldCommand != nullptr ) + { + pOldCommandList = g_list_remove_link( pOldCommandList, pOldCommand ); + g_list_free_full( pOldCommand, g_free ); + } + + g_list_free_full( pNewCommand, g_free ); + } + + while ( pOldCommandList != nullptr ) + { + GList* pCommand = g_list_first( pOldCommandList ); + pOldCommandList = g_list_remove_link( pOldCommandList, pCommand ); + + gchar* aCommand = static_cast<gchar*>(g_list_nth_data( pCommand, 0 )); + + g_lo_action_group_remove( pActionGroup, aCommand ); + + g_list_free_full( pCommand, g_free ); + } +} + +void GtkSalMenu::ImplUpdate(bool bRecurse, bool bRemoveDisabledEntries) +{ + SolarMutexGuard aGuard; + + SAL_INFO("vcl.unity", "ImplUpdate pre PrepUpdate"); + if( !PrepUpdate() ) + return; + + if (mbNeedsUpdate) + { + mbNeedsUpdate = false; + if (mbMenuBar && maUpdateMenuBarIdle.IsActive()) + { + maUpdateMenuBarIdle.Stop(); + maUpdateMenuBarIdle.Invoke(); + return; + } + } + + Menu* pVCLMenu = mpVCLMenu; + GLOMenu* pLOMenu = G_LO_MENU( mpMenuModel ); + GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup ); + SAL_INFO("vcl.unity", "Syncing vcl menu " << pVCLMenu << " to menu model " << pLOMenu << " and action group " << pActionGroup); + GList *pOldCommandList = nullptr; + GList *pNewCommandList = nullptr; + + sal_uInt16 nLOMenuSize = g_menu_model_get_n_items( G_MENU_MODEL( pLOMenu ) ); + + if ( nLOMenuSize == 0 ) + g_lo_menu_new_section( pLOMenu, 0, nullptr ); + + sal_Int32 nSection = 0; + sal_Int32 nItemPos = 0; + sal_Int32 validItems = 0; + sal_Int32 nItem; + + for ( nItem = 0; nItem < static_cast<sal_Int32>(GetItemCount()); nItem++ ) { + if ( !IsItemVisible( nItem ) ) + continue; + + GtkSalMenuItem *pSalMenuItem = GetItemAtPos( nItem ); + sal_uInt16 nId = pSalMenuItem->mnId; + + // PopupMenu::ImplExecute might add <No Selection Possible> entry to top-level + // popup menu, but we have our own implementation below, so skip that one. + if ( nId == 0xFFFF ) + continue; + + if ( pSalMenuItem->mnType == MenuItemType::SEPARATOR ) + { + // Delete extra items from current section. + RemoveSpareItemsFromNativeMenu( pLOMenu, &pOldCommandList, nSection, validItems ); + + nSection++; + nItemPos = 0; + validItems = 0; + + if ( nLOMenuSize <= nSection ) + { + g_lo_menu_new_section( pLOMenu, nSection, nullptr ); + nLOMenuSize++; + } + + continue; + } + + if ( nItemPos >= g_lo_menu_get_n_items_from_section( pLOMenu, nSection ) ) + g_lo_menu_insert_in_section( pLOMenu, nSection, nItemPos, "EMPTY STRING" ); + + // Get internal menu item values. + OUString aText = pVCLMenu->GetItemText( nId ); + Image aImage = pVCLMenu->GetItemImage( nId ); + bool bEnabled = pVCLMenu->IsItemEnabled( nId ); + vcl::KeyCode nAccelKey = pVCLMenu->GetAccelKey( nId ); + bool bChecked = pVCLMenu->IsItemChecked( nId ); + MenuItemBits itemBits = pVCLMenu->GetItemBits( nId ); + + // Store current item command in command list. + gchar *aCurrentCommand = g_lo_menu_get_command_from_item_in_section( pLOMenu, nSection, nItemPos ); + + if ( aCurrentCommand != nullptr ) + pOldCommandList = g_list_append( pOldCommandList, aCurrentCommand ); + + // Get the new command for the item. + gchar* aNativeCommand = GetCommandForItem(pSalMenuItem); + + // Force updating of native menu labels. + NativeSetItemText( nSection, nItemPos, aText ); + NativeSetItemIcon( nSection, nItemPos, aImage ); + NativeSetAccelerator( nSection, nItemPos, nAccelKey, nAccelKey.GetName( GetFrame()->GetWindow() ) ); + + if ( g_strcmp0( aNativeCommand, "" ) != 0 && pSalMenuItem->mpSubMenu == nullptr ) + { + NativeSetItemCommand( nSection, nItemPos, nId, aNativeCommand, itemBits, bChecked, false ); + NativeCheckItem( nSection, nItemPos, itemBits, bChecked ); + NativeSetEnableItem( aNativeCommand, bEnabled ); + + pNewCommandList = g_list_append( pNewCommandList, g_strdup( aNativeCommand ) ); + } + + GtkSalMenu* pSubmenu = pSalMenuItem->mpSubMenu; + + if ( pSubmenu && pSubmenu->GetMenu() ) + { + bool bNonMenuChangedToMenu = NativeSetItemCommand( nSection, nItemPos, nId, aNativeCommand, itemBits, false, true ); + pNewCommandList = g_list_append( pNewCommandList, g_strdup( aNativeCommand ) ); + + GLOMenu* pSubMenuModel = g_lo_menu_get_submenu_from_item_in_section( pLOMenu, nSection, nItemPos ); + + if ( pSubMenuModel == nullptr ) + { + g_lo_menu_new_submenu_in_item_in_section( pLOMenu, nSection, nItemPos ); + pSubMenuModel = g_lo_menu_get_submenu_from_item_in_section( pLOMenu, nSection, nItemPos ); + } + + g_object_unref( pSubMenuModel ); + + if (bRecurse || bNonMenuChangedToMenu) + { + SAL_INFO("vcl.unity", "preparing submenu " << pSubMenuModel << " to menu model " << G_MENU_MODEL(pSubMenuModel) << " and action group " << G_ACTION_GROUP(pActionGroup)); + pSubmenu->SetMenuModel( G_MENU_MODEL( pSubMenuModel ) ); + pSubmenu->SetActionGroup( G_ACTION_GROUP( pActionGroup ) ); + pSubmenu->ImplUpdate(true, bRemoveDisabledEntries); + } + } + + g_free( aNativeCommand ); + + ++nItemPos; + ++validItems; + } + + if (bRemoveDisabledEntries) + { + // Delete disabled items in last section. + RemoveDisabledItemsFromNativeMenu(pLOMenu, &pOldCommandList, nSection, G_ACTION_GROUP(pActionGroup)); + } + + // Delete extra items in last section. + RemoveSpareItemsFromNativeMenu( pLOMenu, &pOldCommandList, nSection, validItems ); + + // Delete extra sections. + RemoveSpareSectionsFromNativeMenu( pLOMenu, &pOldCommandList, nSection ); + + // Delete unused commands. + RemoveUnusedCommands( pActionGroup, pOldCommandList, pNewCommandList ); + + // Resolves: tdf#103166 if the menu is empty, add a disabled + // <No Selection Possible> placeholder. + sal_Int32 nSectionsCount = g_menu_model_get_n_items(G_MENU_MODEL(pLOMenu)); + gint nItemsCount = 0; + for (nSection = 0; nSection < nSectionsCount; ++nSection) + { + nItemsCount += g_lo_menu_get_n_items_from_section(pLOMenu, nSection); + if (nItemsCount) + break; + } + if (!nItemsCount) + { + gchar* aNativeCommand = GetCommandForItem(this, 0xFFFF); + OUString aPlaceholderText(VclResId(SV_RESID_STRING_NOSELECTIONPOSSIBLE)); + g_lo_menu_insert_in_section(pLOMenu, nSection-1, 0, + OUStringToOString(aPlaceholderText, RTL_TEXTENCODING_UTF8).getStr()); + NativeSetItemCommand(nSection-1, 0, 0xFFFF, aNativeCommand, MenuItemBits::NONE, false, false); + NativeSetEnableItem(aNativeCommand, false); + g_free(aNativeCommand); + } +} + +void GtkSalMenu::Update() +{ + //find out if top level is a menubar or not, if not, then it's a popup menu + //hierarchy and in those we hide (most) disabled entries + const GtkSalMenu* pMenu = this; + while (pMenu->mpParentSalMenu) + pMenu = pMenu->mpParentSalMenu; + ImplUpdate(false, !pMenu->mbMenuBar); +} + +static void MenuPositionFunc(GtkMenu* menu, gint* x, gint* y, gboolean* push_in, gpointer user_data) +{ + Point *pPos = static_cast<Point*>(user_data); + *x = pPos->X(); + if (gtk_widget_get_default_direction() == GTK_TEXT_DIR_RTL) + { + GtkRequisition natural_size; + gtk_widget_get_preferred_size(GTK_WIDGET(menu), nullptr, &natural_size); + *x -= natural_size.width; + } + *y = pPos->Y(); + *push_in = false; +} + +bool GtkSalMenu::ShowNativePopupMenu(FloatingWindow* pWin, const tools::Rectangle& rRect, + FloatWinPopupFlags nFlags) +{ + VclPtr<vcl::Window> xParent = pWin->ImplGetWindowImpl()->mpRealParent; + mpFrame = static_cast<GtkSalFrame*>(xParent->ImplGetFrame()); + + GLOActionGroup* pActionGroup = g_lo_action_group_new(); + mpActionGroup = G_ACTION_GROUP(pActionGroup); + mpMenuModel = G_MENU_MODEL(g_lo_menu_new()); + // Generate the main menu structure, populates mpMenuModel + UpdateFull(); + + GtkWidget *pWidget = gtk_menu_new_from_model(mpMenuModel); + gtk_menu_attach_to_widget(GTK_MENU(pWidget), mpFrame->getMouseEventWidget(), nullptr); + gtk_widget_insert_action_group(mpFrame->getMouseEventWidget(), "win", mpActionGroup); + + //run in a sub main loop because we need to keep vcl PopupMenu alive to use + //it during DispatchCommand, returning now to the outer loop causes the + //launching PopupMenu to be destroyed, instead run the subloop here + //until the gtk menu is destroyed + GMainLoop* pLoop = g_main_loop_new(nullptr, true); + g_signal_connect_swapped(G_OBJECT(pWidget), "deactivate", G_CALLBACK(g_main_loop_quit), pLoop); + +#if GTK_CHECK_VERSION(3,22,0) + if (gtk_check_version(3, 22, 0) == nullptr) + { + GdkGravity rect_anchor = GDK_GRAVITY_SOUTH_WEST, menu_anchor = GDK_GRAVITY_NORTH_WEST; + + if (nFlags & FloatWinPopupFlags::Left) + { + rect_anchor = GDK_GRAVITY_NORTH_WEST; + menu_anchor = GDK_GRAVITY_NORTH_EAST; + } + else if (nFlags & FloatWinPopupFlags::Up) + { + rect_anchor = GDK_GRAVITY_NORTH_WEST; + menu_anchor = GDK_GRAVITY_SOUTH_WEST; + } + else if (nFlags & FloatWinPopupFlags::Right) + { + rect_anchor = GDK_GRAVITY_NORTH_EAST; + } + + tools::Rectangle aFloatRect = FloatingWindow::ImplConvertToAbsPos(xParent, rRect); + aFloatRect.Move(-mpFrame->maGeometry.nX, -mpFrame->maGeometry.nY); + GdkRectangle rect {static_cast<int>(aFloatRect.Left()), static_cast<int>(aFloatRect.Top()), + static_cast<int>(aFloatRect.GetWidth()), static_cast<int>(aFloatRect.GetHeight())}; + + GdkWindow* gdkWindow = widget_get_window(mpFrame->getMouseEventWidget()); + gtk_menu_popup_at_rect(GTK_MENU(pWidget), gdkWindow, &rect, rect_anchor, menu_anchor, nullptr); + } + else +#endif + { + guint nButton; + guint32 nTime; + + //typically there is an event, and we can then distinguish if this was + //launched from the keyboard (gets auto-mnemoniced) or the mouse (which + //doesn't) + GdkEvent *pEvent = gtk_get_current_event(); + if (pEvent) + { + gdk_event_get_button(pEvent, &nButton); + nTime = gdk_event_get_time(pEvent); + } + else + { + nButton = 0; + nTime = GtkSalFrame::GetLastInputEventTime(); + } + + // do the same strange semantics as vcl popup windows to arrive at a frame geometry + // in mirrored UI case; best done by actually executing the same code + sal_uInt16 nArrangeIndex; + Point aPos = FloatingWindow::ImplCalcPos(pWin, rRect, nFlags, nArrangeIndex); + aPos = FloatingWindow::ImplConvertToAbsPos(xParent, aPos); + + gtk_menu_popup(GTK_MENU(pWidget), nullptr, nullptr, MenuPositionFunc, + &aPos, nButton, nTime); + } + + if (g_main_loop_is_running(pLoop)) + { + gdk_threads_leave(); + g_main_loop_run(pLoop); + gdk_threads_enter(); + } + g_main_loop_unref(pLoop); + + mpVCLMenu->Deactivate(); + + gtk_widget_insert_action_group(mpFrame->getMouseEventWidget(), "win", nullptr); + + gtk_widget_destroy(pWidget); + + g_object_unref(mpActionGroup); + ClearActionGroupAndMenuModel(); + + mpFrame = nullptr; + + return true; +} + +/* + * GtkSalMenu + */ + +GtkSalMenu::GtkSalMenu( bool bMenuBar ) : + mbInActivateCallback( false ), + mbMenuBar( bMenuBar ), + mbNeedsUpdate( false ), + mbReturnFocusToDocument( false ), + mbAddedGrab( false ), + mpMenuBarContainerWidget( nullptr ), + mpMenuAllowShrinkWidget( nullptr ), + mpMenuBarWidget( nullptr ), + mpMenuBarContainerProvider( nullptr ), + mpMenuBarProvider( nullptr ), + mpCloseButton( nullptr ), + mpVCLMenu( nullptr ), + mpParentSalMenu( nullptr ), + mpFrame( nullptr ), + mpMenuModel( nullptr ), + mpActionGroup( nullptr ) +{ + //typically this only gets called after the menu has been customized on the + //next idle slot, in the normal case of a new menubar SetFrame is called + //directly long before this idle would get called. + maUpdateMenuBarIdle.SetPriority(TaskPriority::HIGHEST); + maUpdateMenuBarIdle.SetInvokeHandler(LINK(this, GtkSalMenu, MenuBarHierarchyChangeHandler)); + maUpdateMenuBarIdle.SetDebugName("Native Gtk Menu Update Idle"); +} + +IMPL_LINK_NOARG(GtkSalMenu, MenuBarHierarchyChangeHandler, Timer *, void) +{ + SAL_WARN_IF(!mpFrame, "vcl.gtk", "MenuBar layout changed, but no frame for some reason!"); + if (!mpFrame) + return; + SetFrame(mpFrame); +} + +void GtkSalMenu::SetNeedsUpdate() +{ + GtkSalMenu* pMenu = this; + // start that the menu and its parents are in need of an update + // on the next activation + while (pMenu && !pMenu->mbNeedsUpdate) + { + pMenu->mbNeedsUpdate = true; + pMenu = pMenu->mpParentSalMenu; + } + // only if a menubar is directly updated do we force in a full + // structure update + if (mbMenuBar && !maUpdateMenuBarIdle.IsActive()) + maUpdateMenuBarIdle.Start(); +} + +void GtkSalMenu::SetMenuModel(GMenuModel* pMenuModel) +{ + if (mpMenuModel) + g_object_unref(mpMenuModel); + mpMenuModel = pMenuModel; + if (mpMenuModel) + g_object_ref(mpMenuModel); +} + +GtkSalMenu::~GtkSalMenu() +{ + SolarMutexGuard aGuard; + + DestroyMenuBarWidget(); + + if (mpMenuModel) + g_object_unref(mpMenuModel); + + maItems.clear(); + + if (mpFrame) + mpFrame->SetMenu(nullptr); +} + +bool GtkSalMenu::VisibleMenuBar() +{ + return mbMenuBar && (bUnityMode || mpMenuBarContainerWidget); +} + +void GtkSalMenu::InsertItem( SalMenuItem* pSalMenuItem, unsigned nPos ) +{ + SolarMutexGuard aGuard; + GtkSalMenuItem *pItem = static_cast<GtkSalMenuItem*>( pSalMenuItem ); + + if ( nPos == MENU_APPEND ) + maItems.push_back( pItem ); + else + maItems.insert( maItems.begin() + nPos, pItem ); + + pItem->mpParentMenu = this; + + SetNeedsUpdate(); +} + +void GtkSalMenu::RemoveItem( unsigned nPos ) +{ + SolarMutexGuard aGuard; + maItems.erase( maItems.begin() + nPos ); + SetNeedsUpdate(); +} + +void GtkSalMenu::SetSubMenu( SalMenuItem* pSalMenuItem, SalMenu* pSubMenu, unsigned ) +{ + SolarMutexGuard aGuard; + GtkSalMenuItem *pItem = static_cast< GtkSalMenuItem* >( pSalMenuItem ); + GtkSalMenu *pGtkSubMenu = static_cast< GtkSalMenu* >( pSubMenu ); + + if ( pGtkSubMenu == nullptr ) + return; + + pGtkSubMenu->mpParentSalMenu = this; + pItem->mpSubMenu = pGtkSubMenu; + + SetNeedsUpdate(); +} + +static void CloseMenuBar(GtkWidget *, gpointer pMenu) +{ + Application::PostUserEvent(static_cast<MenuBar*>(pMenu)->GetCloseButtonClickHdl()); +} + +void GtkSalMenu::ShowCloseButton(bool bShow) +{ + assert(mbMenuBar); + if (!mpMenuBarContainerWidget) + return; + + if (!bShow) + { + if (mpCloseButton) + gtk_widget_destroy(mpCloseButton); + return; + } + + MenuBar *pVclMenuBar = static_cast<MenuBar*>(mpVCLMenu.get()); + mpCloseButton = gtk_button_new(); + g_signal_connect(mpCloseButton, "clicked", G_CALLBACK(CloseMenuBar), pVclMenuBar); + + gtk_button_set_relief(GTK_BUTTON(mpCloseButton), GTK_RELIEF_NONE); + gtk_button_set_focus_on_click(GTK_BUTTON(mpCloseButton), false); + gtk_widget_set_can_focus(mpCloseButton, false); + + GtkStyleContext *pButtonContext = gtk_widget_get_style_context(GTK_WIDGET(mpCloseButton)); + + GtkCssProvider *pProvider = gtk_css_provider_new(); + static const gchar data[] = "* { " + "padding: 0;" + "margin-left: 8px;" + "margin-right: 8px;" + "min-width: 18px;" + "min-height: 18px;" + "}"; + const gchar olddata[] = "* { " + "padding: 0;" + "margin-left: 8px;" + "margin-right: 8px;" + "}"; + gtk_css_provider_load_from_data(pProvider, gtk_check_version(3, 20, 0) == nullptr ? data : olddata, -1, nullptr); + gtk_style_context_add_provider(pButtonContext, + GTK_STYLE_PROVIDER(pProvider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + + gtk_style_context_add_class(pButtonContext, "flat"); + gtk_style_context_add_class(pButtonContext, "small-button"); + + GIcon* icon = g_themed_icon_new_with_default_fallbacks("window-close-symbolic"); + GtkWidget* image = gtk_image_new_from_gicon(icon, GTK_ICON_SIZE_MENU); + gtk_widget_show(image); + g_object_unref(icon); + + OUString sToolTip(VclResId(SV_HELPTEXT_CLOSEDOCUMENT)); + gtk_widget_set_tooltip_text(mpCloseButton, + OUStringToOString(sToolTip, RTL_TEXTENCODING_UTF8).getStr()); + + gtk_widget_set_valign(mpCloseButton, GTK_ALIGN_CENTER); + + gtk_container_add(GTK_CONTAINER(mpCloseButton), image); + gtk_grid_attach(GTK_GRID(mpMenuBarContainerWidget), GTK_WIDGET(mpCloseButton), 1, 0, 1, 1); + gtk_widget_show_all(mpCloseButton); +} + +//Typically when the menubar is deactivated we want the focus to return +//to where it came from. If the menubar was activated because of F6 +//moving focus into the associated VCL menubar then on pressing ESC +//or any other normal reason for deactivation we want focus to return +//to the document, definitely not still stuck in the associated +//VCL menubar. But if F6 is pressed while the menubar is activated +//we want to pass that F6 back to the VCL menubar which will move +//focus to the next pane by itself. +void GtkSalMenu::ReturnFocus() +{ + if (mbAddedGrab) + { + gtk_grab_remove(mpMenuBarWidget); + mbAddedGrab = false; + } + if (!mbReturnFocusToDocument) + gtk_widget_grab_focus(GTK_WIDGET(mpFrame->getEventBox())); + else + mpFrame->GetWindow()->GrabFocusToDocument(); + mbReturnFocusToDocument = false; +} + +gboolean GtkSalMenu::SignalKey(GdkEventKey const * pEvent) +{ + if (pEvent->keyval == GDK_KEY_F6) + { + mbReturnFocusToDocument = false; + gtk_menu_shell_cancel(GTK_MENU_SHELL(mpMenuBarWidget)); + //because we return false here, the keypress will continue + //to propagate and in the case that vcl focus is in + //the vcl menubar then that will also process F6 and move + //to the next pane + } + return false; +} + +//The GtkSalMenu is owner by a Vcl Menu/MenuBar. In the menubar +//case the vcl menubar is present and "visible", but with a 0 height +//so it not apparent. Normally it acts as though it is not there when +//a Native menubar is active. If we return true here, then for keyboard +//activation and traversal with F6 through panes then the vcl menubar +//acts as though it *is* present and we translate its take focus and F6 +//traversal key events into the gtk menubar equivalents. +bool GtkSalMenu::CanGetFocus() const +{ + return mpMenuBarWidget != nullptr; +} + +bool GtkSalMenu::TakeFocus() +{ + if (!mpMenuBarWidget) + return false; + + //Send a keyboard event to the gtk menubar to let it know it has been + //activated via the keyboard. Doesn't do anything except cause the gtk + //menubar "keyboard_mode" member to get set to true, so typically mnemonics + //are shown which will serve as indication that the menubar has focus + //(given that we want to show it with no menus popped down) + GdkEvent *event = GtkSalFrame::makeFakeKeyPress(mpMenuBarWidget); + gtk_widget_event(mpMenuBarWidget, event); + gdk_event_free(event); + + //this pairing results in a menubar with keyboard focus with no menus + //auto-popped down + gtk_grab_add(mpMenuBarWidget); + mbAddedGrab = true; + gtk_menu_shell_select_first(GTK_MENU_SHELL(mpMenuBarWidget), false); + gtk_menu_shell_deselect(GTK_MENU_SHELL(mpMenuBarWidget)); + mbReturnFocusToDocument = true; + return true; +} + +static void MenuBarReturnFocus(GtkMenuShell*, gpointer menu) +{ + GtkSalFrame::UpdateLastInputEventTime(gtk_get_current_event_time()); + GtkSalMenu* pMenu = static_cast<GtkSalMenu*>(menu); + pMenu->ReturnFocus(); +} + +static gboolean MenuBarSignalKey(GtkWidget*, GdkEventKey* pEvent, gpointer menu) +{ + GtkSalMenu* pMenu = static_cast<GtkSalMenu*>(menu); + return pMenu->SignalKey(pEvent); +} + +void GtkSalMenu::CreateMenuBarWidget() +{ + if (mpMenuBarContainerWidget) + return; + + GtkGrid* pGrid = mpFrame->getTopLevelGridWidget(); + mpMenuBarContainerWidget = gtk_grid_new(); + + gtk_widget_set_hexpand(GTK_WIDGET(mpMenuBarContainerWidget), true); + gtk_grid_insert_row(pGrid, 0); + gtk_grid_attach(pGrid, mpMenuBarContainerWidget, 0, 0, 1, 1); + + mpMenuAllowShrinkWidget = gtk_scrolled_window_new(nullptr, nullptr); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(mpMenuAllowShrinkWidget), GTK_SHADOW_NONE); + // tdf#116290 external policy on scrolledwindow will not show a scrollbar, + // but still allow scrolled window to not be sized to the child content. + // So the menubar can be shrunk past its nominal smallest width. + // Unlike a hack using GtkFixed/GtkLayout the correct placement of the menubar occurs under RTL + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(mpMenuAllowShrinkWidget), GTK_POLICY_EXTERNAL, GTK_POLICY_NEVER); + gtk_grid_attach(GTK_GRID(mpMenuBarContainerWidget), mpMenuAllowShrinkWidget, 0, 0, 1, 1); + + mpMenuBarWidget = gtk_menu_bar_new_from_model(mpMenuModel); + + gtk_widget_insert_action_group(mpMenuBarWidget, "win", mpActionGroup); + gtk_widget_set_hexpand(GTK_WIDGET(mpMenuBarWidget), true); + gtk_widget_set_hexpand(mpMenuAllowShrinkWidget, true); + gtk_container_add(GTK_CONTAINER(mpMenuAllowShrinkWidget), mpMenuBarWidget); + + g_signal_connect(G_OBJECT(mpMenuBarWidget), "deactivate", G_CALLBACK(MenuBarReturnFocus), this); + g_signal_connect(G_OBJECT(mpMenuBarWidget), "key-press-event", G_CALLBACK(MenuBarSignalKey), this); + + gtk_widget_show_all(mpMenuBarContainerWidget); + + ShowCloseButton( static_cast<MenuBar*>(mpVCLMenu.get())->HasCloseButton() ); + + ApplyPersona(); +} + +void GtkSalMenu::ApplyPersona() +{ + if (!mpMenuBarContainerWidget) + return; + assert(mbMenuBar); + // I'm dubious about the persona theming feature, but as it exists, lets try and support + // it, apply the image to the mpMenuBarContainerWidget + const BitmapEx& rPersonaBitmap = Application::GetSettings().GetStyleSettings().GetPersonaHeader(); + + GtkStyleContext *pMenuBarContainerContext = gtk_widget_get_style_context(GTK_WIDGET(mpMenuBarContainerWidget)); + if (mpMenuBarContainerProvider) + { + gtk_style_context_remove_provider(pMenuBarContainerContext, GTK_STYLE_PROVIDER(mpMenuBarContainerProvider)); + mpMenuBarContainerProvider = nullptr; + } + GtkStyleContext *pMenuBarContext = gtk_widget_get_style_context(GTK_WIDGET(mpMenuBarWidget)); + if (mpMenuBarProvider) + { + gtk_style_context_remove_provider(pMenuBarContext, GTK_STYLE_PROVIDER(mpMenuBarProvider)); + mpMenuBarProvider = nullptr; + } + + if (!rPersonaBitmap.IsEmpty()) + { + if (maPersonaBitmap != rPersonaBitmap) + { + vcl::PNGWriter aPNGWriter(rPersonaBitmap); + mxPersonaImage.reset(new utl::TempFile); + mxPersonaImage->EnableKillingFile(true); + SvStream* pStream = mxPersonaImage->GetStream(StreamMode::WRITE); + aPNGWriter.Write(*pStream); + mxPersonaImage->CloseStream(); + } + + mpMenuBarContainerProvider = gtk_css_provider_new(); + OUString aBuffer = "* { background-image: url(\"" + mxPersonaImage->GetURL() + "\"); background-position: top right; }"; + OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8); + gtk_css_provider_load_from_data(mpMenuBarContainerProvider, aResult.getStr(), aResult.getLength(), nullptr); + gtk_style_context_add_provider(pMenuBarContainerContext, GTK_STYLE_PROVIDER(mpMenuBarContainerProvider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + + + // force the menubar to be transparent when persona is active otherwise for + // me the menubar becomes gray when its in the backdrop + mpMenuBarProvider = gtk_css_provider_new(); + static const gchar data[] = "* { " + "background-image: none;" + "background-color: transparent;" + "}"; + gtk_css_provider_load_from_data(mpMenuBarProvider, data, -1, nullptr); + gtk_style_context_add_provider(pMenuBarContext, + GTK_STYLE_PROVIDER(mpMenuBarProvider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + } + maPersonaBitmap = rPersonaBitmap; +} + +void GtkSalMenu::DestroyMenuBarWidget() +{ + if (mpMenuBarContainerWidget) + { + gtk_widget_destroy(mpMenuBarContainerWidget); + mpMenuBarContainerWidget = nullptr; + mpCloseButton = nullptr; + } +} + +void GtkSalMenu::SetFrame(const SalFrame* pFrame) +{ + SolarMutexGuard aGuard; + assert(mbMenuBar); + SAL_INFO("vcl.unity", "GtkSalMenu set to frame"); + mpFrame = const_cast<GtkSalFrame*>(static_cast<const GtkSalFrame*>(pFrame)); + + // if we had a menu on the GtkSalMenu we have to free it as we generate a + // full menu anyway and we might need to reuse an existing model and + // actiongroup + mpFrame->SetMenu( this ); + mpFrame->EnsureAppMenuWatch(); + + // Clean menu model and action group if needed. + GtkWidget* pWidget = mpFrame->getWindow(); + GdkWindow* gdkWindow = gtk_widget_get_window( pWidget ); + + GLOMenu* pMenuModel = G_LO_MENU( g_object_get_data( G_OBJECT( gdkWindow ), "g-lo-menubar" ) ); + GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( g_object_get_data( G_OBJECT( gdkWindow ), "g-lo-action-group" ) ); + SAL_INFO("vcl.unity", "Found menu model: " << pMenuModel << " and action group: " << pActionGroup); + + if ( pMenuModel ) + { + if ( g_menu_model_get_n_items( G_MENU_MODEL( pMenuModel ) ) > 0 ) + g_lo_menu_remove( pMenuModel, 0 ); + + mpMenuModel = G_MENU_MODEL( g_lo_menu_new() ); + } + + if ( pActionGroup ) + { + g_lo_action_group_clear( pActionGroup ); + mpActionGroup = G_ACTION_GROUP( pActionGroup ); + } + + // Generate the main menu structure. + if ( PrepUpdate() ) + UpdateFull(); + + g_lo_menu_insert_section( pMenuModel, 0, nullptr, mpMenuModel ); + + if (!bUnityMode && static_cast<MenuBar*>(mpVCLMenu.get())->IsDisplayable()) + { + DestroyMenuBarWidget(); + CreateMenuBarWidget(); + } +} + +const GtkSalFrame* GtkSalMenu::GetFrame() const +{ + SolarMutexGuard aGuard; + const GtkSalMenu* pMenu = this; + while( pMenu && ! pMenu->mpFrame ) + pMenu = pMenu->mpParentSalMenu; + return pMenu ? pMenu->mpFrame : nullptr; +} + +void GtkSalMenu::NativeCheckItem( unsigned nSection, unsigned nItemPos, MenuItemBits bits, gboolean bCheck ) +{ + SolarMutexGuard aGuard; + + if ( mpActionGroup == nullptr ) + return; + + gchar* aCommand = g_lo_menu_get_command_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos ); + + if ( aCommand != nullptr || g_strcmp0( aCommand, "" ) != 0 ) + { + GVariant *pCheckValue = nullptr; + GVariant *pCurrentState = g_action_group_get_action_state( mpActionGroup, aCommand ); + + if ( bits & MenuItemBits::RADIOCHECK ) + pCheckValue = bCheck ? g_variant_new_string( aCommand ) : g_variant_new_string( "" ); + else + { + // By default, all checked items are checkmark buttons. + if (bCheck || pCurrentState != nullptr) + pCheckValue = g_variant_new_boolean( bCheck ); + } + + if ( pCheckValue != nullptr ) + { + if ( pCurrentState == nullptr || g_variant_equal( pCurrentState, pCheckValue ) == FALSE ) + { + g_action_group_change_action_state( mpActionGroup, aCommand, pCheckValue ); + } + else + { + g_variant_unref (pCheckValue); + } + } + + if ( pCurrentState != nullptr ) + g_variant_unref( pCurrentState ); + } + + if ( aCommand ) + g_free( aCommand ); +} + +void GtkSalMenu::NativeSetEnableItem( gchar const * aCommand, gboolean bEnable ) +{ + SolarMutexGuard aGuard; + GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup ); + + if ( g_action_group_get_action_enabled( G_ACTION_GROUP( pActionGroup ), aCommand ) != bEnable ) + g_lo_action_group_set_action_enabled( pActionGroup, aCommand, bEnable ); +} + +void GtkSalMenu::NativeSetItemText( unsigned nSection, unsigned nItemPos, const OUString& rText ) +{ + SolarMutexGuard aGuard; + // Escape all underscores so that they don't get interpreted as hotkeys + OUString aText = rText.replaceAll( "_", "__" ); + // Replace the LibreOffice hotkey identifier with an underscore + aText = aText.replace( '~', '_' ); + OString aConvertedText = OUStringToOString( aText, RTL_TEXTENCODING_UTF8 ); + + // Update item text only when necessary. + gchar* aLabel = g_lo_menu_get_label_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos ); + + if ( aLabel == nullptr || g_strcmp0( aLabel, aConvertedText.getStr() ) != 0 ) + g_lo_menu_set_label_to_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos, aConvertedText.getStr() ); + + if ( aLabel ) + g_free( aLabel ); +} + +namespace +{ + void DestroyMemoryStream(gpointer data) + { + SvMemoryStream* pMemStm = static_cast<SvMemoryStream*>(data); + delete pMemStm; + } +} + +void GtkSalMenu::NativeSetItemIcon( unsigned nSection, unsigned nItemPos, const Image& rImage ) +{ +#if GLIB_CHECK_VERSION(2,38,0) + if (!!rImage && mbHasNullItemIcon) + return; + + SolarMutexGuard aGuard; + + if (!!rImage) + { + SvMemoryStream* pMemStm = new SvMemoryStream; + vcl::PNGWriter aWriter(rImage.GetBitmapEx()); + aWriter.Write(*pMemStm); + + GBytes *pBytes = g_bytes_new_with_free_func(pMemStm->GetData(), + pMemStm->TellEnd(), + DestroyMemoryStream, + pMemStm); + + GIcon *pIcon = g_bytes_icon_new(pBytes); + + g_lo_menu_set_icon_to_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos, pIcon ); + g_object_unref(pIcon); + g_bytes_unref(pBytes); + mbHasNullItemIcon = false; + } + else + { + g_lo_menu_set_icon_to_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos, nullptr ); + mbHasNullItemIcon = true; + } +#else + (void)nSection; + (void)nItemPos; + (void)rImage; +#endif +} + +void GtkSalMenu::NativeSetAccelerator( unsigned nSection, unsigned nItemPos, const vcl::KeyCode& rKeyCode, const OUString& rKeyName ) +{ + SolarMutexGuard aGuard; + + if ( rKeyName.isEmpty() ) + return; + + guint nKeyCode; + GdkModifierType nModifiers; + GtkSalFrame::KeyCodeToGdkKey(rKeyCode, &nKeyCode, &nModifiers); + + gchar* aAccelerator = gtk_accelerator_name( nKeyCode, nModifiers ); + + gchar* aCurrentAccel = g_lo_menu_get_accelerator_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos ); + + if ( aCurrentAccel == nullptr && g_strcmp0( aCurrentAccel, aAccelerator ) != 0 ) + g_lo_menu_set_accelerator_to_item_in_section ( G_LO_MENU( mpMenuModel ), nSection, nItemPos, aAccelerator ); + + g_free( aAccelerator ); + g_free( aCurrentAccel ); +} + +bool GtkSalMenu::NativeSetItemCommand( unsigned nSection, + unsigned nItemPos, + sal_uInt16 nId, + const gchar* aCommand, + MenuItemBits nBits, + bool bChecked, + bool bIsSubmenu ) +{ + bool bSubMenuAddedOrRemoved = false; + + SolarMutexGuard aGuard; + GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup ); + + GVariant *pTarget = nullptr; + + if (g_action_group_has_action(mpActionGroup, aCommand)) + g_lo_action_group_remove(pActionGroup, aCommand); + + if ( ( nBits & MenuItemBits::CHECKABLE ) || bIsSubmenu ) + { + // Item is a checkmark button. + GVariantType* pStateType = g_variant_type_new( reinterpret_cast<gchar const *>(G_VARIANT_TYPE_BOOLEAN) ); + GVariant* pState = g_variant_new_boolean( bChecked ); + + g_lo_action_group_insert_stateful( pActionGroup, aCommand, nId, bIsSubmenu, nullptr, pStateType, nullptr, pState ); + } + else if ( nBits & MenuItemBits::RADIOCHECK ) + { + // Item is a radio button. + GVariantType* pParameterType = g_variant_type_new( reinterpret_cast<gchar const *>(G_VARIANT_TYPE_STRING) ); + GVariantType* pStateType = g_variant_type_new( reinterpret_cast<gchar const *>(G_VARIANT_TYPE_STRING) ); + GVariant* pState = g_variant_new_string( "" ); + pTarget = g_variant_new_string( aCommand ); + + g_lo_action_group_insert_stateful( pActionGroup, aCommand, nId, FALSE, pParameterType, pStateType, nullptr, pState ); + } + else + { + // Item is not special, so insert a stateless action. + g_lo_action_group_insert( pActionGroup, aCommand, nId, FALSE ); + } + + GLOMenu* pMenu = G_LO_MENU( mpMenuModel ); + + // Menu item is not updated unless it's necessary. + gchar* aCurrentCommand = g_lo_menu_get_command_from_item_in_section( pMenu, nSection, nItemPos ); + + if ( aCurrentCommand == nullptr || g_strcmp0( aCurrentCommand, aCommand ) != 0 ) + { + bool bOldHasSubmenu = g_lo_menu_get_submenu_from_item_in_section(pMenu, nSection, nItemPos) != nullptr; + bSubMenuAddedOrRemoved = bOldHasSubmenu != bIsSubmenu; + if (bSubMenuAddedOrRemoved) + { + //tdf#98636 it's not good enough to unset the "submenu-action" attribute to change something + //from a submenu to a non-submenu item, so remove the old one entirely and re-add it to + //support achieving that + gchar* pLabel = g_lo_menu_get_label_from_item_in_section(pMenu, nSection, nItemPos); + g_lo_menu_remove_from_section(pMenu, nSection, nItemPos); + g_lo_menu_insert_in_section(pMenu, nSection, nItemPos, pLabel); + g_free(pLabel); + } + + g_lo_menu_set_command_to_item_in_section( pMenu, nSection, nItemPos, aCommand ); + + gchar* aItemCommand = g_strconcat("win.", aCommand, nullptr ); + + if ( bIsSubmenu ) + g_lo_menu_set_submenu_action_to_item_in_section( pMenu, nSection, nItemPos, aItemCommand ); + else + { + g_lo_menu_set_action_and_target_value_to_item_in_section( pMenu, nSection, nItemPos, aItemCommand, pTarget ); + pTarget = nullptr; + } + + g_free( aItemCommand ); + } + + if ( aCurrentCommand ) + g_free( aCurrentCommand ); + + if (pTarget) + g_variant_unref(pTarget); + + return bSubMenuAddedOrRemoved; +} + +GtkSalMenu* GtkSalMenu::GetTopLevel() +{ + GtkSalMenu *pMenu = this; + while (pMenu->mpParentSalMenu) + pMenu = pMenu->mpParentSalMenu; + return pMenu; +} + +void GtkSalMenu::DispatchCommand(const gchar *pCommand) +{ + SolarMutexGuard aGuard; + MenuAndId aMenuAndId = decode_command(pCommand); + GtkSalMenu* pSalSubMenu = aMenuAndId.first; + GtkSalMenu* pTopLevel = pSalSubMenu->GetTopLevel(); + if (pTopLevel->mpMenuBarWidget) + { + // tdf#125803 spacebar will toggle radios and checkbuttons without automatically + // closing the menu. To handle this properly I imagine we need to set groups for the + // radiobuttons so the others visually untoggle when the active one is toggled and + // we would further need to teach vcl that the state can change more than once. + // + // or we could unconditionally deactivate the menus if regardless of what particular + // type of menu item got activated + gtk_menu_shell_deactivate(GTK_MENU_SHELL(pTopLevel->mpMenuBarWidget)); + } + pTopLevel->GetMenu()->HandleMenuCommandEvent(pSalSubMenu->GetMenu(), aMenuAndId.second); +} + +void GtkSalMenu::ActivateAllSubmenus(Menu* pMenuBar) +{ + for (GtkSalMenuItem* pSalItem : maItems) + { + if ( pSalItem->mpSubMenu != nullptr ) + { + // We can re-enter this method via the new event loop that gets created + // in GtkClipboardTransferable::getTransferDataFlavorsAsVector, so use the InActivateCallback + // flag to detect that and skip some startup work. + if (!pSalItem->mpSubMenu->mbInActivateCallback) + { + pSalItem->mpSubMenu->mbInActivateCallback = true; + pMenuBar->HandleMenuActivateEvent(pSalItem->mpSubMenu->GetMenu()); + pSalItem->mpSubMenu->mbInActivateCallback = false; + pSalItem->mpSubMenu->ActivateAllSubmenus(pMenuBar); + pSalItem->mpSubMenu->Update(); + pMenuBar->HandleMenuDeActivateEvent(pSalItem->mpSubMenu->GetMenu()); + } + } + } +} + +void GtkSalMenu::ClearActionGroupAndMenuModel() +{ + SetMenuModel(nullptr); + mpActionGroup = nullptr; + for (GtkSalMenuItem* pSalItem : maItems) + { + if ( pSalItem->mpSubMenu != nullptr ) + { + pSalItem->mpSubMenu->ClearActionGroupAndMenuModel(); + } + } +} + +void GtkSalMenu::Activate(const gchar* pCommand) +{ + MenuAndId aMenuAndId = decode_command(pCommand); + GtkSalMenu* pSalMenu = aMenuAndId.first; + GtkSalMenu* pTopLevel = pSalMenu->GetTopLevel(); + Menu* pVclMenu = pSalMenu->GetMenu(); + Menu* pVclSubMenu = pVclMenu->GetPopupMenu(aMenuAndId.second); + GtkSalMenu* pSubMenu = pSalMenu->GetItemAtPos(pVclMenu->GetItemPos(aMenuAndId.second))->mpSubMenu; + + pSubMenu->mbInActivateCallback = true; + pTopLevel->GetMenu()->HandleMenuActivateEvent(pVclSubMenu); + pSubMenu->mbInActivateCallback = false; + pVclSubMenu->UpdateNativeMenu(); +} + +void GtkSalMenu::Deactivate(const gchar* pCommand) +{ + MenuAndId aMenuAndId = decode_command(pCommand); + GtkSalMenu* pSalMenu = aMenuAndId.first; + GtkSalMenu* pTopLevel = pSalMenu->GetTopLevel(); + Menu* pVclMenu = pSalMenu->GetMenu(); + Menu* pVclSubMenu = pVclMenu->GetPopupMenu(aMenuAndId.second); + pTopLevel->GetMenu()->HandleMenuDeActivateEvent(pVclSubMenu); +} + +void GtkSalMenu::EnableUnity(bool bEnable) +{ + bUnityMode = bEnable; + + MenuBar* pMenuBar(static_cast<MenuBar*>(mpVCLMenu.get())); + bool bDisplayable(pMenuBar->IsDisplayable()); + + if (bEnable) + { + DestroyMenuBarWidget(); + UpdateFull(); + if (!bDisplayable) + ShowMenuBar(false); + } + else + { + Update(); + ShowMenuBar(bDisplayable); + } + + pMenuBar->LayoutChanged(); +} + +void GtkSalMenu::ShowMenuBar( bool bVisible ) +{ + // Unity tdf#106271: Can't hide global menu, so empty it instead when user wants to hide menubar, + if (bUnityMode) + { + if (bVisible) + Update(); + else if (mpMenuModel && g_menu_model_get_n_items(G_MENU_MODEL(mpMenuModel)) > 0) + g_lo_menu_remove(G_LO_MENU(mpMenuModel), 0); + } + else if (bVisible) + CreateMenuBarWidget(); + else + DestroyMenuBarWidget(); +} + +bool GtkSalMenu::IsItemVisible( unsigned nPos ) +{ + SolarMutexGuard aGuard; + bool bVisible = false; + + if ( nPos < maItems.size() ) + bVisible = maItems[ nPos ]->mbVisible; + + return bVisible; +} + +void GtkSalMenu::CheckItem( unsigned, bool ) +{ +} + +void GtkSalMenu::EnableItem( unsigned nPos, bool bEnable ) +{ + SolarMutexGuard aGuard; + if ( bUnityMode && !mbInActivateCallback && !mbNeedsUpdate && GetTopLevel()->mbMenuBar && ( nPos < maItems.size() ) ) + { + gchar* pCommand = GetCommandForItem( GetItemAtPos( nPos ) ); + NativeSetEnableItem( pCommand, bEnable ); + g_free( pCommand ); + } +} + +void GtkSalMenu::ShowItem( unsigned nPos, bool bShow ) +{ + SolarMutexGuard aGuard; + if ( nPos < maItems.size() ) + { + maItems[ nPos ]->mbVisible = bShow; + if ( bUnityMode && !mbInActivateCallback && !mbNeedsUpdate && GetTopLevel()->mbMenuBar ) + Update(); + } +} + +void GtkSalMenu::SetItemText( unsigned nPos, SalMenuItem* pSalMenuItem, const OUString& rText ) +{ + SolarMutexGuard aGuard; + if ( bUnityMode && !mbInActivateCallback && !mbNeedsUpdate && GetTopLevel()->mbMenuBar && ( nPos < maItems.size() ) ) + { + gchar* pCommand = GetCommandForItem( static_cast< GtkSalMenuItem* >( pSalMenuItem ) ); + + gint nSectionsCount = g_menu_model_get_n_items( mpMenuModel ); + for ( gint nSection = 0; nSection < nSectionsCount; ++nSection ) + { + gint nItemsCount = g_lo_menu_get_n_items_from_section( G_LO_MENU( mpMenuModel ), nSection ); + for ( gint nItem = 0; nItem < nItemsCount; ++nItem ) + { + gchar* pCommandFromModel = g_lo_menu_get_command_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItem ); + + if ( !g_strcmp0( pCommandFromModel, pCommand ) ) + { + NativeSetItemText( nSection, nItem, rText ); + g_free( pCommandFromModel ); + g_free( pCommand ); + return; + } + + g_free( pCommandFromModel ); + } + } + + g_free( pCommand ); + } +} + +void GtkSalMenu::SetItemImage( unsigned, SalMenuItem*, const Image& ) +{ +} + +void GtkSalMenu::SetAccelerator( unsigned, SalMenuItem*, const vcl::KeyCode&, const OUString& ) +{ +} + +void GtkSalMenu::GetSystemMenuData( SystemMenuData* ) +{ +} + +int GtkSalMenu::GetMenuBarHeight() const +{ + return mpMenuBarWidget ? gtk_widget_get_allocated_height(mpMenuBarWidget) : 0; +} + +/* + * GtkSalMenuItem + */ + +GtkSalMenuItem::GtkSalMenuItem( const SalItemParams* pItemData ) : + mpParentMenu( nullptr ), + mpSubMenu( nullptr ), + mnType( pItemData->eType ), + mnId( pItemData->nId ), + mbVisible( true ) +{ +} + +GtkSalMenuItem::~GtkSalMenuItem() +{ +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3gtksys.cxx b/vcl/unx/gtk3/gtk3gtksys.cxx index 2406e0aa3118..229a718c75a7 100644 --- a/vcl/unx/gtk3/gtk3gtksys.cxx +++ b/vcl/unx/gtk3/gtk3gtksys.cxx @@ -5,8 +5,279 @@ * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <string.h> +#include <gmodule.h> +#include <gtk/gtk.h> +#include <unx/gtk/gtkinst.hxx> +#include <unx/gtk/gtksys.hxx> +#include <unx/gtk/gtkbackend.hxx> +#include <osl/module.h> + +GtkSalSystem *GtkSalSystem::GetSingleton() +{ + static GtkSalSystem *pSingleton = new GtkSalSystem(); + return pSingleton; +} + +SalSystem *GtkInstance::CreateSalSystem() +{ + return GtkSalSystem::GetSingleton(); +} + +GtkSalSystem::GtkSalSystem() : SalGenericSystem() +{ + mpDisplay = gdk_display_get_default(); + countScreenMonitors(); + // rhbz#1285356, native look will be gtk2, which crashes + // when gtk3 is already loaded. Until there is a solution + // java-side force look and feel to something that doesn't + // crash when we are using gtk3 + setenv("STOC_FORCE_SYSTEM_LAF", "true", 1); +} + +GtkSalSystem::~GtkSalSystem() +{ +} + +int +GtkSalSystem::GetDisplayXScreenCount() +{ + return gdk_display_get_n_screens (mpDisplay); +} + +namespace +{ + +struct GdkRectangleCoincidentLess +{ + // fdo#78799 - detect and elide overlaying monitors of different sizes + bool operator()(GdkRectangle const& rLeft, GdkRectangle const& rRight) + { + return + rLeft.x < rRight.x + || rLeft.y < rRight.y + ; + } +}; +struct GdkRectangleCoincident +{ + // fdo#78799 - detect and elide overlaying monitors of different sizes + bool operator()(GdkRectangle const& rLeft, GdkRectangle const& rRight) + { + return + rLeft.x == rRight.x + && rLeft.y == rRight.y + ; + } +}; + +} + +/** + * GtkSalSystem::countScreenMonitors() + * + * This method builds the vector which allows us to map from VCL's + * idea of linear integer ScreenNumber to gtk+'s rather more + * complicated screen + monitor concept. */ +void +GtkSalSystem::countScreenMonitors() +{ + maScreenMonitors.clear(); + for (gint i = 0; i < gdk_display_get_n_screens(mpDisplay); i++) + { + GdkScreen* const pScreen(gdk_display_get_screen(mpDisplay, i)); + gint nMonitors(pScreen ? gdk_screen_get_n_monitors(pScreen) : 0); + if (nMonitors > 1) + { + std::vector<GdkRectangle> aGeometries; + aGeometries.reserve(nMonitors); + for (gint j(0); j != nMonitors; ++j) + { + GdkRectangle aGeometry; + gdk_screen_get_monitor_geometry(pScreen, j, &aGeometry); + aGeometries.push_back(aGeometry); + } + std::sort(aGeometries.begin(), aGeometries.end(), + GdkRectangleCoincidentLess()); + const std::vector<GdkRectangle>::iterator aUniqueEnd( + std::unique(aGeometries.begin(), aGeometries.end(), + GdkRectangleCoincident())); + nMonitors = std::distance(aGeometries.begin(), aUniqueEnd); + } + maScreenMonitors.emplace_back(pScreen, nMonitors); + } +} + +SalX11Screen +GtkSalSystem::getXScreenFromDisplayScreen(unsigned int nScreen) +{ + gint nMonitor; + + GdkScreen *pScreen = getScreenMonitorFromIdx (nScreen, nMonitor); + if (!pScreen) + return SalX11Screen (0); + if (!DLSYM_GDK_IS_X11_DISPLAY(mpDisplay)) + return SalX11Screen (0); + return SalX11Screen (gdk_x11_screen_get_screen_number (pScreen)); +} + +GdkScreen * +GtkSalSystem::getScreenMonitorFromIdx (int nIdx, gint &nMonitor) +{ + GdkScreen *pScreen = nullptr; + for (auto const& screenMonitor : maScreenMonitors) + { + pScreen = screenMonitor.first; + if (!pScreen) + break; + if (nIdx >= screenMonitor.second) + nIdx -= screenMonitor.second; + else + break; + } + nMonitor = nIdx; + + // handle invalid monitor indexes as non-existent screens + if (nMonitor < 0 || (pScreen && nMonitor >= gdk_screen_get_n_monitors (pScreen))) + pScreen = nullptr; + + return pScreen; +} + +int +GtkSalSystem::getScreenIdxFromPtr (GdkScreen *pScreen) +{ + int nIdx = 0; + for (auto const& screenMonitor : maScreenMonitors) + { + if (screenMonitor.first == pScreen) + return nIdx; + nIdx += screenMonitor.second; + } + g_warning ("failed to find screen %p", pScreen); + return 0; +} + +int GtkSalSystem::getScreenMonitorIdx (GdkScreen *pScreen, + int nX, int nY) +{ + // TODO: this will fail horribly for exotic combinations like two + // monitors in mirror mode and one extra. Hopefully such + // abominations are not used (or, even better, not possible) in + // practice .-) + return getScreenIdxFromPtr (pScreen) + + gdk_screen_get_monitor_at_point (pScreen, nX, nY); +} + +unsigned int GtkSalSystem::GetDisplayScreenCount() +{ + gint nMonitor; + (void)getScreenMonitorFromIdx (G_MAXINT, nMonitor); + return G_MAXINT - nMonitor; +} + +bool GtkSalSystem::IsUnifiedDisplay() +{ + return gdk_display_get_n_screens (mpDisplay) == 1; +} + +namespace { +int _fallback_get_primary_monitor (GdkScreen *pScreen) +{ + // Use monitor name as primacy heuristic + int max = gdk_screen_get_n_monitors (pScreen); + for (int i = 0; i < max; ++i) + { + char *name = gdk_screen_get_monitor_plug_name (pScreen, i); + bool bLaptop = (name && !g_ascii_strncasecmp (name, "LVDS", 4)); + g_free (name); + if (bLaptop) + return i; + } + return 0; +} + +int _get_primary_monitor (GdkScreen *pScreen) +{ + static int (*get_fn) (GdkScreen *) = nullptr; + get_fn = gdk_screen_get_primary_monitor; + // Perhaps we have a newer gtk+ with this symbol: + if (!get_fn) + { + get_fn = reinterpret_cast<int(*)(GdkScreen*)>(osl_getAsciiFunctionSymbol(nullptr, + "gdk_screen_get_primary_monitor")); + } + if (!get_fn) + get_fn = _fallback_get_primary_monitor; + if (get_fn) + return get_fn (pScreen); + else + return 0; +} +} // end anonymous namespace + +unsigned int GtkSalSystem::GetDisplayBuiltInScreen() +{ + GdkScreen *pDefault = gdk_display_get_default_screen (mpDisplay); + int idx = getScreenIdxFromPtr (pDefault); + return idx + _get_primary_monitor (pDefault); +} + +tools::Rectangle GtkSalSystem::GetDisplayScreenPosSizePixel (unsigned int nScreen) +{ + gint nMonitor; + GdkScreen *pScreen; + GdkRectangle aRect; + pScreen = getScreenMonitorFromIdx (nScreen, nMonitor); + if (!pScreen) + return tools::Rectangle(); + gdk_screen_get_monitor_geometry (pScreen, nMonitor, &aRect); + return tools::Rectangle (Point(aRect.x, aRect.y), Size(aRect.width, aRect.height)); +} + +// convert ~ to indicate mnemonic to '_' +static OString MapToGtkAccelerator(const OUString &rStr) +{ + return OUStringToOString(rStr.replaceFirst("~", "_"), RTL_TEXTENCODING_UTF8); +} + +int GtkSalSystem::ShowNativeDialog (const OUString& rTitle, const OUString& rMessage, + const std::vector< OUString >& rButtonNames) +{ + OString aTitle (OUStringToOString (rTitle, RTL_TEXTENCODING_UTF8)); + OString aMessage (OUStringToOString (rMessage, RTL_TEXTENCODING_UTF8)); + + GtkDialog *pDialog = GTK_DIALOG ( + g_object_new (GTK_TYPE_MESSAGE_DIALOG, + "title", aTitle.getStr(), + "message-type", int(GTK_MESSAGE_WARNING), + "text", aMessage.getStr(), + nullptr)); + int nButton = 0; + for (auto const& buttonName : rButtonNames) + gtk_dialog_add_button (pDialog, MapToGtkAccelerator(buttonName).getStr(), nButton++); + gtk_dialog_set_default_response (pDialog, 0/*nDefaultButton*/); + + nButton = gtk_dialog_run (pDialog); + if (nButton < 0) + nButton = -1; + + gtk_widget_destroy (GTK_WIDGET (pDialog)); -#include "../gtk/gtksys.cxx" + return nButton; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3hudawareness.cxx b/vcl/unx/gtk3/gtk3hudawareness.cxx index 3d928f0fd0b1..0aa5878a1afe 100644 --- a/vcl/unx/gtk3/gtk3hudawareness.cxx +++ b/vcl/unx/gtk3/gtk3hudawareness.cxx @@ -1,4 +1,107 @@ /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -#include "../gtk/hudawareness.cxx" +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <string.h> + +#include <unx/gtk/gtksalmenu.hxx> +#include <unx/gtk/hudawareness.h> + +struct HudAwarenessHandle +{ + GDBusConnection *connection; + HudAwarenessCallback callback; + gpointer user_data; + GDestroyNotify notify; +}; + +static void +hud_awareness_method_call (GDBusConnection * /* connection */, + const gchar * /* sender */, + const gchar * /* object_path */, + const gchar * /* interface_name */, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + HudAwarenessHandle *handle = static_cast<HudAwarenessHandle*>(user_data); + + if (g_str_equal (method_name, "HudActiveChanged")) + { + gboolean active; + + g_variant_get (parameters, "(b)", &active); + + (* handle->callback) (active, handle->user_data); + } + + g_dbus_method_invocation_return_value (invocation, nullptr); +} + +guint +hud_awareness_register (GDBusConnection *connection, + const gchar *object_path, + HudAwarenessCallback callback, + gpointer user_data, + GDestroyNotify notify, + GError **error) +{ + static GDBusInterfaceInfo *iface; + static GDBusNodeInfo *info; + GDBusInterfaceVTable vtable; + HudAwarenessHandle *handle; + guint object_id; + + memset (static_cast<void *>(&vtable), 0, sizeof (vtable)); + vtable.method_call = hud_awareness_method_call; + + if G_UNLIKELY (iface == nullptr) + { + GError *local_error = nullptr; + + info = g_dbus_node_info_new_for_xml ("<node>" + "<interface name='com.canonical.hud.Awareness'>" + "<method name='CheckAwareness'/>" + "<method name='HudActiveChanged'>" + "<arg type='b'/>" + "</method>" + "</interface>" + "</node>", + &local_error); + g_assert_no_error (local_error); + iface = g_dbus_node_info_lookup_interface (info, "com.canonical.hud.Awareness"); + g_assert (iface != nullptr); + } + + handle = static_cast<HudAwarenessHandle*>(g_malloc (sizeof (HudAwarenessHandle))); + + object_id = g_dbus_connection_register_object (connection, object_path, iface, &vtable, handle, &g_free, error); + + if (object_id == 0) + { + g_free (handle); + return 0; + } + + handle->connection = static_cast<GDBusConnection*>(g_object_ref (connection)); + handle->callback = callback; + handle->user_data = user_data; + handle->notify = notify; + + return object_id; +} + +void +hud_awareness_unregister (GDBusConnection *connection, + guint subscription_id) +{ + g_dbus_connection_unregister_object (connection, subscription_id); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/unx/gtk3/gtk3salprn-gtk.cxx b/vcl/unx/gtk3/gtk3salprn-gtk.cxx index 16bf17c8562a..aeb9b1de246c 100644 --- a/vcl/unx/gtk3/gtk3salprn-gtk.cxx +++ b/vcl/unx/gtk3/gtk3salprn-gtk.cxx @@ -7,6 +7,956 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "../gtk/salprn-gtk.cxx" +#include <unx/gtk/gtkprintwrapper.hxx> + +#include <unx/gtk/gtkdata.hxx> +#include <unx/gtk/gtkframe.hxx> +#include <unx/gtk/gtkinst.hxx> +#include <unx/gtk/gtkprn.hxx> + +#include <vcl/configsettings.hxx> +#include <vcl/help.hxx> +#include <vcl/print.hxx> +#include <vcl/svapp.hxx> +#include <vcl/window.hxx> + +#include <gtk/gtk.h> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/container/XNamed.hpp> +#include <com/sun/star/document/XExporter.hpp> +#include <com/sun/star/document/XFilter.hpp> +#include <com/sun/star/frame/XFrame.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/sheet/XSpreadsheetDocument.hpp> +#include <com/sun/star/sheet/XSpreadsheet.hpp> +#include <com/sun/star/sheet/XSpreadsheetView.hpp> +#include <com/sun/star/view/PrintableState.hpp> +#include <com/sun/star/view/XSelectionSupplier.hpp> + +#include <officecfg/Office/Common.hxx> + +#include <rtl/ustring.hxx> +#include <sal/log.hxx> + +#include <unotools/streamwrap.hxx> + +#include <cstring> +#include <map> + +namespace beans = com::sun::star::beans; +namespace uno = com::sun::star::uno; +namespace view = com::sun::star::view; + +using vcl::unx::GtkPrintWrapper; + +using uno::UNO_QUERY; + +class GtkPrintDialog +{ +public: + explicit GtkPrintDialog(vcl::PrinterController& io_rController); + bool run(); + GtkPrinter* getPrinter() const + { + return m_xWrapper->print_unix_dialog_get_selected_printer(GTK_PRINT_UNIX_DIALOG(m_pDialog)); + } + GtkPrintSettings* getSettings() const + { + return m_xWrapper->print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(m_pDialog)); + } + void updateControllerPrintRange(); + + ~GtkPrintDialog(); + + static void UIOption_CheckHdl(GtkWidget* i_pWidget, GtkPrintDialog* io_pThis) + { + io_pThis->impl_UIOption_CheckHdl(i_pWidget); + } + static void UIOption_RadioHdl(GtkWidget* i_pWidget, GtkPrintDialog* io_pThis) + { + io_pThis->impl_UIOption_RadioHdl(i_pWidget); + } + static void UIOption_SelectHdl(GtkWidget* i_pWidget, GtkPrintDialog* io_pThis) + { + io_pThis->impl_UIOption_SelectHdl(i_pWidget); + } + +private: + beans::PropertyValue* impl_queryPropertyValue(GtkWidget* i_pWidget) const; + void impl_checkOptionalControlDependencies(); + + void impl_UIOption_CheckHdl(GtkWidget* i_pWidget); + void impl_UIOption_RadioHdl(GtkWidget* i_pWidget); + void impl_UIOption_SelectHdl(GtkWidget* i_pWidget); + + void impl_initDialog(); + void impl_initCustomTab(); + void impl_initPrintContent(uno::Sequence<sal_Bool> const& i_rDisabled); + + void impl_readFromSettings(); + void impl_storeToSettings() const; + +private: + GtkWidget* m_pDialog; + vcl::PrinterController& m_rController; + std::map<GtkWidget*, OUString> m_aControlToPropertyMap; + std::map<GtkWidget*, sal_Int32> m_aControlToNumValMap; + std::shared_ptr<GtkPrintWrapper> m_xWrapper; +}; + +struct GtkSalPrinter_Impl +{ + OString m_sSpoolFile; + OUString m_sJobName; + GtkPrinter* m_pPrinter; + GtkPrintSettings* m_pSettings; + + GtkSalPrinter_Impl(); + ~GtkSalPrinter_Impl(); +}; + +GtkSalPrinter_Impl::GtkSalPrinter_Impl() + : m_pPrinter(nullptr) + , m_pSettings(nullptr) +{ +} + +GtkSalPrinter_Impl::~GtkSalPrinter_Impl() +{ + if (m_pPrinter) + { + g_object_unref(G_OBJECT(m_pPrinter)); + m_pPrinter = nullptr; + } + if (m_pSettings) + { + g_object_unref(G_OBJECT(m_pSettings)); + m_pSettings = nullptr; + } +} + +namespace +{ + +GtkInstance const& +lcl_getGtkSalInstance() +{ + // we _know_ this is GtkInstance + return *static_cast<GtkInstance*>(GetGtkSalData()->m_pInstance); +} + +bool +lcl_useSystemPrintDialog() +{ + return officecfg::Office::Common::Misc::UseSystemPrintDialog::get() + && officecfg::Office::Common::Misc::ExperimentalMode::get() + && lcl_getGtkSalInstance().getPrintWrapper()->supportsPrinting(); +} + +} + +GtkSalPrinter::GtkSalPrinter(SalInfoPrinter* const i_pInfoPrinter) + : PspSalPrinter(i_pInfoPrinter) +{ +} + +GtkSalPrinter::~GtkSalPrinter() = default; + +bool +GtkSalPrinter::impl_doJob( + const OUString* const i_pFileName, + const OUString& i_rJobName, + const OUString& i_rAppName, + ImplJobSetup* const io_pSetupData, + const bool i_bCollate, + vcl::PrinterController& io_rController) +{ + io_rController.setJobState(view::PrintableState_JOB_STARTED); + io_rController.jobStarted(); + const bool bJobStarted( + PspSalPrinter::StartJob(i_pFileName, i_rJobName, i_rAppName, + 1/*i_nCopies*/, i_bCollate, true, io_pSetupData)) + ; + + if (bJobStarted) + { + io_rController.createProgressDialog(); + const int nPages(io_rController.getFilteredPageCount()); + for (int nPage(0); nPage != nPages; ++nPage) + { + if (nPage == nPages - 1) + io_rController.setLastPage(true); + io_rController.printFilteredPage(nPage); + } + io_rController.setJobState(view::PrintableState_JOB_COMPLETED); + } + + return bJobStarted; +} + +bool +GtkSalPrinter::StartJob( + const OUString* const i_pFileName, + const OUString& i_rJobName, + const OUString& i_rAppName, + ImplJobSetup* io_pSetupData, + vcl::PrinterController& io_rController) +{ + if (!lcl_useSystemPrintDialog()) + return PspSalPrinter::StartJob(i_pFileName, i_rJobName, i_rAppName, io_pSetupData, io_rController); + + assert(!m_xImpl); + + m_xImpl.reset(new GtkSalPrinter_Impl()); + m_xImpl->m_sJobName = i_rJobName; + + OString sFileName; + if (i_pFileName) + sFileName = OUStringToOString(*i_pFileName, osl_getThreadTextEncoding()); + + GtkPrintDialog aDialog(io_rController); + if (!aDialog.run()) + { + io_rController.abortJob(); + return false; + } + aDialog.updateControllerPrintRange(); + m_xImpl->m_pPrinter = aDialog.getPrinter(); + m_xImpl->m_pSettings = aDialog.getSettings(); + + //To-Do proper name, watch for encodings + sFileName = OString("/tmp/hacking.ps"); + m_xImpl->m_sSpoolFile = sFileName; + + OUString aFileName = OStringToOUString(sFileName, osl_getThreadTextEncoding()); + + //To-Do, swap ps/pdf for gtk_printer_accepts_ps()/gtk_printer_accepts_pdf() ? + + return impl_doJob(&aFileName, i_rJobName, i_rAppName, io_pSetupData, /*bCollate*/false, io_rController); +} + +bool +GtkSalPrinter::EndJob() +{ + bool bRet = PspSalPrinter::EndJob(); + + if (!lcl_useSystemPrintDialog()) + return bRet; + + assert(m_xImpl); + + if (!bRet || m_xImpl->m_sSpoolFile.isEmpty()) + return bRet; + + std::shared_ptr<GtkPrintWrapper> const xWrapper(lcl_getGtkSalInstance().getPrintWrapper()); + + GtkPageSetup* pPageSetup = xWrapper->page_setup_new(); + + GtkPrintJob* const pJob = xWrapper->print_job_new( + OUStringToOString(m_xImpl->m_sJobName, RTL_TEXTENCODING_UTF8).getStr(), + m_xImpl->m_pPrinter, m_xImpl->m_pSettings, pPageSetup); + + GError* error = nullptr; + bRet = xWrapper->print_job_set_source_file(pJob, m_xImpl->m_sSpoolFile.getStr(), &error); + if (bRet) + xWrapper->print_job_send(pJob, nullptr, nullptr, nullptr); + else + { + //To-Do, do something with this + fprintf(stderr, "error was %s\n", error->message); + g_error_free(error); + } + + g_object_unref(pPageSetup); + m_xImpl.reset(); + + //To-Do, remove temp spool file + + return bRet; +} + +namespace +{ + +void +lcl_setHelpText( + GtkWidget* const io_pWidget, + const uno::Sequence<OUString>& i_rHelpTexts, + const sal_Int32 i_nIndex) +{ + if (i_nIndex >= 0 && i_nIndex < i_rHelpTexts.getLength()) + gtk_widget_set_tooltip_text(io_pWidget, + OUStringToOString(i_rHelpTexts.getConstArray()[i_nIndex], RTL_TEXTENCODING_UTF8).getStr()); +} + +GtkWidget* +lcl_makeFrame( + GtkWidget* const i_pChild, + const OUString &i_rText, + const uno::Sequence<OUString> &i_rHelpTexts, + sal_Int32* const io_pCurHelpText) +{ + GtkWidget* const pLabel = gtk_label_new(nullptr); + lcl_setHelpText(pLabel, i_rHelpTexts, !io_pCurHelpText ? 0 : (*io_pCurHelpText)++); + gtk_misc_set_alignment(GTK_MISC(pLabel), 0.0, 0.5); + + { + gchar* const pText = g_markup_printf_escaped("<b>%s</b>", + OUStringToOString(i_rText, RTL_TEXTENCODING_UTF8).getStr()); + gtk_label_set_markup_with_mnemonic(GTK_LABEL(pLabel), pText); + g_free(pText); + } + + GtkWidget* const pFrame = gtk_vbox_new(FALSE, 6); + gtk_box_pack_start(GTK_BOX(pFrame), pLabel, FALSE, FALSE, 0); + + GtkWidget* const pAlignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0); + gtk_alignment_set_padding(GTK_ALIGNMENT(pAlignment), 0, 0, 12, 0); + gtk_box_pack_start(GTK_BOX(pFrame), pAlignment, FALSE, FALSE, 0); + + gtk_container_add(GTK_CONTAINER(pAlignment), i_pChild); + return pFrame; +} + +void +lcl_extractHelpTextsOrIds( + const beans::PropertyValue& rEntry, + uno::Sequence<OUString>& rHelpStrings) +{ + if (!(rEntry.Value >>= rHelpStrings)) + { + OUString aHelpString; + if (rEntry.Value >>= aHelpString) + { + rHelpStrings.realloc(1); + *rHelpStrings.getArray() = aHelpString; + } + } +} + +GtkWidget* +lcl_combo_box_text_new() +{ + return gtk_combo_box_text_new(); +} + +void +lcl_combo_box_text_append(GtkWidget* const pWidget, gchar const* const pText) +{ + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(pWidget), pText); +} + +} + +GtkPrintDialog::GtkPrintDialog(vcl::PrinterController& io_rController) + : m_rController(io_rController) + , m_xWrapper(lcl_getGtkSalInstance().getPrintWrapper()) +{ + assert(m_xWrapper->supportsPrinting()); + impl_initDialog(); + impl_initCustomTab(); + impl_readFromSettings(); +} + +void +GtkPrintDialog::impl_initDialog() +{ + //To-Do, like fpicker, set UI language + m_pDialog = m_xWrapper->print_unix_dialog_new(); + + vcl::Window* const pTopWindow(Application::GetActiveTopWindow()); + if (pTopWindow) + { + GtkSalFrame* const pFrame(dynamic_cast<GtkSalFrame*>(pTopWindow->ImplGetFrame())); + if (pFrame) + { + GtkWindow* const pParent(GTK_WINDOW(pFrame->getWindow())); + if (pParent) + gtk_window_set_transient_for(GTK_WINDOW(m_pDialog), pParent); + } + } + + m_xWrapper->print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(m_pDialog), + GtkPrintCapabilities(GTK_PRINT_CAPABILITY_COPIES + | GTK_PRINT_CAPABILITY_COLLATE + | GTK_PRINT_CAPABILITY_REVERSE + | GTK_PRINT_CAPABILITY_GENERATE_PS + | GTK_PRINT_CAPABILITY_NUMBER_UP + | GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT + )); +} + +void +GtkPrintDialog::impl_initCustomTab() +{ + typedef std::vector<std::pair<GtkWidget*, OUString> > CustomTabs_t; + + const uno::Sequence<beans::PropertyValue>& rOptions(m_rController.getUIOptions()); + std::map<OUString, GtkWidget*> aPropertyToDependencyRowMap; + CustomTabs_t aCustomTabs; + GtkWidget* pCurParent = nullptr; + GtkWidget* pCurTabPage = nullptr; + GtkWidget* pCurSubGroup = nullptr; + bool bIgnoreSubgroup = false; + for (const auto& rOption : rOptions) + { + uno::Sequence<beans::PropertyValue> aOptProp; + rOption.Value >>= aOptProp; + + OUString aCtrlType; + OUString aText; + OUString aPropertyName; + uno::Sequence<OUString> aChoices; + uno::Sequence<sal_Bool> aChoicesDisabled; + uno::Sequence<OUString> aHelpTexts; + sal_Int64 nMinValue = 0, nMaxValue = 0; + sal_Int32 nCurHelpText = 0; + OUString aDependsOnName; + sal_Int32 nDependsOnValue = 0; + bool bUseDependencyRow = false; + bool bIgnore = false; + GtkWidget* pGroup = nullptr; + bool bGtkInternal = false; + + //Fix fdo#69381 + //Next options if this one is empty + if (!aOptProp.hasElements()) + continue; + + for (const beans::PropertyValue& rEntry : std::as_const(aOptProp)) + { + if ( rEntry.Name == "Text" ) + { + OUString aValue; + rEntry.Value >>= aValue; + aText = aValue.replace('~', '_'); + } + else if ( rEntry.Name == "ControlType" ) + rEntry.Value >>= aCtrlType; + else if ( rEntry.Name == "Choices" ) + rEntry.Value >>= aChoices; + else if ( rEntry.Name == "ChoicesDisabled" ) + rEntry.Value >>= aChoicesDisabled; + else if ( rEntry.Name == "Property" ) + { + beans::PropertyValue aVal; + rEntry.Value >>= aVal; + aPropertyName = aVal.Name; + } + else if ( rEntry.Name == "DependsOnName" ) + rEntry.Value >>= aDependsOnName; + else if ( rEntry.Name == "DependsOnEntry" ) + rEntry.Value >>= nDependsOnValue; + else if ( rEntry.Name == "AttachToDependency" ) + rEntry.Value >>= bUseDependencyRow; + else if ( rEntry.Name == "MinValue" ) + rEntry.Value >>= nMinValue; + else if ( rEntry.Name == "MaxValue" ) + rEntry.Value >>= nMaxValue; + else if ( rEntry.Name == "HelpId" ) + { + uno::Sequence<OUString> aHelpIds; + lcl_extractHelpTextsOrIds(rEntry, aHelpIds); + Help* const pHelp = Application::GetHelp(); + if (pHelp) + { + const int nLen = aHelpIds.getLength(); + aHelpTexts.realloc(nLen); + std::transform(aHelpIds.begin(), aHelpIds.end(), aHelpTexts.begin(), + [&pHelp](const OUString& rHelpId) { return pHelp->GetHelpText(rHelpId, static_cast<weld::Widget*>(nullptr)); }); + } + else // fallback + aHelpTexts = aHelpIds; + } + else if ( rEntry.Name == "HelpText" ) + lcl_extractHelpTextsOrIds(rEntry, aHelpTexts); + else if ( rEntry.Name == "InternalUIOnly" ) + rEntry.Value >>= bIgnore; + else if ( rEntry.Name == "Enabled" ) + { + // Ignore this. We use UIControlOptions::isUIOptionEnabled + // to check whether a control should be enabled. + } + else if ( rEntry.Name == "GroupingHint" ) + { + // Ignore this. We cannot add/modify controls to/on existing + // tabs of the Gtk print dialog. + } + else + { + SAL_INFO("vcl.gtk", "unhandled UI option entry: " << rEntry.Name); + } + } + + if ( aPropertyName == "PrintContent" ) + bGtkInternal = true; + + if (aCtrlType == "Group" || !pCurParent) + { + pCurTabPage = gtk_vbox_new(FALSE, 12); + gtk_container_set_border_width(GTK_CONTAINER(pCurTabPage), 6); + lcl_setHelpText(pCurTabPage, aHelpTexts, 0); + + pCurParent = pCurTabPage; + aCustomTabs.emplace_back(pCurTabPage, aText); + } + else if (aCtrlType == "Subgroup") + { + bIgnoreSubgroup = bIgnore; + if (bIgnore) + continue; + pCurParent = gtk_vbox_new(FALSE, 12); + gtk_container_set_border_width(GTK_CONTAINER(pCurParent), 0); + + pCurSubGroup = lcl_makeFrame(pCurParent, aText, aHelpTexts, nullptr); + gtk_box_pack_start(GTK_BOX(pCurTabPage), pCurSubGroup, FALSE, FALSE, 0); + } + // special case: we need to map these to controls of the gtk print dialog + else if (bGtkInternal) + { + if ( aPropertyName == "PrintContent" ) + { + // What to print? And, more importantly, is there a selection? + impl_initPrintContent(aChoicesDisabled); + } + } + else if (bIgnoreSubgroup || bIgnore) + continue; + else + { + // change handlers for all the controls set up in this block + // should be set _after_ the control has been made (in)active, + // because: + // 1. value of the property is _known_--we are using it to + // _set_ the control, right?--no need to change it back .-) + // 2. it may cause warning because the widget may not + // have been placed in m_aControlToPropertyMap yet + + GtkWidget* pWidget = nullptr; + beans::PropertyValue* pVal = nullptr; + if (aCtrlType == "Bool" && pCurParent) + { + pWidget = gtk_check_button_new_with_mnemonic( + OUStringToOString(aText, RTL_TEXTENCODING_UTF8).getStr()); + lcl_setHelpText(pWidget, aHelpTexts, 0); + m_aControlToPropertyMap[pWidget] = aPropertyName; + + bool bVal = false; + pVal = m_rController.getValue(aPropertyName); + if (pVal) + pVal->Value >>= bVal; + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pWidget), bVal); + gtk_widget_set_sensitive(pWidget, + m_rController.isUIOptionEnabled(aPropertyName) && pVal != nullptr); + g_signal_connect(pWidget, "toggled", G_CALLBACK(GtkPrintDialog::UIOption_CheckHdl), this); + } + else if (aCtrlType == "Radio" && pCurParent) + { + GtkWidget* const pVbox = gtk_vbox_new(FALSE, 12); + gtk_container_set_border_width(GTK_CONTAINER(pVbox), 0); + + if (!aText.isEmpty()) + pGroup = lcl_makeFrame(pVbox, aText, aHelpTexts, &nCurHelpText); + + sal_Int32 nSelectVal = 0; + pVal = m_rController.getValue(aPropertyName); + if (pVal && pVal->Value.hasValue()) + pVal->Value >>= nSelectVal; + + for (sal_Int32 m = 0; m != aChoices.getLength(); m++) + { + pWidget = gtk_radio_button_new_with_mnemonic_from_widget( + GTK_RADIO_BUTTON(m == 0 ? nullptr : pWidget), + OUStringToOString(aChoices[m].replace('~', '_'), RTL_TEXTENCODING_UTF8).getStr()); + lcl_setHelpText(pWidget, aHelpTexts, nCurHelpText++); + m_aControlToPropertyMap[pWidget] = aPropertyName; + m_aControlToNumValMap[pWidget] = m; + GtkWidget* const pRow = gtk_hbox_new(FALSE, 12); + gtk_box_pack_start(GTK_BOX(pVbox), pRow, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(pRow), pWidget, FALSE, FALSE, 0); + aPropertyToDependencyRowMap[aPropertyName + OUString::number(m)] = pRow; + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pWidget), m == nSelectVal); + gtk_widget_set_sensitive(pWidget, + m_rController.isUIOptionEnabled(aPropertyName) && pVal != nullptr); + g_signal_connect(pWidget, "toggled", + G_CALLBACK(GtkPrintDialog::UIOption_RadioHdl), this); + } + + if (pGroup) + pWidget = pGroup; + else + pWidget = pVbox; + } + else if ((aCtrlType == "List" || + aCtrlType == "Range" || + aCtrlType == "Edit" + ) && pCurParent) + { + GtkWidget* const pHbox = gtk_hbox_new(FALSE, 12); + gtk_container_set_border_width(GTK_CONTAINER(pHbox), 0); + + if ( aCtrlType == "List" ) + { + pWidget = lcl_combo_box_text_new(); + + for (const auto& rChoice : std::as_const(aChoices)) + { + lcl_combo_box_text_append(pWidget, + OUStringToOString(rChoice, RTL_TEXTENCODING_UTF8).getStr()); + } + + sal_Int32 nSelectVal = 0; + pVal = m_rController.getValue(aPropertyName); + if (pVal && pVal->Value.hasValue()) + pVal->Value >>= nSelectVal; + gtk_combo_box_set_active(GTK_COMBO_BOX(pWidget), nSelectVal); + g_signal_connect(pWidget, "changed", G_CALLBACK(GtkPrintDialog::UIOption_SelectHdl), this); + } + else if (aCtrlType == "Edit" && pCurParent) + { + pWidget = gtk_entry_new(); + + OUString aCurVal; + pVal = m_rController.getValue(aPropertyName); + if (pVal && pVal->Value.hasValue()) + pVal->Value >>= aCurVal; + gtk_entry_set_text(GTK_ENTRY(pWidget), + OUStringToOString(aCurVal, RTL_TEXTENCODING_UTF8).getStr()); + } + else if (aCtrlType == "Range" && pCurParent) + { + pWidget = gtk_spin_button_new_with_range(nMinValue, nMaxValue, 1.0); + + sal_Int64 nCurVal = 0; + pVal = m_rController.getValue(aPropertyName); + if (pVal && pVal->Value.hasValue()) + pVal->Value >>= nCurVal; + gtk_spin_button_set_value(GTK_SPIN_BUTTON(pWidget), nCurVal); + } + + lcl_setHelpText(pWidget, aHelpTexts, 0); + m_aControlToPropertyMap[pWidget] = aPropertyName; + + gtk_widget_set_sensitive(pWidget, + m_rController.isUIOptionEnabled(aPropertyName) && pVal != nullptr); + + if (!aText.isEmpty()) + { + GtkWidget* const pLabel = gtk_label_new_with_mnemonic( + OUStringToOString(aText, RTL_TEXTENCODING_UTF8).getStr()); + gtk_label_set_mnemonic_widget(GTK_LABEL(pLabel), pWidget); + gtk_box_pack_start(GTK_BOX(pHbox), pLabel, FALSE, FALSE, 0); + } + + gtk_box_pack_start(GTK_BOX(pHbox), pWidget, FALSE, FALSE, 0); + + pWidget = pHbox; + + } + else + SAL_INFO("vcl.gtk", "unhandled option type: " << aCtrlType); + + GtkWidget* pRow = nullptr; + if (pWidget) + { + if (bUseDependencyRow && !aDependsOnName.isEmpty()) + { + pRow = aPropertyToDependencyRowMap[aDependsOnName + OUString::number(nDependsOnValue)]; + if (!pRow) + { + gtk_widget_destroy(pWidget); + pWidget = nullptr; + } + } + } + if (pWidget) + { + if (!pRow) + { + pRow = gtk_hbox_new(FALSE, 12); + gtk_box_pack_start(GTK_BOX(pCurParent), pRow, FALSE, FALSE, 0); + } + if (!pGroup) + aPropertyToDependencyRowMap[aPropertyName + OUString::number(0)] = pRow; + gtk_box_pack_start(GTK_BOX(pRow), pWidget, FALSE, FALSE, 0); + } + } + } + + CustomTabs_t::const_reverse_iterator aEnd = aCustomTabs.rend(); + for (CustomTabs_t::const_reverse_iterator aI = aCustomTabs.rbegin(); aI != aEnd; ++aI) + { + gtk_widget_show_all(aI->first); + m_xWrapper->print_unix_dialog_add_custom_tab(GTK_PRINT_UNIX_DIALOG(m_pDialog), aI->first, + gtk_label_new(OUStringToOString(aI->second, RTL_TEXTENCODING_UTF8).getStr())); + } +} + +void +GtkPrintDialog::impl_initPrintContent(uno::Sequence<sal_Bool> const& i_rDisabled) +{ + SAL_WARN_IF(i_rDisabled.getLength() != 3, "vcl.gtk", "there is more choices than we expected"); + if (i_rDisabled.getLength() != 3) + return; + + GtkPrintUnixDialog* const pDialog(GTK_PRINT_UNIX_DIALOG(m_pDialog)); + + // XXX: This is a hack that depends on the number and the ordering of + // the controls in the rDisabled sequence (cf. the initialization of + // the "PrintContent" UI option in SwPrintUIOptions::SwPrintUIOptions, + // sw/source/core/view/printdata.cxx) + if (m_xWrapper->supportsPrintSelection() && !i_rDisabled[2]) + { + m_xWrapper->print_unix_dialog_set_support_selection(pDialog, TRUE); + m_xWrapper->print_unix_dialog_set_has_selection(pDialog, TRUE); + } + + beans::PropertyValue* const pPrintContent( + m_rController.getValue(OUString("PrintContent"))); + + if (pPrintContent) + { + sal_Int32 nSelectionType(0); + pPrintContent->Value >>= nSelectionType; + GtkPrintSettings* const pSettings(getSettings()); + GtkPrintPages ePrintPages(GTK_PRINT_PAGES_ALL); + switch (nSelectionType) + { + case 0: + ePrintPages = GTK_PRINT_PAGES_ALL; + break; + case 1: + ePrintPages = GTK_PRINT_PAGES_RANGES; + break; + case 2: + if (m_xWrapper->supportsPrintSelection()) + ePrintPages = GTK_PRINT_PAGES_SELECTION; + else + SAL_INFO("vcl.gtk", "the application wants to print a selection, but the present gtk version does not support it"); + break; + default: + SAL_WARN("vcl.gtk", "unexpected selection type: " << nSelectionType); + } + m_xWrapper->print_settings_set_print_pages(pSettings, ePrintPages); + m_xWrapper->print_unix_dialog_set_settings(pDialog, pSettings); + g_object_unref(G_OBJECT(pSettings)); + } +} + +void +GtkPrintDialog::impl_checkOptionalControlDependencies() +{ + for (auto& rEntry : m_aControlToPropertyMap) + { + gtk_widget_set_sensitive(rEntry.first, m_rController.isUIOptionEnabled(rEntry.second)); + } +} + +beans::PropertyValue* +GtkPrintDialog::impl_queryPropertyValue(GtkWidget* const i_pWidget) const +{ + beans::PropertyValue* pVal(nullptr); + std::map<GtkWidget*, OUString>::const_iterator aIt(m_aControlToPropertyMap.find(i_pWidget)); + if (aIt != m_aControlToPropertyMap.end()) + { + pVal = m_rController.getValue(aIt->second); + SAL_WARN_IF(!pVal, "vcl.gtk", "property value not found"); + } + else + { + SAL_WARN("vcl.gtk", "changed control not in property map"); + } + return pVal; +} + +void +GtkPrintDialog::impl_UIOption_CheckHdl(GtkWidget* const i_pWidget) +{ + beans::PropertyValue* const pVal = impl_queryPropertyValue(i_pWidget); + if (pVal) + { + const bool bVal = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(i_pWidget)); + pVal->Value <<= bVal; + + impl_checkOptionalControlDependencies(); + } +} + +void +GtkPrintDialog::impl_UIOption_RadioHdl(GtkWidget* const i_pWidget) +{ + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(i_pWidget))) + { + beans::PropertyValue* const pVal = impl_queryPropertyValue(i_pWidget); + std::map<GtkWidget*, sal_Int32>::const_iterator it = m_aControlToNumValMap.find(i_pWidget); + if (pVal && it != m_aControlToNumValMap.end()) + { + + const sal_Int32 nVal = it->second; + pVal->Value <<= nVal; + + impl_checkOptionalControlDependencies(); + } + } +} + +void +GtkPrintDialog::impl_UIOption_SelectHdl(GtkWidget* const i_pWidget) +{ + beans::PropertyValue* const pVal = impl_queryPropertyValue(i_pWidget); + if (pVal) + { + const sal_Int32 nVal(gtk_combo_box_get_active(GTK_COMBO_BOX(i_pWidget))); + pVal->Value <<= nVal; + + impl_checkOptionalControlDependencies(); + } +} + +bool +GtkPrintDialog::run() +{ + bool bDoJob = false; + bool bContinue = true; + while (bContinue) + { + bContinue = false; + const gint nStatus = gtk_dialog_run(GTK_DIALOG(m_pDialog)); + switch (nStatus) + { + case GTK_RESPONSE_HELP: + fprintf(stderr, "To-Do: Help ?\n"); + bContinue = true; + break; + case GTK_RESPONSE_OK: + bDoJob = true; + break; + default: + break; + } + } + gtk_widget_hide(m_pDialog); + impl_storeToSettings(); + return bDoJob; +} + +void +GtkPrintDialog::updateControllerPrintRange() +{ + GtkPrintSettings* const pSettings(getSettings()); + // TODO: use get_print_pages + if (const gchar* const pStr = m_xWrapper->print_settings_get(pSettings, GTK_PRINT_SETTINGS_PRINT_PAGES)) + { + beans::PropertyValue* pVal = m_rController.getValue(OUString("PrintRange")); + if (!pVal) + pVal = m_rController.getValue(OUString("PrintContent")); + SAL_WARN_IF(!pVal, "vcl.gtk", "Nothing to map standard print options to!"); + if (pVal) + { + sal_Int32 nVal = 0; + if (!strcmp(pStr, "all")) + nVal = 0; + else if (!strcmp(pStr, "ranges")) + nVal = 1; + else if (!strcmp(pStr, "selection")) + nVal = 2; + pVal->Value <<= nVal; + + if (nVal == 1) + { + pVal = m_rController.getValue(OUString("PageRange")); + SAL_WARN_IF(!pVal, "vcl.gtk", "PageRange doesn't exist!"); + if (pVal) + { + OUStringBuffer sBuf; + gint num_ranges; + const GtkPageRange* const pRanges = m_xWrapper->print_settings_get_page_ranges(pSettings, &num_ranges); + for (gint i = 0; i != num_ranges && pRanges; ++i) + { + sBuf.append(sal_Int32(pRanges[i].start+1)); + if (pRanges[i].start != pRanges[i].end) + { + sBuf.append('-'); + sBuf.append(sal_Int32(pRanges[i].end+1)); + } + + if (i != num_ranges-1) + sBuf.append(','); + } + pVal->Value <<= sBuf.makeStringAndClear(); + } + } + } + } + g_object_unref(G_OBJECT(pSettings)); +} + +GtkPrintDialog::~GtkPrintDialog() +{ + gtk_widget_destroy(m_pDialog); +} + +void +GtkPrintDialog::impl_readFromSettings() +{ + vcl::SettingsConfigItem* const pItem(vcl::SettingsConfigItem::get()); + GtkPrintSettings* const pSettings(getSettings()); + + const OUString aPrintDialogStr("PrintDialog"); + const OUString aCopyCount(pItem->getValue(aPrintDialogStr, + "CopyCount")); + const OUString aCollate(pItem->getValue(aPrintDialogStr, + "Collate")); + + const gint nOldCopyCount(m_xWrapper->print_settings_get_n_copies(pSettings)); + const sal_Int32 nCopyCount(aCopyCount.toInt32()); + if (nCopyCount > 0 && nOldCopyCount != nCopyCount) + { + m_xWrapper->print_settings_set_n_copies(pSettings, sal::static_int_cast<gint>(nCopyCount)); + } + + const bool bOldCollate(m_xWrapper->print_settings_get_collate(pSettings)); + const bool bCollate(aCollate.equalsIgnoreAsciiCase("true")); + if (bOldCollate != bCollate) + { + m_xWrapper->print_settings_set_collate(pSettings, bCollate); + } + + m_xWrapper->print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(m_pDialog), pSettings); + g_object_unref(G_OBJECT(pSettings)); +} + +void +GtkPrintDialog::impl_storeToSettings() +const +{ + vcl::SettingsConfigItem* const pItem(vcl::SettingsConfigItem::get()); + GtkPrintSettings* const pSettings(getSettings()); + + const OUString aPrintDialogStr("PrintDialog"); + pItem->setValue(aPrintDialogStr, + "CopyCount", + OUString::number(m_xWrapper->print_settings_get_n_copies(pSettings))); + pItem->setValue(aPrintDialogStr, + "Collate", + m_xWrapper->print_settings_get_collate(pSettings) + ? OUString("true") + : OUString("false")) + ; + // pItem->setValue(aPrintDialog, OUString("ToFile"), ); + g_object_unref(G_OBJECT(pSettings)); + pItem->Commit(); +} + +sal_uInt32 +GtkSalInfoPrinter::GetCapabilities( + const ImplJobSetup* const i_pSetupData, + const PrinterCapType i_nType) +{ + if (i_nType == PrinterCapType::ExternalDialog && lcl_useSystemPrintDialog()) + return 1; + return PspSalInfoPrinter::GetCapabilities(i_pSetupData, i_nType); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |