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