Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / statusbaritem.cxx
blobd9c4b2ccfd21bf10a012543395681ac794172648
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/statusbaritem.hxx>
21 #include <utility>
22 #include <vcl/status.hxx>
23 #include <vcl/svapp.hxx>
25 #include <com/sun/star/ui/ItemStyle.hpp>
27 using namespace com::sun::star::ui;
29 namespace framework
32 namespace
34 sal_uInt16 impl_convertItemBitsToItemStyle( StatusBarItemBits nItemBits )
36 sal_uInt16 nStyle( 0 );
38 if ( nItemBits & StatusBarItemBits::Right )
39 nStyle |= ItemStyle::ALIGN_RIGHT;
40 else if ( nItemBits & StatusBarItemBits::Left )
41 nStyle |= ItemStyle::ALIGN_LEFT;
42 else
43 nStyle |= ItemStyle::ALIGN_CENTER;
45 if ( nItemBits & StatusBarItemBits::Flat )
46 nStyle |= ItemStyle::DRAW_FLAT;
47 else if ( nItemBits & StatusBarItemBits::Out )
48 nStyle |= ItemStyle::DRAW_OUT3D;
49 else
50 nStyle |= ItemStyle::DRAW_IN3D;
52 if ( nItemBits & StatusBarItemBits::AutoSize )
53 nStyle |= ItemStyle::AUTO_SIZE;
55 if ( nItemBits & StatusBarItemBits::UserDraw )
56 nStyle |= ItemStyle::OWNER_DRAW;
58 return nStyle;
62 StatusbarItem::StatusbarItem(
63 StatusBar *pStatusBar,
64 sal_uInt16 nId,
65 OUString aCommand )
66 : m_pStatusBar( pStatusBar )
67 , m_nId( nId )
68 , m_nStyle( 0 )
69 , m_aCommand(std::move( aCommand ))
71 if ( m_pStatusBar )
72 m_nStyle = impl_convertItemBitsToItemStyle(
73 m_pStatusBar->GetItemBits( m_nId ) );
76 StatusbarItem::~StatusbarItem()
80 void StatusbarItem::disposing(std::unique_lock<std::mutex>&)
82 m_pStatusBar = nullptr;
85 OUString SAL_CALL StatusbarItem::getCommand()
87 return m_aCommand;
90 ::sal_uInt16 SAL_CALL StatusbarItem::getItemId()
92 return m_nId;
95 ::sal_uInt32 SAL_CALL StatusbarItem::getWidth()
97 SolarMutexGuard aGuard;
98 if ( m_pStatusBar )
99 return m_pStatusBar->GetItemWidth( m_nId );
101 return ::sal_uInt32(0);
104 ::sal_uInt16 SAL_CALL StatusbarItem::getStyle()
106 std::unique_lock aGuard( m_aMutex );
107 return m_nStyle;
110 ::sal_Int32 SAL_CALL StatusbarItem::getOffset()
112 SolarMutexGuard aGuard;
113 if ( m_pStatusBar )
114 return m_pStatusBar->GetItemOffset( m_nId );
116 return 0;
119 css::awt::Rectangle SAL_CALL StatusbarItem::getItemRect()
121 SolarMutexGuard aGuard;
122 css::awt::Rectangle aAWTRect;
123 if ( m_pStatusBar )
125 tools::Rectangle aRect = m_pStatusBar->GetItemRect( m_nId );
126 return css::awt::Rectangle( aRect.Left(),
127 aRect.Top(),
128 aRect.GetWidth(),
129 aRect.GetHeight() );
132 return aAWTRect;
135 OUString SAL_CALL StatusbarItem::getText()
137 SolarMutexGuard aGuard;
138 if ( m_pStatusBar )
139 return m_pStatusBar->GetItemText( m_nId );
141 return OUString();
144 void SAL_CALL StatusbarItem::setText( const OUString& rText )
146 SolarMutexGuard aGuard;
147 if ( m_pStatusBar )
148 m_pStatusBar->SetItemText( m_nId, rText );
151 OUString SAL_CALL StatusbarItem::getHelpText()
153 SolarMutexGuard aGuard;
154 if ( m_pStatusBar )
155 return m_pStatusBar->GetHelpText( m_nId );
157 return OUString();
160 void SAL_CALL StatusbarItem::setHelpText( const OUString& rHelpText )
162 SolarMutexGuard aGuard;
163 if ( m_pStatusBar )
164 m_pStatusBar->SetHelpText( m_nId, rHelpText );
167 OUString SAL_CALL StatusbarItem::getQuickHelpText()
169 SolarMutexGuard aGuard;
170 if ( m_pStatusBar )
171 return m_pStatusBar->GetHelpText( m_nId );
173 return OUString();
176 void SAL_CALL StatusbarItem::setQuickHelpText( const OUString& rQuickHelpText )
178 SolarMutexGuard aGuard;
179 if ( m_pStatusBar )
180 m_pStatusBar->SetQuickHelpText( m_nId, rQuickHelpText );
183 OUString SAL_CALL StatusbarItem::getAccessibleName()
185 SolarMutexGuard aGuard;
186 if ( m_pStatusBar )
187 return m_pStatusBar->GetAccessibleName( m_nId );
189 return OUString();
192 void SAL_CALL StatusbarItem::setAccessibleName( const OUString& rAccessibleName )
194 SolarMutexGuard aGuard;
195 if ( m_pStatusBar )
196 m_pStatusBar->SetAccessibleName( m_nId, rAccessibleName );
199 sal_Bool SAL_CALL StatusbarItem::getVisible()
201 SolarMutexGuard aGuard;
202 if ( m_pStatusBar )
203 return m_pStatusBar->IsItemVisible( m_nId );
205 return false;
208 void SAL_CALL StatusbarItem::setVisible( sal_Bool bVisible )
210 SolarMutexGuard aGuard;
211 if ( !m_pStatusBar )
212 return;
214 if ( bool(bVisible) != m_pStatusBar->IsItemVisible( m_nId ) )
216 if ( bVisible )
217 m_pStatusBar->ShowItem( m_nId );
218 else
219 m_pStatusBar->HideItem( m_nId );
223 void SAL_CALL StatusbarItem::repaint( )
225 SolarMutexGuard aGuard;
226 if ( m_pStatusBar )
228 m_pStatusBar->RedrawItem( m_nId );
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */