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 #ifndef INCLUDED_SVL_STYLE_HXX 21 #define INCLUDED_SVL_STYLE_HXX 22 23 #include <config_options.h> 24 #include <com/sun/star/style/XStyle.hpp> 25 #include <com/sun/star/lang/XUnoTunnel.hpp> 26 27 #include <rtl/ref.hxx> 28 #include <comphelper/weak.hxx> 29 #include <cppuhelper/implbase.hxx> 30 #include <svl/svldllapi.h> 31 #include <svl/hint.hxx> 32 #include <svl/lstner.hxx> 33 #include <svl/SfxBroadcaster.hxx> 34 #include <svl/stylesheetuser.hxx> 35 #include <o3tl/typed_flags_set.hxx> 36 #include <tools/mapunit.hxx> 37 #include <tools/solar.h> 38 39 #include <memory> 40 41 // This is used as a flags enum in sw/, but only there, 42 // so I don't pull in o3tl::typed_flags here 43 enum class SfxStyleFamily { 44 None = 0x00, 45 Char = 0x01, 46 Para = 0x02, 47 Frame = 0x04, 48 Page = 0x08, 49 Pseudo = 0x10, 50 Table = 0x20, 51 Cell = 0x40, 52 All = 0x7fff 53 }; 54 55 enum class SfxStyleSearchBits { 56 // sc/calc styles 57 ScStandard = 0x0001, 58 59 // sw/writer styles 60 SwText = 0x0001, 61 SwChapter = 0x0002, 62 SwList = 0x0004, 63 SwIndex = 0x0008, 64 SwExtra = 0x0010, 65 SwHtml = 0x0020, 66 SwCondColl = 0x0040, 67 68 Auto = 0x0000, ///< automatic: flags from application 69 Hidden = 0x0200, ///< hidden styles (search mask) 70 ReadOnly = 0x2000, ///< readonly styles (search mask) 71 Used = 0x4000, ///< used styles (search mask) 72 UserDefined = 0x8000, ///< user defined styles (search mask) 73 AllVisible = 0xe07f, ///< all visible styles 74 All = 0xe27f, ///< all styles 75 }; 76 namespace o3tl { 77 template<> struct typed_flags<SfxStyleSearchBits> : is_typed_flags<SfxStyleSearchBits, 0xe27f> {}; 78 } 79 80 81 class SfxItemSet; 82 class SfxItemPool; 83 class SfxStyleSheetBasePool; 84 class SvStream; 85 86 namespace svl { class IndexedStyleSheets; } 87 /* 88 Everyone changing instances of SfxStyleSheetBasePool or SfxStyleSheetBase 89 must broadcast this using <SfxStyleSheetBasePool::GetBroadcaster()> broadcasts. 90 The class <SfxStyleSheetHint> is used for this, it contains an Action-Id and a 91 pointer to the <SfxStyleSheetBase>. The actions are: 92 93 #define SfxHintId::StyleSheetCreated // style is created 94 #define SfxHintId::StyleSheetModified // style is modified 95 #define SfxHintId::StyleSheetChanged // style is replaced 96 #define SfxHintId::StyleSheetErased // style is deleted 97 98 The following methods already broadcast themself 99 100 SfxSimpleHint(SfxHintId::Dying) from: 101 SfxStyleSheetBasePool::~SfxStyleSheetBasePool() 102 103 SfxStyleSheetHint( SfxHintId::StyleSheetCreated, *p ) from: 104 SfxStyleSheetBasePool::Make( const String& rName, 105 SfxStyleFamily eFam, sal_uInt16 mask) 106 107 SfxStyleSheetHint( SfxHintId::StyleSheetChanged, *pNew ) from: 108 SfxStyleSheetBasePool::Add( SfxStyleSheetBase& rSheet ) 109 110 SfxStyleSheetHint( SfxHintId::StyleSheetErased, *p ) from: 111 SfxStyleSheetBasePool::Erase( SfxStyleSheetBase* p ) 112 SfxStyleSheetBasePool::Clear() 113 */ 114 115 class SVL_DLLPUBLIC SfxStyleSheetBase : public comphelper::OWeakTypeObject 116 { 117 private: 118 friend class SfxStyleSheetBasePool; 119 120 protected: 121 SfxStyleSheetBasePool* m_pPool; // related pool 122 SfxStyleFamily nFamily; 123 124 OUString aName, aParent, aFollow; 125 OUString aHelpFile; // name of the help file 126 SfxItemSet* pSet; // ItemSet 127 SfxStyleSearchBits nMask; // Flags 128 129 sal_uLong nHelpId; // help ID 130 131 bool bMySet; // sal_True: delete Set in dtor 132 bool bHidden; 133 134 SfxStyleSheetBase( const OUString&, SfxStyleSheetBasePool*, SfxStyleFamily eFam, SfxStyleSearchBits mask ); 135 SfxStyleSheetBase( const SfxStyleSheetBase& ); 136 virtual ~SfxStyleSheetBase() override; 137 138 public: 139 140 // returns the internal name of this style 141 const OUString& GetName() const; 142 143 // sets the internal name of this style. 144 // 145 // If the name of a style is changed, then the styles container needs to be 146 // reindexed (see IndexedStyleSheets). If you set bReindexNow to false to 147 // defer that indexing, then you must call the Reindex manually on the 148 // SfxStyleSheetBasePool parent. 149 virtual bool SetName(const OUString& rNewName, bool bReindexNow = true); 150 151 virtual const OUString& GetParent() const; 152 virtual bool SetParent( const OUString& ); 153 virtual const OUString& GetFollow() const; 154 virtual bool SetFollow( const OUString& ); 155 virtual bool HasFollowSupport() const; // Default true 156 virtual bool HasParentSupport() const; // Default true 157 virtual bool HasClearParentSupport() const; // Default false 158 virtual bool IsUsed() const; // Default true 159 virtual OUString GetDescription( MapUnit eMetric ); 160 GetUsedBy()161 virtual OUString GetUsedBy() { return OUString(); } 162 GetPool()163 SfxStyleSheetBasePool* GetPool() { return m_pPool; } GetFamily() const164 SfxStyleFamily GetFamily() const { return nFamily; } GetMask() const165 SfxStyleSearchBits GetMask() const { return nMask; } SetMask(SfxStyleSearchBits mask)166 void SetMask( SfxStyleSearchBits mask) { nMask = mask; } IsUserDefined() const167 bool IsUserDefined() const 168 { return bool( nMask & SfxStyleSearchBits::UserDefined); } 169 IsHidden() const170 virtual bool IsHidden() const { return bHidden; } 171 virtual void SetHidden( bool bValue ); 172 173 virtual sal_uLong GetHelpId( OUString& rFile ); 174 virtual void SetHelpId( const OUString& r, sal_uLong nId ); 175 176 virtual SfxItemSet& GetItemSet(); 177 /// Due to writer's usual lack of sanity this is a separate function for 178 /// preview only; it shall not create the style in case it does not exist. 179 /// If the style has parents, it is _not_ required that the returned item 180 /// set has parents (i.e. use it for display purposes only). 181 virtual std::unique_ptr<SfxItemSet> GetItemSetForPreview(); 182 183 /// Fix for expensive dynamic_cast isScStyleSheet() const184 virtual bool isScStyleSheet() const { return false; } 185 }; 186 187 /* Class to iterate and search on a SfxStyleSheetBasePool */ 188 class SVL_DLLPUBLIC SfxStyleSheetIterator 189 { 190 public: 191 /** Constructor. 192 * The iterator will only iterate over style sheets which have the family \p eFam 193 */ 194 SfxStyleSheetIterator(SfxStyleSheetBasePool *pBase, 195 SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All ); 196 SfxStyleSearchBits GetSearchMask() const; 197 SfxStyleFamily GetSearchFamily() const; 198 virtual sal_Int32 Count(); 199 virtual SfxStyleSheetBase *operator[](sal_Int32 nIdx); 200 virtual SfxStyleSheetBase* First(); 201 virtual SfxStyleSheetBase* Next(); 202 virtual SfxStyleSheetBase* Find(const OUString& rStr); 203 virtual ~SfxStyleSheetIterator(); 204 SearchUsed() const205 bool SearchUsed() const { return bSearchUsed; } 206 207 protected: 208 209 SfxStyleSheetBasePool* pBasePool; 210 SfxStyleFamily nSearchFamily; 211 SfxStyleSearchBits nMask; 212 213 214 private: 215 SVL_DLLPRIVATE bool IsTrivialSearch() const; 216 217 SfxStyleSheetBase* pCurrentStyle; 218 sal_uInt16 nCurrentPosition; 219 bool bSearchUsed; 220 221 friend class SfxStyleSheetBasePool; 222 }; 223 224 class SfxStyleSheetBasePool_Impl; 225 226 class SVL_DLLPUBLIC SfxStyleSheetBasePool: public SfxBroadcaster, public comphelper::OWeakTypeObject 227 { 228 friend class SfxStyleSheetIterator; 229 friend class SfxStyleSheetBase; 230 231 std::unique_ptr<SfxStyleSheetBasePool_Impl> pImpl; 232 233 SfxStyleSheetIterator& GetIterator_Impl(SfxStyleFamily eFamily, SfxStyleSearchBits eMask); 234 protected: 235 SfxStyleSheetIterator* GetCachedIterator(); 236 237 SfxItemPool& rPool; 238 239 void ChangeParent(std::u16string_view rOld, const OUString& rNew, SfxStyleFamily eFamily, bool bVirtual = true); 240 virtual rtl::Reference<SfxStyleSheetBase> Create( const OUString&, SfxStyleFamily, SfxStyleSearchBits ); 241 virtual rtl::Reference<SfxStyleSheetBase> Create( const SfxStyleSheetBase& ); 242 243 virtual ~SfxStyleSheetBasePool() override; 244 245 void StoreStyleSheet(const rtl::Reference< SfxStyleSheetBase >&); 246 247 /** Obtain the indexed style sheets. 248 */ 249 const svl::IndexedStyleSheets& 250 GetIndexedStyleSheets() const; 251 SfxStyleSheetBase* GetStyleSheetByPositionInIndex(unsigned pos); 252 253 public: 254 SfxStyleSheetBasePool( SfxItemPool& ); 255 SfxStyleSheetBasePool( const SfxStyleSheetBasePool& ); 256 GetPool()257 SfxItemPool& GetPool() { return rPool;} GetPool() const258 const SfxItemPool& GetPool() const { return rPool;} 259 260 virtual std::unique_ptr<SfxStyleSheetIterator> CreateIterator(SfxStyleFamily, SfxStyleSearchBits nMask = SfxStyleSearchBits::All); 261 262 virtual SfxStyleSheetBase& Make(const OUString&, 263 SfxStyleFamily eFam, 264 SfxStyleSearchBits nMask = SfxStyleSearchBits::All); 265 266 virtual void Remove( SfxStyleSheetBase* ); 267 void Insert( SfxStyleSheetBase* ); 268 269 void Clear(); 270 271 SfxStyleSheetBasePool& operator=( const SfxStyleSheetBasePool& ); 272 SfxStyleSheetBasePool& operator+=( const SfxStyleSheetBasePool& ); 273 274 SfxStyleSheetBase* First(SfxStyleFamily eFamily, SfxStyleSearchBits eMask = SfxStyleSearchBits::All); 275 SfxStyleSheetBase* Next(); 276 virtual SfxStyleSheetBase* Find( const OUString&, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All ); 277 278 virtual bool SetParent(SfxStyleFamily eFam, 279 const OUString &rStyle, 280 const OUString &rParent); 281 282 void Reindex(); 283 /** Add a style sheet. 284 * Not an actual public function. Do not call it from non-subclasses. 285 */ 286 void Add( const SfxStyleSheetBase& ); 287 }; 288 289 class SVL_DLLPUBLIC SfxStyleSheet: public SfxStyleSheetBase, 290 public SfxListener, public SfxBroadcaster, public svl::StyleSheetUser 291 { 292 public: 293 294 SfxStyleSheet( const OUString&, const SfxStyleSheetBasePool&, SfxStyleFamily, SfxStyleSearchBits ); 295 SfxStyleSheet( const SfxStyleSheet& ); 296 297 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override; 298 299 virtual bool isUsedByModel() const override; 300 301 virtual bool SetParent( const OUString& ) override; 302 303 protected: 304 virtual ~SfxStyleSheet() override; 305 }; 306 307 class SVL_DLLPUBLIC SfxStyleSheetPool: public SfxStyleSheetBasePool 308 { 309 protected: 310 using SfxStyleSheetBasePool::Create; 311 virtual rtl::Reference<SfxStyleSheetBase> Create(const OUString&, SfxStyleFamily, SfxStyleSearchBits mask) override; 312 313 public: 314 SfxStyleSheetPool( SfxItemPool const& ); 315 }; 316 317 318 class SVL_DLLPUBLIC SfxStyleSheetPoolHint final : public SfxHint 319 { 320 public: SfxStyleSheetPoolHint()321 SfxStyleSheetPoolHint() {} 322 }; 323 324 325 class SVL_DLLPUBLIC SfxStyleSheetHint: public SfxHint 326 { 327 SfxStyleSheetBase* pStyleSh; 328 public: 329 SfxStyleSheetHint( SfxHintId, SfxStyleSheetBase& ); GetStyleSheet() const330 SfxStyleSheetBase* GetStyleSheet() const 331 { return pStyleSh; } 332 }; 333 334 class UNLESS_MERGELIBS(SVL_DLLPUBLIC) SfxStyleSheetModifiedHint final : public SfxStyleSheetHint 335 { 336 OUString aName; 337 338 public: 339 SfxStyleSheetModifiedHint( const OUString& rOld, 340 SfxStyleSheetBase& ); 341 const OUString& GetOldName() const { return aName; } 342 }; 343 344 class SVL_DLLPUBLIC SfxUnoStyleSheet : public cppu::ImplInheritanceHelper<SfxStyleSheet, css::style::XStyle, css::lang::XUnoTunnel> 345 { 346 public: 347 SfxUnoStyleSheet( const OUString& _rName, const SfxStyleSheetBasePool& _rPool, SfxStyleFamily _eFamily, SfxStyleSearchBits _nMask ); 348 349 static SfxUnoStyleSheet* getUnoStyleSheet( const css::uno::Reference< css::style::XStyle >& xStyle ); 350 351 // XUnoTunnel 352 static const css::uno::Sequence< ::sal_Int8 >& getUnoTunnelId(); 353 virtual ::sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< ::sal_Int8 >& aIdentifier ) override; 354 }; 355 356 #endif 357 358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 359