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 <opendoccontrols.hxx>
22 #include <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
27 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
28 #include <com/sun/star/graphic/XGraphic.hpp>
29 #include <com/sun/star/ui/XImageManager.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <vcl/commandinfoprovider.hxx>
33 #include <unotools/historyoptions.hxx>
34 #include <comphelper/sequenceashashmap.hxx>
35 #include <tools/urlobj.hxx>
36 #include <osl/diagnose.h>
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::Exception
;
45 using ::com::sun::star::uno::Sequence
;
46 using ::com::sun::star::uno::XComponentContext
;
47 using ::com::sun::star::container::XNameAccess
;
48 using ::com::sun::star::beans::PropertyValue
;
49 using ::com::sun::star::ui::theModuleUIConfigurationManagerSupplier
;
50 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier
;
51 using ::com::sun::star::ui::XUIConfigurationManager
;
52 using ::com::sun::star::ui::XImageManager
;
53 using ::com::sun::star::graphic::XGraphic
;
55 Reference
< XGraphic
> GetCommandIcon( const OUString
& sCommandURL
, const OUString
& _rModuleName
)
61 // Retrieve popup menu labels
62 const Reference
< css::uno::XComponentContext
>& xContext( ::comphelper::getProcessComponentContext() );
66 Reference
< XModuleUIConfigurationManagerSupplier
> xSupplier(
67 theModuleUIConfigurationManagerSupplier::get(xContext
) );
69 Reference
< XUIConfigurationManager
> xManager( xSupplier
->getUIConfigurationManager( _rModuleName
) );
70 Reference
< XImageManager
> xImageManager
;
72 xImageManager
.set(xManager
->getImageManager(), css::uno::UNO_QUERY
);
73 if ( !xImageManager
.is() )
76 Sequence
< OUString
> aCommandList( &sCommandURL
, 1 );
77 Sequence
<Reference
< XGraphic
> > xIconList( xImageManager
->getImages( 0, aCommandList
) );
78 if ( !xIconList
.hasElements() )
85 catch ( Exception
& ) {}
93 OpenDocumentButton::OpenDocumentButton(std::unique_ptr
<weld::Button
> xControl
, const OUString
& _rAsciiModuleName
)
94 : m_xControl(std::move(xControl
))
96 // our label should equal the UI text of the "Open" command
97 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(u
".uno:Open"_ustr
, _rAsciiModuleName
);
98 OUString
sLabel(vcl::CommandInfoProvider::GetLabelForCommand(aProperties
));
99 m_xControl
->set_label(" " + sLabel
.replaceAll("~", ""));
101 // Place icon left of text and both centered in the button.
102 m_xControl
->set_image(GetCommandIcon(u
".uno:Open"_ustr
, _rAsciiModuleName
));
105 // OpenDocumentListBox
107 OpenDocumentListBox::OpenDocumentListBox(std::unique_ptr
<weld::ComboBox
> xControl
, const char* _pAsciiModuleName
)
108 : m_xControl(std::move(xControl
))
110 // we need to limit the max auto width feature of the filter box
111 int nWidth
= m_xControl
->get_approximate_digit_width() * 50;
112 m_xControl
->set_size_request(nWidth
, -1);
114 impl_init( _pAsciiModuleName
);
117 void OpenDocumentListBox::impl_init( const char* _pAsciiModuleName
)
119 OSL_ENSURE( _pAsciiModuleName
, "OpenDocumentListBox::impl_init: invalid module name!" );
121 std::vector
< SvtHistoryOptions::HistoryItem
> aHistory
= SvtHistoryOptions::GetList( EHistoryType::PickList
);
122 Reference
< XNameAccess
> xFilterFactory
;
123 xFilterFactory
.set(::comphelper::getProcessServiceFactory()->createInstance(
124 u
"com.sun.star.document.FilterFactory"_ustr
), css::uno::UNO_QUERY
);
126 for ( const SvtHistoryOptions::HistoryItem
& rHistoryItem
: aHistory
)
130 // Get the current history item's properties.
131 OUString sURL
= rHistoryItem
.sURL
;
132 OUString sFilter
= rHistoryItem
.sFilter
;
133 OUString sTitle
= rHistoryItem
.sTitle
;
135 // If the entry is a Base file then insert it into the
136 // history list and the list box.
137 Sequence
< PropertyValue
> aProps
;
138 xFilterFactory
->getByName( sFilter
) >>= aProps
;
140 ::comphelper::SequenceAsHashMap
aFilterProperties( aProps
);
141 OUString sDocumentService
= aFilterProperties
.getUnpackedValueOrDefault(
142 u
"DocumentService"_ustr
, OUString() );
143 if ( sDocumentService
.equalsAscii( _pAsciiModuleName
) )
145 // yes, it's a Base document
147 aURL
.SetSmartURL( sURL
);
149 if ( sTitle
.isEmpty() )
150 sTitle
= aURL
.getBase( INetURLObject::LAST_SEGMENT
, true, INetURLObject::DecodeMechanism::Unambiguous
);
152 OUString sDecodedURL
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
);
154 m_xControl
->append_text(sTitle
);
155 m_aURLs
.emplace_back(sDecodedURL
, sFilter
);
158 catch( Exception
& ) {}
162 OUString
OpenDocumentListBox::GetSelectedDocumentURL() const
165 sal_Int32 nSelected
= m_xControl
->get_active();
167 sURL
= impl_getDocumentAtIndex( nSelected
).first
;
171 const OpenDocumentListBox::StringPair
& OpenDocumentListBox::impl_getDocumentAtIndex( sal_uInt16 _nListIndex
) const
173 return m_aURLs
[_nListIndex
];
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */