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