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 <FormattedFieldBeautifier.hxx>
23 #include <com/sun/star/report/XFormattedField.hpp>
24 #include <com/sun/star/awt/XVclWindowPeer.hpp>
26 #include <RptObject.hxx>
27 #include <RptModel.hxx>
28 #include <RptPage.hxx>
29 #include <ReportSection.hxx>
30 #include <ReportController.hxx>
31 #include <strings.hxx>
32 #include <reportformula.hxx>
34 #include <svtools/extcolorcfg.hxx>
36 // DBG_UNHANDLED_EXCEPTION
37 #include <tools/diagnose_ex.h>
41 using namespace ::com::sun::star
;
44 FormattedFieldBeautifier::FormattedFieldBeautifier(const OReportController
& _aController
)
45 :m_rReportController(_aController
)
46 ,m_nTextColor(COL_AUTO
)
51 Color
FormattedFieldBeautifier::getTextColor()
53 if (m_nTextColor
== COL_AUTO
)
55 svtools::ExtendedColorConfig aConfig
;
56 m_nTextColor
= aConfig
.GetColorValue(CFG_REPORTDESIGNER
, DBTEXTBOXBOUNDCONTENT
).getColor();
62 FormattedFieldBeautifier::~FormattedFieldBeautifier()
67 void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference
< uno::XInterface
>& _rxComponent
)
71 uno::Reference
< report::XFormattedField
> xControlModel( _rxComponent
, uno::UNO_QUERY
);
72 if ( xControlModel
.is() )
74 OUString sDataField
= xControlModel
->getDataField();
76 if ( !sDataField
.isEmpty() )
78 ReportFormula
aFormula( sDataField
);
80 if ( aFormula
.getType() == ReportFormula::Field
)
82 const OUString
& sColumnName
= aFormula
.getFieldName();
83 OUString sLabel
= m_rReportController
.getColumnLabel_throw(sColumnName
);
84 if ( !sLabel
.isEmpty() )
86 sDataField
= "=" + sLabel
;
91 sDataField
= aFormula
.getEqualUndecoratedContent();
94 setPlaceholderText( getVclWindowPeer( xControlModel
), sDataField
);
97 catch (const uno::Exception
&)
99 DBG_UNHANDLED_EXCEPTION("reportdesign");
104 void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference
< awt::XVclWindowPeer
>& _xVclWindowPeer
, const OUString
& _rText
)
106 OSL_ENSURE( _xVclWindowPeer
.is(), "FormattedFieldBeautifier::setPlaceholderText: invalid peer!" );
107 if ( !_xVclWindowPeer
.is() )
108 throw uno::RuntimeException();
111 _xVclWindowPeer
->setProperty(PROPERTY_TEXT
, uno::makeAny(_rText
));
113 _xVclWindowPeer
->setProperty(PROPERTY_TEXTCOLOR
, uno::makeAny(getTextColor()));
115 uno::Any aFontDescriptor
= _xVclWindowPeer
->getProperty(PROPERTY_FONTDESCRIPTOR
);
116 awt::FontDescriptor aFontDescriptorStructure
;
117 aFontDescriptor
>>= aFontDescriptorStructure
;
118 aFontDescriptorStructure
.Slant
= css::awt::FontSlant_ITALIC
;
119 _xVclWindowPeer
->setProperty(PROPERTY_FONTDESCRIPTOR
, uno::makeAny(aFontDescriptorStructure
));
123 void FormattedFieldBeautifier::notifyPropertyChange( const beans::PropertyChangeEvent
& _rEvent
)
125 if ( _rEvent
.PropertyName
!= "DataField" )
129 setPlaceholderText( _rEvent
.Source
);
133 void FormattedFieldBeautifier::handle( const uno::Reference
< uno::XInterface
>& _rxElement
)
135 setPlaceholderText( _rxElement
);
139 void FormattedFieldBeautifier::notifyElementInserted( const uno::Reference
< uno::XInterface
>& _rxElement
)
141 handle( _rxElement
);
145 uno::Reference
<awt::XVclWindowPeer
> FormattedFieldBeautifier::getVclWindowPeer(const uno::Reference
< report::XReportComponent
>& _xComponent
)
147 uno::Reference
<awt::XVclWindowPeer
> xVclWindowPeer
;
149 std::shared_ptr
<OReportModel
> pModel
= m_rReportController
.getSdrModel();
151 uno::Reference
<report::XSection
> xSection(_xComponent
->getSection());
154 OReportPage
*pPage
= pModel
->getPage(xSection
);
155 const size_t nIndex
= pPage
->getIndexOf(_xComponent
);
156 if (nIndex
< pPage
->GetObjCount() )
158 SdrObject
*pObject
= pPage
->GetObj(nIndex
);
159 OUnoObject
* pUnoObj
= dynamic_cast<OUnoObject
*>(pObject
);
160 if ( pUnoObj
) // this doesn't need to be done for shapes
162 OSectionWindow
* pSectionWindow
= m_rReportController
.getSectionWindow(xSection
);
163 if (pSectionWindow
!= nullptr)
165 OReportSection
& aOutputDevice
= pSectionWindow
->getReportSection(); // OutputDevice
166 OSectionView
& aSdrView
= aOutputDevice
.getSectionView(); // SdrView
167 uno::Reference
<awt::XControl
> xControl
= pUnoObj
->GetUnoControl(aSdrView
, *aOutputDevice
.GetOutDev());
168 xVclWindowPeer
.set( xControl
->getPeer(), uno::UNO_QUERY
);
173 return xVclWindowPeer
;
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */