LibreOffice Module sc (master) 1
stylehelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <string_view>
23
24#include <svl/style.hxx>
25#include <o3tl/string_view.hxx>
26#include <osl/diagnose.h>
27
28#include <stylehelper.hxx>
29#include <globstr.hrc>
30#include <scresid.hxx>
31
32namespace {
33
34struct ScDisplayNameMap
35{
36 OUString aDispName;
37 OUString aProgName;
38};
39
40}
41
42static const ScDisplayNameMap* lcl_GetStyleNameMap( SfxStyleFamily nType )
43{
44 if ( nType == SfxStyleFamily::Para )
45 {
46 static ScDisplayNameMap const aCellMap[]
47 {
48 // Standard builtin styles from configuration.
49 // Defined in sc/res/xml/styles.xml
50 // Installed to "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/calc/styles.xml"
51 // e.g. /usr/lib64/libreoffice/share/calc/styles.xml
52 // or instdir/share/calc/styles.xml
53 { ScResId( STR_STYLENAME_HEADING ), "Heading" },
54 { ScResId( STR_STYLENAME_HEADING_1 ), "Heading 1" },
55 { ScResId( STR_STYLENAME_HEADING_2 ), "Heading 2" },
56 { ScResId( STR_STYLENAME_TEXT ), "Text" },
57 { ScResId( STR_STYLENAME_NOTE ), "Note" },
58 { ScResId( STR_STYLENAME_FOOTNOTE ), "Footnote" },
59 { ScResId( STR_STYLENAME_HYPERLINK ), "Hyperlink" },
60 { ScResId( STR_STYLENAME_STATUS ), "Status" },
61 { ScResId( STR_STYLENAME_GOOD ), "Good" },
62 { ScResId( STR_STYLENAME_NEUTRAL ), "Neutral" },
63 { ScResId( STR_STYLENAME_BAD ), "Bad" },
64 { ScResId( STR_STYLENAME_WARNING ), "Warning" },
65 { ScResId( STR_STYLENAME_ERROR ), "Error" },
66 { ScResId( STR_STYLENAME_ACCENT ), "Accent" },
67 { ScResId( STR_STYLENAME_ACCENT_1 ), "Accent 1" },
68 { ScResId( STR_STYLENAME_ACCENT_2 ), "Accent 2" },
69 { ScResId( STR_STYLENAME_ACCENT_3 ), "Accent 3" },
70 { ScResId( STR_STYLENAME_RESULT ), "Result" },
71 // API compatibility programmatic names after.
72 { ScResId( STR_STYLENAME_STANDARD ), OUString(SC_STYLE_PROG_STANDARD) },
73 { ScResId( STR_STYLENAME_RESULT ), OUString(SC_STYLE_PROG_RESULT) },
74 { ScResId( STR_STYLENAME_RESULT1 ), OUString(SC_STYLE_PROG_RESULT1) },
75 { ScResId( STR_STYLENAME_HEADING ), OUString(SC_STYLE_PROG_HEADING) },
76 { ScResId( STR_STYLENAME_HEADING_1 ), OUString(SC_STYLE_PROG_HEADING1) },
77 // Pivot table styles.
78 { ScResId( STR_PIVOT_STYLENAME_INNER ), OUString(SC_PIVOT_STYLE_PROG_INNER) },
79 { ScResId( STR_PIVOT_STYLENAME_RESULT ), OUString(SC_PIVOT_STYLE_PROG_RESULT) },
80 { ScResId( STR_PIVOT_STYLENAME_CATEGORY ), OUString(SC_PIVOT_STYLE_PROG_CATEGORY) },
81 { ScResId( STR_PIVOT_STYLENAME_TITLE ), OUString(SC_PIVOT_STYLE_PROG_TITLE) },
82 { ScResId( STR_PIVOT_STYLENAME_FIELDNAME ), OUString(SC_PIVOT_STYLE_PROG_FIELDNAME) },
83 { ScResId( STR_PIVOT_STYLENAME_TOP ), OUString(SC_PIVOT_STYLE_PROG_TOP) },
84 // last entry remains empty
85 { OUString(), OUString() },
86 };
87 return aCellMap;
88 }
89 else if ( nType == SfxStyleFamily::Page )
90 {
91 static ScDisplayNameMap const aPageMap[]
92 {
93 { ScResId( STR_STYLENAME_STANDARD ), OUString(SC_STYLE_PROG_STANDARD) },
94 { ScResId( STR_STYLENAME_REPORT ), OUString(SC_STYLE_PROG_REPORT) },
95 // last entry remains empty
96 { OUString(), OUString() },
97 };
98 return aPageMap;
99 }
100 else if ( nType == SfxStyleFamily::Frame )
101 {
102 static ScDisplayNameMap const aGraphicMap[]
103 {
104 { ScResId( STR_STYLENAME_STANDARD ), OUString(SC_STYLE_PROG_STANDARD) },
105 { ScResId( STR_STYLENAME_NOTE ), "Note" },
106 // last entry remains empty
107 { OUString(), OUString() },
108 };
109 return aGraphicMap;
110 }
111 OSL_FAIL("invalid family");
112 return nullptr;
113}
114
115// programmatic name suffix for display names that match other programmatic names
116// is " (user)" including a space
117
118constexpr OUStringLiteral SC_SUFFIX_USER = u" (user)";
119
120static bool lcl_EndsWithUser( std::u16string_view rString )
121{
122 return o3tl::ends_with(rString, SC_SUFFIX_USER);
123}
124
126{
127 bool bDisplayIsProgrammatic = false;
128
129 const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
130 if (pNames)
131 {
132 do
133 {
134 if (pNames->aDispName == rDispName)
135 return pNames->aProgName;
136 else if (pNames->aProgName == rDispName)
137 bDisplayIsProgrammatic = true; // display name matches any programmatic name
138 }
139 while( !(++pNames)->aDispName.isEmpty() );
140 }
141
142 if ( bDisplayIsProgrammatic || lcl_EndsWithUser( rDispName ) )
143 {
144 // add the (user) suffix if the display name matches any style's programmatic name
145 // or if it already contains the suffix
146 return rDispName + SC_SUFFIX_USER;
147 }
148
149 return rDispName;
150}
151
153{
154 if ( lcl_EndsWithUser( rProgName ) )
155 {
156 // remove the (user) suffix, don't compare to map entries
157 return rProgName.copy( 0, rProgName.getLength() - SC_SUFFIX_USER.getLength() );
158 }
159
160 const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
161 if (pNames)
162 {
163 do
164 {
165 if (pNames->aProgName == rProgName)
166 return pNames->aDispName;
167 }
168 while( !(++pNames)->aDispName.isEmpty() );
169 }
170 return rProgName;
171}
172
173/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SC_DLLPUBLIC OUString ProgrammaticToDisplayName(const OUString &rProgName, SfxStyleFamily nType)
static OUString DisplayToProgrammaticName(const OUString &rDispName, SfxStyleFamily nType)
float u
constexpr bool ends_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
SfxStyleFamily
static const ScDisplayNameMap * lcl_GetStyleNameMap(SfxStyleFamily nType)
Definition: stylehelper.cxx:42
static bool lcl_EndsWithUser(std::u16string_view rString)
constexpr OUStringLiteral SC_SUFFIX_USER
constexpr OUStringLiteral SC_STYLE_PROG_HEADING1
Definition: stylehelper.hxx:34
constexpr OUStringLiteral SC_STYLE_PROG_RESULT1
Definition: stylehelper.hxx:32
constexpr OUStringLiteral SC_STYLE_PROG_STANDARD
Definition: stylehelper.hxx:30
constexpr OUStringLiteral SC_STYLE_PROG_HEADING
Definition: stylehelper.hxx:33
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TOP
Definition: stylehelper.hxx:42
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_INNER
Definition: stylehelper.hxx:37
constexpr OUStringLiteral SC_STYLE_PROG_REPORT
Definition: stylehelper.hxx:35
constexpr OUStringLiteral SC_STYLE_PROG_RESULT
Definition: stylehelper.hxx:31
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_CATEGORY
Definition: stylehelper.hxx:39
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_FIELDNAME
Definition: stylehelper.hxx:41
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TITLE
Definition: stylehelper.hxx:40
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_RESULT
Definition: stylehelper.hxx:38