LibreOffice Module framework (master) 1
fontsizemenucontroller.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 <sal/config.h>
21
23
24#include <services.h>
25
26#include <com/sun/star/awt/MenuItemStyle.hpp>
27#include <com/sun/star/beans/PropertyValue.hpp>
28#include <com/sun/star/frame/XDispatchProvider.hpp>
29#include <com/sun/star/view/XPrintable.hpp>
30#include <com/sun/star/util/XURLTransformer.hpp>
31
32#include <vcl/svapp.hxx>
33#include <vcl/i18nhelp.hxx>
34#include <vcl/print.hxx>
35#include <vcl/settings.hxx>
36#include <svtools/ctrltool.hxx>
38#include <osl/mutex.hxx>
39#include <memory>
41
42// Defines
43
44using namespace com::sun::star::uno;
45using namespace com::sun::star::lang;
46using namespace com::sun::star::frame;
47using namespace com::sun::star::beans;
48using namespace com::sun::star::util;
49using namespace com::sun::star::view;
50
51namespace framework
52{
53
55{
56 return "com.sun.star.comp.framework.FontSizeMenuController";
57}
58
59sal_Bool SAL_CALL FontSizeMenuController::supportsService( const OUString& sServiceName )
60{
62}
63
64css::uno::Sequence< OUString > SAL_CALL FontSizeMenuController::getSupportedServiceNames()
65{
67}
68
69FontSizeMenuController::FontSizeMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
70 svt::PopupMenuControllerBase( xContext )
71{
72}
73
75{
76}
77
78// private function
79OUString FontSizeMenuController::retrievePrinterName( css::uno::Reference< css::frame::XFrame > const & rFrame )
80{
81 OUString aPrinterName;
82
83 if ( rFrame.is() )
84 {
86 if ( xController.is() )
87 {
88 Reference< XPrintable > xPrintable( xController->getModel(), UNO_QUERY );
89 if ( xPrintable.is() )
90 {
91 const Sequence< PropertyValue > aPrinterSeq = xPrintable->getPrinter();
92 for ( PropertyValue const & prop : aPrinterSeq )
93 {
94 if ( prop.Name == "Name" )
95 {
96 prop.Value >>= aPrinterName;
97 break;
98 }
99 }
100 }
101 }
102 }
103
104 return aPrinterName;
105}
106
107// private function
109{
110 // check menu item
111 sal_uInt16 nChecked = 0;
112 sal_uInt16 nItemCount = rPopupMenu->getItemCount();
113 for( sal_uInt16 i = 0; i < nItemCount; i++ )
114 {
115 sal_uInt16 nItemId = rPopupMenu->getItemId( i );
116
117 if ( m_aHeightArray[i] == nHeight )
118 {
119 rPopupMenu->checkItem( nItemId, true );
120 return;
121 }
122
123 if ( rPopupMenu->isItemChecked( nItemId ) )
124 nChecked = nItemId;
125 }
126
127 if ( nChecked )
128 rPopupMenu->checkItem( nChecked, false );
129}
130
131// private function
133{
134 resetPopupMenu( rPopupMenu );
135
136 std::unique_ptr<FontList> pFontList;
137 ScopedVclPtr<Printer> pInfoPrinter;
138 OUString aPrinterName;
139
140 SolarMutexGuard aSolarMutexGuard;
141
142 // try to retrieve printer name of document
143 aPrinterName = retrievePrinterName( m_xFrame );
144 if ( !aPrinterName.isEmpty() )
145 {
146 pInfoPrinter.disposeAndReset(VclPtr<Printer>::Create( aPrinterName ));
147 if ( pInfoPrinter && pInfoPrinter->GetFontFaceCollectionCount() > 0 )
148 pFontList.reset(new FontList( pInfoPrinter.get() ));
149 }
150
151 if ( !pFontList )
152 pFontList.reset(new FontList( Application::GetDefaultDevice() ));
153
154 // setup font size array
155 m_aHeightArray.clear();
156
157 sal_uInt16 nPos = 0; // Id is nPos+1
158 static constexpr OUStringLiteral aFontHeightCommand( u".uno:FontHeight?FontHeight.Height:float=" );
159
160 // first insert font size names (for simplified/traditional chinese)
161 FontSizeNames aFontSizeNames( Application::GetSettings().GetUILanguageTag().getLanguageType() );
162 OUString aCommand;
163
164 if (!aFontSizeNames.IsEmpty())
165 {
166 // for scalable fonts all font size names
167 sal_Int32 nCount = aFontSizeNames.Count();
168 for( sal_Int32 i = 0; i < nCount; i++ )
169 {
170 OUString aSizeName = aFontSizeNames.GetIndexName( i );
171 sal_Int32 nSize = aFontSizeNames.GetIndexSize( i );
172 m_aHeightArray.push_back(nSize);
173 rPopupMenu->insertItem(nPos + 1, aSizeName, css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, nPos);
174
175 // Create dispatchable .uno command and set it
176 float fPoint = float(nSize) / 10;
177 aCommand = aFontHeightCommand + OUString::number( fPoint );
178 rPopupMenu->setCommand(nPos + 1, aCommand);
179
180 ++nPos;
181 }
182 }
183
184 // then insert numerical font size values
186 const int* pAry = FontList::GetStdSizeAry();
187 const int* pTempAry = pAry;
188 while ( *pTempAry )
189 {
190 m_aHeightArray.push_back(*pTempAry);
191 rPopupMenu->insertItem(nPos + 1, rI18nHelper.GetNum(*pTempAry, 1, true, false),
192 css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, nPos);
193
194 // Create dispatchable .uno command and set it
195 float fPoint = float(*pTempAry) / 10;
196 aCommand = aFontHeightCommand + OUString::number( fPoint );
197 rPopupMenu->setCommand(nPos + 1, aCommand);
198
199 ++nPos;
200 pTempAry++;
201 }
202
203 setCurHeight( tools::Long( m_aFontHeight.Height * 10), rPopupMenu );
204}
205
206// XEventListener
207void SAL_CALL FontSizeMenuController::disposing( const EventObject& )
208{
210
211 std::unique_lock aLock( m_aMutex );
212 m_xFrame.clear();
213 m_xDispatch.clear();
215 if ( m_xPopupMenu.is() )
216 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
217 m_xPopupMenu.clear();
218}
219
220// XStatusListener
221void SAL_CALL FontSizeMenuController::statusChanged( const FeatureStateEvent& Event )
222{
223 css::awt::FontDescriptor aFontDescriptor;
224 css::frame::status::FontHeight aFontHeight;
225
226 if ( Event.State >>= aFontDescriptor )
227 {
228 std::unique_lock aLock( m_aMutex );
229
230 if ( m_xPopupMenu.is() )
231 fillPopupMenu( m_xPopupMenu );
232 }
233 else if ( Event.State >>= aFontHeight )
234 {
235 std::unique_lock aLock( m_aMutex );
236 m_aFontHeight = aFontHeight;
237
238 if ( m_xPopupMenu.is() )
239 {
240 SolarMutexGuard aSolarMutexGuard;
241 setCurHeight( tools::Long( m_aFontHeight.Height * 10), m_xPopupMenu );
242 }
243 }
244}
245
246// XPopupMenuController
248{
249 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
250 css::util::URL aTargetURL;
251 // Register for font name updates which gives us info about the current font!
252 aTargetURL.Complete = ".uno:CharFontName";
253 m_xURLTransformer->parseStrict( aTargetURL );
254 m_xCurrentFontDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
255}
256
258{
259 std::unique_lock aLock( m_aMutex );
260
261 throwIfDisposed(aLock);
262
264 css::util::URL aTargetURL;
265 aTargetURL.Complete = ".uno:CharFontName";
266 m_xURLTransformer->parseStrict( aTargetURL );
267 aLock.unlock();
268
269 if ( xDispatch.is() )
270 {
271 xDispatch->addStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
272 xDispatch->removeStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
273 }
274
275 svt::PopupMenuControllerBase::updatePopupMenu();
276}
277}
278
279extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
281 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
282{
283 return cppu::acquire(new framework::FontSizeMenuController(context));
284}
285
286/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
const vcl::I18nHelper & GetUILocaleI18nHelper() const
static OutputDevice * GetDefaultDevice()
static const AllSettings & GetSettings()
static const int * GetStdSizeAry()
void disposeAndReset(reference_type *pBody)
reference_type * get() const
void fillPopupMenu(css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu)
FontSizeMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual void SAL_CALL updatePopupMenu() override
css::uno::Reference< css::frame::XDispatch > m_xCurrentFontDispatch
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
void setCurHeight(tools::Long nHeight, css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu)
std::vector< tools::Long > m_aHeightArray
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
css::frame::status::FontHeight m_aFontHeight
OUString retrievePrinterName(css::uno::Reference< css::frame::XFrame > const &rFrame)
OUString GetNum(tools::Long nNumber, sal_uInt16 nDecimals, bool bUseThousandSep=true, bool bTrailingZeros=true) const
int nCount
Reference< XDispatch > xDispatch
float u
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_FontSizeMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
sal_uInt16 nPos
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
int i
long Long
Reference< XController > xController
OUString aCommand
unsigned char sal_Bool
std::mutex m_aMutex
OUString aTargetURL