1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uielement/fontmenucontroller.hxx>
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>
29 #include <vcl/menu.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
32 #include <vcl/i18nhelp.hxx>
33 #include <tools/urlobj.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <vcl/mnemonic.hxx>
36 #include <osl/mutex.hxx>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::frame
;
43 using namespace com::sun::star::beans
;
44 using namespace com::sun::star::util
;
48 static bool lcl_I18nCompareString(const OUString
& rStr1
, const OUString
& rStr2
)
50 const vcl::I18nHelper
& rI18nHelper
= Application::GetSettings().GetUILocaleI18nHelper();
51 return rI18nHelper
.CompareString( rStr1
, rStr2
) < 0;
57 DEFINE_XSERVICEINFO_MULTISERVICE_2 ( FontMenuController
,
59 SERVICENAME_POPUPMENUCONTROLLER
,
60 IMPLEMENTATIONNAME_FONTMENUCONTROLLER
63 DEFINE_INIT_SERVICE ( FontMenuController
, {} )
65 FontMenuController::FontMenuController( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xContext
) :
66 svt::PopupMenuControllerBase( xContext
)
70 FontMenuController::~FontMenuController()
75 void FontMenuController::fillPopupMenu( const Sequence
< OUString
>& rFontNameSeq
, Reference
< css::awt::XPopupMenu
>& rPopupMenu
)
77 const OUString
* pFontNameArray
= rFontNameSeq
.getConstArray();
78 VCLXPopupMenu
* pPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXMenu::GetImplementation( rPopupMenu
));
79 PopupMenu
* pVCLPopupMenu
= 0;
81 SolarMutexGuard aSolarMutexGuard
;
83 resetPopupMenu( rPopupMenu
);
85 pVCLPopupMenu
= static_cast<PopupMenu
*>(pPopupMenu
->GetMenu());
89 vector
<OUString
> aVector
;
90 aVector
.reserve(rFontNameSeq
.getLength());
91 for ( sal_uInt16 i
= 0; i
< rFontNameSeq
.getLength(); i
++ )
93 aVector
.push_back(MnemonicGenerator::EraseAllMnemonicChars(pFontNameArray
[i
]));
95 sort(aVector
.begin(), aVector
.end(), lcl_I18nCompareString
);
97 const OUString
aFontNameCommandPrefix( ".uno:CharFontName?CharFontName.FamilyName:string=" );
98 const sal_Int16 nCount
= (sal_Int16
)aVector
.size();
99 for ( sal_Int16 i
= 0; i
< nCount
; i
++ )
101 const OUString
& rName
= aVector
[i
];
102 m_xPopupMenu
->insertItem( i
+1, rName
, css::awt::MenuItemStyle::RADIOCHECK
| css::awt::MenuItemStyle::AUTOCHECK
, i
);
103 if ( rName
== m_aFontFamilyName
)
104 m_xPopupMenu
->checkItem( i
+1, sal_True
);
105 // use VCL popup menu pointer to set vital information that are not part of the awt implementation
106 OUStringBuffer
aCommandBuffer( aFontNameCommandPrefix
);
107 aCommandBuffer
.append( INetURLObject::encode( rName
, INetURLObject::PART_HTTP_QUERY
, INetURLObject::ENCODE_ALL
));
108 OUString aFontNameCommand
= aCommandBuffer
.makeStringAndClear();
109 pVCLPopupMenu
->SetItemCommand( i
+1, aFontNameCommand
); // Store font name into item command.
116 void SAL_CALL
FontMenuController::disposing( const EventObject
& ) throw ( RuntimeException
, std::exception
)
118 Reference
< css::awt::XMenuListener
> xHolder(( OWeakObject
*)this, UNO_QUERY
);
120 osl::MutexGuard
aLock( m_aMutex
);
123 m_xFontListDispatch
.clear();
125 if ( m_xPopupMenu
.is() )
126 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(( OWeakObject
*)this, UNO_QUERY
));
127 m_xPopupMenu
.clear();
131 void SAL_CALL
FontMenuController::statusChanged( const FeatureStateEvent
& Event
) throw ( RuntimeException
, std::exception
)
133 com::sun::star::awt::FontDescriptor aFontDescriptor
;
134 Sequence
< OUString
> aFontNameSeq
;
136 if ( Event
.State
>>= aFontDescriptor
)
138 osl::MutexGuard
aLock( m_aMutex
);
139 m_aFontFamilyName
= aFontDescriptor
.Name
;
141 else if ( Event
.State
>>= aFontNameSeq
)
143 osl::MutexGuard
aLock( m_aMutex
);
144 if ( m_xPopupMenu
.is() )
145 fillPopupMenu( aFontNameSeq
, m_xPopupMenu
);
150 void FontMenuController::impl_select(const Reference
< XDispatch
>& _xDispatch
,const ::com::sun::star::util::URL
& aTargetURL
)
152 Sequence
<PropertyValue
> aArgs
;
153 OSL_ENSURE(_xDispatch
.is(),"FontMenuController::impl_select: No dispatch");
154 if ( _xDispatch
.is() )
155 _xDispatch
->dispatch( aTargetURL
, aArgs
);
158 void SAL_CALL
FontMenuController::itemActivated( const css::awt::MenuEvent
& ) throw (RuntimeException
, std::exception
)
160 osl::MutexGuard
aLock( m_aMutex
);
162 if ( m_xPopupMenu
.is() )
164 // find new font name and set check mark!
165 sal_uInt16 nChecked
= 0;
166 sal_uInt16 nItemCount
= m_xPopupMenu
->getItemCount();
168 for( sal_uInt16 i
= 0; i
< nItemCount
; i
++ )
170 sal_uInt16 nItemId
= m_xPopupMenu
->getItemId( i
);
172 if ( m_xPopupMenu
->isItemChecked( nItemId
) )
175 OUString aText
= m_xPopupMenu
->getItemText( nItemId
);
177 // TODO: must be replaced by implementation of VCL, when available
178 sal_Int32 nIndex
= aText
.indexOf( (sal_Unicode
)'~' );
180 aText
= aText
.replaceAt( nIndex
, 1, aEmpty
);
181 // TODO: must be replaced by implementation of VCL, when available
183 if ( aText
== m_aFontFamilyName
)
185 m_xPopupMenu
->checkItem( nItemId
, sal_True
);
191 m_xPopupMenu
->checkItem( nChecked
, sal_False
);
195 // XPopupMenuController
196 void FontMenuController::impl_setPopupMenu()
198 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
200 com::sun::star::util::URL aTargetURL
;
201 // Register for font list updates to get the current font list from the controller
202 aTargetURL
.Complete
= ".uno:FontNameList";
203 m_xURLTransformer
->parseStrict( aTargetURL
);
204 m_xFontListDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
207 void SAL_CALL
FontMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
209 svt::PopupMenuControllerBase::updatePopupMenu();
211 osl::ClearableMutexGuard
aLock( m_aMutex
);
212 Reference
< XDispatch
> xDispatch( m_xFontListDispatch
);
213 com::sun::star::util::URL aTargetURL
;
214 aTargetURL
.Complete
= ".uno:FontNameList";
215 m_xURLTransformer
->parseStrict( aTargetURL
);
218 if ( xDispatch
.is() )
220 xDispatch
->addStatusListener( (static_cast< XStatusListener
* >(this)), aTargetURL
);
221 xDispatch
->removeStatusListener( (static_cast< XStatusListener
* >(this)), aTargetURL
);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */