1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <string_view>
24 #include <svl/style.hxx>
25 #include <o3tl/string_view.hxx>
26 #include <osl/diagnose.h>
28 #include <stylehelper.hxx>
29 #include <globstr.hrc>
30 #include <scresid.hxx>
34 struct ScDisplayNameMap
42 static const ScDisplayNameMap
* lcl_GetStyleNameMap( SfxStyleFamily nType
)
44 if ( nType
== SfxStyleFamily::Para
)
46 static ScDisplayNameMap
const aCellMap
[]
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
), u
"Heading"_ustr
},
54 { ScResId( STR_STYLENAME_HEADING_1
), u
"Heading 1"_ustr
},
55 { ScResId( STR_STYLENAME_HEADING_2
), u
"Heading 2"_ustr
},
56 { ScResId( STR_STYLENAME_TEXT
), u
"Text"_ustr
},
57 { ScResId( STR_STYLENAME_NOTE
), u
"Note"_ustr
},
58 { ScResId( STR_STYLENAME_FOOTNOTE
), u
"Footnote"_ustr
},
59 { ScResId( STR_STYLENAME_HYPERLINK
), u
"Hyperlink"_ustr
},
60 { ScResId( STR_STYLENAME_STATUS
), u
"Status"_ustr
},
61 { ScResId( STR_STYLENAME_GOOD
), u
"Good"_ustr
},
62 { ScResId( STR_STYLENAME_NEUTRAL
), u
"Neutral"_ustr
},
63 { ScResId( STR_STYLENAME_BAD
), u
"Bad"_ustr
},
64 { ScResId( STR_STYLENAME_WARNING
), u
"Warning"_ustr
},
65 { ScResId( STR_STYLENAME_ERROR
), u
"Error"_ustr
},
66 { ScResId( STR_STYLENAME_ACCENT
), u
"Accent"_ustr
},
67 { ScResId( STR_STYLENAME_ACCENT_1
), u
"Accent 1"_ustr
},
68 { ScResId( STR_STYLENAME_ACCENT_2
), u
"Accent 2"_ustr
},
69 { ScResId( STR_STYLENAME_ACCENT_3
), u
"Accent 3"_ustr
},
70 { ScResId( STR_STYLENAME_RESULT
), u
"Result"_ustr
},
71 // API compatibility programmatic names after.
72 { ScResId( STR_STYLENAME_STANDARD
), SC_STYLE_PROG_STANDARD
},
73 { ScResId( STR_STYLENAME_RESULT
), SC_STYLE_PROG_RESULT
},
74 { ScResId( STR_STYLENAME_RESULT1
), SC_STYLE_PROG_RESULT1
},
75 { ScResId( STR_STYLENAME_HEADING
), SC_STYLE_PROG_HEADING
},
76 { ScResId( STR_STYLENAME_HEADING_1
), SC_STYLE_PROG_HEADING1
},
77 // Pivot table styles.
78 { ScResId( STR_PIVOT_STYLENAME_INNER
), SC_PIVOT_STYLE_PROG_INNER
},
79 { ScResId( STR_PIVOT_STYLENAME_RESULT
), SC_PIVOT_STYLE_PROG_RESULT
},
80 { ScResId( STR_PIVOT_STYLENAME_CATEGORY
), SC_PIVOT_STYLE_PROG_CATEGORY
},
81 { ScResId( STR_PIVOT_STYLENAME_TITLE
), SC_PIVOT_STYLE_PROG_TITLE
},
82 { ScResId( STR_PIVOT_STYLENAME_FIELDNAME
), SC_PIVOT_STYLE_PROG_FIELDNAME
},
83 { ScResId( STR_PIVOT_STYLENAME_TOP
), SC_PIVOT_STYLE_PROG_TOP
},
84 // last entry remains empty
85 { OUString(), OUString() },
89 else if ( nType
== SfxStyleFamily::Page
)
91 static ScDisplayNameMap
const aPageMap
[]
93 { ScResId( STR_STYLENAME_STANDARD
), SC_STYLE_PROG_STANDARD
},
94 { ScResId( STR_STYLENAME_REPORT
), SC_STYLE_PROG_REPORT
},
95 // last entry remains empty
96 { OUString(), OUString() },
100 else if ( nType
== SfxStyleFamily::Frame
)
102 static ScDisplayNameMap
const aGraphicMap
[]
104 { ScResId( STR_STYLENAME_STANDARD
), SC_STYLE_PROG_STANDARD
},
105 { ScResId( STR_STYLENAME_NOTE
), u
"Note"_ustr
},
106 // last entry remains empty
107 { OUString(), OUString() },
111 OSL_FAIL("invalid family");
115 // programmatic name suffix for display names that match other programmatic names
116 // is " (user)" including a space
118 constexpr OUString SC_SUFFIX_USER
= u
" (user)"_ustr
;
120 static bool lcl_EndsWithUser( std::u16string_view rString
)
122 return o3tl::ends_with(rString
, SC_SUFFIX_USER
);
125 OUString
ScStyleNameConversion::DisplayToProgrammaticName( const OUString
& rDispName
, SfxStyleFamily nType
)
127 bool bDisplayIsProgrammatic
= false;
129 const ScDisplayNameMap
* pNames
= lcl_GetStyleNameMap( nType
);
134 if (pNames
->aDispName
== rDispName
)
135 return pNames
->aProgName
;
136 else if (pNames
->aProgName
== rDispName
)
137 bDisplayIsProgrammatic
= true; // display name matches any programmatic name
139 while( !(++pNames
)->aDispName
.isEmpty() );
142 if ( bDisplayIsProgrammatic
|| lcl_EndsWithUser( rDispName
) )
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
;
152 OUString
ScStyleNameConversion::ProgrammaticToDisplayName( const OUString
& rProgName
, SfxStyleFamily nType
)
154 if ( lcl_EndsWithUser( rProgName
) )
156 // remove the (user) suffix, don't compare to map entries
157 return rProgName
.copy( 0, rProgName
.getLength() - SC_SUFFIX_USER
.getLength() );
160 const ScDisplayNameMap
* pNames
= lcl_GetStyleNameMap( nType
);
165 if (pNames
->aProgName
== rProgName
)
166 return pNames
->aDispName
;
168 while( !(++pNames
)->aDispName
.isEmpty() );
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */