1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "browserline.hxx"
22 #include <com/sun/star/uno/XComponentContext.hpp>
23 #include <com/sun/star/inspection/PropertyLineElement.hpp>
24 #include <com/sun/star/graphic/GraphicProvider.hpp>
25 #include <com/sun/star/graphic/XGraphicProvider.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/string.hxx>
29 #include <tools/debug.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <toolkit/helper/vclunohelper.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/settings.hxx>
40 using ::com::sun::star::uno::Reference
;
41 using ::com::sun::star::uno::XComponentContext
;
42 using ::com::sun::star::inspection::XPropertyControl
;
43 using ::com::sun::star::inspection::XPropertyControlContext
;
44 using ::com::sun::star::uno::UNO_QUERY_THROW
;
45 using ::com::sun::star::uno::Exception
;
46 using ::com::sun::star::graphic::GraphicProvider
;
47 using ::com::sun::star::graphic::XGraphicProvider
;
48 using ::com::sun::star::uno::Sequence
;
49 using ::com::sun::star::beans::PropertyValue
;
50 using ::com::sun::star::graphic::XGraphic
;
52 namespace PropertyLineElement
= ::com::sun::star::inspection::PropertyLineElement
;
54 OBrowserLine::OBrowserLine( const OUString
& _rEntryName
, vcl::Window
* pParent
)
55 :m_sEntryName( _rEntryName
)
56 ,m_aFtTitle(VclPtr
<FixedText
>::Create(pParent
))
57 ,m_pControlWindow( NULL
)
58 ,m_pBrowseButton(NULL
)
59 ,m_pAdditionalBrowseButton( NULL
)
60 ,m_pClickListener( NULL
)
61 ,m_pTheParent(pParent
)
63 ,m_nEnableFlags( 0xFFFF )
64 ,m_bIndentTitle( false )
70 OBrowserLine::~OBrowserLine()
72 implHideBrowseButton( true, false );
73 implHideBrowseButton( false, false );
74 m_aFtTitle
.disposeAndClear();
78 void OBrowserLine::IndentTitle( bool _bIndent
)
80 if ( m_bIndentTitle
!= _bIndent
)
82 m_bIndentTitle
= _bIndent
;
83 impl_layoutComponents();
88 void OBrowserLine::SetComponentHelpIds( const OString
& _rHelpId
, const OString
& _sPrimaryButtonId
, const OString
& _sSecondaryButtonId
)
90 if ( m_pControlWindow
)
91 m_pControlWindow
->SetHelpId( _rHelpId
);
93 if ( m_pBrowseButton
)
95 m_pBrowseButton
->SetHelpId( _rHelpId
);
96 m_pBrowseButton
->SetUniqueId( _sPrimaryButtonId
);
98 if ( m_pAdditionalBrowseButton
)
100 m_pAdditionalBrowseButton
->SetHelpId( _rHelpId
);
101 m_pAdditionalBrowseButton
->SetUniqueId( _sSecondaryButtonId
);
107 void OBrowserLine::setControl( const Reference
< XPropertyControl
>& _rxControl
)
109 m_xControl
= _rxControl
;
110 m_pControlWindow
= m_xControl
.is() ? VCLUnoHelper::GetWindow( _rxControl
->getControlWindow() ) : VclPtr
<vcl::Window
>();
111 DBG_ASSERT( m_pControlWindow
, "OBrowserLine::setControl: setting NULL controls/windows is not allowed!" );
113 if ( m_pControlWindow
)
115 m_pControlWindow
->SetParent( m_pTheParent
);
116 m_pControlWindow
->Show();
118 impl_layoutComponents();
122 vcl::Window
* OBrowserLine::GetRefWindow()
124 vcl::Window
* pRefWindow
= m_aFtTitle
.get();
128 pRefWindow
=(vcl::Window
*)m_pBrowseButton
;
130 else if ( m_pControlWindow
)
132 pRefWindow
= m_pControlWindow
;
138 void OBrowserLine::SetTabOrder(vcl::Window
* pRefWindow
, sal_uInt16 nFlags
)
140 m_aFtTitle
->SetZOrder(pRefWindow
,nFlags
);
141 if ( m_pControlWindow
)
142 m_pControlWindow
->SetZOrder( m_aFtTitle
.get(), WINDOW_ZORDER_BEHIND
);
144 if ( m_pBrowseButton
&& m_pControlWindow
)
145 m_pBrowseButton
->SetZOrder( m_pControlWindow
, WINDOW_ZORDER_BEHIND
);
147 if ( m_pAdditionalBrowseButton
&& m_pBrowseButton
)
148 m_pAdditionalBrowseButton
->SetZOrder( m_pBrowseButton
, WINDOW_ZORDER_BEHIND
);
152 bool OBrowserLine::GrabFocus()
156 if ( m_pControlWindow
&& m_pControlWindow
->IsEnabled() )
158 m_pControlWindow
->GrabFocus();
161 else if ( m_pAdditionalBrowseButton
&& m_pAdditionalBrowseButton
->IsEnabled() )
163 m_pAdditionalBrowseButton
->GrabFocus();
166 else if ( m_pBrowseButton
&& m_pBrowseButton
->IsEnabled() )
168 m_pBrowseButton
->GrabFocus();
175 void OBrowserLine::SetPosSizePixel( Point _rPos
, Size _rSize
)
178 m_aOutputSize
= _rSize
;
180 impl_layoutComponents();
184 void OBrowserLine::Show(bool bFlag
)
186 m_aFtTitle
->Show(bFlag
);
187 if ( m_pControlWindow
)
188 m_pControlWindow
->Show( bFlag
);
189 if ( m_pBrowseButton
)
190 m_pBrowseButton
->Show( bFlag
);
191 if ( m_pAdditionalBrowseButton
)
192 m_pAdditionalBrowseButton
->Show( bFlag
);
196 void OBrowserLine::Hide()
202 bool OBrowserLine::IsVisible()
204 return m_aFtTitle
->IsVisible();
208 void OBrowserLine::impl_layoutComponents()
211 Point
aTitlePos( m_aLinePos
.X(), m_aLinePos
.Y() + 8 );
212 Size
aTitleSize( m_nNameWidth
- 3, m_aOutputSize
.Height() );
214 if ( m_bIndentTitle
)
216 Size
aIndent( m_pTheParent
->LogicToPixel( Size( 8, 0 ), MAP_APPFONT
) );
217 aTitlePos
.X() += aIndent
.Width();
218 aTitleSize
.Width() -= aIndent
.Width();
220 m_aFtTitle
->SetPosSizePixel( aTitlePos
, aTitleSize
);
223 sal_Int32 nBrowseButtonSize
= m_aOutputSize
.Height() - 4;
225 if ( m_pControlWindow
)
227 Point
aControlPos( m_aLinePos
.X() + m_nNameWidth
, m_aLinePos
.Y() + 2 );
228 m_pControlWindow
->SetPosPixel( aControlPos
);
230 Size
aControlSize( m_aOutputSize
.Width() - 4 - m_nNameWidth
- nBrowseButtonSize
- 4, m_pControlWindow
->GetSizePixel().Height() );
231 if ( m_pAdditionalBrowseButton
)
232 aControlSize
.Width() -= nBrowseButtonSize
+ 4;
233 m_pControlWindow
->SetSizePixel( aControlSize
);
236 if ( m_pBrowseButton
)
238 Point
aButtonPos( m_aOutputSize
.Width() - 4 - nBrowseButtonSize
, m_aLinePos
.Y() + 2 );
239 Size
aButtonSize( nBrowseButtonSize
, nBrowseButtonSize
);
240 m_pBrowseButton
->SetPosSizePixel( aButtonPos
, aButtonSize
);
242 if ( m_pAdditionalBrowseButton
)
244 aButtonPos
.X() -= nBrowseButtonSize
+ 4;
245 m_pAdditionalBrowseButton
->SetPosSizePixel( aButtonPos
, aButtonSize
);
251 void OBrowserLine::SetTitle(const OUString
& _rNewTtile
)
253 if ( GetTitle() == _rNewTtile
)
255 // #99102# --------------
256 m_aFtTitle
->SetText( _rNewTtile
);
257 if ( m_pControlWindow
)
258 m_pControlWindow
->SetAccessibleName( _rNewTtile
);
259 if ( m_pBrowseButton
)
260 m_pBrowseButton
->SetAccessibleName( _rNewTtile
);
261 FullFillTitleString();
264 // #99102# ---------------------------------------------------------
265 void OBrowserLine::FullFillTitleString()
269 OUStringBuffer
aText( m_aFtTitle
->GetText() );
271 while( m_pTheParent
->GetTextWidth( aText
.toString() ) < m_nNameWidth
)
272 aText
.append("...........");
275 if (AllSettings::GetLayoutRTL())
277 sal_Unicode cRTL_mark
= 0x200F;
278 aText
.append( OUString(cRTL_mark
) );
281 m_aFtTitle
->SetText( aText
.makeStringAndClear() );
286 OUString
OBrowserLine::GetTitle() const
288 OUString sDisplayName
= m_aFtTitle
->GetText();
291 if (AllSettings::GetLayoutRTL())
293 sal_Unicode cRTL_mark
= 0x200F;
294 sDisplayName
= comphelper::string::stripEnd(sDisplayName
, cRTL_mark
);
297 sDisplayName
= comphelper::string::stripEnd(sDisplayName
, '.');
303 void OBrowserLine::SetReadOnly( bool _bReadOnly
)
305 if ( m_bReadOnly
!= _bReadOnly
)
307 m_bReadOnly
= _bReadOnly
;
308 implUpdateEnabledDisabled();
315 void implSetBitIfAffected( sal_uInt16
& _nEnabledBits
, sal_Int16 _nAffectedMask
, sal_Int16 _nTestBit
, bool _bSet
)
317 if ( _nAffectedMask
& _nTestBit
)
320 _nEnabledBits
|= _nTestBit
;
322 _nEnabledBits
&= ~_nTestBit
;
326 void implEnable( vcl::Window
* _pWindow
, sal_uInt16 _nEnabledBits
, sal_uInt16 _nMatchBits
)
329 _pWindow
->Enable( ( _nEnabledBits
& _nMatchBits
) == _nMatchBits
);
332 void implEnable( vcl::Window
* _pWindow
, bool _bEnable
)
335 _pWindow
->Enable( _bEnable
);
340 void OBrowserLine::implUpdateEnabledDisabled()
342 implEnable( m_aFtTitle
.get(), m_nEnableFlags
, PropertyLineElement::CompleteLine
);
343 if ( m_pControlWindow
)
344 implEnable( m_pControlWindow
, m_nEnableFlags
, PropertyLineElement::CompleteLine
| PropertyLineElement::InputControl
);
348 implEnable( m_pBrowseButton
, false );
349 implEnable( m_pAdditionalBrowseButton
, false );
353 implEnable( m_pBrowseButton
, m_nEnableFlags
, PropertyLineElement::CompleteLine
| PropertyLineElement::PrimaryButton
);
354 implEnable( m_pAdditionalBrowseButton
, m_nEnableFlags
, PropertyLineElement::CompleteLine
| PropertyLineElement::SecondaryButton
);
359 void OBrowserLine::EnablePropertyLine( bool _bEnable
)
361 implSetBitIfAffected( m_nEnableFlags
, PropertyLineElement::CompleteLine
, PropertyLineElement::CompleteLine
, _bEnable
);
362 implUpdateEnabledDisabled();
366 void OBrowserLine::EnablePropertyControls( sal_Int16 _nControls
, bool _bEnable
)
368 implSetBitIfAffected( m_nEnableFlags
, _nControls
, PropertyLineElement::InputControl
, _bEnable
);
369 implSetBitIfAffected( m_nEnableFlags
, _nControls
, PropertyLineElement::PrimaryButton
, _bEnable
);
370 implSetBitIfAffected( m_nEnableFlags
, _nControls
, PropertyLineElement::SecondaryButton
, _bEnable
);
371 implUpdateEnabledDisabled();
375 PushButton
& OBrowserLine::impl_ensureButton( bool _bPrimary
)
377 VclPtr
<PushButton
>& rpButton
= _bPrimary
? m_pBrowseButton
: m_pAdditionalBrowseButton
;
381 rpButton
= VclPtr
<PushButton
>::Create( m_pTheParent
, WB_NOPOINTERFOCUS
);
382 rpButton
->SetGetFocusHdl( LINK( this, OBrowserLine
, OnButtonFocus
) );
383 rpButton
->SetClickHdl( LINK( this, OBrowserLine
, OnButtonClicked
) );
384 rpButton
->SetText(OUString("..."));
389 impl_layoutComponents();
395 void OBrowserLine::impl_getImagesFromURL_nothrow( const OUString
& _rImageURL
, Image
& _out_rImage
)
399 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
400 Reference
< XGraphicProvider
> xGraphicProvider( GraphicProvider::create(xContext
) );
402 Sequence
< PropertyValue
> aMediaProperties(1);
403 aMediaProperties
[0].Name
= "URL";
404 aMediaProperties
[0].Value
<<= _rImageURL
;
406 Reference
< XGraphic
> xGraphic( xGraphicProvider
->queryGraphic( aMediaProperties
), UNO_QUERY_THROW
);
407 _out_rImage
= Image( xGraphic
);
409 catch( const Exception
& )
411 DBG_UNHANDLED_EXCEPTION();
416 void OBrowserLine::ShowBrowseButton( const OUString
& _rImageURL
, bool _bPrimary
)
418 PushButton
& rButton( impl_ensureButton( _bPrimary
) );
420 OSL_PRECOND( !_rImageURL
.isEmpty(), "OBrowserLine::ShowBrowseButton: use the other version if you don't have an image!" );
422 impl_getImagesFromURL_nothrow( _rImageURL
, aImage
);
424 rButton
.SetModeImage( aImage
);
428 void OBrowserLine::ShowBrowseButton( const Image
& _rImage
, bool _bPrimary
)
430 PushButton
& rButton( impl_ensureButton( _bPrimary
) );
432 rButton
.SetModeImage( _rImage
);
436 void OBrowserLine::ShowBrowseButton( bool _bPrimary
)
438 impl_ensureButton( _bPrimary
);
442 void OBrowserLine::implHideBrowseButton( bool _bPrimary
, bool _bReLayout
)
444 VclPtr
<PushButton
>& rpButton
= _bPrimary
? m_pBrowseButton
: m_pAdditionalBrowseButton
;
449 rpButton
.disposeAndClear();
453 impl_layoutComponents();
457 void OBrowserLine::HideBrowseButton( bool _bPrimary
)
459 implHideBrowseButton( _bPrimary
, true );
463 void OBrowserLine::SetTitleWidth(sal_uInt16 nWidth
)
465 if (m_nNameWidth
!= nWidth
+10)
467 m_nNameWidth
= nWidth
+10;
468 impl_layoutComponents();
471 FullFillTitleString();
475 void OBrowserLine::SetClickListener( IButtonClickListener
* _pListener
)
477 m_pClickListener
= _pListener
;
481 IMPL_LINK( OBrowserLine
, OnButtonClicked
, PushButton
*, _pButton
)
483 if ( m_pClickListener
)
484 m_pClickListener
->buttonClicked( this, _pButton
== m_pBrowseButton
);
490 IMPL_LINK( OBrowserLine
, OnButtonFocus
, PushButton
*, /*pPB*/ )
492 if ( m_xControl
.is() )
496 Reference
< XPropertyControlContext
> xContext( m_xControl
->getControlContext(), UNO_QUERY_THROW
);
497 xContext
->focusGained( m_xControl
);
499 catch( const Exception
& )
501 DBG_UNHANDLED_EXCEPTION();
510 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */