Linux x86 build fix
[LibreOffice.git] / extensions / source / propctrlr / browserline.cxx
blob80cbe9266523565f6193efd26aacd24141f7c65e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
36 namespace pcr
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)
62 ,m_nNameWidth(0)
63 ,m_nEnableFlags( 0xFFFF )
64 ,m_bIndentTitle( false )
65 ,m_bReadOnly( false )
67 m_aFtTitle->Show();
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();
126 if(m_pBrowseButton)
128 pRefWindow=(vcl::Window*)m_pBrowseButton;
130 else if ( m_pControlWindow )
132 pRefWindow = m_pControlWindow;
134 return pRefWindow;
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()
154 bool bRes=false;
156 if ( m_pControlWindow && m_pControlWindow->IsEnabled() )
158 m_pControlWindow->GrabFocus();
159 bRes = true;
161 else if ( m_pAdditionalBrowseButton && m_pAdditionalBrowseButton->IsEnabled() )
163 m_pAdditionalBrowseButton->GrabFocus();
164 bRes = true;
166 else if ( m_pBrowseButton && m_pBrowseButton->IsEnabled() )
168 m_pBrowseButton->GrabFocus();
169 bRes = true;
171 return bRes;
175 void OBrowserLine::SetPosSizePixel( Point _rPos, Size _rSize )
177 m_aLinePos = _rPos;
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()
198 Show(false);
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 )
254 return;
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()
267 if( m_pTheParent )
269 OUStringBuffer aText( m_aFtTitle->GetText() );
271 while( m_pTheParent->GetTextWidth( aText.toString() ) < m_nNameWidth )
272 aText.append("...........");
274 // for Issue 69452
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();
290 // for Issue 69452
291 if (AllSettings::GetLayoutRTL())
293 sal_Unicode cRTL_mark = 0x200F;
294 sDisplayName = comphelper::string::stripEnd(sDisplayName, cRTL_mark);
297 sDisplayName = comphelper::string::stripEnd(sDisplayName, '.');
299 return sDisplayName;
303 void OBrowserLine::SetReadOnly( bool _bReadOnly )
305 if ( m_bReadOnly != _bReadOnly )
307 m_bReadOnly = _bReadOnly;
308 implUpdateEnabledDisabled();
313 namespace
315 void implSetBitIfAffected( sal_uInt16& _nEnabledBits, sal_Int16 _nAffectedMask, sal_Int16 _nTestBit, bool _bSet )
317 if ( _nAffectedMask & _nTestBit )
319 if ( _bSet )
320 _nEnabledBits |= _nTestBit;
321 else
322 _nEnabledBits &= ~_nTestBit;
326 void implEnable( vcl::Window* _pWindow, sal_uInt16 _nEnabledBits, sal_uInt16 _nMatchBits )
328 if ( _pWindow )
329 _pWindow->Enable( ( _nEnabledBits & _nMatchBits ) == _nMatchBits );
332 void implEnable( vcl::Window* _pWindow, bool _bEnable )
334 if ( _pWindow )
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 );
346 if ( m_bReadOnly )
348 implEnable( m_pBrowseButton, false );
349 implEnable( m_pAdditionalBrowseButton, false );
351 else
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;
379 if ( !rpButton )
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("..."));
387 rpButton->Show();
389 impl_layoutComponents();
391 return *rpButton;
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!" );
421 Image aImage;
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 ) );
431 if ( !!_rImage )
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;
446 if ( rpButton )
448 rpButton->Hide();
449 rpButton.disposeAndClear();
452 if ( _bReLayout )
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();
470 // #99102# ---------
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 );
486 return 0L;
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();
504 return 0;
507 } // namespace pcr
510 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */