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