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 <uielement/genericstatusbarcontroller.hxx>
21 #include <uielement/statusbarmerger.hxx>
22
23 #include <vcl/svapp.hxx>
24 #include <vcl/status.hxx>
25 #include <vcl/image.hxx>
26 #include <toolkit/helper/convert.hxx>
27
28 #include <com/sun/star/ui/ItemStyle.hpp>
29 #include <com/sun/star/ui/XStatusbarItem.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/awt/ImageDrawMode.hpp>
32 #include <com/sun/star/awt/XGraphics2.hpp>
33 #include <com/sun/star/graphic/GraphicType.hpp>
34
35 using namespace ::cppu;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::frame;
40
41 namespace framework
42 {
43
GenericStatusbarController(const Reference<XComponentContext> & rxContext,const Reference<XFrame> & rxFrame,const Reference<ui::XStatusbarItem> & rxItem,AddonStatusbarItemData * pItemData)44 GenericStatusbarController::GenericStatusbarController(
45 const Reference< XComponentContext >& rxContext,
46 const Reference< XFrame >& rxFrame,
47 const Reference< ui::XStatusbarItem >& rxItem,
48 AddonStatusbarItemData *pItemData )
49 : svt::StatusbarController( rxContext, rxFrame, OUString(), 0 )
50 , m_bEnabled( false )
51 , m_bOwnerDraw( false )
52 , m_pItemData( pItemData )
53 , m_xGraphic()
54 {
55 m_xStatusbarItem = rxItem;
56 if ( m_xStatusbarItem.is() )
57 {
58 assert(m_aCommandURL.pData);
59 m_aCommandURL = m_xStatusbarItem->getCommand();
60 m_nID = m_xStatusbarItem->getItemId();
61 m_bOwnerDraw = ( m_xStatusbarItem->getStyle() & ui::ItemStyle::OWNER_DRAW ) == ui::ItemStyle::OWNER_DRAW;
62 if ( !m_bOwnerDraw && m_pItemData && m_pItemData->aLabel.getLength() )
63 m_xStatusbarItem->setText( m_pItemData->aLabel );
64 }
65 }
66
~GenericStatusbarController()67 GenericStatusbarController::~GenericStatusbarController()
68 {
69 }
70
dispose()71 void SAL_CALL GenericStatusbarController::dispose()
72 {
73 svt::StatusbarController::dispose();
74
75 SolarMutexGuard aGuard;
76 m_pItemData = nullptr;
77 m_xGraphic.clear();
78 m_xStatusbarItem.clear();
79
80 }
81
statusChanged(const FeatureStateEvent & rEvent)82 void SAL_CALL GenericStatusbarController::statusChanged(
83 const FeatureStateEvent& rEvent)
84 {
85 SolarMutexGuard aGuard;
86
87 if ( m_bDisposed || !m_xStatusbarItem.is() )
88 return;
89
90 m_bEnabled = rEvent.IsEnabled;
91
92 OUString aStrValue;
93 Reference< graphic::XGraphic > aGraphic;
94
95 if ( rEvent.State >>= aStrValue )
96 {
97 if ( !m_bOwnerDraw )
98 m_xStatusbarItem->setText( aStrValue );
99 else
100 {
101 if ( aStrValue.getLength() )
102 {
103 m_xStatusbarItem->setQuickHelpText( aStrValue );
104 }
105 }
106 }
107 else if ( ( rEvent.State >>= aGraphic ) && m_bOwnerDraw )
108 {
109 m_xGraphic = aGraphic;
110 }
111
112 // when the status is updated, and the controller is responsible for
113 // painting the statusbar item content, we must trigger a repaint
114 if ( m_bOwnerDraw && m_xStatusbarItem->getVisible() )
115 {
116 m_xStatusbarItem->repaint();
117 }
118 }
119
paint(const Reference<awt::XGraphics> & xGraphics,const awt::Rectangle & rOutputRectangle,::sal_Int32)120 void SAL_CALL GenericStatusbarController::paint(
121 const Reference< awt::XGraphics >& xGraphics,
122 const awt::Rectangle& rOutputRectangle,
123 ::sal_Int32 /*nStyle*/ )
124 {
125 SolarMutexGuard aGuard;
126
127 const Reference< awt::XGraphics2 > xGraphics2(xGraphics, UNO_QUERY);
128
129 if ( !m_xStatusbarItem.is() || !xGraphics2.is() )
130 return;
131
132 Reference< beans::XPropertySet > xGraphicProps( m_xGraphic, UNO_QUERY );
133
134 if ( xGraphicProps.is() && m_xGraphic->getType() != graphic::GraphicType::EMPTY )
135 {
136 awt::Size aGraphicSize;
137 xGraphicProps->getPropertyValue( "SizePixel" ) >>= aGraphicSize;
138 OSL_ENSURE( aGraphicSize.Height > 0 && aGraphicSize.Width > 0, "Empty status bar graphic!" );
139
140 sal_Int32 nOffset = m_xStatusbarItem->getOffset( );
141 awt::Point aPos;
142 aPos.X = ( rOutputRectangle.Width + nOffset ) / 2 - aGraphicSize.Width / 2;
143 aPos.Y = rOutputRectangle.Height / 2 - aGraphicSize.Height / 2;
144
145 xGraphics2->drawImage( rOutputRectangle.X + aPos.X,
146 rOutputRectangle.Y + aPos.Y,
147 aGraphicSize.Width,
148 aGraphicSize.Height,
149 m_bEnabled ? awt::ImageDrawMode::NONE : awt::ImageDrawMode::DISABLE,
150 m_xGraphic );
151 }
152 else
153 {
154 xGraphics2->clear( rOutputRectangle );
155 }
156 }
157
158 }
159
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
161