1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_reportdesign.hxx"
30 
31 #include <FormattedFieldBeautifier.hxx>
32 
33 #include <com/sun/star/report/XFormattedField.hpp>
34 #include <com/sun/star/report/XImageControl.hpp>
35 #include <com/sun/star/awt/XVclWindowPeer.hpp>
36 #include <com/sun/star/awt/InvalidateStyle.hpp>
37 
38 #include <RptObject.hxx>
39 #include <RptModel.hxx>
40 #include <RptPage.hxx>
41 #include <ViewsWindow.hxx>
42 #include <ReportSection.hxx>
43 #include <ReportController.hxx>
44 #include <uistrings.hrc>
45 #include <reportformula.hxx>
46 #include <toolkit/helper/property.hxx>
47 
48 #include <svtools/extcolorcfg.hxx>
49 #include <unotools/confignode.hxx>
50 
51 // DBG_*
52 #include <tools/debug.hxx>
53 // DBG_UNHANDLED_EXCEPTION
54 #include <tools/diagnose_ex.h>
55 
56 namespace rptui
57 {
58 	using namespace ::com::sun::star;
59 
60     DBG_NAME(rpt_FormattedFieldBeautifier)
61 
62 	//--------------------------------------------------------------------
63     FormattedFieldBeautifier::FormattedFieldBeautifier(const OReportController& _aController)
64         :m_rReportController(_aController)
65         ,m_nTextColor(-1)
66     {
67         DBG_CTOR(rpt_FormattedFieldBeautifier, NULL);
68     }
69 
70 	//--------------------------------------------------------------------
71     sal_Int32 FormattedFieldBeautifier::getTextColor()
72     {
73         if (m_nTextColor == -1)
74         {
75             svtools::ExtendedColorConfig aConfig;
76             m_nTextColor = aConfig.GetColorValue(CFG_REPORTDESIGNER, DBTEXTBOXBOUNDCONTENT).getColor();
77         }
78         return m_nTextColor;
79     }
80 
81 	//--------------------------------------------------------------------
82     FormattedFieldBeautifier::~FormattedFieldBeautifier()
83     {
84         DBG_DTOR(rpt_FormattedFieldBeautifier,NULL);
85     }
86 
87     // -----------------------------------------------------------------------------
88     void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference< uno::XInterface >& _rxComponent )
89     {
90         ::rtl::OUString sDataField;
91 
92         try
93         {
94             uno::Reference< report::XFormattedField > xControlModel( _rxComponent, uno::UNO_QUERY );
95             if ( xControlModel.is() )
96             {
97                 sDataField = xControlModel->getDataField();
98 
99                 if ( sDataField.getLength() )
100                 {
101                     ReportFormula aFormula( sDataField );
102                     bool bSet = true;
103                     if ( aFormula.getType() == ReportFormula::Field )
104                     {
105                         const ::rtl::OUString sColumnName = aFormula.getFieldName();
106 		                ::rtl::OUString sLabel = m_rReportController.getColumnLabel_throw(sColumnName);
107                         if ( sLabel.getLength() )
108                         {
109                             ::rtl::OUStringBuffer aBuffer;
110                             aBuffer.appendAscii( "=" );
111                             aBuffer.append( sLabel );
112                             sDataField = aBuffer.makeStringAndClear();
113                             bSet = false;
114                         }
115                     }
116                     if ( bSet )
117                         sDataField = aFormula.getEqualUndecoratedContent();
118                 }
119             }
120 
121             if ( xControlModel.is() )
122                 setPlaceholderText( getVclWindowPeer( xControlModel.get() ), sDataField );
123         }
124         catch (uno::Exception)
125         {
126     	    DBG_UNHANDLED_EXCEPTION();
127         }
128     }
129 
130     // -----------------------------------------------------------------------------
131     void FormattedFieldBeautifier::setPlaceholderText( const uno::Reference< awt::XVclWindowPeer >& _xVclWindowPeer, const ::rtl::OUString& _rText )
132     {
133         OSL_ENSURE( _xVclWindowPeer.is(), "FormattedFieldBeautifier::setPlaceholderText: invalid peer!" );
134         if ( !_xVclWindowPeer.is() )
135             throw uno::RuntimeException();
136 
137         // the actual text
138         _xVclWindowPeer->setProperty(PROPERTY_TEXT, uno::makeAny(_rText));
139         // the text color
140         _xVclWindowPeer->setProperty(PROPERTY_TEXTCOLOR, uno::makeAny(getTextColor()));
141         // font->italic
142         uno::Any aFontDescriptor = _xVclWindowPeer->getProperty(PROPERTY_FONTDESCRIPTOR);
143         awt::FontDescriptor aFontDescriptorStructure;
144         aFontDescriptor >>= aFontDescriptorStructure;
145         aFontDescriptorStructure.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
146         _xVclWindowPeer->setProperty(PROPERTY_FONTDESCRIPTOR, uno::makeAny(aFontDescriptorStructure));
147     }
148 
149     // -----------------------------------------------------------------------------
150     void FormattedFieldBeautifier::notifyPropertyChange( const beans::PropertyChangeEvent& _rEvent )
151     {
152         if  ( !_rEvent.PropertyName.equalsAscii( "DataField" ) )
153             // not interested in
154             return;
155 
156         setPlaceholderText( _rEvent.Source );
157     }
158 
159     // -----------------------------------------------------------------------------
160     void FormattedFieldBeautifier::handle( const uno::Reference< uno::XInterface >& _rxElement )
161     {
162         setPlaceholderText( _rxElement );
163     }
164 
165     // -----------------------------------------------------------------------------
166     void FormattedFieldBeautifier::notifyElementInserted( const uno::Reference< uno::XInterface >& _rxElement )
167     {
168         handle( _rxElement );
169     }
170 
171     // -----------------------------------------------------------------------------
172     uno::Reference<awt::XVclWindowPeer> FormattedFieldBeautifier::getVclWindowPeer(const uno::Reference< report::XReportComponent >& _xComponent) throw(uno::RuntimeException)
173     {
174         uno::Reference<awt::XVclWindowPeer> xVclWindowPeer;
175 
176         ::boost::shared_ptr<OReportModel> pModel = const_cast< OReportController& >( m_rReportController ).getSdrModel();
177 
178         uno::Reference<report::XSection> xSection(_xComponent->getSection());
179         if ( xSection.is() )
180         {
181             OReportPage *pPage = pModel->getPage(xSection);
182             sal_uLong nIndex = pPage->getIndexOf(_xComponent);
183             if (nIndex < pPage->GetObjCount() )
184             {
185                 SdrObject *pObject = pPage->GetObj(nIndex);
186                 OUnoObject* pUnoObj = dynamic_cast<OUnoObject*>(pObject);
187                 if ( pUnoObj ) // this doesn't need to be done for shapes
188                 {
189                     // Rectangle aRect = pUnoObj->GetCurrentBoundRect();
190                     ::boost::shared_ptr<OSectionWindow> pSectionWindow = m_rReportController.getSectionWindow(xSection);
191                     if (pSectionWindow != NULL)
192                     {
193                         OReportSection& aOutputDevice = pSectionWindow->getReportSection(); // OutputDevice
194                         OSectionView& aSdrView = aOutputDevice.getSectionView();            // SdrView
195                         uno::Reference<awt::XControl> xControl = pUnoObj->GetUnoControl(aSdrView, aOutputDevice);
196                         xVclWindowPeer = uno::Reference<awt::XVclWindowPeer>( xControl->getPeer(), uno::UNO_QUERY);
197                     }
198                 }
199             }
200         }
201         return xVclWindowPeer;
202     }
203 }
204