xref: /aoo4110/main/sfx2/source/explorer/nochaos.cxx (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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 
27 #include <svl/itempool.hxx>
28 #include <svl/poolitem.hxx>
29 #include <svl/stritem.hxx>
30 #include <nochaos.hxx>
31 #include <sfx2/sfxuno.hxx>
32 
33 
34 #define WID_CHAOS_START 500
35 //=========================================================================
36 //
37 // class CntStaticPoolDefaults_Impl
38 //
39 //=========================================================================
40 
41 class CntItemPool;
42 
43 class CntStaticPoolDefaults_Impl
44 {
45 	sal_uInt32		  m_nItems;
46 	SfxPoolItem** m_ppDefaults;
47 	SfxItemInfo*  m_pItemInfos;
48 
49 private:
50 	// Forbidden and not implemented...
51 	CntStaticPoolDefaults_Impl( const CntStaticPoolDefaults_Impl& );
52     CntStaticPoolDefaults_Impl&	operator=( const CntStaticPoolDefaults_Impl& );
53 
54 	inline void Insert( SfxPoolItem* pItem, sal_uInt16 nSID, sal_uInt16 nFlags );
55 
56 public:
57 	CntStaticPoolDefaults_Impl( CntItemPool* pPool );
58 	~CntStaticPoolDefaults_Impl();
59 
GetDefaults() const60 	SfxPoolItem** 	   GetDefaults() const  { return m_ppDefaults; }
GetItemInfos() const61 	const SfxItemInfo* GetItemInfos() const { return m_pItemInfos; }
62 };
63 
64 //----------------------------------------------------------------------------
65 
66 //=========================================================================
67 
68 class CntItemPool: public SfxItemPool
69 {
70 	static CntItemPool* _pThePool;
71 	sal_uInt16              _nRefs;
72 
73 protected:
74 			 CntItemPool();
75 	virtual ~CntItemPool();
76 
77 public:
78 	static CntItemPool* Acquire();
79 	static sal_uInt16 		Release();
80 };
81 
82 //----------------------------------------------------------------------------
83 
84 //----------------------------------------------------------------------------
85 // static
GetItemPool()86 SfxItemPool* NoChaos::GetItemPool()
87 {
88 	// Get and hold CHAOS item pool.
89 	return CntItemPool::Acquire();
90 }
91 
92 //----------------------------------------------------------------------------
93 // static
ReleaseItemPool()94 sal_uInt16 NoChaos::ReleaseItemPool()
95 {
96 	// Release CHAOS item pool.
97 	return CntItemPool::Release();
98 }
99 
100 //=========================================================================
101 //
102 //	CntItemPool implementation
103 //
104 //=========================================================================
105 
106 static CntStaticPoolDefaults_Impl* pPoolDefs_Impl = NULL;
107 
108 // static member!
109 CntItemPool* CntItemPool::_pThePool = NULL;
110 
111 //-------------------------------------------------------------------------
CntItemPool()112 CntItemPool::CntItemPool()
113 : SfxItemPool( DEFINE_CONST_UNICODE("chaos"), WID_CHAOS_START, WID_CHAOS_START, NULL ),
114   _nRefs( 0 )
115 {
116     SetFileFormatVersion( SOFFICE_FILEFORMAT_50 );
117 
118 	FreezeIdRanges();
119 
120 	// Create static defaults.
121 	pPoolDefs_Impl = new CntStaticPoolDefaults_Impl( this );
122 
123 	// Set item infos.
124 	SetItemInfos( pPoolDefs_Impl->GetItemInfos() );
125 
126 	// Set static pool default items.
127 	SetDefaults( pPoolDefs_Impl->GetDefaults() );
128 }
129 
130 //-------------------------------------------------------------------------
131 //virtual
~CntItemPool()132 CntItemPool::~CntItemPool()
133 {
134 	// Release static pool default items.
135 	ReleaseDefaults( sal_False );
136 }
137 
138 //-------------------------------------------------------------------------
139 // static
Acquire()140 CntItemPool* CntItemPool::Acquire()
141 {
142 	if ( !_pThePool )
143 		_pThePool = new CntItemPool;
144 
145 	_pThePool->_nRefs++;
146 
147 	return _pThePool;
148 }
149 
150 //-------------------------------------------------------------------------
151 // static
Release()152 sal_uInt16 CntItemPool::Release()
153 {
154 	if ( !_pThePool )
155 		return 0;
156 
157 	sal_uInt16& nRefs = _pThePool->_nRefs;
158 
159 	if ( nRefs )
160 		--nRefs;
161 
162 	if ( !nRefs )
163 	{
164 		DELETEZ( _pThePool );
165 		DELETEZ( pPoolDefs_Impl );
166 		return 0;
167 	}
168 
169 	return nRefs;
170 }
171 
172 //=========================================================================
173 //
174 // CntStaticPoolDefaults_Impl implementation.
175 //
176 //=========================================================================
177 
Insert(SfxPoolItem * pItem,sal_uInt16 nSID,sal_uInt16 nFlags)178 inline void CntStaticPoolDefaults_Impl::Insert(
179 	 	SfxPoolItem* pItem, 		/* Static Pool Default Item */
180      	sal_uInt16 nSID, sal_uInt16 nFlags  /* Item Info */	)
181 {
182 	sal_uInt16 nPos = pItem->Which() - WID_CHAOS_START;
183 
184 	m_ppDefaults[ nPos ]         = pItem;
185 	m_pItemInfos[ nPos ]._nSID   = nSID;
186 	m_pItemInfos[ nPos ]._nFlags = nFlags;
187 }
188 
189 //-------------------------------------------------------------------------
~CntStaticPoolDefaults_Impl()190 CntStaticPoolDefaults_Impl::~CntStaticPoolDefaults_Impl()
191 {
192 	for ( sal_uInt32 n = 0; n < m_nItems; ++n )
193 		delete m_ppDefaults[ n ];
194 
195 	delete [] m_ppDefaults;
196 	delete [] m_pItemInfos;
197 }
198 
199 //-------------------------------------------------------------------------
CntStaticPoolDefaults_Impl(CntItemPool *)200 CntStaticPoolDefaults_Impl::CntStaticPoolDefaults_Impl( CntItemPool* /*pPool*/ )
201 : m_nItems( 1 ),
202   m_ppDefaults( new SfxPoolItem* [ m_nItems ] ),
203   m_pItemInfos( new SfxItemInfo  [ m_nItems ] )
204 {
205     rtl_zeroMemory( m_ppDefaults, sizeof( SfxPoolItem* ) * m_nItems );
206     rtl_zeroMemory( m_pItemInfos, sizeof( SfxItemInfo ) * m_nItems );
207 	Insert(
208 		new	SfxStringItem( WID_CHAOS_START, String() ),
209 		0,
210 		SFX_ITEM_POOLABLE );
211 }
212