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 
32 namespace {
33 
34 struct ScDisplayNameMap
35 {
36  OUString aDispName;
37  OUString aProgName;
38 };
39 
40 }
41 
42 static 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  OSL_FAIL("invalid family");
101  return nullptr;
102 }
103 
104 // programmatic name suffix for display names that match other programmatic names
105 // is " (user)" including a space
106 
107 constexpr OUStringLiteral SC_SUFFIX_USER = u" (user)";
108 
109 static bool lcl_EndsWithUser( std::u16string_view rString )
110 {
111  return o3tl::ends_with(rString, SC_SUFFIX_USER);
112 }
113 
115 {
116  bool bDisplayIsProgrammatic = false;
117 
118  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
119  if (pNames)
120  {
121  do
122  {
123  if (pNames->aDispName == rDispName)
124  return pNames->aProgName;
125  else if (pNames->aProgName == rDispName)
126  bDisplayIsProgrammatic = true; // display name matches any programmatic name
127  }
128  while( !(++pNames)->aDispName.isEmpty() );
129  }
130 
131  if ( bDisplayIsProgrammatic || lcl_EndsWithUser( rDispName ) )
132  {
133  // add the (user) suffix if the display name matches any style's programmatic name
134  // or if it already contains the suffix
135  return rDispName + SC_SUFFIX_USER;
136  }
137 
138  return rDispName;
139 }
140 
142 {
143  if ( lcl_EndsWithUser( rProgName ) )
144  {
145  // remove the (user) suffix, don't compare to map entries
146  return rProgName.copy( 0, rProgName.getLength() - SC_SUFFIX_USER.getLength() );
147  }
148 
149  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
150  if (pNames)
151  {
152  do
153  {
154  if (pNames->aProgName == rProgName)
155  return pNames->aDispName;
156  }
157  while( !(++pNames)->aDispName.isEmpty() );
158  }
159  return rProgName;
160 }
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral SC_STYLE_PROG_HEADING
Definition: stylehelper.hxx:33
constexpr OUStringLiteral SC_STYLE_PROG_REPORT
Definition: stylehelper.hxx:35
static OUString DisplayToProgrammaticName(const OUString &rDispName, SfxStyleFamily nType)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
constexpr OUStringLiteral SC_STYLE_PROG_RESULT
Definition: stylehelper.hxx:31
static SC_DLLPUBLIC OUString ProgrammaticToDisplayName(const OUString &rProgName, SfxStyleFamily nType)
constexpr OUStringLiteral SC_STYLE_PROG_STANDARD
Definition: stylehelper.hxx:30
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_FIELDNAME
Definition: stylehelper.hxx:41
constexpr OUStringLiteral SC_SUFFIX_USER
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TOP
Definition: stylehelper.hxx:42
static const ScDisplayNameMap * lcl_GetStyleNameMap(SfxStyleFamily nType)
Definition: stylehelper.cxx:42
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TITLE
Definition: stylehelper.hxx:40
SfxStyleFamily
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_RESULT
Definition: stylehelper.hxx:38
float u
constexpr OUStringLiteral SC_STYLE_PROG_RESULT1
Definition: stylehelper.hxx:32
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
constexpr bool ends_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_INNER
Definition: stylehelper.hxx:37
static bool lcl_EndsWithUser(std::u16string_view rString)
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_CATEGORY
Definition: stylehelper.hxx:39
constexpr OUStringLiteral SC_STYLE_PROG_HEADING1
Definition: stylehelper.hxx:34