xref: /aoo42x/main/sw/inc/fmtmeta.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef SW_FMTMETA_HXX
29*cdf0e10cSrcweir #define SW_FMTMETA_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <svl/poolitem.hxx>
34*cdf0e10cSrcweir #include <sfx2/Metadatable.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
37*cdf0e10cSrcweir #include <boost/weak_ptr.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <vector>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace com { namespace sun { namespace star {
43*cdf0e10cSrcweir     namespace text {
44*cdf0e10cSrcweir         class XTextField;
45*cdf0e10cSrcweir     }
46*cdf0e10cSrcweir }}}
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /**
50*cdf0e10cSrcweir  * The classes that make up a meta entity are:
51*cdf0e10cSrcweir  * <dl>
52*cdf0e10cSrcweir  *   <dt>SwTxtMeta</dt><dd>the text hint</dd>
53*cdf0e10cSrcweir  *   <dt>SwFmtMeta</dt><dd>the pool item</dd>
54*cdf0e10cSrcweir  *   <dt>sw::Meta</dt><dd>the metadatable entity itself</dd>
55*cdf0e10cSrcweir  *   <dt>SwXMeta</dt><dd>the UNO wrapper object</dd>
56*cdf0e10cSrcweir  * </dl>
57*cdf0e10cSrcweir  *
58*cdf0e10cSrcweir  * The text hint contains the pool item (as usual) and has a pointer to the
59*cdf0e10cSrcweir  * text node at which it is attached.
60*cdf0e10cSrcweir  * The pool item has a shared pointer to the metadatable entity, and a reverse
61*cdf0e10cSrcweir  * pointer to the text attribute at which it is attached.
62*cdf0e10cSrcweir  * The pool item is non-poolable; it may only be attached to one text
63*cdf0e10cSrcweir  * attribute.
64*cdf0e10cSrcweir  * Of all the pool items that refer to a metadatable entity, only one may be
65*cdf0e10cSrcweir  * in the document content at any time. Others may be in the undo array, or in
66*cdf0e10cSrcweir  * undo objects.
67*cdf0e10cSrcweir  * The metadatable entity has a reverse pointer to the pool item that is
68*cdf0e10cSrcweir  * currently in the document. It also registers as a client at the text node
69*cdf0e10cSrcweir  * at which it is attached via this pool item and its text attribute.
70*cdf0e10cSrcweir  * The UNO wrapper object registers as a client at the metadatable entity.
71*cdf0e10cSrcweir  *
72*cdf0e10cSrcweir  * Copying the metadatable entity proceeds in the following way:
73*cdf0e10cSrcweir  * <ol>
74*cdf0e10cSrcweir  *   <li>The pool item is cloned (because it is non-poolable); the clone
75*cdf0e10cSrcweir  *       points to the same metadatable entity, but the metadatable entity's
76*cdf0e10cSrcweir  *       reverse pointer is unchanged.</li>
77*cdf0e10cSrcweir  *   <li>The DoCopy() method is called at the new pool item:
78*cdf0e10cSrcweir  *       it will clone the metadatable entity (using RegisterAsCopyOf).
79*cdf0e10cSrcweir  *       This is necessary, because first, a metadatable entity may
80*cdf0e10cSrcweir  *       only be inserted once into a document, and second, the copy may be
81*cdf0e10cSrcweir  *       inserted into a different document than the source document!</li>
82*cdf0e10cSrcweir  *   <li>A new text hint is created, taking over the new pool item.</li>
83*cdf0e10cSrcweir  *   <li>The text hint is inserted into the hints array of some text node.</li>
84*cdf0e10cSrcweir  * </ol>
85*cdf0e10cSrcweir  */
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir class SwTxtMeta;
88*cdf0e10cSrcweir class SwXMeta;
89*cdf0e10cSrcweir class SwXMetaField;
90*cdf0e10cSrcweir namespace sw {
91*cdf0e10cSrcweir     class Meta;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir class SwFmtMeta
95*cdf0e10cSrcweir     : public SfxPoolItem
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir private:
98*cdf0e10cSrcweir     friend class SwTxtMeta; // needs SetTxtAttr, DoCopy
99*cdf0e10cSrcweir     friend class ::sw::Meta; // needs m_pTxtAttr
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     ::boost::shared_ptr< ::sw::Meta > m_pMeta;
102*cdf0e10cSrcweir     SwTxtMeta * m_pTxtAttr;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     SwTxtMeta * GetTxtAttr() { return m_pTxtAttr; }
105*cdf0e10cSrcweir     void SetTxtAttr(SwTxtMeta * const i_pTxtAttr);
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     /// this method <em>must</em> be called when the hint is actually copied
108*cdf0e10cSrcweir     void DoCopy(::sw::MetaFieldManager & i_rTargetDocManager,
109*cdf0e10cSrcweir         SwTxtNode & i_rTargetTxtNode);
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     explicit SwFmtMeta( const sal_uInt16 i_nWhich );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir public:
114*cdf0e10cSrcweir     // takes ownership
115*cdf0e10cSrcweir     explicit SwFmtMeta( ::boost::shared_ptr< ::sw::Meta > const & i_pMeta,
116*cdf0e10cSrcweir                         const sal_uInt16 i_nWhich );
117*cdf0e10cSrcweir     virtual ~SwFmtMeta();
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     // SfxPoolItem
120*cdf0e10cSrcweir     virtual int              operator==( const SfxPoolItem & ) const;
121*cdf0e10cSrcweir     virtual SfxPoolItem *    Clone( SfxItemPool *pPool = 0 ) const;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     /// notify clients registered at m_pMeta that this meta is being (re-)moved
124*cdf0e10cSrcweir     void NotifyChangeTxtNode(SwTxtNode *const pTxtNode);
125*cdf0e10cSrcweir     static SwFmtMeta * CreatePoolDefault( const sal_uInt16 i_nWhich );
126*cdf0e10cSrcweir     ::sw::Meta * GetMeta() { return m_pMeta.get(); }
127*cdf0e10cSrcweir };
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir namespace sw {
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir class MetaFieldManager;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir class Meta
135*cdf0e10cSrcweir     : public ::sfx2::Metadatable
136*cdf0e10cSrcweir     , public SwModify
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir protected:
139*cdf0e10cSrcweir     friend class ::SwFmtMeta; // SetFmtMeta, NotifyChangeTxtNode
140*cdf0e10cSrcweir     friend class ::SwXMeta; // GetTxtNode, GetTxtAttr, Get/SetXMeta
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     ::com::sun::star::uno::WeakReference<
143*cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable> m_wXMeta;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     SwFmtMeta * m_pFmt;
146*cdf0e10cSrcweir     SwTxtNode * m_pTxtNode;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     SwTxtMeta * GetTxtAttr() const;
149*cdf0e10cSrcweir     SwTxtNode * GetTxtNode() const; // returns 0 if not in document (undo)
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     SwFmtMeta * GetFmtMeta() const { return m_pFmt; }
152*cdf0e10cSrcweir     void SetFmtMeta( SwFmtMeta * const i_pFmt ) { m_pFmt = i_pFmt; };
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     void NotifyChangeTxtNodeImpl();
155*cdf0e10cSrcweir     void NotifyChangeTxtNode(SwTxtNode *const pTxtNode);
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     ::com::sun::star::uno::WeakReference<
158*cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable> const& GetXMeta() const
159*cdf0e10cSrcweir             { return m_wXMeta; }
160*cdf0e10cSrcweir     void SetXMeta(::com::sun::star::uno::Reference<
161*cdf0e10cSrcweir                     ::com::sun::star::rdf::XMetadatable> const& xMeta)
162*cdf0e10cSrcweir             { m_wXMeta = xMeta; }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     // SwClient
165*cdf0e10cSrcweir     virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir public:
168*cdf0e10cSrcweir     explicit Meta(SwFmtMeta * const i_pFmt = 0);
169*cdf0e10cSrcweir     virtual ~Meta();
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     // sfx2::Metadatable
172*cdf0e10cSrcweir     virtual ::sfx2::IXmlIdRegistry& GetRegistry();
173*cdf0e10cSrcweir     virtual bool IsInClipboard() const;
174*cdf0e10cSrcweir     virtual bool IsInUndo() const;
175*cdf0e10cSrcweir     virtual bool IsInContent() const;
176*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference<
177*cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable > MakeUnoObject();
178*cdf0e10cSrcweir };
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir class MetaField
181*cdf0e10cSrcweir     : public Meta
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir private:
184*cdf0e10cSrcweir     friend class ::SwFmtMeta;
185*cdf0e10cSrcweir     friend class ::SwXMetaField;
186*cdf0e10cSrcweir     friend class ::sw::MetaFieldManager;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     sal_uInt32 m_nNumberFormat;
189*cdf0e10cSrcweir     bool       m_bIsFixedLanguage;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     sal_uInt32 GetNumberFormat(::rtl::OUString const & rContent) const;
192*cdf0e10cSrcweir     void SetNumberFormat(sal_uInt32 nNumberFormat);
193*cdf0e10cSrcweir     bool IsFixedLanguage() const    { return m_bIsFixedLanguage; }
194*cdf0e10cSrcweir     void SetIsFixedLanguage(bool b) { m_bIsFixedLanguage = b; }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     explicit MetaField(SwFmtMeta * const i_pFmt = 0,
197*cdf0e10cSrcweir             const sal_uInt32 nNumberFormat = SAL_MAX_UINT32,
198*cdf0e10cSrcweir             const bool bIsFixedLanguage = false );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir public:
201*cdf0e10cSrcweir     /// get prefix/suffix from the RDF repository. @throws RuntimeException
202*cdf0e10cSrcweir     void GetPrefixAndSuffix(
203*cdf0e10cSrcweir         ::rtl::OUString *const o_pPrefix, ::rtl::OUString *const o_pSuffix);
204*cdf0e10cSrcweir };
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir /** knows all meta-fields in the document. */
207*cdf0e10cSrcweir class MetaFieldManager
208*cdf0e10cSrcweir     : private ::boost::noncopyable
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir private:
211*cdf0e10cSrcweir     typedef ::std::vector< ::boost::weak_ptr<MetaField> > MetaFieldList_t;
212*cdf0e10cSrcweir     MetaFieldList_t m_MetaFields;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir public:
215*cdf0e10cSrcweir     MetaFieldManager();
216*cdf0e10cSrcweir     ::boost::shared_ptr<MetaField> makeMetaField(
217*cdf0e10cSrcweir                 SwFmtMeta * const i_pFmt = 0,
218*cdf0e10cSrcweir                 const sal_uInt32 nNumberFormat = SAL_MAX_UINT32,
219*cdf0e10cSrcweir                 const bool bIsFixedLanguage = false );
220*cdf0e10cSrcweir     /// get all meta fields
221*cdf0e10cSrcweir     ::std::vector< ::com::sun::star::uno::Reference<
222*cdf0e10cSrcweir         ::com::sun::star::text::XTextField> > getMetaFields();
223*cdf0e10cSrcweir };
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir } // namespace sw
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir #endif // SW_FMTMETA_HXX
228*cdf0e10cSrcweir 
229