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_SW_INC_SECTION_HXX
21 #define INCLUDED_SW_INC_SECTION_HXX
22 
23 #include <com/sun/star/uno/Sequence.h>
24 
25 #include <tools/ref.hxx>
26 #include <svl/hint.hxx>
27 #include <sfx2/lnkbase.hxx>
28 #include <sfx2/Metadatable.hxx>
29 
30 #include "frmfmt.hxx"
31 #include <vector>
32 
33 namespace com { namespace sun { namespace star {
34     namespace text { class XTextSection; }
35 } } }
36 
37 class SwSectionFormat;
38 class SwDoc;
39 class SwSection;
40 class SwSectionNode;
41 class SwTOXBase;
42 class SwServerObject;
43 
44 typedef std::vector<SwSection*> SwSections;
45 
46 enum SectionType { CONTENT_SECTION,
47                     TOX_HEADER_SECTION,
48                     TOX_CONTENT_SECTION,
49                     DDE_LINK_SECTION    = OBJECT_CLIENT_DDE,
50                     FILE_LINK_SECTION   = OBJECT_CLIENT_FILE
51                     };
52 
53 enum LinkCreateType
54 {
55     CREATE_NONE,            // Do nothing.
56     CREATE_CONNECT,         // Connect created link.
57     CREATE_UPDATE           // Connect created link and update it.
58 };
59 
60 class SW_DLLPUBLIC SwSectionData
61 {
62 private:
63     SectionType m_eType;
64 
65     OUString m_sSectionName;
66     OUString m_sCondition;
67     OUString m_sLinkFileName;
68     OUString m_sLinkFilePassword; // Must be changed to Sequence.
69     css::uno::Sequence <sal_Int8> m_Password;
70 
71     /// It seems this flag caches the current final "hidden" state.
72     bool m_bHiddenFlag          : 1;
73     /// Flags that correspond to attributes in the format:
74     /// may have different value than format attribute:
75     /// format attr has value for this section, while flag is
76     /// effectively ORed with parent sections!
77     bool m_bProtectFlag         : 1;
78     // Edit in readonly sections.
79     bool m_bEditInReadonlyFlag  : 1;
80 
81     bool m_bHidden              : 1; // All paragraphs hidden?
82     bool m_bCondHiddenFlag      : 1; // Hiddenflag for condition.
83     bool m_bConnectFlag         : 1; // Connected to server?
84 
85 public:
86 
87     SwSectionData(SectionType const eType, OUString const& rName);
88     explicit SwSectionData(SwSection const&);
89     SwSectionData(SwSectionData const&);
90     SwSectionData & operator=(SwSectionData const&);
91     bool operator==(SwSectionData const&) const;
92 
GetSectionName() const93     const OUString& GetSectionName() const         { return m_sSectionName; }
SetSectionName(OUString const & rName)94     void SetSectionName(OUString const& rName){ m_sSectionName = rName; }
GetType() const95     SectionType GetType() const             { return m_eType; }
SetType(SectionType const eNew)96     void SetType(SectionType const eNew)    { m_eType = eNew; }
97 
IsHidden() const98     bool IsHidden() const { return m_bHidden; }
SetHidden(bool const bFlag)99     void SetHidden(bool const bFlag) { m_bHidden = bFlag; }
100 
IsHiddenFlag() const101     bool IsHiddenFlag() const { return m_bHiddenFlag; }
102     SAL_DLLPRIVATE void
SetHiddenFlag(bool const bFlag)103         SetHiddenFlag(bool const bFlag) { m_bHiddenFlag = bFlag; }
IsProtectFlag() const104     bool IsProtectFlag() const { return m_bProtectFlag; }
105     SAL_DLLPRIVATE void
SetProtectFlag(bool const bFlag)106         SetProtectFlag(bool const bFlag) { m_bProtectFlag = bFlag; }
IsEditInReadonlyFlag() const107     bool IsEditInReadonlyFlag() const { return m_bEditInReadonlyFlag; }
SetEditInReadonlyFlag(bool const bFlag)108     void SetEditInReadonlyFlag(bool const bFlag)
109         { m_bEditInReadonlyFlag = bFlag; }
110 
SetCondHidden(bool const bFlag)111     void SetCondHidden(bool const bFlag) { m_bCondHiddenFlag = bFlag; }
IsCondHidden() const112     bool IsCondHidden() const { return m_bCondHiddenFlag; }
113 
GetCondition() const114     const OUString& GetCondition() const           { return m_sCondition; }
SetCondition(OUString const & rNew)115     void SetCondition(OUString const& rNew) { m_sCondition = rNew; }
116 
GetLinkFileName() const117     const OUString& GetLinkFileName() const        { return m_sLinkFileName; }
SetLinkFileName(OUString const & rNew)118     void SetLinkFileName(OUString const& rNew)
119     {
120         m_sLinkFileName = rNew;
121     }
122 
GetLinkFilePassword() const123     const OUString& GetLinkFilePassword() const        { return m_sLinkFilePassword; }
SetLinkFilePassword(OUString const & rS)124     void SetLinkFilePassword(OUString const& rS){ m_sLinkFilePassword = rS; }
125 
GetPassword() const126     css::uno::Sequence<sal_Int8> const& GetPassword() const
127                                             { return m_Password; }
SetPassword(css::uno::Sequence<sal_Int8> const & rNew)128     void SetPassword(css::uno::Sequence<sal_Int8> const& rNew)
129                                             { m_Password = rNew; }
IsLinkType() const130     bool IsLinkType() const
131     { return (DDE_LINK_SECTION == m_eType) || (FILE_LINK_SECTION == m_eType); }
132 
IsConnectFlag() const133     bool IsConnectFlag() const                  { return m_bConnectFlag; }
SetConnectFlag(bool const bFlag)134     void SetConnectFlag(bool const bFlag){ m_bConnectFlag = bFlag; }
135 
136     static OUString CollapseWhiteSpaces(const OUString& sName);
137 };
138 
139 class SW_DLLPUBLIC SwSection
140     : public SwClient
141 {
142     // In order to correctly maintain the flag when creating/deleting frames.
143     friend class SwSectionNode;
144     // The "read CTOR" of SwSectionFrame have to change the Hiddenflag.
145     friend class SwSectionFrame;
146 
147 private:
148     mutable SwSectionData m_Data;
149 
150     tools::SvRef<SwServerObject> m_RefObj; // Set if DataServer.
151     tools::SvRef<sfx2::SvBaseLink> m_RefLink;
152 
153     SAL_DLLPRIVATE void ImplSetHiddenFlag(
154             bool const bHidden, bool const bCondition);
155 
156 protected:
157     virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew ) override;
158 
159 public:
160 
161     SwSection(SectionType const eType, OUString const& rName,
162                 SwSectionFormat & rFormat);
163     virtual ~SwSection() override;
164 
165     bool DataEquals(SwSectionData const& rCmp) const;
166 
167     void SetSectionData(SwSectionData const& rData);
168 
GetSectionName() const169     const OUString& GetSectionName() const         { return m_Data.GetSectionName(); }
SetSectionName(OUString const & rName)170     void SetSectionName(OUString const& rName){ m_Data.SetSectionName(rName); }
GetType() const171     SectionType GetType() const             { return m_Data.GetType(); }
SetType(SectionType const eType)172     void SetType(SectionType const eType)   { return m_Data.SetType(eType); }
173 
174     inline SwSectionFormat* GetFormat();
175     inline SwSectionFormat const * GetFormat() const;
176 
177     // Set hidden/protected -> update the whole tree!
178     // (Attributes/flags are set/get.)
IsHidden() const179     bool IsHidden()  const { return m_Data.IsHidden(); }
180     void SetHidden (bool const bFlag = true);
181     bool IsProtect() const;
182     void SetProtect(bool const bFlag = true);
183     bool IsEditInReadonly() const;
184     void SetEditInReadonly(bool const bFlag = true);
185 
186     // Get internal flags (state including parents, not what is
187     // currently set at section!).
IsHiddenFlag() const188     bool IsHiddenFlag()  const { return m_Data.IsHiddenFlag(); }
IsProtectFlag() const189     bool IsProtectFlag() const { return m_Data.IsProtectFlag(); }
IsEditInReadonlyFlag() const190     bool IsEditInReadonlyFlag() const { return m_Data.IsEditInReadonlyFlag(); }
191 
192     void SetCondHidden(bool const bFlag);
IsCondHidden() const193     bool IsCondHidden() const { return m_Data.IsCondHidden(); }
194     // Query (also for parents) if this section is to be hidden.
195     bool CalcHiddenFlag() const;
196 
197     inline SwSection* GetParent() const;
198 
GetCondition() const199     OUString const & GetCondition() const           { return m_Data.GetCondition(); }
SetCondition(OUString const & rNew)200     void SetCondition(OUString const& rNew) { m_Data.SetCondition(rNew); }
201 
202     OUString const & GetLinkFileName() const;
203     void SetLinkFileName(OUString const& rNew);
204     // Password of linked file (only valid during runtime!)
GetLinkFilePassword() const205     OUString const & GetLinkFilePassword() const
206         { return m_Data.GetLinkFilePassword(); }
SetLinkFilePassword(OUString const & rS)207     void SetLinkFilePassword(OUString const& rS)
208         { m_Data.SetLinkFilePassword(rS); }
209 
210     // Get / set password of this section
GetPassword() const211     css::uno::Sequence<sal_Int8> const& GetPassword() const
212                                             { return m_Data.GetPassword(); }
213 
214     // Data server methods.
215     void SetRefObject( SwServerObject* pObj );
GetObject() const216     const SwServerObject* GetObject() const {  return m_RefObj.get(); }
GetObject()217           SwServerObject* GetObject()       {  return m_RefObj.get(); }
IsServer() const218     bool IsServer() const                   {  return m_RefObj.is(); }
219 
220     // Methods for linked ranges.
GetUpdateType() const221     SfxLinkUpdateMode GetUpdateType() const    { return m_RefLink->GetUpdateMode(); }
SetUpdateType(SfxLinkUpdateMode nType)222     void SetUpdateType(SfxLinkUpdateMode nType )
223         { m_RefLink->SetUpdateMode(nType); }
224 
IsConnected() const225     bool IsConnected() const        { return m_RefLink.is(); }
UpdateNow()226     void UpdateNow()                { m_RefLink->Update(); }
Disconnect()227     void Disconnect()               { m_RefLink->Disconnect(); }
228 
GetBaseLink() const229     const ::sfx2::SvBaseLink& GetBaseLink() const    { return *m_RefLink; }
GetBaseLink()230           ::sfx2::SvBaseLink& GetBaseLink()          { return *m_RefLink; }
231 
232     void CreateLink( LinkCreateType eType );
233 
234     static void MakeChildLinksVisible( const SwSectionNode& rSectNd );
235 
IsLinkType() const236     bool IsLinkType() const { return m_Data.IsLinkType(); }
237 
238     // Flags for UI. Did connection work?
IsConnectFlag() const239     bool IsConnectFlag() const      { return m_Data.IsConnectFlag(); }
SetConnectFlag(bool const bFlag=true)240     void SetConnectFlag(bool const bFlag = true)
241                                     { m_Data.SetConnectFlag(bFlag); }
242 
243     // Return the TOX base class if the section is a TOX section
244     const SwTOXBase* GetTOXBase() const;
245 
246     void BreakLink();
247 
248 };
249 
250 // #i117863#
251 class SwSectionFrameMoveAndDeleteHint final : public SfxHint
252 {
253     public:
SwSectionFrameMoveAndDeleteHint(const bool bSaveContent)254         SwSectionFrameMoveAndDeleteHint( const bool bSaveContent )
255             : SfxHint( SfxHintId::SwSectionFrameMoveAndDelete )
256             , mbSaveContent( bSaveContent )
257         {}
258 
IsSaveContent() const259         bool IsSaveContent() const
260         {
261             return mbSaveContent;
262         }
263 
264     private:
265         const bool mbSaveContent;
266 };
267 
268 enum class SectionSort { Not, Pos };
269 
270 class SW_DLLPUBLIC SwSectionFormat final
271     : public SwFrameFormat
272     , public ::sfx2::Metadatable
273 {
274     friend class SwDoc;
275 
276     /** Why does this exist in addition to the m_wXObject in SwFrameFormat?
277         in case of an index, both a SwXDocumentIndex and a SwXTextSection
278         register at this SwSectionFormat, so we need to have two refs.
279      */
280     css::uno::WeakReference<css::text::XTextSection> m_wXTextSection;
281 
282     SAL_DLLPRIVATE void UpdateParent();      // Parent has been changed.
283 
284     SwSectionFormat( SwFrameFormat* pDrvdFrame, SwDoc *pDoc );
285     virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew ) override;
286 
287 public:
288     virtual ~SwSectionFormat() override;
289 
290     // Deletes all Frames in aDepend (Frames are recognized via dynamic_cast).
291     virtual void DelFrames() override;
292 
293     // Creates views.
294     virtual void MakeFrames() override;
295 
296     // Get information from Format.
297     virtual bool GetInfo( SfxPoolItem& ) const override;
298 
299     SwSection* GetSection() const;
300     inline SwSectionFormat* GetParent() const;
301     inline SwSection* GetParentSection() const;
302 
303     //  All sections that are derived from this one:
304     //  - sorted according to name or position or unsorted
305     //  - all of them or only those that are in the normal Nodes-array.
306     void GetChildSections( SwSections& rArr,
307                             SectionSort eSort = SectionSort::Not,
308                             bool bAllSections = true ) const;
309 
310     // Query whether section is in Nodes-array or in UndoNodes-array.
311     bool IsInNodesArr() const;
312 
313           SwSectionNode* GetSectionNode();
GetSectionNode() const314     const SwSectionNode* GetSectionNode() const
315         { return const_cast<SwSectionFormat *>(this)
316                 ->GetSectionNode(); }
317 
318     // Is section a valid one for global document?
319     const SwSection* GetGlobalDocSection() const;
320 
GetXTextSection() const321     SAL_DLLPRIVATE css::uno::WeakReference<css::text::XTextSection> const& GetXTextSection() const
322             { return m_wXTextSection; }
SetXTextSection(css::uno::Reference<css::text::XTextSection> const & xTextSection)323     SAL_DLLPRIVATE void SetXTextSection(css::uno::Reference<css::text::XTextSection> const& xTextSection)
324             { m_wXTextSection = xTextSection; }
325 
326     // sfx2::Metadatable
327     virtual ::sfx2::IXmlIdRegistry& GetRegistry() override;
328     virtual bool IsInClipboard() const override;
329     virtual bool IsInUndo() const override;
330     virtual bool IsInContent() const override;
331     virtual css::uno::Reference< css::rdf::XMetadatable > MakeUnoObject() override;
332     virtual bool supportsFullDrawingLayerFillAttributeSet() const override;
333     void dumpAsXml(xmlTextWriterPtr pWriter) const;
334 
335 };
336 
GetFormat()337 SwSectionFormat* SwSection::GetFormat()
338 {
339     return static_cast<SwSectionFormat*>(GetRegisteredIn());
340 }
341 
GetFormat() const342 SwSectionFormat const * SwSection::GetFormat() const
343 {
344     return static_cast<SwSectionFormat const *>(GetRegisteredIn());
345 }
346 
GetParent() const347 inline SwSection* SwSection::GetParent() const
348 {
349     SwSectionFormat const * pFormat = GetFormat();
350     SwSection* pRet = nullptr;
351     if( pFormat )
352         pRet = pFormat->GetParentSection();
353     return pRet;
354 }
355 
GetParent() const356 inline SwSectionFormat* SwSectionFormat::GetParent() const
357 {
358     SwSectionFormat* pRet = nullptr;
359     if( GetRegisteredIn() )
360         pRet = const_cast<SwSectionFormat*>(dynamic_cast< const SwSectionFormat* >( GetRegisteredIn() ));
361     return pRet;
362 }
363 
GetParentSection() const364 inline SwSection* SwSectionFormat::GetParentSection() const
365 {
366     SwSectionFormat* pParent = GetParent();
367     SwSection* pRet = nullptr;
368     if( pParent )
369     {
370         pRet = pParent->GetSection();
371     }
372     return pRet;
373 }
374 
375 #endif /* _ INCLUDED_SW_INC_SECTION_HXX */
376 
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
378