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 <global.hxx>
25 #include <globstr.hrc>
26 #include <scresid.hxx>
27 
28 // conversion programmatic <-> display (visible) name
29 // currently, the core always has the visible names
30 // the api is required to use programmatic names for default styles
31 // these programmatic names must never change!
32 
33 #define SC_STYLE_PROG_STANDARD "Default"
34 #define SC_STYLE_PROG_RESULT "Result"
35 #define SC_STYLE_PROG_RESULT1 "Result2"
36 #define SC_STYLE_PROG_HEADLINE "Heading"
37 #define SC_STYLE_PROG_HEADLINE1 "Heading1"
38 #define SC_STYLE_PROG_REPORT "Report"
39 
40 #define SC_PIVOT_STYLE_PROG_INNER "Pivot Table Value"
41 #define SC_PIVOT_STYLE_PROG_RESULT "Pivot Table Result"
42 #define SC_PIVOT_STYLE_PROG_CATEGORY "Pivot Table Category"
43 #define SC_PIVOT_STYLE_PROG_TITLE "Pivot Table Title"
44 #define SC_PIVOT_STYLE_PROG_FIELDNAME "Pivot Table Field"
45 #define SC_PIVOT_STYLE_PROG_TOP "Pivot Table Corner"
46 
47 namespace {
48 
49 struct ScDisplayNameMap
50 {
51  OUString aDispName;
52  OUString aProgName;
53 };
54 
55 }
56 
57 static const ScDisplayNameMap* lcl_GetStyleNameMap( SfxStyleFamily nType )
58 {
59  if ( nType == SfxStyleFamily::Para )
60  {
61  static ScDisplayNameMap const aCellMap[]
62  {
63  { ScResId( STR_STYLENAME_STANDARD_CELL ), OUString(SC_STYLE_PROG_STANDARD) },
64  { ScResId( STR_STYLENAME_RESULT ), OUString(SC_STYLE_PROG_RESULT) },
65  { ScResId( STR_STYLENAME_RESULT1 ), OUString(SC_STYLE_PROG_RESULT1) },
66  { ScResId( STR_STYLENAME_HEADLINE ), OUString(SC_STYLE_PROG_HEADLINE) },
67  { ScResId( STR_STYLENAME_HEADLINE1 ), OUString(SC_STYLE_PROG_HEADLINE1) },
68  { ScResId( STR_PIVOT_STYLENAME_INNER ), OUString(SC_PIVOT_STYLE_PROG_INNER) },
69  { ScResId( STR_PIVOT_STYLENAME_RESULT ), OUString(SC_PIVOT_STYLE_PROG_RESULT) },
70  { ScResId( STR_PIVOT_STYLENAME_CATEGORY ), OUString(SC_PIVOT_STYLE_PROG_CATEGORY) },
71  { ScResId( STR_PIVOT_STYLENAME_TITLE ), OUString(SC_PIVOT_STYLE_PROG_TITLE) },
72  { ScResId( STR_PIVOT_STYLENAME_FIELDNAME ), OUString(SC_PIVOT_STYLE_PROG_FIELDNAME) },
73  { ScResId( STR_PIVOT_STYLENAME_TOP ), OUString(SC_PIVOT_STYLE_PROG_TOP) },
74  // last entry remains empty
75  { OUString(), OUString() },
76  };
77  return aCellMap;
78  }
79  else if ( nType == SfxStyleFamily::Page )
80  {
81  static ScDisplayNameMap const aPageMap[]
82  {
83  { ScResId( STR_STYLENAME_STANDARD_PAGE ), OUString(SC_STYLE_PROG_STANDARD) },
84  { ScResId( STR_STYLENAME_REPORT ), OUString(SC_STYLE_PROG_REPORT) },
85  // last entry remains empty
86  { OUString(), OUString() },
87  };
88  return aPageMap;
89  }
90  OSL_FAIL("invalid family");
91  return nullptr;
92 }
93 
94 // programmatic name suffix for display names that match other programmatic names
95 // is " (user)" including a space
96 
97 #define SC_SUFFIX_USER " (user)"
98 #define SC_SUFFIX_USER_LEN 7
99 
100 static bool lcl_EndsWithUser( const OUString& rString )
101 {
102  return rString.endsWith(SC_SUFFIX_USER);
103 }
104 
106 {
107  bool bDisplayIsProgrammatic = false;
108 
109  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
110  if (pNames)
111  {
112  do
113  {
114  if (pNames->aDispName == rDispName)
115  return pNames->aProgName;
116  else if (pNames->aProgName == rDispName)
117  bDisplayIsProgrammatic = true; // display name matches any programmatic name
118  }
119  while( !(++pNames)->aDispName.isEmpty() );
120  }
121 
122  if ( bDisplayIsProgrammatic || lcl_EndsWithUser( rDispName ) )
123  {
124  // add the (user) suffix if the display name matches any style's programmatic name
125  // or if it already contains the suffix
126  return rDispName + SC_SUFFIX_USER;
127  }
128 
129  return rDispName;
130 }
131 
133 {
134  if ( lcl_EndsWithUser( rProgName ) )
135  {
136  // remove the (user) suffix, don't compare to map entries
137  return rProgName.copy( 0, rProgName.getLength() - SC_SUFFIX_USER_LEN );
138  }
139 
140  const ScDisplayNameMap* pNames = lcl_GetStyleNameMap( nType );
141  if (pNames)
142  {
143  do
144  {
145  if (pNames->aProgName == rProgName)
146  return pNames->aDispName;
147  }
148  while( !(++pNames)->aDispName.isEmpty() );
149  }
150  return rProgName;
151 }
152 
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString DisplayToProgrammaticName(const OUString &rDispName, SfxStyleFamily nType)
#define SC_PIVOT_STYLE_PROG_FIELDNAME
Definition: stylehelper.cxx:44
#define SC_SUFFIX_USER
Definition: stylehelper.cxx:97
#define SC_SUFFIX_USER_LEN
Definition: stylehelper.cxx:98
static OUString ProgrammaticToDisplayName(const OUString &rProgName, SfxStyleFamily nType)
#define SC_PIVOT_STYLE_PROG_TOP
Definition: stylehelper.cxx:45
#define SC_STYLE_PROG_RESULT1
Definition: stylehelper.cxx:35
static const ScDisplayNameMap * lcl_GetStyleNameMap(SfxStyleFamily nType)
Definition: stylehelper.cxx:57
#define SC_PIVOT_STYLE_PROG_RESULT
Definition: stylehelper.cxx:41
static bool lcl_EndsWithUser(const OUString &rString)
#define SC_STYLE_PROG_HEADLINE1
Definition: stylehelper.cxx:37
SfxStyleFamily
#define SC_STYLE_PROG_HEADLINE
Definition: stylehelper.cxx:36
#define SC_PIVOT_STYLE_PROG_INNER
Definition: stylehelper.cxx:40
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
#define SC_PIVOT_STYLE_PROG_TITLE
Definition: stylehelper.cxx:43
#define SC_STYLE_PROG_REPORT
Definition: stylehelper.cxx:38
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:401
#define SC_STYLE_PROG_STANDARD
Definition: stylehelper.cxx:33
#define SC_PIVOT_STYLE_PROG_CATEGORY
Definition: stylehelper.cxx:42
#define SC_STYLE_PROG_RESULT
Definition: stylehelper.cxx:34