a11y: Simplify OCommonAccessibleComponent::getAccessibleIndexInParent
[LibreOffice.git] / cui / source / customize / SvxConfigPageHelper.cxx
blob522dfd3eb721a1c196987530220f58770c94b67e
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 #include <SvxConfigPageHelper.hxx>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <com/sun/star/ui/ImageType.hpp>
24 #include <com/sun/star/ui/ItemType.hpp>
26 #include <comphelper/propertyvalue.hxx>
27 #include <comphelper/random.hxx>
28 #include <svtools/imgdef.hxx>
29 #include <svtools/miscopt.hxx>
31 static sal_Int16 theImageType = css::ui::ImageType::COLOR_NORMAL | css::ui::ImageType::SIZE_DEFAULT;
33 void SvxConfigPageHelper::RemoveEntry(SvxEntries* pEntries, SvxConfigEntry const* pChildEntry)
35 SvxEntries::iterator iter = pEntries->begin();
37 while (iter != pEntries->end())
39 if (pChildEntry == *iter)
41 pEntries->erase(iter);
42 break;
44 ++iter;
48 OUString SvxConfigPageHelper::replaceSaveInName(const OUString& rMessage,
49 std::u16string_view rSaveInName)
51 OUString name = rMessage.replaceFirst("%SAVE IN SELECTION%", rSaveInName);
53 return name;
56 OUString SvxConfigPageHelper::stripHotKey(const OUString& str) { return str.replaceFirst("~", ""); }
58 OUString SvxConfigPageHelper::replaceSixteen(const OUString& str, sal_Int32 nReplacement)
60 return str.replaceAll(OUString::number(16), OUString::number(nReplacement));
63 sal_Int16 SvxConfigPageHelper::GetImageType() { return theImageType; }
65 void SvxConfigPageHelper::InitImageType()
67 theImageType = css::ui::ImageType::COLOR_NORMAL | css::ui::ImageType::SIZE_DEFAULT;
69 if (SvtMiscOptions::GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE)
71 theImageType |= css::ui::ImageType::SIZE_LARGE;
73 else if (SvtMiscOptions::GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32)
75 theImageType |= css::ui::ImageType::SIZE_32;
79 css::uno::Reference<css::graphic::XGraphic>
80 SvxConfigPageHelper::GetGraphic(const css::uno::Reference<css::ui::XImageManager>& xImageManager,
81 const OUString& rCommandURL)
83 css::uno::Reference<css::graphic::XGraphic> result;
85 if (xImageManager.is())
87 // TODO handle large graphics
88 css::uno::Sequence<css::uno::Reference<css::graphic::XGraphic>> aGraphicSeq;
90 css::uno::Sequence<OUString> aImageCmdSeq{ rCommandURL };
92 try
94 aGraphicSeq = xImageManager->getImages(GetImageType(), aImageCmdSeq);
96 if (aGraphicSeq.hasElements())
98 result = aGraphicSeq[0];
101 catch (css::uno::Exception&)
103 // will return empty XGraphic
107 return result;
110 OUString SvxConfigPageHelper::generateCustomName(const OUString& prefix, SvxEntries* entries,
111 sal_Int32 suffix /*= 1*/)
113 OUString name;
114 sal_Int32 pos = 0;
116 // find and replace the %n placeholder in the prefix string
117 name = prefix.replaceFirst("%n", OUString::number(suffix), &pos);
119 if (pos == -1)
121 // no placeholder found so just append the suffix
122 name += OUString::number(suffix);
125 if (!entries)
126 return name;
128 // now check if there is an already existing entry with this name
129 bool bFoundEntry = false;
130 for (auto const& entry : *entries)
132 if (name.equals(entry->GetName()))
134 bFoundEntry = true;
135 break;
139 if (bFoundEntry)
141 // name already exists so try the next number up
142 return generateCustomName(prefix, entries, ++suffix);
145 return name;
148 OUString SvxConfigPageHelper::generateCustomMenuURL(SvxEntries* entries, sal_Int32 suffix /*= 1*/)
150 OUString url = "vnd.openoffice.org:CustomMenu" + OUString::number(suffix);
151 if (!entries)
152 return url;
154 // now check is there is an already existing entry with this url
155 bool bFoundEntry = false;
156 for (auto const& entry : *entries)
158 if (url.equals(entry->GetCommand()))
160 bFoundEntry = true;
161 break;
165 if (bFoundEntry)
167 // url already exists so try the next number up
168 return generateCustomMenuURL(entries, ++suffix);
171 return url;
174 sal_uInt32 SvxConfigPageHelper::generateRandomValue()
176 return comphelper::rng::uniform_uint_distribution(0, std::numeric_limits<unsigned int>::max());
179 OUString SvxConfigPageHelper::generateCustomURL(SvxEntries* entries)
181 OUString url = OUString::Concat(ITEM_TOOLBAR_URL) + CUSTOM_TOOLBAR_STR +
182 // use a random number to minimize possible clash with existing custom toolbars
183 OUString::number(generateRandomValue(), 16);
185 // now check is there is an already existing entry with this url
186 bool bFoundEntry = false;
187 for (auto const& entry : *entries)
189 if (url.equals(entry->GetCommand()))
191 bFoundEntry = true;
192 break;
196 if (bFoundEntry)
198 // url already exists so try the next number up
199 return generateCustomURL(entries);
202 return url;
205 OUString SvxConfigPageHelper::GetModuleName(std::u16string_view aModuleId)
207 if (aModuleId == u"com.sun.star.text.TextDocument"
208 || aModuleId == u"com.sun.star.text.GlobalDocument")
209 return u"Writer"_ustr;
210 else if (aModuleId == u"com.sun.star.text.WebDocument")
211 return u"Writer/Web"_ustr;
212 else if (aModuleId == u"com.sun.star.drawing.DrawingDocument")
213 return u"Draw"_ustr;
214 else if (aModuleId == u"com.sun.star.presentation.PresentationDocument")
215 return u"Impress"_ustr;
216 else if (aModuleId == u"com.sun.star.sheet.SpreadsheetDocument")
217 return u"Calc"_ustr;
218 else if (aModuleId == u"com.sun.star.script.BasicIDE")
219 return u"Basic"_ustr;
220 else if (aModuleId == u"com.sun.star.formula.FormulaProperties")
221 return u"Math"_ustr;
222 else if (aModuleId == u"com.sun.star.sdb.RelationDesign")
223 return u"Relation Design"_ustr;
224 else if (aModuleId == u"com.sun.star.sdb.QueryDesign")
225 return u"Query Design"_ustr;
226 else if (aModuleId == u"com.sun.star.sdb.TableDesign")
227 return u"Table Design"_ustr;
228 else if (aModuleId == u"com.sun.star.sdb.DataSourceBrowser")
229 return u"Data Source Browser"_ustr;
230 else if (aModuleId == u"com.sun.star.sdb.DatabaseDocument")
231 return u"Database"_ustr;
233 return OUString();
236 OUString SvxConfigPageHelper::GetUIModuleName(
237 const OUString& aModuleId,
238 const css::uno::Reference<css::frame::XModuleManager2>& rModuleManager)
240 assert(rModuleManager.is());
242 OUString aModuleUIName;
246 css::uno::Any a = rModuleManager->getByName(aModuleId);
247 css::uno::Sequence<css::beans::PropertyValue> aSeq;
249 if (a >>= aSeq)
251 for (css::beans::PropertyValue const& rProp : aSeq)
253 if (rProp.Name == "ooSetupFactoryUIName")
255 rProp.Value >>= aModuleUIName;
256 break;
261 catch (css::uno::RuntimeException&)
263 throw;
265 catch (css::uno::Exception&)
269 if (aModuleUIName.isEmpty())
270 aModuleUIName = GetModuleName(aModuleId);
272 return aModuleUIName;
275 bool SvxConfigPageHelper::GetMenuItemData(
276 const css::uno::Reference<css::container::XIndexAccess>& rItemContainer, sal_Int32 nIndex,
277 OUString& rCommandURL, OUString& rLabel, sal_uInt16& rType, sal_Int32& rStyle,
278 css::uno::Reference<css::container::XIndexAccess>& rSubMenu)
282 css::uno::Sequence<css::beans::PropertyValue> aProps;
283 if (rItemContainer->getByIndex(nIndex) >>= aProps)
285 for (css::beans::PropertyValue const& rProp : aProps)
287 if (rProp.Name == ITEM_DESCRIPTOR_COMMANDURL)
289 rProp.Value >>= rCommandURL;
291 else if (rProp.Name == ITEM_DESCRIPTOR_CONTAINER)
293 rProp.Value >>= rSubMenu;
295 else if (rProp.Name == ITEM_DESCRIPTOR_STYLE)
297 rProp.Value >>= rStyle;
299 else if (rProp.Name == ITEM_DESCRIPTOR_LABEL)
301 rProp.Value >>= rLabel;
303 else if (rProp.Name == ITEM_DESCRIPTOR_TYPE)
305 rProp.Value >>= rType;
309 return true;
312 catch (css::lang::IndexOutOfBoundsException&)
316 return false;
319 bool SvxConfigPageHelper::GetToolbarItemData(
320 const css::uno::Reference<css::container::XIndexAccess>& rItemContainer, sal_Int32 nIndex,
321 OUString& rCommandURL, OUString& rLabel, sal_uInt16& rType, bool& rIsVisible, sal_Int32& rStyle)
325 css::uno::Sequence<css::beans::PropertyValue> aProps;
326 if (rItemContainer->getByIndex(nIndex) >>= aProps)
328 for (css::beans::PropertyValue const& rProp : aProps)
330 if (rProp.Name == ITEM_DESCRIPTOR_COMMANDURL)
332 rProp.Value >>= rCommandURL;
334 else if (rProp.Name == ITEM_DESCRIPTOR_STYLE)
336 rProp.Value >>= rStyle;
338 else if (rProp.Name == ITEM_DESCRIPTOR_LABEL)
340 rProp.Value >>= rLabel;
342 else if (rProp.Name == ITEM_DESCRIPTOR_TYPE)
344 rProp.Value >>= rType;
346 else if (rProp.Name == ITEM_DESCRIPTOR_ISVISIBLE)
348 rProp.Value >>= rIsVisible;
352 return true;
355 catch (css::lang::IndexOutOfBoundsException&)
359 return false;
362 css::uno::Sequence<css::beans::PropertyValue>
363 SvxConfigPageHelper::ConvertSvxConfigEntry(const SvxConfigEntry* pEntry)
365 // If the name has not been changed, then the label can be stored
366 // as an empty string.
367 // It will be initialised again later using the command to label map.
368 OUString sLabel;
369 if (pEntry->HasChangedName() || pEntry->GetCommand().isEmpty())
370 sLabel = pEntry->GetName();
372 css::uno::Sequence<css::beans::PropertyValue> aPropSeq{
373 comphelper::makePropertyValue(ITEM_DESCRIPTOR_COMMANDURL, pEntry->GetCommand()),
374 comphelper::makePropertyValue(ITEM_DESCRIPTOR_TYPE, css::ui::ItemType::DEFAULT),
375 comphelper::makePropertyValue(ITEM_DESCRIPTOR_LABEL, sLabel),
376 comphelper::makePropertyValue(ITEM_DESCRIPTOR_STYLE,
377 static_cast<sal_Int16>(pEntry->GetStyle()))
380 return aPropSeq;
383 css::uno::Sequence<css::beans::PropertyValue>
384 SvxConfigPageHelper::ConvertToolbarEntry(const SvxConfigEntry* pEntry)
386 // If the name has not been changed, then the label can be stored
387 // as an empty string.
388 // It will be initialised again later using the command to label map.
389 OUString sLabel;
390 if (pEntry->HasChangedName() || pEntry->GetCommand().isEmpty())
391 sLabel = pEntry->GetName();
393 css::uno::Sequence<css::beans::PropertyValue> aPropSeq{
394 comphelper::makePropertyValue(ITEM_DESCRIPTOR_COMMANDURL, pEntry->GetCommand()),
395 comphelper::makePropertyValue(ITEM_DESCRIPTOR_TYPE, css::ui::ItemType::DEFAULT),
396 comphelper::makePropertyValue(ITEM_DESCRIPTOR_LABEL, sLabel),
397 comphelper::makePropertyValue(ITEM_DESCRIPTOR_ISVISIBLE, pEntry->IsVisible()),
398 comphelper::makePropertyValue(ITEM_DESCRIPTOR_STYLE,
399 static_cast<sal_Int16>(pEntry->GetStyle()))
402 return aPropSeq;
405 bool SvxConfigPageHelper::EntrySort(SvxConfigEntry const* a, SvxConfigEntry const* b)
407 return a->GetName().compareTo(b->GetName()) < 0;
410 bool SvxConfigPageHelper::SvxConfigEntryModified(SvxConfigEntry const* pEntry)
412 SvxEntries* pEntries = pEntry->GetEntries();
413 if (!pEntries)
414 return false;
416 for (const auto& entry : *pEntries)
418 if (entry->IsModified() || SvxConfigEntryModified(entry))
419 return true;
421 return false;
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */