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