19e0e4191SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39e0e4191SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49e0e4191SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59e0e4191SAndrew Rist  * distributed with this work for additional information
69e0e4191SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79e0e4191SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89e0e4191SAndrew Rist  * "License"); you may not use this file except in compliance
99e0e4191SAndrew Rist  * with the License.  You may obtain a copy of the License at
109e0e4191SAndrew Rist  *
119e0e4191SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129e0e4191SAndrew Rist  *
139e0e4191SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149e0e4191SAndrew Rist  * software distributed under the License is distributed on an
159e0e4191SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169e0e4191SAndrew Rist  * KIND, either express or implied.  See the License for the
179e0e4191SAndrew Rist  * specific language governing permissions and limitations
189e0e4191SAndrew Rist  * under the License.
199e0e4191SAndrew Rist  *
209e0e4191SAndrew Rist  *************************************************************/
219e0e4191SAndrew Rist 
229e0e4191SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_reportdesign.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <FormattedFieldBeautifier.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/report/XFormattedField.hpp>
30cdf0e10cSrcweir #include <com/sun/star/report/XImageControl.hpp>
31cdf0e10cSrcweir #include <com/sun/star/awt/XVclWindowPeer.hpp>
32cdf0e10cSrcweir #include <com/sun/star/awt/InvalidateStyle.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <RptObject.hxx>
35cdf0e10cSrcweir #include <RptModel.hxx>
36cdf0e10cSrcweir #include <RptPage.hxx>
37cdf0e10cSrcweir #include <ViewsWindow.hxx>
38cdf0e10cSrcweir #include <ReportSection.hxx>
39cdf0e10cSrcweir #include <ReportController.hxx>
40cdf0e10cSrcweir #include <uistrings.hrc>
41cdf0e10cSrcweir #include <reportformula.hxx>
42cdf0e10cSrcweir #include <toolkit/helper/property.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <svtools/extcolorcfg.hxx>
45cdf0e10cSrcweir #include <unotools/confignode.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir // DBG_*
48cdf0e10cSrcweir #include <tools/debug.hxx>
49cdf0e10cSrcweir // DBG_UNHANDLED_EXCEPTION
50cdf0e10cSrcweir #include <tools/diagnose_ex.h>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace rptui
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	using namespace ::com::sun::star;
55cdf0e10cSrcweir 
DBG_NAME(rpt_FormattedFieldBeautifier)56cdf0e10cSrcweir     DBG_NAME(rpt_FormattedFieldBeautifier)
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	//--------------------------------------------------------------------
59cdf0e10cSrcweir     FormattedFieldBeautifier::FormattedFieldBeautifier(const OReportController& _aController)
60cdf0e10cSrcweir         :m_rReportController(_aController)
61cdf0e10cSrcweir         ,m_nTextColor(-1)
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         DBG_CTOR(rpt_FormattedFieldBeautifier, NULL);
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	//--------------------------------------------------------------------
getTextColor()67cdf0e10cSrcweir     sal_Int32 FormattedFieldBeautifier::getTextColor()
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         if (m_nTextColor == -1)
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             svtools::ExtendedColorConfig aConfig;
72cdf0e10cSrcweir             m_nTextColor = aConfig.GetColorValue(CFG_REPORTDESIGNER, DBTEXTBOXBOUNDCONTENT).getColor();
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         return m_nTextColor;
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	//--------------------------------------------------------------------
~FormattedFieldBeautifier()78cdf0e10cSrcweir     FormattedFieldBeautifier::~FormattedFieldBeautifier()
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         DBG_DTOR(rpt_FormattedFieldBeautifier,NULL);
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     // -----------------------------------------------------------------------------
setPlaceholderText(const uno::Reference<uno::XInterface> & _rxComponent)84cdf0e10cSrcweir     void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference< uno::XInterface >& _rxComponent )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         ::rtl::OUString sDataField;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         try
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             uno::Reference< report::XFormattedField > xControlModel( _rxComponent, uno::UNO_QUERY );
91cdf0e10cSrcweir             if ( xControlModel.is() )
92cdf0e10cSrcweir             {
93cdf0e10cSrcweir                 sDataField = xControlModel->getDataField();
94cdf0e10cSrcweir 
95cdf0e10cSrcweir                 if ( sDataField.getLength() )
96cdf0e10cSrcweir                 {
97cdf0e10cSrcweir                     ReportFormula aFormula( sDataField );
98cdf0e10cSrcweir                     bool bSet = true;
99cdf0e10cSrcweir                     if ( aFormula.getType() == ReportFormula::Field )
100cdf0e10cSrcweir                     {
101cdf0e10cSrcweir                         const ::rtl::OUString sColumnName = aFormula.getFieldName();
102cdf0e10cSrcweir 		                ::rtl::OUString sLabel = m_rReportController.getColumnLabel_throw(sColumnName);
103cdf0e10cSrcweir                         if ( sLabel.getLength() )
104cdf0e10cSrcweir                         {
105cdf0e10cSrcweir                             ::rtl::OUStringBuffer aBuffer;
106cdf0e10cSrcweir                             aBuffer.appendAscii( "=" );
107cdf0e10cSrcweir                             aBuffer.append( sLabel );
108cdf0e10cSrcweir                             sDataField = aBuffer.makeStringAndClear();
109cdf0e10cSrcweir                             bSet = false;
110cdf0e10cSrcweir                         }
111cdf0e10cSrcweir                     }
112cdf0e10cSrcweir                     if ( bSet )
113cdf0e10cSrcweir                         sDataField = aFormula.getEqualUndecoratedContent();
114cdf0e10cSrcweir                 }
115cdf0e10cSrcweir             }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             if ( xControlModel.is() )
118cdf0e10cSrcweir                 setPlaceholderText( getVclWindowPeer( xControlModel.get() ), sDataField );
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir         catch (uno::Exception)
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir     	    DBG_UNHANDLED_EXCEPTION();
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     // -----------------------------------------------------------------------------
setPlaceholderText(const uno::Reference<awt::XVclWindowPeer> & _xVclWindowPeer,const::rtl::OUString & _rText)127cdf0e10cSrcweir     void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference< awt::XVclWindowPeer >& _xVclWindowPeer, const ::rtl::OUString& _rText )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         OSL_ENSURE( _xVclWindowPeer.is(), "FormattedFieldBeautifier::setPlaceholderText: invalid peer!" );
130cdf0e10cSrcweir         if ( !_xVclWindowPeer.is() )
131cdf0e10cSrcweir             throw uno::RuntimeException();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         // the actual text
134cdf0e10cSrcweir         _xVclWindowPeer->setProperty(PROPERTY_TEXT, uno::makeAny(_rText));
135cdf0e10cSrcweir         // the text color
136cdf0e10cSrcweir         _xVclWindowPeer->setProperty(PROPERTY_TEXTCOLOR, uno::makeAny(getTextColor()));
137cdf0e10cSrcweir         // font->italic
138cdf0e10cSrcweir         uno::Any aFontDescriptor = _xVclWindowPeer->getProperty(PROPERTY_FONTDESCRIPTOR);
139cdf0e10cSrcweir         awt::FontDescriptor aFontDescriptorStructure;
140cdf0e10cSrcweir         aFontDescriptor >>= aFontDescriptorStructure;
141cdf0e10cSrcweir         aFontDescriptorStructure.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
142cdf0e10cSrcweir         _xVclWindowPeer->setProperty(PROPERTY_FONTDESCRIPTOR, uno::makeAny(aFontDescriptorStructure));
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     // -----------------------------------------------------------------------------
notifyPropertyChange(const beans::PropertyChangeEvent & _rEvent)146cdf0e10cSrcweir     void FormattedFieldBeautifier::notifyPropertyChange( const beans::PropertyChangeEvent& _rEvent )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         if  ( !_rEvent.PropertyName.equalsAscii( "DataField" ) )
149cdf0e10cSrcweir             // not interested in
150cdf0e10cSrcweir             return;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         setPlaceholderText( _rEvent.Source );
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     // -----------------------------------------------------------------------------
handle(const uno::Reference<uno::XInterface> & _rxElement)156cdf0e10cSrcweir     void FormattedFieldBeautifier::handle( const uno::Reference< uno::XInterface >& _rxElement )
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         setPlaceholderText( _rxElement );
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     // -----------------------------------------------------------------------------
notifyElementInserted(const uno::Reference<uno::XInterface> & _rxElement)162cdf0e10cSrcweir     void FormattedFieldBeautifier::notifyElementInserted( const uno::Reference< uno::XInterface >& _rxElement )
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         handle( _rxElement );
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     // -----------------------------------------------------------------------------
getVclWindowPeer(const uno::Reference<report::XReportComponent> & _xComponent)168cdf0e10cSrcweir     uno::Reference<awt::XVclWindowPeer> FormattedFieldBeautifier::getVclWindowPeer(const uno::Reference< report::XReportComponent >& _xComponent) throw(uno::RuntimeException)
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         uno::Reference<awt::XVclWindowPeer> xVclWindowPeer;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         ::boost::shared_ptr<OReportModel> pModel = const_cast< OReportController& >( m_rReportController ).getSdrModel();
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         uno::Reference<report::XSection> xSection(_xComponent->getSection());
175cdf0e10cSrcweir         if ( xSection.is() )
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             OReportPage *pPage = pModel->getPage(xSection);
178cdf0e10cSrcweir             sal_uLong nIndex = pPage->getIndexOf(_xComponent);
179cdf0e10cSrcweir             if (nIndex < pPage->GetObjCount() )
180cdf0e10cSrcweir             {
181cdf0e10cSrcweir                 SdrObject *pObject = pPage->GetObj(nIndex);
182cdf0e10cSrcweir                 OUnoObject* pUnoObj = dynamic_cast<OUnoObject*>(pObject);
183cdf0e10cSrcweir                 if ( pUnoObj ) // this doesn't need to be done for shapes
184cdf0e10cSrcweir                 {
185cdf0e10cSrcweir                     // Rectangle aRect = pUnoObj->GetCurrentBoundRect();
186cdf0e10cSrcweir                     ::boost::shared_ptr<OSectionWindow> pSectionWindow = m_rReportController.getSectionWindow(xSection);
187*b862c97cSHerbert Dürr                     if( bool(pSectionWindow))
188cdf0e10cSrcweir                     {
189cdf0e10cSrcweir                         OReportSection& aOutputDevice = pSectionWindow->getReportSection(); // OutputDevice
190cdf0e10cSrcweir                         OSectionView& aSdrView = aOutputDevice.getSectionView();            // SdrView
191cdf0e10cSrcweir                         uno::Reference<awt::XControl> xControl = pUnoObj->GetUnoControl(aSdrView, aOutputDevice);
192cdf0e10cSrcweir                         xVclWindowPeer = uno::Reference<awt::XVclWindowPeer>( xControl->getPeer(), uno::UNO_QUERY);
193cdf0e10cSrcweir                     }
194cdf0e10cSrcweir                 }
195cdf0e10cSrcweir             }
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir         return xVclWindowPeer;
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir }
200