Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / genericstatusbarcontroller.cxx
blob4a5aa46055cf69eaad7bc447c871bfd308f9531d
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 <uielement/genericstatusbarcontroller.hxx>
21 #include <uielement/statusbarmerger.hxx>
23 #include <osl/diagnose.h>
24 #include <vcl/svapp.hxx>
26 #include <com/sun/star/ui/ItemStyle.hpp>
27 #include <com/sun/star/ui/XStatusbarItem.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/awt/ImageDrawMode.hpp>
30 #include <com/sun/star/awt/XGraphics2.hpp>
31 #include <com/sun/star/graphic/GraphicType.hpp>
33 using namespace ::cppu;
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::frame;
39 namespace framework
42 GenericStatusbarController::GenericStatusbarController(
43 const Reference< XComponentContext >& rxContext,
44 const Reference< XFrame >& rxFrame,
45 const Reference< ui::XStatusbarItem >& rxItem,
46 AddonStatusbarItemData *pItemData )
47 : svt::StatusbarController( rxContext, rxFrame, OUString(), 0 )
48 , m_bEnabled( false )
49 , m_bOwnerDraw( false )
50 , m_pItemData( pItemData )
52 m_xStatusbarItem = rxItem;
53 if ( m_xStatusbarItem.is() )
55 assert(m_aCommandURL.pData);
56 m_aCommandURL = m_xStatusbarItem->getCommand();
57 m_nID = m_xStatusbarItem->getItemId();
58 m_bOwnerDraw = ( m_xStatusbarItem->getStyle() & ui::ItemStyle::OWNER_DRAW ) == ui::ItemStyle::OWNER_DRAW;
59 if ( !m_bOwnerDraw && m_pItemData && m_pItemData->aLabel.getLength() )
60 m_xStatusbarItem->setText( m_pItemData->aLabel );
64 GenericStatusbarController::~GenericStatusbarController()
68 void SAL_CALL GenericStatusbarController::dispose()
70 svt::StatusbarController::dispose();
72 SolarMutexGuard aGuard;
73 m_pItemData = nullptr;
74 m_xGraphic.clear();
75 m_xStatusbarItem.clear();
79 void SAL_CALL GenericStatusbarController::statusChanged(
80 const FeatureStateEvent& rEvent)
82 SolarMutexGuard aGuard;
84 if ( m_bDisposed || !m_xStatusbarItem.is() )
85 return;
87 m_bEnabled = rEvent.IsEnabled;
89 OUString aStrValue;
90 Reference< graphic::XGraphic > aGraphic;
92 if ( rEvent.State >>= aStrValue )
94 if ( !m_bOwnerDraw )
95 m_xStatusbarItem->setText( aStrValue );
96 else
98 if ( aStrValue.getLength() )
100 m_xStatusbarItem->setQuickHelpText( aStrValue );
104 else if ( ( rEvent.State >>= aGraphic ) && m_bOwnerDraw )
106 m_xGraphic = aGraphic;
109 // when the status is updated, and the controller is responsible for
110 // painting the statusbar item content, we must trigger a repaint
111 if ( m_bOwnerDraw && m_xStatusbarItem->getVisible() )
113 m_xStatusbarItem->repaint();
117 void SAL_CALL GenericStatusbarController::paint(
118 const Reference< awt::XGraphics >& xGraphics,
119 const awt::Rectangle& rOutputRectangle,
120 ::sal_Int32 /*nStyle*/ )
122 SolarMutexGuard aGuard;
124 const Reference< awt::XGraphics2 > xGraphics2(xGraphics, UNO_QUERY);
126 if ( !m_xStatusbarItem.is() || !xGraphics2.is() )
127 return;
129 Reference< beans::XPropertySet > xGraphicProps( m_xGraphic, UNO_QUERY );
131 if ( xGraphicProps.is() && m_xGraphic->getType() != graphic::GraphicType::EMPTY )
133 awt::Size aGraphicSize;
134 xGraphicProps->getPropertyValue( "SizePixel" ) >>= aGraphicSize;
135 OSL_ENSURE( aGraphicSize.Height > 0 && aGraphicSize.Width > 0, "Empty status bar graphic!" );
137 sal_Int32 nOffset = m_xStatusbarItem->getOffset( );
138 awt::Point aPos;
139 aPos.X = ( rOutputRectangle.Width + nOffset ) / 2 - aGraphicSize.Width / 2;
140 aPos.Y = rOutputRectangle.Height / 2 - aGraphicSize.Height / 2;
142 xGraphics2->drawImage( rOutputRectangle.X + aPos.X,
143 rOutputRectangle.Y + aPos.Y,
144 aGraphicSize.Width,
145 aGraphicSize.Height,
146 m_bEnabled ? awt::ImageDrawMode::NONE : awt::ImageDrawMode::DISABLE,
147 m_xGraphic );
149 else
151 xGraphics2->clear( rOutputRectangle );
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */