Update ooo320-m1
[ooovba.git] / svtools / source / config / menuoptions.cxx
blob5b49e41965691c159dd9f95f3e01ae0c0b53d46c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: menuoptions.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #ifndef GCC
34 #endif
36 //_________________________________________________________________________________________________________________
37 // includes
38 //_________________________________________________________________________________________________________________
40 #include <svtools/menuoptions.hxx>
41 #include <unotools/configmgr.hxx>
42 #include <unotools/configitem.hxx>
43 #include <tools/debug.hxx>
44 #include <com/sun/star/uno/Any.hxx>
45 #include <com/sun/star/uno/Sequence.hxx>
46 #include <vcl/svapp.hxx>
48 #include <rtl/logfile.hxx>
49 #include "itemholder1.hxx"
51 //_________________________________________________________________________________________________________________
52 // namespaces
53 //_________________________________________________________________________________________________________________
55 using namespace ::utl ;
56 using namespace ::rtl ;
57 using namespace ::osl ;
58 using namespace ::com::sun::star::uno ;
60 //_________________________________________________________________________________________________________________
61 // const
62 //_________________________________________________________________________________________________________________
64 #define ROOTNODE_MENU OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/View/Menu" ))
65 #define DEFAULT_DONTHIDEDISABLEDENTRIES sal_False
66 #define DEFAULT_FOLLOWMOUSE sal_True
67 #define DEFAULT_MENUICONS 2
69 #define PROPERTYNAME_DONTHIDEDISABLEDENTRIES OUString(RTL_CONSTASCII_USTRINGPARAM("DontHideDisabledEntry" ))
70 #define PROPERTYNAME_FOLLOWMOUSE OUString(RTL_CONSTASCII_USTRINGPARAM("FollowMouse" ))
71 #define PROPERTYNAME_SHOWICONSINMENUES OUString(RTL_CONSTASCII_USTRINGPARAM("ShowIconsInMenues" ))
72 #define PROPERTYNAME_SYSTEMICONSINMENUES OUString(RTL_CONSTASCII_USTRINGPARAM("IsSystemIconsInMenus" ))
74 #define PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES 0
75 #define PROPERTYHANDLE_FOLLOWMOUSE 1
76 #define PROPERTYHANDLE_SHOWICONSINMENUES 2
77 #define PROPERTYHANDLE_SYSTEMICONSINMENUES 3
79 #define PROPERTYCOUNT 4
81 #include <tools/link.hxx>
82 #include <tools/list.hxx>
83 DECLARE_LIST( LinkList, Link * )
85 //_________________________________________________________________________________________________________________
86 // private declarations!
87 //_________________________________________________________________________________________________________________
89 class SvtMenuOptions_Impl : public ConfigItem
91 //-------------------------------------------------------------------------------------------------------------
92 // private member
93 //-------------------------------------------------------------------------------------------------------------
95 private:
96 LinkList aList;
97 sal_Bool m_bDontHideDisabledEntries ; /// cache "DontHideDisabledEntries" of Menu section
98 sal_Bool m_bFollowMouse ; /// cache "FollowMouse" of Menu section
99 sal_Int16 m_nMenuIcons ; /// cache "MenuIcons" of Menu section
101 //-------------------------------------------------------------------------------------------------------------
102 // public methods
103 //-------------------------------------------------------------------------------------------------------------
105 public:
107 //---------------------------------------------------------------------------------------------------------
108 // constructor / destructor
109 //---------------------------------------------------------------------------------------------------------
111 SvtMenuOptions_Impl();
112 ~SvtMenuOptions_Impl();
114 void AddListener( const Link& rLink );
115 void RemoveListener( const Link& rLink );
117 //---------------------------------------------------------------------------------------------------------
118 // overloaded methods of baseclass
119 //---------------------------------------------------------------------------------------------------------
121 /*-****************************************************************************************************//**
122 @short called for notify of configmanager
123 @descr These method is called from the ConfigManager before application ends or from the
124 PropertyChangeListener if the sub tree broadcasts changes. You must update your
125 internal values.
127 @seealso baseclass ConfigItem
129 @param "seqPropertyNames" is the list of properties which should be updated.
130 @return -
132 @onerror -
133 *//*-*****************************************************************************************************/
135 virtual void Notify( const Sequence< OUString >& seqPropertyNames );
137 /*-****************************************************************************************************//**
138 @short write changes to configuration
139 @descr These method writes the changed values into the sub tree
140 and should always called in our destructor to guarantee consistency of config data.
142 @seealso baseclass ConfigItem
144 @param -
145 @return -
147 @onerror -
148 *//*-*****************************************************************************************************/
150 virtual void Commit();
152 //---------------------------------------------------------------------------------------------------------
153 // public interface
154 //---------------------------------------------------------------------------------------------------------
156 /*-****************************************************************************************************//**
157 @short access method to get internal values
158 @descr These method give us a chance to regulate acces to ouer internal values.
159 It's not used in the moment - but it's possible for the feature!
161 @seealso -
163 @param -
164 @return -
166 @onerror -
167 *//*-*****************************************************************************************************/
169 sal_Bool IsEntryHidingEnabled() const
170 { return m_bDontHideDisabledEntries; }
172 sal_Bool IsFollowMouseEnabled() const
173 { return m_bFollowMouse; }
175 sal_Int16 GetMenuIconsState() const
176 { return m_nMenuIcons; }
178 void SetEntryHidingState ( sal_Bool bState )
180 m_bDontHideDisabledEntries = bState;
181 SetModified();
182 for ( USHORT n=0; n<aList.Count(); n++ )
183 aList.GetObject(n)->Call( this );
184 Commit();
187 void SetFollowMouseState ( sal_Bool bState )
189 m_bFollowMouse = bState;
190 SetModified();
191 for ( USHORT n=0; n<aList.Count(); n++ )
192 aList.GetObject(n)->Call( this );
193 Commit();
196 void SetMenuIconsState ( sal_Int16 bState )
198 m_nMenuIcons = bState;
199 SetModified();
200 for ( USHORT n=0; n<aList.Count(); n++ )
201 aList.GetObject(n)->Call( this );
202 Commit();
205 //-------------------------------------------------------------------------------------------------------------
206 // private methods
207 //-------------------------------------------------------------------------------------------------------------
209 private:
211 /*-****************************************************************************************************//**
212 @short return list of fix key names of ouer configuration management which represent oue module tree
213 @descr These methods return a static const list of key names. We need it to get needed values from our
214 configuration management.
216 @seealso -
218 @param -
219 @return A list of needed configuration keys is returned.
221 @onerror -
222 *//*-*****************************************************************************************************/
224 static Sequence< OUString > impl_GetPropertyNames();
227 //_________________________________________________________________________________________________________________
228 // definitions
229 //_________________________________________________________________________________________________________________
231 //*****************************************************************************************************************
232 // constructor
233 //*****************************************************************************************************************
234 SvtMenuOptions_Impl::SvtMenuOptions_Impl()
235 // Init baseclasses first
236 : ConfigItem ( ROOTNODE_MENU )
237 // Init member then.
238 , m_bDontHideDisabledEntries ( DEFAULT_DONTHIDEDISABLEDENTRIES )
239 , m_bFollowMouse ( DEFAULT_FOLLOWMOUSE )
240 , m_nMenuIcons ( DEFAULT_MENUICONS )
242 // Use our static list of configuration keys to get his values.
243 Sequence< OUString > seqNames = impl_GetPropertyNames();
244 Sequence< Any > seqValues = GetProperties( seqNames ) ;
246 // Safe impossible cases.
247 // We need values from ALL configuration keys.
248 // Follow assignment use order of values in relation to our list of key names!
249 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nI miss some values of configuration keys!\n" );
251 sal_Bool bMenuIcons = true;
252 sal_Bool bSystemMenuIcons = true;
254 // Copy values from list in right order to ouer internal member.
255 sal_Int32 nPropertyCount = seqValues.getLength() ;
256 sal_Int32 nProperty = 0 ;
257 for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
259 // Safe impossible cases.
260 // Check any for valid value.
261 DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nInvalid property value for property detected!\n" );
262 switch( nProperty )
264 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES : {
265 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
266 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
268 break;
270 case PROPERTYHANDLE_FOLLOWMOUSE : {
271 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
272 seqValues[nProperty] >>= m_bFollowMouse;
274 break;
275 case PROPERTYHANDLE_SHOWICONSINMENUES : {
276 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
277 seqValues[nProperty] >>= bMenuIcons;
279 break;
280 case PROPERTYHANDLE_SYSTEMICONSINMENUES : {
281 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
282 seqValues[nProperty] >>= bSystemMenuIcons;
284 break;
288 m_nMenuIcons = bSystemMenuIcons ? 2 : bMenuIcons;
290 EnableNotification( seqNames );
293 //*****************************************************************************************************************
294 // destructor
295 //*****************************************************************************************************************
296 SvtMenuOptions_Impl::~SvtMenuOptions_Impl()
298 // Flush data to configuration!
299 // User has no chance to do that.
300 if( IsModified() == sal_True )
302 Commit();
305 for ( USHORT n=0; n<aList.Count(); )
306 delete aList.Remove(n);
309 //*****************************************************************************************************************
310 // public method
311 //*****************************************************************************************************************
312 void SvtMenuOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
314 // Use given list of updated properties to get his values from configuration directly!
315 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
316 // Safe impossible cases.
317 // We need values from ALL notified configuration keys.
318 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
320 sal_Bool bMenuIcons = true;
321 sal_Bool bSystemMenuIcons = true;
323 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
324 sal_Int32 nCount = seqPropertyNames.getLength();
325 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
327 if( seqPropertyNames[nProperty] == PROPERTYNAME_DONTHIDEDISABLEDENTRIES )
329 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
330 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
332 else if( seqPropertyNames[nProperty] == PROPERTYNAME_FOLLOWMOUSE )
334 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
335 seqValues[nProperty] >>= m_bFollowMouse;
337 else if( seqPropertyNames[nProperty] == PROPERTYNAME_SHOWICONSINMENUES )
339 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
340 seqValues[nProperty] >>= bMenuIcons;
342 else if( seqPropertyNames[nProperty] == PROPERTYNAME_SYSTEMICONSINMENUES )
344 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
345 seqValues[nProperty] >>= bSystemMenuIcons;
348 #if OSL_DEBUG_LEVEL > 1
349 else DBG_ASSERT( sal_False, "SvtMenuOptions_Impl::Notify()\nUnkown property detected ... I can't handle these!\n" );
350 #endif
353 m_nMenuIcons = bSystemMenuIcons ? 2 : bMenuIcons;
355 for ( USHORT n=0; n<aList.Count(); n++ )
356 aList.GetObject(n)->Call( this );
359 //*****************************************************************************************************************
360 // public method
361 //*****************************************************************************************************************
362 void SvtMenuOptions_Impl::Commit()
364 // Get names of supported properties, create a list for values and copy current values to it.
365 Sequence< OUString > seqNames = impl_GetPropertyNames();
366 sal_Int32 nCount = seqNames.getLength();
367 Sequence< Any > seqValues ( nCount );
368 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
370 switch( nProperty )
372 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES : {
373 seqValues[nProperty] <<= m_bDontHideDisabledEntries;
375 break;
377 case PROPERTYHANDLE_FOLLOWMOUSE : {
378 seqValues[nProperty] <<= m_bFollowMouse;
380 break;
381 //Output cache of current setting as possibly modified by System Theme for older version
382 case PROPERTYHANDLE_SHOWICONSINMENUES : {
383 seqValues[nProperty] <<=(sal_Bool)(Application::GetSettings().GetStyleSettings().GetUseImagesInMenus());
385 break;
386 case PROPERTYHANDLE_SYSTEMICONSINMENUES : {
387 seqValues[nProperty] <<= (m_nMenuIcons == 2 ? sal_True : sal_False) ;
389 break;
392 // Set properties in configuration.
393 PutProperties( seqNames, seqValues );
396 //*****************************************************************************************************************
397 // private method
398 //*****************************************************************************************************************
399 Sequence< OUString > SvtMenuOptions_Impl::impl_GetPropertyNames()
401 // Build static list of configuration key names.
402 static const OUString pProperties[] =
404 PROPERTYNAME_DONTHIDEDISABLEDENTRIES ,
405 PROPERTYNAME_FOLLOWMOUSE ,
406 PROPERTYNAME_SHOWICONSINMENUES ,
407 PROPERTYNAME_SYSTEMICONSINMENUES
409 // Initialize return sequence with these list ...
410 static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
411 // ... and return it.
412 return seqPropertyNames;
415 void SvtMenuOptions_Impl::AddListener( const Link& rLink )
417 aList.Insert( new Link( rLink ) );
420 void SvtMenuOptions_Impl::RemoveListener( const Link& rLink )
422 for ( USHORT n=0; n<aList.Count(); n++ )
424 if ( (*aList.GetObject(n) ) == rLink )
426 delete aList.Remove(n);
427 break;
432 //*****************************************************************************************************************
433 // initialize static member
434 // DON'T DO IT IN YOUR HEADER!
435 // see definition for further informations
436 //*****************************************************************************************************************
437 SvtMenuOptions_Impl* SvtMenuOptions::m_pDataContainer = NULL ;
438 sal_Int32 SvtMenuOptions::m_nRefCount = 0 ;
440 //*****************************************************************************************************************
441 // constructor
442 //*****************************************************************************************************************
443 SvtMenuOptions::SvtMenuOptions()
445 // Global access, must be guarded (multithreading!).
446 MutexGuard aGuard( GetOwnStaticMutex() );
447 // Increase ouer refcount ...
448 ++m_nRefCount;
449 // ... and initialize ouer data container only if it not already!
450 if( m_pDataContainer == NULL )
452 RTL_LOGFILE_CONTEXT(aLog, "svtools ( ??? ) ::SvtMenuOptions_Impl::ctor()");
453 m_pDataContainer = new SvtMenuOptions_Impl();
455 ItemHolder1::holdConfigItem(E_MENUOPTIONS);
459 //*****************************************************************************************************************
460 // destructor
461 //*****************************************************************************************************************
462 SvtMenuOptions::~SvtMenuOptions()
464 // Global access, must be guarded (multithreading!)
465 MutexGuard aGuard( GetOwnStaticMutex() );
466 // Decrease ouer refcount.
467 --m_nRefCount;
468 // If last instance was deleted ...
469 // we must destroy ouer static data container!
470 if( m_nRefCount <= 0 )
472 delete m_pDataContainer;
473 m_pDataContainer = NULL;
477 //*****************************************************************************************************************
478 // public method
479 //*****************************************************************************************************************
480 sal_Bool SvtMenuOptions::IsEntryHidingEnabled() const
482 MutexGuard aGuard( GetOwnStaticMutex() );
483 return m_pDataContainer->IsEntryHidingEnabled();
486 //*****************************************************************************************************************
487 // public method
488 //*****************************************************************************************************************
489 sal_Bool SvtMenuOptions::IsFollowMouseEnabled() const
491 MutexGuard aGuard( GetOwnStaticMutex() );
492 return m_pDataContainer->IsFollowMouseEnabled();
495 //*****************************************************************************************************************
496 // public method
497 //*****************************************************************************************************************
498 void SvtMenuOptions::SetEntryHidingState( sal_Bool bState )
500 MutexGuard aGuard( GetOwnStaticMutex() );
501 m_pDataContainer->SetEntryHidingState( bState );
504 //*****************************************************************************************************************
505 // public method
506 //*****************************************************************************************************************
507 void SvtMenuOptions::SetFollowMouseState( sal_Bool bState )
509 MutexGuard aGuard( GetOwnStaticMutex() );
510 m_pDataContainer->SetFollowMouseState( bState );
513 //*****************************************************************************************************************
514 // public method
515 //*****************************************************************************************************************
516 sal_Int16 SvtMenuOptions::GetMenuIconsState() const
518 MutexGuard aGuard( GetOwnStaticMutex() );
519 return m_pDataContainer->GetMenuIconsState();
522 //*****************************************************************************************************************
523 // public method
524 //*****************************************************************************************************************
525 void SvtMenuOptions::SetMenuIconsState( sal_Int16 bState )
527 MutexGuard aGuard( GetOwnStaticMutex() );
528 m_pDataContainer->SetMenuIconsState( bState );
531 //*****************************************************************************************************************
532 // private method
533 //*****************************************************************************************************************
534 Mutex& SvtMenuOptions::GetOwnStaticMutex()
536 // Initialize static mutex only for one time!
537 static Mutex* pMutex = NULL;
538 // If these method first called (Mutex not already exist!) ...
539 if( pMutex == NULL )
541 // ... we must create a new one. Protect follow code with the global mutex -
542 // It must be - we create a static variable!
543 MutexGuard aGuard( Mutex::getGlobalMutex() );
544 // We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
545 if( pMutex == NULL )
547 // Create the new mutex and set it for return on static variable.
548 static Mutex aMutex;
549 pMutex = &aMutex;
552 // Return new created or already existing mutex object.
553 return *pMutex;
556 void SvtMenuOptions::AddListener( const Link& rLink )
558 m_pDataContainer->AddListener( rLink );
561 void SvtMenuOptions::RemoveListener( const Link& rLink )
563 m_pDataContainer->RemoveListener( rLink );