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