tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / forms / source / component / GroupManager.hxx
blob4cefd7ac3a125d17407b19ec0076c80346b3344c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <com/sun/star/awt/XControlModel.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
25 #include <com/sun/star/container/XContainerListener.hpp>
26 #include <com/sun/star/container/XContainer.hpp>
27 #include <cppuhelper/implbase.hxx>
29 #include <map>
30 #include <memory>
31 #include <vector>
34 * The GroupManager listens at the StarForm for FormComponent insertion and removal as well as
35 * its properties "Name" and "TabIndex" and updates its Group using this information.
37 * The GroupManager manages a Group in which all Controls are sorted by TabIndex.
38 * It also manages an array of Groups, in which each FormComponent is assigned a
39 * Group according to its name.
40 * Each Group is activated using a Map, if they contain more than one element.
42 * The Groups manage the FormComponents internally using two arrays.
43 * In the GroupCompArray the Components are sorted by TabIndex and insertion position.
44 * Because this array is accessed via the FormComponent, we also have the GroupCompAccessArray
45 * in which the FormComponents are sorted by their storage address.
46 * Every element of the GroupCompArray has a pointer to the GroupCompArray.
48 namespace frm
52 template <class ELEMENT, class LESS_COMPARE>
53 sal_Int32 insert_sorted(::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, const LESS_COMPARE& _rCompareOp)
55 typename ::std::vector<ELEMENT>::iterator aInsertPos = ::std::lower_bound(
56 _rArray.begin(),
57 _rArray.end(),
58 _rNewElement,
59 _rCompareOp
61 aInsertPos = _rArray.insert(aInsertPos, _rNewElement);
62 return aInsertPos - _rArray.begin();
65 template <class ELEMENT, class LESS_COMPARE>
66 bool seek_entry(const ::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, sal_Int32& nPos, const LESS_COMPARE& _rCompareOp)
68 typename ::std::vector<ELEMENT>::const_iterator aExistentPos = ::std::lower_bound(
69 _rArray.begin(),
70 _rArray.end(),
71 _rNewElement,
72 _rCompareOp
74 if ((aExistentPos != _rArray.end()) && (*aExistentPos == _rNewElement))
75 { // we have a valid "lower or equal" element and it's really "equal"
76 nPos = aExistentPos - _rArray.begin();
77 return true;
79 nPos = -1;
80 return false;
84 class OGroupComp
86 css::uno::Reference< css::beans::XPropertySet> m_xComponent;
87 css::uno::Reference< css::awt::XControlModel> m_xControlModel;
88 sal_Int32 m_nPos;
89 sal_Int16 m_nTabIndex;
91 friend class OGroupCompLess;
93 public:
94 OGroupComp(const css::uno::Reference< css::beans::XPropertySet>& rxElement, sal_Int32 nInsertPos );
95 OGroupComp();
97 bool operator==( const OGroupComp& rComp ) const;
99 const css::uno::Reference< css::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
100 const css::uno::Reference< css::awt::XControlModel>& GetControlModel() const { return m_xControlModel; }
102 sal_Int32 GetPos() const { return m_nPos; }
103 sal_Int16 GetTabIndex() const { return m_nTabIndex; }
107 class OGroupComp;
108 class OGroupCompAcc
110 css::uno::Reference< css::beans::XPropertySet> m_xComponent;
112 OGroupComp m_aGroupComp;
114 friend class OGroupCompAccLess;
116 public:
117 OGroupCompAcc(const css::uno::Reference< css::beans::XPropertySet>& rxElement, OGroupComp _aGroupComp );
119 bool operator==( const OGroupCompAcc& rCompAcc ) const;
121 const OGroupComp& GetGroupComponent() const { return m_aGroupComp; }
124 class OGroup final
126 std::vector<OGroupComp> m_aCompArray;
127 std::vector<OGroupCompAcc> m_aCompAccArray;
129 OUString m_aGroupName;
130 sal_uInt16 m_nInsertPos; // The insertion position of the GroupComps is determined by the Group
132 friend class OGroupLess;
134 public:
135 explicit OGroup(OUString sGroupName);
137 const OUString& GetGroupName() const { return m_aGroupName; }
138 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> > GetControlModels() const;
140 void InsertComponent( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
141 void RemoveComponent( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
142 sal_uInt16 Count() const { return sal::static_int_cast< sal_uInt16 >(m_aCompArray.size()); }
143 const css::uno::Reference< css::beans::XPropertySet>& GetObject( sal_uInt16 nP ) const
144 { return m_aCompArray[nP].GetComponent(); }
147 typedef std::map<OUString, OGroup> OGroupArr;
148 typedef std::vector<OGroupArr::iterator> OActiveGroups;
151 class OGroupManager : public ::cppu::WeakImplHelper< css::beans::XPropertyChangeListener, css::container::XContainerListener>
153 std::unique_ptr<OGroup>
154 m_pCompGroup; // Sort all Components by TabIndices
155 OGroupArr m_aGroupArr; // Sort all Components by group
156 OActiveGroups m_aActiveGroupMap; // This map contains all indices of all groups with more than 1 element
158 css::uno::Reference< css::container::XContainer >
159 m_xContainer;
161 // Helper functions
162 void InsertElement( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
163 void RemoveElement( const css::uno::Reference< css::beans::XPropertySet>& rxElement );
164 void removeFromGroupMap(const OUString& _sGroupName,const css::uno::Reference< css::beans::XPropertySet>& _xSet);
166 public:
167 explicit OGroupManager(const css::uno::Reference< css::container::XContainer >& _rxContainer);
168 virtual ~OGroupManager() override;
170 // css::lang::XEventListener
171 virtual void SAL_CALL disposing(const css::lang::EventObject& _rSource) override;
173 // css::beans::XPropertyChangeListener
174 virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent& evt) override;
176 // css::container::XContainerListener
177 virtual void SAL_CALL elementInserted(const css::container::ContainerEvent& _rEvent) override;
178 virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent& _rEvent) override;
179 virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent& _rEvent) override;
181 // Other functions
182 sal_Int32 getGroupCount() const;
183 void getGroup(sal_Int32 nGroup, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> >& _rGroup, OUString& Name);
184 void getGroupByName(const OUString& Name, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> >& _rGroup);
185 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel> > getControlModels() const;
187 static OUString GetGroupName( const css::uno::Reference< css::beans::XPropertySet>& xComponent );
191 } // namespace frm
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */