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_unotools.hxx"
30 #include <unotools/sourceviewconfig.hxx>
31 #include <com/sun/star/uno/Any.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <unotools/configitem.hxx>
34 #include <tools/debug.hxx>
35 #include <rtl/instance.hxx>
36 
37 #include <itemholder1.hxx>
38 
39 using namespace utl;
40 using namespace rtl;
41 using namespace com::sun::star::uno;
42 namespace utl
43 {
44 class SourceViewConfig_Impl : public utl::ConfigItem
45 {
46 private:
47     OUString        m_sFontName;
48     sal_Int16       m_nFontHeight;
49     sal_Bool        m_bProportionalFontOnly;
50 
51     void        Load();
52 
53 	static Sequence< OUString > GetPropertyNames();
54 
55 public:
56     SourceViewConfig_Impl();
57     ~SourceViewConfig_Impl();
58 
59     virtual void    Notify( const Sequence< rtl::OUString >& aPropertyNames );
60 	virtual void	Commit();
61 
62     const rtl::OUString&    GetFontName() const
63                                 {return m_sFontName;}
64     void                    SetFontName(const rtl::OUString& rName)
65                                 {
66                                     if(rName != m_sFontName)
67                                     {
68                                         m_sFontName = rName;
69                                         SetModified();
70                                     }
71                                 }
72 
73     sal_Int16               GetFontHeight() const
74                                 {return m_nFontHeight;}
75     void                    SetFontHeight(sal_Int16 nHeight)
76                                 {
77                                     if(m_nFontHeight != nHeight)
78                                     {
79                                         m_nFontHeight = nHeight;
80                                         SetModified();
81                                     }
82                                 }
83 
84     sal_Bool                IsShowProportionalFontsOnly() const
85                                 {return m_bProportionalFontOnly;}
86     void                    SetShowProportionalFontsOnly(sal_Bool bSet)
87                                 {
88                                     if(m_bProportionalFontOnly != bSet)
89                                     {
90                                         m_bProportionalFontOnly = bSet;
91                                         SetModified();
92                                     }
93                                 }
94 };
95 // initialization of static members --------------------------------------
96 SourceViewConfig_Impl* SourceViewConfig::m_pImplConfig = 0;
97 sal_Int32              SourceViewConfig::m_nRefCount = 0;
98 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
99 /* -----------------------------28.08.2002 16:45------------------------------
100 
101  ---------------------------------------------------------------------------*/
102 SourceViewConfig_Impl::SourceViewConfig_Impl() :
103     ConfigItem(OUString::createFromAscii("Office.Common/Font/SourceViewFont")),
104     m_nFontHeight(12),
105     m_bProportionalFontOnly(sal_False)
106 {
107 	Load();
108 }
109 /* -----------------------------28.08.2002 16:45------------------------------
110 
111  ---------------------------------------------------------------------------*/
112 SourceViewConfig_Impl::~SourceViewConfig_Impl()
113 {
114 }
115 /* -----------------------------28.08.2002 16:25------------------------------
116 
117  ---------------------------------------------------------------------------*/
118 Sequence< OUString > SourceViewConfig_Impl::GetPropertyNames()
119 {
120 	//this list needs exactly to mach the enum PropertyNameIndex
121 	static const char* aPropNames[] =
122 	{
123         "FontName"                  // 0
124         ,"FontHeight"               // 1
125         ,"NonProportionalFontsOnly" // 2
126 	};
127 	const int nCount = sizeof( aPropNames ) / sizeof( const char* );
128 	Sequence< OUString > aNames( nCount );
129 	OUString* pNames = aNames.getArray();
130 	for ( int i = 0; i < nCount; i++ )
131 		pNames[i] = OUString::createFromAscii( aPropNames[i] );
132 
133 	return aNames;
134 }
135 
136 /*-- 28.08.2002 16:37:59---------------------------------------------------
137 
138   -----------------------------------------------------------------------*/
139 void SourceViewConfig_Impl::Load()
140 {
141     Sequence< OUString > aNames = GetPropertyNames();
142 	Sequence< Any > aValues = GetProperties( aNames );
143 	EnableNotification( aNames );
144 	const Any* pValues = aValues.getConstArray();
145 	DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
146 	if ( aValues.getLength() == aNames.getLength() )
147 	{
148 		for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
149 		{
150 			if ( pValues[nProp].hasValue() )
151 			{
152                 switch( nProp )
153                 {
154                     case 0:  pValues[nProp] >>= m_sFontName;         break;
155                     case 1:  pValues[nProp] >>= m_nFontHeight;      break;
156                     case 2:  pValues[nProp] >>= m_bProportionalFontOnly;     break;
157                 }
158 			}
159 		}
160 	}
161 }
162 /*-- 28.08.2002 16:38:00---------------------------------------------------
163 
164   -----------------------------------------------------------------------*/
165 void SourceViewConfig_Impl::Notify( const Sequence< OUString >& )
166 {
167     Load();
168 }
169 /*-- 28.08.2002 16:38:00---------------------------------------------------
170 
171   -----------------------------------------------------------------------*/
172 void SourceViewConfig_Impl::Commit()
173 {
174     ClearModified();
175 	Sequence< OUString > aNames = GetPropertyNames();
176 	Sequence< Any > aValues( aNames.getLength() );
177 	Any* pValues = aValues.getArray();
178 	for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
179 	{
180         switch( nProp )
181 		{
182             case 0:  pValues[nProp] <<= m_sFontName;         break;
183             case 1:  pValues[nProp] <<= m_nFontHeight;      break;
184             case 2:  pValues[nProp] <<= m_bProportionalFontOnly;     break;
185             default:
186 				DBG_ERRORFILE( "invalid index to save a user token" );
187 		}
188 	}
189 	PutProperties( aNames, aValues );
190 
191     NotifyListeners(0);
192 }
193 /*-- 28.08.2002 16:32:19---------------------------------------------------
194 
195   -----------------------------------------------------------------------*/
196 SourceViewConfig::SourceViewConfig()
197 {
198     {
199         ::osl::MutexGuard aGuard( lclMutex::get() );
200         if(!m_pImplConfig)
201         {
202             m_pImplConfig = new SourceViewConfig_Impl;
203             ItemHolder1::holdConfigItem(E_SOURCEVIEWCONFIG);
204         }
205 
206          ++m_nRefCount;
207 	}
208 
209     m_pImplConfig->AddListener( this );
210 }
211 /*-- 28.08.2002 16:32:19---------------------------------------------------
212 
213   -----------------------------------------------------------------------*/
214 SourceViewConfig::~SourceViewConfig()
215 {
216     m_pImplConfig->RemoveListener( this );
217     ::osl::MutexGuard aGuard( lclMutex::get() );
218     if( !--m_nRefCount )
219 	{
220         if( m_pImplConfig->IsModified() )
221             m_pImplConfig->Commit();
222         DELETEZ( m_pImplConfig );
223 	}
224 }
225 /*-- 28.08.2002 16:32:19---------------------------------------------------
226 
227   -----------------------------------------------------------------------*/
228 const OUString&  SourceViewConfig::GetFontName() const
229 {
230     return m_pImplConfig->GetFontName();
231 }
232 /*-- 28.08.2002 16:32:20---------------------------------------------------
233 
234   -----------------------------------------------------------------------*/
235 void SourceViewConfig::SetFontName(const OUString& rName)
236 {
237     m_pImplConfig->SetFontName(rName);
238 }
239 /*-- 28.08.2002 16:32:20---------------------------------------------------
240 
241   -----------------------------------------------------------------------*/
242 sal_Int16 SourceViewConfig::GetFontHeight() const
243 {
244     return m_pImplConfig->GetFontHeight();
245 }
246 /*-- 28.08.2002 16:32:20---------------------------------------------------
247 
248   -----------------------------------------------------------------------*/
249 void SourceViewConfig::SetFontHeight(sal_Int16 nHeight)
250 {
251     m_pImplConfig->SetFontHeight(nHeight);
252 }
253 /*-- 28.08.2002 16:32:20---------------------------------------------------
254 
255   -----------------------------------------------------------------------*/
256 sal_Bool SourceViewConfig::IsShowProportionalFontsOnly() const
257 {
258     return m_pImplConfig->IsShowProportionalFontsOnly();
259 }
260 /*-- 28.08.2002 16:32:20---------------------------------------------------
261 
262   -----------------------------------------------------------------------*/
263 void SourceViewConfig::SetShowProportionalFontsOnly(sal_Bool bSet)
264 {
265     m_pImplConfig->SetShowProportionalFontsOnly(bSet);
266 }
267 }
268 // namespace utl
269