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 #define SC_STYLE_PROG_STANDARD "Default"
33 #define SC_STYLE_PROG_RESULT "Result"
34 #define SC_STYLE_PROG_RESULT1 "Result2"
35 #define SC_STYLE_PROG_HEADING "Heading"
36 #define SC_STYLE_PROG_HEADING1 "Heading1"
37 #define SC_STYLE_PROG_REPORT "Report"
38 
39 #define SC_PIVOT_STYLE_PROG_INNER "Pivot Table Value"
40 #define SC_PIVOT_STYLE_PROG_RESULT "Pivot Table Result"
41 #define SC_PIVOT_STYLE_PROG_CATEGORY "Pivot Table Category"
42 #define SC_PIVOT_STYLE_PROG_TITLE "Pivot Table Title"
43 #define SC_PIVOT_STYLE_PROG_FIELDNAME "Pivot Table Field"
44 #define SC_PIVOT_STYLE_PROG_TOP "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 #define SC_SUFFIX_USER " (user)"
122 #define SC_SUFFIX_USER_LEN 7
123 
124 static bool lcl_EndsWithUser( const OUString& rString )
125 {
126  return rString.endsWith(SC_SUFFIX_USER);
127 }
128 
130 {
131  bool bDisplayIsProgrammatic = false;
132 
133  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
134  if (pNames)
135  {
136  do
137  {
138  if (pNames->aDispName == rDispName)
139  return pNames->aProgName;
140  else if (pNames->aProgName == rDispName)
141  bDisplayIsProgrammatic = true; // display name matches any programmatic name
142  }
143  while( !(++pNames)->aDispName.isEmpty() );
144  }
145 
146  if ( bDisplayIsProgrammatic || lcl_EndsWithUser( rDispName ) )
147  {
148  // add the (user) suffix if the display name matches any style's programmatic name
149  // or if it already contains the suffix
150  return rDispName + SC_SUFFIX_USER;
151  }
152 
153  return rDispName;
154 }
155 
157 {
158  if ( lcl_EndsWithUser( rProgName ) )
159  {
160  // remove the (user) suffix, don't compare to map entries
161  return rProgName.copy( 0, rProgName.getLength() - SC_SUFFIX_USER_LEN );
162  }
163 
164  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
165  if (pNames)
166  {
167  do
168  {
169  if (pNames->aProgName == rProgName)
170  return pNames->aDispName;
171  }
172  while( !(++pNames)->aDispName.isEmpty() );
173  }
174  return rProgName;
175 }
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SC_STYLE_PROG_HEADING
Definition: stylehelper.cxx:35
static OUString DisplayToProgrammaticName(const OUString &rDispName, SfxStyleFamily nType)
#define SC_PIVOT_STYLE_PROG_FIELDNAME
Definition: stylehelper.cxx:43
#define SC_SUFFIX_USER
#define SC_SUFFIX_USER_LEN
static OUString ProgrammaticToDisplayName(const OUString &rProgName, SfxStyleFamily nType)
#define SC_PIVOT_STYLE_PROG_TOP
Definition: stylehelper.cxx:44
#define SC_STYLE_PROG_RESULT1
Definition: stylehelper.cxx:34
static const ScDisplayNameMap * lcl_GetStyleNameMap(SfxStyleFamily nType)
Definition: stylehelper.cxx:56
#define SC_PIVOT_STYLE_PROG_RESULT
Definition: stylehelper.cxx:40
static bool lcl_EndsWithUser(const OUString &rString)
SfxStyleFamily
#define SC_PIVOT_STYLE_PROG_INNER
Definition: stylehelper.cxx:39
OUString ScResId(const char *pId)
Definition: scdll.cxx:89
#define SC_STYLE_PROG_HEADING1
Definition: stylehelper.cxx:36
#define SC_PIVOT_STYLE_PROG_TITLE
Definition: stylehelper.cxx:42
#define SC_STYLE_PROG_REPORT
Definition: stylehelper.cxx:37
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:400
#define SC_STYLE_PROG_STANDARD
Definition: stylehelper.cxx:32
#define SC_PIVOT_STYLE_PROG_CATEGORY
Definition: stylehelper.cxx:41
#define SC_STYLE_PROG_RESULT
Definition: stylehelper.cxx:33