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