/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
ORadioButtonModel::ORadioButtonModel(const Reference<XComponentContext>& _rxFactory)
:OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_RADIOBUTTON, FRM_SUN_CONTROL_RADIOBUTTON ) // use the old control name for compatibility reasons
{
void ORadioButtonModel::SetSiblingPropsTo(const OUString& rPropName, const Any& rValue)
{ // my name
OUString sMyGroup; if (hasProperty(PROPERTY_GROUP_NAME, this))
getPropertyValue(PROPERTY_GROUP_NAME) >>= sMyGroup; if (sMyGroup.isEmpty())
sMyGroup = m_aName;
// Iterate over my siblings
Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY); if (!xIndexAccess.is()) return;
Reference<XPropertySet> xMyProps = this;
OUString sCurrentGroup;
sal_Int32 nNumSiblings = xIndexAccess->getCount(); for (sal_Int32 i=0; i<nNumSiblings; ++i)
{
Reference<XPropertySet> xSiblingProperties(xIndexAccess->getByIndex(i), UNO_QUERY); if (!xSiblingProperties.is()) continue; if (xMyProps == xSiblingProperties) continue; // do not set myself
// Only if it's a RadioButton if (!hasProperty(PROPERTY_CLASSID, xSiblingProperties)) continue;
sal_Int16 nType = 0;
xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType; if (nType != FormComponentType::RADIOBUTTON) continue;
// The group association is attached to the name
sCurrentGroup = OGroupManager::GetGroupName( xSiblingProperties ); if (sCurrentGroup == sMyGroup)
xSiblingProperties->setPropertyValue(rPropName, rValue);
}
}
// if the label control changed ... if (nHandle == PROPERTY_ID_CONTROLLABEL)
{ // ... forward this to our siblings
SetSiblingPropsTo(PROPERTY_CONTROLLABEL, rValue);
}
// If the ControlSource property has changed ... if (nHandle == PROPERTY_ID_CONTROLSOURCE)
{ // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
SetSiblingPropsTo(PROPERTY_CONTROLSOURCE, rValue);
}
// The other way: if my name changes ... if (nHandle == PROPERTY_ID_NAME)
{
setControlSource();
}
if (nHandle != PROPERTY_ID_DEFAULT_STATE) return;
sal_Int16 nValue;
rValue >>= nValue; if (1 == nValue)
{ // Reset the 'default checked' for all Radios of the same group. // Because (as the Highlander already knew): "There can be only one"
Any aZero;
nValue = 0;
aZero <<= nValue;
SetSiblingPropsTo(PROPERTY_DEFAULT_STATE, aZero);
}
}
void ORadioButtonModel::setControlSource()
{
Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY); if (!xIndexAccess.is()) return;
OUString sName, sGroupName;
if (hasProperty(PROPERTY_GROUP_NAME, this))
getPropertyValue(PROPERTY_GROUP_NAME) >>= sGroupName;
getPropertyValue(PROPERTY_NAME) >>= sName;
Reference<XPropertySet> xMyProps = this; for (sal_Int32 i=0; i<xIndexAccess->getCount(); ++i)
{
Reference<XPropertySet> xSiblingProperties(xIndexAccess->getByIndex(i), UNO_QUERY); if (!xSiblingProperties.is()) continue;
if (xMyProps == xSiblingProperties) // Only if I didn't find myself continue;
sal_Int16 nType = 0;
xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType; if (nType != FormComponentType::RADIOBUTTON) // Only RadioButtons continue;
if ((sGroupName.isEmpty() && sSiblingGroupName.isEmpty() && // (no group name
sName == sSiblingName) || // names match) or
(!sGroupName.isEmpty() && !sSiblingGroupName.isEmpty() && // (have group name
sGroupName == sSiblingGroupName)) // they match)
{
setPropertyValue(PROPERTY_CONTROLSOURCE, xSiblingProperties->getPropertyValue(PROPERTY_CONTROLSOURCE)); break;
}
}
}
// Display default values after read if ( !getControlSource().isEmpty() ) // (not if we don't have a control source - the "State" property acts like it is persistent, then
resetNoBroadcast();
}
void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent& _rEvent)
{ if ( _rEvent.PropertyName == PROPERTY_STATE )
{ if ( _rEvent.NewValue == sal_Int16(1) )
{ // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
Any aZero;
aZero <<= sal_Int16(0);
SetSiblingPropsTo( PROPERTY_STATE, aZero );
}
} elseif ( _rEvent.PropertyName == PROPERTY_GROUP_NAME )
{
setControlSource(); // Can't call OReferenceValueComponent::_propertyChanged(), as it // doesn't know what to do with the GroupName property. return;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.