LibreOffice Module UnoControls (master) 1
statusindicator.cxx
Go to the documentation of this file.
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 <statusindicator.hxx>
21
22#include <com/sun/star/awt/PosSize.hpp>
23#include <com/sun/star/awt/XFixedText.hpp>
24#include <com/sun/star/uno/XComponentContext.hpp>
27
28#include <progressbar.hxx>
29
30using namespace ::cppu;
31using namespace ::osl;
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::lang;
34using namespace ::com::sun::star::awt;
35using namespace ::com::sun::star::task;
36
37constexpr OUStringLiteral FIXEDTEXT_SERVICENAME = u"com.sun.star.awt.UnoControlFixedText";
38constexpr OUStringLiteral FIXEDTEXT_MODELNAME = u"com.sun.star.awt.UnoControlFixedTextModel";
39constexpr OUStringLiteral CONTROLNAME_TEXT = u"Text"; // identifier the control in container
40constexpr OUStringLiteral CONTROLNAME_PROGRESSBAR = u"ProgressBar"; // -||-
41
42namespace unocontrols {
43
44// construct/destruct
45
46StatusIndicator::StatusIndicator( const css::uno::Reference< XComponentContext >& rxContext )
47 : BaseContainerControl ( rxContext )
48{
49 // It's not allowed to work with member in this method (refcounter !!!)
50 // But with a HACK (++refcount) its "OK" :-(
51 osl_atomic_increment(&m_refCount);
52
53 // Create instances for fixedtext and progress ...
54 m_xText.set( rxContext->getServiceManager()->createInstanceWithContext( FIXEDTEXT_SERVICENAME, rxContext ), UNO_QUERY );
55 m_xProgressBar = new ProgressBar(rxContext);
56 // ... cast controls to css::uno::Reference< XControl > and set model ...
57 // ( ProgressBar has no model !!! )
58 css::uno::Reference< XControl > xTextControl ( m_xText , UNO_QUERY );
59 xTextControl->setModel( css::uno::Reference< XControlModel >( rxContext->getServiceManager()->createInstanceWithContext( FIXEDTEXT_MODELNAME, rxContext ), UNO_QUERY ) );
60 // ... and add controls to basecontainercontrol!
61 addControl( CONTROLNAME_TEXT, xTextControl );
62 addControl( CONTROLNAME_PROGRESSBAR, m_xProgressBar );
63 // FixedText make it automatically visible by himself ... but not the progressbar !!!
64 // it must be set explicitly
65 m_xProgressBar->setVisible( true );
66 // Reset to defaults !!!
67 // (progressbar take automatically its own defaults)
68 m_xText->setText( "" );
69
70 osl_atomic_decrement(&m_refCount);
71}
72
74
75// XInterface
76
78{
79 // Ask for my own supported interfaces ...
80 // Attention: XTypeProvider and XInterface are supported by WeakComponentImplHelper!
81 Any aReturn ( ::cppu::queryInterface( rType ,
82 static_cast< XLayoutConstrains* > ( this ) ,
83 static_cast< XStatusIndicator* > ( this )
84 )
85 );
86
87 // If searched interface not supported by this class ...
88 if ( !aReturn.hasValue() )
89 {
90 // ... ask baseclasses.
91 aReturn = BaseControl::queryInterface( rType );
92 }
93
94 return aReturn;
95}
96
97// XInterface
98
99void SAL_CALL StatusIndicator::acquire() noexcept
100{
101 // Attention:
102 // Don't use mutex or guard in this method!!! Is a method of XInterface.
103
104 // Forward to baseclass
106}
107
108// XInterface
109
110void SAL_CALL StatusIndicator::release() noexcept
111{
112 // Attention:
113 // Don't use mutex or guard in this method!!! Is a method of XInterface.
114
115 // Forward to baseclass
117}
118
119// XTypeProvider
120
121Sequence< Type > SAL_CALL StatusIndicator::getTypes()
122{
123 static OTypeCollection ourTypeCollection(
127
128 return ourTypeCollection.getTypes();
129}
130
131// XStatusIndicator
132
133void SAL_CALL StatusIndicator::start( const OUString& sText, sal_Int32 nRange )
134{
135 // Ready for multithreading
136 MutexGuard aGuard( m_aMutex );
137
138 // Initialize status controls with given values.
139 m_xText->setText( sText );
140 m_xProgressBar->setRange( 0, nRange );
141 // force repaint ... fixedtext has changed !
142 impl_recalcLayout ( WindowEvent(static_cast< OWeakObject* >(this),0,0,impl_getWidth(),impl_getHeight(),0,0,0,0) );
143}
144
145// XStatusIndicator
146
147void SAL_CALL StatusIndicator::end()
148{
149 // Ready for multithreading
150 MutexGuard aGuard( m_aMutex );
151
152 // Clear values of status controls.
153 m_xText->setText( OUString() );
154 m_xProgressBar->setValue( 0 );
155 setVisible( false );
156}
157
158// XStatusIndicator
159
160void SAL_CALL StatusIndicator::setText( const OUString& sText )
161{
162 // Ready for multithreading
163 MutexGuard aGuard( m_aMutex );
164
165 // Take text on right control
166 m_xText->setText( sText );
167}
168
169// XStatusIndicator
170
171void SAL_CALL StatusIndicator::setValue( sal_Int32 nValue )
172{
173 // Ready for multithreading
174 MutexGuard aGuard( m_aMutex );
175
176 // Take value on right control
177 m_xProgressBar->setValue( nValue );
178}
179
180// XStatusIndicator
181
183{
184 // Ready for multithreading
185 MutexGuard aGuard( m_aMutex );
186
187 // Clear values of status controls.
188 // (Don't hide the window! User will reset current values ... but he will not finish using of indicator!)
189 m_xText->setText( OUString() );
190 m_xProgressBar->setValue( 0 );
191}
192
193// XLayoutConstrains
194
196{
198}
199
200// XLayoutConstrains
201
203{
204 // Ready for multithreading
205 ClearableMutexGuard aGuard ( m_aMutex );
206
207 // get information about required place of child controls
208 css::uno::Reference< XLayoutConstrains > xTextLayout ( m_xText, UNO_QUERY );
209 Size aTextSize = xTextLayout->getPreferredSize();
210
211 aGuard.clear ();
212
213 // calc preferred size of status indicator
214 sal_Int32 nWidth = impl_getWidth();
215 sal_Int32 nHeight = (2*STATUSINDICATOR_FREEBORDER)+aTextSize.Height;
216
217 // norm to minimum
219 {
221 }
222 if ( nHeight<STATUSINDICATOR_DEFAULT_HEIGHT )
223 {
225 }
226
227 // return to caller
228 return Size ( nWidth, nHeight );
229}
230
231// XLayoutConstrains
232
233Size SAL_CALL StatusIndicator::calcAdjustedSize ( const Size& /*rNewSize*/ )
234{
235 return getPreferredSize ();
236}
237
238// XControl
239
241 const css::uno::Reference< XToolkit > & rToolkit,
242 const css::uno::Reference< XWindowPeer > & rParent
243)
244{
245 if( !getPeer().is() )
246 {
247 BaseContainerControl::createPeer( rToolkit, rParent );
248
249 // If user forget to call "setPosSize()", we have still a correct size.
250 // And a "MinimumSize" IS A "MinimumSize"!
251 // We change not the position of control at this point.
252 Size aDefaultSize = getMinimumSize ();
253 setPosSize ( 0, 0, aDefaultSize.Width, aDefaultSize.Height, PosSize::SIZE );
254 }
255}
256
257// XControl
258
259sal_Bool SAL_CALL StatusIndicator::setModel ( const css::uno::Reference< XControlModel > & /*rModel*/ )
260{
261 // We have no model.
262 return false;
263}
264
265// XControl
266
267css::uno::Reference< XControlModel > SAL_CALL StatusIndicator::getModel ()
268{
269 // We have no model.
270 // return (XControlModel*)this;
271 return css::uno::Reference< XControlModel > ();
272}
273
274// XComponent
275
277{
278 // Ready for multithreading
279 MutexGuard aGuard ( m_aMutex );
280
281 // "removeControl()" control the state of a reference
282 css::uno::Reference< XControl > xTextControl ( m_xText , UNO_QUERY );
283
284 removeControl( xTextControl );
286
287 // don't use "...->clear ()" or "... = XFixedText ()"
288 // when other hold a reference at this object !!!
289 xTextControl->dispose();
290 m_xProgressBar->dispose();
292}
293
294// XWindow
295
297 sal_Int32 nX,
298 sal_Int32 nY,
299 sal_Int32 nWidth,
300 sal_Int32 nHeight,
301 sal_Int16 nFlags
302)
303{
304 Rectangle aBasePosSize = getPosSize ();
305 BaseContainerControl::setPosSize (nX, nY, nWidth, nHeight, nFlags);
306
307 // if position or size changed
308 if (
309 ( nWidth != aBasePosSize.Width ) ||
310 ( nHeight != aBasePosSize.Height)
311 )
312 {
313 // calc new layout for controls
314 impl_recalcLayout ( WindowEvent(static_cast< OWeakObject* >(this),0,0,nWidth,nHeight,0,0,0,0) );
315 // clear background (!)
316 // [Children were repainted in "recalcLayout" by setPosSize() automatically!]
317 getPeer()->invalidate(2);
318 // and repaint the control
320 }
321}
322
323// protected method
324
325WindowDescriptor StatusIndicator::impl_getWindowDescriptor( const css::uno::Reference< XWindowPeer >& xParentPeer )
326{
327 WindowDescriptor aDescriptor;
328
329 aDescriptor.Type = WindowClass_SIMPLE;
330 aDescriptor.WindowServiceName = "floatingwindow";
331 aDescriptor.ParentIndex = -1;
332 aDescriptor.Parent = xParentPeer;
333 aDescriptor.Bounds = getPosSize ();
334
335 return aDescriptor;
336}
337
338// protected method
339
340void StatusIndicator::impl_paint ( sal_Int32 nX, sal_Int32 nY, const css::uno::Reference< XGraphics > & rGraphics )
341{
342 // This paint method is not buffered!
343 // Every request paint the completely control. (But only, if peer exist)
344 if ( !rGraphics.is () )
345 return;
346
347 MutexGuard aGuard (m_aMutex);
348
349 // background = gray
350 css::uno::Reference< XWindowPeer > xPeer( impl_getPeerWindow(), UNO_QUERY );
351 if( xPeer.is() )
352 xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
353
354 // FixedText background = gray
355 css::uno::Reference< XControl > xTextControl( m_xText, UNO_QUERY );
356 xPeer = xTextControl->getPeer();
357 if( xPeer.is() )
358 xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
359
360 // Progress background = gray
361 xPeer = m_xProgressBar->getPeer();
362 if( xPeer.is() )
363 xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
364
365 // paint shadow border
366 rGraphics->setLineColor ( STATUSINDICATOR_LINECOLOR_BRIGHT );
367 rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
368 rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
369
370 rGraphics->setLineColor ( STATUSINDICATOR_LINECOLOR_SHADOW );
371 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
372 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
373}
374
375// protected method
376
377void StatusIndicator::impl_recalcLayout ( const WindowEvent& aEvent )
378{
379 sal_Int32 nX_ProgressBar;
380 sal_Int32 nY_ProgressBar;
381 sal_Int32 nWidth_ProgressBar;
382 sal_Int32 nHeight_ProgressBar;
383 sal_Int32 nX_Text;
384 sal_Int32 nY_Text;
385 sal_Int32 nWidth_Text;
386 sal_Int32 nHeight_Text;
387
388 // Ready for multithreading
389 MutexGuard aGuard ( m_aMutex );
390
391 // get information about required place of child controls
392 Size aWindowSize ( aEvent.Width, aEvent.Height );
393 css::uno::Reference< XLayoutConstrains > xTextLayout ( m_xText, UNO_QUERY );
394 Size aTextSize = xTextLayout->getPreferredSize();
395
396 if( aWindowSize.Width < STATUSINDICATOR_DEFAULT_WIDTH )
397 {
399 }
400 if( aWindowSize.Height < STATUSINDICATOR_DEFAULT_HEIGHT )
401 {
403 }
404
405 // calc position and size of child controls
408 nWidth_Text = aTextSize.Width;
409 nHeight_Text = aTextSize.Height;
410
411 nX_ProgressBar = nX_Text+nWidth_Text+STATUSINDICATOR_FREEBORDER;
412 nY_ProgressBar = nY_Text;
413 nWidth_ProgressBar = aWindowSize.Width-nWidth_Text-(3*STATUSINDICATOR_FREEBORDER);
414 nHeight_ProgressBar = nHeight_Text;
415
416 // Set new position and size on all controls
417 css::uno::Reference< XWindow > xTextWindow ( m_xText , UNO_QUERY );
418
419 xTextWindow->setPosSize ( nX_Text , nY_Text , nWidth_Text , nHeight_Text , 15 );
420 m_xProgressBar->setPosSize( nX_ProgressBar, nY_ProgressBar, nWidth_ProgressBar, nHeight_ProgressBar, 15 );
421}
422
423} // namespace unocontrols
424
425extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
427 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
428{
429 return cppu::acquire(new unocontrols::StatusIndicator(context));
430}
431
432/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
constexpr tools::Long Height() const
constexpr tools::Long Width() const
mutable::osl::Mutex m_aMutex
virtual void SAL_CALL dispose() override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces @seealso XTypeProvider
virtual void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &xToolkit, const css::uno::Reference< css::awt::XWindowPeer > &xParent) override
virtual void SAL_CALL setVisible(sal_Bool bVisible) override
virtual void SAL_CALL removeControl(const css::uno::Reference< css::awt::XControl > &xControl) override
sal_Int32 impl_getWidth() const
const css::uno::Reference< css::awt::XWindow > & impl_getPeerWindow() const
virtual void SAL_CALL setPosSize(sal_Int32 nX, sal_Int32 nY, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nFlags) override
sal_Int32 impl_getHeight() const
const css::uno::Reference< css::awt::XGraphics > & impl_getGraphicsPeer() const
virtual void SAL_CALL release() noexcept override
decrement refcount @seealso XInterface @seealso acquire() @onerror A RuntimeException is thrown.
virtual css::uno::Reference< css::awt::XWindowPeer > SAL_CALL getPeer() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported @descr The interfaces are searched by type.
Definition: basecontrol.cxx:70
virtual css::awt::Rectangle SAL_CALL getPosSize() override
virtual void SAL_CALL acquire() noexcept override
increment refcount @seealso XInterface @seealso release() @onerror A RuntimeException is thrown.
Definition: basecontrol.cxx:99
virtual css::uno::Reference< css::awt::XControlModel > SAL_CALL getModel() override
virtual void SAL_CALL acquire() noexcept override
increment refcount @seealso XInterface @seealso release() @onerror A RuntimeException is thrown.
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces @seealso XTypeProvider
virtual void SAL_CALL reset() override
virtual void SAL_CALL setText(const OUString &sText) override
virtual void SAL_CALL setValue(sal_Int32 nValue) override
virtual void SAL_CALL release() noexcept override
decrement refcount @seealso XInterface @seealso acquire() @onerror A RuntimeException is thrown.
virtual ~StatusIndicator() override
virtual sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &xModel) override
virtual void SAL_CALL setPosSize(sal_Int32 nX, sal_Int32 nY, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nFlags) override
virtual void SAL_CALL start(const OUString &sText, sal_Int32 nRange) override
virtual css::awt::Size SAL_CALL getPreferredSize() override
StatusIndicator(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
css::uno::Reference< css::awt::XFixedText > m_xText
virtual css::awt::Size SAL_CALL calcAdjustedSize(const css::awt::Size &aNewSize) override
virtual css::awt::Size SAL_CALL getMinimumSize() override
virtual void impl_paint(sal_Int32 nX, sal_Int32 nY, const css::uno::Reference< css::awt::XGraphics > &rGraphics) override
virtual css::awt::WindowDescriptor impl_getWindowDescriptor(const css::uno::Reference< css::awt::XWindowPeer > &xParentPeer) override
virtual void SAL_CALL dispose() override
virtual void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &xToolkit, const css::uno::Reference< css::awt::XWindowPeer > &xParent) override
virtual void SAL_CALL end() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported @descr The interfaces are searched by type.
rtl::Reference< ProgressBar > m_xProgressBar
virtual void impl_recalcLayout(const css::awt::WindowEvent &aEvent) override
ULONG m_refCount
float u
sal_Int16 nValue
if(aStr !=aBuf) UpdateName_Impl(m_xFollowLb.get()
Type
constexpr auto STATUSINDICATOR_FREEBORDER
constexpr auto STATUSINDICATOR_DEFAULT_WIDTH
constexpr auto STATUSINDICATOR_DEFAULT_HEIGHT
constexpr sal_Int32 STATUSINDICATOR_BACKGROUNDCOLOR
constexpr sal_Int32 STATUSINDICATOR_LINECOLOR_SHADOW
constexpr sal_Int32 STATUSINDICATOR_LINECOLOR_BRIGHT
constexpr OUStringLiteral FIXEDTEXT_SERVICENAME
constexpr OUStringLiteral CONTROLNAME_PROGRESSBAR
constexpr OUStringLiteral FIXEDTEXT_MODELNAME
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_UnoControls_StatusIndicator_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
constexpr OUStringLiteral CONTROLNAME_TEXT
unsigned char sal_Bool