1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: browserline.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "browserline.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/inspection/PropertyLineElement.hpp>
37 #include <com/sun/star/graphic/XGraphicProvider.hpp>
38 /** === end UNO includes === **/
40 #include <vcl/svapp.hxx>
41 #include <tools/debug.hxx>
42 #include <tools/diagnose_ex.h>
43 #include <tools/urlobj.hxx>
44 #include <toolkit/helper/vclunohelper.hxx>
46 #include <comphelper/processfactory.hxx>
47 #include <comphelper/componentcontext.hxx>
49 //............................................................................
52 //............................................................................
54 /** === begin UNO using === **/
55 using ::com::sun::star::uno::Reference
;
56 using ::com::sun::star::inspection::XPropertyControl
;
57 using ::com::sun::star::inspection::XPropertyControlContext
;
58 using ::com::sun::star::uno::UNO_QUERY_THROW
;
59 using ::com::sun::star::uno::Exception
;
60 using ::com::sun::star::graphic::XGraphicProvider
;
61 using ::com::sun::star::uno::Sequence
;
62 using ::com::sun::star::beans::PropertyValue
;
63 using ::com::sun::star::graphic::XGraphic
;
64 /** === end UNO using === **/
66 namespace PropertyLineElement
= ::com::sun::star::inspection::PropertyLineElement
;
68 //==================================================================
70 //==================================================================
71 DBG_NAME(OBrowserLine
)
72 //------------------------------------------------------------------
74 OBrowserLine::OBrowserLine( const ::rtl::OUString
& _rEntryName
, Window
* pParent
)
75 :m_sEntryName( _rEntryName
)
77 ,m_pControlWindow( NULL
)
78 ,m_pBrowseButton(NULL
)
79 ,m_pAdditionalBrowseButton( NULL
)
80 ,m_pClickListener( NULL
)
81 ,m_pTheParent(pParent
)
83 ,m_nEnableFlags( 0xFFFF )
84 ,m_bIndentTitle( false )
87 DBG_CTOR(OBrowserLine
,NULL
);
91 //------------------------------------------------------------------
92 OBrowserLine::~OBrowserLine()
94 implHideBrowseButton( true, false );
95 implHideBrowseButton( false, false );
97 DBG_DTOR(OBrowserLine
,NULL
);
100 //------------------------------------------------------------------
101 void OBrowserLine::IndentTitle( bool _bIndent
)
103 if ( m_bIndentTitle
!= _bIndent
)
105 m_bIndentTitle
= _bIndent
;
106 impl_layoutComponents();
110 //------------------------------------------------------------------
111 void OBrowserLine::SetComponentHelpIds( const SmartId
& _rHelpId
, sal_uInt32 _bPrimaryButtonId
, sal_uInt32 _nSecondaryButtonId
)
113 if ( m_pControlWindow
)
114 m_pControlWindow
->SetSmartHelpId( _rHelpId
);
116 if ( m_pBrowseButton
)
118 m_pBrowseButton
->SetSmartHelpId( _rHelpId
);
119 m_pBrowseButton
->SetUniqueId( _bPrimaryButtonId
);
121 if ( m_pAdditionalBrowseButton
)
123 m_pAdditionalBrowseButton
->SetSmartHelpId( _rHelpId
);
124 m_pAdditionalBrowseButton
->SetUniqueId( _nSecondaryButtonId
);
129 //------------------------------------------------------------------
130 void OBrowserLine::setControl( const Reference
< XPropertyControl
>& _rxControl
)
132 m_xControl
= _rxControl
;
133 m_pControlWindow
= m_xControl
.is() ? VCLUnoHelper::GetWindow( _rxControl
->getControlWindow() ) : NULL
;
134 DBG_ASSERT( m_pControlWindow
, "OBrowserLine::setControl: setting NULL controls/windows is not allowed!" );
136 if ( m_pControlWindow
)
138 m_pControlWindow
->SetParent( m_pTheParent
);
139 m_pControlWindow
->Show();
141 impl_layoutComponents();
144 //------------------------------------------------------------------
145 Window
* OBrowserLine::GetRefWindow()
147 Window
* pRefWindow
=&m_aFtTitle
;
151 pRefWindow
=(Window
*)m_pBrowseButton
;
153 else if ( m_pControlWindow
)
155 pRefWindow
= m_pControlWindow
;
160 //------------------------------------------------------------------
161 void OBrowserLine::SetTabOrder(Window
* pRefWindow
, sal_uInt16 nFlags
)
163 m_aFtTitle
.SetZOrder(pRefWindow
,nFlags
);
164 if ( m_pControlWindow
)
165 m_pControlWindow
->SetZOrder( (Window
*)&m_aFtTitle
, WINDOW_ZORDER_BEHIND
);
167 if ( m_pBrowseButton
)
168 m_pBrowseButton
->SetZOrder( m_pControlWindow
, WINDOW_ZORDER_BEHIND
);
170 if ( m_pAdditionalBrowseButton
)
171 m_pAdditionalBrowseButton
->SetZOrder( m_pBrowseButton
, WINDOW_ZORDER_BEHIND
);
174 //------------------------------------------------------------------
175 sal_Bool
OBrowserLine::GrabFocus()
177 sal_Bool bRes
=sal_False
;
179 if ( m_pControlWindow
&& m_pControlWindow
->IsEnabled() )
181 m_pControlWindow
->GrabFocus();
184 else if ( m_pAdditionalBrowseButton
&& m_pAdditionalBrowseButton
->IsEnabled() )
186 m_pAdditionalBrowseButton
->GrabFocus();
189 else if ( m_pBrowseButton
&& m_pBrowseButton
->IsEnabled() )
191 m_pBrowseButton
->GrabFocus();
197 //------------------------------------------------------------------
198 void OBrowserLine::SetPosSizePixel( Point _rPos
, Size _rSize
)
201 m_aOutputSize
= _rSize
;
203 impl_layoutComponents();
206 //------------------------------------------------------------------
207 void OBrowserLine::Show(sal_Bool bFlag
)
209 m_aFtTitle
.Show(bFlag
);
210 if ( m_pControlWindow
)
211 m_pControlWindow
->Show( bFlag
);
212 if ( m_pBrowseButton
)
213 m_pBrowseButton
->Show( bFlag
);
214 if ( m_pAdditionalBrowseButton
)
215 m_pAdditionalBrowseButton
->Show( bFlag
);
218 //------------------------------------------------------------------
219 void OBrowserLine::Hide()
224 //------------------------------------------------------------------
225 sal_Bool
OBrowserLine::IsVisible()
227 return m_aFtTitle
.IsVisible();
230 //------------------------------------------------------------------
231 void OBrowserLine::impl_layoutComponents()
234 Point
aTitlePos( m_aLinePos
.X(), m_aLinePos
.Y() + 8 );
235 Size
aTitleSize( m_nNameWidth
- 3, m_aOutputSize
.Height() );
237 if ( m_bIndentTitle
)
239 Size
aIndent( m_pTheParent
->LogicToPixel( Size( 8, 0 ), MAP_APPFONT
) );
240 aTitlePos
.X() += aIndent
.Width();
241 aTitleSize
.Width() -= aIndent
.Width();
243 m_aFtTitle
.SetPosSizePixel( aTitlePos
, aTitleSize
);
246 sal_Int32 nBrowseButtonSize
= m_aOutputSize
.Height() - 4;
248 if ( m_pControlWindow
)
250 Point
aControlPos( m_aLinePos
.X() + m_nNameWidth
, m_aLinePos
.Y() + 2 );
251 m_pControlWindow
->SetPosPixel( aControlPos
);
253 Size
aControlSize( m_aOutputSize
.Width() - 4 - m_nNameWidth
- nBrowseButtonSize
- 4, m_pControlWindow
->GetSizePixel().Height() );
254 if ( m_pAdditionalBrowseButton
)
255 aControlSize
.Width() -= nBrowseButtonSize
+ 4;
256 m_pControlWindow
->SetSizePixel( aControlSize
);
259 if ( m_pBrowseButton
)
261 Point
aButtonPos( m_aOutputSize
.Width() - 4 - nBrowseButtonSize
, m_aLinePos
.Y() + 2 );
262 Size
aButtonSize( nBrowseButtonSize
, nBrowseButtonSize
);
263 m_pBrowseButton
->SetPosSizePixel( aButtonPos
, aButtonSize
);
265 if ( m_pAdditionalBrowseButton
)
267 aButtonPos
.X() -= nBrowseButtonSize
+ 4;
268 m_pAdditionalBrowseButton
->SetPosSizePixel( aButtonPos
, aButtonSize
);
273 //------------------------------------------------------------------
274 void OBrowserLine::SetTitle(const XubString
& _rNewTtile
)
276 if ( GetTitle() == _rNewTtile
)
278 // #99102# --------------
279 m_aFtTitle
.SetText( _rNewTtile
);
280 if ( m_pControlWindow
)
281 m_pControlWindow
->SetAccessibleName( _rNewTtile
);
282 if ( m_pBrowseButton
)
283 m_pBrowseButton
->SetAccessibleName( _rNewTtile
);
284 FullFillTitleString();
287 // #99102# ---------------------------------------------------------
288 void OBrowserLine::FullFillTitleString()
292 String aText
= m_aFtTitle
.GetText();
294 while( m_pTheParent
->GetTextWidth( aText
) < m_nNameWidth
)
295 aText
.AppendAscii("...........");
298 if (Application::GetSettings().GetLayoutRTL())
300 sal_Unicode cRTL_mark
= 0x200F;
301 aText
.Append(cRTL_mark
);
304 m_aFtTitle
.SetText(aText
);
308 //------------------------------------------------------------------
309 XubString
OBrowserLine::GetTitle() const
311 String sDisplayName
= m_aFtTitle
.GetText();
314 if (Application::GetSettings().GetLayoutRTL())
316 sal_Unicode cRTL_mark
= 0x200F;
317 sDisplayName
.EraseTrailingChars(cRTL_mark
);
320 sDisplayName
.EraseTrailingChars( '.' );
325 //------------------------------------------------------------------
326 sal_Bool
OBrowserLine::IsPropertyInputEnabled( ) const
328 return ( m_nEnableFlags
& PropertyLineElement::InputControl
) != 0;
331 //------------------------------------------------------------------
332 void OBrowserLine::SetReadOnly( bool _bReadOnly
)
334 if ( m_bReadOnly
!= _bReadOnly
)
336 m_bReadOnly
= _bReadOnly
;
337 implUpdateEnabledDisabled();
341 //------------------------------------------------------------------
344 void implSetBitIfAffected( sal_uInt16
& _nEnabledBits
, sal_Int16 _nAffectedMask
, sal_Int16 _nTestBit
, bool _bSet
)
346 if ( _nAffectedMask
& _nTestBit
)
349 _nEnabledBits
|= _nTestBit
;
351 _nEnabledBits
&= ~_nTestBit
;
355 void implEnable( Window
* _pWindow
, sal_uInt16 _nEnabledBits
, sal_uInt16 _nMatchBits
)
358 _pWindow
->Enable( ( _nEnabledBits
& _nMatchBits
) == _nMatchBits
);
361 void implEnable( Window
* _pWindow
, bool _bEnable
)
364 _pWindow
->Enable( _bEnable
);
368 //------------------------------------------------------------------
369 void OBrowserLine::implUpdateEnabledDisabled()
371 implEnable( &m_aFtTitle
, m_nEnableFlags
, PropertyLineElement::CompleteLine
);
372 if ( m_pControlWindow
)
373 implEnable( m_pControlWindow
, m_nEnableFlags
, PropertyLineElement::CompleteLine
| PropertyLineElement::InputControl
);
377 implEnable( m_pBrowseButton
, false );
378 implEnable( m_pAdditionalBrowseButton
, false );
382 implEnable( m_pBrowseButton
, m_nEnableFlags
, PropertyLineElement::CompleteLine
| PropertyLineElement::PrimaryButton
);
383 implEnable( m_pAdditionalBrowseButton
, m_nEnableFlags
, PropertyLineElement::CompleteLine
| PropertyLineElement::SecondaryButton
);
387 //------------------------------------------------------------------
388 void OBrowserLine::EnablePropertyLine( bool _bEnable
)
390 implSetBitIfAffected( m_nEnableFlags
, PropertyLineElement::CompleteLine
, PropertyLineElement::CompleteLine
, _bEnable
);
391 implUpdateEnabledDisabled();
394 //------------------------------------------------------------------
395 void OBrowserLine::EnablePropertyControls( sal_Int16 _nControls
, bool _bEnable
)
397 implSetBitIfAffected( m_nEnableFlags
, _nControls
, PropertyLineElement::InputControl
, _bEnable
);
398 implSetBitIfAffected( m_nEnableFlags
, _nControls
, PropertyLineElement::PrimaryButton
, _bEnable
);
399 implSetBitIfAffected( m_nEnableFlags
, _nControls
, PropertyLineElement::SecondaryButton
, _bEnable
);
400 implUpdateEnabledDisabled();
403 //------------------------------------------------------------------
404 PushButton
& OBrowserLine::impl_ensureButton( bool _bPrimary
)
406 PushButton
*& rpButton
= _bPrimary
? m_pBrowseButton
: m_pAdditionalBrowseButton
;
410 rpButton
= new PushButton( m_pTheParent
, WB_NOPOINTERFOCUS
);
411 rpButton
->SetGetFocusHdl( LINK( this, OBrowserLine
, OnButtonFocus
) );
412 rpButton
->SetClickHdl( LINK( this, OBrowserLine
, OnButtonClicked
) );
413 rpButton
->SetText( String::CreateFromAscii( "..." ) );
418 impl_layoutComponents();
423 //------------------------------------------------------------------
424 void OBrowserLine::impl_getImagesFromURL_nothrow( const ::rtl::OUString
& _rImageURL
, Image
& _out_rImage
, Image
& _out_rHCImage
)
428 ::comphelper::ComponentContext
aContext( ::comphelper::getProcessServiceFactory() );
429 Reference
< XGraphicProvider
> xGraphicProvider( aContext
.createComponent( "com.sun.star.graphic.GraphicProvider" ), UNO_QUERY_THROW
);
431 Sequence
< PropertyValue
> aMediaProperties(1);
432 aMediaProperties
[0].Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
433 aMediaProperties
[0].Value
<<= _rImageURL
;
435 Reference
< XGraphic
> xGraphic( xGraphicProvider
->queryGraphic( aMediaProperties
), UNO_QUERY_THROW
);
436 _out_rImage
= _out_rHCImage
= Image( xGraphic
);
438 // see if we find an HC version beside the normal graphic
439 INetURLObject
aURL( _rImageURL
);
440 ::rtl::OUString
sBaseName( aURL
.getBase() );
441 aURL
.setBase( sBaseName
+ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_hc" ) ) );
442 ::rtl::OUString
sHCImageURL( aURL
.GetMainURL( INetURLObject::NO_DECODE
) );
444 Reference
< XGraphic
> xHCGraphic
;
447 aMediaProperties
[0].Value
<<= sHCImageURL
;
448 xHCGraphic
= xGraphicProvider
->queryGraphic( aMediaProperties
);
450 catch( const Exception
& ) { }
452 if ( xHCGraphic
.is() )
453 _out_rHCImage
= Image( xHCGraphic
);
455 catch( const Exception
& )
457 DBG_UNHANDLED_EXCEPTION();
461 //------------------------------------------------------------------
462 void OBrowserLine::ShowBrowseButton( const ::rtl::OUString
& _rImageURL
, sal_Bool _bPrimary
)
464 PushButton
& rButton( impl_ensureButton( _bPrimary
) );
466 OSL_PRECOND( _rImageURL
.getLength(), "OBrowserLine::ShowBrowseButton: use the other version if you don't have an image!" );
467 Image aImage
, aHCImage
;
468 impl_getImagesFromURL_nothrow( _rImageURL
, aImage
, aHCImage
);
470 rButton
.SetModeImage( aImage
, BMP_COLOR_NORMAL
);
471 rButton
.SetModeImage( aHCImage
, BMP_COLOR_HIGHCONTRAST
);
474 //------------------------------------------------------------------
475 void OBrowserLine::ShowBrowseButton( const Image
& _rImage
, sal_Bool _bPrimary
)
477 PushButton
& rButton( impl_ensureButton( _bPrimary
) );
479 rButton
.SetModeImage( _rImage
);
482 //------------------------------------------------------------------
483 void OBrowserLine::ShowBrowseButton( sal_Bool _bPrimary
)
485 impl_ensureButton( _bPrimary
);
488 //------------------------------------------------------------------
489 void OBrowserLine::implHideBrowseButton( sal_Bool _bPrimary
, bool _bReLayout
)
491 PushButton
*& rpButton
= _bPrimary
? m_pBrowseButton
: m_pAdditionalBrowseButton
;
501 impl_layoutComponents();
504 //------------------------------------------------------------------
505 void OBrowserLine::HideBrowseButton( sal_Bool _bPrimary
)
507 implHideBrowseButton( _bPrimary
, true );
510 //------------------------------------------------------------------
511 void OBrowserLine::SetTitleWidth(sal_uInt16 nWidth
)
513 if (m_nNameWidth
!= nWidth
+10)
515 m_nNameWidth
= nWidth
+10;
516 impl_layoutComponents();
519 FullFillTitleString();
522 //------------------------------------------------------------------
523 void OBrowserLine::SetClickListener( IButtonClickListener
* _pListener
)
525 m_pClickListener
= _pListener
;
528 //------------------------------------------------------------------
529 IMPL_LINK( OBrowserLine
, OnButtonClicked
, PushButton
*, _pButton
)
531 if ( m_pClickListener
)
532 m_pClickListener
->buttonClicked( this, _pButton
== m_pBrowseButton
);
537 //------------------------------------------------------------------
538 IMPL_LINK( OBrowserLine
, OnButtonFocus
, PushButton
*, /*pPB*/ )
540 if ( m_xControl
.is() )
544 Reference
< XPropertyControlContext
> xContext( m_xControl
->getControlContext(), UNO_QUERY_THROW
);
545 xContext
->focusGained( m_xControl
);
547 catch( const Exception
& )
549 DBG_UNHANDLED_EXCEPTION();
554 //............................................................................
556 //............................................................................