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_SOURCE_CORE_INC_BOOKMRK_HXX
21 #define INCLUDED_SW_SOURCE_CORE_INC_BOOKMRK_HXX
22 
23 #include <cppuhelper/weakref.hxx>
24 #include <sfx2/Metadatable.hxx>
25 #include <vcl/keycod.hxx>
26 #include <memory>
27 #include <string_view>
28 
29 #include <rtl/ustring.hxx>
30 #include <osl/diagnose.h>
31 #include <tools/ref.hxx>
32 #include <IMark.hxx>
33 #include <swrect.hxx>
34 #include "FormFieldButton.hxx"
35 
36 namespace com::sun::star::text { class XTextContent; }
37 
38 class SwDoc;
39 class SwEditWin;
40 class SwServerObject;
41 class SvNumberFormatter;
42 class SfxViewShell;
43 
44 namespace sw::mark {
45         class MarkBase
46             : virtual public IMark
47         {
48         public:
49             //getters
GetMarkPos() const50             virtual SwPosition& GetMarkPos() const override
51                 { return *m_pPos1; }
GetName() const52             virtual const OUString& GetName() const override
53                 { return m_aName; }
GetOtherMarkPos() const54             virtual SwPosition& GetOtherMarkPos() const override
55             {
56                 OSL_PRECOND(IsExpanded(), "<SwPosition::GetOtherMarkPos(..)> - I have no other Pos set." );
57                 return *m_pPos2;
58             }
GetMarkStart() const59             virtual SwPosition& GetMarkStart() const override
60             {
61                 if( !IsExpanded() ) return GetMarkPos( );
62                 if ( GetMarkPos( ) < GetOtherMarkPos( ) )
63                     return GetMarkPos();
64                 else
65                     return GetOtherMarkPos( );
66             }
GetMarkEnd() const67             virtual SwPosition& GetMarkEnd() const override
68             {
69                 if( !IsExpanded() ) return GetMarkPos();
70                 if ( GetMarkPos( ) >= GetOtherMarkPos( ) )
71                     return GetMarkPos( );
72                 else
73                     return GetOtherMarkPos( );
74             }
75 
76             virtual bool IsCoveringPosition(const SwPosition& rPos) const override;
IsExpanded() const77             virtual bool IsExpanded() const override
78                 { return static_cast< bool >(m_pPos2); }
79 
SetName(const OUString & rName)80             void SetName(const OUString& rName)
81                 { m_aName = rName; }
82             virtual void SetMarkPos(const SwPosition& rNewPos);
83             virtual void SetOtherMarkPos(const SwPosition& rNewPos);
ClearOtherMarkPos()84             virtual void ClearOtherMarkPos()
85                 { m_pPos2.reset(); }
86 
87             virtual auto InvalidateFrames() -> void;
88 
89             virtual OUString ToString( ) const override;
90             virtual void dumpAsXml(xmlTextWriterPtr pWriter) const override;
91 
Swap()92             void Swap()
93             {
94                 if(m_pPos2)
95                     m_pPos1.swap(m_pPos2);
96             }
97 
InitDoc(SwDoc &,sw::mark::InsertMode,SwPosition const *)98             virtual void InitDoc(SwDoc&, sw::mark::InsertMode, SwPosition const*)
99             {
100             }
101 
102             virtual ~MarkBase() override;
103 
GetXBookmark() const104             const css::uno::WeakReference< css::text::XTextContent> & GetXBookmark() const
105                     { return m_wXBookmark; }
SetXBookmark(css::uno::Reference<css::text::XTextContent> const & xBkmk)106             void SetXBookmark(css::uno::Reference< css::text::XTextContent> const& xBkmk)
107                     { m_wXBookmark = xBkmk; }
108 
109         protected:
110             // SwClient
111             virtual void SwClientNotify(const SwModify&, const SfxHint&) override;
112 
113             MarkBase(const SwPaM& rPaM, const OUString& rName);
114             std::unique_ptr<SwPosition> m_pPos1;
115             std::unique_ptr<SwPosition> m_pPos2;
116             OUString m_aName;
117             static OUString GenerateNewName(std::u16string_view rPrefix);
118 
119             css::uno::WeakReference< css::text::XTextContent> m_wXBookmark;
120         };
121 
122         class NavigatorReminder
123             : public MarkBase
124         {
125         public:
126             NavigatorReminder(const SwPaM& rPaM);
127         };
128 
129         class UnoMark
130             : public MarkBase
131         {
132         public:
133             UnoMark(const SwPaM& rPaM);
134         };
135 
136         class DdeBookmark
137             : public MarkBase
138         {
139         public:
140             DdeBookmark(const SwPaM& rPaM);
141 
GetRefObject() const142             const SwServerObject* GetRefObject() const { return m_aRefObj.get(); }
GetRefObject()143             SwServerObject* GetRefObject() { return m_aRefObj.get(); }
144 
IsServer() const145             bool IsServer() const { return m_aRefObj.is(); }
146 
147             void SetRefObject( SwServerObject* pObj );
148 
149             virtual void DeregisterFromDoc(SwDoc& rDoc);
150             virtual ~DdeBookmark() override;
151 
152         private:
153             tools::SvRef<SwServerObject> m_aRefObj;
154         };
155 
156         class Bookmark
157             : virtual public IBookmark
158             , public DdeBookmark
159             , public ::sfx2::Metadatable
160         {
161         public:
162             Bookmark(const SwPaM& rPaM,
163                 const vcl::KeyCode& rCode,
164                 const OUString& rName);
165             virtual void InitDoc(SwDoc& io_Doc, sw::mark::InsertMode eMode, SwPosition const* pSepPos) override;
166 
167             virtual void DeregisterFromDoc(SwDoc& io_rDoc) override;
168 
169             virtual auto InvalidateFrames() -> void override;
170 
GetShortName() const171             virtual const OUString& GetShortName() const override
172                 { return m_sShortName; }
GetKeyCode() const173             virtual const vcl::KeyCode& GetKeyCode() const override
174                 { return m_aCode; }
SetShortName(const OUString & rShortName)175             virtual void SetShortName(const OUString& rShortName) override
176                 { m_sShortName = rShortName; }
SetKeyCode(const vcl::KeyCode & rCode)177             virtual void SetKeyCode(const vcl::KeyCode& rCode) override
178                 { m_aCode = rCode; }
IsHidden() const179             virtual bool IsHidden() const override
180                 { return m_bHidden; }
GetHideCondition() const181             virtual const OUString& GetHideCondition() const override
182                 { return m_sHideCondition; }
183             virtual void Hide(bool rHide) override;
184             virtual void SetHideCondition(const OUString& rHideCondition) override;
185 
186             // ::sfx2::Metadatable
187             virtual ::sfx2::IXmlIdRegistry& GetRegistry() override;
188             virtual bool IsInClipboard() const override;
189             virtual bool IsInUndo() const override;
190             virtual bool IsInContent() const override;
191             virtual css::uno::Reference< css::rdf::XMetadatable > MakeUnoObject() override;
192 
193         private:
194             vcl::KeyCode m_aCode;
195             OUString m_sShortName;
196             bool m_bHidden;
197             OUString m_sHideCondition;
198         };
199 
200         class Fieldmark
201             : virtual public IFieldmark
202             , public MarkBase
203         {
204         public:
205             Fieldmark(const SwPaM& rPaM);
206 
GetFieldname() const207             virtual OUString GetFieldname() const override
208                 { return m_aFieldname; }
GetFieldHelptext() const209             virtual OUString GetFieldHelptext() const override
210                 { return m_aFieldHelptext; }
211 
GetParameters()212             virtual IFieldmark::parameter_map_t* GetParameters() override
213                 { return &m_vParams; }
214 
GetParameters() const215             virtual const IFieldmark::parameter_map_t* GetParameters() const override
216                 { return &m_vParams; }
217 
SetFieldname(const OUString & aFieldname)218             virtual void SetFieldname(const OUString& aFieldname) override
219                 { m_aFieldname = aFieldname; }
SetFieldHelptext(const OUString & aFieldHelptext)220             virtual void SetFieldHelptext(const OUString& aFieldHelptext) override
221                 { m_aFieldHelptext = aFieldHelptext; }
222 
223             virtual void ReleaseDoc(SwDoc&) = 0;
224 
225             void SetMarkStartPos( const SwPosition& rNewStartPos );
226             void SetMarkEndPos( const SwPosition& rNewEndPos );
227 
228             virtual void Invalidate() override;
229             virtual OUString ToString() const override;
230             virtual void dumpAsXml(xmlTextWriterPtr pWriter) const override;
231 
232         private:
233             OUString m_aFieldname;
234             OUString m_aFieldHelptext;
235             IFieldmark::parameter_map_t m_vParams;
236         };
237 
238         class TextFieldmark
239             : public Fieldmark
240         {
241         public:
242             TextFieldmark(const SwPaM& rPaM, const OUString& rName);
243             virtual void InitDoc(SwDoc& io_rDoc, sw::mark::InsertMode eMode, SwPosition const* pSepPos) override;
244             virtual void ReleaseDoc(SwDoc& rDoc) override;
245         };
246 
247         // Non text fieldmarks have no content between the start and end marks.
248         class NonTextFieldmark
249             : public Fieldmark
250         {
251         public:
252             NonTextFieldmark(const SwPaM& rPaM);
253             virtual void InitDoc(SwDoc& io_rDoc, sw::mark::InsertMode eMode, SwPosition const* pSepPos) override;
254             virtual void ReleaseDoc(SwDoc& rDoc) override;
255         };
256 
257         /// Fieldmark representing a checkbox form field.
258         class CheckboxFieldmark
259             : virtual public ICheckboxFieldmark
260             , public NonTextFieldmark
261         {
262         public:
263             CheckboxFieldmark(const SwPaM& rPaM);
264             bool IsChecked() const override;
265             void SetChecked(bool checked) override;
266         };
267 
268         /// Fieldmark with a drop down button (e.g. this button opens the date picker for a date field)
269         class FieldmarkWithDropDownButton
270             : public NonTextFieldmark
271         {
272         public:
273             FieldmarkWithDropDownButton(const SwPaM& rPaM);
274             virtual ~FieldmarkWithDropDownButton() override;
275 
276             virtual void ShowButton(SwEditWin* pEditWin) = 0;
277             virtual void RemoveButton();
278 
279         protected:
280             VclPtr<FormFieldButton> m_pButton;
281         };
282 
283         /// Fieldmark representing a drop-down form field.
284         class DropDownFieldmark
285             : public FieldmarkWithDropDownButton
286         {
287         public:
288             DropDownFieldmark(const SwPaM& rPaM);
289             virtual ~DropDownFieldmark() override;
290 
291             virtual void ShowButton(SwEditWin* pEditWin) override;
292             virtual void RemoveButton() override;
293 
294             // This method should be called only by the portion so we can now the portion's painting area
295             void SetPortionPaintArea(const SwRect& rPortionPaintArea);
296 
297             void SendLOKShowMessage(SfxViewShell* pViewShell);
298             static void SendLOKHideMessage(SfxViewShell* pViewShell);
299 
300         private:
301             SwRect m_aPortionPaintArea;
302         };
303 
304         /// Fieldmark representing a date form field.
305         class DateFieldmark
306             : virtual public IDateFieldmark
307             , public FieldmarkWithDropDownButton
308         {
309         public:
310             DateFieldmark(const SwPaM& rPaM);
311             virtual ~DateFieldmark() override;
312 
313             virtual void InitDoc(SwDoc& io_rDoc, sw::mark::InsertMode eMode, SwPosition const* pSepPos) override;
314             virtual void ReleaseDoc(SwDoc& rDoc) override;
315 
316             virtual void ShowButton(SwEditWin* pEditWin) override;
317 
318             void SetPortionPaintAreaStart(const SwRect& rPortionPaintArea);
319             void SetPortionPaintAreaEnd(const SwRect& rPortionPaintArea);
320 
321             virtual OUString GetContent() const override;
322             virtual void ReplaceContent(const OUString& sNewContent) override;
323 
324             virtual std::pair<bool, double> GetCurrentDate() const override;
325             virtual void SetCurrentDate(double fDate) override;
326             virtual OUString GetDateInStandardDateFormat(double fDate) const override;
327 
328         private:
329             OUString GetDateInCurrentDateFormat(double fDate) const;
330             std::pair<bool, double> ParseCurrentDateParam() const;
331             void InvalidateCurrentDateParam();
332 
333             SvNumberFormatter* m_pNumberFormatter;
334             sw::DocumentContentOperationsManager* m_pDocumentContentOperationsManager;
335             SwRect m_aPaintAreaStart;
336             SwRect m_aPaintAreaEnd;
337         };
338 
339         /// return position of the CH_TXT_ATR_FIELDSEP for rMark
340         SwPosition FindFieldSep(IFieldmark const& rMark);
341 
342         /// check if rPaM is valid range of new fieldmark
343         bool IsFieldmarkOverlap(SwPaM const& rPaM);
344 }
345 
346 #endif
347 
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
349