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 <svl/style.hxx>
21 #include <osl/diagnose.h>
22 
23 #include <stylehelper.hxx>
24 #include <globstr.hrc>
25 #include <scresid.hxx>
26 
27 // conversion programmatic <-> display (visible) name
28 // currently, the core always has the visible names
29 // the api is required to use programmatic names for default styles
30 // these programmatic names must never change!
31 
32 constexpr OUStringLiteral SC_STYLE_PROG_STANDARD = u"Default";
33 constexpr OUStringLiteral SC_STYLE_PROG_RESULT = u"Result";
34 constexpr OUStringLiteral SC_STYLE_PROG_RESULT1 = u"Result2";
35 constexpr OUStringLiteral SC_STYLE_PROG_HEADING = u"Heading";
36 constexpr OUStringLiteral SC_STYLE_PROG_HEADING1 = u"Heading1";
37 constexpr OUStringLiteral SC_STYLE_PROG_REPORT = u"Report";
38 
39 constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_INNER = u"Pivot Table Value";
40 constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_RESULT = u"Pivot Table Result";
41 constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_CATEGORY = u"Pivot Table Category";
42 constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TITLE = u"Pivot Table Title";
43 constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_FIELDNAME = u"Pivot Table Field";
44 constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TOP = u"Pivot Table Corner";
45 
46 namespace {
47 
48 struct ScDisplayNameMap
49 {
50  OUString aDispName;
51  OUString aProgName;
52 };
53 
54 }
55 
56 static const ScDisplayNameMap* lcl_GetStyleNameMap( SfxStyleFamily nType )
57 {
58  if ( nType == SfxStyleFamily::Para )
59  {
60  static ScDisplayNameMap const aCellMap[]
61  {
62  // Standard builtin styles from configuration.
63  // Defined in sc/res/xml/styles.xml
64  // Installed to "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/calc/styles.xml"
65  // e.g. /usr/lib64/libreoffice/share/calc/styles.xml
66  // or instdir/share/calc/styles.xml
67  { ScResId( STR_STYLENAME_HEADING ), "Heading" },
68  { ScResId( STR_STYLENAME_HEADING_1 ), "Heading 1" },
69  { ScResId( STR_STYLENAME_HEADING_2 ), "Heading 2" },
70  { ScResId( STR_STYLENAME_TEXT ), "Text" },
71  { ScResId( STR_STYLENAME_NOTE ), "Note" },
72  { ScResId( STR_STYLENAME_FOOTNOTE ), "Footnote" },
73  { ScResId( STR_STYLENAME_HYPERLINK ), "Hyperlink" },
74  { ScResId( STR_STYLENAME_STATUS ), "Status" },
75  { ScResId( STR_STYLENAME_GOOD ), "Good" },
76  { ScResId( STR_STYLENAME_NEUTRAL ), "Neutral" },
77  { ScResId( STR_STYLENAME_BAD ), "Bad" },
78  { ScResId( STR_STYLENAME_WARNING ), "Warning" },
79  { ScResId( STR_STYLENAME_ERROR ), "Error" },
80  { ScResId( STR_STYLENAME_ACCENT ), "Accent" },
81  { ScResId( STR_STYLENAME_ACCENT_1 ), "Accent 1" },
82  { ScResId( STR_STYLENAME_ACCENT_2 ), "Accent 2" },
83  { ScResId( STR_STYLENAME_ACCENT_3 ), "Accent 3" },
84  { ScResId( STR_STYLENAME_RESULT ), "Result" },
85  // API compatibility programmatic names after.
86  { ScResId( STR_STYLENAME_STANDARD ), OUString(SC_STYLE_PROG_STANDARD) },
87  { ScResId( STR_STYLENAME_RESULT ), OUString(SC_STYLE_PROG_RESULT) },
88  { ScResId( STR_STYLENAME_RESULT1 ), OUString(SC_STYLE_PROG_RESULT1) },
89  { ScResId( STR_STYLENAME_HEADING ), OUString(SC_STYLE_PROG_HEADING) },
90  { ScResId( STR_STYLENAME_HEADING_1 ), OUString(SC_STYLE_PROG_HEADING1) },
91  // Pivot table styles.
92  { ScResId( STR_PIVOT_STYLENAME_INNER ), OUString(SC_PIVOT_STYLE_PROG_INNER) },
93  { ScResId( STR_PIVOT_STYLENAME_RESULT ), OUString(SC_PIVOT_STYLE_PROG_RESULT) },
94  { ScResId( STR_PIVOT_STYLENAME_CATEGORY ), OUString(SC_PIVOT_STYLE_PROG_CATEGORY) },
95  { ScResId( STR_PIVOT_STYLENAME_TITLE ), OUString(SC_PIVOT_STYLE_PROG_TITLE) },
96  { ScResId( STR_PIVOT_STYLENAME_FIELDNAME ), OUString(SC_PIVOT_STYLE_PROG_FIELDNAME) },
97  { ScResId( STR_PIVOT_STYLENAME_TOP ), OUString(SC_PIVOT_STYLE_PROG_TOP) },
98  // last entry remains empty
99  { OUString(), OUString() },
100  };
101  return aCellMap;
102  }
103  else if ( nType == SfxStyleFamily::Page )
104  {
105  static ScDisplayNameMap const aPageMap[]
106  {
107  { ScResId( STR_STYLENAME_STANDARD ), OUString(SC_STYLE_PROG_STANDARD) },
108  { ScResId( STR_STYLENAME_REPORT ), OUString(SC_STYLE_PROG_REPORT) },
109  // last entry remains empty
110  { OUString(), OUString() },
111  };
112  return aPageMap;
113  }
114  OSL_FAIL("invalid family");
115  return nullptr;
116 }
117 
118 // programmatic name suffix for display names that match other programmatic names
119 // is " (user)" including a space
120 
121 constexpr OUStringLiteral SC_SUFFIX_USER = u" (user)";
122 
123 static bool lcl_EndsWithUser( const OUString& rString )
124 {
125  return rString.endsWith(SC_SUFFIX_USER);
126 }
127 
129 {
130  bool bDisplayIsProgrammatic = false;
131 
132  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
133  if (pNames)
134  {
135  do
136  {
137  if (pNames->aDispName == rDispName)
138  return pNames->aProgName;
139  else if (pNames->aProgName == rDispName)
140  bDisplayIsProgrammatic = true; // display name matches any programmatic name
141  }
142  while( !(++pNames)->aDispName.isEmpty() );
143  }
144 
145  if ( bDisplayIsProgrammatic || lcl_EndsWithUser( rDispName ) )
146  {
147  // add the (user) suffix if the display name matches any style's programmatic name
148  // or if it already contains the suffix
149  return rDispName + SC_SUFFIX_USER;
150  }
151 
152  return rDispName;
153 }
154 
156 {
157  if ( lcl_EndsWithUser( rProgName ) )
158  {
159  // remove the (user) suffix, don't compare to map entries
160  return rProgName.copy( 0, rProgName.getLength() - SC_SUFFIX_USER.getLength() );
161  }
162 
163  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
164  if (pNames)
165  {
166  do
167  {
168  if (pNames->aProgName == rProgName)
169  return pNames->aDispName;
170  }
171  while( !(++pNames)->aDispName.isEmpty() );
172  }
173  return rProgName;
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString DisplayToProgrammaticName(const OUString &rDispName, SfxStyleFamily nType)
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_RESULT
Definition: stylehelper.cxx:40
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:89
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_CATEGORY
Definition: stylehelper.cxx:41
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TITLE
Definition: stylehelper.cxx:42
constexpr OUStringLiteral SC_STYLE_PROG_HEADING1
Definition: stylehelper.cxx:36
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_INNER
Definition: stylehelper.cxx:39
static OUString ProgrammaticToDisplayName(const OUString &rProgName, SfxStyleFamily nType)
constexpr OUStringLiteral SC_SUFFIX_USER
static const ScDisplayNameMap * lcl_GetStyleNameMap(SfxStyleFamily nType)
Definition: stylehelper.cxx:56
static bool lcl_EndsWithUser(const OUString &rString)
SfxStyleFamily
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_FIELDNAME
Definition: stylehelper.cxx:43
constexpr OUStringLiteral SC_PIVOT_STYLE_PROG_TOP
Definition: stylehelper.cxx:44
float u
constexpr OUStringLiteral SC_STYLE_PROG_HEADING
Definition: stylehelper.cxx:35
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
constexpr OUStringLiteral SC_STYLE_PROG_REPORT
Definition: stylehelper.cxx:37
constexpr OUStringLiteral SC_STYLE_PROG_STANDARD
Definition: stylehelper.cxx:32
constexpr OUStringLiteral SC_STYLE_PROG_RESULT
Definition: stylehelper.cxx:33
constexpr OUStringLiteral SC_STYLE_PROG_RESULT1
Definition: stylehelper.cxx:34