LCOV - code coverage report
Current view: top level - framework/source/uielement - fontsizemenucontroller.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 26 155 16.8 %
Date: 2015-06-13 12:38:46 Functions: 12 19 63.2 %
Legend: Lines: hit not hit

          Line data    Source code
       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/fontsizemenucontroller.hxx>
      21             : 
      22             : #include "services.h"
      23             : 
      24             : #include <com/sun/star/awt/XDevice.hpp>
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <com/sun/star/awt/MenuItemStyle.hpp>
      27             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      28             : #include <com/sun/star/view/XPrintable.hpp>
      29             : 
      30             : #include <vcl/menu.hxx>
      31             : #include <tools/mapunit.hxx>
      32             : #include <vcl/svapp.hxx>
      33             : #include <vcl/i18nhelp.hxx>
      34             : #include <vcl/outdev.hxx>
      35             : #include <vcl/print.hxx>
      36             : #include <vcl/settings.hxx>
      37             : #include <svtools/ctrltool.hxx>
      38             : #include <osl/mutex.hxx>
      39             : #include <boost/scoped_ptr.hpp>
      40             : 
      41             : //  Defines
      42             : 
      43             : using namespace com::sun::star::uno;
      44             : using namespace com::sun::star::lang;
      45             : using namespace com::sun::star::frame;
      46             : using namespace com::sun::star::beans;
      47             : using namespace com::sun::star::util;
      48             : using namespace com::sun::star::view;
      49             : 
      50             : namespace framework
      51             : {
      52             : 
      53          28 : DEFINE_XSERVICEINFO_MULTISERVICE_2      (   FontSizeMenuController                      ,
      54             :                                             OWeakObject                                 ,
      55             :                                             SERVICENAME_POPUPMENUCONTROLLER             ,
      56             :                                             IMPLEMENTATIONNAME_FONTSIZEMENUCONTROLLER
      57             :                                         )
      58             : 
      59           3 : DEFINE_INIT_SERVICE                     (   FontSizeMenuController, {} )
      60             : 
      61           3 : FontSizeMenuController::FontSizeMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext ) :
      62             :     svt::PopupMenuControllerBase( xContext ),
      63           3 :     m_pHeightArray( 0 )
      64             : {
      65           3 : }
      66             : 
      67           9 : FontSizeMenuController::~FontSizeMenuController()
      68             : {
      69           3 :     delete []m_pHeightArray;
      70           6 : }
      71             : 
      72             : // private function
      73           0 : OUString FontSizeMenuController::retrievePrinterName( com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame )
      74             : {
      75           0 :     OUString aPrinterName;
      76             : 
      77           0 :     if ( rFrame.is() )
      78             :     {
      79           0 :         Reference< XController > xController = m_xFrame->getController();
      80           0 :         if ( xController.is() )
      81             :         {
      82           0 :             Reference< XPrintable > xPrintable( xController->getModel(), UNO_QUERY );
      83           0 :             if ( xPrintable.is() )
      84             :             {
      85           0 :                 Sequence< PropertyValue > aPrinterSeq = xPrintable->getPrinter();
      86           0 :                 for ( int i = 0; i < aPrinterSeq.getLength(); i++ )
      87             :                 {
      88           0 :                     if ( aPrinterSeq[i].Name == "Name" )
      89             :                     {
      90           0 :                         aPrinterSeq[i].Value >>= aPrinterName;
      91           0 :                         break;
      92             :                     }
      93           0 :                 }
      94           0 :             }
      95           0 :         }
      96             :     }
      97             : 
      98           0 :     return aPrinterName;
      99             : }
     100             : 
     101             : // private function
     102           0 : void FontSizeMenuController::setCurHeight( long nHeight, Reference< css::awt::XPopupMenu >& rPopupMenu )
     103             : {
     104             :     // check menu item
     105           0 :     sal_uInt16          nChecked    = 0;
     106           0 :     sal_uInt16          nItemCount  = rPopupMenu->getItemCount();
     107           0 :     for( sal_uInt16 i = 0; i < nItemCount; i++ )
     108             :     {
     109           0 :         sal_uInt16 nItemId = rPopupMenu->getItemId( i );
     110             : 
     111           0 :         if ( m_pHeightArray[i] == nHeight )
     112             :         {
     113           0 :             rPopupMenu->checkItem( nItemId, sal_True );
     114           0 :             return;
     115             :         }
     116             : 
     117           0 :         if ( rPopupMenu->isItemChecked( nItemId ) )
     118           0 :             nChecked = nItemId;
     119             :     }
     120             : 
     121           0 :     if ( nChecked )
     122           0 :         rPopupMenu->checkItem( nChecked, sal_False );
     123             : }
     124             : 
     125             : // private function
     126           0 : void FontSizeMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
     127             : {
     128           0 :     VCLXPopupMenu*          pPopupMenu = static_cast<VCLXPopupMenu *>(VCLXMenu::GetImplementation( rPopupMenu ));
     129           0 :     PopupMenu*              pVCLPopupMenu = 0;
     130             : 
     131           0 :     resetPopupMenu( rPopupMenu );
     132           0 :     if ( pPopupMenu )
     133           0 :         pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
     134             : 
     135           0 :     if ( pVCLPopupMenu )
     136             :     {
     137           0 :         boost::scoped_ptr<FontList> pFontList;
     138           0 :         ScopedVclPtr<Printer>  pInfoPrinter;
     139           0 :         OUString   aPrinterName;
     140             : 
     141           0 :         SolarMutexGuard aSolarMutexGuard;
     142             : 
     143             :         // try to retrieve printer name of document
     144           0 :         aPrinterName = retrievePrinterName( m_xFrame );
     145           0 :         if ( !aPrinterName.isEmpty() )
     146             :         {
     147           0 :             pInfoPrinter.reset(VclPtr<Printer>::Create( aPrinterName ));
     148           0 :             if ( pInfoPrinter && pInfoPrinter->GetDevFontCount() > 0 )
     149           0 :                 pFontList.reset(new FontList( pInfoPrinter.get() ));
     150             :         }
     151             : 
     152           0 :         if ( !pFontList )
     153           0 :             pFontList.reset(new FontList( Application::GetDefaultDevice() ));
     154             : 
     155           0 :         vcl::FontInfo aFntInfo = pFontList->Get( m_aFontDescriptor.Name, m_aFontDescriptor.StyleName );
     156             : 
     157             :         // setup font size array
     158           0 :         if ( m_pHeightArray )
     159           0 :             delete m_pHeightArray;
     160             : 
     161             :         const sal_IntPtr* pTempAry;
     162           0 :         const sal_IntPtr* pAry = pFontList->GetSizeAry( aFntInfo );
     163           0 :         sal_uInt16 nSizeCount = 0;
     164           0 :         while ( pAry[nSizeCount] )
     165           0 :             nSizeCount++;
     166             : 
     167           0 :         sal_uInt16 nPos = 0;
     168           0 :         const OUString aFontHeightCommand( ".uno:FontHeight?FontHeight.Height:float=" );
     169             : 
     170             :         // first insert font size names (for simplified/traditional chinese)
     171             :         float           fPoint;
     172           0 :         FontSizeNames   aFontSizeNames( Application::GetSettings().GetUILanguageTag().getLanguageType() );
     173           0 :         m_pHeightArray = new long[nSizeCount+aFontSizeNames.Count()];
     174           0 :         OUString   aCommand;
     175             : 
     176           0 :         if ( !aFontSizeNames.IsEmpty() )
     177             :         {
     178           0 :             if ( pAry == FontList::GetStdSizeAry() )
     179             :             {
     180             :                 // for scalable fonts all font size names
     181           0 :                 sal_uLong nCount = aFontSizeNames.Count();
     182           0 :                 for( sal_uLong i = 0; i < nCount; i++ )
     183             :                 {
     184           0 :                     OUString  aSizeName = aFontSizeNames.GetIndexName( i );
     185           0 :                     long      nSize = aFontSizeNames.GetIndexSize( i );
     186           0 :                     m_pHeightArray[nPos] = nSize;
     187           0 :                     nPos++; // Id is nPos+1
     188           0 :                     pVCLPopupMenu->InsertItem( nPos, aSizeName, MenuItemBits::RADIOCHECK | MenuItemBits::AUTOCHECK );
     189           0 :                     fPoint = float( m_pHeightArray[nPos-1] ) / 10;
     190             : 
     191             :                     // Create dispatchable .uno command and set it
     192           0 :                     aCommand = aFontHeightCommand + OUString::number( fPoint );
     193           0 :                     pVCLPopupMenu->SetItemCommand( nPos, aCommand );
     194           0 :                 }
     195             :             }
     196             :             else
     197             :             {
     198             :                 // for fixed size fonts only selectable font size names
     199           0 :                 pTempAry = pAry;
     200           0 :                 while ( *pTempAry )
     201             :                 {
     202           0 :                     OUString aSizeName = aFontSizeNames.Size2Name( *pTempAry );
     203           0 :                     if ( !aSizeName.isEmpty() )
     204             :                     {
     205           0 :                         m_pHeightArray[nPos] = *pTempAry;
     206           0 :                         nPos++; // Id is nPos+1
     207           0 :                         pVCLPopupMenu->InsertItem( nPos, aSizeName, MenuItemBits::RADIOCHECK | MenuItemBits::AUTOCHECK );
     208           0 :                         fPoint = float( m_pHeightArray[nPos-1] ) / 10;
     209             : 
     210             :                         // Create dispatchable .uno command and set it
     211           0 :                         aCommand = aFontHeightCommand + OUString::number( fPoint );
     212           0 :                         pVCLPopupMenu->SetItemCommand( nPos, aCommand );
     213             :                     }
     214           0 :                     pTempAry++;
     215           0 :                 }
     216             :             }
     217             :         }
     218             : 
     219             :         // then insert numerical font size values
     220           0 :         const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
     221           0 :         pTempAry = pAry;
     222           0 :         while ( *pTempAry )
     223             :         {
     224           0 :             m_pHeightArray[nPos] = *pTempAry;
     225           0 :             nPos++; // Id is nPos+1
     226           0 :             pVCLPopupMenu->InsertItem( nPos, rI18nHelper.GetNum( *pTempAry, 1, true, false ), MenuItemBits::RADIOCHECK | MenuItemBits::AUTOCHECK );
     227           0 :             fPoint = float( m_pHeightArray[nPos-1] ) / 10;
     228             : 
     229             :             // Create dispatchable .uno command and set it
     230           0 :             aCommand = aFontHeightCommand + OUString::number( fPoint );
     231           0 :             pVCLPopupMenu->SetItemCommand( nPos, aCommand );
     232             : 
     233           0 :             pTempAry++;
     234             :         }
     235             : 
     236           0 :         setCurHeight( long( m_aFontHeight.Height * 10), rPopupMenu );
     237             :     }
     238           0 : }
     239             : 
     240             : // XEventListener
     241           0 : void SAL_CALL FontSizeMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
     242             : {
     243           0 :     Reference< css::awt::XMenuListener > xHolder(static_cast<OWeakObject *>(this), UNO_QUERY );
     244             : 
     245           0 :     osl::MutexGuard aLock( m_aMutex );
     246           0 :     m_xFrame.clear();
     247           0 :     m_xDispatch.clear();
     248           0 :     m_xCurrentFontDispatch.clear();
     249           0 :     if ( m_xPopupMenu.is() )
     250           0 :         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(static_cast<OWeakObject *>(this), UNO_QUERY ));
     251           0 :     m_xPopupMenu.clear();
     252           0 : }
     253             : 
     254             : // XStatusListener
     255           1 : void SAL_CALL FontSizeMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
     256             : {
     257           1 :     com::sun::star::awt::FontDescriptor                 aFontDescriptor;
     258           1 :     ::com::sun::star::frame::status::FontHeight   aFontHeight;
     259             : 
     260           1 :     if ( Event.State >>= aFontDescriptor )
     261             :     {
     262           0 :         osl::MutexGuard aLock( m_aMutex );
     263           0 :         m_aFontDescriptor = aFontDescriptor;
     264             : 
     265           0 :         if ( m_xPopupMenu.is() )
     266           0 :             fillPopupMenu( m_xPopupMenu );
     267             : 
     268             :     }
     269           1 :     else if ( Event.State >>= aFontHeight )
     270             :     {
     271           0 :         osl::MutexGuard aLock( m_aMutex );
     272           0 :         m_aFontHeight = aFontHeight;
     273             : 
     274           0 :         if ( m_xPopupMenu.is() )
     275             :         {
     276           0 :             SolarMutexGuard aSolarMutexGuard;
     277           0 :             setCurHeight( long( m_aFontHeight.Height * 10), m_xPopupMenu );
     278           0 :         }
     279           1 :     }
     280           1 : }
     281             : 
     282             : // XMenuListener
     283           0 : void FontSizeMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
     284             : {
     285           0 :     Sequence<PropertyValue>      aArgs;
     286             :     OSL_ENSURE(_xDispatch.is(),"FontSizeMenuController::impl_select: No dispatch");
     287           0 :     if ( _xDispatch.is() )
     288           0 :         _xDispatch->dispatch( aTargetURL, aArgs );
     289           0 : }
     290             : 
     291             : // XPopupMenuController
     292           0 : void FontSizeMenuController::impl_setPopupMenu()
     293             : {
     294           0 :     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     295           0 :     com::sun::star::util::URL aTargetURL;
     296             :     // Register for font name updates which gives us info about the current font!
     297           0 :     aTargetURL.Complete = ".uno:CharFontName";
     298           0 :     m_xURLTransformer->parseStrict( aTargetURL );
     299           0 :     m_xCurrentFontDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     300           0 : }
     301             : 
     302           1 : void SAL_CALL FontSizeMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
     303             : {
     304           1 :     osl::ClearableMutexGuard aLock( m_aMutex );
     305             : 
     306           1 :     throwIfDisposed();
     307             : 
     308           2 :     Reference< XDispatch > xDispatch( m_xCurrentFontDispatch );
     309           2 :     com::sun::star::util::URL aTargetURL;
     310           1 :     aTargetURL.Complete = ".uno:CharFontName";
     311           1 :     m_xURLTransformer->parseStrict( aTargetURL );
     312           1 :     aLock.clear();
     313             : 
     314           1 :     if ( xDispatch.is() )
     315             :     {
     316           0 :         xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
     317           0 :         xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
     318             :     }
     319             : 
     320           2 :     svt::PopupMenuControllerBase::updatePopupMenu();
     321           1 : }
     322             : }
     323             : 
     324             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11