| 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 | |
| 22 | #include <uielement/fontsizemenucontroller.hxx> |
| 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> |
| 37 | #include <toolkit/awt/vclxmenu.hxx> |
| 38 | #include <osl/mutex.hxx> |
| 39 | #include <memory> |
| 40 | #include <cppuhelper/supportsservice.hxx> |
| 41 | |
| 42 | // Defines |
| 43 | |
| 44 | using namespace com::sun::star::uno; |
| 45 | using namespace com::sun::star::lang; |
| 46 | using namespace com::sun::star::frame; |
| 47 | using namespace com::sun::star::beans; |
| 48 | using namespace com::sun::star::util; |
| 49 | using namespace com::sun::star::view; |
| 50 | |
| 51 | namespace framework |
| 52 | { |
| 53 | |
| 54 | OUString SAL_CALL FontSizeMenuController::getImplementationName() |
| 55 | { |
| 56 | return u"com.sun.star.comp.framework.FontSizeMenuController"_ustr; |
| 57 | } |
| 58 | |
| 59 | sal_Bool SAL_CALL FontSizeMenuController::supportsService( const OUString& sServiceName ) |
| 60 | { |
| 61 | return cppu::supportsService(this, sServiceName); |
| 62 | } |
| 63 | |
| 64 | css::uno::Sequence< OUString > SAL_CALL FontSizeMenuController::getSupportedServiceNames() |
| 65 | { |
| 66 | return { SERVICENAME_POPUPMENUCONTROLLER }; |
| 67 | } |
| 68 | |
| 69 | FontSizeMenuController::FontSizeMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : |
| 70 | svt::PopupMenuControllerBase( xContext ) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | FontSizeMenuController::~FontSizeMenuController() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | // private function |
| 79 | OUString FontSizeMenuController::retrievePrinterName( css::uno::Reference< css::frame::XFrame > const & rFrame ) |
| 80 | { |
| 81 | OUString aPrinterName; |
| 82 | |
| 83 | if ( rFrame.is() ) |
| 84 | { |
| 85 | Reference< XController > xController = m_xFrame->getController(); |
| 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 |
| 108 | void FontSizeMenuController::setCurHeight( tools::Long nHeight, Reference< css::awt::XPopupMenu > const & rPopupMenu ) |
| 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 |
| 132 | void FontSizeMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu ) |
| 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 OUString aFontHeightCommand( u".uno:FontHeight?FontHeight.Height:float="_ustr ); |
| 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 |
| 185 | const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper(); |
| 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 |
| 207 | void SAL_CALL FontSizeMenuController::disposing( const EventObject& ) |
| 208 | { |
| 209 | Reference< css::awt::XMenuListener > xHolder(this); |
| 210 | |
| 211 | std::unique_lock aLock( m_aMutex ); |
| 212 | m_xFrame.clear(); |
| 213 | m_xDispatch.clear(); |
| 214 | m_xCurrentFontDispatch.clear(); |
| 215 | if ( m_xPopupMenu.is() ) |
| 216 | m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) ); |
| 217 | m_xPopupMenu.clear(); |
| 218 | } |
| 219 | |
| 220 | // XStatusListener |
| 221 | void 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 |
| 247 | void FontSizeMenuController::impl_setPopupMenu(std::unique_lock<std::mutex>& /*rGuard*/) |
| 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 | |
| 257 | void SAL_CALL FontSizeMenuController::updatePopupMenu() |
| 258 | { |
| 259 | std::unique_lock aLock( m_aMutex ); |
| 260 | |
| 261 | throwIfDisposed(aLock); |
| 262 | |
| 263 | Reference< XDispatch > xDispatch( m_xCurrentFontDispatch ); |
| 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 | |
| 279 | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
| 280 | framework_FontSizeMenuController_get_implementation( |
| 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: */ |