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