1*1d2dbeb0SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*1d2dbeb0SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*1d2dbeb0SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*1d2dbeb0SAndrew Rist * distributed with this work for additional information
6*1d2dbeb0SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*1d2dbeb0SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*1d2dbeb0SAndrew Rist * "License"); you may not use this file except in compliance
9*1d2dbeb0SAndrew Rist * with the License. You may obtain a copy of the License at
10*1d2dbeb0SAndrew Rist *
11*1d2dbeb0SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*1d2dbeb0SAndrew Rist *
13*1d2dbeb0SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*1d2dbeb0SAndrew Rist * software distributed under the License is distributed on an
15*1d2dbeb0SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1d2dbeb0SAndrew Rist * KIND, either express or implied. See the License for the
17*1d2dbeb0SAndrew Rist * specific language governing permissions and limitations
18*1d2dbeb0SAndrew Rist * under the License.
19*1d2dbeb0SAndrew Rist *
20*1d2dbeb0SAndrew Rist *************************************************************/
21*1d2dbeb0SAndrew Rist
22*1d2dbeb0SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef _CALBCK_HXX
25cdf0e10cSrcweir #define _CALBCK_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <tools/rtti.hxx>
28cdf0e10cSrcweir #include "swdllapi.h"
29cdf0e10cSrcweir #include <boost/noncopyable.hpp>
30cdf0e10cSrcweir
31cdf0e10cSrcweir class SwModify;
32cdf0e10cSrcweir class SwClientIter;
33cdf0e10cSrcweir class SfxPoolItem;
34cdf0e10cSrcweir class SfxHint;
35cdf0e10cSrcweir
36cdf0e10cSrcweir /*
37cdf0e10cSrcweir SwModify and SwClient cooperate in propagating attribute changes.
38cdf0e10cSrcweir If an attribute changes, the change is notified to all dependent
39cdf0e10cSrcweir formats and other interested objects, e.g. Nodes. The clients will detect
40cdf0e10cSrcweir if the change affects them. It could be that the changed attribute is
41cdf0e10cSrcweir overruled in the receiving object so that its change does not become
42cdf0e10cSrcweir effective or that the receiver is not interested in the particular attribute
43cdf0e10cSrcweir in general (though probably in other attributes of the SwModify object they
44cdf0e10cSrcweir are registered in).
45cdf0e10cSrcweir As SwModify objects are derived from SwClient, they can create a chain of SwClient
46cdf0e10cSrcweir objects where changes can get propagated through.
47cdf0e10cSrcweir Each SwClient can be registered at only one SwModify object, while each SwModify
48cdf0e10cSrcweir object is connected to a list of SwClient objects. If an object derived from SwClient
49cdf0e10cSrcweir wants to get notifications from more than one SwModify object, it must create additional
50cdf0e10cSrcweir SwClient objects. The SwDepend class allows to handle their notifications in the same
51cdf0e10cSrcweir notification callback as it forwards the Modify() calls it receives to a "master"
52cdf0e10cSrcweir SwClient implementation.
53cdf0e10cSrcweir The SwClientIter class allows to iterate over the SwClient objects registered at an
54cdf0e10cSrcweir SwModify. For historical reasons its ability to use TypeInfo to restrict this iteration
55cdf0e10cSrcweir to objects of a particular type created a lot of code that misuses SwClient-SwModify
56cdf0e10cSrcweir relationships that basically should be used only for Modify() callbacks.
57cdf0e10cSrcweir This is still subject to refactoring.
58cdf0e10cSrcweir Until this gets resolved, new SwClientIter base code should be reduced to the absolute
59cdf0e10cSrcweir minimum and it also should be wrapped by SwIterator templates that prevent that the
60cdf0e10cSrcweir code gets polluted by pointer casts (see switerator.hxx).
61cdf0e10cSrcweir */
62cdf0e10cSrcweir
63cdf0e10cSrcweir // ----------
64cdf0e10cSrcweir // SwClient
65cdf0e10cSrcweir // ----------
66cdf0e10cSrcweir
67cdf0e10cSrcweir class SW_DLLPUBLIC SwClient : ::boost::noncopyable
68cdf0e10cSrcweir {
69cdf0e10cSrcweir // avoids making the details of the linked list and the callback method public
70cdf0e10cSrcweir friend class SwModify;
71cdf0e10cSrcweir friend class SwClientIter;
72cdf0e10cSrcweir
73cdf0e10cSrcweir SwClient *pLeft, *pRight; // double-linked list of other clients
74cdf0e10cSrcweir SwModify *pRegisteredIn; // event source
75cdf0e10cSrcweir
76cdf0e10cSrcweir // in general clients should not be removed when their SwModify sends out Modify()
77cdf0e10cSrcweir // notifications; in some rare cases this is necessary, but only the concrete SwClient
78cdf0e10cSrcweir // sub class will know that; this flag allows to make that known
79cdf0e10cSrcweir bool mbIsAllowedToBeRemovedInModifyCall;
80cdf0e10cSrcweir
81cdf0e10cSrcweir // callbacks received from SwModify (friend class - so these methods can be private)
82cdf0e10cSrcweir // should be called only from SwModify the client is registered in
83cdf0e10cSrcweir // mba: IMHO these methods should be pure virtual
84cdf0e10cSrcweir virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew);
85cdf0e10cSrcweir virtual void SwClientNotify( const SwModify& rModify, const SfxHint& rHint );
86cdf0e10cSrcweir
87cdf0e10cSrcweir protected:
88cdf0e10cSrcweir // single argument ctors shall be explicit.
89cdf0e10cSrcweir explicit SwClient(SwModify *pToRegisterIn);
90cdf0e10cSrcweir
91cdf0e10cSrcweir // write access to pRegisteredIn shall be granted only to the object itself (protected access)
GetRegisteredInNonConst() const92cdf0e10cSrcweir SwModify* GetRegisteredInNonConst() const { return pRegisteredIn; }
SetIsAllowedToBeRemovedInModifyCall(bool bSet)93cdf0e10cSrcweir void SetIsAllowedToBeRemovedInModifyCall( bool bSet ) { mbIsAllowedToBeRemovedInModifyCall = bSet; }
94cdf0e10cSrcweir
95cdf0e10cSrcweir public:
96cdf0e10cSrcweir
97cdf0e10cSrcweir inline SwClient();
98cdf0e10cSrcweir virtual ~SwClient();
99cdf0e10cSrcweir
100cdf0e10cSrcweir // in case an SwModify object is destroyed that itself is registered in another SwModify,
101cdf0e10cSrcweir // its SwClient objects can decide to get registered to the latter instead by calling this method
102cdf0e10cSrcweir void CheckRegistration( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue );
103cdf0e10cSrcweir
104cdf0e10cSrcweir // controlled access to Modify method
105cdf0e10cSrcweir // mba: this is still considered a hack and it should be fixed; the name makes grep-ing easier
ModifyNotification(const SfxPoolItem * pOldValue,const SfxPoolItem * pNewValue)106cdf0e10cSrcweir void ModifyNotification( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue ) { Modify ( pOldValue, pNewValue ); }
SwClientNotifyCall(const SwModify & rModify,const SfxHint & rHint)107cdf0e10cSrcweir void SwClientNotifyCall( const SwModify& rModify, const SfxHint& rHint ) { SwClientNotify( rModify, rHint ); }
108cdf0e10cSrcweir
GetRegisteredIn() const109cdf0e10cSrcweir const SwModify* GetRegisteredIn() const { return pRegisteredIn; }
IsLast() const110cdf0e10cSrcweir bool IsLast() const { return !pLeft && !pRight; }
111cdf0e10cSrcweir
112cdf0e10cSrcweir // needed for class SwClientIter
113cdf0e10cSrcweir TYPEINFO();
114cdf0e10cSrcweir
115cdf0e10cSrcweir // get information about attribute
116cdf0e10cSrcweir virtual sal_Bool GetInfo( SfxPoolItem& ) const;
117cdf0e10cSrcweir };
118cdf0e10cSrcweir
SwClient()119cdf0e10cSrcweir inline SwClient::SwClient() :
120cdf0e10cSrcweir pLeft(0), pRight(0), pRegisteredIn(0), mbIsAllowedToBeRemovedInModifyCall(false)
121cdf0e10cSrcweir {}
122cdf0e10cSrcweir
123cdf0e10cSrcweir // ----------
124cdf0e10cSrcweir // SwModify
125cdf0e10cSrcweir // ----------
126cdf0e10cSrcweir
127cdf0e10cSrcweir class SW_DLLPUBLIC SwModify: public SwClient
128cdf0e10cSrcweir {
129cdf0e10cSrcweir // friend class SwClientIter;
130cdf0e10cSrcweir
131cdf0e10cSrcweir SwClient* pRoot; // the start of the linked list of clients
132cdf0e10cSrcweir sal_Bool bModifyLocked : 1; // don't broadcast changes now
133cdf0e10cSrcweir sal_Bool bLockClientList : 1; // may be set when this instance notifies its clients
134cdf0e10cSrcweir sal_Bool bInDocDTOR : 1; // workaround for problems when a lot of objects are destroyed
135cdf0e10cSrcweir sal_Bool bInCache : 1;
136cdf0e10cSrcweir sal_Bool bInSwFntCache : 1;
137cdf0e10cSrcweir
138cdf0e10cSrcweir // mba: IMHO this method should be pure virtual
139cdf0e10cSrcweir virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew);
140cdf0e10cSrcweir
141cdf0e10cSrcweir public:
142cdf0e10cSrcweir SwModify();
143cdf0e10cSrcweir
144cdf0e10cSrcweir // broadcasting: send notifications to all clients
145cdf0e10cSrcweir void NotifyClients( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue );
146cdf0e10cSrcweir
147cdf0e10cSrcweir // the same, but without setting bModifyLocked or checking for any of the flags
148cdf0e10cSrcweir // mba: it would be interesting to know why this is necessary
149cdf0e10cSrcweir // also allows to limit callback to certain type (HACK)
150cdf0e10cSrcweir void ModifyBroadcast( const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue, TypeId nType = TYPE(SwClient) );
151cdf0e10cSrcweir
152cdf0e10cSrcweir // a more universal broadcasting mechanism
153cdf0e10cSrcweir void CallSwClientNotify( const SfxHint& rHint ) const;
154cdf0e10cSrcweir
155cdf0e10cSrcweir // single argument ctors shall be explicit.
156cdf0e10cSrcweir explicit SwModify( SwModify *pToRegisterIn );
157cdf0e10cSrcweir virtual ~SwModify();
158cdf0e10cSrcweir
159cdf0e10cSrcweir void Add(SwClient *pDepend);
160cdf0e10cSrcweir SwClient* Remove(SwClient *pDepend);
GetDepends() const161cdf0e10cSrcweir const SwClient* GetDepends() const { return pRoot; }
162cdf0e10cSrcweir
163cdf0e10cSrcweir // get information about attribute
164cdf0e10cSrcweir virtual sal_Bool GetInfo( SfxPoolItem& ) const;
165cdf0e10cSrcweir
LockModify()166cdf0e10cSrcweir void LockModify() { bModifyLocked = sal_True; }
UnlockModify()167cdf0e10cSrcweir void UnlockModify() { bModifyLocked = sal_False; }
SetInCache(sal_Bool bNew)168cdf0e10cSrcweir void SetInCache( sal_Bool bNew ) { bInCache = bNew; }
SetInSwFntCache(sal_Bool bNew)169cdf0e10cSrcweir void SetInSwFntCache( sal_Bool bNew ) { bInSwFntCache = bNew; }
SetInDocDTOR()170cdf0e10cSrcweir void SetInDocDTOR() { bInDocDTOR = sal_True; }
IsModifyLocked() const171cdf0e10cSrcweir sal_Bool IsModifyLocked() const { return bModifyLocked; }
IsInDocDTOR() const172cdf0e10cSrcweir sal_Bool IsInDocDTOR() const { return bInDocDTOR; }
IsInCache() const173cdf0e10cSrcweir sal_Bool IsInCache() const { return bInCache; }
IsInSwFntCache() const174cdf0e10cSrcweir sal_Bool IsInSwFntCache() const { return bInSwFntCache; }
175cdf0e10cSrcweir
176cdf0e10cSrcweir void CheckCaching( const sal_uInt16 nWhich );
IsLastDepend()177cdf0e10cSrcweir bool IsLastDepend() { return pRoot && pRoot->IsLast(); }
178cdf0e10cSrcweir int GetClientCount() const;
179cdf0e10cSrcweir };
180cdf0e10cSrcweir
181cdf0e10cSrcweir // ----------
182cdf0e10cSrcweir // SwDepend
183cdf0e10cSrcweir // ----------
184cdf0e10cSrcweir
185cdf0e10cSrcweir /*
186cdf0e10cSrcweir * Helper class for objects that need to depend on more than one SwClient
187cdf0e10cSrcweir */
188cdf0e10cSrcweir class SW_DLLPUBLIC SwDepend: public SwClient
189cdf0e10cSrcweir {
190cdf0e10cSrcweir SwClient *pToTell;
191cdf0e10cSrcweir
192cdf0e10cSrcweir public:
SwDepend()193cdf0e10cSrcweir SwDepend() : pToTell(0) {}
194cdf0e10cSrcweir SwDepend(SwClient *pTellHim, SwModify *pDepend);
195cdf0e10cSrcweir
GetToTell()196cdf0e10cSrcweir SwClient* GetToTell() { return pToTell; }
197cdf0e10cSrcweir
198cdf0e10cSrcweir virtual sal_Bool GetInfo( SfxPoolItem & ) const;
199cdf0e10cSrcweir
200cdf0e10cSrcweir protected:
201cdf0e10cSrcweir virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNewValue );
202cdf0e10cSrcweir virtual void SwClientNotify( const SwModify& rModify, const SfxHint& rHint );
203cdf0e10cSrcweir };
204cdf0e10cSrcweir
205cdf0e10cSrcweir
206cdf0e10cSrcweir class SwClientIter
207cdf0e10cSrcweir {
208cdf0e10cSrcweir friend SwClient* SwModify::Remove(SwClient *); // for pointer adjustments
209cdf0e10cSrcweir friend void SwModify::Add(SwClient *pDepend); // for pointer adjustments
210cdf0e10cSrcweir
211cdf0e10cSrcweir const SwModify& rRoot;
212cdf0e10cSrcweir
213cdf0e10cSrcweir // the current object in an iteration
214cdf0e10cSrcweir SwClient* pAct;
215cdf0e10cSrcweir
216cdf0e10cSrcweir // in case the current object is already removed, the next object in the list
217cdf0e10cSrcweir // is marked down to become the current object in the next step
218cdf0e10cSrcweir // this is necessary because iteration requires access to members of the current object
219cdf0e10cSrcweir SwClient* pDelNext;
220cdf0e10cSrcweir
221cdf0e10cSrcweir // SwClientIter objects are tracked in linked list so that they can react
222cdf0e10cSrcweir // when the current (pAct) or marked down (pDelNext) SwClient is removed
223cdf0e10cSrcweir // from its SwModify
224cdf0e10cSrcweir SwClientIter *pNxtIter;
225cdf0e10cSrcweir
226cdf0e10cSrcweir // iterator can be limited to return only SwClient objects of a certain type
227cdf0e10cSrcweir TypeId aSrchId;
228cdf0e10cSrcweir
229cdf0e10cSrcweir public:
230cdf0e10cSrcweir SW_DLLPUBLIC SwClientIter( const SwModify& );
231cdf0e10cSrcweir SW_DLLPUBLIC ~SwClientIter();
232cdf0e10cSrcweir
GetModify() const233cdf0e10cSrcweir const SwModify& GetModify() const { return rRoot; }
234cdf0e10cSrcweir
2352f121198SMathias Bauer SwClient* operator++();
236cdf0e10cSrcweir SwClient* GoStart();
237cdf0e10cSrcweir SwClient* GoEnd();
238cdf0e10cSrcweir
239cdf0e10cSrcweir // returns the current SwClient object;
240cdf0e10cSrcweir // in case this was already removed, the object marked down to become
241cdf0e10cSrcweir // the next current one is returned
operator ()() const242cdf0e10cSrcweir SwClient* operator()() const
243cdf0e10cSrcweir { return pDelNext == pAct ? pAct : pDelNext; }
244cdf0e10cSrcweir
245cdf0e10cSrcweir // return "true" if an object was removed from a client chain in iteration
246cdf0e10cSrcweir // adding objects to a client chain in iteration is forbidden
247cdf0e10cSrcweir // SwModify::Add() asserts this
IsChanged() const248cdf0e10cSrcweir bool IsChanged() const { return pDelNext != pAct; }
249cdf0e10cSrcweir
250cdf0e10cSrcweir SW_DLLPUBLIC SwClient* First( TypeId nType );
251cdf0e10cSrcweir SW_DLLPUBLIC SwClient* Next();
252cdf0e10cSrcweir SW_DLLPUBLIC SwClient* Last( TypeId nType );
253cdf0e10cSrcweir SW_DLLPUBLIC SwClient* Previous();
254cdf0e10cSrcweir };
255cdf0e10cSrcweir
256cdf0e10cSrcweir #endif
257