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_extensions.hxx"
26 #include "browserline.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/inspection/PropertyLineElement.hpp>
30 #include <com/sun/star/graphic/XGraphicProvider.hpp>
31 /** === end UNO includes === **/
32 
33 #include <vcl/svapp.hxx>
34 #include <tools/debug.hxx>
35 #include <tools/diagnose_ex.h>
36 #include <tools/urlobj.hxx>
37 #include <toolkit/helper/vclunohelper.hxx>
38 
39 #include <comphelper/processfactory.hxx>
40 #include <comphelper/componentcontext.hxx>
41 
42 //............................................................................
43 namespace pcr
44 {
45 //............................................................................
46 
47     /** === begin UNO using === **/
48     using ::com::sun::star::uno::Reference;
49     using ::com::sun::star::inspection::XPropertyControl;
50     using ::com::sun::star::inspection::XPropertyControlContext;
51     using ::com::sun::star::uno::UNO_QUERY_THROW;
52     using ::com::sun::star::uno::Exception;
53     using ::com::sun::star::graphic::XGraphicProvider;
54     using ::com::sun::star::uno::Sequence;
55     using ::com::sun::star::beans::PropertyValue;
56     using ::com::sun::star::graphic::XGraphic;
57     /** === end UNO using === **/
58 
59     namespace PropertyLineElement = ::com::sun::star::inspection::PropertyLineElement;
60 
61     //==================================================================
62 	//= OBrowserLine
63 	//==================================================================
DBG_NAME(OBrowserLine)64 	DBG_NAME(OBrowserLine)
65 	//------------------------------------------------------------------
66 
67 	OBrowserLine::OBrowserLine( const ::rtl::OUString& _rEntryName, Window* pParent )
68 			:m_sEntryName( _rEntryName )
69             ,m_aFtTitle(pParent)
70             ,m_pControlWindow( NULL )
71             ,m_pBrowseButton(NULL)
72             ,m_pAdditionalBrowseButton( NULL )
73             ,m_pClickListener( NULL )
74 			,m_pTheParent(pParent)
75 			,m_nNameWidth(0)
76             ,m_nEnableFlags( 0xFFFF )
77             ,m_bIndentTitle( false )
78             ,m_bReadOnly( false )
79 	{
80 		DBG_CTOR(OBrowserLine,NULL);
81 		m_aFtTitle.Show();
82 	}
83 
84     //------------------------------------------------------------------
~OBrowserLine()85 	OBrowserLine::~OBrowserLine()
86 	{
87         implHideBrowseButton( true, false );
88         implHideBrowseButton( false, false );
89 
90 		DBG_DTOR(OBrowserLine,NULL);
91 	}
92 
93 	//------------------------------------------------------------------
IndentTitle(bool _bIndent)94 	void OBrowserLine::IndentTitle( bool _bIndent )
95     {
96         if ( m_bIndentTitle != _bIndent )
97         {
98             m_bIndentTitle = _bIndent;
99 		    impl_layoutComponents();
100         }
101     }
102 
103 	//------------------------------------------------------------------
SetComponentHelpIds(const rtl::OString & _rHelpId,const rtl::OString & _sPrimaryButtonId,const rtl::OString & _sSecondaryButtonId)104 	void OBrowserLine::SetComponentHelpIds( const rtl::OString& _rHelpId, const rtl::OString& _sPrimaryButtonId, const rtl::OString& _sSecondaryButtonId )
105 	{
106         if ( m_pControlWindow )
107             m_pControlWindow->SetHelpId( _rHelpId );
108 
109 		if ( m_pBrowseButton )
110 		{
111             m_pBrowseButton->SetHelpId( _rHelpId );
112 			m_pBrowseButton->SetUniqueId( _sPrimaryButtonId );
113 
114             if ( m_pAdditionalBrowseButton )
115 		    {
116 			    m_pAdditionalBrowseButton->SetHelpId( _rHelpId );
117 			    m_pAdditionalBrowseButton->SetUniqueId( _sSecondaryButtonId );
118 		    }
119 		}
120 	}
121 
122 	//------------------------------------------------------------------
setControl(const Reference<XPropertyControl> & _rxControl)123     void OBrowserLine::setControl( const Reference< XPropertyControl >& _rxControl )
124 	{
125         m_xControl = _rxControl;
126         m_pControlWindow = m_xControl.is() ? VCLUnoHelper::GetWindow( _rxControl->getControlWindow() ) : NULL;
127         DBG_ASSERT( m_pControlWindow, "OBrowserLine::setControl: setting NULL controls/windows is not allowed!" );
128 
129         if ( m_pControlWindow )
130         {
131             m_pControlWindow->SetParent( m_pTheParent );
132             m_pControlWindow->Show();
133         }
134 		impl_layoutComponents();
135 	}
136 
137 	//------------------------------------------------------------------
GetRefWindow()138 	Window* OBrowserLine::GetRefWindow()
139 	{
140 		Window* pRefWindow=&m_aFtTitle;
141 
142 		if(m_pBrowseButton)
143 		{
144 			pRefWindow=(Window*)m_pBrowseButton;
145 		}
146 		else if ( m_pControlWindow )
147 		{
148 			pRefWindow = m_pControlWindow;
149 		}
150 		return pRefWindow;
151 	}
152 
153 	//------------------------------------------------------------------
SetTabOrder(Window * pRefWindow,sal_uInt16 nFlags)154 	void OBrowserLine::SetTabOrder(Window* pRefWindow, sal_uInt16 nFlags )
155 	{
156 		m_aFtTitle.SetZOrder(pRefWindow,nFlags);
157 		if ( m_pControlWindow )
158 			m_pControlWindow->SetZOrder( (Window*)&m_aFtTitle, WINDOW_ZORDER_BEHIND );
159 
160 		if ( m_pBrowseButton )
161 			m_pBrowseButton->SetZOrder( m_pControlWindow, WINDOW_ZORDER_BEHIND );
162 
163         if ( m_pAdditionalBrowseButton )
164             m_pAdditionalBrowseButton->SetZOrder( m_pBrowseButton, WINDOW_ZORDER_BEHIND );
165 	}
166 
167 	//------------------------------------------------------------------
GrabFocus()168 	sal_Bool OBrowserLine::GrabFocus()
169 	{
170 		sal_Bool bRes=sal_False;
171 
172 		if ( m_pControlWindow && m_pControlWindow->IsEnabled() )
173 		{
174 			m_pControlWindow->GrabFocus();
175 			bRes = sal_True;
176 		}
177 		else if ( m_pAdditionalBrowseButton && m_pAdditionalBrowseButton->IsEnabled() )
178 		{
179 			m_pAdditionalBrowseButton->GrabFocus();
180 			bRes = sal_True;
181 		}
182 		else if ( m_pBrowseButton && m_pBrowseButton->IsEnabled() )
183 		{
184 			m_pBrowseButton->GrabFocus();
185 			bRes = sal_True;
186 		}
187 		return bRes;
188 	}
189 
190 	//------------------------------------------------------------------
SetPosSizePixel(Point _rPos,Size _rSize)191 	void OBrowserLine::SetPosSizePixel( Point _rPos, Size _rSize )
192 	{
193 		m_aLinePos = _rPos;
194 		m_aOutputSize = _rSize;
195 
196         impl_layoutComponents();
197 	}
198 
199 	//------------------------------------------------------------------
Show(sal_Bool bFlag)200 	void OBrowserLine::Show(sal_Bool bFlag)
201 	{
202 		m_aFtTitle.Show(bFlag);
203 		if ( m_pControlWindow )
204 			m_pControlWindow->Show( bFlag );
205 		if ( m_pBrowseButton )
206 			m_pBrowseButton->Show( bFlag );
207 		if ( m_pAdditionalBrowseButton )
208 			m_pAdditionalBrowseButton->Show( bFlag );
209 	}
210 
211 	//------------------------------------------------------------------
Hide()212 	void OBrowserLine::Hide()
213 	{
214 		Show(sal_False);
215 	}
216 
217 	//------------------------------------------------------------------
IsVisible()218 	sal_Bool OBrowserLine::IsVisible()
219 	{
220 		return m_aFtTitle.IsVisible();
221 	}
222 
223 	//------------------------------------------------------------------
impl_layoutComponents()224 	void OBrowserLine::impl_layoutComponents()
225 	{
226         {
227 		    Point aTitlePos( m_aLinePos.X(), m_aLinePos.Y() + 8 );
228             Size aTitleSize( m_nNameWidth - 3, m_aOutputSize.Height() );
229 
230             if ( m_bIndentTitle )
231             {
232                 Size aIndent( m_pTheParent->LogicToPixel( Size( 8, 0 ), MAP_APPFONT ) );
233                 aTitlePos.X() += aIndent.Width();
234                 aTitleSize.Width() -= aIndent.Width();
235             }
236     		m_aFtTitle.SetPosSizePixel( aTitlePos, aTitleSize );
237         }
238 
239 		sal_Int32 nBrowseButtonSize = m_aOutputSize.Height() - 4;
240 
241 		if ( m_pControlWindow )
242 		{
243             Point aControlPos( m_aLinePos.X() + m_nNameWidth, m_aLinePos.Y() + 2 );
244 			m_pControlWindow->SetPosPixel( aControlPos );
245 
246             Size aControlSize( m_aOutputSize.Width() - 4 - m_nNameWidth - nBrowseButtonSize - 4, m_pControlWindow->GetSizePixel().Height() );
247             if ( m_pAdditionalBrowseButton )
248                 aControlSize.Width() -= nBrowseButtonSize + 4;
249 			m_pControlWindow->SetSizePixel( aControlSize );
250 		}
251 
252         if ( m_pBrowseButton )
253 		{
254             Point aButtonPos( m_aOutputSize.Width() - 4 - nBrowseButtonSize, m_aLinePos.Y() + 2 );
255             Size aButtonSize( nBrowseButtonSize, nBrowseButtonSize );
256 			m_pBrowseButton->SetPosSizePixel( aButtonPos, aButtonSize );
257 
258             if ( m_pAdditionalBrowseButton )
259 		    {
260                 aButtonPos.X() -= nBrowseButtonSize + 4;
261 			    m_pAdditionalBrowseButton->SetPosSizePixel( aButtonPos, aButtonSize );
262 		    }
263 		}
264 	}
265 
266 	//------------------------------------------------------------------
SetTitle(const XubString & _rNewTtile)267 	void OBrowserLine::SetTitle(const XubString& _rNewTtile )
268 	{
269         if ( GetTitle() == _rNewTtile )
270             return;
271 		// #99102# --------------
272 		m_aFtTitle.SetText( _rNewTtile );
273         if ( m_pControlWindow )
274             m_pControlWindow->SetAccessibleName( _rNewTtile );
275         if ( m_pBrowseButton )
276             m_pBrowseButton->SetAccessibleName( _rNewTtile );
277 		FullFillTitleString();
278 	}
279 
280 	// #99102# ---------------------------------------------------------
FullFillTitleString()281 	void OBrowserLine::FullFillTitleString()
282 	{
283 		if( m_pTheParent )
284 		{
285 			String aText = m_aFtTitle.GetText();
286 
287 			while( m_pTheParent->GetTextWidth( aText ) < m_nNameWidth )
288 		                aText.AppendAscii("...........");
289 
290 			// for Issue 69452
291 			if (Application::GetSettings().GetLayoutRTL())
292 			{
293 				sal_Unicode cRTL_mark = 0x200F;
294 				aText.Append(cRTL_mark);
295 			}
296 
297 			m_aFtTitle.SetText(aText);
298 		}
299 	}
300 
301 	//------------------------------------------------------------------
GetTitle() const302 	XubString OBrowserLine::GetTitle() const
303 	{
304         String sDisplayName = m_aFtTitle.GetText();
305 
306 	// for Issue 69452
307 	if (Application::GetSettings().GetLayoutRTL())
308 	{
309 		sal_Unicode cRTL_mark = 0x200F;
310 		sDisplayName.EraseTrailingChars(cRTL_mark);
311 	}
312 
313         sDisplayName.EraseTrailingChars( '.' );
314 
315         return sDisplayName;
316 	}
317 
318     //------------------------------------------------------------------
SetReadOnly(bool _bReadOnly)319     void OBrowserLine::SetReadOnly( bool _bReadOnly )
320     {
321         if ( m_bReadOnly != _bReadOnly )
322         {
323             m_bReadOnly = _bReadOnly;
324             implUpdateEnabledDisabled();
325         }
326     }
327 
328     //------------------------------------------------------------------
329     namespace
330     {
implSetBitIfAffected(sal_uInt16 & _nEnabledBits,sal_Int16 _nAffectedMask,sal_Int16 _nTestBit,bool _bSet)331         void implSetBitIfAffected( sal_uInt16& _nEnabledBits, sal_Int16 _nAffectedMask, sal_Int16 _nTestBit, bool _bSet )
332         {
333             if ( _nAffectedMask & _nTestBit )
334             {
335                 if ( _bSet )
336                     _nEnabledBits |= _nTestBit;
337                 else
338                     _nEnabledBits &= ~_nTestBit;
339             }
340         }
341 
implEnable(Window * _pWindow,sal_uInt16 _nEnabledBits,sal_uInt16 _nMatchBits)342         void implEnable( Window* _pWindow, sal_uInt16 _nEnabledBits, sal_uInt16 _nMatchBits  )
343         {
344             if ( _pWindow )
345                 _pWindow->Enable( ( _nEnabledBits & _nMatchBits ) == _nMatchBits );
346         }
347 
implEnable(Window * _pWindow,bool _bEnable)348         void implEnable( Window* _pWindow, bool _bEnable )
349         {
350             if ( _pWindow )
351                 _pWindow->Enable( _bEnable );
352         }
353     }
354 
355     //------------------------------------------------------------------
implUpdateEnabledDisabled()356 	void OBrowserLine::implUpdateEnabledDisabled()
357     {
358         implEnable( &m_aFtTitle,                m_nEnableFlags, PropertyLineElement::CompleteLine );
359         if ( m_pControlWindow )
360             implEnable( m_pControlWindow,       m_nEnableFlags, PropertyLineElement::CompleteLine | PropertyLineElement::InputControl );
361 
362         if ( m_bReadOnly )
363         {
364             implEnable( m_pBrowseButton,            false );
365             implEnable( m_pAdditionalBrowseButton,  false );
366         }
367         else
368         {
369             implEnable( m_pBrowseButton,            m_nEnableFlags, PropertyLineElement::CompleteLine | PropertyLineElement::PrimaryButton );
370             implEnable( m_pAdditionalBrowseButton,  m_nEnableFlags, PropertyLineElement::CompleteLine | PropertyLineElement::SecondaryButton );
371         }
372     }
373 
374     //------------------------------------------------------------------
EnablePropertyLine(bool _bEnable)375 	void OBrowserLine::EnablePropertyLine( bool _bEnable )
376     {
377         implSetBitIfAffected( m_nEnableFlags, PropertyLineElement::CompleteLine, PropertyLineElement::CompleteLine, _bEnable );
378         implUpdateEnabledDisabled();
379     }
380 
381     //------------------------------------------------------------------
EnablePropertyControls(sal_Int16 _nControls,bool _bEnable)382 	void OBrowserLine::EnablePropertyControls( sal_Int16 _nControls, bool _bEnable )
383 	{
384         implSetBitIfAffected( m_nEnableFlags, _nControls, PropertyLineElement::InputControl, _bEnable );
385         implSetBitIfAffected( m_nEnableFlags, _nControls, PropertyLineElement::PrimaryButton, _bEnable );
386         implSetBitIfAffected( m_nEnableFlags, _nControls, PropertyLineElement::SecondaryButton, _bEnable );
387         implUpdateEnabledDisabled();
388     }
389 
390     //------------------------------------------------------------------
impl_ensureButton(bool _bPrimary)391     PushButton& OBrowserLine::impl_ensureButton( bool _bPrimary )
392     {
393         PushButton*& rpButton = _bPrimary ? m_pBrowseButton : m_pAdditionalBrowseButton;
394 
395         if ( !rpButton )
396         {
397 		    rpButton = new PushButton( m_pTheParent, WB_NOPOINTERFOCUS );
398 		    rpButton->SetGetFocusHdl( LINK( this, OBrowserLine, OnButtonFocus ) );
399 		    rpButton->SetClickHdl( LINK( this, OBrowserLine, OnButtonClicked ) );
400 		    rpButton->SetText( String::CreateFromAscii( "..." ) );
401         }
402 
403         rpButton->Show();
404 
405         impl_layoutComponents();
406 
407         return *rpButton;
408     }
409 
410     //------------------------------------------------------------------
impl_getImagesFromURL_nothrow(const::rtl::OUString & _rImageURL,Image & _out_rImage,Image & _out_rHCImage)411     void OBrowserLine::impl_getImagesFromURL_nothrow( const ::rtl::OUString& _rImageURL, Image& _out_rImage, Image& _out_rHCImage )
412     {
413         try
414         {
415             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
416             Reference< XGraphicProvider > xGraphicProvider( aContext.createComponent( "com.sun.star.graphic.GraphicProvider" ), UNO_QUERY_THROW );
417 
418             Sequence< PropertyValue > aMediaProperties(1);
419             aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
420             aMediaProperties[0].Value <<= _rImageURL;
421 
422             Reference< XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties ), UNO_QUERY_THROW );
423             _out_rImage = _out_rHCImage = Image( xGraphic );
424 
425             // see if we find an HC version beside the normal graphic
426             INetURLObject aURL( _rImageURL );
427             ::rtl::OUString sBaseName( aURL.getBase() );
428             aURL.setBase( sBaseName + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_hc" ) ) );
429             ::rtl::OUString sHCImageURL( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
430 
431             Reference< XGraphic > xHCGraphic;
432             try
433             {
434                 aMediaProperties[0].Value <<= sHCImageURL;
435                 xHCGraphic = xGraphicProvider->queryGraphic( aMediaProperties );
436             }
437             catch( const Exception& ) { }
438 
439             if ( xHCGraphic.is() )
440                 _out_rHCImage = Image( xHCGraphic );
441         }
442         catch( const Exception& )
443         {
444         	DBG_UNHANDLED_EXCEPTION();
445         }
446     }
447 
448     //------------------------------------------------------------------
ShowBrowseButton(const::rtl::OUString & _rImageURL,sal_Bool _bPrimary)449 	void OBrowserLine::ShowBrowseButton( const ::rtl::OUString& _rImageURL, sal_Bool _bPrimary )
450 	{
451         PushButton& rButton( impl_ensureButton( _bPrimary ) );
452 
453         OSL_PRECOND( _rImageURL.getLength(), "OBrowserLine::ShowBrowseButton: use the other version if you don't have an image!" );
454         Image aImage, aHCImage;
455         impl_getImagesFromURL_nothrow( _rImageURL, aImage, aHCImage );
456 
457         rButton.SetModeImage( aImage, BMP_COLOR_NORMAL );
458         rButton.SetModeImage( aHCImage, BMP_COLOR_HIGHCONTRAST );
459 	}
460 
461     //------------------------------------------------------------------
ShowBrowseButton(const Image & _rImage,sal_Bool _bPrimary)462 	void OBrowserLine::ShowBrowseButton( const Image& _rImage, sal_Bool _bPrimary )
463 	{
464         PushButton& rButton( impl_ensureButton( _bPrimary ) );
465         if ( !!_rImage )
466             rButton.SetModeImage( _rImage );
467 	}
468 
469     //------------------------------------------------------------------
ShowBrowseButton(sal_Bool _bPrimary)470 	void OBrowserLine::ShowBrowseButton( sal_Bool _bPrimary )
471 	{
472         impl_ensureButton( _bPrimary );
473 	}
474 
475 	//------------------------------------------------------------------
implHideBrowseButton(sal_Bool _bPrimary,bool _bReLayout)476     void OBrowserLine::implHideBrowseButton( sal_Bool _bPrimary, bool _bReLayout )
477     {
478         PushButton*& rpButton = _bPrimary ? m_pBrowseButton : m_pAdditionalBrowseButton;
479 
480         if ( rpButton )
481 		{
482 			rpButton->Hide();
483 			delete rpButton;
484 			rpButton = NULL;
485 		}
486 
487         if ( _bReLayout )
488 		    impl_layoutComponents();
489     }
490 
491 	//------------------------------------------------------------------
HideBrowseButton(sal_Bool _bPrimary)492 	void OBrowserLine::HideBrowseButton( sal_Bool _bPrimary )
493 	{
494         implHideBrowseButton( _bPrimary, true );
495 	}
496 
497 	//------------------------------------------------------------------
SetTitleWidth(sal_uInt16 nWidth)498 	void OBrowserLine::SetTitleWidth(sal_uInt16 nWidth)
499 	{
500 		if (m_nNameWidth != nWidth+10)
501 		{
502 			m_nNameWidth = nWidth+10;
503 			impl_layoutComponents();
504 		}
505 		// #99102# ---------
506 		FullFillTitleString();
507 	}
508 
509 	//------------------------------------------------------------------
SetClickListener(IButtonClickListener * _pListener)510     void OBrowserLine::SetClickListener( IButtonClickListener* _pListener )
511     {
512         m_pClickListener = _pListener;
513     }
514 
515 	//------------------------------------------------------------------
IMPL_LINK(OBrowserLine,OnButtonClicked,PushButton *,_pButton)516     IMPL_LINK( OBrowserLine, OnButtonClicked, PushButton*, _pButton )
517     {
518         if ( m_pClickListener )
519             m_pClickListener->buttonClicked( this, _pButton == m_pBrowseButton );
520 
521         return 0L;
522     }
523 
524 	//------------------------------------------------------------------
525 	IMPL_LINK( OBrowserLine, OnButtonFocus, PushButton*, /*pPB*/ )
526 	{
527 		if ( m_xControl.is() )
528 		{
529             try
530             {
531                 Reference< XPropertyControlContext > xContext( m_xControl->getControlContext(), UNO_QUERY_THROW );
532                 xContext->focusGained( m_xControl );
533             }
534             catch( const Exception& )
535             {
536                 DBG_UNHANDLED_EXCEPTION();
537             }
538 		}
539 		return 0;
540 	}
541 //............................................................................
542 } // namespace pcr
543 //............................................................................
544 
545