xref: /AOO41X/main/sfx2/source/explorer/nochaos.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 
31 #include <svl/itempool.hxx>
32 #include <svl/poolitem.hxx>
33 #include <svl/stritem.hxx>
34 #include <nochaos.hxx>
35 #include <sfx2/sfxuno.hxx>
36 
37 
38 #define WID_CHAOS_START 500
39 //=========================================================================
40 //
41 // class CntStaticPoolDefaults_Impl
42 //
43 //=========================================================================
44 
45 class CntItemPool;
46 
47 class CntStaticPoolDefaults_Impl
48 {
49 	sal_uInt32		  m_nItems;
50 	SfxPoolItem** m_ppDefaults;
51 	SfxItemInfo*  m_pItemInfos;
52 
53 private:
54 	// Forbidden and not implemented...
55 	CntStaticPoolDefaults_Impl( const CntStaticPoolDefaults_Impl& );
56     CntStaticPoolDefaults_Impl&	operator=( const CntStaticPoolDefaults_Impl& );
57 
58 	inline void Insert( SfxPoolItem* pItem, sal_uInt16 nSID, sal_uInt16 nFlags );
59 
60 public:
61 	CntStaticPoolDefaults_Impl( CntItemPool* pPool );
62 	~CntStaticPoolDefaults_Impl();
63 
64 	SfxPoolItem** 	   GetDefaults() const  { return m_ppDefaults; }
65 	const SfxItemInfo* GetItemInfos() const { return m_pItemInfos; }
66 };
67 
68 //----------------------------------------------------------------------------
69 
70 //=========================================================================
71 
72 class CntItemPool: public SfxItemPool
73 {
74 	static CntItemPool* _pThePool;
75 	sal_uInt16              _nRefs;
76 
77 protected:
78 			 CntItemPool();
79 	virtual ~CntItemPool();
80 
81 public:
82 	static CntItemPool* Acquire();
83 	static sal_uInt16 		Release();
84 };
85 
86 //----------------------------------------------------------------------------
87 
88 //----------------------------------------------------------------------------
89 // static
90 SfxItemPool* NoChaos::GetItemPool()
91 {
92 	// Get and hold CHAOS item pool.
93 	return CntItemPool::Acquire();
94 }
95 
96 //----------------------------------------------------------------------------
97 // static
98 sal_uInt16 NoChaos::ReleaseItemPool()
99 {
100 	// Release CHAOS item pool.
101 	return CntItemPool::Release();
102 }
103 
104 //=========================================================================
105 //
106 //	CntItemPool implementation
107 //
108 //=========================================================================
109 
110 static CntStaticPoolDefaults_Impl* pPoolDefs_Impl = NULL;
111 
112 // static member!
113 CntItemPool* CntItemPool::_pThePool = NULL;
114 
115 //-------------------------------------------------------------------------
116 CntItemPool::CntItemPool()
117 : SfxItemPool( DEFINE_CONST_UNICODE("chaos"), WID_CHAOS_START, WID_CHAOS_START, NULL ),
118   _nRefs( 0 )
119 {
120     SetFileFormatVersion( SOFFICE_FILEFORMAT_50 );
121 
122 	FreezeIdRanges();
123 
124 	// Create static defaults.
125 	pPoolDefs_Impl = new CntStaticPoolDefaults_Impl( this );
126 
127 	// Set item infos.
128 	SetItemInfos( pPoolDefs_Impl->GetItemInfos() );
129 
130 	// Set static pool default items.
131 	SetDefaults( pPoolDefs_Impl->GetDefaults() );
132 }
133 
134 //-------------------------------------------------------------------------
135 //virtual
136 CntItemPool::~CntItemPool()
137 {
138 	// Release static pool default items.
139 	ReleaseDefaults( sal_False );
140 }
141 
142 //-------------------------------------------------------------------------
143 // static
144 CntItemPool* CntItemPool::Acquire()
145 {
146 	if ( !_pThePool )
147 		_pThePool = new CntItemPool;
148 
149 	_pThePool->_nRefs++;
150 
151 	return _pThePool;
152 }
153 
154 //-------------------------------------------------------------------------
155 // static
156 sal_uInt16 CntItemPool::Release()
157 {
158 	if ( !_pThePool )
159 		return 0;
160 
161 	sal_uInt16& nRefs = _pThePool->_nRefs;
162 
163 	if ( nRefs )
164 		--nRefs;
165 
166 	if ( !nRefs )
167 	{
168 		DELETEZ( _pThePool );
169 		DELETEZ( pPoolDefs_Impl );
170 		return 0;
171 	}
172 
173 	return nRefs;
174 }
175 
176 //=========================================================================
177 //
178 // CntStaticPoolDefaults_Impl implementation.
179 //
180 //=========================================================================
181 
182 inline void CntStaticPoolDefaults_Impl::Insert(
183 	 	SfxPoolItem* pItem, 		/* Static Pool Default Item */
184      	sal_uInt16 nSID, sal_uInt16 nFlags  /* Item Info */	)
185 {
186 	sal_uInt16 nPos = pItem->Which() - WID_CHAOS_START;
187 
188 	m_ppDefaults[ nPos ]         = pItem;
189 	m_pItemInfos[ nPos ]._nSID   = nSID;
190 	m_pItemInfos[ nPos ]._nFlags = nFlags;
191 }
192 
193 //-------------------------------------------------------------------------
194 CntStaticPoolDefaults_Impl::~CntStaticPoolDefaults_Impl()
195 {
196 	for ( sal_uInt32 n = 0; n < m_nItems; ++n )
197 		delete m_ppDefaults[ n ];
198 
199 	delete [] m_ppDefaults;
200 	delete [] m_pItemInfos;
201 }
202 
203 //-------------------------------------------------------------------------
204 CntStaticPoolDefaults_Impl::CntStaticPoolDefaults_Impl( CntItemPool* /*pPool*/ )
205 : m_nItems( 1 ),
206   m_ppDefaults( new SfxPoolItem* [ m_nItems ] ),
207   m_pItemInfos( new SfxItemInfo  [ m_nItems ] )
208 {
209     rtl_zeroMemory( m_ppDefaults, sizeof( SfxPoolItem* ) * m_nItems );
210     rtl_zeroMemory( m_pItemInfos, sizeof( SfxItemInfo ) * m_nItems );
211 	Insert(
212 		new	SfxStringItem( WID_CHAOS_START, String() ),
213 		0,
214 		SFX_ITEM_POOLABLE );
215 }
216