1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <svtools/menuoptions.hxx>
22 #include <unotools/configmgr.hxx>
23 #include <unotools/configitem.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <vcl/svapp.hxx>
29 #include <rtl/logfile.hxx>
30 #include "itemholder2.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
38 using namespace ::utl
;
39 using namespace ::rtl
;
40 using namespace ::osl
;
41 using namespace ::com::sun::star::uno
;
43 #define ROOTNODE_MENU OUString("Office.Common/View/Menu" )
44 #define DEFAULT_DONTHIDEDISABLEDENTRIES sal_False
45 #define DEFAULT_FOLLOWMOUSE sal_True
46 #define DEFAULT_MENUICONS 2
48 #define PROPERTYNAME_DONTHIDEDISABLEDENTRIES OUString("DontHideDisabledEntry" )
49 #define PROPERTYNAME_FOLLOWMOUSE OUString("FollowMouse" )
50 #define PROPERTYNAME_SHOWICONSINMENUES OUString("ShowIconsInMenues" )
51 #define PROPERTYNAME_SYSTEMICONSINMENUES OUString("IsSystemIconsInMenus" )
53 #define PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES 0
54 #define PROPERTYHANDLE_FOLLOWMOUSE 1
55 #define PROPERTYHANDLE_SHOWICONSINMENUES 2
56 #define PROPERTYHANDLE_SYSTEMICONSINMENUES 3
58 #define PROPERTYCOUNT 4
60 #include <tools/link.hxx>
62 //_________________________________________________________________________________________________________________
63 // private declarations!
64 //_________________________________________________________________________________________________________________
66 class SvtMenuOptions_Impl
: public ConfigItem
68 //-------------------------------------------------------------------------------------------------------------
70 //-------------------------------------------------------------------------------------------------------------
73 ::std::list
<Link
> aList
;
74 sal_Bool m_bDontHideDisabledEntries
; /// cache "DontHideDisabledEntries" of Menu section
75 sal_Bool m_bFollowMouse
; /// cache "FollowMouse" of Menu section
76 sal_Int16 m_nMenuIcons
; /// cache "MenuIcons" of Menu section
78 //-------------------------------------------------------------------------------------------------------------
80 //-------------------------------------------------------------------------------------------------------------
84 //---------------------------------------------------------------------------------------------------------
85 // constructor / destructor
86 //---------------------------------------------------------------------------------------------------------
88 SvtMenuOptions_Impl();
89 ~SvtMenuOptions_Impl();
91 void AddListenerLink( const Link
& rLink
);
92 void RemoveListenerLink( const Link
& rLink
);
94 //---------------------------------------------------------------------------------------------------------
95 // overloaded methods of baseclass
96 //---------------------------------------------------------------------------------------------------------
98 /*-****************************************************************************************************//**
99 @short called for notify of configmanager
100 @descr These method is called from the ConfigManager before application ends or from the
101 PropertyChangeListener if the sub tree broadcasts changes. You must update your
104 @seealso baseclass ConfigItem
106 @param "seqPropertyNames" is the list of properties which should be updated.
110 *//*-*****************************************************************************************************/
112 virtual void Notify( const Sequence
< OUString
>& seqPropertyNames
);
114 /*-****************************************************************************************************//**
115 @short write changes to configuration
116 @descr These method writes the changed values into the sub tree
117 and should always called in our destructor to guarantee consistency of config data.
119 @seealso baseclass ConfigItem
125 *//*-*****************************************************************************************************/
127 virtual void Commit();
129 //---------------------------------------------------------------------------------------------------------
131 //---------------------------------------------------------------------------------------------------------
133 /*-****************************************************************************************************//**
134 @short access method to get internal values
135 @descr These methods give us a chance to regulate access to our internal values.
136 It's not used in the moment - but it's possible for the future!
144 *//*-*****************************************************************************************************/
146 sal_Bool
IsEntryHidingEnabled() const
147 { return m_bDontHideDisabledEntries
; }
149 sal_Bool
IsFollowMouseEnabled() const
150 { return m_bFollowMouse
; }
152 sal_Int16
GetMenuIconsState() const
153 { return m_nMenuIcons
; }
155 void SetEntryHidingState ( sal_Bool bState
)
157 m_bDontHideDisabledEntries
= bState
;
159 for ( ::std::list
<Link
>::const_iterator iter
= aList
.begin(); iter
!= aList
.end(); ++iter
)
164 void SetFollowMouseState ( sal_Bool bState
)
166 m_bFollowMouse
= bState
;
168 for ( ::std::list
<Link
>::const_iterator iter
= aList
.begin(); iter
!= aList
.end(); ++iter
)
173 void SetMenuIconsState ( sal_Int16 nState
)
175 m_nMenuIcons
= nState
;
177 for ( ::std::list
<Link
>::const_iterator iter
= aList
.begin(); iter
!= aList
.end(); ++iter
)
182 //-------------------------------------------------------------------------------------------------------------
184 //-------------------------------------------------------------------------------------------------------------
188 /*-****************************************************************************************************//**
189 @short return list of fix key names of our configuration management which represent our module tree
190 @descr These methods return a static const list of key names. We need it to get needed values from our
191 configuration management.
196 @return A list of needed configuration keys is returned.
199 *//*-*****************************************************************************************************/
201 static Sequence
< OUString
> impl_GetPropertyNames();
204 //*****************************************************************************************************************
206 //*****************************************************************************************************************
207 SvtMenuOptions_Impl::SvtMenuOptions_Impl()
208 // Init baseclasses first
209 : ConfigItem ( ROOTNODE_MENU
)
211 , m_bDontHideDisabledEntries ( DEFAULT_DONTHIDEDISABLEDENTRIES
)
212 , m_bFollowMouse ( DEFAULT_FOLLOWMOUSE
)
213 , m_nMenuIcons ( DEFAULT_MENUICONS
)
215 // Use our static list of configuration keys to get his values.
216 Sequence
< OUString
> seqNames
= impl_GetPropertyNames();
217 Sequence
< Any
> seqValues
= GetProperties( seqNames
) ;
219 // Safe impossible cases.
220 // We need values from ALL configuration keys.
221 // Follow assignment use order of values in relation to our list of key names!
222 DBG_ASSERT( !(seqNames
.getLength()!=seqValues
.getLength()), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nI miss some values of configuration keys!\n" );
224 sal_Bool bMenuIcons
= sal_True
;
225 sal_Bool bSystemMenuIcons
= sal_True
;
226 if (m_nMenuIcons
== 2)
227 bMenuIcons
= (sal_Bool
)(Application::GetSettings().GetStyleSettings().GetPreferredUseImagesInMenus());
230 bSystemMenuIcons
= sal_False
;
231 bMenuIcons
= m_nMenuIcons
? sal_True
: sal_False
;
234 // Copy values from list in right order to our internal member.
235 sal_Int32 nPropertyCount
= seqValues
.getLength() ;
236 sal_Int32 nProperty
= 0 ;
237 for( nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
239 // Safe impossible cases.
240 // Check any for valid value.
241 DBG_ASSERT( !(seqValues
[nProperty
].hasValue()==sal_False
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nInvalid property value for property detected!\n" );
243 if (!seqValues
[nProperty
].hasValue())
248 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES
: {
249 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
250 seqValues
[nProperty
] >>= m_bDontHideDisabledEntries
;
254 case PROPERTYHANDLE_FOLLOWMOUSE
: {
255 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
256 seqValues
[nProperty
] >>= m_bFollowMouse
;
259 case PROPERTYHANDLE_SHOWICONSINMENUES
: {
260 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
261 seqValues
[nProperty
] >>= bMenuIcons
;
264 case PROPERTYHANDLE_SYSTEMICONSINMENUES
: {
265 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
266 seqValues
[nProperty
] >>= bSystemMenuIcons
;
272 m_nMenuIcons
= bSystemMenuIcons
? 2 : bMenuIcons
;
274 EnableNotification( seqNames
);
277 //*****************************************************************************************************************
279 //*****************************************************************************************************************
280 SvtMenuOptions_Impl::~SvtMenuOptions_Impl()
282 // Flush data to configuration!
283 // User has no chance to do that.
284 if( IsModified() == sal_True
)
290 //*****************************************************************************************************************
292 //*****************************************************************************************************************
293 void SvtMenuOptions_Impl::Notify( const Sequence
< OUString
>& seqPropertyNames
)
295 // Use given list of updated properties to get his values from configuration directly!
296 Sequence
< Any
> seqValues
= GetProperties( seqPropertyNames
);
297 // Safe impossible cases.
298 // We need values from ALL notified configuration keys.
299 DBG_ASSERT( !(seqPropertyNames
.getLength()!=seqValues
.getLength()), "SvtMenuOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
301 sal_Bool bMenuSettingsChanged
= sal_False
;
302 sal_Bool bMenuIcons
= sal_True
;
303 sal_Bool bSystemMenuIcons
= sal_True
;
304 if (m_nMenuIcons
== 2)
305 bMenuIcons
= (sal_Bool
)(Application::GetSettings().GetStyleSettings().GetUseImagesInMenus());
308 bSystemMenuIcons
= sal_False
;
309 bMenuIcons
= m_nMenuIcons
? sal_True
: sal_False
;
312 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
313 sal_Int32 nCount
= seqPropertyNames
.getLength();
314 for( sal_Int32 nProperty
=0; nProperty
<nCount
; ++nProperty
)
316 if( seqPropertyNames
[nProperty
] == PROPERTYNAME_DONTHIDEDISABLEDENTRIES
)
318 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
319 seqValues
[nProperty
] >>= m_bDontHideDisabledEntries
;
321 else if( seqPropertyNames
[nProperty
] == PROPERTYNAME_FOLLOWMOUSE
)
323 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
324 seqValues
[nProperty
] >>= m_bFollowMouse
;
326 else if( seqPropertyNames
[nProperty
] == PROPERTYNAME_SHOWICONSINMENUES
)
328 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
329 bMenuSettingsChanged
|= seqValues
[nProperty
] >>= bMenuIcons
;
331 else if( seqPropertyNames
[nProperty
] == PROPERTYNAME_SYSTEMICONSINMENUES
)
333 DBG_ASSERT(!(seqValues
[nProperty
].getValueTypeClass()!=TypeClass_BOOLEAN
), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
334 bMenuSettingsChanged
|= seqValues
[nProperty
] >>= bSystemMenuIcons
;
337 #if OSL_DEBUG_LEVEL > 1
338 else DBG_ASSERT( sal_False
, "SvtMenuOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
342 if ( bMenuSettingsChanged
)
343 m_nMenuIcons
= bSystemMenuIcons
? 2 : bMenuIcons
;
345 for ( ::std::list
<Link
>::const_iterator iter
= aList
.begin(); iter
!= aList
.end(); ++iter
)
349 //*****************************************************************************************************************
351 //*****************************************************************************************************************
352 void SvtMenuOptions_Impl::Commit()
354 // Get names of supported properties, create a list for values and copy current values to it.
355 Sequence
< OUString
> seqNames
= impl_GetPropertyNames();
356 sal_Int32 nCount
= seqNames
.getLength();
357 Sequence
< Any
> seqValues ( nCount
);
358 for( sal_Int32 nProperty
=0; nProperty
<nCount
; ++nProperty
)
362 case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES
: {
363 seqValues
[nProperty
] <<= m_bDontHideDisabledEntries
;
367 case PROPERTYHANDLE_FOLLOWMOUSE
: {
368 seqValues
[nProperty
] <<= m_bFollowMouse
;
371 //Output cache of current setting as possibly modified by System Theme for older version
372 case PROPERTYHANDLE_SHOWICONSINMENUES
: {
373 sal_Bool bValue
= (sal_Bool
)(Application::GetSettings().GetStyleSettings().GetUseImagesInMenus());
374 seqValues
[nProperty
] <<= bValue
;
377 case PROPERTYHANDLE_SYSTEMICONSINMENUES
: {
378 sal_Bool bValue
= (m_nMenuIcons
== 2 ? sal_True
: sal_False
) ;
379 seqValues
[nProperty
] <<= bValue
;
384 // Set properties in configuration.
385 PutProperties( seqNames
, seqValues
);
388 //*****************************************************************************************************************
390 //*****************************************************************************************************************
391 Sequence
< OUString
> SvtMenuOptions_Impl::impl_GetPropertyNames()
393 // Build static list of configuration key names.
394 static const OUString pProperties
[] =
396 PROPERTYNAME_DONTHIDEDISABLEDENTRIES
,
397 PROPERTYNAME_FOLLOWMOUSE
,
398 PROPERTYNAME_SHOWICONSINMENUES
,
399 PROPERTYNAME_SYSTEMICONSINMENUES
401 // Initialize return sequence with these list ...
402 static const Sequence
< OUString
> seqPropertyNames( pProperties
, PROPERTYCOUNT
);
403 // ... and return it.
404 return seqPropertyNames
;
407 void SvtMenuOptions_Impl::AddListenerLink( const Link
& rLink
)
409 aList
.push_back( rLink
);
412 void SvtMenuOptions_Impl::RemoveListenerLink( const Link
& rLink
)
414 for ( ::std::list
<Link
>::iterator iter
= aList
.begin(); iter
!= aList
.end(); ++iter
)
416 if ( *iter
== rLink
)
424 //*****************************************************************************************************************
425 // initialize static member
426 // DON'T DO IT IN YOUR HEADER!
427 // see definition for further information
428 //*****************************************************************************************************************
429 SvtMenuOptions_Impl
* SvtMenuOptions::m_pDataContainer
= NULL
;
430 sal_Int32
SvtMenuOptions::m_nRefCount
= 0 ;
432 //*****************************************************************************************************************
434 //*****************************************************************************************************************
435 SvtMenuOptions::SvtMenuOptions()
437 // Global access, must be guarded (multithreading!).
438 MutexGuard
aGuard( GetOwnStaticMutex() );
439 // Increase our refcount ...
441 // ... and initialize our data container only if it not already!
442 if( m_pDataContainer
== NULL
)
444 RTL_LOGFILE_CONTEXT(aLog
, "svtools ( ??? ) ::SvtMenuOptions_Impl::ctor()");
445 m_pDataContainer
= new SvtMenuOptions_Impl();
447 svtools::ItemHolder2::holdConfigItem(E_MENUOPTIONS
);
451 //*****************************************************************************************************************
453 //*****************************************************************************************************************
454 SvtMenuOptions::~SvtMenuOptions()
456 // Global access, must be guarded (multithreading!)
457 MutexGuard
aGuard( GetOwnStaticMutex() );
458 // Decrease our refcount.
460 // If last instance was deleted ...
461 // we must destroy our static data container!
462 if( m_nRefCount
<= 0 )
464 delete m_pDataContainer
;
465 m_pDataContainer
= NULL
;
469 //*****************************************************************************************************************
471 //*****************************************************************************************************************
472 sal_Bool
SvtMenuOptions::IsEntryHidingEnabled() const
474 MutexGuard
aGuard( GetOwnStaticMutex() );
475 return m_pDataContainer
->IsEntryHidingEnabled();
478 //*****************************************************************************************************************
480 //*****************************************************************************************************************
481 sal_Int16
SvtMenuOptions::GetMenuIconsState() const
483 MutexGuard
aGuard( GetOwnStaticMutex() );
484 return m_pDataContainer
->GetMenuIconsState();
487 //*****************************************************************************************************************
489 //*****************************************************************************************************************
490 void SvtMenuOptions::SetMenuIconsState( sal_Int16 bState
)
492 MutexGuard
aGuard( GetOwnStaticMutex() );
493 m_pDataContainer
->SetMenuIconsState( bState
);
496 //*****************************************************************************************************************
498 //*****************************************************************************************************************
499 Mutex
& SvtMenuOptions::GetOwnStaticMutex()
501 // Initialize static mutex only for one time!
502 static Mutex
* pMutex
= NULL
;
503 // If these method first called (Mutex not already exist!) ...
506 // ... we must create a new one. Protect follow code with the global mutex -
507 // It must be - we create a static variable!
508 MutexGuard
aGuard( Mutex::getGlobalMutex() );
509 // We must check our pointer again - because it can be that another instance of our class will be faster than these!
512 // Create the new mutex and set it for return on static variable.
517 // Return new created or already existing mutex object.
521 void SvtMenuOptions::AddListenerLink( const Link
& rLink
)
523 m_pDataContainer
->AddListenerLink( rLink
);
526 void SvtMenuOptions::RemoveListenerLink( const Link
& rLink
)
528 m_pDataContainer
->RemoveListenerLink( rLink
);
531 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */